Merge from trunk.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator.
133 Calls to get_next_display_element fill the iterator structure with
134 relevant information about the next thing to display. Calls to
135 set_iterator_to_next move the iterator to the next thing.
136
137 Besides this, an iterator also contains information about the
138 display environment in which glyphs for display elements are to be
139 produced. It has fields for the width and height of the display,
140 the information whether long lines are truncated or continued, a
141 current X and Y position, and lots of other stuff you can better
142 see in dispextern.h.
143
144 Glyphs in a desired matrix are normally constructed in a loop
145 calling get_next_display_element and then PRODUCE_GLYPHS. The call
146 to PRODUCE_GLYPHS will fill the iterator structure with pixel
147 information about the element being displayed and at the same time
148 produce glyphs for it. If the display element fits on the line
149 being displayed, set_iterator_to_next is called next, otherwise the
150 glyphs produced are discarded. The function display_line is the
151 workhorse of filling glyph rows in the desired matrix with glyphs.
152 In addition to producing glyphs, it also handles line truncation
153 and continuation, word wrap, and cursor positioning (for the
154 latter, see also set_cursor_from_row).
155
156 Frame matrices.
157
158 That just couldn't be all, could it? What about terminal types not
159 supporting operations on sub-windows of the screen? To update the
160 display on such a terminal, window-based glyph matrices are not
161 well suited. To be able to reuse part of the display (scrolling
162 lines up and down), we must instead have a view of the whole
163 screen. This is what `frame matrices' are for. They are a trick.
164
165 Frames on terminals like above have a glyph pool. Windows on such
166 a frame sub-allocate their glyph memory from their frame's glyph
167 pool. The frame itself is given its own glyph matrices. By
168 coincidence---or maybe something else---rows in window glyph
169 matrices are slices of corresponding rows in frame matrices. Thus
170 writing to window matrices implicitly updates a frame matrix which
171 provides us with the view of the whole screen that we originally
172 wanted to have without having to move many bytes around. To be
173 honest, there is a little bit more done, but not much more. If you
174 plan to extend that code, take a look at dispnew.c. The function
175 build_frame_matrix is a good starting point.
176
177 Bidirectional display.
178
179 Bidirectional display adds quite some hair to this already complex
180 design. The good news are that a large portion of that hairy stuff
181 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
182 reordering engine which is called by set_iterator_to_next and
183 returns the next character to display in the visual order. See
184 commentary on bidi.c for more details. As far as redisplay is
185 concerned, the effect of calling bidi_move_to_visually_next, the
186 main interface of the reordering engine, is that the iterator gets
187 magically placed on the buffer or string position that is to be
188 displayed next. In other words, a linear iteration through the
189 buffer/string is replaced with a non-linear one. All the rest of
190 the redisplay is oblivious to the bidi reordering.
191
192 Well, almost oblivious---there are still complications, most of
193 them due to the fact that buffer and string positions no longer
194 change monotonously with glyph indices in a glyph row. Moreover,
195 for continued lines, the buffer positions may not even be
196 monotonously changing with vertical positions. Also, accounting
197 for face changes, overlays, etc. becomes more complex because
198 non-linear iteration could potentially skip many positions with
199 changes, and then cross them again on the way back...
200
201 One other prominent effect of bidirectional display is that some
202 paragraphs of text need to be displayed starting at the right
203 margin of the window---the so-called right-to-left, or R2L
204 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
205 which have their reversed_p flag set. The bidi reordering engine
206 produces characters in such rows starting from the character which
207 should be the rightmost on display. PRODUCE_GLYPHS then reverses
208 the order, when it fills up the glyph row whose reversed_p flag is
209 set, by prepending each new glyph to what is already there, instead
210 of appending it. When the glyph row is complete, the function
211 extend_face_to_end_of_line fills the empty space to the left of the
212 leftmost character with special glyphs, which will display as,
213 well, empty. On text terminals, these special glyphs are simply
214 blank characters. On graphics terminals, there's a single stretch
215 glyph of a suitably computed width. Both the blanks and the
216 stretch glyph are given the face of the background of the line.
217 This way, the terminal-specific back-end can still draw the glyphs
218 left to right, even for R2L lines.
219
220 Bidirectional display and character compositions
221
222 Some scripts cannot be displayed by drawing each character
223 individually, because adjacent characters change each other's shape
224 on display. For example, Arabic and Indic scripts belong to this
225 category.
226
227 Emacs display supports this by providing "character compositions",
228 most of which is implemented in composite.c. During the buffer
229 scan that delivers characters to PRODUCE_GLYPHS, if the next
230 character to be delivered is a composed character, the iteration
231 calls composition_reseat_it and next_element_from_composition. If
232 they succeed to compose the character with one or more of the
233 following characters, the whole sequence of characters that where
234 composed is recorded in the `struct composition_it' object that is
235 part of the buffer iterator. The composed sequence could produce
236 one or more font glyphs (called "grapheme clusters") on the screen.
237 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
238 in the direction corresponding to the current bidi scan direction
239 (recorded in the scan_dir member of the `struct bidi_it' object
240 that is part of the buffer iterator). In particular, if the bidi
241 iterator currently scans the buffer backwards, the grapheme
242 clusters are delivered back to front. This reorders the grapheme
243 clusters as appropriate for the current bidi context. Note that
244 this means that the grapheme clusters are always stored in the
245 LGSTRING object (see composite.c) in the logical order.
246
247 Moving an iterator in bidirectional text
248 without producing glyphs
249
250 Note one important detail mentioned above: that the bidi reordering
251 engine, driven by the iterator, produces characters in R2L rows
252 starting at the character that will be the rightmost on display.
253 As far as the iterator is concerned, the geometry of such rows is
254 still left to right, i.e. the iterator "thinks" the first character
255 is at the leftmost pixel position. The iterator does not know that
256 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
257 delivers. This is important when functions from the move_it_*
258 family are used to get to certain screen position or to match
259 screen coordinates with buffer coordinates: these functions use the
260 iterator geometry, which is left to right even in R2L paragraphs.
261 This works well with most callers of move_it_*, because they need
262 to get to a specific column, and columns are still numbered in the
263 reading order, i.e. the rightmost character in a R2L paragraph is
264 still column zero. But some callers do not get well with this; a
265 notable example is mouse clicks that need to find the character
266 that corresponds to certain pixel coordinates. See
267 buffer_posn_from_coords in dispnew.c for how this is handled. */
268
269 #include <config.h>
270 #include <stdio.h>
271 #include <limits.h>
272 #include <setjmp.h>
273
274 #include "lisp.h"
275 #include "keyboard.h"
276 #include "frame.h"
277 #include "window.h"
278 #include "termchar.h"
279 #include "dispextern.h"
280 #include "buffer.h"
281 #include "character.h"
282 #include "charset.h"
283 #include "indent.h"
284 #include "commands.h"
285 #include "keymap.h"
286 #include "macros.h"
287 #include "disptab.h"
288 #include "termhooks.h"
289 #include "termopts.h"
290 #include "intervals.h"
291 #include "coding.h"
292 #include "process.h"
293 #include "region-cache.h"
294 #include "font.h"
295 #include "fontset.h"
296 #include "blockinput.h"
297
298 #ifdef HAVE_X_WINDOWS
299 #include "xterm.h"
300 #endif
301 #ifdef WINDOWSNT
302 #include "w32term.h"
303 #endif
304 #ifdef HAVE_NS
305 #include "nsterm.h"
306 #endif
307 #ifdef USE_GTK
308 #include "gtkutil.h"
309 #endif
310
311 #include "font.h"
312
313 #ifndef FRAME_X_OUTPUT
314 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
315 #endif
316
317 #define INFINITY 10000000
318
319 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
320 Lisp_Object Qwindow_scroll_functions;
321 static Lisp_Object Qwindow_text_change_functions;
322 static Lisp_Object Qredisplay_end_trigger_functions;
323 Lisp_Object Qinhibit_point_motion_hooks;
324 static Lisp_Object QCeval, QCpropertize;
325 Lisp_Object QCfile, QCdata;
326 static Lisp_Object Qfontified;
327 static Lisp_Object Qgrow_only;
328 static Lisp_Object Qinhibit_eval_during_redisplay;
329 static Lisp_Object Qbuffer_position, Qposition, Qobject;
330 static Lisp_Object Qright_to_left, Qleft_to_right;
331
332 /* Cursor shapes */
333 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
334
335 /* Pointer shapes */
336 static Lisp_Object Qarrow, Qhand;
337 Lisp_Object Qtext;
338
339 /* Holds the list (error). */
340 static Lisp_Object list_of_error;
341
342 static Lisp_Object Qfontification_functions;
343
344 static Lisp_Object Qwrap_prefix;
345 static Lisp_Object Qline_prefix;
346
347 /* Non-nil means don't actually do any redisplay. */
348
349 Lisp_Object Qinhibit_redisplay;
350
351 /* Names of text properties relevant for redisplay. */
352
353 Lisp_Object Qdisplay;
354
355 Lisp_Object Qspace, QCalign_to;
356 static Lisp_Object QCrelative_width, QCrelative_height;
357 Lisp_Object Qleft_margin, Qright_margin;
358 static Lisp_Object Qspace_width, Qraise;
359 static Lisp_Object Qslice;
360 Lisp_Object Qcenter;
361 static Lisp_Object Qmargin, Qpointer;
362 static Lisp_Object Qline_height;
363
364 #ifdef HAVE_WINDOW_SYSTEM
365
366 /* Test if overflow newline into fringe. Called with iterator IT
367 at or past right window margin, and with IT->current_x set. */
368
369 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
370 (!NILP (Voverflow_newline_into_fringe) \
371 && FRAME_WINDOW_P ((IT)->f) \
372 && ((IT)->bidi_it.paragraph_dir == R2L \
373 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
374 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
375 && (IT)->current_x == (IT)->last_visible_x \
376 && (IT)->line_wrap != WORD_WRAP)
377
378 #else /* !HAVE_WINDOW_SYSTEM */
379 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
380 #endif /* HAVE_WINDOW_SYSTEM */
381
382 /* Test if the display element loaded in IT is a space or tab
383 character. This is used to determine word wrapping. */
384
385 #define IT_DISPLAYING_WHITESPACE(it) \
386 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
387
388 /* Name of the face used to highlight trailing whitespace. */
389
390 static Lisp_Object Qtrailing_whitespace;
391
392 /* Name and number of the face used to highlight escape glyphs. */
393
394 static Lisp_Object Qescape_glyph;
395
396 /* Name and number of the face used to highlight non-breaking spaces. */
397
398 static Lisp_Object Qnobreak_space;
399
400 /* The symbol `image' which is the car of the lists used to represent
401 images in Lisp. Also a tool bar style. */
402
403 Lisp_Object Qimage;
404
405 /* The image map types. */
406 Lisp_Object QCmap;
407 static Lisp_Object QCpointer;
408 static Lisp_Object Qrect, Qcircle, Qpoly;
409
410 /* Tool bar styles */
411 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
412
413 /* Non-zero means print newline to stdout before next mini-buffer
414 message. */
415
416 int noninteractive_need_newline;
417
418 /* Non-zero means print newline to message log before next message. */
419
420 static int message_log_need_newline;
421
422 /* Three markers that message_dolog uses.
423 It could allocate them itself, but that causes trouble
424 in handling memory-full errors. */
425 static Lisp_Object message_dolog_marker1;
426 static Lisp_Object message_dolog_marker2;
427 static Lisp_Object message_dolog_marker3;
428 \f
429 /* The buffer position of the first character appearing entirely or
430 partially on the line of the selected window which contains the
431 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
432 redisplay optimization in redisplay_internal. */
433
434 static struct text_pos this_line_start_pos;
435
436 /* Number of characters past the end of the line above, including the
437 terminating newline. */
438
439 static struct text_pos this_line_end_pos;
440
441 /* The vertical positions and the height of this line. */
442
443 static int this_line_vpos;
444 static int this_line_y;
445 static int this_line_pixel_height;
446
447 /* X position at which this display line starts. Usually zero;
448 negative if first character is partially visible. */
449
450 static int this_line_start_x;
451
452 /* The smallest character position seen by move_it_* functions as they
453 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
454 hscrolled lines, see display_line. */
455
456 static struct text_pos this_line_min_pos;
457
458 /* Buffer that this_line_.* variables are referring to. */
459
460 static struct buffer *this_line_buffer;
461
462
463 /* Values of those variables at last redisplay are stored as
464 properties on `overlay-arrow-position' symbol. However, if
465 Voverlay_arrow_position is a marker, last-arrow-position is its
466 numerical position. */
467
468 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
469
470 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
471 properties on a symbol in overlay-arrow-variable-list. */
472
473 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
474
475 Lisp_Object Qmenu_bar_update_hook;
476
477 /* Nonzero if an overlay arrow has been displayed in this window. */
478
479 static int overlay_arrow_seen;
480
481 /* Number of windows showing the buffer of the selected window (or
482 another buffer with the same base buffer). keyboard.c refers to
483 this. */
484
485 int buffer_shared;
486
487 /* Vector containing glyphs for an ellipsis `...'. */
488
489 static Lisp_Object default_invis_vector[3];
490
491 /* This is the window where the echo area message was displayed. It
492 is always a mini-buffer window, but it may not be the same window
493 currently active as a mini-buffer. */
494
495 Lisp_Object echo_area_window;
496
497 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
498 pushes the current message and the value of
499 message_enable_multibyte on the stack, the function restore_message
500 pops the stack and displays MESSAGE again. */
501
502 static Lisp_Object Vmessage_stack;
503
504 /* Nonzero means multibyte characters were enabled when the echo area
505 message was specified. */
506
507 static int message_enable_multibyte;
508
509 /* Nonzero if we should redraw the mode lines on the next redisplay. */
510
511 int update_mode_lines;
512
513 /* Nonzero if window sizes or contents have changed since last
514 redisplay that finished. */
515
516 int windows_or_buffers_changed;
517
518 /* Nonzero means a frame's cursor type has been changed. */
519
520 int cursor_type_changed;
521
522 /* Nonzero after display_mode_line if %l was used and it displayed a
523 line number. */
524
525 static int line_number_displayed;
526
527 /* The name of the *Messages* buffer, a string. */
528
529 static Lisp_Object Vmessages_buffer_name;
530
531 /* Current, index 0, and last displayed echo area message. Either
532 buffers from echo_buffers, or nil to indicate no message. */
533
534 Lisp_Object echo_area_buffer[2];
535
536 /* The buffers referenced from echo_area_buffer. */
537
538 static Lisp_Object echo_buffer[2];
539
540 /* A vector saved used in with_area_buffer to reduce consing. */
541
542 static Lisp_Object Vwith_echo_area_save_vector;
543
544 /* Non-zero means display_echo_area should display the last echo area
545 message again. Set by redisplay_preserve_echo_area. */
546
547 static int display_last_displayed_message_p;
548
549 /* Nonzero if echo area is being used by print; zero if being used by
550 message. */
551
552 static int message_buf_print;
553
554 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
555
556 static Lisp_Object Qinhibit_menubar_update;
557 static Lisp_Object Qmessage_truncate_lines;
558
559 /* Set to 1 in clear_message to make redisplay_internal aware
560 of an emptied echo area. */
561
562 static int message_cleared_p;
563
564 /* A scratch glyph row with contents used for generating truncation
565 glyphs. Also used in direct_output_for_insert. */
566
567 #define MAX_SCRATCH_GLYPHS 100
568 static struct glyph_row scratch_glyph_row;
569 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
570
571 /* Ascent and height of the last line processed by move_it_to. */
572
573 static int last_max_ascent, last_height;
574
575 /* Non-zero if there's a help-echo in the echo area. */
576
577 int help_echo_showing_p;
578
579 /* If >= 0, computed, exact values of mode-line and header-line height
580 to use in the macros CURRENT_MODE_LINE_HEIGHT and
581 CURRENT_HEADER_LINE_HEIGHT. */
582
583 int current_mode_line_height, current_header_line_height;
584
585 /* The maximum distance to look ahead for text properties. Values
586 that are too small let us call compute_char_face and similar
587 functions too often which is expensive. Values that are too large
588 let us call compute_char_face and alike too often because we
589 might not be interested in text properties that far away. */
590
591 #define TEXT_PROP_DISTANCE_LIMIT 100
592
593 #if GLYPH_DEBUG
594
595 /* Non-zero means print traces of redisplay if compiled with
596 GLYPH_DEBUG != 0. */
597
598 int trace_redisplay_p;
599
600 #endif /* GLYPH_DEBUG */
601
602 #ifdef DEBUG_TRACE_MOVE
603 /* Non-zero means trace with TRACE_MOVE to stderr. */
604 int trace_move;
605
606 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
607 #else
608 #define TRACE_MOVE(x) (void) 0
609 #endif
610
611 static Lisp_Object Qauto_hscroll_mode;
612
613 /* Buffer being redisplayed -- for redisplay_window_error. */
614
615 static struct buffer *displayed_buffer;
616
617 /* Value returned from text property handlers (see below). */
618
619 enum prop_handled
620 {
621 HANDLED_NORMALLY,
622 HANDLED_RECOMPUTE_PROPS,
623 HANDLED_OVERLAY_STRING_CONSUMED,
624 HANDLED_RETURN
625 };
626
627 /* A description of text properties that redisplay is interested
628 in. */
629
630 struct props
631 {
632 /* The name of the property. */
633 Lisp_Object *name;
634
635 /* A unique index for the property. */
636 enum prop_idx idx;
637
638 /* A handler function called to set up iterator IT from the property
639 at IT's current position. Value is used to steer handle_stop. */
640 enum prop_handled (*handler) (struct it *it);
641 };
642
643 static enum prop_handled handle_face_prop (struct it *);
644 static enum prop_handled handle_invisible_prop (struct it *);
645 static enum prop_handled handle_display_prop (struct it *);
646 static enum prop_handled handle_composition_prop (struct it *);
647 static enum prop_handled handle_overlay_change (struct it *);
648 static enum prop_handled handle_fontified_prop (struct it *);
649
650 /* Properties handled by iterators. */
651
652 static struct props it_props[] =
653 {
654 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
655 /* Handle `face' before `display' because some sub-properties of
656 `display' need to know the face. */
657 {&Qface, FACE_PROP_IDX, handle_face_prop},
658 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
659 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
660 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
661 {NULL, 0, NULL}
662 };
663
664 /* Value is the position described by X. If X is a marker, value is
665 the marker_position of X. Otherwise, value is X. */
666
667 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
668
669 /* Enumeration returned by some move_it_.* functions internally. */
670
671 enum move_it_result
672 {
673 /* Not used. Undefined value. */
674 MOVE_UNDEFINED,
675
676 /* Move ended at the requested buffer position or ZV. */
677 MOVE_POS_MATCH_OR_ZV,
678
679 /* Move ended at the requested X pixel position. */
680 MOVE_X_REACHED,
681
682 /* Move within a line ended at the end of a line that must be
683 continued. */
684 MOVE_LINE_CONTINUED,
685
686 /* Move within a line ended at the end of a line that would
687 be displayed truncated. */
688 MOVE_LINE_TRUNCATED,
689
690 /* Move within a line ended at a line end. */
691 MOVE_NEWLINE_OR_CR
692 };
693
694 /* This counter is used to clear the face cache every once in a while
695 in redisplay_internal. It is incremented for each redisplay.
696 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
697 cleared. */
698
699 #define CLEAR_FACE_CACHE_COUNT 500
700 static int clear_face_cache_count;
701
702 /* Similarly for the image cache. */
703
704 #ifdef HAVE_WINDOW_SYSTEM
705 #define CLEAR_IMAGE_CACHE_COUNT 101
706 static int clear_image_cache_count;
707
708 /* Null glyph slice */
709 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
710 #endif
711
712 /* Non-zero while redisplay_internal is in progress. */
713
714 int redisplaying_p;
715
716 static Lisp_Object Qinhibit_free_realized_faces;
717
718 /* If a string, XTread_socket generates an event to display that string.
719 (The display is done in read_char.) */
720
721 Lisp_Object help_echo_string;
722 Lisp_Object help_echo_window;
723 Lisp_Object help_echo_object;
724 EMACS_INT help_echo_pos;
725
726 /* Temporary variable for XTread_socket. */
727
728 Lisp_Object previous_help_echo_string;
729
730 /* Platform-independent portion of hourglass implementation. */
731
732 /* Non-zero means an hourglass cursor is currently shown. */
733 int hourglass_shown_p;
734
735 /* If non-null, an asynchronous timer that, when it expires, displays
736 an hourglass cursor on all frames. */
737 struct atimer *hourglass_atimer;
738
739 /* Name of the face used to display glyphless characters. */
740 Lisp_Object Qglyphless_char;
741
742 /* Symbol for the purpose of Vglyphless_char_display. */
743 static Lisp_Object Qglyphless_char_display;
744
745 /* Method symbols for Vglyphless_char_display. */
746 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
747
748 /* Default pixel width of `thin-space' display method. */
749 #define THIN_SPACE_WIDTH 1
750
751 /* Default number of seconds to wait before displaying an hourglass
752 cursor. */
753 #define DEFAULT_HOURGLASS_DELAY 1
754
755 \f
756 /* Function prototypes. */
757
758 static void setup_for_ellipsis (struct it *, int);
759 static void set_iterator_to_next (struct it *, int);
760 static void mark_window_display_accurate_1 (struct window *, int);
761 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
762 static int display_prop_string_p (Lisp_Object, Lisp_Object);
763 static int cursor_row_p (struct glyph_row *);
764 static int redisplay_mode_lines (Lisp_Object, int);
765 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
766
767 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
768
769 static void handle_line_prefix (struct it *);
770
771 static void pint2str (char *, int, EMACS_INT);
772 static void pint2hrstr (char *, int, EMACS_INT);
773 static struct text_pos run_window_scroll_functions (Lisp_Object,
774 struct text_pos);
775 static void reconsider_clip_changes (struct window *, struct buffer *);
776 static int text_outside_line_unchanged_p (struct window *,
777 EMACS_INT, EMACS_INT);
778 static void store_mode_line_noprop_char (char);
779 static int store_mode_line_noprop (const char *, int, int);
780 static void handle_stop (struct it *);
781 static void handle_stop_backwards (struct it *, EMACS_INT);
782 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
783 static void ensure_echo_area_buffers (void);
784 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
785 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
786 static int with_echo_area_buffer (struct window *, int,
787 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
788 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
789 static void clear_garbaged_frames (void);
790 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
791 static void pop_message (void);
792 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
793 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
794 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
795 static int display_echo_area (struct window *);
796 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
797 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
798 static Lisp_Object unwind_redisplay (Lisp_Object);
799 static int string_char_and_length (const unsigned char *, int *);
800 static struct text_pos display_prop_end (struct it *, Lisp_Object,
801 struct text_pos);
802 static int compute_window_start_on_continuation_line (struct window *);
803 static Lisp_Object safe_eval_handler (Lisp_Object);
804 static void insert_left_trunc_glyphs (struct it *);
805 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
806 Lisp_Object);
807 static void extend_face_to_end_of_line (struct it *);
808 static int append_space_for_newline (struct it *, int);
809 static int cursor_row_fully_visible_p (struct window *, int, int);
810 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
811 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
812 static int trailing_whitespace_p (EMACS_INT);
813 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
814 static void push_it (struct it *, struct text_pos *);
815 static void pop_it (struct it *);
816 static void sync_frame_with_window_matrix_rows (struct window *);
817 static void select_frame_for_redisplay (Lisp_Object);
818 static void redisplay_internal (void);
819 static int echo_area_display (int);
820 static void redisplay_windows (Lisp_Object);
821 static void redisplay_window (Lisp_Object, int);
822 static Lisp_Object redisplay_window_error (Lisp_Object);
823 static Lisp_Object redisplay_window_0 (Lisp_Object);
824 static Lisp_Object redisplay_window_1 (Lisp_Object);
825 static int set_cursor_from_row (struct window *, struct glyph_row *,
826 struct glyph_matrix *, EMACS_INT, EMACS_INT,
827 int, int);
828 static int update_menu_bar (struct frame *, int, int);
829 static int try_window_reusing_current_matrix (struct window *);
830 static int try_window_id (struct window *);
831 static int display_line (struct it *);
832 static int display_mode_lines (struct window *);
833 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
834 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
835 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
836 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
837 static void display_menu_bar (struct window *);
838 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
839 EMACS_INT *);
840 static int display_string (const char *, Lisp_Object, Lisp_Object,
841 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
842 static void compute_line_metrics (struct it *);
843 static void run_redisplay_end_trigger_hook (struct it *);
844 static int get_overlay_strings (struct it *, EMACS_INT);
845 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
846 static void next_overlay_string (struct it *);
847 static void reseat (struct it *, struct text_pos, int);
848 static void reseat_1 (struct it *, struct text_pos, int);
849 static void back_to_previous_visible_line_start (struct it *);
850 void reseat_at_previous_visible_line_start (struct it *);
851 static void reseat_at_next_visible_line_start (struct it *, int);
852 static int next_element_from_ellipsis (struct it *);
853 static int next_element_from_display_vector (struct it *);
854 static int next_element_from_string (struct it *);
855 static int next_element_from_c_string (struct it *);
856 static int next_element_from_buffer (struct it *);
857 static int next_element_from_composition (struct it *);
858 static int next_element_from_image (struct it *);
859 static int next_element_from_stretch (struct it *);
860 static void load_overlay_strings (struct it *, EMACS_INT);
861 static int init_from_display_pos (struct it *, struct window *,
862 struct display_pos *);
863 static void reseat_to_string (struct it *, const char *,
864 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
865 static int get_next_display_element (struct it *);
866 static enum move_it_result
867 move_it_in_display_line_to (struct it *, EMACS_INT, int,
868 enum move_operation_enum);
869 void move_it_vertically_backward (struct it *, int);
870 static void init_to_row_start (struct it *, struct window *,
871 struct glyph_row *);
872 static int init_to_row_end (struct it *, struct window *,
873 struct glyph_row *);
874 static void back_to_previous_line_start (struct it *);
875 static int forward_to_next_line_start (struct it *, int *);
876 static struct text_pos string_pos_nchars_ahead (struct text_pos,
877 Lisp_Object, EMACS_INT);
878 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
879 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
880 static EMACS_INT number_of_chars (const char *, int);
881 static void compute_stop_pos (struct it *);
882 static void compute_string_pos (struct text_pos *, struct text_pos,
883 Lisp_Object);
884 static int face_before_or_after_it_pos (struct it *, int);
885 static EMACS_INT next_overlay_change (EMACS_INT);
886 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
887 Lisp_Object, struct text_pos *, EMACS_INT, int);
888 static int handle_single_display_spec (struct it *, Lisp_Object,
889 Lisp_Object, Lisp_Object,
890 struct text_pos *, EMACS_INT, int, int);
891 static int underlying_face_id (struct it *);
892 static int in_ellipses_for_invisible_text_p (struct display_pos *,
893 struct window *);
894
895 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
896 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
897
898 #ifdef HAVE_WINDOW_SYSTEM
899
900 static void x_consider_frame_title (Lisp_Object);
901 static int tool_bar_lines_needed (struct frame *, int *);
902 static void update_tool_bar (struct frame *, int);
903 static void build_desired_tool_bar_string (struct frame *f);
904 static int redisplay_tool_bar (struct frame *);
905 static void display_tool_bar_line (struct it *, int);
906 static void notice_overwritten_cursor (struct window *,
907 enum glyph_row_area,
908 int, int, int, int);
909 static void append_stretch_glyph (struct it *, Lisp_Object,
910 int, int, int);
911
912
913 #endif /* HAVE_WINDOW_SYSTEM */
914
915 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
916 static int coords_in_mouse_face_p (struct window *, int, int);
917
918
919 \f
920 /***********************************************************************
921 Window display dimensions
922 ***********************************************************************/
923
924 /* Return the bottom boundary y-position for text lines in window W.
925 This is the first y position at which a line cannot start.
926 It is relative to the top of the window.
927
928 This is the height of W minus the height of a mode line, if any. */
929
930 inline int
931 window_text_bottom_y (struct window *w)
932 {
933 int height = WINDOW_TOTAL_HEIGHT (w);
934
935 if (WINDOW_WANTS_MODELINE_P (w))
936 height -= CURRENT_MODE_LINE_HEIGHT (w);
937 return height;
938 }
939
940 /* Return the pixel width of display area AREA of window W. AREA < 0
941 means return the total width of W, not including fringes to
942 the left and right of the window. */
943
944 inline int
945 window_box_width (struct window *w, int area)
946 {
947 int cols = XFASTINT (w->total_cols);
948 int pixels = 0;
949
950 if (!w->pseudo_window_p)
951 {
952 cols -= WINDOW_SCROLL_BAR_COLS (w);
953
954 if (area == TEXT_AREA)
955 {
956 if (INTEGERP (w->left_margin_cols))
957 cols -= XFASTINT (w->left_margin_cols);
958 if (INTEGERP (w->right_margin_cols))
959 cols -= XFASTINT (w->right_margin_cols);
960 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
961 }
962 else if (area == LEFT_MARGIN_AREA)
963 {
964 cols = (INTEGERP (w->left_margin_cols)
965 ? XFASTINT (w->left_margin_cols) : 0);
966 pixels = 0;
967 }
968 else if (area == RIGHT_MARGIN_AREA)
969 {
970 cols = (INTEGERP (w->right_margin_cols)
971 ? XFASTINT (w->right_margin_cols) : 0);
972 pixels = 0;
973 }
974 }
975
976 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
977 }
978
979
980 /* Return the pixel height of the display area of window W, not
981 including mode lines of W, if any. */
982
983 inline int
984 window_box_height (struct window *w)
985 {
986 struct frame *f = XFRAME (w->frame);
987 int height = WINDOW_TOTAL_HEIGHT (w);
988
989 xassert (height >= 0);
990
991 /* Note: the code below that determines the mode-line/header-line
992 height is essentially the same as that contained in the macro
993 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
994 the appropriate glyph row has its `mode_line_p' flag set,
995 and if it doesn't, uses estimate_mode_line_height instead. */
996
997 if (WINDOW_WANTS_MODELINE_P (w))
998 {
999 struct glyph_row *ml_row
1000 = (w->current_matrix && w->current_matrix->rows
1001 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1002 : 0);
1003 if (ml_row && ml_row->mode_line_p)
1004 height -= ml_row->height;
1005 else
1006 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1007 }
1008
1009 if (WINDOW_WANTS_HEADER_LINE_P (w))
1010 {
1011 struct glyph_row *hl_row
1012 = (w->current_matrix && w->current_matrix->rows
1013 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1014 : 0);
1015 if (hl_row && hl_row->mode_line_p)
1016 height -= hl_row->height;
1017 else
1018 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1019 }
1020
1021 /* With a very small font and a mode-line that's taller than
1022 default, we might end up with a negative height. */
1023 return max (0, height);
1024 }
1025
1026 /* Return the window-relative coordinate of the left edge of display
1027 area AREA of window W. AREA < 0 means return the left edge of the
1028 whole window, to the right of the left fringe of W. */
1029
1030 inline int
1031 window_box_left_offset (struct window *w, int area)
1032 {
1033 int x;
1034
1035 if (w->pseudo_window_p)
1036 return 0;
1037
1038 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1039
1040 if (area == TEXT_AREA)
1041 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1042 + window_box_width (w, LEFT_MARGIN_AREA));
1043 else if (area == RIGHT_MARGIN_AREA)
1044 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1045 + window_box_width (w, LEFT_MARGIN_AREA)
1046 + window_box_width (w, TEXT_AREA)
1047 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1048 ? 0
1049 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1050 else if (area == LEFT_MARGIN_AREA
1051 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1052 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1053
1054 return x;
1055 }
1056
1057
1058 /* Return the window-relative coordinate of the right edge of display
1059 area AREA of window W. AREA < 0 means return the right edge of the
1060 whole window, to the left of the right fringe of W. */
1061
1062 inline int
1063 window_box_right_offset (struct window *w, int area)
1064 {
1065 return window_box_left_offset (w, area) + window_box_width (w, area);
1066 }
1067
1068 /* Return the frame-relative coordinate of the left edge of display
1069 area AREA of window W. AREA < 0 means return the left edge of the
1070 whole window, to the right of the left fringe of W. */
1071
1072 inline int
1073 window_box_left (struct window *w, int area)
1074 {
1075 struct frame *f = XFRAME (w->frame);
1076 int x;
1077
1078 if (w->pseudo_window_p)
1079 return FRAME_INTERNAL_BORDER_WIDTH (f);
1080
1081 x = (WINDOW_LEFT_EDGE_X (w)
1082 + window_box_left_offset (w, area));
1083
1084 return x;
1085 }
1086
1087
1088 /* Return the frame-relative coordinate of the right edge of display
1089 area AREA of window W. AREA < 0 means return the right edge of the
1090 whole window, to the left of the right fringe of W. */
1091
1092 inline int
1093 window_box_right (struct window *w, int area)
1094 {
1095 return window_box_left (w, area) + window_box_width (w, area);
1096 }
1097
1098 /* Get the bounding box of the display area AREA of window W, without
1099 mode lines, in frame-relative coordinates. AREA < 0 means the
1100 whole window, not including the left and right fringes of
1101 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1102 coordinates of the upper-left corner of the box. Return in
1103 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1104
1105 inline void
1106 window_box (struct window *w, int area, int *box_x, int *box_y,
1107 int *box_width, int *box_height)
1108 {
1109 if (box_width)
1110 *box_width = window_box_width (w, area);
1111 if (box_height)
1112 *box_height = window_box_height (w);
1113 if (box_x)
1114 *box_x = window_box_left (w, area);
1115 if (box_y)
1116 {
1117 *box_y = WINDOW_TOP_EDGE_Y (w);
1118 if (WINDOW_WANTS_HEADER_LINE_P (w))
1119 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1120 }
1121 }
1122
1123
1124 /* Get the bounding box of the display area AREA of window W, without
1125 mode lines. AREA < 0 means the whole window, not including the
1126 left and right fringe of the window. Return in *TOP_LEFT_X
1127 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1128 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1129 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1130 box. */
1131
1132 static inline void
1133 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1134 int *bottom_right_x, int *bottom_right_y)
1135 {
1136 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1137 bottom_right_y);
1138 *bottom_right_x += *top_left_x;
1139 *bottom_right_y += *top_left_y;
1140 }
1141
1142
1143 \f
1144 /***********************************************************************
1145 Utilities
1146 ***********************************************************************/
1147
1148 /* Return the bottom y-position of the line the iterator IT is in.
1149 This can modify IT's settings. */
1150
1151 int
1152 line_bottom_y (struct it *it)
1153 {
1154 int line_height = it->max_ascent + it->max_descent;
1155 int line_top_y = it->current_y;
1156
1157 if (line_height == 0)
1158 {
1159 if (last_height)
1160 line_height = last_height;
1161 else if (IT_CHARPOS (*it) < ZV)
1162 {
1163 move_it_by_lines (it, 1);
1164 line_height = (it->max_ascent || it->max_descent
1165 ? it->max_ascent + it->max_descent
1166 : last_height);
1167 }
1168 else
1169 {
1170 struct glyph_row *row = it->glyph_row;
1171
1172 /* Use the default character height. */
1173 it->glyph_row = NULL;
1174 it->what = IT_CHARACTER;
1175 it->c = ' ';
1176 it->len = 1;
1177 PRODUCE_GLYPHS (it);
1178 line_height = it->ascent + it->descent;
1179 it->glyph_row = row;
1180 }
1181 }
1182
1183 return line_top_y + line_height;
1184 }
1185
1186
1187 /* Return 1 if position CHARPOS is visible in window W.
1188 CHARPOS < 0 means return info about WINDOW_END position.
1189 If visible, set *X and *Y to pixel coordinates of top left corner.
1190 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1191 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1192
1193 int
1194 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1195 int *rtop, int *rbot, int *rowh, int *vpos)
1196 {
1197 struct it it;
1198 struct text_pos top;
1199 int visible_p = 0;
1200 struct buffer *old_buffer = NULL;
1201
1202 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1203 return visible_p;
1204
1205 if (XBUFFER (w->buffer) != current_buffer)
1206 {
1207 old_buffer = current_buffer;
1208 set_buffer_internal_1 (XBUFFER (w->buffer));
1209 }
1210
1211 SET_TEXT_POS_FROM_MARKER (top, w->start);
1212
1213 /* Compute exact mode line heights. */
1214 if (WINDOW_WANTS_MODELINE_P (w))
1215 current_mode_line_height
1216 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1217 BVAR (current_buffer, mode_line_format));
1218
1219 if (WINDOW_WANTS_HEADER_LINE_P (w))
1220 current_header_line_height
1221 = display_mode_line (w, HEADER_LINE_FACE_ID,
1222 BVAR (current_buffer, header_line_format));
1223
1224 start_display (&it, w, top);
1225 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1226 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1227
1228 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1229 {
1230 /* We have reached CHARPOS, or passed it. How the call to
1231 move_it_to can overshoot: (i) If CHARPOS is on invisible
1232 text, move_it_to stops at the end of the invisible text,
1233 after CHARPOS. (ii) If CHARPOS is in a display vector,
1234 move_it_to stops on its last glyph. */
1235 int top_x = it.current_x;
1236 int top_y = it.current_y;
1237 enum it_method it_method = it.method;
1238 /* Calling line_bottom_y may change it.method, it.position, etc. */
1239 int bottom_y = (last_height = 0, line_bottom_y (&it));
1240 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1241
1242 if (top_y < window_top_y)
1243 visible_p = bottom_y > window_top_y;
1244 else if (top_y < it.last_visible_y)
1245 visible_p = 1;
1246 if (visible_p)
1247 {
1248 if (it_method == GET_FROM_DISPLAY_VECTOR)
1249 {
1250 /* We stopped on the last glyph of a display vector.
1251 Try and recompute. Hack alert! */
1252 if (charpos < 2 || top.charpos >= charpos)
1253 top_x = it.glyph_row->x;
1254 else
1255 {
1256 struct it it2;
1257 start_display (&it2, w, top);
1258 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1259 get_next_display_element (&it2);
1260 PRODUCE_GLYPHS (&it2);
1261 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1262 || it2.current_x > it2.last_visible_x)
1263 top_x = it.glyph_row->x;
1264 else
1265 {
1266 top_x = it2.current_x;
1267 top_y = it2.current_y;
1268 }
1269 }
1270 }
1271
1272 *x = top_x;
1273 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1274 *rtop = max (0, window_top_y - top_y);
1275 *rbot = max (0, bottom_y - it.last_visible_y);
1276 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1277 - max (top_y, window_top_y)));
1278 *vpos = it.vpos;
1279 }
1280 }
1281 else
1282 {
1283 struct it it2;
1284
1285 it2 = it;
1286 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1287 move_it_by_lines (&it, 1);
1288 if (charpos < IT_CHARPOS (it)
1289 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1290 {
1291 visible_p = 1;
1292 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1293 *x = it2.current_x;
1294 *y = it2.current_y + it2.max_ascent - it2.ascent;
1295 *rtop = max (0, -it2.current_y);
1296 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1297 - it.last_visible_y));
1298 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1299 it.last_visible_y)
1300 - max (it2.current_y,
1301 WINDOW_HEADER_LINE_HEIGHT (w))));
1302 *vpos = it2.vpos;
1303 }
1304 }
1305
1306 if (old_buffer)
1307 set_buffer_internal_1 (old_buffer);
1308
1309 current_header_line_height = current_mode_line_height = -1;
1310
1311 if (visible_p && XFASTINT (w->hscroll) > 0)
1312 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1313
1314 #if 0
1315 /* Debugging code. */
1316 if (visible_p)
1317 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1318 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1319 else
1320 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1321 #endif
1322
1323 return visible_p;
1324 }
1325
1326
1327 /* Return the next character from STR. Return in *LEN the length of
1328 the character. This is like STRING_CHAR_AND_LENGTH but never
1329 returns an invalid character. If we find one, we return a `?', but
1330 with the length of the invalid character. */
1331
1332 static inline int
1333 string_char_and_length (const unsigned char *str, int *len)
1334 {
1335 int c;
1336
1337 c = STRING_CHAR_AND_LENGTH (str, *len);
1338 if (!CHAR_VALID_P (c))
1339 /* We may not change the length here because other places in Emacs
1340 don't use this function, i.e. they silently accept invalid
1341 characters. */
1342 c = '?';
1343
1344 return c;
1345 }
1346
1347
1348
1349 /* Given a position POS containing a valid character and byte position
1350 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1351
1352 static struct text_pos
1353 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1354 {
1355 xassert (STRINGP (string) && nchars >= 0);
1356
1357 if (STRING_MULTIBYTE (string))
1358 {
1359 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1360 int len;
1361
1362 while (nchars--)
1363 {
1364 string_char_and_length (p, &len);
1365 p += len;
1366 CHARPOS (pos) += 1;
1367 BYTEPOS (pos) += len;
1368 }
1369 }
1370 else
1371 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1372
1373 return pos;
1374 }
1375
1376
1377 /* Value is the text position, i.e. character and byte position,
1378 for character position CHARPOS in STRING. */
1379
1380 static inline struct text_pos
1381 string_pos (EMACS_INT charpos, Lisp_Object string)
1382 {
1383 struct text_pos pos;
1384 xassert (STRINGP (string));
1385 xassert (charpos >= 0);
1386 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1387 return pos;
1388 }
1389
1390
1391 /* Value is a text position, i.e. character and byte position, for
1392 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1393 means recognize multibyte characters. */
1394
1395 static struct text_pos
1396 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1397 {
1398 struct text_pos pos;
1399
1400 xassert (s != NULL);
1401 xassert (charpos >= 0);
1402
1403 if (multibyte_p)
1404 {
1405 int len;
1406
1407 SET_TEXT_POS (pos, 0, 0);
1408 while (charpos--)
1409 {
1410 string_char_and_length ((const unsigned char *) s, &len);
1411 s += len;
1412 CHARPOS (pos) += 1;
1413 BYTEPOS (pos) += len;
1414 }
1415 }
1416 else
1417 SET_TEXT_POS (pos, charpos, charpos);
1418
1419 return pos;
1420 }
1421
1422
1423 /* Value is the number of characters in C string S. MULTIBYTE_P
1424 non-zero means recognize multibyte characters. */
1425
1426 static EMACS_INT
1427 number_of_chars (const char *s, int multibyte_p)
1428 {
1429 EMACS_INT nchars;
1430
1431 if (multibyte_p)
1432 {
1433 EMACS_INT rest = strlen (s);
1434 int len;
1435 const unsigned char *p = (const unsigned char *) s;
1436
1437 for (nchars = 0; rest > 0; ++nchars)
1438 {
1439 string_char_and_length (p, &len);
1440 rest -= len, p += len;
1441 }
1442 }
1443 else
1444 nchars = strlen (s);
1445
1446 return nchars;
1447 }
1448
1449
1450 /* Compute byte position NEWPOS->bytepos corresponding to
1451 NEWPOS->charpos. POS is a known position in string STRING.
1452 NEWPOS->charpos must be >= POS.charpos. */
1453
1454 static void
1455 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1456 {
1457 xassert (STRINGP (string));
1458 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1459
1460 if (STRING_MULTIBYTE (string))
1461 *newpos = string_pos_nchars_ahead (pos, string,
1462 CHARPOS (*newpos) - CHARPOS (pos));
1463 else
1464 BYTEPOS (*newpos) = CHARPOS (*newpos);
1465 }
1466
1467 /* EXPORT:
1468 Return an estimation of the pixel height of mode or header lines on
1469 frame F. FACE_ID specifies what line's height to estimate. */
1470
1471 int
1472 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1473 {
1474 #ifdef HAVE_WINDOW_SYSTEM
1475 if (FRAME_WINDOW_P (f))
1476 {
1477 int height = FONT_HEIGHT (FRAME_FONT (f));
1478
1479 /* This function is called so early when Emacs starts that the face
1480 cache and mode line face are not yet initialized. */
1481 if (FRAME_FACE_CACHE (f))
1482 {
1483 struct face *face = FACE_FROM_ID (f, face_id);
1484 if (face)
1485 {
1486 if (face->font)
1487 height = FONT_HEIGHT (face->font);
1488 if (face->box_line_width > 0)
1489 height += 2 * face->box_line_width;
1490 }
1491 }
1492
1493 return height;
1494 }
1495 #endif
1496
1497 return 1;
1498 }
1499
1500 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1501 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1502 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1503 not force the value into range. */
1504
1505 void
1506 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1507 int *x, int *y, NativeRectangle *bounds, int noclip)
1508 {
1509
1510 #ifdef HAVE_WINDOW_SYSTEM
1511 if (FRAME_WINDOW_P (f))
1512 {
1513 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1514 even for negative values. */
1515 if (pix_x < 0)
1516 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1517 if (pix_y < 0)
1518 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1519
1520 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1521 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1522
1523 if (bounds)
1524 STORE_NATIVE_RECT (*bounds,
1525 FRAME_COL_TO_PIXEL_X (f, pix_x),
1526 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1527 FRAME_COLUMN_WIDTH (f) - 1,
1528 FRAME_LINE_HEIGHT (f) - 1);
1529
1530 if (!noclip)
1531 {
1532 if (pix_x < 0)
1533 pix_x = 0;
1534 else if (pix_x > FRAME_TOTAL_COLS (f))
1535 pix_x = FRAME_TOTAL_COLS (f);
1536
1537 if (pix_y < 0)
1538 pix_y = 0;
1539 else if (pix_y > FRAME_LINES (f))
1540 pix_y = FRAME_LINES (f);
1541 }
1542 }
1543 #endif
1544
1545 *x = pix_x;
1546 *y = pix_y;
1547 }
1548
1549
1550 /* Find the glyph under window-relative coordinates X/Y in window W.
1551 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1552 strings. Return in *HPOS and *VPOS the row and column number of
1553 the glyph found. Return in *AREA the glyph area containing X.
1554 Value is a pointer to the glyph found or null if X/Y is not on
1555 text, or we can't tell because W's current matrix is not up to
1556 date. */
1557
1558 static
1559 struct glyph *
1560 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1561 int *dx, int *dy, int *area)
1562 {
1563 struct glyph *glyph, *end;
1564 struct glyph_row *row = NULL;
1565 int x0, i;
1566
1567 /* Find row containing Y. Give up if some row is not enabled. */
1568 for (i = 0; i < w->current_matrix->nrows; ++i)
1569 {
1570 row = MATRIX_ROW (w->current_matrix, i);
1571 if (!row->enabled_p)
1572 return NULL;
1573 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1574 break;
1575 }
1576
1577 *vpos = i;
1578 *hpos = 0;
1579
1580 /* Give up if Y is not in the window. */
1581 if (i == w->current_matrix->nrows)
1582 return NULL;
1583
1584 /* Get the glyph area containing X. */
1585 if (w->pseudo_window_p)
1586 {
1587 *area = TEXT_AREA;
1588 x0 = 0;
1589 }
1590 else
1591 {
1592 if (x < window_box_left_offset (w, TEXT_AREA))
1593 {
1594 *area = LEFT_MARGIN_AREA;
1595 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1596 }
1597 else if (x < window_box_right_offset (w, TEXT_AREA))
1598 {
1599 *area = TEXT_AREA;
1600 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1601 }
1602 else
1603 {
1604 *area = RIGHT_MARGIN_AREA;
1605 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1606 }
1607 }
1608
1609 /* Find glyph containing X. */
1610 glyph = row->glyphs[*area];
1611 end = glyph + row->used[*area];
1612 x -= x0;
1613 while (glyph < end && x >= glyph->pixel_width)
1614 {
1615 x -= glyph->pixel_width;
1616 ++glyph;
1617 }
1618
1619 if (glyph == end)
1620 return NULL;
1621
1622 if (dx)
1623 {
1624 *dx = x;
1625 *dy = y - (row->y + row->ascent - glyph->ascent);
1626 }
1627
1628 *hpos = glyph - row->glyphs[*area];
1629 return glyph;
1630 }
1631
1632 /* Convert frame-relative x/y to coordinates relative to window W.
1633 Takes pseudo-windows into account. */
1634
1635 static void
1636 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1637 {
1638 if (w->pseudo_window_p)
1639 {
1640 /* A pseudo-window is always full-width, and starts at the
1641 left edge of the frame, plus a frame border. */
1642 struct frame *f = XFRAME (w->frame);
1643 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1644 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1645 }
1646 else
1647 {
1648 *x -= WINDOW_LEFT_EDGE_X (w);
1649 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1650 }
1651 }
1652
1653 #ifdef HAVE_WINDOW_SYSTEM
1654
1655 /* EXPORT:
1656 Return in RECTS[] at most N clipping rectangles for glyph string S.
1657 Return the number of stored rectangles. */
1658
1659 int
1660 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1661 {
1662 XRectangle r;
1663
1664 if (n <= 0)
1665 return 0;
1666
1667 if (s->row->full_width_p)
1668 {
1669 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1670 r.x = WINDOW_LEFT_EDGE_X (s->w);
1671 r.width = WINDOW_TOTAL_WIDTH (s->w);
1672
1673 /* Unless displaying a mode or menu bar line, which are always
1674 fully visible, clip to the visible part of the row. */
1675 if (s->w->pseudo_window_p)
1676 r.height = s->row->visible_height;
1677 else
1678 r.height = s->height;
1679 }
1680 else
1681 {
1682 /* This is a text line that may be partially visible. */
1683 r.x = window_box_left (s->w, s->area);
1684 r.width = window_box_width (s->w, s->area);
1685 r.height = s->row->visible_height;
1686 }
1687
1688 if (s->clip_head)
1689 if (r.x < s->clip_head->x)
1690 {
1691 if (r.width >= s->clip_head->x - r.x)
1692 r.width -= s->clip_head->x - r.x;
1693 else
1694 r.width = 0;
1695 r.x = s->clip_head->x;
1696 }
1697 if (s->clip_tail)
1698 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1699 {
1700 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1701 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1702 else
1703 r.width = 0;
1704 }
1705
1706 /* If S draws overlapping rows, it's sufficient to use the top and
1707 bottom of the window for clipping because this glyph string
1708 intentionally draws over other lines. */
1709 if (s->for_overlaps)
1710 {
1711 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1712 r.height = window_text_bottom_y (s->w) - r.y;
1713
1714 /* Alas, the above simple strategy does not work for the
1715 environments with anti-aliased text: if the same text is
1716 drawn onto the same place multiple times, it gets thicker.
1717 If the overlap we are processing is for the erased cursor, we
1718 take the intersection with the rectagle of the cursor. */
1719 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1720 {
1721 XRectangle rc, r_save = r;
1722
1723 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1724 rc.y = s->w->phys_cursor.y;
1725 rc.width = s->w->phys_cursor_width;
1726 rc.height = s->w->phys_cursor_height;
1727
1728 x_intersect_rectangles (&r_save, &rc, &r);
1729 }
1730 }
1731 else
1732 {
1733 /* Don't use S->y for clipping because it doesn't take partially
1734 visible lines into account. For example, it can be negative for
1735 partially visible lines at the top of a window. */
1736 if (!s->row->full_width_p
1737 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1738 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1739 else
1740 r.y = max (0, s->row->y);
1741 }
1742
1743 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1744
1745 /* If drawing the cursor, don't let glyph draw outside its
1746 advertised boundaries. Cleartype does this under some circumstances. */
1747 if (s->hl == DRAW_CURSOR)
1748 {
1749 struct glyph *glyph = s->first_glyph;
1750 int height, max_y;
1751
1752 if (s->x > r.x)
1753 {
1754 r.width -= s->x - r.x;
1755 r.x = s->x;
1756 }
1757 r.width = min (r.width, glyph->pixel_width);
1758
1759 /* If r.y is below window bottom, ensure that we still see a cursor. */
1760 height = min (glyph->ascent + glyph->descent,
1761 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1762 max_y = window_text_bottom_y (s->w) - height;
1763 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1764 if (s->ybase - glyph->ascent > max_y)
1765 {
1766 r.y = max_y;
1767 r.height = height;
1768 }
1769 else
1770 {
1771 /* Don't draw cursor glyph taller than our actual glyph. */
1772 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1773 if (height < r.height)
1774 {
1775 max_y = r.y + r.height;
1776 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1777 r.height = min (max_y - r.y, height);
1778 }
1779 }
1780 }
1781
1782 if (s->row->clip)
1783 {
1784 XRectangle r_save = r;
1785
1786 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1787 r.width = 0;
1788 }
1789
1790 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1791 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1792 {
1793 #ifdef CONVERT_FROM_XRECT
1794 CONVERT_FROM_XRECT (r, *rects);
1795 #else
1796 *rects = r;
1797 #endif
1798 return 1;
1799 }
1800 else
1801 {
1802 /* If we are processing overlapping and allowed to return
1803 multiple clipping rectangles, we exclude the row of the glyph
1804 string from the clipping rectangle. This is to avoid drawing
1805 the same text on the environment with anti-aliasing. */
1806 #ifdef CONVERT_FROM_XRECT
1807 XRectangle rs[2];
1808 #else
1809 XRectangle *rs = rects;
1810 #endif
1811 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1812
1813 if (s->for_overlaps & OVERLAPS_PRED)
1814 {
1815 rs[i] = r;
1816 if (r.y + r.height > row_y)
1817 {
1818 if (r.y < row_y)
1819 rs[i].height = row_y - r.y;
1820 else
1821 rs[i].height = 0;
1822 }
1823 i++;
1824 }
1825 if (s->for_overlaps & OVERLAPS_SUCC)
1826 {
1827 rs[i] = r;
1828 if (r.y < row_y + s->row->visible_height)
1829 {
1830 if (r.y + r.height > row_y + s->row->visible_height)
1831 {
1832 rs[i].y = row_y + s->row->visible_height;
1833 rs[i].height = r.y + r.height - rs[i].y;
1834 }
1835 else
1836 rs[i].height = 0;
1837 }
1838 i++;
1839 }
1840
1841 n = i;
1842 #ifdef CONVERT_FROM_XRECT
1843 for (i = 0; i < n; i++)
1844 CONVERT_FROM_XRECT (rs[i], rects[i]);
1845 #endif
1846 return n;
1847 }
1848 }
1849
1850 /* EXPORT:
1851 Return in *NR the clipping rectangle for glyph string S. */
1852
1853 void
1854 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1855 {
1856 get_glyph_string_clip_rects (s, nr, 1);
1857 }
1858
1859
1860 /* EXPORT:
1861 Return the position and height of the phys cursor in window W.
1862 Set w->phys_cursor_width to width of phys cursor.
1863 */
1864
1865 void
1866 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1867 struct glyph *glyph, int *xp, int *yp, int *heightp)
1868 {
1869 struct frame *f = XFRAME (WINDOW_FRAME (w));
1870 int x, y, wd, h, h0, y0;
1871
1872 /* Compute the width of the rectangle to draw. If on a stretch
1873 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1874 rectangle as wide as the glyph, but use a canonical character
1875 width instead. */
1876 wd = glyph->pixel_width - 1;
1877 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1878 wd++; /* Why? */
1879 #endif
1880
1881 x = w->phys_cursor.x;
1882 if (x < 0)
1883 {
1884 wd += x;
1885 x = 0;
1886 }
1887
1888 if (glyph->type == STRETCH_GLYPH
1889 && !x_stretch_cursor_p)
1890 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1891 w->phys_cursor_width = wd;
1892
1893 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1894
1895 /* If y is below window bottom, ensure that we still see a cursor. */
1896 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1897
1898 h = max (h0, glyph->ascent + glyph->descent);
1899 h0 = min (h0, glyph->ascent + glyph->descent);
1900
1901 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1902 if (y < y0)
1903 {
1904 h = max (h - (y0 - y) + 1, h0);
1905 y = y0 - 1;
1906 }
1907 else
1908 {
1909 y0 = window_text_bottom_y (w) - h0;
1910 if (y > y0)
1911 {
1912 h += y - y0;
1913 y = y0;
1914 }
1915 }
1916
1917 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1918 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1919 *heightp = h;
1920 }
1921
1922 /*
1923 * Remember which glyph the mouse is over.
1924 */
1925
1926 void
1927 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1928 {
1929 Lisp_Object window;
1930 struct window *w;
1931 struct glyph_row *r, *gr, *end_row;
1932 enum window_part part;
1933 enum glyph_row_area area;
1934 int x, y, width, height;
1935
1936 /* Try to determine frame pixel position and size of the glyph under
1937 frame pixel coordinates X/Y on frame F. */
1938
1939 if (!f->glyphs_initialized_p
1940 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1941 NILP (window)))
1942 {
1943 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1944 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1945 goto virtual_glyph;
1946 }
1947
1948 w = XWINDOW (window);
1949 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1950 height = WINDOW_FRAME_LINE_HEIGHT (w);
1951
1952 x = window_relative_x_coord (w, part, gx);
1953 y = gy - WINDOW_TOP_EDGE_Y (w);
1954
1955 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1956 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1957
1958 if (w->pseudo_window_p)
1959 {
1960 area = TEXT_AREA;
1961 part = ON_MODE_LINE; /* Don't adjust margin. */
1962 goto text_glyph;
1963 }
1964
1965 switch (part)
1966 {
1967 case ON_LEFT_MARGIN:
1968 area = LEFT_MARGIN_AREA;
1969 goto text_glyph;
1970
1971 case ON_RIGHT_MARGIN:
1972 area = RIGHT_MARGIN_AREA;
1973 goto text_glyph;
1974
1975 case ON_HEADER_LINE:
1976 case ON_MODE_LINE:
1977 gr = (part == ON_HEADER_LINE
1978 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1979 : MATRIX_MODE_LINE_ROW (w->current_matrix));
1980 gy = gr->y;
1981 area = TEXT_AREA;
1982 goto text_glyph_row_found;
1983
1984 case ON_TEXT:
1985 area = TEXT_AREA;
1986
1987 text_glyph:
1988 gr = 0; gy = 0;
1989 for (; r <= end_row && r->enabled_p; ++r)
1990 if (r->y + r->height > y)
1991 {
1992 gr = r; gy = r->y;
1993 break;
1994 }
1995
1996 text_glyph_row_found:
1997 if (gr && gy <= y)
1998 {
1999 struct glyph *g = gr->glyphs[area];
2000 struct glyph *end = g + gr->used[area];
2001
2002 height = gr->height;
2003 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2004 if (gx + g->pixel_width > x)
2005 break;
2006
2007 if (g < end)
2008 {
2009 if (g->type == IMAGE_GLYPH)
2010 {
2011 /* Don't remember when mouse is over image, as
2012 image may have hot-spots. */
2013 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2014 return;
2015 }
2016 width = g->pixel_width;
2017 }
2018 else
2019 {
2020 /* Use nominal char spacing at end of line. */
2021 x -= gx;
2022 gx += (x / width) * width;
2023 }
2024
2025 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2026 gx += window_box_left_offset (w, area);
2027 }
2028 else
2029 {
2030 /* Use nominal line height at end of window. */
2031 gx = (x / width) * width;
2032 y -= gy;
2033 gy += (y / height) * height;
2034 }
2035 break;
2036
2037 case ON_LEFT_FRINGE:
2038 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2039 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2040 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2041 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2042 goto row_glyph;
2043
2044 case ON_RIGHT_FRINGE:
2045 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2046 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2047 : window_box_right_offset (w, TEXT_AREA));
2048 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2049 goto row_glyph;
2050
2051 case ON_SCROLL_BAR:
2052 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2053 ? 0
2054 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2055 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2056 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2057 : 0)));
2058 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2059
2060 row_glyph:
2061 gr = 0, gy = 0;
2062 for (; r <= end_row && r->enabled_p; ++r)
2063 if (r->y + r->height > y)
2064 {
2065 gr = r; gy = r->y;
2066 break;
2067 }
2068
2069 if (gr && gy <= y)
2070 height = gr->height;
2071 else
2072 {
2073 /* Use nominal line height at end of window. */
2074 y -= gy;
2075 gy += (y / height) * height;
2076 }
2077 break;
2078
2079 default:
2080 ;
2081 virtual_glyph:
2082 /* If there is no glyph under the mouse, then we divide the screen
2083 into a grid of the smallest glyph in the frame, and use that
2084 as our "glyph". */
2085
2086 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2087 round down even for negative values. */
2088 if (gx < 0)
2089 gx -= width - 1;
2090 if (gy < 0)
2091 gy -= height - 1;
2092
2093 gx = (gx / width) * width;
2094 gy = (gy / height) * height;
2095
2096 goto store_rect;
2097 }
2098
2099 gx += WINDOW_LEFT_EDGE_X (w);
2100 gy += WINDOW_TOP_EDGE_Y (w);
2101
2102 store_rect:
2103 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2104
2105 /* Visible feedback for debugging. */
2106 #if 0
2107 #if HAVE_X_WINDOWS
2108 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2109 f->output_data.x->normal_gc,
2110 gx, gy, width, height);
2111 #endif
2112 #endif
2113 }
2114
2115
2116 #endif /* HAVE_WINDOW_SYSTEM */
2117
2118 \f
2119 /***********************************************************************
2120 Lisp form evaluation
2121 ***********************************************************************/
2122
2123 /* Error handler for safe_eval and safe_call. */
2124
2125 static Lisp_Object
2126 safe_eval_handler (Lisp_Object arg)
2127 {
2128 add_to_log ("Error during redisplay: %S", arg, Qnil);
2129 return Qnil;
2130 }
2131
2132
2133 /* Evaluate SEXPR and return the result, or nil if something went
2134 wrong. Prevent redisplay during the evaluation. */
2135
2136 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2137 Return the result, or nil if something went wrong. Prevent
2138 redisplay during the evaluation. */
2139
2140 Lisp_Object
2141 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2142 {
2143 Lisp_Object val;
2144
2145 if (inhibit_eval_during_redisplay)
2146 val = Qnil;
2147 else
2148 {
2149 int count = SPECPDL_INDEX ();
2150 struct gcpro gcpro1;
2151
2152 GCPRO1 (args[0]);
2153 gcpro1.nvars = nargs;
2154 specbind (Qinhibit_redisplay, Qt);
2155 /* Use Qt to ensure debugger does not run,
2156 so there is no possibility of wanting to redisplay. */
2157 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2158 safe_eval_handler);
2159 UNGCPRO;
2160 val = unbind_to (count, val);
2161 }
2162
2163 return val;
2164 }
2165
2166
2167 /* Call function FN with one argument ARG.
2168 Return the result, or nil if something went wrong. */
2169
2170 Lisp_Object
2171 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2172 {
2173 Lisp_Object args[2];
2174 args[0] = fn;
2175 args[1] = arg;
2176 return safe_call (2, args);
2177 }
2178
2179 static Lisp_Object Qeval;
2180
2181 Lisp_Object
2182 safe_eval (Lisp_Object sexpr)
2183 {
2184 return safe_call1 (Qeval, sexpr);
2185 }
2186
2187 /* Call function FN with one argument ARG.
2188 Return the result, or nil if something went wrong. */
2189
2190 Lisp_Object
2191 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2192 {
2193 Lisp_Object args[3];
2194 args[0] = fn;
2195 args[1] = arg1;
2196 args[2] = arg2;
2197 return safe_call (3, args);
2198 }
2199
2200
2201 \f
2202 /***********************************************************************
2203 Debugging
2204 ***********************************************************************/
2205
2206 #if 0
2207
2208 /* Define CHECK_IT to perform sanity checks on iterators.
2209 This is for debugging. It is too slow to do unconditionally. */
2210
2211 static void
2212 check_it (struct it *it)
2213 {
2214 if (it->method == GET_FROM_STRING)
2215 {
2216 xassert (STRINGP (it->string));
2217 xassert (IT_STRING_CHARPOS (*it) >= 0);
2218 }
2219 else
2220 {
2221 xassert (IT_STRING_CHARPOS (*it) < 0);
2222 if (it->method == GET_FROM_BUFFER)
2223 {
2224 /* Check that character and byte positions agree. */
2225 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2226 }
2227 }
2228
2229 if (it->dpvec)
2230 xassert (it->current.dpvec_index >= 0);
2231 else
2232 xassert (it->current.dpvec_index < 0);
2233 }
2234
2235 #define CHECK_IT(IT) check_it ((IT))
2236
2237 #else /* not 0 */
2238
2239 #define CHECK_IT(IT) (void) 0
2240
2241 #endif /* not 0 */
2242
2243
2244 #if GLYPH_DEBUG && XASSERTS
2245
2246 /* Check that the window end of window W is what we expect it
2247 to be---the last row in the current matrix displaying text. */
2248
2249 static void
2250 check_window_end (struct window *w)
2251 {
2252 if (!MINI_WINDOW_P (w)
2253 && !NILP (w->window_end_valid))
2254 {
2255 struct glyph_row *row;
2256 xassert ((row = MATRIX_ROW (w->current_matrix,
2257 XFASTINT (w->window_end_vpos)),
2258 !row->enabled_p
2259 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2260 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2261 }
2262 }
2263
2264 #define CHECK_WINDOW_END(W) check_window_end ((W))
2265
2266 #else
2267
2268 #define CHECK_WINDOW_END(W) (void) 0
2269
2270 #endif
2271
2272
2273 \f
2274 /***********************************************************************
2275 Iterator initialization
2276 ***********************************************************************/
2277
2278 /* Initialize IT for displaying current_buffer in window W, starting
2279 at character position CHARPOS. CHARPOS < 0 means that no buffer
2280 position is specified which is useful when the iterator is assigned
2281 a position later. BYTEPOS is the byte position corresponding to
2282 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2283
2284 If ROW is not null, calls to produce_glyphs with IT as parameter
2285 will produce glyphs in that row.
2286
2287 BASE_FACE_ID is the id of a base face to use. It must be one of
2288 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2289 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2290 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2291
2292 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2293 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2294 will be initialized to use the corresponding mode line glyph row of
2295 the desired matrix of W. */
2296
2297 void
2298 init_iterator (struct it *it, struct window *w,
2299 EMACS_INT charpos, EMACS_INT bytepos,
2300 struct glyph_row *row, enum face_id base_face_id)
2301 {
2302 int highlight_region_p;
2303 enum face_id remapped_base_face_id = base_face_id;
2304
2305 /* Some precondition checks. */
2306 xassert (w != NULL && it != NULL);
2307 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2308 && charpos <= ZV));
2309
2310 /* If face attributes have been changed since the last redisplay,
2311 free realized faces now because they depend on face definitions
2312 that might have changed. Don't free faces while there might be
2313 desired matrices pending which reference these faces. */
2314 if (face_change_count && !inhibit_free_realized_faces)
2315 {
2316 face_change_count = 0;
2317 free_all_realized_faces (Qnil);
2318 }
2319
2320 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2321 if (! NILP (Vface_remapping_alist))
2322 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2323
2324 /* Use one of the mode line rows of W's desired matrix if
2325 appropriate. */
2326 if (row == NULL)
2327 {
2328 if (base_face_id == MODE_LINE_FACE_ID
2329 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2330 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2331 else if (base_face_id == HEADER_LINE_FACE_ID)
2332 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2333 }
2334
2335 /* Clear IT. */
2336 memset (it, 0, sizeof *it);
2337 it->current.overlay_string_index = -1;
2338 it->current.dpvec_index = -1;
2339 it->base_face_id = remapped_base_face_id;
2340 it->string = Qnil;
2341 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2342
2343 /* The window in which we iterate over current_buffer: */
2344 XSETWINDOW (it->window, w);
2345 it->w = w;
2346 it->f = XFRAME (w->frame);
2347
2348 it->cmp_it.id = -1;
2349
2350 /* Extra space between lines (on window systems only). */
2351 if (base_face_id == DEFAULT_FACE_ID
2352 && FRAME_WINDOW_P (it->f))
2353 {
2354 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2355 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2356 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2357 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2358 * FRAME_LINE_HEIGHT (it->f));
2359 else if (it->f->extra_line_spacing > 0)
2360 it->extra_line_spacing = it->f->extra_line_spacing;
2361 it->max_extra_line_spacing = 0;
2362 }
2363
2364 /* If realized faces have been removed, e.g. because of face
2365 attribute changes of named faces, recompute them. When running
2366 in batch mode, the face cache of the initial frame is null. If
2367 we happen to get called, make a dummy face cache. */
2368 if (FRAME_FACE_CACHE (it->f) == NULL)
2369 init_frame_faces (it->f);
2370 if (FRAME_FACE_CACHE (it->f)->used == 0)
2371 recompute_basic_faces (it->f);
2372
2373 /* Current value of the `slice', `space-width', and 'height' properties. */
2374 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2375 it->space_width = Qnil;
2376 it->font_height = Qnil;
2377 it->override_ascent = -1;
2378
2379 /* Are control characters displayed as `^C'? */
2380 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2381
2382 /* -1 means everything between a CR and the following line end
2383 is invisible. >0 means lines indented more than this value are
2384 invisible. */
2385 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2386 ? XINT (BVAR (current_buffer, selective_display))
2387 : (!NILP (BVAR (current_buffer, selective_display))
2388 ? -1 : 0));
2389 it->selective_display_ellipsis_p
2390 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2391
2392 /* Display table to use. */
2393 it->dp = window_display_table (w);
2394
2395 /* Are multibyte characters enabled in current_buffer? */
2396 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2397
2398 /* Do we need to reorder bidirectional text? Not if this is a
2399 unibyte buffer: by definition, none of the single-byte characters
2400 are strong R2L, so no reordering is needed. And bidi.c doesn't
2401 support unibyte buffers anyway. */
2402 it->bidi_p
2403 = !NILP (BVAR (current_buffer, bidi_display_reordering)) && it->multibyte_p;
2404
2405 /* Non-zero if we should highlight the region. */
2406 highlight_region_p
2407 = (!NILP (Vtransient_mark_mode)
2408 && !NILP (BVAR (current_buffer, mark_active))
2409 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2410
2411 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2412 start and end of a visible region in window IT->w. Set both to
2413 -1 to indicate no region. */
2414 if (highlight_region_p
2415 /* Maybe highlight only in selected window. */
2416 && (/* Either show region everywhere. */
2417 highlight_nonselected_windows
2418 /* Or show region in the selected window. */
2419 || w == XWINDOW (selected_window)
2420 /* Or show the region if we are in the mini-buffer and W is
2421 the window the mini-buffer refers to. */
2422 || (MINI_WINDOW_P (XWINDOW (selected_window))
2423 && WINDOWP (minibuf_selected_window)
2424 && w == XWINDOW (minibuf_selected_window))))
2425 {
2426 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2427 it->region_beg_charpos = min (PT, markpos);
2428 it->region_end_charpos = max (PT, markpos);
2429 }
2430 else
2431 it->region_beg_charpos = it->region_end_charpos = -1;
2432
2433 /* Get the position at which the redisplay_end_trigger hook should
2434 be run, if it is to be run at all. */
2435 if (MARKERP (w->redisplay_end_trigger)
2436 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2437 it->redisplay_end_trigger_charpos
2438 = marker_position (w->redisplay_end_trigger);
2439 else if (INTEGERP (w->redisplay_end_trigger))
2440 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2441
2442 /* Correct bogus values of tab_width. */
2443 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2444 if (it->tab_width <= 0 || it->tab_width > 1000)
2445 it->tab_width = 8;
2446
2447 /* Are lines in the display truncated? */
2448 if (base_face_id != DEFAULT_FACE_ID
2449 || XINT (it->w->hscroll)
2450 || (! WINDOW_FULL_WIDTH_P (it->w)
2451 && ((!NILP (Vtruncate_partial_width_windows)
2452 && !INTEGERP (Vtruncate_partial_width_windows))
2453 || (INTEGERP (Vtruncate_partial_width_windows)
2454 && (WINDOW_TOTAL_COLS (it->w)
2455 < XINT (Vtruncate_partial_width_windows))))))
2456 it->line_wrap = TRUNCATE;
2457 else if (NILP (BVAR (current_buffer, truncate_lines)))
2458 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2459 ? WINDOW_WRAP : WORD_WRAP;
2460 else
2461 it->line_wrap = TRUNCATE;
2462
2463 /* Get dimensions of truncation and continuation glyphs. These are
2464 displayed as fringe bitmaps under X, so we don't need them for such
2465 frames. */
2466 if (!FRAME_WINDOW_P (it->f))
2467 {
2468 if (it->line_wrap == TRUNCATE)
2469 {
2470 /* We will need the truncation glyph. */
2471 xassert (it->glyph_row == NULL);
2472 produce_special_glyphs (it, IT_TRUNCATION);
2473 it->truncation_pixel_width = it->pixel_width;
2474 }
2475 else
2476 {
2477 /* We will need the continuation glyph. */
2478 xassert (it->glyph_row == NULL);
2479 produce_special_glyphs (it, IT_CONTINUATION);
2480 it->continuation_pixel_width = it->pixel_width;
2481 }
2482
2483 /* Reset these values to zero because the produce_special_glyphs
2484 above has changed them. */
2485 it->pixel_width = it->ascent = it->descent = 0;
2486 it->phys_ascent = it->phys_descent = 0;
2487 }
2488
2489 /* Set this after getting the dimensions of truncation and
2490 continuation glyphs, so that we don't produce glyphs when calling
2491 produce_special_glyphs, above. */
2492 it->glyph_row = row;
2493 it->area = TEXT_AREA;
2494
2495 /* Forget any previous info about this row being reversed. */
2496 if (it->glyph_row)
2497 it->glyph_row->reversed_p = 0;
2498
2499 /* Get the dimensions of the display area. The display area
2500 consists of the visible window area plus a horizontally scrolled
2501 part to the left of the window. All x-values are relative to the
2502 start of this total display area. */
2503 if (base_face_id != DEFAULT_FACE_ID)
2504 {
2505 /* Mode lines, menu bar in terminal frames. */
2506 it->first_visible_x = 0;
2507 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2508 }
2509 else
2510 {
2511 it->first_visible_x
2512 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2513 it->last_visible_x = (it->first_visible_x
2514 + window_box_width (w, TEXT_AREA));
2515
2516 /* If we truncate lines, leave room for the truncator glyph(s) at
2517 the right margin. Otherwise, leave room for the continuation
2518 glyph(s). Truncation and continuation glyphs are not inserted
2519 for window-based redisplay. */
2520 if (!FRAME_WINDOW_P (it->f))
2521 {
2522 if (it->line_wrap == TRUNCATE)
2523 it->last_visible_x -= it->truncation_pixel_width;
2524 else
2525 it->last_visible_x -= it->continuation_pixel_width;
2526 }
2527
2528 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2529 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2530 }
2531
2532 /* Leave room for a border glyph. */
2533 if (!FRAME_WINDOW_P (it->f)
2534 && !WINDOW_RIGHTMOST_P (it->w))
2535 it->last_visible_x -= 1;
2536
2537 it->last_visible_y = window_text_bottom_y (w);
2538
2539 /* For mode lines and alike, arrange for the first glyph having a
2540 left box line if the face specifies a box. */
2541 if (base_face_id != DEFAULT_FACE_ID)
2542 {
2543 struct face *face;
2544
2545 it->face_id = remapped_base_face_id;
2546
2547 /* If we have a boxed mode line, make the first character appear
2548 with a left box line. */
2549 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2550 if (face->box != FACE_NO_BOX)
2551 it->start_of_box_run_p = 1;
2552 }
2553
2554 /* If we are to reorder bidirectional text, init the bidi
2555 iterator. */
2556 if (it->bidi_p)
2557 {
2558 /* Note the paragraph direction that this buffer wants to
2559 use. */
2560 if (EQ (BVAR (current_buffer, bidi_paragraph_direction), Qleft_to_right))
2561 it->paragraph_embedding = L2R;
2562 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction), Qright_to_left))
2563 it->paragraph_embedding = R2L;
2564 else
2565 it->paragraph_embedding = NEUTRAL_DIR;
2566 bidi_init_it (charpos, bytepos, FRAME_WINDOW_P (it->f), &it->bidi_it);
2567 }
2568
2569 /* If a buffer position was specified, set the iterator there,
2570 getting overlays and face properties from that position. */
2571 if (charpos >= BUF_BEG (current_buffer))
2572 {
2573 it->end_charpos = ZV;
2574 it->face_id = -1;
2575 IT_CHARPOS (*it) = charpos;
2576
2577 /* Compute byte position if not specified. */
2578 if (bytepos < charpos)
2579 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2580 else
2581 IT_BYTEPOS (*it) = bytepos;
2582
2583 it->start = it->current;
2584
2585 /* Compute faces etc. */
2586 reseat (it, it->current.pos, 1);
2587 }
2588
2589 CHECK_IT (it);
2590 }
2591
2592
2593 /* Initialize IT for the display of window W with window start POS. */
2594
2595 void
2596 start_display (struct it *it, struct window *w, struct text_pos pos)
2597 {
2598 struct glyph_row *row;
2599 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2600
2601 row = w->desired_matrix->rows + first_vpos;
2602 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2603 it->first_vpos = first_vpos;
2604
2605 /* Don't reseat to previous visible line start if current start
2606 position is in a string or image. */
2607 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2608 {
2609 int start_at_line_beg_p;
2610 int first_y = it->current_y;
2611
2612 /* If window start is not at a line start, skip forward to POS to
2613 get the correct continuation lines width. */
2614 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2615 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2616 if (!start_at_line_beg_p)
2617 {
2618 int new_x;
2619
2620 reseat_at_previous_visible_line_start (it);
2621 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2622
2623 new_x = it->current_x + it->pixel_width;
2624
2625 /* If lines are continued, this line may end in the middle
2626 of a multi-glyph character (e.g. a control character
2627 displayed as \003, or in the middle of an overlay
2628 string). In this case move_it_to above will not have
2629 taken us to the start of the continuation line but to the
2630 end of the continued line. */
2631 if (it->current_x > 0
2632 && it->line_wrap != TRUNCATE /* Lines are continued. */
2633 && (/* And glyph doesn't fit on the line. */
2634 new_x > it->last_visible_x
2635 /* Or it fits exactly and we're on a window
2636 system frame. */
2637 || (new_x == it->last_visible_x
2638 && FRAME_WINDOW_P (it->f))))
2639 {
2640 if (it->current.dpvec_index >= 0
2641 || it->current.overlay_string_index >= 0)
2642 {
2643 set_iterator_to_next (it, 1);
2644 move_it_in_display_line_to (it, -1, -1, 0);
2645 }
2646
2647 it->continuation_lines_width += it->current_x;
2648 }
2649
2650 /* We're starting a new display line, not affected by the
2651 height of the continued line, so clear the appropriate
2652 fields in the iterator structure. */
2653 it->max_ascent = it->max_descent = 0;
2654 it->max_phys_ascent = it->max_phys_descent = 0;
2655
2656 it->current_y = first_y;
2657 it->vpos = 0;
2658 it->current_x = it->hpos = 0;
2659 }
2660 }
2661 }
2662
2663
2664 /* Return 1 if POS is a position in ellipses displayed for invisible
2665 text. W is the window we display, for text property lookup. */
2666
2667 static int
2668 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2669 {
2670 Lisp_Object prop, window;
2671 int ellipses_p = 0;
2672 EMACS_INT charpos = CHARPOS (pos->pos);
2673
2674 /* If POS specifies a position in a display vector, this might
2675 be for an ellipsis displayed for invisible text. We won't
2676 get the iterator set up for delivering that ellipsis unless
2677 we make sure that it gets aware of the invisible text. */
2678 if (pos->dpvec_index >= 0
2679 && pos->overlay_string_index < 0
2680 && CHARPOS (pos->string_pos) < 0
2681 && charpos > BEGV
2682 && (XSETWINDOW (window, w),
2683 prop = Fget_char_property (make_number (charpos),
2684 Qinvisible, window),
2685 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2686 {
2687 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2688 window);
2689 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2690 }
2691
2692 return ellipses_p;
2693 }
2694
2695
2696 /* Initialize IT for stepping through current_buffer in window W,
2697 starting at position POS that includes overlay string and display
2698 vector/ control character translation position information. Value
2699 is zero if there are overlay strings with newlines at POS. */
2700
2701 static int
2702 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2703 {
2704 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2705 int i, overlay_strings_with_newlines = 0;
2706
2707 /* If POS specifies a position in a display vector, this might
2708 be for an ellipsis displayed for invisible text. We won't
2709 get the iterator set up for delivering that ellipsis unless
2710 we make sure that it gets aware of the invisible text. */
2711 if (in_ellipses_for_invisible_text_p (pos, w))
2712 {
2713 --charpos;
2714 bytepos = 0;
2715 }
2716
2717 /* Keep in mind: the call to reseat in init_iterator skips invisible
2718 text, so we might end up at a position different from POS. This
2719 is only a problem when POS is a row start after a newline and an
2720 overlay starts there with an after-string, and the overlay has an
2721 invisible property. Since we don't skip invisible text in
2722 display_line and elsewhere immediately after consuming the
2723 newline before the row start, such a POS will not be in a string,
2724 but the call to init_iterator below will move us to the
2725 after-string. */
2726 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2727
2728 /* This only scans the current chunk -- it should scan all chunks.
2729 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2730 to 16 in 22.1 to make this a lesser problem. */
2731 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2732 {
2733 const char *s = SSDATA (it->overlay_strings[i]);
2734 const char *e = s + SBYTES (it->overlay_strings[i]);
2735
2736 while (s < e && *s != '\n')
2737 ++s;
2738
2739 if (s < e)
2740 {
2741 overlay_strings_with_newlines = 1;
2742 break;
2743 }
2744 }
2745
2746 /* If position is within an overlay string, set up IT to the right
2747 overlay string. */
2748 if (pos->overlay_string_index >= 0)
2749 {
2750 int relative_index;
2751
2752 /* If the first overlay string happens to have a `display'
2753 property for an image, the iterator will be set up for that
2754 image, and we have to undo that setup first before we can
2755 correct the overlay string index. */
2756 if (it->method == GET_FROM_IMAGE)
2757 pop_it (it);
2758
2759 /* We already have the first chunk of overlay strings in
2760 IT->overlay_strings. Load more until the one for
2761 pos->overlay_string_index is in IT->overlay_strings. */
2762 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2763 {
2764 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2765 it->current.overlay_string_index = 0;
2766 while (n--)
2767 {
2768 load_overlay_strings (it, 0);
2769 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2770 }
2771 }
2772
2773 it->current.overlay_string_index = pos->overlay_string_index;
2774 relative_index = (it->current.overlay_string_index
2775 % OVERLAY_STRING_CHUNK_SIZE);
2776 it->string = it->overlay_strings[relative_index];
2777 xassert (STRINGP (it->string));
2778 it->current.string_pos = pos->string_pos;
2779 it->method = GET_FROM_STRING;
2780 }
2781
2782 if (CHARPOS (pos->string_pos) >= 0)
2783 {
2784 /* Recorded position is not in an overlay string, but in another
2785 string. This can only be a string from a `display' property.
2786 IT should already be filled with that string. */
2787 it->current.string_pos = pos->string_pos;
2788 xassert (STRINGP (it->string));
2789 }
2790
2791 /* Restore position in display vector translations, control
2792 character translations or ellipses. */
2793 if (pos->dpvec_index >= 0)
2794 {
2795 if (it->dpvec == NULL)
2796 get_next_display_element (it);
2797 xassert (it->dpvec && it->current.dpvec_index == 0);
2798 it->current.dpvec_index = pos->dpvec_index;
2799 }
2800
2801 CHECK_IT (it);
2802 return !overlay_strings_with_newlines;
2803 }
2804
2805
2806 /* Initialize IT for stepping through current_buffer in window W
2807 starting at ROW->start. */
2808
2809 static void
2810 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2811 {
2812 init_from_display_pos (it, w, &row->start);
2813 it->start = row->start;
2814 it->continuation_lines_width = row->continuation_lines_width;
2815 CHECK_IT (it);
2816 }
2817
2818
2819 /* Initialize IT for stepping through current_buffer in window W
2820 starting in the line following ROW, i.e. starting at ROW->end.
2821 Value is zero if there are overlay strings with newlines at ROW's
2822 end position. */
2823
2824 static int
2825 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2826 {
2827 int success = 0;
2828
2829 if (init_from_display_pos (it, w, &row->end))
2830 {
2831 if (row->continued_p)
2832 it->continuation_lines_width
2833 = row->continuation_lines_width + row->pixel_width;
2834 CHECK_IT (it);
2835 success = 1;
2836 }
2837
2838 return success;
2839 }
2840
2841
2842
2843 \f
2844 /***********************************************************************
2845 Text properties
2846 ***********************************************************************/
2847
2848 /* Called when IT reaches IT->stop_charpos. Handle text property and
2849 overlay changes. Set IT->stop_charpos to the next position where
2850 to stop. */
2851
2852 static void
2853 handle_stop (struct it *it)
2854 {
2855 enum prop_handled handled;
2856 int handle_overlay_change_p;
2857 struct props *p;
2858
2859 it->dpvec = NULL;
2860 it->current.dpvec_index = -1;
2861 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2862 it->ignore_overlay_strings_at_pos_p = 0;
2863 it->ellipsis_p = 0;
2864
2865 /* Use face of preceding text for ellipsis (if invisible) */
2866 if (it->selective_display_ellipsis_p)
2867 it->saved_face_id = it->face_id;
2868
2869 do
2870 {
2871 handled = HANDLED_NORMALLY;
2872
2873 /* Call text property handlers. */
2874 for (p = it_props; p->handler; ++p)
2875 {
2876 handled = p->handler (it);
2877
2878 if (handled == HANDLED_RECOMPUTE_PROPS)
2879 break;
2880 else if (handled == HANDLED_RETURN)
2881 {
2882 /* We still want to show before and after strings from
2883 overlays even if the actual buffer text is replaced. */
2884 if (!handle_overlay_change_p
2885 || it->sp > 1
2886 || !get_overlay_strings_1 (it, 0, 0))
2887 {
2888 if (it->ellipsis_p)
2889 setup_for_ellipsis (it, 0);
2890 /* When handling a display spec, we might load an
2891 empty string. In that case, discard it here. We
2892 used to discard it in handle_single_display_spec,
2893 but that causes get_overlay_strings_1, above, to
2894 ignore overlay strings that we must check. */
2895 if (STRINGP (it->string) && !SCHARS (it->string))
2896 pop_it (it);
2897 return;
2898 }
2899 else if (STRINGP (it->string) && !SCHARS (it->string))
2900 pop_it (it);
2901 else
2902 {
2903 it->ignore_overlay_strings_at_pos_p = 1;
2904 it->string_from_display_prop_p = 0;
2905 handle_overlay_change_p = 0;
2906 }
2907 handled = HANDLED_RECOMPUTE_PROPS;
2908 break;
2909 }
2910 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2911 handle_overlay_change_p = 0;
2912 }
2913
2914 if (handled != HANDLED_RECOMPUTE_PROPS)
2915 {
2916 /* Don't check for overlay strings below when set to deliver
2917 characters from a display vector. */
2918 if (it->method == GET_FROM_DISPLAY_VECTOR)
2919 handle_overlay_change_p = 0;
2920
2921 /* Handle overlay changes.
2922 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2923 if it finds overlays. */
2924 if (handle_overlay_change_p)
2925 handled = handle_overlay_change (it);
2926 }
2927
2928 if (it->ellipsis_p)
2929 {
2930 setup_for_ellipsis (it, 0);
2931 break;
2932 }
2933 }
2934 while (handled == HANDLED_RECOMPUTE_PROPS);
2935
2936 /* Determine where to stop next. */
2937 if (handled == HANDLED_NORMALLY)
2938 compute_stop_pos (it);
2939 }
2940
2941
2942 /* Compute IT->stop_charpos from text property and overlay change
2943 information for IT's current position. */
2944
2945 static void
2946 compute_stop_pos (struct it *it)
2947 {
2948 register INTERVAL iv, next_iv;
2949 Lisp_Object object, limit, position;
2950 EMACS_INT charpos, bytepos;
2951
2952 /* If nowhere else, stop at the end. */
2953 it->stop_charpos = it->end_charpos;
2954
2955 if (STRINGP (it->string))
2956 {
2957 /* Strings are usually short, so don't limit the search for
2958 properties. */
2959 object = it->string;
2960 limit = Qnil;
2961 charpos = IT_STRING_CHARPOS (*it);
2962 bytepos = IT_STRING_BYTEPOS (*it);
2963 }
2964 else
2965 {
2966 EMACS_INT pos;
2967
2968 /* If next overlay change is in front of the current stop pos
2969 (which is IT->end_charpos), stop there. Note: value of
2970 next_overlay_change is point-max if no overlay change
2971 follows. */
2972 charpos = IT_CHARPOS (*it);
2973 bytepos = IT_BYTEPOS (*it);
2974 pos = next_overlay_change (charpos);
2975 if (pos < it->stop_charpos)
2976 it->stop_charpos = pos;
2977
2978 /* If showing the region, we have to stop at the region
2979 start or end because the face might change there. */
2980 if (it->region_beg_charpos > 0)
2981 {
2982 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2983 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2984 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2985 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2986 }
2987
2988 /* Set up variables for computing the stop position from text
2989 property changes. */
2990 XSETBUFFER (object, current_buffer);
2991 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2992 }
2993
2994 /* Get the interval containing IT's position. Value is a null
2995 interval if there isn't such an interval. */
2996 position = make_number (charpos);
2997 iv = validate_interval_range (object, &position, &position, 0);
2998 if (!NULL_INTERVAL_P (iv))
2999 {
3000 Lisp_Object values_here[LAST_PROP_IDX];
3001 struct props *p;
3002
3003 /* Get properties here. */
3004 for (p = it_props; p->handler; ++p)
3005 values_here[p->idx] = textget (iv->plist, *p->name);
3006
3007 /* Look for an interval following iv that has different
3008 properties. */
3009 for (next_iv = next_interval (iv);
3010 (!NULL_INTERVAL_P (next_iv)
3011 && (NILP (limit)
3012 || XFASTINT (limit) > next_iv->position));
3013 next_iv = next_interval (next_iv))
3014 {
3015 for (p = it_props; p->handler; ++p)
3016 {
3017 Lisp_Object new_value;
3018
3019 new_value = textget (next_iv->plist, *p->name);
3020 if (!EQ (values_here[p->idx], new_value))
3021 break;
3022 }
3023
3024 if (p->handler)
3025 break;
3026 }
3027
3028 if (!NULL_INTERVAL_P (next_iv))
3029 {
3030 if (INTEGERP (limit)
3031 && next_iv->position >= XFASTINT (limit))
3032 /* No text property change up to limit. */
3033 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3034 else
3035 /* Text properties change in next_iv. */
3036 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3037 }
3038 }
3039
3040 if (it->cmp_it.id < 0)
3041 {
3042 EMACS_INT stoppos = it->end_charpos;
3043
3044 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3045 stoppos = -1;
3046 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3047 stoppos, it->string);
3048 }
3049
3050 xassert (STRINGP (it->string)
3051 || (it->stop_charpos >= BEGV
3052 && it->stop_charpos >= IT_CHARPOS (*it)));
3053 }
3054
3055
3056 /* Return the position of the next overlay change after POS in
3057 current_buffer. Value is point-max if no overlay change
3058 follows. This is like `next-overlay-change' but doesn't use
3059 xmalloc. */
3060
3061 static EMACS_INT
3062 next_overlay_change (EMACS_INT pos)
3063 {
3064 ptrdiff_t i, noverlays;
3065 EMACS_INT endpos;
3066 Lisp_Object *overlays;
3067
3068 /* Get all overlays at the given position. */
3069 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3070
3071 /* If any of these overlays ends before endpos,
3072 use its ending point instead. */
3073 for (i = 0; i < noverlays; ++i)
3074 {
3075 Lisp_Object oend;
3076 EMACS_INT oendpos;
3077
3078 oend = OVERLAY_END (overlays[i]);
3079 oendpos = OVERLAY_POSITION (oend);
3080 endpos = min (endpos, oendpos);
3081 }
3082
3083 return endpos;
3084 }
3085
3086 /* Return the character position of a display string at or after CHARPOS.
3087 If no display string exists at or after CHARPOS, return ZV. A
3088 display string is either an overlay with `display' property whose
3089 value is a string, or a `display' text property whose value is a
3090 string. FRAME_WINDOW_P is non-zero when we are displaying a window
3091 on a GUI frame. */
3092 EMACS_INT
3093 compute_display_string_pos (EMACS_INT charpos, int frame_window_p)
3094 {
3095 /* FIXME: Support display properties on strings (object = Qnil means
3096 current buffer). */
3097 Lisp_Object object = Qnil;
3098 Lisp_Object pos, spec;
3099 struct text_pos position;
3100 EMACS_INT bufpos;
3101
3102 if (charpos >= ZV)
3103 return ZV;
3104
3105 /* If the character at CHARPOS is where the display string begins,
3106 return CHARPOS. */
3107 pos = make_number (charpos);
3108 CHARPOS (position) = charpos;
3109 BYTEPOS (position) = CHAR_TO_BYTE (charpos);
3110 bufpos = charpos; /* FIXME! support strings as well */
3111 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3112 && (charpos <= BEGV
3113 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3114 object),
3115 spec))
3116 && handle_display_spec (NULL, spec, object, Qnil, &position, bufpos,
3117 frame_window_p))
3118 return charpos;
3119
3120 /* Look forward for the first character with a `display' property
3121 that will replace the underlying text when displayed. */
3122 do {
3123 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3124 CHARPOS (position) = XFASTINT (pos);
3125 BYTEPOS (position) = CHAR_TO_BYTE (CHARPOS (position));
3126 if (CHARPOS (position) >= ZV)
3127 break;
3128 spec = Fget_char_property (pos, Qdisplay, object);
3129 bufpos = CHARPOS (position); /* FIXME! support strings as well */
3130 } while (NILP (spec)
3131 || !handle_display_spec (NULL, spec, object, Qnil, &position, bufpos,
3132 frame_window_p));
3133
3134 return CHARPOS (position);
3135 }
3136
3137 /* Return the character position of the end of the display string that
3138 started at CHARPOS. A display string is either an overlay with
3139 `display' property whose value is a string or a `display' text
3140 property whose value is a string. */
3141 EMACS_INT
3142 compute_display_string_end (EMACS_INT charpos)
3143 {
3144 /* FIXME: Support display properties on strings (object = Qnil means
3145 current buffer). */
3146 Lisp_Object object = Qnil;
3147 Lisp_Object pos = make_number (charpos);
3148
3149 if (charpos >= ZV)
3150 return ZV;
3151
3152 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3153 abort ();
3154
3155 /* Look forward for the first character where the `display' property
3156 changes. */
3157 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3158
3159 return XFASTINT (pos);
3160 }
3161
3162
3163 \f
3164 /***********************************************************************
3165 Fontification
3166 ***********************************************************************/
3167
3168 /* Handle changes in the `fontified' property of the current buffer by
3169 calling hook functions from Qfontification_functions to fontify
3170 regions of text. */
3171
3172 static enum prop_handled
3173 handle_fontified_prop (struct it *it)
3174 {
3175 Lisp_Object prop, pos;
3176 enum prop_handled handled = HANDLED_NORMALLY;
3177
3178 if (!NILP (Vmemory_full))
3179 return handled;
3180
3181 /* Get the value of the `fontified' property at IT's current buffer
3182 position. (The `fontified' property doesn't have a special
3183 meaning in strings.) If the value is nil, call functions from
3184 Qfontification_functions. */
3185 if (!STRINGP (it->string)
3186 && it->s == NULL
3187 && !NILP (Vfontification_functions)
3188 && !NILP (Vrun_hooks)
3189 && (pos = make_number (IT_CHARPOS (*it)),
3190 prop = Fget_char_property (pos, Qfontified, Qnil),
3191 /* Ignore the special cased nil value always present at EOB since
3192 no amount of fontifying will be able to change it. */
3193 NILP (prop) && IT_CHARPOS (*it) < Z))
3194 {
3195 int count = SPECPDL_INDEX ();
3196 Lisp_Object val;
3197 struct buffer *obuf = current_buffer;
3198 int begv = BEGV, zv = ZV;
3199 int old_clip_changed = current_buffer->clip_changed;
3200
3201 val = Vfontification_functions;
3202 specbind (Qfontification_functions, Qnil);
3203
3204 xassert (it->end_charpos == ZV);
3205
3206 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3207 safe_call1 (val, pos);
3208 else
3209 {
3210 Lisp_Object fns, fn;
3211 struct gcpro gcpro1, gcpro2;
3212
3213 fns = Qnil;
3214 GCPRO2 (val, fns);
3215
3216 for (; CONSP (val); val = XCDR (val))
3217 {
3218 fn = XCAR (val);
3219
3220 if (EQ (fn, Qt))
3221 {
3222 /* A value of t indicates this hook has a local
3223 binding; it means to run the global binding too.
3224 In a global value, t should not occur. If it
3225 does, we must ignore it to avoid an endless
3226 loop. */
3227 for (fns = Fdefault_value (Qfontification_functions);
3228 CONSP (fns);
3229 fns = XCDR (fns))
3230 {
3231 fn = XCAR (fns);
3232 if (!EQ (fn, Qt))
3233 safe_call1 (fn, pos);
3234 }
3235 }
3236 else
3237 safe_call1 (fn, pos);
3238 }
3239
3240 UNGCPRO;
3241 }
3242
3243 unbind_to (count, Qnil);
3244
3245 /* Fontification functions routinely call `save-restriction'.
3246 Normally, this tags clip_changed, which can confuse redisplay
3247 (see discussion in Bug#6671). Since we don't perform any
3248 special handling of fontification changes in the case where
3249 `save-restriction' isn't called, there's no point doing so in
3250 this case either. So, if the buffer's restrictions are
3251 actually left unchanged, reset clip_changed. */
3252 if (obuf == current_buffer)
3253 {
3254 if (begv == BEGV && zv == ZV)
3255 current_buffer->clip_changed = old_clip_changed;
3256 }
3257 /* There isn't much we can reasonably do to protect against
3258 misbehaving fontification, but here's a fig leaf. */
3259 else if (!NILP (BVAR (obuf, name)))
3260 set_buffer_internal_1 (obuf);
3261
3262 /* The fontification code may have added/removed text.
3263 It could do even a lot worse, but let's at least protect against
3264 the most obvious case where only the text past `pos' gets changed',
3265 as is/was done in grep.el where some escapes sequences are turned
3266 into face properties (bug#7876). */
3267 it->end_charpos = ZV;
3268
3269 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3270 something. This avoids an endless loop if they failed to
3271 fontify the text for which reason ever. */
3272 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3273 handled = HANDLED_RECOMPUTE_PROPS;
3274 }
3275
3276 return handled;
3277 }
3278
3279
3280 \f
3281 /***********************************************************************
3282 Faces
3283 ***********************************************************************/
3284
3285 /* Set up iterator IT from face properties at its current position.
3286 Called from handle_stop. */
3287
3288 static enum prop_handled
3289 handle_face_prop (struct it *it)
3290 {
3291 int new_face_id;
3292 EMACS_INT next_stop;
3293
3294 if (!STRINGP (it->string))
3295 {
3296 new_face_id
3297 = face_at_buffer_position (it->w,
3298 IT_CHARPOS (*it),
3299 it->region_beg_charpos,
3300 it->region_end_charpos,
3301 &next_stop,
3302 (IT_CHARPOS (*it)
3303 + TEXT_PROP_DISTANCE_LIMIT),
3304 0, it->base_face_id);
3305
3306 /* Is this a start of a run of characters with box face?
3307 Caveat: this can be called for a freshly initialized
3308 iterator; face_id is -1 in this case. We know that the new
3309 face will not change until limit, i.e. if the new face has a
3310 box, all characters up to limit will have one. But, as
3311 usual, we don't know whether limit is really the end. */
3312 if (new_face_id != it->face_id)
3313 {
3314 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3315
3316 /* If new face has a box but old face has not, this is
3317 the start of a run of characters with box, i.e. it has
3318 a shadow on the left side. The value of face_id of the
3319 iterator will be -1 if this is the initial call that gets
3320 the face. In this case, we have to look in front of IT's
3321 position and see whether there is a face != new_face_id. */
3322 it->start_of_box_run_p
3323 = (new_face->box != FACE_NO_BOX
3324 && (it->face_id >= 0
3325 || IT_CHARPOS (*it) == BEG
3326 || new_face_id != face_before_it_pos (it)));
3327 it->face_box_p = new_face->box != FACE_NO_BOX;
3328 }
3329 }
3330 else
3331 {
3332 int base_face_id;
3333 EMACS_INT bufpos;
3334 int i;
3335 Lisp_Object from_overlay
3336 = (it->current.overlay_string_index >= 0
3337 ? it->string_overlays[it->current.overlay_string_index]
3338 : Qnil);
3339
3340 /* See if we got to this string directly or indirectly from
3341 an overlay property. That includes the before-string or
3342 after-string of an overlay, strings in display properties
3343 provided by an overlay, their text properties, etc.
3344
3345 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3346 if (! NILP (from_overlay))
3347 for (i = it->sp - 1; i >= 0; i--)
3348 {
3349 if (it->stack[i].current.overlay_string_index >= 0)
3350 from_overlay
3351 = it->string_overlays[it->stack[i].current.overlay_string_index];
3352 else if (! NILP (it->stack[i].from_overlay))
3353 from_overlay = it->stack[i].from_overlay;
3354
3355 if (!NILP (from_overlay))
3356 break;
3357 }
3358
3359 if (! NILP (from_overlay))
3360 {
3361 bufpos = IT_CHARPOS (*it);
3362 /* For a string from an overlay, the base face depends
3363 only on text properties and ignores overlays. */
3364 base_face_id
3365 = face_for_overlay_string (it->w,
3366 IT_CHARPOS (*it),
3367 it->region_beg_charpos,
3368 it->region_end_charpos,
3369 &next_stop,
3370 (IT_CHARPOS (*it)
3371 + TEXT_PROP_DISTANCE_LIMIT),
3372 0,
3373 from_overlay);
3374 }
3375 else
3376 {
3377 bufpos = 0;
3378
3379 /* For strings from a `display' property, use the face at
3380 IT's current buffer position as the base face to merge
3381 with, so that overlay strings appear in the same face as
3382 surrounding text, unless they specify their own
3383 faces. */
3384 base_face_id = underlying_face_id (it);
3385 }
3386
3387 new_face_id = face_at_string_position (it->w,
3388 it->string,
3389 IT_STRING_CHARPOS (*it),
3390 bufpos,
3391 it->region_beg_charpos,
3392 it->region_end_charpos,
3393 &next_stop,
3394 base_face_id, 0);
3395
3396 /* Is this a start of a run of characters with box? Caveat:
3397 this can be called for a freshly allocated iterator; face_id
3398 is -1 is this case. We know that the new face will not
3399 change until the next check pos, i.e. if the new face has a
3400 box, all characters up to that position will have a
3401 box. But, as usual, we don't know whether that position
3402 is really the end. */
3403 if (new_face_id != it->face_id)
3404 {
3405 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3406 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3407
3408 /* If new face has a box but old face hasn't, this is the
3409 start of a run of characters with box, i.e. it has a
3410 shadow on the left side. */
3411 it->start_of_box_run_p
3412 = new_face->box && (old_face == NULL || !old_face->box);
3413 it->face_box_p = new_face->box != FACE_NO_BOX;
3414 }
3415 }
3416
3417 it->face_id = new_face_id;
3418 return HANDLED_NORMALLY;
3419 }
3420
3421
3422 /* Return the ID of the face ``underlying'' IT's current position,
3423 which is in a string. If the iterator is associated with a
3424 buffer, return the face at IT's current buffer position.
3425 Otherwise, use the iterator's base_face_id. */
3426
3427 static int
3428 underlying_face_id (struct it *it)
3429 {
3430 int face_id = it->base_face_id, i;
3431
3432 xassert (STRINGP (it->string));
3433
3434 for (i = it->sp - 1; i >= 0; --i)
3435 if (NILP (it->stack[i].string))
3436 face_id = it->stack[i].face_id;
3437
3438 return face_id;
3439 }
3440
3441
3442 /* Compute the face one character before or after the current position
3443 of IT. BEFORE_P non-zero means get the face in front of IT's
3444 position. Value is the id of the face. */
3445
3446 static int
3447 face_before_or_after_it_pos (struct it *it, int before_p)
3448 {
3449 int face_id, limit;
3450 EMACS_INT next_check_charpos;
3451 struct text_pos pos;
3452
3453 xassert (it->s == NULL);
3454
3455 if (STRINGP (it->string))
3456 {
3457 EMACS_INT bufpos;
3458 int base_face_id;
3459
3460 /* No face change past the end of the string (for the case
3461 we are padding with spaces). No face change before the
3462 string start. */
3463 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3464 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3465 return it->face_id;
3466
3467 /* Set pos to the position before or after IT's current position. */
3468 if (before_p)
3469 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3470 else
3471 /* For composition, we must check the character after the
3472 composition. */
3473 pos = (it->what == IT_COMPOSITION
3474 ? string_pos (IT_STRING_CHARPOS (*it)
3475 + it->cmp_it.nchars, it->string)
3476 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3477
3478 if (it->current.overlay_string_index >= 0)
3479 bufpos = IT_CHARPOS (*it);
3480 else
3481 bufpos = 0;
3482
3483 base_face_id = underlying_face_id (it);
3484
3485 /* Get the face for ASCII, or unibyte. */
3486 face_id = face_at_string_position (it->w,
3487 it->string,
3488 CHARPOS (pos),
3489 bufpos,
3490 it->region_beg_charpos,
3491 it->region_end_charpos,
3492 &next_check_charpos,
3493 base_face_id, 0);
3494
3495 /* Correct the face for charsets different from ASCII. Do it
3496 for the multibyte case only. The face returned above is
3497 suitable for unibyte text if IT->string is unibyte. */
3498 if (STRING_MULTIBYTE (it->string))
3499 {
3500 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3501 int c, len;
3502 struct face *face = FACE_FROM_ID (it->f, face_id);
3503
3504 c = string_char_and_length (p, &len);
3505 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3506 }
3507 }
3508 else
3509 {
3510 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3511 || (IT_CHARPOS (*it) <= BEGV && before_p))
3512 return it->face_id;
3513
3514 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3515 pos = it->current.pos;
3516
3517 if (before_p)
3518 DEC_TEXT_POS (pos, it->multibyte_p);
3519 else
3520 {
3521 if (it->what == IT_COMPOSITION)
3522 /* For composition, we must check the position after the
3523 composition. */
3524 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3525 else
3526 INC_TEXT_POS (pos, it->multibyte_p);
3527 }
3528
3529 /* Determine face for CHARSET_ASCII, or unibyte. */
3530 face_id = face_at_buffer_position (it->w,
3531 CHARPOS (pos),
3532 it->region_beg_charpos,
3533 it->region_end_charpos,
3534 &next_check_charpos,
3535 limit, 0, -1);
3536
3537 /* Correct the face for charsets different from ASCII. Do it
3538 for the multibyte case only. The face returned above is
3539 suitable for unibyte text if current_buffer is unibyte. */
3540 if (it->multibyte_p)
3541 {
3542 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3543 struct face *face = FACE_FROM_ID (it->f, face_id);
3544 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3545 }
3546 }
3547
3548 return face_id;
3549 }
3550
3551
3552 \f
3553 /***********************************************************************
3554 Invisible text
3555 ***********************************************************************/
3556
3557 /* Set up iterator IT from invisible properties at its current
3558 position. Called from handle_stop. */
3559
3560 static enum prop_handled
3561 handle_invisible_prop (struct it *it)
3562 {
3563 enum prop_handled handled = HANDLED_NORMALLY;
3564
3565 if (STRINGP (it->string))
3566 {
3567 Lisp_Object prop, end_charpos, limit, charpos;
3568
3569 /* Get the value of the invisible text property at the
3570 current position. Value will be nil if there is no such
3571 property. */
3572 charpos = make_number (IT_STRING_CHARPOS (*it));
3573 prop = Fget_text_property (charpos, Qinvisible, it->string);
3574
3575 if (!NILP (prop)
3576 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3577 {
3578 handled = HANDLED_RECOMPUTE_PROPS;
3579
3580 /* Get the position at which the next change of the
3581 invisible text property can be found in IT->string.
3582 Value will be nil if the property value is the same for
3583 all the rest of IT->string. */
3584 XSETINT (limit, SCHARS (it->string));
3585 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3586 it->string, limit);
3587
3588 /* Text at current position is invisible. The next
3589 change in the property is at position end_charpos.
3590 Move IT's current position to that position. */
3591 if (INTEGERP (end_charpos)
3592 && XFASTINT (end_charpos) < XFASTINT (limit))
3593 {
3594 struct text_pos old;
3595 old = it->current.string_pos;
3596 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3597 compute_string_pos (&it->current.string_pos, old, it->string);
3598 }
3599 else
3600 {
3601 /* The rest of the string is invisible. If this is an
3602 overlay string, proceed with the next overlay string
3603 or whatever comes and return a character from there. */
3604 if (it->current.overlay_string_index >= 0)
3605 {
3606 next_overlay_string (it);
3607 /* Don't check for overlay strings when we just
3608 finished processing them. */
3609 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3610 }
3611 else
3612 {
3613 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3614 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3615 }
3616 }
3617 }
3618 }
3619 else
3620 {
3621 int invis_p;
3622 EMACS_INT newpos, next_stop, start_charpos, tem;
3623 Lisp_Object pos, prop, overlay;
3624
3625 /* First of all, is there invisible text at this position? */
3626 tem = start_charpos = IT_CHARPOS (*it);
3627 pos = make_number (tem);
3628 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3629 &overlay);
3630 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3631
3632 /* If we are on invisible text, skip over it. */
3633 if (invis_p && start_charpos < it->end_charpos)
3634 {
3635 /* Record whether we have to display an ellipsis for the
3636 invisible text. */
3637 int display_ellipsis_p = invis_p == 2;
3638
3639 handled = HANDLED_RECOMPUTE_PROPS;
3640
3641 /* Loop skipping over invisible text. The loop is left at
3642 ZV or with IT on the first char being visible again. */
3643 do
3644 {
3645 /* Try to skip some invisible text. Return value is the
3646 position reached which can be equal to where we start
3647 if there is nothing invisible there. This skips both
3648 over invisible text properties and overlays with
3649 invisible property. */
3650 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3651
3652 /* If we skipped nothing at all we weren't at invisible
3653 text in the first place. If everything to the end of
3654 the buffer was skipped, end the loop. */
3655 if (newpos == tem || newpos >= ZV)
3656 invis_p = 0;
3657 else
3658 {
3659 /* We skipped some characters but not necessarily
3660 all there are. Check if we ended up on visible
3661 text. Fget_char_property returns the property of
3662 the char before the given position, i.e. if we
3663 get invis_p = 0, this means that the char at
3664 newpos is visible. */
3665 pos = make_number (newpos);
3666 prop = Fget_char_property (pos, Qinvisible, it->window);
3667 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3668 }
3669
3670 /* If we ended up on invisible text, proceed to
3671 skip starting with next_stop. */
3672 if (invis_p)
3673 tem = next_stop;
3674
3675 /* If there are adjacent invisible texts, don't lose the
3676 second one's ellipsis. */
3677 if (invis_p == 2)
3678 display_ellipsis_p = 1;
3679 }
3680 while (invis_p);
3681
3682 /* The position newpos is now either ZV or on visible text. */
3683 if (it->bidi_p && newpos < ZV)
3684 {
3685 /* With bidi iteration, the region of invisible text
3686 could start and/or end in the middle of a non-base
3687 embedding level. Therefore, we need to skip
3688 invisible text using the bidi iterator, starting at
3689 IT's current position, until we find ourselves
3690 outside the invisible text. Skipping invisible text
3691 _after_ bidi iteration avoids affecting the visual
3692 order of the displayed text when invisible properties
3693 are added or removed. */
3694 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3695 {
3696 /* If we were `reseat'ed to a new paragraph,
3697 determine the paragraph base direction. We need
3698 to do it now because next_element_from_buffer may
3699 not have a chance to do it, if we are going to
3700 skip any text at the beginning, which resets the
3701 FIRST_ELT flag. */
3702 bidi_paragraph_init (it->paragraph_embedding,
3703 &it->bidi_it, 1);
3704 }
3705 do
3706 {
3707 bidi_move_to_visually_next (&it->bidi_it);
3708 }
3709 while (it->stop_charpos <= it->bidi_it.charpos
3710 && it->bidi_it.charpos < newpos);
3711 IT_CHARPOS (*it) = it->bidi_it.charpos;
3712 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3713 /* If we overstepped NEWPOS, record its position in the
3714 iterator, so that we skip invisible text if later the
3715 bidi iteration lands us in the invisible region
3716 again. */
3717 if (IT_CHARPOS (*it) >= newpos)
3718 it->prev_stop = newpos;
3719 }
3720 else
3721 {
3722 IT_CHARPOS (*it) = newpos;
3723 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3724 }
3725
3726 /* If there are before-strings at the start of invisible
3727 text, and the text is invisible because of a text
3728 property, arrange to show before-strings because 20.x did
3729 it that way. (If the text is invisible because of an
3730 overlay property instead of a text property, this is
3731 already handled in the overlay code.) */
3732 if (NILP (overlay)
3733 && get_overlay_strings (it, it->stop_charpos))
3734 {
3735 handled = HANDLED_RECOMPUTE_PROPS;
3736 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3737 }
3738 else if (display_ellipsis_p)
3739 {
3740 /* Make sure that the glyphs of the ellipsis will get
3741 correct `charpos' values. If we would not update
3742 it->position here, the glyphs would belong to the
3743 last visible character _before_ the invisible
3744 text, which confuses `set_cursor_from_row'.
3745
3746 We use the last invisible position instead of the
3747 first because this way the cursor is always drawn on
3748 the first "." of the ellipsis, whenever PT is inside
3749 the invisible text. Otherwise the cursor would be
3750 placed _after_ the ellipsis when the point is after the
3751 first invisible character. */
3752 if (!STRINGP (it->object))
3753 {
3754 it->position.charpos = newpos - 1;
3755 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3756 }
3757 it->ellipsis_p = 1;
3758 /* Let the ellipsis display before
3759 considering any properties of the following char.
3760 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3761 handled = HANDLED_RETURN;
3762 }
3763 }
3764 }
3765
3766 return handled;
3767 }
3768
3769
3770 /* Make iterator IT return `...' next.
3771 Replaces LEN characters from buffer. */
3772
3773 static void
3774 setup_for_ellipsis (struct it *it, int len)
3775 {
3776 /* Use the display table definition for `...'. Invalid glyphs
3777 will be handled by the method returning elements from dpvec. */
3778 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3779 {
3780 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3781 it->dpvec = v->contents;
3782 it->dpend = v->contents + v->header.size;
3783 }
3784 else
3785 {
3786 /* Default `...'. */
3787 it->dpvec = default_invis_vector;
3788 it->dpend = default_invis_vector + 3;
3789 }
3790
3791 it->dpvec_char_len = len;
3792 it->current.dpvec_index = 0;
3793 it->dpvec_face_id = -1;
3794
3795 /* Remember the current face id in case glyphs specify faces.
3796 IT's face is restored in set_iterator_to_next.
3797 saved_face_id was set to preceding char's face in handle_stop. */
3798 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3799 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3800
3801 it->method = GET_FROM_DISPLAY_VECTOR;
3802 it->ellipsis_p = 1;
3803 }
3804
3805
3806 \f
3807 /***********************************************************************
3808 'display' property
3809 ***********************************************************************/
3810
3811 /* Set up iterator IT from `display' property at its current position.
3812 Called from handle_stop.
3813 We return HANDLED_RETURN if some part of the display property
3814 overrides the display of the buffer text itself.
3815 Otherwise we return HANDLED_NORMALLY. */
3816
3817 static enum prop_handled
3818 handle_display_prop (struct it *it)
3819 {
3820 Lisp_Object propval, object, overlay;
3821 struct text_pos *position;
3822 EMACS_INT bufpos;
3823 /* Nonzero if some property replaces the display of the text itself. */
3824 int display_replaced_p = 0;
3825
3826 if (STRINGP (it->string))
3827 {
3828 object = it->string;
3829 position = &it->current.string_pos;
3830 bufpos = CHARPOS (it->current.pos);
3831 }
3832 else
3833 {
3834 XSETWINDOW (object, it->w);
3835 position = &it->current.pos;
3836 bufpos = CHARPOS (*position);
3837 }
3838
3839 /* Reset those iterator values set from display property values. */
3840 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3841 it->space_width = Qnil;
3842 it->font_height = Qnil;
3843 it->voffset = 0;
3844
3845 /* We don't support recursive `display' properties, i.e. string
3846 values that have a string `display' property, that have a string
3847 `display' property etc. */
3848 if (!it->string_from_display_prop_p)
3849 it->area = TEXT_AREA;
3850
3851 propval = get_char_property_and_overlay (make_number (position->charpos),
3852 Qdisplay, object, &overlay);
3853 if (NILP (propval))
3854 return HANDLED_NORMALLY;
3855 /* Now OVERLAY is the overlay that gave us this property, or nil
3856 if it was a text property. */
3857
3858 if (!STRINGP (it->string))
3859 object = it->w->buffer;
3860
3861 display_replaced_p = handle_display_spec (it, propval, object, overlay,
3862 position, bufpos,
3863 FRAME_WINDOW_P (it->f));
3864
3865 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3866 }
3867
3868 /* Subroutine of handle_display_prop. Returns non-zero if the display
3869 specification in SPEC is a replacing specification, i.e. it would
3870 replace the text covered by `display' property with something else,
3871 such as an image or a display string.
3872
3873 See handle_single_display_spec for documentation of arguments.
3874 frame_window_p is non-zero if the window being redisplayed is on a
3875 GUI frame; this argument is used only if IT is NULL, see below.
3876
3877 IT can be NULL, if this is called by the bidi reordering code
3878 through compute_display_string_pos, which see. In that case, this
3879 function only examines SPEC, but does not otherwise "handle" it, in
3880 the sense that it doesn't set up members of IT from the display
3881 spec. */
3882 static int
3883 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
3884 Lisp_Object overlay, struct text_pos *position,
3885 EMACS_INT bufpos, int frame_window_p)
3886 {
3887 int replacing_p = 0;
3888
3889 if (CONSP (spec)
3890 /* Simple specerties. */
3891 && !EQ (XCAR (spec), Qimage)
3892 && !EQ (XCAR (spec), Qspace)
3893 && !EQ (XCAR (spec), Qwhen)
3894 && !EQ (XCAR (spec), Qslice)
3895 && !EQ (XCAR (spec), Qspace_width)
3896 && !EQ (XCAR (spec), Qheight)
3897 && !EQ (XCAR (spec), Qraise)
3898 /* Marginal area specifications. */
3899 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
3900 && !EQ (XCAR (spec), Qleft_fringe)
3901 && !EQ (XCAR (spec), Qright_fringe)
3902 && !NILP (XCAR (spec)))
3903 {
3904 for (; CONSP (spec); spec = XCDR (spec))
3905 {
3906 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
3907 position, bufpos, replacing_p,
3908 frame_window_p))
3909 {
3910 replacing_p = 1;
3911 /* If some text in a string is replaced, `position' no
3912 longer points to the position of `object'. */
3913 if (!it || STRINGP (object))
3914 break;
3915 }
3916 }
3917 }
3918 else if (VECTORP (spec))
3919 {
3920 int i;
3921 for (i = 0; i < ASIZE (spec); ++i)
3922 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
3923 position, bufpos, replacing_p,
3924 frame_window_p))
3925 {
3926 replacing_p = 1;
3927 /* If some text in a string is replaced, `position' no
3928 longer points to the position of `object'. */
3929 if (!it || STRINGP (object))
3930 break;
3931 }
3932 }
3933 else
3934 {
3935 if (handle_single_display_spec (it, spec, object, overlay,
3936 position, bufpos, 0, frame_window_p))
3937 replacing_p = 1;
3938 }
3939
3940 return replacing_p;
3941 }
3942
3943 /* Value is the position of the end of the `display' property starting
3944 at START_POS in OBJECT. */
3945
3946 static struct text_pos
3947 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
3948 {
3949 Lisp_Object end;
3950 struct text_pos end_pos;
3951
3952 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3953 Qdisplay, object, Qnil);
3954 CHARPOS (end_pos) = XFASTINT (end);
3955 if (STRINGP (object))
3956 compute_string_pos (&end_pos, start_pos, it->string);
3957 else
3958 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3959
3960 return end_pos;
3961 }
3962
3963
3964 /* Set up IT from a single `display' property specification SPEC. OBJECT
3965 is the object in which the `display' property was found. *POSITION
3966 is the position in OBJECT at which the `display' property was found.
3967 BUFPOS is the buffer position of OBJECT (different from POSITION if
3968 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
3969 previously saw a display specification which already replaced text
3970 display with something else, for example an image; we ignore such
3971 properties after the first one has been processed.
3972
3973 OVERLAY is the overlay this `display' property came from,
3974 or nil if it was a text property.
3975
3976 If SPEC is a `space' or `image' specification, and in some other
3977 cases too, set *POSITION to the position where the `display'
3978 property ends.
3979
3980 If IT is NULL, only examine the property specification in SPEC, but
3981 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
3982 is intended to be displayed in a window on a GUI frame.
3983
3984 Value is non-zero if something was found which replaces the display
3985 of buffer or string text. */
3986
3987 static int
3988 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
3989 Lisp_Object overlay, struct text_pos *position,
3990 EMACS_INT bufpos, int display_replaced_p,
3991 int frame_window_p)
3992 {
3993 Lisp_Object form;
3994 Lisp_Object location, value;
3995 struct text_pos start_pos = *position;
3996 int valid_p;
3997
3998 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3999 If the result is non-nil, use VALUE instead of SPEC. */
4000 form = Qt;
4001 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4002 {
4003 spec = XCDR (spec);
4004 if (!CONSP (spec))
4005 return 0;
4006 form = XCAR (spec);
4007 spec = XCDR (spec);
4008 }
4009
4010 if (!NILP (form) && !EQ (form, Qt))
4011 {
4012 int count = SPECPDL_INDEX ();
4013 struct gcpro gcpro1;
4014
4015 /* Bind `object' to the object having the `display' property, a
4016 buffer or string. Bind `position' to the position in the
4017 object where the property was found, and `buffer-position'
4018 to the current position in the buffer. */
4019
4020 if (NILP (object))
4021 XSETBUFFER (object, current_buffer);
4022 specbind (Qobject, object);
4023 specbind (Qposition, make_number (CHARPOS (*position)));
4024 specbind (Qbuffer_position, make_number (bufpos));
4025 GCPRO1 (form);
4026 form = safe_eval (form);
4027 UNGCPRO;
4028 unbind_to (count, Qnil);
4029 }
4030
4031 if (NILP (form))
4032 return 0;
4033
4034 /* Handle `(height HEIGHT)' specifications. */
4035 if (CONSP (spec)
4036 && EQ (XCAR (spec), Qheight)
4037 && CONSP (XCDR (spec)))
4038 {
4039 if (it)
4040 {
4041 if (!FRAME_WINDOW_P (it->f))
4042 return 0;
4043
4044 it->font_height = XCAR (XCDR (spec));
4045 if (!NILP (it->font_height))
4046 {
4047 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4048 int new_height = -1;
4049
4050 if (CONSP (it->font_height)
4051 && (EQ (XCAR (it->font_height), Qplus)
4052 || EQ (XCAR (it->font_height), Qminus))
4053 && CONSP (XCDR (it->font_height))
4054 && INTEGERP (XCAR (XCDR (it->font_height))))
4055 {
4056 /* `(+ N)' or `(- N)' where N is an integer. */
4057 int steps = XINT (XCAR (XCDR (it->font_height)));
4058 if (EQ (XCAR (it->font_height), Qplus))
4059 steps = - steps;
4060 it->face_id = smaller_face (it->f, it->face_id, steps);
4061 }
4062 else if (FUNCTIONP (it->font_height))
4063 {
4064 /* Call function with current height as argument.
4065 Value is the new height. */
4066 Lisp_Object height;
4067 height = safe_call1 (it->font_height,
4068 face->lface[LFACE_HEIGHT_INDEX]);
4069 if (NUMBERP (height))
4070 new_height = XFLOATINT (height);
4071 }
4072 else if (NUMBERP (it->font_height))
4073 {
4074 /* Value is a multiple of the canonical char height. */
4075 struct face *f;
4076
4077 f = FACE_FROM_ID (it->f,
4078 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4079 new_height = (XFLOATINT (it->font_height)
4080 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4081 }
4082 else
4083 {
4084 /* Evaluate IT->font_height with `height' bound to the
4085 current specified height to get the new height. */
4086 int count = SPECPDL_INDEX ();
4087
4088 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4089 value = safe_eval (it->font_height);
4090 unbind_to (count, Qnil);
4091
4092 if (NUMBERP (value))
4093 new_height = XFLOATINT (value);
4094 }
4095
4096 if (new_height > 0)
4097 it->face_id = face_with_height (it->f, it->face_id, new_height);
4098 }
4099 }
4100
4101 return 0;
4102 }
4103
4104 /* Handle `(space-width WIDTH)'. */
4105 if (CONSP (spec)
4106 && EQ (XCAR (spec), Qspace_width)
4107 && CONSP (XCDR (spec)))
4108 {
4109 if (it)
4110 {
4111 if (!FRAME_WINDOW_P (it->f))
4112 return 0;
4113
4114 value = XCAR (XCDR (spec));
4115 if (NUMBERP (value) && XFLOATINT (value) > 0)
4116 it->space_width = value;
4117 }
4118
4119 return 0;
4120 }
4121
4122 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4123 if (CONSP (spec)
4124 && EQ (XCAR (spec), Qslice))
4125 {
4126 Lisp_Object tem;
4127
4128 if (it)
4129 {
4130 if (!FRAME_WINDOW_P (it->f))
4131 return 0;
4132
4133 if (tem = XCDR (spec), CONSP (tem))
4134 {
4135 it->slice.x = XCAR (tem);
4136 if (tem = XCDR (tem), CONSP (tem))
4137 {
4138 it->slice.y = XCAR (tem);
4139 if (tem = XCDR (tem), CONSP (tem))
4140 {
4141 it->slice.width = XCAR (tem);
4142 if (tem = XCDR (tem), CONSP (tem))
4143 it->slice.height = XCAR (tem);
4144 }
4145 }
4146 }
4147 }
4148
4149 return 0;
4150 }
4151
4152 /* Handle `(raise FACTOR)'. */
4153 if (CONSP (spec)
4154 && EQ (XCAR (spec), Qraise)
4155 && CONSP (XCDR (spec)))
4156 {
4157 if (it)
4158 {
4159 if (!FRAME_WINDOW_P (it->f))
4160 return 0;
4161
4162 #ifdef HAVE_WINDOW_SYSTEM
4163 value = XCAR (XCDR (spec));
4164 if (NUMBERP (value))
4165 {
4166 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4167 it->voffset = - (XFLOATINT (value)
4168 * (FONT_HEIGHT (face->font)));
4169 }
4170 #endif /* HAVE_WINDOW_SYSTEM */
4171 }
4172
4173 return 0;
4174 }
4175
4176 /* Don't handle the other kinds of display specifications
4177 inside a string that we got from a `display' property. */
4178 if (it && it->string_from_display_prop_p)
4179 return 0;
4180
4181 /* Characters having this form of property are not displayed, so
4182 we have to find the end of the property. */
4183 if (it)
4184 {
4185 start_pos = *position;
4186 *position = display_prop_end (it, object, start_pos);
4187 }
4188 value = Qnil;
4189
4190 /* Stop the scan at that end position--we assume that all
4191 text properties change there. */
4192 if (it)
4193 it->stop_charpos = position->charpos;
4194
4195 /* Handle `(left-fringe BITMAP [FACE])'
4196 and `(right-fringe BITMAP [FACE])'. */
4197 if (CONSP (spec)
4198 && (EQ (XCAR (spec), Qleft_fringe)
4199 || EQ (XCAR (spec), Qright_fringe))
4200 && CONSP (XCDR (spec)))
4201 {
4202 int fringe_bitmap;
4203
4204 if (it)
4205 {
4206 if (!FRAME_WINDOW_P (it->f))
4207 /* If we return here, POSITION has been advanced
4208 across the text with this property. */
4209 return 0;
4210 }
4211 else if (!frame_window_p)
4212 return 0;
4213
4214 #ifdef HAVE_WINDOW_SYSTEM
4215 value = XCAR (XCDR (spec));
4216 if (!SYMBOLP (value)
4217 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4218 /* If we return here, POSITION has been advanced
4219 across the text with this property. */
4220 return 0;
4221
4222 if (it)
4223 {
4224 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4225
4226 if (CONSP (XCDR (XCDR (spec))))
4227 {
4228 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4229 int face_id2 = lookup_derived_face (it->f, face_name,
4230 FRINGE_FACE_ID, 0);
4231 if (face_id2 >= 0)
4232 face_id = face_id2;
4233 }
4234
4235 /* Save current settings of IT so that we can restore them
4236 when we are finished with the glyph property value. */
4237 push_it (it, position);
4238
4239 it->area = TEXT_AREA;
4240 it->what = IT_IMAGE;
4241 it->image_id = -1; /* no image */
4242 it->position = start_pos;
4243 it->object = NILP (object) ? it->w->buffer : object;
4244 it->method = GET_FROM_IMAGE;
4245 it->from_overlay = Qnil;
4246 it->face_id = face_id;
4247
4248 /* Say that we haven't consumed the characters with
4249 `display' property yet. The call to pop_it in
4250 set_iterator_to_next will clean this up. */
4251 *position = start_pos;
4252
4253 if (EQ (XCAR (spec), Qleft_fringe))
4254 {
4255 it->left_user_fringe_bitmap = fringe_bitmap;
4256 it->left_user_fringe_face_id = face_id;
4257 }
4258 else
4259 {
4260 it->right_user_fringe_bitmap = fringe_bitmap;
4261 it->right_user_fringe_face_id = face_id;
4262 }
4263 }
4264 #endif /* HAVE_WINDOW_SYSTEM */
4265 return 1;
4266 }
4267
4268 /* Prepare to handle `((margin left-margin) ...)',
4269 `((margin right-margin) ...)' and `((margin nil) ...)'
4270 prefixes for display specifications. */
4271 location = Qunbound;
4272 if (CONSP (spec) && CONSP (XCAR (spec)))
4273 {
4274 Lisp_Object tem;
4275
4276 value = XCDR (spec);
4277 if (CONSP (value))
4278 value = XCAR (value);
4279
4280 tem = XCAR (spec);
4281 if (EQ (XCAR (tem), Qmargin)
4282 && (tem = XCDR (tem),
4283 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4284 (NILP (tem)
4285 || EQ (tem, Qleft_margin)
4286 || EQ (tem, Qright_margin))))
4287 location = tem;
4288 }
4289
4290 if (EQ (location, Qunbound))
4291 {
4292 location = Qnil;
4293 value = spec;
4294 }
4295
4296 /* After this point, VALUE is the property after any
4297 margin prefix has been stripped. It must be a string,
4298 an image specification, or `(space ...)'.
4299
4300 LOCATION specifies where to display: `left-margin',
4301 `right-margin' or nil. */
4302
4303 valid_p = (STRINGP (value)
4304 #ifdef HAVE_WINDOW_SYSTEM
4305 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4306 && valid_image_p (value))
4307 #endif /* not HAVE_WINDOW_SYSTEM */
4308 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4309
4310 if (valid_p && !display_replaced_p)
4311 {
4312 if (!it)
4313 return 1;
4314
4315 /* Save current settings of IT so that we can restore them
4316 when we are finished with the glyph property value. */
4317 push_it (it, position);
4318 it->from_overlay = overlay;
4319
4320 if (NILP (location))
4321 it->area = TEXT_AREA;
4322 else if (EQ (location, Qleft_margin))
4323 it->area = LEFT_MARGIN_AREA;
4324 else
4325 it->area = RIGHT_MARGIN_AREA;
4326
4327 if (STRINGP (value))
4328 {
4329 it->string = value;
4330 it->multibyte_p = STRING_MULTIBYTE (it->string);
4331 it->current.overlay_string_index = -1;
4332 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4333 it->end_charpos = it->string_nchars = SCHARS (it->string);
4334 it->method = GET_FROM_STRING;
4335 it->stop_charpos = 0;
4336 it->string_from_display_prop_p = 1;
4337 /* Say that we haven't consumed the characters with
4338 `display' property yet. The call to pop_it in
4339 set_iterator_to_next will clean this up. */
4340 if (BUFFERP (object))
4341 *position = start_pos;
4342 }
4343 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4344 {
4345 it->method = GET_FROM_STRETCH;
4346 it->object = value;
4347 *position = it->position = start_pos;
4348 }
4349 #ifdef HAVE_WINDOW_SYSTEM
4350 else
4351 {
4352 it->what = IT_IMAGE;
4353 it->image_id = lookup_image (it->f, value);
4354 it->position = start_pos;
4355 it->object = NILP (object) ? it->w->buffer : object;
4356 it->method = GET_FROM_IMAGE;
4357
4358 /* Say that we haven't consumed the characters with
4359 `display' property yet. The call to pop_it in
4360 set_iterator_to_next will clean this up. */
4361 *position = start_pos;
4362 }
4363 #endif /* HAVE_WINDOW_SYSTEM */
4364
4365 return 1;
4366 }
4367
4368 /* Invalid property or property not supported. Restore
4369 POSITION to what it was before. */
4370 *position = start_pos;
4371 return 0;
4372 }
4373
4374 /* Check if PROP is a display property value whose text should be
4375 treated as intangible. OVERLAY is the overlay from which PROP
4376 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4377 specify the buffer position covered by PROP. */
4378
4379 int
4380 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4381 EMACS_INT charpos, EMACS_INT bytepos)
4382 {
4383 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4384 struct text_pos position;
4385
4386 SET_TEXT_POS (position, charpos, bytepos);
4387 return handle_display_spec (NULL, prop, Qnil, overlay,
4388 &position, charpos, frame_window_p);
4389 }
4390
4391
4392 /* Return 1 if PROP is a display sub-property value containing STRING.
4393
4394 Implementation note: this and the following function are really
4395 special cases of handle_display_spec and
4396 handle_single_display_spec, and should ideally use the same code.
4397 Until they do, these two pairs must be consistent and must be
4398 modified in sync. */
4399
4400 static int
4401 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4402 {
4403 if (EQ (string, prop))
4404 return 1;
4405
4406 /* Skip over `when FORM'. */
4407 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4408 {
4409 prop = XCDR (prop);
4410 if (!CONSP (prop))
4411 return 0;
4412 /* Actually, the condition following `when' should be eval'ed,
4413 like handle_single_display_spec does, and we should return
4414 zero if it evaluates to nil. However, this function is
4415 called only when the buffer was already displayed and some
4416 glyph in the glyph matrix was found to come from a display
4417 string. Therefore, the condition was already evaluated, and
4418 the result was non-nil, otherwise the display string wouldn't
4419 have been displayed and we would have never been called for
4420 this property. Thus, we can skip the evaluation and assume
4421 its result is non-nil. */
4422 prop = XCDR (prop);
4423 }
4424
4425 if (CONSP (prop))
4426 /* Skip over `margin LOCATION'. */
4427 if (EQ (XCAR (prop), Qmargin))
4428 {
4429 prop = XCDR (prop);
4430 if (!CONSP (prop))
4431 return 0;
4432
4433 prop = XCDR (prop);
4434 if (!CONSP (prop))
4435 return 0;
4436 }
4437
4438 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4439 }
4440
4441
4442 /* Return 1 if STRING appears in the `display' property PROP. */
4443
4444 static int
4445 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4446 {
4447 if (CONSP (prop)
4448 && !EQ (XCAR (prop), Qwhen)
4449 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4450 {
4451 /* A list of sub-properties. */
4452 while (CONSP (prop))
4453 {
4454 if (single_display_spec_string_p (XCAR (prop), string))
4455 return 1;
4456 prop = XCDR (prop);
4457 }
4458 }
4459 else if (VECTORP (prop))
4460 {
4461 /* A vector of sub-properties. */
4462 int i;
4463 for (i = 0; i < ASIZE (prop); ++i)
4464 if (single_display_spec_string_p (AREF (prop, i), string))
4465 return 1;
4466 }
4467 else
4468 return single_display_spec_string_p (prop, string);
4469
4470 return 0;
4471 }
4472
4473 /* Look for STRING in overlays and text properties in the current
4474 buffer, between character positions FROM and TO (excluding TO).
4475 BACK_P non-zero means look back (in this case, TO is supposed to be
4476 less than FROM).
4477 Value is the first character position where STRING was found, or
4478 zero if it wasn't found before hitting TO.
4479
4480 This function may only use code that doesn't eval because it is
4481 called asynchronously from note_mouse_highlight. */
4482
4483 static EMACS_INT
4484 string_buffer_position_lim (Lisp_Object string,
4485 EMACS_INT from, EMACS_INT to, int back_p)
4486 {
4487 Lisp_Object limit, prop, pos;
4488 int found = 0;
4489
4490 pos = make_number (from);
4491
4492 if (!back_p) /* looking forward */
4493 {
4494 limit = make_number (min (to, ZV));
4495 while (!found && !EQ (pos, limit))
4496 {
4497 prop = Fget_char_property (pos, Qdisplay, Qnil);
4498 if (!NILP (prop) && display_prop_string_p (prop, string))
4499 found = 1;
4500 else
4501 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4502 limit);
4503 }
4504 }
4505 else /* looking back */
4506 {
4507 limit = make_number (max (to, BEGV));
4508 while (!found && !EQ (pos, limit))
4509 {
4510 prop = Fget_char_property (pos, Qdisplay, Qnil);
4511 if (!NILP (prop) && display_prop_string_p (prop, string))
4512 found = 1;
4513 else
4514 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4515 limit);
4516 }
4517 }
4518
4519 return found ? XINT (pos) : 0;
4520 }
4521
4522 /* Determine which buffer position in current buffer STRING comes from.
4523 AROUND_CHARPOS is an approximate position where it could come from.
4524 Value is the buffer position or 0 if it couldn't be determined.
4525
4526 This function is necessary because we don't record buffer positions
4527 in glyphs generated from strings (to keep struct glyph small).
4528 This function may only use code that doesn't eval because it is
4529 called asynchronously from note_mouse_highlight. */
4530
4531 static EMACS_INT
4532 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4533 {
4534 const int MAX_DISTANCE = 1000;
4535 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4536 around_charpos + MAX_DISTANCE,
4537 0);
4538
4539 if (!found)
4540 found = string_buffer_position_lim (string, around_charpos,
4541 around_charpos - MAX_DISTANCE, 1);
4542 return found;
4543 }
4544
4545
4546 \f
4547 /***********************************************************************
4548 `composition' property
4549 ***********************************************************************/
4550
4551 /* Set up iterator IT from `composition' property at its current
4552 position. Called from handle_stop. */
4553
4554 static enum prop_handled
4555 handle_composition_prop (struct it *it)
4556 {
4557 Lisp_Object prop, string;
4558 EMACS_INT pos, pos_byte, start, end;
4559
4560 if (STRINGP (it->string))
4561 {
4562 unsigned char *s;
4563
4564 pos = IT_STRING_CHARPOS (*it);
4565 pos_byte = IT_STRING_BYTEPOS (*it);
4566 string = it->string;
4567 s = SDATA (string) + pos_byte;
4568 it->c = STRING_CHAR (s);
4569 }
4570 else
4571 {
4572 pos = IT_CHARPOS (*it);
4573 pos_byte = IT_BYTEPOS (*it);
4574 string = Qnil;
4575 it->c = FETCH_CHAR (pos_byte);
4576 }
4577
4578 /* If there's a valid composition and point is not inside of the
4579 composition (in the case that the composition is from the current
4580 buffer), draw a glyph composed from the composition components. */
4581 if (find_composition (pos, -1, &start, &end, &prop, string)
4582 && COMPOSITION_VALID_P (start, end, prop)
4583 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4584 {
4585 if (start < pos)
4586 /* As we can't handle this situation (perhaps font-lock added
4587 a new composition), we just return here hoping that next
4588 redisplay will detect this composition much earlier. */
4589 return HANDLED_NORMALLY;
4590 if (start != pos)
4591 {
4592 if (STRINGP (it->string))
4593 pos_byte = string_char_to_byte (it->string, start);
4594 else
4595 pos_byte = CHAR_TO_BYTE (start);
4596 }
4597 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4598 prop, string);
4599
4600 if (it->cmp_it.id >= 0)
4601 {
4602 it->cmp_it.ch = -1;
4603 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4604 it->cmp_it.nglyphs = -1;
4605 }
4606 }
4607
4608 return HANDLED_NORMALLY;
4609 }
4610
4611
4612 \f
4613 /***********************************************************************
4614 Overlay strings
4615 ***********************************************************************/
4616
4617 /* The following structure is used to record overlay strings for
4618 later sorting in load_overlay_strings. */
4619
4620 struct overlay_entry
4621 {
4622 Lisp_Object overlay;
4623 Lisp_Object string;
4624 int priority;
4625 int after_string_p;
4626 };
4627
4628
4629 /* Set up iterator IT from overlay strings at its current position.
4630 Called from handle_stop. */
4631
4632 static enum prop_handled
4633 handle_overlay_change (struct it *it)
4634 {
4635 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4636 return HANDLED_RECOMPUTE_PROPS;
4637 else
4638 return HANDLED_NORMALLY;
4639 }
4640
4641
4642 /* Set up the next overlay string for delivery by IT, if there is an
4643 overlay string to deliver. Called by set_iterator_to_next when the
4644 end of the current overlay string is reached. If there are more
4645 overlay strings to display, IT->string and
4646 IT->current.overlay_string_index are set appropriately here.
4647 Otherwise IT->string is set to nil. */
4648
4649 static void
4650 next_overlay_string (struct it *it)
4651 {
4652 ++it->current.overlay_string_index;
4653 if (it->current.overlay_string_index == it->n_overlay_strings)
4654 {
4655 /* No more overlay strings. Restore IT's settings to what
4656 they were before overlay strings were processed, and
4657 continue to deliver from current_buffer. */
4658
4659 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4660 pop_it (it);
4661 xassert (it->sp > 0
4662 || (NILP (it->string)
4663 && it->method == GET_FROM_BUFFER
4664 && it->stop_charpos >= BEGV
4665 && it->stop_charpos <= it->end_charpos));
4666 it->current.overlay_string_index = -1;
4667 it->n_overlay_strings = 0;
4668 it->overlay_strings_charpos = -1;
4669
4670 /* If we're at the end of the buffer, record that we have
4671 processed the overlay strings there already, so that
4672 next_element_from_buffer doesn't try it again. */
4673 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4674 it->overlay_strings_at_end_processed_p = 1;
4675 }
4676 else
4677 {
4678 /* There are more overlay strings to process. If
4679 IT->current.overlay_string_index has advanced to a position
4680 where we must load IT->overlay_strings with more strings, do
4681 it. We must load at the IT->overlay_strings_charpos where
4682 IT->n_overlay_strings was originally computed; when invisible
4683 text is present, this might not be IT_CHARPOS (Bug#7016). */
4684 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4685
4686 if (it->current.overlay_string_index && i == 0)
4687 load_overlay_strings (it, it->overlay_strings_charpos);
4688
4689 /* Initialize IT to deliver display elements from the overlay
4690 string. */
4691 it->string = it->overlay_strings[i];
4692 it->multibyte_p = STRING_MULTIBYTE (it->string);
4693 SET_TEXT_POS (it->current.string_pos, 0, 0);
4694 it->method = GET_FROM_STRING;
4695 it->stop_charpos = 0;
4696 if (it->cmp_it.stop_pos >= 0)
4697 it->cmp_it.stop_pos = 0;
4698 }
4699
4700 CHECK_IT (it);
4701 }
4702
4703
4704 /* Compare two overlay_entry structures E1 and E2. Used as a
4705 comparison function for qsort in load_overlay_strings. Overlay
4706 strings for the same position are sorted so that
4707
4708 1. All after-strings come in front of before-strings, except
4709 when they come from the same overlay.
4710
4711 2. Within after-strings, strings are sorted so that overlay strings
4712 from overlays with higher priorities come first.
4713
4714 2. Within before-strings, strings are sorted so that overlay
4715 strings from overlays with higher priorities come last.
4716
4717 Value is analogous to strcmp. */
4718
4719
4720 static int
4721 compare_overlay_entries (const void *e1, const void *e2)
4722 {
4723 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4724 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4725 int result;
4726
4727 if (entry1->after_string_p != entry2->after_string_p)
4728 {
4729 /* Let after-strings appear in front of before-strings if
4730 they come from different overlays. */
4731 if (EQ (entry1->overlay, entry2->overlay))
4732 result = entry1->after_string_p ? 1 : -1;
4733 else
4734 result = entry1->after_string_p ? -1 : 1;
4735 }
4736 else if (entry1->after_string_p)
4737 /* After-strings sorted in order of decreasing priority. */
4738 result = entry2->priority - entry1->priority;
4739 else
4740 /* Before-strings sorted in order of increasing priority. */
4741 result = entry1->priority - entry2->priority;
4742
4743 return result;
4744 }
4745
4746
4747 /* Load the vector IT->overlay_strings with overlay strings from IT's
4748 current buffer position, or from CHARPOS if that is > 0. Set
4749 IT->n_overlays to the total number of overlay strings found.
4750
4751 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4752 a time. On entry into load_overlay_strings,
4753 IT->current.overlay_string_index gives the number of overlay
4754 strings that have already been loaded by previous calls to this
4755 function.
4756
4757 IT->add_overlay_start contains an additional overlay start
4758 position to consider for taking overlay strings from, if non-zero.
4759 This position comes into play when the overlay has an `invisible'
4760 property, and both before and after-strings. When we've skipped to
4761 the end of the overlay, because of its `invisible' property, we
4762 nevertheless want its before-string to appear.
4763 IT->add_overlay_start will contain the overlay start position
4764 in this case.
4765
4766 Overlay strings are sorted so that after-string strings come in
4767 front of before-string strings. Within before and after-strings,
4768 strings are sorted by overlay priority. See also function
4769 compare_overlay_entries. */
4770
4771 static void
4772 load_overlay_strings (struct it *it, EMACS_INT charpos)
4773 {
4774 Lisp_Object overlay, window, str, invisible;
4775 struct Lisp_Overlay *ov;
4776 EMACS_INT start, end;
4777 int size = 20;
4778 int n = 0, i, j, invis_p;
4779 struct overlay_entry *entries
4780 = (struct overlay_entry *) alloca (size * sizeof *entries);
4781
4782 if (charpos <= 0)
4783 charpos = IT_CHARPOS (*it);
4784
4785 /* Append the overlay string STRING of overlay OVERLAY to vector
4786 `entries' which has size `size' and currently contains `n'
4787 elements. AFTER_P non-zero means STRING is an after-string of
4788 OVERLAY. */
4789 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4790 do \
4791 { \
4792 Lisp_Object priority; \
4793 \
4794 if (n == size) \
4795 { \
4796 int new_size = 2 * size; \
4797 struct overlay_entry *old = entries; \
4798 entries = \
4799 (struct overlay_entry *) alloca (new_size \
4800 * sizeof *entries); \
4801 memcpy (entries, old, size * sizeof *entries); \
4802 size = new_size; \
4803 } \
4804 \
4805 entries[n].string = (STRING); \
4806 entries[n].overlay = (OVERLAY); \
4807 priority = Foverlay_get ((OVERLAY), Qpriority); \
4808 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4809 entries[n].after_string_p = (AFTER_P); \
4810 ++n; \
4811 } \
4812 while (0)
4813
4814 /* Process overlay before the overlay center. */
4815 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4816 {
4817 XSETMISC (overlay, ov);
4818 xassert (OVERLAYP (overlay));
4819 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4820 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4821
4822 if (end < charpos)
4823 break;
4824
4825 /* Skip this overlay if it doesn't start or end at IT's current
4826 position. */
4827 if (end != charpos && start != charpos)
4828 continue;
4829
4830 /* Skip this overlay if it doesn't apply to IT->w. */
4831 window = Foverlay_get (overlay, Qwindow);
4832 if (WINDOWP (window) && XWINDOW (window) != it->w)
4833 continue;
4834
4835 /* If the text ``under'' the overlay is invisible, both before-
4836 and after-strings from this overlay are visible; start and
4837 end position are indistinguishable. */
4838 invisible = Foverlay_get (overlay, Qinvisible);
4839 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4840
4841 /* If overlay has a non-empty before-string, record it. */
4842 if ((start == charpos || (end == charpos && invis_p))
4843 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4844 && SCHARS (str))
4845 RECORD_OVERLAY_STRING (overlay, str, 0);
4846
4847 /* If overlay has a non-empty after-string, record it. */
4848 if ((end == charpos || (start == charpos && invis_p))
4849 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4850 && SCHARS (str))
4851 RECORD_OVERLAY_STRING (overlay, str, 1);
4852 }
4853
4854 /* Process overlays after the overlay center. */
4855 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4856 {
4857 XSETMISC (overlay, ov);
4858 xassert (OVERLAYP (overlay));
4859 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4860 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4861
4862 if (start > charpos)
4863 break;
4864
4865 /* Skip this overlay if it doesn't start or end at IT's current
4866 position. */
4867 if (end != charpos && start != charpos)
4868 continue;
4869
4870 /* Skip this overlay if it doesn't apply to IT->w. */
4871 window = Foverlay_get (overlay, Qwindow);
4872 if (WINDOWP (window) && XWINDOW (window) != it->w)
4873 continue;
4874
4875 /* If the text ``under'' the overlay is invisible, it has a zero
4876 dimension, and both before- and after-strings apply. */
4877 invisible = Foverlay_get (overlay, Qinvisible);
4878 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4879
4880 /* If overlay has a non-empty before-string, record it. */
4881 if ((start == charpos || (end == charpos && invis_p))
4882 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4883 && SCHARS (str))
4884 RECORD_OVERLAY_STRING (overlay, str, 0);
4885
4886 /* If overlay has a non-empty after-string, record it. */
4887 if ((end == charpos || (start == charpos && invis_p))
4888 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4889 && SCHARS (str))
4890 RECORD_OVERLAY_STRING (overlay, str, 1);
4891 }
4892
4893 #undef RECORD_OVERLAY_STRING
4894
4895 /* Sort entries. */
4896 if (n > 1)
4897 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4898
4899 /* Record number of overlay strings, and where we computed it. */
4900 it->n_overlay_strings = n;
4901 it->overlay_strings_charpos = charpos;
4902
4903 /* IT->current.overlay_string_index is the number of overlay strings
4904 that have already been consumed by IT. Copy some of the
4905 remaining overlay strings to IT->overlay_strings. */
4906 i = 0;
4907 j = it->current.overlay_string_index;
4908 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4909 {
4910 it->overlay_strings[i] = entries[j].string;
4911 it->string_overlays[i++] = entries[j++].overlay;
4912 }
4913
4914 CHECK_IT (it);
4915 }
4916
4917
4918 /* Get the first chunk of overlay strings at IT's current buffer
4919 position, or at CHARPOS if that is > 0. Value is non-zero if at
4920 least one overlay string was found. */
4921
4922 static int
4923 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
4924 {
4925 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4926 process. This fills IT->overlay_strings with strings, and sets
4927 IT->n_overlay_strings to the total number of strings to process.
4928 IT->pos.overlay_string_index has to be set temporarily to zero
4929 because load_overlay_strings needs this; it must be set to -1
4930 when no overlay strings are found because a zero value would
4931 indicate a position in the first overlay string. */
4932 it->current.overlay_string_index = 0;
4933 load_overlay_strings (it, charpos);
4934
4935 /* If we found overlay strings, set up IT to deliver display
4936 elements from the first one. Otherwise set up IT to deliver
4937 from current_buffer. */
4938 if (it->n_overlay_strings)
4939 {
4940 /* Make sure we know settings in current_buffer, so that we can
4941 restore meaningful values when we're done with the overlay
4942 strings. */
4943 if (compute_stop_p)
4944 compute_stop_pos (it);
4945 xassert (it->face_id >= 0);
4946
4947 /* Save IT's settings. They are restored after all overlay
4948 strings have been processed. */
4949 xassert (!compute_stop_p || it->sp == 0);
4950
4951 /* When called from handle_stop, there might be an empty display
4952 string loaded. In that case, don't bother saving it. */
4953 if (!STRINGP (it->string) || SCHARS (it->string))
4954 push_it (it, NULL);
4955
4956 /* Set up IT to deliver display elements from the first overlay
4957 string. */
4958 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4959 it->string = it->overlay_strings[0];
4960 it->from_overlay = Qnil;
4961 it->stop_charpos = 0;
4962 xassert (STRINGP (it->string));
4963 it->end_charpos = SCHARS (it->string);
4964 it->multibyte_p = STRING_MULTIBYTE (it->string);
4965 it->method = GET_FROM_STRING;
4966 return 1;
4967 }
4968
4969 it->current.overlay_string_index = -1;
4970 return 0;
4971 }
4972
4973 static int
4974 get_overlay_strings (struct it *it, EMACS_INT charpos)
4975 {
4976 it->string = Qnil;
4977 it->method = GET_FROM_BUFFER;
4978
4979 (void) get_overlay_strings_1 (it, charpos, 1);
4980
4981 CHECK_IT (it);
4982
4983 /* Value is non-zero if we found at least one overlay string. */
4984 return STRINGP (it->string);
4985 }
4986
4987
4988 \f
4989 /***********************************************************************
4990 Saving and restoring state
4991 ***********************************************************************/
4992
4993 /* Save current settings of IT on IT->stack. Called, for example,
4994 before setting up IT for an overlay string, to be able to restore
4995 IT's settings to what they were after the overlay string has been
4996 processed. If POSITION is non-NULL, it is the position to save on
4997 the stack instead of IT->position. */
4998
4999 static void
5000 push_it (struct it *it, struct text_pos *position)
5001 {
5002 struct iterator_stack_entry *p;
5003
5004 xassert (it->sp < IT_STACK_SIZE);
5005 p = it->stack + it->sp;
5006
5007 p->stop_charpos = it->stop_charpos;
5008 p->prev_stop = it->prev_stop;
5009 p->base_level_stop = it->base_level_stop;
5010 p->cmp_it = it->cmp_it;
5011 xassert (it->face_id >= 0);
5012 p->face_id = it->face_id;
5013 p->string = it->string;
5014 p->method = it->method;
5015 p->from_overlay = it->from_overlay;
5016 switch (p->method)
5017 {
5018 case GET_FROM_IMAGE:
5019 p->u.image.object = it->object;
5020 p->u.image.image_id = it->image_id;
5021 p->u.image.slice = it->slice;
5022 break;
5023 case GET_FROM_STRETCH:
5024 p->u.stretch.object = it->object;
5025 break;
5026 }
5027 p->position = position ? *position : it->position;
5028 p->current = it->current;
5029 p->end_charpos = it->end_charpos;
5030 p->string_nchars = it->string_nchars;
5031 p->area = it->area;
5032 p->multibyte_p = it->multibyte_p;
5033 p->avoid_cursor_p = it->avoid_cursor_p;
5034 p->space_width = it->space_width;
5035 p->font_height = it->font_height;
5036 p->voffset = it->voffset;
5037 p->string_from_display_prop_p = it->string_from_display_prop_p;
5038 p->display_ellipsis_p = 0;
5039 p->line_wrap = it->line_wrap;
5040 ++it->sp;
5041 }
5042
5043 static void
5044 iterate_out_of_display_property (struct it *it)
5045 {
5046 /* Maybe initialize paragraph direction. If we are at the beginning
5047 of a new paragraph, next_element_from_buffer may not have a
5048 chance to do that. */
5049 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5050 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5051 /* prev_stop can be zero, so check against BEGV as well. */
5052 while (it->bidi_it.charpos >= BEGV
5053 && it->prev_stop <= it->bidi_it.charpos
5054 && it->bidi_it.charpos < CHARPOS (it->position))
5055 bidi_move_to_visually_next (&it->bidi_it);
5056 /* Record the stop_pos we just crossed, for when we cross it
5057 back, maybe. */
5058 if (it->bidi_it.charpos > CHARPOS (it->position))
5059 it->prev_stop = CHARPOS (it->position);
5060 /* If we ended up not where pop_it put us, resync IT's
5061 positional members with the bidi iterator. */
5062 if (it->bidi_it.charpos != CHARPOS (it->position))
5063 {
5064 SET_TEXT_POS (it->position,
5065 it->bidi_it.charpos, it->bidi_it.bytepos);
5066 it->current.pos = it->position;
5067 }
5068 }
5069
5070 /* Restore IT's settings from IT->stack. Called, for example, when no
5071 more overlay strings must be processed, and we return to delivering
5072 display elements from a buffer, or when the end of a string from a
5073 `display' property is reached and we return to delivering display
5074 elements from an overlay string, or from a buffer. */
5075
5076 static void
5077 pop_it (struct it *it)
5078 {
5079 struct iterator_stack_entry *p;
5080
5081 xassert (it->sp > 0);
5082 --it->sp;
5083 p = it->stack + it->sp;
5084 it->stop_charpos = p->stop_charpos;
5085 it->prev_stop = p->prev_stop;
5086 it->base_level_stop = p->base_level_stop;
5087 it->cmp_it = p->cmp_it;
5088 it->face_id = p->face_id;
5089 it->current = p->current;
5090 it->position = p->position;
5091 it->string = p->string;
5092 it->from_overlay = p->from_overlay;
5093 if (NILP (it->string))
5094 SET_TEXT_POS (it->current.string_pos, -1, -1);
5095 it->method = p->method;
5096 switch (it->method)
5097 {
5098 case GET_FROM_IMAGE:
5099 it->image_id = p->u.image.image_id;
5100 it->object = p->u.image.object;
5101 it->slice = p->u.image.slice;
5102 break;
5103 case GET_FROM_STRETCH:
5104 it->object = p->u.comp.object;
5105 break;
5106 case GET_FROM_BUFFER:
5107 it->object = it->w->buffer;
5108 if (it->bidi_p)
5109 {
5110 /* Bidi-iterate until we get out of the portion of text, if
5111 any, covered by a `display' text property or an overlay
5112 with `display' property. (We cannot just jump there,
5113 because the internal coherency of the bidi iterator state
5114 can not be preserved across such jumps.) We also must
5115 determine the paragraph base direction if the overlay we
5116 just processed is at the beginning of a new
5117 paragraph. */
5118 iterate_out_of_display_property (it);
5119 }
5120 break;
5121 case GET_FROM_STRING:
5122 it->object = it->string;
5123 break;
5124 case GET_FROM_DISPLAY_VECTOR:
5125 if (it->s)
5126 it->method = GET_FROM_C_STRING;
5127 else if (STRINGP (it->string))
5128 it->method = GET_FROM_STRING;
5129 else
5130 {
5131 it->method = GET_FROM_BUFFER;
5132 it->object = it->w->buffer;
5133 }
5134 }
5135 it->end_charpos = p->end_charpos;
5136 it->string_nchars = p->string_nchars;
5137 it->area = p->area;
5138 it->multibyte_p = p->multibyte_p;
5139 it->avoid_cursor_p = p->avoid_cursor_p;
5140 it->space_width = p->space_width;
5141 it->font_height = p->font_height;
5142 it->voffset = p->voffset;
5143 it->string_from_display_prop_p = p->string_from_display_prop_p;
5144 it->line_wrap = p->line_wrap;
5145 }
5146
5147
5148 \f
5149 /***********************************************************************
5150 Moving over lines
5151 ***********************************************************************/
5152
5153 /* Set IT's current position to the previous line start. */
5154
5155 static void
5156 back_to_previous_line_start (struct it *it)
5157 {
5158 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5159 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5160 }
5161
5162
5163 /* Move IT to the next line start.
5164
5165 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5166 we skipped over part of the text (as opposed to moving the iterator
5167 continuously over the text). Otherwise, don't change the value
5168 of *SKIPPED_P.
5169
5170 Newlines may come from buffer text, overlay strings, or strings
5171 displayed via the `display' property. That's the reason we can't
5172 simply use find_next_newline_no_quit.
5173
5174 Note that this function may not skip over invisible text that is so
5175 because of text properties and immediately follows a newline. If
5176 it would, function reseat_at_next_visible_line_start, when called
5177 from set_iterator_to_next, would effectively make invisible
5178 characters following a newline part of the wrong glyph row, which
5179 leads to wrong cursor motion. */
5180
5181 static int
5182 forward_to_next_line_start (struct it *it, int *skipped_p)
5183 {
5184 EMACS_INT old_selective;
5185 int newline_found_p, n;
5186 const int MAX_NEWLINE_DISTANCE = 500;
5187
5188 /* If already on a newline, just consume it to avoid unintended
5189 skipping over invisible text below. */
5190 if (it->what == IT_CHARACTER
5191 && it->c == '\n'
5192 && CHARPOS (it->position) == IT_CHARPOS (*it))
5193 {
5194 set_iterator_to_next (it, 0);
5195 it->c = 0;
5196 return 1;
5197 }
5198
5199 /* Don't handle selective display in the following. It's (a)
5200 unnecessary because it's done by the caller, and (b) leads to an
5201 infinite recursion because next_element_from_ellipsis indirectly
5202 calls this function. */
5203 old_selective = it->selective;
5204 it->selective = 0;
5205
5206 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5207 from buffer text. */
5208 for (n = newline_found_p = 0;
5209 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5210 n += STRINGP (it->string) ? 0 : 1)
5211 {
5212 if (!get_next_display_element (it))
5213 return 0;
5214 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5215 set_iterator_to_next (it, 0);
5216 }
5217
5218 /* If we didn't find a newline near enough, see if we can use a
5219 short-cut. */
5220 if (!newline_found_p)
5221 {
5222 EMACS_INT start = IT_CHARPOS (*it);
5223 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5224 Lisp_Object pos;
5225
5226 xassert (!STRINGP (it->string));
5227
5228 /* If there isn't any `display' property in sight, and no
5229 overlays, we can just use the position of the newline in
5230 buffer text. */
5231 if (it->stop_charpos >= limit
5232 || ((pos = Fnext_single_property_change (make_number (start),
5233 Qdisplay,
5234 Qnil, make_number (limit)),
5235 NILP (pos))
5236 && next_overlay_change (start) == ZV))
5237 {
5238 IT_CHARPOS (*it) = limit;
5239 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5240 *skipped_p = newline_found_p = 1;
5241 }
5242 else
5243 {
5244 while (get_next_display_element (it)
5245 && !newline_found_p)
5246 {
5247 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5248 set_iterator_to_next (it, 0);
5249 }
5250 }
5251 }
5252
5253 it->selective = old_selective;
5254 return newline_found_p;
5255 }
5256
5257
5258 /* Set IT's current position to the previous visible line start. Skip
5259 invisible text that is so either due to text properties or due to
5260 selective display. Caution: this does not change IT->current_x and
5261 IT->hpos. */
5262
5263 static void
5264 back_to_previous_visible_line_start (struct it *it)
5265 {
5266 while (IT_CHARPOS (*it) > BEGV)
5267 {
5268 back_to_previous_line_start (it);
5269
5270 if (IT_CHARPOS (*it) <= BEGV)
5271 break;
5272
5273 /* If selective > 0, then lines indented more than its value are
5274 invisible. */
5275 if (it->selective > 0
5276 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5277 it->selective))
5278 continue;
5279
5280 /* Check the newline before point for invisibility. */
5281 {
5282 Lisp_Object prop;
5283 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5284 Qinvisible, it->window);
5285 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5286 continue;
5287 }
5288
5289 if (IT_CHARPOS (*it) <= BEGV)
5290 break;
5291
5292 {
5293 struct it it2;
5294 EMACS_INT pos;
5295 EMACS_INT beg, end;
5296 Lisp_Object val, overlay;
5297
5298 /* If newline is part of a composition, continue from start of composition */
5299 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5300 && beg < IT_CHARPOS (*it))
5301 goto replaced;
5302
5303 /* If newline is replaced by a display property, find start of overlay
5304 or interval and continue search from that point. */
5305 it2 = *it;
5306 pos = --IT_CHARPOS (it2);
5307 --IT_BYTEPOS (it2);
5308 it2.sp = 0;
5309 it2.string_from_display_prop_p = 0;
5310 if (handle_display_prop (&it2) == HANDLED_RETURN
5311 && !NILP (val = get_char_property_and_overlay
5312 (make_number (pos), Qdisplay, Qnil, &overlay))
5313 && (OVERLAYP (overlay)
5314 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5315 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5316 goto replaced;
5317
5318 /* Newline is not replaced by anything -- so we are done. */
5319 break;
5320
5321 replaced:
5322 if (beg < BEGV)
5323 beg = BEGV;
5324 IT_CHARPOS (*it) = beg;
5325 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5326 }
5327 }
5328
5329 it->continuation_lines_width = 0;
5330
5331 xassert (IT_CHARPOS (*it) >= BEGV);
5332 xassert (IT_CHARPOS (*it) == BEGV
5333 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5334 CHECK_IT (it);
5335 }
5336
5337
5338 /* Reseat iterator IT at the previous visible line start. Skip
5339 invisible text that is so either due to text properties or due to
5340 selective display. At the end, update IT's overlay information,
5341 face information etc. */
5342
5343 void
5344 reseat_at_previous_visible_line_start (struct it *it)
5345 {
5346 back_to_previous_visible_line_start (it);
5347 reseat (it, it->current.pos, 1);
5348 CHECK_IT (it);
5349 }
5350
5351
5352 /* Reseat iterator IT on the next visible line start in the current
5353 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5354 preceding the line start. Skip over invisible text that is so
5355 because of selective display. Compute faces, overlays etc at the
5356 new position. Note that this function does not skip over text that
5357 is invisible because of text properties. */
5358
5359 static void
5360 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5361 {
5362 int newline_found_p, skipped_p = 0;
5363
5364 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5365
5366 /* Skip over lines that are invisible because they are indented
5367 more than the value of IT->selective. */
5368 if (it->selective > 0)
5369 while (IT_CHARPOS (*it) < ZV
5370 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5371 it->selective))
5372 {
5373 xassert (IT_BYTEPOS (*it) == BEGV
5374 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5375 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5376 }
5377
5378 /* Position on the newline if that's what's requested. */
5379 if (on_newline_p && newline_found_p)
5380 {
5381 if (STRINGP (it->string))
5382 {
5383 if (IT_STRING_CHARPOS (*it) > 0)
5384 {
5385 --IT_STRING_CHARPOS (*it);
5386 --IT_STRING_BYTEPOS (*it);
5387 }
5388 }
5389 else if (IT_CHARPOS (*it) > BEGV)
5390 {
5391 --IT_CHARPOS (*it);
5392 --IT_BYTEPOS (*it);
5393 reseat (it, it->current.pos, 0);
5394 }
5395 }
5396 else if (skipped_p)
5397 reseat (it, it->current.pos, 0);
5398
5399 CHECK_IT (it);
5400 }
5401
5402
5403 \f
5404 /***********************************************************************
5405 Changing an iterator's position
5406 ***********************************************************************/
5407
5408 /* Change IT's current position to POS in current_buffer. If FORCE_P
5409 is non-zero, always check for text properties at the new position.
5410 Otherwise, text properties are only looked up if POS >=
5411 IT->check_charpos of a property. */
5412
5413 static void
5414 reseat (struct it *it, struct text_pos pos, int force_p)
5415 {
5416 EMACS_INT original_pos = IT_CHARPOS (*it);
5417
5418 reseat_1 (it, pos, 0);
5419
5420 /* Determine where to check text properties. Avoid doing it
5421 where possible because text property lookup is very expensive. */
5422 if (force_p
5423 || CHARPOS (pos) > it->stop_charpos
5424 || CHARPOS (pos) < original_pos)
5425 {
5426 if (it->bidi_p)
5427 {
5428 /* For bidi iteration, we need to prime prev_stop and
5429 base_level_stop with our best estimations. */
5430 if (CHARPOS (pos) < it->prev_stop)
5431 {
5432 handle_stop_backwards (it, BEGV);
5433 if (CHARPOS (pos) < it->base_level_stop)
5434 it->base_level_stop = 0;
5435 }
5436 else if (CHARPOS (pos) > it->stop_charpos
5437 && it->stop_charpos >= BEGV)
5438 handle_stop_backwards (it, it->stop_charpos);
5439 else /* force_p */
5440 handle_stop (it);
5441 }
5442 else
5443 {
5444 handle_stop (it);
5445 it->prev_stop = it->base_level_stop = 0;
5446 }
5447
5448 }
5449
5450 CHECK_IT (it);
5451 }
5452
5453
5454 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5455 IT->stop_pos to POS, also. */
5456
5457 static void
5458 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5459 {
5460 /* Don't call this function when scanning a C string. */
5461 xassert (it->s == NULL);
5462
5463 /* POS must be a reasonable value. */
5464 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5465
5466 it->current.pos = it->position = pos;
5467 it->end_charpos = ZV;
5468 it->dpvec = NULL;
5469 it->current.dpvec_index = -1;
5470 it->current.overlay_string_index = -1;
5471 IT_STRING_CHARPOS (*it) = -1;
5472 IT_STRING_BYTEPOS (*it) = -1;
5473 it->string = Qnil;
5474 it->string_from_display_prop_p = 0;
5475 it->method = GET_FROM_BUFFER;
5476 it->object = it->w->buffer;
5477 it->area = TEXT_AREA;
5478 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5479 it->sp = 0;
5480 it->string_from_display_prop_p = 0;
5481 it->face_before_selective_p = 0;
5482 if (it->bidi_p)
5483 {
5484 it->bidi_it.first_elt = 1;
5485 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5486 it->bidi_it.disp_pos = -1;
5487 }
5488
5489 if (set_stop_p)
5490 {
5491 it->stop_charpos = CHARPOS (pos);
5492 it->base_level_stop = CHARPOS (pos);
5493 }
5494 }
5495
5496
5497 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5498 If S is non-null, it is a C string to iterate over. Otherwise,
5499 STRING gives a Lisp string to iterate over.
5500
5501 If PRECISION > 0, don't return more then PRECISION number of
5502 characters from the string.
5503
5504 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5505 characters have been returned. FIELD_WIDTH < 0 means an infinite
5506 field width.
5507
5508 MULTIBYTE = 0 means disable processing of multibyte characters,
5509 MULTIBYTE > 0 means enable it,
5510 MULTIBYTE < 0 means use IT->multibyte_p.
5511
5512 IT must be initialized via a prior call to init_iterator before
5513 calling this function. */
5514
5515 static void
5516 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5517 EMACS_INT charpos, EMACS_INT precision, int field_width,
5518 int multibyte)
5519 {
5520 /* No region in strings. */
5521 it->region_beg_charpos = it->region_end_charpos = -1;
5522
5523 /* No text property checks performed by default, but see below. */
5524 it->stop_charpos = -1;
5525
5526 /* Set iterator position and end position. */
5527 memset (&it->current, 0, sizeof it->current);
5528 it->current.overlay_string_index = -1;
5529 it->current.dpvec_index = -1;
5530 xassert (charpos >= 0);
5531
5532 /* If STRING is specified, use its multibyteness, otherwise use the
5533 setting of MULTIBYTE, if specified. */
5534 if (multibyte >= 0)
5535 it->multibyte_p = multibyte > 0;
5536
5537 if (s == NULL)
5538 {
5539 xassert (STRINGP (string));
5540 it->string = string;
5541 it->s = NULL;
5542 it->end_charpos = it->string_nchars = SCHARS (string);
5543 it->method = GET_FROM_STRING;
5544 it->current.string_pos = string_pos (charpos, string);
5545 }
5546 else
5547 {
5548 it->s = (const unsigned char *) s;
5549 it->string = Qnil;
5550
5551 /* Note that we use IT->current.pos, not it->current.string_pos,
5552 for displaying C strings. */
5553 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5554 if (it->multibyte_p)
5555 {
5556 it->current.pos = c_string_pos (charpos, s, 1);
5557 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5558 }
5559 else
5560 {
5561 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5562 it->end_charpos = it->string_nchars = strlen (s);
5563 }
5564
5565 it->method = GET_FROM_C_STRING;
5566 }
5567
5568 /* PRECISION > 0 means don't return more than PRECISION characters
5569 from the string. */
5570 if (precision > 0 && it->end_charpos - charpos > precision)
5571 it->end_charpos = it->string_nchars = charpos + precision;
5572
5573 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5574 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5575 FIELD_WIDTH < 0 means infinite field width. This is useful for
5576 padding with `-' at the end of a mode line. */
5577 if (field_width < 0)
5578 field_width = INFINITY;
5579 if (field_width > it->end_charpos - charpos)
5580 it->end_charpos = charpos + field_width;
5581
5582 /* Use the standard display table for displaying strings. */
5583 if (DISP_TABLE_P (Vstandard_display_table))
5584 it->dp = XCHAR_TABLE (Vstandard_display_table);
5585
5586 it->stop_charpos = charpos;
5587 if (s == NULL && it->multibyte_p)
5588 {
5589 EMACS_INT endpos = SCHARS (it->string);
5590 if (endpos > it->end_charpos)
5591 endpos = it->end_charpos;
5592 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5593 it->string);
5594 }
5595 CHECK_IT (it);
5596 }
5597
5598
5599 \f
5600 /***********************************************************************
5601 Iteration
5602 ***********************************************************************/
5603
5604 /* Map enum it_method value to corresponding next_element_from_* function. */
5605
5606 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5607 {
5608 next_element_from_buffer,
5609 next_element_from_display_vector,
5610 next_element_from_string,
5611 next_element_from_c_string,
5612 next_element_from_image,
5613 next_element_from_stretch
5614 };
5615
5616 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5617
5618
5619 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5620 (possibly with the following characters). */
5621
5622 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5623 ((IT)->cmp_it.id >= 0 \
5624 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5625 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5626 END_CHARPOS, (IT)->w, \
5627 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5628 (IT)->string)))
5629
5630
5631 /* Lookup the char-table Vglyphless_char_display for character C (-1
5632 if we want information for no-font case), and return the display
5633 method symbol. By side-effect, update it->what and
5634 it->glyphless_method. This function is called from
5635 get_next_display_element for each character element, and from
5636 x_produce_glyphs when no suitable font was found. */
5637
5638 Lisp_Object
5639 lookup_glyphless_char_display (int c, struct it *it)
5640 {
5641 Lisp_Object glyphless_method = Qnil;
5642
5643 if (CHAR_TABLE_P (Vglyphless_char_display)
5644 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5645 {
5646 if (c >= 0)
5647 {
5648 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
5649 if (CONSP (glyphless_method))
5650 glyphless_method = FRAME_WINDOW_P (it->f)
5651 ? XCAR (glyphless_method)
5652 : XCDR (glyphless_method);
5653 }
5654 else
5655 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
5656 }
5657
5658 retry:
5659 if (NILP (glyphless_method))
5660 {
5661 if (c >= 0)
5662 /* The default is to display the character by a proper font. */
5663 return Qnil;
5664 /* The default for the no-font case is to display an empty box. */
5665 glyphless_method = Qempty_box;
5666 }
5667 if (EQ (glyphless_method, Qzero_width))
5668 {
5669 if (c >= 0)
5670 return glyphless_method;
5671 /* This method can't be used for the no-font case. */
5672 glyphless_method = Qempty_box;
5673 }
5674 if (EQ (glyphless_method, Qthin_space))
5675 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5676 else if (EQ (glyphless_method, Qempty_box))
5677 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5678 else if (EQ (glyphless_method, Qhex_code))
5679 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5680 else if (STRINGP (glyphless_method))
5681 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5682 else
5683 {
5684 /* Invalid value. We use the default method. */
5685 glyphless_method = Qnil;
5686 goto retry;
5687 }
5688 it->what = IT_GLYPHLESS;
5689 return glyphless_method;
5690 }
5691
5692 /* Load IT's display element fields with information about the next
5693 display element from the current position of IT. Value is zero if
5694 end of buffer (or C string) is reached. */
5695
5696 static struct frame *last_escape_glyph_frame = NULL;
5697 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5698 static int last_escape_glyph_merged_face_id = 0;
5699
5700 struct frame *last_glyphless_glyph_frame = NULL;
5701 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5702 int last_glyphless_glyph_merged_face_id = 0;
5703
5704 static int
5705 get_next_display_element (struct it *it)
5706 {
5707 /* Non-zero means that we found a display element. Zero means that
5708 we hit the end of what we iterate over. Performance note: the
5709 function pointer `method' used here turns out to be faster than
5710 using a sequence of if-statements. */
5711 int success_p;
5712
5713 get_next:
5714 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5715
5716 if (it->what == IT_CHARACTER)
5717 {
5718 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5719 and only if (a) the resolved directionality of that character
5720 is R..." */
5721 /* FIXME: Do we need an exception for characters from display
5722 tables? */
5723 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5724 it->c = bidi_mirror_char (it->c);
5725 /* Map via display table or translate control characters.
5726 IT->c, IT->len etc. have been set to the next character by
5727 the function call above. If we have a display table, and it
5728 contains an entry for IT->c, translate it. Don't do this if
5729 IT->c itself comes from a display table, otherwise we could
5730 end up in an infinite recursion. (An alternative could be to
5731 count the recursion depth of this function and signal an
5732 error when a certain maximum depth is reached.) Is it worth
5733 it? */
5734 if (success_p && it->dpvec == NULL)
5735 {
5736 Lisp_Object dv;
5737 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5738 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5739 nbsp_or_shy = char_is_other;
5740 int c = it->c; /* This is the character to display. */
5741
5742 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5743 {
5744 xassert (SINGLE_BYTE_CHAR_P (c));
5745 if (unibyte_display_via_language_environment)
5746 {
5747 c = DECODE_CHAR (unibyte, c);
5748 if (c < 0)
5749 c = BYTE8_TO_CHAR (it->c);
5750 }
5751 else
5752 c = BYTE8_TO_CHAR (it->c);
5753 }
5754
5755 if (it->dp
5756 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5757 VECTORP (dv)))
5758 {
5759 struct Lisp_Vector *v = XVECTOR (dv);
5760
5761 /* Return the first character from the display table
5762 entry, if not empty. If empty, don't display the
5763 current character. */
5764 if (v->header.size)
5765 {
5766 it->dpvec_char_len = it->len;
5767 it->dpvec = v->contents;
5768 it->dpend = v->contents + v->header.size;
5769 it->current.dpvec_index = 0;
5770 it->dpvec_face_id = -1;
5771 it->saved_face_id = it->face_id;
5772 it->method = GET_FROM_DISPLAY_VECTOR;
5773 it->ellipsis_p = 0;
5774 }
5775 else
5776 {
5777 set_iterator_to_next (it, 0);
5778 }
5779 goto get_next;
5780 }
5781
5782 if (! NILP (lookup_glyphless_char_display (c, it)))
5783 {
5784 if (it->what == IT_GLYPHLESS)
5785 goto done;
5786 /* Don't display this character. */
5787 set_iterator_to_next (it, 0);
5788 goto get_next;
5789 }
5790
5791 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5792 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5793 : c == 0xAD ? char_is_soft_hyphen
5794 : char_is_other);
5795
5796 /* Translate control characters into `\003' or `^C' form.
5797 Control characters coming from a display table entry are
5798 currently not translated because we use IT->dpvec to hold
5799 the translation. This could easily be changed but I
5800 don't believe that it is worth doing.
5801
5802 NBSP and SOFT-HYPEN are property translated too.
5803
5804 Non-printable characters and raw-byte characters are also
5805 translated to octal form. */
5806 if (((c < ' ' || c == 127) /* ASCII control chars */
5807 ? (it->area != TEXT_AREA
5808 /* In mode line, treat \n, \t like other crl chars. */
5809 || (c != '\t'
5810 && it->glyph_row
5811 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5812 || (c != '\n' && c != '\t'))
5813 : (nbsp_or_shy
5814 || CHAR_BYTE8_P (c)
5815 || ! CHAR_PRINTABLE_P (c))))
5816 {
5817 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5818 or a non-printable character which must be displayed
5819 either as '\003' or as `^C' where the '\\' and '^'
5820 can be defined in the display table. Fill
5821 IT->ctl_chars with glyphs for what we have to
5822 display. Then, set IT->dpvec to these glyphs. */
5823 Lisp_Object gc;
5824 int ctl_len;
5825 int face_id;
5826 EMACS_INT lface_id = 0;
5827 int escape_glyph;
5828
5829 /* Handle control characters with ^. */
5830
5831 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5832 {
5833 int g;
5834
5835 g = '^'; /* default glyph for Control */
5836 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5837 if (it->dp
5838 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5839 && GLYPH_CODE_CHAR_VALID_P (gc))
5840 {
5841 g = GLYPH_CODE_CHAR (gc);
5842 lface_id = GLYPH_CODE_FACE (gc);
5843 }
5844 if (lface_id)
5845 {
5846 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5847 }
5848 else if (it->f == last_escape_glyph_frame
5849 && it->face_id == last_escape_glyph_face_id)
5850 {
5851 face_id = last_escape_glyph_merged_face_id;
5852 }
5853 else
5854 {
5855 /* Merge the escape-glyph face into the current face. */
5856 face_id = merge_faces (it->f, Qescape_glyph, 0,
5857 it->face_id);
5858 last_escape_glyph_frame = it->f;
5859 last_escape_glyph_face_id = it->face_id;
5860 last_escape_glyph_merged_face_id = face_id;
5861 }
5862
5863 XSETINT (it->ctl_chars[0], g);
5864 XSETINT (it->ctl_chars[1], c ^ 0100);
5865 ctl_len = 2;
5866 goto display_control;
5867 }
5868
5869 /* Handle non-break space in the mode where it only gets
5870 highlighting. */
5871
5872 if (EQ (Vnobreak_char_display, Qt)
5873 && nbsp_or_shy == char_is_nbsp)
5874 {
5875 /* Merge the no-break-space face into the current face. */
5876 face_id = merge_faces (it->f, Qnobreak_space, 0,
5877 it->face_id);
5878
5879 c = ' ';
5880 XSETINT (it->ctl_chars[0], ' ');
5881 ctl_len = 1;
5882 goto display_control;
5883 }
5884
5885 /* Handle sequences that start with the "escape glyph". */
5886
5887 /* the default escape glyph is \. */
5888 escape_glyph = '\\';
5889
5890 if (it->dp
5891 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5892 && GLYPH_CODE_CHAR_VALID_P (gc))
5893 {
5894 escape_glyph = GLYPH_CODE_CHAR (gc);
5895 lface_id = GLYPH_CODE_FACE (gc);
5896 }
5897 if (lface_id)
5898 {
5899 /* The display table specified a face.
5900 Merge it into face_id and also into escape_glyph. */
5901 face_id = merge_faces (it->f, Qt, lface_id,
5902 it->face_id);
5903 }
5904 else if (it->f == last_escape_glyph_frame
5905 && it->face_id == last_escape_glyph_face_id)
5906 {
5907 face_id = last_escape_glyph_merged_face_id;
5908 }
5909 else
5910 {
5911 /* Merge the escape-glyph face into the current face. */
5912 face_id = merge_faces (it->f, Qescape_glyph, 0,
5913 it->face_id);
5914 last_escape_glyph_frame = it->f;
5915 last_escape_glyph_face_id = it->face_id;
5916 last_escape_glyph_merged_face_id = face_id;
5917 }
5918
5919 /* Handle soft hyphens in the mode where they only get
5920 highlighting. */
5921
5922 if (EQ (Vnobreak_char_display, Qt)
5923 && nbsp_or_shy == char_is_soft_hyphen)
5924 {
5925 XSETINT (it->ctl_chars[0], '-');
5926 ctl_len = 1;
5927 goto display_control;
5928 }
5929
5930 /* Handle non-break space and soft hyphen
5931 with the escape glyph. */
5932
5933 if (nbsp_or_shy)
5934 {
5935 XSETINT (it->ctl_chars[0], escape_glyph);
5936 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5937 XSETINT (it->ctl_chars[1], c);
5938 ctl_len = 2;
5939 goto display_control;
5940 }
5941
5942 {
5943 char str[10];
5944 int len, i;
5945
5946 if (CHAR_BYTE8_P (c))
5947 /* Display \200 instead of \17777600. */
5948 c = CHAR_TO_BYTE8 (c);
5949 len = sprintf (str, "%03o", c);
5950
5951 XSETINT (it->ctl_chars[0], escape_glyph);
5952 for (i = 0; i < len; i++)
5953 XSETINT (it->ctl_chars[i + 1], str[i]);
5954 ctl_len = len + 1;
5955 }
5956
5957 display_control:
5958 /* Set up IT->dpvec and return first character from it. */
5959 it->dpvec_char_len = it->len;
5960 it->dpvec = it->ctl_chars;
5961 it->dpend = it->dpvec + ctl_len;
5962 it->current.dpvec_index = 0;
5963 it->dpvec_face_id = face_id;
5964 it->saved_face_id = it->face_id;
5965 it->method = GET_FROM_DISPLAY_VECTOR;
5966 it->ellipsis_p = 0;
5967 goto get_next;
5968 }
5969 it->char_to_display = c;
5970 }
5971 else if (success_p)
5972 {
5973 it->char_to_display = it->c;
5974 }
5975 }
5976
5977 /* Adjust face id for a multibyte character. There are no multibyte
5978 character in unibyte text. */
5979 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5980 && it->multibyte_p
5981 && success_p
5982 && FRAME_WINDOW_P (it->f))
5983 {
5984 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5985
5986 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5987 {
5988 /* Automatic composition with glyph-string. */
5989 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5990
5991 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5992 }
5993 else
5994 {
5995 EMACS_INT pos = (it->s ? -1
5996 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5997 : IT_CHARPOS (*it));
5998 int c;
5999
6000 if (it->what == IT_CHARACTER)
6001 c = it->char_to_display;
6002 else
6003 {
6004 struct composition *cmp = composition_table[it->cmp_it.id];
6005 int i;
6006
6007 c = ' ';
6008 for (i = 0; i < cmp->glyph_len; i++)
6009 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6010 break;
6011 }
6012 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6013 }
6014 }
6015
6016 done:
6017 /* Is this character the last one of a run of characters with
6018 box? If yes, set IT->end_of_box_run_p to 1. */
6019 if (it->face_box_p
6020 && it->s == NULL)
6021 {
6022 if (it->method == GET_FROM_STRING && it->sp)
6023 {
6024 int face_id = underlying_face_id (it);
6025 struct face *face = FACE_FROM_ID (it->f, face_id);
6026
6027 if (face)
6028 {
6029 if (face->box == FACE_NO_BOX)
6030 {
6031 /* If the box comes from face properties in a
6032 display string, check faces in that string. */
6033 int string_face_id = face_after_it_pos (it);
6034 it->end_of_box_run_p
6035 = (FACE_FROM_ID (it->f, string_face_id)->box
6036 == FACE_NO_BOX);
6037 }
6038 /* Otherwise, the box comes from the underlying face.
6039 If this is the last string character displayed, check
6040 the next buffer location. */
6041 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6042 && (it->current.overlay_string_index
6043 == it->n_overlay_strings - 1))
6044 {
6045 EMACS_INT ignore;
6046 int next_face_id;
6047 struct text_pos pos = it->current.pos;
6048 INC_TEXT_POS (pos, it->multibyte_p);
6049
6050 next_face_id = face_at_buffer_position
6051 (it->w, CHARPOS (pos), it->region_beg_charpos,
6052 it->region_end_charpos, &ignore,
6053 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6054 -1);
6055 it->end_of_box_run_p
6056 = (FACE_FROM_ID (it->f, next_face_id)->box
6057 == FACE_NO_BOX);
6058 }
6059 }
6060 }
6061 else
6062 {
6063 int face_id = face_after_it_pos (it);
6064 it->end_of_box_run_p
6065 = (face_id != it->face_id
6066 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6067 }
6068 }
6069
6070 /* Value is 0 if end of buffer or string reached. */
6071 return success_p;
6072 }
6073
6074
6075 /* Move IT to the next display element.
6076
6077 RESEAT_P non-zero means if called on a newline in buffer text,
6078 skip to the next visible line start.
6079
6080 Functions get_next_display_element and set_iterator_to_next are
6081 separate because I find this arrangement easier to handle than a
6082 get_next_display_element function that also increments IT's
6083 position. The way it is we can first look at an iterator's current
6084 display element, decide whether it fits on a line, and if it does,
6085 increment the iterator position. The other way around we probably
6086 would either need a flag indicating whether the iterator has to be
6087 incremented the next time, or we would have to implement a
6088 decrement position function which would not be easy to write. */
6089
6090 void
6091 set_iterator_to_next (struct it *it, int reseat_p)
6092 {
6093 /* Reset flags indicating start and end of a sequence of characters
6094 with box. Reset them at the start of this function because
6095 moving the iterator to a new position might set them. */
6096 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6097
6098 switch (it->method)
6099 {
6100 case GET_FROM_BUFFER:
6101 /* The current display element of IT is a character from
6102 current_buffer. Advance in the buffer, and maybe skip over
6103 invisible lines that are so because of selective display. */
6104 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6105 reseat_at_next_visible_line_start (it, 0);
6106 else if (it->cmp_it.id >= 0)
6107 {
6108 /* We are currently getting glyphs from a composition. */
6109 int i;
6110
6111 if (! it->bidi_p)
6112 {
6113 IT_CHARPOS (*it) += it->cmp_it.nchars;
6114 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6115 if (it->cmp_it.to < it->cmp_it.nglyphs)
6116 {
6117 it->cmp_it.from = it->cmp_it.to;
6118 }
6119 else
6120 {
6121 it->cmp_it.id = -1;
6122 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6123 IT_BYTEPOS (*it),
6124 it->end_charpos, Qnil);
6125 }
6126 }
6127 else if (! it->cmp_it.reversed_p)
6128 {
6129 /* Composition created while scanning forward. */
6130 /* Update IT's char/byte positions to point to the first
6131 character of the next grapheme cluster, or to the
6132 character visually after the current composition. */
6133 for (i = 0; i < it->cmp_it.nchars; i++)
6134 bidi_move_to_visually_next (&it->bidi_it);
6135 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6136 IT_CHARPOS (*it) = it->bidi_it.charpos;
6137
6138 if (it->cmp_it.to < it->cmp_it.nglyphs)
6139 {
6140 /* Proceed to the next grapheme cluster. */
6141 it->cmp_it.from = it->cmp_it.to;
6142 }
6143 else
6144 {
6145 /* No more grapheme clusters in this composition.
6146 Find the next stop position. */
6147 EMACS_INT stop = it->end_charpos;
6148 if (it->bidi_it.scan_dir < 0)
6149 /* Now we are scanning backward and don't know
6150 where to stop. */
6151 stop = -1;
6152 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6153 IT_BYTEPOS (*it), stop, Qnil);
6154 }
6155 }
6156 else
6157 {
6158 /* Composition created while scanning backward. */
6159 /* Update IT's char/byte positions to point to the last
6160 character of the previous grapheme cluster, or the
6161 character visually after the current composition. */
6162 for (i = 0; i < it->cmp_it.nchars; i++)
6163 bidi_move_to_visually_next (&it->bidi_it);
6164 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6165 IT_CHARPOS (*it) = it->bidi_it.charpos;
6166 if (it->cmp_it.from > 0)
6167 {
6168 /* Proceed to the previous grapheme cluster. */
6169 it->cmp_it.to = it->cmp_it.from;
6170 }
6171 else
6172 {
6173 /* No more grapheme clusters in this composition.
6174 Find the next stop position. */
6175 EMACS_INT stop = it->end_charpos;
6176 if (it->bidi_it.scan_dir < 0)
6177 /* Now we are scanning backward and don't know
6178 where to stop. */
6179 stop = -1;
6180 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6181 IT_BYTEPOS (*it), stop, Qnil);
6182 }
6183 }
6184 }
6185 else
6186 {
6187 xassert (it->len != 0);
6188
6189 if (!it->bidi_p)
6190 {
6191 IT_BYTEPOS (*it) += it->len;
6192 IT_CHARPOS (*it) += 1;
6193 }
6194 else
6195 {
6196 int prev_scan_dir = it->bidi_it.scan_dir;
6197 /* If this is a new paragraph, determine its base
6198 direction (a.k.a. its base embedding level). */
6199 if (it->bidi_it.new_paragraph)
6200 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6201 bidi_move_to_visually_next (&it->bidi_it);
6202 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6203 IT_CHARPOS (*it) = it->bidi_it.charpos;
6204 if (prev_scan_dir != it->bidi_it.scan_dir)
6205 {
6206 /* As the scan direction was changed, we must
6207 re-compute the stop position for composition. */
6208 EMACS_INT stop = it->end_charpos;
6209 if (it->bidi_it.scan_dir < 0)
6210 stop = -1;
6211 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6212 IT_BYTEPOS (*it), stop, Qnil);
6213 }
6214 }
6215 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6216 }
6217 break;
6218
6219 case GET_FROM_C_STRING:
6220 /* Current display element of IT is from a C string. */
6221 IT_BYTEPOS (*it) += it->len;
6222 IT_CHARPOS (*it) += 1;
6223 break;
6224
6225 case GET_FROM_DISPLAY_VECTOR:
6226 /* Current display element of IT is from a display table entry.
6227 Advance in the display table definition. Reset it to null if
6228 end reached, and continue with characters from buffers/
6229 strings. */
6230 ++it->current.dpvec_index;
6231
6232 /* Restore face of the iterator to what they were before the
6233 display vector entry (these entries may contain faces). */
6234 it->face_id = it->saved_face_id;
6235
6236 if (it->dpvec + it->current.dpvec_index == it->dpend)
6237 {
6238 int recheck_faces = it->ellipsis_p;
6239
6240 if (it->s)
6241 it->method = GET_FROM_C_STRING;
6242 else if (STRINGP (it->string))
6243 it->method = GET_FROM_STRING;
6244 else
6245 {
6246 it->method = GET_FROM_BUFFER;
6247 it->object = it->w->buffer;
6248 }
6249
6250 it->dpvec = NULL;
6251 it->current.dpvec_index = -1;
6252
6253 /* Skip over characters which were displayed via IT->dpvec. */
6254 if (it->dpvec_char_len < 0)
6255 reseat_at_next_visible_line_start (it, 1);
6256 else if (it->dpvec_char_len > 0)
6257 {
6258 if (it->method == GET_FROM_STRING
6259 && it->n_overlay_strings > 0)
6260 it->ignore_overlay_strings_at_pos_p = 1;
6261 it->len = it->dpvec_char_len;
6262 set_iterator_to_next (it, reseat_p);
6263 }
6264
6265 /* Maybe recheck faces after display vector */
6266 if (recheck_faces)
6267 it->stop_charpos = IT_CHARPOS (*it);
6268 }
6269 break;
6270
6271 case GET_FROM_STRING:
6272 /* Current display element is a character from a Lisp string. */
6273 xassert (it->s == NULL && STRINGP (it->string));
6274 if (it->cmp_it.id >= 0)
6275 {
6276 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6277 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6278 if (it->cmp_it.to < it->cmp_it.nglyphs)
6279 it->cmp_it.from = it->cmp_it.to;
6280 else
6281 {
6282 it->cmp_it.id = -1;
6283 composition_compute_stop_pos (&it->cmp_it,
6284 IT_STRING_CHARPOS (*it),
6285 IT_STRING_BYTEPOS (*it),
6286 it->end_charpos, it->string);
6287 }
6288 }
6289 else
6290 {
6291 IT_STRING_BYTEPOS (*it) += it->len;
6292 IT_STRING_CHARPOS (*it) += 1;
6293 }
6294
6295 consider_string_end:
6296
6297 if (it->current.overlay_string_index >= 0)
6298 {
6299 /* IT->string is an overlay string. Advance to the
6300 next, if there is one. */
6301 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6302 {
6303 it->ellipsis_p = 0;
6304 next_overlay_string (it);
6305 if (it->ellipsis_p)
6306 setup_for_ellipsis (it, 0);
6307 }
6308 }
6309 else
6310 {
6311 /* IT->string is not an overlay string. If we reached
6312 its end, and there is something on IT->stack, proceed
6313 with what is on the stack. This can be either another
6314 string, this time an overlay string, or a buffer. */
6315 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6316 && it->sp > 0)
6317 {
6318 pop_it (it);
6319 if (it->method == GET_FROM_STRING)
6320 goto consider_string_end;
6321 }
6322 }
6323 break;
6324
6325 case GET_FROM_IMAGE:
6326 case GET_FROM_STRETCH:
6327 /* The position etc with which we have to proceed are on
6328 the stack. The position may be at the end of a string,
6329 if the `display' property takes up the whole string. */
6330 xassert (it->sp > 0);
6331 pop_it (it);
6332 if (it->method == GET_FROM_STRING)
6333 goto consider_string_end;
6334 break;
6335
6336 default:
6337 /* There are no other methods defined, so this should be a bug. */
6338 abort ();
6339 }
6340
6341 xassert (it->method != GET_FROM_STRING
6342 || (STRINGP (it->string)
6343 && IT_STRING_CHARPOS (*it) >= 0));
6344 }
6345
6346 /* Load IT's display element fields with information about the next
6347 display element which comes from a display table entry or from the
6348 result of translating a control character to one of the forms `^C'
6349 or `\003'.
6350
6351 IT->dpvec holds the glyphs to return as characters.
6352 IT->saved_face_id holds the face id before the display vector--it
6353 is restored into IT->face_id in set_iterator_to_next. */
6354
6355 static int
6356 next_element_from_display_vector (struct it *it)
6357 {
6358 Lisp_Object gc;
6359
6360 /* Precondition. */
6361 xassert (it->dpvec && it->current.dpvec_index >= 0);
6362
6363 it->face_id = it->saved_face_id;
6364
6365 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6366 That seemed totally bogus - so I changed it... */
6367 gc = it->dpvec[it->current.dpvec_index];
6368
6369 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6370 {
6371 it->c = GLYPH_CODE_CHAR (gc);
6372 it->len = CHAR_BYTES (it->c);
6373
6374 /* The entry may contain a face id to use. Such a face id is
6375 the id of a Lisp face, not a realized face. A face id of
6376 zero means no face is specified. */
6377 if (it->dpvec_face_id >= 0)
6378 it->face_id = it->dpvec_face_id;
6379 else
6380 {
6381 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
6382 if (lface_id > 0)
6383 it->face_id = merge_faces (it->f, Qt, lface_id,
6384 it->saved_face_id);
6385 }
6386 }
6387 else
6388 /* Display table entry is invalid. Return a space. */
6389 it->c = ' ', it->len = 1;
6390
6391 /* Don't change position and object of the iterator here. They are
6392 still the values of the character that had this display table
6393 entry or was translated, and that's what we want. */
6394 it->what = IT_CHARACTER;
6395 return 1;
6396 }
6397
6398
6399 /* Load IT with the next display element from Lisp string IT->string.
6400 IT->current.string_pos is the current position within the string.
6401 If IT->current.overlay_string_index >= 0, the Lisp string is an
6402 overlay string. */
6403
6404 static int
6405 next_element_from_string (struct it *it)
6406 {
6407 struct text_pos position;
6408
6409 xassert (STRINGP (it->string));
6410 xassert (IT_STRING_CHARPOS (*it) >= 0);
6411 position = it->current.string_pos;
6412
6413 /* Time to check for invisible text? */
6414 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6415 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6416 {
6417 handle_stop (it);
6418
6419 /* Since a handler may have changed IT->method, we must
6420 recurse here. */
6421 return GET_NEXT_DISPLAY_ELEMENT (it);
6422 }
6423
6424 if (it->current.overlay_string_index >= 0)
6425 {
6426 /* Get the next character from an overlay string. In overlay
6427 strings, There is no field width or padding with spaces to
6428 do. */
6429 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6430 {
6431 it->what = IT_EOB;
6432 return 0;
6433 }
6434 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6435 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6436 && next_element_from_composition (it))
6437 {
6438 return 1;
6439 }
6440 else if (STRING_MULTIBYTE (it->string))
6441 {
6442 const unsigned char *s = (SDATA (it->string)
6443 + IT_STRING_BYTEPOS (*it));
6444 it->c = string_char_and_length (s, &it->len);
6445 }
6446 else
6447 {
6448 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6449 it->len = 1;
6450 }
6451 }
6452 else
6453 {
6454 /* Get the next character from a Lisp string that is not an
6455 overlay string. Such strings come from the mode line, for
6456 example. We may have to pad with spaces, or truncate the
6457 string. See also next_element_from_c_string. */
6458 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6459 {
6460 it->what = IT_EOB;
6461 return 0;
6462 }
6463 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6464 {
6465 /* Pad with spaces. */
6466 it->c = ' ', it->len = 1;
6467 CHARPOS (position) = BYTEPOS (position) = -1;
6468 }
6469 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6470 IT_STRING_BYTEPOS (*it), it->string_nchars)
6471 && next_element_from_composition (it))
6472 {
6473 return 1;
6474 }
6475 else if (STRING_MULTIBYTE (it->string))
6476 {
6477 const unsigned char *s = (SDATA (it->string)
6478 + IT_STRING_BYTEPOS (*it));
6479 it->c = string_char_and_length (s, &it->len);
6480 }
6481 else
6482 {
6483 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6484 it->len = 1;
6485 }
6486 }
6487
6488 /* Record what we have and where it came from. */
6489 it->what = IT_CHARACTER;
6490 it->object = it->string;
6491 it->position = position;
6492 return 1;
6493 }
6494
6495
6496 /* Load IT with next display element from C string IT->s.
6497 IT->string_nchars is the maximum number of characters to return
6498 from the string. IT->end_charpos may be greater than
6499 IT->string_nchars when this function is called, in which case we
6500 may have to return padding spaces. Value is zero if end of string
6501 reached, including padding spaces. */
6502
6503 static int
6504 next_element_from_c_string (struct it *it)
6505 {
6506 int success_p = 1;
6507
6508 xassert (it->s);
6509 it->what = IT_CHARACTER;
6510 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6511 it->object = Qnil;
6512
6513 /* IT's position can be greater IT->string_nchars in case a field
6514 width or precision has been specified when the iterator was
6515 initialized. */
6516 if (IT_CHARPOS (*it) >= it->end_charpos)
6517 {
6518 /* End of the game. */
6519 it->what = IT_EOB;
6520 success_p = 0;
6521 }
6522 else if (IT_CHARPOS (*it) >= it->string_nchars)
6523 {
6524 /* Pad with spaces. */
6525 it->c = ' ', it->len = 1;
6526 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6527 }
6528 else if (it->multibyte_p)
6529 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6530 else
6531 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6532
6533 return success_p;
6534 }
6535
6536
6537 /* Set up IT to return characters from an ellipsis, if appropriate.
6538 The definition of the ellipsis glyphs may come from a display table
6539 entry. This function fills IT with the first glyph from the
6540 ellipsis if an ellipsis is to be displayed. */
6541
6542 static int
6543 next_element_from_ellipsis (struct it *it)
6544 {
6545 if (it->selective_display_ellipsis_p)
6546 setup_for_ellipsis (it, it->len);
6547 else
6548 {
6549 /* The face at the current position may be different from the
6550 face we find after the invisible text. Remember what it
6551 was in IT->saved_face_id, and signal that it's there by
6552 setting face_before_selective_p. */
6553 it->saved_face_id = it->face_id;
6554 it->method = GET_FROM_BUFFER;
6555 it->object = it->w->buffer;
6556 reseat_at_next_visible_line_start (it, 1);
6557 it->face_before_selective_p = 1;
6558 }
6559
6560 return GET_NEXT_DISPLAY_ELEMENT (it);
6561 }
6562
6563
6564 /* Deliver an image display element. The iterator IT is already
6565 filled with image information (done in handle_display_prop). Value
6566 is always 1. */
6567
6568
6569 static int
6570 next_element_from_image (struct it *it)
6571 {
6572 it->what = IT_IMAGE;
6573 it->ignore_overlay_strings_at_pos_p = 0;
6574 return 1;
6575 }
6576
6577
6578 /* Fill iterator IT with next display element from a stretch glyph
6579 property. IT->object is the value of the text property. Value is
6580 always 1. */
6581
6582 static int
6583 next_element_from_stretch (struct it *it)
6584 {
6585 it->what = IT_STRETCH;
6586 return 1;
6587 }
6588
6589 /* Scan forward from CHARPOS in the current buffer, until we find a
6590 stop position > current IT's position. Then handle the stop
6591 position before that. This is called when we bump into a stop
6592 position while reordering bidirectional text. CHARPOS should be
6593 the last previously processed stop_pos (or BEGV, if none were
6594 processed yet) whose position is less that IT's current
6595 position. */
6596
6597 static void
6598 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6599 {
6600 EMACS_INT where_we_are = IT_CHARPOS (*it);
6601 struct display_pos save_current = it->current;
6602 struct text_pos save_position = it->position;
6603 struct text_pos pos1;
6604 EMACS_INT next_stop;
6605
6606 /* Scan in strict logical order. */
6607 it->bidi_p = 0;
6608 do
6609 {
6610 it->prev_stop = charpos;
6611 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6612 reseat_1 (it, pos1, 0);
6613 compute_stop_pos (it);
6614 /* We must advance forward, right? */
6615 if (it->stop_charpos <= it->prev_stop)
6616 abort ();
6617 charpos = it->stop_charpos;
6618 }
6619 while (charpos <= where_we_are);
6620
6621 next_stop = it->stop_charpos;
6622 it->stop_charpos = it->prev_stop;
6623 it->bidi_p = 1;
6624 it->current = save_current;
6625 it->position = save_position;
6626 handle_stop (it);
6627 it->stop_charpos = next_stop;
6628 }
6629
6630 /* Load IT with the next display element from current_buffer. Value
6631 is zero if end of buffer reached. IT->stop_charpos is the next
6632 position at which to stop and check for text properties or buffer
6633 end. */
6634
6635 static int
6636 next_element_from_buffer (struct it *it)
6637 {
6638 int success_p = 1;
6639
6640 xassert (IT_CHARPOS (*it) >= BEGV);
6641
6642 /* With bidi reordering, the character to display might not be the
6643 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6644 we were reseat()ed to a new buffer position, which is potentially
6645 a different paragraph. */
6646 if (it->bidi_p && it->bidi_it.first_elt)
6647 {
6648 it->bidi_it.charpos = IT_CHARPOS (*it);
6649 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6650 if (it->bidi_it.bytepos == ZV_BYTE)
6651 {
6652 /* Nothing to do, but reset the FIRST_ELT flag, like
6653 bidi_paragraph_init does, because we are not going to
6654 call it. */
6655 it->bidi_it.first_elt = 0;
6656 }
6657 else if (it->bidi_it.bytepos == BEGV_BYTE
6658 /* FIXME: Should support all Unicode line separators. */
6659 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6660 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6661 {
6662 /* If we are at the beginning of a line, we can produce the
6663 next element right away. */
6664 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6665 bidi_move_to_visually_next (&it->bidi_it);
6666 }
6667 else
6668 {
6669 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6670
6671 /* We need to prime the bidi iterator starting at the line's
6672 beginning, before we will be able to produce the next
6673 element. */
6674 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6675 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6676 it->bidi_it.charpos = IT_CHARPOS (*it);
6677 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6678 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6679 do
6680 {
6681 /* Now return to buffer position where we were asked to
6682 get the next display element, and produce that. */
6683 bidi_move_to_visually_next (&it->bidi_it);
6684 }
6685 while (it->bidi_it.bytepos != orig_bytepos
6686 && it->bidi_it.bytepos < ZV_BYTE);
6687 }
6688
6689 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6690 /* Adjust IT's position information to where we ended up. */
6691 IT_CHARPOS (*it) = it->bidi_it.charpos;
6692 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6693 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6694 {
6695 EMACS_INT stop = it->end_charpos;
6696 if (it->bidi_it.scan_dir < 0)
6697 stop = -1;
6698 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6699 IT_BYTEPOS (*it), stop, Qnil);
6700 }
6701 }
6702
6703 if (IT_CHARPOS (*it) >= it->stop_charpos)
6704 {
6705 if (IT_CHARPOS (*it) >= it->end_charpos)
6706 {
6707 int overlay_strings_follow_p;
6708
6709 /* End of the game, except when overlay strings follow that
6710 haven't been returned yet. */
6711 if (it->overlay_strings_at_end_processed_p)
6712 overlay_strings_follow_p = 0;
6713 else
6714 {
6715 it->overlay_strings_at_end_processed_p = 1;
6716 overlay_strings_follow_p = get_overlay_strings (it, 0);
6717 }
6718
6719 if (overlay_strings_follow_p)
6720 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6721 else
6722 {
6723 it->what = IT_EOB;
6724 it->position = it->current.pos;
6725 success_p = 0;
6726 }
6727 }
6728 else if (!(!it->bidi_p
6729 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6730 || IT_CHARPOS (*it) == it->stop_charpos))
6731 {
6732 /* With bidi non-linear iteration, we could find ourselves
6733 far beyond the last computed stop_charpos, with several
6734 other stop positions in between that we missed. Scan
6735 them all now, in buffer's logical order, until we find
6736 and handle the last stop_charpos that precedes our
6737 current position. */
6738 handle_stop_backwards (it, it->stop_charpos);
6739 return GET_NEXT_DISPLAY_ELEMENT (it);
6740 }
6741 else
6742 {
6743 if (it->bidi_p)
6744 {
6745 /* Take note of the stop position we just moved across,
6746 for when we will move back across it. */
6747 it->prev_stop = it->stop_charpos;
6748 /* If we are at base paragraph embedding level, take
6749 note of the last stop position seen at this
6750 level. */
6751 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6752 it->base_level_stop = it->stop_charpos;
6753 }
6754 handle_stop (it);
6755 return GET_NEXT_DISPLAY_ELEMENT (it);
6756 }
6757 }
6758 else if (it->bidi_p
6759 /* We can sometimes back up for reasons that have nothing
6760 to do with bidi reordering. E.g., compositions. The
6761 code below is only needed when we are above the base
6762 embedding level, so test for that explicitly. */
6763 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6764 && IT_CHARPOS (*it) < it->prev_stop)
6765 {
6766 if (it->base_level_stop <= 0)
6767 it->base_level_stop = BEGV;
6768 if (IT_CHARPOS (*it) < it->base_level_stop)
6769 abort ();
6770 handle_stop_backwards (it, it->base_level_stop);
6771 return GET_NEXT_DISPLAY_ELEMENT (it);
6772 }
6773 else
6774 {
6775 /* No face changes, overlays etc. in sight, so just return a
6776 character from current_buffer. */
6777 unsigned char *p;
6778 EMACS_INT stop;
6779
6780 /* Maybe run the redisplay end trigger hook. Performance note:
6781 This doesn't seem to cost measurable time. */
6782 if (it->redisplay_end_trigger_charpos
6783 && it->glyph_row
6784 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6785 run_redisplay_end_trigger_hook (it);
6786
6787 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6788 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6789 stop)
6790 && next_element_from_composition (it))
6791 {
6792 return 1;
6793 }
6794
6795 /* Get the next character, maybe multibyte. */
6796 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6797 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6798 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6799 else
6800 it->c = *p, it->len = 1;
6801
6802 /* Record what we have and where it came from. */
6803 it->what = IT_CHARACTER;
6804 it->object = it->w->buffer;
6805 it->position = it->current.pos;
6806
6807 /* Normally we return the character found above, except when we
6808 really want to return an ellipsis for selective display. */
6809 if (it->selective)
6810 {
6811 if (it->c == '\n')
6812 {
6813 /* A value of selective > 0 means hide lines indented more
6814 than that number of columns. */
6815 if (it->selective > 0
6816 && IT_CHARPOS (*it) + 1 < ZV
6817 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6818 IT_BYTEPOS (*it) + 1,
6819 it->selective))
6820 {
6821 success_p = next_element_from_ellipsis (it);
6822 it->dpvec_char_len = -1;
6823 }
6824 }
6825 else if (it->c == '\r' && it->selective == -1)
6826 {
6827 /* A value of selective == -1 means that everything from the
6828 CR to the end of the line is invisible, with maybe an
6829 ellipsis displayed for it. */
6830 success_p = next_element_from_ellipsis (it);
6831 it->dpvec_char_len = -1;
6832 }
6833 }
6834 }
6835
6836 /* Value is zero if end of buffer reached. */
6837 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6838 return success_p;
6839 }
6840
6841
6842 /* Run the redisplay end trigger hook for IT. */
6843
6844 static void
6845 run_redisplay_end_trigger_hook (struct it *it)
6846 {
6847 Lisp_Object args[3];
6848
6849 /* IT->glyph_row should be non-null, i.e. we should be actually
6850 displaying something, or otherwise we should not run the hook. */
6851 xassert (it->glyph_row);
6852
6853 /* Set up hook arguments. */
6854 args[0] = Qredisplay_end_trigger_functions;
6855 args[1] = it->window;
6856 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6857 it->redisplay_end_trigger_charpos = 0;
6858
6859 /* Since we are *trying* to run these functions, don't try to run
6860 them again, even if they get an error. */
6861 it->w->redisplay_end_trigger = Qnil;
6862 Frun_hook_with_args (3, args);
6863
6864 /* Notice if it changed the face of the character we are on. */
6865 handle_face_prop (it);
6866 }
6867
6868
6869 /* Deliver a composition display element. Unlike the other
6870 next_element_from_XXX, this function is not registered in the array
6871 get_next_element[]. It is called from next_element_from_buffer and
6872 next_element_from_string when necessary. */
6873
6874 static int
6875 next_element_from_composition (struct it *it)
6876 {
6877 it->what = IT_COMPOSITION;
6878 it->len = it->cmp_it.nbytes;
6879 if (STRINGP (it->string))
6880 {
6881 if (it->c < 0)
6882 {
6883 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6884 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6885 return 0;
6886 }
6887 it->position = it->current.string_pos;
6888 it->object = it->string;
6889 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6890 IT_STRING_BYTEPOS (*it), it->string);
6891 }
6892 else
6893 {
6894 if (it->c < 0)
6895 {
6896 IT_CHARPOS (*it) += it->cmp_it.nchars;
6897 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6898 if (it->bidi_p)
6899 {
6900 if (it->bidi_it.new_paragraph)
6901 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6902 /* Resync the bidi iterator with IT's new position.
6903 FIXME: this doesn't support bidirectional text. */
6904 while (it->bidi_it.charpos < IT_CHARPOS (*it))
6905 bidi_move_to_visually_next (&it->bidi_it);
6906 }
6907 return 0;
6908 }
6909 it->position = it->current.pos;
6910 it->object = it->w->buffer;
6911 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6912 IT_BYTEPOS (*it), Qnil);
6913 }
6914 return 1;
6915 }
6916
6917
6918 \f
6919 /***********************************************************************
6920 Moving an iterator without producing glyphs
6921 ***********************************************************************/
6922
6923 /* Check if iterator is at a position corresponding to a valid buffer
6924 position after some move_it_ call. */
6925
6926 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6927 ((it)->method == GET_FROM_STRING \
6928 ? IT_STRING_CHARPOS (*it) == 0 \
6929 : 1)
6930
6931
6932 /* Move iterator IT to a specified buffer or X position within one
6933 line on the display without producing glyphs.
6934
6935 OP should be a bit mask including some or all of these bits:
6936 MOVE_TO_X: Stop upon reaching x-position TO_X.
6937 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6938 Regardless of OP's value, stop upon reaching the end of the display line.
6939
6940 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6941 This means, in particular, that TO_X includes window's horizontal
6942 scroll amount.
6943
6944 The return value has several possible values that
6945 say what condition caused the scan to stop:
6946
6947 MOVE_POS_MATCH_OR_ZV
6948 - when TO_POS or ZV was reached.
6949
6950 MOVE_X_REACHED
6951 -when TO_X was reached before TO_POS or ZV were reached.
6952
6953 MOVE_LINE_CONTINUED
6954 - when we reached the end of the display area and the line must
6955 be continued.
6956
6957 MOVE_LINE_TRUNCATED
6958 - when we reached the end of the display area and the line is
6959 truncated.
6960
6961 MOVE_NEWLINE_OR_CR
6962 - when we stopped at a line end, i.e. a newline or a CR and selective
6963 display is on. */
6964
6965 static enum move_it_result
6966 move_it_in_display_line_to (struct it *it,
6967 EMACS_INT to_charpos, int to_x,
6968 enum move_operation_enum op)
6969 {
6970 enum move_it_result result = MOVE_UNDEFINED;
6971 struct glyph_row *saved_glyph_row;
6972 struct it wrap_it, atpos_it, atx_it;
6973 int may_wrap = 0;
6974 enum it_method prev_method = it->method;
6975 EMACS_INT prev_pos = IT_CHARPOS (*it);
6976
6977 /* Don't produce glyphs in produce_glyphs. */
6978 saved_glyph_row = it->glyph_row;
6979 it->glyph_row = NULL;
6980
6981 /* Use wrap_it to save a copy of IT wherever a word wrap could
6982 occur. Use atpos_it to save a copy of IT at the desired buffer
6983 position, if found, so that we can scan ahead and check if the
6984 word later overshoots the window edge. Use atx_it similarly, for
6985 pixel positions. */
6986 wrap_it.sp = -1;
6987 atpos_it.sp = -1;
6988 atx_it.sp = -1;
6989
6990 #define BUFFER_POS_REACHED_P() \
6991 ((op & MOVE_TO_POS) != 0 \
6992 && BUFFERP (it->object) \
6993 && (IT_CHARPOS (*it) == to_charpos \
6994 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
6995 && (it->method == GET_FROM_BUFFER \
6996 || (it->method == GET_FROM_DISPLAY_VECTOR \
6997 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6998
6999 /* If there's a line-/wrap-prefix, handle it. */
7000 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7001 && it->current_y < it->last_visible_y)
7002 handle_line_prefix (it);
7003
7004 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7005 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7006
7007 while (1)
7008 {
7009 int x, i, ascent = 0, descent = 0;
7010
7011 /* Utility macro to reset an iterator with x, ascent, and descent. */
7012 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7013 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7014 (IT)->max_descent = descent)
7015
7016 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7017 glyph). */
7018 if ((op & MOVE_TO_POS) != 0
7019 && BUFFERP (it->object)
7020 && it->method == GET_FROM_BUFFER
7021 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7022 || (it->bidi_p
7023 && (prev_method == GET_FROM_IMAGE
7024 || prev_method == GET_FROM_STRETCH)
7025 /* Passed TO_CHARPOS from left to right. */
7026 && ((prev_pos < to_charpos
7027 && IT_CHARPOS (*it) > to_charpos)
7028 /* Passed TO_CHARPOS from right to left. */
7029 || (prev_pos > to_charpos
7030 && IT_CHARPOS (*it) < to_charpos)))))
7031 {
7032 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7033 {
7034 result = MOVE_POS_MATCH_OR_ZV;
7035 break;
7036 }
7037 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7038 /* If wrap_it is valid, the current position might be in a
7039 word that is wrapped. So, save the iterator in
7040 atpos_it and continue to see if wrapping happens. */
7041 atpos_it = *it;
7042 }
7043
7044 prev_method = it->method;
7045 if (it->method == GET_FROM_BUFFER)
7046 prev_pos = IT_CHARPOS (*it);
7047 /* Stop when ZV reached.
7048 We used to stop here when TO_CHARPOS reached as well, but that is
7049 too soon if this glyph does not fit on this line. So we handle it
7050 explicitly below. */
7051 if (!get_next_display_element (it))
7052 {
7053 result = MOVE_POS_MATCH_OR_ZV;
7054 break;
7055 }
7056
7057 if (it->line_wrap == TRUNCATE)
7058 {
7059 if (BUFFER_POS_REACHED_P ())
7060 {
7061 result = MOVE_POS_MATCH_OR_ZV;
7062 break;
7063 }
7064 }
7065 else
7066 {
7067 if (it->line_wrap == WORD_WRAP)
7068 {
7069 if (IT_DISPLAYING_WHITESPACE (it))
7070 may_wrap = 1;
7071 else if (may_wrap)
7072 {
7073 /* We have reached a glyph that follows one or more
7074 whitespace characters. If the position is
7075 already found, we are done. */
7076 if (atpos_it.sp >= 0)
7077 {
7078 *it = atpos_it;
7079 result = MOVE_POS_MATCH_OR_ZV;
7080 goto done;
7081 }
7082 if (atx_it.sp >= 0)
7083 {
7084 *it = atx_it;
7085 result = MOVE_X_REACHED;
7086 goto done;
7087 }
7088 /* Otherwise, we can wrap here. */
7089 wrap_it = *it;
7090 may_wrap = 0;
7091 }
7092 }
7093 }
7094
7095 /* Remember the line height for the current line, in case
7096 the next element doesn't fit on the line. */
7097 ascent = it->max_ascent;
7098 descent = it->max_descent;
7099
7100 /* The call to produce_glyphs will get the metrics of the
7101 display element IT is loaded with. Record the x-position
7102 before this display element, in case it doesn't fit on the
7103 line. */
7104 x = it->current_x;
7105
7106 PRODUCE_GLYPHS (it);
7107
7108 if (it->area != TEXT_AREA)
7109 {
7110 set_iterator_to_next (it, 1);
7111 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7112 SET_TEXT_POS (this_line_min_pos,
7113 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7114 continue;
7115 }
7116
7117 /* The number of glyphs we get back in IT->nglyphs will normally
7118 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7119 character on a terminal frame, or (iii) a line end. For the
7120 second case, IT->nglyphs - 1 padding glyphs will be present.
7121 (On X frames, there is only one glyph produced for a
7122 composite character.)
7123
7124 The behavior implemented below means, for continuation lines,
7125 that as many spaces of a TAB as fit on the current line are
7126 displayed there. For terminal frames, as many glyphs of a
7127 multi-glyph character are displayed in the current line, too.
7128 This is what the old redisplay code did, and we keep it that
7129 way. Under X, the whole shape of a complex character must
7130 fit on the line or it will be completely displayed in the
7131 next line.
7132
7133 Note that both for tabs and padding glyphs, all glyphs have
7134 the same width. */
7135 if (it->nglyphs)
7136 {
7137 /* More than one glyph or glyph doesn't fit on line. All
7138 glyphs have the same width. */
7139 int single_glyph_width = it->pixel_width / it->nglyphs;
7140 int new_x;
7141 int x_before_this_char = x;
7142 int hpos_before_this_char = it->hpos;
7143
7144 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7145 {
7146 new_x = x + single_glyph_width;
7147
7148 /* We want to leave anything reaching TO_X to the caller. */
7149 if ((op & MOVE_TO_X) && new_x > to_x)
7150 {
7151 if (BUFFER_POS_REACHED_P ())
7152 {
7153 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7154 goto buffer_pos_reached;
7155 if (atpos_it.sp < 0)
7156 {
7157 atpos_it = *it;
7158 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7159 }
7160 }
7161 else
7162 {
7163 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7164 {
7165 it->current_x = x;
7166 result = MOVE_X_REACHED;
7167 break;
7168 }
7169 if (atx_it.sp < 0)
7170 {
7171 atx_it = *it;
7172 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7173 }
7174 }
7175 }
7176
7177 if (/* Lines are continued. */
7178 it->line_wrap != TRUNCATE
7179 && (/* And glyph doesn't fit on the line. */
7180 new_x > it->last_visible_x
7181 /* Or it fits exactly and we're on a window
7182 system frame. */
7183 || (new_x == it->last_visible_x
7184 && FRAME_WINDOW_P (it->f))))
7185 {
7186 if (/* IT->hpos == 0 means the very first glyph
7187 doesn't fit on the line, e.g. a wide image. */
7188 it->hpos == 0
7189 || (new_x == it->last_visible_x
7190 && FRAME_WINDOW_P (it->f)))
7191 {
7192 ++it->hpos;
7193 it->current_x = new_x;
7194
7195 /* The character's last glyph just barely fits
7196 in this row. */
7197 if (i == it->nglyphs - 1)
7198 {
7199 /* If this is the destination position,
7200 return a position *before* it in this row,
7201 now that we know it fits in this row. */
7202 if (BUFFER_POS_REACHED_P ())
7203 {
7204 if (it->line_wrap != WORD_WRAP
7205 || wrap_it.sp < 0)
7206 {
7207 it->hpos = hpos_before_this_char;
7208 it->current_x = x_before_this_char;
7209 result = MOVE_POS_MATCH_OR_ZV;
7210 break;
7211 }
7212 if (it->line_wrap == WORD_WRAP
7213 && atpos_it.sp < 0)
7214 {
7215 atpos_it = *it;
7216 atpos_it.current_x = x_before_this_char;
7217 atpos_it.hpos = hpos_before_this_char;
7218 }
7219 }
7220
7221 set_iterator_to_next (it, 1);
7222 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7223 SET_TEXT_POS (this_line_min_pos,
7224 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7225 /* On graphical terminals, newlines may
7226 "overflow" into the fringe if
7227 overflow-newline-into-fringe is non-nil.
7228 On text-only terminals, newlines may
7229 overflow into the last glyph on the
7230 display line.*/
7231 if (!FRAME_WINDOW_P (it->f)
7232 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7233 {
7234 if (!get_next_display_element (it))
7235 {
7236 result = MOVE_POS_MATCH_OR_ZV;
7237 break;
7238 }
7239 if (BUFFER_POS_REACHED_P ())
7240 {
7241 if (ITERATOR_AT_END_OF_LINE_P (it))
7242 result = MOVE_POS_MATCH_OR_ZV;
7243 else
7244 result = MOVE_LINE_CONTINUED;
7245 break;
7246 }
7247 if (ITERATOR_AT_END_OF_LINE_P (it))
7248 {
7249 result = MOVE_NEWLINE_OR_CR;
7250 break;
7251 }
7252 }
7253 }
7254 }
7255 else
7256 IT_RESET_X_ASCENT_DESCENT (it);
7257
7258 if (wrap_it.sp >= 0)
7259 {
7260 *it = wrap_it;
7261 atpos_it.sp = -1;
7262 atx_it.sp = -1;
7263 }
7264
7265 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7266 IT_CHARPOS (*it)));
7267 result = MOVE_LINE_CONTINUED;
7268 break;
7269 }
7270
7271 if (BUFFER_POS_REACHED_P ())
7272 {
7273 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7274 goto buffer_pos_reached;
7275 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7276 {
7277 atpos_it = *it;
7278 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7279 }
7280 }
7281
7282 if (new_x > it->first_visible_x)
7283 {
7284 /* Glyph is visible. Increment number of glyphs that
7285 would be displayed. */
7286 ++it->hpos;
7287 }
7288 }
7289
7290 if (result != MOVE_UNDEFINED)
7291 break;
7292 }
7293 else if (BUFFER_POS_REACHED_P ())
7294 {
7295 buffer_pos_reached:
7296 IT_RESET_X_ASCENT_DESCENT (it);
7297 result = MOVE_POS_MATCH_OR_ZV;
7298 break;
7299 }
7300 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7301 {
7302 /* Stop when TO_X specified and reached. This check is
7303 necessary here because of lines consisting of a line end,
7304 only. The line end will not produce any glyphs and we
7305 would never get MOVE_X_REACHED. */
7306 xassert (it->nglyphs == 0);
7307 result = MOVE_X_REACHED;
7308 break;
7309 }
7310
7311 /* Is this a line end? If yes, we're done. */
7312 if (ITERATOR_AT_END_OF_LINE_P (it))
7313 {
7314 result = MOVE_NEWLINE_OR_CR;
7315 break;
7316 }
7317
7318 if (it->method == GET_FROM_BUFFER)
7319 prev_pos = IT_CHARPOS (*it);
7320 /* The current display element has been consumed. Advance
7321 to the next. */
7322 set_iterator_to_next (it, 1);
7323 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7324 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7325
7326 /* Stop if lines are truncated and IT's current x-position is
7327 past the right edge of the window now. */
7328 if (it->line_wrap == TRUNCATE
7329 && it->current_x >= it->last_visible_x)
7330 {
7331 if (!FRAME_WINDOW_P (it->f)
7332 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7333 {
7334 if (!get_next_display_element (it)
7335 || BUFFER_POS_REACHED_P ())
7336 {
7337 result = MOVE_POS_MATCH_OR_ZV;
7338 break;
7339 }
7340 if (ITERATOR_AT_END_OF_LINE_P (it))
7341 {
7342 result = MOVE_NEWLINE_OR_CR;
7343 break;
7344 }
7345 }
7346 result = MOVE_LINE_TRUNCATED;
7347 break;
7348 }
7349 #undef IT_RESET_X_ASCENT_DESCENT
7350 }
7351
7352 #undef BUFFER_POS_REACHED_P
7353
7354 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7355 restore the saved iterator. */
7356 if (atpos_it.sp >= 0)
7357 *it = atpos_it;
7358 else if (atx_it.sp >= 0)
7359 *it = atx_it;
7360
7361 done:
7362
7363 /* Restore the iterator settings altered at the beginning of this
7364 function. */
7365 it->glyph_row = saved_glyph_row;
7366 return result;
7367 }
7368
7369 /* For external use. */
7370 void
7371 move_it_in_display_line (struct it *it,
7372 EMACS_INT to_charpos, int to_x,
7373 enum move_operation_enum op)
7374 {
7375 if (it->line_wrap == WORD_WRAP
7376 && (op & MOVE_TO_X))
7377 {
7378 struct it save_it = *it;
7379 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7380 /* When word-wrap is on, TO_X may lie past the end
7381 of a wrapped line. Then it->current is the
7382 character on the next line, so backtrack to the
7383 space before the wrap point. */
7384 if (skip == MOVE_LINE_CONTINUED)
7385 {
7386 int prev_x = max (it->current_x - 1, 0);
7387 *it = save_it;
7388 move_it_in_display_line_to
7389 (it, -1, prev_x, MOVE_TO_X);
7390 }
7391 }
7392 else
7393 move_it_in_display_line_to (it, to_charpos, to_x, op);
7394 }
7395
7396
7397 /* Move IT forward until it satisfies one or more of the criteria in
7398 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7399
7400 OP is a bit-mask that specifies where to stop, and in particular,
7401 which of those four position arguments makes a difference. See the
7402 description of enum move_operation_enum.
7403
7404 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7405 screen line, this function will set IT to the next position >
7406 TO_CHARPOS. */
7407
7408 void
7409 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7410 {
7411 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7412 int line_height, line_start_x = 0, reached = 0;
7413
7414 for (;;)
7415 {
7416 if (op & MOVE_TO_VPOS)
7417 {
7418 /* If no TO_CHARPOS and no TO_X specified, stop at the
7419 start of the line TO_VPOS. */
7420 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7421 {
7422 if (it->vpos == to_vpos)
7423 {
7424 reached = 1;
7425 break;
7426 }
7427 else
7428 skip = move_it_in_display_line_to (it, -1, -1, 0);
7429 }
7430 else
7431 {
7432 /* TO_VPOS >= 0 means stop at TO_X in the line at
7433 TO_VPOS, or at TO_POS, whichever comes first. */
7434 if (it->vpos == to_vpos)
7435 {
7436 reached = 2;
7437 break;
7438 }
7439
7440 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7441
7442 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7443 {
7444 reached = 3;
7445 break;
7446 }
7447 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7448 {
7449 /* We have reached TO_X but not in the line we want. */
7450 skip = move_it_in_display_line_to (it, to_charpos,
7451 -1, MOVE_TO_POS);
7452 if (skip == MOVE_POS_MATCH_OR_ZV)
7453 {
7454 reached = 4;
7455 break;
7456 }
7457 }
7458 }
7459 }
7460 else if (op & MOVE_TO_Y)
7461 {
7462 struct it it_backup;
7463
7464 if (it->line_wrap == WORD_WRAP)
7465 it_backup = *it;
7466
7467 /* TO_Y specified means stop at TO_X in the line containing
7468 TO_Y---or at TO_CHARPOS if this is reached first. The
7469 problem is that we can't really tell whether the line
7470 contains TO_Y before we have completely scanned it, and
7471 this may skip past TO_X. What we do is to first scan to
7472 TO_X.
7473
7474 If TO_X is not specified, use a TO_X of zero. The reason
7475 is to make the outcome of this function more predictable.
7476 If we didn't use TO_X == 0, we would stop at the end of
7477 the line which is probably not what a caller would expect
7478 to happen. */
7479 skip = move_it_in_display_line_to
7480 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7481 (MOVE_TO_X | (op & MOVE_TO_POS)));
7482
7483 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7484 if (skip == MOVE_POS_MATCH_OR_ZV)
7485 reached = 5;
7486 else if (skip == MOVE_X_REACHED)
7487 {
7488 /* If TO_X was reached, we want to know whether TO_Y is
7489 in the line. We know this is the case if the already
7490 scanned glyphs make the line tall enough. Otherwise,
7491 we must check by scanning the rest of the line. */
7492 line_height = it->max_ascent + it->max_descent;
7493 if (to_y >= it->current_y
7494 && to_y < it->current_y + line_height)
7495 {
7496 reached = 6;
7497 break;
7498 }
7499 it_backup = *it;
7500 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7501 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7502 op & MOVE_TO_POS);
7503 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7504 line_height = it->max_ascent + it->max_descent;
7505 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7506
7507 if (to_y >= it->current_y
7508 && to_y < it->current_y + line_height)
7509 {
7510 /* If TO_Y is in this line and TO_X was reached
7511 above, we scanned too far. We have to restore
7512 IT's settings to the ones before skipping. */
7513 *it = it_backup;
7514 reached = 6;
7515 }
7516 else
7517 {
7518 skip = skip2;
7519 if (skip == MOVE_POS_MATCH_OR_ZV)
7520 reached = 7;
7521 }
7522 }
7523 else
7524 {
7525 /* Check whether TO_Y is in this line. */
7526 line_height = it->max_ascent + it->max_descent;
7527 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7528
7529 if (to_y >= it->current_y
7530 && to_y < it->current_y + line_height)
7531 {
7532 /* When word-wrap is on, TO_X may lie past the end
7533 of a wrapped line. Then it->current is the
7534 character on the next line, so backtrack to the
7535 space before the wrap point. */
7536 if (skip == MOVE_LINE_CONTINUED
7537 && it->line_wrap == WORD_WRAP)
7538 {
7539 int prev_x = max (it->current_x - 1, 0);
7540 *it = it_backup;
7541 skip = move_it_in_display_line_to
7542 (it, -1, prev_x, MOVE_TO_X);
7543 }
7544 reached = 6;
7545 }
7546 }
7547
7548 if (reached)
7549 break;
7550 }
7551 else if (BUFFERP (it->object)
7552 && (it->method == GET_FROM_BUFFER
7553 || it->method == GET_FROM_STRETCH)
7554 && IT_CHARPOS (*it) >= to_charpos)
7555 skip = MOVE_POS_MATCH_OR_ZV;
7556 else
7557 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7558
7559 switch (skip)
7560 {
7561 case MOVE_POS_MATCH_OR_ZV:
7562 reached = 8;
7563 goto out;
7564
7565 case MOVE_NEWLINE_OR_CR:
7566 set_iterator_to_next (it, 1);
7567 it->continuation_lines_width = 0;
7568 break;
7569
7570 case MOVE_LINE_TRUNCATED:
7571 it->continuation_lines_width = 0;
7572 reseat_at_next_visible_line_start (it, 0);
7573 if ((op & MOVE_TO_POS) != 0
7574 && IT_CHARPOS (*it) > to_charpos)
7575 {
7576 reached = 9;
7577 goto out;
7578 }
7579 break;
7580
7581 case MOVE_LINE_CONTINUED:
7582 /* For continued lines ending in a tab, some of the glyphs
7583 associated with the tab are displayed on the current
7584 line. Since it->current_x does not include these glyphs,
7585 we use it->last_visible_x instead. */
7586 if (it->c == '\t')
7587 {
7588 it->continuation_lines_width += it->last_visible_x;
7589 /* When moving by vpos, ensure that the iterator really
7590 advances to the next line (bug#847, bug#969). Fixme:
7591 do we need to do this in other circumstances? */
7592 if (it->current_x != it->last_visible_x
7593 && (op & MOVE_TO_VPOS)
7594 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7595 {
7596 line_start_x = it->current_x + it->pixel_width
7597 - it->last_visible_x;
7598 set_iterator_to_next (it, 0);
7599 }
7600 }
7601 else
7602 it->continuation_lines_width += it->current_x;
7603 break;
7604
7605 default:
7606 abort ();
7607 }
7608
7609 /* Reset/increment for the next run. */
7610 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7611 it->current_x = line_start_x;
7612 line_start_x = 0;
7613 it->hpos = 0;
7614 it->current_y += it->max_ascent + it->max_descent;
7615 ++it->vpos;
7616 last_height = it->max_ascent + it->max_descent;
7617 last_max_ascent = it->max_ascent;
7618 it->max_ascent = it->max_descent = 0;
7619 }
7620
7621 out:
7622
7623 /* On text terminals, we may stop at the end of a line in the middle
7624 of a multi-character glyph. If the glyph itself is continued,
7625 i.e. it is actually displayed on the next line, don't treat this
7626 stopping point as valid; move to the next line instead (unless
7627 that brings us offscreen). */
7628 if (!FRAME_WINDOW_P (it->f)
7629 && op & MOVE_TO_POS
7630 && IT_CHARPOS (*it) == to_charpos
7631 && it->what == IT_CHARACTER
7632 && it->nglyphs > 1
7633 && it->line_wrap == WINDOW_WRAP
7634 && it->current_x == it->last_visible_x - 1
7635 && it->c != '\n'
7636 && it->c != '\t'
7637 && it->vpos < XFASTINT (it->w->window_end_vpos))
7638 {
7639 it->continuation_lines_width += it->current_x;
7640 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7641 it->current_y += it->max_ascent + it->max_descent;
7642 ++it->vpos;
7643 last_height = it->max_ascent + it->max_descent;
7644 last_max_ascent = it->max_ascent;
7645 }
7646
7647 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7648 }
7649
7650
7651 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7652
7653 If DY > 0, move IT backward at least that many pixels. DY = 0
7654 means move IT backward to the preceding line start or BEGV. This
7655 function may move over more than DY pixels if IT->current_y - DY
7656 ends up in the middle of a line; in this case IT->current_y will be
7657 set to the top of the line moved to. */
7658
7659 void
7660 move_it_vertically_backward (struct it *it, int dy)
7661 {
7662 int nlines, h;
7663 struct it it2, it3;
7664 EMACS_INT start_pos;
7665
7666 move_further_back:
7667 xassert (dy >= 0);
7668
7669 start_pos = IT_CHARPOS (*it);
7670
7671 /* Estimate how many newlines we must move back. */
7672 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7673
7674 /* Set the iterator's position that many lines back. */
7675 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7676 back_to_previous_visible_line_start (it);
7677
7678 /* Reseat the iterator here. When moving backward, we don't want
7679 reseat to skip forward over invisible text, set up the iterator
7680 to deliver from overlay strings at the new position etc. So,
7681 use reseat_1 here. */
7682 reseat_1 (it, it->current.pos, 1);
7683
7684 /* We are now surely at a line start. */
7685 it->current_x = it->hpos = 0;
7686 it->continuation_lines_width = 0;
7687
7688 /* Move forward and see what y-distance we moved. First move to the
7689 start of the next line so that we get its height. We need this
7690 height to be able to tell whether we reached the specified
7691 y-distance. */
7692 it2 = *it;
7693 it2.max_ascent = it2.max_descent = 0;
7694 do
7695 {
7696 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7697 MOVE_TO_POS | MOVE_TO_VPOS);
7698 }
7699 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7700 xassert (IT_CHARPOS (*it) >= BEGV);
7701 it3 = it2;
7702
7703 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7704 xassert (IT_CHARPOS (*it) >= BEGV);
7705 /* H is the actual vertical distance from the position in *IT
7706 and the starting position. */
7707 h = it2.current_y - it->current_y;
7708 /* NLINES is the distance in number of lines. */
7709 nlines = it2.vpos - it->vpos;
7710
7711 /* Correct IT's y and vpos position
7712 so that they are relative to the starting point. */
7713 it->vpos -= nlines;
7714 it->current_y -= h;
7715
7716 if (dy == 0)
7717 {
7718 /* DY == 0 means move to the start of the screen line. The
7719 value of nlines is > 0 if continuation lines were involved. */
7720 if (nlines > 0)
7721 move_it_by_lines (it, nlines);
7722 }
7723 else
7724 {
7725 /* The y-position we try to reach, relative to *IT.
7726 Note that H has been subtracted in front of the if-statement. */
7727 int target_y = it->current_y + h - dy;
7728 int y0 = it3.current_y;
7729 int y1 = line_bottom_y (&it3);
7730 int line_height = y1 - y0;
7731
7732 /* If we did not reach target_y, try to move further backward if
7733 we can. If we moved too far backward, try to move forward. */
7734 if (target_y < it->current_y
7735 /* This is heuristic. In a window that's 3 lines high, with
7736 a line height of 13 pixels each, recentering with point
7737 on the bottom line will try to move -39/2 = 19 pixels
7738 backward. Try to avoid moving into the first line. */
7739 && (it->current_y - target_y
7740 > min (window_box_height (it->w), line_height * 2 / 3))
7741 && IT_CHARPOS (*it) > BEGV)
7742 {
7743 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7744 target_y - it->current_y));
7745 dy = it->current_y - target_y;
7746 goto move_further_back;
7747 }
7748 else if (target_y >= it->current_y + line_height
7749 && IT_CHARPOS (*it) < ZV)
7750 {
7751 /* Should move forward by at least one line, maybe more.
7752
7753 Note: Calling move_it_by_lines can be expensive on
7754 terminal frames, where compute_motion is used (via
7755 vmotion) to do the job, when there are very long lines
7756 and truncate-lines is nil. That's the reason for
7757 treating terminal frames specially here. */
7758
7759 if (!FRAME_WINDOW_P (it->f))
7760 move_it_vertically (it, target_y - (it->current_y + line_height));
7761 else
7762 {
7763 do
7764 {
7765 move_it_by_lines (it, 1);
7766 }
7767 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7768 }
7769 }
7770 }
7771 }
7772
7773
7774 /* Move IT by a specified amount of pixel lines DY. DY negative means
7775 move backwards. DY = 0 means move to start of screen line. At the
7776 end, IT will be on the start of a screen line. */
7777
7778 void
7779 move_it_vertically (struct it *it, int dy)
7780 {
7781 if (dy <= 0)
7782 move_it_vertically_backward (it, -dy);
7783 else
7784 {
7785 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7786 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7787 MOVE_TO_POS | MOVE_TO_Y);
7788 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7789
7790 /* If buffer ends in ZV without a newline, move to the start of
7791 the line to satisfy the post-condition. */
7792 if (IT_CHARPOS (*it) == ZV
7793 && ZV > BEGV
7794 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7795 move_it_by_lines (it, 0);
7796 }
7797 }
7798
7799
7800 /* Move iterator IT past the end of the text line it is in. */
7801
7802 void
7803 move_it_past_eol (struct it *it)
7804 {
7805 enum move_it_result rc;
7806
7807 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7808 if (rc == MOVE_NEWLINE_OR_CR)
7809 set_iterator_to_next (it, 0);
7810 }
7811
7812
7813 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7814 negative means move up. DVPOS == 0 means move to the start of the
7815 screen line.
7816
7817 Optimization idea: If we would know that IT->f doesn't use
7818 a face with proportional font, we could be faster for
7819 truncate-lines nil. */
7820
7821 void
7822 move_it_by_lines (struct it *it, int dvpos)
7823 {
7824
7825 /* The commented-out optimization uses vmotion on terminals. This
7826 gives bad results, because elements like it->what, on which
7827 callers such as pos_visible_p rely, aren't updated. */
7828 /* struct position pos;
7829 if (!FRAME_WINDOW_P (it->f))
7830 {
7831 struct text_pos textpos;
7832
7833 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7834 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7835 reseat (it, textpos, 1);
7836 it->vpos += pos.vpos;
7837 it->current_y += pos.vpos;
7838 }
7839 else */
7840
7841 if (dvpos == 0)
7842 {
7843 /* DVPOS == 0 means move to the start of the screen line. */
7844 move_it_vertically_backward (it, 0);
7845 xassert (it->current_x == 0 && it->hpos == 0);
7846 /* Let next call to line_bottom_y calculate real line height */
7847 last_height = 0;
7848 }
7849 else if (dvpos > 0)
7850 {
7851 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7852 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7853 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7854 }
7855 else
7856 {
7857 struct it it2;
7858 EMACS_INT start_charpos, i;
7859
7860 /* Start at the beginning of the screen line containing IT's
7861 position. This may actually move vertically backwards,
7862 in case of overlays, so adjust dvpos accordingly. */
7863 dvpos += it->vpos;
7864 move_it_vertically_backward (it, 0);
7865 dvpos -= it->vpos;
7866
7867 /* Go back -DVPOS visible lines and reseat the iterator there. */
7868 start_charpos = IT_CHARPOS (*it);
7869 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7870 back_to_previous_visible_line_start (it);
7871 reseat (it, it->current.pos, 1);
7872
7873 /* Move further back if we end up in a string or an image. */
7874 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7875 {
7876 /* First try to move to start of display line. */
7877 dvpos += it->vpos;
7878 move_it_vertically_backward (it, 0);
7879 dvpos -= it->vpos;
7880 if (IT_POS_VALID_AFTER_MOVE_P (it))
7881 break;
7882 /* If start of line is still in string or image,
7883 move further back. */
7884 back_to_previous_visible_line_start (it);
7885 reseat (it, it->current.pos, 1);
7886 dvpos--;
7887 }
7888
7889 it->current_x = it->hpos = 0;
7890
7891 /* Above call may have moved too far if continuation lines
7892 are involved. Scan forward and see if it did. */
7893 it2 = *it;
7894 it2.vpos = it2.current_y = 0;
7895 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7896 it->vpos -= it2.vpos;
7897 it->current_y -= it2.current_y;
7898 it->current_x = it->hpos = 0;
7899
7900 /* If we moved too far back, move IT some lines forward. */
7901 if (it2.vpos > -dvpos)
7902 {
7903 int delta = it2.vpos + dvpos;
7904 it2 = *it;
7905 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7906 /* Move back again if we got too far ahead. */
7907 if (IT_CHARPOS (*it) >= start_charpos)
7908 *it = it2;
7909 }
7910 }
7911 }
7912
7913 /* Return 1 if IT points into the middle of a display vector. */
7914
7915 int
7916 in_display_vector_p (struct it *it)
7917 {
7918 return (it->method == GET_FROM_DISPLAY_VECTOR
7919 && it->current.dpvec_index > 0
7920 && it->dpvec + it->current.dpvec_index != it->dpend);
7921 }
7922
7923 \f
7924 /***********************************************************************
7925 Messages
7926 ***********************************************************************/
7927
7928
7929 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7930 to *Messages*. */
7931
7932 void
7933 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
7934 {
7935 Lisp_Object args[3];
7936 Lisp_Object msg, fmt;
7937 char *buffer;
7938 EMACS_INT len;
7939 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7940 USE_SAFE_ALLOCA;
7941
7942 /* Do nothing if called asynchronously. Inserting text into
7943 a buffer may call after-change-functions and alike and
7944 that would means running Lisp asynchronously. */
7945 if (handling_signal)
7946 return;
7947
7948 fmt = msg = Qnil;
7949 GCPRO4 (fmt, msg, arg1, arg2);
7950
7951 args[0] = fmt = build_string (format);
7952 args[1] = arg1;
7953 args[2] = arg2;
7954 msg = Fformat (3, args);
7955
7956 len = SBYTES (msg) + 1;
7957 SAFE_ALLOCA (buffer, char *, len);
7958 memcpy (buffer, SDATA (msg), len);
7959
7960 message_dolog (buffer, len - 1, 1, 0);
7961 SAFE_FREE ();
7962
7963 UNGCPRO;
7964 }
7965
7966
7967 /* Output a newline in the *Messages* buffer if "needs" one. */
7968
7969 void
7970 message_log_maybe_newline (void)
7971 {
7972 if (message_log_need_newline)
7973 message_dolog ("", 0, 1, 0);
7974 }
7975
7976
7977 /* Add a string M of length NBYTES to the message log, optionally
7978 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7979 nonzero, means interpret the contents of M as multibyte. This
7980 function calls low-level routines in order to bypass text property
7981 hooks, etc. which might not be safe to run.
7982
7983 This may GC (insert may run before/after change hooks),
7984 so the buffer M must NOT point to a Lisp string. */
7985
7986 void
7987 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
7988 {
7989 const unsigned char *msg = (const unsigned char *) m;
7990
7991 if (!NILP (Vmemory_full))
7992 return;
7993
7994 if (!NILP (Vmessage_log_max))
7995 {
7996 struct buffer *oldbuf;
7997 Lisp_Object oldpoint, oldbegv, oldzv;
7998 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7999 EMACS_INT point_at_end = 0;
8000 EMACS_INT zv_at_end = 0;
8001 Lisp_Object old_deactivate_mark, tem;
8002 struct gcpro gcpro1;
8003
8004 old_deactivate_mark = Vdeactivate_mark;
8005 oldbuf = current_buffer;
8006 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8007 BVAR (current_buffer, undo_list) = Qt;
8008
8009 oldpoint = message_dolog_marker1;
8010 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8011 oldbegv = message_dolog_marker2;
8012 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8013 oldzv = message_dolog_marker3;
8014 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8015 GCPRO1 (old_deactivate_mark);
8016
8017 if (PT == Z)
8018 point_at_end = 1;
8019 if (ZV == Z)
8020 zv_at_end = 1;
8021
8022 BEGV = BEG;
8023 BEGV_BYTE = BEG_BYTE;
8024 ZV = Z;
8025 ZV_BYTE = Z_BYTE;
8026 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8027
8028 /* Insert the string--maybe converting multibyte to single byte
8029 or vice versa, so that all the text fits the buffer. */
8030 if (multibyte
8031 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8032 {
8033 EMACS_INT i;
8034 int c, char_bytes;
8035 char work[1];
8036
8037 /* Convert a multibyte string to single-byte
8038 for the *Message* buffer. */
8039 for (i = 0; i < nbytes; i += char_bytes)
8040 {
8041 c = string_char_and_length (msg + i, &char_bytes);
8042 work[0] = (ASCII_CHAR_P (c)
8043 ? c
8044 : multibyte_char_to_unibyte (c));
8045 insert_1_both (work, 1, 1, 1, 0, 0);
8046 }
8047 }
8048 else if (! multibyte
8049 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8050 {
8051 EMACS_INT i;
8052 int c, char_bytes;
8053 unsigned char str[MAX_MULTIBYTE_LENGTH];
8054 /* Convert a single-byte string to multibyte
8055 for the *Message* buffer. */
8056 for (i = 0; i < nbytes; i++)
8057 {
8058 c = msg[i];
8059 MAKE_CHAR_MULTIBYTE (c);
8060 char_bytes = CHAR_STRING (c, str);
8061 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8062 }
8063 }
8064 else if (nbytes)
8065 insert_1 (m, nbytes, 1, 0, 0);
8066
8067 if (nlflag)
8068 {
8069 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8070 printmax_t dups;
8071 insert_1 ("\n", 1, 1, 0, 0);
8072
8073 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8074 this_bol = PT;
8075 this_bol_byte = PT_BYTE;
8076
8077 /* See if this line duplicates the previous one.
8078 If so, combine duplicates. */
8079 if (this_bol > BEG)
8080 {
8081 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8082 prev_bol = PT;
8083 prev_bol_byte = PT_BYTE;
8084
8085 dups = message_log_check_duplicate (prev_bol_byte,
8086 this_bol_byte);
8087 if (dups)
8088 {
8089 del_range_both (prev_bol, prev_bol_byte,
8090 this_bol, this_bol_byte, 0);
8091 if (dups > 1)
8092 {
8093 char dupstr[sizeof " [ times]"
8094 + INT_STRLEN_BOUND (printmax_t)];
8095 int duplen;
8096
8097 /* If you change this format, don't forget to also
8098 change message_log_check_duplicate. */
8099 sprintf (dupstr, " [%"pMd" times]", dups);
8100 duplen = strlen (dupstr);
8101 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8102 insert_1 (dupstr, duplen, 1, 0, 1);
8103 }
8104 }
8105 }
8106
8107 /* If we have more than the desired maximum number of lines
8108 in the *Messages* buffer now, delete the oldest ones.
8109 This is safe because we don't have undo in this buffer. */
8110
8111 if (NATNUMP (Vmessage_log_max))
8112 {
8113 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8114 -XFASTINT (Vmessage_log_max) - 1, 0);
8115 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8116 }
8117 }
8118 BEGV = XMARKER (oldbegv)->charpos;
8119 BEGV_BYTE = marker_byte_position (oldbegv);
8120
8121 if (zv_at_end)
8122 {
8123 ZV = Z;
8124 ZV_BYTE = Z_BYTE;
8125 }
8126 else
8127 {
8128 ZV = XMARKER (oldzv)->charpos;
8129 ZV_BYTE = marker_byte_position (oldzv);
8130 }
8131
8132 if (point_at_end)
8133 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8134 else
8135 /* We can't do Fgoto_char (oldpoint) because it will run some
8136 Lisp code. */
8137 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8138 XMARKER (oldpoint)->bytepos);
8139
8140 UNGCPRO;
8141 unchain_marker (XMARKER (oldpoint));
8142 unchain_marker (XMARKER (oldbegv));
8143 unchain_marker (XMARKER (oldzv));
8144
8145 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8146 set_buffer_internal (oldbuf);
8147 if (NILP (tem))
8148 windows_or_buffers_changed = old_windows_or_buffers_changed;
8149 message_log_need_newline = !nlflag;
8150 Vdeactivate_mark = old_deactivate_mark;
8151 }
8152 }
8153
8154
8155 /* We are at the end of the buffer after just having inserted a newline.
8156 (Note: We depend on the fact we won't be crossing the gap.)
8157 Check to see if the most recent message looks a lot like the previous one.
8158 Return 0 if different, 1 if the new one should just replace it, or a
8159 value N > 1 if we should also append " [N times]". */
8160
8161 static intmax_t
8162 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8163 {
8164 EMACS_INT i;
8165 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8166 int seen_dots = 0;
8167 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8168 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8169
8170 for (i = 0; i < len; i++)
8171 {
8172 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8173 seen_dots = 1;
8174 if (p1[i] != p2[i])
8175 return seen_dots;
8176 }
8177 p1 += len;
8178 if (*p1 == '\n')
8179 return 2;
8180 if (*p1++ == ' ' && *p1++ == '[')
8181 {
8182 char *pend;
8183 intmax_t n = strtoimax ((char *) p1, &pend, 10);
8184 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
8185 return n+1;
8186 }
8187 return 0;
8188 }
8189 \f
8190
8191 /* Display an echo area message M with a specified length of NBYTES
8192 bytes. The string may include null characters. If M is 0, clear
8193 out any existing message, and let the mini-buffer text show
8194 through.
8195
8196 This may GC, so the buffer M must NOT point to a Lisp string. */
8197
8198 void
8199 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8200 {
8201 /* First flush out any partial line written with print. */
8202 message_log_maybe_newline ();
8203 if (m)
8204 message_dolog (m, nbytes, 1, multibyte);
8205 message2_nolog (m, nbytes, multibyte);
8206 }
8207
8208
8209 /* The non-logging counterpart of message2. */
8210
8211 void
8212 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8213 {
8214 struct frame *sf = SELECTED_FRAME ();
8215 message_enable_multibyte = multibyte;
8216
8217 if (FRAME_INITIAL_P (sf))
8218 {
8219 if (noninteractive_need_newline)
8220 putc ('\n', stderr);
8221 noninteractive_need_newline = 0;
8222 if (m)
8223 fwrite (m, nbytes, 1, stderr);
8224 if (cursor_in_echo_area == 0)
8225 fprintf (stderr, "\n");
8226 fflush (stderr);
8227 }
8228 /* A null message buffer means that the frame hasn't really been
8229 initialized yet. Error messages get reported properly by
8230 cmd_error, so this must be just an informative message; toss it. */
8231 else if (INTERACTIVE
8232 && sf->glyphs_initialized_p
8233 && FRAME_MESSAGE_BUF (sf))
8234 {
8235 Lisp_Object mini_window;
8236 struct frame *f;
8237
8238 /* Get the frame containing the mini-buffer
8239 that the selected frame is using. */
8240 mini_window = FRAME_MINIBUF_WINDOW (sf);
8241 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8242
8243 FRAME_SAMPLE_VISIBILITY (f);
8244 if (FRAME_VISIBLE_P (sf)
8245 && ! FRAME_VISIBLE_P (f))
8246 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8247
8248 if (m)
8249 {
8250 set_message (m, Qnil, nbytes, multibyte);
8251 if (minibuffer_auto_raise)
8252 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8253 }
8254 else
8255 clear_message (1, 1);
8256
8257 do_pending_window_change (0);
8258 echo_area_display (1);
8259 do_pending_window_change (0);
8260 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8261 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8262 }
8263 }
8264
8265
8266 /* Display an echo area message M with a specified length of NBYTES
8267 bytes. The string may include null characters. If M is not a
8268 string, clear out any existing message, and let the mini-buffer
8269 text show through.
8270
8271 This function cancels echoing. */
8272
8273 void
8274 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8275 {
8276 struct gcpro gcpro1;
8277
8278 GCPRO1 (m);
8279 clear_message (1,1);
8280 cancel_echoing ();
8281
8282 /* First flush out any partial line written with print. */
8283 message_log_maybe_newline ();
8284 if (STRINGP (m))
8285 {
8286 char *buffer;
8287 USE_SAFE_ALLOCA;
8288
8289 SAFE_ALLOCA (buffer, char *, nbytes);
8290 memcpy (buffer, SDATA (m), nbytes);
8291 message_dolog (buffer, nbytes, 1, multibyte);
8292 SAFE_FREE ();
8293 }
8294 message3_nolog (m, nbytes, multibyte);
8295
8296 UNGCPRO;
8297 }
8298
8299
8300 /* The non-logging version of message3.
8301 This does not cancel echoing, because it is used for echoing.
8302 Perhaps we need to make a separate function for echoing
8303 and make this cancel echoing. */
8304
8305 void
8306 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8307 {
8308 struct frame *sf = SELECTED_FRAME ();
8309 message_enable_multibyte = multibyte;
8310
8311 if (FRAME_INITIAL_P (sf))
8312 {
8313 if (noninteractive_need_newline)
8314 putc ('\n', stderr);
8315 noninteractive_need_newline = 0;
8316 if (STRINGP (m))
8317 fwrite (SDATA (m), nbytes, 1, stderr);
8318 if (cursor_in_echo_area == 0)
8319 fprintf (stderr, "\n");
8320 fflush (stderr);
8321 }
8322 /* A null message buffer means that the frame hasn't really been
8323 initialized yet. Error messages get reported properly by
8324 cmd_error, so this must be just an informative message; toss it. */
8325 else if (INTERACTIVE
8326 && sf->glyphs_initialized_p
8327 && FRAME_MESSAGE_BUF (sf))
8328 {
8329 Lisp_Object mini_window;
8330 Lisp_Object frame;
8331 struct frame *f;
8332
8333 /* Get the frame containing the mini-buffer
8334 that the selected frame is using. */
8335 mini_window = FRAME_MINIBUF_WINDOW (sf);
8336 frame = XWINDOW (mini_window)->frame;
8337 f = XFRAME (frame);
8338
8339 FRAME_SAMPLE_VISIBILITY (f);
8340 if (FRAME_VISIBLE_P (sf)
8341 && !FRAME_VISIBLE_P (f))
8342 Fmake_frame_visible (frame);
8343
8344 if (STRINGP (m) && SCHARS (m) > 0)
8345 {
8346 set_message (NULL, m, nbytes, multibyte);
8347 if (minibuffer_auto_raise)
8348 Fraise_frame (frame);
8349 /* Assume we are not echoing.
8350 (If we are, echo_now will override this.) */
8351 echo_message_buffer = Qnil;
8352 }
8353 else
8354 clear_message (1, 1);
8355
8356 do_pending_window_change (0);
8357 echo_area_display (1);
8358 do_pending_window_change (0);
8359 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8360 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8361 }
8362 }
8363
8364
8365 /* Display a null-terminated echo area message M. If M is 0, clear
8366 out any existing message, and let the mini-buffer text show through.
8367
8368 The buffer M must continue to exist until after the echo area gets
8369 cleared or some other message gets displayed there. Do not pass
8370 text that is stored in a Lisp string. Do not pass text in a buffer
8371 that was alloca'd. */
8372
8373 void
8374 message1 (const char *m)
8375 {
8376 message2 (m, (m ? strlen (m) : 0), 0);
8377 }
8378
8379
8380 /* The non-logging counterpart of message1. */
8381
8382 void
8383 message1_nolog (const char *m)
8384 {
8385 message2_nolog (m, (m ? strlen (m) : 0), 0);
8386 }
8387
8388 /* Display a message M which contains a single %s
8389 which gets replaced with STRING. */
8390
8391 void
8392 message_with_string (const char *m, Lisp_Object string, int log)
8393 {
8394 CHECK_STRING (string);
8395
8396 if (noninteractive)
8397 {
8398 if (m)
8399 {
8400 if (noninteractive_need_newline)
8401 putc ('\n', stderr);
8402 noninteractive_need_newline = 0;
8403 fprintf (stderr, m, SDATA (string));
8404 if (!cursor_in_echo_area)
8405 fprintf (stderr, "\n");
8406 fflush (stderr);
8407 }
8408 }
8409 else if (INTERACTIVE)
8410 {
8411 /* The frame whose minibuffer we're going to display the message on.
8412 It may be larger than the selected frame, so we need
8413 to use its buffer, not the selected frame's buffer. */
8414 Lisp_Object mini_window;
8415 struct frame *f, *sf = SELECTED_FRAME ();
8416
8417 /* Get the frame containing the minibuffer
8418 that the selected frame is using. */
8419 mini_window = FRAME_MINIBUF_WINDOW (sf);
8420 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8421
8422 /* A null message buffer means that the frame hasn't really been
8423 initialized yet. Error messages get reported properly by
8424 cmd_error, so this must be just an informative message; toss it. */
8425 if (FRAME_MESSAGE_BUF (f))
8426 {
8427 Lisp_Object args[2], msg;
8428 struct gcpro gcpro1, gcpro2;
8429
8430 args[0] = build_string (m);
8431 args[1] = msg = string;
8432 GCPRO2 (args[0], msg);
8433 gcpro1.nvars = 2;
8434
8435 msg = Fformat (2, args);
8436
8437 if (log)
8438 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8439 else
8440 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8441
8442 UNGCPRO;
8443
8444 /* Print should start at the beginning of the message
8445 buffer next time. */
8446 message_buf_print = 0;
8447 }
8448 }
8449 }
8450
8451
8452 /* Dump an informative message to the minibuf. If M is 0, clear out
8453 any existing message, and let the mini-buffer text show through. */
8454
8455 static void
8456 vmessage (const char *m, va_list ap)
8457 {
8458 if (noninteractive)
8459 {
8460 if (m)
8461 {
8462 if (noninteractive_need_newline)
8463 putc ('\n', stderr);
8464 noninteractive_need_newline = 0;
8465 vfprintf (stderr, m, ap);
8466 if (cursor_in_echo_area == 0)
8467 fprintf (stderr, "\n");
8468 fflush (stderr);
8469 }
8470 }
8471 else if (INTERACTIVE)
8472 {
8473 /* The frame whose mini-buffer we're going to display the message
8474 on. It may be larger than the selected frame, so we need to
8475 use its buffer, not the selected frame's buffer. */
8476 Lisp_Object mini_window;
8477 struct frame *f, *sf = SELECTED_FRAME ();
8478
8479 /* Get the frame containing the mini-buffer
8480 that the selected frame is using. */
8481 mini_window = FRAME_MINIBUF_WINDOW (sf);
8482 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8483
8484 /* A null message buffer means that the frame hasn't really been
8485 initialized yet. Error messages get reported properly by
8486 cmd_error, so this must be just an informative message; toss
8487 it. */
8488 if (FRAME_MESSAGE_BUF (f))
8489 {
8490 if (m)
8491 {
8492 ptrdiff_t len;
8493
8494 len = doprnt (FRAME_MESSAGE_BUF (f),
8495 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8496
8497 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8498 }
8499 else
8500 message1 (0);
8501
8502 /* Print should start at the beginning of the message
8503 buffer next time. */
8504 message_buf_print = 0;
8505 }
8506 }
8507 }
8508
8509 void
8510 message (const char *m, ...)
8511 {
8512 va_list ap;
8513 va_start (ap, m);
8514 vmessage (m, ap);
8515 va_end (ap);
8516 }
8517
8518
8519 #if 0
8520 /* The non-logging version of message. */
8521
8522 void
8523 message_nolog (const char *m, ...)
8524 {
8525 Lisp_Object old_log_max;
8526 va_list ap;
8527 va_start (ap, m);
8528 old_log_max = Vmessage_log_max;
8529 Vmessage_log_max = Qnil;
8530 vmessage (m, ap);
8531 Vmessage_log_max = old_log_max;
8532 va_end (ap);
8533 }
8534 #endif
8535
8536
8537 /* Display the current message in the current mini-buffer. This is
8538 only called from error handlers in process.c, and is not time
8539 critical. */
8540
8541 void
8542 update_echo_area (void)
8543 {
8544 if (!NILP (echo_area_buffer[0]))
8545 {
8546 Lisp_Object string;
8547 string = Fcurrent_message ();
8548 message3 (string, SBYTES (string),
8549 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
8550 }
8551 }
8552
8553
8554 /* Make sure echo area buffers in `echo_buffers' are live.
8555 If they aren't, make new ones. */
8556
8557 static void
8558 ensure_echo_area_buffers (void)
8559 {
8560 int i;
8561
8562 for (i = 0; i < 2; ++i)
8563 if (!BUFFERP (echo_buffer[i])
8564 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
8565 {
8566 char name[30];
8567 Lisp_Object old_buffer;
8568 int j;
8569
8570 old_buffer = echo_buffer[i];
8571 sprintf (name, " *Echo Area %d*", i);
8572 echo_buffer[i] = Fget_buffer_create (build_string (name));
8573 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
8574 /* to force word wrap in echo area -
8575 it was decided to postpone this*/
8576 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8577
8578 for (j = 0; j < 2; ++j)
8579 if (EQ (old_buffer, echo_area_buffer[j]))
8580 echo_area_buffer[j] = echo_buffer[i];
8581 }
8582 }
8583
8584
8585 /* Call FN with args A1..A4 with either the current or last displayed
8586 echo_area_buffer as current buffer.
8587
8588 WHICH zero means use the current message buffer
8589 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8590 from echo_buffer[] and clear it.
8591
8592 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8593 suitable buffer from echo_buffer[] and clear it.
8594
8595 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8596 that the current message becomes the last displayed one, make
8597 choose a suitable buffer for echo_area_buffer[0], and clear it.
8598
8599 Value is what FN returns. */
8600
8601 static int
8602 with_echo_area_buffer (struct window *w, int which,
8603 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8604 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8605 {
8606 Lisp_Object buffer;
8607 int this_one, the_other, clear_buffer_p, rc;
8608 int count = SPECPDL_INDEX ();
8609
8610 /* If buffers aren't live, make new ones. */
8611 ensure_echo_area_buffers ();
8612
8613 clear_buffer_p = 0;
8614
8615 if (which == 0)
8616 this_one = 0, the_other = 1;
8617 else if (which > 0)
8618 this_one = 1, the_other = 0;
8619 else
8620 {
8621 this_one = 0, the_other = 1;
8622 clear_buffer_p = 1;
8623
8624 /* We need a fresh one in case the current echo buffer equals
8625 the one containing the last displayed echo area message. */
8626 if (!NILP (echo_area_buffer[this_one])
8627 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8628 echo_area_buffer[this_one] = Qnil;
8629 }
8630
8631 /* Choose a suitable buffer from echo_buffer[] is we don't
8632 have one. */
8633 if (NILP (echo_area_buffer[this_one]))
8634 {
8635 echo_area_buffer[this_one]
8636 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8637 ? echo_buffer[the_other]
8638 : echo_buffer[this_one]);
8639 clear_buffer_p = 1;
8640 }
8641
8642 buffer = echo_area_buffer[this_one];
8643
8644 /* Don't get confused by reusing the buffer used for echoing
8645 for a different purpose. */
8646 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8647 cancel_echoing ();
8648
8649 record_unwind_protect (unwind_with_echo_area_buffer,
8650 with_echo_area_buffer_unwind_data (w));
8651
8652 /* Make the echo area buffer current. Note that for display
8653 purposes, it is not necessary that the displayed window's buffer
8654 == current_buffer, except for text property lookup. So, let's
8655 only set that buffer temporarily here without doing a full
8656 Fset_window_buffer. We must also change w->pointm, though,
8657 because otherwise an assertions in unshow_buffer fails, and Emacs
8658 aborts. */
8659 set_buffer_internal_1 (XBUFFER (buffer));
8660 if (w)
8661 {
8662 w->buffer = buffer;
8663 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8664 }
8665
8666 BVAR (current_buffer, undo_list) = Qt;
8667 BVAR (current_buffer, read_only) = Qnil;
8668 specbind (Qinhibit_read_only, Qt);
8669 specbind (Qinhibit_modification_hooks, Qt);
8670
8671 if (clear_buffer_p && Z > BEG)
8672 del_range (BEG, Z);
8673
8674 xassert (BEGV >= BEG);
8675 xassert (ZV <= Z && ZV >= BEGV);
8676
8677 rc = fn (a1, a2, a3, a4);
8678
8679 xassert (BEGV >= BEG);
8680 xassert (ZV <= Z && ZV >= BEGV);
8681
8682 unbind_to (count, Qnil);
8683 return rc;
8684 }
8685
8686
8687 /* Save state that should be preserved around the call to the function
8688 FN called in with_echo_area_buffer. */
8689
8690 static Lisp_Object
8691 with_echo_area_buffer_unwind_data (struct window *w)
8692 {
8693 int i = 0;
8694 Lisp_Object vector, tmp;
8695
8696 /* Reduce consing by keeping one vector in
8697 Vwith_echo_area_save_vector. */
8698 vector = Vwith_echo_area_save_vector;
8699 Vwith_echo_area_save_vector = Qnil;
8700
8701 if (NILP (vector))
8702 vector = Fmake_vector (make_number (7), Qnil);
8703
8704 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8705 ASET (vector, i, Vdeactivate_mark); ++i;
8706 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8707
8708 if (w)
8709 {
8710 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8711 ASET (vector, i, w->buffer); ++i;
8712 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8713 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8714 }
8715 else
8716 {
8717 int end = i + 4;
8718 for (; i < end; ++i)
8719 ASET (vector, i, Qnil);
8720 }
8721
8722 xassert (i == ASIZE (vector));
8723 return vector;
8724 }
8725
8726
8727 /* Restore global state from VECTOR which was created by
8728 with_echo_area_buffer_unwind_data. */
8729
8730 static Lisp_Object
8731 unwind_with_echo_area_buffer (Lisp_Object vector)
8732 {
8733 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8734 Vdeactivate_mark = AREF (vector, 1);
8735 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8736
8737 if (WINDOWP (AREF (vector, 3)))
8738 {
8739 struct window *w;
8740 Lisp_Object buffer, charpos, bytepos;
8741
8742 w = XWINDOW (AREF (vector, 3));
8743 buffer = AREF (vector, 4);
8744 charpos = AREF (vector, 5);
8745 bytepos = AREF (vector, 6);
8746
8747 w->buffer = buffer;
8748 set_marker_both (w->pointm, buffer,
8749 XFASTINT (charpos), XFASTINT (bytepos));
8750 }
8751
8752 Vwith_echo_area_save_vector = vector;
8753 return Qnil;
8754 }
8755
8756
8757 /* Set up the echo area for use by print functions. MULTIBYTE_P
8758 non-zero means we will print multibyte. */
8759
8760 void
8761 setup_echo_area_for_printing (int multibyte_p)
8762 {
8763 /* If we can't find an echo area any more, exit. */
8764 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8765 Fkill_emacs (Qnil);
8766
8767 ensure_echo_area_buffers ();
8768
8769 if (!message_buf_print)
8770 {
8771 /* A message has been output since the last time we printed.
8772 Choose a fresh echo area buffer. */
8773 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8774 echo_area_buffer[0] = echo_buffer[1];
8775 else
8776 echo_area_buffer[0] = echo_buffer[0];
8777
8778 /* Switch to that buffer and clear it. */
8779 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8780 BVAR (current_buffer, truncate_lines) = Qnil;
8781
8782 if (Z > BEG)
8783 {
8784 int count = SPECPDL_INDEX ();
8785 specbind (Qinhibit_read_only, Qt);
8786 /* Note that undo recording is always disabled. */
8787 del_range (BEG, Z);
8788 unbind_to (count, Qnil);
8789 }
8790 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8791
8792 /* Set up the buffer for the multibyteness we need. */
8793 if (multibyte_p
8794 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
8795 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8796
8797 /* Raise the frame containing the echo area. */
8798 if (minibuffer_auto_raise)
8799 {
8800 struct frame *sf = SELECTED_FRAME ();
8801 Lisp_Object mini_window;
8802 mini_window = FRAME_MINIBUF_WINDOW (sf);
8803 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8804 }
8805
8806 message_log_maybe_newline ();
8807 message_buf_print = 1;
8808 }
8809 else
8810 {
8811 if (NILP (echo_area_buffer[0]))
8812 {
8813 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8814 echo_area_buffer[0] = echo_buffer[1];
8815 else
8816 echo_area_buffer[0] = echo_buffer[0];
8817 }
8818
8819 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8820 {
8821 /* Someone switched buffers between print requests. */
8822 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8823 BVAR (current_buffer, truncate_lines) = Qnil;
8824 }
8825 }
8826 }
8827
8828
8829 /* Display an echo area message in window W. Value is non-zero if W's
8830 height is changed. If display_last_displayed_message_p is
8831 non-zero, display the message that was last displayed, otherwise
8832 display the current message. */
8833
8834 static int
8835 display_echo_area (struct window *w)
8836 {
8837 int i, no_message_p, window_height_changed_p, count;
8838
8839 /* Temporarily disable garbage collections while displaying the echo
8840 area. This is done because a GC can print a message itself.
8841 That message would modify the echo area buffer's contents while a
8842 redisplay of the buffer is going on, and seriously confuse
8843 redisplay. */
8844 count = inhibit_garbage_collection ();
8845
8846 /* If there is no message, we must call display_echo_area_1
8847 nevertheless because it resizes the window. But we will have to
8848 reset the echo_area_buffer in question to nil at the end because
8849 with_echo_area_buffer will sets it to an empty buffer. */
8850 i = display_last_displayed_message_p ? 1 : 0;
8851 no_message_p = NILP (echo_area_buffer[i]);
8852
8853 window_height_changed_p
8854 = with_echo_area_buffer (w, display_last_displayed_message_p,
8855 display_echo_area_1,
8856 (intptr_t) w, Qnil, 0, 0);
8857
8858 if (no_message_p)
8859 echo_area_buffer[i] = Qnil;
8860
8861 unbind_to (count, Qnil);
8862 return window_height_changed_p;
8863 }
8864
8865
8866 /* Helper for display_echo_area. Display the current buffer which
8867 contains the current echo area message in window W, a mini-window,
8868 a pointer to which is passed in A1. A2..A4 are currently not used.
8869 Change the height of W so that all of the message is displayed.
8870 Value is non-zero if height of W was changed. */
8871
8872 static int
8873 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8874 {
8875 intptr_t i1 = a1;
8876 struct window *w = (struct window *) i1;
8877 Lisp_Object window;
8878 struct text_pos start;
8879 int window_height_changed_p = 0;
8880
8881 /* Do this before displaying, so that we have a large enough glyph
8882 matrix for the display. If we can't get enough space for the
8883 whole text, display the last N lines. That works by setting w->start. */
8884 window_height_changed_p = resize_mini_window (w, 0);
8885
8886 /* Use the starting position chosen by resize_mini_window. */
8887 SET_TEXT_POS_FROM_MARKER (start, w->start);
8888
8889 /* Display. */
8890 clear_glyph_matrix (w->desired_matrix);
8891 XSETWINDOW (window, w);
8892 try_window (window, start, 0);
8893
8894 return window_height_changed_p;
8895 }
8896
8897
8898 /* Resize the echo area window to exactly the size needed for the
8899 currently displayed message, if there is one. If a mini-buffer
8900 is active, don't shrink it. */
8901
8902 void
8903 resize_echo_area_exactly (void)
8904 {
8905 if (BUFFERP (echo_area_buffer[0])
8906 && WINDOWP (echo_area_window))
8907 {
8908 struct window *w = XWINDOW (echo_area_window);
8909 int resized_p;
8910 Lisp_Object resize_exactly;
8911
8912 if (minibuf_level == 0)
8913 resize_exactly = Qt;
8914 else
8915 resize_exactly = Qnil;
8916
8917 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8918 (intptr_t) w, resize_exactly,
8919 0, 0);
8920 if (resized_p)
8921 {
8922 ++windows_or_buffers_changed;
8923 ++update_mode_lines;
8924 redisplay_internal ();
8925 }
8926 }
8927 }
8928
8929
8930 /* Callback function for with_echo_area_buffer, when used from
8931 resize_echo_area_exactly. A1 contains a pointer to the window to
8932 resize, EXACTLY non-nil means resize the mini-window exactly to the
8933 size of the text displayed. A3 and A4 are not used. Value is what
8934 resize_mini_window returns. */
8935
8936 static int
8937 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
8938 {
8939 intptr_t i1 = a1;
8940 return resize_mini_window ((struct window *) i1, !NILP (exactly));
8941 }
8942
8943
8944 /* Resize mini-window W to fit the size of its contents. EXACT_P
8945 means size the window exactly to the size needed. Otherwise, it's
8946 only enlarged until W's buffer is empty.
8947
8948 Set W->start to the right place to begin display. If the whole
8949 contents fit, start at the beginning. Otherwise, start so as
8950 to make the end of the contents appear. This is particularly
8951 important for y-or-n-p, but seems desirable generally.
8952
8953 Value is non-zero if the window height has been changed. */
8954
8955 int
8956 resize_mini_window (struct window *w, int exact_p)
8957 {
8958 struct frame *f = XFRAME (w->frame);
8959 int window_height_changed_p = 0;
8960
8961 xassert (MINI_WINDOW_P (w));
8962
8963 /* By default, start display at the beginning. */
8964 set_marker_both (w->start, w->buffer,
8965 BUF_BEGV (XBUFFER (w->buffer)),
8966 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8967
8968 /* Don't resize windows while redisplaying a window; it would
8969 confuse redisplay functions when the size of the window they are
8970 displaying changes from under them. Such a resizing can happen,
8971 for instance, when which-func prints a long message while
8972 we are running fontification-functions. We're running these
8973 functions with safe_call which binds inhibit-redisplay to t. */
8974 if (!NILP (Vinhibit_redisplay))
8975 return 0;
8976
8977 /* Nil means don't try to resize. */
8978 if (NILP (Vresize_mini_windows)
8979 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8980 return 0;
8981
8982 if (!FRAME_MINIBUF_ONLY_P (f))
8983 {
8984 struct it it;
8985 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8986 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8987 int height, max_height;
8988 int unit = FRAME_LINE_HEIGHT (f);
8989 struct text_pos start;
8990 struct buffer *old_current_buffer = NULL;
8991
8992 if (current_buffer != XBUFFER (w->buffer))
8993 {
8994 old_current_buffer = current_buffer;
8995 set_buffer_internal (XBUFFER (w->buffer));
8996 }
8997
8998 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8999
9000 /* Compute the max. number of lines specified by the user. */
9001 if (FLOATP (Vmax_mini_window_height))
9002 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9003 else if (INTEGERP (Vmax_mini_window_height))
9004 max_height = XINT (Vmax_mini_window_height);
9005 else
9006 max_height = total_height / 4;
9007
9008 /* Correct that max. height if it's bogus. */
9009 max_height = max (1, max_height);
9010 max_height = min (total_height, max_height);
9011
9012 /* Find out the height of the text in the window. */
9013 if (it.line_wrap == TRUNCATE)
9014 height = 1;
9015 else
9016 {
9017 last_height = 0;
9018 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9019 if (it.max_ascent == 0 && it.max_descent == 0)
9020 height = it.current_y + last_height;
9021 else
9022 height = it.current_y + it.max_ascent + it.max_descent;
9023 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9024 height = (height + unit - 1) / unit;
9025 }
9026
9027 /* Compute a suitable window start. */
9028 if (height > max_height)
9029 {
9030 height = max_height;
9031 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9032 move_it_vertically_backward (&it, (height - 1) * unit);
9033 start = it.current.pos;
9034 }
9035 else
9036 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9037 SET_MARKER_FROM_TEXT_POS (w->start, start);
9038
9039 if (EQ (Vresize_mini_windows, Qgrow_only))
9040 {
9041 /* Let it grow only, until we display an empty message, in which
9042 case the window shrinks again. */
9043 if (height > WINDOW_TOTAL_LINES (w))
9044 {
9045 int old_height = WINDOW_TOTAL_LINES (w);
9046 freeze_window_starts (f, 1);
9047 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9048 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9049 }
9050 else if (height < WINDOW_TOTAL_LINES (w)
9051 && (exact_p || BEGV == ZV))
9052 {
9053 int old_height = WINDOW_TOTAL_LINES (w);
9054 freeze_window_starts (f, 0);
9055 shrink_mini_window (w);
9056 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9057 }
9058 }
9059 else
9060 {
9061 /* Always resize to exact size needed. */
9062 if (height > WINDOW_TOTAL_LINES (w))
9063 {
9064 int old_height = WINDOW_TOTAL_LINES (w);
9065 freeze_window_starts (f, 1);
9066 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9067 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9068 }
9069 else if (height < WINDOW_TOTAL_LINES (w))
9070 {
9071 int old_height = WINDOW_TOTAL_LINES (w);
9072 freeze_window_starts (f, 0);
9073 shrink_mini_window (w);
9074
9075 if (height)
9076 {
9077 freeze_window_starts (f, 1);
9078 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9079 }
9080
9081 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9082 }
9083 }
9084
9085 if (old_current_buffer)
9086 set_buffer_internal (old_current_buffer);
9087 }
9088
9089 return window_height_changed_p;
9090 }
9091
9092
9093 /* Value is the current message, a string, or nil if there is no
9094 current message. */
9095
9096 Lisp_Object
9097 current_message (void)
9098 {
9099 Lisp_Object msg;
9100
9101 if (!BUFFERP (echo_area_buffer[0]))
9102 msg = Qnil;
9103 else
9104 {
9105 with_echo_area_buffer (0, 0, current_message_1,
9106 (intptr_t) &msg, Qnil, 0, 0);
9107 if (NILP (msg))
9108 echo_area_buffer[0] = Qnil;
9109 }
9110
9111 return msg;
9112 }
9113
9114
9115 static int
9116 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9117 {
9118 intptr_t i1 = a1;
9119 Lisp_Object *msg = (Lisp_Object *) i1;
9120
9121 if (Z > BEG)
9122 *msg = make_buffer_string (BEG, Z, 1);
9123 else
9124 *msg = Qnil;
9125 return 0;
9126 }
9127
9128
9129 /* Push the current message on Vmessage_stack for later restauration
9130 by restore_message. Value is non-zero if the current message isn't
9131 empty. This is a relatively infrequent operation, so it's not
9132 worth optimizing. */
9133
9134 int
9135 push_message (void)
9136 {
9137 Lisp_Object msg;
9138 msg = current_message ();
9139 Vmessage_stack = Fcons (msg, Vmessage_stack);
9140 return STRINGP (msg);
9141 }
9142
9143
9144 /* Restore message display from the top of Vmessage_stack. */
9145
9146 void
9147 restore_message (void)
9148 {
9149 Lisp_Object msg;
9150
9151 xassert (CONSP (Vmessage_stack));
9152 msg = XCAR (Vmessage_stack);
9153 if (STRINGP (msg))
9154 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9155 else
9156 message3_nolog (msg, 0, 0);
9157 }
9158
9159
9160 /* Handler for record_unwind_protect calling pop_message. */
9161
9162 Lisp_Object
9163 pop_message_unwind (Lisp_Object dummy)
9164 {
9165 pop_message ();
9166 return Qnil;
9167 }
9168
9169 /* Pop the top-most entry off Vmessage_stack. */
9170
9171 static void
9172 pop_message (void)
9173 {
9174 xassert (CONSP (Vmessage_stack));
9175 Vmessage_stack = XCDR (Vmessage_stack);
9176 }
9177
9178
9179 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9180 exits. If the stack is not empty, we have a missing pop_message
9181 somewhere. */
9182
9183 void
9184 check_message_stack (void)
9185 {
9186 if (!NILP (Vmessage_stack))
9187 abort ();
9188 }
9189
9190
9191 /* Truncate to NCHARS what will be displayed in the echo area the next
9192 time we display it---but don't redisplay it now. */
9193
9194 void
9195 truncate_echo_area (EMACS_INT nchars)
9196 {
9197 if (nchars == 0)
9198 echo_area_buffer[0] = Qnil;
9199 /* A null message buffer means that the frame hasn't really been
9200 initialized yet. Error messages get reported properly by
9201 cmd_error, so this must be just an informative message; toss it. */
9202 else if (!noninteractive
9203 && INTERACTIVE
9204 && !NILP (echo_area_buffer[0]))
9205 {
9206 struct frame *sf = SELECTED_FRAME ();
9207 if (FRAME_MESSAGE_BUF (sf))
9208 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9209 }
9210 }
9211
9212
9213 /* Helper function for truncate_echo_area. Truncate the current
9214 message to at most NCHARS characters. */
9215
9216 static int
9217 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9218 {
9219 if (BEG + nchars < Z)
9220 del_range (BEG + nchars, Z);
9221 if (Z == BEG)
9222 echo_area_buffer[0] = Qnil;
9223 return 0;
9224 }
9225
9226
9227 /* Set the current message to a substring of S or STRING.
9228
9229 If STRING is a Lisp string, set the message to the first NBYTES
9230 bytes from STRING. NBYTES zero means use the whole string. If
9231 STRING is multibyte, the message will be displayed multibyte.
9232
9233 If S is not null, set the message to the first LEN bytes of S. LEN
9234 zero means use the whole string. MULTIBYTE_P non-zero means S is
9235 multibyte. Display the message multibyte in that case.
9236
9237 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9238 to t before calling set_message_1 (which calls insert).
9239 */
9240
9241 static void
9242 set_message (const char *s, Lisp_Object string,
9243 EMACS_INT nbytes, int multibyte_p)
9244 {
9245 message_enable_multibyte
9246 = ((s && multibyte_p)
9247 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9248
9249 with_echo_area_buffer (0, -1, set_message_1,
9250 (intptr_t) s, string, nbytes, multibyte_p);
9251 message_buf_print = 0;
9252 help_echo_showing_p = 0;
9253 }
9254
9255
9256 /* Helper function for set_message. Arguments have the same meaning
9257 as there, with A1 corresponding to S and A2 corresponding to STRING
9258 This function is called with the echo area buffer being
9259 current. */
9260
9261 static int
9262 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9263 {
9264 intptr_t i1 = a1;
9265 const char *s = (const char *) i1;
9266 const unsigned char *msg = (const unsigned char *) s;
9267 Lisp_Object string = a2;
9268
9269 /* Change multibyteness of the echo buffer appropriately. */
9270 if (message_enable_multibyte
9271 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9272 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9273
9274 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9275 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9276 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9277
9278 /* Insert new message at BEG. */
9279 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9280
9281 if (STRINGP (string))
9282 {
9283 EMACS_INT nchars;
9284
9285 if (nbytes == 0)
9286 nbytes = SBYTES (string);
9287 nchars = string_byte_to_char (string, nbytes);
9288
9289 /* This function takes care of single/multibyte conversion. We
9290 just have to ensure that the echo area buffer has the right
9291 setting of enable_multibyte_characters. */
9292 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9293 }
9294 else if (s)
9295 {
9296 if (nbytes == 0)
9297 nbytes = strlen (s);
9298
9299 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9300 {
9301 /* Convert from multi-byte to single-byte. */
9302 EMACS_INT i;
9303 int c, n;
9304 char work[1];
9305
9306 /* Convert a multibyte string to single-byte. */
9307 for (i = 0; i < nbytes; i += n)
9308 {
9309 c = string_char_and_length (msg + i, &n);
9310 work[0] = (ASCII_CHAR_P (c)
9311 ? c
9312 : multibyte_char_to_unibyte (c));
9313 insert_1_both (work, 1, 1, 1, 0, 0);
9314 }
9315 }
9316 else if (!multibyte_p
9317 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9318 {
9319 /* Convert from single-byte to multi-byte. */
9320 EMACS_INT i;
9321 int c, n;
9322 unsigned char str[MAX_MULTIBYTE_LENGTH];
9323
9324 /* Convert a single-byte string to multibyte. */
9325 for (i = 0; i < nbytes; i++)
9326 {
9327 c = msg[i];
9328 MAKE_CHAR_MULTIBYTE (c);
9329 n = CHAR_STRING (c, str);
9330 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9331 }
9332 }
9333 else
9334 insert_1 (s, nbytes, 1, 0, 0);
9335 }
9336
9337 return 0;
9338 }
9339
9340
9341 /* Clear messages. CURRENT_P non-zero means clear the current
9342 message. LAST_DISPLAYED_P non-zero means clear the message
9343 last displayed. */
9344
9345 void
9346 clear_message (int current_p, int last_displayed_p)
9347 {
9348 if (current_p)
9349 {
9350 echo_area_buffer[0] = Qnil;
9351 message_cleared_p = 1;
9352 }
9353
9354 if (last_displayed_p)
9355 echo_area_buffer[1] = Qnil;
9356
9357 message_buf_print = 0;
9358 }
9359
9360 /* Clear garbaged frames.
9361
9362 This function is used where the old redisplay called
9363 redraw_garbaged_frames which in turn called redraw_frame which in
9364 turn called clear_frame. The call to clear_frame was a source of
9365 flickering. I believe a clear_frame is not necessary. It should
9366 suffice in the new redisplay to invalidate all current matrices,
9367 and ensure a complete redisplay of all windows. */
9368
9369 static void
9370 clear_garbaged_frames (void)
9371 {
9372 if (frame_garbaged)
9373 {
9374 Lisp_Object tail, frame;
9375 int changed_count = 0;
9376
9377 FOR_EACH_FRAME (tail, frame)
9378 {
9379 struct frame *f = XFRAME (frame);
9380
9381 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9382 {
9383 if (f->resized_p)
9384 {
9385 Fredraw_frame (frame);
9386 f->force_flush_display_p = 1;
9387 }
9388 clear_current_matrices (f);
9389 changed_count++;
9390 f->garbaged = 0;
9391 f->resized_p = 0;
9392 }
9393 }
9394
9395 frame_garbaged = 0;
9396 if (changed_count)
9397 ++windows_or_buffers_changed;
9398 }
9399 }
9400
9401
9402 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9403 is non-zero update selected_frame. Value is non-zero if the
9404 mini-windows height has been changed. */
9405
9406 static int
9407 echo_area_display (int update_frame_p)
9408 {
9409 Lisp_Object mini_window;
9410 struct window *w;
9411 struct frame *f;
9412 int window_height_changed_p = 0;
9413 struct frame *sf = SELECTED_FRAME ();
9414
9415 mini_window = FRAME_MINIBUF_WINDOW (sf);
9416 w = XWINDOW (mini_window);
9417 f = XFRAME (WINDOW_FRAME (w));
9418
9419 /* Don't display if frame is invisible or not yet initialized. */
9420 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9421 return 0;
9422
9423 #ifdef HAVE_WINDOW_SYSTEM
9424 /* When Emacs starts, selected_frame may be the initial terminal
9425 frame. If we let this through, a message would be displayed on
9426 the terminal. */
9427 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9428 return 0;
9429 #endif /* HAVE_WINDOW_SYSTEM */
9430
9431 /* Redraw garbaged frames. */
9432 if (frame_garbaged)
9433 clear_garbaged_frames ();
9434
9435 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9436 {
9437 echo_area_window = mini_window;
9438 window_height_changed_p = display_echo_area (w);
9439 w->must_be_updated_p = 1;
9440
9441 /* Update the display, unless called from redisplay_internal.
9442 Also don't update the screen during redisplay itself. The
9443 update will happen at the end of redisplay, and an update
9444 here could cause confusion. */
9445 if (update_frame_p && !redisplaying_p)
9446 {
9447 int n = 0;
9448
9449 /* If the display update has been interrupted by pending
9450 input, update mode lines in the frame. Due to the
9451 pending input, it might have been that redisplay hasn't
9452 been called, so that mode lines above the echo area are
9453 garbaged. This looks odd, so we prevent it here. */
9454 if (!display_completed)
9455 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9456
9457 if (window_height_changed_p
9458 /* Don't do this if Emacs is shutting down. Redisplay
9459 needs to run hooks. */
9460 && !NILP (Vrun_hooks))
9461 {
9462 /* Must update other windows. Likewise as in other
9463 cases, don't let this update be interrupted by
9464 pending input. */
9465 int count = SPECPDL_INDEX ();
9466 specbind (Qredisplay_dont_pause, Qt);
9467 windows_or_buffers_changed = 1;
9468 redisplay_internal ();
9469 unbind_to (count, Qnil);
9470 }
9471 else if (FRAME_WINDOW_P (f) && n == 0)
9472 {
9473 /* Window configuration is the same as before.
9474 Can do with a display update of the echo area,
9475 unless we displayed some mode lines. */
9476 update_single_window (w, 1);
9477 FRAME_RIF (f)->flush_display (f);
9478 }
9479 else
9480 update_frame (f, 1, 1);
9481
9482 /* If cursor is in the echo area, make sure that the next
9483 redisplay displays the minibuffer, so that the cursor will
9484 be replaced with what the minibuffer wants. */
9485 if (cursor_in_echo_area)
9486 ++windows_or_buffers_changed;
9487 }
9488 }
9489 else if (!EQ (mini_window, selected_window))
9490 windows_or_buffers_changed++;
9491
9492 /* Last displayed message is now the current message. */
9493 echo_area_buffer[1] = echo_area_buffer[0];
9494 /* Inform read_char that we're not echoing. */
9495 echo_message_buffer = Qnil;
9496
9497 /* Prevent redisplay optimization in redisplay_internal by resetting
9498 this_line_start_pos. This is done because the mini-buffer now
9499 displays the message instead of its buffer text. */
9500 if (EQ (mini_window, selected_window))
9501 CHARPOS (this_line_start_pos) = 0;
9502
9503 return window_height_changed_p;
9504 }
9505
9506
9507 \f
9508 /***********************************************************************
9509 Mode Lines and Frame Titles
9510 ***********************************************************************/
9511
9512 /* A buffer for constructing non-propertized mode-line strings and
9513 frame titles in it; allocated from the heap in init_xdisp and
9514 resized as needed in store_mode_line_noprop_char. */
9515
9516 static char *mode_line_noprop_buf;
9517
9518 /* The buffer's end, and a current output position in it. */
9519
9520 static char *mode_line_noprop_buf_end;
9521 static char *mode_line_noprop_ptr;
9522
9523 #define MODE_LINE_NOPROP_LEN(start) \
9524 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9525
9526 static enum {
9527 MODE_LINE_DISPLAY = 0,
9528 MODE_LINE_TITLE,
9529 MODE_LINE_NOPROP,
9530 MODE_LINE_STRING
9531 } mode_line_target;
9532
9533 /* Alist that caches the results of :propertize.
9534 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9535 static Lisp_Object mode_line_proptrans_alist;
9536
9537 /* List of strings making up the mode-line. */
9538 static Lisp_Object mode_line_string_list;
9539
9540 /* Base face property when building propertized mode line string. */
9541 static Lisp_Object mode_line_string_face;
9542 static Lisp_Object mode_line_string_face_prop;
9543
9544
9545 /* Unwind data for mode line strings */
9546
9547 static Lisp_Object Vmode_line_unwind_vector;
9548
9549 static Lisp_Object
9550 format_mode_line_unwind_data (struct buffer *obuf,
9551 Lisp_Object owin,
9552 int save_proptrans)
9553 {
9554 Lisp_Object vector, tmp;
9555
9556 /* Reduce consing by keeping one vector in
9557 Vwith_echo_area_save_vector. */
9558 vector = Vmode_line_unwind_vector;
9559 Vmode_line_unwind_vector = Qnil;
9560
9561 if (NILP (vector))
9562 vector = Fmake_vector (make_number (8), Qnil);
9563
9564 ASET (vector, 0, make_number (mode_line_target));
9565 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9566 ASET (vector, 2, mode_line_string_list);
9567 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9568 ASET (vector, 4, mode_line_string_face);
9569 ASET (vector, 5, mode_line_string_face_prop);
9570
9571 if (obuf)
9572 XSETBUFFER (tmp, obuf);
9573 else
9574 tmp = Qnil;
9575 ASET (vector, 6, tmp);
9576 ASET (vector, 7, owin);
9577
9578 return vector;
9579 }
9580
9581 static Lisp_Object
9582 unwind_format_mode_line (Lisp_Object vector)
9583 {
9584 mode_line_target = XINT (AREF (vector, 0));
9585 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9586 mode_line_string_list = AREF (vector, 2);
9587 if (! EQ (AREF (vector, 3), Qt))
9588 mode_line_proptrans_alist = AREF (vector, 3);
9589 mode_line_string_face = AREF (vector, 4);
9590 mode_line_string_face_prop = AREF (vector, 5);
9591
9592 if (!NILP (AREF (vector, 7)))
9593 /* Select window before buffer, since it may change the buffer. */
9594 Fselect_window (AREF (vector, 7), Qt);
9595
9596 if (!NILP (AREF (vector, 6)))
9597 {
9598 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9599 ASET (vector, 6, Qnil);
9600 }
9601
9602 Vmode_line_unwind_vector = vector;
9603 return Qnil;
9604 }
9605
9606
9607 /* Store a single character C for the frame title in mode_line_noprop_buf.
9608 Re-allocate mode_line_noprop_buf if necessary. */
9609
9610 static void
9611 store_mode_line_noprop_char (char c)
9612 {
9613 /* If output position has reached the end of the allocated buffer,
9614 double the buffer's size. */
9615 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9616 {
9617 int len = MODE_LINE_NOPROP_LEN (0);
9618 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9619 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9620 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9621 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9622 }
9623
9624 *mode_line_noprop_ptr++ = c;
9625 }
9626
9627
9628 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9629 mode_line_noprop_ptr. STRING is the string to store. Do not copy
9630 characters that yield more columns than PRECISION; PRECISION <= 0
9631 means copy the whole string. Pad with spaces until FIELD_WIDTH
9632 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9633 pad. Called from display_mode_element when it is used to build a
9634 frame title. */
9635
9636 static int
9637 store_mode_line_noprop (const char *string, int field_width, int precision)
9638 {
9639 const unsigned char *str = (const unsigned char *) string;
9640 int n = 0;
9641 EMACS_INT dummy, nbytes;
9642
9643 /* Copy at most PRECISION chars from STR. */
9644 nbytes = strlen (string);
9645 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9646 while (nbytes--)
9647 store_mode_line_noprop_char (*str++);
9648
9649 /* Fill up with spaces until FIELD_WIDTH reached. */
9650 while (field_width > 0
9651 && n < field_width)
9652 {
9653 store_mode_line_noprop_char (' ');
9654 ++n;
9655 }
9656
9657 return n;
9658 }
9659
9660 /***********************************************************************
9661 Frame Titles
9662 ***********************************************************************/
9663
9664 #ifdef HAVE_WINDOW_SYSTEM
9665
9666 /* Set the title of FRAME, if it has changed. The title format is
9667 Vicon_title_format if FRAME is iconified, otherwise it is
9668 frame_title_format. */
9669
9670 static void
9671 x_consider_frame_title (Lisp_Object frame)
9672 {
9673 struct frame *f = XFRAME (frame);
9674
9675 if (FRAME_WINDOW_P (f)
9676 || FRAME_MINIBUF_ONLY_P (f)
9677 || f->explicit_name)
9678 {
9679 /* Do we have more than one visible frame on this X display? */
9680 Lisp_Object tail;
9681 Lisp_Object fmt;
9682 int title_start;
9683 char *title;
9684 int len;
9685 struct it it;
9686 int count = SPECPDL_INDEX ();
9687
9688 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9689 {
9690 Lisp_Object other_frame = XCAR (tail);
9691 struct frame *tf = XFRAME (other_frame);
9692
9693 if (tf != f
9694 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9695 && !FRAME_MINIBUF_ONLY_P (tf)
9696 && !EQ (other_frame, tip_frame)
9697 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9698 break;
9699 }
9700
9701 /* Set global variable indicating that multiple frames exist. */
9702 multiple_frames = CONSP (tail);
9703
9704 /* Switch to the buffer of selected window of the frame. Set up
9705 mode_line_target so that display_mode_element will output into
9706 mode_line_noprop_buf; then display the title. */
9707 record_unwind_protect (unwind_format_mode_line,
9708 format_mode_line_unwind_data
9709 (current_buffer, selected_window, 0));
9710
9711 Fselect_window (f->selected_window, Qt);
9712 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9713 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9714
9715 mode_line_target = MODE_LINE_TITLE;
9716 title_start = MODE_LINE_NOPROP_LEN (0);
9717 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9718 NULL, DEFAULT_FACE_ID);
9719 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9720 len = MODE_LINE_NOPROP_LEN (title_start);
9721 title = mode_line_noprop_buf + title_start;
9722 unbind_to (count, Qnil);
9723
9724 /* Set the title only if it's changed. This avoids consing in
9725 the common case where it hasn't. (If it turns out that we've
9726 already wasted too much time by walking through the list with
9727 display_mode_element, then we might need to optimize at a
9728 higher level than this.) */
9729 if (! STRINGP (f->name)
9730 || SBYTES (f->name) != len
9731 || memcmp (title, SDATA (f->name), len) != 0)
9732 x_implicitly_set_name (f, make_string (title, len), Qnil);
9733 }
9734 }
9735
9736 #endif /* not HAVE_WINDOW_SYSTEM */
9737
9738
9739
9740 \f
9741 /***********************************************************************
9742 Menu Bars
9743 ***********************************************************************/
9744
9745
9746 /* Prepare for redisplay by updating menu-bar item lists when
9747 appropriate. This can call eval. */
9748
9749 void
9750 prepare_menu_bars (void)
9751 {
9752 int all_windows;
9753 struct gcpro gcpro1, gcpro2;
9754 struct frame *f;
9755 Lisp_Object tooltip_frame;
9756
9757 #ifdef HAVE_WINDOW_SYSTEM
9758 tooltip_frame = tip_frame;
9759 #else
9760 tooltip_frame = Qnil;
9761 #endif
9762
9763 /* Update all frame titles based on their buffer names, etc. We do
9764 this before the menu bars so that the buffer-menu will show the
9765 up-to-date frame titles. */
9766 #ifdef HAVE_WINDOW_SYSTEM
9767 if (windows_or_buffers_changed || update_mode_lines)
9768 {
9769 Lisp_Object tail, frame;
9770
9771 FOR_EACH_FRAME (tail, frame)
9772 {
9773 f = XFRAME (frame);
9774 if (!EQ (frame, tooltip_frame)
9775 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9776 x_consider_frame_title (frame);
9777 }
9778 }
9779 #endif /* HAVE_WINDOW_SYSTEM */
9780
9781 /* Update the menu bar item lists, if appropriate. This has to be
9782 done before any actual redisplay or generation of display lines. */
9783 all_windows = (update_mode_lines
9784 || buffer_shared > 1
9785 || windows_or_buffers_changed);
9786 if (all_windows)
9787 {
9788 Lisp_Object tail, frame;
9789 int count = SPECPDL_INDEX ();
9790 /* 1 means that update_menu_bar has run its hooks
9791 so any further calls to update_menu_bar shouldn't do so again. */
9792 int menu_bar_hooks_run = 0;
9793
9794 record_unwind_save_match_data ();
9795
9796 FOR_EACH_FRAME (tail, frame)
9797 {
9798 f = XFRAME (frame);
9799
9800 /* Ignore tooltip frame. */
9801 if (EQ (frame, tooltip_frame))
9802 continue;
9803
9804 /* If a window on this frame changed size, report that to
9805 the user and clear the size-change flag. */
9806 if (FRAME_WINDOW_SIZES_CHANGED (f))
9807 {
9808 Lisp_Object functions;
9809
9810 /* Clear flag first in case we get an error below. */
9811 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9812 functions = Vwindow_size_change_functions;
9813 GCPRO2 (tail, functions);
9814
9815 while (CONSP (functions))
9816 {
9817 if (!EQ (XCAR (functions), Qt))
9818 call1 (XCAR (functions), frame);
9819 functions = XCDR (functions);
9820 }
9821 UNGCPRO;
9822 }
9823
9824 GCPRO1 (tail);
9825 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9826 #ifdef HAVE_WINDOW_SYSTEM
9827 update_tool_bar (f, 0);
9828 #endif
9829 #ifdef HAVE_NS
9830 if (windows_or_buffers_changed
9831 && FRAME_NS_P (f))
9832 ns_set_doc_edited (f, Fbuffer_modified_p
9833 (XWINDOW (f->selected_window)->buffer));
9834 #endif
9835 UNGCPRO;
9836 }
9837
9838 unbind_to (count, Qnil);
9839 }
9840 else
9841 {
9842 struct frame *sf = SELECTED_FRAME ();
9843 update_menu_bar (sf, 1, 0);
9844 #ifdef HAVE_WINDOW_SYSTEM
9845 update_tool_bar (sf, 1);
9846 #endif
9847 }
9848 }
9849
9850
9851 /* Update the menu bar item list for frame F. This has to be done
9852 before we start to fill in any display lines, because it can call
9853 eval.
9854
9855 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9856
9857 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9858 already ran the menu bar hooks for this redisplay, so there
9859 is no need to run them again. The return value is the
9860 updated value of this flag, to pass to the next call. */
9861
9862 static int
9863 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9864 {
9865 Lisp_Object window;
9866 register struct window *w;
9867
9868 /* If called recursively during a menu update, do nothing. This can
9869 happen when, for instance, an activate-menubar-hook causes a
9870 redisplay. */
9871 if (inhibit_menubar_update)
9872 return hooks_run;
9873
9874 window = FRAME_SELECTED_WINDOW (f);
9875 w = XWINDOW (window);
9876
9877 if (FRAME_WINDOW_P (f)
9878 ?
9879 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9880 || defined (HAVE_NS) || defined (USE_GTK)
9881 FRAME_EXTERNAL_MENU_BAR (f)
9882 #else
9883 FRAME_MENU_BAR_LINES (f) > 0
9884 #endif
9885 : FRAME_MENU_BAR_LINES (f) > 0)
9886 {
9887 /* If the user has switched buffers or windows, we need to
9888 recompute to reflect the new bindings. But we'll
9889 recompute when update_mode_lines is set too; that means
9890 that people can use force-mode-line-update to request
9891 that the menu bar be recomputed. The adverse effect on
9892 the rest of the redisplay algorithm is about the same as
9893 windows_or_buffers_changed anyway. */
9894 if (windows_or_buffers_changed
9895 /* This used to test w->update_mode_line, but we believe
9896 there is no need to recompute the menu in that case. */
9897 || update_mode_lines
9898 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9899 < BUF_MODIFF (XBUFFER (w->buffer)))
9900 != !NILP (w->last_had_star))
9901 || ((!NILP (Vtransient_mark_mode)
9902 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
9903 != !NILP (w->region_showing)))
9904 {
9905 struct buffer *prev = current_buffer;
9906 int count = SPECPDL_INDEX ();
9907
9908 specbind (Qinhibit_menubar_update, Qt);
9909
9910 set_buffer_internal_1 (XBUFFER (w->buffer));
9911 if (save_match_data)
9912 record_unwind_save_match_data ();
9913 if (NILP (Voverriding_local_map_menu_flag))
9914 {
9915 specbind (Qoverriding_terminal_local_map, Qnil);
9916 specbind (Qoverriding_local_map, Qnil);
9917 }
9918
9919 if (!hooks_run)
9920 {
9921 /* Run the Lucid hook. */
9922 safe_run_hooks (Qactivate_menubar_hook);
9923
9924 /* If it has changed current-menubar from previous value,
9925 really recompute the menu-bar from the value. */
9926 if (! NILP (Vlucid_menu_bar_dirty_flag))
9927 call0 (Qrecompute_lucid_menubar);
9928
9929 safe_run_hooks (Qmenu_bar_update_hook);
9930
9931 hooks_run = 1;
9932 }
9933
9934 XSETFRAME (Vmenu_updating_frame, f);
9935 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9936
9937 /* Redisplay the menu bar in case we changed it. */
9938 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9939 || defined (HAVE_NS) || defined (USE_GTK)
9940 if (FRAME_WINDOW_P (f))
9941 {
9942 #if defined (HAVE_NS)
9943 /* All frames on Mac OS share the same menubar. So only
9944 the selected frame should be allowed to set it. */
9945 if (f == SELECTED_FRAME ())
9946 #endif
9947 set_frame_menubar (f, 0, 0);
9948 }
9949 else
9950 /* On a terminal screen, the menu bar is an ordinary screen
9951 line, and this makes it get updated. */
9952 w->update_mode_line = Qt;
9953 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9954 /* In the non-toolkit version, the menu bar is an ordinary screen
9955 line, and this makes it get updated. */
9956 w->update_mode_line = Qt;
9957 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9958
9959 unbind_to (count, Qnil);
9960 set_buffer_internal_1 (prev);
9961 }
9962 }
9963
9964 return hooks_run;
9965 }
9966
9967
9968 \f
9969 /***********************************************************************
9970 Output Cursor
9971 ***********************************************************************/
9972
9973 #ifdef HAVE_WINDOW_SYSTEM
9974
9975 /* EXPORT:
9976 Nominal cursor position -- where to draw output.
9977 HPOS and VPOS are window relative glyph matrix coordinates.
9978 X and Y are window relative pixel coordinates. */
9979
9980 struct cursor_pos output_cursor;
9981
9982
9983 /* EXPORT:
9984 Set the global variable output_cursor to CURSOR. All cursor
9985 positions are relative to updated_window. */
9986
9987 void
9988 set_output_cursor (struct cursor_pos *cursor)
9989 {
9990 output_cursor.hpos = cursor->hpos;
9991 output_cursor.vpos = cursor->vpos;
9992 output_cursor.x = cursor->x;
9993 output_cursor.y = cursor->y;
9994 }
9995
9996
9997 /* EXPORT for RIF:
9998 Set a nominal cursor position.
9999
10000 HPOS and VPOS are column/row positions in a window glyph matrix. X
10001 and Y are window text area relative pixel positions.
10002
10003 If this is done during an update, updated_window will contain the
10004 window that is being updated and the position is the future output
10005 cursor position for that window. If updated_window is null, use
10006 selected_window and display the cursor at the given position. */
10007
10008 void
10009 x_cursor_to (int vpos, int hpos, int y, int x)
10010 {
10011 struct window *w;
10012
10013 /* If updated_window is not set, work on selected_window. */
10014 if (updated_window)
10015 w = updated_window;
10016 else
10017 w = XWINDOW (selected_window);
10018
10019 /* Set the output cursor. */
10020 output_cursor.hpos = hpos;
10021 output_cursor.vpos = vpos;
10022 output_cursor.x = x;
10023 output_cursor.y = y;
10024
10025 /* If not called as part of an update, really display the cursor.
10026 This will also set the cursor position of W. */
10027 if (updated_window == NULL)
10028 {
10029 BLOCK_INPUT;
10030 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10031 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10032 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10033 UNBLOCK_INPUT;
10034 }
10035 }
10036
10037 #endif /* HAVE_WINDOW_SYSTEM */
10038
10039 \f
10040 /***********************************************************************
10041 Tool-bars
10042 ***********************************************************************/
10043
10044 #ifdef HAVE_WINDOW_SYSTEM
10045
10046 /* Where the mouse was last time we reported a mouse event. */
10047
10048 FRAME_PTR last_mouse_frame;
10049
10050 /* Tool-bar item index of the item on which a mouse button was pressed
10051 or -1. */
10052
10053 int last_tool_bar_item;
10054
10055
10056 static Lisp_Object
10057 update_tool_bar_unwind (Lisp_Object frame)
10058 {
10059 selected_frame = frame;
10060 return Qnil;
10061 }
10062
10063 /* Update the tool-bar item list for frame F. This has to be done
10064 before we start to fill in any display lines. Called from
10065 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10066 and restore it here. */
10067
10068 static void
10069 update_tool_bar (struct frame *f, int save_match_data)
10070 {
10071 #if defined (USE_GTK) || defined (HAVE_NS)
10072 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10073 #else
10074 int do_update = WINDOWP (f->tool_bar_window)
10075 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10076 #endif
10077
10078 if (do_update)
10079 {
10080 Lisp_Object window;
10081 struct window *w;
10082
10083 window = FRAME_SELECTED_WINDOW (f);
10084 w = XWINDOW (window);
10085
10086 /* If the user has switched buffers or windows, we need to
10087 recompute to reflect the new bindings. But we'll
10088 recompute when update_mode_lines is set too; that means
10089 that people can use force-mode-line-update to request
10090 that the menu bar be recomputed. The adverse effect on
10091 the rest of the redisplay algorithm is about the same as
10092 windows_or_buffers_changed anyway. */
10093 if (windows_or_buffers_changed
10094 || !NILP (w->update_mode_line)
10095 || update_mode_lines
10096 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10097 < BUF_MODIFF (XBUFFER (w->buffer)))
10098 != !NILP (w->last_had_star))
10099 || ((!NILP (Vtransient_mark_mode)
10100 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10101 != !NILP (w->region_showing)))
10102 {
10103 struct buffer *prev = current_buffer;
10104 int count = SPECPDL_INDEX ();
10105 Lisp_Object frame, new_tool_bar;
10106 int new_n_tool_bar;
10107 struct gcpro gcpro1;
10108
10109 /* Set current_buffer to the buffer of the selected
10110 window of the frame, so that we get the right local
10111 keymaps. */
10112 set_buffer_internal_1 (XBUFFER (w->buffer));
10113
10114 /* Save match data, if we must. */
10115 if (save_match_data)
10116 record_unwind_save_match_data ();
10117
10118 /* Make sure that we don't accidentally use bogus keymaps. */
10119 if (NILP (Voverriding_local_map_menu_flag))
10120 {
10121 specbind (Qoverriding_terminal_local_map, Qnil);
10122 specbind (Qoverriding_local_map, Qnil);
10123 }
10124
10125 GCPRO1 (new_tool_bar);
10126
10127 /* We must temporarily set the selected frame to this frame
10128 before calling tool_bar_items, because the calculation of
10129 the tool-bar keymap uses the selected frame (see
10130 `tool-bar-make-keymap' in tool-bar.el). */
10131 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10132 XSETFRAME (frame, f);
10133 selected_frame = frame;
10134
10135 /* Build desired tool-bar items from keymaps. */
10136 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10137 &new_n_tool_bar);
10138
10139 /* Redisplay the tool-bar if we changed it. */
10140 if (new_n_tool_bar != f->n_tool_bar_items
10141 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10142 {
10143 /* Redisplay that happens asynchronously due to an expose event
10144 may access f->tool_bar_items. Make sure we update both
10145 variables within BLOCK_INPUT so no such event interrupts. */
10146 BLOCK_INPUT;
10147 f->tool_bar_items = new_tool_bar;
10148 f->n_tool_bar_items = new_n_tool_bar;
10149 w->update_mode_line = Qt;
10150 UNBLOCK_INPUT;
10151 }
10152
10153 UNGCPRO;
10154
10155 unbind_to (count, Qnil);
10156 set_buffer_internal_1 (prev);
10157 }
10158 }
10159 }
10160
10161
10162 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10163 F's desired tool-bar contents. F->tool_bar_items must have
10164 been set up previously by calling prepare_menu_bars. */
10165
10166 static void
10167 build_desired_tool_bar_string (struct frame *f)
10168 {
10169 int i, size, size_needed;
10170 struct gcpro gcpro1, gcpro2, gcpro3;
10171 Lisp_Object image, plist, props;
10172
10173 image = plist = props = Qnil;
10174 GCPRO3 (image, plist, props);
10175
10176 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10177 Otherwise, make a new string. */
10178
10179 /* The size of the string we might be able to reuse. */
10180 size = (STRINGP (f->desired_tool_bar_string)
10181 ? SCHARS (f->desired_tool_bar_string)
10182 : 0);
10183
10184 /* We need one space in the string for each image. */
10185 size_needed = f->n_tool_bar_items;
10186
10187 /* Reuse f->desired_tool_bar_string, if possible. */
10188 if (size < size_needed || NILP (f->desired_tool_bar_string))
10189 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10190 make_number (' '));
10191 else
10192 {
10193 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10194 Fremove_text_properties (make_number (0), make_number (size),
10195 props, f->desired_tool_bar_string);
10196 }
10197
10198 /* Put a `display' property on the string for the images to display,
10199 put a `menu_item' property on tool-bar items with a value that
10200 is the index of the item in F's tool-bar item vector. */
10201 for (i = 0; i < f->n_tool_bar_items; ++i)
10202 {
10203 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10204
10205 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10206 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10207 int hmargin, vmargin, relief, idx, end;
10208
10209 /* If image is a vector, choose the image according to the
10210 button state. */
10211 image = PROP (TOOL_BAR_ITEM_IMAGES);
10212 if (VECTORP (image))
10213 {
10214 if (enabled_p)
10215 idx = (selected_p
10216 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10217 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10218 else
10219 idx = (selected_p
10220 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10221 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10222
10223 xassert (ASIZE (image) >= idx);
10224 image = AREF (image, idx);
10225 }
10226 else
10227 idx = -1;
10228
10229 /* Ignore invalid image specifications. */
10230 if (!valid_image_p (image))
10231 continue;
10232
10233 /* Display the tool-bar button pressed, or depressed. */
10234 plist = Fcopy_sequence (XCDR (image));
10235
10236 /* Compute margin and relief to draw. */
10237 relief = (tool_bar_button_relief >= 0
10238 ? tool_bar_button_relief
10239 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10240 hmargin = vmargin = relief;
10241
10242 if (INTEGERP (Vtool_bar_button_margin)
10243 && XINT (Vtool_bar_button_margin) > 0)
10244 {
10245 hmargin += XFASTINT (Vtool_bar_button_margin);
10246 vmargin += XFASTINT (Vtool_bar_button_margin);
10247 }
10248 else if (CONSP (Vtool_bar_button_margin))
10249 {
10250 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10251 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10252 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10253
10254 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10255 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10256 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10257 }
10258
10259 if (auto_raise_tool_bar_buttons_p)
10260 {
10261 /* Add a `:relief' property to the image spec if the item is
10262 selected. */
10263 if (selected_p)
10264 {
10265 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10266 hmargin -= relief;
10267 vmargin -= relief;
10268 }
10269 }
10270 else
10271 {
10272 /* If image is selected, display it pressed, i.e. with a
10273 negative relief. If it's not selected, display it with a
10274 raised relief. */
10275 plist = Fplist_put (plist, QCrelief,
10276 (selected_p
10277 ? make_number (-relief)
10278 : make_number (relief)));
10279 hmargin -= relief;
10280 vmargin -= relief;
10281 }
10282
10283 /* Put a margin around the image. */
10284 if (hmargin || vmargin)
10285 {
10286 if (hmargin == vmargin)
10287 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10288 else
10289 plist = Fplist_put (plist, QCmargin,
10290 Fcons (make_number (hmargin),
10291 make_number (vmargin)));
10292 }
10293
10294 /* If button is not enabled, and we don't have special images
10295 for the disabled state, make the image appear disabled by
10296 applying an appropriate algorithm to it. */
10297 if (!enabled_p && idx < 0)
10298 plist = Fplist_put (plist, QCconversion, Qdisabled);
10299
10300 /* Put a `display' text property on the string for the image to
10301 display. Put a `menu-item' property on the string that gives
10302 the start of this item's properties in the tool-bar items
10303 vector. */
10304 image = Fcons (Qimage, plist);
10305 props = list4 (Qdisplay, image,
10306 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10307
10308 /* Let the last image hide all remaining spaces in the tool bar
10309 string. The string can be longer than needed when we reuse a
10310 previous string. */
10311 if (i + 1 == f->n_tool_bar_items)
10312 end = SCHARS (f->desired_tool_bar_string);
10313 else
10314 end = i + 1;
10315 Fadd_text_properties (make_number (i), make_number (end),
10316 props, f->desired_tool_bar_string);
10317 #undef PROP
10318 }
10319
10320 UNGCPRO;
10321 }
10322
10323
10324 /* Display one line of the tool-bar of frame IT->f.
10325
10326 HEIGHT specifies the desired height of the tool-bar line.
10327 If the actual height of the glyph row is less than HEIGHT, the
10328 row's height is increased to HEIGHT, and the icons are centered
10329 vertically in the new height.
10330
10331 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10332 count a final empty row in case the tool-bar width exactly matches
10333 the window width.
10334 */
10335
10336 static void
10337 display_tool_bar_line (struct it *it, int height)
10338 {
10339 struct glyph_row *row = it->glyph_row;
10340 int max_x = it->last_visible_x;
10341 struct glyph *last;
10342
10343 prepare_desired_row (row);
10344 row->y = it->current_y;
10345
10346 /* Note that this isn't made use of if the face hasn't a box,
10347 so there's no need to check the face here. */
10348 it->start_of_box_run_p = 1;
10349
10350 while (it->current_x < max_x)
10351 {
10352 int x, n_glyphs_before, i, nglyphs;
10353 struct it it_before;
10354
10355 /* Get the next display element. */
10356 if (!get_next_display_element (it))
10357 {
10358 /* Don't count empty row if we are counting needed tool-bar lines. */
10359 if (height < 0 && !it->hpos)
10360 return;
10361 break;
10362 }
10363
10364 /* Produce glyphs. */
10365 n_glyphs_before = row->used[TEXT_AREA];
10366 it_before = *it;
10367
10368 PRODUCE_GLYPHS (it);
10369
10370 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10371 i = 0;
10372 x = it_before.current_x;
10373 while (i < nglyphs)
10374 {
10375 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10376
10377 if (x + glyph->pixel_width > max_x)
10378 {
10379 /* Glyph doesn't fit on line. Backtrack. */
10380 row->used[TEXT_AREA] = n_glyphs_before;
10381 *it = it_before;
10382 /* If this is the only glyph on this line, it will never fit on the
10383 tool-bar, so skip it. But ensure there is at least one glyph,
10384 so we don't accidentally disable the tool-bar. */
10385 if (n_glyphs_before == 0
10386 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10387 break;
10388 goto out;
10389 }
10390
10391 ++it->hpos;
10392 x += glyph->pixel_width;
10393 ++i;
10394 }
10395
10396 /* Stop at line ends. */
10397 if (ITERATOR_AT_END_OF_LINE_P (it))
10398 break;
10399
10400 set_iterator_to_next (it, 1);
10401 }
10402
10403 out:;
10404
10405 row->displays_text_p = row->used[TEXT_AREA] != 0;
10406
10407 /* Use default face for the border below the tool bar.
10408
10409 FIXME: When auto-resize-tool-bars is grow-only, there is
10410 no additional border below the possibly empty tool-bar lines.
10411 So to make the extra empty lines look "normal", we have to
10412 use the tool-bar face for the border too. */
10413 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10414 it->face_id = DEFAULT_FACE_ID;
10415
10416 extend_face_to_end_of_line (it);
10417 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10418 last->right_box_line_p = 1;
10419 if (last == row->glyphs[TEXT_AREA])
10420 last->left_box_line_p = 1;
10421
10422 /* Make line the desired height and center it vertically. */
10423 if ((height -= it->max_ascent + it->max_descent) > 0)
10424 {
10425 /* Don't add more than one line height. */
10426 height %= FRAME_LINE_HEIGHT (it->f);
10427 it->max_ascent += height / 2;
10428 it->max_descent += (height + 1) / 2;
10429 }
10430
10431 compute_line_metrics (it);
10432
10433 /* If line is empty, make it occupy the rest of the tool-bar. */
10434 if (!row->displays_text_p)
10435 {
10436 row->height = row->phys_height = it->last_visible_y - row->y;
10437 row->visible_height = row->height;
10438 row->ascent = row->phys_ascent = 0;
10439 row->extra_line_spacing = 0;
10440 }
10441
10442 row->full_width_p = 1;
10443 row->continued_p = 0;
10444 row->truncated_on_left_p = 0;
10445 row->truncated_on_right_p = 0;
10446
10447 it->current_x = it->hpos = 0;
10448 it->current_y += row->height;
10449 ++it->vpos;
10450 ++it->glyph_row;
10451 }
10452
10453
10454 /* Max tool-bar height. */
10455
10456 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10457 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10458
10459 /* Value is the number of screen lines needed to make all tool-bar
10460 items of frame F visible. The number of actual rows needed is
10461 returned in *N_ROWS if non-NULL. */
10462
10463 static int
10464 tool_bar_lines_needed (struct frame *f, int *n_rows)
10465 {
10466 struct window *w = XWINDOW (f->tool_bar_window);
10467 struct it it;
10468 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10469 the desired matrix, so use (unused) mode-line row as temporary row to
10470 avoid destroying the first tool-bar row. */
10471 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10472
10473 /* Initialize an iterator for iteration over
10474 F->desired_tool_bar_string in the tool-bar window of frame F. */
10475 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10476 it.first_visible_x = 0;
10477 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10478 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10479
10480 while (!ITERATOR_AT_END_P (&it))
10481 {
10482 clear_glyph_row (temp_row);
10483 it.glyph_row = temp_row;
10484 display_tool_bar_line (&it, -1);
10485 }
10486 clear_glyph_row (temp_row);
10487
10488 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10489 if (n_rows)
10490 *n_rows = it.vpos > 0 ? it.vpos : -1;
10491
10492 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10493 }
10494
10495
10496 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10497 0, 1, 0,
10498 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10499 (Lisp_Object frame)
10500 {
10501 struct frame *f;
10502 struct window *w;
10503 int nlines = 0;
10504
10505 if (NILP (frame))
10506 frame = selected_frame;
10507 else
10508 CHECK_FRAME (frame);
10509 f = XFRAME (frame);
10510
10511 if (WINDOWP (f->tool_bar_window)
10512 && (w = XWINDOW (f->tool_bar_window),
10513 WINDOW_TOTAL_LINES (w) > 0))
10514 {
10515 update_tool_bar (f, 1);
10516 if (f->n_tool_bar_items)
10517 {
10518 build_desired_tool_bar_string (f);
10519 nlines = tool_bar_lines_needed (f, NULL);
10520 }
10521 }
10522
10523 return make_number (nlines);
10524 }
10525
10526
10527 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10528 height should be changed. */
10529
10530 static int
10531 redisplay_tool_bar (struct frame *f)
10532 {
10533 struct window *w;
10534 struct it it;
10535 struct glyph_row *row;
10536
10537 #if defined (USE_GTK) || defined (HAVE_NS)
10538 if (FRAME_EXTERNAL_TOOL_BAR (f))
10539 update_frame_tool_bar (f);
10540 return 0;
10541 #endif
10542
10543 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10544 do anything. This means you must start with tool-bar-lines
10545 non-zero to get the auto-sizing effect. Or in other words, you
10546 can turn off tool-bars by specifying tool-bar-lines zero. */
10547 if (!WINDOWP (f->tool_bar_window)
10548 || (w = XWINDOW (f->tool_bar_window),
10549 WINDOW_TOTAL_LINES (w) == 0))
10550 return 0;
10551
10552 /* Set up an iterator for the tool-bar window. */
10553 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10554 it.first_visible_x = 0;
10555 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10556 row = it.glyph_row;
10557
10558 /* Build a string that represents the contents of the tool-bar. */
10559 build_desired_tool_bar_string (f);
10560 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10561
10562 if (f->n_tool_bar_rows == 0)
10563 {
10564 int nlines;
10565
10566 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10567 nlines != WINDOW_TOTAL_LINES (w)))
10568 {
10569 Lisp_Object frame;
10570 int old_height = WINDOW_TOTAL_LINES (w);
10571
10572 XSETFRAME (frame, f);
10573 Fmodify_frame_parameters (frame,
10574 Fcons (Fcons (Qtool_bar_lines,
10575 make_number (nlines)),
10576 Qnil));
10577 if (WINDOW_TOTAL_LINES (w) != old_height)
10578 {
10579 clear_glyph_matrix (w->desired_matrix);
10580 fonts_changed_p = 1;
10581 return 1;
10582 }
10583 }
10584 }
10585
10586 /* Display as many lines as needed to display all tool-bar items. */
10587
10588 if (f->n_tool_bar_rows > 0)
10589 {
10590 int border, rows, height, extra;
10591
10592 if (INTEGERP (Vtool_bar_border))
10593 border = XINT (Vtool_bar_border);
10594 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10595 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10596 else if (EQ (Vtool_bar_border, Qborder_width))
10597 border = f->border_width;
10598 else
10599 border = 0;
10600 if (border < 0)
10601 border = 0;
10602
10603 rows = f->n_tool_bar_rows;
10604 height = max (1, (it.last_visible_y - border) / rows);
10605 extra = it.last_visible_y - border - height * rows;
10606
10607 while (it.current_y < it.last_visible_y)
10608 {
10609 int h = 0;
10610 if (extra > 0 && rows-- > 0)
10611 {
10612 h = (extra + rows - 1) / rows;
10613 extra -= h;
10614 }
10615 display_tool_bar_line (&it, height + h);
10616 }
10617 }
10618 else
10619 {
10620 while (it.current_y < it.last_visible_y)
10621 display_tool_bar_line (&it, 0);
10622 }
10623
10624 /* It doesn't make much sense to try scrolling in the tool-bar
10625 window, so don't do it. */
10626 w->desired_matrix->no_scrolling_p = 1;
10627 w->must_be_updated_p = 1;
10628
10629 if (!NILP (Vauto_resize_tool_bars))
10630 {
10631 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10632 int change_height_p = 0;
10633
10634 /* If we couldn't display everything, change the tool-bar's
10635 height if there is room for more. */
10636 if (IT_STRING_CHARPOS (it) < it.end_charpos
10637 && it.current_y < max_tool_bar_height)
10638 change_height_p = 1;
10639
10640 row = it.glyph_row - 1;
10641
10642 /* If there are blank lines at the end, except for a partially
10643 visible blank line at the end that is smaller than
10644 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10645 if (!row->displays_text_p
10646 && row->height >= FRAME_LINE_HEIGHT (f))
10647 change_height_p = 1;
10648
10649 /* If row displays tool-bar items, but is partially visible,
10650 change the tool-bar's height. */
10651 if (row->displays_text_p
10652 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10653 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10654 change_height_p = 1;
10655
10656 /* Resize windows as needed by changing the `tool-bar-lines'
10657 frame parameter. */
10658 if (change_height_p)
10659 {
10660 Lisp_Object frame;
10661 int old_height = WINDOW_TOTAL_LINES (w);
10662 int nrows;
10663 int nlines = tool_bar_lines_needed (f, &nrows);
10664
10665 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10666 && !f->minimize_tool_bar_window_p)
10667 ? (nlines > old_height)
10668 : (nlines != old_height));
10669 f->minimize_tool_bar_window_p = 0;
10670
10671 if (change_height_p)
10672 {
10673 XSETFRAME (frame, f);
10674 Fmodify_frame_parameters (frame,
10675 Fcons (Fcons (Qtool_bar_lines,
10676 make_number (nlines)),
10677 Qnil));
10678 if (WINDOW_TOTAL_LINES (w) != old_height)
10679 {
10680 clear_glyph_matrix (w->desired_matrix);
10681 f->n_tool_bar_rows = nrows;
10682 fonts_changed_p = 1;
10683 return 1;
10684 }
10685 }
10686 }
10687 }
10688
10689 f->minimize_tool_bar_window_p = 0;
10690 return 0;
10691 }
10692
10693
10694 /* Get information about the tool-bar item which is displayed in GLYPH
10695 on frame F. Return in *PROP_IDX the index where tool-bar item
10696 properties start in F->tool_bar_items. Value is zero if
10697 GLYPH doesn't display a tool-bar item. */
10698
10699 static int
10700 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10701 {
10702 Lisp_Object prop;
10703 int success_p;
10704 int charpos;
10705
10706 /* This function can be called asynchronously, which means we must
10707 exclude any possibility that Fget_text_property signals an
10708 error. */
10709 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10710 charpos = max (0, charpos);
10711
10712 /* Get the text property `menu-item' at pos. The value of that
10713 property is the start index of this item's properties in
10714 F->tool_bar_items. */
10715 prop = Fget_text_property (make_number (charpos),
10716 Qmenu_item, f->current_tool_bar_string);
10717 if (INTEGERP (prop))
10718 {
10719 *prop_idx = XINT (prop);
10720 success_p = 1;
10721 }
10722 else
10723 success_p = 0;
10724
10725 return success_p;
10726 }
10727
10728 \f
10729 /* Get information about the tool-bar item at position X/Y on frame F.
10730 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10731 the current matrix of the tool-bar window of F, or NULL if not
10732 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10733 item in F->tool_bar_items. Value is
10734
10735 -1 if X/Y is not on a tool-bar item
10736 0 if X/Y is on the same item that was highlighted before.
10737 1 otherwise. */
10738
10739 static int
10740 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10741 int *hpos, int *vpos, int *prop_idx)
10742 {
10743 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10744 struct window *w = XWINDOW (f->tool_bar_window);
10745 int area;
10746
10747 /* Find the glyph under X/Y. */
10748 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10749 if (*glyph == NULL)
10750 return -1;
10751
10752 /* Get the start of this tool-bar item's properties in
10753 f->tool_bar_items. */
10754 if (!tool_bar_item_info (f, *glyph, prop_idx))
10755 return -1;
10756
10757 /* Is mouse on the highlighted item? */
10758 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10759 && *vpos >= hlinfo->mouse_face_beg_row
10760 && *vpos <= hlinfo->mouse_face_end_row
10761 && (*vpos > hlinfo->mouse_face_beg_row
10762 || *hpos >= hlinfo->mouse_face_beg_col)
10763 && (*vpos < hlinfo->mouse_face_end_row
10764 || *hpos < hlinfo->mouse_face_end_col
10765 || hlinfo->mouse_face_past_end))
10766 return 0;
10767
10768 return 1;
10769 }
10770
10771
10772 /* EXPORT:
10773 Handle mouse button event on the tool-bar of frame F, at
10774 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10775 0 for button release. MODIFIERS is event modifiers for button
10776 release. */
10777
10778 void
10779 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10780 unsigned int modifiers)
10781 {
10782 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10783 struct window *w = XWINDOW (f->tool_bar_window);
10784 int hpos, vpos, prop_idx;
10785 struct glyph *glyph;
10786 Lisp_Object enabled_p;
10787
10788 /* If not on the highlighted tool-bar item, return. */
10789 frame_to_window_pixel_xy (w, &x, &y);
10790 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10791 return;
10792
10793 /* If item is disabled, do nothing. */
10794 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10795 if (NILP (enabled_p))
10796 return;
10797
10798 if (down_p)
10799 {
10800 /* Show item in pressed state. */
10801 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10802 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10803 last_tool_bar_item = prop_idx;
10804 }
10805 else
10806 {
10807 Lisp_Object key, frame;
10808 struct input_event event;
10809 EVENT_INIT (event);
10810
10811 /* Show item in released state. */
10812 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10813 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10814
10815 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10816
10817 XSETFRAME (frame, f);
10818 event.kind = TOOL_BAR_EVENT;
10819 event.frame_or_window = frame;
10820 event.arg = frame;
10821 kbd_buffer_store_event (&event);
10822
10823 event.kind = TOOL_BAR_EVENT;
10824 event.frame_or_window = frame;
10825 event.arg = key;
10826 event.modifiers = modifiers;
10827 kbd_buffer_store_event (&event);
10828 last_tool_bar_item = -1;
10829 }
10830 }
10831
10832
10833 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10834 tool-bar window-relative coordinates X/Y. Called from
10835 note_mouse_highlight. */
10836
10837 static void
10838 note_tool_bar_highlight (struct frame *f, int x, int y)
10839 {
10840 Lisp_Object window = f->tool_bar_window;
10841 struct window *w = XWINDOW (window);
10842 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10843 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10844 int hpos, vpos;
10845 struct glyph *glyph;
10846 struct glyph_row *row;
10847 int i;
10848 Lisp_Object enabled_p;
10849 int prop_idx;
10850 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10851 int mouse_down_p, rc;
10852
10853 /* Function note_mouse_highlight is called with negative X/Y
10854 values when mouse moves outside of the frame. */
10855 if (x <= 0 || y <= 0)
10856 {
10857 clear_mouse_face (hlinfo);
10858 return;
10859 }
10860
10861 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10862 if (rc < 0)
10863 {
10864 /* Not on tool-bar item. */
10865 clear_mouse_face (hlinfo);
10866 return;
10867 }
10868 else if (rc == 0)
10869 /* On same tool-bar item as before. */
10870 goto set_help_echo;
10871
10872 clear_mouse_face (hlinfo);
10873
10874 /* Mouse is down, but on different tool-bar item? */
10875 mouse_down_p = (dpyinfo->grabbed
10876 && f == last_mouse_frame
10877 && FRAME_LIVE_P (f));
10878 if (mouse_down_p
10879 && last_tool_bar_item != prop_idx)
10880 return;
10881
10882 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10883 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10884
10885 /* If tool-bar item is not enabled, don't highlight it. */
10886 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10887 if (!NILP (enabled_p))
10888 {
10889 /* Compute the x-position of the glyph. In front and past the
10890 image is a space. We include this in the highlighted area. */
10891 row = MATRIX_ROW (w->current_matrix, vpos);
10892 for (i = x = 0; i < hpos; ++i)
10893 x += row->glyphs[TEXT_AREA][i].pixel_width;
10894
10895 /* Record this as the current active region. */
10896 hlinfo->mouse_face_beg_col = hpos;
10897 hlinfo->mouse_face_beg_row = vpos;
10898 hlinfo->mouse_face_beg_x = x;
10899 hlinfo->mouse_face_beg_y = row->y;
10900 hlinfo->mouse_face_past_end = 0;
10901
10902 hlinfo->mouse_face_end_col = hpos + 1;
10903 hlinfo->mouse_face_end_row = vpos;
10904 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
10905 hlinfo->mouse_face_end_y = row->y;
10906 hlinfo->mouse_face_window = window;
10907 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10908
10909 /* Display it as active. */
10910 show_mouse_face (hlinfo, draw);
10911 hlinfo->mouse_face_image_state = draw;
10912 }
10913
10914 set_help_echo:
10915
10916 /* Set help_echo_string to a help string to display for this tool-bar item.
10917 XTread_socket does the rest. */
10918 help_echo_object = help_echo_window = Qnil;
10919 help_echo_pos = -1;
10920 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10921 if (NILP (help_echo_string))
10922 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10923 }
10924
10925 #endif /* HAVE_WINDOW_SYSTEM */
10926
10927
10928 \f
10929 /************************************************************************
10930 Horizontal scrolling
10931 ************************************************************************/
10932
10933 static int hscroll_window_tree (Lisp_Object);
10934 static int hscroll_windows (Lisp_Object);
10935
10936 /* For all leaf windows in the window tree rooted at WINDOW, set their
10937 hscroll value so that PT is (i) visible in the window, and (ii) so
10938 that it is not within a certain margin at the window's left and
10939 right border. Value is non-zero if any window's hscroll has been
10940 changed. */
10941
10942 static int
10943 hscroll_window_tree (Lisp_Object window)
10944 {
10945 int hscrolled_p = 0;
10946 int hscroll_relative_p = FLOATP (Vhscroll_step);
10947 int hscroll_step_abs = 0;
10948 double hscroll_step_rel = 0;
10949
10950 if (hscroll_relative_p)
10951 {
10952 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10953 if (hscroll_step_rel < 0)
10954 {
10955 hscroll_relative_p = 0;
10956 hscroll_step_abs = 0;
10957 }
10958 }
10959 else if (INTEGERP (Vhscroll_step))
10960 {
10961 hscroll_step_abs = XINT (Vhscroll_step);
10962 if (hscroll_step_abs < 0)
10963 hscroll_step_abs = 0;
10964 }
10965 else
10966 hscroll_step_abs = 0;
10967
10968 while (WINDOWP (window))
10969 {
10970 struct window *w = XWINDOW (window);
10971
10972 if (WINDOWP (w->hchild))
10973 hscrolled_p |= hscroll_window_tree (w->hchild);
10974 else if (WINDOWP (w->vchild))
10975 hscrolled_p |= hscroll_window_tree (w->vchild);
10976 else if (w->cursor.vpos >= 0)
10977 {
10978 int h_margin;
10979 int text_area_width;
10980 struct glyph_row *current_cursor_row
10981 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10982 struct glyph_row *desired_cursor_row
10983 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10984 struct glyph_row *cursor_row
10985 = (desired_cursor_row->enabled_p
10986 ? desired_cursor_row
10987 : current_cursor_row);
10988
10989 text_area_width = window_box_width (w, TEXT_AREA);
10990
10991 /* Scroll when cursor is inside this scroll margin. */
10992 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10993
10994 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10995 && ((XFASTINT (w->hscroll)
10996 && w->cursor.x <= h_margin)
10997 || (cursor_row->enabled_p
10998 && cursor_row->truncated_on_right_p
10999 && (w->cursor.x >= text_area_width - h_margin))))
11000 {
11001 struct it it;
11002 int hscroll;
11003 struct buffer *saved_current_buffer;
11004 EMACS_INT pt;
11005 int wanted_x;
11006
11007 /* Find point in a display of infinite width. */
11008 saved_current_buffer = current_buffer;
11009 current_buffer = XBUFFER (w->buffer);
11010
11011 if (w == XWINDOW (selected_window))
11012 pt = PT;
11013 else
11014 {
11015 pt = marker_position (w->pointm);
11016 pt = max (BEGV, pt);
11017 pt = min (ZV, pt);
11018 }
11019
11020 /* Move iterator to pt starting at cursor_row->start in
11021 a line with infinite width. */
11022 init_to_row_start (&it, w, cursor_row);
11023 it.last_visible_x = INFINITY;
11024 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11025 current_buffer = saved_current_buffer;
11026
11027 /* Position cursor in window. */
11028 if (!hscroll_relative_p && hscroll_step_abs == 0)
11029 hscroll = max (0, (it.current_x
11030 - (ITERATOR_AT_END_OF_LINE_P (&it)
11031 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11032 : (text_area_width / 2))))
11033 / FRAME_COLUMN_WIDTH (it.f);
11034 else if (w->cursor.x >= text_area_width - h_margin)
11035 {
11036 if (hscroll_relative_p)
11037 wanted_x = text_area_width * (1 - hscroll_step_rel)
11038 - h_margin;
11039 else
11040 wanted_x = text_area_width
11041 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11042 - h_margin;
11043 hscroll
11044 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11045 }
11046 else
11047 {
11048 if (hscroll_relative_p)
11049 wanted_x = text_area_width * hscroll_step_rel
11050 + h_margin;
11051 else
11052 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11053 + h_margin;
11054 hscroll
11055 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11056 }
11057 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11058
11059 /* Don't call Fset_window_hscroll if value hasn't
11060 changed because it will prevent redisplay
11061 optimizations. */
11062 if (XFASTINT (w->hscroll) != hscroll)
11063 {
11064 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11065 w->hscroll = make_number (hscroll);
11066 hscrolled_p = 1;
11067 }
11068 }
11069 }
11070
11071 window = w->next;
11072 }
11073
11074 /* Value is non-zero if hscroll of any leaf window has been changed. */
11075 return hscrolled_p;
11076 }
11077
11078
11079 /* Set hscroll so that cursor is visible and not inside horizontal
11080 scroll margins for all windows in the tree rooted at WINDOW. See
11081 also hscroll_window_tree above. Value is non-zero if any window's
11082 hscroll has been changed. If it has, desired matrices on the frame
11083 of WINDOW are cleared. */
11084
11085 static int
11086 hscroll_windows (Lisp_Object window)
11087 {
11088 int hscrolled_p = hscroll_window_tree (window);
11089 if (hscrolled_p)
11090 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11091 return hscrolled_p;
11092 }
11093
11094
11095 \f
11096 /************************************************************************
11097 Redisplay
11098 ************************************************************************/
11099
11100 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11101 to a non-zero value. This is sometimes handy to have in a debugger
11102 session. */
11103
11104 #if GLYPH_DEBUG
11105
11106 /* First and last unchanged row for try_window_id. */
11107
11108 static int debug_first_unchanged_at_end_vpos;
11109 static int debug_last_unchanged_at_beg_vpos;
11110
11111 /* Delta vpos and y. */
11112
11113 static int debug_dvpos, debug_dy;
11114
11115 /* Delta in characters and bytes for try_window_id. */
11116
11117 static EMACS_INT debug_delta, debug_delta_bytes;
11118
11119 /* Values of window_end_pos and window_end_vpos at the end of
11120 try_window_id. */
11121
11122 static EMACS_INT debug_end_vpos;
11123
11124 /* Append a string to W->desired_matrix->method. FMT is a printf
11125 format string. If trace_redisplay_p is non-zero also printf the
11126 resulting string to stderr. */
11127
11128 static void debug_method_add (struct window *, char const *, ...)
11129 ATTRIBUTE_FORMAT_PRINTF (2, 3);
11130
11131 static void
11132 debug_method_add (struct window *w, char const *fmt, ...)
11133 {
11134 char buffer[512];
11135 char *method = w->desired_matrix->method;
11136 int len = strlen (method);
11137 int size = sizeof w->desired_matrix->method;
11138 int remaining = size - len - 1;
11139 va_list ap;
11140
11141 va_start (ap, fmt);
11142 vsprintf (buffer, fmt, ap);
11143 va_end (ap);
11144 if (len && remaining)
11145 {
11146 method[len] = '|';
11147 --remaining, ++len;
11148 }
11149
11150 strncpy (method + len, buffer, remaining);
11151
11152 if (trace_redisplay_p)
11153 fprintf (stderr, "%p (%s): %s\n",
11154 w,
11155 ((BUFFERP (w->buffer)
11156 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
11157 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
11158 : "no buffer"),
11159 buffer);
11160 }
11161
11162 #endif /* GLYPH_DEBUG */
11163
11164
11165 /* Value is non-zero if all changes in window W, which displays
11166 current_buffer, are in the text between START and END. START is a
11167 buffer position, END is given as a distance from Z. Used in
11168 redisplay_internal for display optimization. */
11169
11170 static inline int
11171 text_outside_line_unchanged_p (struct window *w,
11172 EMACS_INT start, EMACS_INT end)
11173 {
11174 int unchanged_p = 1;
11175
11176 /* If text or overlays have changed, see where. */
11177 if (XFASTINT (w->last_modified) < MODIFF
11178 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11179 {
11180 /* Gap in the line? */
11181 if (GPT < start || Z - GPT < end)
11182 unchanged_p = 0;
11183
11184 /* Changes start in front of the line, or end after it? */
11185 if (unchanged_p
11186 && (BEG_UNCHANGED < start - 1
11187 || END_UNCHANGED < end))
11188 unchanged_p = 0;
11189
11190 /* If selective display, can't optimize if changes start at the
11191 beginning of the line. */
11192 if (unchanged_p
11193 && INTEGERP (BVAR (current_buffer, selective_display))
11194 && XINT (BVAR (current_buffer, selective_display)) > 0
11195 && (BEG_UNCHANGED < start || GPT <= start))
11196 unchanged_p = 0;
11197
11198 /* If there are overlays at the start or end of the line, these
11199 may have overlay strings with newlines in them. A change at
11200 START, for instance, may actually concern the display of such
11201 overlay strings as well, and they are displayed on different
11202 lines. So, quickly rule out this case. (For the future, it
11203 might be desirable to implement something more telling than
11204 just BEG/END_UNCHANGED.) */
11205 if (unchanged_p)
11206 {
11207 if (BEG + BEG_UNCHANGED == start
11208 && overlay_touches_p (start))
11209 unchanged_p = 0;
11210 if (END_UNCHANGED == end
11211 && overlay_touches_p (Z - end))
11212 unchanged_p = 0;
11213 }
11214
11215 /* Under bidi reordering, adding or deleting a character in the
11216 beginning of a paragraph, before the first strong directional
11217 character, can change the base direction of the paragraph (unless
11218 the buffer specifies a fixed paragraph direction), which will
11219 require to redisplay the whole paragraph. It might be worthwhile
11220 to find the paragraph limits and widen the range of redisplayed
11221 lines to that, but for now just give up this optimization. */
11222 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11223 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11224 unchanged_p = 0;
11225 }
11226
11227 return unchanged_p;
11228 }
11229
11230
11231 /* Do a frame update, taking possible shortcuts into account. This is
11232 the main external entry point for redisplay.
11233
11234 If the last redisplay displayed an echo area message and that message
11235 is no longer requested, we clear the echo area or bring back the
11236 mini-buffer if that is in use. */
11237
11238 void
11239 redisplay (void)
11240 {
11241 redisplay_internal ();
11242 }
11243
11244
11245 static Lisp_Object
11246 overlay_arrow_string_or_property (Lisp_Object var)
11247 {
11248 Lisp_Object val;
11249
11250 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11251 return val;
11252
11253 return Voverlay_arrow_string;
11254 }
11255
11256 /* Return 1 if there are any overlay-arrows in current_buffer. */
11257 static int
11258 overlay_arrow_in_current_buffer_p (void)
11259 {
11260 Lisp_Object vlist;
11261
11262 for (vlist = Voverlay_arrow_variable_list;
11263 CONSP (vlist);
11264 vlist = XCDR (vlist))
11265 {
11266 Lisp_Object var = XCAR (vlist);
11267 Lisp_Object val;
11268
11269 if (!SYMBOLP (var))
11270 continue;
11271 val = find_symbol_value (var);
11272 if (MARKERP (val)
11273 && current_buffer == XMARKER (val)->buffer)
11274 return 1;
11275 }
11276 return 0;
11277 }
11278
11279
11280 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11281 has changed. */
11282
11283 static int
11284 overlay_arrows_changed_p (void)
11285 {
11286 Lisp_Object vlist;
11287
11288 for (vlist = Voverlay_arrow_variable_list;
11289 CONSP (vlist);
11290 vlist = XCDR (vlist))
11291 {
11292 Lisp_Object var = XCAR (vlist);
11293 Lisp_Object val, pstr;
11294
11295 if (!SYMBOLP (var))
11296 continue;
11297 val = find_symbol_value (var);
11298 if (!MARKERP (val))
11299 continue;
11300 if (! EQ (COERCE_MARKER (val),
11301 Fget (var, Qlast_arrow_position))
11302 || ! (pstr = overlay_arrow_string_or_property (var),
11303 EQ (pstr, Fget (var, Qlast_arrow_string))))
11304 return 1;
11305 }
11306 return 0;
11307 }
11308
11309 /* Mark overlay arrows to be updated on next redisplay. */
11310
11311 static void
11312 update_overlay_arrows (int up_to_date)
11313 {
11314 Lisp_Object vlist;
11315
11316 for (vlist = Voverlay_arrow_variable_list;
11317 CONSP (vlist);
11318 vlist = XCDR (vlist))
11319 {
11320 Lisp_Object var = XCAR (vlist);
11321
11322 if (!SYMBOLP (var))
11323 continue;
11324
11325 if (up_to_date > 0)
11326 {
11327 Lisp_Object val = find_symbol_value (var);
11328 Fput (var, Qlast_arrow_position,
11329 COERCE_MARKER (val));
11330 Fput (var, Qlast_arrow_string,
11331 overlay_arrow_string_or_property (var));
11332 }
11333 else if (up_to_date < 0
11334 || !NILP (Fget (var, Qlast_arrow_position)))
11335 {
11336 Fput (var, Qlast_arrow_position, Qt);
11337 Fput (var, Qlast_arrow_string, Qt);
11338 }
11339 }
11340 }
11341
11342
11343 /* Return overlay arrow string to display at row.
11344 Return integer (bitmap number) for arrow bitmap in left fringe.
11345 Return nil if no overlay arrow. */
11346
11347 static Lisp_Object
11348 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11349 {
11350 Lisp_Object vlist;
11351
11352 for (vlist = Voverlay_arrow_variable_list;
11353 CONSP (vlist);
11354 vlist = XCDR (vlist))
11355 {
11356 Lisp_Object var = XCAR (vlist);
11357 Lisp_Object val;
11358
11359 if (!SYMBOLP (var))
11360 continue;
11361
11362 val = find_symbol_value (var);
11363
11364 if (MARKERP (val)
11365 && current_buffer == XMARKER (val)->buffer
11366 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11367 {
11368 if (FRAME_WINDOW_P (it->f)
11369 /* FIXME: if ROW->reversed_p is set, this should test
11370 the right fringe, not the left one. */
11371 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11372 {
11373 #ifdef HAVE_WINDOW_SYSTEM
11374 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11375 {
11376 int fringe_bitmap;
11377 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11378 return make_number (fringe_bitmap);
11379 }
11380 #endif
11381 return make_number (-1); /* Use default arrow bitmap */
11382 }
11383 return overlay_arrow_string_or_property (var);
11384 }
11385 }
11386
11387 return Qnil;
11388 }
11389
11390 /* Return 1 if point moved out of or into a composition. Otherwise
11391 return 0. PREV_BUF and PREV_PT are the last point buffer and
11392 position. BUF and PT are the current point buffer and position. */
11393
11394 static int
11395 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11396 struct buffer *buf, EMACS_INT pt)
11397 {
11398 EMACS_INT start, end;
11399 Lisp_Object prop;
11400 Lisp_Object buffer;
11401
11402 XSETBUFFER (buffer, buf);
11403 /* Check a composition at the last point if point moved within the
11404 same buffer. */
11405 if (prev_buf == buf)
11406 {
11407 if (prev_pt == pt)
11408 /* Point didn't move. */
11409 return 0;
11410
11411 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11412 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11413 && COMPOSITION_VALID_P (start, end, prop)
11414 && start < prev_pt && end > prev_pt)
11415 /* The last point was within the composition. Return 1 iff
11416 point moved out of the composition. */
11417 return (pt <= start || pt >= end);
11418 }
11419
11420 /* Check a composition at the current point. */
11421 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11422 && find_composition (pt, -1, &start, &end, &prop, buffer)
11423 && COMPOSITION_VALID_P (start, end, prop)
11424 && start < pt && end > pt);
11425 }
11426
11427
11428 /* Reconsider the setting of B->clip_changed which is displayed
11429 in window W. */
11430
11431 static inline void
11432 reconsider_clip_changes (struct window *w, struct buffer *b)
11433 {
11434 if (b->clip_changed
11435 && !NILP (w->window_end_valid)
11436 && w->current_matrix->buffer == b
11437 && w->current_matrix->zv == BUF_ZV (b)
11438 && w->current_matrix->begv == BUF_BEGV (b))
11439 b->clip_changed = 0;
11440
11441 /* If display wasn't paused, and W is not a tool bar window, see if
11442 point has been moved into or out of a composition. In that case,
11443 we set b->clip_changed to 1 to force updating the screen. If
11444 b->clip_changed has already been set to 1, we can skip this
11445 check. */
11446 if (!b->clip_changed
11447 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11448 {
11449 EMACS_INT pt;
11450
11451 if (w == XWINDOW (selected_window))
11452 pt = PT;
11453 else
11454 pt = marker_position (w->pointm);
11455
11456 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11457 || pt != XINT (w->last_point))
11458 && check_point_in_composition (w->current_matrix->buffer,
11459 XINT (w->last_point),
11460 XBUFFER (w->buffer), pt))
11461 b->clip_changed = 1;
11462 }
11463 }
11464 \f
11465
11466 /* Select FRAME to forward the values of frame-local variables into C
11467 variables so that the redisplay routines can access those values
11468 directly. */
11469
11470 static void
11471 select_frame_for_redisplay (Lisp_Object frame)
11472 {
11473 Lisp_Object tail, tem;
11474 Lisp_Object old = selected_frame;
11475 struct Lisp_Symbol *sym;
11476
11477 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11478
11479 selected_frame = frame;
11480
11481 do {
11482 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11483 if (CONSP (XCAR (tail))
11484 && (tem = XCAR (XCAR (tail)),
11485 SYMBOLP (tem))
11486 && (sym = indirect_variable (XSYMBOL (tem)),
11487 sym->redirect == SYMBOL_LOCALIZED)
11488 && sym->val.blv->frame_local)
11489 /* Use find_symbol_value rather than Fsymbol_value
11490 to avoid an error if it is void. */
11491 find_symbol_value (tem);
11492 } while (!EQ (frame, old) && (frame = old, 1));
11493 }
11494
11495
11496 #define STOP_POLLING \
11497 do { if (! polling_stopped_here) stop_polling (); \
11498 polling_stopped_here = 1; } while (0)
11499
11500 #define RESUME_POLLING \
11501 do { if (polling_stopped_here) start_polling (); \
11502 polling_stopped_here = 0; } while (0)
11503
11504
11505 /* Perhaps in the future avoid recentering windows if it
11506 is not necessary; currently that causes some problems. */
11507
11508 static void
11509 redisplay_internal (void)
11510 {
11511 struct window *w = XWINDOW (selected_window);
11512 struct window *sw;
11513 struct frame *fr;
11514 int pending;
11515 int must_finish = 0;
11516 struct text_pos tlbufpos, tlendpos;
11517 int number_of_visible_frames;
11518 int count, count1;
11519 struct frame *sf;
11520 int polling_stopped_here = 0;
11521 Lisp_Object old_frame = selected_frame;
11522
11523 /* Non-zero means redisplay has to consider all windows on all
11524 frames. Zero means, only selected_window is considered. */
11525 int consider_all_windows_p;
11526
11527 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11528
11529 /* No redisplay if running in batch mode or frame is not yet fully
11530 initialized, or redisplay is explicitly turned off by setting
11531 Vinhibit_redisplay. */
11532 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11533 || !NILP (Vinhibit_redisplay))
11534 return;
11535
11536 /* Don't examine these until after testing Vinhibit_redisplay.
11537 When Emacs is shutting down, perhaps because its connection to
11538 X has dropped, we should not look at them at all. */
11539 fr = XFRAME (w->frame);
11540 sf = SELECTED_FRAME ();
11541
11542 if (!fr->glyphs_initialized_p)
11543 return;
11544
11545 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11546 if (popup_activated ())
11547 return;
11548 #endif
11549
11550 /* I don't think this happens but let's be paranoid. */
11551 if (redisplaying_p)
11552 return;
11553
11554 /* Record a function that resets redisplaying_p to its old value
11555 when we leave this function. */
11556 count = SPECPDL_INDEX ();
11557 record_unwind_protect (unwind_redisplay,
11558 Fcons (make_number (redisplaying_p), selected_frame));
11559 ++redisplaying_p;
11560 specbind (Qinhibit_free_realized_faces, Qnil);
11561
11562 {
11563 Lisp_Object tail, frame;
11564
11565 FOR_EACH_FRAME (tail, frame)
11566 {
11567 struct frame *f = XFRAME (frame);
11568 f->already_hscrolled_p = 0;
11569 }
11570 }
11571
11572 retry:
11573 /* Remember the currently selected window. */
11574 sw = w;
11575
11576 if (!EQ (old_frame, selected_frame)
11577 && FRAME_LIVE_P (XFRAME (old_frame)))
11578 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11579 selected_frame and selected_window to be temporarily out-of-sync so
11580 when we come back here via `goto retry', we need to resync because we
11581 may need to run Elisp code (via prepare_menu_bars). */
11582 select_frame_for_redisplay (old_frame);
11583
11584 pending = 0;
11585 reconsider_clip_changes (w, current_buffer);
11586 last_escape_glyph_frame = NULL;
11587 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11588 last_glyphless_glyph_frame = NULL;
11589 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11590
11591 /* If new fonts have been loaded that make a glyph matrix adjustment
11592 necessary, do it. */
11593 if (fonts_changed_p)
11594 {
11595 adjust_glyphs (NULL);
11596 ++windows_or_buffers_changed;
11597 fonts_changed_p = 0;
11598 }
11599
11600 /* If face_change_count is non-zero, init_iterator will free all
11601 realized faces, which includes the faces referenced from current
11602 matrices. So, we can't reuse current matrices in this case. */
11603 if (face_change_count)
11604 ++windows_or_buffers_changed;
11605
11606 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11607 && FRAME_TTY (sf)->previous_frame != sf)
11608 {
11609 /* Since frames on a single ASCII terminal share the same
11610 display area, displaying a different frame means redisplay
11611 the whole thing. */
11612 windows_or_buffers_changed++;
11613 SET_FRAME_GARBAGED (sf);
11614 #ifndef DOS_NT
11615 set_tty_color_mode (FRAME_TTY (sf), sf);
11616 #endif
11617 FRAME_TTY (sf)->previous_frame = sf;
11618 }
11619
11620 /* Set the visible flags for all frames. Do this before checking
11621 for resized or garbaged frames; they want to know if their frames
11622 are visible. See the comment in frame.h for
11623 FRAME_SAMPLE_VISIBILITY. */
11624 {
11625 Lisp_Object tail, frame;
11626
11627 number_of_visible_frames = 0;
11628
11629 FOR_EACH_FRAME (tail, frame)
11630 {
11631 struct frame *f = XFRAME (frame);
11632
11633 FRAME_SAMPLE_VISIBILITY (f);
11634 if (FRAME_VISIBLE_P (f))
11635 ++number_of_visible_frames;
11636 clear_desired_matrices (f);
11637 }
11638 }
11639
11640 /* Notice any pending interrupt request to change frame size. */
11641 do_pending_window_change (1);
11642
11643 /* do_pending_window_change could change the selected_window due to
11644 frame resizing which makes the selected window too small. */
11645 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
11646 {
11647 sw = w;
11648 reconsider_clip_changes (w, current_buffer);
11649 }
11650
11651 /* Clear frames marked as garbaged. */
11652 if (frame_garbaged)
11653 clear_garbaged_frames ();
11654
11655 /* Build menubar and tool-bar items. */
11656 if (NILP (Vmemory_full))
11657 prepare_menu_bars ();
11658
11659 if (windows_or_buffers_changed)
11660 update_mode_lines++;
11661
11662 /* Detect case that we need to write or remove a star in the mode line. */
11663 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11664 {
11665 w->update_mode_line = Qt;
11666 if (buffer_shared > 1)
11667 update_mode_lines++;
11668 }
11669
11670 /* Avoid invocation of point motion hooks by `current_column' below. */
11671 count1 = SPECPDL_INDEX ();
11672 specbind (Qinhibit_point_motion_hooks, Qt);
11673
11674 /* If %c is in the mode line, update it if needed. */
11675 if (!NILP (w->column_number_displayed)
11676 /* This alternative quickly identifies a common case
11677 where no change is needed. */
11678 && !(PT == XFASTINT (w->last_point)
11679 && XFASTINT (w->last_modified) >= MODIFF
11680 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11681 && (XFASTINT (w->column_number_displayed) != current_column ()))
11682 w->update_mode_line = Qt;
11683
11684 unbind_to (count1, Qnil);
11685
11686 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11687
11688 /* The variable buffer_shared is set in redisplay_window and
11689 indicates that we redisplay a buffer in different windows. See
11690 there. */
11691 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11692 || cursor_type_changed);
11693
11694 /* If specs for an arrow have changed, do thorough redisplay
11695 to ensure we remove any arrow that should no longer exist. */
11696 if (overlay_arrows_changed_p ())
11697 consider_all_windows_p = windows_or_buffers_changed = 1;
11698
11699 /* Normally the message* functions will have already displayed and
11700 updated the echo area, but the frame may have been trashed, or
11701 the update may have been preempted, so display the echo area
11702 again here. Checking message_cleared_p captures the case that
11703 the echo area should be cleared. */
11704 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11705 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11706 || (message_cleared_p
11707 && minibuf_level == 0
11708 /* If the mini-window is currently selected, this means the
11709 echo-area doesn't show through. */
11710 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11711 {
11712 int window_height_changed_p = echo_area_display (0);
11713 must_finish = 1;
11714
11715 /* If we don't display the current message, don't clear the
11716 message_cleared_p flag, because, if we did, we wouldn't clear
11717 the echo area in the next redisplay which doesn't preserve
11718 the echo area. */
11719 if (!display_last_displayed_message_p)
11720 message_cleared_p = 0;
11721
11722 if (fonts_changed_p)
11723 goto retry;
11724 else if (window_height_changed_p)
11725 {
11726 consider_all_windows_p = 1;
11727 ++update_mode_lines;
11728 ++windows_or_buffers_changed;
11729
11730 /* If window configuration was changed, frames may have been
11731 marked garbaged. Clear them or we will experience
11732 surprises wrt scrolling. */
11733 if (frame_garbaged)
11734 clear_garbaged_frames ();
11735 }
11736 }
11737 else if (EQ (selected_window, minibuf_window)
11738 && (current_buffer->clip_changed
11739 || XFASTINT (w->last_modified) < MODIFF
11740 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11741 && resize_mini_window (w, 0))
11742 {
11743 /* Resized active mini-window to fit the size of what it is
11744 showing if its contents might have changed. */
11745 must_finish = 1;
11746 /* FIXME: this causes all frames to be updated, which seems unnecessary
11747 since only the current frame needs to be considered. This function needs
11748 to be rewritten with two variables, consider_all_windows and
11749 consider_all_frames. */
11750 consider_all_windows_p = 1;
11751 ++windows_or_buffers_changed;
11752 ++update_mode_lines;
11753
11754 /* If window configuration was changed, frames may have been
11755 marked garbaged. Clear them or we will experience
11756 surprises wrt scrolling. */
11757 if (frame_garbaged)
11758 clear_garbaged_frames ();
11759 }
11760
11761
11762 /* If showing the region, and mark has changed, we must redisplay
11763 the whole window. The assignment to this_line_start_pos prevents
11764 the optimization directly below this if-statement. */
11765 if (((!NILP (Vtransient_mark_mode)
11766 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11767 != !NILP (w->region_showing))
11768 || (!NILP (w->region_showing)
11769 && !EQ (w->region_showing,
11770 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
11771 CHARPOS (this_line_start_pos) = 0;
11772
11773 /* Optimize the case that only the line containing the cursor in the
11774 selected window has changed. Variables starting with this_ are
11775 set in display_line and record information about the line
11776 containing the cursor. */
11777 tlbufpos = this_line_start_pos;
11778 tlendpos = this_line_end_pos;
11779 if (!consider_all_windows_p
11780 && CHARPOS (tlbufpos) > 0
11781 && NILP (w->update_mode_line)
11782 && !current_buffer->clip_changed
11783 && !current_buffer->prevent_redisplay_optimizations_p
11784 && FRAME_VISIBLE_P (XFRAME (w->frame))
11785 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11786 /* Make sure recorded data applies to current buffer, etc. */
11787 && this_line_buffer == current_buffer
11788 && current_buffer == XBUFFER (w->buffer)
11789 && NILP (w->force_start)
11790 && NILP (w->optional_new_start)
11791 /* Point must be on the line that we have info recorded about. */
11792 && PT >= CHARPOS (tlbufpos)
11793 && PT <= Z - CHARPOS (tlendpos)
11794 /* All text outside that line, including its final newline,
11795 must be unchanged. */
11796 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11797 CHARPOS (tlendpos)))
11798 {
11799 if (CHARPOS (tlbufpos) > BEGV
11800 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11801 && (CHARPOS (tlbufpos) == ZV
11802 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11803 /* Former continuation line has disappeared by becoming empty. */
11804 goto cancel;
11805 else if (XFASTINT (w->last_modified) < MODIFF
11806 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11807 || MINI_WINDOW_P (w))
11808 {
11809 /* We have to handle the case of continuation around a
11810 wide-column character (see the comment in indent.c around
11811 line 1340).
11812
11813 For instance, in the following case:
11814
11815 -------- Insert --------
11816 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11817 J_I_ ==> J_I_ `^^' are cursors.
11818 ^^ ^^
11819 -------- --------
11820
11821 As we have to redraw the line above, we cannot use this
11822 optimization. */
11823
11824 struct it it;
11825 int line_height_before = this_line_pixel_height;
11826
11827 /* Note that start_display will handle the case that the
11828 line starting at tlbufpos is a continuation line. */
11829 start_display (&it, w, tlbufpos);
11830
11831 /* Implementation note: It this still necessary? */
11832 if (it.current_x != this_line_start_x)
11833 goto cancel;
11834
11835 TRACE ((stderr, "trying display optimization 1\n"));
11836 w->cursor.vpos = -1;
11837 overlay_arrow_seen = 0;
11838 it.vpos = this_line_vpos;
11839 it.current_y = this_line_y;
11840 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11841 display_line (&it);
11842
11843 /* If line contains point, is not continued,
11844 and ends at same distance from eob as before, we win. */
11845 if (w->cursor.vpos >= 0
11846 /* Line is not continued, otherwise this_line_start_pos
11847 would have been set to 0 in display_line. */
11848 && CHARPOS (this_line_start_pos)
11849 /* Line ends as before. */
11850 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11851 /* Line has same height as before. Otherwise other lines
11852 would have to be shifted up or down. */
11853 && this_line_pixel_height == line_height_before)
11854 {
11855 /* If this is not the window's last line, we must adjust
11856 the charstarts of the lines below. */
11857 if (it.current_y < it.last_visible_y)
11858 {
11859 struct glyph_row *row
11860 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11861 EMACS_INT delta, delta_bytes;
11862
11863 /* We used to distinguish between two cases here,
11864 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11865 when the line ends in a newline or the end of the
11866 buffer's accessible portion. But both cases did
11867 the same, so they were collapsed. */
11868 delta = (Z
11869 - CHARPOS (tlendpos)
11870 - MATRIX_ROW_START_CHARPOS (row));
11871 delta_bytes = (Z_BYTE
11872 - BYTEPOS (tlendpos)
11873 - MATRIX_ROW_START_BYTEPOS (row));
11874
11875 increment_matrix_positions (w->current_matrix,
11876 this_line_vpos + 1,
11877 w->current_matrix->nrows,
11878 delta, delta_bytes);
11879 }
11880
11881 /* If this row displays text now but previously didn't,
11882 or vice versa, w->window_end_vpos may have to be
11883 adjusted. */
11884 if ((it.glyph_row - 1)->displays_text_p)
11885 {
11886 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11887 XSETINT (w->window_end_vpos, this_line_vpos);
11888 }
11889 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11890 && this_line_vpos > 0)
11891 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11892 w->window_end_valid = Qnil;
11893
11894 /* Update hint: No need to try to scroll in update_window. */
11895 w->desired_matrix->no_scrolling_p = 1;
11896
11897 #if GLYPH_DEBUG
11898 *w->desired_matrix->method = 0;
11899 debug_method_add (w, "optimization 1");
11900 #endif
11901 #ifdef HAVE_WINDOW_SYSTEM
11902 update_window_fringes (w, 0);
11903 #endif
11904 goto update;
11905 }
11906 else
11907 goto cancel;
11908 }
11909 else if (/* Cursor position hasn't changed. */
11910 PT == XFASTINT (w->last_point)
11911 /* Make sure the cursor was last displayed
11912 in this window. Otherwise we have to reposition it. */
11913 && 0 <= w->cursor.vpos
11914 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11915 {
11916 if (!must_finish)
11917 {
11918 do_pending_window_change (1);
11919 /* If selected_window changed, redisplay again. */
11920 if (WINDOWP (selected_window)
11921 && (w = XWINDOW (selected_window)) != sw)
11922 goto retry;
11923
11924 /* We used to always goto end_of_redisplay here, but this
11925 isn't enough if we have a blinking cursor. */
11926 if (w->cursor_off_p == w->last_cursor_off_p)
11927 goto end_of_redisplay;
11928 }
11929 goto update;
11930 }
11931 /* If highlighting the region, or if the cursor is in the echo area,
11932 then we can't just move the cursor. */
11933 else if (! (!NILP (Vtransient_mark_mode)
11934 && !NILP (BVAR (current_buffer, mark_active)))
11935 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
11936 || highlight_nonselected_windows)
11937 && NILP (w->region_showing)
11938 && NILP (Vshow_trailing_whitespace)
11939 && !cursor_in_echo_area)
11940 {
11941 struct it it;
11942 struct glyph_row *row;
11943
11944 /* Skip from tlbufpos to PT and see where it is. Note that
11945 PT may be in invisible text. If so, we will end at the
11946 next visible position. */
11947 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11948 NULL, DEFAULT_FACE_ID);
11949 it.current_x = this_line_start_x;
11950 it.current_y = this_line_y;
11951 it.vpos = this_line_vpos;
11952
11953 /* The call to move_it_to stops in front of PT, but
11954 moves over before-strings. */
11955 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11956
11957 if (it.vpos == this_line_vpos
11958 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11959 row->enabled_p))
11960 {
11961 xassert (this_line_vpos == it.vpos);
11962 xassert (this_line_y == it.current_y);
11963 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11964 #if GLYPH_DEBUG
11965 *w->desired_matrix->method = 0;
11966 debug_method_add (w, "optimization 3");
11967 #endif
11968 goto update;
11969 }
11970 else
11971 goto cancel;
11972 }
11973
11974 cancel:
11975 /* Text changed drastically or point moved off of line. */
11976 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11977 }
11978
11979 CHARPOS (this_line_start_pos) = 0;
11980 consider_all_windows_p |= buffer_shared > 1;
11981 ++clear_face_cache_count;
11982 #ifdef HAVE_WINDOW_SYSTEM
11983 ++clear_image_cache_count;
11984 #endif
11985
11986 /* Build desired matrices, and update the display. If
11987 consider_all_windows_p is non-zero, do it for all windows on all
11988 frames. Otherwise do it for selected_window, only. */
11989
11990 if (consider_all_windows_p)
11991 {
11992 Lisp_Object tail, frame;
11993
11994 FOR_EACH_FRAME (tail, frame)
11995 XFRAME (frame)->updated_p = 0;
11996
11997 /* Recompute # windows showing selected buffer. This will be
11998 incremented each time such a window is displayed. */
11999 buffer_shared = 0;
12000
12001 FOR_EACH_FRAME (tail, frame)
12002 {
12003 struct frame *f = XFRAME (frame);
12004
12005 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12006 {
12007 if (! EQ (frame, selected_frame))
12008 /* Select the frame, for the sake of frame-local
12009 variables. */
12010 select_frame_for_redisplay (frame);
12011
12012 /* Mark all the scroll bars to be removed; we'll redeem
12013 the ones we want when we redisplay their windows. */
12014 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12015 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12016
12017 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12018 redisplay_windows (FRAME_ROOT_WINDOW (f));
12019
12020 /* The X error handler may have deleted that frame. */
12021 if (!FRAME_LIVE_P (f))
12022 continue;
12023
12024 /* Any scroll bars which redisplay_windows should have
12025 nuked should now go away. */
12026 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12027 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12028
12029 /* If fonts changed, display again. */
12030 /* ??? rms: I suspect it is a mistake to jump all the way
12031 back to retry here. It should just retry this frame. */
12032 if (fonts_changed_p)
12033 goto retry;
12034
12035 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12036 {
12037 /* See if we have to hscroll. */
12038 if (!f->already_hscrolled_p)
12039 {
12040 f->already_hscrolled_p = 1;
12041 if (hscroll_windows (f->root_window))
12042 goto retry;
12043 }
12044
12045 /* Prevent various kinds of signals during display
12046 update. stdio is not robust about handling
12047 signals, which can cause an apparent I/O
12048 error. */
12049 if (interrupt_input)
12050 unrequest_sigio ();
12051 STOP_POLLING;
12052
12053 /* Update the display. */
12054 set_window_update_flags (XWINDOW (f->root_window), 1);
12055 pending |= update_frame (f, 0, 0);
12056 f->updated_p = 1;
12057 }
12058 }
12059 }
12060
12061 if (!EQ (old_frame, selected_frame)
12062 && FRAME_LIVE_P (XFRAME (old_frame)))
12063 /* We played a bit fast-and-loose above and allowed selected_frame
12064 and selected_window to be temporarily out-of-sync but let's make
12065 sure this stays contained. */
12066 select_frame_for_redisplay (old_frame);
12067 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12068
12069 if (!pending)
12070 {
12071 /* Do the mark_window_display_accurate after all windows have
12072 been redisplayed because this call resets flags in buffers
12073 which are needed for proper redisplay. */
12074 FOR_EACH_FRAME (tail, frame)
12075 {
12076 struct frame *f = XFRAME (frame);
12077 if (f->updated_p)
12078 {
12079 mark_window_display_accurate (f->root_window, 1);
12080 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12081 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12082 }
12083 }
12084 }
12085 }
12086 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12087 {
12088 Lisp_Object mini_window;
12089 struct frame *mini_frame;
12090
12091 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12092 /* Use list_of_error, not Qerror, so that
12093 we catch only errors and don't run the debugger. */
12094 internal_condition_case_1 (redisplay_window_1, selected_window,
12095 list_of_error,
12096 redisplay_window_error);
12097
12098 /* Compare desired and current matrices, perform output. */
12099
12100 update:
12101 /* If fonts changed, display again. */
12102 if (fonts_changed_p)
12103 goto retry;
12104
12105 /* Prevent various kinds of signals during display update.
12106 stdio is not robust about handling signals,
12107 which can cause an apparent I/O error. */
12108 if (interrupt_input)
12109 unrequest_sigio ();
12110 STOP_POLLING;
12111
12112 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12113 {
12114 if (hscroll_windows (selected_window))
12115 goto retry;
12116
12117 XWINDOW (selected_window)->must_be_updated_p = 1;
12118 pending = update_frame (sf, 0, 0);
12119 }
12120
12121 /* We may have called echo_area_display at the top of this
12122 function. If the echo area is on another frame, that may
12123 have put text on a frame other than the selected one, so the
12124 above call to update_frame would not have caught it. Catch
12125 it here. */
12126 mini_window = FRAME_MINIBUF_WINDOW (sf);
12127 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12128
12129 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12130 {
12131 XWINDOW (mini_window)->must_be_updated_p = 1;
12132 pending |= update_frame (mini_frame, 0, 0);
12133 if (!pending && hscroll_windows (mini_window))
12134 goto retry;
12135 }
12136 }
12137
12138 /* If display was paused because of pending input, make sure we do a
12139 thorough update the next time. */
12140 if (pending)
12141 {
12142 /* Prevent the optimization at the beginning of
12143 redisplay_internal that tries a single-line update of the
12144 line containing the cursor in the selected window. */
12145 CHARPOS (this_line_start_pos) = 0;
12146
12147 /* Let the overlay arrow be updated the next time. */
12148 update_overlay_arrows (0);
12149
12150 /* If we pause after scrolling, some rows in the current
12151 matrices of some windows are not valid. */
12152 if (!WINDOW_FULL_WIDTH_P (w)
12153 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12154 update_mode_lines = 1;
12155 }
12156 else
12157 {
12158 if (!consider_all_windows_p)
12159 {
12160 /* This has already been done above if
12161 consider_all_windows_p is set. */
12162 mark_window_display_accurate_1 (w, 1);
12163
12164 /* Say overlay arrows are up to date. */
12165 update_overlay_arrows (1);
12166
12167 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12168 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12169 }
12170
12171 update_mode_lines = 0;
12172 windows_or_buffers_changed = 0;
12173 cursor_type_changed = 0;
12174 }
12175
12176 /* Start SIGIO interrupts coming again. Having them off during the
12177 code above makes it less likely one will discard output, but not
12178 impossible, since there might be stuff in the system buffer here.
12179 But it is much hairier to try to do anything about that. */
12180 if (interrupt_input)
12181 request_sigio ();
12182 RESUME_POLLING;
12183
12184 /* If a frame has become visible which was not before, redisplay
12185 again, so that we display it. Expose events for such a frame
12186 (which it gets when becoming visible) don't call the parts of
12187 redisplay constructing glyphs, so simply exposing a frame won't
12188 display anything in this case. So, we have to display these
12189 frames here explicitly. */
12190 if (!pending)
12191 {
12192 Lisp_Object tail, frame;
12193 int new_count = 0;
12194
12195 FOR_EACH_FRAME (tail, frame)
12196 {
12197 int this_is_visible = 0;
12198
12199 if (XFRAME (frame)->visible)
12200 this_is_visible = 1;
12201 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12202 if (XFRAME (frame)->visible)
12203 this_is_visible = 1;
12204
12205 if (this_is_visible)
12206 new_count++;
12207 }
12208
12209 if (new_count != number_of_visible_frames)
12210 windows_or_buffers_changed++;
12211 }
12212
12213 /* Change frame size now if a change is pending. */
12214 do_pending_window_change (1);
12215
12216 /* If we just did a pending size change, or have additional
12217 visible frames, or selected_window changed, redisplay again. */
12218 if ((windows_or_buffers_changed && !pending)
12219 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12220 goto retry;
12221
12222 /* Clear the face and image caches.
12223
12224 We used to do this only if consider_all_windows_p. But the cache
12225 needs to be cleared if a timer creates images in the current
12226 buffer (e.g. the test case in Bug#6230). */
12227
12228 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12229 {
12230 clear_face_cache (0);
12231 clear_face_cache_count = 0;
12232 }
12233
12234 #ifdef HAVE_WINDOW_SYSTEM
12235 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12236 {
12237 clear_image_caches (Qnil);
12238 clear_image_cache_count = 0;
12239 }
12240 #endif /* HAVE_WINDOW_SYSTEM */
12241
12242 end_of_redisplay:
12243 unbind_to (count, Qnil);
12244 RESUME_POLLING;
12245 }
12246
12247
12248 /* Redisplay, but leave alone any recent echo area message unless
12249 another message has been requested in its place.
12250
12251 This is useful in situations where you need to redisplay but no
12252 user action has occurred, making it inappropriate for the message
12253 area to be cleared. See tracking_off and
12254 wait_reading_process_output for examples of these situations.
12255
12256 FROM_WHERE is an integer saying from where this function was
12257 called. This is useful for debugging. */
12258
12259 void
12260 redisplay_preserve_echo_area (int from_where)
12261 {
12262 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12263
12264 if (!NILP (echo_area_buffer[1]))
12265 {
12266 /* We have a previously displayed message, but no current
12267 message. Redisplay the previous message. */
12268 display_last_displayed_message_p = 1;
12269 redisplay_internal ();
12270 display_last_displayed_message_p = 0;
12271 }
12272 else
12273 redisplay_internal ();
12274
12275 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12276 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12277 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12278 }
12279
12280
12281 /* Function registered with record_unwind_protect in
12282 redisplay_internal. Reset redisplaying_p to the value it had
12283 before redisplay_internal was called, and clear
12284 prevent_freeing_realized_faces_p. It also selects the previously
12285 selected frame, unless it has been deleted (by an X connection
12286 failure during redisplay, for example). */
12287
12288 static Lisp_Object
12289 unwind_redisplay (Lisp_Object val)
12290 {
12291 Lisp_Object old_redisplaying_p, old_frame;
12292
12293 old_redisplaying_p = XCAR (val);
12294 redisplaying_p = XFASTINT (old_redisplaying_p);
12295 old_frame = XCDR (val);
12296 if (! EQ (old_frame, selected_frame)
12297 && FRAME_LIVE_P (XFRAME (old_frame)))
12298 select_frame_for_redisplay (old_frame);
12299 return Qnil;
12300 }
12301
12302
12303 /* Mark the display of window W as accurate or inaccurate. If
12304 ACCURATE_P is non-zero mark display of W as accurate. If
12305 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12306 redisplay_internal is called. */
12307
12308 static void
12309 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12310 {
12311 if (BUFFERP (w->buffer))
12312 {
12313 struct buffer *b = XBUFFER (w->buffer);
12314
12315 w->last_modified
12316 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12317 w->last_overlay_modified
12318 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12319 w->last_had_star
12320 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12321
12322 if (accurate_p)
12323 {
12324 b->clip_changed = 0;
12325 b->prevent_redisplay_optimizations_p = 0;
12326
12327 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12328 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12329 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12330 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12331
12332 w->current_matrix->buffer = b;
12333 w->current_matrix->begv = BUF_BEGV (b);
12334 w->current_matrix->zv = BUF_ZV (b);
12335
12336 w->last_cursor = w->cursor;
12337 w->last_cursor_off_p = w->cursor_off_p;
12338
12339 if (w == XWINDOW (selected_window))
12340 w->last_point = make_number (BUF_PT (b));
12341 else
12342 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12343 }
12344 }
12345
12346 if (accurate_p)
12347 {
12348 w->window_end_valid = w->buffer;
12349 w->update_mode_line = Qnil;
12350 }
12351 }
12352
12353
12354 /* Mark the display of windows in the window tree rooted at WINDOW as
12355 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12356 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12357 be redisplayed the next time redisplay_internal is called. */
12358
12359 void
12360 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12361 {
12362 struct window *w;
12363
12364 for (; !NILP (window); window = w->next)
12365 {
12366 w = XWINDOW (window);
12367 mark_window_display_accurate_1 (w, accurate_p);
12368
12369 if (!NILP (w->vchild))
12370 mark_window_display_accurate (w->vchild, accurate_p);
12371 if (!NILP (w->hchild))
12372 mark_window_display_accurate (w->hchild, accurate_p);
12373 }
12374
12375 if (accurate_p)
12376 {
12377 update_overlay_arrows (1);
12378 }
12379 else
12380 {
12381 /* Force a thorough redisplay the next time by setting
12382 last_arrow_position and last_arrow_string to t, which is
12383 unequal to any useful value of Voverlay_arrow_... */
12384 update_overlay_arrows (-1);
12385 }
12386 }
12387
12388
12389 /* Return value in display table DP (Lisp_Char_Table *) for character
12390 C. Since a display table doesn't have any parent, we don't have to
12391 follow parent. Do not call this function directly but use the
12392 macro DISP_CHAR_VECTOR. */
12393
12394 Lisp_Object
12395 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12396 {
12397 Lisp_Object val;
12398
12399 if (ASCII_CHAR_P (c))
12400 {
12401 val = dp->ascii;
12402 if (SUB_CHAR_TABLE_P (val))
12403 val = XSUB_CHAR_TABLE (val)->contents[c];
12404 }
12405 else
12406 {
12407 Lisp_Object table;
12408
12409 XSETCHAR_TABLE (table, dp);
12410 val = char_table_ref (table, c);
12411 }
12412 if (NILP (val))
12413 val = dp->defalt;
12414 return val;
12415 }
12416
12417
12418 \f
12419 /***********************************************************************
12420 Window Redisplay
12421 ***********************************************************************/
12422
12423 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12424
12425 static void
12426 redisplay_windows (Lisp_Object window)
12427 {
12428 while (!NILP (window))
12429 {
12430 struct window *w = XWINDOW (window);
12431
12432 if (!NILP (w->hchild))
12433 redisplay_windows (w->hchild);
12434 else if (!NILP (w->vchild))
12435 redisplay_windows (w->vchild);
12436 else if (!NILP (w->buffer))
12437 {
12438 displayed_buffer = XBUFFER (w->buffer);
12439 /* Use list_of_error, not Qerror, so that
12440 we catch only errors and don't run the debugger. */
12441 internal_condition_case_1 (redisplay_window_0, window,
12442 list_of_error,
12443 redisplay_window_error);
12444 }
12445
12446 window = w->next;
12447 }
12448 }
12449
12450 static Lisp_Object
12451 redisplay_window_error (Lisp_Object ignore)
12452 {
12453 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12454 return Qnil;
12455 }
12456
12457 static Lisp_Object
12458 redisplay_window_0 (Lisp_Object window)
12459 {
12460 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12461 redisplay_window (window, 0);
12462 return Qnil;
12463 }
12464
12465 static Lisp_Object
12466 redisplay_window_1 (Lisp_Object window)
12467 {
12468 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12469 redisplay_window (window, 1);
12470 return Qnil;
12471 }
12472 \f
12473
12474 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12475 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12476 which positions recorded in ROW differ from current buffer
12477 positions.
12478
12479 Return 0 if cursor is not on this row, 1 otherwise. */
12480
12481 static int
12482 set_cursor_from_row (struct window *w, struct glyph_row *row,
12483 struct glyph_matrix *matrix,
12484 EMACS_INT delta, EMACS_INT delta_bytes,
12485 int dy, int dvpos)
12486 {
12487 struct glyph *glyph = row->glyphs[TEXT_AREA];
12488 struct glyph *end = glyph + row->used[TEXT_AREA];
12489 struct glyph *cursor = NULL;
12490 /* The last known character position in row. */
12491 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12492 int x = row->x;
12493 EMACS_INT pt_old = PT - delta;
12494 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12495 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12496 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12497 /* A glyph beyond the edge of TEXT_AREA which we should never
12498 touch. */
12499 struct glyph *glyphs_end = end;
12500 /* Non-zero means we've found a match for cursor position, but that
12501 glyph has the avoid_cursor_p flag set. */
12502 int match_with_avoid_cursor = 0;
12503 /* Non-zero means we've seen at least one glyph that came from a
12504 display string. */
12505 int string_seen = 0;
12506 /* Largest and smalles buffer positions seen so far during scan of
12507 glyph row. */
12508 EMACS_INT bpos_max = pos_before;
12509 EMACS_INT bpos_min = pos_after;
12510 /* Last buffer position covered by an overlay string with an integer
12511 `cursor' property. */
12512 EMACS_INT bpos_covered = 0;
12513
12514 /* Skip over glyphs not having an object at the start and the end of
12515 the row. These are special glyphs like truncation marks on
12516 terminal frames. */
12517 if (row->displays_text_p)
12518 {
12519 if (!row->reversed_p)
12520 {
12521 while (glyph < end
12522 && INTEGERP (glyph->object)
12523 && glyph->charpos < 0)
12524 {
12525 x += glyph->pixel_width;
12526 ++glyph;
12527 }
12528 while (end > glyph
12529 && INTEGERP ((end - 1)->object)
12530 /* CHARPOS is zero for blanks and stretch glyphs
12531 inserted by extend_face_to_end_of_line. */
12532 && (end - 1)->charpos <= 0)
12533 --end;
12534 glyph_before = glyph - 1;
12535 glyph_after = end;
12536 }
12537 else
12538 {
12539 struct glyph *g;
12540
12541 /* If the glyph row is reversed, we need to process it from back
12542 to front, so swap the edge pointers. */
12543 glyphs_end = end = glyph - 1;
12544 glyph += row->used[TEXT_AREA] - 1;
12545
12546 while (glyph > end + 1
12547 && INTEGERP (glyph->object)
12548 && glyph->charpos < 0)
12549 {
12550 --glyph;
12551 x -= glyph->pixel_width;
12552 }
12553 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12554 --glyph;
12555 /* By default, in reversed rows we put the cursor on the
12556 rightmost (first in the reading order) glyph. */
12557 for (g = end + 1; g < glyph; g++)
12558 x += g->pixel_width;
12559 while (end < glyph
12560 && INTEGERP ((end + 1)->object)
12561 && (end + 1)->charpos <= 0)
12562 ++end;
12563 glyph_before = glyph + 1;
12564 glyph_after = end;
12565 }
12566 }
12567 else if (row->reversed_p)
12568 {
12569 /* In R2L rows that don't display text, put the cursor on the
12570 rightmost glyph. Case in point: an empty last line that is
12571 part of an R2L paragraph. */
12572 cursor = end - 1;
12573 /* Avoid placing the cursor on the last glyph of the row, where
12574 on terminal frames we hold the vertical border between
12575 adjacent windows. */
12576 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12577 && !WINDOW_RIGHTMOST_P (w)
12578 && cursor == row->glyphs[LAST_AREA] - 1)
12579 cursor--;
12580 x = -1; /* will be computed below, at label compute_x */
12581 }
12582
12583 /* Step 1: Try to find the glyph whose character position
12584 corresponds to point. If that's not possible, find 2 glyphs
12585 whose character positions are the closest to point, one before
12586 point, the other after it. */
12587 if (!row->reversed_p)
12588 while (/* not marched to end of glyph row */
12589 glyph < end
12590 /* glyph was not inserted by redisplay for internal purposes */
12591 && !INTEGERP (glyph->object))
12592 {
12593 if (BUFFERP (glyph->object))
12594 {
12595 EMACS_INT dpos = glyph->charpos - pt_old;
12596
12597 if (glyph->charpos > bpos_max)
12598 bpos_max = glyph->charpos;
12599 if (glyph->charpos < bpos_min)
12600 bpos_min = glyph->charpos;
12601 if (!glyph->avoid_cursor_p)
12602 {
12603 /* If we hit point, we've found the glyph on which to
12604 display the cursor. */
12605 if (dpos == 0)
12606 {
12607 match_with_avoid_cursor = 0;
12608 break;
12609 }
12610 /* See if we've found a better approximation to
12611 POS_BEFORE or to POS_AFTER. Note that we want the
12612 first (leftmost) glyph of all those that are the
12613 closest from below, and the last (rightmost) of all
12614 those from above. */
12615 if (0 > dpos && dpos > pos_before - pt_old)
12616 {
12617 pos_before = glyph->charpos;
12618 glyph_before = glyph;
12619 }
12620 else if (0 < dpos && dpos <= pos_after - pt_old)
12621 {
12622 pos_after = glyph->charpos;
12623 glyph_after = glyph;
12624 }
12625 }
12626 else if (dpos == 0)
12627 match_with_avoid_cursor = 1;
12628 }
12629 else if (STRINGP (glyph->object))
12630 {
12631 Lisp_Object chprop;
12632 EMACS_INT glyph_pos = glyph->charpos;
12633
12634 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12635 glyph->object);
12636 if (INTEGERP (chprop))
12637 {
12638 bpos_covered = bpos_max + XINT (chprop);
12639 /* If the `cursor' property covers buffer positions up
12640 to and including point, we should display cursor on
12641 this glyph. Note that overlays and text properties
12642 with string values stop bidi reordering, so every
12643 buffer position to the left of the string is always
12644 smaller than any position to the right of the
12645 string. Therefore, if a `cursor' property on one
12646 of the string's characters has an integer value, we
12647 will break out of the loop below _before_ we get to
12648 the position match above. IOW, integer values of
12649 the `cursor' property override the "exact match for
12650 point" strategy of positioning the cursor. */
12651 /* Implementation note: bpos_max == pt_old when, e.g.,
12652 we are in an empty line, where bpos_max is set to
12653 MATRIX_ROW_START_CHARPOS, see above. */
12654 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12655 {
12656 cursor = glyph;
12657 break;
12658 }
12659 }
12660
12661 string_seen = 1;
12662 }
12663 x += glyph->pixel_width;
12664 ++glyph;
12665 }
12666 else if (glyph > end) /* row is reversed */
12667 while (!INTEGERP (glyph->object))
12668 {
12669 if (BUFFERP (glyph->object))
12670 {
12671 EMACS_INT dpos = glyph->charpos - pt_old;
12672
12673 if (glyph->charpos > bpos_max)
12674 bpos_max = glyph->charpos;
12675 if (glyph->charpos < bpos_min)
12676 bpos_min = glyph->charpos;
12677 if (!glyph->avoid_cursor_p)
12678 {
12679 if (dpos == 0)
12680 {
12681 match_with_avoid_cursor = 0;
12682 break;
12683 }
12684 if (0 > dpos && dpos > pos_before - pt_old)
12685 {
12686 pos_before = glyph->charpos;
12687 glyph_before = glyph;
12688 }
12689 else if (0 < dpos && dpos <= pos_after - pt_old)
12690 {
12691 pos_after = glyph->charpos;
12692 glyph_after = glyph;
12693 }
12694 }
12695 else if (dpos == 0)
12696 match_with_avoid_cursor = 1;
12697 }
12698 else if (STRINGP (glyph->object))
12699 {
12700 Lisp_Object chprop;
12701 EMACS_INT glyph_pos = glyph->charpos;
12702
12703 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12704 glyph->object);
12705 if (INTEGERP (chprop))
12706 {
12707 bpos_covered = bpos_max + XINT (chprop);
12708 /* If the `cursor' property covers buffer positions up
12709 to and including point, we should display cursor on
12710 this glyph. */
12711 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12712 {
12713 cursor = glyph;
12714 break;
12715 }
12716 }
12717 string_seen = 1;
12718 }
12719 --glyph;
12720 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12721 {
12722 x--; /* can't use any pixel_width */
12723 break;
12724 }
12725 x -= glyph->pixel_width;
12726 }
12727
12728 /* Step 2: If we didn't find an exact match for point, we need to
12729 look for a proper place to put the cursor among glyphs between
12730 GLYPH_BEFORE and GLYPH_AFTER. */
12731 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12732 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12733 && bpos_covered < pt_old)
12734 {
12735 /* An empty line has a single glyph whose OBJECT is zero and
12736 whose CHARPOS is the position of a newline on that line.
12737 Note that on a TTY, there are more glyphs after that, which
12738 were produced by extend_face_to_end_of_line, but their
12739 CHARPOS is zero or negative. */
12740 int empty_line_p =
12741 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12742 && INTEGERP (glyph->object) && glyph->charpos > 0;
12743
12744 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12745 {
12746 EMACS_INT ellipsis_pos;
12747
12748 /* Scan back over the ellipsis glyphs. */
12749 if (!row->reversed_p)
12750 {
12751 ellipsis_pos = (glyph - 1)->charpos;
12752 while (glyph > row->glyphs[TEXT_AREA]
12753 && (glyph - 1)->charpos == ellipsis_pos)
12754 glyph--, x -= glyph->pixel_width;
12755 /* That loop always goes one position too far, including
12756 the glyph before the ellipsis. So scan forward over
12757 that one. */
12758 x += glyph->pixel_width;
12759 glyph++;
12760 }
12761 else /* row is reversed */
12762 {
12763 ellipsis_pos = (glyph + 1)->charpos;
12764 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12765 && (glyph + 1)->charpos == ellipsis_pos)
12766 glyph++, x += glyph->pixel_width;
12767 x -= glyph->pixel_width;
12768 glyph--;
12769 }
12770 }
12771 else if (match_with_avoid_cursor
12772 /* A truncated row may not include PT among its
12773 character positions. Setting the cursor inside the
12774 scroll margin will trigger recalculation of hscroll
12775 in hscroll_window_tree. */
12776 || (row->truncated_on_left_p && pt_old < bpos_min)
12777 || (row->truncated_on_right_p && pt_old > bpos_max)
12778 /* Zero-width characters produce no glyphs. */
12779 || (!string_seen
12780 && !empty_line_p
12781 && (row->reversed_p
12782 ? glyph_after > glyphs_end
12783 : glyph_after < glyphs_end)))
12784 {
12785 cursor = glyph_after;
12786 x = -1;
12787 }
12788 else if (string_seen)
12789 {
12790 int incr = row->reversed_p ? -1 : +1;
12791
12792 /* Need to find the glyph that came out of a string which is
12793 present at point. That glyph is somewhere between
12794 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12795 positioned between POS_BEFORE and POS_AFTER in the
12796 buffer. */
12797 struct glyph *start, *stop;
12798 EMACS_INT pos = pos_before;
12799
12800 x = -1;
12801
12802 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
12803 correspond to POS_BEFORE and POS_AFTER, respectively. We
12804 need START and STOP in the order that corresponds to the
12805 row's direction as given by its reversed_p flag. If the
12806 directionality of characters between POS_BEFORE and
12807 POS_AFTER is the opposite of the row's base direction,
12808 these characters will have been reordered for display,
12809 and we need to reverse START and STOP. */
12810 if (!row->reversed_p)
12811 {
12812 start = min (glyph_before, glyph_after);
12813 stop = max (glyph_before, glyph_after);
12814 }
12815 else
12816 {
12817 start = max (glyph_before, glyph_after);
12818 stop = min (glyph_before, glyph_after);
12819 }
12820 for (glyph = start + incr;
12821 row->reversed_p ? glyph > stop : glyph < stop; )
12822 {
12823
12824 /* Any glyphs that come from the buffer are here because
12825 of bidi reordering. Skip them, and only pay
12826 attention to glyphs that came from some string. */
12827 if (STRINGP (glyph->object))
12828 {
12829 Lisp_Object str;
12830 EMACS_INT tem;
12831
12832 str = glyph->object;
12833 tem = string_buffer_position_lim (str, pos, pos_after, 0);
12834 if (tem == 0 /* from overlay */
12835 || pos <= tem)
12836 {
12837 /* If the string from which this glyph came is
12838 found in the buffer at point, then we've
12839 found the glyph we've been looking for. If
12840 it comes from an overlay (tem == 0), and it
12841 has the `cursor' property on one of its
12842 glyphs, record that glyph as a candidate for
12843 displaying the cursor. (As in the
12844 unidirectional version, we will display the
12845 cursor on the last candidate we find.) */
12846 if (tem == 0 || tem == pt_old)
12847 {
12848 /* The glyphs from this string could have
12849 been reordered. Find the one with the
12850 smallest string position. Or there could
12851 be a character in the string with the
12852 `cursor' property, which means display
12853 cursor on that character's glyph. */
12854 EMACS_INT strpos = glyph->charpos;
12855
12856 if (tem)
12857 cursor = glyph;
12858 for ( ;
12859 (row->reversed_p ? glyph > stop : glyph < stop)
12860 && EQ (glyph->object, str);
12861 glyph += incr)
12862 {
12863 Lisp_Object cprop;
12864 EMACS_INT gpos = glyph->charpos;
12865
12866 cprop = Fget_char_property (make_number (gpos),
12867 Qcursor,
12868 glyph->object);
12869 if (!NILP (cprop))
12870 {
12871 cursor = glyph;
12872 break;
12873 }
12874 if (tem && glyph->charpos < strpos)
12875 {
12876 strpos = glyph->charpos;
12877 cursor = glyph;
12878 }
12879 }
12880
12881 if (tem == pt_old)
12882 goto compute_x;
12883 }
12884 if (tem)
12885 pos = tem + 1; /* don't find previous instances */
12886 }
12887 /* This string is not what we want; skip all of the
12888 glyphs that came from it. */
12889 while ((row->reversed_p ? glyph > stop : glyph < stop)
12890 && EQ (glyph->object, str))
12891 glyph += incr;
12892 }
12893 else
12894 glyph += incr;
12895 }
12896
12897 /* If we reached the end of the line, and END was from a string,
12898 the cursor is not on this line. */
12899 if (cursor == NULL
12900 && (row->reversed_p ? glyph <= end : glyph >= end)
12901 && STRINGP (end->object)
12902 && row->continued_p)
12903 return 0;
12904 }
12905 }
12906
12907 compute_x:
12908 if (cursor != NULL)
12909 glyph = cursor;
12910 if (x < 0)
12911 {
12912 struct glyph *g;
12913
12914 /* Need to compute x that corresponds to GLYPH. */
12915 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12916 {
12917 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12918 abort ();
12919 x += g->pixel_width;
12920 }
12921 }
12922
12923 /* ROW could be part of a continued line, which, under bidi
12924 reordering, might have other rows whose start and end charpos
12925 occlude point. Only set w->cursor if we found a better
12926 approximation to the cursor position than we have from previously
12927 examined candidate rows belonging to the same continued line. */
12928 if (/* we already have a candidate row */
12929 w->cursor.vpos >= 0
12930 /* that candidate is not the row we are processing */
12931 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12932 /* the row we are processing is part of a continued line */
12933 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
12934 /* Make sure cursor.vpos specifies a row whose start and end
12935 charpos occlude point. This is because some callers of this
12936 function leave cursor.vpos at the row where the cursor was
12937 displayed during the last redisplay cycle. */
12938 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12939 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12940 {
12941 struct glyph *g1 =
12942 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12943
12944 /* Don't consider glyphs that are outside TEXT_AREA. */
12945 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
12946 return 0;
12947 /* Keep the candidate whose buffer position is the closest to
12948 point. */
12949 if (/* previous candidate is a glyph in TEXT_AREA of that row */
12950 w->cursor.hpos >= 0
12951 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
12952 && BUFFERP (g1->object)
12953 && (g1->charpos == pt_old /* an exact match always wins */
12954 || (BUFFERP (glyph->object)
12955 && eabs (g1->charpos - pt_old)
12956 < eabs (glyph->charpos - pt_old))))
12957 return 0;
12958 /* If this candidate gives an exact match, use that. */
12959 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12960 /* Otherwise, keep the candidate that comes from a row
12961 spanning less buffer positions. This may win when one or
12962 both candidate positions are on glyphs that came from
12963 display strings, for which we cannot compare buffer
12964 positions. */
12965 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12966 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12967 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12968 return 0;
12969 }
12970 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12971 w->cursor.x = x;
12972 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12973 w->cursor.y = row->y + dy;
12974
12975 if (w == XWINDOW (selected_window))
12976 {
12977 if (!row->continued_p
12978 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12979 && row->x == 0)
12980 {
12981 this_line_buffer = XBUFFER (w->buffer);
12982
12983 CHARPOS (this_line_start_pos)
12984 = MATRIX_ROW_START_CHARPOS (row) + delta;
12985 BYTEPOS (this_line_start_pos)
12986 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12987
12988 CHARPOS (this_line_end_pos)
12989 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12990 BYTEPOS (this_line_end_pos)
12991 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12992
12993 this_line_y = w->cursor.y;
12994 this_line_pixel_height = row->height;
12995 this_line_vpos = w->cursor.vpos;
12996 this_line_start_x = row->x;
12997 }
12998 else
12999 CHARPOS (this_line_start_pos) = 0;
13000 }
13001
13002 return 1;
13003 }
13004
13005
13006 /* Run window scroll functions, if any, for WINDOW with new window
13007 start STARTP. Sets the window start of WINDOW to that position.
13008
13009 We assume that the window's buffer is really current. */
13010
13011 static inline struct text_pos
13012 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13013 {
13014 struct window *w = XWINDOW (window);
13015 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13016
13017 if (current_buffer != XBUFFER (w->buffer))
13018 abort ();
13019
13020 if (!NILP (Vwindow_scroll_functions))
13021 {
13022 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13023 make_number (CHARPOS (startp)));
13024 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13025 /* In case the hook functions switch buffers. */
13026 if (current_buffer != XBUFFER (w->buffer))
13027 set_buffer_internal_1 (XBUFFER (w->buffer));
13028 }
13029
13030 return startp;
13031 }
13032
13033
13034 /* Make sure the line containing the cursor is fully visible.
13035 A value of 1 means there is nothing to be done.
13036 (Either the line is fully visible, or it cannot be made so,
13037 or we cannot tell.)
13038
13039 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13040 is higher than window.
13041
13042 A value of 0 means the caller should do scrolling
13043 as if point had gone off the screen. */
13044
13045 static int
13046 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13047 {
13048 struct glyph_matrix *matrix;
13049 struct glyph_row *row;
13050 int window_height;
13051
13052 if (!make_cursor_line_fully_visible_p)
13053 return 1;
13054
13055 /* It's not always possible to find the cursor, e.g, when a window
13056 is full of overlay strings. Don't do anything in that case. */
13057 if (w->cursor.vpos < 0)
13058 return 1;
13059
13060 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13061 row = MATRIX_ROW (matrix, w->cursor.vpos);
13062
13063 /* If the cursor row is not partially visible, there's nothing to do. */
13064 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13065 return 1;
13066
13067 /* If the row the cursor is in is taller than the window's height,
13068 it's not clear what to do, so do nothing. */
13069 window_height = window_box_height (w);
13070 if (row->height >= window_height)
13071 {
13072 if (!force_p || MINI_WINDOW_P (w)
13073 || w->vscroll || w->cursor.vpos == 0)
13074 return 1;
13075 }
13076 return 0;
13077 }
13078
13079
13080 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13081 non-zero means only WINDOW is redisplayed in redisplay_internal.
13082 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13083 in redisplay_window to bring a partially visible line into view in
13084 the case that only the cursor has moved.
13085
13086 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13087 last screen line's vertical height extends past the end of the screen.
13088
13089 Value is
13090
13091 1 if scrolling succeeded
13092
13093 0 if scrolling didn't find point.
13094
13095 -1 if new fonts have been loaded so that we must interrupt
13096 redisplay, adjust glyph matrices, and try again. */
13097
13098 enum
13099 {
13100 SCROLLING_SUCCESS,
13101 SCROLLING_FAILED,
13102 SCROLLING_NEED_LARGER_MATRICES
13103 };
13104
13105 /* If scroll-conservatively is more than this, never recenter.
13106
13107 If you change this, don't forget to update the doc string of
13108 `scroll-conservatively' and the Emacs manual. */
13109 #define SCROLL_LIMIT 100
13110
13111 static int
13112 try_scrolling (Lisp_Object window, int just_this_one_p,
13113 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13114 int temp_scroll_step, int last_line_misfit)
13115 {
13116 struct window *w = XWINDOW (window);
13117 struct frame *f = XFRAME (w->frame);
13118 struct text_pos pos, startp;
13119 struct it it;
13120 int this_scroll_margin, scroll_max, rc, height;
13121 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13122 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13123 Lisp_Object aggressive;
13124 /* We will never try scrolling more than this number of lines. */
13125 int scroll_limit = SCROLL_LIMIT;
13126
13127 #if GLYPH_DEBUG
13128 debug_method_add (w, "try_scrolling");
13129 #endif
13130
13131 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13132
13133 /* Compute scroll margin height in pixels. We scroll when point is
13134 within this distance from the top or bottom of the window. */
13135 if (scroll_margin > 0)
13136 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13137 * FRAME_LINE_HEIGHT (f);
13138 else
13139 this_scroll_margin = 0;
13140
13141 /* Force arg_scroll_conservatively to have a reasonable value, to
13142 avoid scrolling too far away with slow move_it_* functions. Note
13143 that the user can supply scroll-conservatively equal to
13144 `most-positive-fixnum', which can be larger than INT_MAX. */
13145 if (arg_scroll_conservatively > scroll_limit)
13146 {
13147 arg_scroll_conservatively = scroll_limit + 1;
13148 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13149 }
13150 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13151 /* Compute how much we should try to scroll maximally to bring
13152 point into view. */
13153 scroll_max = (max (scroll_step,
13154 max (arg_scroll_conservatively, temp_scroll_step))
13155 * FRAME_LINE_HEIGHT (f));
13156 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13157 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13158 /* We're trying to scroll because of aggressive scrolling but no
13159 scroll_step is set. Choose an arbitrary one. */
13160 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13161 else
13162 scroll_max = 0;
13163
13164 too_near_end:
13165
13166 /* Decide whether to scroll down. */
13167 if (PT > CHARPOS (startp))
13168 {
13169 int scroll_margin_y;
13170
13171 /* Compute the pixel ypos of the scroll margin, then move it to
13172 either that ypos or PT, whichever comes first. */
13173 start_display (&it, w, startp);
13174 scroll_margin_y = it.last_visible_y - this_scroll_margin
13175 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13176 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13177 (MOVE_TO_POS | MOVE_TO_Y));
13178
13179 if (PT > CHARPOS (it.current.pos))
13180 {
13181 int y0 = line_bottom_y (&it);
13182 /* Compute how many pixels below window bottom to stop searching
13183 for PT. This avoids costly search for PT that is far away if
13184 the user limited scrolling by a small number of lines, but
13185 always finds PT if scroll_conservatively is set to a large
13186 number, such as most-positive-fixnum. */
13187 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13188 int y_to_move = it.last_visible_y + slack;
13189
13190 /* Compute the distance from the scroll margin to PT or to
13191 the scroll limit, whichever comes first. This should
13192 include the height of the cursor line, to make that line
13193 fully visible. */
13194 move_it_to (&it, PT, -1, y_to_move,
13195 -1, MOVE_TO_POS | MOVE_TO_Y);
13196 dy = line_bottom_y (&it) - y0;
13197
13198 if (dy > scroll_max)
13199 return SCROLLING_FAILED;
13200
13201 scroll_down_p = 1;
13202 }
13203 }
13204
13205 if (scroll_down_p)
13206 {
13207 /* Point is in or below the bottom scroll margin, so move the
13208 window start down. If scrolling conservatively, move it just
13209 enough down to make point visible. If scroll_step is set,
13210 move it down by scroll_step. */
13211 if (arg_scroll_conservatively)
13212 amount_to_scroll
13213 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13214 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13215 else if (scroll_step || temp_scroll_step)
13216 amount_to_scroll = scroll_max;
13217 else
13218 {
13219 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13220 height = WINDOW_BOX_TEXT_HEIGHT (w);
13221 if (NUMBERP (aggressive))
13222 {
13223 double float_amount = XFLOATINT (aggressive) * height;
13224 amount_to_scroll = float_amount;
13225 if (amount_to_scroll == 0 && float_amount > 0)
13226 amount_to_scroll = 1;
13227 /* Don't let point enter the scroll margin near top of
13228 the window. */
13229 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13230 amount_to_scroll = height - 2*this_scroll_margin + dy;
13231 }
13232 }
13233
13234 if (amount_to_scroll <= 0)
13235 return SCROLLING_FAILED;
13236
13237 start_display (&it, w, startp);
13238 if (arg_scroll_conservatively <= scroll_limit)
13239 move_it_vertically (&it, amount_to_scroll);
13240 else
13241 {
13242 /* Extra precision for users who set scroll-conservatively
13243 to a large number: make sure the amount we scroll
13244 the window start is never less than amount_to_scroll,
13245 which was computed as distance from window bottom to
13246 point. This matters when lines at window top and lines
13247 below window bottom have different height. */
13248 struct it it1 = it;
13249 /* We use a temporary it1 because line_bottom_y can modify
13250 its argument, if it moves one line down; see there. */
13251 int start_y = line_bottom_y (&it1);
13252
13253 do {
13254 move_it_by_lines (&it, 1);
13255 it1 = it;
13256 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13257 }
13258
13259 /* If STARTP is unchanged, move it down another screen line. */
13260 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13261 move_it_by_lines (&it, 1);
13262 startp = it.current.pos;
13263 }
13264 else
13265 {
13266 struct text_pos scroll_margin_pos = startp;
13267
13268 /* See if point is inside the scroll margin at the top of the
13269 window. */
13270 if (this_scroll_margin)
13271 {
13272 start_display (&it, w, startp);
13273 move_it_vertically (&it, this_scroll_margin);
13274 scroll_margin_pos = it.current.pos;
13275 }
13276
13277 if (PT < CHARPOS (scroll_margin_pos))
13278 {
13279 /* Point is in the scroll margin at the top of the window or
13280 above what is displayed in the window. */
13281 int y0, y_to_move;
13282
13283 /* Compute the vertical distance from PT to the scroll
13284 margin position. Move as far as scroll_max allows, or
13285 one screenful, or 10 screen lines, whichever is largest.
13286 Give up if distance is greater than scroll_max. */
13287 SET_TEXT_POS (pos, PT, PT_BYTE);
13288 start_display (&it, w, pos);
13289 y0 = it.current_y;
13290 y_to_move = max (it.last_visible_y,
13291 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13292 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13293 y_to_move, -1,
13294 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13295 dy = it.current_y - y0;
13296 if (dy > scroll_max)
13297 return SCROLLING_FAILED;
13298
13299 /* Compute new window start. */
13300 start_display (&it, w, startp);
13301
13302 if (arg_scroll_conservatively)
13303 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13304 max (scroll_step, temp_scroll_step));
13305 else if (scroll_step || temp_scroll_step)
13306 amount_to_scroll = scroll_max;
13307 else
13308 {
13309 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13310 height = WINDOW_BOX_TEXT_HEIGHT (w);
13311 if (NUMBERP (aggressive))
13312 {
13313 double float_amount = XFLOATINT (aggressive) * height;
13314 amount_to_scroll = float_amount;
13315 if (amount_to_scroll == 0 && float_amount > 0)
13316 amount_to_scroll = 1;
13317 amount_to_scroll -=
13318 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13319 /* Don't let point enter the scroll margin near
13320 bottom of the window. */
13321 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13322 amount_to_scroll = height - 2*this_scroll_margin + dy;
13323 }
13324 }
13325
13326 if (amount_to_scroll <= 0)
13327 return SCROLLING_FAILED;
13328
13329 move_it_vertically_backward (&it, amount_to_scroll);
13330 startp = it.current.pos;
13331 }
13332 }
13333
13334 /* Run window scroll functions. */
13335 startp = run_window_scroll_functions (window, startp);
13336
13337 /* Display the window. Give up if new fonts are loaded, or if point
13338 doesn't appear. */
13339 if (!try_window (window, startp, 0))
13340 rc = SCROLLING_NEED_LARGER_MATRICES;
13341 else if (w->cursor.vpos < 0)
13342 {
13343 clear_glyph_matrix (w->desired_matrix);
13344 rc = SCROLLING_FAILED;
13345 }
13346 else
13347 {
13348 /* Maybe forget recorded base line for line number display. */
13349 if (!just_this_one_p
13350 || current_buffer->clip_changed
13351 || BEG_UNCHANGED < CHARPOS (startp))
13352 w->base_line_number = Qnil;
13353
13354 /* If cursor ends up on a partially visible line,
13355 treat that as being off the bottom of the screen. */
13356 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13357 /* It's possible that the cursor is on the first line of the
13358 buffer, which is partially obscured due to a vscroll
13359 (Bug#7537). In that case, avoid looping forever . */
13360 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13361 {
13362 clear_glyph_matrix (w->desired_matrix);
13363 ++extra_scroll_margin_lines;
13364 goto too_near_end;
13365 }
13366 rc = SCROLLING_SUCCESS;
13367 }
13368
13369 return rc;
13370 }
13371
13372
13373 /* Compute a suitable window start for window W if display of W starts
13374 on a continuation line. Value is non-zero if a new window start
13375 was computed.
13376
13377 The new window start will be computed, based on W's width, starting
13378 from the start of the continued line. It is the start of the
13379 screen line with the minimum distance from the old start W->start. */
13380
13381 static int
13382 compute_window_start_on_continuation_line (struct window *w)
13383 {
13384 struct text_pos pos, start_pos;
13385 int window_start_changed_p = 0;
13386
13387 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13388
13389 /* If window start is on a continuation line... Window start may be
13390 < BEGV in case there's invisible text at the start of the
13391 buffer (M-x rmail, for example). */
13392 if (CHARPOS (start_pos) > BEGV
13393 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13394 {
13395 struct it it;
13396 struct glyph_row *row;
13397
13398 /* Handle the case that the window start is out of range. */
13399 if (CHARPOS (start_pos) < BEGV)
13400 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13401 else if (CHARPOS (start_pos) > ZV)
13402 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13403
13404 /* Find the start of the continued line. This should be fast
13405 because scan_buffer is fast (newline cache). */
13406 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13407 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13408 row, DEFAULT_FACE_ID);
13409 reseat_at_previous_visible_line_start (&it);
13410
13411 /* If the line start is "too far" away from the window start,
13412 say it takes too much time to compute a new window start. */
13413 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13414 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13415 {
13416 int min_distance, distance;
13417
13418 /* Move forward by display lines to find the new window
13419 start. If window width was enlarged, the new start can
13420 be expected to be > the old start. If window width was
13421 decreased, the new window start will be < the old start.
13422 So, we're looking for the display line start with the
13423 minimum distance from the old window start. */
13424 pos = it.current.pos;
13425 min_distance = INFINITY;
13426 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13427 distance < min_distance)
13428 {
13429 min_distance = distance;
13430 pos = it.current.pos;
13431 move_it_by_lines (&it, 1);
13432 }
13433
13434 /* Set the window start there. */
13435 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13436 window_start_changed_p = 1;
13437 }
13438 }
13439
13440 return window_start_changed_p;
13441 }
13442
13443
13444 /* Try cursor movement in case text has not changed in window WINDOW,
13445 with window start STARTP. Value is
13446
13447 CURSOR_MOVEMENT_SUCCESS if successful
13448
13449 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13450
13451 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13452 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13453 we want to scroll as if scroll-step were set to 1. See the code.
13454
13455 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13456 which case we have to abort this redisplay, and adjust matrices
13457 first. */
13458
13459 enum
13460 {
13461 CURSOR_MOVEMENT_SUCCESS,
13462 CURSOR_MOVEMENT_CANNOT_BE_USED,
13463 CURSOR_MOVEMENT_MUST_SCROLL,
13464 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13465 };
13466
13467 static int
13468 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13469 {
13470 struct window *w = XWINDOW (window);
13471 struct frame *f = XFRAME (w->frame);
13472 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13473
13474 #if GLYPH_DEBUG
13475 if (inhibit_try_cursor_movement)
13476 return rc;
13477 #endif
13478
13479 /* Handle case where text has not changed, only point, and it has
13480 not moved off the frame. */
13481 if (/* Point may be in this window. */
13482 PT >= CHARPOS (startp)
13483 /* Selective display hasn't changed. */
13484 && !current_buffer->clip_changed
13485 /* Function force-mode-line-update is used to force a thorough
13486 redisplay. It sets either windows_or_buffers_changed or
13487 update_mode_lines. So don't take a shortcut here for these
13488 cases. */
13489 && !update_mode_lines
13490 && !windows_or_buffers_changed
13491 && !cursor_type_changed
13492 /* Can't use this case if highlighting a region. When a
13493 region exists, cursor movement has to do more than just
13494 set the cursor. */
13495 && !(!NILP (Vtransient_mark_mode)
13496 && !NILP (BVAR (current_buffer, mark_active)))
13497 && NILP (w->region_showing)
13498 && NILP (Vshow_trailing_whitespace)
13499 /* Right after splitting windows, last_point may be nil. */
13500 && INTEGERP (w->last_point)
13501 /* This code is not used for mini-buffer for the sake of the case
13502 of redisplaying to replace an echo area message; since in
13503 that case the mini-buffer contents per se are usually
13504 unchanged. This code is of no real use in the mini-buffer
13505 since the handling of this_line_start_pos, etc., in redisplay
13506 handles the same cases. */
13507 && !EQ (window, minibuf_window)
13508 /* When splitting windows or for new windows, it happens that
13509 redisplay is called with a nil window_end_vpos or one being
13510 larger than the window. This should really be fixed in
13511 window.c. I don't have this on my list, now, so we do
13512 approximately the same as the old redisplay code. --gerd. */
13513 && INTEGERP (w->window_end_vpos)
13514 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13515 && (FRAME_WINDOW_P (f)
13516 || !overlay_arrow_in_current_buffer_p ()))
13517 {
13518 int this_scroll_margin, top_scroll_margin;
13519 struct glyph_row *row = NULL;
13520
13521 #if GLYPH_DEBUG
13522 debug_method_add (w, "cursor movement");
13523 #endif
13524
13525 /* Scroll if point within this distance from the top or bottom
13526 of the window. This is a pixel value. */
13527 if (scroll_margin > 0)
13528 {
13529 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13530 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13531 }
13532 else
13533 this_scroll_margin = 0;
13534
13535 top_scroll_margin = this_scroll_margin;
13536 if (WINDOW_WANTS_HEADER_LINE_P (w))
13537 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13538
13539 /* Start with the row the cursor was displayed during the last
13540 not paused redisplay. Give up if that row is not valid. */
13541 if (w->last_cursor.vpos < 0
13542 || w->last_cursor.vpos >= w->current_matrix->nrows)
13543 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13544 else
13545 {
13546 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13547 if (row->mode_line_p)
13548 ++row;
13549 if (!row->enabled_p)
13550 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13551 }
13552
13553 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13554 {
13555 int scroll_p = 0, must_scroll = 0;
13556 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13557
13558 if (PT > XFASTINT (w->last_point))
13559 {
13560 /* Point has moved forward. */
13561 while (MATRIX_ROW_END_CHARPOS (row) < PT
13562 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13563 {
13564 xassert (row->enabled_p);
13565 ++row;
13566 }
13567
13568 /* If the end position of a row equals the start
13569 position of the next row, and PT is at that position,
13570 we would rather display cursor in the next line. */
13571 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13572 && MATRIX_ROW_END_CHARPOS (row) == PT
13573 && row < w->current_matrix->rows
13574 + w->current_matrix->nrows - 1
13575 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13576 && !cursor_row_p (row))
13577 ++row;
13578
13579 /* If within the scroll margin, scroll. Note that
13580 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13581 the next line would be drawn, and that
13582 this_scroll_margin can be zero. */
13583 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13584 || PT > MATRIX_ROW_END_CHARPOS (row)
13585 /* Line is completely visible last line in window
13586 and PT is to be set in the next line. */
13587 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13588 && PT == MATRIX_ROW_END_CHARPOS (row)
13589 && !row->ends_at_zv_p
13590 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13591 scroll_p = 1;
13592 }
13593 else if (PT < XFASTINT (w->last_point))
13594 {
13595 /* Cursor has to be moved backward. Note that PT >=
13596 CHARPOS (startp) because of the outer if-statement. */
13597 while (!row->mode_line_p
13598 && (MATRIX_ROW_START_CHARPOS (row) > PT
13599 || (MATRIX_ROW_START_CHARPOS (row) == PT
13600 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13601 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13602 row > w->current_matrix->rows
13603 && (row-1)->ends_in_newline_from_string_p))))
13604 && (row->y > top_scroll_margin
13605 || CHARPOS (startp) == BEGV))
13606 {
13607 xassert (row->enabled_p);
13608 --row;
13609 }
13610
13611 /* Consider the following case: Window starts at BEGV,
13612 there is invisible, intangible text at BEGV, so that
13613 display starts at some point START > BEGV. It can
13614 happen that we are called with PT somewhere between
13615 BEGV and START. Try to handle that case. */
13616 if (row < w->current_matrix->rows
13617 || row->mode_line_p)
13618 {
13619 row = w->current_matrix->rows;
13620 if (row->mode_line_p)
13621 ++row;
13622 }
13623
13624 /* Due to newlines in overlay strings, we may have to
13625 skip forward over overlay strings. */
13626 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13627 && MATRIX_ROW_END_CHARPOS (row) == PT
13628 && !cursor_row_p (row))
13629 ++row;
13630
13631 /* If within the scroll margin, scroll. */
13632 if (row->y < top_scroll_margin
13633 && CHARPOS (startp) != BEGV)
13634 scroll_p = 1;
13635 }
13636 else
13637 {
13638 /* Cursor did not move. So don't scroll even if cursor line
13639 is partially visible, as it was so before. */
13640 rc = CURSOR_MOVEMENT_SUCCESS;
13641 }
13642
13643 if (PT < MATRIX_ROW_START_CHARPOS (row)
13644 || PT > MATRIX_ROW_END_CHARPOS (row))
13645 {
13646 /* if PT is not in the glyph row, give up. */
13647 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13648 must_scroll = 1;
13649 }
13650 else if (rc != CURSOR_MOVEMENT_SUCCESS
13651 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
13652 {
13653 /* If rows are bidi-reordered and point moved, back up
13654 until we find a row that does not belong to a
13655 continuation line. This is because we must consider
13656 all rows of a continued line as candidates for the
13657 new cursor positioning, since row start and end
13658 positions change non-linearly with vertical position
13659 in such rows. */
13660 /* FIXME: Revisit this when glyph ``spilling'' in
13661 continuation lines' rows is implemented for
13662 bidi-reordered rows. */
13663 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13664 {
13665 xassert (row->enabled_p);
13666 --row;
13667 /* If we hit the beginning of the displayed portion
13668 without finding the first row of a continued
13669 line, give up. */
13670 if (row <= w->current_matrix->rows)
13671 {
13672 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13673 break;
13674 }
13675
13676 }
13677 }
13678 if (must_scroll)
13679 ;
13680 else if (rc != CURSOR_MOVEMENT_SUCCESS
13681 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13682 && make_cursor_line_fully_visible_p)
13683 {
13684 if (PT == MATRIX_ROW_END_CHARPOS (row)
13685 && !row->ends_at_zv_p
13686 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13687 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13688 else if (row->height > window_box_height (w))
13689 {
13690 /* If we end up in a partially visible line, let's
13691 make it fully visible, except when it's taller
13692 than the window, in which case we can't do much
13693 about it. */
13694 *scroll_step = 1;
13695 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13696 }
13697 else
13698 {
13699 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13700 if (!cursor_row_fully_visible_p (w, 0, 1))
13701 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13702 else
13703 rc = CURSOR_MOVEMENT_SUCCESS;
13704 }
13705 }
13706 else if (scroll_p)
13707 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13708 else if (rc != CURSOR_MOVEMENT_SUCCESS
13709 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
13710 {
13711 /* With bidi-reordered rows, there could be more than
13712 one candidate row whose start and end positions
13713 occlude point. We need to let set_cursor_from_row
13714 find the best candidate. */
13715 /* FIXME: Revisit this when glyph ``spilling'' in
13716 continuation lines' rows is implemented for
13717 bidi-reordered rows. */
13718 int rv = 0;
13719
13720 do
13721 {
13722 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13723 && PT <= MATRIX_ROW_END_CHARPOS (row)
13724 && cursor_row_p (row))
13725 rv |= set_cursor_from_row (w, row, w->current_matrix,
13726 0, 0, 0, 0);
13727 /* As soon as we've found the first suitable row
13728 whose ends_at_zv_p flag is set, we are done. */
13729 if (rv
13730 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13731 {
13732 rc = CURSOR_MOVEMENT_SUCCESS;
13733 break;
13734 }
13735 ++row;
13736 }
13737 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13738 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13739 || (MATRIX_ROW_START_CHARPOS (row) == PT
13740 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13741 /* If we didn't find any candidate rows, or exited the
13742 loop before all the candidates were examined, signal
13743 to the caller that this method failed. */
13744 if (rc != CURSOR_MOVEMENT_SUCCESS
13745 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13746 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13747 else if (rv)
13748 rc = CURSOR_MOVEMENT_SUCCESS;
13749 }
13750 else
13751 {
13752 do
13753 {
13754 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13755 {
13756 rc = CURSOR_MOVEMENT_SUCCESS;
13757 break;
13758 }
13759 ++row;
13760 }
13761 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13762 && MATRIX_ROW_START_CHARPOS (row) == PT
13763 && cursor_row_p (row));
13764 }
13765 }
13766 }
13767
13768 return rc;
13769 }
13770
13771 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
13772 static
13773 #endif
13774 void
13775 set_vertical_scroll_bar (struct window *w)
13776 {
13777 EMACS_INT start, end, whole;
13778
13779 /* Calculate the start and end positions for the current window.
13780 At some point, it would be nice to choose between scrollbars
13781 which reflect the whole buffer size, with special markers
13782 indicating narrowing, and scrollbars which reflect only the
13783 visible region.
13784
13785 Note that mini-buffers sometimes aren't displaying any text. */
13786 if (!MINI_WINDOW_P (w)
13787 || (w == XWINDOW (minibuf_window)
13788 && NILP (echo_area_buffer[0])))
13789 {
13790 struct buffer *buf = XBUFFER (w->buffer);
13791 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13792 start = marker_position (w->start) - BUF_BEGV (buf);
13793 /* I don't think this is guaranteed to be right. For the
13794 moment, we'll pretend it is. */
13795 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13796
13797 if (end < start)
13798 end = start;
13799 if (whole < (end - start))
13800 whole = end - start;
13801 }
13802 else
13803 start = end = whole = 0;
13804
13805 /* Indicate what this scroll bar ought to be displaying now. */
13806 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13807 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13808 (w, end - start, whole, start);
13809 }
13810
13811
13812 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13813 selected_window is redisplayed.
13814
13815 We can return without actually redisplaying the window if
13816 fonts_changed_p is nonzero. In that case, redisplay_internal will
13817 retry. */
13818
13819 static void
13820 redisplay_window (Lisp_Object window, int just_this_one_p)
13821 {
13822 struct window *w = XWINDOW (window);
13823 struct frame *f = XFRAME (w->frame);
13824 struct buffer *buffer = XBUFFER (w->buffer);
13825 struct buffer *old = current_buffer;
13826 struct text_pos lpoint, opoint, startp;
13827 int update_mode_line;
13828 int tem;
13829 struct it it;
13830 /* Record it now because it's overwritten. */
13831 int current_matrix_up_to_date_p = 0;
13832 int used_current_matrix_p = 0;
13833 /* This is less strict than current_matrix_up_to_date_p.
13834 It indictes that the buffer contents and narrowing are unchanged. */
13835 int buffer_unchanged_p = 0;
13836 int temp_scroll_step = 0;
13837 int count = SPECPDL_INDEX ();
13838 int rc;
13839 int centering_position = -1;
13840 int last_line_misfit = 0;
13841 EMACS_INT beg_unchanged, end_unchanged;
13842
13843 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13844 opoint = lpoint;
13845
13846 /* W must be a leaf window here. */
13847 xassert (!NILP (w->buffer));
13848 #if GLYPH_DEBUG
13849 *w->desired_matrix->method = 0;
13850 #endif
13851
13852 restart:
13853 reconsider_clip_changes (w, buffer);
13854
13855 /* Has the mode line to be updated? */
13856 update_mode_line = (!NILP (w->update_mode_line)
13857 || update_mode_lines
13858 || buffer->clip_changed
13859 || buffer->prevent_redisplay_optimizations_p);
13860
13861 if (MINI_WINDOW_P (w))
13862 {
13863 if (w == XWINDOW (echo_area_window)
13864 && !NILP (echo_area_buffer[0]))
13865 {
13866 if (update_mode_line)
13867 /* We may have to update a tty frame's menu bar or a
13868 tool-bar. Example `M-x C-h C-h C-g'. */
13869 goto finish_menu_bars;
13870 else
13871 /* We've already displayed the echo area glyphs in this window. */
13872 goto finish_scroll_bars;
13873 }
13874 else if ((w != XWINDOW (minibuf_window)
13875 || minibuf_level == 0)
13876 /* When buffer is nonempty, redisplay window normally. */
13877 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13878 /* Quail displays non-mini buffers in minibuffer window.
13879 In that case, redisplay the window normally. */
13880 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13881 {
13882 /* W is a mini-buffer window, but it's not active, so clear
13883 it. */
13884 int yb = window_text_bottom_y (w);
13885 struct glyph_row *row;
13886 int y;
13887
13888 for (y = 0, row = w->desired_matrix->rows;
13889 y < yb;
13890 y += row->height, ++row)
13891 blank_row (w, row, y);
13892 goto finish_scroll_bars;
13893 }
13894
13895 clear_glyph_matrix (w->desired_matrix);
13896 }
13897
13898 /* Otherwise set up data on this window; select its buffer and point
13899 value. */
13900 /* Really select the buffer, for the sake of buffer-local
13901 variables. */
13902 set_buffer_internal_1 (XBUFFER (w->buffer));
13903
13904 current_matrix_up_to_date_p
13905 = (!NILP (w->window_end_valid)
13906 && !current_buffer->clip_changed
13907 && !current_buffer->prevent_redisplay_optimizations_p
13908 && XFASTINT (w->last_modified) >= MODIFF
13909 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13910
13911 /* Run the window-bottom-change-functions
13912 if it is possible that the text on the screen has changed
13913 (either due to modification of the text, or any other reason). */
13914 if (!current_matrix_up_to_date_p
13915 && !NILP (Vwindow_text_change_functions))
13916 {
13917 safe_run_hooks (Qwindow_text_change_functions);
13918 goto restart;
13919 }
13920
13921 beg_unchanged = BEG_UNCHANGED;
13922 end_unchanged = END_UNCHANGED;
13923
13924 SET_TEXT_POS (opoint, PT, PT_BYTE);
13925
13926 specbind (Qinhibit_point_motion_hooks, Qt);
13927
13928 buffer_unchanged_p
13929 = (!NILP (w->window_end_valid)
13930 && !current_buffer->clip_changed
13931 && XFASTINT (w->last_modified) >= MODIFF
13932 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13933
13934 /* When windows_or_buffers_changed is non-zero, we can't rely on
13935 the window end being valid, so set it to nil there. */
13936 if (windows_or_buffers_changed)
13937 {
13938 /* If window starts on a continuation line, maybe adjust the
13939 window start in case the window's width changed. */
13940 if (XMARKER (w->start)->buffer == current_buffer)
13941 compute_window_start_on_continuation_line (w);
13942
13943 w->window_end_valid = Qnil;
13944 }
13945
13946 /* Some sanity checks. */
13947 CHECK_WINDOW_END (w);
13948 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13949 abort ();
13950 if (BYTEPOS (opoint) < CHARPOS (opoint))
13951 abort ();
13952
13953 /* If %c is in mode line, update it if needed. */
13954 if (!NILP (w->column_number_displayed)
13955 /* This alternative quickly identifies a common case
13956 where no change is needed. */
13957 && !(PT == XFASTINT (w->last_point)
13958 && XFASTINT (w->last_modified) >= MODIFF
13959 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13960 && (XFASTINT (w->column_number_displayed) != current_column ()))
13961 update_mode_line = 1;
13962
13963 /* Count number of windows showing the selected buffer. An indirect
13964 buffer counts as its base buffer. */
13965 if (!just_this_one_p)
13966 {
13967 struct buffer *current_base, *window_base;
13968 current_base = current_buffer;
13969 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13970 if (current_base->base_buffer)
13971 current_base = current_base->base_buffer;
13972 if (window_base->base_buffer)
13973 window_base = window_base->base_buffer;
13974 if (current_base == window_base)
13975 buffer_shared++;
13976 }
13977
13978 /* Point refers normally to the selected window. For any other
13979 window, set up appropriate value. */
13980 if (!EQ (window, selected_window))
13981 {
13982 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
13983 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
13984 if (new_pt < BEGV)
13985 {
13986 new_pt = BEGV;
13987 new_pt_byte = BEGV_BYTE;
13988 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13989 }
13990 else if (new_pt > (ZV - 1))
13991 {
13992 new_pt = ZV;
13993 new_pt_byte = ZV_BYTE;
13994 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13995 }
13996
13997 /* We don't use SET_PT so that the point-motion hooks don't run. */
13998 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13999 }
14000
14001 /* If any of the character widths specified in the display table
14002 have changed, invalidate the width run cache. It's true that
14003 this may be a bit late to catch such changes, but the rest of
14004 redisplay goes (non-fatally) haywire when the display table is
14005 changed, so why should we worry about doing any better? */
14006 if (current_buffer->width_run_cache)
14007 {
14008 struct Lisp_Char_Table *disptab = buffer_display_table ();
14009
14010 if (! disptab_matches_widthtab (disptab,
14011 XVECTOR (BVAR (current_buffer, width_table))))
14012 {
14013 invalidate_region_cache (current_buffer,
14014 current_buffer->width_run_cache,
14015 BEG, Z);
14016 recompute_width_table (current_buffer, disptab);
14017 }
14018 }
14019
14020 /* If window-start is screwed up, choose a new one. */
14021 if (XMARKER (w->start)->buffer != current_buffer)
14022 goto recenter;
14023
14024 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14025
14026 /* If someone specified a new starting point but did not insist,
14027 check whether it can be used. */
14028 if (!NILP (w->optional_new_start)
14029 && CHARPOS (startp) >= BEGV
14030 && CHARPOS (startp) <= ZV)
14031 {
14032 w->optional_new_start = Qnil;
14033 start_display (&it, w, startp);
14034 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14035 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14036 if (IT_CHARPOS (it) == PT)
14037 w->force_start = Qt;
14038 /* IT may overshoot PT if text at PT is invisible. */
14039 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14040 w->force_start = Qt;
14041 }
14042
14043 force_start:
14044
14045 /* Handle case where place to start displaying has been specified,
14046 unless the specified location is outside the accessible range. */
14047 if (!NILP (w->force_start)
14048 || w->frozen_window_start_p)
14049 {
14050 /* We set this later on if we have to adjust point. */
14051 int new_vpos = -1;
14052
14053 w->force_start = Qnil;
14054 w->vscroll = 0;
14055 w->window_end_valid = Qnil;
14056
14057 /* Forget any recorded base line for line number display. */
14058 if (!buffer_unchanged_p)
14059 w->base_line_number = Qnil;
14060
14061 /* Redisplay the mode line. Select the buffer properly for that.
14062 Also, run the hook window-scroll-functions
14063 because we have scrolled. */
14064 /* Note, we do this after clearing force_start because
14065 if there's an error, it is better to forget about force_start
14066 than to get into an infinite loop calling the hook functions
14067 and having them get more errors. */
14068 if (!update_mode_line
14069 || ! NILP (Vwindow_scroll_functions))
14070 {
14071 update_mode_line = 1;
14072 w->update_mode_line = Qt;
14073 startp = run_window_scroll_functions (window, startp);
14074 }
14075
14076 w->last_modified = make_number (0);
14077 w->last_overlay_modified = make_number (0);
14078 if (CHARPOS (startp) < BEGV)
14079 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14080 else if (CHARPOS (startp) > ZV)
14081 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14082
14083 /* Redisplay, then check if cursor has been set during the
14084 redisplay. Give up if new fonts were loaded. */
14085 /* We used to issue a CHECK_MARGINS argument to try_window here,
14086 but this causes scrolling to fail when point begins inside
14087 the scroll margin (bug#148) -- cyd */
14088 if (!try_window (window, startp, 0))
14089 {
14090 w->force_start = Qt;
14091 clear_glyph_matrix (w->desired_matrix);
14092 goto need_larger_matrices;
14093 }
14094
14095 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14096 {
14097 /* If point does not appear, try to move point so it does
14098 appear. The desired matrix has been built above, so we
14099 can use it here. */
14100 new_vpos = window_box_height (w) / 2;
14101 }
14102
14103 if (!cursor_row_fully_visible_p (w, 0, 0))
14104 {
14105 /* Point does appear, but on a line partly visible at end of window.
14106 Move it back to a fully-visible line. */
14107 new_vpos = window_box_height (w);
14108 }
14109
14110 /* If we need to move point for either of the above reasons,
14111 now actually do it. */
14112 if (new_vpos >= 0)
14113 {
14114 struct glyph_row *row;
14115
14116 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14117 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14118 ++row;
14119
14120 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14121 MATRIX_ROW_START_BYTEPOS (row));
14122
14123 if (w != XWINDOW (selected_window))
14124 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14125 else if (current_buffer == old)
14126 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14127
14128 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14129
14130 /* If we are highlighting the region, then we just changed
14131 the region, so redisplay to show it. */
14132 if (!NILP (Vtransient_mark_mode)
14133 && !NILP (BVAR (current_buffer, mark_active)))
14134 {
14135 clear_glyph_matrix (w->desired_matrix);
14136 if (!try_window (window, startp, 0))
14137 goto need_larger_matrices;
14138 }
14139 }
14140
14141 #if GLYPH_DEBUG
14142 debug_method_add (w, "forced window start");
14143 #endif
14144 goto done;
14145 }
14146
14147 /* Handle case where text has not changed, only point, and it has
14148 not moved off the frame, and we are not retrying after hscroll.
14149 (current_matrix_up_to_date_p is nonzero when retrying.) */
14150 if (current_matrix_up_to_date_p
14151 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14152 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14153 {
14154 switch (rc)
14155 {
14156 case CURSOR_MOVEMENT_SUCCESS:
14157 used_current_matrix_p = 1;
14158 goto done;
14159
14160 case CURSOR_MOVEMENT_MUST_SCROLL:
14161 goto try_to_scroll;
14162
14163 default:
14164 abort ();
14165 }
14166 }
14167 /* If current starting point was originally the beginning of a line
14168 but no longer is, find a new starting point. */
14169 else if (!NILP (w->start_at_line_beg)
14170 && !(CHARPOS (startp) <= BEGV
14171 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14172 {
14173 #if GLYPH_DEBUG
14174 debug_method_add (w, "recenter 1");
14175 #endif
14176 goto recenter;
14177 }
14178
14179 /* Try scrolling with try_window_id. Value is > 0 if update has
14180 been done, it is -1 if we know that the same window start will
14181 not work. It is 0 if unsuccessful for some other reason. */
14182 else if ((tem = try_window_id (w)) != 0)
14183 {
14184 #if GLYPH_DEBUG
14185 debug_method_add (w, "try_window_id %d", tem);
14186 #endif
14187
14188 if (fonts_changed_p)
14189 goto need_larger_matrices;
14190 if (tem > 0)
14191 goto done;
14192
14193 /* Otherwise try_window_id has returned -1 which means that we
14194 don't want the alternative below this comment to execute. */
14195 }
14196 else if (CHARPOS (startp) >= BEGV
14197 && CHARPOS (startp) <= ZV
14198 && PT >= CHARPOS (startp)
14199 && (CHARPOS (startp) < ZV
14200 /* Avoid starting at end of buffer. */
14201 || CHARPOS (startp) == BEGV
14202 || (XFASTINT (w->last_modified) >= MODIFF
14203 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14204 {
14205
14206 /* If first window line is a continuation line, and window start
14207 is inside the modified region, but the first change is before
14208 current window start, we must select a new window start.
14209
14210 However, if this is the result of a down-mouse event (e.g. by
14211 extending the mouse-drag-overlay), we don't want to select a
14212 new window start, since that would change the position under
14213 the mouse, resulting in an unwanted mouse-movement rather
14214 than a simple mouse-click. */
14215 if (NILP (w->start_at_line_beg)
14216 && NILP (do_mouse_tracking)
14217 && CHARPOS (startp) > BEGV
14218 && CHARPOS (startp) > BEG + beg_unchanged
14219 && CHARPOS (startp) <= Z - end_unchanged
14220 /* Even if w->start_at_line_beg is nil, a new window may
14221 start at a line_beg, since that's how set_buffer_window
14222 sets it. So, we need to check the return value of
14223 compute_window_start_on_continuation_line. (See also
14224 bug#197). */
14225 && XMARKER (w->start)->buffer == current_buffer
14226 && compute_window_start_on_continuation_line (w))
14227 {
14228 w->force_start = Qt;
14229 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14230 goto force_start;
14231 }
14232
14233 #if GLYPH_DEBUG
14234 debug_method_add (w, "same window start");
14235 #endif
14236
14237 /* Try to redisplay starting at same place as before.
14238 If point has not moved off frame, accept the results. */
14239 if (!current_matrix_up_to_date_p
14240 /* Don't use try_window_reusing_current_matrix in this case
14241 because a window scroll function can have changed the
14242 buffer. */
14243 || !NILP (Vwindow_scroll_functions)
14244 || MINI_WINDOW_P (w)
14245 || !(used_current_matrix_p
14246 = try_window_reusing_current_matrix (w)))
14247 {
14248 IF_DEBUG (debug_method_add (w, "1"));
14249 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14250 /* -1 means we need to scroll.
14251 0 means we need new matrices, but fonts_changed_p
14252 is set in that case, so we will detect it below. */
14253 goto try_to_scroll;
14254 }
14255
14256 if (fonts_changed_p)
14257 goto need_larger_matrices;
14258
14259 if (w->cursor.vpos >= 0)
14260 {
14261 if (!just_this_one_p
14262 || current_buffer->clip_changed
14263 || BEG_UNCHANGED < CHARPOS (startp))
14264 /* Forget any recorded base line for line number display. */
14265 w->base_line_number = Qnil;
14266
14267 if (!cursor_row_fully_visible_p (w, 1, 0))
14268 {
14269 clear_glyph_matrix (w->desired_matrix);
14270 last_line_misfit = 1;
14271 }
14272 /* Drop through and scroll. */
14273 else
14274 goto done;
14275 }
14276 else
14277 clear_glyph_matrix (w->desired_matrix);
14278 }
14279
14280 try_to_scroll:
14281
14282 w->last_modified = make_number (0);
14283 w->last_overlay_modified = make_number (0);
14284
14285 /* Redisplay the mode line. Select the buffer properly for that. */
14286 if (!update_mode_line)
14287 {
14288 update_mode_line = 1;
14289 w->update_mode_line = Qt;
14290 }
14291
14292 /* Try to scroll by specified few lines. */
14293 if ((scroll_conservatively
14294 || emacs_scroll_step
14295 || temp_scroll_step
14296 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14297 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14298 && CHARPOS (startp) >= BEGV
14299 && CHARPOS (startp) <= ZV)
14300 {
14301 /* The function returns -1 if new fonts were loaded, 1 if
14302 successful, 0 if not successful. */
14303 int ss = try_scrolling (window, just_this_one_p,
14304 scroll_conservatively,
14305 emacs_scroll_step,
14306 temp_scroll_step, last_line_misfit);
14307 switch (ss)
14308 {
14309 case SCROLLING_SUCCESS:
14310 goto done;
14311
14312 case SCROLLING_NEED_LARGER_MATRICES:
14313 goto need_larger_matrices;
14314
14315 case SCROLLING_FAILED:
14316 break;
14317
14318 default:
14319 abort ();
14320 }
14321 }
14322
14323 /* Finally, just choose a place to start which positions point
14324 according to user preferences. */
14325
14326 recenter:
14327
14328 #if GLYPH_DEBUG
14329 debug_method_add (w, "recenter");
14330 #endif
14331
14332 /* w->vscroll = 0; */
14333
14334 /* Forget any previously recorded base line for line number display. */
14335 if (!buffer_unchanged_p)
14336 w->base_line_number = Qnil;
14337
14338 /* Determine the window start relative to point. */
14339 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14340 it.current_y = it.last_visible_y;
14341 if (centering_position < 0)
14342 {
14343 int margin =
14344 scroll_margin > 0
14345 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14346 : 0;
14347 EMACS_INT margin_pos = CHARPOS (startp);
14348 int scrolling_up;
14349 Lisp_Object aggressive;
14350
14351 /* If there is a scroll margin at the top of the window, find
14352 its character position. */
14353 if (margin
14354 /* Cannot call start_display if startp is not in the
14355 accessible region of the buffer. This can happen when we
14356 have just switched to a different buffer and/or changed
14357 its restriction. In that case, startp is initialized to
14358 the character position 1 (BEG) because we did not yet
14359 have chance to display the buffer even once. */
14360 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14361 {
14362 struct it it1;
14363
14364 start_display (&it1, w, startp);
14365 move_it_vertically (&it1, margin);
14366 margin_pos = IT_CHARPOS (it1);
14367 }
14368 scrolling_up = PT > margin_pos;
14369 aggressive =
14370 scrolling_up
14371 ? BVAR (current_buffer, scroll_up_aggressively)
14372 : BVAR (current_buffer, scroll_down_aggressively);
14373
14374 if (!MINI_WINDOW_P (w)
14375 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
14376 {
14377 int pt_offset = 0;
14378
14379 /* Setting scroll-conservatively overrides
14380 scroll-*-aggressively. */
14381 if (!scroll_conservatively && NUMBERP (aggressive))
14382 {
14383 double float_amount = XFLOATINT (aggressive);
14384
14385 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14386 if (pt_offset == 0 && float_amount > 0)
14387 pt_offset = 1;
14388 if (pt_offset)
14389 margin -= 1;
14390 }
14391 /* Compute how much to move the window start backward from
14392 point so that point will be displayed where the user
14393 wants it. */
14394 if (scrolling_up)
14395 {
14396 centering_position = it.last_visible_y;
14397 if (pt_offset)
14398 centering_position -= pt_offset;
14399 centering_position -=
14400 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
14401 /* Don't let point enter the scroll margin near top of
14402 the window. */
14403 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
14404 centering_position = margin * FRAME_LINE_HEIGHT (f);
14405 }
14406 else
14407 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
14408 }
14409 else
14410 /* Set the window start half the height of the window backward
14411 from point. */
14412 centering_position = window_box_height (w) / 2;
14413 }
14414 move_it_vertically_backward (&it, centering_position);
14415
14416 xassert (IT_CHARPOS (it) >= BEGV);
14417
14418 /* The function move_it_vertically_backward may move over more
14419 than the specified y-distance. If it->w is small, e.g. a
14420 mini-buffer window, we may end up in front of the window's
14421 display area. Start displaying at the start of the line
14422 containing PT in this case. */
14423 if (it.current_y <= 0)
14424 {
14425 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14426 move_it_vertically_backward (&it, 0);
14427 it.current_y = 0;
14428 }
14429
14430 it.current_x = it.hpos = 0;
14431
14432 /* Set the window start position here explicitly, to avoid an
14433 infinite loop in case the functions in window-scroll-functions
14434 get errors. */
14435 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14436
14437 /* Run scroll hooks. */
14438 startp = run_window_scroll_functions (window, it.current.pos);
14439
14440 /* Redisplay the window. */
14441 if (!current_matrix_up_to_date_p
14442 || windows_or_buffers_changed
14443 || cursor_type_changed
14444 /* Don't use try_window_reusing_current_matrix in this case
14445 because it can have changed the buffer. */
14446 || !NILP (Vwindow_scroll_functions)
14447 || !just_this_one_p
14448 || MINI_WINDOW_P (w)
14449 || !(used_current_matrix_p
14450 = try_window_reusing_current_matrix (w)))
14451 try_window (window, startp, 0);
14452
14453 /* If new fonts have been loaded (due to fontsets), give up. We
14454 have to start a new redisplay since we need to re-adjust glyph
14455 matrices. */
14456 if (fonts_changed_p)
14457 goto need_larger_matrices;
14458
14459 /* If cursor did not appear assume that the middle of the window is
14460 in the first line of the window. Do it again with the next line.
14461 (Imagine a window of height 100, displaying two lines of height
14462 60. Moving back 50 from it->last_visible_y will end in the first
14463 line.) */
14464 if (w->cursor.vpos < 0)
14465 {
14466 if (!NILP (w->window_end_valid)
14467 && PT >= Z - XFASTINT (w->window_end_pos))
14468 {
14469 clear_glyph_matrix (w->desired_matrix);
14470 move_it_by_lines (&it, 1);
14471 try_window (window, it.current.pos, 0);
14472 }
14473 else if (PT < IT_CHARPOS (it))
14474 {
14475 clear_glyph_matrix (w->desired_matrix);
14476 move_it_by_lines (&it, -1);
14477 try_window (window, it.current.pos, 0);
14478 }
14479 else
14480 {
14481 /* Not much we can do about it. */
14482 }
14483 }
14484
14485 /* Consider the following case: Window starts at BEGV, there is
14486 invisible, intangible text at BEGV, so that display starts at
14487 some point START > BEGV. It can happen that we are called with
14488 PT somewhere between BEGV and START. Try to handle that case. */
14489 if (w->cursor.vpos < 0)
14490 {
14491 struct glyph_row *row = w->current_matrix->rows;
14492 if (row->mode_line_p)
14493 ++row;
14494 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14495 }
14496
14497 if (!cursor_row_fully_visible_p (w, 0, 0))
14498 {
14499 /* If vscroll is enabled, disable it and try again. */
14500 if (w->vscroll)
14501 {
14502 w->vscroll = 0;
14503 clear_glyph_matrix (w->desired_matrix);
14504 goto recenter;
14505 }
14506
14507 /* If centering point failed to make the whole line visible,
14508 put point at the top instead. That has to make the whole line
14509 visible, if it can be done. */
14510 if (centering_position == 0)
14511 goto done;
14512
14513 clear_glyph_matrix (w->desired_matrix);
14514 centering_position = 0;
14515 goto recenter;
14516 }
14517
14518 done:
14519
14520 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14521 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14522 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14523 ? Qt : Qnil);
14524
14525 /* Display the mode line, if we must. */
14526 if ((update_mode_line
14527 /* If window not full width, must redo its mode line
14528 if (a) the window to its side is being redone and
14529 (b) we do a frame-based redisplay. This is a consequence
14530 of how inverted lines are drawn in frame-based redisplay. */
14531 || (!just_this_one_p
14532 && !FRAME_WINDOW_P (f)
14533 && !WINDOW_FULL_WIDTH_P (w))
14534 /* Line number to display. */
14535 || INTEGERP (w->base_line_pos)
14536 /* Column number is displayed and different from the one displayed. */
14537 || (!NILP (w->column_number_displayed)
14538 && (XFASTINT (w->column_number_displayed) != current_column ())))
14539 /* This means that the window has a mode line. */
14540 && (WINDOW_WANTS_MODELINE_P (w)
14541 || WINDOW_WANTS_HEADER_LINE_P (w)))
14542 {
14543 display_mode_lines (w);
14544
14545 /* If mode line height has changed, arrange for a thorough
14546 immediate redisplay using the correct mode line height. */
14547 if (WINDOW_WANTS_MODELINE_P (w)
14548 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14549 {
14550 fonts_changed_p = 1;
14551 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14552 = DESIRED_MODE_LINE_HEIGHT (w);
14553 }
14554
14555 /* If header line height has changed, arrange for a thorough
14556 immediate redisplay using the correct header line height. */
14557 if (WINDOW_WANTS_HEADER_LINE_P (w)
14558 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14559 {
14560 fonts_changed_p = 1;
14561 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14562 = DESIRED_HEADER_LINE_HEIGHT (w);
14563 }
14564
14565 if (fonts_changed_p)
14566 goto need_larger_matrices;
14567 }
14568
14569 if (!line_number_displayed
14570 && !BUFFERP (w->base_line_pos))
14571 {
14572 w->base_line_pos = Qnil;
14573 w->base_line_number = Qnil;
14574 }
14575
14576 finish_menu_bars:
14577
14578 /* When we reach a frame's selected window, redo the frame's menu bar. */
14579 if (update_mode_line
14580 && EQ (FRAME_SELECTED_WINDOW (f), window))
14581 {
14582 int redisplay_menu_p = 0;
14583
14584 if (FRAME_WINDOW_P (f))
14585 {
14586 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14587 || defined (HAVE_NS) || defined (USE_GTK)
14588 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14589 #else
14590 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14591 #endif
14592 }
14593 else
14594 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14595
14596 if (redisplay_menu_p)
14597 display_menu_bar (w);
14598
14599 #ifdef HAVE_WINDOW_SYSTEM
14600 if (FRAME_WINDOW_P (f))
14601 {
14602 #if defined (USE_GTK) || defined (HAVE_NS)
14603 if (FRAME_EXTERNAL_TOOL_BAR (f))
14604 redisplay_tool_bar (f);
14605 #else
14606 if (WINDOWP (f->tool_bar_window)
14607 && (FRAME_TOOL_BAR_LINES (f) > 0
14608 || !NILP (Vauto_resize_tool_bars))
14609 && redisplay_tool_bar (f))
14610 ignore_mouse_drag_p = 1;
14611 #endif
14612 }
14613 #endif
14614 }
14615
14616 #ifdef HAVE_WINDOW_SYSTEM
14617 if (FRAME_WINDOW_P (f)
14618 && update_window_fringes (w, (just_this_one_p
14619 || (!used_current_matrix_p && !overlay_arrow_seen)
14620 || w->pseudo_window_p)))
14621 {
14622 update_begin (f);
14623 BLOCK_INPUT;
14624 if (draw_window_fringes (w, 1))
14625 x_draw_vertical_border (w);
14626 UNBLOCK_INPUT;
14627 update_end (f);
14628 }
14629 #endif /* HAVE_WINDOW_SYSTEM */
14630
14631 /* We go to this label, with fonts_changed_p nonzero,
14632 if it is necessary to try again using larger glyph matrices.
14633 We have to redeem the scroll bar even in this case,
14634 because the loop in redisplay_internal expects that. */
14635 need_larger_matrices:
14636 ;
14637 finish_scroll_bars:
14638
14639 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14640 {
14641 /* Set the thumb's position and size. */
14642 set_vertical_scroll_bar (w);
14643
14644 /* Note that we actually used the scroll bar attached to this
14645 window, so it shouldn't be deleted at the end of redisplay. */
14646 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14647 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14648 }
14649
14650 /* Restore current_buffer and value of point in it. The window
14651 update may have changed the buffer, so first make sure `opoint'
14652 is still valid (Bug#6177). */
14653 if (CHARPOS (opoint) < BEGV)
14654 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14655 else if (CHARPOS (opoint) > ZV)
14656 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14657 else
14658 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14659
14660 set_buffer_internal_1 (old);
14661 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14662 shorter. This can be caused by log truncation in *Messages*. */
14663 if (CHARPOS (lpoint) <= ZV)
14664 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14665
14666 unbind_to (count, Qnil);
14667 }
14668
14669
14670 /* Build the complete desired matrix of WINDOW with a window start
14671 buffer position POS.
14672
14673 Value is 1 if successful. It is zero if fonts were loaded during
14674 redisplay which makes re-adjusting glyph matrices necessary, and -1
14675 if point would appear in the scroll margins.
14676 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14677 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14678 set in FLAGS.) */
14679
14680 int
14681 try_window (Lisp_Object window, struct text_pos pos, int flags)
14682 {
14683 struct window *w = XWINDOW (window);
14684 struct it it;
14685 struct glyph_row *last_text_row = NULL;
14686 struct frame *f = XFRAME (w->frame);
14687
14688 /* Make POS the new window start. */
14689 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14690
14691 /* Mark cursor position as unknown. No overlay arrow seen. */
14692 w->cursor.vpos = -1;
14693 overlay_arrow_seen = 0;
14694
14695 /* Initialize iterator and info to start at POS. */
14696 start_display (&it, w, pos);
14697
14698 /* Display all lines of W. */
14699 while (it.current_y < it.last_visible_y)
14700 {
14701 if (display_line (&it))
14702 last_text_row = it.glyph_row - 1;
14703 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14704 return 0;
14705 }
14706
14707 /* Don't let the cursor end in the scroll margins. */
14708 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14709 && !MINI_WINDOW_P (w))
14710 {
14711 int this_scroll_margin;
14712
14713 if (scroll_margin > 0)
14714 {
14715 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14716 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14717 }
14718 else
14719 this_scroll_margin = 0;
14720
14721 if ((w->cursor.y >= 0 /* not vscrolled */
14722 && w->cursor.y < this_scroll_margin
14723 && CHARPOS (pos) > BEGV
14724 && IT_CHARPOS (it) < ZV)
14725 /* rms: considering make_cursor_line_fully_visible_p here
14726 seems to give wrong results. We don't want to recenter
14727 when the last line is partly visible, we want to allow
14728 that case to be handled in the usual way. */
14729 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14730 {
14731 w->cursor.vpos = -1;
14732 clear_glyph_matrix (w->desired_matrix);
14733 return -1;
14734 }
14735 }
14736
14737 /* If bottom moved off end of frame, change mode line percentage. */
14738 if (XFASTINT (w->window_end_pos) <= 0
14739 && Z != IT_CHARPOS (it))
14740 w->update_mode_line = Qt;
14741
14742 /* Set window_end_pos to the offset of the last character displayed
14743 on the window from the end of current_buffer. Set
14744 window_end_vpos to its row number. */
14745 if (last_text_row)
14746 {
14747 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14748 w->window_end_bytepos
14749 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14750 w->window_end_pos
14751 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14752 w->window_end_vpos
14753 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14754 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14755 ->displays_text_p);
14756 }
14757 else
14758 {
14759 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14760 w->window_end_pos = make_number (Z - ZV);
14761 w->window_end_vpos = make_number (0);
14762 }
14763
14764 /* But that is not valid info until redisplay finishes. */
14765 w->window_end_valid = Qnil;
14766 return 1;
14767 }
14768
14769
14770 \f
14771 /************************************************************************
14772 Window redisplay reusing current matrix when buffer has not changed
14773 ************************************************************************/
14774
14775 /* Try redisplay of window W showing an unchanged buffer with a
14776 different window start than the last time it was displayed by
14777 reusing its current matrix. Value is non-zero if successful.
14778 W->start is the new window start. */
14779
14780 static int
14781 try_window_reusing_current_matrix (struct window *w)
14782 {
14783 struct frame *f = XFRAME (w->frame);
14784 struct glyph_row *bottom_row;
14785 struct it it;
14786 struct run run;
14787 struct text_pos start, new_start;
14788 int nrows_scrolled, i;
14789 struct glyph_row *last_text_row;
14790 struct glyph_row *last_reused_text_row;
14791 struct glyph_row *start_row;
14792 int start_vpos, min_y, max_y;
14793
14794 #if GLYPH_DEBUG
14795 if (inhibit_try_window_reusing)
14796 return 0;
14797 #endif
14798
14799 if (/* This function doesn't handle terminal frames. */
14800 !FRAME_WINDOW_P (f)
14801 /* Don't try to reuse the display if windows have been split
14802 or such. */
14803 || windows_or_buffers_changed
14804 || cursor_type_changed)
14805 return 0;
14806
14807 /* Can't do this if region may have changed. */
14808 if ((!NILP (Vtransient_mark_mode)
14809 && !NILP (BVAR (current_buffer, mark_active)))
14810 || !NILP (w->region_showing)
14811 || !NILP (Vshow_trailing_whitespace))
14812 return 0;
14813
14814 /* If top-line visibility has changed, give up. */
14815 if (WINDOW_WANTS_HEADER_LINE_P (w)
14816 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14817 return 0;
14818
14819 /* Give up if old or new display is scrolled vertically. We could
14820 make this function handle this, but right now it doesn't. */
14821 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14822 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14823 return 0;
14824
14825 /* The variable new_start now holds the new window start. The old
14826 start `start' can be determined from the current matrix. */
14827 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14828 start = start_row->minpos;
14829 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14830
14831 /* Clear the desired matrix for the display below. */
14832 clear_glyph_matrix (w->desired_matrix);
14833
14834 if (CHARPOS (new_start) <= CHARPOS (start))
14835 {
14836 /* Don't use this method if the display starts with an ellipsis
14837 displayed for invisible text. It's not easy to handle that case
14838 below, and it's certainly not worth the effort since this is
14839 not a frequent case. */
14840 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14841 return 0;
14842
14843 IF_DEBUG (debug_method_add (w, "twu1"));
14844
14845 /* Display up to a row that can be reused. The variable
14846 last_text_row is set to the last row displayed that displays
14847 text. Note that it.vpos == 0 if or if not there is a
14848 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14849 start_display (&it, w, new_start);
14850 w->cursor.vpos = -1;
14851 last_text_row = last_reused_text_row = NULL;
14852
14853 while (it.current_y < it.last_visible_y
14854 && !fonts_changed_p)
14855 {
14856 /* If we have reached into the characters in the START row,
14857 that means the line boundaries have changed. So we
14858 can't start copying with the row START. Maybe it will
14859 work to start copying with the following row. */
14860 while (IT_CHARPOS (it) > CHARPOS (start))
14861 {
14862 /* Advance to the next row as the "start". */
14863 start_row++;
14864 start = start_row->minpos;
14865 /* If there are no more rows to try, or just one, give up. */
14866 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14867 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14868 || CHARPOS (start) == ZV)
14869 {
14870 clear_glyph_matrix (w->desired_matrix);
14871 return 0;
14872 }
14873
14874 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14875 }
14876 /* If we have reached alignment,
14877 we can copy the rest of the rows. */
14878 if (IT_CHARPOS (it) == CHARPOS (start))
14879 break;
14880
14881 if (display_line (&it))
14882 last_text_row = it.glyph_row - 1;
14883 }
14884
14885 /* A value of current_y < last_visible_y means that we stopped
14886 at the previous window start, which in turn means that we
14887 have at least one reusable row. */
14888 if (it.current_y < it.last_visible_y)
14889 {
14890 struct glyph_row *row;
14891
14892 /* IT.vpos always starts from 0; it counts text lines. */
14893 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14894
14895 /* Find PT if not already found in the lines displayed. */
14896 if (w->cursor.vpos < 0)
14897 {
14898 int dy = it.current_y - start_row->y;
14899
14900 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14901 row = row_containing_pos (w, PT, row, NULL, dy);
14902 if (row)
14903 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14904 dy, nrows_scrolled);
14905 else
14906 {
14907 clear_glyph_matrix (w->desired_matrix);
14908 return 0;
14909 }
14910 }
14911
14912 /* Scroll the display. Do it before the current matrix is
14913 changed. The problem here is that update has not yet
14914 run, i.e. part of the current matrix is not up to date.
14915 scroll_run_hook will clear the cursor, and use the
14916 current matrix to get the height of the row the cursor is
14917 in. */
14918 run.current_y = start_row->y;
14919 run.desired_y = it.current_y;
14920 run.height = it.last_visible_y - it.current_y;
14921
14922 if (run.height > 0 && run.current_y != run.desired_y)
14923 {
14924 update_begin (f);
14925 FRAME_RIF (f)->update_window_begin_hook (w);
14926 FRAME_RIF (f)->clear_window_mouse_face (w);
14927 FRAME_RIF (f)->scroll_run_hook (w, &run);
14928 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14929 update_end (f);
14930 }
14931
14932 /* Shift current matrix down by nrows_scrolled lines. */
14933 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14934 rotate_matrix (w->current_matrix,
14935 start_vpos,
14936 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14937 nrows_scrolled);
14938
14939 /* Disable lines that must be updated. */
14940 for (i = 0; i < nrows_scrolled; ++i)
14941 (start_row + i)->enabled_p = 0;
14942
14943 /* Re-compute Y positions. */
14944 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14945 max_y = it.last_visible_y;
14946 for (row = start_row + nrows_scrolled;
14947 row < bottom_row;
14948 ++row)
14949 {
14950 row->y = it.current_y;
14951 row->visible_height = row->height;
14952
14953 if (row->y < min_y)
14954 row->visible_height -= min_y - row->y;
14955 if (row->y + row->height > max_y)
14956 row->visible_height -= row->y + row->height - max_y;
14957 if (row->fringe_bitmap_periodic_p)
14958 row->redraw_fringe_bitmaps_p = 1;
14959
14960 it.current_y += row->height;
14961
14962 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14963 last_reused_text_row = row;
14964 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14965 break;
14966 }
14967
14968 /* Disable lines in the current matrix which are now
14969 below the window. */
14970 for (++row; row < bottom_row; ++row)
14971 row->enabled_p = row->mode_line_p = 0;
14972 }
14973
14974 /* Update window_end_pos etc.; last_reused_text_row is the last
14975 reused row from the current matrix containing text, if any.
14976 The value of last_text_row is the last displayed line
14977 containing text. */
14978 if (last_reused_text_row)
14979 {
14980 w->window_end_bytepos
14981 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14982 w->window_end_pos
14983 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14984 w->window_end_vpos
14985 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14986 w->current_matrix));
14987 }
14988 else if (last_text_row)
14989 {
14990 w->window_end_bytepos
14991 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14992 w->window_end_pos
14993 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14994 w->window_end_vpos
14995 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14996 }
14997 else
14998 {
14999 /* This window must be completely empty. */
15000 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15001 w->window_end_pos = make_number (Z - ZV);
15002 w->window_end_vpos = make_number (0);
15003 }
15004 w->window_end_valid = Qnil;
15005
15006 /* Update hint: don't try scrolling again in update_window. */
15007 w->desired_matrix->no_scrolling_p = 1;
15008
15009 #if GLYPH_DEBUG
15010 debug_method_add (w, "try_window_reusing_current_matrix 1");
15011 #endif
15012 return 1;
15013 }
15014 else if (CHARPOS (new_start) > CHARPOS (start))
15015 {
15016 struct glyph_row *pt_row, *row;
15017 struct glyph_row *first_reusable_row;
15018 struct glyph_row *first_row_to_display;
15019 int dy;
15020 int yb = window_text_bottom_y (w);
15021
15022 /* Find the row starting at new_start, if there is one. Don't
15023 reuse a partially visible line at the end. */
15024 first_reusable_row = start_row;
15025 while (first_reusable_row->enabled_p
15026 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15027 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15028 < CHARPOS (new_start)))
15029 ++first_reusable_row;
15030
15031 /* Give up if there is no row to reuse. */
15032 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15033 || !first_reusable_row->enabled_p
15034 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15035 != CHARPOS (new_start)))
15036 return 0;
15037
15038 /* We can reuse fully visible rows beginning with
15039 first_reusable_row to the end of the window. Set
15040 first_row_to_display to the first row that cannot be reused.
15041 Set pt_row to the row containing point, if there is any. */
15042 pt_row = NULL;
15043 for (first_row_to_display = first_reusable_row;
15044 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15045 ++first_row_to_display)
15046 {
15047 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15048 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15049 pt_row = first_row_to_display;
15050 }
15051
15052 /* Start displaying at the start of first_row_to_display. */
15053 xassert (first_row_to_display->y < yb);
15054 init_to_row_start (&it, w, first_row_to_display);
15055
15056 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15057 - start_vpos);
15058 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15059 - nrows_scrolled);
15060 it.current_y = (first_row_to_display->y - first_reusable_row->y
15061 + WINDOW_HEADER_LINE_HEIGHT (w));
15062
15063 /* Display lines beginning with first_row_to_display in the
15064 desired matrix. Set last_text_row to the last row displayed
15065 that displays text. */
15066 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15067 if (pt_row == NULL)
15068 w->cursor.vpos = -1;
15069 last_text_row = NULL;
15070 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15071 if (display_line (&it))
15072 last_text_row = it.glyph_row - 1;
15073
15074 /* If point is in a reused row, adjust y and vpos of the cursor
15075 position. */
15076 if (pt_row)
15077 {
15078 w->cursor.vpos -= nrows_scrolled;
15079 w->cursor.y -= first_reusable_row->y - start_row->y;
15080 }
15081
15082 /* Give up if point isn't in a row displayed or reused. (This
15083 also handles the case where w->cursor.vpos < nrows_scrolled
15084 after the calls to display_line, which can happen with scroll
15085 margins. See bug#1295.) */
15086 if (w->cursor.vpos < 0)
15087 {
15088 clear_glyph_matrix (w->desired_matrix);
15089 return 0;
15090 }
15091
15092 /* Scroll the display. */
15093 run.current_y = first_reusable_row->y;
15094 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15095 run.height = it.last_visible_y - run.current_y;
15096 dy = run.current_y - run.desired_y;
15097
15098 if (run.height)
15099 {
15100 update_begin (f);
15101 FRAME_RIF (f)->update_window_begin_hook (w);
15102 FRAME_RIF (f)->clear_window_mouse_face (w);
15103 FRAME_RIF (f)->scroll_run_hook (w, &run);
15104 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15105 update_end (f);
15106 }
15107
15108 /* Adjust Y positions of reused rows. */
15109 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15110 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15111 max_y = it.last_visible_y;
15112 for (row = first_reusable_row; row < first_row_to_display; ++row)
15113 {
15114 row->y -= dy;
15115 row->visible_height = row->height;
15116 if (row->y < min_y)
15117 row->visible_height -= min_y - row->y;
15118 if (row->y + row->height > max_y)
15119 row->visible_height -= row->y + row->height - max_y;
15120 if (row->fringe_bitmap_periodic_p)
15121 row->redraw_fringe_bitmaps_p = 1;
15122 }
15123
15124 /* Scroll the current matrix. */
15125 xassert (nrows_scrolled > 0);
15126 rotate_matrix (w->current_matrix,
15127 start_vpos,
15128 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15129 -nrows_scrolled);
15130
15131 /* Disable rows not reused. */
15132 for (row -= nrows_scrolled; row < bottom_row; ++row)
15133 row->enabled_p = 0;
15134
15135 /* Point may have moved to a different line, so we cannot assume that
15136 the previous cursor position is valid; locate the correct row. */
15137 if (pt_row)
15138 {
15139 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15140 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15141 row++)
15142 {
15143 w->cursor.vpos++;
15144 w->cursor.y = row->y;
15145 }
15146 if (row < bottom_row)
15147 {
15148 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15149 struct glyph *end = glyph + row->used[TEXT_AREA];
15150
15151 /* Can't use this optimization with bidi-reordered glyph
15152 rows, unless cursor is already at point. */
15153 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15154 {
15155 if (!(w->cursor.hpos >= 0
15156 && w->cursor.hpos < row->used[TEXT_AREA]
15157 && BUFFERP (glyph->object)
15158 && glyph->charpos == PT))
15159 return 0;
15160 }
15161 else
15162 for (; glyph < end
15163 && (!BUFFERP (glyph->object)
15164 || glyph->charpos < PT);
15165 glyph++)
15166 {
15167 w->cursor.hpos++;
15168 w->cursor.x += glyph->pixel_width;
15169 }
15170 }
15171 }
15172
15173 /* Adjust window end. A null value of last_text_row means that
15174 the window end is in reused rows which in turn means that
15175 only its vpos can have changed. */
15176 if (last_text_row)
15177 {
15178 w->window_end_bytepos
15179 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15180 w->window_end_pos
15181 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15182 w->window_end_vpos
15183 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15184 }
15185 else
15186 {
15187 w->window_end_vpos
15188 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15189 }
15190
15191 w->window_end_valid = Qnil;
15192 w->desired_matrix->no_scrolling_p = 1;
15193
15194 #if GLYPH_DEBUG
15195 debug_method_add (w, "try_window_reusing_current_matrix 2");
15196 #endif
15197 return 1;
15198 }
15199
15200 return 0;
15201 }
15202
15203
15204 \f
15205 /************************************************************************
15206 Window redisplay reusing current matrix when buffer has changed
15207 ************************************************************************/
15208
15209 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15210 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15211 EMACS_INT *, EMACS_INT *);
15212 static struct glyph_row *
15213 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15214 struct glyph_row *);
15215
15216
15217 /* Return the last row in MATRIX displaying text. If row START is
15218 non-null, start searching with that row. IT gives the dimensions
15219 of the display. Value is null if matrix is empty; otherwise it is
15220 a pointer to the row found. */
15221
15222 static struct glyph_row *
15223 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15224 struct glyph_row *start)
15225 {
15226 struct glyph_row *row, *row_found;
15227
15228 /* Set row_found to the last row in IT->w's current matrix
15229 displaying text. The loop looks funny but think of partially
15230 visible lines. */
15231 row_found = NULL;
15232 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15233 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15234 {
15235 xassert (row->enabled_p);
15236 row_found = row;
15237 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15238 break;
15239 ++row;
15240 }
15241
15242 return row_found;
15243 }
15244
15245
15246 /* Return the last row in the current matrix of W that is not affected
15247 by changes at the start of current_buffer that occurred since W's
15248 current matrix was built. Value is null if no such row exists.
15249
15250 BEG_UNCHANGED us the number of characters unchanged at the start of
15251 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15252 first changed character in current_buffer. Characters at positions <
15253 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15254 when the current matrix was built. */
15255
15256 static struct glyph_row *
15257 find_last_unchanged_at_beg_row (struct window *w)
15258 {
15259 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15260 struct glyph_row *row;
15261 struct glyph_row *row_found = NULL;
15262 int yb = window_text_bottom_y (w);
15263
15264 /* Find the last row displaying unchanged text. */
15265 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15266 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15267 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15268 ++row)
15269 {
15270 if (/* If row ends before first_changed_pos, it is unchanged,
15271 except in some case. */
15272 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15273 /* When row ends in ZV and we write at ZV it is not
15274 unchanged. */
15275 && !row->ends_at_zv_p
15276 /* When first_changed_pos is the end of a continued line,
15277 row is not unchanged because it may be no longer
15278 continued. */
15279 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15280 && (row->continued_p
15281 || row->exact_window_width_line_p)))
15282 row_found = row;
15283
15284 /* Stop if last visible row. */
15285 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15286 break;
15287 }
15288
15289 return row_found;
15290 }
15291
15292
15293 /* Find the first glyph row in the current matrix of W that is not
15294 affected by changes at the end of current_buffer since the
15295 time W's current matrix was built.
15296
15297 Return in *DELTA the number of chars by which buffer positions in
15298 unchanged text at the end of current_buffer must be adjusted.
15299
15300 Return in *DELTA_BYTES the corresponding number of bytes.
15301
15302 Value is null if no such row exists, i.e. all rows are affected by
15303 changes. */
15304
15305 static struct glyph_row *
15306 find_first_unchanged_at_end_row (struct window *w,
15307 EMACS_INT *delta, EMACS_INT *delta_bytes)
15308 {
15309 struct glyph_row *row;
15310 struct glyph_row *row_found = NULL;
15311
15312 *delta = *delta_bytes = 0;
15313
15314 /* Display must not have been paused, otherwise the current matrix
15315 is not up to date. */
15316 eassert (!NILP (w->window_end_valid));
15317
15318 /* A value of window_end_pos >= END_UNCHANGED means that the window
15319 end is in the range of changed text. If so, there is no
15320 unchanged row at the end of W's current matrix. */
15321 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15322 return NULL;
15323
15324 /* Set row to the last row in W's current matrix displaying text. */
15325 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15326
15327 /* If matrix is entirely empty, no unchanged row exists. */
15328 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15329 {
15330 /* The value of row is the last glyph row in the matrix having a
15331 meaningful buffer position in it. The end position of row
15332 corresponds to window_end_pos. This allows us to translate
15333 buffer positions in the current matrix to current buffer
15334 positions for characters not in changed text. */
15335 EMACS_INT Z_old =
15336 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15337 EMACS_INT Z_BYTE_old =
15338 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15339 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15340 struct glyph_row *first_text_row
15341 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15342
15343 *delta = Z - Z_old;
15344 *delta_bytes = Z_BYTE - Z_BYTE_old;
15345
15346 /* Set last_unchanged_pos to the buffer position of the last
15347 character in the buffer that has not been changed. Z is the
15348 index + 1 of the last character in current_buffer, i.e. by
15349 subtracting END_UNCHANGED we get the index of the last
15350 unchanged character, and we have to add BEG to get its buffer
15351 position. */
15352 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15353 last_unchanged_pos_old = last_unchanged_pos - *delta;
15354
15355 /* Search backward from ROW for a row displaying a line that
15356 starts at a minimum position >= last_unchanged_pos_old. */
15357 for (; row > first_text_row; --row)
15358 {
15359 /* This used to abort, but it can happen.
15360 It is ok to just stop the search instead here. KFS. */
15361 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15362 break;
15363
15364 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15365 row_found = row;
15366 }
15367 }
15368
15369 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15370
15371 return row_found;
15372 }
15373
15374
15375 /* Make sure that glyph rows in the current matrix of window W
15376 reference the same glyph memory as corresponding rows in the
15377 frame's frame matrix. This function is called after scrolling W's
15378 current matrix on a terminal frame in try_window_id and
15379 try_window_reusing_current_matrix. */
15380
15381 static void
15382 sync_frame_with_window_matrix_rows (struct window *w)
15383 {
15384 struct frame *f = XFRAME (w->frame);
15385 struct glyph_row *window_row, *window_row_end, *frame_row;
15386
15387 /* Preconditions: W must be a leaf window and full-width. Its frame
15388 must have a frame matrix. */
15389 xassert (NILP (w->hchild) && NILP (w->vchild));
15390 xassert (WINDOW_FULL_WIDTH_P (w));
15391 xassert (!FRAME_WINDOW_P (f));
15392
15393 /* If W is a full-width window, glyph pointers in W's current matrix
15394 have, by definition, to be the same as glyph pointers in the
15395 corresponding frame matrix. Note that frame matrices have no
15396 marginal areas (see build_frame_matrix). */
15397 window_row = w->current_matrix->rows;
15398 window_row_end = window_row + w->current_matrix->nrows;
15399 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15400 while (window_row < window_row_end)
15401 {
15402 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15403 struct glyph *end = window_row->glyphs[LAST_AREA];
15404
15405 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15406 frame_row->glyphs[TEXT_AREA] = start;
15407 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15408 frame_row->glyphs[LAST_AREA] = end;
15409
15410 /* Disable frame rows whose corresponding window rows have
15411 been disabled in try_window_id. */
15412 if (!window_row->enabled_p)
15413 frame_row->enabled_p = 0;
15414
15415 ++window_row, ++frame_row;
15416 }
15417 }
15418
15419
15420 /* Find the glyph row in window W containing CHARPOS. Consider all
15421 rows between START and END (not inclusive). END null means search
15422 all rows to the end of the display area of W. Value is the row
15423 containing CHARPOS or null. */
15424
15425 struct glyph_row *
15426 row_containing_pos (struct window *w, EMACS_INT charpos,
15427 struct glyph_row *start, struct glyph_row *end, int dy)
15428 {
15429 struct glyph_row *row = start;
15430 struct glyph_row *best_row = NULL;
15431 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15432 int last_y;
15433
15434 /* If we happen to start on a header-line, skip that. */
15435 if (row->mode_line_p)
15436 ++row;
15437
15438 if ((end && row >= end) || !row->enabled_p)
15439 return NULL;
15440
15441 last_y = window_text_bottom_y (w) - dy;
15442
15443 while (1)
15444 {
15445 /* Give up if we have gone too far. */
15446 if (end && row >= end)
15447 return NULL;
15448 /* This formerly returned if they were equal.
15449 I think that both quantities are of a "last plus one" type;
15450 if so, when they are equal, the row is within the screen. -- rms. */
15451 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15452 return NULL;
15453
15454 /* If it is in this row, return this row. */
15455 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15456 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15457 /* The end position of a row equals the start
15458 position of the next row. If CHARPOS is there, we
15459 would rather display it in the next line, except
15460 when this line ends in ZV. */
15461 && !row->ends_at_zv_p
15462 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15463 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15464 {
15465 struct glyph *g;
15466
15467 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15468 || (!best_row && !row->continued_p))
15469 return row;
15470 /* In bidi-reordered rows, there could be several rows
15471 occluding point, all of them belonging to the same
15472 continued line. We need to find the row which fits
15473 CHARPOS the best. */
15474 for (g = row->glyphs[TEXT_AREA];
15475 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15476 g++)
15477 {
15478 if (!STRINGP (g->object))
15479 {
15480 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15481 {
15482 mindif = eabs (g->charpos - charpos);
15483 best_row = row;
15484 /* Exact match always wins. */
15485 if (mindif == 0)
15486 return best_row;
15487 }
15488 }
15489 }
15490 }
15491 else if (best_row && !row->continued_p)
15492 return best_row;
15493 ++row;
15494 }
15495 }
15496
15497
15498 /* Try to redisplay window W by reusing its existing display. W's
15499 current matrix must be up to date when this function is called,
15500 i.e. window_end_valid must not be nil.
15501
15502 Value is
15503
15504 1 if display has been updated
15505 0 if otherwise unsuccessful
15506 -1 if redisplay with same window start is known not to succeed
15507
15508 The following steps are performed:
15509
15510 1. Find the last row in the current matrix of W that is not
15511 affected by changes at the start of current_buffer. If no such row
15512 is found, give up.
15513
15514 2. Find the first row in W's current matrix that is not affected by
15515 changes at the end of current_buffer. Maybe there is no such row.
15516
15517 3. Display lines beginning with the row + 1 found in step 1 to the
15518 row found in step 2 or, if step 2 didn't find a row, to the end of
15519 the window.
15520
15521 4. If cursor is not known to appear on the window, give up.
15522
15523 5. If display stopped at the row found in step 2, scroll the
15524 display and current matrix as needed.
15525
15526 6. Maybe display some lines at the end of W, if we must. This can
15527 happen under various circumstances, like a partially visible line
15528 becoming fully visible, or because newly displayed lines are displayed
15529 in smaller font sizes.
15530
15531 7. Update W's window end information. */
15532
15533 static int
15534 try_window_id (struct window *w)
15535 {
15536 struct frame *f = XFRAME (w->frame);
15537 struct glyph_matrix *current_matrix = w->current_matrix;
15538 struct glyph_matrix *desired_matrix = w->desired_matrix;
15539 struct glyph_row *last_unchanged_at_beg_row;
15540 struct glyph_row *first_unchanged_at_end_row;
15541 struct glyph_row *row;
15542 struct glyph_row *bottom_row;
15543 int bottom_vpos;
15544 struct it it;
15545 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15546 int dvpos, dy;
15547 struct text_pos start_pos;
15548 struct run run;
15549 int first_unchanged_at_end_vpos = 0;
15550 struct glyph_row *last_text_row, *last_text_row_at_end;
15551 struct text_pos start;
15552 EMACS_INT first_changed_charpos, last_changed_charpos;
15553
15554 #if GLYPH_DEBUG
15555 if (inhibit_try_window_id)
15556 return 0;
15557 #endif
15558
15559 /* This is handy for debugging. */
15560 #if 0
15561 #define GIVE_UP(X) \
15562 do { \
15563 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15564 return 0; \
15565 } while (0)
15566 #else
15567 #define GIVE_UP(X) return 0
15568 #endif
15569
15570 SET_TEXT_POS_FROM_MARKER (start, w->start);
15571
15572 /* Don't use this for mini-windows because these can show
15573 messages and mini-buffers, and we don't handle that here. */
15574 if (MINI_WINDOW_P (w))
15575 GIVE_UP (1);
15576
15577 /* This flag is used to prevent redisplay optimizations. */
15578 if (windows_or_buffers_changed || cursor_type_changed)
15579 GIVE_UP (2);
15580
15581 /* Verify that narrowing has not changed.
15582 Also verify that we were not told to prevent redisplay optimizations.
15583 It would be nice to further
15584 reduce the number of cases where this prevents try_window_id. */
15585 if (current_buffer->clip_changed
15586 || current_buffer->prevent_redisplay_optimizations_p)
15587 GIVE_UP (3);
15588
15589 /* Window must either use window-based redisplay or be full width. */
15590 if (!FRAME_WINDOW_P (f)
15591 && (!FRAME_LINE_INS_DEL_OK (f)
15592 || !WINDOW_FULL_WIDTH_P (w)))
15593 GIVE_UP (4);
15594
15595 /* Give up if point is known NOT to appear in W. */
15596 if (PT < CHARPOS (start))
15597 GIVE_UP (5);
15598
15599 /* Another way to prevent redisplay optimizations. */
15600 if (XFASTINT (w->last_modified) == 0)
15601 GIVE_UP (6);
15602
15603 /* Verify that window is not hscrolled. */
15604 if (XFASTINT (w->hscroll) != 0)
15605 GIVE_UP (7);
15606
15607 /* Verify that display wasn't paused. */
15608 if (NILP (w->window_end_valid))
15609 GIVE_UP (8);
15610
15611 /* Can't use this if highlighting a region because a cursor movement
15612 will do more than just set the cursor. */
15613 if (!NILP (Vtransient_mark_mode)
15614 && !NILP (BVAR (current_buffer, mark_active)))
15615 GIVE_UP (9);
15616
15617 /* Likewise if highlighting trailing whitespace. */
15618 if (!NILP (Vshow_trailing_whitespace))
15619 GIVE_UP (11);
15620
15621 /* Likewise if showing a region. */
15622 if (!NILP (w->region_showing))
15623 GIVE_UP (10);
15624
15625 /* Can't use this if overlay arrow position and/or string have
15626 changed. */
15627 if (overlay_arrows_changed_p ())
15628 GIVE_UP (12);
15629
15630 /* When word-wrap is on, adding a space to the first word of a
15631 wrapped line can change the wrap position, altering the line
15632 above it. It might be worthwhile to handle this more
15633 intelligently, but for now just redisplay from scratch. */
15634 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
15635 GIVE_UP (21);
15636
15637 /* Under bidi reordering, adding or deleting a character in the
15638 beginning of a paragraph, before the first strong directional
15639 character, can change the base direction of the paragraph (unless
15640 the buffer specifies a fixed paragraph direction), which will
15641 require to redisplay the whole paragraph. It might be worthwhile
15642 to find the paragraph limits and widen the range of redisplayed
15643 lines to that, but for now just give up this optimization and
15644 redisplay from scratch. */
15645 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15646 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
15647 GIVE_UP (22);
15648
15649 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15650 only if buffer has really changed. The reason is that the gap is
15651 initially at Z for freshly visited files. The code below would
15652 set end_unchanged to 0 in that case. */
15653 if (MODIFF > SAVE_MODIFF
15654 /* This seems to happen sometimes after saving a buffer. */
15655 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15656 {
15657 if (GPT - BEG < BEG_UNCHANGED)
15658 BEG_UNCHANGED = GPT - BEG;
15659 if (Z - GPT < END_UNCHANGED)
15660 END_UNCHANGED = Z - GPT;
15661 }
15662
15663 /* The position of the first and last character that has been changed. */
15664 first_changed_charpos = BEG + BEG_UNCHANGED;
15665 last_changed_charpos = Z - END_UNCHANGED;
15666
15667 /* If window starts after a line end, and the last change is in
15668 front of that newline, then changes don't affect the display.
15669 This case happens with stealth-fontification. Note that although
15670 the display is unchanged, glyph positions in the matrix have to
15671 be adjusted, of course. */
15672 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15673 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15674 && ((last_changed_charpos < CHARPOS (start)
15675 && CHARPOS (start) == BEGV)
15676 || (last_changed_charpos < CHARPOS (start) - 1
15677 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15678 {
15679 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
15680 struct glyph_row *r0;
15681
15682 /* Compute how many chars/bytes have been added to or removed
15683 from the buffer. */
15684 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15685 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15686 Z_delta = Z - Z_old;
15687 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
15688
15689 /* Give up if PT is not in the window. Note that it already has
15690 been checked at the start of try_window_id that PT is not in
15691 front of the window start. */
15692 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
15693 GIVE_UP (13);
15694
15695 /* If window start is unchanged, we can reuse the whole matrix
15696 as is, after adjusting glyph positions. No need to compute
15697 the window end again, since its offset from Z hasn't changed. */
15698 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15699 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
15700 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
15701 /* PT must not be in a partially visible line. */
15702 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
15703 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15704 {
15705 /* Adjust positions in the glyph matrix. */
15706 if (Z_delta || Z_delta_bytes)
15707 {
15708 struct glyph_row *r1
15709 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15710 increment_matrix_positions (w->current_matrix,
15711 MATRIX_ROW_VPOS (r0, current_matrix),
15712 MATRIX_ROW_VPOS (r1, current_matrix),
15713 Z_delta, Z_delta_bytes);
15714 }
15715
15716 /* Set the cursor. */
15717 row = row_containing_pos (w, PT, r0, NULL, 0);
15718 if (row)
15719 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15720 else
15721 abort ();
15722 return 1;
15723 }
15724 }
15725
15726 /* Handle the case that changes are all below what is displayed in
15727 the window, and that PT is in the window. This shortcut cannot
15728 be taken if ZV is visible in the window, and text has been added
15729 there that is visible in the window. */
15730 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15731 /* ZV is not visible in the window, or there are no
15732 changes at ZV, actually. */
15733 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15734 || first_changed_charpos == last_changed_charpos))
15735 {
15736 struct glyph_row *r0;
15737
15738 /* Give up if PT is not in the window. Note that it already has
15739 been checked at the start of try_window_id that PT is not in
15740 front of the window start. */
15741 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15742 GIVE_UP (14);
15743
15744 /* If window start is unchanged, we can reuse the whole matrix
15745 as is, without changing glyph positions since no text has
15746 been added/removed in front of the window end. */
15747 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15748 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15749 /* PT must not be in a partially visible line. */
15750 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15751 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15752 {
15753 /* We have to compute the window end anew since text
15754 could have been added/removed after it. */
15755 w->window_end_pos
15756 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15757 w->window_end_bytepos
15758 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15759
15760 /* Set the cursor. */
15761 row = row_containing_pos (w, PT, r0, NULL, 0);
15762 if (row)
15763 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15764 else
15765 abort ();
15766 return 2;
15767 }
15768 }
15769
15770 /* Give up if window start is in the changed area.
15771
15772 The condition used to read
15773
15774 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15775
15776 but why that was tested escapes me at the moment. */
15777 if (CHARPOS (start) >= first_changed_charpos
15778 && CHARPOS (start) <= last_changed_charpos)
15779 GIVE_UP (15);
15780
15781 /* Check that window start agrees with the start of the first glyph
15782 row in its current matrix. Check this after we know the window
15783 start is not in changed text, otherwise positions would not be
15784 comparable. */
15785 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15786 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15787 GIVE_UP (16);
15788
15789 /* Give up if the window ends in strings. Overlay strings
15790 at the end are difficult to handle, so don't try. */
15791 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15792 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15793 GIVE_UP (20);
15794
15795 /* Compute the position at which we have to start displaying new
15796 lines. Some of the lines at the top of the window might be
15797 reusable because they are not displaying changed text. Find the
15798 last row in W's current matrix not affected by changes at the
15799 start of current_buffer. Value is null if changes start in the
15800 first line of window. */
15801 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15802 if (last_unchanged_at_beg_row)
15803 {
15804 /* Avoid starting to display in the moddle of a character, a TAB
15805 for instance. This is easier than to set up the iterator
15806 exactly, and it's not a frequent case, so the additional
15807 effort wouldn't really pay off. */
15808 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15809 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15810 && last_unchanged_at_beg_row > w->current_matrix->rows)
15811 --last_unchanged_at_beg_row;
15812
15813 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15814 GIVE_UP (17);
15815
15816 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15817 GIVE_UP (18);
15818 start_pos = it.current.pos;
15819
15820 /* Start displaying new lines in the desired matrix at the same
15821 vpos we would use in the current matrix, i.e. below
15822 last_unchanged_at_beg_row. */
15823 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15824 current_matrix);
15825 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15826 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15827
15828 xassert (it.hpos == 0 && it.current_x == 0);
15829 }
15830 else
15831 {
15832 /* There are no reusable lines at the start of the window.
15833 Start displaying in the first text line. */
15834 start_display (&it, w, start);
15835 it.vpos = it.first_vpos;
15836 start_pos = it.current.pos;
15837 }
15838
15839 /* Find the first row that is not affected by changes at the end of
15840 the buffer. Value will be null if there is no unchanged row, in
15841 which case we must redisplay to the end of the window. delta
15842 will be set to the value by which buffer positions beginning with
15843 first_unchanged_at_end_row have to be adjusted due to text
15844 changes. */
15845 first_unchanged_at_end_row
15846 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15847 IF_DEBUG (debug_delta = delta);
15848 IF_DEBUG (debug_delta_bytes = delta_bytes);
15849
15850 /* Set stop_pos to the buffer position up to which we will have to
15851 display new lines. If first_unchanged_at_end_row != NULL, this
15852 is the buffer position of the start of the line displayed in that
15853 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15854 that we don't stop at a buffer position. */
15855 stop_pos = 0;
15856 if (first_unchanged_at_end_row)
15857 {
15858 xassert (last_unchanged_at_beg_row == NULL
15859 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15860
15861 /* If this is a continuation line, move forward to the next one
15862 that isn't. Changes in lines above affect this line.
15863 Caution: this may move first_unchanged_at_end_row to a row
15864 not displaying text. */
15865 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15866 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15867 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15868 < it.last_visible_y))
15869 ++first_unchanged_at_end_row;
15870
15871 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15872 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15873 >= it.last_visible_y))
15874 first_unchanged_at_end_row = NULL;
15875 else
15876 {
15877 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15878 + delta);
15879 first_unchanged_at_end_vpos
15880 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15881 xassert (stop_pos >= Z - END_UNCHANGED);
15882 }
15883 }
15884 else if (last_unchanged_at_beg_row == NULL)
15885 GIVE_UP (19);
15886
15887
15888 #if GLYPH_DEBUG
15889
15890 /* Either there is no unchanged row at the end, or the one we have
15891 now displays text. This is a necessary condition for the window
15892 end pos calculation at the end of this function. */
15893 xassert (first_unchanged_at_end_row == NULL
15894 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15895
15896 debug_last_unchanged_at_beg_vpos
15897 = (last_unchanged_at_beg_row
15898 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15899 : -1);
15900 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15901
15902 #endif /* GLYPH_DEBUG != 0 */
15903
15904
15905 /* Display new lines. Set last_text_row to the last new line
15906 displayed which has text on it, i.e. might end up as being the
15907 line where the window_end_vpos is. */
15908 w->cursor.vpos = -1;
15909 last_text_row = NULL;
15910 overlay_arrow_seen = 0;
15911 while (it.current_y < it.last_visible_y
15912 && !fonts_changed_p
15913 && (first_unchanged_at_end_row == NULL
15914 || IT_CHARPOS (it) < stop_pos))
15915 {
15916 if (display_line (&it))
15917 last_text_row = it.glyph_row - 1;
15918 }
15919
15920 if (fonts_changed_p)
15921 return -1;
15922
15923
15924 /* Compute differences in buffer positions, y-positions etc. for
15925 lines reused at the bottom of the window. Compute what we can
15926 scroll. */
15927 if (first_unchanged_at_end_row
15928 /* No lines reused because we displayed everything up to the
15929 bottom of the window. */
15930 && it.current_y < it.last_visible_y)
15931 {
15932 dvpos = (it.vpos
15933 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15934 current_matrix));
15935 dy = it.current_y - first_unchanged_at_end_row->y;
15936 run.current_y = first_unchanged_at_end_row->y;
15937 run.desired_y = run.current_y + dy;
15938 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15939 }
15940 else
15941 {
15942 delta = delta_bytes = dvpos = dy
15943 = run.current_y = run.desired_y = run.height = 0;
15944 first_unchanged_at_end_row = NULL;
15945 }
15946 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15947
15948
15949 /* Find the cursor if not already found. We have to decide whether
15950 PT will appear on this window (it sometimes doesn't, but this is
15951 not a very frequent case.) This decision has to be made before
15952 the current matrix is altered. A value of cursor.vpos < 0 means
15953 that PT is either in one of the lines beginning at
15954 first_unchanged_at_end_row or below the window. Don't care for
15955 lines that might be displayed later at the window end; as
15956 mentioned, this is not a frequent case. */
15957 if (w->cursor.vpos < 0)
15958 {
15959 /* Cursor in unchanged rows at the top? */
15960 if (PT < CHARPOS (start_pos)
15961 && last_unchanged_at_beg_row)
15962 {
15963 row = row_containing_pos (w, PT,
15964 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15965 last_unchanged_at_beg_row + 1, 0);
15966 if (row)
15967 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15968 }
15969
15970 /* Start from first_unchanged_at_end_row looking for PT. */
15971 else if (first_unchanged_at_end_row)
15972 {
15973 row = row_containing_pos (w, PT - delta,
15974 first_unchanged_at_end_row, NULL, 0);
15975 if (row)
15976 set_cursor_from_row (w, row, w->current_matrix, delta,
15977 delta_bytes, dy, dvpos);
15978 }
15979
15980 /* Give up if cursor was not found. */
15981 if (w->cursor.vpos < 0)
15982 {
15983 clear_glyph_matrix (w->desired_matrix);
15984 return -1;
15985 }
15986 }
15987
15988 /* Don't let the cursor end in the scroll margins. */
15989 {
15990 int this_scroll_margin, cursor_height;
15991
15992 this_scroll_margin = max (0, scroll_margin);
15993 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15994 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15995 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15996
15997 if ((w->cursor.y < this_scroll_margin
15998 && CHARPOS (start) > BEGV)
15999 /* Old redisplay didn't take scroll margin into account at the bottom,
16000 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16001 || (w->cursor.y + (make_cursor_line_fully_visible_p
16002 ? cursor_height + this_scroll_margin
16003 : 1)) > it.last_visible_y)
16004 {
16005 w->cursor.vpos = -1;
16006 clear_glyph_matrix (w->desired_matrix);
16007 return -1;
16008 }
16009 }
16010
16011 /* Scroll the display. Do it before changing the current matrix so
16012 that xterm.c doesn't get confused about where the cursor glyph is
16013 found. */
16014 if (dy && run.height)
16015 {
16016 update_begin (f);
16017
16018 if (FRAME_WINDOW_P (f))
16019 {
16020 FRAME_RIF (f)->update_window_begin_hook (w);
16021 FRAME_RIF (f)->clear_window_mouse_face (w);
16022 FRAME_RIF (f)->scroll_run_hook (w, &run);
16023 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16024 }
16025 else
16026 {
16027 /* Terminal frame. In this case, dvpos gives the number of
16028 lines to scroll by; dvpos < 0 means scroll up. */
16029 int from_vpos
16030 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16031 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16032 int end = (WINDOW_TOP_EDGE_LINE (w)
16033 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16034 + window_internal_height (w));
16035
16036 #if defined (HAVE_GPM) || defined (MSDOS)
16037 x_clear_window_mouse_face (w);
16038 #endif
16039 /* Perform the operation on the screen. */
16040 if (dvpos > 0)
16041 {
16042 /* Scroll last_unchanged_at_beg_row to the end of the
16043 window down dvpos lines. */
16044 set_terminal_window (f, end);
16045
16046 /* On dumb terminals delete dvpos lines at the end
16047 before inserting dvpos empty lines. */
16048 if (!FRAME_SCROLL_REGION_OK (f))
16049 ins_del_lines (f, end - dvpos, -dvpos);
16050
16051 /* Insert dvpos empty lines in front of
16052 last_unchanged_at_beg_row. */
16053 ins_del_lines (f, from, dvpos);
16054 }
16055 else if (dvpos < 0)
16056 {
16057 /* Scroll up last_unchanged_at_beg_vpos to the end of
16058 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16059 set_terminal_window (f, end);
16060
16061 /* Delete dvpos lines in front of
16062 last_unchanged_at_beg_vpos. ins_del_lines will set
16063 the cursor to the given vpos and emit |dvpos| delete
16064 line sequences. */
16065 ins_del_lines (f, from + dvpos, dvpos);
16066
16067 /* On a dumb terminal insert dvpos empty lines at the
16068 end. */
16069 if (!FRAME_SCROLL_REGION_OK (f))
16070 ins_del_lines (f, end + dvpos, -dvpos);
16071 }
16072
16073 set_terminal_window (f, 0);
16074 }
16075
16076 update_end (f);
16077 }
16078
16079 /* Shift reused rows of the current matrix to the right position.
16080 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16081 text. */
16082 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16083 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16084 if (dvpos < 0)
16085 {
16086 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16087 bottom_vpos, dvpos);
16088 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16089 bottom_vpos, 0);
16090 }
16091 else if (dvpos > 0)
16092 {
16093 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16094 bottom_vpos, dvpos);
16095 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16096 first_unchanged_at_end_vpos + dvpos, 0);
16097 }
16098
16099 /* For frame-based redisplay, make sure that current frame and window
16100 matrix are in sync with respect to glyph memory. */
16101 if (!FRAME_WINDOW_P (f))
16102 sync_frame_with_window_matrix_rows (w);
16103
16104 /* Adjust buffer positions in reused rows. */
16105 if (delta || delta_bytes)
16106 increment_matrix_positions (current_matrix,
16107 first_unchanged_at_end_vpos + dvpos,
16108 bottom_vpos, delta, delta_bytes);
16109
16110 /* Adjust Y positions. */
16111 if (dy)
16112 shift_glyph_matrix (w, current_matrix,
16113 first_unchanged_at_end_vpos + dvpos,
16114 bottom_vpos, dy);
16115
16116 if (first_unchanged_at_end_row)
16117 {
16118 first_unchanged_at_end_row += dvpos;
16119 if (first_unchanged_at_end_row->y >= it.last_visible_y
16120 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16121 first_unchanged_at_end_row = NULL;
16122 }
16123
16124 /* If scrolling up, there may be some lines to display at the end of
16125 the window. */
16126 last_text_row_at_end = NULL;
16127 if (dy < 0)
16128 {
16129 /* Scrolling up can leave for example a partially visible line
16130 at the end of the window to be redisplayed. */
16131 /* Set last_row to the glyph row in the current matrix where the
16132 window end line is found. It has been moved up or down in
16133 the matrix by dvpos. */
16134 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16135 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16136
16137 /* If last_row is the window end line, it should display text. */
16138 xassert (last_row->displays_text_p);
16139
16140 /* If window end line was partially visible before, begin
16141 displaying at that line. Otherwise begin displaying with the
16142 line following it. */
16143 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16144 {
16145 init_to_row_start (&it, w, last_row);
16146 it.vpos = last_vpos;
16147 it.current_y = last_row->y;
16148 }
16149 else
16150 {
16151 init_to_row_end (&it, w, last_row);
16152 it.vpos = 1 + last_vpos;
16153 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16154 ++last_row;
16155 }
16156
16157 /* We may start in a continuation line. If so, we have to
16158 get the right continuation_lines_width and current_x. */
16159 it.continuation_lines_width = last_row->continuation_lines_width;
16160 it.hpos = it.current_x = 0;
16161
16162 /* Display the rest of the lines at the window end. */
16163 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16164 while (it.current_y < it.last_visible_y
16165 && !fonts_changed_p)
16166 {
16167 /* Is it always sure that the display agrees with lines in
16168 the current matrix? I don't think so, so we mark rows
16169 displayed invalid in the current matrix by setting their
16170 enabled_p flag to zero. */
16171 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16172 if (display_line (&it))
16173 last_text_row_at_end = it.glyph_row - 1;
16174 }
16175 }
16176
16177 /* Update window_end_pos and window_end_vpos. */
16178 if (first_unchanged_at_end_row
16179 && !last_text_row_at_end)
16180 {
16181 /* Window end line if one of the preserved rows from the current
16182 matrix. Set row to the last row displaying text in current
16183 matrix starting at first_unchanged_at_end_row, after
16184 scrolling. */
16185 xassert (first_unchanged_at_end_row->displays_text_p);
16186 row = find_last_row_displaying_text (w->current_matrix, &it,
16187 first_unchanged_at_end_row);
16188 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16189
16190 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16191 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16192 w->window_end_vpos
16193 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16194 xassert (w->window_end_bytepos >= 0);
16195 IF_DEBUG (debug_method_add (w, "A"));
16196 }
16197 else if (last_text_row_at_end)
16198 {
16199 w->window_end_pos
16200 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16201 w->window_end_bytepos
16202 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16203 w->window_end_vpos
16204 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16205 xassert (w->window_end_bytepos >= 0);
16206 IF_DEBUG (debug_method_add (w, "B"));
16207 }
16208 else if (last_text_row)
16209 {
16210 /* We have displayed either to the end of the window or at the
16211 end of the window, i.e. the last row with text is to be found
16212 in the desired matrix. */
16213 w->window_end_pos
16214 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16215 w->window_end_bytepos
16216 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16217 w->window_end_vpos
16218 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16219 xassert (w->window_end_bytepos >= 0);
16220 }
16221 else if (first_unchanged_at_end_row == NULL
16222 && last_text_row == NULL
16223 && last_text_row_at_end == NULL)
16224 {
16225 /* Displayed to end of window, but no line containing text was
16226 displayed. Lines were deleted at the end of the window. */
16227 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16228 int vpos = XFASTINT (w->window_end_vpos);
16229 struct glyph_row *current_row = current_matrix->rows + vpos;
16230 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16231
16232 for (row = NULL;
16233 row == NULL && vpos >= first_vpos;
16234 --vpos, --current_row, --desired_row)
16235 {
16236 if (desired_row->enabled_p)
16237 {
16238 if (desired_row->displays_text_p)
16239 row = desired_row;
16240 }
16241 else if (current_row->displays_text_p)
16242 row = current_row;
16243 }
16244
16245 xassert (row != NULL);
16246 w->window_end_vpos = make_number (vpos + 1);
16247 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16248 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16249 xassert (w->window_end_bytepos >= 0);
16250 IF_DEBUG (debug_method_add (w, "C"));
16251 }
16252 else
16253 abort ();
16254
16255 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16256 debug_end_vpos = XFASTINT (w->window_end_vpos));
16257
16258 /* Record that display has not been completed. */
16259 w->window_end_valid = Qnil;
16260 w->desired_matrix->no_scrolling_p = 1;
16261 return 3;
16262
16263 #undef GIVE_UP
16264 }
16265
16266
16267 \f
16268 /***********************************************************************
16269 More debugging support
16270 ***********************************************************************/
16271
16272 #if GLYPH_DEBUG
16273
16274 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
16275 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
16276 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
16277
16278
16279 /* Dump the contents of glyph matrix MATRIX on stderr.
16280
16281 GLYPHS 0 means don't show glyph contents.
16282 GLYPHS 1 means show glyphs in short form
16283 GLYPHS > 1 means show glyphs in long form. */
16284
16285 void
16286 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
16287 {
16288 int i;
16289 for (i = 0; i < matrix->nrows; ++i)
16290 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16291 }
16292
16293
16294 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16295 the glyph row and area where the glyph comes from. */
16296
16297 void
16298 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
16299 {
16300 if (glyph->type == CHAR_GLYPH)
16301 {
16302 fprintf (stderr,
16303 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16304 glyph - row->glyphs[TEXT_AREA],
16305 'C',
16306 glyph->charpos,
16307 (BUFFERP (glyph->object)
16308 ? 'B'
16309 : (STRINGP (glyph->object)
16310 ? 'S'
16311 : '-')),
16312 glyph->pixel_width,
16313 glyph->u.ch,
16314 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16315 ? glyph->u.ch
16316 : '.'),
16317 glyph->face_id,
16318 glyph->left_box_line_p,
16319 glyph->right_box_line_p);
16320 }
16321 else if (glyph->type == STRETCH_GLYPH)
16322 {
16323 fprintf (stderr,
16324 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16325 glyph - row->glyphs[TEXT_AREA],
16326 'S',
16327 glyph->charpos,
16328 (BUFFERP (glyph->object)
16329 ? 'B'
16330 : (STRINGP (glyph->object)
16331 ? 'S'
16332 : '-')),
16333 glyph->pixel_width,
16334 0,
16335 '.',
16336 glyph->face_id,
16337 glyph->left_box_line_p,
16338 glyph->right_box_line_p);
16339 }
16340 else if (glyph->type == IMAGE_GLYPH)
16341 {
16342 fprintf (stderr,
16343 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16344 glyph - row->glyphs[TEXT_AREA],
16345 'I',
16346 glyph->charpos,
16347 (BUFFERP (glyph->object)
16348 ? 'B'
16349 : (STRINGP (glyph->object)
16350 ? 'S'
16351 : '-')),
16352 glyph->pixel_width,
16353 glyph->u.img_id,
16354 '.',
16355 glyph->face_id,
16356 glyph->left_box_line_p,
16357 glyph->right_box_line_p);
16358 }
16359 else if (glyph->type == COMPOSITE_GLYPH)
16360 {
16361 fprintf (stderr,
16362 " %5td %4c %6"pI"d %c %3d 0x%05x",
16363 glyph - row->glyphs[TEXT_AREA],
16364 '+',
16365 glyph->charpos,
16366 (BUFFERP (glyph->object)
16367 ? 'B'
16368 : (STRINGP (glyph->object)
16369 ? 'S'
16370 : '-')),
16371 glyph->pixel_width,
16372 glyph->u.cmp.id);
16373 if (glyph->u.cmp.automatic)
16374 fprintf (stderr,
16375 "[%d-%d]",
16376 glyph->slice.cmp.from, glyph->slice.cmp.to);
16377 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16378 glyph->face_id,
16379 glyph->left_box_line_p,
16380 glyph->right_box_line_p);
16381 }
16382 }
16383
16384
16385 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16386 GLYPHS 0 means don't show glyph contents.
16387 GLYPHS 1 means show glyphs in short form
16388 GLYPHS > 1 means show glyphs in long form. */
16389
16390 void
16391 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
16392 {
16393 if (glyphs != 1)
16394 {
16395 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16396 fprintf (stderr, "======================================================================\n");
16397
16398 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
16399 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16400 vpos,
16401 MATRIX_ROW_START_CHARPOS (row),
16402 MATRIX_ROW_END_CHARPOS (row),
16403 row->used[TEXT_AREA],
16404 row->contains_overlapping_glyphs_p,
16405 row->enabled_p,
16406 row->truncated_on_left_p,
16407 row->truncated_on_right_p,
16408 row->continued_p,
16409 MATRIX_ROW_CONTINUATION_LINE_P (row),
16410 row->displays_text_p,
16411 row->ends_at_zv_p,
16412 row->fill_line_p,
16413 row->ends_in_middle_of_char_p,
16414 row->starts_in_middle_of_char_p,
16415 row->mouse_face_p,
16416 row->x,
16417 row->y,
16418 row->pixel_width,
16419 row->height,
16420 row->visible_height,
16421 row->ascent,
16422 row->phys_ascent);
16423 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16424 row->end.overlay_string_index,
16425 row->continuation_lines_width);
16426 fprintf (stderr, "%9"pI"d %5"pI"d\n",
16427 CHARPOS (row->start.string_pos),
16428 CHARPOS (row->end.string_pos));
16429 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16430 row->end.dpvec_index);
16431 }
16432
16433 if (glyphs > 1)
16434 {
16435 int area;
16436
16437 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16438 {
16439 struct glyph *glyph = row->glyphs[area];
16440 struct glyph *glyph_end = glyph + row->used[area];
16441
16442 /* Glyph for a line end in text. */
16443 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16444 ++glyph_end;
16445
16446 if (glyph < glyph_end)
16447 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16448
16449 for (; glyph < glyph_end; ++glyph)
16450 dump_glyph (row, glyph, area);
16451 }
16452 }
16453 else if (glyphs == 1)
16454 {
16455 int area;
16456
16457 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16458 {
16459 char *s = (char *) alloca (row->used[area] + 1);
16460 int i;
16461
16462 for (i = 0; i < row->used[area]; ++i)
16463 {
16464 struct glyph *glyph = row->glyphs[area] + i;
16465 if (glyph->type == CHAR_GLYPH
16466 && glyph->u.ch < 0x80
16467 && glyph->u.ch >= ' ')
16468 s[i] = glyph->u.ch;
16469 else
16470 s[i] = '.';
16471 }
16472
16473 s[i] = '\0';
16474 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16475 }
16476 }
16477 }
16478
16479
16480 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16481 Sdump_glyph_matrix, 0, 1, "p",
16482 doc: /* Dump the current matrix of the selected window to stderr.
16483 Shows contents of glyph row structures. With non-nil
16484 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16485 glyphs in short form, otherwise show glyphs in long form. */)
16486 (Lisp_Object glyphs)
16487 {
16488 struct window *w = XWINDOW (selected_window);
16489 struct buffer *buffer = XBUFFER (w->buffer);
16490
16491 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
16492 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16493 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16494 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16495 fprintf (stderr, "=============================================\n");
16496 dump_glyph_matrix (w->current_matrix,
16497 NILP (glyphs) ? 0 : XINT (glyphs));
16498 return Qnil;
16499 }
16500
16501
16502 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16503 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16504 (void)
16505 {
16506 struct frame *f = XFRAME (selected_frame);
16507 dump_glyph_matrix (f->current_matrix, 1);
16508 return Qnil;
16509 }
16510
16511
16512 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16513 doc: /* Dump glyph row ROW to stderr.
16514 GLYPH 0 means don't dump glyphs.
16515 GLYPH 1 means dump glyphs in short form.
16516 GLYPH > 1 or omitted means dump glyphs in long form. */)
16517 (Lisp_Object row, Lisp_Object glyphs)
16518 {
16519 struct glyph_matrix *matrix;
16520 int vpos;
16521
16522 CHECK_NUMBER (row);
16523 matrix = XWINDOW (selected_window)->current_matrix;
16524 vpos = XINT (row);
16525 if (vpos >= 0 && vpos < matrix->nrows)
16526 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16527 vpos,
16528 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16529 return Qnil;
16530 }
16531
16532
16533 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16534 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16535 GLYPH 0 means don't dump glyphs.
16536 GLYPH 1 means dump glyphs in short form.
16537 GLYPH > 1 or omitted means dump glyphs in long form. */)
16538 (Lisp_Object row, Lisp_Object glyphs)
16539 {
16540 struct frame *sf = SELECTED_FRAME ();
16541 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16542 int vpos;
16543
16544 CHECK_NUMBER (row);
16545 vpos = XINT (row);
16546 if (vpos >= 0 && vpos < m->nrows)
16547 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16548 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16549 return Qnil;
16550 }
16551
16552
16553 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16554 doc: /* Toggle tracing of redisplay.
16555 With ARG, turn tracing on if and only if ARG is positive. */)
16556 (Lisp_Object arg)
16557 {
16558 if (NILP (arg))
16559 trace_redisplay_p = !trace_redisplay_p;
16560 else
16561 {
16562 arg = Fprefix_numeric_value (arg);
16563 trace_redisplay_p = XINT (arg) > 0;
16564 }
16565
16566 return Qnil;
16567 }
16568
16569
16570 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16571 doc: /* Like `format', but print result to stderr.
16572 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16573 (ptrdiff_t nargs, Lisp_Object *args)
16574 {
16575 Lisp_Object s = Fformat (nargs, args);
16576 fprintf (stderr, "%s", SDATA (s));
16577 return Qnil;
16578 }
16579
16580 #endif /* GLYPH_DEBUG */
16581
16582
16583 \f
16584 /***********************************************************************
16585 Building Desired Matrix Rows
16586 ***********************************************************************/
16587
16588 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16589 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16590
16591 static struct glyph_row *
16592 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16593 {
16594 struct frame *f = XFRAME (WINDOW_FRAME (w));
16595 struct buffer *buffer = XBUFFER (w->buffer);
16596 struct buffer *old = current_buffer;
16597 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16598 int arrow_len = SCHARS (overlay_arrow_string);
16599 const unsigned char *arrow_end = arrow_string + arrow_len;
16600 const unsigned char *p;
16601 struct it it;
16602 int multibyte_p;
16603 int n_glyphs_before;
16604
16605 set_buffer_temp (buffer);
16606 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16607 it.glyph_row->used[TEXT_AREA] = 0;
16608 SET_TEXT_POS (it.position, 0, 0);
16609
16610 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
16611 p = arrow_string;
16612 while (p < arrow_end)
16613 {
16614 Lisp_Object face, ilisp;
16615
16616 /* Get the next character. */
16617 if (multibyte_p)
16618 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16619 else
16620 {
16621 it.c = it.char_to_display = *p, it.len = 1;
16622 if (! ASCII_CHAR_P (it.c))
16623 it.char_to_display = BYTE8_TO_CHAR (it.c);
16624 }
16625 p += it.len;
16626
16627 /* Get its face. */
16628 ilisp = make_number (p - arrow_string);
16629 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16630 it.face_id = compute_char_face (f, it.char_to_display, face);
16631
16632 /* Compute its width, get its glyphs. */
16633 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16634 SET_TEXT_POS (it.position, -1, -1);
16635 PRODUCE_GLYPHS (&it);
16636
16637 /* If this character doesn't fit any more in the line, we have
16638 to remove some glyphs. */
16639 if (it.current_x > it.last_visible_x)
16640 {
16641 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16642 break;
16643 }
16644 }
16645
16646 set_buffer_temp (old);
16647 return it.glyph_row;
16648 }
16649
16650
16651 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16652 glyphs are only inserted for terminal frames since we can't really
16653 win with truncation glyphs when partially visible glyphs are
16654 involved. Which glyphs to insert is determined by
16655 produce_special_glyphs. */
16656
16657 static void
16658 insert_left_trunc_glyphs (struct it *it)
16659 {
16660 struct it truncate_it;
16661 struct glyph *from, *end, *to, *toend;
16662
16663 xassert (!FRAME_WINDOW_P (it->f));
16664
16665 /* Get the truncation glyphs. */
16666 truncate_it = *it;
16667 truncate_it.current_x = 0;
16668 truncate_it.face_id = DEFAULT_FACE_ID;
16669 truncate_it.glyph_row = &scratch_glyph_row;
16670 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16671 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16672 truncate_it.object = make_number (0);
16673 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16674
16675 /* Overwrite glyphs from IT with truncation glyphs. */
16676 if (!it->glyph_row->reversed_p)
16677 {
16678 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16679 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16680 to = it->glyph_row->glyphs[TEXT_AREA];
16681 toend = to + it->glyph_row->used[TEXT_AREA];
16682
16683 while (from < end)
16684 *to++ = *from++;
16685
16686 /* There may be padding glyphs left over. Overwrite them too. */
16687 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16688 {
16689 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16690 while (from < end)
16691 *to++ = *from++;
16692 }
16693
16694 if (to > toend)
16695 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16696 }
16697 else
16698 {
16699 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16700 that back to front. */
16701 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16702 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16703 toend = it->glyph_row->glyphs[TEXT_AREA];
16704 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16705
16706 while (from >= end && to >= toend)
16707 *to-- = *from--;
16708 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16709 {
16710 from =
16711 truncate_it.glyph_row->glyphs[TEXT_AREA]
16712 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16713 while (from >= end && to >= toend)
16714 *to-- = *from--;
16715 }
16716 if (from >= end)
16717 {
16718 /* Need to free some room before prepending additional
16719 glyphs. */
16720 int move_by = from - end + 1;
16721 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16722 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16723
16724 for ( ; g >= g0; g--)
16725 g[move_by] = *g;
16726 while (from >= end)
16727 *to-- = *from--;
16728 it->glyph_row->used[TEXT_AREA] += move_by;
16729 }
16730 }
16731 }
16732
16733
16734 /* Compute the pixel height and width of IT->glyph_row.
16735
16736 Most of the time, ascent and height of a display line will be equal
16737 to the max_ascent and max_height values of the display iterator
16738 structure. This is not the case if
16739
16740 1. We hit ZV without displaying anything. In this case, max_ascent
16741 and max_height will be zero.
16742
16743 2. We have some glyphs that don't contribute to the line height.
16744 (The glyph row flag contributes_to_line_height_p is for future
16745 pixmap extensions).
16746
16747 The first case is easily covered by using default values because in
16748 these cases, the line height does not really matter, except that it
16749 must not be zero. */
16750
16751 static void
16752 compute_line_metrics (struct it *it)
16753 {
16754 struct glyph_row *row = it->glyph_row;
16755
16756 if (FRAME_WINDOW_P (it->f))
16757 {
16758 int i, min_y, max_y;
16759
16760 /* The line may consist of one space only, that was added to
16761 place the cursor on it. If so, the row's height hasn't been
16762 computed yet. */
16763 if (row->height == 0)
16764 {
16765 if (it->max_ascent + it->max_descent == 0)
16766 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16767 row->ascent = it->max_ascent;
16768 row->height = it->max_ascent + it->max_descent;
16769 row->phys_ascent = it->max_phys_ascent;
16770 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16771 row->extra_line_spacing = it->max_extra_line_spacing;
16772 }
16773
16774 /* Compute the width of this line. */
16775 row->pixel_width = row->x;
16776 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16777 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16778
16779 xassert (row->pixel_width >= 0);
16780 xassert (row->ascent >= 0 && row->height > 0);
16781
16782 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16783 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16784
16785 /* If first line's physical ascent is larger than its logical
16786 ascent, use the physical ascent, and make the row taller.
16787 This makes accented characters fully visible. */
16788 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16789 && row->phys_ascent > row->ascent)
16790 {
16791 row->height += row->phys_ascent - row->ascent;
16792 row->ascent = row->phys_ascent;
16793 }
16794
16795 /* Compute how much of the line is visible. */
16796 row->visible_height = row->height;
16797
16798 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16799 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16800
16801 if (row->y < min_y)
16802 row->visible_height -= min_y - row->y;
16803 if (row->y + row->height > max_y)
16804 row->visible_height -= row->y + row->height - max_y;
16805 }
16806 else
16807 {
16808 row->pixel_width = row->used[TEXT_AREA];
16809 if (row->continued_p)
16810 row->pixel_width -= it->continuation_pixel_width;
16811 else if (row->truncated_on_right_p)
16812 row->pixel_width -= it->truncation_pixel_width;
16813 row->ascent = row->phys_ascent = 0;
16814 row->height = row->phys_height = row->visible_height = 1;
16815 row->extra_line_spacing = 0;
16816 }
16817
16818 /* Compute a hash code for this row. */
16819 {
16820 int area, i;
16821 row->hash = 0;
16822 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16823 for (i = 0; i < row->used[area]; ++i)
16824 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16825 + row->glyphs[area][i].u.val
16826 + row->glyphs[area][i].face_id
16827 + row->glyphs[area][i].padding_p
16828 + (row->glyphs[area][i].type << 2));
16829 }
16830
16831 it->max_ascent = it->max_descent = 0;
16832 it->max_phys_ascent = it->max_phys_descent = 0;
16833 }
16834
16835
16836 /* Append one space to the glyph row of iterator IT if doing a
16837 window-based redisplay. The space has the same face as
16838 IT->face_id. Value is non-zero if a space was added.
16839
16840 This function is called to make sure that there is always one glyph
16841 at the end of a glyph row that the cursor can be set on under
16842 window-systems. (If there weren't such a glyph we would not know
16843 how wide and tall a box cursor should be displayed).
16844
16845 At the same time this space let's a nicely handle clearing to the
16846 end of the line if the row ends in italic text. */
16847
16848 static int
16849 append_space_for_newline (struct it *it, int default_face_p)
16850 {
16851 if (FRAME_WINDOW_P (it->f))
16852 {
16853 int n = it->glyph_row->used[TEXT_AREA];
16854
16855 if (it->glyph_row->glyphs[TEXT_AREA] + n
16856 < it->glyph_row->glyphs[1 + TEXT_AREA])
16857 {
16858 /* Save some values that must not be changed.
16859 Must save IT->c and IT->len because otherwise
16860 ITERATOR_AT_END_P wouldn't work anymore after
16861 append_space_for_newline has been called. */
16862 enum display_element_type saved_what = it->what;
16863 int saved_c = it->c, saved_len = it->len;
16864 int saved_char_to_display = it->char_to_display;
16865 int saved_x = it->current_x;
16866 int saved_face_id = it->face_id;
16867 struct text_pos saved_pos;
16868 Lisp_Object saved_object;
16869 struct face *face;
16870
16871 saved_object = it->object;
16872 saved_pos = it->position;
16873
16874 it->what = IT_CHARACTER;
16875 memset (&it->position, 0, sizeof it->position);
16876 it->object = make_number (0);
16877 it->c = it->char_to_display = ' ';
16878 it->len = 1;
16879
16880 if (default_face_p)
16881 it->face_id = DEFAULT_FACE_ID;
16882 else if (it->face_before_selective_p)
16883 it->face_id = it->saved_face_id;
16884 face = FACE_FROM_ID (it->f, it->face_id);
16885 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16886
16887 PRODUCE_GLYPHS (it);
16888
16889 it->override_ascent = -1;
16890 it->constrain_row_ascent_descent_p = 0;
16891 it->current_x = saved_x;
16892 it->object = saved_object;
16893 it->position = saved_pos;
16894 it->what = saved_what;
16895 it->face_id = saved_face_id;
16896 it->len = saved_len;
16897 it->c = saved_c;
16898 it->char_to_display = saved_char_to_display;
16899 return 1;
16900 }
16901 }
16902
16903 return 0;
16904 }
16905
16906
16907 /* Extend the face of the last glyph in the text area of IT->glyph_row
16908 to the end of the display line. Called from display_line. If the
16909 glyph row is empty, add a space glyph to it so that we know the
16910 face to draw. Set the glyph row flag fill_line_p. If the glyph
16911 row is R2L, prepend a stretch glyph to cover the empty space to the
16912 left of the leftmost glyph. */
16913
16914 static void
16915 extend_face_to_end_of_line (struct it *it)
16916 {
16917 struct face *face;
16918 struct frame *f = it->f;
16919
16920 /* If line is already filled, do nothing. Non window-system frames
16921 get a grace of one more ``pixel'' because their characters are
16922 1-``pixel'' wide, so they hit the equality too early. This grace
16923 is needed only for R2L rows that are not continued, to produce
16924 one extra blank where we could display the cursor. */
16925 if (it->current_x >= it->last_visible_x
16926 + (!FRAME_WINDOW_P (f)
16927 && it->glyph_row->reversed_p
16928 && !it->glyph_row->continued_p))
16929 return;
16930
16931 /* Face extension extends the background and box of IT->face_id
16932 to the end of the line. If the background equals the background
16933 of the frame, we don't have to do anything. */
16934 if (it->face_before_selective_p)
16935 face = FACE_FROM_ID (f, it->saved_face_id);
16936 else
16937 face = FACE_FROM_ID (f, it->face_id);
16938
16939 if (FRAME_WINDOW_P (f)
16940 && it->glyph_row->displays_text_p
16941 && face->box == FACE_NO_BOX
16942 && face->background == FRAME_BACKGROUND_PIXEL (f)
16943 && !face->stipple
16944 && !it->glyph_row->reversed_p)
16945 return;
16946
16947 /* Set the glyph row flag indicating that the face of the last glyph
16948 in the text area has to be drawn to the end of the text area. */
16949 it->glyph_row->fill_line_p = 1;
16950
16951 /* If current character of IT is not ASCII, make sure we have the
16952 ASCII face. This will be automatically undone the next time
16953 get_next_display_element returns a multibyte character. Note
16954 that the character will always be single byte in unibyte
16955 text. */
16956 if (!ASCII_CHAR_P (it->c))
16957 {
16958 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16959 }
16960
16961 if (FRAME_WINDOW_P (f))
16962 {
16963 /* If the row is empty, add a space with the current face of IT,
16964 so that we know which face to draw. */
16965 if (it->glyph_row->used[TEXT_AREA] == 0)
16966 {
16967 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16968 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16969 it->glyph_row->used[TEXT_AREA] = 1;
16970 }
16971 #ifdef HAVE_WINDOW_SYSTEM
16972 if (it->glyph_row->reversed_p)
16973 {
16974 /* Prepend a stretch glyph to the row, such that the
16975 rightmost glyph will be drawn flushed all the way to the
16976 right margin of the window. The stretch glyph that will
16977 occupy the empty space, if any, to the left of the
16978 glyphs. */
16979 struct font *font = face->font ? face->font : FRAME_FONT (f);
16980 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16981 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16982 struct glyph *g;
16983 int row_width, stretch_ascent, stretch_width;
16984 struct text_pos saved_pos;
16985 int saved_face_id, saved_avoid_cursor;
16986
16987 for (row_width = 0, g = row_start; g < row_end; g++)
16988 row_width += g->pixel_width;
16989 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16990 if (stretch_width > 0)
16991 {
16992 stretch_ascent =
16993 (((it->ascent + it->descent)
16994 * FONT_BASE (font)) / FONT_HEIGHT (font));
16995 saved_pos = it->position;
16996 memset (&it->position, 0, sizeof it->position);
16997 saved_avoid_cursor = it->avoid_cursor_p;
16998 it->avoid_cursor_p = 1;
16999 saved_face_id = it->face_id;
17000 /* The last row's stretch glyph should get the default
17001 face, to avoid painting the rest of the window with
17002 the region face, if the region ends at ZV. */
17003 if (it->glyph_row->ends_at_zv_p)
17004 it->face_id = DEFAULT_FACE_ID;
17005 else
17006 it->face_id = face->id;
17007 append_stretch_glyph (it, make_number (0), stretch_width,
17008 it->ascent + it->descent, stretch_ascent);
17009 it->position = saved_pos;
17010 it->avoid_cursor_p = saved_avoid_cursor;
17011 it->face_id = saved_face_id;
17012 }
17013 }
17014 #endif /* HAVE_WINDOW_SYSTEM */
17015 }
17016 else
17017 {
17018 /* Save some values that must not be changed. */
17019 int saved_x = it->current_x;
17020 struct text_pos saved_pos;
17021 Lisp_Object saved_object;
17022 enum display_element_type saved_what = it->what;
17023 int saved_face_id = it->face_id;
17024
17025 saved_object = it->object;
17026 saved_pos = it->position;
17027
17028 it->what = IT_CHARACTER;
17029 memset (&it->position, 0, sizeof it->position);
17030 it->object = make_number (0);
17031 it->c = it->char_to_display = ' ';
17032 it->len = 1;
17033 /* The last row's blank glyphs should get the default face, to
17034 avoid painting the rest of the window with the region face,
17035 if the region ends at ZV. */
17036 if (it->glyph_row->ends_at_zv_p)
17037 it->face_id = DEFAULT_FACE_ID;
17038 else
17039 it->face_id = face->id;
17040
17041 PRODUCE_GLYPHS (it);
17042
17043 while (it->current_x <= it->last_visible_x)
17044 PRODUCE_GLYPHS (it);
17045
17046 /* Don't count these blanks really. It would let us insert a left
17047 truncation glyph below and make us set the cursor on them, maybe. */
17048 it->current_x = saved_x;
17049 it->object = saved_object;
17050 it->position = saved_pos;
17051 it->what = saved_what;
17052 it->face_id = saved_face_id;
17053 }
17054 }
17055
17056
17057 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17058 trailing whitespace. */
17059
17060 static int
17061 trailing_whitespace_p (EMACS_INT charpos)
17062 {
17063 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17064 int c = 0;
17065
17066 while (bytepos < ZV_BYTE
17067 && (c = FETCH_CHAR (bytepos),
17068 c == ' ' || c == '\t'))
17069 ++bytepos;
17070
17071 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17072 {
17073 if (bytepos != PT_BYTE)
17074 return 1;
17075 }
17076 return 0;
17077 }
17078
17079
17080 /* Highlight trailing whitespace, if any, in ROW. */
17081
17082 static void
17083 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17084 {
17085 int used = row->used[TEXT_AREA];
17086
17087 if (used)
17088 {
17089 struct glyph *start = row->glyphs[TEXT_AREA];
17090 struct glyph *glyph = start + used - 1;
17091
17092 if (row->reversed_p)
17093 {
17094 /* Right-to-left rows need to be processed in the opposite
17095 direction, so swap the edge pointers. */
17096 glyph = start;
17097 start = row->glyphs[TEXT_AREA] + used - 1;
17098 }
17099
17100 /* Skip over glyphs inserted to display the cursor at the
17101 end of a line, for extending the face of the last glyph
17102 to the end of the line on terminals, and for truncation
17103 and continuation glyphs. */
17104 if (!row->reversed_p)
17105 {
17106 while (glyph >= start
17107 && glyph->type == CHAR_GLYPH
17108 && INTEGERP (glyph->object))
17109 --glyph;
17110 }
17111 else
17112 {
17113 while (glyph <= start
17114 && glyph->type == CHAR_GLYPH
17115 && INTEGERP (glyph->object))
17116 ++glyph;
17117 }
17118
17119 /* If last glyph is a space or stretch, and it's trailing
17120 whitespace, set the face of all trailing whitespace glyphs in
17121 IT->glyph_row to `trailing-whitespace'. */
17122 if ((row->reversed_p ? glyph <= start : glyph >= start)
17123 && BUFFERP (glyph->object)
17124 && (glyph->type == STRETCH_GLYPH
17125 || (glyph->type == CHAR_GLYPH
17126 && glyph->u.ch == ' '))
17127 && trailing_whitespace_p (glyph->charpos))
17128 {
17129 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17130 if (face_id < 0)
17131 return;
17132
17133 if (!row->reversed_p)
17134 {
17135 while (glyph >= start
17136 && BUFFERP (glyph->object)
17137 && (glyph->type == STRETCH_GLYPH
17138 || (glyph->type == CHAR_GLYPH
17139 && glyph->u.ch == ' ')))
17140 (glyph--)->face_id = face_id;
17141 }
17142 else
17143 {
17144 while (glyph <= start
17145 && BUFFERP (glyph->object)
17146 && (glyph->type == STRETCH_GLYPH
17147 || (glyph->type == CHAR_GLYPH
17148 && glyph->u.ch == ' ')))
17149 (glyph++)->face_id = face_id;
17150 }
17151 }
17152 }
17153 }
17154
17155
17156 /* Value is non-zero if glyph row ROW should be
17157 used to hold the cursor. */
17158
17159 static int
17160 cursor_row_p (struct glyph_row *row)
17161 {
17162 int result = 1;
17163
17164 if (PT == CHARPOS (row->end.pos))
17165 {
17166 /* Suppose the row ends on a string.
17167 Unless the row is continued, that means it ends on a newline
17168 in the string. If it's anything other than a display string
17169 (e.g. a before-string from an overlay), we don't want the
17170 cursor there. (This heuristic seems to give the optimal
17171 behavior for the various types of multi-line strings.) */
17172 if (CHARPOS (row->end.string_pos) >= 0)
17173 {
17174 if (row->continued_p)
17175 result = 1;
17176 else
17177 {
17178 /* Check for `display' property. */
17179 struct glyph *beg = row->glyphs[TEXT_AREA];
17180 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17181 struct glyph *glyph;
17182
17183 result = 0;
17184 for (glyph = end; glyph >= beg; --glyph)
17185 if (STRINGP (glyph->object))
17186 {
17187 Lisp_Object prop
17188 = Fget_char_property (make_number (PT),
17189 Qdisplay, Qnil);
17190 result =
17191 (!NILP (prop)
17192 && display_prop_string_p (prop, glyph->object));
17193 break;
17194 }
17195 }
17196 }
17197 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17198 {
17199 /* If the row ends in middle of a real character,
17200 and the line is continued, we want the cursor here.
17201 That's because CHARPOS (ROW->end.pos) would equal
17202 PT if PT is before the character. */
17203 if (!row->ends_in_ellipsis_p)
17204 result = row->continued_p;
17205 else
17206 /* If the row ends in an ellipsis, then
17207 CHARPOS (ROW->end.pos) will equal point after the
17208 invisible text. We want that position to be displayed
17209 after the ellipsis. */
17210 result = 0;
17211 }
17212 /* If the row ends at ZV, display the cursor at the end of that
17213 row instead of at the start of the row below. */
17214 else if (row->ends_at_zv_p)
17215 result = 1;
17216 else
17217 result = 0;
17218 }
17219
17220 return result;
17221 }
17222
17223 \f
17224
17225 /* Push the display property PROP so that it will be rendered at the
17226 current position in IT. Return 1 if PROP was successfully pushed,
17227 0 otherwise. */
17228
17229 static int
17230 push_display_prop (struct it *it, Lisp_Object prop)
17231 {
17232 push_it (it, NULL);
17233
17234 if (STRINGP (prop))
17235 {
17236 if (SCHARS (prop) == 0)
17237 {
17238 pop_it (it);
17239 return 0;
17240 }
17241
17242 it->string = prop;
17243 it->multibyte_p = STRING_MULTIBYTE (it->string);
17244 it->current.overlay_string_index = -1;
17245 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17246 it->end_charpos = it->string_nchars = SCHARS (it->string);
17247 it->method = GET_FROM_STRING;
17248 it->stop_charpos = 0;
17249 }
17250 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17251 {
17252 it->method = GET_FROM_STRETCH;
17253 it->object = prop;
17254 }
17255 #ifdef HAVE_WINDOW_SYSTEM
17256 else if (IMAGEP (prop))
17257 {
17258 it->what = IT_IMAGE;
17259 it->image_id = lookup_image (it->f, prop);
17260 it->method = GET_FROM_IMAGE;
17261 }
17262 #endif /* HAVE_WINDOW_SYSTEM */
17263 else
17264 {
17265 pop_it (it); /* bogus display property, give up */
17266 return 0;
17267 }
17268
17269 return 1;
17270 }
17271
17272 /* Return the character-property PROP at the current position in IT. */
17273
17274 static Lisp_Object
17275 get_it_property (struct it *it, Lisp_Object prop)
17276 {
17277 Lisp_Object position;
17278
17279 if (STRINGP (it->object))
17280 position = make_number (IT_STRING_CHARPOS (*it));
17281 else if (BUFFERP (it->object))
17282 position = make_number (IT_CHARPOS (*it));
17283 else
17284 return Qnil;
17285
17286 return Fget_char_property (position, prop, it->object);
17287 }
17288
17289 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17290
17291 static void
17292 handle_line_prefix (struct it *it)
17293 {
17294 Lisp_Object prefix;
17295 if (it->continuation_lines_width > 0)
17296 {
17297 prefix = get_it_property (it, Qwrap_prefix);
17298 if (NILP (prefix))
17299 prefix = Vwrap_prefix;
17300 }
17301 else
17302 {
17303 prefix = get_it_property (it, Qline_prefix);
17304 if (NILP (prefix))
17305 prefix = Vline_prefix;
17306 }
17307 if (! NILP (prefix) && push_display_prop (it, prefix))
17308 {
17309 /* If the prefix is wider than the window, and we try to wrap
17310 it, it would acquire its own wrap prefix, and so on till the
17311 iterator stack overflows. So, don't wrap the prefix. */
17312 it->line_wrap = TRUNCATE;
17313 it->avoid_cursor_p = 1;
17314 }
17315 }
17316
17317 \f
17318
17319 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17320 only for R2L lines from display_line, when it decides that too many
17321 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17322 continued. */
17323 static void
17324 unproduce_glyphs (struct it *it, int n)
17325 {
17326 struct glyph *glyph, *end;
17327
17328 xassert (it->glyph_row);
17329 xassert (it->glyph_row->reversed_p);
17330 xassert (it->area == TEXT_AREA);
17331 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17332
17333 if (n > it->glyph_row->used[TEXT_AREA])
17334 n = it->glyph_row->used[TEXT_AREA];
17335 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17336 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17337 for ( ; glyph < end; glyph++)
17338 glyph[-n] = *glyph;
17339 }
17340
17341 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17342 and ROW->maxpos. */
17343 static void
17344 find_row_edges (struct it *it, struct glyph_row *row,
17345 EMACS_INT min_pos, EMACS_INT min_bpos,
17346 EMACS_INT max_pos, EMACS_INT max_bpos)
17347 {
17348 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17349 lines' rows is implemented for bidi-reordered rows. */
17350
17351 /* ROW->minpos is the value of min_pos, the minimal buffer position
17352 we have in ROW. */
17353 if (min_pos <= ZV)
17354 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17355 else
17356 /* We didn't find _any_ valid buffer positions in any of the
17357 glyphs, so we must trust the iterator's computed positions. */
17358 row->minpos = row->start.pos;
17359 if (max_pos <= 0)
17360 {
17361 max_pos = CHARPOS (it->current.pos);
17362 max_bpos = BYTEPOS (it->current.pos);
17363 }
17364
17365 /* Here are the various use-cases for ending the row, and the
17366 corresponding values for ROW->maxpos:
17367
17368 Line ends in a newline from buffer eol_pos + 1
17369 Line is continued from buffer max_pos + 1
17370 Line is truncated on right it->current.pos
17371 Line ends in a newline from string max_pos
17372 Line is continued from string max_pos
17373 Line is continued from display vector max_pos
17374 Line is entirely from a string min_pos == max_pos
17375 Line is entirely from a display vector min_pos == max_pos
17376 Line that ends at ZV ZV
17377
17378 If you discover other use-cases, please add them here as
17379 appropriate. */
17380 if (row->ends_at_zv_p)
17381 row->maxpos = it->current.pos;
17382 else if (row->used[TEXT_AREA])
17383 {
17384 if (row->ends_in_newline_from_string_p)
17385 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17386 else if (CHARPOS (it->eol_pos) > 0)
17387 SET_TEXT_POS (row->maxpos,
17388 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17389 else if (row->continued_p)
17390 {
17391 /* If max_pos is different from IT's current position, it
17392 means IT->method does not belong to the display element
17393 at max_pos. However, it also means that the display
17394 element at max_pos was displayed in its entirety on this
17395 line, which is equivalent to saying that the next line
17396 starts at the next buffer position. */
17397 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17398 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17399 else
17400 {
17401 INC_BOTH (max_pos, max_bpos);
17402 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17403 }
17404 }
17405 else if (row->truncated_on_right_p)
17406 /* display_line already called reseat_at_next_visible_line_start,
17407 which puts the iterator at the beginning of the next line, in
17408 the logical order. */
17409 row->maxpos = it->current.pos;
17410 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17411 /* A line that is entirely from a string/image/stretch... */
17412 row->maxpos = row->minpos;
17413 else
17414 abort ();
17415 }
17416 else
17417 row->maxpos = it->current.pos;
17418 }
17419
17420 /* Construct the glyph row IT->glyph_row in the desired matrix of
17421 IT->w from text at the current position of IT. See dispextern.h
17422 for an overview of struct it. Value is non-zero if
17423 IT->glyph_row displays text, as opposed to a line displaying ZV
17424 only. */
17425
17426 static int
17427 display_line (struct it *it)
17428 {
17429 struct glyph_row *row = it->glyph_row;
17430 Lisp_Object overlay_arrow_string;
17431 struct it wrap_it;
17432 int may_wrap = 0, wrap_x IF_LINT (= 0);
17433 int wrap_row_used = -1;
17434 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
17435 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
17436 int wrap_row_extra_line_spacing IF_LINT (= 0);
17437 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
17438 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
17439 int cvpos;
17440 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17441 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
17442
17443 /* We always start displaying at hpos zero even if hscrolled. */
17444 xassert (it->hpos == 0 && it->current_x == 0);
17445
17446 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17447 >= it->w->desired_matrix->nrows)
17448 {
17449 it->w->nrows_scale_factor++;
17450 fonts_changed_p = 1;
17451 return 0;
17452 }
17453
17454 /* Is IT->w showing the region? */
17455 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17456
17457 /* Clear the result glyph row and enable it. */
17458 prepare_desired_row (row);
17459
17460 row->y = it->current_y;
17461 row->start = it->start;
17462 row->continuation_lines_width = it->continuation_lines_width;
17463 row->displays_text_p = 1;
17464 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17465 it->starts_in_middle_of_char_p = 0;
17466
17467 /* Arrange the overlays nicely for our purposes. Usually, we call
17468 display_line on only one line at a time, in which case this
17469 can't really hurt too much, or we call it on lines which appear
17470 one after another in the buffer, in which case all calls to
17471 recenter_overlay_lists but the first will be pretty cheap. */
17472 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17473
17474 /* Move over display elements that are not visible because we are
17475 hscrolled. This may stop at an x-position < IT->first_visible_x
17476 if the first glyph is partially visible or if we hit a line end. */
17477 if (it->current_x < it->first_visible_x)
17478 {
17479 this_line_min_pos = row->start.pos;
17480 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17481 MOVE_TO_POS | MOVE_TO_X);
17482 /* Record the smallest positions seen while we moved over
17483 display elements that are not visible. This is needed by
17484 redisplay_internal for optimizing the case where the cursor
17485 stays inside the same line. The rest of this function only
17486 considers positions that are actually displayed, so
17487 RECORD_MAX_MIN_POS will not otherwise record positions that
17488 are hscrolled to the left of the left edge of the window. */
17489 min_pos = CHARPOS (this_line_min_pos);
17490 min_bpos = BYTEPOS (this_line_min_pos);
17491 }
17492 else
17493 {
17494 /* We only do this when not calling `move_it_in_display_line_to'
17495 above, because move_it_in_display_line_to calls
17496 handle_line_prefix itself. */
17497 handle_line_prefix (it);
17498 }
17499
17500 /* Get the initial row height. This is either the height of the
17501 text hscrolled, if there is any, or zero. */
17502 row->ascent = it->max_ascent;
17503 row->height = it->max_ascent + it->max_descent;
17504 row->phys_ascent = it->max_phys_ascent;
17505 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17506 row->extra_line_spacing = it->max_extra_line_spacing;
17507
17508 /* Utility macro to record max and min buffer positions seen until now. */
17509 #define RECORD_MAX_MIN_POS(IT) \
17510 do \
17511 { \
17512 if (IT_CHARPOS (*(IT)) < min_pos) \
17513 { \
17514 min_pos = IT_CHARPOS (*(IT)); \
17515 min_bpos = IT_BYTEPOS (*(IT)); \
17516 } \
17517 if (IT_CHARPOS (*(IT)) > max_pos) \
17518 { \
17519 max_pos = IT_CHARPOS (*(IT)); \
17520 max_bpos = IT_BYTEPOS (*(IT)); \
17521 } \
17522 } \
17523 while (0)
17524
17525 /* Loop generating characters. The loop is left with IT on the next
17526 character to display. */
17527 while (1)
17528 {
17529 int n_glyphs_before, hpos_before, x_before;
17530 int x, nglyphs;
17531 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17532
17533 /* Retrieve the next thing to display. Value is zero if end of
17534 buffer reached. */
17535 if (!get_next_display_element (it))
17536 {
17537 /* Maybe add a space at the end of this line that is used to
17538 display the cursor there under X. Set the charpos of the
17539 first glyph of blank lines not corresponding to any text
17540 to -1. */
17541 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17542 row->exact_window_width_line_p = 1;
17543 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17544 || row->used[TEXT_AREA] == 0)
17545 {
17546 row->glyphs[TEXT_AREA]->charpos = -1;
17547 row->displays_text_p = 0;
17548
17549 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
17550 && (!MINI_WINDOW_P (it->w)
17551 || (minibuf_level && EQ (it->window, minibuf_window))))
17552 row->indicate_empty_line_p = 1;
17553 }
17554
17555 it->continuation_lines_width = 0;
17556 row->ends_at_zv_p = 1;
17557 /* A row that displays right-to-left text must always have
17558 its last face extended all the way to the end of line,
17559 even if this row ends in ZV, because we still write to
17560 the screen left to right. */
17561 if (row->reversed_p)
17562 extend_face_to_end_of_line (it);
17563 break;
17564 }
17565
17566 /* Now, get the metrics of what we want to display. This also
17567 generates glyphs in `row' (which is IT->glyph_row). */
17568 n_glyphs_before = row->used[TEXT_AREA];
17569 x = it->current_x;
17570
17571 /* Remember the line height so far in case the next element doesn't
17572 fit on the line. */
17573 if (it->line_wrap != TRUNCATE)
17574 {
17575 ascent = it->max_ascent;
17576 descent = it->max_descent;
17577 phys_ascent = it->max_phys_ascent;
17578 phys_descent = it->max_phys_descent;
17579
17580 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17581 {
17582 if (IT_DISPLAYING_WHITESPACE (it))
17583 may_wrap = 1;
17584 else if (may_wrap)
17585 {
17586 wrap_it = *it;
17587 wrap_x = x;
17588 wrap_row_used = row->used[TEXT_AREA];
17589 wrap_row_ascent = row->ascent;
17590 wrap_row_height = row->height;
17591 wrap_row_phys_ascent = row->phys_ascent;
17592 wrap_row_phys_height = row->phys_height;
17593 wrap_row_extra_line_spacing = row->extra_line_spacing;
17594 wrap_row_min_pos = min_pos;
17595 wrap_row_min_bpos = min_bpos;
17596 wrap_row_max_pos = max_pos;
17597 wrap_row_max_bpos = max_bpos;
17598 may_wrap = 0;
17599 }
17600 }
17601 }
17602
17603 PRODUCE_GLYPHS (it);
17604
17605 /* If this display element was in marginal areas, continue with
17606 the next one. */
17607 if (it->area != TEXT_AREA)
17608 {
17609 row->ascent = max (row->ascent, it->max_ascent);
17610 row->height = max (row->height, it->max_ascent + it->max_descent);
17611 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17612 row->phys_height = max (row->phys_height,
17613 it->max_phys_ascent + it->max_phys_descent);
17614 row->extra_line_spacing = max (row->extra_line_spacing,
17615 it->max_extra_line_spacing);
17616 set_iterator_to_next (it, 1);
17617 continue;
17618 }
17619
17620 /* Does the display element fit on the line? If we truncate
17621 lines, we should draw past the right edge of the window. If
17622 we don't truncate, we want to stop so that we can display the
17623 continuation glyph before the right margin. If lines are
17624 continued, there are two possible strategies for characters
17625 resulting in more than 1 glyph (e.g. tabs): Display as many
17626 glyphs as possible in this line and leave the rest for the
17627 continuation line, or display the whole element in the next
17628 line. Original redisplay did the former, so we do it also. */
17629 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17630 hpos_before = it->hpos;
17631 x_before = x;
17632
17633 if (/* Not a newline. */
17634 nglyphs > 0
17635 /* Glyphs produced fit entirely in the line. */
17636 && it->current_x < it->last_visible_x)
17637 {
17638 it->hpos += nglyphs;
17639 row->ascent = max (row->ascent, it->max_ascent);
17640 row->height = max (row->height, it->max_ascent + it->max_descent);
17641 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17642 row->phys_height = max (row->phys_height,
17643 it->max_phys_ascent + it->max_phys_descent);
17644 row->extra_line_spacing = max (row->extra_line_spacing,
17645 it->max_extra_line_spacing);
17646 if (it->current_x - it->pixel_width < it->first_visible_x)
17647 row->x = x - it->first_visible_x;
17648 /* Record the maximum and minimum buffer positions seen so
17649 far in glyphs that will be displayed by this row. */
17650 if (it->bidi_p)
17651 RECORD_MAX_MIN_POS (it);
17652 }
17653 else
17654 {
17655 int i, new_x;
17656 struct glyph *glyph;
17657
17658 for (i = 0; i < nglyphs; ++i, x = new_x)
17659 {
17660 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17661 new_x = x + glyph->pixel_width;
17662
17663 if (/* Lines are continued. */
17664 it->line_wrap != TRUNCATE
17665 && (/* Glyph doesn't fit on the line. */
17666 new_x > it->last_visible_x
17667 /* Or it fits exactly on a window system frame. */
17668 || (new_x == it->last_visible_x
17669 && FRAME_WINDOW_P (it->f))))
17670 {
17671 /* End of a continued line. */
17672
17673 if (it->hpos == 0
17674 || (new_x == it->last_visible_x
17675 && FRAME_WINDOW_P (it->f)))
17676 {
17677 /* Current glyph is the only one on the line or
17678 fits exactly on the line. We must continue
17679 the line because we can't draw the cursor
17680 after the glyph. */
17681 row->continued_p = 1;
17682 it->current_x = new_x;
17683 it->continuation_lines_width += new_x;
17684 ++it->hpos;
17685 /* Record the maximum and minimum buffer
17686 positions seen so far in glyphs that will be
17687 displayed by this row. */
17688 if (it->bidi_p)
17689 RECORD_MAX_MIN_POS (it);
17690 if (i == nglyphs - 1)
17691 {
17692 /* If line-wrap is on, check if a previous
17693 wrap point was found. */
17694 if (wrap_row_used > 0
17695 /* Even if there is a previous wrap
17696 point, continue the line here as
17697 usual, if (i) the previous character
17698 was a space or tab AND (ii) the
17699 current character is not. */
17700 && (!may_wrap
17701 || IT_DISPLAYING_WHITESPACE (it)))
17702 goto back_to_wrap;
17703
17704 set_iterator_to_next (it, 1);
17705 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17706 {
17707 if (!get_next_display_element (it))
17708 {
17709 row->exact_window_width_line_p = 1;
17710 it->continuation_lines_width = 0;
17711 row->continued_p = 0;
17712 row->ends_at_zv_p = 1;
17713 }
17714 else if (ITERATOR_AT_END_OF_LINE_P (it))
17715 {
17716 row->continued_p = 0;
17717 row->exact_window_width_line_p = 1;
17718 }
17719 }
17720 }
17721 }
17722 else if (CHAR_GLYPH_PADDING_P (*glyph)
17723 && !FRAME_WINDOW_P (it->f))
17724 {
17725 /* A padding glyph that doesn't fit on this line.
17726 This means the whole character doesn't fit
17727 on the line. */
17728 if (row->reversed_p)
17729 unproduce_glyphs (it, row->used[TEXT_AREA]
17730 - n_glyphs_before);
17731 row->used[TEXT_AREA] = n_glyphs_before;
17732
17733 /* Fill the rest of the row with continuation
17734 glyphs like in 20.x. */
17735 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17736 < row->glyphs[1 + TEXT_AREA])
17737 produce_special_glyphs (it, IT_CONTINUATION);
17738
17739 row->continued_p = 1;
17740 it->current_x = x_before;
17741 it->continuation_lines_width += x_before;
17742
17743 /* Restore the height to what it was before the
17744 element not fitting on the line. */
17745 it->max_ascent = ascent;
17746 it->max_descent = descent;
17747 it->max_phys_ascent = phys_ascent;
17748 it->max_phys_descent = phys_descent;
17749 }
17750 else if (wrap_row_used > 0)
17751 {
17752 back_to_wrap:
17753 if (row->reversed_p)
17754 unproduce_glyphs (it,
17755 row->used[TEXT_AREA] - wrap_row_used);
17756 *it = wrap_it;
17757 it->continuation_lines_width += wrap_x;
17758 row->used[TEXT_AREA] = wrap_row_used;
17759 row->ascent = wrap_row_ascent;
17760 row->height = wrap_row_height;
17761 row->phys_ascent = wrap_row_phys_ascent;
17762 row->phys_height = wrap_row_phys_height;
17763 row->extra_line_spacing = wrap_row_extra_line_spacing;
17764 min_pos = wrap_row_min_pos;
17765 min_bpos = wrap_row_min_bpos;
17766 max_pos = wrap_row_max_pos;
17767 max_bpos = wrap_row_max_bpos;
17768 row->continued_p = 1;
17769 row->ends_at_zv_p = 0;
17770 row->exact_window_width_line_p = 0;
17771 it->continuation_lines_width += x;
17772
17773 /* Make sure that a non-default face is extended
17774 up to the right margin of the window. */
17775 extend_face_to_end_of_line (it);
17776 }
17777 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17778 {
17779 /* A TAB that extends past the right edge of the
17780 window. This produces a single glyph on
17781 window system frames. We leave the glyph in
17782 this row and let it fill the row, but don't
17783 consume the TAB. */
17784 it->continuation_lines_width += it->last_visible_x;
17785 row->ends_in_middle_of_char_p = 1;
17786 row->continued_p = 1;
17787 glyph->pixel_width = it->last_visible_x - x;
17788 it->starts_in_middle_of_char_p = 1;
17789 }
17790 else
17791 {
17792 /* Something other than a TAB that draws past
17793 the right edge of the window. Restore
17794 positions to values before the element. */
17795 if (row->reversed_p)
17796 unproduce_glyphs (it, row->used[TEXT_AREA]
17797 - (n_glyphs_before + i));
17798 row->used[TEXT_AREA] = n_glyphs_before + i;
17799
17800 /* Display continuation glyphs. */
17801 if (!FRAME_WINDOW_P (it->f))
17802 produce_special_glyphs (it, IT_CONTINUATION);
17803 row->continued_p = 1;
17804
17805 it->current_x = x_before;
17806 it->continuation_lines_width += x;
17807 extend_face_to_end_of_line (it);
17808
17809 if (nglyphs > 1 && i > 0)
17810 {
17811 row->ends_in_middle_of_char_p = 1;
17812 it->starts_in_middle_of_char_p = 1;
17813 }
17814
17815 /* Restore the height to what it was before the
17816 element not fitting on the line. */
17817 it->max_ascent = ascent;
17818 it->max_descent = descent;
17819 it->max_phys_ascent = phys_ascent;
17820 it->max_phys_descent = phys_descent;
17821 }
17822
17823 break;
17824 }
17825 else if (new_x > it->first_visible_x)
17826 {
17827 /* Increment number of glyphs actually displayed. */
17828 ++it->hpos;
17829
17830 /* Record the maximum and minimum buffer positions
17831 seen so far in glyphs that will be displayed by
17832 this row. */
17833 if (it->bidi_p)
17834 RECORD_MAX_MIN_POS (it);
17835
17836 if (x < it->first_visible_x)
17837 /* Glyph is partially visible, i.e. row starts at
17838 negative X position. */
17839 row->x = x - it->first_visible_x;
17840 }
17841 else
17842 {
17843 /* Glyph is completely off the left margin of the
17844 window. This should not happen because of the
17845 move_it_in_display_line at the start of this
17846 function, unless the text display area of the
17847 window is empty. */
17848 xassert (it->first_visible_x <= it->last_visible_x);
17849 }
17850 }
17851
17852 row->ascent = max (row->ascent, it->max_ascent);
17853 row->height = max (row->height, it->max_ascent + it->max_descent);
17854 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17855 row->phys_height = max (row->phys_height,
17856 it->max_phys_ascent + it->max_phys_descent);
17857 row->extra_line_spacing = max (row->extra_line_spacing,
17858 it->max_extra_line_spacing);
17859
17860 /* End of this display line if row is continued. */
17861 if (row->continued_p || row->ends_at_zv_p)
17862 break;
17863 }
17864
17865 at_end_of_line:
17866 /* Is this a line end? If yes, we're also done, after making
17867 sure that a non-default face is extended up to the right
17868 margin of the window. */
17869 if (ITERATOR_AT_END_OF_LINE_P (it))
17870 {
17871 int used_before = row->used[TEXT_AREA];
17872
17873 row->ends_in_newline_from_string_p = STRINGP (it->object);
17874
17875 /* Add a space at the end of the line that is used to
17876 display the cursor there. */
17877 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17878 append_space_for_newline (it, 0);
17879
17880 /* Extend the face to the end of the line. */
17881 extend_face_to_end_of_line (it);
17882
17883 /* Make sure we have the position. */
17884 if (used_before == 0)
17885 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17886
17887 /* Record the position of the newline, for use in
17888 find_row_edges. */
17889 it->eol_pos = it->current.pos;
17890
17891 /* Consume the line end. This skips over invisible lines. */
17892 set_iterator_to_next (it, 1);
17893 it->continuation_lines_width = 0;
17894 break;
17895 }
17896
17897 /* Proceed with next display element. Note that this skips
17898 over lines invisible because of selective display. */
17899 set_iterator_to_next (it, 1);
17900
17901 /* If we truncate lines, we are done when the last displayed
17902 glyphs reach past the right margin of the window. */
17903 if (it->line_wrap == TRUNCATE
17904 && (FRAME_WINDOW_P (it->f)
17905 ? (it->current_x >= it->last_visible_x)
17906 : (it->current_x > it->last_visible_x)))
17907 {
17908 /* Maybe add truncation glyphs. */
17909 if (!FRAME_WINDOW_P (it->f))
17910 {
17911 int i, n;
17912
17913 if (!row->reversed_p)
17914 {
17915 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17916 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17917 break;
17918 }
17919 else
17920 {
17921 for (i = 0; i < row->used[TEXT_AREA]; i++)
17922 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17923 break;
17924 /* Remove any padding glyphs at the front of ROW, to
17925 make room for the truncation glyphs we will be
17926 adding below. The loop below always inserts at
17927 least one truncation glyph, so also remove the
17928 last glyph added to ROW. */
17929 unproduce_glyphs (it, i + 1);
17930 /* Adjust i for the loop below. */
17931 i = row->used[TEXT_AREA] - (i + 1);
17932 }
17933
17934 for (n = row->used[TEXT_AREA]; i < n; ++i)
17935 {
17936 row->used[TEXT_AREA] = i;
17937 produce_special_glyphs (it, IT_TRUNCATION);
17938 }
17939 }
17940 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17941 {
17942 /* Don't truncate if we can overflow newline into fringe. */
17943 if (!get_next_display_element (it))
17944 {
17945 it->continuation_lines_width = 0;
17946 row->ends_at_zv_p = 1;
17947 row->exact_window_width_line_p = 1;
17948 break;
17949 }
17950 if (ITERATOR_AT_END_OF_LINE_P (it))
17951 {
17952 row->exact_window_width_line_p = 1;
17953 goto at_end_of_line;
17954 }
17955 }
17956
17957 row->truncated_on_right_p = 1;
17958 it->continuation_lines_width = 0;
17959 reseat_at_next_visible_line_start (it, 0);
17960 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17961 it->hpos = hpos_before;
17962 it->current_x = x_before;
17963 break;
17964 }
17965 }
17966
17967 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17968 at the left window margin. */
17969 if (it->first_visible_x
17970 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17971 {
17972 if (!FRAME_WINDOW_P (it->f))
17973 insert_left_trunc_glyphs (it);
17974 row->truncated_on_left_p = 1;
17975 }
17976
17977 /* Remember the position at which this line ends.
17978
17979 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17980 cannot be before the call to find_row_edges below, since that is
17981 where these positions are determined. */
17982 row->end = it->current;
17983 if (!it->bidi_p)
17984 {
17985 row->minpos = row->start.pos;
17986 row->maxpos = row->end.pos;
17987 }
17988 else
17989 {
17990 /* ROW->minpos and ROW->maxpos must be the smallest and
17991 `1 + the largest' buffer positions in ROW. But if ROW was
17992 bidi-reordered, these two positions can be anywhere in the
17993 row, so we must determine them now. */
17994 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17995 }
17996
17997 /* If the start of this line is the overlay arrow-position, then
17998 mark this glyph row as the one containing the overlay arrow.
17999 This is clearly a mess with variable size fonts. It would be
18000 better to let it be displayed like cursors under X. */
18001 if ((row->displays_text_p || !overlay_arrow_seen)
18002 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18003 !NILP (overlay_arrow_string)))
18004 {
18005 /* Overlay arrow in window redisplay is a fringe bitmap. */
18006 if (STRINGP (overlay_arrow_string))
18007 {
18008 struct glyph_row *arrow_row
18009 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18010 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18011 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18012 struct glyph *p = row->glyphs[TEXT_AREA];
18013 struct glyph *p2, *end;
18014
18015 /* Copy the arrow glyphs. */
18016 while (glyph < arrow_end)
18017 *p++ = *glyph++;
18018
18019 /* Throw away padding glyphs. */
18020 p2 = p;
18021 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18022 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18023 ++p2;
18024 if (p2 > p)
18025 {
18026 while (p2 < end)
18027 *p++ = *p2++;
18028 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18029 }
18030 }
18031 else
18032 {
18033 xassert (INTEGERP (overlay_arrow_string));
18034 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18035 }
18036 overlay_arrow_seen = 1;
18037 }
18038
18039 /* Compute pixel dimensions of this line. */
18040 compute_line_metrics (it);
18041
18042 /* Record whether this row ends inside an ellipsis. */
18043 row->ends_in_ellipsis_p
18044 = (it->method == GET_FROM_DISPLAY_VECTOR
18045 && it->ellipsis_p);
18046
18047 /* Save fringe bitmaps in this row. */
18048 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18049 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18050 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18051 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18052
18053 it->left_user_fringe_bitmap = 0;
18054 it->left_user_fringe_face_id = 0;
18055 it->right_user_fringe_bitmap = 0;
18056 it->right_user_fringe_face_id = 0;
18057
18058 /* Maybe set the cursor. */
18059 cvpos = it->w->cursor.vpos;
18060 if ((cvpos < 0
18061 /* In bidi-reordered rows, keep checking for proper cursor
18062 position even if one has been found already, because buffer
18063 positions in such rows change non-linearly with ROW->VPOS,
18064 when a line is continued. One exception: when we are at ZV,
18065 display cursor on the first suitable glyph row, since all
18066 the empty rows after that also have their position set to ZV. */
18067 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18068 lines' rows is implemented for bidi-reordered rows. */
18069 || (it->bidi_p
18070 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18071 && PT >= MATRIX_ROW_START_CHARPOS (row)
18072 && PT <= MATRIX_ROW_END_CHARPOS (row)
18073 && cursor_row_p (row))
18074 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18075
18076 /* Highlight trailing whitespace. */
18077 if (!NILP (Vshow_trailing_whitespace))
18078 highlight_trailing_whitespace (it->f, it->glyph_row);
18079
18080 /* Prepare for the next line. This line starts horizontally at (X
18081 HPOS) = (0 0). Vertical positions are incremented. As a
18082 convenience for the caller, IT->glyph_row is set to the next
18083 row to be used. */
18084 it->current_x = it->hpos = 0;
18085 it->current_y += row->height;
18086 SET_TEXT_POS (it->eol_pos, 0, 0);
18087 ++it->vpos;
18088 ++it->glyph_row;
18089 /* The next row should by default use the same value of the
18090 reversed_p flag as this one. set_iterator_to_next decides when
18091 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18092 the flag accordingly. */
18093 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18094 it->glyph_row->reversed_p = row->reversed_p;
18095 it->start = row->end;
18096 return row->displays_text_p;
18097
18098 #undef RECORD_MAX_MIN_POS
18099 }
18100
18101 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18102 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18103 doc: /* Return paragraph direction at point in BUFFER.
18104 Value is either `left-to-right' or `right-to-left'.
18105 If BUFFER is omitted or nil, it defaults to the current buffer.
18106
18107 Paragraph direction determines how the text in the paragraph is displayed.
18108 In left-to-right paragraphs, text begins at the left margin of the window
18109 and the reading direction is generally left to right. In right-to-left
18110 paragraphs, text begins at the right margin and is read from right to left.
18111
18112 See also `bidi-paragraph-direction'. */)
18113 (Lisp_Object buffer)
18114 {
18115 struct buffer *buf = current_buffer;
18116 struct buffer *old = buf;
18117
18118 if (! NILP (buffer))
18119 {
18120 CHECK_BUFFER (buffer);
18121 buf = XBUFFER (buffer);
18122 }
18123
18124 if (NILP (BVAR (buf, bidi_display_reordering)))
18125 return Qleft_to_right;
18126 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18127 return BVAR (buf, bidi_paragraph_direction);
18128 else
18129 {
18130 /* Determine the direction from buffer text. We could try to
18131 use current_matrix if it is up to date, but this seems fast
18132 enough as it is. */
18133 struct bidi_it itb;
18134 EMACS_INT pos = BUF_PT (buf);
18135 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18136 int c;
18137
18138 set_buffer_temp (buf);
18139 /* bidi_paragraph_init finds the base direction of the paragraph
18140 by searching forward from paragraph start. We need the base
18141 direction of the current or _previous_ paragraph, so we need
18142 to make sure we are within that paragraph. To that end, find
18143 the previous non-empty line. */
18144 if (pos >= ZV && pos > BEGV)
18145 {
18146 pos--;
18147 bytepos = CHAR_TO_BYTE (pos);
18148 }
18149 while ((c = FETCH_BYTE (bytepos)) == '\n'
18150 || c == ' ' || c == '\t' || c == '\f')
18151 {
18152 if (bytepos <= BEGV_BYTE)
18153 break;
18154 bytepos--;
18155 pos--;
18156 }
18157 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18158 bytepos--;
18159 itb.charpos = pos;
18160 itb.bytepos = bytepos;
18161 itb.nchars = -1;
18162 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18163 itb.first_elt = 1;
18164 itb.separator_limit = -1;
18165 itb.paragraph_dir = NEUTRAL_DIR;
18166
18167 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18168 set_buffer_temp (old);
18169 switch (itb.paragraph_dir)
18170 {
18171 case L2R:
18172 return Qleft_to_right;
18173 break;
18174 case R2L:
18175 return Qright_to_left;
18176 break;
18177 default:
18178 abort ();
18179 }
18180 }
18181 }
18182
18183
18184 \f
18185 /***********************************************************************
18186 Menu Bar
18187 ***********************************************************************/
18188
18189 /* Redisplay the menu bar in the frame for window W.
18190
18191 The menu bar of X frames that don't have X toolkit support is
18192 displayed in a special window W->frame->menu_bar_window.
18193
18194 The menu bar of terminal frames is treated specially as far as
18195 glyph matrices are concerned. Menu bar lines are not part of
18196 windows, so the update is done directly on the frame matrix rows
18197 for the menu bar. */
18198
18199 static void
18200 display_menu_bar (struct window *w)
18201 {
18202 struct frame *f = XFRAME (WINDOW_FRAME (w));
18203 struct it it;
18204 Lisp_Object items;
18205 int i;
18206
18207 /* Don't do all this for graphical frames. */
18208 #ifdef HAVE_NTGUI
18209 if (FRAME_W32_P (f))
18210 return;
18211 #endif
18212 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18213 if (FRAME_X_P (f))
18214 return;
18215 #endif
18216
18217 #ifdef HAVE_NS
18218 if (FRAME_NS_P (f))
18219 return;
18220 #endif /* HAVE_NS */
18221
18222 #ifdef USE_X_TOOLKIT
18223 xassert (!FRAME_WINDOW_P (f));
18224 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18225 it.first_visible_x = 0;
18226 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18227 #else /* not USE_X_TOOLKIT */
18228 if (FRAME_WINDOW_P (f))
18229 {
18230 /* Menu bar lines are displayed in the desired matrix of the
18231 dummy window menu_bar_window. */
18232 struct window *menu_w;
18233 xassert (WINDOWP (f->menu_bar_window));
18234 menu_w = XWINDOW (f->menu_bar_window);
18235 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18236 MENU_FACE_ID);
18237 it.first_visible_x = 0;
18238 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18239 }
18240 else
18241 {
18242 /* This is a TTY frame, i.e. character hpos/vpos are used as
18243 pixel x/y. */
18244 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18245 MENU_FACE_ID);
18246 it.first_visible_x = 0;
18247 it.last_visible_x = FRAME_COLS (f);
18248 }
18249 #endif /* not USE_X_TOOLKIT */
18250
18251 if (! mode_line_inverse_video)
18252 /* Force the menu-bar to be displayed in the default face. */
18253 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18254
18255 /* Clear all rows of the menu bar. */
18256 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18257 {
18258 struct glyph_row *row = it.glyph_row + i;
18259 clear_glyph_row (row);
18260 row->enabled_p = 1;
18261 row->full_width_p = 1;
18262 }
18263
18264 /* Display all items of the menu bar. */
18265 items = FRAME_MENU_BAR_ITEMS (it.f);
18266 for (i = 0; i < ASIZE (items); i += 4)
18267 {
18268 Lisp_Object string;
18269
18270 /* Stop at nil string. */
18271 string = AREF (items, i + 1);
18272 if (NILP (string))
18273 break;
18274
18275 /* Remember where item was displayed. */
18276 ASET (items, i + 3, make_number (it.hpos));
18277
18278 /* Display the item, pad with one space. */
18279 if (it.current_x < it.last_visible_x)
18280 display_string (NULL, string, Qnil, 0, 0, &it,
18281 SCHARS (string) + 1, 0, 0, -1);
18282 }
18283
18284 /* Fill out the line with spaces. */
18285 if (it.current_x < it.last_visible_x)
18286 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18287
18288 /* Compute the total height of the lines. */
18289 compute_line_metrics (&it);
18290 }
18291
18292
18293 \f
18294 /***********************************************************************
18295 Mode Line
18296 ***********************************************************************/
18297
18298 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18299 FORCE is non-zero, redisplay mode lines unconditionally.
18300 Otherwise, redisplay only mode lines that are garbaged. Value is
18301 the number of windows whose mode lines were redisplayed. */
18302
18303 static int
18304 redisplay_mode_lines (Lisp_Object window, int force)
18305 {
18306 int nwindows = 0;
18307
18308 while (!NILP (window))
18309 {
18310 struct window *w = XWINDOW (window);
18311
18312 if (WINDOWP (w->hchild))
18313 nwindows += redisplay_mode_lines (w->hchild, force);
18314 else if (WINDOWP (w->vchild))
18315 nwindows += redisplay_mode_lines (w->vchild, force);
18316 else if (force
18317 || FRAME_GARBAGED_P (XFRAME (w->frame))
18318 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18319 {
18320 struct text_pos lpoint;
18321 struct buffer *old = current_buffer;
18322
18323 /* Set the window's buffer for the mode line display. */
18324 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18325 set_buffer_internal_1 (XBUFFER (w->buffer));
18326
18327 /* Point refers normally to the selected window. For any
18328 other window, set up appropriate value. */
18329 if (!EQ (window, selected_window))
18330 {
18331 struct text_pos pt;
18332
18333 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18334 if (CHARPOS (pt) < BEGV)
18335 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18336 else if (CHARPOS (pt) > (ZV - 1))
18337 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18338 else
18339 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18340 }
18341
18342 /* Display mode lines. */
18343 clear_glyph_matrix (w->desired_matrix);
18344 if (display_mode_lines (w))
18345 {
18346 ++nwindows;
18347 w->must_be_updated_p = 1;
18348 }
18349
18350 /* Restore old settings. */
18351 set_buffer_internal_1 (old);
18352 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18353 }
18354
18355 window = w->next;
18356 }
18357
18358 return nwindows;
18359 }
18360
18361
18362 /* Display the mode and/or header line of window W. Value is the
18363 sum number of mode lines and header lines displayed. */
18364
18365 static int
18366 display_mode_lines (struct window *w)
18367 {
18368 Lisp_Object old_selected_window, old_selected_frame;
18369 int n = 0;
18370
18371 old_selected_frame = selected_frame;
18372 selected_frame = w->frame;
18373 old_selected_window = selected_window;
18374 XSETWINDOW (selected_window, w);
18375
18376 /* These will be set while the mode line specs are processed. */
18377 line_number_displayed = 0;
18378 w->column_number_displayed = Qnil;
18379
18380 if (WINDOW_WANTS_MODELINE_P (w))
18381 {
18382 struct window *sel_w = XWINDOW (old_selected_window);
18383
18384 /* Select mode line face based on the real selected window. */
18385 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18386 BVAR (current_buffer, mode_line_format));
18387 ++n;
18388 }
18389
18390 if (WINDOW_WANTS_HEADER_LINE_P (w))
18391 {
18392 display_mode_line (w, HEADER_LINE_FACE_ID,
18393 BVAR (current_buffer, header_line_format));
18394 ++n;
18395 }
18396
18397 selected_frame = old_selected_frame;
18398 selected_window = old_selected_window;
18399 return n;
18400 }
18401
18402
18403 /* Display mode or header line of window W. FACE_ID specifies which
18404 line to display; it is either MODE_LINE_FACE_ID or
18405 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18406 display. Value is the pixel height of the mode/header line
18407 displayed. */
18408
18409 static int
18410 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18411 {
18412 struct it it;
18413 struct face *face;
18414 int count = SPECPDL_INDEX ();
18415
18416 init_iterator (&it, w, -1, -1, NULL, face_id);
18417 /* Don't extend on a previously drawn mode-line.
18418 This may happen if called from pos_visible_p. */
18419 it.glyph_row->enabled_p = 0;
18420 prepare_desired_row (it.glyph_row);
18421
18422 it.glyph_row->mode_line_p = 1;
18423
18424 if (! mode_line_inverse_video)
18425 /* Force the mode-line to be displayed in the default face. */
18426 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18427
18428 record_unwind_protect (unwind_format_mode_line,
18429 format_mode_line_unwind_data (NULL, Qnil, 0));
18430
18431 mode_line_target = MODE_LINE_DISPLAY;
18432
18433 /* Temporarily make frame's keyboard the current kboard so that
18434 kboard-local variables in the mode_line_format will get the right
18435 values. */
18436 push_kboard (FRAME_KBOARD (it.f));
18437 record_unwind_save_match_data ();
18438 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18439 pop_kboard ();
18440
18441 unbind_to (count, Qnil);
18442
18443 /* Fill up with spaces. */
18444 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18445
18446 compute_line_metrics (&it);
18447 it.glyph_row->full_width_p = 1;
18448 it.glyph_row->continued_p = 0;
18449 it.glyph_row->truncated_on_left_p = 0;
18450 it.glyph_row->truncated_on_right_p = 0;
18451
18452 /* Make a 3D mode-line have a shadow at its right end. */
18453 face = FACE_FROM_ID (it.f, face_id);
18454 extend_face_to_end_of_line (&it);
18455 if (face->box != FACE_NO_BOX)
18456 {
18457 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18458 + it.glyph_row->used[TEXT_AREA] - 1);
18459 last->right_box_line_p = 1;
18460 }
18461
18462 return it.glyph_row->height;
18463 }
18464
18465 /* Move element ELT in LIST to the front of LIST.
18466 Return the updated list. */
18467
18468 static Lisp_Object
18469 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18470 {
18471 register Lisp_Object tail, prev;
18472 register Lisp_Object tem;
18473
18474 tail = list;
18475 prev = Qnil;
18476 while (CONSP (tail))
18477 {
18478 tem = XCAR (tail);
18479
18480 if (EQ (elt, tem))
18481 {
18482 /* Splice out the link TAIL. */
18483 if (NILP (prev))
18484 list = XCDR (tail);
18485 else
18486 Fsetcdr (prev, XCDR (tail));
18487
18488 /* Now make it the first. */
18489 Fsetcdr (tail, list);
18490 return tail;
18491 }
18492 else
18493 prev = tail;
18494 tail = XCDR (tail);
18495 QUIT;
18496 }
18497
18498 /* Not found--return unchanged LIST. */
18499 return list;
18500 }
18501
18502 /* Contribute ELT to the mode line for window IT->w. How it
18503 translates into text depends on its data type.
18504
18505 IT describes the display environment in which we display, as usual.
18506
18507 DEPTH is the depth in recursion. It is used to prevent
18508 infinite recursion here.
18509
18510 FIELD_WIDTH is the number of characters the display of ELT should
18511 occupy in the mode line, and PRECISION is the maximum number of
18512 characters to display from ELT's representation. See
18513 display_string for details.
18514
18515 Returns the hpos of the end of the text generated by ELT.
18516
18517 PROPS is a property list to add to any string we encounter.
18518
18519 If RISKY is nonzero, remove (disregard) any properties in any string
18520 we encounter, and ignore :eval and :propertize.
18521
18522 The global variable `mode_line_target' determines whether the
18523 output is passed to `store_mode_line_noprop',
18524 `store_mode_line_string', or `display_string'. */
18525
18526 static int
18527 display_mode_element (struct it *it, int depth, int field_width, int precision,
18528 Lisp_Object elt, Lisp_Object props, int risky)
18529 {
18530 int n = 0, field, prec;
18531 int literal = 0;
18532
18533 tail_recurse:
18534 if (depth > 100)
18535 elt = build_string ("*too-deep*");
18536
18537 depth++;
18538
18539 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18540 {
18541 case Lisp_String:
18542 {
18543 /* A string: output it and check for %-constructs within it. */
18544 unsigned char c;
18545 EMACS_INT offset = 0;
18546
18547 if (SCHARS (elt) > 0
18548 && (!NILP (props) || risky))
18549 {
18550 Lisp_Object oprops, aelt;
18551 oprops = Ftext_properties_at (make_number (0), elt);
18552
18553 /* If the starting string's properties are not what
18554 we want, translate the string. Also, if the string
18555 is risky, do that anyway. */
18556
18557 if (NILP (Fequal (props, oprops)) || risky)
18558 {
18559 /* If the starting string has properties,
18560 merge the specified ones onto the existing ones. */
18561 if (! NILP (oprops) && !risky)
18562 {
18563 Lisp_Object tem;
18564
18565 oprops = Fcopy_sequence (oprops);
18566 tem = props;
18567 while (CONSP (tem))
18568 {
18569 oprops = Fplist_put (oprops, XCAR (tem),
18570 XCAR (XCDR (tem)));
18571 tem = XCDR (XCDR (tem));
18572 }
18573 props = oprops;
18574 }
18575
18576 aelt = Fassoc (elt, mode_line_proptrans_alist);
18577 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18578 {
18579 /* AELT is what we want. Move it to the front
18580 without consing. */
18581 elt = XCAR (aelt);
18582 mode_line_proptrans_alist
18583 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18584 }
18585 else
18586 {
18587 Lisp_Object tem;
18588
18589 /* If AELT has the wrong props, it is useless.
18590 so get rid of it. */
18591 if (! NILP (aelt))
18592 mode_line_proptrans_alist
18593 = Fdelq (aelt, mode_line_proptrans_alist);
18594
18595 elt = Fcopy_sequence (elt);
18596 Fset_text_properties (make_number (0), Flength (elt),
18597 props, elt);
18598 /* Add this item to mode_line_proptrans_alist. */
18599 mode_line_proptrans_alist
18600 = Fcons (Fcons (elt, props),
18601 mode_line_proptrans_alist);
18602 /* Truncate mode_line_proptrans_alist
18603 to at most 50 elements. */
18604 tem = Fnthcdr (make_number (50),
18605 mode_line_proptrans_alist);
18606 if (! NILP (tem))
18607 XSETCDR (tem, Qnil);
18608 }
18609 }
18610 }
18611
18612 offset = 0;
18613
18614 if (literal)
18615 {
18616 prec = precision - n;
18617 switch (mode_line_target)
18618 {
18619 case MODE_LINE_NOPROP:
18620 case MODE_LINE_TITLE:
18621 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
18622 break;
18623 case MODE_LINE_STRING:
18624 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18625 break;
18626 case MODE_LINE_DISPLAY:
18627 n += display_string (NULL, elt, Qnil, 0, 0, it,
18628 0, prec, 0, STRING_MULTIBYTE (elt));
18629 break;
18630 }
18631
18632 break;
18633 }
18634
18635 /* Handle the non-literal case. */
18636
18637 while ((precision <= 0 || n < precision)
18638 && SREF (elt, offset) != 0
18639 && (mode_line_target != MODE_LINE_DISPLAY
18640 || it->current_x < it->last_visible_x))
18641 {
18642 EMACS_INT last_offset = offset;
18643
18644 /* Advance to end of string or next format specifier. */
18645 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18646 ;
18647
18648 if (offset - 1 != last_offset)
18649 {
18650 EMACS_INT nchars, nbytes;
18651
18652 /* Output to end of string or up to '%'. Field width
18653 is length of string. Don't output more than
18654 PRECISION allows us. */
18655 offset--;
18656
18657 prec = c_string_width (SDATA (elt) + last_offset,
18658 offset - last_offset, precision - n,
18659 &nchars, &nbytes);
18660
18661 switch (mode_line_target)
18662 {
18663 case MODE_LINE_NOPROP:
18664 case MODE_LINE_TITLE:
18665 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
18666 break;
18667 case MODE_LINE_STRING:
18668 {
18669 EMACS_INT bytepos = last_offset;
18670 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18671 EMACS_INT endpos = (precision <= 0
18672 ? string_byte_to_char (elt, offset)
18673 : charpos + nchars);
18674
18675 n += store_mode_line_string (NULL,
18676 Fsubstring (elt, make_number (charpos),
18677 make_number (endpos)),
18678 0, 0, 0, Qnil);
18679 }
18680 break;
18681 case MODE_LINE_DISPLAY:
18682 {
18683 EMACS_INT bytepos = last_offset;
18684 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18685
18686 if (precision <= 0)
18687 nchars = string_byte_to_char (elt, offset) - charpos;
18688 n += display_string (NULL, elt, Qnil, 0, charpos,
18689 it, 0, nchars, 0,
18690 STRING_MULTIBYTE (elt));
18691 }
18692 break;
18693 }
18694 }
18695 else /* c == '%' */
18696 {
18697 EMACS_INT percent_position = offset;
18698
18699 /* Get the specified minimum width. Zero means
18700 don't pad. */
18701 field = 0;
18702 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18703 field = field * 10 + c - '0';
18704
18705 /* Don't pad beyond the total padding allowed. */
18706 if (field_width - n > 0 && field > field_width - n)
18707 field = field_width - n;
18708
18709 /* Note that either PRECISION <= 0 or N < PRECISION. */
18710 prec = precision - n;
18711
18712 if (c == 'M')
18713 n += display_mode_element (it, depth, field, prec,
18714 Vglobal_mode_string, props,
18715 risky);
18716 else if (c != 0)
18717 {
18718 int multibyte;
18719 EMACS_INT bytepos, charpos;
18720 const char *spec;
18721 Lisp_Object string;
18722
18723 bytepos = percent_position;
18724 charpos = (STRING_MULTIBYTE (elt)
18725 ? string_byte_to_char (elt, bytepos)
18726 : bytepos);
18727 spec = decode_mode_spec (it->w, c, field, &string);
18728 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18729
18730 switch (mode_line_target)
18731 {
18732 case MODE_LINE_NOPROP:
18733 case MODE_LINE_TITLE:
18734 n += store_mode_line_noprop (spec, field, prec);
18735 break;
18736 case MODE_LINE_STRING:
18737 {
18738 Lisp_Object tem = build_string (spec);
18739 props = Ftext_properties_at (make_number (charpos), elt);
18740 /* Should only keep face property in props */
18741 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18742 }
18743 break;
18744 case MODE_LINE_DISPLAY:
18745 {
18746 int nglyphs_before, nwritten;
18747
18748 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18749 nwritten = display_string (spec, string, elt,
18750 charpos, 0, it,
18751 field, prec, 0,
18752 multibyte);
18753
18754 /* Assign to the glyphs written above the
18755 string where the `%x' came from, position
18756 of the `%'. */
18757 if (nwritten > 0)
18758 {
18759 struct glyph *glyph
18760 = (it->glyph_row->glyphs[TEXT_AREA]
18761 + nglyphs_before);
18762 int i;
18763
18764 for (i = 0; i < nwritten; ++i)
18765 {
18766 glyph[i].object = elt;
18767 glyph[i].charpos = charpos;
18768 }
18769
18770 n += nwritten;
18771 }
18772 }
18773 break;
18774 }
18775 }
18776 else /* c == 0 */
18777 break;
18778 }
18779 }
18780 }
18781 break;
18782
18783 case Lisp_Symbol:
18784 /* A symbol: process the value of the symbol recursively
18785 as if it appeared here directly. Avoid error if symbol void.
18786 Special case: if value of symbol is a string, output the string
18787 literally. */
18788 {
18789 register Lisp_Object tem;
18790
18791 /* If the variable is not marked as risky to set
18792 then its contents are risky to use. */
18793 if (NILP (Fget (elt, Qrisky_local_variable)))
18794 risky = 1;
18795
18796 tem = Fboundp (elt);
18797 if (!NILP (tem))
18798 {
18799 tem = Fsymbol_value (elt);
18800 /* If value is a string, output that string literally:
18801 don't check for % within it. */
18802 if (STRINGP (tem))
18803 literal = 1;
18804
18805 if (!EQ (tem, elt))
18806 {
18807 /* Give up right away for nil or t. */
18808 elt = tem;
18809 goto tail_recurse;
18810 }
18811 }
18812 }
18813 break;
18814
18815 case Lisp_Cons:
18816 {
18817 register Lisp_Object car, tem;
18818
18819 /* A cons cell: five distinct cases.
18820 If first element is :eval or :propertize, do something special.
18821 If first element is a string or a cons, process all the elements
18822 and effectively concatenate them.
18823 If first element is a negative number, truncate displaying cdr to
18824 at most that many characters. If positive, pad (with spaces)
18825 to at least that many characters.
18826 If first element is a symbol, process the cadr or caddr recursively
18827 according to whether the symbol's value is non-nil or nil. */
18828 car = XCAR (elt);
18829 if (EQ (car, QCeval))
18830 {
18831 /* An element of the form (:eval FORM) means evaluate FORM
18832 and use the result as mode line elements. */
18833
18834 if (risky)
18835 break;
18836
18837 if (CONSP (XCDR (elt)))
18838 {
18839 Lisp_Object spec;
18840 spec = safe_eval (XCAR (XCDR (elt)));
18841 n += display_mode_element (it, depth, field_width - n,
18842 precision - n, spec, props,
18843 risky);
18844 }
18845 }
18846 else if (EQ (car, QCpropertize))
18847 {
18848 /* An element of the form (:propertize ELT PROPS...)
18849 means display ELT but applying properties PROPS. */
18850
18851 if (risky)
18852 break;
18853
18854 if (CONSP (XCDR (elt)))
18855 n += display_mode_element (it, depth, field_width - n,
18856 precision - n, XCAR (XCDR (elt)),
18857 XCDR (XCDR (elt)), risky);
18858 }
18859 else if (SYMBOLP (car))
18860 {
18861 tem = Fboundp (car);
18862 elt = XCDR (elt);
18863 if (!CONSP (elt))
18864 goto invalid;
18865 /* elt is now the cdr, and we know it is a cons cell.
18866 Use its car if CAR has a non-nil value. */
18867 if (!NILP (tem))
18868 {
18869 tem = Fsymbol_value (car);
18870 if (!NILP (tem))
18871 {
18872 elt = XCAR (elt);
18873 goto tail_recurse;
18874 }
18875 }
18876 /* Symbol's value is nil (or symbol is unbound)
18877 Get the cddr of the original list
18878 and if possible find the caddr and use that. */
18879 elt = XCDR (elt);
18880 if (NILP (elt))
18881 break;
18882 else if (!CONSP (elt))
18883 goto invalid;
18884 elt = XCAR (elt);
18885 goto tail_recurse;
18886 }
18887 else if (INTEGERP (car))
18888 {
18889 register int lim = XINT (car);
18890 elt = XCDR (elt);
18891 if (lim < 0)
18892 {
18893 /* Negative int means reduce maximum width. */
18894 if (precision <= 0)
18895 precision = -lim;
18896 else
18897 precision = min (precision, -lim);
18898 }
18899 else if (lim > 0)
18900 {
18901 /* Padding specified. Don't let it be more than
18902 current maximum. */
18903 if (precision > 0)
18904 lim = min (precision, lim);
18905
18906 /* If that's more padding than already wanted, queue it.
18907 But don't reduce padding already specified even if
18908 that is beyond the current truncation point. */
18909 field_width = max (lim, field_width);
18910 }
18911 goto tail_recurse;
18912 }
18913 else if (STRINGP (car) || CONSP (car))
18914 {
18915 Lisp_Object halftail = elt;
18916 int len = 0;
18917
18918 while (CONSP (elt)
18919 && (precision <= 0 || n < precision))
18920 {
18921 n += display_mode_element (it, depth,
18922 /* Do padding only after the last
18923 element in the list. */
18924 (! CONSP (XCDR (elt))
18925 ? field_width - n
18926 : 0),
18927 precision - n, XCAR (elt),
18928 props, risky);
18929 elt = XCDR (elt);
18930 len++;
18931 if ((len & 1) == 0)
18932 halftail = XCDR (halftail);
18933 /* Check for cycle. */
18934 if (EQ (halftail, elt))
18935 break;
18936 }
18937 }
18938 }
18939 break;
18940
18941 default:
18942 invalid:
18943 elt = build_string ("*invalid*");
18944 goto tail_recurse;
18945 }
18946
18947 /* Pad to FIELD_WIDTH. */
18948 if (field_width > 0 && n < field_width)
18949 {
18950 switch (mode_line_target)
18951 {
18952 case MODE_LINE_NOPROP:
18953 case MODE_LINE_TITLE:
18954 n += store_mode_line_noprop ("", field_width - n, 0);
18955 break;
18956 case MODE_LINE_STRING:
18957 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18958 break;
18959 case MODE_LINE_DISPLAY:
18960 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18961 0, 0, 0);
18962 break;
18963 }
18964 }
18965
18966 return n;
18967 }
18968
18969 /* Store a mode-line string element in mode_line_string_list.
18970
18971 If STRING is non-null, display that C string. Otherwise, the Lisp
18972 string LISP_STRING is displayed.
18973
18974 FIELD_WIDTH is the minimum number of output glyphs to produce.
18975 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18976 with spaces. FIELD_WIDTH <= 0 means don't pad.
18977
18978 PRECISION is the maximum number of characters to output from
18979 STRING. PRECISION <= 0 means don't truncate the string.
18980
18981 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18982 properties to the string.
18983
18984 PROPS are the properties to add to the string.
18985 The mode_line_string_face face property is always added to the string.
18986 */
18987
18988 static int
18989 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18990 int field_width, int precision, Lisp_Object props)
18991 {
18992 EMACS_INT len;
18993 int n = 0;
18994
18995 if (string != NULL)
18996 {
18997 len = strlen (string);
18998 if (precision > 0 && len > precision)
18999 len = precision;
19000 lisp_string = make_string (string, len);
19001 if (NILP (props))
19002 props = mode_line_string_face_prop;
19003 else if (!NILP (mode_line_string_face))
19004 {
19005 Lisp_Object face = Fplist_get (props, Qface);
19006 props = Fcopy_sequence (props);
19007 if (NILP (face))
19008 face = mode_line_string_face;
19009 else
19010 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19011 props = Fplist_put (props, Qface, face);
19012 }
19013 Fadd_text_properties (make_number (0), make_number (len),
19014 props, lisp_string);
19015 }
19016 else
19017 {
19018 len = XFASTINT (Flength (lisp_string));
19019 if (precision > 0 && len > precision)
19020 {
19021 len = precision;
19022 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19023 precision = -1;
19024 }
19025 if (!NILP (mode_line_string_face))
19026 {
19027 Lisp_Object face;
19028 if (NILP (props))
19029 props = Ftext_properties_at (make_number (0), lisp_string);
19030 face = Fplist_get (props, Qface);
19031 if (NILP (face))
19032 face = mode_line_string_face;
19033 else
19034 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19035 props = Fcons (Qface, Fcons (face, Qnil));
19036 if (copy_string)
19037 lisp_string = Fcopy_sequence (lisp_string);
19038 }
19039 if (!NILP (props))
19040 Fadd_text_properties (make_number (0), make_number (len),
19041 props, lisp_string);
19042 }
19043
19044 if (len > 0)
19045 {
19046 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19047 n += len;
19048 }
19049
19050 if (field_width > len)
19051 {
19052 field_width -= len;
19053 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19054 if (!NILP (props))
19055 Fadd_text_properties (make_number (0), make_number (field_width),
19056 props, lisp_string);
19057 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19058 n += field_width;
19059 }
19060
19061 return n;
19062 }
19063
19064
19065 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19066 1, 4, 0,
19067 doc: /* Format a string out of a mode line format specification.
19068 First arg FORMAT specifies the mode line format (see `mode-line-format'
19069 for details) to use.
19070
19071 By default, the format is evaluated for the currently selected window.
19072
19073 Optional second arg FACE specifies the face property to put on all
19074 characters for which no face is specified. The value nil means the
19075 default face. The value t means whatever face the window's mode line
19076 currently uses (either `mode-line' or `mode-line-inactive',
19077 depending on whether the window is the selected window or not).
19078 An integer value means the value string has no text
19079 properties.
19080
19081 Optional third and fourth args WINDOW and BUFFER specify the window
19082 and buffer to use as the context for the formatting (defaults
19083 are the selected window and the WINDOW's buffer). */)
19084 (Lisp_Object format, Lisp_Object face,
19085 Lisp_Object window, Lisp_Object buffer)
19086 {
19087 struct it it;
19088 int len;
19089 struct window *w;
19090 struct buffer *old_buffer = NULL;
19091 int face_id;
19092 int no_props = INTEGERP (face);
19093 int count = SPECPDL_INDEX ();
19094 Lisp_Object str;
19095 int string_start = 0;
19096
19097 if (NILP (window))
19098 window = selected_window;
19099 CHECK_WINDOW (window);
19100 w = XWINDOW (window);
19101
19102 if (NILP (buffer))
19103 buffer = w->buffer;
19104 CHECK_BUFFER (buffer);
19105
19106 /* Make formatting the modeline a non-op when noninteractive, otherwise
19107 there will be problems later caused by a partially initialized frame. */
19108 if (NILP (format) || noninteractive)
19109 return empty_unibyte_string;
19110
19111 if (no_props)
19112 face = Qnil;
19113
19114 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19115 : EQ (face, Qt) ? (EQ (window, selected_window)
19116 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19117 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19118 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19119 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19120 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19121 : DEFAULT_FACE_ID;
19122
19123 if (XBUFFER (buffer) != current_buffer)
19124 old_buffer = current_buffer;
19125
19126 /* Save things including mode_line_proptrans_alist,
19127 and set that to nil so that we don't alter the outer value. */
19128 record_unwind_protect (unwind_format_mode_line,
19129 format_mode_line_unwind_data
19130 (old_buffer, selected_window, 1));
19131 mode_line_proptrans_alist = Qnil;
19132
19133 Fselect_window (window, Qt);
19134 if (old_buffer)
19135 set_buffer_internal_1 (XBUFFER (buffer));
19136
19137 init_iterator (&it, w, -1, -1, NULL, face_id);
19138
19139 if (no_props)
19140 {
19141 mode_line_target = MODE_LINE_NOPROP;
19142 mode_line_string_face_prop = Qnil;
19143 mode_line_string_list = Qnil;
19144 string_start = MODE_LINE_NOPROP_LEN (0);
19145 }
19146 else
19147 {
19148 mode_line_target = MODE_LINE_STRING;
19149 mode_line_string_list = Qnil;
19150 mode_line_string_face = face;
19151 mode_line_string_face_prop
19152 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19153 }
19154
19155 push_kboard (FRAME_KBOARD (it.f));
19156 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19157 pop_kboard ();
19158
19159 if (no_props)
19160 {
19161 len = MODE_LINE_NOPROP_LEN (string_start);
19162 str = make_string (mode_line_noprop_buf + string_start, len);
19163 }
19164 else
19165 {
19166 mode_line_string_list = Fnreverse (mode_line_string_list);
19167 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19168 empty_unibyte_string);
19169 }
19170
19171 unbind_to (count, Qnil);
19172 return str;
19173 }
19174
19175 /* Write a null-terminated, right justified decimal representation of
19176 the positive integer D to BUF using a minimal field width WIDTH. */
19177
19178 static void
19179 pint2str (register char *buf, register int width, register EMACS_INT d)
19180 {
19181 register char *p = buf;
19182
19183 if (d <= 0)
19184 *p++ = '0';
19185 else
19186 {
19187 while (d > 0)
19188 {
19189 *p++ = d % 10 + '0';
19190 d /= 10;
19191 }
19192 }
19193
19194 for (width -= (int) (p - buf); width > 0; --width)
19195 *p++ = ' ';
19196 *p-- = '\0';
19197 while (p > buf)
19198 {
19199 d = *buf;
19200 *buf++ = *p;
19201 *p-- = d;
19202 }
19203 }
19204
19205 /* Write a null-terminated, right justified decimal and "human
19206 readable" representation of the nonnegative integer D to BUF using
19207 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19208
19209 static const char power_letter[] =
19210 {
19211 0, /* no letter */
19212 'k', /* kilo */
19213 'M', /* mega */
19214 'G', /* giga */
19215 'T', /* tera */
19216 'P', /* peta */
19217 'E', /* exa */
19218 'Z', /* zetta */
19219 'Y' /* yotta */
19220 };
19221
19222 static void
19223 pint2hrstr (char *buf, int width, EMACS_INT d)
19224 {
19225 /* We aim to represent the nonnegative integer D as
19226 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19227 EMACS_INT quotient = d;
19228 int remainder = 0;
19229 /* -1 means: do not use TENTHS. */
19230 int tenths = -1;
19231 int exponent = 0;
19232
19233 /* Length of QUOTIENT.TENTHS as a string. */
19234 int length;
19235
19236 char * psuffix;
19237 char * p;
19238
19239 if (1000 <= quotient)
19240 {
19241 /* Scale to the appropriate EXPONENT. */
19242 do
19243 {
19244 remainder = quotient % 1000;
19245 quotient /= 1000;
19246 exponent++;
19247 }
19248 while (1000 <= quotient);
19249
19250 /* Round to nearest and decide whether to use TENTHS or not. */
19251 if (quotient <= 9)
19252 {
19253 tenths = remainder / 100;
19254 if (50 <= remainder % 100)
19255 {
19256 if (tenths < 9)
19257 tenths++;
19258 else
19259 {
19260 quotient++;
19261 if (quotient == 10)
19262 tenths = -1;
19263 else
19264 tenths = 0;
19265 }
19266 }
19267 }
19268 else
19269 if (500 <= remainder)
19270 {
19271 if (quotient < 999)
19272 quotient++;
19273 else
19274 {
19275 quotient = 1;
19276 exponent++;
19277 tenths = 0;
19278 }
19279 }
19280 }
19281
19282 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19283 if (tenths == -1 && quotient <= 99)
19284 if (quotient <= 9)
19285 length = 1;
19286 else
19287 length = 2;
19288 else
19289 length = 3;
19290 p = psuffix = buf + max (width, length);
19291
19292 /* Print EXPONENT. */
19293 *psuffix++ = power_letter[exponent];
19294 *psuffix = '\0';
19295
19296 /* Print TENTHS. */
19297 if (tenths >= 0)
19298 {
19299 *--p = '0' + tenths;
19300 *--p = '.';
19301 }
19302
19303 /* Print QUOTIENT. */
19304 do
19305 {
19306 int digit = quotient % 10;
19307 *--p = '0' + digit;
19308 }
19309 while ((quotient /= 10) != 0);
19310
19311 /* Print leading spaces. */
19312 while (buf < p)
19313 *--p = ' ';
19314 }
19315
19316 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19317 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19318 type of CODING_SYSTEM. Return updated pointer into BUF. */
19319
19320 static unsigned char invalid_eol_type[] = "(*invalid*)";
19321
19322 static char *
19323 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19324 {
19325 Lisp_Object val;
19326 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19327 const unsigned char *eol_str;
19328 int eol_str_len;
19329 /* The EOL conversion we are using. */
19330 Lisp_Object eoltype;
19331
19332 val = CODING_SYSTEM_SPEC (coding_system);
19333 eoltype = Qnil;
19334
19335 if (!VECTORP (val)) /* Not yet decided. */
19336 {
19337 if (multibyte)
19338 *buf++ = '-';
19339 if (eol_flag)
19340 eoltype = eol_mnemonic_undecided;
19341 /* Don't mention EOL conversion if it isn't decided. */
19342 }
19343 else
19344 {
19345 Lisp_Object attrs;
19346 Lisp_Object eolvalue;
19347
19348 attrs = AREF (val, 0);
19349 eolvalue = AREF (val, 2);
19350
19351 if (multibyte)
19352 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19353
19354 if (eol_flag)
19355 {
19356 /* The EOL conversion that is normal on this system. */
19357
19358 if (NILP (eolvalue)) /* Not yet decided. */
19359 eoltype = eol_mnemonic_undecided;
19360 else if (VECTORP (eolvalue)) /* Not yet decided. */
19361 eoltype = eol_mnemonic_undecided;
19362 else /* eolvalue is Qunix, Qdos, or Qmac. */
19363 eoltype = (EQ (eolvalue, Qunix)
19364 ? eol_mnemonic_unix
19365 : (EQ (eolvalue, Qdos) == 1
19366 ? eol_mnemonic_dos : eol_mnemonic_mac));
19367 }
19368 }
19369
19370 if (eol_flag)
19371 {
19372 /* Mention the EOL conversion if it is not the usual one. */
19373 if (STRINGP (eoltype))
19374 {
19375 eol_str = SDATA (eoltype);
19376 eol_str_len = SBYTES (eoltype);
19377 }
19378 else if (CHARACTERP (eoltype))
19379 {
19380 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19381 int c = XFASTINT (eoltype);
19382 eol_str_len = CHAR_STRING (c, tmp);
19383 eol_str = tmp;
19384 }
19385 else
19386 {
19387 eol_str = invalid_eol_type;
19388 eol_str_len = sizeof (invalid_eol_type) - 1;
19389 }
19390 memcpy (buf, eol_str, eol_str_len);
19391 buf += eol_str_len;
19392 }
19393
19394 return buf;
19395 }
19396
19397 /* Return a string for the output of a mode line %-spec for window W,
19398 generated by character C. FIELD_WIDTH > 0 means pad the string
19399 returned with spaces to that value. Return a Lisp string in
19400 *STRING if the resulting string is taken from that Lisp string.
19401
19402 Note we operate on the current buffer for most purposes,
19403 the exception being w->base_line_pos. */
19404
19405 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19406
19407 static const char *
19408 decode_mode_spec (struct window *w, register int c, int field_width,
19409 Lisp_Object *string)
19410 {
19411 Lisp_Object obj;
19412 struct frame *f = XFRAME (WINDOW_FRAME (w));
19413 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19414 struct buffer *b = current_buffer;
19415
19416 obj = Qnil;
19417 *string = Qnil;
19418
19419 switch (c)
19420 {
19421 case '*':
19422 if (!NILP (BVAR (b, read_only)))
19423 return "%";
19424 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19425 return "*";
19426 return "-";
19427
19428 case '+':
19429 /* This differs from %* only for a modified read-only buffer. */
19430 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19431 return "*";
19432 if (!NILP (BVAR (b, read_only)))
19433 return "%";
19434 return "-";
19435
19436 case '&':
19437 /* This differs from %* in ignoring read-only-ness. */
19438 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19439 return "*";
19440 return "-";
19441
19442 case '%':
19443 return "%";
19444
19445 case '[':
19446 {
19447 int i;
19448 char *p;
19449
19450 if (command_loop_level > 5)
19451 return "[[[... ";
19452 p = decode_mode_spec_buf;
19453 for (i = 0; i < command_loop_level; i++)
19454 *p++ = '[';
19455 *p = 0;
19456 return decode_mode_spec_buf;
19457 }
19458
19459 case ']':
19460 {
19461 int i;
19462 char *p;
19463
19464 if (command_loop_level > 5)
19465 return " ...]]]";
19466 p = decode_mode_spec_buf;
19467 for (i = 0; i < command_loop_level; i++)
19468 *p++ = ']';
19469 *p = 0;
19470 return decode_mode_spec_buf;
19471 }
19472
19473 case '-':
19474 {
19475 register int i;
19476
19477 /* Let lots_of_dashes be a string of infinite length. */
19478 if (mode_line_target == MODE_LINE_NOPROP ||
19479 mode_line_target == MODE_LINE_STRING)
19480 return "--";
19481 if (field_width <= 0
19482 || field_width > sizeof (lots_of_dashes))
19483 {
19484 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19485 decode_mode_spec_buf[i] = '-';
19486 decode_mode_spec_buf[i] = '\0';
19487 return decode_mode_spec_buf;
19488 }
19489 else
19490 return lots_of_dashes;
19491 }
19492
19493 case 'b':
19494 obj = BVAR (b, name);
19495 break;
19496
19497 case 'c':
19498 /* %c and %l are ignored in `frame-title-format'.
19499 (In redisplay_internal, the frame title is drawn _before_ the
19500 windows are updated, so the stuff which depends on actual
19501 window contents (such as %l) may fail to render properly, or
19502 even crash emacs.) */
19503 if (mode_line_target == MODE_LINE_TITLE)
19504 return "";
19505 else
19506 {
19507 EMACS_INT col = current_column ();
19508 w->column_number_displayed = make_number (col);
19509 pint2str (decode_mode_spec_buf, field_width, col);
19510 return decode_mode_spec_buf;
19511 }
19512
19513 case 'e':
19514 #ifndef SYSTEM_MALLOC
19515 {
19516 if (NILP (Vmemory_full))
19517 return "";
19518 else
19519 return "!MEM FULL! ";
19520 }
19521 #else
19522 return "";
19523 #endif
19524
19525 case 'F':
19526 /* %F displays the frame name. */
19527 if (!NILP (f->title))
19528 return SSDATA (f->title);
19529 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19530 return SSDATA (f->name);
19531 return "Emacs";
19532
19533 case 'f':
19534 obj = BVAR (b, filename);
19535 break;
19536
19537 case 'i':
19538 {
19539 EMACS_INT size = ZV - BEGV;
19540 pint2str (decode_mode_spec_buf, field_width, size);
19541 return decode_mode_spec_buf;
19542 }
19543
19544 case 'I':
19545 {
19546 EMACS_INT size = ZV - BEGV;
19547 pint2hrstr (decode_mode_spec_buf, field_width, size);
19548 return decode_mode_spec_buf;
19549 }
19550
19551 case 'l':
19552 {
19553 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19554 EMACS_INT topline, nlines, height;
19555 EMACS_INT junk;
19556
19557 /* %c and %l are ignored in `frame-title-format'. */
19558 if (mode_line_target == MODE_LINE_TITLE)
19559 return "";
19560
19561 startpos = XMARKER (w->start)->charpos;
19562 startpos_byte = marker_byte_position (w->start);
19563 height = WINDOW_TOTAL_LINES (w);
19564
19565 /* If we decided that this buffer isn't suitable for line numbers,
19566 don't forget that too fast. */
19567 if (EQ (w->base_line_pos, w->buffer))
19568 goto no_value;
19569 /* But do forget it, if the window shows a different buffer now. */
19570 else if (BUFFERP (w->base_line_pos))
19571 w->base_line_pos = Qnil;
19572
19573 /* If the buffer is very big, don't waste time. */
19574 if (INTEGERP (Vline_number_display_limit)
19575 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19576 {
19577 w->base_line_pos = Qnil;
19578 w->base_line_number = Qnil;
19579 goto no_value;
19580 }
19581
19582 if (INTEGERP (w->base_line_number)
19583 && INTEGERP (w->base_line_pos)
19584 && XFASTINT (w->base_line_pos) <= startpos)
19585 {
19586 line = XFASTINT (w->base_line_number);
19587 linepos = XFASTINT (w->base_line_pos);
19588 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19589 }
19590 else
19591 {
19592 line = 1;
19593 linepos = BUF_BEGV (b);
19594 linepos_byte = BUF_BEGV_BYTE (b);
19595 }
19596
19597 /* Count lines from base line to window start position. */
19598 nlines = display_count_lines (linepos_byte,
19599 startpos_byte,
19600 startpos, &junk);
19601
19602 topline = nlines + line;
19603
19604 /* Determine a new base line, if the old one is too close
19605 or too far away, or if we did not have one.
19606 "Too close" means it's plausible a scroll-down would
19607 go back past it. */
19608 if (startpos == BUF_BEGV (b))
19609 {
19610 w->base_line_number = make_number (topline);
19611 w->base_line_pos = make_number (BUF_BEGV (b));
19612 }
19613 else if (nlines < height + 25 || nlines > height * 3 + 50
19614 || linepos == BUF_BEGV (b))
19615 {
19616 EMACS_INT limit = BUF_BEGV (b);
19617 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19618 EMACS_INT position;
19619 EMACS_INT distance =
19620 (height * 2 + 30) * line_number_display_limit_width;
19621
19622 if (startpos - distance > limit)
19623 {
19624 limit = startpos - distance;
19625 limit_byte = CHAR_TO_BYTE (limit);
19626 }
19627
19628 nlines = display_count_lines (startpos_byte,
19629 limit_byte,
19630 - (height * 2 + 30),
19631 &position);
19632 /* If we couldn't find the lines we wanted within
19633 line_number_display_limit_width chars per line,
19634 give up on line numbers for this window. */
19635 if (position == limit_byte && limit == startpos - distance)
19636 {
19637 w->base_line_pos = w->buffer;
19638 w->base_line_number = Qnil;
19639 goto no_value;
19640 }
19641
19642 w->base_line_number = make_number (topline - nlines);
19643 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19644 }
19645
19646 /* Now count lines from the start pos to point. */
19647 nlines = display_count_lines (startpos_byte,
19648 PT_BYTE, PT, &junk);
19649
19650 /* Record that we did display the line number. */
19651 line_number_displayed = 1;
19652
19653 /* Make the string to show. */
19654 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19655 return decode_mode_spec_buf;
19656 no_value:
19657 {
19658 char* p = decode_mode_spec_buf;
19659 int pad = field_width - 2;
19660 while (pad-- > 0)
19661 *p++ = ' ';
19662 *p++ = '?';
19663 *p++ = '?';
19664 *p = '\0';
19665 return decode_mode_spec_buf;
19666 }
19667 }
19668 break;
19669
19670 case 'm':
19671 obj = BVAR (b, mode_name);
19672 break;
19673
19674 case 'n':
19675 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19676 return " Narrow";
19677 break;
19678
19679 case 'p':
19680 {
19681 EMACS_INT pos = marker_position (w->start);
19682 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19683
19684 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19685 {
19686 if (pos <= BUF_BEGV (b))
19687 return "All";
19688 else
19689 return "Bottom";
19690 }
19691 else if (pos <= BUF_BEGV (b))
19692 return "Top";
19693 else
19694 {
19695 if (total > 1000000)
19696 /* Do it differently for a large value, to avoid overflow. */
19697 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19698 else
19699 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19700 /* We can't normally display a 3-digit number,
19701 so get us a 2-digit number that is close. */
19702 if (total == 100)
19703 total = 99;
19704 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
19705 return decode_mode_spec_buf;
19706 }
19707 }
19708
19709 /* Display percentage of size above the bottom of the screen. */
19710 case 'P':
19711 {
19712 EMACS_INT toppos = marker_position (w->start);
19713 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19714 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19715
19716 if (botpos >= BUF_ZV (b))
19717 {
19718 if (toppos <= BUF_BEGV (b))
19719 return "All";
19720 else
19721 return "Bottom";
19722 }
19723 else
19724 {
19725 if (total > 1000000)
19726 /* Do it differently for a large value, to avoid overflow. */
19727 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19728 else
19729 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19730 /* We can't normally display a 3-digit number,
19731 so get us a 2-digit number that is close. */
19732 if (total == 100)
19733 total = 99;
19734 if (toppos <= BUF_BEGV (b))
19735 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
19736 else
19737 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
19738 return decode_mode_spec_buf;
19739 }
19740 }
19741
19742 case 's':
19743 /* status of process */
19744 obj = Fget_buffer_process (Fcurrent_buffer ());
19745 if (NILP (obj))
19746 return "no process";
19747 #ifndef MSDOS
19748 obj = Fsymbol_name (Fprocess_status (obj));
19749 #endif
19750 break;
19751
19752 case '@':
19753 {
19754 int count = inhibit_garbage_collection ();
19755 Lisp_Object val = call1 (intern ("file-remote-p"),
19756 BVAR (current_buffer, directory));
19757 unbind_to (count, Qnil);
19758
19759 if (NILP (val))
19760 return "-";
19761 else
19762 return "@";
19763 }
19764
19765 case 't': /* indicate TEXT or BINARY */
19766 return "T";
19767
19768 case 'z':
19769 /* coding-system (not including end-of-line format) */
19770 case 'Z':
19771 /* coding-system (including end-of-line type) */
19772 {
19773 int eol_flag = (c == 'Z');
19774 char *p = decode_mode_spec_buf;
19775
19776 if (! FRAME_WINDOW_P (f))
19777 {
19778 /* No need to mention EOL here--the terminal never needs
19779 to do EOL conversion. */
19780 p = decode_mode_spec_coding (CODING_ID_NAME
19781 (FRAME_KEYBOARD_CODING (f)->id),
19782 p, 0);
19783 p = decode_mode_spec_coding (CODING_ID_NAME
19784 (FRAME_TERMINAL_CODING (f)->id),
19785 p, 0);
19786 }
19787 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
19788 p, eol_flag);
19789
19790 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19791 #ifdef subprocesses
19792 obj = Fget_buffer_process (Fcurrent_buffer ());
19793 if (PROCESSP (obj))
19794 {
19795 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19796 p, eol_flag);
19797 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19798 p, eol_flag);
19799 }
19800 #endif /* subprocesses */
19801 #endif /* 0 */
19802 *p = 0;
19803 return decode_mode_spec_buf;
19804 }
19805 }
19806
19807 if (STRINGP (obj))
19808 {
19809 *string = obj;
19810 return SSDATA (obj);
19811 }
19812 else
19813 return "";
19814 }
19815
19816
19817 /* Count up to COUNT lines starting from START_BYTE.
19818 But don't go beyond LIMIT_BYTE.
19819 Return the number of lines thus found (always nonnegative).
19820
19821 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19822
19823 static EMACS_INT
19824 display_count_lines (EMACS_INT start_byte,
19825 EMACS_INT limit_byte, EMACS_INT count,
19826 EMACS_INT *byte_pos_ptr)
19827 {
19828 register unsigned char *cursor;
19829 unsigned char *base;
19830
19831 register EMACS_INT ceiling;
19832 register unsigned char *ceiling_addr;
19833 EMACS_INT orig_count = count;
19834
19835 /* If we are not in selective display mode,
19836 check only for newlines. */
19837 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
19838 && !INTEGERP (BVAR (current_buffer, selective_display)));
19839
19840 if (count > 0)
19841 {
19842 while (start_byte < limit_byte)
19843 {
19844 ceiling = BUFFER_CEILING_OF (start_byte);
19845 ceiling = min (limit_byte - 1, ceiling);
19846 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19847 base = (cursor = BYTE_POS_ADDR (start_byte));
19848 while (1)
19849 {
19850 if (selective_display)
19851 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19852 ;
19853 else
19854 while (*cursor != '\n' && ++cursor != ceiling_addr)
19855 ;
19856
19857 if (cursor != ceiling_addr)
19858 {
19859 if (--count == 0)
19860 {
19861 start_byte += cursor - base + 1;
19862 *byte_pos_ptr = start_byte;
19863 return orig_count;
19864 }
19865 else
19866 if (++cursor == ceiling_addr)
19867 break;
19868 }
19869 else
19870 break;
19871 }
19872 start_byte += cursor - base;
19873 }
19874 }
19875 else
19876 {
19877 while (start_byte > limit_byte)
19878 {
19879 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19880 ceiling = max (limit_byte, ceiling);
19881 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19882 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19883 while (1)
19884 {
19885 if (selective_display)
19886 while (--cursor != ceiling_addr
19887 && *cursor != '\n' && *cursor != 015)
19888 ;
19889 else
19890 while (--cursor != ceiling_addr && *cursor != '\n')
19891 ;
19892
19893 if (cursor != ceiling_addr)
19894 {
19895 if (++count == 0)
19896 {
19897 start_byte += cursor - base + 1;
19898 *byte_pos_ptr = start_byte;
19899 /* When scanning backwards, we should
19900 not count the newline posterior to which we stop. */
19901 return - orig_count - 1;
19902 }
19903 }
19904 else
19905 break;
19906 }
19907 /* Here we add 1 to compensate for the last decrement
19908 of CURSOR, which took it past the valid range. */
19909 start_byte += cursor - base + 1;
19910 }
19911 }
19912
19913 *byte_pos_ptr = limit_byte;
19914
19915 if (count < 0)
19916 return - orig_count + count;
19917 return orig_count - count;
19918
19919 }
19920
19921
19922 \f
19923 /***********************************************************************
19924 Displaying strings
19925 ***********************************************************************/
19926
19927 /* Display a NUL-terminated string, starting with index START.
19928
19929 If STRING is non-null, display that C string. Otherwise, the Lisp
19930 string LISP_STRING is displayed. There's a case that STRING is
19931 non-null and LISP_STRING is not nil. It means STRING is a string
19932 data of LISP_STRING. In that case, we display LISP_STRING while
19933 ignoring its text properties.
19934
19935 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19936 FACE_STRING. Display STRING or LISP_STRING with the face at
19937 FACE_STRING_POS in FACE_STRING:
19938
19939 Display the string in the environment given by IT, but use the
19940 standard display table, temporarily.
19941
19942 FIELD_WIDTH is the minimum number of output glyphs to produce.
19943 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19944 with spaces. If STRING has more characters, more than FIELD_WIDTH
19945 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19946
19947 PRECISION is the maximum number of characters to output from
19948 STRING. PRECISION < 0 means don't truncate the string.
19949
19950 This is roughly equivalent to printf format specifiers:
19951
19952 FIELD_WIDTH PRECISION PRINTF
19953 ----------------------------------------
19954 -1 -1 %s
19955 -1 10 %.10s
19956 10 -1 %10s
19957 20 10 %20.10s
19958
19959 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19960 display them, and < 0 means obey the current buffer's value of
19961 enable_multibyte_characters.
19962
19963 Value is the number of columns displayed. */
19964
19965 static int
19966 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19967 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19968 int field_width, int precision, int max_x, int multibyte)
19969 {
19970 int hpos_at_start = it->hpos;
19971 int saved_face_id = it->face_id;
19972 struct glyph_row *row = it->glyph_row;
19973
19974 /* Initialize the iterator IT for iteration over STRING beginning
19975 with index START. */
19976 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19977 precision, field_width, multibyte);
19978 if (string && STRINGP (lisp_string))
19979 /* LISP_STRING is the one returned by decode_mode_spec. We should
19980 ignore its text properties. */
19981 it->stop_charpos = -1;
19982
19983 /* If displaying STRING, set up the face of the iterator
19984 from LISP_STRING, if that's given. */
19985 if (STRINGP (face_string))
19986 {
19987 EMACS_INT endptr;
19988 struct face *face;
19989
19990 it->face_id
19991 = face_at_string_position (it->w, face_string, face_string_pos,
19992 0, it->region_beg_charpos,
19993 it->region_end_charpos,
19994 &endptr, it->base_face_id, 0);
19995 face = FACE_FROM_ID (it->f, it->face_id);
19996 it->face_box_p = face->box != FACE_NO_BOX;
19997 }
19998
19999 /* Set max_x to the maximum allowed X position. Don't let it go
20000 beyond the right edge of the window. */
20001 if (max_x <= 0)
20002 max_x = it->last_visible_x;
20003 else
20004 max_x = min (max_x, it->last_visible_x);
20005
20006 /* Skip over display elements that are not visible. because IT->w is
20007 hscrolled. */
20008 if (it->current_x < it->first_visible_x)
20009 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20010 MOVE_TO_POS | MOVE_TO_X);
20011
20012 row->ascent = it->max_ascent;
20013 row->height = it->max_ascent + it->max_descent;
20014 row->phys_ascent = it->max_phys_ascent;
20015 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20016 row->extra_line_spacing = it->max_extra_line_spacing;
20017
20018 /* This condition is for the case that we are called with current_x
20019 past last_visible_x. */
20020 while (it->current_x < max_x)
20021 {
20022 int x_before, x, n_glyphs_before, i, nglyphs;
20023
20024 /* Get the next display element. */
20025 if (!get_next_display_element (it))
20026 break;
20027
20028 /* Produce glyphs. */
20029 x_before = it->current_x;
20030 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
20031 PRODUCE_GLYPHS (it);
20032
20033 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
20034 i = 0;
20035 x = x_before;
20036 while (i < nglyphs)
20037 {
20038 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20039
20040 if (it->line_wrap != TRUNCATE
20041 && x + glyph->pixel_width > max_x)
20042 {
20043 /* End of continued line or max_x reached. */
20044 if (CHAR_GLYPH_PADDING_P (*glyph))
20045 {
20046 /* A wide character is unbreakable. */
20047 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
20048 it->current_x = x_before;
20049 }
20050 else
20051 {
20052 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
20053 it->current_x = x;
20054 }
20055 break;
20056 }
20057 else if (x + glyph->pixel_width >= it->first_visible_x)
20058 {
20059 /* Glyph is at least partially visible. */
20060 ++it->hpos;
20061 if (x < it->first_visible_x)
20062 it->glyph_row->x = x - it->first_visible_x;
20063 }
20064 else
20065 {
20066 /* Glyph is off the left margin of the display area.
20067 Should not happen. */
20068 abort ();
20069 }
20070
20071 row->ascent = max (row->ascent, it->max_ascent);
20072 row->height = max (row->height, it->max_ascent + it->max_descent);
20073 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20074 row->phys_height = max (row->phys_height,
20075 it->max_phys_ascent + it->max_phys_descent);
20076 row->extra_line_spacing = max (row->extra_line_spacing,
20077 it->max_extra_line_spacing);
20078 x += glyph->pixel_width;
20079 ++i;
20080 }
20081
20082 /* Stop if max_x reached. */
20083 if (i < nglyphs)
20084 break;
20085
20086 /* Stop at line ends. */
20087 if (ITERATOR_AT_END_OF_LINE_P (it))
20088 {
20089 it->continuation_lines_width = 0;
20090 break;
20091 }
20092
20093 set_iterator_to_next (it, 1);
20094
20095 /* Stop if truncating at the right edge. */
20096 if (it->line_wrap == TRUNCATE
20097 && it->current_x >= it->last_visible_x)
20098 {
20099 /* Add truncation mark, but don't do it if the line is
20100 truncated at a padding space. */
20101 if (IT_CHARPOS (*it) < it->string_nchars)
20102 {
20103 if (!FRAME_WINDOW_P (it->f))
20104 {
20105 int ii, n;
20106
20107 if (it->current_x > it->last_visible_x)
20108 {
20109 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20110 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20111 break;
20112 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20113 {
20114 row->used[TEXT_AREA] = ii;
20115 produce_special_glyphs (it, IT_TRUNCATION);
20116 }
20117 }
20118 produce_special_glyphs (it, IT_TRUNCATION);
20119 }
20120 it->glyph_row->truncated_on_right_p = 1;
20121 }
20122 break;
20123 }
20124 }
20125
20126 /* Maybe insert a truncation at the left. */
20127 if (it->first_visible_x
20128 && IT_CHARPOS (*it) > 0)
20129 {
20130 if (!FRAME_WINDOW_P (it->f))
20131 insert_left_trunc_glyphs (it);
20132 it->glyph_row->truncated_on_left_p = 1;
20133 }
20134
20135 it->face_id = saved_face_id;
20136
20137 /* Value is number of columns displayed. */
20138 return it->hpos - hpos_at_start;
20139 }
20140
20141
20142 \f
20143 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20144 appears as an element of LIST or as the car of an element of LIST.
20145 If PROPVAL is a list, compare each element against LIST in that
20146 way, and return 1/2 if any element of PROPVAL is found in LIST.
20147 Otherwise return 0. This function cannot quit.
20148 The return value is 2 if the text is invisible but with an ellipsis
20149 and 1 if it's invisible and without an ellipsis. */
20150
20151 int
20152 invisible_p (register Lisp_Object propval, Lisp_Object list)
20153 {
20154 register Lisp_Object tail, proptail;
20155
20156 for (tail = list; CONSP (tail); tail = XCDR (tail))
20157 {
20158 register Lisp_Object tem;
20159 tem = XCAR (tail);
20160 if (EQ (propval, tem))
20161 return 1;
20162 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20163 return NILP (XCDR (tem)) ? 1 : 2;
20164 }
20165
20166 if (CONSP (propval))
20167 {
20168 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20169 {
20170 Lisp_Object propelt;
20171 propelt = XCAR (proptail);
20172 for (tail = list; CONSP (tail); tail = XCDR (tail))
20173 {
20174 register Lisp_Object tem;
20175 tem = XCAR (tail);
20176 if (EQ (propelt, tem))
20177 return 1;
20178 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20179 return NILP (XCDR (tem)) ? 1 : 2;
20180 }
20181 }
20182 }
20183
20184 return 0;
20185 }
20186
20187 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20188 doc: /* Non-nil if the property makes the text invisible.
20189 POS-OR-PROP can be a marker or number, in which case it is taken to be
20190 a position in the current buffer and the value of the `invisible' property
20191 is checked; or it can be some other value, which is then presumed to be the
20192 value of the `invisible' property of the text of interest.
20193 The non-nil value returned can be t for truly invisible text or something
20194 else if the text is replaced by an ellipsis. */)
20195 (Lisp_Object pos_or_prop)
20196 {
20197 Lisp_Object prop
20198 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20199 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20200 : pos_or_prop);
20201 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20202 return (invis == 0 ? Qnil
20203 : invis == 1 ? Qt
20204 : make_number (invis));
20205 }
20206
20207 /* Calculate a width or height in pixels from a specification using
20208 the following elements:
20209
20210 SPEC ::=
20211 NUM - a (fractional) multiple of the default font width/height
20212 (NUM) - specifies exactly NUM pixels
20213 UNIT - a fixed number of pixels, see below.
20214 ELEMENT - size of a display element in pixels, see below.
20215 (NUM . SPEC) - equals NUM * SPEC
20216 (+ SPEC SPEC ...) - add pixel values
20217 (- SPEC SPEC ...) - subtract pixel values
20218 (- SPEC) - negate pixel value
20219
20220 NUM ::=
20221 INT or FLOAT - a number constant
20222 SYMBOL - use symbol's (buffer local) variable binding.
20223
20224 UNIT ::=
20225 in - pixels per inch *)
20226 mm - pixels per 1/1000 meter *)
20227 cm - pixels per 1/100 meter *)
20228 width - width of current font in pixels.
20229 height - height of current font in pixels.
20230
20231 *) using the ratio(s) defined in display-pixels-per-inch.
20232
20233 ELEMENT ::=
20234
20235 left-fringe - left fringe width in pixels
20236 right-fringe - right fringe width in pixels
20237
20238 left-margin - left margin width in pixels
20239 right-margin - right margin width in pixels
20240
20241 scroll-bar - scroll-bar area width in pixels
20242
20243 Examples:
20244
20245 Pixels corresponding to 5 inches:
20246 (5 . in)
20247
20248 Total width of non-text areas on left side of window (if scroll-bar is on left):
20249 '(space :width (+ left-fringe left-margin scroll-bar))
20250
20251 Align to first text column (in header line):
20252 '(space :align-to 0)
20253
20254 Align to middle of text area minus half the width of variable `my-image'
20255 containing a loaded image:
20256 '(space :align-to (0.5 . (- text my-image)))
20257
20258 Width of left margin minus width of 1 character in the default font:
20259 '(space :width (- left-margin 1))
20260
20261 Width of left margin minus width of 2 characters in the current font:
20262 '(space :width (- left-margin (2 . width)))
20263
20264 Center 1 character over left-margin (in header line):
20265 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20266
20267 Different ways to express width of left fringe plus left margin minus one pixel:
20268 '(space :width (- (+ left-fringe left-margin) (1)))
20269 '(space :width (+ left-fringe left-margin (- (1))))
20270 '(space :width (+ left-fringe left-margin (-1)))
20271
20272 */
20273
20274 #define NUMVAL(X) \
20275 ((INTEGERP (X) || FLOATP (X)) \
20276 ? XFLOATINT (X) \
20277 : - 1)
20278
20279 int
20280 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20281 struct font *font, int width_p, int *align_to)
20282 {
20283 double pixels;
20284
20285 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20286 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20287
20288 if (NILP (prop))
20289 return OK_PIXELS (0);
20290
20291 xassert (FRAME_LIVE_P (it->f));
20292
20293 if (SYMBOLP (prop))
20294 {
20295 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20296 {
20297 char *unit = SSDATA (SYMBOL_NAME (prop));
20298
20299 if (unit[0] == 'i' && unit[1] == 'n')
20300 pixels = 1.0;
20301 else if (unit[0] == 'm' && unit[1] == 'm')
20302 pixels = 25.4;
20303 else if (unit[0] == 'c' && unit[1] == 'm')
20304 pixels = 2.54;
20305 else
20306 pixels = 0;
20307 if (pixels > 0)
20308 {
20309 double ppi;
20310 #ifdef HAVE_WINDOW_SYSTEM
20311 if (FRAME_WINDOW_P (it->f)
20312 && (ppi = (width_p
20313 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20314 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20315 ppi > 0))
20316 return OK_PIXELS (ppi / pixels);
20317 #endif
20318
20319 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20320 || (CONSP (Vdisplay_pixels_per_inch)
20321 && (ppi = (width_p
20322 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20323 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20324 ppi > 0)))
20325 return OK_PIXELS (ppi / pixels);
20326
20327 return 0;
20328 }
20329 }
20330
20331 #ifdef HAVE_WINDOW_SYSTEM
20332 if (EQ (prop, Qheight))
20333 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20334 if (EQ (prop, Qwidth))
20335 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20336 #else
20337 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20338 return OK_PIXELS (1);
20339 #endif
20340
20341 if (EQ (prop, Qtext))
20342 return OK_PIXELS (width_p
20343 ? window_box_width (it->w, TEXT_AREA)
20344 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20345
20346 if (align_to && *align_to < 0)
20347 {
20348 *res = 0;
20349 if (EQ (prop, Qleft))
20350 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20351 if (EQ (prop, Qright))
20352 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20353 if (EQ (prop, Qcenter))
20354 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20355 + window_box_width (it->w, TEXT_AREA) / 2);
20356 if (EQ (prop, Qleft_fringe))
20357 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20358 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20359 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20360 if (EQ (prop, Qright_fringe))
20361 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20362 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20363 : window_box_right_offset (it->w, TEXT_AREA));
20364 if (EQ (prop, Qleft_margin))
20365 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20366 if (EQ (prop, Qright_margin))
20367 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20368 if (EQ (prop, Qscroll_bar))
20369 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20370 ? 0
20371 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20372 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20373 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20374 : 0)));
20375 }
20376 else
20377 {
20378 if (EQ (prop, Qleft_fringe))
20379 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20380 if (EQ (prop, Qright_fringe))
20381 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20382 if (EQ (prop, Qleft_margin))
20383 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20384 if (EQ (prop, Qright_margin))
20385 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20386 if (EQ (prop, Qscroll_bar))
20387 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20388 }
20389
20390 prop = Fbuffer_local_value (prop, it->w->buffer);
20391 }
20392
20393 if (INTEGERP (prop) || FLOATP (prop))
20394 {
20395 int base_unit = (width_p
20396 ? FRAME_COLUMN_WIDTH (it->f)
20397 : FRAME_LINE_HEIGHT (it->f));
20398 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20399 }
20400
20401 if (CONSP (prop))
20402 {
20403 Lisp_Object car = XCAR (prop);
20404 Lisp_Object cdr = XCDR (prop);
20405
20406 if (SYMBOLP (car))
20407 {
20408 #ifdef HAVE_WINDOW_SYSTEM
20409 if (FRAME_WINDOW_P (it->f)
20410 && valid_image_p (prop))
20411 {
20412 int id = lookup_image (it->f, prop);
20413 struct image *img = IMAGE_FROM_ID (it->f, id);
20414
20415 return OK_PIXELS (width_p ? img->width : img->height);
20416 }
20417 #endif
20418 if (EQ (car, Qplus) || EQ (car, Qminus))
20419 {
20420 int first = 1;
20421 double px;
20422
20423 pixels = 0;
20424 while (CONSP (cdr))
20425 {
20426 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20427 font, width_p, align_to))
20428 return 0;
20429 if (first)
20430 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20431 else
20432 pixels += px;
20433 cdr = XCDR (cdr);
20434 }
20435 if (EQ (car, Qminus))
20436 pixels = -pixels;
20437 return OK_PIXELS (pixels);
20438 }
20439
20440 car = Fbuffer_local_value (car, it->w->buffer);
20441 }
20442
20443 if (INTEGERP (car) || FLOATP (car))
20444 {
20445 double fact;
20446 pixels = XFLOATINT (car);
20447 if (NILP (cdr))
20448 return OK_PIXELS (pixels);
20449 if (calc_pixel_width_or_height (&fact, it, cdr,
20450 font, width_p, align_to))
20451 return OK_PIXELS (pixels * fact);
20452 return 0;
20453 }
20454
20455 return 0;
20456 }
20457
20458 return 0;
20459 }
20460
20461 \f
20462 /***********************************************************************
20463 Glyph Display
20464 ***********************************************************************/
20465
20466 #ifdef HAVE_WINDOW_SYSTEM
20467
20468 #if GLYPH_DEBUG
20469
20470 void
20471 dump_glyph_string (struct glyph_string *s)
20472 {
20473 fprintf (stderr, "glyph string\n");
20474 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20475 s->x, s->y, s->width, s->height);
20476 fprintf (stderr, " ybase = %d\n", s->ybase);
20477 fprintf (stderr, " hl = %d\n", s->hl);
20478 fprintf (stderr, " left overhang = %d, right = %d\n",
20479 s->left_overhang, s->right_overhang);
20480 fprintf (stderr, " nchars = %d\n", s->nchars);
20481 fprintf (stderr, " extends to end of line = %d\n",
20482 s->extends_to_end_of_line_p);
20483 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20484 fprintf (stderr, " bg width = %d\n", s->background_width);
20485 }
20486
20487 #endif /* GLYPH_DEBUG */
20488
20489 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20490 of XChar2b structures for S; it can't be allocated in
20491 init_glyph_string because it must be allocated via `alloca'. W
20492 is the window on which S is drawn. ROW and AREA are the glyph row
20493 and area within the row from which S is constructed. START is the
20494 index of the first glyph structure covered by S. HL is a
20495 face-override for drawing S. */
20496
20497 #ifdef HAVE_NTGUI
20498 #define OPTIONAL_HDC(hdc) HDC hdc,
20499 #define DECLARE_HDC(hdc) HDC hdc;
20500 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20501 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20502 #endif
20503
20504 #ifndef OPTIONAL_HDC
20505 #define OPTIONAL_HDC(hdc)
20506 #define DECLARE_HDC(hdc)
20507 #define ALLOCATE_HDC(hdc, f)
20508 #define RELEASE_HDC(hdc, f)
20509 #endif
20510
20511 static void
20512 init_glyph_string (struct glyph_string *s,
20513 OPTIONAL_HDC (hdc)
20514 XChar2b *char2b, struct window *w, struct glyph_row *row,
20515 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20516 {
20517 memset (s, 0, sizeof *s);
20518 s->w = w;
20519 s->f = XFRAME (w->frame);
20520 #ifdef HAVE_NTGUI
20521 s->hdc = hdc;
20522 #endif
20523 s->display = FRAME_X_DISPLAY (s->f);
20524 s->window = FRAME_X_WINDOW (s->f);
20525 s->char2b = char2b;
20526 s->hl = hl;
20527 s->row = row;
20528 s->area = area;
20529 s->first_glyph = row->glyphs[area] + start;
20530 s->height = row->height;
20531 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20532 s->ybase = s->y + row->ascent;
20533 }
20534
20535
20536 /* Append the list of glyph strings with head H and tail T to the list
20537 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20538
20539 static inline void
20540 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20541 struct glyph_string *h, struct glyph_string *t)
20542 {
20543 if (h)
20544 {
20545 if (*head)
20546 (*tail)->next = h;
20547 else
20548 *head = h;
20549 h->prev = *tail;
20550 *tail = t;
20551 }
20552 }
20553
20554
20555 /* Prepend the list of glyph strings with head H and tail T to the
20556 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20557 result. */
20558
20559 static inline void
20560 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20561 struct glyph_string *h, struct glyph_string *t)
20562 {
20563 if (h)
20564 {
20565 if (*head)
20566 (*head)->prev = t;
20567 else
20568 *tail = t;
20569 t->next = *head;
20570 *head = h;
20571 }
20572 }
20573
20574
20575 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20576 Set *HEAD and *TAIL to the resulting list. */
20577
20578 static inline void
20579 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20580 struct glyph_string *s)
20581 {
20582 s->next = s->prev = NULL;
20583 append_glyph_string_lists (head, tail, s, s);
20584 }
20585
20586
20587 /* Get face and two-byte form of character C in face FACE_ID on frame F.
20588 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
20589 make sure that X resources for the face returned are allocated.
20590 Value is a pointer to a realized face that is ready for display if
20591 DISPLAY_P is non-zero. */
20592
20593 static inline struct face *
20594 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20595 XChar2b *char2b, int display_p)
20596 {
20597 struct face *face = FACE_FROM_ID (f, face_id);
20598
20599 if (face->font)
20600 {
20601 unsigned code = face->font->driver->encode_char (face->font, c);
20602
20603 if (code != FONT_INVALID_CODE)
20604 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20605 else
20606 STORE_XCHAR2B (char2b, 0, 0);
20607 }
20608
20609 /* Make sure X resources of the face are allocated. */
20610 #ifdef HAVE_X_WINDOWS
20611 if (display_p)
20612 #endif
20613 {
20614 xassert (face != NULL);
20615 PREPARE_FACE_FOR_DISPLAY (f, face);
20616 }
20617
20618 return face;
20619 }
20620
20621
20622 /* Get face and two-byte form of character glyph GLYPH on frame F.
20623 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20624 a pointer to a realized face that is ready for display. */
20625
20626 static inline struct face *
20627 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20628 XChar2b *char2b, int *two_byte_p)
20629 {
20630 struct face *face;
20631
20632 xassert (glyph->type == CHAR_GLYPH);
20633 face = FACE_FROM_ID (f, glyph->face_id);
20634
20635 if (two_byte_p)
20636 *two_byte_p = 0;
20637
20638 if (face->font)
20639 {
20640 unsigned code;
20641
20642 if (CHAR_BYTE8_P (glyph->u.ch))
20643 code = CHAR_TO_BYTE8 (glyph->u.ch);
20644 else
20645 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20646
20647 if (code != FONT_INVALID_CODE)
20648 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20649 else
20650 STORE_XCHAR2B (char2b, 0, 0);
20651 }
20652
20653 /* Make sure X resources of the face are allocated. */
20654 xassert (face != NULL);
20655 PREPARE_FACE_FOR_DISPLAY (f, face);
20656 return face;
20657 }
20658
20659
20660 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20661 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20662
20663 static inline int
20664 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20665 {
20666 unsigned code;
20667
20668 if (CHAR_BYTE8_P (c))
20669 code = CHAR_TO_BYTE8 (c);
20670 else
20671 code = font->driver->encode_char (font, c);
20672
20673 if (code == FONT_INVALID_CODE)
20674 return 0;
20675 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20676 return 1;
20677 }
20678
20679
20680 /* Fill glyph string S with composition components specified by S->cmp.
20681
20682 BASE_FACE is the base face of the composition.
20683 S->cmp_from is the index of the first component for S.
20684
20685 OVERLAPS non-zero means S should draw the foreground only, and use
20686 its physical height for clipping. See also draw_glyphs.
20687
20688 Value is the index of a component not in S. */
20689
20690 static int
20691 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20692 int overlaps)
20693 {
20694 int i;
20695 /* For all glyphs of this composition, starting at the offset
20696 S->cmp_from, until we reach the end of the definition or encounter a
20697 glyph that requires the different face, add it to S. */
20698 struct face *face;
20699
20700 xassert (s);
20701
20702 s->for_overlaps = overlaps;
20703 s->face = NULL;
20704 s->font = NULL;
20705 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20706 {
20707 int c = COMPOSITION_GLYPH (s->cmp, i);
20708
20709 if (c != '\t')
20710 {
20711 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20712 -1, Qnil);
20713
20714 face = get_char_face_and_encoding (s->f, c, face_id,
20715 s->char2b + i, 1);
20716 if (face)
20717 {
20718 if (! s->face)
20719 {
20720 s->face = face;
20721 s->font = s->face->font;
20722 }
20723 else if (s->face != face)
20724 break;
20725 }
20726 }
20727 ++s->nchars;
20728 }
20729 s->cmp_to = i;
20730
20731 /* All glyph strings for the same composition has the same width,
20732 i.e. the width set for the first component of the composition. */
20733 s->width = s->first_glyph->pixel_width;
20734
20735 /* If the specified font could not be loaded, use the frame's
20736 default font, but record the fact that we couldn't load it in
20737 the glyph string so that we can draw rectangles for the
20738 characters of the glyph string. */
20739 if (s->font == NULL)
20740 {
20741 s->font_not_found_p = 1;
20742 s->font = FRAME_FONT (s->f);
20743 }
20744
20745 /* Adjust base line for subscript/superscript text. */
20746 s->ybase += s->first_glyph->voffset;
20747
20748 /* This glyph string must always be drawn with 16-bit functions. */
20749 s->two_byte_p = 1;
20750
20751 return s->cmp_to;
20752 }
20753
20754 static int
20755 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20756 int start, int end, int overlaps)
20757 {
20758 struct glyph *glyph, *last;
20759 Lisp_Object lgstring;
20760 int i;
20761
20762 s->for_overlaps = overlaps;
20763 glyph = s->row->glyphs[s->area] + start;
20764 last = s->row->glyphs[s->area] + end;
20765 s->cmp_id = glyph->u.cmp.id;
20766 s->cmp_from = glyph->slice.cmp.from;
20767 s->cmp_to = glyph->slice.cmp.to + 1;
20768 s->face = FACE_FROM_ID (s->f, face_id);
20769 lgstring = composition_gstring_from_id (s->cmp_id);
20770 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20771 glyph++;
20772 while (glyph < last
20773 && glyph->u.cmp.automatic
20774 && glyph->u.cmp.id == s->cmp_id
20775 && s->cmp_to == glyph->slice.cmp.from)
20776 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20777
20778 for (i = s->cmp_from; i < s->cmp_to; i++)
20779 {
20780 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20781 unsigned code = LGLYPH_CODE (lglyph);
20782
20783 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20784 }
20785 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20786 return glyph - s->row->glyphs[s->area];
20787 }
20788
20789
20790 /* Fill glyph string S from a sequence glyphs for glyphless characters.
20791 See the comment of fill_glyph_string for arguments.
20792 Value is the index of the first glyph not in S. */
20793
20794
20795 static int
20796 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
20797 int start, int end, int overlaps)
20798 {
20799 struct glyph *glyph, *last;
20800 int voffset;
20801
20802 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
20803 s->for_overlaps = overlaps;
20804 glyph = s->row->glyphs[s->area] + start;
20805 last = s->row->glyphs[s->area] + end;
20806 voffset = glyph->voffset;
20807 s->face = FACE_FROM_ID (s->f, face_id);
20808 s->font = s->face->font;
20809 s->nchars = 1;
20810 s->width = glyph->pixel_width;
20811 glyph++;
20812 while (glyph < last
20813 && glyph->type == GLYPHLESS_GLYPH
20814 && glyph->voffset == voffset
20815 && glyph->face_id == face_id)
20816 {
20817 s->nchars++;
20818 s->width += glyph->pixel_width;
20819 glyph++;
20820 }
20821 s->ybase += voffset;
20822 return glyph - s->row->glyphs[s->area];
20823 }
20824
20825
20826 /* Fill glyph string S from a sequence of character glyphs.
20827
20828 FACE_ID is the face id of the string. START is the index of the
20829 first glyph to consider, END is the index of the last + 1.
20830 OVERLAPS non-zero means S should draw the foreground only, and use
20831 its physical height for clipping. See also draw_glyphs.
20832
20833 Value is the index of the first glyph not in S. */
20834
20835 static int
20836 fill_glyph_string (struct glyph_string *s, int face_id,
20837 int start, int end, int overlaps)
20838 {
20839 struct glyph *glyph, *last;
20840 int voffset;
20841 int glyph_not_available_p;
20842
20843 xassert (s->f == XFRAME (s->w->frame));
20844 xassert (s->nchars == 0);
20845 xassert (start >= 0 && end > start);
20846
20847 s->for_overlaps = overlaps;
20848 glyph = s->row->glyphs[s->area] + start;
20849 last = s->row->glyphs[s->area] + end;
20850 voffset = glyph->voffset;
20851 s->padding_p = glyph->padding_p;
20852 glyph_not_available_p = glyph->glyph_not_available_p;
20853
20854 while (glyph < last
20855 && glyph->type == CHAR_GLYPH
20856 && glyph->voffset == voffset
20857 /* Same face id implies same font, nowadays. */
20858 && glyph->face_id == face_id
20859 && glyph->glyph_not_available_p == glyph_not_available_p)
20860 {
20861 int two_byte_p;
20862
20863 s->face = get_glyph_face_and_encoding (s->f, glyph,
20864 s->char2b + s->nchars,
20865 &two_byte_p);
20866 s->two_byte_p = two_byte_p;
20867 ++s->nchars;
20868 xassert (s->nchars <= end - start);
20869 s->width += glyph->pixel_width;
20870 if (glyph++->padding_p != s->padding_p)
20871 break;
20872 }
20873
20874 s->font = s->face->font;
20875
20876 /* If the specified font could not be loaded, use the frame's font,
20877 but record the fact that we couldn't load it in
20878 S->font_not_found_p so that we can draw rectangles for the
20879 characters of the glyph string. */
20880 if (s->font == NULL || glyph_not_available_p)
20881 {
20882 s->font_not_found_p = 1;
20883 s->font = FRAME_FONT (s->f);
20884 }
20885
20886 /* Adjust base line for subscript/superscript text. */
20887 s->ybase += voffset;
20888
20889 xassert (s->face && s->face->gc);
20890 return glyph - s->row->glyphs[s->area];
20891 }
20892
20893
20894 /* Fill glyph string S from image glyph S->first_glyph. */
20895
20896 static void
20897 fill_image_glyph_string (struct glyph_string *s)
20898 {
20899 xassert (s->first_glyph->type == IMAGE_GLYPH);
20900 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20901 xassert (s->img);
20902 s->slice = s->first_glyph->slice.img;
20903 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20904 s->font = s->face->font;
20905 s->width = s->first_glyph->pixel_width;
20906
20907 /* Adjust base line for subscript/superscript text. */
20908 s->ybase += s->first_glyph->voffset;
20909 }
20910
20911
20912 /* Fill glyph string S from a sequence of stretch glyphs.
20913
20914 START is the index of the first glyph to consider,
20915 END is the index of the last + 1.
20916
20917 Value is the index of the first glyph not in S. */
20918
20919 static int
20920 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
20921 {
20922 struct glyph *glyph, *last;
20923 int voffset, face_id;
20924
20925 xassert (s->first_glyph->type == STRETCH_GLYPH);
20926
20927 glyph = s->row->glyphs[s->area] + start;
20928 last = s->row->glyphs[s->area] + end;
20929 face_id = glyph->face_id;
20930 s->face = FACE_FROM_ID (s->f, face_id);
20931 s->font = s->face->font;
20932 s->width = glyph->pixel_width;
20933 s->nchars = 1;
20934 voffset = glyph->voffset;
20935
20936 for (++glyph;
20937 (glyph < last
20938 && glyph->type == STRETCH_GLYPH
20939 && glyph->voffset == voffset
20940 && glyph->face_id == face_id);
20941 ++glyph)
20942 s->width += glyph->pixel_width;
20943
20944 /* Adjust base line for subscript/superscript text. */
20945 s->ybase += voffset;
20946
20947 /* The case that face->gc == 0 is handled when drawing the glyph
20948 string by calling PREPARE_FACE_FOR_DISPLAY. */
20949 xassert (s->face);
20950 return glyph - s->row->glyphs[s->area];
20951 }
20952
20953 static struct font_metrics *
20954 get_per_char_metric (struct font *font, XChar2b *char2b)
20955 {
20956 static struct font_metrics metrics;
20957 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20958
20959 if (! font || code == FONT_INVALID_CODE)
20960 return NULL;
20961 font->driver->text_extents (font, &code, 1, &metrics);
20962 return &metrics;
20963 }
20964
20965 /* EXPORT for RIF:
20966 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20967 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20968 assumed to be zero. */
20969
20970 void
20971 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20972 {
20973 *left = *right = 0;
20974
20975 if (glyph->type == CHAR_GLYPH)
20976 {
20977 struct face *face;
20978 XChar2b char2b;
20979 struct font_metrics *pcm;
20980
20981 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20982 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
20983 {
20984 if (pcm->rbearing > pcm->width)
20985 *right = pcm->rbearing - pcm->width;
20986 if (pcm->lbearing < 0)
20987 *left = -pcm->lbearing;
20988 }
20989 }
20990 else if (glyph->type == COMPOSITE_GLYPH)
20991 {
20992 if (! glyph->u.cmp.automatic)
20993 {
20994 struct composition *cmp = composition_table[glyph->u.cmp.id];
20995
20996 if (cmp->rbearing > cmp->pixel_width)
20997 *right = cmp->rbearing - cmp->pixel_width;
20998 if (cmp->lbearing < 0)
20999 *left = - cmp->lbearing;
21000 }
21001 else
21002 {
21003 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21004 struct font_metrics metrics;
21005
21006 composition_gstring_width (gstring, glyph->slice.cmp.from,
21007 glyph->slice.cmp.to + 1, &metrics);
21008 if (metrics.rbearing > metrics.width)
21009 *right = metrics.rbearing - metrics.width;
21010 if (metrics.lbearing < 0)
21011 *left = - metrics.lbearing;
21012 }
21013 }
21014 }
21015
21016
21017 /* Return the index of the first glyph preceding glyph string S that
21018 is overwritten by S because of S's left overhang. Value is -1
21019 if no glyphs are overwritten. */
21020
21021 static int
21022 left_overwritten (struct glyph_string *s)
21023 {
21024 int k;
21025
21026 if (s->left_overhang)
21027 {
21028 int x = 0, i;
21029 struct glyph *glyphs = s->row->glyphs[s->area];
21030 int first = s->first_glyph - glyphs;
21031
21032 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21033 x -= glyphs[i].pixel_width;
21034
21035 k = i + 1;
21036 }
21037 else
21038 k = -1;
21039
21040 return k;
21041 }
21042
21043
21044 /* Return the index of the first glyph preceding glyph string S that
21045 is overwriting S because of its right overhang. Value is -1 if no
21046 glyph in front of S overwrites S. */
21047
21048 static int
21049 left_overwriting (struct glyph_string *s)
21050 {
21051 int i, k, x;
21052 struct glyph *glyphs = s->row->glyphs[s->area];
21053 int first = s->first_glyph - glyphs;
21054
21055 k = -1;
21056 x = 0;
21057 for (i = first - 1; i >= 0; --i)
21058 {
21059 int left, right;
21060 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21061 if (x + right > 0)
21062 k = i;
21063 x -= glyphs[i].pixel_width;
21064 }
21065
21066 return k;
21067 }
21068
21069
21070 /* Return the index of the last glyph following glyph string S that is
21071 overwritten by S because of S's right overhang. Value is -1 if
21072 no such glyph is found. */
21073
21074 static int
21075 right_overwritten (struct glyph_string *s)
21076 {
21077 int k = -1;
21078
21079 if (s->right_overhang)
21080 {
21081 int x = 0, i;
21082 struct glyph *glyphs = s->row->glyphs[s->area];
21083 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21084 int end = s->row->used[s->area];
21085
21086 for (i = first; i < end && s->right_overhang > x; ++i)
21087 x += glyphs[i].pixel_width;
21088
21089 k = i;
21090 }
21091
21092 return k;
21093 }
21094
21095
21096 /* Return the index of the last glyph following glyph string S that
21097 overwrites S because of its left overhang. Value is negative
21098 if no such glyph is found. */
21099
21100 static int
21101 right_overwriting (struct glyph_string *s)
21102 {
21103 int i, k, x;
21104 int end = s->row->used[s->area];
21105 struct glyph *glyphs = s->row->glyphs[s->area];
21106 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21107
21108 k = -1;
21109 x = 0;
21110 for (i = first; i < end; ++i)
21111 {
21112 int left, right;
21113 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21114 if (x - left < 0)
21115 k = i;
21116 x += glyphs[i].pixel_width;
21117 }
21118
21119 return k;
21120 }
21121
21122
21123 /* Set background width of glyph string S. START is the index of the
21124 first glyph following S. LAST_X is the right-most x-position + 1
21125 in the drawing area. */
21126
21127 static inline void
21128 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21129 {
21130 /* If the face of this glyph string has to be drawn to the end of
21131 the drawing area, set S->extends_to_end_of_line_p. */
21132
21133 if (start == s->row->used[s->area]
21134 && s->area == TEXT_AREA
21135 && ((s->row->fill_line_p
21136 && (s->hl == DRAW_NORMAL_TEXT
21137 || s->hl == DRAW_IMAGE_RAISED
21138 || s->hl == DRAW_IMAGE_SUNKEN))
21139 || s->hl == DRAW_MOUSE_FACE))
21140 s->extends_to_end_of_line_p = 1;
21141
21142 /* If S extends its face to the end of the line, set its
21143 background_width to the distance to the right edge of the drawing
21144 area. */
21145 if (s->extends_to_end_of_line_p)
21146 s->background_width = last_x - s->x + 1;
21147 else
21148 s->background_width = s->width;
21149 }
21150
21151
21152 /* Compute overhangs and x-positions for glyph string S and its
21153 predecessors, or successors. X is the starting x-position for S.
21154 BACKWARD_P non-zero means process predecessors. */
21155
21156 static void
21157 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21158 {
21159 if (backward_p)
21160 {
21161 while (s)
21162 {
21163 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21164 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21165 x -= s->width;
21166 s->x = x;
21167 s = s->prev;
21168 }
21169 }
21170 else
21171 {
21172 while (s)
21173 {
21174 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21175 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21176 s->x = x;
21177 x += s->width;
21178 s = s->next;
21179 }
21180 }
21181 }
21182
21183
21184
21185 /* The following macros are only called from draw_glyphs below.
21186 They reference the following parameters of that function directly:
21187 `w', `row', `area', and `overlap_p'
21188 as well as the following local variables:
21189 `s', `f', and `hdc' (in W32) */
21190
21191 #ifdef HAVE_NTGUI
21192 /* On W32, silently add local `hdc' variable to argument list of
21193 init_glyph_string. */
21194 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21195 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21196 #else
21197 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21198 init_glyph_string (s, char2b, w, row, area, start, hl)
21199 #endif
21200
21201 /* Add a glyph string for a stretch glyph to the list of strings
21202 between HEAD and TAIL. START is the index of the stretch glyph in
21203 row area AREA of glyph row ROW. END is the index of the last glyph
21204 in that glyph row area. X is the current output position assigned
21205 to the new glyph string constructed. HL overrides that face of the
21206 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21207 is the right-most x-position of the drawing area. */
21208
21209 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21210 and below -- keep them on one line. */
21211 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21212 do \
21213 { \
21214 s = (struct glyph_string *) alloca (sizeof *s); \
21215 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21216 START = fill_stretch_glyph_string (s, START, END); \
21217 append_glyph_string (&HEAD, &TAIL, s); \
21218 s->x = (X); \
21219 } \
21220 while (0)
21221
21222
21223 /* Add a glyph string for an image glyph to the list of strings
21224 between HEAD and TAIL. START is the index of the image glyph in
21225 row area AREA of glyph row ROW. END is the index of the last glyph
21226 in that glyph row area. X is the current output position assigned
21227 to the new glyph string constructed. HL overrides that face of the
21228 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21229 is the right-most x-position of the drawing area. */
21230
21231 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21232 do \
21233 { \
21234 s = (struct glyph_string *) alloca (sizeof *s); \
21235 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21236 fill_image_glyph_string (s); \
21237 append_glyph_string (&HEAD, &TAIL, s); \
21238 ++START; \
21239 s->x = (X); \
21240 } \
21241 while (0)
21242
21243
21244 /* Add a glyph string for a sequence of character glyphs to the list
21245 of strings between HEAD and TAIL. START is the index of the first
21246 glyph in row area AREA of glyph row ROW that is part of the new
21247 glyph string. END is the index of the last glyph in that glyph row
21248 area. X is the current output position assigned to the new glyph
21249 string constructed. HL overrides that face of the glyph; e.g. it
21250 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21251 right-most x-position of the drawing area. */
21252
21253 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21254 do \
21255 { \
21256 int face_id; \
21257 XChar2b *char2b; \
21258 \
21259 face_id = (row)->glyphs[area][START].face_id; \
21260 \
21261 s = (struct glyph_string *) alloca (sizeof *s); \
21262 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21263 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21264 append_glyph_string (&HEAD, &TAIL, s); \
21265 s->x = (X); \
21266 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21267 } \
21268 while (0)
21269
21270
21271 /* Add a glyph string for a composite sequence to the list of strings
21272 between HEAD and TAIL. START is the index of the first glyph in
21273 row area AREA of glyph row ROW that is part of the new glyph
21274 string. END is the index of the last glyph in that glyph row area.
21275 X is the current output position assigned to the new glyph string
21276 constructed. HL overrides that face of the glyph; e.g. it is
21277 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21278 x-position of the drawing area. */
21279
21280 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21281 do { \
21282 int face_id = (row)->glyphs[area][START].face_id; \
21283 struct face *base_face = FACE_FROM_ID (f, face_id); \
21284 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21285 struct composition *cmp = composition_table[cmp_id]; \
21286 XChar2b *char2b; \
21287 struct glyph_string *first_s IF_LINT (= NULL); \
21288 int n; \
21289 \
21290 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21291 \
21292 /* Make glyph_strings for each glyph sequence that is drawable by \
21293 the same face, and append them to HEAD/TAIL. */ \
21294 for (n = 0; n < cmp->glyph_len;) \
21295 { \
21296 s = (struct glyph_string *) alloca (sizeof *s); \
21297 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21298 append_glyph_string (&(HEAD), &(TAIL), s); \
21299 s->cmp = cmp; \
21300 s->cmp_from = n; \
21301 s->x = (X); \
21302 if (n == 0) \
21303 first_s = s; \
21304 n = fill_composite_glyph_string (s, base_face, overlaps); \
21305 } \
21306 \
21307 ++START; \
21308 s = first_s; \
21309 } while (0)
21310
21311
21312 /* Add a glyph string for a glyph-string sequence to the list of strings
21313 between HEAD and TAIL. */
21314
21315 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21316 do { \
21317 int face_id; \
21318 XChar2b *char2b; \
21319 Lisp_Object gstring; \
21320 \
21321 face_id = (row)->glyphs[area][START].face_id; \
21322 gstring = (composition_gstring_from_id \
21323 ((row)->glyphs[area][START].u.cmp.id)); \
21324 s = (struct glyph_string *) alloca (sizeof *s); \
21325 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21326 * LGSTRING_GLYPH_LEN (gstring)); \
21327 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21328 append_glyph_string (&(HEAD), &(TAIL), s); \
21329 s->x = (X); \
21330 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21331 } while (0)
21332
21333
21334 /* Add a glyph string for a sequence of glyphless character's glyphs
21335 to the list of strings between HEAD and TAIL. The meanings of
21336 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21337
21338 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21339 do \
21340 { \
21341 int face_id; \
21342 \
21343 face_id = (row)->glyphs[area][START].face_id; \
21344 \
21345 s = (struct glyph_string *) alloca (sizeof *s); \
21346 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21347 append_glyph_string (&HEAD, &TAIL, s); \
21348 s->x = (X); \
21349 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21350 overlaps); \
21351 } \
21352 while (0)
21353
21354
21355 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21356 of AREA of glyph row ROW on window W between indices START and END.
21357 HL overrides the face for drawing glyph strings, e.g. it is
21358 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21359 x-positions of the drawing area.
21360
21361 This is an ugly monster macro construct because we must use alloca
21362 to allocate glyph strings (because draw_glyphs can be called
21363 asynchronously). */
21364
21365 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21366 do \
21367 { \
21368 HEAD = TAIL = NULL; \
21369 while (START < END) \
21370 { \
21371 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21372 switch (first_glyph->type) \
21373 { \
21374 case CHAR_GLYPH: \
21375 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21376 HL, X, LAST_X); \
21377 break; \
21378 \
21379 case COMPOSITE_GLYPH: \
21380 if (first_glyph->u.cmp.automatic) \
21381 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21382 HL, X, LAST_X); \
21383 else \
21384 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21385 HL, X, LAST_X); \
21386 break; \
21387 \
21388 case STRETCH_GLYPH: \
21389 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21390 HL, X, LAST_X); \
21391 break; \
21392 \
21393 case IMAGE_GLYPH: \
21394 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21395 HL, X, LAST_X); \
21396 break; \
21397 \
21398 case GLYPHLESS_GLYPH: \
21399 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21400 HL, X, LAST_X); \
21401 break; \
21402 \
21403 default: \
21404 abort (); \
21405 } \
21406 \
21407 if (s) \
21408 { \
21409 set_glyph_string_background_width (s, START, LAST_X); \
21410 (X) += s->width; \
21411 } \
21412 } \
21413 } while (0)
21414
21415
21416 /* Draw glyphs between START and END in AREA of ROW on window W,
21417 starting at x-position X. X is relative to AREA in W. HL is a
21418 face-override with the following meaning:
21419
21420 DRAW_NORMAL_TEXT draw normally
21421 DRAW_CURSOR draw in cursor face
21422 DRAW_MOUSE_FACE draw in mouse face.
21423 DRAW_INVERSE_VIDEO draw in mode line face
21424 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21425 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21426
21427 If OVERLAPS is non-zero, draw only the foreground of characters and
21428 clip to the physical height of ROW. Non-zero value also defines
21429 the overlapping part to be drawn:
21430
21431 OVERLAPS_PRED overlap with preceding rows
21432 OVERLAPS_SUCC overlap with succeeding rows
21433 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21434 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21435
21436 Value is the x-position reached, relative to AREA of W. */
21437
21438 static int
21439 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21440 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21441 enum draw_glyphs_face hl, int overlaps)
21442 {
21443 struct glyph_string *head, *tail;
21444 struct glyph_string *s;
21445 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21446 int i, j, x_reached, last_x, area_left = 0;
21447 struct frame *f = XFRAME (WINDOW_FRAME (w));
21448 DECLARE_HDC (hdc);
21449
21450 ALLOCATE_HDC (hdc, f);
21451
21452 /* Let's rather be paranoid than getting a SEGV. */
21453 end = min (end, row->used[area]);
21454 start = max (0, start);
21455 start = min (end, start);
21456
21457 /* Translate X to frame coordinates. Set last_x to the right
21458 end of the drawing area. */
21459 if (row->full_width_p)
21460 {
21461 /* X is relative to the left edge of W, without scroll bars
21462 or fringes. */
21463 area_left = WINDOW_LEFT_EDGE_X (w);
21464 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21465 }
21466 else
21467 {
21468 area_left = window_box_left (w, area);
21469 last_x = area_left + window_box_width (w, area);
21470 }
21471 x += area_left;
21472
21473 /* Build a doubly-linked list of glyph_string structures between
21474 head and tail from what we have to draw. Note that the macro
21475 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21476 the reason we use a separate variable `i'. */
21477 i = start;
21478 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21479 if (tail)
21480 x_reached = tail->x + tail->background_width;
21481 else
21482 x_reached = x;
21483
21484 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21485 the row, redraw some glyphs in front or following the glyph
21486 strings built above. */
21487 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21488 {
21489 struct glyph_string *h, *t;
21490 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21491 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
21492 int check_mouse_face = 0;
21493 int dummy_x = 0;
21494
21495 /* If mouse highlighting is on, we may need to draw adjacent
21496 glyphs using mouse-face highlighting. */
21497 if (area == TEXT_AREA && row->mouse_face_p)
21498 {
21499 struct glyph_row *mouse_beg_row, *mouse_end_row;
21500
21501 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21502 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21503
21504 if (row >= mouse_beg_row && row <= mouse_end_row)
21505 {
21506 check_mouse_face = 1;
21507 mouse_beg_col = (row == mouse_beg_row)
21508 ? hlinfo->mouse_face_beg_col : 0;
21509 mouse_end_col = (row == mouse_end_row)
21510 ? hlinfo->mouse_face_end_col
21511 : row->used[TEXT_AREA];
21512 }
21513 }
21514
21515 /* Compute overhangs for all glyph strings. */
21516 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21517 for (s = head; s; s = s->next)
21518 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21519
21520 /* Prepend glyph strings for glyphs in front of the first glyph
21521 string that are overwritten because of the first glyph
21522 string's left overhang. The background of all strings
21523 prepended must be drawn because the first glyph string
21524 draws over it. */
21525 i = left_overwritten (head);
21526 if (i >= 0)
21527 {
21528 enum draw_glyphs_face overlap_hl;
21529
21530 /* If this row contains mouse highlighting, attempt to draw
21531 the overlapped glyphs with the correct highlight. This
21532 code fails if the overlap encompasses more than one glyph
21533 and mouse-highlight spans only some of these glyphs.
21534 However, making it work perfectly involves a lot more
21535 code, and I don't know if the pathological case occurs in
21536 practice, so we'll stick to this for now. --- cyd */
21537 if (check_mouse_face
21538 && mouse_beg_col < start && mouse_end_col > i)
21539 overlap_hl = DRAW_MOUSE_FACE;
21540 else
21541 overlap_hl = DRAW_NORMAL_TEXT;
21542
21543 j = i;
21544 BUILD_GLYPH_STRINGS (j, start, h, t,
21545 overlap_hl, dummy_x, last_x);
21546 start = i;
21547 compute_overhangs_and_x (t, head->x, 1);
21548 prepend_glyph_string_lists (&head, &tail, h, t);
21549 clip_head = head;
21550 }
21551
21552 /* Prepend glyph strings for glyphs in front of the first glyph
21553 string that overwrite that glyph string because of their
21554 right overhang. For these strings, only the foreground must
21555 be drawn, because it draws over the glyph string at `head'.
21556 The background must not be drawn because this would overwrite
21557 right overhangs of preceding glyphs for which no glyph
21558 strings exist. */
21559 i = left_overwriting (head);
21560 if (i >= 0)
21561 {
21562 enum draw_glyphs_face overlap_hl;
21563
21564 if (check_mouse_face
21565 && mouse_beg_col < start && mouse_end_col > i)
21566 overlap_hl = DRAW_MOUSE_FACE;
21567 else
21568 overlap_hl = DRAW_NORMAL_TEXT;
21569
21570 clip_head = head;
21571 BUILD_GLYPH_STRINGS (i, start, h, t,
21572 overlap_hl, dummy_x, last_x);
21573 for (s = h; s; s = s->next)
21574 s->background_filled_p = 1;
21575 compute_overhangs_and_x (t, head->x, 1);
21576 prepend_glyph_string_lists (&head, &tail, h, t);
21577 }
21578
21579 /* Append glyphs strings for glyphs following the last glyph
21580 string tail that are overwritten by tail. The background of
21581 these strings has to be drawn because tail's foreground draws
21582 over it. */
21583 i = right_overwritten (tail);
21584 if (i >= 0)
21585 {
21586 enum draw_glyphs_face overlap_hl;
21587
21588 if (check_mouse_face
21589 && mouse_beg_col < i && mouse_end_col > end)
21590 overlap_hl = DRAW_MOUSE_FACE;
21591 else
21592 overlap_hl = DRAW_NORMAL_TEXT;
21593
21594 BUILD_GLYPH_STRINGS (end, i, h, t,
21595 overlap_hl, x, last_x);
21596 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21597 we don't have `end = i;' here. */
21598 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21599 append_glyph_string_lists (&head, &tail, h, t);
21600 clip_tail = tail;
21601 }
21602
21603 /* Append glyph strings for glyphs following the last glyph
21604 string tail that overwrite tail. The foreground of such
21605 glyphs has to be drawn because it writes into the background
21606 of tail. The background must not be drawn because it could
21607 paint over the foreground of following glyphs. */
21608 i = right_overwriting (tail);
21609 if (i >= 0)
21610 {
21611 enum draw_glyphs_face overlap_hl;
21612 if (check_mouse_face
21613 && mouse_beg_col < i && mouse_end_col > end)
21614 overlap_hl = DRAW_MOUSE_FACE;
21615 else
21616 overlap_hl = DRAW_NORMAL_TEXT;
21617
21618 clip_tail = tail;
21619 i++; /* We must include the Ith glyph. */
21620 BUILD_GLYPH_STRINGS (end, i, h, t,
21621 overlap_hl, x, last_x);
21622 for (s = h; s; s = s->next)
21623 s->background_filled_p = 1;
21624 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21625 append_glyph_string_lists (&head, &tail, h, t);
21626 }
21627 if (clip_head || clip_tail)
21628 for (s = head; s; s = s->next)
21629 {
21630 s->clip_head = clip_head;
21631 s->clip_tail = clip_tail;
21632 }
21633 }
21634
21635 /* Draw all strings. */
21636 for (s = head; s; s = s->next)
21637 FRAME_RIF (f)->draw_glyph_string (s);
21638
21639 #ifndef HAVE_NS
21640 /* When focus a sole frame and move horizontally, this sets on_p to 0
21641 causing a failure to erase prev cursor position. */
21642 if (area == TEXT_AREA
21643 && !row->full_width_p
21644 /* When drawing overlapping rows, only the glyph strings'
21645 foreground is drawn, which doesn't erase a cursor
21646 completely. */
21647 && !overlaps)
21648 {
21649 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21650 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21651 : (tail ? tail->x + tail->background_width : x));
21652 x0 -= area_left;
21653 x1 -= area_left;
21654
21655 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21656 row->y, MATRIX_ROW_BOTTOM_Y (row));
21657 }
21658 #endif
21659
21660 /* Value is the x-position up to which drawn, relative to AREA of W.
21661 This doesn't include parts drawn because of overhangs. */
21662 if (row->full_width_p)
21663 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21664 else
21665 x_reached -= area_left;
21666
21667 RELEASE_HDC (hdc, f);
21668
21669 return x_reached;
21670 }
21671
21672 /* Expand row matrix if too narrow. Don't expand if area
21673 is not present. */
21674
21675 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21676 { \
21677 if (!fonts_changed_p \
21678 && (it->glyph_row->glyphs[area] \
21679 < it->glyph_row->glyphs[area + 1])) \
21680 { \
21681 it->w->ncols_scale_factor++; \
21682 fonts_changed_p = 1; \
21683 } \
21684 }
21685
21686 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21687 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21688
21689 static inline void
21690 append_glyph (struct it *it)
21691 {
21692 struct glyph *glyph;
21693 enum glyph_row_area area = it->area;
21694
21695 xassert (it->glyph_row);
21696 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21697
21698 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21699 if (glyph < it->glyph_row->glyphs[area + 1])
21700 {
21701 /* If the glyph row is reversed, we need to prepend the glyph
21702 rather than append it. */
21703 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21704 {
21705 struct glyph *g;
21706
21707 /* Make room for the additional glyph. */
21708 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21709 g[1] = *g;
21710 glyph = it->glyph_row->glyphs[area];
21711 }
21712 glyph->charpos = CHARPOS (it->position);
21713 glyph->object = it->object;
21714 if (it->pixel_width > 0)
21715 {
21716 glyph->pixel_width = it->pixel_width;
21717 glyph->padding_p = 0;
21718 }
21719 else
21720 {
21721 /* Assure at least 1-pixel width. Otherwise, cursor can't
21722 be displayed correctly. */
21723 glyph->pixel_width = 1;
21724 glyph->padding_p = 1;
21725 }
21726 glyph->ascent = it->ascent;
21727 glyph->descent = it->descent;
21728 glyph->voffset = it->voffset;
21729 glyph->type = CHAR_GLYPH;
21730 glyph->avoid_cursor_p = it->avoid_cursor_p;
21731 glyph->multibyte_p = it->multibyte_p;
21732 glyph->left_box_line_p = it->start_of_box_run_p;
21733 glyph->right_box_line_p = it->end_of_box_run_p;
21734 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21735 || it->phys_descent > it->descent);
21736 glyph->glyph_not_available_p = it->glyph_not_available_p;
21737 glyph->face_id = it->face_id;
21738 glyph->u.ch = it->char_to_display;
21739 glyph->slice.img = null_glyph_slice;
21740 glyph->font_type = FONT_TYPE_UNKNOWN;
21741 if (it->bidi_p)
21742 {
21743 glyph->resolved_level = it->bidi_it.resolved_level;
21744 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21745 abort ();
21746 glyph->bidi_type = it->bidi_it.type;
21747 }
21748 else
21749 {
21750 glyph->resolved_level = 0;
21751 glyph->bidi_type = UNKNOWN_BT;
21752 }
21753 ++it->glyph_row->used[area];
21754 }
21755 else
21756 IT_EXPAND_MATRIX_WIDTH (it, area);
21757 }
21758
21759 /* Store one glyph for the composition IT->cmp_it.id in
21760 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21761 non-null. */
21762
21763 static inline void
21764 append_composite_glyph (struct it *it)
21765 {
21766 struct glyph *glyph;
21767 enum glyph_row_area area = it->area;
21768
21769 xassert (it->glyph_row);
21770
21771 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21772 if (glyph < it->glyph_row->glyphs[area + 1])
21773 {
21774 /* If the glyph row is reversed, we need to prepend the glyph
21775 rather than append it. */
21776 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21777 {
21778 struct glyph *g;
21779
21780 /* Make room for the new glyph. */
21781 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21782 g[1] = *g;
21783 glyph = it->glyph_row->glyphs[it->area];
21784 }
21785 glyph->charpos = it->cmp_it.charpos;
21786 glyph->object = it->object;
21787 glyph->pixel_width = it->pixel_width;
21788 glyph->ascent = it->ascent;
21789 glyph->descent = it->descent;
21790 glyph->voffset = it->voffset;
21791 glyph->type = COMPOSITE_GLYPH;
21792 if (it->cmp_it.ch < 0)
21793 {
21794 glyph->u.cmp.automatic = 0;
21795 glyph->u.cmp.id = it->cmp_it.id;
21796 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21797 }
21798 else
21799 {
21800 glyph->u.cmp.automatic = 1;
21801 glyph->u.cmp.id = it->cmp_it.id;
21802 glyph->slice.cmp.from = it->cmp_it.from;
21803 glyph->slice.cmp.to = it->cmp_it.to - 1;
21804 }
21805 glyph->avoid_cursor_p = it->avoid_cursor_p;
21806 glyph->multibyte_p = it->multibyte_p;
21807 glyph->left_box_line_p = it->start_of_box_run_p;
21808 glyph->right_box_line_p = it->end_of_box_run_p;
21809 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21810 || it->phys_descent > it->descent);
21811 glyph->padding_p = 0;
21812 glyph->glyph_not_available_p = 0;
21813 glyph->face_id = it->face_id;
21814 glyph->font_type = FONT_TYPE_UNKNOWN;
21815 if (it->bidi_p)
21816 {
21817 glyph->resolved_level = it->bidi_it.resolved_level;
21818 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21819 abort ();
21820 glyph->bidi_type = it->bidi_it.type;
21821 }
21822 ++it->glyph_row->used[area];
21823 }
21824 else
21825 IT_EXPAND_MATRIX_WIDTH (it, area);
21826 }
21827
21828
21829 /* Change IT->ascent and IT->height according to the setting of
21830 IT->voffset. */
21831
21832 static inline void
21833 take_vertical_position_into_account (struct it *it)
21834 {
21835 if (it->voffset)
21836 {
21837 if (it->voffset < 0)
21838 /* Increase the ascent so that we can display the text higher
21839 in the line. */
21840 it->ascent -= it->voffset;
21841 else
21842 /* Increase the descent so that we can display the text lower
21843 in the line. */
21844 it->descent += it->voffset;
21845 }
21846 }
21847
21848
21849 /* Produce glyphs/get display metrics for the image IT is loaded with.
21850 See the description of struct display_iterator in dispextern.h for
21851 an overview of struct display_iterator. */
21852
21853 static void
21854 produce_image_glyph (struct it *it)
21855 {
21856 struct image *img;
21857 struct face *face;
21858 int glyph_ascent, crop;
21859 struct glyph_slice slice;
21860
21861 xassert (it->what == IT_IMAGE);
21862
21863 face = FACE_FROM_ID (it->f, it->face_id);
21864 xassert (face);
21865 /* Make sure X resources of the face is loaded. */
21866 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21867
21868 if (it->image_id < 0)
21869 {
21870 /* Fringe bitmap. */
21871 it->ascent = it->phys_ascent = 0;
21872 it->descent = it->phys_descent = 0;
21873 it->pixel_width = 0;
21874 it->nglyphs = 0;
21875 return;
21876 }
21877
21878 img = IMAGE_FROM_ID (it->f, it->image_id);
21879 xassert (img);
21880 /* Make sure X resources of the image is loaded. */
21881 prepare_image_for_display (it->f, img);
21882
21883 slice.x = slice.y = 0;
21884 slice.width = img->width;
21885 slice.height = img->height;
21886
21887 if (INTEGERP (it->slice.x))
21888 slice.x = XINT (it->slice.x);
21889 else if (FLOATP (it->slice.x))
21890 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21891
21892 if (INTEGERP (it->slice.y))
21893 slice.y = XINT (it->slice.y);
21894 else if (FLOATP (it->slice.y))
21895 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21896
21897 if (INTEGERP (it->slice.width))
21898 slice.width = XINT (it->slice.width);
21899 else if (FLOATP (it->slice.width))
21900 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21901
21902 if (INTEGERP (it->slice.height))
21903 slice.height = XINT (it->slice.height);
21904 else if (FLOATP (it->slice.height))
21905 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21906
21907 if (slice.x >= img->width)
21908 slice.x = img->width;
21909 if (slice.y >= img->height)
21910 slice.y = img->height;
21911 if (slice.x + slice.width >= img->width)
21912 slice.width = img->width - slice.x;
21913 if (slice.y + slice.height > img->height)
21914 slice.height = img->height - slice.y;
21915
21916 if (slice.width == 0 || slice.height == 0)
21917 return;
21918
21919 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21920
21921 it->descent = slice.height - glyph_ascent;
21922 if (slice.y == 0)
21923 it->descent += img->vmargin;
21924 if (slice.y + slice.height == img->height)
21925 it->descent += img->vmargin;
21926 it->phys_descent = it->descent;
21927
21928 it->pixel_width = slice.width;
21929 if (slice.x == 0)
21930 it->pixel_width += img->hmargin;
21931 if (slice.x + slice.width == img->width)
21932 it->pixel_width += img->hmargin;
21933
21934 /* It's quite possible for images to have an ascent greater than
21935 their height, so don't get confused in that case. */
21936 if (it->descent < 0)
21937 it->descent = 0;
21938
21939 it->nglyphs = 1;
21940
21941 if (face->box != FACE_NO_BOX)
21942 {
21943 if (face->box_line_width > 0)
21944 {
21945 if (slice.y == 0)
21946 it->ascent += face->box_line_width;
21947 if (slice.y + slice.height == img->height)
21948 it->descent += face->box_line_width;
21949 }
21950
21951 if (it->start_of_box_run_p && slice.x == 0)
21952 it->pixel_width += eabs (face->box_line_width);
21953 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21954 it->pixel_width += eabs (face->box_line_width);
21955 }
21956
21957 take_vertical_position_into_account (it);
21958
21959 /* Automatically crop wide image glyphs at right edge so we can
21960 draw the cursor on same display row. */
21961 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21962 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21963 {
21964 it->pixel_width -= crop;
21965 slice.width -= crop;
21966 }
21967
21968 if (it->glyph_row)
21969 {
21970 struct glyph *glyph;
21971 enum glyph_row_area area = it->area;
21972
21973 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21974 if (glyph < it->glyph_row->glyphs[area + 1])
21975 {
21976 glyph->charpos = CHARPOS (it->position);
21977 glyph->object = it->object;
21978 glyph->pixel_width = it->pixel_width;
21979 glyph->ascent = glyph_ascent;
21980 glyph->descent = it->descent;
21981 glyph->voffset = it->voffset;
21982 glyph->type = IMAGE_GLYPH;
21983 glyph->avoid_cursor_p = it->avoid_cursor_p;
21984 glyph->multibyte_p = it->multibyte_p;
21985 glyph->left_box_line_p = it->start_of_box_run_p;
21986 glyph->right_box_line_p = it->end_of_box_run_p;
21987 glyph->overlaps_vertically_p = 0;
21988 glyph->padding_p = 0;
21989 glyph->glyph_not_available_p = 0;
21990 glyph->face_id = it->face_id;
21991 glyph->u.img_id = img->id;
21992 glyph->slice.img = slice;
21993 glyph->font_type = FONT_TYPE_UNKNOWN;
21994 if (it->bidi_p)
21995 {
21996 glyph->resolved_level = it->bidi_it.resolved_level;
21997 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21998 abort ();
21999 glyph->bidi_type = it->bidi_it.type;
22000 }
22001 ++it->glyph_row->used[area];
22002 }
22003 else
22004 IT_EXPAND_MATRIX_WIDTH (it, area);
22005 }
22006 }
22007
22008
22009 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22010 of the glyph, WIDTH and HEIGHT are the width and height of the
22011 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22012
22013 static void
22014 append_stretch_glyph (struct it *it, Lisp_Object object,
22015 int width, int height, int ascent)
22016 {
22017 struct glyph *glyph;
22018 enum glyph_row_area area = it->area;
22019
22020 xassert (ascent >= 0 && ascent <= height);
22021
22022 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22023 if (glyph < it->glyph_row->glyphs[area + 1])
22024 {
22025 /* If the glyph row is reversed, we need to prepend the glyph
22026 rather than append it. */
22027 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22028 {
22029 struct glyph *g;
22030
22031 /* Make room for the additional glyph. */
22032 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22033 g[1] = *g;
22034 glyph = it->glyph_row->glyphs[area];
22035 }
22036 glyph->charpos = CHARPOS (it->position);
22037 glyph->object = object;
22038 glyph->pixel_width = width;
22039 glyph->ascent = ascent;
22040 glyph->descent = height - ascent;
22041 glyph->voffset = it->voffset;
22042 glyph->type = STRETCH_GLYPH;
22043 glyph->avoid_cursor_p = it->avoid_cursor_p;
22044 glyph->multibyte_p = it->multibyte_p;
22045 glyph->left_box_line_p = it->start_of_box_run_p;
22046 glyph->right_box_line_p = it->end_of_box_run_p;
22047 glyph->overlaps_vertically_p = 0;
22048 glyph->padding_p = 0;
22049 glyph->glyph_not_available_p = 0;
22050 glyph->face_id = it->face_id;
22051 glyph->u.stretch.ascent = ascent;
22052 glyph->u.stretch.height = height;
22053 glyph->slice.img = null_glyph_slice;
22054 glyph->font_type = FONT_TYPE_UNKNOWN;
22055 if (it->bidi_p)
22056 {
22057 glyph->resolved_level = it->bidi_it.resolved_level;
22058 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22059 abort ();
22060 glyph->bidi_type = it->bidi_it.type;
22061 }
22062 else
22063 {
22064 glyph->resolved_level = 0;
22065 glyph->bidi_type = UNKNOWN_BT;
22066 }
22067 ++it->glyph_row->used[area];
22068 }
22069 else
22070 IT_EXPAND_MATRIX_WIDTH (it, area);
22071 }
22072
22073
22074 /* Produce a stretch glyph for iterator IT. IT->object is the value
22075 of the glyph property displayed. The value must be a list
22076 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22077 being recognized:
22078
22079 1. `:width WIDTH' specifies that the space should be WIDTH *
22080 canonical char width wide. WIDTH may be an integer or floating
22081 point number.
22082
22083 2. `:relative-width FACTOR' specifies that the width of the stretch
22084 should be computed from the width of the first character having the
22085 `glyph' property, and should be FACTOR times that width.
22086
22087 3. `:align-to HPOS' specifies that the space should be wide enough
22088 to reach HPOS, a value in canonical character units.
22089
22090 Exactly one of the above pairs must be present.
22091
22092 4. `:height HEIGHT' specifies that the height of the stretch produced
22093 should be HEIGHT, measured in canonical character units.
22094
22095 5. `:relative-height FACTOR' specifies that the height of the
22096 stretch should be FACTOR times the height of the characters having
22097 the glyph property.
22098
22099 Either none or exactly one of 4 or 5 must be present.
22100
22101 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22102 of the stretch should be used for the ascent of the stretch.
22103 ASCENT must be in the range 0 <= ASCENT <= 100. */
22104
22105 static void
22106 produce_stretch_glyph (struct it *it)
22107 {
22108 /* (space :width WIDTH :height HEIGHT ...) */
22109 Lisp_Object prop, plist;
22110 int width = 0, height = 0, align_to = -1;
22111 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22112 int ascent = 0;
22113 double tem;
22114 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22115 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22116
22117 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22118
22119 /* List should start with `space'. */
22120 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22121 plist = XCDR (it->object);
22122
22123 /* Compute the width of the stretch. */
22124 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22125 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22126 {
22127 /* Absolute width `:width WIDTH' specified and valid. */
22128 zero_width_ok_p = 1;
22129 width = (int)tem;
22130 }
22131 else if (prop = Fplist_get (plist, QCrelative_width),
22132 NUMVAL (prop) > 0)
22133 {
22134 /* Relative width `:relative-width FACTOR' specified and valid.
22135 Compute the width of the characters having the `glyph'
22136 property. */
22137 struct it it2;
22138 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22139
22140 it2 = *it;
22141 if (it->multibyte_p)
22142 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22143 else
22144 {
22145 it2.c = it2.char_to_display = *p, it2.len = 1;
22146 if (! ASCII_CHAR_P (it2.c))
22147 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22148 }
22149
22150 it2.glyph_row = NULL;
22151 it2.what = IT_CHARACTER;
22152 x_produce_glyphs (&it2);
22153 width = NUMVAL (prop) * it2.pixel_width;
22154 }
22155 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22156 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22157 {
22158 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22159 align_to = (align_to < 0
22160 ? 0
22161 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22162 else if (align_to < 0)
22163 align_to = window_box_left_offset (it->w, TEXT_AREA);
22164 width = max (0, (int)tem + align_to - it->current_x);
22165 zero_width_ok_p = 1;
22166 }
22167 else
22168 /* Nothing specified -> width defaults to canonical char width. */
22169 width = FRAME_COLUMN_WIDTH (it->f);
22170
22171 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22172 width = 1;
22173
22174 /* Compute height. */
22175 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22176 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22177 {
22178 height = (int)tem;
22179 zero_height_ok_p = 1;
22180 }
22181 else if (prop = Fplist_get (plist, QCrelative_height),
22182 NUMVAL (prop) > 0)
22183 height = FONT_HEIGHT (font) * NUMVAL (prop);
22184 else
22185 height = FONT_HEIGHT (font);
22186
22187 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22188 height = 1;
22189
22190 /* Compute percentage of height used for ascent. If
22191 `:ascent ASCENT' is present and valid, use that. Otherwise,
22192 derive the ascent from the font in use. */
22193 if (prop = Fplist_get (plist, QCascent),
22194 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22195 ascent = height * NUMVAL (prop) / 100.0;
22196 else if (!NILP (prop)
22197 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22198 ascent = min (max (0, (int)tem), height);
22199 else
22200 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22201
22202 if (width > 0 && it->line_wrap != TRUNCATE
22203 && it->current_x + width > it->last_visible_x)
22204 width = it->last_visible_x - it->current_x - 1;
22205
22206 if (width > 0 && height > 0 && it->glyph_row)
22207 {
22208 Lisp_Object object = it->stack[it->sp - 1].string;
22209 if (!STRINGP (object))
22210 object = it->w->buffer;
22211 append_stretch_glyph (it, object, width, height, ascent);
22212 }
22213
22214 it->pixel_width = width;
22215 it->ascent = it->phys_ascent = ascent;
22216 it->descent = it->phys_descent = height - it->ascent;
22217 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22218
22219 take_vertical_position_into_account (it);
22220 }
22221
22222 /* Calculate line-height and line-spacing properties.
22223 An integer value specifies explicit pixel value.
22224 A float value specifies relative value to current face height.
22225 A cons (float . face-name) specifies relative value to
22226 height of specified face font.
22227
22228 Returns height in pixels, or nil. */
22229
22230
22231 static Lisp_Object
22232 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22233 int boff, int override)
22234 {
22235 Lisp_Object face_name = Qnil;
22236 int ascent, descent, height;
22237
22238 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22239 return val;
22240
22241 if (CONSP (val))
22242 {
22243 face_name = XCAR (val);
22244 val = XCDR (val);
22245 if (!NUMBERP (val))
22246 val = make_number (1);
22247 if (NILP (face_name))
22248 {
22249 height = it->ascent + it->descent;
22250 goto scale;
22251 }
22252 }
22253
22254 if (NILP (face_name))
22255 {
22256 font = FRAME_FONT (it->f);
22257 boff = FRAME_BASELINE_OFFSET (it->f);
22258 }
22259 else if (EQ (face_name, Qt))
22260 {
22261 override = 0;
22262 }
22263 else
22264 {
22265 int face_id;
22266 struct face *face;
22267
22268 face_id = lookup_named_face (it->f, face_name, 0);
22269 if (face_id < 0)
22270 return make_number (-1);
22271
22272 face = FACE_FROM_ID (it->f, face_id);
22273 font = face->font;
22274 if (font == NULL)
22275 return make_number (-1);
22276 boff = font->baseline_offset;
22277 if (font->vertical_centering)
22278 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22279 }
22280
22281 ascent = FONT_BASE (font) + boff;
22282 descent = FONT_DESCENT (font) - boff;
22283
22284 if (override)
22285 {
22286 it->override_ascent = ascent;
22287 it->override_descent = descent;
22288 it->override_boff = boff;
22289 }
22290
22291 height = ascent + descent;
22292
22293 scale:
22294 if (FLOATP (val))
22295 height = (int)(XFLOAT_DATA (val) * height);
22296 else if (INTEGERP (val))
22297 height *= XINT (val);
22298
22299 return make_number (height);
22300 }
22301
22302
22303 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22304 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22305 and only if this is for a character for which no font was found.
22306
22307 If the display method (it->glyphless_method) is
22308 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22309 length of the acronym or the hexadecimal string, UPPER_XOFF and
22310 UPPER_YOFF are pixel offsets for the upper part of the string,
22311 LOWER_XOFF and LOWER_YOFF are for the lower part.
22312
22313 For the other display methods, LEN through LOWER_YOFF are zero. */
22314
22315 static void
22316 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22317 short upper_xoff, short upper_yoff,
22318 short lower_xoff, short lower_yoff)
22319 {
22320 struct glyph *glyph;
22321 enum glyph_row_area area = it->area;
22322
22323 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22324 if (glyph < it->glyph_row->glyphs[area + 1])
22325 {
22326 /* If the glyph row is reversed, we need to prepend the glyph
22327 rather than append it. */
22328 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22329 {
22330 struct glyph *g;
22331
22332 /* Make room for the additional glyph. */
22333 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22334 g[1] = *g;
22335 glyph = it->glyph_row->glyphs[area];
22336 }
22337 glyph->charpos = CHARPOS (it->position);
22338 glyph->object = it->object;
22339 glyph->pixel_width = it->pixel_width;
22340 glyph->ascent = it->ascent;
22341 glyph->descent = it->descent;
22342 glyph->voffset = it->voffset;
22343 glyph->type = GLYPHLESS_GLYPH;
22344 glyph->u.glyphless.method = it->glyphless_method;
22345 glyph->u.glyphless.for_no_font = for_no_font;
22346 glyph->u.glyphless.len = len;
22347 glyph->u.glyphless.ch = it->c;
22348 glyph->slice.glyphless.upper_xoff = upper_xoff;
22349 glyph->slice.glyphless.upper_yoff = upper_yoff;
22350 glyph->slice.glyphless.lower_xoff = lower_xoff;
22351 glyph->slice.glyphless.lower_yoff = lower_yoff;
22352 glyph->avoid_cursor_p = it->avoid_cursor_p;
22353 glyph->multibyte_p = it->multibyte_p;
22354 glyph->left_box_line_p = it->start_of_box_run_p;
22355 glyph->right_box_line_p = it->end_of_box_run_p;
22356 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22357 || it->phys_descent > it->descent);
22358 glyph->padding_p = 0;
22359 glyph->glyph_not_available_p = 0;
22360 glyph->face_id = face_id;
22361 glyph->font_type = FONT_TYPE_UNKNOWN;
22362 if (it->bidi_p)
22363 {
22364 glyph->resolved_level = it->bidi_it.resolved_level;
22365 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22366 abort ();
22367 glyph->bidi_type = it->bidi_it.type;
22368 }
22369 ++it->glyph_row->used[area];
22370 }
22371 else
22372 IT_EXPAND_MATRIX_WIDTH (it, area);
22373 }
22374
22375
22376 /* Produce a glyph for a glyphless character for iterator IT.
22377 IT->glyphless_method specifies which method to use for displaying
22378 the character. See the description of enum
22379 glyphless_display_method in dispextern.h for the detail.
22380
22381 FOR_NO_FONT is nonzero if and only if this is for a character for
22382 which no font was found. ACRONYM, if non-nil, is an acronym string
22383 for the character. */
22384
22385 static void
22386 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22387 {
22388 int face_id;
22389 struct face *face;
22390 struct font *font;
22391 int base_width, base_height, width, height;
22392 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22393 int len;
22394
22395 /* Get the metrics of the base font. We always refer to the current
22396 ASCII face. */
22397 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22398 font = face->font ? face->font : FRAME_FONT (it->f);
22399 it->ascent = FONT_BASE (font) + font->baseline_offset;
22400 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22401 base_height = it->ascent + it->descent;
22402 base_width = font->average_width;
22403
22404 /* Get a face ID for the glyph by utilizing a cache (the same way as
22405 doen for `escape-glyph' in get_next_display_element). */
22406 if (it->f == last_glyphless_glyph_frame
22407 && it->face_id == last_glyphless_glyph_face_id)
22408 {
22409 face_id = last_glyphless_glyph_merged_face_id;
22410 }
22411 else
22412 {
22413 /* Merge the `glyphless-char' face into the current face. */
22414 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22415 last_glyphless_glyph_frame = it->f;
22416 last_glyphless_glyph_face_id = it->face_id;
22417 last_glyphless_glyph_merged_face_id = face_id;
22418 }
22419
22420 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22421 {
22422 it->pixel_width = THIN_SPACE_WIDTH;
22423 len = 0;
22424 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22425 }
22426 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22427 {
22428 width = CHAR_WIDTH (it->c);
22429 if (width == 0)
22430 width = 1;
22431 else if (width > 4)
22432 width = 4;
22433 it->pixel_width = base_width * width;
22434 len = 0;
22435 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22436 }
22437 else
22438 {
22439 char buf[7];
22440 const char *str;
22441 unsigned int code[6];
22442 int upper_len;
22443 int ascent, descent;
22444 struct font_metrics metrics_upper, metrics_lower;
22445
22446 face = FACE_FROM_ID (it->f, face_id);
22447 font = face->font ? face->font : FRAME_FONT (it->f);
22448 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22449
22450 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22451 {
22452 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22453 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22454 if (CONSP (acronym))
22455 acronym = XCAR (acronym);
22456 str = STRINGP (acronym) ? SSDATA (acronym) : "";
22457 }
22458 else
22459 {
22460 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22461 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22462 str = buf;
22463 }
22464 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22465 code[len] = font->driver->encode_char (font, str[len]);
22466 upper_len = (len + 1) / 2;
22467 font->driver->text_extents (font, code, upper_len,
22468 &metrics_upper);
22469 font->driver->text_extents (font, code + upper_len, len - upper_len,
22470 &metrics_lower);
22471
22472
22473
22474 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22475 width = max (metrics_upper.width, metrics_lower.width) + 4;
22476 upper_xoff = upper_yoff = 2; /* the typical case */
22477 if (base_width >= width)
22478 {
22479 /* Align the upper to the left, the lower to the right. */
22480 it->pixel_width = base_width;
22481 lower_xoff = base_width - 2 - metrics_lower.width;
22482 }
22483 else
22484 {
22485 /* Center the shorter one. */
22486 it->pixel_width = width;
22487 if (metrics_upper.width >= metrics_lower.width)
22488 lower_xoff = (width - metrics_lower.width) / 2;
22489 else
22490 {
22491 /* FIXME: This code doesn't look right. It formerly was
22492 missing the "lower_xoff = 0;", which couldn't have
22493 been right since it left lower_xoff uninitialized. */
22494 lower_xoff = 0;
22495 upper_xoff = (width - metrics_upper.width) / 2;
22496 }
22497 }
22498
22499 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22500 top, bottom, and between upper and lower strings. */
22501 height = (metrics_upper.ascent + metrics_upper.descent
22502 + metrics_lower.ascent + metrics_lower.descent) + 5;
22503 /* Center vertically.
22504 H:base_height, D:base_descent
22505 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22506
22507 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22508 descent = D - H/2 + h/2;
22509 lower_yoff = descent - 2 - ld;
22510 upper_yoff = lower_yoff - la - 1 - ud; */
22511 ascent = - (it->descent - (base_height + height + 1) / 2);
22512 descent = it->descent - (base_height - height) / 2;
22513 lower_yoff = descent - 2 - metrics_lower.descent;
22514 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22515 - metrics_upper.descent);
22516 /* Don't make the height shorter than the base height. */
22517 if (height > base_height)
22518 {
22519 it->ascent = ascent;
22520 it->descent = descent;
22521 }
22522 }
22523
22524 it->phys_ascent = it->ascent;
22525 it->phys_descent = it->descent;
22526 if (it->glyph_row)
22527 append_glyphless_glyph (it, face_id, for_no_font, len,
22528 upper_xoff, upper_yoff,
22529 lower_xoff, lower_yoff);
22530 it->nglyphs = 1;
22531 take_vertical_position_into_account (it);
22532 }
22533
22534
22535 /* RIF:
22536 Produce glyphs/get display metrics for the display element IT is
22537 loaded with. See the description of struct it in dispextern.h
22538 for an overview of struct it. */
22539
22540 void
22541 x_produce_glyphs (struct it *it)
22542 {
22543 int extra_line_spacing = it->extra_line_spacing;
22544
22545 it->glyph_not_available_p = 0;
22546
22547 if (it->what == IT_CHARACTER)
22548 {
22549 XChar2b char2b;
22550 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22551 struct font *font = face->font;
22552 struct font_metrics *pcm = NULL;
22553 int boff; /* baseline offset */
22554
22555 if (font == NULL)
22556 {
22557 /* When no suitable font is found, display this character by
22558 the method specified in the first extra slot of
22559 Vglyphless_char_display. */
22560 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22561
22562 xassert (it->what == IT_GLYPHLESS);
22563 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22564 goto done;
22565 }
22566
22567 boff = font->baseline_offset;
22568 if (font->vertical_centering)
22569 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22570
22571 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22572 {
22573 int stretched_p;
22574
22575 it->nglyphs = 1;
22576
22577 if (it->override_ascent >= 0)
22578 {
22579 it->ascent = it->override_ascent;
22580 it->descent = it->override_descent;
22581 boff = it->override_boff;
22582 }
22583 else
22584 {
22585 it->ascent = FONT_BASE (font) + boff;
22586 it->descent = FONT_DESCENT (font) - boff;
22587 }
22588
22589 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22590 {
22591 pcm = get_per_char_metric (font, &char2b);
22592 if (pcm->width == 0
22593 && pcm->rbearing == 0 && pcm->lbearing == 0)
22594 pcm = NULL;
22595 }
22596
22597 if (pcm)
22598 {
22599 it->phys_ascent = pcm->ascent + boff;
22600 it->phys_descent = pcm->descent - boff;
22601 it->pixel_width = pcm->width;
22602 }
22603 else
22604 {
22605 it->glyph_not_available_p = 1;
22606 it->phys_ascent = it->ascent;
22607 it->phys_descent = it->descent;
22608 it->pixel_width = font->space_width;
22609 }
22610
22611 if (it->constrain_row_ascent_descent_p)
22612 {
22613 if (it->descent > it->max_descent)
22614 {
22615 it->ascent += it->descent - it->max_descent;
22616 it->descent = it->max_descent;
22617 }
22618 if (it->ascent > it->max_ascent)
22619 {
22620 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22621 it->ascent = it->max_ascent;
22622 }
22623 it->phys_ascent = min (it->phys_ascent, it->ascent);
22624 it->phys_descent = min (it->phys_descent, it->descent);
22625 extra_line_spacing = 0;
22626 }
22627
22628 /* If this is a space inside a region of text with
22629 `space-width' property, change its width. */
22630 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22631 if (stretched_p)
22632 it->pixel_width *= XFLOATINT (it->space_width);
22633
22634 /* If face has a box, add the box thickness to the character
22635 height. If character has a box line to the left and/or
22636 right, add the box line width to the character's width. */
22637 if (face->box != FACE_NO_BOX)
22638 {
22639 int thick = face->box_line_width;
22640
22641 if (thick > 0)
22642 {
22643 it->ascent += thick;
22644 it->descent += thick;
22645 }
22646 else
22647 thick = -thick;
22648
22649 if (it->start_of_box_run_p)
22650 it->pixel_width += thick;
22651 if (it->end_of_box_run_p)
22652 it->pixel_width += thick;
22653 }
22654
22655 /* If face has an overline, add the height of the overline
22656 (1 pixel) and a 1 pixel margin to the character height. */
22657 if (face->overline_p)
22658 it->ascent += overline_margin;
22659
22660 if (it->constrain_row_ascent_descent_p)
22661 {
22662 if (it->ascent > it->max_ascent)
22663 it->ascent = it->max_ascent;
22664 if (it->descent > it->max_descent)
22665 it->descent = it->max_descent;
22666 }
22667
22668 take_vertical_position_into_account (it);
22669
22670 /* If we have to actually produce glyphs, do it. */
22671 if (it->glyph_row)
22672 {
22673 if (stretched_p)
22674 {
22675 /* Translate a space with a `space-width' property
22676 into a stretch glyph. */
22677 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22678 / FONT_HEIGHT (font));
22679 append_stretch_glyph (it, it->object, it->pixel_width,
22680 it->ascent + it->descent, ascent);
22681 }
22682 else
22683 append_glyph (it);
22684
22685 /* If characters with lbearing or rbearing are displayed
22686 in this line, record that fact in a flag of the
22687 glyph row. This is used to optimize X output code. */
22688 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22689 it->glyph_row->contains_overlapping_glyphs_p = 1;
22690 }
22691 if (! stretched_p && it->pixel_width == 0)
22692 /* We assure that all visible glyphs have at least 1-pixel
22693 width. */
22694 it->pixel_width = 1;
22695 }
22696 else if (it->char_to_display == '\n')
22697 {
22698 /* A newline has no width, but we need the height of the
22699 line. But if previous part of the line sets a height,
22700 don't increase that height */
22701
22702 Lisp_Object height;
22703 Lisp_Object total_height = Qnil;
22704
22705 it->override_ascent = -1;
22706 it->pixel_width = 0;
22707 it->nglyphs = 0;
22708
22709 height = get_it_property (it, Qline_height);
22710 /* Split (line-height total-height) list */
22711 if (CONSP (height)
22712 && CONSP (XCDR (height))
22713 && NILP (XCDR (XCDR (height))))
22714 {
22715 total_height = XCAR (XCDR (height));
22716 height = XCAR (height);
22717 }
22718 height = calc_line_height_property (it, height, font, boff, 1);
22719
22720 if (it->override_ascent >= 0)
22721 {
22722 it->ascent = it->override_ascent;
22723 it->descent = it->override_descent;
22724 boff = it->override_boff;
22725 }
22726 else
22727 {
22728 it->ascent = FONT_BASE (font) + boff;
22729 it->descent = FONT_DESCENT (font) - boff;
22730 }
22731
22732 if (EQ (height, Qt))
22733 {
22734 if (it->descent > it->max_descent)
22735 {
22736 it->ascent += it->descent - it->max_descent;
22737 it->descent = it->max_descent;
22738 }
22739 if (it->ascent > it->max_ascent)
22740 {
22741 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22742 it->ascent = it->max_ascent;
22743 }
22744 it->phys_ascent = min (it->phys_ascent, it->ascent);
22745 it->phys_descent = min (it->phys_descent, it->descent);
22746 it->constrain_row_ascent_descent_p = 1;
22747 extra_line_spacing = 0;
22748 }
22749 else
22750 {
22751 Lisp_Object spacing;
22752
22753 it->phys_ascent = it->ascent;
22754 it->phys_descent = it->descent;
22755
22756 if ((it->max_ascent > 0 || it->max_descent > 0)
22757 && face->box != FACE_NO_BOX
22758 && face->box_line_width > 0)
22759 {
22760 it->ascent += face->box_line_width;
22761 it->descent += face->box_line_width;
22762 }
22763 if (!NILP (height)
22764 && XINT (height) > it->ascent + it->descent)
22765 it->ascent = XINT (height) - it->descent;
22766
22767 if (!NILP (total_height))
22768 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22769 else
22770 {
22771 spacing = get_it_property (it, Qline_spacing);
22772 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22773 }
22774 if (INTEGERP (spacing))
22775 {
22776 extra_line_spacing = XINT (spacing);
22777 if (!NILP (total_height))
22778 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22779 }
22780 }
22781 }
22782 else /* i.e. (it->char_to_display == '\t') */
22783 {
22784 if (font->space_width > 0)
22785 {
22786 int tab_width = it->tab_width * font->space_width;
22787 int x = it->current_x + it->continuation_lines_width;
22788 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22789
22790 /* If the distance from the current position to the next tab
22791 stop is less than a space character width, use the
22792 tab stop after that. */
22793 if (next_tab_x - x < font->space_width)
22794 next_tab_x += tab_width;
22795
22796 it->pixel_width = next_tab_x - x;
22797 it->nglyphs = 1;
22798 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22799 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22800
22801 if (it->glyph_row)
22802 {
22803 append_stretch_glyph (it, it->object, it->pixel_width,
22804 it->ascent + it->descent, it->ascent);
22805 }
22806 }
22807 else
22808 {
22809 it->pixel_width = 0;
22810 it->nglyphs = 1;
22811 }
22812 }
22813 }
22814 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22815 {
22816 /* A static composition.
22817
22818 Note: A composition is represented as one glyph in the
22819 glyph matrix. There are no padding glyphs.
22820
22821 Important note: pixel_width, ascent, and descent are the
22822 values of what is drawn by draw_glyphs (i.e. the values of
22823 the overall glyphs composed). */
22824 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22825 int boff; /* baseline offset */
22826 struct composition *cmp = composition_table[it->cmp_it.id];
22827 int glyph_len = cmp->glyph_len;
22828 struct font *font = face->font;
22829
22830 it->nglyphs = 1;
22831
22832 /* If we have not yet calculated pixel size data of glyphs of
22833 the composition for the current face font, calculate them
22834 now. Theoretically, we have to check all fonts for the
22835 glyphs, but that requires much time and memory space. So,
22836 here we check only the font of the first glyph. This may
22837 lead to incorrect display, but it's very rare, and C-l
22838 (recenter-top-bottom) can correct the display anyway. */
22839 if (! cmp->font || cmp->font != font)
22840 {
22841 /* Ascent and descent of the font of the first character
22842 of this composition (adjusted by baseline offset).
22843 Ascent and descent of overall glyphs should not be less
22844 than these, respectively. */
22845 int font_ascent, font_descent, font_height;
22846 /* Bounding box of the overall glyphs. */
22847 int leftmost, rightmost, lowest, highest;
22848 int lbearing, rbearing;
22849 int i, width, ascent, descent;
22850 int left_padded = 0, right_padded = 0;
22851 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
22852 XChar2b char2b;
22853 struct font_metrics *pcm;
22854 int font_not_found_p;
22855 EMACS_INT pos;
22856
22857 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22858 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22859 break;
22860 if (glyph_len < cmp->glyph_len)
22861 right_padded = 1;
22862 for (i = 0; i < glyph_len; i++)
22863 {
22864 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22865 break;
22866 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22867 }
22868 if (i > 0)
22869 left_padded = 1;
22870
22871 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22872 : IT_CHARPOS (*it));
22873 /* If no suitable font is found, use the default font. */
22874 font_not_found_p = font == NULL;
22875 if (font_not_found_p)
22876 {
22877 face = face->ascii_face;
22878 font = face->font;
22879 }
22880 boff = font->baseline_offset;
22881 if (font->vertical_centering)
22882 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22883 font_ascent = FONT_BASE (font) + boff;
22884 font_descent = FONT_DESCENT (font) - boff;
22885 font_height = FONT_HEIGHT (font);
22886
22887 cmp->font = (void *) font;
22888
22889 pcm = NULL;
22890 if (! font_not_found_p)
22891 {
22892 get_char_face_and_encoding (it->f, c, it->face_id,
22893 &char2b, 0);
22894 pcm = get_per_char_metric (font, &char2b);
22895 }
22896
22897 /* Initialize the bounding box. */
22898 if (pcm)
22899 {
22900 width = pcm->width;
22901 ascent = pcm->ascent;
22902 descent = pcm->descent;
22903 lbearing = pcm->lbearing;
22904 rbearing = pcm->rbearing;
22905 }
22906 else
22907 {
22908 width = font->space_width;
22909 ascent = FONT_BASE (font);
22910 descent = FONT_DESCENT (font);
22911 lbearing = 0;
22912 rbearing = width;
22913 }
22914
22915 rightmost = width;
22916 leftmost = 0;
22917 lowest = - descent + boff;
22918 highest = ascent + boff;
22919
22920 if (! font_not_found_p
22921 && font->default_ascent
22922 && CHAR_TABLE_P (Vuse_default_ascent)
22923 && !NILP (Faref (Vuse_default_ascent,
22924 make_number (it->char_to_display))))
22925 highest = font->default_ascent + boff;
22926
22927 /* Draw the first glyph at the normal position. It may be
22928 shifted to right later if some other glyphs are drawn
22929 at the left. */
22930 cmp->offsets[i * 2] = 0;
22931 cmp->offsets[i * 2 + 1] = boff;
22932 cmp->lbearing = lbearing;
22933 cmp->rbearing = rbearing;
22934
22935 /* Set cmp->offsets for the remaining glyphs. */
22936 for (i++; i < glyph_len; i++)
22937 {
22938 int left, right, btm, top;
22939 int ch = COMPOSITION_GLYPH (cmp, i);
22940 int face_id;
22941 struct face *this_face;
22942
22943 if (ch == '\t')
22944 ch = ' ';
22945 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22946 this_face = FACE_FROM_ID (it->f, face_id);
22947 font = this_face->font;
22948
22949 if (font == NULL)
22950 pcm = NULL;
22951 else
22952 {
22953 get_char_face_and_encoding (it->f, ch, face_id,
22954 &char2b, 0);
22955 pcm = get_per_char_metric (font, &char2b);
22956 }
22957 if (! pcm)
22958 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22959 else
22960 {
22961 width = pcm->width;
22962 ascent = pcm->ascent;
22963 descent = pcm->descent;
22964 lbearing = pcm->lbearing;
22965 rbearing = pcm->rbearing;
22966 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22967 {
22968 /* Relative composition with or without
22969 alternate chars. */
22970 left = (leftmost + rightmost - width) / 2;
22971 btm = - descent + boff;
22972 if (font->relative_compose
22973 && (! CHAR_TABLE_P (Vignore_relative_composition)
22974 || NILP (Faref (Vignore_relative_composition,
22975 make_number (ch)))))
22976 {
22977
22978 if (- descent >= font->relative_compose)
22979 /* One extra pixel between two glyphs. */
22980 btm = highest + 1;
22981 else if (ascent <= 0)
22982 /* One extra pixel between two glyphs. */
22983 btm = lowest - 1 - ascent - descent;
22984 }
22985 }
22986 else
22987 {
22988 /* A composition rule is specified by an integer
22989 value that encodes global and new reference
22990 points (GREF and NREF). GREF and NREF are
22991 specified by numbers as below:
22992
22993 0---1---2 -- ascent
22994 | |
22995 | |
22996 | |
22997 9--10--11 -- center
22998 | |
22999 ---3---4---5--- baseline
23000 | |
23001 6---7---8 -- descent
23002 */
23003 int rule = COMPOSITION_RULE (cmp, i);
23004 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23005
23006 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23007 grefx = gref % 3, nrefx = nref % 3;
23008 grefy = gref / 3, nrefy = nref / 3;
23009 if (xoff)
23010 xoff = font_height * (xoff - 128) / 256;
23011 if (yoff)
23012 yoff = font_height * (yoff - 128) / 256;
23013
23014 left = (leftmost
23015 + grefx * (rightmost - leftmost) / 2
23016 - nrefx * width / 2
23017 + xoff);
23018
23019 btm = ((grefy == 0 ? highest
23020 : grefy == 1 ? 0
23021 : grefy == 2 ? lowest
23022 : (highest + lowest) / 2)
23023 - (nrefy == 0 ? ascent + descent
23024 : nrefy == 1 ? descent - boff
23025 : nrefy == 2 ? 0
23026 : (ascent + descent) / 2)
23027 + yoff);
23028 }
23029
23030 cmp->offsets[i * 2] = left;
23031 cmp->offsets[i * 2 + 1] = btm + descent;
23032
23033 /* Update the bounding box of the overall glyphs. */
23034 if (width > 0)
23035 {
23036 right = left + width;
23037 if (left < leftmost)
23038 leftmost = left;
23039 if (right > rightmost)
23040 rightmost = right;
23041 }
23042 top = btm + descent + ascent;
23043 if (top > highest)
23044 highest = top;
23045 if (btm < lowest)
23046 lowest = btm;
23047
23048 if (cmp->lbearing > left + lbearing)
23049 cmp->lbearing = left + lbearing;
23050 if (cmp->rbearing < left + rbearing)
23051 cmp->rbearing = left + rbearing;
23052 }
23053 }
23054
23055 /* If there are glyphs whose x-offsets are negative,
23056 shift all glyphs to the right and make all x-offsets
23057 non-negative. */
23058 if (leftmost < 0)
23059 {
23060 for (i = 0; i < cmp->glyph_len; i++)
23061 cmp->offsets[i * 2] -= leftmost;
23062 rightmost -= leftmost;
23063 cmp->lbearing -= leftmost;
23064 cmp->rbearing -= leftmost;
23065 }
23066
23067 if (left_padded && cmp->lbearing < 0)
23068 {
23069 for (i = 0; i < cmp->glyph_len; i++)
23070 cmp->offsets[i * 2] -= cmp->lbearing;
23071 rightmost -= cmp->lbearing;
23072 cmp->rbearing -= cmp->lbearing;
23073 cmp->lbearing = 0;
23074 }
23075 if (right_padded && rightmost < cmp->rbearing)
23076 {
23077 rightmost = cmp->rbearing;
23078 }
23079
23080 cmp->pixel_width = rightmost;
23081 cmp->ascent = highest;
23082 cmp->descent = - lowest;
23083 if (cmp->ascent < font_ascent)
23084 cmp->ascent = font_ascent;
23085 if (cmp->descent < font_descent)
23086 cmp->descent = font_descent;
23087 }
23088
23089 if (it->glyph_row
23090 && (cmp->lbearing < 0
23091 || cmp->rbearing > cmp->pixel_width))
23092 it->glyph_row->contains_overlapping_glyphs_p = 1;
23093
23094 it->pixel_width = cmp->pixel_width;
23095 it->ascent = it->phys_ascent = cmp->ascent;
23096 it->descent = it->phys_descent = cmp->descent;
23097 if (face->box != FACE_NO_BOX)
23098 {
23099 int thick = face->box_line_width;
23100
23101 if (thick > 0)
23102 {
23103 it->ascent += thick;
23104 it->descent += thick;
23105 }
23106 else
23107 thick = - thick;
23108
23109 if (it->start_of_box_run_p)
23110 it->pixel_width += thick;
23111 if (it->end_of_box_run_p)
23112 it->pixel_width += thick;
23113 }
23114
23115 /* If face has an overline, add the height of the overline
23116 (1 pixel) and a 1 pixel margin to the character height. */
23117 if (face->overline_p)
23118 it->ascent += overline_margin;
23119
23120 take_vertical_position_into_account (it);
23121 if (it->ascent < 0)
23122 it->ascent = 0;
23123 if (it->descent < 0)
23124 it->descent = 0;
23125
23126 if (it->glyph_row)
23127 append_composite_glyph (it);
23128 }
23129 else if (it->what == IT_COMPOSITION)
23130 {
23131 /* A dynamic (automatic) composition. */
23132 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23133 Lisp_Object gstring;
23134 struct font_metrics metrics;
23135
23136 gstring = composition_gstring_from_id (it->cmp_it.id);
23137 it->pixel_width
23138 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23139 &metrics);
23140 if (it->glyph_row
23141 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23142 it->glyph_row->contains_overlapping_glyphs_p = 1;
23143 it->ascent = it->phys_ascent = metrics.ascent;
23144 it->descent = it->phys_descent = metrics.descent;
23145 if (face->box != FACE_NO_BOX)
23146 {
23147 int thick = face->box_line_width;
23148
23149 if (thick > 0)
23150 {
23151 it->ascent += thick;
23152 it->descent += thick;
23153 }
23154 else
23155 thick = - thick;
23156
23157 if (it->start_of_box_run_p)
23158 it->pixel_width += thick;
23159 if (it->end_of_box_run_p)
23160 it->pixel_width += thick;
23161 }
23162 /* If face has an overline, add the height of the overline
23163 (1 pixel) and a 1 pixel margin to the character height. */
23164 if (face->overline_p)
23165 it->ascent += overline_margin;
23166 take_vertical_position_into_account (it);
23167 if (it->ascent < 0)
23168 it->ascent = 0;
23169 if (it->descent < 0)
23170 it->descent = 0;
23171
23172 if (it->glyph_row)
23173 append_composite_glyph (it);
23174 }
23175 else if (it->what == IT_GLYPHLESS)
23176 produce_glyphless_glyph (it, 0, Qnil);
23177 else if (it->what == IT_IMAGE)
23178 produce_image_glyph (it);
23179 else if (it->what == IT_STRETCH)
23180 produce_stretch_glyph (it);
23181
23182 done:
23183 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23184 because this isn't true for images with `:ascent 100'. */
23185 xassert (it->ascent >= 0 && it->descent >= 0);
23186 if (it->area == TEXT_AREA)
23187 it->current_x += it->pixel_width;
23188
23189 if (extra_line_spacing > 0)
23190 {
23191 it->descent += extra_line_spacing;
23192 if (extra_line_spacing > it->max_extra_line_spacing)
23193 it->max_extra_line_spacing = extra_line_spacing;
23194 }
23195
23196 it->max_ascent = max (it->max_ascent, it->ascent);
23197 it->max_descent = max (it->max_descent, it->descent);
23198 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23199 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23200 }
23201
23202 /* EXPORT for RIF:
23203 Output LEN glyphs starting at START at the nominal cursor position.
23204 Advance the nominal cursor over the text. The global variable
23205 updated_window contains the window being updated, updated_row is
23206 the glyph row being updated, and updated_area is the area of that
23207 row being updated. */
23208
23209 void
23210 x_write_glyphs (struct glyph *start, int len)
23211 {
23212 int x, hpos;
23213
23214 xassert (updated_window && updated_row);
23215 BLOCK_INPUT;
23216
23217 /* Write glyphs. */
23218
23219 hpos = start - updated_row->glyphs[updated_area];
23220 x = draw_glyphs (updated_window, output_cursor.x,
23221 updated_row, updated_area,
23222 hpos, hpos + len,
23223 DRAW_NORMAL_TEXT, 0);
23224
23225 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23226 if (updated_area == TEXT_AREA
23227 && updated_window->phys_cursor_on_p
23228 && updated_window->phys_cursor.vpos == output_cursor.vpos
23229 && updated_window->phys_cursor.hpos >= hpos
23230 && updated_window->phys_cursor.hpos < hpos + len)
23231 updated_window->phys_cursor_on_p = 0;
23232
23233 UNBLOCK_INPUT;
23234
23235 /* Advance the output cursor. */
23236 output_cursor.hpos += len;
23237 output_cursor.x = x;
23238 }
23239
23240
23241 /* EXPORT for RIF:
23242 Insert LEN glyphs from START at the nominal cursor position. */
23243
23244 void
23245 x_insert_glyphs (struct glyph *start, int len)
23246 {
23247 struct frame *f;
23248 struct window *w;
23249 int line_height, shift_by_width, shifted_region_width;
23250 struct glyph_row *row;
23251 struct glyph *glyph;
23252 int frame_x, frame_y;
23253 EMACS_INT hpos;
23254
23255 xassert (updated_window && updated_row);
23256 BLOCK_INPUT;
23257 w = updated_window;
23258 f = XFRAME (WINDOW_FRAME (w));
23259
23260 /* Get the height of the line we are in. */
23261 row = updated_row;
23262 line_height = row->height;
23263
23264 /* Get the width of the glyphs to insert. */
23265 shift_by_width = 0;
23266 for (glyph = start; glyph < start + len; ++glyph)
23267 shift_by_width += glyph->pixel_width;
23268
23269 /* Get the width of the region to shift right. */
23270 shifted_region_width = (window_box_width (w, updated_area)
23271 - output_cursor.x
23272 - shift_by_width);
23273
23274 /* Shift right. */
23275 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23276 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23277
23278 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23279 line_height, shift_by_width);
23280
23281 /* Write the glyphs. */
23282 hpos = start - row->glyphs[updated_area];
23283 draw_glyphs (w, output_cursor.x, row, updated_area,
23284 hpos, hpos + len,
23285 DRAW_NORMAL_TEXT, 0);
23286
23287 /* Advance the output cursor. */
23288 output_cursor.hpos += len;
23289 output_cursor.x += shift_by_width;
23290 UNBLOCK_INPUT;
23291 }
23292
23293
23294 /* EXPORT for RIF:
23295 Erase the current text line from the nominal cursor position
23296 (inclusive) to pixel column TO_X (exclusive). The idea is that
23297 everything from TO_X onward is already erased.
23298
23299 TO_X is a pixel position relative to updated_area of
23300 updated_window. TO_X == -1 means clear to the end of this area. */
23301
23302 void
23303 x_clear_end_of_line (int to_x)
23304 {
23305 struct frame *f;
23306 struct window *w = updated_window;
23307 int max_x, min_y, max_y;
23308 int from_x, from_y, to_y;
23309
23310 xassert (updated_window && updated_row);
23311 f = XFRAME (w->frame);
23312
23313 if (updated_row->full_width_p)
23314 max_x = WINDOW_TOTAL_WIDTH (w);
23315 else
23316 max_x = window_box_width (w, updated_area);
23317 max_y = window_text_bottom_y (w);
23318
23319 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23320 of window. For TO_X > 0, truncate to end of drawing area. */
23321 if (to_x == 0)
23322 return;
23323 else if (to_x < 0)
23324 to_x = max_x;
23325 else
23326 to_x = min (to_x, max_x);
23327
23328 to_y = min (max_y, output_cursor.y + updated_row->height);
23329
23330 /* Notice if the cursor will be cleared by this operation. */
23331 if (!updated_row->full_width_p)
23332 notice_overwritten_cursor (w, updated_area,
23333 output_cursor.x, -1,
23334 updated_row->y,
23335 MATRIX_ROW_BOTTOM_Y (updated_row));
23336
23337 from_x = output_cursor.x;
23338
23339 /* Translate to frame coordinates. */
23340 if (updated_row->full_width_p)
23341 {
23342 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23343 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23344 }
23345 else
23346 {
23347 int area_left = window_box_left (w, updated_area);
23348 from_x += area_left;
23349 to_x += area_left;
23350 }
23351
23352 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23353 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23354 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23355
23356 /* Prevent inadvertently clearing to end of the X window. */
23357 if (to_x > from_x && to_y > from_y)
23358 {
23359 BLOCK_INPUT;
23360 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23361 to_x - from_x, to_y - from_y);
23362 UNBLOCK_INPUT;
23363 }
23364 }
23365
23366 #endif /* HAVE_WINDOW_SYSTEM */
23367
23368
23369 \f
23370 /***********************************************************************
23371 Cursor types
23372 ***********************************************************************/
23373
23374 /* Value is the internal representation of the specified cursor type
23375 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23376 of the bar cursor. */
23377
23378 static enum text_cursor_kinds
23379 get_specified_cursor_type (Lisp_Object arg, int *width)
23380 {
23381 enum text_cursor_kinds type;
23382
23383 if (NILP (arg))
23384 return NO_CURSOR;
23385
23386 if (EQ (arg, Qbox))
23387 return FILLED_BOX_CURSOR;
23388
23389 if (EQ (arg, Qhollow))
23390 return HOLLOW_BOX_CURSOR;
23391
23392 if (EQ (arg, Qbar))
23393 {
23394 *width = 2;
23395 return BAR_CURSOR;
23396 }
23397
23398 if (CONSP (arg)
23399 && EQ (XCAR (arg), Qbar)
23400 && INTEGERP (XCDR (arg))
23401 && XINT (XCDR (arg)) >= 0)
23402 {
23403 *width = XINT (XCDR (arg));
23404 return BAR_CURSOR;
23405 }
23406
23407 if (EQ (arg, Qhbar))
23408 {
23409 *width = 2;
23410 return HBAR_CURSOR;
23411 }
23412
23413 if (CONSP (arg)
23414 && EQ (XCAR (arg), Qhbar)
23415 && INTEGERP (XCDR (arg))
23416 && XINT (XCDR (arg)) >= 0)
23417 {
23418 *width = XINT (XCDR (arg));
23419 return HBAR_CURSOR;
23420 }
23421
23422 /* Treat anything unknown as "hollow box cursor".
23423 It was bad to signal an error; people have trouble fixing
23424 .Xdefaults with Emacs, when it has something bad in it. */
23425 type = HOLLOW_BOX_CURSOR;
23426
23427 return type;
23428 }
23429
23430 /* Set the default cursor types for specified frame. */
23431 void
23432 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23433 {
23434 int width = 1;
23435 Lisp_Object tem;
23436
23437 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23438 FRAME_CURSOR_WIDTH (f) = width;
23439
23440 /* By default, set up the blink-off state depending on the on-state. */
23441
23442 tem = Fassoc (arg, Vblink_cursor_alist);
23443 if (!NILP (tem))
23444 {
23445 FRAME_BLINK_OFF_CURSOR (f)
23446 = get_specified_cursor_type (XCDR (tem), &width);
23447 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23448 }
23449 else
23450 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23451 }
23452
23453
23454 #ifdef HAVE_WINDOW_SYSTEM
23455
23456 /* Return the cursor we want to be displayed in window W. Return
23457 width of bar/hbar cursor through WIDTH arg. Return with
23458 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23459 (i.e. if the `system caret' should track this cursor).
23460
23461 In a mini-buffer window, we want the cursor only to appear if we
23462 are reading input from this window. For the selected window, we
23463 want the cursor type given by the frame parameter or buffer local
23464 setting of cursor-type. If explicitly marked off, draw no cursor.
23465 In all other cases, we want a hollow box cursor. */
23466
23467 static enum text_cursor_kinds
23468 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23469 int *active_cursor)
23470 {
23471 struct frame *f = XFRAME (w->frame);
23472 struct buffer *b = XBUFFER (w->buffer);
23473 int cursor_type = DEFAULT_CURSOR;
23474 Lisp_Object alt_cursor;
23475 int non_selected = 0;
23476
23477 *active_cursor = 1;
23478
23479 /* Echo area */
23480 if (cursor_in_echo_area
23481 && FRAME_HAS_MINIBUF_P (f)
23482 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23483 {
23484 if (w == XWINDOW (echo_area_window))
23485 {
23486 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
23487 {
23488 *width = FRAME_CURSOR_WIDTH (f);
23489 return FRAME_DESIRED_CURSOR (f);
23490 }
23491 else
23492 return get_specified_cursor_type (BVAR (b, cursor_type), width);
23493 }
23494
23495 *active_cursor = 0;
23496 non_selected = 1;
23497 }
23498
23499 /* Detect a nonselected window or nonselected frame. */
23500 else if (w != XWINDOW (f->selected_window)
23501 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23502 {
23503 *active_cursor = 0;
23504
23505 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23506 return NO_CURSOR;
23507
23508 non_selected = 1;
23509 }
23510
23511 /* Never display a cursor in a window in which cursor-type is nil. */
23512 if (NILP (BVAR (b, cursor_type)))
23513 return NO_CURSOR;
23514
23515 /* Get the normal cursor type for this window. */
23516 if (EQ (BVAR (b, cursor_type), Qt))
23517 {
23518 cursor_type = FRAME_DESIRED_CURSOR (f);
23519 *width = FRAME_CURSOR_WIDTH (f);
23520 }
23521 else
23522 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
23523
23524 /* Use cursor-in-non-selected-windows instead
23525 for non-selected window or frame. */
23526 if (non_selected)
23527 {
23528 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
23529 if (!EQ (Qt, alt_cursor))
23530 return get_specified_cursor_type (alt_cursor, width);
23531 /* t means modify the normal cursor type. */
23532 if (cursor_type == FILLED_BOX_CURSOR)
23533 cursor_type = HOLLOW_BOX_CURSOR;
23534 else if (cursor_type == BAR_CURSOR && *width > 1)
23535 --*width;
23536 return cursor_type;
23537 }
23538
23539 /* Use normal cursor if not blinked off. */
23540 if (!w->cursor_off_p)
23541 {
23542 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23543 {
23544 if (cursor_type == FILLED_BOX_CURSOR)
23545 {
23546 /* Using a block cursor on large images can be very annoying.
23547 So use a hollow cursor for "large" images.
23548 If image is not transparent (no mask), also use hollow cursor. */
23549 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23550 if (img != NULL && IMAGEP (img->spec))
23551 {
23552 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23553 where N = size of default frame font size.
23554 This should cover most of the "tiny" icons people may use. */
23555 if (!img->mask
23556 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23557 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23558 cursor_type = HOLLOW_BOX_CURSOR;
23559 }
23560 }
23561 else if (cursor_type != NO_CURSOR)
23562 {
23563 /* Display current only supports BOX and HOLLOW cursors for images.
23564 So for now, unconditionally use a HOLLOW cursor when cursor is
23565 not a solid box cursor. */
23566 cursor_type = HOLLOW_BOX_CURSOR;
23567 }
23568 }
23569 return cursor_type;
23570 }
23571
23572 /* Cursor is blinked off, so determine how to "toggle" it. */
23573
23574 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23575 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
23576 return get_specified_cursor_type (XCDR (alt_cursor), width);
23577
23578 /* Then see if frame has specified a specific blink off cursor type. */
23579 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23580 {
23581 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23582 return FRAME_BLINK_OFF_CURSOR (f);
23583 }
23584
23585 #if 0
23586 /* Some people liked having a permanently visible blinking cursor,
23587 while others had very strong opinions against it. So it was
23588 decided to remove it. KFS 2003-09-03 */
23589
23590 /* Finally perform built-in cursor blinking:
23591 filled box <-> hollow box
23592 wide [h]bar <-> narrow [h]bar
23593 narrow [h]bar <-> no cursor
23594 other type <-> no cursor */
23595
23596 if (cursor_type == FILLED_BOX_CURSOR)
23597 return HOLLOW_BOX_CURSOR;
23598
23599 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23600 {
23601 *width = 1;
23602 return cursor_type;
23603 }
23604 #endif
23605
23606 return NO_CURSOR;
23607 }
23608
23609
23610 /* Notice when the text cursor of window W has been completely
23611 overwritten by a drawing operation that outputs glyphs in AREA
23612 starting at X0 and ending at X1 in the line starting at Y0 and
23613 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23614 the rest of the line after X0 has been written. Y coordinates
23615 are window-relative. */
23616
23617 static void
23618 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23619 int x0, int x1, int y0, int y1)
23620 {
23621 int cx0, cx1, cy0, cy1;
23622 struct glyph_row *row;
23623
23624 if (!w->phys_cursor_on_p)
23625 return;
23626 if (area != TEXT_AREA)
23627 return;
23628
23629 if (w->phys_cursor.vpos < 0
23630 || w->phys_cursor.vpos >= w->current_matrix->nrows
23631 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23632 !(row->enabled_p && row->displays_text_p)))
23633 return;
23634
23635 if (row->cursor_in_fringe_p)
23636 {
23637 row->cursor_in_fringe_p = 0;
23638 draw_fringe_bitmap (w, row, row->reversed_p);
23639 w->phys_cursor_on_p = 0;
23640 return;
23641 }
23642
23643 cx0 = w->phys_cursor.x;
23644 cx1 = cx0 + w->phys_cursor_width;
23645 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23646 return;
23647
23648 /* The cursor image will be completely removed from the
23649 screen if the output area intersects the cursor area in
23650 y-direction. When we draw in [y0 y1[, and some part of
23651 the cursor is at y < y0, that part must have been drawn
23652 before. When scrolling, the cursor is erased before
23653 actually scrolling, so we don't come here. When not
23654 scrolling, the rows above the old cursor row must have
23655 changed, and in this case these rows must have written
23656 over the cursor image.
23657
23658 Likewise if part of the cursor is below y1, with the
23659 exception of the cursor being in the first blank row at
23660 the buffer and window end because update_text_area
23661 doesn't draw that row. (Except when it does, but
23662 that's handled in update_text_area.) */
23663
23664 cy0 = w->phys_cursor.y;
23665 cy1 = cy0 + w->phys_cursor_height;
23666 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23667 return;
23668
23669 w->phys_cursor_on_p = 0;
23670 }
23671
23672 #endif /* HAVE_WINDOW_SYSTEM */
23673
23674 \f
23675 /************************************************************************
23676 Mouse Face
23677 ************************************************************************/
23678
23679 #ifdef HAVE_WINDOW_SYSTEM
23680
23681 /* EXPORT for RIF:
23682 Fix the display of area AREA of overlapping row ROW in window W
23683 with respect to the overlapping part OVERLAPS. */
23684
23685 void
23686 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23687 enum glyph_row_area area, int overlaps)
23688 {
23689 int i, x;
23690
23691 BLOCK_INPUT;
23692
23693 x = 0;
23694 for (i = 0; i < row->used[area];)
23695 {
23696 if (row->glyphs[area][i].overlaps_vertically_p)
23697 {
23698 int start = i, start_x = x;
23699
23700 do
23701 {
23702 x += row->glyphs[area][i].pixel_width;
23703 ++i;
23704 }
23705 while (i < row->used[area]
23706 && row->glyphs[area][i].overlaps_vertically_p);
23707
23708 draw_glyphs (w, start_x, row, area,
23709 start, i,
23710 DRAW_NORMAL_TEXT, overlaps);
23711 }
23712 else
23713 {
23714 x += row->glyphs[area][i].pixel_width;
23715 ++i;
23716 }
23717 }
23718
23719 UNBLOCK_INPUT;
23720 }
23721
23722
23723 /* EXPORT:
23724 Draw the cursor glyph of window W in glyph row ROW. See the
23725 comment of draw_glyphs for the meaning of HL. */
23726
23727 void
23728 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23729 enum draw_glyphs_face hl)
23730 {
23731 /* If cursor hpos is out of bounds, don't draw garbage. This can
23732 happen in mini-buffer windows when switching between echo area
23733 glyphs and mini-buffer. */
23734 if ((row->reversed_p
23735 ? (w->phys_cursor.hpos >= 0)
23736 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23737 {
23738 int on_p = w->phys_cursor_on_p;
23739 int x1;
23740 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23741 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23742 hl, 0);
23743 w->phys_cursor_on_p = on_p;
23744
23745 if (hl == DRAW_CURSOR)
23746 w->phys_cursor_width = x1 - w->phys_cursor.x;
23747 /* When we erase the cursor, and ROW is overlapped by other
23748 rows, make sure that these overlapping parts of other rows
23749 are redrawn. */
23750 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23751 {
23752 w->phys_cursor_width = x1 - w->phys_cursor.x;
23753
23754 if (row > w->current_matrix->rows
23755 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23756 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23757 OVERLAPS_ERASED_CURSOR);
23758
23759 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23760 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23761 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23762 OVERLAPS_ERASED_CURSOR);
23763 }
23764 }
23765 }
23766
23767
23768 /* EXPORT:
23769 Erase the image of a cursor of window W from the screen. */
23770
23771 void
23772 erase_phys_cursor (struct window *w)
23773 {
23774 struct frame *f = XFRAME (w->frame);
23775 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23776 int hpos = w->phys_cursor.hpos;
23777 int vpos = w->phys_cursor.vpos;
23778 int mouse_face_here_p = 0;
23779 struct glyph_matrix *active_glyphs = w->current_matrix;
23780 struct glyph_row *cursor_row;
23781 struct glyph *cursor_glyph;
23782 enum draw_glyphs_face hl;
23783
23784 /* No cursor displayed or row invalidated => nothing to do on the
23785 screen. */
23786 if (w->phys_cursor_type == NO_CURSOR)
23787 goto mark_cursor_off;
23788
23789 /* VPOS >= active_glyphs->nrows means that window has been resized.
23790 Don't bother to erase the cursor. */
23791 if (vpos >= active_glyphs->nrows)
23792 goto mark_cursor_off;
23793
23794 /* If row containing cursor is marked invalid, there is nothing we
23795 can do. */
23796 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23797 if (!cursor_row->enabled_p)
23798 goto mark_cursor_off;
23799
23800 /* If line spacing is > 0, old cursor may only be partially visible in
23801 window after split-window. So adjust visible height. */
23802 cursor_row->visible_height = min (cursor_row->visible_height,
23803 window_text_bottom_y (w) - cursor_row->y);
23804
23805 /* If row is completely invisible, don't attempt to delete a cursor which
23806 isn't there. This can happen if cursor is at top of a window, and
23807 we switch to a buffer with a header line in that window. */
23808 if (cursor_row->visible_height <= 0)
23809 goto mark_cursor_off;
23810
23811 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23812 if (cursor_row->cursor_in_fringe_p)
23813 {
23814 cursor_row->cursor_in_fringe_p = 0;
23815 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23816 goto mark_cursor_off;
23817 }
23818
23819 /* This can happen when the new row is shorter than the old one.
23820 In this case, either draw_glyphs or clear_end_of_line
23821 should have cleared the cursor. Note that we wouldn't be
23822 able to erase the cursor in this case because we don't have a
23823 cursor glyph at hand. */
23824 if ((cursor_row->reversed_p
23825 ? (w->phys_cursor.hpos < 0)
23826 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23827 goto mark_cursor_off;
23828
23829 /* If the cursor is in the mouse face area, redisplay that when
23830 we clear the cursor. */
23831 if (! NILP (hlinfo->mouse_face_window)
23832 && coords_in_mouse_face_p (w, hpos, vpos)
23833 /* Don't redraw the cursor's spot in mouse face if it is at the
23834 end of a line (on a newline). The cursor appears there, but
23835 mouse highlighting does not. */
23836 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23837 mouse_face_here_p = 1;
23838
23839 /* Maybe clear the display under the cursor. */
23840 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23841 {
23842 int x, y, left_x;
23843 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23844 int width;
23845
23846 cursor_glyph = get_phys_cursor_glyph (w);
23847 if (cursor_glyph == NULL)
23848 goto mark_cursor_off;
23849
23850 width = cursor_glyph->pixel_width;
23851 left_x = window_box_left_offset (w, TEXT_AREA);
23852 x = w->phys_cursor.x;
23853 if (x < left_x)
23854 width -= left_x - x;
23855 width = min (width, window_box_width (w, TEXT_AREA) - x);
23856 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23857 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23858
23859 if (width > 0)
23860 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23861 }
23862
23863 /* Erase the cursor by redrawing the character underneath it. */
23864 if (mouse_face_here_p)
23865 hl = DRAW_MOUSE_FACE;
23866 else
23867 hl = DRAW_NORMAL_TEXT;
23868 draw_phys_cursor_glyph (w, cursor_row, hl);
23869
23870 mark_cursor_off:
23871 w->phys_cursor_on_p = 0;
23872 w->phys_cursor_type = NO_CURSOR;
23873 }
23874
23875
23876 /* EXPORT:
23877 Display or clear cursor of window W. If ON is zero, clear the
23878 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23879 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23880
23881 void
23882 display_and_set_cursor (struct window *w, int on,
23883 int hpos, int vpos, int x, int y)
23884 {
23885 struct frame *f = XFRAME (w->frame);
23886 int new_cursor_type;
23887 int new_cursor_width;
23888 int active_cursor;
23889 struct glyph_row *glyph_row;
23890 struct glyph *glyph;
23891
23892 /* This is pointless on invisible frames, and dangerous on garbaged
23893 windows and frames; in the latter case, the frame or window may
23894 be in the midst of changing its size, and x and y may be off the
23895 window. */
23896 if (! FRAME_VISIBLE_P (f)
23897 || FRAME_GARBAGED_P (f)
23898 || vpos >= w->current_matrix->nrows
23899 || hpos >= w->current_matrix->matrix_w)
23900 return;
23901
23902 /* If cursor is off and we want it off, return quickly. */
23903 if (!on && !w->phys_cursor_on_p)
23904 return;
23905
23906 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23907 /* If cursor row is not enabled, we don't really know where to
23908 display the cursor. */
23909 if (!glyph_row->enabled_p)
23910 {
23911 w->phys_cursor_on_p = 0;
23912 return;
23913 }
23914
23915 glyph = NULL;
23916 if (!glyph_row->exact_window_width_line_p
23917 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23918 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23919
23920 xassert (interrupt_input_blocked);
23921
23922 /* Set new_cursor_type to the cursor we want to be displayed. */
23923 new_cursor_type = get_window_cursor_type (w, glyph,
23924 &new_cursor_width, &active_cursor);
23925
23926 /* If cursor is currently being shown and we don't want it to be or
23927 it is in the wrong place, or the cursor type is not what we want,
23928 erase it. */
23929 if (w->phys_cursor_on_p
23930 && (!on
23931 || w->phys_cursor.x != x
23932 || w->phys_cursor.y != y
23933 || new_cursor_type != w->phys_cursor_type
23934 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23935 && new_cursor_width != w->phys_cursor_width)))
23936 erase_phys_cursor (w);
23937
23938 /* Don't check phys_cursor_on_p here because that flag is only set
23939 to zero in some cases where we know that the cursor has been
23940 completely erased, to avoid the extra work of erasing the cursor
23941 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23942 still not be visible, or it has only been partly erased. */
23943 if (on)
23944 {
23945 w->phys_cursor_ascent = glyph_row->ascent;
23946 w->phys_cursor_height = glyph_row->height;
23947
23948 /* Set phys_cursor_.* before x_draw_.* is called because some
23949 of them may need the information. */
23950 w->phys_cursor.x = x;
23951 w->phys_cursor.y = glyph_row->y;
23952 w->phys_cursor.hpos = hpos;
23953 w->phys_cursor.vpos = vpos;
23954 }
23955
23956 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23957 new_cursor_type, new_cursor_width,
23958 on, active_cursor);
23959 }
23960
23961
23962 /* Switch the display of W's cursor on or off, according to the value
23963 of ON. */
23964
23965 static void
23966 update_window_cursor (struct window *w, int on)
23967 {
23968 /* Don't update cursor in windows whose frame is in the process
23969 of being deleted. */
23970 if (w->current_matrix)
23971 {
23972 BLOCK_INPUT;
23973 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23974 w->phys_cursor.x, w->phys_cursor.y);
23975 UNBLOCK_INPUT;
23976 }
23977 }
23978
23979
23980 /* Call update_window_cursor with parameter ON_P on all leaf windows
23981 in the window tree rooted at W. */
23982
23983 static void
23984 update_cursor_in_window_tree (struct window *w, int on_p)
23985 {
23986 while (w)
23987 {
23988 if (!NILP (w->hchild))
23989 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23990 else if (!NILP (w->vchild))
23991 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23992 else
23993 update_window_cursor (w, on_p);
23994
23995 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23996 }
23997 }
23998
23999
24000 /* EXPORT:
24001 Display the cursor on window W, or clear it, according to ON_P.
24002 Don't change the cursor's position. */
24003
24004 void
24005 x_update_cursor (struct frame *f, int on_p)
24006 {
24007 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24008 }
24009
24010
24011 /* EXPORT:
24012 Clear the cursor of window W to background color, and mark the
24013 cursor as not shown. This is used when the text where the cursor
24014 is about to be rewritten. */
24015
24016 void
24017 x_clear_cursor (struct window *w)
24018 {
24019 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24020 update_window_cursor (w, 0);
24021 }
24022
24023 #endif /* HAVE_WINDOW_SYSTEM */
24024
24025 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24026 and MSDOS. */
24027 static void
24028 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24029 int start_hpos, int end_hpos,
24030 enum draw_glyphs_face draw)
24031 {
24032 #ifdef HAVE_WINDOW_SYSTEM
24033 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24034 {
24035 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24036 return;
24037 }
24038 #endif
24039 #if defined (HAVE_GPM) || defined (MSDOS)
24040 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24041 #endif
24042 }
24043
24044 /* Display the active region described by mouse_face_* according to DRAW. */
24045
24046 static void
24047 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24048 {
24049 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24050 struct frame *f = XFRAME (WINDOW_FRAME (w));
24051
24052 if (/* If window is in the process of being destroyed, don't bother
24053 to do anything. */
24054 w->current_matrix != NULL
24055 /* Don't update mouse highlight if hidden */
24056 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24057 /* Recognize when we are called to operate on rows that don't exist
24058 anymore. This can happen when a window is split. */
24059 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24060 {
24061 int phys_cursor_on_p = w->phys_cursor_on_p;
24062 struct glyph_row *row, *first, *last;
24063
24064 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24065 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24066
24067 for (row = first; row <= last && row->enabled_p; ++row)
24068 {
24069 int start_hpos, end_hpos, start_x;
24070
24071 /* For all but the first row, the highlight starts at column 0. */
24072 if (row == first)
24073 {
24074 /* R2L rows have BEG and END in reversed order, but the
24075 screen drawing geometry is always left to right. So
24076 we need to mirror the beginning and end of the
24077 highlighted area in R2L rows. */
24078 if (!row->reversed_p)
24079 {
24080 start_hpos = hlinfo->mouse_face_beg_col;
24081 start_x = hlinfo->mouse_face_beg_x;
24082 }
24083 else if (row == last)
24084 {
24085 start_hpos = hlinfo->mouse_face_end_col;
24086 start_x = hlinfo->mouse_face_end_x;
24087 }
24088 else
24089 {
24090 start_hpos = 0;
24091 start_x = 0;
24092 }
24093 }
24094 else if (row->reversed_p && row == last)
24095 {
24096 start_hpos = hlinfo->mouse_face_end_col;
24097 start_x = hlinfo->mouse_face_end_x;
24098 }
24099 else
24100 {
24101 start_hpos = 0;
24102 start_x = 0;
24103 }
24104
24105 if (row == last)
24106 {
24107 if (!row->reversed_p)
24108 end_hpos = hlinfo->mouse_face_end_col;
24109 else if (row == first)
24110 end_hpos = hlinfo->mouse_face_beg_col;
24111 else
24112 {
24113 end_hpos = row->used[TEXT_AREA];
24114 if (draw == DRAW_NORMAL_TEXT)
24115 row->fill_line_p = 1; /* Clear to end of line */
24116 }
24117 }
24118 else if (row->reversed_p && row == first)
24119 end_hpos = hlinfo->mouse_face_beg_col;
24120 else
24121 {
24122 end_hpos = row->used[TEXT_AREA];
24123 if (draw == DRAW_NORMAL_TEXT)
24124 row->fill_line_p = 1; /* Clear to end of line */
24125 }
24126
24127 if (end_hpos > start_hpos)
24128 {
24129 draw_row_with_mouse_face (w, start_x, row,
24130 start_hpos, end_hpos, draw);
24131
24132 row->mouse_face_p
24133 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24134 }
24135 }
24136
24137 #ifdef HAVE_WINDOW_SYSTEM
24138 /* When we've written over the cursor, arrange for it to
24139 be displayed again. */
24140 if (FRAME_WINDOW_P (f)
24141 && phys_cursor_on_p && !w->phys_cursor_on_p)
24142 {
24143 BLOCK_INPUT;
24144 display_and_set_cursor (w, 1,
24145 w->phys_cursor.hpos, w->phys_cursor.vpos,
24146 w->phys_cursor.x, w->phys_cursor.y);
24147 UNBLOCK_INPUT;
24148 }
24149 #endif /* HAVE_WINDOW_SYSTEM */
24150 }
24151
24152 #ifdef HAVE_WINDOW_SYSTEM
24153 /* Change the mouse cursor. */
24154 if (FRAME_WINDOW_P (f))
24155 {
24156 if (draw == DRAW_NORMAL_TEXT
24157 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24158 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24159 else if (draw == DRAW_MOUSE_FACE)
24160 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24161 else
24162 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24163 }
24164 #endif /* HAVE_WINDOW_SYSTEM */
24165 }
24166
24167 /* EXPORT:
24168 Clear out the mouse-highlighted active region.
24169 Redraw it un-highlighted first. Value is non-zero if mouse
24170 face was actually drawn unhighlighted. */
24171
24172 int
24173 clear_mouse_face (Mouse_HLInfo *hlinfo)
24174 {
24175 int cleared = 0;
24176
24177 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24178 {
24179 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24180 cleared = 1;
24181 }
24182
24183 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24184 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24185 hlinfo->mouse_face_window = Qnil;
24186 hlinfo->mouse_face_overlay = Qnil;
24187 return cleared;
24188 }
24189
24190 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24191 within the mouse face on that window. */
24192 static int
24193 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24194 {
24195 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24196
24197 /* Quickly resolve the easy cases. */
24198 if (!(WINDOWP (hlinfo->mouse_face_window)
24199 && XWINDOW (hlinfo->mouse_face_window) == w))
24200 return 0;
24201 if (vpos < hlinfo->mouse_face_beg_row
24202 || vpos > hlinfo->mouse_face_end_row)
24203 return 0;
24204 if (vpos > hlinfo->mouse_face_beg_row
24205 && vpos < hlinfo->mouse_face_end_row)
24206 return 1;
24207
24208 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24209 {
24210 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24211 {
24212 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24213 return 1;
24214 }
24215 else if ((vpos == hlinfo->mouse_face_beg_row
24216 && hpos >= hlinfo->mouse_face_beg_col)
24217 || (vpos == hlinfo->mouse_face_end_row
24218 && hpos < hlinfo->mouse_face_end_col))
24219 return 1;
24220 }
24221 else
24222 {
24223 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24224 {
24225 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24226 return 1;
24227 }
24228 else if ((vpos == hlinfo->mouse_face_beg_row
24229 && hpos <= hlinfo->mouse_face_beg_col)
24230 || (vpos == hlinfo->mouse_face_end_row
24231 && hpos > hlinfo->mouse_face_end_col))
24232 return 1;
24233 }
24234 return 0;
24235 }
24236
24237
24238 /* EXPORT:
24239 Non-zero if physical cursor of window W is within mouse face. */
24240
24241 int
24242 cursor_in_mouse_face_p (struct window *w)
24243 {
24244 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24245 }
24246
24247
24248 \f
24249 /* Find the glyph rows START_ROW and END_ROW of window W that display
24250 characters between buffer positions START_CHARPOS and END_CHARPOS
24251 (excluding END_CHARPOS). This is similar to row_containing_pos,
24252 but is more accurate when bidi reordering makes buffer positions
24253 change non-linearly with glyph rows. */
24254 static void
24255 rows_from_pos_range (struct window *w,
24256 EMACS_INT start_charpos, EMACS_INT end_charpos,
24257 struct glyph_row **start, struct glyph_row **end)
24258 {
24259 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24260 int last_y = window_text_bottom_y (w);
24261 struct glyph_row *row;
24262
24263 *start = NULL;
24264 *end = NULL;
24265
24266 while (!first->enabled_p
24267 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24268 first++;
24269
24270 /* Find the START row. */
24271 for (row = first;
24272 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24273 row++)
24274 {
24275 /* A row can potentially be the START row if the range of the
24276 characters it displays intersects the range
24277 [START_CHARPOS..END_CHARPOS). */
24278 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24279 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24280 /* See the commentary in row_containing_pos, for the
24281 explanation of the complicated way to check whether
24282 some position is beyond the end of the characters
24283 displayed by a row. */
24284 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24285 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24286 && !row->ends_at_zv_p
24287 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24288 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24289 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24290 && !row->ends_at_zv_p
24291 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24292 {
24293 /* Found a candidate row. Now make sure at least one of the
24294 glyphs it displays has a charpos from the range
24295 [START_CHARPOS..END_CHARPOS).
24296
24297 This is not obvious because bidi reordering could make
24298 buffer positions of a row be 1,2,3,102,101,100, and if we
24299 want to highlight characters in [50..60), we don't want
24300 this row, even though [50..60) does intersect [1..103),
24301 the range of character positions given by the row's start
24302 and end positions. */
24303 struct glyph *g = row->glyphs[TEXT_AREA];
24304 struct glyph *e = g + row->used[TEXT_AREA];
24305
24306 while (g < e)
24307 {
24308 if (BUFFERP (g->object)
24309 && start_charpos <= g->charpos && g->charpos < end_charpos)
24310 *start = row;
24311 g++;
24312 }
24313 if (*start)
24314 break;
24315 }
24316 }
24317
24318 /* Find the END row. */
24319 if (!*start
24320 /* If the last row is partially visible, start looking for END
24321 from that row, instead of starting from FIRST. */
24322 && !(row->enabled_p
24323 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24324 row = first;
24325 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24326 {
24327 struct glyph_row *next = row + 1;
24328
24329 if (!next->enabled_p
24330 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24331 /* The first row >= START whose range of displayed characters
24332 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24333 is the row END + 1. */
24334 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24335 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24336 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24337 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24338 && !next->ends_at_zv_p
24339 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24340 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24341 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24342 && !next->ends_at_zv_p
24343 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24344 {
24345 *end = row;
24346 break;
24347 }
24348 else
24349 {
24350 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24351 but none of the characters it displays are in the range, it is
24352 also END + 1. */
24353 struct glyph *g = next->glyphs[TEXT_AREA];
24354 struct glyph *e = g + next->used[TEXT_AREA];
24355
24356 while (g < e)
24357 {
24358 if (BUFFERP (g->object)
24359 && start_charpos <= g->charpos && g->charpos < end_charpos)
24360 break;
24361 g++;
24362 }
24363 if (g == e)
24364 {
24365 *end = row;
24366 break;
24367 }
24368 }
24369 }
24370 }
24371
24372 /* This function sets the mouse_face_* elements of HLINFO, assuming
24373 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24374 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24375 for the overlay or run of text properties specifying the mouse
24376 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24377 before-string and after-string that must also be highlighted.
24378 COVER_STRING, if non-nil, is a display string that may cover some
24379 or all of the highlighted text. */
24380
24381 static void
24382 mouse_face_from_buffer_pos (Lisp_Object window,
24383 Mouse_HLInfo *hlinfo,
24384 EMACS_INT mouse_charpos,
24385 EMACS_INT start_charpos,
24386 EMACS_INT end_charpos,
24387 Lisp_Object before_string,
24388 Lisp_Object after_string,
24389 Lisp_Object cover_string)
24390 {
24391 struct window *w = XWINDOW (window);
24392 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24393 struct glyph_row *r1, *r2;
24394 struct glyph *glyph, *end;
24395 EMACS_INT ignore, pos;
24396 int x;
24397
24398 xassert (NILP (cover_string) || STRINGP (cover_string));
24399 xassert (NILP (before_string) || STRINGP (before_string));
24400 xassert (NILP (after_string) || STRINGP (after_string));
24401
24402 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24403 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24404 if (r1 == NULL)
24405 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24406 /* If the before-string or display-string contains newlines,
24407 rows_from_pos_range skips to its last row. Move back. */
24408 if (!NILP (before_string) || !NILP (cover_string))
24409 {
24410 struct glyph_row *prev;
24411 while ((prev = r1 - 1, prev >= first)
24412 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24413 && prev->used[TEXT_AREA] > 0)
24414 {
24415 struct glyph *beg = prev->glyphs[TEXT_AREA];
24416 glyph = beg + prev->used[TEXT_AREA];
24417 while (--glyph >= beg && INTEGERP (glyph->object));
24418 if (glyph < beg
24419 || !(EQ (glyph->object, before_string)
24420 || EQ (glyph->object, cover_string)))
24421 break;
24422 r1 = prev;
24423 }
24424 }
24425 if (r2 == NULL)
24426 {
24427 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24428 hlinfo->mouse_face_past_end = 1;
24429 }
24430 else if (!NILP (after_string))
24431 {
24432 /* If the after-string has newlines, advance to its last row. */
24433 struct glyph_row *next;
24434 struct glyph_row *last
24435 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24436
24437 for (next = r2 + 1;
24438 next <= last
24439 && next->used[TEXT_AREA] > 0
24440 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24441 ++next)
24442 r2 = next;
24443 }
24444 /* The rest of the display engine assumes that mouse_face_beg_row is
24445 either above below mouse_face_end_row or identical to it. But
24446 with bidi-reordered continued lines, the row for START_CHARPOS
24447 could be below the row for END_CHARPOS. If so, swap the rows and
24448 store them in correct order. */
24449 if (r1->y > r2->y)
24450 {
24451 struct glyph_row *tem = r2;
24452
24453 r2 = r1;
24454 r1 = tem;
24455 }
24456
24457 hlinfo->mouse_face_beg_y = r1->y;
24458 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24459 hlinfo->mouse_face_end_y = r2->y;
24460 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24461
24462 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24463 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
24464 could be anywhere in the row and in any order. The strategy
24465 below is to find the leftmost and the rightmost glyph that
24466 belongs to either of these 3 strings, or whose position is
24467 between START_CHARPOS and END_CHARPOS, and highlight all the
24468 glyphs between those two. This may cover more than just the text
24469 between START_CHARPOS and END_CHARPOS if the range of characters
24470 strides the bidi level boundary, e.g. if the beginning is in R2L
24471 text while the end is in L2R text or vice versa. */
24472 if (!r1->reversed_p)
24473 {
24474 /* This row is in a left to right paragraph. Scan it left to
24475 right. */
24476 glyph = r1->glyphs[TEXT_AREA];
24477 end = glyph + r1->used[TEXT_AREA];
24478 x = r1->x;
24479
24480 /* Skip truncation glyphs at the start of the glyph row. */
24481 if (r1->displays_text_p)
24482 for (; glyph < end
24483 && INTEGERP (glyph->object)
24484 && glyph->charpos < 0;
24485 ++glyph)
24486 x += glyph->pixel_width;
24487
24488 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24489 or COVER_STRING, and the first glyph from buffer whose
24490 position is between START_CHARPOS and END_CHARPOS. */
24491 for (; glyph < end
24492 && !INTEGERP (glyph->object)
24493 && !EQ (glyph->object, cover_string)
24494 && !(BUFFERP (glyph->object)
24495 && (glyph->charpos >= start_charpos
24496 && glyph->charpos < end_charpos));
24497 ++glyph)
24498 {
24499 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24500 are present at buffer positions between START_CHARPOS and
24501 END_CHARPOS, or if they come from an overlay. */
24502 if (EQ (glyph->object, before_string))
24503 {
24504 pos = string_buffer_position (before_string,
24505 start_charpos);
24506 /* If pos == 0, it means before_string came from an
24507 overlay, not from a buffer position. */
24508 if (!pos || (pos >= start_charpos && pos < end_charpos))
24509 break;
24510 }
24511 else if (EQ (glyph->object, after_string))
24512 {
24513 pos = string_buffer_position (after_string, end_charpos);
24514 if (!pos || (pos >= start_charpos && pos < end_charpos))
24515 break;
24516 }
24517 x += glyph->pixel_width;
24518 }
24519 hlinfo->mouse_face_beg_x = x;
24520 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24521 }
24522 else
24523 {
24524 /* This row is in a right to left paragraph. Scan it right to
24525 left. */
24526 struct glyph *g;
24527
24528 end = r1->glyphs[TEXT_AREA] - 1;
24529 glyph = end + r1->used[TEXT_AREA];
24530
24531 /* Skip truncation glyphs at the start of the glyph row. */
24532 if (r1->displays_text_p)
24533 for (; glyph > end
24534 && INTEGERP (glyph->object)
24535 && glyph->charpos < 0;
24536 --glyph)
24537 ;
24538
24539 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24540 or COVER_STRING, and the first glyph from buffer whose
24541 position is between START_CHARPOS and END_CHARPOS. */
24542 for (; glyph > end
24543 && !INTEGERP (glyph->object)
24544 && !EQ (glyph->object, cover_string)
24545 && !(BUFFERP (glyph->object)
24546 && (glyph->charpos >= start_charpos
24547 && glyph->charpos < end_charpos));
24548 --glyph)
24549 {
24550 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24551 are present at buffer positions between START_CHARPOS and
24552 END_CHARPOS, or if they come from an overlay. */
24553 if (EQ (glyph->object, before_string))
24554 {
24555 pos = string_buffer_position (before_string, start_charpos);
24556 /* If pos == 0, it means before_string came from an
24557 overlay, not from a buffer position. */
24558 if (!pos || (pos >= start_charpos && pos < end_charpos))
24559 break;
24560 }
24561 else if (EQ (glyph->object, after_string))
24562 {
24563 pos = string_buffer_position (after_string, end_charpos);
24564 if (!pos || (pos >= start_charpos && pos < end_charpos))
24565 break;
24566 }
24567 }
24568
24569 glyph++; /* first glyph to the right of the highlighted area */
24570 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24571 x += g->pixel_width;
24572 hlinfo->mouse_face_beg_x = x;
24573 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24574 }
24575
24576 /* If the highlight ends in a different row, compute GLYPH and END
24577 for the end row. Otherwise, reuse the values computed above for
24578 the row where the highlight begins. */
24579 if (r2 != r1)
24580 {
24581 if (!r2->reversed_p)
24582 {
24583 glyph = r2->glyphs[TEXT_AREA];
24584 end = glyph + r2->used[TEXT_AREA];
24585 x = r2->x;
24586 }
24587 else
24588 {
24589 end = r2->glyphs[TEXT_AREA] - 1;
24590 glyph = end + r2->used[TEXT_AREA];
24591 }
24592 }
24593
24594 if (!r2->reversed_p)
24595 {
24596 /* Skip truncation and continuation glyphs near the end of the
24597 row, and also blanks and stretch glyphs inserted by
24598 extend_face_to_end_of_line. */
24599 while (end > glyph
24600 && INTEGERP ((end - 1)->object)
24601 && (end - 1)->charpos <= 0)
24602 --end;
24603 /* Scan the rest of the glyph row from the end, looking for the
24604 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24605 COVER_STRING, or whose position is between START_CHARPOS
24606 and END_CHARPOS */
24607 for (--end;
24608 end > glyph
24609 && !INTEGERP (end->object)
24610 && !EQ (end->object, cover_string)
24611 && !(BUFFERP (end->object)
24612 && (end->charpos >= start_charpos
24613 && end->charpos < end_charpos));
24614 --end)
24615 {
24616 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24617 are present at buffer positions between START_CHARPOS and
24618 END_CHARPOS, or if they come from an overlay. */
24619 if (EQ (end->object, before_string))
24620 {
24621 pos = string_buffer_position (before_string, start_charpos);
24622 if (!pos || (pos >= start_charpos && pos < end_charpos))
24623 break;
24624 }
24625 else if (EQ (end->object, after_string))
24626 {
24627 pos = string_buffer_position (after_string, end_charpos);
24628 if (!pos || (pos >= start_charpos && pos < end_charpos))
24629 break;
24630 }
24631 }
24632 /* Find the X coordinate of the last glyph to be highlighted. */
24633 for (; glyph <= end; ++glyph)
24634 x += glyph->pixel_width;
24635
24636 hlinfo->mouse_face_end_x = x;
24637 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24638 }
24639 else
24640 {
24641 /* Skip truncation and continuation glyphs near the end of the
24642 row, and also blanks and stretch glyphs inserted by
24643 extend_face_to_end_of_line. */
24644 x = r2->x;
24645 end++;
24646 while (end < glyph
24647 && INTEGERP (end->object)
24648 && end->charpos <= 0)
24649 {
24650 x += end->pixel_width;
24651 ++end;
24652 }
24653 /* Scan the rest of the glyph row from the end, looking for the
24654 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24655 COVER_STRING, or whose position is between START_CHARPOS
24656 and END_CHARPOS */
24657 for ( ;
24658 end < glyph
24659 && !INTEGERP (end->object)
24660 && !EQ (end->object, cover_string)
24661 && !(BUFFERP (end->object)
24662 && (end->charpos >= start_charpos
24663 && end->charpos < end_charpos));
24664 ++end)
24665 {
24666 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24667 are present at buffer positions between START_CHARPOS and
24668 END_CHARPOS, or if they come from an overlay. */
24669 if (EQ (end->object, before_string))
24670 {
24671 pos = string_buffer_position (before_string, start_charpos);
24672 if (!pos || (pos >= start_charpos && pos < end_charpos))
24673 break;
24674 }
24675 else if (EQ (end->object, after_string))
24676 {
24677 pos = string_buffer_position (after_string, end_charpos);
24678 if (!pos || (pos >= start_charpos && pos < end_charpos))
24679 break;
24680 }
24681 x += end->pixel_width;
24682 }
24683 hlinfo->mouse_face_end_x = x;
24684 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24685 }
24686
24687 hlinfo->mouse_face_window = window;
24688 hlinfo->mouse_face_face_id
24689 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24690 mouse_charpos + 1,
24691 !hlinfo->mouse_face_hidden, -1);
24692 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24693 }
24694
24695 /* The following function is not used anymore (replaced with
24696 mouse_face_from_string_pos), but I leave it here for the time
24697 being, in case someone would. */
24698
24699 #if 0 /* not used */
24700
24701 /* Find the position of the glyph for position POS in OBJECT in
24702 window W's current matrix, and return in *X, *Y the pixel
24703 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24704
24705 RIGHT_P non-zero means return the position of the right edge of the
24706 glyph, RIGHT_P zero means return the left edge position.
24707
24708 If no glyph for POS exists in the matrix, return the position of
24709 the glyph with the next smaller position that is in the matrix, if
24710 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24711 exists in the matrix, return the position of the glyph with the
24712 next larger position in OBJECT.
24713
24714 Value is non-zero if a glyph was found. */
24715
24716 static int
24717 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24718 int *hpos, int *vpos, int *x, int *y, int right_p)
24719 {
24720 int yb = window_text_bottom_y (w);
24721 struct glyph_row *r;
24722 struct glyph *best_glyph = NULL;
24723 struct glyph_row *best_row = NULL;
24724 int best_x = 0;
24725
24726 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24727 r->enabled_p && r->y < yb;
24728 ++r)
24729 {
24730 struct glyph *g = r->glyphs[TEXT_AREA];
24731 struct glyph *e = g + r->used[TEXT_AREA];
24732 int gx;
24733
24734 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24735 if (EQ (g->object, object))
24736 {
24737 if (g->charpos == pos)
24738 {
24739 best_glyph = g;
24740 best_x = gx;
24741 best_row = r;
24742 goto found;
24743 }
24744 else if (best_glyph == NULL
24745 || ((eabs (g->charpos - pos)
24746 < eabs (best_glyph->charpos - pos))
24747 && (right_p
24748 ? g->charpos < pos
24749 : g->charpos > pos)))
24750 {
24751 best_glyph = g;
24752 best_x = gx;
24753 best_row = r;
24754 }
24755 }
24756 }
24757
24758 found:
24759
24760 if (best_glyph)
24761 {
24762 *x = best_x;
24763 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24764
24765 if (right_p)
24766 {
24767 *x += best_glyph->pixel_width;
24768 ++*hpos;
24769 }
24770
24771 *y = best_row->y;
24772 *vpos = best_row - w->current_matrix->rows;
24773 }
24774
24775 return best_glyph != NULL;
24776 }
24777 #endif /* not used */
24778
24779 /* Find the positions of the first and the last glyphs in window W's
24780 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24781 (assumed to be a string), and return in HLINFO's mouse_face_*
24782 members the pixel and column/row coordinates of those glyphs. */
24783
24784 static void
24785 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24786 Lisp_Object object,
24787 EMACS_INT startpos, EMACS_INT endpos)
24788 {
24789 int yb = window_text_bottom_y (w);
24790 struct glyph_row *r;
24791 struct glyph *g, *e;
24792 int gx;
24793 int found = 0;
24794
24795 /* Find the glyph row with at least one position in the range
24796 [STARTPOS..ENDPOS], and the first glyph in that row whose
24797 position belongs to that range. */
24798 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24799 r->enabled_p && r->y < yb;
24800 ++r)
24801 {
24802 if (!r->reversed_p)
24803 {
24804 g = r->glyphs[TEXT_AREA];
24805 e = g + r->used[TEXT_AREA];
24806 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24807 if (EQ (g->object, object)
24808 && startpos <= g->charpos && g->charpos <= endpos)
24809 {
24810 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24811 hlinfo->mouse_face_beg_y = r->y;
24812 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24813 hlinfo->mouse_face_beg_x = gx;
24814 found = 1;
24815 break;
24816 }
24817 }
24818 else
24819 {
24820 struct glyph *g1;
24821
24822 e = r->glyphs[TEXT_AREA];
24823 g = e + r->used[TEXT_AREA];
24824 for ( ; g > e; --g)
24825 if (EQ ((g-1)->object, object)
24826 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24827 {
24828 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24829 hlinfo->mouse_face_beg_y = r->y;
24830 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24831 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24832 gx += g1->pixel_width;
24833 hlinfo->mouse_face_beg_x = gx;
24834 found = 1;
24835 break;
24836 }
24837 }
24838 if (found)
24839 break;
24840 }
24841
24842 if (!found)
24843 return;
24844
24845 /* Starting with the next row, look for the first row which does NOT
24846 include any glyphs whose positions are in the range. */
24847 for (++r; r->enabled_p && r->y < yb; ++r)
24848 {
24849 g = r->glyphs[TEXT_AREA];
24850 e = g + r->used[TEXT_AREA];
24851 found = 0;
24852 for ( ; g < e; ++g)
24853 if (EQ (g->object, object)
24854 && startpos <= g->charpos && g->charpos <= endpos)
24855 {
24856 found = 1;
24857 break;
24858 }
24859 if (!found)
24860 break;
24861 }
24862
24863 /* The highlighted region ends on the previous row. */
24864 r--;
24865
24866 /* Set the end row and its vertical pixel coordinate. */
24867 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24868 hlinfo->mouse_face_end_y = r->y;
24869
24870 /* Compute and set the end column and the end column's horizontal
24871 pixel coordinate. */
24872 if (!r->reversed_p)
24873 {
24874 g = r->glyphs[TEXT_AREA];
24875 e = g + r->used[TEXT_AREA];
24876 for ( ; e > g; --e)
24877 if (EQ ((e-1)->object, object)
24878 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24879 break;
24880 hlinfo->mouse_face_end_col = e - g;
24881
24882 for (gx = r->x; g < e; ++g)
24883 gx += g->pixel_width;
24884 hlinfo->mouse_face_end_x = gx;
24885 }
24886 else
24887 {
24888 e = r->glyphs[TEXT_AREA];
24889 g = e + r->used[TEXT_AREA];
24890 for (gx = r->x ; e < g; ++e)
24891 {
24892 if (EQ (e->object, object)
24893 && startpos <= e->charpos && e->charpos <= endpos)
24894 break;
24895 gx += e->pixel_width;
24896 }
24897 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24898 hlinfo->mouse_face_end_x = gx;
24899 }
24900 }
24901
24902 #ifdef HAVE_WINDOW_SYSTEM
24903
24904 /* See if position X, Y is within a hot-spot of an image. */
24905
24906 static int
24907 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24908 {
24909 if (!CONSP (hot_spot))
24910 return 0;
24911
24912 if (EQ (XCAR (hot_spot), Qrect))
24913 {
24914 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24915 Lisp_Object rect = XCDR (hot_spot);
24916 Lisp_Object tem;
24917 if (!CONSP (rect))
24918 return 0;
24919 if (!CONSP (XCAR (rect)))
24920 return 0;
24921 if (!CONSP (XCDR (rect)))
24922 return 0;
24923 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24924 return 0;
24925 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24926 return 0;
24927 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24928 return 0;
24929 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24930 return 0;
24931 return 1;
24932 }
24933 else if (EQ (XCAR (hot_spot), Qcircle))
24934 {
24935 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24936 Lisp_Object circ = XCDR (hot_spot);
24937 Lisp_Object lr, lx0, ly0;
24938 if (CONSP (circ)
24939 && CONSP (XCAR (circ))
24940 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24941 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24942 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24943 {
24944 double r = XFLOATINT (lr);
24945 double dx = XINT (lx0) - x;
24946 double dy = XINT (ly0) - y;
24947 return (dx * dx + dy * dy <= r * r);
24948 }
24949 }
24950 else if (EQ (XCAR (hot_spot), Qpoly))
24951 {
24952 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24953 if (VECTORP (XCDR (hot_spot)))
24954 {
24955 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24956 Lisp_Object *poly = v->contents;
24957 int n = v->header.size;
24958 int i;
24959 int inside = 0;
24960 Lisp_Object lx, ly;
24961 int x0, y0;
24962
24963 /* Need an even number of coordinates, and at least 3 edges. */
24964 if (n < 6 || n & 1)
24965 return 0;
24966
24967 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24968 If count is odd, we are inside polygon. Pixels on edges
24969 may or may not be included depending on actual geometry of the
24970 polygon. */
24971 if ((lx = poly[n-2], !INTEGERP (lx))
24972 || (ly = poly[n-1], !INTEGERP (lx)))
24973 return 0;
24974 x0 = XINT (lx), y0 = XINT (ly);
24975 for (i = 0; i < n; i += 2)
24976 {
24977 int x1 = x0, y1 = y0;
24978 if ((lx = poly[i], !INTEGERP (lx))
24979 || (ly = poly[i+1], !INTEGERP (ly)))
24980 return 0;
24981 x0 = XINT (lx), y0 = XINT (ly);
24982
24983 /* Does this segment cross the X line? */
24984 if (x0 >= x)
24985 {
24986 if (x1 >= x)
24987 continue;
24988 }
24989 else if (x1 < x)
24990 continue;
24991 if (y > y0 && y > y1)
24992 continue;
24993 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24994 inside = !inside;
24995 }
24996 return inside;
24997 }
24998 }
24999 return 0;
25000 }
25001
25002 Lisp_Object
25003 find_hot_spot (Lisp_Object map, int x, int y)
25004 {
25005 while (CONSP (map))
25006 {
25007 if (CONSP (XCAR (map))
25008 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25009 return XCAR (map);
25010 map = XCDR (map);
25011 }
25012
25013 return Qnil;
25014 }
25015
25016 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25017 3, 3, 0,
25018 doc: /* Lookup in image map MAP coordinates X and Y.
25019 An image map is an alist where each element has the format (AREA ID PLIST).
25020 An AREA is specified as either a rectangle, a circle, or a polygon:
25021 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25022 pixel coordinates of the upper left and bottom right corners.
25023 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25024 and the radius of the circle; r may be a float or integer.
25025 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25026 vector describes one corner in the polygon.
25027 Returns the alist element for the first matching AREA in MAP. */)
25028 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25029 {
25030 if (NILP (map))
25031 return Qnil;
25032
25033 CHECK_NUMBER (x);
25034 CHECK_NUMBER (y);
25035
25036 return find_hot_spot (map, XINT (x), XINT (y));
25037 }
25038
25039
25040 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25041 static void
25042 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25043 {
25044 /* Do not change cursor shape while dragging mouse. */
25045 if (!NILP (do_mouse_tracking))
25046 return;
25047
25048 if (!NILP (pointer))
25049 {
25050 if (EQ (pointer, Qarrow))
25051 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25052 else if (EQ (pointer, Qhand))
25053 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25054 else if (EQ (pointer, Qtext))
25055 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25056 else if (EQ (pointer, intern ("hdrag")))
25057 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25058 #ifdef HAVE_X_WINDOWS
25059 else if (EQ (pointer, intern ("vdrag")))
25060 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25061 #endif
25062 else if (EQ (pointer, intern ("hourglass")))
25063 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25064 else if (EQ (pointer, Qmodeline))
25065 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25066 else
25067 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25068 }
25069
25070 if (cursor != No_Cursor)
25071 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25072 }
25073
25074 #endif /* HAVE_WINDOW_SYSTEM */
25075
25076 /* Take proper action when mouse has moved to the mode or header line
25077 or marginal area AREA of window W, x-position X and y-position Y.
25078 X is relative to the start of the text display area of W, so the
25079 width of bitmap areas and scroll bars must be subtracted to get a
25080 position relative to the start of the mode line. */
25081
25082 static void
25083 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25084 enum window_part area)
25085 {
25086 struct window *w = XWINDOW (window);
25087 struct frame *f = XFRAME (w->frame);
25088 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25089 #ifdef HAVE_WINDOW_SYSTEM
25090 Display_Info *dpyinfo;
25091 #endif
25092 Cursor cursor = No_Cursor;
25093 Lisp_Object pointer = Qnil;
25094 int dx, dy, width, height;
25095 EMACS_INT charpos;
25096 Lisp_Object string, object = Qnil;
25097 Lisp_Object pos, help;
25098
25099 Lisp_Object mouse_face;
25100 int original_x_pixel = x;
25101 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25102 struct glyph_row *row;
25103
25104 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25105 {
25106 int x0;
25107 struct glyph *end;
25108
25109 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25110 returns them in row/column units! */
25111 string = mode_line_string (w, area, &x, &y, &charpos,
25112 &object, &dx, &dy, &width, &height);
25113
25114 row = (area == ON_MODE_LINE
25115 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25116 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25117
25118 /* Find the glyph under the mouse pointer. */
25119 if (row->mode_line_p && row->enabled_p)
25120 {
25121 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25122 end = glyph + row->used[TEXT_AREA];
25123
25124 for (x0 = original_x_pixel;
25125 glyph < end && x0 >= glyph->pixel_width;
25126 ++glyph)
25127 x0 -= glyph->pixel_width;
25128
25129 if (glyph >= end)
25130 glyph = NULL;
25131 }
25132 }
25133 else
25134 {
25135 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25136 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25137 returns them in row/column units! */
25138 string = marginal_area_string (w, area, &x, &y, &charpos,
25139 &object, &dx, &dy, &width, &height);
25140 }
25141
25142 help = Qnil;
25143
25144 #ifdef HAVE_WINDOW_SYSTEM
25145 if (IMAGEP (object))
25146 {
25147 Lisp_Object image_map, hotspot;
25148 if ((image_map = Fplist_get (XCDR (object), QCmap),
25149 !NILP (image_map))
25150 && (hotspot = find_hot_spot (image_map, dx, dy),
25151 CONSP (hotspot))
25152 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25153 {
25154 Lisp_Object plist;
25155
25156 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25157 If so, we could look for mouse-enter, mouse-leave
25158 properties in PLIST (and do something...). */
25159 hotspot = XCDR (hotspot);
25160 if (CONSP (hotspot)
25161 && (plist = XCAR (hotspot), CONSP (plist)))
25162 {
25163 pointer = Fplist_get (plist, Qpointer);
25164 if (NILP (pointer))
25165 pointer = Qhand;
25166 help = Fplist_get (plist, Qhelp_echo);
25167 if (!NILP (help))
25168 {
25169 help_echo_string = help;
25170 /* Is this correct? ++kfs */
25171 XSETWINDOW (help_echo_window, w);
25172 help_echo_object = w->buffer;
25173 help_echo_pos = charpos;
25174 }
25175 }
25176 }
25177 if (NILP (pointer))
25178 pointer = Fplist_get (XCDR (object), QCpointer);
25179 }
25180 #endif /* HAVE_WINDOW_SYSTEM */
25181
25182 if (STRINGP (string))
25183 {
25184 pos = make_number (charpos);
25185 /* If we're on a string with `help-echo' text property, arrange
25186 for the help to be displayed. This is done by setting the
25187 global variable help_echo_string to the help string. */
25188 if (NILP (help))
25189 {
25190 help = Fget_text_property (pos, Qhelp_echo, string);
25191 if (!NILP (help))
25192 {
25193 help_echo_string = help;
25194 XSETWINDOW (help_echo_window, w);
25195 help_echo_object = string;
25196 help_echo_pos = charpos;
25197 }
25198 }
25199
25200 #ifdef HAVE_WINDOW_SYSTEM
25201 if (FRAME_WINDOW_P (f))
25202 {
25203 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25204 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25205 if (NILP (pointer))
25206 pointer = Fget_text_property (pos, Qpointer, string);
25207
25208 /* Change the mouse pointer according to what is under X/Y. */
25209 if (NILP (pointer)
25210 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25211 {
25212 Lisp_Object map;
25213 map = Fget_text_property (pos, Qlocal_map, string);
25214 if (!KEYMAPP (map))
25215 map = Fget_text_property (pos, Qkeymap, string);
25216 if (!KEYMAPP (map))
25217 cursor = dpyinfo->vertical_scroll_bar_cursor;
25218 }
25219 }
25220 #endif
25221
25222 /* Change the mouse face according to what is under X/Y. */
25223 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25224 if (!NILP (mouse_face)
25225 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25226 && glyph)
25227 {
25228 Lisp_Object b, e;
25229
25230 struct glyph * tmp_glyph;
25231
25232 int gpos;
25233 int gseq_length;
25234 int total_pixel_width;
25235 EMACS_INT begpos, endpos, ignore;
25236
25237 int vpos, hpos;
25238
25239 b = Fprevious_single_property_change (make_number (charpos + 1),
25240 Qmouse_face, string, Qnil);
25241 if (NILP (b))
25242 begpos = 0;
25243 else
25244 begpos = XINT (b);
25245
25246 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25247 if (NILP (e))
25248 endpos = SCHARS (string);
25249 else
25250 endpos = XINT (e);
25251
25252 /* Calculate the glyph position GPOS of GLYPH in the
25253 displayed string, relative to the beginning of the
25254 highlighted part of the string.
25255
25256 Note: GPOS is different from CHARPOS. CHARPOS is the
25257 position of GLYPH in the internal string object. A mode
25258 line string format has structures which are converted to
25259 a flattened string by the Emacs Lisp interpreter. The
25260 internal string is an element of those structures. The
25261 displayed string is the flattened string. */
25262 tmp_glyph = row_start_glyph;
25263 while (tmp_glyph < glyph
25264 && (!(EQ (tmp_glyph->object, glyph->object)
25265 && begpos <= tmp_glyph->charpos
25266 && tmp_glyph->charpos < endpos)))
25267 tmp_glyph++;
25268 gpos = glyph - tmp_glyph;
25269
25270 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25271 the highlighted part of the displayed string to which
25272 GLYPH belongs. Note: GSEQ_LENGTH is different from
25273 SCHARS (STRING), because the latter returns the length of
25274 the internal string. */
25275 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25276 tmp_glyph > glyph
25277 && (!(EQ (tmp_glyph->object, glyph->object)
25278 && begpos <= tmp_glyph->charpos
25279 && tmp_glyph->charpos < endpos));
25280 tmp_glyph--)
25281 ;
25282 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25283
25284 /* Calculate the total pixel width of all the glyphs between
25285 the beginning of the highlighted area and GLYPH. */
25286 total_pixel_width = 0;
25287 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25288 total_pixel_width += tmp_glyph->pixel_width;
25289
25290 /* Pre calculation of re-rendering position. Note: X is in
25291 column units here, after the call to mode_line_string or
25292 marginal_area_string. */
25293 hpos = x - gpos;
25294 vpos = (area == ON_MODE_LINE
25295 ? (w->current_matrix)->nrows - 1
25296 : 0);
25297
25298 /* If GLYPH's position is included in the region that is
25299 already drawn in mouse face, we have nothing to do. */
25300 if ( EQ (window, hlinfo->mouse_face_window)
25301 && (!row->reversed_p
25302 ? (hlinfo->mouse_face_beg_col <= hpos
25303 && hpos < hlinfo->mouse_face_end_col)
25304 /* In R2L rows we swap BEG and END, see below. */
25305 : (hlinfo->mouse_face_end_col <= hpos
25306 && hpos < hlinfo->mouse_face_beg_col))
25307 && hlinfo->mouse_face_beg_row == vpos )
25308 return;
25309
25310 if (clear_mouse_face (hlinfo))
25311 cursor = No_Cursor;
25312
25313 if (!row->reversed_p)
25314 {
25315 hlinfo->mouse_face_beg_col = hpos;
25316 hlinfo->mouse_face_beg_x = original_x_pixel
25317 - (total_pixel_width + dx);
25318 hlinfo->mouse_face_end_col = hpos + gseq_length;
25319 hlinfo->mouse_face_end_x = 0;
25320 }
25321 else
25322 {
25323 /* In R2L rows, show_mouse_face expects BEG and END
25324 coordinates to be swapped. */
25325 hlinfo->mouse_face_end_col = hpos;
25326 hlinfo->mouse_face_end_x = original_x_pixel
25327 - (total_pixel_width + dx);
25328 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25329 hlinfo->mouse_face_beg_x = 0;
25330 }
25331
25332 hlinfo->mouse_face_beg_row = vpos;
25333 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25334 hlinfo->mouse_face_beg_y = 0;
25335 hlinfo->mouse_face_end_y = 0;
25336 hlinfo->mouse_face_past_end = 0;
25337 hlinfo->mouse_face_window = window;
25338
25339 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25340 charpos,
25341 0, 0, 0,
25342 &ignore,
25343 glyph->face_id,
25344 1);
25345 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25346
25347 if (NILP (pointer))
25348 pointer = Qhand;
25349 }
25350 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25351 clear_mouse_face (hlinfo);
25352 }
25353 #ifdef HAVE_WINDOW_SYSTEM
25354 if (FRAME_WINDOW_P (f))
25355 define_frame_cursor1 (f, cursor, pointer);
25356 #endif
25357 }
25358
25359
25360 /* EXPORT:
25361 Take proper action when the mouse has moved to position X, Y on
25362 frame F as regards highlighting characters that have mouse-face
25363 properties. Also de-highlighting chars where the mouse was before.
25364 X and Y can be negative or out of range. */
25365
25366 void
25367 note_mouse_highlight (struct frame *f, int x, int y)
25368 {
25369 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25370 enum window_part part;
25371 Lisp_Object window;
25372 struct window *w;
25373 Cursor cursor = No_Cursor;
25374 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25375 struct buffer *b;
25376
25377 /* When a menu is active, don't highlight because this looks odd. */
25378 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25379 if (popup_activated ())
25380 return;
25381 #endif
25382
25383 if (NILP (Vmouse_highlight)
25384 || !f->glyphs_initialized_p
25385 || f->pointer_invisible)
25386 return;
25387
25388 hlinfo->mouse_face_mouse_x = x;
25389 hlinfo->mouse_face_mouse_y = y;
25390 hlinfo->mouse_face_mouse_frame = f;
25391
25392 if (hlinfo->mouse_face_defer)
25393 return;
25394
25395 if (gc_in_progress)
25396 {
25397 hlinfo->mouse_face_deferred_gc = 1;
25398 return;
25399 }
25400
25401 /* Which window is that in? */
25402 window = window_from_coordinates (f, x, y, &part, 1);
25403
25404 /* If we were displaying active text in another window, clear that.
25405 Also clear if we move out of text area in same window. */
25406 if (! EQ (window, hlinfo->mouse_face_window)
25407 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25408 && !NILP (hlinfo->mouse_face_window)))
25409 clear_mouse_face (hlinfo);
25410
25411 /* Not on a window -> return. */
25412 if (!WINDOWP (window))
25413 return;
25414
25415 /* Reset help_echo_string. It will get recomputed below. */
25416 help_echo_string = Qnil;
25417
25418 /* Convert to window-relative pixel coordinates. */
25419 w = XWINDOW (window);
25420 frame_to_window_pixel_xy (w, &x, &y);
25421
25422 #ifdef HAVE_WINDOW_SYSTEM
25423 /* Handle tool-bar window differently since it doesn't display a
25424 buffer. */
25425 if (EQ (window, f->tool_bar_window))
25426 {
25427 note_tool_bar_highlight (f, x, y);
25428 return;
25429 }
25430 #endif
25431
25432 /* Mouse is on the mode, header line or margin? */
25433 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25434 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25435 {
25436 note_mode_line_or_margin_highlight (window, x, y, part);
25437 return;
25438 }
25439
25440 #ifdef HAVE_WINDOW_SYSTEM
25441 if (part == ON_VERTICAL_BORDER)
25442 {
25443 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25444 help_echo_string = build_string ("drag-mouse-1: resize");
25445 }
25446 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25447 || part == ON_SCROLL_BAR)
25448 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25449 else
25450 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25451 #endif
25452
25453 /* Are we in a window whose display is up to date?
25454 And verify the buffer's text has not changed. */
25455 b = XBUFFER (w->buffer);
25456 if (part == ON_TEXT
25457 && EQ (w->window_end_valid, w->buffer)
25458 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25459 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25460 {
25461 int hpos, vpos, dx, dy, area;
25462 EMACS_INT pos;
25463 struct glyph *glyph;
25464 Lisp_Object object;
25465 Lisp_Object mouse_face = Qnil, position;
25466 Lisp_Object *overlay_vec = NULL;
25467 ptrdiff_t i, noverlays;
25468 struct buffer *obuf;
25469 EMACS_INT obegv, ozv;
25470 int same_region;
25471
25472 /* Find the glyph under X/Y. */
25473 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25474
25475 #ifdef HAVE_WINDOW_SYSTEM
25476 /* Look for :pointer property on image. */
25477 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25478 {
25479 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25480 if (img != NULL && IMAGEP (img->spec))
25481 {
25482 Lisp_Object image_map, hotspot;
25483 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25484 !NILP (image_map))
25485 && (hotspot = find_hot_spot (image_map,
25486 glyph->slice.img.x + dx,
25487 glyph->slice.img.y + dy),
25488 CONSP (hotspot))
25489 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25490 {
25491 Lisp_Object plist;
25492
25493 /* Could check XCAR (hotspot) to see if we enter/leave
25494 this hot-spot.
25495 If so, we could look for mouse-enter, mouse-leave
25496 properties in PLIST (and do something...). */
25497 hotspot = XCDR (hotspot);
25498 if (CONSP (hotspot)
25499 && (plist = XCAR (hotspot), CONSP (plist)))
25500 {
25501 pointer = Fplist_get (plist, Qpointer);
25502 if (NILP (pointer))
25503 pointer = Qhand;
25504 help_echo_string = Fplist_get (plist, Qhelp_echo);
25505 if (!NILP (help_echo_string))
25506 {
25507 help_echo_window = window;
25508 help_echo_object = glyph->object;
25509 help_echo_pos = glyph->charpos;
25510 }
25511 }
25512 }
25513 if (NILP (pointer))
25514 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25515 }
25516 }
25517 #endif /* HAVE_WINDOW_SYSTEM */
25518
25519 /* Clear mouse face if X/Y not over text. */
25520 if (glyph == NULL
25521 || area != TEXT_AREA
25522 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25523 /* Glyph's OBJECT is an integer for glyphs inserted by the
25524 display engine for its internal purposes, like truncation
25525 and continuation glyphs and blanks beyond the end of
25526 line's text on text terminals. If we are over such a
25527 glyph, we are not over any text. */
25528 || INTEGERP (glyph->object)
25529 /* R2L rows have a stretch glyph at their front, which
25530 stands for no text, whereas L2R rows have no glyphs at
25531 all beyond the end of text. Treat such stretch glyphs
25532 like we do with NULL glyphs in L2R rows. */
25533 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25534 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25535 && glyph->type == STRETCH_GLYPH
25536 && glyph->avoid_cursor_p))
25537 {
25538 if (clear_mouse_face (hlinfo))
25539 cursor = No_Cursor;
25540 #ifdef HAVE_WINDOW_SYSTEM
25541 if (FRAME_WINDOW_P (f) && NILP (pointer))
25542 {
25543 if (area != TEXT_AREA)
25544 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25545 else
25546 pointer = Vvoid_text_area_pointer;
25547 }
25548 #endif
25549 goto set_cursor;
25550 }
25551
25552 pos = glyph->charpos;
25553 object = glyph->object;
25554 if (!STRINGP (object) && !BUFFERP (object))
25555 goto set_cursor;
25556
25557 /* If we get an out-of-range value, return now; avoid an error. */
25558 if (BUFFERP (object) && pos > BUF_Z (b))
25559 goto set_cursor;
25560
25561 /* Make the window's buffer temporarily current for
25562 overlays_at and compute_char_face. */
25563 obuf = current_buffer;
25564 current_buffer = b;
25565 obegv = BEGV;
25566 ozv = ZV;
25567 BEGV = BEG;
25568 ZV = Z;
25569
25570 /* Is this char mouse-active or does it have help-echo? */
25571 position = make_number (pos);
25572
25573 if (BUFFERP (object))
25574 {
25575 /* Put all the overlays we want in a vector in overlay_vec. */
25576 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25577 /* Sort overlays into increasing priority order. */
25578 noverlays = sort_overlays (overlay_vec, noverlays, w);
25579 }
25580 else
25581 noverlays = 0;
25582
25583 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25584
25585 if (same_region)
25586 cursor = No_Cursor;
25587
25588 /* Check mouse-face highlighting. */
25589 if (! same_region
25590 /* If there exists an overlay with mouse-face overlapping
25591 the one we are currently highlighting, we have to
25592 check if we enter the overlapping overlay, and then
25593 highlight only that. */
25594 || (OVERLAYP (hlinfo->mouse_face_overlay)
25595 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25596 {
25597 /* Find the highest priority overlay with a mouse-face. */
25598 Lisp_Object overlay = Qnil;
25599 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25600 {
25601 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25602 if (!NILP (mouse_face))
25603 overlay = overlay_vec[i];
25604 }
25605
25606 /* If we're highlighting the same overlay as before, there's
25607 no need to do that again. */
25608 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25609 goto check_help_echo;
25610 hlinfo->mouse_face_overlay = overlay;
25611
25612 /* Clear the display of the old active region, if any. */
25613 if (clear_mouse_face (hlinfo))
25614 cursor = No_Cursor;
25615
25616 /* If no overlay applies, get a text property. */
25617 if (NILP (overlay))
25618 mouse_face = Fget_text_property (position, Qmouse_face, object);
25619
25620 /* Next, compute the bounds of the mouse highlighting and
25621 display it. */
25622 if (!NILP (mouse_face) && STRINGP (object))
25623 {
25624 /* The mouse-highlighting comes from a display string
25625 with a mouse-face. */
25626 Lisp_Object s, e;
25627 EMACS_INT ignore;
25628
25629 s = Fprevious_single_property_change
25630 (make_number (pos + 1), Qmouse_face, object, Qnil);
25631 e = Fnext_single_property_change
25632 (position, Qmouse_face, object, Qnil);
25633 if (NILP (s))
25634 s = make_number (0);
25635 if (NILP (e))
25636 e = make_number (SCHARS (object) - 1);
25637 mouse_face_from_string_pos (w, hlinfo, object,
25638 XINT (s), XINT (e));
25639 hlinfo->mouse_face_past_end = 0;
25640 hlinfo->mouse_face_window = window;
25641 hlinfo->mouse_face_face_id
25642 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25643 glyph->face_id, 1);
25644 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25645 cursor = No_Cursor;
25646 }
25647 else
25648 {
25649 /* The mouse-highlighting, if any, comes from an overlay
25650 or text property in the buffer. */
25651 Lisp_Object buffer IF_LINT (= Qnil);
25652 Lisp_Object cover_string IF_LINT (= Qnil);
25653
25654 if (STRINGP (object))
25655 {
25656 /* If we are on a display string with no mouse-face,
25657 check if the text under it has one. */
25658 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25659 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25660 pos = string_buffer_position (object, start);
25661 if (pos > 0)
25662 {
25663 mouse_face = get_char_property_and_overlay
25664 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25665 buffer = w->buffer;
25666 cover_string = object;
25667 }
25668 }
25669 else
25670 {
25671 buffer = object;
25672 cover_string = Qnil;
25673 }
25674
25675 if (!NILP (mouse_face))
25676 {
25677 Lisp_Object before, after;
25678 Lisp_Object before_string, after_string;
25679 /* To correctly find the limits of mouse highlight
25680 in a bidi-reordered buffer, we must not use the
25681 optimization of limiting the search in
25682 previous-single-property-change and
25683 next-single-property-change, because
25684 rows_from_pos_range needs the real start and end
25685 positions to DTRT in this case. That's because
25686 the first row visible in a window does not
25687 necessarily display the character whose position
25688 is the smallest. */
25689 Lisp_Object lim1 =
25690 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
25691 ? Fmarker_position (w->start)
25692 : Qnil;
25693 Lisp_Object lim2 =
25694 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
25695 ? make_number (BUF_Z (XBUFFER (buffer))
25696 - XFASTINT (w->window_end_pos))
25697 : Qnil;
25698
25699 if (NILP (overlay))
25700 {
25701 /* Handle the text property case. */
25702 before = Fprevious_single_property_change
25703 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25704 after = Fnext_single_property_change
25705 (make_number (pos), Qmouse_face, buffer, lim2);
25706 before_string = after_string = Qnil;
25707 }
25708 else
25709 {
25710 /* Handle the overlay case. */
25711 before = Foverlay_start (overlay);
25712 after = Foverlay_end (overlay);
25713 before_string = Foverlay_get (overlay, Qbefore_string);
25714 after_string = Foverlay_get (overlay, Qafter_string);
25715
25716 if (!STRINGP (before_string)) before_string = Qnil;
25717 if (!STRINGP (after_string)) after_string = Qnil;
25718 }
25719
25720 mouse_face_from_buffer_pos (window, hlinfo, pos,
25721 XFASTINT (before),
25722 XFASTINT (after),
25723 before_string, after_string,
25724 cover_string);
25725 cursor = No_Cursor;
25726 }
25727 }
25728 }
25729
25730 check_help_echo:
25731
25732 /* Look for a `help-echo' property. */
25733 if (NILP (help_echo_string)) {
25734 Lisp_Object help, overlay;
25735
25736 /* Check overlays first. */
25737 help = overlay = Qnil;
25738 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25739 {
25740 overlay = overlay_vec[i];
25741 help = Foverlay_get (overlay, Qhelp_echo);
25742 }
25743
25744 if (!NILP (help))
25745 {
25746 help_echo_string = help;
25747 help_echo_window = window;
25748 help_echo_object = overlay;
25749 help_echo_pos = pos;
25750 }
25751 else
25752 {
25753 Lisp_Object obj = glyph->object;
25754 EMACS_INT charpos = glyph->charpos;
25755
25756 /* Try text properties. */
25757 if (STRINGP (obj)
25758 && charpos >= 0
25759 && charpos < SCHARS (obj))
25760 {
25761 help = Fget_text_property (make_number (charpos),
25762 Qhelp_echo, obj);
25763 if (NILP (help))
25764 {
25765 /* If the string itself doesn't specify a help-echo,
25766 see if the buffer text ``under'' it does. */
25767 struct glyph_row *r
25768 = MATRIX_ROW (w->current_matrix, vpos);
25769 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25770 EMACS_INT p = string_buffer_position (obj, start);
25771 if (p > 0)
25772 {
25773 help = Fget_char_property (make_number (p),
25774 Qhelp_echo, w->buffer);
25775 if (!NILP (help))
25776 {
25777 charpos = p;
25778 obj = w->buffer;
25779 }
25780 }
25781 }
25782 }
25783 else if (BUFFERP (obj)
25784 && charpos >= BEGV
25785 && charpos < ZV)
25786 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25787 obj);
25788
25789 if (!NILP (help))
25790 {
25791 help_echo_string = help;
25792 help_echo_window = window;
25793 help_echo_object = obj;
25794 help_echo_pos = charpos;
25795 }
25796 }
25797 }
25798
25799 #ifdef HAVE_WINDOW_SYSTEM
25800 /* Look for a `pointer' property. */
25801 if (FRAME_WINDOW_P (f) && NILP (pointer))
25802 {
25803 /* Check overlays first. */
25804 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25805 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25806
25807 if (NILP (pointer))
25808 {
25809 Lisp_Object obj = glyph->object;
25810 EMACS_INT charpos = glyph->charpos;
25811
25812 /* Try text properties. */
25813 if (STRINGP (obj)
25814 && charpos >= 0
25815 && charpos < SCHARS (obj))
25816 {
25817 pointer = Fget_text_property (make_number (charpos),
25818 Qpointer, obj);
25819 if (NILP (pointer))
25820 {
25821 /* If the string itself doesn't specify a pointer,
25822 see if the buffer text ``under'' it does. */
25823 struct glyph_row *r
25824 = MATRIX_ROW (w->current_matrix, vpos);
25825 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25826 EMACS_INT p = string_buffer_position (obj, start);
25827 if (p > 0)
25828 pointer = Fget_char_property (make_number (p),
25829 Qpointer, w->buffer);
25830 }
25831 }
25832 else if (BUFFERP (obj)
25833 && charpos >= BEGV
25834 && charpos < ZV)
25835 pointer = Fget_text_property (make_number (charpos),
25836 Qpointer, obj);
25837 }
25838 }
25839 #endif /* HAVE_WINDOW_SYSTEM */
25840
25841 BEGV = obegv;
25842 ZV = ozv;
25843 current_buffer = obuf;
25844 }
25845
25846 set_cursor:
25847
25848 #ifdef HAVE_WINDOW_SYSTEM
25849 if (FRAME_WINDOW_P (f))
25850 define_frame_cursor1 (f, cursor, pointer);
25851 #else
25852 /* This is here to prevent a compiler error, about "label at end of
25853 compound statement". */
25854 return;
25855 #endif
25856 }
25857
25858
25859 /* EXPORT for RIF:
25860 Clear any mouse-face on window W. This function is part of the
25861 redisplay interface, and is called from try_window_id and similar
25862 functions to ensure the mouse-highlight is off. */
25863
25864 void
25865 x_clear_window_mouse_face (struct window *w)
25866 {
25867 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25868 Lisp_Object window;
25869
25870 BLOCK_INPUT;
25871 XSETWINDOW (window, w);
25872 if (EQ (window, hlinfo->mouse_face_window))
25873 clear_mouse_face (hlinfo);
25874 UNBLOCK_INPUT;
25875 }
25876
25877
25878 /* EXPORT:
25879 Just discard the mouse face information for frame F, if any.
25880 This is used when the size of F is changed. */
25881
25882 void
25883 cancel_mouse_face (struct frame *f)
25884 {
25885 Lisp_Object window;
25886 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25887
25888 window = hlinfo->mouse_face_window;
25889 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25890 {
25891 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25892 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25893 hlinfo->mouse_face_window = Qnil;
25894 }
25895 }
25896
25897
25898 \f
25899 /***********************************************************************
25900 Exposure Events
25901 ***********************************************************************/
25902
25903 #ifdef HAVE_WINDOW_SYSTEM
25904
25905 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25906 which intersects rectangle R. R is in window-relative coordinates. */
25907
25908 static void
25909 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25910 enum glyph_row_area area)
25911 {
25912 struct glyph *first = row->glyphs[area];
25913 struct glyph *end = row->glyphs[area] + row->used[area];
25914 struct glyph *last;
25915 int first_x, start_x, x;
25916
25917 if (area == TEXT_AREA && row->fill_line_p)
25918 /* If row extends face to end of line write the whole line. */
25919 draw_glyphs (w, 0, row, area,
25920 0, row->used[area],
25921 DRAW_NORMAL_TEXT, 0);
25922 else
25923 {
25924 /* Set START_X to the window-relative start position for drawing glyphs of
25925 AREA. The first glyph of the text area can be partially visible.
25926 The first glyphs of other areas cannot. */
25927 start_x = window_box_left_offset (w, area);
25928 x = start_x;
25929 if (area == TEXT_AREA)
25930 x += row->x;
25931
25932 /* Find the first glyph that must be redrawn. */
25933 while (first < end
25934 && x + first->pixel_width < r->x)
25935 {
25936 x += first->pixel_width;
25937 ++first;
25938 }
25939
25940 /* Find the last one. */
25941 last = first;
25942 first_x = x;
25943 while (last < end
25944 && x < r->x + r->width)
25945 {
25946 x += last->pixel_width;
25947 ++last;
25948 }
25949
25950 /* Repaint. */
25951 if (last > first)
25952 draw_glyphs (w, first_x - start_x, row, area,
25953 first - row->glyphs[area], last - row->glyphs[area],
25954 DRAW_NORMAL_TEXT, 0);
25955 }
25956 }
25957
25958
25959 /* Redraw the parts of the glyph row ROW on window W intersecting
25960 rectangle R. R is in window-relative coordinates. Value is
25961 non-zero if mouse-face was overwritten. */
25962
25963 static int
25964 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25965 {
25966 xassert (row->enabled_p);
25967
25968 if (row->mode_line_p || w->pseudo_window_p)
25969 draw_glyphs (w, 0, row, TEXT_AREA,
25970 0, row->used[TEXT_AREA],
25971 DRAW_NORMAL_TEXT, 0);
25972 else
25973 {
25974 if (row->used[LEFT_MARGIN_AREA])
25975 expose_area (w, row, r, LEFT_MARGIN_AREA);
25976 if (row->used[TEXT_AREA])
25977 expose_area (w, row, r, TEXT_AREA);
25978 if (row->used[RIGHT_MARGIN_AREA])
25979 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25980 draw_row_fringe_bitmaps (w, row);
25981 }
25982
25983 return row->mouse_face_p;
25984 }
25985
25986
25987 /* Redraw those parts of glyphs rows during expose event handling that
25988 overlap other rows. Redrawing of an exposed line writes over parts
25989 of lines overlapping that exposed line; this function fixes that.
25990
25991 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25992 row in W's current matrix that is exposed and overlaps other rows.
25993 LAST_OVERLAPPING_ROW is the last such row. */
25994
25995 static void
25996 expose_overlaps (struct window *w,
25997 struct glyph_row *first_overlapping_row,
25998 struct glyph_row *last_overlapping_row,
25999 XRectangle *r)
26000 {
26001 struct glyph_row *row;
26002
26003 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26004 if (row->overlapping_p)
26005 {
26006 xassert (row->enabled_p && !row->mode_line_p);
26007
26008 row->clip = r;
26009 if (row->used[LEFT_MARGIN_AREA])
26010 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26011
26012 if (row->used[TEXT_AREA])
26013 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26014
26015 if (row->used[RIGHT_MARGIN_AREA])
26016 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26017 row->clip = NULL;
26018 }
26019 }
26020
26021
26022 /* Return non-zero if W's cursor intersects rectangle R. */
26023
26024 static int
26025 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26026 {
26027 XRectangle cr, result;
26028 struct glyph *cursor_glyph;
26029 struct glyph_row *row;
26030
26031 if (w->phys_cursor.vpos >= 0
26032 && w->phys_cursor.vpos < w->current_matrix->nrows
26033 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26034 row->enabled_p)
26035 && row->cursor_in_fringe_p)
26036 {
26037 /* Cursor is in the fringe. */
26038 cr.x = window_box_right_offset (w,
26039 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26040 ? RIGHT_MARGIN_AREA
26041 : TEXT_AREA));
26042 cr.y = row->y;
26043 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26044 cr.height = row->height;
26045 return x_intersect_rectangles (&cr, r, &result);
26046 }
26047
26048 cursor_glyph = get_phys_cursor_glyph (w);
26049 if (cursor_glyph)
26050 {
26051 /* r is relative to W's box, but w->phys_cursor.x is relative
26052 to left edge of W's TEXT area. Adjust it. */
26053 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26054 cr.y = w->phys_cursor.y;
26055 cr.width = cursor_glyph->pixel_width;
26056 cr.height = w->phys_cursor_height;
26057 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26058 I assume the effect is the same -- and this is portable. */
26059 return x_intersect_rectangles (&cr, r, &result);
26060 }
26061 /* If we don't understand the format, pretend we're not in the hot-spot. */
26062 return 0;
26063 }
26064
26065
26066 /* EXPORT:
26067 Draw a vertical window border to the right of window W if W doesn't
26068 have vertical scroll bars. */
26069
26070 void
26071 x_draw_vertical_border (struct window *w)
26072 {
26073 struct frame *f = XFRAME (WINDOW_FRAME (w));
26074
26075 /* We could do better, if we knew what type of scroll-bar the adjacent
26076 windows (on either side) have... But we don't :-(
26077 However, I think this works ok. ++KFS 2003-04-25 */
26078
26079 /* Redraw borders between horizontally adjacent windows. Don't
26080 do it for frames with vertical scroll bars because either the
26081 right scroll bar of a window, or the left scroll bar of its
26082 neighbor will suffice as a border. */
26083 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26084 return;
26085
26086 if (!WINDOW_RIGHTMOST_P (w)
26087 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26088 {
26089 int x0, x1, y0, y1;
26090
26091 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26092 y1 -= 1;
26093
26094 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26095 x1 -= 1;
26096
26097 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26098 }
26099 else if (!WINDOW_LEFTMOST_P (w)
26100 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26101 {
26102 int x0, x1, y0, y1;
26103
26104 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26105 y1 -= 1;
26106
26107 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26108 x0 -= 1;
26109
26110 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26111 }
26112 }
26113
26114
26115 /* Redraw the part of window W intersection rectangle FR. Pixel
26116 coordinates in FR are frame-relative. Call this function with
26117 input blocked. Value is non-zero if the exposure overwrites
26118 mouse-face. */
26119
26120 static int
26121 expose_window (struct window *w, XRectangle *fr)
26122 {
26123 struct frame *f = XFRAME (w->frame);
26124 XRectangle wr, r;
26125 int mouse_face_overwritten_p = 0;
26126
26127 /* If window is not yet fully initialized, do nothing. This can
26128 happen when toolkit scroll bars are used and a window is split.
26129 Reconfiguring the scroll bar will generate an expose for a newly
26130 created window. */
26131 if (w->current_matrix == NULL)
26132 return 0;
26133
26134 /* When we're currently updating the window, display and current
26135 matrix usually don't agree. Arrange for a thorough display
26136 later. */
26137 if (w == updated_window)
26138 {
26139 SET_FRAME_GARBAGED (f);
26140 return 0;
26141 }
26142
26143 /* Frame-relative pixel rectangle of W. */
26144 wr.x = WINDOW_LEFT_EDGE_X (w);
26145 wr.y = WINDOW_TOP_EDGE_Y (w);
26146 wr.width = WINDOW_TOTAL_WIDTH (w);
26147 wr.height = WINDOW_TOTAL_HEIGHT (w);
26148
26149 if (x_intersect_rectangles (fr, &wr, &r))
26150 {
26151 int yb = window_text_bottom_y (w);
26152 struct glyph_row *row;
26153 int cursor_cleared_p;
26154 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26155
26156 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26157 r.x, r.y, r.width, r.height));
26158
26159 /* Convert to window coordinates. */
26160 r.x -= WINDOW_LEFT_EDGE_X (w);
26161 r.y -= WINDOW_TOP_EDGE_Y (w);
26162
26163 /* Turn off the cursor. */
26164 if (!w->pseudo_window_p
26165 && phys_cursor_in_rect_p (w, &r))
26166 {
26167 x_clear_cursor (w);
26168 cursor_cleared_p = 1;
26169 }
26170 else
26171 cursor_cleared_p = 0;
26172
26173 /* Update lines intersecting rectangle R. */
26174 first_overlapping_row = last_overlapping_row = NULL;
26175 for (row = w->current_matrix->rows;
26176 row->enabled_p;
26177 ++row)
26178 {
26179 int y0 = row->y;
26180 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26181
26182 if ((y0 >= r.y && y0 < r.y + r.height)
26183 || (y1 > r.y && y1 < r.y + r.height)
26184 || (r.y >= y0 && r.y < y1)
26185 || (r.y + r.height > y0 && r.y + r.height < y1))
26186 {
26187 /* A header line may be overlapping, but there is no need
26188 to fix overlapping areas for them. KFS 2005-02-12 */
26189 if (row->overlapping_p && !row->mode_line_p)
26190 {
26191 if (first_overlapping_row == NULL)
26192 first_overlapping_row = row;
26193 last_overlapping_row = row;
26194 }
26195
26196 row->clip = fr;
26197 if (expose_line (w, row, &r))
26198 mouse_face_overwritten_p = 1;
26199 row->clip = NULL;
26200 }
26201 else if (row->overlapping_p)
26202 {
26203 /* We must redraw a row overlapping the exposed area. */
26204 if (y0 < r.y
26205 ? y0 + row->phys_height > r.y
26206 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26207 {
26208 if (first_overlapping_row == NULL)
26209 first_overlapping_row = row;
26210 last_overlapping_row = row;
26211 }
26212 }
26213
26214 if (y1 >= yb)
26215 break;
26216 }
26217
26218 /* Display the mode line if there is one. */
26219 if (WINDOW_WANTS_MODELINE_P (w)
26220 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26221 row->enabled_p)
26222 && row->y < r.y + r.height)
26223 {
26224 if (expose_line (w, row, &r))
26225 mouse_face_overwritten_p = 1;
26226 }
26227
26228 if (!w->pseudo_window_p)
26229 {
26230 /* Fix the display of overlapping rows. */
26231 if (first_overlapping_row)
26232 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26233 fr);
26234
26235 /* Draw border between windows. */
26236 x_draw_vertical_border (w);
26237
26238 /* Turn the cursor on again. */
26239 if (cursor_cleared_p)
26240 update_window_cursor (w, 1);
26241 }
26242 }
26243
26244 return mouse_face_overwritten_p;
26245 }
26246
26247
26248
26249 /* Redraw (parts) of all windows in the window tree rooted at W that
26250 intersect R. R contains frame pixel coordinates. Value is
26251 non-zero if the exposure overwrites mouse-face. */
26252
26253 static int
26254 expose_window_tree (struct window *w, XRectangle *r)
26255 {
26256 struct frame *f = XFRAME (w->frame);
26257 int mouse_face_overwritten_p = 0;
26258
26259 while (w && !FRAME_GARBAGED_P (f))
26260 {
26261 if (!NILP (w->hchild))
26262 mouse_face_overwritten_p
26263 |= expose_window_tree (XWINDOW (w->hchild), r);
26264 else if (!NILP (w->vchild))
26265 mouse_face_overwritten_p
26266 |= expose_window_tree (XWINDOW (w->vchild), r);
26267 else
26268 mouse_face_overwritten_p |= expose_window (w, r);
26269
26270 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26271 }
26272
26273 return mouse_face_overwritten_p;
26274 }
26275
26276
26277 /* EXPORT:
26278 Redisplay an exposed area of frame F. X and Y are the upper-left
26279 corner of the exposed rectangle. W and H are width and height of
26280 the exposed area. All are pixel values. W or H zero means redraw
26281 the entire frame. */
26282
26283 void
26284 expose_frame (struct frame *f, int x, int y, int w, int h)
26285 {
26286 XRectangle r;
26287 int mouse_face_overwritten_p = 0;
26288
26289 TRACE ((stderr, "expose_frame "));
26290
26291 /* No need to redraw if frame will be redrawn soon. */
26292 if (FRAME_GARBAGED_P (f))
26293 {
26294 TRACE ((stderr, " garbaged\n"));
26295 return;
26296 }
26297
26298 /* If basic faces haven't been realized yet, there is no point in
26299 trying to redraw anything. This can happen when we get an expose
26300 event while Emacs is starting, e.g. by moving another window. */
26301 if (FRAME_FACE_CACHE (f) == NULL
26302 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26303 {
26304 TRACE ((stderr, " no faces\n"));
26305 return;
26306 }
26307
26308 if (w == 0 || h == 0)
26309 {
26310 r.x = r.y = 0;
26311 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26312 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26313 }
26314 else
26315 {
26316 r.x = x;
26317 r.y = y;
26318 r.width = w;
26319 r.height = h;
26320 }
26321
26322 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26323 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26324
26325 if (WINDOWP (f->tool_bar_window))
26326 mouse_face_overwritten_p
26327 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26328
26329 #ifdef HAVE_X_WINDOWS
26330 #ifndef MSDOS
26331 #ifndef USE_X_TOOLKIT
26332 if (WINDOWP (f->menu_bar_window))
26333 mouse_face_overwritten_p
26334 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26335 #endif /* not USE_X_TOOLKIT */
26336 #endif
26337 #endif
26338
26339 /* Some window managers support a focus-follows-mouse style with
26340 delayed raising of frames. Imagine a partially obscured frame,
26341 and moving the mouse into partially obscured mouse-face on that
26342 frame. The visible part of the mouse-face will be highlighted,
26343 then the WM raises the obscured frame. With at least one WM, KDE
26344 2.1, Emacs is not getting any event for the raising of the frame
26345 (even tried with SubstructureRedirectMask), only Expose events.
26346 These expose events will draw text normally, i.e. not
26347 highlighted. Which means we must redo the highlight here.
26348 Subsume it under ``we love X''. --gerd 2001-08-15 */
26349 /* Included in Windows version because Windows most likely does not
26350 do the right thing if any third party tool offers
26351 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26352 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26353 {
26354 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26355 if (f == hlinfo->mouse_face_mouse_frame)
26356 {
26357 int mouse_x = hlinfo->mouse_face_mouse_x;
26358 int mouse_y = hlinfo->mouse_face_mouse_y;
26359 clear_mouse_face (hlinfo);
26360 note_mouse_highlight (f, mouse_x, mouse_y);
26361 }
26362 }
26363 }
26364
26365
26366 /* EXPORT:
26367 Determine the intersection of two rectangles R1 and R2. Return
26368 the intersection in *RESULT. Value is non-zero if RESULT is not
26369 empty. */
26370
26371 int
26372 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26373 {
26374 XRectangle *left, *right;
26375 XRectangle *upper, *lower;
26376 int intersection_p = 0;
26377
26378 /* Rearrange so that R1 is the left-most rectangle. */
26379 if (r1->x < r2->x)
26380 left = r1, right = r2;
26381 else
26382 left = r2, right = r1;
26383
26384 /* X0 of the intersection is right.x0, if this is inside R1,
26385 otherwise there is no intersection. */
26386 if (right->x <= left->x + left->width)
26387 {
26388 result->x = right->x;
26389
26390 /* The right end of the intersection is the minimum of
26391 the right ends of left and right. */
26392 result->width = (min (left->x + left->width, right->x + right->width)
26393 - result->x);
26394
26395 /* Same game for Y. */
26396 if (r1->y < r2->y)
26397 upper = r1, lower = r2;
26398 else
26399 upper = r2, lower = r1;
26400
26401 /* The upper end of the intersection is lower.y0, if this is inside
26402 of upper. Otherwise, there is no intersection. */
26403 if (lower->y <= upper->y + upper->height)
26404 {
26405 result->y = lower->y;
26406
26407 /* The lower end of the intersection is the minimum of the lower
26408 ends of upper and lower. */
26409 result->height = (min (lower->y + lower->height,
26410 upper->y + upper->height)
26411 - result->y);
26412 intersection_p = 1;
26413 }
26414 }
26415
26416 return intersection_p;
26417 }
26418
26419 #endif /* HAVE_WINDOW_SYSTEM */
26420
26421 \f
26422 /***********************************************************************
26423 Initialization
26424 ***********************************************************************/
26425
26426 void
26427 syms_of_xdisp (void)
26428 {
26429 Vwith_echo_area_save_vector = Qnil;
26430 staticpro (&Vwith_echo_area_save_vector);
26431
26432 Vmessage_stack = Qnil;
26433 staticpro (&Vmessage_stack);
26434
26435 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
26436
26437 message_dolog_marker1 = Fmake_marker ();
26438 staticpro (&message_dolog_marker1);
26439 message_dolog_marker2 = Fmake_marker ();
26440 staticpro (&message_dolog_marker2);
26441 message_dolog_marker3 = Fmake_marker ();
26442 staticpro (&message_dolog_marker3);
26443
26444 #if GLYPH_DEBUG
26445 defsubr (&Sdump_frame_glyph_matrix);
26446 defsubr (&Sdump_glyph_matrix);
26447 defsubr (&Sdump_glyph_row);
26448 defsubr (&Sdump_tool_bar_row);
26449 defsubr (&Strace_redisplay);
26450 defsubr (&Strace_to_stderr);
26451 #endif
26452 #ifdef HAVE_WINDOW_SYSTEM
26453 defsubr (&Stool_bar_lines_needed);
26454 defsubr (&Slookup_image_map);
26455 #endif
26456 defsubr (&Sformat_mode_line);
26457 defsubr (&Sinvisible_p);
26458 defsubr (&Scurrent_bidi_paragraph_direction);
26459
26460 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
26461 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
26462 DEFSYM (Qoverriding_local_map, "overriding-local-map");
26463 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
26464 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
26465 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
26466 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
26467 DEFSYM (Qeval, "eval");
26468 DEFSYM (QCdata, ":data");
26469 DEFSYM (Qdisplay, "display");
26470 DEFSYM (Qspace_width, "space-width");
26471 DEFSYM (Qraise, "raise");
26472 DEFSYM (Qslice, "slice");
26473 DEFSYM (Qspace, "space");
26474 DEFSYM (Qmargin, "margin");
26475 DEFSYM (Qpointer, "pointer");
26476 DEFSYM (Qleft_margin, "left-margin");
26477 DEFSYM (Qright_margin, "right-margin");
26478 DEFSYM (Qcenter, "center");
26479 DEFSYM (Qline_height, "line-height");
26480 DEFSYM (QCalign_to, ":align-to");
26481 DEFSYM (QCrelative_width, ":relative-width");
26482 DEFSYM (QCrelative_height, ":relative-height");
26483 DEFSYM (QCeval, ":eval");
26484 DEFSYM (QCpropertize, ":propertize");
26485 DEFSYM (QCfile, ":file");
26486 DEFSYM (Qfontified, "fontified");
26487 DEFSYM (Qfontification_functions, "fontification-functions");
26488 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
26489 DEFSYM (Qescape_glyph, "escape-glyph");
26490 DEFSYM (Qnobreak_space, "nobreak-space");
26491 DEFSYM (Qimage, "image");
26492 DEFSYM (Qtext, "text");
26493 DEFSYM (Qboth, "both");
26494 DEFSYM (Qboth_horiz, "both-horiz");
26495 DEFSYM (Qtext_image_horiz, "text-image-horiz");
26496 DEFSYM (QCmap, ":map");
26497 DEFSYM (QCpointer, ":pointer");
26498 DEFSYM (Qrect, "rect");
26499 DEFSYM (Qcircle, "circle");
26500 DEFSYM (Qpoly, "poly");
26501 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
26502 DEFSYM (Qgrow_only, "grow-only");
26503 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
26504 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
26505 DEFSYM (Qposition, "position");
26506 DEFSYM (Qbuffer_position, "buffer-position");
26507 DEFSYM (Qobject, "object");
26508 DEFSYM (Qbar, "bar");
26509 DEFSYM (Qhbar, "hbar");
26510 DEFSYM (Qbox, "box");
26511 DEFSYM (Qhollow, "hollow");
26512 DEFSYM (Qhand, "hand");
26513 DEFSYM (Qarrow, "arrow");
26514 DEFSYM (Qtext, "text");
26515 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
26516
26517 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26518 Fcons (intern_c_string ("void-variable"), Qnil)),
26519 Qnil);
26520 staticpro (&list_of_error);
26521
26522 DEFSYM (Qlast_arrow_position, "last-arrow-position");
26523 DEFSYM (Qlast_arrow_string, "last-arrow-string");
26524 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
26525 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
26526
26527 echo_buffer[0] = echo_buffer[1] = Qnil;
26528 staticpro (&echo_buffer[0]);
26529 staticpro (&echo_buffer[1]);
26530
26531 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26532 staticpro (&echo_area_buffer[0]);
26533 staticpro (&echo_area_buffer[1]);
26534
26535 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26536 staticpro (&Vmessages_buffer_name);
26537
26538 mode_line_proptrans_alist = Qnil;
26539 staticpro (&mode_line_proptrans_alist);
26540 mode_line_string_list = Qnil;
26541 staticpro (&mode_line_string_list);
26542 mode_line_string_face = Qnil;
26543 staticpro (&mode_line_string_face);
26544 mode_line_string_face_prop = Qnil;
26545 staticpro (&mode_line_string_face_prop);
26546 Vmode_line_unwind_vector = Qnil;
26547 staticpro (&Vmode_line_unwind_vector);
26548
26549 help_echo_string = Qnil;
26550 staticpro (&help_echo_string);
26551 help_echo_object = Qnil;
26552 staticpro (&help_echo_object);
26553 help_echo_window = Qnil;
26554 staticpro (&help_echo_window);
26555 previous_help_echo_string = Qnil;
26556 staticpro (&previous_help_echo_string);
26557 help_echo_pos = -1;
26558
26559 DEFSYM (Qright_to_left, "right-to-left");
26560 DEFSYM (Qleft_to_right, "left-to-right");
26561
26562 #ifdef HAVE_WINDOW_SYSTEM
26563 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
26564 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26565 For example, if a block cursor is over a tab, it will be drawn as
26566 wide as that tab on the display. */);
26567 x_stretch_cursor_p = 0;
26568 #endif
26569
26570 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
26571 doc: /* *Non-nil means highlight trailing whitespace.
26572 The face used for trailing whitespace is `trailing-whitespace'. */);
26573 Vshow_trailing_whitespace = Qnil;
26574
26575 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
26576 doc: /* *Control highlighting of nobreak space and soft hyphen.
26577 A value of t means highlight the character itself (for nobreak space,
26578 use face `nobreak-space').
26579 A value of nil means no highlighting.
26580 Other values mean display the escape glyph followed by an ordinary
26581 space or ordinary hyphen. */);
26582 Vnobreak_char_display = Qt;
26583
26584 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
26585 doc: /* *The pointer shape to show in void text areas.
26586 A value of nil means to show the text pointer. Other options are `arrow',
26587 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26588 Vvoid_text_area_pointer = Qarrow;
26589
26590 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
26591 doc: /* Non-nil means don't actually do any redisplay.
26592 This is used for internal purposes. */);
26593 Vinhibit_redisplay = Qnil;
26594
26595 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
26596 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26597 Vglobal_mode_string = Qnil;
26598
26599 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
26600 doc: /* Marker for where to display an arrow on top of the buffer text.
26601 This must be the beginning of a line in order to work.
26602 See also `overlay-arrow-string'. */);
26603 Voverlay_arrow_position = Qnil;
26604
26605 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
26606 doc: /* String to display as an arrow in non-window frames.
26607 See also `overlay-arrow-position'. */);
26608 Voverlay_arrow_string = make_pure_c_string ("=>");
26609
26610 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
26611 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26612 The symbols on this list are examined during redisplay to determine
26613 where to display overlay arrows. */);
26614 Voverlay_arrow_variable_list
26615 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26616
26617 DEFVAR_INT ("scroll-step", emacs_scroll_step,
26618 doc: /* *The number of lines to try scrolling a window by when point moves out.
26619 If that fails to bring point back on frame, point is centered instead.
26620 If this is zero, point is always centered after it moves off frame.
26621 If you want scrolling to always be a line at a time, you should set
26622 `scroll-conservatively' to a large value rather than set this to 1. */);
26623
26624 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
26625 doc: /* *Scroll up to this many lines, to bring point back on screen.
26626 If point moves off-screen, redisplay will scroll by up to
26627 `scroll-conservatively' lines in order to bring point just barely
26628 onto the screen again. If that cannot be done, then redisplay
26629 recenters point as usual.
26630
26631 If the value is greater than 100, redisplay will never recenter point,
26632 but will always scroll just enough text to bring point into view, even
26633 if you move far away.
26634
26635 A value of zero means always recenter point if it moves off screen. */);
26636 scroll_conservatively = 0;
26637
26638 DEFVAR_INT ("scroll-margin", scroll_margin,
26639 doc: /* *Number of lines of margin at the top and bottom of a window.
26640 Recenter the window whenever point gets within this many lines
26641 of the top or bottom of the window. */);
26642 scroll_margin = 0;
26643
26644 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
26645 doc: /* Pixels per inch value for non-window system displays.
26646 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26647 Vdisplay_pixels_per_inch = make_float (72.0);
26648
26649 #if GLYPH_DEBUG
26650 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
26651 #endif
26652
26653 DEFVAR_LISP ("truncate-partial-width-windows",
26654 Vtruncate_partial_width_windows,
26655 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26656 For an integer value, truncate lines in each window narrower than the
26657 full frame width, provided the window width is less than that integer;
26658 otherwise, respect the value of `truncate-lines'.
26659
26660 For any other non-nil value, truncate lines in all windows that do
26661 not span the full frame width.
26662
26663 A value of nil means to respect the value of `truncate-lines'.
26664
26665 If `word-wrap' is enabled, you might want to reduce this. */);
26666 Vtruncate_partial_width_windows = make_number (50);
26667
26668 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
26669 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26670 Any other value means to use the appropriate face, `mode-line',
26671 `header-line', or `menu' respectively. */);
26672 mode_line_inverse_video = 1;
26673
26674 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
26675 doc: /* *Maximum buffer size for which line number should be displayed.
26676 If the buffer is bigger than this, the line number does not appear
26677 in the mode line. A value of nil means no limit. */);
26678 Vline_number_display_limit = Qnil;
26679
26680 DEFVAR_INT ("line-number-display-limit-width",
26681 line_number_display_limit_width,
26682 doc: /* *Maximum line width (in characters) for line number display.
26683 If the average length of the lines near point is bigger than this, then the
26684 line number may be omitted from the mode line. */);
26685 line_number_display_limit_width = 200;
26686
26687 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
26688 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26689 highlight_nonselected_windows = 0;
26690
26691 DEFVAR_BOOL ("multiple-frames", multiple_frames,
26692 doc: /* Non-nil if more than one frame is visible on this display.
26693 Minibuffer-only frames don't count, but iconified frames do.
26694 This variable is not guaranteed to be accurate except while processing
26695 `frame-title-format' and `icon-title-format'. */);
26696
26697 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
26698 doc: /* Template for displaying the title bar of visible frames.
26699 \(Assuming the window manager supports this feature.)
26700
26701 This variable has the same structure as `mode-line-format', except that
26702 the %c and %l constructs are ignored. It is used only on frames for
26703 which no explicit name has been set \(see `modify-frame-parameters'). */);
26704
26705 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
26706 doc: /* Template for displaying the title bar of an iconified frame.
26707 \(Assuming the window manager supports this feature.)
26708 This variable has the same structure as `mode-line-format' (which see),
26709 and is used only on frames for which no explicit name has been set
26710 \(see `modify-frame-parameters'). */);
26711 Vicon_title_format
26712 = Vframe_title_format
26713 = pure_cons (intern_c_string ("multiple-frames"),
26714 pure_cons (make_pure_c_string ("%b"),
26715 pure_cons (pure_cons (empty_unibyte_string,
26716 pure_cons (intern_c_string ("invocation-name"),
26717 pure_cons (make_pure_c_string ("@"),
26718 pure_cons (intern_c_string ("system-name"),
26719 Qnil)))),
26720 Qnil)));
26721
26722 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
26723 doc: /* Maximum number of lines to keep in the message log buffer.
26724 If nil, disable message logging. If t, log messages but don't truncate
26725 the buffer when it becomes large. */);
26726 Vmessage_log_max = make_number (100);
26727
26728 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
26729 doc: /* Functions called before redisplay, if window sizes have changed.
26730 The value should be a list of functions that take one argument.
26731 Just before redisplay, for each frame, if any of its windows have changed
26732 size since the last redisplay, or have been split or deleted,
26733 all the functions in the list are called, with the frame as argument. */);
26734 Vwindow_size_change_functions = Qnil;
26735
26736 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
26737 doc: /* List of functions to call before redisplaying a window with scrolling.
26738 Each function is called with two arguments, the window and its new
26739 display-start position. Note that these functions are also called by
26740 `set-window-buffer'. Also note that the value of `window-end' is not
26741 valid when these functions are called. */);
26742 Vwindow_scroll_functions = Qnil;
26743
26744 DEFVAR_LISP ("window-text-change-functions",
26745 Vwindow_text_change_functions,
26746 doc: /* Functions to call in redisplay when text in the window might change. */);
26747 Vwindow_text_change_functions = Qnil;
26748
26749 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
26750 doc: /* Functions called when redisplay of a window reaches the end trigger.
26751 Each function is called with two arguments, the window and the end trigger value.
26752 See `set-window-redisplay-end-trigger'. */);
26753 Vredisplay_end_trigger_functions = Qnil;
26754
26755 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
26756 doc: /* *Non-nil means autoselect window with mouse pointer.
26757 If nil, do not autoselect windows.
26758 A positive number means delay autoselection by that many seconds: a
26759 window is autoselected only after the mouse has remained in that
26760 window for the duration of the delay.
26761 A negative number has a similar effect, but causes windows to be
26762 autoselected only after the mouse has stopped moving. \(Because of
26763 the way Emacs compares mouse events, you will occasionally wait twice
26764 that time before the window gets selected.\)
26765 Any other value means to autoselect window instantaneously when the
26766 mouse pointer enters it.
26767
26768 Autoselection selects the minibuffer only if it is active, and never
26769 unselects the minibuffer if it is active.
26770
26771 When customizing this variable make sure that the actual value of
26772 `focus-follows-mouse' matches the behavior of your window manager. */);
26773 Vmouse_autoselect_window = Qnil;
26774
26775 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
26776 doc: /* *Non-nil means automatically resize tool-bars.
26777 This dynamically changes the tool-bar's height to the minimum height
26778 that is needed to make all tool-bar items visible.
26779 If value is `grow-only', the tool-bar's height is only increased
26780 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26781 Vauto_resize_tool_bars = Qt;
26782
26783 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
26784 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26785 auto_raise_tool_bar_buttons_p = 1;
26786
26787 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
26788 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26789 make_cursor_line_fully_visible_p = 1;
26790
26791 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
26792 doc: /* *Border below tool-bar in pixels.
26793 If an integer, use it as the height of the border.
26794 If it is one of `internal-border-width' or `border-width', use the
26795 value of the corresponding frame parameter.
26796 Otherwise, no border is added below the tool-bar. */);
26797 Vtool_bar_border = Qinternal_border_width;
26798
26799 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
26800 doc: /* *Margin around tool-bar buttons in pixels.
26801 If an integer, use that for both horizontal and vertical margins.
26802 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26803 HORZ specifying the horizontal margin, and VERT specifying the
26804 vertical margin. */);
26805 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26806
26807 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
26808 doc: /* *Relief thickness of tool-bar buttons. */);
26809 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26810
26811 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
26812 doc: /* Tool bar style to use.
26813 It can be one of
26814 image - show images only
26815 text - show text only
26816 both - show both, text below image
26817 both-horiz - show text to the right of the image
26818 text-image-horiz - show text to the left of the image
26819 any other - use system default or image if no system default. */);
26820 Vtool_bar_style = Qnil;
26821
26822 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
26823 doc: /* *Maximum number of characters a label can have to be shown.
26824 The tool bar style must also show labels for this to have any effect, see
26825 `tool-bar-style'. */);
26826 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26827
26828 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
26829 doc: /* List of functions to call to fontify regions of text.
26830 Each function is called with one argument POS. Functions must
26831 fontify a region starting at POS in the current buffer, and give
26832 fontified regions the property `fontified'. */);
26833 Vfontification_functions = Qnil;
26834 Fmake_variable_buffer_local (Qfontification_functions);
26835
26836 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26837 unibyte_display_via_language_environment,
26838 doc: /* *Non-nil means display unibyte text according to language environment.
26839 Specifically, this means that raw bytes in the range 160-255 decimal
26840 are displayed by converting them to the equivalent multibyte characters
26841 according to the current language environment. As a result, they are
26842 displayed according to the current fontset.
26843
26844 Note that this variable affects only how these bytes are displayed,
26845 but does not change the fact they are interpreted as raw bytes. */);
26846 unibyte_display_via_language_environment = 0;
26847
26848 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
26849 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
26850 If a float, it specifies a fraction of the mini-window frame's height.
26851 If an integer, it specifies a number of lines. */);
26852 Vmax_mini_window_height = make_float (0.25);
26853
26854 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
26855 doc: /* *How to resize mini-windows (the minibuffer and the echo area).
26856 A value of nil means don't automatically resize mini-windows.
26857 A value of t means resize them to fit the text displayed in them.
26858 A value of `grow-only', the default, means let mini-windows grow
26859 only, until their display becomes empty, at which point the windows
26860 go back to their normal size. */);
26861 Vresize_mini_windows = Qgrow_only;
26862
26863 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
26864 doc: /* Alist specifying how to blink the cursor off.
26865 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26866 `cursor-type' frame-parameter or variable equals ON-STATE,
26867 comparing using `equal', Emacs uses OFF-STATE to specify
26868 how to blink it off. ON-STATE and OFF-STATE are values for
26869 the `cursor-type' frame parameter.
26870
26871 If a frame's ON-STATE has no entry in this list,
26872 the frame's other specifications determine how to blink the cursor off. */);
26873 Vblink_cursor_alist = Qnil;
26874
26875 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
26876 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26877 If non-nil, windows are automatically scrolled horizontally to make
26878 point visible. */);
26879 automatic_hscrolling_p = 1;
26880 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
26881
26882 DEFVAR_INT ("hscroll-margin", hscroll_margin,
26883 doc: /* *How many columns away from the window edge point is allowed to get
26884 before automatic hscrolling will horizontally scroll the window. */);
26885 hscroll_margin = 5;
26886
26887 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
26888 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26889 When point is less than `hscroll-margin' columns from the window
26890 edge, automatic hscrolling will scroll the window by the amount of columns
26891 determined by this variable. If its value is a positive integer, scroll that
26892 many columns. If it's a positive floating-point number, it specifies the
26893 fraction of the window's width to scroll. If it's nil or zero, point will be
26894 centered horizontally after the scroll. Any other value, including negative
26895 numbers, are treated as if the value were zero.
26896
26897 Automatic hscrolling always moves point outside the scroll margin, so if
26898 point was more than scroll step columns inside the margin, the window will
26899 scroll more than the value given by the scroll step.
26900
26901 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26902 and `scroll-right' overrides this variable's effect. */);
26903 Vhscroll_step = make_number (0);
26904
26905 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
26906 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26907 Bind this around calls to `message' to let it take effect. */);
26908 message_truncate_lines = 0;
26909
26910 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
26911 doc: /* Normal hook run to update the menu bar definitions.
26912 Redisplay runs this hook before it redisplays the menu bar.
26913 This is used to update submenus such as Buffers,
26914 whose contents depend on various data. */);
26915 Vmenu_bar_update_hook = Qnil;
26916
26917 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
26918 doc: /* Frame for which we are updating a menu.
26919 The enable predicate for a menu binding should check this variable. */);
26920 Vmenu_updating_frame = Qnil;
26921
26922 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
26923 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26924 inhibit_menubar_update = 0;
26925
26926 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
26927 doc: /* Prefix prepended to all continuation lines at display time.
26928 The value may be a string, an image, or a stretch-glyph; it is
26929 interpreted in the same way as the value of a `display' text property.
26930
26931 This variable is overridden by any `wrap-prefix' text or overlay
26932 property.
26933
26934 To add a prefix to non-continuation lines, use `line-prefix'. */);
26935 Vwrap_prefix = Qnil;
26936 DEFSYM (Qwrap_prefix, "wrap-prefix");
26937 Fmake_variable_buffer_local (Qwrap_prefix);
26938
26939 DEFVAR_LISP ("line-prefix", Vline_prefix,
26940 doc: /* Prefix prepended to all non-continuation lines at display time.
26941 The value may be a string, an image, or a stretch-glyph; it is
26942 interpreted in the same way as the value of a `display' text property.
26943
26944 This variable is overridden by any `line-prefix' text or overlay
26945 property.
26946
26947 To add a prefix to continuation lines, use `wrap-prefix'. */);
26948 Vline_prefix = Qnil;
26949 DEFSYM (Qline_prefix, "line-prefix");
26950 Fmake_variable_buffer_local (Qline_prefix);
26951
26952 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
26953 doc: /* Non-nil means don't eval Lisp during redisplay. */);
26954 inhibit_eval_during_redisplay = 0;
26955
26956 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
26957 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26958 inhibit_free_realized_faces = 0;
26959
26960 #if GLYPH_DEBUG
26961 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
26962 doc: /* Inhibit try_window_id display optimization. */);
26963 inhibit_try_window_id = 0;
26964
26965 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
26966 doc: /* Inhibit try_window_reusing display optimization. */);
26967 inhibit_try_window_reusing = 0;
26968
26969 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
26970 doc: /* Inhibit try_cursor_movement display optimization. */);
26971 inhibit_try_cursor_movement = 0;
26972 #endif /* GLYPH_DEBUG */
26973
26974 DEFVAR_INT ("overline-margin", overline_margin,
26975 doc: /* *Space between overline and text, in pixels.
26976 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26977 margin to the caracter height. */);
26978 overline_margin = 2;
26979
26980 DEFVAR_INT ("underline-minimum-offset",
26981 underline_minimum_offset,
26982 doc: /* Minimum distance between baseline and underline.
26983 This can improve legibility of underlined text at small font sizes,
26984 particularly when using variable `x-use-underline-position-properties'
26985 with fonts that specify an UNDERLINE_POSITION relatively close to the
26986 baseline. The default value is 1. */);
26987 underline_minimum_offset = 1;
26988
26989 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
26990 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
26991 This feature only works when on a window system that can change
26992 cursor shapes. */);
26993 display_hourglass_p = 1;
26994
26995 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
26996 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
26997 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26998
26999 hourglass_atimer = NULL;
27000 hourglass_shown_p = 0;
27001
27002 DEFSYM (Qglyphless_char, "glyphless-char");
27003 DEFSYM (Qhex_code, "hex-code");
27004 DEFSYM (Qempty_box, "empty-box");
27005 DEFSYM (Qthin_space, "thin-space");
27006 DEFSYM (Qzero_width, "zero-width");
27007
27008 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27009 /* Intern this now in case it isn't already done.
27010 Setting this variable twice is harmless.
27011 But don't staticpro it here--that is done in alloc.c. */
27012 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27013 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27014
27015 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27016 doc: /* Char-table defining glyphless characters.
27017 Each element, if non-nil, should be one of the following:
27018 an ASCII acronym string: display this string in a box
27019 `hex-code': display the hexadecimal code of a character in a box
27020 `empty-box': display as an empty box
27021 `thin-space': display as 1-pixel width space
27022 `zero-width': don't display
27023 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27024 display method for graphical terminals and text terminals respectively.
27025 GRAPHICAL and TEXT should each have one of the values listed above.
27026
27027 The char-table has one extra slot to control the display of a character for
27028 which no font is found. This slot only takes effect on graphical terminals.
27029 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27030 `thin-space'. The default is `empty-box'. */);
27031 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27032 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27033 Qempty_box);
27034 }
27035
27036
27037 /* Initialize this module when Emacs starts. */
27038
27039 void
27040 init_xdisp (void)
27041 {
27042 current_header_line_height = current_mode_line_height = -1;
27043
27044 CHARPOS (this_line_start_pos) = 0;
27045
27046 if (!noninteractive)
27047 {
27048 struct window *m = XWINDOW (minibuf_window);
27049 Lisp_Object frame = m->frame;
27050 struct frame *f = XFRAME (frame);
27051 Lisp_Object root = FRAME_ROOT_WINDOW (f);
27052 struct window *r = XWINDOW (root);
27053 int i;
27054
27055 echo_area_window = minibuf_window;
27056
27057 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
27058 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
27059 XSETFASTINT (r->total_cols, FRAME_COLS (f));
27060 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
27061 XSETFASTINT (m->total_lines, 1);
27062 XSETFASTINT (m->total_cols, FRAME_COLS (f));
27063
27064 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27065 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27066 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27067
27068 /* The default ellipsis glyphs `...'. */
27069 for (i = 0; i < 3; ++i)
27070 default_invis_vector[i] = make_number ('.');
27071 }
27072
27073 {
27074 /* Allocate the buffer for frame titles.
27075 Also used for `format-mode-line'. */
27076 int size = 100;
27077 mode_line_noprop_buf = (char *) xmalloc (size);
27078 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27079 mode_line_noprop_ptr = mode_line_noprop_buf;
27080 mode_line_target = MODE_LINE_DISPLAY;
27081 }
27082
27083 help_echo_showing_p = 0;
27084 }
27085
27086 /* Since w32 does not support atimers, it defines its own implementation of
27087 the following three functions in w32fns.c. */
27088 #ifndef WINDOWSNT
27089
27090 /* Platform-independent portion of hourglass implementation. */
27091
27092 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27093 int
27094 hourglass_started (void)
27095 {
27096 return hourglass_shown_p || hourglass_atimer != NULL;
27097 }
27098
27099 /* Cancel a currently active hourglass timer, and start a new one. */
27100 void
27101 start_hourglass (void)
27102 {
27103 #if defined (HAVE_WINDOW_SYSTEM)
27104 EMACS_TIME delay;
27105 int secs, usecs = 0;
27106
27107 cancel_hourglass ();
27108
27109 if (INTEGERP (Vhourglass_delay)
27110 && XINT (Vhourglass_delay) > 0)
27111 secs = XFASTINT (Vhourglass_delay);
27112 else if (FLOATP (Vhourglass_delay)
27113 && XFLOAT_DATA (Vhourglass_delay) > 0)
27114 {
27115 Lisp_Object tem;
27116 tem = Ftruncate (Vhourglass_delay, Qnil);
27117 secs = XFASTINT (tem);
27118 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27119 }
27120 else
27121 secs = DEFAULT_HOURGLASS_DELAY;
27122
27123 EMACS_SET_SECS_USECS (delay, secs, usecs);
27124 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27125 show_hourglass, NULL);
27126 #endif
27127 }
27128
27129
27130 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27131 shown. */
27132 void
27133 cancel_hourglass (void)
27134 {
27135 #if defined (HAVE_WINDOW_SYSTEM)
27136 if (hourglass_atimer)
27137 {
27138 cancel_atimer (hourglass_atimer);
27139 hourglass_atimer = NULL;
27140 }
27141
27142 if (hourglass_shown_p)
27143 hide_hourglass ();
27144 #endif
27145 }
27146 #endif /* ! WINDOWSNT */