]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Set up the bidi iterator for iterating display strings and overlay strings.
[gnu-emacs] / 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 EMACS_INT eob = STRINGP (object) ? string->schars : ZV;
3117 EMACS_INT begb = STRINGP (object) ? 0 : BEGV;
3118 EMACS_INT bufpos, charpos = CHARPOS (*position);
3119 struct text_pos tpos;
3120
3121 if (charpos >= eob
3122 /* We don't support display properties whose values are strings
3123 that have display string properties. */
3124 || string->from_disp_str
3125 /* C strings cannot have display properties. */
3126 || (string->s && !STRINGP (object)))
3127 return eob;
3128
3129 /* If the character at CHARPOS is where the display string begins,
3130 return CHARPOS. */
3131 pos = make_number (charpos);
3132 if (STRINGP (object))
3133 bufpos = string->bufpos;
3134 else
3135 bufpos = charpos;
3136 tpos = *position;
3137 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3138 && (charpos <= begb
3139 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3140 object),
3141 spec))
3142 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3143 frame_window_p))
3144 return charpos;
3145
3146 /* Look forward for the first character with a `display' property
3147 that will replace the underlying text when displayed. */
3148 do {
3149 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3150 CHARPOS (tpos) = XFASTINT (pos);
3151 if (STRINGP (object))
3152 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3153 else
3154 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3155 if (CHARPOS (tpos) >= eob)
3156 break;
3157 spec = Fget_char_property (pos, Qdisplay, object);
3158 if (!STRINGP (object))
3159 bufpos = CHARPOS (tpos);
3160 } while (NILP (spec)
3161 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3162 frame_window_p));
3163
3164 return CHARPOS (tpos);
3165 }
3166
3167 /* Return the character position of the end of the display string that
3168 started at CHARPOS. A display string is either an overlay with
3169 `display' property whose value is a string or a `display' text
3170 property whose value is a string. */
3171 EMACS_INT
3172 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3173 {
3174 /* OBJECT = nil means current buffer. */
3175 Lisp_Object object =
3176 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3177 Lisp_Object pos = make_number (charpos);
3178 EMACS_INT eob = STRINGP (object) ? string->schars : ZV;
3179
3180 if (charpos >= eob || (string->s && !STRINGP (object)))
3181 return eob;
3182
3183 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3184 abort ();
3185
3186 /* Look forward for the first character where the `display' property
3187 changes. */
3188 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3189
3190 return XFASTINT (pos);
3191 }
3192
3193
3194 \f
3195 /***********************************************************************
3196 Fontification
3197 ***********************************************************************/
3198
3199 /* Handle changes in the `fontified' property of the current buffer by
3200 calling hook functions from Qfontification_functions to fontify
3201 regions of text. */
3202
3203 static enum prop_handled
3204 handle_fontified_prop (struct it *it)
3205 {
3206 Lisp_Object prop, pos;
3207 enum prop_handled handled = HANDLED_NORMALLY;
3208
3209 if (!NILP (Vmemory_full))
3210 return handled;
3211
3212 /* Get the value of the `fontified' property at IT's current buffer
3213 position. (The `fontified' property doesn't have a special
3214 meaning in strings.) If the value is nil, call functions from
3215 Qfontification_functions. */
3216 if (!STRINGP (it->string)
3217 && it->s == NULL
3218 && !NILP (Vfontification_functions)
3219 && !NILP (Vrun_hooks)
3220 && (pos = make_number (IT_CHARPOS (*it)),
3221 prop = Fget_char_property (pos, Qfontified, Qnil),
3222 /* Ignore the special cased nil value always present at EOB since
3223 no amount of fontifying will be able to change it. */
3224 NILP (prop) && IT_CHARPOS (*it) < Z))
3225 {
3226 int count = SPECPDL_INDEX ();
3227 Lisp_Object val;
3228 struct buffer *obuf = current_buffer;
3229 int begv = BEGV, zv = ZV;
3230 int old_clip_changed = current_buffer->clip_changed;
3231
3232 val = Vfontification_functions;
3233 specbind (Qfontification_functions, Qnil);
3234
3235 xassert (it->end_charpos == ZV);
3236
3237 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3238 safe_call1 (val, pos);
3239 else
3240 {
3241 Lisp_Object fns, fn;
3242 struct gcpro gcpro1, gcpro2;
3243
3244 fns = Qnil;
3245 GCPRO2 (val, fns);
3246
3247 for (; CONSP (val); val = XCDR (val))
3248 {
3249 fn = XCAR (val);
3250
3251 if (EQ (fn, Qt))
3252 {
3253 /* A value of t indicates this hook has a local
3254 binding; it means to run the global binding too.
3255 In a global value, t should not occur. If it
3256 does, we must ignore it to avoid an endless
3257 loop. */
3258 for (fns = Fdefault_value (Qfontification_functions);
3259 CONSP (fns);
3260 fns = XCDR (fns))
3261 {
3262 fn = XCAR (fns);
3263 if (!EQ (fn, Qt))
3264 safe_call1 (fn, pos);
3265 }
3266 }
3267 else
3268 safe_call1 (fn, pos);
3269 }
3270
3271 UNGCPRO;
3272 }
3273
3274 unbind_to (count, Qnil);
3275
3276 /* Fontification functions routinely call `save-restriction'.
3277 Normally, this tags clip_changed, which can confuse redisplay
3278 (see discussion in Bug#6671). Since we don't perform any
3279 special handling of fontification changes in the case where
3280 `save-restriction' isn't called, there's no point doing so in
3281 this case either. So, if the buffer's restrictions are
3282 actually left unchanged, reset clip_changed. */
3283 if (obuf == current_buffer)
3284 {
3285 if (begv == BEGV && zv == ZV)
3286 current_buffer->clip_changed = old_clip_changed;
3287 }
3288 /* There isn't much we can reasonably do to protect against
3289 misbehaving fontification, but here's a fig leaf. */
3290 else if (!NILP (BVAR (obuf, name)))
3291 set_buffer_internal_1 (obuf);
3292
3293 /* The fontification code may have added/removed text.
3294 It could do even a lot worse, but let's at least protect against
3295 the most obvious case where only the text past `pos' gets changed',
3296 as is/was done in grep.el where some escapes sequences are turned
3297 into face properties (bug#7876). */
3298 it->end_charpos = ZV;
3299
3300 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3301 something. This avoids an endless loop if they failed to
3302 fontify the text for which reason ever. */
3303 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3304 handled = HANDLED_RECOMPUTE_PROPS;
3305 }
3306
3307 return handled;
3308 }
3309
3310
3311 \f
3312 /***********************************************************************
3313 Faces
3314 ***********************************************************************/
3315
3316 /* Set up iterator IT from face properties at its current position.
3317 Called from handle_stop. */
3318
3319 static enum prop_handled
3320 handle_face_prop (struct it *it)
3321 {
3322 int new_face_id;
3323 EMACS_INT next_stop;
3324
3325 if (!STRINGP (it->string))
3326 {
3327 new_face_id
3328 = face_at_buffer_position (it->w,
3329 IT_CHARPOS (*it),
3330 it->region_beg_charpos,
3331 it->region_end_charpos,
3332 &next_stop,
3333 (IT_CHARPOS (*it)
3334 + TEXT_PROP_DISTANCE_LIMIT),
3335 0, it->base_face_id);
3336
3337 /* Is this a start of a run of characters with box face?
3338 Caveat: this can be called for a freshly initialized
3339 iterator; face_id is -1 in this case. We know that the new
3340 face will not change until limit, i.e. if the new face has a
3341 box, all characters up to limit will have one. But, as
3342 usual, we don't know whether limit is really the end. */
3343 if (new_face_id != it->face_id)
3344 {
3345 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3346
3347 /* If new face has a box but old face has not, this is
3348 the start of a run of characters with box, i.e. it has
3349 a shadow on the left side. The value of face_id of the
3350 iterator will be -1 if this is the initial call that gets
3351 the face. In this case, we have to look in front of IT's
3352 position and see whether there is a face != new_face_id. */
3353 it->start_of_box_run_p
3354 = (new_face->box != FACE_NO_BOX
3355 && (it->face_id >= 0
3356 || IT_CHARPOS (*it) == BEG
3357 || new_face_id != face_before_it_pos (it)));
3358 it->face_box_p = new_face->box != FACE_NO_BOX;
3359 }
3360 }
3361 else
3362 {
3363 int base_face_id;
3364 EMACS_INT bufpos;
3365 int i;
3366 Lisp_Object from_overlay
3367 = (it->current.overlay_string_index >= 0
3368 ? it->string_overlays[it->current.overlay_string_index]
3369 : Qnil);
3370
3371 /* See if we got to this string directly or indirectly from
3372 an overlay property. That includes the before-string or
3373 after-string of an overlay, strings in display properties
3374 provided by an overlay, their text properties, etc.
3375
3376 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3377 if (! NILP (from_overlay))
3378 for (i = it->sp - 1; i >= 0; i--)
3379 {
3380 if (it->stack[i].current.overlay_string_index >= 0)
3381 from_overlay
3382 = it->string_overlays[it->stack[i].current.overlay_string_index];
3383 else if (! NILP (it->stack[i].from_overlay))
3384 from_overlay = it->stack[i].from_overlay;
3385
3386 if (!NILP (from_overlay))
3387 break;
3388 }
3389
3390 if (! NILP (from_overlay))
3391 {
3392 bufpos = IT_CHARPOS (*it);
3393 /* For a string from an overlay, the base face depends
3394 only on text properties and ignores overlays. */
3395 base_face_id
3396 = face_for_overlay_string (it->w,
3397 IT_CHARPOS (*it),
3398 it->region_beg_charpos,
3399 it->region_end_charpos,
3400 &next_stop,
3401 (IT_CHARPOS (*it)
3402 + TEXT_PROP_DISTANCE_LIMIT),
3403 0,
3404 from_overlay);
3405 }
3406 else
3407 {
3408 bufpos = 0;
3409
3410 /* For strings from a `display' property, use the face at
3411 IT's current buffer position as the base face to merge
3412 with, so that overlay strings appear in the same face as
3413 surrounding text, unless they specify their own
3414 faces. */
3415 base_face_id = underlying_face_id (it);
3416 }
3417
3418 new_face_id = face_at_string_position (it->w,
3419 it->string,
3420 IT_STRING_CHARPOS (*it),
3421 bufpos,
3422 it->region_beg_charpos,
3423 it->region_end_charpos,
3424 &next_stop,
3425 base_face_id, 0);
3426
3427 /* Is this a start of a run of characters with box? Caveat:
3428 this can be called for a freshly allocated iterator; face_id
3429 is -1 is this case. We know that the new face will not
3430 change until the next check pos, i.e. if the new face has a
3431 box, all characters up to that position will have a
3432 box. But, as usual, we don't know whether that position
3433 is really the end. */
3434 if (new_face_id != it->face_id)
3435 {
3436 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3437 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3438
3439 /* If new face has a box but old face hasn't, this is the
3440 start of a run of characters with box, i.e. it has a
3441 shadow on the left side. */
3442 it->start_of_box_run_p
3443 = new_face->box && (old_face == NULL || !old_face->box);
3444 it->face_box_p = new_face->box != FACE_NO_BOX;
3445 }
3446 }
3447
3448 it->face_id = new_face_id;
3449 return HANDLED_NORMALLY;
3450 }
3451
3452
3453 /* Return the ID of the face ``underlying'' IT's current position,
3454 which is in a string. If the iterator is associated with a
3455 buffer, return the face at IT's current buffer position.
3456 Otherwise, use the iterator's base_face_id. */
3457
3458 static int
3459 underlying_face_id (struct it *it)
3460 {
3461 int face_id = it->base_face_id, i;
3462
3463 xassert (STRINGP (it->string));
3464
3465 for (i = it->sp - 1; i >= 0; --i)
3466 if (NILP (it->stack[i].string))
3467 face_id = it->stack[i].face_id;
3468
3469 return face_id;
3470 }
3471
3472
3473 /* Compute the face one character before or after the current position
3474 of IT, in the visual order. BEFORE_P non-zero means get the face
3475 in front (to the left in L2R paragraphs, to the right in R2L
3476 paragraphs) of IT's screen position. Value is the ID of the face. */
3477
3478 static int
3479 face_before_or_after_it_pos (struct it *it, int before_p)
3480 {
3481 int face_id, limit;
3482 EMACS_INT next_check_charpos;
3483 struct it it_copy;
3484
3485 xassert (it->s == NULL);
3486
3487 if (STRINGP (it->string))
3488 {
3489 EMACS_INT bufpos, charpos;
3490 int base_face_id;
3491
3492 /* No face change past the end of the string (for the case
3493 we are padding with spaces). No face change before the
3494 string start. */
3495 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3496 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3497 return it->face_id;
3498
3499 if (!it->bidi_p)
3500 {
3501 /* Set charpos to the position before or after IT's current
3502 position, in the logical order, which in the non-bidi
3503 case is the same as the visual order. */
3504 if (before_p)
3505 charpos = IT_STRING_CHARPOS (*it) - 1;
3506 else if (it->what == IT_COMPOSITION)
3507 /* For composition, we must check the character after the
3508 composition. */
3509 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3510 else
3511 charpos = IT_STRING_CHARPOS (*it) + 1;
3512 }
3513 else
3514 {
3515 if (before_p)
3516 {
3517 /* With bidi iteration, the character before the current
3518 in the visual order cannot be found by simple
3519 iteration, because "reverse" reordering is not
3520 supported. Instead, we need to use the move_it_*
3521 family of functions. */
3522 /* Ignore face changes before the first visible
3523 character on this display line. */
3524 if (it->current_x <= it->first_visible_x)
3525 return it->face_id;
3526 it_copy = *it;
3527 /* Implementation note: Since move_it_in_display_line
3528 works in the iterator geometry, and thinks the first
3529 character is always the leftmost, even in R2L lines,
3530 we don't need to distinguish between the R2L and L2R
3531 cases here. */
3532 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3533 it_copy.current_x - 1, MOVE_TO_X);
3534 charpos = IT_STRING_CHARPOS (it_copy);
3535 }
3536 else
3537 {
3538 /* Set charpos to the string position of the character
3539 that comes after IT's current position in the visual
3540 order. */
3541 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3542
3543 it_copy = *it;
3544 while (n--)
3545 bidi_move_to_visually_next (&it_copy.bidi_it);
3546
3547 charpos = it_copy.bidi_it.charpos;
3548 }
3549 }
3550 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3551
3552 if (it->current.overlay_string_index >= 0)
3553 bufpos = IT_CHARPOS (*it);
3554 else
3555 bufpos = 0;
3556
3557 base_face_id = underlying_face_id (it);
3558
3559 /* Get the face for ASCII, or unibyte. */
3560 face_id = face_at_string_position (it->w,
3561 it->string,
3562 charpos,
3563 bufpos,
3564 it->region_beg_charpos,
3565 it->region_end_charpos,
3566 &next_check_charpos,
3567 base_face_id, 0);
3568
3569 /* Correct the face for charsets different from ASCII. Do it
3570 for the multibyte case only. The face returned above is
3571 suitable for unibyte text if IT->string is unibyte. */
3572 if (STRING_MULTIBYTE (it->string))
3573 {
3574 struct text_pos pos1 = string_pos (charpos, it->string);
3575 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3576 int c, len;
3577 struct face *face = FACE_FROM_ID (it->f, face_id);
3578
3579 c = string_char_and_length (p, &len);
3580 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3581 }
3582 }
3583 else
3584 {
3585 struct text_pos pos;
3586
3587 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3588 || (IT_CHARPOS (*it) <= BEGV && before_p))
3589 return it->face_id;
3590
3591 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3592 pos = it->current.pos;
3593
3594 if (!it->bidi_p)
3595 {
3596 if (before_p)
3597 DEC_TEXT_POS (pos, it->multibyte_p);
3598 else
3599 {
3600 if (it->what == IT_COMPOSITION)
3601 {
3602 /* For composition, we must check the position after
3603 the composition. */
3604 pos.charpos += it->cmp_it.nchars;
3605 pos.bytepos += it->len;
3606 }
3607 else
3608 INC_TEXT_POS (pos, it->multibyte_p);
3609 }
3610 }
3611 else
3612 {
3613 if (before_p)
3614 {
3615 /* With bidi iteration, the character before the current
3616 in the visual order cannot be found by simple
3617 iteration, because "reverse" reordering is not
3618 supported. Instead, we need to use the move_it_*
3619 family of functions. */
3620 /* Ignore face changes before the first visible
3621 character on this display line. */
3622 if (it->current_x <= it->first_visible_x)
3623 return it->face_id;
3624 it_copy = *it;
3625 /* Implementation note: Since move_it_in_display_line
3626 works in the iterator geometry, and thinks the first
3627 character is always the leftmost, even in R2L lines,
3628 we don't need to distinguish between the R2L and L2R
3629 cases here. */
3630 move_it_in_display_line (&it_copy, ZV,
3631 it_copy.current_x - 1, MOVE_TO_X);
3632 pos = it_copy.current.pos;
3633 }
3634 else
3635 {
3636 /* Set charpos to the buffer position of the character
3637 that comes after IT's current position in the visual
3638 order. */
3639 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3640
3641 it_copy = *it;
3642 while (n--)
3643 bidi_move_to_visually_next (&it_copy.bidi_it);
3644
3645 SET_TEXT_POS (pos,
3646 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3647 }
3648 }
3649 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3650
3651 /* Determine face for CHARSET_ASCII, or unibyte. */
3652 face_id = face_at_buffer_position (it->w,
3653 CHARPOS (pos),
3654 it->region_beg_charpos,
3655 it->region_end_charpos,
3656 &next_check_charpos,
3657 limit, 0, -1);
3658
3659 /* Correct the face for charsets different from ASCII. Do it
3660 for the multibyte case only. The face returned above is
3661 suitable for unibyte text if current_buffer is unibyte. */
3662 if (it->multibyte_p)
3663 {
3664 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3665 struct face *face = FACE_FROM_ID (it->f, face_id);
3666 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3667 }
3668 }
3669
3670 return face_id;
3671 }
3672
3673
3674 \f
3675 /***********************************************************************
3676 Invisible text
3677 ***********************************************************************/
3678
3679 /* Set up iterator IT from invisible properties at its current
3680 position. Called from handle_stop. */
3681
3682 static enum prop_handled
3683 handle_invisible_prop (struct it *it)
3684 {
3685 enum prop_handled handled = HANDLED_NORMALLY;
3686
3687 if (STRINGP (it->string))
3688 {
3689 Lisp_Object prop, end_charpos, limit, charpos;
3690
3691 /* Get the value of the invisible text property at the
3692 current position. Value will be nil if there is no such
3693 property. */
3694 charpos = make_number (IT_STRING_CHARPOS (*it));
3695 prop = Fget_text_property (charpos, Qinvisible, it->string);
3696
3697 if (!NILP (prop)
3698 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3699 {
3700 EMACS_INT endpos;
3701
3702 handled = HANDLED_RECOMPUTE_PROPS;
3703
3704 /* Get the position at which the next change of the
3705 invisible text property can be found in IT->string.
3706 Value will be nil if the property value is the same for
3707 all the rest of IT->string. */
3708 XSETINT (limit, SCHARS (it->string));
3709 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3710 it->string, limit);
3711
3712 /* Text at current position is invisible. The next
3713 change in the property is at position end_charpos.
3714 Move IT's current position to that position. */
3715 if (INTEGERP (end_charpos)
3716 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3717 {
3718 struct text_pos old;
3719 EMACS_INT oldpos;
3720
3721 old = it->current.string_pos;
3722 oldpos = CHARPOS (old);
3723 if (it->bidi_p)
3724 {
3725 if (it->bidi_it.first_elt
3726 && it->bidi_it.charpos < SCHARS (it->string))
3727 bidi_paragraph_init (it->paragraph_embedding,
3728 &it->bidi_it, 1);
3729 /* Bidi-iterate out of the invisible text. */
3730 do
3731 {
3732 bidi_move_to_visually_next (&it->bidi_it);
3733 }
3734 while (oldpos <= it->bidi_it.charpos
3735 && it->bidi_it.charpos < endpos);
3736
3737 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3738 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3739 if (IT_CHARPOS (*it) >= endpos)
3740 it->prev_stop = endpos;
3741 }
3742 else
3743 {
3744 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3745 compute_string_pos (&it->current.string_pos, old, it->string);
3746 }
3747 }
3748 else
3749 {
3750 /* The rest of the string is invisible. If this is an
3751 overlay string, proceed with the next overlay string
3752 or whatever comes and return a character from there. */
3753 if (it->current.overlay_string_index >= 0)
3754 {
3755 next_overlay_string (it);
3756 /* Don't check for overlay strings when we just
3757 finished processing them. */
3758 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3759 }
3760 else
3761 {
3762 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3763 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3764 }
3765 }
3766 }
3767 }
3768 else
3769 {
3770 int invis_p;
3771 EMACS_INT newpos, next_stop, start_charpos, tem;
3772 Lisp_Object pos, prop, overlay;
3773
3774 /* First of all, is there invisible text at this position? */
3775 tem = start_charpos = IT_CHARPOS (*it);
3776 pos = make_number (tem);
3777 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3778 &overlay);
3779 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3780
3781 /* If we are on invisible text, skip over it. */
3782 if (invis_p && start_charpos < it->end_charpos)
3783 {
3784 /* Record whether we have to display an ellipsis for the
3785 invisible text. */
3786 int display_ellipsis_p = invis_p == 2;
3787
3788 handled = HANDLED_RECOMPUTE_PROPS;
3789
3790 /* Loop skipping over invisible text. The loop is left at
3791 ZV or with IT on the first char being visible again. */
3792 do
3793 {
3794 /* Try to skip some invisible text. Return value is the
3795 position reached which can be equal to where we start
3796 if there is nothing invisible there. This skips both
3797 over invisible text properties and overlays with
3798 invisible property. */
3799 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3800
3801 /* If we skipped nothing at all we weren't at invisible
3802 text in the first place. If everything to the end of
3803 the buffer was skipped, end the loop. */
3804 if (newpos == tem || newpos >= ZV)
3805 invis_p = 0;
3806 else
3807 {
3808 /* We skipped some characters but not necessarily
3809 all there are. Check if we ended up on visible
3810 text. Fget_char_property returns the property of
3811 the char before the given position, i.e. if we
3812 get invis_p = 0, this means that the char at
3813 newpos is visible. */
3814 pos = make_number (newpos);
3815 prop = Fget_char_property (pos, Qinvisible, it->window);
3816 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3817 }
3818
3819 /* If we ended up on invisible text, proceed to
3820 skip starting with next_stop. */
3821 if (invis_p)
3822 tem = next_stop;
3823
3824 /* If there are adjacent invisible texts, don't lose the
3825 second one's ellipsis. */
3826 if (invis_p == 2)
3827 display_ellipsis_p = 1;
3828 }
3829 while (invis_p);
3830
3831 /* The position newpos is now either ZV or on visible text. */
3832 if (it->bidi_p && newpos < ZV)
3833 {
3834 /* With bidi iteration, the region of invisible text
3835 could start and/or end in the middle of a non-base
3836 embedding level. Therefore, we need to skip
3837 invisible text using the bidi iterator, starting at
3838 IT's current position, until we find ourselves
3839 outside the invisible text. Skipping invisible text
3840 _after_ bidi iteration avoids affecting the visual
3841 order of the displayed text when invisible properties
3842 are added or removed. */
3843 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3844 {
3845 /* If we were `reseat'ed to a new paragraph,
3846 determine the paragraph base direction. We need
3847 to do it now because next_element_from_buffer may
3848 not have a chance to do it, if we are going to
3849 skip any text at the beginning, which resets the
3850 FIRST_ELT flag. */
3851 bidi_paragraph_init (it->paragraph_embedding,
3852 &it->bidi_it, 1);
3853 }
3854 do
3855 {
3856 bidi_move_to_visually_next (&it->bidi_it);
3857 }
3858 while (it->stop_charpos <= it->bidi_it.charpos
3859 && it->bidi_it.charpos < newpos);
3860 IT_CHARPOS (*it) = it->bidi_it.charpos;
3861 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3862 /* If we overstepped NEWPOS, record its position in the
3863 iterator, so that we skip invisible text if later the
3864 bidi iteration lands us in the invisible region
3865 again. */
3866 if (IT_CHARPOS (*it) >= newpos)
3867 it->prev_stop = newpos;
3868 }
3869 else
3870 {
3871 IT_CHARPOS (*it) = newpos;
3872 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3873 }
3874
3875 /* If there are before-strings at the start of invisible
3876 text, and the text is invisible because of a text
3877 property, arrange to show before-strings because 20.x did
3878 it that way. (If the text is invisible because of an
3879 overlay property instead of a text property, this is
3880 already handled in the overlay code.) */
3881 if (NILP (overlay)
3882 && get_overlay_strings (it, it->stop_charpos))
3883 {
3884 handled = HANDLED_RECOMPUTE_PROPS;
3885 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3886 }
3887 else if (display_ellipsis_p)
3888 {
3889 /* Make sure that the glyphs of the ellipsis will get
3890 correct `charpos' values. If we would not update
3891 it->position here, the glyphs would belong to the
3892 last visible character _before_ the invisible
3893 text, which confuses `set_cursor_from_row'.
3894
3895 We use the last invisible position instead of the
3896 first because this way the cursor is always drawn on
3897 the first "." of the ellipsis, whenever PT is inside
3898 the invisible text. Otherwise the cursor would be
3899 placed _after_ the ellipsis when the point is after the
3900 first invisible character. */
3901 if (!STRINGP (it->object))
3902 {
3903 it->position.charpos = newpos - 1;
3904 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3905 }
3906 it->ellipsis_p = 1;
3907 /* Let the ellipsis display before
3908 considering any properties of the following char.
3909 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3910 handled = HANDLED_RETURN;
3911 }
3912 }
3913 }
3914
3915 return handled;
3916 }
3917
3918
3919 /* Make iterator IT return `...' next.
3920 Replaces LEN characters from buffer. */
3921
3922 static void
3923 setup_for_ellipsis (struct it *it, int len)
3924 {
3925 /* Use the display table definition for `...'. Invalid glyphs
3926 will be handled by the method returning elements from dpvec. */
3927 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3928 {
3929 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3930 it->dpvec = v->contents;
3931 it->dpend = v->contents + v->header.size;
3932 }
3933 else
3934 {
3935 /* Default `...'. */
3936 it->dpvec = default_invis_vector;
3937 it->dpend = default_invis_vector + 3;
3938 }
3939
3940 it->dpvec_char_len = len;
3941 it->current.dpvec_index = 0;
3942 it->dpvec_face_id = -1;
3943
3944 /* Remember the current face id in case glyphs specify faces.
3945 IT's face is restored in set_iterator_to_next.
3946 saved_face_id was set to preceding char's face in handle_stop. */
3947 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3948 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3949
3950 it->method = GET_FROM_DISPLAY_VECTOR;
3951 it->ellipsis_p = 1;
3952 }
3953
3954
3955 \f
3956 /***********************************************************************
3957 'display' property
3958 ***********************************************************************/
3959
3960 /* Set up iterator IT from `display' property at its current position.
3961 Called from handle_stop.
3962 We return HANDLED_RETURN if some part of the display property
3963 overrides the display of the buffer text itself.
3964 Otherwise we return HANDLED_NORMALLY. */
3965
3966 static enum prop_handled
3967 handle_display_prop (struct it *it)
3968 {
3969 Lisp_Object propval, object, overlay;
3970 struct text_pos *position;
3971 EMACS_INT bufpos;
3972 /* Nonzero if some property replaces the display of the text itself. */
3973 int display_replaced_p = 0;
3974
3975 if (STRINGP (it->string))
3976 {
3977 object = it->string;
3978 position = &it->current.string_pos;
3979 bufpos = CHARPOS (it->current.pos);
3980 }
3981 else
3982 {
3983 XSETWINDOW (object, it->w);
3984 position = &it->current.pos;
3985 bufpos = CHARPOS (*position);
3986 }
3987
3988 /* Reset those iterator values set from display property values. */
3989 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3990 it->space_width = Qnil;
3991 it->font_height = Qnil;
3992 it->voffset = 0;
3993
3994 /* We don't support recursive `display' properties, i.e. string
3995 values that have a string `display' property, that have a string
3996 `display' property etc. */
3997 if (!it->string_from_display_prop_p)
3998 it->area = TEXT_AREA;
3999
4000 propval = get_char_property_and_overlay (make_number (position->charpos),
4001 Qdisplay, object, &overlay);
4002 if (NILP (propval))
4003 return HANDLED_NORMALLY;
4004 /* Now OVERLAY is the overlay that gave us this property, or nil
4005 if it was a text property. */
4006
4007 if (!STRINGP (it->string))
4008 object = it->w->buffer;
4009
4010 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4011 position, bufpos,
4012 FRAME_WINDOW_P (it->f));
4013
4014 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4015 }
4016
4017 /* Subroutine of handle_display_prop. Returns non-zero if the display
4018 specification in SPEC is a replacing specification, i.e. it would
4019 replace the text covered by `display' property with something else,
4020 such as an image or a display string.
4021
4022 See handle_single_display_spec for documentation of arguments.
4023 frame_window_p is non-zero if the window being redisplayed is on a
4024 GUI frame; this argument is used only if IT is NULL, see below.
4025
4026 IT can be NULL, if this is called by the bidi reordering code
4027 through compute_display_string_pos, which see. In that case, this
4028 function only examines SPEC, but does not otherwise "handle" it, in
4029 the sense that it doesn't set up members of IT from the display
4030 spec. */
4031 static int
4032 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4033 Lisp_Object overlay, struct text_pos *position,
4034 EMACS_INT bufpos, int frame_window_p)
4035 {
4036 int replacing_p = 0;
4037
4038 if (CONSP (spec)
4039 /* Simple specerties. */
4040 && !EQ (XCAR (spec), Qimage)
4041 && !EQ (XCAR (spec), Qspace)
4042 && !EQ (XCAR (spec), Qwhen)
4043 && !EQ (XCAR (spec), Qslice)
4044 && !EQ (XCAR (spec), Qspace_width)
4045 && !EQ (XCAR (spec), Qheight)
4046 && !EQ (XCAR (spec), Qraise)
4047 /* Marginal area specifications. */
4048 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4049 && !EQ (XCAR (spec), Qleft_fringe)
4050 && !EQ (XCAR (spec), Qright_fringe)
4051 && !NILP (XCAR (spec)))
4052 {
4053 for (; CONSP (spec); spec = XCDR (spec))
4054 {
4055 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
4056 position, bufpos, replacing_p,
4057 frame_window_p))
4058 {
4059 replacing_p = 1;
4060 /* If some text in a string is replaced, `position' no
4061 longer points to the position of `object'. */
4062 if (!it || STRINGP (object))
4063 break;
4064 }
4065 }
4066 }
4067 else if (VECTORP (spec))
4068 {
4069 int i;
4070 for (i = 0; i < ASIZE (spec); ++i)
4071 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
4072 position, bufpos, replacing_p,
4073 frame_window_p))
4074 {
4075 replacing_p = 1;
4076 /* If some text in a string is replaced, `position' no
4077 longer points to the position of `object'. */
4078 if (!it || STRINGP (object))
4079 break;
4080 }
4081 }
4082 else
4083 {
4084 if (handle_single_display_spec (it, spec, object, overlay,
4085 position, bufpos, 0, frame_window_p))
4086 replacing_p = 1;
4087 }
4088
4089 return replacing_p;
4090 }
4091
4092 /* Value is the position of the end of the `display' property starting
4093 at START_POS in OBJECT. */
4094
4095 static struct text_pos
4096 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4097 {
4098 Lisp_Object end;
4099 struct text_pos end_pos;
4100
4101 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4102 Qdisplay, object, Qnil);
4103 CHARPOS (end_pos) = XFASTINT (end);
4104 if (STRINGP (object))
4105 compute_string_pos (&end_pos, start_pos, it->string);
4106 else
4107 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4108
4109 return end_pos;
4110 }
4111
4112
4113 /* Set up IT from a single `display' property specification SPEC. OBJECT
4114 is the object in which the `display' property was found. *POSITION
4115 is the position in OBJECT at which the `display' property was found.
4116 BUFPOS is the buffer position of OBJECT (different from POSITION if
4117 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4118 previously saw a display specification which already replaced text
4119 display with something else, for example an image; we ignore such
4120 properties after the first one has been processed.
4121
4122 OVERLAY is the overlay this `display' property came from,
4123 or nil if it was a text property.
4124
4125 If SPEC is a `space' or `image' specification, and in some other
4126 cases too, set *POSITION to the position where the `display'
4127 property ends.
4128
4129 If IT is NULL, only examine the property specification in SPEC, but
4130 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4131 is intended to be displayed in a window on a GUI frame.
4132
4133 Value is non-zero if something was found which replaces the display
4134 of buffer or string text. */
4135
4136 static int
4137 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4138 Lisp_Object overlay, struct text_pos *position,
4139 EMACS_INT bufpos, int display_replaced_p,
4140 int frame_window_p)
4141 {
4142 Lisp_Object form;
4143 Lisp_Object location, value;
4144 struct text_pos start_pos = *position;
4145 int valid_p;
4146
4147 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4148 If the result is non-nil, use VALUE instead of SPEC. */
4149 form = Qt;
4150 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4151 {
4152 spec = XCDR (spec);
4153 if (!CONSP (spec))
4154 return 0;
4155 form = XCAR (spec);
4156 spec = XCDR (spec);
4157 }
4158
4159 if (!NILP (form) && !EQ (form, Qt))
4160 {
4161 int count = SPECPDL_INDEX ();
4162 struct gcpro gcpro1;
4163
4164 /* Bind `object' to the object having the `display' property, a
4165 buffer or string. Bind `position' to the position in the
4166 object where the property was found, and `buffer-position'
4167 to the current position in the buffer. */
4168
4169 if (NILP (object))
4170 XSETBUFFER (object, current_buffer);
4171 specbind (Qobject, object);
4172 specbind (Qposition, make_number (CHARPOS (*position)));
4173 specbind (Qbuffer_position, make_number (bufpos));
4174 GCPRO1 (form);
4175 form = safe_eval (form);
4176 UNGCPRO;
4177 unbind_to (count, Qnil);
4178 }
4179
4180 if (NILP (form))
4181 return 0;
4182
4183 /* Handle `(height HEIGHT)' specifications. */
4184 if (CONSP (spec)
4185 && EQ (XCAR (spec), Qheight)
4186 && CONSP (XCDR (spec)))
4187 {
4188 if (it)
4189 {
4190 if (!FRAME_WINDOW_P (it->f))
4191 return 0;
4192
4193 it->font_height = XCAR (XCDR (spec));
4194 if (!NILP (it->font_height))
4195 {
4196 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4197 int new_height = -1;
4198
4199 if (CONSP (it->font_height)
4200 && (EQ (XCAR (it->font_height), Qplus)
4201 || EQ (XCAR (it->font_height), Qminus))
4202 && CONSP (XCDR (it->font_height))
4203 && INTEGERP (XCAR (XCDR (it->font_height))))
4204 {
4205 /* `(+ N)' or `(- N)' where N is an integer. */
4206 int steps = XINT (XCAR (XCDR (it->font_height)));
4207 if (EQ (XCAR (it->font_height), Qplus))
4208 steps = - steps;
4209 it->face_id = smaller_face (it->f, it->face_id, steps);
4210 }
4211 else if (FUNCTIONP (it->font_height))
4212 {
4213 /* Call function with current height as argument.
4214 Value is the new height. */
4215 Lisp_Object height;
4216 height = safe_call1 (it->font_height,
4217 face->lface[LFACE_HEIGHT_INDEX]);
4218 if (NUMBERP (height))
4219 new_height = XFLOATINT (height);
4220 }
4221 else if (NUMBERP (it->font_height))
4222 {
4223 /* Value is a multiple of the canonical char height. */
4224 struct face *f;
4225
4226 f = FACE_FROM_ID (it->f,
4227 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4228 new_height = (XFLOATINT (it->font_height)
4229 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4230 }
4231 else
4232 {
4233 /* Evaluate IT->font_height with `height' bound to the
4234 current specified height to get the new height. */
4235 int count = SPECPDL_INDEX ();
4236
4237 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4238 value = safe_eval (it->font_height);
4239 unbind_to (count, Qnil);
4240
4241 if (NUMBERP (value))
4242 new_height = XFLOATINT (value);
4243 }
4244
4245 if (new_height > 0)
4246 it->face_id = face_with_height (it->f, it->face_id, new_height);
4247 }
4248 }
4249
4250 return 0;
4251 }
4252
4253 /* Handle `(space-width WIDTH)'. */
4254 if (CONSP (spec)
4255 && EQ (XCAR (spec), Qspace_width)
4256 && CONSP (XCDR (spec)))
4257 {
4258 if (it)
4259 {
4260 if (!FRAME_WINDOW_P (it->f))
4261 return 0;
4262
4263 value = XCAR (XCDR (spec));
4264 if (NUMBERP (value) && XFLOATINT (value) > 0)
4265 it->space_width = value;
4266 }
4267
4268 return 0;
4269 }
4270
4271 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4272 if (CONSP (spec)
4273 && EQ (XCAR (spec), Qslice))
4274 {
4275 Lisp_Object tem;
4276
4277 if (it)
4278 {
4279 if (!FRAME_WINDOW_P (it->f))
4280 return 0;
4281
4282 if (tem = XCDR (spec), CONSP (tem))
4283 {
4284 it->slice.x = XCAR (tem);
4285 if (tem = XCDR (tem), CONSP (tem))
4286 {
4287 it->slice.y = XCAR (tem);
4288 if (tem = XCDR (tem), CONSP (tem))
4289 {
4290 it->slice.width = XCAR (tem);
4291 if (tem = XCDR (tem), CONSP (tem))
4292 it->slice.height = XCAR (tem);
4293 }
4294 }
4295 }
4296 }
4297
4298 return 0;
4299 }
4300
4301 /* Handle `(raise FACTOR)'. */
4302 if (CONSP (spec)
4303 && EQ (XCAR (spec), Qraise)
4304 && CONSP (XCDR (spec)))
4305 {
4306 if (it)
4307 {
4308 if (!FRAME_WINDOW_P (it->f))
4309 return 0;
4310
4311 #ifdef HAVE_WINDOW_SYSTEM
4312 value = XCAR (XCDR (spec));
4313 if (NUMBERP (value))
4314 {
4315 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4316 it->voffset = - (XFLOATINT (value)
4317 * (FONT_HEIGHT (face->font)));
4318 }
4319 #endif /* HAVE_WINDOW_SYSTEM */
4320 }
4321
4322 return 0;
4323 }
4324
4325 /* Don't handle the other kinds of display specifications
4326 inside a string that we got from a `display' property. */
4327 if (it && it->string_from_display_prop_p)
4328 return 0;
4329
4330 /* Characters having this form of property are not displayed, so
4331 we have to find the end of the property. */
4332 if (it)
4333 {
4334 start_pos = *position;
4335 *position = display_prop_end (it, object, start_pos);
4336 }
4337 value = Qnil;
4338
4339 /* Stop the scan at that end position--we assume that all
4340 text properties change there. */
4341 if (it)
4342 it->stop_charpos = position->charpos;
4343
4344 /* Handle `(left-fringe BITMAP [FACE])'
4345 and `(right-fringe BITMAP [FACE])'. */
4346 if (CONSP (spec)
4347 && (EQ (XCAR (spec), Qleft_fringe)
4348 || EQ (XCAR (spec), Qright_fringe))
4349 && CONSP (XCDR (spec)))
4350 {
4351 int fringe_bitmap;
4352
4353 if (it)
4354 {
4355 if (!FRAME_WINDOW_P (it->f))
4356 /* If we return here, POSITION has been advanced
4357 across the text with this property. */
4358 return 0;
4359 }
4360 else if (!frame_window_p)
4361 return 0;
4362
4363 #ifdef HAVE_WINDOW_SYSTEM
4364 value = XCAR (XCDR (spec));
4365 if (!SYMBOLP (value)
4366 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4367 /* If we return here, POSITION has been advanced
4368 across the text with this property. */
4369 return 0;
4370
4371 if (it)
4372 {
4373 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4374
4375 if (CONSP (XCDR (XCDR (spec))))
4376 {
4377 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4378 int face_id2 = lookup_derived_face (it->f, face_name,
4379 FRINGE_FACE_ID, 0);
4380 if (face_id2 >= 0)
4381 face_id = face_id2;
4382 }
4383
4384 /* Save current settings of IT so that we can restore them
4385 when we are finished with the glyph property value. */
4386 push_it (it, position);
4387
4388 it->area = TEXT_AREA;
4389 it->what = IT_IMAGE;
4390 it->image_id = -1; /* no image */
4391 it->position = start_pos;
4392 it->object = NILP (object) ? it->w->buffer : object;
4393 it->method = GET_FROM_IMAGE;
4394 it->from_overlay = Qnil;
4395 it->face_id = face_id;
4396
4397 /* Say that we haven't consumed the characters with
4398 `display' property yet. The call to pop_it in
4399 set_iterator_to_next will clean this up. */
4400 *position = start_pos;
4401
4402 if (EQ (XCAR (spec), Qleft_fringe))
4403 {
4404 it->left_user_fringe_bitmap = fringe_bitmap;
4405 it->left_user_fringe_face_id = face_id;
4406 }
4407 else
4408 {
4409 it->right_user_fringe_bitmap = fringe_bitmap;
4410 it->right_user_fringe_face_id = face_id;
4411 }
4412 }
4413 #endif /* HAVE_WINDOW_SYSTEM */
4414 return 1;
4415 }
4416
4417 /* Prepare to handle `((margin left-margin) ...)',
4418 `((margin right-margin) ...)' and `((margin nil) ...)'
4419 prefixes for display specifications. */
4420 location = Qunbound;
4421 if (CONSP (spec) && CONSP (XCAR (spec)))
4422 {
4423 Lisp_Object tem;
4424
4425 value = XCDR (spec);
4426 if (CONSP (value))
4427 value = XCAR (value);
4428
4429 tem = XCAR (spec);
4430 if (EQ (XCAR (tem), Qmargin)
4431 && (tem = XCDR (tem),
4432 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4433 (NILP (tem)
4434 || EQ (tem, Qleft_margin)
4435 || EQ (tem, Qright_margin))))
4436 location = tem;
4437 }
4438
4439 if (EQ (location, Qunbound))
4440 {
4441 location = Qnil;
4442 value = spec;
4443 }
4444
4445 /* After this point, VALUE is the property after any
4446 margin prefix has been stripped. It must be a string,
4447 an image specification, or `(space ...)'.
4448
4449 LOCATION specifies where to display: `left-margin',
4450 `right-margin' or nil. */
4451
4452 valid_p = (STRINGP (value)
4453 #ifdef HAVE_WINDOW_SYSTEM
4454 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4455 && valid_image_p (value))
4456 #endif /* not HAVE_WINDOW_SYSTEM */
4457 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4458
4459 if (valid_p && !display_replaced_p)
4460 {
4461 if (!it)
4462 return 1;
4463
4464 /* Save current settings of IT so that we can restore them
4465 when we are finished with the glyph property value. */
4466 push_it (it, position);
4467 it->from_overlay = overlay;
4468
4469 if (NILP (location))
4470 it->area = TEXT_AREA;
4471 else if (EQ (location, Qleft_margin))
4472 it->area = LEFT_MARGIN_AREA;
4473 else
4474 it->area = RIGHT_MARGIN_AREA;
4475
4476 if (STRINGP (value))
4477 {
4478 it->string = value;
4479 it->multibyte_p = STRING_MULTIBYTE (it->string);
4480 it->current.overlay_string_index = -1;
4481 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4482 it->end_charpos = it->string_nchars = SCHARS (it->string);
4483 it->method = GET_FROM_STRING;
4484 it->stop_charpos = 0;
4485 it->prev_stop = 0;
4486 it->base_level_stop = 0;
4487 it->string_from_display_prop_p = 1;
4488 /* Say that we haven't consumed the characters with
4489 `display' property yet. The call to pop_it in
4490 set_iterator_to_next will clean this up. */
4491 if (BUFFERP (object))
4492 *position = start_pos;
4493
4494 /* Force paragraph direction to be that of the parent
4495 object. */
4496 it->paragraph_embedding =
4497 (it->bidi_p ? it->bidi_it.paragraph_dir : L2R);
4498
4499 /* Do we need to reorder this display string? */
4500 if (it->multibyte_p)
4501 {
4502 if (BUFFERP (object))
4503 it->bidi_p =
4504 !NILP (BVAR (XBUFFER (object), bidi_display_reordering));
4505 else
4506 it->bidi_p =
4507 !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
4508 }
4509 else
4510 it->bidi_p = 0;
4511
4512 /* Set up the bidi iterator for this display string. */
4513 if (it->bidi_p)
4514 {
4515 it->bidi_it.string.lstring = it->string;
4516 it->bidi_it.string.s = NULL;
4517 it->bidi_it.string.schars = it->end_charpos;
4518 it->bidi_it.string.bufpos = bufpos;
4519 it->bidi_it.string.from_disp_str = 1;
4520 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4521 }
4522 }
4523 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4524 {
4525 it->method = GET_FROM_STRETCH;
4526 it->object = value;
4527 *position = it->position = start_pos;
4528 }
4529 #ifdef HAVE_WINDOW_SYSTEM
4530 else
4531 {
4532 it->what = IT_IMAGE;
4533 it->image_id = lookup_image (it->f, value);
4534 it->position = start_pos;
4535 it->object = NILP (object) ? it->w->buffer : object;
4536 it->method = GET_FROM_IMAGE;
4537
4538 /* Say that we haven't consumed the characters with
4539 `display' property yet. The call to pop_it in
4540 set_iterator_to_next will clean this up. */
4541 *position = start_pos;
4542 }
4543 #endif /* HAVE_WINDOW_SYSTEM */
4544
4545 return 1;
4546 }
4547
4548 /* Invalid property or property not supported. Restore
4549 POSITION to what it was before. */
4550 *position = start_pos;
4551 return 0;
4552 }
4553
4554 /* Check if PROP is a display property value whose text should be
4555 treated as intangible. OVERLAY is the overlay from which PROP
4556 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4557 specify the buffer position covered by PROP. */
4558
4559 int
4560 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4561 EMACS_INT charpos, EMACS_INT bytepos)
4562 {
4563 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4564 struct text_pos position;
4565
4566 SET_TEXT_POS (position, charpos, bytepos);
4567 return handle_display_spec (NULL, prop, Qnil, overlay,
4568 &position, charpos, frame_window_p);
4569 }
4570
4571
4572 /* Return 1 if PROP is a display sub-property value containing STRING.
4573
4574 Implementation note: this and the following function are really
4575 special cases of handle_display_spec and
4576 handle_single_display_spec, and should ideally use the same code.
4577 Until they do, these two pairs must be consistent and must be
4578 modified in sync. */
4579
4580 static int
4581 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4582 {
4583 if (EQ (string, prop))
4584 return 1;
4585
4586 /* Skip over `when FORM'. */
4587 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4588 {
4589 prop = XCDR (prop);
4590 if (!CONSP (prop))
4591 return 0;
4592 /* Actually, the condition following `when' should be eval'ed,
4593 like handle_single_display_spec does, and we should return
4594 zero if it evaluates to nil. However, this function is
4595 called only when the buffer was already displayed and some
4596 glyph in the glyph matrix was found to come from a display
4597 string. Therefore, the condition was already evaluated, and
4598 the result was non-nil, otherwise the display string wouldn't
4599 have been displayed and we would have never been called for
4600 this property. Thus, we can skip the evaluation and assume
4601 its result is non-nil. */
4602 prop = XCDR (prop);
4603 }
4604
4605 if (CONSP (prop))
4606 /* Skip over `margin LOCATION'. */
4607 if (EQ (XCAR (prop), Qmargin))
4608 {
4609 prop = XCDR (prop);
4610 if (!CONSP (prop))
4611 return 0;
4612
4613 prop = XCDR (prop);
4614 if (!CONSP (prop))
4615 return 0;
4616 }
4617
4618 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4619 }
4620
4621
4622 /* Return 1 if STRING appears in the `display' property PROP. */
4623
4624 static int
4625 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4626 {
4627 if (CONSP (prop)
4628 && !EQ (XCAR (prop), Qwhen)
4629 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4630 {
4631 /* A list of sub-properties. */
4632 while (CONSP (prop))
4633 {
4634 if (single_display_spec_string_p (XCAR (prop), string))
4635 return 1;
4636 prop = XCDR (prop);
4637 }
4638 }
4639 else if (VECTORP (prop))
4640 {
4641 /* A vector of sub-properties. */
4642 int i;
4643 for (i = 0; i < ASIZE (prop); ++i)
4644 if (single_display_spec_string_p (AREF (prop, i), string))
4645 return 1;
4646 }
4647 else
4648 return single_display_spec_string_p (prop, string);
4649
4650 return 0;
4651 }
4652
4653 /* Look for STRING in overlays and text properties in the current
4654 buffer, between character positions FROM and TO (excluding TO).
4655 BACK_P non-zero means look back (in this case, TO is supposed to be
4656 less than FROM).
4657 Value is the first character position where STRING was found, or
4658 zero if it wasn't found before hitting TO.
4659
4660 This function may only use code that doesn't eval because it is
4661 called asynchronously from note_mouse_highlight. */
4662
4663 static EMACS_INT
4664 string_buffer_position_lim (Lisp_Object string,
4665 EMACS_INT from, EMACS_INT to, int back_p)
4666 {
4667 Lisp_Object limit, prop, pos;
4668 int found = 0;
4669
4670 pos = make_number (from);
4671
4672 if (!back_p) /* looking forward */
4673 {
4674 limit = make_number (min (to, ZV));
4675 while (!found && !EQ (pos, limit))
4676 {
4677 prop = Fget_char_property (pos, Qdisplay, Qnil);
4678 if (!NILP (prop) && display_prop_string_p (prop, string))
4679 found = 1;
4680 else
4681 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4682 limit);
4683 }
4684 }
4685 else /* looking back */
4686 {
4687 limit = make_number (max (to, BEGV));
4688 while (!found && !EQ (pos, limit))
4689 {
4690 prop = Fget_char_property (pos, Qdisplay, Qnil);
4691 if (!NILP (prop) && display_prop_string_p (prop, string))
4692 found = 1;
4693 else
4694 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4695 limit);
4696 }
4697 }
4698
4699 return found ? XINT (pos) : 0;
4700 }
4701
4702 /* Determine which buffer position in current buffer STRING comes from.
4703 AROUND_CHARPOS is an approximate position where it could come from.
4704 Value is the buffer position or 0 if it couldn't be determined.
4705
4706 This function is necessary because we don't record buffer positions
4707 in glyphs generated from strings (to keep struct glyph small).
4708 This function may only use code that doesn't eval because it is
4709 called asynchronously from note_mouse_highlight. */
4710
4711 static EMACS_INT
4712 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4713 {
4714 const int MAX_DISTANCE = 1000;
4715 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4716 around_charpos + MAX_DISTANCE,
4717 0);
4718
4719 if (!found)
4720 found = string_buffer_position_lim (string, around_charpos,
4721 around_charpos - MAX_DISTANCE, 1);
4722 return found;
4723 }
4724
4725
4726 \f
4727 /***********************************************************************
4728 `composition' property
4729 ***********************************************************************/
4730
4731 /* Set up iterator IT from `composition' property at its current
4732 position. Called from handle_stop. */
4733
4734 static enum prop_handled
4735 handle_composition_prop (struct it *it)
4736 {
4737 Lisp_Object prop, string;
4738 EMACS_INT pos, pos_byte, start, end;
4739
4740 if (STRINGP (it->string))
4741 {
4742 unsigned char *s;
4743
4744 pos = IT_STRING_CHARPOS (*it);
4745 pos_byte = IT_STRING_BYTEPOS (*it);
4746 string = it->string;
4747 s = SDATA (string) + pos_byte;
4748 it->c = STRING_CHAR (s);
4749 }
4750 else
4751 {
4752 pos = IT_CHARPOS (*it);
4753 pos_byte = IT_BYTEPOS (*it);
4754 string = Qnil;
4755 it->c = FETCH_CHAR (pos_byte);
4756 }
4757
4758 /* If there's a valid composition and point is not inside of the
4759 composition (in the case that the composition is from the current
4760 buffer), draw a glyph composed from the composition components. */
4761 if (find_composition (pos, -1, &start, &end, &prop, string)
4762 && COMPOSITION_VALID_P (start, end, prop)
4763 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4764 {
4765 if (start != pos)
4766 {
4767 if (STRINGP (it->string))
4768 pos_byte = string_char_to_byte (it->string, start);
4769 else
4770 pos_byte = CHAR_TO_BYTE (start);
4771 }
4772 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4773 prop, string);
4774
4775 if (it->cmp_it.id >= 0)
4776 {
4777 it->cmp_it.ch = -1;
4778 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4779 it->cmp_it.nglyphs = -1;
4780 }
4781 }
4782
4783 return HANDLED_NORMALLY;
4784 }
4785
4786
4787 \f
4788 /***********************************************************************
4789 Overlay strings
4790 ***********************************************************************/
4791
4792 /* The following structure is used to record overlay strings for
4793 later sorting in load_overlay_strings. */
4794
4795 struct overlay_entry
4796 {
4797 Lisp_Object overlay;
4798 Lisp_Object string;
4799 int priority;
4800 int after_string_p;
4801 };
4802
4803
4804 /* Set up iterator IT from overlay strings at its current position.
4805 Called from handle_stop. */
4806
4807 static enum prop_handled
4808 handle_overlay_change (struct it *it)
4809 {
4810 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4811 return HANDLED_RECOMPUTE_PROPS;
4812 else
4813 return HANDLED_NORMALLY;
4814 }
4815
4816
4817 /* Set up the next overlay string for delivery by IT, if there is an
4818 overlay string to deliver. Called by set_iterator_to_next when the
4819 end of the current overlay string is reached. If there are more
4820 overlay strings to display, IT->string and
4821 IT->current.overlay_string_index are set appropriately here.
4822 Otherwise IT->string is set to nil. */
4823
4824 static void
4825 next_overlay_string (struct it *it)
4826 {
4827 ++it->current.overlay_string_index;
4828 if (it->current.overlay_string_index == it->n_overlay_strings)
4829 {
4830 /* No more overlay strings. Restore IT's settings to what
4831 they were before overlay strings were processed, and
4832 continue to deliver from current_buffer. */
4833
4834 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4835 pop_it (it);
4836 xassert (it->sp > 0
4837 || (NILP (it->string)
4838 && it->method == GET_FROM_BUFFER
4839 && it->stop_charpos >= BEGV
4840 && it->stop_charpos <= it->end_charpos));
4841 it->current.overlay_string_index = -1;
4842 it->n_overlay_strings = 0;
4843 it->overlay_strings_charpos = -1;
4844
4845 /* If we're at the end of the buffer, record that we have
4846 processed the overlay strings there already, so that
4847 next_element_from_buffer doesn't try it again. */
4848 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4849 it->overlay_strings_at_end_processed_p = 1;
4850 }
4851 else
4852 {
4853 /* There are more overlay strings to process. If
4854 IT->current.overlay_string_index has advanced to a position
4855 where we must load IT->overlay_strings with more strings, do
4856 it. We must load at the IT->overlay_strings_charpos where
4857 IT->n_overlay_strings was originally computed; when invisible
4858 text is present, this might not be IT_CHARPOS (Bug#7016). */
4859 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4860
4861 if (it->current.overlay_string_index && i == 0)
4862 load_overlay_strings (it, it->overlay_strings_charpos);
4863
4864 /* Initialize IT to deliver display elements from the overlay
4865 string. */
4866 it->string = it->overlay_strings[i];
4867 it->multibyte_p = STRING_MULTIBYTE (it->string);
4868 SET_TEXT_POS (it->current.string_pos, 0, 0);
4869 it->method = GET_FROM_STRING;
4870 it->stop_charpos = 0;
4871 if (it->cmp_it.stop_pos >= 0)
4872 it->cmp_it.stop_pos = 0;
4873 it->prev_stop = 0;
4874 it->base_level_stop = 0;
4875
4876 /* Do we need to reorder this overlay string? */
4877 it->bidi_p =
4878 it->multibyte_p
4879 && !NILP (BVAR (current_buffer, bidi_display_reordering));
4880
4881 /* Set up the bidi iterator for this overlay string. */
4882 if (it->bidi_p)
4883 {
4884 it->bidi_it.string.lstring = it->string;
4885 it->bidi_it.string.s = NULL;
4886 it->bidi_it.string.schars = SCHARS (it->string);
4887 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4888 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4889 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4890 }
4891 }
4892
4893 CHECK_IT (it);
4894 }
4895
4896
4897 /* Compare two overlay_entry structures E1 and E2. Used as a
4898 comparison function for qsort in load_overlay_strings. Overlay
4899 strings for the same position are sorted so that
4900
4901 1. All after-strings come in front of before-strings, except
4902 when they come from the same overlay.
4903
4904 2. Within after-strings, strings are sorted so that overlay strings
4905 from overlays with higher priorities come first.
4906
4907 2. Within before-strings, strings are sorted so that overlay
4908 strings from overlays with higher priorities come last.
4909
4910 Value is analogous to strcmp. */
4911
4912
4913 static int
4914 compare_overlay_entries (const void *e1, const void *e2)
4915 {
4916 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4917 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4918 int result;
4919
4920 if (entry1->after_string_p != entry2->after_string_p)
4921 {
4922 /* Let after-strings appear in front of before-strings if
4923 they come from different overlays. */
4924 if (EQ (entry1->overlay, entry2->overlay))
4925 result = entry1->after_string_p ? 1 : -1;
4926 else
4927 result = entry1->after_string_p ? -1 : 1;
4928 }
4929 else if (entry1->after_string_p)
4930 /* After-strings sorted in order of decreasing priority. */
4931 result = entry2->priority - entry1->priority;
4932 else
4933 /* Before-strings sorted in order of increasing priority. */
4934 result = entry1->priority - entry2->priority;
4935
4936 return result;
4937 }
4938
4939
4940 /* Load the vector IT->overlay_strings with overlay strings from IT's
4941 current buffer position, or from CHARPOS if that is > 0. Set
4942 IT->n_overlays to the total number of overlay strings found.
4943
4944 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4945 a time. On entry into load_overlay_strings,
4946 IT->current.overlay_string_index gives the number of overlay
4947 strings that have already been loaded by previous calls to this
4948 function.
4949
4950 IT->add_overlay_start contains an additional overlay start
4951 position to consider for taking overlay strings from, if non-zero.
4952 This position comes into play when the overlay has an `invisible'
4953 property, and both before and after-strings. When we've skipped to
4954 the end of the overlay, because of its `invisible' property, we
4955 nevertheless want its before-string to appear.
4956 IT->add_overlay_start will contain the overlay start position
4957 in this case.
4958
4959 Overlay strings are sorted so that after-string strings come in
4960 front of before-string strings. Within before and after-strings,
4961 strings are sorted by overlay priority. See also function
4962 compare_overlay_entries. */
4963
4964 static void
4965 load_overlay_strings (struct it *it, EMACS_INT charpos)
4966 {
4967 Lisp_Object overlay, window, str, invisible;
4968 struct Lisp_Overlay *ov;
4969 EMACS_INT start, end;
4970 int size = 20;
4971 int n = 0, i, j, invis_p;
4972 struct overlay_entry *entries
4973 = (struct overlay_entry *) alloca (size * sizeof *entries);
4974
4975 if (charpos <= 0)
4976 charpos = IT_CHARPOS (*it);
4977
4978 /* Append the overlay string STRING of overlay OVERLAY to vector
4979 `entries' which has size `size' and currently contains `n'
4980 elements. AFTER_P non-zero means STRING is an after-string of
4981 OVERLAY. */
4982 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4983 do \
4984 { \
4985 Lisp_Object priority; \
4986 \
4987 if (n == size) \
4988 { \
4989 int new_size = 2 * size; \
4990 struct overlay_entry *old = entries; \
4991 entries = \
4992 (struct overlay_entry *) alloca (new_size \
4993 * sizeof *entries); \
4994 memcpy (entries, old, size * sizeof *entries); \
4995 size = new_size; \
4996 } \
4997 \
4998 entries[n].string = (STRING); \
4999 entries[n].overlay = (OVERLAY); \
5000 priority = Foverlay_get ((OVERLAY), Qpriority); \
5001 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5002 entries[n].after_string_p = (AFTER_P); \
5003 ++n; \
5004 } \
5005 while (0)
5006
5007 /* Process overlay before the overlay center. */
5008 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5009 {
5010 XSETMISC (overlay, ov);
5011 xassert (OVERLAYP (overlay));
5012 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5013 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5014
5015 if (end < charpos)
5016 break;
5017
5018 /* Skip this overlay if it doesn't start or end at IT's current
5019 position. */
5020 if (end != charpos && start != charpos)
5021 continue;
5022
5023 /* Skip this overlay if it doesn't apply to IT->w. */
5024 window = Foverlay_get (overlay, Qwindow);
5025 if (WINDOWP (window) && XWINDOW (window) != it->w)
5026 continue;
5027
5028 /* If the text ``under'' the overlay is invisible, both before-
5029 and after-strings from this overlay are visible; start and
5030 end position are indistinguishable. */
5031 invisible = Foverlay_get (overlay, Qinvisible);
5032 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5033
5034 /* If overlay has a non-empty before-string, record it. */
5035 if ((start == charpos || (end == charpos && invis_p))
5036 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5037 && SCHARS (str))
5038 RECORD_OVERLAY_STRING (overlay, str, 0);
5039
5040 /* If overlay has a non-empty after-string, record it. */
5041 if ((end == charpos || (start == charpos && invis_p))
5042 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5043 && SCHARS (str))
5044 RECORD_OVERLAY_STRING (overlay, str, 1);
5045 }
5046
5047 /* Process overlays after the overlay center. */
5048 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5049 {
5050 XSETMISC (overlay, ov);
5051 xassert (OVERLAYP (overlay));
5052 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5053 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5054
5055 if (start > charpos)
5056 break;
5057
5058 /* Skip this overlay if it doesn't start or end at IT's current
5059 position. */
5060 if (end != charpos && start != charpos)
5061 continue;
5062
5063 /* Skip this overlay if it doesn't apply to IT->w. */
5064 window = Foverlay_get (overlay, Qwindow);
5065 if (WINDOWP (window) && XWINDOW (window) != it->w)
5066 continue;
5067
5068 /* If the text ``under'' the overlay is invisible, it has a zero
5069 dimension, and both before- and after-strings apply. */
5070 invisible = Foverlay_get (overlay, Qinvisible);
5071 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5072
5073 /* If overlay has a non-empty before-string, record it. */
5074 if ((start == charpos || (end == charpos && invis_p))
5075 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5076 && SCHARS (str))
5077 RECORD_OVERLAY_STRING (overlay, str, 0);
5078
5079 /* If overlay has a non-empty after-string, record it. */
5080 if ((end == charpos || (start == charpos && invis_p))
5081 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5082 && SCHARS (str))
5083 RECORD_OVERLAY_STRING (overlay, str, 1);
5084 }
5085
5086 #undef RECORD_OVERLAY_STRING
5087
5088 /* Sort entries. */
5089 if (n > 1)
5090 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5091
5092 /* Record number of overlay strings, and where we computed it. */
5093 it->n_overlay_strings = n;
5094 it->overlay_strings_charpos = charpos;
5095
5096 /* IT->current.overlay_string_index is the number of overlay strings
5097 that have already been consumed by IT. Copy some of the
5098 remaining overlay strings to IT->overlay_strings. */
5099 i = 0;
5100 j = it->current.overlay_string_index;
5101 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5102 {
5103 it->overlay_strings[i] = entries[j].string;
5104 it->string_overlays[i++] = entries[j++].overlay;
5105 }
5106
5107 CHECK_IT (it);
5108 }
5109
5110
5111 /* Get the first chunk of overlay strings at IT's current buffer
5112 position, or at CHARPOS if that is > 0. Value is non-zero if at
5113 least one overlay string was found. */
5114
5115 static int
5116 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5117 {
5118 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5119 process. This fills IT->overlay_strings with strings, and sets
5120 IT->n_overlay_strings to the total number of strings to process.
5121 IT->pos.overlay_string_index has to be set temporarily to zero
5122 because load_overlay_strings needs this; it must be set to -1
5123 when no overlay strings are found because a zero value would
5124 indicate a position in the first overlay string. */
5125 it->current.overlay_string_index = 0;
5126 load_overlay_strings (it, charpos);
5127
5128 /* If we found overlay strings, set up IT to deliver display
5129 elements from the first one. Otherwise set up IT to deliver
5130 from current_buffer. */
5131 if (it->n_overlay_strings)
5132 {
5133 /* Make sure we know settings in current_buffer, so that we can
5134 restore meaningful values when we're done with the overlay
5135 strings. */
5136 if (compute_stop_p)
5137 compute_stop_pos (it);
5138 xassert (it->face_id >= 0);
5139
5140 /* Save IT's settings. They are restored after all overlay
5141 strings have been processed. */
5142 xassert (!compute_stop_p || it->sp == 0);
5143
5144 /* When called from handle_stop, there might be an empty display
5145 string loaded. In that case, don't bother saving it. */
5146 if (!STRINGP (it->string) || SCHARS (it->string))
5147 push_it (it, NULL);
5148
5149 /* Set up IT to deliver display elements from the first overlay
5150 string. */
5151 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5152 it->string = it->overlay_strings[0];
5153 it->from_overlay = Qnil;
5154 it->stop_charpos = 0;
5155 xassert (STRINGP (it->string));
5156 it->end_charpos = SCHARS (it->string);
5157 it->prev_stop = 0;
5158 it->base_level_stop = 0;
5159 it->multibyte_p = STRING_MULTIBYTE (it->string);
5160 it->method = GET_FROM_STRING;
5161
5162 /* Do we need to reorder this overlay string? */
5163 it->bidi_p =
5164 it->multibyte_p
5165 && !NILP (BVAR (current_buffer, bidi_display_reordering));
5166
5167 /* Force paragraph direction to be that of the parent
5168 buffer. */
5169 it->paragraph_embedding = (it->bidi_p ? it->bidi_it.paragraph_dir : L2R);
5170
5171 /* Set up the bidi iterator for this overlay string. */
5172 if (it->bidi_p)
5173 {
5174 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5175
5176 it->bidi_it.string.lstring = it->string;
5177 it->bidi_it.string.s = NULL;
5178 it->bidi_it.string.schars = SCHARS (it->string);
5179 it->bidi_it.string.bufpos = pos;
5180 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5181 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5182 }
5183 return 1;
5184 }
5185
5186 it->current.overlay_string_index = -1;
5187 return 0;
5188 }
5189
5190 static int
5191 get_overlay_strings (struct it *it, EMACS_INT charpos)
5192 {
5193 it->string = Qnil;
5194 it->method = GET_FROM_BUFFER;
5195
5196 (void) get_overlay_strings_1 (it, charpos, 1);
5197
5198 CHECK_IT (it);
5199
5200 /* Value is non-zero if we found at least one overlay string. */
5201 return STRINGP (it->string);
5202 }
5203
5204
5205 \f
5206 /***********************************************************************
5207 Saving and restoring state
5208 ***********************************************************************/
5209
5210 /* Save current settings of IT on IT->stack. Called, for example,
5211 before setting up IT for an overlay string, to be able to restore
5212 IT's settings to what they were after the overlay string has been
5213 processed. If POSITION is non-NULL, it is the position to save on
5214 the stack instead of IT->position. */
5215
5216 static void
5217 push_it (struct it *it, struct text_pos *position)
5218 {
5219 struct iterator_stack_entry *p;
5220
5221 xassert (it->sp < IT_STACK_SIZE);
5222 p = it->stack + it->sp;
5223
5224 p->stop_charpos = it->stop_charpos;
5225 p->prev_stop = it->prev_stop;
5226 p->base_level_stop = it->base_level_stop;
5227 p->cmp_it = it->cmp_it;
5228 xassert (it->face_id >= 0);
5229 p->face_id = it->face_id;
5230 p->string = it->string;
5231 p->method = it->method;
5232 p->from_overlay = it->from_overlay;
5233 switch (p->method)
5234 {
5235 case GET_FROM_IMAGE:
5236 p->u.image.object = it->object;
5237 p->u.image.image_id = it->image_id;
5238 p->u.image.slice = it->slice;
5239 break;
5240 case GET_FROM_STRETCH:
5241 p->u.stretch.object = it->object;
5242 break;
5243 }
5244 p->position = position ? *position : it->position;
5245 p->current = it->current;
5246 p->end_charpos = it->end_charpos;
5247 p->string_nchars = it->string_nchars;
5248 p->area = it->area;
5249 p->multibyte_p = it->multibyte_p;
5250 p->avoid_cursor_p = it->avoid_cursor_p;
5251 p->space_width = it->space_width;
5252 p->font_height = it->font_height;
5253 p->voffset = it->voffset;
5254 p->string_from_display_prop_p = it->string_from_display_prop_p;
5255 p->display_ellipsis_p = 0;
5256 p->line_wrap = it->line_wrap;
5257 p->bidi_p = it->bidi_p;
5258 ++it->sp;
5259
5260 /* Save the state of the bidi iterator as well. */
5261 if (it->bidi_p)
5262 bidi_push_it (&it->bidi_it);
5263 }
5264
5265 static void
5266 iterate_out_of_display_property (struct it *it)
5267 {
5268 int buffer_p = BUFFERP (it->object);
5269 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5270 EMACS_INT bob = (buffer_p ? BEGV : 0);
5271
5272 /* Maybe initialize paragraph direction. If we are at the beginning
5273 of a new paragraph, next_element_from_buffer may not have a
5274 chance to do that. */
5275 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5276 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5277 /* prev_stop can be zero, so check against BEGV as well. */
5278 while (it->bidi_it.charpos >= bob
5279 && it->prev_stop <= it->bidi_it.charpos
5280 && it->bidi_it.charpos < CHARPOS (it->position))
5281 bidi_move_to_visually_next (&it->bidi_it);
5282 /* Record the stop_pos we just crossed, for when we cross it
5283 back, maybe. */
5284 if (it->bidi_it.charpos > CHARPOS (it->position))
5285 it->prev_stop = CHARPOS (it->position);
5286 /* If we ended up not where pop_it put us, resync IT's
5287 positional members with the bidi iterator. */
5288 if (it->bidi_it.charpos != CHARPOS (it->position))
5289 {
5290 SET_TEXT_POS (it->position,
5291 it->bidi_it.charpos, it->bidi_it.bytepos);
5292 if (buffer_p)
5293 it->current.pos = it->position;
5294 else
5295 it->current.string_pos = it->position;
5296 }
5297 }
5298
5299 /* Restore IT's settings from IT->stack. Called, for example, when no
5300 more overlay strings must be processed, and we return to delivering
5301 display elements from a buffer, or when the end of a string from a
5302 `display' property is reached and we return to delivering display
5303 elements from an overlay string, or from a buffer. */
5304
5305 static void
5306 pop_it (struct it *it)
5307 {
5308 struct iterator_stack_entry *p;
5309
5310 xassert (it->sp > 0);
5311 --it->sp;
5312 p = it->stack + it->sp;
5313 it->stop_charpos = p->stop_charpos;
5314 it->prev_stop = p->prev_stop;
5315 it->base_level_stop = p->base_level_stop;
5316 it->cmp_it = p->cmp_it;
5317 it->face_id = p->face_id;
5318 it->current = p->current;
5319 it->position = p->position;
5320 it->string = p->string;
5321 it->from_overlay = p->from_overlay;
5322 if (NILP (it->string))
5323 SET_TEXT_POS (it->current.string_pos, -1, -1);
5324 it->method = p->method;
5325 switch (it->method)
5326 {
5327 case GET_FROM_IMAGE:
5328 it->image_id = p->u.image.image_id;
5329 it->object = p->u.image.object;
5330 it->slice = p->u.image.slice;
5331 break;
5332 case GET_FROM_STRETCH:
5333 it->object = p->u.comp.object;
5334 break;
5335 case GET_FROM_BUFFER:
5336 it->object = it->w->buffer;
5337 break;
5338 case GET_FROM_STRING:
5339 it->object = it->string;
5340 break;
5341 case GET_FROM_DISPLAY_VECTOR:
5342 if (it->s)
5343 it->method = GET_FROM_C_STRING;
5344 else if (STRINGP (it->string))
5345 it->method = GET_FROM_STRING;
5346 else
5347 {
5348 it->method = GET_FROM_BUFFER;
5349 it->object = it->w->buffer;
5350 }
5351 }
5352 it->end_charpos = p->end_charpos;
5353 it->string_nchars = p->string_nchars;
5354 it->area = p->area;
5355 it->multibyte_p = p->multibyte_p;
5356 it->avoid_cursor_p = p->avoid_cursor_p;
5357 it->space_width = p->space_width;
5358 it->font_height = p->font_height;
5359 it->voffset = p->voffset;
5360 it->string_from_display_prop_p = p->string_from_display_prop_p;
5361 it->line_wrap = p->line_wrap;
5362 it->bidi_p = p->bidi_p;
5363 if (it->bidi_p)
5364 {
5365 bidi_pop_it (&it->bidi_it);
5366 /* Bidi-iterate until we get out of the portion of text, if any,
5367 covered by a `display' text property or by an overlay with
5368 `display' property. (We cannot just jump there, because the
5369 internal coherency of the bidi iterator state can not be
5370 preserved across such jumps.) We also must determine the
5371 paragraph base direction if the overlay we just processed is
5372 at the beginning of a new paragraph. */
5373 if (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING)
5374 iterate_out_of_display_property (it);
5375 }
5376 }
5377
5378
5379 \f
5380 /***********************************************************************
5381 Moving over lines
5382 ***********************************************************************/
5383
5384 /* Set IT's current position to the previous line start. */
5385
5386 static void
5387 back_to_previous_line_start (struct it *it)
5388 {
5389 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5390 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5391 }
5392
5393
5394 /* Move IT to the next line start.
5395
5396 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5397 we skipped over part of the text (as opposed to moving the iterator
5398 continuously over the text). Otherwise, don't change the value
5399 of *SKIPPED_P.
5400
5401 Newlines may come from buffer text, overlay strings, or strings
5402 displayed via the `display' property. That's the reason we can't
5403 simply use find_next_newline_no_quit.
5404
5405 Note that this function may not skip over invisible text that is so
5406 because of text properties and immediately follows a newline. If
5407 it would, function reseat_at_next_visible_line_start, when called
5408 from set_iterator_to_next, would effectively make invisible
5409 characters following a newline part of the wrong glyph row, which
5410 leads to wrong cursor motion. */
5411
5412 static int
5413 forward_to_next_line_start (struct it *it, int *skipped_p)
5414 {
5415 int old_selective, newline_found_p, n;
5416 const int MAX_NEWLINE_DISTANCE = 500;
5417
5418 /* If already on a newline, just consume it to avoid unintended
5419 skipping over invisible text below. */
5420 if (it->what == IT_CHARACTER
5421 && it->c == '\n'
5422 && CHARPOS (it->position) == IT_CHARPOS (*it))
5423 {
5424 set_iterator_to_next (it, 0);
5425 it->c = 0;
5426 return 1;
5427 }
5428
5429 /* Don't handle selective display in the following. It's (a)
5430 unnecessary because it's done by the caller, and (b) leads to an
5431 infinite recursion because next_element_from_ellipsis indirectly
5432 calls this function. */
5433 old_selective = it->selective;
5434 it->selective = 0;
5435
5436 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5437 from buffer text. */
5438 for (n = newline_found_p = 0;
5439 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5440 n += STRINGP (it->string) ? 0 : 1)
5441 {
5442 if (!get_next_display_element (it))
5443 return 0;
5444 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5445 set_iterator_to_next (it, 0);
5446 }
5447
5448 /* If we didn't find a newline near enough, see if we can use a
5449 short-cut. */
5450 if (!newline_found_p)
5451 {
5452 EMACS_INT start = IT_CHARPOS (*it);
5453 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5454 Lisp_Object pos;
5455
5456 xassert (!STRINGP (it->string));
5457
5458 /* If we are not bidi-reordering, and there isn't any `display'
5459 property in sight, and no overlays, we can just use the
5460 position of the newline in buffer text. */
5461 if (!it->bidi_p
5462 && (it->stop_charpos >= limit
5463 || ((pos = Fnext_single_property_change (make_number (start),
5464 Qdisplay, Qnil,
5465 make_number (limit)),
5466 NILP (pos))
5467 && next_overlay_change (start) == ZV)))
5468 {
5469 IT_CHARPOS (*it) = limit;
5470 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5471 *skipped_p = newline_found_p = 1;
5472 }
5473 else
5474 {
5475 while (get_next_display_element (it)
5476 && !newline_found_p)
5477 {
5478 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5479 set_iterator_to_next (it, 0);
5480 }
5481 }
5482 }
5483
5484 it->selective = old_selective;
5485 return newline_found_p;
5486 }
5487
5488
5489 /* Set IT's current position to the previous visible line start. Skip
5490 invisible text that is so either due to text properties or due to
5491 selective display. Caution: this does not change IT->current_x and
5492 IT->hpos. */
5493
5494 static void
5495 back_to_previous_visible_line_start (struct it *it)
5496 {
5497 while (IT_CHARPOS (*it) > BEGV)
5498 {
5499 back_to_previous_line_start (it);
5500
5501 if (IT_CHARPOS (*it) <= BEGV)
5502 break;
5503
5504 /* If selective > 0, then lines indented more than its value are
5505 invisible. */
5506 if (it->selective > 0
5507 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5508 (double) it->selective)) /* iftc */
5509 continue;
5510
5511 /* Check the newline before point for invisibility. */
5512 {
5513 Lisp_Object prop;
5514 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5515 Qinvisible, it->window);
5516 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5517 continue;
5518 }
5519
5520 if (IT_CHARPOS (*it) <= BEGV)
5521 break;
5522
5523 {
5524 struct it it2;
5525 EMACS_INT pos;
5526 EMACS_INT beg, end;
5527 Lisp_Object val, overlay;
5528
5529 /* If newline is part of a composition, continue from start of composition */
5530 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5531 && beg < IT_CHARPOS (*it))
5532 goto replaced;
5533
5534 /* If newline is replaced by a display property, find start of overlay
5535 or interval and continue search from that point. */
5536 it2 = *it;
5537 pos = --IT_CHARPOS (it2);
5538 --IT_BYTEPOS (it2);
5539 it2.sp = 0;
5540 it2.string_from_display_prop_p = 0;
5541 if (handle_display_prop (&it2) == HANDLED_RETURN
5542 && !NILP (val = get_char_property_and_overlay
5543 (make_number (pos), Qdisplay, Qnil, &overlay))
5544 && (OVERLAYP (overlay)
5545 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5546 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5547 {
5548 /* If the call to handle_display_prop above pushed the
5549 iterator state, that causes side effects for the bidi
5550 iterator by calling bidi_push_it. Undo those side
5551 effects. */
5552 while (it2.sp > 0)
5553 {
5554 /* push_it calls bidi_push_it only if the bidi_p flag
5555 is set in the iterator being pushed. */
5556 if (it2.stack[--it2.sp].bidi_p)
5557 bidi_pop_it (&it2.bidi_it);
5558 }
5559 goto replaced;
5560 }
5561
5562 /* Newline is not replaced by anything -- so we are done. */
5563 break;
5564
5565 replaced:
5566 if (beg < BEGV)
5567 beg = BEGV;
5568 IT_CHARPOS (*it) = beg;
5569 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5570 }
5571 }
5572
5573 it->continuation_lines_width = 0;
5574
5575 xassert (IT_CHARPOS (*it) >= BEGV);
5576 xassert (IT_CHARPOS (*it) == BEGV
5577 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5578 CHECK_IT (it);
5579 }
5580
5581
5582 /* Reseat iterator IT at the previous visible line start. Skip
5583 invisible text that is so either due to text properties or due to
5584 selective display. At the end, update IT's overlay information,
5585 face information etc. */
5586
5587 void
5588 reseat_at_previous_visible_line_start (struct it *it)
5589 {
5590 back_to_previous_visible_line_start (it);
5591 reseat (it, it->current.pos, 1);
5592 CHECK_IT (it);
5593 }
5594
5595
5596 /* Reseat iterator IT on the next visible line start in the current
5597 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5598 preceding the line start. Skip over invisible text that is so
5599 because of selective display. Compute faces, overlays etc at the
5600 new position. Note that this function does not skip over text that
5601 is invisible because of text properties. */
5602
5603 static void
5604 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5605 {
5606 int newline_found_p, skipped_p = 0;
5607
5608 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5609
5610 /* Skip over lines that are invisible because they are indented
5611 more than the value of IT->selective. */
5612 if (it->selective > 0)
5613 while (IT_CHARPOS (*it) < ZV
5614 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5615 (double) it->selective)) /* iftc */
5616 {
5617 xassert (IT_BYTEPOS (*it) == BEGV
5618 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5619 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5620 }
5621
5622 /* Position on the newline if that's what's requested. */
5623 if (on_newline_p && newline_found_p)
5624 {
5625 if (STRINGP (it->string))
5626 {
5627 if (IT_STRING_CHARPOS (*it) > 0)
5628 {
5629 if (!it->bidi_p)
5630 {
5631 --IT_STRING_CHARPOS (*it);
5632 --IT_STRING_BYTEPOS (*it);
5633 }
5634 else
5635 /* Setting this flag will cause
5636 bidi_move_to_visually_next not to advance, but
5637 instead deliver the current character (newline),
5638 which is what the ON_NEWLINE_P flag wants. */
5639 it->bidi_it.first_elt = 1;
5640 }
5641 }
5642 else if (IT_CHARPOS (*it) > BEGV)
5643 {
5644 if (!it->bidi_p)
5645 {
5646 --IT_CHARPOS (*it);
5647 --IT_BYTEPOS (*it);
5648 }
5649 /* With bidi iteration, the call to `reseat' will cause
5650 bidi_move_to_visually_next deliver the current character,
5651 the newline, instead of advancing. */
5652 reseat (it, it->current.pos, 0);
5653 }
5654 }
5655 else if (skipped_p)
5656 reseat (it, it->current.pos, 0);
5657
5658 CHECK_IT (it);
5659 }
5660
5661
5662 \f
5663 /***********************************************************************
5664 Changing an iterator's position
5665 ***********************************************************************/
5666
5667 /* Change IT's current position to POS in current_buffer. If FORCE_P
5668 is non-zero, always check for text properties at the new position.
5669 Otherwise, text properties are only looked up if POS >=
5670 IT->check_charpos of a property. */
5671
5672 static void
5673 reseat (struct it *it, struct text_pos pos, int force_p)
5674 {
5675 EMACS_INT original_pos = IT_CHARPOS (*it);
5676
5677 reseat_1 (it, pos, 0);
5678
5679 /* Determine where to check text properties. Avoid doing it
5680 where possible because text property lookup is very expensive. */
5681 if (force_p
5682 || CHARPOS (pos) > it->stop_charpos
5683 || CHARPOS (pos) < original_pos)
5684 {
5685 if (it->bidi_p)
5686 {
5687 /* For bidi iteration, we need to prime prev_stop and
5688 base_level_stop with our best estimations. */
5689 if (CHARPOS (pos) < it->prev_stop)
5690 {
5691 handle_stop_backwards (it, BEGV);
5692 if (CHARPOS (pos) < it->base_level_stop)
5693 it->base_level_stop = 0;
5694 }
5695 else if (CHARPOS (pos) > it->stop_charpos
5696 && it->stop_charpos >= BEGV)
5697 handle_stop_backwards (it, it->stop_charpos);
5698 else /* force_p */
5699 handle_stop (it);
5700 }
5701 else
5702 {
5703 handle_stop (it);
5704 it->prev_stop = it->base_level_stop = 0;
5705 }
5706
5707 }
5708
5709 CHECK_IT (it);
5710 }
5711
5712
5713 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5714 IT->stop_pos to POS, also. */
5715
5716 static void
5717 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5718 {
5719 /* Don't call this function when scanning a C string. */
5720 xassert (it->s == NULL);
5721
5722 /* POS must be a reasonable value. */
5723 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5724
5725 it->current.pos = it->position = pos;
5726 it->end_charpos = ZV;
5727 it->dpvec = NULL;
5728 it->current.dpvec_index = -1;
5729 it->current.overlay_string_index = -1;
5730 IT_STRING_CHARPOS (*it) = -1;
5731 IT_STRING_BYTEPOS (*it) = -1;
5732 it->string = Qnil;
5733 it->method = GET_FROM_BUFFER;
5734 it->object = it->w->buffer;
5735 it->area = TEXT_AREA;
5736 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5737 it->sp = 0;
5738 it->string_from_display_prop_p = 0;
5739 it->face_before_selective_p = 0;
5740 if (it->bidi_p)
5741 {
5742 it->bidi_it.first_elt = 1;
5743 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5744 it->bidi_it.disp_pos = -1;
5745 it->bidi_it.string.s = NULL;
5746 it->bidi_it.string.lstring = Qnil;
5747 it->bidi_it.string.bufpos = 0;
5748 }
5749
5750 if (set_stop_p)
5751 {
5752 it->stop_charpos = CHARPOS (pos);
5753 it->base_level_stop = CHARPOS (pos);
5754 }
5755 }
5756
5757
5758 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5759 If S is non-null, it is a C string to iterate over. Otherwise,
5760 STRING gives a Lisp string to iterate over.
5761
5762 If PRECISION > 0, don't return more then PRECISION number of
5763 characters from the string.
5764
5765 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5766 characters have been returned. FIELD_WIDTH < 0 means an infinite
5767 field width.
5768
5769 MULTIBYTE = 0 means disable processing of multibyte characters,
5770 MULTIBYTE > 0 means enable it,
5771 MULTIBYTE < 0 means use IT->multibyte_p.
5772
5773 IT must be initialized via a prior call to init_iterator before
5774 calling this function. */
5775
5776 static void
5777 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5778 EMACS_INT charpos, EMACS_INT precision, int field_width,
5779 int multibyte)
5780 {
5781 /* No region in strings. */
5782 it->region_beg_charpos = it->region_end_charpos = -1;
5783
5784 /* No text property checks performed by default, but see below. */
5785 it->stop_charpos = -1;
5786
5787 /* Set iterator position and end position. */
5788 memset (&it->current, 0, sizeof it->current);
5789 it->current.overlay_string_index = -1;
5790 it->current.dpvec_index = -1;
5791 xassert (charpos >= 0);
5792
5793 /* If STRING is specified, use its multibyteness, otherwise use the
5794 setting of MULTIBYTE, if specified. */
5795 if (multibyte >= 0)
5796 it->multibyte_p = multibyte > 0;
5797
5798 /* Bidirectional reordering of strings is controlled by the default
5799 value of bidi-display-reordering. */
5800 it->bidi_p =
5801 !NILP (BVAR (&buffer_defaults, bidi_display_reordering))
5802 && it->multibyte_p;
5803
5804 if (s == NULL)
5805 {
5806 xassert (STRINGP (string));
5807 it->string = string;
5808 it->s = NULL;
5809 it->end_charpos = it->string_nchars = SCHARS (string);
5810 it->method = GET_FROM_STRING;
5811 it->current.string_pos = string_pos (charpos, string);
5812
5813 if (it->bidi_p)
5814 {
5815 it->bidi_it.string.lstring = string;
5816 it->bidi_it.string.s = NULL;
5817 it->bidi_it.string.schars = it->end_charpos;
5818 it->bidi_it.string.bufpos = 0;
5819 it->bidi_it.string.from_disp_str = 0;
5820 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5821 FRAME_WINDOW_P (it->f), &it->bidi_it);
5822 }
5823 }
5824 else
5825 {
5826 it->s = (const unsigned char *) s;
5827 it->string = Qnil;
5828
5829 /* Note that we use IT->current.pos, not it->current.string_pos,
5830 for displaying C strings. */
5831 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5832 if (it->multibyte_p)
5833 {
5834 it->current.pos = c_string_pos (charpos, s, 1);
5835 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5836
5837 if (it->bidi_p)
5838 {
5839 it->bidi_it.string.lstring = Qnil;
5840 it->bidi_it.string.s = s;
5841 it->bidi_it.string.schars = it->end_charpos;
5842 it->bidi_it.string.bufpos = 0;
5843 it->bidi_it.string.from_disp_str = 0;
5844 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5845 &it->bidi_it);
5846 }
5847 }
5848 else
5849 {
5850 /* Unibyte (a.k.a. ASCII) C strings are never bidi-reordered. */
5851 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5852 it->end_charpos = it->string_nchars = strlen (s);
5853 }
5854
5855 it->method = GET_FROM_C_STRING;
5856 }
5857
5858 /* PRECISION > 0 means don't return more than PRECISION characters
5859 from the string. */
5860 if (precision > 0 && it->end_charpos - charpos > precision)
5861 {
5862 it->end_charpos = it->string_nchars = charpos + precision;
5863 if (it->bidi_p)
5864 it->bidi_it.string.schars = it->end_charpos;
5865 }
5866
5867 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5868 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5869 FIELD_WIDTH < 0 means infinite field width. This is useful for
5870 padding with `-' at the end of a mode line. */
5871 if (field_width < 0)
5872 field_width = INFINITY;
5873 /* Implementation note: We deliberately don't enlarge
5874 it->bidi_it.string.schars here to fit it->end_charpos, because
5875 the bidi iterator cannot produce characters out of thin air. */
5876 if (field_width > it->end_charpos - charpos)
5877 it->end_charpos = charpos + field_width;
5878
5879 /* Use the standard display table for displaying strings. */
5880 if (DISP_TABLE_P (Vstandard_display_table))
5881 it->dp = XCHAR_TABLE (Vstandard_display_table);
5882
5883 it->stop_charpos = charpos;
5884 it->prev_stop = charpos;
5885 it->base_level_stop = 0;
5886 if (it->bidi_p)
5887 {
5888 it->bidi_it.first_elt = 1;
5889 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5890 it->bidi_it.disp_pos = -1;
5891 }
5892 if (s == NULL && it->multibyte_p)
5893 {
5894 EMACS_INT endpos = SCHARS (it->string);
5895 if (endpos > it->end_charpos)
5896 endpos = it->end_charpos;
5897 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5898 it->string);
5899 }
5900 CHECK_IT (it);
5901 }
5902
5903
5904 \f
5905 /***********************************************************************
5906 Iteration
5907 ***********************************************************************/
5908
5909 /* Map enum it_method value to corresponding next_element_from_* function. */
5910
5911 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5912 {
5913 next_element_from_buffer,
5914 next_element_from_display_vector,
5915 next_element_from_string,
5916 next_element_from_c_string,
5917 next_element_from_image,
5918 next_element_from_stretch
5919 };
5920
5921 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5922
5923
5924 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5925 (possibly with the following characters). */
5926
5927 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5928 ((IT)->cmp_it.id >= 0 \
5929 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5930 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5931 END_CHARPOS, (IT)->w, \
5932 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5933 (IT)->string)))
5934
5935
5936 /* Lookup the char-table Vglyphless_char_display for character C (-1
5937 if we want information for no-font case), and return the display
5938 method symbol. By side-effect, update it->what and
5939 it->glyphless_method. This function is called from
5940 get_next_display_element for each character element, and from
5941 x_produce_glyphs when no suitable font was found. */
5942
5943 Lisp_Object
5944 lookup_glyphless_char_display (int c, struct it *it)
5945 {
5946 Lisp_Object glyphless_method = Qnil;
5947
5948 if (CHAR_TABLE_P (Vglyphless_char_display)
5949 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5950 {
5951 if (c >= 0)
5952 {
5953 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
5954 if (CONSP (glyphless_method))
5955 glyphless_method = FRAME_WINDOW_P (it->f)
5956 ? XCAR (glyphless_method)
5957 : XCDR (glyphless_method);
5958 }
5959 else
5960 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
5961 }
5962
5963 retry:
5964 if (NILP (glyphless_method))
5965 {
5966 if (c >= 0)
5967 /* The default is to display the character by a proper font. */
5968 return Qnil;
5969 /* The default for the no-font case is to display an empty box. */
5970 glyphless_method = Qempty_box;
5971 }
5972 if (EQ (glyphless_method, Qzero_width))
5973 {
5974 if (c >= 0)
5975 return glyphless_method;
5976 /* This method can't be used for the no-font case. */
5977 glyphless_method = Qempty_box;
5978 }
5979 if (EQ (glyphless_method, Qthin_space))
5980 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5981 else if (EQ (glyphless_method, Qempty_box))
5982 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5983 else if (EQ (glyphless_method, Qhex_code))
5984 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5985 else if (STRINGP (glyphless_method))
5986 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5987 else
5988 {
5989 /* Invalid value. We use the default method. */
5990 glyphless_method = Qnil;
5991 goto retry;
5992 }
5993 it->what = IT_GLYPHLESS;
5994 return glyphless_method;
5995 }
5996
5997 /* Load IT's display element fields with information about the next
5998 display element from the current position of IT. Value is zero if
5999 end of buffer (or C string) is reached. */
6000
6001 static struct frame *last_escape_glyph_frame = NULL;
6002 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6003 static int last_escape_glyph_merged_face_id = 0;
6004
6005 struct frame *last_glyphless_glyph_frame = NULL;
6006 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6007 int last_glyphless_glyph_merged_face_id = 0;
6008
6009 static int
6010 get_next_display_element (struct it *it)
6011 {
6012 /* Non-zero means that we found a display element. Zero means that
6013 we hit the end of what we iterate over. Performance note: the
6014 function pointer `method' used here turns out to be faster than
6015 using a sequence of if-statements. */
6016 int success_p;
6017
6018 get_next:
6019 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6020
6021 if (it->what == IT_CHARACTER)
6022 {
6023 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6024 and only if (a) the resolved directionality of that character
6025 is R..." */
6026 /* FIXME: Do we need an exception for characters from display
6027 tables? */
6028 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6029 it->c = bidi_mirror_char (it->c);
6030 /* Map via display table or translate control characters.
6031 IT->c, IT->len etc. have been set to the next character by
6032 the function call above. If we have a display table, and it
6033 contains an entry for IT->c, translate it. Don't do this if
6034 IT->c itself comes from a display table, otherwise we could
6035 end up in an infinite recursion. (An alternative could be to
6036 count the recursion depth of this function and signal an
6037 error when a certain maximum depth is reached.) Is it worth
6038 it? */
6039 if (success_p && it->dpvec == NULL)
6040 {
6041 Lisp_Object dv;
6042 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6043 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6044 nbsp_or_shy = char_is_other;
6045 int c = it->c; /* This is the character to display. */
6046
6047 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6048 {
6049 xassert (SINGLE_BYTE_CHAR_P (c));
6050 if (unibyte_display_via_language_environment)
6051 {
6052 c = DECODE_CHAR (unibyte, c);
6053 if (c < 0)
6054 c = BYTE8_TO_CHAR (it->c);
6055 }
6056 else
6057 c = BYTE8_TO_CHAR (it->c);
6058 }
6059
6060 if (it->dp
6061 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6062 VECTORP (dv)))
6063 {
6064 struct Lisp_Vector *v = XVECTOR (dv);
6065
6066 /* Return the first character from the display table
6067 entry, if not empty. If empty, don't display the
6068 current character. */
6069 if (v->header.size)
6070 {
6071 it->dpvec_char_len = it->len;
6072 it->dpvec = v->contents;
6073 it->dpend = v->contents + v->header.size;
6074 it->current.dpvec_index = 0;
6075 it->dpvec_face_id = -1;
6076 it->saved_face_id = it->face_id;
6077 it->method = GET_FROM_DISPLAY_VECTOR;
6078 it->ellipsis_p = 0;
6079 }
6080 else
6081 {
6082 set_iterator_to_next (it, 0);
6083 }
6084 goto get_next;
6085 }
6086
6087 if (! NILP (lookup_glyphless_char_display (c, it)))
6088 {
6089 if (it->what == IT_GLYPHLESS)
6090 goto done;
6091 /* Don't display this character. */
6092 set_iterator_to_next (it, 0);
6093 goto get_next;
6094 }
6095
6096 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6097 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6098 : c == 0xAD ? char_is_soft_hyphen
6099 : char_is_other);
6100
6101 /* Translate control characters into `\003' or `^C' form.
6102 Control characters coming from a display table entry are
6103 currently not translated because we use IT->dpvec to hold
6104 the translation. This could easily be changed but I
6105 don't believe that it is worth doing.
6106
6107 NBSP and SOFT-HYPEN are property translated too.
6108
6109 Non-printable characters and raw-byte characters are also
6110 translated to octal form. */
6111 if (((c < ' ' || c == 127) /* ASCII control chars */
6112 ? (it->area != TEXT_AREA
6113 /* In mode line, treat \n, \t like other crl chars. */
6114 || (c != '\t'
6115 && it->glyph_row
6116 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6117 || (c != '\n' && c != '\t'))
6118 : (nbsp_or_shy
6119 || CHAR_BYTE8_P (c)
6120 || ! CHAR_PRINTABLE_P (c))))
6121 {
6122 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6123 or a non-printable character which must be displayed
6124 either as '\003' or as `^C' where the '\\' and '^'
6125 can be defined in the display table. Fill
6126 IT->ctl_chars with glyphs for what we have to
6127 display. Then, set IT->dpvec to these glyphs. */
6128 Lisp_Object gc;
6129 int ctl_len;
6130 int face_id, lface_id = 0 ;
6131 int escape_glyph;
6132
6133 /* Handle control characters with ^. */
6134
6135 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6136 {
6137 int g;
6138
6139 g = '^'; /* default glyph for Control */
6140 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6141 if (it->dp
6142 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6143 && GLYPH_CODE_CHAR_VALID_P (gc))
6144 {
6145 g = GLYPH_CODE_CHAR (gc);
6146 lface_id = GLYPH_CODE_FACE (gc);
6147 }
6148 if (lface_id)
6149 {
6150 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6151 }
6152 else if (it->f == last_escape_glyph_frame
6153 && it->face_id == last_escape_glyph_face_id)
6154 {
6155 face_id = last_escape_glyph_merged_face_id;
6156 }
6157 else
6158 {
6159 /* Merge the escape-glyph face into the current face. */
6160 face_id = merge_faces (it->f, Qescape_glyph, 0,
6161 it->face_id);
6162 last_escape_glyph_frame = it->f;
6163 last_escape_glyph_face_id = it->face_id;
6164 last_escape_glyph_merged_face_id = face_id;
6165 }
6166
6167 XSETINT (it->ctl_chars[0], g);
6168 XSETINT (it->ctl_chars[1], c ^ 0100);
6169 ctl_len = 2;
6170 goto display_control;
6171 }
6172
6173 /* Handle non-break space in the mode where it only gets
6174 highlighting. */
6175
6176 if (EQ (Vnobreak_char_display, Qt)
6177 && nbsp_or_shy == char_is_nbsp)
6178 {
6179 /* Merge the no-break-space face into the current face. */
6180 face_id = merge_faces (it->f, Qnobreak_space, 0,
6181 it->face_id);
6182
6183 c = ' ';
6184 XSETINT (it->ctl_chars[0], ' ');
6185 ctl_len = 1;
6186 goto display_control;
6187 }
6188
6189 /* Handle sequences that start with the "escape glyph". */
6190
6191 /* the default escape glyph is \. */
6192 escape_glyph = '\\';
6193
6194 if (it->dp
6195 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6196 && GLYPH_CODE_CHAR_VALID_P (gc))
6197 {
6198 escape_glyph = GLYPH_CODE_CHAR (gc);
6199 lface_id = GLYPH_CODE_FACE (gc);
6200 }
6201 if (lface_id)
6202 {
6203 /* The display table specified a face.
6204 Merge it into face_id and also into escape_glyph. */
6205 face_id = merge_faces (it->f, Qt, lface_id,
6206 it->face_id);
6207 }
6208 else if (it->f == last_escape_glyph_frame
6209 && it->face_id == last_escape_glyph_face_id)
6210 {
6211 face_id = last_escape_glyph_merged_face_id;
6212 }
6213 else
6214 {
6215 /* Merge the escape-glyph face into the current face. */
6216 face_id = merge_faces (it->f, Qescape_glyph, 0,
6217 it->face_id);
6218 last_escape_glyph_frame = it->f;
6219 last_escape_glyph_face_id = it->face_id;
6220 last_escape_glyph_merged_face_id = face_id;
6221 }
6222
6223 /* Handle soft hyphens in the mode where they only get
6224 highlighting. */
6225
6226 if (EQ (Vnobreak_char_display, Qt)
6227 && nbsp_or_shy == char_is_soft_hyphen)
6228 {
6229 XSETINT (it->ctl_chars[0], '-');
6230 ctl_len = 1;
6231 goto display_control;
6232 }
6233
6234 /* Handle non-break space and soft hyphen
6235 with the escape glyph. */
6236
6237 if (nbsp_or_shy)
6238 {
6239 XSETINT (it->ctl_chars[0], escape_glyph);
6240 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6241 XSETINT (it->ctl_chars[1], c);
6242 ctl_len = 2;
6243 goto display_control;
6244 }
6245
6246 {
6247 char str[10];
6248 int len, i;
6249
6250 if (CHAR_BYTE8_P (c))
6251 /* Display \200 instead of \17777600. */
6252 c = CHAR_TO_BYTE8 (c);
6253 len = sprintf (str, "%03o", c);
6254
6255 XSETINT (it->ctl_chars[0], escape_glyph);
6256 for (i = 0; i < len; i++)
6257 XSETINT (it->ctl_chars[i + 1], str[i]);
6258 ctl_len = len + 1;
6259 }
6260
6261 display_control:
6262 /* Set up IT->dpvec and return first character from it. */
6263 it->dpvec_char_len = it->len;
6264 it->dpvec = it->ctl_chars;
6265 it->dpend = it->dpvec + ctl_len;
6266 it->current.dpvec_index = 0;
6267 it->dpvec_face_id = face_id;
6268 it->saved_face_id = it->face_id;
6269 it->method = GET_FROM_DISPLAY_VECTOR;
6270 it->ellipsis_p = 0;
6271 goto get_next;
6272 }
6273 it->char_to_display = c;
6274 }
6275 else if (success_p)
6276 {
6277 it->char_to_display = it->c;
6278 }
6279 }
6280
6281 /* Adjust face id for a multibyte character. There are no multibyte
6282 character in unibyte text. */
6283 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6284 && it->multibyte_p
6285 && success_p
6286 && FRAME_WINDOW_P (it->f))
6287 {
6288 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6289
6290 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6291 {
6292 /* Automatic composition with glyph-string. */
6293 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6294
6295 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6296 }
6297 else
6298 {
6299 EMACS_INT pos = (it->s ? -1
6300 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6301 : IT_CHARPOS (*it));
6302
6303 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6304 it->string);
6305 }
6306 }
6307
6308 done:
6309 /* Is this character the last one of a run of characters with
6310 box? If yes, set IT->end_of_box_run_p to 1. */
6311 if (it->face_box_p
6312 && it->s == NULL)
6313 {
6314 if (it->method == GET_FROM_STRING && it->sp)
6315 {
6316 int face_id = underlying_face_id (it);
6317 struct face *face = FACE_FROM_ID (it->f, face_id);
6318
6319 if (face)
6320 {
6321 if (face->box == FACE_NO_BOX)
6322 {
6323 /* If the box comes from face properties in a
6324 display string, check faces in that string. */
6325 int string_face_id = face_after_it_pos (it);
6326 it->end_of_box_run_p
6327 = (FACE_FROM_ID (it->f, string_face_id)->box
6328 == FACE_NO_BOX);
6329 }
6330 /* Otherwise, the box comes from the underlying face.
6331 If this is the last string character displayed, check
6332 the next buffer location. */
6333 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6334 && (it->current.overlay_string_index
6335 == it->n_overlay_strings - 1))
6336 {
6337 EMACS_INT ignore;
6338 int next_face_id;
6339 struct text_pos pos = it->current.pos;
6340 INC_TEXT_POS (pos, it->multibyte_p);
6341
6342 next_face_id = face_at_buffer_position
6343 (it->w, CHARPOS (pos), it->region_beg_charpos,
6344 it->region_end_charpos, &ignore,
6345 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6346 -1);
6347 it->end_of_box_run_p
6348 = (FACE_FROM_ID (it->f, next_face_id)->box
6349 == FACE_NO_BOX);
6350 }
6351 }
6352 }
6353 else
6354 {
6355 int face_id = face_after_it_pos (it);
6356 it->end_of_box_run_p
6357 = (face_id != it->face_id
6358 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6359 }
6360 }
6361
6362 /* Value is 0 if end of buffer or string reached. */
6363 return success_p;
6364 }
6365
6366
6367 /* Move IT to the next display element.
6368
6369 RESEAT_P non-zero means if called on a newline in buffer text,
6370 skip to the next visible line start.
6371
6372 Functions get_next_display_element and set_iterator_to_next are
6373 separate because I find this arrangement easier to handle than a
6374 get_next_display_element function that also increments IT's
6375 position. The way it is we can first look at an iterator's current
6376 display element, decide whether it fits on a line, and if it does,
6377 increment the iterator position. The other way around we probably
6378 would either need a flag indicating whether the iterator has to be
6379 incremented the next time, or we would have to implement a
6380 decrement position function which would not be easy to write. */
6381
6382 void
6383 set_iterator_to_next (struct it *it, int reseat_p)
6384 {
6385 /* Reset flags indicating start and end of a sequence of characters
6386 with box. Reset them at the start of this function because
6387 moving the iterator to a new position might set them. */
6388 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6389
6390 switch (it->method)
6391 {
6392 case GET_FROM_BUFFER:
6393 /* The current display element of IT is a character from
6394 current_buffer. Advance in the buffer, and maybe skip over
6395 invisible lines that are so because of selective display. */
6396 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6397 reseat_at_next_visible_line_start (it, 0);
6398 else if (it->cmp_it.id >= 0)
6399 {
6400 /* We are currently getting glyphs from a composition. */
6401 int i;
6402
6403 if (! it->bidi_p)
6404 {
6405 IT_CHARPOS (*it) += it->cmp_it.nchars;
6406 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6407 if (it->cmp_it.to < it->cmp_it.nglyphs)
6408 {
6409 it->cmp_it.from = it->cmp_it.to;
6410 }
6411 else
6412 {
6413 it->cmp_it.id = -1;
6414 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6415 IT_BYTEPOS (*it),
6416 it->end_charpos, Qnil);
6417 }
6418 }
6419 else if (! it->cmp_it.reversed_p)
6420 {
6421 /* Composition created while scanning forward. */
6422 /* Update IT's char/byte positions to point to the first
6423 character of the next grapheme cluster, or to the
6424 character visually after the current composition. */
6425 for (i = 0; i < it->cmp_it.nchars; i++)
6426 bidi_move_to_visually_next (&it->bidi_it);
6427 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6428 IT_CHARPOS (*it) = it->bidi_it.charpos;
6429
6430 if (it->cmp_it.to < it->cmp_it.nglyphs)
6431 {
6432 /* Proceed to the next grapheme cluster. */
6433 it->cmp_it.from = it->cmp_it.to;
6434 }
6435 else
6436 {
6437 /* No more grapheme clusters in this composition.
6438 Find the next stop position. */
6439 EMACS_INT stop = it->end_charpos;
6440 if (it->bidi_it.scan_dir < 0)
6441 /* Now we are scanning backward and don't know
6442 where to stop. */
6443 stop = -1;
6444 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6445 IT_BYTEPOS (*it), stop, Qnil);
6446 }
6447 }
6448 else
6449 {
6450 /* Composition created while scanning backward. */
6451 /* Update IT's char/byte positions to point to the last
6452 character of the previous grapheme cluster, or the
6453 character visually after the current composition. */
6454 for (i = 0; i < it->cmp_it.nchars; i++)
6455 bidi_move_to_visually_next (&it->bidi_it);
6456 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6457 IT_CHARPOS (*it) = it->bidi_it.charpos;
6458 if (it->cmp_it.from > 0)
6459 {
6460 /* Proceed to the previous grapheme cluster. */
6461 it->cmp_it.to = it->cmp_it.from;
6462 }
6463 else
6464 {
6465 /* No more grapheme clusters in this composition.
6466 Find the next stop position. */
6467 EMACS_INT stop = it->end_charpos;
6468 if (it->bidi_it.scan_dir < 0)
6469 /* Now we are scanning backward and don't know
6470 where to stop. */
6471 stop = -1;
6472 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6473 IT_BYTEPOS (*it), stop, Qnil);
6474 }
6475 }
6476 }
6477 else
6478 {
6479 xassert (it->len != 0);
6480
6481 if (!it->bidi_p)
6482 {
6483 IT_BYTEPOS (*it) += it->len;
6484 IT_CHARPOS (*it) += 1;
6485 }
6486 else
6487 {
6488 int prev_scan_dir = it->bidi_it.scan_dir;
6489 /* If this is a new paragraph, determine its base
6490 direction (a.k.a. its base embedding level). */
6491 if (it->bidi_it.new_paragraph)
6492 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6493 bidi_move_to_visually_next (&it->bidi_it);
6494 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6495 IT_CHARPOS (*it) = it->bidi_it.charpos;
6496 if (prev_scan_dir != it->bidi_it.scan_dir)
6497 {
6498 /* As the scan direction was changed, we must
6499 re-compute the stop position for composition. */
6500 EMACS_INT stop = it->end_charpos;
6501 if (it->bidi_it.scan_dir < 0)
6502 stop = -1;
6503 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6504 IT_BYTEPOS (*it), stop, Qnil);
6505 }
6506 }
6507 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6508 }
6509 break;
6510
6511 case GET_FROM_C_STRING:
6512 /* Current display element of IT is from a C string. */
6513 if (!it->bidi_p
6514 /* If the string position is beyond string_nchars, it means
6515 next_element_from_c_string is padding the string with
6516 blanks, in which case we bypass the bidi iterator,
6517 because it cannot deal with such virtual characters. */
6518 || IT_CHARPOS (*it) >= it->string_nchars)
6519 {
6520 IT_BYTEPOS (*it) += it->len;
6521 IT_CHARPOS (*it) += 1;
6522 }
6523 else
6524 {
6525 bidi_move_to_visually_next (&it->bidi_it);
6526 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6527 IT_CHARPOS (*it) = it->bidi_it.charpos;
6528 }
6529 break;
6530
6531 case GET_FROM_DISPLAY_VECTOR:
6532 /* Current display element of IT is from a display table entry.
6533 Advance in the display table definition. Reset it to null if
6534 end reached, and continue with characters from buffers/
6535 strings. */
6536 ++it->current.dpvec_index;
6537
6538 /* Restore face of the iterator to what they were before the
6539 display vector entry (these entries may contain faces). */
6540 it->face_id = it->saved_face_id;
6541
6542 if (it->dpvec + it->current.dpvec_index == it->dpend)
6543 {
6544 int recheck_faces = it->ellipsis_p;
6545
6546 if (it->s)
6547 it->method = GET_FROM_C_STRING;
6548 else if (STRINGP (it->string))
6549 it->method = GET_FROM_STRING;
6550 else
6551 {
6552 it->method = GET_FROM_BUFFER;
6553 it->object = it->w->buffer;
6554 }
6555
6556 it->dpvec = NULL;
6557 it->current.dpvec_index = -1;
6558
6559 /* Skip over characters which were displayed via IT->dpvec. */
6560 if (it->dpvec_char_len < 0)
6561 reseat_at_next_visible_line_start (it, 1);
6562 else if (it->dpvec_char_len > 0)
6563 {
6564 if (it->method == GET_FROM_STRING
6565 && it->n_overlay_strings > 0)
6566 it->ignore_overlay_strings_at_pos_p = 1;
6567 it->len = it->dpvec_char_len;
6568 set_iterator_to_next (it, reseat_p);
6569 }
6570
6571 /* Maybe recheck faces after display vector */
6572 if (recheck_faces)
6573 it->stop_charpos = IT_CHARPOS (*it);
6574 }
6575 break;
6576
6577 case GET_FROM_STRING:
6578 /* Current display element is a character from a Lisp string. */
6579 xassert (it->s == NULL && STRINGP (it->string));
6580 if (it->cmp_it.id >= 0)
6581 {
6582 int i;
6583
6584 if (! it->bidi_p)
6585 {
6586 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6587 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6588 if (it->cmp_it.to < it->cmp_it.nglyphs)
6589 it->cmp_it.from = it->cmp_it.to;
6590 else
6591 {
6592 it->cmp_it.id = -1;
6593 composition_compute_stop_pos (&it->cmp_it,
6594 IT_STRING_CHARPOS (*it),
6595 IT_STRING_BYTEPOS (*it),
6596 it->end_charpos, it->string);
6597 }
6598 }
6599 else if (! it->cmp_it.reversed_p)
6600 {
6601 for (i = 0; i < it->cmp_it.nchars; i++)
6602 bidi_move_to_visually_next (&it->bidi_it);
6603 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6604 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6605
6606 if (it->cmp_it.to < it->cmp_it.nglyphs)
6607 it->cmp_it.from = it->cmp_it.to;
6608 else
6609 {
6610 EMACS_INT stop = it->end_charpos;
6611 if (it->bidi_it.scan_dir < 0)
6612 stop = -1;
6613 composition_compute_stop_pos (&it->cmp_it,
6614 IT_STRING_CHARPOS (*it),
6615 IT_STRING_BYTEPOS (*it), stop,
6616 it->string);
6617 }
6618 }
6619 else
6620 {
6621 for (i = 0; i < it->cmp_it.nchars; i++)
6622 bidi_move_to_visually_next (&it->bidi_it);
6623 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6624 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6625 if (it->cmp_it.from > 0)
6626 it->cmp_it.to = it->cmp_it.from;
6627 else
6628 {
6629 EMACS_INT stop = it->end_charpos;
6630 if (it->bidi_it.scan_dir < 0)
6631 stop = -1;
6632 composition_compute_stop_pos (&it->cmp_it,
6633 IT_STRING_CHARPOS (*it),
6634 IT_STRING_BYTEPOS (*it), stop,
6635 it->string);
6636 }
6637 }
6638 }
6639 else
6640 {
6641 if (!it->bidi_p
6642 /* If the string position is beyond string_nchars, it
6643 means next_element_from_string is padding the string
6644 with blanks, in which case we bypass the bidi
6645 iterator, because it cannot deal with such virtual
6646 characters. */
6647 || IT_STRING_CHARPOS (*it) >= it->string_nchars)
6648 {
6649 IT_STRING_BYTEPOS (*it) += it->len;
6650 IT_STRING_CHARPOS (*it) += 1;
6651 }
6652 else
6653 {
6654 int prev_scan_dir = it->bidi_it.scan_dir;
6655
6656 bidi_move_to_visually_next (&it->bidi_it);
6657 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6658 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6659 if (prev_scan_dir != it->bidi_it.scan_dir)
6660 {
6661 EMACS_INT stop = it->end_charpos;
6662
6663 if (it->bidi_it.scan_dir < 0)
6664 stop = -1;
6665 composition_compute_stop_pos (&it->cmp_it,
6666 IT_STRING_CHARPOS (*it),
6667 IT_STRING_BYTEPOS (*it), stop,
6668 it->string);
6669 }
6670 }
6671 }
6672
6673 consider_string_end:
6674
6675 if (it->current.overlay_string_index >= 0)
6676 {
6677 /* IT->string is an overlay string. Advance to the
6678 next, if there is one. */
6679 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6680 {
6681 it->ellipsis_p = 0;
6682 next_overlay_string (it);
6683 if (it->ellipsis_p)
6684 setup_for_ellipsis (it, 0);
6685 }
6686 }
6687 else
6688 {
6689 /* IT->string is not an overlay string. If we reached
6690 its end, and there is something on IT->stack, proceed
6691 with what is on the stack. This can be either another
6692 string, this time an overlay string, or a buffer. */
6693 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6694 && it->sp > 0)
6695 {
6696 pop_it (it);
6697 if (it->method == GET_FROM_STRING)
6698 goto consider_string_end;
6699 }
6700 }
6701 break;
6702
6703 case GET_FROM_IMAGE:
6704 case GET_FROM_STRETCH:
6705 /* The position etc with which we have to proceed are on
6706 the stack. The position may be at the end of a string,
6707 if the `display' property takes up the whole string. */
6708 xassert (it->sp > 0);
6709 pop_it (it);
6710 if (it->method == GET_FROM_STRING)
6711 goto consider_string_end;
6712 break;
6713
6714 default:
6715 /* There are no other methods defined, so this should be a bug. */
6716 abort ();
6717 }
6718
6719 xassert (it->method != GET_FROM_STRING
6720 || (STRINGP (it->string)
6721 && IT_STRING_CHARPOS (*it) >= 0));
6722 }
6723
6724 /* Load IT's display element fields with information about the next
6725 display element which comes from a display table entry or from the
6726 result of translating a control character to one of the forms `^C'
6727 or `\003'.
6728
6729 IT->dpvec holds the glyphs to return as characters.
6730 IT->saved_face_id holds the face id before the display vector--it
6731 is restored into IT->face_id in set_iterator_to_next. */
6732
6733 static int
6734 next_element_from_display_vector (struct it *it)
6735 {
6736 Lisp_Object gc;
6737
6738 /* Precondition. */
6739 xassert (it->dpvec && it->current.dpvec_index >= 0);
6740
6741 it->face_id = it->saved_face_id;
6742
6743 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6744 That seemed totally bogus - so I changed it... */
6745 gc = it->dpvec[it->current.dpvec_index];
6746
6747 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6748 {
6749 it->c = GLYPH_CODE_CHAR (gc);
6750 it->len = CHAR_BYTES (it->c);
6751
6752 /* The entry may contain a face id to use. Such a face id is
6753 the id of a Lisp face, not a realized face. A face id of
6754 zero means no face is specified. */
6755 if (it->dpvec_face_id >= 0)
6756 it->face_id = it->dpvec_face_id;
6757 else
6758 {
6759 int lface_id = GLYPH_CODE_FACE (gc);
6760 if (lface_id > 0)
6761 it->face_id = merge_faces (it->f, Qt, lface_id,
6762 it->saved_face_id);
6763 }
6764 }
6765 else
6766 /* Display table entry is invalid. Return a space. */
6767 it->c = ' ', it->len = 1;
6768
6769 /* Don't change position and object of the iterator here. They are
6770 still the values of the character that had this display table
6771 entry or was translated, and that's what we want. */
6772 it->what = IT_CHARACTER;
6773 return 1;
6774 }
6775
6776 /* Get the first element of string/buffer in the visual order, after
6777 being reseated to a new position in a string or a buffer. */
6778 static void
6779 get_visually_first_element (struct it *it)
6780 {
6781 int string_p = STRINGP (it->string) || it->s;
6782 EMACS_INT eob = (string_p ? it->string_nchars : ZV);
6783 EMACS_INT bob = (string_p ? 0 : BEGV);
6784
6785 if (STRINGP (it->string))
6786 {
6787 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6788 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6789 }
6790 else
6791 {
6792 it->bidi_it.charpos = IT_CHARPOS (*it);
6793 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6794 }
6795
6796 if (it->bidi_it.charpos == eob)
6797 {
6798 /* Nothing to do, but reset the FIRST_ELT flag, like
6799 bidi_paragraph_init does, because we are not going to
6800 call it. */
6801 it->bidi_it.first_elt = 0;
6802 }
6803 else if (it->bidi_it.charpos == bob
6804 || (!string_p
6805 /* FIXME: Should support all Unicode line separators. */
6806 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6807 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6808 {
6809 /* If we are at the beginning of a line/string, we can produce
6810 the next element right away. */
6811 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6812 bidi_move_to_visually_next (&it->bidi_it);
6813 }
6814 else
6815 {
6816 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6817
6818 /* We need to prime the bidi iterator starting at the line's or
6819 string's beginning, before we will be able to produce the
6820 next element. */
6821 if (string_p)
6822 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6823 else
6824 {
6825 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6826 -1);
6827 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6828 }
6829 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6830 do
6831 {
6832 /* Now return to buffer/string position where we were asked
6833 to get the next display element, and produce that. */
6834 bidi_move_to_visually_next (&it->bidi_it);
6835 }
6836 while (it->bidi_it.bytepos != orig_bytepos
6837 && it->bidi_it.charpos < eob);
6838 }
6839
6840 /* Adjust IT's position information to where we ended up. */
6841 if (STRINGP (it->string))
6842 {
6843 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6844 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6845 }
6846 else
6847 {
6848 IT_CHARPOS (*it) = it->bidi_it.charpos;
6849 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6850 }
6851
6852 if (STRINGP (it->string) || !it->s)
6853 {
6854 EMACS_INT stop, charpos, bytepos;
6855
6856 if (STRINGP (it->string))
6857 {
6858 xassert (!it->s);
6859 stop = SCHARS (it->string);
6860 if (stop > it->end_charpos)
6861 stop = it->end_charpos;
6862 charpos = IT_STRING_CHARPOS (*it);
6863 bytepos = IT_STRING_BYTEPOS (*it);
6864 }
6865 else
6866 {
6867 stop = it->end_charpos;
6868 charpos = IT_CHARPOS (*it);
6869 bytepos = IT_BYTEPOS (*it);
6870 }
6871 if (it->bidi_it.scan_dir < 0)
6872 stop = -1;
6873 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
6874 it->string);
6875 }
6876 }
6877
6878 /* Load IT with the next display element from Lisp string IT->string.
6879 IT->current.string_pos is the current position within the string.
6880 If IT->current.overlay_string_index >= 0, the Lisp string is an
6881 overlay string. */
6882
6883 static int
6884 next_element_from_string (struct it *it)
6885 {
6886 struct text_pos position;
6887
6888 xassert (STRINGP (it->string));
6889 xassert (!it->bidi_p || it->string == it->bidi_it.string.lstring);
6890 xassert (IT_STRING_CHARPOS (*it) >= 0);
6891 position = it->current.string_pos;
6892
6893 /* With bidi reordering, the character to display might not be the
6894 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
6895 that we were reseat()ed to a new string, whose paragraph
6896 direction is not known. */
6897 if (it->bidi_p && it->bidi_it.first_elt)
6898 {
6899 get_visually_first_element (it);
6900 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
6901 }
6902
6903 /* Time to check for invisible text? */
6904 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
6905 {
6906 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
6907 {
6908 if (!(!it->bidi_p
6909 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6910 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
6911 {
6912 /* With bidi non-linear iteration, we could find
6913 ourselves far beyond the last computed stop_charpos,
6914 with several other stop positions in between that we
6915 missed. Scan them all now, in buffer's logical
6916 order, until we find and handle the last stop_charpos
6917 that precedes our current position. */
6918 handle_stop_backwards (it, it->stop_charpos);
6919 return GET_NEXT_DISPLAY_ELEMENT (it);
6920 }
6921 else
6922 {
6923 if (it->bidi_p)
6924 {
6925 /* Take note of the stop position we just moved
6926 across, for when we will move back across it. */
6927 it->prev_stop = it->stop_charpos;
6928 /* If we are at base paragraph embedding level, take
6929 note of the last stop position seen at this
6930 level. */
6931 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6932 it->base_level_stop = it->stop_charpos;
6933 }
6934 handle_stop (it);
6935
6936 /* Since a handler may have changed IT->method, we must
6937 recurse here. */
6938 return GET_NEXT_DISPLAY_ELEMENT (it);
6939 }
6940 }
6941 else if (it->bidi_p
6942 /* If we are before prev_stop, we may have overstepped
6943 on our way backwards a stop_pos, and if so, we need
6944 to handle that stop_pos. */
6945 && IT_STRING_CHARPOS (*it) < it->prev_stop
6946 /* We can sometimes back up for reasons that have nothing
6947 to do with bidi reordering. E.g., compositions. The
6948 code below is only needed when we are above the base
6949 embedding level, so test for that explicitly. */
6950 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
6951 {
6952 /* If we lost track of base_level_stop, we have no better place
6953 for handle_stop_backwards to start from than BEGV. This
6954 happens, e.g., when we were reseated to the previous
6955 screenful of text by vertical-motion. */
6956 if (it->base_level_stop <= 0
6957 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
6958 it->base_level_stop = 0;
6959 handle_stop_backwards (it, it->base_level_stop);
6960 return GET_NEXT_DISPLAY_ELEMENT (it);
6961 }
6962 }
6963
6964 if (it->current.overlay_string_index >= 0)
6965 {
6966 /* Get the next character from an overlay string. In overlay
6967 strings, There is no field width or padding with spaces to
6968 do. */
6969 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6970 {
6971 it->what = IT_EOB;
6972 return 0;
6973 }
6974 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6975 IT_STRING_BYTEPOS (*it),
6976 it->bidi_it.scan_dir < 0
6977 ? -1
6978 : SCHARS (it->string))
6979 && next_element_from_composition (it))
6980 {
6981 return 1;
6982 }
6983 else if (STRING_MULTIBYTE (it->string))
6984 {
6985 const unsigned char *s = (SDATA (it->string)
6986 + IT_STRING_BYTEPOS (*it));
6987 it->c = string_char_and_length (s, &it->len);
6988 }
6989 else
6990 {
6991 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6992 it->len = 1;
6993 }
6994 }
6995 else
6996 {
6997 /* Get the next character from a Lisp string that is not an
6998 overlay string. Such strings come from the mode line, for
6999 example. We may have to pad with spaces, or truncate the
7000 string. See also next_element_from_c_string. */
7001 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7002 {
7003 it->what = IT_EOB;
7004 return 0;
7005 }
7006 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7007 {
7008 /* Pad with spaces. */
7009 it->c = ' ', it->len = 1;
7010 CHARPOS (position) = BYTEPOS (position) = -1;
7011 }
7012 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7013 IT_STRING_BYTEPOS (*it),
7014 it->bidi_it.scan_dir < 0
7015 ? -1
7016 : it->string_nchars)
7017 && next_element_from_composition (it))
7018 {
7019 return 1;
7020 }
7021 else if (STRING_MULTIBYTE (it->string))
7022 {
7023 const unsigned char *s = (SDATA (it->string)
7024 + IT_STRING_BYTEPOS (*it));
7025 it->c = string_char_and_length (s, &it->len);
7026 }
7027 else
7028 {
7029 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7030 it->len = 1;
7031 }
7032 }
7033
7034 /* Record what we have and where it came from. */
7035 it->what = IT_CHARACTER;
7036 it->object = it->string;
7037 it->position = position;
7038 return 1;
7039 }
7040
7041
7042 /* Load IT with next display element from C string IT->s.
7043 IT->string_nchars is the maximum number of characters to return
7044 from the string. IT->end_charpos may be greater than
7045 IT->string_nchars when this function is called, in which case we
7046 may have to return padding spaces. Value is zero if end of string
7047 reached, including padding spaces. */
7048
7049 static int
7050 next_element_from_c_string (struct it *it)
7051 {
7052 int success_p = 1;
7053
7054 xassert (it->s);
7055 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7056 it->what = IT_CHARACTER;
7057 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7058 it->object = Qnil;
7059
7060 /* With bidi reordering, the character to display might not be the
7061 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7062 we were reseated to a new string, whose paragraph direction is
7063 not known. */
7064 if (it->bidi_p && it->bidi_it.first_elt)
7065 get_visually_first_element (it);
7066
7067 /* IT's position can be greater than IT->string_nchars in case a
7068 field width or precision has been specified when the iterator was
7069 initialized. */
7070 if (IT_CHARPOS (*it) >= it->end_charpos)
7071 {
7072 /* End of the game. */
7073 it->what = IT_EOB;
7074 success_p = 0;
7075 }
7076 else if (IT_CHARPOS (*it) >= it->string_nchars)
7077 {
7078 /* Pad with spaces. */
7079 it->c = ' ', it->len = 1;
7080 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7081 }
7082 else if (it->multibyte_p)
7083 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7084 else
7085 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7086
7087 return success_p;
7088 }
7089
7090
7091 /* Set up IT to return characters from an ellipsis, if appropriate.
7092 The definition of the ellipsis glyphs may come from a display table
7093 entry. This function fills IT with the first glyph from the
7094 ellipsis if an ellipsis is to be displayed. */
7095
7096 static int
7097 next_element_from_ellipsis (struct it *it)
7098 {
7099 if (it->selective_display_ellipsis_p)
7100 setup_for_ellipsis (it, it->len);
7101 else
7102 {
7103 /* The face at the current position may be different from the
7104 face we find after the invisible text. Remember what it
7105 was in IT->saved_face_id, and signal that it's there by
7106 setting face_before_selective_p. */
7107 it->saved_face_id = it->face_id;
7108 it->method = GET_FROM_BUFFER;
7109 it->object = it->w->buffer;
7110 reseat_at_next_visible_line_start (it, 1);
7111 it->face_before_selective_p = 1;
7112 }
7113
7114 return GET_NEXT_DISPLAY_ELEMENT (it);
7115 }
7116
7117
7118 /* Deliver an image display element. The iterator IT is already
7119 filled with image information (done in handle_display_prop). Value
7120 is always 1. */
7121
7122
7123 static int
7124 next_element_from_image (struct it *it)
7125 {
7126 it->what = IT_IMAGE;
7127 it->ignore_overlay_strings_at_pos_p = 0;
7128 return 1;
7129 }
7130
7131
7132 /* Fill iterator IT with next display element from a stretch glyph
7133 property. IT->object is the value of the text property. Value is
7134 always 1. */
7135
7136 static int
7137 next_element_from_stretch (struct it *it)
7138 {
7139 it->what = IT_STRETCH;
7140 return 1;
7141 }
7142
7143 /* Scan forward from CHARPOS in the current buffer/string, until we
7144 find a stop position > current IT's position. Then handle the stop
7145 position before that. This is called when we bump into a stop
7146 position while reordering bidirectional text. CHARPOS should be
7147 the last previously processed stop_pos (or BEGV/0, if none were
7148 processed yet) whose position is less that IT's current
7149 position. */
7150
7151 static void
7152 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7153 {
7154 int bufp = !STRINGP (it->string);
7155 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7156 struct display_pos save_current = it->current;
7157 struct text_pos save_position = it->position;
7158 struct text_pos pos1;
7159 EMACS_INT next_stop;
7160
7161 /* Scan in strict logical order. */
7162 it->bidi_p = 0;
7163 do
7164 {
7165 it->prev_stop = charpos;
7166 if (bufp)
7167 {
7168 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7169 reseat_1 (it, pos1, 0);
7170 }
7171 else
7172 it->current.string_pos = string_pos (charpos, it->string);
7173 compute_stop_pos (it);
7174 /* We must advance forward, right? */
7175 if (it->stop_charpos <= it->prev_stop)
7176 abort ();
7177 charpos = it->stop_charpos;
7178 }
7179 while (charpos <= where_we_are);
7180
7181 next_stop = it->stop_charpos;
7182 it->stop_charpos = it->prev_stop;
7183 it->bidi_p = 1;
7184 it->current = save_current;
7185 it->position = save_position;
7186 handle_stop (it);
7187 it->stop_charpos = next_stop;
7188 }
7189
7190 /* Load IT with the next display element from current_buffer. Value
7191 is zero if end of buffer reached. IT->stop_charpos is the next
7192 position at which to stop and check for text properties or buffer
7193 end. */
7194
7195 static int
7196 next_element_from_buffer (struct it *it)
7197 {
7198 int success_p = 1;
7199
7200 xassert (IT_CHARPOS (*it) >= BEGV);
7201 xassert (NILP (it->string) && !it->s);
7202 xassert (!it->bidi_p
7203 || (it->bidi_it.string.lstring == Qnil
7204 && it->bidi_it.string.s == NULL));
7205
7206 /* With bidi reordering, the character to display might not be the
7207 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7208 we were reseat()ed to a new buffer position, which is potentially
7209 a different paragraph. */
7210 if (it->bidi_p && it->bidi_it.first_elt)
7211 {
7212 get_visually_first_element (it);
7213 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7214 }
7215
7216 if (IT_CHARPOS (*it) >= it->stop_charpos)
7217 {
7218 if (IT_CHARPOS (*it) >= it->end_charpos)
7219 {
7220 int overlay_strings_follow_p;
7221
7222 /* End of the game, except when overlay strings follow that
7223 haven't been returned yet. */
7224 if (it->overlay_strings_at_end_processed_p)
7225 overlay_strings_follow_p = 0;
7226 else
7227 {
7228 it->overlay_strings_at_end_processed_p = 1;
7229 overlay_strings_follow_p = get_overlay_strings (it, 0);
7230 }
7231
7232 if (overlay_strings_follow_p)
7233 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7234 else
7235 {
7236 it->what = IT_EOB;
7237 it->position = it->current.pos;
7238 success_p = 0;
7239 }
7240 }
7241 else if (!(!it->bidi_p
7242 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7243 || IT_CHARPOS (*it) == it->stop_charpos))
7244 {
7245 /* With bidi non-linear iteration, we could find ourselves
7246 far beyond the last computed stop_charpos, with several
7247 other stop positions in between that we missed. Scan
7248 them all now, in buffer's logical order, until we find
7249 and handle the last stop_charpos that precedes our
7250 current position. */
7251 handle_stop_backwards (it, it->stop_charpos);
7252 return GET_NEXT_DISPLAY_ELEMENT (it);
7253 }
7254 else
7255 {
7256 if (it->bidi_p)
7257 {
7258 /* Take note of the stop position we just moved across,
7259 for when we will move back across it. */
7260 it->prev_stop = it->stop_charpos;
7261 /* If we are at base paragraph embedding level, take
7262 note of the last stop position seen at this
7263 level. */
7264 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7265 it->base_level_stop = it->stop_charpos;
7266 }
7267 handle_stop (it);
7268 return GET_NEXT_DISPLAY_ELEMENT (it);
7269 }
7270 }
7271 else if (it->bidi_p
7272 /* If we are before prev_stop, we may have overstepped on
7273 our way backwards a stop_pos, and if so, we need to
7274 handle that stop_pos. */
7275 && IT_CHARPOS (*it) < it->prev_stop
7276 /* We can sometimes back up for reasons that have nothing
7277 to do with bidi reordering. E.g., compositions. The
7278 code below is only needed when we are above the base
7279 embedding level, so test for that explicitly. */
7280 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7281 {
7282 /* If we lost track of base_level_stop, we have no better place
7283 for handle_stop_backwards to start from than BEGV. This
7284 happens, e.g., when we were reseated to the previous
7285 screenful of text by vertical-motion. */
7286 if (it->base_level_stop <= 0
7287 || IT_CHARPOS (*it) < it->base_level_stop)
7288 it->base_level_stop = BEGV;
7289 handle_stop_backwards (it, it->base_level_stop);
7290 return GET_NEXT_DISPLAY_ELEMENT (it);
7291 }
7292 else
7293 {
7294 /* No face changes, overlays etc. in sight, so just return a
7295 character from current_buffer. */
7296 unsigned char *p;
7297 EMACS_INT stop;
7298
7299 /* Maybe run the redisplay end trigger hook. Performance note:
7300 This doesn't seem to cost measurable time. */
7301 if (it->redisplay_end_trigger_charpos
7302 && it->glyph_row
7303 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7304 run_redisplay_end_trigger_hook (it);
7305
7306 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7307 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7308 stop)
7309 && next_element_from_composition (it))
7310 {
7311 return 1;
7312 }
7313
7314 /* Get the next character, maybe multibyte. */
7315 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7316 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7317 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7318 else
7319 it->c = *p, it->len = 1;
7320
7321 /* Record what we have and where it came from. */
7322 it->what = IT_CHARACTER;
7323 it->object = it->w->buffer;
7324 it->position = it->current.pos;
7325
7326 /* Normally we return the character found above, except when we
7327 really want to return an ellipsis for selective display. */
7328 if (it->selective)
7329 {
7330 if (it->c == '\n')
7331 {
7332 /* A value of selective > 0 means hide lines indented more
7333 than that number of columns. */
7334 if (it->selective > 0
7335 && IT_CHARPOS (*it) + 1 < ZV
7336 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7337 IT_BYTEPOS (*it) + 1,
7338 (double) it->selective)) /* iftc */
7339 {
7340 success_p = next_element_from_ellipsis (it);
7341 it->dpvec_char_len = -1;
7342 }
7343 }
7344 else if (it->c == '\r' && it->selective == -1)
7345 {
7346 /* A value of selective == -1 means that everything from the
7347 CR to the end of the line is invisible, with maybe an
7348 ellipsis displayed for it. */
7349 success_p = next_element_from_ellipsis (it);
7350 it->dpvec_char_len = -1;
7351 }
7352 }
7353 }
7354
7355 /* Value is zero if end of buffer reached. */
7356 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7357 return success_p;
7358 }
7359
7360
7361 /* Run the redisplay end trigger hook for IT. */
7362
7363 static void
7364 run_redisplay_end_trigger_hook (struct it *it)
7365 {
7366 Lisp_Object args[3];
7367
7368 /* IT->glyph_row should be non-null, i.e. we should be actually
7369 displaying something, or otherwise we should not run the hook. */
7370 xassert (it->glyph_row);
7371
7372 /* Set up hook arguments. */
7373 args[0] = Qredisplay_end_trigger_functions;
7374 args[1] = it->window;
7375 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7376 it->redisplay_end_trigger_charpos = 0;
7377
7378 /* Since we are *trying* to run these functions, don't try to run
7379 them again, even if they get an error. */
7380 it->w->redisplay_end_trigger = Qnil;
7381 Frun_hook_with_args (3, args);
7382
7383 /* Notice if it changed the face of the character we are on. */
7384 handle_face_prop (it);
7385 }
7386
7387
7388 /* Deliver a composition display element. Unlike the other
7389 next_element_from_XXX, this function is not registered in the array
7390 get_next_element[]. It is called from next_element_from_buffer and
7391 next_element_from_string when necessary. */
7392
7393 static int
7394 next_element_from_composition (struct it *it)
7395 {
7396 it->what = IT_COMPOSITION;
7397 it->len = it->cmp_it.nbytes;
7398 if (STRINGP (it->string))
7399 {
7400 if (it->c < 0)
7401 {
7402 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7403 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7404 return 0;
7405 }
7406 it->position = it->current.string_pos;
7407 it->object = it->string;
7408 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7409 IT_STRING_BYTEPOS (*it), it->string);
7410 }
7411 else
7412 {
7413 if (it->c < 0)
7414 {
7415 IT_CHARPOS (*it) += it->cmp_it.nchars;
7416 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7417 if (it->bidi_p)
7418 {
7419 if (it->bidi_it.new_paragraph)
7420 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7421 /* Resync the bidi iterator with IT's new position.
7422 FIXME: this doesn't support bidirectional text. */
7423 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7424 bidi_move_to_visually_next (&it->bidi_it);
7425 }
7426 return 0;
7427 }
7428 it->position = it->current.pos;
7429 it->object = it->w->buffer;
7430 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7431 IT_BYTEPOS (*it), Qnil);
7432 }
7433 return 1;
7434 }
7435
7436
7437 \f
7438 /***********************************************************************
7439 Moving an iterator without producing glyphs
7440 ***********************************************************************/
7441
7442 /* Check if iterator is at a position corresponding to a valid buffer
7443 position after some move_it_ call. */
7444
7445 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7446 ((it)->method == GET_FROM_STRING \
7447 ? IT_STRING_CHARPOS (*it) == 0 \
7448 : 1)
7449
7450
7451 /* Move iterator IT to a specified buffer or X position within one
7452 line on the display without producing glyphs.
7453
7454 OP should be a bit mask including some or all of these bits:
7455 MOVE_TO_X: Stop upon reaching x-position TO_X.
7456 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7457 Regardless of OP's value, stop upon reaching the end of the display line.
7458
7459 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7460 This means, in particular, that TO_X includes window's horizontal
7461 scroll amount.
7462
7463 The return value has several possible values that
7464 say what condition caused the scan to stop:
7465
7466 MOVE_POS_MATCH_OR_ZV
7467 - when TO_POS or ZV was reached.
7468
7469 MOVE_X_REACHED
7470 -when TO_X was reached before TO_POS or ZV were reached.
7471
7472 MOVE_LINE_CONTINUED
7473 - when we reached the end of the display area and the line must
7474 be continued.
7475
7476 MOVE_LINE_TRUNCATED
7477 - when we reached the end of the display area and the line is
7478 truncated.
7479
7480 MOVE_NEWLINE_OR_CR
7481 - when we stopped at a line end, i.e. a newline or a CR and selective
7482 display is on. */
7483
7484 static enum move_it_result
7485 move_it_in_display_line_to (struct it *it,
7486 EMACS_INT to_charpos, int to_x,
7487 enum move_operation_enum op)
7488 {
7489 enum move_it_result result = MOVE_UNDEFINED;
7490 struct glyph_row *saved_glyph_row;
7491 struct it wrap_it, atpos_it, atx_it;
7492 int may_wrap = 0;
7493 enum it_method prev_method = it->method;
7494 EMACS_INT prev_pos = IT_CHARPOS (*it);
7495
7496 /* Don't produce glyphs in produce_glyphs. */
7497 saved_glyph_row = it->glyph_row;
7498 it->glyph_row = NULL;
7499
7500 /* Use wrap_it to save a copy of IT wherever a word wrap could
7501 occur. Use atpos_it to save a copy of IT at the desired buffer
7502 position, if found, so that we can scan ahead and check if the
7503 word later overshoots the window edge. Use atx_it similarly, for
7504 pixel positions. */
7505 wrap_it.sp = -1;
7506 atpos_it.sp = -1;
7507 atx_it.sp = -1;
7508
7509 #define BUFFER_POS_REACHED_P() \
7510 ((op & MOVE_TO_POS) != 0 \
7511 && BUFFERP (it->object) \
7512 && (IT_CHARPOS (*it) == to_charpos \
7513 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7514 && (it->method == GET_FROM_BUFFER \
7515 || (it->method == GET_FROM_DISPLAY_VECTOR \
7516 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7517
7518 /* If there's a line-/wrap-prefix, handle it. */
7519 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7520 && it->current_y < it->last_visible_y)
7521 handle_line_prefix (it);
7522
7523 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7524 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7525
7526 while (1)
7527 {
7528 int x, i, ascent = 0, descent = 0;
7529
7530 /* Utility macro to reset an iterator with x, ascent, and descent. */
7531 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7532 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7533 (IT)->max_descent = descent)
7534
7535 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7536 glyph). */
7537 if ((op & MOVE_TO_POS) != 0
7538 && BUFFERP (it->object)
7539 && it->method == GET_FROM_BUFFER
7540 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7541 || (it->bidi_p
7542 && (prev_method == GET_FROM_IMAGE
7543 || prev_method == GET_FROM_STRETCH)
7544 /* Passed TO_CHARPOS from left to right. */
7545 && ((prev_pos < to_charpos
7546 && IT_CHARPOS (*it) > to_charpos)
7547 /* Passed TO_CHARPOS from right to left. */
7548 || (prev_pos > to_charpos
7549 && IT_CHARPOS (*it) < to_charpos)))))
7550 {
7551 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7552 {
7553 result = MOVE_POS_MATCH_OR_ZV;
7554 break;
7555 }
7556 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7557 /* If wrap_it is valid, the current position might be in a
7558 word that is wrapped. So, save the iterator in
7559 atpos_it and continue to see if wrapping happens. */
7560 atpos_it = *it;
7561 }
7562
7563 prev_method = it->method;
7564 if (it->method == GET_FROM_BUFFER)
7565 prev_pos = IT_CHARPOS (*it);
7566 /* Stop when ZV reached.
7567 We used to stop here when TO_CHARPOS reached as well, but that is
7568 too soon if this glyph does not fit on this line. So we handle it
7569 explicitly below. */
7570 if (!get_next_display_element (it))
7571 {
7572 result = MOVE_POS_MATCH_OR_ZV;
7573 break;
7574 }
7575
7576 if (it->line_wrap == TRUNCATE)
7577 {
7578 if (BUFFER_POS_REACHED_P ())
7579 {
7580 result = MOVE_POS_MATCH_OR_ZV;
7581 break;
7582 }
7583 }
7584 else
7585 {
7586 if (it->line_wrap == WORD_WRAP)
7587 {
7588 if (IT_DISPLAYING_WHITESPACE (it))
7589 may_wrap = 1;
7590 else if (may_wrap)
7591 {
7592 /* We have reached a glyph that follows one or more
7593 whitespace characters. If the position is
7594 already found, we are done. */
7595 if (atpos_it.sp >= 0)
7596 {
7597 *it = atpos_it;
7598 result = MOVE_POS_MATCH_OR_ZV;
7599 goto done;
7600 }
7601 if (atx_it.sp >= 0)
7602 {
7603 *it = atx_it;
7604 result = MOVE_X_REACHED;
7605 goto done;
7606 }
7607 /* Otherwise, we can wrap here. */
7608 wrap_it = *it;
7609 may_wrap = 0;
7610 }
7611 }
7612 }
7613
7614 /* Remember the line height for the current line, in case
7615 the next element doesn't fit on the line. */
7616 ascent = it->max_ascent;
7617 descent = it->max_descent;
7618
7619 /* The call to produce_glyphs will get the metrics of the
7620 display element IT is loaded with. Record the x-position
7621 before this display element, in case it doesn't fit on the
7622 line. */
7623 x = it->current_x;
7624
7625 PRODUCE_GLYPHS (it);
7626
7627 if (it->area != TEXT_AREA)
7628 {
7629 set_iterator_to_next (it, 1);
7630 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7631 SET_TEXT_POS (this_line_min_pos,
7632 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7633 continue;
7634 }
7635
7636 /* The number of glyphs we get back in IT->nglyphs will normally
7637 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7638 character on a terminal frame, or (iii) a line end. For the
7639 second case, IT->nglyphs - 1 padding glyphs will be present.
7640 (On X frames, there is only one glyph produced for a
7641 composite character.)
7642
7643 The behavior implemented below means, for continuation lines,
7644 that as many spaces of a TAB as fit on the current line are
7645 displayed there. For terminal frames, as many glyphs of a
7646 multi-glyph character are displayed in the current line, too.
7647 This is what the old redisplay code did, and we keep it that
7648 way. Under X, the whole shape of a complex character must
7649 fit on the line or it will be completely displayed in the
7650 next line.
7651
7652 Note that both for tabs and padding glyphs, all glyphs have
7653 the same width. */
7654 if (it->nglyphs)
7655 {
7656 /* More than one glyph or glyph doesn't fit on line. All
7657 glyphs have the same width. */
7658 int single_glyph_width = it->pixel_width / it->nglyphs;
7659 int new_x;
7660 int x_before_this_char = x;
7661 int hpos_before_this_char = it->hpos;
7662
7663 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7664 {
7665 new_x = x + single_glyph_width;
7666
7667 /* We want to leave anything reaching TO_X to the caller. */
7668 if ((op & MOVE_TO_X) && new_x > to_x)
7669 {
7670 if (BUFFER_POS_REACHED_P ())
7671 {
7672 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7673 goto buffer_pos_reached;
7674 if (atpos_it.sp < 0)
7675 {
7676 atpos_it = *it;
7677 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7678 }
7679 }
7680 else
7681 {
7682 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7683 {
7684 it->current_x = x;
7685 result = MOVE_X_REACHED;
7686 break;
7687 }
7688 if (atx_it.sp < 0)
7689 {
7690 atx_it = *it;
7691 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7692 }
7693 }
7694 }
7695
7696 if (/* Lines are continued. */
7697 it->line_wrap != TRUNCATE
7698 && (/* And glyph doesn't fit on the line. */
7699 new_x > it->last_visible_x
7700 /* Or it fits exactly and we're on a window
7701 system frame. */
7702 || (new_x == it->last_visible_x
7703 && FRAME_WINDOW_P (it->f))))
7704 {
7705 if (/* IT->hpos == 0 means the very first glyph
7706 doesn't fit on the line, e.g. a wide image. */
7707 it->hpos == 0
7708 || (new_x == it->last_visible_x
7709 && FRAME_WINDOW_P (it->f)))
7710 {
7711 ++it->hpos;
7712 it->current_x = new_x;
7713
7714 /* The character's last glyph just barely fits
7715 in this row. */
7716 if (i == it->nglyphs - 1)
7717 {
7718 /* If this is the destination position,
7719 return a position *before* it in this row,
7720 now that we know it fits in this row. */
7721 if (BUFFER_POS_REACHED_P ())
7722 {
7723 if (it->line_wrap != WORD_WRAP
7724 || wrap_it.sp < 0)
7725 {
7726 it->hpos = hpos_before_this_char;
7727 it->current_x = x_before_this_char;
7728 result = MOVE_POS_MATCH_OR_ZV;
7729 break;
7730 }
7731 if (it->line_wrap == WORD_WRAP
7732 && atpos_it.sp < 0)
7733 {
7734 atpos_it = *it;
7735 atpos_it.current_x = x_before_this_char;
7736 atpos_it.hpos = hpos_before_this_char;
7737 }
7738 }
7739
7740 set_iterator_to_next (it, 1);
7741 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7742 SET_TEXT_POS (this_line_min_pos,
7743 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7744 /* On graphical terminals, newlines may
7745 "overflow" into the fringe if
7746 overflow-newline-into-fringe is non-nil.
7747 On text-only terminals, newlines may
7748 overflow into the last glyph on the
7749 display line.*/
7750 if (!FRAME_WINDOW_P (it->f)
7751 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7752 {
7753 if (!get_next_display_element (it))
7754 {
7755 result = MOVE_POS_MATCH_OR_ZV;
7756 break;
7757 }
7758 if (BUFFER_POS_REACHED_P ())
7759 {
7760 if (ITERATOR_AT_END_OF_LINE_P (it))
7761 result = MOVE_POS_MATCH_OR_ZV;
7762 else
7763 result = MOVE_LINE_CONTINUED;
7764 break;
7765 }
7766 if (ITERATOR_AT_END_OF_LINE_P (it))
7767 {
7768 result = MOVE_NEWLINE_OR_CR;
7769 break;
7770 }
7771 }
7772 }
7773 }
7774 else
7775 IT_RESET_X_ASCENT_DESCENT (it);
7776
7777 if (wrap_it.sp >= 0)
7778 {
7779 *it = wrap_it;
7780 atpos_it.sp = -1;
7781 atx_it.sp = -1;
7782 }
7783
7784 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7785 IT_CHARPOS (*it)));
7786 result = MOVE_LINE_CONTINUED;
7787 break;
7788 }
7789
7790 if (BUFFER_POS_REACHED_P ())
7791 {
7792 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7793 goto buffer_pos_reached;
7794 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7795 {
7796 atpos_it = *it;
7797 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7798 }
7799 }
7800
7801 if (new_x > it->first_visible_x)
7802 {
7803 /* Glyph is visible. Increment number of glyphs that
7804 would be displayed. */
7805 ++it->hpos;
7806 }
7807 }
7808
7809 if (result != MOVE_UNDEFINED)
7810 break;
7811 }
7812 else if (BUFFER_POS_REACHED_P ())
7813 {
7814 buffer_pos_reached:
7815 IT_RESET_X_ASCENT_DESCENT (it);
7816 result = MOVE_POS_MATCH_OR_ZV;
7817 break;
7818 }
7819 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7820 {
7821 /* Stop when TO_X specified and reached. This check is
7822 necessary here because of lines consisting of a line end,
7823 only. The line end will not produce any glyphs and we
7824 would never get MOVE_X_REACHED. */
7825 xassert (it->nglyphs == 0);
7826 result = MOVE_X_REACHED;
7827 break;
7828 }
7829
7830 /* Is this a line end? If yes, we're done. */
7831 if (ITERATOR_AT_END_OF_LINE_P (it))
7832 {
7833 result = MOVE_NEWLINE_OR_CR;
7834 break;
7835 }
7836
7837 if (it->method == GET_FROM_BUFFER)
7838 prev_pos = IT_CHARPOS (*it);
7839 /* The current display element has been consumed. Advance
7840 to the next. */
7841 set_iterator_to_next (it, 1);
7842 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7843 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7844
7845 /* Stop if lines are truncated and IT's current x-position is
7846 past the right edge of the window now. */
7847 if (it->line_wrap == TRUNCATE
7848 && it->current_x >= it->last_visible_x)
7849 {
7850 if (!FRAME_WINDOW_P (it->f)
7851 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7852 {
7853 if (!get_next_display_element (it)
7854 || BUFFER_POS_REACHED_P ())
7855 {
7856 result = MOVE_POS_MATCH_OR_ZV;
7857 break;
7858 }
7859 if (ITERATOR_AT_END_OF_LINE_P (it))
7860 {
7861 result = MOVE_NEWLINE_OR_CR;
7862 break;
7863 }
7864 }
7865 result = MOVE_LINE_TRUNCATED;
7866 break;
7867 }
7868 #undef IT_RESET_X_ASCENT_DESCENT
7869 }
7870
7871 #undef BUFFER_POS_REACHED_P
7872
7873 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7874 restore the saved iterator. */
7875 if (atpos_it.sp >= 0)
7876 *it = atpos_it;
7877 else if (atx_it.sp >= 0)
7878 *it = atx_it;
7879
7880 done:
7881
7882 /* Restore the iterator settings altered at the beginning of this
7883 function. */
7884 it->glyph_row = saved_glyph_row;
7885 return result;
7886 }
7887
7888 /* For external use. */
7889 void
7890 move_it_in_display_line (struct it *it,
7891 EMACS_INT to_charpos, int to_x,
7892 enum move_operation_enum op)
7893 {
7894 if (it->line_wrap == WORD_WRAP
7895 && (op & MOVE_TO_X))
7896 {
7897 struct it save_it = *it;
7898 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7899 /* When word-wrap is on, TO_X may lie past the end
7900 of a wrapped line. Then it->current is the
7901 character on the next line, so backtrack to the
7902 space before the wrap point. */
7903 if (skip == MOVE_LINE_CONTINUED)
7904 {
7905 int prev_x = max (it->current_x - 1, 0);
7906 *it = save_it;
7907 move_it_in_display_line_to
7908 (it, -1, prev_x, MOVE_TO_X);
7909 }
7910 }
7911 else
7912 move_it_in_display_line_to (it, to_charpos, to_x, op);
7913 }
7914
7915
7916 /* Move IT forward until it satisfies one or more of the criteria in
7917 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7918
7919 OP is a bit-mask that specifies where to stop, and in particular,
7920 which of those four position arguments makes a difference. See the
7921 description of enum move_operation_enum.
7922
7923 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7924 screen line, this function will set IT to the next position >
7925 TO_CHARPOS. */
7926
7927 void
7928 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7929 {
7930 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7931 int line_height, line_start_x = 0, reached = 0;
7932
7933 for (;;)
7934 {
7935 if (op & MOVE_TO_VPOS)
7936 {
7937 /* If no TO_CHARPOS and no TO_X specified, stop at the
7938 start of the line TO_VPOS. */
7939 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7940 {
7941 if (it->vpos == to_vpos)
7942 {
7943 reached = 1;
7944 break;
7945 }
7946 else
7947 skip = move_it_in_display_line_to (it, -1, -1, 0);
7948 }
7949 else
7950 {
7951 /* TO_VPOS >= 0 means stop at TO_X in the line at
7952 TO_VPOS, or at TO_POS, whichever comes first. */
7953 if (it->vpos == to_vpos)
7954 {
7955 reached = 2;
7956 break;
7957 }
7958
7959 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7960
7961 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7962 {
7963 reached = 3;
7964 break;
7965 }
7966 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7967 {
7968 /* We have reached TO_X but not in the line we want. */
7969 skip = move_it_in_display_line_to (it, to_charpos,
7970 -1, MOVE_TO_POS);
7971 if (skip == MOVE_POS_MATCH_OR_ZV)
7972 {
7973 reached = 4;
7974 break;
7975 }
7976 }
7977 }
7978 }
7979 else if (op & MOVE_TO_Y)
7980 {
7981 struct it it_backup;
7982
7983 if (it->line_wrap == WORD_WRAP)
7984 it_backup = *it;
7985
7986 /* TO_Y specified means stop at TO_X in the line containing
7987 TO_Y---or at TO_CHARPOS if this is reached first. The
7988 problem is that we can't really tell whether the line
7989 contains TO_Y before we have completely scanned it, and
7990 this may skip past TO_X. What we do is to first scan to
7991 TO_X.
7992
7993 If TO_X is not specified, use a TO_X of zero. The reason
7994 is to make the outcome of this function more predictable.
7995 If we didn't use TO_X == 0, we would stop at the end of
7996 the line which is probably not what a caller would expect
7997 to happen. */
7998 skip = move_it_in_display_line_to
7999 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8000 (MOVE_TO_X | (op & MOVE_TO_POS)));
8001
8002 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8003 if (skip == MOVE_POS_MATCH_OR_ZV)
8004 reached = 5;
8005 else if (skip == MOVE_X_REACHED)
8006 {
8007 /* If TO_X was reached, we want to know whether TO_Y is
8008 in the line. We know this is the case if the already
8009 scanned glyphs make the line tall enough. Otherwise,
8010 we must check by scanning the rest of the line. */
8011 line_height = it->max_ascent + it->max_descent;
8012 if (to_y >= it->current_y
8013 && to_y < it->current_y + line_height)
8014 {
8015 reached = 6;
8016 break;
8017 }
8018 it_backup = *it;
8019 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8020 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8021 op & MOVE_TO_POS);
8022 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8023 line_height = it->max_ascent + it->max_descent;
8024 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8025
8026 if (to_y >= it->current_y
8027 && to_y < it->current_y + line_height)
8028 {
8029 /* If TO_Y is in this line and TO_X was reached
8030 above, we scanned too far. We have to restore
8031 IT's settings to the ones before skipping. */
8032 *it = it_backup;
8033 reached = 6;
8034 }
8035 else
8036 {
8037 skip = skip2;
8038 if (skip == MOVE_POS_MATCH_OR_ZV)
8039 reached = 7;
8040 }
8041 }
8042 else
8043 {
8044 /* Check whether TO_Y is in this line. */
8045 line_height = it->max_ascent + it->max_descent;
8046 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8047
8048 if (to_y >= it->current_y
8049 && to_y < it->current_y + line_height)
8050 {
8051 /* When word-wrap is on, TO_X may lie past the end
8052 of a wrapped line. Then it->current is the
8053 character on the next line, so backtrack to the
8054 space before the wrap point. */
8055 if (skip == MOVE_LINE_CONTINUED
8056 && it->line_wrap == WORD_WRAP)
8057 {
8058 int prev_x = max (it->current_x - 1, 0);
8059 *it = it_backup;
8060 skip = move_it_in_display_line_to
8061 (it, -1, prev_x, MOVE_TO_X);
8062 }
8063 reached = 6;
8064 }
8065 }
8066
8067 if (reached)
8068 break;
8069 }
8070 else if (BUFFERP (it->object)
8071 && (it->method == GET_FROM_BUFFER
8072 || it->method == GET_FROM_STRETCH)
8073 && IT_CHARPOS (*it) >= to_charpos)
8074 skip = MOVE_POS_MATCH_OR_ZV;
8075 else
8076 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8077
8078 switch (skip)
8079 {
8080 case MOVE_POS_MATCH_OR_ZV:
8081 reached = 8;
8082 goto out;
8083
8084 case MOVE_NEWLINE_OR_CR:
8085 set_iterator_to_next (it, 1);
8086 it->continuation_lines_width = 0;
8087 break;
8088
8089 case MOVE_LINE_TRUNCATED:
8090 it->continuation_lines_width = 0;
8091 reseat_at_next_visible_line_start (it, 0);
8092 if ((op & MOVE_TO_POS) != 0
8093 && IT_CHARPOS (*it) > to_charpos)
8094 {
8095 reached = 9;
8096 goto out;
8097 }
8098 break;
8099
8100 case MOVE_LINE_CONTINUED:
8101 /* For continued lines ending in a tab, some of the glyphs
8102 associated with the tab are displayed on the current
8103 line. Since it->current_x does not include these glyphs,
8104 we use it->last_visible_x instead. */
8105 if (it->c == '\t')
8106 {
8107 it->continuation_lines_width += it->last_visible_x;
8108 /* When moving by vpos, ensure that the iterator really
8109 advances to the next line (bug#847, bug#969). Fixme:
8110 do we need to do this in other circumstances? */
8111 if (it->current_x != it->last_visible_x
8112 && (op & MOVE_TO_VPOS)
8113 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8114 {
8115 line_start_x = it->current_x + it->pixel_width
8116 - it->last_visible_x;
8117 set_iterator_to_next (it, 0);
8118 }
8119 }
8120 else
8121 it->continuation_lines_width += it->current_x;
8122 break;
8123
8124 default:
8125 abort ();
8126 }
8127
8128 /* Reset/increment for the next run. */
8129 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8130 it->current_x = line_start_x;
8131 line_start_x = 0;
8132 it->hpos = 0;
8133 it->current_y += it->max_ascent + it->max_descent;
8134 ++it->vpos;
8135 last_height = it->max_ascent + it->max_descent;
8136 last_max_ascent = it->max_ascent;
8137 it->max_ascent = it->max_descent = 0;
8138 }
8139
8140 out:
8141
8142 /* On text terminals, we may stop at the end of a line in the middle
8143 of a multi-character glyph. If the glyph itself is continued,
8144 i.e. it is actually displayed on the next line, don't treat this
8145 stopping point as valid; move to the next line instead (unless
8146 that brings us offscreen). */
8147 if (!FRAME_WINDOW_P (it->f)
8148 && op & MOVE_TO_POS
8149 && IT_CHARPOS (*it) == to_charpos
8150 && it->what == IT_CHARACTER
8151 && it->nglyphs > 1
8152 && it->line_wrap == WINDOW_WRAP
8153 && it->current_x == it->last_visible_x - 1
8154 && it->c != '\n'
8155 && it->c != '\t'
8156 && it->vpos < XFASTINT (it->w->window_end_vpos))
8157 {
8158 it->continuation_lines_width += it->current_x;
8159 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8160 it->current_y += it->max_ascent + it->max_descent;
8161 ++it->vpos;
8162 last_height = it->max_ascent + it->max_descent;
8163 last_max_ascent = it->max_ascent;
8164 }
8165
8166 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8167 }
8168
8169
8170 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8171
8172 If DY > 0, move IT backward at least that many pixels. DY = 0
8173 means move IT backward to the preceding line start or BEGV. This
8174 function may move over more than DY pixels if IT->current_y - DY
8175 ends up in the middle of a line; in this case IT->current_y will be
8176 set to the top of the line moved to. */
8177
8178 void
8179 move_it_vertically_backward (struct it *it, int dy)
8180 {
8181 int nlines, h;
8182 struct it it2, it3;
8183 EMACS_INT start_pos;
8184
8185 move_further_back:
8186 xassert (dy >= 0);
8187
8188 start_pos = IT_CHARPOS (*it);
8189
8190 /* Estimate how many newlines we must move back. */
8191 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8192
8193 /* Set the iterator's position that many lines back. */
8194 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8195 back_to_previous_visible_line_start (it);
8196
8197 /* Reseat the iterator here. When moving backward, we don't want
8198 reseat to skip forward over invisible text, set up the iterator
8199 to deliver from overlay strings at the new position etc. So,
8200 use reseat_1 here. */
8201 reseat_1 (it, it->current.pos, 1);
8202
8203 /* We are now surely at a line start. */
8204 it->current_x = it->hpos = 0;
8205 it->continuation_lines_width = 0;
8206
8207 /* Move forward and see what y-distance we moved. First move to the
8208 start of the next line so that we get its height. We need this
8209 height to be able to tell whether we reached the specified
8210 y-distance. */
8211 it2 = *it;
8212 it2.max_ascent = it2.max_descent = 0;
8213 do
8214 {
8215 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8216 MOVE_TO_POS | MOVE_TO_VPOS);
8217 }
8218 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8219 xassert (IT_CHARPOS (*it) >= BEGV);
8220 it3 = it2;
8221
8222 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8223 xassert (IT_CHARPOS (*it) >= BEGV);
8224 /* H is the actual vertical distance from the position in *IT
8225 and the starting position. */
8226 h = it2.current_y - it->current_y;
8227 /* NLINES is the distance in number of lines. */
8228 nlines = it2.vpos - it->vpos;
8229
8230 /* Correct IT's y and vpos position
8231 so that they are relative to the starting point. */
8232 it->vpos -= nlines;
8233 it->current_y -= h;
8234
8235 if (dy == 0)
8236 {
8237 /* DY == 0 means move to the start of the screen line. The
8238 value of nlines is > 0 if continuation lines were involved. */
8239 if (nlines > 0)
8240 move_it_by_lines (it, nlines);
8241 }
8242 else
8243 {
8244 /* The y-position we try to reach, relative to *IT.
8245 Note that H has been subtracted in front of the if-statement. */
8246 int target_y = it->current_y + h - dy;
8247 int y0 = it3.current_y;
8248 int y1 = line_bottom_y (&it3);
8249 int line_height = y1 - y0;
8250
8251 /* If we did not reach target_y, try to move further backward if
8252 we can. If we moved too far backward, try to move forward. */
8253 if (target_y < it->current_y
8254 /* This is heuristic. In a window that's 3 lines high, with
8255 a line height of 13 pixels each, recentering with point
8256 on the bottom line will try to move -39/2 = 19 pixels
8257 backward. Try to avoid moving into the first line. */
8258 && (it->current_y - target_y
8259 > min (window_box_height (it->w), line_height * 2 / 3))
8260 && IT_CHARPOS (*it) > BEGV)
8261 {
8262 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8263 target_y - it->current_y));
8264 dy = it->current_y - target_y;
8265 goto move_further_back;
8266 }
8267 else if (target_y >= it->current_y + line_height
8268 && IT_CHARPOS (*it) < ZV)
8269 {
8270 /* Should move forward by at least one line, maybe more.
8271
8272 Note: Calling move_it_by_lines can be expensive on
8273 terminal frames, where compute_motion is used (via
8274 vmotion) to do the job, when there are very long lines
8275 and truncate-lines is nil. That's the reason for
8276 treating terminal frames specially here. */
8277
8278 if (!FRAME_WINDOW_P (it->f))
8279 move_it_vertically (it, target_y - (it->current_y + line_height));
8280 else
8281 {
8282 do
8283 {
8284 move_it_by_lines (it, 1);
8285 }
8286 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8287 }
8288 }
8289 }
8290 }
8291
8292
8293 /* Move IT by a specified amount of pixel lines DY. DY negative means
8294 move backwards. DY = 0 means move to start of screen line. At the
8295 end, IT will be on the start of a screen line. */
8296
8297 void
8298 move_it_vertically (struct it *it, int dy)
8299 {
8300 if (dy <= 0)
8301 move_it_vertically_backward (it, -dy);
8302 else
8303 {
8304 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8305 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8306 MOVE_TO_POS | MOVE_TO_Y);
8307 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8308
8309 /* If buffer ends in ZV without a newline, move to the start of
8310 the line to satisfy the post-condition. */
8311 if (IT_CHARPOS (*it) == ZV
8312 && ZV > BEGV
8313 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8314 move_it_by_lines (it, 0);
8315 }
8316 }
8317
8318
8319 /* Move iterator IT past the end of the text line it is in. */
8320
8321 void
8322 move_it_past_eol (struct it *it)
8323 {
8324 enum move_it_result rc;
8325
8326 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8327 if (rc == MOVE_NEWLINE_OR_CR)
8328 set_iterator_to_next (it, 0);
8329 }
8330
8331
8332 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8333 negative means move up. DVPOS == 0 means move to the start of the
8334 screen line.
8335
8336 Optimization idea: If we would know that IT->f doesn't use
8337 a face with proportional font, we could be faster for
8338 truncate-lines nil. */
8339
8340 void
8341 move_it_by_lines (struct it *it, int dvpos)
8342 {
8343
8344 /* The commented-out optimization uses vmotion on terminals. This
8345 gives bad results, because elements like it->what, on which
8346 callers such as pos_visible_p rely, aren't updated. */
8347 /* struct position pos;
8348 if (!FRAME_WINDOW_P (it->f))
8349 {
8350 struct text_pos textpos;
8351
8352 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8353 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8354 reseat (it, textpos, 1);
8355 it->vpos += pos.vpos;
8356 it->current_y += pos.vpos;
8357 }
8358 else */
8359
8360 if (dvpos == 0)
8361 {
8362 /* DVPOS == 0 means move to the start of the screen line. */
8363 move_it_vertically_backward (it, 0);
8364 xassert (it->current_x == 0 && it->hpos == 0);
8365 /* Let next call to line_bottom_y calculate real line height */
8366 last_height = 0;
8367 }
8368 else if (dvpos > 0)
8369 {
8370 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8371 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8372 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8373 }
8374 else
8375 {
8376 struct it it2;
8377 EMACS_INT start_charpos, i;
8378
8379 /* Start at the beginning of the screen line containing IT's
8380 position. This may actually move vertically backwards,
8381 in case of overlays, so adjust dvpos accordingly. */
8382 dvpos += it->vpos;
8383 move_it_vertically_backward (it, 0);
8384 dvpos -= it->vpos;
8385
8386 /* Go back -DVPOS visible lines and reseat the iterator there. */
8387 start_charpos = IT_CHARPOS (*it);
8388 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8389 back_to_previous_visible_line_start (it);
8390 reseat (it, it->current.pos, 1);
8391
8392 /* Move further back if we end up in a string or an image. */
8393 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8394 {
8395 /* First try to move to start of display line. */
8396 dvpos += it->vpos;
8397 move_it_vertically_backward (it, 0);
8398 dvpos -= it->vpos;
8399 if (IT_POS_VALID_AFTER_MOVE_P (it))
8400 break;
8401 /* If start of line is still in string or image,
8402 move further back. */
8403 back_to_previous_visible_line_start (it);
8404 reseat (it, it->current.pos, 1);
8405 dvpos--;
8406 }
8407
8408 it->current_x = it->hpos = 0;
8409
8410 /* Above call may have moved too far if continuation lines
8411 are involved. Scan forward and see if it did. */
8412 it2 = *it;
8413 it2.vpos = it2.current_y = 0;
8414 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8415 it->vpos -= it2.vpos;
8416 it->current_y -= it2.current_y;
8417 it->current_x = it->hpos = 0;
8418
8419 /* If we moved too far back, move IT some lines forward. */
8420 if (it2.vpos > -dvpos)
8421 {
8422 int delta = it2.vpos + dvpos;
8423 it2 = *it;
8424 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8425 /* Move back again if we got too far ahead. */
8426 if (IT_CHARPOS (*it) >= start_charpos)
8427 *it = it2;
8428 }
8429 }
8430 }
8431
8432 /* Return 1 if IT points into the middle of a display vector. */
8433
8434 int
8435 in_display_vector_p (struct it *it)
8436 {
8437 return (it->method == GET_FROM_DISPLAY_VECTOR
8438 && it->current.dpvec_index > 0
8439 && it->dpvec + it->current.dpvec_index != it->dpend);
8440 }
8441
8442 \f
8443 /***********************************************************************
8444 Messages
8445 ***********************************************************************/
8446
8447
8448 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8449 to *Messages*. */
8450
8451 void
8452 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8453 {
8454 Lisp_Object args[3];
8455 Lisp_Object msg, fmt;
8456 char *buffer;
8457 EMACS_INT len;
8458 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8459 USE_SAFE_ALLOCA;
8460
8461 /* Do nothing if called asynchronously. Inserting text into
8462 a buffer may call after-change-functions and alike and
8463 that would means running Lisp asynchronously. */
8464 if (handling_signal)
8465 return;
8466
8467 fmt = msg = Qnil;
8468 GCPRO4 (fmt, msg, arg1, arg2);
8469
8470 args[0] = fmt = build_string (format);
8471 args[1] = arg1;
8472 args[2] = arg2;
8473 msg = Fformat (3, args);
8474
8475 len = SBYTES (msg) + 1;
8476 SAFE_ALLOCA (buffer, char *, len);
8477 memcpy (buffer, SDATA (msg), len);
8478
8479 message_dolog (buffer, len - 1, 1, 0);
8480 SAFE_FREE ();
8481
8482 UNGCPRO;
8483 }
8484
8485
8486 /* Output a newline in the *Messages* buffer if "needs" one. */
8487
8488 void
8489 message_log_maybe_newline (void)
8490 {
8491 if (message_log_need_newline)
8492 message_dolog ("", 0, 1, 0);
8493 }
8494
8495
8496 /* Add a string M of length NBYTES to the message log, optionally
8497 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8498 nonzero, means interpret the contents of M as multibyte. This
8499 function calls low-level routines in order to bypass text property
8500 hooks, etc. which might not be safe to run.
8501
8502 This may GC (insert may run before/after change hooks),
8503 so the buffer M must NOT point to a Lisp string. */
8504
8505 void
8506 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8507 {
8508 const unsigned char *msg = (const unsigned char *) m;
8509
8510 if (!NILP (Vmemory_full))
8511 return;
8512
8513 if (!NILP (Vmessage_log_max))
8514 {
8515 struct buffer *oldbuf;
8516 Lisp_Object oldpoint, oldbegv, oldzv;
8517 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8518 EMACS_INT point_at_end = 0;
8519 EMACS_INT zv_at_end = 0;
8520 Lisp_Object old_deactivate_mark, tem;
8521 struct gcpro gcpro1;
8522
8523 old_deactivate_mark = Vdeactivate_mark;
8524 oldbuf = current_buffer;
8525 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8526 BVAR (current_buffer, undo_list) = Qt;
8527
8528 oldpoint = message_dolog_marker1;
8529 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8530 oldbegv = message_dolog_marker2;
8531 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8532 oldzv = message_dolog_marker3;
8533 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8534 GCPRO1 (old_deactivate_mark);
8535
8536 if (PT == Z)
8537 point_at_end = 1;
8538 if (ZV == Z)
8539 zv_at_end = 1;
8540
8541 BEGV = BEG;
8542 BEGV_BYTE = BEG_BYTE;
8543 ZV = Z;
8544 ZV_BYTE = Z_BYTE;
8545 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8546
8547 /* Insert the string--maybe converting multibyte to single byte
8548 or vice versa, so that all the text fits the buffer. */
8549 if (multibyte
8550 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8551 {
8552 EMACS_INT i;
8553 int c, char_bytes;
8554 char work[1];
8555
8556 /* Convert a multibyte string to single-byte
8557 for the *Message* buffer. */
8558 for (i = 0; i < nbytes; i += char_bytes)
8559 {
8560 c = string_char_and_length (msg + i, &char_bytes);
8561 work[0] = (ASCII_CHAR_P (c)
8562 ? c
8563 : multibyte_char_to_unibyte (c));
8564 insert_1_both (work, 1, 1, 1, 0, 0);
8565 }
8566 }
8567 else if (! multibyte
8568 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8569 {
8570 EMACS_INT i;
8571 int c, char_bytes;
8572 unsigned char str[MAX_MULTIBYTE_LENGTH];
8573 /* Convert a single-byte string to multibyte
8574 for the *Message* buffer. */
8575 for (i = 0; i < nbytes; i++)
8576 {
8577 c = msg[i];
8578 MAKE_CHAR_MULTIBYTE (c);
8579 char_bytes = CHAR_STRING (c, str);
8580 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8581 }
8582 }
8583 else if (nbytes)
8584 insert_1 (m, nbytes, 1, 0, 0);
8585
8586 if (nlflag)
8587 {
8588 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8589 unsigned long int dups;
8590 insert_1 ("\n", 1, 1, 0, 0);
8591
8592 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8593 this_bol = PT;
8594 this_bol_byte = PT_BYTE;
8595
8596 /* See if this line duplicates the previous one.
8597 If so, combine duplicates. */
8598 if (this_bol > BEG)
8599 {
8600 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8601 prev_bol = PT;
8602 prev_bol_byte = PT_BYTE;
8603
8604 dups = message_log_check_duplicate (prev_bol_byte,
8605 this_bol_byte);
8606 if (dups)
8607 {
8608 del_range_both (prev_bol, prev_bol_byte,
8609 this_bol, this_bol_byte, 0);
8610 if (dups > 1)
8611 {
8612 char dupstr[40];
8613 int duplen;
8614
8615 /* If you change this format, don't forget to also
8616 change message_log_check_duplicate. */
8617 sprintf (dupstr, " [%lu times]", dups);
8618 duplen = strlen (dupstr);
8619 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8620 insert_1 (dupstr, duplen, 1, 0, 1);
8621 }
8622 }
8623 }
8624
8625 /* If we have more than the desired maximum number of lines
8626 in the *Messages* buffer now, delete the oldest ones.
8627 This is safe because we don't have undo in this buffer. */
8628
8629 if (NATNUMP (Vmessage_log_max))
8630 {
8631 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8632 -XFASTINT (Vmessage_log_max) - 1, 0);
8633 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8634 }
8635 }
8636 BEGV = XMARKER (oldbegv)->charpos;
8637 BEGV_BYTE = marker_byte_position (oldbegv);
8638
8639 if (zv_at_end)
8640 {
8641 ZV = Z;
8642 ZV_BYTE = Z_BYTE;
8643 }
8644 else
8645 {
8646 ZV = XMARKER (oldzv)->charpos;
8647 ZV_BYTE = marker_byte_position (oldzv);
8648 }
8649
8650 if (point_at_end)
8651 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8652 else
8653 /* We can't do Fgoto_char (oldpoint) because it will run some
8654 Lisp code. */
8655 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8656 XMARKER (oldpoint)->bytepos);
8657
8658 UNGCPRO;
8659 unchain_marker (XMARKER (oldpoint));
8660 unchain_marker (XMARKER (oldbegv));
8661 unchain_marker (XMARKER (oldzv));
8662
8663 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8664 set_buffer_internal (oldbuf);
8665 if (NILP (tem))
8666 windows_or_buffers_changed = old_windows_or_buffers_changed;
8667 message_log_need_newline = !nlflag;
8668 Vdeactivate_mark = old_deactivate_mark;
8669 }
8670 }
8671
8672
8673 /* We are at the end of the buffer after just having inserted a newline.
8674 (Note: We depend on the fact we won't be crossing the gap.)
8675 Check to see if the most recent message looks a lot like the previous one.
8676 Return 0 if different, 1 if the new one should just replace it, or a
8677 value N > 1 if we should also append " [N times]". */
8678
8679 static unsigned long int
8680 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8681 {
8682 EMACS_INT i;
8683 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8684 int seen_dots = 0;
8685 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8686 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8687
8688 for (i = 0; i < len; i++)
8689 {
8690 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8691 seen_dots = 1;
8692 if (p1[i] != p2[i])
8693 return seen_dots;
8694 }
8695 p1 += len;
8696 if (*p1 == '\n')
8697 return 2;
8698 if (*p1++ == ' ' && *p1++ == '[')
8699 {
8700 char *pend;
8701 unsigned long int n = strtoul ((char *) p1, &pend, 10);
8702 if (strncmp (pend, " times]\n", 8) == 0)
8703 return n+1;
8704 }
8705 return 0;
8706 }
8707 \f
8708
8709 /* Display an echo area message M with a specified length of NBYTES
8710 bytes. The string may include null characters. If M is 0, clear
8711 out any existing message, and let the mini-buffer text show
8712 through.
8713
8714 This may GC, so the buffer M must NOT point to a Lisp string. */
8715
8716 void
8717 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8718 {
8719 /* First flush out any partial line written with print. */
8720 message_log_maybe_newline ();
8721 if (m)
8722 message_dolog (m, nbytes, 1, multibyte);
8723 message2_nolog (m, nbytes, multibyte);
8724 }
8725
8726
8727 /* The non-logging counterpart of message2. */
8728
8729 void
8730 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8731 {
8732 struct frame *sf = SELECTED_FRAME ();
8733 message_enable_multibyte = multibyte;
8734
8735 if (FRAME_INITIAL_P (sf))
8736 {
8737 if (noninteractive_need_newline)
8738 putc ('\n', stderr);
8739 noninteractive_need_newline = 0;
8740 if (m)
8741 fwrite (m, nbytes, 1, stderr);
8742 if (cursor_in_echo_area == 0)
8743 fprintf (stderr, "\n");
8744 fflush (stderr);
8745 }
8746 /* A null message buffer means that the frame hasn't really been
8747 initialized yet. Error messages get reported properly by
8748 cmd_error, so this must be just an informative message; toss it. */
8749 else if (INTERACTIVE
8750 && sf->glyphs_initialized_p
8751 && FRAME_MESSAGE_BUF (sf))
8752 {
8753 Lisp_Object mini_window;
8754 struct frame *f;
8755
8756 /* Get the frame containing the mini-buffer
8757 that the selected frame is using. */
8758 mini_window = FRAME_MINIBUF_WINDOW (sf);
8759 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8760
8761 FRAME_SAMPLE_VISIBILITY (f);
8762 if (FRAME_VISIBLE_P (sf)
8763 && ! FRAME_VISIBLE_P (f))
8764 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8765
8766 if (m)
8767 {
8768 set_message (m, Qnil, nbytes, multibyte);
8769 if (minibuffer_auto_raise)
8770 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8771 }
8772 else
8773 clear_message (1, 1);
8774
8775 do_pending_window_change (0);
8776 echo_area_display (1);
8777 do_pending_window_change (0);
8778 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8779 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8780 }
8781 }
8782
8783
8784 /* Display an echo area message M with a specified length of NBYTES
8785 bytes. The string may include null characters. If M is not a
8786 string, clear out any existing message, and let the mini-buffer
8787 text show through.
8788
8789 This function cancels echoing. */
8790
8791 void
8792 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8793 {
8794 struct gcpro gcpro1;
8795
8796 GCPRO1 (m);
8797 clear_message (1,1);
8798 cancel_echoing ();
8799
8800 /* First flush out any partial line written with print. */
8801 message_log_maybe_newline ();
8802 if (STRINGP (m))
8803 {
8804 char *buffer;
8805 USE_SAFE_ALLOCA;
8806
8807 SAFE_ALLOCA (buffer, char *, nbytes);
8808 memcpy (buffer, SDATA (m), nbytes);
8809 message_dolog (buffer, nbytes, 1, multibyte);
8810 SAFE_FREE ();
8811 }
8812 message3_nolog (m, nbytes, multibyte);
8813
8814 UNGCPRO;
8815 }
8816
8817
8818 /* The non-logging version of message3.
8819 This does not cancel echoing, because it is used for echoing.
8820 Perhaps we need to make a separate function for echoing
8821 and make this cancel echoing. */
8822
8823 void
8824 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8825 {
8826 struct frame *sf = SELECTED_FRAME ();
8827 message_enable_multibyte = multibyte;
8828
8829 if (FRAME_INITIAL_P (sf))
8830 {
8831 if (noninteractive_need_newline)
8832 putc ('\n', stderr);
8833 noninteractive_need_newline = 0;
8834 if (STRINGP (m))
8835 fwrite (SDATA (m), nbytes, 1, stderr);
8836 if (cursor_in_echo_area == 0)
8837 fprintf (stderr, "\n");
8838 fflush (stderr);
8839 }
8840 /* A null message buffer means that the frame hasn't really been
8841 initialized yet. Error messages get reported properly by
8842 cmd_error, so this must be just an informative message; toss it. */
8843 else if (INTERACTIVE
8844 && sf->glyphs_initialized_p
8845 && FRAME_MESSAGE_BUF (sf))
8846 {
8847 Lisp_Object mini_window;
8848 Lisp_Object frame;
8849 struct frame *f;
8850
8851 /* Get the frame containing the mini-buffer
8852 that the selected frame is using. */
8853 mini_window = FRAME_MINIBUF_WINDOW (sf);
8854 frame = XWINDOW (mini_window)->frame;
8855 f = XFRAME (frame);
8856
8857 FRAME_SAMPLE_VISIBILITY (f);
8858 if (FRAME_VISIBLE_P (sf)
8859 && !FRAME_VISIBLE_P (f))
8860 Fmake_frame_visible (frame);
8861
8862 if (STRINGP (m) && SCHARS (m) > 0)
8863 {
8864 set_message (NULL, m, nbytes, multibyte);
8865 if (minibuffer_auto_raise)
8866 Fraise_frame (frame);
8867 /* Assume we are not echoing.
8868 (If we are, echo_now will override this.) */
8869 echo_message_buffer = Qnil;
8870 }
8871 else
8872 clear_message (1, 1);
8873
8874 do_pending_window_change (0);
8875 echo_area_display (1);
8876 do_pending_window_change (0);
8877 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8878 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8879 }
8880 }
8881
8882
8883 /* Display a null-terminated echo area message M. If M is 0, clear
8884 out any existing message, and let the mini-buffer text show through.
8885
8886 The buffer M must continue to exist until after the echo area gets
8887 cleared or some other message gets displayed there. Do not pass
8888 text that is stored in a Lisp string. Do not pass text in a buffer
8889 that was alloca'd. */
8890
8891 void
8892 message1 (const char *m)
8893 {
8894 message2 (m, (m ? strlen (m) : 0), 0);
8895 }
8896
8897
8898 /* The non-logging counterpart of message1. */
8899
8900 void
8901 message1_nolog (const char *m)
8902 {
8903 message2_nolog (m, (m ? strlen (m) : 0), 0);
8904 }
8905
8906 /* Display a message M which contains a single %s
8907 which gets replaced with STRING. */
8908
8909 void
8910 message_with_string (const char *m, Lisp_Object string, int log)
8911 {
8912 CHECK_STRING (string);
8913
8914 if (noninteractive)
8915 {
8916 if (m)
8917 {
8918 if (noninteractive_need_newline)
8919 putc ('\n', stderr);
8920 noninteractive_need_newline = 0;
8921 fprintf (stderr, m, SDATA (string));
8922 if (!cursor_in_echo_area)
8923 fprintf (stderr, "\n");
8924 fflush (stderr);
8925 }
8926 }
8927 else if (INTERACTIVE)
8928 {
8929 /* The frame whose minibuffer we're going to display the message on.
8930 It may be larger than the selected frame, so we need
8931 to use its buffer, not the selected frame's buffer. */
8932 Lisp_Object mini_window;
8933 struct frame *f, *sf = SELECTED_FRAME ();
8934
8935 /* Get the frame containing the minibuffer
8936 that the selected frame is using. */
8937 mini_window = FRAME_MINIBUF_WINDOW (sf);
8938 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8939
8940 /* A null message buffer means that the frame hasn't really been
8941 initialized yet. Error messages get reported properly by
8942 cmd_error, so this must be just an informative message; toss it. */
8943 if (FRAME_MESSAGE_BUF (f))
8944 {
8945 Lisp_Object args[2], msg;
8946 struct gcpro gcpro1, gcpro2;
8947
8948 args[0] = build_string (m);
8949 args[1] = msg = string;
8950 GCPRO2 (args[0], msg);
8951 gcpro1.nvars = 2;
8952
8953 msg = Fformat (2, args);
8954
8955 if (log)
8956 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8957 else
8958 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8959
8960 UNGCPRO;
8961
8962 /* Print should start at the beginning of the message
8963 buffer next time. */
8964 message_buf_print = 0;
8965 }
8966 }
8967 }
8968
8969
8970 /* Dump an informative message to the minibuf. If M is 0, clear out
8971 any existing message, and let the mini-buffer text show through. */
8972
8973 static void
8974 vmessage (const char *m, va_list ap)
8975 {
8976 if (noninteractive)
8977 {
8978 if (m)
8979 {
8980 if (noninteractive_need_newline)
8981 putc ('\n', stderr);
8982 noninteractive_need_newline = 0;
8983 vfprintf (stderr, m, ap);
8984 if (cursor_in_echo_area == 0)
8985 fprintf (stderr, "\n");
8986 fflush (stderr);
8987 }
8988 }
8989 else if (INTERACTIVE)
8990 {
8991 /* The frame whose mini-buffer we're going to display the message
8992 on. It may be larger than the selected frame, so we need to
8993 use its buffer, not the selected frame's buffer. */
8994 Lisp_Object mini_window;
8995 struct frame *f, *sf = SELECTED_FRAME ();
8996
8997 /* Get the frame containing the mini-buffer
8998 that the selected frame is using. */
8999 mini_window = FRAME_MINIBUF_WINDOW (sf);
9000 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9001
9002 /* A null message buffer means that the frame hasn't really been
9003 initialized yet. Error messages get reported properly by
9004 cmd_error, so this must be just an informative message; toss
9005 it. */
9006 if (FRAME_MESSAGE_BUF (f))
9007 {
9008 if (m)
9009 {
9010 size_t len;
9011
9012 len = doprnt (FRAME_MESSAGE_BUF (f),
9013 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9014
9015 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9016 }
9017 else
9018 message1 (0);
9019
9020 /* Print should start at the beginning of the message
9021 buffer next time. */
9022 message_buf_print = 0;
9023 }
9024 }
9025 }
9026
9027 void
9028 message (const char *m, ...)
9029 {
9030 va_list ap;
9031 va_start (ap, m);
9032 vmessage (m, ap);
9033 va_end (ap);
9034 }
9035
9036
9037 #if 0
9038 /* The non-logging version of message. */
9039
9040 void
9041 message_nolog (const char *m, ...)
9042 {
9043 Lisp_Object old_log_max;
9044 va_list ap;
9045 va_start (ap, m);
9046 old_log_max = Vmessage_log_max;
9047 Vmessage_log_max = Qnil;
9048 vmessage (m, ap);
9049 Vmessage_log_max = old_log_max;
9050 va_end (ap);
9051 }
9052 #endif
9053
9054
9055 /* Display the current message in the current mini-buffer. This is
9056 only called from error handlers in process.c, and is not time
9057 critical. */
9058
9059 void
9060 update_echo_area (void)
9061 {
9062 if (!NILP (echo_area_buffer[0]))
9063 {
9064 Lisp_Object string;
9065 string = Fcurrent_message ();
9066 message3 (string, SBYTES (string),
9067 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9068 }
9069 }
9070
9071
9072 /* Make sure echo area buffers in `echo_buffers' are live.
9073 If they aren't, make new ones. */
9074
9075 static void
9076 ensure_echo_area_buffers (void)
9077 {
9078 int i;
9079
9080 for (i = 0; i < 2; ++i)
9081 if (!BUFFERP (echo_buffer[i])
9082 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9083 {
9084 char name[30];
9085 Lisp_Object old_buffer;
9086 int j;
9087
9088 old_buffer = echo_buffer[i];
9089 sprintf (name, " *Echo Area %d*", i);
9090 echo_buffer[i] = Fget_buffer_create (build_string (name));
9091 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9092 /* to force word wrap in echo area -
9093 it was decided to postpone this*/
9094 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9095
9096 for (j = 0; j < 2; ++j)
9097 if (EQ (old_buffer, echo_area_buffer[j]))
9098 echo_area_buffer[j] = echo_buffer[i];
9099 }
9100 }
9101
9102
9103 /* Call FN with args A1..A4 with either the current or last displayed
9104 echo_area_buffer as current buffer.
9105
9106 WHICH zero means use the current message buffer
9107 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9108 from echo_buffer[] and clear it.
9109
9110 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9111 suitable buffer from echo_buffer[] and clear it.
9112
9113 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9114 that the current message becomes the last displayed one, make
9115 choose a suitable buffer for echo_area_buffer[0], and clear it.
9116
9117 Value is what FN returns. */
9118
9119 static int
9120 with_echo_area_buffer (struct window *w, int which,
9121 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9122 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9123 {
9124 Lisp_Object buffer;
9125 int this_one, the_other, clear_buffer_p, rc;
9126 int count = SPECPDL_INDEX ();
9127
9128 /* If buffers aren't live, make new ones. */
9129 ensure_echo_area_buffers ();
9130
9131 clear_buffer_p = 0;
9132
9133 if (which == 0)
9134 this_one = 0, the_other = 1;
9135 else if (which > 0)
9136 this_one = 1, the_other = 0;
9137 else
9138 {
9139 this_one = 0, the_other = 1;
9140 clear_buffer_p = 1;
9141
9142 /* We need a fresh one in case the current echo buffer equals
9143 the one containing the last displayed echo area message. */
9144 if (!NILP (echo_area_buffer[this_one])
9145 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9146 echo_area_buffer[this_one] = Qnil;
9147 }
9148
9149 /* Choose a suitable buffer from echo_buffer[] is we don't
9150 have one. */
9151 if (NILP (echo_area_buffer[this_one]))
9152 {
9153 echo_area_buffer[this_one]
9154 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9155 ? echo_buffer[the_other]
9156 : echo_buffer[this_one]);
9157 clear_buffer_p = 1;
9158 }
9159
9160 buffer = echo_area_buffer[this_one];
9161
9162 /* Don't get confused by reusing the buffer used for echoing
9163 for a different purpose. */
9164 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9165 cancel_echoing ();
9166
9167 record_unwind_protect (unwind_with_echo_area_buffer,
9168 with_echo_area_buffer_unwind_data (w));
9169
9170 /* Make the echo area buffer current. Note that for display
9171 purposes, it is not necessary that the displayed window's buffer
9172 == current_buffer, except for text property lookup. So, let's
9173 only set that buffer temporarily here without doing a full
9174 Fset_window_buffer. We must also change w->pointm, though,
9175 because otherwise an assertions in unshow_buffer fails, and Emacs
9176 aborts. */
9177 set_buffer_internal_1 (XBUFFER (buffer));
9178 if (w)
9179 {
9180 w->buffer = buffer;
9181 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9182 }
9183
9184 BVAR (current_buffer, undo_list) = Qt;
9185 BVAR (current_buffer, read_only) = Qnil;
9186 specbind (Qinhibit_read_only, Qt);
9187 specbind (Qinhibit_modification_hooks, Qt);
9188
9189 if (clear_buffer_p && Z > BEG)
9190 del_range (BEG, Z);
9191
9192 xassert (BEGV >= BEG);
9193 xassert (ZV <= Z && ZV >= BEGV);
9194
9195 rc = fn (a1, a2, a3, a4);
9196
9197 xassert (BEGV >= BEG);
9198 xassert (ZV <= Z && ZV >= BEGV);
9199
9200 unbind_to (count, Qnil);
9201 return rc;
9202 }
9203
9204
9205 /* Save state that should be preserved around the call to the function
9206 FN called in with_echo_area_buffer. */
9207
9208 static Lisp_Object
9209 with_echo_area_buffer_unwind_data (struct window *w)
9210 {
9211 int i = 0;
9212 Lisp_Object vector, tmp;
9213
9214 /* Reduce consing by keeping one vector in
9215 Vwith_echo_area_save_vector. */
9216 vector = Vwith_echo_area_save_vector;
9217 Vwith_echo_area_save_vector = Qnil;
9218
9219 if (NILP (vector))
9220 vector = Fmake_vector (make_number (7), Qnil);
9221
9222 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9223 ASET (vector, i, Vdeactivate_mark); ++i;
9224 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9225
9226 if (w)
9227 {
9228 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9229 ASET (vector, i, w->buffer); ++i;
9230 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9231 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9232 }
9233 else
9234 {
9235 int end = i + 4;
9236 for (; i < end; ++i)
9237 ASET (vector, i, Qnil);
9238 }
9239
9240 xassert (i == ASIZE (vector));
9241 return vector;
9242 }
9243
9244
9245 /* Restore global state from VECTOR which was created by
9246 with_echo_area_buffer_unwind_data. */
9247
9248 static Lisp_Object
9249 unwind_with_echo_area_buffer (Lisp_Object vector)
9250 {
9251 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9252 Vdeactivate_mark = AREF (vector, 1);
9253 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9254
9255 if (WINDOWP (AREF (vector, 3)))
9256 {
9257 struct window *w;
9258 Lisp_Object buffer, charpos, bytepos;
9259
9260 w = XWINDOW (AREF (vector, 3));
9261 buffer = AREF (vector, 4);
9262 charpos = AREF (vector, 5);
9263 bytepos = AREF (vector, 6);
9264
9265 w->buffer = buffer;
9266 set_marker_both (w->pointm, buffer,
9267 XFASTINT (charpos), XFASTINT (bytepos));
9268 }
9269
9270 Vwith_echo_area_save_vector = vector;
9271 return Qnil;
9272 }
9273
9274
9275 /* Set up the echo area for use by print functions. MULTIBYTE_P
9276 non-zero means we will print multibyte. */
9277
9278 void
9279 setup_echo_area_for_printing (int multibyte_p)
9280 {
9281 /* If we can't find an echo area any more, exit. */
9282 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9283 Fkill_emacs (Qnil);
9284
9285 ensure_echo_area_buffers ();
9286
9287 if (!message_buf_print)
9288 {
9289 /* A message has been output since the last time we printed.
9290 Choose a fresh echo area buffer. */
9291 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9292 echo_area_buffer[0] = echo_buffer[1];
9293 else
9294 echo_area_buffer[0] = echo_buffer[0];
9295
9296 /* Switch to that buffer and clear it. */
9297 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9298 BVAR (current_buffer, truncate_lines) = Qnil;
9299
9300 if (Z > BEG)
9301 {
9302 int count = SPECPDL_INDEX ();
9303 specbind (Qinhibit_read_only, Qt);
9304 /* Note that undo recording is always disabled. */
9305 del_range (BEG, Z);
9306 unbind_to (count, Qnil);
9307 }
9308 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9309
9310 /* Set up the buffer for the multibyteness we need. */
9311 if (multibyte_p
9312 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9313 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9314
9315 /* Raise the frame containing the echo area. */
9316 if (minibuffer_auto_raise)
9317 {
9318 struct frame *sf = SELECTED_FRAME ();
9319 Lisp_Object mini_window;
9320 mini_window = FRAME_MINIBUF_WINDOW (sf);
9321 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9322 }
9323
9324 message_log_maybe_newline ();
9325 message_buf_print = 1;
9326 }
9327 else
9328 {
9329 if (NILP (echo_area_buffer[0]))
9330 {
9331 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9332 echo_area_buffer[0] = echo_buffer[1];
9333 else
9334 echo_area_buffer[0] = echo_buffer[0];
9335 }
9336
9337 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9338 {
9339 /* Someone switched buffers between print requests. */
9340 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9341 BVAR (current_buffer, truncate_lines) = Qnil;
9342 }
9343 }
9344 }
9345
9346
9347 /* Display an echo area message in window W. Value is non-zero if W's
9348 height is changed. If display_last_displayed_message_p is
9349 non-zero, display the message that was last displayed, otherwise
9350 display the current message. */
9351
9352 static int
9353 display_echo_area (struct window *w)
9354 {
9355 int i, no_message_p, window_height_changed_p, count;
9356
9357 /* Temporarily disable garbage collections while displaying the echo
9358 area. This is done because a GC can print a message itself.
9359 That message would modify the echo area buffer's contents while a
9360 redisplay of the buffer is going on, and seriously confuse
9361 redisplay. */
9362 count = inhibit_garbage_collection ();
9363
9364 /* If there is no message, we must call display_echo_area_1
9365 nevertheless because it resizes the window. But we will have to
9366 reset the echo_area_buffer in question to nil at the end because
9367 with_echo_area_buffer will sets it to an empty buffer. */
9368 i = display_last_displayed_message_p ? 1 : 0;
9369 no_message_p = NILP (echo_area_buffer[i]);
9370
9371 window_height_changed_p
9372 = with_echo_area_buffer (w, display_last_displayed_message_p,
9373 display_echo_area_1,
9374 (intptr_t) w, Qnil, 0, 0);
9375
9376 if (no_message_p)
9377 echo_area_buffer[i] = Qnil;
9378
9379 unbind_to (count, Qnil);
9380 return window_height_changed_p;
9381 }
9382
9383
9384 /* Helper for display_echo_area. Display the current buffer which
9385 contains the current echo area message in window W, a mini-window,
9386 a pointer to which is passed in A1. A2..A4 are currently not used.
9387 Change the height of W so that all of the message is displayed.
9388 Value is non-zero if height of W was changed. */
9389
9390 static int
9391 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9392 {
9393 intptr_t i1 = a1;
9394 struct window *w = (struct window *) i1;
9395 Lisp_Object window;
9396 struct text_pos start;
9397 int window_height_changed_p = 0;
9398
9399 /* Do this before displaying, so that we have a large enough glyph
9400 matrix for the display. If we can't get enough space for the
9401 whole text, display the last N lines. That works by setting w->start. */
9402 window_height_changed_p = resize_mini_window (w, 0);
9403
9404 /* Use the starting position chosen by resize_mini_window. */
9405 SET_TEXT_POS_FROM_MARKER (start, w->start);
9406
9407 /* Display. */
9408 clear_glyph_matrix (w->desired_matrix);
9409 XSETWINDOW (window, w);
9410 try_window (window, start, 0);
9411
9412 return window_height_changed_p;
9413 }
9414
9415
9416 /* Resize the echo area window to exactly the size needed for the
9417 currently displayed message, if there is one. If a mini-buffer
9418 is active, don't shrink it. */
9419
9420 void
9421 resize_echo_area_exactly (void)
9422 {
9423 if (BUFFERP (echo_area_buffer[0])
9424 && WINDOWP (echo_area_window))
9425 {
9426 struct window *w = XWINDOW (echo_area_window);
9427 int resized_p;
9428 Lisp_Object resize_exactly;
9429
9430 if (minibuf_level == 0)
9431 resize_exactly = Qt;
9432 else
9433 resize_exactly = Qnil;
9434
9435 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9436 (intptr_t) w, resize_exactly,
9437 0, 0);
9438 if (resized_p)
9439 {
9440 ++windows_or_buffers_changed;
9441 ++update_mode_lines;
9442 redisplay_internal ();
9443 }
9444 }
9445 }
9446
9447
9448 /* Callback function for with_echo_area_buffer, when used from
9449 resize_echo_area_exactly. A1 contains a pointer to the window to
9450 resize, EXACTLY non-nil means resize the mini-window exactly to the
9451 size of the text displayed. A3 and A4 are not used. Value is what
9452 resize_mini_window returns. */
9453
9454 static int
9455 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9456 {
9457 intptr_t i1 = a1;
9458 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9459 }
9460
9461
9462 /* Resize mini-window W to fit the size of its contents. EXACT_P
9463 means size the window exactly to the size needed. Otherwise, it's
9464 only enlarged until W's buffer is empty.
9465
9466 Set W->start to the right place to begin display. If the whole
9467 contents fit, start at the beginning. Otherwise, start so as
9468 to make the end of the contents appear. This is particularly
9469 important for y-or-n-p, but seems desirable generally.
9470
9471 Value is non-zero if the window height has been changed. */
9472
9473 int
9474 resize_mini_window (struct window *w, int exact_p)
9475 {
9476 struct frame *f = XFRAME (w->frame);
9477 int window_height_changed_p = 0;
9478
9479 xassert (MINI_WINDOW_P (w));
9480
9481 /* By default, start display at the beginning. */
9482 set_marker_both (w->start, w->buffer,
9483 BUF_BEGV (XBUFFER (w->buffer)),
9484 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9485
9486 /* Don't resize windows while redisplaying a window; it would
9487 confuse redisplay functions when the size of the window they are
9488 displaying changes from under them. Such a resizing can happen,
9489 for instance, when which-func prints a long message while
9490 we are running fontification-functions. We're running these
9491 functions with safe_call which binds inhibit-redisplay to t. */
9492 if (!NILP (Vinhibit_redisplay))
9493 return 0;
9494
9495 /* Nil means don't try to resize. */
9496 if (NILP (Vresize_mini_windows)
9497 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9498 return 0;
9499
9500 if (!FRAME_MINIBUF_ONLY_P (f))
9501 {
9502 struct it it;
9503 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9504 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9505 int height, max_height;
9506 int unit = FRAME_LINE_HEIGHT (f);
9507 struct text_pos start;
9508 struct buffer *old_current_buffer = NULL;
9509
9510 if (current_buffer != XBUFFER (w->buffer))
9511 {
9512 old_current_buffer = current_buffer;
9513 set_buffer_internal (XBUFFER (w->buffer));
9514 }
9515
9516 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9517
9518 /* Compute the max. number of lines specified by the user. */
9519 if (FLOATP (Vmax_mini_window_height))
9520 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9521 else if (INTEGERP (Vmax_mini_window_height))
9522 max_height = XINT (Vmax_mini_window_height);
9523 else
9524 max_height = total_height / 4;
9525
9526 /* Correct that max. height if it's bogus. */
9527 max_height = max (1, max_height);
9528 max_height = min (total_height, max_height);
9529
9530 /* Find out the height of the text in the window. */
9531 if (it.line_wrap == TRUNCATE)
9532 height = 1;
9533 else
9534 {
9535 last_height = 0;
9536 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9537 if (it.max_ascent == 0 && it.max_descent == 0)
9538 height = it.current_y + last_height;
9539 else
9540 height = it.current_y + it.max_ascent + it.max_descent;
9541 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9542 height = (height + unit - 1) / unit;
9543 }
9544
9545 /* Compute a suitable window start. */
9546 if (height > max_height)
9547 {
9548 height = max_height;
9549 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9550 move_it_vertically_backward (&it, (height - 1) * unit);
9551 start = it.current.pos;
9552 }
9553 else
9554 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9555 SET_MARKER_FROM_TEXT_POS (w->start, start);
9556
9557 if (EQ (Vresize_mini_windows, Qgrow_only))
9558 {
9559 /* Let it grow only, until we display an empty message, in which
9560 case the window shrinks again. */
9561 if (height > WINDOW_TOTAL_LINES (w))
9562 {
9563 int old_height = WINDOW_TOTAL_LINES (w);
9564 freeze_window_starts (f, 1);
9565 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9566 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9567 }
9568 else if (height < WINDOW_TOTAL_LINES (w)
9569 && (exact_p || BEGV == ZV))
9570 {
9571 int old_height = WINDOW_TOTAL_LINES (w);
9572 freeze_window_starts (f, 0);
9573 shrink_mini_window (w);
9574 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9575 }
9576 }
9577 else
9578 {
9579 /* Always resize to exact size needed. */
9580 if (height > WINDOW_TOTAL_LINES (w))
9581 {
9582 int old_height = WINDOW_TOTAL_LINES (w);
9583 freeze_window_starts (f, 1);
9584 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9585 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9586 }
9587 else if (height < WINDOW_TOTAL_LINES (w))
9588 {
9589 int old_height = WINDOW_TOTAL_LINES (w);
9590 freeze_window_starts (f, 0);
9591 shrink_mini_window (w);
9592
9593 if (height)
9594 {
9595 freeze_window_starts (f, 1);
9596 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9597 }
9598
9599 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9600 }
9601 }
9602
9603 if (old_current_buffer)
9604 set_buffer_internal (old_current_buffer);
9605 }
9606
9607 return window_height_changed_p;
9608 }
9609
9610
9611 /* Value is the current message, a string, or nil if there is no
9612 current message. */
9613
9614 Lisp_Object
9615 current_message (void)
9616 {
9617 Lisp_Object msg;
9618
9619 if (!BUFFERP (echo_area_buffer[0]))
9620 msg = Qnil;
9621 else
9622 {
9623 with_echo_area_buffer (0, 0, current_message_1,
9624 (intptr_t) &msg, Qnil, 0, 0);
9625 if (NILP (msg))
9626 echo_area_buffer[0] = Qnil;
9627 }
9628
9629 return msg;
9630 }
9631
9632
9633 static int
9634 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9635 {
9636 intptr_t i1 = a1;
9637 Lisp_Object *msg = (Lisp_Object *) i1;
9638
9639 if (Z > BEG)
9640 *msg = make_buffer_string (BEG, Z, 1);
9641 else
9642 *msg = Qnil;
9643 return 0;
9644 }
9645
9646
9647 /* Push the current message on Vmessage_stack for later restauration
9648 by restore_message. Value is non-zero if the current message isn't
9649 empty. This is a relatively infrequent operation, so it's not
9650 worth optimizing. */
9651
9652 int
9653 push_message (void)
9654 {
9655 Lisp_Object msg;
9656 msg = current_message ();
9657 Vmessage_stack = Fcons (msg, Vmessage_stack);
9658 return STRINGP (msg);
9659 }
9660
9661
9662 /* Restore message display from the top of Vmessage_stack. */
9663
9664 void
9665 restore_message (void)
9666 {
9667 Lisp_Object msg;
9668
9669 xassert (CONSP (Vmessage_stack));
9670 msg = XCAR (Vmessage_stack);
9671 if (STRINGP (msg))
9672 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9673 else
9674 message3_nolog (msg, 0, 0);
9675 }
9676
9677
9678 /* Handler for record_unwind_protect calling pop_message. */
9679
9680 Lisp_Object
9681 pop_message_unwind (Lisp_Object dummy)
9682 {
9683 pop_message ();
9684 return Qnil;
9685 }
9686
9687 /* Pop the top-most entry off Vmessage_stack. */
9688
9689 static void
9690 pop_message (void)
9691 {
9692 xassert (CONSP (Vmessage_stack));
9693 Vmessage_stack = XCDR (Vmessage_stack);
9694 }
9695
9696
9697 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9698 exits. If the stack is not empty, we have a missing pop_message
9699 somewhere. */
9700
9701 void
9702 check_message_stack (void)
9703 {
9704 if (!NILP (Vmessage_stack))
9705 abort ();
9706 }
9707
9708
9709 /* Truncate to NCHARS what will be displayed in the echo area the next
9710 time we display it---but don't redisplay it now. */
9711
9712 void
9713 truncate_echo_area (EMACS_INT nchars)
9714 {
9715 if (nchars == 0)
9716 echo_area_buffer[0] = Qnil;
9717 /* A null message buffer means that the frame hasn't really been
9718 initialized yet. Error messages get reported properly by
9719 cmd_error, so this must be just an informative message; toss it. */
9720 else if (!noninteractive
9721 && INTERACTIVE
9722 && !NILP (echo_area_buffer[0]))
9723 {
9724 struct frame *sf = SELECTED_FRAME ();
9725 if (FRAME_MESSAGE_BUF (sf))
9726 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9727 }
9728 }
9729
9730
9731 /* Helper function for truncate_echo_area. Truncate the current
9732 message to at most NCHARS characters. */
9733
9734 static int
9735 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9736 {
9737 if (BEG + nchars < Z)
9738 del_range (BEG + nchars, Z);
9739 if (Z == BEG)
9740 echo_area_buffer[0] = Qnil;
9741 return 0;
9742 }
9743
9744
9745 /* Set the current message to a substring of S or STRING.
9746
9747 If STRING is a Lisp string, set the message to the first NBYTES
9748 bytes from STRING. NBYTES zero means use the whole string. If
9749 STRING is multibyte, the message will be displayed multibyte.
9750
9751 If S is not null, set the message to the first LEN bytes of S. LEN
9752 zero means use the whole string. MULTIBYTE_P non-zero means S is
9753 multibyte. Display the message multibyte in that case.
9754
9755 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9756 to t before calling set_message_1 (which calls insert).
9757 */
9758
9759 static void
9760 set_message (const char *s, Lisp_Object string,
9761 EMACS_INT nbytes, int multibyte_p)
9762 {
9763 message_enable_multibyte
9764 = ((s && multibyte_p)
9765 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9766
9767 with_echo_area_buffer (0, -1, set_message_1,
9768 (intptr_t) s, string, nbytes, multibyte_p);
9769 message_buf_print = 0;
9770 help_echo_showing_p = 0;
9771 }
9772
9773
9774 /* Helper function for set_message. Arguments have the same meaning
9775 as there, with A1 corresponding to S and A2 corresponding to STRING
9776 This function is called with the echo area buffer being
9777 current. */
9778
9779 static int
9780 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9781 {
9782 intptr_t i1 = a1;
9783 const char *s = (const char *) i1;
9784 const unsigned char *msg = (const unsigned char *) s;
9785 Lisp_Object string = a2;
9786
9787 /* Change multibyteness of the echo buffer appropriately. */
9788 if (message_enable_multibyte
9789 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9790 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9791
9792 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9793 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9794 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9795
9796 /* Insert new message at BEG. */
9797 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9798
9799 if (STRINGP (string))
9800 {
9801 EMACS_INT nchars;
9802
9803 if (nbytes == 0)
9804 nbytes = SBYTES (string);
9805 nchars = string_byte_to_char (string, nbytes);
9806
9807 /* This function takes care of single/multibyte conversion. We
9808 just have to ensure that the echo area buffer has the right
9809 setting of enable_multibyte_characters. */
9810 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9811 }
9812 else if (s)
9813 {
9814 if (nbytes == 0)
9815 nbytes = strlen (s);
9816
9817 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9818 {
9819 /* Convert from multi-byte to single-byte. */
9820 EMACS_INT i;
9821 int c, n;
9822 char work[1];
9823
9824 /* Convert a multibyte string to single-byte. */
9825 for (i = 0; i < nbytes; i += n)
9826 {
9827 c = string_char_and_length (msg + i, &n);
9828 work[0] = (ASCII_CHAR_P (c)
9829 ? c
9830 : multibyte_char_to_unibyte (c));
9831 insert_1_both (work, 1, 1, 1, 0, 0);
9832 }
9833 }
9834 else if (!multibyte_p
9835 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9836 {
9837 /* Convert from single-byte to multi-byte. */
9838 EMACS_INT i;
9839 int c, n;
9840 unsigned char str[MAX_MULTIBYTE_LENGTH];
9841
9842 /* Convert a single-byte string to multibyte. */
9843 for (i = 0; i < nbytes; i++)
9844 {
9845 c = msg[i];
9846 MAKE_CHAR_MULTIBYTE (c);
9847 n = CHAR_STRING (c, str);
9848 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9849 }
9850 }
9851 else
9852 insert_1 (s, nbytes, 1, 0, 0);
9853 }
9854
9855 return 0;
9856 }
9857
9858
9859 /* Clear messages. CURRENT_P non-zero means clear the current
9860 message. LAST_DISPLAYED_P non-zero means clear the message
9861 last displayed. */
9862
9863 void
9864 clear_message (int current_p, int last_displayed_p)
9865 {
9866 if (current_p)
9867 {
9868 echo_area_buffer[0] = Qnil;
9869 message_cleared_p = 1;
9870 }
9871
9872 if (last_displayed_p)
9873 echo_area_buffer[1] = Qnil;
9874
9875 message_buf_print = 0;
9876 }
9877
9878 /* Clear garbaged frames.
9879
9880 This function is used where the old redisplay called
9881 redraw_garbaged_frames which in turn called redraw_frame which in
9882 turn called clear_frame. The call to clear_frame was a source of
9883 flickering. I believe a clear_frame is not necessary. It should
9884 suffice in the new redisplay to invalidate all current matrices,
9885 and ensure a complete redisplay of all windows. */
9886
9887 static void
9888 clear_garbaged_frames (void)
9889 {
9890 if (frame_garbaged)
9891 {
9892 Lisp_Object tail, frame;
9893 int changed_count = 0;
9894
9895 FOR_EACH_FRAME (tail, frame)
9896 {
9897 struct frame *f = XFRAME (frame);
9898
9899 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9900 {
9901 if (f->resized_p)
9902 {
9903 Fredraw_frame (frame);
9904 f->force_flush_display_p = 1;
9905 }
9906 clear_current_matrices (f);
9907 changed_count++;
9908 f->garbaged = 0;
9909 f->resized_p = 0;
9910 }
9911 }
9912
9913 frame_garbaged = 0;
9914 if (changed_count)
9915 ++windows_or_buffers_changed;
9916 }
9917 }
9918
9919
9920 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9921 is non-zero update selected_frame. Value is non-zero if the
9922 mini-windows height has been changed. */
9923
9924 static int
9925 echo_area_display (int update_frame_p)
9926 {
9927 Lisp_Object mini_window;
9928 struct window *w;
9929 struct frame *f;
9930 int window_height_changed_p = 0;
9931 struct frame *sf = SELECTED_FRAME ();
9932
9933 mini_window = FRAME_MINIBUF_WINDOW (sf);
9934 w = XWINDOW (mini_window);
9935 f = XFRAME (WINDOW_FRAME (w));
9936
9937 /* Don't display if frame is invisible or not yet initialized. */
9938 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9939 return 0;
9940
9941 #ifdef HAVE_WINDOW_SYSTEM
9942 /* When Emacs starts, selected_frame may be the initial terminal
9943 frame. If we let this through, a message would be displayed on
9944 the terminal. */
9945 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9946 return 0;
9947 #endif /* HAVE_WINDOW_SYSTEM */
9948
9949 /* Redraw garbaged frames. */
9950 if (frame_garbaged)
9951 clear_garbaged_frames ();
9952
9953 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9954 {
9955 echo_area_window = mini_window;
9956 window_height_changed_p = display_echo_area (w);
9957 w->must_be_updated_p = 1;
9958
9959 /* Update the display, unless called from redisplay_internal.
9960 Also don't update the screen during redisplay itself. The
9961 update will happen at the end of redisplay, and an update
9962 here could cause confusion. */
9963 if (update_frame_p && !redisplaying_p)
9964 {
9965 int n = 0;
9966
9967 /* If the display update has been interrupted by pending
9968 input, update mode lines in the frame. Due to the
9969 pending input, it might have been that redisplay hasn't
9970 been called, so that mode lines above the echo area are
9971 garbaged. This looks odd, so we prevent it here. */
9972 if (!display_completed)
9973 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9974
9975 if (window_height_changed_p
9976 /* Don't do this if Emacs is shutting down. Redisplay
9977 needs to run hooks. */
9978 && !NILP (Vrun_hooks))
9979 {
9980 /* Must update other windows. Likewise as in other
9981 cases, don't let this update be interrupted by
9982 pending input. */
9983 int count = SPECPDL_INDEX ();
9984 specbind (Qredisplay_dont_pause, Qt);
9985 windows_or_buffers_changed = 1;
9986 redisplay_internal ();
9987 unbind_to (count, Qnil);
9988 }
9989 else if (FRAME_WINDOW_P (f) && n == 0)
9990 {
9991 /* Window configuration is the same as before.
9992 Can do with a display update of the echo area,
9993 unless we displayed some mode lines. */
9994 update_single_window (w, 1);
9995 FRAME_RIF (f)->flush_display (f);
9996 }
9997 else
9998 update_frame (f, 1, 1);
9999
10000 /* If cursor is in the echo area, make sure that the next
10001 redisplay displays the minibuffer, so that the cursor will
10002 be replaced with what the minibuffer wants. */
10003 if (cursor_in_echo_area)
10004 ++windows_or_buffers_changed;
10005 }
10006 }
10007 else if (!EQ (mini_window, selected_window))
10008 windows_or_buffers_changed++;
10009
10010 /* Last displayed message is now the current message. */
10011 echo_area_buffer[1] = echo_area_buffer[0];
10012 /* Inform read_char that we're not echoing. */
10013 echo_message_buffer = Qnil;
10014
10015 /* Prevent redisplay optimization in redisplay_internal by resetting
10016 this_line_start_pos. This is done because the mini-buffer now
10017 displays the message instead of its buffer text. */
10018 if (EQ (mini_window, selected_window))
10019 CHARPOS (this_line_start_pos) = 0;
10020
10021 return window_height_changed_p;
10022 }
10023
10024
10025 \f
10026 /***********************************************************************
10027 Mode Lines and Frame Titles
10028 ***********************************************************************/
10029
10030 /* A buffer for constructing non-propertized mode-line strings and
10031 frame titles in it; allocated from the heap in init_xdisp and
10032 resized as needed in store_mode_line_noprop_char. */
10033
10034 static char *mode_line_noprop_buf;
10035
10036 /* The buffer's end, and a current output position in it. */
10037
10038 static char *mode_line_noprop_buf_end;
10039 static char *mode_line_noprop_ptr;
10040
10041 #define MODE_LINE_NOPROP_LEN(start) \
10042 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10043
10044 static enum {
10045 MODE_LINE_DISPLAY = 0,
10046 MODE_LINE_TITLE,
10047 MODE_LINE_NOPROP,
10048 MODE_LINE_STRING
10049 } mode_line_target;
10050
10051 /* Alist that caches the results of :propertize.
10052 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10053 static Lisp_Object mode_line_proptrans_alist;
10054
10055 /* List of strings making up the mode-line. */
10056 static Lisp_Object mode_line_string_list;
10057
10058 /* Base face property when building propertized mode line string. */
10059 static Lisp_Object mode_line_string_face;
10060 static Lisp_Object mode_line_string_face_prop;
10061
10062
10063 /* Unwind data for mode line strings */
10064
10065 static Lisp_Object Vmode_line_unwind_vector;
10066
10067 static Lisp_Object
10068 format_mode_line_unwind_data (struct buffer *obuf,
10069 Lisp_Object owin,
10070 int save_proptrans)
10071 {
10072 Lisp_Object vector, tmp;
10073
10074 /* Reduce consing by keeping one vector in
10075 Vwith_echo_area_save_vector. */
10076 vector = Vmode_line_unwind_vector;
10077 Vmode_line_unwind_vector = Qnil;
10078
10079 if (NILP (vector))
10080 vector = Fmake_vector (make_number (8), Qnil);
10081
10082 ASET (vector, 0, make_number (mode_line_target));
10083 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10084 ASET (vector, 2, mode_line_string_list);
10085 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10086 ASET (vector, 4, mode_line_string_face);
10087 ASET (vector, 5, mode_line_string_face_prop);
10088
10089 if (obuf)
10090 XSETBUFFER (tmp, obuf);
10091 else
10092 tmp = Qnil;
10093 ASET (vector, 6, tmp);
10094 ASET (vector, 7, owin);
10095
10096 return vector;
10097 }
10098
10099 static Lisp_Object
10100 unwind_format_mode_line (Lisp_Object vector)
10101 {
10102 mode_line_target = XINT (AREF (vector, 0));
10103 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10104 mode_line_string_list = AREF (vector, 2);
10105 if (! EQ (AREF (vector, 3), Qt))
10106 mode_line_proptrans_alist = AREF (vector, 3);
10107 mode_line_string_face = AREF (vector, 4);
10108 mode_line_string_face_prop = AREF (vector, 5);
10109
10110 if (!NILP (AREF (vector, 7)))
10111 /* Select window before buffer, since it may change the buffer. */
10112 Fselect_window (AREF (vector, 7), Qt);
10113
10114 if (!NILP (AREF (vector, 6)))
10115 {
10116 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10117 ASET (vector, 6, Qnil);
10118 }
10119
10120 Vmode_line_unwind_vector = vector;
10121 return Qnil;
10122 }
10123
10124
10125 /* Store a single character C for the frame title in mode_line_noprop_buf.
10126 Re-allocate mode_line_noprop_buf if necessary. */
10127
10128 static void
10129 store_mode_line_noprop_char (char c)
10130 {
10131 /* If output position has reached the end of the allocated buffer,
10132 double the buffer's size. */
10133 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10134 {
10135 int len = MODE_LINE_NOPROP_LEN (0);
10136 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10137 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10138 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10139 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10140 }
10141
10142 *mode_line_noprop_ptr++ = c;
10143 }
10144
10145
10146 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10147 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10148 characters that yield more columns than PRECISION; PRECISION <= 0
10149 means copy the whole string. Pad with spaces until FIELD_WIDTH
10150 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10151 pad. Called from display_mode_element when it is used to build a
10152 frame title. */
10153
10154 static int
10155 store_mode_line_noprop (const char *string, int field_width, int precision)
10156 {
10157 const unsigned char *str = (const unsigned char *) string;
10158 int n = 0;
10159 EMACS_INT dummy, nbytes;
10160
10161 /* Copy at most PRECISION chars from STR. */
10162 nbytes = strlen (string);
10163 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10164 while (nbytes--)
10165 store_mode_line_noprop_char (*str++);
10166
10167 /* Fill up with spaces until FIELD_WIDTH reached. */
10168 while (field_width > 0
10169 && n < field_width)
10170 {
10171 store_mode_line_noprop_char (' ');
10172 ++n;
10173 }
10174
10175 return n;
10176 }
10177
10178 /***********************************************************************
10179 Frame Titles
10180 ***********************************************************************/
10181
10182 #ifdef HAVE_WINDOW_SYSTEM
10183
10184 /* Set the title of FRAME, if it has changed. The title format is
10185 Vicon_title_format if FRAME is iconified, otherwise it is
10186 frame_title_format. */
10187
10188 static void
10189 x_consider_frame_title (Lisp_Object frame)
10190 {
10191 struct frame *f = XFRAME (frame);
10192
10193 if (FRAME_WINDOW_P (f)
10194 || FRAME_MINIBUF_ONLY_P (f)
10195 || f->explicit_name)
10196 {
10197 /* Do we have more than one visible frame on this X display? */
10198 Lisp_Object tail;
10199 Lisp_Object fmt;
10200 int title_start;
10201 char *title;
10202 int len;
10203 struct it it;
10204 int count = SPECPDL_INDEX ();
10205
10206 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10207 {
10208 Lisp_Object other_frame = XCAR (tail);
10209 struct frame *tf = XFRAME (other_frame);
10210
10211 if (tf != f
10212 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10213 && !FRAME_MINIBUF_ONLY_P (tf)
10214 && !EQ (other_frame, tip_frame)
10215 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10216 break;
10217 }
10218
10219 /* Set global variable indicating that multiple frames exist. */
10220 multiple_frames = CONSP (tail);
10221
10222 /* Switch to the buffer of selected window of the frame. Set up
10223 mode_line_target so that display_mode_element will output into
10224 mode_line_noprop_buf; then display the title. */
10225 record_unwind_protect (unwind_format_mode_line,
10226 format_mode_line_unwind_data
10227 (current_buffer, selected_window, 0));
10228
10229 Fselect_window (f->selected_window, Qt);
10230 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10231 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10232
10233 mode_line_target = MODE_LINE_TITLE;
10234 title_start = MODE_LINE_NOPROP_LEN (0);
10235 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10236 NULL, DEFAULT_FACE_ID);
10237 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10238 len = MODE_LINE_NOPROP_LEN (title_start);
10239 title = mode_line_noprop_buf + title_start;
10240 unbind_to (count, Qnil);
10241
10242 /* Set the title only if it's changed. This avoids consing in
10243 the common case where it hasn't. (If it turns out that we've
10244 already wasted too much time by walking through the list with
10245 display_mode_element, then we might need to optimize at a
10246 higher level than this.) */
10247 if (! STRINGP (f->name)
10248 || SBYTES (f->name) != len
10249 || memcmp (title, SDATA (f->name), len) != 0)
10250 x_implicitly_set_name (f, make_string (title, len), Qnil);
10251 }
10252 }
10253
10254 #endif /* not HAVE_WINDOW_SYSTEM */
10255
10256
10257
10258 \f
10259 /***********************************************************************
10260 Menu Bars
10261 ***********************************************************************/
10262
10263
10264 /* Prepare for redisplay by updating menu-bar item lists when
10265 appropriate. This can call eval. */
10266
10267 void
10268 prepare_menu_bars (void)
10269 {
10270 int all_windows;
10271 struct gcpro gcpro1, gcpro2;
10272 struct frame *f;
10273 Lisp_Object tooltip_frame;
10274
10275 #ifdef HAVE_WINDOW_SYSTEM
10276 tooltip_frame = tip_frame;
10277 #else
10278 tooltip_frame = Qnil;
10279 #endif
10280
10281 /* Update all frame titles based on their buffer names, etc. We do
10282 this before the menu bars so that the buffer-menu will show the
10283 up-to-date frame titles. */
10284 #ifdef HAVE_WINDOW_SYSTEM
10285 if (windows_or_buffers_changed || update_mode_lines)
10286 {
10287 Lisp_Object tail, frame;
10288
10289 FOR_EACH_FRAME (tail, frame)
10290 {
10291 f = XFRAME (frame);
10292 if (!EQ (frame, tooltip_frame)
10293 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10294 x_consider_frame_title (frame);
10295 }
10296 }
10297 #endif /* HAVE_WINDOW_SYSTEM */
10298
10299 /* Update the menu bar item lists, if appropriate. This has to be
10300 done before any actual redisplay or generation of display lines. */
10301 all_windows = (update_mode_lines
10302 || buffer_shared > 1
10303 || windows_or_buffers_changed);
10304 if (all_windows)
10305 {
10306 Lisp_Object tail, frame;
10307 int count = SPECPDL_INDEX ();
10308 /* 1 means that update_menu_bar has run its hooks
10309 so any further calls to update_menu_bar shouldn't do so again. */
10310 int menu_bar_hooks_run = 0;
10311
10312 record_unwind_save_match_data ();
10313
10314 FOR_EACH_FRAME (tail, frame)
10315 {
10316 f = XFRAME (frame);
10317
10318 /* Ignore tooltip frame. */
10319 if (EQ (frame, tooltip_frame))
10320 continue;
10321
10322 /* If a window on this frame changed size, report that to
10323 the user and clear the size-change flag. */
10324 if (FRAME_WINDOW_SIZES_CHANGED (f))
10325 {
10326 Lisp_Object functions;
10327
10328 /* Clear flag first in case we get an error below. */
10329 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10330 functions = Vwindow_size_change_functions;
10331 GCPRO2 (tail, functions);
10332
10333 while (CONSP (functions))
10334 {
10335 if (!EQ (XCAR (functions), Qt))
10336 call1 (XCAR (functions), frame);
10337 functions = XCDR (functions);
10338 }
10339 UNGCPRO;
10340 }
10341
10342 GCPRO1 (tail);
10343 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10344 #ifdef HAVE_WINDOW_SYSTEM
10345 update_tool_bar (f, 0);
10346 #endif
10347 #ifdef HAVE_NS
10348 if (windows_or_buffers_changed
10349 && FRAME_NS_P (f))
10350 ns_set_doc_edited (f, Fbuffer_modified_p
10351 (XWINDOW (f->selected_window)->buffer));
10352 #endif
10353 UNGCPRO;
10354 }
10355
10356 unbind_to (count, Qnil);
10357 }
10358 else
10359 {
10360 struct frame *sf = SELECTED_FRAME ();
10361 update_menu_bar (sf, 1, 0);
10362 #ifdef HAVE_WINDOW_SYSTEM
10363 update_tool_bar (sf, 1);
10364 #endif
10365 }
10366 }
10367
10368
10369 /* Update the menu bar item list for frame F. This has to be done
10370 before we start to fill in any display lines, because it can call
10371 eval.
10372
10373 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10374
10375 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10376 already ran the menu bar hooks for this redisplay, so there
10377 is no need to run them again. The return value is the
10378 updated value of this flag, to pass to the next call. */
10379
10380 static int
10381 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10382 {
10383 Lisp_Object window;
10384 register struct window *w;
10385
10386 /* If called recursively during a menu update, do nothing. This can
10387 happen when, for instance, an activate-menubar-hook causes a
10388 redisplay. */
10389 if (inhibit_menubar_update)
10390 return hooks_run;
10391
10392 window = FRAME_SELECTED_WINDOW (f);
10393 w = XWINDOW (window);
10394
10395 if (FRAME_WINDOW_P (f)
10396 ?
10397 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10398 || defined (HAVE_NS) || defined (USE_GTK)
10399 FRAME_EXTERNAL_MENU_BAR (f)
10400 #else
10401 FRAME_MENU_BAR_LINES (f) > 0
10402 #endif
10403 : FRAME_MENU_BAR_LINES (f) > 0)
10404 {
10405 /* If the user has switched buffers or windows, we need to
10406 recompute to reflect the new bindings. But we'll
10407 recompute when update_mode_lines is set too; that means
10408 that people can use force-mode-line-update to request
10409 that the menu bar be recomputed. The adverse effect on
10410 the rest of the redisplay algorithm is about the same as
10411 windows_or_buffers_changed anyway. */
10412 if (windows_or_buffers_changed
10413 /* This used to test w->update_mode_line, but we believe
10414 there is no need to recompute the menu in that case. */
10415 || update_mode_lines
10416 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10417 < BUF_MODIFF (XBUFFER (w->buffer)))
10418 != !NILP (w->last_had_star))
10419 || ((!NILP (Vtransient_mark_mode)
10420 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10421 != !NILP (w->region_showing)))
10422 {
10423 struct buffer *prev = current_buffer;
10424 int count = SPECPDL_INDEX ();
10425
10426 specbind (Qinhibit_menubar_update, Qt);
10427
10428 set_buffer_internal_1 (XBUFFER (w->buffer));
10429 if (save_match_data)
10430 record_unwind_save_match_data ();
10431 if (NILP (Voverriding_local_map_menu_flag))
10432 {
10433 specbind (Qoverriding_terminal_local_map, Qnil);
10434 specbind (Qoverriding_local_map, Qnil);
10435 }
10436
10437 if (!hooks_run)
10438 {
10439 /* Run the Lucid hook. */
10440 safe_run_hooks (Qactivate_menubar_hook);
10441
10442 /* If it has changed current-menubar from previous value,
10443 really recompute the menu-bar from the value. */
10444 if (! NILP (Vlucid_menu_bar_dirty_flag))
10445 call0 (Qrecompute_lucid_menubar);
10446
10447 safe_run_hooks (Qmenu_bar_update_hook);
10448
10449 hooks_run = 1;
10450 }
10451
10452 XSETFRAME (Vmenu_updating_frame, f);
10453 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10454
10455 /* Redisplay the menu bar in case we changed it. */
10456 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10457 || defined (HAVE_NS) || defined (USE_GTK)
10458 if (FRAME_WINDOW_P (f))
10459 {
10460 #if defined (HAVE_NS)
10461 /* All frames on Mac OS share the same menubar. So only
10462 the selected frame should be allowed to set it. */
10463 if (f == SELECTED_FRAME ())
10464 #endif
10465 set_frame_menubar (f, 0, 0);
10466 }
10467 else
10468 /* On a terminal screen, the menu bar is an ordinary screen
10469 line, and this makes it get updated. */
10470 w->update_mode_line = Qt;
10471 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10472 /* In the non-toolkit version, the menu bar is an ordinary screen
10473 line, and this makes it get updated. */
10474 w->update_mode_line = Qt;
10475 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10476
10477 unbind_to (count, Qnil);
10478 set_buffer_internal_1 (prev);
10479 }
10480 }
10481
10482 return hooks_run;
10483 }
10484
10485
10486 \f
10487 /***********************************************************************
10488 Output Cursor
10489 ***********************************************************************/
10490
10491 #ifdef HAVE_WINDOW_SYSTEM
10492
10493 /* EXPORT:
10494 Nominal cursor position -- where to draw output.
10495 HPOS and VPOS are window relative glyph matrix coordinates.
10496 X and Y are window relative pixel coordinates. */
10497
10498 struct cursor_pos output_cursor;
10499
10500
10501 /* EXPORT:
10502 Set the global variable output_cursor to CURSOR. All cursor
10503 positions are relative to updated_window. */
10504
10505 void
10506 set_output_cursor (struct cursor_pos *cursor)
10507 {
10508 output_cursor.hpos = cursor->hpos;
10509 output_cursor.vpos = cursor->vpos;
10510 output_cursor.x = cursor->x;
10511 output_cursor.y = cursor->y;
10512 }
10513
10514
10515 /* EXPORT for RIF:
10516 Set a nominal cursor position.
10517
10518 HPOS and VPOS are column/row positions in a window glyph matrix. X
10519 and Y are window text area relative pixel positions.
10520
10521 If this is done during an update, updated_window will contain the
10522 window that is being updated and the position is the future output
10523 cursor position for that window. If updated_window is null, use
10524 selected_window and display the cursor at the given position. */
10525
10526 void
10527 x_cursor_to (int vpos, int hpos, int y, int x)
10528 {
10529 struct window *w;
10530
10531 /* If updated_window is not set, work on selected_window. */
10532 if (updated_window)
10533 w = updated_window;
10534 else
10535 w = XWINDOW (selected_window);
10536
10537 /* Set the output cursor. */
10538 output_cursor.hpos = hpos;
10539 output_cursor.vpos = vpos;
10540 output_cursor.x = x;
10541 output_cursor.y = y;
10542
10543 /* If not called as part of an update, really display the cursor.
10544 This will also set the cursor position of W. */
10545 if (updated_window == NULL)
10546 {
10547 BLOCK_INPUT;
10548 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10549 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10550 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10551 UNBLOCK_INPUT;
10552 }
10553 }
10554
10555 #endif /* HAVE_WINDOW_SYSTEM */
10556
10557 \f
10558 /***********************************************************************
10559 Tool-bars
10560 ***********************************************************************/
10561
10562 #ifdef HAVE_WINDOW_SYSTEM
10563
10564 /* Where the mouse was last time we reported a mouse event. */
10565
10566 FRAME_PTR last_mouse_frame;
10567
10568 /* Tool-bar item index of the item on which a mouse button was pressed
10569 or -1. */
10570
10571 int last_tool_bar_item;
10572
10573
10574 static Lisp_Object
10575 update_tool_bar_unwind (Lisp_Object frame)
10576 {
10577 selected_frame = frame;
10578 return Qnil;
10579 }
10580
10581 /* Update the tool-bar item list for frame F. This has to be done
10582 before we start to fill in any display lines. Called from
10583 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10584 and restore it here. */
10585
10586 static void
10587 update_tool_bar (struct frame *f, int save_match_data)
10588 {
10589 #if defined (USE_GTK) || defined (HAVE_NS)
10590 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10591 #else
10592 int do_update = WINDOWP (f->tool_bar_window)
10593 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10594 #endif
10595
10596 if (do_update)
10597 {
10598 Lisp_Object window;
10599 struct window *w;
10600
10601 window = FRAME_SELECTED_WINDOW (f);
10602 w = XWINDOW (window);
10603
10604 /* If the user has switched buffers or windows, we need to
10605 recompute to reflect the new bindings. But we'll
10606 recompute when update_mode_lines is set too; that means
10607 that people can use force-mode-line-update to request
10608 that the menu bar be recomputed. The adverse effect on
10609 the rest of the redisplay algorithm is about the same as
10610 windows_or_buffers_changed anyway. */
10611 if (windows_or_buffers_changed
10612 || !NILP (w->update_mode_line)
10613 || update_mode_lines
10614 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10615 < BUF_MODIFF (XBUFFER (w->buffer)))
10616 != !NILP (w->last_had_star))
10617 || ((!NILP (Vtransient_mark_mode)
10618 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10619 != !NILP (w->region_showing)))
10620 {
10621 struct buffer *prev = current_buffer;
10622 int count = SPECPDL_INDEX ();
10623 Lisp_Object frame, new_tool_bar;
10624 int new_n_tool_bar;
10625 struct gcpro gcpro1;
10626
10627 /* Set current_buffer to the buffer of the selected
10628 window of the frame, so that we get the right local
10629 keymaps. */
10630 set_buffer_internal_1 (XBUFFER (w->buffer));
10631
10632 /* Save match data, if we must. */
10633 if (save_match_data)
10634 record_unwind_save_match_data ();
10635
10636 /* Make sure that we don't accidentally use bogus keymaps. */
10637 if (NILP (Voverriding_local_map_menu_flag))
10638 {
10639 specbind (Qoverriding_terminal_local_map, Qnil);
10640 specbind (Qoverriding_local_map, Qnil);
10641 }
10642
10643 GCPRO1 (new_tool_bar);
10644
10645 /* We must temporarily set the selected frame to this frame
10646 before calling tool_bar_items, because the calculation of
10647 the tool-bar keymap uses the selected frame (see
10648 `tool-bar-make-keymap' in tool-bar.el). */
10649 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10650 XSETFRAME (frame, f);
10651 selected_frame = frame;
10652
10653 /* Build desired tool-bar items from keymaps. */
10654 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10655 &new_n_tool_bar);
10656
10657 /* Redisplay the tool-bar if we changed it. */
10658 if (new_n_tool_bar != f->n_tool_bar_items
10659 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10660 {
10661 /* Redisplay that happens asynchronously due to an expose event
10662 may access f->tool_bar_items. Make sure we update both
10663 variables within BLOCK_INPUT so no such event interrupts. */
10664 BLOCK_INPUT;
10665 f->tool_bar_items = new_tool_bar;
10666 f->n_tool_bar_items = new_n_tool_bar;
10667 w->update_mode_line = Qt;
10668 UNBLOCK_INPUT;
10669 }
10670
10671 UNGCPRO;
10672
10673 unbind_to (count, Qnil);
10674 set_buffer_internal_1 (prev);
10675 }
10676 }
10677 }
10678
10679
10680 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10681 F's desired tool-bar contents. F->tool_bar_items must have
10682 been set up previously by calling prepare_menu_bars. */
10683
10684 static void
10685 build_desired_tool_bar_string (struct frame *f)
10686 {
10687 int i, size, size_needed;
10688 struct gcpro gcpro1, gcpro2, gcpro3;
10689 Lisp_Object image, plist, props;
10690
10691 image = plist = props = Qnil;
10692 GCPRO3 (image, plist, props);
10693
10694 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10695 Otherwise, make a new string. */
10696
10697 /* The size of the string we might be able to reuse. */
10698 size = (STRINGP (f->desired_tool_bar_string)
10699 ? SCHARS (f->desired_tool_bar_string)
10700 : 0);
10701
10702 /* We need one space in the string for each image. */
10703 size_needed = f->n_tool_bar_items;
10704
10705 /* Reuse f->desired_tool_bar_string, if possible. */
10706 if (size < size_needed || NILP (f->desired_tool_bar_string))
10707 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10708 make_number (' '));
10709 else
10710 {
10711 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10712 Fremove_text_properties (make_number (0), make_number (size),
10713 props, f->desired_tool_bar_string);
10714 }
10715
10716 /* Put a `display' property on the string for the images to display,
10717 put a `menu_item' property on tool-bar items with a value that
10718 is the index of the item in F's tool-bar item vector. */
10719 for (i = 0; i < f->n_tool_bar_items; ++i)
10720 {
10721 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10722
10723 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10724 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10725 int hmargin, vmargin, relief, idx, end;
10726
10727 /* If image is a vector, choose the image according to the
10728 button state. */
10729 image = PROP (TOOL_BAR_ITEM_IMAGES);
10730 if (VECTORP (image))
10731 {
10732 if (enabled_p)
10733 idx = (selected_p
10734 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10735 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10736 else
10737 idx = (selected_p
10738 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10739 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10740
10741 xassert (ASIZE (image) >= idx);
10742 image = AREF (image, idx);
10743 }
10744 else
10745 idx = -1;
10746
10747 /* Ignore invalid image specifications. */
10748 if (!valid_image_p (image))
10749 continue;
10750
10751 /* Display the tool-bar button pressed, or depressed. */
10752 plist = Fcopy_sequence (XCDR (image));
10753
10754 /* Compute margin and relief to draw. */
10755 relief = (tool_bar_button_relief >= 0
10756 ? tool_bar_button_relief
10757 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10758 hmargin = vmargin = relief;
10759
10760 if (INTEGERP (Vtool_bar_button_margin)
10761 && XINT (Vtool_bar_button_margin) > 0)
10762 {
10763 hmargin += XFASTINT (Vtool_bar_button_margin);
10764 vmargin += XFASTINT (Vtool_bar_button_margin);
10765 }
10766 else if (CONSP (Vtool_bar_button_margin))
10767 {
10768 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10769 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10770 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10771
10772 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10773 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10774 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10775 }
10776
10777 if (auto_raise_tool_bar_buttons_p)
10778 {
10779 /* Add a `:relief' property to the image spec if the item is
10780 selected. */
10781 if (selected_p)
10782 {
10783 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10784 hmargin -= relief;
10785 vmargin -= relief;
10786 }
10787 }
10788 else
10789 {
10790 /* If image is selected, display it pressed, i.e. with a
10791 negative relief. If it's not selected, display it with a
10792 raised relief. */
10793 plist = Fplist_put (plist, QCrelief,
10794 (selected_p
10795 ? make_number (-relief)
10796 : make_number (relief)));
10797 hmargin -= relief;
10798 vmargin -= relief;
10799 }
10800
10801 /* Put a margin around the image. */
10802 if (hmargin || vmargin)
10803 {
10804 if (hmargin == vmargin)
10805 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10806 else
10807 plist = Fplist_put (plist, QCmargin,
10808 Fcons (make_number (hmargin),
10809 make_number (vmargin)));
10810 }
10811
10812 /* If button is not enabled, and we don't have special images
10813 for the disabled state, make the image appear disabled by
10814 applying an appropriate algorithm to it. */
10815 if (!enabled_p && idx < 0)
10816 plist = Fplist_put (plist, QCconversion, Qdisabled);
10817
10818 /* Put a `display' text property on the string for the image to
10819 display. Put a `menu-item' property on the string that gives
10820 the start of this item's properties in the tool-bar items
10821 vector. */
10822 image = Fcons (Qimage, plist);
10823 props = list4 (Qdisplay, image,
10824 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10825
10826 /* Let the last image hide all remaining spaces in the tool bar
10827 string. The string can be longer than needed when we reuse a
10828 previous string. */
10829 if (i + 1 == f->n_tool_bar_items)
10830 end = SCHARS (f->desired_tool_bar_string);
10831 else
10832 end = i + 1;
10833 Fadd_text_properties (make_number (i), make_number (end),
10834 props, f->desired_tool_bar_string);
10835 #undef PROP
10836 }
10837
10838 UNGCPRO;
10839 }
10840
10841
10842 /* Display one line of the tool-bar of frame IT->f.
10843
10844 HEIGHT specifies the desired height of the tool-bar line.
10845 If the actual height of the glyph row is less than HEIGHT, the
10846 row's height is increased to HEIGHT, and the icons are centered
10847 vertically in the new height.
10848
10849 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10850 count a final empty row in case the tool-bar width exactly matches
10851 the window width.
10852 */
10853
10854 static void
10855 display_tool_bar_line (struct it *it, int height)
10856 {
10857 struct glyph_row *row = it->glyph_row;
10858 int max_x = it->last_visible_x;
10859 struct glyph *last;
10860
10861 prepare_desired_row (row);
10862 row->y = it->current_y;
10863
10864 /* Note that this isn't made use of if the face hasn't a box,
10865 so there's no need to check the face here. */
10866 it->start_of_box_run_p = 1;
10867
10868 while (it->current_x < max_x)
10869 {
10870 int x, n_glyphs_before, i, nglyphs;
10871 struct it it_before;
10872
10873 /* Get the next display element. */
10874 if (!get_next_display_element (it))
10875 {
10876 /* Don't count empty row if we are counting needed tool-bar lines. */
10877 if (height < 0 && !it->hpos)
10878 return;
10879 break;
10880 }
10881
10882 /* Produce glyphs. */
10883 n_glyphs_before = row->used[TEXT_AREA];
10884 it_before = *it;
10885
10886 PRODUCE_GLYPHS (it);
10887
10888 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10889 i = 0;
10890 x = it_before.current_x;
10891 while (i < nglyphs)
10892 {
10893 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10894
10895 if (x + glyph->pixel_width > max_x)
10896 {
10897 /* Glyph doesn't fit on line. Backtrack. */
10898 row->used[TEXT_AREA] = n_glyphs_before;
10899 *it = it_before;
10900 /* If this is the only glyph on this line, it will never fit on the
10901 tool-bar, so skip it. But ensure there is at least one glyph,
10902 so we don't accidentally disable the tool-bar. */
10903 if (n_glyphs_before == 0
10904 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10905 break;
10906 goto out;
10907 }
10908
10909 ++it->hpos;
10910 x += glyph->pixel_width;
10911 ++i;
10912 }
10913
10914 /* Stop at line end. */
10915 if (ITERATOR_AT_END_OF_LINE_P (it))
10916 break;
10917
10918 set_iterator_to_next (it, 1);
10919 }
10920
10921 out:;
10922
10923 row->displays_text_p = row->used[TEXT_AREA] != 0;
10924
10925 /* Use default face for the border below the tool bar.
10926
10927 FIXME: When auto-resize-tool-bars is grow-only, there is
10928 no additional border below the possibly empty tool-bar lines.
10929 So to make the extra empty lines look "normal", we have to
10930 use the tool-bar face for the border too. */
10931 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10932 it->face_id = DEFAULT_FACE_ID;
10933
10934 extend_face_to_end_of_line (it);
10935 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10936 last->right_box_line_p = 1;
10937 if (last == row->glyphs[TEXT_AREA])
10938 last->left_box_line_p = 1;
10939
10940 /* Make line the desired height and center it vertically. */
10941 if ((height -= it->max_ascent + it->max_descent) > 0)
10942 {
10943 /* Don't add more than one line height. */
10944 height %= FRAME_LINE_HEIGHT (it->f);
10945 it->max_ascent += height / 2;
10946 it->max_descent += (height + 1) / 2;
10947 }
10948
10949 compute_line_metrics (it);
10950
10951 /* If line is empty, make it occupy the rest of the tool-bar. */
10952 if (!row->displays_text_p)
10953 {
10954 row->height = row->phys_height = it->last_visible_y - row->y;
10955 row->visible_height = row->height;
10956 row->ascent = row->phys_ascent = 0;
10957 row->extra_line_spacing = 0;
10958 }
10959
10960 row->full_width_p = 1;
10961 row->continued_p = 0;
10962 row->truncated_on_left_p = 0;
10963 row->truncated_on_right_p = 0;
10964
10965 it->current_x = it->hpos = 0;
10966 it->current_y += row->height;
10967 ++it->vpos;
10968 ++it->glyph_row;
10969 }
10970
10971
10972 /* Max tool-bar height. */
10973
10974 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10975 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10976
10977 /* Value is the number of screen lines needed to make all tool-bar
10978 items of frame F visible. The number of actual rows needed is
10979 returned in *N_ROWS if non-NULL. */
10980
10981 static int
10982 tool_bar_lines_needed (struct frame *f, int *n_rows)
10983 {
10984 struct window *w = XWINDOW (f->tool_bar_window);
10985 struct it it;
10986 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10987 the desired matrix, so use (unused) mode-line row as temporary row to
10988 avoid destroying the first tool-bar row. */
10989 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10990
10991 /* Initialize an iterator for iteration over
10992 F->desired_tool_bar_string in the tool-bar window of frame F. */
10993 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10994 it.first_visible_x = 0;
10995 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10996 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10997 it.paragraph_embedding = L2R;
10998
10999 while (!ITERATOR_AT_END_P (&it))
11000 {
11001 clear_glyph_row (temp_row);
11002 it.glyph_row = temp_row;
11003 display_tool_bar_line (&it, -1);
11004 }
11005 clear_glyph_row (temp_row);
11006
11007 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11008 if (n_rows)
11009 *n_rows = it.vpos > 0 ? it.vpos : -1;
11010
11011 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11012 }
11013
11014
11015 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11016 0, 1, 0,
11017 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11018 (Lisp_Object frame)
11019 {
11020 struct frame *f;
11021 struct window *w;
11022 int nlines = 0;
11023
11024 if (NILP (frame))
11025 frame = selected_frame;
11026 else
11027 CHECK_FRAME (frame);
11028 f = XFRAME (frame);
11029
11030 if (WINDOWP (f->tool_bar_window)
11031 || (w = XWINDOW (f->tool_bar_window),
11032 WINDOW_TOTAL_LINES (w) > 0))
11033 {
11034 update_tool_bar (f, 1);
11035 if (f->n_tool_bar_items)
11036 {
11037 build_desired_tool_bar_string (f);
11038 nlines = tool_bar_lines_needed (f, NULL);
11039 }
11040 }
11041
11042 return make_number (nlines);
11043 }
11044
11045
11046 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11047 height should be changed. */
11048
11049 static int
11050 redisplay_tool_bar (struct frame *f)
11051 {
11052 struct window *w;
11053 struct it it;
11054 struct glyph_row *row;
11055
11056 #if defined (USE_GTK) || defined (HAVE_NS)
11057 if (FRAME_EXTERNAL_TOOL_BAR (f))
11058 update_frame_tool_bar (f);
11059 return 0;
11060 #endif
11061
11062 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11063 do anything. This means you must start with tool-bar-lines
11064 non-zero to get the auto-sizing effect. Or in other words, you
11065 can turn off tool-bars by specifying tool-bar-lines zero. */
11066 if (!WINDOWP (f->tool_bar_window)
11067 || (w = XWINDOW (f->tool_bar_window),
11068 WINDOW_TOTAL_LINES (w) == 0))
11069 return 0;
11070
11071 /* Set up an iterator for the tool-bar window. */
11072 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11073 it.first_visible_x = 0;
11074 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11075 row = it.glyph_row;
11076
11077 /* Build a string that represents the contents of the tool-bar. */
11078 build_desired_tool_bar_string (f);
11079 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11080 /* FIXME: This should be controlled by a user option. But it
11081 doesn't make sense to have an R2L tool bar if the menu bar cannot
11082 be drawn also R2L, and making the menu bar R2L is tricky due to
11083 unibyte strings it uses and toolkit-specific code that implements
11084 it. If an R2L tool bar is ever supported, display_tool_bar_line
11085 should also be augmented to call unproduce_glyphs like
11086 display_line and display_string do. */
11087 it.paragraph_embedding = L2R;
11088
11089 if (f->n_tool_bar_rows == 0)
11090 {
11091 int nlines;
11092
11093 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11094 nlines != WINDOW_TOTAL_LINES (w)))
11095 {
11096 Lisp_Object frame;
11097 int old_height = WINDOW_TOTAL_LINES (w);
11098
11099 XSETFRAME (frame, f);
11100 Fmodify_frame_parameters (frame,
11101 Fcons (Fcons (Qtool_bar_lines,
11102 make_number (nlines)),
11103 Qnil));
11104 if (WINDOW_TOTAL_LINES (w) != old_height)
11105 {
11106 clear_glyph_matrix (w->desired_matrix);
11107 fonts_changed_p = 1;
11108 return 1;
11109 }
11110 }
11111 }
11112
11113 /* Display as many lines as needed to display all tool-bar items. */
11114
11115 if (f->n_tool_bar_rows > 0)
11116 {
11117 int border, rows, height, extra;
11118
11119 if (INTEGERP (Vtool_bar_border))
11120 border = XINT (Vtool_bar_border);
11121 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11122 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11123 else if (EQ (Vtool_bar_border, Qborder_width))
11124 border = f->border_width;
11125 else
11126 border = 0;
11127 if (border < 0)
11128 border = 0;
11129
11130 rows = f->n_tool_bar_rows;
11131 height = max (1, (it.last_visible_y - border) / rows);
11132 extra = it.last_visible_y - border - height * rows;
11133
11134 while (it.current_y < it.last_visible_y)
11135 {
11136 int h = 0;
11137 if (extra > 0 && rows-- > 0)
11138 {
11139 h = (extra + rows - 1) / rows;
11140 extra -= h;
11141 }
11142 display_tool_bar_line (&it, height + h);
11143 }
11144 }
11145 else
11146 {
11147 while (it.current_y < it.last_visible_y)
11148 display_tool_bar_line (&it, 0);
11149 }
11150
11151 /* It doesn't make much sense to try scrolling in the tool-bar
11152 window, so don't do it. */
11153 w->desired_matrix->no_scrolling_p = 1;
11154 w->must_be_updated_p = 1;
11155
11156 if (!NILP (Vauto_resize_tool_bars))
11157 {
11158 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11159 int change_height_p = 0;
11160
11161 /* If we couldn't display everything, change the tool-bar's
11162 height if there is room for more. */
11163 if (IT_STRING_CHARPOS (it) < it.end_charpos
11164 && it.current_y < max_tool_bar_height)
11165 change_height_p = 1;
11166
11167 row = it.glyph_row - 1;
11168
11169 /* If there are blank lines at the end, except for a partially
11170 visible blank line at the end that is smaller than
11171 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11172 if (!row->displays_text_p
11173 && row->height >= FRAME_LINE_HEIGHT (f))
11174 change_height_p = 1;
11175
11176 /* If row displays tool-bar items, but is partially visible,
11177 change the tool-bar's height. */
11178 if (row->displays_text_p
11179 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11180 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11181 change_height_p = 1;
11182
11183 /* Resize windows as needed by changing the `tool-bar-lines'
11184 frame parameter. */
11185 if (change_height_p)
11186 {
11187 Lisp_Object frame;
11188 int old_height = WINDOW_TOTAL_LINES (w);
11189 int nrows;
11190 int nlines = tool_bar_lines_needed (f, &nrows);
11191
11192 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11193 && !f->minimize_tool_bar_window_p)
11194 ? (nlines > old_height)
11195 : (nlines != old_height));
11196 f->minimize_tool_bar_window_p = 0;
11197
11198 if (change_height_p)
11199 {
11200 XSETFRAME (frame, f);
11201 Fmodify_frame_parameters (frame,
11202 Fcons (Fcons (Qtool_bar_lines,
11203 make_number (nlines)),
11204 Qnil));
11205 if (WINDOW_TOTAL_LINES (w) != old_height)
11206 {
11207 clear_glyph_matrix (w->desired_matrix);
11208 f->n_tool_bar_rows = nrows;
11209 fonts_changed_p = 1;
11210 return 1;
11211 }
11212 }
11213 }
11214 }
11215
11216 f->minimize_tool_bar_window_p = 0;
11217 return 0;
11218 }
11219
11220
11221 /* Get information about the tool-bar item which is displayed in GLYPH
11222 on frame F. Return in *PROP_IDX the index where tool-bar item
11223 properties start in F->tool_bar_items. Value is zero if
11224 GLYPH doesn't display a tool-bar item. */
11225
11226 static int
11227 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11228 {
11229 Lisp_Object prop;
11230 int success_p;
11231 int charpos;
11232
11233 /* This function can be called asynchronously, which means we must
11234 exclude any possibility that Fget_text_property signals an
11235 error. */
11236 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11237 charpos = max (0, charpos);
11238
11239 /* Get the text property `menu-item' at pos. The value of that
11240 property is the start index of this item's properties in
11241 F->tool_bar_items. */
11242 prop = Fget_text_property (make_number (charpos),
11243 Qmenu_item, f->current_tool_bar_string);
11244 if (INTEGERP (prop))
11245 {
11246 *prop_idx = XINT (prop);
11247 success_p = 1;
11248 }
11249 else
11250 success_p = 0;
11251
11252 return success_p;
11253 }
11254
11255 \f
11256 /* Get information about the tool-bar item at position X/Y on frame F.
11257 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11258 the current matrix of the tool-bar window of F, or NULL if not
11259 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11260 item in F->tool_bar_items. Value is
11261
11262 -1 if X/Y is not on a tool-bar item
11263 0 if X/Y is on the same item that was highlighted before.
11264 1 otherwise. */
11265
11266 static int
11267 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11268 int *hpos, int *vpos, int *prop_idx)
11269 {
11270 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11271 struct window *w = XWINDOW (f->tool_bar_window);
11272 int area;
11273
11274 /* Find the glyph under X/Y. */
11275 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11276 if (*glyph == NULL)
11277 return -1;
11278
11279 /* Get the start of this tool-bar item's properties in
11280 f->tool_bar_items. */
11281 if (!tool_bar_item_info (f, *glyph, prop_idx))
11282 return -1;
11283
11284 /* Is mouse on the highlighted item? */
11285 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11286 && *vpos >= hlinfo->mouse_face_beg_row
11287 && *vpos <= hlinfo->mouse_face_end_row
11288 && (*vpos > hlinfo->mouse_face_beg_row
11289 || *hpos >= hlinfo->mouse_face_beg_col)
11290 && (*vpos < hlinfo->mouse_face_end_row
11291 || *hpos < hlinfo->mouse_face_end_col
11292 || hlinfo->mouse_face_past_end))
11293 return 0;
11294
11295 return 1;
11296 }
11297
11298
11299 /* EXPORT:
11300 Handle mouse button event on the tool-bar of frame F, at
11301 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11302 0 for button release. MODIFIERS is event modifiers for button
11303 release. */
11304
11305 void
11306 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11307 unsigned int modifiers)
11308 {
11309 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11310 struct window *w = XWINDOW (f->tool_bar_window);
11311 int hpos, vpos, prop_idx;
11312 struct glyph *glyph;
11313 Lisp_Object enabled_p;
11314
11315 /* If not on the highlighted tool-bar item, return. */
11316 frame_to_window_pixel_xy (w, &x, &y);
11317 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11318 return;
11319
11320 /* If item is disabled, do nothing. */
11321 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11322 if (NILP (enabled_p))
11323 return;
11324
11325 if (down_p)
11326 {
11327 /* Show item in pressed state. */
11328 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11329 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11330 last_tool_bar_item = prop_idx;
11331 }
11332 else
11333 {
11334 Lisp_Object key, frame;
11335 struct input_event event;
11336 EVENT_INIT (event);
11337
11338 /* Show item in released state. */
11339 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11340 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11341
11342 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11343
11344 XSETFRAME (frame, f);
11345 event.kind = TOOL_BAR_EVENT;
11346 event.frame_or_window = frame;
11347 event.arg = frame;
11348 kbd_buffer_store_event (&event);
11349
11350 event.kind = TOOL_BAR_EVENT;
11351 event.frame_or_window = frame;
11352 event.arg = key;
11353 event.modifiers = modifiers;
11354 kbd_buffer_store_event (&event);
11355 last_tool_bar_item = -1;
11356 }
11357 }
11358
11359
11360 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11361 tool-bar window-relative coordinates X/Y. Called from
11362 note_mouse_highlight. */
11363
11364 static void
11365 note_tool_bar_highlight (struct frame *f, int x, int y)
11366 {
11367 Lisp_Object window = f->tool_bar_window;
11368 struct window *w = XWINDOW (window);
11369 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11370 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11371 int hpos, vpos;
11372 struct glyph *glyph;
11373 struct glyph_row *row;
11374 int i;
11375 Lisp_Object enabled_p;
11376 int prop_idx;
11377 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11378 int mouse_down_p, rc;
11379
11380 /* Function note_mouse_highlight is called with negative X/Y
11381 values when mouse moves outside of the frame. */
11382 if (x <= 0 || y <= 0)
11383 {
11384 clear_mouse_face (hlinfo);
11385 return;
11386 }
11387
11388 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11389 if (rc < 0)
11390 {
11391 /* Not on tool-bar item. */
11392 clear_mouse_face (hlinfo);
11393 return;
11394 }
11395 else if (rc == 0)
11396 /* On same tool-bar item as before. */
11397 goto set_help_echo;
11398
11399 clear_mouse_face (hlinfo);
11400
11401 /* Mouse is down, but on different tool-bar item? */
11402 mouse_down_p = (dpyinfo->grabbed
11403 && f == last_mouse_frame
11404 && FRAME_LIVE_P (f));
11405 if (mouse_down_p
11406 && last_tool_bar_item != prop_idx)
11407 return;
11408
11409 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11410 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11411
11412 /* If tool-bar item is not enabled, don't highlight it. */
11413 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11414 if (!NILP (enabled_p))
11415 {
11416 /* Compute the x-position of the glyph. In front and past the
11417 image is a space. We include this in the highlighted area. */
11418 row = MATRIX_ROW (w->current_matrix, vpos);
11419 for (i = x = 0; i < hpos; ++i)
11420 x += row->glyphs[TEXT_AREA][i].pixel_width;
11421
11422 /* Record this as the current active region. */
11423 hlinfo->mouse_face_beg_col = hpos;
11424 hlinfo->mouse_face_beg_row = vpos;
11425 hlinfo->mouse_face_beg_x = x;
11426 hlinfo->mouse_face_beg_y = row->y;
11427 hlinfo->mouse_face_past_end = 0;
11428
11429 hlinfo->mouse_face_end_col = hpos + 1;
11430 hlinfo->mouse_face_end_row = vpos;
11431 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11432 hlinfo->mouse_face_end_y = row->y;
11433 hlinfo->mouse_face_window = window;
11434 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11435
11436 /* Display it as active. */
11437 show_mouse_face (hlinfo, draw);
11438 hlinfo->mouse_face_image_state = draw;
11439 }
11440
11441 set_help_echo:
11442
11443 /* Set help_echo_string to a help string to display for this tool-bar item.
11444 XTread_socket does the rest. */
11445 help_echo_object = help_echo_window = Qnil;
11446 help_echo_pos = -1;
11447 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11448 if (NILP (help_echo_string))
11449 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11450 }
11451
11452 #endif /* HAVE_WINDOW_SYSTEM */
11453
11454
11455 \f
11456 /************************************************************************
11457 Horizontal scrolling
11458 ************************************************************************/
11459
11460 static int hscroll_window_tree (Lisp_Object);
11461 static int hscroll_windows (Lisp_Object);
11462
11463 /* For all leaf windows in the window tree rooted at WINDOW, set their
11464 hscroll value so that PT is (i) visible in the window, and (ii) so
11465 that it is not within a certain margin at the window's left and
11466 right border. Value is non-zero if any window's hscroll has been
11467 changed. */
11468
11469 static int
11470 hscroll_window_tree (Lisp_Object window)
11471 {
11472 int hscrolled_p = 0;
11473 int hscroll_relative_p = FLOATP (Vhscroll_step);
11474 int hscroll_step_abs = 0;
11475 double hscroll_step_rel = 0;
11476
11477 if (hscroll_relative_p)
11478 {
11479 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11480 if (hscroll_step_rel < 0)
11481 {
11482 hscroll_relative_p = 0;
11483 hscroll_step_abs = 0;
11484 }
11485 }
11486 else if (INTEGERP (Vhscroll_step))
11487 {
11488 hscroll_step_abs = XINT (Vhscroll_step);
11489 if (hscroll_step_abs < 0)
11490 hscroll_step_abs = 0;
11491 }
11492 else
11493 hscroll_step_abs = 0;
11494
11495 while (WINDOWP (window))
11496 {
11497 struct window *w = XWINDOW (window);
11498
11499 if (WINDOWP (w->hchild))
11500 hscrolled_p |= hscroll_window_tree (w->hchild);
11501 else if (WINDOWP (w->vchild))
11502 hscrolled_p |= hscroll_window_tree (w->vchild);
11503 else if (w->cursor.vpos >= 0)
11504 {
11505 int h_margin;
11506 int text_area_width;
11507 struct glyph_row *current_cursor_row
11508 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11509 struct glyph_row *desired_cursor_row
11510 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11511 struct glyph_row *cursor_row
11512 = (desired_cursor_row->enabled_p
11513 ? desired_cursor_row
11514 : current_cursor_row);
11515
11516 text_area_width = window_box_width (w, TEXT_AREA);
11517
11518 /* Scroll when cursor is inside this scroll margin. */
11519 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11520
11521 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11522 && ((XFASTINT (w->hscroll)
11523 && w->cursor.x <= h_margin)
11524 || (cursor_row->enabled_p
11525 && cursor_row->truncated_on_right_p
11526 && (w->cursor.x >= text_area_width - h_margin))))
11527 {
11528 struct it it;
11529 int hscroll;
11530 struct buffer *saved_current_buffer;
11531 EMACS_INT pt;
11532 int wanted_x;
11533
11534 /* Find point in a display of infinite width. */
11535 saved_current_buffer = current_buffer;
11536 current_buffer = XBUFFER (w->buffer);
11537
11538 if (w == XWINDOW (selected_window))
11539 pt = PT;
11540 else
11541 {
11542 pt = marker_position (w->pointm);
11543 pt = max (BEGV, pt);
11544 pt = min (ZV, pt);
11545 }
11546
11547 /* Move iterator to pt starting at cursor_row->start in
11548 a line with infinite width. */
11549 init_to_row_start (&it, w, cursor_row);
11550 it.last_visible_x = INFINITY;
11551 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11552 current_buffer = saved_current_buffer;
11553
11554 /* Position cursor in window. */
11555 if (!hscroll_relative_p && hscroll_step_abs == 0)
11556 hscroll = max (0, (it.current_x
11557 - (ITERATOR_AT_END_OF_LINE_P (&it)
11558 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11559 : (text_area_width / 2))))
11560 / FRAME_COLUMN_WIDTH (it.f);
11561 else if (w->cursor.x >= text_area_width - h_margin)
11562 {
11563 if (hscroll_relative_p)
11564 wanted_x = text_area_width * (1 - hscroll_step_rel)
11565 - h_margin;
11566 else
11567 wanted_x = text_area_width
11568 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11569 - h_margin;
11570 hscroll
11571 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11572 }
11573 else
11574 {
11575 if (hscroll_relative_p)
11576 wanted_x = text_area_width * hscroll_step_rel
11577 + h_margin;
11578 else
11579 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11580 + h_margin;
11581 hscroll
11582 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11583 }
11584 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11585
11586 /* Don't call Fset_window_hscroll if value hasn't
11587 changed because it will prevent redisplay
11588 optimizations. */
11589 if (XFASTINT (w->hscroll) != hscroll)
11590 {
11591 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11592 w->hscroll = make_number (hscroll);
11593 hscrolled_p = 1;
11594 }
11595 }
11596 }
11597
11598 window = w->next;
11599 }
11600
11601 /* Value is non-zero if hscroll of any leaf window has been changed. */
11602 return hscrolled_p;
11603 }
11604
11605
11606 /* Set hscroll so that cursor is visible and not inside horizontal
11607 scroll margins for all windows in the tree rooted at WINDOW. See
11608 also hscroll_window_tree above. Value is non-zero if any window's
11609 hscroll has been changed. If it has, desired matrices on the frame
11610 of WINDOW are cleared. */
11611
11612 static int
11613 hscroll_windows (Lisp_Object window)
11614 {
11615 int hscrolled_p = hscroll_window_tree (window);
11616 if (hscrolled_p)
11617 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11618 return hscrolled_p;
11619 }
11620
11621
11622 \f
11623 /************************************************************************
11624 Redisplay
11625 ************************************************************************/
11626
11627 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11628 to a non-zero value. This is sometimes handy to have in a debugger
11629 session. */
11630
11631 #if GLYPH_DEBUG
11632
11633 /* First and last unchanged row for try_window_id. */
11634
11635 int debug_first_unchanged_at_end_vpos;
11636 int debug_last_unchanged_at_beg_vpos;
11637
11638 /* Delta vpos and y. */
11639
11640 int debug_dvpos, debug_dy;
11641
11642 /* Delta in characters and bytes for try_window_id. */
11643
11644 EMACS_INT debug_delta, debug_delta_bytes;
11645
11646 /* Values of window_end_pos and window_end_vpos at the end of
11647 try_window_id. */
11648
11649 EMACS_INT debug_end_vpos;
11650
11651 /* Append a string to W->desired_matrix->method. FMT is a printf
11652 format string. A1...A9 are a supplement for a variable-length
11653 argument list. If trace_redisplay_p is non-zero also printf the
11654 resulting string to stderr. */
11655
11656 static void
11657 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11658 struct window *w;
11659 char *fmt;
11660 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11661 {
11662 char buffer[512];
11663 char *method = w->desired_matrix->method;
11664 int len = strlen (method);
11665 int size = sizeof w->desired_matrix->method;
11666 int remaining = size - len - 1;
11667
11668 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11669 if (len && remaining)
11670 {
11671 method[len] = '|';
11672 --remaining, ++len;
11673 }
11674
11675 strncpy (method + len, buffer, remaining);
11676
11677 if (trace_redisplay_p)
11678 fprintf (stderr, "%p (%s): %s\n",
11679 w,
11680 ((BUFFERP (w->buffer)
11681 && STRINGP (XBUFFER (w->buffer)->name))
11682 ? SSDATA (XBUFFER (w->buffer)->name)
11683 : "no buffer"),
11684 buffer);
11685 }
11686
11687 #endif /* GLYPH_DEBUG */
11688
11689
11690 /* Value is non-zero if all changes in window W, which displays
11691 current_buffer, are in the text between START and END. START is a
11692 buffer position, END is given as a distance from Z. Used in
11693 redisplay_internal for display optimization. */
11694
11695 static INLINE int
11696 text_outside_line_unchanged_p (struct window *w,
11697 EMACS_INT start, EMACS_INT end)
11698 {
11699 int unchanged_p = 1;
11700
11701 /* If text or overlays have changed, see where. */
11702 if (XFASTINT (w->last_modified) < MODIFF
11703 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11704 {
11705 /* Gap in the line? */
11706 if (GPT < start || Z - GPT < end)
11707 unchanged_p = 0;
11708
11709 /* Changes start in front of the line, or end after it? */
11710 if (unchanged_p
11711 && (BEG_UNCHANGED < start - 1
11712 || END_UNCHANGED < end))
11713 unchanged_p = 0;
11714
11715 /* If selective display, can't optimize if changes start at the
11716 beginning of the line. */
11717 if (unchanged_p
11718 && INTEGERP (BVAR (current_buffer, selective_display))
11719 && XINT (BVAR (current_buffer, selective_display)) > 0
11720 && (BEG_UNCHANGED < start || GPT <= start))
11721 unchanged_p = 0;
11722
11723 /* If there are overlays at the start or end of the line, these
11724 may have overlay strings with newlines in them. A change at
11725 START, for instance, may actually concern the display of such
11726 overlay strings as well, and they are displayed on different
11727 lines. So, quickly rule out this case. (For the future, it
11728 might be desirable to implement something more telling than
11729 just BEG/END_UNCHANGED.) */
11730 if (unchanged_p)
11731 {
11732 if (BEG + BEG_UNCHANGED == start
11733 && overlay_touches_p (start))
11734 unchanged_p = 0;
11735 if (END_UNCHANGED == end
11736 && overlay_touches_p (Z - end))
11737 unchanged_p = 0;
11738 }
11739
11740 /* Under bidi reordering, adding or deleting a character in the
11741 beginning of a paragraph, before the first strong directional
11742 character, can change the base direction of the paragraph (unless
11743 the buffer specifies a fixed paragraph direction), which will
11744 require to redisplay the whole paragraph. It might be worthwhile
11745 to find the paragraph limits and widen the range of redisplayed
11746 lines to that, but for now just give up this optimization. */
11747 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11748 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11749 unchanged_p = 0;
11750 }
11751
11752 return unchanged_p;
11753 }
11754
11755
11756 /* Do a frame update, taking possible shortcuts into account. This is
11757 the main external entry point for redisplay.
11758
11759 If the last redisplay displayed an echo area message and that message
11760 is no longer requested, we clear the echo area or bring back the
11761 mini-buffer if that is in use. */
11762
11763 void
11764 redisplay (void)
11765 {
11766 redisplay_internal ();
11767 }
11768
11769
11770 static Lisp_Object
11771 overlay_arrow_string_or_property (Lisp_Object var)
11772 {
11773 Lisp_Object val;
11774
11775 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11776 return val;
11777
11778 return Voverlay_arrow_string;
11779 }
11780
11781 /* Return 1 if there are any overlay-arrows in current_buffer. */
11782 static int
11783 overlay_arrow_in_current_buffer_p (void)
11784 {
11785 Lisp_Object vlist;
11786
11787 for (vlist = Voverlay_arrow_variable_list;
11788 CONSP (vlist);
11789 vlist = XCDR (vlist))
11790 {
11791 Lisp_Object var = XCAR (vlist);
11792 Lisp_Object val;
11793
11794 if (!SYMBOLP (var))
11795 continue;
11796 val = find_symbol_value (var);
11797 if (MARKERP (val)
11798 && current_buffer == XMARKER (val)->buffer)
11799 return 1;
11800 }
11801 return 0;
11802 }
11803
11804
11805 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11806 has changed. */
11807
11808 static int
11809 overlay_arrows_changed_p (void)
11810 {
11811 Lisp_Object vlist;
11812
11813 for (vlist = Voverlay_arrow_variable_list;
11814 CONSP (vlist);
11815 vlist = XCDR (vlist))
11816 {
11817 Lisp_Object var = XCAR (vlist);
11818 Lisp_Object val, pstr;
11819
11820 if (!SYMBOLP (var))
11821 continue;
11822 val = find_symbol_value (var);
11823 if (!MARKERP (val))
11824 continue;
11825 if (! EQ (COERCE_MARKER (val),
11826 Fget (var, Qlast_arrow_position))
11827 || ! (pstr = overlay_arrow_string_or_property (var),
11828 EQ (pstr, Fget (var, Qlast_arrow_string))))
11829 return 1;
11830 }
11831 return 0;
11832 }
11833
11834 /* Mark overlay arrows to be updated on next redisplay. */
11835
11836 static void
11837 update_overlay_arrows (int up_to_date)
11838 {
11839 Lisp_Object vlist;
11840
11841 for (vlist = Voverlay_arrow_variable_list;
11842 CONSP (vlist);
11843 vlist = XCDR (vlist))
11844 {
11845 Lisp_Object var = XCAR (vlist);
11846
11847 if (!SYMBOLP (var))
11848 continue;
11849
11850 if (up_to_date > 0)
11851 {
11852 Lisp_Object val = find_symbol_value (var);
11853 Fput (var, Qlast_arrow_position,
11854 COERCE_MARKER (val));
11855 Fput (var, Qlast_arrow_string,
11856 overlay_arrow_string_or_property (var));
11857 }
11858 else if (up_to_date < 0
11859 || !NILP (Fget (var, Qlast_arrow_position)))
11860 {
11861 Fput (var, Qlast_arrow_position, Qt);
11862 Fput (var, Qlast_arrow_string, Qt);
11863 }
11864 }
11865 }
11866
11867
11868 /* Return overlay arrow string to display at row.
11869 Return integer (bitmap number) for arrow bitmap in left fringe.
11870 Return nil if no overlay arrow. */
11871
11872 static Lisp_Object
11873 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11874 {
11875 Lisp_Object vlist;
11876
11877 for (vlist = Voverlay_arrow_variable_list;
11878 CONSP (vlist);
11879 vlist = XCDR (vlist))
11880 {
11881 Lisp_Object var = XCAR (vlist);
11882 Lisp_Object val;
11883
11884 if (!SYMBOLP (var))
11885 continue;
11886
11887 val = find_symbol_value (var);
11888
11889 if (MARKERP (val)
11890 && current_buffer == XMARKER (val)->buffer
11891 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11892 {
11893 if (FRAME_WINDOW_P (it->f)
11894 /* FIXME: if ROW->reversed_p is set, this should test
11895 the right fringe, not the left one. */
11896 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11897 {
11898 #ifdef HAVE_WINDOW_SYSTEM
11899 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11900 {
11901 int fringe_bitmap;
11902 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11903 return make_number (fringe_bitmap);
11904 }
11905 #endif
11906 return make_number (-1); /* Use default arrow bitmap */
11907 }
11908 return overlay_arrow_string_or_property (var);
11909 }
11910 }
11911
11912 return Qnil;
11913 }
11914
11915 /* Return 1 if point moved out of or into a composition. Otherwise
11916 return 0. PREV_BUF and PREV_PT are the last point buffer and
11917 position. BUF and PT are the current point buffer and position. */
11918
11919 static int
11920 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11921 struct buffer *buf, EMACS_INT pt)
11922 {
11923 EMACS_INT start, end;
11924 Lisp_Object prop;
11925 Lisp_Object buffer;
11926
11927 XSETBUFFER (buffer, buf);
11928 /* Check a composition at the last point if point moved within the
11929 same buffer. */
11930 if (prev_buf == buf)
11931 {
11932 if (prev_pt == pt)
11933 /* Point didn't move. */
11934 return 0;
11935
11936 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11937 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11938 && COMPOSITION_VALID_P (start, end, prop)
11939 && start < prev_pt && end > prev_pt)
11940 /* The last point was within the composition. Return 1 iff
11941 point moved out of the composition. */
11942 return (pt <= start || pt >= end);
11943 }
11944
11945 /* Check a composition at the current point. */
11946 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11947 && find_composition (pt, -1, &start, &end, &prop, buffer)
11948 && COMPOSITION_VALID_P (start, end, prop)
11949 && start < pt && end > pt);
11950 }
11951
11952
11953 /* Reconsider the setting of B->clip_changed which is displayed
11954 in window W. */
11955
11956 static INLINE void
11957 reconsider_clip_changes (struct window *w, struct buffer *b)
11958 {
11959 if (b->clip_changed
11960 && !NILP (w->window_end_valid)
11961 && w->current_matrix->buffer == b
11962 && w->current_matrix->zv == BUF_ZV (b)
11963 && w->current_matrix->begv == BUF_BEGV (b))
11964 b->clip_changed = 0;
11965
11966 /* If display wasn't paused, and W is not a tool bar window, see if
11967 point has been moved into or out of a composition. In that case,
11968 we set b->clip_changed to 1 to force updating the screen. If
11969 b->clip_changed has already been set to 1, we can skip this
11970 check. */
11971 if (!b->clip_changed
11972 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11973 {
11974 EMACS_INT pt;
11975
11976 if (w == XWINDOW (selected_window))
11977 pt = PT;
11978 else
11979 pt = marker_position (w->pointm);
11980
11981 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11982 || pt != XINT (w->last_point))
11983 && check_point_in_composition (w->current_matrix->buffer,
11984 XINT (w->last_point),
11985 XBUFFER (w->buffer), pt))
11986 b->clip_changed = 1;
11987 }
11988 }
11989 \f
11990
11991 /* Select FRAME to forward the values of frame-local variables into C
11992 variables so that the redisplay routines can access those values
11993 directly. */
11994
11995 static void
11996 select_frame_for_redisplay (Lisp_Object frame)
11997 {
11998 Lisp_Object tail, tem;
11999 Lisp_Object old = selected_frame;
12000 struct Lisp_Symbol *sym;
12001
12002 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12003
12004 selected_frame = frame;
12005
12006 do {
12007 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12008 if (CONSP (XCAR (tail))
12009 && (tem = XCAR (XCAR (tail)),
12010 SYMBOLP (tem))
12011 && (sym = indirect_variable (XSYMBOL (tem)),
12012 sym->redirect == SYMBOL_LOCALIZED)
12013 && sym->val.blv->frame_local)
12014 /* Use find_symbol_value rather than Fsymbol_value
12015 to avoid an error if it is void. */
12016 find_symbol_value (tem);
12017 } while (!EQ (frame, old) && (frame = old, 1));
12018 }
12019
12020
12021 #define STOP_POLLING \
12022 do { if (! polling_stopped_here) stop_polling (); \
12023 polling_stopped_here = 1; } while (0)
12024
12025 #define RESUME_POLLING \
12026 do { if (polling_stopped_here) start_polling (); \
12027 polling_stopped_here = 0; } while (0)
12028
12029
12030 /* Perhaps in the future avoid recentering windows if it
12031 is not necessary; currently that causes some problems. */
12032
12033 static void
12034 redisplay_internal (void)
12035 {
12036 struct window *w = XWINDOW (selected_window);
12037 struct window *sw;
12038 struct frame *fr;
12039 int pending;
12040 int must_finish = 0;
12041 struct text_pos tlbufpos, tlendpos;
12042 int number_of_visible_frames;
12043 int count, count1;
12044 struct frame *sf;
12045 int polling_stopped_here = 0;
12046 Lisp_Object old_frame = selected_frame;
12047
12048 /* Non-zero means redisplay has to consider all windows on all
12049 frames. Zero means, only selected_window is considered. */
12050 int consider_all_windows_p;
12051
12052 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12053
12054 /* No redisplay if running in batch mode or frame is not yet fully
12055 initialized, or redisplay is explicitly turned off by setting
12056 Vinhibit_redisplay. */
12057 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12058 || !NILP (Vinhibit_redisplay))
12059 return;
12060
12061 /* Don't examine these until after testing Vinhibit_redisplay.
12062 When Emacs is shutting down, perhaps because its connection to
12063 X has dropped, we should not look at them at all. */
12064 fr = XFRAME (w->frame);
12065 sf = SELECTED_FRAME ();
12066
12067 if (!fr->glyphs_initialized_p)
12068 return;
12069
12070 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12071 if (popup_activated ())
12072 return;
12073 #endif
12074
12075 /* I don't think this happens but let's be paranoid. */
12076 if (redisplaying_p)
12077 return;
12078
12079 /* Record a function that resets redisplaying_p to its old value
12080 when we leave this function. */
12081 count = SPECPDL_INDEX ();
12082 record_unwind_protect (unwind_redisplay,
12083 Fcons (make_number (redisplaying_p), selected_frame));
12084 ++redisplaying_p;
12085 specbind (Qinhibit_free_realized_faces, Qnil);
12086
12087 {
12088 Lisp_Object tail, frame;
12089
12090 FOR_EACH_FRAME (tail, frame)
12091 {
12092 struct frame *f = XFRAME (frame);
12093 f->already_hscrolled_p = 0;
12094 }
12095 }
12096
12097 retry:
12098 /* Remember the currently selected window. */
12099 sw = w;
12100
12101 if (!EQ (old_frame, selected_frame)
12102 && FRAME_LIVE_P (XFRAME (old_frame)))
12103 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12104 selected_frame and selected_window to be temporarily out-of-sync so
12105 when we come back here via `goto retry', we need to resync because we
12106 may need to run Elisp code (via prepare_menu_bars). */
12107 select_frame_for_redisplay (old_frame);
12108
12109 pending = 0;
12110 reconsider_clip_changes (w, current_buffer);
12111 last_escape_glyph_frame = NULL;
12112 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12113 last_glyphless_glyph_frame = NULL;
12114 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12115
12116 /* If new fonts have been loaded that make a glyph matrix adjustment
12117 necessary, do it. */
12118 if (fonts_changed_p)
12119 {
12120 adjust_glyphs (NULL);
12121 ++windows_or_buffers_changed;
12122 fonts_changed_p = 0;
12123 }
12124
12125 /* If face_change_count is non-zero, init_iterator will free all
12126 realized faces, which includes the faces referenced from current
12127 matrices. So, we can't reuse current matrices in this case. */
12128 if (face_change_count)
12129 ++windows_or_buffers_changed;
12130
12131 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12132 && FRAME_TTY (sf)->previous_frame != sf)
12133 {
12134 /* Since frames on a single ASCII terminal share the same
12135 display area, displaying a different frame means redisplay
12136 the whole thing. */
12137 windows_or_buffers_changed++;
12138 SET_FRAME_GARBAGED (sf);
12139 #ifndef DOS_NT
12140 set_tty_color_mode (FRAME_TTY (sf), sf);
12141 #endif
12142 FRAME_TTY (sf)->previous_frame = sf;
12143 }
12144
12145 /* Set the visible flags for all frames. Do this before checking
12146 for resized or garbaged frames; they want to know if their frames
12147 are visible. See the comment in frame.h for
12148 FRAME_SAMPLE_VISIBILITY. */
12149 {
12150 Lisp_Object tail, frame;
12151
12152 number_of_visible_frames = 0;
12153
12154 FOR_EACH_FRAME (tail, frame)
12155 {
12156 struct frame *f = XFRAME (frame);
12157
12158 FRAME_SAMPLE_VISIBILITY (f);
12159 if (FRAME_VISIBLE_P (f))
12160 ++number_of_visible_frames;
12161 clear_desired_matrices (f);
12162 }
12163 }
12164
12165 /* Notice any pending interrupt request to change frame size. */
12166 do_pending_window_change (1);
12167
12168 /* do_pending_window_change could change the selected_window due to
12169 frame resizing which makes the selected window too small. */
12170 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12171 {
12172 sw = w;
12173 reconsider_clip_changes (w, current_buffer);
12174 }
12175
12176 /* Clear frames marked as garbaged. */
12177 if (frame_garbaged)
12178 clear_garbaged_frames ();
12179
12180 /* Build menubar and tool-bar items. */
12181 if (NILP (Vmemory_full))
12182 prepare_menu_bars ();
12183
12184 if (windows_or_buffers_changed)
12185 update_mode_lines++;
12186
12187 /* Detect case that we need to write or remove a star in the mode line. */
12188 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12189 {
12190 w->update_mode_line = Qt;
12191 if (buffer_shared > 1)
12192 update_mode_lines++;
12193 }
12194
12195 /* Avoid invocation of point motion hooks by `current_column' below. */
12196 count1 = SPECPDL_INDEX ();
12197 specbind (Qinhibit_point_motion_hooks, Qt);
12198
12199 /* If %c is in the mode line, update it if needed. */
12200 if (!NILP (w->column_number_displayed)
12201 /* This alternative quickly identifies a common case
12202 where no change is needed. */
12203 && !(PT == XFASTINT (w->last_point)
12204 && XFASTINT (w->last_modified) >= MODIFF
12205 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12206 && (XFASTINT (w->column_number_displayed) != current_column ()))
12207 w->update_mode_line = Qt;
12208
12209 unbind_to (count1, Qnil);
12210
12211 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12212
12213 /* The variable buffer_shared is set in redisplay_window and
12214 indicates that we redisplay a buffer in different windows. See
12215 there. */
12216 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12217 || cursor_type_changed);
12218
12219 /* If specs for an arrow have changed, do thorough redisplay
12220 to ensure we remove any arrow that should no longer exist. */
12221 if (overlay_arrows_changed_p ())
12222 consider_all_windows_p = windows_or_buffers_changed = 1;
12223
12224 /* Normally the message* functions will have already displayed and
12225 updated the echo area, but the frame may have been trashed, or
12226 the update may have been preempted, so display the echo area
12227 again here. Checking message_cleared_p captures the case that
12228 the echo area should be cleared. */
12229 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12230 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12231 || (message_cleared_p
12232 && minibuf_level == 0
12233 /* If the mini-window is currently selected, this means the
12234 echo-area doesn't show through. */
12235 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12236 {
12237 int window_height_changed_p = echo_area_display (0);
12238 must_finish = 1;
12239
12240 /* If we don't display the current message, don't clear the
12241 message_cleared_p flag, because, if we did, we wouldn't clear
12242 the echo area in the next redisplay which doesn't preserve
12243 the echo area. */
12244 if (!display_last_displayed_message_p)
12245 message_cleared_p = 0;
12246
12247 if (fonts_changed_p)
12248 goto retry;
12249 else if (window_height_changed_p)
12250 {
12251 consider_all_windows_p = 1;
12252 ++update_mode_lines;
12253 ++windows_or_buffers_changed;
12254
12255 /* If window configuration was changed, frames may have been
12256 marked garbaged. Clear them or we will experience
12257 surprises wrt scrolling. */
12258 if (frame_garbaged)
12259 clear_garbaged_frames ();
12260 }
12261 }
12262 else if (EQ (selected_window, minibuf_window)
12263 && (current_buffer->clip_changed
12264 || XFASTINT (w->last_modified) < MODIFF
12265 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12266 && resize_mini_window (w, 0))
12267 {
12268 /* Resized active mini-window to fit the size of what it is
12269 showing if its contents might have changed. */
12270 must_finish = 1;
12271 /* FIXME: this causes all frames to be updated, which seems unnecessary
12272 since only the current frame needs to be considered. This function needs
12273 to be rewritten with two variables, consider_all_windows and
12274 consider_all_frames. */
12275 consider_all_windows_p = 1;
12276 ++windows_or_buffers_changed;
12277 ++update_mode_lines;
12278
12279 /* If window configuration was changed, frames may have been
12280 marked garbaged. Clear them or we will experience
12281 surprises wrt scrolling. */
12282 if (frame_garbaged)
12283 clear_garbaged_frames ();
12284 }
12285
12286
12287 /* If showing the region, and mark has changed, we must redisplay
12288 the whole window. The assignment to this_line_start_pos prevents
12289 the optimization directly below this if-statement. */
12290 if (((!NILP (Vtransient_mark_mode)
12291 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12292 != !NILP (w->region_showing))
12293 || (!NILP (w->region_showing)
12294 && !EQ (w->region_showing,
12295 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12296 CHARPOS (this_line_start_pos) = 0;
12297
12298 /* Optimize the case that only the line containing the cursor in the
12299 selected window has changed. Variables starting with this_ are
12300 set in display_line and record information about the line
12301 containing the cursor. */
12302 tlbufpos = this_line_start_pos;
12303 tlendpos = this_line_end_pos;
12304 if (!consider_all_windows_p
12305 && CHARPOS (tlbufpos) > 0
12306 && NILP (w->update_mode_line)
12307 && !current_buffer->clip_changed
12308 && !current_buffer->prevent_redisplay_optimizations_p
12309 && FRAME_VISIBLE_P (XFRAME (w->frame))
12310 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12311 /* Make sure recorded data applies to current buffer, etc. */
12312 && this_line_buffer == current_buffer
12313 && current_buffer == XBUFFER (w->buffer)
12314 && NILP (w->force_start)
12315 && NILP (w->optional_new_start)
12316 /* Point must be on the line that we have info recorded about. */
12317 && PT >= CHARPOS (tlbufpos)
12318 && PT <= Z - CHARPOS (tlendpos)
12319 /* All text outside that line, including its final newline,
12320 must be unchanged. */
12321 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12322 CHARPOS (tlendpos)))
12323 {
12324 if (CHARPOS (tlbufpos) > BEGV
12325 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12326 && (CHARPOS (tlbufpos) == ZV
12327 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12328 /* Former continuation line has disappeared by becoming empty. */
12329 goto cancel;
12330 else if (XFASTINT (w->last_modified) < MODIFF
12331 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12332 || MINI_WINDOW_P (w))
12333 {
12334 /* We have to handle the case of continuation around a
12335 wide-column character (see the comment in indent.c around
12336 line 1340).
12337
12338 For instance, in the following case:
12339
12340 -------- Insert --------
12341 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12342 J_I_ ==> J_I_ `^^' are cursors.
12343 ^^ ^^
12344 -------- --------
12345
12346 As we have to redraw the line above, we cannot use this
12347 optimization. */
12348
12349 struct it it;
12350 int line_height_before = this_line_pixel_height;
12351
12352 /* Note that start_display will handle the case that the
12353 line starting at tlbufpos is a continuation line. */
12354 start_display (&it, w, tlbufpos);
12355
12356 /* Implementation note: It this still necessary? */
12357 if (it.current_x != this_line_start_x)
12358 goto cancel;
12359
12360 TRACE ((stderr, "trying display optimization 1\n"));
12361 w->cursor.vpos = -1;
12362 overlay_arrow_seen = 0;
12363 it.vpos = this_line_vpos;
12364 it.current_y = this_line_y;
12365 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12366 display_line (&it);
12367
12368 /* If line contains point, is not continued,
12369 and ends at same distance from eob as before, we win. */
12370 if (w->cursor.vpos >= 0
12371 /* Line is not continued, otherwise this_line_start_pos
12372 would have been set to 0 in display_line. */
12373 && CHARPOS (this_line_start_pos)
12374 /* Line ends as before. */
12375 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12376 /* Line has same height as before. Otherwise other lines
12377 would have to be shifted up or down. */
12378 && this_line_pixel_height == line_height_before)
12379 {
12380 /* If this is not the window's last line, we must adjust
12381 the charstarts of the lines below. */
12382 if (it.current_y < it.last_visible_y)
12383 {
12384 struct glyph_row *row
12385 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12386 EMACS_INT delta, delta_bytes;
12387
12388 /* We used to distinguish between two cases here,
12389 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12390 when the line ends in a newline or the end of the
12391 buffer's accessible portion. But both cases did
12392 the same, so they were collapsed. */
12393 delta = (Z
12394 - CHARPOS (tlendpos)
12395 - MATRIX_ROW_START_CHARPOS (row));
12396 delta_bytes = (Z_BYTE
12397 - BYTEPOS (tlendpos)
12398 - MATRIX_ROW_START_BYTEPOS (row));
12399
12400 increment_matrix_positions (w->current_matrix,
12401 this_line_vpos + 1,
12402 w->current_matrix->nrows,
12403 delta, delta_bytes);
12404 }
12405
12406 /* If this row displays text now but previously didn't,
12407 or vice versa, w->window_end_vpos may have to be
12408 adjusted. */
12409 if ((it.glyph_row - 1)->displays_text_p)
12410 {
12411 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12412 XSETINT (w->window_end_vpos, this_line_vpos);
12413 }
12414 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12415 && this_line_vpos > 0)
12416 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12417 w->window_end_valid = Qnil;
12418
12419 /* Update hint: No need to try to scroll in update_window. */
12420 w->desired_matrix->no_scrolling_p = 1;
12421
12422 #if GLYPH_DEBUG
12423 *w->desired_matrix->method = 0;
12424 debug_method_add (w, "optimization 1");
12425 #endif
12426 #ifdef HAVE_WINDOW_SYSTEM
12427 update_window_fringes (w, 0);
12428 #endif
12429 goto update;
12430 }
12431 else
12432 goto cancel;
12433 }
12434 else if (/* Cursor position hasn't changed. */
12435 PT == XFASTINT (w->last_point)
12436 /* Make sure the cursor was last displayed
12437 in this window. Otherwise we have to reposition it. */
12438 && 0 <= w->cursor.vpos
12439 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12440 {
12441 if (!must_finish)
12442 {
12443 do_pending_window_change (1);
12444 /* If selected_window changed, redisplay again. */
12445 if (WINDOWP (selected_window)
12446 && (w = XWINDOW (selected_window)) != sw)
12447 goto retry;
12448
12449 /* We used to always goto end_of_redisplay here, but this
12450 isn't enough if we have a blinking cursor. */
12451 if (w->cursor_off_p == w->last_cursor_off_p)
12452 goto end_of_redisplay;
12453 }
12454 goto update;
12455 }
12456 /* If highlighting the region, or if the cursor is in the echo area,
12457 then we can't just move the cursor. */
12458 else if (! (!NILP (Vtransient_mark_mode)
12459 && !NILP (BVAR (current_buffer, mark_active)))
12460 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12461 || highlight_nonselected_windows)
12462 && NILP (w->region_showing)
12463 && NILP (Vshow_trailing_whitespace)
12464 && !cursor_in_echo_area)
12465 {
12466 struct it it;
12467 struct glyph_row *row;
12468
12469 /* Skip from tlbufpos to PT and see where it is. Note that
12470 PT may be in invisible text. If so, we will end at the
12471 next visible position. */
12472 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12473 NULL, DEFAULT_FACE_ID);
12474 it.current_x = this_line_start_x;
12475 it.current_y = this_line_y;
12476 it.vpos = this_line_vpos;
12477
12478 /* The call to move_it_to stops in front of PT, but
12479 moves over before-strings. */
12480 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12481
12482 if (it.vpos == this_line_vpos
12483 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12484 row->enabled_p))
12485 {
12486 xassert (this_line_vpos == it.vpos);
12487 xassert (this_line_y == it.current_y);
12488 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12489 #if GLYPH_DEBUG
12490 *w->desired_matrix->method = 0;
12491 debug_method_add (w, "optimization 3");
12492 #endif
12493 goto update;
12494 }
12495 else
12496 goto cancel;
12497 }
12498
12499 cancel:
12500 /* Text changed drastically or point moved off of line. */
12501 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12502 }
12503
12504 CHARPOS (this_line_start_pos) = 0;
12505 consider_all_windows_p |= buffer_shared > 1;
12506 ++clear_face_cache_count;
12507 #ifdef HAVE_WINDOW_SYSTEM
12508 ++clear_image_cache_count;
12509 #endif
12510
12511 /* Build desired matrices, and update the display. If
12512 consider_all_windows_p is non-zero, do it for all windows on all
12513 frames. Otherwise do it for selected_window, only. */
12514
12515 if (consider_all_windows_p)
12516 {
12517 Lisp_Object tail, frame;
12518
12519 FOR_EACH_FRAME (tail, frame)
12520 XFRAME (frame)->updated_p = 0;
12521
12522 /* Recompute # windows showing selected buffer. This will be
12523 incremented each time such a window is displayed. */
12524 buffer_shared = 0;
12525
12526 FOR_EACH_FRAME (tail, frame)
12527 {
12528 struct frame *f = XFRAME (frame);
12529
12530 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12531 {
12532 if (! EQ (frame, selected_frame))
12533 /* Select the frame, for the sake of frame-local
12534 variables. */
12535 select_frame_for_redisplay (frame);
12536
12537 /* Mark all the scroll bars to be removed; we'll redeem
12538 the ones we want when we redisplay their windows. */
12539 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12540 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12541
12542 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12543 redisplay_windows (FRAME_ROOT_WINDOW (f));
12544
12545 /* The X error handler may have deleted that frame. */
12546 if (!FRAME_LIVE_P (f))
12547 continue;
12548
12549 /* Any scroll bars which redisplay_windows should have
12550 nuked should now go away. */
12551 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12552 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12553
12554 /* If fonts changed, display again. */
12555 /* ??? rms: I suspect it is a mistake to jump all the way
12556 back to retry here. It should just retry this frame. */
12557 if (fonts_changed_p)
12558 goto retry;
12559
12560 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12561 {
12562 /* See if we have to hscroll. */
12563 if (!f->already_hscrolled_p)
12564 {
12565 f->already_hscrolled_p = 1;
12566 if (hscroll_windows (f->root_window))
12567 goto retry;
12568 }
12569
12570 /* Prevent various kinds of signals during display
12571 update. stdio is not robust about handling
12572 signals, which can cause an apparent I/O
12573 error. */
12574 if (interrupt_input)
12575 unrequest_sigio ();
12576 STOP_POLLING;
12577
12578 /* Update the display. */
12579 set_window_update_flags (XWINDOW (f->root_window), 1);
12580 pending |= update_frame (f, 0, 0);
12581 f->updated_p = 1;
12582 }
12583 }
12584 }
12585
12586 if (!EQ (old_frame, selected_frame)
12587 && FRAME_LIVE_P (XFRAME (old_frame)))
12588 /* We played a bit fast-and-loose above and allowed selected_frame
12589 and selected_window to be temporarily out-of-sync but let's make
12590 sure this stays contained. */
12591 select_frame_for_redisplay (old_frame);
12592 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12593
12594 if (!pending)
12595 {
12596 /* Do the mark_window_display_accurate after all windows have
12597 been redisplayed because this call resets flags in buffers
12598 which are needed for proper redisplay. */
12599 FOR_EACH_FRAME (tail, frame)
12600 {
12601 struct frame *f = XFRAME (frame);
12602 if (f->updated_p)
12603 {
12604 mark_window_display_accurate (f->root_window, 1);
12605 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12606 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12607 }
12608 }
12609 }
12610 }
12611 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12612 {
12613 Lisp_Object mini_window;
12614 struct frame *mini_frame;
12615
12616 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12617 /* Use list_of_error, not Qerror, so that
12618 we catch only errors and don't run the debugger. */
12619 internal_condition_case_1 (redisplay_window_1, selected_window,
12620 list_of_error,
12621 redisplay_window_error);
12622
12623 /* Compare desired and current matrices, perform output. */
12624
12625 update:
12626 /* If fonts changed, display again. */
12627 if (fonts_changed_p)
12628 goto retry;
12629
12630 /* Prevent various kinds of signals during display update.
12631 stdio is not robust about handling signals,
12632 which can cause an apparent I/O error. */
12633 if (interrupt_input)
12634 unrequest_sigio ();
12635 STOP_POLLING;
12636
12637 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12638 {
12639 if (hscroll_windows (selected_window))
12640 goto retry;
12641
12642 XWINDOW (selected_window)->must_be_updated_p = 1;
12643 pending = update_frame (sf, 0, 0);
12644 }
12645
12646 /* We may have called echo_area_display at the top of this
12647 function. If the echo area is on another frame, that may
12648 have put text on a frame other than the selected one, so the
12649 above call to update_frame would not have caught it. Catch
12650 it here. */
12651 mini_window = FRAME_MINIBUF_WINDOW (sf);
12652 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12653
12654 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12655 {
12656 XWINDOW (mini_window)->must_be_updated_p = 1;
12657 pending |= update_frame (mini_frame, 0, 0);
12658 if (!pending && hscroll_windows (mini_window))
12659 goto retry;
12660 }
12661 }
12662
12663 /* If display was paused because of pending input, make sure we do a
12664 thorough update the next time. */
12665 if (pending)
12666 {
12667 /* Prevent the optimization at the beginning of
12668 redisplay_internal that tries a single-line update of the
12669 line containing the cursor in the selected window. */
12670 CHARPOS (this_line_start_pos) = 0;
12671
12672 /* Let the overlay arrow be updated the next time. */
12673 update_overlay_arrows (0);
12674
12675 /* If we pause after scrolling, some rows in the current
12676 matrices of some windows are not valid. */
12677 if (!WINDOW_FULL_WIDTH_P (w)
12678 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12679 update_mode_lines = 1;
12680 }
12681 else
12682 {
12683 if (!consider_all_windows_p)
12684 {
12685 /* This has already been done above if
12686 consider_all_windows_p is set. */
12687 mark_window_display_accurate_1 (w, 1);
12688
12689 /* Say overlay arrows are up to date. */
12690 update_overlay_arrows (1);
12691
12692 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12693 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12694 }
12695
12696 update_mode_lines = 0;
12697 windows_or_buffers_changed = 0;
12698 cursor_type_changed = 0;
12699 }
12700
12701 /* Start SIGIO interrupts coming again. Having them off during the
12702 code above makes it less likely one will discard output, but not
12703 impossible, since there might be stuff in the system buffer here.
12704 But it is much hairier to try to do anything about that. */
12705 if (interrupt_input)
12706 request_sigio ();
12707 RESUME_POLLING;
12708
12709 /* If a frame has become visible which was not before, redisplay
12710 again, so that we display it. Expose events for such a frame
12711 (which it gets when becoming visible) don't call the parts of
12712 redisplay constructing glyphs, so simply exposing a frame won't
12713 display anything in this case. So, we have to display these
12714 frames here explicitly. */
12715 if (!pending)
12716 {
12717 Lisp_Object tail, frame;
12718 int new_count = 0;
12719
12720 FOR_EACH_FRAME (tail, frame)
12721 {
12722 int this_is_visible = 0;
12723
12724 if (XFRAME (frame)->visible)
12725 this_is_visible = 1;
12726 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12727 if (XFRAME (frame)->visible)
12728 this_is_visible = 1;
12729
12730 if (this_is_visible)
12731 new_count++;
12732 }
12733
12734 if (new_count != number_of_visible_frames)
12735 windows_or_buffers_changed++;
12736 }
12737
12738 /* Change frame size now if a change is pending. */
12739 do_pending_window_change (1);
12740
12741 /* If we just did a pending size change, or have additional
12742 visible frames, or selected_window changed, redisplay again. */
12743 if ((windows_or_buffers_changed && !pending)
12744 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12745 goto retry;
12746
12747 /* Clear the face and image caches.
12748
12749 We used to do this only if consider_all_windows_p. But the cache
12750 needs to be cleared if a timer creates images in the current
12751 buffer (e.g. the test case in Bug#6230). */
12752
12753 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12754 {
12755 clear_face_cache (0);
12756 clear_face_cache_count = 0;
12757 }
12758
12759 #ifdef HAVE_WINDOW_SYSTEM
12760 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12761 {
12762 clear_image_caches (Qnil);
12763 clear_image_cache_count = 0;
12764 }
12765 #endif /* HAVE_WINDOW_SYSTEM */
12766
12767 end_of_redisplay:
12768 unbind_to (count, Qnil);
12769 RESUME_POLLING;
12770 }
12771
12772
12773 /* Redisplay, but leave alone any recent echo area message unless
12774 another message has been requested in its place.
12775
12776 This is useful in situations where you need to redisplay but no
12777 user action has occurred, making it inappropriate for the message
12778 area to be cleared. See tracking_off and
12779 wait_reading_process_output for examples of these situations.
12780
12781 FROM_WHERE is an integer saying from where this function was
12782 called. This is useful for debugging. */
12783
12784 void
12785 redisplay_preserve_echo_area (int from_where)
12786 {
12787 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12788
12789 if (!NILP (echo_area_buffer[1]))
12790 {
12791 /* We have a previously displayed message, but no current
12792 message. Redisplay the previous message. */
12793 display_last_displayed_message_p = 1;
12794 redisplay_internal ();
12795 display_last_displayed_message_p = 0;
12796 }
12797 else
12798 redisplay_internal ();
12799
12800 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12801 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12802 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12803 }
12804
12805
12806 /* Function registered with record_unwind_protect in
12807 redisplay_internal. Reset redisplaying_p to the value it had
12808 before redisplay_internal was called, and clear
12809 prevent_freeing_realized_faces_p. It also selects the previously
12810 selected frame, unless it has been deleted (by an X connection
12811 failure during redisplay, for example). */
12812
12813 static Lisp_Object
12814 unwind_redisplay (Lisp_Object val)
12815 {
12816 Lisp_Object old_redisplaying_p, old_frame;
12817
12818 old_redisplaying_p = XCAR (val);
12819 redisplaying_p = XFASTINT (old_redisplaying_p);
12820 old_frame = XCDR (val);
12821 if (! EQ (old_frame, selected_frame)
12822 && FRAME_LIVE_P (XFRAME (old_frame)))
12823 select_frame_for_redisplay (old_frame);
12824 return Qnil;
12825 }
12826
12827
12828 /* Mark the display of window W as accurate or inaccurate. If
12829 ACCURATE_P is non-zero mark display of W as accurate. If
12830 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12831 redisplay_internal is called. */
12832
12833 static void
12834 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12835 {
12836 if (BUFFERP (w->buffer))
12837 {
12838 struct buffer *b = XBUFFER (w->buffer);
12839
12840 w->last_modified
12841 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12842 w->last_overlay_modified
12843 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12844 w->last_had_star
12845 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12846
12847 if (accurate_p)
12848 {
12849 b->clip_changed = 0;
12850 b->prevent_redisplay_optimizations_p = 0;
12851
12852 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12853 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12854 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12855 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12856
12857 w->current_matrix->buffer = b;
12858 w->current_matrix->begv = BUF_BEGV (b);
12859 w->current_matrix->zv = BUF_ZV (b);
12860
12861 w->last_cursor = w->cursor;
12862 w->last_cursor_off_p = w->cursor_off_p;
12863
12864 if (w == XWINDOW (selected_window))
12865 w->last_point = make_number (BUF_PT (b));
12866 else
12867 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12868 }
12869 }
12870
12871 if (accurate_p)
12872 {
12873 w->window_end_valid = w->buffer;
12874 w->update_mode_line = Qnil;
12875 }
12876 }
12877
12878
12879 /* Mark the display of windows in the window tree rooted at WINDOW as
12880 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12881 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12882 be redisplayed the next time redisplay_internal is called. */
12883
12884 void
12885 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12886 {
12887 struct window *w;
12888
12889 for (; !NILP (window); window = w->next)
12890 {
12891 w = XWINDOW (window);
12892 mark_window_display_accurate_1 (w, accurate_p);
12893
12894 if (!NILP (w->vchild))
12895 mark_window_display_accurate (w->vchild, accurate_p);
12896 if (!NILP (w->hchild))
12897 mark_window_display_accurate (w->hchild, accurate_p);
12898 }
12899
12900 if (accurate_p)
12901 {
12902 update_overlay_arrows (1);
12903 }
12904 else
12905 {
12906 /* Force a thorough redisplay the next time by setting
12907 last_arrow_position and last_arrow_string to t, which is
12908 unequal to any useful value of Voverlay_arrow_... */
12909 update_overlay_arrows (-1);
12910 }
12911 }
12912
12913
12914 /* Return value in display table DP (Lisp_Char_Table *) for character
12915 C. Since a display table doesn't have any parent, we don't have to
12916 follow parent. Do not call this function directly but use the
12917 macro DISP_CHAR_VECTOR. */
12918
12919 Lisp_Object
12920 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12921 {
12922 Lisp_Object val;
12923
12924 if (ASCII_CHAR_P (c))
12925 {
12926 val = dp->ascii;
12927 if (SUB_CHAR_TABLE_P (val))
12928 val = XSUB_CHAR_TABLE (val)->contents[c];
12929 }
12930 else
12931 {
12932 Lisp_Object table;
12933
12934 XSETCHAR_TABLE (table, dp);
12935 val = char_table_ref (table, c);
12936 }
12937 if (NILP (val))
12938 val = dp->defalt;
12939 return val;
12940 }
12941
12942
12943 \f
12944 /***********************************************************************
12945 Window Redisplay
12946 ***********************************************************************/
12947
12948 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12949
12950 static void
12951 redisplay_windows (Lisp_Object window)
12952 {
12953 while (!NILP (window))
12954 {
12955 struct window *w = XWINDOW (window);
12956
12957 if (!NILP (w->hchild))
12958 redisplay_windows (w->hchild);
12959 else if (!NILP (w->vchild))
12960 redisplay_windows (w->vchild);
12961 else if (!NILP (w->buffer))
12962 {
12963 displayed_buffer = XBUFFER (w->buffer);
12964 /* Use list_of_error, not Qerror, so that
12965 we catch only errors and don't run the debugger. */
12966 internal_condition_case_1 (redisplay_window_0, window,
12967 list_of_error,
12968 redisplay_window_error);
12969 }
12970
12971 window = w->next;
12972 }
12973 }
12974
12975 static Lisp_Object
12976 redisplay_window_error (Lisp_Object ignore)
12977 {
12978 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12979 return Qnil;
12980 }
12981
12982 static Lisp_Object
12983 redisplay_window_0 (Lisp_Object window)
12984 {
12985 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12986 redisplay_window (window, 0);
12987 return Qnil;
12988 }
12989
12990 static Lisp_Object
12991 redisplay_window_1 (Lisp_Object window)
12992 {
12993 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12994 redisplay_window (window, 1);
12995 return Qnil;
12996 }
12997 \f
12998
12999 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13000 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13001 which positions recorded in ROW differ from current buffer
13002 positions.
13003
13004 Return 0 if cursor is not on this row, 1 otherwise. */
13005
13006 static int
13007 set_cursor_from_row (struct window *w, struct glyph_row *row,
13008 struct glyph_matrix *matrix,
13009 EMACS_INT delta, EMACS_INT delta_bytes,
13010 int dy, int dvpos)
13011 {
13012 struct glyph *glyph = row->glyphs[TEXT_AREA];
13013 struct glyph *end = glyph + row->used[TEXT_AREA];
13014 struct glyph *cursor = NULL;
13015 /* The last known character position in row. */
13016 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13017 int x = row->x;
13018 EMACS_INT pt_old = PT - delta;
13019 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13020 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13021 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13022 /* A glyph beyond the edge of TEXT_AREA which we should never
13023 touch. */
13024 struct glyph *glyphs_end = end;
13025 /* Non-zero means we've found a match for cursor position, but that
13026 glyph has the avoid_cursor_p flag set. */
13027 int match_with_avoid_cursor = 0;
13028 /* Non-zero means we've seen at least one glyph that came from a
13029 display string. */
13030 int string_seen = 0;
13031 /* Largest and smalles buffer positions seen so far during scan of
13032 glyph row. */
13033 EMACS_INT bpos_max = pos_before;
13034 EMACS_INT bpos_min = pos_after;
13035 /* Last buffer position covered by an overlay string with an integer
13036 `cursor' property. */
13037 EMACS_INT bpos_covered = 0;
13038
13039 /* Skip over glyphs not having an object at the start and the end of
13040 the row. These are special glyphs like truncation marks on
13041 terminal frames. */
13042 if (row->displays_text_p)
13043 {
13044 if (!row->reversed_p)
13045 {
13046 while (glyph < end
13047 && INTEGERP (glyph->object)
13048 && glyph->charpos < 0)
13049 {
13050 x += glyph->pixel_width;
13051 ++glyph;
13052 }
13053 while (end > glyph
13054 && INTEGERP ((end - 1)->object)
13055 /* CHARPOS is zero for blanks and stretch glyphs
13056 inserted by extend_face_to_end_of_line. */
13057 && (end - 1)->charpos <= 0)
13058 --end;
13059 glyph_before = glyph - 1;
13060 glyph_after = end;
13061 }
13062 else
13063 {
13064 struct glyph *g;
13065
13066 /* If the glyph row is reversed, we need to process it from back
13067 to front, so swap the edge pointers. */
13068 glyphs_end = end = glyph - 1;
13069 glyph += row->used[TEXT_AREA] - 1;
13070
13071 while (glyph > end + 1
13072 && INTEGERP (glyph->object)
13073 && glyph->charpos < 0)
13074 {
13075 --glyph;
13076 x -= glyph->pixel_width;
13077 }
13078 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13079 --glyph;
13080 /* By default, in reversed rows we put the cursor on the
13081 rightmost (first in the reading order) glyph. */
13082 for (g = end + 1; g < glyph; g++)
13083 x += g->pixel_width;
13084 while (end < glyph
13085 && INTEGERP ((end + 1)->object)
13086 && (end + 1)->charpos <= 0)
13087 ++end;
13088 glyph_before = glyph + 1;
13089 glyph_after = end;
13090 }
13091 }
13092 else if (row->reversed_p)
13093 {
13094 /* In R2L rows that don't display text, put the cursor on the
13095 rightmost glyph. Case in point: an empty last line that is
13096 part of an R2L paragraph. */
13097 cursor = end - 1;
13098 /* Avoid placing the cursor on the last glyph of the row, where
13099 on terminal frames we hold the vertical border between
13100 adjacent windows. */
13101 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13102 && !WINDOW_RIGHTMOST_P (w)
13103 && cursor == row->glyphs[LAST_AREA] - 1)
13104 cursor--;
13105 x = -1; /* will be computed below, at label compute_x */
13106 }
13107
13108 /* Step 1: Try to find the glyph whose character position
13109 corresponds to point. If that's not possible, find 2 glyphs
13110 whose character positions are the closest to point, one before
13111 point, the other after it. */
13112 if (!row->reversed_p)
13113 while (/* not marched to end of glyph row */
13114 glyph < end
13115 /* glyph was not inserted by redisplay for internal purposes */
13116 && !INTEGERP (glyph->object))
13117 {
13118 if (BUFFERP (glyph->object))
13119 {
13120 EMACS_INT dpos = glyph->charpos - pt_old;
13121
13122 if (glyph->charpos > bpos_max)
13123 bpos_max = glyph->charpos;
13124 if (glyph->charpos < bpos_min)
13125 bpos_min = glyph->charpos;
13126 if (!glyph->avoid_cursor_p)
13127 {
13128 /* If we hit point, we've found the glyph on which to
13129 display the cursor. */
13130 if (dpos == 0)
13131 {
13132 match_with_avoid_cursor = 0;
13133 break;
13134 }
13135 /* See if we've found a better approximation to
13136 POS_BEFORE or to POS_AFTER. Note that we want the
13137 first (leftmost) glyph of all those that are the
13138 closest from below, and the last (rightmost) of all
13139 those from above. */
13140 if (0 > dpos && dpos > pos_before - pt_old)
13141 {
13142 pos_before = glyph->charpos;
13143 glyph_before = glyph;
13144 }
13145 else if (0 < dpos && dpos <= pos_after - pt_old)
13146 {
13147 pos_after = glyph->charpos;
13148 glyph_after = glyph;
13149 }
13150 }
13151 else if (dpos == 0)
13152 match_with_avoid_cursor = 1;
13153 }
13154 else if (STRINGP (glyph->object))
13155 {
13156 Lisp_Object chprop;
13157 EMACS_INT glyph_pos = glyph->charpos;
13158
13159 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13160 glyph->object);
13161 if (INTEGERP (chprop))
13162 {
13163 bpos_covered = bpos_max + XINT (chprop);
13164 /* If the `cursor' property covers buffer positions up
13165 to and including point, we should display cursor on
13166 this glyph. Note that overlays and text properties
13167 with string values stop bidi reordering, so every
13168 buffer position to the left of the string is always
13169 smaller than any position to the right of the
13170 string. Therefore, if a `cursor' property on one
13171 of the string's characters has an integer value, we
13172 will break out of the loop below _before_ we get to
13173 the position match above. IOW, integer values of
13174 the `cursor' property override the "exact match for
13175 point" strategy of positioning the cursor. */
13176 /* Implementation note: bpos_max == pt_old when, e.g.,
13177 we are in an empty line, where bpos_max is set to
13178 MATRIX_ROW_START_CHARPOS, see above. */
13179 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13180 {
13181 cursor = glyph;
13182 break;
13183 }
13184 }
13185
13186 string_seen = 1;
13187 }
13188 x += glyph->pixel_width;
13189 ++glyph;
13190 }
13191 else if (glyph > end) /* row is reversed */
13192 while (!INTEGERP (glyph->object))
13193 {
13194 if (BUFFERP (glyph->object))
13195 {
13196 EMACS_INT dpos = glyph->charpos - pt_old;
13197
13198 if (glyph->charpos > bpos_max)
13199 bpos_max = glyph->charpos;
13200 if (glyph->charpos < bpos_min)
13201 bpos_min = glyph->charpos;
13202 if (!glyph->avoid_cursor_p)
13203 {
13204 if (dpos == 0)
13205 {
13206 match_with_avoid_cursor = 0;
13207 break;
13208 }
13209 if (0 > dpos && dpos > pos_before - pt_old)
13210 {
13211 pos_before = glyph->charpos;
13212 glyph_before = glyph;
13213 }
13214 else if (0 < dpos && dpos <= pos_after - pt_old)
13215 {
13216 pos_after = glyph->charpos;
13217 glyph_after = glyph;
13218 }
13219 }
13220 else if (dpos == 0)
13221 match_with_avoid_cursor = 1;
13222 }
13223 else if (STRINGP (glyph->object))
13224 {
13225 Lisp_Object chprop;
13226 EMACS_INT glyph_pos = glyph->charpos;
13227
13228 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13229 glyph->object);
13230 if (INTEGERP (chprop))
13231 {
13232 bpos_covered = bpos_max + XINT (chprop);
13233 /* If the `cursor' property covers buffer positions up
13234 to and including point, we should display cursor on
13235 this glyph. */
13236 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13237 {
13238 cursor = glyph;
13239 break;
13240 }
13241 }
13242 string_seen = 1;
13243 }
13244 --glyph;
13245 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13246 {
13247 x--; /* can't use any pixel_width */
13248 break;
13249 }
13250 x -= glyph->pixel_width;
13251 }
13252
13253 /* Step 2: If we didn't find an exact match for point, we need to
13254 look for a proper place to put the cursor among glyphs between
13255 GLYPH_BEFORE and GLYPH_AFTER. */
13256 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13257 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13258 && bpos_covered < pt_old)
13259 {
13260 /* An empty line has a single glyph whose OBJECT is zero and
13261 whose CHARPOS is the position of a newline on that line.
13262 Note that on a TTY, there are more glyphs after that, which
13263 were produced by extend_face_to_end_of_line, but their
13264 CHARPOS is zero or negative. */
13265 int empty_line_p =
13266 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13267 && INTEGERP (glyph->object) && glyph->charpos > 0;
13268
13269 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13270 {
13271 EMACS_INT ellipsis_pos;
13272
13273 /* Scan back over the ellipsis glyphs. */
13274 if (!row->reversed_p)
13275 {
13276 ellipsis_pos = (glyph - 1)->charpos;
13277 while (glyph > row->glyphs[TEXT_AREA]
13278 && (glyph - 1)->charpos == ellipsis_pos)
13279 glyph--, x -= glyph->pixel_width;
13280 /* That loop always goes one position too far, including
13281 the glyph before the ellipsis. So scan forward over
13282 that one. */
13283 x += glyph->pixel_width;
13284 glyph++;
13285 }
13286 else /* row is reversed */
13287 {
13288 ellipsis_pos = (glyph + 1)->charpos;
13289 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13290 && (glyph + 1)->charpos == ellipsis_pos)
13291 glyph++, x += glyph->pixel_width;
13292 x -= glyph->pixel_width;
13293 glyph--;
13294 }
13295 }
13296 else if (match_with_avoid_cursor
13297 /* A truncated row may not include PT among its
13298 character positions. Setting the cursor inside the
13299 scroll margin will trigger recalculation of hscroll
13300 in hscroll_window_tree. */
13301 || (row->truncated_on_left_p && pt_old < bpos_min)
13302 || (row->truncated_on_right_p && pt_old > bpos_max)
13303 /* Zero-width characters produce no glyphs. */
13304 || (!string_seen
13305 && !empty_line_p
13306 && (row->reversed_p
13307 ? glyph_after > glyphs_end
13308 : glyph_after < glyphs_end)))
13309 {
13310 cursor = glyph_after;
13311 x = -1;
13312 }
13313 else if (string_seen)
13314 {
13315 int incr = row->reversed_p ? -1 : +1;
13316
13317 /* Need to find the glyph that came out of a string which is
13318 present at point. That glyph is somewhere between
13319 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13320 positioned between POS_BEFORE and POS_AFTER in the
13321 buffer. */
13322 struct glyph *start, *stop;
13323 EMACS_INT pos = pos_before;
13324
13325 x = -1;
13326
13327 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13328 correspond to POS_BEFORE and POS_AFTER, respectively. We
13329 need START and STOP in the order that corresponds to the
13330 row's direction as given by its reversed_p flag. If the
13331 directionality of characters between POS_BEFORE and
13332 POS_AFTER is the opposite of the row's base direction,
13333 these characters will have been reordered for display,
13334 and we need to reverse START and STOP. */
13335 if (!row->reversed_p)
13336 {
13337 start = min (glyph_before, glyph_after);
13338 stop = max (glyph_before, glyph_after);
13339 }
13340 else
13341 {
13342 start = max (glyph_before, glyph_after);
13343 stop = min (glyph_before, glyph_after);
13344 }
13345 for (glyph = start + incr;
13346 row->reversed_p ? glyph > stop : glyph < stop; )
13347 {
13348
13349 /* Any glyphs that come from the buffer are here because
13350 of bidi reordering. Skip them, and only pay
13351 attention to glyphs that came from some string. */
13352 if (STRINGP (glyph->object))
13353 {
13354 Lisp_Object str;
13355 EMACS_INT tem;
13356
13357 str = glyph->object;
13358 tem = string_buffer_position_lim (str, pos, pos_after, 0);
13359 if (tem == 0 /* from overlay */
13360 || pos <= tem)
13361 {
13362 /* If the string from which this glyph came is
13363 found in the buffer at point, then we've
13364 found the glyph we've been looking for. If
13365 it comes from an overlay (tem == 0), and it
13366 has the `cursor' property on one of its
13367 glyphs, record that glyph as a candidate for
13368 displaying the cursor. (As in the
13369 unidirectional version, we will display the
13370 cursor on the last candidate we find.) */
13371 if (tem == 0 || tem == pt_old)
13372 {
13373 /* The glyphs from this string could have
13374 been reordered. Find the one with the
13375 smallest string position. Or there could
13376 be a character in the string with the
13377 `cursor' property, which means display
13378 cursor on that character's glyph. */
13379 EMACS_INT strpos = glyph->charpos;
13380
13381 if (tem)
13382 cursor = glyph;
13383 for ( ;
13384 (row->reversed_p ? glyph > stop : glyph < stop)
13385 && EQ (glyph->object, str);
13386 glyph += incr)
13387 {
13388 Lisp_Object cprop;
13389 EMACS_INT gpos = glyph->charpos;
13390
13391 cprop = Fget_char_property (make_number (gpos),
13392 Qcursor,
13393 glyph->object);
13394 if (!NILP (cprop))
13395 {
13396 cursor = glyph;
13397 break;
13398 }
13399 if (tem && glyph->charpos < strpos)
13400 {
13401 strpos = glyph->charpos;
13402 cursor = glyph;
13403 }
13404 }
13405
13406 if (tem == pt_old)
13407 goto compute_x;
13408 }
13409 if (tem)
13410 pos = tem + 1; /* don't find previous instances */
13411 }
13412 /* This string is not what we want; skip all of the
13413 glyphs that came from it. */
13414 while ((row->reversed_p ? glyph > stop : glyph < stop)
13415 && EQ (glyph->object, str))
13416 glyph += incr;
13417 }
13418 else
13419 glyph += incr;
13420 }
13421
13422 /* If we reached the end of the line, and END was from a string,
13423 the cursor is not on this line. */
13424 if (cursor == NULL
13425 && (row->reversed_p ? glyph <= end : glyph >= end)
13426 && STRINGP (end->object)
13427 && row->continued_p)
13428 return 0;
13429 }
13430 }
13431
13432 compute_x:
13433 if (cursor != NULL)
13434 glyph = cursor;
13435 if (x < 0)
13436 {
13437 struct glyph *g;
13438
13439 /* Need to compute x that corresponds to GLYPH. */
13440 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13441 {
13442 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13443 abort ();
13444 x += g->pixel_width;
13445 }
13446 }
13447
13448 /* ROW could be part of a continued line, which, under bidi
13449 reordering, might have other rows whose start and end charpos
13450 occlude point. Only set w->cursor if we found a better
13451 approximation to the cursor position than we have from previously
13452 examined candidate rows belonging to the same continued line. */
13453 if (/* we already have a candidate row */
13454 w->cursor.vpos >= 0
13455 /* that candidate is not the row we are processing */
13456 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13457 /* the row we are processing is part of a continued line */
13458 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13459 /* Make sure cursor.vpos specifies a row whose start and end
13460 charpos occlude point. This is because some callers of this
13461 function leave cursor.vpos at the row where the cursor was
13462 displayed during the last redisplay cycle. */
13463 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13464 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13465 {
13466 struct glyph *g1 =
13467 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13468
13469 /* Don't consider glyphs that are outside TEXT_AREA. */
13470 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13471 return 0;
13472 /* Keep the candidate whose buffer position is the closest to
13473 point. */
13474 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13475 w->cursor.hpos >= 0
13476 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13477 && BUFFERP (g1->object)
13478 && (g1->charpos == pt_old /* an exact match always wins */
13479 || (BUFFERP (glyph->object)
13480 && eabs (g1->charpos - pt_old)
13481 < eabs (glyph->charpos - pt_old))))
13482 return 0;
13483 /* If this candidate gives an exact match, use that. */
13484 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13485 /* Otherwise, keep the candidate that comes from a row
13486 spanning less buffer positions. This may win when one or
13487 both candidate positions are on glyphs that came from
13488 display strings, for which we cannot compare buffer
13489 positions. */
13490 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13491 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13492 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13493 return 0;
13494 }
13495 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13496 w->cursor.x = x;
13497 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13498 w->cursor.y = row->y + dy;
13499
13500 if (w == XWINDOW (selected_window))
13501 {
13502 if (!row->continued_p
13503 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13504 && row->x == 0)
13505 {
13506 this_line_buffer = XBUFFER (w->buffer);
13507
13508 CHARPOS (this_line_start_pos)
13509 = MATRIX_ROW_START_CHARPOS (row) + delta;
13510 BYTEPOS (this_line_start_pos)
13511 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13512
13513 CHARPOS (this_line_end_pos)
13514 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13515 BYTEPOS (this_line_end_pos)
13516 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13517
13518 this_line_y = w->cursor.y;
13519 this_line_pixel_height = row->height;
13520 this_line_vpos = w->cursor.vpos;
13521 this_line_start_x = row->x;
13522 }
13523 else
13524 CHARPOS (this_line_start_pos) = 0;
13525 }
13526
13527 return 1;
13528 }
13529
13530
13531 /* Run window scroll functions, if any, for WINDOW with new window
13532 start STARTP. Sets the window start of WINDOW to that position.
13533
13534 We assume that the window's buffer is really current. */
13535
13536 static INLINE struct text_pos
13537 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13538 {
13539 struct window *w = XWINDOW (window);
13540 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13541
13542 if (current_buffer != XBUFFER (w->buffer))
13543 abort ();
13544
13545 if (!NILP (Vwindow_scroll_functions))
13546 {
13547 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13548 make_number (CHARPOS (startp)));
13549 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13550 /* In case the hook functions switch buffers. */
13551 if (current_buffer != XBUFFER (w->buffer))
13552 set_buffer_internal_1 (XBUFFER (w->buffer));
13553 }
13554
13555 return startp;
13556 }
13557
13558
13559 /* Make sure the line containing the cursor is fully visible.
13560 A value of 1 means there is nothing to be done.
13561 (Either the line is fully visible, or it cannot be made so,
13562 or we cannot tell.)
13563
13564 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13565 is higher than window.
13566
13567 A value of 0 means the caller should do scrolling
13568 as if point had gone off the screen. */
13569
13570 static int
13571 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13572 {
13573 struct glyph_matrix *matrix;
13574 struct glyph_row *row;
13575 int window_height;
13576
13577 if (!make_cursor_line_fully_visible_p)
13578 return 1;
13579
13580 /* It's not always possible to find the cursor, e.g, when a window
13581 is full of overlay strings. Don't do anything in that case. */
13582 if (w->cursor.vpos < 0)
13583 return 1;
13584
13585 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13586 row = MATRIX_ROW (matrix, w->cursor.vpos);
13587
13588 /* If the cursor row is not partially visible, there's nothing to do. */
13589 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13590 return 1;
13591
13592 /* If the row the cursor is in is taller than the window's height,
13593 it's not clear what to do, so do nothing. */
13594 window_height = window_box_height (w);
13595 if (row->height >= window_height)
13596 {
13597 if (!force_p || MINI_WINDOW_P (w)
13598 || w->vscroll || w->cursor.vpos == 0)
13599 return 1;
13600 }
13601 return 0;
13602 }
13603
13604
13605 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13606 non-zero means only WINDOW is redisplayed in redisplay_internal.
13607 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13608 in redisplay_window to bring a partially visible line into view in
13609 the case that only the cursor has moved.
13610
13611 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13612 last screen line's vertical height extends past the end of the screen.
13613
13614 Value is
13615
13616 1 if scrolling succeeded
13617
13618 0 if scrolling didn't find point.
13619
13620 -1 if new fonts have been loaded so that we must interrupt
13621 redisplay, adjust glyph matrices, and try again. */
13622
13623 enum
13624 {
13625 SCROLLING_SUCCESS,
13626 SCROLLING_FAILED,
13627 SCROLLING_NEED_LARGER_MATRICES
13628 };
13629
13630 /* If scroll-conservatively is more than this, never recenter.
13631
13632 If you change this, don't forget to update the doc string of
13633 `scroll-conservatively' and the Emacs manual. */
13634 #define SCROLL_LIMIT 100
13635
13636 static int
13637 try_scrolling (Lisp_Object window, int just_this_one_p,
13638 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13639 int temp_scroll_step, int last_line_misfit)
13640 {
13641 struct window *w = XWINDOW (window);
13642 struct frame *f = XFRAME (w->frame);
13643 struct text_pos pos, startp;
13644 struct it it;
13645 int this_scroll_margin, scroll_max, rc, height;
13646 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13647 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13648 Lisp_Object aggressive;
13649 /* We will never try scrolling more than this number of lines. */
13650 int scroll_limit = SCROLL_LIMIT;
13651
13652 #if GLYPH_DEBUG
13653 debug_method_add (w, "try_scrolling");
13654 #endif
13655
13656 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13657
13658 /* Compute scroll margin height in pixels. We scroll when point is
13659 within this distance from the top or bottom of the window. */
13660 if (scroll_margin > 0)
13661 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13662 * FRAME_LINE_HEIGHT (f);
13663 else
13664 this_scroll_margin = 0;
13665
13666 /* Force arg_scroll_conservatively to have a reasonable value, to
13667 avoid scrolling too far away with slow move_it_* functions. Note
13668 that the user can supply scroll-conservatively equal to
13669 `most-positive-fixnum', which can be larger than INT_MAX. */
13670 if (arg_scroll_conservatively > scroll_limit)
13671 {
13672 arg_scroll_conservatively = scroll_limit + 1;
13673 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13674 }
13675 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13676 /* Compute how much we should try to scroll maximally to bring
13677 point into view. */
13678 scroll_max = (max (scroll_step,
13679 max (arg_scroll_conservatively, temp_scroll_step))
13680 * FRAME_LINE_HEIGHT (f));
13681 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13682 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13683 /* We're trying to scroll because of aggressive scrolling but no
13684 scroll_step is set. Choose an arbitrary one. */
13685 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13686 else
13687 scroll_max = 0;
13688
13689 too_near_end:
13690
13691 /* Decide whether to scroll down. */
13692 if (PT > CHARPOS (startp))
13693 {
13694 int scroll_margin_y;
13695
13696 /* Compute the pixel ypos of the scroll margin, then move it to
13697 either that ypos or PT, whichever comes first. */
13698 start_display (&it, w, startp);
13699 scroll_margin_y = it.last_visible_y - this_scroll_margin
13700 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13701 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13702 (MOVE_TO_POS | MOVE_TO_Y));
13703
13704 if (PT > CHARPOS (it.current.pos))
13705 {
13706 int y0 = line_bottom_y (&it);
13707 /* Compute how many pixels below window bottom to stop searching
13708 for PT. This avoids costly search for PT that is far away if
13709 the user limited scrolling by a small number of lines, but
13710 always finds PT if scroll_conservatively is set to a large
13711 number, such as most-positive-fixnum. */
13712 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13713 int y_to_move = it.last_visible_y + slack;
13714
13715 /* Compute the distance from the scroll margin to PT or to
13716 the scroll limit, whichever comes first. This should
13717 include the height of the cursor line, to make that line
13718 fully visible. */
13719 move_it_to (&it, PT, -1, y_to_move,
13720 -1, MOVE_TO_POS | MOVE_TO_Y);
13721 dy = line_bottom_y (&it) - y0;
13722
13723 if (dy > scroll_max)
13724 return SCROLLING_FAILED;
13725
13726 scroll_down_p = 1;
13727 }
13728 }
13729
13730 if (scroll_down_p)
13731 {
13732 /* Point is in or below the bottom scroll margin, so move the
13733 window start down. If scrolling conservatively, move it just
13734 enough down to make point visible. If scroll_step is set,
13735 move it down by scroll_step. */
13736 if (arg_scroll_conservatively)
13737 amount_to_scroll
13738 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13739 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13740 else if (scroll_step || temp_scroll_step)
13741 amount_to_scroll = scroll_max;
13742 else
13743 {
13744 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13745 height = WINDOW_BOX_TEXT_HEIGHT (w);
13746 if (NUMBERP (aggressive))
13747 {
13748 double float_amount = XFLOATINT (aggressive) * height;
13749 amount_to_scroll = float_amount;
13750 if (amount_to_scroll == 0 && float_amount > 0)
13751 amount_to_scroll = 1;
13752 /* Don't let point enter the scroll margin near top of
13753 the window. */
13754 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13755 amount_to_scroll = height - 2*this_scroll_margin + dy;
13756 }
13757 }
13758
13759 if (amount_to_scroll <= 0)
13760 return SCROLLING_FAILED;
13761
13762 start_display (&it, w, startp);
13763 if (arg_scroll_conservatively <= scroll_limit)
13764 move_it_vertically (&it, amount_to_scroll);
13765 else
13766 {
13767 /* Extra precision for users who set scroll-conservatively
13768 to a large number: make sure the amount we scroll
13769 the window start is never less than amount_to_scroll,
13770 which was computed as distance from window bottom to
13771 point. This matters when lines at window top and lines
13772 below window bottom have different height. */
13773 struct it it1 = it;
13774 /* We use a temporary it1 because line_bottom_y can modify
13775 its argument, if it moves one line down; see there. */
13776 int start_y = line_bottom_y (&it1);
13777
13778 do {
13779 move_it_by_lines (&it, 1);
13780 it1 = it;
13781 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13782 }
13783
13784 /* If STARTP is unchanged, move it down another screen line. */
13785 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13786 move_it_by_lines (&it, 1);
13787 startp = it.current.pos;
13788 }
13789 else
13790 {
13791 struct text_pos scroll_margin_pos = startp;
13792
13793 /* See if point is inside the scroll margin at the top of the
13794 window. */
13795 if (this_scroll_margin)
13796 {
13797 start_display (&it, w, startp);
13798 move_it_vertically (&it, this_scroll_margin);
13799 scroll_margin_pos = it.current.pos;
13800 }
13801
13802 if (PT < CHARPOS (scroll_margin_pos))
13803 {
13804 /* Point is in the scroll margin at the top of the window or
13805 above what is displayed in the window. */
13806 int y0, y_to_move;
13807
13808 /* Compute the vertical distance from PT to the scroll
13809 margin position. Move as far as scroll_max allows, or
13810 one screenful, or 10 screen lines, whichever is largest.
13811 Give up if distance is greater than scroll_max. */
13812 SET_TEXT_POS (pos, PT, PT_BYTE);
13813 start_display (&it, w, pos);
13814 y0 = it.current_y;
13815 y_to_move = max (it.last_visible_y,
13816 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13817 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13818 y_to_move, -1,
13819 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13820 dy = it.current_y - y0;
13821 if (dy > scroll_max)
13822 return SCROLLING_FAILED;
13823
13824 /* Compute new window start. */
13825 start_display (&it, w, startp);
13826
13827 if (arg_scroll_conservatively)
13828 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13829 max (scroll_step, temp_scroll_step));
13830 else if (scroll_step || temp_scroll_step)
13831 amount_to_scroll = scroll_max;
13832 else
13833 {
13834 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13835 height = WINDOW_BOX_TEXT_HEIGHT (w);
13836 if (NUMBERP (aggressive))
13837 {
13838 double float_amount = XFLOATINT (aggressive) * height;
13839 amount_to_scroll = float_amount;
13840 if (amount_to_scroll == 0 && float_amount > 0)
13841 amount_to_scroll = 1;
13842 amount_to_scroll -=
13843 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13844 /* Don't let point enter the scroll margin near
13845 bottom of the window. */
13846 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13847 amount_to_scroll = height - 2*this_scroll_margin + dy;
13848 }
13849 }
13850
13851 if (amount_to_scroll <= 0)
13852 return SCROLLING_FAILED;
13853
13854 move_it_vertically_backward (&it, amount_to_scroll);
13855 startp = it.current.pos;
13856 }
13857 }
13858
13859 /* Run window scroll functions. */
13860 startp = run_window_scroll_functions (window, startp);
13861
13862 /* Display the window. Give up if new fonts are loaded, or if point
13863 doesn't appear. */
13864 if (!try_window (window, startp, 0))
13865 rc = SCROLLING_NEED_LARGER_MATRICES;
13866 else if (w->cursor.vpos < 0)
13867 {
13868 clear_glyph_matrix (w->desired_matrix);
13869 rc = SCROLLING_FAILED;
13870 }
13871 else
13872 {
13873 /* Maybe forget recorded base line for line number display. */
13874 if (!just_this_one_p
13875 || current_buffer->clip_changed
13876 || BEG_UNCHANGED < CHARPOS (startp))
13877 w->base_line_number = Qnil;
13878
13879 /* If cursor ends up on a partially visible line,
13880 treat that as being off the bottom of the screen. */
13881 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13882 /* It's possible that the cursor is on the first line of the
13883 buffer, which is partially obscured due to a vscroll
13884 (Bug#7537). In that case, avoid looping forever . */
13885 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13886 {
13887 clear_glyph_matrix (w->desired_matrix);
13888 ++extra_scroll_margin_lines;
13889 goto too_near_end;
13890 }
13891 rc = SCROLLING_SUCCESS;
13892 }
13893
13894 return rc;
13895 }
13896
13897
13898 /* Compute a suitable window start for window W if display of W starts
13899 on a continuation line. Value is non-zero if a new window start
13900 was computed.
13901
13902 The new window start will be computed, based on W's width, starting
13903 from the start of the continued line. It is the start of the
13904 screen line with the minimum distance from the old start W->start. */
13905
13906 static int
13907 compute_window_start_on_continuation_line (struct window *w)
13908 {
13909 struct text_pos pos, start_pos;
13910 int window_start_changed_p = 0;
13911
13912 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13913
13914 /* If window start is on a continuation line... Window start may be
13915 < BEGV in case there's invisible text at the start of the
13916 buffer (M-x rmail, for example). */
13917 if (CHARPOS (start_pos) > BEGV
13918 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13919 {
13920 struct it it;
13921 struct glyph_row *row;
13922
13923 /* Handle the case that the window start is out of range. */
13924 if (CHARPOS (start_pos) < BEGV)
13925 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13926 else if (CHARPOS (start_pos) > ZV)
13927 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13928
13929 /* Find the start of the continued line. This should be fast
13930 because scan_buffer is fast (newline cache). */
13931 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13932 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13933 row, DEFAULT_FACE_ID);
13934 reseat_at_previous_visible_line_start (&it);
13935
13936 /* If the line start is "too far" away from the window start,
13937 say it takes too much time to compute a new window start. */
13938 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13939 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13940 {
13941 int min_distance, distance;
13942
13943 /* Move forward by display lines to find the new window
13944 start. If window width was enlarged, the new start can
13945 be expected to be > the old start. If window width was
13946 decreased, the new window start will be < the old start.
13947 So, we're looking for the display line start with the
13948 minimum distance from the old window start. */
13949 pos = it.current.pos;
13950 min_distance = INFINITY;
13951 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13952 distance < min_distance)
13953 {
13954 min_distance = distance;
13955 pos = it.current.pos;
13956 move_it_by_lines (&it, 1);
13957 }
13958
13959 /* Set the window start there. */
13960 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13961 window_start_changed_p = 1;
13962 }
13963 }
13964
13965 return window_start_changed_p;
13966 }
13967
13968
13969 /* Try cursor movement in case text has not changed in window WINDOW,
13970 with window start STARTP. Value is
13971
13972 CURSOR_MOVEMENT_SUCCESS if successful
13973
13974 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13975
13976 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13977 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13978 we want to scroll as if scroll-step were set to 1. See the code.
13979
13980 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13981 which case we have to abort this redisplay, and adjust matrices
13982 first. */
13983
13984 enum
13985 {
13986 CURSOR_MOVEMENT_SUCCESS,
13987 CURSOR_MOVEMENT_CANNOT_BE_USED,
13988 CURSOR_MOVEMENT_MUST_SCROLL,
13989 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13990 };
13991
13992 static int
13993 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13994 {
13995 struct window *w = XWINDOW (window);
13996 struct frame *f = XFRAME (w->frame);
13997 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13998
13999 #if GLYPH_DEBUG
14000 if (inhibit_try_cursor_movement)
14001 return rc;
14002 #endif
14003
14004 /* Handle case where text has not changed, only point, and it has
14005 not moved off the frame. */
14006 if (/* Point may be in this window. */
14007 PT >= CHARPOS (startp)
14008 /* Selective display hasn't changed. */
14009 && !current_buffer->clip_changed
14010 /* Function force-mode-line-update is used to force a thorough
14011 redisplay. It sets either windows_or_buffers_changed or
14012 update_mode_lines. So don't take a shortcut here for these
14013 cases. */
14014 && !update_mode_lines
14015 && !windows_or_buffers_changed
14016 && !cursor_type_changed
14017 /* Can't use this case if highlighting a region. When a
14018 region exists, cursor movement has to do more than just
14019 set the cursor. */
14020 && !(!NILP (Vtransient_mark_mode)
14021 && !NILP (BVAR (current_buffer, mark_active)))
14022 && NILP (w->region_showing)
14023 && NILP (Vshow_trailing_whitespace)
14024 /* Right after splitting windows, last_point may be nil. */
14025 && INTEGERP (w->last_point)
14026 /* This code is not used for mini-buffer for the sake of the case
14027 of redisplaying to replace an echo area message; since in
14028 that case the mini-buffer contents per se are usually
14029 unchanged. This code is of no real use in the mini-buffer
14030 since the handling of this_line_start_pos, etc., in redisplay
14031 handles the same cases. */
14032 && !EQ (window, minibuf_window)
14033 /* When splitting windows or for new windows, it happens that
14034 redisplay is called with a nil window_end_vpos or one being
14035 larger than the window. This should really be fixed in
14036 window.c. I don't have this on my list, now, so we do
14037 approximately the same as the old redisplay code. --gerd. */
14038 && INTEGERP (w->window_end_vpos)
14039 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14040 && (FRAME_WINDOW_P (f)
14041 || !overlay_arrow_in_current_buffer_p ()))
14042 {
14043 int this_scroll_margin, top_scroll_margin;
14044 struct glyph_row *row = NULL;
14045
14046 #if GLYPH_DEBUG
14047 debug_method_add (w, "cursor movement");
14048 #endif
14049
14050 /* Scroll if point within this distance from the top or bottom
14051 of the window. This is a pixel value. */
14052 if (scroll_margin > 0)
14053 {
14054 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14055 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14056 }
14057 else
14058 this_scroll_margin = 0;
14059
14060 top_scroll_margin = this_scroll_margin;
14061 if (WINDOW_WANTS_HEADER_LINE_P (w))
14062 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14063
14064 /* Start with the row the cursor was displayed during the last
14065 not paused redisplay. Give up if that row is not valid. */
14066 if (w->last_cursor.vpos < 0
14067 || w->last_cursor.vpos >= w->current_matrix->nrows)
14068 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14069 else
14070 {
14071 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14072 if (row->mode_line_p)
14073 ++row;
14074 if (!row->enabled_p)
14075 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14076 }
14077
14078 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14079 {
14080 int scroll_p = 0, must_scroll = 0;
14081 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14082
14083 if (PT > XFASTINT (w->last_point))
14084 {
14085 /* Point has moved forward. */
14086 while (MATRIX_ROW_END_CHARPOS (row) < PT
14087 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14088 {
14089 xassert (row->enabled_p);
14090 ++row;
14091 }
14092
14093 /* If the end position of a row equals the start
14094 position of the next row, and PT is at that position,
14095 we would rather display cursor in the next line. */
14096 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14097 && MATRIX_ROW_END_CHARPOS (row) == PT
14098 && row < w->current_matrix->rows
14099 + w->current_matrix->nrows - 1
14100 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14101 && !cursor_row_p (row))
14102 ++row;
14103
14104 /* If within the scroll margin, scroll. Note that
14105 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14106 the next line would be drawn, and that
14107 this_scroll_margin can be zero. */
14108 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14109 || PT > MATRIX_ROW_END_CHARPOS (row)
14110 /* Line is completely visible last line in window
14111 and PT is to be set in the next line. */
14112 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14113 && PT == MATRIX_ROW_END_CHARPOS (row)
14114 && !row->ends_at_zv_p
14115 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14116 scroll_p = 1;
14117 }
14118 else if (PT < XFASTINT (w->last_point))
14119 {
14120 /* Cursor has to be moved backward. Note that PT >=
14121 CHARPOS (startp) because of the outer if-statement. */
14122 while (!row->mode_line_p
14123 && (MATRIX_ROW_START_CHARPOS (row) > PT
14124 || (MATRIX_ROW_START_CHARPOS (row) == PT
14125 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14126 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14127 row > w->current_matrix->rows
14128 && (row-1)->ends_in_newline_from_string_p))))
14129 && (row->y > top_scroll_margin
14130 || CHARPOS (startp) == BEGV))
14131 {
14132 xassert (row->enabled_p);
14133 --row;
14134 }
14135
14136 /* Consider the following case: Window starts at BEGV,
14137 there is invisible, intangible text at BEGV, so that
14138 display starts at some point START > BEGV. It can
14139 happen that we are called with PT somewhere between
14140 BEGV and START. Try to handle that case. */
14141 if (row < w->current_matrix->rows
14142 || row->mode_line_p)
14143 {
14144 row = w->current_matrix->rows;
14145 if (row->mode_line_p)
14146 ++row;
14147 }
14148
14149 /* Due to newlines in overlay strings, we may have to
14150 skip forward over overlay strings. */
14151 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14152 && MATRIX_ROW_END_CHARPOS (row) == PT
14153 && !cursor_row_p (row))
14154 ++row;
14155
14156 /* If within the scroll margin, scroll. */
14157 if (row->y < top_scroll_margin
14158 && CHARPOS (startp) != BEGV)
14159 scroll_p = 1;
14160 }
14161 else
14162 {
14163 /* Cursor did not move. So don't scroll even if cursor line
14164 is partially visible, as it was so before. */
14165 rc = CURSOR_MOVEMENT_SUCCESS;
14166 }
14167
14168 if (PT < MATRIX_ROW_START_CHARPOS (row)
14169 || PT > MATRIX_ROW_END_CHARPOS (row))
14170 {
14171 /* if PT is not in the glyph row, give up. */
14172 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14173 must_scroll = 1;
14174 }
14175 else if (rc != CURSOR_MOVEMENT_SUCCESS
14176 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14177 {
14178 /* If rows are bidi-reordered and point moved, back up
14179 until we find a row that does not belong to a
14180 continuation line. This is because we must consider
14181 all rows of a continued line as candidates for the
14182 new cursor positioning, since row start and end
14183 positions change non-linearly with vertical position
14184 in such rows. */
14185 /* FIXME: Revisit this when glyph ``spilling'' in
14186 continuation lines' rows is implemented for
14187 bidi-reordered rows. */
14188 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14189 {
14190 xassert (row->enabled_p);
14191 --row;
14192 /* If we hit the beginning of the displayed portion
14193 without finding the first row of a continued
14194 line, give up. */
14195 if (row <= w->current_matrix->rows)
14196 {
14197 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14198 break;
14199 }
14200
14201 }
14202 }
14203 if (must_scroll)
14204 ;
14205 else if (rc != CURSOR_MOVEMENT_SUCCESS
14206 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14207 && make_cursor_line_fully_visible_p)
14208 {
14209 if (PT == MATRIX_ROW_END_CHARPOS (row)
14210 && !row->ends_at_zv_p
14211 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14212 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14213 else if (row->height > window_box_height (w))
14214 {
14215 /* If we end up in a partially visible line, let's
14216 make it fully visible, except when it's taller
14217 than the window, in which case we can't do much
14218 about it. */
14219 *scroll_step = 1;
14220 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14221 }
14222 else
14223 {
14224 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14225 if (!cursor_row_fully_visible_p (w, 0, 1))
14226 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14227 else
14228 rc = CURSOR_MOVEMENT_SUCCESS;
14229 }
14230 }
14231 else if (scroll_p)
14232 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14233 else if (rc != CURSOR_MOVEMENT_SUCCESS
14234 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14235 {
14236 /* With bidi-reordered rows, there could be more than
14237 one candidate row whose start and end positions
14238 occlude point. We need to let set_cursor_from_row
14239 find the best candidate. */
14240 /* FIXME: Revisit this when glyph ``spilling'' in
14241 continuation lines' rows is implemented for
14242 bidi-reordered rows. */
14243 int rv = 0;
14244
14245 do
14246 {
14247 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14248 && PT <= MATRIX_ROW_END_CHARPOS (row)
14249 && cursor_row_p (row))
14250 rv |= set_cursor_from_row (w, row, w->current_matrix,
14251 0, 0, 0, 0);
14252 /* As soon as we've found the first suitable row
14253 whose ends_at_zv_p flag is set, we are done. */
14254 if (rv
14255 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14256 {
14257 rc = CURSOR_MOVEMENT_SUCCESS;
14258 break;
14259 }
14260 ++row;
14261 }
14262 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14263 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14264 || (MATRIX_ROW_START_CHARPOS (row) == PT
14265 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14266 /* If we didn't find any candidate rows, or exited the
14267 loop before all the candidates were examined, signal
14268 to the caller that this method failed. */
14269 if (rc != CURSOR_MOVEMENT_SUCCESS
14270 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14271 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14272 else if (rv)
14273 rc = CURSOR_MOVEMENT_SUCCESS;
14274 }
14275 else
14276 {
14277 do
14278 {
14279 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14280 {
14281 rc = CURSOR_MOVEMENT_SUCCESS;
14282 break;
14283 }
14284 ++row;
14285 }
14286 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14287 && MATRIX_ROW_START_CHARPOS (row) == PT
14288 && cursor_row_p (row));
14289 }
14290 }
14291 }
14292
14293 return rc;
14294 }
14295
14296 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14297 static
14298 #endif
14299 void
14300 set_vertical_scroll_bar (struct window *w)
14301 {
14302 EMACS_INT start, end, whole;
14303
14304 /* Calculate the start and end positions for the current window.
14305 At some point, it would be nice to choose between scrollbars
14306 which reflect the whole buffer size, with special markers
14307 indicating narrowing, and scrollbars which reflect only the
14308 visible region.
14309
14310 Note that mini-buffers sometimes aren't displaying any text. */
14311 if (!MINI_WINDOW_P (w)
14312 || (w == XWINDOW (minibuf_window)
14313 && NILP (echo_area_buffer[0])))
14314 {
14315 struct buffer *buf = XBUFFER (w->buffer);
14316 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14317 start = marker_position (w->start) - BUF_BEGV (buf);
14318 /* I don't think this is guaranteed to be right. For the
14319 moment, we'll pretend it is. */
14320 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14321
14322 if (end < start)
14323 end = start;
14324 if (whole < (end - start))
14325 whole = end - start;
14326 }
14327 else
14328 start = end = whole = 0;
14329
14330 /* Indicate what this scroll bar ought to be displaying now. */
14331 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14332 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14333 (w, end - start, whole, start);
14334 }
14335
14336
14337 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14338 selected_window is redisplayed.
14339
14340 We can return without actually redisplaying the window if
14341 fonts_changed_p is nonzero. In that case, redisplay_internal will
14342 retry. */
14343
14344 static void
14345 redisplay_window (Lisp_Object window, int just_this_one_p)
14346 {
14347 struct window *w = XWINDOW (window);
14348 struct frame *f = XFRAME (w->frame);
14349 struct buffer *buffer = XBUFFER (w->buffer);
14350 struct buffer *old = current_buffer;
14351 struct text_pos lpoint, opoint, startp;
14352 int update_mode_line;
14353 int tem;
14354 struct it it;
14355 /* Record it now because it's overwritten. */
14356 int current_matrix_up_to_date_p = 0;
14357 int used_current_matrix_p = 0;
14358 /* This is less strict than current_matrix_up_to_date_p.
14359 It indictes that the buffer contents and narrowing are unchanged. */
14360 int buffer_unchanged_p = 0;
14361 int temp_scroll_step = 0;
14362 int count = SPECPDL_INDEX ();
14363 int rc;
14364 int centering_position = -1;
14365 int last_line_misfit = 0;
14366 EMACS_INT beg_unchanged, end_unchanged;
14367
14368 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14369 opoint = lpoint;
14370
14371 /* W must be a leaf window here. */
14372 xassert (!NILP (w->buffer));
14373 #if GLYPH_DEBUG
14374 *w->desired_matrix->method = 0;
14375 #endif
14376
14377 restart:
14378 reconsider_clip_changes (w, buffer);
14379
14380 /* Has the mode line to be updated? */
14381 update_mode_line = (!NILP (w->update_mode_line)
14382 || update_mode_lines
14383 || buffer->clip_changed
14384 || buffer->prevent_redisplay_optimizations_p);
14385
14386 if (MINI_WINDOW_P (w))
14387 {
14388 if (w == XWINDOW (echo_area_window)
14389 && !NILP (echo_area_buffer[0]))
14390 {
14391 if (update_mode_line)
14392 /* We may have to update a tty frame's menu bar or a
14393 tool-bar. Example `M-x C-h C-h C-g'. */
14394 goto finish_menu_bars;
14395 else
14396 /* We've already displayed the echo area glyphs in this window. */
14397 goto finish_scroll_bars;
14398 }
14399 else if ((w != XWINDOW (minibuf_window)
14400 || minibuf_level == 0)
14401 /* When buffer is nonempty, redisplay window normally. */
14402 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14403 /* Quail displays non-mini buffers in minibuffer window.
14404 In that case, redisplay the window normally. */
14405 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14406 {
14407 /* W is a mini-buffer window, but it's not active, so clear
14408 it. */
14409 int yb = window_text_bottom_y (w);
14410 struct glyph_row *row;
14411 int y;
14412
14413 for (y = 0, row = w->desired_matrix->rows;
14414 y < yb;
14415 y += row->height, ++row)
14416 blank_row (w, row, y);
14417 goto finish_scroll_bars;
14418 }
14419
14420 clear_glyph_matrix (w->desired_matrix);
14421 }
14422
14423 /* Otherwise set up data on this window; select its buffer and point
14424 value. */
14425 /* Really select the buffer, for the sake of buffer-local
14426 variables. */
14427 set_buffer_internal_1 (XBUFFER (w->buffer));
14428
14429 current_matrix_up_to_date_p
14430 = (!NILP (w->window_end_valid)
14431 && !current_buffer->clip_changed
14432 && !current_buffer->prevent_redisplay_optimizations_p
14433 && XFASTINT (w->last_modified) >= MODIFF
14434 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14435
14436 /* Run the window-bottom-change-functions
14437 if it is possible that the text on the screen has changed
14438 (either due to modification of the text, or any other reason). */
14439 if (!current_matrix_up_to_date_p
14440 && !NILP (Vwindow_text_change_functions))
14441 {
14442 safe_run_hooks (Qwindow_text_change_functions);
14443 goto restart;
14444 }
14445
14446 beg_unchanged = BEG_UNCHANGED;
14447 end_unchanged = END_UNCHANGED;
14448
14449 SET_TEXT_POS (opoint, PT, PT_BYTE);
14450
14451 specbind (Qinhibit_point_motion_hooks, Qt);
14452
14453 buffer_unchanged_p
14454 = (!NILP (w->window_end_valid)
14455 && !current_buffer->clip_changed
14456 && XFASTINT (w->last_modified) >= MODIFF
14457 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14458
14459 /* When windows_or_buffers_changed is non-zero, we can't rely on
14460 the window end being valid, so set it to nil there. */
14461 if (windows_or_buffers_changed)
14462 {
14463 /* If window starts on a continuation line, maybe adjust the
14464 window start in case the window's width changed. */
14465 if (XMARKER (w->start)->buffer == current_buffer)
14466 compute_window_start_on_continuation_line (w);
14467
14468 w->window_end_valid = Qnil;
14469 }
14470
14471 /* Some sanity checks. */
14472 CHECK_WINDOW_END (w);
14473 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14474 abort ();
14475 if (BYTEPOS (opoint) < CHARPOS (opoint))
14476 abort ();
14477
14478 /* If %c is in mode line, update it if needed. */
14479 if (!NILP (w->column_number_displayed)
14480 /* This alternative quickly identifies a common case
14481 where no change is needed. */
14482 && !(PT == XFASTINT (w->last_point)
14483 && XFASTINT (w->last_modified) >= MODIFF
14484 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14485 && (XFASTINT (w->column_number_displayed) != current_column ()))
14486 update_mode_line = 1;
14487
14488 /* Count number of windows showing the selected buffer. An indirect
14489 buffer counts as its base buffer. */
14490 if (!just_this_one_p)
14491 {
14492 struct buffer *current_base, *window_base;
14493 current_base = current_buffer;
14494 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14495 if (current_base->base_buffer)
14496 current_base = current_base->base_buffer;
14497 if (window_base->base_buffer)
14498 window_base = window_base->base_buffer;
14499 if (current_base == window_base)
14500 buffer_shared++;
14501 }
14502
14503 /* Point refers normally to the selected window. For any other
14504 window, set up appropriate value. */
14505 if (!EQ (window, selected_window))
14506 {
14507 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14508 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14509 if (new_pt < BEGV)
14510 {
14511 new_pt = BEGV;
14512 new_pt_byte = BEGV_BYTE;
14513 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14514 }
14515 else if (new_pt > (ZV - 1))
14516 {
14517 new_pt = ZV;
14518 new_pt_byte = ZV_BYTE;
14519 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14520 }
14521
14522 /* We don't use SET_PT so that the point-motion hooks don't run. */
14523 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14524 }
14525
14526 /* If any of the character widths specified in the display table
14527 have changed, invalidate the width run cache. It's true that
14528 this may be a bit late to catch such changes, but the rest of
14529 redisplay goes (non-fatally) haywire when the display table is
14530 changed, so why should we worry about doing any better? */
14531 if (current_buffer->width_run_cache)
14532 {
14533 struct Lisp_Char_Table *disptab = buffer_display_table ();
14534
14535 if (! disptab_matches_widthtab (disptab,
14536 XVECTOR (BVAR (current_buffer, width_table))))
14537 {
14538 invalidate_region_cache (current_buffer,
14539 current_buffer->width_run_cache,
14540 BEG, Z);
14541 recompute_width_table (current_buffer, disptab);
14542 }
14543 }
14544
14545 /* If window-start is screwed up, choose a new one. */
14546 if (XMARKER (w->start)->buffer != current_buffer)
14547 goto recenter;
14548
14549 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14550
14551 /* If someone specified a new starting point but did not insist,
14552 check whether it can be used. */
14553 if (!NILP (w->optional_new_start)
14554 && CHARPOS (startp) >= BEGV
14555 && CHARPOS (startp) <= ZV)
14556 {
14557 w->optional_new_start = Qnil;
14558 start_display (&it, w, startp);
14559 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14560 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14561 if (IT_CHARPOS (it) == PT)
14562 w->force_start = Qt;
14563 /* IT may overshoot PT if text at PT is invisible. */
14564 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14565 w->force_start = Qt;
14566 }
14567
14568 force_start:
14569
14570 /* Handle case where place to start displaying has been specified,
14571 unless the specified location is outside the accessible range. */
14572 if (!NILP (w->force_start)
14573 || w->frozen_window_start_p)
14574 {
14575 /* We set this later on if we have to adjust point. */
14576 int new_vpos = -1;
14577
14578 w->force_start = Qnil;
14579 w->vscroll = 0;
14580 w->window_end_valid = Qnil;
14581
14582 /* Forget any recorded base line for line number display. */
14583 if (!buffer_unchanged_p)
14584 w->base_line_number = Qnil;
14585
14586 /* Redisplay the mode line. Select the buffer properly for that.
14587 Also, run the hook window-scroll-functions
14588 because we have scrolled. */
14589 /* Note, we do this after clearing force_start because
14590 if there's an error, it is better to forget about force_start
14591 than to get into an infinite loop calling the hook functions
14592 and having them get more errors. */
14593 if (!update_mode_line
14594 || ! NILP (Vwindow_scroll_functions))
14595 {
14596 update_mode_line = 1;
14597 w->update_mode_line = Qt;
14598 startp = run_window_scroll_functions (window, startp);
14599 }
14600
14601 w->last_modified = make_number (0);
14602 w->last_overlay_modified = make_number (0);
14603 if (CHARPOS (startp) < BEGV)
14604 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14605 else if (CHARPOS (startp) > ZV)
14606 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14607
14608 /* Redisplay, then check if cursor has been set during the
14609 redisplay. Give up if new fonts were loaded. */
14610 /* We used to issue a CHECK_MARGINS argument to try_window here,
14611 but this causes scrolling to fail when point begins inside
14612 the scroll margin (bug#148) -- cyd */
14613 if (!try_window (window, startp, 0))
14614 {
14615 w->force_start = Qt;
14616 clear_glyph_matrix (w->desired_matrix);
14617 goto need_larger_matrices;
14618 }
14619
14620 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14621 {
14622 /* If point does not appear, try to move point so it does
14623 appear. The desired matrix has been built above, so we
14624 can use it here. */
14625 new_vpos = window_box_height (w) / 2;
14626 }
14627
14628 if (!cursor_row_fully_visible_p (w, 0, 0))
14629 {
14630 /* Point does appear, but on a line partly visible at end of window.
14631 Move it back to a fully-visible line. */
14632 new_vpos = window_box_height (w);
14633 }
14634
14635 /* If we need to move point for either of the above reasons,
14636 now actually do it. */
14637 if (new_vpos >= 0)
14638 {
14639 struct glyph_row *row;
14640
14641 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14642 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14643 ++row;
14644
14645 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14646 MATRIX_ROW_START_BYTEPOS (row));
14647
14648 if (w != XWINDOW (selected_window))
14649 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14650 else if (current_buffer == old)
14651 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14652
14653 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14654
14655 /* If we are highlighting the region, then we just changed
14656 the region, so redisplay to show it. */
14657 if (!NILP (Vtransient_mark_mode)
14658 && !NILP (BVAR (current_buffer, mark_active)))
14659 {
14660 clear_glyph_matrix (w->desired_matrix);
14661 if (!try_window (window, startp, 0))
14662 goto need_larger_matrices;
14663 }
14664 }
14665
14666 #if GLYPH_DEBUG
14667 debug_method_add (w, "forced window start");
14668 #endif
14669 goto done;
14670 }
14671
14672 /* Handle case where text has not changed, only point, and it has
14673 not moved off the frame, and we are not retrying after hscroll.
14674 (current_matrix_up_to_date_p is nonzero when retrying.) */
14675 if (current_matrix_up_to_date_p
14676 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14677 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14678 {
14679 switch (rc)
14680 {
14681 case CURSOR_MOVEMENT_SUCCESS:
14682 used_current_matrix_p = 1;
14683 goto done;
14684
14685 case CURSOR_MOVEMENT_MUST_SCROLL:
14686 goto try_to_scroll;
14687
14688 default:
14689 abort ();
14690 }
14691 }
14692 /* If current starting point was originally the beginning of a line
14693 but no longer is, find a new starting point. */
14694 else if (!NILP (w->start_at_line_beg)
14695 && !(CHARPOS (startp) <= BEGV
14696 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14697 {
14698 #if GLYPH_DEBUG
14699 debug_method_add (w, "recenter 1");
14700 #endif
14701 goto recenter;
14702 }
14703
14704 /* Try scrolling with try_window_id. Value is > 0 if update has
14705 been done, it is -1 if we know that the same window start will
14706 not work. It is 0 if unsuccessful for some other reason. */
14707 else if ((tem = try_window_id (w)) != 0)
14708 {
14709 #if GLYPH_DEBUG
14710 debug_method_add (w, "try_window_id %d", tem);
14711 #endif
14712
14713 if (fonts_changed_p)
14714 goto need_larger_matrices;
14715 if (tem > 0)
14716 goto done;
14717
14718 /* Otherwise try_window_id has returned -1 which means that we
14719 don't want the alternative below this comment to execute. */
14720 }
14721 else if (CHARPOS (startp) >= BEGV
14722 && CHARPOS (startp) <= ZV
14723 && PT >= CHARPOS (startp)
14724 && (CHARPOS (startp) < ZV
14725 /* Avoid starting at end of buffer. */
14726 || CHARPOS (startp) == BEGV
14727 || (XFASTINT (w->last_modified) >= MODIFF
14728 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14729 {
14730
14731 /* If first window line is a continuation line, and window start
14732 is inside the modified region, but the first change is before
14733 current window start, we must select a new window start.
14734
14735 However, if this is the result of a down-mouse event (e.g. by
14736 extending the mouse-drag-overlay), we don't want to select a
14737 new window start, since that would change the position under
14738 the mouse, resulting in an unwanted mouse-movement rather
14739 than a simple mouse-click. */
14740 if (NILP (w->start_at_line_beg)
14741 && NILP (do_mouse_tracking)
14742 && CHARPOS (startp) > BEGV
14743 && CHARPOS (startp) > BEG + beg_unchanged
14744 && CHARPOS (startp) <= Z - end_unchanged
14745 /* Even if w->start_at_line_beg is nil, a new window may
14746 start at a line_beg, since that's how set_buffer_window
14747 sets it. So, we need to check the return value of
14748 compute_window_start_on_continuation_line. (See also
14749 bug#197). */
14750 && XMARKER (w->start)->buffer == current_buffer
14751 && compute_window_start_on_continuation_line (w))
14752 {
14753 w->force_start = Qt;
14754 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14755 goto force_start;
14756 }
14757
14758 #if GLYPH_DEBUG
14759 debug_method_add (w, "same window start");
14760 #endif
14761
14762 /* Try to redisplay starting at same place as before.
14763 If point has not moved off frame, accept the results. */
14764 if (!current_matrix_up_to_date_p
14765 /* Don't use try_window_reusing_current_matrix in this case
14766 because a window scroll function can have changed the
14767 buffer. */
14768 || !NILP (Vwindow_scroll_functions)
14769 || MINI_WINDOW_P (w)
14770 || !(used_current_matrix_p
14771 = try_window_reusing_current_matrix (w)))
14772 {
14773 IF_DEBUG (debug_method_add (w, "1"));
14774 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14775 /* -1 means we need to scroll.
14776 0 means we need new matrices, but fonts_changed_p
14777 is set in that case, so we will detect it below. */
14778 goto try_to_scroll;
14779 }
14780
14781 if (fonts_changed_p)
14782 goto need_larger_matrices;
14783
14784 if (w->cursor.vpos >= 0)
14785 {
14786 if (!just_this_one_p
14787 || current_buffer->clip_changed
14788 || BEG_UNCHANGED < CHARPOS (startp))
14789 /* Forget any recorded base line for line number display. */
14790 w->base_line_number = Qnil;
14791
14792 if (!cursor_row_fully_visible_p (w, 1, 0))
14793 {
14794 clear_glyph_matrix (w->desired_matrix);
14795 last_line_misfit = 1;
14796 }
14797 /* Drop through and scroll. */
14798 else
14799 goto done;
14800 }
14801 else
14802 clear_glyph_matrix (w->desired_matrix);
14803 }
14804
14805 try_to_scroll:
14806
14807 w->last_modified = make_number (0);
14808 w->last_overlay_modified = make_number (0);
14809
14810 /* Redisplay the mode line. Select the buffer properly for that. */
14811 if (!update_mode_line)
14812 {
14813 update_mode_line = 1;
14814 w->update_mode_line = Qt;
14815 }
14816
14817 /* Try to scroll by specified few lines. */
14818 if ((scroll_conservatively
14819 || emacs_scroll_step
14820 || temp_scroll_step
14821 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14822 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14823 && CHARPOS (startp) >= BEGV
14824 && CHARPOS (startp) <= ZV)
14825 {
14826 /* The function returns -1 if new fonts were loaded, 1 if
14827 successful, 0 if not successful. */
14828 int ss = try_scrolling (window, just_this_one_p,
14829 scroll_conservatively,
14830 emacs_scroll_step,
14831 temp_scroll_step, last_line_misfit);
14832 switch (ss)
14833 {
14834 case SCROLLING_SUCCESS:
14835 goto done;
14836
14837 case SCROLLING_NEED_LARGER_MATRICES:
14838 goto need_larger_matrices;
14839
14840 case SCROLLING_FAILED:
14841 break;
14842
14843 default:
14844 abort ();
14845 }
14846 }
14847
14848 /* Finally, just choose a place to start which positions point
14849 according to user preferences. */
14850
14851 recenter:
14852
14853 #if GLYPH_DEBUG
14854 debug_method_add (w, "recenter");
14855 #endif
14856
14857 /* w->vscroll = 0; */
14858
14859 /* Forget any previously recorded base line for line number display. */
14860 if (!buffer_unchanged_p)
14861 w->base_line_number = Qnil;
14862
14863 /* Determine the window start relative to point. */
14864 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14865 it.current_y = it.last_visible_y;
14866 if (centering_position < 0)
14867 {
14868 int margin =
14869 scroll_margin > 0
14870 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14871 : 0;
14872 EMACS_INT margin_pos = CHARPOS (startp);
14873 int scrolling_up;
14874 Lisp_Object aggressive;
14875
14876 /* If there is a scroll margin at the top of the window, find
14877 its character position. */
14878 if (margin
14879 /* Cannot call start_display if startp is not in the
14880 accessible region of the buffer. This can happen when we
14881 have just switched to a different buffer and/or changed
14882 its restriction. In that case, startp is initialized to
14883 the character position 1 (BEG) because we did not yet
14884 have chance to display the buffer even once. */
14885 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14886 {
14887 struct it it1;
14888
14889 start_display (&it1, w, startp);
14890 move_it_vertically (&it1, margin);
14891 margin_pos = IT_CHARPOS (it1);
14892 }
14893 scrolling_up = PT > margin_pos;
14894 aggressive =
14895 scrolling_up
14896 ? BVAR (current_buffer, scroll_up_aggressively)
14897 : BVAR (current_buffer, scroll_down_aggressively);
14898
14899 if (!MINI_WINDOW_P (w)
14900 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
14901 {
14902 int pt_offset = 0;
14903
14904 /* Setting scroll-conservatively overrides
14905 scroll-*-aggressively. */
14906 if (!scroll_conservatively && NUMBERP (aggressive))
14907 {
14908 double float_amount = XFLOATINT (aggressive);
14909
14910 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14911 if (pt_offset == 0 && float_amount > 0)
14912 pt_offset = 1;
14913 if (pt_offset)
14914 margin -= 1;
14915 }
14916 /* Compute how much to move the window start backward from
14917 point so that point will be displayed where the user
14918 wants it. */
14919 if (scrolling_up)
14920 {
14921 centering_position = it.last_visible_y;
14922 if (pt_offset)
14923 centering_position -= pt_offset;
14924 centering_position -=
14925 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
14926 /* Don't let point enter the scroll margin near top of
14927 the window. */
14928 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
14929 centering_position = margin * FRAME_LINE_HEIGHT (f);
14930 }
14931 else
14932 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
14933 }
14934 else
14935 /* Set the window start half the height of the window backward
14936 from point. */
14937 centering_position = window_box_height (w) / 2;
14938 }
14939 move_it_vertically_backward (&it, centering_position);
14940
14941 xassert (IT_CHARPOS (it) >= BEGV);
14942
14943 /* The function move_it_vertically_backward may move over more
14944 than the specified y-distance. If it->w is small, e.g. a
14945 mini-buffer window, we may end up in front of the window's
14946 display area. Start displaying at the start of the line
14947 containing PT in this case. */
14948 if (it.current_y <= 0)
14949 {
14950 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14951 move_it_vertically_backward (&it, 0);
14952 it.current_y = 0;
14953 }
14954
14955 it.current_x = it.hpos = 0;
14956
14957 /* Set the window start position here explicitly, to avoid an
14958 infinite loop in case the functions in window-scroll-functions
14959 get errors. */
14960 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14961
14962 /* Run scroll hooks. */
14963 startp = run_window_scroll_functions (window, it.current.pos);
14964
14965 /* Redisplay the window. */
14966 if (!current_matrix_up_to_date_p
14967 || windows_or_buffers_changed
14968 || cursor_type_changed
14969 /* Don't use try_window_reusing_current_matrix in this case
14970 because it can have changed the buffer. */
14971 || !NILP (Vwindow_scroll_functions)
14972 || !just_this_one_p
14973 || MINI_WINDOW_P (w)
14974 || !(used_current_matrix_p
14975 = try_window_reusing_current_matrix (w)))
14976 try_window (window, startp, 0);
14977
14978 /* If new fonts have been loaded (due to fontsets), give up. We
14979 have to start a new redisplay since we need to re-adjust glyph
14980 matrices. */
14981 if (fonts_changed_p)
14982 goto need_larger_matrices;
14983
14984 /* If cursor did not appear assume that the middle of the window is
14985 in the first line of the window. Do it again with the next line.
14986 (Imagine a window of height 100, displaying two lines of height
14987 60. Moving back 50 from it->last_visible_y will end in the first
14988 line.) */
14989 if (w->cursor.vpos < 0)
14990 {
14991 if (!NILP (w->window_end_valid)
14992 && PT >= Z - XFASTINT (w->window_end_pos))
14993 {
14994 clear_glyph_matrix (w->desired_matrix);
14995 move_it_by_lines (&it, 1);
14996 try_window (window, it.current.pos, 0);
14997 }
14998 else if (PT < IT_CHARPOS (it))
14999 {
15000 clear_glyph_matrix (w->desired_matrix);
15001 move_it_by_lines (&it, -1);
15002 try_window (window, it.current.pos, 0);
15003 }
15004 else
15005 {
15006 /* Not much we can do about it. */
15007 }
15008 }
15009
15010 /* Consider the following case: Window starts at BEGV, there is
15011 invisible, intangible text at BEGV, so that display starts at
15012 some point START > BEGV. It can happen that we are called with
15013 PT somewhere between BEGV and START. Try to handle that case. */
15014 if (w->cursor.vpos < 0)
15015 {
15016 struct glyph_row *row = w->current_matrix->rows;
15017 if (row->mode_line_p)
15018 ++row;
15019 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15020 }
15021
15022 if (!cursor_row_fully_visible_p (w, 0, 0))
15023 {
15024 /* If vscroll is enabled, disable it and try again. */
15025 if (w->vscroll)
15026 {
15027 w->vscroll = 0;
15028 clear_glyph_matrix (w->desired_matrix);
15029 goto recenter;
15030 }
15031
15032 /* If centering point failed to make the whole line visible,
15033 put point at the top instead. That has to make the whole line
15034 visible, if it can be done. */
15035 if (centering_position == 0)
15036 goto done;
15037
15038 clear_glyph_matrix (w->desired_matrix);
15039 centering_position = 0;
15040 goto recenter;
15041 }
15042
15043 done:
15044
15045 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15046 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15047 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15048 ? Qt : Qnil);
15049
15050 /* Display the mode line, if we must. */
15051 if ((update_mode_line
15052 /* If window not full width, must redo its mode line
15053 if (a) the window to its side is being redone and
15054 (b) we do a frame-based redisplay. This is a consequence
15055 of how inverted lines are drawn in frame-based redisplay. */
15056 || (!just_this_one_p
15057 && !FRAME_WINDOW_P (f)
15058 && !WINDOW_FULL_WIDTH_P (w))
15059 /* Line number to display. */
15060 || INTEGERP (w->base_line_pos)
15061 /* Column number is displayed and different from the one displayed. */
15062 || (!NILP (w->column_number_displayed)
15063 && (XFASTINT (w->column_number_displayed) != current_column ())))
15064 /* This means that the window has a mode line. */
15065 && (WINDOW_WANTS_MODELINE_P (w)
15066 || WINDOW_WANTS_HEADER_LINE_P (w)))
15067 {
15068 display_mode_lines (w);
15069
15070 /* If mode line height has changed, arrange for a thorough
15071 immediate redisplay using the correct mode line height. */
15072 if (WINDOW_WANTS_MODELINE_P (w)
15073 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15074 {
15075 fonts_changed_p = 1;
15076 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15077 = DESIRED_MODE_LINE_HEIGHT (w);
15078 }
15079
15080 /* If header line height has changed, arrange for a thorough
15081 immediate redisplay using the correct header line height. */
15082 if (WINDOW_WANTS_HEADER_LINE_P (w)
15083 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15084 {
15085 fonts_changed_p = 1;
15086 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15087 = DESIRED_HEADER_LINE_HEIGHT (w);
15088 }
15089
15090 if (fonts_changed_p)
15091 goto need_larger_matrices;
15092 }
15093
15094 if (!line_number_displayed
15095 && !BUFFERP (w->base_line_pos))
15096 {
15097 w->base_line_pos = Qnil;
15098 w->base_line_number = Qnil;
15099 }
15100
15101 finish_menu_bars:
15102
15103 /* When we reach a frame's selected window, redo the frame's menu bar. */
15104 if (update_mode_line
15105 && EQ (FRAME_SELECTED_WINDOW (f), window))
15106 {
15107 int redisplay_menu_p = 0;
15108
15109 if (FRAME_WINDOW_P (f))
15110 {
15111 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15112 || defined (HAVE_NS) || defined (USE_GTK)
15113 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15114 #else
15115 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15116 #endif
15117 }
15118 else
15119 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15120
15121 if (redisplay_menu_p)
15122 display_menu_bar (w);
15123
15124 #ifdef HAVE_WINDOW_SYSTEM
15125 if (FRAME_WINDOW_P (f))
15126 {
15127 #if defined (USE_GTK) || defined (HAVE_NS)
15128 if (FRAME_EXTERNAL_TOOL_BAR (f))
15129 redisplay_tool_bar (f);
15130 #else
15131 if (WINDOWP (f->tool_bar_window)
15132 && (FRAME_TOOL_BAR_LINES (f) > 0
15133 || !NILP (Vauto_resize_tool_bars))
15134 && redisplay_tool_bar (f))
15135 ignore_mouse_drag_p = 1;
15136 #endif
15137 }
15138 #endif
15139 }
15140
15141 #ifdef HAVE_WINDOW_SYSTEM
15142 if (FRAME_WINDOW_P (f)
15143 && update_window_fringes (w, (just_this_one_p
15144 || (!used_current_matrix_p && !overlay_arrow_seen)
15145 || w->pseudo_window_p)))
15146 {
15147 update_begin (f);
15148 BLOCK_INPUT;
15149 if (draw_window_fringes (w, 1))
15150 x_draw_vertical_border (w);
15151 UNBLOCK_INPUT;
15152 update_end (f);
15153 }
15154 #endif /* HAVE_WINDOW_SYSTEM */
15155
15156 /* We go to this label, with fonts_changed_p nonzero,
15157 if it is necessary to try again using larger glyph matrices.
15158 We have to redeem the scroll bar even in this case,
15159 because the loop in redisplay_internal expects that. */
15160 need_larger_matrices:
15161 ;
15162 finish_scroll_bars:
15163
15164 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15165 {
15166 /* Set the thumb's position and size. */
15167 set_vertical_scroll_bar (w);
15168
15169 /* Note that we actually used the scroll bar attached to this
15170 window, so it shouldn't be deleted at the end of redisplay. */
15171 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15172 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15173 }
15174
15175 /* Restore current_buffer and value of point in it. The window
15176 update may have changed the buffer, so first make sure `opoint'
15177 is still valid (Bug#6177). */
15178 if (CHARPOS (opoint) < BEGV)
15179 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15180 else if (CHARPOS (opoint) > ZV)
15181 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15182 else
15183 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15184
15185 set_buffer_internal_1 (old);
15186 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15187 shorter. This can be caused by log truncation in *Messages*. */
15188 if (CHARPOS (lpoint) <= ZV)
15189 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15190
15191 unbind_to (count, Qnil);
15192 }
15193
15194
15195 /* Build the complete desired matrix of WINDOW with a window start
15196 buffer position POS.
15197
15198 Value is 1 if successful. It is zero if fonts were loaded during
15199 redisplay which makes re-adjusting glyph matrices necessary, and -1
15200 if point would appear in the scroll margins.
15201 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15202 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15203 set in FLAGS.) */
15204
15205 int
15206 try_window (Lisp_Object window, struct text_pos pos, int flags)
15207 {
15208 struct window *w = XWINDOW (window);
15209 struct it it;
15210 struct glyph_row *last_text_row = NULL;
15211 struct frame *f = XFRAME (w->frame);
15212
15213 /* Make POS the new window start. */
15214 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15215
15216 /* Mark cursor position as unknown. No overlay arrow seen. */
15217 w->cursor.vpos = -1;
15218 overlay_arrow_seen = 0;
15219
15220 /* Initialize iterator and info to start at POS. */
15221 start_display (&it, w, pos);
15222
15223 /* Display all lines of W. */
15224 while (it.current_y < it.last_visible_y)
15225 {
15226 if (display_line (&it))
15227 last_text_row = it.glyph_row - 1;
15228 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15229 return 0;
15230 }
15231
15232 /* Don't let the cursor end in the scroll margins. */
15233 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15234 && !MINI_WINDOW_P (w))
15235 {
15236 int this_scroll_margin;
15237
15238 if (scroll_margin > 0)
15239 {
15240 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15241 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15242 }
15243 else
15244 this_scroll_margin = 0;
15245
15246 if ((w->cursor.y >= 0 /* not vscrolled */
15247 && w->cursor.y < this_scroll_margin
15248 && CHARPOS (pos) > BEGV
15249 && IT_CHARPOS (it) < ZV)
15250 /* rms: considering make_cursor_line_fully_visible_p here
15251 seems to give wrong results. We don't want to recenter
15252 when the last line is partly visible, we want to allow
15253 that case to be handled in the usual way. */
15254 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15255 {
15256 w->cursor.vpos = -1;
15257 clear_glyph_matrix (w->desired_matrix);
15258 return -1;
15259 }
15260 }
15261
15262 /* If bottom moved off end of frame, change mode line percentage. */
15263 if (XFASTINT (w->window_end_pos) <= 0
15264 && Z != IT_CHARPOS (it))
15265 w->update_mode_line = Qt;
15266
15267 /* Set window_end_pos to the offset of the last character displayed
15268 on the window from the end of current_buffer. Set
15269 window_end_vpos to its row number. */
15270 if (last_text_row)
15271 {
15272 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15273 w->window_end_bytepos
15274 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15275 w->window_end_pos
15276 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15277 w->window_end_vpos
15278 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15279 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15280 ->displays_text_p);
15281 }
15282 else
15283 {
15284 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15285 w->window_end_pos = make_number (Z - ZV);
15286 w->window_end_vpos = make_number (0);
15287 }
15288
15289 /* But that is not valid info until redisplay finishes. */
15290 w->window_end_valid = Qnil;
15291 return 1;
15292 }
15293
15294
15295 \f
15296 /************************************************************************
15297 Window redisplay reusing current matrix when buffer has not changed
15298 ************************************************************************/
15299
15300 /* Try redisplay of window W showing an unchanged buffer with a
15301 different window start than the last time it was displayed by
15302 reusing its current matrix. Value is non-zero if successful.
15303 W->start is the new window start. */
15304
15305 static int
15306 try_window_reusing_current_matrix (struct window *w)
15307 {
15308 struct frame *f = XFRAME (w->frame);
15309 struct glyph_row *bottom_row;
15310 struct it it;
15311 struct run run;
15312 struct text_pos start, new_start;
15313 int nrows_scrolled, i;
15314 struct glyph_row *last_text_row;
15315 struct glyph_row *last_reused_text_row;
15316 struct glyph_row *start_row;
15317 int start_vpos, min_y, max_y;
15318
15319 #if GLYPH_DEBUG
15320 if (inhibit_try_window_reusing)
15321 return 0;
15322 #endif
15323
15324 if (/* This function doesn't handle terminal frames. */
15325 !FRAME_WINDOW_P (f)
15326 /* Don't try to reuse the display if windows have been split
15327 or such. */
15328 || windows_or_buffers_changed
15329 || cursor_type_changed)
15330 return 0;
15331
15332 /* Can't do this if region may have changed. */
15333 if ((!NILP (Vtransient_mark_mode)
15334 && !NILP (BVAR (current_buffer, mark_active)))
15335 || !NILP (w->region_showing)
15336 || !NILP (Vshow_trailing_whitespace))
15337 return 0;
15338
15339 /* If top-line visibility has changed, give up. */
15340 if (WINDOW_WANTS_HEADER_LINE_P (w)
15341 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15342 return 0;
15343
15344 /* Give up if old or new display is scrolled vertically. We could
15345 make this function handle this, but right now it doesn't. */
15346 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15347 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15348 return 0;
15349
15350 /* The variable new_start now holds the new window start. The old
15351 start `start' can be determined from the current matrix. */
15352 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15353 start = start_row->minpos;
15354 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15355
15356 /* Clear the desired matrix for the display below. */
15357 clear_glyph_matrix (w->desired_matrix);
15358
15359 if (CHARPOS (new_start) <= CHARPOS (start))
15360 {
15361 /* Don't use this method if the display starts with an ellipsis
15362 displayed for invisible text. It's not easy to handle that case
15363 below, and it's certainly not worth the effort since this is
15364 not a frequent case. */
15365 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15366 return 0;
15367
15368 IF_DEBUG (debug_method_add (w, "twu1"));
15369
15370 /* Display up to a row that can be reused. The variable
15371 last_text_row is set to the last row displayed that displays
15372 text. Note that it.vpos == 0 if or if not there is a
15373 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15374 start_display (&it, w, new_start);
15375 w->cursor.vpos = -1;
15376 last_text_row = last_reused_text_row = NULL;
15377
15378 while (it.current_y < it.last_visible_y
15379 && !fonts_changed_p)
15380 {
15381 /* If we have reached into the characters in the START row,
15382 that means the line boundaries have changed. So we
15383 can't start copying with the row START. Maybe it will
15384 work to start copying with the following row. */
15385 while (IT_CHARPOS (it) > CHARPOS (start))
15386 {
15387 /* Advance to the next row as the "start". */
15388 start_row++;
15389 start = start_row->minpos;
15390 /* If there are no more rows to try, or just one, give up. */
15391 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15392 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15393 || CHARPOS (start) == ZV)
15394 {
15395 clear_glyph_matrix (w->desired_matrix);
15396 return 0;
15397 }
15398
15399 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15400 }
15401 /* If we have reached alignment,
15402 we can copy the rest of the rows. */
15403 if (IT_CHARPOS (it) == CHARPOS (start))
15404 break;
15405
15406 if (display_line (&it))
15407 last_text_row = it.glyph_row - 1;
15408 }
15409
15410 /* A value of current_y < last_visible_y means that we stopped
15411 at the previous window start, which in turn means that we
15412 have at least one reusable row. */
15413 if (it.current_y < it.last_visible_y)
15414 {
15415 struct glyph_row *row;
15416
15417 /* IT.vpos always starts from 0; it counts text lines. */
15418 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15419
15420 /* Find PT if not already found in the lines displayed. */
15421 if (w->cursor.vpos < 0)
15422 {
15423 int dy = it.current_y - start_row->y;
15424
15425 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15426 row = row_containing_pos (w, PT, row, NULL, dy);
15427 if (row)
15428 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15429 dy, nrows_scrolled);
15430 else
15431 {
15432 clear_glyph_matrix (w->desired_matrix);
15433 return 0;
15434 }
15435 }
15436
15437 /* Scroll the display. Do it before the current matrix is
15438 changed. The problem here is that update has not yet
15439 run, i.e. part of the current matrix is not up to date.
15440 scroll_run_hook will clear the cursor, and use the
15441 current matrix to get the height of the row the cursor is
15442 in. */
15443 run.current_y = start_row->y;
15444 run.desired_y = it.current_y;
15445 run.height = it.last_visible_y - it.current_y;
15446
15447 if (run.height > 0 && run.current_y != run.desired_y)
15448 {
15449 update_begin (f);
15450 FRAME_RIF (f)->update_window_begin_hook (w);
15451 FRAME_RIF (f)->clear_window_mouse_face (w);
15452 FRAME_RIF (f)->scroll_run_hook (w, &run);
15453 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15454 update_end (f);
15455 }
15456
15457 /* Shift current matrix down by nrows_scrolled lines. */
15458 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15459 rotate_matrix (w->current_matrix,
15460 start_vpos,
15461 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15462 nrows_scrolled);
15463
15464 /* Disable lines that must be updated. */
15465 for (i = 0; i < nrows_scrolled; ++i)
15466 (start_row + i)->enabled_p = 0;
15467
15468 /* Re-compute Y positions. */
15469 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15470 max_y = it.last_visible_y;
15471 for (row = start_row + nrows_scrolled;
15472 row < bottom_row;
15473 ++row)
15474 {
15475 row->y = it.current_y;
15476 row->visible_height = row->height;
15477
15478 if (row->y < min_y)
15479 row->visible_height -= min_y - row->y;
15480 if (row->y + row->height > max_y)
15481 row->visible_height -= row->y + row->height - max_y;
15482 row->redraw_fringe_bitmaps_p = 1;
15483
15484 it.current_y += row->height;
15485
15486 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15487 last_reused_text_row = row;
15488 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15489 break;
15490 }
15491
15492 /* Disable lines in the current matrix which are now
15493 below the window. */
15494 for (++row; row < bottom_row; ++row)
15495 row->enabled_p = row->mode_line_p = 0;
15496 }
15497
15498 /* Update window_end_pos etc.; last_reused_text_row is the last
15499 reused row from the current matrix containing text, if any.
15500 The value of last_text_row is the last displayed line
15501 containing text. */
15502 if (last_reused_text_row)
15503 {
15504 w->window_end_bytepos
15505 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15506 w->window_end_pos
15507 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15508 w->window_end_vpos
15509 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15510 w->current_matrix));
15511 }
15512 else if (last_text_row)
15513 {
15514 w->window_end_bytepos
15515 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15516 w->window_end_pos
15517 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15518 w->window_end_vpos
15519 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15520 }
15521 else
15522 {
15523 /* This window must be completely empty. */
15524 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15525 w->window_end_pos = make_number (Z - ZV);
15526 w->window_end_vpos = make_number (0);
15527 }
15528 w->window_end_valid = Qnil;
15529
15530 /* Update hint: don't try scrolling again in update_window. */
15531 w->desired_matrix->no_scrolling_p = 1;
15532
15533 #if GLYPH_DEBUG
15534 debug_method_add (w, "try_window_reusing_current_matrix 1");
15535 #endif
15536 return 1;
15537 }
15538 else if (CHARPOS (new_start) > CHARPOS (start))
15539 {
15540 struct glyph_row *pt_row, *row;
15541 struct glyph_row *first_reusable_row;
15542 struct glyph_row *first_row_to_display;
15543 int dy;
15544 int yb = window_text_bottom_y (w);
15545
15546 /* Find the row starting at new_start, if there is one. Don't
15547 reuse a partially visible line at the end. */
15548 first_reusable_row = start_row;
15549 while (first_reusable_row->enabled_p
15550 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15551 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15552 < CHARPOS (new_start)))
15553 ++first_reusable_row;
15554
15555 /* Give up if there is no row to reuse. */
15556 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15557 || !first_reusable_row->enabled_p
15558 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15559 != CHARPOS (new_start)))
15560 return 0;
15561
15562 /* We can reuse fully visible rows beginning with
15563 first_reusable_row to the end of the window. Set
15564 first_row_to_display to the first row that cannot be reused.
15565 Set pt_row to the row containing point, if there is any. */
15566 pt_row = NULL;
15567 for (first_row_to_display = first_reusable_row;
15568 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15569 ++first_row_to_display)
15570 {
15571 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15572 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15573 pt_row = first_row_to_display;
15574 }
15575
15576 /* Start displaying at the start of first_row_to_display. */
15577 xassert (first_row_to_display->y < yb);
15578 init_to_row_start (&it, w, first_row_to_display);
15579
15580 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15581 - start_vpos);
15582 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15583 - nrows_scrolled);
15584 it.current_y = (first_row_to_display->y - first_reusable_row->y
15585 + WINDOW_HEADER_LINE_HEIGHT (w));
15586
15587 /* Display lines beginning with first_row_to_display in the
15588 desired matrix. Set last_text_row to the last row displayed
15589 that displays text. */
15590 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15591 if (pt_row == NULL)
15592 w->cursor.vpos = -1;
15593 last_text_row = NULL;
15594 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15595 if (display_line (&it))
15596 last_text_row = it.glyph_row - 1;
15597
15598 /* If point is in a reused row, adjust y and vpos of the cursor
15599 position. */
15600 if (pt_row)
15601 {
15602 w->cursor.vpos -= nrows_scrolled;
15603 w->cursor.y -= first_reusable_row->y - start_row->y;
15604 }
15605
15606 /* Give up if point isn't in a row displayed or reused. (This
15607 also handles the case where w->cursor.vpos < nrows_scrolled
15608 after the calls to display_line, which can happen with scroll
15609 margins. See bug#1295.) */
15610 if (w->cursor.vpos < 0)
15611 {
15612 clear_glyph_matrix (w->desired_matrix);
15613 return 0;
15614 }
15615
15616 /* Scroll the display. */
15617 run.current_y = first_reusable_row->y;
15618 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15619 run.height = it.last_visible_y - run.current_y;
15620 dy = run.current_y - run.desired_y;
15621
15622 if (run.height)
15623 {
15624 update_begin (f);
15625 FRAME_RIF (f)->update_window_begin_hook (w);
15626 FRAME_RIF (f)->clear_window_mouse_face (w);
15627 FRAME_RIF (f)->scroll_run_hook (w, &run);
15628 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15629 update_end (f);
15630 }
15631
15632 /* Adjust Y positions of reused rows. */
15633 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15634 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15635 max_y = it.last_visible_y;
15636 for (row = first_reusable_row; row < first_row_to_display; ++row)
15637 {
15638 row->y -= dy;
15639 row->visible_height = row->height;
15640 if (row->y < min_y)
15641 row->visible_height -= min_y - row->y;
15642 if (row->y + row->height > max_y)
15643 row->visible_height -= row->y + row->height - max_y;
15644 row->redraw_fringe_bitmaps_p = 1;
15645 }
15646
15647 /* Scroll the current matrix. */
15648 xassert (nrows_scrolled > 0);
15649 rotate_matrix (w->current_matrix,
15650 start_vpos,
15651 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15652 -nrows_scrolled);
15653
15654 /* Disable rows not reused. */
15655 for (row -= nrows_scrolled; row < bottom_row; ++row)
15656 row->enabled_p = 0;
15657
15658 /* Point may have moved to a different line, so we cannot assume that
15659 the previous cursor position is valid; locate the correct row. */
15660 if (pt_row)
15661 {
15662 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15663 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15664 row++)
15665 {
15666 w->cursor.vpos++;
15667 w->cursor.y = row->y;
15668 }
15669 if (row < bottom_row)
15670 {
15671 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15672 struct glyph *end = glyph + row->used[TEXT_AREA];
15673
15674 /* Can't use this optimization with bidi-reordered glyph
15675 rows, unless cursor is already at point. */
15676 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15677 {
15678 if (!(w->cursor.hpos >= 0
15679 && w->cursor.hpos < row->used[TEXT_AREA]
15680 && BUFFERP (glyph->object)
15681 && glyph->charpos == PT))
15682 return 0;
15683 }
15684 else
15685 for (; glyph < end
15686 && (!BUFFERP (glyph->object)
15687 || glyph->charpos < PT);
15688 glyph++)
15689 {
15690 w->cursor.hpos++;
15691 w->cursor.x += glyph->pixel_width;
15692 }
15693 }
15694 }
15695
15696 /* Adjust window end. A null value of last_text_row means that
15697 the window end is in reused rows which in turn means that
15698 only its vpos can have changed. */
15699 if (last_text_row)
15700 {
15701 w->window_end_bytepos
15702 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15703 w->window_end_pos
15704 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15705 w->window_end_vpos
15706 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15707 }
15708 else
15709 {
15710 w->window_end_vpos
15711 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15712 }
15713
15714 w->window_end_valid = Qnil;
15715 w->desired_matrix->no_scrolling_p = 1;
15716
15717 #if GLYPH_DEBUG
15718 debug_method_add (w, "try_window_reusing_current_matrix 2");
15719 #endif
15720 return 1;
15721 }
15722
15723 return 0;
15724 }
15725
15726
15727 \f
15728 /************************************************************************
15729 Window redisplay reusing current matrix when buffer has changed
15730 ************************************************************************/
15731
15732 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15733 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15734 EMACS_INT *, EMACS_INT *);
15735 static struct glyph_row *
15736 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15737 struct glyph_row *);
15738
15739
15740 /* Return the last row in MATRIX displaying text. If row START is
15741 non-null, start searching with that row. IT gives the dimensions
15742 of the display. Value is null if matrix is empty; otherwise it is
15743 a pointer to the row found. */
15744
15745 static struct glyph_row *
15746 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15747 struct glyph_row *start)
15748 {
15749 struct glyph_row *row, *row_found;
15750
15751 /* Set row_found to the last row in IT->w's current matrix
15752 displaying text. The loop looks funny but think of partially
15753 visible lines. */
15754 row_found = NULL;
15755 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15756 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15757 {
15758 xassert (row->enabled_p);
15759 row_found = row;
15760 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15761 break;
15762 ++row;
15763 }
15764
15765 return row_found;
15766 }
15767
15768
15769 /* Return the last row in the current matrix of W that is not affected
15770 by changes at the start of current_buffer that occurred since W's
15771 current matrix was built. Value is null if no such row exists.
15772
15773 BEG_UNCHANGED us the number of characters unchanged at the start of
15774 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15775 first changed character in current_buffer. Characters at positions <
15776 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15777 when the current matrix was built. */
15778
15779 static struct glyph_row *
15780 find_last_unchanged_at_beg_row (struct window *w)
15781 {
15782 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15783 struct glyph_row *row;
15784 struct glyph_row *row_found = NULL;
15785 int yb = window_text_bottom_y (w);
15786
15787 /* Find the last row displaying unchanged text. */
15788 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15789 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15790 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15791 ++row)
15792 {
15793 if (/* If row ends before first_changed_pos, it is unchanged,
15794 except in some case. */
15795 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15796 /* When row ends in ZV and we write at ZV it is not
15797 unchanged. */
15798 && !row->ends_at_zv_p
15799 /* When first_changed_pos is the end of a continued line,
15800 row is not unchanged because it may be no longer
15801 continued. */
15802 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15803 && (row->continued_p
15804 || row->exact_window_width_line_p)))
15805 row_found = row;
15806
15807 /* Stop if last visible row. */
15808 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15809 break;
15810 }
15811
15812 return row_found;
15813 }
15814
15815
15816 /* Find the first glyph row in the current matrix of W that is not
15817 affected by changes at the end of current_buffer since the
15818 time W's current matrix was built.
15819
15820 Return in *DELTA the number of chars by which buffer positions in
15821 unchanged text at the end of current_buffer must be adjusted.
15822
15823 Return in *DELTA_BYTES the corresponding number of bytes.
15824
15825 Value is null if no such row exists, i.e. all rows are affected by
15826 changes. */
15827
15828 static struct glyph_row *
15829 find_first_unchanged_at_end_row (struct window *w,
15830 EMACS_INT *delta, EMACS_INT *delta_bytes)
15831 {
15832 struct glyph_row *row;
15833 struct glyph_row *row_found = NULL;
15834
15835 *delta = *delta_bytes = 0;
15836
15837 /* Display must not have been paused, otherwise the current matrix
15838 is not up to date. */
15839 eassert (!NILP (w->window_end_valid));
15840
15841 /* A value of window_end_pos >= END_UNCHANGED means that the window
15842 end is in the range of changed text. If so, there is no
15843 unchanged row at the end of W's current matrix. */
15844 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15845 return NULL;
15846
15847 /* Set row to the last row in W's current matrix displaying text. */
15848 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15849
15850 /* If matrix is entirely empty, no unchanged row exists. */
15851 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15852 {
15853 /* The value of row is the last glyph row in the matrix having a
15854 meaningful buffer position in it. The end position of row
15855 corresponds to window_end_pos. This allows us to translate
15856 buffer positions in the current matrix to current buffer
15857 positions for characters not in changed text. */
15858 EMACS_INT Z_old =
15859 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15860 EMACS_INT Z_BYTE_old =
15861 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15862 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15863 struct glyph_row *first_text_row
15864 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15865
15866 *delta = Z - Z_old;
15867 *delta_bytes = Z_BYTE - Z_BYTE_old;
15868
15869 /* Set last_unchanged_pos to the buffer position of the last
15870 character in the buffer that has not been changed. Z is the
15871 index + 1 of the last character in current_buffer, i.e. by
15872 subtracting END_UNCHANGED we get the index of the last
15873 unchanged character, and we have to add BEG to get its buffer
15874 position. */
15875 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15876 last_unchanged_pos_old = last_unchanged_pos - *delta;
15877
15878 /* Search backward from ROW for a row displaying a line that
15879 starts at a minimum position >= last_unchanged_pos_old. */
15880 for (; row > first_text_row; --row)
15881 {
15882 /* This used to abort, but it can happen.
15883 It is ok to just stop the search instead here. KFS. */
15884 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15885 break;
15886
15887 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15888 row_found = row;
15889 }
15890 }
15891
15892 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15893
15894 return row_found;
15895 }
15896
15897
15898 /* Make sure that glyph rows in the current matrix of window W
15899 reference the same glyph memory as corresponding rows in the
15900 frame's frame matrix. This function is called after scrolling W's
15901 current matrix on a terminal frame in try_window_id and
15902 try_window_reusing_current_matrix. */
15903
15904 static void
15905 sync_frame_with_window_matrix_rows (struct window *w)
15906 {
15907 struct frame *f = XFRAME (w->frame);
15908 struct glyph_row *window_row, *window_row_end, *frame_row;
15909
15910 /* Preconditions: W must be a leaf window and full-width. Its frame
15911 must have a frame matrix. */
15912 xassert (NILP (w->hchild) && NILP (w->vchild));
15913 xassert (WINDOW_FULL_WIDTH_P (w));
15914 xassert (!FRAME_WINDOW_P (f));
15915
15916 /* If W is a full-width window, glyph pointers in W's current matrix
15917 have, by definition, to be the same as glyph pointers in the
15918 corresponding frame matrix. Note that frame matrices have no
15919 marginal areas (see build_frame_matrix). */
15920 window_row = w->current_matrix->rows;
15921 window_row_end = window_row + w->current_matrix->nrows;
15922 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15923 while (window_row < window_row_end)
15924 {
15925 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15926 struct glyph *end = window_row->glyphs[LAST_AREA];
15927
15928 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15929 frame_row->glyphs[TEXT_AREA] = start;
15930 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15931 frame_row->glyphs[LAST_AREA] = end;
15932
15933 /* Disable frame rows whose corresponding window rows have
15934 been disabled in try_window_id. */
15935 if (!window_row->enabled_p)
15936 frame_row->enabled_p = 0;
15937
15938 ++window_row, ++frame_row;
15939 }
15940 }
15941
15942
15943 /* Find the glyph row in window W containing CHARPOS. Consider all
15944 rows between START and END (not inclusive). END null means search
15945 all rows to the end of the display area of W. Value is the row
15946 containing CHARPOS or null. */
15947
15948 struct glyph_row *
15949 row_containing_pos (struct window *w, EMACS_INT charpos,
15950 struct glyph_row *start, struct glyph_row *end, int dy)
15951 {
15952 struct glyph_row *row = start;
15953 struct glyph_row *best_row = NULL;
15954 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15955 int last_y;
15956
15957 /* If we happen to start on a header-line, skip that. */
15958 if (row->mode_line_p)
15959 ++row;
15960
15961 if ((end && row >= end) || !row->enabled_p)
15962 return NULL;
15963
15964 last_y = window_text_bottom_y (w) - dy;
15965
15966 while (1)
15967 {
15968 /* Give up if we have gone too far. */
15969 if (end && row >= end)
15970 return NULL;
15971 /* This formerly returned if they were equal.
15972 I think that both quantities are of a "last plus one" type;
15973 if so, when they are equal, the row is within the screen. -- rms. */
15974 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15975 return NULL;
15976
15977 /* If it is in this row, return this row. */
15978 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15979 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15980 /* The end position of a row equals the start
15981 position of the next row. If CHARPOS is there, we
15982 would rather display it in the next line, except
15983 when this line ends in ZV. */
15984 && !row->ends_at_zv_p
15985 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15986 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15987 {
15988 struct glyph *g;
15989
15990 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15991 || (!best_row && !row->continued_p))
15992 return row;
15993 /* In bidi-reordered rows, there could be several rows
15994 occluding point, all of them belonging to the same
15995 continued line. We need to find the row which fits
15996 CHARPOS the best. */
15997 for (g = row->glyphs[TEXT_AREA];
15998 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15999 g++)
16000 {
16001 if (!STRINGP (g->object))
16002 {
16003 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16004 {
16005 mindif = eabs (g->charpos - charpos);
16006 best_row = row;
16007 /* Exact match always wins. */
16008 if (mindif == 0)
16009 return best_row;
16010 }
16011 }
16012 }
16013 }
16014 else if (best_row && !row->continued_p)
16015 return best_row;
16016 ++row;
16017 }
16018 }
16019
16020
16021 /* Try to redisplay window W by reusing its existing display. W's
16022 current matrix must be up to date when this function is called,
16023 i.e. window_end_valid must not be nil.
16024
16025 Value is
16026
16027 1 if display has been updated
16028 0 if otherwise unsuccessful
16029 -1 if redisplay with same window start is known not to succeed
16030
16031 The following steps are performed:
16032
16033 1. Find the last row in the current matrix of W that is not
16034 affected by changes at the start of current_buffer. If no such row
16035 is found, give up.
16036
16037 2. Find the first row in W's current matrix that is not affected by
16038 changes at the end of current_buffer. Maybe there is no such row.
16039
16040 3. Display lines beginning with the row + 1 found in step 1 to the
16041 row found in step 2 or, if step 2 didn't find a row, to the end of
16042 the window.
16043
16044 4. If cursor is not known to appear on the window, give up.
16045
16046 5. If display stopped at the row found in step 2, scroll the
16047 display and current matrix as needed.
16048
16049 6. Maybe display some lines at the end of W, if we must. This can
16050 happen under various circumstances, like a partially visible line
16051 becoming fully visible, or because newly displayed lines are displayed
16052 in smaller font sizes.
16053
16054 7. Update W's window end information. */
16055
16056 static int
16057 try_window_id (struct window *w)
16058 {
16059 struct frame *f = XFRAME (w->frame);
16060 struct glyph_matrix *current_matrix = w->current_matrix;
16061 struct glyph_matrix *desired_matrix = w->desired_matrix;
16062 struct glyph_row *last_unchanged_at_beg_row;
16063 struct glyph_row *first_unchanged_at_end_row;
16064 struct glyph_row *row;
16065 struct glyph_row *bottom_row;
16066 int bottom_vpos;
16067 struct it it;
16068 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16069 int dvpos, dy;
16070 struct text_pos start_pos;
16071 struct run run;
16072 int first_unchanged_at_end_vpos = 0;
16073 struct glyph_row *last_text_row, *last_text_row_at_end;
16074 struct text_pos start;
16075 EMACS_INT first_changed_charpos, last_changed_charpos;
16076
16077 #if GLYPH_DEBUG
16078 if (inhibit_try_window_id)
16079 return 0;
16080 #endif
16081
16082 /* This is handy for debugging. */
16083 #if 0
16084 #define GIVE_UP(X) \
16085 do { \
16086 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16087 return 0; \
16088 } while (0)
16089 #else
16090 #define GIVE_UP(X) return 0
16091 #endif
16092
16093 SET_TEXT_POS_FROM_MARKER (start, w->start);
16094
16095 /* Don't use this for mini-windows because these can show
16096 messages and mini-buffers, and we don't handle that here. */
16097 if (MINI_WINDOW_P (w))
16098 GIVE_UP (1);
16099
16100 /* This flag is used to prevent redisplay optimizations. */
16101 if (windows_or_buffers_changed || cursor_type_changed)
16102 GIVE_UP (2);
16103
16104 /* Verify that narrowing has not changed.
16105 Also verify that we were not told to prevent redisplay optimizations.
16106 It would be nice to further
16107 reduce the number of cases where this prevents try_window_id. */
16108 if (current_buffer->clip_changed
16109 || current_buffer->prevent_redisplay_optimizations_p)
16110 GIVE_UP (3);
16111
16112 /* Window must either use window-based redisplay or be full width. */
16113 if (!FRAME_WINDOW_P (f)
16114 && (!FRAME_LINE_INS_DEL_OK (f)
16115 || !WINDOW_FULL_WIDTH_P (w)))
16116 GIVE_UP (4);
16117
16118 /* Give up if point is known NOT to appear in W. */
16119 if (PT < CHARPOS (start))
16120 GIVE_UP (5);
16121
16122 /* Another way to prevent redisplay optimizations. */
16123 if (XFASTINT (w->last_modified) == 0)
16124 GIVE_UP (6);
16125
16126 /* Verify that window is not hscrolled. */
16127 if (XFASTINT (w->hscroll) != 0)
16128 GIVE_UP (7);
16129
16130 /* Verify that display wasn't paused. */
16131 if (NILP (w->window_end_valid))
16132 GIVE_UP (8);
16133
16134 /* Can't use this if highlighting a region because a cursor movement
16135 will do more than just set the cursor. */
16136 if (!NILP (Vtransient_mark_mode)
16137 && !NILP (BVAR (current_buffer, mark_active)))
16138 GIVE_UP (9);
16139
16140 /* Likewise if highlighting trailing whitespace. */
16141 if (!NILP (Vshow_trailing_whitespace))
16142 GIVE_UP (11);
16143
16144 /* Likewise if showing a region. */
16145 if (!NILP (w->region_showing))
16146 GIVE_UP (10);
16147
16148 /* Can't use this if overlay arrow position and/or string have
16149 changed. */
16150 if (overlay_arrows_changed_p ())
16151 GIVE_UP (12);
16152
16153 /* When word-wrap is on, adding a space to the first word of a
16154 wrapped line can change the wrap position, altering the line
16155 above it. It might be worthwhile to handle this more
16156 intelligently, but for now just redisplay from scratch. */
16157 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16158 GIVE_UP (21);
16159
16160 /* Under bidi reordering, adding or deleting a character in the
16161 beginning of a paragraph, before the first strong directional
16162 character, can change the base direction of the paragraph (unless
16163 the buffer specifies a fixed paragraph direction), which will
16164 require to redisplay the whole paragraph. It might be worthwhile
16165 to find the paragraph limits and widen the range of redisplayed
16166 lines to that, but for now just give up this optimization and
16167 redisplay from scratch. */
16168 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16169 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16170 GIVE_UP (22);
16171
16172 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16173 only if buffer has really changed. The reason is that the gap is
16174 initially at Z for freshly visited files. The code below would
16175 set end_unchanged to 0 in that case. */
16176 if (MODIFF > SAVE_MODIFF
16177 /* This seems to happen sometimes after saving a buffer. */
16178 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16179 {
16180 if (GPT - BEG < BEG_UNCHANGED)
16181 BEG_UNCHANGED = GPT - BEG;
16182 if (Z - GPT < END_UNCHANGED)
16183 END_UNCHANGED = Z - GPT;
16184 }
16185
16186 /* The position of the first and last character that has been changed. */
16187 first_changed_charpos = BEG + BEG_UNCHANGED;
16188 last_changed_charpos = Z - END_UNCHANGED;
16189
16190 /* If window starts after a line end, and the last change is in
16191 front of that newline, then changes don't affect the display.
16192 This case happens with stealth-fontification. Note that although
16193 the display is unchanged, glyph positions in the matrix have to
16194 be adjusted, of course. */
16195 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16196 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16197 && ((last_changed_charpos < CHARPOS (start)
16198 && CHARPOS (start) == BEGV)
16199 || (last_changed_charpos < CHARPOS (start) - 1
16200 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16201 {
16202 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16203 struct glyph_row *r0;
16204
16205 /* Compute how many chars/bytes have been added to or removed
16206 from the buffer. */
16207 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16208 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16209 Z_delta = Z - Z_old;
16210 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16211
16212 /* Give up if PT is not in the window. Note that it already has
16213 been checked at the start of try_window_id that PT is not in
16214 front of the window start. */
16215 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16216 GIVE_UP (13);
16217
16218 /* If window start is unchanged, we can reuse the whole matrix
16219 as is, after adjusting glyph positions. No need to compute
16220 the window end again, since its offset from Z hasn't changed. */
16221 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16222 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16223 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16224 /* PT must not be in a partially visible line. */
16225 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16226 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16227 {
16228 /* Adjust positions in the glyph matrix. */
16229 if (Z_delta || Z_delta_bytes)
16230 {
16231 struct glyph_row *r1
16232 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16233 increment_matrix_positions (w->current_matrix,
16234 MATRIX_ROW_VPOS (r0, current_matrix),
16235 MATRIX_ROW_VPOS (r1, current_matrix),
16236 Z_delta, Z_delta_bytes);
16237 }
16238
16239 /* Set the cursor. */
16240 row = row_containing_pos (w, PT, r0, NULL, 0);
16241 if (row)
16242 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16243 else
16244 abort ();
16245 return 1;
16246 }
16247 }
16248
16249 /* Handle the case that changes are all below what is displayed in
16250 the window, and that PT is in the window. This shortcut cannot
16251 be taken if ZV is visible in the window, and text has been added
16252 there that is visible in the window. */
16253 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16254 /* ZV is not visible in the window, or there are no
16255 changes at ZV, actually. */
16256 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16257 || first_changed_charpos == last_changed_charpos))
16258 {
16259 struct glyph_row *r0;
16260
16261 /* Give up if PT is not in the window. Note that it already has
16262 been checked at the start of try_window_id that PT is not in
16263 front of the window start. */
16264 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16265 GIVE_UP (14);
16266
16267 /* If window start is unchanged, we can reuse the whole matrix
16268 as is, without changing glyph positions since no text has
16269 been added/removed in front of the window end. */
16270 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16271 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16272 /* PT must not be in a partially visible line. */
16273 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16274 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16275 {
16276 /* We have to compute the window end anew since text
16277 could have been added/removed after it. */
16278 w->window_end_pos
16279 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16280 w->window_end_bytepos
16281 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16282
16283 /* Set the cursor. */
16284 row = row_containing_pos (w, PT, r0, NULL, 0);
16285 if (row)
16286 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16287 else
16288 abort ();
16289 return 2;
16290 }
16291 }
16292
16293 /* Give up if window start is in the changed area.
16294
16295 The condition used to read
16296
16297 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16298
16299 but why that was tested escapes me at the moment. */
16300 if (CHARPOS (start) >= first_changed_charpos
16301 && CHARPOS (start) <= last_changed_charpos)
16302 GIVE_UP (15);
16303
16304 /* Check that window start agrees with the start of the first glyph
16305 row in its current matrix. Check this after we know the window
16306 start is not in changed text, otherwise positions would not be
16307 comparable. */
16308 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16309 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16310 GIVE_UP (16);
16311
16312 /* Give up if the window ends in strings. Overlay strings
16313 at the end are difficult to handle, so don't try. */
16314 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16315 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16316 GIVE_UP (20);
16317
16318 /* Compute the position at which we have to start displaying new
16319 lines. Some of the lines at the top of the window might be
16320 reusable because they are not displaying changed text. Find the
16321 last row in W's current matrix not affected by changes at the
16322 start of current_buffer. Value is null if changes start in the
16323 first line of window. */
16324 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16325 if (last_unchanged_at_beg_row)
16326 {
16327 /* Avoid starting to display in the moddle of a character, a TAB
16328 for instance. This is easier than to set up the iterator
16329 exactly, and it's not a frequent case, so the additional
16330 effort wouldn't really pay off. */
16331 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16332 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16333 && last_unchanged_at_beg_row > w->current_matrix->rows)
16334 --last_unchanged_at_beg_row;
16335
16336 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16337 GIVE_UP (17);
16338
16339 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16340 GIVE_UP (18);
16341 start_pos = it.current.pos;
16342
16343 /* Start displaying new lines in the desired matrix at the same
16344 vpos we would use in the current matrix, i.e. below
16345 last_unchanged_at_beg_row. */
16346 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16347 current_matrix);
16348 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16349 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16350
16351 xassert (it.hpos == 0 && it.current_x == 0);
16352 }
16353 else
16354 {
16355 /* There are no reusable lines at the start of the window.
16356 Start displaying in the first text line. */
16357 start_display (&it, w, start);
16358 it.vpos = it.first_vpos;
16359 start_pos = it.current.pos;
16360 }
16361
16362 /* Find the first row that is not affected by changes at the end of
16363 the buffer. Value will be null if there is no unchanged row, in
16364 which case we must redisplay to the end of the window. delta
16365 will be set to the value by which buffer positions beginning with
16366 first_unchanged_at_end_row have to be adjusted due to text
16367 changes. */
16368 first_unchanged_at_end_row
16369 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16370 IF_DEBUG (debug_delta = delta);
16371 IF_DEBUG (debug_delta_bytes = delta_bytes);
16372
16373 /* Set stop_pos to the buffer position up to which we will have to
16374 display new lines. If first_unchanged_at_end_row != NULL, this
16375 is the buffer position of the start of the line displayed in that
16376 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16377 that we don't stop at a buffer position. */
16378 stop_pos = 0;
16379 if (first_unchanged_at_end_row)
16380 {
16381 xassert (last_unchanged_at_beg_row == NULL
16382 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16383
16384 /* If this is a continuation line, move forward to the next one
16385 that isn't. Changes in lines above affect this line.
16386 Caution: this may move first_unchanged_at_end_row to a row
16387 not displaying text. */
16388 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16389 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16390 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16391 < it.last_visible_y))
16392 ++first_unchanged_at_end_row;
16393
16394 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16395 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16396 >= it.last_visible_y))
16397 first_unchanged_at_end_row = NULL;
16398 else
16399 {
16400 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16401 + delta);
16402 first_unchanged_at_end_vpos
16403 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16404 xassert (stop_pos >= Z - END_UNCHANGED);
16405 }
16406 }
16407 else if (last_unchanged_at_beg_row == NULL)
16408 GIVE_UP (19);
16409
16410
16411 #if GLYPH_DEBUG
16412
16413 /* Either there is no unchanged row at the end, or the one we have
16414 now displays text. This is a necessary condition for the window
16415 end pos calculation at the end of this function. */
16416 xassert (first_unchanged_at_end_row == NULL
16417 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16418
16419 debug_last_unchanged_at_beg_vpos
16420 = (last_unchanged_at_beg_row
16421 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16422 : -1);
16423 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16424
16425 #endif /* GLYPH_DEBUG != 0 */
16426
16427
16428 /* Display new lines. Set last_text_row to the last new line
16429 displayed which has text on it, i.e. might end up as being the
16430 line where the window_end_vpos is. */
16431 w->cursor.vpos = -1;
16432 last_text_row = NULL;
16433 overlay_arrow_seen = 0;
16434 while (it.current_y < it.last_visible_y
16435 && !fonts_changed_p
16436 && (first_unchanged_at_end_row == NULL
16437 || IT_CHARPOS (it) < stop_pos))
16438 {
16439 if (display_line (&it))
16440 last_text_row = it.glyph_row - 1;
16441 }
16442
16443 if (fonts_changed_p)
16444 return -1;
16445
16446
16447 /* Compute differences in buffer positions, y-positions etc. for
16448 lines reused at the bottom of the window. Compute what we can
16449 scroll. */
16450 if (first_unchanged_at_end_row
16451 /* No lines reused because we displayed everything up to the
16452 bottom of the window. */
16453 && it.current_y < it.last_visible_y)
16454 {
16455 dvpos = (it.vpos
16456 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16457 current_matrix));
16458 dy = it.current_y - first_unchanged_at_end_row->y;
16459 run.current_y = first_unchanged_at_end_row->y;
16460 run.desired_y = run.current_y + dy;
16461 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16462 }
16463 else
16464 {
16465 delta = delta_bytes = dvpos = dy
16466 = run.current_y = run.desired_y = run.height = 0;
16467 first_unchanged_at_end_row = NULL;
16468 }
16469 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16470
16471
16472 /* Find the cursor if not already found. We have to decide whether
16473 PT will appear on this window (it sometimes doesn't, but this is
16474 not a very frequent case.) This decision has to be made before
16475 the current matrix is altered. A value of cursor.vpos < 0 means
16476 that PT is either in one of the lines beginning at
16477 first_unchanged_at_end_row or below the window. Don't care for
16478 lines that might be displayed later at the window end; as
16479 mentioned, this is not a frequent case. */
16480 if (w->cursor.vpos < 0)
16481 {
16482 /* Cursor in unchanged rows at the top? */
16483 if (PT < CHARPOS (start_pos)
16484 && last_unchanged_at_beg_row)
16485 {
16486 row = row_containing_pos (w, PT,
16487 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16488 last_unchanged_at_beg_row + 1, 0);
16489 if (row)
16490 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16491 }
16492
16493 /* Start from first_unchanged_at_end_row looking for PT. */
16494 else if (first_unchanged_at_end_row)
16495 {
16496 row = row_containing_pos (w, PT - delta,
16497 first_unchanged_at_end_row, NULL, 0);
16498 if (row)
16499 set_cursor_from_row (w, row, w->current_matrix, delta,
16500 delta_bytes, dy, dvpos);
16501 }
16502
16503 /* Give up if cursor was not found. */
16504 if (w->cursor.vpos < 0)
16505 {
16506 clear_glyph_matrix (w->desired_matrix);
16507 return -1;
16508 }
16509 }
16510
16511 /* Don't let the cursor end in the scroll margins. */
16512 {
16513 int this_scroll_margin, cursor_height;
16514
16515 this_scroll_margin = max (0, scroll_margin);
16516 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16517 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16518 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16519
16520 if ((w->cursor.y < this_scroll_margin
16521 && CHARPOS (start) > BEGV)
16522 /* Old redisplay didn't take scroll margin into account at the bottom,
16523 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16524 || (w->cursor.y + (make_cursor_line_fully_visible_p
16525 ? cursor_height + this_scroll_margin
16526 : 1)) > it.last_visible_y)
16527 {
16528 w->cursor.vpos = -1;
16529 clear_glyph_matrix (w->desired_matrix);
16530 return -1;
16531 }
16532 }
16533
16534 /* Scroll the display. Do it before changing the current matrix so
16535 that xterm.c doesn't get confused about where the cursor glyph is
16536 found. */
16537 if (dy && run.height)
16538 {
16539 update_begin (f);
16540
16541 if (FRAME_WINDOW_P (f))
16542 {
16543 FRAME_RIF (f)->update_window_begin_hook (w);
16544 FRAME_RIF (f)->clear_window_mouse_face (w);
16545 FRAME_RIF (f)->scroll_run_hook (w, &run);
16546 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16547 }
16548 else
16549 {
16550 /* Terminal frame. In this case, dvpos gives the number of
16551 lines to scroll by; dvpos < 0 means scroll up. */
16552 int from_vpos
16553 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16554 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16555 int end = (WINDOW_TOP_EDGE_LINE (w)
16556 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16557 + window_internal_height (w));
16558
16559 #if defined (HAVE_GPM) || defined (MSDOS)
16560 x_clear_window_mouse_face (w);
16561 #endif
16562 /* Perform the operation on the screen. */
16563 if (dvpos > 0)
16564 {
16565 /* Scroll last_unchanged_at_beg_row to the end of the
16566 window down dvpos lines. */
16567 set_terminal_window (f, end);
16568
16569 /* On dumb terminals delete dvpos lines at the end
16570 before inserting dvpos empty lines. */
16571 if (!FRAME_SCROLL_REGION_OK (f))
16572 ins_del_lines (f, end - dvpos, -dvpos);
16573
16574 /* Insert dvpos empty lines in front of
16575 last_unchanged_at_beg_row. */
16576 ins_del_lines (f, from, dvpos);
16577 }
16578 else if (dvpos < 0)
16579 {
16580 /* Scroll up last_unchanged_at_beg_vpos to the end of
16581 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16582 set_terminal_window (f, end);
16583
16584 /* Delete dvpos lines in front of
16585 last_unchanged_at_beg_vpos. ins_del_lines will set
16586 the cursor to the given vpos and emit |dvpos| delete
16587 line sequences. */
16588 ins_del_lines (f, from + dvpos, dvpos);
16589
16590 /* On a dumb terminal insert dvpos empty lines at the
16591 end. */
16592 if (!FRAME_SCROLL_REGION_OK (f))
16593 ins_del_lines (f, end + dvpos, -dvpos);
16594 }
16595
16596 set_terminal_window (f, 0);
16597 }
16598
16599 update_end (f);
16600 }
16601
16602 /* Shift reused rows of the current matrix to the right position.
16603 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16604 text. */
16605 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16606 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16607 if (dvpos < 0)
16608 {
16609 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16610 bottom_vpos, dvpos);
16611 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16612 bottom_vpos, 0);
16613 }
16614 else if (dvpos > 0)
16615 {
16616 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16617 bottom_vpos, dvpos);
16618 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16619 first_unchanged_at_end_vpos + dvpos, 0);
16620 }
16621
16622 /* For frame-based redisplay, make sure that current frame and window
16623 matrix are in sync with respect to glyph memory. */
16624 if (!FRAME_WINDOW_P (f))
16625 sync_frame_with_window_matrix_rows (w);
16626
16627 /* Adjust buffer positions in reused rows. */
16628 if (delta || delta_bytes)
16629 increment_matrix_positions (current_matrix,
16630 first_unchanged_at_end_vpos + dvpos,
16631 bottom_vpos, delta, delta_bytes);
16632
16633 /* Adjust Y positions. */
16634 if (dy)
16635 shift_glyph_matrix (w, current_matrix,
16636 first_unchanged_at_end_vpos + dvpos,
16637 bottom_vpos, dy);
16638
16639 if (first_unchanged_at_end_row)
16640 {
16641 first_unchanged_at_end_row += dvpos;
16642 if (first_unchanged_at_end_row->y >= it.last_visible_y
16643 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16644 first_unchanged_at_end_row = NULL;
16645 }
16646
16647 /* If scrolling up, there may be some lines to display at the end of
16648 the window. */
16649 last_text_row_at_end = NULL;
16650 if (dy < 0)
16651 {
16652 /* Scrolling up can leave for example a partially visible line
16653 at the end of the window to be redisplayed. */
16654 /* Set last_row to the glyph row in the current matrix where the
16655 window end line is found. It has been moved up or down in
16656 the matrix by dvpos. */
16657 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16658 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16659
16660 /* If last_row is the window end line, it should display text. */
16661 xassert (last_row->displays_text_p);
16662
16663 /* If window end line was partially visible before, begin
16664 displaying at that line. Otherwise begin displaying with the
16665 line following it. */
16666 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16667 {
16668 init_to_row_start (&it, w, last_row);
16669 it.vpos = last_vpos;
16670 it.current_y = last_row->y;
16671 }
16672 else
16673 {
16674 init_to_row_end (&it, w, last_row);
16675 it.vpos = 1 + last_vpos;
16676 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16677 ++last_row;
16678 }
16679
16680 /* We may start in a continuation line. If so, we have to
16681 get the right continuation_lines_width and current_x. */
16682 it.continuation_lines_width = last_row->continuation_lines_width;
16683 it.hpos = it.current_x = 0;
16684
16685 /* Display the rest of the lines at the window end. */
16686 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16687 while (it.current_y < it.last_visible_y
16688 && !fonts_changed_p)
16689 {
16690 /* Is it always sure that the display agrees with lines in
16691 the current matrix? I don't think so, so we mark rows
16692 displayed invalid in the current matrix by setting their
16693 enabled_p flag to zero. */
16694 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16695 if (display_line (&it))
16696 last_text_row_at_end = it.glyph_row - 1;
16697 }
16698 }
16699
16700 /* Update window_end_pos and window_end_vpos. */
16701 if (first_unchanged_at_end_row
16702 && !last_text_row_at_end)
16703 {
16704 /* Window end line if one of the preserved rows from the current
16705 matrix. Set row to the last row displaying text in current
16706 matrix starting at first_unchanged_at_end_row, after
16707 scrolling. */
16708 xassert (first_unchanged_at_end_row->displays_text_p);
16709 row = find_last_row_displaying_text (w->current_matrix, &it,
16710 first_unchanged_at_end_row);
16711 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16712
16713 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16714 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16715 w->window_end_vpos
16716 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16717 xassert (w->window_end_bytepos >= 0);
16718 IF_DEBUG (debug_method_add (w, "A"));
16719 }
16720 else if (last_text_row_at_end)
16721 {
16722 w->window_end_pos
16723 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16724 w->window_end_bytepos
16725 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16726 w->window_end_vpos
16727 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16728 xassert (w->window_end_bytepos >= 0);
16729 IF_DEBUG (debug_method_add (w, "B"));
16730 }
16731 else if (last_text_row)
16732 {
16733 /* We have displayed either to the end of the window or at the
16734 end of the window, i.e. the last row with text is to be found
16735 in the desired matrix. */
16736 w->window_end_pos
16737 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16738 w->window_end_bytepos
16739 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16740 w->window_end_vpos
16741 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16742 xassert (w->window_end_bytepos >= 0);
16743 }
16744 else if (first_unchanged_at_end_row == NULL
16745 && last_text_row == NULL
16746 && last_text_row_at_end == NULL)
16747 {
16748 /* Displayed to end of window, but no line containing text was
16749 displayed. Lines were deleted at the end of the window. */
16750 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16751 int vpos = XFASTINT (w->window_end_vpos);
16752 struct glyph_row *current_row = current_matrix->rows + vpos;
16753 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16754
16755 for (row = NULL;
16756 row == NULL && vpos >= first_vpos;
16757 --vpos, --current_row, --desired_row)
16758 {
16759 if (desired_row->enabled_p)
16760 {
16761 if (desired_row->displays_text_p)
16762 row = desired_row;
16763 }
16764 else if (current_row->displays_text_p)
16765 row = current_row;
16766 }
16767
16768 xassert (row != NULL);
16769 w->window_end_vpos = make_number (vpos + 1);
16770 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16771 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16772 xassert (w->window_end_bytepos >= 0);
16773 IF_DEBUG (debug_method_add (w, "C"));
16774 }
16775 else
16776 abort ();
16777
16778 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16779 debug_end_vpos = XFASTINT (w->window_end_vpos));
16780
16781 /* Record that display has not been completed. */
16782 w->window_end_valid = Qnil;
16783 w->desired_matrix->no_scrolling_p = 1;
16784 return 3;
16785
16786 #undef GIVE_UP
16787 }
16788
16789
16790 \f
16791 /***********************************************************************
16792 More debugging support
16793 ***********************************************************************/
16794
16795 #if GLYPH_DEBUG
16796
16797 void dump_glyph_row (struct glyph_row *, int, int);
16798 void dump_glyph_matrix (struct glyph_matrix *, int);
16799 void dump_glyph (struct glyph_row *, struct glyph *, int);
16800
16801
16802 /* Dump the contents of glyph matrix MATRIX on stderr.
16803
16804 GLYPHS 0 means don't show glyph contents.
16805 GLYPHS 1 means show glyphs in short form
16806 GLYPHS > 1 means show glyphs in long form. */
16807
16808 void
16809 dump_glyph_matrix (matrix, glyphs)
16810 struct glyph_matrix *matrix;
16811 int glyphs;
16812 {
16813 int i;
16814 for (i = 0; i < matrix->nrows; ++i)
16815 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16816 }
16817
16818
16819 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16820 the glyph row and area where the glyph comes from. */
16821
16822 void
16823 dump_glyph (row, glyph, area)
16824 struct glyph_row *row;
16825 struct glyph *glyph;
16826 int area;
16827 {
16828 if (glyph->type == CHAR_GLYPH)
16829 {
16830 fprintf (stderr,
16831 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16832 glyph - row->glyphs[TEXT_AREA],
16833 'C',
16834 glyph->charpos,
16835 (BUFFERP (glyph->object)
16836 ? 'B'
16837 : (STRINGP (glyph->object)
16838 ? 'S'
16839 : '-')),
16840 glyph->pixel_width,
16841 glyph->u.ch,
16842 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16843 ? glyph->u.ch
16844 : '.'),
16845 glyph->face_id,
16846 glyph->left_box_line_p,
16847 glyph->right_box_line_p);
16848 }
16849 else if (glyph->type == STRETCH_GLYPH)
16850 {
16851 fprintf (stderr,
16852 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16853 glyph - row->glyphs[TEXT_AREA],
16854 'S',
16855 glyph->charpos,
16856 (BUFFERP (glyph->object)
16857 ? 'B'
16858 : (STRINGP (glyph->object)
16859 ? 'S'
16860 : '-')),
16861 glyph->pixel_width,
16862 0,
16863 '.',
16864 glyph->face_id,
16865 glyph->left_box_line_p,
16866 glyph->right_box_line_p);
16867 }
16868 else if (glyph->type == IMAGE_GLYPH)
16869 {
16870 fprintf (stderr,
16871 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16872 glyph - row->glyphs[TEXT_AREA],
16873 'I',
16874 glyph->charpos,
16875 (BUFFERP (glyph->object)
16876 ? 'B'
16877 : (STRINGP (glyph->object)
16878 ? 'S'
16879 : '-')),
16880 glyph->pixel_width,
16881 glyph->u.img_id,
16882 '.',
16883 glyph->face_id,
16884 glyph->left_box_line_p,
16885 glyph->right_box_line_p);
16886 }
16887 else if (glyph->type == COMPOSITE_GLYPH)
16888 {
16889 fprintf (stderr,
16890 " %5d %4c %6d %c %3d 0x%05x",
16891 glyph - row->glyphs[TEXT_AREA],
16892 '+',
16893 glyph->charpos,
16894 (BUFFERP (glyph->object)
16895 ? 'B'
16896 : (STRINGP (glyph->object)
16897 ? 'S'
16898 : '-')),
16899 glyph->pixel_width,
16900 glyph->u.cmp.id);
16901 if (glyph->u.cmp.automatic)
16902 fprintf (stderr,
16903 "[%d-%d]",
16904 glyph->slice.cmp.from, glyph->slice.cmp.to);
16905 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16906 glyph->face_id,
16907 glyph->left_box_line_p,
16908 glyph->right_box_line_p);
16909 }
16910 }
16911
16912
16913 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16914 GLYPHS 0 means don't show glyph contents.
16915 GLYPHS 1 means show glyphs in short form
16916 GLYPHS > 1 means show glyphs in long form. */
16917
16918 void
16919 dump_glyph_row (row, vpos, glyphs)
16920 struct glyph_row *row;
16921 int vpos, glyphs;
16922 {
16923 if (glyphs != 1)
16924 {
16925 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16926 fprintf (stderr, "======================================================================\n");
16927
16928 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16929 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16930 vpos,
16931 MATRIX_ROW_START_CHARPOS (row),
16932 MATRIX_ROW_END_CHARPOS (row),
16933 row->used[TEXT_AREA],
16934 row->contains_overlapping_glyphs_p,
16935 row->enabled_p,
16936 row->truncated_on_left_p,
16937 row->truncated_on_right_p,
16938 row->continued_p,
16939 MATRIX_ROW_CONTINUATION_LINE_P (row),
16940 row->displays_text_p,
16941 row->ends_at_zv_p,
16942 row->fill_line_p,
16943 row->ends_in_middle_of_char_p,
16944 row->starts_in_middle_of_char_p,
16945 row->mouse_face_p,
16946 row->x,
16947 row->y,
16948 row->pixel_width,
16949 row->height,
16950 row->visible_height,
16951 row->ascent,
16952 row->phys_ascent);
16953 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16954 row->end.overlay_string_index,
16955 row->continuation_lines_width);
16956 fprintf (stderr, "%9d %5d\n",
16957 CHARPOS (row->start.string_pos),
16958 CHARPOS (row->end.string_pos));
16959 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16960 row->end.dpvec_index);
16961 }
16962
16963 if (glyphs > 1)
16964 {
16965 int area;
16966
16967 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16968 {
16969 struct glyph *glyph = row->glyphs[area];
16970 struct glyph *glyph_end = glyph + row->used[area];
16971
16972 /* Glyph for a line end in text. */
16973 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16974 ++glyph_end;
16975
16976 if (glyph < glyph_end)
16977 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16978
16979 for (; glyph < glyph_end; ++glyph)
16980 dump_glyph (row, glyph, area);
16981 }
16982 }
16983 else if (glyphs == 1)
16984 {
16985 int area;
16986
16987 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16988 {
16989 char *s = (char *) alloca (row->used[area] + 1);
16990 int i;
16991
16992 for (i = 0; i < row->used[area]; ++i)
16993 {
16994 struct glyph *glyph = row->glyphs[area] + i;
16995 if (glyph->type == CHAR_GLYPH
16996 && glyph->u.ch < 0x80
16997 && glyph->u.ch >= ' ')
16998 s[i] = glyph->u.ch;
16999 else
17000 s[i] = '.';
17001 }
17002
17003 s[i] = '\0';
17004 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17005 }
17006 }
17007 }
17008
17009
17010 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17011 Sdump_glyph_matrix, 0, 1, "p",
17012 doc: /* Dump the current matrix of the selected window to stderr.
17013 Shows contents of glyph row structures. With non-nil
17014 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17015 glyphs in short form, otherwise show glyphs in long form. */)
17016 (Lisp_Object glyphs)
17017 {
17018 struct window *w = XWINDOW (selected_window);
17019 struct buffer *buffer = XBUFFER (w->buffer);
17020
17021 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
17022 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17023 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17024 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17025 fprintf (stderr, "=============================================\n");
17026 dump_glyph_matrix (w->current_matrix,
17027 NILP (glyphs) ? 0 : XINT (glyphs));
17028 return Qnil;
17029 }
17030
17031
17032 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17033 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17034 (void)
17035 {
17036 struct frame *f = XFRAME (selected_frame);
17037 dump_glyph_matrix (f->current_matrix, 1);
17038 return Qnil;
17039 }
17040
17041
17042 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17043 doc: /* Dump glyph row ROW to stderr.
17044 GLYPH 0 means don't dump glyphs.
17045 GLYPH 1 means dump glyphs in short form.
17046 GLYPH > 1 or omitted means dump glyphs in long form. */)
17047 (Lisp_Object row, Lisp_Object glyphs)
17048 {
17049 struct glyph_matrix *matrix;
17050 int vpos;
17051
17052 CHECK_NUMBER (row);
17053 matrix = XWINDOW (selected_window)->current_matrix;
17054 vpos = XINT (row);
17055 if (vpos >= 0 && vpos < matrix->nrows)
17056 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17057 vpos,
17058 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17059 return Qnil;
17060 }
17061
17062
17063 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17064 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17065 GLYPH 0 means don't dump glyphs.
17066 GLYPH 1 means dump glyphs in short form.
17067 GLYPH > 1 or omitted means dump glyphs in long form. */)
17068 (Lisp_Object row, Lisp_Object glyphs)
17069 {
17070 struct frame *sf = SELECTED_FRAME ();
17071 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17072 int vpos;
17073
17074 CHECK_NUMBER (row);
17075 vpos = XINT (row);
17076 if (vpos >= 0 && vpos < m->nrows)
17077 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17078 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17079 return Qnil;
17080 }
17081
17082
17083 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17084 doc: /* Toggle tracing of redisplay.
17085 With ARG, turn tracing on if and only if ARG is positive. */)
17086 (Lisp_Object arg)
17087 {
17088 if (NILP (arg))
17089 trace_redisplay_p = !trace_redisplay_p;
17090 else
17091 {
17092 arg = Fprefix_numeric_value (arg);
17093 trace_redisplay_p = XINT (arg) > 0;
17094 }
17095
17096 return Qnil;
17097 }
17098
17099
17100 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17101 doc: /* Like `format', but print result to stderr.
17102 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17103 (size_t nargs, Lisp_Object *args)
17104 {
17105 Lisp_Object s = Fformat (nargs, args);
17106 fprintf (stderr, "%s", SDATA (s));
17107 return Qnil;
17108 }
17109
17110 #endif /* GLYPH_DEBUG */
17111
17112
17113 \f
17114 /***********************************************************************
17115 Building Desired Matrix Rows
17116 ***********************************************************************/
17117
17118 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17119 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17120
17121 static struct glyph_row *
17122 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17123 {
17124 struct frame *f = XFRAME (WINDOW_FRAME (w));
17125 struct buffer *buffer = XBUFFER (w->buffer);
17126 struct buffer *old = current_buffer;
17127 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17128 int arrow_len = SCHARS (overlay_arrow_string);
17129 const unsigned char *arrow_end = arrow_string + arrow_len;
17130 const unsigned char *p;
17131 struct it it;
17132 int multibyte_p;
17133 int n_glyphs_before;
17134
17135 set_buffer_temp (buffer);
17136 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17137 it.glyph_row->used[TEXT_AREA] = 0;
17138 SET_TEXT_POS (it.position, 0, 0);
17139
17140 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17141 p = arrow_string;
17142 while (p < arrow_end)
17143 {
17144 Lisp_Object face, ilisp;
17145
17146 /* Get the next character. */
17147 if (multibyte_p)
17148 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17149 else
17150 {
17151 it.c = it.char_to_display = *p, it.len = 1;
17152 if (! ASCII_CHAR_P (it.c))
17153 it.char_to_display = BYTE8_TO_CHAR (it.c);
17154 }
17155 p += it.len;
17156
17157 /* Get its face. */
17158 ilisp = make_number (p - arrow_string);
17159 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17160 it.face_id = compute_char_face (f, it.char_to_display, face);
17161
17162 /* Compute its width, get its glyphs. */
17163 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17164 SET_TEXT_POS (it.position, -1, -1);
17165 PRODUCE_GLYPHS (&it);
17166
17167 /* If this character doesn't fit any more in the line, we have
17168 to remove some glyphs. */
17169 if (it.current_x > it.last_visible_x)
17170 {
17171 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17172 break;
17173 }
17174 }
17175
17176 set_buffer_temp (old);
17177 return it.glyph_row;
17178 }
17179
17180
17181 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17182 glyphs are only inserted for terminal frames since we can't really
17183 win with truncation glyphs when partially visible glyphs are
17184 involved. Which glyphs to insert is determined by
17185 produce_special_glyphs. */
17186
17187 static void
17188 insert_left_trunc_glyphs (struct it *it)
17189 {
17190 struct it truncate_it;
17191 struct glyph *from, *end, *to, *toend;
17192
17193 xassert (!FRAME_WINDOW_P (it->f));
17194
17195 /* Get the truncation glyphs. */
17196 truncate_it = *it;
17197 truncate_it.current_x = 0;
17198 truncate_it.face_id = DEFAULT_FACE_ID;
17199 truncate_it.glyph_row = &scratch_glyph_row;
17200 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17201 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17202 truncate_it.object = make_number (0);
17203 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17204
17205 /* Overwrite glyphs from IT with truncation glyphs. */
17206 if (!it->glyph_row->reversed_p)
17207 {
17208 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17209 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17210 to = it->glyph_row->glyphs[TEXT_AREA];
17211 toend = to + it->glyph_row->used[TEXT_AREA];
17212
17213 while (from < end)
17214 *to++ = *from++;
17215
17216 /* There may be padding glyphs left over. Overwrite them too. */
17217 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17218 {
17219 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17220 while (from < end)
17221 *to++ = *from++;
17222 }
17223
17224 if (to > toend)
17225 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17226 }
17227 else
17228 {
17229 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17230 that back to front. */
17231 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17232 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17233 toend = it->glyph_row->glyphs[TEXT_AREA];
17234 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17235
17236 while (from >= end && to >= toend)
17237 *to-- = *from--;
17238 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17239 {
17240 from =
17241 truncate_it.glyph_row->glyphs[TEXT_AREA]
17242 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17243 while (from >= end && to >= toend)
17244 *to-- = *from--;
17245 }
17246 if (from >= end)
17247 {
17248 /* Need to free some room before prepending additional
17249 glyphs. */
17250 int move_by = from - end + 1;
17251 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17252 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17253
17254 for ( ; g >= g0; g--)
17255 g[move_by] = *g;
17256 while (from >= end)
17257 *to-- = *from--;
17258 it->glyph_row->used[TEXT_AREA] += move_by;
17259 }
17260 }
17261 }
17262
17263
17264 /* Compute the pixel height and width of IT->glyph_row.
17265
17266 Most of the time, ascent and height of a display line will be equal
17267 to the max_ascent and max_height values of the display iterator
17268 structure. This is not the case if
17269
17270 1. We hit ZV without displaying anything. In this case, max_ascent
17271 and max_height will be zero.
17272
17273 2. We have some glyphs that don't contribute to the line height.
17274 (The glyph row flag contributes_to_line_height_p is for future
17275 pixmap extensions).
17276
17277 The first case is easily covered by using default values because in
17278 these cases, the line height does not really matter, except that it
17279 must not be zero. */
17280
17281 static void
17282 compute_line_metrics (struct it *it)
17283 {
17284 struct glyph_row *row = it->glyph_row;
17285
17286 if (FRAME_WINDOW_P (it->f))
17287 {
17288 int i, min_y, max_y;
17289
17290 /* The line may consist of one space only, that was added to
17291 place the cursor on it. If so, the row's height hasn't been
17292 computed yet. */
17293 if (row->height == 0)
17294 {
17295 if (it->max_ascent + it->max_descent == 0)
17296 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17297 row->ascent = it->max_ascent;
17298 row->height = it->max_ascent + it->max_descent;
17299 row->phys_ascent = it->max_phys_ascent;
17300 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17301 row->extra_line_spacing = it->max_extra_line_spacing;
17302 }
17303
17304 /* Compute the width of this line. */
17305 row->pixel_width = row->x;
17306 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17307 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17308
17309 xassert (row->pixel_width >= 0);
17310 xassert (row->ascent >= 0 && row->height > 0);
17311
17312 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17313 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17314
17315 /* If first line's physical ascent is larger than its logical
17316 ascent, use the physical ascent, and make the row taller.
17317 This makes accented characters fully visible. */
17318 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17319 && row->phys_ascent > row->ascent)
17320 {
17321 row->height += row->phys_ascent - row->ascent;
17322 row->ascent = row->phys_ascent;
17323 }
17324
17325 /* Compute how much of the line is visible. */
17326 row->visible_height = row->height;
17327
17328 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17329 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17330
17331 if (row->y < min_y)
17332 row->visible_height -= min_y - row->y;
17333 if (row->y + row->height > max_y)
17334 row->visible_height -= row->y + row->height - max_y;
17335 }
17336 else
17337 {
17338 row->pixel_width = row->used[TEXT_AREA];
17339 if (row->continued_p)
17340 row->pixel_width -= it->continuation_pixel_width;
17341 else if (row->truncated_on_right_p)
17342 row->pixel_width -= it->truncation_pixel_width;
17343 row->ascent = row->phys_ascent = 0;
17344 row->height = row->phys_height = row->visible_height = 1;
17345 row->extra_line_spacing = 0;
17346 }
17347
17348 /* Compute a hash code for this row. */
17349 {
17350 int area, i;
17351 row->hash = 0;
17352 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17353 for (i = 0; i < row->used[area]; ++i)
17354 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17355 + row->glyphs[area][i].u.val
17356 + row->glyphs[area][i].face_id
17357 + row->glyphs[area][i].padding_p
17358 + (row->glyphs[area][i].type << 2));
17359 }
17360
17361 it->max_ascent = it->max_descent = 0;
17362 it->max_phys_ascent = it->max_phys_descent = 0;
17363 }
17364
17365
17366 /* Append one space to the glyph row of iterator IT if doing a
17367 window-based redisplay. The space has the same face as
17368 IT->face_id. Value is non-zero if a space was added.
17369
17370 This function is called to make sure that there is always one glyph
17371 at the end of a glyph row that the cursor can be set on under
17372 window-systems. (If there weren't such a glyph we would not know
17373 how wide and tall a box cursor should be displayed).
17374
17375 At the same time this space let's a nicely handle clearing to the
17376 end of the line if the row ends in italic text. */
17377
17378 static int
17379 append_space_for_newline (struct it *it, int default_face_p)
17380 {
17381 if (FRAME_WINDOW_P (it->f))
17382 {
17383 int n = it->glyph_row->used[TEXT_AREA];
17384
17385 if (it->glyph_row->glyphs[TEXT_AREA] + n
17386 < it->glyph_row->glyphs[1 + TEXT_AREA])
17387 {
17388 /* Save some values that must not be changed.
17389 Must save IT->c and IT->len because otherwise
17390 ITERATOR_AT_END_P wouldn't work anymore after
17391 append_space_for_newline has been called. */
17392 enum display_element_type saved_what = it->what;
17393 int saved_c = it->c, saved_len = it->len;
17394 int saved_char_to_display = it->char_to_display;
17395 int saved_x = it->current_x;
17396 int saved_face_id = it->face_id;
17397 struct text_pos saved_pos;
17398 Lisp_Object saved_object;
17399 struct face *face;
17400
17401 saved_object = it->object;
17402 saved_pos = it->position;
17403
17404 it->what = IT_CHARACTER;
17405 memset (&it->position, 0, sizeof it->position);
17406 it->object = make_number (0);
17407 it->c = it->char_to_display = ' ';
17408 it->len = 1;
17409
17410 if (default_face_p)
17411 it->face_id = DEFAULT_FACE_ID;
17412 else if (it->face_before_selective_p)
17413 it->face_id = it->saved_face_id;
17414 face = FACE_FROM_ID (it->f, it->face_id);
17415 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17416
17417 PRODUCE_GLYPHS (it);
17418
17419 it->override_ascent = -1;
17420 it->constrain_row_ascent_descent_p = 0;
17421 it->current_x = saved_x;
17422 it->object = saved_object;
17423 it->position = saved_pos;
17424 it->what = saved_what;
17425 it->face_id = saved_face_id;
17426 it->len = saved_len;
17427 it->c = saved_c;
17428 it->char_to_display = saved_char_to_display;
17429 return 1;
17430 }
17431 }
17432
17433 return 0;
17434 }
17435
17436
17437 /* Extend the face of the last glyph in the text area of IT->glyph_row
17438 to the end of the display line. Called from display_line. If the
17439 glyph row is empty, add a space glyph to it so that we know the
17440 face to draw. Set the glyph row flag fill_line_p. If the glyph
17441 row is R2L, prepend a stretch glyph to cover the empty space to the
17442 left of the leftmost glyph. */
17443
17444 static void
17445 extend_face_to_end_of_line (struct it *it)
17446 {
17447 struct face *face;
17448 struct frame *f = it->f;
17449
17450 /* If line is already filled, do nothing. Non window-system frames
17451 get a grace of one more ``pixel'' because their characters are
17452 1-``pixel'' wide, so they hit the equality too early. This grace
17453 is needed only for R2L rows that are not continued, to produce
17454 one extra blank where we could display the cursor. */
17455 if (it->current_x >= it->last_visible_x
17456 + (!FRAME_WINDOW_P (f)
17457 && it->glyph_row->reversed_p
17458 && !it->glyph_row->continued_p))
17459 return;
17460
17461 /* Face extension extends the background and box of IT->face_id
17462 to the end of the line. If the background equals the background
17463 of the frame, we don't have to do anything. */
17464 if (it->face_before_selective_p)
17465 face = FACE_FROM_ID (f, it->saved_face_id);
17466 else
17467 face = FACE_FROM_ID (f, it->face_id);
17468
17469 if (FRAME_WINDOW_P (f)
17470 && it->glyph_row->displays_text_p
17471 && face->box == FACE_NO_BOX
17472 && face->background == FRAME_BACKGROUND_PIXEL (f)
17473 && !face->stipple
17474 && !it->glyph_row->reversed_p)
17475 return;
17476
17477 /* Set the glyph row flag indicating that the face of the last glyph
17478 in the text area has to be drawn to the end of the text area. */
17479 it->glyph_row->fill_line_p = 1;
17480
17481 /* If current character of IT is not ASCII, make sure we have the
17482 ASCII face. This will be automatically undone the next time
17483 get_next_display_element returns a multibyte character. Note
17484 that the character will always be single byte in unibyte
17485 text. */
17486 if (!ASCII_CHAR_P (it->c))
17487 {
17488 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17489 }
17490
17491 if (FRAME_WINDOW_P (f))
17492 {
17493 /* If the row is empty, add a space with the current face of IT,
17494 so that we know which face to draw. */
17495 if (it->glyph_row->used[TEXT_AREA] == 0)
17496 {
17497 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17498 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17499 it->glyph_row->used[TEXT_AREA] = 1;
17500 }
17501 #ifdef HAVE_WINDOW_SYSTEM
17502 if (it->glyph_row->reversed_p)
17503 {
17504 /* Prepend a stretch glyph to the row, such that the
17505 rightmost glyph will be drawn flushed all the way to the
17506 right margin of the window. The stretch glyph that will
17507 occupy the empty space, if any, to the left of the
17508 glyphs. */
17509 struct font *font = face->font ? face->font : FRAME_FONT (f);
17510 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17511 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17512 struct glyph *g;
17513 int row_width, stretch_ascent, stretch_width;
17514 struct text_pos saved_pos;
17515 int saved_face_id, saved_avoid_cursor;
17516
17517 for (row_width = 0, g = row_start; g < row_end; g++)
17518 row_width += g->pixel_width;
17519 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17520 if (stretch_width > 0)
17521 {
17522 stretch_ascent =
17523 (((it->ascent + it->descent)
17524 * FONT_BASE (font)) / FONT_HEIGHT (font));
17525 saved_pos = it->position;
17526 memset (&it->position, 0, sizeof it->position);
17527 saved_avoid_cursor = it->avoid_cursor_p;
17528 it->avoid_cursor_p = 1;
17529 saved_face_id = it->face_id;
17530 /* The last row's stretch glyph should get the default
17531 face, to avoid painting the rest of the window with
17532 the region face, if the region ends at ZV. */
17533 if (it->glyph_row->ends_at_zv_p)
17534 it->face_id = DEFAULT_FACE_ID;
17535 else
17536 it->face_id = face->id;
17537 append_stretch_glyph (it, make_number (0), stretch_width,
17538 it->ascent + it->descent, stretch_ascent);
17539 it->position = saved_pos;
17540 it->avoid_cursor_p = saved_avoid_cursor;
17541 it->face_id = saved_face_id;
17542 }
17543 }
17544 #endif /* HAVE_WINDOW_SYSTEM */
17545 }
17546 else
17547 {
17548 /* Save some values that must not be changed. */
17549 int saved_x = it->current_x;
17550 struct text_pos saved_pos;
17551 Lisp_Object saved_object;
17552 enum display_element_type saved_what = it->what;
17553 int saved_face_id = it->face_id;
17554
17555 saved_object = it->object;
17556 saved_pos = it->position;
17557
17558 it->what = IT_CHARACTER;
17559 memset (&it->position, 0, sizeof it->position);
17560 it->object = make_number (0);
17561 it->c = it->char_to_display = ' ';
17562 it->len = 1;
17563 /* The last row's blank glyphs should get the default face, to
17564 avoid painting the rest of the window with the region face,
17565 if the region ends at ZV. */
17566 if (it->glyph_row->ends_at_zv_p)
17567 it->face_id = DEFAULT_FACE_ID;
17568 else
17569 it->face_id = face->id;
17570
17571 PRODUCE_GLYPHS (it);
17572
17573 while (it->current_x <= it->last_visible_x)
17574 PRODUCE_GLYPHS (it);
17575
17576 /* Don't count these blanks really. It would let us insert a left
17577 truncation glyph below and make us set the cursor on them, maybe. */
17578 it->current_x = saved_x;
17579 it->object = saved_object;
17580 it->position = saved_pos;
17581 it->what = saved_what;
17582 it->face_id = saved_face_id;
17583 }
17584 }
17585
17586
17587 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17588 trailing whitespace. */
17589
17590 static int
17591 trailing_whitespace_p (EMACS_INT charpos)
17592 {
17593 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17594 int c = 0;
17595
17596 while (bytepos < ZV_BYTE
17597 && (c = FETCH_CHAR (bytepos),
17598 c == ' ' || c == '\t'))
17599 ++bytepos;
17600
17601 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17602 {
17603 if (bytepos != PT_BYTE)
17604 return 1;
17605 }
17606 return 0;
17607 }
17608
17609
17610 /* Highlight trailing whitespace, if any, in ROW. */
17611
17612 static void
17613 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17614 {
17615 int used = row->used[TEXT_AREA];
17616
17617 if (used)
17618 {
17619 struct glyph *start = row->glyphs[TEXT_AREA];
17620 struct glyph *glyph = start + used - 1;
17621
17622 if (row->reversed_p)
17623 {
17624 /* Right-to-left rows need to be processed in the opposite
17625 direction, so swap the edge pointers. */
17626 glyph = start;
17627 start = row->glyphs[TEXT_AREA] + used - 1;
17628 }
17629
17630 /* Skip over glyphs inserted to display the cursor at the
17631 end of a line, for extending the face of the last glyph
17632 to the end of the line on terminals, and for truncation
17633 and continuation glyphs. */
17634 if (!row->reversed_p)
17635 {
17636 while (glyph >= start
17637 && glyph->type == CHAR_GLYPH
17638 && INTEGERP (glyph->object))
17639 --glyph;
17640 }
17641 else
17642 {
17643 while (glyph <= start
17644 && glyph->type == CHAR_GLYPH
17645 && INTEGERP (glyph->object))
17646 ++glyph;
17647 }
17648
17649 /* If last glyph is a space or stretch, and it's trailing
17650 whitespace, set the face of all trailing whitespace glyphs in
17651 IT->glyph_row to `trailing-whitespace'. */
17652 if ((row->reversed_p ? glyph <= start : glyph >= start)
17653 && BUFFERP (glyph->object)
17654 && (glyph->type == STRETCH_GLYPH
17655 || (glyph->type == CHAR_GLYPH
17656 && glyph->u.ch == ' '))
17657 && trailing_whitespace_p (glyph->charpos))
17658 {
17659 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17660 if (face_id < 0)
17661 return;
17662
17663 if (!row->reversed_p)
17664 {
17665 while (glyph >= start
17666 && BUFFERP (glyph->object)
17667 && (glyph->type == STRETCH_GLYPH
17668 || (glyph->type == CHAR_GLYPH
17669 && glyph->u.ch == ' ')))
17670 (glyph--)->face_id = face_id;
17671 }
17672 else
17673 {
17674 while (glyph <= start
17675 && BUFFERP (glyph->object)
17676 && (glyph->type == STRETCH_GLYPH
17677 || (glyph->type == CHAR_GLYPH
17678 && glyph->u.ch == ' ')))
17679 (glyph++)->face_id = face_id;
17680 }
17681 }
17682 }
17683 }
17684
17685
17686 /* Value is non-zero if glyph row ROW should be
17687 used to hold the cursor. */
17688
17689 static int
17690 cursor_row_p (struct glyph_row *row)
17691 {
17692 int result = 1;
17693
17694 if (PT == CHARPOS (row->end.pos))
17695 {
17696 /* Suppose the row ends on a string.
17697 Unless the row is continued, that means it ends on a newline
17698 in the string. If it's anything other than a display string
17699 (e.g. a before-string from an overlay), we don't want the
17700 cursor there. (This heuristic seems to give the optimal
17701 behavior for the various types of multi-line strings.) */
17702 if (CHARPOS (row->end.string_pos) >= 0)
17703 {
17704 if (row->continued_p)
17705 result = 1;
17706 else
17707 {
17708 /* Check for `display' property. */
17709 struct glyph *beg = row->glyphs[TEXT_AREA];
17710 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17711 struct glyph *glyph;
17712
17713 result = 0;
17714 for (glyph = end; glyph >= beg; --glyph)
17715 if (STRINGP (glyph->object))
17716 {
17717 Lisp_Object prop
17718 = Fget_char_property (make_number (PT),
17719 Qdisplay, Qnil);
17720 result =
17721 (!NILP (prop)
17722 && display_prop_string_p (prop, glyph->object));
17723 break;
17724 }
17725 }
17726 }
17727 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17728 {
17729 /* If the row ends in middle of a real character,
17730 and the line is continued, we want the cursor here.
17731 That's because CHARPOS (ROW->end.pos) would equal
17732 PT if PT is before the character. */
17733 if (!row->ends_in_ellipsis_p)
17734 result = row->continued_p;
17735 else
17736 /* If the row ends in an ellipsis, then
17737 CHARPOS (ROW->end.pos) will equal point after the
17738 invisible text. We want that position to be displayed
17739 after the ellipsis. */
17740 result = 0;
17741 }
17742 /* If the row ends at ZV, display the cursor at the end of that
17743 row instead of at the start of the row below. */
17744 else if (row->ends_at_zv_p)
17745 result = 1;
17746 else
17747 result = 0;
17748 }
17749
17750 return result;
17751 }
17752
17753 \f
17754
17755 /* Push the display property PROP so that it will be rendered at the
17756 current position in IT. Return 1 if PROP was successfully pushed,
17757 0 otherwise. */
17758
17759 static int
17760 push_display_prop (struct it *it, Lisp_Object prop)
17761 {
17762 xassert (it->method == GET_FROM_BUFFER);
17763
17764 push_it (it, NULL);
17765
17766 if (STRINGP (prop))
17767 {
17768 if (SCHARS (prop) == 0)
17769 {
17770 pop_it (it);
17771 return 0;
17772 }
17773
17774 it->string = prop;
17775 it->multibyte_p = STRING_MULTIBYTE (it->string);
17776 it->current.overlay_string_index = -1;
17777 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17778 it->end_charpos = it->string_nchars = SCHARS (it->string);
17779 it->method = GET_FROM_STRING;
17780 it->stop_charpos = 0;
17781 it->prev_stop = 0;
17782 it->base_level_stop = 0;
17783 it->string_from_display_prop_p = 1;
17784
17785 /* Force paragraph direction to be that of the parent
17786 buffer. */
17787 it->paragraph_embedding = (it->bidi_p ? it->bidi_it.paragraph_dir : L2R);
17788
17789 /* Do we need to reorder this string? */
17790 if (it->multibyte_p)
17791 it->bidi_p = !NILP (BVAR (current_buffer, bidi_display_reordering));
17792 else
17793 it->bidi_p = 0;
17794
17795 /* Set up the bidi iterator for this display string. */
17796 if (it->bidi_p)
17797 {
17798 it->bidi_it.string.lstring = it->string;
17799 it->bidi_it.string.s = NULL;
17800 it->bidi_it.string.schars = it->end_charpos;
17801 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
17802 it->bidi_it.string.from_disp_str = 1;
17803 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
17804 }
17805 }
17806 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17807 {
17808 it->method = GET_FROM_STRETCH;
17809 it->object = prop;
17810 }
17811 #ifdef HAVE_WINDOW_SYSTEM
17812 else if (IMAGEP (prop))
17813 {
17814 it->what = IT_IMAGE;
17815 it->image_id = lookup_image (it->f, prop);
17816 it->method = GET_FROM_IMAGE;
17817 }
17818 #endif /* HAVE_WINDOW_SYSTEM */
17819 else
17820 {
17821 pop_it (it); /* bogus display property, give up */
17822 return 0;
17823 }
17824
17825 return 1;
17826 }
17827
17828 /* Return the character-property PROP at the current position in IT. */
17829
17830 static Lisp_Object
17831 get_it_property (struct it *it, Lisp_Object prop)
17832 {
17833 Lisp_Object position;
17834
17835 if (STRINGP (it->object))
17836 position = make_number (IT_STRING_CHARPOS (*it));
17837 else if (BUFFERP (it->object))
17838 position = make_number (IT_CHARPOS (*it));
17839 else
17840 return Qnil;
17841
17842 return Fget_char_property (position, prop, it->object);
17843 }
17844
17845 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17846
17847 static void
17848 handle_line_prefix (struct it *it)
17849 {
17850 Lisp_Object prefix;
17851
17852 if (it->continuation_lines_width > 0)
17853 {
17854 prefix = get_it_property (it, Qwrap_prefix);
17855 if (NILP (prefix))
17856 prefix = Vwrap_prefix;
17857 }
17858 else
17859 {
17860 prefix = get_it_property (it, Qline_prefix);
17861 if (NILP (prefix))
17862 prefix = Vline_prefix;
17863 }
17864 if (! NILP (prefix) && push_display_prop (it, prefix))
17865 {
17866 /* If the prefix is wider than the window, and we try to wrap
17867 it, it would acquire its own wrap prefix, and so on till the
17868 iterator stack overflows. So, don't wrap the prefix. */
17869 it->line_wrap = TRUNCATE;
17870 it->avoid_cursor_p = 1;
17871 }
17872 }
17873
17874 \f
17875
17876 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17877 only for R2L lines from display_line and display_string, when they
17878 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
17879 the line/string needs to be continued on the next glyph row. */
17880 static void
17881 unproduce_glyphs (struct it *it, int n)
17882 {
17883 struct glyph *glyph, *end;
17884
17885 xassert (it->glyph_row);
17886 xassert (it->glyph_row->reversed_p);
17887 xassert (it->area == TEXT_AREA);
17888 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17889
17890 if (n > it->glyph_row->used[TEXT_AREA])
17891 n = it->glyph_row->used[TEXT_AREA];
17892 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17893 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17894 for ( ; glyph < end; glyph++)
17895 glyph[-n] = *glyph;
17896 }
17897
17898 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17899 and ROW->maxpos. */
17900 static void
17901 find_row_edges (struct it *it, struct glyph_row *row,
17902 EMACS_INT min_pos, EMACS_INT min_bpos,
17903 EMACS_INT max_pos, EMACS_INT max_bpos)
17904 {
17905 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17906 lines' rows is implemented for bidi-reordered rows. */
17907
17908 /* ROW->minpos is the value of min_pos, the minimal buffer position
17909 we have in ROW. */
17910 if (min_pos <= ZV)
17911 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17912 else
17913 /* We didn't find _any_ valid buffer positions in any of the
17914 glyphs, so we must trust the iterator's computed positions. */
17915 row->minpos = row->start.pos;
17916 if (max_pos <= 0)
17917 {
17918 max_pos = CHARPOS (it->current.pos);
17919 max_bpos = BYTEPOS (it->current.pos);
17920 }
17921
17922 /* Here are the various use-cases for ending the row, and the
17923 corresponding values for ROW->maxpos:
17924
17925 Line ends in a newline from buffer eol_pos + 1
17926 Line is continued from buffer max_pos + 1
17927 Line is truncated on right it->current.pos
17928 Line ends in a newline from string max_pos
17929 Line is continued from string max_pos
17930 Line is continued from display vector max_pos
17931 Line is entirely from a string min_pos == max_pos
17932 Line is entirely from a display vector min_pos == max_pos
17933 Line that ends at ZV ZV
17934
17935 If you discover other use-cases, please add them here as
17936 appropriate. */
17937 if (row->ends_at_zv_p)
17938 row->maxpos = it->current.pos;
17939 else if (row->used[TEXT_AREA])
17940 {
17941 if (row->ends_in_newline_from_string_p)
17942 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17943 else if (CHARPOS (it->eol_pos) > 0)
17944 SET_TEXT_POS (row->maxpos,
17945 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17946 else if (row->continued_p)
17947 {
17948 /* If max_pos is different from IT's current position, it
17949 means IT->method does not belong to the display element
17950 at max_pos. However, it also means that the display
17951 element at max_pos was displayed in its entirety on this
17952 line, which is equivalent to saying that the next line
17953 starts at the next buffer position. */
17954 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17955 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17956 else
17957 {
17958 INC_BOTH (max_pos, max_bpos);
17959 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17960 }
17961 }
17962 else if (row->truncated_on_right_p)
17963 /* display_line already called reseat_at_next_visible_line_start,
17964 which puts the iterator at the beginning of the next line, in
17965 the logical order. */
17966 row->maxpos = it->current.pos;
17967 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17968 /* A line that is entirely from a string/image/stretch... */
17969 row->maxpos = row->minpos;
17970 else
17971 abort ();
17972 }
17973 else
17974 row->maxpos = it->current.pos;
17975 }
17976
17977 /* Construct the glyph row IT->glyph_row in the desired matrix of
17978 IT->w from text at the current position of IT. See dispextern.h
17979 for an overview of struct it. Value is non-zero if
17980 IT->glyph_row displays text, as opposed to a line displaying ZV
17981 only. */
17982
17983 static int
17984 display_line (struct it *it)
17985 {
17986 struct glyph_row *row = it->glyph_row;
17987 Lisp_Object overlay_arrow_string;
17988 struct it wrap_it;
17989 int may_wrap = 0, wrap_x IF_LINT (= 0);
17990 int wrap_row_used = -1;
17991 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
17992 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
17993 int wrap_row_extra_line_spacing IF_LINT (= 0);
17994 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
17995 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
17996 int cvpos;
17997 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17998 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
17999
18000 /* We always start displaying at hpos zero even if hscrolled. */
18001 xassert (it->hpos == 0 && it->current_x == 0);
18002
18003 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18004 >= it->w->desired_matrix->nrows)
18005 {
18006 it->w->nrows_scale_factor++;
18007 fonts_changed_p = 1;
18008 return 0;
18009 }
18010
18011 /* Is IT->w showing the region? */
18012 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18013
18014 /* Clear the result glyph row and enable it. */
18015 prepare_desired_row (row);
18016
18017 row->y = it->current_y;
18018 row->start = it->start;
18019 row->continuation_lines_width = it->continuation_lines_width;
18020 row->displays_text_p = 1;
18021 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18022 it->starts_in_middle_of_char_p = 0;
18023
18024 /* Arrange the overlays nicely for our purposes. Usually, we call
18025 display_line on only one line at a time, in which case this
18026 can't really hurt too much, or we call it on lines which appear
18027 one after another in the buffer, in which case all calls to
18028 recenter_overlay_lists but the first will be pretty cheap. */
18029 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18030
18031 /* Move over display elements that are not visible because we are
18032 hscrolled. This may stop at an x-position < IT->first_visible_x
18033 if the first glyph is partially visible or if we hit a line end. */
18034 if (it->current_x < it->first_visible_x)
18035 {
18036 this_line_min_pos = row->start.pos;
18037 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18038 MOVE_TO_POS | MOVE_TO_X);
18039 /* Record the smallest positions seen while we moved over
18040 display elements that are not visible. This is needed by
18041 redisplay_internal for optimizing the case where the cursor
18042 stays inside the same line. The rest of this function only
18043 considers positions that are actually displayed, so
18044 RECORD_MAX_MIN_POS will not otherwise record positions that
18045 are hscrolled to the left of the left edge of the window. */
18046 min_pos = CHARPOS (this_line_min_pos);
18047 min_bpos = BYTEPOS (this_line_min_pos);
18048 }
18049 else
18050 {
18051 /* We only do this when not calling `move_it_in_display_line_to'
18052 above, because move_it_in_display_line_to calls
18053 handle_line_prefix itself. */
18054 handle_line_prefix (it);
18055 }
18056
18057 /* Get the initial row height. This is either the height of the
18058 text hscrolled, if there is any, or zero. */
18059 row->ascent = it->max_ascent;
18060 row->height = it->max_ascent + it->max_descent;
18061 row->phys_ascent = it->max_phys_ascent;
18062 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18063 row->extra_line_spacing = it->max_extra_line_spacing;
18064
18065 /* Utility macro to record max and min buffer positions seen until now. */
18066 #define RECORD_MAX_MIN_POS(IT) \
18067 do \
18068 { \
18069 if (IT_CHARPOS (*(IT)) < min_pos) \
18070 { \
18071 min_pos = IT_CHARPOS (*(IT)); \
18072 min_bpos = IT_BYTEPOS (*(IT)); \
18073 } \
18074 if (IT_CHARPOS (*(IT)) > max_pos) \
18075 { \
18076 max_pos = IT_CHARPOS (*(IT)); \
18077 max_bpos = IT_BYTEPOS (*(IT)); \
18078 } \
18079 } \
18080 while (0)
18081
18082 /* Loop generating characters. The loop is left with IT on the next
18083 character to display. */
18084 while (1)
18085 {
18086 int n_glyphs_before, hpos_before, x_before;
18087 int x, nglyphs;
18088 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18089
18090 /* Retrieve the next thing to display. Value is zero if end of
18091 buffer reached. */
18092 if (!get_next_display_element (it))
18093 {
18094 /* Maybe add a space at the end of this line that is used to
18095 display the cursor there under X. Set the charpos of the
18096 first glyph of blank lines not corresponding to any text
18097 to -1. */
18098 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18099 row->exact_window_width_line_p = 1;
18100 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18101 || row->used[TEXT_AREA] == 0)
18102 {
18103 row->glyphs[TEXT_AREA]->charpos = -1;
18104 row->displays_text_p = 0;
18105
18106 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18107 && (!MINI_WINDOW_P (it->w)
18108 || (minibuf_level && EQ (it->window, minibuf_window))))
18109 row->indicate_empty_line_p = 1;
18110 }
18111
18112 it->continuation_lines_width = 0;
18113 row->ends_at_zv_p = 1;
18114 /* A row that displays right-to-left text must always have
18115 its last face extended all the way to the end of line,
18116 even if this row ends in ZV, because we still write to
18117 the screen left to right. */
18118 if (row->reversed_p)
18119 extend_face_to_end_of_line (it);
18120 break;
18121 }
18122
18123 /* Now, get the metrics of what we want to display. This also
18124 generates glyphs in `row' (which is IT->glyph_row). */
18125 n_glyphs_before = row->used[TEXT_AREA];
18126 x = it->current_x;
18127
18128 /* Remember the line height so far in case the next element doesn't
18129 fit on the line. */
18130 if (it->line_wrap != TRUNCATE)
18131 {
18132 ascent = it->max_ascent;
18133 descent = it->max_descent;
18134 phys_ascent = it->max_phys_ascent;
18135 phys_descent = it->max_phys_descent;
18136
18137 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18138 {
18139 if (IT_DISPLAYING_WHITESPACE (it))
18140 may_wrap = 1;
18141 else if (may_wrap)
18142 {
18143 wrap_it = *it;
18144 wrap_x = x;
18145 wrap_row_used = row->used[TEXT_AREA];
18146 wrap_row_ascent = row->ascent;
18147 wrap_row_height = row->height;
18148 wrap_row_phys_ascent = row->phys_ascent;
18149 wrap_row_phys_height = row->phys_height;
18150 wrap_row_extra_line_spacing = row->extra_line_spacing;
18151 wrap_row_min_pos = min_pos;
18152 wrap_row_min_bpos = min_bpos;
18153 wrap_row_max_pos = max_pos;
18154 wrap_row_max_bpos = max_bpos;
18155 may_wrap = 0;
18156 }
18157 }
18158 }
18159
18160 PRODUCE_GLYPHS (it);
18161
18162 /* If this display element was in marginal areas, continue with
18163 the next one. */
18164 if (it->area != TEXT_AREA)
18165 {
18166 row->ascent = max (row->ascent, it->max_ascent);
18167 row->height = max (row->height, it->max_ascent + it->max_descent);
18168 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18169 row->phys_height = max (row->phys_height,
18170 it->max_phys_ascent + it->max_phys_descent);
18171 row->extra_line_spacing = max (row->extra_line_spacing,
18172 it->max_extra_line_spacing);
18173 set_iterator_to_next (it, 1);
18174 continue;
18175 }
18176
18177 /* Does the display element fit on the line? If we truncate
18178 lines, we should draw past the right edge of the window. If
18179 we don't truncate, we want to stop so that we can display the
18180 continuation glyph before the right margin. If lines are
18181 continued, there are two possible strategies for characters
18182 resulting in more than 1 glyph (e.g. tabs): Display as many
18183 glyphs as possible in this line and leave the rest for the
18184 continuation line, or display the whole element in the next
18185 line. Original redisplay did the former, so we do it also. */
18186 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18187 hpos_before = it->hpos;
18188 x_before = x;
18189
18190 if (/* Not a newline. */
18191 nglyphs > 0
18192 /* Glyphs produced fit entirely in the line. */
18193 && it->current_x < it->last_visible_x)
18194 {
18195 it->hpos += nglyphs;
18196 row->ascent = max (row->ascent, it->max_ascent);
18197 row->height = max (row->height, it->max_ascent + it->max_descent);
18198 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18199 row->phys_height = max (row->phys_height,
18200 it->max_phys_ascent + it->max_phys_descent);
18201 row->extra_line_spacing = max (row->extra_line_spacing,
18202 it->max_extra_line_spacing);
18203 if (it->current_x - it->pixel_width < it->first_visible_x)
18204 row->x = x - it->first_visible_x;
18205 /* Record the maximum and minimum buffer positions seen so
18206 far in glyphs that will be displayed by this row. */
18207 if (it->bidi_p)
18208 RECORD_MAX_MIN_POS (it);
18209 }
18210 else
18211 {
18212 int i, new_x;
18213 struct glyph *glyph;
18214
18215 for (i = 0; i < nglyphs; ++i, x = new_x)
18216 {
18217 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18218 new_x = x + glyph->pixel_width;
18219
18220 if (/* Lines are continued. */
18221 it->line_wrap != TRUNCATE
18222 && (/* Glyph doesn't fit on the line. */
18223 new_x > it->last_visible_x
18224 /* Or it fits exactly on a window system frame. */
18225 || (new_x == it->last_visible_x
18226 && FRAME_WINDOW_P (it->f))))
18227 {
18228 /* End of a continued line. */
18229
18230 if (it->hpos == 0
18231 || (new_x == it->last_visible_x
18232 && FRAME_WINDOW_P (it->f)))
18233 {
18234 /* Current glyph is the only one on the line or
18235 fits exactly on the line. We must continue
18236 the line because we can't draw the cursor
18237 after the glyph. */
18238 row->continued_p = 1;
18239 it->current_x = new_x;
18240 it->continuation_lines_width += new_x;
18241 ++it->hpos;
18242 /* Record the maximum and minimum buffer
18243 positions seen so far in glyphs that will be
18244 displayed by this row. */
18245 if (it->bidi_p)
18246 RECORD_MAX_MIN_POS (it);
18247 if (i == nglyphs - 1)
18248 {
18249 /* If line-wrap is on, check if a previous
18250 wrap point was found. */
18251 if (wrap_row_used > 0
18252 /* Even if there is a previous wrap
18253 point, continue the line here as
18254 usual, if (i) the previous character
18255 was a space or tab AND (ii) the
18256 current character is not. */
18257 && (!may_wrap
18258 || IT_DISPLAYING_WHITESPACE (it)))
18259 goto back_to_wrap;
18260
18261 set_iterator_to_next (it, 1);
18262 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18263 {
18264 if (!get_next_display_element (it))
18265 {
18266 row->exact_window_width_line_p = 1;
18267 it->continuation_lines_width = 0;
18268 row->continued_p = 0;
18269 row->ends_at_zv_p = 1;
18270 }
18271 else if (ITERATOR_AT_END_OF_LINE_P (it))
18272 {
18273 row->continued_p = 0;
18274 row->exact_window_width_line_p = 1;
18275 }
18276 }
18277 }
18278 }
18279 else if (CHAR_GLYPH_PADDING_P (*glyph)
18280 && !FRAME_WINDOW_P (it->f))
18281 {
18282 /* A padding glyph that doesn't fit on this line.
18283 This means the whole character doesn't fit
18284 on the line. */
18285 if (row->reversed_p)
18286 unproduce_glyphs (it, row->used[TEXT_AREA]
18287 - n_glyphs_before);
18288 row->used[TEXT_AREA] = n_glyphs_before;
18289
18290 /* Fill the rest of the row with continuation
18291 glyphs like in 20.x. */
18292 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18293 < row->glyphs[1 + TEXT_AREA])
18294 produce_special_glyphs (it, IT_CONTINUATION);
18295
18296 row->continued_p = 1;
18297 it->current_x = x_before;
18298 it->continuation_lines_width += x_before;
18299
18300 /* Restore the height to what it was before the
18301 element not fitting on the line. */
18302 it->max_ascent = ascent;
18303 it->max_descent = descent;
18304 it->max_phys_ascent = phys_ascent;
18305 it->max_phys_descent = phys_descent;
18306 }
18307 else if (wrap_row_used > 0)
18308 {
18309 back_to_wrap:
18310 if (row->reversed_p)
18311 unproduce_glyphs (it,
18312 row->used[TEXT_AREA] - wrap_row_used);
18313 *it = wrap_it;
18314 it->continuation_lines_width += wrap_x;
18315 row->used[TEXT_AREA] = wrap_row_used;
18316 row->ascent = wrap_row_ascent;
18317 row->height = wrap_row_height;
18318 row->phys_ascent = wrap_row_phys_ascent;
18319 row->phys_height = wrap_row_phys_height;
18320 row->extra_line_spacing = wrap_row_extra_line_spacing;
18321 min_pos = wrap_row_min_pos;
18322 min_bpos = wrap_row_min_bpos;
18323 max_pos = wrap_row_max_pos;
18324 max_bpos = wrap_row_max_bpos;
18325 row->continued_p = 1;
18326 row->ends_at_zv_p = 0;
18327 row->exact_window_width_line_p = 0;
18328 it->continuation_lines_width += x;
18329
18330 /* Make sure that a non-default face is extended
18331 up to the right margin of the window. */
18332 extend_face_to_end_of_line (it);
18333 }
18334 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18335 {
18336 /* A TAB that extends past the right edge of the
18337 window. This produces a single glyph on
18338 window system frames. We leave the glyph in
18339 this row and let it fill the row, but don't
18340 consume the TAB. */
18341 it->continuation_lines_width += it->last_visible_x;
18342 row->ends_in_middle_of_char_p = 1;
18343 row->continued_p = 1;
18344 glyph->pixel_width = it->last_visible_x - x;
18345 it->starts_in_middle_of_char_p = 1;
18346 }
18347 else
18348 {
18349 /* Something other than a TAB that draws past
18350 the right edge of the window. Restore
18351 positions to values before the element. */
18352 if (row->reversed_p)
18353 unproduce_glyphs (it, row->used[TEXT_AREA]
18354 - (n_glyphs_before + i));
18355 row->used[TEXT_AREA] = n_glyphs_before + i;
18356
18357 /* Display continuation glyphs. */
18358 if (!FRAME_WINDOW_P (it->f))
18359 produce_special_glyphs (it, IT_CONTINUATION);
18360 row->continued_p = 1;
18361
18362 it->current_x = x_before;
18363 it->continuation_lines_width += x;
18364 extend_face_to_end_of_line (it);
18365
18366 if (nglyphs > 1 && i > 0)
18367 {
18368 row->ends_in_middle_of_char_p = 1;
18369 it->starts_in_middle_of_char_p = 1;
18370 }
18371
18372 /* Restore the height to what it was before the
18373 element not fitting on the line. */
18374 it->max_ascent = ascent;
18375 it->max_descent = descent;
18376 it->max_phys_ascent = phys_ascent;
18377 it->max_phys_descent = phys_descent;
18378 }
18379
18380 break;
18381 }
18382 else if (new_x > it->first_visible_x)
18383 {
18384 /* Increment number of glyphs actually displayed. */
18385 ++it->hpos;
18386
18387 /* Record the maximum and minimum buffer positions
18388 seen so far in glyphs that will be displayed by
18389 this row. */
18390 if (it->bidi_p)
18391 RECORD_MAX_MIN_POS (it);
18392
18393 if (x < it->first_visible_x)
18394 /* Glyph is partially visible, i.e. row starts at
18395 negative X position. */
18396 row->x = x - it->first_visible_x;
18397 }
18398 else
18399 {
18400 /* Glyph is completely off the left margin of the
18401 window. This should not happen because of the
18402 move_it_in_display_line at the start of this
18403 function, unless the text display area of the
18404 window is empty. */
18405 xassert (it->first_visible_x <= it->last_visible_x);
18406 }
18407 }
18408
18409 row->ascent = max (row->ascent, it->max_ascent);
18410 row->height = max (row->height, it->max_ascent + it->max_descent);
18411 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18412 row->phys_height = max (row->phys_height,
18413 it->max_phys_ascent + it->max_phys_descent);
18414 row->extra_line_spacing = max (row->extra_line_spacing,
18415 it->max_extra_line_spacing);
18416
18417 /* End of this display line if row is continued. */
18418 if (row->continued_p || row->ends_at_zv_p)
18419 break;
18420 }
18421
18422 at_end_of_line:
18423 /* Is this a line end? If yes, we're also done, after making
18424 sure that a non-default face is extended up to the right
18425 margin of the window. */
18426 if (ITERATOR_AT_END_OF_LINE_P (it))
18427 {
18428 int used_before = row->used[TEXT_AREA];
18429
18430 row->ends_in_newline_from_string_p = STRINGP (it->object);
18431
18432 /* Add a space at the end of the line that is used to
18433 display the cursor there. */
18434 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18435 append_space_for_newline (it, 0);
18436
18437 /* Extend the face to the end of the line. */
18438 extend_face_to_end_of_line (it);
18439
18440 /* Make sure we have the position. */
18441 if (used_before == 0)
18442 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18443
18444 /* Record the position of the newline, for use in
18445 find_row_edges. */
18446 it->eol_pos = it->current.pos;
18447
18448 /* Consume the line end. This skips over invisible lines. */
18449 set_iterator_to_next (it, 1);
18450 it->continuation_lines_width = 0;
18451 break;
18452 }
18453
18454 /* Proceed with next display element. Note that this skips
18455 over lines invisible because of selective display. */
18456 set_iterator_to_next (it, 1);
18457
18458 /* If we truncate lines, we are done when the last displayed
18459 glyphs reach past the right margin of the window. */
18460 if (it->line_wrap == TRUNCATE
18461 && (FRAME_WINDOW_P (it->f)
18462 ? (it->current_x >= it->last_visible_x)
18463 : (it->current_x > it->last_visible_x)))
18464 {
18465 /* Maybe add truncation glyphs. */
18466 if (!FRAME_WINDOW_P (it->f))
18467 {
18468 int i, n;
18469
18470 if (!row->reversed_p)
18471 {
18472 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18473 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18474 break;
18475 }
18476 else
18477 {
18478 for (i = 0; i < row->used[TEXT_AREA]; i++)
18479 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18480 break;
18481 /* Remove any padding glyphs at the front of ROW, to
18482 make room for the truncation glyphs we will be
18483 adding below. The loop below always inserts at
18484 least one truncation glyph, so also remove the
18485 last glyph added to ROW. */
18486 unproduce_glyphs (it, i + 1);
18487 /* Adjust i for the loop below. */
18488 i = row->used[TEXT_AREA] - (i + 1);
18489 }
18490
18491 for (n = row->used[TEXT_AREA]; i < n; ++i)
18492 {
18493 row->used[TEXT_AREA] = i;
18494 produce_special_glyphs (it, IT_TRUNCATION);
18495 }
18496 }
18497 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18498 {
18499 /* Don't truncate if we can overflow newline into fringe. */
18500 if (!get_next_display_element (it))
18501 {
18502 it->continuation_lines_width = 0;
18503 row->ends_at_zv_p = 1;
18504 row->exact_window_width_line_p = 1;
18505 break;
18506 }
18507 if (ITERATOR_AT_END_OF_LINE_P (it))
18508 {
18509 row->exact_window_width_line_p = 1;
18510 goto at_end_of_line;
18511 }
18512 }
18513
18514 row->truncated_on_right_p = 1;
18515 it->continuation_lines_width = 0;
18516 reseat_at_next_visible_line_start (it, 0);
18517 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18518 it->hpos = hpos_before;
18519 it->current_x = x_before;
18520 break;
18521 }
18522 }
18523
18524 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18525 at the left window margin. */
18526 if (it->first_visible_x
18527 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18528 {
18529 if (!FRAME_WINDOW_P (it->f))
18530 insert_left_trunc_glyphs (it);
18531 row->truncated_on_left_p = 1;
18532 }
18533
18534 /* Remember the position at which this line ends.
18535
18536 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18537 cannot be before the call to find_row_edges below, since that is
18538 where these positions are determined. */
18539 row->end = it->current;
18540 if (!it->bidi_p)
18541 {
18542 row->minpos = row->start.pos;
18543 row->maxpos = row->end.pos;
18544 }
18545 else
18546 {
18547 /* ROW->minpos and ROW->maxpos must be the smallest and
18548 `1 + the largest' buffer positions in ROW. But if ROW was
18549 bidi-reordered, these two positions can be anywhere in the
18550 row, so we must determine them now. */
18551 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18552 }
18553
18554 /* If the start of this line is the overlay arrow-position, then
18555 mark this glyph row as the one containing the overlay arrow.
18556 This is clearly a mess with variable size fonts. It would be
18557 better to let it be displayed like cursors under X. */
18558 if ((row->displays_text_p || !overlay_arrow_seen)
18559 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18560 !NILP (overlay_arrow_string)))
18561 {
18562 /* Overlay arrow in window redisplay is a fringe bitmap. */
18563 if (STRINGP (overlay_arrow_string))
18564 {
18565 struct glyph_row *arrow_row
18566 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18567 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18568 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18569 struct glyph *p = row->glyphs[TEXT_AREA];
18570 struct glyph *p2, *end;
18571
18572 /* Copy the arrow glyphs. */
18573 while (glyph < arrow_end)
18574 *p++ = *glyph++;
18575
18576 /* Throw away padding glyphs. */
18577 p2 = p;
18578 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18579 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18580 ++p2;
18581 if (p2 > p)
18582 {
18583 while (p2 < end)
18584 *p++ = *p2++;
18585 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18586 }
18587 }
18588 else
18589 {
18590 xassert (INTEGERP (overlay_arrow_string));
18591 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18592 }
18593 overlay_arrow_seen = 1;
18594 }
18595
18596 /* Compute pixel dimensions of this line. */
18597 compute_line_metrics (it);
18598
18599 /* Record whether this row ends inside an ellipsis. */
18600 row->ends_in_ellipsis_p
18601 = (it->method == GET_FROM_DISPLAY_VECTOR
18602 && it->ellipsis_p);
18603
18604 /* Save fringe bitmaps in this row. */
18605 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18606 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18607 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18608 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18609
18610 it->left_user_fringe_bitmap = 0;
18611 it->left_user_fringe_face_id = 0;
18612 it->right_user_fringe_bitmap = 0;
18613 it->right_user_fringe_face_id = 0;
18614
18615 /* Maybe set the cursor. */
18616 cvpos = it->w->cursor.vpos;
18617 if ((cvpos < 0
18618 /* In bidi-reordered rows, keep checking for proper cursor
18619 position even if one has been found already, because buffer
18620 positions in such rows change non-linearly with ROW->VPOS,
18621 when a line is continued. One exception: when we are at ZV,
18622 display cursor on the first suitable glyph row, since all
18623 the empty rows after that also have their position set to ZV. */
18624 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18625 lines' rows is implemented for bidi-reordered rows. */
18626 || (it->bidi_p
18627 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18628 && PT >= MATRIX_ROW_START_CHARPOS (row)
18629 && PT <= MATRIX_ROW_END_CHARPOS (row)
18630 && cursor_row_p (row))
18631 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18632
18633 /* Highlight trailing whitespace. */
18634 if (!NILP (Vshow_trailing_whitespace))
18635 highlight_trailing_whitespace (it->f, it->glyph_row);
18636
18637 /* Prepare for the next line. This line starts horizontally at (X
18638 HPOS) = (0 0). Vertical positions are incremented. As a
18639 convenience for the caller, IT->glyph_row is set to the next
18640 row to be used. */
18641 it->current_x = it->hpos = 0;
18642 it->current_y += row->height;
18643 SET_TEXT_POS (it->eol_pos, 0, 0);
18644 ++it->vpos;
18645 ++it->glyph_row;
18646 /* The next row should by default use the same value of the
18647 reversed_p flag as this one. set_iterator_to_next decides when
18648 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18649 the flag accordingly. */
18650 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18651 it->glyph_row->reversed_p = row->reversed_p;
18652 it->start = row->end;
18653 return row->displays_text_p;
18654
18655 #undef RECORD_MAX_MIN_POS
18656 }
18657
18658 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18659 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18660 doc: /* Return paragraph direction at point in BUFFER.
18661 Value is either `left-to-right' or `right-to-left'.
18662 If BUFFER is omitted or nil, it defaults to the current buffer.
18663
18664 Paragraph direction determines how the text in the paragraph is displayed.
18665 In left-to-right paragraphs, text begins at the left margin of the window
18666 and the reading direction is generally left to right. In right-to-left
18667 paragraphs, text begins at the right margin and is read from right to left.
18668
18669 See also `bidi-paragraph-direction'. */)
18670 (Lisp_Object buffer)
18671 {
18672 struct buffer *buf = current_buffer;
18673 struct buffer *old = buf;
18674
18675 if (! NILP (buffer))
18676 {
18677 CHECK_BUFFER (buffer);
18678 buf = XBUFFER (buffer);
18679 }
18680
18681 if (NILP (BVAR (buf, bidi_display_reordering)))
18682 return Qleft_to_right;
18683 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18684 return BVAR (buf, bidi_paragraph_direction);
18685 else
18686 {
18687 /* Determine the direction from buffer text. We could try to
18688 use current_matrix if it is up to date, but this seems fast
18689 enough as it is. */
18690 struct bidi_it itb;
18691 EMACS_INT pos = BUF_PT (buf);
18692 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18693 int c;
18694
18695 set_buffer_temp (buf);
18696 /* bidi_paragraph_init finds the base direction of the paragraph
18697 by searching forward from paragraph start. We need the base
18698 direction of the current or _previous_ paragraph, so we need
18699 to make sure we are within that paragraph. To that end, find
18700 the previous non-empty line. */
18701 if (pos >= ZV && pos > BEGV)
18702 {
18703 pos--;
18704 bytepos = CHAR_TO_BYTE (pos);
18705 }
18706 while ((c = FETCH_BYTE (bytepos)) == '\n'
18707 || c == ' ' || c == '\t' || c == '\f')
18708 {
18709 if (bytepos <= BEGV_BYTE)
18710 break;
18711 bytepos--;
18712 pos--;
18713 }
18714 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18715 bytepos--;
18716 itb.charpos = pos;
18717 itb.bytepos = bytepos;
18718 itb.nchars = -1;
18719 itb.string.s = NULL;
18720 itb.string.lstring = Qnil;
18721 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18722 itb.first_elt = 1;
18723 itb.separator_limit = -1;
18724 itb.paragraph_dir = NEUTRAL_DIR;
18725
18726 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18727 set_buffer_temp (old);
18728 switch (itb.paragraph_dir)
18729 {
18730 case L2R:
18731 return Qleft_to_right;
18732 break;
18733 case R2L:
18734 return Qright_to_left;
18735 break;
18736 default:
18737 abort ();
18738 }
18739 }
18740 }
18741
18742
18743 \f
18744 /***********************************************************************
18745 Menu Bar
18746 ***********************************************************************/
18747
18748 /* Redisplay the menu bar in the frame for window W.
18749
18750 The menu bar of X frames that don't have X toolkit support is
18751 displayed in a special window W->frame->menu_bar_window.
18752
18753 The menu bar of terminal frames is treated specially as far as
18754 glyph matrices are concerned. Menu bar lines are not part of
18755 windows, so the update is done directly on the frame matrix rows
18756 for the menu bar. */
18757
18758 static void
18759 display_menu_bar (struct window *w)
18760 {
18761 struct frame *f = XFRAME (WINDOW_FRAME (w));
18762 struct it it;
18763 Lisp_Object items;
18764 int i;
18765
18766 /* Don't do all this for graphical frames. */
18767 #ifdef HAVE_NTGUI
18768 if (FRAME_W32_P (f))
18769 return;
18770 #endif
18771 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18772 if (FRAME_X_P (f))
18773 return;
18774 #endif
18775
18776 #ifdef HAVE_NS
18777 if (FRAME_NS_P (f))
18778 return;
18779 #endif /* HAVE_NS */
18780
18781 #ifdef USE_X_TOOLKIT
18782 xassert (!FRAME_WINDOW_P (f));
18783 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18784 it.first_visible_x = 0;
18785 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18786 #else /* not USE_X_TOOLKIT */
18787 if (FRAME_WINDOW_P (f))
18788 {
18789 /* Menu bar lines are displayed in the desired matrix of the
18790 dummy window menu_bar_window. */
18791 struct window *menu_w;
18792 xassert (WINDOWP (f->menu_bar_window));
18793 menu_w = XWINDOW (f->menu_bar_window);
18794 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18795 MENU_FACE_ID);
18796 it.first_visible_x = 0;
18797 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18798 }
18799 else
18800 {
18801 /* This is a TTY frame, i.e. character hpos/vpos are used as
18802 pixel x/y. */
18803 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18804 MENU_FACE_ID);
18805 it.first_visible_x = 0;
18806 it.last_visible_x = FRAME_COLS (f);
18807 }
18808 #endif /* not USE_X_TOOLKIT */
18809
18810 /* FIXME: This should be controlled by a user option. See the
18811 comments in redisplay_tool_bar and display_mode_line about
18812 this. */
18813 it.paragraph_embedding = L2R;
18814
18815 if (! mode_line_inverse_video)
18816 /* Force the menu-bar to be displayed in the default face. */
18817 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18818
18819 /* Clear all rows of the menu bar. */
18820 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18821 {
18822 struct glyph_row *row = it.glyph_row + i;
18823 clear_glyph_row (row);
18824 row->enabled_p = 1;
18825 row->full_width_p = 1;
18826 }
18827
18828 /* Display all items of the menu bar. */
18829 items = FRAME_MENU_BAR_ITEMS (it.f);
18830 for (i = 0; i < ASIZE (items); i += 4)
18831 {
18832 Lisp_Object string;
18833
18834 /* Stop at nil string. */
18835 string = AREF (items, i + 1);
18836 if (NILP (string))
18837 break;
18838
18839 /* Remember where item was displayed. */
18840 ASET (items, i + 3, make_number (it.hpos));
18841
18842 /* Display the item, pad with one space. */
18843 if (it.current_x < it.last_visible_x)
18844 display_string (NULL, string, Qnil, 0, 0, &it,
18845 SCHARS (string) + 1, 0, 0, -1);
18846 }
18847
18848 /* Fill out the line with spaces. */
18849 if (it.current_x < it.last_visible_x)
18850 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18851
18852 /* Compute the total height of the lines. */
18853 compute_line_metrics (&it);
18854 }
18855
18856
18857 \f
18858 /***********************************************************************
18859 Mode Line
18860 ***********************************************************************/
18861
18862 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18863 FORCE is non-zero, redisplay mode lines unconditionally.
18864 Otherwise, redisplay only mode lines that are garbaged. Value is
18865 the number of windows whose mode lines were redisplayed. */
18866
18867 static int
18868 redisplay_mode_lines (Lisp_Object window, int force)
18869 {
18870 int nwindows = 0;
18871
18872 while (!NILP (window))
18873 {
18874 struct window *w = XWINDOW (window);
18875
18876 if (WINDOWP (w->hchild))
18877 nwindows += redisplay_mode_lines (w->hchild, force);
18878 else if (WINDOWP (w->vchild))
18879 nwindows += redisplay_mode_lines (w->vchild, force);
18880 else if (force
18881 || FRAME_GARBAGED_P (XFRAME (w->frame))
18882 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18883 {
18884 struct text_pos lpoint;
18885 struct buffer *old = current_buffer;
18886
18887 /* Set the window's buffer for the mode line display. */
18888 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18889 set_buffer_internal_1 (XBUFFER (w->buffer));
18890
18891 /* Point refers normally to the selected window. For any
18892 other window, set up appropriate value. */
18893 if (!EQ (window, selected_window))
18894 {
18895 struct text_pos pt;
18896
18897 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18898 if (CHARPOS (pt) < BEGV)
18899 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18900 else if (CHARPOS (pt) > (ZV - 1))
18901 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18902 else
18903 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18904 }
18905
18906 /* Display mode lines. */
18907 clear_glyph_matrix (w->desired_matrix);
18908 if (display_mode_lines (w))
18909 {
18910 ++nwindows;
18911 w->must_be_updated_p = 1;
18912 }
18913
18914 /* Restore old settings. */
18915 set_buffer_internal_1 (old);
18916 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18917 }
18918
18919 window = w->next;
18920 }
18921
18922 return nwindows;
18923 }
18924
18925
18926 /* Display the mode and/or header line of window W. Value is the
18927 sum number of mode lines and header lines displayed. */
18928
18929 static int
18930 display_mode_lines (struct window *w)
18931 {
18932 Lisp_Object old_selected_window, old_selected_frame;
18933 int n = 0;
18934
18935 old_selected_frame = selected_frame;
18936 selected_frame = w->frame;
18937 old_selected_window = selected_window;
18938 XSETWINDOW (selected_window, w);
18939
18940 /* These will be set while the mode line specs are processed. */
18941 line_number_displayed = 0;
18942 w->column_number_displayed = Qnil;
18943
18944 if (WINDOW_WANTS_MODELINE_P (w))
18945 {
18946 struct window *sel_w = XWINDOW (old_selected_window);
18947
18948 /* Select mode line face based on the real selected window. */
18949 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18950 BVAR (current_buffer, mode_line_format));
18951 ++n;
18952 }
18953
18954 if (WINDOW_WANTS_HEADER_LINE_P (w))
18955 {
18956 display_mode_line (w, HEADER_LINE_FACE_ID,
18957 BVAR (current_buffer, header_line_format));
18958 ++n;
18959 }
18960
18961 selected_frame = old_selected_frame;
18962 selected_window = old_selected_window;
18963 return n;
18964 }
18965
18966
18967 /* Display mode or header line of window W. FACE_ID specifies which
18968 line to display; it is either MODE_LINE_FACE_ID or
18969 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18970 display. Value is the pixel height of the mode/header line
18971 displayed. */
18972
18973 static int
18974 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18975 {
18976 struct it it;
18977 struct face *face;
18978 int count = SPECPDL_INDEX ();
18979
18980 init_iterator (&it, w, -1, -1, NULL, face_id);
18981 /* Don't extend on a previously drawn mode-line.
18982 This may happen if called from pos_visible_p. */
18983 it.glyph_row->enabled_p = 0;
18984 prepare_desired_row (it.glyph_row);
18985
18986 it.glyph_row->mode_line_p = 1;
18987
18988 if (! mode_line_inverse_video)
18989 /* Force the mode-line to be displayed in the default face. */
18990 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18991
18992 /* FIXME: This should be controlled by a user option. But
18993 supporting such an option is not trivial, since the mode line is
18994 made up of many separate strings, most of which are normally
18995 unibyte, and unibyte strings currently don't get reordered for
18996 display. */
18997 it.paragraph_embedding = L2R;
18998
18999 record_unwind_protect (unwind_format_mode_line,
19000 format_mode_line_unwind_data (NULL, Qnil, 0));
19001
19002 mode_line_target = MODE_LINE_DISPLAY;
19003
19004 /* Temporarily make frame's keyboard the current kboard so that
19005 kboard-local variables in the mode_line_format will get the right
19006 values. */
19007 push_kboard (FRAME_KBOARD (it.f));
19008 record_unwind_save_match_data ();
19009 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19010 pop_kboard ();
19011
19012 unbind_to (count, Qnil);
19013
19014 /* Fill up with spaces. */
19015 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19016
19017 compute_line_metrics (&it);
19018 it.glyph_row->full_width_p = 1;
19019 it.glyph_row->continued_p = 0;
19020 it.glyph_row->truncated_on_left_p = 0;
19021 it.glyph_row->truncated_on_right_p = 0;
19022
19023 /* Make a 3D mode-line have a shadow at its right end. */
19024 face = FACE_FROM_ID (it.f, face_id);
19025 extend_face_to_end_of_line (&it);
19026 if (face->box != FACE_NO_BOX)
19027 {
19028 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19029 + it.glyph_row->used[TEXT_AREA] - 1);
19030 last->right_box_line_p = 1;
19031 }
19032
19033 return it.glyph_row->height;
19034 }
19035
19036 /* Move element ELT in LIST to the front of LIST.
19037 Return the updated list. */
19038
19039 static Lisp_Object
19040 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19041 {
19042 register Lisp_Object tail, prev;
19043 register Lisp_Object tem;
19044
19045 tail = list;
19046 prev = Qnil;
19047 while (CONSP (tail))
19048 {
19049 tem = XCAR (tail);
19050
19051 if (EQ (elt, tem))
19052 {
19053 /* Splice out the link TAIL. */
19054 if (NILP (prev))
19055 list = XCDR (tail);
19056 else
19057 Fsetcdr (prev, XCDR (tail));
19058
19059 /* Now make it the first. */
19060 Fsetcdr (tail, list);
19061 return tail;
19062 }
19063 else
19064 prev = tail;
19065 tail = XCDR (tail);
19066 QUIT;
19067 }
19068
19069 /* Not found--return unchanged LIST. */
19070 return list;
19071 }
19072
19073 /* Contribute ELT to the mode line for window IT->w. How it
19074 translates into text depends on its data type.
19075
19076 IT describes the display environment in which we display, as usual.
19077
19078 DEPTH is the depth in recursion. It is used to prevent
19079 infinite recursion here.
19080
19081 FIELD_WIDTH is the number of characters the display of ELT should
19082 occupy in the mode line, and PRECISION is the maximum number of
19083 characters to display from ELT's representation. See
19084 display_string for details.
19085
19086 Returns the hpos of the end of the text generated by ELT.
19087
19088 PROPS is a property list to add to any string we encounter.
19089
19090 If RISKY is nonzero, remove (disregard) any properties in any string
19091 we encounter, and ignore :eval and :propertize.
19092
19093 The global variable `mode_line_target' determines whether the
19094 output is passed to `store_mode_line_noprop',
19095 `store_mode_line_string', or `display_string'. */
19096
19097 static int
19098 display_mode_element (struct it *it, int depth, int field_width, int precision,
19099 Lisp_Object elt, Lisp_Object props, int risky)
19100 {
19101 int n = 0, field, prec;
19102 int literal = 0;
19103
19104 tail_recurse:
19105 if (depth > 100)
19106 elt = build_string ("*too-deep*");
19107
19108 depth++;
19109
19110 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19111 {
19112 case Lisp_String:
19113 {
19114 /* A string: output it and check for %-constructs within it. */
19115 unsigned char c;
19116 EMACS_INT offset = 0;
19117
19118 if (SCHARS (elt) > 0
19119 && (!NILP (props) || risky))
19120 {
19121 Lisp_Object oprops, aelt;
19122 oprops = Ftext_properties_at (make_number (0), elt);
19123
19124 /* If the starting string's properties are not what
19125 we want, translate the string. Also, if the string
19126 is risky, do that anyway. */
19127
19128 if (NILP (Fequal (props, oprops)) || risky)
19129 {
19130 /* If the starting string has properties,
19131 merge the specified ones onto the existing ones. */
19132 if (! NILP (oprops) && !risky)
19133 {
19134 Lisp_Object tem;
19135
19136 oprops = Fcopy_sequence (oprops);
19137 tem = props;
19138 while (CONSP (tem))
19139 {
19140 oprops = Fplist_put (oprops, XCAR (tem),
19141 XCAR (XCDR (tem)));
19142 tem = XCDR (XCDR (tem));
19143 }
19144 props = oprops;
19145 }
19146
19147 aelt = Fassoc (elt, mode_line_proptrans_alist);
19148 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19149 {
19150 /* AELT is what we want. Move it to the front
19151 without consing. */
19152 elt = XCAR (aelt);
19153 mode_line_proptrans_alist
19154 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19155 }
19156 else
19157 {
19158 Lisp_Object tem;
19159
19160 /* If AELT has the wrong props, it is useless.
19161 so get rid of it. */
19162 if (! NILP (aelt))
19163 mode_line_proptrans_alist
19164 = Fdelq (aelt, mode_line_proptrans_alist);
19165
19166 elt = Fcopy_sequence (elt);
19167 Fset_text_properties (make_number (0), Flength (elt),
19168 props, elt);
19169 /* Add this item to mode_line_proptrans_alist. */
19170 mode_line_proptrans_alist
19171 = Fcons (Fcons (elt, props),
19172 mode_line_proptrans_alist);
19173 /* Truncate mode_line_proptrans_alist
19174 to at most 50 elements. */
19175 tem = Fnthcdr (make_number (50),
19176 mode_line_proptrans_alist);
19177 if (! NILP (tem))
19178 XSETCDR (tem, Qnil);
19179 }
19180 }
19181 }
19182
19183 offset = 0;
19184
19185 if (literal)
19186 {
19187 prec = precision - n;
19188 switch (mode_line_target)
19189 {
19190 case MODE_LINE_NOPROP:
19191 case MODE_LINE_TITLE:
19192 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19193 break;
19194 case MODE_LINE_STRING:
19195 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19196 break;
19197 case MODE_LINE_DISPLAY:
19198 n += display_string (NULL, elt, Qnil, 0, 0, it,
19199 0, prec, 0, STRING_MULTIBYTE (elt));
19200 break;
19201 }
19202
19203 break;
19204 }
19205
19206 /* Handle the non-literal case. */
19207
19208 while ((precision <= 0 || n < precision)
19209 && SREF (elt, offset) != 0
19210 && (mode_line_target != MODE_LINE_DISPLAY
19211 || it->current_x < it->last_visible_x))
19212 {
19213 EMACS_INT last_offset = offset;
19214
19215 /* Advance to end of string or next format specifier. */
19216 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19217 ;
19218
19219 if (offset - 1 != last_offset)
19220 {
19221 EMACS_INT nchars, nbytes;
19222
19223 /* Output to end of string or up to '%'. Field width
19224 is length of string. Don't output more than
19225 PRECISION allows us. */
19226 offset--;
19227
19228 prec = c_string_width (SDATA (elt) + last_offset,
19229 offset - last_offset, precision - n,
19230 &nchars, &nbytes);
19231
19232 switch (mode_line_target)
19233 {
19234 case MODE_LINE_NOPROP:
19235 case MODE_LINE_TITLE:
19236 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19237 break;
19238 case MODE_LINE_STRING:
19239 {
19240 EMACS_INT bytepos = last_offset;
19241 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19242 EMACS_INT endpos = (precision <= 0
19243 ? string_byte_to_char (elt, offset)
19244 : charpos + nchars);
19245
19246 n += store_mode_line_string (NULL,
19247 Fsubstring (elt, make_number (charpos),
19248 make_number (endpos)),
19249 0, 0, 0, Qnil);
19250 }
19251 break;
19252 case MODE_LINE_DISPLAY:
19253 {
19254 EMACS_INT bytepos = last_offset;
19255 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19256
19257 if (precision <= 0)
19258 nchars = string_byte_to_char (elt, offset) - charpos;
19259 n += display_string (NULL, elt, Qnil, 0, charpos,
19260 it, 0, nchars, 0,
19261 STRING_MULTIBYTE (elt));
19262 }
19263 break;
19264 }
19265 }
19266 else /* c == '%' */
19267 {
19268 EMACS_INT percent_position = offset;
19269
19270 /* Get the specified minimum width. Zero means
19271 don't pad. */
19272 field = 0;
19273 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19274 field = field * 10 + c - '0';
19275
19276 /* Don't pad beyond the total padding allowed. */
19277 if (field_width - n > 0 && field > field_width - n)
19278 field = field_width - n;
19279
19280 /* Note that either PRECISION <= 0 or N < PRECISION. */
19281 prec = precision - n;
19282
19283 if (c == 'M')
19284 n += display_mode_element (it, depth, field, prec,
19285 Vglobal_mode_string, props,
19286 risky);
19287 else if (c != 0)
19288 {
19289 int multibyte;
19290 EMACS_INT bytepos, charpos;
19291 const char *spec;
19292 Lisp_Object string;
19293
19294 bytepos = percent_position;
19295 charpos = (STRING_MULTIBYTE (elt)
19296 ? string_byte_to_char (elt, bytepos)
19297 : bytepos);
19298 spec = decode_mode_spec (it->w, c, field, &string);
19299 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19300
19301 switch (mode_line_target)
19302 {
19303 case MODE_LINE_NOPROP:
19304 case MODE_LINE_TITLE:
19305 n += store_mode_line_noprop (spec, field, prec);
19306 break;
19307 case MODE_LINE_STRING:
19308 {
19309 int len = strlen (spec);
19310 Lisp_Object tem = make_string (spec, len);
19311 props = Ftext_properties_at (make_number (charpos), elt);
19312 /* Should only keep face property in props */
19313 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19314 }
19315 break;
19316 case MODE_LINE_DISPLAY:
19317 {
19318 int nglyphs_before, nwritten;
19319
19320 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19321 nwritten = display_string (spec, string, elt,
19322 charpos, 0, it,
19323 field, prec, 0,
19324 multibyte);
19325
19326 /* Assign to the glyphs written above the
19327 string where the `%x' came from, position
19328 of the `%'. */
19329 if (nwritten > 0)
19330 {
19331 struct glyph *glyph
19332 = (it->glyph_row->glyphs[TEXT_AREA]
19333 + nglyphs_before);
19334 int i;
19335
19336 for (i = 0; i < nwritten; ++i)
19337 {
19338 glyph[i].object = elt;
19339 glyph[i].charpos = charpos;
19340 }
19341
19342 n += nwritten;
19343 }
19344 }
19345 break;
19346 }
19347 }
19348 else /* c == 0 */
19349 break;
19350 }
19351 }
19352 }
19353 break;
19354
19355 case Lisp_Symbol:
19356 /* A symbol: process the value of the symbol recursively
19357 as if it appeared here directly. Avoid error if symbol void.
19358 Special case: if value of symbol is a string, output the string
19359 literally. */
19360 {
19361 register Lisp_Object tem;
19362
19363 /* If the variable is not marked as risky to set
19364 then its contents are risky to use. */
19365 if (NILP (Fget (elt, Qrisky_local_variable)))
19366 risky = 1;
19367
19368 tem = Fboundp (elt);
19369 if (!NILP (tem))
19370 {
19371 tem = Fsymbol_value (elt);
19372 /* If value is a string, output that string literally:
19373 don't check for % within it. */
19374 if (STRINGP (tem))
19375 literal = 1;
19376
19377 if (!EQ (tem, elt))
19378 {
19379 /* Give up right away for nil or t. */
19380 elt = tem;
19381 goto tail_recurse;
19382 }
19383 }
19384 }
19385 break;
19386
19387 case Lisp_Cons:
19388 {
19389 register Lisp_Object car, tem;
19390
19391 /* A cons cell: five distinct cases.
19392 If first element is :eval or :propertize, do something special.
19393 If first element is a string or a cons, process all the elements
19394 and effectively concatenate them.
19395 If first element is a negative number, truncate displaying cdr to
19396 at most that many characters. If positive, pad (with spaces)
19397 to at least that many characters.
19398 If first element is a symbol, process the cadr or caddr recursively
19399 according to whether the symbol's value is non-nil or nil. */
19400 car = XCAR (elt);
19401 if (EQ (car, QCeval))
19402 {
19403 /* An element of the form (:eval FORM) means evaluate FORM
19404 and use the result as mode line elements. */
19405
19406 if (risky)
19407 break;
19408
19409 if (CONSP (XCDR (elt)))
19410 {
19411 Lisp_Object spec;
19412 spec = safe_eval (XCAR (XCDR (elt)));
19413 n += display_mode_element (it, depth, field_width - n,
19414 precision - n, spec, props,
19415 risky);
19416 }
19417 }
19418 else if (EQ (car, QCpropertize))
19419 {
19420 /* An element of the form (:propertize ELT PROPS...)
19421 means display ELT but applying properties PROPS. */
19422
19423 if (risky)
19424 break;
19425
19426 if (CONSP (XCDR (elt)))
19427 n += display_mode_element (it, depth, field_width - n,
19428 precision - n, XCAR (XCDR (elt)),
19429 XCDR (XCDR (elt)), risky);
19430 }
19431 else if (SYMBOLP (car))
19432 {
19433 tem = Fboundp (car);
19434 elt = XCDR (elt);
19435 if (!CONSP (elt))
19436 goto invalid;
19437 /* elt is now the cdr, and we know it is a cons cell.
19438 Use its car if CAR has a non-nil value. */
19439 if (!NILP (tem))
19440 {
19441 tem = Fsymbol_value (car);
19442 if (!NILP (tem))
19443 {
19444 elt = XCAR (elt);
19445 goto tail_recurse;
19446 }
19447 }
19448 /* Symbol's value is nil (or symbol is unbound)
19449 Get the cddr of the original list
19450 and if possible find the caddr and use that. */
19451 elt = XCDR (elt);
19452 if (NILP (elt))
19453 break;
19454 else if (!CONSP (elt))
19455 goto invalid;
19456 elt = XCAR (elt);
19457 goto tail_recurse;
19458 }
19459 else if (INTEGERP (car))
19460 {
19461 register int lim = XINT (car);
19462 elt = XCDR (elt);
19463 if (lim < 0)
19464 {
19465 /* Negative int means reduce maximum width. */
19466 if (precision <= 0)
19467 precision = -lim;
19468 else
19469 precision = min (precision, -lim);
19470 }
19471 else if (lim > 0)
19472 {
19473 /* Padding specified. Don't let it be more than
19474 current maximum. */
19475 if (precision > 0)
19476 lim = min (precision, lim);
19477
19478 /* If that's more padding than already wanted, queue it.
19479 But don't reduce padding already specified even if
19480 that is beyond the current truncation point. */
19481 field_width = max (lim, field_width);
19482 }
19483 goto tail_recurse;
19484 }
19485 else if (STRINGP (car) || CONSP (car))
19486 {
19487 Lisp_Object halftail = elt;
19488 int len = 0;
19489
19490 while (CONSP (elt)
19491 && (precision <= 0 || n < precision))
19492 {
19493 n += display_mode_element (it, depth,
19494 /* Do padding only after the last
19495 element in the list. */
19496 (! CONSP (XCDR (elt))
19497 ? field_width - n
19498 : 0),
19499 precision - n, XCAR (elt),
19500 props, risky);
19501 elt = XCDR (elt);
19502 len++;
19503 if ((len & 1) == 0)
19504 halftail = XCDR (halftail);
19505 /* Check for cycle. */
19506 if (EQ (halftail, elt))
19507 break;
19508 }
19509 }
19510 }
19511 break;
19512
19513 default:
19514 invalid:
19515 elt = build_string ("*invalid*");
19516 goto tail_recurse;
19517 }
19518
19519 /* Pad to FIELD_WIDTH. */
19520 if (field_width > 0 && n < field_width)
19521 {
19522 switch (mode_line_target)
19523 {
19524 case MODE_LINE_NOPROP:
19525 case MODE_LINE_TITLE:
19526 n += store_mode_line_noprop ("", field_width - n, 0);
19527 break;
19528 case MODE_LINE_STRING:
19529 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19530 break;
19531 case MODE_LINE_DISPLAY:
19532 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19533 0, 0, 0);
19534 break;
19535 }
19536 }
19537
19538 return n;
19539 }
19540
19541 /* Store a mode-line string element in mode_line_string_list.
19542
19543 If STRING is non-null, display that C string. Otherwise, the Lisp
19544 string LISP_STRING is displayed.
19545
19546 FIELD_WIDTH is the minimum number of output glyphs to produce.
19547 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19548 with spaces. FIELD_WIDTH <= 0 means don't pad.
19549
19550 PRECISION is the maximum number of characters to output from
19551 STRING. PRECISION <= 0 means don't truncate the string.
19552
19553 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19554 properties to the string.
19555
19556 PROPS are the properties to add to the string.
19557 The mode_line_string_face face property is always added to the string.
19558 */
19559
19560 static int
19561 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19562 int field_width, int precision, Lisp_Object props)
19563 {
19564 EMACS_INT len;
19565 int n = 0;
19566
19567 if (string != NULL)
19568 {
19569 len = strlen (string);
19570 if (precision > 0 && len > precision)
19571 len = precision;
19572 lisp_string = make_string (string, len);
19573 if (NILP (props))
19574 props = mode_line_string_face_prop;
19575 else if (!NILP (mode_line_string_face))
19576 {
19577 Lisp_Object face = Fplist_get (props, Qface);
19578 props = Fcopy_sequence (props);
19579 if (NILP (face))
19580 face = mode_line_string_face;
19581 else
19582 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19583 props = Fplist_put (props, Qface, face);
19584 }
19585 Fadd_text_properties (make_number (0), make_number (len),
19586 props, lisp_string);
19587 }
19588 else
19589 {
19590 len = XFASTINT (Flength (lisp_string));
19591 if (precision > 0 && len > precision)
19592 {
19593 len = precision;
19594 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19595 precision = -1;
19596 }
19597 if (!NILP (mode_line_string_face))
19598 {
19599 Lisp_Object face;
19600 if (NILP (props))
19601 props = Ftext_properties_at (make_number (0), lisp_string);
19602 face = Fplist_get (props, Qface);
19603 if (NILP (face))
19604 face = mode_line_string_face;
19605 else
19606 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19607 props = Fcons (Qface, Fcons (face, Qnil));
19608 if (copy_string)
19609 lisp_string = Fcopy_sequence (lisp_string);
19610 }
19611 if (!NILP (props))
19612 Fadd_text_properties (make_number (0), make_number (len),
19613 props, lisp_string);
19614 }
19615
19616 if (len > 0)
19617 {
19618 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19619 n += len;
19620 }
19621
19622 if (field_width > len)
19623 {
19624 field_width -= len;
19625 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19626 if (!NILP (props))
19627 Fadd_text_properties (make_number (0), make_number (field_width),
19628 props, lisp_string);
19629 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19630 n += field_width;
19631 }
19632
19633 return n;
19634 }
19635
19636
19637 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19638 1, 4, 0,
19639 doc: /* Format a string out of a mode line format specification.
19640 First arg FORMAT specifies the mode line format (see `mode-line-format'
19641 for details) to use.
19642
19643 By default, the format is evaluated for the currently selected window.
19644
19645 Optional second arg FACE specifies the face property to put on all
19646 characters for which no face is specified. The value nil means the
19647 default face. The value t means whatever face the window's mode line
19648 currently uses (either `mode-line' or `mode-line-inactive',
19649 depending on whether the window is the selected window or not).
19650 An integer value means the value string has no text
19651 properties.
19652
19653 Optional third and fourth args WINDOW and BUFFER specify the window
19654 and buffer to use as the context for the formatting (defaults
19655 are the selected window and the WINDOW's buffer). */)
19656 (Lisp_Object format, Lisp_Object face,
19657 Lisp_Object window, Lisp_Object buffer)
19658 {
19659 struct it it;
19660 int len;
19661 struct window *w;
19662 struct buffer *old_buffer = NULL;
19663 int face_id;
19664 int no_props = INTEGERP (face);
19665 int count = SPECPDL_INDEX ();
19666 Lisp_Object str;
19667 int string_start = 0;
19668
19669 if (NILP (window))
19670 window = selected_window;
19671 CHECK_WINDOW (window);
19672 w = XWINDOW (window);
19673
19674 if (NILP (buffer))
19675 buffer = w->buffer;
19676 CHECK_BUFFER (buffer);
19677
19678 /* Make formatting the modeline a non-op when noninteractive, otherwise
19679 there will be problems later caused by a partially initialized frame. */
19680 if (NILP (format) || noninteractive)
19681 return empty_unibyte_string;
19682
19683 if (no_props)
19684 face = Qnil;
19685
19686 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19687 : EQ (face, Qt) ? (EQ (window, selected_window)
19688 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19689 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19690 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19691 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19692 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19693 : DEFAULT_FACE_ID;
19694
19695 if (XBUFFER (buffer) != current_buffer)
19696 old_buffer = current_buffer;
19697
19698 /* Save things including mode_line_proptrans_alist,
19699 and set that to nil so that we don't alter the outer value. */
19700 record_unwind_protect (unwind_format_mode_line,
19701 format_mode_line_unwind_data
19702 (old_buffer, selected_window, 1));
19703 mode_line_proptrans_alist = Qnil;
19704
19705 Fselect_window (window, Qt);
19706 if (old_buffer)
19707 set_buffer_internal_1 (XBUFFER (buffer));
19708
19709 init_iterator (&it, w, -1, -1, NULL, face_id);
19710
19711 if (no_props)
19712 {
19713 mode_line_target = MODE_LINE_NOPROP;
19714 mode_line_string_face_prop = Qnil;
19715 mode_line_string_list = Qnil;
19716 string_start = MODE_LINE_NOPROP_LEN (0);
19717 }
19718 else
19719 {
19720 mode_line_target = MODE_LINE_STRING;
19721 mode_line_string_list = Qnil;
19722 mode_line_string_face = face;
19723 mode_line_string_face_prop
19724 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19725 }
19726
19727 push_kboard (FRAME_KBOARD (it.f));
19728 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19729 pop_kboard ();
19730
19731 if (no_props)
19732 {
19733 len = MODE_LINE_NOPROP_LEN (string_start);
19734 str = make_string (mode_line_noprop_buf + string_start, len);
19735 }
19736 else
19737 {
19738 mode_line_string_list = Fnreverse (mode_line_string_list);
19739 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19740 empty_unibyte_string);
19741 }
19742
19743 unbind_to (count, Qnil);
19744 return str;
19745 }
19746
19747 /* Write a null-terminated, right justified decimal representation of
19748 the positive integer D to BUF using a minimal field width WIDTH. */
19749
19750 static void
19751 pint2str (register char *buf, register int width, register EMACS_INT d)
19752 {
19753 register char *p = buf;
19754
19755 if (d <= 0)
19756 *p++ = '0';
19757 else
19758 {
19759 while (d > 0)
19760 {
19761 *p++ = d % 10 + '0';
19762 d /= 10;
19763 }
19764 }
19765
19766 for (width -= (int) (p - buf); width > 0; --width)
19767 *p++ = ' ';
19768 *p-- = '\0';
19769 while (p > buf)
19770 {
19771 d = *buf;
19772 *buf++ = *p;
19773 *p-- = d;
19774 }
19775 }
19776
19777 /* Write a null-terminated, right justified decimal and "human
19778 readable" representation of the nonnegative integer D to BUF using
19779 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19780
19781 static const char power_letter[] =
19782 {
19783 0, /* no letter */
19784 'k', /* kilo */
19785 'M', /* mega */
19786 'G', /* giga */
19787 'T', /* tera */
19788 'P', /* peta */
19789 'E', /* exa */
19790 'Z', /* zetta */
19791 'Y' /* yotta */
19792 };
19793
19794 static void
19795 pint2hrstr (char *buf, int width, EMACS_INT d)
19796 {
19797 /* We aim to represent the nonnegative integer D as
19798 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19799 EMACS_INT quotient = d;
19800 int remainder = 0;
19801 /* -1 means: do not use TENTHS. */
19802 int tenths = -1;
19803 int exponent = 0;
19804
19805 /* Length of QUOTIENT.TENTHS as a string. */
19806 int length;
19807
19808 char * psuffix;
19809 char * p;
19810
19811 if (1000 <= quotient)
19812 {
19813 /* Scale to the appropriate EXPONENT. */
19814 do
19815 {
19816 remainder = quotient % 1000;
19817 quotient /= 1000;
19818 exponent++;
19819 }
19820 while (1000 <= quotient);
19821
19822 /* Round to nearest and decide whether to use TENTHS or not. */
19823 if (quotient <= 9)
19824 {
19825 tenths = remainder / 100;
19826 if (50 <= remainder % 100)
19827 {
19828 if (tenths < 9)
19829 tenths++;
19830 else
19831 {
19832 quotient++;
19833 if (quotient == 10)
19834 tenths = -1;
19835 else
19836 tenths = 0;
19837 }
19838 }
19839 }
19840 else
19841 if (500 <= remainder)
19842 {
19843 if (quotient < 999)
19844 quotient++;
19845 else
19846 {
19847 quotient = 1;
19848 exponent++;
19849 tenths = 0;
19850 }
19851 }
19852 }
19853
19854 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19855 if (tenths == -1 && quotient <= 99)
19856 if (quotient <= 9)
19857 length = 1;
19858 else
19859 length = 2;
19860 else
19861 length = 3;
19862 p = psuffix = buf + max (width, length);
19863
19864 /* Print EXPONENT. */
19865 *psuffix++ = power_letter[exponent];
19866 *psuffix = '\0';
19867
19868 /* Print TENTHS. */
19869 if (tenths >= 0)
19870 {
19871 *--p = '0' + tenths;
19872 *--p = '.';
19873 }
19874
19875 /* Print QUOTIENT. */
19876 do
19877 {
19878 int digit = quotient % 10;
19879 *--p = '0' + digit;
19880 }
19881 while ((quotient /= 10) != 0);
19882
19883 /* Print leading spaces. */
19884 while (buf < p)
19885 *--p = ' ';
19886 }
19887
19888 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19889 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19890 type of CODING_SYSTEM. Return updated pointer into BUF. */
19891
19892 static unsigned char invalid_eol_type[] = "(*invalid*)";
19893
19894 static char *
19895 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19896 {
19897 Lisp_Object val;
19898 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19899 const unsigned char *eol_str;
19900 int eol_str_len;
19901 /* The EOL conversion we are using. */
19902 Lisp_Object eoltype;
19903
19904 val = CODING_SYSTEM_SPEC (coding_system);
19905 eoltype = Qnil;
19906
19907 if (!VECTORP (val)) /* Not yet decided. */
19908 {
19909 if (multibyte)
19910 *buf++ = '-';
19911 if (eol_flag)
19912 eoltype = eol_mnemonic_undecided;
19913 /* Don't mention EOL conversion if it isn't decided. */
19914 }
19915 else
19916 {
19917 Lisp_Object attrs;
19918 Lisp_Object eolvalue;
19919
19920 attrs = AREF (val, 0);
19921 eolvalue = AREF (val, 2);
19922
19923 if (multibyte)
19924 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19925
19926 if (eol_flag)
19927 {
19928 /* The EOL conversion that is normal on this system. */
19929
19930 if (NILP (eolvalue)) /* Not yet decided. */
19931 eoltype = eol_mnemonic_undecided;
19932 else if (VECTORP (eolvalue)) /* Not yet decided. */
19933 eoltype = eol_mnemonic_undecided;
19934 else /* eolvalue is Qunix, Qdos, or Qmac. */
19935 eoltype = (EQ (eolvalue, Qunix)
19936 ? eol_mnemonic_unix
19937 : (EQ (eolvalue, Qdos) == 1
19938 ? eol_mnemonic_dos : eol_mnemonic_mac));
19939 }
19940 }
19941
19942 if (eol_flag)
19943 {
19944 /* Mention the EOL conversion if it is not the usual one. */
19945 if (STRINGP (eoltype))
19946 {
19947 eol_str = SDATA (eoltype);
19948 eol_str_len = SBYTES (eoltype);
19949 }
19950 else if (CHARACTERP (eoltype))
19951 {
19952 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19953 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19954 eol_str = tmp;
19955 }
19956 else
19957 {
19958 eol_str = invalid_eol_type;
19959 eol_str_len = sizeof (invalid_eol_type) - 1;
19960 }
19961 memcpy (buf, eol_str, eol_str_len);
19962 buf += eol_str_len;
19963 }
19964
19965 return buf;
19966 }
19967
19968 /* Return a string for the output of a mode line %-spec for window W,
19969 generated by character C. FIELD_WIDTH > 0 means pad the string
19970 returned with spaces to that value. Return a Lisp string in
19971 *STRING if the resulting string is taken from that Lisp string.
19972
19973 Note we operate on the current buffer for most purposes,
19974 the exception being w->base_line_pos. */
19975
19976 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19977
19978 static const char *
19979 decode_mode_spec (struct window *w, register int c, int field_width,
19980 Lisp_Object *string)
19981 {
19982 Lisp_Object obj;
19983 struct frame *f = XFRAME (WINDOW_FRAME (w));
19984 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19985 struct buffer *b = current_buffer;
19986
19987 obj = Qnil;
19988 *string = Qnil;
19989
19990 switch (c)
19991 {
19992 case '*':
19993 if (!NILP (BVAR (b, read_only)))
19994 return "%";
19995 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19996 return "*";
19997 return "-";
19998
19999 case '+':
20000 /* This differs from %* only for a modified read-only buffer. */
20001 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20002 return "*";
20003 if (!NILP (BVAR (b, read_only)))
20004 return "%";
20005 return "-";
20006
20007 case '&':
20008 /* This differs from %* in ignoring read-only-ness. */
20009 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20010 return "*";
20011 return "-";
20012
20013 case '%':
20014 return "%";
20015
20016 case '[':
20017 {
20018 int i;
20019 char *p;
20020
20021 if (command_loop_level > 5)
20022 return "[[[... ";
20023 p = decode_mode_spec_buf;
20024 for (i = 0; i < command_loop_level; i++)
20025 *p++ = '[';
20026 *p = 0;
20027 return decode_mode_spec_buf;
20028 }
20029
20030 case ']':
20031 {
20032 int i;
20033 char *p;
20034
20035 if (command_loop_level > 5)
20036 return " ...]]]";
20037 p = decode_mode_spec_buf;
20038 for (i = 0; i < command_loop_level; i++)
20039 *p++ = ']';
20040 *p = 0;
20041 return decode_mode_spec_buf;
20042 }
20043
20044 case '-':
20045 {
20046 register int i;
20047
20048 /* Let lots_of_dashes be a string of infinite length. */
20049 if (mode_line_target == MODE_LINE_NOPROP ||
20050 mode_line_target == MODE_LINE_STRING)
20051 return "--";
20052 if (field_width <= 0
20053 || field_width > sizeof (lots_of_dashes))
20054 {
20055 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20056 decode_mode_spec_buf[i] = '-';
20057 decode_mode_spec_buf[i] = '\0';
20058 return decode_mode_spec_buf;
20059 }
20060 else
20061 return lots_of_dashes;
20062 }
20063
20064 case 'b':
20065 obj = BVAR (b, name);
20066 break;
20067
20068 case 'c':
20069 /* %c and %l are ignored in `frame-title-format'.
20070 (In redisplay_internal, the frame title is drawn _before_ the
20071 windows are updated, so the stuff which depends on actual
20072 window contents (such as %l) may fail to render properly, or
20073 even crash emacs.) */
20074 if (mode_line_target == MODE_LINE_TITLE)
20075 return "";
20076 else
20077 {
20078 EMACS_INT col = current_column ();
20079 w->column_number_displayed = make_number (col);
20080 pint2str (decode_mode_spec_buf, field_width, col);
20081 return decode_mode_spec_buf;
20082 }
20083
20084 case 'e':
20085 #ifndef SYSTEM_MALLOC
20086 {
20087 if (NILP (Vmemory_full))
20088 return "";
20089 else
20090 return "!MEM FULL! ";
20091 }
20092 #else
20093 return "";
20094 #endif
20095
20096 case 'F':
20097 /* %F displays the frame name. */
20098 if (!NILP (f->title))
20099 return SSDATA (f->title);
20100 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20101 return SSDATA (f->name);
20102 return "Emacs";
20103
20104 case 'f':
20105 obj = BVAR (b, filename);
20106 break;
20107
20108 case 'i':
20109 {
20110 EMACS_INT size = ZV - BEGV;
20111 pint2str (decode_mode_spec_buf, field_width, size);
20112 return decode_mode_spec_buf;
20113 }
20114
20115 case 'I':
20116 {
20117 EMACS_INT size = ZV - BEGV;
20118 pint2hrstr (decode_mode_spec_buf, field_width, size);
20119 return decode_mode_spec_buf;
20120 }
20121
20122 case 'l':
20123 {
20124 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20125 EMACS_INT topline, nlines, height;
20126 EMACS_INT junk;
20127
20128 /* %c and %l are ignored in `frame-title-format'. */
20129 if (mode_line_target == MODE_LINE_TITLE)
20130 return "";
20131
20132 startpos = XMARKER (w->start)->charpos;
20133 startpos_byte = marker_byte_position (w->start);
20134 height = WINDOW_TOTAL_LINES (w);
20135
20136 /* If we decided that this buffer isn't suitable for line numbers,
20137 don't forget that too fast. */
20138 if (EQ (w->base_line_pos, w->buffer))
20139 goto no_value;
20140 /* But do forget it, if the window shows a different buffer now. */
20141 else if (BUFFERP (w->base_line_pos))
20142 w->base_line_pos = Qnil;
20143
20144 /* If the buffer is very big, don't waste time. */
20145 if (INTEGERP (Vline_number_display_limit)
20146 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20147 {
20148 w->base_line_pos = Qnil;
20149 w->base_line_number = Qnil;
20150 goto no_value;
20151 }
20152
20153 if (INTEGERP (w->base_line_number)
20154 && INTEGERP (w->base_line_pos)
20155 && XFASTINT (w->base_line_pos) <= startpos)
20156 {
20157 line = XFASTINT (w->base_line_number);
20158 linepos = XFASTINT (w->base_line_pos);
20159 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20160 }
20161 else
20162 {
20163 line = 1;
20164 linepos = BUF_BEGV (b);
20165 linepos_byte = BUF_BEGV_BYTE (b);
20166 }
20167
20168 /* Count lines from base line to window start position. */
20169 nlines = display_count_lines (linepos_byte,
20170 startpos_byte,
20171 startpos, &junk);
20172
20173 topline = nlines + line;
20174
20175 /* Determine a new base line, if the old one is too close
20176 or too far away, or if we did not have one.
20177 "Too close" means it's plausible a scroll-down would
20178 go back past it. */
20179 if (startpos == BUF_BEGV (b))
20180 {
20181 w->base_line_number = make_number (topline);
20182 w->base_line_pos = make_number (BUF_BEGV (b));
20183 }
20184 else if (nlines < height + 25 || nlines > height * 3 + 50
20185 || linepos == BUF_BEGV (b))
20186 {
20187 EMACS_INT limit = BUF_BEGV (b);
20188 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20189 EMACS_INT position;
20190 EMACS_INT distance =
20191 (height * 2 + 30) * line_number_display_limit_width;
20192
20193 if (startpos - distance > limit)
20194 {
20195 limit = startpos - distance;
20196 limit_byte = CHAR_TO_BYTE (limit);
20197 }
20198
20199 nlines = display_count_lines (startpos_byte,
20200 limit_byte,
20201 - (height * 2 + 30),
20202 &position);
20203 /* If we couldn't find the lines we wanted within
20204 line_number_display_limit_width chars per line,
20205 give up on line numbers for this window. */
20206 if (position == limit_byte && limit == startpos - distance)
20207 {
20208 w->base_line_pos = w->buffer;
20209 w->base_line_number = Qnil;
20210 goto no_value;
20211 }
20212
20213 w->base_line_number = make_number (topline - nlines);
20214 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20215 }
20216
20217 /* Now count lines from the start pos to point. */
20218 nlines = display_count_lines (startpos_byte,
20219 PT_BYTE, PT, &junk);
20220
20221 /* Record that we did display the line number. */
20222 line_number_displayed = 1;
20223
20224 /* Make the string to show. */
20225 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20226 return decode_mode_spec_buf;
20227 no_value:
20228 {
20229 char* p = decode_mode_spec_buf;
20230 int pad = field_width - 2;
20231 while (pad-- > 0)
20232 *p++ = ' ';
20233 *p++ = '?';
20234 *p++ = '?';
20235 *p = '\0';
20236 return decode_mode_spec_buf;
20237 }
20238 }
20239 break;
20240
20241 case 'm':
20242 obj = BVAR (b, mode_name);
20243 break;
20244
20245 case 'n':
20246 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20247 return " Narrow";
20248 break;
20249
20250 case 'p':
20251 {
20252 EMACS_INT pos = marker_position (w->start);
20253 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20254
20255 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20256 {
20257 if (pos <= BUF_BEGV (b))
20258 return "All";
20259 else
20260 return "Bottom";
20261 }
20262 else if (pos <= BUF_BEGV (b))
20263 return "Top";
20264 else
20265 {
20266 if (total > 1000000)
20267 /* Do it differently for a large value, to avoid overflow. */
20268 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20269 else
20270 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20271 /* We can't normally display a 3-digit number,
20272 so get us a 2-digit number that is close. */
20273 if (total == 100)
20274 total = 99;
20275 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20276 return decode_mode_spec_buf;
20277 }
20278 }
20279
20280 /* Display percentage of size above the bottom of the screen. */
20281 case 'P':
20282 {
20283 EMACS_INT toppos = marker_position (w->start);
20284 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20285 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20286
20287 if (botpos >= BUF_ZV (b))
20288 {
20289 if (toppos <= BUF_BEGV (b))
20290 return "All";
20291 else
20292 return "Bottom";
20293 }
20294 else
20295 {
20296 if (total > 1000000)
20297 /* Do it differently for a large value, to avoid overflow. */
20298 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20299 else
20300 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20301 /* We can't normally display a 3-digit number,
20302 so get us a 2-digit number that is close. */
20303 if (total == 100)
20304 total = 99;
20305 if (toppos <= BUF_BEGV (b))
20306 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20307 else
20308 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20309 return decode_mode_spec_buf;
20310 }
20311 }
20312
20313 case 's':
20314 /* status of process */
20315 obj = Fget_buffer_process (Fcurrent_buffer ());
20316 if (NILP (obj))
20317 return "no process";
20318 #ifndef MSDOS
20319 obj = Fsymbol_name (Fprocess_status (obj));
20320 #endif
20321 break;
20322
20323 case '@':
20324 {
20325 int count = inhibit_garbage_collection ();
20326 Lisp_Object val = call1 (intern ("file-remote-p"),
20327 BVAR (current_buffer, directory));
20328 unbind_to (count, Qnil);
20329
20330 if (NILP (val))
20331 return "-";
20332 else
20333 return "@";
20334 }
20335
20336 case 't': /* indicate TEXT or BINARY */
20337 return "T";
20338
20339 case 'z':
20340 /* coding-system (not including end-of-line format) */
20341 case 'Z':
20342 /* coding-system (including end-of-line type) */
20343 {
20344 int eol_flag = (c == 'Z');
20345 char *p = decode_mode_spec_buf;
20346
20347 if (! FRAME_WINDOW_P (f))
20348 {
20349 /* No need to mention EOL here--the terminal never needs
20350 to do EOL conversion. */
20351 p = decode_mode_spec_coding (CODING_ID_NAME
20352 (FRAME_KEYBOARD_CODING (f)->id),
20353 p, 0);
20354 p = decode_mode_spec_coding (CODING_ID_NAME
20355 (FRAME_TERMINAL_CODING (f)->id),
20356 p, 0);
20357 }
20358 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20359 p, eol_flag);
20360
20361 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20362 #ifdef subprocesses
20363 obj = Fget_buffer_process (Fcurrent_buffer ());
20364 if (PROCESSP (obj))
20365 {
20366 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20367 p, eol_flag);
20368 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20369 p, eol_flag);
20370 }
20371 #endif /* subprocesses */
20372 #endif /* 0 */
20373 *p = 0;
20374 return decode_mode_spec_buf;
20375 }
20376 }
20377
20378 if (STRINGP (obj))
20379 {
20380 *string = obj;
20381 return SSDATA (obj);
20382 }
20383 else
20384 return "";
20385 }
20386
20387
20388 /* Count up to COUNT lines starting from START_BYTE.
20389 But don't go beyond LIMIT_BYTE.
20390 Return the number of lines thus found (always nonnegative).
20391
20392 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20393
20394 static EMACS_INT
20395 display_count_lines (EMACS_INT start_byte,
20396 EMACS_INT limit_byte, EMACS_INT count,
20397 EMACS_INT *byte_pos_ptr)
20398 {
20399 register unsigned char *cursor;
20400 unsigned char *base;
20401
20402 register EMACS_INT ceiling;
20403 register unsigned char *ceiling_addr;
20404 EMACS_INT orig_count = count;
20405
20406 /* If we are not in selective display mode,
20407 check only for newlines. */
20408 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20409 && !INTEGERP (BVAR (current_buffer, selective_display)));
20410
20411 if (count > 0)
20412 {
20413 while (start_byte < limit_byte)
20414 {
20415 ceiling = BUFFER_CEILING_OF (start_byte);
20416 ceiling = min (limit_byte - 1, ceiling);
20417 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20418 base = (cursor = BYTE_POS_ADDR (start_byte));
20419 while (1)
20420 {
20421 if (selective_display)
20422 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20423 ;
20424 else
20425 while (*cursor != '\n' && ++cursor != ceiling_addr)
20426 ;
20427
20428 if (cursor != ceiling_addr)
20429 {
20430 if (--count == 0)
20431 {
20432 start_byte += cursor - base + 1;
20433 *byte_pos_ptr = start_byte;
20434 return orig_count;
20435 }
20436 else
20437 if (++cursor == ceiling_addr)
20438 break;
20439 }
20440 else
20441 break;
20442 }
20443 start_byte += cursor - base;
20444 }
20445 }
20446 else
20447 {
20448 while (start_byte > limit_byte)
20449 {
20450 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20451 ceiling = max (limit_byte, ceiling);
20452 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20453 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20454 while (1)
20455 {
20456 if (selective_display)
20457 while (--cursor != ceiling_addr
20458 && *cursor != '\n' && *cursor != 015)
20459 ;
20460 else
20461 while (--cursor != ceiling_addr && *cursor != '\n')
20462 ;
20463
20464 if (cursor != ceiling_addr)
20465 {
20466 if (++count == 0)
20467 {
20468 start_byte += cursor - base + 1;
20469 *byte_pos_ptr = start_byte;
20470 /* When scanning backwards, we should
20471 not count the newline posterior to which we stop. */
20472 return - orig_count - 1;
20473 }
20474 }
20475 else
20476 break;
20477 }
20478 /* Here we add 1 to compensate for the last decrement
20479 of CURSOR, which took it past the valid range. */
20480 start_byte += cursor - base + 1;
20481 }
20482 }
20483
20484 *byte_pos_ptr = limit_byte;
20485
20486 if (count < 0)
20487 return - orig_count + count;
20488 return orig_count - count;
20489
20490 }
20491
20492
20493 \f
20494 /***********************************************************************
20495 Displaying strings
20496 ***********************************************************************/
20497
20498 /* Display a NUL-terminated string, starting with index START.
20499
20500 If STRING is non-null, display that C string. Otherwise, the Lisp
20501 string LISP_STRING is displayed. There's a case that STRING is
20502 non-null and LISP_STRING is not nil. It means STRING is a string
20503 data of LISP_STRING. In that case, we display LISP_STRING while
20504 ignoring its text properties.
20505
20506 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20507 FACE_STRING. Display STRING or LISP_STRING with the face at
20508 FACE_STRING_POS in FACE_STRING:
20509
20510 Display the string in the environment given by IT, but use the
20511 standard display table, temporarily.
20512
20513 FIELD_WIDTH is the minimum number of output glyphs to produce.
20514 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20515 with spaces. If STRING has more characters, more than FIELD_WIDTH
20516 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20517
20518 PRECISION is the maximum number of characters to output from
20519 STRING. PRECISION < 0 means don't truncate the string.
20520
20521 This is roughly equivalent to printf format specifiers:
20522
20523 FIELD_WIDTH PRECISION PRINTF
20524 ----------------------------------------
20525 -1 -1 %s
20526 -1 10 %.10s
20527 10 -1 %10s
20528 20 10 %20.10s
20529
20530 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20531 display them, and < 0 means obey the current buffer's value of
20532 enable_multibyte_characters.
20533
20534 Value is the number of columns displayed. */
20535
20536 static int
20537 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20538 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20539 int field_width, int precision, int max_x, int multibyte)
20540 {
20541 int hpos_at_start = it->hpos;
20542 int saved_face_id = it->face_id;
20543 struct glyph_row *row = it->glyph_row;
20544 EMACS_INT it_charpos;
20545
20546 /* Initialize the iterator IT for iteration over STRING beginning
20547 with index START. */
20548 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20549 precision, field_width, multibyte);
20550 if (string && STRINGP (lisp_string))
20551 /* LISP_STRING is the one returned by decode_mode_spec. We should
20552 ignore its text properties. */
20553 it->stop_charpos = it->end_charpos;
20554
20555 /* If displaying STRING, set up the face of the iterator from
20556 FACE_STRING, if that's given. */
20557 if (STRINGP (face_string))
20558 {
20559 EMACS_INT endptr;
20560 struct face *face;
20561
20562 it->face_id
20563 = face_at_string_position (it->w, face_string, face_string_pos,
20564 0, it->region_beg_charpos,
20565 it->region_end_charpos,
20566 &endptr, it->base_face_id, 0);
20567 face = FACE_FROM_ID (it->f, it->face_id);
20568 it->face_box_p = face->box != FACE_NO_BOX;
20569 }
20570
20571 /* Set max_x to the maximum allowed X position. Don't let it go
20572 beyond the right edge of the window. */
20573 if (max_x <= 0)
20574 max_x = it->last_visible_x;
20575 else
20576 max_x = min (max_x, it->last_visible_x);
20577
20578 /* Skip over display elements that are not visible. because IT->w is
20579 hscrolled. */
20580 if (it->current_x < it->first_visible_x)
20581 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20582 MOVE_TO_POS | MOVE_TO_X);
20583
20584 row->ascent = it->max_ascent;
20585 row->height = it->max_ascent + it->max_descent;
20586 row->phys_ascent = it->max_phys_ascent;
20587 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20588 row->extra_line_spacing = it->max_extra_line_spacing;
20589
20590 if (STRINGP (it->string))
20591 it_charpos = IT_STRING_CHARPOS (*it);
20592 else
20593 it_charpos = IT_CHARPOS (*it);
20594
20595 /* This condition is for the case that we are called with current_x
20596 past last_visible_x. */
20597 while (it->current_x < max_x)
20598 {
20599 int x_before, x, n_glyphs_before, i, nglyphs;
20600
20601 /* Get the next display element. */
20602 if (!get_next_display_element (it))
20603 break;
20604
20605 /* Produce glyphs. */
20606 x_before = it->current_x;
20607 n_glyphs_before = row->used[TEXT_AREA];
20608 PRODUCE_GLYPHS (it);
20609
20610 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20611 i = 0;
20612 x = x_before;
20613 while (i < nglyphs)
20614 {
20615 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20616
20617 if (it->line_wrap != TRUNCATE
20618 && x + glyph->pixel_width > max_x)
20619 {
20620 /* End of continued line or max_x reached. */
20621 if (CHAR_GLYPH_PADDING_P (*glyph))
20622 {
20623 /* A wide character is unbreakable. */
20624 if (row->reversed_p)
20625 unproduce_glyphs (it, row->used[TEXT_AREA]
20626 - n_glyphs_before);
20627 row->used[TEXT_AREA] = n_glyphs_before;
20628 it->current_x = x_before;
20629 }
20630 else
20631 {
20632 if (row->reversed_p)
20633 unproduce_glyphs (it, row->used[TEXT_AREA]
20634 - (n_glyphs_before + i));
20635 row->used[TEXT_AREA] = n_glyphs_before + i;
20636 it->current_x = x;
20637 }
20638 break;
20639 }
20640 else if (x + glyph->pixel_width >= it->first_visible_x)
20641 {
20642 /* Glyph is at least partially visible. */
20643 ++it->hpos;
20644 if (x < it->first_visible_x)
20645 row->x = x - it->first_visible_x;
20646 }
20647 else
20648 {
20649 /* Glyph is off the left margin of the display area.
20650 Should not happen. */
20651 abort ();
20652 }
20653
20654 row->ascent = max (row->ascent, it->max_ascent);
20655 row->height = max (row->height, it->max_ascent + it->max_descent);
20656 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20657 row->phys_height = max (row->phys_height,
20658 it->max_phys_ascent + it->max_phys_descent);
20659 row->extra_line_spacing = max (row->extra_line_spacing,
20660 it->max_extra_line_spacing);
20661 x += glyph->pixel_width;
20662 ++i;
20663 }
20664
20665 /* Stop if max_x reached. */
20666 if (i < nglyphs)
20667 break;
20668
20669 /* Stop at line ends. */
20670 if (ITERATOR_AT_END_OF_LINE_P (it))
20671 {
20672 it->continuation_lines_width = 0;
20673 break;
20674 }
20675
20676 set_iterator_to_next (it, 1);
20677 if (STRINGP (it->string))
20678 it_charpos = IT_STRING_CHARPOS (*it);
20679 else
20680 it_charpos = IT_CHARPOS (*it);
20681
20682 /* Stop if truncating at the right edge. */
20683 if (it->line_wrap == TRUNCATE
20684 && it->current_x >= it->last_visible_x)
20685 {
20686 /* Add truncation mark, but don't do it if the line is
20687 truncated at a padding space. */
20688 if (it_charpos < it->string_nchars)
20689 {
20690 if (!FRAME_WINDOW_P (it->f))
20691 {
20692 int ii, n;
20693
20694 if (it->current_x > it->last_visible_x)
20695 {
20696 if (!row->reversed_p)
20697 {
20698 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20699 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20700 break;
20701 }
20702 else
20703 {
20704 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
20705 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20706 break;
20707 unproduce_glyphs (it, ii + 1);
20708 ii = row->used[TEXT_AREA] - (ii + 1);
20709 }
20710 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20711 {
20712 row->used[TEXT_AREA] = ii;
20713 produce_special_glyphs (it, IT_TRUNCATION);
20714 }
20715 }
20716 produce_special_glyphs (it, IT_TRUNCATION);
20717 }
20718 row->truncated_on_right_p = 1;
20719 }
20720 break;
20721 }
20722 }
20723
20724 /* Maybe insert a truncation at the left. */
20725 if (it->first_visible_x
20726 && it_charpos > 0)
20727 {
20728 if (!FRAME_WINDOW_P (it->f))
20729 insert_left_trunc_glyphs (it);
20730 row->truncated_on_left_p = 1;
20731 }
20732
20733 it->face_id = saved_face_id;
20734
20735 /* Value is number of columns displayed. */
20736 return it->hpos - hpos_at_start;
20737 }
20738
20739
20740 \f
20741 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20742 appears as an element of LIST or as the car of an element of LIST.
20743 If PROPVAL is a list, compare each element against LIST in that
20744 way, and return 1/2 if any element of PROPVAL is found in LIST.
20745 Otherwise return 0. This function cannot quit.
20746 The return value is 2 if the text is invisible but with an ellipsis
20747 and 1 if it's invisible and without an ellipsis. */
20748
20749 int
20750 invisible_p (register Lisp_Object propval, Lisp_Object list)
20751 {
20752 register Lisp_Object tail, proptail;
20753
20754 for (tail = list; CONSP (tail); tail = XCDR (tail))
20755 {
20756 register Lisp_Object tem;
20757 tem = XCAR (tail);
20758 if (EQ (propval, tem))
20759 return 1;
20760 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20761 return NILP (XCDR (tem)) ? 1 : 2;
20762 }
20763
20764 if (CONSP (propval))
20765 {
20766 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20767 {
20768 Lisp_Object propelt;
20769 propelt = XCAR (proptail);
20770 for (tail = list; CONSP (tail); tail = XCDR (tail))
20771 {
20772 register Lisp_Object tem;
20773 tem = XCAR (tail);
20774 if (EQ (propelt, tem))
20775 return 1;
20776 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20777 return NILP (XCDR (tem)) ? 1 : 2;
20778 }
20779 }
20780 }
20781
20782 return 0;
20783 }
20784
20785 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20786 doc: /* Non-nil if the property makes the text invisible.
20787 POS-OR-PROP can be a marker or number, in which case it is taken to be
20788 a position in the current buffer and the value of the `invisible' property
20789 is checked; or it can be some other value, which is then presumed to be the
20790 value of the `invisible' property of the text of interest.
20791 The non-nil value returned can be t for truly invisible text or something
20792 else if the text is replaced by an ellipsis. */)
20793 (Lisp_Object pos_or_prop)
20794 {
20795 Lisp_Object prop
20796 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20797 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20798 : pos_or_prop);
20799 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20800 return (invis == 0 ? Qnil
20801 : invis == 1 ? Qt
20802 : make_number (invis));
20803 }
20804
20805 /* Calculate a width or height in pixels from a specification using
20806 the following elements:
20807
20808 SPEC ::=
20809 NUM - a (fractional) multiple of the default font width/height
20810 (NUM) - specifies exactly NUM pixels
20811 UNIT - a fixed number of pixels, see below.
20812 ELEMENT - size of a display element in pixels, see below.
20813 (NUM . SPEC) - equals NUM * SPEC
20814 (+ SPEC SPEC ...) - add pixel values
20815 (- SPEC SPEC ...) - subtract pixel values
20816 (- SPEC) - negate pixel value
20817
20818 NUM ::=
20819 INT or FLOAT - a number constant
20820 SYMBOL - use symbol's (buffer local) variable binding.
20821
20822 UNIT ::=
20823 in - pixels per inch *)
20824 mm - pixels per 1/1000 meter *)
20825 cm - pixels per 1/100 meter *)
20826 width - width of current font in pixels.
20827 height - height of current font in pixels.
20828
20829 *) using the ratio(s) defined in display-pixels-per-inch.
20830
20831 ELEMENT ::=
20832
20833 left-fringe - left fringe width in pixels
20834 right-fringe - right fringe width in pixels
20835
20836 left-margin - left margin width in pixels
20837 right-margin - right margin width in pixels
20838
20839 scroll-bar - scroll-bar area width in pixels
20840
20841 Examples:
20842
20843 Pixels corresponding to 5 inches:
20844 (5 . in)
20845
20846 Total width of non-text areas on left side of window (if scroll-bar is on left):
20847 '(space :width (+ left-fringe left-margin scroll-bar))
20848
20849 Align to first text column (in header line):
20850 '(space :align-to 0)
20851
20852 Align to middle of text area minus half the width of variable `my-image'
20853 containing a loaded image:
20854 '(space :align-to (0.5 . (- text my-image)))
20855
20856 Width of left margin minus width of 1 character in the default font:
20857 '(space :width (- left-margin 1))
20858
20859 Width of left margin minus width of 2 characters in the current font:
20860 '(space :width (- left-margin (2 . width)))
20861
20862 Center 1 character over left-margin (in header line):
20863 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20864
20865 Different ways to express width of left fringe plus left margin minus one pixel:
20866 '(space :width (- (+ left-fringe left-margin) (1)))
20867 '(space :width (+ left-fringe left-margin (- (1))))
20868 '(space :width (+ left-fringe left-margin (-1)))
20869
20870 */
20871
20872 #define NUMVAL(X) \
20873 ((INTEGERP (X) || FLOATP (X)) \
20874 ? XFLOATINT (X) \
20875 : - 1)
20876
20877 int
20878 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20879 struct font *font, int width_p, int *align_to)
20880 {
20881 double pixels;
20882
20883 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20884 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20885
20886 if (NILP (prop))
20887 return OK_PIXELS (0);
20888
20889 xassert (FRAME_LIVE_P (it->f));
20890
20891 if (SYMBOLP (prop))
20892 {
20893 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20894 {
20895 char *unit = SSDATA (SYMBOL_NAME (prop));
20896
20897 if (unit[0] == 'i' && unit[1] == 'n')
20898 pixels = 1.0;
20899 else if (unit[0] == 'm' && unit[1] == 'm')
20900 pixels = 25.4;
20901 else if (unit[0] == 'c' && unit[1] == 'm')
20902 pixels = 2.54;
20903 else
20904 pixels = 0;
20905 if (pixels > 0)
20906 {
20907 double ppi;
20908 #ifdef HAVE_WINDOW_SYSTEM
20909 if (FRAME_WINDOW_P (it->f)
20910 && (ppi = (width_p
20911 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20912 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20913 ppi > 0))
20914 return OK_PIXELS (ppi / pixels);
20915 #endif
20916
20917 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20918 || (CONSP (Vdisplay_pixels_per_inch)
20919 && (ppi = (width_p
20920 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20921 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20922 ppi > 0)))
20923 return OK_PIXELS (ppi / pixels);
20924
20925 return 0;
20926 }
20927 }
20928
20929 #ifdef HAVE_WINDOW_SYSTEM
20930 if (EQ (prop, Qheight))
20931 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20932 if (EQ (prop, Qwidth))
20933 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20934 #else
20935 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20936 return OK_PIXELS (1);
20937 #endif
20938
20939 if (EQ (prop, Qtext))
20940 return OK_PIXELS (width_p
20941 ? window_box_width (it->w, TEXT_AREA)
20942 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20943
20944 if (align_to && *align_to < 0)
20945 {
20946 *res = 0;
20947 if (EQ (prop, Qleft))
20948 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20949 if (EQ (prop, Qright))
20950 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20951 if (EQ (prop, Qcenter))
20952 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20953 + window_box_width (it->w, TEXT_AREA) / 2);
20954 if (EQ (prop, Qleft_fringe))
20955 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20956 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20957 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20958 if (EQ (prop, Qright_fringe))
20959 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20960 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20961 : window_box_right_offset (it->w, TEXT_AREA));
20962 if (EQ (prop, Qleft_margin))
20963 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20964 if (EQ (prop, Qright_margin))
20965 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20966 if (EQ (prop, Qscroll_bar))
20967 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20968 ? 0
20969 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20970 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20971 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20972 : 0)));
20973 }
20974 else
20975 {
20976 if (EQ (prop, Qleft_fringe))
20977 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20978 if (EQ (prop, Qright_fringe))
20979 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20980 if (EQ (prop, Qleft_margin))
20981 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20982 if (EQ (prop, Qright_margin))
20983 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20984 if (EQ (prop, Qscroll_bar))
20985 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20986 }
20987
20988 prop = Fbuffer_local_value (prop, it->w->buffer);
20989 }
20990
20991 if (INTEGERP (prop) || FLOATP (prop))
20992 {
20993 int base_unit = (width_p
20994 ? FRAME_COLUMN_WIDTH (it->f)
20995 : FRAME_LINE_HEIGHT (it->f));
20996 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20997 }
20998
20999 if (CONSP (prop))
21000 {
21001 Lisp_Object car = XCAR (prop);
21002 Lisp_Object cdr = XCDR (prop);
21003
21004 if (SYMBOLP (car))
21005 {
21006 #ifdef HAVE_WINDOW_SYSTEM
21007 if (FRAME_WINDOW_P (it->f)
21008 && valid_image_p (prop))
21009 {
21010 int id = lookup_image (it->f, prop);
21011 struct image *img = IMAGE_FROM_ID (it->f, id);
21012
21013 return OK_PIXELS (width_p ? img->width : img->height);
21014 }
21015 #endif
21016 if (EQ (car, Qplus) || EQ (car, Qminus))
21017 {
21018 int first = 1;
21019 double px;
21020
21021 pixels = 0;
21022 while (CONSP (cdr))
21023 {
21024 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21025 font, width_p, align_to))
21026 return 0;
21027 if (first)
21028 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21029 else
21030 pixels += px;
21031 cdr = XCDR (cdr);
21032 }
21033 if (EQ (car, Qminus))
21034 pixels = -pixels;
21035 return OK_PIXELS (pixels);
21036 }
21037
21038 car = Fbuffer_local_value (car, it->w->buffer);
21039 }
21040
21041 if (INTEGERP (car) || FLOATP (car))
21042 {
21043 double fact;
21044 pixels = XFLOATINT (car);
21045 if (NILP (cdr))
21046 return OK_PIXELS (pixels);
21047 if (calc_pixel_width_or_height (&fact, it, cdr,
21048 font, width_p, align_to))
21049 return OK_PIXELS (pixels * fact);
21050 return 0;
21051 }
21052
21053 return 0;
21054 }
21055
21056 return 0;
21057 }
21058
21059 \f
21060 /***********************************************************************
21061 Glyph Display
21062 ***********************************************************************/
21063
21064 #ifdef HAVE_WINDOW_SYSTEM
21065
21066 #if GLYPH_DEBUG
21067
21068 void
21069 dump_glyph_string (s)
21070 struct glyph_string *s;
21071 {
21072 fprintf (stderr, "glyph string\n");
21073 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21074 s->x, s->y, s->width, s->height);
21075 fprintf (stderr, " ybase = %d\n", s->ybase);
21076 fprintf (stderr, " hl = %d\n", s->hl);
21077 fprintf (stderr, " left overhang = %d, right = %d\n",
21078 s->left_overhang, s->right_overhang);
21079 fprintf (stderr, " nchars = %d\n", s->nchars);
21080 fprintf (stderr, " extends to end of line = %d\n",
21081 s->extends_to_end_of_line_p);
21082 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21083 fprintf (stderr, " bg width = %d\n", s->background_width);
21084 }
21085
21086 #endif /* GLYPH_DEBUG */
21087
21088 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21089 of XChar2b structures for S; it can't be allocated in
21090 init_glyph_string because it must be allocated via `alloca'. W
21091 is the window on which S is drawn. ROW and AREA are the glyph row
21092 and area within the row from which S is constructed. START is the
21093 index of the first glyph structure covered by S. HL is a
21094 face-override for drawing S. */
21095
21096 #ifdef HAVE_NTGUI
21097 #define OPTIONAL_HDC(hdc) HDC hdc,
21098 #define DECLARE_HDC(hdc) HDC hdc;
21099 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21100 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21101 #endif
21102
21103 #ifndef OPTIONAL_HDC
21104 #define OPTIONAL_HDC(hdc)
21105 #define DECLARE_HDC(hdc)
21106 #define ALLOCATE_HDC(hdc, f)
21107 #define RELEASE_HDC(hdc, f)
21108 #endif
21109
21110 static void
21111 init_glyph_string (struct glyph_string *s,
21112 OPTIONAL_HDC (hdc)
21113 XChar2b *char2b, struct window *w, struct glyph_row *row,
21114 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21115 {
21116 memset (s, 0, sizeof *s);
21117 s->w = w;
21118 s->f = XFRAME (w->frame);
21119 #ifdef HAVE_NTGUI
21120 s->hdc = hdc;
21121 #endif
21122 s->display = FRAME_X_DISPLAY (s->f);
21123 s->window = FRAME_X_WINDOW (s->f);
21124 s->char2b = char2b;
21125 s->hl = hl;
21126 s->row = row;
21127 s->area = area;
21128 s->first_glyph = row->glyphs[area] + start;
21129 s->height = row->height;
21130 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21131 s->ybase = s->y + row->ascent;
21132 }
21133
21134
21135 /* Append the list of glyph strings with head H and tail T to the list
21136 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21137
21138 static INLINE void
21139 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21140 struct glyph_string *h, struct glyph_string *t)
21141 {
21142 if (h)
21143 {
21144 if (*head)
21145 (*tail)->next = h;
21146 else
21147 *head = h;
21148 h->prev = *tail;
21149 *tail = t;
21150 }
21151 }
21152
21153
21154 /* Prepend the list of glyph strings with head H and tail T to the
21155 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21156 result. */
21157
21158 static INLINE void
21159 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21160 struct glyph_string *h, struct glyph_string *t)
21161 {
21162 if (h)
21163 {
21164 if (*head)
21165 (*head)->prev = t;
21166 else
21167 *tail = t;
21168 t->next = *head;
21169 *head = h;
21170 }
21171 }
21172
21173
21174 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21175 Set *HEAD and *TAIL to the resulting list. */
21176
21177 static INLINE void
21178 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21179 struct glyph_string *s)
21180 {
21181 s->next = s->prev = NULL;
21182 append_glyph_string_lists (head, tail, s, s);
21183 }
21184
21185
21186 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21187 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21188 make sure that X resources for the face returned are allocated.
21189 Value is a pointer to a realized face that is ready for display if
21190 DISPLAY_P is non-zero. */
21191
21192 static INLINE struct face *
21193 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21194 XChar2b *char2b, int display_p)
21195 {
21196 struct face *face = FACE_FROM_ID (f, face_id);
21197
21198 if (face->font)
21199 {
21200 unsigned code = face->font->driver->encode_char (face->font, c);
21201
21202 if (code != FONT_INVALID_CODE)
21203 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21204 else
21205 STORE_XCHAR2B (char2b, 0, 0);
21206 }
21207
21208 /* Make sure X resources of the face are allocated. */
21209 #ifdef HAVE_X_WINDOWS
21210 if (display_p)
21211 #endif
21212 {
21213 xassert (face != NULL);
21214 PREPARE_FACE_FOR_DISPLAY (f, face);
21215 }
21216
21217 return face;
21218 }
21219
21220
21221 /* Get face and two-byte form of character glyph GLYPH on frame F.
21222 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21223 a pointer to a realized face that is ready for display. */
21224
21225 static INLINE struct face *
21226 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21227 XChar2b *char2b, int *two_byte_p)
21228 {
21229 struct face *face;
21230
21231 xassert (glyph->type == CHAR_GLYPH);
21232 face = FACE_FROM_ID (f, glyph->face_id);
21233
21234 if (two_byte_p)
21235 *two_byte_p = 0;
21236
21237 if (face->font)
21238 {
21239 unsigned code;
21240
21241 if (CHAR_BYTE8_P (glyph->u.ch))
21242 code = CHAR_TO_BYTE8 (glyph->u.ch);
21243 else
21244 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21245
21246 if (code != FONT_INVALID_CODE)
21247 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21248 else
21249 STORE_XCHAR2B (char2b, 0, 0);
21250 }
21251
21252 /* Make sure X resources of the face are allocated. */
21253 xassert (face != NULL);
21254 PREPARE_FACE_FOR_DISPLAY (f, face);
21255 return face;
21256 }
21257
21258
21259 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21260 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21261
21262 static INLINE int
21263 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21264 {
21265 unsigned code;
21266
21267 if (CHAR_BYTE8_P (c))
21268 code = CHAR_TO_BYTE8 (c);
21269 else
21270 code = font->driver->encode_char (font, c);
21271
21272 if (code == FONT_INVALID_CODE)
21273 return 0;
21274 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21275 return 1;
21276 }
21277
21278
21279 /* Fill glyph string S with composition components specified by S->cmp.
21280
21281 BASE_FACE is the base face of the composition.
21282 S->cmp_from is the index of the first component for S.
21283
21284 OVERLAPS non-zero means S should draw the foreground only, and use
21285 its physical height for clipping. See also draw_glyphs.
21286
21287 Value is the index of a component not in S. */
21288
21289 static int
21290 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21291 int overlaps)
21292 {
21293 int i;
21294 /* For all glyphs of this composition, starting at the offset
21295 S->cmp_from, until we reach the end of the definition or encounter a
21296 glyph that requires the different face, add it to S. */
21297 struct face *face;
21298
21299 xassert (s);
21300
21301 s->for_overlaps = overlaps;
21302 s->face = NULL;
21303 s->font = NULL;
21304 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21305 {
21306 int c = COMPOSITION_GLYPH (s->cmp, i);
21307
21308 if (c != '\t')
21309 {
21310 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21311 -1, Qnil);
21312
21313 face = get_char_face_and_encoding (s->f, c, face_id,
21314 s->char2b + i, 1);
21315 if (face)
21316 {
21317 if (! s->face)
21318 {
21319 s->face = face;
21320 s->font = s->face->font;
21321 }
21322 else if (s->face != face)
21323 break;
21324 }
21325 }
21326 ++s->nchars;
21327 }
21328 s->cmp_to = i;
21329
21330 /* All glyph strings for the same composition has the same width,
21331 i.e. the width set for the first component of the composition. */
21332 s->width = s->first_glyph->pixel_width;
21333
21334 /* If the specified font could not be loaded, use the frame's
21335 default font, but record the fact that we couldn't load it in
21336 the glyph string so that we can draw rectangles for the
21337 characters of the glyph string. */
21338 if (s->font == NULL)
21339 {
21340 s->font_not_found_p = 1;
21341 s->font = FRAME_FONT (s->f);
21342 }
21343
21344 /* Adjust base line for subscript/superscript text. */
21345 s->ybase += s->first_glyph->voffset;
21346
21347 /* This glyph string must always be drawn with 16-bit functions. */
21348 s->two_byte_p = 1;
21349
21350 return s->cmp_to;
21351 }
21352
21353 static int
21354 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21355 int start, int end, int overlaps)
21356 {
21357 struct glyph *glyph, *last;
21358 Lisp_Object lgstring;
21359 int i;
21360
21361 s->for_overlaps = overlaps;
21362 glyph = s->row->glyphs[s->area] + start;
21363 last = s->row->glyphs[s->area] + end;
21364 s->cmp_id = glyph->u.cmp.id;
21365 s->cmp_from = glyph->slice.cmp.from;
21366 s->cmp_to = glyph->slice.cmp.to + 1;
21367 s->face = FACE_FROM_ID (s->f, face_id);
21368 lgstring = composition_gstring_from_id (s->cmp_id);
21369 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21370 glyph++;
21371 while (glyph < last
21372 && glyph->u.cmp.automatic
21373 && glyph->u.cmp.id == s->cmp_id
21374 && s->cmp_to == glyph->slice.cmp.from)
21375 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21376
21377 for (i = s->cmp_from; i < s->cmp_to; i++)
21378 {
21379 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21380 unsigned code = LGLYPH_CODE (lglyph);
21381
21382 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21383 }
21384 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21385 return glyph - s->row->glyphs[s->area];
21386 }
21387
21388
21389 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21390 See the comment of fill_glyph_string for arguments.
21391 Value is the index of the first glyph not in S. */
21392
21393
21394 static int
21395 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21396 int start, int end, int overlaps)
21397 {
21398 struct glyph *glyph, *last;
21399 int voffset;
21400
21401 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21402 s->for_overlaps = overlaps;
21403 glyph = s->row->glyphs[s->area] + start;
21404 last = s->row->glyphs[s->area] + end;
21405 voffset = glyph->voffset;
21406 s->face = FACE_FROM_ID (s->f, face_id);
21407 s->font = s->face->font;
21408 s->nchars = 1;
21409 s->width = glyph->pixel_width;
21410 glyph++;
21411 while (glyph < last
21412 && glyph->type == GLYPHLESS_GLYPH
21413 && glyph->voffset == voffset
21414 && glyph->face_id == face_id)
21415 {
21416 s->nchars++;
21417 s->width += glyph->pixel_width;
21418 glyph++;
21419 }
21420 s->ybase += voffset;
21421 return glyph - s->row->glyphs[s->area];
21422 }
21423
21424
21425 /* Fill glyph string S from a sequence of character glyphs.
21426
21427 FACE_ID is the face id of the string. START is the index of the
21428 first glyph to consider, END is the index of the last + 1.
21429 OVERLAPS non-zero means S should draw the foreground only, and use
21430 its physical height for clipping. See also draw_glyphs.
21431
21432 Value is the index of the first glyph not in S. */
21433
21434 static int
21435 fill_glyph_string (struct glyph_string *s, int face_id,
21436 int start, int end, int overlaps)
21437 {
21438 struct glyph *glyph, *last;
21439 int voffset;
21440 int glyph_not_available_p;
21441
21442 xassert (s->f == XFRAME (s->w->frame));
21443 xassert (s->nchars == 0);
21444 xassert (start >= 0 && end > start);
21445
21446 s->for_overlaps = overlaps;
21447 glyph = s->row->glyphs[s->area] + start;
21448 last = s->row->glyphs[s->area] + end;
21449 voffset = glyph->voffset;
21450 s->padding_p = glyph->padding_p;
21451 glyph_not_available_p = glyph->glyph_not_available_p;
21452
21453 while (glyph < last
21454 && glyph->type == CHAR_GLYPH
21455 && glyph->voffset == voffset
21456 /* Same face id implies same font, nowadays. */
21457 && glyph->face_id == face_id
21458 && glyph->glyph_not_available_p == glyph_not_available_p)
21459 {
21460 int two_byte_p;
21461
21462 s->face = get_glyph_face_and_encoding (s->f, glyph,
21463 s->char2b + s->nchars,
21464 &two_byte_p);
21465 s->two_byte_p = two_byte_p;
21466 ++s->nchars;
21467 xassert (s->nchars <= end - start);
21468 s->width += glyph->pixel_width;
21469 if (glyph++->padding_p != s->padding_p)
21470 break;
21471 }
21472
21473 s->font = s->face->font;
21474
21475 /* If the specified font could not be loaded, use the frame's font,
21476 but record the fact that we couldn't load it in
21477 S->font_not_found_p so that we can draw rectangles for the
21478 characters of the glyph string. */
21479 if (s->font == NULL || glyph_not_available_p)
21480 {
21481 s->font_not_found_p = 1;
21482 s->font = FRAME_FONT (s->f);
21483 }
21484
21485 /* Adjust base line for subscript/superscript text. */
21486 s->ybase += voffset;
21487
21488 xassert (s->face && s->face->gc);
21489 return glyph - s->row->glyphs[s->area];
21490 }
21491
21492
21493 /* Fill glyph string S from image glyph S->first_glyph. */
21494
21495 static void
21496 fill_image_glyph_string (struct glyph_string *s)
21497 {
21498 xassert (s->first_glyph->type == IMAGE_GLYPH);
21499 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21500 xassert (s->img);
21501 s->slice = s->first_glyph->slice.img;
21502 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21503 s->font = s->face->font;
21504 s->width = s->first_glyph->pixel_width;
21505
21506 /* Adjust base line for subscript/superscript text. */
21507 s->ybase += s->first_glyph->voffset;
21508 }
21509
21510
21511 /* Fill glyph string S from a sequence of stretch glyphs.
21512
21513 START is the index of the first glyph to consider,
21514 END is the index of the last + 1.
21515
21516 Value is the index of the first glyph not in S. */
21517
21518 static int
21519 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21520 {
21521 struct glyph *glyph, *last;
21522 int voffset, face_id;
21523
21524 xassert (s->first_glyph->type == STRETCH_GLYPH);
21525
21526 glyph = s->row->glyphs[s->area] + start;
21527 last = s->row->glyphs[s->area] + end;
21528 face_id = glyph->face_id;
21529 s->face = FACE_FROM_ID (s->f, face_id);
21530 s->font = s->face->font;
21531 s->width = glyph->pixel_width;
21532 s->nchars = 1;
21533 voffset = glyph->voffset;
21534
21535 for (++glyph;
21536 (glyph < last
21537 && glyph->type == STRETCH_GLYPH
21538 && glyph->voffset == voffset
21539 && glyph->face_id == face_id);
21540 ++glyph)
21541 s->width += glyph->pixel_width;
21542
21543 /* Adjust base line for subscript/superscript text. */
21544 s->ybase += voffset;
21545
21546 /* The case that face->gc == 0 is handled when drawing the glyph
21547 string by calling PREPARE_FACE_FOR_DISPLAY. */
21548 xassert (s->face);
21549 return glyph - s->row->glyphs[s->area];
21550 }
21551
21552 static struct font_metrics *
21553 get_per_char_metric (struct font *font, XChar2b *char2b)
21554 {
21555 static struct font_metrics metrics;
21556 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21557
21558 if (! font || code == FONT_INVALID_CODE)
21559 return NULL;
21560 font->driver->text_extents (font, &code, 1, &metrics);
21561 return &metrics;
21562 }
21563
21564 /* EXPORT for RIF:
21565 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21566 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21567 assumed to be zero. */
21568
21569 void
21570 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21571 {
21572 *left = *right = 0;
21573
21574 if (glyph->type == CHAR_GLYPH)
21575 {
21576 struct face *face;
21577 XChar2b char2b;
21578 struct font_metrics *pcm;
21579
21580 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21581 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21582 {
21583 if (pcm->rbearing > pcm->width)
21584 *right = pcm->rbearing - pcm->width;
21585 if (pcm->lbearing < 0)
21586 *left = -pcm->lbearing;
21587 }
21588 }
21589 else if (glyph->type == COMPOSITE_GLYPH)
21590 {
21591 if (! glyph->u.cmp.automatic)
21592 {
21593 struct composition *cmp = composition_table[glyph->u.cmp.id];
21594
21595 if (cmp->rbearing > cmp->pixel_width)
21596 *right = cmp->rbearing - cmp->pixel_width;
21597 if (cmp->lbearing < 0)
21598 *left = - cmp->lbearing;
21599 }
21600 else
21601 {
21602 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21603 struct font_metrics metrics;
21604
21605 composition_gstring_width (gstring, glyph->slice.cmp.from,
21606 glyph->slice.cmp.to + 1, &metrics);
21607 if (metrics.rbearing > metrics.width)
21608 *right = metrics.rbearing - metrics.width;
21609 if (metrics.lbearing < 0)
21610 *left = - metrics.lbearing;
21611 }
21612 }
21613 }
21614
21615
21616 /* Return the index of the first glyph preceding glyph string S that
21617 is overwritten by S because of S's left overhang. Value is -1
21618 if no glyphs are overwritten. */
21619
21620 static int
21621 left_overwritten (struct glyph_string *s)
21622 {
21623 int k;
21624
21625 if (s->left_overhang)
21626 {
21627 int x = 0, i;
21628 struct glyph *glyphs = s->row->glyphs[s->area];
21629 int first = s->first_glyph - glyphs;
21630
21631 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21632 x -= glyphs[i].pixel_width;
21633
21634 k = i + 1;
21635 }
21636 else
21637 k = -1;
21638
21639 return k;
21640 }
21641
21642
21643 /* Return the index of the first glyph preceding glyph string S that
21644 is overwriting S because of its right overhang. Value is -1 if no
21645 glyph in front of S overwrites S. */
21646
21647 static int
21648 left_overwriting (struct glyph_string *s)
21649 {
21650 int i, k, x;
21651 struct glyph *glyphs = s->row->glyphs[s->area];
21652 int first = s->first_glyph - glyphs;
21653
21654 k = -1;
21655 x = 0;
21656 for (i = first - 1; i >= 0; --i)
21657 {
21658 int left, right;
21659 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21660 if (x + right > 0)
21661 k = i;
21662 x -= glyphs[i].pixel_width;
21663 }
21664
21665 return k;
21666 }
21667
21668
21669 /* Return the index of the last glyph following glyph string S that is
21670 overwritten by S because of S's right overhang. Value is -1 if
21671 no such glyph is found. */
21672
21673 static int
21674 right_overwritten (struct glyph_string *s)
21675 {
21676 int k = -1;
21677
21678 if (s->right_overhang)
21679 {
21680 int x = 0, i;
21681 struct glyph *glyphs = s->row->glyphs[s->area];
21682 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21683 int end = s->row->used[s->area];
21684
21685 for (i = first; i < end && s->right_overhang > x; ++i)
21686 x += glyphs[i].pixel_width;
21687
21688 k = i;
21689 }
21690
21691 return k;
21692 }
21693
21694
21695 /* Return the index of the last glyph following glyph string S that
21696 overwrites S because of its left overhang. Value is negative
21697 if no such glyph is found. */
21698
21699 static int
21700 right_overwriting (struct glyph_string *s)
21701 {
21702 int i, k, x;
21703 int end = s->row->used[s->area];
21704 struct glyph *glyphs = s->row->glyphs[s->area];
21705 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21706
21707 k = -1;
21708 x = 0;
21709 for (i = first; i < end; ++i)
21710 {
21711 int left, right;
21712 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21713 if (x - left < 0)
21714 k = i;
21715 x += glyphs[i].pixel_width;
21716 }
21717
21718 return k;
21719 }
21720
21721
21722 /* Set background width of glyph string S. START is the index of the
21723 first glyph following S. LAST_X is the right-most x-position + 1
21724 in the drawing area. */
21725
21726 static INLINE void
21727 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21728 {
21729 /* If the face of this glyph string has to be drawn to the end of
21730 the drawing area, set S->extends_to_end_of_line_p. */
21731
21732 if (start == s->row->used[s->area]
21733 && s->area == TEXT_AREA
21734 && ((s->row->fill_line_p
21735 && (s->hl == DRAW_NORMAL_TEXT
21736 || s->hl == DRAW_IMAGE_RAISED
21737 || s->hl == DRAW_IMAGE_SUNKEN))
21738 || s->hl == DRAW_MOUSE_FACE))
21739 s->extends_to_end_of_line_p = 1;
21740
21741 /* If S extends its face to the end of the line, set its
21742 background_width to the distance to the right edge of the drawing
21743 area. */
21744 if (s->extends_to_end_of_line_p)
21745 s->background_width = last_x - s->x + 1;
21746 else
21747 s->background_width = s->width;
21748 }
21749
21750
21751 /* Compute overhangs and x-positions for glyph string S and its
21752 predecessors, or successors. X is the starting x-position for S.
21753 BACKWARD_P non-zero means process predecessors. */
21754
21755 static void
21756 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21757 {
21758 if (backward_p)
21759 {
21760 while (s)
21761 {
21762 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21763 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21764 x -= s->width;
21765 s->x = x;
21766 s = s->prev;
21767 }
21768 }
21769 else
21770 {
21771 while (s)
21772 {
21773 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21774 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21775 s->x = x;
21776 x += s->width;
21777 s = s->next;
21778 }
21779 }
21780 }
21781
21782
21783
21784 /* The following macros are only called from draw_glyphs below.
21785 They reference the following parameters of that function directly:
21786 `w', `row', `area', and `overlap_p'
21787 as well as the following local variables:
21788 `s', `f', and `hdc' (in W32) */
21789
21790 #ifdef HAVE_NTGUI
21791 /* On W32, silently add local `hdc' variable to argument list of
21792 init_glyph_string. */
21793 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21794 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21795 #else
21796 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21797 init_glyph_string (s, char2b, w, row, area, start, hl)
21798 #endif
21799
21800 /* Add a glyph string for a stretch glyph to the list of strings
21801 between HEAD and TAIL. START is the index of the stretch 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 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21809 and below -- keep them on one line. */
21810 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21811 do \
21812 { \
21813 s = (struct glyph_string *) alloca (sizeof *s); \
21814 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21815 START = fill_stretch_glyph_string (s, START, END); \
21816 append_glyph_string (&HEAD, &TAIL, s); \
21817 s->x = (X); \
21818 } \
21819 while (0)
21820
21821
21822 /* Add a glyph string for an image glyph to the list of strings
21823 between HEAD and TAIL. START is the index of the image glyph in
21824 row area AREA of glyph row ROW. END is the index of the last glyph
21825 in that glyph row area. X is the current output position assigned
21826 to the new glyph string constructed. HL overrides that face of the
21827 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21828 is the right-most x-position of the drawing area. */
21829
21830 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21831 do \
21832 { \
21833 s = (struct glyph_string *) alloca (sizeof *s); \
21834 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21835 fill_image_glyph_string (s); \
21836 append_glyph_string (&HEAD, &TAIL, s); \
21837 ++START; \
21838 s->x = (X); \
21839 } \
21840 while (0)
21841
21842
21843 /* Add a glyph string for a sequence of character glyphs to the list
21844 of strings between HEAD and TAIL. START is the index of the first
21845 glyph in row area AREA of glyph row ROW that is part of the new
21846 glyph string. END is the index of the last glyph in that glyph row
21847 area. X is the current output position assigned to the new glyph
21848 string constructed. HL overrides that face of the glyph; e.g. it
21849 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21850 right-most x-position of the drawing area. */
21851
21852 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21853 do \
21854 { \
21855 int face_id; \
21856 XChar2b *char2b; \
21857 \
21858 face_id = (row)->glyphs[area][START].face_id; \
21859 \
21860 s = (struct glyph_string *) alloca (sizeof *s); \
21861 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21862 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21863 append_glyph_string (&HEAD, &TAIL, s); \
21864 s->x = (X); \
21865 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21866 } \
21867 while (0)
21868
21869
21870 /* Add a glyph string for a composite sequence to the list of strings
21871 between HEAD and TAIL. START is the index of the first glyph in
21872 row area AREA of glyph row ROW that is part of the new glyph
21873 string. END is the index of the last glyph in that glyph row area.
21874 X is the current output position assigned to the new glyph string
21875 constructed. HL overrides that face of the glyph; e.g. it is
21876 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21877 x-position of the drawing area. */
21878
21879 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21880 do { \
21881 int face_id = (row)->glyphs[area][START].face_id; \
21882 struct face *base_face = FACE_FROM_ID (f, face_id); \
21883 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21884 struct composition *cmp = composition_table[cmp_id]; \
21885 XChar2b *char2b; \
21886 struct glyph_string *first_s IF_LINT (= NULL); \
21887 int n; \
21888 \
21889 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21890 \
21891 /* Make glyph_strings for each glyph sequence that is drawable by \
21892 the same face, and append them to HEAD/TAIL. */ \
21893 for (n = 0; n < cmp->glyph_len;) \
21894 { \
21895 s = (struct glyph_string *) alloca (sizeof *s); \
21896 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21897 append_glyph_string (&(HEAD), &(TAIL), s); \
21898 s->cmp = cmp; \
21899 s->cmp_from = n; \
21900 s->x = (X); \
21901 if (n == 0) \
21902 first_s = s; \
21903 n = fill_composite_glyph_string (s, base_face, overlaps); \
21904 } \
21905 \
21906 ++START; \
21907 s = first_s; \
21908 } while (0)
21909
21910
21911 /* Add a glyph string for a glyph-string sequence to the list of strings
21912 between HEAD and TAIL. */
21913
21914 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21915 do { \
21916 int face_id; \
21917 XChar2b *char2b; \
21918 Lisp_Object gstring; \
21919 \
21920 face_id = (row)->glyphs[area][START].face_id; \
21921 gstring = (composition_gstring_from_id \
21922 ((row)->glyphs[area][START].u.cmp.id)); \
21923 s = (struct glyph_string *) alloca (sizeof *s); \
21924 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21925 * LGSTRING_GLYPH_LEN (gstring)); \
21926 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21927 append_glyph_string (&(HEAD), &(TAIL), s); \
21928 s->x = (X); \
21929 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21930 } while (0)
21931
21932
21933 /* Add a glyph string for a sequence of glyphless character's glyphs
21934 to the list of strings between HEAD and TAIL. The meanings of
21935 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21936
21937 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21938 do \
21939 { \
21940 int face_id; \
21941 \
21942 face_id = (row)->glyphs[area][START].face_id; \
21943 \
21944 s = (struct glyph_string *) alloca (sizeof *s); \
21945 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21946 append_glyph_string (&HEAD, &TAIL, s); \
21947 s->x = (X); \
21948 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21949 overlaps); \
21950 } \
21951 while (0)
21952
21953
21954 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21955 of AREA of glyph row ROW on window W between indices START and END.
21956 HL overrides the face for drawing glyph strings, e.g. it is
21957 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21958 x-positions of the drawing area.
21959
21960 This is an ugly monster macro construct because we must use alloca
21961 to allocate glyph strings (because draw_glyphs can be called
21962 asynchronously). */
21963
21964 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21965 do \
21966 { \
21967 HEAD = TAIL = NULL; \
21968 while (START < END) \
21969 { \
21970 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21971 switch (first_glyph->type) \
21972 { \
21973 case CHAR_GLYPH: \
21974 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21975 HL, X, LAST_X); \
21976 break; \
21977 \
21978 case COMPOSITE_GLYPH: \
21979 if (first_glyph->u.cmp.automatic) \
21980 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21981 HL, X, LAST_X); \
21982 else \
21983 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21984 HL, X, LAST_X); \
21985 break; \
21986 \
21987 case STRETCH_GLYPH: \
21988 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21989 HL, X, LAST_X); \
21990 break; \
21991 \
21992 case IMAGE_GLYPH: \
21993 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21994 HL, X, LAST_X); \
21995 break; \
21996 \
21997 case GLYPHLESS_GLYPH: \
21998 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21999 HL, X, LAST_X); \
22000 break; \
22001 \
22002 default: \
22003 abort (); \
22004 } \
22005 \
22006 if (s) \
22007 { \
22008 set_glyph_string_background_width (s, START, LAST_X); \
22009 (X) += s->width; \
22010 } \
22011 } \
22012 } while (0)
22013
22014
22015 /* Draw glyphs between START and END in AREA of ROW on window W,
22016 starting at x-position X. X is relative to AREA in W. HL is a
22017 face-override with the following meaning:
22018
22019 DRAW_NORMAL_TEXT draw normally
22020 DRAW_CURSOR draw in cursor face
22021 DRAW_MOUSE_FACE draw in mouse face.
22022 DRAW_INVERSE_VIDEO draw in mode line face
22023 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22024 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22025
22026 If OVERLAPS is non-zero, draw only the foreground of characters and
22027 clip to the physical height of ROW. Non-zero value also defines
22028 the overlapping part to be drawn:
22029
22030 OVERLAPS_PRED overlap with preceding rows
22031 OVERLAPS_SUCC overlap with succeeding rows
22032 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22033 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22034
22035 Value is the x-position reached, relative to AREA of W. */
22036
22037 static int
22038 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22039 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22040 enum draw_glyphs_face hl, int overlaps)
22041 {
22042 struct glyph_string *head, *tail;
22043 struct glyph_string *s;
22044 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22045 int i, j, x_reached, last_x, area_left = 0;
22046 struct frame *f = XFRAME (WINDOW_FRAME (w));
22047 DECLARE_HDC (hdc);
22048
22049 ALLOCATE_HDC (hdc, f);
22050
22051 /* Let's rather be paranoid than getting a SEGV. */
22052 end = min (end, row->used[area]);
22053 start = max (0, start);
22054 start = min (end, start);
22055
22056 /* Translate X to frame coordinates. Set last_x to the right
22057 end of the drawing area. */
22058 if (row->full_width_p)
22059 {
22060 /* X is relative to the left edge of W, without scroll bars
22061 or fringes. */
22062 area_left = WINDOW_LEFT_EDGE_X (w);
22063 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22064 }
22065 else
22066 {
22067 area_left = window_box_left (w, area);
22068 last_x = area_left + window_box_width (w, area);
22069 }
22070 x += area_left;
22071
22072 /* Build a doubly-linked list of glyph_string structures between
22073 head and tail from what we have to draw. Note that the macro
22074 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22075 the reason we use a separate variable `i'. */
22076 i = start;
22077 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22078 if (tail)
22079 x_reached = tail->x + tail->background_width;
22080 else
22081 x_reached = x;
22082
22083 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22084 the row, redraw some glyphs in front or following the glyph
22085 strings built above. */
22086 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22087 {
22088 struct glyph_string *h, *t;
22089 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22090 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22091 int check_mouse_face = 0;
22092 int dummy_x = 0;
22093
22094 /* If mouse highlighting is on, we may need to draw adjacent
22095 glyphs using mouse-face highlighting. */
22096 if (area == TEXT_AREA && row->mouse_face_p)
22097 {
22098 struct glyph_row *mouse_beg_row, *mouse_end_row;
22099
22100 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22101 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22102
22103 if (row >= mouse_beg_row && row <= mouse_end_row)
22104 {
22105 check_mouse_face = 1;
22106 mouse_beg_col = (row == mouse_beg_row)
22107 ? hlinfo->mouse_face_beg_col : 0;
22108 mouse_end_col = (row == mouse_end_row)
22109 ? hlinfo->mouse_face_end_col
22110 : row->used[TEXT_AREA];
22111 }
22112 }
22113
22114 /* Compute overhangs for all glyph strings. */
22115 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22116 for (s = head; s; s = s->next)
22117 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22118
22119 /* Prepend glyph strings for glyphs in front of the first glyph
22120 string that are overwritten because of the first glyph
22121 string's left overhang. The background of all strings
22122 prepended must be drawn because the first glyph string
22123 draws over it. */
22124 i = left_overwritten (head);
22125 if (i >= 0)
22126 {
22127 enum draw_glyphs_face overlap_hl;
22128
22129 /* If this row contains mouse highlighting, attempt to draw
22130 the overlapped glyphs with the correct highlight. This
22131 code fails if the overlap encompasses more than one glyph
22132 and mouse-highlight spans only some of these glyphs.
22133 However, making it work perfectly involves a lot more
22134 code, and I don't know if the pathological case occurs in
22135 practice, so we'll stick to this for now. --- cyd */
22136 if (check_mouse_face
22137 && mouse_beg_col < start && mouse_end_col > i)
22138 overlap_hl = DRAW_MOUSE_FACE;
22139 else
22140 overlap_hl = DRAW_NORMAL_TEXT;
22141
22142 j = i;
22143 BUILD_GLYPH_STRINGS (j, start, h, t,
22144 overlap_hl, dummy_x, last_x);
22145 start = i;
22146 compute_overhangs_and_x (t, head->x, 1);
22147 prepend_glyph_string_lists (&head, &tail, h, t);
22148 clip_head = head;
22149 }
22150
22151 /* Prepend glyph strings for glyphs in front of the first glyph
22152 string that overwrite that glyph string because of their
22153 right overhang. For these strings, only the foreground must
22154 be drawn, because it draws over the glyph string at `head'.
22155 The background must not be drawn because this would overwrite
22156 right overhangs of preceding glyphs for which no glyph
22157 strings exist. */
22158 i = left_overwriting (head);
22159 if (i >= 0)
22160 {
22161 enum draw_glyphs_face overlap_hl;
22162
22163 if (check_mouse_face
22164 && mouse_beg_col < start && mouse_end_col > i)
22165 overlap_hl = DRAW_MOUSE_FACE;
22166 else
22167 overlap_hl = DRAW_NORMAL_TEXT;
22168
22169 clip_head = head;
22170 BUILD_GLYPH_STRINGS (i, start, h, t,
22171 overlap_hl, dummy_x, last_x);
22172 for (s = h; s; s = s->next)
22173 s->background_filled_p = 1;
22174 compute_overhangs_and_x (t, head->x, 1);
22175 prepend_glyph_string_lists (&head, &tail, h, t);
22176 }
22177
22178 /* Append glyphs strings for glyphs following the last glyph
22179 string tail that are overwritten by tail. The background of
22180 these strings has to be drawn because tail's foreground draws
22181 over it. */
22182 i = right_overwritten (tail);
22183 if (i >= 0)
22184 {
22185 enum draw_glyphs_face overlap_hl;
22186
22187 if (check_mouse_face
22188 && mouse_beg_col < i && mouse_end_col > end)
22189 overlap_hl = DRAW_MOUSE_FACE;
22190 else
22191 overlap_hl = DRAW_NORMAL_TEXT;
22192
22193 BUILD_GLYPH_STRINGS (end, i, h, t,
22194 overlap_hl, x, last_x);
22195 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22196 we don't have `end = i;' here. */
22197 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22198 append_glyph_string_lists (&head, &tail, h, t);
22199 clip_tail = tail;
22200 }
22201
22202 /* Append glyph strings for glyphs following the last glyph
22203 string tail that overwrite tail. The foreground of such
22204 glyphs has to be drawn because it writes into the background
22205 of tail. The background must not be drawn because it could
22206 paint over the foreground of following glyphs. */
22207 i = right_overwriting (tail);
22208 if (i >= 0)
22209 {
22210 enum draw_glyphs_face overlap_hl;
22211 if (check_mouse_face
22212 && mouse_beg_col < i && mouse_end_col > end)
22213 overlap_hl = DRAW_MOUSE_FACE;
22214 else
22215 overlap_hl = DRAW_NORMAL_TEXT;
22216
22217 clip_tail = tail;
22218 i++; /* We must include the Ith glyph. */
22219 BUILD_GLYPH_STRINGS (end, i, h, t,
22220 overlap_hl, x, last_x);
22221 for (s = h; s; s = s->next)
22222 s->background_filled_p = 1;
22223 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22224 append_glyph_string_lists (&head, &tail, h, t);
22225 }
22226 if (clip_head || clip_tail)
22227 for (s = head; s; s = s->next)
22228 {
22229 s->clip_head = clip_head;
22230 s->clip_tail = clip_tail;
22231 }
22232 }
22233
22234 /* Draw all strings. */
22235 for (s = head; s; s = s->next)
22236 FRAME_RIF (f)->draw_glyph_string (s);
22237
22238 #ifndef HAVE_NS
22239 /* When focus a sole frame and move horizontally, this sets on_p to 0
22240 causing a failure to erase prev cursor position. */
22241 if (area == TEXT_AREA
22242 && !row->full_width_p
22243 /* When drawing overlapping rows, only the glyph strings'
22244 foreground is drawn, which doesn't erase a cursor
22245 completely. */
22246 && !overlaps)
22247 {
22248 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22249 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22250 : (tail ? tail->x + tail->background_width : x));
22251 x0 -= area_left;
22252 x1 -= area_left;
22253
22254 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22255 row->y, MATRIX_ROW_BOTTOM_Y (row));
22256 }
22257 #endif
22258
22259 /* Value is the x-position up to which drawn, relative to AREA of W.
22260 This doesn't include parts drawn because of overhangs. */
22261 if (row->full_width_p)
22262 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22263 else
22264 x_reached -= area_left;
22265
22266 RELEASE_HDC (hdc, f);
22267
22268 return x_reached;
22269 }
22270
22271 /* Expand row matrix if too narrow. Don't expand if area
22272 is not present. */
22273
22274 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22275 { \
22276 if (!fonts_changed_p \
22277 && (it->glyph_row->glyphs[area] \
22278 < it->glyph_row->glyphs[area + 1])) \
22279 { \
22280 it->w->ncols_scale_factor++; \
22281 fonts_changed_p = 1; \
22282 } \
22283 }
22284
22285 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22286 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22287
22288 static INLINE void
22289 append_glyph (struct it *it)
22290 {
22291 struct glyph *glyph;
22292 enum glyph_row_area area = it->area;
22293
22294 xassert (it->glyph_row);
22295 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22296
22297 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22298 if (glyph < it->glyph_row->glyphs[area + 1])
22299 {
22300 /* If the glyph row is reversed, we need to prepend the glyph
22301 rather than append it. */
22302 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22303 {
22304 struct glyph *g;
22305
22306 /* Make room for the additional glyph. */
22307 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22308 g[1] = *g;
22309 glyph = it->glyph_row->glyphs[area];
22310 }
22311 glyph->charpos = CHARPOS (it->position);
22312 glyph->object = it->object;
22313 if (it->pixel_width > 0)
22314 {
22315 glyph->pixel_width = it->pixel_width;
22316 glyph->padding_p = 0;
22317 }
22318 else
22319 {
22320 /* Assure at least 1-pixel width. Otherwise, cursor can't
22321 be displayed correctly. */
22322 glyph->pixel_width = 1;
22323 glyph->padding_p = 1;
22324 }
22325 glyph->ascent = it->ascent;
22326 glyph->descent = it->descent;
22327 glyph->voffset = it->voffset;
22328 glyph->type = CHAR_GLYPH;
22329 glyph->avoid_cursor_p = it->avoid_cursor_p;
22330 glyph->multibyte_p = it->multibyte_p;
22331 glyph->left_box_line_p = it->start_of_box_run_p;
22332 glyph->right_box_line_p = it->end_of_box_run_p;
22333 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22334 || it->phys_descent > it->descent);
22335 glyph->glyph_not_available_p = it->glyph_not_available_p;
22336 glyph->face_id = it->face_id;
22337 glyph->u.ch = it->char_to_display;
22338 glyph->slice.img = null_glyph_slice;
22339 glyph->font_type = FONT_TYPE_UNKNOWN;
22340 if (it->bidi_p)
22341 {
22342 glyph->resolved_level = it->bidi_it.resolved_level;
22343 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22344 abort ();
22345 glyph->bidi_type = it->bidi_it.type;
22346 }
22347 else
22348 {
22349 glyph->resolved_level = 0;
22350 glyph->bidi_type = UNKNOWN_BT;
22351 }
22352 ++it->glyph_row->used[area];
22353 }
22354 else
22355 IT_EXPAND_MATRIX_WIDTH (it, area);
22356 }
22357
22358 /* Store one glyph for the composition IT->cmp_it.id in
22359 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22360 non-null. */
22361
22362 static INLINE void
22363 append_composite_glyph (struct it *it)
22364 {
22365 struct glyph *glyph;
22366 enum glyph_row_area area = it->area;
22367
22368 xassert (it->glyph_row);
22369
22370 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22371 if (glyph < it->glyph_row->glyphs[area + 1])
22372 {
22373 /* If the glyph row is reversed, we need to prepend the glyph
22374 rather than append it. */
22375 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22376 {
22377 struct glyph *g;
22378
22379 /* Make room for the new glyph. */
22380 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22381 g[1] = *g;
22382 glyph = it->glyph_row->glyphs[it->area];
22383 }
22384 glyph->charpos = it->cmp_it.charpos;
22385 glyph->object = it->object;
22386 glyph->pixel_width = it->pixel_width;
22387 glyph->ascent = it->ascent;
22388 glyph->descent = it->descent;
22389 glyph->voffset = it->voffset;
22390 glyph->type = COMPOSITE_GLYPH;
22391 if (it->cmp_it.ch < 0)
22392 {
22393 glyph->u.cmp.automatic = 0;
22394 glyph->u.cmp.id = it->cmp_it.id;
22395 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22396 }
22397 else
22398 {
22399 glyph->u.cmp.automatic = 1;
22400 glyph->u.cmp.id = it->cmp_it.id;
22401 glyph->slice.cmp.from = it->cmp_it.from;
22402 glyph->slice.cmp.to = it->cmp_it.to - 1;
22403 }
22404 glyph->avoid_cursor_p = it->avoid_cursor_p;
22405 glyph->multibyte_p = it->multibyte_p;
22406 glyph->left_box_line_p = it->start_of_box_run_p;
22407 glyph->right_box_line_p = it->end_of_box_run_p;
22408 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22409 || it->phys_descent > it->descent);
22410 glyph->padding_p = 0;
22411 glyph->glyph_not_available_p = 0;
22412 glyph->face_id = it->face_id;
22413 glyph->font_type = FONT_TYPE_UNKNOWN;
22414 if (it->bidi_p)
22415 {
22416 glyph->resolved_level = it->bidi_it.resolved_level;
22417 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22418 abort ();
22419 glyph->bidi_type = it->bidi_it.type;
22420 }
22421 ++it->glyph_row->used[area];
22422 }
22423 else
22424 IT_EXPAND_MATRIX_WIDTH (it, area);
22425 }
22426
22427
22428 /* Change IT->ascent and IT->height according to the setting of
22429 IT->voffset. */
22430
22431 static INLINE void
22432 take_vertical_position_into_account (struct it *it)
22433 {
22434 if (it->voffset)
22435 {
22436 if (it->voffset < 0)
22437 /* Increase the ascent so that we can display the text higher
22438 in the line. */
22439 it->ascent -= it->voffset;
22440 else
22441 /* Increase the descent so that we can display the text lower
22442 in the line. */
22443 it->descent += it->voffset;
22444 }
22445 }
22446
22447
22448 /* Produce glyphs/get display metrics for the image IT is loaded with.
22449 See the description of struct display_iterator in dispextern.h for
22450 an overview of struct display_iterator. */
22451
22452 static void
22453 produce_image_glyph (struct it *it)
22454 {
22455 struct image *img;
22456 struct face *face;
22457 int glyph_ascent, crop;
22458 struct glyph_slice slice;
22459
22460 xassert (it->what == IT_IMAGE);
22461
22462 face = FACE_FROM_ID (it->f, it->face_id);
22463 xassert (face);
22464 /* Make sure X resources of the face is loaded. */
22465 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22466
22467 if (it->image_id < 0)
22468 {
22469 /* Fringe bitmap. */
22470 it->ascent = it->phys_ascent = 0;
22471 it->descent = it->phys_descent = 0;
22472 it->pixel_width = 0;
22473 it->nglyphs = 0;
22474 return;
22475 }
22476
22477 img = IMAGE_FROM_ID (it->f, it->image_id);
22478 xassert (img);
22479 /* Make sure X resources of the image is loaded. */
22480 prepare_image_for_display (it->f, img);
22481
22482 slice.x = slice.y = 0;
22483 slice.width = img->width;
22484 slice.height = img->height;
22485
22486 if (INTEGERP (it->slice.x))
22487 slice.x = XINT (it->slice.x);
22488 else if (FLOATP (it->slice.x))
22489 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22490
22491 if (INTEGERP (it->slice.y))
22492 slice.y = XINT (it->slice.y);
22493 else if (FLOATP (it->slice.y))
22494 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22495
22496 if (INTEGERP (it->slice.width))
22497 slice.width = XINT (it->slice.width);
22498 else if (FLOATP (it->slice.width))
22499 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22500
22501 if (INTEGERP (it->slice.height))
22502 slice.height = XINT (it->slice.height);
22503 else if (FLOATP (it->slice.height))
22504 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22505
22506 if (slice.x >= img->width)
22507 slice.x = img->width;
22508 if (slice.y >= img->height)
22509 slice.y = img->height;
22510 if (slice.x + slice.width >= img->width)
22511 slice.width = img->width - slice.x;
22512 if (slice.y + slice.height > img->height)
22513 slice.height = img->height - slice.y;
22514
22515 if (slice.width == 0 || slice.height == 0)
22516 return;
22517
22518 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22519
22520 it->descent = slice.height - glyph_ascent;
22521 if (slice.y == 0)
22522 it->descent += img->vmargin;
22523 if (slice.y + slice.height == img->height)
22524 it->descent += img->vmargin;
22525 it->phys_descent = it->descent;
22526
22527 it->pixel_width = slice.width;
22528 if (slice.x == 0)
22529 it->pixel_width += img->hmargin;
22530 if (slice.x + slice.width == img->width)
22531 it->pixel_width += img->hmargin;
22532
22533 /* It's quite possible for images to have an ascent greater than
22534 their height, so don't get confused in that case. */
22535 if (it->descent < 0)
22536 it->descent = 0;
22537
22538 it->nglyphs = 1;
22539
22540 if (face->box != FACE_NO_BOX)
22541 {
22542 if (face->box_line_width > 0)
22543 {
22544 if (slice.y == 0)
22545 it->ascent += face->box_line_width;
22546 if (slice.y + slice.height == img->height)
22547 it->descent += face->box_line_width;
22548 }
22549
22550 if (it->start_of_box_run_p && slice.x == 0)
22551 it->pixel_width += eabs (face->box_line_width);
22552 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22553 it->pixel_width += eabs (face->box_line_width);
22554 }
22555
22556 take_vertical_position_into_account (it);
22557
22558 /* Automatically crop wide image glyphs at right edge so we can
22559 draw the cursor on same display row. */
22560 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22561 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22562 {
22563 it->pixel_width -= crop;
22564 slice.width -= crop;
22565 }
22566
22567 if (it->glyph_row)
22568 {
22569 struct glyph *glyph;
22570 enum glyph_row_area area = it->area;
22571
22572 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22573 if (glyph < it->glyph_row->glyphs[area + 1])
22574 {
22575 glyph->charpos = CHARPOS (it->position);
22576 glyph->object = it->object;
22577 glyph->pixel_width = it->pixel_width;
22578 glyph->ascent = glyph_ascent;
22579 glyph->descent = it->descent;
22580 glyph->voffset = it->voffset;
22581 glyph->type = IMAGE_GLYPH;
22582 glyph->avoid_cursor_p = it->avoid_cursor_p;
22583 glyph->multibyte_p = it->multibyte_p;
22584 glyph->left_box_line_p = it->start_of_box_run_p;
22585 glyph->right_box_line_p = it->end_of_box_run_p;
22586 glyph->overlaps_vertically_p = 0;
22587 glyph->padding_p = 0;
22588 glyph->glyph_not_available_p = 0;
22589 glyph->face_id = it->face_id;
22590 glyph->u.img_id = img->id;
22591 glyph->slice.img = slice;
22592 glyph->font_type = FONT_TYPE_UNKNOWN;
22593 if (it->bidi_p)
22594 {
22595 glyph->resolved_level = it->bidi_it.resolved_level;
22596 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22597 abort ();
22598 glyph->bidi_type = it->bidi_it.type;
22599 }
22600 ++it->glyph_row->used[area];
22601 }
22602 else
22603 IT_EXPAND_MATRIX_WIDTH (it, area);
22604 }
22605 }
22606
22607
22608 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22609 of the glyph, WIDTH and HEIGHT are the width and height of the
22610 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22611
22612 static void
22613 append_stretch_glyph (struct it *it, Lisp_Object object,
22614 int width, int height, int ascent)
22615 {
22616 struct glyph *glyph;
22617 enum glyph_row_area area = it->area;
22618
22619 xassert (ascent >= 0 && ascent <= height);
22620
22621 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22622 if (glyph < it->glyph_row->glyphs[area + 1])
22623 {
22624 /* If the glyph row is reversed, we need to prepend the glyph
22625 rather than append it. */
22626 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22627 {
22628 struct glyph *g;
22629
22630 /* Make room for the additional glyph. */
22631 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22632 g[1] = *g;
22633 glyph = it->glyph_row->glyphs[area];
22634 }
22635 glyph->charpos = CHARPOS (it->position);
22636 glyph->object = object;
22637 glyph->pixel_width = width;
22638 glyph->ascent = ascent;
22639 glyph->descent = height - ascent;
22640 glyph->voffset = it->voffset;
22641 glyph->type = STRETCH_GLYPH;
22642 glyph->avoid_cursor_p = it->avoid_cursor_p;
22643 glyph->multibyte_p = it->multibyte_p;
22644 glyph->left_box_line_p = it->start_of_box_run_p;
22645 glyph->right_box_line_p = it->end_of_box_run_p;
22646 glyph->overlaps_vertically_p = 0;
22647 glyph->padding_p = 0;
22648 glyph->glyph_not_available_p = 0;
22649 glyph->face_id = it->face_id;
22650 glyph->u.stretch.ascent = ascent;
22651 glyph->u.stretch.height = height;
22652 glyph->slice.img = null_glyph_slice;
22653 glyph->font_type = FONT_TYPE_UNKNOWN;
22654 if (it->bidi_p)
22655 {
22656 glyph->resolved_level = it->bidi_it.resolved_level;
22657 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22658 abort ();
22659 glyph->bidi_type = it->bidi_it.type;
22660 }
22661 else
22662 {
22663 glyph->resolved_level = 0;
22664 glyph->bidi_type = UNKNOWN_BT;
22665 }
22666 ++it->glyph_row->used[area];
22667 }
22668 else
22669 IT_EXPAND_MATRIX_WIDTH (it, area);
22670 }
22671
22672
22673 /* Produce a stretch glyph for iterator IT. IT->object is the value
22674 of the glyph property displayed. The value must be a list
22675 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22676 being recognized:
22677
22678 1. `:width WIDTH' specifies that the space should be WIDTH *
22679 canonical char width wide. WIDTH may be an integer or floating
22680 point number.
22681
22682 2. `:relative-width FACTOR' specifies that the width of the stretch
22683 should be computed from the width of the first character having the
22684 `glyph' property, and should be FACTOR times that width.
22685
22686 3. `:align-to HPOS' specifies that the space should be wide enough
22687 to reach HPOS, a value in canonical character units.
22688
22689 Exactly one of the above pairs must be present.
22690
22691 4. `:height HEIGHT' specifies that the height of the stretch produced
22692 should be HEIGHT, measured in canonical character units.
22693
22694 5. `:relative-height FACTOR' specifies that the height of the
22695 stretch should be FACTOR times the height of the characters having
22696 the glyph property.
22697
22698 Either none or exactly one of 4 or 5 must be present.
22699
22700 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22701 of the stretch should be used for the ascent of the stretch.
22702 ASCENT must be in the range 0 <= ASCENT <= 100. */
22703
22704 static void
22705 produce_stretch_glyph (struct it *it)
22706 {
22707 /* (space :width WIDTH :height HEIGHT ...) */
22708 Lisp_Object prop, plist;
22709 int width = 0, height = 0, align_to = -1;
22710 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22711 int ascent = 0;
22712 double tem;
22713 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22714 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22715
22716 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22717
22718 /* List should start with `space'. */
22719 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22720 plist = XCDR (it->object);
22721
22722 /* Compute the width of the stretch. */
22723 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22724 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22725 {
22726 /* Absolute width `:width WIDTH' specified and valid. */
22727 zero_width_ok_p = 1;
22728 width = (int)tem;
22729 }
22730 else if (prop = Fplist_get (plist, QCrelative_width),
22731 NUMVAL (prop) > 0)
22732 {
22733 /* Relative width `:relative-width FACTOR' specified and valid.
22734 Compute the width of the characters having the `glyph'
22735 property. */
22736 struct it it2;
22737 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22738
22739 it2 = *it;
22740 if (it->multibyte_p)
22741 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22742 else
22743 {
22744 it2.c = it2.char_to_display = *p, it2.len = 1;
22745 if (! ASCII_CHAR_P (it2.c))
22746 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22747 }
22748
22749 it2.glyph_row = NULL;
22750 it2.what = IT_CHARACTER;
22751 x_produce_glyphs (&it2);
22752 width = NUMVAL (prop) * it2.pixel_width;
22753 }
22754 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22755 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22756 {
22757 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22758 align_to = (align_to < 0
22759 ? 0
22760 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22761 else if (align_to < 0)
22762 align_to = window_box_left_offset (it->w, TEXT_AREA);
22763 width = max (0, (int)tem + align_to - it->current_x);
22764 zero_width_ok_p = 1;
22765 }
22766 else
22767 /* Nothing specified -> width defaults to canonical char width. */
22768 width = FRAME_COLUMN_WIDTH (it->f);
22769
22770 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22771 width = 1;
22772
22773 /* Compute height. */
22774 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22775 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22776 {
22777 height = (int)tem;
22778 zero_height_ok_p = 1;
22779 }
22780 else if (prop = Fplist_get (plist, QCrelative_height),
22781 NUMVAL (prop) > 0)
22782 height = FONT_HEIGHT (font) * NUMVAL (prop);
22783 else
22784 height = FONT_HEIGHT (font);
22785
22786 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22787 height = 1;
22788
22789 /* Compute percentage of height used for ascent. If
22790 `:ascent ASCENT' is present and valid, use that. Otherwise,
22791 derive the ascent from the font in use. */
22792 if (prop = Fplist_get (plist, QCascent),
22793 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22794 ascent = height * NUMVAL (prop) / 100.0;
22795 else if (!NILP (prop)
22796 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22797 ascent = min (max (0, (int)tem), height);
22798 else
22799 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22800
22801 if (width > 0 && it->line_wrap != TRUNCATE
22802 && it->current_x + width > it->last_visible_x)
22803 width = it->last_visible_x - it->current_x - 1;
22804
22805 if (width > 0 && height > 0 && it->glyph_row)
22806 {
22807 Lisp_Object object = it->stack[it->sp - 1].string;
22808 if (!STRINGP (object))
22809 object = it->w->buffer;
22810 append_stretch_glyph (it, object, width, height, ascent);
22811 }
22812
22813 it->pixel_width = width;
22814 it->ascent = it->phys_ascent = ascent;
22815 it->descent = it->phys_descent = height - it->ascent;
22816 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22817
22818 take_vertical_position_into_account (it);
22819 }
22820
22821 /* Calculate line-height and line-spacing properties.
22822 An integer value specifies explicit pixel value.
22823 A float value specifies relative value to current face height.
22824 A cons (float . face-name) specifies relative value to
22825 height of specified face font.
22826
22827 Returns height in pixels, or nil. */
22828
22829
22830 static Lisp_Object
22831 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22832 int boff, int override)
22833 {
22834 Lisp_Object face_name = Qnil;
22835 int ascent, descent, height;
22836
22837 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22838 return val;
22839
22840 if (CONSP (val))
22841 {
22842 face_name = XCAR (val);
22843 val = XCDR (val);
22844 if (!NUMBERP (val))
22845 val = make_number (1);
22846 if (NILP (face_name))
22847 {
22848 height = it->ascent + it->descent;
22849 goto scale;
22850 }
22851 }
22852
22853 if (NILP (face_name))
22854 {
22855 font = FRAME_FONT (it->f);
22856 boff = FRAME_BASELINE_OFFSET (it->f);
22857 }
22858 else if (EQ (face_name, Qt))
22859 {
22860 override = 0;
22861 }
22862 else
22863 {
22864 int face_id;
22865 struct face *face;
22866
22867 face_id = lookup_named_face (it->f, face_name, 0);
22868 if (face_id < 0)
22869 return make_number (-1);
22870
22871 face = FACE_FROM_ID (it->f, face_id);
22872 font = face->font;
22873 if (font == NULL)
22874 return make_number (-1);
22875 boff = font->baseline_offset;
22876 if (font->vertical_centering)
22877 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22878 }
22879
22880 ascent = FONT_BASE (font) + boff;
22881 descent = FONT_DESCENT (font) - boff;
22882
22883 if (override)
22884 {
22885 it->override_ascent = ascent;
22886 it->override_descent = descent;
22887 it->override_boff = boff;
22888 }
22889
22890 height = ascent + descent;
22891
22892 scale:
22893 if (FLOATP (val))
22894 height = (int)(XFLOAT_DATA (val) * height);
22895 else if (INTEGERP (val))
22896 height *= XINT (val);
22897
22898 return make_number (height);
22899 }
22900
22901
22902 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22903 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22904 and only if this is for a character for which no font was found.
22905
22906 If the display method (it->glyphless_method) is
22907 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22908 length of the acronym or the hexadecimal string, UPPER_XOFF and
22909 UPPER_YOFF are pixel offsets for the upper part of the string,
22910 LOWER_XOFF and LOWER_YOFF are for the lower part.
22911
22912 For the other display methods, LEN through LOWER_YOFF are zero. */
22913
22914 static void
22915 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22916 short upper_xoff, short upper_yoff,
22917 short lower_xoff, short lower_yoff)
22918 {
22919 struct glyph *glyph;
22920 enum glyph_row_area area = it->area;
22921
22922 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22923 if (glyph < it->glyph_row->glyphs[area + 1])
22924 {
22925 /* If the glyph row is reversed, we need to prepend the glyph
22926 rather than append it. */
22927 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22928 {
22929 struct glyph *g;
22930
22931 /* Make room for the additional glyph. */
22932 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22933 g[1] = *g;
22934 glyph = it->glyph_row->glyphs[area];
22935 }
22936 glyph->charpos = CHARPOS (it->position);
22937 glyph->object = it->object;
22938 glyph->pixel_width = it->pixel_width;
22939 glyph->ascent = it->ascent;
22940 glyph->descent = it->descent;
22941 glyph->voffset = it->voffset;
22942 glyph->type = GLYPHLESS_GLYPH;
22943 glyph->u.glyphless.method = it->glyphless_method;
22944 glyph->u.glyphless.for_no_font = for_no_font;
22945 glyph->u.glyphless.len = len;
22946 glyph->u.glyphless.ch = it->c;
22947 glyph->slice.glyphless.upper_xoff = upper_xoff;
22948 glyph->slice.glyphless.upper_yoff = upper_yoff;
22949 glyph->slice.glyphless.lower_xoff = lower_xoff;
22950 glyph->slice.glyphless.lower_yoff = lower_yoff;
22951 glyph->avoid_cursor_p = it->avoid_cursor_p;
22952 glyph->multibyte_p = it->multibyte_p;
22953 glyph->left_box_line_p = it->start_of_box_run_p;
22954 glyph->right_box_line_p = it->end_of_box_run_p;
22955 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22956 || it->phys_descent > it->descent);
22957 glyph->padding_p = 0;
22958 glyph->glyph_not_available_p = 0;
22959 glyph->face_id = face_id;
22960 glyph->font_type = FONT_TYPE_UNKNOWN;
22961 if (it->bidi_p)
22962 {
22963 glyph->resolved_level = it->bidi_it.resolved_level;
22964 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22965 abort ();
22966 glyph->bidi_type = it->bidi_it.type;
22967 }
22968 ++it->glyph_row->used[area];
22969 }
22970 else
22971 IT_EXPAND_MATRIX_WIDTH (it, area);
22972 }
22973
22974
22975 /* Produce a glyph for a glyphless character for iterator IT.
22976 IT->glyphless_method specifies which method to use for displaying
22977 the character. See the description of enum
22978 glyphless_display_method in dispextern.h for the detail.
22979
22980 FOR_NO_FONT is nonzero if and only if this is for a character for
22981 which no font was found. ACRONYM, if non-nil, is an acronym string
22982 for the character. */
22983
22984 static void
22985 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22986 {
22987 int face_id;
22988 struct face *face;
22989 struct font *font;
22990 int base_width, base_height, width, height;
22991 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22992 int len;
22993
22994 /* Get the metrics of the base font. We always refer to the current
22995 ASCII face. */
22996 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22997 font = face->font ? face->font : FRAME_FONT (it->f);
22998 it->ascent = FONT_BASE (font) + font->baseline_offset;
22999 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23000 base_height = it->ascent + it->descent;
23001 base_width = font->average_width;
23002
23003 /* Get a face ID for the glyph by utilizing a cache (the same way as
23004 doen for `escape-glyph' in get_next_display_element). */
23005 if (it->f == last_glyphless_glyph_frame
23006 && it->face_id == last_glyphless_glyph_face_id)
23007 {
23008 face_id = last_glyphless_glyph_merged_face_id;
23009 }
23010 else
23011 {
23012 /* Merge the `glyphless-char' face into the current face. */
23013 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23014 last_glyphless_glyph_frame = it->f;
23015 last_glyphless_glyph_face_id = it->face_id;
23016 last_glyphless_glyph_merged_face_id = face_id;
23017 }
23018
23019 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23020 {
23021 it->pixel_width = THIN_SPACE_WIDTH;
23022 len = 0;
23023 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23024 }
23025 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23026 {
23027 width = CHAR_WIDTH (it->c);
23028 if (width == 0)
23029 width = 1;
23030 else if (width > 4)
23031 width = 4;
23032 it->pixel_width = base_width * width;
23033 len = 0;
23034 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23035 }
23036 else
23037 {
23038 char buf[7];
23039 const char *str;
23040 unsigned int code[6];
23041 int upper_len;
23042 int ascent, descent;
23043 struct font_metrics metrics_upper, metrics_lower;
23044
23045 face = FACE_FROM_ID (it->f, face_id);
23046 font = face->font ? face->font : FRAME_FONT (it->f);
23047 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23048
23049 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23050 {
23051 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23052 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23053 if (CONSP (acronym))
23054 acronym = XCAR (acronym);
23055 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23056 }
23057 else
23058 {
23059 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23060 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23061 str = buf;
23062 }
23063 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23064 code[len] = font->driver->encode_char (font, str[len]);
23065 upper_len = (len + 1) / 2;
23066 font->driver->text_extents (font, code, upper_len,
23067 &metrics_upper);
23068 font->driver->text_extents (font, code + upper_len, len - upper_len,
23069 &metrics_lower);
23070
23071
23072
23073 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23074 width = max (metrics_upper.width, metrics_lower.width) + 4;
23075 upper_xoff = upper_yoff = 2; /* the typical case */
23076 if (base_width >= width)
23077 {
23078 /* Align the upper to the left, the lower to the right. */
23079 it->pixel_width = base_width;
23080 lower_xoff = base_width - 2 - metrics_lower.width;
23081 }
23082 else
23083 {
23084 /* Center the shorter one. */
23085 it->pixel_width = width;
23086 if (metrics_upper.width >= metrics_lower.width)
23087 lower_xoff = (width - metrics_lower.width) / 2;
23088 else
23089 {
23090 /* FIXME: This code doesn't look right. It formerly was
23091 missing the "lower_xoff = 0;", which couldn't have
23092 been right since it left lower_xoff uninitialized. */
23093 lower_xoff = 0;
23094 upper_xoff = (width - metrics_upper.width) / 2;
23095 }
23096 }
23097
23098 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23099 top, bottom, and between upper and lower strings. */
23100 height = (metrics_upper.ascent + metrics_upper.descent
23101 + metrics_lower.ascent + metrics_lower.descent) + 5;
23102 /* Center vertically.
23103 H:base_height, D:base_descent
23104 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23105
23106 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23107 descent = D - H/2 + h/2;
23108 lower_yoff = descent - 2 - ld;
23109 upper_yoff = lower_yoff - la - 1 - ud; */
23110 ascent = - (it->descent - (base_height + height + 1) / 2);
23111 descent = it->descent - (base_height - height) / 2;
23112 lower_yoff = descent - 2 - metrics_lower.descent;
23113 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23114 - metrics_upper.descent);
23115 /* Don't make the height shorter than the base height. */
23116 if (height > base_height)
23117 {
23118 it->ascent = ascent;
23119 it->descent = descent;
23120 }
23121 }
23122
23123 it->phys_ascent = it->ascent;
23124 it->phys_descent = it->descent;
23125 if (it->glyph_row)
23126 append_glyphless_glyph (it, face_id, for_no_font, len,
23127 upper_xoff, upper_yoff,
23128 lower_xoff, lower_yoff);
23129 it->nglyphs = 1;
23130 take_vertical_position_into_account (it);
23131 }
23132
23133
23134 /* RIF:
23135 Produce glyphs/get display metrics for the display element IT is
23136 loaded with. See the description of struct it in dispextern.h
23137 for an overview of struct it. */
23138
23139 void
23140 x_produce_glyphs (struct it *it)
23141 {
23142 int extra_line_spacing = it->extra_line_spacing;
23143
23144 it->glyph_not_available_p = 0;
23145
23146 if (it->what == IT_CHARACTER)
23147 {
23148 XChar2b char2b;
23149 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23150 struct font *font = face->font;
23151 struct font_metrics *pcm = NULL;
23152 int boff; /* baseline offset */
23153
23154 if (font == NULL)
23155 {
23156 /* When no suitable font is found, display this character by
23157 the method specified in the first extra slot of
23158 Vglyphless_char_display. */
23159 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23160
23161 xassert (it->what == IT_GLYPHLESS);
23162 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23163 goto done;
23164 }
23165
23166 boff = font->baseline_offset;
23167 if (font->vertical_centering)
23168 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23169
23170 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23171 {
23172 int stretched_p;
23173
23174 it->nglyphs = 1;
23175
23176 if (it->override_ascent >= 0)
23177 {
23178 it->ascent = it->override_ascent;
23179 it->descent = it->override_descent;
23180 boff = it->override_boff;
23181 }
23182 else
23183 {
23184 it->ascent = FONT_BASE (font) + boff;
23185 it->descent = FONT_DESCENT (font) - boff;
23186 }
23187
23188 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23189 {
23190 pcm = get_per_char_metric (font, &char2b);
23191 if (pcm->width == 0
23192 && pcm->rbearing == 0 && pcm->lbearing == 0)
23193 pcm = NULL;
23194 }
23195
23196 if (pcm)
23197 {
23198 it->phys_ascent = pcm->ascent + boff;
23199 it->phys_descent = pcm->descent - boff;
23200 it->pixel_width = pcm->width;
23201 }
23202 else
23203 {
23204 it->glyph_not_available_p = 1;
23205 it->phys_ascent = it->ascent;
23206 it->phys_descent = it->descent;
23207 it->pixel_width = font->space_width;
23208 }
23209
23210 if (it->constrain_row_ascent_descent_p)
23211 {
23212 if (it->descent > it->max_descent)
23213 {
23214 it->ascent += it->descent - it->max_descent;
23215 it->descent = it->max_descent;
23216 }
23217 if (it->ascent > it->max_ascent)
23218 {
23219 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23220 it->ascent = it->max_ascent;
23221 }
23222 it->phys_ascent = min (it->phys_ascent, it->ascent);
23223 it->phys_descent = min (it->phys_descent, it->descent);
23224 extra_line_spacing = 0;
23225 }
23226
23227 /* If this is a space inside a region of text with
23228 `space-width' property, change its width. */
23229 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23230 if (stretched_p)
23231 it->pixel_width *= XFLOATINT (it->space_width);
23232
23233 /* If face has a box, add the box thickness to the character
23234 height. If character has a box line to the left and/or
23235 right, add the box line width to the character's width. */
23236 if (face->box != FACE_NO_BOX)
23237 {
23238 int thick = face->box_line_width;
23239
23240 if (thick > 0)
23241 {
23242 it->ascent += thick;
23243 it->descent += thick;
23244 }
23245 else
23246 thick = -thick;
23247
23248 if (it->start_of_box_run_p)
23249 it->pixel_width += thick;
23250 if (it->end_of_box_run_p)
23251 it->pixel_width += thick;
23252 }
23253
23254 /* If face has an overline, add the height of the overline
23255 (1 pixel) and a 1 pixel margin to the character height. */
23256 if (face->overline_p)
23257 it->ascent += overline_margin;
23258
23259 if (it->constrain_row_ascent_descent_p)
23260 {
23261 if (it->ascent > it->max_ascent)
23262 it->ascent = it->max_ascent;
23263 if (it->descent > it->max_descent)
23264 it->descent = it->max_descent;
23265 }
23266
23267 take_vertical_position_into_account (it);
23268
23269 /* If we have to actually produce glyphs, do it. */
23270 if (it->glyph_row)
23271 {
23272 if (stretched_p)
23273 {
23274 /* Translate a space with a `space-width' property
23275 into a stretch glyph. */
23276 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23277 / FONT_HEIGHT (font));
23278 append_stretch_glyph (it, it->object, it->pixel_width,
23279 it->ascent + it->descent, ascent);
23280 }
23281 else
23282 append_glyph (it);
23283
23284 /* If characters with lbearing or rbearing are displayed
23285 in this line, record that fact in a flag of the
23286 glyph row. This is used to optimize X output code. */
23287 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23288 it->glyph_row->contains_overlapping_glyphs_p = 1;
23289 }
23290 if (! stretched_p && it->pixel_width == 0)
23291 /* We assure that all visible glyphs have at least 1-pixel
23292 width. */
23293 it->pixel_width = 1;
23294 }
23295 else if (it->char_to_display == '\n')
23296 {
23297 /* A newline has no width, but we need the height of the
23298 line. But if previous part of the line sets a height,
23299 don't increase that height */
23300
23301 Lisp_Object height;
23302 Lisp_Object total_height = Qnil;
23303
23304 it->override_ascent = -1;
23305 it->pixel_width = 0;
23306 it->nglyphs = 0;
23307
23308 height = get_it_property (it, Qline_height);
23309 /* Split (line-height total-height) list */
23310 if (CONSP (height)
23311 && CONSP (XCDR (height))
23312 && NILP (XCDR (XCDR (height))))
23313 {
23314 total_height = XCAR (XCDR (height));
23315 height = XCAR (height);
23316 }
23317 height = calc_line_height_property (it, height, font, boff, 1);
23318
23319 if (it->override_ascent >= 0)
23320 {
23321 it->ascent = it->override_ascent;
23322 it->descent = it->override_descent;
23323 boff = it->override_boff;
23324 }
23325 else
23326 {
23327 it->ascent = FONT_BASE (font) + boff;
23328 it->descent = FONT_DESCENT (font) - boff;
23329 }
23330
23331 if (EQ (height, Qt))
23332 {
23333 if (it->descent > it->max_descent)
23334 {
23335 it->ascent += it->descent - it->max_descent;
23336 it->descent = it->max_descent;
23337 }
23338 if (it->ascent > it->max_ascent)
23339 {
23340 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23341 it->ascent = it->max_ascent;
23342 }
23343 it->phys_ascent = min (it->phys_ascent, it->ascent);
23344 it->phys_descent = min (it->phys_descent, it->descent);
23345 it->constrain_row_ascent_descent_p = 1;
23346 extra_line_spacing = 0;
23347 }
23348 else
23349 {
23350 Lisp_Object spacing;
23351
23352 it->phys_ascent = it->ascent;
23353 it->phys_descent = it->descent;
23354
23355 if ((it->max_ascent > 0 || it->max_descent > 0)
23356 && face->box != FACE_NO_BOX
23357 && face->box_line_width > 0)
23358 {
23359 it->ascent += face->box_line_width;
23360 it->descent += face->box_line_width;
23361 }
23362 if (!NILP (height)
23363 && XINT (height) > it->ascent + it->descent)
23364 it->ascent = XINT (height) - it->descent;
23365
23366 if (!NILP (total_height))
23367 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23368 else
23369 {
23370 spacing = get_it_property (it, Qline_spacing);
23371 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23372 }
23373 if (INTEGERP (spacing))
23374 {
23375 extra_line_spacing = XINT (spacing);
23376 if (!NILP (total_height))
23377 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23378 }
23379 }
23380 }
23381 else /* i.e. (it->char_to_display == '\t') */
23382 {
23383 if (font->space_width > 0)
23384 {
23385 int tab_width = it->tab_width * font->space_width;
23386 int x = it->current_x + it->continuation_lines_width;
23387 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23388
23389 /* If the distance from the current position to the next tab
23390 stop is less than a space character width, use the
23391 tab stop after that. */
23392 if (next_tab_x - x < font->space_width)
23393 next_tab_x += tab_width;
23394
23395 it->pixel_width = next_tab_x - x;
23396 it->nglyphs = 1;
23397 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23398 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23399
23400 if (it->glyph_row)
23401 {
23402 append_stretch_glyph (it, it->object, it->pixel_width,
23403 it->ascent + it->descent, it->ascent);
23404 }
23405 }
23406 else
23407 {
23408 it->pixel_width = 0;
23409 it->nglyphs = 1;
23410 }
23411 }
23412 }
23413 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23414 {
23415 /* A static composition.
23416
23417 Note: A composition is represented as one glyph in the
23418 glyph matrix. There are no padding glyphs.
23419
23420 Important note: pixel_width, ascent, and descent are the
23421 values of what is drawn by draw_glyphs (i.e. the values of
23422 the overall glyphs composed). */
23423 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23424 int boff; /* baseline offset */
23425 struct composition *cmp = composition_table[it->cmp_it.id];
23426 int glyph_len = cmp->glyph_len;
23427 struct font *font = face->font;
23428
23429 it->nglyphs = 1;
23430
23431 /* If we have not yet calculated pixel size data of glyphs of
23432 the composition for the current face font, calculate them
23433 now. Theoretically, we have to check all fonts for the
23434 glyphs, but that requires much time and memory space. So,
23435 here we check only the font of the first glyph. This may
23436 lead to incorrect display, but it's very rare, and C-l
23437 (recenter-top-bottom) can correct the display anyway. */
23438 if (! cmp->font || cmp->font != font)
23439 {
23440 /* Ascent and descent of the font of the first character
23441 of this composition (adjusted by baseline offset).
23442 Ascent and descent of overall glyphs should not be less
23443 than these, respectively. */
23444 int font_ascent, font_descent, font_height;
23445 /* Bounding box of the overall glyphs. */
23446 int leftmost, rightmost, lowest, highest;
23447 int lbearing, rbearing;
23448 int i, width, ascent, descent;
23449 int left_padded = 0, right_padded = 0;
23450 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23451 XChar2b char2b;
23452 struct font_metrics *pcm;
23453 int font_not_found_p;
23454 EMACS_INT pos;
23455
23456 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23457 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23458 break;
23459 if (glyph_len < cmp->glyph_len)
23460 right_padded = 1;
23461 for (i = 0; i < glyph_len; i++)
23462 {
23463 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23464 break;
23465 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23466 }
23467 if (i > 0)
23468 left_padded = 1;
23469
23470 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23471 : IT_CHARPOS (*it));
23472 /* If no suitable font is found, use the default font. */
23473 font_not_found_p = font == NULL;
23474 if (font_not_found_p)
23475 {
23476 face = face->ascii_face;
23477 font = face->font;
23478 }
23479 boff = font->baseline_offset;
23480 if (font->vertical_centering)
23481 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23482 font_ascent = FONT_BASE (font) + boff;
23483 font_descent = FONT_DESCENT (font) - boff;
23484 font_height = FONT_HEIGHT (font);
23485
23486 cmp->font = (void *) font;
23487
23488 pcm = NULL;
23489 if (! font_not_found_p)
23490 {
23491 get_char_face_and_encoding (it->f, c, it->face_id,
23492 &char2b, 0);
23493 pcm = get_per_char_metric (font, &char2b);
23494 }
23495
23496 /* Initialize the bounding box. */
23497 if (pcm)
23498 {
23499 width = pcm->width;
23500 ascent = pcm->ascent;
23501 descent = pcm->descent;
23502 lbearing = pcm->lbearing;
23503 rbearing = pcm->rbearing;
23504 }
23505 else
23506 {
23507 width = font->space_width;
23508 ascent = FONT_BASE (font);
23509 descent = FONT_DESCENT (font);
23510 lbearing = 0;
23511 rbearing = width;
23512 }
23513
23514 rightmost = width;
23515 leftmost = 0;
23516 lowest = - descent + boff;
23517 highest = ascent + boff;
23518
23519 if (! font_not_found_p
23520 && font->default_ascent
23521 && CHAR_TABLE_P (Vuse_default_ascent)
23522 && !NILP (Faref (Vuse_default_ascent,
23523 make_number (it->char_to_display))))
23524 highest = font->default_ascent + boff;
23525
23526 /* Draw the first glyph at the normal position. It may be
23527 shifted to right later if some other glyphs are drawn
23528 at the left. */
23529 cmp->offsets[i * 2] = 0;
23530 cmp->offsets[i * 2 + 1] = boff;
23531 cmp->lbearing = lbearing;
23532 cmp->rbearing = rbearing;
23533
23534 /* Set cmp->offsets for the remaining glyphs. */
23535 for (i++; i < glyph_len; i++)
23536 {
23537 int left, right, btm, top;
23538 int ch = COMPOSITION_GLYPH (cmp, i);
23539 int face_id;
23540 struct face *this_face;
23541
23542 if (ch == '\t')
23543 ch = ' ';
23544 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23545 this_face = FACE_FROM_ID (it->f, face_id);
23546 font = this_face->font;
23547
23548 if (font == NULL)
23549 pcm = NULL;
23550 else
23551 {
23552 get_char_face_and_encoding (it->f, ch, face_id,
23553 &char2b, 0);
23554 pcm = get_per_char_metric (font, &char2b);
23555 }
23556 if (! pcm)
23557 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23558 else
23559 {
23560 width = pcm->width;
23561 ascent = pcm->ascent;
23562 descent = pcm->descent;
23563 lbearing = pcm->lbearing;
23564 rbearing = pcm->rbearing;
23565 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23566 {
23567 /* Relative composition with or without
23568 alternate chars. */
23569 left = (leftmost + rightmost - width) / 2;
23570 btm = - descent + boff;
23571 if (font->relative_compose
23572 && (! CHAR_TABLE_P (Vignore_relative_composition)
23573 || NILP (Faref (Vignore_relative_composition,
23574 make_number (ch)))))
23575 {
23576
23577 if (- descent >= font->relative_compose)
23578 /* One extra pixel between two glyphs. */
23579 btm = highest + 1;
23580 else if (ascent <= 0)
23581 /* One extra pixel between two glyphs. */
23582 btm = lowest - 1 - ascent - descent;
23583 }
23584 }
23585 else
23586 {
23587 /* A composition rule is specified by an integer
23588 value that encodes global and new reference
23589 points (GREF and NREF). GREF and NREF are
23590 specified by numbers as below:
23591
23592 0---1---2 -- ascent
23593 | |
23594 | |
23595 | |
23596 9--10--11 -- center
23597 | |
23598 ---3---4---5--- baseline
23599 | |
23600 6---7---8 -- descent
23601 */
23602 int rule = COMPOSITION_RULE (cmp, i);
23603 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23604
23605 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23606 grefx = gref % 3, nrefx = nref % 3;
23607 grefy = gref / 3, nrefy = nref / 3;
23608 if (xoff)
23609 xoff = font_height * (xoff - 128) / 256;
23610 if (yoff)
23611 yoff = font_height * (yoff - 128) / 256;
23612
23613 left = (leftmost
23614 + grefx * (rightmost - leftmost) / 2
23615 - nrefx * width / 2
23616 + xoff);
23617
23618 btm = ((grefy == 0 ? highest
23619 : grefy == 1 ? 0
23620 : grefy == 2 ? lowest
23621 : (highest + lowest) / 2)
23622 - (nrefy == 0 ? ascent + descent
23623 : nrefy == 1 ? descent - boff
23624 : nrefy == 2 ? 0
23625 : (ascent + descent) / 2)
23626 + yoff);
23627 }
23628
23629 cmp->offsets[i * 2] = left;
23630 cmp->offsets[i * 2 + 1] = btm + descent;
23631
23632 /* Update the bounding box of the overall glyphs. */
23633 if (width > 0)
23634 {
23635 right = left + width;
23636 if (left < leftmost)
23637 leftmost = left;
23638 if (right > rightmost)
23639 rightmost = right;
23640 }
23641 top = btm + descent + ascent;
23642 if (top > highest)
23643 highest = top;
23644 if (btm < lowest)
23645 lowest = btm;
23646
23647 if (cmp->lbearing > left + lbearing)
23648 cmp->lbearing = left + lbearing;
23649 if (cmp->rbearing < left + rbearing)
23650 cmp->rbearing = left + rbearing;
23651 }
23652 }
23653
23654 /* If there are glyphs whose x-offsets are negative,
23655 shift all glyphs to the right and make all x-offsets
23656 non-negative. */
23657 if (leftmost < 0)
23658 {
23659 for (i = 0; i < cmp->glyph_len; i++)
23660 cmp->offsets[i * 2] -= leftmost;
23661 rightmost -= leftmost;
23662 cmp->lbearing -= leftmost;
23663 cmp->rbearing -= leftmost;
23664 }
23665
23666 if (left_padded && cmp->lbearing < 0)
23667 {
23668 for (i = 0; i < cmp->glyph_len; i++)
23669 cmp->offsets[i * 2] -= cmp->lbearing;
23670 rightmost -= cmp->lbearing;
23671 cmp->rbearing -= cmp->lbearing;
23672 cmp->lbearing = 0;
23673 }
23674 if (right_padded && rightmost < cmp->rbearing)
23675 {
23676 rightmost = cmp->rbearing;
23677 }
23678
23679 cmp->pixel_width = rightmost;
23680 cmp->ascent = highest;
23681 cmp->descent = - lowest;
23682 if (cmp->ascent < font_ascent)
23683 cmp->ascent = font_ascent;
23684 if (cmp->descent < font_descent)
23685 cmp->descent = font_descent;
23686 }
23687
23688 if (it->glyph_row
23689 && (cmp->lbearing < 0
23690 || cmp->rbearing > cmp->pixel_width))
23691 it->glyph_row->contains_overlapping_glyphs_p = 1;
23692
23693 it->pixel_width = cmp->pixel_width;
23694 it->ascent = it->phys_ascent = cmp->ascent;
23695 it->descent = it->phys_descent = cmp->descent;
23696 if (face->box != FACE_NO_BOX)
23697 {
23698 int thick = face->box_line_width;
23699
23700 if (thick > 0)
23701 {
23702 it->ascent += thick;
23703 it->descent += thick;
23704 }
23705 else
23706 thick = - thick;
23707
23708 if (it->start_of_box_run_p)
23709 it->pixel_width += thick;
23710 if (it->end_of_box_run_p)
23711 it->pixel_width += thick;
23712 }
23713
23714 /* If face has an overline, add the height of the overline
23715 (1 pixel) and a 1 pixel margin to the character height. */
23716 if (face->overline_p)
23717 it->ascent += overline_margin;
23718
23719 take_vertical_position_into_account (it);
23720 if (it->ascent < 0)
23721 it->ascent = 0;
23722 if (it->descent < 0)
23723 it->descent = 0;
23724
23725 if (it->glyph_row)
23726 append_composite_glyph (it);
23727 }
23728 else if (it->what == IT_COMPOSITION)
23729 {
23730 /* A dynamic (automatic) composition. */
23731 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23732 Lisp_Object gstring;
23733 struct font_metrics metrics;
23734
23735 gstring = composition_gstring_from_id (it->cmp_it.id);
23736 it->pixel_width
23737 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23738 &metrics);
23739 if (it->glyph_row
23740 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23741 it->glyph_row->contains_overlapping_glyphs_p = 1;
23742 it->ascent = it->phys_ascent = metrics.ascent;
23743 it->descent = it->phys_descent = metrics.descent;
23744 if (face->box != FACE_NO_BOX)
23745 {
23746 int thick = face->box_line_width;
23747
23748 if (thick > 0)
23749 {
23750 it->ascent += thick;
23751 it->descent += thick;
23752 }
23753 else
23754 thick = - thick;
23755
23756 if (it->start_of_box_run_p)
23757 it->pixel_width += thick;
23758 if (it->end_of_box_run_p)
23759 it->pixel_width += thick;
23760 }
23761 /* If face has an overline, add the height of the overline
23762 (1 pixel) and a 1 pixel margin to the character height. */
23763 if (face->overline_p)
23764 it->ascent += overline_margin;
23765 take_vertical_position_into_account (it);
23766 if (it->ascent < 0)
23767 it->ascent = 0;
23768 if (it->descent < 0)
23769 it->descent = 0;
23770
23771 if (it->glyph_row)
23772 append_composite_glyph (it);
23773 }
23774 else if (it->what == IT_GLYPHLESS)
23775 produce_glyphless_glyph (it, 0, Qnil);
23776 else if (it->what == IT_IMAGE)
23777 produce_image_glyph (it);
23778 else if (it->what == IT_STRETCH)
23779 produce_stretch_glyph (it);
23780
23781 done:
23782 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23783 because this isn't true for images with `:ascent 100'. */
23784 xassert (it->ascent >= 0 && it->descent >= 0);
23785 if (it->area == TEXT_AREA)
23786 it->current_x += it->pixel_width;
23787
23788 if (extra_line_spacing > 0)
23789 {
23790 it->descent += extra_line_spacing;
23791 if (extra_line_spacing > it->max_extra_line_spacing)
23792 it->max_extra_line_spacing = extra_line_spacing;
23793 }
23794
23795 it->max_ascent = max (it->max_ascent, it->ascent);
23796 it->max_descent = max (it->max_descent, it->descent);
23797 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23798 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23799 }
23800
23801 /* EXPORT for RIF:
23802 Output LEN glyphs starting at START at the nominal cursor position.
23803 Advance the nominal cursor over the text. The global variable
23804 updated_window contains the window being updated, updated_row is
23805 the glyph row being updated, and updated_area is the area of that
23806 row being updated. */
23807
23808 void
23809 x_write_glyphs (struct glyph *start, int len)
23810 {
23811 int x, hpos;
23812
23813 xassert (updated_window && updated_row);
23814 BLOCK_INPUT;
23815
23816 /* Write glyphs. */
23817
23818 hpos = start - updated_row->glyphs[updated_area];
23819 x = draw_glyphs (updated_window, output_cursor.x,
23820 updated_row, updated_area,
23821 hpos, hpos + len,
23822 DRAW_NORMAL_TEXT, 0);
23823
23824 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23825 if (updated_area == TEXT_AREA
23826 && updated_window->phys_cursor_on_p
23827 && updated_window->phys_cursor.vpos == output_cursor.vpos
23828 && updated_window->phys_cursor.hpos >= hpos
23829 && updated_window->phys_cursor.hpos < hpos + len)
23830 updated_window->phys_cursor_on_p = 0;
23831
23832 UNBLOCK_INPUT;
23833
23834 /* Advance the output cursor. */
23835 output_cursor.hpos += len;
23836 output_cursor.x = x;
23837 }
23838
23839
23840 /* EXPORT for RIF:
23841 Insert LEN glyphs from START at the nominal cursor position. */
23842
23843 void
23844 x_insert_glyphs (struct glyph *start, int len)
23845 {
23846 struct frame *f;
23847 struct window *w;
23848 int line_height, shift_by_width, shifted_region_width;
23849 struct glyph_row *row;
23850 struct glyph *glyph;
23851 int frame_x, frame_y;
23852 EMACS_INT hpos;
23853
23854 xassert (updated_window && updated_row);
23855 BLOCK_INPUT;
23856 w = updated_window;
23857 f = XFRAME (WINDOW_FRAME (w));
23858
23859 /* Get the height of the line we are in. */
23860 row = updated_row;
23861 line_height = row->height;
23862
23863 /* Get the width of the glyphs to insert. */
23864 shift_by_width = 0;
23865 for (glyph = start; glyph < start + len; ++glyph)
23866 shift_by_width += glyph->pixel_width;
23867
23868 /* Get the width of the region to shift right. */
23869 shifted_region_width = (window_box_width (w, updated_area)
23870 - output_cursor.x
23871 - shift_by_width);
23872
23873 /* Shift right. */
23874 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23875 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23876
23877 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23878 line_height, shift_by_width);
23879
23880 /* Write the glyphs. */
23881 hpos = start - row->glyphs[updated_area];
23882 draw_glyphs (w, output_cursor.x, row, updated_area,
23883 hpos, hpos + len,
23884 DRAW_NORMAL_TEXT, 0);
23885
23886 /* Advance the output cursor. */
23887 output_cursor.hpos += len;
23888 output_cursor.x += shift_by_width;
23889 UNBLOCK_INPUT;
23890 }
23891
23892
23893 /* EXPORT for RIF:
23894 Erase the current text line from the nominal cursor position
23895 (inclusive) to pixel column TO_X (exclusive). The idea is that
23896 everything from TO_X onward is already erased.
23897
23898 TO_X is a pixel position relative to updated_area of
23899 updated_window. TO_X == -1 means clear to the end of this area. */
23900
23901 void
23902 x_clear_end_of_line (int to_x)
23903 {
23904 struct frame *f;
23905 struct window *w = updated_window;
23906 int max_x, min_y, max_y;
23907 int from_x, from_y, to_y;
23908
23909 xassert (updated_window && updated_row);
23910 f = XFRAME (w->frame);
23911
23912 if (updated_row->full_width_p)
23913 max_x = WINDOW_TOTAL_WIDTH (w);
23914 else
23915 max_x = window_box_width (w, updated_area);
23916 max_y = window_text_bottom_y (w);
23917
23918 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23919 of window. For TO_X > 0, truncate to end of drawing area. */
23920 if (to_x == 0)
23921 return;
23922 else if (to_x < 0)
23923 to_x = max_x;
23924 else
23925 to_x = min (to_x, max_x);
23926
23927 to_y = min (max_y, output_cursor.y + updated_row->height);
23928
23929 /* Notice if the cursor will be cleared by this operation. */
23930 if (!updated_row->full_width_p)
23931 notice_overwritten_cursor (w, updated_area,
23932 output_cursor.x, -1,
23933 updated_row->y,
23934 MATRIX_ROW_BOTTOM_Y (updated_row));
23935
23936 from_x = output_cursor.x;
23937
23938 /* Translate to frame coordinates. */
23939 if (updated_row->full_width_p)
23940 {
23941 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23942 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23943 }
23944 else
23945 {
23946 int area_left = window_box_left (w, updated_area);
23947 from_x += area_left;
23948 to_x += area_left;
23949 }
23950
23951 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23952 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23953 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23954
23955 /* Prevent inadvertently clearing to end of the X window. */
23956 if (to_x > from_x && to_y > from_y)
23957 {
23958 BLOCK_INPUT;
23959 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23960 to_x - from_x, to_y - from_y);
23961 UNBLOCK_INPUT;
23962 }
23963 }
23964
23965 #endif /* HAVE_WINDOW_SYSTEM */
23966
23967
23968 \f
23969 /***********************************************************************
23970 Cursor types
23971 ***********************************************************************/
23972
23973 /* Value is the internal representation of the specified cursor type
23974 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23975 of the bar cursor. */
23976
23977 static enum text_cursor_kinds
23978 get_specified_cursor_type (Lisp_Object arg, int *width)
23979 {
23980 enum text_cursor_kinds type;
23981
23982 if (NILP (arg))
23983 return NO_CURSOR;
23984
23985 if (EQ (arg, Qbox))
23986 return FILLED_BOX_CURSOR;
23987
23988 if (EQ (arg, Qhollow))
23989 return HOLLOW_BOX_CURSOR;
23990
23991 if (EQ (arg, Qbar))
23992 {
23993 *width = 2;
23994 return BAR_CURSOR;
23995 }
23996
23997 if (CONSP (arg)
23998 && EQ (XCAR (arg), Qbar)
23999 && INTEGERP (XCDR (arg))
24000 && XINT (XCDR (arg)) >= 0)
24001 {
24002 *width = XINT (XCDR (arg));
24003 return BAR_CURSOR;
24004 }
24005
24006 if (EQ (arg, Qhbar))
24007 {
24008 *width = 2;
24009 return HBAR_CURSOR;
24010 }
24011
24012 if (CONSP (arg)
24013 && EQ (XCAR (arg), Qhbar)
24014 && INTEGERP (XCDR (arg))
24015 && XINT (XCDR (arg)) >= 0)
24016 {
24017 *width = XINT (XCDR (arg));
24018 return HBAR_CURSOR;
24019 }
24020
24021 /* Treat anything unknown as "hollow box cursor".
24022 It was bad to signal an error; people have trouble fixing
24023 .Xdefaults with Emacs, when it has something bad in it. */
24024 type = HOLLOW_BOX_CURSOR;
24025
24026 return type;
24027 }
24028
24029 /* Set the default cursor types for specified frame. */
24030 void
24031 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24032 {
24033 int width = 1;
24034 Lisp_Object tem;
24035
24036 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24037 FRAME_CURSOR_WIDTH (f) = width;
24038
24039 /* By default, set up the blink-off state depending on the on-state. */
24040
24041 tem = Fassoc (arg, Vblink_cursor_alist);
24042 if (!NILP (tem))
24043 {
24044 FRAME_BLINK_OFF_CURSOR (f)
24045 = get_specified_cursor_type (XCDR (tem), &width);
24046 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24047 }
24048 else
24049 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24050 }
24051
24052
24053 #ifdef HAVE_WINDOW_SYSTEM
24054
24055 /* Return the cursor we want to be displayed in window W. Return
24056 width of bar/hbar cursor through WIDTH arg. Return with
24057 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24058 (i.e. if the `system caret' should track this cursor).
24059
24060 In a mini-buffer window, we want the cursor only to appear if we
24061 are reading input from this window. For the selected window, we
24062 want the cursor type given by the frame parameter or buffer local
24063 setting of cursor-type. If explicitly marked off, draw no cursor.
24064 In all other cases, we want a hollow box cursor. */
24065
24066 static enum text_cursor_kinds
24067 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24068 int *active_cursor)
24069 {
24070 struct frame *f = XFRAME (w->frame);
24071 struct buffer *b = XBUFFER (w->buffer);
24072 int cursor_type = DEFAULT_CURSOR;
24073 Lisp_Object alt_cursor;
24074 int non_selected = 0;
24075
24076 *active_cursor = 1;
24077
24078 /* Echo area */
24079 if (cursor_in_echo_area
24080 && FRAME_HAS_MINIBUF_P (f)
24081 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24082 {
24083 if (w == XWINDOW (echo_area_window))
24084 {
24085 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24086 {
24087 *width = FRAME_CURSOR_WIDTH (f);
24088 return FRAME_DESIRED_CURSOR (f);
24089 }
24090 else
24091 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24092 }
24093
24094 *active_cursor = 0;
24095 non_selected = 1;
24096 }
24097
24098 /* Detect a nonselected window or nonselected frame. */
24099 else if (w != XWINDOW (f->selected_window)
24100 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24101 {
24102 *active_cursor = 0;
24103
24104 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24105 return NO_CURSOR;
24106
24107 non_selected = 1;
24108 }
24109
24110 /* Never display a cursor in a window in which cursor-type is nil. */
24111 if (NILP (BVAR (b, cursor_type)))
24112 return NO_CURSOR;
24113
24114 /* Get the normal cursor type for this window. */
24115 if (EQ (BVAR (b, cursor_type), Qt))
24116 {
24117 cursor_type = FRAME_DESIRED_CURSOR (f);
24118 *width = FRAME_CURSOR_WIDTH (f);
24119 }
24120 else
24121 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24122
24123 /* Use cursor-in-non-selected-windows instead
24124 for non-selected window or frame. */
24125 if (non_selected)
24126 {
24127 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24128 if (!EQ (Qt, alt_cursor))
24129 return get_specified_cursor_type (alt_cursor, width);
24130 /* t means modify the normal cursor type. */
24131 if (cursor_type == FILLED_BOX_CURSOR)
24132 cursor_type = HOLLOW_BOX_CURSOR;
24133 else if (cursor_type == BAR_CURSOR && *width > 1)
24134 --*width;
24135 return cursor_type;
24136 }
24137
24138 /* Use normal cursor if not blinked off. */
24139 if (!w->cursor_off_p)
24140 {
24141 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24142 {
24143 if (cursor_type == FILLED_BOX_CURSOR)
24144 {
24145 /* Using a block cursor on large images can be very annoying.
24146 So use a hollow cursor for "large" images.
24147 If image is not transparent (no mask), also use hollow cursor. */
24148 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24149 if (img != NULL && IMAGEP (img->spec))
24150 {
24151 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24152 where N = size of default frame font size.
24153 This should cover most of the "tiny" icons people may use. */
24154 if (!img->mask
24155 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24156 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24157 cursor_type = HOLLOW_BOX_CURSOR;
24158 }
24159 }
24160 else if (cursor_type != NO_CURSOR)
24161 {
24162 /* Display current only supports BOX and HOLLOW cursors for images.
24163 So for now, unconditionally use a HOLLOW cursor when cursor is
24164 not a solid box cursor. */
24165 cursor_type = HOLLOW_BOX_CURSOR;
24166 }
24167 }
24168 return cursor_type;
24169 }
24170
24171 /* Cursor is blinked off, so determine how to "toggle" it. */
24172
24173 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24174 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24175 return get_specified_cursor_type (XCDR (alt_cursor), width);
24176
24177 /* Then see if frame has specified a specific blink off cursor type. */
24178 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24179 {
24180 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24181 return FRAME_BLINK_OFF_CURSOR (f);
24182 }
24183
24184 #if 0
24185 /* Some people liked having a permanently visible blinking cursor,
24186 while others had very strong opinions against it. So it was
24187 decided to remove it. KFS 2003-09-03 */
24188
24189 /* Finally perform built-in cursor blinking:
24190 filled box <-> hollow box
24191 wide [h]bar <-> narrow [h]bar
24192 narrow [h]bar <-> no cursor
24193 other type <-> no cursor */
24194
24195 if (cursor_type == FILLED_BOX_CURSOR)
24196 return HOLLOW_BOX_CURSOR;
24197
24198 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24199 {
24200 *width = 1;
24201 return cursor_type;
24202 }
24203 #endif
24204
24205 return NO_CURSOR;
24206 }
24207
24208
24209 /* Notice when the text cursor of window W has been completely
24210 overwritten by a drawing operation that outputs glyphs in AREA
24211 starting at X0 and ending at X1 in the line starting at Y0 and
24212 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24213 the rest of the line after X0 has been written. Y coordinates
24214 are window-relative. */
24215
24216 static void
24217 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24218 int x0, int x1, int y0, int y1)
24219 {
24220 int cx0, cx1, cy0, cy1;
24221 struct glyph_row *row;
24222
24223 if (!w->phys_cursor_on_p)
24224 return;
24225 if (area != TEXT_AREA)
24226 return;
24227
24228 if (w->phys_cursor.vpos < 0
24229 || w->phys_cursor.vpos >= w->current_matrix->nrows
24230 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24231 !(row->enabled_p && row->displays_text_p)))
24232 return;
24233
24234 if (row->cursor_in_fringe_p)
24235 {
24236 row->cursor_in_fringe_p = 0;
24237 draw_fringe_bitmap (w, row, row->reversed_p);
24238 w->phys_cursor_on_p = 0;
24239 return;
24240 }
24241
24242 cx0 = w->phys_cursor.x;
24243 cx1 = cx0 + w->phys_cursor_width;
24244 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24245 return;
24246
24247 /* The cursor image will be completely removed from the
24248 screen if the output area intersects the cursor area in
24249 y-direction. When we draw in [y0 y1[, and some part of
24250 the cursor is at y < y0, that part must have been drawn
24251 before. When scrolling, the cursor is erased before
24252 actually scrolling, so we don't come here. When not
24253 scrolling, the rows above the old cursor row must have
24254 changed, and in this case these rows must have written
24255 over the cursor image.
24256
24257 Likewise if part of the cursor is below y1, with the
24258 exception of the cursor being in the first blank row at
24259 the buffer and window end because update_text_area
24260 doesn't draw that row. (Except when it does, but
24261 that's handled in update_text_area.) */
24262
24263 cy0 = w->phys_cursor.y;
24264 cy1 = cy0 + w->phys_cursor_height;
24265 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24266 return;
24267
24268 w->phys_cursor_on_p = 0;
24269 }
24270
24271 #endif /* HAVE_WINDOW_SYSTEM */
24272
24273 \f
24274 /************************************************************************
24275 Mouse Face
24276 ************************************************************************/
24277
24278 #ifdef HAVE_WINDOW_SYSTEM
24279
24280 /* EXPORT for RIF:
24281 Fix the display of area AREA of overlapping row ROW in window W
24282 with respect to the overlapping part OVERLAPS. */
24283
24284 void
24285 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24286 enum glyph_row_area area, int overlaps)
24287 {
24288 int i, x;
24289
24290 BLOCK_INPUT;
24291
24292 x = 0;
24293 for (i = 0; i < row->used[area];)
24294 {
24295 if (row->glyphs[area][i].overlaps_vertically_p)
24296 {
24297 int start = i, start_x = x;
24298
24299 do
24300 {
24301 x += row->glyphs[area][i].pixel_width;
24302 ++i;
24303 }
24304 while (i < row->used[area]
24305 && row->glyphs[area][i].overlaps_vertically_p);
24306
24307 draw_glyphs (w, start_x, row, area,
24308 start, i,
24309 DRAW_NORMAL_TEXT, overlaps);
24310 }
24311 else
24312 {
24313 x += row->glyphs[area][i].pixel_width;
24314 ++i;
24315 }
24316 }
24317
24318 UNBLOCK_INPUT;
24319 }
24320
24321
24322 /* EXPORT:
24323 Draw the cursor glyph of window W in glyph row ROW. See the
24324 comment of draw_glyphs for the meaning of HL. */
24325
24326 void
24327 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24328 enum draw_glyphs_face hl)
24329 {
24330 /* If cursor hpos is out of bounds, don't draw garbage. This can
24331 happen in mini-buffer windows when switching between echo area
24332 glyphs and mini-buffer. */
24333 if ((row->reversed_p
24334 ? (w->phys_cursor.hpos >= 0)
24335 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24336 {
24337 int on_p = w->phys_cursor_on_p;
24338 int x1;
24339 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24340 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24341 hl, 0);
24342 w->phys_cursor_on_p = on_p;
24343
24344 if (hl == DRAW_CURSOR)
24345 w->phys_cursor_width = x1 - w->phys_cursor.x;
24346 /* When we erase the cursor, and ROW is overlapped by other
24347 rows, make sure that these overlapping parts of other rows
24348 are redrawn. */
24349 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24350 {
24351 w->phys_cursor_width = x1 - w->phys_cursor.x;
24352
24353 if (row > w->current_matrix->rows
24354 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24355 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24356 OVERLAPS_ERASED_CURSOR);
24357
24358 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24359 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24360 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24361 OVERLAPS_ERASED_CURSOR);
24362 }
24363 }
24364 }
24365
24366
24367 /* EXPORT:
24368 Erase the image of a cursor of window W from the screen. */
24369
24370 void
24371 erase_phys_cursor (struct window *w)
24372 {
24373 struct frame *f = XFRAME (w->frame);
24374 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24375 int hpos = w->phys_cursor.hpos;
24376 int vpos = w->phys_cursor.vpos;
24377 int mouse_face_here_p = 0;
24378 struct glyph_matrix *active_glyphs = w->current_matrix;
24379 struct glyph_row *cursor_row;
24380 struct glyph *cursor_glyph;
24381 enum draw_glyphs_face hl;
24382
24383 /* No cursor displayed or row invalidated => nothing to do on the
24384 screen. */
24385 if (w->phys_cursor_type == NO_CURSOR)
24386 goto mark_cursor_off;
24387
24388 /* VPOS >= active_glyphs->nrows means that window has been resized.
24389 Don't bother to erase the cursor. */
24390 if (vpos >= active_glyphs->nrows)
24391 goto mark_cursor_off;
24392
24393 /* If row containing cursor is marked invalid, there is nothing we
24394 can do. */
24395 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24396 if (!cursor_row->enabled_p)
24397 goto mark_cursor_off;
24398
24399 /* If line spacing is > 0, old cursor may only be partially visible in
24400 window after split-window. So adjust visible height. */
24401 cursor_row->visible_height = min (cursor_row->visible_height,
24402 window_text_bottom_y (w) - cursor_row->y);
24403
24404 /* If row is completely invisible, don't attempt to delete a cursor which
24405 isn't there. This can happen if cursor is at top of a window, and
24406 we switch to a buffer with a header line in that window. */
24407 if (cursor_row->visible_height <= 0)
24408 goto mark_cursor_off;
24409
24410 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24411 if (cursor_row->cursor_in_fringe_p)
24412 {
24413 cursor_row->cursor_in_fringe_p = 0;
24414 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24415 goto mark_cursor_off;
24416 }
24417
24418 /* This can happen when the new row is shorter than the old one.
24419 In this case, either draw_glyphs or clear_end_of_line
24420 should have cleared the cursor. Note that we wouldn't be
24421 able to erase the cursor in this case because we don't have a
24422 cursor glyph at hand. */
24423 if ((cursor_row->reversed_p
24424 ? (w->phys_cursor.hpos < 0)
24425 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24426 goto mark_cursor_off;
24427
24428 /* If the cursor is in the mouse face area, redisplay that when
24429 we clear the cursor. */
24430 if (! NILP (hlinfo->mouse_face_window)
24431 && coords_in_mouse_face_p (w, hpos, vpos)
24432 /* Don't redraw the cursor's spot in mouse face if it is at the
24433 end of a line (on a newline). The cursor appears there, but
24434 mouse highlighting does not. */
24435 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24436 mouse_face_here_p = 1;
24437
24438 /* Maybe clear the display under the cursor. */
24439 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24440 {
24441 int x, y, left_x;
24442 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24443 int width;
24444
24445 cursor_glyph = get_phys_cursor_glyph (w);
24446 if (cursor_glyph == NULL)
24447 goto mark_cursor_off;
24448
24449 width = cursor_glyph->pixel_width;
24450 left_x = window_box_left_offset (w, TEXT_AREA);
24451 x = w->phys_cursor.x;
24452 if (x < left_x)
24453 width -= left_x - x;
24454 width = min (width, window_box_width (w, TEXT_AREA) - x);
24455 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24456 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24457
24458 if (width > 0)
24459 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24460 }
24461
24462 /* Erase the cursor by redrawing the character underneath it. */
24463 if (mouse_face_here_p)
24464 hl = DRAW_MOUSE_FACE;
24465 else
24466 hl = DRAW_NORMAL_TEXT;
24467 draw_phys_cursor_glyph (w, cursor_row, hl);
24468
24469 mark_cursor_off:
24470 w->phys_cursor_on_p = 0;
24471 w->phys_cursor_type = NO_CURSOR;
24472 }
24473
24474
24475 /* EXPORT:
24476 Display or clear cursor of window W. If ON is zero, clear the
24477 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24478 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24479
24480 void
24481 display_and_set_cursor (struct window *w, int on,
24482 int hpos, int vpos, int x, int y)
24483 {
24484 struct frame *f = XFRAME (w->frame);
24485 int new_cursor_type;
24486 int new_cursor_width;
24487 int active_cursor;
24488 struct glyph_row *glyph_row;
24489 struct glyph *glyph;
24490
24491 /* This is pointless on invisible frames, and dangerous on garbaged
24492 windows and frames; in the latter case, the frame or window may
24493 be in the midst of changing its size, and x and y may be off the
24494 window. */
24495 if (! FRAME_VISIBLE_P (f)
24496 || FRAME_GARBAGED_P (f)
24497 || vpos >= w->current_matrix->nrows
24498 || hpos >= w->current_matrix->matrix_w)
24499 return;
24500
24501 /* If cursor is off and we want it off, return quickly. */
24502 if (!on && !w->phys_cursor_on_p)
24503 return;
24504
24505 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24506 /* If cursor row is not enabled, we don't really know where to
24507 display the cursor. */
24508 if (!glyph_row->enabled_p)
24509 {
24510 w->phys_cursor_on_p = 0;
24511 return;
24512 }
24513
24514 glyph = NULL;
24515 if (!glyph_row->exact_window_width_line_p
24516 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24517 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24518
24519 xassert (interrupt_input_blocked);
24520
24521 /* Set new_cursor_type to the cursor we want to be displayed. */
24522 new_cursor_type = get_window_cursor_type (w, glyph,
24523 &new_cursor_width, &active_cursor);
24524
24525 /* If cursor is currently being shown and we don't want it to be or
24526 it is in the wrong place, or the cursor type is not what we want,
24527 erase it. */
24528 if (w->phys_cursor_on_p
24529 && (!on
24530 || w->phys_cursor.x != x
24531 || w->phys_cursor.y != y
24532 || new_cursor_type != w->phys_cursor_type
24533 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24534 && new_cursor_width != w->phys_cursor_width)))
24535 erase_phys_cursor (w);
24536
24537 /* Don't check phys_cursor_on_p here because that flag is only set
24538 to zero in some cases where we know that the cursor has been
24539 completely erased, to avoid the extra work of erasing the cursor
24540 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24541 still not be visible, or it has only been partly erased. */
24542 if (on)
24543 {
24544 w->phys_cursor_ascent = glyph_row->ascent;
24545 w->phys_cursor_height = glyph_row->height;
24546
24547 /* Set phys_cursor_.* before x_draw_.* is called because some
24548 of them may need the information. */
24549 w->phys_cursor.x = x;
24550 w->phys_cursor.y = glyph_row->y;
24551 w->phys_cursor.hpos = hpos;
24552 w->phys_cursor.vpos = vpos;
24553 }
24554
24555 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24556 new_cursor_type, new_cursor_width,
24557 on, active_cursor);
24558 }
24559
24560
24561 /* Switch the display of W's cursor on or off, according to the value
24562 of ON. */
24563
24564 static void
24565 update_window_cursor (struct window *w, int on)
24566 {
24567 /* Don't update cursor in windows whose frame is in the process
24568 of being deleted. */
24569 if (w->current_matrix)
24570 {
24571 BLOCK_INPUT;
24572 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24573 w->phys_cursor.x, w->phys_cursor.y);
24574 UNBLOCK_INPUT;
24575 }
24576 }
24577
24578
24579 /* Call update_window_cursor with parameter ON_P on all leaf windows
24580 in the window tree rooted at W. */
24581
24582 static void
24583 update_cursor_in_window_tree (struct window *w, int on_p)
24584 {
24585 while (w)
24586 {
24587 if (!NILP (w->hchild))
24588 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24589 else if (!NILP (w->vchild))
24590 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24591 else
24592 update_window_cursor (w, on_p);
24593
24594 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24595 }
24596 }
24597
24598
24599 /* EXPORT:
24600 Display the cursor on window W, or clear it, according to ON_P.
24601 Don't change the cursor's position. */
24602
24603 void
24604 x_update_cursor (struct frame *f, int on_p)
24605 {
24606 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24607 }
24608
24609
24610 /* EXPORT:
24611 Clear the cursor of window W to background color, and mark the
24612 cursor as not shown. This is used when the text where the cursor
24613 is about to be rewritten. */
24614
24615 void
24616 x_clear_cursor (struct window *w)
24617 {
24618 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24619 update_window_cursor (w, 0);
24620 }
24621
24622 #endif /* HAVE_WINDOW_SYSTEM */
24623
24624 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24625 and MSDOS. */
24626 static void
24627 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24628 int start_hpos, int end_hpos,
24629 enum draw_glyphs_face draw)
24630 {
24631 #ifdef HAVE_WINDOW_SYSTEM
24632 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24633 {
24634 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24635 return;
24636 }
24637 #endif
24638 #if defined (HAVE_GPM) || defined (MSDOS)
24639 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24640 #endif
24641 }
24642
24643 /* Display the active region described by mouse_face_* according to DRAW. */
24644
24645 static void
24646 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24647 {
24648 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24649 struct frame *f = XFRAME (WINDOW_FRAME (w));
24650
24651 if (/* If window is in the process of being destroyed, don't bother
24652 to do anything. */
24653 w->current_matrix != NULL
24654 /* Don't update mouse highlight if hidden */
24655 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24656 /* Recognize when we are called to operate on rows that don't exist
24657 anymore. This can happen when a window is split. */
24658 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24659 {
24660 int phys_cursor_on_p = w->phys_cursor_on_p;
24661 struct glyph_row *row, *first, *last;
24662
24663 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24664 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24665
24666 for (row = first; row <= last && row->enabled_p; ++row)
24667 {
24668 int start_hpos, end_hpos, start_x;
24669
24670 /* For all but the first row, the highlight starts at column 0. */
24671 if (row == first)
24672 {
24673 /* R2L rows have BEG and END in reversed order, but the
24674 screen drawing geometry is always left to right. So
24675 we need to mirror the beginning and end of the
24676 highlighted area in R2L rows. */
24677 if (!row->reversed_p)
24678 {
24679 start_hpos = hlinfo->mouse_face_beg_col;
24680 start_x = hlinfo->mouse_face_beg_x;
24681 }
24682 else if (row == last)
24683 {
24684 start_hpos = hlinfo->mouse_face_end_col;
24685 start_x = hlinfo->mouse_face_end_x;
24686 }
24687 else
24688 {
24689 start_hpos = 0;
24690 start_x = 0;
24691 }
24692 }
24693 else if (row->reversed_p && row == last)
24694 {
24695 start_hpos = hlinfo->mouse_face_end_col;
24696 start_x = hlinfo->mouse_face_end_x;
24697 }
24698 else
24699 {
24700 start_hpos = 0;
24701 start_x = 0;
24702 }
24703
24704 if (row == last)
24705 {
24706 if (!row->reversed_p)
24707 end_hpos = hlinfo->mouse_face_end_col;
24708 else if (row == first)
24709 end_hpos = hlinfo->mouse_face_beg_col;
24710 else
24711 {
24712 end_hpos = row->used[TEXT_AREA];
24713 if (draw == DRAW_NORMAL_TEXT)
24714 row->fill_line_p = 1; /* Clear to end of line */
24715 }
24716 }
24717 else if (row->reversed_p && row == first)
24718 end_hpos = hlinfo->mouse_face_beg_col;
24719 else
24720 {
24721 end_hpos = row->used[TEXT_AREA];
24722 if (draw == DRAW_NORMAL_TEXT)
24723 row->fill_line_p = 1; /* Clear to end of line */
24724 }
24725
24726 if (end_hpos > start_hpos)
24727 {
24728 draw_row_with_mouse_face (w, start_x, row,
24729 start_hpos, end_hpos, draw);
24730
24731 row->mouse_face_p
24732 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24733 }
24734 }
24735
24736 #ifdef HAVE_WINDOW_SYSTEM
24737 /* When we've written over the cursor, arrange for it to
24738 be displayed again. */
24739 if (FRAME_WINDOW_P (f)
24740 && phys_cursor_on_p && !w->phys_cursor_on_p)
24741 {
24742 BLOCK_INPUT;
24743 display_and_set_cursor (w, 1,
24744 w->phys_cursor.hpos, w->phys_cursor.vpos,
24745 w->phys_cursor.x, w->phys_cursor.y);
24746 UNBLOCK_INPUT;
24747 }
24748 #endif /* HAVE_WINDOW_SYSTEM */
24749 }
24750
24751 #ifdef HAVE_WINDOW_SYSTEM
24752 /* Change the mouse cursor. */
24753 if (FRAME_WINDOW_P (f))
24754 {
24755 if (draw == DRAW_NORMAL_TEXT
24756 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24757 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24758 else if (draw == DRAW_MOUSE_FACE)
24759 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24760 else
24761 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24762 }
24763 #endif /* HAVE_WINDOW_SYSTEM */
24764 }
24765
24766 /* EXPORT:
24767 Clear out the mouse-highlighted active region.
24768 Redraw it un-highlighted first. Value is non-zero if mouse
24769 face was actually drawn unhighlighted. */
24770
24771 int
24772 clear_mouse_face (Mouse_HLInfo *hlinfo)
24773 {
24774 int cleared = 0;
24775
24776 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24777 {
24778 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24779 cleared = 1;
24780 }
24781
24782 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24783 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24784 hlinfo->mouse_face_window = Qnil;
24785 hlinfo->mouse_face_overlay = Qnil;
24786 return cleared;
24787 }
24788
24789 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24790 within the mouse face on that window. */
24791 static int
24792 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24793 {
24794 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24795
24796 /* Quickly resolve the easy cases. */
24797 if (!(WINDOWP (hlinfo->mouse_face_window)
24798 && XWINDOW (hlinfo->mouse_face_window) == w))
24799 return 0;
24800 if (vpos < hlinfo->mouse_face_beg_row
24801 || vpos > hlinfo->mouse_face_end_row)
24802 return 0;
24803 if (vpos > hlinfo->mouse_face_beg_row
24804 && vpos < hlinfo->mouse_face_end_row)
24805 return 1;
24806
24807 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24808 {
24809 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24810 {
24811 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24812 return 1;
24813 }
24814 else if ((vpos == hlinfo->mouse_face_beg_row
24815 && hpos >= hlinfo->mouse_face_beg_col)
24816 || (vpos == hlinfo->mouse_face_end_row
24817 && hpos < hlinfo->mouse_face_end_col))
24818 return 1;
24819 }
24820 else
24821 {
24822 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24823 {
24824 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24825 return 1;
24826 }
24827 else if ((vpos == hlinfo->mouse_face_beg_row
24828 && hpos <= hlinfo->mouse_face_beg_col)
24829 || (vpos == hlinfo->mouse_face_end_row
24830 && hpos > hlinfo->mouse_face_end_col))
24831 return 1;
24832 }
24833 return 0;
24834 }
24835
24836
24837 /* EXPORT:
24838 Non-zero if physical cursor of window W is within mouse face. */
24839
24840 int
24841 cursor_in_mouse_face_p (struct window *w)
24842 {
24843 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24844 }
24845
24846
24847 \f
24848 /* Find the glyph rows START_ROW and END_ROW of window W that display
24849 characters between buffer positions START_CHARPOS and END_CHARPOS
24850 (excluding END_CHARPOS). This is similar to row_containing_pos,
24851 but is more accurate when bidi reordering makes buffer positions
24852 change non-linearly with glyph rows. */
24853 static void
24854 rows_from_pos_range (struct window *w,
24855 EMACS_INT start_charpos, EMACS_INT end_charpos,
24856 struct glyph_row **start, struct glyph_row **end)
24857 {
24858 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24859 int last_y = window_text_bottom_y (w);
24860 struct glyph_row *row;
24861
24862 *start = NULL;
24863 *end = NULL;
24864
24865 while (!first->enabled_p
24866 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24867 first++;
24868
24869 /* Find the START row. */
24870 for (row = first;
24871 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24872 row++)
24873 {
24874 /* A row can potentially be the START row if the range of the
24875 characters it displays intersects the range
24876 [START_CHARPOS..END_CHARPOS). */
24877 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24878 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24879 /* See the commentary in row_containing_pos, for the
24880 explanation of the complicated way to check whether
24881 some position is beyond the end of the characters
24882 displayed by a row. */
24883 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24884 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24885 && !row->ends_at_zv_p
24886 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24887 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24888 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24889 && !row->ends_at_zv_p
24890 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24891 {
24892 /* Found a candidate row. Now make sure at least one of the
24893 glyphs it displays has a charpos from the range
24894 [START_CHARPOS..END_CHARPOS).
24895
24896 This is not obvious because bidi reordering could make
24897 buffer positions of a row be 1,2,3,102,101,100, and if we
24898 want to highlight characters in [50..60), we don't want
24899 this row, even though [50..60) does intersect [1..103),
24900 the range of character positions given by the row's start
24901 and end positions. */
24902 struct glyph *g = row->glyphs[TEXT_AREA];
24903 struct glyph *e = g + row->used[TEXT_AREA];
24904
24905 while (g < e)
24906 {
24907 if (BUFFERP (g->object)
24908 && start_charpos <= g->charpos && g->charpos < end_charpos)
24909 *start = row;
24910 g++;
24911 }
24912 if (*start)
24913 break;
24914 }
24915 }
24916
24917 /* Find the END row. */
24918 if (!*start
24919 /* If the last row is partially visible, start looking for END
24920 from that row, instead of starting from FIRST. */
24921 && !(row->enabled_p
24922 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24923 row = first;
24924 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24925 {
24926 struct glyph_row *next = row + 1;
24927
24928 if (!next->enabled_p
24929 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24930 /* The first row >= START whose range of displayed characters
24931 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24932 is the row END + 1. */
24933 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24934 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24935 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24936 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24937 && !next->ends_at_zv_p
24938 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24939 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24940 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24941 && !next->ends_at_zv_p
24942 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24943 {
24944 *end = row;
24945 break;
24946 }
24947 else
24948 {
24949 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24950 but none of the characters it displays are in the range, it is
24951 also END + 1. */
24952 struct glyph *g = next->glyphs[TEXT_AREA];
24953 struct glyph *e = g + next->used[TEXT_AREA];
24954
24955 while (g < e)
24956 {
24957 if (BUFFERP (g->object)
24958 && start_charpos <= g->charpos && g->charpos < end_charpos)
24959 break;
24960 g++;
24961 }
24962 if (g == e)
24963 {
24964 *end = row;
24965 break;
24966 }
24967 }
24968 }
24969 }
24970
24971 /* This function sets the mouse_face_* elements of HLINFO, assuming
24972 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24973 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24974 for the overlay or run of text properties specifying the mouse
24975 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24976 before-string and after-string that must also be highlighted.
24977 COVER_STRING, if non-nil, is a display string that may cover some
24978 or all of the highlighted text. */
24979
24980 static void
24981 mouse_face_from_buffer_pos (Lisp_Object window,
24982 Mouse_HLInfo *hlinfo,
24983 EMACS_INT mouse_charpos,
24984 EMACS_INT start_charpos,
24985 EMACS_INT end_charpos,
24986 Lisp_Object before_string,
24987 Lisp_Object after_string,
24988 Lisp_Object cover_string)
24989 {
24990 struct window *w = XWINDOW (window);
24991 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24992 struct glyph_row *r1, *r2;
24993 struct glyph *glyph, *end;
24994 EMACS_INT ignore, pos;
24995 int x;
24996
24997 xassert (NILP (cover_string) || STRINGP (cover_string));
24998 xassert (NILP (before_string) || STRINGP (before_string));
24999 xassert (NILP (after_string) || STRINGP (after_string));
25000
25001 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25002 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25003 if (r1 == NULL)
25004 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25005 /* If the before-string or display-string contains newlines,
25006 rows_from_pos_range skips to its last row. Move back. */
25007 if (!NILP (before_string) || !NILP (cover_string))
25008 {
25009 struct glyph_row *prev;
25010 while ((prev = r1 - 1, prev >= first)
25011 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25012 && prev->used[TEXT_AREA] > 0)
25013 {
25014 struct glyph *beg = prev->glyphs[TEXT_AREA];
25015 glyph = beg + prev->used[TEXT_AREA];
25016 while (--glyph >= beg && INTEGERP (glyph->object));
25017 if (glyph < beg
25018 || !(EQ (glyph->object, before_string)
25019 || EQ (glyph->object, cover_string)))
25020 break;
25021 r1 = prev;
25022 }
25023 }
25024 if (r2 == NULL)
25025 {
25026 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25027 hlinfo->mouse_face_past_end = 1;
25028 }
25029 else if (!NILP (after_string))
25030 {
25031 /* If the after-string has newlines, advance to its last row. */
25032 struct glyph_row *next;
25033 struct glyph_row *last
25034 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25035
25036 for (next = r2 + 1;
25037 next <= last
25038 && next->used[TEXT_AREA] > 0
25039 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25040 ++next)
25041 r2 = next;
25042 }
25043 /* The rest of the display engine assumes that mouse_face_beg_row is
25044 either above below mouse_face_end_row or identical to it. But
25045 with bidi-reordered continued lines, the row for START_CHARPOS
25046 could be below the row for END_CHARPOS. If so, swap the rows and
25047 store them in correct order. */
25048 if (r1->y > r2->y)
25049 {
25050 struct glyph_row *tem = r2;
25051
25052 r2 = r1;
25053 r1 = tem;
25054 }
25055
25056 hlinfo->mouse_face_beg_y = r1->y;
25057 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25058 hlinfo->mouse_face_end_y = r2->y;
25059 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25060
25061 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25062 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25063 could be anywhere in the row and in any order. The strategy
25064 below is to find the leftmost and the rightmost glyph that
25065 belongs to either of these 3 strings, or whose position is
25066 between START_CHARPOS and END_CHARPOS, and highlight all the
25067 glyphs between those two. This may cover more than just the text
25068 between START_CHARPOS and END_CHARPOS if the range of characters
25069 strides the bidi level boundary, e.g. if the beginning is in R2L
25070 text while the end is in L2R text or vice versa. */
25071 if (!r1->reversed_p)
25072 {
25073 /* This row is in a left to right paragraph. Scan it left to
25074 right. */
25075 glyph = r1->glyphs[TEXT_AREA];
25076 end = glyph + r1->used[TEXT_AREA];
25077 x = r1->x;
25078
25079 /* Skip truncation glyphs at the start of the glyph row. */
25080 if (r1->displays_text_p)
25081 for (; glyph < end
25082 && INTEGERP (glyph->object)
25083 && glyph->charpos < 0;
25084 ++glyph)
25085 x += glyph->pixel_width;
25086
25087 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25088 or COVER_STRING, and the first glyph from buffer whose
25089 position is between START_CHARPOS and END_CHARPOS. */
25090 for (; glyph < end
25091 && !INTEGERP (glyph->object)
25092 && !EQ (glyph->object, cover_string)
25093 && !(BUFFERP (glyph->object)
25094 && (glyph->charpos >= start_charpos
25095 && glyph->charpos < end_charpos));
25096 ++glyph)
25097 {
25098 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25099 are present at buffer positions between START_CHARPOS and
25100 END_CHARPOS, or if they come from an overlay. */
25101 if (EQ (glyph->object, before_string))
25102 {
25103 pos = string_buffer_position (before_string,
25104 start_charpos);
25105 /* If pos == 0, it means before_string came from an
25106 overlay, not from a buffer position. */
25107 if (!pos || (pos >= start_charpos && pos < end_charpos))
25108 break;
25109 }
25110 else if (EQ (glyph->object, after_string))
25111 {
25112 pos = string_buffer_position (after_string, end_charpos);
25113 if (!pos || (pos >= start_charpos && pos < end_charpos))
25114 break;
25115 }
25116 x += glyph->pixel_width;
25117 }
25118 hlinfo->mouse_face_beg_x = x;
25119 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25120 }
25121 else
25122 {
25123 /* This row is in a right to left paragraph. Scan it right to
25124 left. */
25125 struct glyph *g;
25126
25127 end = r1->glyphs[TEXT_AREA] - 1;
25128 glyph = end + r1->used[TEXT_AREA];
25129
25130 /* Skip truncation glyphs at the start of the glyph row. */
25131 if (r1->displays_text_p)
25132 for (; glyph > end
25133 && INTEGERP (glyph->object)
25134 && glyph->charpos < 0;
25135 --glyph)
25136 ;
25137
25138 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25139 or COVER_STRING, and the first glyph from buffer whose
25140 position is between START_CHARPOS and END_CHARPOS. */
25141 for (; glyph > end
25142 && !INTEGERP (glyph->object)
25143 && !EQ (glyph->object, cover_string)
25144 && !(BUFFERP (glyph->object)
25145 && (glyph->charpos >= start_charpos
25146 && glyph->charpos < end_charpos));
25147 --glyph)
25148 {
25149 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25150 are present at buffer positions between START_CHARPOS and
25151 END_CHARPOS, or if they come from an overlay. */
25152 if (EQ (glyph->object, before_string))
25153 {
25154 pos = string_buffer_position (before_string, start_charpos);
25155 /* If pos == 0, it means before_string came from an
25156 overlay, not from a buffer position. */
25157 if (!pos || (pos >= start_charpos && pos < end_charpos))
25158 break;
25159 }
25160 else if (EQ (glyph->object, after_string))
25161 {
25162 pos = string_buffer_position (after_string, end_charpos);
25163 if (!pos || (pos >= start_charpos && pos < end_charpos))
25164 break;
25165 }
25166 }
25167
25168 glyph++; /* first glyph to the right of the highlighted area */
25169 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25170 x += g->pixel_width;
25171 hlinfo->mouse_face_beg_x = x;
25172 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25173 }
25174
25175 /* If the highlight ends in a different row, compute GLYPH and END
25176 for the end row. Otherwise, reuse the values computed above for
25177 the row where the highlight begins. */
25178 if (r2 != r1)
25179 {
25180 if (!r2->reversed_p)
25181 {
25182 glyph = r2->glyphs[TEXT_AREA];
25183 end = glyph + r2->used[TEXT_AREA];
25184 x = r2->x;
25185 }
25186 else
25187 {
25188 end = r2->glyphs[TEXT_AREA] - 1;
25189 glyph = end + r2->used[TEXT_AREA];
25190 }
25191 }
25192
25193 if (!r2->reversed_p)
25194 {
25195 /* Skip truncation and continuation glyphs near the end of the
25196 row, and also blanks and stretch glyphs inserted by
25197 extend_face_to_end_of_line. */
25198 while (end > glyph
25199 && INTEGERP ((end - 1)->object)
25200 && (end - 1)->charpos <= 0)
25201 --end;
25202 /* Scan the rest of the glyph row from the end, looking for the
25203 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25204 COVER_STRING, or whose position is between START_CHARPOS
25205 and END_CHARPOS */
25206 for (--end;
25207 end > glyph
25208 && !INTEGERP (end->object)
25209 && !EQ (end->object, cover_string)
25210 && !(BUFFERP (end->object)
25211 && (end->charpos >= start_charpos
25212 && end->charpos < end_charpos));
25213 --end)
25214 {
25215 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25216 are present at buffer positions between START_CHARPOS and
25217 END_CHARPOS, or if they come from an overlay. */
25218 if (EQ (end->object, before_string))
25219 {
25220 pos = string_buffer_position (before_string, start_charpos);
25221 if (!pos || (pos >= start_charpos && pos < end_charpos))
25222 break;
25223 }
25224 else if (EQ (end->object, after_string))
25225 {
25226 pos = string_buffer_position (after_string, end_charpos);
25227 if (!pos || (pos >= start_charpos && pos < end_charpos))
25228 break;
25229 }
25230 }
25231 /* Find the X coordinate of the last glyph to be highlighted. */
25232 for (; glyph <= end; ++glyph)
25233 x += glyph->pixel_width;
25234
25235 hlinfo->mouse_face_end_x = x;
25236 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25237 }
25238 else
25239 {
25240 /* Skip truncation and continuation glyphs near the end of the
25241 row, and also blanks and stretch glyphs inserted by
25242 extend_face_to_end_of_line. */
25243 x = r2->x;
25244 end++;
25245 while (end < glyph
25246 && INTEGERP (end->object)
25247 && end->charpos <= 0)
25248 {
25249 x += end->pixel_width;
25250 ++end;
25251 }
25252 /* Scan the rest of the glyph row from the end, looking for the
25253 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25254 COVER_STRING, or whose position is between START_CHARPOS
25255 and END_CHARPOS */
25256 for ( ;
25257 end < glyph
25258 && !INTEGERP (end->object)
25259 && !EQ (end->object, cover_string)
25260 && !(BUFFERP (end->object)
25261 && (end->charpos >= start_charpos
25262 && end->charpos < end_charpos));
25263 ++end)
25264 {
25265 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25266 are present at buffer positions between START_CHARPOS and
25267 END_CHARPOS, or if they come from an overlay. */
25268 if (EQ (end->object, before_string))
25269 {
25270 pos = string_buffer_position (before_string, start_charpos);
25271 if (!pos || (pos >= start_charpos && pos < end_charpos))
25272 break;
25273 }
25274 else if (EQ (end->object, after_string))
25275 {
25276 pos = string_buffer_position (after_string, end_charpos);
25277 if (!pos || (pos >= start_charpos && pos < end_charpos))
25278 break;
25279 }
25280 x += end->pixel_width;
25281 }
25282 hlinfo->mouse_face_end_x = x;
25283 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25284 }
25285
25286 hlinfo->mouse_face_window = window;
25287 hlinfo->mouse_face_face_id
25288 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25289 mouse_charpos + 1,
25290 !hlinfo->mouse_face_hidden, -1);
25291 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25292 }
25293
25294 /* The following function is not used anymore (replaced with
25295 mouse_face_from_string_pos), but I leave it here for the time
25296 being, in case someone would. */
25297
25298 #if 0 /* not used */
25299
25300 /* Find the position of the glyph for position POS in OBJECT in
25301 window W's current matrix, and return in *X, *Y the pixel
25302 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25303
25304 RIGHT_P non-zero means return the position of the right edge of the
25305 glyph, RIGHT_P zero means return the left edge position.
25306
25307 If no glyph for POS exists in the matrix, return the position of
25308 the glyph with the next smaller position that is in the matrix, if
25309 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25310 exists in the matrix, return the position of the glyph with the
25311 next larger position in OBJECT.
25312
25313 Value is non-zero if a glyph was found. */
25314
25315 static int
25316 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25317 int *hpos, int *vpos, int *x, int *y, int right_p)
25318 {
25319 int yb = window_text_bottom_y (w);
25320 struct glyph_row *r;
25321 struct glyph *best_glyph = NULL;
25322 struct glyph_row *best_row = NULL;
25323 int best_x = 0;
25324
25325 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25326 r->enabled_p && r->y < yb;
25327 ++r)
25328 {
25329 struct glyph *g = r->glyphs[TEXT_AREA];
25330 struct glyph *e = g + r->used[TEXT_AREA];
25331 int gx;
25332
25333 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25334 if (EQ (g->object, object))
25335 {
25336 if (g->charpos == pos)
25337 {
25338 best_glyph = g;
25339 best_x = gx;
25340 best_row = r;
25341 goto found;
25342 }
25343 else if (best_glyph == NULL
25344 || ((eabs (g->charpos - pos)
25345 < eabs (best_glyph->charpos - pos))
25346 && (right_p
25347 ? g->charpos < pos
25348 : g->charpos > pos)))
25349 {
25350 best_glyph = g;
25351 best_x = gx;
25352 best_row = r;
25353 }
25354 }
25355 }
25356
25357 found:
25358
25359 if (best_glyph)
25360 {
25361 *x = best_x;
25362 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25363
25364 if (right_p)
25365 {
25366 *x += best_glyph->pixel_width;
25367 ++*hpos;
25368 }
25369
25370 *y = best_row->y;
25371 *vpos = best_row - w->current_matrix->rows;
25372 }
25373
25374 return best_glyph != NULL;
25375 }
25376 #endif /* not used */
25377
25378 /* Find the positions of the first and the last glyphs in window W's
25379 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25380 (assumed to be a string), and return in HLINFO's mouse_face_*
25381 members the pixel and column/row coordinates of those glyphs. */
25382
25383 static void
25384 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25385 Lisp_Object object,
25386 EMACS_INT startpos, EMACS_INT endpos)
25387 {
25388 int yb = window_text_bottom_y (w);
25389 struct glyph_row *r;
25390 struct glyph *g, *e;
25391 int gx;
25392 int found = 0;
25393
25394 /* Find the glyph row with at least one position in the range
25395 [STARTPOS..ENDPOS], and the first glyph in that row whose
25396 position belongs to that range. */
25397 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25398 r->enabled_p && r->y < yb;
25399 ++r)
25400 {
25401 if (!r->reversed_p)
25402 {
25403 g = r->glyphs[TEXT_AREA];
25404 e = g + r->used[TEXT_AREA];
25405 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25406 if (EQ (g->object, object)
25407 && startpos <= g->charpos && g->charpos <= endpos)
25408 {
25409 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25410 hlinfo->mouse_face_beg_y = r->y;
25411 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25412 hlinfo->mouse_face_beg_x = gx;
25413 found = 1;
25414 break;
25415 }
25416 }
25417 else
25418 {
25419 struct glyph *g1;
25420
25421 e = r->glyphs[TEXT_AREA];
25422 g = e + r->used[TEXT_AREA];
25423 for ( ; g > e; --g)
25424 if (EQ ((g-1)->object, object)
25425 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25426 {
25427 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25428 hlinfo->mouse_face_beg_y = r->y;
25429 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25430 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25431 gx += g1->pixel_width;
25432 hlinfo->mouse_face_beg_x = gx;
25433 found = 1;
25434 break;
25435 }
25436 }
25437 if (found)
25438 break;
25439 }
25440
25441 if (!found)
25442 return;
25443
25444 /* Starting with the next row, look for the first row which does NOT
25445 include any glyphs whose positions are in the range. */
25446 for (++r; r->enabled_p && r->y < yb; ++r)
25447 {
25448 g = r->glyphs[TEXT_AREA];
25449 e = g + r->used[TEXT_AREA];
25450 found = 0;
25451 for ( ; g < e; ++g)
25452 if (EQ (g->object, object)
25453 && startpos <= g->charpos && g->charpos <= endpos)
25454 {
25455 found = 1;
25456 break;
25457 }
25458 if (!found)
25459 break;
25460 }
25461
25462 /* The highlighted region ends on the previous row. */
25463 r--;
25464
25465 /* Set the end row and its vertical pixel coordinate. */
25466 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25467 hlinfo->mouse_face_end_y = r->y;
25468
25469 /* Compute and set the end column and the end column's horizontal
25470 pixel coordinate. */
25471 if (!r->reversed_p)
25472 {
25473 g = r->glyphs[TEXT_AREA];
25474 e = g + r->used[TEXT_AREA];
25475 for ( ; e > g; --e)
25476 if (EQ ((e-1)->object, object)
25477 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25478 break;
25479 hlinfo->mouse_face_end_col = e - g;
25480
25481 for (gx = r->x; g < e; ++g)
25482 gx += g->pixel_width;
25483 hlinfo->mouse_face_end_x = gx;
25484 }
25485 else
25486 {
25487 e = r->glyphs[TEXT_AREA];
25488 g = e + r->used[TEXT_AREA];
25489 for (gx = r->x ; e < g; ++e)
25490 {
25491 if (EQ (e->object, object)
25492 && startpos <= e->charpos && e->charpos <= endpos)
25493 break;
25494 gx += e->pixel_width;
25495 }
25496 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25497 hlinfo->mouse_face_end_x = gx;
25498 }
25499 }
25500
25501 #ifdef HAVE_WINDOW_SYSTEM
25502
25503 /* See if position X, Y is within a hot-spot of an image. */
25504
25505 static int
25506 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25507 {
25508 if (!CONSP (hot_spot))
25509 return 0;
25510
25511 if (EQ (XCAR (hot_spot), Qrect))
25512 {
25513 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25514 Lisp_Object rect = XCDR (hot_spot);
25515 Lisp_Object tem;
25516 if (!CONSP (rect))
25517 return 0;
25518 if (!CONSP (XCAR (rect)))
25519 return 0;
25520 if (!CONSP (XCDR (rect)))
25521 return 0;
25522 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25523 return 0;
25524 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25525 return 0;
25526 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25527 return 0;
25528 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25529 return 0;
25530 return 1;
25531 }
25532 else if (EQ (XCAR (hot_spot), Qcircle))
25533 {
25534 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25535 Lisp_Object circ = XCDR (hot_spot);
25536 Lisp_Object lr, lx0, ly0;
25537 if (CONSP (circ)
25538 && CONSP (XCAR (circ))
25539 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25540 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25541 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25542 {
25543 double r = XFLOATINT (lr);
25544 double dx = XINT (lx0) - x;
25545 double dy = XINT (ly0) - y;
25546 return (dx * dx + dy * dy <= r * r);
25547 }
25548 }
25549 else if (EQ (XCAR (hot_spot), Qpoly))
25550 {
25551 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25552 if (VECTORP (XCDR (hot_spot)))
25553 {
25554 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25555 Lisp_Object *poly = v->contents;
25556 int n = v->header.size;
25557 int i;
25558 int inside = 0;
25559 Lisp_Object lx, ly;
25560 int x0, y0;
25561
25562 /* Need an even number of coordinates, and at least 3 edges. */
25563 if (n < 6 || n & 1)
25564 return 0;
25565
25566 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25567 If count is odd, we are inside polygon. Pixels on edges
25568 may or may not be included depending on actual geometry of the
25569 polygon. */
25570 if ((lx = poly[n-2], !INTEGERP (lx))
25571 || (ly = poly[n-1], !INTEGERP (lx)))
25572 return 0;
25573 x0 = XINT (lx), y0 = XINT (ly);
25574 for (i = 0; i < n; i += 2)
25575 {
25576 int x1 = x0, y1 = y0;
25577 if ((lx = poly[i], !INTEGERP (lx))
25578 || (ly = poly[i+1], !INTEGERP (ly)))
25579 return 0;
25580 x0 = XINT (lx), y0 = XINT (ly);
25581
25582 /* Does this segment cross the X line? */
25583 if (x0 >= x)
25584 {
25585 if (x1 >= x)
25586 continue;
25587 }
25588 else if (x1 < x)
25589 continue;
25590 if (y > y0 && y > y1)
25591 continue;
25592 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25593 inside = !inside;
25594 }
25595 return inside;
25596 }
25597 }
25598 return 0;
25599 }
25600
25601 Lisp_Object
25602 find_hot_spot (Lisp_Object map, int x, int y)
25603 {
25604 while (CONSP (map))
25605 {
25606 if (CONSP (XCAR (map))
25607 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25608 return XCAR (map);
25609 map = XCDR (map);
25610 }
25611
25612 return Qnil;
25613 }
25614
25615 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25616 3, 3, 0,
25617 doc: /* Lookup in image map MAP coordinates X and Y.
25618 An image map is an alist where each element has the format (AREA ID PLIST).
25619 An AREA is specified as either a rectangle, a circle, or a polygon:
25620 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25621 pixel coordinates of the upper left and bottom right corners.
25622 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25623 and the radius of the circle; r may be a float or integer.
25624 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25625 vector describes one corner in the polygon.
25626 Returns the alist element for the first matching AREA in MAP. */)
25627 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25628 {
25629 if (NILP (map))
25630 return Qnil;
25631
25632 CHECK_NUMBER (x);
25633 CHECK_NUMBER (y);
25634
25635 return find_hot_spot (map, XINT (x), XINT (y));
25636 }
25637
25638
25639 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25640 static void
25641 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25642 {
25643 /* Do not change cursor shape while dragging mouse. */
25644 if (!NILP (do_mouse_tracking))
25645 return;
25646
25647 if (!NILP (pointer))
25648 {
25649 if (EQ (pointer, Qarrow))
25650 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25651 else if (EQ (pointer, Qhand))
25652 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25653 else if (EQ (pointer, Qtext))
25654 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25655 else if (EQ (pointer, intern ("hdrag")))
25656 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25657 #ifdef HAVE_X_WINDOWS
25658 else if (EQ (pointer, intern ("vdrag")))
25659 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25660 #endif
25661 else if (EQ (pointer, intern ("hourglass")))
25662 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25663 else if (EQ (pointer, Qmodeline))
25664 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25665 else
25666 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25667 }
25668
25669 if (cursor != No_Cursor)
25670 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25671 }
25672
25673 #endif /* HAVE_WINDOW_SYSTEM */
25674
25675 /* Take proper action when mouse has moved to the mode or header line
25676 or marginal area AREA of window W, x-position X and y-position Y.
25677 X is relative to the start of the text display area of W, so the
25678 width of bitmap areas and scroll bars must be subtracted to get a
25679 position relative to the start of the mode line. */
25680
25681 static void
25682 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25683 enum window_part area)
25684 {
25685 struct window *w = XWINDOW (window);
25686 struct frame *f = XFRAME (w->frame);
25687 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25688 #ifdef HAVE_WINDOW_SYSTEM
25689 Display_Info *dpyinfo;
25690 #endif
25691 Cursor cursor = No_Cursor;
25692 Lisp_Object pointer = Qnil;
25693 int dx, dy, width, height;
25694 EMACS_INT charpos;
25695 Lisp_Object string, object = Qnil;
25696 Lisp_Object pos, help;
25697
25698 Lisp_Object mouse_face;
25699 int original_x_pixel = x;
25700 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25701 struct glyph_row *row;
25702
25703 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25704 {
25705 int x0;
25706 struct glyph *end;
25707
25708 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25709 returns them in row/column units! */
25710 string = mode_line_string (w, area, &x, &y, &charpos,
25711 &object, &dx, &dy, &width, &height);
25712
25713 row = (area == ON_MODE_LINE
25714 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25715 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25716
25717 /* Find the glyph under the mouse pointer. */
25718 if (row->mode_line_p && row->enabled_p)
25719 {
25720 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25721 end = glyph + row->used[TEXT_AREA];
25722
25723 for (x0 = original_x_pixel;
25724 glyph < end && x0 >= glyph->pixel_width;
25725 ++glyph)
25726 x0 -= glyph->pixel_width;
25727
25728 if (glyph >= end)
25729 glyph = NULL;
25730 }
25731 }
25732 else
25733 {
25734 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25735 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25736 returns them in row/column units! */
25737 string = marginal_area_string (w, area, &x, &y, &charpos,
25738 &object, &dx, &dy, &width, &height);
25739 }
25740
25741 help = Qnil;
25742
25743 #ifdef HAVE_WINDOW_SYSTEM
25744 if (IMAGEP (object))
25745 {
25746 Lisp_Object image_map, hotspot;
25747 if ((image_map = Fplist_get (XCDR (object), QCmap),
25748 !NILP (image_map))
25749 && (hotspot = find_hot_spot (image_map, dx, dy),
25750 CONSP (hotspot))
25751 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25752 {
25753 Lisp_Object plist;
25754
25755 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25756 If so, we could look for mouse-enter, mouse-leave
25757 properties in PLIST (and do something...). */
25758 hotspot = XCDR (hotspot);
25759 if (CONSP (hotspot)
25760 && (plist = XCAR (hotspot), CONSP (plist)))
25761 {
25762 pointer = Fplist_get (plist, Qpointer);
25763 if (NILP (pointer))
25764 pointer = Qhand;
25765 help = Fplist_get (plist, Qhelp_echo);
25766 if (!NILP (help))
25767 {
25768 help_echo_string = help;
25769 /* Is this correct? ++kfs */
25770 XSETWINDOW (help_echo_window, w);
25771 help_echo_object = w->buffer;
25772 help_echo_pos = charpos;
25773 }
25774 }
25775 }
25776 if (NILP (pointer))
25777 pointer = Fplist_get (XCDR (object), QCpointer);
25778 }
25779 #endif /* HAVE_WINDOW_SYSTEM */
25780
25781 if (STRINGP (string))
25782 {
25783 pos = make_number (charpos);
25784 /* If we're on a string with `help-echo' text property, arrange
25785 for the help to be displayed. This is done by setting the
25786 global variable help_echo_string to the help string. */
25787 if (NILP (help))
25788 {
25789 help = Fget_text_property (pos, Qhelp_echo, string);
25790 if (!NILP (help))
25791 {
25792 help_echo_string = help;
25793 XSETWINDOW (help_echo_window, w);
25794 help_echo_object = string;
25795 help_echo_pos = charpos;
25796 }
25797 }
25798
25799 #ifdef HAVE_WINDOW_SYSTEM
25800 if (FRAME_WINDOW_P (f))
25801 {
25802 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25803 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25804 if (NILP (pointer))
25805 pointer = Fget_text_property (pos, Qpointer, string);
25806
25807 /* Change the mouse pointer according to what is under X/Y. */
25808 if (NILP (pointer)
25809 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25810 {
25811 Lisp_Object map;
25812 map = Fget_text_property (pos, Qlocal_map, string);
25813 if (!KEYMAPP (map))
25814 map = Fget_text_property (pos, Qkeymap, string);
25815 if (!KEYMAPP (map))
25816 cursor = dpyinfo->vertical_scroll_bar_cursor;
25817 }
25818 }
25819 #endif
25820
25821 /* Change the mouse face according to what is under X/Y. */
25822 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25823 if (!NILP (mouse_face)
25824 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25825 && glyph)
25826 {
25827 Lisp_Object b, e;
25828
25829 struct glyph * tmp_glyph;
25830
25831 int gpos;
25832 int gseq_length;
25833 int total_pixel_width;
25834 EMACS_INT begpos, endpos, ignore;
25835
25836 int vpos, hpos;
25837
25838 b = Fprevious_single_property_change (make_number (charpos + 1),
25839 Qmouse_face, string, Qnil);
25840 if (NILP (b))
25841 begpos = 0;
25842 else
25843 begpos = XINT (b);
25844
25845 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25846 if (NILP (e))
25847 endpos = SCHARS (string);
25848 else
25849 endpos = XINT (e);
25850
25851 /* Calculate the glyph position GPOS of GLYPH in the
25852 displayed string, relative to the beginning of the
25853 highlighted part of the string.
25854
25855 Note: GPOS is different from CHARPOS. CHARPOS is the
25856 position of GLYPH in the internal string object. A mode
25857 line string format has structures which are converted to
25858 a flattened string by the Emacs Lisp interpreter. The
25859 internal string is an element of those structures. The
25860 displayed string is the flattened string. */
25861 tmp_glyph = row_start_glyph;
25862 while (tmp_glyph < glyph
25863 && (!(EQ (tmp_glyph->object, glyph->object)
25864 && begpos <= tmp_glyph->charpos
25865 && tmp_glyph->charpos < endpos)))
25866 tmp_glyph++;
25867 gpos = glyph - tmp_glyph;
25868
25869 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25870 the highlighted part of the displayed string to which
25871 GLYPH belongs. Note: GSEQ_LENGTH is different from
25872 SCHARS (STRING), because the latter returns the length of
25873 the internal string. */
25874 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25875 tmp_glyph > glyph
25876 && (!(EQ (tmp_glyph->object, glyph->object)
25877 && begpos <= tmp_glyph->charpos
25878 && tmp_glyph->charpos < endpos));
25879 tmp_glyph--)
25880 ;
25881 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25882
25883 /* Calculate the total pixel width of all the glyphs between
25884 the beginning of the highlighted area and GLYPH. */
25885 total_pixel_width = 0;
25886 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25887 total_pixel_width += tmp_glyph->pixel_width;
25888
25889 /* Pre calculation of re-rendering position. Note: X is in
25890 column units here, after the call to mode_line_string or
25891 marginal_area_string. */
25892 hpos = x - gpos;
25893 vpos = (area == ON_MODE_LINE
25894 ? (w->current_matrix)->nrows - 1
25895 : 0);
25896
25897 /* If GLYPH's position is included in the region that is
25898 already drawn in mouse face, we have nothing to do. */
25899 if ( EQ (window, hlinfo->mouse_face_window)
25900 && (!row->reversed_p
25901 ? (hlinfo->mouse_face_beg_col <= hpos
25902 && hpos < hlinfo->mouse_face_end_col)
25903 /* In R2L rows we swap BEG and END, see below. */
25904 : (hlinfo->mouse_face_end_col <= hpos
25905 && hpos < hlinfo->mouse_face_beg_col))
25906 && hlinfo->mouse_face_beg_row == vpos )
25907 return;
25908
25909 if (clear_mouse_face (hlinfo))
25910 cursor = No_Cursor;
25911
25912 if (!row->reversed_p)
25913 {
25914 hlinfo->mouse_face_beg_col = hpos;
25915 hlinfo->mouse_face_beg_x = original_x_pixel
25916 - (total_pixel_width + dx);
25917 hlinfo->mouse_face_end_col = hpos + gseq_length;
25918 hlinfo->mouse_face_end_x = 0;
25919 }
25920 else
25921 {
25922 /* In R2L rows, show_mouse_face expects BEG and END
25923 coordinates to be swapped. */
25924 hlinfo->mouse_face_end_col = hpos;
25925 hlinfo->mouse_face_end_x = original_x_pixel
25926 - (total_pixel_width + dx);
25927 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25928 hlinfo->mouse_face_beg_x = 0;
25929 }
25930
25931 hlinfo->mouse_face_beg_row = vpos;
25932 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25933 hlinfo->mouse_face_beg_y = 0;
25934 hlinfo->mouse_face_end_y = 0;
25935 hlinfo->mouse_face_past_end = 0;
25936 hlinfo->mouse_face_window = window;
25937
25938 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25939 charpos,
25940 0, 0, 0,
25941 &ignore,
25942 glyph->face_id,
25943 1);
25944 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25945
25946 if (NILP (pointer))
25947 pointer = Qhand;
25948 }
25949 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25950 clear_mouse_face (hlinfo);
25951 }
25952 #ifdef HAVE_WINDOW_SYSTEM
25953 if (FRAME_WINDOW_P (f))
25954 define_frame_cursor1 (f, cursor, pointer);
25955 #endif
25956 }
25957
25958
25959 /* EXPORT:
25960 Take proper action when the mouse has moved to position X, Y on
25961 frame F as regards highlighting characters that have mouse-face
25962 properties. Also de-highlighting chars where the mouse was before.
25963 X and Y can be negative or out of range. */
25964
25965 void
25966 note_mouse_highlight (struct frame *f, int x, int y)
25967 {
25968 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25969 enum window_part part;
25970 Lisp_Object window;
25971 struct window *w;
25972 Cursor cursor = No_Cursor;
25973 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25974 struct buffer *b;
25975
25976 /* When a menu is active, don't highlight because this looks odd. */
25977 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25978 if (popup_activated ())
25979 return;
25980 #endif
25981
25982 if (NILP (Vmouse_highlight)
25983 || !f->glyphs_initialized_p
25984 || f->pointer_invisible)
25985 return;
25986
25987 hlinfo->mouse_face_mouse_x = x;
25988 hlinfo->mouse_face_mouse_y = y;
25989 hlinfo->mouse_face_mouse_frame = f;
25990
25991 if (hlinfo->mouse_face_defer)
25992 return;
25993
25994 if (gc_in_progress)
25995 {
25996 hlinfo->mouse_face_deferred_gc = 1;
25997 return;
25998 }
25999
26000 /* Which window is that in? */
26001 window = window_from_coordinates (f, x, y, &part, 1);
26002
26003 /* If we were displaying active text in another window, clear that.
26004 Also clear if we move out of text area in same window. */
26005 if (! EQ (window, hlinfo->mouse_face_window)
26006 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26007 && !NILP (hlinfo->mouse_face_window)))
26008 clear_mouse_face (hlinfo);
26009
26010 /* Not on a window -> return. */
26011 if (!WINDOWP (window))
26012 return;
26013
26014 /* Reset help_echo_string. It will get recomputed below. */
26015 help_echo_string = Qnil;
26016
26017 /* Convert to window-relative pixel coordinates. */
26018 w = XWINDOW (window);
26019 frame_to_window_pixel_xy (w, &x, &y);
26020
26021 #ifdef HAVE_WINDOW_SYSTEM
26022 /* Handle tool-bar window differently since it doesn't display a
26023 buffer. */
26024 if (EQ (window, f->tool_bar_window))
26025 {
26026 note_tool_bar_highlight (f, x, y);
26027 return;
26028 }
26029 #endif
26030
26031 /* Mouse is on the mode, header line or margin? */
26032 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26033 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26034 {
26035 note_mode_line_or_margin_highlight (window, x, y, part);
26036 return;
26037 }
26038
26039 #ifdef HAVE_WINDOW_SYSTEM
26040 if (part == ON_VERTICAL_BORDER)
26041 {
26042 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26043 help_echo_string = build_string ("drag-mouse-1: resize");
26044 }
26045 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26046 || part == ON_SCROLL_BAR)
26047 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26048 else
26049 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26050 #endif
26051
26052 /* Are we in a window whose display is up to date?
26053 And verify the buffer's text has not changed. */
26054 b = XBUFFER (w->buffer);
26055 if (part == ON_TEXT
26056 && EQ (w->window_end_valid, w->buffer)
26057 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26058 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26059 {
26060 int hpos, vpos, i, dx, dy, area;
26061 EMACS_INT pos;
26062 struct glyph *glyph;
26063 Lisp_Object object;
26064 Lisp_Object mouse_face = Qnil, position;
26065 Lisp_Object *overlay_vec = NULL;
26066 int noverlays;
26067 struct buffer *obuf;
26068 EMACS_INT obegv, ozv;
26069 int same_region;
26070
26071 /* Find the glyph under X/Y. */
26072 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26073
26074 #ifdef HAVE_WINDOW_SYSTEM
26075 /* Look for :pointer property on image. */
26076 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26077 {
26078 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26079 if (img != NULL && IMAGEP (img->spec))
26080 {
26081 Lisp_Object image_map, hotspot;
26082 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26083 !NILP (image_map))
26084 && (hotspot = find_hot_spot (image_map,
26085 glyph->slice.img.x + dx,
26086 glyph->slice.img.y + dy),
26087 CONSP (hotspot))
26088 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26089 {
26090 Lisp_Object plist;
26091
26092 /* Could check XCAR (hotspot) to see if we enter/leave
26093 this hot-spot.
26094 If so, we could look for mouse-enter, mouse-leave
26095 properties in PLIST (and do something...). */
26096 hotspot = XCDR (hotspot);
26097 if (CONSP (hotspot)
26098 && (plist = XCAR (hotspot), CONSP (plist)))
26099 {
26100 pointer = Fplist_get (plist, Qpointer);
26101 if (NILP (pointer))
26102 pointer = Qhand;
26103 help_echo_string = Fplist_get (plist, Qhelp_echo);
26104 if (!NILP (help_echo_string))
26105 {
26106 help_echo_window = window;
26107 help_echo_object = glyph->object;
26108 help_echo_pos = glyph->charpos;
26109 }
26110 }
26111 }
26112 if (NILP (pointer))
26113 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26114 }
26115 }
26116 #endif /* HAVE_WINDOW_SYSTEM */
26117
26118 /* Clear mouse face if X/Y not over text. */
26119 if (glyph == NULL
26120 || area != TEXT_AREA
26121 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26122 /* Glyph's OBJECT is an integer for glyphs inserted by the
26123 display engine for its internal purposes, like truncation
26124 and continuation glyphs and blanks beyond the end of
26125 line's text on text terminals. If we are over such a
26126 glyph, we are not over any text. */
26127 || INTEGERP (glyph->object)
26128 /* R2L rows have a stretch glyph at their front, which
26129 stands for no text, whereas L2R rows have no glyphs at
26130 all beyond the end of text. Treat such stretch glyphs
26131 like we do with NULL glyphs in L2R rows. */
26132 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26133 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26134 && glyph->type == STRETCH_GLYPH
26135 && glyph->avoid_cursor_p))
26136 {
26137 if (clear_mouse_face (hlinfo))
26138 cursor = No_Cursor;
26139 #ifdef HAVE_WINDOW_SYSTEM
26140 if (FRAME_WINDOW_P (f) && NILP (pointer))
26141 {
26142 if (area != TEXT_AREA)
26143 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26144 else
26145 pointer = Vvoid_text_area_pointer;
26146 }
26147 #endif
26148 goto set_cursor;
26149 }
26150
26151 pos = glyph->charpos;
26152 object = glyph->object;
26153 if (!STRINGP (object) && !BUFFERP (object))
26154 goto set_cursor;
26155
26156 /* If we get an out-of-range value, return now; avoid an error. */
26157 if (BUFFERP (object) && pos > BUF_Z (b))
26158 goto set_cursor;
26159
26160 /* Make the window's buffer temporarily current for
26161 overlays_at and compute_char_face. */
26162 obuf = current_buffer;
26163 current_buffer = b;
26164 obegv = BEGV;
26165 ozv = ZV;
26166 BEGV = BEG;
26167 ZV = Z;
26168
26169 /* Is this char mouse-active or does it have help-echo? */
26170 position = make_number (pos);
26171
26172 if (BUFFERP (object))
26173 {
26174 /* Put all the overlays we want in a vector in overlay_vec. */
26175 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26176 /* Sort overlays into increasing priority order. */
26177 noverlays = sort_overlays (overlay_vec, noverlays, w);
26178 }
26179 else
26180 noverlays = 0;
26181
26182 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26183
26184 if (same_region)
26185 cursor = No_Cursor;
26186
26187 /* Check mouse-face highlighting. */
26188 if (! same_region
26189 /* If there exists an overlay with mouse-face overlapping
26190 the one we are currently highlighting, we have to
26191 check if we enter the overlapping overlay, and then
26192 highlight only that. */
26193 || (OVERLAYP (hlinfo->mouse_face_overlay)
26194 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26195 {
26196 /* Find the highest priority overlay with a mouse-face. */
26197 Lisp_Object overlay = Qnil;
26198 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26199 {
26200 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26201 if (!NILP (mouse_face))
26202 overlay = overlay_vec[i];
26203 }
26204
26205 /* If we're highlighting the same overlay as before, there's
26206 no need to do that again. */
26207 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26208 goto check_help_echo;
26209 hlinfo->mouse_face_overlay = overlay;
26210
26211 /* Clear the display of the old active region, if any. */
26212 if (clear_mouse_face (hlinfo))
26213 cursor = No_Cursor;
26214
26215 /* If no overlay applies, get a text property. */
26216 if (NILP (overlay))
26217 mouse_face = Fget_text_property (position, Qmouse_face, object);
26218
26219 /* Next, compute the bounds of the mouse highlighting and
26220 display it. */
26221 if (!NILP (mouse_face) && STRINGP (object))
26222 {
26223 /* The mouse-highlighting comes from a display string
26224 with a mouse-face. */
26225 Lisp_Object s, e;
26226 EMACS_INT ignore;
26227
26228 s = Fprevious_single_property_change
26229 (make_number (pos + 1), Qmouse_face, object, Qnil);
26230 e = Fnext_single_property_change
26231 (position, Qmouse_face, object, Qnil);
26232 if (NILP (s))
26233 s = make_number (0);
26234 if (NILP (e))
26235 e = make_number (SCHARS (object) - 1);
26236 mouse_face_from_string_pos (w, hlinfo, object,
26237 XINT (s), XINT (e));
26238 hlinfo->mouse_face_past_end = 0;
26239 hlinfo->mouse_face_window = window;
26240 hlinfo->mouse_face_face_id
26241 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26242 glyph->face_id, 1);
26243 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26244 cursor = No_Cursor;
26245 }
26246 else
26247 {
26248 /* The mouse-highlighting, if any, comes from an overlay
26249 or text property in the buffer. */
26250 Lisp_Object buffer IF_LINT (= Qnil);
26251 Lisp_Object cover_string IF_LINT (= Qnil);
26252
26253 if (STRINGP (object))
26254 {
26255 /* If we are on a display string with no mouse-face,
26256 check if the text under it has one. */
26257 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26258 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26259 pos = string_buffer_position (object, start);
26260 if (pos > 0)
26261 {
26262 mouse_face = get_char_property_and_overlay
26263 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26264 buffer = w->buffer;
26265 cover_string = object;
26266 }
26267 }
26268 else
26269 {
26270 buffer = object;
26271 cover_string = Qnil;
26272 }
26273
26274 if (!NILP (mouse_face))
26275 {
26276 Lisp_Object before, after;
26277 Lisp_Object before_string, after_string;
26278 /* To correctly find the limits of mouse highlight
26279 in a bidi-reordered buffer, we must not use the
26280 optimization of limiting the search in
26281 previous-single-property-change and
26282 next-single-property-change, because
26283 rows_from_pos_range needs the real start and end
26284 positions to DTRT in this case. That's because
26285 the first row visible in a window does not
26286 necessarily display the character whose position
26287 is the smallest. */
26288 Lisp_Object lim1 =
26289 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26290 ? Fmarker_position (w->start)
26291 : Qnil;
26292 Lisp_Object lim2 =
26293 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26294 ? make_number (BUF_Z (XBUFFER (buffer))
26295 - XFASTINT (w->window_end_pos))
26296 : Qnil;
26297
26298 if (NILP (overlay))
26299 {
26300 /* Handle the text property case. */
26301 before = Fprevious_single_property_change
26302 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26303 after = Fnext_single_property_change
26304 (make_number (pos), Qmouse_face, buffer, lim2);
26305 before_string = after_string = Qnil;
26306 }
26307 else
26308 {
26309 /* Handle the overlay case. */
26310 before = Foverlay_start (overlay);
26311 after = Foverlay_end (overlay);
26312 before_string = Foverlay_get (overlay, Qbefore_string);
26313 after_string = Foverlay_get (overlay, Qafter_string);
26314
26315 if (!STRINGP (before_string)) before_string = Qnil;
26316 if (!STRINGP (after_string)) after_string = Qnil;
26317 }
26318
26319 mouse_face_from_buffer_pos (window, hlinfo, pos,
26320 XFASTINT (before),
26321 XFASTINT (after),
26322 before_string, after_string,
26323 cover_string);
26324 cursor = No_Cursor;
26325 }
26326 }
26327 }
26328
26329 check_help_echo:
26330
26331 /* Look for a `help-echo' property. */
26332 if (NILP (help_echo_string)) {
26333 Lisp_Object help, overlay;
26334
26335 /* Check overlays first. */
26336 help = overlay = Qnil;
26337 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26338 {
26339 overlay = overlay_vec[i];
26340 help = Foverlay_get (overlay, Qhelp_echo);
26341 }
26342
26343 if (!NILP (help))
26344 {
26345 help_echo_string = help;
26346 help_echo_window = window;
26347 help_echo_object = overlay;
26348 help_echo_pos = pos;
26349 }
26350 else
26351 {
26352 Lisp_Object obj = glyph->object;
26353 EMACS_INT charpos = glyph->charpos;
26354
26355 /* Try text properties. */
26356 if (STRINGP (obj)
26357 && charpos >= 0
26358 && charpos < SCHARS (obj))
26359 {
26360 help = Fget_text_property (make_number (charpos),
26361 Qhelp_echo, obj);
26362 if (NILP (help))
26363 {
26364 /* If the string itself doesn't specify a help-echo,
26365 see if the buffer text ``under'' it does. */
26366 struct glyph_row *r
26367 = MATRIX_ROW (w->current_matrix, vpos);
26368 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26369 EMACS_INT p = string_buffer_position (obj, start);
26370 if (p > 0)
26371 {
26372 help = Fget_char_property (make_number (p),
26373 Qhelp_echo, w->buffer);
26374 if (!NILP (help))
26375 {
26376 charpos = p;
26377 obj = w->buffer;
26378 }
26379 }
26380 }
26381 }
26382 else if (BUFFERP (obj)
26383 && charpos >= BEGV
26384 && charpos < ZV)
26385 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26386 obj);
26387
26388 if (!NILP (help))
26389 {
26390 help_echo_string = help;
26391 help_echo_window = window;
26392 help_echo_object = obj;
26393 help_echo_pos = charpos;
26394 }
26395 }
26396 }
26397
26398 #ifdef HAVE_WINDOW_SYSTEM
26399 /* Look for a `pointer' property. */
26400 if (FRAME_WINDOW_P (f) && NILP (pointer))
26401 {
26402 /* Check overlays first. */
26403 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26404 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26405
26406 if (NILP (pointer))
26407 {
26408 Lisp_Object obj = glyph->object;
26409 EMACS_INT charpos = glyph->charpos;
26410
26411 /* Try text properties. */
26412 if (STRINGP (obj)
26413 && charpos >= 0
26414 && charpos < SCHARS (obj))
26415 {
26416 pointer = Fget_text_property (make_number (charpos),
26417 Qpointer, obj);
26418 if (NILP (pointer))
26419 {
26420 /* If the string itself doesn't specify a pointer,
26421 see if the buffer text ``under'' it does. */
26422 struct glyph_row *r
26423 = MATRIX_ROW (w->current_matrix, vpos);
26424 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26425 EMACS_INT p = string_buffer_position (obj, start);
26426 if (p > 0)
26427 pointer = Fget_char_property (make_number (p),
26428 Qpointer, w->buffer);
26429 }
26430 }
26431 else if (BUFFERP (obj)
26432 && charpos >= BEGV
26433 && charpos < ZV)
26434 pointer = Fget_text_property (make_number (charpos),
26435 Qpointer, obj);
26436 }
26437 }
26438 #endif /* HAVE_WINDOW_SYSTEM */
26439
26440 BEGV = obegv;
26441 ZV = ozv;
26442 current_buffer = obuf;
26443 }
26444
26445 set_cursor:
26446
26447 #ifdef HAVE_WINDOW_SYSTEM
26448 if (FRAME_WINDOW_P (f))
26449 define_frame_cursor1 (f, cursor, pointer);
26450 #else
26451 /* This is here to prevent a compiler error, about "label at end of
26452 compound statement". */
26453 return;
26454 #endif
26455 }
26456
26457
26458 /* EXPORT for RIF:
26459 Clear any mouse-face on window W. This function is part of the
26460 redisplay interface, and is called from try_window_id and similar
26461 functions to ensure the mouse-highlight is off. */
26462
26463 void
26464 x_clear_window_mouse_face (struct window *w)
26465 {
26466 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26467 Lisp_Object window;
26468
26469 BLOCK_INPUT;
26470 XSETWINDOW (window, w);
26471 if (EQ (window, hlinfo->mouse_face_window))
26472 clear_mouse_face (hlinfo);
26473 UNBLOCK_INPUT;
26474 }
26475
26476
26477 /* EXPORT:
26478 Just discard the mouse face information for frame F, if any.
26479 This is used when the size of F is changed. */
26480
26481 void
26482 cancel_mouse_face (struct frame *f)
26483 {
26484 Lisp_Object window;
26485 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26486
26487 window = hlinfo->mouse_face_window;
26488 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26489 {
26490 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26491 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26492 hlinfo->mouse_face_window = Qnil;
26493 }
26494 }
26495
26496
26497 \f
26498 /***********************************************************************
26499 Exposure Events
26500 ***********************************************************************/
26501
26502 #ifdef HAVE_WINDOW_SYSTEM
26503
26504 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26505 which intersects rectangle R. R is in window-relative coordinates. */
26506
26507 static void
26508 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26509 enum glyph_row_area area)
26510 {
26511 struct glyph *first = row->glyphs[area];
26512 struct glyph *end = row->glyphs[area] + row->used[area];
26513 struct glyph *last;
26514 int first_x, start_x, x;
26515
26516 if (area == TEXT_AREA && row->fill_line_p)
26517 /* If row extends face to end of line write the whole line. */
26518 draw_glyphs (w, 0, row, area,
26519 0, row->used[area],
26520 DRAW_NORMAL_TEXT, 0);
26521 else
26522 {
26523 /* Set START_X to the window-relative start position for drawing glyphs of
26524 AREA. The first glyph of the text area can be partially visible.
26525 The first glyphs of other areas cannot. */
26526 start_x = window_box_left_offset (w, area);
26527 x = start_x;
26528 if (area == TEXT_AREA)
26529 x += row->x;
26530
26531 /* Find the first glyph that must be redrawn. */
26532 while (first < end
26533 && x + first->pixel_width < r->x)
26534 {
26535 x += first->pixel_width;
26536 ++first;
26537 }
26538
26539 /* Find the last one. */
26540 last = first;
26541 first_x = x;
26542 while (last < end
26543 && x < r->x + r->width)
26544 {
26545 x += last->pixel_width;
26546 ++last;
26547 }
26548
26549 /* Repaint. */
26550 if (last > first)
26551 draw_glyphs (w, first_x - start_x, row, area,
26552 first - row->glyphs[area], last - row->glyphs[area],
26553 DRAW_NORMAL_TEXT, 0);
26554 }
26555 }
26556
26557
26558 /* Redraw the parts of the glyph row ROW on window W intersecting
26559 rectangle R. R is in window-relative coordinates. Value is
26560 non-zero if mouse-face was overwritten. */
26561
26562 static int
26563 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26564 {
26565 xassert (row->enabled_p);
26566
26567 if (row->mode_line_p || w->pseudo_window_p)
26568 draw_glyphs (w, 0, row, TEXT_AREA,
26569 0, row->used[TEXT_AREA],
26570 DRAW_NORMAL_TEXT, 0);
26571 else
26572 {
26573 if (row->used[LEFT_MARGIN_AREA])
26574 expose_area (w, row, r, LEFT_MARGIN_AREA);
26575 if (row->used[TEXT_AREA])
26576 expose_area (w, row, r, TEXT_AREA);
26577 if (row->used[RIGHT_MARGIN_AREA])
26578 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26579 draw_row_fringe_bitmaps (w, row);
26580 }
26581
26582 return row->mouse_face_p;
26583 }
26584
26585
26586 /* Redraw those parts of glyphs rows during expose event handling that
26587 overlap other rows. Redrawing of an exposed line writes over parts
26588 of lines overlapping that exposed line; this function fixes that.
26589
26590 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26591 row in W's current matrix that is exposed and overlaps other rows.
26592 LAST_OVERLAPPING_ROW is the last such row. */
26593
26594 static void
26595 expose_overlaps (struct window *w,
26596 struct glyph_row *first_overlapping_row,
26597 struct glyph_row *last_overlapping_row,
26598 XRectangle *r)
26599 {
26600 struct glyph_row *row;
26601
26602 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26603 if (row->overlapping_p)
26604 {
26605 xassert (row->enabled_p && !row->mode_line_p);
26606
26607 row->clip = r;
26608 if (row->used[LEFT_MARGIN_AREA])
26609 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26610
26611 if (row->used[TEXT_AREA])
26612 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26613
26614 if (row->used[RIGHT_MARGIN_AREA])
26615 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26616 row->clip = NULL;
26617 }
26618 }
26619
26620
26621 /* Return non-zero if W's cursor intersects rectangle R. */
26622
26623 static int
26624 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26625 {
26626 XRectangle cr, result;
26627 struct glyph *cursor_glyph;
26628 struct glyph_row *row;
26629
26630 if (w->phys_cursor.vpos >= 0
26631 && w->phys_cursor.vpos < w->current_matrix->nrows
26632 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26633 row->enabled_p)
26634 && row->cursor_in_fringe_p)
26635 {
26636 /* Cursor is in the fringe. */
26637 cr.x = window_box_right_offset (w,
26638 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26639 ? RIGHT_MARGIN_AREA
26640 : TEXT_AREA));
26641 cr.y = row->y;
26642 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26643 cr.height = row->height;
26644 return x_intersect_rectangles (&cr, r, &result);
26645 }
26646
26647 cursor_glyph = get_phys_cursor_glyph (w);
26648 if (cursor_glyph)
26649 {
26650 /* r is relative to W's box, but w->phys_cursor.x is relative
26651 to left edge of W's TEXT area. Adjust it. */
26652 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26653 cr.y = w->phys_cursor.y;
26654 cr.width = cursor_glyph->pixel_width;
26655 cr.height = w->phys_cursor_height;
26656 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26657 I assume the effect is the same -- and this is portable. */
26658 return x_intersect_rectangles (&cr, r, &result);
26659 }
26660 /* If we don't understand the format, pretend we're not in the hot-spot. */
26661 return 0;
26662 }
26663
26664
26665 /* EXPORT:
26666 Draw a vertical window border to the right of window W if W doesn't
26667 have vertical scroll bars. */
26668
26669 void
26670 x_draw_vertical_border (struct window *w)
26671 {
26672 struct frame *f = XFRAME (WINDOW_FRAME (w));
26673
26674 /* We could do better, if we knew what type of scroll-bar the adjacent
26675 windows (on either side) have... But we don't :-(
26676 However, I think this works ok. ++KFS 2003-04-25 */
26677
26678 /* Redraw borders between horizontally adjacent windows. Don't
26679 do it for frames with vertical scroll bars because either the
26680 right scroll bar of a window, or the left scroll bar of its
26681 neighbor will suffice as a border. */
26682 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26683 return;
26684
26685 if (!WINDOW_RIGHTMOST_P (w)
26686 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26687 {
26688 int x0, x1, y0, y1;
26689
26690 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26691 y1 -= 1;
26692
26693 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26694 x1 -= 1;
26695
26696 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26697 }
26698 else if (!WINDOW_LEFTMOST_P (w)
26699 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26700 {
26701 int x0, x1, y0, y1;
26702
26703 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26704 y1 -= 1;
26705
26706 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26707 x0 -= 1;
26708
26709 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26710 }
26711 }
26712
26713
26714 /* Redraw the part of window W intersection rectangle FR. Pixel
26715 coordinates in FR are frame-relative. Call this function with
26716 input blocked. Value is non-zero if the exposure overwrites
26717 mouse-face. */
26718
26719 static int
26720 expose_window (struct window *w, XRectangle *fr)
26721 {
26722 struct frame *f = XFRAME (w->frame);
26723 XRectangle wr, r;
26724 int mouse_face_overwritten_p = 0;
26725
26726 /* If window is not yet fully initialized, do nothing. This can
26727 happen when toolkit scroll bars are used and a window is split.
26728 Reconfiguring the scroll bar will generate an expose for a newly
26729 created window. */
26730 if (w->current_matrix == NULL)
26731 return 0;
26732
26733 /* When we're currently updating the window, display and current
26734 matrix usually don't agree. Arrange for a thorough display
26735 later. */
26736 if (w == updated_window)
26737 {
26738 SET_FRAME_GARBAGED (f);
26739 return 0;
26740 }
26741
26742 /* Frame-relative pixel rectangle of W. */
26743 wr.x = WINDOW_LEFT_EDGE_X (w);
26744 wr.y = WINDOW_TOP_EDGE_Y (w);
26745 wr.width = WINDOW_TOTAL_WIDTH (w);
26746 wr.height = WINDOW_TOTAL_HEIGHT (w);
26747
26748 if (x_intersect_rectangles (fr, &wr, &r))
26749 {
26750 int yb = window_text_bottom_y (w);
26751 struct glyph_row *row;
26752 int cursor_cleared_p;
26753 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26754
26755 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26756 r.x, r.y, r.width, r.height));
26757
26758 /* Convert to window coordinates. */
26759 r.x -= WINDOW_LEFT_EDGE_X (w);
26760 r.y -= WINDOW_TOP_EDGE_Y (w);
26761
26762 /* Turn off the cursor. */
26763 if (!w->pseudo_window_p
26764 && phys_cursor_in_rect_p (w, &r))
26765 {
26766 x_clear_cursor (w);
26767 cursor_cleared_p = 1;
26768 }
26769 else
26770 cursor_cleared_p = 0;
26771
26772 /* Update lines intersecting rectangle R. */
26773 first_overlapping_row = last_overlapping_row = NULL;
26774 for (row = w->current_matrix->rows;
26775 row->enabled_p;
26776 ++row)
26777 {
26778 int y0 = row->y;
26779 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26780
26781 if ((y0 >= r.y && y0 < r.y + r.height)
26782 || (y1 > r.y && y1 < r.y + r.height)
26783 || (r.y >= y0 && r.y < y1)
26784 || (r.y + r.height > y0 && r.y + r.height < y1))
26785 {
26786 /* A header line may be overlapping, but there is no need
26787 to fix overlapping areas for them. KFS 2005-02-12 */
26788 if (row->overlapping_p && !row->mode_line_p)
26789 {
26790 if (first_overlapping_row == NULL)
26791 first_overlapping_row = row;
26792 last_overlapping_row = row;
26793 }
26794
26795 row->clip = fr;
26796 if (expose_line (w, row, &r))
26797 mouse_face_overwritten_p = 1;
26798 row->clip = NULL;
26799 }
26800 else if (row->overlapping_p)
26801 {
26802 /* We must redraw a row overlapping the exposed area. */
26803 if (y0 < r.y
26804 ? y0 + row->phys_height > r.y
26805 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26806 {
26807 if (first_overlapping_row == NULL)
26808 first_overlapping_row = row;
26809 last_overlapping_row = row;
26810 }
26811 }
26812
26813 if (y1 >= yb)
26814 break;
26815 }
26816
26817 /* Display the mode line if there is one. */
26818 if (WINDOW_WANTS_MODELINE_P (w)
26819 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26820 row->enabled_p)
26821 && row->y < r.y + r.height)
26822 {
26823 if (expose_line (w, row, &r))
26824 mouse_face_overwritten_p = 1;
26825 }
26826
26827 if (!w->pseudo_window_p)
26828 {
26829 /* Fix the display of overlapping rows. */
26830 if (first_overlapping_row)
26831 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26832 fr);
26833
26834 /* Draw border between windows. */
26835 x_draw_vertical_border (w);
26836
26837 /* Turn the cursor on again. */
26838 if (cursor_cleared_p)
26839 update_window_cursor (w, 1);
26840 }
26841 }
26842
26843 return mouse_face_overwritten_p;
26844 }
26845
26846
26847
26848 /* Redraw (parts) of all windows in the window tree rooted at W that
26849 intersect R. R contains frame pixel coordinates. Value is
26850 non-zero if the exposure overwrites mouse-face. */
26851
26852 static int
26853 expose_window_tree (struct window *w, XRectangle *r)
26854 {
26855 struct frame *f = XFRAME (w->frame);
26856 int mouse_face_overwritten_p = 0;
26857
26858 while (w && !FRAME_GARBAGED_P (f))
26859 {
26860 if (!NILP (w->hchild))
26861 mouse_face_overwritten_p
26862 |= expose_window_tree (XWINDOW (w->hchild), r);
26863 else if (!NILP (w->vchild))
26864 mouse_face_overwritten_p
26865 |= expose_window_tree (XWINDOW (w->vchild), r);
26866 else
26867 mouse_face_overwritten_p |= expose_window (w, r);
26868
26869 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26870 }
26871
26872 return mouse_face_overwritten_p;
26873 }
26874
26875
26876 /* EXPORT:
26877 Redisplay an exposed area of frame F. X and Y are the upper-left
26878 corner of the exposed rectangle. W and H are width and height of
26879 the exposed area. All are pixel values. W or H zero means redraw
26880 the entire frame. */
26881
26882 void
26883 expose_frame (struct frame *f, int x, int y, int w, int h)
26884 {
26885 XRectangle r;
26886 int mouse_face_overwritten_p = 0;
26887
26888 TRACE ((stderr, "expose_frame "));
26889
26890 /* No need to redraw if frame will be redrawn soon. */
26891 if (FRAME_GARBAGED_P (f))
26892 {
26893 TRACE ((stderr, " garbaged\n"));
26894 return;
26895 }
26896
26897 /* If basic faces haven't been realized yet, there is no point in
26898 trying to redraw anything. This can happen when we get an expose
26899 event while Emacs is starting, e.g. by moving another window. */
26900 if (FRAME_FACE_CACHE (f) == NULL
26901 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26902 {
26903 TRACE ((stderr, " no faces\n"));
26904 return;
26905 }
26906
26907 if (w == 0 || h == 0)
26908 {
26909 r.x = r.y = 0;
26910 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26911 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26912 }
26913 else
26914 {
26915 r.x = x;
26916 r.y = y;
26917 r.width = w;
26918 r.height = h;
26919 }
26920
26921 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26922 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26923
26924 if (WINDOWP (f->tool_bar_window))
26925 mouse_face_overwritten_p
26926 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26927
26928 #ifdef HAVE_X_WINDOWS
26929 #ifndef MSDOS
26930 #ifndef USE_X_TOOLKIT
26931 if (WINDOWP (f->menu_bar_window))
26932 mouse_face_overwritten_p
26933 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26934 #endif /* not USE_X_TOOLKIT */
26935 #endif
26936 #endif
26937
26938 /* Some window managers support a focus-follows-mouse style with
26939 delayed raising of frames. Imagine a partially obscured frame,
26940 and moving the mouse into partially obscured mouse-face on that
26941 frame. The visible part of the mouse-face will be highlighted,
26942 then the WM raises the obscured frame. With at least one WM, KDE
26943 2.1, Emacs is not getting any event for the raising of the frame
26944 (even tried with SubstructureRedirectMask), only Expose events.
26945 These expose events will draw text normally, i.e. not
26946 highlighted. Which means we must redo the highlight here.
26947 Subsume it under ``we love X''. --gerd 2001-08-15 */
26948 /* Included in Windows version because Windows most likely does not
26949 do the right thing if any third party tool offers
26950 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26951 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26952 {
26953 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26954 if (f == hlinfo->mouse_face_mouse_frame)
26955 {
26956 int mouse_x = hlinfo->mouse_face_mouse_x;
26957 int mouse_y = hlinfo->mouse_face_mouse_y;
26958 clear_mouse_face (hlinfo);
26959 note_mouse_highlight (f, mouse_x, mouse_y);
26960 }
26961 }
26962 }
26963
26964
26965 /* EXPORT:
26966 Determine the intersection of two rectangles R1 and R2. Return
26967 the intersection in *RESULT. Value is non-zero if RESULT is not
26968 empty. */
26969
26970 int
26971 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26972 {
26973 XRectangle *left, *right;
26974 XRectangle *upper, *lower;
26975 int intersection_p = 0;
26976
26977 /* Rearrange so that R1 is the left-most rectangle. */
26978 if (r1->x < r2->x)
26979 left = r1, right = r2;
26980 else
26981 left = r2, right = r1;
26982
26983 /* X0 of the intersection is right.x0, if this is inside R1,
26984 otherwise there is no intersection. */
26985 if (right->x <= left->x + left->width)
26986 {
26987 result->x = right->x;
26988
26989 /* The right end of the intersection is the minimum of the
26990 the right ends of left and right. */
26991 result->width = (min (left->x + left->width, right->x + right->width)
26992 - result->x);
26993
26994 /* Same game for Y. */
26995 if (r1->y < r2->y)
26996 upper = r1, lower = r2;
26997 else
26998 upper = r2, lower = r1;
26999
27000 /* The upper end of the intersection is lower.y0, if this is inside
27001 of upper. Otherwise, there is no intersection. */
27002 if (lower->y <= upper->y + upper->height)
27003 {
27004 result->y = lower->y;
27005
27006 /* The lower end of the intersection is the minimum of the lower
27007 ends of upper and lower. */
27008 result->height = (min (lower->y + lower->height,
27009 upper->y + upper->height)
27010 - result->y);
27011 intersection_p = 1;
27012 }
27013 }
27014
27015 return intersection_p;
27016 }
27017
27018 #endif /* HAVE_WINDOW_SYSTEM */
27019
27020 \f
27021 /***********************************************************************
27022 Initialization
27023 ***********************************************************************/
27024
27025 void
27026 syms_of_xdisp (void)
27027 {
27028 Vwith_echo_area_save_vector = Qnil;
27029 staticpro (&Vwith_echo_area_save_vector);
27030
27031 Vmessage_stack = Qnil;
27032 staticpro (&Vmessage_stack);
27033
27034 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
27035 staticpro (&Qinhibit_redisplay);
27036
27037 message_dolog_marker1 = Fmake_marker ();
27038 staticpro (&message_dolog_marker1);
27039 message_dolog_marker2 = Fmake_marker ();
27040 staticpro (&message_dolog_marker2);
27041 message_dolog_marker3 = Fmake_marker ();
27042 staticpro (&message_dolog_marker3);
27043
27044 #if GLYPH_DEBUG
27045 defsubr (&Sdump_frame_glyph_matrix);
27046 defsubr (&Sdump_glyph_matrix);
27047 defsubr (&Sdump_glyph_row);
27048 defsubr (&Sdump_tool_bar_row);
27049 defsubr (&Strace_redisplay);
27050 defsubr (&Strace_to_stderr);
27051 #endif
27052 #ifdef HAVE_WINDOW_SYSTEM
27053 defsubr (&Stool_bar_lines_needed);
27054 defsubr (&Slookup_image_map);
27055 #endif
27056 defsubr (&Sformat_mode_line);
27057 defsubr (&Sinvisible_p);
27058 defsubr (&Scurrent_bidi_paragraph_direction);
27059
27060 staticpro (&Qmenu_bar_update_hook);
27061 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
27062
27063 staticpro (&Qoverriding_terminal_local_map);
27064 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
27065
27066 staticpro (&Qoverriding_local_map);
27067 Qoverriding_local_map = intern_c_string ("overriding-local-map");
27068
27069 staticpro (&Qwindow_scroll_functions);
27070 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
27071
27072 staticpro (&Qwindow_text_change_functions);
27073 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
27074
27075 staticpro (&Qredisplay_end_trigger_functions);
27076 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
27077
27078 staticpro (&Qinhibit_point_motion_hooks);
27079 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
27080
27081 Qeval = intern_c_string ("eval");
27082 staticpro (&Qeval);
27083
27084 QCdata = intern_c_string (":data");
27085 staticpro (&QCdata);
27086 Qdisplay = intern_c_string ("display");
27087 staticpro (&Qdisplay);
27088 Qspace_width = intern_c_string ("space-width");
27089 staticpro (&Qspace_width);
27090 Qraise = intern_c_string ("raise");
27091 staticpro (&Qraise);
27092 Qslice = intern_c_string ("slice");
27093 staticpro (&Qslice);
27094 Qspace = intern_c_string ("space");
27095 staticpro (&Qspace);
27096 Qmargin = intern_c_string ("margin");
27097 staticpro (&Qmargin);
27098 Qpointer = intern_c_string ("pointer");
27099 staticpro (&Qpointer);
27100 Qleft_margin = intern_c_string ("left-margin");
27101 staticpro (&Qleft_margin);
27102 Qright_margin = intern_c_string ("right-margin");
27103 staticpro (&Qright_margin);
27104 Qcenter = intern_c_string ("center");
27105 staticpro (&Qcenter);
27106 Qline_height = intern_c_string ("line-height");
27107 staticpro (&Qline_height);
27108 QCalign_to = intern_c_string (":align-to");
27109 staticpro (&QCalign_to);
27110 QCrelative_width = intern_c_string (":relative-width");
27111 staticpro (&QCrelative_width);
27112 QCrelative_height = intern_c_string (":relative-height");
27113 staticpro (&QCrelative_height);
27114 QCeval = intern_c_string (":eval");
27115 staticpro (&QCeval);
27116 QCpropertize = intern_c_string (":propertize");
27117 staticpro (&QCpropertize);
27118 QCfile = intern_c_string (":file");
27119 staticpro (&QCfile);
27120 Qfontified = intern_c_string ("fontified");
27121 staticpro (&Qfontified);
27122 Qfontification_functions = intern_c_string ("fontification-functions");
27123 staticpro (&Qfontification_functions);
27124 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
27125 staticpro (&Qtrailing_whitespace);
27126 Qescape_glyph = intern_c_string ("escape-glyph");
27127 staticpro (&Qescape_glyph);
27128 Qnobreak_space = intern_c_string ("nobreak-space");
27129 staticpro (&Qnobreak_space);
27130 Qimage = intern_c_string ("image");
27131 staticpro (&Qimage);
27132 Qtext = intern_c_string ("text");
27133 staticpro (&Qtext);
27134 Qboth = intern_c_string ("both");
27135 staticpro (&Qboth);
27136 Qboth_horiz = intern_c_string ("both-horiz");
27137 staticpro (&Qboth_horiz);
27138 Qtext_image_horiz = intern_c_string ("text-image-horiz");
27139 staticpro (&Qtext_image_horiz);
27140 QCmap = intern_c_string (":map");
27141 staticpro (&QCmap);
27142 QCpointer = intern_c_string (":pointer");
27143 staticpro (&QCpointer);
27144 Qrect = intern_c_string ("rect");
27145 staticpro (&Qrect);
27146 Qcircle = intern_c_string ("circle");
27147 staticpro (&Qcircle);
27148 Qpoly = intern_c_string ("poly");
27149 staticpro (&Qpoly);
27150 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
27151 staticpro (&Qmessage_truncate_lines);
27152 Qgrow_only = intern_c_string ("grow-only");
27153 staticpro (&Qgrow_only);
27154 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
27155 staticpro (&Qinhibit_menubar_update);
27156 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
27157 staticpro (&Qinhibit_eval_during_redisplay);
27158 Qposition = intern_c_string ("position");
27159 staticpro (&Qposition);
27160 Qbuffer_position = intern_c_string ("buffer-position");
27161 staticpro (&Qbuffer_position);
27162 Qobject = intern_c_string ("object");
27163 staticpro (&Qobject);
27164 Qbar = intern_c_string ("bar");
27165 staticpro (&Qbar);
27166 Qhbar = intern_c_string ("hbar");
27167 staticpro (&Qhbar);
27168 Qbox = intern_c_string ("box");
27169 staticpro (&Qbox);
27170 Qhollow = intern_c_string ("hollow");
27171 staticpro (&Qhollow);
27172 Qhand = intern_c_string ("hand");
27173 staticpro (&Qhand);
27174 Qarrow = intern_c_string ("arrow");
27175 staticpro (&Qarrow);
27176 Qtext = intern_c_string ("text");
27177 staticpro (&Qtext);
27178 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
27179 staticpro (&Qinhibit_free_realized_faces);
27180
27181 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27182 Fcons (intern_c_string ("void-variable"), Qnil)),
27183 Qnil);
27184 staticpro (&list_of_error);
27185
27186 Qlast_arrow_position = intern_c_string ("last-arrow-position");
27187 staticpro (&Qlast_arrow_position);
27188 Qlast_arrow_string = intern_c_string ("last-arrow-string");
27189 staticpro (&Qlast_arrow_string);
27190
27191 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
27192 staticpro (&Qoverlay_arrow_string);
27193 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
27194 staticpro (&Qoverlay_arrow_bitmap);
27195
27196 echo_buffer[0] = echo_buffer[1] = Qnil;
27197 staticpro (&echo_buffer[0]);
27198 staticpro (&echo_buffer[1]);
27199
27200 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27201 staticpro (&echo_area_buffer[0]);
27202 staticpro (&echo_area_buffer[1]);
27203
27204 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27205 staticpro (&Vmessages_buffer_name);
27206
27207 mode_line_proptrans_alist = Qnil;
27208 staticpro (&mode_line_proptrans_alist);
27209 mode_line_string_list = Qnil;
27210 staticpro (&mode_line_string_list);
27211 mode_line_string_face = Qnil;
27212 staticpro (&mode_line_string_face);
27213 mode_line_string_face_prop = Qnil;
27214 staticpro (&mode_line_string_face_prop);
27215 Vmode_line_unwind_vector = Qnil;
27216 staticpro (&Vmode_line_unwind_vector);
27217
27218 help_echo_string = Qnil;
27219 staticpro (&help_echo_string);
27220 help_echo_object = Qnil;
27221 staticpro (&help_echo_object);
27222 help_echo_window = Qnil;
27223 staticpro (&help_echo_window);
27224 previous_help_echo_string = Qnil;
27225 staticpro (&previous_help_echo_string);
27226 help_echo_pos = -1;
27227
27228 Qright_to_left = intern_c_string ("right-to-left");
27229 staticpro (&Qright_to_left);
27230 Qleft_to_right = intern_c_string ("left-to-right");
27231 staticpro (&Qleft_to_right);
27232
27233 #ifdef HAVE_WINDOW_SYSTEM
27234 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27235 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27236 For example, if a block cursor is over a tab, it will be drawn as
27237 wide as that tab on the display. */);
27238 x_stretch_cursor_p = 0;
27239 #endif
27240
27241 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27242 doc: /* *Non-nil means highlight trailing whitespace.
27243 The face used for trailing whitespace is `trailing-whitespace'. */);
27244 Vshow_trailing_whitespace = Qnil;
27245
27246 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27247 doc: /* *Control highlighting of nobreak space and soft hyphen.
27248 A value of t means highlight the character itself (for nobreak space,
27249 use face `nobreak-space').
27250 A value of nil means no highlighting.
27251 Other values mean display the escape glyph followed by an ordinary
27252 space or ordinary hyphen. */);
27253 Vnobreak_char_display = Qt;
27254
27255 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27256 doc: /* *The pointer shape to show in void text areas.
27257 A value of nil means to show the text pointer. Other options are `arrow',
27258 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27259 Vvoid_text_area_pointer = Qarrow;
27260
27261 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27262 doc: /* Non-nil means don't actually do any redisplay.
27263 This is used for internal purposes. */);
27264 Vinhibit_redisplay = Qnil;
27265
27266 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27267 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27268 Vglobal_mode_string = Qnil;
27269
27270 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27271 doc: /* Marker for where to display an arrow on top of the buffer text.
27272 This must be the beginning of a line in order to work.
27273 See also `overlay-arrow-string'. */);
27274 Voverlay_arrow_position = Qnil;
27275
27276 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27277 doc: /* String to display as an arrow in non-window frames.
27278 See also `overlay-arrow-position'. */);
27279 Voverlay_arrow_string = make_pure_c_string ("=>");
27280
27281 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27282 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27283 The symbols on this list are examined during redisplay to determine
27284 where to display overlay arrows. */);
27285 Voverlay_arrow_variable_list
27286 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27287
27288 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27289 doc: /* *The number of lines to try scrolling a window by when point moves out.
27290 If that fails to bring point back on frame, point is centered instead.
27291 If this is zero, point is always centered after it moves off frame.
27292 If you want scrolling to always be a line at a time, you should set
27293 `scroll-conservatively' to a large value rather than set this to 1. */);
27294
27295 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27296 doc: /* *Scroll up to this many lines, to bring point back on screen.
27297 If point moves off-screen, redisplay will scroll by up to
27298 `scroll-conservatively' lines in order to bring point just barely
27299 onto the screen again. If that cannot be done, then redisplay
27300 recenters point as usual.
27301
27302 If the value is greater than 100, redisplay will never recenter point,
27303 but will always scroll just enough text to bring point into view, even
27304 if you move far away.
27305
27306 A value of zero means always recenter point if it moves off screen. */);
27307 scroll_conservatively = 0;
27308
27309 DEFVAR_INT ("scroll-margin", scroll_margin,
27310 doc: /* *Number of lines of margin at the top and bottom of a window.
27311 Recenter the window whenever point gets within this many lines
27312 of the top or bottom of the window. */);
27313 scroll_margin = 0;
27314
27315 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27316 doc: /* Pixels per inch value for non-window system displays.
27317 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27318 Vdisplay_pixels_per_inch = make_float (72.0);
27319
27320 #if GLYPH_DEBUG
27321 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27322 #endif
27323
27324 DEFVAR_LISP ("truncate-partial-width-windows",
27325 Vtruncate_partial_width_windows,
27326 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27327 For an integer value, truncate lines in each window narrower than the
27328 full frame width, provided the window width is less than that integer;
27329 otherwise, respect the value of `truncate-lines'.
27330
27331 For any other non-nil value, truncate lines in all windows that do
27332 not span the full frame width.
27333
27334 A value of nil means to respect the value of `truncate-lines'.
27335
27336 If `word-wrap' is enabled, you might want to reduce this. */);
27337 Vtruncate_partial_width_windows = make_number (50);
27338
27339 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27340 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27341 Any other value means to use the appropriate face, `mode-line',
27342 `header-line', or `menu' respectively. */);
27343 mode_line_inverse_video = 1;
27344
27345 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27346 doc: /* *Maximum buffer size for which line number should be displayed.
27347 If the buffer is bigger than this, the line number does not appear
27348 in the mode line. A value of nil means no limit. */);
27349 Vline_number_display_limit = Qnil;
27350
27351 DEFVAR_INT ("line-number-display-limit-width",
27352 line_number_display_limit_width,
27353 doc: /* *Maximum line width (in characters) for line number display.
27354 If the average length of the lines near point is bigger than this, then the
27355 line number may be omitted from the mode line. */);
27356 line_number_display_limit_width = 200;
27357
27358 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27359 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27360 highlight_nonselected_windows = 0;
27361
27362 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27363 doc: /* Non-nil if more than one frame is visible on this display.
27364 Minibuffer-only frames don't count, but iconified frames do.
27365 This variable is not guaranteed to be accurate except while processing
27366 `frame-title-format' and `icon-title-format'. */);
27367
27368 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27369 doc: /* Template for displaying the title bar of visible frames.
27370 \(Assuming the window manager supports this feature.)
27371
27372 This variable has the same structure as `mode-line-format', except that
27373 the %c and %l constructs are ignored. It is used only on frames for
27374 which no explicit name has been set \(see `modify-frame-parameters'). */);
27375
27376 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27377 doc: /* Template for displaying the title bar of an iconified frame.
27378 \(Assuming the window manager supports this feature.)
27379 This variable has the same structure as `mode-line-format' (which see),
27380 and is used only on frames for which no explicit name has been set
27381 \(see `modify-frame-parameters'). */);
27382 Vicon_title_format
27383 = Vframe_title_format
27384 = pure_cons (intern_c_string ("multiple-frames"),
27385 pure_cons (make_pure_c_string ("%b"),
27386 pure_cons (pure_cons (empty_unibyte_string,
27387 pure_cons (intern_c_string ("invocation-name"),
27388 pure_cons (make_pure_c_string ("@"),
27389 pure_cons (intern_c_string ("system-name"),
27390 Qnil)))),
27391 Qnil)));
27392
27393 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27394 doc: /* Maximum number of lines to keep in the message log buffer.
27395 If nil, disable message logging. If t, log messages but don't truncate
27396 the buffer when it becomes large. */);
27397 Vmessage_log_max = make_number (100);
27398
27399 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27400 doc: /* Functions called before redisplay, if window sizes have changed.
27401 The value should be a list of functions that take one argument.
27402 Just before redisplay, for each frame, if any of its windows have changed
27403 size since the last redisplay, or have been split or deleted,
27404 all the functions in the list are called, with the frame as argument. */);
27405 Vwindow_size_change_functions = Qnil;
27406
27407 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27408 doc: /* List of functions to call before redisplaying a window with scrolling.
27409 Each function is called with two arguments, the window and its new
27410 display-start position. Note that these functions are also called by
27411 `set-window-buffer'. Also note that the value of `window-end' is not
27412 valid when these functions are called. */);
27413 Vwindow_scroll_functions = Qnil;
27414
27415 DEFVAR_LISP ("window-text-change-functions",
27416 Vwindow_text_change_functions,
27417 doc: /* Functions to call in redisplay when text in the window might change. */);
27418 Vwindow_text_change_functions = Qnil;
27419
27420 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27421 doc: /* Functions called when redisplay of a window reaches the end trigger.
27422 Each function is called with two arguments, the window and the end trigger value.
27423 See `set-window-redisplay-end-trigger'. */);
27424 Vredisplay_end_trigger_functions = Qnil;
27425
27426 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27427 doc: /* *Non-nil means autoselect window with mouse pointer.
27428 If nil, do not autoselect windows.
27429 A positive number means delay autoselection by that many seconds: a
27430 window is autoselected only after the mouse has remained in that
27431 window for the duration of the delay.
27432 A negative number has a similar effect, but causes windows to be
27433 autoselected only after the mouse has stopped moving. \(Because of
27434 the way Emacs compares mouse events, you will occasionally wait twice
27435 that time before the window gets selected.\)
27436 Any other value means to autoselect window instantaneously when the
27437 mouse pointer enters it.
27438
27439 Autoselection selects the minibuffer only if it is active, and never
27440 unselects the minibuffer if it is active.
27441
27442 When customizing this variable make sure that the actual value of
27443 `focus-follows-mouse' matches the behavior of your window manager. */);
27444 Vmouse_autoselect_window = Qnil;
27445
27446 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27447 doc: /* *Non-nil means automatically resize tool-bars.
27448 This dynamically changes the tool-bar's height to the minimum height
27449 that is needed to make all tool-bar items visible.
27450 If value is `grow-only', the tool-bar's height is only increased
27451 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27452 Vauto_resize_tool_bars = Qt;
27453
27454 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27455 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27456 auto_raise_tool_bar_buttons_p = 1;
27457
27458 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27459 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27460 make_cursor_line_fully_visible_p = 1;
27461
27462 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27463 doc: /* *Border below tool-bar in pixels.
27464 If an integer, use it as the height of the border.
27465 If it is one of `internal-border-width' or `border-width', use the
27466 value of the corresponding frame parameter.
27467 Otherwise, no border is added below the tool-bar. */);
27468 Vtool_bar_border = Qinternal_border_width;
27469
27470 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27471 doc: /* *Margin around tool-bar buttons in pixels.
27472 If an integer, use that for both horizontal and vertical margins.
27473 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27474 HORZ specifying the horizontal margin, and VERT specifying the
27475 vertical margin. */);
27476 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27477
27478 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27479 doc: /* *Relief thickness of tool-bar buttons. */);
27480 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27481
27482 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27483 doc: /* Tool bar style to use.
27484 It can be one of
27485 image - show images only
27486 text - show text only
27487 both - show both, text below image
27488 both-horiz - show text to the right of the image
27489 text-image-horiz - show text to the left of the image
27490 any other - use system default or image if no system default. */);
27491 Vtool_bar_style = Qnil;
27492
27493 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27494 doc: /* *Maximum number of characters a label can have to be shown.
27495 The tool bar style must also show labels for this to have any effect, see
27496 `tool-bar-style'. */);
27497 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27498
27499 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27500 doc: /* List of functions to call to fontify regions of text.
27501 Each function is called with one argument POS. Functions must
27502 fontify a region starting at POS in the current buffer, and give
27503 fontified regions the property `fontified'. */);
27504 Vfontification_functions = Qnil;
27505 Fmake_variable_buffer_local (Qfontification_functions);
27506
27507 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27508 unibyte_display_via_language_environment,
27509 doc: /* *Non-nil means display unibyte text according to language environment.
27510 Specifically, this means that raw bytes in the range 160-255 decimal
27511 are displayed by converting them to the equivalent multibyte characters
27512 according to the current language environment. As a result, they are
27513 displayed according to the current fontset.
27514
27515 Note that this variable affects only how these bytes are displayed,
27516 but does not change the fact they are interpreted as raw bytes. */);
27517 unibyte_display_via_language_environment = 0;
27518
27519 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27520 doc: /* *Maximum height for resizing mini-windows.
27521 If a float, it specifies a fraction of the mini-window frame's height.
27522 If an integer, it specifies a number of lines. */);
27523 Vmax_mini_window_height = make_float (0.25);
27524
27525 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27526 doc: /* *How to resize mini-windows.
27527 A value of nil means don't automatically resize mini-windows.
27528 A value of t means resize them to fit the text displayed in them.
27529 A value of `grow-only', the default, means let mini-windows grow
27530 only, until their display becomes empty, at which point the windows
27531 go back to their normal size. */);
27532 Vresize_mini_windows = Qgrow_only;
27533
27534 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27535 doc: /* Alist specifying how to blink the cursor off.
27536 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27537 `cursor-type' frame-parameter or variable equals ON-STATE,
27538 comparing using `equal', Emacs uses OFF-STATE to specify
27539 how to blink it off. ON-STATE and OFF-STATE are values for
27540 the `cursor-type' frame parameter.
27541
27542 If a frame's ON-STATE has no entry in this list,
27543 the frame's other specifications determine how to blink the cursor off. */);
27544 Vblink_cursor_alist = Qnil;
27545
27546 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27547 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27548 If non-nil, windows are automatically scrolled horizontally to make
27549 point visible. */);
27550 automatic_hscrolling_p = 1;
27551 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
27552 staticpro (&Qauto_hscroll_mode);
27553
27554 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27555 doc: /* *How many columns away from the window edge point is allowed to get
27556 before automatic hscrolling will horizontally scroll the window. */);
27557 hscroll_margin = 5;
27558
27559 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27560 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27561 When point is less than `hscroll-margin' columns from the window
27562 edge, automatic hscrolling will scroll the window by the amount of columns
27563 determined by this variable. If its value is a positive integer, scroll that
27564 many columns. If it's a positive floating-point number, it specifies the
27565 fraction of the window's width to scroll. If it's nil or zero, point will be
27566 centered horizontally after the scroll. Any other value, including negative
27567 numbers, are treated as if the value were zero.
27568
27569 Automatic hscrolling always moves point outside the scroll margin, so if
27570 point was more than scroll step columns inside the margin, the window will
27571 scroll more than the value given by the scroll step.
27572
27573 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27574 and `scroll-right' overrides this variable's effect. */);
27575 Vhscroll_step = make_number (0);
27576
27577 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27578 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27579 Bind this around calls to `message' to let it take effect. */);
27580 message_truncate_lines = 0;
27581
27582 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27583 doc: /* Normal hook run to update the menu bar definitions.
27584 Redisplay runs this hook before it redisplays the menu bar.
27585 This is used to update submenus such as Buffers,
27586 whose contents depend on various data. */);
27587 Vmenu_bar_update_hook = Qnil;
27588
27589 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27590 doc: /* Frame for which we are updating a menu.
27591 The enable predicate for a menu binding should check this variable. */);
27592 Vmenu_updating_frame = Qnil;
27593
27594 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27595 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27596 inhibit_menubar_update = 0;
27597
27598 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27599 doc: /* Prefix prepended to all continuation lines at display time.
27600 The value may be a string, an image, or a stretch-glyph; it is
27601 interpreted in the same way as the value of a `display' text property.
27602
27603 This variable is overridden by any `wrap-prefix' text or overlay
27604 property.
27605
27606 To add a prefix to non-continuation lines, use `line-prefix'. */);
27607 Vwrap_prefix = Qnil;
27608 staticpro (&Qwrap_prefix);
27609 Qwrap_prefix = intern_c_string ("wrap-prefix");
27610 Fmake_variable_buffer_local (Qwrap_prefix);
27611
27612 DEFVAR_LISP ("line-prefix", Vline_prefix,
27613 doc: /* Prefix prepended to all non-continuation lines at display time.
27614 The value may be a string, an image, or a stretch-glyph; it is
27615 interpreted in the same way as the value of a `display' text property.
27616
27617 This variable is overridden by any `line-prefix' text or overlay
27618 property.
27619
27620 To add a prefix to continuation lines, use `wrap-prefix'. */);
27621 Vline_prefix = Qnil;
27622 staticpro (&Qline_prefix);
27623 Qline_prefix = intern_c_string ("line-prefix");
27624 Fmake_variable_buffer_local (Qline_prefix);
27625
27626 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27627 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27628 inhibit_eval_during_redisplay = 0;
27629
27630 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27631 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27632 inhibit_free_realized_faces = 0;
27633
27634 #if GLYPH_DEBUG
27635 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27636 doc: /* Inhibit try_window_id display optimization. */);
27637 inhibit_try_window_id = 0;
27638
27639 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27640 doc: /* Inhibit try_window_reusing display optimization. */);
27641 inhibit_try_window_reusing = 0;
27642
27643 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27644 doc: /* Inhibit try_cursor_movement display optimization. */);
27645 inhibit_try_cursor_movement = 0;
27646 #endif /* GLYPH_DEBUG */
27647
27648 DEFVAR_INT ("overline-margin", overline_margin,
27649 doc: /* *Space between overline and text, in pixels.
27650 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27651 margin to the caracter height. */);
27652 overline_margin = 2;
27653
27654 DEFVAR_INT ("underline-minimum-offset",
27655 underline_minimum_offset,
27656 doc: /* Minimum distance between baseline and underline.
27657 This can improve legibility of underlined text at small font sizes,
27658 particularly when using variable `x-use-underline-position-properties'
27659 with fonts that specify an UNDERLINE_POSITION relatively close to the
27660 baseline. The default value is 1. */);
27661 underline_minimum_offset = 1;
27662
27663 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27664 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27665 This feature only works when on a window system that can change
27666 cursor shapes. */);
27667 display_hourglass_p = 1;
27668
27669 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27670 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27671 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27672
27673 hourglass_atimer = NULL;
27674 hourglass_shown_p = 0;
27675
27676 DEFSYM (Qglyphless_char, "glyphless-char");
27677 DEFSYM (Qhex_code, "hex-code");
27678 DEFSYM (Qempty_box, "empty-box");
27679 DEFSYM (Qthin_space, "thin-space");
27680 DEFSYM (Qzero_width, "zero-width");
27681
27682 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27683 /* Intern this now in case it isn't already done.
27684 Setting this variable twice is harmless.
27685 But don't staticpro it here--that is done in alloc.c. */
27686 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27687 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27688
27689 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27690 doc: /* Char-table defining glyphless characters.
27691 Each element, if non-nil, should be one of the following:
27692 an ASCII acronym string: display this string in a box
27693 `hex-code': display the hexadecimal code of a character in a box
27694 `empty-box': display as an empty box
27695 `thin-space': display as 1-pixel width space
27696 `zero-width': don't display
27697 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27698 display method for graphical terminals and text terminals respectively.
27699 GRAPHICAL and TEXT should each have one of the values listed above.
27700
27701 The char-table has one extra slot to control the display of a character for
27702 which no font is found. This slot only takes effect on graphical terminals.
27703 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27704 `thin-space'. The default is `empty-box'. */);
27705 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27706 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27707 Qempty_box);
27708 }
27709
27710
27711 /* Initialize this module when Emacs starts. */
27712
27713 void
27714 init_xdisp (void)
27715 {
27716 Lisp_Object root_window;
27717 struct window *mini_w;
27718
27719 current_header_line_height = current_mode_line_height = -1;
27720
27721 CHARPOS (this_line_start_pos) = 0;
27722
27723 mini_w = XWINDOW (minibuf_window);
27724 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27725 echo_area_window = minibuf_window;
27726
27727 if (!noninteractive)
27728 {
27729 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27730 int i;
27731
27732 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27733 set_window_height (root_window,
27734 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27735 0);
27736 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27737 set_window_height (minibuf_window, 1, 0);
27738
27739 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27740 mini_w->total_cols = make_number (FRAME_COLS (f));
27741
27742 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27743 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27744 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27745
27746 /* The default ellipsis glyphs `...'. */
27747 for (i = 0; i < 3; ++i)
27748 default_invis_vector[i] = make_number ('.');
27749 }
27750
27751 {
27752 /* Allocate the buffer for frame titles.
27753 Also used for `format-mode-line'. */
27754 int size = 100;
27755 mode_line_noprop_buf = (char *) xmalloc (size);
27756 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27757 mode_line_noprop_ptr = mode_line_noprop_buf;
27758 mode_line_target = MODE_LINE_DISPLAY;
27759 }
27760
27761 help_echo_showing_p = 0;
27762 }
27763
27764 /* Since w32 does not support atimers, it defines its own implementation of
27765 the following three functions in w32fns.c. */
27766 #ifndef WINDOWSNT
27767
27768 /* Platform-independent portion of hourglass implementation. */
27769
27770 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27771 int
27772 hourglass_started (void)
27773 {
27774 return hourglass_shown_p || hourglass_atimer != NULL;
27775 }
27776
27777 /* Cancel a currently active hourglass timer, and start a new one. */
27778 void
27779 start_hourglass (void)
27780 {
27781 #if defined (HAVE_WINDOW_SYSTEM)
27782 EMACS_TIME delay;
27783 int secs, usecs = 0;
27784
27785 cancel_hourglass ();
27786
27787 if (INTEGERP (Vhourglass_delay)
27788 && XINT (Vhourglass_delay) > 0)
27789 secs = XFASTINT (Vhourglass_delay);
27790 else if (FLOATP (Vhourglass_delay)
27791 && XFLOAT_DATA (Vhourglass_delay) > 0)
27792 {
27793 Lisp_Object tem;
27794 tem = Ftruncate (Vhourglass_delay, Qnil);
27795 secs = XFASTINT (tem);
27796 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27797 }
27798 else
27799 secs = DEFAULT_HOURGLASS_DELAY;
27800
27801 EMACS_SET_SECS_USECS (delay, secs, usecs);
27802 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27803 show_hourglass, NULL);
27804 #endif
27805 }
27806
27807
27808 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27809 shown. */
27810 void
27811 cancel_hourglass (void)
27812 {
27813 #if defined (HAVE_WINDOW_SYSTEM)
27814 if (hourglass_atimer)
27815 {
27816 cancel_atimer (hourglass_atimer);
27817 hourglass_atimer = NULL;
27818 }
27819
27820 if (hourglass_shown_p)
27821 hide_hourglass ();
27822 #endif
27823 }
27824 #endif /* ! WINDOWSNT */