Fix bug #11035 with cursor positioning on overlay strings with newlines.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT is a space or tab
387 character. This is used to determine word wrapping. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
391
392 /* Name of the face used to highlight trailing whitespace. */
393
394 static Lisp_Object Qtrailing_whitespace;
395
396 /* Name and number of the face used to highlight escape glyphs. */
397
398 static Lisp_Object Qescape_glyph;
399
400 /* Name and number of the face used to highlight non-breaking spaces. */
401
402 static Lisp_Object Qnobreak_space;
403
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
406
407 Lisp_Object Qimage;
408
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
413
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
419
420 int noninteractive_need_newline;
421
422 /* Non-zero means print newline to message log before next message. */
423
424 static int message_log_need_newline;
425
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
432 \f
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
437
438 static struct text_pos this_line_start_pos;
439
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
442
443 static struct text_pos this_line_end_pos;
444
445 /* The vertical positions and the height of this line. */
446
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
450
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
453
454 static int this_line_start_x;
455
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
459
460 static struct text_pos this_line_min_pos;
461
462 /* Buffer that this_line_.* variables are referring to. */
463
464 static struct buffer *this_line_buffer;
465
466
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
471
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
476
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478
479 Lisp_Object Qmenu_bar_update_hook;
480
481 /* Nonzero if an overlay arrow has been displayed in this window. */
482
483 static int overlay_arrow_seen;
484
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
488
489 int buffer_shared;
490
491 /* Vector containing glyphs for an ellipsis `...'. */
492
493 static Lisp_Object default_invis_vector[3];
494
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
498
499 Lisp_Object echo_area_window;
500
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
505
506 static Lisp_Object Vmessage_stack;
507
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
510
511 static int message_enable_multibyte;
512
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
514
515 int update_mode_lines;
516
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero means a frame's cursor type has been changed. */
523
524 int cursor_type_changed;
525
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
528
529 static int line_number_displayed;
530
531 /* The name of the *Messages* buffer, a string. */
532
533 static Lisp_Object Vmessages_buffer_name;
534
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
537
538 Lisp_Object echo_area_buffer[2];
539
540 /* The buffers referenced from echo_area_buffer. */
541
542 static Lisp_Object echo_buffer[2];
543
544 /* A vector saved used in with_area_buffer to reduce consing. */
545
546 static Lisp_Object Vwith_echo_area_save_vector;
547
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
550
551 static int display_last_displayed_message_p;
552
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
555
556 static int message_buf_print;
557
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
559
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
562
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
565
566 static int message_cleared_p;
567
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
570
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
574
575 /* Ascent and height of the last line processed by move_it_to. */
576
577 static int last_max_ascent, last_height;
578
579 /* Non-zero if there's a help-echo in the echo area. */
580
581 int help_echo_showing_p;
582
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
586
587 int current_mode_line_height, current_header_line_height;
588
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
594
595 #define TEXT_PROP_DISTANCE_LIMIT 100
596
597 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
598 iterator state and later restore it. This is needed because the
599 bidi iterator on bidi.c keeps a stacked cache of its states, which
600 is really a singleton. When we use scratch iterator objects to
601 move around the buffer, we can cause the bidi cache to be pushed or
602 popped, and therefore we need to restore the cache state when we
603 return to the original iterator. */
604 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
605 do { \
606 if (CACHE) \
607 bidi_unshelve_cache (CACHE, 1); \
608 ITCOPY = ITORIG; \
609 CACHE = bidi_shelve_cache (); \
610 } while (0)
611
612 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
613 do { \
614 if (pITORIG != pITCOPY) \
615 *(pITORIG) = *(pITCOPY); \
616 bidi_unshelve_cache (CACHE, 0); \
617 CACHE = NULL; \
618 } while (0)
619
620 #if GLYPH_DEBUG
621
622 /* Non-zero means print traces of redisplay if compiled with
623 GLYPH_DEBUG != 0. */
624
625 int trace_redisplay_p;
626
627 #endif /* GLYPH_DEBUG */
628
629 #ifdef DEBUG_TRACE_MOVE
630 /* Non-zero means trace with TRACE_MOVE to stderr. */
631 int trace_move;
632
633 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
634 #else
635 #define TRACE_MOVE(x) (void) 0
636 #endif
637
638 static Lisp_Object Qauto_hscroll_mode;
639
640 /* Buffer being redisplayed -- for redisplay_window_error. */
641
642 static struct buffer *displayed_buffer;
643
644 /* Value returned from text property handlers (see below). */
645
646 enum prop_handled
647 {
648 HANDLED_NORMALLY,
649 HANDLED_RECOMPUTE_PROPS,
650 HANDLED_OVERLAY_STRING_CONSUMED,
651 HANDLED_RETURN
652 };
653
654 /* A description of text properties that redisplay is interested
655 in. */
656
657 struct props
658 {
659 /* The name of the property. */
660 Lisp_Object *name;
661
662 /* A unique index for the property. */
663 enum prop_idx idx;
664
665 /* A handler function called to set up iterator IT from the property
666 at IT's current position. Value is used to steer handle_stop. */
667 enum prop_handled (*handler) (struct it *it);
668 };
669
670 static enum prop_handled handle_face_prop (struct it *);
671 static enum prop_handled handle_invisible_prop (struct it *);
672 static enum prop_handled handle_display_prop (struct it *);
673 static enum prop_handled handle_composition_prop (struct it *);
674 static enum prop_handled handle_overlay_change (struct it *);
675 static enum prop_handled handle_fontified_prop (struct it *);
676
677 /* Properties handled by iterators. */
678
679 static struct props it_props[] =
680 {
681 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
682 /* Handle `face' before `display' because some sub-properties of
683 `display' need to know the face. */
684 {&Qface, FACE_PROP_IDX, handle_face_prop},
685 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
686 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
687 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
688 {NULL, 0, NULL}
689 };
690
691 /* Value is the position described by X. If X is a marker, value is
692 the marker_position of X. Otherwise, value is X. */
693
694 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
695
696 /* Enumeration returned by some move_it_.* functions internally. */
697
698 enum move_it_result
699 {
700 /* Not used. Undefined value. */
701 MOVE_UNDEFINED,
702
703 /* Move ended at the requested buffer position or ZV. */
704 MOVE_POS_MATCH_OR_ZV,
705
706 /* Move ended at the requested X pixel position. */
707 MOVE_X_REACHED,
708
709 /* Move within a line ended at the end of a line that must be
710 continued. */
711 MOVE_LINE_CONTINUED,
712
713 /* Move within a line ended at the end of a line that would
714 be displayed truncated. */
715 MOVE_LINE_TRUNCATED,
716
717 /* Move within a line ended at a line end. */
718 MOVE_NEWLINE_OR_CR
719 };
720
721 /* This counter is used to clear the face cache every once in a while
722 in redisplay_internal. It is incremented for each redisplay.
723 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
724 cleared. */
725
726 #define CLEAR_FACE_CACHE_COUNT 500
727 static int clear_face_cache_count;
728
729 /* Similarly for the image cache. */
730
731 #ifdef HAVE_WINDOW_SYSTEM
732 #define CLEAR_IMAGE_CACHE_COUNT 101
733 static int clear_image_cache_count;
734
735 /* Null glyph slice */
736 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
737 #endif
738
739 /* Non-zero while redisplay_internal is in progress. */
740
741 int redisplaying_p;
742
743 static Lisp_Object Qinhibit_free_realized_faces;
744
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
747
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 EMACS_INT help_echo_pos;
752
753 /* Temporary variable for XTread_socket. */
754
755 Lisp_Object previous_help_echo_string;
756
757 /* Platform-independent portion of hourglass implementation. */
758
759 /* Non-zero means an hourglass cursor is currently shown. */
760 int hourglass_shown_p;
761
762 /* If non-null, an asynchronous timer that, when it expires, displays
763 an hourglass cursor on all frames. */
764 struct atimer *hourglass_atimer;
765
766 /* Name of the face used to display glyphless characters. */
767 Lisp_Object Qglyphless_char;
768
769 /* Symbol for the purpose of Vglyphless_char_display. */
770 static Lisp_Object Qglyphless_char_display;
771
772 /* Method symbols for Vglyphless_char_display. */
773 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
774
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
777
778 /* Default number of seconds to wait before displaying an hourglass
779 cursor. */
780 #define DEFAULT_HOURGLASS_DELAY 1
781
782 \f
783 /* Function prototypes. */
784
785 static void setup_for_ellipsis (struct it *, int);
786 static void set_iterator_to_next (struct it *, int);
787 static void mark_window_display_accurate_1 (struct window *, int);
788 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
789 static int display_prop_string_p (Lisp_Object, Lisp_Object);
790 static int cursor_row_p (struct glyph_row *);
791 static int redisplay_mode_lines (Lisp_Object, int);
792 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
793
794 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
795
796 static void handle_line_prefix (struct it *);
797
798 static void pint2str (char *, int, EMACS_INT);
799 static void pint2hrstr (char *, int, EMACS_INT);
800 static struct text_pos run_window_scroll_functions (Lisp_Object,
801 struct text_pos);
802 static void reconsider_clip_changes (struct window *, struct buffer *);
803 static int text_outside_line_unchanged_p (struct window *,
804 EMACS_INT, EMACS_INT);
805 static void store_mode_line_noprop_char (char);
806 static int store_mode_line_noprop (const char *, int, int);
807 static void handle_stop (struct it *);
808 static void handle_stop_backwards (struct it *, EMACS_INT);
809 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
810 static void ensure_echo_area_buffers (void);
811 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
812 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
813 static int with_echo_area_buffer (struct window *, int,
814 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
815 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
816 static void clear_garbaged_frames (void);
817 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
818 static void pop_message (void);
819 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
820 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
821 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
822 static int display_echo_area (struct window *);
823 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
824 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
825 static Lisp_Object unwind_redisplay (Lisp_Object);
826 static int string_char_and_length (const unsigned char *, int *);
827 static struct text_pos display_prop_end (struct it *, Lisp_Object,
828 struct text_pos);
829 static int compute_window_start_on_continuation_line (struct window *);
830 static Lisp_Object safe_eval_handler (Lisp_Object);
831 static void insert_left_trunc_glyphs (struct it *);
832 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
833 Lisp_Object);
834 static void extend_face_to_end_of_line (struct it *);
835 static int append_space_for_newline (struct it *, int);
836 static int cursor_row_fully_visible_p (struct window *, int, int);
837 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
838 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
839 static int trailing_whitespace_p (EMACS_INT);
840 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
841 static void push_it (struct it *, struct text_pos *);
842 static void pop_it (struct it *);
843 static void sync_frame_with_window_matrix_rows (struct window *);
844 static void select_frame_for_redisplay (Lisp_Object);
845 static void redisplay_internal (void);
846 static int echo_area_display (int);
847 static void redisplay_windows (Lisp_Object);
848 static void redisplay_window (Lisp_Object, int);
849 static Lisp_Object redisplay_window_error (Lisp_Object);
850 static Lisp_Object redisplay_window_0 (Lisp_Object);
851 static Lisp_Object redisplay_window_1 (Lisp_Object);
852 static int set_cursor_from_row (struct window *, struct glyph_row *,
853 struct glyph_matrix *, EMACS_INT, EMACS_INT,
854 int, int);
855 static int update_menu_bar (struct frame *, int, int);
856 static int try_window_reusing_current_matrix (struct window *);
857 static int try_window_id (struct window *);
858 static int display_line (struct it *);
859 static int display_mode_lines (struct window *);
860 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
861 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
862 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
863 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
864 static void display_menu_bar (struct window *);
865 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
866 EMACS_INT *);
867 static int display_string (const char *, Lisp_Object, Lisp_Object,
868 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
869 static void compute_line_metrics (struct it *);
870 static void run_redisplay_end_trigger_hook (struct it *);
871 static int get_overlay_strings (struct it *, EMACS_INT);
872 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
873 static void next_overlay_string (struct it *);
874 static void reseat (struct it *, struct text_pos, int);
875 static void reseat_1 (struct it *, struct text_pos, int);
876 static void back_to_previous_visible_line_start (struct it *);
877 void reseat_at_previous_visible_line_start (struct it *);
878 static void reseat_at_next_visible_line_start (struct it *, int);
879 static int next_element_from_ellipsis (struct it *);
880 static int next_element_from_display_vector (struct it *);
881 static int next_element_from_string (struct it *);
882 static int next_element_from_c_string (struct it *);
883 static int next_element_from_buffer (struct it *);
884 static int next_element_from_composition (struct it *);
885 static int next_element_from_image (struct it *);
886 static int next_element_from_stretch (struct it *);
887 static void load_overlay_strings (struct it *, EMACS_INT);
888 static int init_from_display_pos (struct it *, struct window *,
889 struct display_pos *);
890 static void reseat_to_string (struct it *, const char *,
891 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
892 static int get_next_display_element (struct it *);
893 static enum move_it_result
894 move_it_in_display_line_to (struct it *, EMACS_INT, int,
895 enum move_operation_enum);
896 void move_it_vertically_backward (struct it *, int);
897 static void init_to_row_start (struct it *, struct window *,
898 struct glyph_row *);
899 static int init_to_row_end (struct it *, struct window *,
900 struct glyph_row *);
901 static void back_to_previous_line_start (struct it *);
902 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
903 static struct text_pos string_pos_nchars_ahead (struct text_pos,
904 Lisp_Object, EMACS_INT);
905 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
906 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
907 static EMACS_INT number_of_chars (const char *, int);
908 static void compute_stop_pos (struct it *);
909 static void compute_string_pos (struct text_pos *, struct text_pos,
910 Lisp_Object);
911 static int face_before_or_after_it_pos (struct it *, int);
912 static EMACS_INT next_overlay_change (EMACS_INT);
913 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
914 Lisp_Object, struct text_pos *, EMACS_INT, int);
915 static int handle_single_display_spec (struct it *, Lisp_Object,
916 Lisp_Object, Lisp_Object,
917 struct text_pos *, EMACS_INT, int, int);
918 static int underlying_face_id (struct it *);
919 static int in_ellipses_for_invisible_text_p (struct display_pos *,
920 struct window *);
921
922 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
923 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
924
925 #ifdef HAVE_WINDOW_SYSTEM
926
927 static void x_consider_frame_title (Lisp_Object);
928 static int tool_bar_lines_needed (struct frame *, int *);
929 static void update_tool_bar (struct frame *, int);
930 static void build_desired_tool_bar_string (struct frame *f);
931 static int redisplay_tool_bar (struct frame *);
932 static void display_tool_bar_line (struct it *, int);
933 static void notice_overwritten_cursor (struct window *,
934 enum glyph_row_area,
935 int, int, int, int);
936 static void append_stretch_glyph (struct it *, Lisp_Object,
937 int, int, int);
938
939
940 #endif /* HAVE_WINDOW_SYSTEM */
941
942 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
943 static int coords_in_mouse_face_p (struct window *, int, int);
944
945
946 \f
947 /***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
950
951 /* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
954
955 This is the height of W minus the height of a mode line, if any. */
956
957 int
958 window_text_bottom_y (struct window *w)
959 {
960 int height = WINDOW_TOTAL_HEIGHT (w);
961
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
965 }
966
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
970
971 int
972 window_box_width (struct window *w, int area)
973 {
974 int cols = XFASTINT (w->total_cols);
975 int pixels = 0;
976
977 if (!w->pseudo_window_p)
978 {
979 cols -= WINDOW_SCROLL_BAR_COLS (w);
980
981 if (area == TEXT_AREA)
982 {
983 if (INTEGERP (w->left_margin_cols))
984 cols -= XFASTINT (w->left_margin_cols);
985 if (INTEGERP (w->right_margin_cols))
986 cols -= XFASTINT (w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
988 }
989 else if (area == LEFT_MARGIN_AREA)
990 {
991 cols = (INTEGERP (w->left_margin_cols)
992 ? XFASTINT (w->left_margin_cols) : 0);
993 pixels = 0;
994 }
995 else if (area == RIGHT_MARGIN_AREA)
996 {
997 cols = (INTEGERP (w->right_margin_cols)
998 ? XFASTINT (w->right_margin_cols) : 0);
999 pixels = 0;
1000 }
1001 }
1002
1003 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1004 }
1005
1006
1007 /* Return the pixel height of the display area of window W, not
1008 including mode lines of W, if any. */
1009
1010 int
1011 window_box_height (struct window *w)
1012 {
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1015
1016 xassert (height >= 0);
1017
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1023
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 {
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1034 }
1035
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1037 {
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1046 }
1047
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1051 }
1052
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1056
1057 int
1058 window_box_left_offset (struct window *w, int area)
1059 {
1060 int x;
1061
1062 if (w->pseudo_window_p)
1063 return 0;
1064
1065 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1066
1067 if (area == TEXT_AREA)
1068 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1069 + window_box_width (w, LEFT_MARGIN_AREA));
1070 else if (area == RIGHT_MARGIN_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA)
1073 + window_box_width (w, TEXT_AREA)
1074 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1075 ? 0
1076 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1077 else if (area == LEFT_MARGIN_AREA
1078 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1079 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1080
1081 return x;
1082 }
1083
1084
1085 /* Return the window-relative coordinate of the right edge of display
1086 area AREA of window W. AREA < 0 means return the right edge of the
1087 whole window, to the left of the right fringe of W. */
1088
1089 int
1090 window_box_right_offset (struct window *w, int area)
1091 {
1092 return window_box_left_offset (w, area) + window_box_width (w, area);
1093 }
1094
1095 /* Return the frame-relative coordinate of the left edge of display
1096 area AREA of window W. AREA < 0 means return the left edge of the
1097 whole window, to the right of the left fringe of W. */
1098
1099 int
1100 window_box_left (struct window *w, int area)
1101 {
1102 struct frame *f = XFRAME (w->frame);
1103 int x;
1104
1105 if (w->pseudo_window_p)
1106 return FRAME_INTERNAL_BORDER_WIDTH (f);
1107
1108 x = (WINDOW_LEFT_EDGE_X (w)
1109 + window_box_left_offset (w, area));
1110
1111 return x;
1112 }
1113
1114
1115 /* Return the frame-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1118
1119 int
1120 window_box_right (struct window *w, int area)
1121 {
1122 return window_box_left (w, area) + window_box_width (w, area);
1123 }
1124
1125 /* Get the bounding box of the display area AREA of window W, without
1126 mode lines, in frame-relative coordinates. AREA < 0 means the
1127 whole window, not including the left and right fringes of
1128 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1129 coordinates of the upper-left corner of the box. Return in
1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1131
1132 void
1133 window_box (struct window *w, int area, int *box_x, int *box_y,
1134 int *box_width, int *box_height)
1135 {
1136 if (box_width)
1137 *box_width = window_box_width (w, area);
1138 if (box_height)
1139 *box_height = window_box_height (w);
1140 if (box_x)
1141 *box_x = window_box_left (w, area);
1142 if (box_y)
1143 {
1144 *box_y = WINDOW_TOP_EDGE_Y (w);
1145 if (WINDOW_WANTS_HEADER_LINE_P (w))
1146 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1147 }
1148 }
1149
1150
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines. AREA < 0 means the whole window, not including the
1153 left and right fringe of the window. Return in *TOP_LEFT_X
1154 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1155 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1156 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1157 box. */
1158
1159 static inline void
1160 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1161 int *bottom_right_x, int *bottom_right_y)
1162 {
1163 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1164 bottom_right_y);
1165 *bottom_right_x += *top_left_x;
1166 *bottom_right_y += *top_left_y;
1167 }
1168
1169
1170 \f
1171 /***********************************************************************
1172 Utilities
1173 ***********************************************************************/
1174
1175 /* Return the bottom y-position of the line the iterator IT is in.
1176 This can modify IT's settings. */
1177
1178 int
1179 line_bottom_y (struct it *it)
1180 {
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1183
1184 if (line_height == 0)
1185 {
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1189 {
1190 move_it_by_lines (it, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1194 }
1195 else
1196 {
1197 struct glyph_row *row = it->glyph_row;
1198
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1207 }
1208 }
1209
1210 return line_top_y + line_height;
1211 }
1212
1213 /* Subroutine of pos_visible_p below. Extracts a display string, if
1214 any, from the display spec given as its argument. */
1215 static Lisp_Object
1216 string_from_display_spec (Lisp_Object spec)
1217 {
1218 if (CONSP (spec))
1219 {
1220 while (CONSP (spec))
1221 {
1222 if (STRINGP (XCAR (spec)))
1223 return XCAR (spec);
1224 spec = XCDR (spec);
1225 }
1226 }
1227 else if (VECTORP (spec))
1228 {
1229 ptrdiff_t i;
1230
1231 for (i = 0; i < ASIZE (spec); i++)
1232 {
1233 if (STRINGP (AREF (spec, i)))
1234 return AREF (spec, i);
1235 }
1236 return Qnil;
1237 }
1238
1239 return spec;
1240 }
1241
1242 /* Return 1 if position CHARPOS is visible in window W.
1243 CHARPOS < 0 means return info about WINDOW_END position.
1244 If visible, set *X and *Y to pixel coordinates of top left corner.
1245 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1246 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1247
1248 int
1249 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1250 int *rtop, int *rbot, int *rowh, int *vpos)
1251 {
1252 struct it it;
1253 void *itdata = bidi_shelve_cache ();
1254 struct text_pos top;
1255 int visible_p = 0;
1256 struct buffer *old_buffer = NULL;
1257
1258 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1259 return visible_p;
1260
1261 if (XBUFFER (w->buffer) != current_buffer)
1262 {
1263 old_buffer = current_buffer;
1264 set_buffer_internal_1 (XBUFFER (w->buffer));
1265 }
1266
1267 SET_TEXT_POS_FROM_MARKER (top, w->start);
1268
1269 /* Compute exact mode line heights. */
1270 if (WINDOW_WANTS_MODELINE_P (w))
1271 current_mode_line_height
1272 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1273 BVAR (current_buffer, mode_line_format));
1274
1275 if (WINDOW_WANTS_HEADER_LINE_P (w))
1276 current_header_line_height
1277 = display_mode_line (w, HEADER_LINE_FACE_ID,
1278 BVAR (current_buffer, header_line_format));
1279
1280 start_display (&it, w, top);
1281 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1282 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1283
1284 if (charpos >= 0
1285 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1286 && IT_CHARPOS (it) >= charpos)
1287 /* When scanning backwards under bidi iteration, move_it_to
1288 stops at or _before_ CHARPOS, because it stops at or to
1289 the _right_ of the character at CHARPOS. */
1290 || (it.bidi_p && it.bidi_it.scan_dir == -1
1291 && IT_CHARPOS (it) <= charpos)))
1292 {
1293 /* We have reached CHARPOS, or passed it. How the call to
1294 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1295 or covered by a display property, move_it_to stops at the end
1296 of the invisible text, to the right of CHARPOS. (ii) If
1297 CHARPOS is in a display vector, move_it_to stops on its last
1298 glyph. */
1299 int top_x = it.current_x;
1300 int top_y = it.current_y;
1301 enum it_method it_method = it.method;
1302 /* Calling line_bottom_y may change it.method, it.position, etc. */
1303 int bottom_y = (last_height = 0, line_bottom_y (&it));
1304 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1305
1306 if (top_y < window_top_y)
1307 visible_p = bottom_y > window_top_y;
1308 else if (top_y < it.last_visible_y)
1309 visible_p = 1;
1310 if (visible_p)
1311 {
1312 if (it_method == GET_FROM_DISPLAY_VECTOR)
1313 {
1314 /* We stopped on the last glyph of a display vector.
1315 Try and recompute. Hack alert! */
1316 if (charpos < 2 || top.charpos >= charpos)
1317 top_x = it.glyph_row->x;
1318 else
1319 {
1320 struct it it2;
1321 start_display (&it2, w, top);
1322 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1323 get_next_display_element (&it2);
1324 PRODUCE_GLYPHS (&it2);
1325 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1326 || it2.current_x > it2.last_visible_x)
1327 top_x = it.glyph_row->x;
1328 else
1329 {
1330 top_x = it2.current_x;
1331 top_y = it2.current_y;
1332 }
1333 }
1334 }
1335 else if (IT_CHARPOS (it) != charpos)
1336 {
1337 Lisp_Object cpos = make_number (charpos);
1338 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1339 Lisp_Object string = string_from_display_spec (spec);
1340 int newline_in_string = 0;
1341
1342 if (STRINGP (string))
1343 {
1344 const char *s = SSDATA (string);
1345 const char *e = s + SBYTES (string);
1346 while (s < e)
1347 {
1348 if (*s++ == '\n')
1349 {
1350 newline_in_string = 1;
1351 break;
1352 }
1353 }
1354 }
1355 /* The tricky code below is needed because there's a
1356 discrepancy between move_it_to and how we set cursor
1357 when the display line ends in a newline from a
1358 display string. move_it_to will stop _after_ such
1359 display strings, whereas set_cursor_from_row
1360 conspires with cursor_row_p to place the cursor on
1361 the first glyph produced from the display string. */
1362
1363 /* We have overshoot PT because it is covered by a
1364 display property whose value is a string. If the
1365 string includes embedded newlines, we are also in the
1366 wrong display line. Backtrack to the correct line,
1367 where the display string begins. */
1368 if (newline_in_string)
1369 {
1370 Lisp_Object startpos, endpos;
1371 EMACS_INT start, end;
1372 struct it it3;
1373
1374 /* Find the first and the last buffer positions
1375 covered by the display string. */
1376 endpos =
1377 Fnext_single_char_property_change (cpos, Qdisplay,
1378 Qnil, Qnil);
1379 startpos =
1380 Fprevious_single_char_property_change (endpos, Qdisplay,
1381 Qnil, Qnil);
1382 start = XFASTINT (startpos);
1383 end = XFASTINT (endpos);
1384 /* Move to the last buffer position before the
1385 display property. */
1386 start_display (&it3, w, top);
1387 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1388 /* Move forward one more line if the position before
1389 the display string is a newline or if it is the
1390 rightmost character on a line that is
1391 continued or word-wrapped. */
1392 if (it3.method == GET_FROM_BUFFER
1393 && it3.c == '\n')
1394 move_it_by_lines (&it3, 1);
1395 else if (move_it_in_display_line_to (&it3, -1,
1396 it3.current_x
1397 + it3.pixel_width,
1398 MOVE_TO_X)
1399 == MOVE_LINE_CONTINUED)
1400 {
1401 move_it_by_lines (&it3, 1);
1402 /* When we are under word-wrap, the #$@%!
1403 move_it_by_lines moves 2 lines, so we need to
1404 fix that up. */
1405 if (it3.line_wrap == WORD_WRAP)
1406 move_it_by_lines (&it3, -1);
1407 }
1408
1409 /* Record the vertical coordinate of the display
1410 line where we wound up. */
1411 top_y = it3.current_y;
1412 if (it3.bidi_p)
1413 {
1414 /* When characters are reordered for display,
1415 the character displayed to the left of the
1416 display string could be _after_ the display
1417 property in the logical order. Use the
1418 smallest vertical position of these two. */
1419 start_display (&it3, w, top);
1420 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1421 if (it3.current_y < top_y)
1422 top_y = it3.current_y;
1423 }
1424 /* Move from the top of the window to the beginning
1425 of the display line where the display string
1426 begins. */
1427 start_display (&it3, w, top);
1428 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1429 /* Finally, advance the iterator until we hit the
1430 first display element whose character position is
1431 CHARPOS, or until the first newline from the
1432 display string, which signals the end of the
1433 display line. */
1434 while (get_next_display_element (&it3))
1435 {
1436 PRODUCE_GLYPHS (&it3);
1437 if (IT_CHARPOS (it3) == charpos
1438 || ITERATOR_AT_END_OF_LINE_P (&it3))
1439 break;
1440 set_iterator_to_next (&it3, 0);
1441 }
1442 top_x = it3.current_x - it3.pixel_width;
1443 /* Normally, we would exit the above loop because we
1444 found the display element whose character
1445 position is CHARPOS. For the contingency that we
1446 didn't, and stopped at the first newline from the
1447 display string, move back over the glyphs
1448 produced from the string, until we find the
1449 rightmost glyph not from the string. */
1450 if (IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1451 {
1452 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1453 + it3.glyph_row->used[TEXT_AREA];
1454
1455 while (EQ ((g - 1)->object, string))
1456 {
1457 --g;
1458 top_x -= g->pixel_width;
1459 }
1460 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1461 + it3.glyph_row->used[TEXT_AREA]);
1462 }
1463 }
1464 }
1465
1466 *x = top_x;
1467 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1468 *rtop = max (0, window_top_y - top_y);
1469 *rbot = max (0, bottom_y - it.last_visible_y);
1470 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1471 - max (top_y, window_top_y)));
1472 *vpos = it.vpos;
1473 }
1474 }
1475 else
1476 {
1477 /* We were asked to provide info about WINDOW_END. */
1478 struct it it2;
1479 void *it2data = NULL;
1480
1481 SAVE_IT (it2, it, it2data);
1482 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1483 move_it_by_lines (&it, 1);
1484 if (charpos < IT_CHARPOS (it)
1485 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1486 {
1487 visible_p = 1;
1488 RESTORE_IT (&it2, &it2, it2data);
1489 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1490 *x = it2.current_x;
1491 *y = it2.current_y + it2.max_ascent - it2.ascent;
1492 *rtop = max (0, -it2.current_y);
1493 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1494 - it.last_visible_y));
1495 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1496 it.last_visible_y)
1497 - max (it2.current_y,
1498 WINDOW_HEADER_LINE_HEIGHT (w))));
1499 *vpos = it2.vpos;
1500 }
1501 else
1502 bidi_unshelve_cache (it2data, 1);
1503 }
1504 bidi_unshelve_cache (itdata, 0);
1505
1506 if (old_buffer)
1507 set_buffer_internal_1 (old_buffer);
1508
1509 current_header_line_height = current_mode_line_height = -1;
1510
1511 if (visible_p && XFASTINT (w->hscroll) > 0)
1512 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1513
1514 #if 0
1515 /* Debugging code. */
1516 if (visible_p)
1517 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1518 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1519 else
1520 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1521 #endif
1522
1523 return visible_p;
1524 }
1525
1526
1527 /* Return the next character from STR. Return in *LEN the length of
1528 the character. This is like STRING_CHAR_AND_LENGTH but never
1529 returns an invalid character. If we find one, we return a `?', but
1530 with the length of the invalid character. */
1531
1532 static inline int
1533 string_char_and_length (const unsigned char *str, int *len)
1534 {
1535 int c;
1536
1537 c = STRING_CHAR_AND_LENGTH (str, *len);
1538 if (!CHAR_VALID_P (c))
1539 /* We may not change the length here because other places in Emacs
1540 don't use this function, i.e. they silently accept invalid
1541 characters. */
1542 c = '?';
1543
1544 return c;
1545 }
1546
1547
1548
1549 /* Given a position POS containing a valid character and byte position
1550 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1551
1552 static struct text_pos
1553 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1554 {
1555 xassert (STRINGP (string) && nchars >= 0);
1556
1557 if (STRING_MULTIBYTE (string))
1558 {
1559 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1560 int len;
1561
1562 while (nchars--)
1563 {
1564 string_char_and_length (p, &len);
1565 p += len;
1566 CHARPOS (pos) += 1;
1567 BYTEPOS (pos) += len;
1568 }
1569 }
1570 else
1571 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1572
1573 return pos;
1574 }
1575
1576
1577 /* Value is the text position, i.e. character and byte position,
1578 for character position CHARPOS in STRING. */
1579
1580 static inline struct text_pos
1581 string_pos (EMACS_INT charpos, Lisp_Object string)
1582 {
1583 struct text_pos pos;
1584 xassert (STRINGP (string));
1585 xassert (charpos >= 0);
1586 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1587 return pos;
1588 }
1589
1590
1591 /* Value is a text position, i.e. character and byte position, for
1592 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1593 means recognize multibyte characters. */
1594
1595 static struct text_pos
1596 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1597 {
1598 struct text_pos pos;
1599
1600 xassert (s != NULL);
1601 xassert (charpos >= 0);
1602
1603 if (multibyte_p)
1604 {
1605 int len;
1606
1607 SET_TEXT_POS (pos, 0, 0);
1608 while (charpos--)
1609 {
1610 string_char_and_length ((const unsigned char *) s, &len);
1611 s += len;
1612 CHARPOS (pos) += 1;
1613 BYTEPOS (pos) += len;
1614 }
1615 }
1616 else
1617 SET_TEXT_POS (pos, charpos, charpos);
1618
1619 return pos;
1620 }
1621
1622
1623 /* Value is the number of characters in C string S. MULTIBYTE_P
1624 non-zero means recognize multibyte characters. */
1625
1626 static EMACS_INT
1627 number_of_chars (const char *s, int multibyte_p)
1628 {
1629 EMACS_INT nchars;
1630
1631 if (multibyte_p)
1632 {
1633 EMACS_INT rest = strlen (s);
1634 int len;
1635 const unsigned char *p = (const unsigned char *) s;
1636
1637 for (nchars = 0; rest > 0; ++nchars)
1638 {
1639 string_char_and_length (p, &len);
1640 rest -= len, p += len;
1641 }
1642 }
1643 else
1644 nchars = strlen (s);
1645
1646 return nchars;
1647 }
1648
1649
1650 /* Compute byte position NEWPOS->bytepos corresponding to
1651 NEWPOS->charpos. POS is a known position in string STRING.
1652 NEWPOS->charpos must be >= POS.charpos. */
1653
1654 static void
1655 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1656 {
1657 xassert (STRINGP (string));
1658 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1659
1660 if (STRING_MULTIBYTE (string))
1661 *newpos = string_pos_nchars_ahead (pos, string,
1662 CHARPOS (*newpos) - CHARPOS (pos));
1663 else
1664 BYTEPOS (*newpos) = CHARPOS (*newpos);
1665 }
1666
1667 /* EXPORT:
1668 Return an estimation of the pixel height of mode or header lines on
1669 frame F. FACE_ID specifies what line's height to estimate. */
1670
1671 int
1672 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1673 {
1674 #ifdef HAVE_WINDOW_SYSTEM
1675 if (FRAME_WINDOW_P (f))
1676 {
1677 int height = FONT_HEIGHT (FRAME_FONT (f));
1678
1679 /* This function is called so early when Emacs starts that the face
1680 cache and mode line face are not yet initialized. */
1681 if (FRAME_FACE_CACHE (f))
1682 {
1683 struct face *face = FACE_FROM_ID (f, face_id);
1684 if (face)
1685 {
1686 if (face->font)
1687 height = FONT_HEIGHT (face->font);
1688 if (face->box_line_width > 0)
1689 height += 2 * face->box_line_width;
1690 }
1691 }
1692
1693 return height;
1694 }
1695 #endif
1696
1697 return 1;
1698 }
1699
1700 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1701 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1702 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1703 not force the value into range. */
1704
1705 void
1706 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1707 int *x, int *y, NativeRectangle *bounds, int noclip)
1708 {
1709
1710 #ifdef HAVE_WINDOW_SYSTEM
1711 if (FRAME_WINDOW_P (f))
1712 {
1713 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1714 even for negative values. */
1715 if (pix_x < 0)
1716 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1717 if (pix_y < 0)
1718 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1719
1720 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1721 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1722
1723 if (bounds)
1724 STORE_NATIVE_RECT (*bounds,
1725 FRAME_COL_TO_PIXEL_X (f, pix_x),
1726 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1727 FRAME_COLUMN_WIDTH (f) - 1,
1728 FRAME_LINE_HEIGHT (f) - 1);
1729
1730 if (!noclip)
1731 {
1732 if (pix_x < 0)
1733 pix_x = 0;
1734 else if (pix_x > FRAME_TOTAL_COLS (f))
1735 pix_x = FRAME_TOTAL_COLS (f);
1736
1737 if (pix_y < 0)
1738 pix_y = 0;
1739 else if (pix_y > FRAME_LINES (f))
1740 pix_y = FRAME_LINES (f);
1741 }
1742 }
1743 #endif
1744
1745 *x = pix_x;
1746 *y = pix_y;
1747 }
1748
1749
1750 /* Find the glyph under window-relative coordinates X/Y in window W.
1751 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1752 strings. Return in *HPOS and *VPOS the row and column number of
1753 the glyph found. Return in *AREA the glyph area containing X.
1754 Value is a pointer to the glyph found or null if X/Y is not on
1755 text, or we can't tell because W's current matrix is not up to
1756 date. */
1757
1758 static
1759 struct glyph *
1760 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1761 int *dx, int *dy, int *area)
1762 {
1763 struct glyph *glyph, *end;
1764 struct glyph_row *row = NULL;
1765 int x0, i;
1766
1767 /* Find row containing Y. Give up if some row is not enabled. */
1768 for (i = 0; i < w->current_matrix->nrows; ++i)
1769 {
1770 row = MATRIX_ROW (w->current_matrix, i);
1771 if (!row->enabled_p)
1772 return NULL;
1773 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1774 break;
1775 }
1776
1777 *vpos = i;
1778 *hpos = 0;
1779
1780 /* Give up if Y is not in the window. */
1781 if (i == w->current_matrix->nrows)
1782 return NULL;
1783
1784 /* Get the glyph area containing X. */
1785 if (w->pseudo_window_p)
1786 {
1787 *area = TEXT_AREA;
1788 x0 = 0;
1789 }
1790 else
1791 {
1792 if (x < window_box_left_offset (w, TEXT_AREA))
1793 {
1794 *area = LEFT_MARGIN_AREA;
1795 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1796 }
1797 else if (x < window_box_right_offset (w, TEXT_AREA))
1798 {
1799 *area = TEXT_AREA;
1800 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1801 }
1802 else
1803 {
1804 *area = RIGHT_MARGIN_AREA;
1805 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1806 }
1807 }
1808
1809 /* Find glyph containing X. */
1810 glyph = row->glyphs[*area];
1811 end = glyph + row->used[*area];
1812 x -= x0;
1813 while (glyph < end && x >= glyph->pixel_width)
1814 {
1815 x -= glyph->pixel_width;
1816 ++glyph;
1817 }
1818
1819 if (glyph == end)
1820 return NULL;
1821
1822 if (dx)
1823 {
1824 *dx = x;
1825 *dy = y - (row->y + row->ascent - glyph->ascent);
1826 }
1827
1828 *hpos = glyph - row->glyphs[*area];
1829 return glyph;
1830 }
1831
1832 /* Convert frame-relative x/y to coordinates relative to window W.
1833 Takes pseudo-windows into account. */
1834
1835 static void
1836 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1837 {
1838 if (w->pseudo_window_p)
1839 {
1840 /* A pseudo-window is always full-width, and starts at the
1841 left edge of the frame, plus a frame border. */
1842 struct frame *f = XFRAME (w->frame);
1843 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1844 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1845 }
1846 else
1847 {
1848 *x -= WINDOW_LEFT_EDGE_X (w);
1849 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1850 }
1851 }
1852
1853 #ifdef HAVE_WINDOW_SYSTEM
1854
1855 /* EXPORT:
1856 Return in RECTS[] at most N clipping rectangles for glyph string S.
1857 Return the number of stored rectangles. */
1858
1859 int
1860 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1861 {
1862 XRectangle r;
1863
1864 if (n <= 0)
1865 return 0;
1866
1867 if (s->row->full_width_p)
1868 {
1869 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1870 r.x = WINDOW_LEFT_EDGE_X (s->w);
1871 r.width = WINDOW_TOTAL_WIDTH (s->w);
1872
1873 /* Unless displaying a mode or menu bar line, which are always
1874 fully visible, clip to the visible part of the row. */
1875 if (s->w->pseudo_window_p)
1876 r.height = s->row->visible_height;
1877 else
1878 r.height = s->height;
1879 }
1880 else
1881 {
1882 /* This is a text line that may be partially visible. */
1883 r.x = window_box_left (s->w, s->area);
1884 r.width = window_box_width (s->w, s->area);
1885 r.height = s->row->visible_height;
1886 }
1887
1888 if (s->clip_head)
1889 if (r.x < s->clip_head->x)
1890 {
1891 if (r.width >= s->clip_head->x - r.x)
1892 r.width -= s->clip_head->x - r.x;
1893 else
1894 r.width = 0;
1895 r.x = s->clip_head->x;
1896 }
1897 if (s->clip_tail)
1898 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1899 {
1900 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1901 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1902 else
1903 r.width = 0;
1904 }
1905
1906 /* If S draws overlapping rows, it's sufficient to use the top and
1907 bottom of the window for clipping because this glyph string
1908 intentionally draws over other lines. */
1909 if (s->for_overlaps)
1910 {
1911 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1912 r.height = window_text_bottom_y (s->w) - r.y;
1913
1914 /* Alas, the above simple strategy does not work for the
1915 environments with anti-aliased text: if the same text is
1916 drawn onto the same place multiple times, it gets thicker.
1917 If the overlap we are processing is for the erased cursor, we
1918 take the intersection with the rectangle of the cursor. */
1919 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1920 {
1921 XRectangle rc, r_save = r;
1922
1923 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1924 rc.y = s->w->phys_cursor.y;
1925 rc.width = s->w->phys_cursor_width;
1926 rc.height = s->w->phys_cursor_height;
1927
1928 x_intersect_rectangles (&r_save, &rc, &r);
1929 }
1930 }
1931 else
1932 {
1933 /* Don't use S->y for clipping because it doesn't take partially
1934 visible lines into account. For example, it can be negative for
1935 partially visible lines at the top of a window. */
1936 if (!s->row->full_width_p
1937 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1938 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1939 else
1940 r.y = max (0, s->row->y);
1941 }
1942
1943 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1944
1945 /* If drawing the cursor, don't let glyph draw outside its
1946 advertised boundaries. Cleartype does this under some circumstances. */
1947 if (s->hl == DRAW_CURSOR)
1948 {
1949 struct glyph *glyph = s->first_glyph;
1950 int height, max_y;
1951
1952 if (s->x > r.x)
1953 {
1954 r.width -= s->x - r.x;
1955 r.x = s->x;
1956 }
1957 r.width = min (r.width, glyph->pixel_width);
1958
1959 /* If r.y is below window bottom, ensure that we still see a cursor. */
1960 height = min (glyph->ascent + glyph->descent,
1961 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1962 max_y = window_text_bottom_y (s->w) - height;
1963 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1964 if (s->ybase - glyph->ascent > max_y)
1965 {
1966 r.y = max_y;
1967 r.height = height;
1968 }
1969 else
1970 {
1971 /* Don't draw cursor glyph taller than our actual glyph. */
1972 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1973 if (height < r.height)
1974 {
1975 max_y = r.y + r.height;
1976 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1977 r.height = min (max_y - r.y, height);
1978 }
1979 }
1980 }
1981
1982 if (s->row->clip)
1983 {
1984 XRectangle r_save = r;
1985
1986 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1987 r.width = 0;
1988 }
1989
1990 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1991 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1992 {
1993 #ifdef CONVERT_FROM_XRECT
1994 CONVERT_FROM_XRECT (r, *rects);
1995 #else
1996 *rects = r;
1997 #endif
1998 return 1;
1999 }
2000 else
2001 {
2002 /* If we are processing overlapping and allowed to return
2003 multiple clipping rectangles, we exclude the row of the glyph
2004 string from the clipping rectangle. This is to avoid drawing
2005 the same text on the environment with anti-aliasing. */
2006 #ifdef CONVERT_FROM_XRECT
2007 XRectangle rs[2];
2008 #else
2009 XRectangle *rs = rects;
2010 #endif
2011 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2012
2013 if (s->for_overlaps & OVERLAPS_PRED)
2014 {
2015 rs[i] = r;
2016 if (r.y + r.height > row_y)
2017 {
2018 if (r.y < row_y)
2019 rs[i].height = row_y - r.y;
2020 else
2021 rs[i].height = 0;
2022 }
2023 i++;
2024 }
2025 if (s->for_overlaps & OVERLAPS_SUCC)
2026 {
2027 rs[i] = r;
2028 if (r.y < row_y + s->row->visible_height)
2029 {
2030 if (r.y + r.height > row_y + s->row->visible_height)
2031 {
2032 rs[i].y = row_y + s->row->visible_height;
2033 rs[i].height = r.y + r.height - rs[i].y;
2034 }
2035 else
2036 rs[i].height = 0;
2037 }
2038 i++;
2039 }
2040
2041 n = i;
2042 #ifdef CONVERT_FROM_XRECT
2043 for (i = 0; i < n; i++)
2044 CONVERT_FROM_XRECT (rs[i], rects[i]);
2045 #endif
2046 return n;
2047 }
2048 }
2049
2050 /* EXPORT:
2051 Return in *NR the clipping rectangle for glyph string S. */
2052
2053 void
2054 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2055 {
2056 get_glyph_string_clip_rects (s, nr, 1);
2057 }
2058
2059
2060 /* EXPORT:
2061 Return the position and height of the phys cursor in window W.
2062 Set w->phys_cursor_width to width of phys cursor.
2063 */
2064
2065 void
2066 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2067 struct glyph *glyph, int *xp, int *yp, int *heightp)
2068 {
2069 struct frame *f = XFRAME (WINDOW_FRAME (w));
2070 int x, y, wd, h, h0, y0;
2071
2072 /* Compute the width of the rectangle to draw. If on a stretch
2073 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2074 rectangle as wide as the glyph, but use a canonical character
2075 width instead. */
2076 wd = glyph->pixel_width - 1;
2077 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2078 wd++; /* Why? */
2079 #endif
2080
2081 x = w->phys_cursor.x;
2082 if (x < 0)
2083 {
2084 wd += x;
2085 x = 0;
2086 }
2087
2088 if (glyph->type == STRETCH_GLYPH
2089 && !x_stretch_cursor_p)
2090 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2091 w->phys_cursor_width = wd;
2092
2093 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2094
2095 /* If y is below window bottom, ensure that we still see a cursor. */
2096 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2097
2098 h = max (h0, glyph->ascent + glyph->descent);
2099 h0 = min (h0, glyph->ascent + glyph->descent);
2100
2101 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2102 if (y < y0)
2103 {
2104 h = max (h - (y0 - y) + 1, h0);
2105 y = y0 - 1;
2106 }
2107 else
2108 {
2109 y0 = window_text_bottom_y (w) - h0;
2110 if (y > y0)
2111 {
2112 h += y - y0;
2113 y = y0;
2114 }
2115 }
2116
2117 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2118 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2119 *heightp = h;
2120 }
2121
2122 /*
2123 * Remember which glyph the mouse is over.
2124 */
2125
2126 void
2127 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2128 {
2129 Lisp_Object window;
2130 struct window *w;
2131 struct glyph_row *r, *gr, *end_row;
2132 enum window_part part;
2133 enum glyph_row_area area;
2134 int x, y, width, height;
2135
2136 /* Try to determine frame pixel position and size of the glyph under
2137 frame pixel coordinates X/Y on frame F. */
2138
2139 if (!f->glyphs_initialized_p
2140 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2141 NILP (window)))
2142 {
2143 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2144 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2145 goto virtual_glyph;
2146 }
2147
2148 w = XWINDOW (window);
2149 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2150 height = WINDOW_FRAME_LINE_HEIGHT (w);
2151
2152 x = window_relative_x_coord (w, part, gx);
2153 y = gy - WINDOW_TOP_EDGE_Y (w);
2154
2155 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2156 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2157
2158 if (w->pseudo_window_p)
2159 {
2160 area = TEXT_AREA;
2161 part = ON_MODE_LINE; /* Don't adjust margin. */
2162 goto text_glyph;
2163 }
2164
2165 switch (part)
2166 {
2167 case ON_LEFT_MARGIN:
2168 area = LEFT_MARGIN_AREA;
2169 goto text_glyph;
2170
2171 case ON_RIGHT_MARGIN:
2172 area = RIGHT_MARGIN_AREA;
2173 goto text_glyph;
2174
2175 case ON_HEADER_LINE:
2176 case ON_MODE_LINE:
2177 gr = (part == ON_HEADER_LINE
2178 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2179 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2180 gy = gr->y;
2181 area = TEXT_AREA;
2182 goto text_glyph_row_found;
2183
2184 case ON_TEXT:
2185 area = TEXT_AREA;
2186
2187 text_glyph:
2188 gr = 0; gy = 0;
2189 for (; r <= end_row && r->enabled_p; ++r)
2190 if (r->y + r->height > y)
2191 {
2192 gr = r; gy = r->y;
2193 break;
2194 }
2195
2196 text_glyph_row_found:
2197 if (gr && gy <= y)
2198 {
2199 struct glyph *g = gr->glyphs[area];
2200 struct glyph *end = g + gr->used[area];
2201
2202 height = gr->height;
2203 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2204 if (gx + g->pixel_width > x)
2205 break;
2206
2207 if (g < end)
2208 {
2209 if (g->type == IMAGE_GLYPH)
2210 {
2211 /* Don't remember when mouse is over image, as
2212 image may have hot-spots. */
2213 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2214 return;
2215 }
2216 width = g->pixel_width;
2217 }
2218 else
2219 {
2220 /* Use nominal char spacing at end of line. */
2221 x -= gx;
2222 gx += (x / width) * width;
2223 }
2224
2225 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2226 gx += window_box_left_offset (w, area);
2227 }
2228 else
2229 {
2230 /* Use nominal line height at end of window. */
2231 gx = (x / width) * width;
2232 y -= gy;
2233 gy += (y / height) * height;
2234 }
2235 break;
2236
2237 case ON_LEFT_FRINGE:
2238 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2239 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2240 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2241 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2242 goto row_glyph;
2243
2244 case ON_RIGHT_FRINGE:
2245 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2246 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2247 : window_box_right_offset (w, TEXT_AREA));
2248 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2249 goto row_glyph;
2250
2251 case ON_SCROLL_BAR:
2252 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2253 ? 0
2254 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2255 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2256 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2257 : 0)));
2258 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2259
2260 row_glyph:
2261 gr = 0, gy = 0;
2262 for (; r <= end_row && r->enabled_p; ++r)
2263 if (r->y + r->height > y)
2264 {
2265 gr = r; gy = r->y;
2266 break;
2267 }
2268
2269 if (gr && gy <= y)
2270 height = gr->height;
2271 else
2272 {
2273 /* Use nominal line height at end of window. */
2274 y -= gy;
2275 gy += (y / height) * height;
2276 }
2277 break;
2278
2279 default:
2280 ;
2281 virtual_glyph:
2282 /* If there is no glyph under the mouse, then we divide the screen
2283 into a grid of the smallest glyph in the frame, and use that
2284 as our "glyph". */
2285
2286 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2287 round down even for negative values. */
2288 if (gx < 0)
2289 gx -= width - 1;
2290 if (gy < 0)
2291 gy -= height - 1;
2292
2293 gx = (gx / width) * width;
2294 gy = (gy / height) * height;
2295
2296 goto store_rect;
2297 }
2298
2299 gx += WINDOW_LEFT_EDGE_X (w);
2300 gy += WINDOW_TOP_EDGE_Y (w);
2301
2302 store_rect:
2303 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2304
2305 /* Visible feedback for debugging. */
2306 #if 0
2307 #if HAVE_X_WINDOWS
2308 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2309 f->output_data.x->normal_gc,
2310 gx, gy, width, height);
2311 #endif
2312 #endif
2313 }
2314
2315
2316 #endif /* HAVE_WINDOW_SYSTEM */
2317
2318 \f
2319 /***********************************************************************
2320 Lisp form evaluation
2321 ***********************************************************************/
2322
2323 /* Error handler for safe_eval and safe_call. */
2324
2325 static Lisp_Object
2326 safe_eval_handler (Lisp_Object arg)
2327 {
2328 add_to_log ("Error during redisplay: %S", arg, Qnil);
2329 return Qnil;
2330 }
2331
2332
2333 /* Evaluate SEXPR and return the result, or nil if something went
2334 wrong. Prevent redisplay during the evaluation. */
2335
2336 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2337 Return the result, or nil if something went wrong. Prevent
2338 redisplay during the evaluation. */
2339
2340 Lisp_Object
2341 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2342 {
2343 Lisp_Object val;
2344
2345 if (inhibit_eval_during_redisplay)
2346 val = Qnil;
2347 else
2348 {
2349 int count = SPECPDL_INDEX ();
2350 struct gcpro gcpro1;
2351
2352 GCPRO1 (args[0]);
2353 gcpro1.nvars = nargs;
2354 specbind (Qinhibit_redisplay, Qt);
2355 /* Use Qt to ensure debugger does not run,
2356 so there is no possibility of wanting to redisplay. */
2357 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2358 safe_eval_handler);
2359 UNGCPRO;
2360 val = unbind_to (count, val);
2361 }
2362
2363 return val;
2364 }
2365
2366
2367 /* Call function FN with one argument ARG.
2368 Return the result, or nil if something went wrong. */
2369
2370 Lisp_Object
2371 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2372 {
2373 Lisp_Object args[2];
2374 args[0] = fn;
2375 args[1] = arg;
2376 return safe_call (2, args);
2377 }
2378
2379 static Lisp_Object Qeval;
2380
2381 Lisp_Object
2382 safe_eval (Lisp_Object sexpr)
2383 {
2384 return safe_call1 (Qeval, sexpr);
2385 }
2386
2387 /* Call function FN with one argument ARG.
2388 Return the result, or nil if something went wrong. */
2389
2390 Lisp_Object
2391 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2392 {
2393 Lisp_Object args[3];
2394 args[0] = fn;
2395 args[1] = arg1;
2396 args[2] = arg2;
2397 return safe_call (3, args);
2398 }
2399
2400
2401 \f
2402 /***********************************************************************
2403 Debugging
2404 ***********************************************************************/
2405
2406 #if 0
2407
2408 /* Define CHECK_IT to perform sanity checks on iterators.
2409 This is for debugging. It is too slow to do unconditionally. */
2410
2411 static void
2412 check_it (struct it *it)
2413 {
2414 if (it->method == GET_FROM_STRING)
2415 {
2416 xassert (STRINGP (it->string));
2417 xassert (IT_STRING_CHARPOS (*it) >= 0);
2418 }
2419 else
2420 {
2421 xassert (IT_STRING_CHARPOS (*it) < 0);
2422 if (it->method == GET_FROM_BUFFER)
2423 {
2424 /* Check that character and byte positions agree. */
2425 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2426 }
2427 }
2428
2429 if (it->dpvec)
2430 xassert (it->current.dpvec_index >= 0);
2431 else
2432 xassert (it->current.dpvec_index < 0);
2433 }
2434
2435 #define CHECK_IT(IT) check_it ((IT))
2436
2437 #else /* not 0 */
2438
2439 #define CHECK_IT(IT) (void) 0
2440
2441 #endif /* not 0 */
2442
2443
2444 #if GLYPH_DEBUG && XASSERTS
2445
2446 /* Check that the window end of window W is what we expect it
2447 to be---the last row in the current matrix displaying text. */
2448
2449 static void
2450 check_window_end (struct window *w)
2451 {
2452 if (!MINI_WINDOW_P (w)
2453 && !NILP (w->window_end_valid))
2454 {
2455 struct glyph_row *row;
2456 xassert ((row = MATRIX_ROW (w->current_matrix,
2457 XFASTINT (w->window_end_vpos)),
2458 !row->enabled_p
2459 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2460 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2461 }
2462 }
2463
2464 #define CHECK_WINDOW_END(W) check_window_end ((W))
2465
2466 #else
2467
2468 #define CHECK_WINDOW_END(W) (void) 0
2469
2470 #endif
2471
2472
2473 \f
2474 /***********************************************************************
2475 Iterator initialization
2476 ***********************************************************************/
2477
2478 /* Initialize IT for displaying current_buffer in window W, starting
2479 at character position CHARPOS. CHARPOS < 0 means that no buffer
2480 position is specified which is useful when the iterator is assigned
2481 a position later. BYTEPOS is the byte position corresponding to
2482 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2483
2484 If ROW is not null, calls to produce_glyphs with IT as parameter
2485 will produce glyphs in that row.
2486
2487 BASE_FACE_ID is the id of a base face to use. It must be one of
2488 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2489 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2490 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2491
2492 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2493 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2494 will be initialized to use the corresponding mode line glyph row of
2495 the desired matrix of W. */
2496
2497 void
2498 init_iterator (struct it *it, struct window *w,
2499 EMACS_INT charpos, EMACS_INT bytepos,
2500 struct glyph_row *row, enum face_id base_face_id)
2501 {
2502 int highlight_region_p;
2503 enum face_id remapped_base_face_id = base_face_id;
2504
2505 /* Some precondition checks. */
2506 xassert (w != NULL && it != NULL);
2507 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2508 && charpos <= ZV));
2509
2510 /* If face attributes have been changed since the last redisplay,
2511 free realized faces now because they depend on face definitions
2512 that might have changed. Don't free faces while there might be
2513 desired matrices pending which reference these faces. */
2514 if (face_change_count && !inhibit_free_realized_faces)
2515 {
2516 face_change_count = 0;
2517 free_all_realized_faces (Qnil);
2518 }
2519
2520 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2521 if (! NILP (Vface_remapping_alist))
2522 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2523
2524 /* Use one of the mode line rows of W's desired matrix if
2525 appropriate. */
2526 if (row == NULL)
2527 {
2528 if (base_face_id == MODE_LINE_FACE_ID
2529 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2530 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2531 else if (base_face_id == HEADER_LINE_FACE_ID)
2532 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2533 }
2534
2535 /* Clear IT. */
2536 memset (it, 0, sizeof *it);
2537 it->current.overlay_string_index = -1;
2538 it->current.dpvec_index = -1;
2539 it->base_face_id = remapped_base_face_id;
2540 it->string = Qnil;
2541 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2542 it->paragraph_embedding = L2R;
2543 it->bidi_it.string.lstring = Qnil;
2544 it->bidi_it.string.s = NULL;
2545 it->bidi_it.string.bufpos = 0;
2546
2547 /* The window in which we iterate over current_buffer: */
2548 XSETWINDOW (it->window, w);
2549 it->w = w;
2550 it->f = XFRAME (w->frame);
2551
2552 it->cmp_it.id = -1;
2553
2554 /* Extra space between lines (on window systems only). */
2555 if (base_face_id == DEFAULT_FACE_ID
2556 && FRAME_WINDOW_P (it->f))
2557 {
2558 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2559 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2560 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2561 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2562 * FRAME_LINE_HEIGHT (it->f));
2563 else if (it->f->extra_line_spacing > 0)
2564 it->extra_line_spacing = it->f->extra_line_spacing;
2565 it->max_extra_line_spacing = 0;
2566 }
2567
2568 /* If realized faces have been removed, e.g. because of face
2569 attribute changes of named faces, recompute them. When running
2570 in batch mode, the face cache of the initial frame is null. If
2571 we happen to get called, make a dummy face cache. */
2572 if (FRAME_FACE_CACHE (it->f) == NULL)
2573 init_frame_faces (it->f);
2574 if (FRAME_FACE_CACHE (it->f)->used == 0)
2575 recompute_basic_faces (it->f);
2576
2577 /* Current value of the `slice', `space-width', and 'height' properties. */
2578 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2579 it->space_width = Qnil;
2580 it->font_height = Qnil;
2581 it->override_ascent = -1;
2582
2583 /* Are control characters displayed as `^C'? */
2584 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2585
2586 /* -1 means everything between a CR and the following line end
2587 is invisible. >0 means lines indented more than this value are
2588 invisible. */
2589 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2590 ? XINT (BVAR (current_buffer, selective_display))
2591 : (!NILP (BVAR (current_buffer, selective_display))
2592 ? -1 : 0));
2593 it->selective_display_ellipsis_p
2594 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2595
2596 /* Display table to use. */
2597 it->dp = window_display_table (w);
2598
2599 /* Are multibyte characters enabled in current_buffer? */
2600 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2601
2602 /* Non-zero if we should highlight the region. */
2603 highlight_region_p
2604 = (!NILP (Vtransient_mark_mode)
2605 && !NILP (BVAR (current_buffer, mark_active))
2606 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2607
2608 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2609 start and end of a visible region in window IT->w. Set both to
2610 -1 to indicate no region. */
2611 if (highlight_region_p
2612 /* Maybe highlight only in selected window. */
2613 && (/* Either show region everywhere. */
2614 highlight_nonselected_windows
2615 /* Or show region in the selected window. */
2616 || w == XWINDOW (selected_window)
2617 /* Or show the region if we are in the mini-buffer and W is
2618 the window the mini-buffer refers to. */
2619 || (MINI_WINDOW_P (XWINDOW (selected_window))
2620 && WINDOWP (minibuf_selected_window)
2621 && w == XWINDOW (minibuf_selected_window))))
2622 {
2623 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2624 it->region_beg_charpos = min (PT, markpos);
2625 it->region_end_charpos = max (PT, markpos);
2626 }
2627 else
2628 it->region_beg_charpos = it->region_end_charpos = -1;
2629
2630 /* Get the position at which the redisplay_end_trigger hook should
2631 be run, if it is to be run at all. */
2632 if (MARKERP (w->redisplay_end_trigger)
2633 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2634 it->redisplay_end_trigger_charpos
2635 = marker_position (w->redisplay_end_trigger);
2636 else if (INTEGERP (w->redisplay_end_trigger))
2637 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2638
2639 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2640
2641 /* Are lines in the display truncated? */
2642 if (base_face_id != DEFAULT_FACE_ID
2643 || XINT (it->w->hscroll)
2644 || (! WINDOW_FULL_WIDTH_P (it->w)
2645 && ((!NILP (Vtruncate_partial_width_windows)
2646 && !INTEGERP (Vtruncate_partial_width_windows))
2647 || (INTEGERP (Vtruncate_partial_width_windows)
2648 && (WINDOW_TOTAL_COLS (it->w)
2649 < XINT (Vtruncate_partial_width_windows))))))
2650 it->line_wrap = TRUNCATE;
2651 else if (NILP (BVAR (current_buffer, truncate_lines)))
2652 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2653 ? WINDOW_WRAP : WORD_WRAP;
2654 else
2655 it->line_wrap = TRUNCATE;
2656
2657 /* Get dimensions of truncation and continuation glyphs. These are
2658 displayed as fringe bitmaps under X, so we don't need them for such
2659 frames. */
2660 if (!FRAME_WINDOW_P (it->f))
2661 {
2662 if (it->line_wrap == TRUNCATE)
2663 {
2664 /* We will need the truncation glyph. */
2665 xassert (it->glyph_row == NULL);
2666 produce_special_glyphs (it, IT_TRUNCATION);
2667 it->truncation_pixel_width = it->pixel_width;
2668 }
2669 else
2670 {
2671 /* We will need the continuation glyph. */
2672 xassert (it->glyph_row == NULL);
2673 produce_special_glyphs (it, IT_CONTINUATION);
2674 it->continuation_pixel_width = it->pixel_width;
2675 }
2676
2677 /* Reset these values to zero because the produce_special_glyphs
2678 above has changed them. */
2679 it->pixel_width = it->ascent = it->descent = 0;
2680 it->phys_ascent = it->phys_descent = 0;
2681 }
2682
2683 /* Set this after getting the dimensions of truncation and
2684 continuation glyphs, so that we don't produce glyphs when calling
2685 produce_special_glyphs, above. */
2686 it->glyph_row = row;
2687 it->area = TEXT_AREA;
2688
2689 /* Forget any previous info about this row being reversed. */
2690 if (it->glyph_row)
2691 it->glyph_row->reversed_p = 0;
2692
2693 /* Get the dimensions of the display area. The display area
2694 consists of the visible window area plus a horizontally scrolled
2695 part to the left of the window. All x-values are relative to the
2696 start of this total display area. */
2697 if (base_face_id != DEFAULT_FACE_ID)
2698 {
2699 /* Mode lines, menu bar in terminal frames. */
2700 it->first_visible_x = 0;
2701 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2702 }
2703 else
2704 {
2705 it->first_visible_x
2706 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2707 it->last_visible_x = (it->first_visible_x
2708 + window_box_width (w, TEXT_AREA));
2709
2710 /* If we truncate lines, leave room for the truncator glyph(s) at
2711 the right margin. Otherwise, leave room for the continuation
2712 glyph(s). Truncation and continuation glyphs are not inserted
2713 for window-based redisplay. */
2714 if (!FRAME_WINDOW_P (it->f))
2715 {
2716 if (it->line_wrap == TRUNCATE)
2717 it->last_visible_x -= it->truncation_pixel_width;
2718 else
2719 it->last_visible_x -= it->continuation_pixel_width;
2720 }
2721
2722 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2723 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2724 }
2725
2726 /* Leave room for a border glyph. */
2727 if (!FRAME_WINDOW_P (it->f)
2728 && !WINDOW_RIGHTMOST_P (it->w))
2729 it->last_visible_x -= 1;
2730
2731 it->last_visible_y = window_text_bottom_y (w);
2732
2733 /* For mode lines and alike, arrange for the first glyph having a
2734 left box line if the face specifies a box. */
2735 if (base_face_id != DEFAULT_FACE_ID)
2736 {
2737 struct face *face;
2738
2739 it->face_id = remapped_base_face_id;
2740
2741 /* If we have a boxed mode line, make the first character appear
2742 with a left box line. */
2743 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2744 if (face->box != FACE_NO_BOX)
2745 it->start_of_box_run_p = 1;
2746 }
2747
2748 /* If a buffer position was specified, set the iterator there,
2749 getting overlays and face properties from that position. */
2750 if (charpos >= BUF_BEG (current_buffer))
2751 {
2752 it->end_charpos = ZV;
2753 IT_CHARPOS (*it) = charpos;
2754
2755 /* We will rely on `reseat' to set this up properly, via
2756 handle_face_prop. */
2757 it->face_id = it->base_face_id;
2758
2759 /* Compute byte position if not specified. */
2760 if (bytepos < charpos)
2761 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2762 else
2763 IT_BYTEPOS (*it) = bytepos;
2764
2765 it->start = it->current;
2766 /* Do we need to reorder bidirectional text? Not if this is a
2767 unibyte buffer: by definition, none of the single-byte
2768 characters are strong R2L, so no reordering is needed. And
2769 bidi.c doesn't support unibyte buffers anyway. Also, don't
2770 reorder while we are loading loadup.el, since the tables of
2771 character properties needed for reordering are not yet
2772 available. */
2773 it->bidi_p =
2774 NILP (Vpurify_flag)
2775 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2776 && it->multibyte_p;
2777
2778 /* If we are to reorder bidirectional text, init the bidi
2779 iterator. */
2780 if (it->bidi_p)
2781 {
2782 /* Note the paragraph direction that this buffer wants to
2783 use. */
2784 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2785 Qleft_to_right))
2786 it->paragraph_embedding = L2R;
2787 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2788 Qright_to_left))
2789 it->paragraph_embedding = R2L;
2790 else
2791 it->paragraph_embedding = NEUTRAL_DIR;
2792 bidi_unshelve_cache (NULL, 0);
2793 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2794 &it->bidi_it);
2795 }
2796
2797 /* Compute faces etc. */
2798 reseat (it, it->current.pos, 1);
2799 }
2800
2801 CHECK_IT (it);
2802 }
2803
2804
2805 /* Initialize IT for the display of window W with window start POS. */
2806
2807 void
2808 start_display (struct it *it, struct window *w, struct text_pos pos)
2809 {
2810 struct glyph_row *row;
2811 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2812
2813 row = w->desired_matrix->rows + first_vpos;
2814 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2815 it->first_vpos = first_vpos;
2816
2817 /* Don't reseat to previous visible line start if current start
2818 position is in a string or image. */
2819 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2820 {
2821 int start_at_line_beg_p;
2822 int first_y = it->current_y;
2823
2824 /* If window start is not at a line start, skip forward to POS to
2825 get the correct continuation lines width. */
2826 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2827 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2828 if (!start_at_line_beg_p)
2829 {
2830 int new_x;
2831
2832 reseat_at_previous_visible_line_start (it);
2833 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2834
2835 new_x = it->current_x + it->pixel_width;
2836
2837 /* If lines are continued, this line may end in the middle
2838 of a multi-glyph character (e.g. a control character
2839 displayed as \003, or in the middle of an overlay
2840 string). In this case move_it_to above will not have
2841 taken us to the start of the continuation line but to the
2842 end of the continued line. */
2843 if (it->current_x > 0
2844 && it->line_wrap != TRUNCATE /* Lines are continued. */
2845 && (/* And glyph doesn't fit on the line. */
2846 new_x > it->last_visible_x
2847 /* Or it fits exactly and we're on a window
2848 system frame. */
2849 || (new_x == it->last_visible_x
2850 && FRAME_WINDOW_P (it->f))))
2851 {
2852 if ((it->current.dpvec_index >= 0
2853 || it->current.overlay_string_index >= 0)
2854 /* If we are on a newline from a display vector or
2855 overlay string, then we are already at the end of
2856 a screen line; no need to go to the next line in
2857 that case, as this line is not really continued.
2858 (If we do go to the next line, C-e will not DTRT.) */
2859 && it->c != '\n')
2860 {
2861 set_iterator_to_next (it, 1);
2862 move_it_in_display_line_to (it, -1, -1, 0);
2863 }
2864
2865 it->continuation_lines_width += it->current_x;
2866 }
2867 /* If the character at POS is displayed via a display
2868 vector, move_it_to above stops at the final glyph of
2869 IT->dpvec. To make the caller redisplay that character
2870 again (a.k.a. start at POS), we need to reset the
2871 dpvec_index to the beginning of IT->dpvec. */
2872 else if (it->current.dpvec_index >= 0)
2873 it->current.dpvec_index = 0;
2874
2875 /* We're starting a new display line, not affected by the
2876 height of the continued line, so clear the appropriate
2877 fields in the iterator structure. */
2878 it->max_ascent = it->max_descent = 0;
2879 it->max_phys_ascent = it->max_phys_descent = 0;
2880
2881 it->current_y = first_y;
2882 it->vpos = 0;
2883 it->current_x = it->hpos = 0;
2884 }
2885 }
2886 }
2887
2888
2889 /* Return 1 if POS is a position in ellipses displayed for invisible
2890 text. W is the window we display, for text property lookup. */
2891
2892 static int
2893 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2894 {
2895 Lisp_Object prop, window;
2896 int ellipses_p = 0;
2897 EMACS_INT charpos = CHARPOS (pos->pos);
2898
2899 /* If POS specifies a position in a display vector, this might
2900 be for an ellipsis displayed for invisible text. We won't
2901 get the iterator set up for delivering that ellipsis unless
2902 we make sure that it gets aware of the invisible text. */
2903 if (pos->dpvec_index >= 0
2904 && pos->overlay_string_index < 0
2905 && CHARPOS (pos->string_pos) < 0
2906 && charpos > BEGV
2907 && (XSETWINDOW (window, w),
2908 prop = Fget_char_property (make_number (charpos),
2909 Qinvisible, window),
2910 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2911 {
2912 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2913 window);
2914 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2915 }
2916
2917 return ellipses_p;
2918 }
2919
2920
2921 /* Initialize IT for stepping through current_buffer in window W,
2922 starting at position POS that includes overlay string and display
2923 vector/ control character translation position information. Value
2924 is zero if there are overlay strings with newlines at POS. */
2925
2926 static int
2927 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2928 {
2929 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2930 int i, overlay_strings_with_newlines = 0;
2931
2932 /* If POS specifies a position in a display vector, this might
2933 be for an ellipsis displayed for invisible text. We won't
2934 get the iterator set up for delivering that ellipsis unless
2935 we make sure that it gets aware of the invisible text. */
2936 if (in_ellipses_for_invisible_text_p (pos, w))
2937 {
2938 --charpos;
2939 bytepos = 0;
2940 }
2941
2942 /* Keep in mind: the call to reseat in init_iterator skips invisible
2943 text, so we might end up at a position different from POS. This
2944 is only a problem when POS is a row start after a newline and an
2945 overlay starts there with an after-string, and the overlay has an
2946 invisible property. Since we don't skip invisible text in
2947 display_line and elsewhere immediately after consuming the
2948 newline before the row start, such a POS will not be in a string,
2949 but the call to init_iterator below will move us to the
2950 after-string. */
2951 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2952
2953 /* This only scans the current chunk -- it should scan all chunks.
2954 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2955 to 16 in 22.1 to make this a lesser problem. */
2956 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2957 {
2958 const char *s = SSDATA (it->overlay_strings[i]);
2959 const char *e = s + SBYTES (it->overlay_strings[i]);
2960
2961 while (s < e && *s != '\n')
2962 ++s;
2963
2964 if (s < e)
2965 {
2966 overlay_strings_with_newlines = 1;
2967 break;
2968 }
2969 }
2970
2971 /* If position is within an overlay string, set up IT to the right
2972 overlay string. */
2973 if (pos->overlay_string_index >= 0)
2974 {
2975 int relative_index;
2976
2977 /* If the first overlay string happens to have a `display'
2978 property for an image, the iterator will be set up for that
2979 image, and we have to undo that setup first before we can
2980 correct the overlay string index. */
2981 if (it->method == GET_FROM_IMAGE)
2982 pop_it (it);
2983
2984 /* We already have the first chunk of overlay strings in
2985 IT->overlay_strings. Load more until the one for
2986 pos->overlay_string_index is in IT->overlay_strings. */
2987 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2988 {
2989 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2990 it->current.overlay_string_index = 0;
2991 while (n--)
2992 {
2993 load_overlay_strings (it, 0);
2994 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2995 }
2996 }
2997
2998 it->current.overlay_string_index = pos->overlay_string_index;
2999 relative_index = (it->current.overlay_string_index
3000 % OVERLAY_STRING_CHUNK_SIZE);
3001 it->string = it->overlay_strings[relative_index];
3002 xassert (STRINGP (it->string));
3003 it->current.string_pos = pos->string_pos;
3004 it->method = GET_FROM_STRING;
3005 }
3006
3007 if (CHARPOS (pos->string_pos) >= 0)
3008 {
3009 /* Recorded position is not in an overlay string, but in another
3010 string. This can only be a string from a `display' property.
3011 IT should already be filled with that string. */
3012 it->current.string_pos = pos->string_pos;
3013 xassert (STRINGP (it->string));
3014 }
3015
3016 /* Restore position in display vector translations, control
3017 character translations or ellipses. */
3018 if (pos->dpvec_index >= 0)
3019 {
3020 if (it->dpvec == NULL)
3021 get_next_display_element (it);
3022 xassert (it->dpvec && it->current.dpvec_index == 0);
3023 it->current.dpvec_index = pos->dpvec_index;
3024 }
3025
3026 CHECK_IT (it);
3027 return !overlay_strings_with_newlines;
3028 }
3029
3030
3031 /* Initialize IT for stepping through current_buffer in window W
3032 starting at ROW->start. */
3033
3034 static void
3035 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3036 {
3037 init_from_display_pos (it, w, &row->start);
3038 it->start = row->start;
3039 it->continuation_lines_width = row->continuation_lines_width;
3040 CHECK_IT (it);
3041 }
3042
3043
3044 /* Initialize IT for stepping through current_buffer in window W
3045 starting in the line following ROW, i.e. starting at ROW->end.
3046 Value is zero if there are overlay strings with newlines at ROW's
3047 end position. */
3048
3049 static int
3050 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3051 {
3052 int success = 0;
3053
3054 if (init_from_display_pos (it, w, &row->end))
3055 {
3056 if (row->continued_p)
3057 it->continuation_lines_width
3058 = row->continuation_lines_width + row->pixel_width;
3059 CHECK_IT (it);
3060 success = 1;
3061 }
3062
3063 return success;
3064 }
3065
3066
3067
3068 \f
3069 /***********************************************************************
3070 Text properties
3071 ***********************************************************************/
3072
3073 /* Called when IT reaches IT->stop_charpos. Handle text property and
3074 overlay changes. Set IT->stop_charpos to the next position where
3075 to stop. */
3076
3077 static void
3078 handle_stop (struct it *it)
3079 {
3080 enum prop_handled handled;
3081 int handle_overlay_change_p;
3082 struct props *p;
3083
3084 it->dpvec = NULL;
3085 it->current.dpvec_index = -1;
3086 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3087 it->ignore_overlay_strings_at_pos_p = 0;
3088 it->ellipsis_p = 0;
3089
3090 /* Use face of preceding text for ellipsis (if invisible) */
3091 if (it->selective_display_ellipsis_p)
3092 it->saved_face_id = it->face_id;
3093
3094 do
3095 {
3096 handled = HANDLED_NORMALLY;
3097
3098 /* Call text property handlers. */
3099 for (p = it_props; p->handler; ++p)
3100 {
3101 handled = p->handler (it);
3102
3103 if (handled == HANDLED_RECOMPUTE_PROPS)
3104 break;
3105 else if (handled == HANDLED_RETURN)
3106 {
3107 /* We still want to show before and after strings from
3108 overlays even if the actual buffer text is replaced. */
3109 if (!handle_overlay_change_p
3110 || it->sp > 1
3111 || !get_overlay_strings_1 (it, 0, 0))
3112 {
3113 if (it->ellipsis_p)
3114 setup_for_ellipsis (it, 0);
3115 /* When handling a display spec, we might load an
3116 empty string. In that case, discard it here. We
3117 used to discard it in handle_single_display_spec,
3118 but that causes get_overlay_strings_1, above, to
3119 ignore overlay strings that we must check. */
3120 if (STRINGP (it->string) && !SCHARS (it->string))
3121 pop_it (it);
3122 return;
3123 }
3124 else if (STRINGP (it->string) && !SCHARS (it->string))
3125 pop_it (it);
3126 else
3127 {
3128 it->ignore_overlay_strings_at_pos_p = 1;
3129 it->string_from_display_prop_p = 0;
3130 it->from_disp_prop_p = 0;
3131 handle_overlay_change_p = 0;
3132 }
3133 handled = HANDLED_RECOMPUTE_PROPS;
3134 break;
3135 }
3136 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3137 handle_overlay_change_p = 0;
3138 }
3139
3140 if (handled != HANDLED_RECOMPUTE_PROPS)
3141 {
3142 /* Don't check for overlay strings below when set to deliver
3143 characters from a display vector. */
3144 if (it->method == GET_FROM_DISPLAY_VECTOR)
3145 handle_overlay_change_p = 0;
3146
3147 /* Handle overlay changes.
3148 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3149 if it finds overlays. */
3150 if (handle_overlay_change_p)
3151 handled = handle_overlay_change (it);
3152 }
3153
3154 if (it->ellipsis_p)
3155 {
3156 setup_for_ellipsis (it, 0);
3157 break;
3158 }
3159 }
3160 while (handled == HANDLED_RECOMPUTE_PROPS);
3161
3162 /* Determine where to stop next. */
3163 if (handled == HANDLED_NORMALLY)
3164 compute_stop_pos (it);
3165 }
3166
3167
3168 /* Compute IT->stop_charpos from text property and overlay change
3169 information for IT's current position. */
3170
3171 static void
3172 compute_stop_pos (struct it *it)
3173 {
3174 register INTERVAL iv, next_iv;
3175 Lisp_Object object, limit, position;
3176 EMACS_INT charpos, bytepos;
3177
3178 if (STRINGP (it->string))
3179 {
3180 /* Strings are usually short, so don't limit the search for
3181 properties. */
3182 it->stop_charpos = it->end_charpos;
3183 object = it->string;
3184 limit = Qnil;
3185 charpos = IT_STRING_CHARPOS (*it);
3186 bytepos = IT_STRING_BYTEPOS (*it);
3187 }
3188 else
3189 {
3190 EMACS_INT pos;
3191
3192 /* If end_charpos is out of range for some reason, such as a
3193 misbehaving display function, rationalize it (Bug#5984). */
3194 if (it->end_charpos > ZV)
3195 it->end_charpos = ZV;
3196 it->stop_charpos = it->end_charpos;
3197
3198 /* If next overlay change is in front of the current stop pos
3199 (which is IT->end_charpos), stop there. Note: value of
3200 next_overlay_change is point-max if no overlay change
3201 follows. */
3202 charpos = IT_CHARPOS (*it);
3203 bytepos = IT_BYTEPOS (*it);
3204 pos = next_overlay_change (charpos);
3205 if (pos < it->stop_charpos)
3206 it->stop_charpos = pos;
3207
3208 /* If showing the region, we have to stop at the region
3209 start or end because the face might change there. */
3210 if (it->region_beg_charpos > 0)
3211 {
3212 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3213 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3214 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3215 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3216 }
3217
3218 /* Set up variables for computing the stop position from text
3219 property changes. */
3220 XSETBUFFER (object, current_buffer);
3221 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3222 }
3223
3224 /* Get the interval containing IT's position. Value is a null
3225 interval if there isn't such an interval. */
3226 position = make_number (charpos);
3227 iv = validate_interval_range (object, &position, &position, 0);
3228 if (!NULL_INTERVAL_P (iv))
3229 {
3230 Lisp_Object values_here[LAST_PROP_IDX];
3231 struct props *p;
3232
3233 /* Get properties here. */
3234 for (p = it_props; p->handler; ++p)
3235 values_here[p->idx] = textget (iv->plist, *p->name);
3236
3237 /* Look for an interval following iv that has different
3238 properties. */
3239 for (next_iv = next_interval (iv);
3240 (!NULL_INTERVAL_P (next_iv)
3241 && (NILP (limit)
3242 || XFASTINT (limit) > next_iv->position));
3243 next_iv = next_interval (next_iv))
3244 {
3245 for (p = it_props; p->handler; ++p)
3246 {
3247 Lisp_Object new_value;
3248
3249 new_value = textget (next_iv->plist, *p->name);
3250 if (!EQ (values_here[p->idx], new_value))
3251 break;
3252 }
3253
3254 if (p->handler)
3255 break;
3256 }
3257
3258 if (!NULL_INTERVAL_P (next_iv))
3259 {
3260 if (INTEGERP (limit)
3261 && next_iv->position >= XFASTINT (limit))
3262 /* No text property change up to limit. */
3263 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3264 else
3265 /* Text properties change in next_iv. */
3266 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3267 }
3268 }
3269
3270 if (it->cmp_it.id < 0)
3271 {
3272 EMACS_INT stoppos = it->end_charpos;
3273
3274 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3275 stoppos = -1;
3276 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3277 stoppos, it->string);
3278 }
3279
3280 xassert (STRINGP (it->string)
3281 || (it->stop_charpos >= BEGV
3282 && it->stop_charpos >= IT_CHARPOS (*it)));
3283 }
3284
3285
3286 /* Return the position of the next overlay change after POS in
3287 current_buffer. Value is point-max if no overlay change
3288 follows. This is like `next-overlay-change' but doesn't use
3289 xmalloc. */
3290
3291 static EMACS_INT
3292 next_overlay_change (EMACS_INT pos)
3293 {
3294 ptrdiff_t i, noverlays;
3295 EMACS_INT endpos;
3296 Lisp_Object *overlays;
3297
3298 /* Get all overlays at the given position. */
3299 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3300
3301 /* If any of these overlays ends before endpos,
3302 use its ending point instead. */
3303 for (i = 0; i < noverlays; ++i)
3304 {
3305 Lisp_Object oend;
3306 EMACS_INT oendpos;
3307
3308 oend = OVERLAY_END (overlays[i]);
3309 oendpos = OVERLAY_POSITION (oend);
3310 endpos = min (endpos, oendpos);
3311 }
3312
3313 return endpos;
3314 }
3315
3316 /* How many characters forward to search for a display property or
3317 display string. Searching too far forward makes the bidi display
3318 sluggish, especially in small windows. */
3319 #define MAX_DISP_SCAN 250
3320
3321 /* Return the character position of a display string at or after
3322 position specified by POSITION. If no display string exists at or
3323 after POSITION, return ZV. A display string is either an overlay
3324 with `display' property whose value is a string, or a `display'
3325 text property whose value is a string. STRING is data about the
3326 string to iterate; if STRING->lstring is nil, we are iterating a
3327 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3328 on a GUI frame. DISP_PROP is set to zero if we searched
3329 MAX_DISP_SCAN characters forward without finding any display
3330 strings, non-zero otherwise. It is set to 2 if the display string
3331 uses any kind of `(space ...)' spec that will produce a stretch of
3332 white space in the text area. */
3333 EMACS_INT
3334 compute_display_string_pos (struct text_pos *position,
3335 struct bidi_string_data *string,
3336 int frame_window_p, int *disp_prop)
3337 {
3338 /* OBJECT = nil means current buffer. */
3339 Lisp_Object object =
3340 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3341 Lisp_Object pos, spec, limpos;
3342 int string_p = (string && (STRINGP (string->lstring) || string->s));
3343 EMACS_INT eob = string_p ? string->schars : ZV;
3344 EMACS_INT begb = string_p ? 0 : BEGV;
3345 EMACS_INT bufpos, charpos = CHARPOS (*position);
3346 EMACS_INT lim =
3347 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3348 struct text_pos tpos;
3349 int rv = 0;
3350
3351 *disp_prop = 1;
3352
3353 if (charpos >= eob
3354 /* We don't support display properties whose values are strings
3355 that have display string properties. */
3356 || string->from_disp_str
3357 /* C strings cannot have display properties. */
3358 || (string->s && !STRINGP (object)))
3359 {
3360 *disp_prop = 0;
3361 return eob;
3362 }
3363
3364 /* If the character at CHARPOS is where the display string begins,
3365 return CHARPOS. */
3366 pos = make_number (charpos);
3367 if (STRINGP (object))
3368 bufpos = string->bufpos;
3369 else
3370 bufpos = charpos;
3371 tpos = *position;
3372 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3373 && (charpos <= begb
3374 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3375 object),
3376 spec))
3377 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3378 frame_window_p)))
3379 {
3380 if (rv == 2)
3381 *disp_prop = 2;
3382 return charpos;
3383 }
3384
3385 /* Look forward for the first character with a `display' property
3386 that will replace the underlying text when displayed. */
3387 limpos = make_number (lim);
3388 do {
3389 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3390 CHARPOS (tpos) = XFASTINT (pos);
3391 if (CHARPOS (tpos) >= lim)
3392 {
3393 *disp_prop = 0;
3394 break;
3395 }
3396 if (STRINGP (object))
3397 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3398 else
3399 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3400 spec = Fget_char_property (pos, Qdisplay, object);
3401 if (!STRINGP (object))
3402 bufpos = CHARPOS (tpos);
3403 } while (NILP (spec)
3404 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3405 bufpos, frame_window_p)));
3406 if (rv == 2)
3407 *disp_prop = 2;
3408
3409 return CHARPOS (tpos);
3410 }
3411
3412 /* Return the character position of the end of the display string that
3413 started at CHARPOS. If there's no display string at CHARPOS,
3414 return -1. A display string is either an overlay with `display'
3415 property whose value is a string or a `display' text property whose
3416 value is a string. */
3417 EMACS_INT
3418 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3419 {
3420 /* OBJECT = nil means current buffer. */
3421 Lisp_Object object =
3422 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3423 Lisp_Object pos = make_number (charpos);
3424 EMACS_INT eob =
3425 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3426
3427 if (charpos >= eob || (string->s && !STRINGP (object)))
3428 return eob;
3429
3430 /* It could happen that the display property or overlay was removed
3431 since we found it in compute_display_string_pos above. One way
3432 this can happen is if JIT font-lock was called (through
3433 handle_fontified_prop), and jit-lock-functions remove text
3434 properties or overlays from the portion of buffer that includes
3435 CHARPOS. Muse mode is known to do that, for example. In this
3436 case, we return -1 to the caller, to signal that no display
3437 string is actually present at CHARPOS. See bidi_fetch_char for
3438 how this is handled.
3439
3440 An alternative would be to never look for display properties past
3441 it->stop_charpos. But neither compute_display_string_pos nor
3442 bidi_fetch_char that calls it know or care where the next
3443 stop_charpos is. */
3444 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3445 return -1;
3446
3447 /* Look forward for the first character where the `display' property
3448 changes. */
3449 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3450
3451 return XFASTINT (pos);
3452 }
3453
3454
3455 \f
3456 /***********************************************************************
3457 Fontification
3458 ***********************************************************************/
3459
3460 /* Handle changes in the `fontified' property of the current buffer by
3461 calling hook functions from Qfontification_functions to fontify
3462 regions of text. */
3463
3464 static enum prop_handled
3465 handle_fontified_prop (struct it *it)
3466 {
3467 Lisp_Object prop, pos;
3468 enum prop_handled handled = HANDLED_NORMALLY;
3469
3470 if (!NILP (Vmemory_full))
3471 return handled;
3472
3473 /* Get the value of the `fontified' property at IT's current buffer
3474 position. (The `fontified' property doesn't have a special
3475 meaning in strings.) If the value is nil, call functions from
3476 Qfontification_functions. */
3477 if (!STRINGP (it->string)
3478 && it->s == NULL
3479 && !NILP (Vfontification_functions)
3480 && !NILP (Vrun_hooks)
3481 && (pos = make_number (IT_CHARPOS (*it)),
3482 prop = Fget_char_property (pos, Qfontified, Qnil),
3483 /* Ignore the special cased nil value always present at EOB since
3484 no amount of fontifying will be able to change it. */
3485 NILP (prop) && IT_CHARPOS (*it) < Z))
3486 {
3487 int count = SPECPDL_INDEX ();
3488 Lisp_Object val;
3489 struct buffer *obuf = current_buffer;
3490 int begv = BEGV, zv = ZV;
3491 int old_clip_changed = current_buffer->clip_changed;
3492
3493 val = Vfontification_functions;
3494 specbind (Qfontification_functions, Qnil);
3495
3496 xassert (it->end_charpos == ZV);
3497
3498 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3499 safe_call1 (val, pos);
3500 else
3501 {
3502 Lisp_Object fns, fn;
3503 struct gcpro gcpro1, gcpro2;
3504
3505 fns = Qnil;
3506 GCPRO2 (val, fns);
3507
3508 for (; CONSP (val); val = XCDR (val))
3509 {
3510 fn = XCAR (val);
3511
3512 if (EQ (fn, Qt))
3513 {
3514 /* A value of t indicates this hook has a local
3515 binding; it means to run the global binding too.
3516 In a global value, t should not occur. If it
3517 does, we must ignore it to avoid an endless
3518 loop. */
3519 for (fns = Fdefault_value (Qfontification_functions);
3520 CONSP (fns);
3521 fns = XCDR (fns))
3522 {
3523 fn = XCAR (fns);
3524 if (!EQ (fn, Qt))
3525 safe_call1 (fn, pos);
3526 }
3527 }
3528 else
3529 safe_call1 (fn, pos);
3530 }
3531
3532 UNGCPRO;
3533 }
3534
3535 unbind_to (count, Qnil);
3536
3537 /* Fontification functions routinely call `save-restriction'.
3538 Normally, this tags clip_changed, which can confuse redisplay
3539 (see discussion in Bug#6671). Since we don't perform any
3540 special handling of fontification changes in the case where
3541 `save-restriction' isn't called, there's no point doing so in
3542 this case either. So, if the buffer's restrictions are
3543 actually left unchanged, reset clip_changed. */
3544 if (obuf == current_buffer)
3545 {
3546 if (begv == BEGV && zv == ZV)
3547 current_buffer->clip_changed = old_clip_changed;
3548 }
3549 /* There isn't much we can reasonably do to protect against
3550 misbehaving fontification, but here's a fig leaf. */
3551 else if (!NILP (BVAR (obuf, name)))
3552 set_buffer_internal_1 (obuf);
3553
3554 /* The fontification code may have added/removed text.
3555 It could do even a lot worse, but let's at least protect against
3556 the most obvious case where only the text past `pos' gets changed',
3557 as is/was done in grep.el where some escapes sequences are turned
3558 into face properties (bug#7876). */
3559 it->end_charpos = ZV;
3560
3561 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3562 something. This avoids an endless loop if they failed to
3563 fontify the text for which reason ever. */
3564 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3565 handled = HANDLED_RECOMPUTE_PROPS;
3566 }
3567
3568 return handled;
3569 }
3570
3571
3572 \f
3573 /***********************************************************************
3574 Faces
3575 ***********************************************************************/
3576
3577 /* Set up iterator IT from face properties at its current position.
3578 Called from handle_stop. */
3579
3580 static enum prop_handled
3581 handle_face_prop (struct it *it)
3582 {
3583 int new_face_id;
3584 EMACS_INT next_stop;
3585
3586 if (!STRINGP (it->string))
3587 {
3588 new_face_id
3589 = face_at_buffer_position (it->w,
3590 IT_CHARPOS (*it),
3591 it->region_beg_charpos,
3592 it->region_end_charpos,
3593 &next_stop,
3594 (IT_CHARPOS (*it)
3595 + TEXT_PROP_DISTANCE_LIMIT),
3596 0, it->base_face_id);
3597
3598 /* Is this a start of a run of characters with box face?
3599 Caveat: this can be called for a freshly initialized
3600 iterator; face_id is -1 in this case. We know that the new
3601 face will not change until limit, i.e. if the new face has a
3602 box, all characters up to limit will have one. But, as
3603 usual, we don't know whether limit is really the end. */
3604 if (new_face_id != it->face_id)
3605 {
3606 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3607
3608 /* If new face has a box but old face has not, this is
3609 the start of a run of characters with box, i.e. it has
3610 a shadow on the left side. The value of face_id of the
3611 iterator will be -1 if this is the initial call that gets
3612 the face. In this case, we have to look in front of IT's
3613 position and see whether there is a face != new_face_id. */
3614 it->start_of_box_run_p
3615 = (new_face->box != FACE_NO_BOX
3616 && (it->face_id >= 0
3617 || IT_CHARPOS (*it) == BEG
3618 || new_face_id != face_before_it_pos (it)));
3619 it->face_box_p = new_face->box != FACE_NO_BOX;
3620 }
3621 }
3622 else
3623 {
3624 int base_face_id;
3625 EMACS_INT bufpos;
3626 int i;
3627 Lisp_Object from_overlay
3628 = (it->current.overlay_string_index >= 0
3629 ? it->string_overlays[it->current.overlay_string_index]
3630 : Qnil);
3631
3632 /* See if we got to this string directly or indirectly from
3633 an overlay property. That includes the before-string or
3634 after-string of an overlay, strings in display properties
3635 provided by an overlay, their text properties, etc.
3636
3637 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3638 if (! NILP (from_overlay))
3639 for (i = it->sp - 1; i >= 0; i--)
3640 {
3641 if (it->stack[i].current.overlay_string_index >= 0)
3642 from_overlay
3643 = it->string_overlays[it->stack[i].current.overlay_string_index];
3644 else if (! NILP (it->stack[i].from_overlay))
3645 from_overlay = it->stack[i].from_overlay;
3646
3647 if (!NILP (from_overlay))
3648 break;
3649 }
3650
3651 if (! NILP (from_overlay))
3652 {
3653 bufpos = IT_CHARPOS (*it);
3654 /* For a string from an overlay, the base face depends
3655 only on text properties and ignores overlays. */
3656 base_face_id
3657 = face_for_overlay_string (it->w,
3658 IT_CHARPOS (*it),
3659 it->region_beg_charpos,
3660 it->region_end_charpos,
3661 &next_stop,
3662 (IT_CHARPOS (*it)
3663 + TEXT_PROP_DISTANCE_LIMIT),
3664 0,
3665 from_overlay);
3666 }
3667 else
3668 {
3669 bufpos = 0;
3670
3671 /* For strings from a `display' property, use the face at
3672 IT's current buffer position as the base face to merge
3673 with, so that overlay strings appear in the same face as
3674 surrounding text, unless they specify their own
3675 faces. */
3676 base_face_id = underlying_face_id (it);
3677 }
3678
3679 new_face_id = face_at_string_position (it->w,
3680 it->string,
3681 IT_STRING_CHARPOS (*it),
3682 bufpos,
3683 it->region_beg_charpos,
3684 it->region_end_charpos,
3685 &next_stop,
3686 base_face_id, 0);
3687
3688 /* Is this a start of a run of characters with box? Caveat:
3689 this can be called for a freshly allocated iterator; face_id
3690 is -1 is this case. We know that the new face will not
3691 change until the next check pos, i.e. if the new face has a
3692 box, all characters up to that position will have a
3693 box. But, as usual, we don't know whether that position
3694 is really the end. */
3695 if (new_face_id != it->face_id)
3696 {
3697 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3698 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3699
3700 /* If new face has a box but old face hasn't, this is the
3701 start of a run of characters with box, i.e. it has a
3702 shadow on the left side. */
3703 it->start_of_box_run_p
3704 = new_face->box && (old_face == NULL || !old_face->box);
3705 it->face_box_p = new_face->box != FACE_NO_BOX;
3706 }
3707 }
3708
3709 it->face_id = new_face_id;
3710 return HANDLED_NORMALLY;
3711 }
3712
3713
3714 /* Return the ID of the face ``underlying'' IT's current position,
3715 which is in a string. If the iterator is associated with a
3716 buffer, return the face at IT's current buffer position.
3717 Otherwise, use the iterator's base_face_id. */
3718
3719 static int
3720 underlying_face_id (struct it *it)
3721 {
3722 int face_id = it->base_face_id, i;
3723
3724 xassert (STRINGP (it->string));
3725
3726 for (i = it->sp - 1; i >= 0; --i)
3727 if (NILP (it->stack[i].string))
3728 face_id = it->stack[i].face_id;
3729
3730 return face_id;
3731 }
3732
3733
3734 /* Compute the face one character before or after the current position
3735 of IT, in the visual order. BEFORE_P non-zero means get the face
3736 in front (to the left in L2R paragraphs, to the right in R2L
3737 paragraphs) of IT's screen position. Value is the ID of the face. */
3738
3739 static int
3740 face_before_or_after_it_pos (struct it *it, int before_p)
3741 {
3742 int face_id, limit;
3743 EMACS_INT next_check_charpos;
3744 struct it it_copy;
3745 void *it_copy_data = NULL;
3746
3747 xassert (it->s == NULL);
3748
3749 if (STRINGP (it->string))
3750 {
3751 EMACS_INT bufpos, charpos;
3752 int base_face_id;
3753
3754 /* No face change past the end of the string (for the case
3755 we are padding with spaces). No face change before the
3756 string start. */
3757 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3758 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3759 return it->face_id;
3760
3761 if (!it->bidi_p)
3762 {
3763 /* Set charpos to the position before or after IT's current
3764 position, in the logical order, which in the non-bidi
3765 case is the same as the visual order. */
3766 if (before_p)
3767 charpos = IT_STRING_CHARPOS (*it) - 1;
3768 else if (it->what == IT_COMPOSITION)
3769 /* For composition, we must check the character after the
3770 composition. */
3771 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3772 else
3773 charpos = IT_STRING_CHARPOS (*it) + 1;
3774 }
3775 else
3776 {
3777 if (before_p)
3778 {
3779 /* With bidi iteration, the character before the current
3780 in the visual order cannot be found by simple
3781 iteration, because "reverse" reordering is not
3782 supported. Instead, we need to use the move_it_*
3783 family of functions. */
3784 /* Ignore face changes before the first visible
3785 character on this display line. */
3786 if (it->current_x <= it->first_visible_x)
3787 return it->face_id;
3788 SAVE_IT (it_copy, *it, it_copy_data);
3789 /* Implementation note: Since move_it_in_display_line
3790 works in the iterator geometry, and thinks the first
3791 character is always the leftmost, even in R2L lines,
3792 we don't need to distinguish between the R2L and L2R
3793 cases here. */
3794 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3795 it_copy.current_x - 1, MOVE_TO_X);
3796 charpos = IT_STRING_CHARPOS (it_copy);
3797 RESTORE_IT (it, it, it_copy_data);
3798 }
3799 else
3800 {
3801 /* Set charpos to the string position of the character
3802 that comes after IT's current position in the visual
3803 order. */
3804 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3805
3806 it_copy = *it;
3807 while (n--)
3808 bidi_move_to_visually_next (&it_copy.bidi_it);
3809
3810 charpos = it_copy.bidi_it.charpos;
3811 }
3812 }
3813 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3814
3815 if (it->current.overlay_string_index >= 0)
3816 bufpos = IT_CHARPOS (*it);
3817 else
3818 bufpos = 0;
3819
3820 base_face_id = underlying_face_id (it);
3821
3822 /* Get the face for ASCII, or unibyte. */
3823 face_id = face_at_string_position (it->w,
3824 it->string,
3825 charpos,
3826 bufpos,
3827 it->region_beg_charpos,
3828 it->region_end_charpos,
3829 &next_check_charpos,
3830 base_face_id, 0);
3831
3832 /* Correct the face for charsets different from ASCII. Do it
3833 for the multibyte case only. The face returned above is
3834 suitable for unibyte text if IT->string is unibyte. */
3835 if (STRING_MULTIBYTE (it->string))
3836 {
3837 struct text_pos pos1 = string_pos (charpos, it->string);
3838 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3839 int c, len;
3840 struct face *face = FACE_FROM_ID (it->f, face_id);
3841
3842 c = string_char_and_length (p, &len);
3843 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3844 }
3845 }
3846 else
3847 {
3848 struct text_pos pos;
3849
3850 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3851 || (IT_CHARPOS (*it) <= BEGV && before_p))
3852 return it->face_id;
3853
3854 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3855 pos = it->current.pos;
3856
3857 if (!it->bidi_p)
3858 {
3859 if (before_p)
3860 DEC_TEXT_POS (pos, it->multibyte_p);
3861 else
3862 {
3863 if (it->what == IT_COMPOSITION)
3864 {
3865 /* For composition, we must check the position after
3866 the composition. */
3867 pos.charpos += it->cmp_it.nchars;
3868 pos.bytepos += it->len;
3869 }
3870 else
3871 INC_TEXT_POS (pos, it->multibyte_p);
3872 }
3873 }
3874 else
3875 {
3876 if (before_p)
3877 {
3878 /* With bidi iteration, the character before the current
3879 in the visual order cannot be found by simple
3880 iteration, because "reverse" reordering is not
3881 supported. Instead, we need to use the move_it_*
3882 family of functions. */
3883 /* Ignore face changes before the first visible
3884 character on this display line. */
3885 if (it->current_x <= it->first_visible_x)
3886 return it->face_id;
3887 SAVE_IT (it_copy, *it, it_copy_data);
3888 /* Implementation note: Since move_it_in_display_line
3889 works in the iterator geometry, and thinks the first
3890 character is always the leftmost, even in R2L lines,
3891 we don't need to distinguish between the R2L and L2R
3892 cases here. */
3893 move_it_in_display_line (&it_copy, ZV,
3894 it_copy.current_x - 1, MOVE_TO_X);
3895 pos = it_copy.current.pos;
3896 RESTORE_IT (it, it, it_copy_data);
3897 }
3898 else
3899 {
3900 /* Set charpos to the buffer position of the character
3901 that comes after IT's current position in the visual
3902 order. */
3903 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3904
3905 it_copy = *it;
3906 while (n--)
3907 bidi_move_to_visually_next (&it_copy.bidi_it);
3908
3909 SET_TEXT_POS (pos,
3910 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3911 }
3912 }
3913 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3914
3915 /* Determine face for CHARSET_ASCII, or unibyte. */
3916 face_id = face_at_buffer_position (it->w,
3917 CHARPOS (pos),
3918 it->region_beg_charpos,
3919 it->region_end_charpos,
3920 &next_check_charpos,
3921 limit, 0, -1);
3922
3923 /* Correct the face for charsets different from ASCII. Do it
3924 for the multibyte case only. The face returned above is
3925 suitable for unibyte text if current_buffer is unibyte. */
3926 if (it->multibyte_p)
3927 {
3928 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3929 struct face *face = FACE_FROM_ID (it->f, face_id);
3930 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3931 }
3932 }
3933
3934 return face_id;
3935 }
3936
3937
3938 \f
3939 /***********************************************************************
3940 Invisible text
3941 ***********************************************************************/
3942
3943 /* Set up iterator IT from invisible properties at its current
3944 position. Called from handle_stop. */
3945
3946 static enum prop_handled
3947 handle_invisible_prop (struct it *it)
3948 {
3949 enum prop_handled handled = HANDLED_NORMALLY;
3950
3951 if (STRINGP (it->string))
3952 {
3953 Lisp_Object prop, end_charpos, limit, charpos;
3954
3955 /* Get the value of the invisible text property at the
3956 current position. Value will be nil if there is no such
3957 property. */
3958 charpos = make_number (IT_STRING_CHARPOS (*it));
3959 prop = Fget_text_property (charpos, Qinvisible, it->string);
3960
3961 if (!NILP (prop)
3962 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3963 {
3964 EMACS_INT endpos;
3965
3966 handled = HANDLED_RECOMPUTE_PROPS;
3967
3968 /* Get the position at which the next change of the
3969 invisible text property can be found in IT->string.
3970 Value will be nil if the property value is the same for
3971 all the rest of IT->string. */
3972 XSETINT (limit, SCHARS (it->string));
3973 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3974 it->string, limit);
3975
3976 /* Text at current position is invisible. The next
3977 change in the property is at position end_charpos.
3978 Move IT's current position to that position. */
3979 if (INTEGERP (end_charpos)
3980 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3981 {
3982 struct text_pos old;
3983 EMACS_INT oldpos;
3984
3985 old = it->current.string_pos;
3986 oldpos = CHARPOS (old);
3987 if (it->bidi_p)
3988 {
3989 if (it->bidi_it.first_elt
3990 && it->bidi_it.charpos < SCHARS (it->string))
3991 bidi_paragraph_init (it->paragraph_embedding,
3992 &it->bidi_it, 1);
3993 /* Bidi-iterate out of the invisible text. */
3994 do
3995 {
3996 bidi_move_to_visually_next (&it->bidi_it);
3997 }
3998 while (oldpos <= it->bidi_it.charpos
3999 && it->bidi_it.charpos < endpos);
4000
4001 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4002 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4003 if (IT_CHARPOS (*it) >= endpos)
4004 it->prev_stop = endpos;
4005 }
4006 else
4007 {
4008 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4009 compute_string_pos (&it->current.string_pos, old, it->string);
4010 }
4011 }
4012 else
4013 {
4014 /* The rest of the string is invisible. If this is an
4015 overlay string, proceed with the next overlay string
4016 or whatever comes and return a character from there. */
4017 if (it->current.overlay_string_index >= 0)
4018 {
4019 next_overlay_string (it);
4020 /* Don't check for overlay strings when we just
4021 finished processing them. */
4022 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4023 }
4024 else
4025 {
4026 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4027 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4028 }
4029 }
4030 }
4031 }
4032 else
4033 {
4034 int invis_p;
4035 EMACS_INT newpos, next_stop, start_charpos, tem;
4036 Lisp_Object pos, prop, overlay;
4037
4038 /* First of all, is there invisible text at this position? */
4039 tem = start_charpos = IT_CHARPOS (*it);
4040 pos = make_number (tem);
4041 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4042 &overlay);
4043 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4044
4045 /* If we are on invisible text, skip over it. */
4046 if (invis_p && start_charpos < it->end_charpos)
4047 {
4048 /* Record whether we have to display an ellipsis for the
4049 invisible text. */
4050 int display_ellipsis_p = invis_p == 2;
4051
4052 handled = HANDLED_RECOMPUTE_PROPS;
4053
4054 /* Loop skipping over invisible text. The loop is left at
4055 ZV or with IT on the first char being visible again. */
4056 do
4057 {
4058 /* Try to skip some invisible text. Return value is the
4059 position reached which can be equal to where we start
4060 if there is nothing invisible there. This skips both
4061 over invisible text properties and overlays with
4062 invisible property. */
4063 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4064
4065 /* If we skipped nothing at all we weren't at invisible
4066 text in the first place. If everything to the end of
4067 the buffer was skipped, end the loop. */
4068 if (newpos == tem || newpos >= ZV)
4069 invis_p = 0;
4070 else
4071 {
4072 /* We skipped some characters but not necessarily
4073 all there are. Check if we ended up on visible
4074 text. Fget_char_property returns the property of
4075 the char before the given position, i.e. if we
4076 get invis_p = 0, this means that the char at
4077 newpos is visible. */
4078 pos = make_number (newpos);
4079 prop = Fget_char_property (pos, Qinvisible, it->window);
4080 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4081 }
4082
4083 /* If we ended up on invisible text, proceed to
4084 skip starting with next_stop. */
4085 if (invis_p)
4086 tem = next_stop;
4087
4088 /* If there are adjacent invisible texts, don't lose the
4089 second one's ellipsis. */
4090 if (invis_p == 2)
4091 display_ellipsis_p = 1;
4092 }
4093 while (invis_p);
4094
4095 /* The position newpos is now either ZV or on visible text. */
4096 if (it->bidi_p)
4097 {
4098 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
4099 int on_newline =
4100 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4101 int after_newline =
4102 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4103
4104 /* If the invisible text ends on a newline or on a
4105 character after a newline, we can avoid the costly,
4106 character by character, bidi iteration to NEWPOS, and
4107 instead simply reseat the iterator there. That's
4108 because all bidi reordering information is tossed at
4109 the newline. This is a big win for modes that hide
4110 complete lines, like Outline, Org, etc. */
4111 if (on_newline || after_newline)
4112 {
4113 struct text_pos tpos;
4114 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4115
4116 SET_TEXT_POS (tpos, newpos, bpos);
4117 reseat_1 (it, tpos, 0);
4118 /* If we reseat on a newline/ZV, we need to prep the
4119 bidi iterator for advancing to the next character
4120 after the newline/EOB, keeping the current paragraph
4121 direction (so that PRODUCE_GLYPHS does TRT wrt
4122 prepending/appending glyphs to a glyph row). */
4123 if (on_newline)
4124 {
4125 it->bidi_it.first_elt = 0;
4126 it->bidi_it.paragraph_dir = pdir;
4127 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4128 it->bidi_it.nchars = 1;
4129 it->bidi_it.ch_len = 1;
4130 }
4131 }
4132 else /* Must use the slow method. */
4133 {
4134 /* With bidi iteration, the region of invisible text
4135 could start and/or end in the middle of a
4136 non-base embedding level. Therefore, we need to
4137 skip invisible text using the bidi iterator,
4138 starting at IT's current position, until we find
4139 ourselves outside of the invisible text.
4140 Skipping invisible text _after_ bidi iteration
4141 avoids affecting the visual order of the
4142 displayed text when invisible properties are
4143 added or removed. */
4144 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4145 {
4146 /* If we were `reseat'ed to a new paragraph,
4147 determine the paragraph base direction. We
4148 need to do it now because
4149 next_element_from_buffer may not have a
4150 chance to do it, if we are going to skip any
4151 text at the beginning, which resets the
4152 FIRST_ELT flag. */
4153 bidi_paragraph_init (it->paragraph_embedding,
4154 &it->bidi_it, 1);
4155 }
4156 do
4157 {
4158 bidi_move_to_visually_next (&it->bidi_it);
4159 }
4160 while (it->stop_charpos <= it->bidi_it.charpos
4161 && it->bidi_it.charpos < newpos);
4162 IT_CHARPOS (*it) = it->bidi_it.charpos;
4163 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4164 /* If we overstepped NEWPOS, record its position in
4165 the iterator, so that we skip invisible text if
4166 later the bidi iteration lands us in the
4167 invisible region again. */
4168 if (IT_CHARPOS (*it) >= newpos)
4169 it->prev_stop = newpos;
4170 }
4171 }
4172 else
4173 {
4174 IT_CHARPOS (*it) = newpos;
4175 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4176 }
4177
4178 /* If there are before-strings at the start of invisible
4179 text, and the text is invisible because of a text
4180 property, arrange to show before-strings because 20.x did
4181 it that way. (If the text is invisible because of an
4182 overlay property instead of a text property, this is
4183 already handled in the overlay code.) */
4184 if (NILP (overlay)
4185 && get_overlay_strings (it, it->stop_charpos))
4186 {
4187 handled = HANDLED_RECOMPUTE_PROPS;
4188 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4189 }
4190 else if (display_ellipsis_p)
4191 {
4192 /* Make sure that the glyphs of the ellipsis will get
4193 correct `charpos' values. If we would not update
4194 it->position here, the glyphs would belong to the
4195 last visible character _before_ the invisible
4196 text, which confuses `set_cursor_from_row'.
4197
4198 We use the last invisible position instead of the
4199 first because this way the cursor is always drawn on
4200 the first "." of the ellipsis, whenever PT is inside
4201 the invisible text. Otherwise the cursor would be
4202 placed _after_ the ellipsis when the point is after the
4203 first invisible character. */
4204 if (!STRINGP (it->object))
4205 {
4206 it->position.charpos = newpos - 1;
4207 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4208 }
4209 it->ellipsis_p = 1;
4210 /* Let the ellipsis display before
4211 considering any properties of the following char.
4212 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4213 handled = HANDLED_RETURN;
4214 }
4215 }
4216 }
4217
4218 return handled;
4219 }
4220
4221
4222 /* Make iterator IT return `...' next.
4223 Replaces LEN characters from buffer. */
4224
4225 static void
4226 setup_for_ellipsis (struct it *it, int len)
4227 {
4228 /* Use the display table definition for `...'. Invalid glyphs
4229 will be handled by the method returning elements from dpvec. */
4230 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4231 {
4232 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4233 it->dpvec = v->contents;
4234 it->dpend = v->contents + v->header.size;
4235 }
4236 else
4237 {
4238 /* Default `...'. */
4239 it->dpvec = default_invis_vector;
4240 it->dpend = default_invis_vector + 3;
4241 }
4242
4243 it->dpvec_char_len = len;
4244 it->current.dpvec_index = 0;
4245 it->dpvec_face_id = -1;
4246
4247 /* Remember the current face id in case glyphs specify faces.
4248 IT's face is restored in set_iterator_to_next.
4249 saved_face_id was set to preceding char's face in handle_stop. */
4250 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4251 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4252
4253 it->method = GET_FROM_DISPLAY_VECTOR;
4254 it->ellipsis_p = 1;
4255 }
4256
4257
4258 \f
4259 /***********************************************************************
4260 'display' property
4261 ***********************************************************************/
4262
4263 /* Set up iterator IT from `display' property at its current position.
4264 Called from handle_stop.
4265 We return HANDLED_RETURN if some part of the display property
4266 overrides the display of the buffer text itself.
4267 Otherwise we return HANDLED_NORMALLY. */
4268
4269 static enum prop_handled
4270 handle_display_prop (struct it *it)
4271 {
4272 Lisp_Object propval, object, overlay;
4273 struct text_pos *position;
4274 EMACS_INT bufpos;
4275 /* Nonzero if some property replaces the display of the text itself. */
4276 int display_replaced_p = 0;
4277
4278 if (STRINGP (it->string))
4279 {
4280 object = it->string;
4281 position = &it->current.string_pos;
4282 bufpos = CHARPOS (it->current.pos);
4283 }
4284 else
4285 {
4286 XSETWINDOW (object, it->w);
4287 position = &it->current.pos;
4288 bufpos = CHARPOS (*position);
4289 }
4290
4291 /* Reset those iterator values set from display property values. */
4292 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4293 it->space_width = Qnil;
4294 it->font_height = Qnil;
4295 it->voffset = 0;
4296
4297 /* We don't support recursive `display' properties, i.e. string
4298 values that have a string `display' property, that have a string
4299 `display' property etc. */
4300 if (!it->string_from_display_prop_p)
4301 it->area = TEXT_AREA;
4302
4303 propval = get_char_property_and_overlay (make_number (position->charpos),
4304 Qdisplay, object, &overlay);
4305 if (NILP (propval))
4306 return HANDLED_NORMALLY;
4307 /* Now OVERLAY is the overlay that gave us this property, or nil
4308 if it was a text property. */
4309
4310 if (!STRINGP (it->string))
4311 object = it->w->buffer;
4312
4313 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4314 position, bufpos,
4315 FRAME_WINDOW_P (it->f));
4316
4317 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4318 }
4319
4320 /* Subroutine of handle_display_prop. Returns non-zero if the display
4321 specification in SPEC is a replacing specification, i.e. it would
4322 replace the text covered by `display' property with something else,
4323 such as an image or a display string. If SPEC includes any kind or
4324 `(space ...) specification, the value is 2; this is used by
4325 compute_display_string_pos, which see.
4326
4327 See handle_single_display_spec for documentation of arguments.
4328 frame_window_p is non-zero if the window being redisplayed is on a
4329 GUI frame; this argument is used only if IT is NULL, see below.
4330
4331 IT can be NULL, if this is called by the bidi reordering code
4332 through compute_display_string_pos, which see. In that case, this
4333 function only examines SPEC, but does not otherwise "handle" it, in
4334 the sense that it doesn't set up members of IT from the display
4335 spec. */
4336 static int
4337 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4338 Lisp_Object overlay, struct text_pos *position,
4339 EMACS_INT bufpos, int frame_window_p)
4340 {
4341 int replacing_p = 0;
4342 int rv;
4343
4344 if (CONSP (spec)
4345 /* Simple specifications. */
4346 && !EQ (XCAR (spec), Qimage)
4347 && !EQ (XCAR (spec), Qspace)
4348 && !EQ (XCAR (spec), Qwhen)
4349 && !EQ (XCAR (spec), Qslice)
4350 && !EQ (XCAR (spec), Qspace_width)
4351 && !EQ (XCAR (spec), Qheight)
4352 && !EQ (XCAR (spec), Qraise)
4353 /* Marginal area specifications. */
4354 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4355 && !EQ (XCAR (spec), Qleft_fringe)
4356 && !EQ (XCAR (spec), Qright_fringe)
4357 && !NILP (XCAR (spec)))
4358 {
4359 for (; CONSP (spec); spec = XCDR (spec))
4360 {
4361 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4362 overlay, position, bufpos,
4363 replacing_p, frame_window_p)))
4364 {
4365 replacing_p = rv;
4366 /* If some text in a string is replaced, `position' no
4367 longer points to the position of `object'. */
4368 if (!it || STRINGP (object))
4369 break;
4370 }
4371 }
4372 }
4373 else if (VECTORP (spec))
4374 {
4375 int i;
4376 for (i = 0; i < ASIZE (spec); ++i)
4377 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4378 overlay, position, bufpos,
4379 replacing_p, frame_window_p)))
4380 {
4381 replacing_p = rv;
4382 /* If some text in a string is replaced, `position' no
4383 longer points to the position of `object'. */
4384 if (!it || STRINGP (object))
4385 break;
4386 }
4387 }
4388 else
4389 {
4390 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4391 position, bufpos, 0,
4392 frame_window_p)))
4393 replacing_p = rv;
4394 }
4395
4396 return replacing_p;
4397 }
4398
4399 /* Value is the position of the end of the `display' property starting
4400 at START_POS in OBJECT. */
4401
4402 static struct text_pos
4403 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4404 {
4405 Lisp_Object end;
4406 struct text_pos end_pos;
4407
4408 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4409 Qdisplay, object, Qnil);
4410 CHARPOS (end_pos) = XFASTINT (end);
4411 if (STRINGP (object))
4412 compute_string_pos (&end_pos, start_pos, it->string);
4413 else
4414 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4415
4416 return end_pos;
4417 }
4418
4419
4420 /* Set up IT from a single `display' property specification SPEC. OBJECT
4421 is the object in which the `display' property was found. *POSITION
4422 is the position in OBJECT at which the `display' property was found.
4423 BUFPOS is the buffer position of OBJECT (different from POSITION if
4424 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4425 previously saw a display specification which already replaced text
4426 display with something else, for example an image; we ignore such
4427 properties after the first one has been processed.
4428
4429 OVERLAY is the overlay this `display' property came from,
4430 or nil if it was a text property.
4431
4432 If SPEC is a `space' or `image' specification, and in some other
4433 cases too, set *POSITION to the position where the `display'
4434 property ends.
4435
4436 If IT is NULL, only examine the property specification in SPEC, but
4437 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4438 is intended to be displayed in a window on a GUI frame.
4439
4440 Value is non-zero if something was found which replaces the display
4441 of buffer or string text. */
4442
4443 static int
4444 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4445 Lisp_Object overlay, struct text_pos *position,
4446 EMACS_INT bufpos, int display_replaced_p,
4447 int frame_window_p)
4448 {
4449 Lisp_Object form;
4450 Lisp_Object location, value;
4451 struct text_pos start_pos = *position;
4452 int valid_p;
4453
4454 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4455 If the result is non-nil, use VALUE instead of SPEC. */
4456 form = Qt;
4457 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4458 {
4459 spec = XCDR (spec);
4460 if (!CONSP (spec))
4461 return 0;
4462 form = XCAR (spec);
4463 spec = XCDR (spec);
4464 }
4465
4466 if (!NILP (form) && !EQ (form, Qt))
4467 {
4468 int count = SPECPDL_INDEX ();
4469 struct gcpro gcpro1;
4470
4471 /* Bind `object' to the object having the `display' property, a
4472 buffer or string. Bind `position' to the position in the
4473 object where the property was found, and `buffer-position'
4474 to the current position in the buffer. */
4475
4476 if (NILP (object))
4477 XSETBUFFER (object, current_buffer);
4478 specbind (Qobject, object);
4479 specbind (Qposition, make_number (CHARPOS (*position)));
4480 specbind (Qbuffer_position, make_number (bufpos));
4481 GCPRO1 (form);
4482 form = safe_eval (form);
4483 UNGCPRO;
4484 unbind_to (count, Qnil);
4485 }
4486
4487 if (NILP (form))
4488 return 0;
4489
4490 /* Handle `(height HEIGHT)' specifications. */
4491 if (CONSP (spec)
4492 && EQ (XCAR (spec), Qheight)
4493 && CONSP (XCDR (spec)))
4494 {
4495 if (it)
4496 {
4497 if (!FRAME_WINDOW_P (it->f))
4498 return 0;
4499
4500 it->font_height = XCAR (XCDR (spec));
4501 if (!NILP (it->font_height))
4502 {
4503 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4504 int new_height = -1;
4505
4506 if (CONSP (it->font_height)
4507 && (EQ (XCAR (it->font_height), Qplus)
4508 || EQ (XCAR (it->font_height), Qminus))
4509 && CONSP (XCDR (it->font_height))
4510 && INTEGERP (XCAR (XCDR (it->font_height))))
4511 {
4512 /* `(+ N)' or `(- N)' where N is an integer. */
4513 int steps = XINT (XCAR (XCDR (it->font_height)));
4514 if (EQ (XCAR (it->font_height), Qplus))
4515 steps = - steps;
4516 it->face_id = smaller_face (it->f, it->face_id, steps);
4517 }
4518 else if (FUNCTIONP (it->font_height))
4519 {
4520 /* Call function with current height as argument.
4521 Value is the new height. */
4522 Lisp_Object height;
4523 height = safe_call1 (it->font_height,
4524 face->lface[LFACE_HEIGHT_INDEX]);
4525 if (NUMBERP (height))
4526 new_height = XFLOATINT (height);
4527 }
4528 else if (NUMBERP (it->font_height))
4529 {
4530 /* Value is a multiple of the canonical char height. */
4531 struct face *f;
4532
4533 f = FACE_FROM_ID (it->f,
4534 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4535 new_height = (XFLOATINT (it->font_height)
4536 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4537 }
4538 else
4539 {
4540 /* Evaluate IT->font_height with `height' bound to the
4541 current specified height to get the new height. */
4542 int count = SPECPDL_INDEX ();
4543
4544 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4545 value = safe_eval (it->font_height);
4546 unbind_to (count, Qnil);
4547
4548 if (NUMBERP (value))
4549 new_height = XFLOATINT (value);
4550 }
4551
4552 if (new_height > 0)
4553 it->face_id = face_with_height (it->f, it->face_id, new_height);
4554 }
4555 }
4556
4557 return 0;
4558 }
4559
4560 /* Handle `(space-width WIDTH)'. */
4561 if (CONSP (spec)
4562 && EQ (XCAR (spec), Qspace_width)
4563 && CONSP (XCDR (spec)))
4564 {
4565 if (it)
4566 {
4567 if (!FRAME_WINDOW_P (it->f))
4568 return 0;
4569
4570 value = XCAR (XCDR (spec));
4571 if (NUMBERP (value) && XFLOATINT (value) > 0)
4572 it->space_width = value;
4573 }
4574
4575 return 0;
4576 }
4577
4578 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4579 if (CONSP (spec)
4580 && EQ (XCAR (spec), Qslice))
4581 {
4582 Lisp_Object tem;
4583
4584 if (it)
4585 {
4586 if (!FRAME_WINDOW_P (it->f))
4587 return 0;
4588
4589 if (tem = XCDR (spec), CONSP (tem))
4590 {
4591 it->slice.x = XCAR (tem);
4592 if (tem = XCDR (tem), CONSP (tem))
4593 {
4594 it->slice.y = XCAR (tem);
4595 if (tem = XCDR (tem), CONSP (tem))
4596 {
4597 it->slice.width = XCAR (tem);
4598 if (tem = XCDR (tem), CONSP (tem))
4599 it->slice.height = XCAR (tem);
4600 }
4601 }
4602 }
4603 }
4604
4605 return 0;
4606 }
4607
4608 /* Handle `(raise FACTOR)'. */
4609 if (CONSP (spec)
4610 && EQ (XCAR (spec), Qraise)
4611 && CONSP (XCDR (spec)))
4612 {
4613 if (it)
4614 {
4615 if (!FRAME_WINDOW_P (it->f))
4616 return 0;
4617
4618 #ifdef HAVE_WINDOW_SYSTEM
4619 value = XCAR (XCDR (spec));
4620 if (NUMBERP (value))
4621 {
4622 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4623 it->voffset = - (XFLOATINT (value)
4624 * (FONT_HEIGHT (face->font)));
4625 }
4626 #endif /* HAVE_WINDOW_SYSTEM */
4627 }
4628
4629 return 0;
4630 }
4631
4632 /* Don't handle the other kinds of display specifications
4633 inside a string that we got from a `display' property. */
4634 if (it && it->string_from_display_prop_p)
4635 return 0;
4636
4637 /* Characters having this form of property are not displayed, so
4638 we have to find the end of the property. */
4639 if (it)
4640 {
4641 start_pos = *position;
4642 *position = display_prop_end (it, object, start_pos);
4643 }
4644 value = Qnil;
4645
4646 /* Stop the scan at that end position--we assume that all
4647 text properties change there. */
4648 if (it)
4649 it->stop_charpos = position->charpos;
4650
4651 /* Handle `(left-fringe BITMAP [FACE])'
4652 and `(right-fringe BITMAP [FACE])'. */
4653 if (CONSP (spec)
4654 && (EQ (XCAR (spec), Qleft_fringe)
4655 || EQ (XCAR (spec), Qright_fringe))
4656 && CONSP (XCDR (spec)))
4657 {
4658 int fringe_bitmap;
4659
4660 if (it)
4661 {
4662 if (!FRAME_WINDOW_P (it->f))
4663 /* If we return here, POSITION has been advanced
4664 across the text with this property. */
4665 return 0;
4666 }
4667 else if (!frame_window_p)
4668 return 0;
4669
4670 #ifdef HAVE_WINDOW_SYSTEM
4671 value = XCAR (XCDR (spec));
4672 if (!SYMBOLP (value)
4673 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4674 /* If we return here, POSITION has been advanced
4675 across the text with this property. */
4676 return 0;
4677
4678 if (it)
4679 {
4680 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4681
4682 if (CONSP (XCDR (XCDR (spec))))
4683 {
4684 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4685 int face_id2 = lookup_derived_face (it->f, face_name,
4686 FRINGE_FACE_ID, 0);
4687 if (face_id2 >= 0)
4688 face_id = face_id2;
4689 }
4690
4691 /* Save current settings of IT so that we can restore them
4692 when we are finished with the glyph property value. */
4693 push_it (it, position);
4694
4695 it->area = TEXT_AREA;
4696 it->what = IT_IMAGE;
4697 it->image_id = -1; /* no image */
4698 it->position = start_pos;
4699 it->object = NILP (object) ? it->w->buffer : object;
4700 it->method = GET_FROM_IMAGE;
4701 it->from_overlay = Qnil;
4702 it->face_id = face_id;
4703 it->from_disp_prop_p = 1;
4704
4705 /* Say that we haven't consumed the characters with
4706 `display' property yet. The call to pop_it in
4707 set_iterator_to_next will clean this up. */
4708 *position = start_pos;
4709
4710 if (EQ (XCAR (spec), Qleft_fringe))
4711 {
4712 it->left_user_fringe_bitmap = fringe_bitmap;
4713 it->left_user_fringe_face_id = face_id;
4714 }
4715 else
4716 {
4717 it->right_user_fringe_bitmap = fringe_bitmap;
4718 it->right_user_fringe_face_id = face_id;
4719 }
4720 }
4721 #endif /* HAVE_WINDOW_SYSTEM */
4722 return 1;
4723 }
4724
4725 /* Prepare to handle `((margin left-margin) ...)',
4726 `((margin right-margin) ...)' and `((margin nil) ...)'
4727 prefixes for display specifications. */
4728 location = Qunbound;
4729 if (CONSP (spec) && CONSP (XCAR (spec)))
4730 {
4731 Lisp_Object tem;
4732
4733 value = XCDR (spec);
4734 if (CONSP (value))
4735 value = XCAR (value);
4736
4737 tem = XCAR (spec);
4738 if (EQ (XCAR (tem), Qmargin)
4739 && (tem = XCDR (tem),
4740 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4741 (NILP (tem)
4742 || EQ (tem, Qleft_margin)
4743 || EQ (tem, Qright_margin))))
4744 location = tem;
4745 }
4746
4747 if (EQ (location, Qunbound))
4748 {
4749 location = Qnil;
4750 value = spec;
4751 }
4752
4753 /* After this point, VALUE is the property after any
4754 margin prefix has been stripped. It must be a string,
4755 an image specification, or `(space ...)'.
4756
4757 LOCATION specifies where to display: `left-margin',
4758 `right-margin' or nil. */
4759
4760 valid_p = (STRINGP (value)
4761 #ifdef HAVE_WINDOW_SYSTEM
4762 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4763 && valid_image_p (value))
4764 #endif /* not HAVE_WINDOW_SYSTEM */
4765 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4766
4767 if (valid_p && !display_replaced_p)
4768 {
4769 int retval = 1;
4770
4771 if (!it)
4772 {
4773 /* Callers need to know whether the display spec is any kind
4774 of `(space ...)' spec that is about to affect text-area
4775 display. */
4776 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4777 retval = 2;
4778 return retval;
4779 }
4780
4781 /* Save current settings of IT so that we can restore them
4782 when we are finished with the glyph property value. */
4783 push_it (it, position);
4784 it->from_overlay = overlay;
4785 it->from_disp_prop_p = 1;
4786
4787 if (NILP (location))
4788 it->area = TEXT_AREA;
4789 else if (EQ (location, Qleft_margin))
4790 it->area = LEFT_MARGIN_AREA;
4791 else
4792 it->area = RIGHT_MARGIN_AREA;
4793
4794 if (STRINGP (value))
4795 {
4796 it->string = value;
4797 it->multibyte_p = STRING_MULTIBYTE (it->string);
4798 it->current.overlay_string_index = -1;
4799 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4800 it->end_charpos = it->string_nchars = SCHARS (it->string);
4801 it->method = GET_FROM_STRING;
4802 it->stop_charpos = 0;
4803 it->prev_stop = 0;
4804 it->base_level_stop = 0;
4805 it->string_from_display_prop_p = 1;
4806 /* Say that we haven't consumed the characters with
4807 `display' property yet. The call to pop_it in
4808 set_iterator_to_next will clean this up. */
4809 if (BUFFERP (object))
4810 *position = start_pos;
4811
4812 /* Force paragraph direction to be that of the parent
4813 object. If the parent object's paragraph direction is
4814 not yet determined, default to L2R. */
4815 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4816 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4817 else
4818 it->paragraph_embedding = L2R;
4819
4820 /* Set up the bidi iterator for this display string. */
4821 if (it->bidi_p)
4822 {
4823 it->bidi_it.string.lstring = it->string;
4824 it->bidi_it.string.s = NULL;
4825 it->bidi_it.string.schars = it->end_charpos;
4826 it->bidi_it.string.bufpos = bufpos;
4827 it->bidi_it.string.from_disp_str = 1;
4828 it->bidi_it.string.unibyte = !it->multibyte_p;
4829 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4830 }
4831 }
4832 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4833 {
4834 it->method = GET_FROM_STRETCH;
4835 it->object = value;
4836 *position = it->position = start_pos;
4837 retval = 1 + (it->area == TEXT_AREA);
4838 }
4839 #ifdef HAVE_WINDOW_SYSTEM
4840 else
4841 {
4842 it->what = IT_IMAGE;
4843 it->image_id = lookup_image (it->f, value);
4844 it->position = start_pos;
4845 it->object = NILP (object) ? it->w->buffer : object;
4846 it->method = GET_FROM_IMAGE;
4847
4848 /* Say that we haven't consumed the characters with
4849 `display' property yet. The call to pop_it in
4850 set_iterator_to_next will clean this up. */
4851 *position = start_pos;
4852 }
4853 #endif /* HAVE_WINDOW_SYSTEM */
4854
4855 return retval;
4856 }
4857
4858 /* Invalid property or property not supported. Restore
4859 POSITION to what it was before. */
4860 *position = start_pos;
4861 return 0;
4862 }
4863
4864 /* Check if PROP is a display property value whose text should be
4865 treated as intangible. OVERLAY is the overlay from which PROP
4866 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4867 specify the buffer position covered by PROP. */
4868
4869 int
4870 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4871 EMACS_INT charpos, EMACS_INT bytepos)
4872 {
4873 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4874 struct text_pos position;
4875
4876 SET_TEXT_POS (position, charpos, bytepos);
4877 return handle_display_spec (NULL, prop, Qnil, overlay,
4878 &position, charpos, frame_window_p);
4879 }
4880
4881
4882 /* Return 1 if PROP is a display sub-property value containing STRING.
4883
4884 Implementation note: this and the following function are really
4885 special cases of handle_display_spec and
4886 handle_single_display_spec, and should ideally use the same code.
4887 Until they do, these two pairs must be consistent and must be
4888 modified in sync. */
4889
4890 static int
4891 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4892 {
4893 if (EQ (string, prop))
4894 return 1;
4895
4896 /* Skip over `when FORM'. */
4897 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4898 {
4899 prop = XCDR (prop);
4900 if (!CONSP (prop))
4901 return 0;
4902 /* Actually, the condition following `when' should be eval'ed,
4903 like handle_single_display_spec does, and we should return
4904 zero if it evaluates to nil. However, this function is
4905 called only when the buffer was already displayed and some
4906 glyph in the glyph matrix was found to come from a display
4907 string. Therefore, the condition was already evaluated, and
4908 the result was non-nil, otherwise the display string wouldn't
4909 have been displayed and we would have never been called for
4910 this property. Thus, we can skip the evaluation and assume
4911 its result is non-nil. */
4912 prop = XCDR (prop);
4913 }
4914
4915 if (CONSP (prop))
4916 /* Skip over `margin LOCATION'. */
4917 if (EQ (XCAR (prop), Qmargin))
4918 {
4919 prop = XCDR (prop);
4920 if (!CONSP (prop))
4921 return 0;
4922
4923 prop = XCDR (prop);
4924 if (!CONSP (prop))
4925 return 0;
4926 }
4927
4928 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4929 }
4930
4931
4932 /* Return 1 if STRING appears in the `display' property PROP. */
4933
4934 static int
4935 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4936 {
4937 if (CONSP (prop)
4938 && !EQ (XCAR (prop), Qwhen)
4939 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4940 {
4941 /* A list of sub-properties. */
4942 while (CONSP (prop))
4943 {
4944 if (single_display_spec_string_p (XCAR (prop), string))
4945 return 1;
4946 prop = XCDR (prop);
4947 }
4948 }
4949 else if (VECTORP (prop))
4950 {
4951 /* A vector of sub-properties. */
4952 int i;
4953 for (i = 0; i < ASIZE (prop); ++i)
4954 if (single_display_spec_string_p (AREF (prop, i), string))
4955 return 1;
4956 }
4957 else
4958 return single_display_spec_string_p (prop, string);
4959
4960 return 0;
4961 }
4962
4963 /* Look for STRING in overlays and text properties in the current
4964 buffer, between character positions FROM and TO (excluding TO).
4965 BACK_P non-zero means look back (in this case, TO is supposed to be
4966 less than FROM).
4967 Value is the first character position where STRING was found, or
4968 zero if it wasn't found before hitting TO.
4969
4970 This function may only use code that doesn't eval because it is
4971 called asynchronously from note_mouse_highlight. */
4972
4973 static EMACS_INT
4974 string_buffer_position_lim (Lisp_Object string,
4975 EMACS_INT from, EMACS_INT to, int back_p)
4976 {
4977 Lisp_Object limit, prop, pos;
4978 int found = 0;
4979
4980 pos = make_number (from);
4981
4982 if (!back_p) /* looking forward */
4983 {
4984 limit = make_number (min (to, ZV));
4985 while (!found && !EQ (pos, limit))
4986 {
4987 prop = Fget_char_property (pos, Qdisplay, Qnil);
4988 if (!NILP (prop) && display_prop_string_p (prop, string))
4989 found = 1;
4990 else
4991 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4992 limit);
4993 }
4994 }
4995 else /* looking back */
4996 {
4997 limit = make_number (max (to, BEGV));
4998 while (!found && !EQ (pos, limit))
4999 {
5000 prop = Fget_char_property (pos, Qdisplay, Qnil);
5001 if (!NILP (prop) && display_prop_string_p (prop, string))
5002 found = 1;
5003 else
5004 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5005 limit);
5006 }
5007 }
5008
5009 return found ? XINT (pos) : 0;
5010 }
5011
5012 /* Determine which buffer position in current buffer STRING comes from.
5013 AROUND_CHARPOS is an approximate position where it could come from.
5014 Value is the buffer position or 0 if it couldn't be determined.
5015
5016 This function is necessary because we don't record buffer positions
5017 in glyphs generated from strings (to keep struct glyph small).
5018 This function may only use code that doesn't eval because it is
5019 called asynchronously from note_mouse_highlight. */
5020
5021 static EMACS_INT
5022 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
5023 {
5024 const int MAX_DISTANCE = 1000;
5025 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
5026 around_charpos + MAX_DISTANCE,
5027 0);
5028
5029 if (!found)
5030 found = string_buffer_position_lim (string, around_charpos,
5031 around_charpos - MAX_DISTANCE, 1);
5032 return found;
5033 }
5034
5035
5036 \f
5037 /***********************************************************************
5038 `composition' property
5039 ***********************************************************************/
5040
5041 /* Set up iterator IT from `composition' property at its current
5042 position. Called from handle_stop. */
5043
5044 static enum prop_handled
5045 handle_composition_prop (struct it *it)
5046 {
5047 Lisp_Object prop, string;
5048 EMACS_INT pos, pos_byte, start, end;
5049
5050 if (STRINGP (it->string))
5051 {
5052 unsigned char *s;
5053
5054 pos = IT_STRING_CHARPOS (*it);
5055 pos_byte = IT_STRING_BYTEPOS (*it);
5056 string = it->string;
5057 s = SDATA (string) + pos_byte;
5058 it->c = STRING_CHAR (s);
5059 }
5060 else
5061 {
5062 pos = IT_CHARPOS (*it);
5063 pos_byte = IT_BYTEPOS (*it);
5064 string = Qnil;
5065 it->c = FETCH_CHAR (pos_byte);
5066 }
5067
5068 /* If there's a valid composition and point is not inside of the
5069 composition (in the case that the composition is from the current
5070 buffer), draw a glyph composed from the composition components. */
5071 if (find_composition (pos, -1, &start, &end, &prop, string)
5072 && COMPOSITION_VALID_P (start, end, prop)
5073 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5074 {
5075 if (start < pos)
5076 /* As we can't handle this situation (perhaps font-lock added
5077 a new composition), we just return here hoping that next
5078 redisplay will detect this composition much earlier. */
5079 return HANDLED_NORMALLY;
5080 if (start != pos)
5081 {
5082 if (STRINGP (it->string))
5083 pos_byte = string_char_to_byte (it->string, start);
5084 else
5085 pos_byte = CHAR_TO_BYTE (start);
5086 }
5087 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5088 prop, string);
5089
5090 if (it->cmp_it.id >= 0)
5091 {
5092 it->cmp_it.ch = -1;
5093 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5094 it->cmp_it.nglyphs = -1;
5095 }
5096 }
5097
5098 return HANDLED_NORMALLY;
5099 }
5100
5101
5102 \f
5103 /***********************************************************************
5104 Overlay strings
5105 ***********************************************************************/
5106
5107 /* The following structure is used to record overlay strings for
5108 later sorting in load_overlay_strings. */
5109
5110 struct overlay_entry
5111 {
5112 Lisp_Object overlay;
5113 Lisp_Object string;
5114 int priority;
5115 int after_string_p;
5116 };
5117
5118
5119 /* Set up iterator IT from overlay strings at its current position.
5120 Called from handle_stop. */
5121
5122 static enum prop_handled
5123 handle_overlay_change (struct it *it)
5124 {
5125 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5126 return HANDLED_RECOMPUTE_PROPS;
5127 else
5128 return HANDLED_NORMALLY;
5129 }
5130
5131
5132 /* Set up the next overlay string for delivery by IT, if there is an
5133 overlay string to deliver. Called by set_iterator_to_next when the
5134 end of the current overlay string is reached. If there are more
5135 overlay strings to display, IT->string and
5136 IT->current.overlay_string_index are set appropriately here.
5137 Otherwise IT->string is set to nil. */
5138
5139 static void
5140 next_overlay_string (struct it *it)
5141 {
5142 ++it->current.overlay_string_index;
5143 if (it->current.overlay_string_index == it->n_overlay_strings)
5144 {
5145 /* No more overlay strings. Restore IT's settings to what
5146 they were before overlay strings were processed, and
5147 continue to deliver from current_buffer. */
5148
5149 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5150 pop_it (it);
5151 xassert (it->sp > 0
5152 || (NILP (it->string)
5153 && it->method == GET_FROM_BUFFER
5154 && it->stop_charpos >= BEGV
5155 && it->stop_charpos <= it->end_charpos));
5156 it->current.overlay_string_index = -1;
5157 it->n_overlay_strings = 0;
5158 it->overlay_strings_charpos = -1;
5159 /* If there's an empty display string on the stack, pop the
5160 stack, to resync the bidi iterator with IT's position. Such
5161 empty strings are pushed onto the stack in
5162 get_overlay_strings_1. */
5163 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5164 pop_it (it);
5165
5166 /* If we're at the end of the buffer, record that we have
5167 processed the overlay strings there already, so that
5168 next_element_from_buffer doesn't try it again. */
5169 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5170 it->overlay_strings_at_end_processed_p = 1;
5171 }
5172 else
5173 {
5174 /* There are more overlay strings to process. If
5175 IT->current.overlay_string_index has advanced to a position
5176 where we must load IT->overlay_strings with more strings, do
5177 it. We must load at the IT->overlay_strings_charpos where
5178 IT->n_overlay_strings was originally computed; when invisible
5179 text is present, this might not be IT_CHARPOS (Bug#7016). */
5180 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5181
5182 if (it->current.overlay_string_index && i == 0)
5183 load_overlay_strings (it, it->overlay_strings_charpos);
5184
5185 /* Initialize IT to deliver display elements from the overlay
5186 string. */
5187 it->string = it->overlay_strings[i];
5188 it->multibyte_p = STRING_MULTIBYTE (it->string);
5189 SET_TEXT_POS (it->current.string_pos, 0, 0);
5190 it->method = GET_FROM_STRING;
5191 it->stop_charpos = 0;
5192 if (it->cmp_it.stop_pos >= 0)
5193 it->cmp_it.stop_pos = 0;
5194 it->prev_stop = 0;
5195 it->base_level_stop = 0;
5196
5197 /* Set up the bidi iterator for this overlay string. */
5198 if (it->bidi_p)
5199 {
5200 it->bidi_it.string.lstring = it->string;
5201 it->bidi_it.string.s = NULL;
5202 it->bidi_it.string.schars = SCHARS (it->string);
5203 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5204 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5205 it->bidi_it.string.unibyte = !it->multibyte_p;
5206 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5207 }
5208 }
5209
5210 CHECK_IT (it);
5211 }
5212
5213
5214 /* Compare two overlay_entry structures E1 and E2. Used as a
5215 comparison function for qsort in load_overlay_strings. Overlay
5216 strings for the same position are sorted so that
5217
5218 1. All after-strings come in front of before-strings, except
5219 when they come from the same overlay.
5220
5221 2. Within after-strings, strings are sorted so that overlay strings
5222 from overlays with higher priorities come first.
5223
5224 2. Within before-strings, strings are sorted so that overlay
5225 strings from overlays with higher priorities come last.
5226
5227 Value is analogous to strcmp. */
5228
5229
5230 static int
5231 compare_overlay_entries (const void *e1, const void *e2)
5232 {
5233 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5234 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5235 int result;
5236
5237 if (entry1->after_string_p != entry2->after_string_p)
5238 {
5239 /* Let after-strings appear in front of before-strings if
5240 they come from different overlays. */
5241 if (EQ (entry1->overlay, entry2->overlay))
5242 result = entry1->after_string_p ? 1 : -1;
5243 else
5244 result = entry1->after_string_p ? -1 : 1;
5245 }
5246 else if (entry1->after_string_p)
5247 /* After-strings sorted in order of decreasing priority. */
5248 result = entry2->priority - entry1->priority;
5249 else
5250 /* Before-strings sorted in order of increasing priority. */
5251 result = entry1->priority - entry2->priority;
5252
5253 return result;
5254 }
5255
5256
5257 /* Load the vector IT->overlay_strings with overlay strings from IT's
5258 current buffer position, or from CHARPOS if that is > 0. Set
5259 IT->n_overlays to the total number of overlay strings found.
5260
5261 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5262 a time. On entry into load_overlay_strings,
5263 IT->current.overlay_string_index gives the number of overlay
5264 strings that have already been loaded by previous calls to this
5265 function.
5266
5267 IT->add_overlay_start contains an additional overlay start
5268 position to consider for taking overlay strings from, if non-zero.
5269 This position comes into play when the overlay has an `invisible'
5270 property, and both before and after-strings. When we've skipped to
5271 the end of the overlay, because of its `invisible' property, we
5272 nevertheless want its before-string to appear.
5273 IT->add_overlay_start will contain the overlay start position
5274 in this case.
5275
5276 Overlay strings are sorted so that after-string strings come in
5277 front of before-string strings. Within before and after-strings,
5278 strings are sorted by overlay priority. See also function
5279 compare_overlay_entries. */
5280
5281 static void
5282 load_overlay_strings (struct it *it, EMACS_INT charpos)
5283 {
5284 Lisp_Object overlay, window, str, invisible;
5285 struct Lisp_Overlay *ov;
5286 EMACS_INT start, end;
5287 int size = 20;
5288 int n = 0, i, j, invis_p;
5289 struct overlay_entry *entries
5290 = (struct overlay_entry *) alloca (size * sizeof *entries);
5291
5292 if (charpos <= 0)
5293 charpos = IT_CHARPOS (*it);
5294
5295 /* Append the overlay string STRING of overlay OVERLAY to vector
5296 `entries' which has size `size' and currently contains `n'
5297 elements. AFTER_P non-zero means STRING is an after-string of
5298 OVERLAY. */
5299 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5300 do \
5301 { \
5302 Lisp_Object priority; \
5303 \
5304 if (n == size) \
5305 { \
5306 int new_size = 2 * size; \
5307 struct overlay_entry *old = entries; \
5308 entries = \
5309 (struct overlay_entry *) alloca (new_size \
5310 * sizeof *entries); \
5311 memcpy (entries, old, size * sizeof *entries); \
5312 size = new_size; \
5313 } \
5314 \
5315 entries[n].string = (STRING); \
5316 entries[n].overlay = (OVERLAY); \
5317 priority = Foverlay_get ((OVERLAY), Qpriority); \
5318 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5319 entries[n].after_string_p = (AFTER_P); \
5320 ++n; \
5321 } \
5322 while (0)
5323
5324 /* Process overlay before the overlay center. */
5325 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5326 {
5327 XSETMISC (overlay, ov);
5328 xassert (OVERLAYP (overlay));
5329 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5330 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5331
5332 if (end < charpos)
5333 break;
5334
5335 /* Skip this overlay if it doesn't start or end at IT's current
5336 position. */
5337 if (end != charpos && start != charpos)
5338 continue;
5339
5340 /* Skip this overlay if it doesn't apply to IT->w. */
5341 window = Foverlay_get (overlay, Qwindow);
5342 if (WINDOWP (window) && XWINDOW (window) != it->w)
5343 continue;
5344
5345 /* If the text ``under'' the overlay is invisible, both before-
5346 and after-strings from this overlay are visible; start and
5347 end position are indistinguishable. */
5348 invisible = Foverlay_get (overlay, Qinvisible);
5349 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5350
5351 /* If overlay has a non-empty before-string, record it. */
5352 if ((start == charpos || (end == charpos && invis_p))
5353 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5354 && SCHARS (str))
5355 RECORD_OVERLAY_STRING (overlay, str, 0);
5356
5357 /* If overlay has a non-empty after-string, record it. */
5358 if ((end == charpos || (start == charpos && invis_p))
5359 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5360 && SCHARS (str))
5361 RECORD_OVERLAY_STRING (overlay, str, 1);
5362 }
5363
5364 /* Process overlays after the overlay center. */
5365 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5366 {
5367 XSETMISC (overlay, ov);
5368 xassert (OVERLAYP (overlay));
5369 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5370 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5371
5372 if (start > charpos)
5373 break;
5374
5375 /* Skip this overlay if it doesn't start or end at IT's current
5376 position. */
5377 if (end != charpos && start != charpos)
5378 continue;
5379
5380 /* Skip this overlay if it doesn't apply to IT->w. */
5381 window = Foverlay_get (overlay, Qwindow);
5382 if (WINDOWP (window) && XWINDOW (window) != it->w)
5383 continue;
5384
5385 /* If the text ``under'' the overlay is invisible, it has a zero
5386 dimension, and both before- and after-strings apply. */
5387 invisible = Foverlay_get (overlay, Qinvisible);
5388 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5389
5390 /* If overlay has a non-empty before-string, record it. */
5391 if ((start == charpos || (end == charpos && invis_p))
5392 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5393 && SCHARS (str))
5394 RECORD_OVERLAY_STRING (overlay, str, 0);
5395
5396 /* If overlay has a non-empty after-string, record it. */
5397 if ((end == charpos || (start == charpos && invis_p))
5398 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5399 && SCHARS (str))
5400 RECORD_OVERLAY_STRING (overlay, str, 1);
5401 }
5402
5403 #undef RECORD_OVERLAY_STRING
5404
5405 /* Sort entries. */
5406 if (n > 1)
5407 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5408
5409 /* Record number of overlay strings, and where we computed it. */
5410 it->n_overlay_strings = n;
5411 it->overlay_strings_charpos = charpos;
5412
5413 /* IT->current.overlay_string_index is the number of overlay strings
5414 that have already been consumed by IT. Copy some of the
5415 remaining overlay strings to IT->overlay_strings. */
5416 i = 0;
5417 j = it->current.overlay_string_index;
5418 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5419 {
5420 it->overlay_strings[i] = entries[j].string;
5421 it->string_overlays[i++] = entries[j++].overlay;
5422 }
5423
5424 CHECK_IT (it);
5425 }
5426
5427
5428 /* Get the first chunk of overlay strings at IT's current buffer
5429 position, or at CHARPOS if that is > 0. Value is non-zero if at
5430 least one overlay string was found. */
5431
5432 static int
5433 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5434 {
5435 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5436 process. This fills IT->overlay_strings with strings, and sets
5437 IT->n_overlay_strings to the total number of strings to process.
5438 IT->pos.overlay_string_index has to be set temporarily to zero
5439 because load_overlay_strings needs this; it must be set to -1
5440 when no overlay strings are found because a zero value would
5441 indicate a position in the first overlay string. */
5442 it->current.overlay_string_index = 0;
5443 load_overlay_strings (it, charpos);
5444
5445 /* If we found overlay strings, set up IT to deliver display
5446 elements from the first one. Otherwise set up IT to deliver
5447 from current_buffer. */
5448 if (it->n_overlay_strings)
5449 {
5450 /* Make sure we know settings in current_buffer, so that we can
5451 restore meaningful values when we're done with the overlay
5452 strings. */
5453 if (compute_stop_p)
5454 compute_stop_pos (it);
5455 xassert (it->face_id >= 0);
5456
5457 /* Save IT's settings. They are restored after all overlay
5458 strings have been processed. */
5459 xassert (!compute_stop_p || it->sp == 0);
5460
5461 /* When called from handle_stop, there might be an empty display
5462 string loaded. In that case, don't bother saving it. But
5463 don't use this optimization with the bidi iterator, since we
5464 need the corresponding pop_it call to resync the bidi
5465 iterator's position with IT's position, after we are done
5466 with the overlay strings. (The corresponding call to pop_it
5467 in case of an empty display string is in
5468 next_overlay_string.) */
5469 if (!(!it->bidi_p
5470 && STRINGP (it->string) && !SCHARS (it->string)))
5471 push_it (it, NULL);
5472
5473 /* Set up IT to deliver display elements from the first overlay
5474 string. */
5475 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5476 it->string = it->overlay_strings[0];
5477 it->from_overlay = Qnil;
5478 it->stop_charpos = 0;
5479 xassert (STRINGP (it->string));
5480 it->end_charpos = SCHARS (it->string);
5481 it->prev_stop = 0;
5482 it->base_level_stop = 0;
5483 it->multibyte_p = STRING_MULTIBYTE (it->string);
5484 it->method = GET_FROM_STRING;
5485 it->from_disp_prop_p = 0;
5486
5487 /* Force paragraph direction to be that of the parent
5488 buffer. */
5489 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5490 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5491 else
5492 it->paragraph_embedding = L2R;
5493
5494 /* Set up the bidi iterator for this overlay string. */
5495 if (it->bidi_p)
5496 {
5497 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5498
5499 it->bidi_it.string.lstring = it->string;
5500 it->bidi_it.string.s = NULL;
5501 it->bidi_it.string.schars = SCHARS (it->string);
5502 it->bidi_it.string.bufpos = pos;
5503 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5504 it->bidi_it.string.unibyte = !it->multibyte_p;
5505 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5506 }
5507 return 1;
5508 }
5509
5510 it->current.overlay_string_index = -1;
5511 return 0;
5512 }
5513
5514 static int
5515 get_overlay_strings (struct it *it, EMACS_INT charpos)
5516 {
5517 it->string = Qnil;
5518 it->method = GET_FROM_BUFFER;
5519
5520 (void) get_overlay_strings_1 (it, charpos, 1);
5521
5522 CHECK_IT (it);
5523
5524 /* Value is non-zero if we found at least one overlay string. */
5525 return STRINGP (it->string);
5526 }
5527
5528
5529 \f
5530 /***********************************************************************
5531 Saving and restoring state
5532 ***********************************************************************/
5533
5534 /* Save current settings of IT on IT->stack. Called, for example,
5535 before setting up IT for an overlay string, to be able to restore
5536 IT's settings to what they were after the overlay string has been
5537 processed. If POSITION is non-NULL, it is the position to save on
5538 the stack instead of IT->position. */
5539
5540 static void
5541 push_it (struct it *it, struct text_pos *position)
5542 {
5543 struct iterator_stack_entry *p;
5544
5545 xassert (it->sp < IT_STACK_SIZE);
5546 p = it->stack + it->sp;
5547
5548 p->stop_charpos = it->stop_charpos;
5549 p->prev_stop = it->prev_stop;
5550 p->base_level_stop = it->base_level_stop;
5551 p->cmp_it = it->cmp_it;
5552 xassert (it->face_id >= 0);
5553 p->face_id = it->face_id;
5554 p->string = it->string;
5555 p->method = it->method;
5556 p->from_overlay = it->from_overlay;
5557 switch (p->method)
5558 {
5559 case GET_FROM_IMAGE:
5560 p->u.image.object = it->object;
5561 p->u.image.image_id = it->image_id;
5562 p->u.image.slice = it->slice;
5563 break;
5564 case GET_FROM_STRETCH:
5565 p->u.stretch.object = it->object;
5566 break;
5567 }
5568 p->position = position ? *position : it->position;
5569 p->current = it->current;
5570 p->end_charpos = it->end_charpos;
5571 p->string_nchars = it->string_nchars;
5572 p->area = it->area;
5573 p->multibyte_p = it->multibyte_p;
5574 p->avoid_cursor_p = it->avoid_cursor_p;
5575 p->space_width = it->space_width;
5576 p->font_height = it->font_height;
5577 p->voffset = it->voffset;
5578 p->string_from_display_prop_p = it->string_from_display_prop_p;
5579 p->display_ellipsis_p = 0;
5580 p->line_wrap = it->line_wrap;
5581 p->bidi_p = it->bidi_p;
5582 p->paragraph_embedding = it->paragraph_embedding;
5583 p->from_disp_prop_p = it->from_disp_prop_p;
5584 ++it->sp;
5585
5586 /* Save the state of the bidi iterator as well. */
5587 if (it->bidi_p)
5588 bidi_push_it (&it->bidi_it);
5589 }
5590
5591 static void
5592 iterate_out_of_display_property (struct it *it)
5593 {
5594 int buffer_p = BUFFERP (it->object);
5595 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5596 EMACS_INT bob = (buffer_p ? BEGV : 0);
5597
5598 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5599
5600 /* Maybe initialize paragraph direction. If we are at the beginning
5601 of a new paragraph, next_element_from_buffer may not have a
5602 chance to do that. */
5603 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5604 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5605 /* prev_stop can be zero, so check against BEGV as well. */
5606 while (it->bidi_it.charpos >= bob
5607 && it->prev_stop <= it->bidi_it.charpos
5608 && it->bidi_it.charpos < CHARPOS (it->position)
5609 && it->bidi_it.charpos < eob)
5610 bidi_move_to_visually_next (&it->bidi_it);
5611 /* Record the stop_pos we just crossed, for when we cross it
5612 back, maybe. */
5613 if (it->bidi_it.charpos > CHARPOS (it->position))
5614 it->prev_stop = CHARPOS (it->position);
5615 /* If we ended up not where pop_it put us, resync IT's
5616 positional members with the bidi iterator. */
5617 if (it->bidi_it.charpos != CHARPOS (it->position))
5618 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5619 if (buffer_p)
5620 it->current.pos = it->position;
5621 else
5622 it->current.string_pos = it->position;
5623 }
5624
5625 /* Restore IT's settings from IT->stack. Called, for example, when no
5626 more overlay strings must be processed, and we return to delivering
5627 display elements from a buffer, or when the end of a string from a
5628 `display' property is reached and we return to delivering display
5629 elements from an overlay string, or from a buffer. */
5630
5631 static void
5632 pop_it (struct it *it)
5633 {
5634 struct iterator_stack_entry *p;
5635 int from_display_prop = it->from_disp_prop_p;
5636
5637 xassert (it->sp > 0);
5638 --it->sp;
5639 p = it->stack + it->sp;
5640 it->stop_charpos = p->stop_charpos;
5641 it->prev_stop = p->prev_stop;
5642 it->base_level_stop = p->base_level_stop;
5643 it->cmp_it = p->cmp_it;
5644 it->face_id = p->face_id;
5645 it->current = p->current;
5646 it->position = p->position;
5647 it->string = p->string;
5648 it->from_overlay = p->from_overlay;
5649 if (NILP (it->string))
5650 SET_TEXT_POS (it->current.string_pos, -1, -1);
5651 it->method = p->method;
5652 switch (it->method)
5653 {
5654 case GET_FROM_IMAGE:
5655 it->image_id = p->u.image.image_id;
5656 it->object = p->u.image.object;
5657 it->slice = p->u.image.slice;
5658 break;
5659 case GET_FROM_STRETCH:
5660 it->object = p->u.stretch.object;
5661 break;
5662 case GET_FROM_BUFFER:
5663 it->object = it->w->buffer;
5664 break;
5665 case GET_FROM_STRING:
5666 it->object = it->string;
5667 break;
5668 case GET_FROM_DISPLAY_VECTOR:
5669 if (it->s)
5670 it->method = GET_FROM_C_STRING;
5671 else if (STRINGP (it->string))
5672 it->method = GET_FROM_STRING;
5673 else
5674 {
5675 it->method = GET_FROM_BUFFER;
5676 it->object = it->w->buffer;
5677 }
5678 }
5679 it->end_charpos = p->end_charpos;
5680 it->string_nchars = p->string_nchars;
5681 it->area = p->area;
5682 it->multibyte_p = p->multibyte_p;
5683 it->avoid_cursor_p = p->avoid_cursor_p;
5684 it->space_width = p->space_width;
5685 it->font_height = p->font_height;
5686 it->voffset = p->voffset;
5687 it->string_from_display_prop_p = p->string_from_display_prop_p;
5688 it->line_wrap = p->line_wrap;
5689 it->bidi_p = p->bidi_p;
5690 it->paragraph_embedding = p->paragraph_embedding;
5691 it->from_disp_prop_p = p->from_disp_prop_p;
5692 if (it->bidi_p)
5693 {
5694 bidi_pop_it (&it->bidi_it);
5695 /* Bidi-iterate until we get out of the portion of text, if any,
5696 covered by a `display' text property or by an overlay with
5697 `display' property. (We cannot just jump there, because the
5698 internal coherency of the bidi iterator state can not be
5699 preserved across such jumps.) We also must determine the
5700 paragraph base direction if the overlay we just processed is
5701 at the beginning of a new paragraph. */
5702 if (from_display_prop
5703 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5704 iterate_out_of_display_property (it);
5705
5706 xassert ((BUFFERP (it->object)
5707 && IT_CHARPOS (*it) == it->bidi_it.charpos
5708 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5709 || (STRINGP (it->object)
5710 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5711 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5712 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5713 }
5714 }
5715
5716
5717 \f
5718 /***********************************************************************
5719 Moving over lines
5720 ***********************************************************************/
5721
5722 /* Set IT's current position to the previous line start. */
5723
5724 static void
5725 back_to_previous_line_start (struct it *it)
5726 {
5727 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5728 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5729 }
5730
5731
5732 /* Move IT to the next line start.
5733
5734 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5735 we skipped over part of the text (as opposed to moving the iterator
5736 continuously over the text). Otherwise, don't change the value
5737 of *SKIPPED_P.
5738
5739 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5740 iterator on the newline, if it was found.
5741
5742 Newlines may come from buffer text, overlay strings, or strings
5743 displayed via the `display' property. That's the reason we can't
5744 simply use find_next_newline_no_quit.
5745
5746 Note that this function may not skip over invisible text that is so
5747 because of text properties and immediately follows a newline. If
5748 it would, function reseat_at_next_visible_line_start, when called
5749 from set_iterator_to_next, would effectively make invisible
5750 characters following a newline part of the wrong glyph row, which
5751 leads to wrong cursor motion. */
5752
5753 static int
5754 forward_to_next_line_start (struct it *it, int *skipped_p,
5755 struct bidi_it *bidi_it_prev)
5756 {
5757 EMACS_INT old_selective;
5758 int newline_found_p, n;
5759 const int MAX_NEWLINE_DISTANCE = 500;
5760
5761 /* If already on a newline, just consume it to avoid unintended
5762 skipping over invisible text below. */
5763 if (it->what == IT_CHARACTER
5764 && it->c == '\n'
5765 && CHARPOS (it->position) == IT_CHARPOS (*it))
5766 {
5767 if (it->bidi_p && bidi_it_prev)
5768 *bidi_it_prev = it->bidi_it;
5769 set_iterator_to_next (it, 0);
5770 it->c = 0;
5771 return 1;
5772 }
5773
5774 /* Don't handle selective display in the following. It's (a)
5775 unnecessary because it's done by the caller, and (b) leads to an
5776 infinite recursion because next_element_from_ellipsis indirectly
5777 calls this function. */
5778 old_selective = it->selective;
5779 it->selective = 0;
5780
5781 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5782 from buffer text. */
5783 for (n = newline_found_p = 0;
5784 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5785 n += STRINGP (it->string) ? 0 : 1)
5786 {
5787 if (!get_next_display_element (it))
5788 return 0;
5789 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5790 if (newline_found_p && it->bidi_p && bidi_it_prev)
5791 *bidi_it_prev = it->bidi_it;
5792 set_iterator_to_next (it, 0);
5793 }
5794
5795 /* If we didn't find a newline near enough, see if we can use a
5796 short-cut. */
5797 if (!newline_found_p)
5798 {
5799 EMACS_INT start = IT_CHARPOS (*it);
5800 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5801 Lisp_Object pos;
5802
5803 xassert (!STRINGP (it->string));
5804
5805 /* If there isn't any `display' property in sight, and no
5806 overlays, we can just use the position of the newline in
5807 buffer text. */
5808 if (it->stop_charpos >= limit
5809 || ((pos = Fnext_single_property_change (make_number (start),
5810 Qdisplay, Qnil,
5811 make_number (limit)),
5812 NILP (pos))
5813 && next_overlay_change (start) == ZV))
5814 {
5815 if (!it->bidi_p)
5816 {
5817 IT_CHARPOS (*it) = limit;
5818 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5819 }
5820 else
5821 {
5822 struct bidi_it bprev;
5823
5824 /* Help bidi.c avoid expensive searches for display
5825 properties and overlays, by telling it that there are
5826 none up to `limit'. */
5827 if (it->bidi_it.disp_pos < limit)
5828 {
5829 it->bidi_it.disp_pos = limit;
5830 it->bidi_it.disp_prop = 0;
5831 }
5832 do {
5833 bprev = it->bidi_it;
5834 bidi_move_to_visually_next (&it->bidi_it);
5835 } while (it->bidi_it.charpos != limit);
5836 IT_CHARPOS (*it) = limit;
5837 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5838 if (bidi_it_prev)
5839 *bidi_it_prev = bprev;
5840 }
5841 *skipped_p = newline_found_p = 1;
5842 }
5843 else
5844 {
5845 while (get_next_display_element (it)
5846 && !newline_found_p)
5847 {
5848 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5849 if (newline_found_p && it->bidi_p && bidi_it_prev)
5850 *bidi_it_prev = it->bidi_it;
5851 set_iterator_to_next (it, 0);
5852 }
5853 }
5854 }
5855
5856 it->selective = old_selective;
5857 return newline_found_p;
5858 }
5859
5860
5861 /* Set IT's current position to the previous visible line start. Skip
5862 invisible text that is so either due to text properties or due to
5863 selective display. Caution: this does not change IT->current_x and
5864 IT->hpos. */
5865
5866 static void
5867 back_to_previous_visible_line_start (struct it *it)
5868 {
5869 while (IT_CHARPOS (*it) > BEGV)
5870 {
5871 back_to_previous_line_start (it);
5872
5873 if (IT_CHARPOS (*it) <= BEGV)
5874 break;
5875
5876 /* If selective > 0, then lines indented more than its value are
5877 invisible. */
5878 if (it->selective > 0
5879 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5880 it->selective))
5881 continue;
5882
5883 /* Check the newline before point for invisibility. */
5884 {
5885 Lisp_Object prop;
5886 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5887 Qinvisible, it->window);
5888 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5889 continue;
5890 }
5891
5892 if (IT_CHARPOS (*it) <= BEGV)
5893 break;
5894
5895 {
5896 struct it it2;
5897 void *it2data = NULL;
5898 EMACS_INT pos;
5899 EMACS_INT beg, end;
5900 Lisp_Object val, overlay;
5901
5902 SAVE_IT (it2, *it, it2data);
5903
5904 /* If newline is part of a composition, continue from start of composition */
5905 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5906 && beg < IT_CHARPOS (*it))
5907 goto replaced;
5908
5909 /* If newline is replaced by a display property, find start of overlay
5910 or interval and continue search from that point. */
5911 pos = --IT_CHARPOS (it2);
5912 --IT_BYTEPOS (it2);
5913 it2.sp = 0;
5914 bidi_unshelve_cache (NULL, 0);
5915 it2.string_from_display_prop_p = 0;
5916 it2.from_disp_prop_p = 0;
5917 if (handle_display_prop (&it2) == HANDLED_RETURN
5918 && !NILP (val = get_char_property_and_overlay
5919 (make_number (pos), Qdisplay, Qnil, &overlay))
5920 && (OVERLAYP (overlay)
5921 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5922 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5923 {
5924 RESTORE_IT (it, it, it2data);
5925 goto replaced;
5926 }
5927
5928 /* Newline is not replaced by anything -- so we are done. */
5929 RESTORE_IT (it, it, it2data);
5930 break;
5931
5932 replaced:
5933 if (beg < BEGV)
5934 beg = BEGV;
5935 IT_CHARPOS (*it) = beg;
5936 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5937 }
5938 }
5939
5940 it->continuation_lines_width = 0;
5941
5942 xassert (IT_CHARPOS (*it) >= BEGV);
5943 xassert (IT_CHARPOS (*it) == BEGV
5944 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5945 CHECK_IT (it);
5946 }
5947
5948
5949 /* Reseat iterator IT at the previous visible line start. Skip
5950 invisible text that is so either due to text properties or due to
5951 selective display. At the end, update IT's overlay information,
5952 face information etc. */
5953
5954 void
5955 reseat_at_previous_visible_line_start (struct it *it)
5956 {
5957 back_to_previous_visible_line_start (it);
5958 reseat (it, it->current.pos, 1);
5959 CHECK_IT (it);
5960 }
5961
5962
5963 /* Reseat iterator IT on the next visible line start in the current
5964 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5965 preceding the line start. Skip over invisible text that is so
5966 because of selective display. Compute faces, overlays etc at the
5967 new position. Note that this function does not skip over text that
5968 is invisible because of text properties. */
5969
5970 static void
5971 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5972 {
5973 int newline_found_p, skipped_p = 0;
5974 struct bidi_it bidi_it_prev;
5975
5976 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5977
5978 /* Skip over lines that are invisible because they are indented
5979 more than the value of IT->selective. */
5980 if (it->selective > 0)
5981 while (IT_CHARPOS (*it) < ZV
5982 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5983 it->selective))
5984 {
5985 xassert (IT_BYTEPOS (*it) == BEGV
5986 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5987 newline_found_p =
5988 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5989 }
5990
5991 /* Position on the newline if that's what's requested. */
5992 if (on_newline_p && newline_found_p)
5993 {
5994 if (STRINGP (it->string))
5995 {
5996 if (IT_STRING_CHARPOS (*it) > 0)
5997 {
5998 if (!it->bidi_p)
5999 {
6000 --IT_STRING_CHARPOS (*it);
6001 --IT_STRING_BYTEPOS (*it);
6002 }
6003 else
6004 {
6005 /* We need to restore the bidi iterator to the state
6006 it had on the newline, and resync the IT's
6007 position with that. */
6008 it->bidi_it = bidi_it_prev;
6009 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6010 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6011 }
6012 }
6013 }
6014 else if (IT_CHARPOS (*it) > BEGV)
6015 {
6016 if (!it->bidi_p)
6017 {
6018 --IT_CHARPOS (*it);
6019 --IT_BYTEPOS (*it);
6020 }
6021 else
6022 {
6023 /* We need to restore the bidi iterator to the state it
6024 had on the newline and resync IT with that. */
6025 it->bidi_it = bidi_it_prev;
6026 IT_CHARPOS (*it) = it->bidi_it.charpos;
6027 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6028 }
6029 reseat (it, it->current.pos, 0);
6030 }
6031 }
6032 else if (skipped_p)
6033 reseat (it, it->current.pos, 0);
6034
6035 CHECK_IT (it);
6036 }
6037
6038
6039 \f
6040 /***********************************************************************
6041 Changing an iterator's position
6042 ***********************************************************************/
6043
6044 /* Change IT's current position to POS in current_buffer. If FORCE_P
6045 is non-zero, always check for text properties at the new position.
6046 Otherwise, text properties are only looked up if POS >=
6047 IT->check_charpos of a property. */
6048
6049 static void
6050 reseat (struct it *it, struct text_pos pos, int force_p)
6051 {
6052 EMACS_INT original_pos = IT_CHARPOS (*it);
6053
6054 reseat_1 (it, pos, 0);
6055
6056 /* Determine where to check text properties. Avoid doing it
6057 where possible because text property lookup is very expensive. */
6058 if (force_p
6059 || CHARPOS (pos) > it->stop_charpos
6060 || CHARPOS (pos) < original_pos)
6061 {
6062 if (it->bidi_p)
6063 {
6064 /* For bidi iteration, we need to prime prev_stop and
6065 base_level_stop with our best estimations. */
6066 /* Implementation note: Of course, POS is not necessarily a
6067 stop position, so assigning prev_pos to it is a lie; we
6068 should have called compute_stop_backwards. However, if
6069 the current buffer does not include any R2L characters,
6070 that call would be a waste of cycles, because the
6071 iterator will never move back, and thus never cross this
6072 "fake" stop position. So we delay that backward search
6073 until the time we really need it, in next_element_from_buffer. */
6074 if (CHARPOS (pos) != it->prev_stop)
6075 it->prev_stop = CHARPOS (pos);
6076 if (CHARPOS (pos) < it->base_level_stop)
6077 it->base_level_stop = 0; /* meaning it's unknown */
6078 handle_stop (it);
6079 }
6080 else
6081 {
6082 handle_stop (it);
6083 it->prev_stop = it->base_level_stop = 0;
6084 }
6085
6086 }
6087
6088 CHECK_IT (it);
6089 }
6090
6091
6092 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6093 IT->stop_pos to POS, also. */
6094
6095 static void
6096 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6097 {
6098 /* Don't call this function when scanning a C string. */
6099 xassert (it->s == NULL);
6100
6101 /* POS must be a reasonable value. */
6102 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6103
6104 it->current.pos = it->position = pos;
6105 it->end_charpos = ZV;
6106 it->dpvec = NULL;
6107 it->current.dpvec_index = -1;
6108 it->current.overlay_string_index = -1;
6109 IT_STRING_CHARPOS (*it) = -1;
6110 IT_STRING_BYTEPOS (*it) = -1;
6111 it->string = Qnil;
6112 it->method = GET_FROM_BUFFER;
6113 it->object = it->w->buffer;
6114 it->area = TEXT_AREA;
6115 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6116 it->sp = 0;
6117 it->string_from_display_prop_p = 0;
6118 it->from_disp_prop_p = 0;
6119 it->face_before_selective_p = 0;
6120 if (it->bidi_p)
6121 {
6122 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6123 &it->bidi_it);
6124 bidi_unshelve_cache (NULL, 0);
6125 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6126 it->bidi_it.string.s = NULL;
6127 it->bidi_it.string.lstring = Qnil;
6128 it->bidi_it.string.bufpos = 0;
6129 it->bidi_it.string.unibyte = 0;
6130 }
6131
6132 if (set_stop_p)
6133 {
6134 it->stop_charpos = CHARPOS (pos);
6135 it->base_level_stop = CHARPOS (pos);
6136 }
6137 }
6138
6139
6140 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6141 If S is non-null, it is a C string to iterate over. Otherwise,
6142 STRING gives a Lisp string to iterate over.
6143
6144 If PRECISION > 0, don't return more then PRECISION number of
6145 characters from the string.
6146
6147 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6148 characters have been returned. FIELD_WIDTH < 0 means an infinite
6149 field width.
6150
6151 MULTIBYTE = 0 means disable processing of multibyte characters,
6152 MULTIBYTE > 0 means enable it,
6153 MULTIBYTE < 0 means use IT->multibyte_p.
6154
6155 IT must be initialized via a prior call to init_iterator before
6156 calling this function. */
6157
6158 static void
6159 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6160 EMACS_INT charpos, EMACS_INT precision, int field_width,
6161 int multibyte)
6162 {
6163 /* No region in strings. */
6164 it->region_beg_charpos = it->region_end_charpos = -1;
6165
6166 /* No text property checks performed by default, but see below. */
6167 it->stop_charpos = -1;
6168
6169 /* Set iterator position and end position. */
6170 memset (&it->current, 0, sizeof it->current);
6171 it->current.overlay_string_index = -1;
6172 it->current.dpvec_index = -1;
6173 xassert (charpos >= 0);
6174
6175 /* If STRING is specified, use its multibyteness, otherwise use the
6176 setting of MULTIBYTE, if specified. */
6177 if (multibyte >= 0)
6178 it->multibyte_p = multibyte > 0;
6179
6180 /* Bidirectional reordering of strings is controlled by the default
6181 value of bidi-display-reordering. Don't try to reorder while
6182 loading loadup.el, as the necessary character property tables are
6183 not yet available. */
6184 it->bidi_p =
6185 NILP (Vpurify_flag)
6186 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6187
6188 if (s == NULL)
6189 {
6190 xassert (STRINGP (string));
6191 it->string = string;
6192 it->s = NULL;
6193 it->end_charpos = it->string_nchars = SCHARS (string);
6194 it->method = GET_FROM_STRING;
6195 it->current.string_pos = string_pos (charpos, string);
6196
6197 if (it->bidi_p)
6198 {
6199 it->bidi_it.string.lstring = string;
6200 it->bidi_it.string.s = NULL;
6201 it->bidi_it.string.schars = it->end_charpos;
6202 it->bidi_it.string.bufpos = 0;
6203 it->bidi_it.string.from_disp_str = 0;
6204 it->bidi_it.string.unibyte = !it->multibyte_p;
6205 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6206 FRAME_WINDOW_P (it->f), &it->bidi_it);
6207 }
6208 }
6209 else
6210 {
6211 it->s = (const unsigned char *) s;
6212 it->string = Qnil;
6213
6214 /* Note that we use IT->current.pos, not it->current.string_pos,
6215 for displaying C strings. */
6216 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6217 if (it->multibyte_p)
6218 {
6219 it->current.pos = c_string_pos (charpos, s, 1);
6220 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6221 }
6222 else
6223 {
6224 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6225 it->end_charpos = it->string_nchars = strlen (s);
6226 }
6227
6228 if (it->bidi_p)
6229 {
6230 it->bidi_it.string.lstring = Qnil;
6231 it->bidi_it.string.s = (const unsigned char *) s;
6232 it->bidi_it.string.schars = it->end_charpos;
6233 it->bidi_it.string.bufpos = 0;
6234 it->bidi_it.string.from_disp_str = 0;
6235 it->bidi_it.string.unibyte = !it->multibyte_p;
6236 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6237 &it->bidi_it);
6238 }
6239 it->method = GET_FROM_C_STRING;
6240 }
6241
6242 /* PRECISION > 0 means don't return more than PRECISION characters
6243 from the string. */
6244 if (precision > 0 && it->end_charpos - charpos > precision)
6245 {
6246 it->end_charpos = it->string_nchars = charpos + precision;
6247 if (it->bidi_p)
6248 it->bidi_it.string.schars = it->end_charpos;
6249 }
6250
6251 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6252 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6253 FIELD_WIDTH < 0 means infinite field width. This is useful for
6254 padding with `-' at the end of a mode line. */
6255 if (field_width < 0)
6256 field_width = INFINITY;
6257 /* Implementation note: We deliberately don't enlarge
6258 it->bidi_it.string.schars here to fit it->end_charpos, because
6259 the bidi iterator cannot produce characters out of thin air. */
6260 if (field_width > it->end_charpos - charpos)
6261 it->end_charpos = charpos + field_width;
6262
6263 /* Use the standard display table for displaying strings. */
6264 if (DISP_TABLE_P (Vstandard_display_table))
6265 it->dp = XCHAR_TABLE (Vstandard_display_table);
6266
6267 it->stop_charpos = charpos;
6268 it->prev_stop = charpos;
6269 it->base_level_stop = 0;
6270 if (it->bidi_p)
6271 {
6272 it->bidi_it.first_elt = 1;
6273 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6274 it->bidi_it.disp_pos = -1;
6275 }
6276 if (s == NULL && it->multibyte_p)
6277 {
6278 EMACS_INT endpos = SCHARS (it->string);
6279 if (endpos > it->end_charpos)
6280 endpos = it->end_charpos;
6281 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6282 it->string);
6283 }
6284 CHECK_IT (it);
6285 }
6286
6287
6288 \f
6289 /***********************************************************************
6290 Iteration
6291 ***********************************************************************/
6292
6293 /* Map enum it_method value to corresponding next_element_from_* function. */
6294
6295 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6296 {
6297 next_element_from_buffer,
6298 next_element_from_display_vector,
6299 next_element_from_string,
6300 next_element_from_c_string,
6301 next_element_from_image,
6302 next_element_from_stretch
6303 };
6304
6305 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6306
6307
6308 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6309 (possibly with the following characters). */
6310
6311 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6312 ((IT)->cmp_it.id >= 0 \
6313 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6314 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6315 END_CHARPOS, (IT)->w, \
6316 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6317 (IT)->string)))
6318
6319
6320 /* Lookup the char-table Vglyphless_char_display for character C (-1
6321 if we want information for no-font case), and return the display
6322 method symbol. By side-effect, update it->what and
6323 it->glyphless_method. This function is called from
6324 get_next_display_element for each character element, and from
6325 x_produce_glyphs when no suitable font was found. */
6326
6327 Lisp_Object
6328 lookup_glyphless_char_display (int c, struct it *it)
6329 {
6330 Lisp_Object glyphless_method = Qnil;
6331
6332 if (CHAR_TABLE_P (Vglyphless_char_display)
6333 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6334 {
6335 if (c >= 0)
6336 {
6337 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6338 if (CONSP (glyphless_method))
6339 glyphless_method = FRAME_WINDOW_P (it->f)
6340 ? XCAR (glyphless_method)
6341 : XCDR (glyphless_method);
6342 }
6343 else
6344 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6345 }
6346
6347 retry:
6348 if (NILP (glyphless_method))
6349 {
6350 if (c >= 0)
6351 /* The default is to display the character by a proper font. */
6352 return Qnil;
6353 /* The default for the no-font case is to display an empty box. */
6354 glyphless_method = Qempty_box;
6355 }
6356 if (EQ (glyphless_method, Qzero_width))
6357 {
6358 if (c >= 0)
6359 return glyphless_method;
6360 /* This method can't be used for the no-font case. */
6361 glyphless_method = Qempty_box;
6362 }
6363 if (EQ (glyphless_method, Qthin_space))
6364 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6365 else if (EQ (glyphless_method, Qempty_box))
6366 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6367 else if (EQ (glyphless_method, Qhex_code))
6368 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6369 else if (STRINGP (glyphless_method))
6370 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6371 else
6372 {
6373 /* Invalid value. We use the default method. */
6374 glyphless_method = Qnil;
6375 goto retry;
6376 }
6377 it->what = IT_GLYPHLESS;
6378 return glyphless_method;
6379 }
6380
6381 /* Load IT's display element fields with information about the next
6382 display element from the current position of IT. Value is zero if
6383 end of buffer (or C string) is reached. */
6384
6385 static struct frame *last_escape_glyph_frame = NULL;
6386 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6387 static int last_escape_glyph_merged_face_id = 0;
6388
6389 struct frame *last_glyphless_glyph_frame = NULL;
6390 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6391 int last_glyphless_glyph_merged_face_id = 0;
6392
6393 static int
6394 get_next_display_element (struct it *it)
6395 {
6396 /* Non-zero means that we found a display element. Zero means that
6397 we hit the end of what we iterate over. Performance note: the
6398 function pointer `method' used here turns out to be faster than
6399 using a sequence of if-statements. */
6400 int success_p;
6401
6402 get_next:
6403 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6404
6405 if (it->what == IT_CHARACTER)
6406 {
6407 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6408 and only if (a) the resolved directionality of that character
6409 is R..." */
6410 /* FIXME: Do we need an exception for characters from display
6411 tables? */
6412 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6413 it->c = bidi_mirror_char (it->c);
6414 /* Map via display table or translate control characters.
6415 IT->c, IT->len etc. have been set to the next character by
6416 the function call above. If we have a display table, and it
6417 contains an entry for IT->c, translate it. Don't do this if
6418 IT->c itself comes from a display table, otherwise we could
6419 end up in an infinite recursion. (An alternative could be to
6420 count the recursion depth of this function and signal an
6421 error when a certain maximum depth is reached.) Is it worth
6422 it? */
6423 if (success_p && it->dpvec == NULL)
6424 {
6425 Lisp_Object dv;
6426 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6427 int nonascii_space_p = 0;
6428 int nonascii_hyphen_p = 0;
6429 int c = it->c; /* This is the character to display. */
6430
6431 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6432 {
6433 xassert (SINGLE_BYTE_CHAR_P (c));
6434 if (unibyte_display_via_language_environment)
6435 {
6436 c = DECODE_CHAR (unibyte, c);
6437 if (c < 0)
6438 c = BYTE8_TO_CHAR (it->c);
6439 }
6440 else
6441 c = BYTE8_TO_CHAR (it->c);
6442 }
6443
6444 if (it->dp
6445 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6446 VECTORP (dv)))
6447 {
6448 struct Lisp_Vector *v = XVECTOR (dv);
6449
6450 /* Return the first character from the display table
6451 entry, if not empty. If empty, don't display the
6452 current character. */
6453 if (v->header.size)
6454 {
6455 it->dpvec_char_len = it->len;
6456 it->dpvec = v->contents;
6457 it->dpend = v->contents + v->header.size;
6458 it->current.dpvec_index = 0;
6459 it->dpvec_face_id = -1;
6460 it->saved_face_id = it->face_id;
6461 it->method = GET_FROM_DISPLAY_VECTOR;
6462 it->ellipsis_p = 0;
6463 }
6464 else
6465 {
6466 set_iterator_to_next (it, 0);
6467 }
6468 goto get_next;
6469 }
6470
6471 if (! NILP (lookup_glyphless_char_display (c, it)))
6472 {
6473 if (it->what == IT_GLYPHLESS)
6474 goto done;
6475 /* Don't display this character. */
6476 set_iterator_to_next (it, 0);
6477 goto get_next;
6478 }
6479
6480 /* If `nobreak-char-display' is non-nil, we display
6481 non-ASCII spaces and hyphens specially. */
6482 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6483 {
6484 if (c == 0xA0)
6485 nonascii_space_p = 1;
6486 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6487 nonascii_hyphen_p = 1;
6488 }
6489
6490 /* Translate control characters into `\003' or `^C' form.
6491 Control characters coming from a display table entry are
6492 currently not translated because we use IT->dpvec to hold
6493 the translation. This could easily be changed but I
6494 don't believe that it is worth doing.
6495
6496 The characters handled by `nobreak-char-display' must be
6497 translated too.
6498
6499 Non-printable characters and raw-byte characters are also
6500 translated to octal form. */
6501 if (((c < ' ' || c == 127) /* ASCII control chars */
6502 ? (it->area != TEXT_AREA
6503 /* In mode line, treat \n, \t like other crl chars. */
6504 || (c != '\t'
6505 && it->glyph_row
6506 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6507 || (c != '\n' && c != '\t'))
6508 : (nonascii_space_p
6509 || nonascii_hyphen_p
6510 || CHAR_BYTE8_P (c)
6511 || ! CHAR_PRINTABLE_P (c))))
6512 {
6513 /* C is a control character, non-ASCII space/hyphen,
6514 raw-byte, or a non-printable character which must be
6515 displayed either as '\003' or as `^C' where the '\\'
6516 and '^' can be defined in the display table. Fill
6517 IT->ctl_chars with glyphs for what we have to
6518 display. Then, set IT->dpvec to these glyphs. */
6519 Lisp_Object gc;
6520 int ctl_len;
6521 int face_id;
6522 EMACS_INT lface_id = 0;
6523 int escape_glyph;
6524
6525 /* Handle control characters with ^. */
6526
6527 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6528 {
6529 int g;
6530
6531 g = '^'; /* default glyph for Control */
6532 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6533 if (it->dp
6534 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6535 && GLYPH_CODE_CHAR_VALID_P (gc))
6536 {
6537 g = GLYPH_CODE_CHAR (gc);
6538 lface_id = GLYPH_CODE_FACE (gc);
6539 }
6540 if (lface_id)
6541 {
6542 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6543 }
6544 else if (it->f == last_escape_glyph_frame
6545 && it->face_id == last_escape_glyph_face_id)
6546 {
6547 face_id = last_escape_glyph_merged_face_id;
6548 }
6549 else
6550 {
6551 /* Merge the escape-glyph face into the current face. */
6552 face_id = merge_faces (it->f, Qescape_glyph, 0,
6553 it->face_id);
6554 last_escape_glyph_frame = it->f;
6555 last_escape_glyph_face_id = it->face_id;
6556 last_escape_glyph_merged_face_id = face_id;
6557 }
6558
6559 XSETINT (it->ctl_chars[0], g);
6560 XSETINT (it->ctl_chars[1], c ^ 0100);
6561 ctl_len = 2;
6562 goto display_control;
6563 }
6564
6565 /* Handle non-ascii space in the mode where it only gets
6566 highlighting. */
6567
6568 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6569 {
6570 /* Merge `nobreak-space' into the current face. */
6571 face_id = merge_faces (it->f, Qnobreak_space, 0,
6572 it->face_id);
6573 XSETINT (it->ctl_chars[0], ' ');
6574 ctl_len = 1;
6575 goto display_control;
6576 }
6577
6578 /* Handle sequences that start with the "escape glyph". */
6579
6580 /* the default escape glyph is \. */
6581 escape_glyph = '\\';
6582
6583 if (it->dp
6584 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6585 && GLYPH_CODE_CHAR_VALID_P (gc))
6586 {
6587 escape_glyph = GLYPH_CODE_CHAR (gc);
6588 lface_id = GLYPH_CODE_FACE (gc);
6589 }
6590 if (lface_id)
6591 {
6592 /* The display table specified a face.
6593 Merge it into face_id and also into escape_glyph. */
6594 face_id = merge_faces (it->f, Qt, lface_id,
6595 it->face_id);
6596 }
6597 else if (it->f == last_escape_glyph_frame
6598 && it->face_id == last_escape_glyph_face_id)
6599 {
6600 face_id = last_escape_glyph_merged_face_id;
6601 }
6602 else
6603 {
6604 /* Merge the escape-glyph face into the current face. */
6605 face_id = merge_faces (it->f, Qescape_glyph, 0,
6606 it->face_id);
6607 last_escape_glyph_frame = it->f;
6608 last_escape_glyph_face_id = it->face_id;
6609 last_escape_glyph_merged_face_id = face_id;
6610 }
6611
6612 /* Draw non-ASCII hyphen with just highlighting: */
6613
6614 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6615 {
6616 XSETINT (it->ctl_chars[0], '-');
6617 ctl_len = 1;
6618 goto display_control;
6619 }
6620
6621 /* Draw non-ASCII space/hyphen with escape glyph: */
6622
6623 if (nonascii_space_p || nonascii_hyphen_p)
6624 {
6625 XSETINT (it->ctl_chars[0], escape_glyph);
6626 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6627 ctl_len = 2;
6628 goto display_control;
6629 }
6630
6631 {
6632 char str[10];
6633 int len, i;
6634
6635 if (CHAR_BYTE8_P (c))
6636 /* Display \200 instead of \17777600. */
6637 c = CHAR_TO_BYTE8 (c);
6638 len = sprintf (str, "%03o", c);
6639
6640 XSETINT (it->ctl_chars[0], escape_glyph);
6641 for (i = 0; i < len; i++)
6642 XSETINT (it->ctl_chars[i + 1], str[i]);
6643 ctl_len = len + 1;
6644 }
6645
6646 display_control:
6647 /* Set up IT->dpvec and return first character from it. */
6648 it->dpvec_char_len = it->len;
6649 it->dpvec = it->ctl_chars;
6650 it->dpend = it->dpvec + ctl_len;
6651 it->current.dpvec_index = 0;
6652 it->dpvec_face_id = face_id;
6653 it->saved_face_id = it->face_id;
6654 it->method = GET_FROM_DISPLAY_VECTOR;
6655 it->ellipsis_p = 0;
6656 goto get_next;
6657 }
6658 it->char_to_display = c;
6659 }
6660 else if (success_p)
6661 {
6662 it->char_to_display = it->c;
6663 }
6664 }
6665
6666 /* Adjust face id for a multibyte character. There are no multibyte
6667 character in unibyte text. */
6668 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6669 && it->multibyte_p
6670 && success_p
6671 && FRAME_WINDOW_P (it->f))
6672 {
6673 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6674
6675 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6676 {
6677 /* Automatic composition with glyph-string. */
6678 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6679
6680 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6681 }
6682 else
6683 {
6684 EMACS_INT pos = (it->s ? -1
6685 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6686 : IT_CHARPOS (*it));
6687 int c;
6688
6689 if (it->what == IT_CHARACTER)
6690 c = it->char_to_display;
6691 else
6692 {
6693 struct composition *cmp = composition_table[it->cmp_it.id];
6694 int i;
6695
6696 c = ' ';
6697 for (i = 0; i < cmp->glyph_len; i++)
6698 /* TAB in a composition means display glyphs with
6699 padding space on the left or right. */
6700 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6701 break;
6702 }
6703 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6704 }
6705 }
6706
6707 done:
6708 /* Is this character the last one of a run of characters with
6709 box? If yes, set IT->end_of_box_run_p to 1. */
6710 if (it->face_box_p
6711 && it->s == NULL)
6712 {
6713 if (it->method == GET_FROM_STRING && it->sp)
6714 {
6715 int face_id = underlying_face_id (it);
6716 struct face *face = FACE_FROM_ID (it->f, face_id);
6717
6718 if (face)
6719 {
6720 if (face->box == FACE_NO_BOX)
6721 {
6722 /* If the box comes from face properties in a
6723 display string, check faces in that string. */
6724 int string_face_id = face_after_it_pos (it);
6725 it->end_of_box_run_p
6726 = (FACE_FROM_ID (it->f, string_face_id)->box
6727 == FACE_NO_BOX);
6728 }
6729 /* Otherwise, the box comes from the underlying face.
6730 If this is the last string character displayed, check
6731 the next buffer location. */
6732 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6733 && (it->current.overlay_string_index
6734 == it->n_overlay_strings - 1))
6735 {
6736 EMACS_INT ignore;
6737 int next_face_id;
6738 struct text_pos pos = it->current.pos;
6739 INC_TEXT_POS (pos, it->multibyte_p);
6740
6741 next_face_id = face_at_buffer_position
6742 (it->w, CHARPOS (pos), it->region_beg_charpos,
6743 it->region_end_charpos, &ignore,
6744 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6745 -1);
6746 it->end_of_box_run_p
6747 = (FACE_FROM_ID (it->f, next_face_id)->box
6748 == FACE_NO_BOX);
6749 }
6750 }
6751 }
6752 else
6753 {
6754 int face_id = face_after_it_pos (it);
6755 it->end_of_box_run_p
6756 = (face_id != it->face_id
6757 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6758 }
6759 }
6760
6761 /* Value is 0 if end of buffer or string reached. */
6762 return success_p;
6763 }
6764
6765
6766 /* Move IT to the next display element.
6767
6768 RESEAT_P non-zero means if called on a newline in buffer text,
6769 skip to the next visible line start.
6770
6771 Functions get_next_display_element and set_iterator_to_next are
6772 separate because I find this arrangement easier to handle than a
6773 get_next_display_element function that also increments IT's
6774 position. The way it is we can first look at an iterator's current
6775 display element, decide whether it fits on a line, and if it does,
6776 increment the iterator position. The other way around we probably
6777 would either need a flag indicating whether the iterator has to be
6778 incremented the next time, or we would have to implement a
6779 decrement position function which would not be easy to write. */
6780
6781 void
6782 set_iterator_to_next (struct it *it, int reseat_p)
6783 {
6784 /* Reset flags indicating start and end of a sequence of characters
6785 with box. Reset them at the start of this function because
6786 moving the iterator to a new position might set them. */
6787 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6788
6789 switch (it->method)
6790 {
6791 case GET_FROM_BUFFER:
6792 /* The current display element of IT is a character from
6793 current_buffer. Advance in the buffer, and maybe skip over
6794 invisible lines that are so because of selective display. */
6795 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6796 reseat_at_next_visible_line_start (it, 0);
6797 else if (it->cmp_it.id >= 0)
6798 {
6799 /* We are currently getting glyphs from a composition. */
6800 int i;
6801
6802 if (! it->bidi_p)
6803 {
6804 IT_CHARPOS (*it) += it->cmp_it.nchars;
6805 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6806 if (it->cmp_it.to < it->cmp_it.nglyphs)
6807 {
6808 it->cmp_it.from = it->cmp_it.to;
6809 }
6810 else
6811 {
6812 it->cmp_it.id = -1;
6813 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6814 IT_BYTEPOS (*it),
6815 it->end_charpos, Qnil);
6816 }
6817 }
6818 else if (! it->cmp_it.reversed_p)
6819 {
6820 /* Composition created while scanning forward. */
6821 /* Update IT's char/byte positions to point to the first
6822 character of the next grapheme cluster, or to the
6823 character visually after the current composition. */
6824 for (i = 0; i < it->cmp_it.nchars; i++)
6825 bidi_move_to_visually_next (&it->bidi_it);
6826 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6827 IT_CHARPOS (*it) = it->bidi_it.charpos;
6828
6829 if (it->cmp_it.to < it->cmp_it.nglyphs)
6830 {
6831 /* Proceed to the next grapheme cluster. */
6832 it->cmp_it.from = it->cmp_it.to;
6833 }
6834 else
6835 {
6836 /* No more grapheme clusters in this composition.
6837 Find the next stop position. */
6838 EMACS_INT stop = it->end_charpos;
6839 if (it->bidi_it.scan_dir < 0)
6840 /* Now we are scanning backward and don't know
6841 where to stop. */
6842 stop = -1;
6843 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6844 IT_BYTEPOS (*it), stop, Qnil);
6845 }
6846 }
6847 else
6848 {
6849 /* Composition created while scanning backward. */
6850 /* Update IT's char/byte positions to point to the last
6851 character of the previous grapheme cluster, or the
6852 character visually after the current composition. */
6853 for (i = 0; i < it->cmp_it.nchars; i++)
6854 bidi_move_to_visually_next (&it->bidi_it);
6855 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6856 IT_CHARPOS (*it) = it->bidi_it.charpos;
6857 if (it->cmp_it.from > 0)
6858 {
6859 /* Proceed to the previous grapheme cluster. */
6860 it->cmp_it.to = it->cmp_it.from;
6861 }
6862 else
6863 {
6864 /* No more grapheme clusters in this composition.
6865 Find the next stop position. */
6866 EMACS_INT stop = it->end_charpos;
6867 if (it->bidi_it.scan_dir < 0)
6868 /* Now we are scanning backward and don't know
6869 where to stop. */
6870 stop = -1;
6871 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6872 IT_BYTEPOS (*it), stop, Qnil);
6873 }
6874 }
6875 }
6876 else
6877 {
6878 xassert (it->len != 0);
6879
6880 if (!it->bidi_p)
6881 {
6882 IT_BYTEPOS (*it) += it->len;
6883 IT_CHARPOS (*it) += 1;
6884 }
6885 else
6886 {
6887 int prev_scan_dir = it->bidi_it.scan_dir;
6888 /* If this is a new paragraph, determine its base
6889 direction (a.k.a. its base embedding level). */
6890 if (it->bidi_it.new_paragraph)
6891 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6892 bidi_move_to_visually_next (&it->bidi_it);
6893 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6894 IT_CHARPOS (*it) = it->bidi_it.charpos;
6895 if (prev_scan_dir != it->bidi_it.scan_dir)
6896 {
6897 /* As the scan direction was changed, we must
6898 re-compute the stop position for composition. */
6899 EMACS_INT stop = it->end_charpos;
6900 if (it->bidi_it.scan_dir < 0)
6901 stop = -1;
6902 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6903 IT_BYTEPOS (*it), stop, Qnil);
6904 }
6905 }
6906 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6907 }
6908 break;
6909
6910 case GET_FROM_C_STRING:
6911 /* Current display element of IT is from a C string. */
6912 if (!it->bidi_p
6913 /* If the string position is beyond string's end, it means
6914 next_element_from_c_string is padding the string with
6915 blanks, in which case we bypass the bidi iterator,
6916 because it cannot deal with such virtual characters. */
6917 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6918 {
6919 IT_BYTEPOS (*it) += it->len;
6920 IT_CHARPOS (*it) += 1;
6921 }
6922 else
6923 {
6924 bidi_move_to_visually_next (&it->bidi_it);
6925 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6926 IT_CHARPOS (*it) = it->bidi_it.charpos;
6927 }
6928 break;
6929
6930 case GET_FROM_DISPLAY_VECTOR:
6931 /* Current display element of IT is from a display table entry.
6932 Advance in the display table definition. Reset it to null if
6933 end reached, and continue with characters from buffers/
6934 strings. */
6935 ++it->current.dpvec_index;
6936
6937 /* Restore face of the iterator to what they were before the
6938 display vector entry (these entries may contain faces). */
6939 it->face_id = it->saved_face_id;
6940
6941 if (it->dpvec + it->current.dpvec_index == it->dpend)
6942 {
6943 int recheck_faces = it->ellipsis_p;
6944
6945 if (it->s)
6946 it->method = GET_FROM_C_STRING;
6947 else if (STRINGP (it->string))
6948 it->method = GET_FROM_STRING;
6949 else
6950 {
6951 it->method = GET_FROM_BUFFER;
6952 it->object = it->w->buffer;
6953 }
6954
6955 it->dpvec = NULL;
6956 it->current.dpvec_index = -1;
6957
6958 /* Skip over characters which were displayed via IT->dpvec. */
6959 if (it->dpvec_char_len < 0)
6960 reseat_at_next_visible_line_start (it, 1);
6961 else if (it->dpvec_char_len > 0)
6962 {
6963 if (it->method == GET_FROM_STRING
6964 && it->n_overlay_strings > 0)
6965 it->ignore_overlay_strings_at_pos_p = 1;
6966 it->len = it->dpvec_char_len;
6967 set_iterator_to_next (it, reseat_p);
6968 }
6969
6970 /* Maybe recheck faces after display vector */
6971 if (recheck_faces)
6972 it->stop_charpos = IT_CHARPOS (*it);
6973 }
6974 break;
6975
6976 case GET_FROM_STRING:
6977 /* Current display element is a character from a Lisp string. */
6978 xassert (it->s == NULL && STRINGP (it->string));
6979 if (it->cmp_it.id >= 0)
6980 {
6981 int i;
6982
6983 if (! it->bidi_p)
6984 {
6985 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6986 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6987 if (it->cmp_it.to < it->cmp_it.nglyphs)
6988 it->cmp_it.from = it->cmp_it.to;
6989 else
6990 {
6991 it->cmp_it.id = -1;
6992 composition_compute_stop_pos (&it->cmp_it,
6993 IT_STRING_CHARPOS (*it),
6994 IT_STRING_BYTEPOS (*it),
6995 it->end_charpos, it->string);
6996 }
6997 }
6998 else if (! it->cmp_it.reversed_p)
6999 {
7000 for (i = 0; i < it->cmp_it.nchars; i++)
7001 bidi_move_to_visually_next (&it->bidi_it);
7002 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7003 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7004
7005 if (it->cmp_it.to < it->cmp_it.nglyphs)
7006 it->cmp_it.from = it->cmp_it.to;
7007 else
7008 {
7009 EMACS_INT stop = it->end_charpos;
7010 if (it->bidi_it.scan_dir < 0)
7011 stop = -1;
7012 composition_compute_stop_pos (&it->cmp_it,
7013 IT_STRING_CHARPOS (*it),
7014 IT_STRING_BYTEPOS (*it), stop,
7015 it->string);
7016 }
7017 }
7018 else
7019 {
7020 for (i = 0; i < it->cmp_it.nchars; i++)
7021 bidi_move_to_visually_next (&it->bidi_it);
7022 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7023 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7024 if (it->cmp_it.from > 0)
7025 it->cmp_it.to = it->cmp_it.from;
7026 else
7027 {
7028 EMACS_INT stop = it->end_charpos;
7029 if (it->bidi_it.scan_dir < 0)
7030 stop = -1;
7031 composition_compute_stop_pos (&it->cmp_it,
7032 IT_STRING_CHARPOS (*it),
7033 IT_STRING_BYTEPOS (*it), stop,
7034 it->string);
7035 }
7036 }
7037 }
7038 else
7039 {
7040 if (!it->bidi_p
7041 /* If the string position is beyond string's end, it
7042 means next_element_from_string is padding the string
7043 with blanks, in which case we bypass the bidi
7044 iterator, because it cannot deal with such virtual
7045 characters. */
7046 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7047 {
7048 IT_STRING_BYTEPOS (*it) += it->len;
7049 IT_STRING_CHARPOS (*it) += 1;
7050 }
7051 else
7052 {
7053 int prev_scan_dir = it->bidi_it.scan_dir;
7054
7055 bidi_move_to_visually_next (&it->bidi_it);
7056 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7057 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7058 if (prev_scan_dir != it->bidi_it.scan_dir)
7059 {
7060 EMACS_INT stop = it->end_charpos;
7061
7062 if (it->bidi_it.scan_dir < 0)
7063 stop = -1;
7064 composition_compute_stop_pos (&it->cmp_it,
7065 IT_STRING_CHARPOS (*it),
7066 IT_STRING_BYTEPOS (*it), stop,
7067 it->string);
7068 }
7069 }
7070 }
7071
7072 consider_string_end:
7073
7074 if (it->current.overlay_string_index >= 0)
7075 {
7076 /* IT->string is an overlay string. Advance to the
7077 next, if there is one. */
7078 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7079 {
7080 it->ellipsis_p = 0;
7081 next_overlay_string (it);
7082 if (it->ellipsis_p)
7083 setup_for_ellipsis (it, 0);
7084 }
7085 }
7086 else
7087 {
7088 /* IT->string is not an overlay string. If we reached
7089 its end, and there is something on IT->stack, proceed
7090 with what is on the stack. This can be either another
7091 string, this time an overlay string, or a buffer. */
7092 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7093 && it->sp > 0)
7094 {
7095 pop_it (it);
7096 if (it->method == GET_FROM_STRING)
7097 goto consider_string_end;
7098 }
7099 }
7100 break;
7101
7102 case GET_FROM_IMAGE:
7103 case GET_FROM_STRETCH:
7104 /* The position etc with which we have to proceed are on
7105 the stack. The position may be at the end of a string,
7106 if the `display' property takes up the whole string. */
7107 xassert (it->sp > 0);
7108 pop_it (it);
7109 if (it->method == GET_FROM_STRING)
7110 goto consider_string_end;
7111 break;
7112
7113 default:
7114 /* There are no other methods defined, so this should be a bug. */
7115 abort ();
7116 }
7117
7118 xassert (it->method != GET_FROM_STRING
7119 || (STRINGP (it->string)
7120 && IT_STRING_CHARPOS (*it) >= 0));
7121 }
7122
7123 /* Load IT's display element fields with information about the next
7124 display element which comes from a display table entry or from the
7125 result of translating a control character to one of the forms `^C'
7126 or `\003'.
7127
7128 IT->dpvec holds the glyphs to return as characters.
7129 IT->saved_face_id holds the face id before the display vector--it
7130 is restored into IT->face_id in set_iterator_to_next. */
7131
7132 static int
7133 next_element_from_display_vector (struct it *it)
7134 {
7135 Lisp_Object gc;
7136
7137 /* Precondition. */
7138 xassert (it->dpvec && it->current.dpvec_index >= 0);
7139
7140 it->face_id = it->saved_face_id;
7141
7142 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7143 That seemed totally bogus - so I changed it... */
7144 gc = it->dpvec[it->current.dpvec_index];
7145
7146 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
7147 {
7148 it->c = GLYPH_CODE_CHAR (gc);
7149 it->len = CHAR_BYTES (it->c);
7150
7151 /* The entry may contain a face id to use. Such a face id is
7152 the id of a Lisp face, not a realized face. A face id of
7153 zero means no face is specified. */
7154 if (it->dpvec_face_id >= 0)
7155 it->face_id = it->dpvec_face_id;
7156 else
7157 {
7158 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
7159 if (lface_id > 0)
7160 it->face_id = merge_faces (it->f, Qt, lface_id,
7161 it->saved_face_id);
7162 }
7163 }
7164 else
7165 /* Display table entry is invalid. Return a space. */
7166 it->c = ' ', it->len = 1;
7167
7168 /* Don't change position and object of the iterator here. They are
7169 still the values of the character that had this display table
7170 entry or was translated, and that's what we want. */
7171 it->what = IT_CHARACTER;
7172 return 1;
7173 }
7174
7175 /* Get the first element of string/buffer in the visual order, after
7176 being reseated to a new position in a string or a buffer. */
7177 static void
7178 get_visually_first_element (struct it *it)
7179 {
7180 int string_p = STRINGP (it->string) || it->s;
7181 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7182 EMACS_INT bob = (string_p ? 0 : BEGV);
7183
7184 if (STRINGP (it->string))
7185 {
7186 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7187 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7188 }
7189 else
7190 {
7191 it->bidi_it.charpos = IT_CHARPOS (*it);
7192 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7193 }
7194
7195 if (it->bidi_it.charpos == eob)
7196 {
7197 /* Nothing to do, but reset the FIRST_ELT flag, like
7198 bidi_paragraph_init does, because we are not going to
7199 call it. */
7200 it->bidi_it.first_elt = 0;
7201 }
7202 else if (it->bidi_it.charpos == bob
7203 || (!string_p
7204 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7205 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7206 {
7207 /* If we are at the beginning of a line/string, we can produce
7208 the next element right away. */
7209 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7210 bidi_move_to_visually_next (&it->bidi_it);
7211 }
7212 else
7213 {
7214 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7215
7216 /* We need to prime the bidi iterator starting at the line's or
7217 string's beginning, before we will be able to produce the
7218 next element. */
7219 if (string_p)
7220 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7221 else
7222 {
7223 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7224 -1);
7225 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7226 }
7227 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7228 do
7229 {
7230 /* Now return to buffer/string position where we were asked
7231 to get the next display element, and produce that. */
7232 bidi_move_to_visually_next (&it->bidi_it);
7233 }
7234 while (it->bidi_it.bytepos != orig_bytepos
7235 && it->bidi_it.charpos < eob);
7236 }
7237
7238 /* Adjust IT's position information to where we ended up. */
7239 if (STRINGP (it->string))
7240 {
7241 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7242 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7243 }
7244 else
7245 {
7246 IT_CHARPOS (*it) = it->bidi_it.charpos;
7247 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7248 }
7249
7250 if (STRINGP (it->string) || !it->s)
7251 {
7252 EMACS_INT stop, charpos, bytepos;
7253
7254 if (STRINGP (it->string))
7255 {
7256 xassert (!it->s);
7257 stop = SCHARS (it->string);
7258 if (stop > it->end_charpos)
7259 stop = it->end_charpos;
7260 charpos = IT_STRING_CHARPOS (*it);
7261 bytepos = IT_STRING_BYTEPOS (*it);
7262 }
7263 else
7264 {
7265 stop = it->end_charpos;
7266 charpos = IT_CHARPOS (*it);
7267 bytepos = IT_BYTEPOS (*it);
7268 }
7269 if (it->bidi_it.scan_dir < 0)
7270 stop = -1;
7271 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7272 it->string);
7273 }
7274 }
7275
7276 /* Load IT with the next display element from Lisp string IT->string.
7277 IT->current.string_pos is the current position within the string.
7278 If IT->current.overlay_string_index >= 0, the Lisp string is an
7279 overlay string. */
7280
7281 static int
7282 next_element_from_string (struct it *it)
7283 {
7284 struct text_pos position;
7285
7286 xassert (STRINGP (it->string));
7287 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7288 xassert (IT_STRING_CHARPOS (*it) >= 0);
7289 position = it->current.string_pos;
7290
7291 /* With bidi reordering, the character to display might not be the
7292 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7293 that we were reseat()ed to a new string, whose paragraph
7294 direction is not known. */
7295 if (it->bidi_p && it->bidi_it.first_elt)
7296 {
7297 get_visually_first_element (it);
7298 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7299 }
7300
7301 /* Time to check for invisible text? */
7302 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7303 {
7304 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7305 {
7306 if (!(!it->bidi_p
7307 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7308 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7309 {
7310 /* With bidi non-linear iteration, we could find
7311 ourselves far beyond the last computed stop_charpos,
7312 with several other stop positions in between that we
7313 missed. Scan them all now, in buffer's logical
7314 order, until we find and handle the last stop_charpos
7315 that precedes our current position. */
7316 handle_stop_backwards (it, it->stop_charpos);
7317 return GET_NEXT_DISPLAY_ELEMENT (it);
7318 }
7319 else
7320 {
7321 if (it->bidi_p)
7322 {
7323 /* Take note of the stop position we just moved
7324 across, for when we will move back across it. */
7325 it->prev_stop = it->stop_charpos;
7326 /* If we are at base paragraph embedding level, take
7327 note of the last stop position seen at this
7328 level. */
7329 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7330 it->base_level_stop = it->stop_charpos;
7331 }
7332 handle_stop (it);
7333
7334 /* Since a handler may have changed IT->method, we must
7335 recurse here. */
7336 return GET_NEXT_DISPLAY_ELEMENT (it);
7337 }
7338 }
7339 else if (it->bidi_p
7340 /* If we are before prev_stop, we may have overstepped
7341 on our way backwards a stop_pos, and if so, we need
7342 to handle that stop_pos. */
7343 && IT_STRING_CHARPOS (*it) < it->prev_stop
7344 /* We can sometimes back up for reasons that have nothing
7345 to do with bidi reordering. E.g., compositions. The
7346 code below is only needed when we are above the base
7347 embedding level, so test for that explicitly. */
7348 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7349 {
7350 /* If we lost track of base_level_stop, we have no better
7351 place for handle_stop_backwards to start from than string
7352 beginning. This happens, e.g., when we were reseated to
7353 the previous screenful of text by vertical-motion. */
7354 if (it->base_level_stop <= 0
7355 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7356 it->base_level_stop = 0;
7357 handle_stop_backwards (it, it->base_level_stop);
7358 return GET_NEXT_DISPLAY_ELEMENT (it);
7359 }
7360 }
7361
7362 if (it->current.overlay_string_index >= 0)
7363 {
7364 /* Get the next character from an overlay string. In overlay
7365 strings, there is no field width or padding with spaces to
7366 do. */
7367 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7368 {
7369 it->what = IT_EOB;
7370 return 0;
7371 }
7372 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7373 IT_STRING_BYTEPOS (*it),
7374 it->bidi_it.scan_dir < 0
7375 ? -1
7376 : SCHARS (it->string))
7377 && next_element_from_composition (it))
7378 {
7379 return 1;
7380 }
7381 else if (STRING_MULTIBYTE (it->string))
7382 {
7383 const unsigned char *s = (SDATA (it->string)
7384 + IT_STRING_BYTEPOS (*it));
7385 it->c = string_char_and_length (s, &it->len);
7386 }
7387 else
7388 {
7389 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7390 it->len = 1;
7391 }
7392 }
7393 else
7394 {
7395 /* Get the next character from a Lisp string that is not an
7396 overlay string. Such strings come from the mode line, for
7397 example. We may have to pad with spaces, or truncate the
7398 string. See also next_element_from_c_string. */
7399 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7400 {
7401 it->what = IT_EOB;
7402 return 0;
7403 }
7404 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7405 {
7406 /* Pad with spaces. */
7407 it->c = ' ', it->len = 1;
7408 CHARPOS (position) = BYTEPOS (position) = -1;
7409 }
7410 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7411 IT_STRING_BYTEPOS (*it),
7412 it->bidi_it.scan_dir < 0
7413 ? -1
7414 : it->string_nchars)
7415 && next_element_from_composition (it))
7416 {
7417 return 1;
7418 }
7419 else if (STRING_MULTIBYTE (it->string))
7420 {
7421 const unsigned char *s = (SDATA (it->string)
7422 + IT_STRING_BYTEPOS (*it));
7423 it->c = string_char_and_length (s, &it->len);
7424 }
7425 else
7426 {
7427 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7428 it->len = 1;
7429 }
7430 }
7431
7432 /* Record what we have and where it came from. */
7433 it->what = IT_CHARACTER;
7434 it->object = it->string;
7435 it->position = position;
7436 return 1;
7437 }
7438
7439
7440 /* Load IT with next display element from C string IT->s.
7441 IT->string_nchars is the maximum number of characters to return
7442 from the string. IT->end_charpos may be greater than
7443 IT->string_nchars when this function is called, in which case we
7444 may have to return padding spaces. Value is zero if end of string
7445 reached, including padding spaces. */
7446
7447 static int
7448 next_element_from_c_string (struct it *it)
7449 {
7450 int success_p = 1;
7451
7452 xassert (it->s);
7453 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7454 it->what = IT_CHARACTER;
7455 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7456 it->object = Qnil;
7457
7458 /* With bidi reordering, the character to display might not be the
7459 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7460 we were reseated to a new string, whose paragraph direction is
7461 not known. */
7462 if (it->bidi_p && it->bidi_it.first_elt)
7463 get_visually_first_element (it);
7464
7465 /* IT's position can be greater than IT->string_nchars in case a
7466 field width or precision has been specified when the iterator was
7467 initialized. */
7468 if (IT_CHARPOS (*it) >= it->end_charpos)
7469 {
7470 /* End of the game. */
7471 it->what = IT_EOB;
7472 success_p = 0;
7473 }
7474 else if (IT_CHARPOS (*it) >= it->string_nchars)
7475 {
7476 /* Pad with spaces. */
7477 it->c = ' ', it->len = 1;
7478 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7479 }
7480 else if (it->multibyte_p)
7481 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7482 else
7483 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7484
7485 return success_p;
7486 }
7487
7488
7489 /* Set up IT to return characters from an ellipsis, if appropriate.
7490 The definition of the ellipsis glyphs may come from a display table
7491 entry. This function fills IT with the first glyph from the
7492 ellipsis if an ellipsis is to be displayed. */
7493
7494 static int
7495 next_element_from_ellipsis (struct it *it)
7496 {
7497 if (it->selective_display_ellipsis_p)
7498 setup_for_ellipsis (it, it->len);
7499 else
7500 {
7501 /* The face at the current position may be different from the
7502 face we find after the invisible text. Remember what it
7503 was in IT->saved_face_id, and signal that it's there by
7504 setting face_before_selective_p. */
7505 it->saved_face_id = it->face_id;
7506 it->method = GET_FROM_BUFFER;
7507 it->object = it->w->buffer;
7508 reseat_at_next_visible_line_start (it, 1);
7509 it->face_before_selective_p = 1;
7510 }
7511
7512 return GET_NEXT_DISPLAY_ELEMENT (it);
7513 }
7514
7515
7516 /* Deliver an image display element. The iterator IT is already
7517 filled with image information (done in handle_display_prop). Value
7518 is always 1. */
7519
7520
7521 static int
7522 next_element_from_image (struct it *it)
7523 {
7524 it->what = IT_IMAGE;
7525 it->ignore_overlay_strings_at_pos_p = 0;
7526 return 1;
7527 }
7528
7529
7530 /* Fill iterator IT with next display element from a stretch glyph
7531 property. IT->object is the value of the text property. Value is
7532 always 1. */
7533
7534 static int
7535 next_element_from_stretch (struct it *it)
7536 {
7537 it->what = IT_STRETCH;
7538 return 1;
7539 }
7540
7541 /* Scan backwards from IT's current position until we find a stop
7542 position, or until BEGV. This is called when we find ourself
7543 before both the last known prev_stop and base_level_stop while
7544 reordering bidirectional text. */
7545
7546 static void
7547 compute_stop_pos_backwards (struct it *it)
7548 {
7549 const int SCAN_BACK_LIMIT = 1000;
7550 struct text_pos pos;
7551 struct display_pos save_current = it->current;
7552 struct text_pos save_position = it->position;
7553 EMACS_INT charpos = IT_CHARPOS (*it);
7554 EMACS_INT where_we_are = charpos;
7555 EMACS_INT save_stop_pos = it->stop_charpos;
7556 EMACS_INT save_end_pos = it->end_charpos;
7557
7558 xassert (NILP (it->string) && !it->s);
7559 xassert (it->bidi_p);
7560 it->bidi_p = 0;
7561 do
7562 {
7563 it->end_charpos = min (charpos + 1, ZV);
7564 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7565 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7566 reseat_1 (it, pos, 0);
7567 compute_stop_pos (it);
7568 /* We must advance forward, right? */
7569 if (it->stop_charpos <= charpos)
7570 abort ();
7571 }
7572 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7573
7574 if (it->stop_charpos <= where_we_are)
7575 it->prev_stop = it->stop_charpos;
7576 else
7577 it->prev_stop = BEGV;
7578 it->bidi_p = 1;
7579 it->current = save_current;
7580 it->position = save_position;
7581 it->stop_charpos = save_stop_pos;
7582 it->end_charpos = save_end_pos;
7583 }
7584
7585 /* Scan forward from CHARPOS in the current buffer/string, until we
7586 find a stop position > current IT's position. Then handle the stop
7587 position before that. This is called when we bump into a stop
7588 position while reordering bidirectional text. CHARPOS should be
7589 the last previously processed stop_pos (or BEGV/0, if none were
7590 processed yet) whose position is less that IT's current
7591 position. */
7592
7593 static void
7594 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7595 {
7596 int bufp = !STRINGP (it->string);
7597 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7598 struct display_pos save_current = it->current;
7599 struct text_pos save_position = it->position;
7600 struct text_pos pos1;
7601 EMACS_INT next_stop;
7602
7603 /* Scan in strict logical order. */
7604 xassert (it->bidi_p);
7605 it->bidi_p = 0;
7606 do
7607 {
7608 it->prev_stop = charpos;
7609 if (bufp)
7610 {
7611 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7612 reseat_1 (it, pos1, 0);
7613 }
7614 else
7615 it->current.string_pos = string_pos (charpos, it->string);
7616 compute_stop_pos (it);
7617 /* We must advance forward, right? */
7618 if (it->stop_charpos <= it->prev_stop)
7619 abort ();
7620 charpos = it->stop_charpos;
7621 }
7622 while (charpos <= where_we_are);
7623
7624 it->bidi_p = 1;
7625 it->current = save_current;
7626 it->position = save_position;
7627 next_stop = it->stop_charpos;
7628 it->stop_charpos = it->prev_stop;
7629 handle_stop (it);
7630 it->stop_charpos = next_stop;
7631 }
7632
7633 /* Load IT with the next display element from current_buffer. Value
7634 is zero if end of buffer reached. IT->stop_charpos is the next
7635 position at which to stop and check for text properties or buffer
7636 end. */
7637
7638 static int
7639 next_element_from_buffer (struct it *it)
7640 {
7641 int success_p = 1;
7642
7643 xassert (IT_CHARPOS (*it) >= BEGV);
7644 xassert (NILP (it->string) && !it->s);
7645 xassert (!it->bidi_p
7646 || (EQ (it->bidi_it.string.lstring, Qnil)
7647 && it->bidi_it.string.s == NULL));
7648
7649 /* With bidi reordering, the character to display might not be the
7650 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7651 we were reseat()ed to a new buffer position, which is potentially
7652 a different paragraph. */
7653 if (it->bidi_p && it->bidi_it.first_elt)
7654 {
7655 get_visually_first_element (it);
7656 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7657 }
7658
7659 if (IT_CHARPOS (*it) >= it->stop_charpos)
7660 {
7661 if (IT_CHARPOS (*it) >= it->end_charpos)
7662 {
7663 int overlay_strings_follow_p;
7664
7665 /* End of the game, except when overlay strings follow that
7666 haven't been returned yet. */
7667 if (it->overlay_strings_at_end_processed_p)
7668 overlay_strings_follow_p = 0;
7669 else
7670 {
7671 it->overlay_strings_at_end_processed_p = 1;
7672 overlay_strings_follow_p = get_overlay_strings (it, 0);
7673 }
7674
7675 if (overlay_strings_follow_p)
7676 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7677 else
7678 {
7679 it->what = IT_EOB;
7680 it->position = it->current.pos;
7681 success_p = 0;
7682 }
7683 }
7684 else if (!(!it->bidi_p
7685 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7686 || IT_CHARPOS (*it) == it->stop_charpos))
7687 {
7688 /* With bidi non-linear iteration, we could find ourselves
7689 far beyond the last computed stop_charpos, with several
7690 other stop positions in between that we missed. Scan
7691 them all now, in buffer's logical order, until we find
7692 and handle the last stop_charpos that precedes our
7693 current position. */
7694 handle_stop_backwards (it, it->stop_charpos);
7695 return GET_NEXT_DISPLAY_ELEMENT (it);
7696 }
7697 else
7698 {
7699 if (it->bidi_p)
7700 {
7701 /* Take note of the stop position we just moved across,
7702 for when we will move back across it. */
7703 it->prev_stop = it->stop_charpos;
7704 /* If we are at base paragraph embedding level, take
7705 note of the last stop position seen at this
7706 level. */
7707 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7708 it->base_level_stop = it->stop_charpos;
7709 }
7710 handle_stop (it);
7711 return GET_NEXT_DISPLAY_ELEMENT (it);
7712 }
7713 }
7714 else if (it->bidi_p
7715 /* If we are before prev_stop, we may have overstepped on
7716 our way backwards a stop_pos, and if so, we need to
7717 handle that stop_pos. */
7718 && IT_CHARPOS (*it) < it->prev_stop
7719 /* We can sometimes back up for reasons that have nothing
7720 to do with bidi reordering. E.g., compositions. The
7721 code below is only needed when we are above the base
7722 embedding level, so test for that explicitly. */
7723 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7724 {
7725 if (it->base_level_stop <= 0
7726 || IT_CHARPOS (*it) < it->base_level_stop)
7727 {
7728 /* If we lost track of base_level_stop, we need to find
7729 prev_stop by looking backwards. This happens, e.g., when
7730 we were reseated to the previous screenful of text by
7731 vertical-motion. */
7732 it->base_level_stop = BEGV;
7733 compute_stop_pos_backwards (it);
7734 handle_stop_backwards (it, it->prev_stop);
7735 }
7736 else
7737 handle_stop_backwards (it, it->base_level_stop);
7738 return GET_NEXT_DISPLAY_ELEMENT (it);
7739 }
7740 else
7741 {
7742 /* No face changes, overlays etc. in sight, so just return a
7743 character from current_buffer. */
7744 unsigned char *p;
7745 EMACS_INT stop;
7746
7747 /* Maybe run the redisplay end trigger hook. Performance note:
7748 This doesn't seem to cost measurable time. */
7749 if (it->redisplay_end_trigger_charpos
7750 && it->glyph_row
7751 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7752 run_redisplay_end_trigger_hook (it);
7753
7754 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7755 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7756 stop)
7757 && next_element_from_composition (it))
7758 {
7759 return 1;
7760 }
7761
7762 /* Get the next character, maybe multibyte. */
7763 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7764 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7765 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7766 else
7767 it->c = *p, it->len = 1;
7768
7769 /* Record what we have and where it came from. */
7770 it->what = IT_CHARACTER;
7771 it->object = it->w->buffer;
7772 it->position = it->current.pos;
7773
7774 /* Normally we return the character found above, except when we
7775 really want to return an ellipsis for selective display. */
7776 if (it->selective)
7777 {
7778 if (it->c == '\n')
7779 {
7780 /* A value of selective > 0 means hide lines indented more
7781 than that number of columns. */
7782 if (it->selective > 0
7783 && IT_CHARPOS (*it) + 1 < ZV
7784 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7785 IT_BYTEPOS (*it) + 1,
7786 it->selective))
7787 {
7788 success_p = next_element_from_ellipsis (it);
7789 it->dpvec_char_len = -1;
7790 }
7791 }
7792 else if (it->c == '\r' && it->selective == -1)
7793 {
7794 /* A value of selective == -1 means that everything from the
7795 CR to the end of the line is invisible, with maybe an
7796 ellipsis displayed for it. */
7797 success_p = next_element_from_ellipsis (it);
7798 it->dpvec_char_len = -1;
7799 }
7800 }
7801 }
7802
7803 /* Value is zero if end of buffer reached. */
7804 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7805 return success_p;
7806 }
7807
7808
7809 /* Run the redisplay end trigger hook for IT. */
7810
7811 static void
7812 run_redisplay_end_trigger_hook (struct it *it)
7813 {
7814 Lisp_Object args[3];
7815
7816 /* IT->glyph_row should be non-null, i.e. we should be actually
7817 displaying something, or otherwise we should not run the hook. */
7818 xassert (it->glyph_row);
7819
7820 /* Set up hook arguments. */
7821 args[0] = Qredisplay_end_trigger_functions;
7822 args[1] = it->window;
7823 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7824 it->redisplay_end_trigger_charpos = 0;
7825
7826 /* Since we are *trying* to run these functions, don't try to run
7827 them again, even if they get an error. */
7828 it->w->redisplay_end_trigger = Qnil;
7829 Frun_hook_with_args (3, args);
7830
7831 /* Notice if it changed the face of the character we are on. */
7832 handle_face_prop (it);
7833 }
7834
7835
7836 /* Deliver a composition display element. Unlike the other
7837 next_element_from_XXX, this function is not registered in the array
7838 get_next_element[]. It is called from next_element_from_buffer and
7839 next_element_from_string when necessary. */
7840
7841 static int
7842 next_element_from_composition (struct it *it)
7843 {
7844 it->what = IT_COMPOSITION;
7845 it->len = it->cmp_it.nbytes;
7846 if (STRINGP (it->string))
7847 {
7848 if (it->c < 0)
7849 {
7850 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7851 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7852 return 0;
7853 }
7854 it->position = it->current.string_pos;
7855 it->object = it->string;
7856 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7857 IT_STRING_BYTEPOS (*it), it->string);
7858 }
7859 else
7860 {
7861 if (it->c < 0)
7862 {
7863 IT_CHARPOS (*it) += it->cmp_it.nchars;
7864 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7865 if (it->bidi_p)
7866 {
7867 if (it->bidi_it.new_paragraph)
7868 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7869 /* Resync the bidi iterator with IT's new position.
7870 FIXME: this doesn't support bidirectional text. */
7871 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7872 bidi_move_to_visually_next (&it->bidi_it);
7873 }
7874 return 0;
7875 }
7876 it->position = it->current.pos;
7877 it->object = it->w->buffer;
7878 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7879 IT_BYTEPOS (*it), Qnil);
7880 }
7881 return 1;
7882 }
7883
7884
7885 \f
7886 /***********************************************************************
7887 Moving an iterator without producing glyphs
7888 ***********************************************************************/
7889
7890 /* Check if iterator is at a position corresponding to a valid buffer
7891 position after some move_it_ call. */
7892
7893 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7894 ((it)->method == GET_FROM_STRING \
7895 ? IT_STRING_CHARPOS (*it) == 0 \
7896 : 1)
7897
7898
7899 /* Move iterator IT to a specified buffer or X position within one
7900 line on the display without producing glyphs.
7901
7902 OP should be a bit mask including some or all of these bits:
7903 MOVE_TO_X: Stop upon reaching x-position TO_X.
7904 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7905 Regardless of OP's value, stop upon reaching the end of the display line.
7906
7907 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7908 This means, in particular, that TO_X includes window's horizontal
7909 scroll amount.
7910
7911 The return value has several possible values that
7912 say what condition caused the scan to stop:
7913
7914 MOVE_POS_MATCH_OR_ZV
7915 - when TO_POS or ZV was reached.
7916
7917 MOVE_X_REACHED
7918 -when TO_X was reached before TO_POS or ZV were reached.
7919
7920 MOVE_LINE_CONTINUED
7921 - when we reached the end of the display area and the line must
7922 be continued.
7923
7924 MOVE_LINE_TRUNCATED
7925 - when we reached the end of the display area and the line is
7926 truncated.
7927
7928 MOVE_NEWLINE_OR_CR
7929 - when we stopped at a line end, i.e. a newline or a CR and selective
7930 display is on. */
7931
7932 static enum move_it_result
7933 move_it_in_display_line_to (struct it *it,
7934 EMACS_INT to_charpos, int to_x,
7935 enum move_operation_enum op)
7936 {
7937 enum move_it_result result = MOVE_UNDEFINED;
7938 struct glyph_row *saved_glyph_row;
7939 struct it wrap_it, atpos_it, atx_it, ppos_it;
7940 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7941 void *ppos_data = NULL;
7942 int may_wrap = 0;
7943 enum it_method prev_method = it->method;
7944 EMACS_INT prev_pos = IT_CHARPOS (*it);
7945 int saw_smaller_pos = prev_pos < to_charpos;
7946
7947 /* Don't produce glyphs in produce_glyphs. */
7948 saved_glyph_row = it->glyph_row;
7949 it->glyph_row = NULL;
7950
7951 /* Use wrap_it to save a copy of IT wherever a word wrap could
7952 occur. Use atpos_it to save a copy of IT at the desired buffer
7953 position, if found, so that we can scan ahead and check if the
7954 word later overshoots the window edge. Use atx_it similarly, for
7955 pixel positions. */
7956 wrap_it.sp = -1;
7957 atpos_it.sp = -1;
7958 atx_it.sp = -1;
7959
7960 /* Use ppos_it under bidi reordering to save a copy of IT for the
7961 position > CHARPOS that is the closest to CHARPOS. We restore
7962 that position in IT when we have scanned the entire display line
7963 without finding a match for CHARPOS and all the character
7964 positions are greater than CHARPOS. */
7965 if (it->bidi_p)
7966 {
7967 SAVE_IT (ppos_it, *it, ppos_data);
7968 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7969 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7970 SAVE_IT (ppos_it, *it, ppos_data);
7971 }
7972
7973 #define BUFFER_POS_REACHED_P() \
7974 ((op & MOVE_TO_POS) != 0 \
7975 && BUFFERP (it->object) \
7976 && (IT_CHARPOS (*it) == to_charpos \
7977 || ((!it->bidi_p \
7978 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
7979 && IT_CHARPOS (*it) > to_charpos) \
7980 || (it->what == IT_COMPOSITION \
7981 && ((IT_CHARPOS (*it) > to_charpos \
7982 && to_charpos >= it->cmp_it.charpos) \
7983 || (IT_CHARPOS (*it) < to_charpos \
7984 && to_charpos <= it->cmp_it.charpos)))) \
7985 && (it->method == GET_FROM_BUFFER \
7986 || (it->method == GET_FROM_DISPLAY_VECTOR \
7987 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7988
7989 /* If there's a line-/wrap-prefix, handle it. */
7990 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7991 && it->current_y < it->last_visible_y)
7992 handle_line_prefix (it);
7993
7994 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7995 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7996
7997 while (1)
7998 {
7999 int x, i, ascent = 0, descent = 0;
8000
8001 /* Utility macro to reset an iterator with x, ascent, and descent. */
8002 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8003 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8004 (IT)->max_descent = descent)
8005
8006 /* Stop if we move beyond TO_CHARPOS (after an image or a
8007 display string or stretch glyph). */
8008 if ((op & MOVE_TO_POS) != 0
8009 && BUFFERP (it->object)
8010 && it->method == GET_FROM_BUFFER
8011 && (((!it->bidi_p
8012 /* When the iterator is at base embedding level, we
8013 are guaranteed that characters are delivered for
8014 display in strictly increasing order of their
8015 buffer positions. */
8016 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8017 && IT_CHARPOS (*it) > to_charpos)
8018 || (it->bidi_p
8019 && (prev_method == GET_FROM_IMAGE
8020 || prev_method == GET_FROM_STRETCH
8021 || prev_method == GET_FROM_STRING)
8022 /* Passed TO_CHARPOS from left to right. */
8023 && ((prev_pos < to_charpos
8024 && IT_CHARPOS (*it) > to_charpos)
8025 /* Passed TO_CHARPOS from right to left. */
8026 || (prev_pos > to_charpos
8027 && IT_CHARPOS (*it) < to_charpos)))))
8028 {
8029 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8030 {
8031 result = MOVE_POS_MATCH_OR_ZV;
8032 break;
8033 }
8034 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8035 /* If wrap_it is valid, the current position might be in a
8036 word that is wrapped. So, save the iterator in
8037 atpos_it and continue to see if wrapping happens. */
8038 SAVE_IT (atpos_it, *it, atpos_data);
8039 }
8040
8041 /* Stop when ZV reached.
8042 We used to stop here when TO_CHARPOS reached as well, but that is
8043 too soon if this glyph does not fit on this line. So we handle it
8044 explicitly below. */
8045 if (!get_next_display_element (it))
8046 {
8047 result = MOVE_POS_MATCH_OR_ZV;
8048 break;
8049 }
8050
8051 if (it->line_wrap == TRUNCATE)
8052 {
8053 if (BUFFER_POS_REACHED_P ())
8054 {
8055 result = MOVE_POS_MATCH_OR_ZV;
8056 break;
8057 }
8058 }
8059 else
8060 {
8061 if (it->line_wrap == WORD_WRAP)
8062 {
8063 if (IT_DISPLAYING_WHITESPACE (it))
8064 may_wrap = 1;
8065 else if (may_wrap)
8066 {
8067 /* We have reached a glyph that follows one or more
8068 whitespace characters. If the position is
8069 already found, we are done. */
8070 if (atpos_it.sp >= 0)
8071 {
8072 RESTORE_IT (it, &atpos_it, atpos_data);
8073 result = MOVE_POS_MATCH_OR_ZV;
8074 goto done;
8075 }
8076 if (atx_it.sp >= 0)
8077 {
8078 RESTORE_IT (it, &atx_it, atx_data);
8079 result = MOVE_X_REACHED;
8080 goto done;
8081 }
8082 /* Otherwise, we can wrap here. */
8083 SAVE_IT (wrap_it, *it, wrap_data);
8084 may_wrap = 0;
8085 }
8086 }
8087 }
8088
8089 /* Remember the line height for the current line, in case
8090 the next element doesn't fit on the line. */
8091 ascent = it->max_ascent;
8092 descent = it->max_descent;
8093
8094 /* The call to produce_glyphs will get the metrics of the
8095 display element IT is loaded with. Record the x-position
8096 before this display element, in case it doesn't fit on the
8097 line. */
8098 x = it->current_x;
8099
8100 PRODUCE_GLYPHS (it);
8101
8102 if (it->area != TEXT_AREA)
8103 {
8104 prev_method = it->method;
8105 if (it->method == GET_FROM_BUFFER)
8106 prev_pos = IT_CHARPOS (*it);
8107 set_iterator_to_next (it, 1);
8108 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8109 SET_TEXT_POS (this_line_min_pos,
8110 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8111 if (it->bidi_p
8112 && (op & MOVE_TO_POS)
8113 && IT_CHARPOS (*it) > to_charpos
8114 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8115 SAVE_IT (ppos_it, *it, ppos_data);
8116 continue;
8117 }
8118
8119 /* The number of glyphs we get back in IT->nglyphs will normally
8120 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8121 character on a terminal frame, or (iii) a line end. For the
8122 second case, IT->nglyphs - 1 padding glyphs will be present.
8123 (On X frames, there is only one glyph produced for a
8124 composite character.)
8125
8126 The behavior implemented below means, for continuation lines,
8127 that as many spaces of a TAB as fit on the current line are
8128 displayed there. For terminal frames, as many glyphs of a
8129 multi-glyph character are displayed in the current line, too.
8130 This is what the old redisplay code did, and we keep it that
8131 way. Under X, the whole shape of a complex character must
8132 fit on the line or it will be completely displayed in the
8133 next line.
8134
8135 Note that both for tabs and padding glyphs, all glyphs have
8136 the same width. */
8137 if (it->nglyphs)
8138 {
8139 /* More than one glyph or glyph doesn't fit on line. All
8140 glyphs have the same width. */
8141 int single_glyph_width = it->pixel_width / it->nglyphs;
8142 int new_x;
8143 int x_before_this_char = x;
8144 int hpos_before_this_char = it->hpos;
8145
8146 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8147 {
8148 new_x = x + single_glyph_width;
8149
8150 /* We want to leave anything reaching TO_X to the caller. */
8151 if ((op & MOVE_TO_X) && new_x > to_x)
8152 {
8153 if (BUFFER_POS_REACHED_P ())
8154 {
8155 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8156 goto buffer_pos_reached;
8157 if (atpos_it.sp < 0)
8158 {
8159 SAVE_IT (atpos_it, *it, atpos_data);
8160 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8161 }
8162 }
8163 else
8164 {
8165 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8166 {
8167 it->current_x = x;
8168 result = MOVE_X_REACHED;
8169 break;
8170 }
8171 if (atx_it.sp < 0)
8172 {
8173 SAVE_IT (atx_it, *it, atx_data);
8174 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8175 }
8176 }
8177 }
8178
8179 if (/* Lines are continued. */
8180 it->line_wrap != TRUNCATE
8181 && (/* And glyph doesn't fit on the line. */
8182 new_x > it->last_visible_x
8183 /* Or it fits exactly and we're on a window
8184 system frame. */
8185 || (new_x == it->last_visible_x
8186 && FRAME_WINDOW_P (it->f))))
8187 {
8188 if (/* IT->hpos == 0 means the very first glyph
8189 doesn't fit on the line, e.g. a wide image. */
8190 it->hpos == 0
8191 || (new_x == it->last_visible_x
8192 && FRAME_WINDOW_P (it->f)))
8193 {
8194 ++it->hpos;
8195 it->current_x = new_x;
8196
8197 /* The character's last glyph just barely fits
8198 in this row. */
8199 if (i == it->nglyphs - 1)
8200 {
8201 /* If this is the destination position,
8202 return a position *before* it in this row,
8203 now that we know it fits in this row. */
8204 if (BUFFER_POS_REACHED_P ())
8205 {
8206 if (it->line_wrap != WORD_WRAP
8207 || wrap_it.sp < 0)
8208 {
8209 it->hpos = hpos_before_this_char;
8210 it->current_x = x_before_this_char;
8211 result = MOVE_POS_MATCH_OR_ZV;
8212 break;
8213 }
8214 if (it->line_wrap == WORD_WRAP
8215 && atpos_it.sp < 0)
8216 {
8217 SAVE_IT (atpos_it, *it, atpos_data);
8218 atpos_it.current_x = x_before_this_char;
8219 atpos_it.hpos = hpos_before_this_char;
8220 }
8221 }
8222
8223 prev_method = it->method;
8224 if (it->method == GET_FROM_BUFFER)
8225 prev_pos = IT_CHARPOS (*it);
8226 set_iterator_to_next (it, 1);
8227 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8228 SET_TEXT_POS (this_line_min_pos,
8229 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8230 /* On graphical terminals, newlines may
8231 "overflow" into the fringe if
8232 overflow-newline-into-fringe is non-nil.
8233 On text-only terminals, newlines may
8234 overflow into the last glyph on the
8235 display line.*/
8236 if (!FRAME_WINDOW_P (it->f)
8237 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8238 {
8239 if (!get_next_display_element (it))
8240 {
8241 result = MOVE_POS_MATCH_OR_ZV;
8242 break;
8243 }
8244 if (BUFFER_POS_REACHED_P ())
8245 {
8246 if (ITERATOR_AT_END_OF_LINE_P (it))
8247 result = MOVE_POS_MATCH_OR_ZV;
8248 else
8249 result = MOVE_LINE_CONTINUED;
8250 break;
8251 }
8252 if (ITERATOR_AT_END_OF_LINE_P (it))
8253 {
8254 result = MOVE_NEWLINE_OR_CR;
8255 break;
8256 }
8257 }
8258 }
8259 }
8260 else
8261 IT_RESET_X_ASCENT_DESCENT (it);
8262
8263 if (wrap_it.sp >= 0)
8264 {
8265 RESTORE_IT (it, &wrap_it, wrap_data);
8266 atpos_it.sp = -1;
8267 atx_it.sp = -1;
8268 }
8269
8270 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8271 IT_CHARPOS (*it)));
8272 result = MOVE_LINE_CONTINUED;
8273 break;
8274 }
8275
8276 if (BUFFER_POS_REACHED_P ())
8277 {
8278 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8279 goto buffer_pos_reached;
8280 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8281 {
8282 SAVE_IT (atpos_it, *it, atpos_data);
8283 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8284 }
8285 }
8286
8287 if (new_x > it->first_visible_x)
8288 {
8289 /* Glyph is visible. Increment number of glyphs that
8290 would be displayed. */
8291 ++it->hpos;
8292 }
8293 }
8294
8295 if (result != MOVE_UNDEFINED)
8296 break;
8297 }
8298 else if (BUFFER_POS_REACHED_P ())
8299 {
8300 buffer_pos_reached:
8301 IT_RESET_X_ASCENT_DESCENT (it);
8302 result = MOVE_POS_MATCH_OR_ZV;
8303 break;
8304 }
8305 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8306 {
8307 /* Stop when TO_X specified and reached. This check is
8308 necessary here because of lines consisting of a line end,
8309 only. The line end will not produce any glyphs and we
8310 would never get MOVE_X_REACHED. */
8311 xassert (it->nglyphs == 0);
8312 result = MOVE_X_REACHED;
8313 break;
8314 }
8315
8316 /* Is this a line end? If yes, we're done. */
8317 if (ITERATOR_AT_END_OF_LINE_P (it))
8318 {
8319 /* If we are past TO_CHARPOS, but never saw any character
8320 positions smaller than TO_CHARPOS, return
8321 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8322 did. */
8323 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8324 {
8325 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8326 {
8327 if (IT_CHARPOS (ppos_it) < ZV)
8328 {
8329 RESTORE_IT (it, &ppos_it, ppos_data);
8330 result = MOVE_POS_MATCH_OR_ZV;
8331 }
8332 else
8333 goto buffer_pos_reached;
8334 }
8335 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8336 && IT_CHARPOS (*it) > to_charpos)
8337 goto buffer_pos_reached;
8338 else
8339 result = MOVE_NEWLINE_OR_CR;
8340 }
8341 else
8342 result = MOVE_NEWLINE_OR_CR;
8343 break;
8344 }
8345
8346 prev_method = it->method;
8347 if (it->method == GET_FROM_BUFFER)
8348 prev_pos = IT_CHARPOS (*it);
8349 /* The current display element has been consumed. Advance
8350 to the next. */
8351 set_iterator_to_next (it, 1);
8352 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8353 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8354 if (IT_CHARPOS (*it) < to_charpos)
8355 saw_smaller_pos = 1;
8356 if (it->bidi_p
8357 && (op & MOVE_TO_POS)
8358 && IT_CHARPOS (*it) >= to_charpos
8359 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8360 SAVE_IT (ppos_it, *it, ppos_data);
8361
8362 /* Stop if lines are truncated and IT's current x-position is
8363 past the right edge of the window now. */
8364 if (it->line_wrap == TRUNCATE
8365 && it->current_x >= it->last_visible_x)
8366 {
8367 if (!FRAME_WINDOW_P (it->f)
8368 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8369 {
8370 int at_eob_p = 0;
8371
8372 if ((at_eob_p = !get_next_display_element (it))
8373 || BUFFER_POS_REACHED_P ()
8374 /* If we are past TO_CHARPOS, but never saw any
8375 character positions smaller than TO_CHARPOS,
8376 return MOVE_POS_MATCH_OR_ZV, like the
8377 unidirectional display did. */
8378 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8379 && !saw_smaller_pos
8380 && IT_CHARPOS (*it) > to_charpos))
8381 {
8382 if (it->bidi_p
8383 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8384 RESTORE_IT (it, &ppos_it, ppos_data);
8385 result = MOVE_POS_MATCH_OR_ZV;
8386 break;
8387 }
8388 if (ITERATOR_AT_END_OF_LINE_P (it))
8389 {
8390 result = MOVE_NEWLINE_OR_CR;
8391 break;
8392 }
8393 }
8394 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8395 && !saw_smaller_pos
8396 && IT_CHARPOS (*it) > to_charpos)
8397 {
8398 if (IT_CHARPOS (ppos_it) < ZV)
8399 RESTORE_IT (it, &ppos_it, ppos_data);
8400 result = MOVE_POS_MATCH_OR_ZV;
8401 break;
8402 }
8403 result = MOVE_LINE_TRUNCATED;
8404 break;
8405 }
8406 #undef IT_RESET_X_ASCENT_DESCENT
8407 }
8408
8409 #undef BUFFER_POS_REACHED_P
8410
8411 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8412 restore the saved iterator. */
8413 if (atpos_it.sp >= 0)
8414 RESTORE_IT (it, &atpos_it, atpos_data);
8415 else if (atx_it.sp >= 0)
8416 RESTORE_IT (it, &atx_it, atx_data);
8417
8418 done:
8419
8420 if (atpos_data)
8421 bidi_unshelve_cache (atpos_data, 1);
8422 if (atx_data)
8423 bidi_unshelve_cache (atx_data, 1);
8424 if (wrap_data)
8425 bidi_unshelve_cache (wrap_data, 1);
8426 if (ppos_data)
8427 bidi_unshelve_cache (ppos_data, 1);
8428
8429 /* Restore the iterator settings altered at the beginning of this
8430 function. */
8431 it->glyph_row = saved_glyph_row;
8432 return result;
8433 }
8434
8435 /* For external use. */
8436 void
8437 move_it_in_display_line (struct it *it,
8438 EMACS_INT to_charpos, int to_x,
8439 enum move_operation_enum op)
8440 {
8441 if (it->line_wrap == WORD_WRAP
8442 && (op & MOVE_TO_X))
8443 {
8444 struct it save_it;
8445 void *save_data = NULL;
8446 int skip;
8447
8448 SAVE_IT (save_it, *it, save_data);
8449 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8450 /* When word-wrap is on, TO_X may lie past the end
8451 of a wrapped line. Then it->current is the
8452 character on the next line, so backtrack to the
8453 space before the wrap point. */
8454 if (skip == MOVE_LINE_CONTINUED)
8455 {
8456 int prev_x = max (it->current_x - 1, 0);
8457 RESTORE_IT (it, &save_it, save_data);
8458 move_it_in_display_line_to
8459 (it, -1, prev_x, MOVE_TO_X);
8460 }
8461 else
8462 bidi_unshelve_cache (save_data, 1);
8463 }
8464 else
8465 move_it_in_display_line_to (it, to_charpos, to_x, op);
8466 }
8467
8468
8469 /* Move IT forward until it satisfies one or more of the criteria in
8470 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8471
8472 OP is a bit-mask that specifies where to stop, and in particular,
8473 which of those four position arguments makes a difference. See the
8474 description of enum move_operation_enum.
8475
8476 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8477 screen line, this function will set IT to the next position that is
8478 displayed to the right of TO_CHARPOS on the screen. */
8479
8480 void
8481 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8482 {
8483 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8484 int line_height, line_start_x = 0, reached = 0;
8485 void *backup_data = NULL;
8486
8487 for (;;)
8488 {
8489 if (op & MOVE_TO_VPOS)
8490 {
8491 /* If no TO_CHARPOS and no TO_X specified, stop at the
8492 start of the line TO_VPOS. */
8493 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8494 {
8495 if (it->vpos == to_vpos)
8496 {
8497 reached = 1;
8498 break;
8499 }
8500 else
8501 skip = move_it_in_display_line_to (it, -1, -1, 0);
8502 }
8503 else
8504 {
8505 /* TO_VPOS >= 0 means stop at TO_X in the line at
8506 TO_VPOS, or at TO_POS, whichever comes first. */
8507 if (it->vpos == to_vpos)
8508 {
8509 reached = 2;
8510 break;
8511 }
8512
8513 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8514
8515 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8516 {
8517 reached = 3;
8518 break;
8519 }
8520 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8521 {
8522 /* We have reached TO_X but not in the line we want. */
8523 skip = move_it_in_display_line_to (it, to_charpos,
8524 -1, MOVE_TO_POS);
8525 if (skip == MOVE_POS_MATCH_OR_ZV)
8526 {
8527 reached = 4;
8528 break;
8529 }
8530 }
8531 }
8532 }
8533 else if (op & MOVE_TO_Y)
8534 {
8535 struct it it_backup;
8536
8537 if (it->line_wrap == WORD_WRAP)
8538 SAVE_IT (it_backup, *it, backup_data);
8539
8540 /* TO_Y specified means stop at TO_X in the line containing
8541 TO_Y---or at TO_CHARPOS if this is reached first. The
8542 problem is that we can't really tell whether the line
8543 contains TO_Y before we have completely scanned it, and
8544 this may skip past TO_X. What we do is to first scan to
8545 TO_X.
8546
8547 If TO_X is not specified, use a TO_X of zero. The reason
8548 is to make the outcome of this function more predictable.
8549 If we didn't use TO_X == 0, we would stop at the end of
8550 the line which is probably not what a caller would expect
8551 to happen. */
8552 skip = move_it_in_display_line_to
8553 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8554 (MOVE_TO_X | (op & MOVE_TO_POS)));
8555
8556 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8557 if (skip == MOVE_POS_MATCH_OR_ZV)
8558 reached = 5;
8559 else if (skip == MOVE_X_REACHED)
8560 {
8561 /* If TO_X was reached, we want to know whether TO_Y is
8562 in the line. We know this is the case if the already
8563 scanned glyphs make the line tall enough. Otherwise,
8564 we must check by scanning the rest of the line. */
8565 line_height = it->max_ascent + it->max_descent;
8566 if (to_y >= it->current_y
8567 && to_y < it->current_y + line_height)
8568 {
8569 reached = 6;
8570 break;
8571 }
8572 SAVE_IT (it_backup, *it, backup_data);
8573 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8574 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8575 op & MOVE_TO_POS);
8576 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8577 line_height = it->max_ascent + it->max_descent;
8578 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8579
8580 if (to_y >= it->current_y
8581 && to_y < it->current_y + line_height)
8582 {
8583 /* If TO_Y is in this line and TO_X was reached
8584 above, we scanned too far. We have to restore
8585 IT's settings to the ones before skipping. */
8586 RESTORE_IT (it, &it_backup, backup_data);
8587 reached = 6;
8588 }
8589 else
8590 {
8591 skip = skip2;
8592 if (skip == MOVE_POS_MATCH_OR_ZV)
8593 reached = 7;
8594 }
8595 }
8596 else
8597 {
8598 /* Check whether TO_Y is in this line. */
8599 line_height = it->max_ascent + it->max_descent;
8600 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8601
8602 if (to_y >= it->current_y
8603 && to_y < it->current_y + line_height)
8604 {
8605 /* When word-wrap is on, TO_X may lie past the end
8606 of a wrapped line. Then it->current is the
8607 character on the next line, so backtrack to the
8608 space before the wrap point. */
8609 if (skip == MOVE_LINE_CONTINUED
8610 && it->line_wrap == WORD_WRAP)
8611 {
8612 int prev_x = max (it->current_x - 1, 0);
8613 RESTORE_IT (it, &it_backup, backup_data);
8614 skip = move_it_in_display_line_to
8615 (it, -1, prev_x, MOVE_TO_X);
8616 }
8617 reached = 6;
8618 }
8619 }
8620
8621 if (reached)
8622 break;
8623 }
8624 else if (BUFFERP (it->object)
8625 && (it->method == GET_FROM_BUFFER
8626 || it->method == GET_FROM_STRETCH)
8627 && IT_CHARPOS (*it) >= to_charpos
8628 /* Under bidi iteration, a call to set_iterator_to_next
8629 can scan far beyond to_charpos if the initial
8630 portion of the next line needs to be reordered. In
8631 that case, give move_it_in_display_line_to another
8632 chance below. */
8633 && !(it->bidi_p
8634 && it->bidi_it.scan_dir == -1))
8635 skip = MOVE_POS_MATCH_OR_ZV;
8636 else
8637 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8638
8639 switch (skip)
8640 {
8641 case MOVE_POS_MATCH_OR_ZV:
8642 reached = 8;
8643 goto out;
8644
8645 case MOVE_NEWLINE_OR_CR:
8646 set_iterator_to_next (it, 1);
8647 it->continuation_lines_width = 0;
8648 break;
8649
8650 case MOVE_LINE_TRUNCATED:
8651 it->continuation_lines_width = 0;
8652 reseat_at_next_visible_line_start (it, 0);
8653 if ((op & MOVE_TO_POS) != 0
8654 && IT_CHARPOS (*it) > to_charpos)
8655 {
8656 reached = 9;
8657 goto out;
8658 }
8659 break;
8660
8661 case MOVE_LINE_CONTINUED:
8662 /* For continued lines ending in a tab, some of the glyphs
8663 associated with the tab are displayed on the current
8664 line. Since it->current_x does not include these glyphs,
8665 we use it->last_visible_x instead. */
8666 if (it->c == '\t')
8667 {
8668 it->continuation_lines_width += it->last_visible_x;
8669 /* When moving by vpos, ensure that the iterator really
8670 advances to the next line (bug#847, bug#969). Fixme:
8671 do we need to do this in other circumstances? */
8672 if (it->current_x != it->last_visible_x
8673 && (op & MOVE_TO_VPOS)
8674 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8675 {
8676 line_start_x = it->current_x + it->pixel_width
8677 - it->last_visible_x;
8678 set_iterator_to_next (it, 0);
8679 }
8680 }
8681 else
8682 it->continuation_lines_width += it->current_x;
8683 break;
8684
8685 default:
8686 abort ();
8687 }
8688
8689 /* Reset/increment for the next run. */
8690 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8691 it->current_x = line_start_x;
8692 line_start_x = 0;
8693 it->hpos = 0;
8694 it->current_y += it->max_ascent + it->max_descent;
8695 ++it->vpos;
8696 last_height = it->max_ascent + it->max_descent;
8697 last_max_ascent = it->max_ascent;
8698 it->max_ascent = it->max_descent = 0;
8699 }
8700
8701 out:
8702
8703 /* On text terminals, we may stop at the end of a line in the middle
8704 of a multi-character glyph. If the glyph itself is continued,
8705 i.e. it is actually displayed on the next line, don't treat this
8706 stopping point as valid; move to the next line instead (unless
8707 that brings us offscreen). */
8708 if (!FRAME_WINDOW_P (it->f)
8709 && op & MOVE_TO_POS
8710 && IT_CHARPOS (*it) == to_charpos
8711 && it->what == IT_CHARACTER
8712 && it->nglyphs > 1
8713 && it->line_wrap == WINDOW_WRAP
8714 && it->current_x == it->last_visible_x - 1
8715 && it->c != '\n'
8716 && it->c != '\t'
8717 && it->vpos < XFASTINT (it->w->window_end_vpos))
8718 {
8719 it->continuation_lines_width += it->current_x;
8720 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8721 it->current_y += it->max_ascent + it->max_descent;
8722 ++it->vpos;
8723 last_height = it->max_ascent + it->max_descent;
8724 last_max_ascent = it->max_ascent;
8725 }
8726
8727 if (backup_data)
8728 bidi_unshelve_cache (backup_data, 1);
8729
8730 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8731 }
8732
8733
8734 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8735
8736 If DY > 0, move IT backward at least that many pixels. DY = 0
8737 means move IT backward to the preceding line start or BEGV. This
8738 function may move over more than DY pixels if IT->current_y - DY
8739 ends up in the middle of a line; in this case IT->current_y will be
8740 set to the top of the line moved to. */
8741
8742 void
8743 move_it_vertically_backward (struct it *it, int dy)
8744 {
8745 int nlines, h;
8746 struct it it2, it3;
8747 void *it2data = NULL, *it3data = NULL;
8748 EMACS_INT start_pos;
8749
8750 move_further_back:
8751 xassert (dy >= 0);
8752
8753 start_pos = IT_CHARPOS (*it);
8754
8755 /* Estimate how many newlines we must move back. */
8756 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8757
8758 /* Set the iterator's position that many lines back. */
8759 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8760 back_to_previous_visible_line_start (it);
8761
8762 /* Reseat the iterator here. When moving backward, we don't want
8763 reseat to skip forward over invisible text, set up the iterator
8764 to deliver from overlay strings at the new position etc. So,
8765 use reseat_1 here. */
8766 reseat_1 (it, it->current.pos, 1);
8767
8768 /* We are now surely at a line start. */
8769 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8770 reordering is in effect. */
8771 it->continuation_lines_width = 0;
8772
8773 /* Move forward and see what y-distance we moved. First move to the
8774 start of the next line so that we get its height. We need this
8775 height to be able to tell whether we reached the specified
8776 y-distance. */
8777 SAVE_IT (it2, *it, it2data);
8778 it2.max_ascent = it2.max_descent = 0;
8779 do
8780 {
8781 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8782 MOVE_TO_POS | MOVE_TO_VPOS);
8783 }
8784 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8785 /* If we are in a display string which starts at START_POS,
8786 and that display string includes a newline, and we are
8787 right after that newline (i.e. at the beginning of a
8788 display line), exit the loop, because otherwise we will
8789 infloop, since move_it_to will see that it is already at
8790 START_POS and will not move. */
8791 || (it2.method == GET_FROM_STRING
8792 && IT_CHARPOS (it2) == start_pos
8793 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8794 xassert (IT_CHARPOS (*it) >= BEGV);
8795 SAVE_IT (it3, it2, it3data);
8796
8797 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8798 xassert (IT_CHARPOS (*it) >= BEGV);
8799 /* H is the actual vertical distance from the position in *IT
8800 and the starting position. */
8801 h = it2.current_y - it->current_y;
8802 /* NLINES is the distance in number of lines. */
8803 nlines = it2.vpos - it->vpos;
8804
8805 /* Correct IT's y and vpos position
8806 so that they are relative to the starting point. */
8807 it->vpos -= nlines;
8808 it->current_y -= h;
8809
8810 if (dy == 0)
8811 {
8812 /* DY == 0 means move to the start of the screen line. The
8813 value of nlines is > 0 if continuation lines were involved,
8814 or if the original IT position was at start of a line. */
8815 RESTORE_IT (it, it, it2data);
8816 if (nlines > 0)
8817 move_it_by_lines (it, nlines);
8818 /* The above code moves us to some position NLINES down,
8819 usually to its first glyph (leftmost in an L2R line), but
8820 that's not necessarily the start of the line, under bidi
8821 reordering. We want to get to the character position
8822 that is immediately after the newline of the previous
8823 line. */
8824 if (it->bidi_p
8825 && !it->continuation_lines_width
8826 && !STRINGP (it->string)
8827 && IT_CHARPOS (*it) > BEGV
8828 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8829 {
8830 EMACS_INT nl_pos =
8831 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8832
8833 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8834 }
8835 bidi_unshelve_cache (it3data, 1);
8836 }
8837 else
8838 {
8839 /* The y-position we try to reach, relative to *IT.
8840 Note that H has been subtracted in front of the if-statement. */
8841 int target_y = it->current_y + h - dy;
8842 int y0 = it3.current_y;
8843 int y1;
8844 int line_height;
8845
8846 RESTORE_IT (&it3, &it3, it3data);
8847 y1 = line_bottom_y (&it3);
8848 line_height = y1 - y0;
8849 RESTORE_IT (it, it, it2data);
8850 /* If we did not reach target_y, try to move further backward if
8851 we can. If we moved too far backward, try to move forward. */
8852 if (target_y < it->current_y
8853 /* This is heuristic. In a window that's 3 lines high, with
8854 a line height of 13 pixels each, recentering with point
8855 on the bottom line will try to move -39/2 = 19 pixels
8856 backward. Try to avoid moving into the first line. */
8857 && (it->current_y - target_y
8858 > min (window_box_height (it->w), line_height * 2 / 3))
8859 && IT_CHARPOS (*it) > BEGV)
8860 {
8861 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8862 target_y - it->current_y));
8863 dy = it->current_y - target_y;
8864 goto move_further_back;
8865 }
8866 else if (target_y >= it->current_y + line_height
8867 && IT_CHARPOS (*it) < ZV)
8868 {
8869 /* Should move forward by at least one line, maybe more.
8870
8871 Note: Calling move_it_by_lines can be expensive on
8872 terminal frames, where compute_motion is used (via
8873 vmotion) to do the job, when there are very long lines
8874 and truncate-lines is nil. That's the reason for
8875 treating terminal frames specially here. */
8876
8877 if (!FRAME_WINDOW_P (it->f))
8878 move_it_vertically (it, target_y - (it->current_y + line_height));
8879 else
8880 {
8881 do
8882 {
8883 move_it_by_lines (it, 1);
8884 }
8885 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8886 }
8887 }
8888 }
8889 }
8890
8891
8892 /* Move IT by a specified amount of pixel lines DY. DY negative means
8893 move backwards. DY = 0 means move to start of screen line. At the
8894 end, IT will be on the start of a screen line. */
8895
8896 void
8897 move_it_vertically (struct it *it, int dy)
8898 {
8899 if (dy <= 0)
8900 move_it_vertically_backward (it, -dy);
8901 else
8902 {
8903 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8904 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8905 MOVE_TO_POS | MOVE_TO_Y);
8906 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8907
8908 /* If buffer ends in ZV without a newline, move to the start of
8909 the line to satisfy the post-condition. */
8910 if (IT_CHARPOS (*it) == ZV
8911 && ZV > BEGV
8912 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8913 move_it_by_lines (it, 0);
8914 }
8915 }
8916
8917
8918 /* Move iterator IT past the end of the text line it is in. */
8919
8920 void
8921 move_it_past_eol (struct it *it)
8922 {
8923 enum move_it_result rc;
8924
8925 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8926 if (rc == MOVE_NEWLINE_OR_CR)
8927 set_iterator_to_next (it, 0);
8928 }
8929
8930
8931 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8932 negative means move up. DVPOS == 0 means move to the start of the
8933 screen line.
8934
8935 Optimization idea: If we would know that IT->f doesn't use
8936 a face with proportional font, we could be faster for
8937 truncate-lines nil. */
8938
8939 void
8940 move_it_by_lines (struct it *it, int dvpos)
8941 {
8942
8943 /* The commented-out optimization uses vmotion on terminals. This
8944 gives bad results, because elements like it->what, on which
8945 callers such as pos_visible_p rely, aren't updated. */
8946 /* struct position pos;
8947 if (!FRAME_WINDOW_P (it->f))
8948 {
8949 struct text_pos textpos;
8950
8951 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8952 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8953 reseat (it, textpos, 1);
8954 it->vpos += pos.vpos;
8955 it->current_y += pos.vpos;
8956 }
8957 else */
8958
8959 if (dvpos == 0)
8960 {
8961 /* DVPOS == 0 means move to the start of the screen line. */
8962 move_it_vertically_backward (it, 0);
8963 xassert (it->current_x == 0 && it->hpos == 0);
8964 /* Let next call to line_bottom_y calculate real line height */
8965 last_height = 0;
8966 }
8967 else if (dvpos > 0)
8968 {
8969 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8970 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8971 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8972 }
8973 else
8974 {
8975 struct it it2;
8976 void *it2data = NULL;
8977 EMACS_INT start_charpos, i;
8978
8979 /* Start at the beginning of the screen line containing IT's
8980 position. This may actually move vertically backwards,
8981 in case of overlays, so adjust dvpos accordingly. */
8982 dvpos += it->vpos;
8983 move_it_vertically_backward (it, 0);
8984 dvpos -= it->vpos;
8985
8986 /* Go back -DVPOS visible lines and reseat the iterator there. */
8987 start_charpos = IT_CHARPOS (*it);
8988 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8989 back_to_previous_visible_line_start (it);
8990 reseat (it, it->current.pos, 1);
8991
8992 /* Move further back if we end up in a string or an image. */
8993 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8994 {
8995 /* First try to move to start of display line. */
8996 dvpos += it->vpos;
8997 move_it_vertically_backward (it, 0);
8998 dvpos -= it->vpos;
8999 if (IT_POS_VALID_AFTER_MOVE_P (it))
9000 break;
9001 /* If start of line is still in string or image,
9002 move further back. */
9003 back_to_previous_visible_line_start (it);
9004 reseat (it, it->current.pos, 1);
9005 dvpos--;
9006 }
9007
9008 it->current_x = it->hpos = 0;
9009
9010 /* Above call may have moved too far if continuation lines
9011 are involved. Scan forward and see if it did. */
9012 SAVE_IT (it2, *it, it2data);
9013 it2.vpos = it2.current_y = 0;
9014 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9015 it->vpos -= it2.vpos;
9016 it->current_y -= it2.current_y;
9017 it->current_x = it->hpos = 0;
9018
9019 /* If we moved too far back, move IT some lines forward. */
9020 if (it2.vpos > -dvpos)
9021 {
9022 int delta = it2.vpos + dvpos;
9023
9024 RESTORE_IT (&it2, &it2, it2data);
9025 SAVE_IT (it2, *it, it2data);
9026 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9027 /* Move back again if we got too far ahead. */
9028 if (IT_CHARPOS (*it) >= start_charpos)
9029 RESTORE_IT (it, &it2, it2data);
9030 else
9031 bidi_unshelve_cache (it2data, 1);
9032 }
9033 else
9034 RESTORE_IT (it, it, it2data);
9035 }
9036 }
9037
9038 /* Return 1 if IT points into the middle of a display vector. */
9039
9040 int
9041 in_display_vector_p (struct it *it)
9042 {
9043 return (it->method == GET_FROM_DISPLAY_VECTOR
9044 && it->current.dpvec_index > 0
9045 && it->dpvec + it->current.dpvec_index != it->dpend);
9046 }
9047
9048 \f
9049 /***********************************************************************
9050 Messages
9051 ***********************************************************************/
9052
9053
9054 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9055 to *Messages*. */
9056
9057 void
9058 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9059 {
9060 Lisp_Object args[3];
9061 Lisp_Object msg, fmt;
9062 char *buffer;
9063 EMACS_INT len;
9064 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9065 USE_SAFE_ALLOCA;
9066
9067 /* Do nothing if called asynchronously. Inserting text into
9068 a buffer may call after-change-functions and alike and
9069 that would means running Lisp asynchronously. */
9070 if (handling_signal)
9071 return;
9072
9073 fmt = msg = Qnil;
9074 GCPRO4 (fmt, msg, arg1, arg2);
9075
9076 args[0] = fmt = build_string (format);
9077 args[1] = arg1;
9078 args[2] = arg2;
9079 msg = Fformat (3, args);
9080
9081 len = SBYTES (msg) + 1;
9082 SAFE_ALLOCA (buffer, char *, len);
9083 memcpy (buffer, SDATA (msg), len);
9084
9085 message_dolog (buffer, len - 1, 1, 0);
9086 SAFE_FREE ();
9087
9088 UNGCPRO;
9089 }
9090
9091
9092 /* Output a newline in the *Messages* buffer if "needs" one. */
9093
9094 void
9095 message_log_maybe_newline (void)
9096 {
9097 if (message_log_need_newline)
9098 message_dolog ("", 0, 1, 0);
9099 }
9100
9101
9102 /* Add a string M of length NBYTES to the message log, optionally
9103 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9104 nonzero, means interpret the contents of M as multibyte. This
9105 function calls low-level routines in order to bypass text property
9106 hooks, etc. which might not be safe to run.
9107
9108 This may GC (insert may run before/after change hooks),
9109 so the buffer M must NOT point to a Lisp string. */
9110
9111 void
9112 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
9113 {
9114 const unsigned char *msg = (const unsigned char *) m;
9115
9116 if (!NILP (Vmemory_full))
9117 return;
9118
9119 if (!NILP (Vmessage_log_max))
9120 {
9121 struct buffer *oldbuf;
9122 Lisp_Object oldpoint, oldbegv, oldzv;
9123 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9124 EMACS_INT point_at_end = 0;
9125 EMACS_INT zv_at_end = 0;
9126 Lisp_Object old_deactivate_mark, tem;
9127 struct gcpro gcpro1;
9128
9129 old_deactivate_mark = Vdeactivate_mark;
9130 oldbuf = current_buffer;
9131 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9132 BVAR (current_buffer, undo_list) = Qt;
9133
9134 oldpoint = message_dolog_marker1;
9135 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9136 oldbegv = message_dolog_marker2;
9137 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9138 oldzv = message_dolog_marker3;
9139 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9140 GCPRO1 (old_deactivate_mark);
9141
9142 if (PT == Z)
9143 point_at_end = 1;
9144 if (ZV == Z)
9145 zv_at_end = 1;
9146
9147 BEGV = BEG;
9148 BEGV_BYTE = BEG_BYTE;
9149 ZV = Z;
9150 ZV_BYTE = Z_BYTE;
9151 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9152
9153 /* Insert the string--maybe converting multibyte to single byte
9154 or vice versa, so that all the text fits the buffer. */
9155 if (multibyte
9156 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9157 {
9158 EMACS_INT i;
9159 int c, char_bytes;
9160 char work[1];
9161
9162 /* Convert a multibyte string to single-byte
9163 for the *Message* buffer. */
9164 for (i = 0; i < nbytes; i += char_bytes)
9165 {
9166 c = string_char_and_length (msg + i, &char_bytes);
9167 work[0] = (ASCII_CHAR_P (c)
9168 ? c
9169 : multibyte_char_to_unibyte (c));
9170 insert_1_both (work, 1, 1, 1, 0, 0);
9171 }
9172 }
9173 else if (! multibyte
9174 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9175 {
9176 EMACS_INT i;
9177 int c, char_bytes;
9178 unsigned char str[MAX_MULTIBYTE_LENGTH];
9179 /* Convert a single-byte string to multibyte
9180 for the *Message* buffer. */
9181 for (i = 0; i < nbytes; i++)
9182 {
9183 c = msg[i];
9184 MAKE_CHAR_MULTIBYTE (c);
9185 char_bytes = CHAR_STRING (c, str);
9186 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9187 }
9188 }
9189 else if (nbytes)
9190 insert_1 (m, nbytes, 1, 0, 0);
9191
9192 if (nlflag)
9193 {
9194 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9195 printmax_t dups;
9196 insert_1 ("\n", 1, 1, 0, 0);
9197
9198 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9199 this_bol = PT;
9200 this_bol_byte = PT_BYTE;
9201
9202 /* See if this line duplicates the previous one.
9203 If so, combine duplicates. */
9204 if (this_bol > BEG)
9205 {
9206 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9207 prev_bol = PT;
9208 prev_bol_byte = PT_BYTE;
9209
9210 dups = message_log_check_duplicate (prev_bol_byte,
9211 this_bol_byte);
9212 if (dups)
9213 {
9214 del_range_both (prev_bol, prev_bol_byte,
9215 this_bol, this_bol_byte, 0);
9216 if (dups > 1)
9217 {
9218 char dupstr[sizeof " [ times]"
9219 + INT_STRLEN_BOUND (printmax_t)];
9220 int duplen;
9221
9222 /* If you change this format, don't forget to also
9223 change message_log_check_duplicate. */
9224 sprintf (dupstr, " [%"pMd" times]", dups);
9225 duplen = strlen (dupstr);
9226 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9227 insert_1 (dupstr, duplen, 1, 0, 1);
9228 }
9229 }
9230 }
9231
9232 /* If we have more than the desired maximum number of lines
9233 in the *Messages* buffer now, delete the oldest ones.
9234 This is safe because we don't have undo in this buffer. */
9235
9236 if (NATNUMP (Vmessage_log_max))
9237 {
9238 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9239 -XFASTINT (Vmessage_log_max) - 1, 0);
9240 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9241 }
9242 }
9243 BEGV = XMARKER (oldbegv)->charpos;
9244 BEGV_BYTE = marker_byte_position (oldbegv);
9245
9246 if (zv_at_end)
9247 {
9248 ZV = Z;
9249 ZV_BYTE = Z_BYTE;
9250 }
9251 else
9252 {
9253 ZV = XMARKER (oldzv)->charpos;
9254 ZV_BYTE = marker_byte_position (oldzv);
9255 }
9256
9257 if (point_at_end)
9258 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9259 else
9260 /* We can't do Fgoto_char (oldpoint) because it will run some
9261 Lisp code. */
9262 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9263 XMARKER (oldpoint)->bytepos);
9264
9265 UNGCPRO;
9266 unchain_marker (XMARKER (oldpoint));
9267 unchain_marker (XMARKER (oldbegv));
9268 unchain_marker (XMARKER (oldzv));
9269
9270 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9271 set_buffer_internal (oldbuf);
9272 if (NILP (tem))
9273 windows_or_buffers_changed = old_windows_or_buffers_changed;
9274 message_log_need_newline = !nlflag;
9275 Vdeactivate_mark = old_deactivate_mark;
9276 }
9277 }
9278
9279
9280 /* We are at the end of the buffer after just having inserted a newline.
9281 (Note: We depend on the fact we won't be crossing the gap.)
9282 Check to see if the most recent message looks a lot like the previous one.
9283 Return 0 if different, 1 if the new one should just replace it, or a
9284 value N > 1 if we should also append " [N times]". */
9285
9286 static intmax_t
9287 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9288 {
9289 EMACS_INT i;
9290 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9291 int seen_dots = 0;
9292 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9293 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9294
9295 for (i = 0; i < len; i++)
9296 {
9297 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9298 seen_dots = 1;
9299 if (p1[i] != p2[i])
9300 return seen_dots;
9301 }
9302 p1 += len;
9303 if (*p1 == '\n')
9304 return 2;
9305 if (*p1++ == ' ' && *p1++ == '[')
9306 {
9307 char *pend;
9308 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9309 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9310 return n+1;
9311 }
9312 return 0;
9313 }
9314 \f
9315
9316 /* Display an echo area message M with a specified length of NBYTES
9317 bytes. The string may include null characters. If M is 0, clear
9318 out any existing message, and let the mini-buffer text show
9319 through.
9320
9321 This may GC, so the buffer M must NOT point to a Lisp string. */
9322
9323 void
9324 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9325 {
9326 /* First flush out any partial line written with print. */
9327 message_log_maybe_newline ();
9328 if (m)
9329 message_dolog (m, nbytes, 1, multibyte);
9330 message2_nolog (m, nbytes, multibyte);
9331 }
9332
9333
9334 /* The non-logging counterpart of message2. */
9335
9336 void
9337 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9338 {
9339 struct frame *sf = SELECTED_FRAME ();
9340 message_enable_multibyte = multibyte;
9341
9342 if (FRAME_INITIAL_P (sf))
9343 {
9344 if (noninteractive_need_newline)
9345 putc ('\n', stderr);
9346 noninteractive_need_newline = 0;
9347 if (m)
9348 fwrite (m, nbytes, 1, stderr);
9349 if (cursor_in_echo_area == 0)
9350 fprintf (stderr, "\n");
9351 fflush (stderr);
9352 }
9353 /* A null message buffer means that the frame hasn't really been
9354 initialized yet. Error messages get reported properly by
9355 cmd_error, so this must be just an informative message; toss it. */
9356 else if (INTERACTIVE
9357 && sf->glyphs_initialized_p
9358 && FRAME_MESSAGE_BUF (sf))
9359 {
9360 Lisp_Object mini_window;
9361 struct frame *f;
9362
9363 /* Get the frame containing the mini-buffer
9364 that the selected frame is using. */
9365 mini_window = FRAME_MINIBUF_WINDOW (sf);
9366 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9367
9368 FRAME_SAMPLE_VISIBILITY (f);
9369 if (FRAME_VISIBLE_P (sf)
9370 && ! FRAME_VISIBLE_P (f))
9371 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9372
9373 if (m)
9374 {
9375 set_message (m, Qnil, nbytes, multibyte);
9376 if (minibuffer_auto_raise)
9377 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9378 }
9379 else
9380 clear_message (1, 1);
9381
9382 do_pending_window_change (0);
9383 echo_area_display (1);
9384 do_pending_window_change (0);
9385 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9386 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9387 }
9388 }
9389
9390
9391 /* Display an echo area message M with a specified length of NBYTES
9392 bytes. The string may include null characters. If M is not a
9393 string, clear out any existing message, and let the mini-buffer
9394 text show through.
9395
9396 This function cancels echoing. */
9397
9398 void
9399 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9400 {
9401 struct gcpro gcpro1;
9402
9403 GCPRO1 (m);
9404 clear_message (1,1);
9405 cancel_echoing ();
9406
9407 /* First flush out any partial line written with print. */
9408 message_log_maybe_newline ();
9409 if (STRINGP (m))
9410 {
9411 char *buffer;
9412 USE_SAFE_ALLOCA;
9413
9414 SAFE_ALLOCA (buffer, char *, nbytes);
9415 memcpy (buffer, SDATA (m), nbytes);
9416 message_dolog (buffer, nbytes, 1, multibyte);
9417 SAFE_FREE ();
9418 }
9419 message3_nolog (m, nbytes, multibyte);
9420
9421 UNGCPRO;
9422 }
9423
9424
9425 /* The non-logging version of message3.
9426 This does not cancel echoing, because it is used for echoing.
9427 Perhaps we need to make a separate function for echoing
9428 and make this cancel echoing. */
9429
9430 void
9431 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9432 {
9433 struct frame *sf = SELECTED_FRAME ();
9434 message_enable_multibyte = multibyte;
9435
9436 if (FRAME_INITIAL_P (sf))
9437 {
9438 if (noninteractive_need_newline)
9439 putc ('\n', stderr);
9440 noninteractive_need_newline = 0;
9441 if (STRINGP (m))
9442 fwrite (SDATA (m), nbytes, 1, stderr);
9443 if (cursor_in_echo_area == 0)
9444 fprintf (stderr, "\n");
9445 fflush (stderr);
9446 }
9447 /* A null message buffer means that the frame hasn't really been
9448 initialized yet. Error messages get reported properly by
9449 cmd_error, so this must be just an informative message; toss it. */
9450 else if (INTERACTIVE
9451 && sf->glyphs_initialized_p
9452 && FRAME_MESSAGE_BUF (sf))
9453 {
9454 Lisp_Object mini_window;
9455 Lisp_Object frame;
9456 struct frame *f;
9457
9458 /* Get the frame containing the mini-buffer
9459 that the selected frame is using. */
9460 mini_window = FRAME_MINIBUF_WINDOW (sf);
9461 frame = XWINDOW (mini_window)->frame;
9462 f = XFRAME (frame);
9463
9464 FRAME_SAMPLE_VISIBILITY (f);
9465 if (FRAME_VISIBLE_P (sf)
9466 && !FRAME_VISIBLE_P (f))
9467 Fmake_frame_visible (frame);
9468
9469 if (STRINGP (m) && SCHARS (m) > 0)
9470 {
9471 set_message (NULL, m, nbytes, multibyte);
9472 if (minibuffer_auto_raise)
9473 Fraise_frame (frame);
9474 /* Assume we are not echoing.
9475 (If we are, echo_now will override this.) */
9476 echo_message_buffer = Qnil;
9477 }
9478 else
9479 clear_message (1, 1);
9480
9481 do_pending_window_change (0);
9482 echo_area_display (1);
9483 do_pending_window_change (0);
9484 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9485 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9486 }
9487 }
9488
9489
9490 /* Display a null-terminated echo area message M. If M is 0, clear
9491 out any existing message, and let the mini-buffer text show through.
9492
9493 The buffer M must continue to exist until after the echo area gets
9494 cleared or some other message gets displayed there. Do not pass
9495 text that is stored in a Lisp string. Do not pass text in a buffer
9496 that was alloca'd. */
9497
9498 void
9499 message1 (const char *m)
9500 {
9501 message2 (m, (m ? strlen (m) : 0), 0);
9502 }
9503
9504
9505 /* The non-logging counterpart of message1. */
9506
9507 void
9508 message1_nolog (const char *m)
9509 {
9510 message2_nolog (m, (m ? strlen (m) : 0), 0);
9511 }
9512
9513 /* Display a message M which contains a single %s
9514 which gets replaced with STRING. */
9515
9516 void
9517 message_with_string (const char *m, Lisp_Object string, int log)
9518 {
9519 CHECK_STRING (string);
9520
9521 if (noninteractive)
9522 {
9523 if (m)
9524 {
9525 if (noninteractive_need_newline)
9526 putc ('\n', stderr);
9527 noninteractive_need_newline = 0;
9528 fprintf (stderr, m, SDATA (string));
9529 if (!cursor_in_echo_area)
9530 fprintf (stderr, "\n");
9531 fflush (stderr);
9532 }
9533 }
9534 else if (INTERACTIVE)
9535 {
9536 /* The frame whose minibuffer we're going to display the message on.
9537 It may be larger than the selected frame, so we need
9538 to use its buffer, not the selected frame's buffer. */
9539 Lisp_Object mini_window;
9540 struct frame *f, *sf = SELECTED_FRAME ();
9541
9542 /* Get the frame containing the minibuffer
9543 that the selected frame is using. */
9544 mini_window = FRAME_MINIBUF_WINDOW (sf);
9545 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9546
9547 /* A null message buffer means that the frame hasn't really been
9548 initialized yet. Error messages get reported properly by
9549 cmd_error, so this must be just an informative message; toss it. */
9550 if (FRAME_MESSAGE_BUF (f))
9551 {
9552 Lisp_Object args[2], msg;
9553 struct gcpro gcpro1, gcpro2;
9554
9555 args[0] = build_string (m);
9556 args[1] = msg = string;
9557 GCPRO2 (args[0], msg);
9558 gcpro1.nvars = 2;
9559
9560 msg = Fformat (2, args);
9561
9562 if (log)
9563 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9564 else
9565 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9566
9567 UNGCPRO;
9568
9569 /* Print should start at the beginning of the message
9570 buffer next time. */
9571 message_buf_print = 0;
9572 }
9573 }
9574 }
9575
9576
9577 /* Dump an informative message to the minibuf. If M is 0, clear out
9578 any existing message, and let the mini-buffer text show through. */
9579
9580 static void
9581 vmessage (const char *m, va_list ap)
9582 {
9583 if (noninteractive)
9584 {
9585 if (m)
9586 {
9587 if (noninteractive_need_newline)
9588 putc ('\n', stderr);
9589 noninteractive_need_newline = 0;
9590 vfprintf (stderr, m, ap);
9591 if (cursor_in_echo_area == 0)
9592 fprintf (stderr, "\n");
9593 fflush (stderr);
9594 }
9595 }
9596 else if (INTERACTIVE)
9597 {
9598 /* The frame whose mini-buffer we're going to display the message
9599 on. It may be larger than the selected frame, so we need to
9600 use its buffer, not the selected frame's buffer. */
9601 Lisp_Object mini_window;
9602 struct frame *f, *sf = SELECTED_FRAME ();
9603
9604 /* Get the frame containing the mini-buffer
9605 that the selected frame is using. */
9606 mini_window = FRAME_MINIBUF_WINDOW (sf);
9607 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9608
9609 /* A null message buffer means that the frame hasn't really been
9610 initialized yet. Error messages get reported properly by
9611 cmd_error, so this must be just an informative message; toss
9612 it. */
9613 if (FRAME_MESSAGE_BUF (f))
9614 {
9615 if (m)
9616 {
9617 ptrdiff_t len;
9618
9619 len = doprnt (FRAME_MESSAGE_BUF (f),
9620 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9621
9622 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9623 }
9624 else
9625 message1 (0);
9626
9627 /* Print should start at the beginning of the message
9628 buffer next time. */
9629 message_buf_print = 0;
9630 }
9631 }
9632 }
9633
9634 void
9635 message (const char *m, ...)
9636 {
9637 va_list ap;
9638 va_start (ap, m);
9639 vmessage (m, ap);
9640 va_end (ap);
9641 }
9642
9643
9644 #if 0
9645 /* The non-logging version of message. */
9646
9647 void
9648 message_nolog (const char *m, ...)
9649 {
9650 Lisp_Object old_log_max;
9651 va_list ap;
9652 va_start (ap, m);
9653 old_log_max = Vmessage_log_max;
9654 Vmessage_log_max = Qnil;
9655 vmessage (m, ap);
9656 Vmessage_log_max = old_log_max;
9657 va_end (ap);
9658 }
9659 #endif
9660
9661
9662 /* Display the current message in the current mini-buffer. This is
9663 only called from error handlers in process.c, and is not time
9664 critical. */
9665
9666 void
9667 update_echo_area (void)
9668 {
9669 if (!NILP (echo_area_buffer[0]))
9670 {
9671 Lisp_Object string;
9672 string = Fcurrent_message ();
9673 message3 (string, SBYTES (string),
9674 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9675 }
9676 }
9677
9678
9679 /* Make sure echo area buffers in `echo_buffers' are live.
9680 If they aren't, make new ones. */
9681
9682 static void
9683 ensure_echo_area_buffers (void)
9684 {
9685 int i;
9686
9687 for (i = 0; i < 2; ++i)
9688 if (!BUFFERP (echo_buffer[i])
9689 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9690 {
9691 char name[30];
9692 Lisp_Object old_buffer;
9693 int j;
9694
9695 old_buffer = echo_buffer[i];
9696 sprintf (name, " *Echo Area %d*", i);
9697 echo_buffer[i] = Fget_buffer_create (build_string (name));
9698 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9699 /* to force word wrap in echo area -
9700 it was decided to postpone this*/
9701 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9702
9703 for (j = 0; j < 2; ++j)
9704 if (EQ (old_buffer, echo_area_buffer[j]))
9705 echo_area_buffer[j] = echo_buffer[i];
9706 }
9707 }
9708
9709
9710 /* Call FN with args A1..A4 with either the current or last displayed
9711 echo_area_buffer as current buffer.
9712
9713 WHICH zero means use the current message buffer
9714 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9715 from echo_buffer[] and clear it.
9716
9717 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9718 suitable buffer from echo_buffer[] and clear it.
9719
9720 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9721 that the current message becomes the last displayed one, make
9722 choose a suitable buffer for echo_area_buffer[0], and clear it.
9723
9724 Value is what FN returns. */
9725
9726 static int
9727 with_echo_area_buffer (struct window *w, int which,
9728 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9729 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9730 {
9731 Lisp_Object buffer;
9732 int this_one, the_other, clear_buffer_p, rc;
9733 int count = SPECPDL_INDEX ();
9734
9735 /* If buffers aren't live, make new ones. */
9736 ensure_echo_area_buffers ();
9737
9738 clear_buffer_p = 0;
9739
9740 if (which == 0)
9741 this_one = 0, the_other = 1;
9742 else if (which > 0)
9743 this_one = 1, the_other = 0;
9744 else
9745 {
9746 this_one = 0, the_other = 1;
9747 clear_buffer_p = 1;
9748
9749 /* We need a fresh one in case the current echo buffer equals
9750 the one containing the last displayed echo area message. */
9751 if (!NILP (echo_area_buffer[this_one])
9752 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9753 echo_area_buffer[this_one] = Qnil;
9754 }
9755
9756 /* Choose a suitable buffer from echo_buffer[] is we don't
9757 have one. */
9758 if (NILP (echo_area_buffer[this_one]))
9759 {
9760 echo_area_buffer[this_one]
9761 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9762 ? echo_buffer[the_other]
9763 : echo_buffer[this_one]);
9764 clear_buffer_p = 1;
9765 }
9766
9767 buffer = echo_area_buffer[this_one];
9768
9769 /* Don't get confused by reusing the buffer used for echoing
9770 for a different purpose. */
9771 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9772 cancel_echoing ();
9773
9774 record_unwind_protect (unwind_with_echo_area_buffer,
9775 with_echo_area_buffer_unwind_data (w));
9776
9777 /* Make the echo area buffer current. Note that for display
9778 purposes, it is not necessary that the displayed window's buffer
9779 == current_buffer, except for text property lookup. So, let's
9780 only set that buffer temporarily here without doing a full
9781 Fset_window_buffer. We must also change w->pointm, though,
9782 because otherwise an assertions in unshow_buffer fails, and Emacs
9783 aborts. */
9784 set_buffer_internal_1 (XBUFFER (buffer));
9785 if (w)
9786 {
9787 w->buffer = buffer;
9788 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9789 }
9790
9791 BVAR (current_buffer, undo_list) = Qt;
9792 BVAR (current_buffer, read_only) = Qnil;
9793 specbind (Qinhibit_read_only, Qt);
9794 specbind (Qinhibit_modification_hooks, Qt);
9795
9796 if (clear_buffer_p && Z > BEG)
9797 del_range (BEG, Z);
9798
9799 xassert (BEGV >= BEG);
9800 xassert (ZV <= Z && ZV >= BEGV);
9801
9802 rc = fn (a1, a2, a3, a4);
9803
9804 xassert (BEGV >= BEG);
9805 xassert (ZV <= Z && ZV >= BEGV);
9806
9807 unbind_to (count, Qnil);
9808 return rc;
9809 }
9810
9811
9812 /* Save state that should be preserved around the call to the function
9813 FN called in with_echo_area_buffer. */
9814
9815 static Lisp_Object
9816 with_echo_area_buffer_unwind_data (struct window *w)
9817 {
9818 int i = 0;
9819 Lisp_Object vector, tmp;
9820
9821 /* Reduce consing by keeping one vector in
9822 Vwith_echo_area_save_vector. */
9823 vector = Vwith_echo_area_save_vector;
9824 Vwith_echo_area_save_vector = Qnil;
9825
9826 if (NILP (vector))
9827 vector = Fmake_vector (make_number (7), Qnil);
9828
9829 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9830 ASET (vector, i, Vdeactivate_mark); ++i;
9831 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9832
9833 if (w)
9834 {
9835 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9836 ASET (vector, i, w->buffer); ++i;
9837 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9838 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9839 }
9840 else
9841 {
9842 int end = i + 4;
9843 for (; i < end; ++i)
9844 ASET (vector, i, Qnil);
9845 }
9846
9847 xassert (i == ASIZE (vector));
9848 return vector;
9849 }
9850
9851
9852 /* Restore global state from VECTOR which was created by
9853 with_echo_area_buffer_unwind_data. */
9854
9855 static Lisp_Object
9856 unwind_with_echo_area_buffer (Lisp_Object vector)
9857 {
9858 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9859 Vdeactivate_mark = AREF (vector, 1);
9860 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9861
9862 if (WINDOWP (AREF (vector, 3)))
9863 {
9864 struct window *w;
9865 Lisp_Object buffer, charpos, bytepos;
9866
9867 w = XWINDOW (AREF (vector, 3));
9868 buffer = AREF (vector, 4);
9869 charpos = AREF (vector, 5);
9870 bytepos = AREF (vector, 6);
9871
9872 w->buffer = buffer;
9873 set_marker_both (w->pointm, buffer,
9874 XFASTINT (charpos), XFASTINT (bytepos));
9875 }
9876
9877 Vwith_echo_area_save_vector = vector;
9878 return Qnil;
9879 }
9880
9881
9882 /* Set up the echo area for use by print functions. MULTIBYTE_P
9883 non-zero means we will print multibyte. */
9884
9885 void
9886 setup_echo_area_for_printing (int multibyte_p)
9887 {
9888 /* If we can't find an echo area any more, exit. */
9889 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9890 Fkill_emacs (Qnil);
9891
9892 ensure_echo_area_buffers ();
9893
9894 if (!message_buf_print)
9895 {
9896 /* A message has been output since the last time we printed.
9897 Choose a fresh echo area buffer. */
9898 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9899 echo_area_buffer[0] = echo_buffer[1];
9900 else
9901 echo_area_buffer[0] = echo_buffer[0];
9902
9903 /* Switch to that buffer and clear it. */
9904 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9905 BVAR (current_buffer, truncate_lines) = Qnil;
9906
9907 if (Z > BEG)
9908 {
9909 int count = SPECPDL_INDEX ();
9910 specbind (Qinhibit_read_only, Qt);
9911 /* Note that undo recording is always disabled. */
9912 del_range (BEG, Z);
9913 unbind_to (count, Qnil);
9914 }
9915 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9916
9917 /* Set up the buffer for the multibyteness we need. */
9918 if (multibyte_p
9919 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9920 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9921
9922 /* Raise the frame containing the echo area. */
9923 if (minibuffer_auto_raise)
9924 {
9925 struct frame *sf = SELECTED_FRAME ();
9926 Lisp_Object mini_window;
9927 mini_window = FRAME_MINIBUF_WINDOW (sf);
9928 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9929 }
9930
9931 message_log_maybe_newline ();
9932 message_buf_print = 1;
9933 }
9934 else
9935 {
9936 if (NILP (echo_area_buffer[0]))
9937 {
9938 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9939 echo_area_buffer[0] = echo_buffer[1];
9940 else
9941 echo_area_buffer[0] = echo_buffer[0];
9942 }
9943
9944 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9945 {
9946 /* Someone switched buffers between print requests. */
9947 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9948 BVAR (current_buffer, truncate_lines) = Qnil;
9949 }
9950 }
9951 }
9952
9953
9954 /* Display an echo area message in window W. Value is non-zero if W's
9955 height is changed. If display_last_displayed_message_p is
9956 non-zero, display the message that was last displayed, otherwise
9957 display the current message. */
9958
9959 static int
9960 display_echo_area (struct window *w)
9961 {
9962 int i, no_message_p, window_height_changed_p, count;
9963
9964 /* Temporarily disable garbage collections while displaying the echo
9965 area. This is done because a GC can print a message itself.
9966 That message would modify the echo area buffer's contents while a
9967 redisplay of the buffer is going on, and seriously confuse
9968 redisplay. */
9969 count = inhibit_garbage_collection ();
9970
9971 /* If there is no message, we must call display_echo_area_1
9972 nevertheless because it resizes the window. But we will have to
9973 reset the echo_area_buffer in question to nil at the end because
9974 with_echo_area_buffer will sets it to an empty buffer. */
9975 i = display_last_displayed_message_p ? 1 : 0;
9976 no_message_p = NILP (echo_area_buffer[i]);
9977
9978 window_height_changed_p
9979 = with_echo_area_buffer (w, display_last_displayed_message_p,
9980 display_echo_area_1,
9981 (intptr_t) w, Qnil, 0, 0);
9982
9983 if (no_message_p)
9984 echo_area_buffer[i] = Qnil;
9985
9986 unbind_to (count, Qnil);
9987 return window_height_changed_p;
9988 }
9989
9990
9991 /* Helper for display_echo_area. Display the current buffer which
9992 contains the current echo area message in window W, a mini-window,
9993 a pointer to which is passed in A1. A2..A4 are currently not used.
9994 Change the height of W so that all of the message is displayed.
9995 Value is non-zero if height of W was changed. */
9996
9997 static int
9998 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9999 {
10000 intptr_t i1 = a1;
10001 struct window *w = (struct window *) i1;
10002 Lisp_Object window;
10003 struct text_pos start;
10004 int window_height_changed_p = 0;
10005
10006 /* Do this before displaying, so that we have a large enough glyph
10007 matrix for the display. If we can't get enough space for the
10008 whole text, display the last N lines. That works by setting w->start. */
10009 window_height_changed_p = resize_mini_window (w, 0);
10010
10011 /* Use the starting position chosen by resize_mini_window. */
10012 SET_TEXT_POS_FROM_MARKER (start, w->start);
10013
10014 /* Display. */
10015 clear_glyph_matrix (w->desired_matrix);
10016 XSETWINDOW (window, w);
10017 try_window (window, start, 0);
10018
10019 return window_height_changed_p;
10020 }
10021
10022
10023 /* Resize the echo area window to exactly the size needed for the
10024 currently displayed message, if there is one. If a mini-buffer
10025 is active, don't shrink it. */
10026
10027 void
10028 resize_echo_area_exactly (void)
10029 {
10030 if (BUFFERP (echo_area_buffer[0])
10031 && WINDOWP (echo_area_window))
10032 {
10033 struct window *w = XWINDOW (echo_area_window);
10034 int resized_p;
10035 Lisp_Object resize_exactly;
10036
10037 if (minibuf_level == 0)
10038 resize_exactly = Qt;
10039 else
10040 resize_exactly = Qnil;
10041
10042 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10043 (intptr_t) w, resize_exactly,
10044 0, 0);
10045 if (resized_p)
10046 {
10047 ++windows_or_buffers_changed;
10048 ++update_mode_lines;
10049 redisplay_internal ();
10050 }
10051 }
10052 }
10053
10054
10055 /* Callback function for with_echo_area_buffer, when used from
10056 resize_echo_area_exactly. A1 contains a pointer to the window to
10057 resize, EXACTLY non-nil means resize the mini-window exactly to the
10058 size of the text displayed. A3 and A4 are not used. Value is what
10059 resize_mini_window returns. */
10060
10061 static int
10062 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
10063 {
10064 intptr_t i1 = a1;
10065 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10066 }
10067
10068
10069 /* Resize mini-window W to fit the size of its contents. EXACT_P
10070 means size the window exactly to the size needed. Otherwise, it's
10071 only enlarged until W's buffer is empty.
10072
10073 Set W->start to the right place to begin display. If the whole
10074 contents fit, start at the beginning. Otherwise, start so as
10075 to make the end of the contents appear. This is particularly
10076 important for y-or-n-p, but seems desirable generally.
10077
10078 Value is non-zero if the window height has been changed. */
10079
10080 int
10081 resize_mini_window (struct window *w, int exact_p)
10082 {
10083 struct frame *f = XFRAME (w->frame);
10084 int window_height_changed_p = 0;
10085
10086 xassert (MINI_WINDOW_P (w));
10087
10088 /* By default, start display at the beginning. */
10089 set_marker_both (w->start, w->buffer,
10090 BUF_BEGV (XBUFFER (w->buffer)),
10091 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10092
10093 /* Don't resize windows while redisplaying a window; it would
10094 confuse redisplay functions when the size of the window they are
10095 displaying changes from under them. Such a resizing can happen,
10096 for instance, when which-func prints a long message while
10097 we are running fontification-functions. We're running these
10098 functions with safe_call which binds inhibit-redisplay to t. */
10099 if (!NILP (Vinhibit_redisplay))
10100 return 0;
10101
10102 /* Nil means don't try to resize. */
10103 if (NILP (Vresize_mini_windows)
10104 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10105 return 0;
10106
10107 if (!FRAME_MINIBUF_ONLY_P (f))
10108 {
10109 struct it it;
10110 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10111 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10112 int height, max_height;
10113 int unit = FRAME_LINE_HEIGHT (f);
10114 struct text_pos start;
10115 struct buffer *old_current_buffer = NULL;
10116
10117 if (current_buffer != XBUFFER (w->buffer))
10118 {
10119 old_current_buffer = current_buffer;
10120 set_buffer_internal (XBUFFER (w->buffer));
10121 }
10122
10123 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10124
10125 /* Compute the max. number of lines specified by the user. */
10126 if (FLOATP (Vmax_mini_window_height))
10127 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10128 else if (INTEGERP (Vmax_mini_window_height))
10129 max_height = XINT (Vmax_mini_window_height);
10130 else
10131 max_height = total_height / 4;
10132
10133 /* Correct that max. height if it's bogus. */
10134 max_height = max (1, max_height);
10135 max_height = min (total_height, max_height);
10136
10137 /* Find out the height of the text in the window. */
10138 if (it.line_wrap == TRUNCATE)
10139 height = 1;
10140 else
10141 {
10142 last_height = 0;
10143 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10144 if (it.max_ascent == 0 && it.max_descent == 0)
10145 height = it.current_y + last_height;
10146 else
10147 height = it.current_y + it.max_ascent + it.max_descent;
10148 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10149 height = (height + unit - 1) / unit;
10150 }
10151
10152 /* Compute a suitable window start. */
10153 if (height > max_height)
10154 {
10155 height = max_height;
10156 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10157 move_it_vertically_backward (&it, (height - 1) * unit);
10158 start = it.current.pos;
10159 }
10160 else
10161 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10162 SET_MARKER_FROM_TEXT_POS (w->start, start);
10163
10164 if (EQ (Vresize_mini_windows, Qgrow_only))
10165 {
10166 /* Let it grow only, until we display an empty message, in which
10167 case the window shrinks again. */
10168 if (height > WINDOW_TOTAL_LINES (w))
10169 {
10170 int old_height = WINDOW_TOTAL_LINES (w);
10171 freeze_window_starts (f, 1);
10172 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10173 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10174 }
10175 else if (height < WINDOW_TOTAL_LINES (w)
10176 && (exact_p || BEGV == ZV))
10177 {
10178 int old_height = WINDOW_TOTAL_LINES (w);
10179 freeze_window_starts (f, 0);
10180 shrink_mini_window (w);
10181 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10182 }
10183 }
10184 else
10185 {
10186 /* Always resize to exact size needed. */
10187 if (height > WINDOW_TOTAL_LINES (w))
10188 {
10189 int old_height = WINDOW_TOTAL_LINES (w);
10190 freeze_window_starts (f, 1);
10191 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10192 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10193 }
10194 else if (height < WINDOW_TOTAL_LINES (w))
10195 {
10196 int old_height = WINDOW_TOTAL_LINES (w);
10197 freeze_window_starts (f, 0);
10198 shrink_mini_window (w);
10199
10200 if (height)
10201 {
10202 freeze_window_starts (f, 1);
10203 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10204 }
10205
10206 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10207 }
10208 }
10209
10210 if (old_current_buffer)
10211 set_buffer_internal (old_current_buffer);
10212 }
10213
10214 return window_height_changed_p;
10215 }
10216
10217
10218 /* Value is the current message, a string, or nil if there is no
10219 current message. */
10220
10221 Lisp_Object
10222 current_message (void)
10223 {
10224 Lisp_Object msg;
10225
10226 if (!BUFFERP (echo_area_buffer[0]))
10227 msg = Qnil;
10228 else
10229 {
10230 with_echo_area_buffer (0, 0, current_message_1,
10231 (intptr_t) &msg, Qnil, 0, 0);
10232 if (NILP (msg))
10233 echo_area_buffer[0] = Qnil;
10234 }
10235
10236 return msg;
10237 }
10238
10239
10240 static int
10241 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10242 {
10243 intptr_t i1 = a1;
10244 Lisp_Object *msg = (Lisp_Object *) i1;
10245
10246 if (Z > BEG)
10247 *msg = make_buffer_string (BEG, Z, 1);
10248 else
10249 *msg = Qnil;
10250 return 0;
10251 }
10252
10253
10254 /* Push the current message on Vmessage_stack for later restoration
10255 by restore_message. Value is non-zero if the current message isn't
10256 empty. This is a relatively infrequent operation, so it's not
10257 worth optimizing. */
10258
10259 int
10260 push_message (void)
10261 {
10262 Lisp_Object msg;
10263 msg = current_message ();
10264 Vmessage_stack = Fcons (msg, Vmessage_stack);
10265 return STRINGP (msg);
10266 }
10267
10268
10269 /* Restore message display from the top of Vmessage_stack. */
10270
10271 void
10272 restore_message (void)
10273 {
10274 Lisp_Object msg;
10275
10276 xassert (CONSP (Vmessage_stack));
10277 msg = XCAR (Vmessage_stack);
10278 if (STRINGP (msg))
10279 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10280 else
10281 message3_nolog (msg, 0, 0);
10282 }
10283
10284
10285 /* Handler for record_unwind_protect calling pop_message. */
10286
10287 Lisp_Object
10288 pop_message_unwind (Lisp_Object dummy)
10289 {
10290 pop_message ();
10291 return Qnil;
10292 }
10293
10294 /* Pop the top-most entry off Vmessage_stack. */
10295
10296 static void
10297 pop_message (void)
10298 {
10299 xassert (CONSP (Vmessage_stack));
10300 Vmessage_stack = XCDR (Vmessage_stack);
10301 }
10302
10303
10304 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10305 exits. If the stack is not empty, we have a missing pop_message
10306 somewhere. */
10307
10308 void
10309 check_message_stack (void)
10310 {
10311 if (!NILP (Vmessage_stack))
10312 abort ();
10313 }
10314
10315
10316 /* Truncate to NCHARS what will be displayed in the echo area the next
10317 time we display it---but don't redisplay it now. */
10318
10319 void
10320 truncate_echo_area (EMACS_INT nchars)
10321 {
10322 if (nchars == 0)
10323 echo_area_buffer[0] = Qnil;
10324 /* A null message buffer means that the frame hasn't really been
10325 initialized yet. Error messages get reported properly by
10326 cmd_error, so this must be just an informative message; toss it. */
10327 else if (!noninteractive
10328 && INTERACTIVE
10329 && !NILP (echo_area_buffer[0]))
10330 {
10331 struct frame *sf = SELECTED_FRAME ();
10332 if (FRAME_MESSAGE_BUF (sf))
10333 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10334 }
10335 }
10336
10337
10338 /* Helper function for truncate_echo_area. Truncate the current
10339 message to at most NCHARS characters. */
10340
10341 static int
10342 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10343 {
10344 if (BEG + nchars < Z)
10345 del_range (BEG + nchars, Z);
10346 if (Z == BEG)
10347 echo_area_buffer[0] = Qnil;
10348 return 0;
10349 }
10350
10351
10352 /* Set the current message to a substring of S or STRING.
10353
10354 If STRING is a Lisp string, set the message to the first NBYTES
10355 bytes from STRING. NBYTES zero means use the whole string. If
10356 STRING is multibyte, the message will be displayed multibyte.
10357
10358 If S is not null, set the message to the first LEN bytes of S. LEN
10359 zero means use the whole string. MULTIBYTE_P non-zero means S is
10360 multibyte. Display the message multibyte in that case.
10361
10362 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10363 to t before calling set_message_1 (which calls insert).
10364 */
10365
10366 static void
10367 set_message (const char *s, Lisp_Object string,
10368 EMACS_INT nbytes, int multibyte_p)
10369 {
10370 message_enable_multibyte
10371 = ((s && multibyte_p)
10372 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10373
10374 with_echo_area_buffer (0, -1, set_message_1,
10375 (intptr_t) s, string, nbytes, multibyte_p);
10376 message_buf_print = 0;
10377 help_echo_showing_p = 0;
10378 }
10379
10380
10381 /* Helper function for set_message. Arguments have the same meaning
10382 as there, with A1 corresponding to S and A2 corresponding to STRING
10383 This function is called with the echo area buffer being
10384 current. */
10385
10386 static int
10387 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10388 {
10389 intptr_t i1 = a1;
10390 const char *s = (const char *) i1;
10391 const unsigned char *msg = (const unsigned char *) s;
10392 Lisp_Object string = a2;
10393
10394 /* Change multibyteness of the echo buffer appropriately. */
10395 if (message_enable_multibyte
10396 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10397 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10398
10399 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10400 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10401 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10402
10403 /* Insert new message at BEG. */
10404 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10405
10406 if (STRINGP (string))
10407 {
10408 EMACS_INT nchars;
10409
10410 if (nbytes == 0)
10411 nbytes = SBYTES (string);
10412 nchars = string_byte_to_char (string, nbytes);
10413
10414 /* This function takes care of single/multibyte conversion. We
10415 just have to ensure that the echo area buffer has the right
10416 setting of enable_multibyte_characters. */
10417 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10418 }
10419 else if (s)
10420 {
10421 if (nbytes == 0)
10422 nbytes = strlen (s);
10423
10424 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10425 {
10426 /* Convert from multi-byte to single-byte. */
10427 EMACS_INT i;
10428 int c, n;
10429 char work[1];
10430
10431 /* Convert a multibyte string to single-byte. */
10432 for (i = 0; i < nbytes; i += n)
10433 {
10434 c = string_char_and_length (msg + i, &n);
10435 work[0] = (ASCII_CHAR_P (c)
10436 ? c
10437 : multibyte_char_to_unibyte (c));
10438 insert_1_both (work, 1, 1, 1, 0, 0);
10439 }
10440 }
10441 else if (!multibyte_p
10442 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10443 {
10444 /* Convert from single-byte to multi-byte. */
10445 EMACS_INT i;
10446 int c, n;
10447 unsigned char str[MAX_MULTIBYTE_LENGTH];
10448
10449 /* Convert a single-byte string to multibyte. */
10450 for (i = 0; i < nbytes; i++)
10451 {
10452 c = msg[i];
10453 MAKE_CHAR_MULTIBYTE (c);
10454 n = CHAR_STRING (c, str);
10455 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10456 }
10457 }
10458 else
10459 insert_1 (s, nbytes, 1, 0, 0);
10460 }
10461
10462 return 0;
10463 }
10464
10465
10466 /* Clear messages. CURRENT_P non-zero means clear the current
10467 message. LAST_DISPLAYED_P non-zero means clear the message
10468 last displayed. */
10469
10470 void
10471 clear_message (int current_p, int last_displayed_p)
10472 {
10473 if (current_p)
10474 {
10475 echo_area_buffer[0] = Qnil;
10476 message_cleared_p = 1;
10477 }
10478
10479 if (last_displayed_p)
10480 echo_area_buffer[1] = Qnil;
10481
10482 message_buf_print = 0;
10483 }
10484
10485 /* Clear garbaged frames.
10486
10487 This function is used where the old redisplay called
10488 redraw_garbaged_frames which in turn called redraw_frame which in
10489 turn called clear_frame. The call to clear_frame was a source of
10490 flickering. I believe a clear_frame is not necessary. It should
10491 suffice in the new redisplay to invalidate all current matrices,
10492 and ensure a complete redisplay of all windows. */
10493
10494 static void
10495 clear_garbaged_frames (void)
10496 {
10497 if (frame_garbaged)
10498 {
10499 Lisp_Object tail, frame;
10500 int changed_count = 0;
10501
10502 FOR_EACH_FRAME (tail, frame)
10503 {
10504 struct frame *f = XFRAME (frame);
10505
10506 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10507 {
10508 if (f->resized_p)
10509 {
10510 Fredraw_frame (frame);
10511 f->force_flush_display_p = 1;
10512 }
10513 clear_current_matrices (f);
10514 changed_count++;
10515 f->garbaged = 0;
10516 f->resized_p = 0;
10517 }
10518 }
10519
10520 frame_garbaged = 0;
10521 if (changed_count)
10522 ++windows_or_buffers_changed;
10523 }
10524 }
10525
10526
10527 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10528 is non-zero update selected_frame. Value is non-zero if the
10529 mini-windows height has been changed. */
10530
10531 static int
10532 echo_area_display (int update_frame_p)
10533 {
10534 Lisp_Object mini_window;
10535 struct window *w;
10536 struct frame *f;
10537 int window_height_changed_p = 0;
10538 struct frame *sf = SELECTED_FRAME ();
10539
10540 mini_window = FRAME_MINIBUF_WINDOW (sf);
10541 w = XWINDOW (mini_window);
10542 f = XFRAME (WINDOW_FRAME (w));
10543
10544 /* Don't display if frame is invisible or not yet initialized. */
10545 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10546 return 0;
10547
10548 #ifdef HAVE_WINDOW_SYSTEM
10549 /* When Emacs starts, selected_frame may be the initial terminal
10550 frame. If we let this through, a message would be displayed on
10551 the terminal. */
10552 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10553 return 0;
10554 #endif /* HAVE_WINDOW_SYSTEM */
10555
10556 /* Redraw garbaged frames. */
10557 if (frame_garbaged)
10558 clear_garbaged_frames ();
10559
10560 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10561 {
10562 echo_area_window = mini_window;
10563 window_height_changed_p = display_echo_area (w);
10564 w->must_be_updated_p = 1;
10565
10566 /* Update the display, unless called from redisplay_internal.
10567 Also don't update the screen during redisplay itself. The
10568 update will happen at the end of redisplay, and an update
10569 here could cause confusion. */
10570 if (update_frame_p && !redisplaying_p)
10571 {
10572 int n = 0;
10573
10574 /* If the display update has been interrupted by pending
10575 input, update mode lines in the frame. Due to the
10576 pending input, it might have been that redisplay hasn't
10577 been called, so that mode lines above the echo area are
10578 garbaged. This looks odd, so we prevent it here. */
10579 if (!display_completed)
10580 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10581
10582 if (window_height_changed_p
10583 /* Don't do this if Emacs is shutting down. Redisplay
10584 needs to run hooks. */
10585 && !NILP (Vrun_hooks))
10586 {
10587 /* Must update other windows. Likewise as in other
10588 cases, don't let this update be interrupted by
10589 pending input. */
10590 int count = SPECPDL_INDEX ();
10591 specbind (Qredisplay_dont_pause, Qt);
10592 windows_or_buffers_changed = 1;
10593 redisplay_internal ();
10594 unbind_to (count, Qnil);
10595 }
10596 else if (FRAME_WINDOW_P (f) && n == 0)
10597 {
10598 /* Window configuration is the same as before.
10599 Can do with a display update of the echo area,
10600 unless we displayed some mode lines. */
10601 update_single_window (w, 1);
10602 FRAME_RIF (f)->flush_display (f);
10603 }
10604 else
10605 update_frame (f, 1, 1);
10606
10607 /* If cursor is in the echo area, make sure that the next
10608 redisplay displays the minibuffer, so that the cursor will
10609 be replaced with what the minibuffer wants. */
10610 if (cursor_in_echo_area)
10611 ++windows_or_buffers_changed;
10612 }
10613 }
10614 else if (!EQ (mini_window, selected_window))
10615 windows_or_buffers_changed++;
10616
10617 /* Last displayed message is now the current message. */
10618 echo_area_buffer[1] = echo_area_buffer[0];
10619 /* Inform read_char that we're not echoing. */
10620 echo_message_buffer = Qnil;
10621
10622 /* Prevent redisplay optimization in redisplay_internal by resetting
10623 this_line_start_pos. This is done because the mini-buffer now
10624 displays the message instead of its buffer text. */
10625 if (EQ (mini_window, selected_window))
10626 CHARPOS (this_line_start_pos) = 0;
10627
10628 return window_height_changed_p;
10629 }
10630
10631
10632 \f
10633 /***********************************************************************
10634 Mode Lines and Frame Titles
10635 ***********************************************************************/
10636
10637 /* A buffer for constructing non-propertized mode-line strings and
10638 frame titles in it; allocated from the heap in init_xdisp and
10639 resized as needed in store_mode_line_noprop_char. */
10640
10641 static char *mode_line_noprop_buf;
10642
10643 /* The buffer's end, and a current output position in it. */
10644
10645 static char *mode_line_noprop_buf_end;
10646 static char *mode_line_noprop_ptr;
10647
10648 #define MODE_LINE_NOPROP_LEN(start) \
10649 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10650
10651 static enum {
10652 MODE_LINE_DISPLAY = 0,
10653 MODE_LINE_TITLE,
10654 MODE_LINE_NOPROP,
10655 MODE_LINE_STRING
10656 } mode_line_target;
10657
10658 /* Alist that caches the results of :propertize.
10659 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10660 static Lisp_Object mode_line_proptrans_alist;
10661
10662 /* List of strings making up the mode-line. */
10663 static Lisp_Object mode_line_string_list;
10664
10665 /* Base face property when building propertized mode line string. */
10666 static Lisp_Object mode_line_string_face;
10667 static Lisp_Object mode_line_string_face_prop;
10668
10669
10670 /* Unwind data for mode line strings */
10671
10672 static Lisp_Object Vmode_line_unwind_vector;
10673
10674 static Lisp_Object
10675 format_mode_line_unwind_data (struct buffer *obuf,
10676 Lisp_Object owin,
10677 int save_proptrans)
10678 {
10679 Lisp_Object vector, tmp;
10680
10681 /* Reduce consing by keeping one vector in
10682 Vwith_echo_area_save_vector. */
10683 vector = Vmode_line_unwind_vector;
10684 Vmode_line_unwind_vector = Qnil;
10685
10686 if (NILP (vector))
10687 vector = Fmake_vector (make_number (8), Qnil);
10688
10689 ASET (vector, 0, make_number (mode_line_target));
10690 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10691 ASET (vector, 2, mode_line_string_list);
10692 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10693 ASET (vector, 4, mode_line_string_face);
10694 ASET (vector, 5, mode_line_string_face_prop);
10695
10696 if (obuf)
10697 XSETBUFFER (tmp, obuf);
10698 else
10699 tmp = Qnil;
10700 ASET (vector, 6, tmp);
10701 ASET (vector, 7, owin);
10702
10703 return vector;
10704 }
10705
10706 static Lisp_Object
10707 unwind_format_mode_line (Lisp_Object vector)
10708 {
10709 mode_line_target = XINT (AREF (vector, 0));
10710 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10711 mode_line_string_list = AREF (vector, 2);
10712 if (! EQ (AREF (vector, 3), Qt))
10713 mode_line_proptrans_alist = AREF (vector, 3);
10714 mode_line_string_face = AREF (vector, 4);
10715 mode_line_string_face_prop = AREF (vector, 5);
10716
10717 if (!NILP (AREF (vector, 7)))
10718 /* Select window before buffer, since it may change the buffer. */
10719 Fselect_window (AREF (vector, 7), Qt);
10720
10721 if (!NILP (AREF (vector, 6)))
10722 {
10723 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10724 ASET (vector, 6, Qnil);
10725 }
10726
10727 Vmode_line_unwind_vector = vector;
10728 return Qnil;
10729 }
10730
10731
10732 /* Store a single character C for the frame title in mode_line_noprop_buf.
10733 Re-allocate mode_line_noprop_buf if necessary. */
10734
10735 static void
10736 store_mode_line_noprop_char (char c)
10737 {
10738 /* If output position has reached the end of the allocated buffer,
10739 increase the buffer's size. */
10740 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10741 {
10742 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10743 ptrdiff_t size = len;
10744 mode_line_noprop_buf =
10745 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10746 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10747 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10748 }
10749
10750 *mode_line_noprop_ptr++ = c;
10751 }
10752
10753
10754 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10755 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10756 characters that yield more columns than PRECISION; PRECISION <= 0
10757 means copy the whole string. Pad with spaces until FIELD_WIDTH
10758 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10759 pad. Called from display_mode_element when it is used to build a
10760 frame title. */
10761
10762 static int
10763 store_mode_line_noprop (const char *string, int field_width, int precision)
10764 {
10765 const unsigned char *str = (const unsigned char *) string;
10766 int n = 0;
10767 EMACS_INT dummy, nbytes;
10768
10769 /* Copy at most PRECISION chars from STR. */
10770 nbytes = strlen (string);
10771 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10772 while (nbytes--)
10773 store_mode_line_noprop_char (*str++);
10774
10775 /* Fill up with spaces until FIELD_WIDTH reached. */
10776 while (field_width > 0
10777 && n < field_width)
10778 {
10779 store_mode_line_noprop_char (' ');
10780 ++n;
10781 }
10782
10783 return n;
10784 }
10785
10786 /***********************************************************************
10787 Frame Titles
10788 ***********************************************************************/
10789
10790 #ifdef HAVE_WINDOW_SYSTEM
10791
10792 /* Set the title of FRAME, if it has changed. The title format is
10793 Vicon_title_format if FRAME is iconified, otherwise it is
10794 frame_title_format. */
10795
10796 static void
10797 x_consider_frame_title (Lisp_Object frame)
10798 {
10799 struct frame *f = XFRAME (frame);
10800
10801 if (FRAME_WINDOW_P (f)
10802 || FRAME_MINIBUF_ONLY_P (f)
10803 || f->explicit_name)
10804 {
10805 /* Do we have more than one visible frame on this X display? */
10806 Lisp_Object tail;
10807 Lisp_Object fmt;
10808 ptrdiff_t title_start;
10809 char *title;
10810 ptrdiff_t len;
10811 struct it it;
10812 int count = SPECPDL_INDEX ();
10813
10814 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10815 {
10816 Lisp_Object other_frame = XCAR (tail);
10817 struct frame *tf = XFRAME (other_frame);
10818
10819 if (tf != f
10820 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10821 && !FRAME_MINIBUF_ONLY_P (tf)
10822 && !EQ (other_frame, tip_frame)
10823 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10824 break;
10825 }
10826
10827 /* Set global variable indicating that multiple frames exist. */
10828 multiple_frames = CONSP (tail);
10829
10830 /* Switch to the buffer of selected window of the frame. Set up
10831 mode_line_target so that display_mode_element will output into
10832 mode_line_noprop_buf; then display the title. */
10833 record_unwind_protect (unwind_format_mode_line,
10834 format_mode_line_unwind_data
10835 (current_buffer, selected_window, 0));
10836
10837 Fselect_window (f->selected_window, Qt);
10838 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10839 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10840
10841 mode_line_target = MODE_LINE_TITLE;
10842 title_start = MODE_LINE_NOPROP_LEN (0);
10843 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10844 NULL, DEFAULT_FACE_ID);
10845 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10846 len = MODE_LINE_NOPROP_LEN (title_start);
10847 title = mode_line_noprop_buf + title_start;
10848 unbind_to (count, Qnil);
10849
10850 /* Set the title only if it's changed. This avoids consing in
10851 the common case where it hasn't. (If it turns out that we've
10852 already wasted too much time by walking through the list with
10853 display_mode_element, then we might need to optimize at a
10854 higher level than this.) */
10855 if (! STRINGP (f->name)
10856 || SBYTES (f->name) != len
10857 || memcmp (title, SDATA (f->name), len) != 0)
10858 x_implicitly_set_name (f, make_string (title, len), Qnil);
10859 }
10860 }
10861
10862 #endif /* not HAVE_WINDOW_SYSTEM */
10863
10864
10865
10866 \f
10867 /***********************************************************************
10868 Menu Bars
10869 ***********************************************************************/
10870
10871
10872 /* Prepare for redisplay by updating menu-bar item lists when
10873 appropriate. This can call eval. */
10874
10875 void
10876 prepare_menu_bars (void)
10877 {
10878 int all_windows;
10879 struct gcpro gcpro1, gcpro2;
10880 struct frame *f;
10881 Lisp_Object tooltip_frame;
10882
10883 #ifdef HAVE_WINDOW_SYSTEM
10884 tooltip_frame = tip_frame;
10885 #else
10886 tooltip_frame = Qnil;
10887 #endif
10888
10889 /* Update all frame titles based on their buffer names, etc. We do
10890 this before the menu bars so that the buffer-menu will show the
10891 up-to-date frame titles. */
10892 #ifdef HAVE_WINDOW_SYSTEM
10893 if (windows_or_buffers_changed || update_mode_lines)
10894 {
10895 Lisp_Object tail, frame;
10896
10897 FOR_EACH_FRAME (tail, frame)
10898 {
10899 f = XFRAME (frame);
10900 if (!EQ (frame, tooltip_frame)
10901 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10902 x_consider_frame_title (frame);
10903 }
10904 }
10905 #endif /* HAVE_WINDOW_SYSTEM */
10906
10907 /* Update the menu bar item lists, if appropriate. This has to be
10908 done before any actual redisplay or generation of display lines. */
10909 all_windows = (update_mode_lines
10910 || buffer_shared > 1
10911 || windows_or_buffers_changed);
10912 if (all_windows)
10913 {
10914 Lisp_Object tail, frame;
10915 int count = SPECPDL_INDEX ();
10916 /* 1 means that update_menu_bar has run its hooks
10917 so any further calls to update_menu_bar shouldn't do so again. */
10918 int menu_bar_hooks_run = 0;
10919
10920 record_unwind_save_match_data ();
10921
10922 FOR_EACH_FRAME (tail, frame)
10923 {
10924 f = XFRAME (frame);
10925
10926 /* Ignore tooltip frame. */
10927 if (EQ (frame, tooltip_frame))
10928 continue;
10929
10930 /* If a window on this frame changed size, report that to
10931 the user and clear the size-change flag. */
10932 if (FRAME_WINDOW_SIZES_CHANGED (f))
10933 {
10934 Lisp_Object functions;
10935
10936 /* Clear flag first in case we get an error below. */
10937 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10938 functions = Vwindow_size_change_functions;
10939 GCPRO2 (tail, functions);
10940
10941 while (CONSP (functions))
10942 {
10943 if (!EQ (XCAR (functions), Qt))
10944 call1 (XCAR (functions), frame);
10945 functions = XCDR (functions);
10946 }
10947 UNGCPRO;
10948 }
10949
10950 GCPRO1 (tail);
10951 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10952 #ifdef HAVE_WINDOW_SYSTEM
10953 update_tool_bar (f, 0);
10954 #endif
10955 #ifdef HAVE_NS
10956 if (windows_or_buffers_changed
10957 && FRAME_NS_P (f))
10958 ns_set_doc_edited (f, Fbuffer_modified_p
10959 (XWINDOW (f->selected_window)->buffer));
10960 #endif
10961 UNGCPRO;
10962 }
10963
10964 unbind_to (count, Qnil);
10965 }
10966 else
10967 {
10968 struct frame *sf = SELECTED_FRAME ();
10969 update_menu_bar (sf, 1, 0);
10970 #ifdef HAVE_WINDOW_SYSTEM
10971 update_tool_bar (sf, 1);
10972 #endif
10973 }
10974 }
10975
10976
10977 /* Update the menu bar item list for frame F. This has to be done
10978 before we start to fill in any display lines, because it can call
10979 eval.
10980
10981 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10982
10983 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10984 already ran the menu bar hooks for this redisplay, so there
10985 is no need to run them again. The return value is the
10986 updated value of this flag, to pass to the next call. */
10987
10988 static int
10989 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10990 {
10991 Lisp_Object window;
10992 register struct window *w;
10993
10994 /* If called recursively during a menu update, do nothing. This can
10995 happen when, for instance, an activate-menubar-hook causes a
10996 redisplay. */
10997 if (inhibit_menubar_update)
10998 return hooks_run;
10999
11000 window = FRAME_SELECTED_WINDOW (f);
11001 w = XWINDOW (window);
11002
11003 if (FRAME_WINDOW_P (f)
11004 ?
11005 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11006 || defined (HAVE_NS) || defined (USE_GTK)
11007 FRAME_EXTERNAL_MENU_BAR (f)
11008 #else
11009 FRAME_MENU_BAR_LINES (f) > 0
11010 #endif
11011 : FRAME_MENU_BAR_LINES (f) > 0)
11012 {
11013 /* If the user has switched buffers or windows, we need to
11014 recompute to reflect the new bindings. But we'll
11015 recompute when update_mode_lines is set too; that means
11016 that people can use force-mode-line-update to request
11017 that the menu bar be recomputed. The adverse effect on
11018 the rest of the redisplay algorithm is about the same as
11019 windows_or_buffers_changed anyway. */
11020 if (windows_or_buffers_changed
11021 /* This used to test w->update_mode_line, but we believe
11022 there is no need to recompute the menu in that case. */
11023 || update_mode_lines
11024 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11025 < BUF_MODIFF (XBUFFER (w->buffer)))
11026 != !NILP (w->last_had_star))
11027 || ((!NILP (Vtransient_mark_mode)
11028 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11029 != !NILP (w->region_showing)))
11030 {
11031 struct buffer *prev = current_buffer;
11032 int count = SPECPDL_INDEX ();
11033
11034 specbind (Qinhibit_menubar_update, Qt);
11035
11036 set_buffer_internal_1 (XBUFFER (w->buffer));
11037 if (save_match_data)
11038 record_unwind_save_match_data ();
11039 if (NILP (Voverriding_local_map_menu_flag))
11040 {
11041 specbind (Qoverriding_terminal_local_map, Qnil);
11042 specbind (Qoverriding_local_map, Qnil);
11043 }
11044
11045 if (!hooks_run)
11046 {
11047 /* Run the Lucid hook. */
11048 safe_run_hooks (Qactivate_menubar_hook);
11049
11050 /* If it has changed current-menubar from previous value,
11051 really recompute the menu-bar from the value. */
11052 if (! NILP (Vlucid_menu_bar_dirty_flag))
11053 call0 (Qrecompute_lucid_menubar);
11054
11055 safe_run_hooks (Qmenu_bar_update_hook);
11056
11057 hooks_run = 1;
11058 }
11059
11060 XSETFRAME (Vmenu_updating_frame, f);
11061 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11062
11063 /* Redisplay the menu bar in case we changed it. */
11064 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11065 || defined (HAVE_NS) || defined (USE_GTK)
11066 if (FRAME_WINDOW_P (f))
11067 {
11068 #if defined (HAVE_NS)
11069 /* All frames on Mac OS share the same menubar. So only
11070 the selected frame should be allowed to set it. */
11071 if (f == SELECTED_FRAME ())
11072 #endif
11073 set_frame_menubar (f, 0, 0);
11074 }
11075 else
11076 /* On a terminal screen, the menu bar is an ordinary screen
11077 line, and this makes it get updated. */
11078 w->update_mode_line = Qt;
11079 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11080 /* In the non-toolkit version, the menu bar is an ordinary screen
11081 line, and this makes it get updated. */
11082 w->update_mode_line = Qt;
11083 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11084
11085 unbind_to (count, Qnil);
11086 set_buffer_internal_1 (prev);
11087 }
11088 }
11089
11090 return hooks_run;
11091 }
11092
11093
11094 \f
11095 /***********************************************************************
11096 Output Cursor
11097 ***********************************************************************/
11098
11099 #ifdef HAVE_WINDOW_SYSTEM
11100
11101 /* EXPORT:
11102 Nominal cursor position -- where to draw output.
11103 HPOS and VPOS are window relative glyph matrix coordinates.
11104 X and Y are window relative pixel coordinates. */
11105
11106 struct cursor_pos output_cursor;
11107
11108
11109 /* EXPORT:
11110 Set the global variable output_cursor to CURSOR. All cursor
11111 positions are relative to updated_window. */
11112
11113 void
11114 set_output_cursor (struct cursor_pos *cursor)
11115 {
11116 output_cursor.hpos = cursor->hpos;
11117 output_cursor.vpos = cursor->vpos;
11118 output_cursor.x = cursor->x;
11119 output_cursor.y = cursor->y;
11120 }
11121
11122
11123 /* EXPORT for RIF:
11124 Set a nominal cursor position.
11125
11126 HPOS and VPOS are column/row positions in a window glyph matrix. X
11127 and Y are window text area relative pixel positions.
11128
11129 If this is done during an update, updated_window will contain the
11130 window that is being updated and the position is the future output
11131 cursor position for that window. If updated_window is null, use
11132 selected_window and display the cursor at the given position. */
11133
11134 void
11135 x_cursor_to (int vpos, int hpos, int y, int x)
11136 {
11137 struct window *w;
11138
11139 /* If updated_window is not set, work on selected_window. */
11140 if (updated_window)
11141 w = updated_window;
11142 else
11143 w = XWINDOW (selected_window);
11144
11145 /* Set the output cursor. */
11146 output_cursor.hpos = hpos;
11147 output_cursor.vpos = vpos;
11148 output_cursor.x = x;
11149 output_cursor.y = y;
11150
11151 /* If not called as part of an update, really display the cursor.
11152 This will also set the cursor position of W. */
11153 if (updated_window == NULL)
11154 {
11155 BLOCK_INPUT;
11156 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11157 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11158 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11159 UNBLOCK_INPUT;
11160 }
11161 }
11162
11163 #endif /* HAVE_WINDOW_SYSTEM */
11164
11165 \f
11166 /***********************************************************************
11167 Tool-bars
11168 ***********************************************************************/
11169
11170 #ifdef HAVE_WINDOW_SYSTEM
11171
11172 /* Where the mouse was last time we reported a mouse event. */
11173
11174 FRAME_PTR last_mouse_frame;
11175
11176 /* Tool-bar item index of the item on which a mouse button was pressed
11177 or -1. */
11178
11179 int last_tool_bar_item;
11180
11181
11182 static Lisp_Object
11183 update_tool_bar_unwind (Lisp_Object frame)
11184 {
11185 selected_frame = frame;
11186 return Qnil;
11187 }
11188
11189 /* Update the tool-bar item list for frame F. This has to be done
11190 before we start to fill in any display lines. Called from
11191 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11192 and restore it here. */
11193
11194 static void
11195 update_tool_bar (struct frame *f, int save_match_data)
11196 {
11197 #if defined (USE_GTK) || defined (HAVE_NS)
11198 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11199 #else
11200 int do_update = WINDOWP (f->tool_bar_window)
11201 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11202 #endif
11203
11204 if (do_update)
11205 {
11206 Lisp_Object window;
11207 struct window *w;
11208
11209 window = FRAME_SELECTED_WINDOW (f);
11210 w = XWINDOW (window);
11211
11212 /* If the user has switched buffers or windows, we need to
11213 recompute to reflect the new bindings. But we'll
11214 recompute when update_mode_lines is set too; that means
11215 that people can use force-mode-line-update to request
11216 that the menu bar be recomputed. The adverse effect on
11217 the rest of the redisplay algorithm is about the same as
11218 windows_or_buffers_changed anyway. */
11219 if (windows_or_buffers_changed
11220 || !NILP (w->update_mode_line)
11221 || update_mode_lines
11222 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11223 < BUF_MODIFF (XBUFFER (w->buffer)))
11224 != !NILP (w->last_had_star))
11225 || ((!NILP (Vtransient_mark_mode)
11226 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11227 != !NILP (w->region_showing)))
11228 {
11229 struct buffer *prev = current_buffer;
11230 int count = SPECPDL_INDEX ();
11231 Lisp_Object frame, new_tool_bar;
11232 int new_n_tool_bar;
11233 struct gcpro gcpro1;
11234
11235 /* Set current_buffer to the buffer of the selected
11236 window of the frame, so that we get the right local
11237 keymaps. */
11238 set_buffer_internal_1 (XBUFFER (w->buffer));
11239
11240 /* Save match data, if we must. */
11241 if (save_match_data)
11242 record_unwind_save_match_data ();
11243
11244 /* Make sure that we don't accidentally use bogus keymaps. */
11245 if (NILP (Voverriding_local_map_menu_flag))
11246 {
11247 specbind (Qoverriding_terminal_local_map, Qnil);
11248 specbind (Qoverriding_local_map, Qnil);
11249 }
11250
11251 GCPRO1 (new_tool_bar);
11252
11253 /* We must temporarily set the selected frame to this frame
11254 before calling tool_bar_items, because the calculation of
11255 the tool-bar keymap uses the selected frame (see
11256 `tool-bar-make-keymap' in tool-bar.el). */
11257 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11258 XSETFRAME (frame, f);
11259 selected_frame = frame;
11260
11261 /* Build desired tool-bar items from keymaps. */
11262 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11263 &new_n_tool_bar);
11264
11265 /* Redisplay the tool-bar if we changed it. */
11266 if (new_n_tool_bar != f->n_tool_bar_items
11267 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11268 {
11269 /* Redisplay that happens asynchronously due to an expose event
11270 may access f->tool_bar_items. Make sure we update both
11271 variables within BLOCK_INPUT so no such event interrupts. */
11272 BLOCK_INPUT;
11273 f->tool_bar_items = new_tool_bar;
11274 f->n_tool_bar_items = new_n_tool_bar;
11275 w->update_mode_line = Qt;
11276 UNBLOCK_INPUT;
11277 }
11278
11279 UNGCPRO;
11280
11281 unbind_to (count, Qnil);
11282 set_buffer_internal_1 (prev);
11283 }
11284 }
11285 }
11286
11287
11288 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11289 F's desired tool-bar contents. F->tool_bar_items must have
11290 been set up previously by calling prepare_menu_bars. */
11291
11292 static void
11293 build_desired_tool_bar_string (struct frame *f)
11294 {
11295 int i, size, size_needed;
11296 struct gcpro gcpro1, gcpro2, gcpro3;
11297 Lisp_Object image, plist, props;
11298
11299 image = plist = props = Qnil;
11300 GCPRO3 (image, plist, props);
11301
11302 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11303 Otherwise, make a new string. */
11304
11305 /* The size of the string we might be able to reuse. */
11306 size = (STRINGP (f->desired_tool_bar_string)
11307 ? SCHARS (f->desired_tool_bar_string)
11308 : 0);
11309
11310 /* We need one space in the string for each image. */
11311 size_needed = f->n_tool_bar_items;
11312
11313 /* Reuse f->desired_tool_bar_string, if possible. */
11314 if (size < size_needed || NILP (f->desired_tool_bar_string))
11315 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11316 make_number (' '));
11317 else
11318 {
11319 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11320 Fremove_text_properties (make_number (0), make_number (size),
11321 props, f->desired_tool_bar_string);
11322 }
11323
11324 /* Put a `display' property on the string for the images to display,
11325 put a `menu_item' property on tool-bar items with a value that
11326 is the index of the item in F's tool-bar item vector. */
11327 for (i = 0; i < f->n_tool_bar_items; ++i)
11328 {
11329 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11330
11331 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11332 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11333 int hmargin, vmargin, relief, idx, end;
11334
11335 /* If image is a vector, choose the image according to the
11336 button state. */
11337 image = PROP (TOOL_BAR_ITEM_IMAGES);
11338 if (VECTORP (image))
11339 {
11340 if (enabled_p)
11341 idx = (selected_p
11342 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11343 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11344 else
11345 idx = (selected_p
11346 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11347 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11348
11349 xassert (ASIZE (image) >= idx);
11350 image = AREF (image, idx);
11351 }
11352 else
11353 idx = -1;
11354
11355 /* Ignore invalid image specifications. */
11356 if (!valid_image_p (image))
11357 continue;
11358
11359 /* Display the tool-bar button pressed, or depressed. */
11360 plist = Fcopy_sequence (XCDR (image));
11361
11362 /* Compute margin and relief to draw. */
11363 relief = (tool_bar_button_relief >= 0
11364 ? tool_bar_button_relief
11365 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11366 hmargin = vmargin = relief;
11367
11368 if (INTEGERP (Vtool_bar_button_margin)
11369 && XINT (Vtool_bar_button_margin) > 0)
11370 {
11371 hmargin += XFASTINT (Vtool_bar_button_margin);
11372 vmargin += XFASTINT (Vtool_bar_button_margin);
11373 }
11374 else if (CONSP (Vtool_bar_button_margin))
11375 {
11376 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11377 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11378 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11379
11380 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11381 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11382 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11383 }
11384
11385 if (auto_raise_tool_bar_buttons_p)
11386 {
11387 /* Add a `:relief' property to the image spec if the item is
11388 selected. */
11389 if (selected_p)
11390 {
11391 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11392 hmargin -= relief;
11393 vmargin -= relief;
11394 }
11395 }
11396 else
11397 {
11398 /* If image is selected, display it pressed, i.e. with a
11399 negative relief. If it's not selected, display it with a
11400 raised relief. */
11401 plist = Fplist_put (plist, QCrelief,
11402 (selected_p
11403 ? make_number (-relief)
11404 : make_number (relief)));
11405 hmargin -= relief;
11406 vmargin -= relief;
11407 }
11408
11409 /* Put a margin around the image. */
11410 if (hmargin || vmargin)
11411 {
11412 if (hmargin == vmargin)
11413 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11414 else
11415 plist = Fplist_put (plist, QCmargin,
11416 Fcons (make_number (hmargin),
11417 make_number (vmargin)));
11418 }
11419
11420 /* If button is not enabled, and we don't have special images
11421 for the disabled state, make the image appear disabled by
11422 applying an appropriate algorithm to it. */
11423 if (!enabled_p && idx < 0)
11424 plist = Fplist_put (plist, QCconversion, Qdisabled);
11425
11426 /* Put a `display' text property on the string for the image to
11427 display. Put a `menu-item' property on the string that gives
11428 the start of this item's properties in the tool-bar items
11429 vector. */
11430 image = Fcons (Qimage, plist);
11431 props = list4 (Qdisplay, image,
11432 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11433
11434 /* Let the last image hide all remaining spaces in the tool bar
11435 string. The string can be longer than needed when we reuse a
11436 previous string. */
11437 if (i + 1 == f->n_tool_bar_items)
11438 end = SCHARS (f->desired_tool_bar_string);
11439 else
11440 end = i + 1;
11441 Fadd_text_properties (make_number (i), make_number (end),
11442 props, f->desired_tool_bar_string);
11443 #undef PROP
11444 }
11445
11446 UNGCPRO;
11447 }
11448
11449
11450 /* Display one line of the tool-bar of frame IT->f.
11451
11452 HEIGHT specifies the desired height of the tool-bar line.
11453 If the actual height of the glyph row is less than HEIGHT, the
11454 row's height is increased to HEIGHT, and the icons are centered
11455 vertically in the new height.
11456
11457 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11458 count a final empty row in case the tool-bar width exactly matches
11459 the window width.
11460 */
11461
11462 static void
11463 display_tool_bar_line (struct it *it, int height)
11464 {
11465 struct glyph_row *row = it->glyph_row;
11466 int max_x = it->last_visible_x;
11467 struct glyph *last;
11468
11469 prepare_desired_row (row);
11470 row->y = it->current_y;
11471
11472 /* Note that this isn't made use of if the face hasn't a box,
11473 so there's no need to check the face here. */
11474 it->start_of_box_run_p = 1;
11475
11476 while (it->current_x < max_x)
11477 {
11478 int x, n_glyphs_before, i, nglyphs;
11479 struct it it_before;
11480
11481 /* Get the next display element. */
11482 if (!get_next_display_element (it))
11483 {
11484 /* Don't count empty row if we are counting needed tool-bar lines. */
11485 if (height < 0 && !it->hpos)
11486 return;
11487 break;
11488 }
11489
11490 /* Produce glyphs. */
11491 n_glyphs_before = row->used[TEXT_AREA];
11492 it_before = *it;
11493
11494 PRODUCE_GLYPHS (it);
11495
11496 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11497 i = 0;
11498 x = it_before.current_x;
11499 while (i < nglyphs)
11500 {
11501 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11502
11503 if (x + glyph->pixel_width > max_x)
11504 {
11505 /* Glyph doesn't fit on line. Backtrack. */
11506 row->used[TEXT_AREA] = n_glyphs_before;
11507 *it = it_before;
11508 /* If this is the only glyph on this line, it will never fit on the
11509 tool-bar, so skip it. But ensure there is at least one glyph,
11510 so we don't accidentally disable the tool-bar. */
11511 if (n_glyphs_before == 0
11512 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11513 break;
11514 goto out;
11515 }
11516
11517 ++it->hpos;
11518 x += glyph->pixel_width;
11519 ++i;
11520 }
11521
11522 /* Stop at line end. */
11523 if (ITERATOR_AT_END_OF_LINE_P (it))
11524 break;
11525
11526 set_iterator_to_next (it, 1);
11527 }
11528
11529 out:;
11530
11531 row->displays_text_p = row->used[TEXT_AREA] != 0;
11532
11533 /* Use default face for the border below the tool bar.
11534
11535 FIXME: When auto-resize-tool-bars is grow-only, there is
11536 no additional border below the possibly empty tool-bar lines.
11537 So to make the extra empty lines look "normal", we have to
11538 use the tool-bar face for the border too. */
11539 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11540 it->face_id = DEFAULT_FACE_ID;
11541
11542 extend_face_to_end_of_line (it);
11543 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11544 last->right_box_line_p = 1;
11545 if (last == row->glyphs[TEXT_AREA])
11546 last->left_box_line_p = 1;
11547
11548 /* Make line the desired height and center it vertically. */
11549 if ((height -= it->max_ascent + it->max_descent) > 0)
11550 {
11551 /* Don't add more than one line height. */
11552 height %= FRAME_LINE_HEIGHT (it->f);
11553 it->max_ascent += height / 2;
11554 it->max_descent += (height + 1) / 2;
11555 }
11556
11557 compute_line_metrics (it);
11558
11559 /* If line is empty, make it occupy the rest of the tool-bar. */
11560 if (!row->displays_text_p)
11561 {
11562 row->height = row->phys_height = it->last_visible_y - row->y;
11563 row->visible_height = row->height;
11564 row->ascent = row->phys_ascent = 0;
11565 row->extra_line_spacing = 0;
11566 }
11567
11568 row->full_width_p = 1;
11569 row->continued_p = 0;
11570 row->truncated_on_left_p = 0;
11571 row->truncated_on_right_p = 0;
11572
11573 it->current_x = it->hpos = 0;
11574 it->current_y += row->height;
11575 ++it->vpos;
11576 ++it->glyph_row;
11577 }
11578
11579
11580 /* Max tool-bar height. */
11581
11582 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11583 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11584
11585 /* Value is the number of screen lines needed to make all tool-bar
11586 items of frame F visible. The number of actual rows needed is
11587 returned in *N_ROWS if non-NULL. */
11588
11589 static int
11590 tool_bar_lines_needed (struct frame *f, int *n_rows)
11591 {
11592 struct window *w = XWINDOW (f->tool_bar_window);
11593 struct it it;
11594 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11595 the desired matrix, so use (unused) mode-line row as temporary row to
11596 avoid destroying the first tool-bar row. */
11597 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11598
11599 /* Initialize an iterator for iteration over
11600 F->desired_tool_bar_string in the tool-bar window of frame F. */
11601 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11602 it.first_visible_x = 0;
11603 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11604 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11605 it.paragraph_embedding = L2R;
11606
11607 while (!ITERATOR_AT_END_P (&it))
11608 {
11609 clear_glyph_row (temp_row);
11610 it.glyph_row = temp_row;
11611 display_tool_bar_line (&it, -1);
11612 }
11613 clear_glyph_row (temp_row);
11614
11615 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11616 if (n_rows)
11617 *n_rows = it.vpos > 0 ? it.vpos : -1;
11618
11619 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11620 }
11621
11622
11623 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11624 0, 1, 0,
11625 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11626 (Lisp_Object frame)
11627 {
11628 struct frame *f;
11629 struct window *w;
11630 int nlines = 0;
11631
11632 if (NILP (frame))
11633 frame = selected_frame;
11634 else
11635 CHECK_FRAME (frame);
11636 f = XFRAME (frame);
11637
11638 if (WINDOWP (f->tool_bar_window)
11639 && (w = XWINDOW (f->tool_bar_window),
11640 WINDOW_TOTAL_LINES (w) > 0))
11641 {
11642 update_tool_bar (f, 1);
11643 if (f->n_tool_bar_items)
11644 {
11645 build_desired_tool_bar_string (f);
11646 nlines = tool_bar_lines_needed (f, NULL);
11647 }
11648 }
11649
11650 return make_number (nlines);
11651 }
11652
11653
11654 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11655 height should be changed. */
11656
11657 static int
11658 redisplay_tool_bar (struct frame *f)
11659 {
11660 struct window *w;
11661 struct it it;
11662 struct glyph_row *row;
11663
11664 #if defined (USE_GTK) || defined (HAVE_NS)
11665 if (FRAME_EXTERNAL_TOOL_BAR (f))
11666 update_frame_tool_bar (f);
11667 return 0;
11668 #endif
11669
11670 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11671 do anything. This means you must start with tool-bar-lines
11672 non-zero to get the auto-sizing effect. Or in other words, you
11673 can turn off tool-bars by specifying tool-bar-lines zero. */
11674 if (!WINDOWP (f->tool_bar_window)
11675 || (w = XWINDOW (f->tool_bar_window),
11676 WINDOW_TOTAL_LINES (w) == 0))
11677 return 0;
11678
11679 /* Set up an iterator for the tool-bar window. */
11680 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11681 it.first_visible_x = 0;
11682 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11683 row = it.glyph_row;
11684
11685 /* Build a string that represents the contents of the tool-bar. */
11686 build_desired_tool_bar_string (f);
11687 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11688 /* FIXME: This should be controlled by a user option. But it
11689 doesn't make sense to have an R2L tool bar if the menu bar cannot
11690 be drawn also R2L, and making the menu bar R2L is tricky due
11691 toolkit-specific code that implements it. If an R2L tool bar is
11692 ever supported, display_tool_bar_line should also be augmented to
11693 call unproduce_glyphs like display_line and display_string
11694 do. */
11695 it.paragraph_embedding = L2R;
11696
11697 if (f->n_tool_bar_rows == 0)
11698 {
11699 int nlines;
11700
11701 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11702 nlines != WINDOW_TOTAL_LINES (w)))
11703 {
11704 Lisp_Object frame;
11705 int old_height = WINDOW_TOTAL_LINES (w);
11706
11707 XSETFRAME (frame, f);
11708 Fmodify_frame_parameters (frame,
11709 Fcons (Fcons (Qtool_bar_lines,
11710 make_number (nlines)),
11711 Qnil));
11712 if (WINDOW_TOTAL_LINES (w) != old_height)
11713 {
11714 clear_glyph_matrix (w->desired_matrix);
11715 fonts_changed_p = 1;
11716 return 1;
11717 }
11718 }
11719 }
11720
11721 /* Display as many lines as needed to display all tool-bar items. */
11722
11723 if (f->n_tool_bar_rows > 0)
11724 {
11725 int border, rows, height, extra;
11726
11727 if (INTEGERP (Vtool_bar_border))
11728 border = XINT (Vtool_bar_border);
11729 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11730 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11731 else if (EQ (Vtool_bar_border, Qborder_width))
11732 border = f->border_width;
11733 else
11734 border = 0;
11735 if (border < 0)
11736 border = 0;
11737
11738 rows = f->n_tool_bar_rows;
11739 height = max (1, (it.last_visible_y - border) / rows);
11740 extra = it.last_visible_y - border - height * rows;
11741
11742 while (it.current_y < it.last_visible_y)
11743 {
11744 int h = 0;
11745 if (extra > 0 && rows-- > 0)
11746 {
11747 h = (extra + rows - 1) / rows;
11748 extra -= h;
11749 }
11750 display_tool_bar_line (&it, height + h);
11751 }
11752 }
11753 else
11754 {
11755 while (it.current_y < it.last_visible_y)
11756 display_tool_bar_line (&it, 0);
11757 }
11758
11759 /* It doesn't make much sense to try scrolling in the tool-bar
11760 window, so don't do it. */
11761 w->desired_matrix->no_scrolling_p = 1;
11762 w->must_be_updated_p = 1;
11763
11764 if (!NILP (Vauto_resize_tool_bars))
11765 {
11766 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11767 int change_height_p = 0;
11768
11769 /* If we couldn't display everything, change the tool-bar's
11770 height if there is room for more. */
11771 if (IT_STRING_CHARPOS (it) < it.end_charpos
11772 && it.current_y < max_tool_bar_height)
11773 change_height_p = 1;
11774
11775 row = it.glyph_row - 1;
11776
11777 /* If there are blank lines at the end, except for a partially
11778 visible blank line at the end that is smaller than
11779 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11780 if (!row->displays_text_p
11781 && row->height >= FRAME_LINE_HEIGHT (f))
11782 change_height_p = 1;
11783
11784 /* If row displays tool-bar items, but is partially visible,
11785 change the tool-bar's height. */
11786 if (row->displays_text_p
11787 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11788 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11789 change_height_p = 1;
11790
11791 /* Resize windows as needed by changing the `tool-bar-lines'
11792 frame parameter. */
11793 if (change_height_p)
11794 {
11795 Lisp_Object frame;
11796 int old_height = WINDOW_TOTAL_LINES (w);
11797 int nrows;
11798 int nlines = tool_bar_lines_needed (f, &nrows);
11799
11800 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11801 && !f->minimize_tool_bar_window_p)
11802 ? (nlines > old_height)
11803 : (nlines != old_height));
11804 f->minimize_tool_bar_window_p = 0;
11805
11806 if (change_height_p)
11807 {
11808 XSETFRAME (frame, f);
11809 Fmodify_frame_parameters (frame,
11810 Fcons (Fcons (Qtool_bar_lines,
11811 make_number (nlines)),
11812 Qnil));
11813 if (WINDOW_TOTAL_LINES (w) != old_height)
11814 {
11815 clear_glyph_matrix (w->desired_matrix);
11816 f->n_tool_bar_rows = nrows;
11817 fonts_changed_p = 1;
11818 return 1;
11819 }
11820 }
11821 }
11822 }
11823
11824 f->minimize_tool_bar_window_p = 0;
11825 return 0;
11826 }
11827
11828
11829 /* Get information about the tool-bar item which is displayed in GLYPH
11830 on frame F. Return in *PROP_IDX the index where tool-bar item
11831 properties start in F->tool_bar_items. Value is zero if
11832 GLYPH doesn't display a tool-bar item. */
11833
11834 static int
11835 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11836 {
11837 Lisp_Object prop;
11838 int success_p;
11839 int charpos;
11840
11841 /* This function can be called asynchronously, which means we must
11842 exclude any possibility that Fget_text_property signals an
11843 error. */
11844 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11845 charpos = max (0, charpos);
11846
11847 /* Get the text property `menu-item' at pos. The value of that
11848 property is the start index of this item's properties in
11849 F->tool_bar_items. */
11850 prop = Fget_text_property (make_number (charpos),
11851 Qmenu_item, f->current_tool_bar_string);
11852 if (INTEGERP (prop))
11853 {
11854 *prop_idx = XINT (prop);
11855 success_p = 1;
11856 }
11857 else
11858 success_p = 0;
11859
11860 return success_p;
11861 }
11862
11863 \f
11864 /* Get information about the tool-bar item at position X/Y on frame F.
11865 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11866 the current matrix of the tool-bar window of F, or NULL if not
11867 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11868 item in F->tool_bar_items. Value is
11869
11870 -1 if X/Y is not on a tool-bar item
11871 0 if X/Y is on the same item that was highlighted before.
11872 1 otherwise. */
11873
11874 static int
11875 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11876 int *hpos, int *vpos, int *prop_idx)
11877 {
11878 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11879 struct window *w = XWINDOW (f->tool_bar_window);
11880 int area;
11881
11882 /* Find the glyph under X/Y. */
11883 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11884 if (*glyph == NULL)
11885 return -1;
11886
11887 /* Get the start of this tool-bar item's properties in
11888 f->tool_bar_items. */
11889 if (!tool_bar_item_info (f, *glyph, prop_idx))
11890 return -1;
11891
11892 /* Is mouse on the highlighted item? */
11893 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11894 && *vpos >= hlinfo->mouse_face_beg_row
11895 && *vpos <= hlinfo->mouse_face_end_row
11896 && (*vpos > hlinfo->mouse_face_beg_row
11897 || *hpos >= hlinfo->mouse_face_beg_col)
11898 && (*vpos < hlinfo->mouse_face_end_row
11899 || *hpos < hlinfo->mouse_face_end_col
11900 || hlinfo->mouse_face_past_end))
11901 return 0;
11902
11903 return 1;
11904 }
11905
11906
11907 /* EXPORT:
11908 Handle mouse button event on the tool-bar of frame F, at
11909 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11910 0 for button release. MODIFIERS is event modifiers for button
11911 release. */
11912
11913 void
11914 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11915 unsigned int modifiers)
11916 {
11917 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11918 struct window *w = XWINDOW (f->tool_bar_window);
11919 int hpos, vpos, prop_idx;
11920 struct glyph *glyph;
11921 Lisp_Object enabled_p;
11922
11923 /* If not on the highlighted tool-bar item, return. */
11924 frame_to_window_pixel_xy (w, &x, &y);
11925 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11926 return;
11927
11928 /* If item is disabled, do nothing. */
11929 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11930 if (NILP (enabled_p))
11931 return;
11932
11933 if (down_p)
11934 {
11935 /* Show item in pressed state. */
11936 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11937 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11938 last_tool_bar_item = prop_idx;
11939 }
11940 else
11941 {
11942 Lisp_Object key, frame;
11943 struct input_event event;
11944 EVENT_INIT (event);
11945
11946 /* Show item in released state. */
11947 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11948 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11949
11950 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11951
11952 XSETFRAME (frame, f);
11953 event.kind = TOOL_BAR_EVENT;
11954 event.frame_or_window = frame;
11955 event.arg = frame;
11956 kbd_buffer_store_event (&event);
11957
11958 event.kind = TOOL_BAR_EVENT;
11959 event.frame_or_window = frame;
11960 event.arg = key;
11961 event.modifiers = modifiers;
11962 kbd_buffer_store_event (&event);
11963 last_tool_bar_item = -1;
11964 }
11965 }
11966
11967
11968 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11969 tool-bar window-relative coordinates X/Y. Called from
11970 note_mouse_highlight. */
11971
11972 static void
11973 note_tool_bar_highlight (struct frame *f, int x, int y)
11974 {
11975 Lisp_Object window = f->tool_bar_window;
11976 struct window *w = XWINDOW (window);
11977 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11978 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11979 int hpos, vpos;
11980 struct glyph *glyph;
11981 struct glyph_row *row;
11982 int i;
11983 Lisp_Object enabled_p;
11984 int prop_idx;
11985 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11986 int mouse_down_p, rc;
11987
11988 /* Function note_mouse_highlight is called with negative X/Y
11989 values when mouse moves outside of the frame. */
11990 if (x <= 0 || y <= 0)
11991 {
11992 clear_mouse_face (hlinfo);
11993 return;
11994 }
11995
11996 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11997 if (rc < 0)
11998 {
11999 /* Not on tool-bar item. */
12000 clear_mouse_face (hlinfo);
12001 return;
12002 }
12003 else if (rc == 0)
12004 /* On same tool-bar item as before. */
12005 goto set_help_echo;
12006
12007 clear_mouse_face (hlinfo);
12008
12009 /* Mouse is down, but on different tool-bar item? */
12010 mouse_down_p = (dpyinfo->grabbed
12011 && f == last_mouse_frame
12012 && FRAME_LIVE_P (f));
12013 if (mouse_down_p
12014 && last_tool_bar_item != prop_idx)
12015 return;
12016
12017 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12018 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12019
12020 /* If tool-bar item is not enabled, don't highlight it. */
12021 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12022 if (!NILP (enabled_p))
12023 {
12024 /* Compute the x-position of the glyph. In front and past the
12025 image is a space. We include this in the highlighted area. */
12026 row = MATRIX_ROW (w->current_matrix, vpos);
12027 for (i = x = 0; i < hpos; ++i)
12028 x += row->glyphs[TEXT_AREA][i].pixel_width;
12029
12030 /* Record this as the current active region. */
12031 hlinfo->mouse_face_beg_col = hpos;
12032 hlinfo->mouse_face_beg_row = vpos;
12033 hlinfo->mouse_face_beg_x = x;
12034 hlinfo->mouse_face_beg_y = row->y;
12035 hlinfo->mouse_face_past_end = 0;
12036
12037 hlinfo->mouse_face_end_col = hpos + 1;
12038 hlinfo->mouse_face_end_row = vpos;
12039 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12040 hlinfo->mouse_face_end_y = row->y;
12041 hlinfo->mouse_face_window = window;
12042 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12043
12044 /* Display it as active. */
12045 show_mouse_face (hlinfo, draw);
12046 hlinfo->mouse_face_image_state = draw;
12047 }
12048
12049 set_help_echo:
12050
12051 /* Set help_echo_string to a help string to display for this tool-bar item.
12052 XTread_socket does the rest. */
12053 help_echo_object = help_echo_window = Qnil;
12054 help_echo_pos = -1;
12055 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12056 if (NILP (help_echo_string))
12057 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12058 }
12059
12060 #endif /* HAVE_WINDOW_SYSTEM */
12061
12062
12063 \f
12064 /************************************************************************
12065 Horizontal scrolling
12066 ************************************************************************/
12067
12068 static int hscroll_window_tree (Lisp_Object);
12069 static int hscroll_windows (Lisp_Object);
12070
12071 /* For all leaf windows in the window tree rooted at WINDOW, set their
12072 hscroll value so that PT is (i) visible in the window, and (ii) so
12073 that it is not within a certain margin at the window's left and
12074 right border. Value is non-zero if any window's hscroll has been
12075 changed. */
12076
12077 static int
12078 hscroll_window_tree (Lisp_Object window)
12079 {
12080 int hscrolled_p = 0;
12081 int hscroll_relative_p = FLOATP (Vhscroll_step);
12082 int hscroll_step_abs = 0;
12083 double hscroll_step_rel = 0;
12084
12085 if (hscroll_relative_p)
12086 {
12087 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12088 if (hscroll_step_rel < 0)
12089 {
12090 hscroll_relative_p = 0;
12091 hscroll_step_abs = 0;
12092 }
12093 }
12094 else if (INTEGERP (Vhscroll_step))
12095 {
12096 hscroll_step_abs = XINT (Vhscroll_step);
12097 if (hscroll_step_abs < 0)
12098 hscroll_step_abs = 0;
12099 }
12100 else
12101 hscroll_step_abs = 0;
12102
12103 while (WINDOWP (window))
12104 {
12105 struct window *w = XWINDOW (window);
12106
12107 if (WINDOWP (w->hchild))
12108 hscrolled_p |= hscroll_window_tree (w->hchild);
12109 else if (WINDOWP (w->vchild))
12110 hscrolled_p |= hscroll_window_tree (w->vchild);
12111 else if (w->cursor.vpos >= 0)
12112 {
12113 int h_margin;
12114 int text_area_width;
12115 struct glyph_row *current_cursor_row
12116 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12117 struct glyph_row *desired_cursor_row
12118 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12119 struct glyph_row *cursor_row
12120 = (desired_cursor_row->enabled_p
12121 ? desired_cursor_row
12122 : current_cursor_row);
12123 int row_r2l_p = cursor_row->reversed_p;
12124
12125 text_area_width = window_box_width (w, TEXT_AREA);
12126
12127 /* Scroll when cursor is inside this scroll margin. */
12128 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12129
12130 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12131 /* For left-to-right rows, hscroll when cursor is either
12132 (i) inside the right hscroll margin, or (ii) if it is
12133 inside the left margin and the window is already
12134 hscrolled. */
12135 && ((!row_r2l_p
12136 && ((XFASTINT (w->hscroll)
12137 && w->cursor.x <= h_margin)
12138 || (cursor_row->enabled_p
12139 && cursor_row->truncated_on_right_p
12140 && (w->cursor.x >= text_area_width - h_margin))))
12141 /* For right-to-left rows, the logic is similar,
12142 except that rules for scrolling to left and right
12143 are reversed. E.g., if cursor.x <= h_margin, we
12144 need to hscroll "to the right" unconditionally,
12145 and that will scroll the screen to the left so as
12146 to reveal the next portion of the row. */
12147 || (row_r2l_p
12148 && ((cursor_row->enabled_p
12149 /* FIXME: It is confusing to set the
12150 truncated_on_right_p flag when R2L rows
12151 are actually truncated on the left. */
12152 && cursor_row->truncated_on_right_p
12153 && w->cursor.x <= h_margin)
12154 || (XFASTINT (w->hscroll)
12155 && (w->cursor.x >= text_area_width - h_margin))))))
12156 {
12157 struct it it;
12158 int hscroll;
12159 struct buffer *saved_current_buffer;
12160 EMACS_INT pt;
12161 int wanted_x;
12162
12163 /* Find point in a display of infinite width. */
12164 saved_current_buffer = current_buffer;
12165 current_buffer = XBUFFER (w->buffer);
12166
12167 if (w == XWINDOW (selected_window))
12168 pt = PT;
12169 else
12170 {
12171 pt = marker_position (w->pointm);
12172 pt = max (BEGV, pt);
12173 pt = min (ZV, pt);
12174 }
12175
12176 /* Move iterator to pt starting at cursor_row->start in
12177 a line with infinite width. */
12178 init_to_row_start (&it, w, cursor_row);
12179 it.last_visible_x = INFINITY;
12180 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12181 current_buffer = saved_current_buffer;
12182
12183 /* Position cursor in window. */
12184 if (!hscroll_relative_p && hscroll_step_abs == 0)
12185 hscroll = max (0, (it.current_x
12186 - (ITERATOR_AT_END_OF_LINE_P (&it)
12187 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12188 : (text_area_width / 2))))
12189 / FRAME_COLUMN_WIDTH (it.f);
12190 else if ((!row_r2l_p
12191 && w->cursor.x >= text_area_width - h_margin)
12192 || (row_r2l_p && w->cursor.x <= h_margin))
12193 {
12194 if (hscroll_relative_p)
12195 wanted_x = text_area_width * (1 - hscroll_step_rel)
12196 - h_margin;
12197 else
12198 wanted_x = text_area_width
12199 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12200 - h_margin;
12201 hscroll
12202 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12203 }
12204 else
12205 {
12206 if (hscroll_relative_p)
12207 wanted_x = text_area_width * hscroll_step_rel
12208 + h_margin;
12209 else
12210 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12211 + h_margin;
12212 hscroll
12213 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12214 }
12215 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12216
12217 /* Don't prevent redisplay optimizations if hscroll
12218 hasn't changed, as it will unnecessarily slow down
12219 redisplay. */
12220 if (XFASTINT (w->hscroll) != hscroll)
12221 {
12222 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12223 w->hscroll = make_number (hscroll);
12224 hscrolled_p = 1;
12225 }
12226 }
12227 }
12228
12229 window = w->next;
12230 }
12231
12232 /* Value is non-zero if hscroll of any leaf window has been changed. */
12233 return hscrolled_p;
12234 }
12235
12236
12237 /* Set hscroll so that cursor is visible and not inside horizontal
12238 scroll margins for all windows in the tree rooted at WINDOW. See
12239 also hscroll_window_tree above. Value is non-zero if any window's
12240 hscroll has been changed. If it has, desired matrices on the frame
12241 of WINDOW are cleared. */
12242
12243 static int
12244 hscroll_windows (Lisp_Object window)
12245 {
12246 int hscrolled_p = hscroll_window_tree (window);
12247 if (hscrolled_p)
12248 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12249 return hscrolled_p;
12250 }
12251
12252
12253 \f
12254 /************************************************************************
12255 Redisplay
12256 ************************************************************************/
12257
12258 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12259 to a non-zero value. This is sometimes handy to have in a debugger
12260 session. */
12261
12262 #if GLYPH_DEBUG
12263
12264 /* First and last unchanged row for try_window_id. */
12265
12266 static int debug_first_unchanged_at_end_vpos;
12267 static int debug_last_unchanged_at_beg_vpos;
12268
12269 /* Delta vpos and y. */
12270
12271 static int debug_dvpos, debug_dy;
12272
12273 /* Delta in characters and bytes for try_window_id. */
12274
12275 static EMACS_INT debug_delta, debug_delta_bytes;
12276
12277 /* Values of window_end_pos and window_end_vpos at the end of
12278 try_window_id. */
12279
12280 static EMACS_INT debug_end_vpos;
12281
12282 /* Append a string to W->desired_matrix->method. FMT is a printf
12283 format string. If trace_redisplay_p is non-zero also printf the
12284 resulting string to stderr. */
12285
12286 static void debug_method_add (struct window *, char const *, ...)
12287 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12288
12289 static void
12290 debug_method_add (struct window *w, char const *fmt, ...)
12291 {
12292 char buffer[512];
12293 char *method = w->desired_matrix->method;
12294 int len = strlen (method);
12295 int size = sizeof w->desired_matrix->method;
12296 int remaining = size - len - 1;
12297 va_list ap;
12298
12299 va_start (ap, fmt);
12300 vsprintf (buffer, fmt, ap);
12301 va_end (ap);
12302 if (len && remaining)
12303 {
12304 method[len] = '|';
12305 --remaining, ++len;
12306 }
12307
12308 strncpy (method + len, buffer, remaining);
12309
12310 if (trace_redisplay_p)
12311 fprintf (stderr, "%p (%s): %s\n",
12312 w,
12313 ((BUFFERP (w->buffer)
12314 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12315 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12316 : "no buffer"),
12317 buffer);
12318 }
12319
12320 #endif /* GLYPH_DEBUG */
12321
12322
12323 /* Value is non-zero if all changes in window W, which displays
12324 current_buffer, are in the text between START and END. START is a
12325 buffer position, END is given as a distance from Z. Used in
12326 redisplay_internal for display optimization. */
12327
12328 static inline int
12329 text_outside_line_unchanged_p (struct window *w,
12330 EMACS_INT start, EMACS_INT end)
12331 {
12332 int unchanged_p = 1;
12333
12334 /* If text or overlays have changed, see where. */
12335 if (XFASTINT (w->last_modified) < MODIFF
12336 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12337 {
12338 /* Gap in the line? */
12339 if (GPT < start || Z - GPT < end)
12340 unchanged_p = 0;
12341
12342 /* Changes start in front of the line, or end after it? */
12343 if (unchanged_p
12344 && (BEG_UNCHANGED < start - 1
12345 || END_UNCHANGED < end))
12346 unchanged_p = 0;
12347
12348 /* If selective display, can't optimize if changes start at the
12349 beginning of the line. */
12350 if (unchanged_p
12351 && INTEGERP (BVAR (current_buffer, selective_display))
12352 && XINT (BVAR (current_buffer, selective_display)) > 0
12353 && (BEG_UNCHANGED < start || GPT <= start))
12354 unchanged_p = 0;
12355
12356 /* If there are overlays at the start or end of the line, these
12357 may have overlay strings with newlines in them. A change at
12358 START, for instance, may actually concern the display of such
12359 overlay strings as well, and they are displayed on different
12360 lines. So, quickly rule out this case. (For the future, it
12361 might be desirable to implement something more telling than
12362 just BEG/END_UNCHANGED.) */
12363 if (unchanged_p)
12364 {
12365 if (BEG + BEG_UNCHANGED == start
12366 && overlay_touches_p (start))
12367 unchanged_p = 0;
12368 if (END_UNCHANGED == end
12369 && overlay_touches_p (Z - end))
12370 unchanged_p = 0;
12371 }
12372
12373 /* Under bidi reordering, adding or deleting a character in the
12374 beginning of a paragraph, before the first strong directional
12375 character, can change the base direction of the paragraph (unless
12376 the buffer specifies a fixed paragraph direction), which will
12377 require to redisplay the whole paragraph. It might be worthwhile
12378 to find the paragraph limits and widen the range of redisplayed
12379 lines to that, but for now just give up this optimization. */
12380 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12381 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12382 unchanged_p = 0;
12383 }
12384
12385 return unchanged_p;
12386 }
12387
12388
12389 /* Do a frame update, taking possible shortcuts into account. This is
12390 the main external entry point for redisplay.
12391
12392 If the last redisplay displayed an echo area message and that message
12393 is no longer requested, we clear the echo area or bring back the
12394 mini-buffer if that is in use. */
12395
12396 void
12397 redisplay (void)
12398 {
12399 redisplay_internal ();
12400 }
12401
12402
12403 static Lisp_Object
12404 overlay_arrow_string_or_property (Lisp_Object var)
12405 {
12406 Lisp_Object val;
12407
12408 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12409 return val;
12410
12411 return Voverlay_arrow_string;
12412 }
12413
12414 /* Return 1 if there are any overlay-arrows in current_buffer. */
12415 static int
12416 overlay_arrow_in_current_buffer_p (void)
12417 {
12418 Lisp_Object vlist;
12419
12420 for (vlist = Voverlay_arrow_variable_list;
12421 CONSP (vlist);
12422 vlist = XCDR (vlist))
12423 {
12424 Lisp_Object var = XCAR (vlist);
12425 Lisp_Object val;
12426
12427 if (!SYMBOLP (var))
12428 continue;
12429 val = find_symbol_value (var);
12430 if (MARKERP (val)
12431 && current_buffer == XMARKER (val)->buffer)
12432 return 1;
12433 }
12434 return 0;
12435 }
12436
12437
12438 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12439 has changed. */
12440
12441 static int
12442 overlay_arrows_changed_p (void)
12443 {
12444 Lisp_Object vlist;
12445
12446 for (vlist = Voverlay_arrow_variable_list;
12447 CONSP (vlist);
12448 vlist = XCDR (vlist))
12449 {
12450 Lisp_Object var = XCAR (vlist);
12451 Lisp_Object val, pstr;
12452
12453 if (!SYMBOLP (var))
12454 continue;
12455 val = find_symbol_value (var);
12456 if (!MARKERP (val))
12457 continue;
12458 if (! EQ (COERCE_MARKER (val),
12459 Fget (var, Qlast_arrow_position))
12460 || ! (pstr = overlay_arrow_string_or_property (var),
12461 EQ (pstr, Fget (var, Qlast_arrow_string))))
12462 return 1;
12463 }
12464 return 0;
12465 }
12466
12467 /* Mark overlay arrows to be updated on next redisplay. */
12468
12469 static void
12470 update_overlay_arrows (int up_to_date)
12471 {
12472 Lisp_Object vlist;
12473
12474 for (vlist = Voverlay_arrow_variable_list;
12475 CONSP (vlist);
12476 vlist = XCDR (vlist))
12477 {
12478 Lisp_Object var = XCAR (vlist);
12479
12480 if (!SYMBOLP (var))
12481 continue;
12482
12483 if (up_to_date > 0)
12484 {
12485 Lisp_Object val = find_symbol_value (var);
12486 Fput (var, Qlast_arrow_position,
12487 COERCE_MARKER (val));
12488 Fput (var, Qlast_arrow_string,
12489 overlay_arrow_string_or_property (var));
12490 }
12491 else if (up_to_date < 0
12492 || !NILP (Fget (var, Qlast_arrow_position)))
12493 {
12494 Fput (var, Qlast_arrow_position, Qt);
12495 Fput (var, Qlast_arrow_string, Qt);
12496 }
12497 }
12498 }
12499
12500
12501 /* Return overlay arrow string to display at row.
12502 Return integer (bitmap number) for arrow bitmap in left fringe.
12503 Return nil if no overlay arrow. */
12504
12505 static Lisp_Object
12506 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12507 {
12508 Lisp_Object vlist;
12509
12510 for (vlist = Voverlay_arrow_variable_list;
12511 CONSP (vlist);
12512 vlist = XCDR (vlist))
12513 {
12514 Lisp_Object var = XCAR (vlist);
12515 Lisp_Object val;
12516
12517 if (!SYMBOLP (var))
12518 continue;
12519
12520 val = find_symbol_value (var);
12521
12522 if (MARKERP (val)
12523 && current_buffer == XMARKER (val)->buffer
12524 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12525 {
12526 if (FRAME_WINDOW_P (it->f)
12527 /* FIXME: if ROW->reversed_p is set, this should test
12528 the right fringe, not the left one. */
12529 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12530 {
12531 #ifdef HAVE_WINDOW_SYSTEM
12532 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12533 {
12534 int fringe_bitmap;
12535 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12536 return make_number (fringe_bitmap);
12537 }
12538 #endif
12539 return make_number (-1); /* Use default arrow bitmap */
12540 }
12541 return overlay_arrow_string_or_property (var);
12542 }
12543 }
12544
12545 return Qnil;
12546 }
12547
12548 /* Return 1 if point moved out of or into a composition. Otherwise
12549 return 0. PREV_BUF and PREV_PT are the last point buffer and
12550 position. BUF and PT are the current point buffer and position. */
12551
12552 static int
12553 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12554 struct buffer *buf, EMACS_INT pt)
12555 {
12556 EMACS_INT start, end;
12557 Lisp_Object prop;
12558 Lisp_Object buffer;
12559
12560 XSETBUFFER (buffer, buf);
12561 /* Check a composition at the last point if point moved within the
12562 same buffer. */
12563 if (prev_buf == buf)
12564 {
12565 if (prev_pt == pt)
12566 /* Point didn't move. */
12567 return 0;
12568
12569 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12570 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12571 && COMPOSITION_VALID_P (start, end, prop)
12572 && start < prev_pt && end > prev_pt)
12573 /* The last point was within the composition. Return 1 iff
12574 point moved out of the composition. */
12575 return (pt <= start || pt >= end);
12576 }
12577
12578 /* Check a composition at the current point. */
12579 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12580 && find_composition (pt, -1, &start, &end, &prop, buffer)
12581 && COMPOSITION_VALID_P (start, end, prop)
12582 && start < pt && end > pt);
12583 }
12584
12585
12586 /* Reconsider the setting of B->clip_changed which is displayed
12587 in window W. */
12588
12589 static inline void
12590 reconsider_clip_changes (struct window *w, struct buffer *b)
12591 {
12592 if (b->clip_changed
12593 && !NILP (w->window_end_valid)
12594 && w->current_matrix->buffer == b
12595 && w->current_matrix->zv == BUF_ZV (b)
12596 && w->current_matrix->begv == BUF_BEGV (b))
12597 b->clip_changed = 0;
12598
12599 /* If display wasn't paused, and W is not a tool bar window, see if
12600 point has been moved into or out of a composition. In that case,
12601 we set b->clip_changed to 1 to force updating the screen. If
12602 b->clip_changed has already been set to 1, we can skip this
12603 check. */
12604 if (!b->clip_changed
12605 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12606 {
12607 EMACS_INT pt;
12608
12609 if (w == XWINDOW (selected_window))
12610 pt = PT;
12611 else
12612 pt = marker_position (w->pointm);
12613
12614 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12615 || pt != XINT (w->last_point))
12616 && check_point_in_composition (w->current_matrix->buffer,
12617 XINT (w->last_point),
12618 XBUFFER (w->buffer), pt))
12619 b->clip_changed = 1;
12620 }
12621 }
12622 \f
12623
12624 /* Select FRAME to forward the values of frame-local variables into C
12625 variables so that the redisplay routines can access those values
12626 directly. */
12627
12628 static void
12629 select_frame_for_redisplay (Lisp_Object frame)
12630 {
12631 Lisp_Object tail, tem;
12632 Lisp_Object old = selected_frame;
12633 struct Lisp_Symbol *sym;
12634
12635 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12636
12637 selected_frame = frame;
12638
12639 do {
12640 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12641 if (CONSP (XCAR (tail))
12642 && (tem = XCAR (XCAR (tail)),
12643 SYMBOLP (tem))
12644 && (sym = indirect_variable (XSYMBOL (tem)),
12645 sym->redirect == SYMBOL_LOCALIZED)
12646 && sym->val.blv->frame_local)
12647 /* Use find_symbol_value rather than Fsymbol_value
12648 to avoid an error if it is void. */
12649 find_symbol_value (tem);
12650 } while (!EQ (frame, old) && (frame = old, 1));
12651 }
12652
12653
12654 #define STOP_POLLING \
12655 do { if (! polling_stopped_here) stop_polling (); \
12656 polling_stopped_here = 1; } while (0)
12657
12658 #define RESUME_POLLING \
12659 do { if (polling_stopped_here) start_polling (); \
12660 polling_stopped_here = 0; } while (0)
12661
12662
12663 /* Perhaps in the future avoid recentering windows if it
12664 is not necessary; currently that causes some problems. */
12665
12666 static void
12667 redisplay_internal (void)
12668 {
12669 struct window *w = XWINDOW (selected_window);
12670 struct window *sw;
12671 struct frame *fr;
12672 int pending;
12673 int must_finish = 0;
12674 struct text_pos tlbufpos, tlendpos;
12675 int number_of_visible_frames;
12676 int count, count1;
12677 struct frame *sf;
12678 int polling_stopped_here = 0;
12679 Lisp_Object old_frame = selected_frame;
12680
12681 /* Non-zero means redisplay has to consider all windows on all
12682 frames. Zero means, only selected_window is considered. */
12683 int consider_all_windows_p;
12684
12685 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12686
12687 /* No redisplay if running in batch mode or frame is not yet fully
12688 initialized, or redisplay is explicitly turned off by setting
12689 Vinhibit_redisplay. */
12690 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12691 || !NILP (Vinhibit_redisplay))
12692 return;
12693
12694 /* Don't examine these until after testing Vinhibit_redisplay.
12695 When Emacs is shutting down, perhaps because its connection to
12696 X has dropped, we should not look at them at all. */
12697 fr = XFRAME (w->frame);
12698 sf = SELECTED_FRAME ();
12699
12700 if (!fr->glyphs_initialized_p)
12701 return;
12702
12703 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12704 if (popup_activated ())
12705 return;
12706 #endif
12707
12708 /* I don't think this happens but let's be paranoid. */
12709 if (redisplaying_p)
12710 return;
12711
12712 /* Record a function that resets redisplaying_p to its old value
12713 when we leave this function. */
12714 count = SPECPDL_INDEX ();
12715 record_unwind_protect (unwind_redisplay,
12716 Fcons (make_number (redisplaying_p), selected_frame));
12717 ++redisplaying_p;
12718 specbind (Qinhibit_free_realized_faces, Qnil);
12719
12720 {
12721 Lisp_Object tail, frame;
12722
12723 FOR_EACH_FRAME (tail, frame)
12724 {
12725 struct frame *f = XFRAME (frame);
12726 f->already_hscrolled_p = 0;
12727 }
12728 }
12729
12730 retry:
12731 /* Remember the currently selected window. */
12732 sw = w;
12733
12734 if (!EQ (old_frame, selected_frame)
12735 && FRAME_LIVE_P (XFRAME (old_frame)))
12736 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12737 selected_frame and selected_window to be temporarily out-of-sync so
12738 when we come back here via `goto retry', we need to resync because we
12739 may need to run Elisp code (via prepare_menu_bars). */
12740 select_frame_for_redisplay (old_frame);
12741
12742 pending = 0;
12743 reconsider_clip_changes (w, current_buffer);
12744 last_escape_glyph_frame = NULL;
12745 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12746 last_glyphless_glyph_frame = NULL;
12747 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12748
12749 /* If new fonts have been loaded that make a glyph matrix adjustment
12750 necessary, do it. */
12751 if (fonts_changed_p)
12752 {
12753 adjust_glyphs (NULL);
12754 ++windows_or_buffers_changed;
12755 fonts_changed_p = 0;
12756 }
12757
12758 /* If face_change_count is non-zero, init_iterator will free all
12759 realized faces, which includes the faces referenced from current
12760 matrices. So, we can't reuse current matrices in this case. */
12761 if (face_change_count)
12762 ++windows_or_buffers_changed;
12763
12764 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12765 && FRAME_TTY (sf)->previous_frame != sf)
12766 {
12767 /* Since frames on a single ASCII terminal share the same
12768 display area, displaying a different frame means redisplay
12769 the whole thing. */
12770 windows_or_buffers_changed++;
12771 SET_FRAME_GARBAGED (sf);
12772 #ifndef DOS_NT
12773 set_tty_color_mode (FRAME_TTY (sf), sf);
12774 #endif
12775 FRAME_TTY (sf)->previous_frame = sf;
12776 }
12777
12778 /* Set the visible flags for all frames. Do this before checking
12779 for resized or garbaged frames; they want to know if their frames
12780 are visible. See the comment in frame.h for
12781 FRAME_SAMPLE_VISIBILITY. */
12782 {
12783 Lisp_Object tail, frame;
12784
12785 number_of_visible_frames = 0;
12786
12787 FOR_EACH_FRAME (tail, frame)
12788 {
12789 struct frame *f = XFRAME (frame);
12790
12791 FRAME_SAMPLE_VISIBILITY (f);
12792 if (FRAME_VISIBLE_P (f))
12793 ++number_of_visible_frames;
12794 clear_desired_matrices (f);
12795 }
12796 }
12797
12798 /* Notice any pending interrupt request to change frame size. */
12799 do_pending_window_change (1);
12800
12801 /* do_pending_window_change could change the selected_window due to
12802 frame resizing which makes the selected window too small. */
12803 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12804 {
12805 sw = w;
12806 reconsider_clip_changes (w, current_buffer);
12807 }
12808
12809 /* Clear frames marked as garbaged. */
12810 if (frame_garbaged)
12811 clear_garbaged_frames ();
12812
12813 /* Build menubar and tool-bar items. */
12814 if (NILP (Vmemory_full))
12815 prepare_menu_bars ();
12816
12817 if (windows_or_buffers_changed)
12818 update_mode_lines++;
12819
12820 /* Detect case that we need to write or remove a star in the mode line. */
12821 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12822 {
12823 w->update_mode_line = Qt;
12824 if (buffer_shared > 1)
12825 update_mode_lines++;
12826 }
12827
12828 /* Avoid invocation of point motion hooks by `current_column' below. */
12829 count1 = SPECPDL_INDEX ();
12830 specbind (Qinhibit_point_motion_hooks, Qt);
12831
12832 /* If %c is in the mode line, update it if needed. */
12833 if (!NILP (w->column_number_displayed)
12834 /* This alternative quickly identifies a common case
12835 where no change is needed. */
12836 && !(PT == XFASTINT (w->last_point)
12837 && XFASTINT (w->last_modified) >= MODIFF
12838 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12839 && (XFASTINT (w->column_number_displayed) != current_column ()))
12840 w->update_mode_line = Qt;
12841
12842 unbind_to (count1, Qnil);
12843
12844 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12845
12846 /* The variable buffer_shared is set in redisplay_window and
12847 indicates that we redisplay a buffer in different windows. See
12848 there. */
12849 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12850 || cursor_type_changed);
12851
12852 /* If specs for an arrow have changed, do thorough redisplay
12853 to ensure we remove any arrow that should no longer exist. */
12854 if (overlay_arrows_changed_p ())
12855 consider_all_windows_p = windows_or_buffers_changed = 1;
12856
12857 /* Normally the message* functions will have already displayed and
12858 updated the echo area, but the frame may have been trashed, or
12859 the update may have been preempted, so display the echo area
12860 again here. Checking message_cleared_p captures the case that
12861 the echo area should be cleared. */
12862 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12863 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12864 || (message_cleared_p
12865 && minibuf_level == 0
12866 /* If the mini-window is currently selected, this means the
12867 echo-area doesn't show through. */
12868 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12869 {
12870 int window_height_changed_p = echo_area_display (0);
12871 must_finish = 1;
12872
12873 /* If we don't display the current message, don't clear the
12874 message_cleared_p flag, because, if we did, we wouldn't clear
12875 the echo area in the next redisplay which doesn't preserve
12876 the echo area. */
12877 if (!display_last_displayed_message_p)
12878 message_cleared_p = 0;
12879
12880 if (fonts_changed_p)
12881 goto retry;
12882 else if (window_height_changed_p)
12883 {
12884 consider_all_windows_p = 1;
12885 ++update_mode_lines;
12886 ++windows_or_buffers_changed;
12887
12888 /* If window configuration was changed, frames may have been
12889 marked garbaged. Clear them or we will experience
12890 surprises wrt scrolling. */
12891 if (frame_garbaged)
12892 clear_garbaged_frames ();
12893 }
12894 }
12895 else if (EQ (selected_window, minibuf_window)
12896 && (current_buffer->clip_changed
12897 || XFASTINT (w->last_modified) < MODIFF
12898 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12899 && resize_mini_window (w, 0))
12900 {
12901 /* Resized active mini-window to fit the size of what it is
12902 showing if its contents might have changed. */
12903 must_finish = 1;
12904 /* FIXME: this causes all frames to be updated, which seems unnecessary
12905 since only the current frame needs to be considered. This function needs
12906 to be rewritten with two variables, consider_all_windows and
12907 consider_all_frames. */
12908 consider_all_windows_p = 1;
12909 ++windows_or_buffers_changed;
12910 ++update_mode_lines;
12911
12912 /* If window configuration was changed, frames may have been
12913 marked garbaged. Clear them or we will experience
12914 surprises wrt scrolling. */
12915 if (frame_garbaged)
12916 clear_garbaged_frames ();
12917 }
12918
12919
12920 /* If showing the region, and mark has changed, we must redisplay
12921 the whole window. The assignment to this_line_start_pos prevents
12922 the optimization directly below this if-statement. */
12923 if (((!NILP (Vtransient_mark_mode)
12924 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12925 != !NILP (w->region_showing))
12926 || (!NILP (w->region_showing)
12927 && !EQ (w->region_showing,
12928 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12929 CHARPOS (this_line_start_pos) = 0;
12930
12931 /* Optimize the case that only the line containing the cursor in the
12932 selected window has changed. Variables starting with this_ are
12933 set in display_line and record information about the line
12934 containing the cursor. */
12935 tlbufpos = this_line_start_pos;
12936 tlendpos = this_line_end_pos;
12937 if (!consider_all_windows_p
12938 && CHARPOS (tlbufpos) > 0
12939 && NILP (w->update_mode_line)
12940 && !current_buffer->clip_changed
12941 && !current_buffer->prevent_redisplay_optimizations_p
12942 && FRAME_VISIBLE_P (XFRAME (w->frame))
12943 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12944 /* Make sure recorded data applies to current buffer, etc. */
12945 && this_line_buffer == current_buffer
12946 && current_buffer == XBUFFER (w->buffer)
12947 && NILP (w->force_start)
12948 && NILP (w->optional_new_start)
12949 /* Point must be on the line that we have info recorded about. */
12950 && PT >= CHARPOS (tlbufpos)
12951 && PT <= Z - CHARPOS (tlendpos)
12952 /* All text outside that line, including its final newline,
12953 must be unchanged. */
12954 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12955 CHARPOS (tlendpos)))
12956 {
12957 if (CHARPOS (tlbufpos) > BEGV
12958 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12959 && (CHARPOS (tlbufpos) == ZV
12960 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12961 /* Former continuation line has disappeared by becoming empty. */
12962 goto cancel;
12963 else if (XFASTINT (w->last_modified) < MODIFF
12964 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12965 || MINI_WINDOW_P (w))
12966 {
12967 /* We have to handle the case of continuation around a
12968 wide-column character (see the comment in indent.c around
12969 line 1340).
12970
12971 For instance, in the following case:
12972
12973 -------- Insert --------
12974 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12975 J_I_ ==> J_I_ `^^' are cursors.
12976 ^^ ^^
12977 -------- --------
12978
12979 As we have to redraw the line above, we cannot use this
12980 optimization. */
12981
12982 struct it it;
12983 int line_height_before = this_line_pixel_height;
12984
12985 /* Note that start_display will handle the case that the
12986 line starting at tlbufpos is a continuation line. */
12987 start_display (&it, w, tlbufpos);
12988
12989 /* Implementation note: It this still necessary? */
12990 if (it.current_x != this_line_start_x)
12991 goto cancel;
12992
12993 TRACE ((stderr, "trying display optimization 1\n"));
12994 w->cursor.vpos = -1;
12995 overlay_arrow_seen = 0;
12996 it.vpos = this_line_vpos;
12997 it.current_y = this_line_y;
12998 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12999 display_line (&it);
13000
13001 /* If line contains point, is not continued,
13002 and ends at same distance from eob as before, we win. */
13003 if (w->cursor.vpos >= 0
13004 /* Line is not continued, otherwise this_line_start_pos
13005 would have been set to 0 in display_line. */
13006 && CHARPOS (this_line_start_pos)
13007 /* Line ends as before. */
13008 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13009 /* Line has same height as before. Otherwise other lines
13010 would have to be shifted up or down. */
13011 && this_line_pixel_height == line_height_before)
13012 {
13013 /* If this is not the window's last line, we must adjust
13014 the charstarts of the lines below. */
13015 if (it.current_y < it.last_visible_y)
13016 {
13017 struct glyph_row *row
13018 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13019 EMACS_INT delta, delta_bytes;
13020
13021 /* We used to distinguish between two cases here,
13022 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13023 when the line ends in a newline or the end of the
13024 buffer's accessible portion. But both cases did
13025 the same, so they were collapsed. */
13026 delta = (Z
13027 - CHARPOS (tlendpos)
13028 - MATRIX_ROW_START_CHARPOS (row));
13029 delta_bytes = (Z_BYTE
13030 - BYTEPOS (tlendpos)
13031 - MATRIX_ROW_START_BYTEPOS (row));
13032
13033 increment_matrix_positions (w->current_matrix,
13034 this_line_vpos + 1,
13035 w->current_matrix->nrows,
13036 delta, delta_bytes);
13037 }
13038
13039 /* If this row displays text now but previously didn't,
13040 or vice versa, w->window_end_vpos may have to be
13041 adjusted. */
13042 if ((it.glyph_row - 1)->displays_text_p)
13043 {
13044 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13045 XSETINT (w->window_end_vpos, this_line_vpos);
13046 }
13047 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13048 && this_line_vpos > 0)
13049 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13050 w->window_end_valid = Qnil;
13051
13052 /* Update hint: No need to try to scroll in update_window. */
13053 w->desired_matrix->no_scrolling_p = 1;
13054
13055 #if GLYPH_DEBUG
13056 *w->desired_matrix->method = 0;
13057 debug_method_add (w, "optimization 1");
13058 #endif
13059 #ifdef HAVE_WINDOW_SYSTEM
13060 update_window_fringes (w, 0);
13061 #endif
13062 goto update;
13063 }
13064 else
13065 goto cancel;
13066 }
13067 else if (/* Cursor position hasn't changed. */
13068 PT == XFASTINT (w->last_point)
13069 /* Make sure the cursor was last displayed
13070 in this window. Otherwise we have to reposition it. */
13071 && 0 <= w->cursor.vpos
13072 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13073 {
13074 if (!must_finish)
13075 {
13076 do_pending_window_change (1);
13077 /* If selected_window changed, redisplay again. */
13078 if (WINDOWP (selected_window)
13079 && (w = XWINDOW (selected_window)) != sw)
13080 goto retry;
13081
13082 /* We used to always goto end_of_redisplay here, but this
13083 isn't enough if we have a blinking cursor. */
13084 if (w->cursor_off_p == w->last_cursor_off_p)
13085 goto end_of_redisplay;
13086 }
13087 goto update;
13088 }
13089 /* If highlighting the region, or if the cursor is in the echo area,
13090 then we can't just move the cursor. */
13091 else if (! (!NILP (Vtransient_mark_mode)
13092 && !NILP (BVAR (current_buffer, mark_active)))
13093 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13094 || highlight_nonselected_windows)
13095 && NILP (w->region_showing)
13096 && NILP (Vshow_trailing_whitespace)
13097 && !cursor_in_echo_area)
13098 {
13099 struct it it;
13100 struct glyph_row *row;
13101
13102 /* Skip from tlbufpos to PT and see where it is. Note that
13103 PT may be in invisible text. If so, we will end at the
13104 next visible position. */
13105 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13106 NULL, DEFAULT_FACE_ID);
13107 it.current_x = this_line_start_x;
13108 it.current_y = this_line_y;
13109 it.vpos = this_line_vpos;
13110
13111 /* The call to move_it_to stops in front of PT, but
13112 moves over before-strings. */
13113 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13114
13115 if (it.vpos == this_line_vpos
13116 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13117 row->enabled_p))
13118 {
13119 xassert (this_line_vpos == it.vpos);
13120 xassert (this_line_y == it.current_y);
13121 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13122 #if GLYPH_DEBUG
13123 *w->desired_matrix->method = 0;
13124 debug_method_add (w, "optimization 3");
13125 #endif
13126 goto update;
13127 }
13128 else
13129 goto cancel;
13130 }
13131
13132 cancel:
13133 /* Text changed drastically or point moved off of line. */
13134 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13135 }
13136
13137 CHARPOS (this_line_start_pos) = 0;
13138 consider_all_windows_p |= buffer_shared > 1;
13139 ++clear_face_cache_count;
13140 #ifdef HAVE_WINDOW_SYSTEM
13141 ++clear_image_cache_count;
13142 #endif
13143
13144 /* Build desired matrices, and update the display. If
13145 consider_all_windows_p is non-zero, do it for all windows on all
13146 frames. Otherwise do it for selected_window, only. */
13147
13148 if (consider_all_windows_p)
13149 {
13150 Lisp_Object tail, frame;
13151
13152 FOR_EACH_FRAME (tail, frame)
13153 XFRAME (frame)->updated_p = 0;
13154
13155 /* Recompute # windows showing selected buffer. This will be
13156 incremented each time such a window is displayed. */
13157 buffer_shared = 0;
13158
13159 FOR_EACH_FRAME (tail, frame)
13160 {
13161 struct frame *f = XFRAME (frame);
13162
13163 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13164 {
13165 if (! EQ (frame, selected_frame))
13166 /* Select the frame, for the sake of frame-local
13167 variables. */
13168 select_frame_for_redisplay (frame);
13169
13170 /* Mark all the scroll bars to be removed; we'll redeem
13171 the ones we want when we redisplay their windows. */
13172 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13173 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13174
13175 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13176 redisplay_windows (FRAME_ROOT_WINDOW (f));
13177
13178 /* The X error handler may have deleted that frame. */
13179 if (!FRAME_LIVE_P (f))
13180 continue;
13181
13182 /* Any scroll bars which redisplay_windows should have
13183 nuked should now go away. */
13184 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13185 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13186
13187 /* If fonts changed, display again. */
13188 /* ??? rms: I suspect it is a mistake to jump all the way
13189 back to retry here. It should just retry this frame. */
13190 if (fonts_changed_p)
13191 goto retry;
13192
13193 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13194 {
13195 /* See if we have to hscroll. */
13196 if (!f->already_hscrolled_p)
13197 {
13198 f->already_hscrolled_p = 1;
13199 if (hscroll_windows (f->root_window))
13200 goto retry;
13201 }
13202
13203 /* Prevent various kinds of signals during display
13204 update. stdio is not robust about handling
13205 signals, which can cause an apparent I/O
13206 error. */
13207 if (interrupt_input)
13208 unrequest_sigio ();
13209 STOP_POLLING;
13210
13211 /* Update the display. */
13212 set_window_update_flags (XWINDOW (f->root_window), 1);
13213 pending |= update_frame (f, 0, 0);
13214 f->updated_p = 1;
13215 }
13216 }
13217 }
13218
13219 if (!EQ (old_frame, selected_frame)
13220 && FRAME_LIVE_P (XFRAME (old_frame)))
13221 /* We played a bit fast-and-loose above and allowed selected_frame
13222 and selected_window to be temporarily out-of-sync but let's make
13223 sure this stays contained. */
13224 select_frame_for_redisplay (old_frame);
13225 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13226
13227 if (!pending)
13228 {
13229 /* Do the mark_window_display_accurate after all windows have
13230 been redisplayed because this call resets flags in buffers
13231 which are needed for proper redisplay. */
13232 FOR_EACH_FRAME (tail, frame)
13233 {
13234 struct frame *f = XFRAME (frame);
13235 if (f->updated_p)
13236 {
13237 mark_window_display_accurate (f->root_window, 1);
13238 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13239 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13240 }
13241 }
13242 }
13243 }
13244 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13245 {
13246 Lisp_Object mini_window;
13247 struct frame *mini_frame;
13248
13249 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13250 /* Use list_of_error, not Qerror, so that
13251 we catch only errors and don't run the debugger. */
13252 internal_condition_case_1 (redisplay_window_1, selected_window,
13253 list_of_error,
13254 redisplay_window_error);
13255
13256 /* Compare desired and current matrices, perform output. */
13257
13258 update:
13259 /* If fonts changed, display again. */
13260 if (fonts_changed_p)
13261 goto retry;
13262
13263 /* Prevent various kinds of signals during display update.
13264 stdio is not robust about handling signals,
13265 which can cause an apparent I/O error. */
13266 if (interrupt_input)
13267 unrequest_sigio ();
13268 STOP_POLLING;
13269
13270 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13271 {
13272 if (hscroll_windows (selected_window))
13273 goto retry;
13274
13275 XWINDOW (selected_window)->must_be_updated_p = 1;
13276 pending = update_frame (sf, 0, 0);
13277 }
13278
13279 /* We may have called echo_area_display at the top of this
13280 function. If the echo area is on another frame, that may
13281 have put text on a frame other than the selected one, so the
13282 above call to update_frame would not have caught it. Catch
13283 it here. */
13284 mini_window = FRAME_MINIBUF_WINDOW (sf);
13285 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13286
13287 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13288 {
13289 XWINDOW (mini_window)->must_be_updated_p = 1;
13290 pending |= update_frame (mini_frame, 0, 0);
13291 if (!pending && hscroll_windows (mini_window))
13292 goto retry;
13293 }
13294 }
13295
13296 /* If display was paused because of pending input, make sure we do a
13297 thorough update the next time. */
13298 if (pending)
13299 {
13300 /* Prevent the optimization at the beginning of
13301 redisplay_internal that tries a single-line update of the
13302 line containing the cursor in the selected window. */
13303 CHARPOS (this_line_start_pos) = 0;
13304
13305 /* Let the overlay arrow be updated the next time. */
13306 update_overlay_arrows (0);
13307
13308 /* If we pause after scrolling, some rows in the current
13309 matrices of some windows are not valid. */
13310 if (!WINDOW_FULL_WIDTH_P (w)
13311 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13312 update_mode_lines = 1;
13313 }
13314 else
13315 {
13316 if (!consider_all_windows_p)
13317 {
13318 /* This has already been done above if
13319 consider_all_windows_p is set. */
13320 mark_window_display_accurate_1 (w, 1);
13321
13322 /* Say overlay arrows are up to date. */
13323 update_overlay_arrows (1);
13324
13325 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13326 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13327 }
13328
13329 update_mode_lines = 0;
13330 windows_or_buffers_changed = 0;
13331 cursor_type_changed = 0;
13332 }
13333
13334 /* Start SIGIO interrupts coming again. Having them off during the
13335 code above makes it less likely one will discard output, but not
13336 impossible, since there might be stuff in the system buffer here.
13337 But it is much hairier to try to do anything about that. */
13338 if (interrupt_input)
13339 request_sigio ();
13340 RESUME_POLLING;
13341
13342 /* If a frame has become visible which was not before, redisplay
13343 again, so that we display it. Expose events for such a frame
13344 (which it gets when becoming visible) don't call the parts of
13345 redisplay constructing glyphs, so simply exposing a frame won't
13346 display anything in this case. So, we have to display these
13347 frames here explicitly. */
13348 if (!pending)
13349 {
13350 Lisp_Object tail, frame;
13351 int new_count = 0;
13352
13353 FOR_EACH_FRAME (tail, frame)
13354 {
13355 int this_is_visible = 0;
13356
13357 if (XFRAME (frame)->visible)
13358 this_is_visible = 1;
13359 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13360 if (XFRAME (frame)->visible)
13361 this_is_visible = 1;
13362
13363 if (this_is_visible)
13364 new_count++;
13365 }
13366
13367 if (new_count != number_of_visible_frames)
13368 windows_or_buffers_changed++;
13369 }
13370
13371 /* Change frame size now if a change is pending. */
13372 do_pending_window_change (1);
13373
13374 /* If we just did a pending size change, or have additional
13375 visible frames, or selected_window changed, redisplay again. */
13376 if ((windows_or_buffers_changed && !pending)
13377 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13378 goto retry;
13379
13380 /* Clear the face and image caches.
13381
13382 We used to do this only if consider_all_windows_p. But the cache
13383 needs to be cleared if a timer creates images in the current
13384 buffer (e.g. the test case in Bug#6230). */
13385
13386 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13387 {
13388 clear_face_cache (0);
13389 clear_face_cache_count = 0;
13390 }
13391
13392 #ifdef HAVE_WINDOW_SYSTEM
13393 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13394 {
13395 clear_image_caches (Qnil);
13396 clear_image_cache_count = 0;
13397 }
13398 #endif /* HAVE_WINDOW_SYSTEM */
13399
13400 end_of_redisplay:
13401 unbind_to (count, Qnil);
13402 RESUME_POLLING;
13403 }
13404
13405
13406 /* Redisplay, but leave alone any recent echo area message unless
13407 another message has been requested in its place.
13408
13409 This is useful in situations where you need to redisplay but no
13410 user action has occurred, making it inappropriate for the message
13411 area to be cleared. See tracking_off and
13412 wait_reading_process_output for examples of these situations.
13413
13414 FROM_WHERE is an integer saying from where this function was
13415 called. This is useful for debugging. */
13416
13417 void
13418 redisplay_preserve_echo_area (int from_where)
13419 {
13420 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13421
13422 if (!NILP (echo_area_buffer[1]))
13423 {
13424 /* We have a previously displayed message, but no current
13425 message. Redisplay the previous message. */
13426 display_last_displayed_message_p = 1;
13427 redisplay_internal ();
13428 display_last_displayed_message_p = 0;
13429 }
13430 else
13431 redisplay_internal ();
13432
13433 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13434 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13435 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13436 }
13437
13438
13439 /* Function registered with record_unwind_protect in
13440 redisplay_internal. Reset redisplaying_p to the value it had
13441 before redisplay_internal was called, and clear
13442 prevent_freeing_realized_faces_p. It also selects the previously
13443 selected frame, unless it has been deleted (by an X connection
13444 failure during redisplay, for example). */
13445
13446 static Lisp_Object
13447 unwind_redisplay (Lisp_Object val)
13448 {
13449 Lisp_Object old_redisplaying_p, old_frame;
13450
13451 old_redisplaying_p = XCAR (val);
13452 redisplaying_p = XFASTINT (old_redisplaying_p);
13453 old_frame = XCDR (val);
13454 if (! EQ (old_frame, selected_frame)
13455 && FRAME_LIVE_P (XFRAME (old_frame)))
13456 select_frame_for_redisplay (old_frame);
13457 return Qnil;
13458 }
13459
13460
13461 /* Mark the display of window W as accurate or inaccurate. If
13462 ACCURATE_P is non-zero mark display of W as accurate. If
13463 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13464 redisplay_internal is called. */
13465
13466 static void
13467 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13468 {
13469 if (BUFFERP (w->buffer))
13470 {
13471 struct buffer *b = XBUFFER (w->buffer);
13472
13473 w->last_modified
13474 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13475 w->last_overlay_modified
13476 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13477 w->last_had_star
13478 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13479
13480 if (accurate_p)
13481 {
13482 b->clip_changed = 0;
13483 b->prevent_redisplay_optimizations_p = 0;
13484
13485 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13486 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13487 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13488 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13489
13490 w->current_matrix->buffer = b;
13491 w->current_matrix->begv = BUF_BEGV (b);
13492 w->current_matrix->zv = BUF_ZV (b);
13493
13494 w->last_cursor = w->cursor;
13495 w->last_cursor_off_p = w->cursor_off_p;
13496
13497 if (w == XWINDOW (selected_window))
13498 w->last_point = make_number (BUF_PT (b));
13499 else
13500 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13501 }
13502 }
13503
13504 if (accurate_p)
13505 {
13506 w->window_end_valid = w->buffer;
13507 w->update_mode_line = Qnil;
13508 }
13509 }
13510
13511
13512 /* Mark the display of windows in the window tree rooted at WINDOW as
13513 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13514 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13515 be redisplayed the next time redisplay_internal is called. */
13516
13517 void
13518 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13519 {
13520 struct window *w;
13521
13522 for (; !NILP (window); window = w->next)
13523 {
13524 w = XWINDOW (window);
13525 mark_window_display_accurate_1 (w, accurate_p);
13526
13527 if (!NILP (w->vchild))
13528 mark_window_display_accurate (w->vchild, accurate_p);
13529 if (!NILP (w->hchild))
13530 mark_window_display_accurate (w->hchild, accurate_p);
13531 }
13532
13533 if (accurate_p)
13534 {
13535 update_overlay_arrows (1);
13536 }
13537 else
13538 {
13539 /* Force a thorough redisplay the next time by setting
13540 last_arrow_position and last_arrow_string to t, which is
13541 unequal to any useful value of Voverlay_arrow_... */
13542 update_overlay_arrows (-1);
13543 }
13544 }
13545
13546
13547 /* Return value in display table DP (Lisp_Char_Table *) for character
13548 C. Since a display table doesn't have any parent, we don't have to
13549 follow parent. Do not call this function directly but use the
13550 macro DISP_CHAR_VECTOR. */
13551
13552 Lisp_Object
13553 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13554 {
13555 Lisp_Object val;
13556
13557 if (ASCII_CHAR_P (c))
13558 {
13559 val = dp->ascii;
13560 if (SUB_CHAR_TABLE_P (val))
13561 val = XSUB_CHAR_TABLE (val)->contents[c];
13562 }
13563 else
13564 {
13565 Lisp_Object table;
13566
13567 XSETCHAR_TABLE (table, dp);
13568 val = char_table_ref (table, c);
13569 }
13570 if (NILP (val))
13571 val = dp->defalt;
13572 return val;
13573 }
13574
13575
13576 \f
13577 /***********************************************************************
13578 Window Redisplay
13579 ***********************************************************************/
13580
13581 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13582
13583 static void
13584 redisplay_windows (Lisp_Object window)
13585 {
13586 while (!NILP (window))
13587 {
13588 struct window *w = XWINDOW (window);
13589
13590 if (!NILP (w->hchild))
13591 redisplay_windows (w->hchild);
13592 else if (!NILP (w->vchild))
13593 redisplay_windows (w->vchild);
13594 else if (!NILP (w->buffer))
13595 {
13596 displayed_buffer = XBUFFER (w->buffer);
13597 /* Use list_of_error, not Qerror, so that
13598 we catch only errors and don't run the debugger. */
13599 internal_condition_case_1 (redisplay_window_0, window,
13600 list_of_error,
13601 redisplay_window_error);
13602 }
13603
13604 window = w->next;
13605 }
13606 }
13607
13608 static Lisp_Object
13609 redisplay_window_error (Lisp_Object ignore)
13610 {
13611 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13612 return Qnil;
13613 }
13614
13615 static Lisp_Object
13616 redisplay_window_0 (Lisp_Object window)
13617 {
13618 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13619 redisplay_window (window, 0);
13620 return Qnil;
13621 }
13622
13623 static Lisp_Object
13624 redisplay_window_1 (Lisp_Object window)
13625 {
13626 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13627 redisplay_window (window, 1);
13628 return Qnil;
13629 }
13630 \f
13631
13632 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13633 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13634 which positions recorded in ROW differ from current buffer
13635 positions.
13636
13637 Return 0 if cursor is not on this row, 1 otherwise. */
13638
13639 static int
13640 set_cursor_from_row (struct window *w, struct glyph_row *row,
13641 struct glyph_matrix *matrix,
13642 EMACS_INT delta, EMACS_INT delta_bytes,
13643 int dy, int dvpos)
13644 {
13645 struct glyph *glyph = row->glyphs[TEXT_AREA];
13646 struct glyph *end = glyph + row->used[TEXT_AREA];
13647 struct glyph *cursor = NULL;
13648 /* The last known character position in row. */
13649 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13650 int x = row->x;
13651 EMACS_INT pt_old = PT - delta;
13652 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13653 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13654 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13655 /* A glyph beyond the edge of TEXT_AREA which we should never
13656 touch. */
13657 struct glyph *glyphs_end = end;
13658 /* Non-zero means we've found a match for cursor position, but that
13659 glyph has the avoid_cursor_p flag set. */
13660 int match_with_avoid_cursor = 0;
13661 /* Non-zero means we've seen at least one glyph that came from a
13662 display string. */
13663 int string_seen = 0;
13664 /* Largest and smallest buffer positions seen so far during scan of
13665 glyph row. */
13666 EMACS_INT bpos_max = pos_before;
13667 EMACS_INT bpos_min = pos_after;
13668 /* Last buffer position covered by an overlay string with an integer
13669 `cursor' property. */
13670 EMACS_INT bpos_covered = 0;
13671 /* Non-zero means the display string on which to display the cursor
13672 comes from a text property, not from an overlay. */
13673 int string_from_text_prop = 0;
13674
13675 /* Skip over glyphs not having an object at the start and the end of
13676 the row. These are special glyphs like truncation marks on
13677 terminal frames. */
13678 if (row->displays_text_p)
13679 {
13680 if (!row->reversed_p)
13681 {
13682 while (glyph < end
13683 && INTEGERP (glyph->object)
13684 && glyph->charpos < 0)
13685 {
13686 x += glyph->pixel_width;
13687 ++glyph;
13688 }
13689 while (end > glyph
13690 && INTEGERP ((end - 1)->object)
13691 /* CHARPOS is zero for blanks and stretch glyphs
13692 inserted by extend_face_to_end_of_line. */
13693 && (end - 1)->charpos <= 0)
13694 --end;
13695 glyph_before = glyph - 1;
13696 glyph_after = end;
13697 }
13698 else
13699 {
13700 struct glyph *g;
13701
13702 /* If the glyph row is reversed, we need to process it from back
13703 to front, so swap the edge pointers. */
13704 glyphs_end = end = glyph - 1;
13705 glyph += row->used[TEXT_AREA] - 1;
13706
13707 while (glyph > end + 1
13708 && INTEGERP (glyph->object)
13709 && glyph->charpos < 0)
13710 {
13711 --glyph;
13712 x -= glyph->pixel_width;
13713 }
13714 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13715 --glyph;
13716 /* By default, in reversed rows we put the cursor on the
13717 rightmost (first in the reading order) glyph. */
13718 for (g = end + 1; g < glyph; g++)
13719 x += g->pixel_width;
13720 while (end < glyph
13721 && INTEGERP ((end + 1)->object)
13722 && (end + 1)->charpos <= 0)
13723 ++end;
13724 glyph_before = glyph + 1;
13725 glyph_after = end;
13726 }
13727 }
13728 else if (row->reversed_p)
13729 {
13730 /* In R2L rows that don't display text, put the cursor on the
13731 rightmost glyph. Case in point: an empty last line that is
13732 part of an R2L paragraph. */
13733 cursor = end - 1;
13734 /* Avoid placing the cursor on the last glyph of the row, where
13735 on terminal frames we hold the vertical border between
13736 adjacent windows. */
13737 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13738 && !WINDOW_RIGHTMOST_P (w)
13739 && cursor == row->glyphs[LAST_AREA] - 1)
13740 cursor--;
13741 x = -1; /* will be computed below, at label compute_x */
13742 }
13743
13744 /* Step 1: Try to find the glyph whose character position
13745 corresponds to point. If that's not possible, find 2 glyphs
13746 whose character positions are the closest to point, one before
13747 point, the other after it. */
13748 if (!row->reversed_p)
13749 while (/* not marched to end of glyph row */
13750 glyph < end
13751 /* glyph was not inserted by redisplay for internal purposes */
13752 && !INTEGERP (glyph->object))
13753 {
13754 if (BUFFERP (glyph->object))
13755 {
13756 EMACS_INT dpos = glyph->charpos - pt_old;
13757
13758 if (glyph->charpos > bpos_max)
13759 bpos_max = glyph->charpos;
13760 if (glyph->charpos < bpos_min)
13761 bpos_min = glyph->charpos;
13762 if (!glyph->avoid_cursor_p)
13763 {
13764 /* If we hit point, we've found the glyph on which to
13765 display the cursor. */
13766 if (dpos == 0)
13767 {
13768 match_with_avoid_cursor = 0;
13769 break;
13770 }
13771 /* See if we've found a better approximation to
13772 POS_BEFORE or to POS_AFTER. Note that we want the
13773 first (leftmost) glyph of all those that are the
13774 closest from below, and the last (rightmost) of all
13775 those from above. */
13776 if (0 > dpos && dpos > pos_before - pt_old)
13777 {
13778 pos_before = glyph->charpos;
13779 glyph_before = glyph;
13780 }
13781 else if (0 < dpos && dpos <= pos_after - pt_old)
13782 {
13783 pos_after = glyph->charpos;
13784 glyph_after = glyph;
13785 }
13786 }
13787 else if (dpos == 0)
13788 match_with_avoid_cursor = 1;
13789 }
13790 else if (STRINGP (glyph->object))
13791 {
13792 Lisp_Object chprop;
13793 EMACS_INT glyph_pos = glyph->charpos;
13794
13795 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13796 glyph->object);
13797 if (INTEGERP (chprop))
13798 {
13799 bpos_covered = bpos_max + XINT (chprop);
13800 /* If the `cursor' property covers buffer positions up
13801 to and including point, we should display cursor on
13802 this glyph. Note that overlays and text properties
13803 with string values stop bidi reordering, so every
13804 buffer position to the left of the string is always
13805 smaller than any position to the right of the
13806 string. Therefore, if a `cursor' property on one
13807 of the string's characters has an integer value, we
13808 will break out of the loop below _before_ we get to
13809 the position match above. IOW, integer values of
13810 the `cursor' property override the "exact match for
13811 point" strategy of positioning the cursor. */
13812 /* Implementation note: bpos_max == pt_old when, e.g.,
13813 we are in an empty line, where bpos_max is set to
13814 MATRIX_ROW_START_CHARPOS, see above. */
13815 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13816 {
13817 cursor = glyph;
13818 break;
13819 }
13820 }
13821
13822 string_seen = 1;
13823 }
13824 x += glyph->pixel_width;
13825 ++glyph;
13826 }
13827 else if (glyph > end) /* row is reversed */
13828 while (!INTEGERP (glyph->object))
13829 {
13830 if (BUFFERP (glyph->object))
13831 {
13832 EMACS_INT dpos = glyph->charpos - pt_old;
13833
13834 if (glyph->charpos > bpos_max)
13835 bpos_max = glyph->charpos;
13836 if (glyph->charpos < bpos_min)
13837 bpos_min = glyph->charpos;
13838 if (!glyph->avoid_cursor_p)
13839 {
13840 if (dpos == 0)
13841 {
13842 match_with_avoid_cursor = 0;
13843 break;
13844 }
13845 if (0 > dpos && dpos > pos_before - pt_old)
13846 {
13847 pos_before = glyph->charpos;
13848 glyph_before = glyph;
13849 }
13850 else if (0 < dpos && dpos <= pos_after - pt_old)
13851 {
13852 pos_after = glyph->charpos;
13853 glyph_after = glyph;
13854 }
13855 }
13856 else if (dpos == 0)
13857 match_with_avoid_cursor = 1;
13858 }
13859 else if (STRINGP (glyph->object))
13860 {
13861 Lisp_Object chprop;
13862 EMACS_INT glyph_pos = glyph->charpos;
13863
13864 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13865 glyph->object);
13866 if (INTEGERP (chprop))
13867 {
13868 bpos_covered = bpos_max + XINT (chprop);
13869 /* If the `cursor' property covers buffer positions up
13870 to and including point, we should display cursor on
13871 this glyph. */
13872 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13873 {
13874 cursor = glyph;
13875 break;
13876 }
13877 }
13878 string_seen = 1;
13879 }
13880 --glyph;
13881 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13882 {
13883 x--; /* can't use any pixel_width */
13884 break;
13885 }
13886 x -= glyph->pixel_width;
13887 }
13888
13889 /* Step 2: If we didn't find an exact match for point, we need to
13890 look for a proper place to put the cursor among glyphs between
13891 GLYPH_BEFORE and GLYPH_AFTER. */
13892 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13893 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13894 && bpos_covered < pt_old)
13895 {
13896 /* An empty line has a single glyph whose OBJECT is zero and
13897 whose CHARPOS is the position of a newline on that line.
13898 Note that on a TTY, there are more glyphs after that, which
13899 were produced by extend_face_to_end_of_line, but their
13900 CHARPOS is zero or negative. */
13901 int empty_line_p =
13902 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13903 && INTEGERP (glyph->object) && glyph->charpos > 0;
13904
13905 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13906 {
13907 EMACS_INT ellipsis_pos;
13908
13909 /* Scan back over the ellipsis glyphs. */
13910 if (!row->reversed_p)
13911 {
13912 ellipsis_pos = (glyph - 1)->charpos;
13913 while (glyph > row->glyphs[TEXT_AREA]
13914 && (glyph - 1)->charpos == ellipsis_pos)
13915 glyph--, x -= glyph->pixel_width;
13916 /* That loop always goes one position too far, including
13917 the glyph before the ellipsis. So scan forward over
13918 that one. */
13919 x += glyph->pixel_width;
13920 glyph++;
13921 }
13922 else /* row is reversed */
13923 {
13924 ellipsis_pos = (glyph + 1)->charpos;
13925 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13926 && (glyph + 1)->charpos == ellipsis_pos)
13927 glyph++, x += glyph->pixel_width;
13928 x -= glyph->pixel_width;
13929 glyph--;
13930 }
13931 }
13932 else if (match_with_avoid_cursor)
13933 {
13934 cursor = glyph_after;
13935 x = -1;
13936 }
13937 else if (string_seen)
13938 {
13939 int incr = row->reversed_p ? -1 : +1;
13940
13941 /* Need to find the glyph that came out of a string which is
13942 present at point. That glyph is somewhere between
13943 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13944 positioned between POS_BEFORE and POS_AFTER in the
13945 buffer. */
13946 struct glyph *start, *stop;
13947 EMACS_INT pos = pos_before;
13948
13949 x = -1;
13950
13951 /* If the row ends in a newline from a display string,
13952 reordering could have moved the glyphs belonging to the
13953 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
13954 in this case we extend the search to the last glyph in
13955 the row that was not inserted by redisplay. */
13956 if (row->ends_in_newline_from_string_p)
13957 {
13958 glyph_after = end;
13959 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13960 }
13961
13962 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13963 correspond to POS_BEFORE and POS_AFTER, respectively. We
13964 need START and STOP in the order that corresponds to the
13965 row's direction as given by its reversed_p flag. If the
13966 directionality of characters between POS_BEFORE and
13967 POS_AFTER is the opposite of the row's base direction,
13968 these characters will have been reordered for display,
13969 and we need to reverse START and STOP. */
13970 if (!row->reversed_p)
13971 {
13972 start = min (glyph_before, glyph_after);
13973 stop = max (glyph_before, glyph_after);
13974 }
13975 else
13976 {
13977 start = max (glyph_before, glyph_after);
13978 stop = min (glyph_before, glyph_after);
13979 }
13980 for (glyph = start + incr;
13981 row->reversed_p ? glyph > stop : glyph < stop; )
13982 {
13983
13984 /* Any glyphs that come from the buffer are here because
13985 of bidi reordering. Skip them, and only pay
13986 attention to glyphs that came from some string. */
13987 if (STRINGP (glyph->object))
13988 {
13989 Lisp_Object str;
13990 EMACS_INT tem;
13991 /* If the display property covers the newline, we
13992 need to search for it one position farther. */
13993 EMACS_INT lim = pos_after
13994 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
13995
13996 string_from_text_prop = 0;
13997 str = glyph->object;
13998 tem = string_buffer_position_lim (str, pos, lim, 0);
13999 if (tem == 0 /* from overlay */
14000 || pos <= tem)
14001 {
14002 /* If the string from which this glyph came is
14003 found in the buffer at point, then we've
14004 found the glyph we've been looking for. If
14005 it comes from an overlay (tem == 0), and it
14006 has the `cursor' property on one of its
14007 glyphs, record that glyph as a candidate for
14008 displaying the cursor. (As in the
14009 unidirectional version, we will display the
14010 cursor on the last candidate we find.) */
14011 if (tem == 0 || tem == pt_old)
14012 {
14013 /* The glyphs from this string could have
14014 been reordered. Find the one with the
14015 smallest string position. Or there could
14016 be a character in the string with the
14017 `cursor' property, which means display
14018 cursor on that character's glyph. */
14019 EMACS_INT strpos = glyph->charpos;
14020
14021 if (tem)
14022 {
14023 cursor = glyph;
14024 string_from_text_prop = 1;
14025 }
14026 for ( ;
14027 (row->reversed_p ? glyph > stop : glyph < stop)
14028 && EQ (glyph->object, str);
14029 glyph += incr)
14030 {
14031 Lisp_Object cprop;
14032 EMACS_INT gpos = glyph->charpos;
14033
14034 cprop = Fget_char_property (make_number (gpos),
14035 Qcursor,
14036 glyph->object);
14037 if (!NILP (cprop))
14038 {
14039 cursor = glyph;
14040 break;
14041 }
14042 if (tem && glyph->charpos < strpos)
14043 {
14044 strpos = glyph->charpos;
14045 cursor = glyph;
14046 }
14047 }
14048
14049 if (tem == pt_old)
14050 goto compute_x;
14051 }
14052 if (tem)
14053 pos = tem + 1; /* don't find previous instances */
14054 }
14055 /* This string is not what we want; skip all of the
14056 glyphs that came from it. */
14057 while ((row->reversed_p ? glyph > stop : glyph < stop)
14058 && EQ (glyph->object, str))
14059 glyph += incr;
14060 }
14061 else
14062 glyph += incr;
14063 }
14064
14065 /* If we reached the end of the line, and END was from a string,
14066 the cursor is not on this line. */
14067 if (cursor == NULL
14068 && (row->reversed_p ? glyph <= end : glyph >= end)
14069 && STRINGP (end->object)
14070 && row->continued_p)
14071 return 0;
14072 }
14073 /* A truncated row may not include PT among its character positions.
14074 Setting the cursor inside the scroll margin will trigger
14075 recalculation of hscroll in hscroll_window_tree. But if a
14076 display string covers point, defer to the string-handling
14077 code below to figure this out. */
14078 else if (row->truncated_on_left_p && pt_old < bpos_min)
14079 {
14080 cursor = glyph_before;
14081 x = -1;
14082 }
14083 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14084 /* Zero-width characters produce no glyphs. */
14085 || (!empty_line_p
14086 && (row->reversed_p
14087 ? glyph_after > glyphs_end
14088 : glyph_after < glyphs_end)))
14089 {
14090 cursor = glyph_after;
14091 x = -1;
14092 }
14093 }
14094
14095 compute_x:
14096 if (cursor != NULL)
14097 glyph = cursor;
14098 if (x < 0)
14099 {
14100 struct glyph *g;
14101
14102 /* Need to compute x that corresponds to GLYPH. */
14103 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14104 {
14105 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14106 abort ();
14107 x += g->pixel_width;
14108 }
14109 }
14110
14111 /* ROW could be part of a continued line, which, under bidi
14112 reordering, might have other rows whose start and end charpos
14113 occlude point. Only set w->cursor if we found a better
14114 approximation to the cursor position than we have from previously
14115 examined candidate rows belonging to the same continued line. */
14116 if (/* we already have a candidate row */
14117 w->cursor.vpos >= 0
14118 /* that candidate is not the row we are processing */
14119 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14120 /* Make sure cursor.vpos specifies a row whose start and end
14121 charpos occlude point, and it is valid candidate for being a
14122 cursor-row. This is because some callers of this function
14123 leave cursor.vpos at the row where the cursor was displayed
14124 during the last redisplay cycle. */
14125 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14126 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14127 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14128 {
14129 struct glyph *g1 =
14130 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14131
14132 /* Don't consider glyphs that are outside TEXT_AREA. */
14133 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14134 return 0;
14135 /* Keep the candidate whose buffer position is the closest to
14136 point or has the `cursor' property. */
14137 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14138 w->cursor.hpos >= 0
14139 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14140 && ((BUFFERP (g1->object)
14141 && (g1->charpos == pt_old /* an exact match always wins */
14142 || (BUFFERP (glyph->object)
14143 && eabs (g1->charpos - pt_old)
14144 < eabs (glyph->charpos - pt_old))))
14145 /* previous candidate is a glyph from a string that has
14146 a non-nil `cursor' property */
14147 || (STRINGP (g1->object)
14148 && (!NILP (Fget_char_property (make_number (g1->charpos),
14149 Qcursor, g1->object))
14150 /* previous candidate is from the same display
14151 string as this one, and the display string
14152 came from a text property */
14153 || (EQ (g1->object, glyph->object)
14154 && string_from_text_prop)
14155 /* this candidate is from newline and its
14156 position is not an exact match */
14157 || (INTEGERP (glyph->object)
14158 && glyph->charpos != pt_old)))))
14159 return 0;
14160 /* If this candidate gives an exact match, use that. */
14161 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14162 /* If this candidate is a glyph created for the
14163 terminating newline of a line, and point is on that
14164 newline, it wins because it's an exact match. */
14165 || (!row->continued_p
14166 && INTEGERP (glyph->object)
14167 && glyph->charpos == 0
14168 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14169 /* Otherwise, keep the candidate that comes from a row
14170 spanning less buffer positions. This may win when one or
14171 both candidate positions are on glyphs that came from
14172 display strings, for which we cannot compare buffer
14173 positions. */
14174 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14175 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14176 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14177 return 0;
14178 }
14179 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14180 w->cursor.x = x;
14181 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14182 w->cursor.y = row->y + dy;
14183
14184 if (w == XWINDOW (selected_window))
14185 {
14186 if (!row->continued_p
14187 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14188 && row->x == 0)
14189 {
14190 this_line_buffer = XBUFFER (w->buffer);
14191
14192 CHARPOS (this_line_start_pos)
14193 = MATRIX_ROW_START_CHARPOS (row) + delta;
14194 BYTEPOS (this_line_start_pos)
14195 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14196
14197 CHARPOS (this_line_end_pos)
14198 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14199 BYTEPOS (this_line_end_pos)
14200 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14201
14202 this_line_y = w->cursor.y;
14203 this_line_pixel_height = row->height;
14204 this_line_vpos = w->cursor.vpos;
14205 this_line_start_x = row->x;
14206 }
14207 else
14208 CHARPOS (this_line_start_pos) = 0;
14209 }
14210
14211 return 1;
14212 }
14213
14214
14215 /* Run window scroll functions, if any, for WINDOW with new window
14216 start STARTP. Sets the window start of WINDOW to that position.
14217
14218 We assume that the window's buffer is really current. */
14219
14220 static inline struct text_pos
14221 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14222 {
14223 struct window *w = XWINDOW (window);
14224 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14225
14226 if (current_buffer != XBUFFER (w->buffer))
14227 abort ();
14228
14229 if (!NILP (Vwindow_scroll_functions))
14230 {
14231 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14232 make_number (CHARPOS (startp)));
14233 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14234 /* In case the hook functions switch buffers. */
14235 if (current_buffer != XBUFFER (w->buffer))
14236 set_buffer_internal_1 (XBUFFER (w->buffer));
14237 }
14238
14239 return startp;
14240 }
14241
14242
14243 /* Make sure the line containing the cursor is fully visible.
14244 A value of 1 means there is nothing to be done.
14245 (Either the line is fully visible, or it cannot be made so,
14246 or we cannot tell.)
14247
14248 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14249 is higher than window.
14250
14251 A value of 0 means the caller should do scrolling
14252 as if point had gone off the screen. */
14253
14254 static int
14255 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14256 {
14257 struct glyph_matrix *matrix;
14258 struct glyph_row *row;
14259 int window_height;
14260
14261 if (!make_cursor_line_fully_visible_p)
14262 return 1;
14263
14264 /* It's not always possible to find the cursor, e.g, when a window
14265 is full of overlay strings. Don't do anything in that case. */
14266 if (w->cursor.vpos < 0)
14267 return 1;
14268
14269 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14270 row = MATRIX_ROW (matrix, w->cursor.vpos);
14271
14272 /* If the cursor row is not partially visible, there's nothing to do. */
14273 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14274 return 1;
14275
14276 /* If the row the cursor is in is taller than the window's height,
14277 it's not clear what to do, so do nothing. */
14278 window_height = window_box_height (w);
14279 if (row->height >= window_height)
14280 {
14281 if (!force_p || MINI_WINDOW_P (w)
14282 || w->vscroll || w->cursor.vpos == 0)
14283 return 1;
14284 }
14285 return 0;
14286 }
14287
14288
14289 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14290 non-zero means only WINDOW is redisplayed in redisplay_internal.
14291 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14292 in redisplay_window to bring a partially visible line into view in
14293 the case that only the cursor has moved.
14294
14295 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14296 last screen line's vertical height extends past the end of the screen.
14297
14298 Value is
14299
14300 1 if scrolling succeeded
14301
14302 0 if scrolling didn't find point.
14303
14304 -1 if new fonts have been loaded so that we must interrupt
14305 redisplay, adjust glyph matrices, and try again. */
14306
14307 enum
14308 {
14309 SCROLLING_SUCCESS,
14310 SCROLLING_FAILED,
14311 SCROLLING_NEED_LARGER_MATRICES
14312 };
14313
14314 /* If scroll-conservatively is more than this, never recenter.
14315
14316 If you change this, don't forget to update the doc string of
14317 `scroll-conservatively' and the Emacs manual. */
14318 #define SCROLL_LIMIT 100
14319
14320 static int
14321 try_scrolling (Lisp_Object window, int just_this_one_p,
14322 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14323 int temp_scroll_step, int last_line_misfit)
14324 {
14325 struct window *w = XWINDOW (window);
14326 struct frame *f = XFRAME (w->frame);
14327 struct text_pos pos, startp;
14328 struct it it;
14329 int this_scroll_margin, scroll_max, rc, height;
14330 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14331 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14332 Lisp_Object aggressive;
14333 /* We will never try scrolling more than this number of lines. */
14334 int scroll_limit = SCROLL_LIMIT;
14335
14336 #if GLYPH_DEBUG
14337 debug_method_add (w, "try_scrolling");
14338 #endif
14339
14340 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14341
14342 /* Compute scroll margin height in pixels. We scroll when point is
14343 within this distance from the top or bottom of the window. */
14344 if (scroll_margin > 0)
14345 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14346 * FRAME_LINE_HEIGHT (f);
14347 else
14348 this_scroll_margin = 0;
14349
14350 /* Force arg_scroll_conservatively to have a reasonable value, to
14351 avoid scrolling too far away with slow move_it_* functions. Note
14352 that the user can supply scroll-conservatively equal to
14353 `most-positive-fixnum', which can be larger than INT_MAX. */
14354 if (arg_scroll_conservatively > scroll_limit)
14355 {
14356 arg_scroll_conservatively = scroll_limit + 1;
14357 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14358 }
14359 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14360 /* Compute how much we should try to scroll maximally to bring
14361 point into view. */
14362 scroll_max = (max (scroll_step,
14363 max (arg_scroll_conservatively, temp_scroll_step))
14364 * FRAME_LINE_HEIGHT (f));
14365 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14366 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14367 /* We're trying to scroll because of aggressive scrolling but no
14368 scroll_step is set. Choose an arbitrary one. */
14369 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14370 else
14371 scroll_max = 0;
14372
14373 too_near_end:
14374
14375 /* Decide whether to scroll down. */
14376 if (PT > CHARPOS (startp))
14377 {
14378 int scroll_margin_y;
14379
14380 /* Compute the pixel ypos of the scroll margin, then move IT to
14381 either that ypos or PT, whichever comes first. */
14382 start_display (&it, w, startp);
14383 scroll_margin_y = it.last_visible_y - this_scroll_margin
14384 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14385 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14386 (MOVE_TO_POS | MOVE_TO_Y));
14387
14388 if (PT > CHARPOS (it.current.pos))
14389 {
14390 int y0 = line_bottom_y (&it);
14391 /* Compute how many pixels below window bottom to stop searching
14392 for PT. This avoids costly search for PT that is far away if
14393 the user limited scrolling by a small number of lines, but
14394 always finds PT if scroll_conservatively is set to a large
14395 number, such as most-positive-fixnum. */
14396 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14397 int y_to_move = it.last_visible_y + slack;
14398
14399 /* Compute the distance from the scroll margin to PT or to
14400 the scroll limit, whichever comes first. This should
14401 include the height of the cursor line, to make that line
14402 fully visible. */
14403 move_it_to (&it, PT, -1, y_to_move,
14404 -1, MOVE_TO_POS | MOVE_TO_Y);
14405 dy = line_bottom_y (&it) - y0;
14406
14407 if (dy > scroll_max)
14408 return SCROLLING_FAILED;
14409
14410 if (dy > 0)
14411 scroll_down_p = 1;
14412 }
14413 }
14414
14415 if (scroll_down_p)
14416 {
14417 /* Point is in or below the bottom scroll margin, so move the
14418 window start down. If scrolling conservatively, move it just
14419 enough down to make point visible. If scroll_step is set,
14420 move it down by scroll_step. */
14421 if (arg_scroll_conservatively)
14422 amount_to_scroll
14423 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14424 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14425 else if (scroll_step || temp_scroll_step)
14426 amount_to_scroll = scroll_max;
14427 else
14428 {
14429 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14430 height = WINDOW_BOX_TEXT_HEIGHT (w);
14431 if (NUMBERP (aggressive))
14432 {
14433 double float_amount = XFLOATINT (aggressive) * height;
14434 amount_to_scroll = float_amount;
14435 if (amount_to_scroll == 0 && float_amount > 0)
14436 amount_to_scroll = 1;
14437 /* Don't let point enter the scroll margin near top of
14438 the window. */
14439 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14440 amount_to_scroll = height - 2*this_scroll_margin + dy;
14441 }
14442 }
14443
14444 if (amount_to_scroll <= 0)
14445 return SCROLLING_FAILED;
14446
14447 start_display (&it, w, startp);
14448 if (arg_scroll_conservatively <= scroll_limit)
14449 move_it_vertically (&it, amount_to_scroll);
14450 else
14451 {
14452 /* Extra precision for users who set scroll-conservatively
14453 to a large number: make sure the amount we scroll
14454 the window start is never less than amount_to_scroll,
14455 which was computed as distance from window bottom to
14456 point. This matters when lines at window top and lines
14457 below window bottom have different height. */
14458 struct it it1;
14459 void *it1data = NULL;
14460 /* We use a temporary it1 because line_bottom_y can modify
14461 its argument, if it moves one line down; see there. */
14462 int start_y;
14463
14464 SAVE_IT (it1, it, it1data);
14465 start_y = line_bottom_y (&it1);
14466 do {
14467 RESTORE_IT (&it, &it, it1data);
14468 move_it_by_lines (&it, 1);
14469 SAVE_IT (it1, it, it1data);
14470 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14471 }
14472
14473 /* If STARTP is unchanged, move it down another screen line. */
14474 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14475 move_it_by_lines (&it, 1);
14476 startp = it.current.pos;
14477 }
14478 else
14479 {
14480 struct text_pos scroll_margin_pos = startp;
14481
14482 /* See if point is inside the scroll margin at the top of the
14483 window. */
14484 if (this_scroll_margin)
14485 {
14486 start_display (&it, w, startp);
14487 move_it_vertically (&it, this_scroll_margin);
14488 scroll_margin_pos = it.current.pos;
14489 }
14490
14491 if (PT < CHARPOS (scroll_margin_pos))
14492 {
14493 /* Point is in the scroll margin at the top of the window or
14494 above what is displayed in the window. */
14495 int y0, y_to_move;
14496
14497 /* Compute the vertical distance from PT to the scroll
14498 margin position. Move as far as scroll_max allows, or
14499 one screenful, or 10 screen lines, whichever is largest.
14500 Give up if distance is greater than scroll_max. */
14501 SET_TEXT_POS (pos, PT, PT_BYTE);
14502 start_display (&it, w, pos);
14503 y0 = it.current_y;
14504 y_to_move = max (it.last_visible_y,
14505 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14506 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14507 y_to_move, -1,
14508 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14509 dy = it.current_y - y0;
14510 if (dy > scroll_max)
14511 return SCROLLING_FAILED;
14512
14513 /* Compute new window start. */
14514 start_display (&it, w, startp);
14515
14516 if (arg_scroll_conservatively)
14517 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14518 max (scroll_step, temp_scroll_step));
14519 else if (scroll_step || temp_scroll_step)
14520 amount_to_scroll = scroll_max;
14521 else
14522 {
14523 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14524 height = WINDOW_BOX_TEXT_HEIGHT (w);
14525 if (NUMBERP (aggressive))
14526 {
14527 double float_amount = XFLOATINT (aggressive) * height;
14528 amount_to_scroll = float_amount;
14529 if (amount_to_scroll == 0 && float_amount > 0)
14530 amount_to_scroll = 1;
14531 amount_to_scroll -=
14532 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14533 /* Don't let point enter the scroll margin near
14534 bottom of the window. */
14535 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14536 amount_to_scroll = height - 2*this_scroll_margin + dy;
14537 }
14538 }
14539
14540 if (amount_to_scroll <= 0)
14541 return SCROLLING_FAILED;
14542
14543 move_it_vertically_backward (&it, amount_to_scroll);
14544 startp = it.current.pos;
14545 }
14546 }
14547
14548 /* Run window scroll functions. */
14549 startp = run_window_scroll_functions (window, startp);
14550
14551 /* Display the window. Give up if new fonts are loaded, or if point
14552 doesn't appear. */
14553 if (!try_window (window, startp, 0))
14554 rc = SCROLLING_NEED_LARGER_MATRICES;
14555 else if (w->cursor.vpos < 0)
14556 {
14557 clear_glyph_matrix (w->desired_matrix);
14558 rc = SCROLLING_FAILED;
14559 }
14560 else
14561 {
14562 /* Maybe forget recorded base line for line number display. */
14563 if (!just_this_one_p
14564 || current_buffer->clip_changed
14565 || BEG_UNCHANGED < CHARPOS (startp))
14566 w->base_line_number = Qnil;
14567
14568 /* If cursor ends up on a partially visible line,
14569 treat that as being off the bottom of the screen. */
14570 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14571 /* It's possible that the cursor is on the first line of the
14572 buffer, which is partially obscured due to a vscroll
14573 (Bug#7537). In that case, avoid looping forever . */
14574 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14575 {
14576 clear_glyph_matrix (w->desired_matrix);
14577 ++extra_scroll_margin_lines;
14578 goto too_near_end;
14579 }
14580 rc = SCROLLING_SUCCESS;
14581 }
14582
14583 return rc;
14584 }
14585
14586
14587 /* Compute a suitable window start for window W if display of W starts
14588 on a continuation line. Value is non-zero if a new window start
14589 was computed.
14590
14591 The new window start will be computed, based on W's width, starting
14592 from the start of the continued line. It is the start of the
14593 screen line with the minimum distance from the old start W->start. */
14594
14595 static int
14596 compute_window_start_on_continuation_line (struct window *w)
14597 {
14598 struct text_pos pos, start_pos;
14599 int window_start_changed_p = 0;
14600
14601 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14602
14603 /* If window start is on a continuation line... Window start may be
14604 < BEGV in case there's invisible text at the start of the
14605 buffer (M-x rmail, for example). */
14606 if (CHARPOS (start_pos) > BEGV
14607 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14608 {
14609 struct it it;
14610 struct glyph_row *row;
14611
14612 /* Handle the case that the window start is out of range. */
14613 if (CHARPOS (start_pos) < BEGV)
14614 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14615 else if (CHARPOS (start_pos) > ZV)
14616 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14617
14618 /* Find the start of the continued line. This should be fast
14619 because scan_buffer is fast (newline cache). */
14620 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14621 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14622 row, DEFAULT_FACE_ID);
14623 reseat_at_previous_visible_line_start (&it);
14624
14625 /* If the line start is "too far" away from the window start,
14626 say it takes too much time to compute a new window start. */
14627 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14628 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14629 {
14630 int min_distance, distance;
14631
14632 /* Move forward by display lines to find the new window
14633 start. If window width was enlarged, the new start can
14634 be expected to be > the old start. If window width was
14635 decreased, the new window start will be < the old start.
14636 So, we're looking for the display line start with the
14637 minimum distance from the old window start. */
14638 pos = it.current.pos;
14639 min_distance = INFINITY;
14640 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14641 distance < min_distance)
14642 {
14643 min_distance = distance;
14644 pos = it.current.pos;
14645 move_it_by_lines (&it, 1);
14646 }
14647
14648 /* Set the window start there. */
14649 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14650 window_start_changed_p = 1;
14651 }
14652 }
14653
14654 return window_start_changed_p;
14655 }
14656
14657
14658 /* Try cursor movement in case text has not changed in window WINDOW,
14659 with window start STARTP. Value is
14660
14661 CURSOR_MOVEMENT_SUCCESS if successful
14662
14663 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14664
14665 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14666 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14667 we want to scroll as if scroll-step were set to 1. See the code.
14668
14669 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14670 which case we have to abort this redisplay, and adjust matrices
14671 first. */
14672
14673 enum
14674 {
14675 CURSOR_MOVEMENT_SUCCESS,
14676 CURSOR_MOVEMENT_CANNOT_BE_USED,
14677 CURSOR_MOVEMENT_MUST_SCROLL,
14678 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14679 };
14680
14681 static int
14682 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14683 {
14684 struct window *w = XWINDOW (window);
14685 struct frame *f = XFRAME (w->frame);
14686 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14687
14688 #if GLYPH_DEBUG
14689 if (inhibit_try_cursor_movement)
14690 return rc;
14691 #endif
14692
14693 /* Handle case where text has not changed, only point, and it has
14694 not moved off the frame. */
14695 if (/* Point may be in this window. */
14696 PT >= CHARPOS (startp)
14697 /* Selective display hasn't changed. */
14698 && !current_buffer->clip_changed
14699 /* Function force-mode-line-update is used to force a thorough
14700 redisplay. It sets either windows_or_buffers_changed or
14701 update_mode_lines. So don't take a shortcut here for these
14702 cases. */
14703 && !update_mode_lines
14704 && !windows_or_buffers_changed
14705 && !cursor_type_changed
14706 /* Can't use this case if highlighting a region. When a
14707 region exists, cursor movement has to do more than just
14708 set the cursor. */
14709 && !(!NILP (Vtransient_mark_mode)
14710 && !NILP (BVAR (current_buffer, mark_active)))
14711 && NILP (w->region_showing)
14712 && NILP (Vshow_trailing_whitespace)
14713 /* Right after splitting windows, last_point may be nil. */
14714 && INTEGERP (w->last_point)
14715 /* This code is not used for mini-buffer for the sake of the case
14716 of redisplaying to replace an echo area message; since in
14717 that case the mini-buffer contents per se are usually
14718 unchanged. This code is of no real use in the mini-buffer
14719 since the handling of this_line_start_pos, etc., in redisplay
14720 handles the same cases. */
14721 && !EQ (window, minibuf_window)
14722 /* When splitting windows or for new windows, it happens that
14723 redisplay is called with a nil window_end_vpos or one being
14724 larger than the window. This should really be fixed in
14725 window.c. I don't have this on my list, now, so we do
14726 approximately the same as the old redisplay code. --gerd. */
14727 && INTEGERP (w->window_end_vpos)
14728 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14729 && (FRAME_WINDOW_P (f)
14730 || !overlay_arrow_in_current_buffer_p ()))
14731 {
14732 int this_scroll_margin, top_scroll_margin;
14733 struct glyph_row *row = NULL;
14734
14735 #if GLYPH_DEBUG
14736 debug_method_add (w, "cursor movement");
14737 #endif
14738
14739 /* Scroll if point within this distance from the top or bottom
14740 of the window. This is a pixel value. */
14741 if (scroll_margin > 0)
14742 {
14743 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14744 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14745 }
14746 else
14747 this_scroll_margin = 0;
14748
14749 top_scroll_margin = this_scroll_margin;
14750 if (WINDOW_WANTS_HEADER_LINE_P (w))
14751 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14752
14753 /* Start with the row the cursor was displayed during the last
14754 not paused redisplay. Give up if that row is not valid. */
14755 if (w->last_cursor.vpos < 0
14756 || w->last_cursor.vpos >= w->current_matrix->nrows)
14757 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14758 else
14759 {
14760 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14761 if (row->mode_line_p)
14762 ++row;
14763 if (!row->enabled_p)
14764 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14765 }
14766
14767 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14768 {
14769 int scroll_p = 0, must_scroll = 0;
14770 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14771
14772 if (PT > XFASTINT (w->last_point))
14773 {
14774 /* Point has moved forward. */
14775 while (MATRIX_ROW_END_CHARPOS (row) < PT
14776 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14777 {
14778 xassert (row->enabled_p);
14779 ++row;
14780 }
14781
14782 /* If the end position of a row equals the start
14783 position of the next row, and PT is at that position,
14784 we would rather display cursor in the next line. */
14785 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14786 && MATRIX_ROW_END_CHARPOS (row) == PT
14787 && row < w->current_matrix->rows
14788 + w->current_matrix->nrows - 1
14789 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14790 && !cursor_row_p (row))
14791 ++row;
14792
14793 /* If within the scroll margin, scroll. Note that
14794 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14795 the next line would be drawn, and that
14796 this_scroll_margin can be zero. */
14797 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14798 || PT > MATRIX_ROW_END_CHARPOS (row)
14799 /* Line is completely visible last line in window
14800 and PT is to be set in the next line. */
14801 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14802 && PT == MATRIX_ROW_END_CHARPOS (row)
14803 && !row->ends_at_zv_p
14804 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14805 scroll_p = 1;
14806 }
14807 else if (PT < XFASTINT (w->last_point))
14808 {
14809 /* Cursor has to be moved backward. Note that PT >=
14810 CHARPOS (startp) because of the outer if-statement. */
14811 while (!row->mode_line_p
14812 && (MATRIX_ROW_START_CHARPOS (row) > PT
14813 || (MATRIX_ROW_START_CHARPOS (row) == PT
14814 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14815 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14816 row > w->current_matrix->rows
14817 && (row-1)->ends_in_newline_from_string_p))))
14818 && (row->y > top_scroll_margin
14819 || CHARPOS (startp) == BEGV))
14820 {
14821 xassert (row->enabled_p);
14822 --row;
14823 }
14824
14825 /* Consider the following case: Window starts at BEGV,
14826 there is invisible, intangible text at BEGV, so that
14827 display starts at some point START > BEGV. It can
14828 happen that we are called with PT somewhere between
14829 BEGV and START. Try to handle that case. */
14830 if (row < w->current_matrix->rows
14831 || row->mode_line_p)
14832 {
14833 row = w->current_matrix->rows;
14834 if (row->mode_line_p)
14835 ++row;
14836 }
14837
14838 /* Due to newlines in overlay strings, we may have to
14839 skip forward over overlay strings. */
14840 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14841 && MATRIX_ROW_END_CHARPOS (row) == PT
14842 && !cursor_row_p (row))
14843 ++row;
14844
14845 /* If within the scroll margin, scroll. */
14846 if (row->y < top_scroll_margin
14847 && CHARPOS (startp) != BEGV)
14848 scroll_p = 1;
14849 }
14850 else
14851 {
14852 /* Cursor did not move. So don't scroll even if cursor line
14853 is partially visible, as it was so before. */
14854 rc = CURSOR_MOVEMENT_SUCCESS;
14855 }
14856
14857 if (PT < MATRIX_ROW_START_CHARPOS (row)
14858 || PT > MATRIX_ROW_END_CHARPOS (row))
14859 {
14860 /* if PT is not in the glyph row, give up. */
14861 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14862 must_scroll = 1;
14863 }
14864 else if (rc != CURSOR_MOVEMENT_SUCCESS
14865 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14866 {
14867 /* If rows are bidi-reordered and point moved, back up
14868 until we find a row that does not belong to a
14869 continuation line. This is because we must consider
14870 all rows of a continued line as candidates for the
14871 new cursor positioning, since row start and end
14872 positions change non-linearly with vertical position
14873 in such rows. */
14874 /* FIXME: Revisit this when glyph ``spilling'' in
14875 continuation lines' rows is implemented for
14876 bidi-reordered rows. */
14877 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14878 {
14879 /* If we hit the beginning of the displayed portion
14880 without finding the first row of a continued
14881 line, give up. */
14882 if (row <= w->current_matrix->rows)
14883 {
14884 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14885 break;
14886 }
14887 xassert (row->enabled_p);
14888 --row;
14889 }
14890 }
14891 if (must_scroll)
14892 ;
14893 else if (rc != CURSOR_MOVEMENT_SUCCESS
14894 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14895 && make_cursor_line_fully_visible_p)
14896 {
14897 if (PT == MATRIX_ROW_END_CHARPOS (row)
14898 && !row->ends_at_zv_p
14899 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14900 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14901 else if (row->height > window_box_height (w))
14902 {
14903 /* If we end up in a partially visible line, let's
14904 make it fully visible, except when it's taller
14905 than the window, in which case we can't do much
14906 about it. */
14907 *scroll_step = 1;
14908 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14909 }
14910 else
14911 {
14912 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14913 if (!cursor_row_fully_visible_p (w, 0, 1))
14914 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14915 else
14916 rc = CURSOR_MOVEMENT_SUCCESS;
14917 }
14918 }
14919 else if (scroll_p)
14920 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14921 else if (rc != CURSOR_MOVEMENT_SUCCESS
14922 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14923 {
14924 /* With bidi-reordered rows, there could be more than
14925 one candidate row whose start and end positions
14926 occlude point. We need to let set_cursor_from_row
14927 find the best candidate. */
14928 /* FIXME: Revisit this when glyph ``spilling'' in
14929 continuation lines' rows is implemented for
14930 bidi-reordered rows. */
14931 int rv = 0;
14932
14933 do
14934 {
14935 int at_zv_p = 0, exact_match_p = 0;
14936
14937 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14938 && PT <= MATRIX_ROW_END_CHARPOS (row)
14939 && cursor_row_p (row))
14940 rv |= set_cursor_from_row (w, row, w->current_matrix,
14941 0, 0, 0, 0);
14942 /* As soon as we've found the exact match for point,
14943 or the first suitable row whose ends_at_zv_p flag
14944 is set, we are done. */
14945 at_zv_p =
14946 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
14947 if (rv && !at_zv_p
14948 && w->cursor.hpos >= 0
14949 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
14950 w->cursor.vpos))
14951 {
14952 struct glyph_row *candidate =
14953 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14954 struct glyph *g =
14955 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
14956 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
14957
14958 exact_match_p =
14959 (BUFFERP (g->object) && g->charpos == PT)
14960 || (INTEGERP (g->object)
14961 && (g->charpos == PT
14962 || (g->charpos == 0 && endpos - 1 == PT)));
14963 }
14964 if (rv && (at_zv_p || exact_match_p))
14965 {
14966 rc = CURSOR_MOVEMENT_SUCCESS;
14967 break;
14968 }
14969 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
14970 break;
14971 ++row;
14972 }
14973 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
14974 || row->continued_p)
14975 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14976 || (MATRIX_ROW_START_CHARPOS (row) == PT
14977 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14978 /* If we didn't find any candidate rows, or exited the
14979 loop before all the candidates were examined, signal
14980 to the caller that this method failed. */
14981 if (rc != CURSOR_MOVEMENT_SUCCESS
14982 && !(rv
14983 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14984 && !row->continued_p))
14985 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14986 else if (rv)
14987 rc = CURSOR_MOVEMENT_SUCCESS;
14988 }
14989 else
14990 {
14991 do
14992 {
14993 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14994 {
14995 rc = CURSOR_MOVEMENT_SUCCESS;
14996 break;
14997 }
14998 ++row;
14999 }
15000 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15001 && MATRIX_ROW_START_CHARPOS (row) == PT
15002 && cursor_row_p (row));
15003 }
15004 }
15005 }
15006
15007 return rc;
15008 }
15009
15010 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15011 static
15012 #endif
15013 void
15014 set_vertical_scroll_bar (struct window *w)
15015 {
15016 EMACS_INT start, end, whole;
15017
15018 /* Calculate the start and end positions for the current window.
15019 At some point, it would be nice to choose between scrollbars
15020 which reflect the whole buffer size, with special markers
15021 indicating narrowing, and scrollbars which reflect only the
15022 visible region.
15023
15024 Note that mini-buffers sometimes aren't displaying any text. */
15025 if (!MINI_WINDOW_P (w)
15026 || (w == XWINDOW (minibuf_window)
15027 && NILP (echo_area_buffer[0])))
15028 {
15029 struct buffer *buf = XBUFFER (w->buffer);
15030 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15031 start = marker_position (w->start) - BUF_BEGV (buf);
15032 /* I don't think this is guaranteed to be right. For the
15033 moment, we'll pretend it is. */
15034 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15035
15036 if (end < start)
15037 end = start;
15038 if (whole < (end - start))
15039 whole = end - start;
15040 }
15041 else
15042 start = end = whole = 0;
15043
15044 /* Indicate what this scroll bar ought to be displaying now. */
15045 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15046 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15047 (w, end - start, whole, start);
15048 }
15049
15050
15051 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15052 selected_window is redisplayed.
15053
15054 We can return without actually redisplaying the window if
15055 fonts_changed_p is nonzero. In that case, redisplay_internal will
15056 retry. */
15057
15058 static void
15059 redisplay_window (Lisp_Object window, int just_this_one_p)
15060 {
15061 struct window *w = XWINDOW (window);
15062 struct frame *f = XFRAME (w->frame);
15063 struct buffer *buffer = XBUFFER (w->buffer);
15064 struct buffer *old = current_buffer;
15065 struct text_pos lpoint, opoint, startp;
15066 int update_mode_line;
15067 int tem;
15068 struct it it;
15069 /* Record it now because it's overwritten. */
15070 int current_matrix_up_to_date_p = 0;
15071 int used_current_matrix_p = 0;
15072 /* This is less strict than current_matrix_up_to_date_p.
15073 It indicates that the buffer contents and narrowing are unchanged. */
15074 int buffer_unchanged_p = 0;
15075 int temp_scroll_step = 0;
15076 int count = SPECPDL_INDEX ();
15077 int rc;
15078 int centering_position = -1;
15079 int last_line_misfit = 0;
15080 EMACS_INT beg_unchanged, end_unchanged;
15081
15082 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15083 opoint = lpoint;
15084
15085 /* W must be a leaf window here. */
15086 xassert (!NILP (w->buffer));
15087 #if GLYPH_DEBUG
15088 *w->desired_matrix->method = 0;
15089 #endif
15090
15091 restart:
15092 reconsider_clip_changes (w, buffer);
15093
15094 /* Has the mode line to be updated? */
15095 update_mode_line = (!NILP (w->update_mode_line)
15096 || update_mode_lines
15097 || buffer->clip_changed
15098 || buffer->prevent_redisplay_optimizations_p);
15099
15100 if (MINI_WINDOW_P (w))
15101 {
15102 if (w == XWINDOW (echo_area_window)
15103 && !NILP (echo_area_buffer[0]))
15104 {
15105 if (update_mode_line)
15106 /* We may have to update a tty frame's menu bar or a
15107 tool-bar. Example `M-x C-h C-h C-g'. */
15108 goto finish_menu_bars;
15109 else
15110 /* We've already displayed the echo area glyphs in this window. */
15111 goto finish_scroll_bars;
15112 }
15113 else if ((w != XWINDOW (minibuf_window)
15114 || minibuf_level == 0)
15115 /* When buffer is nonempty, redisplay window normally. */
15116 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15117 /* Quail displays non-mini buffers in minibuffer window.
15118 In that case, redisplay the window normally. */
15119 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15120 {
15121 /* W is a mini-buffer window, but it's not active, so clear
15122 it. */
15123 int yb = window_text_bottom_y (w);
15124 struct glyph_row *row;
15125 int y;
15126
15127 for (y = 0, row = w->desired_matrix->rows;
15128 y < yb;
15129 y += row->height, ++row)
15130 blank_row (w, row, y);
15131 goto finish_scroll_bars;
15132 }
15133
15134 clear_glyph_matrix (w->desired_matrix);
15135 }
15136
15137 /* Otherwise set up data on this window; select its buffer and point
15138 value. */
15139 /* Really select the buffer, for the sake of buffer-local
15140 variables. */
15141 set_buffer_internal_1 (XBUFFER (w->buffer));
15142
15143 current_matrix_up_to_date_p
15144 = (!NILP (w->window_end_valid)
15145 && !current_buffer->clip_changed
15146 && !current_buffer->prevent_redisplay_optimizations_p
15147 && XFASTINT (w->last_modified) >= MODIFF
15148 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15149
15150 /* Run the window-bottom-change-functions
15151 if it is possible that the text on the screen has changed
15152 (either due to modification of the text, or any other reason). */
15153 if (!current_matrix_up_to_date_p
15154 && !NILP (Vwindow_text_change_functions))
15155 {
15156 safe_run_hooks (Qwindow_text_change_functions);
15157 goto restart;
15158 }
15159
15160 beg_unchanged = BEG_UNCHANGED;
15161 end_unchanged = END_UNCHANGED;
15162
15163 SET_TEXT_POS (opoint, PT, PT_BYTE);
15164
15165 specbind (Qinhibit_point_motion_hooks, Qt);
15166
15167 buffer_unchanged_p
15168 = (!NILP (w->window_end_valid)
15169 && !current_buffer->clip_changed
15170 && XFASTINT (w->last_modified) >= MODIFF
15171 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15172
15173 /* When windows_or_buffers_changed is non-zero, we can't rely on
15174 the window end being valid, so set it to nil there. */
15175 if (windows_or_buffers_changed)
15176 {
15177 /* If window starts on a continuation line, maybe adjust the
15178 window start in case the window's width changed. */
15179 if (XMARKER (w->start)->buffer == current_buffer)
15180 compute_window_start_on_continuation_line (w);
15181
15182 w->window_end_valid = Qnil;
15183 }
15184
15185 /* Some sanity checks. */
15186 CHECK_WINDOW_END (w);
15187 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15188 abort ();
15189 if (BYTEPOS (opoint) < CHARPOS (opoint))
15190 abort ();
15191
15192 /* If %c is in mode line, update it if needed. */
15193 if (!NILP (w->column_number_displayed)
15194 /* This alternative quickly identifies a common case
15195 where no change is needed. */
15196 && !(PT == XFASTINT (w->last_point)
15197 && XFASTINT (w->last_modified) >= MODIFF
15198 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15199 && (XFASTINT (w->column_number_displayed) != current_column ()))
15200 update_mode_line = 1;
15201
15202 /* Count number of windows showing the selected buffer. An indirect
15203 buffer counts as its base buffer. */
15204 if (!just_this_one_p)
15205 {
15206 struct buffer *current_base, *window_base;
15207 current_base = current_buffer;
15208 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15209 if (current_base->base_buffer)
15210 current_base = current_base->base_buffer;
15211 if (window_base->base_buffer)
15212 window_base = window_base->base_buffer;
15213 if (current_base == window_base)
15214 buffer_shared++;
15215 }
15216
15217 /* Point refers normally to the selected window. For any other
15218 window, set up appropriate value. */
15219 if (!EQ (window, selected_window))
15220 {
15221 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
15222 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
15223 if (new_pt < BEGV)
15224 {
15225 new_pt = BEGV;
15226 new_pt_byte = BEGV_BYTE;
15227 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15228 }
15229 else if (new_pt > (ZV - 1))
15230 {
15231 new_pt = ZV;
15232 new_pt_byte = ZV_BYTE;
15233 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15234 }
15235
15236 /* We don't use SET_PT so that the point-motion hooks don't run. */
15237 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15238 }
15239
15240 /* If any of the character widths specified in the display table
15241 have changed, invalidate the width run cache. It's true that
15242 this may be a bit late to catch such changes, but the rest of
15243 redisplay goes (non-fatally) haywire when the display table is
15244 changed, so why should we worry about doing any better? */
15245 if (current_buffer->width_run_cache)
15246 {
15247 struct Lisp_Char_Table *disptab = buffer_display_table ();
15248
15249 if (! disptab_matches_widthtab (disptab,
15250 XVECTOR (BVAR (current_buffer, width_table))))
15251 {
15252 invalidate_region_cache (current_buffer,
15253 current_buffer->width_run_cache,
15254 BEG, Z);
15255 recompute_width_table (current_buffer, disptab);
15256 }
15257 }
15258
15259 /* If window-start is screwed up, choose a new one. */
15260 if (XMARKER (w->start)->buffer != current_buffer)
15261 goto recenter;
15262
15263 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15264
15265 /* If someone specified a new starting point but did not insist,
15266 check whether it can be used. */
15267 if (!NILP (w->optional_new_start)
15268 && CHARPOS (startp) >= BEGV
15269 && CHARPOS (startp) <= ZV)
15270 {
15271 w->optional_new_start = Qnil;
15272 start_display (&it, w, startp);
15273 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15274 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15275 if (IT_CHARPOS (it) == PT)
15276 w->force_start = Qt;
15277 /* IT may overshoot PT if text at PT is invisible. */
15278 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15279 w->force_start = Qt;
15280 }
15281
15282 force_start:
15283
15284 /* Handle case where place to start displaying has been specified,
15285 unless the specified location is outside the accessible range. */
15286 if (!NILP (w->force_start)
15287 || w->frozen_window_start_p)
15288 {
15289 /* We set this later on if we have to adjust point. */
15290 int new_vpos = -1;
15291
15292 w->force_start = Qnil;
15293 w->vscroll = 0;
15294 w->window_end_valid = Qnil;
15295
15296 /* Forget any recorded base line for line number display. */
15297 if (!buffer_unchanged_p)
15298 w->base_line_number = Qnil;
15299
15300 /* Redisplay the mode line. Select the buffer properly for that.
15301 Also, run the hook window-scroll-functions
15302 because we have scrolled. */
15303 /* Note, we do this after clearing force_start because
15304 if there's an error, it is better to forget about force_start
15305 than to get into an infinite loop calling the hook functions
15306 and having them get more errors. */
15307 if (!update_mode_line
15308 || ! NILP (Vwindow_scroll_functions))
15309 {
15310 update_mode_line = 1;
15311 w->update_mode_line = Qt;
15312 startp = run_window_scroll_functions (window, startp);
15313 }
15314
15315 w->last_modified = make_number (0);
15316 w->last_overlay_modified = make_number (0);
15317 if (CHARPOS (startp) < BEGV)
15318 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15319 else if (CHARPOS (startp) > ZV)
15320 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15321
15322 /* Redisplay, then check if cursor has been set during the
15323 redisplay. Give up if new fonts were loaded. */
15324 /* We used to issue a CHECK_MARGINS argument to try_window here,
15325 but this causes scrolling to fail when point begins inside
15326 the scroll margin (bug#148) -- cyd */
15327 if (!try_window (window, startp, 0))
15328 {
15329 w->force_start = Qt;
15330 clear_glyph_matrix (w->desired_matrix);
15331 goto need_larger_matrices;
15332 }
15333
15334 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15335 {
15336 /* If point does not appear, try to move point so it does
15337 appear. The desired matrix has been built above, so we
15338 can use it here. */
15339 new_vpos = window_box_height (w) / 2;
15340 }
15341
15342 if (!cursor_row_fully_visible_p (w, 0, 0))
15343 {
15344 /* Point does appear, but on a line partly visible at end of window.
15345 Move it back to a fully-visible line. */
15346 new_vpos = window_box_height (w);
15347 }
15348
15349 /* If we need to move point for either of the above reasons,
15350 now actually do it. */
15351 if (new_vpos >= 0)
15352 {
15353 struct glyph_row *row;
15354
15355 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15356 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15357 ++row;
15358
15359 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15360 MATRIX_ROW_START_BYTEPOS (row));
15361
15362 if (w != XWINDOW (selected_window))
15363 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15364 else if (current_buffer == old)
15365 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15366
15367 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15368
15369 /* If we are highlighting the region, then we just changed
15370 the region, so redisplay to show it. */
15371 if (!NILP (Vtransient_mark_mode)
15372 && !NILP (BVAR (current_buffer, mark_active)))
15373 {
15374 clear_glyph_matrix (w->desired_matrix);
15375 if (!try_window (window, startp, 0))
15376 goto need_larger_matrices;
15377 }
15378 }
15379
15380 #if GLYPH_DEBUG
15381 debug_method_add (w, "forced window start");
15382 #endif
15383 goto done;
15384 }
15385
15386 /* Handle case where text has not changed, only point, and it has
15387 not moved off the frame, and we are not retrying after hscroll.
15388 (current_matrix_up_to_date_p is nonzero when retrying.) */
15389 if (current_matrix_up_to_date_p
15390 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15391 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15392 {
15393 switch (rc)
15394 {
15395 case CURSOR_MOVEMENT_SUCCESS:
15396 used_current_matrix_p = 1;
15397 goto done;
15398
15399 case CURSOR_MOVEMENT_MUST_SCROLL:
15400 goto try_to_scroll;
15401
15402 default:
15403 abort ();
15404 }
15405 }
15406 /* If current starting point was originally the beginning of a line
15407 but no longer is, find a new starting point. */
15408 else if (!NILP (w->start_at_line_beg)
15409 && !(CHARPOS (startp) <= BEGV
15410 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15411 {
15412 #if GLYPH_DEBUG
15413 debug_method_add (w, "recenter 1");
15414 #endif
15415 goto recenter;
15416 }
15417
15418 /* Try scrolling with try_window_id. Value is > 0 if update has
15419 been done, it is -1 if we know that the same window start will
15420 not work. It is 0 if unsuccessful for some other reason. */
15421 else if ((tem = try_window_id (w)) != 0)
15422 {
15423 #if GLYPH_DEBUG
15424 debug_method_add (w, "try_window_id %d", tem);
15425 #endif
15426
15427 if (fonts_changed_p)
15428 goto need_larger_matrices;
15429 if (tem > 0)
15430 goto done;
15431
15432 /* Otherwise try_window_id has returned -1 which means that we
15433 don't want the alternative below this comment to execute. */
15434 }
15435 else if (CHARPOS (startp) >= BEGV
15436 && CHARPOS (startp) <= ZV
15437 && PT >= CHARPOS (startp)
15438 && (CHARPOS (startp) < ZV
15439 /* Avoid starting at end of buffer. */
15440 || CHARPOS (startp) == BEGV
15441 || (XFASTINT (w->last_modified) >= MODIFF
15442 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15443 {
15444 int d1, d2, d3, d4, d5, d6;
15445
15446 /* If first window line is a continuation line, and window start
15447 is inside the modified region, but the first change is before
15448 current window start, we must select a new window start.
15449
15450 However, if this is the result of a down-mouse event (e.g. by
15451 extending the mouse-drag-overlay), we don't want to select a
15452 new window start, since that would change the position under
15453 the mouse, resulting in an unwanted mouse-movement rather
15454 than a simple mouse-click. */
15455 if (NILP (w->start_at_line_beg)
15456 && NILP (do_mouse_tracking)
15457 && CHARPOS (startp) > BEGV
15458 && CHARPOS (startp) > BEG + beg_unchanged
15459 && CHARPOS (startp) <= Z - end_unchanged
15460 /* Even if w->start_at_line_beg is nil, a new window may
15461 start at a line_beg, since that's how set_buffer_window
15462 sets it. So, we need to check the return value of
15463 compute_window_start_on_continuation_line. (See also
15464 bug#197). */
15465 && XMARKER (w->start)->buffer == current_buffer
15466 && compute_window_start_on_continuation_line (w)
15467 /* It doesn't make sense to force the window start like we
15468 do at label force_start if it is already known that point
15469 will not be visible in the resulting window, because
15470 doing so will move point from its correct position
15471 instead of scrolling the window to bring point into view.
15472 See bug#9324. */
15473 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15474 {
15475 w->force_start = Qt;
15476 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15477 goto force_start;
15478 }
15479
15480 #if GLYPH_DEBUG
15481 debug_method_add (w, "same window start");
15482 #endif
15483
15484 /* Try to redisplay starting at same place as before.
15485 If point has not moved off frame, accept the results. */
15486 if (!current_matrix_up_to_date_p
15487 /* Don't use try_window_reusing_current_matrix in this case
15488 because a window scroll function can have changed the
15489 buffer. */
15490 || !NILP (Vwindow_scroll_functions)
15491 || MINI_WINDOW_P (w)
15492 || !(used_current_matrix_p
15493 = try_window_reusing_current_matrix (w)))
15494 {
15495 IF_DEBUG (debug_method_add (w, "1"));
15496 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15497 /* -1 means we need to scroll.
15498 0 means we need new matrices, but fonts_changed_p
15499 is set in that case, so we will detect it below. */
15500 goto try_to_scroll;
15501 }
15502
15503 if (fonts_changed_p)
15504 goto need_larger_matrices;
15505
15506 if (w->cursor.vpos >= 0)
15507 {
15508 if (!just_this_one_p
15509 || current_buffer->clip_changed
15510 || BEG_UNCHANGED < CHARPOS (startp))
15511 /* Forget any recorded base line for line number display. */
15512 w->base_line_number = Qnil;
15513
15514 if (!cursor_row_fully_visible_p (w, 1, 0))
15515 {
15516 clear_glyph_matrix (w->desired_matrix);
15517 last_line_misfit = 1;
15518 }
15519 /* Drop through and scroll. */
15520 else
15521 goto done;
15522 }
15523 else
15524 clear_glyph_matrix (w->desired_matrix);
15525 }
15526
15527 try_to_scroll:
15528
15529 w->last_modified = make_number (0);
15530 w->last_overlay_modified = make_number (0);
15531
15532 /* Redisplay the mode line. Select the buffer properly for that. */
15533 if (!update_mode_line)
15534 {
15535 update_mode_line = 1;
15536 w->update_mode_line = Qt;
15537 }
15538
15539 /* Try to scroll by specified few lines. */
15540 if ((scroll_conservatively
15541 || emacs_scroll_step
15542 || temp_scroll_step
15543 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15544 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15545 && CHARPOS (startp) >= BEGV
15546 && CHARPOS (startp) <= ZV)
15547 {
15548 /* The function returns -1 if new fonts were loaded, 1 if
15549 successful, 0 if not successful. */
15550 int ss = try_scrolling (window, just_this_one_p,
15551 scroll_conservatively,
15552 emacs_scroll_step,
15553 temp_scroll_step, last_line_misfit);
15554 switch (ss)
15555 {
15556 case SCROLLING_SUCCESS:
15557 goto done;
15558
15559 case SCROLLING_NEED_LARGER_MATRICES:
15560 goto need_larger_matrices;
15561
15562 case SCROLLING_FAILED:
15563 break;
15564
15565 default:
15566 abort ();
15567 }
15568 }
15569
15570 /* Finally, just choose a place to start which positions point
15571 according to user preferences. */
15572
15573 recenter:
15574
15575 #if GLYPH_DEBUG
15576 debug_method_add (w, "recenter");
15577 #endif
15578
15579 /* w->vscroll = 0; */
15580
15581 /* Forget any previously recorded base line for line number display. */
15582 if (!buffer_unchanged_p)
15583 w->base_line_number = Qnil;
15584
15585 /* Determine the window start relative to point. */
15586 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15587 it.current_y = it.last_visible_y;
15588 if (centering_position < 0)
15589 {
15590 int margin =
15591 scroll_margin > 0
15592 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15593 : 0;
15594 EMACS_INT margin_pos = CHARPOS (startp);
15595 Lisp_Object aggressive;
15596 int scrolling_up;
15597
15598 /* If there is a scroll margin at the top of the window, find
15599 its character position. */
15600 if (margin
15601 /* Cannot call start_display if startp is not in the
15602 accessible region of the buffer. This can happen when we
15603 have just switched to a different buffer and/or changed
15604 its restriction. In that case, startp is initialized to
15605 the character position 1 (BEGV) because we did not yet
15606 have chance to display the buffer even once. */
15607 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15608 {
15609 struct it it1;
15610 void *it1data = NULL;
15611
15612 SAVE_IT (it1, it, it1data);
15613 start_display (&it1, w, startp);
15614 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15615 margin_pos = IT_CHARPOS (it1);
15616 RESTORE_IT (&it, &it, it1data);
15617 }
15618 scrolling_up = PT > margin_pos;
15619 aggressive =
15620 scrolling_up
15621 ? BVAR (current_buffer, scroll_up_aggressively)
15622 : BVAR (current_buffer, scroll_down_aggressively);
15623
15624 if (!MINI_WINDOW_P (w)
15625 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15626 {
15627 int pt_offset = 0;
15628
15629 /* Setting scroll-conservatively overrides
15630 scroll-*-aggressively. */
15631 if (!scroll_conservatively && NUMBERP (aggressive))
15632 {
15633 double float_amount = XFLOATINT (aggressive);
15634
15635 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15636 if (pt_offset == 0 && float_amount > 0)
15637 pt_offset = 1;
15638 if (pt_offset && margin > 0)
15639 margin -= 1;
15640 }
15641 /* Compute how much to move the window start backward from
15642 point so that point will be displayed where the user
15643 wants it. */
15644 if (scrolling_up)
15645 {
15646 centering_position = it.last_visible_y;
15647 if (pt_offset)
15648 centering_position -= pt_offset;
15649 centering_position -=
15650 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15651 + WINDOW_HEADER_LINE_HEIGHT (w);
15652 /* Don't let point enter the scroll margin near top of
15653 the window. */
15654 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15655 centering_position = margin * FRAME_LINE_HEIGHT (f);
15656 }
15657 else
15658 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15659 }
15660 else
15661 /* Set the window start half the height of the window backward
15662 from point. */
15663 centering_position = window_box_height (w) / 2;
15664 }
15665 move_it_vertically_backward (&it, centering_position);
15666
15667 xassert (IT_CHARPOS (it) >= BEGV);
15668
15669 /* The function move_it_vertically_backward may move over more
15670 than the specified y-distance. If it->w is small, e.g. a
15671 mini-buffer window, we may end up in front of the window's
15672 display area. Start displaying at the start of the line
15673 containing PT in this case. */
15674 if (it.current_y <= 0)
15675 {
15676 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15677 move_it_vertically_backward (&it, 0);
15678 it.current_y = 0;
15679 }
15680
15681 it.current_x = it.hpos = 0;
15682
15683 /* Set the window start position here explicitly, to avoid an
15684 infinite loop in case the functions in window-scroll-functions
15685 get errors. */
15686 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15687
15688 /* Run scroll hooks. */
15689 startp = run_window_scroll_functions (window, it.current.pos);
15690
15691 /* Redisplay the window. */
15692 if (!current_matrix_up_to_date_p
15693 || windows_or_buffers_changed
15694 || cursor_type_changed
15695 /* Don't use try_window_reusing_current_matrix in this case
15696 because it can have changed the buffer. */
15697 || !NILP (Vwindow_scroll_functions)
15698 || !just_this_one_p
15699 || MINI_WINDOW_P (w)
15700 || !(used_current_matrix_p
15701 = try_window_reusing_current_matrix (w)))
15702 try_window (window, startp, 0);
15703
15704 /* If new fonts have been loaded (due to fontsets), give up. We
15705 have to start a new redisplay since we need to re-adjust glyph
15706 matrices. */
15707 if (fonts_changed_p)
15708 goto need_larger_matrices;
15709
15710 /* If cursor did not appear assume that the middle of the window is
15711 in the first line of the window. Do it again with the next line.
15712 (Imagine a window of height 100, displaying two lines of height
15713 60. Moving back 50 from it->last_visible_y will end in the first
15714 line.) */
15715 if (w->cursor.vpos < 0)
15716 {
15717 if (!NILP (w->window_end_valid)
15718 && PT >= Z - XFASTINT (w->window_end_pos))
15719 {
15720 clear_glyph_matrix (w->desired_matrix);
15721 move_it_by_lines (&it, 1);
15722 try_window (window, it.current.pos, 0);
15723 }
15724 else if (PT < IT_CHARPOS (it))
15725 {
15726 clear_glyph_matrix (w->desired_matrix);
15727 move_it_by_lines (&it, -1);
15728 try_window (window, it.current.pos, 0);
15729 }
15730 else
15731 {
15732 /* Not much we can do about it. */
15733 }
15734 }
15735
15736 /* Consider the following case: Window starts at BEGV, there is
15737 invisible, intangible text at BEGV, so that display starts at
15738 some point START > BEGV. It can happen that we are called with
15739 PT somewhere between BEGV and START. Try to handle that case. */
15740 if (w->cursor.vpos < 0)
15741 {
15742 struct glyph_row *row = w->current_matrix->rows;
15743 if (row->mode_line_p)
15744 ++row;
15745 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15746 }
15747
15748 if (!cursor_row_fully_visible_p (w, 0, 0))
15749 {
15750 /* If vscroll is enabled, disable it and try again. */
15751 if (w->vscroll)
15752 {
15753 w->vscroll = 0;
15754 clear_glyph_matrix (w->desired_matrix);
15755 goto recenter;
15756 }
15757
15758 /* Users who set scroll-conservatively to a large number want
15759 point just above/below the scroll margin. If we ended up
15760 with point's row partially visible, move the window start to
15761 make that row fully visible and out of the margin. */
15762 if (scroll_conservatively > SCROLL_LIMIT)
15763 {
15764 int margin =
15765 scroll_margin > 0
15766 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15767 : 0;
15768 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15769
15770 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15771 clear_glyph_matrix (w->desired_matrix);
15772 if (1 == try_window (window, it.current.pos,
15773 TRY_WINDOW_CHECK_MARGINS))
15774 goto done;
15775 }
15776
15777 /* If centering point failed to make the whole line visible,
15778 put point at the top instead. That has to make the whole line
15779 visible, if it can be done. */
15780 if (centering_position == 0)
15781 goto done;
15782
15783 clear_glyph_matrix (w->desired_matrix);
15784 centering_position = 0;
15785 goto recenter;
15786 }
15787
15788 done:
15789
15790 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15791 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15792 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15793 ? Qt : Qnil);
15794
15795 /* Display the mode line, if we must. */
15796 if ((update_mode_line
15797 /* If window not full width, must redo its mode line
15798 if (a) the window to its side is being redone and
15799 (b) we do a frame-based redisplay. This is a consequence
15800 of how inverted lines are drawn in frame-based redisplay. */
15801 || (!just_this_one_p
15802 && !FRAME_WINDOW_P (f)
15803 && !WINDOW_FULL_WIDTH_P (w))
15804 /* Line number to display. */
15805 || INTEGERP (w->base_line_pos)
15806 /* Column number is displayed and different from the one displayed. */
15807 || (!NILP (w->column_number_displayed)
15808 && (XFASTINT (w->column_number_displayed) != current_column ())))
15809 /* This means that the window has a mode line. */
15810 && (WINDOW_WANTS_MODELINE_P (w)
15811 || WINDOW_WANTS_HEADER_LINE_P (w)))
15812 {
15813 display_mode_lines (w);
15814
15815 /* If mode line height has changed, arrange for a thorough
15816 immediate redisplay using the correct mode line height. */
15817 if (WINDOW_WANTS_MODELINE_P (w)
15818 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15819 {
15820 fonts_changed_p = 1;
15821 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15822 = DESIRED_MODE_LINE_HEIGHT (w);
15823 }
15824
15825 /* If header line height has changed, arrange for a thorough
15826 immediate redisplay using the correct header line height. */
15827 if (WINDOW_WANTS_HEADER_LINE_P (w)
15828 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15829 {
15830 fonts_changed_p = 1;
15831 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15832 = DESIRED_HEADER_LINE_HEIGHT (w);
15833 }
15834
15835 if (fonts_changed_p)
15836 goto need_larger_matrices;
15837 }
15838
15839 if (!line_number_displayed
15840 && !BUFFERP (w->base_line_pos))
15841 {
15842 w->base_line_pos = Qnil;
15843 w->base_line_number = Qnil;
15844 }
15845
15846 finish_menu_bars:
15847
15848 /* When we reach a frame's selected window, redo the frame's menu bar. */
15849 if (update_mode_line
15850 && EQ (FRAME_SELECTED_WINDOW (f), window))
15851 {
15852 int redisplay_menu_p = 0;
15853
15854 if (FRAME_WINDOW_P (f))
15855 {
15856 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15857 || defined (HAVE_NS) || defined (USE_GTK)
15858 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15859 #else
15860 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15861 #endif
15862 }
15863 else
15864 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15865
15866 if (redisplay_menu_p)
15867 display_menu_bar (w);
15868
15869 #ifdef HAVE_WINDOW_SYSTEM
15870 if (FRAME_WINDOW_P (f))
15871 {
15872 #if defined (USE_GTK) || defined (HAVE_NS)
15873 if (FRAME_EXTERNAL_TOOL_BAR (f))
15874 redisplay_tool_bar (f);
15875 #else
15876 if (WINDOWP (f->tool_bar_window)
15877 && (FRAME_TOOL_BAR_LINES (f) > 0
15878 || !NILP (Vauto_resize_tool_bars))
15879 && redisplay_tool_bar (f))
15880 ignore_mouse_drag_p = 1;
15881 #endif
15882 }
15883 #endif
15884 }
15885
15886 #ifdef HAVE_WINDOW_SYSTEM
15887 if (FRAME_WINDOW_P (f)
15888 && update_window_fringes (w, (just_this_one_p
15889 || (!used_current_matrix_p && !overlay_arrow_seen)
15890 || w->pseudo_window_p)))
15891 {
15892 update_begin (f);
15893 BLOCK_INPUT;
15894 if (draw_window_fringes (w, 1))
15895 x_draw_vertical_border (w);
15896 UNBLOCK_INPUT;
15897 update_end (f);
15898 }
15899 #endif /* HAVE_WINDOW_SYSTEM */
15900
15901 /* We go to this label, with fonts_changed_p nonzero,
15902 if it is necessary to try again using larger glyph matrices.
15903 We have to redeem the scroll bar even in this case,
15904 because the loop in redisplay_internal expects that. */
15905 need_larger_matrices:
15906 ;
15907 finish_scroll_bars:
15908
15909 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15910 {
15911 /* Set the thumb's position and size. */
15912 set_vertical_scroll_bar (w);
15913
15914 /* Note that we actually used the scroll bar attached to this
15915 window, so it shouldn't be deleted at the end of redisplay. */
15916 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15917 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15918 }
15919
15920 /* Restore current_buffer and value of point in it. The window
15921 update may have changed the buffer, so first make sure `opoint'
15922 is still valid (Bug#6177). */
15923 if (CHARPOS (opoint) < BEGV)
15924 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15925 else if (CHARPOS (opoint) > ZV)
15926 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15927 else
15928 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15929
15930 set_buffer_internal_1 (old);
15931 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15932 shorter. This can be caused by log truncation in *Messages*. */
15933 if (CHARPOS (lpoint) <= ZV)
15934 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15935
15936 unbind_to (count, Qnil);
15937 }
15938
15939
15940 /* Build the complete desired matrix of WINDOW with a window start
15941 buffer position POS.
15942
15943 Value is 1 if successful. It is zero if fonts were loaded during
15944 redisplay which makes re-adjusting glyph matrices necessary, and -1
15945 if point would appear in the scroll margins.
15946 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15947 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15948 set in FLAGS.) */
15949
15950 int
15951 try_window (Lisp_Object window, struct text_pos pos, int flags)
15952 {
15953 struct window *w = XWINDOW (window);
15954 struct it it;
15955 struct glyph_row *last_text_row = NULL;
15956 struct frame *f = XFRAME (w->frame);
15957
15958 /* Make POS the new window start. */
15959 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15960
15961 /* Mark cursor position as unknown. No overlay arrow seen. */
15962 w->cursor.vpos = -1;
15963 overlay_arrow_seen = 0;
15964
15965 /* Initialize iterator and info to start at POS. */
15966 start_display (&it, w, pos);
15967
15968 /* Display all lines of W. */
15969 while (it.current_y < it.last_visible_y)
15970 {
15971 if (display_line (&it))
15972 last_text_row = it.glyph_row - 1;
15973 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15974 return 0;
15975 }
15976
15977 /* Don't let the cursor end in the scroll margins. */
15978 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15979 && !MINI_WINDOW_P (w))
15980 {
15981 int this_scroll_margin;
15982
15983 if (scroll_margin > 0)
15984 {
15985 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15986 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15987 }
15988 else
15989 this_scroll_margin = 0;
15990
15991 if ((w->cursor.y >= 0 /* not vscrolled */
15992 && w->cursor.y < this_scroll_margin
15993 && CHARPOS (pos) > BEGV
15994 && IT_CHARPOS (it) < ZV)
15995 /* rms: considering make_cursor_line_fully_visible_p here
15996 seems to give wrong results. We don't want to recenter
15997 when the last line is partly visible, we want to allow
15998 that case to be handled in the usual way. */
15999 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16000 {
16001 w->cursor.vpos = -1;
16002 clear_glyph_matrix (w->desired_matrix);
16003 return -1;
16004 }
16005 }
16006
16007 /* If bottom moved off end of frame, change mode line percentage. */
16008 if (XFASTINT (w->window_end_pos) <= 0
16009 && Z != IT_CHARPOS (it))
16010 w->update_mode_line = Qt;
16011
16012 /* Set window_end_pos to the offset of the last character displayed
16013 on the window from the end of current_buffer. Set
16014 window_end_vpos to its row number. */
16015 if (last_text_row)
16016 {
16017 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16018 w->window_end_bytepos
16019 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16020 w->window_end_pos
16021 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16022 w->window_end_vpos
16023 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16024 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16025 ->displays_text_p);
16026 }
16027 else
16028 {
16029 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16030 w->window_end_pos = make_number (Z - ZV);
16031 w->window_end_vpos = make_number (0);
16032 }
16033
16034 /* But that is not valid info until redisplay finishes. */
16035 w->window_end_valid = Qnil;
16036 return 1;
16037 }
16038
16039
16040 \f
16041 /************************************************************************
16042 Window redisplay reusing current matrix when buffer has not changed
16043 ************************************************************************/
16044
16045 /* Try redisplay of window W showing an unchanged buffer with a
16046 different window start than the last time it was displayed by
16047 reusing its current matrix. Value is non-zero if successful.
16048 W->start is the new window start. */
16049
16050 static int
16051 try_window_reusing_current_matrix (struct window *w)
16052 {
16053 struct frame *f = XFRAME (w->frame);
16054 struct glyph_row *bottom_row;
16055 struct it it;
16056 struct run run;
16057 struct text_pos start, new_start;
16058 int nrows_scrolled, i;
16059 struct glyph_row *last_text_row;
16060 struct glyph_row *last_reused_text_row;
16061 struct glyph_row *start_row;
16062 int start_vpos, min_y, max_y;
16063
16064 #if GLYPH_DEBUG
16065 if (inhibit_try_window_reusing)
16066 return 0;
16067 #endif
16068
16069 if (/* This function doesn't handle terminal frames. */
16070 !FRAME_WINDOW_P (f)
16071 /* Don't try to reuse the display if windows have been split
16072 or such. */
16073 || windows_or_buffers_changed
16074 || cursor_type_changed)
16075 return 0;
16076
16077 /* Can't do this if region may have changed. */
16078 if ((!NILP (Vtransient_mark_mode)
16079 && !NILP (BVAR (current_buffer, mark_active)))
16080 || !NILP (w->region_showing)
16081 || !NILP (Vshow_trailing_whitespace))
16082 return 0;
16083
16084 /* If top-line visibility has changed, give up. */
16085 if (WINDOW_WANTS_HEADER_LINE_P (w)
16086 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16087 return 0;
16088
16089 /* Give up if old or new display is scrolled vertically. We could
16090 make this function handle this, but right now it doesn't. */
16091 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16092 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16093 return 0;
16094
16095 /* The variable new_start now holds the new window start. The old
16096 start `start' can be determined from the current matrix. */
16097 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16098 start = start_row->minpos;
16099 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16100
16101 /* Clear the desired matrix for the display below. */
16102 clear_glyph_matrix (w->desired_matrix);
16103
16104 if (CHARPOS (new_start) <= CHARPOS (start))
16105 {
16106 /* Don't use this method if the display starts with an ellipsis
16107 displayed for invisible text. It's not easy to handle that case
16108 below, and it's certainly not worth the effort since this is
16109 not a frequent case. */
16110 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16111 return 0;
16112
16113 IF_DEBUG (debug_method_add (w, "twu1"));
16114
16115 /* Display up to a row that can be reused. The variable
16116 last_text_row is set to the last row displayed that displays
16117 text. Note that it.vpos == 0 if or if not there is a
16118 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16119 start_display (&it, w, new_start);
16120 w->cursor.vpos = -1;
16121 last_text_row = last_reused_text_row = NULL;
16122
16123 while (it.current_y < it.last_visible_y
16124 && !fonts_changed_p)
16125 {
16126 /* If we have reached into the characters in the START row,
16127 that means the line boundaries have changed. So we
16128 can't start copying with the row START. Maybe it will
16129 work to start copying with the following row. */
16130 while (IT_CHARPOS (it) > CHARPOS (start))
16131 {
16132 /* Advance to the next row as the "start". */
16133 start_row++;
16134 start = start_row->minpos;
16135 /* If there are no more rows to try, or just one, give up. */
16136 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16137 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16138 || CHARPOS (start) == ZV)
16139 {
16140 clear_glyph_matrix (w->desired_matrix);
16141 return 0;
16142 }
16143
16144 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16145 }
16146 /* If we have reached alignment, we can copy the rest of the
16147 rows. */
16148 if (IT_CHARPOS (it) == CHARPOS (start)
16149 /* Don't accept "alignment" inside a display vector,
16150 since start_row could have started in the middle of
16151 that same display vector (thus their character
16152 positions match), and we have no way of telling if
16153 that is the case. */
16154 && it.current.dpvec_index < 0)
16155 break;
16156
16157 if (display_line (&it))
16158 last_text_row = it.glyph_row - 1;
16159
16160 }
16161
16162 /* A value of current_y < last_visible_y means that we stopped
16163 at the previous window start, which in turn means that we
16164 have at least one reusable row. */
16165 if (it.current_y < it.last_visible_y)
16166 {
16167 struct glyph_row *row;
16168
16169 /* IT.vpos always starts from 0; it counts text lines. */
16170 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16171
16172 /* Find PT if not already found in the lines displayed. */
16173 if (w->cursor.vpos < 0)
16174 {
16175 int dy = it.current_y - start_row->y;
16176
16177 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16178 row = row_containing_pos (w, PT, row, NULL, dy);
16179 if (row)
16180 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16181 dy, nrows_scrolled);
16182 else
16183 {
16184 clear_glyph_matrix (w->desired_matrix);
16185 return 0;
16186 }
16187 }
16188
16189 /* Scroll the display. Do it before the current matrix is
16190 changed. The problem here is that update has not yet
16191 run, i.e. part of the current matrix is not up to date.
16192 scroll_run_hook will clear the cursor, and use the
16193 current matrix to get the height of the row the cursor is
16194 in. */
16195 run.current_y = start_row->y;
16196 run.desired_y = it.current_y;
16197 run.height = it.last_visible_y - it.current_y;
16198
16199 if (run.height > 0 && run.current_y != run.desired_y)
16200 {
16201 update_begin (f);
16202 FRAME_RIF (f)->update_window_begin_hook (w);
16203 FRAME_RIF (f)->clear_window_mouse_face (w);
16204 FRAME_RIF (f)->scroll_run_hook (w, &run);
16205 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16206 update_end (f);
16207 }
16208
16209 /* Shift current matrix down by nrows_scrolled lines. */
16210 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16211 rotate_matrix (w->current_matrix,
16212 start_vpos,
16213 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16214 nrows_scrolled);
16215
16216 /* Disable lines that must be updated. */
16217 for (i = 0; i < nrows_scrolled; ++i)
16218 (start_row + i)->enabled_p = 0;
16219
16220 /* Re-compute Y positions. */
16221 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16222 max_y = it.last_visible_y;
16223 for (row = start_row + nrows_scrolled;
16224 row < bottom_row;
16225 ++row)
16226 {
16227 row->y = it.current_y;
16228 row->visible_height = row->height;
16229
16230 if (row->y < min_y)
16231 row->visible_height -= min_y - row->y;
16232 if (row->y + row->height > max_y)
16233 row->visible_height -= row->y + row->height - max_y;
16234 if (row->fringe_bitmap_periodic_p)
16235 row->redraw_fringe_bitmaps_p = 1;
16236
16237 it.current_y += row->height;
16238
16239 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16240 last_reused_text_row = row;
16241 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16242 break;
16243 }
16244
16245 /* Disable lines in the current matrix which are now
16246 below the window. */
16247 for (++row; row < bottom_row; ++row)
16248 row->enabled_p = row->mode_line_p = 0;
16249 }
16250
16251 /* Update window_end_pos etc.; last_reused_text_row is the last
16252 reused row from the current matrix containing text, if any.
16253 The value of last_text_row is the last displayed line
16254 containing text. */
16255 if (last_reused_text_row)
16256 {
16257 w->window_end_bytepos
16258 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16259 w->window_end_pos
16260 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16261 w->window_end_vpos
16262 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16263 w->current_matrix));
16264 }
16265 else if (last_text_row)
16266 {
16267 w->window_end_bytepos
16268 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16269 w->window_end_pos
16270 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16271 w->window_end_vpos
16272 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16273 }
16274 else
16275 {
16276 /* This window must be completely empty. */
16277 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16278 w->window_end_pos = make_number (Z - ZV);
16279 w->window_end_vpos = make_number (0);
16280 }
16281 w->window_end_valid = Qnil;
16282
16283 /* Update hint: don't try scrolling again in update_window. */
16284 w->desired_matrix->no_scrolling_p = 1;
16285
16286 #if GLYPH_DEBUG
16287 debug_method_add (w, "try_window_reusing_current_matrix 1");
16288 #endif
16289 return 1;
16290 }
16291 else if (CHARPOS (new_start) > CHARPOS (start))
16292 {
16293 struct glyph_row *pt_row, *row;
16294 struct glyph_row *first_reusable_row;
16295 struct glyph_row *first_row_to_display;
16296 int dy;
16297 int yb = window_text_bottom_y (w);
16298
16299 /* Find the row starting at new_start, if there is one. Don't
16300 reuse a partially visible line at the end. */
16301 first_reusable_row = start_row;
16302 while (first_reusable_row->enabled_p
16303 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16304 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16305 < CHARPOS (new_start)))
16306 ++first_reusable_row;
16307
16308 /* Give up if there is no row to reuse. */
16309 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16310 || !first_reusable_row->enabled_p
16311 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16312 != CHARPOS (new_start)))
16313 return 0;
16314
16315 /* We can reuse fully visible rows beginning with
16316 first_reusable_row to the end of the window. Set
16317 first_row_to_display to the first row that cannot be reused.
16318 Set pt_row to the row containing point, if there is any. */
16319 pt_row = NULL;
16320 for (first_row_to_display = first_reusable_row;
16321 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16322 ++first_row_to_display)
16323 {
16324 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16325 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16326 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16327 && first_row_to_display->ends_at_zv_p
16328 && pt_row == NULL)))
16329 pt_row = first_row_to_display;
16330 }
16331
16332 /* Start displaying at the start of first_row_to_display. */
16333 xassert (first_row_to_display->y < yb);
16334 init_to_row_start (&it, w, first_row_to_display);
16335
16336 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16337 - start_vpos);
16338 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16339 - nrows_scrolled);
16340 it.current_y = (first_row_to_display->y - first_reusable_row->y
16341 + WINDOW_HEADER_LINE_HEIGHT (w));
16342
16343 /* Display lines beginning with first_row_to_display in the
16344 desired matrix. Set last_text_row to the last row displayed
16345 that displays text. */
16346 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16347 if (pt_row == NULL)
16348 w->cursor.vpos = -1;
16349 last_text_row = NULL;
16350 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16351 if (display_line (&it))
16352 last_text_row = it.glyph_row - 1;
16353
16354 /* If point is in a reused row, adjust y and vpos of the cursor
16355 position. */
16356 if (pt_row)
16357 {
16358 w->cursor.vpos -= nrows_scrolled;
16359 w->cursor.y -= first_reusable_row->y - start_row->y;
16360 }
16361
16362 /* Give up if point isn't in a row displayed or reused. (This
16363 also handles the case where w->cursor.vpos < nrows_scrolled
16364 after the calls to display_line, which can happen with scroll
16365 margins. See bug#1295.) */
16366 if (w->cursor.vpos < 0)
16367 {
16368 clear_glyph_matrix (w->desired_matrix);
16369 return 0;
16370 }
16371
16372 /* Scroll the display. */
16373 run.current_y = first_reusable_row->y;
16374 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16375 run.height = it.last_visible_y - run.current_y;
16376 dy = run.current_y - run.desired_y;
16377
16378 if (run.height)
16379 {
16380 update_begin (f);
16381 FRAME_RIF (f)->update_window_begin_hook (w);
16382 FRAME_RIF (f)->clear_window_mouse_face (w);
16383 FRAME_RIF (f)->scroll_run_hook (w, &run);
16384 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16385 update_end (f);
16386 }
16387
16388 /* Adjust Y positions of reused rows. */
16389 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16390 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16391 max_y = it.last_visible_y;
16392 for (row = first_reusable_row; row < first_row_to_display; ++row)
16393 {
16394 row->y -= dy;
16395 row->visible_height = row->height;
16396 if (row->y < min_y)
16397 row->visible_height -= min_y - row->y;
16398 if (row->y + row->height > max_y)
16399 row->visible_height -= row->y + row->height - max_y;
16400 if (row->fringe_bitmap_periodic_p)
16401 row->redraw_fringe_bitmaps_p = 1;
16402 }
16403
16404 /* Scroll the current matrix. */
16405 xassert (nrows_scrolled > 0);
16406 rotate_matrix (w->current_matrix,
16407 start_vpos,
16408 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16409 -nrows_scrolled);
16410
16411 /* Disable rows not reused. */
16412 for (row -= nrows_scrolled; row < bottom_row; ++row)
16413 row->enabled_p = 0;
16414
16415 /* Point may have moved to a different line, so we cannot assume that
16416 the previous cursor position is valid; locate the correct row. */
16417 if (pt_row)
16418 {
16419 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16420 row < bottom_row
16421 && PT >= MATRIX_ROW_END_CHARPOS (row)
16422 && !row->ends_at_zv_p;
16423 row++)
16424 {
16425 w->cursor.vpos++;
16426 w->cursor.y = row->y;
16427 }
16428 if (row < bottom_row)
16429 {
16430 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16431 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16432
16433 /* Can't use this optimization with bidi-reordered glyph
16434 rows, unless cursor is already at point. */
16435 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16436 {
16437 if (!(w->cursor.hpos >= 0
16438 && w->cursor.hpos < row->used[TEXT_AREA]
16439 && BUFFERP (glyph->object)
16440 && glyph->charpos == PT))
16441 return 0;
16442 }
16443 else
16444 for (; glyph < end
16445 && (!BUFFERP (glyph->object)
16446 || glyph->charpos < PT);
16447 glyph++)
16448 {
16449 w->cursor.hpos++;
16450 w->cursor.x += glyph->pixel_width;
16451 }
16452 }
16453 }
16454
16455 /* Adjust window end. A null value of last_text_row means that
16456 the window end is in reused rows which in turn means that
16457 only its vpos can have changed. */
16458 if (last_text_row)
16459 {
16460 w->window_end_bytepos
16461 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16462 w->window_end_pos
16463 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16464 w->window_end_vpos
16465 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16466 }
16467 else
16468 {
16469 w->window_end_vpos
16470 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16471 }
16472
16473 w->window_end_valid = Qnil;
16474 w->desired_matrix->no_scrolling_p = 1;
16475
16476 #if GLYPH_DEBUG
16477 debug_method_add (w, "try_window_reusing_current_matrix 2");
16478 #endif
16479 return 1;
16480 }
16481
16482 return 0;
16483 }
16484
16485
16486 \f
16487 /************************************************************************
16488 Window redisplay reusing current matrix when buffer has changed
16489 ************************************************************************/
16490
16491 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16492 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16493 EMACS_INT *, EMACS_INT *);
16494 static struct glyph_row *
16495 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16496 struct glyph_row *);
16497
16498
16499 /* Return the last row in MATRIX displaying text. If row START is
16500 non-null, start searching with that row. IT gives the dimensions
16501 of the display. Value is null if matrix is empty; otherwise it is
16502 a pointer to the row found. */
16503
16504 static struct glyph_row *
16505 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16506 struct glyph_row *start)
16507 {
16508 struct glyph_row *row, *row_found;
16509
16510 /* Set row_found to the last row in IT->w's current matrix
16511 displaying text. The loop looks funny but think of partially
16512 visible lines. */
16513 row_found = NULL;
16514 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16515 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16516 {
16517 xassert (row->enabled_p);
16518 row_found = row;
16519 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16520 break;
16521 ++row;
16522 }
16523
16524 return row_found;
16525 }
16526
16527
16528 /* Return the last row in the current matrix of W that is not affected
16529 by changes at the start of current_buffer that occurred since W's
16530 current matrix was built. Value is null if no such row exists.
16531
16532 BEG_UNCHANGED us the number of characters unchanged at the start of
16533 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16534 first changed character in current_buffer. Characters at positions <
16535 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16536 when the current matrix was built. */
16537
16538 static struct glyph_row *
16539 find_last_unchanged_at_beg_row (struct window *w)
16540 {
16541 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16542 struct glyph_row *row;
16543 struct glyph_row *row_found = NULL;
16544 int yb = window_text_bottom_y (w);
16545
16546 /* Find the last row displaying unchanged text. */
16547 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16548 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16549 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16550 ++row)
16551 {
16552 if (/* If row ends before first_changed_pos, it is unchanged,
16553 except in some case. */
16554 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16555 /* When row ends in ZV and we write at ZV it is not
16556 unchanged. */
16557 && !row->ends_at_zv_p
16558 /* When first_changed_pos is the end of a continued line,
16559 row is not unchanged because it may be no longer
16560 continued. */
16561 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16562 && (row->continued_p
16563 || row->exact_window_width_line_p)))
16564 row_found = row;
16565
16566 /* Stop if last visible row. */
16567 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16568 break;
16569 }
16570
16571 return row_found;
16572 }
16573
16574
16575 /* Find the first glyph row in the current matrix of W that is not
16576 affected by changes at the end of current_buffer since the
16577 time W's current matrix was built.
16578
16579 Return in *DELTA the number of chars by which buffer positions in
16580 unchanged text at the end of current_buffer must be adjusted.
16581
16582 Return in *DELTA_BYTES the corresponding number of bytes.
16583
16584 Value is null if no such row exists, i.e. all rows are affected by
16585 changes. */
16586
16587 static struct glyph_row *
16588 find_first_unchanged_at_end_row (struct window *w,
16589 EMACS_INT *delta, EMACS_INT *delta_bytes)
16590 {
16591 struct glyph_row *row;
16592 struct glyph_row *row_found = NULL;
16593
16594 *delta = *delta_bytes = 0;
16595
16596 /* Display must not have been paused, otherwise the current matrix
16597 is not up to date. */
16598 eassert (!NILP (w->window_end_valid));
16599
16600 /* A value of window_end_pos >= END_UNCHANGED means that the window
16601 end is in the range of changed text. If so, there is no
16602 unchanged row at the end of W's current matrix. */
16603 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16604 return NULL;
16605
16606 /* Set row to the last row in W's current matrix displaying text. */
16607 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16608
16609 /* If matrix is entirely empty, no unchanged row exists. */
16610 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16611 {
16612 /* The value of row is the last glyph row in the matrix having a
16613 meaningful buffer position in it. The end position of row
16614 corresponds to window_end_pos. This allows us to translate
16615 buffer positions in the current matrix to current buffer
16616 positions for characters not in changed text. */
16617 EMACS_INT Z_old =
16618 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16619 EMACS_INT Z_BYTE_old =
16620 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16621 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16622 struct glyph_row *first_text_row
16623 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16624
16625 *delta = Z - Z_old;
16626 *delta_bytes = Z_BYTE - Z_BYTE_old;
16627
16628 /* Set last_unchanged_pos to the buffer position of the last
16629 character in the buffer that has not been changed. Z is the
16630 index + 1 of the last character in current_buffer, i.e. by
16631 subtracting END_UNCHANGED we get the index of the last
16632 unchanged character, and we have to add BEG to get its buffer
16633 position. */
16634 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16635 last_unchanged_pos_old = last_unchanged_pos - *delta;
16636
16637 /* Search backward from ROW for a row displaying a line that
16638 starts at a minimum position >= last_unchanged_pos_old. */
16639 for (; row > first_text_row; --row)
16640 {
16641 /* This used to abort, but it can happen.
16642 It is ok to just stop the search instead here. KFS. */
16643 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16644 break;
16645
16646 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16647 row_found = row;
16648 }
16649 }
16650
16651 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16652
16653 return row_found;
16654 }
16655
16656
16657 /* Make sure that glyph rows in the current matrix of window W
16658 reference the same glyph memory as corresponding rows in the
16659 frame's frame matrix. This function is called after scrolling W's
16660 current matrix on a terminal frame in try_window_id and
16661 try_window_reusing_current_matrix. */
16662
16663 static void
16664 sync_frame_with_window_matrix_rows (struct window *w)
16665 {
16666 struct frame *f = XFRAME (w->frame);
16667 struct glyph_row *window_row, *window_row_end, *frame_row;
16668
16669 /* Preconditions: W must be a leaf window and full-width. Its frame
16670 must have a frame matrix. */
16671 xassert (NILP (w->hchild) && NILP (w->vchild));
16672 xassert (WINDOW_FULL_WIDTH_P (w));
16673 xassert (!FRAME_WINDOW_P (f));
16674
16675 /* If W is a full-width window, glyph pointers in W's current matrix
16676 have, by definition, to be the same as glyph pointers in the
16677 corresponding frame matrix. Note that frame matrices have no
16678 marginal areas (see build_frame_matrix). */
16679 window_row = w->current_matrix->rows;
16680 window_row_end = window_row + w->current_matrix->nrows;
16681 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16682 while (window_row < window_row_end)
16683 {
16684 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16685 struct glyph *end = window_row->glyphs[LAST_AREA];
16686
16687 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16688 frame_row->glyphs[TEXT_AREA] = start;
16689 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16690 frame_row->glyphs[LAST_AREA] = end;
16691
16692 /* Disable frame rows whose corresponding window rows have
16693 been disabled in try_window_id. */
16694 if (!window_row->enabled_p)
16695 frame_row->enabled_p = 0;
16696
16697 ++window_row, ++frame_row;
16698 }
16699 }
16700
16701
16702 /* Find the glyph row in window W containing CHARPOS. Consider all
16703 rows between START and END (not inclusive). END null means search
16704 all rows to the end of the display area of W. Value is the row
16705 containing CHARPOS or null. */
16706
16707 struct glyph_row *
16708 row_containing_pos (struct window *w, EMACS_INT charpos,
16709 struct glyph_row *start, struct glyph_row *end, int dy)
16710 {
16711 struct glyph_row *row = start;
16712 struct glyph_row *best_row = NULL;
16713 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16714 int last_y;
16715
16716 /* If we happen to start on a header-line, skip that. */
16717 if (row->mode_line_p)
16718 ++row;
16719
16720 if ((end && row >= end) || !row->enabled_p)
16721 return NULL;
16722
16723 last_y = window_text_bottom_y (w) - dy;
16724
16725 while (1)
16726 {
16727 /* Give up if we have gone too far. */
16728 if (end && row >= end)
16729 return NULL;
16730 /* This formerly returned if they were equal.
16731 I think that both quantities are of a "last plus one" type;
16732 if so, when they are equal, the row is within the screen. -- rms. */
16733 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16734 return NULL;
16735
16736 /* If it is in this row, return this row. */
16737 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16738 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16739 /* The end position of a row equals the start
16740 position of the next row. If CHARPOS is there, we
16741 would rather display it in the next line, except
16742 when this line ends in ZV. */
16743 && !row->ends_at_zv_p
16744 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16745 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16746 {
16747 struct glyph *g;
16748
16749 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16750 || (!best_row && !row->continued_p))
16751 return row;
16752 /* In bidi-reordered rows, there could be several rows
16753 occluding point, all of them belonging to the same
16754 continued line. We need to find the row which fits
16755 CHARPOS the best. */
16756 for (g = row->glyphs[TEXT_AREA];
16757 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16758 g++)
16759 {
16760 if (!STRINGP (g->object))
16761 {
16762 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16763 {
16764 mindif = eabs (g->charpos - charpos);
16765 best_row = row;
16766 /* Exact match always wins. */
16767 if (mindif == 0)
16768 return best_row;
16769 }
16770 }
16771 }
16772 }
16773 else if (best_row && !row->continued_p)
16774 return best_row;
16775 ++row;
16776 }
16777 }
16778
16779
16780 /* Try to redisplay window W by reusing its existing display. W's
16781 current matrix must be up to date when this function is called,
16782 i.e. window_end_valid must not be nil.
16783
16784 Value is
16785
16786 1 if display has been updated
16787 0 if otherwise unsuccessful
16788 -1 if redisplay with same window start is known not to succeed
16789
16790 The following steps are performed:
16791
16792 1. Find the last row in the current matrix of W that is not
16793 affected by changes at the start of current_buffer. If no such row
16794 is found, give up.
16795
16796 2. Find the first row in W's current matrix that is not affected by
16797 changes at the end of current_buffer. Maybe there is no such row.
16798
16799 3. Display lines beginning with the row + 1 found in step 1 to the
16800 row found in step 2 or, if step 2 didn't find a row, to the end of
16801 the window.
16802
16803 4. If cursor is not known to appear on the window, give up.
16804
16805 5. If display stopped at the row found in step 2, scroll the
16806 display and current matrix as needed.
16807
16808 6. Maybe display some lines at the end of W, if we must. This can
16809 happen under various circumstances, like a partially visible line
16810 becoming fully visible, or because newly displayed lines are displayed
16811 in smaller font sizes.
16812
16813 7. Update W's window end information. */
16814
16815 static int
16816 try_window_id (struct window *w)
16817 {
16818 struct frame *f = XFRAME (w->frame);
16819 struct glyph_matrix *current_matrix = w->current_matrix;
16820 struct glyph_matrix *desired_matrix = w->desired_matrix;
16821 struct glyph_row *last_unchanged_at_beg_row;
16822 struct glyph_row *first_unchanged_at_end_row;
16823 struct glyph_row *row;
16824 struct glyph_row *bottom_row;
16825 int bottom_vpos;
16826 struct it it;
16827 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16828 int dvpos, dy;
16829 struct text_pos start_pos;
16830 struct run run;
16831 int first_unchanged_at_end_vpos = 0;
16832 struct glyph_row *last_text_row, *last_text_row_at_end;
16833 struct text_pos start;
16834 EMACS_INT first_changed_charpos, last_changed_charpos;
16835
16836 #if GLYPH_DEBUG
16837 if (inhibit_try_window_id)
16838 return 0;
16839 #endif
16840
16841 /* This is handy for debugging. */
16842 #if 0
16843 #define GIVE_UP(X) \
16844 do { \
16845 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16846 return 0; \
16847 } while (0)
16848 #else
16849 #define GIVE_UP(X) return 0
16850 #endif
16851
16852 SET_TEXT_POS_FROM_MARKER (start, w->start);
16853
16854 /* Don't use this for mini-windows because these can show
16855 messages and mini-buffers, and we don't handle that here. */
16856 if (MINI_WINDOW_P (w))
16857 GIVE_UP (1);
16858
16859 /* This flag is used to prevent redisplay optimizations. */
16860 if (windows_or_buffers_changed || cursor_type_changed)
16861 GIVE_UP (2);
16862
16863 /* Verify that narrowing has not changed.
16864 Also verify that we were not told to prevent redisplay optimizations.
16865 It would be nice to further
16866 reduce the number of cases where this prevents try_window_id. */
16867 if (current_buffer->clip_changed
16868 || current_buffer->prevent_redisplay_optimizations_p)
16869 GIVE_UP (3);
16870
16871 /* Window must either use window-based redisplay or be full width. */
16872 if (!FRAME_WINDOW_P (f)
16873 && (!FRAME_LINE_INS_DEL_OK (f)
16874 || !WINDOW_FULL_WIDTH_P (w)))
16875 GIVE_UP (4);
16876
16877 /* Give up if point is known NOT to appear in W. */
16878 if (PT < CHARPOS (start))
16879 GIVE_UP (5);
16880
16881 /* Another way to prevent redisplay optimizations. */
16882 if (XFASTINT (w->last_modified) == 0)
16883 GIVE_UP (6);
16884
16885 /* Verify that window is not hscrolled. */
16886 if (XFASTINT (w->hscroll) != 0)
16887 GIVE_UP (7);
16888
16889 /* Verify that display wasn't paused. */
16890 if (NILP (w->window_end_valid))
16891 GIVE_UP (8);
16892
16893 /* Can't use this if highlighting a region because a cursor movement
16894 will do more than just set the cursor. */
16895 if (!NILP (Vtransient_mark_mode)
16896 && !NILP (BVAR (current_buffer, mark_active)))
16897 GIVE_UP (9);
16898
16899 /* Likewise if highlighting trailing whitespace. */
16900 if (!NILP (Vshow_trailing_whitespace))
16901 GIVE_UP (11);
16902
16903 /* Likewise if showing a region. */
16904 if (!NILP (w->region_showing))
16905 GIVE_UP (10);
16906
16907 /* Can't use this if overlay arrow position and/or string have
16908 changed. */
16909 if (overlay_arrows_changed_p ())
16910 GIVE_UP (12);
16911
16912 /* When word-wrap is on, adding a space to the first word of a
16913 wrapped line can change the wrap position, altering the line
16914 above it. It might be worthwhile to handle this more
16915 intelligently, but for now just redisplay from scratch. */
16916 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16917 GIVE_UP (21);
16918
16919 /* Under bidi reordering, adding or deleting a character in the
16920 beginning of a paragraph, before the first strong directional
16921 character, can change the base direction of the paragraph (unless
16922 the buffer specifies a fixed paragraph direction), which will
16923 require to redisplay the whole paragraph. It might be worthwhile
16924 to find the paragraph limits and widen the range of redisplayed
16925 lines to that, but for now just give up this optimization and
16926 redisplay from scratch. */
16927 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16928 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16929 GIVE_UP (22);
16930
16931 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16932 only if buffer has really changed. The reason is that the gap is
16933 initially at Z for freshly visited files. The code below would
16934 set end_unchanged to 0 in that case. */
16935 if (MODIFF > SAVE_MODIFF
16936 /* This seems to happen sometimes after saving a buffer. */
16937 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16938 {
16939 if (GPT - BEG < BEG_UNCHANGED)
16940 BEG_UNCHANGED = GPT - BEG;
16941 if (Z - GPT < END_UNCHANGED)
16942 END_UNCHANGED = Z - GPT;
16943 }
16944
16945 /* The position of the first and last character that has been changed. */
16946 first_changed_charpos = BEG + BEG_UNCHANGED;
16947 last_changed_charpos = Z - END_UNCHANGED;
16948
16949 /* If window starts after a line end, and the last change is in
16950 front of that newline, then changes don't affect the display.
16951 This case happens with stealth-fontification. Note that although
16952 the display is unchanged, glyph positions in the matrix have to
16953 be adjusted, of course. */
16954 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16955 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16956 && ((last_changed_charpos < CHARPOS (start)
16957 && CHARPOS (start) == BEGV)
16958 || (last_changed_charpos < CHARPOS (start) - 1
16959 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16960 {
16961 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16962 struct glyph_row *r0;
16963
16964 /* Compute how many chars/bytes have been added to or removed
16965 from the buffer. */
16966 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16967 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16968 Z_delta = Z - Z_old;
16969 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16970
16971 /* Give up if PT is not in the window. Note that it already has
16972 been checked at the start of try_window_id that PT is not in
16973 front of the window start. */
16974 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16975 GIVE_UP (13);
16976
16977 /* If window start is unchanged, we can reuse the whole matrix
16978 as is, after adjusting glyph positions. No need to compute
16979 the window end again, since its offset from Z hasn't changed. */
16980 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16981 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16982 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16983 /* PT must not be in a partially visible line. */
16984 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16985 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16986 {
16987 /* Adjust positions in the glyph matrix. */
16988 if (Z_delta || Z_delta_bytes)
16989 {
16990 struct glyph_row *r1
16991 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16992 increment_matrix_positions (w->current_matrix,
16993 MATRIX_ROW_VPOS (r0, current_matrix),
16994 MATRIX_ROW_VPOS (r1, current_matrix),
16995 Z_delta, Z_delta_bytes);
16996 }
16997
16998 /* Set the cursor. */
16999 row = row_containing_pos (w, PT, r0, NULL, 0);
17000 if (row)
17001 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17002 else
17003 abort ();
17004 return 1;
17005 }
17006 }
17007
17008 /* Handle the case that changes are all below what is displayed in
17009 the window, and that PT is in the window. This shortcut cannot
17010 be taken if ZV is visible in the window, and text has been added
17011 there that is visible in the window. */
17012 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17013 /* ZV is not visible in the window, or there are no
17014 changes at ZV, actually. */
17015 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17016 || first_changed_charpos == last_changed_charpos))
17017 {
17018 struct glyph_row *r0;
17019
17020 /* Give up if PT is not in the window. Note that it already has
17021 been checked at the start of try_window_id that PT is not in
17022 front of the window start. */
17023 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17024 GIVE_UP (14);
17025
17026 /* If window start is unchanged, we can reuse the whole matrix
17027 as is, without changing glyph positions since no text has
17028 been added/removed in front of the window end. */
17029 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17030 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17031 /* PT must not be in a partially visible line. */
17032 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17033 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17034 {
17035 /* We have to compute the window end anew since text
17036 could have been added/removed after it. */
17037 w->window_end_pos
17038 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17039 w->window_end_bytepos
17040 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17041
17042 /* Set the cursor. */
17043 row = row_containing_pos (w, PT, r0, NULL, 0);
17044 if (row)
17045 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17046 else
17047 abort ();
17048 return 2;
17049 }
17050 }
17051
17052 /* Give up if window start is in the changed area.
17053
17054 The condition used to read
17055
17056 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17057
17058 but why that was tested escapes me at the moment. */
17059 if (CHARPOS (start) >= first_changed_charpos
17060 && CHARPOS (start) <= last_changed_charpos)
17061 GIVE_UP (15);
17062
17063 /* Check that window start agrees with the start of the first glyph
17064 row in its current matrix. Check this after we know the window
17065 start is not in changed text, otherwise positions would not be
17066 comparable. */
17067 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17068 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17069 GIVE_UP (16);
17070
17071 /* Give up if the window ends in strings. Overlay strings
17072 at the end are difficult to handle, so don't try. */
17073 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17074 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17075 GIVE_UP (20);
17076
17077 /* Compute the position at which we have to start displaying new
17078 lines. Some of the lines at the top of the window might be
17079 reusable because they are not displaying changed text. Find the
17080 last row in W's current matrix not affected by changes at the
17081 start of current_buffer. Value is null if changes start in the
17082 first line of window. */
17083 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17084 if (last_unchanged_at_beg_row)
17085 {
17086 /* Avoid starting to display in the middle of a character, a TAB
17087 for instance. This is easier than to set up the iterator
17088 exactly, and it's not a frequent case, so the additional
17089 effort wouldn't really pay off. */
17090 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17091 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17092 && last_unchanged_at_beg_row > w->current_matrix->rows)
17093 --last_unchanged_at_beg_row;
17094
17095 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17096 GIVE_UP (17);
17097
17098 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17099 GIVE_UP (18);
17100 start_pos = it.current.pos;
17101
17102 /* Start displaying new lines in the desired matrix at the same
17103 vpos we would use in the current matrix, i.e. below
17104 last_unchanged_at_beg_row. */
17105 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17106 current_matrix);
17107 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17108 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17109
17110 xassert (it.hpos == 0 && it.current_x == 0);
17111 }
17112 else
17113 {
17114 /* There are no reusable lines at the start of the window.
17115 Start displaying in the first text line. */
17116 start_display (&it, w, start);
17117 it.vpos = it.first_vpos;
17118 start_pos = it.current.pos;
17119 }
17120
17121 /* Find the first row that is not affected by changes at the end of
17122 the buffer. Value will be null if there is no unchanged row, in
17123 which case we must redisplay to the end of the window. delta
17124 will be set to the value by which buffer positions beginning with
17125 first_unchanged_at_end_row have to be adjusted due to text
17126 changes. */
17127 first_unchanged_at_end_row
17128 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17129 IF_DEBUG (debug_delta = delta);
17130 IF_DEBUG (debug_delta_bytes = delta_bytes);
17131
17132 /* Set stop_pos to the buffer position up to which we will have to
17133 display new lines. If first_unchanged_at_end_row != NULL, this
17134 is the buffer position of the start of the line displayed in that
17135 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17136 that we don't stop at a buffer position. */
17137 stop_pos = 0;
17138 if (first_unchanged_at_end_row)
17139 {
17140 xassert (last_unchanged_at_beg_row == NULL
17141 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17142
17143 /* If this is a continuation line, move forward to the next one
17144 that isn't. Changes in lines above affect this line.
17145 Caution: this may move first_unchanged_at_end_row to a row
17146 not displaying text. */
17147 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17148 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17149 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17150 < it.last_visible_y))
17151 ++first_unchanged_at_end_row;
17152
17153 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17154 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17155 >= it.last_visible_y))
17156 first_unchanged_at_end_row = NULL;
17157 else
17158 {
17159 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17160 + delta);
17161 first_unchanged_at_end_vpos
17162 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17163 xassert (stop_pos >= Z - END_UNCHANGED);
17164 }
17165 }
17166 else if (last_unchanged_at_beg_row == NULL)
17167 GIVE_UP (19);
17168
17169
17170 #if GLYPH_DEBUG
17171
17172 /* Either there is no unchanged row at the end, or the one we have
17173 now displays text. This is a necessary condition for the window
17174 end pos calculation at the end of this function. */
17175 xassert (first_unchanged_at_end_row == NULL
17176 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17177
17178 debug_last_unchanged_at_beg_vpos
17179 = (last_unchanged_at_beg_row
17180 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17181 : -1);
17182 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17183
17184 #endif /* GLYPH_DEBUG != 0 */
17185
17186
17187 /* Display new lines. Set last_text_row to the last new line
17188 displayed which has text on it, i.e. might end up as being the
17189 line where the window_end_vpos is. */
17190 w->cursor.vpos = -1;
17191 last_text_row = NULL;
17192 overlay_arrow_seen = 0;
17193 while (it.current_y < it.last_visible_y
17194 && !fonts_changed_p
17195 && (first_unchanged_at_end_row == NULL
17196 || IT_CHARPOS (it) < stop_pos))
17197 {
17198 if (display_line (&it))
17199 last_text_row = it.glyph_row - 1;
17200 }
17201
17202 if (fonts_changed_p)
17203 return -1;
17204
17205
17206 /* Compute differences in buffer positions, y-positions etc. for
17207 lines reused at the bottom of the window. Compute what we can
17208 scroll. */
17209 if (first_unchanged_at_end_row
17210 /* No lines reused because we displayed everything up to the
17211 bottom of the window. */
17212 && it.current_y < it.last_visible_y)
17213 {
17214 dvpos = (it.vpos
17215 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17216 current_matrix));
17217 dy = it.current_y - first_unchanged_at_end_row->y;
17218 run.current_y = first_unchanged_at_end_row->y;
17219 run.desired_y = run.current_y + dy;
17220 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17221 }
17222 else
17223 {
17224 delta = delta_bytes = dvpos = dy
17225 = run.current_y = run.desired_y = run.height = 0;
17226 first_unchanged_at_end_row = NULL;
17227 }
17228 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17229
17230
17231 /* Find the cursor if not already found. We have to decide whether
17232 PT will appear on this window (it sometimes doesn't, but this is
17233 not a very frequent case.) This decision has to be made before
17234 the current matrix is altered. A value of cursor.vpos < 0 means
17235 that PT is either in one of the lines beginning at
17236 first_unchanged_at_end_row or below the window. Don't care for
17237 lines that might be displayed later at the window end; as
17238 mentioned, this is not a frequent case. */
17239 if (w->cursor.vpos < 0)
17240 {
17241 /* Cursor in unchanged rows at the top? */
17242 if (PT < CHARPOS (start_pos)
17243 && last_unchanged_at_beg_row)
17244 {
17245 row = row_containing_pos (w, PT,
17246 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17247 last_unchanged_at_beg_row + 1, 0);
17248 if (row)
17249 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17250 }
17251
17252 /* Start from first_unchanged_at_end_row looking for PT. */
17253 else if (first_unchanged_at_end_row)
17254 {
17255 row = row_containing_pos (w, PT - delta,
17256 first_unchanged_at_end_row, NULL, 0);
17257 if (row)
17258 set_cursor_from_row (w, row, w->current_matrix, delta,
17259 delta_bytes, dy, dvpos);
17260 }
17261
17262 /* Give up if cursor was not found. */
17263 if (w->cursor.vpos < 0)
17264 {
17265 clear_glyph_matrix (w->desired_matrix);
17266 return -1;
17267 }
17268 }
17269
17270 /* Don't let the cursor end in the scroll margins. */
17271 {
17272 int this_scroll_margin, cursor_height;
17273
17274 this_scroll_margin =
17275 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17276 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17277 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17278
17279 if ((w->cursor.y < this_scroll_margin
17280 && CHARPOS (start) > BEGV)
17281 /* Old redisplay didn't take scroll margin into account at the bottom,
17282 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17283 || (w->cursor.y + (make_cursor_line_fully_visible_p
17284 ? cursor_height + this_scroll_margin
17285 : 1)) > it.last_visible_y)
17286 {
17287 w->cursor.vpos = -1;
17288 clear_glyph_matrix (w->desired_matrix);
17289 return -1;
17290 }
17291 }
17292
17293 /* Scroll the display. Do it before changing the current matrix so
17294 that xterm.c doesn't get confused about where the cursor glyph is
17295 found. */
17296 if (dy && run.height)
17297 {
17298 update_begin (f);
17299
17300 if (FRAME_WINDOW_P (f))
17301 {
17302 FRAME_RIF (f)->update_window_begin_hook (w);
17303 FRAME_RIF (f)->clear_window_mouse_face (w);
17304 FRAME_RIF (f)->scroll_run_hook (w, &run);
17305 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17306 }
17307 else
17308 {
17309 /* Terminal frame. In this case, dvpos gives the number of
17310 lines to scroll by; dvpos < 0 means scroll up. */
17311 int from_vpos
17312 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17313 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17314 int end = (WINDOW_TOP_EDGE_LINE (w)
17315 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17316 + window_internal_height (w));
17317
17318 #if defined (HAVE_GPM) || defined (MSDOS)
17319 x_clear_window_mouse_face (w);
17320 #endif
17321 /* Perform the operation on the screen. */
17322 if (dvpos > 0)
17323 {
17324 /* Scroll last_unchanged_at_beg_row to the end of the
17325 window down dvpos lines. */
17326 set_terminal_window (f, end);
17327
17328 /* On dumb terminals delete dvpos lines at the end
17329 before inserting dvpos empty lines. */
17330 if (!FRAME_SCROLL_REGION_OK (f))
17331 ins_del_lines (f, end - dvpos, -dvpos);
17332
17333 /* Insert dvpos empty lines in front of
17334 last_unchanged_at_beg_row. */
17335 ins_del_lines (f, from, dvpos);
17336 }
17337 else if (dvpos < 0)
17338 {
17339 /* Scroll up last_unchanged_at_beg_vpos to the end of
17340 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17341 set_terminal_window (f, end);
17342
17343 /* Delete dvpos lines in front of
17344 last_unchanged_at_beg_vpos. ins_del_lines will set
17345 the cursor to the given vpos and emit |dvpos| delete
17346 line sequences. */
17347 ins_del_lines (f, from + dvpos, dvpos);
17348
17349 /* On a dumb terminal insert dvpos empty lines at the
17350 end. */
17351 if (!FRAME_SCROLL_REGION_OK (f))
17352 ins_del_lines (f, end + dvpos, -dvpos);
17353 }
17354
17355 set_terminal_window (f, 0);
17356 }
17357
17358 update_end (f);
17359 }
17360
17361 /* Shift reused rows of the current matrix to the right position.
17362 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17363 text. */
17364 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17365 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17366 if (dvpos < 0)
17367 {
17368 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17369 bottom_vpos, dvpos);
17370 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17371 bottom_vpos, 0);
17372 }
17373 else if (dvpos > 0)
17374 {
17375 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17376 bottom_vpos, dvpos);
17377 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17378 first_unchanged_at_end_vpos + dvpos, 0);
17379 }
17380
17381 /* For frame-based redisplay, make sure that current frame and window
17382 matrix are in sync with respect to glyph memory. */
17383 if (!FRAME_WINDOW_P (f))
17384 sync_frame_with_window_matrix_rows (w);
17385
17386 /* Adjust buffer positions in reused rows. */
17387 if (delta || delta_bytes)
17388 increment_matrix_positions (current_matrix,
17389 first_unchanged_at_end_vpos + dvpos,
17390 bottom_vpos, delta, delta_bytes);
17391
17392 /* Adjust Y positions. */
17393 if (dy)
17394 shift_glyph_matrix (w, current_matrix,
17395 first_unchanged_at_end_vpos + dvpos,
17396 bottom_vpos, dy);
17397
17398 if (first_unchanged_at_end_row)
17399 {
17400 first_unchanged_at_end_row += dvpos;
17401 if (first_unchanged_at_end_row->y >= it.last_visible_y
17402 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17403 first_unchanged_at_end_row = NULL;
17404 }
17405
17406 /* If scrolling up, there may be some lines to display at the end of
17407 the window. */
17408 last_text_row_at_end = NULL;
17409 if (dy < 0)
17410 {
17411 /* Scrolling up can leave for example a partially visible line
17412 at the end of the window to be redisplayed. */
17413 /* Set last_row to the glyph row in the current matrix where the
17414 window end line is found. It has been moved up or down in
17415 the matrix by dvpos. */
17416 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17417 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17418
17419 /* If last_row is the window end line, it should display text. */
17420 xassert (last_row->displays_text_p);
17421
17422 /* If window end line was partially visible before, begin
17423 displaying at that line. Otherwise begin displaying with the
17424 line following it. */
17425 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17426 {
17427 init_to_row_start (&it, w, last_row);
17428 it.vpos = last_vpos;
17429 it.current_y = last_row->y;
17430 }
17431 else
17432 {
17433 init_to_row_end (&it, w, last_row);
17434 it.vpos = 1 + last_vpos;
17435 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17436 ++last_row;
17437 }
17438
17439 /* We may start in a continuation line. If so, we have to
17440 get the right continuation_lines_width and current_x. */
17441 it.continuation_lines_width = last_row->continuation_lines_width;
17442 it.hpos = it.current_x = 0;
17443
17444 /* Display the rest of the lines at the window end. */
17445 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17446 while (it.current_y < it.last_visible_y
17447 && !fonts_changed_p)
17448 {
17449 /* Is it always sure that the display agrees with lines in
17450 the current matrix? I don't think so, so we mark rows
17451 displayed invalid in the current matrix by setting their
17452 enabled_p flag to zero. */
17453 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17454 if (display_line (&it))
17455 last_text_row_at_end = it.glyph_row - 1;
17456 }
17457 }
17458
17459 /* Update window_end_pos and window_end_vpos. */
17460 if (first_unchanged_at_end_row
17461 && !last_text_row_at_end)
17462 {
17463 /* Window end line if one of the preserved rows from the current
17464 matrix. Set row to the last row displaying text in current
17465 matrix starting at first_unchanged_at_end_row, after
17466 scrolling. */
17467 xassert (first_unchanged_at_end_row->displays_text_p);
17468 row = find_last_row_displaying_text (w->current_matrix, &it,
17469 first_unchanged_at_end_row);
17470 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17471
17472 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17473 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17474 w->window_end_vpos
17475 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17476 xassert (w->window_end_bytepos >= 0);
17477 IF_DEBUG (debug_method_add (w, "A"));
17478 }
17479 else if (last_text_row_at_end)
17480 {
17481 w->window_end_pos
17482 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17483 w->window_end_bytepos
17484 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17485 w->window_end_vpos
17486 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17487 xassert (w->window_end_bytepos >= 0);
17488 IF_DEBUG (debug_method_add (w, "B"));
17489 }
17490 else if (last_text_row)
17491 {
17492 /* We have displayed either to the end of the window or at the
17493 end of the window, i.e. the last row with text is to be found
17494 in the desired matrix. */
17495 w->window_end_pos
17496 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17497 w->window_end_bytepos
17498 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17499 w->window_end_vpos
17500 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17501 xassert (w->window_end_bytepos >= 0);
17502 }
17503 else if (first_unchanged_at_end_row == NULL
17504 && last_text_row == NULL
17505 && last_text_row_at_end == NULL)
17506 {
17507 /* Displayed to end of window, but no line containing text was
17508 displayed. Lines were deleted at the end of the window. */
17509 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17510 int vpos = XFASTINT (w->window_end_vpos);
17511 struct glyph_row *current_row = current_matrix->rows + vpos;
17512 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17513
17514 for (row = NULL;
17515 row == NULL && vpos >= first_vpos;
17516 --vpos, --current_row, --desired_row)
17517 {
17518 if (desired_row->enabled_p)
17519 {
17520 if (desired_row->displays_text_p)
17521 row = desired_row;
17522 }
17523 else if (current_row->displays_text_p)
17524 row = current_row;
17525 }
17526
17527 xassert (row != NULL);
17528 w->window_end_vpos = make_number (vpos + 1);
17529 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17530 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17531 xassert (w->window_end_bytepos >= 0);
17532 IF_DEBUG (debug_method_add (w, "C"));
17533 }
17534 else
17535 abort ();
17536
17537 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17538 debug_end_vpos = XFASTINT (w->window_end_vpos));
17539
17540 /* Record that display has not been completed. */
17541 w->window_end_valid = Qnil;
17542 w->desired_matrix->no_scrolling_p = 1;
17543 return 3;
17544
17545 #undef GIVE_UP
17546 }
17547
17548
17549 \f
17550 /***********************************************************************
17551 More debugging support
17552 ***********************************************************************/
17553
17554 #if GLYPH_DEBUG
17555
17556 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17557 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17558 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17559
17560
17561 /* Dump the contents of glyph matrix MATRIX on stderr.
17562
17563 GLYPHS 0 means don't show glyph contents.
17564 GLYPHS 1 means show glyphs in short form
17565 GLYPHS > 1 means show glyphs in long form. */
17566
17567 void
17568 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17569 {
17570 int i;
17571 for (i = 0; i < matrix->nrows; ++i)
17572 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17573 }
17574
17575
17576 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17577 the glyph row and area where the glyph comes from. */
17578
17579 void
17580 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17581 {
17582 if (glyph->type == CHAR_GLYPH)
17583 {
17584 fprintf (stderr,
17585 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17586 glyph - row->glyphs[TEXT_AREA],
17587 'C',
17588 glyph->charpos,
17589 (BUFFERP (glyph->object)
17590 ? 'B'
17591 : (STRINGP (glyph->object)
17592 ? 'S'
17593 : '-')),
17594 glyph->pixel_width,
17595 glyph->u.ch,
17596 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17597 ? glyph->u.ch
17598 : '.'),
17599 glyph->face_id,
17600 glyph->left_box_line_p,
17601 glyph->right_box_line_p);
17602 }
17603 else if (glyph->type == STRETCH_GLYPH)
17604 {
17605 fprintf (stderr,
17606 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17607 glyph - row->glyphs[TEXT_AREA],
17608 'S',
17609 glyph->charpos,
17610 (BUFFERP (glyph->object)
17611 ? 'B'
17612 : (STRINGP (glyph->object)
17613 ? 'S'
17614 : '-')),
17615 glyph->pixel_width,
17616 0,
17617 '.',
17618 glyph->face_id,
17619 glyph->left_box_line_p,
17620 glyph->right_box_line_p);
17621 }
17622 else if (glyph->type == IMAGE_GLYPH)
17623 {
17624 fprintf (stderr,
17625 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17626 glyph - row->glyphs[TEXT_AREA],
17627 'I',
17628 glyph->charpos,
17629 (BUFFERP (glyph->object)
17630 ? 'B'
17631 : (STRINGP (glyph->object)
17632 ? 'S'
17633 : '-')),
17634 glyph->pixel_width,
17635 glyph->u.img_id,
17636 '.',
17637 glyph->face_id,
17638 glyph->left_box_line_p,
17639 glyph->right_box_line_p);
17640 }
17641 else if (glyph->type == COMPOSITE_GLYPH)
17642 {
17643 fprintf (stderr,
17644 " %5td %4c %6"pI"d %c %3d 0x%05x",
17645 glyph - row->glyphs[TEXT_AREA],
17646 '+',
17647 glyph->charpos,
17648 (BUFFERP (glyph->object)
17649 ? 'B'
17650 : (STRINGP (glyph->object)
17651 ? 'S'
17652 : '-')),
17653 glyph->pixel_width,
17654 glyph->u.cmp.id);
17655 if (glyph->u.cmp.automatic)
17656 fprintf (stderr,
17657 "[%d-%d]",
17658 glyph->slice.cmp.from, glyph->slice.cmp.to);
17659 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17660 glyph->face_id,
17661 glyph->left_box_line_p,
17662 glyph->right_box_line_p);
17663 }
17664 }
17665
17666
17667 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17668 GLYPHS 0 means don't show glyph contents.
17669 GLYPHS 1 means show glyphs in short form
17670 GLYPHS > 1 means show glyphs in long form. */
17671
17672 void
17673 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17674 {
17675 if (glyphs != 1)
17676 {
17677 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17678 fprintf (stderr, "======================================================================\n");
17679
17680 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17681 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17682 vpos,
17683 MATRIX_ROW_START_CHARPOS (row),
17684 MATRIX_ROW_END_CHARPOS (row),
17685 row->used[TEXT_AREA],
17686 row->contains_overlapping_glyphs_p,
17687 row->enabled_p,
17688 row->truncated_on_left_p,
17689 row->truncated_on_right_p,
17690 row->continued_p,
17691 MATRIX_ROW_CONTINUATION_LINE_P (row),
17692 row->displays_text_p,
17693 row->ends_at_zv_p,
17694 row->fill_line_p,
17695 row->ends_in_middle_of_char_p,
17696 row->starts_in_middle_of_char_p,
17697 row->mouse_face_p,
17698 row->x,
17699 row->y,
17700 row->pixel_width,
17701 row->height,
17702 row->visible_height,
17703 row->ascent,
17704 row->phys_ascent);
17705 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17706 row->end.overlay_string_index,
17707 row->continuation_lines_width);
17708 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17709 CHARPOS (row->start.string_pos),
17710 CHARPOS (row->end.string_pos));
17711 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17712 row->end.dpvec_index);
17713 }
17714
17715 if (glyphs > 1)
17716 {
17717 int area;
17718
17719 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17720 {
17721 struct glyph *glyph = row->glyphs[area];
17722 struct glyph *glyph_end = glyph + row->used[area];
17723
17724 /* Glyph for a line end in text. */
17725 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17726 ++glyph_end;
17727
17728 if (glyph < glyph_end)
17729 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17730
17731 for (; glyph < glyph_end; ++glyph)
17732 dump_glyph (row, glyph, area);
17733 }
17734 }
17735 else if (glyphs == 1)
17736 {
17737 int area;
17738
17739 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17740 {
17741 char *s = (char *) alloca (row->used[area] + 1);
17742 int i;
17743
17744 for (i = 0; i < row->used[area]; ++i)
17745 {
17746 struct glyph *glyph = row->glyphs[area] + i;
17747 if (glyph->type == CHAR_GLYPH
17748 && glyph->u.ch < 0x80
17749 && glyph->u.ch >= ' ')
17750 s[i] = glyph->u.ch;
17751 else
17752 s[i] = '.';
17753 }
17754
17755 s[i] = '\0';
17756 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17757 }
17758 }
17759 }
17760
17761
17762 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17763 Sdump_glyph_matrix, 0, 1, "p",
17764 doc: /* Dump the current matrix of the selected window to stderr.
17765 Shows contents of glyph row structures. With non-nil
17766 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17767 glyphs in short form, otherwise show glyphs in long form. */)
17768 (Lisp_Object glyphs)
17769 {
17770 struct window *w = XWINDOW (selected_window);
17771 struct buffer *buffer = XBUFFER (w->buffer);
17772
17773 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17774 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17775 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17776 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17777 fprintf (stderr, "=============================================\n");
17778 dump_glyph_matrix (w->current_matrix,
17779 NILP (glyphs) ? 0 : XINT (glyphs));
17780 return Qnil;
17781 }
17782
17783
17784 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17785 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17786 (void)
17787 {
17788 struct frame *f = XFRAME (selected_frame);
17789 dump_glyph_matrix (f->current_matrix, 1);
17790 return Qnil;
17791 }
17792
17793
17794 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17795 doc: /* Dump glyph row ROW to stderr.
17796 GLYPH 0 means don't dump glyphs.
17797 GLYPH 1 means dump glyphs in short form.
17798 GLYPH > 1 or omitted means dump glyphs in long form. */)
17799 (Lisp_Object row, Lisp_Object glyphs)
17800 {
17801 struct glyph_matrix *matrix;
17802 int vpos;
17803
17804 CHECK_NUMBER (row);
17805 matrix = XWINDOW (selected_window)->current_matrix;
17806 vpos = XINT (row);
17807 if (vpos >= 0 && vpos < matrix->nrows)
17808 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17809 vpos,
17810 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17811 return Qnil;
17812 }
17813
17814
17815 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17816 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17817 GLYPH 0 means don't dump glyphs.
17818 GLYPH 1 means dump glyphs in short form.
17819 GLYPH > 1 or omitted means dump glyphs in long form. */)
17820 (Lisp_Object row, Lisp_Object glyphs)
17821 {
17822 struct frame *sf = SELECTED_FRAME ();
17823 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17824 int vpos;
17825
17826 CHECK_NUMBER (row);
17827 vpos = XINT (row);
17828 if (vpos >= 0 && vpos < m->nrows)
17829 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17830 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17831 return Qnil;
17832 }
17833
17834
17835 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17836 doc: /* Toggle tracing of redisplay.
17837 With ARG, turn tracing on if and only if ARG is positive. */)
17838 (Lisp_Object arg)
17839 {
17840 if (NILP (arg))
17841 trace_redisplay_p = !trace_redisplay_p;
17842 else
17843 {
17844 arg = Fprefix_numeric_value (arg);
17845 trace_redisplay_p = XINT (arg) > 0;
17846 }
17847
17848 return Qnil;
17849 }
17850
17851
17852 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17853 doc: /* Like `format', but print result to stderr.
17854 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17855 (ptrdiff_t nargs, Lisp_Object *args)
17856 {
17857 Lisp_Object s = Fformat (nargs, args);
17858 fprintf (stderr, "%s", SDATA (s));
17859 return Qnil;
17860 }
17861
17862 #endif /* GLYPH_DEBUG */
17863
17864
17865 \f
17866 /***********************************************************************
17867 Building Desired Matrix Rows
17868 ***********************************************************************/
17869
17870 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17871 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17872
17873 static struct glyph_row *
17874 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17875 {
17876 struct frame *f = XFRAME (WINDOW_FRAME (w));
17877 struct buffer *buffer = XBUFFER (w->buffer);
17878 struct buffer *old = current_buffer;
17879 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17880 int arrow_len = SCHARS (overlay_arrow_string);
17881 const unsigned char *arrow_end = arrow_string + arrow_len;
17882 const unsigned char *p;
17883 struct it it;
17884 int multibyte_p;
17885 int n_glyphs_before;
17886
17887 set_buffer_temp (buffer);
17888 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17889 it.glyph_row->used[TEXT_AREA] = 0;
17890 SET_TEXT_POS (it.position, 0, 0);
17891
17892 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17893 p = arrow_string;
17894 while (p < arrow_end)
17895 {
17896 Lisp_Object face, ilisp;
17897
17898 /* Get the next character. */
17899 if (multibyte_p)
17900 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17901 else
17902 {
17903 it.c = it.char_to_display = *p, it.len = 1;
17904 if (! ASCII_CHAR_P (it.c))
17905 it.char_to_display = BYTE8_TO_CHAR (it.c);
17906 }
17907 p += it.len;
17908
17909 /* Get its face. */
17910 ilisp = make_number (p - arrow_string);
17911 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17912 it.face_id = compute_char_face (f, it.char_to_display, face);
17913
17914 /* Compute its width, get its glyphs. */
17915 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17916 SET_TEXT_POS (it.position, -1, -1);
17917 PRODUCE_GLYPHS (&it);
17918
17919 /* If this character doesn't fit any more in the line, we have
17920 to remove some glyphs. */
17921 if (it.current_x > it.last_visible_x)
17922 {
17923 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17924 break;
17925 }
17926 }
17927
17928 set_buffer_temp (old);
17929 return it.glyph_row;
17930 }
17931
17932
17933 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17934 glyphs are only inserted for terminal frames since we can't really
17935 win with truncation glyphs when partially visible glyphs are
17936 involved. Which glyphs to insert is determined by
17937 produce_special_glyphs. */
17938
17939 static void
17940 insert_left_trunc_glyphs (struct it *it)
17941 {
17942 struct it truncate_it;
17943 struct glyph *from, *end, *to, *toend;
17944
17945 xassert (!FRAME_WINDOW_P (it->f));
17946
17947 /* Get the truncation glyphs. */
17948 truncate_it = *it;
17949 truncate_it.current_x = 0;
17950 truncate_it.face_id = DEFAULT_FACE_ID;
17951 truncate_it.glyph_row = &scratch_glyph_row;
17952 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17953 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17954 truncate_it.object = make_number (0);
17955 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17956
17957 /* Overwrite glyphs from IT with truncation glyphs. */
17958 if (!it->glyph_row->reversed_p)
17959 {
17960 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17961 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17962 to = it->glyph_row->glyphs[TEXT_AREA];
17963 toend = to + it->glyph_row->used[TEXT_AREA];
17964
17965 while (from < end)
17966 *to++ = *from++;
17967
17968 /* There may be padding glyphs left over. Overwrite them too. */
17969 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17970 {
17971 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17972 while (from < end)
17973 *to++ = *from++;
17974 }
17975
17976 if (to > toend)
17977 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17978 }
17979 else
17980 {
17981 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17982 that back to front. */
17983 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17984 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17985 toend = it->glyph_row->glyphs[TEXT_AREA];
17986 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17987
17988 while (from >= end && to >= toend)
17989 *to-- = *from--;
17990 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17991 {
17992 from =
17993 truncate_it.glyph_row->glyphs[TEXT_AREA]
17994 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17995 while (from >= end && to >= toend)
17996 *to-- = *from--;
17997 }
17998 if (from >= end)
17999 {
18000 /* Need to free some room before prepending additional
18001 glyphs. */
18002 int move_by = from - end + 1;
18003 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18004 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18005
18006 for ( ; g >= g0; g--)
18007 g[move_by] = *g;
18008 while (from >= end)
18009 *to-- = *from--;
18010 it->glyph_row->used[TEXT_AREA] += move_by;
18011 }
18012 }
18013 }
18014
18015 /* Compute the hash code for ROW. */
18016 unsigned
18017 row_hash (struct glyph_row *row)
18018 {
18019 int area, k;
18020 unsigned hashval = 0;
18021
18022 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18023 for (k = 0; k < row->used[area]; ++k)
18024 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18025 + row->glyphs[area][k].u.val
18026 + row->glyphs[area][k].face_id
18027 + row->glyphs[area][k].padding_p
18028 + (row->glyphs[area][k].type << 2));
18029
18030 return hashval;
18031 }
18032
18033 /* Compute the pixel height and width of IT->glyph_row.
18034
18035 Most of the time, ascent and height of a display line will be equal
18036 to the max_ascent and max_height values of the display iterator
18037 structure. This is not the case if
18038
18039 1. We hit ZV without displaying anything. In this case, max_ascent
18040 and max_height will be zero.
18041
18042 2. We have some glyphs that don't contribute to the line height.
18043 (The glyph row flag contributes_to_line_height_p is for future
18044 pixmap extensions).
18045
18046 The first case is easily covered by using default values because in
18047 these cases, the line height does not really matter, except that it
18048 must not be zero. */
18049
18050 static void
18051 compute_line_metrics (struct it *it)
18052 {
18053 struct glyph_row *row = it->glyph_row;
18054
18055 if (FRAME_WINDOW_P (it->f))
18056 {
18057 int i, min_y, max_y;
18058
18059 /* The line may consist of one space only, that was added to
18060 place the cursor on it. If so, the row's height hasn't been
18061 computed yet. */
18062 if (row->height == 0)
18063 {
18064 if (it->max_ascent + it->max_descent == 0)
18065 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18066 row->ascent = it->max_ascent;
18067 row->height = it->max_ascent + it->max_descent;
18068 row->phys_ascent = it->max_phys_ascent;
18069 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18070 row->extra_line_spacing = it->max_extra_line_spacing;
18071 }
18072
18073 /* Compute the width of this line. */
18074 row->pixel_width = row->x;
18075 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18076 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18077
18078 xassert (row->pixel_width >= 0);
18079 xassert (row->ascent >= 0 && row->height > 0);
18080
18081 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18082 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18083
18084 /* If first line's physical ascent is larger than its logical
18085 ascent, use the physical ascent, and make the row taller.
18086 This makes accented characters fully visible. */
18087 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18088 && row->phys_ascent > row->ascent)
18089 {
18090 row->height += row->phys_ascent - row->ascent;
18091 row->ascent = row->phys_ascent;
18092 }
18093
18094 /* Compute how much of the line is visible. */
18095 row->visible_height = row->height;
18096
18097 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18098 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18099
18100 if (row->y < min_y)
18101 row->visible_height -= min_y - row->y;
18102 if (row->y + row->height > max_y)
18103 row->visible_height -= row->y + row->height - max_y;
18104 }
18105 else
18106 {
18107 row->pixel_width = row->used[TEXT_AREA];
18108 if (row->continued_p)
18109 row->pixel_width -= it->continuation_pixel_width;
18110 else if (row->truncated_on_right_p)
18111 row->pixel_width -= it->truncation_pixel_width;
18112 row->ascent = row->phys_ascent = 0;
18113 row->height = row->phys_height = row->visible_height = 1;
18114 row->extra_line_spacing = 0;
18115 }
18116
18117 /* Compute a hash code for this row. */
18118 row->hash = row_hash (row);
18119
18120 it->max_ascent = it->max_descent = 0;
18121 it->max_phys_ascent = it->max_phys_descent = 0;
18122 }
18123
18124
18125 /* Append one space to the glyph row of iterator IT if doing a
18126 window-based redisplay. The space has the same face as
18127 IT->face_id. Value is non-zero if a space was added.
18128
18129 This function is called to make sure that there is always one glyph
18130 at the end of a glyph row that the cursor can be set on under
18131 window-systems. (If there weren't such a glyph we would not know
18132 how wide and tall a box cursor should be displayed).
18133
18134 At the same time this space let's a nicely handle clearing to the
18135 end of the line if the row ends in italic text. */
18136
18137 static int
18138 append_space_for_newline (struct it *it, int default_face_p)
18139 {
18140 if (FRAME_WINDOW_P (it->f))
18141 {
18142 int n = it->glyph_row->used[TEXT_AREA];
18143
18144 if (it->glyph_row->glyphs[TEXT_AREA] + n
18145 < it->glyph_row->glyphs[1 + TEXT_AREA])
18146 {
18147 /* Save some values that must not be changed.
18148 Must save IT->c and IT->len because otherwise
18149 ITERATOR_AT_END_P wouldn't work anymore after
18150 append_space_for_newline has been called. */
18151 enum display_element_type saved_what = it->what;
18152 int saved_c = it->c, saved_len = it->len;
18153 int saved_char_to_display = it->char_to_display;
18154 int saved_x = it->current_x;
18155 int saved_face_id = it->face_id;
18156 struct text_pos saved_pos;
18157 Lisp_Object saved_object;
18158 struct face *face;
18159
18160 saved_object = it->object;
18161 saved_pos = it->position;
18162
18163 it->what = IT_CHARACTER;
18164 memset (&it->position, 0, sizeof it->position);
18165 it->object = make_number (0);
18166 it->c = it->char_to_display = ' ';
18167 it->len = 1;
18168
18169 if (default_face_p)
18170 it->face_id = DEFAULT_FACE_ID;
18171 else if (it->face_before_selective_p)
18172 it->face_id = it->saved_face_id;
18173 face = FACE_FROM_ID (it->f, it->face_id);
18174 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18175
18176 PRODUCE_GLYPHS (it);
18177
18178 it->override_ascent = -1;
18179 it->constrain_row_ascent_descent_p = 0;
18180 it->current_x = saved_x;
18181 it->object = saved_object;
18182 it->position = saved_pos;
18183 it->what = saved_what;
18184 it->face_id = saved_face_id;
18185 it->len = saved_len;
18186 it->c = saved_c;
18187 it->char_to_display = saved_char_to_display;
18188 return 1;
18189 }
18190 }
18191
18192 return 0;
18193 }
18194
18195
18196 /* Extend the face of the last glyph in the text area of IT->glyph_row
18197 to the end of the display line. Called from display_line. If the
18198 glyph row is empty, add a space glyph to it so that we know the
18199 face to draw. Set the glyph row flag fill_line_p. If the glyph
18200 row is R2L, prepend a stretch glyph to cover the empty space to the
18201 left of the leftmost glyph. */
18202
18203 static void
18204 extend_face_to_end_of_line (struct it *it)
18205 {
18206 struct face *face;
18207 struct frame *f = it->f;
18208
18209 /* If line is already filled, do nothing. Non window-system frames
18210 get a grace of one more ``pixel'' because their characters are
18211 1-``pixel'' wide, so they hit the equality too early. This grace
18212 is needed only for R2L rows that are not continued, to produce
18213 one extra blank where we could display the cursor. */
18214 if (it->current_x >= it->last_visible_x
18215 + (!FRAME_WINDOW_P (f)
18216 && it->glyph_row->reversed_p
18217 && !it->glyph_row->continued_p))
18218 return;
18219
18220 /* Face extension extends the background and box of IT->face_id
18221 to the end of the line. If the background equals the background
18222 of the frame, we don't have to do anything. */
18223 if (it->face_before_selective_p)
18224 face = FACE_FROM_ID (f, it->saved_face_id);
18225 else
18226 face = FACE_FROM_ID (f, it->face_id);
18227
18228 if (FRAME_WINDOW_P (f)
18229 && it->glyph_row->displays_text_p
18230 && face->box == FACE_NO_BOX
18231 && face->background == FRAME_BACKGROUND_PIXEL (f)
18232 && !face->stipple
18233 && !it->glyph_row->reversed_p)
18234 return;
18235
18236 /* Set the glyph row flag indicating that the face of the last glyph
18237 in the text area has to be drawn to the end of the text area. */
18238 it->glyph_row->fill_line_p = 1;
18239
18240 /* If current character of IT is not ASCII, make sure we have the
18241 ASCII face. This will be automatically undone the next time
18242 get_next_display_element returns a multibyte character. Note
18243 that the character will always be single byte in unibyte
18244 text. */
18245 if (!ASCII_CHAR_P (it->c))
18246 {
18247 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18248 }
18249
18250 if (FRAME_WINDOW_P (f))
18251 {
18252 /* If the row is empty, add a space with the current face of IT,
18253 so that we know which face to draw. */
18254 if (it->glyph_row->used[TEXT_AREA] == 0)
18255 {
18256 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18257 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
18258 it->glyph_row->used[TEXT_AREA] = 1;
18259 }
18260 #ifdef HAVE_WINDOW_SYSTEM
18261 if (it->glyph_row->reversed_p)
18262 {
18263 /* Prepend a stretch glyph to the row, such that the
18264 rightmost glyph will be drawn flushed all the way to the
18265 right margin of the window. The stretch glyph that will
18266 occupy the empty space, if any, to the left of the
18267 glyphs. */
18268 struct font *font = face->font ? face->font : FRAME_FONT (f);
18269 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18270 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18271 struct glyph *g;
18272 int row_width, stretch_ascent, stretch_width;
18273 struct text_pos saved_pos;
18274 int saved_face_id, saved_avoid_cursor;
18275
18276 for (row_width = 0, g = row_start; g < row_end; g++)
18277 row_width += g->pixel_width;
18278 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18279 if (stretch_width > 0)
18280 {
18281 stretch_ascent =
18282 (((it->ascent + it->descent)
18283 * FONT_BASE (font)) / FONT_HEIGHT (font));
18284 saved_pos = it->position;
18285 memset (&it->position, 0, sizeof it->position);
18286 saved_avoid_cursor = it->avoid_cursor_p;
18287 it->avoid_cursor_p = 1;
18288 saved_face_id = it->face_id;
18289 /* The last row's stretch glyph should get the default
18290 face, to avoid painting the rest of the window with
18291 the region face, if the region ends at ZV. */
18292 if (it->glyph_row->ends_at_zv_p)
18293 it->face_id = DEFAULT_FACE_ID;
18294 else
18295 it->face_id = face->id;
18296 append_stretch_glyph (it, make_number (0), stretch_width,
18297 it->ascent + it->descent, stretch_ascent);
18298 it->position = saved_pos;
18299 it->avoid_cursor_p = saved_avoid_cursor;
18300 it->face_id = saved_face_id;
18301 }
18302 }
18303 #endif /* HAVE_WINDOW_SYSTEM */
18304 }
18305 else
18306 {
18307 /* Save some values that must not be changed. */
18308 int saved_x = it->current_x;
18309 struct text_pos saved_pos;
18310 Lisp_Object saved_object;
18311 enum display_element_type saved_what = it->what;
18312 int saved_face_id = it->face_id;
18313
18314 saved_object = it->object;
18315 saved_pos = it->position;
18316
18317 it->what = IT_CHARACTER;
18318 memset (&it->position, 0, sizeof it->position);
18319 it->object = make_number (0);
18320 it->c = it->char_to_display = ' ';
18321 it->len = 1;
18322 /* The last row's blank glyphs should get the default face, to
18323 avoid painting the rest of the window with the region face,
18324 if the region ends at ZV. */
18325 if (it->glyph_row->ends_at_zv_p)
18326 it->face_id = DEFAULT_FACE_ID;
18327 else
18328 it->face_id = face->id;
18329
18330 PRODUCE_GLYPHS (it);
18331
18332 while (it->current_x <= it->last_visible_x)
18333 PRODUCE_GLYPHS (it);
18334
18335 /* Don't count these blanks really. It would let us insert a left
18336 truncation glyph below and make us set the cursor on them, maybe. */
18337 it->current_x = saved_x;
18338 it->object = saved_object;
18339 it->position = saved_pos;
18340 it->what = saved_what;
18341 it->face_id = saved_face_id;
18342 }
18343 }
18344
18345
18346 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18347 trailing whitespace. */
18348
18349 static int
18350 trailing_whitespace_p (EMACS_INT charpos)
18351 {
18352 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18353 int c = 0;
18354
18355 while (bytepos < ZV_BYTE
18356 && (c = FETCH_CHAR (bytepos),
18357 c == ' ' || c == '\t'))
18358 ++bytepos;
18359
18360 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18361 {
18362 if (bytepos != PT_BYTE)
18363 return 1;
18364 }
18365 return 0;
18366 }
18367
18368
18369 /* Highlight trailing whitespace, if any, in ROW. */
18370
18371 static void
18372 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18373 {
18374 int used = row->used[TEXT_AREA];
18375
18376 if (used)
18377 {
18378 struct glyph *start = row->glyphs[TEXT_AREA];
18379 struct glyph *glyph = start + used - 1;
18380
18381 if (row->reversed_p)
18382 {
18383 /* Right-to-left rows need to be processed in the opposite
18384 direction, so swap the edge pointers. */
18385 glyph = start;
18386 start = row->glyphs[TEXT_AREA] + used - 1;
18387 }
18388
18389 /* Skip over glyphs inserted to display the cursor at the
18390 end of a line, for extending the face of the last glyph
18391 to the end of the line on terminals, and for truncation
18392 and continuation glyphs. */
18393 if (!row->reversed_p)
18394 {
18395 while (glyph >= start
18396 && glyph->type == CHAR_GLYPH
18397 && INTEGERP (glyph->object))
18398 --glyph;
18399 }
18400 else
18401 {
18402 while (glyph <= start
18403 && glyph->type == CHAR_GLYPH
18404 && INTEGERP (glyph->object))
18405 ++glyph;
18406 }
18407
18408 /* If last glyph is a space or stretch, and it's trailing
18409 whitespace, set the face of all trailing whitespace glyphs in
18410 IT->glyph_row to `trailing-whitespace'. */
18411 if ((row->reversed_p ? glyph <= start : glyph >= start)
18412 && BUFFERP (glyph->object)
18413 && (glyph->type == STRETCH_GLYPH
18414 || (glyph->type == CHAR_GLYPH
18415 && glyph->u.ch == ' '))
18416 && trailing_whitespace_p (glyph->charpos))
18417 {
18418 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18419 if (face_id < 0)
18420 return;
18421
18422 if (!row->reversed_p)
18423 {
18424 while (glyph >= start
18425 && BUFFERP (glyph->object)
18426 && (glyph->type == STRETCH_GLYPH
18427 || (glyph->type == CHAR_GLYPH
18428 && glyph->u.ch == ' ')))
18429 (glyph--)->face_id = face_id;
18430 }
18431 else
18432 {
18433 while (glyph <= start
18434 && BUFFERP (glyph->object)
18435 && (glyph->type == STRETCH_GLYPH
18436 || (glyph->type == CHAR_GLYPH
18437 && glyph->u.ch == ' ')))
18438 (glyph++)->face_id = face_id;
18439 }
18440 }
18441 }
18442 }
18443
18444
18445 /* Value is non-zero if glyph row ROW should be
18446 used to hold the cursor. */
18447
18448 static int
18449 cursor_row_p (struct glyph_row *row)
18450 {
18451 int result = 1;
18452
18453 if (PT == CHARPOS (row->end.pos)
18454 || PT == MATRIX_ROW_END_CHARPOS (row))
18455 {
18456 /* Suppose the row ends on a string.
18457 Unless the row is continued, that means it ends on a newline
18458 in the string. If it's anything other than a display string
18459 (e.g., a before-string from an overlay), we don't want the
18460 cursor there. (This heuristic seems to give the optimal
18461 behavior for the various types of multi-line strings.)
18462 One exception: if the string has `cursor' property on one of
18463 its characters, we _do_ want the cursor there. */
18464 if (CHARPOS (row->end.string_pos) >= 0)
18465 {
18466 if (row->continued_p)
18467 result = 1;
18468 else
18469 {
18470 /* Check for `display' property. */
18471 struct glyph *beg = row->glyphs[TEXT_AREA];
18472 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18473 struct glyph *glyph;
18474
18475 result = 0;
18476 for (glyph = end; glyph >= beg; --glyph)
18477 if (STRINGP (glyph->object))
18478 {
18479 Lisp_Object prop
18480 = Fget_char_property (make_number (PT),
18481 Qdisplay, Qnil);
18482 result =
18483 (!NILP (prop)
18484 && display_prop_string_p (prop, glyph->object));
18485 /* If there's a `cursor' property on one of the
18486 string's characters, this row is a cursor row,
18487 even though this is not a display string. */
18488 if (!result)
18489 {
18490 Lisp_Object s = glyph->object;
18491
18492 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18493 {
18494 EMACS_INT gpos = glyph->charpos;
18495
18496 if (!NILP (Fget_char_property (make_number (gpos),
18497 Qcursor, s)))
18498 {
18499 result = 1;
18500 break;
18501 }
18502 }
18503 }
18504 break;
18505 }
18506 }
18507 }
18508 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18509 {
18510 /* If the row ends in middle of a real character,
18511 and the line is continued, we want the cursor here.
18512 That's because CHARPOS (ROW->end.pos) would equal
18513 PT if PT is before the character. */
18514 if (!row->ends_in_ellipsis_p)
18515 result = row->continued_p;
18516 else
18517 /* If the row ends in an ellipsis, then
18518 CHARPOS (ROW->end.pos) will equal point after the
18519 invisible text. We want that position to be displayed
18520 after the ellipsis. */
18521 result = 0;
18522 }
18523 /* If the row ends at ZV, display the cursor at the end of that
18524 row instead of at the start of the row below. */
18525 else if (row->ends_at_zv_p)
18526 result = 1;
18527 else
18528 result = 0;
18529 }
18530
18531 return result;
18532 }
18533
18534 \f
18535
18536 /* Push the property PROP so that it will be rendered at the current
18537 position in IT. Return 1 if PROP was successfully pushed, 0
18538 otherwise. Called from handle_line_prefix to handle the
18539 `line-prefix' and `wrap-prefix' properties. */
18540
18541 static int
18542 push_display_prop (struct it *it, Lisp_Object prop)
18543 {
18544 struct text_pos pos =
18545 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18546
18547 xassert (it->method == GET_FROM_BUFFER
18548 || it->method == GET_FROM_DISPLAY_VECTOR
18549 || it->method == GET_FROM_STRING);
18550
18551 /* We need to save the current buffer/string position, so it will be
18552 restored by pop_it, because iterate_out_of_display_property
18553 depends on that being set correctly, but some situations leave
18554 it->position not yet set when this function is called. */
18555 push_it (it, &pos);
18556
18557 if (STRINGP (prop))
18558 {
18559 if (SCHARS (prop) == 0)
18560 {
18561 pop_it (it);
18562 return 0;
18563 }
18564
18565 it->string = prop;
18566 it->multibyte_p = STRING_MULTIBYTE (it->string);
18567 it->current.overlay_string_index = -1;
18568 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18569 it->end_charpos = it->string_nchars = SCHARS (it->string);
18570 it->method = GET_FROM_STRING;
18571 it->stop_charpos = 0;
18572 it->prev_stop = 0;
18573 it->base_level_stop = 0;
18574
18575 /* Force paragraph direction to be that of the parent
18576 buffer/string. */
18577 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18578 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18579 else
18580 it->paragraph_embedding = L2R;
18581
18582 /* Set up the bidi iterator for this display string. */
18583 if (it->bidi_p)
18584 {
18585 it->bidi_it.string.lstring = it->string;
18586 it->bidi_it.string.s = NULL;
18587 it->bidi_it.string.schars = it->end_charpos;
18588 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18589 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18590 it->bidi_it.string.unibyte = !it->multibyte_p;
18591 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18592 }
18593 }
18594 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18595 {
18596 it->method = GET_FROM_STRETCH;
18597 it->object = prop;
18598 }
18599 #ifdef HAVE_WINDOW_SYSTEM
18600 else if (IMAGEP (prop))
18601 {
18602 it->what = IT_IMAGE;
18603 it->image_id = lookup_image (it->f, prop);
18604 it->method = GET_FROM_IMAGE;
18605 }
18606 #endif /* HAVE_WINDOW_SYSTEM */
18607 else
18608 {
18609 pop_it (it); /* bogus display property, give up */
18610 return 0;
18611 }
18612
18613 return 1;
18614 }
18615
18616 /* Return the character-property PROP at the current position in IT. */
18617
18618 static Lisp_Object
18619 get_it_property (struct it *it, Lisp_Object prop)
18620 {
18621 Lisp_Object position;
18622
18623 if (STRINGP (it->object))
18624 position = make_number (IT_STRING_CHARPOS (*it));
18625 else if (BUFFERP (it->object))
18626 position = make_number (IT_CHARPOS (*it));
18627 else
18628 return Qnil;
18629
18630 return Fget_char_property (position, prop, it->object);
18631 }
18632
18633 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18634
18635 static void
18636 handle_line_prefix (struct it *it)
18637 {
18638 Lisp_Object prefix;
18639
18640 if (it->continuation_lines_width > 0)
18641 {
18642 prefix = get_it_property (it, Qwrap_prefix);
18643 if (NILP (prefix))
18644 prefix = Vwrap_prefix;
18645 }
18646 else
18647 {
18648 prefix = get_it_property (it, Qline_prefix);
18649 if (NILP (prefix))
18650 prefix = Vline_prefix;
18651 }
18652 if (! NILP (prefix) && push_display_prop (it, prefix))
18653 {
18654 /* If the prefix is wider than the window, and we try to wrap
18655 it, it would acquire its own wrap prefix, and so on till the
18656 iterator stack overflows. So, don't wrap the prefix. */
18657 it->line_wrap = TRUNCATE;
18658 it->avoid_cursor_p = 1;
18659 }
18660 }
18661
18662 \f
18663
18664 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18665 only for R2L lines from display_line and display_string, when they
18666 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18667 the line/string needs to be continued on the next glyph row. */
18668 static void
18669 unproduce_glyphs (struct it *it, int n)
18670 {
18671 struct glyph *glyph, *end;
18672
18673 xassert (it->glyph_row);
18674 xassert (it->glyph_row->reversed_p);
18675 xassert (it->area == TEXT_AREA);
18676 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18677
18678 if (n > it->glyph_row->used[TEXT_AREA])
18679 n = it->glyph_row->used[TEXT_AREA];
18680 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18681 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18682 for ( ; glyph < end; glyph++)
18683 glyph[-n] = *glyph;
18684 }
18685
18686 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18687 and ROW->maxpos. */
18688 static void
18689 find_row_edges (struct it *it, struct glyph_row *row,
18690 EMACS_INT min_pos, EMACS_INT min_bpos,
18691 EMACS_INT max_pos, EMACS_INT max_bpos)
18692 {
18693 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18694 lines' rows is implemented for bidi-reordered rows. */
18695
18696 /* ROW->minpos is the value of min_pos, the minimal buffer position
18697 we have in ROW, or ROW->start.pos if that is smaller. */
18698 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18699 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18700 else
18701 /* We didn't find buffer positions smaller than ROW->start, or
18702 didn't find _any_ valid buffer positions in any of the glyphs,
18703 so we must trust the iterator's computed positions. */
18704 row->minpos = row->start.pos;
18705 if (max_pos <= 0)
18706 {
18707 max_pos = CHARPOS (it->current.pos);
18708 max_bpos = BYTEPOS (it->current.pos);
18709 }
18710
18711 /* Here are the various use-cases for ending the row, and the
18712 corresponding values for ROW->maxpos:
18713
18714 Line ends in a newline from buffer eol_pos + 1
18715 Line is continued from buffer max_pos + 1
18716 Line is truncated on right it->current.pos
18717 Line ends in a newline from string max_pos + 1(*)
18718 (*) + 1 only when line ends in a forward scan
18719 Line is continued from string max_pos
18720 Line is continued from display vector max_pos
18721 Line is entirely from a string min_pos == max_pos
18722 Line is entirely from a display vector min_pos == max_pos
18723 Line that ends at ZV ZV
18724
18725 If you discover other use-cases, please add them here as
18726 appropriate. */
18727 if (row->ends_at_zv_p)
18728 row->maxpos = it->current.pos;
18729 else if (row->used[TEXT_AREA])
18730 {
18731 int seen_this_string = 0;
18732 struct glyph_row *r1 = row - 1;
18733
18734 /* Did we see the same display string on the previous row? */
18735 if (STRINGP (it->object)
18736 /* this is not the first row */
18737 && row > it->w->desired_matrix->rows
18738 /* previous row is not the header line */
18739 && !r1->mode_line_p
18740 /* previous row also ends in a newline from a string */
18741 && r1->ends_in_newline_from_string_p)
18742 {
18743 struct glyph *start, *end;
18744
18745 /* Search for the last glyph of the previous row that came
18746 from buffer or string. Depending on whether the row is
18747 L2R or R2L, we need to process it front to back or the
18748 other way round. */
18749 if (!r1->reversed_p)
18750 {
18751 start = r1->glyphs[TEXT_AREA];
18752 end = start + r1->used[TEXT_AREA];
18753 /* Glyphs inserted by redisplay have an integer (zero)
18754 as their object. */
18755 while (end > start
18756 && INTEGERP ((end - 1)->object)
18757 && (end - 1)->charpos <= 0)
18758 --end;
18759 if (end > start)
18760 {
18761 if (EQ ((end - 1)->object, it->object))
18762 seen_this_string = 1;
18763 }
18764 else
18765 /* If all the glyphs of the previous row were inserted
18766 by redisplay, it means the previous row was
18767 produced from a single newline, which is only
18768 possible if that newline came from the same string
18769 as the one which produced this ROW. */
18770 seen_this_string = 1;
18771 }
18772 else
18773 {
18774 end = r1->glyphs[TEXT_AREA] - 1;
18775 start = end + r1->used[TEXT_AREA];
18776 while (end < start
18777 && INTEGERP ((end + 1)->object)
18778 && (end + 1)->charpos <= 0)
18779 ++end;
18780 if (end < start)
18781 {
18782 if (EQ ((end + 1)->object, it->object))
18783 seen_this_string = 1;
18784 }
18785 else
18786 seen_this_string = 1;
18787 }
18788 }
18789 /* Take note of each display string that covers a newline only
18790 once, the first time we see it. This is for when a display
18791 string includes more than one newline in it. */
18792 if (row->ends_in_newline_from_string_p && !seen_this_string)
18793 {
18794 /* If we were scanning the buffer forward when we displayed
18795 the string, we want to account for at least one buffer
18796 position that belongs to this row (position covered by
18797 the display string), so that cursor positioning will
18798 consider this row as a candidate when point is at the end
18799 of the visual line represented by this row. This is not
18800 required when scanning back, because max_pos will already
18801 have a much larger value. */
18802 if (CHARPOS (row->end.pos) > max_pos)
18803 INC_BOTH (max_pos, max_bpos);
18804 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18805 }
18806 else if (CHARPOS (it->eol_pos) > 0)
18807 SET_TEXT_POS (row->maxpos,
18808 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18809 else if (row->continued_p)
18810 {
18811 /* If max_pos is different from IT's current position, it
18812 means IT->method does not belong to the display element
18813 at max_pos. However, it also means that the display
18814 element at max_pos was displayed in its entirety on this
18815 line, which is equivalent to saying that the next line
18816 starts at the next buffer position. */
18817 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18818 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18819 else
18820 {
18821 INC_BOTH (max_pos, max_bpos);
18822 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18823 }
18824 }
18825 else if (row->truncated_on_right_p)
18826 /* display_line already called reseat_at_next_visible_line_start,
18827 which puts the iterator at the beginning of the next line, in
18828 the logical order. */
18829 row->maxpos = it->current.pos;
18830 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18831 /* A line that is entirely from a string/image/stretch... */
18832 row->maxpos = row->minpos;
18833 else
18834 abort ();
18835 }
18836 else
18837 row->maxpos = it->current.pos;
18838 }
18839
18840 /* Construct the glyph row IT->glyph_row in the desired matrix of
18841 IT->w from text at the current position of IT. See dispextern.h
18842 for an overview of struct it. Value is non-zero if
18843 IT->glyph_row displays text, as opposed to a line displaying ZV
18844 only. */
18845
18846 static int
18847 display_line (struct it *it)
18848 {
18849 struct glyph_row *row = it->glyph_row;
18850 Lisp_Object overlay_arrow_string;
18851 struct it wrap_it;
18852 void *wrap_data = NULL;
18853 int may_wrap = 0, wrap_x IF_LINT (= 0);
18854 int wrap_row_used = -1;
18855 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18856 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18857 int wrap_row_extra_line_spacing IF_LINT (= 0);
18858 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18859 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18860 int cvpos;
18861 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18862 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18863
18864 /* We always start displaying at hpos zero even if hscrolled. */
18865 xassert (it->hpos == 0 && it->current_x == 0);
18866
18867 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18868 >= it->w->desired_matrix->nrows)
18869 {
18870 it->w->nrows_scale_factor++;
18871 fonts_changed_p = 1;
18872 return 0;
18873 }
18874
18875 /* Is IT->w showing the region? */
18876 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18877
18878 /* Clear the result glyph row and enable it. */
18879 prepare_desired_row (row);
18880
18881 row->y = it->current_y;
18882 row->start = it->start;
18883 row->continuation_lines_width = it->continuation_lines_width;
18884 row->displays_text_p = 1;
18885 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18886 it->starts_in_middle_of_char_p = 0;
18887
18888 /* Arrange the overlays nicely for our purposes. Usually, we call
18889 display_line on only one line at a time, in which case this
18890 can't really hurt too much, or we call it on lines which appear
18891 one after another in the buffer, in which case all calls to
18892 recenter_overlay_lists but the first will be pretty cheap. */
18893 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18894
18895 /* Move over display elements that are not visible because we are
18896 hscrolled. This may stop at an x-position < IT->first_visible_x
18897 if the first glyph is partially visible or if we hit a line end. */
18898 if (it->current_x < it->first_visible_x)
18899 {
18900 this_line_min_pos = row->start.pos;
18901 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18902 MOVE_TO_POS | MOVE_TO_X);
18903 /* Record the smallest positions seen while we moved over
18904 display elements that are not visible. This is needed by
18905 redisplay_internal for optimizing the case where the cursor
18906 stays inside the same line. The rest of this function only
18907 considers positions that are actually displayed, so
18908 RECORD_MAX_MIN_POS will not otherwise record positions that
18909 are hscrolled to the left of the left edge of the window. */
18910 min_pos = CHARPOS (this_line_min_pos);
18911 min_bpos = BYTEPOS (this_line_min_pos);
18912 }
18913 else
18914 {
18915 /* We only do this when not calling `move_it_in_display_line_to'
18916 above, because move_it_in_display_line_to calls
18917 handle_line_prefix itself. */
18918 handle_line_prefix (it);
18919 }
18920
18921 /* Get the initial row height. This is either the height of the
18922 text hscrolled, if there is any, or zero. */
18923 row->ascent = it->max_ascent;
18924 row->height = it->max_ascent + it->max_descent;
18925 row->phys_ascent = it->max_phys_ascent;
18926 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18927 row->extra_line_spacing = it->max_extra_line_spacing;
18928
18929 /* Utility macro to record max and min buffer positions seen until now. */
18930 #define RECORD_MAX_MIN_POS(IT) \
18931 do \
18932 { \
18933 int composition_p = !STRINGP ((IT)->string) \
18934 && ((IT)->what == IT_COMPOSITION); \
18935 EMACS_INT current_pos = \
18936 composition_p ? (IT)->cmp_it.charpos \
18937 : IT_CHARPOS (*(IT)); \
18938 EMACS_INT current_bpos = \
18939 composition_p ? CHAR_TO_BYTE (current_pos) \
18940 : IT_BYTEPOS (*(IT)); \
18941 if (current_pos < min_pos) \
18942 { \
18943 min_pos = current_pos; \
18944 min_bpos = current_bpos; \
18945 } \
18946 if (IT_CHARPOS (*it) > max_pos) \
18947 { \
18948 max_pos = IT_CHARPOS (*it); \
18949 max_bpos = IT_BYTEPOS (*it); \
18950 } \
18951 } \
18952 while (0)
18953
18954 /* Loop generating characters. The loop is left with IT on the next
18955 character to display. */
18956 while (1)
18957 {
18958 int n_glyphs_before, hpos_before, x_before;
18959 int x, nglyphs;
18960 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18961
18962 /* Retrieve the next thing to display. Value is zero if end of
18963 buffer reached. */
18964 if (!get_next_display_element (it))
18965 {
18966 /* Maybe add a space at the end of this line that is used to
18967 display the cursor there under X. Set the charpos of the
18968 first glyph of blank lines not corresponding to any text
18969 to -1. */
18970 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18971 row->exact_window_width_line_p = 1;
18972 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18973 || row->used[TEXT_AREA] == 0)
18974 {
18975 row->glyphs[TEXT_AREA]->charpos = -1;
18976 row->displays_text_p = 0;
18977
18978 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18979 && (!MINI_WINDOW_P (it->w)
18980 || (minibuf_level && EQ (it->window, minibuf_window))))
18981 row->indicate_empty_line_p = 1;
18982 }
18983
18984 it->continuation_lines_width = 0;
18985 row->ends_at_zv_p = 1;
18986 /* A row that displays right-to-left text must always have
18987 its last face extended all the way to the end of line,
18988 even if this row ends in ZV, because we still write to
18989 the screen left to right. */
18990 if (row->reversed_p)
18991 extend_face_to_end_of_line (it);
18992 break;
18993 }
18994
18995 /* Now, get the metrics of what we want to display. This also
18996 generates glyphs in `row' (which is IT->glyph_row). */
18997 n_glyphs_before = row->used[TEXT_AREA];
18998 x = it->current_x;
18999
19000 /* Remember the line height so far in case the next element doesn't
19001 fit on the line. */
19002 if (it->line_wrap != TRUNCATE)
19003 {
19004 ascent = it->max_ascent;
19005 descent = it->max_descent;
19006 phys_ascent = it->max_phys_ascent;
19007 phys_descent = it->max_phys_descent;
19008
19009 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19010 {
19011 if (IT_DISPLAYING_WHITESPACE (it))
19012 may_wrap = 1;
19013 else if (may_wrap)
19014 {
19015 SAVE_IT (wrap_it, *it, wrap_data);
19016 wrap_x = x;
19017 wrap_row_used = row->used[TEXT_AREA];
19018 wrap_row_ascent = row->ascent;
19019 wrap_row_height = row->height;
19020 wrap_row_phys_ascent = row->phys_ascent;
19021 wrap_row_phys_height = row->phys_height;
19022 wrap_row_extra_line_spacing = row->extra_line_spacing;
19023 wrap_row_min_pos = min_pos;
19024 wrap_row_min_bpos = min_bpos;
19025 wrap_row_max_pos = max_pos;
19026 wrap_row_max_bpos = max_bpos;
19027 may_wrap = 0;
19028 }
19029 }
19030 }
19031
19032 PRODUCE_GLYPHS (it);
19033
19034 /* If this display element was in marginal areas, continue with
19035 the next one. */
19036 if (it->area != TEXT_AREA)
19037 {
19038 row->ascent = max (row->ascent, it->max_ascent);
19039 row->height = max (row->height, it->max_ascent + it->max_descent);
19040 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19041 row->phys_height = max (row->phys_height,
19042 it->max_phys_ascent + it->max_phys_descent);
19043 row->extra_line_spacing = max (row->extra_line_spacing,
19044 it->max_extra_line_spacing);
19045 set_iterator_to_next (it, 1);
19046 continue;
19047 }
19048
19049 /* Does the display element fit on the line? If we truncate
19050 lines, we should draw past the right edge of the window. If
19051 we don't truncate, we want to stop so that we can display the
19052 continuation glyph before the right margin. If lines are
19053 continued, there are two possible strategies for characters
19054 resulting in more than 1 glyph (e.g. tabs): Display as many
19055 glyphs as possible in this line and leave the rest for the
19056 continuation line, or display the whole element in the next
19057 line. Original redisplay did the former, so we do it also. */
19058 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19059 hpos_before = it->hpos;
19060 x_before = x;
19061
19062 if (/* Not a newline. */
19063 nglyphs > 0
19064 /* Glyphs produced fit entirely in the line. */
19065 && it->current_x < it->last_visible_x)
19066 {
19067 it->hpos += nglyphs;
19068 row->ascent = max (row->ascent, it->max_ascent);
19069 row->height = max (row->height, it->max_ascent + it->max_descent);
19070 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19071 row->phys_height = max (row->phys_height,
19072 it->max_phys_ascent + it->max_phys_descent);
19073 row->extra_line_spacing = max (row->extra_line_spacing,
19074 it->max_extra_line_spacing);
19075 if (it->current_x - it->pixel_width < it->first_visible_x)
19076 row->x = x - it->first_visible_x;
19077 /* Record the maximum and minimum buffer positions seen so
19078 far in glyphs that will be displayed by this row. */
19079 if (it->bidi_p)
19080 RECORD_MAX_MIN_POS (it);
19081 }
19082 else
19083 {
19084 int i, new_x;
19085 struct glyph *glyph;
19086
19087 for (i = 0; i < nglyphs; ++i, x = new_x)
19088 {
19089 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19090 new_x = x + glyph->pixel_width;
19091
19092 if (/* Lines are continued. */
19093 it->line_wrap != TRUNCATE
19094 && (/* Glyph doesn't fit on the line. */
19095 new_x > it->last_visible_x
19096 /* Or it fits exactly on a window system frame. */
19097 || (new_x == it->last_visible_x
19098 && FRAME_WINDOW_P (it->f))))
19099 {
19100 /* End of a continued line. */
19101
19102 if (it->hpos == 0
19103 || (new_x == it->last_visible_x
19104 && FRAME_WINDOW_P (it->f)))
19105 {
19106 /* Current glyph is the only one on the line or
19107 fits exactly on the line. We must continue
19108 the line because we can't draw the cursor
19109 after the glyph. */
19110 row->continued_p = 1;
19111 it->current_x = new_x;
19112 it->continuation_lines_width += new_x;
19113 ++it->hpos;
19114 if (i == nglyphs - 1)
19115 {
19116 /* If line-wrap is on, check if a previous
19117 wrap point was found. */
19118 if (wrap_row_used > 0
19119 /* Even if there is a previous wrap
19120 point, continue the line here as
19121 usual, if (i) the previous character
19122 was a space or tab AND (ii) the
19123 current character is not. */
19124 && (!may_wrap
19125 || IT_DISPLAYING_WHITESPACE (it)))
19126 goto back_to_wrap;
19127
19128 /* Record the maximum and minimum buffer
19129 positions seen so far in glyphs that will be
19130 displayed by this row. */
19131 if (it->bidi_p)
19132 RECORD_MAX_MIN_POS (it);
19133 set_iterator_to_next (it, 1);
19134 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19135 {
19136 if (!get_next_display_element (it))
19137 {
19138 row->exact_window_width_line_p = 1;
19139 it->continuation_lines_width = 0;
19140 row->continued_p = 0;
19141 row->ends_at_zv_p = 1;
19142 }
19143 else if (ITERATOR_AT_END_OF_LINE_P (it))
19144 {
19145 row->continued_p = 0;
19146 row->exact_window_width_line_p = 1;
19147 }
19148 }
19149 }
19150 else if (it->bidi_p)
19151 RECORD_MAX_MIN_POS (it);
19152 }
19153 else if (CHAR_GLYPH_PADDING_P (*glyph)
19154 && !FRAME_WINDOW_P (it->f))
19155 {
19156 /* A padding glyph that doesn't fit on this line.
19157 This means the whole character doesn't fit
19158 on the line. */
19159 if (row->reversed_p)
19160 unproduce_glyphs (it, row->used[TEXT_AREA]
19161 - n_glyphs_before);
19162 row->used[TEXT_AREA] = n_glyphs_before;
19163
19164 /* Fill the rest of the row with continuation
19165 glyphs like in 20.x. */
19166 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19167 < row->glyphs[1 + TEXT_AREA])
19168 produce_special_glyphs (it, IT_CONTINUATION);
19169
19170 row->continued_p = 1;
19171 it->current_x = x_before;
19172 it->continuation_lines_width += x_before;
19173
19174 /* Restore the height to what it was before the
19175 element not fitting on the line. */
19176 it->max_ascent = ascent;
19177 it->max_descent = descent;
19178 it->max_phys_ascent = phys_ascent;
19179 it->max_phys_descent = phys_descent;
19180 }
19181 else if (wrap_row_used > 0)
19182 {
19183 back_to_wrap:
19184 if (row->reversed_p)
19185 unproduce_glyphs (it,
19186 row->used[TEXT_AREA] - wrap_row_used);
19187 RESTORE_IT (it, &wrap_it, wrap_data);
19188 it->continuation_lines_width += wrap_x;
19189 row->used[TEXT_AREA] = wrap_row_used;
19190 row->ascent = wrap_row_ascent;
19191 row->height = wrap_row_height;
19192 row->phys_ascent = wrap_row_phys_ascent;
19193 row->phys_height = wrap_row_phys_height;
19194 row->extra_line_spacing = wrap_row_extra_line_spacing;
19195 min_pos = wrap_row_min_pos;
19196 min_bpos = wrap_row_min_bpos;
19197 max_pos = wrap_row_max_pos;
19198 max_bpos = wrap_row_max_bpos;
19199 row->continued_p = 1;
19200 row->ends_at_zv_p = 0;
19201 row->exact_window_width_line_p = 0;
19202 it->continuation_lines_width += x;
19203
19204 /* Make sure that a non-default face is extended
19205 up to the right margin of the window. */
19206 extend_face_to_end_of_line (it);
19207 }
19208 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19209 {
19210 /* A TAB that extends past the right edge of the
19211 window. This produces a single glyph on
19212 window system frames. We leave the glyph in
19213 this row and let it fill the row, but don't
19214 consume the TAB. */
19215 it->continuation_lines_width += it->last_visible_x;
19216 row->ends_in_middle_of_char_p = 1;
19217 row->continued_p = 1;
19218 glyph->pixel_width = it->last_visible_x - x;
19219 it->starts_in_middle_of_char_p = 1;
19220 }
19221 else
19222 {
19223 /* Something other than a TAB that draws past
19224 the right edge of the window. Restore
19225 positions to values before the element. */
19226 if (row->reversed_p)
19227 unproduce_glyphs (it, row->used[TEXT_AREA]
19228 - (n_glyphs_before + i));
19229 row->used[TEXT_AREA] = n_glyphs_before + i;
19230
19231 /* Display continuation glyphs. */
19232 if (!FRAME_WINDOW_P (it->f))
19233 produce_special_glyphs (it, IT_CONTINUATION);
19234 row->continued_p = 1;
19235
19236 it->current_x = x_before;
19237 it->continuation_lines_width += x;
19238 extend_face_to_end_of_line (it);
19239
19240 if (nglyphs > 1 && i > 0)
19241 {
19242 row->ends_in_middle_of_char_p = 1;
19243 it->starts_in_middle_of_char_p = 1;
19244 }
19245
19246 /* Restore the height to what it was before the
19247 element not fitting on the line. */
19248 it->max_ascent = ascent;
19249 it->max_descent = descent;
19250 it->max_phys_ascent = phys_ascent;
19251 it->max_phys_descent = phys_descent;
19252 }
19253
19254 break;
19255 }
19256 else if (new_x > it->first_visible_x)
19257 {
19258 /* Increment number of glyphs actually displayed. */
19259 ++it->hpos;
19260
19261 /* Record the maximum and minimum buffer positions
19262 seen so far in glyphs that will be displayed by
19263 this row. */
19264 if (it->bidi_p)
19265 RECORD_MAX_MIN_POS (it);
19266
19267 if (x < it->first_visible_x)
19268 /* Glyph is partially visible, i.e. row starts at
19269 negative X position. */
19270 row->x = x - it->first_visible_x;
19271 }
19272 else
19273 {
19274 /* Glyph is completely off the left margin of the
19275 window. This should not happen because of the
19276 move_it_in_display_line at the start of this
19277 function, unless the text display area of the
19278 window is empty. */
19279 xassert (it->first_visible_x <= it->last_visible_x);
19280 }
19281 }
19282 /* Even if this display element produced no glyphs at all,
19283 we want to record its position. */
19284 if (it->bidi_p && nglyphs == 0)
19285 RECORD_MAX_MIN_POS (it);
19286
19287 row->ascent = max (row->ascent, it->max_ascent);
19288 row->height = max (row->height, it->max_ascent + it->max_descent);
19289 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19290 row->phys_height = max (row->phys_height,
19291 it->max_phys_ascent + it->max_phys_descent);
19292 row->extra_line_spacing = max (row->extra_line_spacing,
19293 it->max_extra_line_spacing);
19294
19295 /* End of this display line if row is continued. */
19296 if (row->continued_p || row->ends_at_zv_p)
19297 break;
19298 }
19299
19300 at_end_of_line:
19301 /* Is this a line end? If yes, we're also done, after making
19302 sure that a non-default face is extended up to the right
19303 margin of the window. */
19304 if (ITERATOR_AT_END_OF_LINE_P (it))
19305 {
19306 int used_before = row->used[TEXT_AREA];
19307
19308 row->ends_in_newline_from_string_p = STRINGP (it->object);
19309
19310 /* Add a space at the end of the line that is used to
19311 display the cursor there. */
19312 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19313 append_space_for_newline (it, 0);
19314
19315 /* Extend the face to the end of the line. */
19316 extend_face_to_end_of_line (it);
19317
19318 /* Make sure we have the position. */
19319 if (used_before == 0)
19320 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19321
19322 /* Record the position of the newline, for use in
19323 find_row_edges. */
19324 it->eol_pos = it->current.pos;
19325
19326 /* Consume the line end. This skips over invisible lines. */
19327 set_iterator_to_next (it, 1);
19328 it->continuation_lines_width = 0;
19329 break;
19330 }
19331
19332 /* Proceed with next display element. Note that this skips
19333 over lines invisible because of selective display. */
19334 set_iterator_to_next (it, 1);
19335
19336 /* If we truncate lines, we are done when the last displayed
19337 glyphs reach past the right margin of the window. */
19338 if (it->line_wrap == TRUNCATE
19339 && (FRAME_WINDOW_P (it->f)
19340 ? (it->current_x >= it->last_visible_x)
19341 : (it->current_x > it->last_visible_x)))
19342 {
19343 /* Maybe add truncation glyphs. */
19344 if (!FRAME_WINDOW_P (it->f))
19345 {
19346 int i, n;
19347
19348 if (!row->reversed_p)
19349 {
19350 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19351 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19352 break;
19353 }
19354 else
19355 {
19356 for (i = 0; i < row->used[TEXT_AREA]; i++)
19357 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19358 break;
19359 /* Remove any padding glyphs at the front of ROW, to
19360 make room for the truncation glyphs we will be
19361 adding below. The loop below always inserts at
19362 least one truncation glyph, so also remove the
19363 last glyph added to ROW. */
19364 unproduce_glyphs (it, i + 1);
19365 /* Adjust i for the loop below. */
19366 i = row->used[TEXT_AREA] - (i + 1);
19367 }
19368
19369 for (n = row->used[TEXT_AREA]; i < n; ++i)
19370 {
19371 row->used[TEXT_AREA] = i;
19372 produce_special_glyphs (it, IT_TRUNCATION);
19373 }
19374 }
19375 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19376 {
19377 /* Don't truncate if we can overflow newline into fringe. */
19378 if (!get_next_display_element (it))
19379 {
19380 it->continuation_lines_width = 0;
19381 row->ends_at_zv_p = 1;
19382 row->exact_window_width_line_p = 1;
19383 break;
19384 }
19385 if (ITERATOR_AT_END_OF_LINE_P (it))
19386 {
19387 row->exact_window_width_line_p = 1;
19388 goto at_end_of_line;
19389 }
19390 }
19391
19392 row->truncated_on_right_p = 1;
19393 it->continuation_lines_width = 0;
19394 reseat_at_next_visible_line_start (it, 0);
19395 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19396 it->hpos = hpos_before;
19397 it->current_x = x_before;
19398 break;
19399 }
19400 }
19401
19402 if (wrap_data)
19403 bidi_unshelve_cache (wrap_data, 1);
19404
19405 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19406 at the left window margin. */
19407 if (it->first_visible_x
19408 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19409 {
19410 if (!FRAME_WINDOW_P (it->f))
19411 insert_left_trunc_glyphs (it);
19412 row->truncated_on_left_p = 1;
19413 }
19414
19415 /* Remember the position at which this line ends.
19416
19417 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19418 cannot be before the call to find_row_edges below, since that is
19419 where these positions are determined. */
19420 row->end = it->current;
19421 if (!it->bidi_p)
19422 {
19423 row->minpos = row->start.pos;
19424 row->maxpos = row->end.pos;
19425 }
19426 else
19427 {
19428 /* ROW->minpos and ROW->maxpos must be the smallest and
19429 `1 + the largest' buffer positions in ROW. But if ROW was
19430 bidi-reordered, these two positions can be anywhere in the
19431 row, so we must determine them now. */
19432 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19433 }
19434
19435 /* If the start of this line is the overlay arrow-position, then
19436 mark this glyph row as the one containing the overlay arrow.
19437 This is clearly a mess with variable size fonts. It would be
19438 better to let it be displayed like cursors under X. */
19439 if ((row->displays_text_p || !overlay_arrow_seen)
19440 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19441 !NILP (overlay_arrow_string)))
19442 {
19443 /* Overlay arrow in window redisplay is a fringe bitmap. */
19444 if (STRINGP (overlay_arrow_string))
19445 {
19446 struct glyph_row *arrow_row
19447 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19448 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19449 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19450 struct glyph *p = row->glyphs[TEXT_AREA];
19451 struct glyph *p2, *end;
19452
19453 /* Copy the arrow glyphs. */
19454 while (glyph < arrow_end)
19455 *p++ = *glyph++;
19456
19457 /* Throw away padding glyphs. */
19458 p2 = p;
19459 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19460 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19461 ++p2;
19462 if (p2 > p)
19463 {
19464 while (p2 < end)
19465 *p++ = *p2++;
19466 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19467 }
19468 }
19469 else
19470 {
19471 xassert (INTEGERP (overlay_arrow_string));
19472 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19473 }
19474 overlay_arrow_seen = 1;
19475 }
19476
19477 /* Highlight trailing whitespace. */
19478 if (!NILP (Vshow_trailing_whitespace))
19479 highlight_trailing_whitespace (it->f, it->glyph_row);
19480
19481 /* Compute pixel dimensions of this line. */
19482 compute_line_metrics (it);
19483
19484 /* Implementation note: No changes in the glyphs of ROW or in their
19485 faces can be done past this point, because compute_line_metrics
19486 computes ROW's hash value and stores it within the glyph_row
19487 structure. */
19488
19489 /* Record whether this row ends inside an ellipsis. */
19490 row->ends_in_ellipsis_p
19491 = (it->method == GET_FROM_DISPLAY_VECTOR
19492 && it->ellipsis_p);
19493
19494 /* Save fringe bitmaps in this row. */
19495 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19496 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19497 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19498 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19499
19500 it->left_user_fringe_bitmap = 0;
19501 it->left_user_fringe_face_id = 0;
19502 it->right_user_fringe_bitmap = 0;
19503 it->right_user_fringe_face_id = 0;
19504
19505 /* Maybe set the cursor. */
19506 cvpos = it->w->cursor.vpos;
19507 if ((cvpos < 0
19508 /* In bidi-reordered rows, keep checking for proper cursor
19509 position even if one has been found already, because buffer
19510 positions in such rows change non-linearly with ROW->VPOS,
19511 when a line is continued. One exception: when we are at ZV,
19512 display cursor on the first suitable glyph row, since all
19513 the empty rows after that also have their position set to ZV. */
19514 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19515 lines' rows is implemented for bidi-reordered rows. */
19516 || (it->bidi_p
19517 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19518 && PT >= MATRIX_ROW_START_CHARPOS (row)
19519 && PT <= MATRIX_ROW_END_CHARPOS (row)
19520 && cursor_row_p (row))
19521 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19522
19523 /* Prepare for the next line. This line starts horizontally at (X
19524 HPOS) = (0 0). Vertical positions are incremented. As a
19525 convenience for the caller, IT->glyph_row is set to the next
19526 row to be used. */
19527 it->current_x = it->hpos = 0;
19528 it->current_y += row->height;
19529 SET_TEXT_POS (it->eol_pos, 0, 0);
19530 ++it->vpos;
19531 ++it->glyph_row;
19532 /* The next row should by default use the same value of the
19533 reversed_p flag as this one. set_iterator_to_next decides when
19534 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19535 the flag accordingly. */
19536 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19537 it->glyph_row->reversed_p = row->reversed_p;
19538 it->start = row->end;
19539 return row->displays_text_p;
19540
19541 #undef RECORD_MAX_MIN_POS
19542 }
19543
19544 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19545 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19546 doc: /* Return paragraph direction at point in BUFFER.
19547 Value is either `left-to-right' or `right-to-left'.
19548 If BUFFER is omitted or nil, it defaults to the current buffer.
19549
19550 Paragraph direction determines how the text in the paragraph is displayed.
19551 In left-to-right paragraphs, text begins at the left margin of the window
19552 and the reading direction is generally left to right. In right-to-left
19553 paragraphs, text begins at the right margin and is read from right to left.
19554
19555 See also `bidi-paragraph-direction'. */)
19556 (Lisp_Object buffer)
19557 {
19558 struct buffer *buf = current_buffer;
19559 struct buffer *old = buf;
19560
19561 if (! NILP (buffer))
19562 {
19563 CHECK_BUFFER (buffer);
19564 buf = XBUFFER (buffer);
19565 }
19566
19567 if (NILP (BVAR (buf, bidi_display_reordering))
19568 || NILP (BVAR (buf, enable_multibyte_characters))
19569 /* When we are loading loadup.el, the character property tables
19570 needed for bidi iteration are not yet available. */
19571 || !NILP (Vpurify_flag))
19572 return Qleft_to_right;
19573 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19574 return BVAR (buf, bidi_paragraph_direction);
19575 else
19576 {
19577 /* Determine the direction from buffer text. We could try to
19578 use current_matrix if it is up to date, but this seems fast
19579 enough as it is. */
19580 struct bidi_it itb;
19581 EMACS_INT pos = BUF_PT (buf);
19582 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19583 int c;
19584 void *itb_data = bidi_shelve_cache ();
19585
19586 set_buffer_temp (buf);
19587 /* bidi_paragraph_init finds the base direction of the paragraph
19588 by searching forward from paragraph start. We need the base
19589 direction of the current or _previous_ paragraph, so we need
19590 to make sure we are within that paragraph. To that end, find
19591 the previous non-empty line. */
19592 if (pos >= ZV && pos > BEGV)
19593 {
19594 pos--;
19595 bytepos = CHAR_TO_BYTE (pos);
19596 }
19597 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19598 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19599 {
19600 while ((c = FETCH_BYTE (bytepos)) == '\n'
19601 || c == ' ' || c == '\t' || c == '\f')
19602 {
19603 if (bytepos <= BEGV_BYTE)
19604 break;
19605 bytepos--;
19606 pos--;
19607 }
19608 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19609 bytepos--;
19610 }
19611 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19612 itb.paragraph_dir = NEUTRAL_DIR;
19613 itb.string.s = NULL;
19614 itb.string.lstring = Qnil;
19615 itb.string.bufpos = 0;
19616 itb.string.unibyte = 0;
19617 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19618 bidi_unshelve_cache (itb_data, 0);
19619 set_buffer_temp (old);
19620 switch (itb.paragraph_dir)
19621 {
19622 case L2R:
19623 return Qleft_to_right;
19624 break;
19625 case R2L:
19626 return Qright_to_left;
19627 break;
19628 default:
19629 abort ();
19630 }
19631 }
19632 }
19633
19634
19635 \f
19636 /***********************************************************************
19637 Menu Bar
19638 ***********************************************************************/
19639
19640 /* Redisplay the menu bar in the frame for window W.
19641
19642 The menu bar of X frames that don't have X toolkit support is
19643 displayed in a special window W->frame->menu_bar_window.
19644
19645 The menu bar of terminal frames is treated specially as far as
19646 glyph matrices are concerned. Menu bar lines are not part of
19647 windows, so the update is done directly on the frame matrix rows
19648 for the menu bar. */
19649
19650 static void
19651 display_menu_bar (struct window *w)
19652 {
19653 struct frame *f = XFRAME (WINDOW_FRAME (w));
19654 struct it it;
19655 Lisp_Object items;
19656 int i;
19657
19658 /* Don't do all this for graphical frames. */
19659 #ifdef HAVE_NTGUI
19660 if (FRAME_W32_P (f))
19661 return;
19662 #endif
19663 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19664 if (FRAME_X_P (f))
19665 return;
19666 #endif
19667
19668 #ifdef HAVE_NS
19669 if (FRAME_NS_P (f))
19670 return;
19671 #endif /* HAVE_NS */
19672
19673 #ifdef USE_X_TOOLKIT
19674 xassert (!FRAME_WINDOW_P (f));
19675 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19676 it.first_visible_x = 0;
19677 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19678 #else /* not USE_X_TOOLKIT */
19679 if (FRAME_WINDOW_P (f))
19680 {
19681 /* Menu bar lines are displayed in the desired matrix of the
19682 dummy window menu_bar_window. */
19683 struct window *menu_w;
19684 xassert (WINDOWP (f->menu_bar_window));
19685 menu_w = XWINDOW (f->menu_bar_window);
19686 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19687 MENU_FACE_ID);
19688 it.first_visible_x = 0;
19689 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19690 }
19691 else
19692 {
19693 /* This is a TTY frame, i.e. character hpos/vpos are used as
19694 pixel x/y. */
19695 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19696 MENU_FACE_ID);
19697 it.first_visible_x = 0;
19698 it.last_visible_x = FRAME_COLS (f);
19699 }
19700 #endif /* not USE_X_TOOLKIT */
19701
19702 /* FIXME: This should be controlled by a user option. See the
19703 comments in redisplay_tool_bar and display_mode_line about
19704 this. */
19705 it.paragraph_embedding = L2R;
19706
19707 if (! mode_line_inverse_video)
19708 /* Force the menu-bar to be displayed in the default face. */
19709 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19710
19711 /* Clear all rows of the menu bar. */
19712 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19713 {
19714 struct glyph_row *row = it.glyph_row + i;
19715 clear_glyph_row (row);
19716 row->enabled_p = 1;
19717 row->full_width_p = 1;
19718 }
19719
19720 /* Display all items of the menu bar. */
19721 items = FRAME_MENU_BAR_ITEMS (it.f);
19722 for (i = 0; i < ASIZE (items); i += 4)
19723 {
19724 Lisp_Object string;
19725
19726 /* Stop at nil string. */
19727 string = AREF (items, i + 1);
19728 if (NILP (string))
19729 break;
19730
19731 /* Remember where item was displayed. */
19732 ASET (items, i + 3, make_number (it.hpos));
19733
19734 /* Display the item, pad with one space. */
19735 if (it.current_x < it.last_visible_x)
19736 display_string (NULL, string, Qnil, 0, 0, &it,
19737 SCHARS (string) + 1, 0, 0, -1);
19738 }
19739
19740 /* Fill out the line with spaces. */
19741 if (it.current_x < it.last_visible_x)
19742 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19743
19744 /* Compute the total height of the lines. */
19745 compute_line_metrics (&it);
19746 }
19747
19748
19749 \f
19750 /***********************************************************************
19751 Mode Line
19752 ***********************************************************************/
19753
19754 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19755 FORCE is non-zero, redisplay mode lines unconditionally.
19756 Otherwise, redisplay only mode lines that are garbaged. Value is
19757 the number of windows whose mode lines were redisplayed. */
19758
19759 static int
19760 redisplay_mode_lines (Lisp_Object window, int force)
19761 {
19762 int nwindows = 0;
19763
19764 while (!NILP (window))
19765 {
19766 struct window *w = XWINDOW (window);
19767
19768 if (WINDOWP (w->hchild))
19769 nwindows += redisplay_mode_lines (w->hchild, force);
19770 else if (WINDOWP (w->vchild))
19771 nwindows += redisplay_mode_lines (w->vchild, force);
19772 else if (force
19773 || FRAME_GARBAGED_P (XFRAME (w->frame))
19774 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19775 {
19776 struct text_pos lpoint;
19777 struct buffer *old = current_buffer;
19778
19779 /* Set the window's buffer for the mode line display. */
19780 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19781 set_buffer_internal_1 (XBUFFER (w->buffer));
19782
19783 /* Point refers normally to the selected window. For any
19784 other window, set up appropriate value. */
19785 if (!EQ (window, selected_window))
19786 {
19787 struct text_pos pt;
19788
19789 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19790 if (CHARPOS (pt) < BEGV)
19791 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19792 else if (CHARPOS (pt) > (ZV - 1))
19793 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19794 else
19795 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19796 }
19797
19798 /* Display mode lines. */
19799 clear_glyph_matrix (w->desired_matrix);
19800 if (display_mode_lines (w))
19801 {
19802 ++nwindows;
19803 w->must_be_updated_p = 1;
19804 }
19805
19806 /* Restore old settings. */
19807 set_buffer_internal_1 (old);
19808 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19809 }
19810
19811 window = w->next;
19812 }
19813
19814 return nwindows;
19815 }
19816
19817
19818 /* Display the mode and/or header line of window W. Value is the
19819 sum number of mode lines and header lines displayed. */
19820
19821 static int
19822 display_mode_lines (struct window *w)
19823 {
19824 Lisp_Object old_selected_window, old_selected_frame;
19825 int n = 0;
19826
19827 old_selected_frame = selected_frame;
19828 selected_frame = w->frame;
19829 old_selected_window = selected_window;
19830 XSETWINDOW (selected_window, w);
19831
19832 /* These will be set while the mode line specs are processed. */
19833 line_number_displayed = 0;
19834 w->column_number_displayed = Qnil;
19835
19836 if (WINDOW_WANTS_MODELINE_P (w))
19837 {
19838 struct window *sel_w = XWINDOW (old_selected_window);
19839
19840 /* Select mode line face based on the real selected window. */
19841 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19842 BVAR (current_buffer, mode_line_format));
19843 ++n;
19844 }
19845
19846 if (WINDOW_WANTS_HEADER_LINE_P (w))
19847 {
19848 display_mode_line (w, HEADER_LINE_FACE_ID,
19849 BVAR (current_buffer, header_line_format));
19850 ++n;
19851 }
19852
19853 selected_frame = old_selected_frame;
19854 selected_window = old_selected_window;
19855 return n;
19856 }
19857
19858
19859 /* Display mode or header line of window W. FACE_ID specifies which
19860 line to display; it is either MODE_LINE_FACE_ID or
19861 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19862 display. Value is the pixel height of the mode/header line
19863 displayed. */
19864
19865 static int
19866 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19867 {
19868 struct it it;
19869 struct face *face;
19870 int count = SPECPDL_INDEX ();
19871
19872 init_iterator (&it, w, -1, -1, NULL, face_id);
19873 /* Don't extend on a previously drawn mode-line.
19874 This may happen if called from pos_visible_p. */
19875 it.glyph_row->enabled_p = 0;
19876 prepare_desired_row (it.glyph_row);
19877
19878 it.glyph_row->mode_line_p = 1;
19879
19880 if (! mode_line_inverse_video)
19881 /* Force the mode-line to be displayed in the default face. */
19882 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19883
19884 /* FIXME: This should be controlled by a user option. But
19885 supporting such an option is not trivial, since the mode line is
19886 made up of many separate strings. */
19887 it.paragraph_embedding = L2R;
19888
19889 record_unwind_protect (unwind_format_mode_line,
19890 format_mode_line_unwind_data (NULL, Qnil, 0));
19891
19892 mode_line_target = MODE_LINE_DISPLAY;
19893
19894 /* Temporarily make frame's keyboard the current kboard so that
19895 kboard-local variables in the mode_line_format will get the right
19896 values. */
19897 push_kboard (FRAME_KBOARD (it.f));
19898 record_unwind_save_match_data ();
19899 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19900 pop_kboard ();
19901
19902 unbind_to (count, Qnil);
19903
19904 /* Fill up with spaces. */
19905 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19906
19907 compute_line_metrics (&it);
19908 it.glyph_row->full_width_p = 1;
19909 it.glyph_row->continued_p = 0;
19910 it.glyph_row->truncated_on_left_p = 0;
19911 it.glyph_row->truncated_on_right_p = 0;
19912
19913 /* Make a 3D mode-line have a shadow at its right end. */
19914 face = FACE_FROM_ID (it.f, face_id);
19915 extend_face_to_end_of_line (&it);
19916 if (face->box != FACE_NO_BOX)
19917 {
19918 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19919 + it.glyph_row->used[TEXT_AREA] - 1);
19920 last->right_box_line_p = 1;
19921 }
19922
19923 return it.glyph_row->height;
19924 }
19925
19926 /* Move element ELT in LIST to the front of LIST.
19927 Return the updated list. */
19928
19929 static Lisp_Object
19930 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19931 {
19932 register Lisp_Object tail, prev;
19933 register Lisp_Object tem;
19934
19935 tail = list;
19936 prev = Qnil;
19937 while (CONSP (tail))
19938 {
19939 tem = XCAR (tail);
19940
19941 if (EQ (elt, tem))
19942 {
19943 /* Splice out the link TAIL. */
19944 if (NILP (prev))
19945 list = XCDR (tail);
19946 else
19947 Fsetcdr (prev, XCDR (tail));
19948
19949 /* Now make it the first. */
19950 Fsetcdr (tail, list);
19951 return tail;
19952 }
19953 else
19954 prev = tail;
19955 tail = XCDR (tail);
19956 QUIT;
19957 }
19958
19959 /* Not found--return unchanged LIST. */
19960 return list;
19961 }
19962
19963 /* Contribute ELT to the mode line for window IT->w. How it
19964 translates into text depends on its data type.
19965
19966 IT describes the display environment in which we display, as usual.
19967
19968 DEPTH is the depth in recursion. It is used to prevent
19969 infinite recursion here.
19970
19971 FIELD_WIDTH is the number of characters the display of ELT should
19972 occupy in the mode line, and PRECISION is the maximum number of
19973 characters to display from ELT's representation. See
19974 display_string for details.
19975
19976 Returns the hpos of the end of the text generated by ELT.
19977
19978 PROPS is a property list to add to any string we encounter.
19979
19980 If RISKY is nonzero, remove (disregard) any properties in any string
19981 we encounter, and ignore :eval and :propertize.
19982
19983 The global variable `mode_line_target' determines whether the
19984 output is passed to `store_mode_line_noprop',
19985 `store_mode_line_string', or `display_string'. */
19986
19987 static int
19988 display_mode_element (struct it *it, int depth, int field_width, int precision,
19989 Lisp_Object elt, Lisp_Object props, int risky)
19990 {
19991 int n = 0, field, prec;
19992 int literal = 0;
19993
19994 tail_recurse:
19995 if (depth > 100)
19996 elt = build_string ("*too-deep*");
19997
19998 depth++;
19999
20000 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20001 {
20002 case Lisp_String:
20003 {
20004 /* A string: output it and check for %-constructs within it. */
20005 unsigned char c;
20006 EMACS_INT offset = 0;
20007
20008 if (SCHARS (elt) > 0
20009 && (!NILP (props) || risky))
20010 {
20011 Lisp_Object oprops, aelt;
20012 oprops = Ftext_properties_at (make_number (0), elt);
20013
20014 /* If the starting string's properties are not what
20015 we want, translate the string. Also, if the string
20016 is risky, do that anyway. */
20017
20018 if (NILP (Fequal (props, oprops)) || risky)
20019 {
20020 /* If the starting string has properties,
20021 merge the specified ones onto the existing ones. */
20022 if (! NILP (oprops) && !risky)
20023 {
20024 Lisp_Object tem;
20025
20026 oprops = Fcopy_sequence (oprops);
20027 tem = props;
20028 while (CONSP (tem))
20029 {
20030 oprops = Fplist_put (oprops, XCAR (tem),
20031 XCAR (XCDR (tem)));
20032 tem = XCDR (XCDR (tem));
20033 }
20034 props = oprops;
20035 }
20036
20037 aelt = Fassoc (elt, mode_line_proptrans_alist);
20038 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20039 {
20040 /* AELT is what we want. Move it to the front
20041 without consing. */
20042 elt = XCAR (aelt);
20043 mode_line_proptrans_alist
20044 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20045 }
20046 else
20047 {
20048 Lisp_Object tem;
20049
20050 /* If AELT has the wrong props, it is useless.
20051 so get rid of it. */
20052 if (! NILP (aelt))
20053 mode_line_proptrans_alist
20054 = Fdelq (aelt, mode_line_proptrans_alist);
20055
20056 elt = Fcopy_sequence (elt);
20057 Fset_text_properties (make_number (0), Flength (elt),
20058 props, elt);
20059 /* Add this item to mode_line_proptrans_alist. */
20060 mode_line_proptrans_alist
20061 = Fcons (Fcons (elt, props),
20062 mode_line_proptrans_alist);
20063 /* Truncate mode_line_proptrans_alist
20064 to at most 50 elements. */
20065 tem = Fnthcdr (make_number (50),
20066 mode_line_proptrans_alist);
20067 if (! NILP (tem))
20068 XSETCDR (tem, Qnil);
20069 }
20070 }
20071 }
20072
20073 offset = 0;
20074
20075 if (literal)
20076 {
20077 prec = precision - n;
20078 switch (mode_line_target)
20079 {
20080 case MODE_LINE_NOPROP:
20081 case MODE_LINE_TITLE:
20082 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20083 break;
20084 case MODE_LINE_STRING:
20085 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20086 break;
20087 case MODE_LINE_DISPLAY:
20088 n += display_string (NULL, elt, Qnil, 0, 0, it,
20089 0, prec, 0, STRING_MULTIBYTE (elt));
20090 break;
20091 }
20092
20093 break;
20094 }
20095
20096 /* Handle the non-literal case. */
20097
20098 while ((precision <= 0 || n < precision)
20099 && SREF (elt, offset) != 0
20100 && (mode_line_target != MODE_LINE_DISPLAY
20101 || it->current_x < it->last_visible_x))
20102 {
20103 EMACS_INT last_offset = offset;
20104
20105 /* Advance to end of string or next format specifier. */
20106 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20107 ;
20108
20109 if (offset - 1 != last_offset)
20110 {
20111 EMACS_INT nchars, nbytes;
20112
20113 /* Output to end of string or up to '%'. Field width
20114 is length of string. Don't output more than
20115 PRECISION allows us. */
20116 offset--;
20117
20118 prec = c_string_width (SDATA (elt) + last_offset,
20119 offset - last_offset, precision - n,
20120 &nchars, &nbytes);
20121
20122 switch (mode_line_target)
20123 {
20124 case MODE_LINE_NOPROP:
20125 case MODE_LINE_TITLE:
20126 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20127 break;
20128 case MODE_LINE_STRING:
20129 {
20130 EMACS_INT bytepos = last_offset;
20131 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20132 EMACS_INT endpos = (precision <= 0
20133 ? string_byte_to_char (elt, offset)
20134 : charpos + nchars);
20135
20136 n += store_mode_line_string (NULL,
20137 Fsubstring (elt, make_number (charpos),
20138 make_number (endpos)),
20139 0, 0, 0, Qnil);
20140 }
20141 break;
20142 case MODE_LINE_DISPLAY:
20143 {
20144 EMACS_INT bytepos = last_offset;
20145 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20146
20147 if (precision <= 0)
20148 nchars = string_byte_to_char (elt, offset) - charpos;
20149 n += display_string (NULL, elt, Qnil, 0, charpos,
20150 it, 0, nchars, 0,
20151 STRING_MULTIBYTE (elt));
20152 }
20153 break;
20154 }
20155 }
20156 else /* c == '%' */
20157 {
20158 EMACS_INT percent_position = offset;
20159
20160 /* Get the specified minimum width. Zero means
20161 don't pad. */
20162 field = 0;
20163 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20164 field = field * 10 + c - '0';
20165
20166 /* Don't pad beyond the total padding allowed. */
20167 if (field_width - n > 0 && field > field_width - n)
20168 field = field_width - n;
20169
20170 /* Note that either PRECISION <= 0 or N < PRECISION. */
20171 prec = precision - n;
20172
20173 if (c == 'M')
20174 n += display_mode_element (it, depth, field, prec,
20175 Vglobal_mode_string, props,
20176 risky);
20177 else if (c != 0)
20178 {
20179 int multibyte;
20180 EMACS_INT bytepos, charpos;
20181 const char *spec;
20182 Lisp_Object string;
20183
20184 bytepos = percent_position;
20185 charpos = (STRING_MULTIBYTE (elt)
20186 ? string_byte_to_char (elt, bytepos)
20187 : bytepos);
20188 spec = decode_mode_spec (it->w, c, field, &string);
20189 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20190
20191 switch (mode_line_target)
20192 {
20193 case MODE_LINE_NOPROP:
20194 case MODE_LINE_TITLE:
20195 n += store_mode_line_noprop (spec, field, prec);
20196 break;
20197 case MODE_LINE_STRING:
20198 {
20199 Lisp_Object tem = build_string (spec);
20200 props = Ftext_properties_at (make_number (charpos), elt);
20201 /* Should only keep face property in props */
20202 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20203 }
20204 break;
20205 case MODE_LINE_DISPLAY:
20206 {
20207 int nglyphs_before, nwritten;
20208
20209 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20210 nwritten = display_string (spec, string, elt,
20211 charpos, 0, it,
20212 field, prec, 0,
20213 multibyte);
20214
20215 /* Assign to the glyphs written above the
20216 string where the `%x' came from, position
20217 of the `%'. */
20218 if (nwritten > 0)
20219 {
20220 struct glyph *glyph
20221 = (it->glyph_row->glyphs[TEXT_AREA]
20222 + nglyphs_before);
20223 int i;
20224
20225 for (i = 0; i < nwritten; ++i)
20226 {
20227 glyph[i].object = elt;
20228 glyph[i].charpos = charpos;
20229 }
20230
20231 n += nwritten;
20232 }
20233 }
20234 break;
20235 }
20236 }
20237 else /* c == 0 */
20238 break;
20239 }
20240 }
20241 }
20242 break;
20243
20244 case Lisp_Symbol:
20245 /* A symbol: process the value of the symbol recursively
20246 as if it appeared here directly. Avoid error if symbol void.
20247 Special case: if value of symbol is a string, output the string
20248 literally. */
20249 {
20250 register Lisp_Object tem;
20251
20252 /* If the variable is not marked as risky to set
20253 then its contents are risky to use. */
20254 if (NILP (Fget (elt, Qrisky_local_variable)))
20255 risky = 1;
20256
20257 tem = Fboundp (elt);
20258 if (!NILP (tem))
20259 {
20260 tem = Fsymbol_value (elt);
20261 /* If value is a string, output that string literally:
20262 don't check for % within it. */
20263 if (STRINGP (tem))
20264 literal = 1;
20265
20266 if (!EQ (tem, elt))
20267 {
20268 /* Give up right away for nil or t. */
20269 elt = tem;
20270 goto tail_recurse;
20271 }
20272 }
20273 }
20274 break;
20275
20276 case Lisp_Cons:
20277 {
20278 register Lisp_Object car, tem;
20279
20280 /* A cons cell: five distinct cases.
20281 If first element is :eval or :propertize, do something special.
20282 If first element is a string or a cons, process all the elements
20283 and effectively concatenate them.
20284 If first element is a negative number, truncate displaying cdr to
20285 at most that many characters. If positive, pad (with spaces)
20286 to at least that many characters.
20287 If first element is a symbol, process the cadr or caddr recursively
20288 according to whether the symbol's value is non-nil or nil. */
20289 car = XCAR (elt);
20290 if (EQ (car, QCeval))
20291 {
20292 /* An element of the form (:eval FORM) means evaluate FORM
20293 and use the result as mode line elements. */
20294
20295 if (risky)
20296 break;
20297
20298 if (CONSP (XCDR (elt)))
20299 {
20300 Lisp_Object spec;
20301 spec = safe_eval (XCAR (XCDR (elt)));
20302 n += display_mode_element (it, depth, field_width - n,
20303 precision - n, spec, props,
20304 risky);
20305 }
20306 }
20307 else if (EQ (car, QCpropertize))
20308 {
20309 /* An element of the form (:propertize ELT PROPS...)
20310 means display ELT but applying properties PROPS. */
20311
20312 if (risky)
20313 break;
20314
20315 if (CONSP (XCDR (elt)))
20316 n += display_mode_element (it, depth, field_width - n,
20317 precision - n, XCAR (XCDR (elt)),
20318 XCDR (XCDR (elt)), risky);
20319 }
20320 else if (SYMBOLP (car))
20321 {
20322 tem = Fboundp (car);
20323 elt = XCDR (elt);
20324 if (!CONSP (elt))
20325 goto invalid;
20326 /* elt is now the cdr, and we know it is a cons cell.
20327 Use its car if CAR has a non-nil value. */
20328 if (!NILP (tem))
20329 {
20330 tem = Fsymbol_value (car);
20331 if (!NILP (tem))
20332 {
20333 elt = XCAR (elt);
20334 goto tail_recurse;
20335 }
20336 }
20337 /* Symbol's value is nil (or symbol is unbound)
20338 Get the cddr of the original list
20339 and if possible find the caddr and use that. */
20340 elt = XCDR (elt);
20341 if (NILP (elt))
20342 break;
20343 else if (!CONSP (elt))
20344 goto invalid;
20345 elt = XCAR (elt);
20346 goto tail_recurse;
20347 }
20348 else if (INTEGERP (car))
20349 {
20350 register int lim = XINT (car);
20351 elt = XCDR (elt);
20352 if (lim < 0)
20353 {
20354 /* Negative int means reduce maximum width. */
20355 if (precision <= 0)
20356 precision = -lim;
20357 else
20358 precision = min (precision, -lim);
20359 }
20360 else if (lim > 0)
20361 {
20362 /* Padding specified. Don't let it be more than
20363 current maximum. */
20364 if (precision > 0)
20365 lim = min (precision, lim);
20366
20367 /* If that's more padding than already wanted, queue it.
20368 But don't reduce padding already specified even if
20369 that is beyond the current truncation point. */
20370 field_width = max (lim, field_width);
20371 }
20372 goto tail_recurse;
20373 }
20374 else if (STRINGP (car) || CONSP (car))
20375 {
20376 Lisp_Object halftail = elt;
20377 int len = 0;
20378
20379 while (CONSP (elt)
20380 && (precision <= 0 || n < precision))
20381 {
20382 n += display_mode_element (it, depth,
20383 /* Do padding only after the last
20384 element in the list. */
20385 (! CONSP (XCDR (elt))
20386 ? field_width - n
20387 : 0),
20388 precision - n, XCAR (elt),
20389 props, risky);
20390 elt = XCDR (elt);
20391 len++;
20392 if ((len & 1) == 0)
20393 halftail = XCDR (halftail);
20394 /* Check for cycle. */
20395 if (EQ (halftail, elt))
20396 break;
20397 }
20398 }
20399 }
20400 break;
20401
20402 default:
20403 invalid:
20404 elt = build_string ("*invalid*");
20405 goto tail_recurse;
20406 }
20407
20408 /* Pad to FIELD_WIDTH. */
20409 if (field_width > 0 && n < field_width)
20410 {
20411 switch (mode_line_target)
20412 {
20413 case MODE_LINE_NOPROP:
20414 case MODE_LINE_TITLE:
20415 n += store_mode_line_noprop ("", field_width - n, 0);
20416 break;
20417 case MODE_LINE_STRING:
20418 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20419 break;
20420 case MODE_LINE_DISPLAY:
20421 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20422 0, 0, 0);
20423 break;
20424 }
20425 }
20426
20427 return n;
20428 }
20429
20430 /* Store a mode-line string element in mode_line_string_list.
20431
20432 If STRING is non-null, display that C string. Otherwise, the Lisp
20433 string LISP_STRING is displayed.
20434
20435 FIELD_WIDTH is the minimum number of output glyphs to produce.
20436 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20437 with spaces. FIELD_WIDTH <= 0 means don't pad.
20438
20439 PRECISION is the maximum number of characters to output from
20440 STRING. PRECISION <= 0 means don't truncate the string.
20441
20442 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20443 properties to the string.
20444
20445 PROPS are the properties to add to the string.
20446 The mode_line_string_face face property is always added to the string.
20447 */
20448
20449 static int
20450 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20451 int field_width, int precision, Lisp_Object props)
20452 {
20453 EMACS_INT len;
20454 int n = 0;
20455
20456 if (string != NULL)
20457 {
20458 len = strlen (string);
20459 if (precision > 0 && len > precision)
20460 len = precision;
20461 lisp_string = make_string (string, len);
20462 if (NILP (props))
20463 props = mode_line_string_face_prop;
20464 else if (!NILP (mode_line_string_face))
20465 {
20466 Lisp_Object face = Fplist_get (props, Qface);
20467 props = Fcopy_sequence (props);
20468 if (NILP (face))
20469 face = mode_line_string_face;
20470 else
20471 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20472 props = Fplist_put (props, Qface, face);
20473 }
20474 Fadd_text_properties (make_number (0), make_number (len),
20475 props, lisp_string);
20476 }
20477 else
20478 {
20479 len = XFASTINT (Flength (lisp_string));
20480 if (precision > 0 && len > precision)
20481 {
20482 len = precision;
20483 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20484 precision = -1;
20485 }
20486 if (!NILP (mode_line_string_face))
20487 {
20488 Lisp_Object face;
20489 if (NILP (props))
20490 props = Ftext_properties_at (make_number (0), lisp_string);
20491 face = Fplist_get (props, Qface);
20492 if (NILP (face))
20493 face = mode_line_string_face;
20494 else
20495 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20496 props = Fcons (Qface, Fcons (face, Qnil));
20497 if (copy_string)
20498 lisp_string = Fcopy_sequence (lisp_string);
20499 }
20500 if (!NILP (props))
20501 Fadd_text_properties (make_number (0), make_number (len),
20502 props, lisp_string);
20503 }
20504
20505 if (len > 0)
20506 {
20507 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20508 n += len;
20509 }
20510
20511 if (field_width > len)
20512 {
20513 field_width -= len;
20514 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20515 if (!NILP (props))
20516 Fadd_text_properties (make_number (0), make_number (field_width),
20517 props, lisp_string);
20518 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20519 n += field_width;
20520 }
20521
20522 return n;
20523 }
20524
20525
20526 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20527 1, 4, 0,
20528 doc: /* Format a string out of a mode line format specification.
20529 First arg FORMAT specifies the mode line format (see `mode-line-format'
20530 for details) to use.
20531
20532 By default, the format is evaluated for the currently selected window.
20533
20534 Optional second arg FACE specifies the face property to put on all
20535 characters for which no face is specified. The value nil means the
20536 default face. The value t means whatever face the window's mode line
20537 currently uses (either `mode-line' or `mode-line-inactive',
20538 depending on whether the window is the selected window or not).
20539 An integer value means the value string has no text
20540 properties.
20541
20542 Optional third and fourth args WINDOW and BUFFER specify the window
20543 and buffer to use as the context for the formatting (defaults
20544 are the selected window and the WINDOW's buffer). */)
20545 (Lisp_Object format, Lisp_Object face,
20546 Lisp_Object window, Lisp_Object buffer)
20547 {
20548 struct it it;
20549 int len;
20550 struct window *w;
20551 struct buffer *old_buffer = NULL;
20552 int face_id;
20553 int no_props = INTEGERP (face);
20554 int count = SPECPDL_INDEX ();
20555 Lisp_Object str;
20556 int string_start = 0;
20557
20558 if (NILP (window))
20559 window = selected_window;
20560 CHECK_WINDOW (window);
20561 w = XWINDOW (window);
20562
20563 if (NILP (buffer))
20564 buffer = w->buffer;
20565 CHECK_BUFFER (buffer);
20566
20567 /* Make formatting the modeline a non-op when noninteractive, otherwise
20568 there will be problems later caused by a partially initialized frame. */
20569 if (NILP (format) || noninteractive)
20570 return empty_unibyte_string;
20571
20572 if (no_props)
20573 face = Qnil;
20574
20575 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20576 : EQ (face, Qt) ? (EQ (window, selected_window)
20577 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20578 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20579 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20580 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20581 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20582 : DEFAULT_FACE_ID;
20583
20584 if (XBUFFER (buffer) != current_buffer)
20585 old_buffer = current_buffer;
20586
20587 /* Save things including mode_line_proptrans_alist,
20588 and set that to nil so that we don't alter the outer value. */
20589 record_unwind_protect (unwind_format_mode_line,
20590 format_mode_line_unwind_data
20591 (old_buffer, selected_window, 1));
20592 mode_line_proptrans_alist = Qnil;
20593
20594 Fselect_window (window, Qt);
20595 if (old_buffer)
20596 set_buffer_internal_1 (XBUFFER (buffer));
20597
20598 init_iterator (&it, w, -1, -1, NULL, face_id);
20599
20600 if (no_props)
20601 {
20602 mode_line_target = MODE_LINE_NOPROP;
20603 mode_line_string_face_prop = Qnil;
20604 mode_line_string_list = Qnil;
20605 string_start = MODE_LINE_NOPROP_LEN (0);
20606 }
20607 else
20608 {
20609 mode_line_target = MODE_LINE_STRING;
20610 mode_line_string_list = Qnil;
20611 mode_line_string_face = face;
20612 mode_line_string_face_prop
20613 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20614 }
20615
20616 push_kboard (FRAME_KBOARD (it.f));
20617 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20618 pop_kboard ();
20619
20620 if (no_props)
20621 {
20622 len = MODE_LINE_NOPROP_LEN (string_start);
20623 str = make_string (mode_line_noprop_buf + string_start, len);
20624 }
20625 else
20626 {
20627 mode_line_string_list = Fnreverse (mode_line_string_list);
20628 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20629 empty_unibyte_string);
20630 }
20631
20632 unbind_to (count, Qnil);
20633 return str;
20634 }
20635
20636 /* Write a null-terminated, right justified decimal representation of
20637 the positive integer D to BUF using a minimal field width WIDTH. */
20638
20639 static void
20640 pint2str (register char *buf, register int width, register EMACS_INT d)
20641 {
20642 register char *p = buf;
20643
20644 if (d <= 0)
20645 *p++ = '0';
20646 else
20647 {
20648 while (d > 0)
20649 {
20650 *p++ = d % 10 + '0';
20651 d /= 10;
20652 }
20653 }
20654
20655 for (width -= (int) (p - buf); width > 0; --width)
20656 *p++ = ' ';
20657 *p-- = '\0';
20658 while (p > buf)
20659 {
20660 d = *buf;
20661 *buf++ = *p;
20662 *p-- = d;
20663 }
20664 }
20665
20666 /* Write a null-terminated, right justified decimal and "human
20667 readable" representation of the nonnegative integer D to BUF using
20668 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20669
20670 static const char power_letter[] =
20671 {
20672 0, /* no letter */
20673 'k', /* kilo */
20674 'M', /* mega */
20675 'G', /* giga */
20676 'T', /* tera */
20677 'P', /* peta */
20678 'E', /* exa */
20679 'Z', /* zetta */
20680 'Y' /* yotta */
20681 };
20682
20683 static void
20684 pint2hrstr (char *buf, int width, EMACS_INT d)
20685 {
20686 /* We aim to represent the nonnegative integer D as
20687 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20688 EMACS_INT quotient = d;
20689 int remainder = 0;
20690 /* -1 means: do not use TENTHS. */
20691 int tenths = -1;
20692 int exponent = 0;
20693
20694 /* Length of QUOTIENT.TENTHS as a string. */
20695 int length;
20696
20697 char * psuffix;
20698 char * p;
20699
20700 if (1000 <= quotient)
20701 {
20702 /* Scale to the appropriate EXPONENT. */
20703 do
20704 {
20705 remainder = quotient % 1000;
20706 quotient /= 1000;
20707 exponent++;
20708 }
20709 while (1000 <= quotient);
20710
20711 /* Round to nearest and decide whether to use TENTHS or not. */
20712 if (quotient <= 9)
20713 {
20714 tenths = remainder / 100;
20715 if (50 <= remainder % 100)
20716 {
20717 if (tenths < 9)
20718 tenths++;
20719 else
20720 {
20721 quotient++;
20722 if (quotient == 10)
20723 tenths = -1;
20724 else
20725 tenths = 0;
20726 }
20727 }
20728 }
20729 else
20730 if (500 <= remainder)
20731 {
20732 if (quotient < 999)
20733 quotient++;
20734 else
20735 {
20736 quotient = 1;
20737 exponent++;
20738 tenths = 0;
20739 }
20740 }
20741 }
20742
20743 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20744 if (tenths == -1 && quotient <= 99)
20745 if (quotient <= 9)
20746 length = 1;
20747 else
20748 length = 2;
20749 else
20750 length = 3;
20751 p = psuffix = buf + max (width, length);
20752
20753 /* Print EXPONENT. */
20754 *psuffix++ = power_letter[exponent];
20755 *psuffix = '\0';
20756
20757 /* Print TENTHS. */
20758 if (tenths >= 0)
20759 {
20760 *--p = '0' + tenths;
20761 *--p = '.';
20762 }
20763
20764 /* Print QUOTIENT. */
20765 do
20766 {
20767 int digit = quotient % 10;
20768 *--p = '0' + digit;
20769 }
20770 while ((quotient /= 10) != 0);
20771
20772 /* Print leading spaces. */
20773 while (buf < p)
20774 *--p = ' ';
20775 }
20776
20777 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20778 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20779 type of CODING_SYSTEM. Return updated pointer into BUF. */
20780
20781 static unsigned char invalid_eol_type[] = "(*invalid*)";
20782
20783 static char *
20784 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20785 {
20786 Lisp_Object val;
20787 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20788 const unsigned char *eol_str;
20789 int eol_str_len;
20790 /* The EOL conversion we are using. */
20791 Lisp_Object eoltype;
20792
20793 val = CODING_SYSTEM_SPEC (coding_system);
20794 eoltype = Qnil;
20795
20796 if (!VECTORP (val)) /* Not yet decided. */
20797 {
20798 if (multibyte)
20799 *buf++ = '-';
20800 if (eol_flag)
20801 eoltype = eol_mnemonic_undecided;
20802 /* Don't mention EOL conversion if it isn't decided. */
20803 }
20804 else
20805 {
20806 Lisp_Object attrs;
20807 Lisp_Object eolvalue;
20808
20809 attrs = AREF (val, 0);
20810 eolvalue = AREF (val, 2);
20811
20812 if (multibyte)
20813 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20814
20815 if (eol_flag)
20816 {
20817 /* The EOL conversion that is normal on this system. */
20818
20819 if (NILP (eolvalue)) /* Not yet decided. */
20820 eoltype = eol_mnemonic_undecided;
20821 else if (VECTORP (eolvalue)) /* Not yet decided. */
20822 eoltype = eol_mnemonic_undecided;
20823 else /* eolvalue is Qunix, Qdos, or Qmac. */
20824 eoltype = (EQ (eolvalue, Qunix)
20825 ? eol_mnemonic_unix
20826 : (EQ (eolvalue, Qdos) == 1
20827 ? eol_mnemonic_dos : eol_mnemonic_mac));
20828 }
20829 }
20830
20831 if (eol_flag)
20832 {
20833 /* Mention the EOL conversion if it is not the usual one. */
20834 if (STRINGP (eoltype))
20835 {
20836 eol_str = SDATA (eoltype);
20837 eol_str_len = SBYTES (eoltype);
20838 }
20839 else if (CHARACTERP (eoltype))
20840 {
20841 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20842 int c = XFASTINT (eoltype);
20843 eol_str_len = CHAR_STRING (c, tmp);
20844 eol_str = tmp;
20845 }
20846 else
20847 {
20848 eol_str = invalid_eol_type;
20849 eol_str_len = sizeof (invalid_eol_type) - 1;
20850 }
20851 memcpy (buf, eol_str, eol_str_len);
20852 buf += eol_str_len;
20853 }
20854
20855 return buf;
20856 }
20857
20858 /* Return a string for the output of a mode line %-spec for window W,
20859 generated by character C. FIELD_WIDTH > 0 means pad the string
20860 returned with spaces to that value. Return a Lisp string in
20861 *STRING if the resulting string is taken from that Lisp string.
20862
20863 Note we operate on the current buffer for most purposes,
20864 the exception being w->base_line_pos. */
20865
20866 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20867
20868 static const char *
20869 decode_mode_spec (struct window *w, register int c, int field_width,
20870 Lisp_Object *string)
20871 {
20872 Lisp_Object obj;
20873 struct frame *f = XFRAME (WINDOW_FRAME (w));
20874 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20875 struct buffer *b = current_buffer;
20876
20877 obj = Qnil;
20878 *string = Qnil;
20879
20880 switch (c)
20881 {
20882 case '*':
20883 if (!NILP (BVAR (b, read_only)))
20884 return "%";
20885 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20886 return "*";
20887 return "-";
20888
20889 case '+':
20890 /* This differs from %* only for a modified read-only buffer. */
20891 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20892 return "*";
20893 if (!NILP (BVAR (b, read_only)))
20894 return "%";
20895 return "-";
20896
20897 case '&':
20898 /* This differs from %* in ignoring read-only-ness. */
20899 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20900 return "*";
20901 return "-";
20902
20903 case '%':
20904 return "%";
20905
20906 case '[':
20907 {
20908 int i;
20909 char *p;
20910
20911 if (command_loop_level > 5)
20912 return "[[[... ";
20913 p = decode_mode_spec_buf;
20914 for (i = 0; i < command_loop_level; i++)
20915 *p++ = '[';
20916 *p = 0;
20917 return decode_mode_spec_buf;
20918 }
20919
20920 case ']':
20921 {
20922 int i;
20923 char *p;
20924
20925 if (command_loop_level > 5)
20926 return " ...]]]";
20927 p = decode_mode_spec_buf;
20928 for (i = 0; i < command_loop_level; i++)
20929 *p++ = ']';
20930 *p = 0;
20931 return decode_mode_spec_buf;
20932 }
20933
20934 case '-':
20935 {
20936 register int i;
20937
20938 /* Let lots_of_dashes be a string of infinite length. */
20939 if (mode_line_target == MODE_LINE_NOPROP ||
20940 mode_line_target == MODE_LINE_STRING)
20941 return "--";
20942 if (field_width <= 0
20943 || field_width > sizeof (lots_of_dashes))
20944 {
20945 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20946 decode_mode_spec_buf[i] = '-';
20947 decode_mode_spec_buf[i] = '\0';
20948 return decode_mode_spec_buf;
20949 }
20950 else
20951 return lots_of_dashes;
20952 }
20953
20954 case 'b':
20955 obj = BVAR (b, name);
20956 break;
20957
20958 case 'c':
20959 /* %c and %l are ignored in `frame-title-format'.
20960 (In redisplay_internal, the frame title is drawn _before_ the
20961 windows are updated, so the stuff which depends on actual
20962 window contents (such as %l) may fail to render properly, or
20963 even crash emacs.) */
20964 if (mode_line_target == MODE_LINE_TITLE)
20965 return "";
20966 else
20967 {
20968 EMACS_INT col = current_column ();
20969 w->column_number_displayed = make_number (col);
20970 pint2str (decode_mode_spec_buf, field_width, col);
20971 return decode_mode_spec_buf;
20972 }
20973
20974 case 'e':
20975 #ifndef SYSTEM_MALLOC
20976 {
20977 if (NILP (Vmemory_full))
20978 return "";
20979 else
20980 return "!MEM FULL! ";
20981 }
20982 #else
20983 return "";
20984 #endif
20985
20986 case 'F':
20987 /* %F displays the frame name. */
20988 if (!NILP (f->title))
20989 return SSDATA (f->title);
20990 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20991 return SSDATA (f->name);
20992 return "Emacs";
20993
20994 case 'f':
20995 obj = BVAR (b, filename);
20996 break;
20997
20998 case 'i':
20999 {
21000 EMACS_INT size = ZV - BEGV;
21001 pint2str (decode_mode_spec_buf, field_width, size);
21002 return decode_mode_spec_buf;
21003 }
21004
21005 case 'I':
21006 {
21007 EMACS_INT size = ZV - BEGV;
21008 pint2hrstr (decode_mode_spec_buf, field_width, size);
21009 return decode_mode_spec_buf;
21010 }
21011
21012 case 'l':
21013 {
21014 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
21015 EMACS_INT topline, nlines, height;
21016 EMACS_INT junk;
21017
21018 /* %c and %l are ignored in `frame-title-format'. */
21019 if (mode_line_target == MODE_LINE_TITLE)
21020 return "";
21021
21022 startpos = XMARKER (w->start)->charpos;
21023 startpos_byte = marker_byte_position (w->start);
21024 height = WINDOW_TOTAL_LINES (w);
21025
21026 /* If we decided that this buffer isn't suitable for line numbers,
21027 don't forget that too fast. */
21028 if (EQ (w->base_line_pos, w->buffer))
21029 goto no_value;
21030 /* But do forget it, if the window shows a different buffer now. */
21031 else if (BUFFERP (w->base_line_pos))
21032 w->base_line_pos = Qnil;
21033
21034 /* If the buffer is very big, don't waste time. */
21035 if (INTEGERP (Vline_number_display_limit)
21036 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21037 {
21038 w->base_line_pos = Qnil;
21039 w->base_line_number = Qnil;
21040 goto no_value;
21041 }
21042
21043 if (INTEGERP (w->base_line_number)
21044 && INTEGERP (w->base_line_pos)
21045 && XFASTINT (w->base_line_pos) <= startpos)
21046 {
21047 line = XFASTINT (w->base_line_number);
21048 linepos = XFASTINT (w->base_line_pos);
21049 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21050 }
21051 else
21052 {
21053 line = 1;
21054 linepos = BUF_BEGV (b);
21055 linepos_byte = BUF_BEGV_BYTE (b);
21056 }
21057
21058 /* Count lines from base line to window start position. */
21059 nlines = display_count_lines (linepos_byte,
21060 startpos_byte,
21061 startpos, &junk);
21062
21063 topline = nlines + line;
21064
21065 /* Determine a new base line, if the old one is too close
21066 or too far away, or if we did not have one.
21067 "Too close" means it's plausible a scroll-down would
21068 go back past it. */
21069 if (startpos == BUF_BEGV (b))
21070 {
21071 w->base_line_number = make_number (topline);
21072 w->base_line_pos = make_number (BUF_BEGV (b));
21073 }
21074 else if (nlines < height + 25 || nlines > height * 3 + 50
21075 || linepos == BUF_BEGV (b))
21076 {
21077 EMACS_INT limit = BUF_BEGV (b);
21078 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
21079 EMACS_INT position;
21080 EMACS_INT distance =
21081 (height * 2 + 30) * line_number_display_limit_width;
21082
21083 if (startpos - distance > limit)
21084 {
21085 limit = startpos - distance;
21086 limit_byte = CHAR_TO_BYTE (limit);
21087 }
21088
21089 nlines = display_count_lines (startpos_byte,
21090 limit_byte,
21091 - (height * 2 + 30),
21092 &position);
21093 /* If we couldn't find the lines we wanted within
21094 line_number_display_limit_width chars per line,
21095 give up on line numbers for this window. */
21096 if (position == limit_byte && limit == startpos - distance)
21097 {
21098 w->base_line_pos = w->buffer;
21099 w->base_line_number = Qnil;
21100 goto no_value;
21101 }
21102
21103 w->base_line_number = make_number (topline - nlines);
21104 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21105 }
21106
21107 /* Now count lines from the start pos to point. */
21108 nlines = display_count_lines (startpos_byte,
21109 PT_BYTE, PT, &junk);
21110
21111 /* Record that we did display the line number. */
21112 line_number_displayed = 1;
21113
21114 /* Make the string to show. */
21115 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21116 return decode_mode_spec_buf;
21117 no_value:
21118 {
21119 char* p = decode_mode_spec_buf;
21120 int pad = field_width - 2;
21121 while (pad-- > 0)
21122 *p++ = ' ';
21123 *p++ = '?';
21124 *p++ = '?';
21125 *p = '\0';
21126 return decode_mode_spec_buf;
21127 }
21128 }
21129 break;
21130
21131 case 'm':
21132 obj = BVAR (b, mode_name);
21133 break;
21134
21135 case 'n':
21136 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21137 return " Narrow";
21138 break;
21139
21140 case 'p':
21141 {
21142 EMACS_INT pos = marker_position (w->start);
21143 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21144
21145 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21146 {
21147 if (pos <= BUF_BEGV (b))
21148 return "All";
21149 else
21150 return "Bottom";
21151 }
21152 else if (pos <= BUF_BEGV (b))
21153 return "Top";
21154 else
21155 {
21156 if (total > 1000000)
21157 /* Do it differently for a large value, to avoid overflow. */
21158 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21159 else
21160 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21161 /* We can't normally display a 3-digit number,
21162 so get us a 2-digit number that is close. */
21163 if (total == 100)
21164 total = 99;
21165 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21166 return decode_mode_spec_buf;
21167 }
21168 }
21169
21170 /* Display percentage of size above the bottom of the screen. */
21171 case 'P':
21172 {
21173 EMACS_INT toppos = marker_position (w->start);
21174 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21175 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21176
21177 if (botpos >= BUF_ZV (b))
21178 {
21179 if (toppos <= BUF_BEGV (b))
21180 return "All";
21181 else
21182 return "Bottom";
21183 }
21184 else
21185 {
21186 if (total > 1000000)
21187 /* Do it differently for a large value, to avoid overflow. */
21188 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21189 else
21190 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21191 /* We can't normally display a 3-digit number,
21192 so get us a 2-digit number that is close. */
21193 if (total == 100)
21194 total = 99;
21195 if (toppos <= BUF_BEGV (b))
21196 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
21197 else
21198 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21199 return decode_mode_spec_buf;
21200 }
21201 }
21202
21203 case 's':
21204 /* status of process */
21205 obj = Fget_buffer_process (Fcurrent_buffer ());
21206 if (NILP (obj))
21207 return "no process";
21208 #ifndef MSDOS
21209 obj = Fsymbol_name (Fprocess_status (obj));
21210 #endif
21211 break;
21212
21213 case '@':
21214 {
21215 int count = inhibit_garbage_collection ();
21216 Lisp_Object val = call1 (intern ("file-remote-p"),
21217 BVAR (current_buffer, directory));
21218 unbind_to (count, Qnil);
21219
21220 if (NILP (val))
21221 return "-";
21222 else
21223 return "@";
21224 }
21225
21226 case 't': /* indicate TEXT or BINARY */
21227 return "T";
21228
21229 case 'z':
21230 /* coding-system (not including end-of-line format) */
21231 case 'Z':
21232 /* coding-system (including end-of-line type) */
21233 {
21234 int eol_flag = (c == 'Z');
21235 char *p = decode_mode_spec_buf;
21236
21237 if (! FRAME_WINDOW_P (f))
21238 {
21239 /* No need to mention EOL here--the terminal never needs
21240 to do EOL conversion. */
21241 p = decode_mode_spec_coding (CODING_ID_NAME
21242 (FRAME_KEYBOARD_CODING (f)->id),
21243 p, 0);
21244 p = decode_mode_spec_coding (CODING_ID_NAME
21245 (FRAME_TERMINAL_CODING (f)->id),
21246 p, 0);
21247 }
21248 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21249 p, eol_flag);
21250
21251 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21252 #ifdef subprocesses
21253 obj = Fget_buffer_process (Fcurrent_buffer ());
21254 if (PROCESSP (obj))
21255 {
21256 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21257 p, eol_flag);
21258 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21259 p, eol_flag);
21260 }
21261 #endif /* subprocesses */
21262 #endif /* 0 */
21263 *p = 0;
21264 return decode_mode_spec_buf;
21265 }
21266 }
21267
21268 if (STRINGP (obj))
21269 {
21270 *string = obj;
21271 return SSDATA (obj);
21272 }
21273 else
21274 return "";
21275 }
21276
21277
21278 /* Count up to COUNT lines starting from START_BYTE.
21279 But don't go beyond LIMIT_BYTE.
21280 Return the number of lines thus found (always nonnegative).
21281
21282 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21283
21284 static EMACS_INT
21285 display_count_lines (EMACS_INT start_byte,
21286 EMACS_INT limit_byte, EMACS_INT count,
21287 EMACS_INT *byte_pos_ptr)
21288 {
21289 register unsigned char *cursor;
21290 unsigned char *base;
21291
21292 register EMACS_INT ceiling;
21293 register unsigned char *ceiling_addr;
21294 EMACS_INT orig_count = count;
21295
21296 /* If we are not in selective display mode,
21297 check only for newlines. */
21298 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21299 && !INTEGERP (BVAR (current_buffer, selective_display)));
21300
21301 if (count > 0)
21302 {
21303 while (start_byte < limit_byte)
21304 {
21305 ceiling = BUFFER_CEILING_OF (start_byte);
21306 ceiling = min (limit_byte - 1, ceiling);
21307 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21308 base = (cursor = BYTE_POS_ADDR (start_byte));
21309 while (1)
21310 {
21311 if (selective_display)
21312 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21313 ;
21314 else
21315 while (*cursor != '\n' && ++cursor != ceiling_addr)
21316 ;
21317
21318 if (cursor != ceiling_addr)
21319 {
21320 if (--count == 0)
21321 {
21322 start_byte += cursor - base + 1;
21323 *byte_pos_ptr = start_byte;
21324 return orig_count;
21325 }
21326 else
21327 if (++cursor == ceiling_addr)
21328 break;
21329 }
21330 else
21331 break;
21332 }
21333 start_byte += cursor - base;
21334 }
21335 }
21336 else
21337 {
21338 while (start_byte > limit_byte)
21339 {
21340 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21341 ceiling = max (limit_byte, ceiling);
21342 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21343 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21344 while (1)
21345 {
21346 if (selective_display)
21347 while (--cursor != ceiling_addr
21348 && *cursor != '\n' && *cursor != 015)
21349 ;
21350 else
21351 while (--cursor != ceiling_addr && *cursor != '\n')
21352 ;
21353
21354 if (cursor != ceiling_addr)
21355 {
21356 if (++count == 0)
21357 {
21358 start_byte += cursor - base + 1;
21359 *byte_pos_ptr = start_byte;
21360 /* When scanning backwards, we should
21361 not count the newline posterior to which we stop. */
21362 return - orig_count - 1;
21363 }
21364 }
21365 else
21366 break;
21367 }
21368 /* Here we add 1 to compensate for the last decrement
21369 of CURSOR, which took it past the valid range. */
21370 start_byte += cursor - base + 1;
21371 }
21372 }
21373
21374 *byte_pos_ptr = limit_byte;
21375
21376 if (count < 0)
21377 return - orig_count + count;
21378 return orig_count - count;
21379
21380 }
21381
21382
21383 \f
21384 /***********************************************************************
21385 Displaying strings
21386 ***********************************************************************/
21387
21388 /* Display a NUL-terminated string, starting with index START.
21389
21390 If STRING is non-null, display that C string. Otherwise, the Lisp
21391 string LISP_STRING is displayed. There's a case that STRING is
21392 non-null and LISP_STRING is not nil. It means STRING is a string
21393 data of LISP_STRING. In that case, we display LISP_STRING while
21394 ignoring its text properties.
21395
21396 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21397 FACE_STRING. Display STRING or LISP_STRING with the face at
21398 FACE_STRING_POS in FACE_STRING:
21399
21400 Display the string in the environment given by IT, but use the
21401 standard display table, temporarily.
21402
21403 FIELD_WIDTH is the minimum number of output glyphs to produce.
21404 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21405 with spaces. If STRING has more characters, more than FIELD_WIDTH
21406 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21407
21408 PRECISION is the maximum number of characters to output from
21409 STRING. PRECISION < 0 means don't truncate the string.
21410
21411 This is roughly equivalent to printf format specifiers:
21412
21413 FIELD_WIDTH PRECISION PRINTF
21414 ----------------------------------------
21415 -1 -1 %s
21416 -1 10 %.10s
21417 10 -1 %10s
21418 20 10 %20.10s
21419
21420 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21421 display them, and < 0 means obey the current buffer's value of
21422 enable_multibyte_characters.
21423
21424 Value is the number of columns displayed. */
21425
21426 static int
21427 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21428 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21429 int field_width, int precision, int max_x, int multibyte)
21430 {
21431 int hpos_at_start = it->hpos;
21432 int saved_face_id = it->face_id;
21433 struct glyph_row *row = it->glyph_row;
21434 EMACS_INT it_charpos;
21435
21436 /* Initialize the iterator IT for iteration over STRING beginning
21437 with index START. */
21438 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21439 precision, field_width, multibyte);
21440 if (string && STRINGP (lisp_string))
21441 /* LISP_STRING is the one returned by decode_mode_spec. We should
21442 ignore its text properties. */
21443 it->stop_charpos = it->end_charpos;
21444
21445 /* If displaying STRING, set up the face of the iterator from
21446 FACE_STRING, if that's given. */
21447 if (STRINGP (face_string))
21448 {
21449 EMACS_INT endptr;
21450 struct face *face;
21451
21452 it->face_id
21453 = face_at_string_position (it->w, face_string, face_string_pos,
21454 0, it->region_beg_charpos,
21455 it->region_end_charpos,
21456 &endptr, it->base_face_id, 0);
21457 face = FACE_FROM_ID (it->f, it->face_id);
21458 it->face_box_p = face->box != FACE_NO_BOX;
21459 }
21460
21461 /* Set max_x to the maximum allowed X position. Don't let it go
21462 beyond the right edge of the window. */
21463 if (max_x <= 0)
21464 max_x = it->last_visible_x;
21465 else
21466 max_x = min (max_x, it->last_visible_x);
21467
21468 /* Skip over display elements that are not visible. because IT->w is
21469 hscrolled. */
21470 if (it->current_x < it->first_visible_x)
21471 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21472 MOVE_TO_POS | MOVE_TO_X);
21473
21474 row->ascent = it->max_ascent;
21475 row->height = it->max_ascent + it->max_descent;
21476 row->phys_ascent = it->max_phys_ascent;
21477 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21478 row->extra_line_spacing = it->max_extra_line_spacing;
21479
21480 if (STRINGP (it->string))
21481 it_charpos = IT_STRING_CHARPOS (*it);
21482 else
21483 it_charpos = IT_CHARPOS (*it);
21484
21485 /* This condition is for the case that we are called with current_x
21486 past last_visible_x. */
21487 while (it->current_x < max_x)
21488 {
21489 int x_before, x, n_glyphs_before, i, nglyphs;
21490
21491 /* Get the next display element. */
21492 if (!get_next_display_element (it))
21493 break;
21494
21495 /* Produce glyphs. */
21496 x_before = it->current_x;
21497 n_glyphs_before = row->used[TEXT_AREA];
21498 PRODUCE_GLYPHS (it);
21499
21500 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21501 i = 0;
21502 x = x_before;
21503 while (i < nglyphs)
21504 {
21505 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21506
21507 if (it->line_wrap != TRUNCATE
21508 && x + glyph->pixel_width > max_x)
21509 {
21510 /* End of continued line or max_x reached. */
21511 if (CHAR_GLYPH_PADDING_P (*glyph))
21512 {
21513 /* A wide character is unbreakable. */
21514 if (row->reversed_p)
21515 unproduce_glyphs (it, row->used[TEXT_AREA]
21516 - n_glyphs_before);
21517 row->used[TEXT_AREA] = n_glyphs_before;
21518 it->current_x = x_before;
21519 }
21520 else
21521 {
21522 if (row->reversed_p)
21523 unproduce_glyphs (it, row->used[TEXT_AREA]
21524 - (n_glyphs_before + i));
21525 row->used[TEXT_AREA] = n_glyphs_before + i;
21526 it->current_x = x;
21527 }
21528 break;
21529 }
21530 else if (x + glyph->pixel_width >= it->first_visible_x)
21531 {
21532 /* Glyph is at least partially visible. */
21533 ++it->hpos;
21534 if (x < it->first_visible_x)
21535 row->x = x - it->first_visible_x;
21536 }
21537 else
21538 {
21539 /* Glyph is off the left margin of the display area.
21540 Should not happen. */
21541 abort ();
21542 }
21543
21544 row->ascent = max (row->ascent, it->max_ascent);
21545 row->height = max (row->height, it->max_ascent + it->max_descent);
21546 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21547 row->phys_height = max (row->phys_height,
21548 it->max_phys_ascent + it->max_phys_descent);
21549 row->extra_line_spacing = max (row->extra_line_spacing,
21550 it->max_extra_line_spacing);
21551 x += glyph->pixel_width;
21552 ++i;
21553 }
21554
21555 /* Stop if max_x reached. */
21556 if (i < nglyphs)
21557 break;
21558
21559 /* Stop at line ends. */
21560 if (ITERATOR_AT_END_OF_LINE_P (it))
21561 {
21562 it->continuation_lines_width = 0;
21563 break;
21564 }
21565
21566 set_iterator_to_next (it, 1);
21567 if (STRINGP (it->string))
21568 it_charpos = IT_STRING_CHARPOS (*it);
21569 else
21570 it_charpos = IT_CHARPOS (*it);
21571
21572 /* Stop if truncating at the right edge. */
21573 if (it->line_wrap == TRUNCATE
21574 && it->current_x >= it->last_visible_x)
21575 {
21576 /* Add truncation mark, but don't do it if the line is
21577 truncated at a padding space. */
21578 if (it_charpos < it->string_nchars)
21579 {
21580 if (!FRAME_WINDOW_P (it->f))
21581 {
21582 int ii, n;
21583
21584 if (it->current_x > it->last_visible_x)
21585 {
21586 if (!row->reversed_p)
21587 {
21588 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21589 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21590 break;
21591 }
21592 else
21593 {
21594 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21595 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21596 break;
21597 unproduce_glyphs (it, ii + 1);
21598 ii = row->used[TEXT_AREA] - (ii + 1);
21599 }
21600 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21601 {
21602 row->used[TEXT_AREA] = ii;
21603 produce_special_glyphs (it, IT_TRUNCATION);
21604 }
21605 }
21606 produce_special_glyphs (it, IT_TRUNCATION);
21607 }
21608 row->truncated_on_right_p = 1;
21609 }
21610 break;
21611 }
21612 }
21613
21614 /* Maybe insert a truncation at the left. */
21615 if (it->first_visible_x
21616 && it_charpos > 0)
21617 {
21618 if (!FRAME_WINDOW_P (it->f))
21619 insert_left_trunc_glyphs (it);
21620 row->truncated_on_left_p = 1;
21621 }
21622
21623 it->face_id = saved_face_id;
21624
21625 /* Value is number of columns displayed. */
21626 return it->hpos - hpos_at_start;
21627 }
21628
21629
21630 \f
21631 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21632 appears as an element of LIST or as the car of an element of LIST.
21633 If PROPVAL is a list, compare each element against LIST in that
21634 way, and return 1/2 if any element of PROPVAL is found in LIST.
21635 Otherwise return 0. This function cannot quit.
21636 The return value is 2 if the text is invisible but with an ellipsis
21637 and 1 if it's invisible and without an ellipsis. */
21638
21639 int
21640 invisible_p (register Lisp_Object propval, Lisp_Object list)
21641 {
21642 register Lisp_Object tail, proptail;
21643
21644 for (tail = list; CONSP (tail); tail = XCDR (tail))
21645 {
21646 register Lisp_Object tem;
21647 tem = XCAR (tail);
21648 if (EQ (propval, tem))
21649 return 1;
21650 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21651 return NILP (XCDR (tem)) ? 1 : 2;
21652 }
21653
21654 if (CONSP (propval))
21655 {
21656 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21657 {
21658 Lisp_Object propelt;
21659 propelt = XCAR (proptail);
21660 for (tail = list; CONSP (tail); tail = XCDR (tail))
21661 {
21662 register Lisp_Object tem;
21663 tem = XCAR (tail);
21664 if (EQ (propelt, tem))
21665 return 1;
21666 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21667 return NILP (XCDR (tem)) ? 1 : 2;
21668 }
21669 }
21670 }
21671
21672 return 0;
21673 }
21674
21675 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21676 doc: /* Non-nil if the property makes the text invisible.
21677 POS-OR-PROP can be a marker or number, in which case it is taken to be
21678 a position in the current buffer and the value of the `invisible' property
21679 is checked; or it can be some other value, which is then presumed to be the
21680 value of the `invisible' property of the text of interest.
21681 The non-nil value returned can be t for truly invisible text or something
21682 else if the text is replaced by an ellipsis. */)
21683 (Lisp_Object pos_or_prop)
21684 {
21685 Lisp_Object prop
21686 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21687 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21688 : pos_or_prop);
21689 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21690 return (invis == 0 ? Qnil
21691 : invis == 1 ? Qt
21692 : make_number (invis));
21693 }
21694
21695 /* Calculate a width or height in pixels from a specification using
21696 the following elements:
21697
21698 SPEC ::=
21699 NUM - a (fractional) multiple of the default font width/height
21700 (NUM) - specifies exactly NUM pixels
21701 UNIT - a fixed number of pixels, see below.
21702 ELEMENT - size of a display element in pixels, see below.
21703 (NUM . SPEC) - equals NUM * SPEC
21704 (+ SPEC SPEC ...) - add pixel values
21705 (- SPEC SPEC ...) - subtract pixel values
21706 (- SPEC) - negate pixel value
21707
21708 NUM ::=
21709 INT or FLOAT - a number constant
21710 SYMBOL - use symbol's (buffer local) variable binding.
21711
21712 UNIT ::=
21713 in - pixels per inch *)
21714 mm - pixels per 1/1000 meter *)
21715 cm - pixels per 1/100 meter *)
21716 width - width of current font in pixels.
21717 height - height of current font in pixels.
21718
21719 *) using the ratio(s) defined in display-pixels-per-inch.
21720
21721 ELEMENT ::=
21722
21723 left-fringe - left fringe width in pixels
21724 right-fringe - right fringe width in pixels
21725
21726 left-margin - left margin width in pixels
21727 right-margin - right margin width in pixels
21728
21729 scroll-bar - scroll-bar area width in pixels
21730
21731 Examples:
21732
21733 Pixels corresponding to 5 inches:
21734 (5 . in)
21735
21736 Total width of non-text areas on left side of window (if scroll-bar is on left):
21737 '(space :width (+ left-fringe left-margin scroll-bar))
21738
21739 Align to first text column (in header line):
21740 '(space :align-to 0)
21741
21742 Align to middle of text area minus half the width of variable `my-image'
21743 containing a loaded image:
21744 '(space :align-to (0.5 . (- text my-image)))
21745
21746 Width of left margin minus width of 1 character in the default font:
21747 '(space :width (- left-margin 1))
21748
21749 Width of left margin minus width of 2 characters in the current font:
21750 '(space :width (- left-margin (2 . width)))
21751
21752 Center 1 character over left-margin (in header line):
21753 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21754
21755 Different ways to express width of left fringe plus left margin minus one pixel:
21756 '(space :width (- (+ left-fringe left-margin) (1)))
21757 '(space :width (+ left-fringe left-margin (- (1))))
21758 '(space :width (+ left-fringe left-margin (-1)))
21759
21760 */
21761
21762 #define NUMVAL(X) \
21763 ((INTEGERP (X) || FLOATP (X)) \
21764 ? XFLOATINT (X) \
21765 : - 1)
21766
21767 static int
21768 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21769 struct font *font, int width_p, int *align_to)
21770 {
21771 double pixels;
21772
21773 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21774 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21775
21776 if (NILP (prop))
21777 return OK_PIXELS (0);
21778
21779 xassert (FRAME_LIVE_P (it->f));
21780
21781 if (SYMBOLP (prop))
21782 {
21783 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21784 {
21785 char *unit = SSDATA (SYMBOL_NAME (prop));
21786
21787 if (unit[0] == 'i' && unit[1] == 'n')
21788 pixels = 1.0;
21789 else if (unit[0] == 'm' && unit[1] == 'm')
21790 pixels = 25.4;
21791 else if (unit[0] == 'c' && unit[1] == 'm')
21792 pixels = 2.54;
21793 else
21794 pixels = 0;
21795 if (pixels > 0)
21796 {
21797 double ppi;
21798 #ifdef HAVE_WINDOW_SYSTEM
21799 if (FRAME_WINDOW_P (it->f)
21800 && (ppi = (width_p
21801 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21802 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21803 ppi > 0))
21804 return OK_PIXELS (ppi / pixels);
21805 #endif
21806
21807 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21808 || (CONSP (Vdisplay_pixels_per_inch)
21809 && (ppi = (width_p
21810 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21811 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21812 ppi > 0)))
21813 return OK_PIXELS (ppi / pixels);
21814
21815 return 0;
21816 }
21817 }
21818
21819 #ifdef HAVE_WINDOW_SYSTEM
21820 if (EQ (prop, Qheight))
21821 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21822 if (EQ (prop, Qwidth))
21823 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21824 #else
21825 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21826 return OK_PIXELS (1);
21827 #endif
21828
21829 if (EQ (prop, Qtext))
21830 return OK_PIXELS (width_p
21831 ? window_box_width (it->w, TEXT_AREA)
21832 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21833
21834 if (align_to && *align_to < 0)
21835 {
21836 *res = 0;
21837 if (EQ (prop, Qleft))
21838 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21839 if (EQ (prop, Qright))
21840 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21841 if (EQ (prop, Qcenter))
21842 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21843 + window_box_width (it->w, TEXT_AREA) / 2);
21844 if (EQ (prop, Qleft_fringe))
21845 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21846 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21847 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21848 if (EQ (prop, Qright_fringe))
21849 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21850 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21851 : window_box_right_offset (it->w, TEXT_AREA));
21852 if (EQ (prop, Qleft_margin))
21853 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21854 if (EQ (prop, Qright_margin))
21855 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21856 if (EQ (prop, Qscroll_bar))
21857 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21858 ? 0
21859 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21860 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21861 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21862 : 0)));
21863 }
21864 else
21865 {
21866 if (EQ (prop, Qleft_fringe))
21867 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21868 if (EQ (prop, Qright_fringe))
21869 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21870 if (EQ (prop, Qleft_margin))
21871 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21872 if (EQ (prop, Qright_margin))
21873 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21874 if (EQ (prop, Qscroll_bar))
21875 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21876 }
21877
21878 prop = Fbuffer_local_value (prop, it->w->buffer);
21879 }
21880
21881 if (INTEGERP (prop) || FLOATP (prop))
21882 {
21883 int base_unit = (width_p
21884 ? FRAME_COLUMN_WIDTH (it->f)
21885 : FRAME_LINE_HEIGHT (it->f));
21886 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21887 }
21888
21889 if (CONSP (prop))
21890 {
21891 Lisp_Object car = XCAR (prop);
21892 Lisp_Object cdr = XCDR (prop);
21893
21894 if (SYMBOLP (car))
21895 {
21896 #ifdef HAVE_WINDOW_SYSTEM
21897 if (FRAME_WINDOW_P (it->f)
21898 && valid_image_p (prop))
21899 {
21900 ptrdiff_t id = lookup_image (it->f, prop);
21901 struct image *img = IMAGE_FROM_ID (it->f, id);
21902
21903 return OK_PIXELS (width_p ? img->width : img->height);
21904 }
21905 #endif
21906 if (EQ (car, Qplus) || EQ (car, Qminus))
21907 {
21908 int first = 1;
21909 double px;
21910
21911 pixels = 0;
21912 while (CONSP (cdr))
21913 {
21914 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21915 font, width_p, align_to))
21916 return 0;
21917 if (first)
21918 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21919 else
21920 pixels += px;
21921 cdr = XCDR (cdr);
21922 }
21923 if (EQ (car, Qminus))
21924 pixels = -pixels;
21925 return OK_PIXELS (pixels);
21926 }
21927
21928 car = Fbuffer_local_value (car, it->w->buffer);
21929 }
21930
21931 if (INTEGERP (car) || FLOATP (car))
21932 {
21933 double fact;
21934 pixels = XFLOATINT (car);
21935 if (NILP (cdr))
21936 return OK_PIXELS (pixels);
21937 if (calc_pixel_width_or_height (&fact, it, cdr,
21938 font, width_p, align_to))
21939 return OK_PIXELS (pixels * fact);
21940 return 0;
21941 }
21942
21943 return 0;
21944 }
21945
21946 return 0;
21947 }
21948
21949 \f
21950 /***********************************************************************
21951 Glyph Display
21952 ***********************************************************************/
21953
21954 #ifdef HAVE_WINDOW_SYSTEM
21955
21956 #if GLYPH_DEBUG
21957
21958 void
21959 dump_glyph_string (struct glyph_string *s)
21960 {
21961 fprintf (stderr, "glyph string\n");
21962 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21963 s->x, s->y, s->width, s->height);
21964 fprintf (stderr, " ybase = %d\n", s->ybase);
21965 fprintf (stderr, " hl = %d\n", s->hl);
21966 fprintf (stderr, " left overhang = %d, right = %d\n",
21967 s->left_overhang, s->right_overhang);
21968 fprintf (stderr, " nchars = %d\n", s->nchars);
21969 fprintf (stderr, " extends to end of line = %d\n",
21970 s->extends_to_end_of_line_p);
21971 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21972 fprintf (stderr, " bg width = %d\n", s->background_width);
21973 }
21974
21975 #endif /* GLYPH_DEBUG */
21976
21977 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21978 of XChar2b structures for S; it can't be allocated in
21979 init_glyph_string because it must be allocated via `alloca'. W
21980 is the window on which S is drawn. ROW and AREA are the glyph row
21981 and area within the row from which S is constructed. START is the
21982 index of the first glyph structure covered by S. HL is a
21983 face-override for drawing S. */
21984
21985 #ifdef HAVE_NTGUI
21986 #define OPTIONAL_HDC(hdc) HDC hdc,
21987 #define DECLARE_HDC(hdc) HDC hdc;
21988 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21989 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21990 #endif
21991
21992 #ifndef OPTIONAL_HDC
21993 #define OPTIONAL_HDC(hdc)
21994 #define DECLARE_HDC(hdc)
21995 #define ALLOCATE_HDC(hdc, f)
21996 #define RELEASE_HDC(hdc, f)
21997 #endif
21998
21999 static void
22000 init_glyph_string (struct glyph_string *s,
22001 OPTIONAL_HDC (hdc)
22002 XChar2b *char2b, struct window *w, struct glyph_row *row,
22003 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22004 {
22005 memset (s, 0, sizeof *s);
22006 s->w = w;
22007 s->f = XFRAME (w->frame);
22008 #ifdef HAVE_NTGUI
22009 s->hdc = hdc;
22010 #endif
22011 s->display = FRAME_X_DISPLAY (s->f);
22012 s->window = FRAME_X_WINDOW (s->f);
22013 s->char2b = char2b;
22014 s->hl = hl;
22015 s->row = row;
22016 s->area = area;
22017 s->first_glyph = row->glyphs[area] + start;
22018 s->height = row->height;
22019 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22020 s->ybase = s->y + row->ascent;
22021 }
22022
22023
22024 /* Append the list of glyph strings with head H and tail T to the list
22025 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22026
22027 static inline void
22028 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22029 struct glyph_string *h, struct glyph_string *t)
22030 {
22031 if (h)
22032 {
22033 if (*head)
22034 (*tail)->next = h;
22035 else
22036 *head = h;
22037 h->prev = *tail;
22038 *tail = t;
22039 }
22040 }
22041
22042
22043 /* Prepend the list of glyph strings with head H and tail T to the
22044 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22045 result. */
22046
22047 static inline void
22048 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22049 struct glyph_string *h, struct glyph_string *t)
22050 {
22051 if (h)
22052 {
22053 if (*head)
22054 (*head)->prev = t;
22055 else
22056 *tail = t;
22057 t->next = *head;
22058 *head = h;
22059 }
22060 }
22061
22062
22063 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22064 Set *HEAD and *TAIL to the resulting list. */
22065
22066 static inline void
22067 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22068 struct glyph_string *s)
22069 {
22070 s->next = s->prev = NULL;
22071 append_glyph_string_lists (head, tail, s, s);
22072 }
22073
22074
22075 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22076 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22077 make sure that X resources for the face returned are allocated.
22078 Value is a pointer to a realized face that is ready for display if
22079 DISPLAY_P is non-zero. */
22080
22081 static inline struct face *
22082 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22083 XChar2b *char2b, int display_p)
22084 {
22085 struct face *face = FACE_FROM_ID (f, face_id);
22086
22087 if (face->font)
22088 {
22089 unsigned code = face->font->driver->encode_char (face->font, c);
22090
22091 if (code != FONT_INVALID_CODE)
22092 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22093 else
22094 STORE_XCHAR2B (char2b, 0, 0);
22095 }
22096
22097 /* Make sure X resources of the face are allocated. */
22098 #ifdef HAVE_X_WINDOWS
22099 if (display_p)
22100 #endif
22101 {
22102 xassert (face != NULL);
22103 PREPARE_FACE_FOR_DISPLAY (f, face);
22104 }
22105
22106 return face;
22107 }
22108
22109
22110 /* Get face and two-byte form of character glyph GLYPH on frame F.
22111 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22112 a pointer to a realized face that is ready for display. */
22113
22114 static inline struct face *
22115 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22116 XChar2b *char2b, int *two_byte_p)
22117 {
22118 struct face *face;
22119
22120 xassert (glyph->type == CHAR_GLYPH);
22121 face = FACE_FROM_ID (f, glyph->face_id);
22122
22123 if (two_byte_p)
22124 *two_byte_p = 0;
22125
22126 if (face->font)
22127 {
22128 unsigned code;
22129
22130 if (CHAR_BYTE8_P (glyph->u.ch))
22131 code = CHAR_TO_BYTE8 (glyph->u.ch);
22132 else
22133 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22134
22135 if (code != FONT_INVALID_CODE)
22136 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22137 else
22138 STORE_XCHAR2B (char2b, 0, 0);
22139 }
22140
22141 /* Make sure X resources of the face are allocated. */
22142 xassert (face != NULL);
22143 PREPARE_FACE_FOR_DISPLAY (f, face);
22144 return face;
22145 }
22146
22147
22148 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22149 Return 1 if FONT has a glyph for C, otherwise return 0. */
22150
22151 static inline int
22152 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22153 {
22154 unsigned code;
22155
22156 if (CHAR_BYTE8_P (c))
22157 code = CHAR_TO_BYTE8 (c);
22158 else
22159 code = font->driver->encode_char (font, c);
22160
22161 if (code == FONT_INVALID_CODE)
22162 return 0;
22163 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22164 return 1;
22165 }
22166
22167
22168 /* Fill glyph string S with composition components specified by S->cmp.
22169
22170 BASE_FACE is the base face of the composition.
22171 S->cmp_from is the index of the first component for S.
22172
22173 OVERLAPS non-zero means S should draw the foreground only, and use
22174 its physical height for clipping. See also draw_glyphs.
22175
22176 Value is the index of a component not in S. */
22177
22178 static int
22179 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22180 int overlaps)
22181 {
22182 int i;
22183 /* For all glyphs of this composition, starting at the offset
22184 S->cmp_from, until we reach the end of the definition or encounter a
22185 glyph that requires the different face, add it to S. */
22186 struct face *face;
22187
22188 xassert (s);
22189
22190 s->for_overlaps = overlaps;
22191 s->face = NULL;
22192 s->font = NULL;
22193 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22194 {
22195 int c = COMPOSITION_GLYPH (s->cmp, i);
22196
22197 /* TAB in a composition means display glyphs with padding space
22198 on the left or right. */
22199 if (c != '\t')
22200 {
22201 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22202 -1, Qnil);
22203
22204 face = get_char_face_and_encoding (s->f, c, face_id,
22205 s->char2b + i, 1);
22206 if (face)
22207 {
22208 if (! s->face)
22209 {
22210 s->face = face;
22211 s->font = s->face->font;
22212 }
22213 else if (s->face != face)
22214 break;
22215 }
22216 }
22217 ++s->nchars;
22218 }
22219 s->cmp_to = i;
22220
22221 if (s->face == NULL)
22222 {
22223 s->face = base_face->ascii_face;
22224 s->font = s->face->font;
22225 }
22226
22227 /* All glyph strings for the same composition has the same width,
22228 i.e. the width set for the first component of the composition. */
22229 s->width = s->first_glyph->pixel_width;
22230
22231 /* If the specified font could not be loaded, use the frame's
22232 default font, but record the fact that we couldn't load it in
22233 the glyph string so that we can draw rectangles for the
22234 characters of the glyph string. */
22235 if (s->font == NULL)
22236 {
22237 s->font_not_found_p = 1;
22238 s->font = FRAME_FONT (s->f);
22239 }
22240
22241 /* Adjust base line for subscript/superscript text. */
22242 s->ybase += s->first_glyph->voffset;
22243
22244 /* This glyph string must always be drawn with 16-bit functions. */
22245 s->two_byte_p = 1;
22246
22247 return s->cmp_to;
22248 }
22249
22250 static int
22251 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22252 int start, int end, int overlaps)
22253 {
22254 struct glyph *glyph, *last;
22255 Lisp_Object lgstring;
22256 int i;
22257
22258 s->for_overlaps = overlaps;
22259 glyph = s->row->glyphs[s->area] + start;
22260 last = s->row->glyphs[s->area] + end;
22261 s->cmp_id = glyph->u.cmp.id;
22262 s->cmp_from = glyph->slice.cmp.from;
22263 s->cmp_to = glyph->slice.cmp.to + 1;
22264 s->face = FACE_FROM_ID (s->f, face_id);
22265 lgstring = composition_gstring_from_id (s->cmp_id);
22266 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22267 glyph++;
22268 while (glyph < last
22269 && glyph->u.cmp.automatic
22270 && glyph->u.cmp.id == s->cmp_id
22271 && s->cmp_to == glyph->slice.cmp.from)
22272 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22273
22274 for (i = s->cmp_from; i < s->cmp_to; i++)
22275 {
22276 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22277 unsigned code = LGLYPH_CODE (lglyph);
22278
22279 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22280 }
22281 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22282 return glyph - s->row->glyphs[s->area];
22283 }
22284
22285
22286 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22287 See the comment of fill_glyph_string for arguments.
22288 Value is the index of the first glyph not in S. */
22289
22290
22291 static int
22292 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22293 int start, int end, int overlaps)
22294 {
22295 struct glyph *glyph, *last;
22296 int voffset;
22297
22298 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22299 s->for_overlaps = overlaps;
22300 glyph = s->row->glyphs[s->area] + start;
22301 last = s->row->glyphs[s->area] + end;
22302 voffset = glyph->voffset;
22303 s->face = FACE_FROM_ID (s->f, face_id);
22304 s->font = s->face->font;
22305 s->nchars = 1;
22306 s->width = glyph->pixel_width;
22307 glyph++;
22308 while (glyph < last
22309 && glyph->type == GLYPHLESS_GLYPH
22310 && glyph->voffset == voffset
22311 && glyph->face_id == face_id)
22312 {
22313 s->nchars++;
22314 s->width += glyph->pixel_width;
22315 glyph++;
22316 }
22317 s->ybase += voffset;
22318 return glyph - s->row->glyphs[s->area];
22319 }
22320
22321
22322 /* Fill glyph string S from a sequence of character glyphs.
22323
22324 FACE_ID is the face id of the string. START is the index of the
22325 first glyph to consider, END is the index of the last + 1.
22326 OVERLAPS non-zero means S should draw the foreground only, and use
22327 its physical height for clipping. See also draw_glyphs.
22328
22329 Value is the index of the first glyph not in S. */
22330
22331 static int
22332 fill_glyph_string (struct glyph_string *s, int face_id,
22333 int start, int end, int overlaps)
22334 {
22335 struct glyph *glyph, *last;
22336 int voffset;
22337 int glyph_not_available_p;
22338
22339 xassert (s->f == XFRAME (s->w->frame));
22340 xassert (s->nchars == 0);
22341 xassert (start >= 0 && end > start);
22342
22343 s->for_overlaps = overlaps;
22344 glyph = s->row->glyphs[s->area] + start;
22345 last = s->row->glyphs[s->area] + end;
22346 voffset = glyph->voffset;
22347 s->padding_p = glyph->padding_p;
22348 glyph_not_available_p = glyph->glyph_not_available_p;
22349
22350 while (glyph < last
22351 && glyph->type == CHAR_GLYPH
22352 && glyph->voffset == voffset
22353 /* Same face id implies same font, nowadays. */
22354 && glyph->face_id == face_id
22355 && glyph->glyph_not_available_p == glyph_not_available_p)
22356 {
22357 int two_byte_p;
22358
22359 s->face = get_glyph_face_and_encoding (s->f, glyph,
22360 s->char2b + s->nchars,
22361 &two_byte_p);
22362 s->two_byte_p = two_byte_p;
22363 ++s->nchars;
22364 xassert (s->nchars <= end - start);
22365 s->width += glyph->pixel_width;
22366 if (glyph++->padding_p != s->padding_p)
22367 break;
22368 }
22369
22370 s->font = s->face->font;
22371
22372 /* If the specified font could not be loaded, use the frame's font,
22373 but record the fact that we couldn't load it in
22374 S->font_not_found_p so that we can draw rectangles for the
22375 characters of the glyph string. */
22376 if (s->font == NULL || glyph_not_available_p)
22377 {
22378 s->font_not_found_p = 1;
22379 s->font = FRAME_FONT (s->f);
22380 }
22381
22382 /* Adjust base line for subscript/superscript text. */
22383 s->ybase += voffset;
22384
22385 xassert (s->face && s->face->gc);
22386 return glyph - s->row->glyphs[s->area];
22387 }
22388
22389
22390 /* Fill glyph string S from image glyph S->first_glyph. */
22391
22392 static void
22393 fill_image_glyph_string (struct glyph_string *s)
22394 {
22395 xassert (s->first_glyph->type == IMAGE_GLYPH);
22396 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22397 xassert (s->img);
22398 s->slice = s->first_glyph->slice.img;
22399 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22400 s->font = s->face->font;
22401 s->width = s->first_glyph->pixel_width;
22402
22403 /* Adjust base line for subscript/superscript text. */
22404 s->ybase += s->first_glyph->voffset;
22405 }
22406
22407
22408 /* Fill glyph string S from a sequence of stretch glyphs.
22409
22410 START is the index of the first glyph to consider,
22411 END is the index of the last + 1.
22412
22413 Value is the index of the first glyph not in S. */
22414
22415 static int
22416 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22417 {
22418 struct glyph *glyph, *last;
22419 int voffset, face_id;
22420
22421 xassert (s->first_glyph->type == STRETCH_GLYPH);
22422
22423 glyph = s->row->glyphs[s->area] + start;
22424 last = s->row->glyphs[s->area] + end;
22425 face_id = glyph->face_id;
22426 s->face = FACE_FROM_ID (s->f, face_id);
22427 s->font = s->face->font;
22428 s->width = glyph->pixel_width;
22429 s->nchars = 1;
22430 voffset = glyph->voffset;
22431
22432 for (++glyph;
22433 (glyph < last
22434 && glyph->type == STRETCH_GLYPH
22435 && glyph->voffset == voffset
22436 && glyph->face_id == face_id);
22437 ++glyph)
22438 s->width += glyph->pixel_width;
22439
22440 /* Adjust base line for subscript/superscript text. */
22441 s->ybase += voffset;
22442
22443 /* The case that face->gc == 0 is handled when drawing the glyph
22444 string by calling PREPARE_FACE_FOR_DISPLAY. */
22445 xassert (s->face);
22446 return glyph - s->row->glyphs[s->area];
22447 }
22448
22449 static struct font_metrics *
22450 get_per_char_metric (struct font *font, XChar2b *char2b)
22451 {
22452 static struct font_metrics metrics;
22453 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22454
22455 if (! font || code == FONT_INVALID_CODE)
22456 return NULL;
22457 font->driver->text_extents (font, &code, 1, &metrics);
22458 return &metrics;
22459 }
22460
22461 /* EXPORT for RIF:
22462 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22463 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22464 assumed to be zero. */
22465
22466 void
22467 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22468 {
22469 *left = *right = 0;
22470
22471 if (glyph->type == CHAR_GLYPH)
22472 {
22473 struct face *face;
22474 XChar2b char2b;
22475 struct font_metrics *pcm;
22476
22477 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22478 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22479 {
22480 if (pcm->rbearing > pcm->width)
22481 *right = pcm->rbearing - pcm->width;
22482 if (pcm->lbearing < 0)
22483 *left = -pcm->lbearing;
22484 }
22485 }
22486 else if (glyph->type == COMPOSITE_GLYPH)
22487 {
22488 if (! glyph->u.cmp.automatic)
22489 {
22490 struct composition *cmp = composition_table[glyph->u.cmp.id];
22491
22492 if (cmp->rbearing > cmp->pixel_width)
22493 *right = cmp->rbearing - cmp->pixel_width;
22494 if (cmp->lbearing < 0)
22495 *left = - cmp->lbearing;
22496 }
22497 else
22498 {
22499 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22500 struct font_metrics metrics;
22501
22502 composition_gstring_width (gstring, glyph->slice.cmp.from,
22503 glyph->slice.cmp.to + 1, &metrics);
22504 if (metrics.rbearing > metrics.width)
22505 *right = metrics.rbearing - metrics.width;
22506 if (metrics.lbearing < 0)
22507 *left = - metrics.lbearing;
22508 }
22509 }
22510 }
22511
22512
22513 /* Return the index of the first glyph preceding glyph string S that
22514 is overwritten by S because of S's left overhang. Value is -1
22515 if no glyphs are overwritten. */
22516
22517 static int
22518 left_overwritten (struct glyph_string *s)
22519 {
22520 int k;
22521
22522 if (s->left_overhang)
22523 {
22524 int x = 0, i;
22525 struct glyph *glyphs = s->row->glyphs[s->area];
22526 int first = s->first_glyph - glyphs;
22527
22528 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22529 x -= glyphs[i].pixel_width;
22530
22531 k = i + 1;
22532 }
22533 else
22534 k = -1;
22535
22536 return k;
22537 }
22538
22539
22540 /* Return the index of the first glyph preceding glyph string S that
22541 is overwriting S because of its right overhang. Value is -1 if no
22542 glyph in front of S overwrites S. */
22543
22544 static int
22545 left_overwriting (struct glyph_string *s)
22546 {
22547 int i, k, x;
22548 struct glyph *glyphs = s->row->glyphs[s->area];
22549 int first = s->first_glyph - glyphs;
22550
22551 k = -1;
22552 x = 0;
22553 for (i = first - 1; i >= 0; --i)
22554 {
22555 int left, right;
22556 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22557 if (x + right > 0)
22558 k = i;
22559 x -= glyphs[i].pixel_width;
22560 }
22561
22562 return k;
22563 }
22564
22565
22566 /* Return the index of the last glyph following glyph string S that is
22567 overwritten by S because of S's right overhang. Value is -1 if
22568 no such glyph is found. */
22569
22570 static int
22571 right_overwritten (struct glyph_string *s)
22572 {
22573 int k = -1;
22574
22575 if (s->right_overhang)
22576 {
22577 int x = 0, i;
22578 struct glyph *glyphs = s->row->glyphs[s->area];
22579 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22580 int end = s->row->used[s->area];
22581
22582 for (i = first; i < end && s->right_overhang > x; ++i)
22583 x += glyphs[i].pixel_width;
22584
22585 k = i;
22586 }
22587
22588 return k;
22589 }
22590
22591
22592 /* Return the index of the last glyph following glyph string S that
22593 overwrites S because of its left overhang. Value is negative
22594 if no such glyph is found. */
22595
22596 static int
22597 right_overwriting (struct glyph_string *s)
22598 {
22599 int i, k, x;
22600 int end = s->row->used[s->area];
22601 struct glyph *glyphs = s->row->glyphs[s->area];
22602 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22603
22604 k = -1;
22605 x = 0;
22606 for (i = first; i < end; ++i)
22607 {
22608 int left, right;
22609 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22610 if (x - left < 0)
22611 k = i;
22612 x += glyphs[i].pixel_width;
22613 }
22614
22615 return k;
22616 }
22617
22618
22619 /* Set background width of glyph string S. START is the index of the
22620 first glyph following S. LAST_X is the right-most x-position + 1
22621 in the drawing area. */
22622
22623 static inline void
22624 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22625 {
22626 /* If the face of this glyph string has to be drawn to the end of
22627 the drawing area, set S->extends_to_end_of_line_p. */
22628
22629 if (start == s->row->used[s->area]
22630 && s->area == TEXT_AREA
22631 && ((s->row->fill_line_p
22632 && (s->hl == DRAW_NORMAL_TEXT
22633 || s->hl == DRAW_IMAGE_RAISED
22634 || s->hl == DRAW_IMAGE_SUNKEN))
22635 || s->hl == DRAW_MOUSE_FACE))
22636 s->extends_to_end_of_line_p = 1;
22637
22638 /* If S extends its face to the end of the line, set its
22639 background_width to the distance to the right edge of the drawing
22640 area. */
22641 if (s->extends_to_end_of_line_p)
22642 s->background_width = last_x - s->x + 1;
22643 else
22644 s->background_width = s->width;
22645 }
22646
22647
22648 /* Compute overhangs and x-positions for glyph string S and its
22649 predecessors, or successors. X is the starting x-position for S.
22650 BACKWARD_P non-zero means process predecessors. */
22651
22652 static void
22653 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22654 {
22655 if (backward_p)
22656 {
22657 while (s)
22658 {
22659 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22660 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22661 x -= s->width;
22662 s->x = x;
22663 s = s->prev;
22664 }
22665 }
22666 else
22667 {
22668 while (s)
22669 {
22670 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22671 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22672 s->x = x;
22673 x += s->width;
22674 s = s->next;
22675 }
22676 }
22677 }
22678
22679
22680
22681 /* The following macros are only called from draw_glyphs below.
22682 They reference the following parameters of that function directly:
22683 `w', `row', `area', and `overlap_p'
22684 as well as the following local variables:
22685 `s', `f', and `hdc' (in W32) */
22686
22687 #ifdef HAVE_NTGUI
22688 /* On W32, silently add local `hdc' variable to argument list of
22689 init_glyph_string. */
22690 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22691 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22692 #else
22693 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22694 init_glyph_string (s, char2b, w, row, area, start, hl)
22695 #endif
22696
22697 /* Add a glyph string for a stretch glyph to the list of strings
22698 between HEAD and TAIL. START is the index of the stretch glyph in
22699 row area AREA of glyph row ROW. END is the index of the last glyph
22700 in that glyph row area. X is the current output position assigned
22701 to the new glyph string constructed. HL overrides that face of the
22702 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22703 is the right-most x-position of the drawing area. */
22704
22705 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22706 and below -- keep them on one line. */
22707 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22708 do \
22709 { \
22710 s = (struct glyph_string *) alloca (sizeof *s); \
22711 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22712 START = fill_stretch_glyph_string (s, START, END); \
22713 append_glyph_string (&HEAD, &TAIL, s); \
22714 s->x = (X); \
22715 } \
22716 while (0)
22717
22718
22719 /* Add a glyph string for an image glyph to the list of strings
22720 between HEAD and TAIL. START is the index of the image glyph in
22721 row area AREA of glyph row ROW. END is the index of the last glyph
22722 in that glyph row area. X is the current output position assigned
22723 to the new glyph string constructed. HL overrides that face of the
22724 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22725 is the right-most x-position of the drawing area. */
22726
22727 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22728 do \
22729 { \
22730 s = (struct glyph_string *) alloca (sizeof *s); \
22731 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22732 fill_image_glyph_string (s); \
22733 append_glyph_string (&HEAD, &TAIL, s); \
22734 ++START; \
22735 s->x = (X); \
22736 } \
22737 while (0)
22738
22739
22740 /* Add a glyph string for a sequence of character glyphs to the list
22741 of strings between HEAD and TAIL. START is the index of the first
22742 glyph in row area AREA of glyph row ROW that is part of the new
22743 glyph string. END is the index of the last glyph in that glyph row
22744 area. X is the current output position assigned to the new glyph
22745 string constructed. HL overrides that face of the glyph; e.g. it
22746 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22747 right-most x-position of the drawing area. */
22748
22749 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22750 do \
22751 { \
22752 int face_id; \
22753 XChar2b *char2b; \
22754 \
22755 face_id = (row)->glyphs[area][START].face_id; \
22756 \
22757 s = (struct glyph_string *) alloca (sizeof *s); \
22758 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22759 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22760 append_glyph_string (&HEAD, &TAIL, s); \
22761 s->x = (X); \
22762 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22763 } \
22764 while (0)
22765
22766
22767 /* Add a glyph string for a composite sequence to the list of strings
22768 between HEAD and TAIL. START is the index of the first glyph in
22769 row area AREA of glyph row ROW that is part of the new glyph
22770 string. END is the index of the last glyph in that glyph row area.
22771 X is the current output position assigned to the new glyph string
22772 constructed. HL overrides that face of the glyph; e.g. it is
22773 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22774 x-position of the drawing area. */
22775
22776 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22777 do { \
22778 int face_id = (row)->glyphs[area][START].face_id; \
22779 struct face *base_face = FACE_FROM_ID (f, face_id); \
22780 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22781 struct composition *cmp = composition_table[cmp_id]; \
22782 XChar2b *char2b; \
22783 struct glyph_string *first_s = NULL; \
22784 int n; \
22785 \
22786 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22787 \
22788 /* Make glyph_strings for each glyph sequence that is drawable by \
22789 the same face, and append them to HEAD/TAIL. */ \
22790 for (n = 0; n < cmp->glyph_len;) \
22791 { \
22792 s = (struct glyph_string *) alloca (sizeof *s); \
22793 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22794 append_glyph_string (&(HEAD), &(TAIL), s); \
22795 s->cmp = cmp; \
22796 s->cmp_from = n; \
22797 s->x = (X); \
22798 if (n == 0) \
22799 first_s = s; \
22800 n = fill_composite_glyph_string (s, base_face, overlaps); \
22801 } \
22802 \
22803 ++START; \
22804 s = first_s; \
22805 } while (0)
22806
22807
22808 /* Add a glyph string for a glyph-string sequence to the list of strings
22809 between HEAD and TAIL. */
22810
22811 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22812 do { \
22813 int face_id; \
22814 XChar2b *char2b; \
22815 Lisp_Object gstring; \
22816 \
22817 face_id = (row)->glyphs[area][START].face_id; \
22818 gstring = (composition_gstring_from_id \
22819 ((row)->glyphs[area][START].u.cmp.id)); \
22820 s = (struct glyph_string *) alloca (sizeof *s); \
22821 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22822 * LGSTRING_GLYPH_LEN (gstring)); \
22823 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22824 append_glyph_string (&(HEAD), &(TAIL), s); \
22825 s->x = (X); \
22826 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22827 } while (0)
22828
22829
22830 /* Add a glyph string for a sequence of glyphless character's glyphs
22831 to the list of strings between HEAD and TAIL. The meanings of
22832 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22833
22834 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22835 do \
22836 { \
22837 int face_id; \
22838 \
22839 face_id = (row)->glyphs[area][START].face_id; \
22840 \
22841 s = (struct glyph_string *) alloca (sizeof *s); \
22842 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22843 append_glyph_string (&HEAD, &TAIL, s); \
22844 s->x = (X); \
22845 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22846 overlaps); \
22847 } \
22848 while (0)
22849
22850
22851 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22852 of AREA of glyph row ROW on window W between indices START and END.
22853 HL overrides the face for drawing glyph strings, e.g. it is
22854 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22855 x-positions of the drawing area.
22856
22857 This is an ugly monster macro construct because we must use alloca
22858 to allocate glyph strings (because draw_glyphs can be called
22859 asynchronously). */
22860
22861 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22862 do \
22863 { \
22864 HEAD = TAIL = NULL; \
22865 while (START < END) \
22866 { \
22867 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22868 switch (first_glyph->type) \
22869 { \
22870 case CHAR_GLYPH: \
22871 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22872 HL, X, LAST_X); \
22873 break; \
22874 \
22875 case COMPOSITE_GLYPH: \
22876 if (first_glyph->u.cmp.automatic) \
22877 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22878 HL, X, LAST_X); \
22879 else \
22880 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22881 HL, X, LAST_X); \
22882 break; \
22883 \
22884 case STRETCH_GLYPH: \
22885 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22886 HL, X, LAST_X); \
22887 break; \
22888 \
22889 case IMAGE_GLYPH: \
22890 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22891 HL, X, LAST_X); \
22892 break; \
22893 \
22894 case GLYPHLESS_GLYPH: \
22895 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22896 HL, X, LAST_X); \
22897 break; \
22898 \
22899 default: \
22900 abort (); \
22901 } \
22902 \
22903 if (s) \
22904 { \
22905 set_glyph_string_background_width (s, START, LAST_X); \
22906 (X) += s->width; \
22907 } \
22908 } \
22909 } while (0)
22910
22911
22912 /* Draw glyphs between START and END in AREA of ROW on window W,
22913 starting at x-position X. X is relative to AREA in W. HL is a
22914 face-override with the following meaning:
22915
22916 DRAW_NORMAL_TEXT draw normally
22917 DRAW_CURSOR draw in cursor face
22918 DRAW_MOUSE_FACE draw in mouse face.
22919 DRAW_INVERSE_VIDEO draw in mode line face
22920 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22921 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22922
22923 If OVERLAPS is non-zero, draw only the foreground of characters and
22924 clip to the physical height of ROW. Non-zero value also defines
22925 the overlapping part to be drawn:
22926
22927 OVERLAPS_PRED overlap with preceding rows
22928 OVERLAPS_SUCC overlap with succeeding rows
22929 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22930 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22931
22932 Value is the x-position reached, relative to AREA of W. */
22933
22934 static int
22935 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22936 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22937 enum draw_glyphs_face hl, int overlaps)
22938 {
22939 struct glyph_string *head, *tail;
22940 struct glyph_string *s;
22941 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22942 int i, j, x_reached, last_x, area_left = 0;
22943 struct frame *f = XFRAME (WINDOW_FRAME (w));
22944 DECLARE_HDC (hdc);
22945
22946 ALLOCATE_HDC (hdc, f);
22947
22948 /* Let's rather be paranoid than getting a SEGV. */
22949 end = min (end, row->used[area]);
22950 start = max (0, start);
22951 start = min (end, start);
22952
22953 /* Translate X to frame coordinates. Set last_x to the right
22954 end of the drawing area. */
22955 if (row->full_width_p)
22956 {
22957 /* X is relative to the left edge of W, without scroll bars
22958 or fringes. */
22959 area_left = WINDOW_LEFT_EDGE_X (w);
22960 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22961 }
22962 else
22963 {
22964 area_left = window_box_left (w, area);
22965 last_x = area_left + window_box_width (w, area);
22966 }
22967 x += area_left;
22968
22969 /* Build a doubly-linked list of glyph_string structures between
22970 head and tail from what we have to draw. Note that the macro
22971 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22972 the reason we use a separate variable `i'. */
22973 i = start;
22974 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22975 if (tail)
22976 x_reached = tail->x + tail->background_width;
22977 else
22978 x_reached = x;
22979
22980 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22981 the row, redraw some glyphs in front or following the glyph
22982 strings built above. */
22983 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22984 {
22985 struct glyph_string *h, *t;
22986 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22987 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22988 int check_mouse_face = 0;
22989 int dummy_x = 0;
22990
22991 /* If mouse highlighting is on, we may need to draw adjacent
22992 glyphs using mouse-face highlighting. */
22993 if (area == TEXT_AREA && row->mouse_face_p)
22994 {
22995 struct glyph_row *mouse_beg_row, *mouse_end_row;
22996
22997 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22998 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22999
23000 if (row >= mouse_beg_row && row <= mouse_end_row)
23001 {
23002 check_mouse_face = 1;
23003 mouse_beg_col = (row == mouse_beg_row)
23004 ? hlinfo->mouse_face_beg_col : 0;
23005 mouse_end_col = (row == mouse_end_row)
23006 ? hlinfo->mouse_face_end_col
23007 : row->used[TEXT_AREA];
23008 }
23009 }
23010
23011 /* Compute overhangs for all glyph strings. */
23012 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23013 for (s = head; s; s = s->next)
23014 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23015
23016 /* Prepend glyph strings for glyphs in front of the first glyph
23017 string that are overwritten because of the first glyph
23018 string's left overhang. The background of all strings
23019 prepended must be drawn because the first glyph string
23020 draws over it. */
23021 i = left_overwritten (head);
23022 if (i >= 0)
23023 {
23024 enum draw_glyphs_face overlap_hl;
23025
23026 /* If this row contains mouse highlighting, attempt to draw
23027 the overlapped glyphs with the correct highlight. This
23028 code fails if the overlap encompasses more than one glyph
23029 and mouse-highlight spans only some of these glyphs.
23030 However, making it work perfectly involves a lot more
23031 code, and I don't know if the pathological case occurs in
23032 practice, so we'll stick to this for now. --- cyd */
23033 if (check_mouse_face
23034 && mouse_beg_col < start && mouse_end_col > i)
23035 overlap_hl = DRAW_MOUSE_FACE;
23036 else
23037 overlap_hl = DRAW_NORMAL_TEXT;
23038
23039 j = i;
23040 BUILD_GLYPH_STRINGS (j, start, h, t,
23041 overlap_hl, dummy_x, last_x);
23042 start = i;
23043 compute_overhangs_and_x (t, head->x, 1);
23044 prepend_glyph_string_lists (&head, &tail, h, t);
23045 clip_head = head;
23046 }
23047
23048 /* Prepend glyph strings for glyphs in front of the first glyph
23049 string that overwrite that glyph string because of their
23050 right overhang. For these strings, only the foreground must
23051 be drawn, because it draws over the glyph string at `head'.
23052 The background must not be drawn because this would overwrite
23053 right overhangs of preceding glyphs for which no glyph
23054 strings exist. */
23055 i = left_overwriting (head);
23056 if (i >= 0)
23057 {
23058 enum draw_glyphs_face overlap_hl;
23059
23060 if (check_mouse_face
23061 && mouse_beg_col < start && mouse_end_col > i)
23062 overlap_hl = DRAW_MOUSE_FACE;
23063 else
23064 overlap_hl = DRAW_NORMAL_TEXT;
23065
23066 clip_head = head;
23067 BUILD_GLYPH_STRINGS (i, start, h, t,
23068 overlap_hl, dummy_x, last_x);
23069 for (s = h; s; s = s->next)
23070 s->background_filled_p = 1;
23071 compute_overhangs_and_x (t, head->x, 1);
23072 prepend_glyph_string_lists (&head, &tail, h, t);
23073 }
23074
23075 /* Append glyphs strings for glyphs following the last glyph
23076 string tail that are overwritten by tail. The background of
23077 these strings has to be drawn because tail's foreground draws
23078 over it. */
23079 i = right_overwritten (tail);
23080 if (i >= 0)
23081 {
23082 enum draw_glyphs_face overlap_hl;
23083
23084 if (check_mouse_face
23085 && mouse_beg_col < i && mouse_end_col > end)
23086 overlap_hl = DRAW_MOUSE_FACE;
23087 else
23088 overlap_hl = DRAW_NORMAL_TEXT;
23089
23090 BUILD_GLYPH_STRINGS (end, i, h, t,
23091 overlap_hl, x, last_x);
23092 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23093 we don't have `end = i;' here. */
23094 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23095 append_glyph_string_lists (&head, &tail, h, t);
23096 clip_tail = tail;
23097 }
23098
23099 /* Append glyph strings for glyphs following the last glyph
23100 string tail that overwrite tail. The foreground of such
23101 glyphs has to be drawn because it writes into the background
23102 of tail. The background must not be drawn because it could
23103 paint over the foreground of following glyphs. */
23104 i = right_overwriting (tail);
23105 if (i >= 0)
23106 {
23107 enum draw_glyphs_face overlap_hl;
23108 if (check_mouse_face
23109 && mouse_beg_col < i && mouse_end_col > end)
23110 overlap_hl = DRAW_MOUSE_FACE;
23111 else
23112 overlap_hl = DRAW_NORMAL_TEXT;
23113
23114 clip_tail = tail;
23115 i++; /* We must include the Ith glyph. */
23116 BUILD_GLYPH_STRINGS (end, i, h, t,
23117 overlap_hl, x, last_x);
23118 for (s = h; s; s = s->next)
23119 s->background_filled_p = 1;
23120 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23121 append_glyph_string_lists (&head, &tail, h, t);
23122 }
23123 if (clip_head || clip_tail)
23124 for (s = head; s; s = s->next)
23125 {
23126 s->clip_head = clip_head;
23127 s->clip_tail = clip_tail;
23128 }
23129 }
23130
23131 /* Draw all strings. */
23132 for (s = head; s; s = s->next)
23133 FRAME_RIF (f)->draw_glyph_string (s);
23134
23135 #ifndef HAVE_NS
23136 /* When focus a sole frame and move horizontally, this sets on_p to 0
23137 causing a failure to erase prev cursor position. */
23138 if (area == TEXT_AREA
23139 && !row->full_width_p
23140 /* When drawing overlapping rows, only the glyph strings'
23141 foreground is drawn, which doesn't erase a cursor
23142 completely. */
23143 && !overlaps)
23144 {
23145 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23146 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23147 : (tail ? tail->x + tail->background_width : x));
23148 x0 -= area_left;
23149 x1 -= area_left;
23150
23151 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23152 row->y, MATRIX_ROW_BOTTOM_Y (row));
23153 }
23154 #endif
23155
23156 /* Value is the x-position up to which drawn, relative to AREA of W.
23157 This doesn't include parts drawn because of overhangs. */
23158 if (row->full_width_p)
23159 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23160 else
23161 x_reached -= area_left;
23162
23163 RELEASE_HDC (hdc, f);
23164
23165 return x_reached;
23166 }
23167
23168 /* Expand row matrix if too narrow. Don't expand if area
23169 is not present. */
23170
23171 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23172 { \
23173 if (!fonts_changed_p \
23174 && (it->glyph_row->glyphs[area] \
23175 < it->glyph_row->glyphs[area + 1])) \
23176 { \
23177 it->w->ncols_scale_factor++; \
23178 fonts_changed_p = 1; \
23179 } \
23180 }
23181
23182 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23183 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23184
23185 static inline void
23186 append_glyph (struct it *it)
23187 {
23188 struct glyph *glyph;
23189 enum glyph_row_area area = it->area;
23190
23191 xassert (it->glyph_row);
23192 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23193
23194 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23195 if (glyph < it->glyph_row->glyphs[area + 1])
23196 {
23197 /* If the glyph row is reversed, we need to prepend the glyph
23198 rather than append it. */
23199 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23200 {
23201 struct glyph *g;
23202
23203 /* Make room for the additional glyph. */
23204 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23205 g[1] = *g;
23206 glyph = it->glyph_row->glyphs[area];
23207 }
23208 glyph->charpos = CHARPOS (it->position);
23209 glyph->object = it->object;
23210 if (it->pixel_width > 0)
23211 {
23212 glyph->pixel_width = it->pixel_width;
23213 glyph->padding_p = 0;
23214 }
23215 else
23216 {
23217 /* Assure at least 1-pixel width. Otherwise, cursor can't
23218 be displayed correctly. */
23219 glyph->pixel_width = 1;
23220 glyph->padding_p = 1;
23221 }
23222 glyph->ascent = it->ascent;
23223 glyph->descent = it->descent;
23224 glyph->voffset = it->voffset;
23225 glyph->type = CHAR_GLYPH;
23226 glyph->avoid_cursor_p = it->avoid_cursor_p;
23227 glyph->multibyte_p = it->multibyte_p;
23228 glyph->left_box_line_p = it->start_of_box_run_p;
23229 glyph->right_box_line_p = it->end_of_box_run_p;
23230 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23231 || it->phys_descent > it->descent);
23232 glyph->glyph_not_available_p = it->glyph_not_available_p;
23233 glyph->face_id = it->face_id;
23234 glyph->u.ch = it->char_to_display;
23235 glyph->slice.img = null_glyph_slice;
23236 glyph->font_type = FONT_TYPE_UNKNOWN;
23237 if (it->bidi_p)
23238 {
23239 glyph->resolved_level = it->bidi_it.resolved_level;
23240 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23241 abort ();
23242 glyph->bidi_type = it->bidi_it.type;
23243 }
23244 else
23245 {
23246 glyph->resolved_level = 0;
23247 glyph->bidi_type = UNKNOWN_BT;
23248 }
23249 ++it->glyph_row->used[area];
23250 }
23251 else
23252 IT_EXPAND_MATRIX_WIDTH (it, area);
23253 }
23254
23255 /* Store one glyph for the composition IT->cmp_it.id in
23256 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23257 non-null. */
23258
23259 static inline void
23260 append_composite_glyph (struct it *it)
23261 {
23262 struct glyph *glyph;
23263 enum glyph_row_area area = it->area;
23264
23265 xassert (it->glyph_row);
23266
23267 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23268 if (glyph < it->glyph_row->glyphs[area + 1])
23269 {
23270 /* If the glyph row is reversed, we need to prepend the glyph
23271 rather than append it. */
23272 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23273 {
23274 struct glyph *g;
23275
23276 /* Make room for the new glyph. */
23277 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23278 g[1] = *g;
23279 glyph = it->glyph_row->glyphs[it->area];
23280 }
23281 glyph->charpos = it->cmp_it.charpos;
23282 glyph->object = it->object;
23283 glyph->pixel_width = it->pixel_width;
23284 glyph->ascent = it->ascent;
23285 glyph->descent = it->descent;
23286 glyph->voffset = it->voffset;
23287 glyph->type = COMPOSITE_GLYPH;
23288 if (it->cmp_it.ch < 0)
23289 {
23290 glyph->u.cmp.automatic = 0;
23291 glyph->u.cmp.id = it->cmp_it.id;
23292 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23293 }
23294 else
23295 {
23296 glyph->u.cmp.automatic = 1;
23297 glyph->u.cmp.id = it->cmp_it.id;
23298 glyph->slice.cmp.from = it->cmp_it.from;
23299 glyph->slice.cmp.to = it->cmp_it.to - 1;
23300 }
23301 glyph->avoid_cursor_p = it->avoid_cursor_p;
23302 glyph->multibyte_p = it->multibyte_p;
23303 glyph->left_box_line_p = it->start_of_box_run_p;
23304 glyph->right_box_line_p = it->end_of_box_run_p;
23305 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23306 || it->phys_descent > it->descent);
23307 glyph->padding_p = 0;
23308 glyph->glyph_not_available_p = 0;
23309 glyph->face_id = it->face_id;
23310 glyph->font_type = FONT_TYPE_UNKNOWN;
23311 if (it->bidi_p)
23312 {
23313 glyph->resolved_level = it->bidi_it.resolved_level;
23314 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23315 abort ();
23316 glyph->bidi_type = it->bidi_it.type;
23317 }
23318 ++it->glyph_row->used[area];
23319 }
23320 else
23321 IT_EXPAND_MATRIX_WIDTH (it, area);
23322 }
23323
23324
23325 /* Change IT->ascent and IT->height according to the setting of
23326 IT->voffset. */
23327
23328 static inline void
23329 take_vertical_position_into_account (struct it *it)
23330 {
23331 if (it->voffset)
23332 {
23333 if (it->voffset < 0)
23334 /* Increase the ascent so that we can display the text higher
23335 in the line. */
23336 it->ascent -= it->voffset;
23337 else
23338 /* Increase the descent so that we can display the text lower
23339 in the line. */
23340 it->descent += it->voffset;
23341 }
23342 }
23343
23344
23345 /* Produce glyphs/get display metrics for the image IT is loaded with.
23346 See the description of struct display_iterator in dispextern.h for
23347 an overview of struct display_iterator. */
23348
23349 static void
23350 produce_image_glyph (struct it *it)
23351 {
23352 struct image *img;
23353 struct face *face;
23354 int glyph_ascent, crop;
23355 struct glyph_slice slice;
23356
23357 xassert (it->what == IT_IMAGE);
23358
23359 face = FACE_FROM_ID (it->f, it->face_id);
23360 xassert (face);
23361 /* Make sure X resources of the face is loaded. */
23362 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23363
23364 if (it->image_id < 0)
23365 {
23366 /* Fringe bitmap. */
23367 it->ascent = it->phys_ascent = 0;
23368 it->descent = it->phys_descent = 0;
23369 it->pixel_width = 0;
23370 it->nglyphs = 0;
23371 return;
23372 }
23373
23374 img = IMAGE_FROM_ID (it->f, it->image_id);
23375 xassert (img);
23376 /* Make sure X resources of the image is loaded. */
23377 prepare_image_for_display (it->f, img);
23378
23379 slice.x = slice.y = 0;
23380 slice.width = img->width;
23381 slice.height = img->height;
23382
23383 if (INTEGERP (it->slice.x))
23384 slice.x = XINT (it->slice.x);
23385 else if (FLOATP (it->slice.x))
23386 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23387
23388 if (INTEGERP (it->slice.y))
23389 slice.y = XINT (it->slice.y);
23390 else if (FLOATP (it->slice.y))
23391 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23392
23393 if (INTEGERP (it->slice.width))
23394 slice.width = XINT (it->slice.width);
23395 else if (FLOATP (it->slice.width))
23396 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23397
23398 if (INTEGERP (it->slice.height))
23399 slice.height = XINT (it->slice.height);
23400 else if (FLOATP (it->slice.height))
23401 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23402
23403 if (slice.x >= img->width)
23404 slice.x = img->width;
23405 if (slice.y >= img->height)
23406 slice.y = img->height;
23407 if (slice.x + slice.width >= img->width)
23408 slice.width = img->width - slice.x;
23409 if (slice.y + slice.height > img->height)
23410 slice.height = img->height - slice.y;
23411
23412 if (slice.width == 0 || slice.height == 0)
23413 return;
23414
23415 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23416
23417 it->descent = slice.height - glyph_ascent;
23418 if (slice.y == 0)
23419 it->descent += img->vmargin;
23420 if (slice.y + slice.height == img->height)
23421 it->descent += img->vmargin;
23422 it->phys_descent = it->descent;
23423
23424 it->pixel_width = slice.width;
23425 if (slice.x == 0)
23426 it->pixel_width += img->hmargin;
23427 if (slice.x + slice.width == img->width)
23428 it->pixel_width += img->hmargin;
23429
23430 /* It's quite possible for images to have an ascent greater than
23431 their height, so don't get confused in that case. */
23432 if (it->descent < 0)
23433 it->descent = 0;
23434
23435 it->nglyphs = 1;
23436
23437 if (face->box != FACE_NO_BOX)
23438 {
23439 if (face->box_line_width > 0)
23440 {
23441 if (slice.y == 0)
23442 it->ascent += face->box_line_width;
23443 if (slice.y + slice.height == img->height)
23444 it->descent += face->box_line_width;
23445 }
23446
23447 if (it->start_of_box_run_p && slice.x == 0)
23448 it->pixel_width += eabs (face->box_line_width);
23449 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23450 it->pixel_width += eabs (face->box_line_width);
23451 }
23452
23453 take_vertical_position_into_account (it);
23454
23455 /* Automatically crop wide image glyphs at right edge so we can
23456 draw the cursor on same display row. */
23457 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23458 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23459 {
23460 it->pixel_width -= crop;
23461 slice.width -= crop;
23462 }
23463
23464 if (it->glyph_row)
23465 {
23466 struct glyph *glyph;
23467 enum glyph_row_area area = it->area;
23468
23469 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23470 if (glyph < it->glyph_row->glyphs[area + 1])
23471 {
23472 glyph->charpos = CHARPOS (it->position);
23473 glyph->object = it->object;
23474 glyph->pixel_width = it->pixel_width;
23475 glyph->ascent = glyph_ascent;
23476 glyph->descent = it->descent;
23477 glyph->voffset = it->voffset;
23478 glyph->type = IMAGE_GLYPH;
23479 glyph->avoid_cursor_p = it->avoid_cursor_p;
23480 glyph->multibyte_p = it->multibyte_p;
23481 glyph->left_box_line_p = it->start_of_box_run_p;
23482 glyph->right_box_line_p = it->end_of_box_run_p;
23483 glyph->overlaps_vertically_p = 0;
23484 glyph->padding_p = 0;
23485 glyph->glyph_not_available_p = 0;
23486 glyph->face_id = it->face_id;
23487 glyph->u.img_id = img->id;
23488 glyph->slice.img = slice;
23489 glyph->font_type = FONT_TYPE_UNKNOWN;
23490 if (it->bidi_p)
23491 {
23492 glyph->resolved_level = it->bidi_it.resolved_level;
23493 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23494 abort ();
23495 glyph->bidi_type = it->bidi_it.type;
23496 }
23497 ++it->glyph_row->used[area];
23498 }
23499 else
23500 IT_EXPAND_MATRIX_WIDTH (it, area);
23501 }
23502 }
23503
23504
23505 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23506 of the glyph, WIDTH and HEIGHT are the width and height of the
23507 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23508
23509 static void
23510 append_stretch_glyph (struct it *it, Lisp_Object object,
23511 int width, int height, int ascent)
23512 {
23513 struct glyph *glyph;
23514 enum glyph_row_area area = it->area;
23515
23516 xassert (ascent >= 0 && ascent <= height);
23517
23518 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23519 if (glyph < it->glyph_row->glyphs[area + 1])
23520 {
23521 /* If the glyph row is reversed, we need to prepend the glyph
23522 rather than append it. */
23523 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23524 {
23525 struct glyph *g;
23526
23527 /* Make room for the additional glyph. */
23528 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23529 g[1] = *g;
23530 glyph = it->glyph_row->glyphs[area];
23531 }
23532 glyph->charpos = CHARPOS (it->position);
23533 glyph->object = object;
23534 glyph->pixel_width = width;
23535 glyph->ascent = ascent;
23536 glyph->descent = height - ascent;
23537 glyph->voffset = it->voffset;
23538 glyph->type = STRETCH_GLYPH;
23539 glyph->avoid_cursor_p = it->avoid_cursor_p;
23540 glyph->multibyte_p = it->multibyte_p;
23541 glyph->left_box_line_p = it->start_of_box_run_p;
23542 glyph->right_box_line_p = it->end_of_box_run_p;
23543 glyph->overlaps_vertically_p = 0;
23544 glyph->padding_p = 0;
23545 glyph->glyph_not_available_p = 0;
23546 glyph->face_id = it->face_id;
23547 glyph->u.stretch.ascent = ascent;
23548 glyph->u.stretch.height = height;
23549 glyph->slice.img = null_glyph_slice;
23550 glyph->font_type = FONT_TYPE_UNKNOWN;
23551 if (it->bidi_p)
23552 {
23553 glyph->resolved_level = it->bidi_it.resolved_level;
23554 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23555 abort ();
23556 glyph->bidi_type = it->bidi_it.type;
23557 }
23558 else
23559 {
23560 glyph->resolved_level = 0;
23561 glyph->bidi_type = UNKNOWN_BT;
23562 }
23563 ++it->glyph_row->used[area];
23564 }
23565 else
23566 IT_EXPAND_MATRIX_WIDTH (it, area);
23567 }
23568
23569 #endif /* HAVE_WINDOW_SYSTEM */
23570
23571 /* Produce a stretch glyph for iterator IT. IT->object is the value
23572 of the glyph property displayed. The value must be a list
23573 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23574 being recognized:
23575
23576 1. `:width WIDTH' specifies that the space should be WIDTH *
23577 canonical char width wide. WIDTH may be an integer or floating
23578 point number.
23579
23580 2. `:relative-width FACTOR' specifies that the width of the stretch
23581 should be computed from the width of the first character having the
23582 `glyph' property, and should be FACTOR times that width.
23583
23584 3. `:align-to HPOS' specifies that the space should be wide enough
23585 to reach HPOS, a value in canonical character units.
23586
23587 Exactly one of the above pairs must be present.
23588
23589 4. `:height HEIGHT' specifies that the height of the stretch produced
23590 should be HEIGHT, measured in canonical character units.
23591
23592 5. `:relative-height FACTOR' specifies that the height of the
23593 stretch should be FACTOR times the height of the characters having
23594 the glyph property.
23595
23596 Either none or exactly one of 4 or 5 must be present.
23597
23598 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23599 of the stretch should be used for the ascent of the stretch.
23600 ASCENT must be in the range 0 <= ASCENT <= 100. */
23601
23602 void
23603 produce_stretch_glyph (struct it *it)
23604 {
23605 /* (space :width WIDTH :height HEIGHT ...) */
23606 Lisp_Object prop, plist;
23607 int width = 0, height = 0, align_to = -1;
23608 int zero_width_ok_p = 0;
23609 int ascent = 0;
23610 double tem;
23611 struct face *face = NULL;
23612 struct font *font = NULL;
23613
23614 #ifdef HAVE_WINDOW_SYSTEM
23615 int zero_height_ok_p = 0;
23616
23617 if (FRAME_WINDOW_P (it->f))
23618 {
23619 face = FACE_FROM_ID (it->f, it->face_id);
23620 font = face->font ? face->font : FRAME_FONT (it->f);
23621 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23622 }
23623 #endif
23624
23625 /* List should start with `space'. */
23626 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23627 plist = XCDR (it->object);
23628
23629 /* Compute the width of the stretch. */
23630 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23631 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23632 {
23633 /* Absolute width `:width WIDTH' specified and valid. */
23634 zero_width_ok_p = 1;
23635 width = (int)tem;
23636 }
23637 #ifdef HAVE_WINDOW_SYSTEM
23638 else if (FRAME_WINDOW_P (it->f)
23639 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23640 {
23641 /* Relative width `:relative-width FACTOR' specified and valid.
23642 Compute the width of the characters having the `glyph'
23643 property. */
23644 struct it it2;
23645 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23646
23647 it2 = *it;
23648 if (it->multibyte_p)
23649 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23650 else
23651 {
23652 it2.c = it2.char_to_display = *p, it2.len = 1;
23653 if (! ASCII_CHAR_P (it2.c))
23654 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23655 }
23656
23657 it2.glyph_row = NULL;
23658 it2.what = IT_CHARACTER;
23659 x_produce_glyphs (&it2);
23660 width = NUMVAL (prop) * it2.pixel_width;
23661 }
23662 #endif /* HAVE_WINDOW_SYSTEM */
23663 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23664 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23665 {
23666 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23667 align_to = (align_to < 0
23668 ? 0
23669 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23670 else if (align_to < 0)
23671 align_to = window_box_left_offset (it->w, TEXT_AREA);
23672 width = max (0, (int)tem + align_to - it->current_x);
23673 zero_width_ok_p = 1;
23674 }
23675 else
23676 /* Nothing specified -> width defaults to canonical char width. */
23677 width = FRAME_COLUMN_WIDTH (it->f);
23678
23679 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23680 width = 1;
23681
23682 #ifdef HAVE_WINDOW_SYSTEM
23683 /* Compute height. */
23684 if (FRAME_WINDOW_P (it->f))
23685 {
23686 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23687 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23688 {
23689 height = (int)tem;
23690 zero_height_ok_p = 1;
23691 }
23692 else if (prop = Fplist_get (plist, QCrelative_height),
23693 NUMVAL (prop) > 0)
23694 height = FONT_HEIGHT (font) * NUMVAL (prop);
23695 else
23696 height = FONT_HEIGHT (font);
23697
23698 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23699 height = 1;
23700
23701 /* Compute percentage of height used for ascent. If
23702 `:ascent ASCENT' is present and valid, use that. Otherwise,
23703 derive the ascent from the font in use. */
23704 if (prop = Fplist_get (plist, QCascent),
23705 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23706 ascent = height * NUMVAL (prop) / 100.0;
23707 else if (!NILP (prop)
23708 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23709 ascent = min (max (0, (int)tem), height);
23710 else
23711 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23712 }
23713 else
23714 #endif /* HAVE_WINDOW_SYSTEM */
23715 height = 1;
23716
23717 if (width > 0 && it->line_wrap != TRUNCATE
23718 && it->current_x + width > it->last_visible_x)
23719 {
23720 width = it->last_visible_x - it->current_x;
23721 #ifdef HAVE_WINDOW_SYSTEM
23722 /* Subtract one more pixel from the stretch width, but only on
23723 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23724 width -= FRAME_WINDOW_P (it->f);
23725 #endif
23726 }
23727
23728 if (width > 0 && height > 0 && it->glyph_row)
23729 {
23730 Lisp_Object o_object = it->object;
23731 Lisp_Object object = it->stack[it->sp - 1].string;
23732 int n = width;
23733
23734 if (!STRINGP (object))
23735 object = it->w->buffer;
23736 #ifdef HAVE_WINDOW_SYSTEM
23737 if (FRAME_WINDOW_P (it->f))
23738 append_stretch_glyph (it, object, width, height, ascent);
23739 else
23740 #endif
23741 {
23742 it->object = object;
23743 it->char_to_display = ' ';
23744 it->pixel_width = it->len = 1;
23745 while (n--)
23746 tty_append_glyph (it);
23747 it->object = o_object;
23748 }
23749 }
23750
23751 it->pixel_width = width;
23752 #ifdef HAVE_WINDOW_SYSTEM
23753 if (FRAME_WINDOW_P (it->f))
23754 {
23755 it->ascent = it->phys_ascent = ascent;
23756 it->descent = it->phys_descent = height - it->ascent;
23757 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23758 take_vertical_position_into_account (it);
23759 }
23760 else
23761 #endif
23762 it->nglyphs = width;
23763 }
23764
23765 #ifdef HAVE_WINDOW_SYSTEM
23766
23767 /* Calculate line-height and line-spacing properties.
23768 An integer value specifies explicit pixel value.
23769 A float value specifies relative value to current face height.
23770 A cons (float . face-name) specifies relative value to
23771 height of specified face font.
23772
23773 Returns height in pixels, or nil. */
23774
23775
23776 static Lisp_Object
23777 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23778 int boff, int override)
23779 {
23780 Lisp_Object face_name = Qnil;
23781 int ascent, descent, height;
23782
23783 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23784 return val;
23785
23786 if (CONSP (val))
23787 {
23788 face_name = XCAR (val);
23789 val = XCDR (val);
23790 if (!NUMBERP (val))
23791 val = make_number (1);
23792 if (NILP (face_name))
23793 {
23794 height = it->ascent + it->descent;
23795 goto scale;
23796 }
23797 }
23798
23799 if (NILP (face_name))
23800 {
23801 font = FRAME_FONT (it->f);
23802 boff = FRAME_BASELINE_OFFSET (it->f);
23803 }
23804 else if (EQ (face_name, Qt))
23805 {
23806 override = 0;
23807 }
23808 else
23809 {
23810 int face_id;
23811 struct face *face;
23812
23813 face_id = lookup_named_face (it->f, face_name, 0);
23814 if (face_id < 0)
23815 return make_number (-1);
23816
23817 face = FACE_FROM_ID (it->f, face_id);
23818 font = face->font;
23819 if (font == NULL)
23820 return make_number (-1);
23821 boff = font->baseline_offset;
23822 if (font->vertical_centering)
23823 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23824 }
23825
23826 ascent = FONT_BASE (font) + boff;
23827 descent = FONT_DESCENT (font) - boff;
23828
23829 if (override)
23830 {
23831 it->override_ascent = ascent;
23832 it->override_descent = descent;
23833 it->override_boff = boff;
23834 }
23835
23836 height = ascent + descent;
23837
23838 scale:
23839 if (FLOATP (val))
23840 height = (int)(XFLOAT_DATA (val) * height);
23841 else if (INTEGERP (val))
23842 height *= XINT (val);
23843
23844 return make_number (height);
23845 }
23846
23847
23848 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23849 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23850 and only if this is for a character for which no font was found.
23851
23852 If the display method (it->glyphless_method) is
23853 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23854 length of the acronym or the hexadecimal string, UPPER_XOFF and
23855 UPPER_YOFF are pixel offsets for the upper part of the string,
23856 LOWER_XOFF and LOWER_YOFF are for the lower part.
23857
23858 For the other display methods, LEN through LOWER_YOFF are zero. */
23859
23860 static void
23861 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23862 short upper_xoff, short upper_yoff,
23863 short lower_xoff, short lower_yoff)
23864 {
23865 struct glyph *glyph;
23866 enum glyph_row_area area = it->area;
23867
23868 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23869 if (glyph < it->glyph_row->glyphs[area + 1])
23870 {
23871 /* If the glyph row is reversed, we need to prepend the glyph
23872 rather than append it. */
23873 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23874 {
23875 struct glyph *g;
23876
23877 /* Make room for the additional glyph. */
23878 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23879 g[1] = *g;
23880 glyph = it->glyph_row->glyphs[area];
23881 }
23882 glyph->charpos = CHARPOS (it->position);
23883 glyph->object = it->object;
23884 glyph->pixel_width = it->pixel_width;
23885 glyph->ascent = it->ascent;
23886 glyph->descent = it->descent;
23887 glyph->voffset = it->voffset;
23888 glyph->type = GLYPHLESS_GLYPH;
23889 glyph->u.glyphless.method = it->glyphless_method;
23890 glyph->u.glyphless.for_no_font = for_no_font;
23891 glyph->u.glyphless.len = len;
23892 glyph->u.glyphless.ch = it->c;
23893 glyph->slice.glyphless.upper_xoff = upper_xoff;
23894 glyph->slice.glyphless.upper_yoff = upper_yoff;
23895 glyph->slice.glyphless.lower_xoff = lower_xoff;
23896 glyph->slice.glyphless.lower_yoff = lower_yoff;
23897 glyph->avoid_cursor_p = it->avoid_cursor_p;
23898 glyph->multibyte_p = it->multibyte_p;
23899 glyph->left_box_line_p = it->start_of_box_run_p;
23900 glyph->right_box_line_p = it->end_of_box_run_p;
23901 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23902 || it->phys_descent > it->descent);
23903 glyph->padding_p = 0;
23904 glyph->glyph_not_available_p = 0;
23905 glyph->face_id = face_id;
23906 glyph->font_type = FONT_TYPE_UNKNOWN;
23907 if (it->bidi_p)
23908 {
23909 glyph->resolved_level = it->bidi_it.resolved_level;
23910 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23911 abort ();
23912 glyph->bidi_type = it->bidi_it.type;
23913 }
23914 ++it->glyph_row->used[area];
23915 }
23916 else
23917 IT_EXPAND_MATRIX_WIDTH (it, area);
23918 }
23919
23920
23921 /* Produce a glyph for a glyphless character for iterator IT.
23922 IT->glyphless_method specifies which method to use for displaying
23923 the character. See the description of enum
23924 glyphless_display_method in dispextern.h for the detail.
23925
23926 FOR_NO_FONT is nonzero if and only if this is for a character for
23927 which no font was found. ACRONYM, if non-nil, is an acronym string
23928 for the character. */
23929
23930 static void
23931 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23932 {
23933 int face_id;
23934 struct face *face;
23935 struct font *font;
23936 int base_width, base_height, width, height;
23937 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23938 int len;
23939
23940 /* Get the metrics of the base font. We always refer to the current
23941 ASCII face. */
23942 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23943 font = face->font ? face->font : FRAME_FONT (it->f);
23944 it->ascent = FONT_BASE (font) + font->baseline_offset;
23945 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23946 base_height = it->ascent + it->descent;
23947 base_width = font->average_width;
23948
23949 /* Get a face ID for the glyph by utilizing a cache (the same way as
23950 done for `escape-glyph' in get_next_display_element). */
23951 if (it->f == last_glyphless_glyph_frame
23952 && it->face_id == last_glyphless_glyph_face_id)
23953 {
23954 face_id = last_glyphless_glyph_merged_face_id;
23955 }
23956 else
23957 {
23958 /* Merge the `glyphless-char' face into the current face. */
23959 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23960 last_glyphless_glyph_frame = it->f;
23961 last_glyphless_glyph_face_id = it->face_id;
23962 last_glyphless_glyph_merged_face_id = face_id;
23963 }
23964
23965 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23966 {
23967 it->pixel_width = THIN_SPACE_WIDTH;
23968 len = 0;
23969 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23970 }
23971 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23972 {
23973 width = CHAR_WIDTH (it->c);
23974 if (width == 0)
23975 width = 1;
23976 else if (width > 4)
23977 width = 4;
23978 it->pixel_width = base_width * width;
23979 len = 0;
23980 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23981 }
23982 else
23983 {
23984 char buf[7];
23985 const char *str;
23986 unsigned int code[6];
23987 int upper_len;
23988 int ascent, descent;
23989 struct font_metrics metrics_upper, metrics_lower;
23990
23991 face = FACE_FROM_ID (it->f, face_id);
23992 font = face->font ? face->font : FRAME_FONT (it->f);
23993 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23994
23995 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23996 {
23997 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23998 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23999 if (CONSP (acronym))
24000 acronym = XCAR (acronym);
24001 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24002 }
24003 else
24004 {
24005 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24006 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24007 str = buf;
24008 }
24009 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
24010 code[len] = font->driver->encode_char (font, str[len]);
24011 upper_len = (len + 1) / 2;
24012 font->driver->text_extents (font, code, upper_len,
24013 &metrics_upper);
24014 font->driver->text_extents (font, code + upper_len, len - upper_len,
24015 &metrics_lower);
24016
24017
24018
24019 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24020 width = max (metrics_upper.width, metrics_lower.width) + 4;
24021 upper_xoff = upper_yoff = 2; /* the typical case */
24022 if (base_width >= width)
24023 {
24024 /* Align the upper to the left, the lower to the right. */
24025 it->pixel_width = base_width;
24026 lower_xoff = base_width - 2 - metrics_lower.width;
24027 }
24028 else
24029 {
24030 /* Center the shorter one. */
24031 it->pixel_width = width;
24032 if (metrics_upper.width >= metrics_lower.width)
24033 lower_xoff = (width - metrics_lower.width) / 2;
24034 else
24035 {
24036 /* FIXME: This code doesn't look right. It formerly was
24037 missing the "lower_xoff = 0;", which couldn't have
24038 been right since it left lower_xoff uninitialized. */
24039 lower_xoff = 0;
24040 upper_xoff = (width - metrics_upper.width) / 2;
24041 }
24042 }
24043
24044 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24045 top, bottom, and between upper and lower strings. */
24046 height = (metrics_upper.ascent + metrics_upper.descent
24047 + metrics_lower.ascent + metrics_lower.descent) + 5;
24048 /* Center vertically.
24049 H:base_height, D:base_descent
24050 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24051
24052 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24053 descent = D - H/2 + h/2;
24054 lower_yoff = descent - 2 - ld;
24055 upper_yoff = lower_yoff - la - 1 - ud; */
24056 ascent = - (it->descent - (base_height + height + 1) / 2);
24057 descent = it->descent - (base_height - height) / 2;
24058 lower_yoff = descent - 2 - metrics_lower.descent;
24059 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24060 - metrics_upper.descent);
24061 /* Don't make the height shorter than the base height. */
24062 if (height > base_height)
24063 {
24064 it->ascent = ascent;
24065 it->descent = descent;
24066 }
24067 }
24068
24069 it->phys_ascent = it->ascent;
24070 it->phys_descent = it->descent;
24071 if (it->glyph_row)
24072 append_glyphless_glyph (it, face_id, for_no_font, len,
24073 upper_xoff, upper_yoff,
24074 lower_xoff, lower_yoff);
24075 it->nglyphs = 1;
24076 take_vertical_position_into_account (it);
24077 }
24078
24079
24080 /* RIF:
24081 Produce glyphs/get display metrics for the display element IT is
24082 loaded with. See the description of struct it in dispextern.h
24083 for an overview of struct it. */
24084
24085 void
24086 x_produce_glyphs (struct it *it)
24087 {
24088 int extra_line_spacing = it->extra_line_spacing;
24089
24090 it->glyph_not_available_p = 0;
24091
24092 if (it->what == IT_CHARACTER)
24093 {
24094 XChar2b char2b;
24095 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24096 struct font *font = face->font;
24097 struct font_metrics *pcm = NULL;
24098 int boff; /* baseline offset */
24099
24100 if (font == NULL)
24101 {
24102 /* When no suitable font is found, display this character by
24103 the method specified in the first extra slot of
24104 Vglyphless_char_display. */
24105 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24106
24107 xassert (it->what == IT_GLYPHLESS);
24108 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24109 goto done;
24110 }
24111
24112 boff = font->baseline_offset;
24113 if (font->vertical_centering)
24114 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24115
24116 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24117 {
24118 int stretched_p;
24119
24120 it->nglyphs = 1;
24121
24122 if (it->override_ascent >= 0)
24123 {
24124 it->ascent = it->override_ascent;
24125 it->descent = it->override_descent;
24126 boff = it->override_boff;
24127 }
24128 else
24129 {
24130 it->ascent = FONT_BASE (font) + boff;
24131 it->descent = FONT_DESCENT (font) - boff;
24132 }
24133
24134 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24135 {
24136 pcm = get_per_char_metric (font, &char2b);
24137 if (pcm->width == 0
24138 && pcm->rbearing == 0 && pcm->lbearing == 0)
24139 pcm = NULL;
24140 }
24141
24142 if (pcm)
24143 {
24144 it->phys_ascent = pcm->ascent + boff;
24145 it->phys_descent = pcm->descent - boff;
24146 it->pixel_width = pcm->width;
24147 }
24148 else
24149 {
24150 it->glyph_not_available_p = 1;
24151 it->phys_ascent = it->ascent;
24152 it->phys_descent = it->descent;
24153 it->pixel_width = font->space_width;
24154 }
24155
24156 if (it->constrain_row_ascent_descent_p)
24157 {
24158 if (it->descent > it->max_descent)
24159 {
24160 it->ascent += it->descent - it->max_descent;
24161 it->descent = it->max_descent;
24162 }
24163 if (it->ascent > it->max_ascent)
24164 {
24165 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24166 it->ascent = it->max_ascent;
24167 }
24168 it->phys_ascent = min (it->phys_ascent, it->ascent);
24169 it->phys_descent = min (it->phys_descent, it->descent);
24170 extra_line_spacing = 0;
24171 }
24172
24173 /* If this is a space inside a region of text with
24174 `space-width' property, change its width. */
24175 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24176 if (stretched_p)
24177 it->pixel_width *= XFLOATINT (it->space_width);
24178
24179 /* If face has a box, add the box thickness to the character
24180 height. If character has a box line to the left and/or
24181 right, add the box line width to the character's width. */
24182 if (face->box != FACE_NO_BOX)
24183 {
24184 int thick = face->box_line_width;
24185
24186 if (thick > 0)
24187 {
24188 it->ascent += thick;
24189 it->descent += thick;
24190 }
24191 else
24192 thick = -thick;
24193
24194 if (it->start_of_box_run_p)
24195 it->pixel_width += thick;
24196 if (it->end_of_box_run_p)
24197 it->pixel_width += thick;
24198 }
24199
24200 /* If face has an overline, add the height of the overline
24201 (1 pixel) and a 1 pixel margin to the character height. */
24202 if (face->overline_p)
24203 it->ascent += overline_margin;
24204
24205 if (it->constrain_row_ascent_descent_p)
24206 {
24207 if (it->ascent > it->max_ascent)
24208 it->ascent = it->max_ascent;
24209 if (it->descent > it->max_descent)
24210 it->descent = it->max_descent;
24211 }
24212
24213 take_vertical_position_into_account (it);
24214
24215 /* If we have to actually produce glyphs, do it. */
24216 if (it->glyph_row)
24217 {
24218 if (stretched_p)
24219 {
24220 /* Translate a space with a `space-width' property
24221 into a stretch glyph. */
24222 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24223 / FONT_HEIGHT (font));
24224 append_stretch_glyph (it, it->object, it->pixel_width,
24225 it->ascent + it->descent, ascent);
24226 }
24227 else
24228 append_glyph (it);
24229
24230 /* If characters with lbearing or rbearing are displayed
24231 in this line, record that fact in a flag of the
24232 glyph row. This is used to optimize X output code. */
24233 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24234 it->glyph_row->contains_overlapping_glyphs_p = 1;
24235 }
24236 if (! stretched_p && it->pixel_width == 0)
24237 /* We assure that all visible glyphs have at least 1-pixel
24238 width. */
24239 it->pixel_width = 1;
24240 }
24241 else if (it->char_to_display == '\n')
24242 {
24243 /* A newline has no width, but we need the height of the
24244 line. But if previous part of the line sets a height,
24245 don't increase that height */
24246
24247 Lisp_Object height;
24248 Lisp_Object total_height = Qnil;
24249
24250 it->override_ascent = -1;
24251 it->pixel_width = 0;
24252 it->nglyphs = 0;
24253
24254 height = get_it_property (it, Qline_height);
24255 /* Split (line-height total-height) list */
24256 if (CONSP (height)
24257 && CONSP (XCDR (height))
24258 && NILP (XCDR (XCDR (height))))
24259 {
24260 total_height = XCAR (XCDR (height));
24261 height = XCAR (height);
24262 }
24263 height = calc_line_height_property (it, height, font, boff, 1);
24264
24265 if (it->override_ascent >= 0)
24266 {
24267 it->ascent = it->override_ascent;
24268 it->descent = it->override_descent;
24269 boff = it->override_boff;
24270 }
24271 else
24272 {
24273 it->ascent = FONT_BASE (font) + boff;
24274 it->descent = FONT_DESCENT (font) - boff;
24275 }
24276
24277 if (EQ (height, Qt))
24278 {
24279 if (it->descent > it->max_descent)
24280 {
24281 it->ascent += it->descent - it->max_descent;
24282 it->descent = it->max_descent;
24283 }
24284 if (it->ascent > it->max_ascent)
24285 {
24286 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24287 it->ascent = it->max_ascent;
24288 }
24289 it->phys_ascent = min (it->phys_ascent, it->ascent);
24290 it->phys_descent = min (it->phys_descent, it->descent);
24291 it->constrain_row_ascent_descent_p = 1;
24292 extra_line_spacing = 0;
24293 }
24294 else
24295 {
24296 Lisp_Object spacing;
24297
24298 it->phys_ascent = it->ascent;
24299 it->phys_descent = it->descent;
24300
24301 if ((it->max_ascent > 0 || it->max_descent > 0)
24302 && face->box != FACE_NO_BOX
24303 && face->box_line_width > 0)
24304 {
24305 it->ascent += face->box_line_width;
24306 it->descent += face->box_line_width;
24307 }
24308 if (!NILP (height)
24309 && XINT (height) > it->ascent + it->descent)
24310 it->ascent = XINT (height) - it->descent;
24311
24312 if (!NILP (total_height))
24313 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24314 else
24315 {
24316 spacing = get_it_property (it, Qline_spacing);
24317 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24318 }
24319 if (INTEGERP (spacing))
24320 {
24321 extra_line_spacing = XINT (spacing);
24322 if (!NILP (total_height))
24323 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24324 }
24325 }
24326 }
24327 else /* i.e. (it->char_to_display == '\t') */
24328 {
24329 if (font->space_width > 0)
24330 {
24331 int tab_width = it->tab_width * font->space_width;
24332 int x = it->current_x + it->continuation_lines_width;
24333 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24334
24335 /* If the distance from the current position to the next tab
24336 stop is less than a space character width, use the
24337 tab stop after that. */
24338 if (next_tab_x - x < font->space_width)
24339 next_tab_x += tab_width;
24340
24341 it->pixel_width = next_tab_x - x;
24342 it->nglyphs = 1;
24343 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24344 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24345
24346 if (it->glyph_row)
24347 {
24348 append_stretch_glyph (it, it->object, it->pixel_width,
24349 it->ascent + it->descent, it->ascent);
24350 }
24351 }
24352 else
24353 {
24354 it->pixel_width = 0;
24355 it->nglyphs = 1;
24356 }
24357 }
24358 }
24359 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24360 {
24361 /* A static composition.
24362
24363 Note: A composition is represented as one glyph in the
24364 glyph matrix. There are no padding glyphs.
24365
24366 Important note: pixel_width, ascent, and descent are the
24367 values of what is drawn by draw_glyphs (i.e. the values of
24368 the overall glyphs composed). */
24369 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24370 int boff; /* baseline offset */
24371 struct composition *cmp = composition_table[it->cmp_it.id];
24372 int glyph_len = cmp->glyph_len;
24373 struct font *font = face->font;
24374
24375 it->nglyphs = 1;
24376
24377 /* If we have not yet calculated pixel size data of glyphs of
24378 the composition for the current face font, calculate them
24379 now. Theoretically, we have to check all fonts for the
24380 glyphs, but that requires much time and memory space. So,
24381 here we check only the font of the first glyph. This may
24382 lead to incorrect display, but it's very rare, and C-l
24383 (recenter-top-bottom) can correct the display anyway. */
24384 if (! cmp->font || cmp->font != font)
24385 {
24386 /* Ascent and descent of the font of the first character
24387 of this composition (adjusted by baseline offset).
24388 Ascent and descent of overall glyphs should not be less
24389 than these, respectively. */
24390 int font_ascent, font_descent, font_height;
24391 /* Bounding box of the overall glyphs. */
24392 int leftmost, rightmost, lowest, highest;
24393 int lbearing, rbearing;
24394 int i, width, ascent, descent;
24395 int left_padded = 0, right_padded = 0;
24396 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24397 XChar2b char2b;
24398 struct font_metrics *pcm;
24399 int font_not_found_p;
24400 EMACS_INT pos;
24401
24402 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24403 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24404 break;
24405 if (glyph_len < cmp->glyph_len)
24406 right_padded = 1;
24407 for (i = 0; i < glyph_len; i++)
24408 {
24409 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24410 break;
24411 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24412 }
24413 if (i > 0)
24414 left_padded = 1;
24415
24416 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24417 : IT_CHARPOS (*it));
24418 /* If no suitable font is found, use the default font. */
24419 font_not_found_p = font == NULL;
24420 if (font_not_found_p)
24421 {
24422 face = face->ascii_face;
24423 font = face->font;
24424 }
24425 boff = font->baseline_offset;
24426 if (font->vertical_centering)
24427 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24428 font_ascent = FONT_BASE (font) + boff;
24429 font_descent = FONT_DESCENT (font) - boff;
24430 font_height = FONT_HEIGHT (font);
24431
24432 cmp->font = (void *) font;
24433
24434 pcm = NULL;
24435 if (! font_not_found_p)
24436 {
24437 get_char_face_and_encoding (it->f, c, it->face_id,
24438 &char2b, 0);
24439 pcm = get_per_char_metric (font, &char2b);
24440 }
24441
24442 /* Initialize the bounding box. */
24443 if (pcm)
24444 {
24445 width = cmp->glyph_len > 0 ? pcm->width : 0;
24446 ascent = pcm->ascent;
24447 descent = pcm->descent;
24448 lbearing = pcm->lbearing;
24449 rbearing = pcm->rbearing;
24450 }
24451 else
24452 {
24453 width = cmp->glyph_len > 0 ? font->space_width : 0;
24454 ascent = FONT_BASE (font);
24455 descent = FONT_DESCENT (font);
24456 lbearing = 0;
24457 rbearing = width;
24458 }
24459
24460 rightmost = width;
24461 leftmost = 0;
24462 lowest = - descent + boff;
24463 highest = ascent + boff;
24464
24465 if (! font_not_found_p
24466 && font->default_ascent
24467 && CHAR_TABLE_P (Vuse_default_ascent)
24468 && !NILP (Faref (Vuse_default_ascent,
24469 make_number (it->char_to_display))))
24470 highest = font->default_ascent + boff;
24471
24472 /* Draw the first glyph at the normal position. It may be
24473 shifted to right later if some other glyphs are drawn
24474 at the left. */
24475 cmp->offsets[i * 2] = 0;
24476 cmp->offsets[i * 2 + 1] = boff;
24477 cmp->lbearing = lbearing;
24478 cmp->rbearing = rbearing;
24479
24480 /* Set cmp->offsets for the remaining glyphs. */
24481 for (i++; i < glyph_len; i++)
24482 {
24483 int left, right, btm, top;
24484 int ch = COMPOSITION_GLYPH (cmp, i);
24485 int face_id;
24486 struct face *this_face;
24487
24488 if (ch == '\t')
24489 ch = ' ';
24490 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24491 this_face = FACE_FROM_ID (it->f, face_id);
24492 font = this_face->font;
24493
24494 if (font == NULL)
24495 pcm = NULL;
24496 else
24497 {
24498 get_char_face_and_encoding (it->f, ch, face_id,
24499 &char2b, 0);
24500 pcm = get_per_char_metric (font, &char2b);
24501 }
24502 if (! pcm)
24503 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24504 else
24505 {
24506 width = pcm->width;
24507 ascent = pcm->ascent;
24508 descent = pcm->descent;
24509 lbearing = pcm->lbearing;
24510 rbearing = pcm->rbearing;
24511 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24512 {
24513 /* Relative composition with or without
24514 alternate chars. */
24515 left = (leftmost + rightmost - width) / 2;
24516 btm = - descent + boff;
24517 if (font->relative_compose
24518 && (! CHAR_TABLE_P (Vignore_relative_composition)
24519 || NILP (Faref (Vignore_relative_composition,
24520 make_number (ch)))))
24521 {
24522
24523 if (- descent >= font->relative_compose)
24524 /* One extra pixel between two glyphs. */
24525 btm = highest + 1;
24526 else if (ascent <= 0)
24527 /* One extra pixel between two glyphs. */
24528 btm = lowest - 1 - ascent - descent;
24529 }
24530 }
24531 else
24532 {
24533 /* A composition rule is specified by an integer
24534 value that encodes global and new reference
24535 points (GREF and NREF). GREF and NREF are
24536 specified by numbers as below:
24537
24538 0---1---2 -- ascent
24539 | |
24540 | |
24541 | |
24542 9--10--11 -- center
24543 | |
24544 ---3---4---5--- baseline
24545 | |
24546 6---7---8 -- descent
24547 */
24548 int rule = COMPOSITION_RULE (cmp, i);
24549 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24550
24551 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24552 grefx = gref % 3, nrefx = nref % 3;
24553 grefy = gref / 3, nrefy = nref / 3;
24554 if (xoff)
24555 xoff = font_height * (xoff - 128) / 256;
24556 if (yoff)
24557 yoff = font_height * (yoff - 128) / 256;
24558
24559 left = (leftmost
24560 + grefx * (rightmost - leftmost) / 2
24561 - nrefx * width / 2
24562 + xoff);
24563
24564 btm = ((grefy == 0 ? highest
24565 : grefy == 1 ? 0
24566 : grefy == 2 ? lowest
24567 : (highest + lowest) / 2)
24568 - (nrefy == 0 ? ascent + descent
24569 : nrefy == 1 ? descent - boff
24570 : nrefy == 2 ? 0
24571 : (ascent + descent) / 2)
24572 + yoff);
24573 }
24574
24575 cmp->offsets[i * 2] = left;
24576 cmp->offsets[i * 2 + 1] = btm + descent;
24577
24578 /* Update the bounding box of the overall glyphs. */
24579 if (width > 0)
24580 {
24581 right = left + width;
24582 if (left < leftmost)
24583 leftmost = left;
24584 if (right > rightmost)
24585 rightmost = right;
24586 }
24587 top = btm + descent + ascent;
24588 if (top > highest)
24589 highest = top;
24590 if (btm < lowest)
24591 lowest = btm;
24592
24593 if (cmp->lbearing > left + lbearing)
24594 cmp->lbearing = left + lbearing;
24595 if (cmp->rbearing < left + rbearing)
24596 cmp->rbearing = left + rbearing;
24597 }
24598 }
24599
24600 /* If there are glyphs whose x-offsets are negative,
24601 shift all glyphs to the right and make all x-offsets
24602 non-negative. */
24603 if (leftmost < 0)
24604 {
24605 for (i = 0; i < cmp->glyph_len; i++)
24606 cmp->offsets[i * 2] -= leftmost;
24607 rightmost -= leftmost;
24608 cmp->lbearing -= leftmost;
24609 cmp->rbearing -= leftmost;
24610 }
24611
24612 if (left_padded && cmp->lbearing < 0)
24613 {
24614 for (i = 0; i < cmp->glyph_len; i++)
24615 cmp->offsets[i * 2] -= cmp->lbearing;
24616 rightmost -= cmp->lbearing;
24617 cmp->rbearing -= cmp->lbearing;
24618 cmp->lbearing = 0;
24619 }
24620 if (right_padded && rightmost < cmp->rbearing)
24621 {
24622 rightmost = cmp->rbearing;
24623 }
24624
24625 cmp->pixel_width = rightmost;
24626 cmp->ascent = highest;
24627 cmp->descent = - lowest;
24628 if (cmp->ascent < font_ascent)
24629 cmp->ascent = font_ascent;
24630 if (cmp->descent < font_descent)
24631 cmp->descent = font_descent;
24632 }
24633
24634 if (it->glyph_row
24635 && (cmp->lbearing < 0
24636 || cmp->rbearing > cmp->pixel_width))
24637 it->glyph_row->contains_overlapping_glyphs_p = 1;
24638
24639 it->pixel_width = cmp->pixel_width;
24640 it->ascent = it->phys_ascent = cmp->ascent;
24641 it->descent = it->phys_descent = cmp->descent;
24642 if (face->box != FACE_NO_BOX)
24643 {
24644 int thick = face->box_line_width;
24645
24646 if (thick > 0)
24647 {
24648 it->ascent += thick;
24649 it->descent += thick;
24650 }
24651 else
24652 thick = - thick;
24653
24654 if (it->start_of_box_run_p)
24655 it->pixel_width += thick;
24656 if (it->end_of_box_run_p)
24657 it->pixel_width += thick;
24658 }
24659
24660 /* If face has an overline, add the height of the overline
24661 (1 pixel) and a 1 pixel margin to the character height. */
24662 if (face->overline_p)
24663 it->ascent += overline_margin;
24664
24665 take_vertical_position_into_account (it);
24666 if (it->ascent < 0)
24667 it->ascent = 0;
24668 if (it->descent < 0)
24669 it->descent = 0;
24670
24671 if (it->glyph_row && cmp->glyph_len > 0)
24672 append_composite_glyph (it);
24673 }
24674 else if (it->what == IT_COMPOSITION)
24675 {
24676 /* A dynamic (automatic) composition. */
24677 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24678 Lisp_Object gstring;
24679 struct font_metrics metrics;
24680
24681 it->nglyphs = 1;
24682
24683 gstring = composition_gstring_from_id (it->cmp_it.id);
24684 it->pixel_width
24685 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24686 &metrics);
24687 if (it->glyph_row
24688 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24689 it->glyph_row->contains_overlapping_glyphs_p = 1;
24690 it->ascent = it->phys_ascent = metrics.ascent;
24691 it->descent = it->phys_descent = metrics.descent;
24692 if (face->box != FACE_NO_BOX)
24693 {
24694 int thick = face->box_line_width;
24695
24696 if (thick > 0)
24697 {
24698 it->ascent += thick;
24699 it->descent += thick;
24700 }
24701 else
24702 thick = - thick;
24703
24704 if (it->start_of_box_run_p)
24705 it->pixel_width += thick;
24706 if (it->end_of_box_run_p)
24707 it->pixel_width += thick;
24708 }
24709 /* If face has an overline, add the height of the overline
24710 (1 pixel) and a 1 pixel margin to the character height. */
24711 if (face->overline_p)
24712 it->ascent += overline_margin;
24713 take_vertical_position_into_account (it);
24714 if (it->ascent < 0)
24715 it->ascent = 0;
24716 if (it->descent < 0)
24717 it->descent = 0;
24718
24719 if (it->glyph_row)
24720 append_composite_glyph (it);
24721 }
24722 else if (it->what == IT_GLYPHLESS)
24723 produce_glyphless_glyph (it, 0, Qnil);
24724 else if (it->what == IT_IMAGE)
24725 produce_image_glyph (it);
24726 else if (it->what == IT_STRETCH)
24727 produce_stretch_glyph (it);
24728
24729 done:
24730 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24731 because this isn't true for images with `:ascent 100'. */
24732 xassert (it->ascent >= 0 && it->descent >= 0);
24733 if (it->area == TEXT_AREA)
24734 it->current_x += it->pixel_width;
24735
24736 if (extra_line_spacing > 0)
24737 {
24738 it->descent += extra_line_spacing;
24739 if (extra_line_spacing > it->max_extra_line_spacing)
24740 it->max_extra_line_spacing = extra_line_spacing;
24741 }
24742
24743 it->max_ascent = max (it->max_ascent, it->ascent);
24744 it->max_descent = max (it->max_descent, it->descent);
24745 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24746 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24747 }
24748
24749 /* EXPORT for RIF:
24750 Output LEN glyphs starting at START at the nominal cursor position.
24751 Advance the nominal cursor over the text. The global variable
24752 updated_window contains the window being updated, updated_row is
24753 the glyph row being updated, and updated_area is the area of that
24754 row being updated. */
24755
24756 void
24757 x_write_glyphs (struct glyph *start, int len)
24758 {
24759 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24760
24761 xassert (updated_window && updated_row);
24762 /* When the window is hscrolled, cursor hpos can legitimately be out
24763 of bounds, but we draw the cursor at the corresponding window
24764 margin in that case. */
24765 if (!updated_row->reversed_p && chpos < 0)
24766 chpos = 0;
24767 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24768 chpos = updated_row->used[TEXT_AREA] - 1;
24769
24770 BLOCK_INPUT;
24771
24772 /* Write glyphs. */
24773
24774 hpos = start - updated_row->glyphs[updated_area];
24775 x = draw_glyphs (updated_window, output_cursor.x,
24776 updated_row, updated_area,
24777 hpos, hpos + len,
24778 DRAW_NORMAL_TEXT, 0);
24779
24780 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24781 if (updated_area == TEXT_AREA
24782 && updated_window->phys_cursor_on_p
24783 && updated_window->phys_cursor.vpos == output_cursor.vpos
24784 && chpos >= hpos
24785 && chpos < hpos + len)
24786 updated_window->phys_cursor_on_p = 0;
24787
24788 UNBLOCK_INPUT;
24789
24790 /* Advance the output cursor. */
24791 output_cursor.hpos += len;
24792 output_cursor.x = x;
24793 }
24794
24795
24796 /* EXPORT for RIF:
24797 Insert LEN glyphs from START at the nominal cursor position. */
24798
24799 void
24800 x_insert_glyphs (struct glyph *start, int len)
24801 {
24802 struct frame *f;
24803 struct window *w;
24804 int line_height, shift_by_width, shifted_region_width;
24805 struct glyph_row *row;
24806 struct glyph *glyph;
24807 int frame_x, frame_y;
24808 EMACS_INT hpos;
24809
24810 xassert (updated_window && updated_row);
24811 BLOCK_INPUT;
24812 w = updated_window;
24813 f = XFRAME (WINDOW_FRAME (w));
24814
24815 /* Get the height of the line we are in. */
24816 row = updated_row;
24817 line_height = row->height;
24818
24819 /* Get the width of the glyphs to insert. */
24820 shift_by_width = 0;
24821 for (glyph = start; glyph < start + len; ++glyph)
24822 shift_by_width += glyph->pixel_width;
24823
24824 /* Get the width of the region to shift right. */
24825 shifted_region_width = (window_box_width (w, updated_area)
24826 - output_cursor.x
24827 - shift_by_width);
24828
24829 /* Shift right. */
24830 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24831 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24832
24833 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24834 line_height, shift_by_width);
24835
24836 /* Write the glyphs. */
24837 hpos = start - row->glyphs[updated_area];
24838 draw_glyphs (w, output_cursor.x, row, updated_area,
24839 hpos, hpos + len,
24840 DRAW_NORMAL_TEXT, 0);
24841
24842 /* Advance the output cursor. */
24843 output_cursor.hpos += len;
24844 output_cursor.x += shift_by_width;
24845 UNBLOCK_INPUT;
24846 }
24847
24848
24849 /* EXPORT for RIF:
24850 Erase the current text line from the nominal cursor position
24851 (inclusive) to pixel column TO_X (exclusive). The idea is that
24852 everything from TO_X onward is already erased.
24853
24854 TO_X is a pixel position relative to updated_area of
24855 updated_window. TO_X == -1 means clear to the end of this area. */
24856
24857 void
24858 x_clear_end_of_line (int to_x)
24859 {
24860 struct frame *f;
24861 struct window *w = updated_window;
24862 int max_x, min_y, max_y;
24863 int from_x, from_y, to_y;
24864
24865 xassert (updated_window && updated_row);
24866 f = XFRAME (w->frame);
24867
24868 if (updated_row->full_width_p)
24869 max_x = WINDOW_TOTAL_WIDTH (w);
24870 else
24871 max_x = window_box_width (w, updated_area);
24872 max_y = window_text_bottom_y (w);
24873
24874 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24875 of window. For TO_X > 0, truncate to end of drawing area. */
24876 if (to_x == 0)
24877 return;
24878 else if (to_x < 0)
24879 to_x = max_x;
24880 else
24881 to_x = min (to_x, max_x);
24882
24883 to_y = min (max_y, output_cursor.y + updated_row->height);
24884
24885 /* Notice if the cursor will be cleared by this operation. */
24886 if (!updated_row->full_width_p)
24887 notice_overwritten_cursor (w, updated_area,
24888 output_cursor.x, -1,
24889 updated_row->y,
24890 MATRIX_ROW_BOTTOM_Y (updated_row));
24891
24892 from_x = output_cursor.x;
24893
24894 /* Translate to frame coordinates. */
24895 if (updated_row->full_width_p)
24896 {
24897 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24898 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24899 }
24900 else
24901 {
24902 int area_left = window_box_left (w, updated_area);
24903 from_x += area_left;
24904 to_x += area_left;
24905 }
24906
24907 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24908 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24909 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24910
24911 /* Prevent inadvertently clearing to end of the X window. */
24912 if (to_x > from_x && to_y > from_y)
24913 {
24914 BLOCK_INPUT;
24915 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24916 to_x - from_x, to_y - from_y);
24917 UNBLOCK_INPUT;
24918 }
24919 }
24920
24921 #endif /* HAVE_WINDOW_SYSTEM */
24922
24923
24924 \f
24925 /***********************************************************************
24926 Cursor types
24927 ***********************************************************************/
24928
24929 /* Value is the internal representation of the specified cursor type
24930 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24931 of the bar cursor. */
24932
24933 static enum text_cursor_kinds
24934 get_specified_cursor_type (Lisp_Object arg, int *width)
24935 {
24936 enum text_cursor_kinds type;
24937
24938 if (NILP (arg))
24939 return NO_CURSOR;
24940
24941 if (EQ (arg, Qbox))
24942 return FILLED_BOX_CURSOR;
24943
24944 if (EQ (arg, Qhollow))
24945 return HOLLOW_BOX_CURSOR;
24946
24947 if (EQ (arg, Qbar))
24948 {
24949 *width = 2;
24950 return BAR_CURSOR;
24951 }
24952
24953 if (CONSP (arg)
24954 && EQ (XCAR (arg), Qbar)
24955 && INTEGERP (XCDR (arg))
24956 && XINT (XCDR (arg)) >= 0)
24957 {
24958 *width = XINT (XCDR (arg));
24959 return BAR_CURSOR;
24960 }
24961
24962 if (EQ (arg, Qhbar))
24963 {
24964 *width = 2;
24965 return HBAR_CURSOR;
24966 }
24967
24968 if (CONSP (arg)
24969 && EQ (XCAR (arg), Qhbar)
24970 && INTEGERP (XCDR (arg))
24971 && XINT (XCDR (arg)) >= 0)
24972 {
24973 *width = XINT (XCDR (arg));
24974 return HBAR_CURSOR;
24975 }
24976
24977 /* Treat anything unknown as "hollow box cursor".
24978 It was bad to signal an error; people have trouble fixing
24979 .Xdefaults with Emacs, when it has something bad in it. */
24980 type = HOLLOW_BOX_CURSOR;
24981
24982 return type;
24983 }
24984
24985 /* Set the default cursor types for specified frame. */
24986 void
24987 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24988 {
24989 int width = 1;
24990 Lisp_Object tem;
24991
24992 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24993 FRAME_CURSOR_WIDTH (f) = width;
24994
24995 /* By default, set up the blink-off state depending on the on-state. */
24996
24997 tem = Fassoc (arg, Vblink_cursor_alist);
24998 if (!NILP (tem))
24999 {
25000 FRAME_BLINK_OFF_CURSOR (f)
25001 = get_specified_cursor_type (XCDR (tem), &width);
25002 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25003 }
25004 else
25005 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25006 }
25007
25008
25009 #ifdef HAVE_WINDOW_SYSTEM
25010
25011 /* Return the cursor we want to be displayed in window W. Return
25012 width of bar/hbar cursor through WIDTH arg. Return with
25013 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25014 (i.e. if the `system caret' should track this cursor).
25015
25016 In a mini-buffer window, we want the cursor only to appear if we
25017 are reading input from this window. For the selected window, we
25018 want the cursor type given by the frame parameter or buffer local
25019 setting of cursor-type. If explicitly marked off, draw no cursor.
25020 In all other cases, we want a hollow box cursor. */
25021
25022 static enum text_cursor_kinds
25023 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25024 int *active_cursor)
25025 {
25026 struct frame *f = XFRAME (w->frame);
25027 struct buffer *b = XBUFFER (w->buffer);
25028 int cursor_type = DEFAULT_CURSOR;
25029 Lisp_Object alt_cursor;
25030 int non_selected = 0;
25031
25032 *active_cursor = 1;
25033
25034 /* Echo area */
25035 if (cursor_in_echo_area
25036 && FRAME_HAS_MINIBUF_P (f)
25037 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25038 {
25039 if (w == XWINDOW (echo_area_window))
25040 {
25041 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25042 {
25043 *width = FRAME_CURSOR_WIDTH (f);
25044 return FRAME_DESIRED_CURSOR (f);
25045 }
25046 else
25047 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25048 }
25049
25050 *active_cursor = 0;
25051 non_selected = 1;
25052 }
25053
25054 /* Detect a nonselected window or nonselected frame. */
25055 else if (w != XWINDOW (f->selected_window)
25056 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25057 {
25058 *active_cursor = 0;
25059
25060 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25061 return NO_CURSOR;
25062
25063 non_selected = 1;
25064 }
25065
25066 /* Never display a cursor in a window in which cursor-type is nil. */
25067 if (NILP (BVAR (b, cursor_type)))
25068 return NO_CURSOR;
25069
25070 /* Get the normal cursor type for this window. */
25071 if (EQ (BVAR (b, cursor_type), Qt))
25072 {
25073 cursor_type = FRAME_DESIRED_CURSOR (f);
25074 *width = FRAME_CURSOR_WIDTH (f);
25075 }
25076 else
25077 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25078
25079 /* Use cursor-in-non-selected-windows instead
25080 for non-selected window or frame. */
25081 if (non_selected)
25082 {
25083 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25084 if (!EQ (Qt, alt_cursor))
25085 return get_specified_cursor_type (alt_cursor, width);
25086 /* t means modify the normal cursor type. */
25087 if (cursor_type == FILLED_BOX_CURSOR)
25088 cursor_type = HOLLOW_BOX_CURSOR;
25089 else if (cursor_type == BAR_CURSOR && *width > 1)
25090 --*width;
25091 return cursor_type;
25092 }
25093
25094 /* Use normal cursor if not blinked off. */
25095 if (!w->cursor_off_p)
25096 {
25097 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25098 {
25099 if (cursor_type == FILLED_BOX_CURSOR)
25100 {
25101 /* Using a block cursor on large images can be very annoying.
25102 So use a hollow cursor for "large" images.
25103 If image is not transparent (no mask), also use hollow cursor. */
25104 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25105 if (img != NULL && IMAGEP (img->spec))
25106 {
25107 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25108 where N = size of default frame font size.
25109 This should cover most of the "tiny" icons people may use. */
25110 if (!img->mask
25111 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25112 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25113 cursor_type = HOLLOW_BOX_CURSOR;
25114 }
25115 }
25116 else if (cursor_type != NO_CURSOR)
25117 {
25118 /* Display current only supports BOX and HOLLOW cursors for images.
25119 So for now, unconditionally use a HOLLOW cursor when cursor is
25120 not a solid box cursor. */
25121 cursor_type = HOLLOW_BOX_CURSOR;
25122 }
25123 }
25124 return cursor_type;
25125 }
25126
25127 /* Cursor is blinked off, so determine how to "toggle" it. */
25128
25129 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25130 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25131 return get_specified_cursor_type (XCDR (alt_cursor), width);
25132
25133 /* Then see if frame has specified a specific blink off cursor type. */
25134 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25135 {
25136 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25137 return FRAME_BLINK_OFF_CURSOR (f);
25138 }
25139
25140 #if 0
25141 /* Some people liked having a permanently visible blinking cursor,
25142 while others had very strong opinions against it. So it was
25143 decided to remove it. KFS 2003-09-03 */
25144
25145 /* Finally perform built-in cursor blinking:
25146 filled box <-> hollow box
25147 wide [h]bar <-> narrow [h]bar
25148 narrow [h]bar <-> no cursor
25149 other type <-> no cursor */
25150
25151 if (cursor_type == FILLED_BOX_CURSOR)
25152 return HOLLOW_BOX_CURSOR;
25153
25154 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25155 {
25156 *width = 1;
25157 return cursor_type;
25158 }
25159 #endif
25160
25161 return NO_CURSOR;
25162 }
25163
25164
25165 /* Notice when the text cursor of window W has been completely
25166 overwritten by a drawing operation that outputs glyphs in AREA
25167 starting at X0 and ending at X1 in the line starting at Y0 and
25168 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25169 the rest of the line after X0 has been written. Y coordinates
25170 are window-relative. */
25171
25172 static void
25173 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25174 int x0, int x1, int y0, int y1)
25175 {
25176 int cx0, cx1, cy0, cy1;
25177 struct glyph_row *row;
25178
25179 if (!w->phys_cursor_on_p)
25180 return;
25181 if (area != TEXT_AREA)
25182 return;
25183
25184 if (w->phys_cursor.vpos < 0
25185 || w->phys_cursor.vpos >= w->current_matrix->nrows
25186 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25187 !(row->enabled_p && row->displays_text_p)))
25188 return;
25189
25190 if (row->cursor_in_fringe_p)
25191 {
25192 row->cursor_in_fringe_p = 0;
25193 draw_fringe_bitmap (w, row, row->reversed_p);
25194 w->phys_cursor_on_p = 0;
25195 return;
25196 }
25197
25198 cx0 = w->phys_cursor.x;
25199 cx1 = cx0 + w->phys_cursor_width;
25200 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25201 return;
25202
25203 /* The cursor image will be completely removed from the
25204 screen if the output area intersects the cursor area in
25205 y-direction. When we draw in [y0 y1[, and some part of
25206 the cursor is at y < y0, that part must have been drawn
25207 before. When scrolling, the cursor is erased before
25208 actually scrolling, so we don't come here. When not
25209 scrolling, the rows above the old cursor row must have
25210 changed, and in this case these rows must have written
25211 over the cursor image.
25212
25213 Likewise if part of the cursor is below y1, with the
25214 exception of the cursor being in the first blank row at
25215 the buffer and window end because update_text_area
25216 doesn't draw that row. (Except when it does, but
25217 that's handled in update_text_area.) */
25218
25219 cy0 = w->phys_cursor.y;
25220 cy1 = cy0 + w->phys_cursor_height;
25221 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25222 return;
25223
25224 w->phys_cursor_on_p = 0;
25225 }
25226
25227 #endif /* HAVE_WINDOW_SYSTEM */
25228
25229 \f
25230 /************************************************************************
25231 Mouse Face
25232 ************************************************************************/
25233
25234 #ifdef HAVE_WINDOW_SYSTEM
25235
25236 /* EXPORT for RIF:
25237 Fix the display of area AREA of overlapping row ROW in window W
25238 with respect to the overlapping part OVERLAPS. */
25239
25240 void
25241 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25242 enum glyph_row_area area, int overlaps)
25243 {
25244 int i, x;
25245
25246 BLOCK_INPUT;
25247
25248 x = 0;
25249 for (i = 0; i < row->used[area];)
25250 {
25251 if (row->glyphs[area][i].overlaps_vertically_p)
25252 {
25253 int start = i, start_x = x;
25254
25255 do
25256 {
25257 x += row->glyphs[area][i].pixel_width;
25258 ++i;
25259 }
25260 while (i < row->used[area]
25261 && row->glyphs[area][i].overlaps_vertically_p);
25262
25263 draw_glyphs (w, start_x, row, area,
25264 start, i,
25265 DRAW_NORMAL_TEXT, overlaps);
25266 }
25267 else
25268 {
25269 x += row->glyphs[area][i].pixel_width;
25270 ++i;
25271 }
25272 }
25273
25274 UNBLOCK_INPUT;
25275 }
25276
25277
25278 /* EXPORT:
25279 Draw the cursor glyph of window W in glyph row ROW. See the
25280 comment of draw_glyphs for the meaning of HL. */
25281
25282 void
25283 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25284 enum draw_glyphs_face hl)
25285 {
25286 /* If cursor hpos is out of bounds, don't draw garbage. This can
25287 happen in mini-buffer windows when switching between echo area
25288 glyphs and mini-buffer. */
25289 if ((row->reversed_p
25290 ? (w->phys_cursor.hpos >= 0)
25291 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25292 {
25293 int on_p = w->phys_cursor_on_p;
25294 int x1;
25295 int hpos = w->phys_cursor.hpos;
25296
25297 /* When the window is hscrolled, cursor hpos can legitimately be
25298 out of bounds, but we draw the cursor at the corresponding
25299 window margin in that case. */
25300 if (!row->reversed_p && hpos < 0)
25301 hpos = 0;
25302 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25303 hpos = row->used[TEXT_AREA] - 1;
25304
25305 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25306 hl, 0);
25307 w->phys_cursor_on_p = on_p;
25308
25309 if (hl == DRAW_CURSOR)
25310 w->phys_cursor_width = x1 - w->phys_cursor.x;
25311 /* When we erase the cursor, and ROW is overlapped by other
25312 rows, make sure that these overlapping parts of other rows
25313 are redrawn. */
25314 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25315 {
25316 w->phys_cursor_width = x1 - w->phys_cursor.x;
25317
25318 if (row > w->current_matrix->rows
25319 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25320 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25321 OVERLAPS_ERASED_CURSOR);
25322
25323 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25324 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25325 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25326 OVERLAPS_ERASED_CURSOR);
25327 }
25328 }
25329 }
25330
25331
25332 /* EXPORT:
25333 Erase the image of a cursor of window W from the screen. */
25334
25335 void
25336 erase_phys_cursor (struct window *w)
25337 {
25338 struct frame *f = XFRAME (w->frame);
25339 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25340 int hpos = w->phys_cursor.hpos;
25341 int vpos = w->phys_cursor.vpos;
25342 int mouse_face_here_p = 0;
25343 struct glyph_matrix *active_glyphs = w->current_matrix;
25344 struct glyph_row *cursor_row;
25345 struct glyph *cursor_glyph;
25346 enum draw_glyphs_face hl;
25347
25348 /* No cursor displayed or row invalidated => nothing to do on the
25349 screen. */
25350 if (w->phys_cursor_type == NO_CURSOR)
25351 goto mark_cursor_off;
25352
25353 /* VPOS >= active_glyphs->nrows means that window has been resized.
25354 Don't bother to erase the cursor. */
25355 if (vpos >= active_glyphs->nrows)
25356 goto mark_cursor_off;
25357
25358 /* If row containing cursor is marked invalid, there is nothing we
25359 can do. */
25360 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25361 if (!cursor_row->enabled_p)
25362 goto mark_cursor_off;
25363
25364 /* If line spacing is > 0, old cursor may only be partially visible in
25365 window after split-window. So adjust visible height. */
25366 cursor_row->visible_height = min (cursor_row->visible_height,
25367 window_text_bottom_y (w) - cursor_row->y);
25368
25369 /* If row is completely invisible, don't attempt to delete a cursor which
25370 isn't there. This can happen if cursor is at top of a window, and
25371 we switch to a buffer with a header line in that window. */
25372 if (cursor_row->visible_height <= 0)
25373 goto mark_cursor_off;
25374
25375 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25376 if (cursor_row->cursor_in_fringe_p)
25377 {
25378 cursor_row->cursor_in_fringe_p = 0;
25379 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25380 goto mark_cursor_off;
25381 }
25382
25383 /* This can happen when the new row is shorter than the old one.
25384 In this case, either draw_glyphs or clear_end_of_line
25385 should have cleared the cursor. Note that we wouldn't be
25386 able to erase the cursor in this case because we don't have a
25387 cursor glyph at hand. */
25388 if ((cursor_row->reversed_p
25389 ? (w->phys_cursor.hpos < 0)
25390 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25391 goto mark_cursor_off;
25392
25393 /* When the window is hscrolled, cursor hpos can legitimately be out
25394 of bounds, but we draw the cursor at the corresponding window
25395 margin in that case. */
25396 if (!cursor_row->reversed_p && hpos < 0)
25397 hpos = 0;
25398 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25399 hpos = cursor_row->used[TEXT_AREA] - 1;
25400
25401 /* If the cursor is in the mouse face area, redisplay that when
25402 we clear the cursor. */
25403 if (! NILP (hlinfo->mouse_face_window)
25404 && coords_in_mouse_face_p (w, hpos, vpos)
25405 /* Don't redraw the cursor's spot in mouse face if it is at the
25406 end of a line (on a newline). The cursor appears there, but
25407 mouse highlighting does not. */
25408 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25409 mouse_face_here_p = 1;
25410
25411 /* Maybe clear the display under the cursor. */
25412 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25413 {
25414 int x, y, left_x;
25415 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25416 int width;
25417
25418 cursor_glyph = get_phys_cursor_glyph (w);
25419 if (cursor_glyph == NULL)
25420 goto mark_cursor_off;
25421
25422 width = cursor_glyph->pixel_width;
25423 left_x = window_box_left_offset (w, TEXT_AREA);
25424 x = w->phys_cursor.x;
25425 if (x < left_x)
25426 width -= left_x - x;
25427 width = min (width, window_box_width (w, TEXT_AREA) - x);
25428 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25429 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25430
25431 if (width > 0)
25432 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25433 }
25434
25435 /* Erase the cursor by redrawing the character underneath it. */
25436 if (mouse_face_here_p)
25437 hl = DRAW_MOUSE_FACE;
25438 else
25439 hl = DRAW_NORMAL_TEXT;
25440 draw_phys_cursor_glyph (w, cursor_row, hl);
25441
25442 mark_cursor_off:
25443 w->phys_cursor_on_p = 0;
25444 w->phys_cursor_type = NO_CURSOR;
25445 }
25446
25447
25448 /* EXPORT:
25449 Display or clear cursor of window W. If ON is zero, clear the
25450 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25451 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25452
25453 void
25454 display_and_set_cursor (struct window *w, int on,
25455 int hpos, int vpos, int x, int y)
25456 {
25457 struct frame *f = XFRAME (w->frame);
25458 int new_cursor_type;
25459 int new_cursor_width;
25460 int active_cursor;
25461 struct glyph_row *glyph_row;
25462 struct glyph *glyph;
25463
25464 /* This is pointless on invisible frames, and dangerous on garbaged
25465 windows and frames; in the latter case, the frame or window may
25466 be in the midst of changing its size, and x and y may be off the
25467 window. */
25468 if (! FRAME_VISIBLE_P (f)
25469 || FRAME_GARBAGED_P (f)
25470 || vpos >= w->current_matrix->nrows
25471 || hpos >= w->current_matrix->matrix_w)
25472 return;
25473
25474 /* If cursor is off and we want it off, return quickly. */
25475 if (!on && !w->phys_cursor_on_p)
25476 return;
25477
25478 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25479 /* If cursor row is not enabled, we don't really know where to
25480 display the cursor. */
25481 if (!glyph_row->enabled_p)
25482 {
25483 w->phys_cursor_on_p = 0;
25484 return;
25485 }
25486
25487 glyph = NULL;
25488 if (!glyph_row->exact_window_width_line_p
25489 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25490 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25491
25492 xassert (interrupt_input_blocked);
25493
25494 /* Set new_cursor_type to the cursor we want to be displayed. */
25495 new_cursor_type = get_window_cursor_type (w, glyph,
25496 &new_cursor_width, &active_cursor);
25497
25498 /* If cursor is currently being shown and we don't want it to be or
25499 it is in the wrong place, or the cursor type is not what we want,
25500 erase it. */
25501 if (w->phys_cursor_on_p
25502 && (!on
25503 || w->phys_cursor.x != x
25504 || w->phys_cursor.y != y
25505 || new_cursor_type != w->phys_cursor_type
25506 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25507 && new_cursor_width != w->phys_cursor_width)))
25508 erase_phys_cursor (w);
25509
25510 /* Don't check phys_cursor_on_p here because that flag is only set
25511 to zero in some cases where we know that the cursor has been
25512 completely erased, to avoid the extra work of erasing the cursor
25513 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25514 still not be visible, or it has only been partly erased. */
25515 if (on)
25516 {
25517 w->phys_cursor_ascent = glyph_row->ascent;
25518 w->phys_cursor_height = glyph_row->height;
25519
25520 /* Set phys_cursor_.* before x_draw_.* is called because some
25521 of them may need the information. */
25522 w->phys_cursor.x = x;
25523 w->phys_cursor.y = glyph_row->y;
25524 w->phys_cursor.hpos = hpos;
25525 w->phys_cursor.vpos = vpos;
25526 }
25527
25528 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25529 new_cursor_type, new_cursor_width,
25530 on, active_cursor);
25531 }
25532
25533
25534 /* Switch the display of W's cursor on or off, according to the value
25535 of ON. */
25536
25537 static void
25538 update_window_cursor (struct window *w, int on)
25539 {
25540 /* Don't update cursor in windows whose frame is in the process
25541 of being deleted. */
25542 if (w->current_matrix)
25543 {
25544 int hpos = w->phys_cursor.hpos;
25545 int vpos = w->phys_cursor.vpos;
25546 struct glyph_row *row;
25547
25548 if (vpos >= w->current_matrix->nrows
25549 || hpos >= w->current_matrix->matrix_w)
25550 return;
25551
25552 row = MATRIX_ROW (w->current_matrix, vpos);
25553
25554 /* When the window is hscrolled, cursor hpos can legitimately be
25555 out of bounds, but we draw the cursor at the corresponding
25556 window margin in that case. */
25557 if (!row->reversed_p && hpos < 0)
25558 hpos = 0;
25559 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25560 hpos = row->used[TEXT_AREA] - 1;
25561
25562 BLOCK_INPUT;
25563 display_and_set_cursor (w, on, hpos, vpos,
25564 w->phys_cursor.x, w->phys_cursor.y);
25565 UNBLOCK_INPUT;
25566 }
25567 }
25568
25569
25570 /* Call update_window_cursor with parameter ON_P on all leaf windows
25571 in the window tree rooted at W. */
25572
25573 static void
25574 update_cursor_in_window_tree (struct window *w, int on_p)
25575 {
25576 while (w)
25577 {
25578 if (!NILP (w->hchild))
25579 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25580 else if (!NILP (w->vchild))
25581 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25582 else
25583 update_window_cursor (w, on_p);
25584
25585 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25586 }
25587 }
25588
25589
25590 /* EXPORT:
25591 Display the cursor on window W, or clear it, according to ON_P.
25592 Don't change the cursor's position. */
25593
25594 void
25595 x_update_cursor (struct frame *f, int on_p)
25596 {
25597 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25598 }
25599
25600
25601 /* EXPORT:
25602 Clear the cursor of window W to background color, and mark the
25603 cursor as not shown. This is used when the text where the cursor
25604 is about to be rewritten. */
25605
25606 void
25607 x_clear_cursor (struct window *w)
25608 {
25609 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25610 update_window_cursor (w, 0);
25611 }
25612
25613 #endif /* HAVE_WINDOW_SYSTEM */
25614
25615 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25616 and MSDOS. */
25617 static void
25618 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25619 int start_hpos, int end_hpos,
25620 enum draw_glyphs_face draw)
25621 {
25622 #ifdef HAVE_WINDOW_SYSTEM
25623 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25624 {
25625 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25626 return;
25627 }
25628 #endif
25629 #if defined (HAVE_GPM) || defined (MSDOS)
25630 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25631 #endif
25632 }
25633
25634 /* Display the active region described by mouse_face_* according to DRAW. */
25635
25636 static void
25637 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25638 {
25639 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25640 struct frame *f = XFRAME (WINDOW_FRAME (w));
25641
25642 if (/* If window is in the process of being destroyed, don't bother
25643 to do anything. */
25644 w->current_matrix != NULL
25645 /* Don't update mouse highlight if hidden */
25646 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25647 /* Recognize when we are called to operate on rows that don't exist
25648 anymore. This can happen when a window is split. */
25649 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25650 {
25651 int phys_cursor_on_p = w->phys_cursor_on_p;
25652 struct glyph_row *row, *first, *last;
25653
25654 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25655 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25656
25657 for (row = first; row <= last && row->enabled_p; ++row)
25658 {
25659 int start_hpos, end_hpos, start_x;
25660
25661 /* For all but the first row, the highlight starts at column 0. */
25662 if (row == first)
25663 {
25664 /* R2L rows have BEG and END in reversed order, but the
25665 screen drawing geometry is always left to right. So
25666 we need to mirror the beginning and end of the
25667 highlighted area in R2L rows. */
25668 if (!row->reversed_p)
25669 {
25670 start_hpos = hlinfo->mouse_face_beg_col;
25671 start_x = hlinfo->mouse_face_beg_x;
25672 }
25673 else if (row == last)
25674 {
25675 start_hpos = hlinfo->mouse_face_end_col;
25676 start_x = hlinfo->mouse_face_end_x;
25677 }
25678 else
25679 {
25680 start_hpos = 0;
25681 start_x = 0;
25682 }
25683 }
25684 else if (row->reversed_p && row == last)
25685 {
25686 start_hpos = hlinfo->mouse_face_end_col;
25687 start_x = hlinfo->mouse_face_end_x;
25688 }
25689 else
25690 {
25691 start_hpos = 0;
25692 start_x = 0;
25693 }
25694
25695 if (row == last)
25696 {
25697 if (!row->reversed_p)
25698 end_hpos = hlinfo->mouse_face_end_col;
25699 else if (row == first)
25700 end_hpos = hlinfo->mouse_face_beg_col;
25701 else
25702 {
25703 end_hpos = row->used[TEXT_AREA];
25704 if (draw == DRAW_NORMAL_TEXT)
25705 row->fill_line_p = 1; /* Clear to end of line */
25706 }
25707 }
25708 else if (row->reversed_p && row == first)
25709 end_hpos = hlinfo->mouse_face_beg_col;
25710 else
25711 {
25712 end_hpos = row->used[TEXT_AREA];
25713 if (draw == DRAW_NORMAL_TEXT)
25714 row->fill_line_p = 1; /* Clear to end of line */
25715 }
25716
25717 if (end_hpos > start_hpos)
25718 {
25719 draw_row_with_mouse_face (w, start_x, row,
25720 start_hpos, end_hpos, draw);
25721
25722 row->mouse_face_p
25723 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25724 }
25725 }
25726
25727 #ifdef HAVE_WINDOW_SYSTEM
25728 /* When we've written over the cursor, arrange for it to
25729 be displayed again. */
25730 if (FRAME_WINDOW_P (f)
25731 && phys_cursor_on_p && !w->phys_cursor_on_p)
25732 {
25733 int hpos = w->phys_cursor.hpos;
25734
25735 /* When the window is hscrolled, cursor hpos can legitimately be
25736 out of bounds, but we draw the cursor at the corresponding
25737 window margin in that case. */
25738 if (!row->reversed_p && hpos < 0)
25739 hpos = 0;
25740 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25741 hpos = row->used[TEXT_AREA] - 1;
25742
25743 BLOCK_INPUT;
25744 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25745 w->phys_cursor.x, w->phys_cursor.y);
25746 UNBLOCK_INPUT;
25747 }
25748 #endif /* HAVE_WINDOW_SYSTEM */
25749 }
25750
25751 #ifdef HAVE_WINDOW_SYSTEM
25752 /* Change the mouse cursor. */
25753 if (FRAME_WINDOW_P (f))
25754 {
25755 if (draw == DRAW_NORMAL_TEXT
25756 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25757 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25758 else if (draw == DRAW_MOUSE_FACE)
25759 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25760 else
25761 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25762 }
25763 #endif /* HAVE_WINDOW_SYSTEM */
25764 }
25765
25766 /* EXPORT:
25767 Clear out the mouse-highlighted active region.
25768 Redraw it un-highlighted first. Value is non-zero if mouse
25769 face was actually drawn unhighlighted. */
25770
25771 int
25772 clear_mouse_face (Mouse_HLInfo *hlinfo)
25773 {
25774 int cleared = 0;
25775
25776 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25777 {
25778 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25779 cleared = 1;
25780 }
25781
25782 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25783 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25784 hlinfo->mouse_face_window = Qnil;
25785 hlinfo->mouse_face_overlay = Qnil;
25786 return cleared;
25787 }
25788
25789 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25790 within the mouse face on that window. */
25791 static int
25792 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25793 {
25794 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25795
25796 /* Quickly resolve the easy cases. */
25797 if (!(WINDOWP (hlinfo->mouse_face_window)
25798 && XWINDOW (hlinfo->mouse_face_window) == w))
25799 return 0;
25800 if (vpos < hlinfo->mouse_face_beg_row
25801 || vpos > hlinfo->mouse_face_end_row)
25802 return 0;
25803 if (vpos > hlinfo->mouse_face_beg_row
25804 && vpos < hlinfo->mouse_face_end_row)
25805 return 1;
25806
25807 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25808 {
25809 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25810 {
25811 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25812 return 1;
25813 }
25814 else if ((vpos == hlinfo->mouse_face_beg_row
25815 && hpos >= hlinfo->mouse_face_beg_col)
25816 || (vpos == hlinfo->mouse_face_end_row
25817 && hpos < hlinfo->mouse_face_end_col))
25818 return 1;
25819 }
25820 else
25821 {
25822 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25823 {
25824 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25825 return 1;
25826 }
25827 else if ((vpos == hlinfo->mouse_face_beg_row
25828 && hpos <= hlinfo->mouse_face_beg_col)
25829 || (vpos == hlinfo->mouse_face_end_row
25830 && hpos > hlinfo->mouse_face_end_col))
25831 return 1;
25832 }
25833 return 0;
25834 }
25835
25836
25837 /* EXPORT:
25838 Non-zero if physical cursor of window W is within mouse face. */
25839
25840 int
25841 cursor_in_mouse_face_p (struct window *w)
25842 {
25843 int hpos = w->phys_cursor.hpos;
25844 int vpos = w->phys_cursor.vpos;
25845 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
25846
25847 /* When the window is hscrolled, cursor hpos can legitimately be out
25848 of bounds, but we draw the cursor at the corresponding window
25849 margin in that case. */
25850 if (!row->reversed_p && hpos < 0)
25851 hpos = 0;
25852 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25853 hpos = row->used[TEXT_AREA] - 1;
25854
25855 return coords_in_mouse_face_p (w, hpos, vpos);
25856 }
25857
25858
25859 \f
25860 /* Find the glyph rows START_ROW and END_ROW of window W that display
25861 characters between buffer positions START_CHARPOS and END_CHARPOS
25862 (excluding END_CHARPOS). DISP_STRING is a display string that
25863 covers these buffer positions. This is similar to
25864 row_containing_pos, but is more accurate when bidi reordering makes
25865 buffer positions change non-linearly with glyph rows. */
25866 static void
25867 rows_from_pos_range (struct window *w,
25868 EMACS_INT start_charpos, EMACS_INT end_charpos,
25869 Lisp_Object disp_string,
25870 struct glyph_row **start, struct glyph_row **end)
25871 {
25872 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25873 int last_y = window_text_bottom_y (w);
25874 struct glyph_row *row;
25875
25876 *start = NULL;
25877 *end = NULL;
25878
25879 while (!first->enabled_p
25880 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25881 first++;
25882
25883 /* Find the START row. */
25884 for (row = first;
25885 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25886 row++)
25887 {
25888 /* A row can potentially be the START row if the range of the
25889 characters it displays intersects the range
25890 [START_CHARPOS..END_CHARPOS). */
25891 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25892 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25893 /* See the commentary in row_containing_pos, for the
25894 explanation of the complicated way to check whether
25895 some position is beyond the end of the characters
25896 displayed by a row. */
25897 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25898 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25899 && !row->ends_at_zv_p
25900 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25901 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25902 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25903 && !row->ends_at_zv_p
25904 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25905 {
25906 /* Found a candidate row. Now make sure at least one of the
25907 glyphs it displays has a charpos from the range
25908 [START_CHARPOS..END_CHARPOS).
25909
25910 This is not obvious because bidi reordering could make
25911 buffer positions of a row be 1,2,3,102,101,100, and if we
25912 want to highlight characters in [50..60), we don't want
25913 this row, even though [50..60) does intersect [1..103),
25914 the range of character positions given by the row's start
25915 and end positions. */
25916 struct glyph *g = row->glyphs[TEXT_AREA];
25917 struct glyph *e = g + row->used[TEXT_AREA];
25918
25919 while (g < e)
25920 {
25921 if (((BUFFERP (g->object) || INTEGERP (g->object))
25922 && start_charpos <= g->charpos && g->charpos < end_charpos)
25923 /* A glyph that comes from DISP_STRING is by
25924 definition to be highlighted. */
25925 || EQ (g->object, disp_string))
25926 *start = row;
25927 g++;
25928 }
25929 if (*start)
25930 break;
25931 }
25932 }
25933
25934 /* Find the END row. */
25935 if (!*start
25936 /* If the last row is partially visible, start looking for END
25937 from that row, instead of starting from FIRST. */
25938 && !(row->enabled_p
25939 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25940 row = first;
25941 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25942 {
25943 struct glyph_row *next = row + 1;
25944 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
25945
25946 if (!next->enabled_p
25947 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25948 /* The first row >= START whose range of displayed characters
25949 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25950 is the row END + 1. */
25951 || (start_charpos < next_start
25952 && end_charpos < next_start)
25953 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25954 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25955 && !next->ends_at_zv_p
25956 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25957 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25958 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25959 && !next->ends_at_zv_p
25960 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25961 {
25962 *end = row;
25963 break;
25964 }
25965 else
25966 {
25967 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25968 but none of the characters it displays are in the range, it is
25969 also END + 1. */
25970 struct glyph *g = next->glyphs[TEXT_AREA];
25971 struct glyph *s = g;
25972 struct glyph *e = g + next->used[TEXT_AREA];
25973
25974 while (g < e)
25975 {
25976 if (((BUFFERP (g->object) || INTEGERP (g->object))
25977 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
25978 /* If the buffer position of the first glyph in
25979 the row is equal to END_CHARPOS, it means
25980 the last character to be highlighted is the
25981 newline of ROW, and we must consider NEXT as
25982 END, not END+1. */
25983 || (((!next->reversed_p && g == s)
25984 || (next->reversed_p && g == e - 1))
25985 && (g->charpos == end_charpos
25986 /* Special case for when NEXT is an
25987 empty line at ZV. */
25988 || (g->charpos == -1
25989 && !row->ends_at_zv_p
25990 && next_start == end_charpos)))))
25991 /* A glyph that comes from DISP_STRING is by
25992 definition to be highlighted. */
25993 || EQ (g->object, disp_string))
25994 break;
25995 g++;
25996 }
25997 if (g == e)
25998 {
25999 *end = row;
26000 break;
26001 }
26002 /* The first row that ends at ZV must be the last to be
26003 highlighted. */
26004 else if (next->ends_at_zv_p)
26005 {
26006 *end = next;
26007 break;
26008 }
26009 }
26010 }
26011 }
26012
26013 /* This function sets the mouse_face_* elements of HLINFO, assuming
26014 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26015 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26016 for the overlay or run of text properties specifying the mouse
26017 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26018 before-string and after-string that must also be highlighted.
26019 DISP_STRING, if non-nil, is a display string that may cover some
26020 or all of the highlighted text. */
26021
26022 static void
26023 mouse_face_from_buffer_pos (Lisp_Object window,
26024 Mouse_HLInfo *hlinfo,
26025 EMACS_INT mouse_charpos,
26026 EMACS_INT start_charpos,
26027 EMACS_INT end_charpos,
26028 Lisp_Object before_string,
26029 Lisp_Object after_string,
26030 Lisp_Object disp_string)
26031 {
26032 struct window *w = XWINDOW (window);
26033 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26034 struct glyph_row *r1, *r2;
26035 struct glyph *glyph, *end;
26036 EMACS_INT ignore, pos;
26037 int x;
26038
26039 xassert (NILP (disp_string) || STRINGP (disp_string));
26040 xassert (NILP (before_string) || STRINGP (before_string));
26041 xassert (NILP (after_string) || STRINGP (after_string));
26042
26043 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26044 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26045 if (r1 == NULL)
26046 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26047 /* If the before-string or display-string contains newlines,
26048 rows_from_pos_range skips to its last row. Move back. */
26049 if (!NILP (before_string) || !NILP (disp_string))
26050 {
26051 struct glyph_row *prev;
26052 while ((prev = r1 - 1, prev >= first)
26053 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26054 && prev->used[TEXT_AREA] > 0)
26055 {
26056 struct glyph *beg = prev->glyphs[TEXT_AREA];
26057 glyph = beg + prev->used[TEXT_AREA];
26058 while (--glyph >= beg && INTEGERP (glyph->object));
26059 if (glyph < beg
26060 || !(EQ (glyph->object, before_string)
26061 || EQ (glyph->object, disp_string)))
26062 break;
26063 r1 = prev;
26064 }
26065 }
26066 if (r2 == NULL)
26067 {
26068 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26069 hlinfo->mouse_face_past_end = 1;
26070 }
26071 else if (!NILP (after_string))
26072 {
26073 /* If the after-string has newlines, advance to its last row. */
26074 struct glyph_row *next;
26075 struct glyph_row *last
26076 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26077
26078 for (next = r2 + 1;
26079 next <= last
26080 && next->used[TEXT_AREA] > 0
26081 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26082 ++next)
26083 r2 = next;
26084 }
26085 /* The rest of the display engine assumes that mouse_face_beg_row is
26086 either above mouse_face_end_row or identical to it. But with
26087 bidi-reordered continued lines, the row for START_CHARPOS could
26088 be below the row for END_CHARPOS. If so, swap the rows and store
26089 them in correct order. */
26090 if (r1->y > r2->y)
26091 {
26092 struct glyph_row *tem = r2;
26093
26094 r2 = r1;
26095 r1 = tem;
26096 }
26097
26098 hlinfo->mouse_face_beg_y = r1->y;
26099 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26100 hlinfo->mouse_face_end_y = r2->y;
26101 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26102
26103 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26104 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26105 could be anywhere in the row and in any order. The strategy
26106 below is to find the leftmost and the rightmost glyph that
26107 belongs to either of these 3 strings, or whose position is
26108 between START_CHARPOS and END_CHARPOS, and highlight all the
26109 glyphs between those two. This may cover more than just the text
26110 between START_CHARPOS and END_CHARPOS if the range of characters
26111 strides the bidi level boundary, e.g. if the beginning is in R2L
26112 text while the end is in L2R text or vice versa. */
26113 if (!r1->reversed_p)
26114 {
26115 /* This row is in a left to right paragraph. Scan it left to
26116 right. */
26117 glyph = r1->glyphs[TEXT_AREA];
26118 end = glyph + r1->used[TEXT_AREA];
26119 x = r1->x;
26120
26121 /* Skip truncation glyphs at the start of the glyph row. */
26122 if (r1->displays_text_p)
26123 for (; glyph < end
26124 && INTEGERP (glyph->object)
26125 && glyph->charpos < 0;
26126 ++glyph)
26127 x += glyph->pixel_width;
26128
26129 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26130 or DISP_STRING, and the first glyph from buffer whose
26131 position is between START_CHARPOS and END_CHARPOS. */
26132 for (; glyph < end
26133 && !INTEGERP (glyph->object)
26134 && !EQ (glyph->object, disp_string)
26135 && !(BUFFERP (glyph->object)
26136 && (glyph->charpos >= start_charpos
26137 && glyph->charpos < end_charpos));
26138 ++glyph)
26139 {
26140 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26141 are present at buffer positions between START_CHARPOS and
26142 END_CHARPOS, or if they come from an overlay. */
26143 if (EQ (glyph->object, before_string))
26144 {
26145 pos = string_buffer_position (before_string,
26146 start_charpos);
26147 /* If pos == 0, it means before_string came from an
26148 overlay, not from a buffer position. */
26149 if (!pos || (pos >= start_charpos && pos < end_charpos))
26150 break;
26151 }
26152 else if (EQ (glyph->object, after_string))
26153 {
26154 pos = string_buffer_position (after_string, end_charpos);
26155 if (!pos || (pos >= start_charpos && pos < end_charpos))
26156 break;
26157 }
26158 x += glyph->pixel_width;
26159 }
26160 hlinfo->mouse_face_beg_x = x;
26161 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26162 }
26163 else
26164 {
26165 /* This row is in a right to left paragraph. Scan it right to
26166 left. */
26167 struct glyph *g;
26168
26169 end = r1->glyphs[TEXT_AREA] - 1;
26170 glyph = end + r1->used[TEXT_AREA];
26171
26172 /* Skip truncation glyphs at the start of the glyph row. */
26173 if (r1->displays_text_p)
26174 for (; glyph > end
26175 && INTEGERP (glyph->object)
26176 && glyph->charpos < 0;
26177 --glyph)
26178 ;
26179
26180 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26181 or DISP_STRING, and the first glyph from buffer whose
26182 position is between START_CHARPOS and END_CHARPOS. */
26183 for (; glyph > end
26184 && !INTEGERP (glyph->object)
26185 && !EQ (glyph->object, disp_string)
26186 && !(BUFFERP (glyph->object)
26187 && (glyph->charpos >= start_charpos
26188 && glyph->charpos < end_charpos));
26189 --glyph)
26190 {
26191 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26192 are present at buffer positions between START_CHARPOS and
26193 END_CHARPOS, or if they come from an overlay. */
26194 if (EQ (glyph->object, before_string))
26195 {
26196 pos = string_buffer_position (before_string, start_charpos);
26197 /* If pos == 0, it means before_string came from an
26198 overlay, not from a buffer position. */
26199 if (!pos || (pos >= start_charpos && pos < end_charpos))
26200 break;
26201 }
26202 else if (EQ (glyph->object, after_string))
26203 {
26204 pos = string_buffer_position (after_string, end_charpos);
26205 if (!pos || (pos >= start_charpos && pos < end_charpos))
26206 break;
26207 }
26208 }
26209
26210 glyph++; /* first glyph to the right of the highlighted area */
26211 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26212 x += g->pixel_width;
26213 hlinfo->mouse_face_beg_x = x;
26214 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26215 }
26216
26217 /* If the highlight ends in a different row, compute GLYPH and END
26218 for the end row. Otherwise, reuse the values computed above for
26219 the row where the highlight begins. */
26220 if (r2 != r1)
26221 {
26222 if (!r2->reversed_p)
26223 {
26224 glyph = r2->glyphs[TEXT_AREA];
26225 end = glyph + r2->used[TEXT_AREA];
26226 x = r2->x;
26227 }
26228 else
26229 {
26230 end = r2->glyphs[TEXT_AREA] - 1;
26231 glyph = end + r2->used[TEXT_AREA];
26232 }
26233 }
26234
26235 if (!r2->reversed_p)
26236 {
26237 /* Skip truncation and continuation glyphs near the end of the
26238 row, and also blanks and stretch glyphs inserted by
26239 extend_face_to_end_of_line. */
26240 while (end > glyph
26241 && INTEGERP ((end - 1)->object))
26242 --end;
26243 /* Scan the rest of the glyph row from the end, looking for the
26244 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26245 DISP_STRING, or whose position is between START_CHARPOS
26246 and END_CHARPOS */
26247 for (--end;
26248 end > glyph
26249 && !INTEGERP (end->object)
26250 && !EQ (end->object, disp_string)
26251 && !(BUFFERP (end->object)
26252 && (end->charpos >= start_charpos
26253 && end->charpos < end_charpos));
26254 --end)
26255 {
26256 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26257 are present at buffer positions between START_CHARPOS and
26258 END_CHARPOS, or if they come from an overlay. */
26259 if (EQ (end->object, before_string))
26260 {
26261 pos = string_buffer_position (before_string, start_charpos);
26262 if (!pos || (pos >= start_charpos && pos < end_charpos))
26263 break;
26264 }
26265 else if (EQ (end->object, after_string))
26266 {
26267 pos = string_buffer_position (after_string, end_charpos);
26268 if (!pos || (pos >= start_charpos && pos < end_charpos))
26269 break;
26270 }
26271 }
26272 /* Find the X coordinate of the last glyph to be highlighted. */
26273 for (; glyph <= end; ++glyph)
26274 x += glyph->pixel_width;
26275
26276 hlinfo->mouse_face_end_x = x;
26277 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26278 }
26279 else
26280 {
26281 /* Skip truncation and continuation glyphs near the end of the
26282 row, and also blanks and stretch glyphs inserted by
26283 extend_face_to_end_of_line. */
26284 x = r2->x;
26285 end++;
26286 while (end < glyph
26287 && INTEGERP (end->object))
26288 {
26289 x += end->pixel_width;
26290 ++end;
26291 }
26292 /* Scan the rest of the glyph row from the end, looking for the
26293 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26294 DISP_STRING, or whose position is between START_CHARPOS
26295 and END_CHARPOS */
26296 for ( ;
26297 end < glyph
26298 && !INTEGERP (end->object)
26299 && !EQ (end->object, disp_string)
26300 && !(BUFFERP (end->object)
26301 && (end->charpos >= start_charpos
26302 && end->charpos < end_charpos));
26303 ++end)
26304 {
26305 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26306 are present at buffer positions between START_CHARPOS and
26307 END_CHARPOS, or if they come from an overlay. */
26308 if (EQ (end->object, before_string))
26309 {
26310 pos = string_buffer_position (before_string, start_charpos);
26311 if (!pos || (pos >= start_charpos && pos < end_charpos))
26312 break;
26313 }
26314 else if (EQ (end->object, after_string))
26315 {
26316 pos = string_buffer_position (after_string, end_charpos);
26317 if (!pos || (pos >= start_charpos && pos < end_charpos))
26318 break;
26319 }
26320 x += end->pixel_width;
26321 }
26322 /* If we exited the above loop because we arrived at the last
26323 glyph of the row, and its buffer position is still not in
26324 range, it means the last character in range is the preceding
26325 newline. Bump the end column and x values to get past the
26326 last glyph. */
26327 if (end == glyph
26328 && BUFFERP (end->object)
26329 && (end->charpos < start_charpos
26330 || end->charpos >= end_charpos))
26331 {
26332 x += end->pixel_width;
26333 ++end;
26334 }
26335 hlinfo->mouse_face_end_x = x;
26336 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26337 }
26338
26339 hlinfo->mouse_face_window = window;
26340 hlinfo->mouse_face_face_id
26341 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26342 mouse_charpos + 1,
26343 !hlinfo->mouse_face_hidden, -1);
26344 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26345 }
26346
26347 /* The following function is not used anymore (replaced with
26348 mouse_face_from_string_pos), but I leave it here for the time
26349 being, in case someone would. */
26350
26351 #if 0 /* not used */
26352
26353 /* Find the position of the glyph for position POS in OBJECT in
26354 window W's current matrix, and return in *X, *Y the pixel
26355 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26356
26357 RIGHT_P non-zero means return the position of the right edge of the
26358 glyph, RIGHT_P zero means return the left edge position.
26359
26360 If no glyph for POS exists in the matrix, return the position of
26361 the glyph with the next smaller position that is in the matrix, if
26362 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26363 exists in the matrix, return the position of the glyph with the
26364 next larger position in OBJECT.
26365
26366 Value is non-zero if a glyph was found. */
26367
26368 static int
26369 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26370 int *hpos, int *vpos, int *x, int *y, int right_p)
26371 {
26372 int yb = window_text_bottom_y (w);
26373 struct glyph_row *r;
26374 struct glyph *best_glyph = NULL;
26375 struct glyph_row *best_row = NULL;
26376 int best_x = 0;
26377
26378 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26379 r->enabled_p && r->y < yb;
26380 ++r)
26381 {
26382 struct glyph *g = r->glyphs[TEXT_AREA];
26383 struct glyph *e = g + r->used[TEXT_AREA];
26384 int gx;
26385
26386 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26387 if (EQ (g->object, object))
26388 {
26389 if (g->charpos == pos)
26390 {
26391 best_glyph = g;
26392 best_x = gx;
26393 best_row = r;
26394 goto found;
26395 }
26396 else if (best_glyph == NULL
26397 || ((eabs (g->charpos - pos)
26398 < eabs (best_glyph->charpos - pos))
26399 && (right_p
26400 ? g->charpos < pos
26401 : g->charpos > pos)))
26402 {
26403 best_glyph = g;
26404 best_x = gx;
26405 best_row = r;
26406 }
26407 }
26408 }
26409
26410 found:
26411
26412 if (best_glyph)
26413 {
26414 *x = best_x;
26415 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26416
26417 if (right_p)
26418 {
26419 *x += best_glyph->pixel_width;
26420 ++*hpos;
26421 }
26422
26423 *y = best_row->y;
26424 *vpos = best_row - w->current_matrix->rows;
26425 }
26426
26427 return best_glyph != NULL;
26428 }
26429 #endif /* not used */
26430
26431 /* Find the positions of the first and the last glyphs in window W's
26432 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26433 (assumed to be a string), and return in HLINFO's mouse_face_*
26434 members the pixel and column/row coordinates of those glyphs. */
26435
26436 static void
26437 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26438 Lisp_Object object,
26439 EMACS_INT startpos, EMACS_INT endpos)
26440 {
26441 int yb = window_text_bottom_y (w);
26442 struct glyph_row *r;
26443 struct glyph *g, *e;
26444 int gx;
26445 int found = 0;
26446
26447 /* Find the glyph row with at least one position in the range
26448 [STARTPOS..ENDPOS], and the first glyph in that row whose
26449 position belongs to that range. */
26450 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26451 r->enabled_p && r->y < yb;
26452 ++r)
26453 {
26454 if (!r->reversed_p)
26455 {
26456 g = r->glyphs[TEXT_AREA];
26457 e = g + r->used[TEXT_AREA];
26458 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26459 if (EQ (g->object, object)
26460 && startpos <= g->charpos && g->charpos <= endpos)
26461 {
26462 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26463 hlinfo->mouse_face_beg_y = r->y;
26464 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26465 hlinfo->mouse_face_beg_x = gx;
26466 found = 1;
26467 break;
26468 }
26469 }
26470 else
26471 {
26472 struct glyph *g1;
26473
26474 e = r->glyphs[TEXT_AREA];
26475 g = e + r->used[TEXT_AREA];
26476 for ( ; g > e; --g)
26477 if (EQ ((g-1)->object, object)
26478 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26479 {
26480 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26481 hlinfo->mouse_face_beg_y = r->y;
26482 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26483 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26484 gx += g1->pixel_width;
26485 hlinfo->mouse_face_beg_x = gx;
26486 found = 1;
26487 break;
26488 }
26489 }
26490 if (found)
26491 break;
26492 }
26493
26494 if (!found)
26495 return;
26496
26497 /* Starting with the next row, look for the first row which does NOT
26498 include any glyphs whose positions are in the range. */
26499 for (++r; r->enabled_p && r->y < yb; ++r)
26500 {
26501 g = r->glyphs[TEXT_AREA];
26502 e = g + r->used[TEXT_AREA];
26503 found = 0;
26504 for ( ; g < e; ++g)
26505 if (EQ (g->object, object)
26506 && startpos <= g->charpos && g->charpos <= endpos)
26507 {
26508 found = 1;
26509 break;
26510 }
26511 if (!found)
26512 break;
26513 }
26514
26515 /* The highlighted region ends on the previous row. */
26516 r--;
26517
26518 /* Set the end row and its vertical pixel coordinate. */
26519 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26520 hlinfo->mouse_face_end_y = r->y;
26521
26522 /* Compute and set the end column and the end column's horizontal
26523 pixel coordinate. */
26524 if (!r->reversed_p)
26525 {
26526 g = r->glyphs[TEXT_AREA];
26527 e = g + r->used[TEXT_AREA];
26528 for ( ; e > g; --e)
26529 if (EQ ((e-1)->object, object)
26530 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26531 break;
26532 hlinfo->mouse_face_end_col = e - g;
26533
26534 for (gx = r->x; g < e; ++g)
26535 gx += g->pixel_width;
26536 hlinfo->mouse_face_end_x = gx;
26537 }
26538 else
26539 {
26540 e = r->glyphs[TEXT_AREA];
26541 g = e + r->used[TEXT_AREA];
26542 for (gx = r->x ; e < g; ++e)
26543 {
26544 if (EQ (e->object, object)
26545 && startpos <= e->charpos && e->charpos <= endpos)
26546 break;
26547 gx += e->pixel_width;
26548 }
26549 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26550 hlinfo->mouse_face_end_x = gx;
26551 }
26552 }
26553
26554 #ifdef HAVE_WINDOW_SYSTEM
26555
26556 /* See if position X, Y is within a hot-spot of an image. */
26557
26558 static int
26559 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26560 {
26561 if (!CONSP (hot_spot))
26562 return 0;
26563
26564 if (EQ (XCAR (hot_spot), Qrect))
26565 {
26566 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26567 Lisp_Object rect = XCDR (hot_spot);
26568 Lisp_Object tem;
26569 if (!CONSP (rect))
26570 return 0;
26571 if (!CONSP (XCAR (rect)))
26572 return 0;
26573 if (!CONSP (XCDR (rect)))
26574 return 0;
26575 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26576 return 0;
26577 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26578 return 0;
26579 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26580 return 0;
26581 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26582 return 0;
26583 return 1;
26584 }
26585 else if (EQ (XCAR (hot_spot), Qcircle))
26586 {
26587 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26588 Lisp_Object circ = XCDR (hot_spot);
26589 Lisp_Object lr, lx0, ly0;
26590 if (CONSP (circ)
26591 && CONSP (XCAR (circ))
26592 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26593 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26594 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26595 {
26596 double r = XFLOATINT (lr);
26597 double dx = XINT (lx0) - x;
26598 double dy = XINT (ly0) - y;
26599 return (dx * dx + dy * dy <= r * r);
26600 }
26601 }
26602 else if (EQ (XCAR (hot_spot), Qpoly))
26603 {
26604 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26605 if (VECTORP (XCDR (hot_spot)))
26606 {
26607 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26608 Lisp_Object *poly = v->contents;
26609 int n = v->header.size;
26610 int i;
26611 int inside = 0;
26612 Lisp_Object lx, ly;
26613 int x0, y0;
26614
26615 /* Need an even number of coordinates, and at least 3 edges. */
26616 if (n < 6 || n & 1)
26617 return 0;
26618
26619 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26620 If count is odd, we are inside polygon. Pixels on edges
26621 may or may not be included depending on actual geometry of the
26622 polygon. */
26623 if ((lx = poly[n-2], !INTEGERP (lx))
26624 || (ly = poly[n-1], !INTEGERP (lx)))
26625 return 0;
26626 x0 = XINT (lx), y0 = XINT (ly);
26627 for (i = 0; i < n; i += 2)
26628 {
26629 int x1 = x0, y1 = y0;
26630 if ((lx = poly[i], !INTEGERP (lx))
26631 || (ly = poly[i+1], !INTEGERP (ly)))
26632 return 0;
26633 x0 = XINT (lx), y0 = XINT (ly);
26634
26635 /* Does this segment cross the X line? */
26636 if (x0 >= x)
26637 {
26638 if (x1 >= x)
26639 continue;
26640 }
26641 else if (x1 < x)
26642 continue;
26643 if (y > y0 && y > y1)
26644 continue;
26645 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26646 inside = !inside;
26647 }
26648 return inside;
26649 }
26650 }
26651 return 0;
26652 }
26653
26654 Lisp_Object
26655 find_hot_spot (Lisp_Object map, int x, int y)
26656 {
26657 while (CONSP (map))
26658 {
26659 if (CONSP (XCAR (map))
26660 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26661 return XCAR (map);
26662 map = XCDR (map);
26663 }
26664
26665 return Qnil;
26666 }
26667
26668 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26669 3, 3, 0,
26670 doc: /* Lookup in image map MAP coordinates X and Y.
26671 An image map is an alist where each element has the format (AREA ID PLIST).
26672 An AREA is specified as either a rectangle, a circle, or a polygon:
26673 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26674 pixel coordinates of the upper left and bottom right corners.
26675 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26676 and the radius of the circle; r may be a float or integer.
26677 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26678 vector describes one corner in the polygon.
26679 Returns the alist element for the first matching AREA in MAP. */)
26680 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26681 {
26682 if (NILP (map))
26683 return Qnil;
26684
26685 CHECK_NUMBER (x);
26686 CHECK_NUMBER (y);
26687
26688 return find_hot_spot (map, XINT (x), XINT (y));
26689 }
26690
26691
26692 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26693 static void
26694 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26695 {
26696 /* Do not change cursor shape while dragging mouse. */
26697 if (!NILP (do_mouse_tracking))
26698 return;
26699
26700 if (!NILP (pointer))
26701 {
26702 if (EQ (pointer, Qarrow))
26703 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26704 else if (EQ (pointer, Qhand))
26705 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26706 else if (EQ (pointer, Qtext))
26707 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26708 else if (EQ (pointer, intern ("hdrag")))
26709 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26710 #ifdef HAVE_X_WINDOWS
26711 else if (EQ (pointer, intern ("vdrag")))
26712 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26713 #endif
26714 else if (EQ (pointer, intern ("hourglass")))
26715 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26716 else if (EQ (pointer, Qmodeline))
26717 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26718 else
26719 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26720 }
26721
26722 if (cursor != No_Cursor)
26723 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26724 }
26725
26726 #endif /* HAVE_WINDOW_SYSTEM */
26727
26728 /* Take proper action when mouse has moved to the mode or header line
26729 or marginal area AREA of window W, x-position X and y-position Y.
26730 X is relative to the start of the text display area of W, so the
26731 width of bitmap areas and scroll bars must be subtracted to get a
26732 position relative to the start of the mode line. */
26733
26734 static void
26735 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26736 enum window_part area)
26737 {
26738 struct window *w = XWINDOW (window);
26739 struct frame *f = XFRAME (w->frame);
26740 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26741 #ifdef HAVE_WINDOW_SYSTEM
26742 Display_Info *dpyinfo;
26743 #endif
26744 Cursor cursor = No_Cursor;
26745 Lisp_Object pointer = Qnil;
26746 int dx, dy, width, height;
26747 EMACS_INT charpos;
26748 Lisp_Object string, object = Qnil;
26749 Lisp_Object pos, help;
26750
26751 Lisp_Object mouse_face;
26752 int original_x_pixel = x;
26753 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26754 struct glyph_row *row;
26755
26756 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26757 {
26758 int x0;
26759 struct glyph *end;
26760
26761 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26762 returns them in row/column units! */
26763 string = mode_line_string (w, area, &x, &y, &charpos,
26764 &object, &dx, &dy, &width, &height);
26765
26766 row = (area == ON_MODE_LINE
26767 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26768 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26769
26770 /* Find the glyph under the mouse pointer. */
26771 if (row->mode_line_p && row->enabled_p)
26772 {
26773 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26774 end = glyph + row->used[TEXT_AREA];
26775
26776 for (x0 = original_x_pixel;
26777 glyph < end && x0 >= glyph->pixel_width;
26778 ++glyph)
26779 x0 -= glyph->pixel_width;
26780
26781 if (glyph >= end)
26782 glyph = NULL;
26783 }
26784 }
26785 else
26786 {
26787 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26788 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26789 returns them in row/column units! */
26790 string = marginal_area_string (w, area, &x, &y, &charpos,
26791 &object, &dx, &dy, &width, &height);
26792 }
26793
26794 help = Qnil;
26795
26796 #ifdef HAVE_WINDOW_SYSTEM
26797 if (IMAGEP (object))
26798 {
26799 Lisp_Object image_map, hotspot;
26800 if ((image_map = Fplist_get (XCDR (object), QCmap),
26801 !NILP (image_map))
26802 && (hotspot = find_hot_spot (image_map, dx, dy),
26803 CONSP (hotspot))
26804 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26805 {
26806 Lisp_Object plist;
26807
26808 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26809 If so, we could look for mouse-enter, mouse-leave
26810 properties in PLIST (and do something...). */
26811 hotspot = XCDR (hotspot);
26812 if (CONSP (hotspot)
26813 && (plist = XCAR (hotspot), CONSP (plist)))
26814 {
26815 pointer = Fplist_get (plist, Qpointer);
26816 if (NILP (pointer))
26817 pointer = Qhand;
26818 help = Fplist_get (plist, Qhelp_echo);
26819 if (!NILP (help))
26820 {
26821 help_echo_string = help;
26822 /* Is this correct? ++kfs */
26823 XSETWINDOW (help_echo_window, w);
26824 help_echo_object = w->buffer;
26825 help_echo_pos = charpos;
26826 }
26827 }
26828 }
26829 if (NILP (pointer))
26830 pointer = Fplist_get (XCDR (object), QCpointer);
26831 }
26832 #endif /* HAVE_WINDOW_SYSTEM */
26833
26834 if (STRINGP (string))
26835 {
26836 pos = make_number (charpos);
26837 /* If we're on a string with `help-echo' text property, arrange
26838 for the help to be displayed. This is done by setting the
26839 global variable help_echo_string to the help string. */
26840 if (NILP (help))
26841 {
26842 help = Fget_text_property (pos, Qhelp_echo, string);
26843 if (!NILP (help))
26844 {
26845 help_echo_string = help;
26846 XSETWINDOW (help_echo_window, w);
26847 help_echo_object = string;
26848 help_echo_pos = charpos;
26849 }
26850 }
26851
26852 #ifdef HAVE_WINDOW_SYSTEM
26853 if (FRAME_WINDOW_P (f))
26854 {
26855 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26856 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26857 if (NILP (pointer))
26858 pointer = Fget_text_property (pos, Qpointer, string);
26859
26860 /* Change the mouse pointer according to what is under X/Y. */
26861 if (NILP (pointer)
26862 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26863 {
26864 Lisp_Object map;
26865 map = Fget_text_property (pos, Qlocal_map, string);
26866 if (!KEYMAPP (map))
26867 map = Fget_text_property (pos, Qkeymap, string);
26868 if (!KEYMAPP (map))
26869 cursor = dpyinfo->vertical_scroll_bar_cursor;
26870 }
26871 }
26872 #endif
26873
26874 /* Change the mouse face according to what is under X/Y. */
26875 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26876 if (!NILP (mouse_face)
26877 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26878 && glyph)
26879 {
26880 Lisp_Object b, e;
26881
26882 struct glyph * tmp_glyph;
26883
26884 int gpos;
26885 int gseq_length;
26886 int total_pixel_width;
26887 EMACS_INT begpos, endpos, ignore;
26888
26889 int vpos, hpos;
26890
26891 b = Fprevious_single_property_change (make_number (charpos + 1),
26892 Qmouse_face, string, Qnil);
26893 if (NILP (b))
26894 begpos = 0;
26895 else
26896 begpos = XINT (b);
26897
26898 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26899 if (NILP (e))
26900 endpos = SCHARS (string);
26901 else
26902 endpos = XINT (e);
26903
26904 /* Calculate the glyph position GPOS of GLYPH in the
26905 displayed string, relative to the beginning of the
26906 highlighted part of the string.
26907
26908 Note: GPOS is different from CHARPOS. CHARPOS is the
26909 position of GLYPH in the internal string object. A mode
26910 line string format has structures which are converted to
26911 a flattened string by the Emacs Lisp interpreter. The
26912 internal string is an element of those structures. The
26913 displayed string is the flattened string. */
26914 tmp_glyph = row_start_glyph;
26915 while (tmp_glyph < glyph
26916 && (!(EQ (tmp_glyph->object, glyph->object)
26917 && begpos <= tmp_glyph->charpos
26918 && tmp_glyph->charpos < endpos)))
26919 tmp_glyph++;
26920 gpos = glyph - tmp_glyph;
26921
26922 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26923 the highlighted part of the displayed string to which
26924 GLYPH belongs. Note: GSEQ_LENGTH is different from
26925 SCHARS (STRING), because the latter returns the length of
26926 the internal string. */
26927 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26928 tmp_glyph > glyph
26929 && (!(EQ (tmp_glyph->object, glyph->object)
26930 && begpos <= tmp_glyph->charpos
26931 && tmp_glyph->charpos < endpos));
26932 tmp_glyph--)
26933 ;
26934 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26935
26936 /* Calculate the total pixel width of all the glyphs between
26937 the beginning of the highlighted area and GLYPH. */
26938 total_pixel_width = 0;
26939 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26940 total_pixel_width += tmp_glyph->pixel_width;
26941
26942 /* Pre calculation of re-rendering position. Note: X is in
26943 column units here, after the call to mode_line_string or
26944 marginal_area_string. */
26945 hpos = x - gpos;
26946 vpos = (area == ON_MODE_LINE
26947 ? (w->current_matrix)->nrows - 1
26948 : 0);
26949
26950 /* If GLYPH's position is included in the region that is
26951 already drawn in mouse face, we have nothing to do. */
26952 if ( EQ (window, hlinfo->mouse_face_window)
26953 && (!row->reversed_p
26954 ? (hlinfo->mouse_face_beg_col <= hpos
26955 && hpos < hlinfo->mouse_face_end_col)
26956 /* In R2L rows we swap BEG and END, see below. */
26957 : (hlinfo->mouse_face_end_col <= hpos
26958 && hpos < hlinfo->mouse_face_beg_col))
26959 && hlinfo->mouse_face_beg_row == vpos )
26960 return;
26961
26962 if (clear_mouse_face (hlinfo))
26963 cursor = No_Cursor;
26964
26965 if (!row->reversed_p)
26966 {
26967 hlinfo->mouse_face_beg_col = hpos;
26968 hlinfo->mouse_face_beg_x = original_x_pixel
26969 - (total_pixel_width + dx);
26970 hlinfo->mouse_face_end_col = hpos + gseq_length;
26971 hlinfo->mouse_face_end_x = 0;
26972 }
26973 else
26974 {
26975 /* In R2L rows, show_mouse_face expects BEG and END
26976 coordinates to be swapped. */
26977 hlinfo->mouse_face_end_col = hpos;
26978 hlinfo->mouse_face_end_x = original_x_pixel
26979 - (total_pixel_width + dx);
26980 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26981 hlinfo->mouse_face_beg_x = 0;
26982 }
26983
26984 hlinfo->mouse_face_beg_row = vpos;
26985 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26986 hlinfo->mouse_face_beg_y = 0;
26987 hlinfo->mouse_face_end_y = 0;
26988 hlinfo->mouse_face_past_end = 0;
26989 hlinfo->mouse_face_window = window;
26990
26991 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26992 charpos,
26993 0, 0, 0,
26994 &ignore,
26995 glyph->face_id,
26996 1);
26997 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26998
26999 if (NILP (pointer))
27000 pointer = Qhand;
27001 }
27002 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27003 clear_mouse_face (hlinfo);
27004 }
27005 #ifdef HAVE_WINDOW_SYSTEM
27006 if (FRAME_WINDOW_P (f))
27007 define_frame_cursor1 (f, cursor, pointer);
27008 #endif
27009 }
27010
27011
27012 /* EXPORT:
27013 Take proper action when the mouse has moved to position X, Y on
27014 frame F as regards highlighting characters that have mouse-face
27015 properties. Also de-highlighting chars where the mouse was before.
27016 X and Y can be negative or out of range. */
27017
27018 void
27019 note_mouse_highlight (struct frame *f, int x, int y)
27020 {
27021 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27022 enum window_part part = ON_NOTHING;
27023 Lisp_Object window;
27024 struct window *w;
27025 Cursor cursor = No_Cursor;
27026 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27027 struct buffer *b;
27028
27029 /* When a menu is active, don't highlight because this looks odd. */
27030 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27031 if (popup_activated ())
27032 return;
27033 #endif
27034
27035 if (NILP (Vmouse_highlight)
27036 || !f->glyphs_initialized_p
27037 || f->pointer_invisible)
27038 return;
27039
27040 hlinfo->mouse_face_mouse_x = x;
27041 hlinfo->mouse_face_mouse_y = y;
27042 hlinfo->mouse_face_mouse_frame = f;
27043
27044 if (hlinfo->mouse_face_defer)
27045 return;
27046
27047 if (gc_in_progress)
27048 {
27049 hlinfo->mouse_face_deferred_gc = 1;
27050 return;
27051 }
27052
27053 /* Which window is that in? */
27054 window = window_from_coordinates (f, x, y, &part, 1);
27055
27056 /* If displaying active text in another window, clear that. */
27057 if (! EQ (window, hlinfo->mouse_face_window)
27058 /* Also clear if we move out of text area in same window. */
27059 || (!NILP (hlinfo->mouse_face_window)
27060 && !NILP (window)
27061 && part != ON_TEXT
27062 && part != ON_MODE_LINE
27063 && part != ON_HEADER_LINE))
27064 clear_mouse_face (hlinfo);
27065
27066 /* Not on a window -> return. */
27067 if (!WINDOWP (window))
27068 return;
27069
27070 /* Reset help_echo_string. It will get recomputed below. */
27071 help_echo_string = Qnil;
27072
27073 /* Convert to window-relative pixel coordinates. */
27074 w = XWINDOW (window);
27075 frame_to_window_pixel_xy (w, &x, &y);
27076
27077 #ifdef HAVE_WINDOW_SYSTEM
27078 /* Handle tool-bar window differently since it doesn't display a
27079 buffer. */
27080 if (EQ (window, f->tool_bar_window))
27081 {
27082 note_tool_bar_highlight (f, x, y);
27083 return;
27084 }
27085 #endif
27086
27087 /* Mouse is on the mode, header line or margin? */
27088 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27089 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27090 {
27091 note_mode_line_or_margin_highlight (window, x, y, part);
27092 return;
27093 }
27094
27095 #ifdef HAVE_WINDOW_SYSTEM
27096 if (part == ON_VERTICAL_BORDER)
27097 {
27098 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27099 help_echo_string = build_string ("drag-mouse-1: resize");
27100 }
27101 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27102 || part == ON_SCROLL_BAR)
27103 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27104 else
27105 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27106 #endif
27107
27108 /* Are we in a window whose display is up to date?
27109 And verify the buffer's text has not changed. */
27110 b = XBUFFER (w->buffer);
27111 if (part == ON_TEXT
27112 && EQ (w->window_end_valid, w->buffer)
27113 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27114 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27115 {
27116 int hpos, vpos, dx, dy, area = LAST_AREA;
27117 EMACS_INT pos;
27118 struct glyph *glyph;
27119 Lisp_Object object;
27120 Lisp_Object mouse_face = Qnil, position;
27121 Lisp_Object *overlay_vec = NULL;
27122 ptrdiff_t i, noverlays;
27123 struct buffer *obuf;
27124 EMACS_INT obegv, ozv;
27125 int same_region;
27126
27127 /* Find the glyph under X/Y. */
27128 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27129
27130 #ifdef HAVE_WINDOW_SYSTEM
27131 /* Look for :pointer property on image. */
27132 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27133 {
27134 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27135 if (img != NULL && IMAGEP (img->spec))
27136 {
27137 Lisp_Object image_map, hotspot;
27138 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27139 !NILP (image_map))
27140 && (hotspot = find_hot_spot (image_map,
27141 glyph->slice.img.x + dx,
27142 glyph->slice.img.y + dy),
27143 CONSP (hotspot))
27144 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27145 {
27146 Lisp_Object plist;
27147
27148 /* Could check XCAR (hotspot) to see if we enter/leave
27149 this hot-spot.
27150 If so, we could look for mouse-enter, mouse-leave
27151 properties in PLIST (and do something...). */
27152 hotspot = XCDR (hotspot);
27153 if (CONSP (hotspot)
27154 && (plist = XCAR (hotspot), CONSP (plist)))
27155 {
27156 pointer = Fplist_get (plist, Qpointer);
27157 if (NILP (pointer))
27158 pointer = Qhand;
27159 help_echo_string = Fplist_get (plist, Qhelp_echo);
27160 if (!NILP (help_echo_string))
27161 {
27162 help_echo_window = window;
27163 help_echo_object = glyph->object;
27164 help_echo_pos = glyph->charpos;
27165 }
27166 }
27167 }
27168 if (NILP (pointer))
27169 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27170 }
27171 }
27172 #endif /* HAVE_WINDOW_SYSTEM */
27173
27174 /* Clear mouse face if X/Y not over text. */
27175 if (glyph == NULL
27176 || area != TEXT_AREA
27177 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27178 /* Glyph's OBJECT is an integer for glyphs inserted by the
27179 display engine for its internal purposes, like truncation
27180 and continuation glyphs and blanks beyond the end of
27181 line's text on text terminals. If we are over such a
27182 glyph, we are not over any text. */
27183 || INTEGERP (glyph->object)
27184 /* R2L rows have a stretch glyph at their front, which
27185 stands for no text, whereas L2R rows have no glyphs at
27186 all beyond the end of text. Treat such stretch glyphs
27187 like we do with NULL glyphs in L2R rows. */
27188 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27189 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27190 && glyph->type == STRETCH_GLYPH
27191 && glyph->avoid_cursor_p))
27192 {
27193 if (clear_mouse_face (hlinfo))
27194 cursor = No_Cursor;
27195 #ifdef HAVE_WINDOW_SYSTEM
27196 if (FRAME_WINDOW_P (f) && NILP (pointer))
27197 {
27198 if (area != TEXT_AREA)
27199 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27200 else
27201 pointer = Vvoid_text_area_pointer;
27202 }
27203 #endif
27204 goto set_cursor;
27205 }
27206
27207 pos = glyph->charpos;
27208 object = glyph->object;
27209 if (!STRINGP (object) && !BUFFERP (object))
27210 goto set_cursor;
27211
27212 /* If we get an out-of-range value, return now; avoid an error. */
27213 if (BUFFERP (object) && pos > BUF_Z (b))
27214 goto set_cursor;
27215
27216 /* Make the window's buffer temporarily current for
27217 overlays_at and compute_char_face. */
27218 obuf = current_buffer;
27219 current_buffer = b;
27220 obegv = BEGV;
27221 ozv = ZV;
27222 BEGV = BEG;
27223 ZV = Z;
27224
27225 /* Is this char mouse-active or does it have help-echo? */
27226 position = make_number (pos);
27227
27228 if (BUFFERP (object))
27229 {
27230 /* Put all the overlays we want in a vector in overlay_vec. */
27231 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27232 /* Sort overlays into increasing priority order. */
27233 noverlays = sort_overlays (overlay_vec, noverlays, w);
27234 }
27235 else
27236 noverlays = 0;
27237
27238 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27239
27240 if (same_region)
27241 cursor = No_Cursor;
27242
27243 /* Check mouse-face highlighting. */
27244 if (! same_region
27245 /* If there exists an overlay with mouse-face overlapping
27246 the one we are currently highlighting, we have to
27247 check if we enter the overlapping overlay, and then
27248 highlight only that. */
27249 || (OVERLAYP (hlinfo->mouse_face_overlay)
27250 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27251 {
27252 /* Find the highest priority overlay with a mouse-face. */
27253 Lisp_Object overlay = Qnil;
27254 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27255 {
27256 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27257 if (!NILP (mouse_face))
27258 overlay = overlay_vec[i];
27259 }
27260
27261 /* If we're highlighting the same overlay as before, there's
27262 no need to do that again. */
27263 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27264 goto check_help_echo;
27265 hlinfo->mouse_face_overlay = overlay;
27266
27267 /* Clear the display of the old active region, if any. */
27268 if (clear_mouse_face (hlinfo))
27269 cursor = No_Cursor;
27270
27271 /* If no overlay applies, get a text property. */
27272 if (NILP (overlay))
27273 mouse_face = Fget_text_property (position, Qmouse_face, object);
27274
27275 /* Next, compute the bounds of the mouse highlighting and
27276 display it. */
27277 if (!NILP (mouse_face) && STRINGP (object))
27278 {
27279 /* The mouse-highlighting comes from a display string
27280 with a mouse-face. */
27281 Lisp_Object s, e;
27282 EMACS_INT ignore;
27283
27284 s = Fprevious_single_property_change
27285 (make_number (pos + 1), Qmouse_face, object, Qnil);
27286 e = Fnext_single_property_change
27287 (position, Qmouse_face, object, Qnil);
27288 if (NILP (s))
27289 s = make_number (0);
27290 if (NILP (e))
27291 e = make_number (SCHARS (object) - 1);
27292 mouse_face_from_string_pos (w, hlinfo, object,
27293 XINT (s), XINT (e));
27294 hlinfo->mouse_face_past_end = 0;
27295 hlinfo->mouse_face_window = window;
27296 hlinfo->mouse_face_face_id
27297 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27298 glyph->face_id, 1);
27299 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27300 cursor = No_Cursor;
27301 }
27302 else
27303 {
27304 /* The mouse-highlighting, if any, comes from an overlay
27305 or text property in the buffer. */
27306 Lisp_Object buffer IF_LINT (= Qnil);
27307 Lisp_Object disp_string IF_LINT (= Qnil);
27308
27309 if (STRINGP (object))
27310 {
27311 /* If we are on a display string with no mouse-face,
27312 check if the text under it has one. */
27313 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27314 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27315 pos = string_buffer_position (object, start);
27316 if (pos > 0)
27317 {
27318 mouse_face = get_char_property_and_overlay
27319 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27320 buffer = w->buffer;
27321 disp_string = object;
27322 }
27323 }
27324 else
27325 {
27326 buffer = object;
27327 disp_string = Qnil;
27328 }
27329
27330 if (!NILP (mouse_face))
27331 {
27332 Lisp_Object before, after;
27333 Lisp_Object before_string, after_string;
27334 /* To correctly find the limits of mouse highlight
27335 in a bidi-reordered buffer, we must not use the
27336 optimization of limiting the search in
27337 previous-single-property-change and
27338 next-single-property-change, because
27339 rows_from_pos_range needs the real start and end
27340 positions to DTRT in this case. That's because
27341 the first row visible in a window does not
27342 necessarily display the character whose position
27343 is the smallest. */
27344 Lisp_Object lim1 =
27345 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27346 ? Fmarker_position (w->start)
27347 : Qnil;
27348 Lisp_Object lim2 =
27349 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27350 ? make_number (BUF_Z (XBUFFER (buffer))
27351 - XFASTINT (w->window_end_pos))
27352 : Qnil;
27353
27354 if (NILP (overlay))
27355 {
27356 /* Handle the text property case. */
27357 before = Fprevious_single_property_change
27358 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27359 after = Fnext_single_property_change
27360 (make_number (pos), Qmouse_face, buffer, lim2);
27361 before_string = after_string = Qnil;
27362 }
27363 else
27364 {
27365 /* Handle the overlay case. */
27366 before = Foverlay_start (overlay);
27367 after = Foverlay_end (overlay);
27368 before_string = Foverlay_get (overlay, Qbefore_string);
27369 after_string = Foverlay_get (overlay, Qafter_string);
27370
27371 if (!STRINGP (before_string)) before_string = Qnil;
27372 if (!STRINGP (after_string)) after_string = Qnil;
27373 }
27374
27375 mouse_face_from_buffer_pos (window, hlinfo, pos,
27376 NILP (before)
27377 ? 1
27378 : XFASTINT (before),
27379 NILP (after)
27380 ? BUF_Z (XBUFFER (buffer))
27381 : XFASTINT (after),
27382 before_string, after_string,
27383 disp_string);
27384 cursor = No_Cursor;
27385 }
27386 }
27387 }
27388
27389 check_help_echo:
27390
27391 /* Look for a `help-echo' property. */
27392 if (NILP (help_echo_string)) {
27393 Lisp_Object help, overlay;
27394
27395 /* Check overlays first. */
27396 help = overlay = Qnil;
27397 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27398 {
27399 overlay = overlay_vec[i];
27400 help = Foverlay_get (overlay, Qhelp_echo);
27401 }
27402
27403 if (!NILP (help))
27404 {
27405 help_echo_string = help;
27406 help_echo_window = window;
27407 help_echo_object = overlay;
27408 help_echo_pos = pos;
27409 }
27410 else
27411 {
27412 Lisp_Object obj = glyph->object;
27413 EMACS_INT charpos = glyph->charpos;
27414
27415 /* Try text properties. */
27416 if (STRINGP (obj)
27417 && charpos >= 0
27418 && charpos < SCHARS (obj))
27419 {
27420 help = Fget_text_property (make_number (charpos),
27421 Qhelp_echo, obj);
27422 if (NILP (help))
27423 {
27424 /* If the string itself doesn't specify a help-echo,
27425 see if the buffer text ``under'' it does. */
27426 struct glyph_row *r
27427 = MATRIX_ROW (w->current_matrix, vpos);
27428 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27429 EMACS_INT p = string_buffer_position (obj, start);
27430 if (p > 0)
27431 {
27432 help = Fget_char_property (make_number (p),
27433 Qhelp_echo, w->buffer);
27434 if (!NILP (help))
27435 {
27436 charpos = p;
27437 obj = w->buffer;
27438 }
27439 }
27440 }
27441 }
27442 else if (BUFFERP (obj)
27443 && charpos >= BEGV
27444 && charpos < ZV)
27445 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27446 obj);
27447
27448 if (!NILP (help))
27449 {
27450 help_echo_string = help;
27451 help_echo_window = window;
27452 help_echo_object = obj;
27453 help_echo_pos = charpos;
27454 }
27455 }
27456 }
27457
27458 #ifdef HAVE_WINDOW_SYSTEM
27459 /* Look for a `pointer' property. */
27460 if (FRAME_WINDOW_P (f) && NILP (pointer))
27461 {
27462 /* Check overlays first. */
27463 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27464 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27465
27466 if (NILP (pointer))
27467 {
27468 Lisp_Object obj = glyph->object;
27469 EMACS_INT charpos = glyph->charpos;
27470
27471 /* Try text properties. */
27472 if (STRINGP (obj)
27473 && charpos >= 0
27474 && charpos < SCHARS (obj))
27475 {
27476 pointer = Fget_text_property (make_number (charpos),
27477 Qpointer, obj);
27478 if (NILP (pointer))
27479 {
27480 /* If the string itself doesn't specify a pointer,
27481 see if the buffer text ``under'' it does. */
27482 struct glyph_row *r
27483 = MATRIX_ROW (w->current_matrix, vpos);
27484 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27485 EMACS_INT p = string_buffer_position (obj, start);
27486 if (p > 0)
27487 pointer = Fget_char_property (make_number (p),
27488 Qpointer, w->buffer);
27489 }
27490 }
27491 else if (BUFFERP (obj)
27492 && charpos >= BEGV
27493 && charpos < ZV)
27494 pointer = Fget_text_property (make_number (charpos),
27495 Qpointer, obj);
27496 }
27497 }
27498 #endif /* HAVE_WINDOW_SYSTEM */
27499
27500 BEGV = obegv;
27501 ZV = ozv;
27502 current_buffer = obuf;
27503 }
27504
27505 set_cursor:
27506
27507 #ifdef HAVE_WINDOW_SYSTEM
27508 if (FRAME_WINDOW_P (f))
27509 define_frame_cursor1 (f, cursor, pointer);
27510 #else
27511 /* This is here to prevent a compiler error, about "label at end of
27512 compound statement". */
27513 return;
27514 #endif
27515 }
27516
27517
27518 /* EXPORT for RIF:
27519 Clear any mouse-face on window W. This function is part of the
27520 redisplay interface, and is called from try_window_id and similar
27521 functions to ensure the mouse-highlight is off. */
27522
27523 void
27524 x_clear_window_mouse_face (struct window *w)
27525 {
27526 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27527 Lisp_Object window;
27528
27529 BLOCK_INPUT;
27530 XSETWINDOW (window, w);
27531 if (EQ (window, hlinfo->mouse_face_window))
27532 clear_mouse_face (hlinfo);
27533 UNBLOCK_INPUT;
27534 }
27535
27536
27537 /* EXPORT:
27538 Just discard the mouse face information for frame F, if any.
27539 This is used when the size of F is changed. */
27540
27541 void
27542 cancel_mouse_face (struct frame *f)
27543 {
27544 Lisp_Object window;
27545 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27546
27547 window = hlinfo->mouse_face_window;
27548 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27549 {
27550 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27551 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27552 hlinfo->mouse_face_window = Qnil;
27553 }
27554 }
27555
27556
27557 \f
27558 /***********************************************************************
27559 Exposure Events
27560 ***********************************************************************/
27561
27562 #ifdef HAVE_WINDOW_SYSTEM
27563
27564 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27565 which intersects rectangle R. R is in window-relative coordinates. */
27566
27567 static void
27568 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27569 enum glyph_row_area area)
27570 {
27571 struct glyph *first = row->glyphs[area];
27572 struct glyph *end = row->glyphs[area] + row->used[area];
27573 struct glyph *last;
27574 int first_x, start_x, x;
27575
27576 if (area == TEXT_AREA && row->fill_line_p)
27577 /* If row extends face to end of line write the whole line. */
27578 draw_glyphs (w, 0, row, area,
27579 0, row->used[area],
27580 DRAW_NORMAL_TEXT, 0);
27581 else
27582 {
27583 /* Set START_X to the window-relative start position for drawing glyphs of
27584 AREA. The first glyph of the text area can be partially visible.
27585 The first glyphs of other areas cannot. */
27586 start_x = window_box_left_offset (w, area);
27587 x = start_x;
27588 if (area == TEXT_AREA)
27589 x += row->x;
27590
27591 /* Find the first glyph that must be redrawn. */
27592 while (first < end
27593 && x + first->pixel_width < r->x)
27594 {
27595 x += first->pixel_width;
27596 ++first;
27597 }
27598
27599 /* Find the last one. */
27600 last = first;
27601 first_x = x;
27602 while (last < end
27603 && x < r->x + r->width)
27604 {
27605 x += last->pixel_width;
27606 ++last;
27607 }
27608
27609 /* Repaint. */
27610 if (last > first)
27611 draw_glyphs (w, first_x - start_x, row, area,
27612 first - row->glyphs[area], last - row->glyphs[area],
27613 DRAW_NORMAL_TEXT, 0);
27614 }
27615 }
27616
27617
27618 /* Redraw the parts of the glyph row ROW on window W intersecting
27619 rectangle R. R is in window-relative coordinates. Value is
27620 non-zero if mouse-face was overwritten. */
27621
27622 static int
27623 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27624 {
27625 xassert (row->enabled_p);
27626
27627 if (row->mode_line_p || w->pseudo_window_p)
27628 draw_glyphs (w, 0, row, TEXT_AREA,
27629 0, row->used[TEXT_AREA],
27630 DRAW_NORMAL_TEXT, 0);
27631 else
27632 {
27633 if (row->used[LEFT_MARGIN_AREA])
27634 expose_area (w, row, r, LEFT_MARGIN_AREA);
27635 if (row->used[TEXT_AREA])
27636 expose_area (w, row, r, TEXT_AREA);
27637 if (row->used[RIGHT_MARGIN_AREA])
27638 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27639 draw_row_fringe_bitmaps (w, row);
27640 }
27641
27642 return row->mouse_face_p;
27643 }
27644
27645
27646 /* Redraw those parts of glyphs rows during expose event handling that
27647 overlap other rows. Redrawing of an exposed line writes over parts
27648 of lines overlapping that exposed line; this function fixes that.
27649
27650 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27651 row in W's current matrix that is exposed and overlaps other rows.
27652 LAST_OVERLAPPING_ROW is the last such row. */
27653
27654 static void
27655 expose_overlaps (struct window *w,
27656 struct glyph_row *first_overlapping_row,
27657 struct glyph_row *last_overlapping_row,
27658 XRectangle *r)
27659 {
27660 struct glyph_row *row;
27661
27662 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27663 if (row->overlapping_p)
27664 {
27665 xassert (row->enabled_p && !row->mode_line_p);
27666
27667 row->clip = r;
27668 if (row->used[LEFT_MARGIN_AREA])
27669 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27670
27671 if (row->used[TEXT_AREA])
27672 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27673
27674 if (row->used[RIGHT_MARGIN_AREA])
27675 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27676 row->clip = NULL;
27677 }
27678 }
27679
27680
27681 /* Return non-zero if W's cursor intersects rectangle R. */
27682
27683 static int
27684 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27685 {
27686 XRectangle cr, result;
27687 struct glyph *cursor_glyph;
27688 struct glyph_row *row;
27689
27690 if (w->phys_cursor.vpos >= 0
27691 && w->phys_cursor.vpos < w->current_matrix->nrows
27692 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27693 row->enabled_p)
27694 && row->cursor_in_fringe_p)
27695 {
27696 /* Cursor is in the fringe. */
27697 cr.x = window_box_right_offset (w,
27698 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27699 ? RIGHT_MARGIN_AREA
27700 : TEXT_AREA));
27701 cr.y = row->y;
27702 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27703 cr.height = row->height;
27704 return x_intersect_rectangles (&cr, r, &result);
27705 }
27706
27707 cursor_glyph = get_phys_cursor_glyph (w);
27708 if (cursor_glyph)
27709 {
27710 /* r is relative to W's box, but w->phys_cursor.x is relative
27711 to left edge of W's TEXT area. Adjust it. */
27712 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27713 cr.y = w->phys_cursor.y;
27714 cr.width = cursor_glyph->pixel_width;
27715 cr.height = w->phys_cursor_height;
27716 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27717 I assume the effect is the same -- and this is portable. */
27718 return x_intersect_rectangles (&cr, r, &result);
27719 }
27720 /* If we don't understand the format, pretend we're not in the hot-spot. */
27721 return 0;
27722 }
27723
27724
27725 /* EXPORT:
27726 Draw a vertical window border to the right of window W if W doesn't
27727 have vertical scroll bars. */
27728
27729 void
27730 x_draw_vertical_border (struct window *w)
27731 {
27732 struct frame *f = XFRAME (WINDOW_FRAME (w));
27733
27734 /* We could do better, if we knew what type of scroll-bar the adjacent
27735 windows (on either side) have... But we don't :-(
27736 However, I think this works ok. ++KFS 2003-04-25 */
27737
27738 /* Redraw borders between horizontally adjacent windows. Don't
27739 do it for frames with vertical scroll bars because either the
27740 right scroll bar of a window, or the left scroll bar of its
27741 neighbor will suffice as a border. */
27742 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27743 return;
27744
27745 if (!WINDOW_RIGHTMOST_P (w)
27746 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27747 {
27748 int x0, x1, y0, y1;
27749
27750 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27751 y1 -= 1;
27752
27753 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27754 x1 -= 1;
27755
27756 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27757 }
27758 else if (!WINDOW_LEFTMOST_P (w)
27759 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27760 {
27761 int x0, x1, y0, y1;
27762
27763 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27764 y1 -= 1;
27765
27766 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27767 x0 -= 1;
27768
27769 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27770 }
27771 }
27772
27773
27774 /* Redraw the part of window W intersection rectangle FR. Pixel
27775 coordinates in FR are frame-relative. Call this function with
27776 input blocked. Value is non-zero if the exposure overwrites
27777 mouse-face. */
27778
27779 static int
27780 expose_window (struct window *w, XRectangle *fr)
27781 {
27782 struct frame *f = XFRAME (w->frame);
27783 XRectangle wr, r;
27784 int mouse_face_overwritten_p = 0;
27785
27786 /* If window is not yet fully initialized, do nothing. This can
27787 happen when toolkit scroll bars are used and a window is split.
27788 Reconfiguring the scroll bar will generate an expose for a newly
27789 created window. */
27790 if (w->current_matrix == NULL)
27791 return 0;
27792
27793 /* When we're currently updating the window, display and current
27794 matrix usually don't agree. Arrange for a thorough display
27795 later. */
27796 if (w == updated_window)
27797 {
27798 SET_FRAME_GARBAGED (f);
27799 return 0;
27800 }
27801
27802 /* Frame-relative pixel rectangle of W. */
27803 wr.x = WINDOW_LEFT_EDGE_X (w);
27804 wr.y = WINDOW_TOP_EDGE_Y (w);
27805 wr.width = WINDOW_TOTAL_WIDTH (w);
27806 wr.height = WINDOW_TOTAL_HEIGHT (w);
27807
27808 if (x_intersect_rectangles (fr, &wr, &r))
27809 {
27810 int yb = window_text_bottom_y (w);
27811 struct glyph_row *row;
27812 int cursor_cleared_p, phys_cursor_on_p;
27813 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27814
27815 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27816 r.x, r.y, r.width, r.height));
27817
27818 /* Convert to window coordinates. */
27819 r.x -= WINDOW_LEFT_EDGE_X (w);
27820 r.y -= WINDOW_TOP_EDGE_Y (w);
27821
27822 /* Turn off the cursor. */
27823 if (!w->pseudo_window_p
27824 && phys_cursor_in_rect_p (w, &r))
27825 {
27826 x_clear_cursor (w);
27827 cursor_cleared_p = 1;
27828 }
27829 else
27830 cursor_cleared_p = 0;
27831
27832 /* If the row containing the cursor extends face to end of line,
27833 then expose_area might overwrite the cursor outside the
27834 rectangle and thus notice_overwritten_cursor might clear
27835 w->phys_cursor_on_p. We remember the original value and
27836 check later if it is changed. */
27837 phys_cursor_on_p = w->phys_cursor_on_p;
27838
27839 /* Update lines intersecting rectangle R. */
27840 first_overlapping_row = last_overlapping_row = NULL;
27841 for (row = w->current_matrix->rows;
27842 row->enabled_p;
27843 ++row)
27844 {
27845 int y0 = row->y;
27846 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27847
27848 if ((y0 >= r.y && y0 < r.y + r.height)
27849 || (y1 > r.y && y1 < r.y + r.height)
27850 || (r.y >= y0 && r.y < y1)
27851 || (r.y + r.height > y0 && r.y + r.height < y1))
27852 {
27853 /* A header line may be overlapping, but there is no need
27854 to fix overlapping areas for them. KFS 2005-02-12 */
27855 if (row->overlapping_p && !row->mode_line_p)
27856 {
27857 if (first_overlapping_row == NULL)
27858 first_overlapping_row = row;
27859 last_overlapping_row = row;
27860 }
27861
27862 row->clip = fr;
27863 if (expose_line (w, row, &r))
27864 mouse_face_overwritten_p = 1;
27865 row->clip = NULL;
27866 }
27867 else if (row->overlapping_p)
27868 {
27869 /* We must redraw a row overlapping the exposed area. */
27870 if (y0 < r.y
27871 ? y0 + row->phys_height > r.y
27872 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27873 {
27874 if (first_overlapping_row == NULL)
27875 first_overlapping_row = row;
27876 last_overlapping_row = row;
27877 }
27878 }
27879
27880 if (y1 >= yb)
27881 break;
27882 }
27883
27884 /* Display the mode line if there is one. */
27885 if (WINDOW_WANTS_MODELINE_P (w)
27886 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27887 row->enabled_p)
27888 && row->y < r.y + r.height)
27889 {
27890 if (expose_line (w, row, &r))
27891 mouse_face_overwritten_p = 1;
27892 }
27893
27894 if (!w->pseudo_window_p)
27895 {
27896 /* Fix the display of overlapping rows. */
27897 if (first_overlapping_row)
27898 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27899 fr);
27900
27901 /* Draw border between windows. */
27902 x_draw_vertical_border (w);
27903
27904 /* Turn the cursor on again. */
27905 if (cursor_cleared_p
27906 || (phys_cursor_on_p && !w->phys_cursor_on_p))
27907 update_window_cursor (w, 1);
27908 }
27909 }
27910
27911 return mouse_face_overwritten_p;
27912 }
27913
27914
27915
27916 /* Redraw (parts) of all windows in the window tree rooted at W that
27917 intersect R. R contains frame pixel coordinates. Value is
27918 non-zero if the exposure overwrites mouse-face. */
27919
27920 static int
27921 expose_window_tree (struct window *w, XRectangle *r)
27922 {
27923 struct frame *f = XFRAME (w->frame);
27924 int mouse_face_overwritten_p = 0;
27925
27926 while (w && !FRAME_GARBAGED_P (f))
27927 {
27928 if (!NILP (w->hchild))
27929 mouse_face_overwritten_p
27930 |= expose_window_tree (XWINDOW (w->hchild), r);
27931 else if (!NILP (w->vchild))
27932 mouse_face_overwritten_p
27933 |= expose_window_tree (XWINDOW (w->vchild), r);
27934 else
27935 mouse_face_overwritten_p |= expose_window (w, r);
27936
27937 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27938 }
27939
27940 return mouse_face_overwritten_p;
27941 }
27942
27943
27944 /* EXPORT:
27945 Redisplay an exposed area of frame F. X and Y are the upper-left
27946 corner of the exposed rectangle. W and H are width and height of
27947 the exposed area. All are pixel values. W or H zero means redraw
27948 the entire frame. */
27949
27950 void
27951 expose_frame (struct frame *f, int x, int y, int w, int h)
27952 {
27953 XRectangle r;
27954 int mouse_face_overwritten_p = 0;
27955
27956 TRACE ((stderr, "expose_frame "));
27957
27958 /* No need to redraw if frame will be redrawn soon. */
27959 if (FRAME_GARBAGED_P (f))
27960 {
27961 TRACE ((stderr, " garbaged\n"));
27962 return;
27963 }
27964
27965 /* If basic faces haven't been realized yet, there is no point in
27966 trying to redraw anything. This can happen when we get an expose
27967 event while Emacs is starting, e.g. by moving another window. */
27968 if (FRAME_FACE_CACHE (f) == NULL
27969 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27970 {
27971 TRACE ((stderr, " no faces\n"));
27972 return;
27973 }
27974
27975 if (w == 0 || h == 0)
27976 {
27977 r.x = r.y = 0;
27978 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27979 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27980 }
27981 else
27982 {
27983 r.x = x;
27984 r.y = y;
27985 r.width = w;
27986 r.height = h;
27987 }
27988
27989 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27990 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27991
27992 if (WINDOWP (f->tool_bar_window))
27993 mouse_face_overwritten_p
27994 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27995
27996 #ifdef HAVE_X_WINDOWS
27997 #ifndef MSDOS
27998 #ifndef USE_X_TOOLKIT
27999 if (WINDOWP (f->menu_bar_window))
28000 mouse_face_overwritten_p
28001 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28002 #endif /* not USE_X_TOOLKIT */
28003 #endif
28004 #endif
28005
28006 /* Some window managers support a focus-follows-mouse style with
28007 delayed raising of frames. Imagine a partially obscured frame,
28008 and moving the mouse into partially obscured mouse-face on that
28009 frame. The visible part of the mouse-face will be highlighted,
28010 then the WM raises the obscured frame. With at least one WM, KDE
28011 2.1, Emacs is not getting any event for the raising of the frame
28012 (even tried with SubstructureRedirectMask), only Expose events.
28013 These expose events will draw text normally, i.e. not
28014 highlighted. Which means we must redo the highlight here.
28015 Subsume it under ``we love X''. --gerd 2001-08-15 */
28016 /* Included in Windows version because Windows most likely does not
28017 do the right thing if any third party tool offers
28018 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28019 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28020 {
28021 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28022 if (f == hlinfo->mouse_face_mouse_frame)
28023 {
28024 int mouse_x = hlinfo->mouse_face_mouse_x;
28025 int mouse_y = hlinfo->mouse_face_mouse_y;
28026 clear_mouse_face (hlinfo);
28027 note_mouse_highlight (f, mouse_x, mouse_y);
28028 }
28029 }
28030 }
28031
28032
28033 /* EXPORT:
28034 Determine the intersection of two rectangles R1 and R2. Return
28035 the intersection in *RESULT. Value is non-zero if RESULT is not
28036 empty. */
28037
28038 int
28039 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28040 {
28041 XRectangle *left, *right;
28042 XRectangle *upper, *lower;
28043 int intersection_p = 0;
28044
28045 /* Rearrange so that R1 is the left-most rectangle. */
28046 if (r1->x < r2->x)
28047 left = r1, right = r2;
28048 else
28049 left = r2, right = r1;
28050
28051 /* X0 of the intersection is right.x0, if this is inside R1,
28052 otherwise there is no intersection. */
28053 if (right->x <= left->x + left->width)
28054 {
28055 result->x = right->x;
28056
28057 /* The right end of the intersection is the minimum of
28058 the right ends of left and right. */
28059 result->width = (min (left->x + left->width, right->x + right->width)
28060 - result->x);
28061
28062 /* Same game for Y. */
28063 if (r1->y < r2->y)
28064 upper = r1, lower = r2;
28065 else
28066 upper = r2, lower = r1;
28067
28068 /* The upper end of the intersection is lower.y0, if this is inside
28069 of upper. Otherwise, there is no intersection. */
28070 if (lower->y <= upper->y + upper->height)
28071 {
28072 result->y = lower->y;
28073
28074 /* The lower end of the intersection is the minimum of the lower
28075 ends of upper and lower. */
28076 result->height = (min (lower->y + lower->height,
28077 upper->y + upper->height)
28078 - result->y);
28079 intersection_p = 1;
28080 }
28081 }
28082
28083 return intersection_p;
28084 }
28085
28086 #endif /* HAVE_WINDOW_SYSTEM */
28087
28088 \f
28089 /***********************************************************************
28090 Initialization
28091 ***********************************************************************/
28092
28093 void
28094 syms_of_xdisp (void)
28095 {
28096 Vwith_echo_area_save_vector = Qnil;
28097 staticpro (&Vwith_echo_area_save_vector);
28098
28099 Vmessage_stack = Qnil;
28100 staticpro (&Vmessage_stack);
28101
28102 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28103
28104 message_dolog_marker1 = Fmake_marker ();
28105 staticpro (&message_dolog_marker1);
28106 message_dolog_marker2 = Fmake_marker ();
28107 staticpro (&message_dolog_marker2);
28108 message_dolog_marker3 = Fmake_marker ();
28109 staticpro (&message_dolog_marker3);
28110
28111 #if GLYPH_DEBUG
28112 defsubr (&Sdump_frame_glyph_matrix);
28113 defsubr (&Sdump_glyph_matrix);
28114 defsubr (&Sdump_glyph_row);
28115 defsubr (&Sdump_tool_bar_row);
28116 defsubr (&Strace_redisplay);
28117 defsubr (&Strace_to_stderr);
28118 #endif
28119 #ifdef HAVE_WINDOW_SYSTEM
28120 defsubr (&Stool_bar_lines_needed);
28121 defsubr (&Slookup_image_map);
28122 #endif
28123 defsubr (&Sformat_mode_line);
28124 defsubr (&Sinvisible_p);
28125 defsubr (&Scurrent_bidi_paragraph_direction);
28126
28127 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28128 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28129 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28130 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28131 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28132 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28133 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28134 DEFSYM (Qeval, "eval");
28135 DEFSYM (QCdata, ":data");
28136 DEFSYM (Qdisplay, "display");
28137 DEFSYM (Qspace_width, "space-width");
28138 DEFSYM (Qraise, "raise");
28139 DEFSYM (Qslice, "slice");
28140 DEFSYM (Qspace, "space");
28141 DEFSYM (Qmargin, "margin");
28142 DEFSYM (Qpointer, "pointer");
28143 DEFSYM (Qleft_margin, "left-margin");
28144 DEFSYM (Qright_margin, "right-margin");
28145 DEFSYM (Qcenter, "center");
28146 DEFSYM (Qline_height, "line-height");
28147 DEFSYM (QCalign_to, ":align-to");
28148 DEFSYM (QCrelative_width, ":relative-width");
28149 DEFSYM (QCrelative_height, ":relative-height");
28150 DEFSYM (QCeval, ":eval");
28151 DEFSYM (QCpropertize, ":propertize");
28152 DEFSYM (QCfile, ":file");
28153 DEFSYM (Qfontified, "fontified");
28154 DEFSYM (Qfontification_functions, "fontification-functions");
28155 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28156 DEFSYM (Qescape_glyph, "escape-glyph");
28157 DEFSYM (Qnobreak_space, "nobreak-space");
28158 DEFSYM (Qimage, "image");
28159 DEFSYM (Qtext, "text");
28160 DEFSYM (Qboth, "both");
28161 DEFSYM (Qboth_horiz, "both-horiz");
28162 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28163 DEFSYM (QCmap, ":map");
28164 DEFSYM (QCpointer, ":pointer");
28165 DEFSYM (Qrect, "rect");
28166 DEFSYM (Qcircle, "circle");
28167 DEFSYM (Qpoly, "poly");
28168 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28169 DEFSYM (Qgrow_only, "grow-only");
28170 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28171 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28172 DEFSYM (Qposition, "position");
28173 DEFSYM (Qbuffer_position, "buffer-position");
28174 DEFSYM (Qobject, "object");
28175 DEFSYM (Qbar, "bar");
28176 DEFSYM (Qhbar, "hbar");
28177 DEFSYM (Qbox, "box");
28178 DEFSYM (Qhollow, "hollow");
28179 DEFSYM (Qhand, "hand");
28180 DEFSYM (Qarrow, "arrow");
28181 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28182
28183 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28184 Fcons (intern_c_string ("void-variable"), Qnil)),
28185 Qnil);
28186 staticpro (&list_of_error);
28187
28188 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28189 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28190 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28191 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28192
28193 echo_buffer[0] = echo_buffer[1] = Qnil;
28194 staticpro (&echo_buffer[0]);
28195 staticpro (&echo_buffer[1]);
28196
28197 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28198 staticpro (&echo_area_buffer[0]);
28199 staticpro (&echo_area_buffer[1]);
28200
28201 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28202 staticpro (&Vmessages_buffer_name);
28203
28204 mode_line_proptrans_alist = Qnil;
28205 staticpro (&mode_line_proptrans_alist);
28206 mode_line_string_list = Qnil;
28207 staticpro (&mode_line_string_list);
28208 mode_line_string_face = Qnil;
28209 staticpro (&mode_line_string_face);
28210 mode_line_string_face_prop = Qnil;
28211 staticpro (&mode_line_string_face_prop);
28212 Vmode_line_unwind_vector = Qnil;
28213 staticpro (&Vmode_line_unwind_vector);
28214
28215 help_echo_string = Qnil;
28216 staticpro (&help_echo_string);
28217 help_echo_object = Qnil;
28218 staticpro (&help_echo_object);
28219 help_echo_window = Qnil;
28220 staticpro (&help_echo_window);
28221 previous_help_echo_string = Qnil;
28222 staticpro (&previous_help_echo_string);
28223 help_echo_pos = -1;
28224
28225 DEFSYM (Qright_to_left, "right-to-left");
28226 DEFSYM (Qleft_to_right, "left-to-right");
28227
28228 #ifdef HAVE_WINDOW_SYSTEM
28229 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28230 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
28231 For example, if a block cursor is over a tab, it will be drawn as
28232 wide as that tab on the display. */);
28233 x_stretch_cursor_p = 0;
28234 #endif
28235
28236 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28237 doc: /* *Non-nil means highlight trailing whitespace.
28238 The face used for trailing whitespace is `trailing-whitespace'. */);
28239 Vshow_trailing_whitespace = Qnil;
28240
28241 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28242 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28243 If the value is t, Emacs highlights non-ASCII chars which have the
28244 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28245 or `escape-glyph' face respectively.
28246
28247 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28248 U+2011 (non-breaking hyphen) are affected.
28249
28250 Any other non-nil value means to display these characters as a escape
28251 glyph followed by an ordinary space or hyphen.
28252
28253 A value of nil means no special handling of these characters. */);
28254 Vnobreak_char_display = Qt;
28255
28256 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28257 doc: /* *The pointer shape to show in void text areas.
28258 A value of nil means to show the text pointer. Other options are `arrow',
28259 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28260 Vvoid_text_area_pointer = Qarrow;
28261
28262 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28263 doc: /* Non-nil means don't actually do any redisplay.
28264 This is used for internal purposes. */);
28265 Vinhibit_redisplay = Qnil;
28266
28267 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28268 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28269 Vglobal_mode_string = Qnil;
28270
28271 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28272 doc: /* Marker for where to display an arrow on top of the buffer text.
28273 This must be the beginning of a line in order to work.
28274 See also `overlay-arrow-string'. */);
28275 Voverlay_arrow_position = Qnil;
28276
28277 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28278 doc: /* String to display as an arrow in non-window frames.
28279 See also `overlay-arrow-position'. */);
28280 Voverlay_arrow_string = make_pure_c_string ("=>");
28281
28282 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28283 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28284 The symbols on this list are examined during redisplay to determine
28285 where to display overlay arrows. */);
28286 Voverlay_arrow_variable_list
28287 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28288
28289 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28290 doc: /* *The number of lines to try scrolling a window by when point moves out.
28291 If that fails to bring point back on frame, point is centered instead.
28292 If this is zero, point is always centered after it moves off frame.
28293 If you want scrolling to always be a line at a time, you should set
28294 `scroll-conservatively' to a large value rather than set this to 1. */);
28295
28296 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28297 doc: /* *Scroll up to this many lines, to bring point back on screen.
28298 If point moves off-screen, redisplay will scroll by up to
28299 `scroll-conservatively' lines in order to bring point just barely
28300 onto the screen again. If that cannot be done, then redisplay
28301 recenters point as usual.
28302
28303 If the value is greater than 100, redisplay will never recenter point,
28304 but will always scroll just enough text to bring point into view, even
28305 if you move far away.
28306
28307 A value of zero means always recenter point if it moves off screen. */);
28308 scroll_conservatively = 0;
28309
28310 DEFVAR_INT ("scroll-margin", scroll_margin,
28311 doc: /* *Number of lines of margin at the top and bottom of a window.
28312 Recenter the window whenever point gets within this many lines
28313 of the top or bottom of the window. */);
28314 scroll_margin = 0;
28315
28316 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28317 doc: /* Pixels per inch value for non-window system displays.
28318 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28319 Vdisplay_pixels_per_inch = make_float (72.0);
28320
28321 #if GLYPH_DEBUG
28322 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28323 #endif
28324
28325 DEFVAR_LISP ("truncate-partial-width-windows",
28326 Vtruncate_partial_width_windows,
28327 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28328 For an integer value, truncate lines in each window narrower than the
28329 full frame width, provided the window width is less than that integer;
28330 otherwise, respect the value of `truncate-lines'.
28331
28332 For any other non-nil value, truncate lines in all windows that do
28333 not span the full frame width.
28334
28335 A value of nil means to respect the value of `truncate-lines'.
28336
28337 If `word-wrap' is enabled, you might want to reduce this. */);
28338 Vtruncate_partial_width_windows = make_number (50);
28339
28340 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28341 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28342 Any other value means to use the appropriate face, `mode-line',
28343 `header-line', or `menu' respectively. */);
28344 mode_line_inverse_video = 1;
28345
28346 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28347 doc: /* *Maximum buffer size for which line number should be displayed.
28348 If the buffer is bigger than this, the line number does not appear
28349 in the mode line. A value of nil means no limit. */);
28350 Vline_number_display_limit = Qnil;
28351
28352 DEFVAR_INT ("line-number-display-limit-width",
28353 line_number_display_limit_width,
28354 doc: /* *Maximum line width (in characters) for line number display.
28355 If the average length of the lines near point is bigger than this, then the
28356 line number may be omitted from the mode line. */);
28357 line_number_display_limit_width = 200;
28358
28359 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28360 doc: /* *Non-nil means highlight region even in nonselected windows. */);
28361 highlight_nonselected_windows = 0;
28362
28363 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28364 doc: /* Non-nil if more than one frame is visible on this display.
28365 Minibuffer-only frames don't count, but iconified frames do.
28366 This variable is not guaranteed to be accurate except while processing
28367 `frame-title-format' and `icon-title-format'. */);
28368
28369 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28370 doc: /* Template for displaying the title bar of visible frames.
28371 \(Assuming the window manager supports this feature.)
28372
28373 This variable has the same structure as `mode-line-format', except that
28374 the %c and %l constructs are ignored. It is used only on frames for
28375 which no explicit name has been set \(see `modify-frame-parameters'). */);
28376
28377 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28378 doc: /* Template for displaying the title bar of an iconified frame.
28379 \(Assuming the window manager supports this feature.)
28380 This variable has the same structure as `mode-line-format' (which see),
28381 and is used only on frames for which no explicit name has been set
28382 \(see `modify-frame-parameters'). */);
28383 Vicon_title_format
28384 = Vframe_title_format
28385 = pure_cons (intern_c_string ("multiple-frames"),
28386 pure_cons (make_pure_c_string ("%b"),
28387 pure_cons (pure_cons (empty_unibyte_string,
28388 pure_cons (intern_c_string ("invocation-name"),
28389 pure_cons (make_pure_c_string ("@"),
28390 pure_cons (intern_c_string ("system-name"),
28391 Qnil)))),
28392 Qnil)));
28393
28394 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28395 doc: /* Maximum number of lines to keep in the message log buffer.
28396 If nil, disable message logging. If t, log messages but don't truncate
28397 the buffer when it becomes large. */);
28398 Vmessage_log_max = make_number (100);
28399
28400 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28401 doc: /* Functions called before redisplay, if window sizes have changed.
28402 The value should be a list of functions that take one argument.
28403 Just before redisplay, for each frame, if any of its windows have changed
28404 size since the last redisplay, or have been split or deleted,
28405 all the functions in the list are called, with the frame as argument. */);
28406 Vwindow_size_change_functions = Qnil;
28407
28408 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28409 doc: /* List of functions to call before redisplaying a window with scrolling.
28410 Each function is called with two arguments, the window and its new
28411 display-start position. Note that these functions are also called by
28412 `set-window-buffer'. Also note that the value of `window-end' is not
28413 valid when these functions are called.
28414
28415 Warning: Do not use this feature to alter the way the window
28416 is scrolled. It is not designed for that, and such use probably won't
28417 work. */);
28418 Vwindow_scroll_functions = Qnil;
28419
28420 DEFVAR_LISP ("window-text-change-functions",
28421 Vwindow_text_change_functions,
28422 doc: /* Functions to call in redisplay when text in the window might change. */);
28423 Vwindow_text_change_functions = Qnil;
28424
28425 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28426 doc: /* Functions called when redisplay of a window reaches the end trigger.
28427 Each function is called with two arguments, the window and the end trigger value.
28428 See `set-window-redisplay-end-trigger'. */);
28429 Vredisplay_end_trigger_functions = Qnil;
28430
28431 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28432 doc: /* *Non-nil means autoselect window with mouse pointer.
28433 If nil, do not autoselect windows.
28434 A positive number means delay autoselection by that many seconds: a
28435 window is autoselected only after the mouse has remained in that
28436 window for the duration of the delay.
28437 A negative number has a similar effect, but causes windows to be
28438 autoselected only after the mouse has stopped moving. \(Because of
28439 the way Emacs compares mouse events, you will occasionally wait twice
28440 that time before the window gets selected.\)
28441 Any other value means to autoselect window instantaneously when the
28442 mouse pointer enters it.
28443
28444 Autoselection selects the minibuffer only if it is active, and never
28445 unselects the minibuffer if it is active.
28446
28447 When customizing this variable make sure that the actual value of
28448 `focus-follows-mouse' matches the behavior of your window manager. */);
28449 Vmouse_autoselect_window = Qnil;
28450
28451 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28452 doc: /* *Non-nil means automatically resize tool-bars.
28453 This dynamically changes the tool-bar's height to the minimum height
28454 that is needed to make all tool-bar items visible.
28455 If value is `grow-only', the tool-bar's height is only increased
28456 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28457 Vauto_resize_tool_bars = Qt;
28458
28459 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28460 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28461 auto_raise_tool_bar_buttons_p = 1;
28462
28463 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28464 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28465 make_cursor_line_fully_visible_p = 1;
28466
28467 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28468 doc: /* *Border below tool-bar in pixels.
28469 If an integer, use it as the height of the border.
28470 If it is one of `internal-border-width' or `border-width', use the
28471 value of the corresponding frame parameter.
28472 Otherwise, no border is added below the tool-bar. */);
28473 Vtool_bar_border = Qinternal_border_width;
28474
28475 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28476 doc: /* *Margin around tool-bar buttons in pixels.
28477 If an integer, use that for both horizontal and vertical margins.
28478 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28479 HORZ specifying the horizontal margin, and VERT specifying the
28480 vertical margin. */);
28481 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28482
28483 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28484 doc: /* *Relief thickness of tool-bar buttons. */);
28485 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28486
28487 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28488 doc: /* Tool bar style to use.
28489 It can be one of
28490 image - show images only
28491 text - show text only
28492 both - show both, text below image
28493 both-horiz - show text to the right of the image
28494 text-image-horiz - show text to the left of the image
28495 any other - use system default or image if no system default. */);
28496 Vtool_bar_style = Qnil;
28497
28498 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28499 doc: /* *Maximum number of characters a label can have to be shown.
28500 The tool bar style must also show labels for this to have any effect, see
28501 `tool-bar-style'. */);
28502 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28503
28504 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28505 doc: /* List of functions to call to fontify regions of text.
28506 Each function is called with one argument POS. Functions must
28507 fontify a region starting at POS in the current buffer, and give
28508 fontified regions the property `fontified'. */);
28509 Vfontification_functions = Qnil;
28510 Fmake_variable_buffer_local (Qfontification_functions);
28511
28512 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28513 unibyte_display_via_language_environment,
28514 doc: /* *Non-nil means display unibyte text according to language environment.
28515 Specifically, this means that raw bytes in the range 160-255 decimal
28516 are displayed by converting them to the equivalent multibyte characters
28517 according to the current language environment. As a result, they are
28518 displayed according to the current fontset.
28519
28520 Note that this variable affects only how these bytes are displayed,
28521 but does not change the fact they are interpreted as raw bytes. */);
28522 unibyte_display_via_language_environment = 0;
28523
28524 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28525 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28526 If a float, it specifies a fraction of the mini-window frame's height.
28527 If an integer, it specifies a number of lines. */);
28528 Vmax_mini_window_height = make_float (0.25);
28529
28530 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28531 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28532 A value of nil means don't automatically resize mini-windows.
28533 A value of t means resize them to fit the text displayed in them.
28534 A value of `grow-only', the default, means let mini-windows grow only;
28535 they return to their normal size when the minibuffer is closed, or the
28536 echo area becomes empty. */);
28537 Vresize_mini_windows = Qgrow_only;
28538
28539 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28540 doc: /* Alist specifying how to blink the cursor off.
28541 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28542 `cursor-type' frame-parameter or variable equals ON-STATE,
28543 comparing using `equal', Emacs uses OFF-STATE to specify
28544 how to blink it off. ON-STATE and OFF-STATE are values for
28545 the `cursor-type' frame parameter.
28546
28547 If a frame's ON-STATE has no entry in this list,
28548 the frame's other specifications determine how to blink the cursor off. */);
28549 Vblink_cursor_alist = Qnil;
28550
28551 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28552 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28553 If non-nil, windows are automatically scrolled horizontally to make
28554 point visible. */);
28555 automatic_hscrolling_p = 1;
28556 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28557
28558 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28559 doc: /* *How many columns away from the window edge point is allowed to get
28560 before automatic hscrolling will horizontally scroll the window. */);
28561 hscroll_margin = 5;
28562
28563 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28564 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28565 When point is less than `hscroll-margin' columns from the window
28566 edge, automatic hscrolling will scroll the window by the amount of columns
28567 determined by this variable. If its value is a positive integer, scroll that
28568 many columns. If it's a positive floating-point number, it specifies the
28569 fraction of the window's width to scroll. If it's nil or zero, point will be
28570 centered horizontally after the scroll. Any other value, including negative
28571 numbers, are treated as if the value were zero.
28572
28573 Automatic hscrolling always moves point outside the scroll margin, so if
28574 point was more than scroll step columns inside the margin, the window will
28575 scroll more than the value given by the scroll step.
28576
28577 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28578 and `scroll-right' overrides this variable's effect. */);
28579 Vhscroll_step = make_number (0);
28580
28581 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28582 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28583 Bind this around calls to `message' to let it take effect. */);
28584 message_truncate_lines = 0;
28585
28586 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28587 doc: /* Normal hook run to update the menu bar definitions.
28588 Redisplay runs this hook before it redisplays the menu bar.
28589 This is used to update submenus such as Buffers,
28590 whose contents depend on various data. */);
28591 Vmenu_bar_update_hook = Qnil;
28592
28593 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28594 doc: /* Frame for which we are updating a menu.
28595 The enable predicate for a menu binding should check this variable. */);
28596 Vmenu_updating_frame = Qnil;
28597
28598 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28599 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28600 inhibit_menubar_update = 0;
28601
28602 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28603 doc: /* Prefix prepended to all continuation lines at display time.
28604 The value may be a string, an image, or a stretch-glyph; it is
28605 interpreted in the same way as the value of a `display' text property.
28606
28607 This variable is overridden by any `wrap-prefix' text or overlay
28608 property.
28609
28610 To add a prefix to non-continuation lines, use `line-prefix'. */);
28611 Vwrap_prefix = Qnil;
28612 DEFSYM (Qwrap_prefix, "wrap-prefix");
28613 Fmake_variable_buffer_local (Qwrap_prefix);
28614
28615 DEFVAR_LISP ("line-prefix", Vline_prefix,
28616 doc: /* Prefix prepended to all non-continuation lines at display time.
28617 The value may be a string, an image, or a stretch-glyph; it is
28618 interpreted in the same way as the value of a `display' text property.
28619
28620 This variable is overridden by any `line-prefix' text or overlay
28621 property.
28622
28623 To add a prefix to continuation lines, use `wrap-prefix'. */);
28624 Vline_prefix = Qnil;
28625 DEFSYM (Qline_prefix, "line-prefix");
28626 Fmake_variable_buffer_local (Qline_prefix);
28627
28628 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28629 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28630 inhibit_eval_during_redisplay = 0;
28631
28632 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28633 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28634 inhibit_free_realized_faces = 0;
28635
28636 #if GLYPH_DEBUG
28637 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28638 doc: /* Inhibit try_window_id display optimization. */);
28639 inhibit_try_window_id = 0;
28640
28641 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28642 doc: /* Inhibit try_window_reusing display optimization. */);
28643 inhibit_try_window_reusing = 0;
28644
28645 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28646 doc: /* Inhibit try_cursor_movement display optimization. */);
28647 inhibit_try_cursor_movement = 0;
28648 #endif /* GLYPH_DEBUG */
28649
28650 DEFVAR_INT ("overline-margin", overline_margin,
28651 doc: /* *Space between overline and text, in pixels.
28652 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28653 margin to the character height. */);
28654 overline_margin = 2;
28655
28656 DEFVAR_INT ("underline-minimum-offset",
28657 underline_minimum_offset,
28658 doc: /* Minimum distance between baseline and underline.
28659 This can improve legibility of underlined text at small font sizes,
28660 particularly when using variable `x-use-underline-position-properties'
28661 with fonts that specify an UNDERLINE_POSITION relatively close to the
28662 baseline. The default value is 1. */);
28663 underline_minimum_offset = 1;
28664
28665 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28666 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28667 This feature only works when on a window system that can change
28668 cursor shapes. */);
28669 display_hourglass_p = 1;
28670
28671 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28672 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28673 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28674
28675 hourglass_atimer = NULL;
28676 hourglass_shown_p = 0;
28677
28678 DEFSYM (Qglyphless_char, "glyphless-char");
28679 DEFSYM (Qhex_code, "hex-code");
28680 DEFSYM (Qempty_box, "empty-box");
28681 DEFSYM (Qthin_space, "thin-space");
28682 DEFSYM (Qzero_width, "zero-width");
28683
28684 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28685 /* Intern this now in case it isn't already done.
28686 Setting this variable twice is harmless.
28687 But don't staticpro it here--that is done in alloc.c. */
28688 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28689 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28690
28691 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28692 doc: /* Char-table defining glyphless characters.
28693 Each element, if non-nil, should be one of the following:
28694 an ASCII acronym string: display this string in a box
28695 `hex-code': display the hexadecimal code of a character in a box
28696 `empty-box': display as an empty box
28697 `thin-space': display as 1-pixel width space
28698 `zero-width': don't display
28699 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28700 display method for graphical terminals and text terminals respectively.
28701 GRAPHICAL and TEXT should each have one of the values listed above.
28702
28703 The char-table has one extra slot to control the display of a character for
28704 which no font is found. This slot only takes effect on graphical terminals.
28705 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28706 `thin-space'. The default is `empty-box'. */);
28707 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28708 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28709 Qempty_box);
28710 }
28711
28712
28713 /* Initialize this module when Emacs starts. */
28714
28715 void
28716 init_xdisp (void)
28717 {
28718 current_header_line_height = current_mode_line_height = -1;
28719
28720 CHARPOS (this_line_start_pos) = 0;
28721
28722 if (!noninteractive)
28723 {
28724 struct window *m = XWINDOW (minibuf_window);
28725 Lisp_Object frame = m->frame;
28726 struct frame *f = XFRAME (frame);
28727 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28728 struct window *r = XWINDOW (root);
28729 int i;
28730
28731 echo_area_window = minibuf_window;
28732
28733 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28734 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28735 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28736 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28737 XSETFASTINT (m->total_lines, 1);
28738 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28739
28740 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28741 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28742 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28743
28744 /* The default ellipsis glyphs `...'. */
28745 for (i = 0; i < 3; ++i)
28746 default_invis_vector[i] = make_number ('.');
28747 }
28748
28749 {
28750 /* Allocate the buffer for frame titles.
28751 Also used for `format-mode-line'. */
28752 int size = 100;
28753 mode_line_noprop_buf = (char *) xmalloc (size);
28754 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28755 mode_line_noprop_ptr = mode_line_noprop_buf;
28756 mode_line_target = MODE_LINE_DISPLAY;
28757 }
28758
28759 help_echo_showing_p = 0;
28760 }
28761
28762 /* Since w32 does not support atimers, it defines its own implementation of
28763 the following three functions in w32fns.c. */
28764 #ifndef WINDOWSNT
28765
28766 /* Platform-independent portion of hourglass implementation. */
28767
28768 /* Return non-zero if hourglass timer has been started or hourglass is
28769 shown. */
28770 int
28771 hourglass_started (void)
28772 {
28773 return hourglass_shown_p || hourglass_atimer != NULL;
28774 }
28775
28776 /* Cancel a currently active hourglass timer, and start a new one. */
28777 void
28778 start_hourglass (void)
28779 {
28780 #if defined (HAVE_WINDOW_SYSTEM)
28781 EMACS_TIME delay;
28782 int secs, usecs = 0;
28783
28784 cancel_hourglass ();
28785
28786 if (INTEGERP (Vhourglass_delay)
28787 && XINT (Vhourglass_delay) > 0)
28788 secs = XFASTINT (Vhourglass_delay);
28789 else if (FLOATP (Vhourglass_delay)
28790 && XFLOAT_DATA (Vhourglass_delay) > 0)
28791 {
28792 Lisp_Object tem;
28793 tem = Ftruncate (Vhourglass_delay, Qnil);
28794 secs = XFASTINT (tem);
28795 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28796 }
28797 else
28798 secs = DEFAULT_HOURGLASS_DELAY;
28799
28800 EMACS_SET_SECS_USECS (delay, secs, usecs);
28801 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28802 show_hourglass, NULL);
28803 #endif
28804 }
28805
28806
28807 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28808 shown. */
28809 void
28810 cancel_hourglass (void)
28811 {
28812 #if defined (HAVE_WINDOW_SYSTEM)
28813 if (hourglass_atimer)
28814 {
28815 cancel_atimer (hourglass_atimer);
28816 hourglass_atimer = NULL;
28817 }
28818
28819 if (hourglass_shown_p)
28820 hide_hourglass ();
28821 #endif
28822 }
28823 #endif /* ! WINDOWSNT */