Fix the use case of popping from display property.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT is a space or tab
387 character. This is used to determine word wrapping. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
391
392 /* Name of the face used to highlight trailing whitespace. */
393
394 static Lisp_Object Qtrailing_whitespace;
395
396 /* Name and number of the face used to highlight escape glyphs. */
397
398 static Lisp_Object Qescape_glyph;
399
400 /* Name and number of the face used to highlight non-breaking spaces. */
401
402 static Lisp_Object Qnobreak_space;
403
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
406
407 Lisp_Object Qimage;
408
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
413
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
419
420 int noninteractive_need_newline;
421
422 /* Non-zero means print newline to message log before next message. */
423
424 static int message_log_need_newline;
425
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
432 \f
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
437
438 static struct text_pos this_line_start_pos;
439
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
442
443 static struct text_pos this_line_end_pos;
444
445 /* The vertical positions and the height of this line. */
446
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
450
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
453
454 static int this_line_start_x;
455
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
459
460 static struct text_pos this_line_min_pos;
461
462 /* Buffer that this_line_.* variables are referring to. */
463
464 static struct buffer *this_line_buffer;
465
466
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
471
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
476
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478
479 Lisp_Object Qmenu_bar_update_hook;
480
481 /* Nonzero if an overlay arrow has been displayed in this window. */
482
483 static int overlay_arrow_seen;
484
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
488
489 int buffer_shared;
490
491 /* Vector containing glyphs for an ellipsis `...'. */
492
493 static Lisp_Object default_invis_vector[3];
494
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
498
499 Lisp_Object echo_area_window;
500
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
505
506 static Lisp_Object Vmessage_stack;
507
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
510
511 static int message_enable_multibyte;
512
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
514
515 int update_mode_lines;
516
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero means a frame's cursor type has been changed. */
523
524 int cursor_type_changed;
525
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
528
529 static int line_number_displayed;
530
531 /* The name of the *Messages* buffer, a string. */
532
533 static Lisp_Object Vmessages_buffer_name;
534
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
537
538 Lisp_Object echo_area_buffer[2];
539
540 /* The buffers referenced from echo_area_buffer. */
541
542 static Lisp_Object echo_buffer[2];
543
544 /* A vector saved used in with_area_buffer to reduce consing. */
545
546 static Lisp_Object Vwith_echo_area_save_vector;
547
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
550
551 static int display_last_displayed_message_p;
552
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
555
556 static int message_buf_print;
557
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
559
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
562
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
565
566 static int message_cleared_p;
567
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
570
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
574
575 /* Ascent and height of the last line processed by move_it_to. */
576
577 static int last_max_ascent, last_height;
578
579 /* Non-zero if there's a help-echo in the echo area. */
580
581 int help_echo_showing_p;
582
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
586
587 int current_mode_line_height, current_header_line_height;
588
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
594
595 #define TEXT_PROP_DISTANCE_LIMIT 100
596
597 #if GLYPH_DEBUG
598
599 /* Non-zero means print traces of redisplay if compiled with
600 GLYPH_DEBUG != 0. */
601
602 int trace_redisplay_p;
603
604 #endif /* GLYPH_DEBUG */
605
606 #ifdef DEBUG_TRACE_MOVE
607 /* Non-zero means trace with TRACE_MOVE to stderr. */
608 int trace_move;
609
610 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
611 #else
612 #define TRACE_MOVE(x) (void) 0
613 #endif
614
615 static Lisp_Object Qauto_hscroll_mode;
616
617 /* Buffer being redisplayed -- for redisplay_window_error. */
618
619 static struct buffer *displayed_buffer;
620
621 /* Value returned from text property handlers (see below). */
622
623 enum prop_handled
624 {
625 HANDLED_NORMALLY,
626 HANDLED_RECOMPUTE_PROPS,
627 HANDLED_OVERLAY_STRING_CONSUMED,
628 HANDLED_RETURN
629 };
630
631 /* A description of text properties that redisplay is interested
632 in. */
633
634 struct props
635 {
636 /* The name of the property. */
637 Lisp_Object *name;
638
639 /* A unique index for the property. */
640 enum prop_idx idx;
641
642 /* A handler function called to set up iterator IT from the property
643 at IT's current position. Value is used to steer handle_stop. */
644 enum prop_handled (*handler) (struct it *it);
645 };
646
647 static enum prop_handled handle_face_prop (struct it *);
648 static enum prop_handled handle_invisible_prop (struct it *);
649 static enum prop_handled handle_display_prop (struct it *);
650 static enum prop_handled handle_composition_prop (struct it *);
651 static enum prop_handled handle_overlay_change (struct it *);
652 static enum prop_handled handle_fontified_prop (struct it *);
653
654 /* Properties handled by iterators. */
655
656 static struct props it_props[] =
657 {
658 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
659 /* Handle `face' before `display' because some sub-properties of
660 `display' need to know the face. */
661 {&Qface, FACE_PROP_IDX, handle_face_prop},
662 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
663 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
664 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
665 {NULL, 0, NULL}
666 };
667
668 /* Value is the position described by X. If X is a marker, value is
669 the marker_position of X. Otherwise, value is X. */
670
671 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
672
673 /* Enumeration returned by some move_it_.* functions internally. */
674
675 enum move_it_result
676 {
677 /* Not used. Undefined value. */
678 MOVE_UNDEFINED,
679
680 /* Move ended at the requested buffer position or ZV. */
681 MOVE_POS_MATCH_OR_ZV,
682
683 /* Move ended at the requested X pixel position. */
684 MOVE_X_REACHED,
685
686 /* Move within a line ended at the end of a line that must be
687 continued. */
688 MOVE_LINE_CONTINUED,
689
690 /* Move within a line ended at the end of a line that would
691 be displayed truncated. */
692 MOVE_LINE_TRUNCATED,
693
694 /* Move within a line ended at a line end. */
695 MOVE_NEWLINE_OR_CR
696 };
697
698 /* This counter is used to clear the face cache every once in a while
699 in redisplay_internal. It is incremented for each redisplay.
700 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
701 cleared. */
702
703 #define CLEAR_FACE_CACHE_COUNT 500
704 static int clear_face_cache_count;
705
706 /* Similarly for the image cache. */
707
708 #ifdef HAVE_WINDOW_SYSTEM
709 #define CLEAR_IMAGE_CACHE_COUNT 101
710 static int clear_image_cache_count;
711
712 /* Null glyph slice */
713 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
714 #endif
715
716 /* Non-zero while redisplay_internal is in progress. */
717
718 int redisplaying_p;
719
720 static Lisp_Object Qinhibit_free_realized_faces;
721
722 /* If a string, XTread_socket generates an event to display that string.
723 (The display is done in read_char.) */
724
725 Lisp_Object help_echo_string;
726 Lisp_Object help_echo_window;
727 Lisp_Object help_echo_object;
728 EMACS_INT help_echo_pos;
729
730 /* Temporary variable for XTread_socket. */
731
732 Lisp_Object previous_help_echo_string;
733
734 /* Platform-independent portion of hourglass implementation. */
735
736 /* Non-zero means an hourglass cursor is currently shown. */
737 int hourglass_shown_p;
738
739 /* If non-null, an asynchronous timer that, when it expires, displays
740 an hourglass cursor on all frames. */
741 struct atimer *hourglass_atimer;
742
743 /* Name of the face used to display glyphless characters. */
744 Lisp_Object Qglyphless_char;
745
746 /* Symbol for the purpose of Vglyphless_char_display. */
747 static Lisp_Object Qglyphless_char_display;
748
749 /* Method symbols for Vglyphless_char_display. */
750 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
751
752 /* Default pixel width of `thin-space' display method. */
753 #define THIN_SPACE_WIDTH 1
754
755 /* Default number of seconds to wait before displaying an hourglass
756 cursor. */
757 #define DEFAULT_HOURGLASS_DELAY 1
758
759 \f
760 /* Function prototypes. */
761
762 static void setup_for_ellipsis (struct it *, int);
763 static void set_iterator_to_next (struct it *, int);
764 static void mark_window_display_accurate_1 (struct window *, int);
765 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
766 static int display_prop_string_p (Lisp_Object, Lisp_Object);
767 static int cursor_row_p (struct glyph_row *);
768 static int redisplay_mode_lines (Lisp_Object, int);
769 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
770
771 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
772
773 static void handle_line_prefix (struct it *);
774
775 static void pint2str (char *, int, EMACS_INT);
776 static void pint2hrstr (char *, int, EMACS_INT);
777 static struct text_pos run_window_scroll_functions (Lisp_Object,
778 struct text_pos);
779 static void reconsider_clip_changes (struct window *, struct buffer *);
780 static int text_outside_line_unchanged_p (struct window *,
781 EMACS_INT, EMACS_INT);
782 static void store_mode_line_noprop_char (char);
783 static int store_mode_line_noprop (const char *, int, int);
784 static void handle_stop (struct it *);
785 static void handle_stop_backwards (struct it *, EMACS_INT);
786 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
787 static void ensure_echo_area_buffers (void);
788 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
789 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
790 static int with_echo_area_buffer (struct window *, int,
791 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
792 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
793 static void clear_garbaged_frames (void);
794 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
795 static void pop_message (void);
796 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
797 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
798 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
799 static int display_echo_area (struct window *);
800 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
801 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
802 static Lisp_Object unwind_redisplay (Lisp_Object);
803 static int string_char_and_length (const unsigned char *, int *);
804 static struct text_pos display_prop_end (struct it *, Lisp_Object,
805 struct text_pos);
806 static int compute_window_start_on_continuation_line (struct window *);
807 static Lisp_Object safe_eval_handler (Lisp_Object);
808 static void insert_left_trunc_glyphs (struct it *);
809 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
810 Lisp_Object);
811 static void extend_face_to_end_of_line (struct it *);
812 static int append_space_for_newline (struct it *, int);
813 static int cursor_row_fully_visible_p (struct window *, int, int);
814 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
815 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
816 static int trailing_whitespace_p (EMACS_INT);
817 static unsigned long int message_log_check_duplicate (EMACS_INT, EMACS_INT);
818 static void push_it (struct it *, struct text_pos *);
819 static void pop_it (struct it *);
820 static void sync_frame_with_window_matrix_rows (struct window *);
821 static void select_frame_for_redisplay (Lisp_Object);
822 static void redisplay_internal (void);
823 static int echo_area_display (int);
824 static void redisplay_windows (Lisp_Object);
825 static void redisplay_window (Lisp_Object, int);
826 static Lisp_Object redisplay_window_error (Lisp_Object);
827 static Lisp_Object redisplay_window_0 (Lisp_Object);
828 static Lisp_Object redisplay_window_1 (Lisp_Object);
829 static int set_cursor_from_row (struct window *, struct glyph_row *,
830 struct glyph_matrix *, EMACS_INT, EMACS_INT,
831 int, int);
832 static int update_menu_bar (struct frame *, int, int);
833 static int try_window_reusing_current_matrix (struct window *);
834 static int try_window_id (struct window *);
835 static int display_line (struct it *);
836 static int display_mode_lines (struct window *);
837 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
838 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
839 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
840 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
841 static void display_menu_bar (struct window *);
842 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
843 EMACS_INT *);
844 static int display_string (const char *, Lisp_Object, Lisp_Object,
845 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
846 static void compute_line_metrics (struct it *);
847 static void run_redisplay_end_trigger_hook (struct it *);
848 static int get_overlay_strings (struct it *, EMACS_INT);
849 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
850 static void next_overlay_string (struct it *);
851 static void reseat (struct it *, struct text_pos, int);
852 static void reseat_1 (struct it *, struct text_pos, int);
853 static void back_to_previous_visible_line_start (struct it *);
854 void reseat_at_previous_visible_line_start (struct it *);
855 static void reseat_at_next_visible_line_start (struct it *, int);
856 static int next_element_from_ellipsis (struct it *);
857 static int next_element_from_display_vector (struct it *);
858 static int next_element_from_string (struct it *);
859 static int next_element_from_c_string (struct it *);
860 static int next_element_from_buffer (struct it *);
861 static int next_element_from_composition (struct it *);
862 static int next_element_from_image (struct it *);
863 static int next_element_from_stretch (struct it *);
864 static void load_overlay_strings (struct it *, EMACS_INT);
865 static int init_from_display_pos (struct it *, struct window *,
866 struct display_pos *);
867 static void reseat_to_string (struct it *, const char *,
868 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
869 static int get_next_display_element (struct it *);
870 static enum move_it_result
871 move_it_in_display_line_to (struct it *, EMACS_INT, int,
872 enum move_operation_enum);
873 void move_it_vertically_backward (struct it *, int);
874 static void init_to_row_start (struct it *, struct window *,
875 struct glyph_row *);
876 static int init_to_row_end (struct it *, struct window *,
877 struct glyph_row *);
878 static void back_to_previous_line_start (struct it *);
879 static int forward_to_next_line_start (struct it *, int *);
880 static struct text_pos string_pos_nchars_ahead (struct text_pos,
881 Lisp_Object, EMACS_INT);
882 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
883 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
884 static EMACS_INT number_of_chars (const char *, int);
885 static void compute_stop_pos (struct it *);
886 static void compute_string_pos (struct text_pos *, struct text_pos,
887 Lisp_Object);
888 static int face_before_or_after_it_pos (struct it *, int);
889 static EMACS_INT next_overlay_change (EMACS_INT);
890 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
891 Lisp_Object, struct text_pos *, EMACS_INT, int);
892 static int handle_single_display_spec (struct it *, Lisp_Object,
893 Lisp_Object, Lisp_Object,
894 struct text_pos *, EMACS_INT, int, int);
895 static int underlying_face_id (struct it *);
896 static int in_ellipses_for_invisible_text_p (struct display_pos *,
897 struct window *);
898
899 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
900 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
901
902 #ifdef HAVE_WINDOW_SYSTEM
903
904 static void x_consider_frame_title (Lisp_Object);
905 static int tool_bar_lines_needed (struct frame *, int *);
906 static void update_tool_bar (struct frame *, int);
907 static void build_desired_tool_bar_string (struct frame *f);
908 static int redisplay_tool_bar (struct frame *);
909 static void display_tool_bar_line (struct it *, int);
910 static void notice_overwritten_cursor (struct window *,
911 enum glyph_row_area,
912 int, int, int, int);
913 static void append_stretch_glyph (struct it *, Lisp_Object,
914 int, int, int);
915
916
917 #endif /* HAVE_WINDOW_SYSTEM */
918
919 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
920 static int coords_in_mouse_face_p (struct window *, int, int);
921
922
923 \f
924 /***********************************************************************
925 Window display dimensions
926 ***********************************************************************/
927
928 /* Return the bottom boundary y-position for text lines in window W.
929 This is the first y position at which a line cannot start.
930 It is relative to the top of the window.
931
932 This is the height of W minus the height of a mode line, if any. */
933
934 INLINE int
935 window_text_bottom_y (struct window *w)
936 {
937 int height = WINDOW_TOTAL_HEIGHT (w);
938
939 if (WINDOW_WANTS_MODELINE_P (w))
940 height -= CURRENT_MODE_LINE_HEIGHT (w);
941 return height;
942 }
943
944 /* Return the pixel width of display area AREA of window W. AREA < 0
945 means return the total width of W, not including fringes to
946 the left and right of the window. */
947
948 INLINE int
949 window_box_width (struct window *w, int area)
950 {
951 int cols = XFASTINT (w->total_cols);
952 int pixels = 0;
953
954 if (!w->pseudo_window_p)
955 {
956 cols -= WINDOW_SCROLL_BAR_COLS (w);
957
958 if (area == TEXT_AREA)
959 {
960 if (INTEGERP (w->left_margin_cols))
961 cols -= XFASTINT (w->left_margin_cols);
962 if (INTEGERP (w->right_margin_cols))
963 cols -= XFASTINT (w->right_margin_cols);
964 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
965 }
966 else if (area == LEFT_MARGIN_AREA)
967 {
968 cols = (INTEGERP (w->left_margin_cols)
969 ? XFASTINT (w->left_margin_cols) : 0);
970 pixels = 0;
971 }
972 else if (area == RIGHT_MARGIN_AREA)
973 {
974 cols = (INTEGERP (w->right_margin_cols)
975 ? XFASTINT (w->right_margin_cols) : 0);
976 pixels = 0;
977 }
978 }
979
980 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
981 }
982
983
984 /* Return the pixel height of the display area of window W, not
985 including mode lines of W, if any. */
986
987 INLINE int
988 window_box_height (struct window *w)
989 {
990 struct frame *f = XFRAME (w->frame);
991 int height = WINDOW_TOTAL_HEIGHT (w);
992
993 xassert (height >= 0);
994
995 /* Note: the code below that determines the mode-line/header-line
996 height is essentially the same as that contained in the macro
997 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
998 the appropriate glyph row has its `mode_line_p' flag set,
999 and if it doesn't, uses estimate_mode_line_height instead. */
1000
1001 if (WINDOW_WANTS_MODELINE_P (w))
1002 {
1003 struct glyph_row *ml_row
1004 = (w->current_matrix && w->current_matrix->rows
1005 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1006 : 0);
1007 if (ml_row && ml_row->mode_line_p)
1008 height -= ml_row->height;
1009 else
1010 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1011 }
1012
1013 if (WINDOW_WANTS_HEADER_LINE_P (w))
1014 {
1015 struct glyph_row *hl_row
1016 = (w->current_matrix && w->current_matrix->rows
1017 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1018 : 0);
1019 if (hl_row && hl_row->mode_line_p)
1020 height -= hl_row->height;
1021 else
1022 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1023 }
1024
1025 /* With a very small font and a mode-line that's taller than
1026 default, we might end up with a negative height. */
1027 return max (0, height);
1028 }
1029
1030 /* Return the window-relative coordinate of the left edge of display
1031 area AREA of window W. AREA < 0 means return the left edge of the
1032 whole window, to the right of the left fringe of W. */
1033
1034 INLINE int
1035 window_box_left_offset (struct window *w, int area)
1036 {
1037 int x;
1038
1039 if (w->pseudo_window_p)
1040 return 0;
1041
1042 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1043
1044 if (area == TEXT_AREA)
1045 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1046 + window_box_width (w, LEFT_MARGIN_AREA));
1047 else if (area == RIGHT_MARGIN_AREA)
1048 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1049 + window_box_width (w, LEFT_MARGIN_AREA)
1050 + window_box_width (w, TEXT_AREA)
1051 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1052 ? 0
1053 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1054 else if (area == LEFT_MARGIN_AREA
1055 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1056 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1057
1058 return x;
1059 }
1060
1061
1062 /* Return the window-relative coordinate of the right edge of display
1063 area AREA of window W. AREA < 0 means return the right edge of the
1064 whole window, to the left of the right fringe of W. */
1065
1066 INLINE int
1067 window_box_right_offset (struct window *w, int area)
1068 {
1069 return window_box_left_offset (w, area) + window_box_width (w, area);
1070 }
1071
1072 /* Return the frame-relative coordinate of the left edge of display
1073 area AREA of window W. AREA < 0 means return the left edge of the
1074 whole window, to the right of the left fringe of W. */
1075
1076 INLINE int
1077 window_box_left (struct window *w, int area)
1078 {
1079 struct frame *f = XFRAME (w->frame);
1080 int x;
1081
1082 if (w->pseudo_window_p)
1083 return FRAME_INTERNAL_BORDER_WIDTH (f);
1084
1085 x = (WINDOW_LEFT_EDGE_X (w)
1086 + window_box_left_offset (w, area));
1087
1088 return x;
1089 }
1090
1091
1092 /* Return the frame-relative coordinate of the right edge of display
1093 area AREA of window W. AREA < 0 means return the right edge of the
1094 whole window, to the left of the right fringe of W. */
1095
1096 INLINE int
1097 window_box_right (struct window *w, int area)
1098 {
1099 return window_box_left (w, area) + window_box_width (w, area);
1100 }
1101
1102 /* Get the bounding box of the display area AREA of window W, without
1103 mode lines, in frame-relative coordinates. AREA < 0 means the
1104 whole window, not including the left and right fringes of
1105 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1106 coordinates of the upper-left corner of the box. Return in
1107 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1108
1109 INLINE void
1110 window_box (struct window *w, int area, int *box_x, int *box_y,
1111 int *box_width, int *box_height)
1112 {
1113 if (box_width)
1114 *box_width = window_box_width (w, area);
1115 if (box_height)
1116 *box_height = window_box_height (w);
1117 if (box_x)
1118 *box_x = window_box_left (w, area);
1119 if (box_y)
1120 {
1121 *box_y = WINDOW_TOP_EDGE_Y (w);
1122 if (WINDOW_WANTS_HEADER_LINE_P (w))
1123 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1124 }
1125 }
1126
1127
1128 /* Get the bounding box of the display area AREA of window W, without
1129 mode lines. AREA < 0 means the whole window, not including the
1130 left and right fringe of the window. Return in *TOP_LEFT_X
1131 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1132 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1133 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1134 box. */
1135
1136 static INLINE void
1137 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1138 int *bottom_right_x, int *bottom_right_y)
1139 {
1140 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1141 bottom_right_y);
1142 *bottom_right_x += *top_left_x;
1143 *bottom_right_y += *top_left_y;
1144 }
1145
1146
1147 \f
1148 /***********************************************************************
1149 Utilities
1150 ***********************************************************************/
1151
1152 /* Return the bottom y-position of the line the iterator IT is in.
1153 This can modify IT's settings. */
1154
1155 int
1156 line_bottom_y (struct it *it)
1157 {
1158 int line_height = it->max_ascent + it->max_descent;
1159 int line_top_y = it->current_y;
1160
1161 if (line_height == 0)
1162 {
1163 if (last_height)
1164 line_height = last_height;
1165 else if (IT_CHARPOS (*it) < ZV)
1166 {
1167 move_it_by_lines (it, 1);
1168 line_height = (it->max_ascent || it->max_descent
1169 ? it->max_ascent + it->max_descent
1170 : last_height);
1171 }
1172 else
1173 {
1174 struct glyph_row *row = it->glyph_row;
1175
1176 /* Use the default character height. */
1177 it->glyph_row = NULL;
1178 it->what = IT_CHARACTER;
1179 it->c = ' ';
1180 it->len = 1;
1181 PRODUCE_GLYPHS (it);
1182 line_height = it->ascent + it->descent;
1183 it->glyph_row = row;
1184 }
1185 }
1186
1187 return line_top_y + line_height;
1188 }
1189
1190
1191 /* Return 1 if position CHARPOS is visible in window W.
1192 CHARPOS < 0 means return info about WINDOW_END position.
1193 If visible, set *X and *Y to pixel coordinates of top left corner.
1194 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1195 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1196
1197 int
1198 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1199 int *rtop, int *rbot, int *rowh, int *vpos)
1200 {
1201 struct it it;
1202 struct text_pos top;
1203 int visible_p = 0;
1204 struct buffer *old_buffer = NULL;
1205
1206 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1207 return visible_p;
1208
1209 if (XBUFFER (w->buffer) != current_buffer)
1210 {
1211 old_buffer = current_buffer;
1212 set_buffer_internal_1 (XBUFFER (w->buffer));
1213 }
1214
1215 SET_TEXT_POS_FROM_MARKER (top, w->start);
1216
1217 /* Compute exact mode line heights. */
1218 if (WINDOW_WANTS_MODELINE_P (w))
1219 current_mode_line_height
1220 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1221 BVAR (current_buffer, mode_line_format));
1222
1223 if (WINDOW_WANTS_HEADER_LINE_P (w))
1224 current_header_line_height
1225 = display_mode_line (w, HEADER_LINE_FACE_ID,
1226 BVAR (current_buffer, header_line_format));
1227
1228 start_display (&it, w, top);
1229 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1230 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1231
1232 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1233 {
1234 /* We have reached CHARPOS, or passed it. How the call to
1235 move_it_to can overshoot: (i) If CHARPOS is on invisible
1236 text, move_it_to stops at the end of the invisible text,
1237 after CHARPOS. (ii) If CHARPOS is in a display vector,
1238 move_it_to stops on its last glyph. */
1239 int top_x = it.current_x;
1240 int top_y = it.current_y;
1241 enum it_method it_method = it.method;
1242 /* Calling line_bottom_y may change it.method, it.position, etc. */
1243 int bottom_y = (last_height = 0, line_bottom_y (&it));
1244 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1245
1246 if (top_y < window_top_y)
1247 visible_p = bottom_y > window_top_y;
1248 else if (top_y < it.last_visible_y)
1249 visible_p = 1;
1250 if (visible_p)
1251 {
1252 if (it_method == GET_FROM_DISPLAY_VECTOR)
1253 {
1254 /* We stopped on the last glyph of a display vector.
1255 Try and recompute. Hack alert! */
1256 if (charpos < 2 || top.charpos >= charpos)
1257 top_x = it.glyph_row->x;
1258 else
1259 {
1260 struct it it2;
1261 start_display (&it2, w, top);
1262 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1263 get_next_display_element (&it2);
1264 PRODUCE_GLYPHS (&it2);
1265 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1266 || it2.current_x > it2.last_visible_x)
1267 top_x = it.glyph_row->x;
1268 else
1269 {
1270 top_x = it2.current_x;
1271 top_y = it2.current_y;
1272 }
1273 }
1274 }
1275
1276 *x = top_x;
1277 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1278 *rtop = max (0, window_top_y - top_y);
1279 *rbot = max (0, bottom_y - it.last_visible_y);
1280 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1281 - max (top_y, window_top_y)));
1282 *vpos = it.vpos;
1283 }
1284 }
1285 else
1286 {
1287 struct it it2;
1288
1289 it2 = it;
1290 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1291 move_it_by_lines (&it, 1);
1292 if (charpos < IT_CHARPOS (it)
1293 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1294 {
1295 visible_p = 1;
1296 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1297 *x = it2.current_x;
1298 *y = it2.current_y + it2.max_ascent - it2.ascent;
1299 *rtop = max (0, -it2.current_y);
1300 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1301 - it.last_visible_y));
1302 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1303 it.last_visible_y)
1304 - max (it2.current_y,
1305 WINDOW_HEADER_LINE_HEIGHT (w))));
1306 *vpos = it2.vpos;
1307 }
1308 }
1309
1310 if (old_buffer)
1311 set_buffer_internal_1 (old_buffer);
1312
1313 current_header_line_height = current_mode_line_height = -1;
1314
1315 if (visible_p && XFASTINT (w->hscroll) > 0)
1316 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1317
1318 #if 0
1319 /* Debugging code. */
1320 if (visible_p)
1321 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1322 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1323 else
1324 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1325 #endif
1326
1327 return visible_p;
1328 }
1329
1330
1331 /* Return the next character from STR. Return in *LEN the length of
1332 the character. This is like STRING_CHAR_AND_LENGTH but never
1333 returns an invalid character. If we find one, we return a `?', but
1334 with the length of the invalid character. */
1335
1336 static INLINE int
1337 string_char_and_length (const unsigned char *str, int *len)
1338 {
1339 int c;
1340
1341 c = STRING_CHAR_AND_LENGTH (str, *len);
1342 if (!CHAR_VALID_P (c, 1))
1343 /* We may not change the length here because other places in Emacs
1344 don't use this function, i.e. they silently accept invalid
1345 characters. */
1346 c = '?';
1347
1348 return c;
1349 }
1350
1351
1352
1353 /* Given a position POS containing a valid character and byte position
1354 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1355
1356 static struct text_pos
1357 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1358 {
1359 xassert (STRINGP (string) && nchars >= 0);
1360
1361 if (STRING_MULTIBYTE (string))
1362 {
1363 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1364 int len;
1365
1366 while (nchars--)
1367 {
1368 string_char_and_length (p, &len);
1369 p += len;
1370 CHARPOS (pos) += 1;
1371 BYTEPOS (pos) += len;
1372 }
1373 }
1374 else
1375 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1376
1377 return pos;
1378 }
1379
1380
1381 /* Value is the text position, i.e. character and byte position,
1382 for character position CHARPOS in STRING. */
1383
1384 static INLINE struct text_pos
1385 string_pos (EMACS_INT charpos, Lisp_Object string)
1386 {
1387 struct text_pos pos;
1388 xassert (STRINGP (string));
1389 xassert (charpos >= 0);
1390 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1391 return pos;
1392 }
1393
1394
1395 /* Value is a text position, i.e. character and byte position, for
1396 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1397 means recognize multibyte characters. */
1398
1399 static struct text_pos
1400 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1401 {
1402 struct text_pos pos;
1403
1404 xassert (s != NULL);
1405 xassert (charpos >= 0);
1406
1407 if (multibyte_p)
1408 {
1409 int len;
1410
1411 SET_TEXT_POS (pos, 0, 0);
1412 while (charpos--)
1413 {
1414 string_char_and_length ((const unsigned char *) s, &len);
1415 s += len;
1416 CHARPOS (pos) += 1;
1417 BYTEPOS (pos) += len;
1418 }
1419 }
1420 else
1421 SET_TEXT_POS (pos, charpos, charpos);
1422
1423 return pos;
1424 }
1425
1426
1427 /* Value is the number of characters in C string S. MULTIBYTE_P
1428 non-zero means recognize multibyte characters. */
1429
1430 static EMACS_INT
1431 number_of_chars (const char *s, int multibyte_p)
1432 {
1433 EMACS_INT nchars;
1434
1435 if (multibyte_p)
1436 {
1437 EMACS_INT rest = strlen (s);
1438 int len;
1439 const unsigned char *p = (const unsigned char *) s;
1440
1441 for (nchars = 0; rest > 0; ++nchars)
1442 {
1443 string_char_and_length (p, &len);
1444 rest -= len, p += len;
1445 }
1446 }
1447 else
1448 nchars = strlen (s);
1449
1450 return nchars;
1451 }
1452
1453
1454 /* Compute byte position NEWPOS->bytepos corresponding to
1455 NEWPOS->charpos. POS is a known position in string STRING.
1456 NEWPOS->charpos must be >= POS.charpos. */
1457
1458 static void
1459 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1460 {
1461 xassert (STRINGP (string));
1462 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1463
1464 if (STRING_MULTIBYTE (string))
1465 *newpos = string_pos_nchars_ahead (pos, string,
1466 CHARPOS (*newpos) - CHARPOS (pos));
1467 else
1468 BYTEPOS (*newpos) = CHARPOS (*newpos);
1469 }
1470
1471 /* EXPORT:
1472 Return an estimation of the pixel height of mode or header lines on
1473 frame F. FACE_ID specifies what line's height to estimate. */
1474
1475 int
1476 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1477 {
1478 #ifdef HAVE_WINDOW_SYSTEM
1479 if (FRAME_WINDOW_P (f))
1480 {
1481 int height = FONT_HEIGHT (FRAME_FONT (f));
1482
1483 /* This function is called so early when Emacs starts that the face
1484 cache and mode line face are not yet initialized. */
1485 if (FRAME_FACE_CACHE (f))
1486 {
1487 struct face *face = FACE_FROM_ID (f, face_id);
1488 if (face)
1489 {
1490 if (face->font)
1491 height = FONT_HEIGHT (face->font);
1492 if (face->box_line_width > 0)
1493 height += 2 * face->box_line_width;
1494 }
1495 }
1496
1497 return height;
1498 }
1499 #endif
1500
1501 return 1;
1502 }
1503
1504 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1505 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1506 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1507 not force the value into range. */
1508
1509 void
1510 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1511 int *x, int *y, NativeRectangle *bounds, int noclip)
1512 {
1513
1514 #ifdef HAVE_WINDOW_SYSTEM
1515 if (FRAME_WINDOW_P (f))
1516 {
1517 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1518 even for negative values. */
1519 if (pix_x < 0)
1520 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1521 if (pix_y < 0)
1522 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1523
1524 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1525 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1526
1527 if (bounds)
1528 STORE_NATIVE_RECT (*bounds,
1529 FRAME_COL_TO_PIXEL_X (f, pix_x),
1530 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1531 FRAME_COLUMN_WIDTH (f) - 1,
1532 FRAME_LINE_HEIGHT (f) - 1);
1533
1534 if (!noclip)
1535 {
1536 if (pix_x < 0)
1537 pix_x = 0;
1538 else if (pix_x > FRAME_TOTAL_COLS (f))
1539 pix_x = FRAME_TOTAL_COLS (f);
1540
1541 if (pix_y < 0)
1542 pix_y = 0;
1543 else if (pix_y > FRAME_LINES (f))
1544 pix_y = FRAME_LINES (f);
1545 }
1546 }
1547 #endif
1548
1549 *x = pix_x;
1550 *y = pix_y;
1551 }
1552
1553
1554 /* Find the glyph under window-relative coordinates X/Y in window W.
1555 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1556 strings. Return in *HPOS and *VPOS the row and column number of
1557 the glyph found. Return in *AREA the glyph area containing X.
1558 Value is a pointer to the glyph found or null if X/Y is not on
1559 text, or we can't tell because W's current matrix is not up to
1560 date. */
1561
1562 static
1563 struct glyph *
1564 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1565 int *dx, int *dy, int *area)
1566 {
1567 struct glyph *glyph, *end;
1568 struct glyph_row *row = NULL;
1569 int x0, i;
1570
1571 /* Find row containing Y. Give up if some row is not enabled. */
1572 for (i = 0; i < w->current_matrix->nrows; ++i)
1573 {
1574 row = MATRIX_ROW (w->current_matrix, i);
1575 if (!row->enabled_p)
1576 return NULL;
1577 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1578 break;
1579 }
1580
1581 *vpos = i;
1582 *hpos = 0;
1583
1584 /* Give up if Y is not in the window. */
1585 if (i == w->current_matrix->nrows)
1586 return NULL;
1587
1588 /* Get the glyph area containing X. */
1589 if (w->pseudo_window_p)
1590 {
1591 *area = TEXT_AREA;
1592 x0 = 0;
1593 }
1594 else
1595 {
1596 if (x < window_box_left_offset (w, TEXT_AREA))
1597 {
1598 *area = LEFT_MARGIN_AREA;
1599 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1600 }
1601 else if (x < window_box_right_offset (w, TEXT_AREA))
1602 {
1603 *area = TEXT_AREA;
1604 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1605 }
1606 else
1607 {
1608 *area = RIGHT_MARGIN_AREA;
1609 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1610 }
1611 }
1612
1613 /* Find glyph containing X. */
1614 glyph = row->glyphs[*area];
1615 end = glyph + row->used[*area];
1616 x -= x0;
1617 while (glyph < end && x >= glyph->pixel_width)
1618 {
1619 x -= glyph->pixel_width;
1620 ++glyph;
1621 }
1622
1623 if (glyph == end)
1624 return NULL;
1625
1626 if (dx)
1627 {
1628 *dx = x;
1629 *dy = y - (row->y + row->ascent - glyph->ascent);
1630 }
1631
1632 *hpos = glyph - row->glyphs[*area];
1633 return glyph;
1634 }
1635
1636 /* Convert frame-relative x/y to coordinates relative to window W.
1637 Takes pseudo-windows into account. */
1638
1639 static void
1640 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1641 {
1642 if (w->pseudo_window_p)
1643 {
1644 /* A pseudo-window is always full-width, and starts at the
1645 left edge of the frame, plus a frame border. */
1646 struct frame *f = XFRAME (w->frame);
1647 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1648 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1649 }
1650 else
1651 {
1652 *x -= WINDOW_LEFT_EDGE_X (w);
1653 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1654 }
1655 }
1656
1657 #ifdef HAVE_WINDOW_SYSTEM
1658
1659 /* EXPORT:
1660 Return in RECTS[] at most N clipping rectangles for glyph string S.
1661 Return the number of stored rectangles. */
1662
1663 int
1664 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1665 {
1666 XRectangle r;
1667
1668 if (n <= 0)
1669 return 0;
1670
1671 if (s->row->full_width_p)
1672 {
1673 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1674 r.x = WINDOW_LEFT_EDGE_X (s->w);
1675 r.width = WINDOW_TOTAL_WIDTH (s->w);
1676
1677 /* Unless displaying a mode or menu bar line, which are always
1678 fully visible, clip to the visible part of the row. */
1679 if (s->w->pseudo_window_p)
1680 r.height = s->row->visible_height;
1681 else
1682 r.height = s->height;
1683 }
1684 else
1685 {
1686 /* This is a text line that may be partially visible. */
1687 r.x = window_box_left (s->w, s->area);
1688 r.width = window_box_width (s->w, s->area);
1689 r.height = s->row->visible_height;
1690 }
1691
1692 if (s->clip_head)
1693 if (r.x < s->clip_head->x)
1694 {
1695 if (r.width >= s->clip_head->x - r.x)
1696 r.width -= s->clip_head->x - r.x;
1697 else
1698 r.width = 0;
1699 r.x = s->clip_head->x;
1700 }
1701 if (s->clip_tail)
1702 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1703 {
1704 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1705 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1706 else
1707 r.width = 0;
1708 }
1709
1710 /* If S draws overlapping rows, it's sufficient to use the top and
1711 bottom of the window for clipping because this glyph string
1712 intentionally draws over other lines. */
1713 if (s->for_overlaps)
1714 {
1715 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1716 r.height = window_text_bottom_y (s->w) - r.y;
1717
1718 /* Alas, the above simple strategy does not work for the
1719 environments with anti-aliased text: if the same text is
1720 drawn onto the same place multiple times, it gets thicker.
1721 If the overlap we are processing is for the erased cursor, we
1722 take the intersection with the rectagle of the cursor. */
1723 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1724 {
1725 XRectangle rc, r_save = r;
1726
1727 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1728 rc.y = s->w->phys_cursor.y;
1729 rc.width = s->w->phys_cursor_width;
1730 rc.height = s->w->phys_cursor_height;
1731
1732 x_intersect_rectangles (&r_save, &rc, &r);
1733 }
1734 }
1735 else
1736 {
1737 /* Don't use S->y for clipping because it doesn't take partially
1738 visible lines into account. For example, it can be negative for
1739 partially visible lines at the top of a window. */
1740 if (!s->row->full_width_p
1741 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1742 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1743 else
1744 r.y = max (0, s->row->y);
1745 }
1746
1747 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1748
1749 /* If drawing the cursor, don't let glyph draw outside its
1750 advertised boundaries. Cleartype does this under some circumstances. */
1751 if (s->hl == DRAW_CURSOR)
1752 {
1753 struct glyph *glyph = s->first_glyph;
1754 int height, max_y;
1755
1756 if (s->x > r.x)
1757 {
1758 r.width -= s->x - r.x;
1759 r.x = s->x;
1760 }
1761 r.width = min (r.width, glyph->pixel_width);
1762
1763 /* If r.y is below window bottom, ensure that we still see a cursor. */
1764 height = min (glyph->ascent + glyph->descent,
1765 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1766 max_y = window_text_bottom_y (s->w) - height;
1767 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1768 if (s->ybase - glyph->ascent > max_y)
1769 {
1770 r.y = max_y;
1771 r.height = height;
1772 }
1773 else
1774 {
1775 /* Don't draw cursor glyph taller than our actual glyph. */
1776 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1777 if (height < r.height)
1778 {
1779 max_y = r.y + r.height;
1780 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1781 r.height = min (max_y - r.y, height);
1782 }
1783 }
1784 }
1785
1786 if (s->row->clip)
1787 {
1788 XRectangle r_save = r;
1789
1790 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1791 r.width = 0;
1792 }
1793
1794 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1795 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1796 {
1797 #ifdef CONVERT_FROM_XRECT
1798 CONVERT_FROM_XRECT (r, *rects);
1799 #else
1800 *rects = r;
1801 #endif
1802 return 1;
1803 }
1804 else
1805 {
1806 /* If we are processing overlapping and allowed to return
1807 multiple clipping rectangles, we exclude the row of the glyph
1808 string from the clipping rectangle. This is to avoid drawing
1809 the same text on the environment with anti-aliasing. */
1810 #ifdef CONVERT_FROM_XRECT
1811 XRectangle rs[2];
1812 #else
1813 XRectangle *rs = rects;
1814 #endif
1815 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1816
1817 if (s->for_overlaps & OVERLAPS_PRED)
1818 {
1819 rs[i] = r;
1820 if (r.y + r.height > row_y)
1821 {
1822 if (r.y < row_y)
1823 rs[i].height = row_y - r.y;
1824 else
1825 rs[i].height = 0;
1826 }
1827 i++;
1828 }
1829 if (s->for_overlaps & OVERLAPS_SUCC)
1830 {
1831 rs[i] = r;
1832 if (r.y < row_y + s->row->visible_height)
1833 {
1834 if (r.y + r.height > row_y + s->row->visible_height)
1835 {
1836 rs[i].y = row_y + s->row->visible_height;
1837 rs[i].height = r.y + r.height - rs[i].y;
1838 }
1839 else
1840 rs[i].height = 0;
1841 }
1842 i++;
1843 }
1844
1845 n = i;
1846 #ifdef CONVERT_FROM_XRECT
1847 for (i = 0; i < n; i++)
1848 CONVERT_FROM_XRECT (rs[i], rects[i]);
1849 #endif
1850 return n;
1851 }
1852 }
1853
1854 /* EXPORT:
1855 Return in *NR the clipping rectangle for glyph string S. */
1856
1857 void
1858 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1859 {
1860 get_glyph_string_clip_rects (s, nr, 1);
1861 }
1862
1863
1864 /* EXPORT:
1865 Return the position and height of the phys cursor in window W.
1866 Set w->phys_cursor_width to width of phys cursor.
1867 */
1868
1869 void
1870 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1871 struct glyph *glyph, int *xp, int *yp, int *heightp)
1872 {
1873 struct frame *f = XFRAME (WINDOW_FRAME (w));
1874 int x, y, wd, h, h0, y0;
1875
1876 /* Compute the width of the rectangle to draw. If on a stretch
1877 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1878 rectangle as wide as the glyph, but use a canonical character
1879 width instead. */
1880 wd = glyph->pixel_width - 1;
1881 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1882 wd++; /* Why? */
1883 #endif
1884
1885 x = w->phys_cursor.x;
1886 if (x < 0)
1887 {
1888 wd += x;
1889 x = 0;
1890 }
1891
1892 if (glyph->type == STRETCH_GLYPH
1893 && !x_stretch_cursor_p)
1894 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1895 w->phys_cursor_width = wd;
1896
1897 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1898
1899 /* If y is below window bottom, ensure that we still see a cursor. */
1900 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1901
1902 h = max (h0, glyph->ascent + glyph->descent);
1903 h0 = min (h0, glyph->ascent + glyph->descent);
1904
1905 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1906 if (y < y0)
1907 {
1908 h = max (h - (y0 - y) + 1, h0);
1909 y = y0 - 1;
1910 }
1911 else
1912 {
1913 y0 = window_text_bottom_y (w) - h0;
1914 if (y > y0)
1915 {
1916 h += y - y0;
1917 y = y0;
1918 }
1919 }
1920
1921 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1922 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1923 *heightp = h;
1924 }
1925
1926 /*
1927 * Remember which glyph the mouse is over.
1928 */
1929
1930 void
1931 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1932 {
1933 Lisp_Object window;
1934 struct window *w;
1935 struct glyph_row *r, *gr, *end_row;
1936 enum window_part part;
1937 enum glyph_row_area area;
1938 int x, y, width, height;
1939
1940 /* Try to determine frame pixel position and size of the glyph under
1941 frame pixel coordinates X/Y on frame F. */
1942
1943 if (!f->glyphs_initialized_p
1944 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1945 NILP (window)))
1946 {
1947 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1948 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1949 goto virtual_glyph;
1950 }
1951
1952 w = XWINDOW (window);
1953 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1954 height = WINDOW_FRAME_LINE_HEIGHT (w);
1955
1956 x = window_relative_x_coord (w, part, gx);
1957 y = gy - WINDOW_TOP_EDGE_Y (w);
1958
1959 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1960 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1961
1962 if (w->pseudo_window_p)
1963 {
1964 area = TEXT_AREA;
1965 part = ON_MODE_LINE; /* Don't adjust margin. */
1966 goto text_glyph;
1967 }
1968
1969 switch (part)
1970 {
1971 case ON_LEFT_MARGIN:
1972 area = LEFT_MARGIN_AREA;
1973 goto text_glyph;
1974
1975 case ON_RIGHT_MARGIN:
1976 area = RIGHT_MARGIN_AREA;
1977 goto text_glyph;
1978
1979 case ON_HEADER_LINE:
1980 case ON_MODE_LINE:
1981 gr = (part == ON_HEADER_LINE
1982 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1983 : MATRIX_MODE_LINE_ROW (w->current_matrix));
1984 gy = gr->y;
1985 area = TEXT_AREA;
1986 goto text_glyph_row_found;
1987
1988 case ON_TEXT:
1989 area = TEXT_AREA;
1990
1991 text_glyph:
1992 gr = 0; gy = 0;
1993 for (; r <= end_row && r->enabled_p; ++r)
1994 if (r->y + r->height > y)
1995 {
1996 gr = r; gy = r->y;
1997 break;
1998 }
1999
2000 text_glyph_row_found:
2001 if (gr && gy <= y)
2002 {
2003 struct glyph *g = gr->glyphs[area];
2004 struct glyph *end = g + gr->used[area];
2005
2006 height = gr->height;
2007 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2008 if (gx + g->pixel_width > x)
2009 break;
2010
2011 if (g < end)
2012 {
2013 if (g->type == IMAGE_GLYPH)
2014 {
2015 /* Don't remember when mouse is over image, as
2016 image may have hot-spots. */
2017 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2018 return;
2019 }
2020 width = g->pixel_width;
2021 }
2022 else
2023 {
2024 /* Use nominal char spacing at end of line. */
2025 x -= gx;
2026 gx += (x / width) * width;
2027 }
2028
2029 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2030 gx += window_box_left_offset (w, area);
2031 }
2032 else
2033 {
2034 /* Use nominal line height at end of window. */
2035 gx = (x / width) * width;
2036 y -= gy;
2037 gy += (y / height) * height;
2038 }
2039 break;
2040
2041 case ON_LEFT_FRINGE:
2042 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2043 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2044 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2045 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2046 goto row_glyph;
2047
2048 case ON_RIGHT_FRINGE:
2049 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2050 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2051 : window_box_right_offset (w, TEXT_AREA));
2052 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2053 goto row_glyph;
2054
2055 case ON_SCROLL_BAR:
2056 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2057 ? 0
2058 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2059 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2060 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2061 : 0)));
2062 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2063
2064 row_glyph:
2065 gr = 0, gy = 0;
2066 for (; r <= end_row && r->enabled_p; ++r)
2067 if (r->y + r->height > y)
2068 {
2069 gr = r; gy = r->y;
2070 break;
2071 }
2072
2073 if (gr && gy <= y)
2074 height = gr->height;
2075 else
2076 {
2077 /* Use nominal line height at end of window. */
2078 y -= gy;
2079 gy += (y / height) * height;
2080 }
2081 break;
2082
2083 default:
2084 ;
2085 virtual_glyph:
2086 /* If there is no glyph under the mouse, then we divide the screen
2087 into a grid of the smallest glyph in the frame, and use that
2088 as our "glyph". */
2089
2090 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2091 round down even for negative values. */
2092 if (gx < 0)
2093 gx -= width - 1;
2094 if (gy < 0)
2095 gy -= height - 1;
2096
2097 gx = (gx / width) * width;
2098 gy = (gy / height) * height;
2099
2100 goto store_rect;
2101 }
2102
2103 gx += WINDOW_LEFT_EDGE_X (w);
2104 gy += WINDOW_TOP_EDGE_Y (w);
2105
2106 store_rect:
2107 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2108
2109 /* Visible feedback for debugging. */
2110 #if 0
2111 #if HAVE_X_WINDOWS
2112 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2113 f->output_data.x->normal_gc,
2114 gx, gy, width, height);
2115 #endif
2116 #endif
2117 }
2118
2119
2120 #endif /* HAVE_WINDOW_SYSTEM */
2121
2122 \f
2123 /***********************************************************************
2124 Lisp form evaluation
2125 ***********************************************************************/
2126
2127 /* Error handler for safe_eval and safe_call. */
2128
2129 static Lisp_Object
2130 safe_eval_handler (Lisp_Object arg)
2131 {
2132 add_to_log ("Error during redisplay: %S", arg, Qnil);
2133 return Qnil;
2134 }
2135
2136
2137 /* Evaluate SEXPR and return the result, or nil if something went
2138 wrong. Prevent redisplay during the evaluation. */
2139
2140 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2141 Return the result, or nil if something went wrong. Prevent
2142 redisplay during the evaluation. */
2143
2144 Lisp_Object
2145 safe_call (size_t nargs, Lisp_Object *args)
2146 {
2147 Lisp_Object val;
2148
2149 if (inhibit_eval_during_redisplay)
2150 val = Qnil;
2151 else
2152 {
2153 int count = SPECPDL_INDEX ();
2154 struct gcpro gcpro1;
2155
2156 GCPRO1 (args[0]);
2157 gcpro1.nvars = nargs;
2158 specbind (Qinhibit_redisplay, Qt);
2159 /* Use Qt to ensure debugger does not run,
2160 so there is no possibility of wanting to redisplay. */
2161 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2162 safe_eval_handler);
2163 UNGCPRO;
2164 val = unbind_to (count, val);
2165 }
2166
2167 return val;
2168 }
2169
2170
2171 /* Call function FN with one argument ARG.
2172 Return the result, or nil if something went wrong. */
2173
2174 Lisp_Object
2175 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2176 {
2177 Lisp_Object args[2];
2178 args[0] = fn;
2179 args[1] = arg;
2180 return safe_call (2, args);
2181 }
2182
2183 static Lisp_Object Qeval;
2184
2185 Lisp_Object
2186 safe_eval (Lisp_Object sexpr)
2187 {
2188 return safe_call1 (Qeval, sexpr);
2189 }
2190
2191 /* Call function FN with one argument ARG.
2192 Return the result, or nil if something went wrong. */
2193
2194 Lisp_Object
2195 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2196 {
2197 Lisp_Object args[3];
2198 args[0] = fn;
2199 args[1] = arg1;
2200 args[2] = arg2;
2201 return safe_call (3, args);
2202 }
2203
2204
2205 \f
2206 /***********************************************************************
2207 Debugging
2208 ***********************************************************************/
2209
2210 #if 0
2211
2212 /* Define CHECK_IT to perform sanity checks on iterators.
2213 This is for debugging. It is too slow to do unconditionally. */
2214
2215 static void
2216 check_it (it)
2217 struct it *it;
2218 {
2219 if (it->method == GET_FROM_STRING)
2220 {
2221 xassert (STRINGP (it->string));
2222 xassert (IT_STRING_CHARPOS (*it) >= 0);
2223 }
2224 else
2225 {
2226 xassert (IT_STRING_CHARPOS (*it) < 0);
2227 if (it->method == GET_FROM_BUFFER)
2228 {
2229 /* Check that character and byte positions agree. */
2230 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2231 }
2232 }
2233
2234 if (it->dpvec)
2235 xassert (it->current.dpvec_index >= 0);
2236 else
2237 xassert (it->current.dpvec_index < 0);
2238 }
2239
2240 #define CHECK_IT(IT) check_it ((IT))
2241
2242 #else /* not 0 */
2243
2244 #define CHECK_IT(IT) (void) 0
2245
2246 #endif /* not 0 */
2247
2248
2249 #if GLYPH_DEBUG
2250
2251 /* Check that the window end of window W is what we expect it
2252 to be---the last row in the current matrix displaying text. */
2253
2254 static void
2255 check_window_end (w)
2256 struct window *w;
2257 {
2258 if (!MINI_WINDOW_P (w)
2259 && !NILP (w->window_end_valid))
2260 {
2261 struct glyph_row *row;
2262 xassert ((row = MATRIX_ROW (w->current_matrix,
2263 XFASTINT (w->window_end_vpos)),
2264 !row->enabled_p
2265 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2266 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2267 }
2268 }
2269
2270 #define CHECK_WINDOW_END(W) check_window_end ((W))
2271
2272 #else /* not GLYPH_DEBUG */
2273
2274 #define CHECK_WINDOW_END(W) (void) 0
2275
2276 #endif /* not GLYPH_DEBUG */
2277
2278
2279 \f
2280 /***********************************************************************
2281 Iterator initialization
2282 ***********************************************************************/
2283
2284 /* Initialize IT for displaying current_buffer in window W, starting
2285 at character position CHARPOS. CHARPOS < 0 means that no buffer
2286 position is specified which is useful when the iterator is assigned
2287 a position later. BYTEPOS is the byte position corresponding to
2288 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2289
2290 If ROW is not null, calls to produce_glyphs with IT as parameter
2291 will produce glyphs in that row.
2292
2293 BASE_FACE_ID is the id of a base face to use. It must be one of
2294 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2295 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2296 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2297
2298 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2299 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2300 will be initialized to use the corresponding mode line glyph row of
2301 the desired matrix of W. */
2302
2303 void
2304 init_iterator (struct it *it, struct window *w,
2305 EMACS_INT charpos, EMACS_INT bytepos,
2306 struct glyph_row *row, enum face_id base_face_id)
2307 {
2308 int highlight_region_p;
2309 enum face_id remapped_base_face_id = base_face_id;
2310
2311 /* Some precondition checks. */
2312 xassert (w != NULL && it != NULL);
2313 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2314 && charpos <= ZV));
2315
2316 /* If face attributes have been changed since the last redisplay,
2317 free realized faces now because they depend on face definitions
2318 that might have changed. Don't free faces while there might be
2319 desired matrices pending which reference these faces. */
2320 if (face_change_count && !inhibit_free_realized_faces)
2321 {
2322 face_change_count = 0;
2323 free_all_realized_faces (Qnil);
2324 }
2325
2326 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2327 if (! NILP (Vface_remapping_alist))
2328 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2329
2330 /* Use one of the mode line rows of W's desired matrix if
2331 appropriate. */
2332 if (row == NULL)
2333 {
2334 if (base_face_id == MODE_LINE_FACE_ID
2335 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2336 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2337 else if (base_face_id == HEADER_LINE_FACE_ID)
2338 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2339 }
2340
2341 /* Clear IT. */
2342 memset (it, 0, sizeof *it);
2343 it->current.overlay_string_index = -1;
2344 it->current.dpvec_index = -1;
2345 it->base_face_id = remapped_base_face_id;
2346 it->string = Qnil;
2347 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2348 it->paragraph_embedding = L2R;
2349 it->bidi_it.string.lstring = Qnil;
2350 it->bidi_it.string.s = NULL;
2351 it->bidi_it.string.bufpos = 0;
2352
2353 /* The window in which we iterate over current_buffer: */
2354 XSETWINDOW (it->window, w);
2355 it->w = w;
2356 it->f = XFRAME (w->frame);
2357
2358 it->cmp_it.id = -1;
2359
2360 /* Extra space between lines (on window systems only). */
2361 if (base_face_id == DEFAULT_FACE_ID
2362 && FRAME_WINDOW_P (it->f))
2363 {
2364 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2365 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2366 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2367 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2368 * FRAME_LINE_HEIGHT (it->f));
2369 else if (it->f->extra_line_spacing > 0)
2370 it->extra_line_spacing = it->f->extra_line_spacing;
2371 it->max_extra_line_spacing = 0;
2372 }
2373
2374 /* If realized faces have been removed, e.g. because of face
2375 attribute changes of named faces, recompute them. When running
2376 in batch mode, the face cache of the initial frame is null. If
2377 we happen to get called, make a dummy face cache. */
2378 if (FRAME_FACE_CACHE (it->f) == NULL)
2379 init_frame_faces (it->f);
2380 if (FRAME_FACE_CACHE (it->f)->used == 0)
2381 recompute_basic_faces (it->f);
2382
2383 /* Current value of the `slice', `space-width', and 'height' properties. */
2384 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2385 it->space_width = Qnil;
2386 it->font_height = Qnil;
2387 it->override_ascent = -1;
2388
2389 /* Are control characters displayed as `^C'? */
2390 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2391
2392 /* -1 means everything between a CR and the following line end
2393 is invisible. >0 means lines indented more than this value are
2394 invisible. */
2395 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2396 ? XFASTINT (BVAR (current_buffer, selective_display))
2397 : (!NILP (BVAR (current_buffer, selective_display))
2398 ? -1 : 0));
2399 it->selective_display_ellipsis_p
2400 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2401
2402 /* Display table to use. */
2403 it->dp = window_display_table (w);
2404
2405 /* Are multibyte characters enabled in current_buffer? */
2406 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2407
2408 /* Non-zero if we should highlight the region. */
2409 highlight_region_p
2410 = (!NILP (Vtransient_mark_mode)
2411 && !NILP (BVAR (current_buffer, mark_active))
2412 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2413
2414 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2415 start and end of a visible region in window IT->w. Set both to
2416 -1 to indicate no region. */
2417 if (highlight_region_p
2418 /* Maybe highlight only in selected window. */
2419 && (/* Either show region everywhere. */
2420 highlight_nonselected_windows
2421 /* Or show region in the selected window. */
2422 || w == XWINDOW (selected_window)
2423 /* Or show the region if we are in the mini-buffer and W is
2424 the window the mini-buffer refers to. */
2425 || (MINI_WINDOW_P (XWINDOW (selected_window))
2426 && WINDOWP (minibuf_selected_window)
2427 && w == XWINDOW (minibuf_selected_window))))
2428 {
2429 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2430 it->region_beg_charpos = min (PT, markpos);
2431 it->region_end_charpos = max (PT, markpos);
2432 }
2433 else
2434 it->region_beg_charpos = it->region_end_charpos = -1;
2435
2436 /* Get the position at which the redisplay_end_trigger hook should
2437 be run, if it is to be run at all. */
2438 if (MARKERP (w->redisplay_end_trigger)
2439 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2440 it->redisplay_end_trigger_charpos
2441 = marker_position (w->redisplay_end_trigger);
2442 else if (INTEGERP (w->redisplay_end_trigger))
2443 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2444
2445 /* Correct bogus values of tab_width. */
2446 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2447 if (it->tab_width <= 0 || it->tab_width > 1000)
2448 it->tab_width = 8;
2449
2450 /* Are lines in the display truncated? */
2451 if (base_face_id != DEFAULT_FACE_ID
2452 || XINT (it->w->hscroll)
2453 || (! WINDOW_FULL_WIDTH_P (it->w)
2454 && ((!NILP (Vtruncate_partial_width_windows)
2455 && !INTEGERP (Vtruncate_partial_width_windows))
2456 || (INTEGERP (Vtruncate_partial_width_windows)
2457 && (WINDOW_TOTAL_COLS (it->w)
2458 < XINT (Vtruncate_partial_width_windows))))))
2459 it->line_wrap = TRUNCATE;
2460 else if (NILP (BVAR (current_buffer, truncate_lines)))
2461 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2462 ? WINDOW_WRAP : WORD_WRAP;
2463 else
2464 it->line_wrap = TRUNCATE;
2465
2466 /* Get dimensions of truncation and continuation glyphs. These are
2467 displayed as fringe bitmaps under X, so we don't need them for such
2468 frames. */
2469 if (!FRAME_WINDOW_P (it->f))
2470 {
2471 if (it->line_wrap == TRUNCATE)
2472 {
2473 /* We will need the truncation glyph. */
2474 xassert (it->glyph_row == NULL);
2475 produce_special_glyphs (it, IT_TRUNCATION);
2476 it->truncation_pixel_width = it->pixel_width;
2477 }
2478 else
2479 {
2480 /* We will need the continuation glyph. */
2481 xassert (it->glyph_row == NULL);
2482 produce_special_glyphs (it, IT_CONTINUATION);
2483 it->continuation_pixel_width = it->pixel_width;
2484 }
2485
2486 /* Reset these values to zero because the produce_special_glyphs
2487 above has changed them. */
2488 it->pixel_width = it->ascent = it->descent = 0;
2489 it->phys_ascent = it->phys_descent = 0;
2490 }
2491
2492 /* Set this after getting the dimensions of truncation and
2493 continuation glyphs, so that we don't produce glyphs when calling
2494 produce_special_glyphs, above. */
2495 it->glyph_row = row;
2496 it->area = TEXT_AREA;
2497
2498 /* Forget any previous info about this row being reversed. */
2499 if (it->glyph_row)
2500 it->glyph_row->reversed_p = 0;
2501
2502 /* Get the dimensions of the display area. The display area
2503 consists of the visible window area plus a horizontally scrolled
2504 part to the left of the window. All x-values are relative to the
2505 start of this total display area. */
2506 if (base_face_id != DEFAULT_FACE_ID)
2507 {
2508 /* Mode lines, menu bar in terminal frames. */
2509 it->first_visible_x = 0;
2510 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2511 }
2512 else
2513 {
2514 it->first_visible_x
2515 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2516 it->last_visible_x = (it->first_visible_x
2517 + window_box_width (w, TEXT_AREA));
2518
2519 /* If we truncate lines, leave room for the truncator glyph(s) at
2520 the right margin. Otherwise, leave room for the continuation
2521 glyph(s). Truncation and continuation glyphs are not inserted
2522 for window-based redisplay. */
2523 if (!FRAME_WINDOW_P (it->f))
2524 {
2525 if (it->line_wrap == TRUNCATE)
2526 it->last_visible_x -= it->truncation_pixel_width;
2527 else
2528 it->last_visible_x -= it->continuation_pixel_width;
2529 }
2530
2531 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2532 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2533 }
2534
2535 /* Leave room for a border glyph. */
2536 if (!FRAME_WINDOW_P (it->f)
2537 && !WINDOW_RIGHTMOST_P (it->w))
2538 it->last_visible_x -= 1;
2539
2540 it->last_visible_y = window_text_bottom_y (w);
2541
2542 /* For mode lines and alike, arrange for the first glyph having a
2543 left box line if the face specifies a box. */
2544 if (base_face_id != DEFAULT_FACE_ID)
2545 {
2546 struct face *face;
2547
2548 it->face_id = remapped_base_face_id;
2549
2550 /* If we have a boxed mode line, make the first character appear
2551 with a left box line. */
2552 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2553 if (face->box != FACE_NO_BOX)
2554 it->start_of_box_run_p = 1;
2555 }
2556
2557 /* If a buffer position was specified, set the iterator there,
2558 getting overlays and face properties from that position. */
2559 if (charpos >= BUF_BEG (current_buffer))
2560 {
2561 it->end_charpos = ZV;
2562 it->face_id = -1;
2563 IT_CHARPOS (*it) = charpos;
2564
2565 /* Compute byte position if not specified. */
2566 if (bytepos < charpos)
2567 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2568 else
2569 IT_BYTEPOS (*it) = bytepos;
2570
2571 it->start = it->current;
2572 /* Do we need to reorder bidirectional text? Not if this is a
2573 unibyte buffer: by definition, none of the single-byte
2574 characters are strong R2L, so no reordering is needed. And
2575 bidi.c doesn't support unibyte buffers anyway. */
2576 it->bidi_p =
2577 !NILP (BVAR (current_buffer, bidi_display_reordering))
2578 && it->multibyte_p;
2579
2580 /* If we are to reorder bidirectional text, init the bidi
2581 iterator. */
2582 if (it->bidi_p)
2583 {
2584 /* Note the paragraph direction that this buffer wants to
2585 use. */
2586 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2587 Qleft_to_right))
2588 it->paragraph_embedding = L2R;
2589 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2590 Qright_to_left))
2591 it->paragraph_embedding = R2L;
2592 else
2593 it->paragraph_embedding = NEUTRAL_DIR;
2594 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2595 &it->bidi_it);
2596 }
2597
2598 /* Compute faces etc. */
2599 reseat (it, it->current.pos, 1);
2600 }
2601
2602 CHECK_IT (it);
2603 }
2604
2605
2606 /* Initialize IT for the display of window W with window start POS. */
2607
2608 void
2609 start_display (struct it *it, struct window *w, struct text_pos pos)
2610 {
2611 struct glyph_row *row;
2612 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2613
2614 row = w->desired_matrix->rows + first_vpos;
2615 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2616 it->first_vpos = first_vpos;
2617
2618 /* Don't reseat to previous visible line start if current start
2619 position is in a string or image. */
2620 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2621 {
2622 int start_at_line_beg_p;
2623 int first_y = it->current_y;
2624
2625 /* If window start is not at a line start, skip forward to POS to
2626 get the correct continuation lines width. */
2627 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2628 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2629 if (!start_at_line_beg_p)
2630 {
2631 int new_x;
2632
2633 reseat_at_previous_visible_line_start (it);
2634 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2635
2636 new_x = it->current_x + it->pixel_width;
2637
2638 /* If lines are continued, this line may end in the middle
2639 of a multi-glyph character (e.g. a control character
2640 displayed as \003, or in the middle of an overlay
2641 string). In this case move_it_to above will not have
2642 taken us to the start of the continuation line but to the
2643 end of the continued line. */
2644 if (it->current_x > 0
2645 && it->line_wrap != TRUNCATE /* Lines are continued. */
2646 && (/* And glyph doesn't fit on the line. */
2647 new_x > it->last_visible_x
2648 /* Or it fits exactly and we're on a window
2649 system frame. */
2650 || (new_x == it->last_visible_x
2651 && FRAME_WINDOW_P (it->f))))
2652 {
2653 if (it->current.dpvec_index >= 0
2654 || it->current.overlay_string_index >= 0)
2655 {
2656 set_iterator_to_next (it, 1);
2657 move_it_in_display_line_to (it, -1, -1, 0);
2658 }
2659
2660 it->continuation_lines_width += it->current_x;
2661 }
2662
2663 /* We're starting a new display line, not affected by the
2664 height of the continued line, so clear the appropriate
2665 fields in the iterator structure. */
2666 it->max_ascent = it->max_descent = 0;
2667 it->max_phys_ascent = it->max_phys_descent = 0;
2668
2669 it->current_y = first_y;
2670 it->vpos = 0;
2671 it->current_x = it->hpos = 0;
2672 }
2673 }
2674 }
2675
2676
2677 /* Return 1 if POS is a position in ellipses displayed for invisible
2678 text. W is the window we display, for text property lookup. */
2679
2680 static int
2681 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2682 {
2683 Lisp_Object prop, window;
2684 int ellipses_p = 0;
2685 EMACS_INT charpos = CHARPOS (pos->pos);
2686
2687 /* If POS specifies a position in a display vector, this might
2688 be for an ellipsis displayed for invisible text. We won't
2689 get the iterator set up for delivering that ellipsis unless
2690 we make sure that it gets aware of the invisible text. */
2691 if (pos->dpvec_index >= 0
2692 && pos->overlay_string_index < 0
2693 && CHARPOS (pos->string_pos) < 0
2694 && charpos > BEGV
2695 && (XSETWINDOW (window, w),
2696 prop = Fget_char_property (make_number (charpos),
2697 Qinvisible, window),
2698 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2699 {
2700 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2701 window);
2702 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2703 }
2704
2705 return ellipses_p;
2706 }
2707
2708
2709 /* Initialize IT for stepping through current_buffer in window W,
2710 starting at position POS that includes overlay string and display
2711 vector/ control character translation position information. Value
2712 is zero if there are overlay strings with newlines at POS. */
2713
2714 static int
2715 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2716 {
2717 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2718 int i, overlay_strings_with_newlines = 0;
2719
2720 /* If POS specifies a position in a display vector, this might
2721 be for an ellipsis displayed for invisible text. We won't
2722 get the iterator set up for delivering that ellipsis unless
2723 we make sure that it gets aware of the invisible text. */
2724 if (in_ellipses_for_invisible_text_p (pos, w))
2725 {
2726 --charpos;
2727 bytepos = 0;
2728 }
2729
2730 /* Keep in mind: the call to reseat in init_iterator skips invisible
2731 text, so we might end up at a position different from POS. This
2732 is only a problem when POS is a row start after a newline and an
2733 overlay starts there with an after-string, and the overlay has an
2734 invisible property. Since we don't skip invisible text in
2735 display_line and elsewhere immediately after consuming the
2736 newline before the row start, such a POS will not be in a string,
2737 but the call to init_iterator below will move us to the
2738 after-string. */
2739 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2740
2741 /* This only scans the current chunk -- it should scan all chunks.
2742 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2743 to 16 in 22.1 to make this a lesser problem. */
2744 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2745 {
2746 const char *s = SSDATA (it->overlay_strings[i]);
2747 const char *e = s + SBYTES (it->overlay_strings[i]);
2748
2749 while (s < e && *s != '\n')
2750 ++s;
2751
2752 if (s < e)
2753 {
2754 overlay_strings_with_newlines = 1;
2755 break;
2756 }
2757 }
2758
2759 /* If position is within an overlay string, set up IT to the right
2760 overlay string. */
2761 if (pos->overlay_string_index >= 0)
2762 {
2763 int relative_index;
2764
2765 /* If the first overlay string happens to have a `display'
2766 property for an image, the iterator will be set up for that
2767 image, and we have to undo that setup first before we can
2768 correct the overlay string index. */
2769 if (it->method == GET_FROM_IMAGE)
2770 pop_it (it);
2771
2772 /* We already have the first chunk of overlay strings in
2773 IT->overlay_strings. Load more until the one for
2774 pos->overlay_string_index is in IT->overlay_strings. */
2775 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2776 {
2777 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2778 it->current.overlay_string_index = 0;
2779 while (n--)
2780 {
2781 load_overlay_strings (it, 0);
2782 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2783 }
2784 }
2785
2786 it->current.overlay_string_index = pos->overlay_string_index;
2787 relative_index = (it->current.overlay_string_index
2788 % OVERLAY_STRING_CHUNK_SIZE);
2789 it->string = it->overlay_strings[relative_index];
2790 xassert (STRINGP (it->string));
2791 it->current.string_pos = pos->string_pos;
2792 it->method = GET_FROM_STRING;
2793 }
2794
2795 if (CHARPOS (pos->string_pos) >= 0)
2796 {
2797 /* Recorded position is not in an overlay string, but in another
2798 string. This can only be a string from a `display' property.
2799 IT should already be filled with that string. */
2800 it->current.string_pos = pos->string_pos;
2801 xassert (STRINGP (it->string));
2802 }
2803
2804 /* Restore position in display vector translations, control
2805 character translations or ellipses. */
2806 if (pos->dpvec_index >= 0)
2807 {
2808 if (it->dpvec == NULL)
2809 get_next_display_element (it);
2810 xassert (it->dpvec && it->current.dpvec_index == 0);
2811 it->current.dpvec_index = pos->dpvec_index;
2812 }
2813
2814 CHECK_IT (it);
2815 return !overlay_strings_with_newlines;
2816 }
2817
2818
2819 /* Initialize IT for stepping through current_buffer in window W
2820 starting at ROW->start. */
2821
2822 static void
2823 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2824 {
2825 init_from_display_pos (it, w, &row->start);
2826 it->start = row->start;
2827 it->continuation_lines_width = row->continuation_lines_width;
2828 CHECK_IT (it);
2829 }
2830
2831
2832 /* Initialize IT for stepping through current_buffer in window W
2833 starting in the line following ROW, i.e. starting at ROW->end.
2834 Value is zero if there are overlay strings with newlines at ROW's
2835 end position. */
2836
2837 static int
2838 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2839 {
2840 int success = 0;
2841
2842 if (init_from_display_pos (it, w, &row->end))
2843 {
2844 if (row->continued_p)
2845 it->continuation_lines_width
2846 = row->continuation_lines_width + row->pixel_width;
2847 CHECK_IT (it);
2848 success = 1;
2849 }
2850
2851 return success;
2852 }
2853
2854
2855
2856 \f
2857 /***********************************************************************
2858 Text properties
2859 ***********************************************************************/
2860
2861 /* Called when IT reaches IT->stop_charpos. Handle text property and
2862 overlay changes. Set IT->stop_charpos to the next position where
2863 to stop. */
2864
2865 static void
2866 handle_stop (struct it *it)
2867 {
2868 enum prop_handled handled;
2869 int handle_overlay_change_p;
2870 struct props *p;
2871
2872 it->dpvec = NULL;
2873 it->current.dpvec_index = -1;
2874 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2875 it->ignore_overlay_strings_at_pos_p = 0;
2876 it->ellipsis_p = 0;
2877
2878 /* Use face of preceding text for ellipsis (if invisible) */
2879 if (it->selective_display_ellipsis_p)
2880 it->saved_face_id = it->face_id;
2881
2882 do
2883 {
2884 handled = HANDLED_NORMALLY;
2885
2886 /* Call text property handlers. */
2887 for (p = it_props; p->handler; ++p)
2888 {
2889 handled = p->handler (it);
2890
2891 if (handled == HANDLED_RECOMPUTE_PROPS)
2892 break;
2893 else if (handled == HANDLED_RETURN)
2894 {
2895 /* We still want to show before and after strings from
2896 overlays even if the actual buffer text is replaced. */
2897 if (!handle_overlay_change_p
2898 || it->sp > 1
2899 || !get_overlay_strings_1 (it, 0, 0))
2900 {
2901 if (it->ellipsis_p)
2902 setup_for_ellipsis (it, 0);
2903 /* When handling a display spec, we might load an
2904 empty string. In that case, discard it here. We
2905 used to discard it in handle_single_display_spec,
2906 but that causes get_overlay_strings_1, above, to
2907 ignore overlay strings that we must check. */
2908 if (STRINGP (it->string) && !SCHARS (it->string))
2909 pop_it (it);
2910 return;
2911 }
2912 else if (STRINGP (it->string) && !SCHARS (it->string))
2913 pop_it (it);
2914 else
2915 {
2916 it->ignore_overlay_strings_at_pos_p = 1;
2917 it->string_from_display_prop_p = 0;
2918 handle_overlay_change_p = 0;
2919 }
2920 handled = HANDLED_RECOMPUTE_PROPS;
2921 break;
2922 }
2923 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2924 handle_overlay_change_p = 0;
2925 }
2926
2927 if (handled != HANDLED_RECOMPUTE_PROPS)
2928 {
2929 /* Don't check for overlay strings below when set to deliver
2930 characters from a display vector. */
2931 if (it->method == GET_FROM_DISPLAY_VECTOR)
2932 handle_overlay_change_p = 0;
2933
2934 /* Handle overlay changes.
2935 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2936 if it finds overlays. */
2937 if (handle_overlay_change_p)
2938 handled = handle_overlay_change (it);
2939 }
2940
2941 if (it->ellipsis_p)
2942 {
2943 setup_for_ellipsis (it, 0);
2944 break;
2945 }
2946 }
2947 while (handled == HANDLED_RECOMPUTE_PROPS);
2948
2949 /* Determine where to stop next. */
2950 if (handled == HANDLED_NORMALLY)
2951 compute_stop_pos (it);
2952 }
2953
2954
2955 /* Compute IT->stop_charpos from text property and overlay change
2956 information for IT's current position. */
2957
2958 static void
2959 compute_stop_pos (struct it *it)
2960 {
2961 register INTERVAL iv, next_iv;
2962 Lisp_Object object, limit, position;
2963 EMACS_INT charpos, bytepos;
2964
2965 /* If nowhere else, stop at the end. */
2966 it->stop_charpos = it->end_charpos;
2967
2968 if (STRINGP (it->string))
2969 {
2970 /* Strings are usually short, so don't limit the search for
2971 properties. */
2972 object = it->string;
2973 limit = Qnil;
2974 charpos = IT_STRING_CHARPOS (*it);
2975 bytepos = IT_STRING_BYTEPOS (*it);
2976 }
2977 else
2978 {
2979 EMACS_INT pos;
2980
2981 /* If next overlay change is in front of the current stop pos
2982 (which is IT->end_charpos), stop there. Note: value of
2983 next_overlay_change is point-max if no overlay change
2984 follows. */
2985 charpos = IT_CHARPOS (*it);
2986 bytepos = IT_BYTEPOS (*it);
2987 pos = next_overlay_change (charpos);
2988 if (pos < it->stop_charpos)
2989 it->stop_charpos = pos;
2990
2991 /* If showing the region, we have to stop at the region
2992 start or end because the face might change there. */
2993 if (it->region_beg_charpos > 0)
2994 {
2995 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2996 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2997 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2998 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2999 }
3000
3001 /* Set up variables for computing the stop position from text
3002 property changes. */
3003 XSETBUFFER (object, current_buffer);
3004 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3005 }
3006
3007 /* Get the interval containing IT's position. Value is a null
3008 interval if there isn't such an interval. */
3009 position = make_number (charpos);
3010 iv = validate_interval_range (object, &position, &position, 0);
3011 if (!NULL_INTERVAL_P (iv))
3012 {
3013 Lisp_Object values_here[LAST_PROP_IDX];
3014 struct props *p;
3015
3016 /* Get properties here. */
3017 for (p = it_props; p->handler; ++p)
3018 values_here[p->idx] = textget (iv->plist, *p->name);
3019
3020 /* Look for an interval following iv that has different
3021 properties. */
3022 for (next_iv = next_interval (iv);
3023 (!NULL_INTERVAL_P (next_iv)
3024 && (NILP (limit)
3025 || XFASTINT (limit) > next_iv->position));
3026 next_iv = next_interval (next_iv))
3027 {
3028 for (p = it_props; p->handler; ++p)
3029 {
3030 Lisp_Object new_value;
3031
3032 new_value = textget (next_iv->plist, *p->name);
3033 if (!EQ (values_here[p->idx], new_value))
3034 break;
3035 }
3036
3037 if (p->handler)
3038 break;
3039 }
3040
3041 if (!NULL_INTERVAL_P (next_iv))
3042 {
3043 if (INTEGERP (limit)
3044 && next_iv->position >= XFASTINT (limit))
3045 /* No text property change up to limit. */
3046 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3047 else
3048 /* Text properties change in next_iv. */
3049 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3050 }
3051 }
3052
3053 if (it->cmp_it.id < 0)
3054 {
3055 EMACS_INT stoppos = it->end_charpos;
3056
3057 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3058 stoppos = -1;
3059 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3060 stoppos, it->string);
3061 }
3062
3063 xassert (STRINGP (it->string)
3064 || (it->stop_charpos >= BEGV
3065 && it->stop_charpos >= IT_CHARPOS (*it)));
3066 }
3067
3068
3069 /* Return the position of the next overlay change after POS in
3070 current_buffer. Value is point-max if no overlay change
3071 follows. This is like `next-overlay-change' but doesn't use
3072 xmalloc. */
3073
3074 static EMACS_INT
3075 next_overlay_change (EMACS_INT pos)
3076 {
3077 int noverlays;
3078 EMACS_INT endpos;
3079 Lisp_Object *overlays;
3080 int i;
3081
3082 /* Get all overlays at the given position. */
3083 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3084
3085 /* If any of these overlays ends before endpos,
3086 use its ending point instead. */
3087 for (i = 0; i < noverlays; ++i)
3088 {
3089 Lisp_Object oend;
3090 EMACS_INT oendpos;
3091
3092 oend = OVERLAY_END (overlays[i]);
3093 oendpos = OVERLAY_POSITION (oend);
3094 endpos = min (endpos, oendpos);
3095 }
3096
3097 return endpos;
3098 }
3099
3100 /* Return the character position of a display string at or after
3101 position specified by POSITION. If no display string exists at or
3102 after POSITION, return ZV. A display string is either an overlay
3103 with `display' property whose value is a string, or a `display'
3104 text property whose value is a string. STRING is data about the
3105 string to iterate; if STRING->lstring is nil, we are iterating a
3106 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3107 on a GUI frame. */
3108 EMACS_INT
3109 compute_display_string_pos (struct text_pos *position,
3110 struct bidi_string_data *string, int frame_window_p)
3111 {
3112 /* OBJECT = nil means current buffer. */
3113 Lisp_Object object =
3114 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3115 Lisp_Object pos, spec;
3116 int string_p = (string && (STRINGP (string->lstring) || string->s));
3117 EMACS_INT eob = string_p ? string->schars : ZV;
3118 EMACS_INT begb = string_p ? 0 : BEGV;
3119 EMACS_INT bufpos, charpos = CHARPOS (*position);
3120 struct text_pos tpos;
3121
3122 if (charpos >= eob
3123 /* We don't support display properties whose values are strings
3124 that have display string properties. */
3125 || string->from_disp_str
3126 /* C strings cannot have display properties. */
3127 || (string->s && !STRINGP (object)))
3128 return eob;
3129
3130 /* If the character at CHARPOS is where the display string begins,
3131 return CHARPOS. */
3132 pos = make_number (charpos);
3133 if (STRINGP (object))
3134 bufpos = string->bufpos;
3135 else
3136 bufpos = charpos;
3137 tpos = *position;
3138 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3139 && (charpos <= begb
3140 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3141 object),
3142 spec))
3143 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3144 frame_window_p))
3145 return charpos;
3146
3147 /* Look forward for the first character with a `display' property
3148 that will replace the underlying text when displayed. */
3149 do {
3150 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3151 CHARPOS (tpos) = XFASTINT (pos);
3152 if (STRINGP (object))
3153 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3154 else
3155 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3156 if (CHARPOS (tpos) >= eob)
3157 break;
3158 spec = Fget_char_property (pos, Qdisplay, object);
3159 if (!STRINGP (object))
3160 bufpos = CHARPOS (tpos);
3161 } while (NILP (spec)
3162 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3163 frame_window_p));
3164
3165 return CHARPOS (tpos);
3166 }
3167
3168 /* Return the character position of the end of the display string that
3169 started at CHARPOS. A display string is either an overlay with
3170 `display' property whose value is a string or a `display' text
3171 property whose value is a string. */
3172 EMACS_INT
3173 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3174 {
3175 /* OBJECT = nil means current buffer. */
3176 Lisp_Object object =
3177 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3178 Lisp_Object pos = make_number (charpos);
3179 EMACS_INT eob =
3180 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3181
3182 if (charpos >= eob || (string->s && !STRINGP (object)))
3183 return eob;
3184
3185 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3186 abort ();
3187
3188 /* Look forward for the first character where the `display' property
3189 changes. */
3190 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3191
3192 return XFASTINT (pos);
3193 }
3194
3195
3196 \f
3197 /***********************************************************************
3198 Fontification
3199 ***********************************************************************/
3200
3201 /* Handle changes in the `fontified' property of the current buffer by
3202 calling hook functions from Qfontification_functions to fontify
3203 regions of text. */
3204
3205 static enum prop_handled
3206 handle_fontified_prop (struct it *it)
3207 {
3208 Lisp_Object prop, pos;
3209 enum prop_handled handled = HANDLED_NORMALLY;
3210
3211 if (!NILP (Vmemory_full))
3212 return handled;
3213
3214 /* Get the value of the `fontified' property at IT's current buffer
3215 position. (The `fontified' property doesn't have a special
3216 meaning in strings.) If the value is nil, call functions from
3217 Qfontification_functions. */
3218 if (!STRINGP (it->string)
3219 && it->s == NULL
3220 && !NILP (Vfontification_functions)
3221 && !NILP (Vrun_hooks)
3222 && (pos = make_number (IT_CHARPOS (*it)),
3223 prop = Fget_char_property (pos, Qfontified, Qnil),
3224 /* Ignore the special cased nil value always present at EOB since
3225 no amount of fontifying will be able to change it. */
3226 NILP (prop) && IT_CHARPOS (*it) < Z))
3227 {
3228 int count = SPECPDL_INDEX ();
3229 Lisp_Object val;
3230 struct buffer *obuf = current_buffer;
3231 int begv = BEGV, zv = ZV;
3232 int old_clip_changed = current_buffer->clip_changed;
3233
3234 val = Vfontification_functions;
3235 specbind (Qfontification_functions, Qnil);
3236
3237 xassert (it->end_charpos == ZV);
3238
3239 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3240 safe_call1 (val, pos);
3241 else
3242 {
3243 Lisp_Object fns, fn;
3244 struct gcpro gcpro1, gcpro2;
3245
3246 fns = Qnil;
3247 GCPRO2 (val, fns);
3248
3249 for (; CONSP (val); val = XCDR (val))
3250 {
3251 fn = XCAR (val);
3252
3253 if (EQ (fn, Qt))
3254 {
3255 /* A value of t indicates this hook has a local
3256 binding; it means to run the global binding too.
3257 In a global value, t should not occur. If it
3258 does, we must ignore it to avoid an endless
3259 loop. */
3260 for (fns = Fdefault_value (Qfontification_functions);
3261 CONSP (fns);
3262 fns = XCDR (fns))
3263 {
3264 fn = XCAR (fns);
3265 if (!EQ (fn, Qt))
3266 safe_call1 (fn, pos);
3267 }
3268 }
3269 else
3270 safe_call1 (fn, pos);
3271 }
3272
3273 UNGCPRO;
3274 }
3275
3276 unbind_to (count, Qnil);
3277
3278 /* Fontification functions routinely call `save-restriction'.
3279 Normally, this tags clip_changed, which can confuse redisplay
3280 (see discussion in Bug#6671). Since we don't perform any
3281 special handling of fontification changes in the case where
3282 `save-restriction' isn't called, there's no point doing so in
3283 this case either. So, if the buffer's restrictions are
3284 actually left unchanged, reset clip_changed. */
3285 if (obuf == current_buffer)
3286 {
3287 if (begv == BEGV && zv == ZV)
3288 current_buffer->clip_changed = old_clip_changed;
3289 }
3290 /* There isn't much we can reasonably do to protect against
3291 misbehaving fontification, but here's a fig leaf. */
3292 else if (!NILP (BVAR (obuf, name)))
3293 set_buffer_internal_1 (obuf);
3294
3295 /* The fontification code may have added/removed text.
3296 It could do even a lot worse, but let's at least protect against
3297 the most obvious case where only the text past `pos' gets changed',
3298 as is/was done in grep.el where some escapes sequences are turned
3299 into face properties (bug#7876). */
3300 it->end_charpos = ZV;
3301
3302 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3303 something. This avoids an endless loop if they failed to
3304 fontify the text for which reason ever. */
3305 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3306 handled = HANDLED_RECOMPUTE_PROPS;
3307 }
3308
3309 return handled;
3310 }
3311
3312
3313 \f
3314 /***********************************************************************
3315 Faces
3316 ***********************************************************************/
3317
3318 /* Set up iterator IT from face properties at its current position.
3319 Called from handle_stop. */
3320
3321 static enum prop_handled
3322 handle_face_prop (struct it *it)
3323 {
3324 int new_face_id;
3325 EMACS_INT next_stop;
3326
3327 if (!STRINGP (it->string))
3328 {
3329 new_face_id
3330 = face_at_buffer_position (it->w,
3331 IT_CHARPOS (*it),
3332 it->region_beg_charpos,
3333 it->region_end_charpos,
3334 &next_stop,
3335 (IT_CHARPOS (*it)
3336 + TEXT_PROP_DISTANCE_LIMIT),
3337 0, it->base_face_id);
3338
3339 /* Is this a start of a run of characters with box face?
3340 Caveat: this can be called for a freshly initialized
3341 iterator; face_id is -1 in this case. We know that the new
3342 face will not change until limit, i.e. if the new face has a
3343 box, all characters up to limit will have one. But, as
3344 usual, we don't know whether limit is really the end. */
3345 if (new_face_id != it->face_id)
3346 {
3347 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3348
3349 /* If new face has a box but old face has not, this is
3350 the start of a run of characters with box, i.e. it has
3351 a shadow on the left side. The value of face_id of the
3352 iterator will be -1 if this is the initial call that gets
3353 the face. In this case, we have to look in front of IT's
3354 position and see whether there is a face != new_face_id. */
3355 it->start_of_box_run_p
3356 = (new_face->box != FACE_NO_BOX
3357 && (it->face_id >= 0
3358 || IT_CHARPOS (*it) == BEG
3359 || new_face_id != face_before_it_pos (it)));
3360 it->face_box_p = new_face->box != FACE_NO_BOX;
3361 }
3362 }
3363 else
3364 {
3365 int base_face_id;
3366 EMACS_INT bufpos;
3367 int i;
3368 Lisp_Object from_overlay
3369 = (it->current.overlay_string_index >= 0
3370 ? it->string_overlays[it->current.overlay_string_index]
3371 : Qnil);
3372
3373 /* See if we got to this string directly or indirectly from
3374 an overlay property. That includes the before-string or
3375 after-string of an overlay, strings in display properties
3376 provided by an overlay, their text properties, etc.
3377
3378 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3379 if (! NILP (from_overlay))
3380 for (i = it->sp - 1; i >= 0; i--)
3381 {
3382 if (it->stack[i].current.overlay_string_index >= 0)
3383 from_overlay
3384 = it->string_overlays[it->stack[i].current.overlay_string_index];
3385 else if (! NILP (it->stack[i].from_overlay))
3386 from_overlay = it->stack[i].from_overlay;
3387
3388 if (!NILP (from_overlay))
3389 break;
3390 }
3391
3392 if (! NILP (from_overlay))
3393 {
3394 bufpos = IT_CHARPOS (*it);
3395 /* For a string from an overlay, the base face depends
3396 only on text properties and ignores overlays. */
3397 base_face_id
3398 = face_for_overlay_string (it->w,
3399 IT_CHARPOS (*it),
3400 it->region_beg_charpos,
3401 it->region_end_charpos,
3402 &next_stop,
3403 (IT_CHARPOS (*it)
3404 + TEXT_PROP_DISTANCE_LIMIT),
3405 0,
3406 from_overlay);
3407 }
3408 else
3409 {
3410 bufpos = 0;
3411
3412 /* For strings from a `display' property, use the face at
3413 IT's current buffer position as the base face to merge
3414 with, so that overlay strings appear in the same face as
3415 surrounding text, unless they specify their own
3416 faces. */
3417 base_face_id = underlying_face_id (it);
3418 }
3419
3420 new_face_id = face_at_string_position (it->w,
3421 it->string,
3422 IT_STRING_CHARPOS (*it),
3423 bufpos,
3424 it->region_beg_charpos,
3425 it->region_end_charpos,
3426 &next_stop,
3427 base_face_id, 0);
3428
3429 /* Is this a start of a run of characters with box? Caveat:
3430 this can be called for a freshly allocated iterator; face_id
3431 is -1 is this case. We know that the new face will not
3432 change until the next check pos, i.e. if the new face has a
3433 box, all characters up to that position will have a
3434 box. But, as usual, we don't know whether that position
3435 is really the end. */
3436 if (new_face_id != it->face_id)
3437 {
3438 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3439 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3440
3441 /* If new face has a box but old face hasn't, this is the
3442 start of a run of characters with box, i.e. it has a
3443 shadow on the left side. */
3444 it->start_of_box_run_p
3445 = new_face->box && (old_face == NULL || !old_face->box);
3446 it->face_box_p = new_face->box != FACE_NO_BOX;
3447 }
3448 }
3449
3450 it->face_id = new_face_id;
3451 return HANDLED_NORMALLY;
3452 }
3453
3454
3455 /* Return the ID of the face ``underlying'' IT's current position,
3456 which is in a string. If the iterator is associated with a
3457 buffer, return the face at IT's current buffer position.
3458 Otherwise, use the iterator's base_face_id. */
3459
3460 static int
3461 underlying_face_id (struct it *it)
3462 {
3463 int face_id = it->base_face_id, i;
3464
3465 xassert (STRINGP (it->string));
3466
3467 for (i = it->sp - 1; i >= 0; --i)
3468 if (NILP (it->stack[i].string))
3469 face_id = it->stack[i].face_id;
3470
3471 return face_id;
3472 }
3473
3474
3475 /* Compute the face one character before or after the current position
3476 of IT, in the visual order. BEFORE_P non-zero means get the face
3477 in front (to the left in L2R paragraphs, to the right in R2L
3478 paragraphs) of IT's screen position. Value is the ID of the face. */
3479
3480 static int
3481 face_before_or_after_it_pos (struct it *it, int before_p)
3482 {
3483 int face_id, limit;
3484 EMACS_INT next_check_charpos;
3485 struct it it_copy;
3486
3487 xassert (it->s == NULL);
3488
3489 if (STRINGP (it->string))
3490 {
3491 EMACS_INT bufpos, charpos;
3492 int base_face_id;
3493
3494 /* No face change past the end of the string (for the case
3495 we are padding with spaces). No face change before the
3496 string start. */
3497 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3498 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3499 return it->face_id;
3500
3501 if (!it->bidi_p)
3502 {
3503 /* Set charpos to the position before or after IT's current
3504 position, in the logical order, which in the non-bidi
3505 case is the same as the visual order. */
3506 if (before_p)
3507 charpos = IT_STRING_CHARPOS (*it) - 1;
3508 else if (it->what == IT_COMPOSITION)
3509 /* For composition, we must check the character after the
3510 composition. */
3511 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3512 else
3513 charpos = IT_STRING_CHARPOS (*it) + 1;
3514 }
3515 else
3516 {
3517 if (before_p)
3518 {
3519 /* With bidi iteration, the character before the current
3520 in the visual order cannot be found by simple
3521 iteration, because "reverse" reordering is not
3522 supported. Instead, we need to use the move_it_*
3523 family of functions. */
3524 /* Ignore face changes before the first visible
3525 character on this display line. */
3526 if (it->current_x <= it->first_visible_x)
3527 return it->face_id;
3528 it_copy = *it;
3529 /* Implementation note: Since move_it_in_display_line
3530 works in the iterator geometry, and thinks the first
3531 character is always the leftmost, even in R2L lines,
3532 we don't need to distinguish between the R2L and L2R
3533 cases here. */
3534 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3535 it_copy.current_x - 1, MOVE_TO_X);
3536 charpos = IT_STRING_CHARPOS (it_copy);
3537 }
3538 else
3539 {
3540 /* Set charpos to the string position of the character
3541 that comes after IT's current position in the visual
3542 order. */
3543 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3544
3545 it_copy = *it;
3546 while (n--)
3547 bidi_move_to_visually_next (&it_copy.bidi_it);
3548
3549 charpos = it_copy.bidi_it.charpos;
3550 }
3551 }
3552 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3553
3554 if (it->current.overlay_string_index >= 0)
3555 bufpos = IT_CHARPOS (*it);
3556 else
3557 bufpos = 0;
3558
3559 base_face_id = underlying_face_id (it);
3560
3561 /* Get the face for ASCII, or unibyte. */
3562 face_id = face_at_string_position (it->w,
3563 it->string,
3564 charpos,
3565 bufpos,
3566 it->region_beg_charpos,
3567 it->region_end_charpos,
3568 &next_check_charpos,
3569 base_face_id, 0);
3570
3571 /* Correct the face for charsets different from ASCII. Do it
3572 for the multibyte case only. The face returned above is
3573 suitable for unibyte text if IT->string is unibyte. */
3574 if (STRING_MULTIBYTE (it->string))
3575 {
3576 struct text_pos pos1 = string_pos (charpos, it->string);
3577 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3578 int c, len;
3579 struct face *face = FACE_FROM_ID (it->f, face_id);
3580
3581 c = string_char_and_length (p, &len);
3582 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3583 }
3584 }
3585 else
3586 {
3587 struct text_pos pos;
3588
3589 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3590 || (IT_CHARPOS (*it) <= BEGV && before_p))
3591 return it->face_id;
3592
3593 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3594 pos = it->current.pos;
3595
3596 if (!it->bidi_p)
3597 {
3598 if (before_p)
3599 DEC_TEXT_POS (pos, it->multibyte_p);
3600 else
3601 {
3602 if (it->what == IT_COMPOSITION)
3603 {
3604 /* For composition, we must check the position after
3605 the composition. */
3606 pos.charpos += it->cmp_it.nchars;
3607 pos.bytepos += it->len;
3608 }
3609 else
3610 INC_TEXT_POS (pos, it->multibyte_p);
3611 }
3612 }
3613 else
3614 {
3615 if (before_p)
3616 {
3617 /* With bidi iteration, the character before the current
3618 in the visual order cannot be found by simple
3619 iteration, because "reverse" reordering is not
3620 supported. Instead, we need to use the move_it_*
3621 family of functions. */
3622 /* Ignore face changes before the first visible
3623 character on this display line. */
3624 if (it->current_x <= it->first_visible_x)
3625 return it->face_id;
3626 it_copy = *it;
3627 /* Implementation note: Since move_it_in_display_line
3628 works in the iterator geometry, and thinks the first
3629 character is always the leftmost, even in R2L lines,
3630 we don't need to distinguish between the R2L and L2R
3631 cases here. */
3632 move_it_in_display_line (&it_copy, ZV,
3633 it_copy.current_x - 1, MOVE_TO_X);
3634 pos = it_copy.current.pos;
3635 }
3636 else
3637 {
3638 /* Set charpos to the buffer position of the character
3639 that comes after IT's current position in the visual
3640 order. */
3641 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3642
3643 it_copy = *it;
3644 while (n--)
3645 bidi_move_to_visually_next (&it_copy.bidi_it);
3646
3647 SET_TEXT_POS (pos,
3648 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3649 }
3650 }
3651 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3652
3653 /* Determine face for CHARSET_ASCII, or unibyte. */
3654 face_id = face_at_buffer_position (it->w,
3655 CHARPOS (pos),
3656 it->region_beg_charpos,
3657 it->region_end_charpos,
3658 &next_check_charpos,
3659 limit, 0, -1);
3660
3661 /* Correct the face for charsets different from ASCII. Do it
3662 for the multibyte case only. The face returned above is
3663 suitable for unibyte text if current_buffer is unibyte. */
3664 if (it->multibyte_p)
3665 {
3666 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3667 struct face *face = FACE_FROM_ID (it->f, face_id);
3668 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3669 }
3670 }
3671
3672 return face_id;
3673 }
3674
3675
3676 \f
3677 /***********************************************************************
3678 Invisible text
3679 ***********************************************************************/
3680
3681 /* Set up iterator IT from invisible properties at its current
3682 position. Called from handle_stop. */
3683
3684 static enum prop_handled
3685 handle_invisible_prop (struct it *it)
3686 {
3687 enum prop_handled handled = HANDLED_NORMALLY;
3688
3689 if (STRINGP (it->string))
3690 {
3691 Lisp_Object prop, end_charpos, limit, charpos;
3692
3693 /* Get the value of the invisible text property at the
3694 current position. Value will be nil if there is no such
3695 property. */
3696 charpos = make_number (IT_STRING_CHARPOS (*it));
3697 prop = Fget_text_property (charpos, Qinvisible, it->string);
3698
3699 if (!NILP (prop)
3700 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3701 {
3702 EMACS_INT endpos;
3703
3704 handled = HANDLED_RECOMPUTE_PROPS;
3705
3706 /* Get the position at which the next change of the
3707 invisible text property can be found in IT->string.
3708 Value will be nil if the property value is the same for
3709 all the rest of IT->string. */
3710 XSETINT (limit, SCHARS (it->string));
3711 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3712 it->string, limit);
3713
3714 /* Text at current position is invisible. The next
3715 change in the property is at position end_charpos.
3716 Move IT's current position to that position. */
3717 if (INTEGERP (end_charpos)
3718 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3719 {
3720 struct text_pos old;
3721 EMACS_INT oldpos;
3722
3723 old = it->current.string_pos;
3724 oldpos = CHARPOS (old);
3725 if (it->bidi_p)
3726 {
3727 if (it->bidi_it.first_elt
3728 && it->bidi_it.charpos < SCHARS (it->string))
3729 bidi_paragraph_init (it->paragraph_embedding,
3730 &it->bidi_it, 1);
3731 /* Bidi-iterate out of the invisible text. */
3732 do
3733 {
3734 bidi_move_to_visually_next (&it->bidi_it);
3735 }
3736 while (oldpos <= it->bidi_it.charpos
3737 && it->bidi_it.charpos < endpos);
3738
3739 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3740 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3741 if (IT_CHARPOS (*it) >= endpos)
3742 it->prev_stop = endpos;
3743 }
3744 else
3745 {
3746 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3747 compute_string_pos (&it->current.string_pos, old, it->string);
3748 }
3749 }
3750 else
3751 {
3752 /* The rest of the string is invisible. If this is an
3753 overlay string, proceed with the next overlay string
3754 or whatever comes and return a character from there. */
3755 if (it->current.overlay_string_index >= 0)
3756 {
3757 next_overlay_string (it);
3758 /* Don't check for overlay strings when we just
3759 finished processing them. */
3760 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3761 }
3762 else
3763 {
3764 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3765 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3766 }
3767 }
3768 }
3769 }
3770 else
3771 {
3772 int invis_p;
3773 EMACS_INT newpos, next_stop, start_charpos, tem;
3774 Lisp_Object pos, prop, overlay;
3775
3776 /* First of all, is there invisible text at this position? */
3777 tem = start_charpos = IT_CHARPOS (*it);
3778 pos = make_number (tem);
3779 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3780 &overlay);
3781 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3782
3783 /* If we are on invisible text, skip over it. */
3784 if (invis_p && start_charpos < it->end_charpos)
3785 {
3786 /* Record whether we have to display an ellipsis for the
3787 invisible text. */
3788 int display_ellipsis_p = invis_p == 2;
3789
3790 handled = HANDLED_RECOMPUTE_PROPS;
3791
3792 /* Loop skipping over invisible text. The loop is left at
3793 ZV or with IT on the first char being visible again. */
3794 do
3795 {
3796 /* Try to skip some invisible text. Return value is the
3797 position reached which can be equal to where we start
3798 if there is nothing invisible there. This skips both
3799 over invisible text properties and overlays with
3800 invisible property. */
3801 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3802
3803 /* If we skipped nothing at all we weren't at invisible
3804 text in the first place. If everything to the end of
3805 the buffer was skipped, end the loop. */
3806 if (newpos == tem || newpos >= ZV)
3807 invis_p = 0;
3808 else
3809 {
3810 /* We skipped some characters but not necessarily
3811 all there are. Check if we ended up on visible
3812 text. Fget_char_property returns the property of
3813 the char before the given position, i.e. if we
3814 get invis_p = 0, this means that the char at
3815 newpos is visible. */
3816 pos = make_number (newpos);
3817 prop = Fget_char_property (pos, Qinvisible, it->window);
3818 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3819 }
3820
3821 /* If we ended up on invisible text, proceed to
3822 skip starting with next_stop. */
3823 if (invis_p)
3824 tem = next_stop;
3825
3826 /* If there are adjacent invisible texts, don't lose the
3827 second one's ellipsis. */
3828 if (invis_p == 2)
3829 display_ellipsis_p = 1;
3830 }
3831 while (invis_p);
3832
3833 /* The position newpos is now either ZV or on visible text. */
3834 if (it->bidi_p && newpos < ZV)
3835 {
3836 /* With bidi iteration, the region of invisible text
3837 could start and/or end in the middle of a non-base
3838 embedding level. Therefore, we need to skip
3839 invisible text using the bidi iterator, starting at
3840 IT's current position, until we find ourselves
3841 outside the invisible text. Skipping invisible text
3842 _after_ bidi iteration avoids affecting the visual
3843 order of the displayed text when invisible properties
3844 are added or removed. */
3845 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3846 {
3847 /* If we were `reseat'ed to a new paragraph,
3848 determine the paragraph base direction. We need
3849 to do it now because next_element_from_buffer may
3850 not have a chance to do it, if we are going to
3851 skip any text at the beginning, which resets the
3852 FIRST_ELT flag. */
3853 bidi_paragraph_init (it->paragraph_embedding,
3854 &it->bidi_it, 1);
3855 }
3856 do
3857 {
3858 bidi_move_to_visually_next (&it->bidi_it);
3859 }
3860 while (it->stop_charpos <= it->bidi_it.charpos
3861 && it->bidi_it.charpos < newpos);
3862 IT_CHARPOS (*it) = it->bidi_it.charpos;
3863 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3864 /* If we overstepped NEWPOS, record its position in the
3865 iterator, so that we skip invisible text if later the
3866 bidi iteration lands us in the invisible region
3867 again. */
3868 if (IT_CHARPOS (*it) >= newpos)
3869 it->prev_stop = newpos;
3870 }
3871 else
3872 {
3873 IT_CHARPOS (*it) = newpos;
3874 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3875 }
3876
3877 /* If there are before-strings at the start of invisible
3878 text, and the text is invisible because of a text
3879 property, arrange to show before-strings because 20.x did
3880 it that way. (If the text is invisible because of an
3881 overlay property instead of a text property, this is
3882 already handled in the overlay code.) */
3883 if (NILP (overlay)
3884 && get_overlay_strings (it, it->stop_charpos))
3885 {
3886 handled = HANDLED_RECOMPUTE_PROPS;
3887 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3888 }
3889 else if (display_ellipsis_p)
3890 {
3891 /* Make sure that the glyphs of the ellipsis will get
3892 correct `charpos' values. If we would not update
3893 it->position here, the glyphs would belong to the
3894 last visible character _before_ the invisible
3895 text, which confuses `set_cursor_from_row'.
3896
3897 We use the last invisible position instead of the
3898 first because this way the cursor is always drawn on
3899 the first "." of the ellipsis, whenever PT is inside
3900 the invisible text. Otherwise the cursor would be
3901 placed _after_ the ellipsis when the point is after the
3902 first invisible character. */
3903 if (!STRINGP (it->object))
3904 {
3905 it->position.charpos = newpos - 1;
3906 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3907 }
3908 it->ellipsis_p = 1;
3909 /* Let the ellipsis display before
3910 considering any properties of the following char.
3911 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3912 handled = HANDLED_RETURN;
3913 }
3914 }
3915 }
3916
3917 return handled;
3918 }
3919
3920
3921 /* Make iterator IT return `...' next.
3922 Replaces LEN characters from buffer. */
3923
3924 static void
3925 setup_for_ellipsis (struct it *it, int len)
3926 {
3927 /* Use the display table definition for `...'. Invalid glyphs
3928 will be handled by the method returning elements from dpvec. */
3929 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3930 {
3931 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3932 it->dpvec = v->contents;
3933 it->dpend = v->contents + v->header.size;
3934 }
3935 else
3936 {
3937 /* Default `...'. */
3938 it->dpvec = default_invis_vector;
3939 it->dpend = default_invis_vector + 3;
3940 }
3941
3942 it->dpvec_char_len = len;
3943 it->current.dpvec_index = 0;
3944 it->dpvec_face_id = -1;
3945
3946 /* Remember the current face id in case glyphs specify faces.
3947 IT's face is restored in set_iterator_to_next.
3948 saved_face_id was set to preceding char's face in handle_stop. */
3949 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3950 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3951
3952 it->method = GET_FROM_DISPLAY_VECTOR;
3953 it->ellipsis_p = 1;
3954 }
3955
3956
3957 \f
3958 /***********************************************************************
3959 'display' property
3960 ***********************************************************************/
3961
3962 /* Set up iterator IT from `display' property at its current position.
3963 Called from handle_stop.
3964 We return HANDLED_RETURN if some part of the display property
3965 overrides the display of the buffer text itself.
3966 Otherwise we return HANDLED_NORMALLY. */
3967
3968 static enum prop_handled
3969 handle_display_prop (struct it *it)
3970 {
3971 Lisp_Object propval, object, overlay;
3972 struct text_pos *position;
3973 EMACS_INT bufpos;
3974 /* Nonzero if some property replaces the display of the text itself. */
3975 int display_replaced_p = 0;
3976
3977 if (STRINGP (it->string))
3978 {
3979 object = it->string;
3980 position = &it->current.string_pos;
3981 bufpos = CHARPOS (it->current.pos);
3982 }
3983 else
3984 {
3985 XSETWINDOW (object, it->w);
3986 position = &it->current.pos;
3987 bufpos = CHARPOS (*position);
3988 }
3989
3990 /* Reset those iterator values set from display property values. */
3991 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3992 it->space_width = Qnil;
3993 it->font_height = Qnil;
3994 it->voffset = 0;
3995
3996 /* We don't support recursive `display' properties, i.e. string
3997 values that have a string `display' property, that have a string
3998 `display' property etc. */
3999 if (!it->string_from_display_prop_p)
4000 it->area = TEXT_AREA;
4001
4002 propval = get_char_property_and_overlay (make_number (position->charpos),
4003 Qdisplay, object, &overlay);
4004 if (NILP (propval))
4005 return HANDLED_NORMALLY;
4006 /* Now OVERLAY is the overlay that gave us this property, or nil
4007 if it was a text property. */
4008
4009 if (!STRINGP (it->string))
4010 object = it->w->buffer;
4011
4012 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4013 position, bufpos,
4014 FRAME_WINDOW_P (it->f));
4015
4016 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4017 }
4018
4019 /* Subroutine of handle_display_prop. Returns non-zero if the display
4020 specification in SPEC is a replacing specification, i.e. it would
4021 replace the text covered by `display' property with something else,
4022 such as an image or a display string.
4023
4024 See handle_single_display_spec for documentation of arguments.
4025 frame_window_p is non-zero if the window being redisplayed is on a
4026 GUI frame; this argument is used only if IT is NULL, see below.
4027
4028 IT can be NULL, if this is called by the bidi reordering code
4029 through compute_display_string_pos, which see. In that case, this
4030 function only examines SPEC, but does not otherwise "handle" it, in
4031 the sense that it doesn't set up members of IT from the display
4032 spec. */
4033 static int
4034 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4035 Lisp_Object overlay, struct text_pos *position,
4036 EMACS_INT bufpos, int frame_window_p)
4037 {
4038 int replacing_p = 0;
4039
4040 if (CONSP (spec)
4041 /* Simple specerties. */
4042 && !EQ (XCAR (spec), Qimage)
4043 && !EQ (XCAR (spec), Qspace)
4044 && !EQ (XCAR (spec), Qwhen)
4045 && !EQ (XCAR (spec), Qslice)
4046 && !EQ (XCAR (spec), Qspace_width)
4047 && !EQ (XCAR (spec), Qheight)
4048 && !EQ (XCAR (spec), Qraise)
4049 /* Marginal area specifications. */
4050 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4051 && !EQ (XCAR (spec), Qleft_fringe)
4052 && !EQ (XCAR (spec), Qright_fringe)
4053 && !NILP (XCAR (spec)))
4054 {
4055 for (; CONSP (spec); spec = XCDR (spec))
4056 {
4057 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
4058 position, bufpos, replacing_p,
4059 frame_window_p))
4060 {
4061 replacing_p = 1;
4062 /* If some text in a string is replaced, `position' no
4063 longer points to the position of `object'. */
4064 if (!it || STRINGP (object))
4065 break;
4066 }
4067 }
4068 }
4069 else if (VECTORP (spec))
4070 {
4071 int i;
4072 for (i = 0; i < ASIZE (spec); ++i)
4073 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
4074 position, bufpos, replacing_p,
4075 frame_window_p))
4076 {
4077 replacing_p = 1;
4078 /* If some text in a string is replaced, `position' no
4079 longer points to the position of `object'. */
4080 if (!it || STRINGP (object))
4081 break;
4082 }
4083 }
4084 else
4085 {
4086 if (handle_single_display_spec (it, spec, object, overlay,
4087 position, bufpos, 0, frame_window_p))
4088 replacing_p = 1;
4089 }
4090
4091 return replacing_p;
4092 }
4093
4094 /* Value is the position of the end of the `display' property starting
4095 at START_POS in OBJECT. */
4096
4097 static struct text_pos
4098 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4099 {
4100 Lisp_Object end;
4101 struct text_pos end_pos;
4102
4103 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4104 Qdisplay, object, Qnil);
4105 CHARPOS (end_pos) = XFASTINT (end);
4106 if (STRINGP (object))
4107 compute_string_pos (&end_pos, start_pos, it->string);
4108 else
4109 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4110
4111 return end_pos;
4112 }
4113
4114
4115 /* Set up IT from a single `display' property specification SPEC. OBJECT
4116 is the object in which the `display' property was found. *POSITION
4117 is the position in OBJECT at which the `display' property was found.
4118 BUFPOS is the buffer position of OBJECT (different from POSITION if
4119 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4120 previously saw a display specification which already replaced text
4121 display with something else, for example an image; we ignore such
4122 properties after the first one has been processed.
4123
4124 OVERLAY is the overlay this `display' property came from,
4125 or nil if it was a text property.
4126
4127 If SPEC is a `space' or `image' specification, and in some other
4128 cases too, set *POSITION to the position where the `display'
4129 property ends.
4130
4131 If IT is NULL, only examine the property specification in SPEC, but
4132 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4133 is intended to be displayed in a window on a GUI frame.
4134
4135 Value is non-zero if something was found which replaces the display
4136 of buffer or string text. */
4137
4138 static int
4139 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4140 Lisp_Object overlay, struct text_pos *position,
4141 EMACS_INT bufpos, int display_replaced_p,
4142 int frame_window_p)
4143 {
4144 Lisp_Object form;
4145 Lisp_Object location, value;
4146 struct text_pos start_pos = *position;
4147 int valid_p;
4148
4149 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4150 If the result is non-nil, use VALUE instead of SPEC. */
4151 form = Qt;
4152 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4153 {
4154 spec = XCDR (spec);
4155 if (!CONSP (spec))
4156 return 0;
4157 form = XCAR (spec);
4158 spec = XCDR (spec);
4159 }
4160
4161 if (!NILP (form) && !EQ (form, Qt))
4162 {
4163 int count = SPECPDL_INDEX ();
4164 struct gcpro gcpro1;
4165
4166 /* Bind `object' to the object having the `display' property, a
4167 buffer or string. Bind `position' to the position in the
4168 object where the property was found, and `buffer-position'
4169 to the current position in the buffer. */
4170
4171 if (NILP (object))
4172 XSETBUFFER (object, current_buffer);
4173 specbind (Qobject, object);
4174 specbind (Qposition, make_number (CHARPOS (*position)));
4175 specbind (Qbuffer_position, make_number (bufpos));
4176 GCPRO1 (form);
4177 form = safe_eval (form);
4178 UNGCPRO;
4179 unbind_to (count, Qnil);
4180 }
4181
4182 if (NILP (form))
4183 return 0;
4184
4185 /* Handle `(height HEIGHT)' specifications. */
4186 if (CONSP (spec)
4187 && EQ (XCAR (spec), Qheight)
4188 && CONSP (XCDR (spec)))
4189 {
4190 if (it)
4191 {
4192 if (!FRAME_WINDOW_P (it->f))
4193 return 0;
4194
4195 it->font_height = XCAR (XCDR (spec));
4196 if (!NILP (it->font_height))
4197 {
4198 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4199 int new_height = -1;
4200
4201 if (CONSP (it->font_height)
4202 && (EQ (XCAR (it->font_height), Qplus)
4203 || EQ (XCAR (it->font_height), Qminus))
4204 && CONSP (XCDR (it->font_height))
4205 && INTEGERP (XCAR (XCDR (it->font_height))))
4206 {
4207 /* `(+ N)' or `(- N)' where N is an integer. */
4208 int steps = XINT (XCAR (XCDR (it->font_height)));
4209 if (EQ (XCAR (it->font_height), Qplus))
4210 steps = - steps;
4211 it->face_id = smaller_face (it->f, it->face_id, steps);
4212 }
4213 else if (FUNCTIONP (it->font_height))
4214 {
4215 /* Call function with current height as argument.
4216 Value is the new height. */
4217 Lisp_Object height;
4218 height = safe_call1 (it->font_height,
4219 face->lface[LFACE_HEIGHT_INDEX]);
4220 if (NUMBERP (height))
4221 new_height = XFLOATINT (height);
4222 }
4223 else if (NUMBERP (it->font_height))
4224 {
4225 /* Value is a multiple of the canonical char height. */
4226 struct face *f;
4227
4228 f = FACE_FROM_ID (it->f,
4229 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4230 new_height = (XFLOATINT (it->font_height)
4231 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4232 }
4233 else
4234 {
4235 /* Evaluate IT->font_height with `height' bound to the
4236 current specified height to get the new height. */
4237 int count = SPECPDL_INDEX ();
4238
4239 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4240 value = safe_eval (it->font_height);
4241 unbind_to (count, Qnil);
4242
4243 if (NUMBERP (value))
4244 new_height = XFLOATINT (value);
4245 }
4246
4247 if (new_height > 0)
4248 it->face_id = face_with_height (it->f, it->face_id, new_height);
4249 }
4250 }
4251
4252 return 0;
4253 }
4254
4255 /* Handle `(space-width WIDTH)'. */
4256 if (CONSP (spec)
4257 && EQ (XCAR (spec), Qspace_width)
4258 && CONSP (XCDR (spec)))
4259 {
4260 if (it)
4261 {
4262 if (!FRAME_WINDOW_P (it->f))
4263 return 0;
4264
4265 value = XCAR (XCDR (spec));
4266 if (NUMBERP (value) && XFLOATINT (value) > 0)
4267 it->space_width = value;
4268 }
4269
4270 return 0;
4271 }
4272
4273 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4274 if (CONSP (spec)
4275 && EQ (XCAR (spec), Qslice))
4276 {
4277 Lisp_Object tem;
4278
4279 if (it)
4280 {
4281 if (!FRAME_WINDOW_P (it->f))
4282 return 0;
4283
4284 if (tem = XCDR (spec), CONSP (tem))
4285 {
4286 it->slice.x = XCAR (tem);
4287 if (tem = XCDR (tem), CONSP (tem))
4288 {
4289 it->slice.y = XCAR (tem);
4290 if (tem = XCDR (tem), CONSP (tem))
4291 {
4292 it->slice.width = XCAR (tem);
4293 if (tem = XCDR (tem), CONSP (tem))
4294 it->slice.height = XCAR (tem);
4295 }
4296 }
4297 }
4298 }
4299
4300 return 0;
4301 }
4302
4303 /* Handle `(raise FACTOR)'. */
4304 if (CONSP (spec)
4305 && EQ (XCAR (spec), Qraise)
4306 && CONSP (XCDR (spec)))
4307 {
4308 if (it)
4309 {
4310 if (!FRAME_WINDOW_P (it->f))
4311 return 0;
4312
4313 #ifdef HAVE_WINDOW_SYSTEM
4314 value = XCAR (XCDR (spec));
4315 if (NUMBERP (value))
4316 {
4317 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4318 it->voffset = - (XFLOATINT (value)
4319 * (FONT_HEIGHT (face->font)));
4320 }
4321 #endif /* HAVE_WINDOW_SYSTEM */
4322 }
4323
4324 return 0;
4325 }
4326
4327 /* Don't handle the other kinds of display specifications
4328 inside a string that we got from a `display' property. */
4329 if (it && it->string_from_display_prop_p)
4330 return 0;
4331
4332 /* Characters having this form of property are not displayed, so
4333 we have to find the end of the property. */
4334 if (it)
4335 {
4336 start_pos = *position;
4337 *position = display_prop_end (it, object, start_pos);
4338 }
4339 value = Qnil;
4340
4341 /* Stop the scan at that end position--we assume that all
4342 text properties change there. */
4343 if (it)
4344 it->stop_charpos = position->charpos;
4345
4346 /* Handle `(left-fringe BITMAP [FACE])'
4347 and `(right-fringe BITMAP [FACE])'. */
4348 if (CONSP (spec)
4349 && (EQ (XCAR (spec), Qleft_fringe)
4350 || EQ (XCAR (spec), Qright_fringe))
4351 && CONSP (XCDR (spec)))
4352 {
4353 int fringe_bitmap;
4354
4355 if (it)
4356 {
4357 if (!FRAME_WINDOW_P (it->f))
4358 /* If we return here, POSITION has been advanced
4359 across the text with this property. */
4360 return 0;
4361 }
4362 else if (!frame_window_p)
4363 return 0;
4364
4365 #ifdef HAVE_WINDOW_SYSTEM
4366 value = XCAR (XCDR (spec));
4367 if (!SYMBOLP (value)
4368 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4369 /* If we return here, POSITION has been advanced
4370 across the text with this property. */
4371 return 0;
4372
4373 if (it)
4374 {
4375 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4376
4377 if (CONSP (XCDR (XCDR (spec))))
4378 {
4379 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4380 int face_id2 = lookup_derived_face (it->f, face_name,
4381 FRINGE_FACE_ID, 0);
4382 if (face_id2 >= 0)
4383 face_id = face_id2;
4384 }
4385
4386 /* Save current settings of IT so that we can restore them
4387 when we are finished with the glyph property value. */
4388 push_it (it, position);
4389
4390 it->area = TEXT_AREA;
4391 it->what = IT_IMAGE;
4392 it->image_id = -1; /* no image */
4393 it->position = start_pos;
4394 it->object = NILP (object) ? it->w->buffer : object;
4395 it->method = GET_FROM_IMAGE;
4396 it->from_overlay = Qnil;
4397 it->face_id = face_id;
4398 it->from_disp_prop_p = 1;
4399
4400 /* Say that we haven't consumed the characters with
4401 `display' property yet. The call to pop_it in
4402 set_iterator_to_next will clean this up. */
4403 *position = start_pos;
4404
4405 if (EQ (XCAR (spec), Qleft_fringe))
4406 {
4407 it->left_user_fringe_bitmap = fringe_bitmap;
4408 it->left_user_fringe_face_id = face_id;
4409 }
4410 else
4411 {
4412 it->right_user_fringe_bitmap = fringe_bitmap;
4413 it->right_user_fringe_face_id = face_id;
4414 }
4415 }
4416 #endif /* HAVE_WINDOW_SYSTEM */
4417 return 1;
4418 }
4419
4420 /* Prepare to handle `((margin left-margin) ...)',
4421 `((margin right-margin) ...)' and `((margin nil) ...)'
4422 prefixes for display specifications. */
4423 location = Qunbound;
4424 if (CONSP (spec) && CONSP (XCAR (spec)))
4425 {
4426 Lisp_Object tem;
4427
4428 value = XCDR (spec);
4429 if (CONSP (value))
4430 value = XCAR (value);
4431
4432 tem = XCAR (spec);
4433 if (EQ (XCAR (tem), Qmargin)
4434 && (tem = XCDR (tem),
4435 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4436 (NILP (tem)
4437 || EQ (tem, Qleft_margin)
4438 || EQ (tem, Qright_margin))))
4439 location = tem;
4440 }
4441
4442 if (EQ (location, Qunbound))
4443 {
4444 location = Qnil;
4445 value = spec;
4446 }
4447
4448 /* After this point, VALUE is the property after any
4449 margin prefix has been stripped. It must be a string,
4450 an image specification, or `(space ...)'.
4451
4452 LOCATION specifies where to display: `left-margin',
4453 `right-margin' or nil. */
4454
4455 valid_p = (STRINGP (value)
4456 #ifdef HAVE_WINDOW_SYSTEM
4457 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4458 && valid_image_p (value))
4459 #endif /* not HAVE_WINDOW_SYSTEM */
4460 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4461
4462 if (valid_p && !display_replaced_p)
4463 {
4464 if (!it)
4465 return 1;
4466
4467 /* Save current settings of IT so that we can restore them
4468 when we are finished with the glyph property value. */
4469 push_it (it, position);
4470 it->from_overlay = overlay;
4471 it->from_disp_prop_p = 1;
4472
4473 if (NILP (location))
4474 it->area = TEXT_AREA;
4475 else if (EQ (location, Qleft_margin))
4476 it->area = LEFT_MARGIN_AREA;
4477 else
4478 it->area = RIGHT_MARGIN_AREA;
4479
4480 if (STRINGP (value))
4481 {
4482 it->string = value;
4483 it->multibyte_p = STRING_MULTIBYTE (it->string);
4484 it->current.overlay_string_index = -1;
4485 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4486 it->end_charpos = it->string_nchars = SCHARS (it->string);
4487 it->method = GET_FROM_STRING;
4488 it->stop_charpos = 0;
4489 it->prev_stop = 0;
4490 it->base_level_stop = 0;
4491 it->string_from_display_prop_p = 1;
4492 /* Say that we haven't consumed the characters with
4493 `display' property yet. The call to pop_it in
4494 set_iterator_to_next will clean this up. */
4495 if (BUFFERP (object))
4496 *position = start_pos;
4497
4498 /* Force paragraph direction to be that of the parent
4499 object. If the parent object's paragraph direction is
4500 not yet determined, default to L2R. */
4501 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4502 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4503 else
4504 it->paragraph_embedding = L2R;
4505
4506 /* Set up the bidi iterator for this display string. */
4507 if (it->bidi_p)
4508 {
4509 it->bidi_it.string.lstring = it->string;
4510 it->bidi_it.string.s = NULL;
4511 it->bidi_it.string.schars = it->end_charpos;
4512 it->bidi_it.string.bufpos = bufpos;
4513 it->bidi_it.string.from_disp_str = 1;
4514 it->bidi_it.string.unibyte = !it->multibyte_p;
4515 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4516 }
4517 }
4518 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4519 {
4520 it->method = GET_FROM_STRETCH;
4521 it->object = value;
4522 *position = it->position = start_pos;
4523 }
4524 #ifdef HAVE_WINDOW_SYSTEM
4525 else
4526 {
4527 it->what = IT_IMAGE;
4528 it->image_id = lookup_image (it->f, value);
4529 it->position = start_pos;
4530 it->object = NILP (object) ? it->w->buffer : object;
4531 it->method = GET_FROM_IMAGE;
4532
4533 /* Say that we haven't consumed the characters with
4534 `display' property yet. The call to pop_it in
4535 set_iterator_to_next will clean this up. */
4536 *position = start_pos;
4537 }
4538 #endif /* HAVE_WINDOW_SYSTEM */
4539
4540 return 1;
4541 }
4542
4543 /* Invalid property or property not supported. Restore
4544 POSITION to what it was before. */
4545 *position = start_pos;
4546 return 0;
4547 }
4548
4549 /* Check if PROP is a display property value whose text should be
4550 treated as intangible. OVERLAY is the overlay from which PROP
4551 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4552 specify the buffer position covered by PROP. */
4553
4554 int
4555 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4556 EMACS_INT charpos, EMACS_INT bytepos)
4557 {
4558 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4559 struct text_pos position;
4560
4561 SET_TEXT_POS (position, charpos, bytepos);
4562 return handle_display_spec (NULL, prop, Qnil, overlay,
4563 &position, charpos, frame_window_p);
4564 }
4565
4566
4567 /* Return 1 if PROP is a display sub-property value containing STRING.
4568
4569 Implementation note: this and the following function are really
4570 special cases of handle_display_spec and
4571 handle_single_display_spec, and should ideally use the same code.
4572 Until they do, these two pairs must be consistent and must be
4573 modified in sync. */
4574
4575 static int
4576 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4577 {
4578 if (EQ (string, prop))
4579 return 1;
4580
4581 /* Skip over `when FORM'. */
4582 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4583 {
4584 prop = XCDR (prop);
4585 if (!CONSP (prop))
4586 return 0;
4587 /* Actually, the condition following `when' should be eval'ed,
4588 like handle_single_display_spec does, and we should return
4589 zero if it evaluates to nil. However, this function is
4590 called only when the buffer was already displayed and some
4591 glyph in the glyph matrix was found to come from a display
4592 string. Therefore, the condition was already evaluated, and
4593 the result was non-nil, otherwise the display string wouldn't
4594 have been displayed and we would have never been called for
4595 this property. Thus, we can skip the evaluation and assume
4596 its result is non-nil. */
4597 prop = XCDR (prop);
4598 }
4599
4600 if (CONSP (prop))
4601 /* Skip over `margin LOCATION'. */
4602 if (EQ (XCAR (prop), Qmargin))
4603 {
4604 prop = XCDR (prop);
4605 if (!CONSP (prop))
4606 return 0;
4607
4608 prop = XCDR (prop);
4609 if (!CONSP (prop))
4610 return 0;
4611 }
4612
4613 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4614 }
4615
4616
4617 /* Return 1 if STRING appears in the `display' property PROP. */
4618
4619 static int
4620 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4621 {
4622 if (CONSP (prop)
4623 && !EQ (XCAR (prop), Qwhen)
4624 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4625 {
4626 /* A list of sub-properties. */
4627 while (CONSP (prop))
4628 {
4629 if (single_display_spec_string_p (XCAR (prop), string))
4630 return 1;
4631 prop = XCDR (prop);
4632 }
4633 }
4634 else if (VECTORP (prop))
4635 {
4636 /* A vector of sub-properties. */
4637 int i;
4638 for (i = 0; i < ASIZE (prop); ++i)
4639 if (single_display_spec_string_p (AREF (prop, i), string))
4640 return 1;
4641 }
4642 else
4643 return single_display_spec_string_p (prop, string);
4644
4645 return 0;
4646 }
4647
4648 /* Look for STRING in overlays and text properties in the current
4649 buffer, between character positions FROM and TO (excluding TO).
4650 BACK_P non-zero means look back (in this case, TO is supposed to be
4651 less than FROM).
4652 Value is the first character position where STRING was found, or
4653 zero if it wasn't found before hitting TO.
4654
4655 This function may only use code that doesn't eval because it is
4656 called asynchronously from note_mouse_highlight. */
4657
4658 static EMACS_INT
4659 string_buffer_position_lim (Lisp_Object string,
4660 EMACS_INT from, EMACS_INT to, int back_p)
4661 {
4662 Lisp_Object limit, prop, pos;
4663 int found = 0;
4664
4665 pos = make_number (from);
4666
4667 if (!back_p) /* looking forward */
4668 {
4669 limit = make_number (min (to, ZV));
4670 while (!found && !EQ (pos, limit))
4671 {
4672 prop = Fget_char_property (pos, Qdisplay, Qnil);
4673 if (!NILP (prop) && display_prop_string_p (prop, string))
4674 found = 1;
4675 else
4676 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4677 limit);
4678 }
4679 }
4680 else /* looking back */
4681 {
4682 limit = make_number (max (to, BEGV));
4683 while (!found && !EQ (pos, limit))
4684 {
4685 prop = Fget_char_property (pos, Qdisplay, Qnil);
4686 if (!NILP (prop) && display_prop_string_p (prop, string))
4687 found = 1;
4688 else
4689 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4690 limit);
4691 }
4692 }
4693
4694 return found ? XINT (pos) : 0;
4695 }
4696
4697 /* Determine which buffer position in current buffer STRING comes from.
4698 AROUND_CHARPOS is an approximate position where it could come from.
4699 Value is the buffer position or 0 if it couldn't be determined.
4700
4701 This function is necessary because we don't record buffer positions
4702 in glyphs generated from strings (to keep struct glyph small).
4703 This function may only use code that doesn't eval because it is
4704 called asynchronously from note_mouse_highlight. */
4705
4706 static EMACS_INT
4707 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4708 {
4709 const int MAX_DISTANCE = 1000;
4710 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4711 around_charpos + MAX_DISTANCE,
4712 0);
4713
4714 if (!found)
4715 found = string_buffer_position_lim (string, around_charpos,
4716 around_charpos - MAX_DISTANCE, 1);
4717 return found;
4718 }
4719
4720
4721 \f
4722 /***********************************************************************
4723 `composition' property
4724 ***********************************************************************/
4725
4726 /* Set up iterator IT from `composition' property at its current
4727 position. Called from handle_stop. */
4728
4729 static enum prop_handled
4730 handle_composition_prop (struct it *it)
4731 {
4732 Lisp_Object prop, string;
4733 EMACS_INT pos, pos_byte, start, end;
4734
4735 if (STRINGP (it->string))
4736 {
4737 unsigned char *s;
4738
4739 pos = IT_STRING_CHARPOS (*it);
4740 pos_byte = IT_STRING_BYTEPOS (*it);
4741 string = it->string;
4742 s = SDATA (string) + pos_byte;
4743 it->c = STRING_CHAR (s);
4744 }
4745 else
4746 {
4747 pos = IT_CHARPOS (*it);
4748 pos_byte = IT_BYTEPOS (*it);
4749 string = Qnil;
4750 it->c = FETCH_CHAR (pos_byte);
4751 }
4752
4753 /* If there's a valid composition and point is not inside of the
4754 composition (in the case that the composition is from the current
4755 buffer), draw a glyph composed from the composition components. */
4756 if (find_composition (pos, -1, &start, &end, &prop, string)
4757 && COMPOSITION_VALID_P (start, end, prop)
4758 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4759 {
4760 if (start != pos)
4761 {
4762 if (STRINGP (it->string))
4763 pos_byte = string_char_to_byte (it->string, start);
4764 else
4765 pos_byte = CHAR_TO_BYTE (start);
4766 }
4767 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4768 prop, string);
4769
4770 if (it->cmp_it.id >= 0)
4771 {
4772 it->cmp_it.ch = -1;
4773 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4774 it->cmp_it.nglyphs = -1;
4775 }
4776 }
4777
4778 return HANDLED_NORMALLY;
4779 }
4780
4781
4782 \f
4783 /***********************************************************************
4784 Overlay strings
4785 ***********************************************************************/
4786
4787 /* The following structure is used to record overlay strings for
4788 later sorting in load_overlay_strings. */
4789
4790 struct overlay_entry
4791 {
4792 Lisp_Object overlay;
4793 Lisp_Object string;
4794 int priority;
4795 int after_string_p;
4796 };
4797
4798
4799 /* Set up iterator IT from overlay strings at its current position.
4800 Called from handle_stop. */
4801
4802 static enum prop_handled
4803 handle_overlay_change (struct it *it)
4804 {
4805 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4806 return HANDLED_RECOMPUTE_PROPS;
4807 else
4808 return HANDLED_NORMALLY;
4809 }
4810
4811
4812 /* Set up the next overlay string for delivery by IT, if there is an
4813 overlay string to deliver. Called by set_iterator_to_next when the
4814 end of the current overlay string is reached. If there are more
4815 overlay strings to display, IT->string and
4816 IT->current.overlay_string_index are set appropriately here.
4817 Otherwise IT->string is set to nil. */
4818
4819 static void
4820 next_overlay_string (struct it *it)
4821 {
4822 ++it->current.overlay_string_index;
4823 if (it->current.overlay_string_index == it->n_overlay_strings)
4824 {
4825 /* No more overlay strings. Restore IT's settings to what
4826 they were before overlay strings were processed, and
4827 continue to deliver from current_buffer. */
4828
4829 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4830 pop_it (it);
4831 xassert (it->sp > 0
4832 || (NILP (it->string)
4833 && it->method == GET_FROM_BUFFER
4834 && it->stop_charpos >= BEGV
4835 && it->stop_charpos <= it->end_charpos));
4836 it->current.overlay_string_index = -1;
4837 it->n_overlay_strings = 0;
4838 it->overlay_strings_charpos = -1;
4839
4840 /* If we're at the end of the buffer, record that we have
4841 processed the overlay strings there already, so that
4842 next_element_from_buffer doesn't try it again. */
4843 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4844 it->overlay_strings_at_end_processed_p = 1;
4845 }
4846 else
4847 {
4848 /* There are more overlay strings to process. If
4849 IT->current.overlay_string_index has advanced to a position
4850 where we must load IT->overlay_strings with more strings, do
4851 it. We must load at the IT->overlay_strings_charpos where
4852 IT->n_overlay_strings was originally computed; when invisible
4853 text is present, this might not be IT_CHARPOS (Bug#7016). */
4854 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4855
4856 if (it->current.overlay_string_index && i == 0)
4857 load_overlay_strings (it, it->overlay_strings_charpos);
4858
4859 /* Initialize IT to deliver display elements from the overlay
4860 string. */
4861 it->string = it->overlay_strings[i];
4862 it->multibyte_p = STRING_MULTIBYTE (it->string);
4863 SET_TEXT_POS (it->current.string_pos, 0, 0);
4864 it->method = GET_FROM_STRING;
4865 it->stop_charpos = 0;
4866 if (it->cmp_it.stop_pos >= 0)
4867 it->cmp_it.stop_pos = 0;
4868 it->prev_stop = 0;
4869 it->base_level_stop = 0;
4870
4871 /* Set up the bidi iterator for this overlay string. */
4872 if (it->bidi_p)
4873 {
4874 it->bidi_it.string.lstring = it->string;
4875 it->bidi_it.string.s = NULL;
4876 it->bidi_it.string.schars = SCHARS (it->string);
4877 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4878 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4879 it->bidi_it.string.unibyte = !it->multibyte_p;
4880 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4881 }
4882 }
4883
4884 CHECK_IT (it);
4885 }
4886
4887
4888 /* Compare two overlay_entry structures E1 and E2. Used as a
4889 comparison function for qsort in load_overlay_strings. Overlay
4890 strings for the same position are sorted so that
4891
4892 1. All after-strings come in front of before-strings, except
4893 when they come from the same overlay.
4894
4895 2. Within after-strings, strings are sorted so that overlay strings
4896 from overlays with higher priorities come first.
4897
4898 2. Within before-strings, strings are sorted so that overlay
4899 strings from overlays with higher priorities come last.
4900
4901 Value is analogous to strcmp. */
4902
4903
4904 static int
4905 compare_overlay_entries (const void *e1, const void *e2)
4906 {
4907 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4908 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4909 int result;
4910
4911 if (entry1->after_string_p != entry2->after_string_p)
4912 {
4913 /* Let after-strings appear in front of before-strings if
4914 they come from different overlays. */
4915 if (EQ (entry1->overlay, entry2->overlay))
4916 result = entry1->after_string_p ? 1 : -1;
4917 else
4918 result = entry1->after_string_p ? -1 : 1;
4919 }
4920 else if (entry1->after_string_p)
4921 /* After-strings sorted in order of decreasing priority. */
4922 result = entry2->priority - entry1->priority;
4923 else
4924 /* Before-strings sorted in order of increasing priority. */
4925 result = entry1->priority - entry2->priority;
4926
4927 return result;
4928 }
4929
4930
4931 /* Load the vector IT->overlay_strings with overlay strings from IT's
4932 current buffer position, or from CHARPOS if that is > 0. Set
4933 IT->n_overlays to the total number of overlay strings found.
4934
4935 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4936 a time. On entry into load_overlay_strings,
4937 IT->current.overlay_string_index gives the number of overlay
4938 strings that have already been loaded by previous calls to this
4939 function.
4940
4941 IT->add_overlay_start contains an additional overlay start
4942 position to consider for taking overlay strings from, if non-zero.
4943 This position comes into play when the overlay has an `invisible'
4944 property, and both before and after-strings. When we've skipped to
4945 the end of the overlay, because of its `invisible' property, we
4946 nevertheless want its before-string to appear.
4947 IT->add_overlay_start will contain the overlay start position
4948 in this case.
4949
4950 Overlay strings are sorted so that after-string strings come in
4951 front of before-string strings. Within before and after-strings,
4952 strings are sorted by overlay priority. See also function
4953 compare_overlay_entries. */
4954
4955 static void
4956 load_overlay_strings (struct it *it, EMACS_INT charpos)
4957 {
4958 Lisp_Object overlay, window, str, invisible;
4959 struct Lisp_Overlay *ov;
4960 EMACS_INT start, end;
4961 int size = 20;
4962 int n = 0, i, j, invis_p;
4963 struct overlay_entry *entries
4964 = (struct overlay_entry *) alloca (size * sizeof *entries);
4965
4966 if (charpos <= 0)
4967 charpos = IT_CHARPOS (*it);
4968
4969 /* Append the overlay string STRING of overlay OVERLAY to vector
4970 `entries' which has size `size' and currently contains `n'
4971 elements. AFTER_P non-zero means STRING is an after-string of
4972 OVERLAY. */
4973 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4974 do \
4975 { \
4976 Lisp_Object priority; \
4977 \
4978 if (n == size) \
4979 { \
4980 int new_size = 2 * size; \
4981 struct overlay_entry *old = entries; \
4982 entries = \
4983 (struct overlay_entry *) alloca (new_size \
4984 * sizeof *entries); \
4985 memcpy (entries, old, size * sizeof *entries); \
4986 size = new_size; \
4987 } \
4988 \
4989 entries[n].string = (STRING); \
4990 entries[n].overlay = (OVERLAY); \
4991 priority = Foverlay_get ((OVERLAY), Qpriority); \
4992 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4993 entries[n].after_string_p = (AFTER_P); \
4994 ++n; \
4995 } \
4996 while (0)
4997
4998 /* Process overlay before the overlay center. */
4999 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5000 {
5001 XSETMISC (overlay, ov);
5002 xassert (OVERLAYP (overlay));
5003 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5004 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5005
5006 if (end < charpos)
5007 break;
5008
5009 /* Skip this overlay if it doesn't start or end at IT's current
5010 position. */
5011 if (end != charpos && start != charpos)
5012 continue;
5013
5014 /* Skip this overlay if it doesn't apply to IT->w. */
5015 window = Foverlay_get (overlay, Qwindow);
5016 if (WINDOWP (window) && XWINDOW (window) != it->w)
5017 continue;
5018
5019 /* If the text ``under'' the overlay is invisible, both before-
5020 and after-strings from this overlay are visible; start and
5021 end position are indistinguishable. */
5022 invisible = Foverlay_get (overlay, Qinvisible);
5023 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5024
5025 /* If overlay has a non-empty before-string, record it. */
5026 if ((start == charpos || (end == charpos && invis_p))
5027 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5028 && SCHARS (str))
5029 RECORD_OVERLAY_STRING (overlay, str, 0);
5030
5031 /* If overlay has a non-empty after-string, record it. */
5032 if ((end == charpos || (start == charpos && invis_p))
5033 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5034 && SCHARS (str))
5035 RECORD_OVERLAY_STRING (overlay, str, 1);
5036 }
5037
5038 /* Process overlays after the overlay center. */
5039 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5040 {
5041 XSETMISC (overlay, ov);
5042 xassert (OVERLAYP (overlay));
5043 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5044 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5045
5046 if (start > charpos)
5047 break;
5048
5049 /* Skip this overlay if it doesn't start or end at IT's current
5050 position. */
5051 if (end != charpos && start != charpos)
5052 continue;
5053
5054 /* Skip this overlay if it doesn't apply to IT->w. */
5055 window = Foverlay_get (overlay, Qwindow);
5056 if (WINDOWP (window) && XWINDOW (window) != it->w)
5057 continue;
5058
5059 /* If the text ``under'' the overlay is invisible, it has a zero
5060 dimension, and both before- and after-strings apply. */
5061 invisible = Foverlay_get (overlay, Qinvisible);
5062 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5063
5064 /* If overlay has a non-empty before-string, record it. */
5065 if ((start == charpos || (end == charpos && invis_p))
5066 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5067 && SCHARS (str))
5068 RECORD_OVERLAY_STRING (overlay, str, 0);
5069
5070 /* If overlay has a non-empty after-string, record it. */
5071 if ((end == charpos || (start == charpos && invis_p))
5072 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5073 && SCHARS (str))
5074 RECORD_OVERLAY_STRING (overlay, str, 1);
5075 }
5076
5077 #undef RECORD_OVERLAY_STRING
5078
5079 /* Sort entries. */
5080 if (n > 1)
5081 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5082
5083 /* Record number of overlay strings, and where we computed it. */
5084 it->n_overlay_strings = n;
5085 it->overlay_strings_charpos = charpos;
5086
5087 /* IT->current.overlay_string_index is the number of overlay strings
5088 that have already been consumed by IT. Copy some of the
5089 remaining overlay strings to IT->overlay_strings. */
5090 i = 0;
5091 j = it->current.overlay_string_index;
5092 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5093 {
5094 it->overlay_strings[i] = entries[j].string;
5095 it->string_overlays[i++] = entries[j++].overlay;
5096 }
5097
5098 CHECK_IT (it);
5099 }
5100
5101
5102 /* Get the first chunk of overlay strings at IT's current buffer
5103 position, or at CHARPOS if that is > 0. Value is non-zero if at
5104 least one overlay string was found. */
5105
5106 static int
5107 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5108 {
5109 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5110 process. This fills IT->overlay_strings with strings, and sets
5111 IT->n_overlay_strings to the total number of strings to process.
5112 IT->pos.overlay_string_index has to be set temporarily to zero
5113 because load_overlay_strings needs this; it must be set to -1
5114 when no overlay strings are found because a zero value would
5115 indicate a position in the first overlay string. */
5116 it->current.overlay_string_index = 0;
5117 load_overlay_strings (it, charpos);
5118
5119 /* If we found overlay strings, set up IT to deliver display
5120 elements from the first one. Otherwise set up IT to deliver
5121 from current_buffer. */
5122 if (it->n_overlay_strings)
5123 {
5124 /* Make sure we know settings in current_buffer, so that we can
5125 restore meaningful values when we're done with the overlay
5126 strings. */
5127 if (compute_stop_p)
5128 compute_stop_pos (it);
5129 xassert (it->face_id >= 0);
5130
5131 /* Save IT's settings. They are restored after all overlay
5132 strings have been processed. */
5133 xassert (!compute_stop_p || it->sp == 0);
5134
5135 /* When called from handle_stop, there might be an empty display
5136 string loaded. In that case, don't bother saving it. */
5137 if (!STRINGP (it->string) || SCHARS (it->string))
5138 push_it (it, NULL);
5139
5140 /* Set up IT to deliver display elements from the first overlay
5141 string. */
5142 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5143 it->string = it->overlay_strings[0];
5144 it->from_overlay = Qnil;
5145 it->stop_charpos = 0;
5146 xassert (STRINGP (it->string));
5147 it->end_charpos = SCHARS (it->string);
5148 it->prev_stop = 0;
5149 it->base_level_stop = 0;
5150 it->multibyte_p = STRING_MULTIBYTE (it->string);
5151 it->method = GET_FROM_STRING;
5152 it->from_disp_prop_p = 0;
5153
5154 /* Force paragraph direction to be that of the parent
5155 buffer. */
5156 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5157 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5158 else
5159 it->paragraph_embedding = L2R;
5160
5161 /* Set up the bidi iterator for this overlay string. */
5162 if (it->bidi_p)
5163 {
5164 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5165
5166 it->bidi_it.string.lstring = it->string;
5167 it->bidi_it.string.s = NULL;
5168 it->bidi_it.string.schars = SCHARS (it->string);
5169 it->bidi_it.string.bufpos = pos;
5170 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5171 it->bidi_it.string.unibyte = !it->multibyte_p;
5172 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5173 }
5174 return 1;
5175 }
5176
5177 it->current.overlay_string_index = -1;
5178 return 0;
5179 }
5180
5181 static int
5182 get_overlay_strings (struct it *it, EMACS_INT charpos)
5183 {
5184 it->string = Qnil;
5185 it->method = GET_FROM_BUFFER;
5186
5187 (void) get_overlay_strings_1 (it, charpos, 1);
5188
5189 CHECK_IT (it);
5190
5191 /* Value is non-zero if we found at least one overlay string. */
5192 return STRINGP (it->string);
5193 }
5194
5195
5196 \f
5197 /***********************************************************************
5198 Saving and restoring state
5199 ***********************************************************************/
5200
5201 /* Save current settings of IT on IT->stack. Called, for example,
5202 before setting up IT for an overlay string, to be able to restore
5203 IT's settings to what they were after the overlay string has been
5204 processed. If POSITION is non-NULL, it is the position to save on
5205 the stack instead of IT->position. */
5206
5207 static void
5208 push_it (struct it *it, struct text_pos *position)
5209 {
5210 struct iterator_stack_entry *p;
5211
5212 xassert (it->sp < IT_STACK_SIZE);
5213 p = it->stack + it->sp;
5214
5215 p->stop_charpos = it->stop_charpos;
5216 p->prev_stop = it->prev_stop;
5217 p->base_level_stop = it->base_level_stop;
5218 p->cmp_it = it->cmp_it;
5219 xassert (it->face_id >= 0);
5220 p->face_id = it->face_id;
5221 p->string = it->string;
5222 p->method = it->method;
5223 p->from_overlay = it->from_overlay;
5224 switch (p->method)
5225 {
5226 case GET_FROM_IMAGE:
5227 p->u.image.object = it->object;
5228 p->u.image.image_id = it->image_id;
5229 p->u.image.slice = it->slice;
5230 break;
5231 case GET_FROM_STRETCH:
5232 p->u.stretch.object = it->object;
5233 break;
5234 }
5235 p->position = position ? *position : it->position;
5236 p->current = it->current;
5237 p->end_charpos = it->end_charpos;
5238 p->string_nchars = it->string_nchars;
5239 p->area = it->area;
5240 p->multibyte_p = it->multibyte_p;
5241 p->avoid_cursor_p = it->avoid_cursor_p;
5242 p->space_width = it->space_width;
5243 p->font_height = it->font_height;
5244 p->voffset = it->voffset;
5245 p->string_from_display_prop_p = it->string_from_display_prop_p;
5246 p->display_ellipsis_p = 0;
5247 p->line_wrap = it->line_wrap;
5248 p->bidi_p = it->bidi_p;
5249 p->paragraph_embedding = it->paragraph_embedding;
5250 p->from_disp_prop_p = it->from_disp_prop_p;
5251 ++it->sp;
5252
5253 /* Save the state of the bidi iterator as well. */
5254 if (it->bidi_p)
5255 bidi_push_it (&it->bidi_it);
5256 }
5257
5258 static void
5259 iterate_out_of_display_property (struct it *it)
5260 {
5261 int buffer_p = BUFFERP (it->object);
5262 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5263 EMACS_INT bob = (buffer_p ? BEGV : 0);
5264
5265 /* Maybe initialize paragraph direction. If we are at the beginning
5266 of a new paragraph, next_element_from_buffer may not have a
5267 chance to do that. */
5268 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5269 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5270 /* prev_stop can be zero, so check against BEGV as well. */
5271 while (it->bidi_it.charpos >= bob
5272 && it->prev_stop <= it->bidi_it.charpos
5273 && it->bidi_it.charpos < CHARPOS (it->position))
5274 bidi_move_to_visually_next (&it->bidi_it);
5275 /* Record the stop_pos we just crossed, for when we cross it
5276 back, maybe. */
5277 if (it->bidi_it.charpos > CHARPOS (it->position))
5278 it->prev_stop = CHARPOS (it->position);
5279 /* If we ended up not where pop_it put us, resync IT's
5280 positional members with the bidi iterator. */
5281 if (it->bidi_it.charpos != CHARPOS (it->position))
5282 {
5283 SET_TEXT_POS (it->position,
5284 it->bidi_it.charpos, it->bidi_it.bytepos);
5285 if (buffer_p)
5286 it->current.pos = it->position;
5287 else
5288 it->current.string_pos = it->position;
5289 }
5290 }
5291
5292 /* Restore IT's settings from IT->stack. Called, for example, when no
5293 more overlay strings must be processed, and we return to delivering
5294 display elements from a buffer, or when the end of a string from a
5295 `display' property is reached and we return to delivering display
5296 elements from an overlay string, or from a buffer. */
5297
5298 static void
5299 pop_it (struct it *it)
5300 {
5301 struct iterator_stack_entry *p;
5302 int from_display_prop = it->from_disp_prop_p;
5303
5304 xassert (it->sp > 0);
5305 --it->sp;
5306 p = it->stack + it->sp;
5307 it->stop_charpos = p->stop_charpos;
5308 it->prev_stop = p->prev_stop;
5309 it->base_level_stop = p->base_level_stop;
5310 it->cmp_it = p->cmp_it;
5311 it->face_id = p->face_id;
5312 it->current = p->current;
5313 it->position = p->position;
5314 it->string = p->string;
5315 it->from_overlay = p->from_overlay;
5316 if (NILP (it->string))
5317 SET_TEXT_POS (it->current.string_pos, -1, -1);
5318 it->method = p->method;
5319 switch (it->method)
5320 {
5321 case GET_FROM_IMAGE:
5322 it->image_id = p->u.image.image_id;
5323 it->object = p->u.image.object;
5324 it->slice = p->u.image.slice;
5325 break;
5326 case GET_FROM_STRETCH:
5327 it->object = p->u.stretch.object;
5328 break;
5329 case GET_FROM_BUFFER:
5330 it->object = it->w->buffer;
5331 break;
5332 case GET_FROM_STRING:
5333 it->object = it->string;
5334 break;
5335 case GET_FROM_DISPLAY_VECTOR:
5336 if (it->s)
5337 it->method = GET_FROM_C_STRING;
5338 else if (STRINGP (it->string))
5339 it->method = GET_FROM_STRING;
5340 else
5341 {
5342 it->method = GET_FROM_BUFFER;
5343 it->object = it->w->buffer;
5344 }
5345 }
5346 it->end_charpos = p->end_charpos;
5347 it->string_nchars = p->string_nchars;
5348 it->area = p->area;
5349 it->multibyte_p = p->multibyte_p;
5350 it->avoid_cursor_p = p->avoid_cursor_p;
5351 it->space_width = p->space_width;
5352 it->font_height = p->font_height;
5353 it->voffset = p->voffset;
5354 it->string_from_display_prop_p = p->string_from_display_prop_p;
5355 it->line_wrap = p->line_wrap;
5356 it->bidi_p = p->bidi_p;
5357 it->paragraph_embedding = p->paragraph_embedding;
5358 it->from_disp_prop_p = p->from_disp_prop_p;
5359 if (it->bidi_p)
5360 {
5361 bidi_pop_it (&it->bidi_it);
5362 /* Bidi-iterate until we get out of the portion of text, if any,
5363 covered by a `display' text property or by an overlay with
5364 `display' property. (We cannot just jump there, because the
5365 internal coherency of the bidi iterator state can not be
5366 preserved across such jumps.) We also must determine the
5367 paragraph base direction if the overlay we just processed is
5368 at the beginning of a new paragraph. */
5369 if (from_display_prop
5370 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5371 iterate_out_of_display_property (it);
5372
5373 xassert ((BUFFERP (it->object)
5374 && IT_CHARPOS (*it) == it->bidi_it.charpos
5375 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5376 || (STRINGP (it->object)
5377 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5378 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos));
5379 }
5380 }
5381
5382
5383 \f
5384 /***********************************************************************
5385 Moving over lines
5386 ***********************************************************************/
5387
5388 /* Set IT's current position to the previous line start. */
5389
5390 static void
5391 back_to_previous_line_start (struct it *it)
5392 {
5393 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5394 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5395 }
5396
5397
5398 /* Move IT to the next line start.
5399
5400 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5401 we skipped over part of the text (as opposed to moving the iterator
5402 continuously over the text). Otherwise, don't change the value
5403 of *SKIPPED_P.
5404
5405 Newlines may come from buffer text, overlay strings, or strings
5406 displayed via the `display' property. That's the reason we can't
5407 simply use find_next_newline_no_quit.
5408
5409 Note that this function may not skip over invisible text that is so
5410 because of text properties and immediately follows a newline. If
5411 it would, function reseat_at_next_visible_line_start, when called
5412 from set_iterator_to_next, would effectively make invisible
5413 characters following a newline part of the wrong glyph row, which
5414 leads to wrong cursor motion. */
5415
5416 static int
5417 forward_to_next_line_start (struct it *it, int *skipped_p)
5418 {
5419 int old_selective, newline_found_p, n;
5420 const int MAX_NEWLINE_DISTANCE = 500;
5421
5422 /* If already on a newline, just consume it to avoid unintended
5423 skipping over invisible text below. */
5424 if (it->what == IT_CHARACTER
5425 && it->c == '\n'
5426 && CHARPOS (it->position) == IT_CHARPOS (*it))
5427 {
5428 set_iterator_to_next (it, 0);
5429 it->c = 0;
5430 return 1;
5431 }
5432
5433 /* Don't handle selective display in the following. It's (a)
5434 unnecessary because it's done by the caller, and (b) leads to an
5435 infinite recursion because next_element_from_ellipsis indirectly
5436 calls this function. */
5437 old_selective = it->selective;
5438 it->selective = 0;
5439
5440 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5441 from buffer text. */
5442 for (n = newline_found_p = 0;
5443 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5444 n += STRINGP (it->string) ? 0 : 1)
5445 {
5446 if (!get_next_display_element (it))
5447 return 0;
5448 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5449 set_iterator_to_next (it, 0);
5450 }
5451
5452 /* If we didn't find a newline near enough, see if we can use a
5453 short-cut. */
5454 if (!newline_found_p)
5455 {
5456 EMACS_INT start = IT_CHARPOS (*it);
5457 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5458 Lisp_Object pos;
5459
5460 xassert (!STRINGP (it->string));
5461
5462 /* If we are not bidi-reordering, and there isn't any `display'
5463 property in sight, and no overlays, we can just use the
5464 position of the newline in buffer text. */
5465 if (!it->bidi_p
5466 && (it->stop_charpos >= limit
5467 || ((pos = Fnext_single_property_change (make_number (start),
5468 Qdisplay, Qnil,
5469 make_number (limit)),
5470 NILP (pos))
5471 && next_overlay_change (start) == ZV)))
5472 {
5473 IT_CHARPOS (*it) = limit;
5474 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5475 *skipped_p = newline_found_p = 1;
5476 }
5477 else
5478 {
5479 while (get_next_display_element (it)
5480 && !newline_found_p)
5481 {
5482 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5483 set_iterator_to_next (it, 0);
5484 }
5485 }
5486 }
5487
5488 it->selective = old_selective;
5489 return newline_found_p;
5490 }
5491
5492
5493 /* Set IT's current position to the previous visible line start. Skip
5494 invisible text that is so either due to text properties or due to
5495 selective display. Caution: this does not change IT->current_x and
5496 IT->hpos. */
5497
5498 static void
5499 back_to_previous_visible_line_start (struct it *it)
5500 {
5501 while (IT_CHARPOS (*it) > BEGV)
5502 {
5503 back_to_previous_line_start (it);
5504
5505 if (IT_CHARPOS (*it) <= BEGV)
5506 break;
5507
5508 /* If selective > 0, then lines indented more than its value are
5509 invisible. */
5510 if (it->selective > 0
5511 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5512 (double) it->selective)) /* iftc */
5513 continue;
5514
5515 /* Check the newline before point for invisibility. */
5516 {
5517 Lisp_Object prop;
5518 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5519 Qinvisible, it->window);
5520 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5521 continue;
5522 }
5523
5524 if (IT_CHARPOS (*it) <= BEGV)
5525 break;
5526
5527 {
5528 struct it it2;
5529 EMACS_INT pos;
5530 EMACS_INT beg, end;
5531 Lisp_Object val, overlay;
5532
5533 /* If newline is part of a composition, continue from start of composition */
5534 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5535 && beg < IT_CHARPOS (*it))
5536 goto replaced;
5537
5538 /* If newline is replaced by a display property, find start of overlay
5539 or interval and continue search from that point. */
5540 it2 = *it;
5541 pos = --IT_CHARPOS (it2);
5542 --IT_BYTEPOS (it2);
5543 it2.sp = 0;
5544 it2.string_from_display_prop_p = 0;
5545 if (handle_display_prop (&it2) == HANDLED_RETURN
5546 && !NILP (val = get_char_property_and_overlay
5547 (make_number (pos), Qdisplay, Qnil, &overlay))
5548 && (OVERLAYP (overlay)
5549 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5550 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5551 {
5552 /* If the call to handle_display_prop above pushed the
5553 iterator state, that causes side effects for the bidi
5554 iterator by calling bidi_push_it. Undo those side
5555 effects. */
5556 while (it2.sp > 0)
5557 {
5558 /* push_it calls bidi_push_it only if the bidi_p flag
5559 is set in the iterator being pushed. */
5560 if (it2.stack[--it2.sp].bidi_p)
5561 bidi_pop_it (&it2.bidi_it);
5562 }
5563 goto replaced;
5564 }
5565
5566 /* Newline is not replaced by anything -- so we are done. */
5567 break;
5568
5569 replaced:
5570 if (beg < BEGV)
5571 beg = BEGV;
5572 IT_CHARPOS (*it) = beg;
5573 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5574 }
5575 }
5576
5577 it->continuation_lines_width = 0;
5578
5579 xassert (IT_CHARPOS (*it) >= BEGV);
5580 xassert (IT_CHARPOS (*it) == BEGV
5581 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5582 CHECK_IT (it);
5583 }
5584
5585
5586 /* Reseat iterator IT at the previous visible line start. Skip
5587 invisible text that is so either due to text properties or due to
5588 selective display. At the end, update IT's overlay information,
5589 face information etc. */
5590
5591 void
5592 reseat_at_previous_visible_line_start (struct it *it)
5593 {
5594 back_to_previous_visible_line_start (it);
5595 reseat (it, it->current.pos, 1);
5596 CHECK_IT (it);
5597 }
5598
5599
5600 /* Reseat iterator IT on the next visible line start in the current
5601 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5602 preceding the line start. Skip over invisible text that is so
5603 because of selective display. Compute faces, overlays etc at the
5604 new position. Note that this function does not skip over text that
5605 is invisible because of text properties. */
5606
5607 static void
5608 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5609 {
5610 int newline_found_p, skipped_p = 0;
5611
5612 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5613
5614 /* Skip over lines that are invisible because they are indented
5615 more than the value of IT->selective. */
5616 if (it->selective > 0)
5617 while (IT_CHARPOS (*it) < ZV
5618 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5619 (double) it->selective)) /* iftc */
5620 {
5621 xassert (IT_BYTEPOS (*it) == BEGV
5622 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5623 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5624 }
5625
5626 /* Position on the newline if that's what's requested. */
5627 if (on_newline_p && newline_found_p)
5628 {
5629 if (STRINGP (it->string))
5630 {
5631 if (IT_STRING_CHARPOS (*it) > 0)
5632 {
5633 if (!it->bidi_p)
5634 {
5635 --IT_STRING_CHARPOS (*it);
5636 --IT_STRING_BYTEPOS (*it);
5637 }
5638 else
5639 /* Setting this flag will cause
5640 bidi_move_to_visually_next not to advance, but
5641 instead deliver the current character (newline),
5642 which is what the ON_NEWLINE_P flag wants. */
5643 it->bidi_it.first_elt = 1;
5644 }
5645 }
5646 else if (IT_CHARPOS (*it) > BEGV)
5647 {
5648 if (!it->bidi_p)
5649 {
5650 --IT_CHARPOS (*it);
5651 --IT_BYTEPOS (*it);
5652 }
5653 /* With bidi iteration, the call to `reseat' will cause
5654 bidi_move_to_visually_next deliver the current character,
5655 the newline, instead of advancing. */
5656 reseat (it, it->current.pos, 0);
5657 }
5658 }
5659 else if (skipped_p)
5660 reseat (it, it->current.pos, 0);
5661
5662 CHECK_IT (it);
5663 }
5664
5665
5666 \f
5667 /***********************************************************************
5668 Changing an iterator's position
5669 ***********************************************************************/
5670
5671 /* Change IT's current position to POS in current_buffer. If FORCE_P
5672 is non-zero, always check for text properties at the new position.
5673 Otherwise, text properties are only looked up if POS >=
5674 IT->check_charpos of a property. */
5675
5676 static void
5677 reseat (struct it *it, struct text_pos pos, int force_p)
5678 {
5679 EMACS_INT original_pos = IT_CHARPOS (*it);
5680
5681 reseat_1 (it, pos, 0);
5682
5683 /* Determine where to check text properties. Avoid doing it
5684 where possible because text property lookup is very expensive. */
5685 if (force_p
5686 || CHARPOS (pos) > it->stop_charpos
5687 || CHARPOS (pos) < original_pos)
5688 {
5689 if (it->bidi_p)
5690 {
5691 /* For bidi iteration, we need to prime prev_stop and
5692 base_level_stop with our best estimations. */
5693 if (CHARPOS (pos) < it->prev_stop)
5694 {
5695 handle_stop_backwards (it, BEGV);
5696 if (CHARPOS (pos) < it->base_level_stop)
5697 it->base_level_stop = 0;
5698 }
5699 else if (CHARPOS (pos) > it->stop_charpos
5700 && it->stop_charpos >= BEGV)
5701 handle_stop_backwards (it, it->stop_charpos);
5702 else /* force_p */
5703 handle_stop (it);
5704 }
5705 else
5706 {
5707 handle_stop (it);
5708 it->prev_stop = it->base_level_stop = 0;
5709 }
5710
5711 }
5712
5713 CHECK_IT (it);
5714 }
5715
5716
5717 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5718 IT->stop_pos to POS, also. */
5719
5720 static void
5721 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5722 {
5723 /* Don't call this function when scanning a C string. */
5724 xassert (it->s == NULL);
5725
5726 /* POS must be a reasonable value. */
5727 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5728
5729 it->current.pos = it->position = pos;
5730 it->end_charpos = ZV;
5731 it->dpvec = NULL;
5732 it->current.dpvec_index = -1;
5733 it->current.overlay_string_index = -1;
5734 IT_STRING_CHARPOS (*it) = -1;
5735 IT_STRING_BYTEPOS (*it) = -1;
5736 it->string = Qnil;
5737 it->method = GET_FROM_BUFFER;
5738 it->object = it->w->buffer;
5739 it->area = TEXT_AREA;
5740 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5741 it->sp = 0;
5742 it->string_from_display_prop_p = 0;
5743 it->face_before_selective_p = 0;
5744 if (it->bidi_p)
5745 {
5746 it->bidi_it.first_elt = 1;
5747 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5748 it->bidi_it.disp_pos = -1;
5749 it->bidi_it.string.s = NULL;
5750 it->bidi_it.string.lstring = Qnil;
5751 it->bidi_it.string.bufpos = 0;
5752 it->bidi_it.string.unibyte = 0;
5753 }
5754
5755 if (set_stop_p)
5756 {
5757 it->stop_charpos = CHARPOS (pos);
5758 it->base_level_stop = CHARPOS (pos);
5759 }
5760 }
5761
5762
5763 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5764 If S is non-null, it is a C string to iterate over. Otherwise,
5765 STRING gives a Lisp string to iterate over.
5766
5767 If PRECISION > 0, don't return more then PRECISION number of
5768 characters from the string.
5769
5770 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5771 characters have been returned. FIELD_WIDTH < 0 means an infinite
5772 field width.
5773
5774 MULTIBYTE = 0 means disable processing of multibyte characters,
5775 MULTIBYTE > 0 means enable it,
5776 MULTIBYTE < 0 means use IT->multibyte_p.
5777
5778 IT must be initialized via a prior call to init_iterator before
5779 calling this function. */
5780
5781 static void
5782 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5783 EMACS_INT charpos, EMACS_INT precision, int field_width,
5784 int multibyte)
5785 {
5786 /* No region in strings. */
5787 it->region_beg_charpos = it->region_end_charpos = -1;
5788
5789 /* No text property checks performed by default, but see below. */
5790 it->stop_charpos = -1;
5791
5792 /* Set iterator position and end position. */
5793 memset (&it->current, 0, sizeof it->current);
5794 it->current.overlay_string_index = -1;
5795 it->current.dpvec_index = -1;
5796 xassert (charpos >= 0);
5797
5798 /* If STRING is specified, use its multibyteness, otherwise use the
5799 setting of MULTIBYTE, if specified. */
5800 if (multibyte >= 0)
5801 it->multibyte_p = multibyte > 0;
5802
5803 /* Bidirectional reordering of strings is controlled by the default
5804 value of bidi-display-reordering. */
5805 it->bidi_p = !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
5806
5807 if (s == NULL)
5808 {
5809 xassert (STRINGP (string));
5810 it->string = string;
5811 it->s = NULL;
5812 it->end_charpos = it->string_nchars = SCHARS (string);
5813 it->method = GET_FROM_STRING;
5814 it->current.string_pos = string_pos (charpos, string);
5815
5816 if (it->bidi_p)
5817 {
5818 it->bidi_it.string.lstring = string;
5819 it->bidi_it.string.s = NULL;
5820 it->bidi_it.string.schars = it->end_charpos;
5821 it->bidi_it.string.bufpos = 0;
5822 it->bidi_it.string.from_disp_str = 0;
5823 it->bidi_it.string.unibyte = !it->multibyte_p;
5824 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5825 FRAME_WINDOW_P (it->f), &it->bidi_it);
5826 }
5827 }
5828 else
5829 {
5830 it->s = (const unsigned char *) s;
5831 it->string = Qnil;
5832
5833 /* Note that we use IT->current.pos, not it->current.string_pos,
5834 for displaying C strings. */
5835 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5836 if (it->multibyte_p)
5837 {
5838 it->current.pos = c_string_pos (charpos, s, 1);
5839 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5840 }
5841 else
5842 {
5843 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5844 it->end_charpos = it->string_nchars = strlen (s);
5845 }
5846
5847 if (it->bidi_p)
5848 {
5849 it->bidi_it.string.lstring = Qnil;
5850 it->bidi_it.string.s = s;
5851 it->bidi_it.string.schars = it->end_charpos;
5852 it->bidi_it.string.bufpos = 0;
5853 it->bidi_it.string.from_disp_str = 0;
5854 it->bidi_it.string.unibyte = !it->multibyte_p;
5855 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5856 &it->bidi_it);
5857 }
5858 it->method = GET_FROM_C_STRING;
5859 }
5860
5861 /* PRECISION > 0 means don't return more than PRECISION characters
5862 from the string. */
5863 if (precision > 0 && it->end_charpos - charpos > precision)
5864 {
5865 it->end_charpos = it->string_nchars = charpos + precision;
5866 if (it->bidi_p)
5867 it->bidi_it.string.schars = it->end_charpos;
5868 }
5869
5870 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5871 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5872 FIELD_WIDTH < 0 means infinite field width. This is useful for
5873 padding with `-' at the end of a mode line. */
5874 if (field_width < 0)
5875 field_width = INFINITY;
5876 /* Implementation note: We deliberately don't enlarge
5877 it->bidi_it.string.schars here to fit it->end_charpos, because
5878 the bidi iterator cannot produce characters out of thin air. */
5879 if (field_width > it->end_charpos - charpos)
5880 it->end_charpos = charpos + field_width;
5881
5882 /* Use the standard display table for displaying strings. */
5883 if (DISP_TABLE_P (Vstandard_display_table))
5884 it->dp = XCHAR_TABLE (Vstandard_display_table);
5885
5886 it->stop_charpos = charpos;
5887 it->prev_stop = charpos;
5888 it->base_level_stop = 0;
5889 if (it->bidi_p)
5890 {
5891 it->bidi_it.first_elt = 1;
5892 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5893 it->bidi_it.disp_pos = -1;
5894 }
5895 if (s == NULL && it->multibyte_p)
5896 {
5897 EMACS_INT endpos = SCHARS (it->string);
5898 if (endpos > it->end_charpos)
5899 endpos = it->end_charpos;
5900 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5901 it->string);
5902 }
5903 CHECK_IT (it);
5904 }
5905
5906
5907 \f
5908 /***********************************************************************
5909 Iteration
5910 ***********************************************************************/
5911
5912 /* Map enum it_method value to corresponding next_element_from_* function. */
5913
5914 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5915 {
5916 next_element_from_buffer,
5917 next_element_from_display_vector,
5918 next_element_from_string,
5919 next_element_from_c_string,
5920 next_element_from_image,
5921 next_element_from_stretch
5922 };
5923
5924 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5925
5926
5927 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5928 (possibly with the following characters). */
5929
5930 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5931 ((IT)->cmp_it.id >= 0 \
5932 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5933 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5934 END_CHARPOS, (IT)->w, \
5935 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5936 (IT)->string)))
5937
5938
5939 /* Lookup the char-table Vglyphless_char_display for character C (-1
5940 if we want information for no-font case), and return the display
5941 method symbol. By side-effect, update it->what and
5942 it->glyphless_method. This function is called from
5943 get_next_display_element for each character element, and from
5944 x_produce_glyphs when no suitable font was found. */
5945
5946 Lisp_Object
5947 lookup_glyphless_char_display (int c, struct it *it)
5948 {
5949 Lisp_Object glyphless_method = Qnil;
5950
5951 if (CHAR_TABLE_P (Vglyphless_char_display)
5952 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5953 {
5954 if (c >= 0)
5955 {
5956 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
5957 if (CONSP (glyphless_method))
5958 glyphless_method = FRAME_WINDOW_P (it->f)
5959 ? XCAR (glyphless_method)
5960 : XCDR (glyphless_method);
5961 }
5962 else
5963 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
5964 }
5965
5966 retry:
5967 if (NILP (glyphless_method))
5968 {
5969 if (c >= 0)
5970 /* The default is to display the character by a proper font. */
5971 return Qnil;
5972 /* The default for the no-font case is to display an empty box. */
5973 glyphless_method = Qempty_box;
5974 }
5975 if (EQ (glyphless_method, Qzero_width))
5976 {
5977 if (c >= 0)
5978 return glyphless_method;
5979 /* This method can't be used for the no-font case. */
5980 glyphless_method = Qempty_box;
5981 }
5982 if (EQ (glyphless_method, Qthin_space))
5983 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5984 else if (EQ (glyphless_method, Qempty_box))
5985 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5986 else if (EQ (glyphless_method, Qhex_code))
5987 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5988 else if (STRINGP (glyphless_method))
5989 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5990 else
5991 {
5992 /* Invalid value. We use the default method. */
5993 glyphless_method = Qnil;
5994 goto retry;
5995 }
5996 it->what = IT_GLYPHLESS;
5997 return glyphless_method;
5998 }
5999
6000 /* Load IT's display element fields with information about the next
6001 display element from the current position of IT. Value is zero if
6002 end of buffer (or C string) is reached. */
6003
6004 static struct frame *last_escape_glyph_frame = NULL;
6005 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6006 static int last_escape_glyph_merged_face_id = 0;
6007
6008 struct frame *last_glyphless_glyph_frame = NULL;
6009 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6010 int last_glyphless_glyph_merged_face_id = 0;
6011
6012 static int
6013 get_next_display_element (struct it *it)
6014 {
6015 /* Non-zero means that we found a display element. Zero means that
6016 we hit the end of what we iterate over. Performance note: the
6017 function pointer `method' used here turns out to be faster than
6018 using a sequence of if-statements. */
6019 int success_p;
6020
6021 get_next:
6022 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6023
6024 if (it->what == IT_CHARACTER)
6025 {
6026 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6027 and only if (a) the resolved directionality of that character
6028 is R..." */
6029 /* FIXME: Do we need an exception for characters from display
6030 tables? */
6031 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6032 it->c = bidi_mirror_char (it->c);
6033 /* Map via display table or translate control characters.
6034 IT->c, IT->len etc. have been set to the next character by
6035 the function call above. If we have a display table, and it
6036 contains an entry for IT->c, translate it. Don't do this if
6037 IT->c itself comes from a display table, otherwise we could
6038 end up in an infinite recursion. (An alternative could be to
6039 count the recursion depth of this function and signal an
6040 error when a certain maximum depth is reached.) Is it worth
6041 it? */
6042 if (success_p && it->dpvec == NULL)
6043 {
6044 Lisp_Object dv;
6045 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6046 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6047 nbsp_or_shy = char_is_other;
6048 int c = it->c; /* This is the character to display. */
6049
6050 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6051 {
6052 xassert (SINGLE_BYTE_CHAR_P (c));
6053 if (unibyte_display_via_language_environment)
6054 {
6055 c = DECODE_CHAR (unibyte, c);
6056 if (c < 0)
6057 c = BYTE8_TO_CHAR (it->c);
6058 }
6059 else
6060 c = BYTE8_TO_CHAR (it->c);
6061 }
6062
6063 if (it->dp
6064 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6065 VECTORP (dv)))
6066 {
6067 struct Lisp_Vector *v = XVECTOR (dv);
6068
6069 /* Return the first character from the display table
6070 entry, if not empty. If empty, don't display the
6071 current character. */
6072 if (v->header.size)
6073 {
6074 it->dpvec_char_len = it->len;
6075 it->dpvec = v->contents;
6076 it->dpend = v->contents + v->header.size;
6077 it->current.dpvec_index = 0;
6078 it->dpvec_face_id = -1;
6079 it->saved_face_id = it->face_id;
6080 it->method = GET_FROM_DISPLAY_VECTOR;
6081 it->ellipsis_p = 0;
6082 }
6083 else
6084 {
6085 set_iterator_to_next (it, 0);
6086 }
6087 goto get_next;
6088 }
6089
6090 if (! NILP (lookup_glyphless_char_display (c, it)))
6091 {
6092 if (it->what == IT_GLYPHLESS)
6093 goto done;
6094 /* Don't display this character. */
6095 set_iterator_to_next (it, 0);
6096 goto get_next;
6097 }
6098
6099 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6100 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6101 : c == 0xAD ? char_is_soft_hyphen
6102 : char_is_other);
6103
6104 /* Translate control characters into `\003' or `^C' form.
6105 Control characters coming from a display table entry are
6106 currently not translated because we use IT->dpvec to hold
6107 the translation. This could easily be changed but I
6108 don't believe that it is worth doing.
6109
6110 NBSP and SOFT-HYPEN are property translated too.
6111
6112 Non-printable characters and raw-byte characters are also
6113 translated to octal form. */
6114 if (((c < ' ' || c == 127) /* ASCII control chars */
6115 ? (it->area != TEXT_AREA
6116 /* In mode line, treat \n, \t like other crl chars. */
6117 || (c != '\t'
6118 && it->glyph_row
6119 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6120 || (c != '\n' && c != '\t'))
6121 : (nbsp_or_shy
6122 || CHAR_BYTE8_P (c)
6123 || ! CHAR_PRINTABLE_P (c))))
6124 {
6125 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6126 or a non-printable character which must be displayed
6127 either as '\003' or as `^C' where the '\\' and '^'
6128 can be defined in the display table. Fill
6129 IT->ctl_chars with glyphs for what we have to
6130 display. Then, set IT->dpvec to these glyphs. */
6131 Lisp_Object gc;
6132 int ctl_len;
6133 int face_id, lface_id = 0 ;
6134 int escape_glyph;
6135
6136 /* Handle control characters with ^. */
6137
6138 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6139 {
6140 int g;
6141
6142 g = '^'; /* default glyph for Control */
6143 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6144 if (it->dp
6145 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6146 && GLYPH_CODE_CHAR_VALID_P (gc))
6147 {
6148 g = GLYPH_CODE_CHAR (gc);
6149 lface_id = GLYPH_CODE_FACE (gc);
6150 }
6151 if (lface_id)
6152 {
6153 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6154 }
6155 else if (it->f == last_escape_glyph_frame
6156 && it->face_id == last_escape_glyph_face_id)
6157 {
6158 face_id = last_escape_glyph_merged_face_id;
6159 }
6160 else
6161 {
6162 /* Merge the escape-glyph face into the current face. */
6163 face_id = merge_faces (it->f, Qescape_glyph, 0,
6164 it->face_id);
6165 last_escape_glyph_frame = it->f;
6166 last_escape_glyph_face_id = it->face_id;
6167 last_escape_glyph_merged_face_id = face_id;
6168 }
6169
6170 XSETINT (it->ctl_chars[0], g);
6171 XSETINT (it->ctl_chars[1], c ^ 0100);
6172 ctl_len = 2;
6173 goto display_control;
6174 }
6175
6176 /* Handle non-break space in the mode where it only gets
6177 highlighting. */
6178
6179 if (EQ (Vnobreak_char_display, Qt)
6180 && nbsp_or_shy == char_is_nbsp)
6181 {
6182 /* Merge the no-break-space face into the current face. */
6183 face_id = merge_faces (it->f, Qnobreak_space, 0,
6184 it->face_id);
6185
6186 c = ' ';
6187 XSETINT (it->ctl_chars[0], ' ');
6188 ctl_len = 1;
6189 goto display_control;
6190 }
6191
6192 /* Handle sequences that start with the "escape glyph". */
6193
6194 /* the default escape glyph is \. */
6195 escape_glyph = '\\';
6196
6197 if (it->dp
6198 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6199 && GLYPH_CODE_CHAR_VALID_P (gc))
6200 {
6201 escape_glyph = GLYPH_CODE_CHAR (gc);
6202 lface_id = GLYPH_CODE_FACE (gc);
6203 }
6204 if (lface_id)
6205 {
6206 /* The display table specified a face.
6207 Merge it into face_id and also into escape_glyph. */
6208 face_id = merge_faces (it->f, Qt, lface_id,
6209 it->face_id);
6210 }
6211 else if (it->f == last_escape_glyph_frame
6212 && it->face_id == last_escape_glyph_face_id)
6213 {
6214 face_id = last_escape_glyph_merged_face_id;
6215 }
6216 else
6217 {
6218 /* Merge the escape-glyph face into the current face. */
6219 face_id = merge_faces (it->f, Qescape_glyph, 0,
6220 it->face_id);
6221 last_escape_glyph_frame = it->f;
6222 last_escape_glyph_face_id = it->face_id;
6223 last_escape_glyph_merged_face_id = face_id;
6224 }
6225
6226 /* Handle soft hyphens in the mode where they only get
6227 highlighting. */
6228
6229 if (EQ (Vnobreak_char_display, Qt)
6230 && nbsp_or_shy == char_is_soft_hyphen)
6231 {
6232 XSETINT (it->ctl_chars[0], '-');
6233 ctl_len = 1;
6234 goto display_control;
6235 }
6236
6237 /* Handle non-break space and soft hyphen
6238 with the escape glyph. */
6239
6240 if (nbsp_or_shy)
6241 {
6242 XSETINT (it->ctl_chars[0], escape_glyph);
6243 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6244 XSETINT (it->ctl_chars[1], c);
6245 ctl_len = 2;
6246 goto display_control;
6247 }
6248
6249 {
6250 char str[10];
6251 int len, i;
6252
6253 if (CHAR_BYTE8_P (c))
6254 /* Display \200 instead of \17777600. */
6255 c = CHAR_TO_BYTE8 (c);
6256 len = sprintf (str, "%03o", c);
6257
6258 XSETINT (it->ctl_chars[0], escape_glyph);
6259 for (i = 0; i < len; i++)
6260 XSETINT (it->ctl_chars[i + 1], str[i]);
6261 ctl_len = len + 1;
6262 }
6263
6264 display_control:
6265 /* Set up IT->dpvec and return first character from it. */
6266 it->dpvec_char_len = it->len;
6267 it->dpvec = it->ctl_chars;
6268 it->dpend = it->dpvec + ctl_len;
6269 it->current.dpvec_index = 0;
6270 it->dpvec_face_id = face_id;
6271 it->saved_face_id = it->face_id;
6272 it->method = GET_FROM_DISPLAY_VECTOR;
6273 it->ellipsis_p = 0;
6274 goto get_next;
6275 }
6276 it->char_to_display = c;
6277 }
6278 else if (success_p)
6279 {
6280 it->char_to_display = it->c;
6281 }
6282 }
6283
6284 /* Adjust face id for a multibyte character. There are no multibyte
6285 character in unibyte text. */
6286 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6287 && it->multibyte_p
6288 && success_p
6289 && FRAME_WINDOW_P (it->f))
6290 {
6291 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6292
6293 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6294 {
6295 /* Automatic composition with glyph-string. */
6296 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6297
6298 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6299 }
6300 else
6301 {
6302 EMACS_INT pos = (it->s ? -1
6303 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6304 : IT_CHARPOS (*it));
6305
6306 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6307 it->string);
6308 }
6309 }
6310
6311 done:
6312 /* Is this character the last one of a run of characters with
6313 box? If yes, set IT->end_of_box_run_p to 1. */
6314 if (it->face_box_p
6315 && it->s == NULL)
6316 {
6317 if (it->method == GET_FROM_STRING && it->sp)
6318 {
6319 int face_id = underlying_face_id (it);
6320 struct face *face = FACE_FROM_ID (it->f, face_id);
6321
6322 if (face)
6323 {
6324 if (face->box == FACE_NO_BOX)
6325 {
6326 /* If the box comes from face properties in a
6327 display string, check faces in that string. */
6328 int string_face_id = face_after_it_pos (it);
6329 it->end_of_box_run_p
6330 = (FACE_FROM_ID (it->f, string_face_id)->box
6331 == FACE_NO_BOX);
6332 }
6333 /* Otherwise, the box comes from the underlying face.
6334 If this is the last string character displayed, check
6335 the next buffer location. */
6336 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6337 && (it->current.overlay_string_index
6338 == it->n_overlay_strings - 1))
6339 {
6340 EMACS_INT ignore;
6341 int next_face_id;
6342 struct text_pos pos = it->current.pos;
6343 INC_TEXT_POS (pos, it->multibyte_p);
6344
6345 next_face_id = face_at_buffer_position
6346 (it->w, CHARPOS (pos), it->region_beg_charpos,
6347 it->region_end_charpos, &ignore,
6348 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6349 -1);
6350 it->end_of_box_run_p
6351 = (FACE_FROM_ID (it->f, next_face_id)->box
6352 == FACE_NO_BOX);
6353 }
6354 }
6355 }
6356 else
6357 {
6358 int face_id = face_after_it_pos (it);
6359 it->end_of_box_run_p
6360 = (face_id != it->face_id
6361 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6362 }
6363 }
6364
6365 /* Value is 0 if end of buffer or string reached. */
6366 return success_p;
6367 }
6368
6369
6370 /* Move IT to the next display element.
6371
6372 RESEAT_P non-zero means if called on a newline in buffer text,
6373 skip to the next visible line start.
6374
6375 Functions get_next_display_element and set_iterator_to_next are
6376 separate because I find this arrangement easier to handle than a
6377 get_next_display_element function that also increments IT's
6378 position. The way it is we can first look at an iterator's current
6379 display element, decide whether it fits on a line, and if it does,
6380 increment the iterator position. The other way around we probably
6381 would either need a flag indicating whether the iterator has to be
6382 incremented the next time, or we would have to implement a
6383 decrement position function which would not be easy to write. */
6384
6385 void
6386 set_iterator_to_next (struct it *it, int reseat_p)
6387 {
6388 /* Reset flags indicating start and end of a sequence of characters
6389 with box. Reset them at the start of this function because
6390 moving the iterator to a new position might set them. */
6391 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6392
6393 switch (it->method)
6394 {
6395 case GET_FROM_BUFFER:
6396 /* The current display element of IT is a character from
6397 current_buffer. Advance in the buffer, and maybe skip over
6398 invisible lines that are so because of selective display. */
6399 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6400 reseat_at_next_visible_line_start (it, 0);
6401 else if (it->cmp_it.id >= 0)
6402 {
6403 /* We are currently getting glyphs from a composition. */
6404 int i;
6405
6406 if (! it->bidi_p)
6407 {
6408 IT_CHARPOS (*it) += it->cmp_it.nchars;
6409 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6410 if (it->cmp_it.to < it->cmp_it.nglyphs)
6411 {
6412 it->cmp_it.from = it->cmp_it.to;
6413 }
6414 else
6415 {
6416 it->cmp_it.id = -1;
6417 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6418 IT_BYTEPOS (*it),
6419 it->end_charpos, Qnil);
6420 }
6421 }
6422 else if (! it->cmp_it.reversed_p)
6423 {
6424 /* Composition created while scanning forward. */
6425 /* Update IT's char/byte positions to point to the first
6426 character of the next grapheme cluster, or to the
6427 character visually after the current composition. */
6428 for (i = 0; i < it->cmp_it.nchars; i++)
6429 bidi_move_to_visually_next (&it->bidi_it);
6430 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6431 IT_CHARPOS (*it) = it->bidi_it.charpos;
6432
6433 if (it->cmp_it.to < it->cmp_it.nglyphs)
6434 {
6435 /* Proceed to the next grapheme cluster. */
6436 it->cmp_it.from = it->cmp_it.to;
6437 }
6438 else
6439 {
6440 /* No more grapheme clusters in this composition.
6441 Find the next stop position. */
6442 EMACS_INT stop = it->end_charpos;
6443 if (it->bidi_it.scan_dir < 0)
6444 /* Now we are scanning backward and don't know
6445 where to stop. */
6446 stop = -1;
6447 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6448 IT_BYTEPOS (*it), stop, Qnil);
6449 }
6450 }
6451 else
6452 {
6453 /* Composition created while scanning backward. */
6454 /* Update IT's char/byte positions to point to the last
6455 character of the previous grapheme cluster, or the
6456 character visually after the current composition. */
6457 for (i = 0; i < it->cmp_it.nchars; i++)
6458 bidi_move_to_visually_next (&it->bidi_it);
6459 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6460 IT_CHARPOS (*it) = it->bidi_it.charpos;
6461 if (it->cmp_it.from > 0)
6462 {
6463 /* Proceed to the previous grapheme cluster. */
6464 it->cmp_it.to = it->cmp_it.from;
6465 }
6466 else
6467 {
6468 /* No more grapheme clusters in this composition.
6469 Find the next stop position. */
6470 EMACS_INT stop = it->end_charpos;
6471 if (it->bidi_it.scan_dir < 0)
6472 /* Now we are scanning backward and don't know
6473 where to stop. */
6474 stop = -1;
6475 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6476 IT_BYTEPOS (*it), stop, Qnil);
6477 }
6478 }
6479 }
6480 else
6481 {
6482 xassert (it->len != 0);
6483
6484 if (!it->bidi_p)
6485 {
6486 IT_BYTEPOS (*it) += it->len;
6487 IT_CHARPOS (*it) += 1;
6488 }
6489 else
6490 {
6491 int prev_scan_dir = it->bidi_it.scan_dir;
6492 /* If this is a new paragraph, determine its base
6493 direction (a.k.a. its base embedding level). */
6494 if (it->bidi_it.new_paragraph)
6495 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6496 bidi_move_to_visually_next (&it->bidi_it);
6497 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6498 IT_CHARPOS (*it) = it->bidi_it.charpos;
6499 if (prev_scan_dir != it->bidi_it.scan_dir)
6500 {
6501 /* As the scan direction was changed, we must
6502 re-compute the stop position for composition. */
6503 EMACS_INT stop = it->end_charpos;
6504 if (it->bidi_it.scan_dir < 0)
6505 stop = -1;
6506 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6507 IT_BYTEPOS (*it), stop, Qnil);
6508 }
6509 }
6510 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6511 }
6512 break;
6513
6514 case GET_FROM_C_STRING:
6515 /* Current display element of IT is from a C string. */
6516 if (!it->bidi_p
6517 /* If the string position is beyond string's end, it means
6518 next_element_from_c_string is padding the string with
6519 blanks, in which case we bypass the bidi iterator,
6520 because it cannot deal with such virtual characters. */
6521 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6522 {
6523 IT_BYTEPOS (*it) += it->len;
6524 IT_CHARPOS (*it) += 1;
6525 }
6526 else
6527 {
6528 bidi_move_to_visually_next (&it->bidi_it);
6529 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6530 IT_CHARPOS (*it) = it->bidi_it.charpos;
6531 }
6532 break;
6533
6534 case GET_FROM_DISPLAY_VECTOR:
6535 /* Current display element of IT is from a display table entry.
6536 Advance in the display table definition. Reset it to null if
6537 end reached, and continue with characters from buffers/
6538 strings. */
6539 ++it->current.dpvec_index;
6540
6541 /* Restore face of the iterator to what they were before the
6542 display vector entry (these entries may contain faces). */
6543 it->face_id = it->saved_face_id;
6544
6545 if (it->dpvec + it->current.dpvec_index == it->dpend)
6546 {
6547 int recheck_faces = it->ellipsis_p;
6548
6549 if (it->s)
6550 it->method = GET_FROM_C_STRING;
6551 else if (STRINGP (it->string))
6552 it->method = GET_FROM_STRING;
6553 else
6554 {
6555 it->method = GET_FROM_BUFFER;
6556 it->object = it->w->buffer;
6557 }
6558
6559 it->dpvec = NULL;
6560 it->current.dpvec_index = -1;
6561
6562 /* Skip over characters which were displayed via IT->dpvec. */
6563 if (it->dpvec_char_len < 0)
6564 reseat_at_next_visible_line_start (it, 1);
6565 else if (it->dpvec_char_len > 0)
6566 {
6567 if (it->method == GET_FROM_STRING
6568 && it->n_overlay_strings > 0)
6569 it->ignore_overlay_strings_at_pos_p = 1;
6570 it->len = it->dpvec_char_len;
6571 set_iterator_to_next (it, reseat_p);
6572 }
6573
6574 /* Maybe recheck faces after display vector */
6575 if (recheck_faces)
6576 it->stop_charpos = IT_CHARPOS (*it);
6577 }
6578 break;
6579
6580 case GET_FROM_STRING:
6581 /* Current display element is a character from a Lisp string. */
6582 xassert (it->s == NULL && STRINGP (it->string));
6583 if (it->cmp_it.id >= 0)
6584 {
6585 int i;
6586
6587 if (! it->bidi_p)
6588 {
6589 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6590 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6591 if (it->cmp_it.to < it->cmp_it.nglyphs)
6592 it->cmp_it.from = it->cmp_it.to;
6593 else
6594 {
6595 it->cmp_it.id = -1;
6596 composition_compute_stop_pos (&it->cmp_it,
6597 IT_STRING_CHARPOS (*it),
6598 IT_STRING_BYTEPOS (*it),
6599 it->end_charpos, it->string);
6600 }
6601 }
6602 else if (! it->cmp_it.reversed_p)
6603 {
6604 for (i = 0; i < it->cmp_it.nchars; i++)
6605 bidi_move_to_visually_next (&it->bidi_it);
6606 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6607 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6608
6609 if (it->cmp_it.to < it->cmp_it.nglyphs)
6610 it->cmp_it.from = it->cmp_it.to;
6611 else
6612 {
6613 EMACS_INT stop = it->end_charpos;
6614 if (it->bidi_it.scan_dir < 0)
6615 stop = -1;
6616 composition_compute_stop_pos (&it->cmp_it,
6617 IT_STRING_CHARPOS (*it),
6618 IT_STRING_BYTEPOS (*it), stop,
6619 it->string);
6620 }
6621 }
6622 else
6623 {
6624 for (i = 0; i < it->cmp_it.nchars; i++)
6625 bidi_move_to_visually_next (&it->bidi_it);
6626 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6627 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6628 if (it->cmp_it.from > 0)
6629 it->cmp_it.to = it->cmp_it.from;
6630 else
6631 {
6632 EMACS_INT stop = it->end_charpos;
6633 if (it->bidi_it.scan_dir < 0)
6634 stop = -1;
6635 composition_compute_stop_pos (&it->cmp_it,
6636 IT_STRING_CHARPOS (*it),
6637 IT_STRING_BYTEPOS (*it), stop,
6638 it->string);
6639 }
6640 }
6641 }
6642 else
6643 {
6644 if (!it->bidi_p
6645 /* If the string position is beyond string's end, it
6646 means next_element_from_string is padding the string
6647 with blanks, in which case we bypass the bidi
6648 iterator, because it cannot deal with such virtual
6649 characters. */
6650 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
6651 {
6652 IT_STRING_BYTEPOS (*it) += it->len;
6653 IT_STRING_CHARPOS (*it) += 1;
6654 }
6655 else
6656 {
6657 int prev_scan_dir = it->bidi_it.scan_dir;
6658
6659 bidi_move_to_visually_next (&it->bidi_it);
6660 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6661 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6662 if (prev_scan_dir != it->bidi_it.scan_dir)
6663 {
6664 EMACS_INT stop = it->end_charpos;
6665
6666 if (it->bidi_it.scan_dir < 0)
6667 stop = -1;
6668 composition_compute_stop_pos (&it->cmp_it,
6669 IT_STRING_CHARPOS (*it),
6670 IT_STRING_BYTEPOS (*it), stop,
6671 it->string);
6672 }
6673 }
6674 }
6675
6676 consider_string_end:
6677
6678 if (it->current.overlay_string_index >= 0)
6679 {
6680 /* IT->string is an overlay string. Advance to the
6681 next, if there is one. */
6682 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6683 {
6684 it->ellipsis_p = 0;
6685 next_overlay_string (it);
6686 if (it->ellipsis_p)
6687 setup_for_ellipsis (it, 0);
6688 }
6689 }
6690 else
6691 {
6692 /* IT->string is not an overlay string. If we reached
6693 its end, and there is something on IT->stack, proceed
6694 with what is on the stack. This can be either another
6695 string, this time an overlay string, or a buffer. */
6696 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6697 && it->sp > 0)
6698 {
6699 pop_it (it);
6700 if (it->method == GET_FROM_STRING)
6701 goto consider_string_end;
6702 }
6703 }
6704 break;
6705
6706 case GET_FROM_IMAGE:
6707 case GET_FROM_STRETCH:
6708 /* The position etc with which we have to proceed are on
6709 the stack. The position may be at the end of a string,
6710 if the `display' property takes up the whole string. */
6711 xassert (it->sp > 0);
6712 pop_it (it);
6713 if (it->method == GET_FROM_STRING)
6714 goto consider_string_end;
6715 break;
6716
6717 default:
6718 /* There are no other methods defined, so this should be a bug. */
6719 abort ();
6720 }
6721
6722 xassert (it->method != GET_FROM_STRING
6723 || (STRINGP (it->string)
6724 && IT_STRING_CHARPOS (*it) >= 0));
6725 }
6726
6727 /* Load IT's display element fields with information about the next
6728 display element which comes from a display table entry or from the
6729 result of translating a control character to one of the forms `^C'
6730 or `\003'.
6731
6732 IT->dpvec holds the glyphs to return as characters.
6733 IT->saved_face_id holds the face id before the display vector--it
6734 is restored into IT->face_id in set_iterator_to_next. */
6735
6736 static int
6737 next_element_from_display_vector (struct it *it)
6738 {
6739 Lisp_Object gc;
6740
6741 /* Precondition. */
6742 xassert (it->dpvec && it->current.dpvec_index >= 0);
6743
6744 it->face_id = it->saved_face_id;
6745
6746 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6747 That seemed totally bogus - so I changed it... */
6748 gc = it->dpvec[it->current.dpvec_index];
6749
6750 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6751 {
6752 it->c = GLYPH_CODE_CHAR (gc);
6753 it->len = CHAR_BYTES (it->c);
6754
6755 /* The entry may contain a face id to use. Such a face id is
6756 the id of a Lisp face, not a realized face. A face id of
6757 zero means no face is specified. */
6758 if (it->dpvec_face_id >= 0)
6759 it->face_id = it->dpvec_face_id;
6760 else
6761 {
6762 int lface_id = GLYPH_CODE_FACE (gc);
6763 if (lface_id > 0)
6764 it->face_id = merge_faces (it->f, Qt, lface_id,
6765 it->saved_face_id);
6766 }
6767 }
6768 else
6769 /* Display table entry is invalid. Return a space. */
6770 it->c = ' ', it->len = 1;
6771
6772 /* Don't change position and object of the iterator here. They are
6773 still the values of the character that had this display table
6774 entry or was translated, and that's what we want. */
6775 it->what = IT_CHARACTER;
6776 return 1;
6777 }
6778
6779 /* Get the first element of string/buffer in the visual order, after
6780 being reseated to a new position in a string or a buffer. */
6781 static void
6782 get_visually_first_element (struct it *it)
6783 {
6784 int string_p = STRINGP (it->string) || it->s;
6785 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
6786 EMACS_INT bob = (string_p ? 0 : BEGV);
6787
6788 if (STRINGP (it->string))
6789 {
6790 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6791 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6792 }
6793 else
6794 {
6795 it->bidi_it.charpos = IT_CHARPOS (*it);
6796 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6797 }
6798
6799 if (it->bidi_it.charpos == eob)
6800 {
6801 /* Nothing to do, but reset the FIRST_ELT flag, like
6802 bidi_paragraph_init does, because we are not going to
6803 call it. */
6804 it->bidi_it.first_elt = 0;
6805 }
6806 else if (it->bidi_it.charpos == bob
6807 || (!string_p
6808 /* FIXME: Should support all Unicode line separators. */
6809 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6810 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6811 {
6812 /* If we are at the beginning of a line/string, we can produce
6813 the next element right away. */
6814 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6815 bidi_move_to_visually_next (&it->bidi_it);
6816 }
6817 else
6818 {
6819 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6820
6821 /* We need to prime the bidi iterator starting at the line's or
6822 string's beginning, before we will be able to produce the
6823 next element. */
6824 if (string_p)
6825 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6826 else
6827 {
6828 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6829 -1);
6830 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6831 }
6832 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6833 do
6834 {
6835 /* Now return to buffer/string position where we were asked
6836 to get the next display element, and produce that. */
6837 bidi_move_to_visually_next (&it->bidi_it);
6838 }
6839 while (it->bidi_it.bytepos != orig_bytepos
6840 && it->bidi_it.charpos < eob);
6841 }
6842
6843 /* Adjust IT's position information to where we ended up. */
6844 if (STRINGP (it->string))
6845 {
6846 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6847 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6848 }
6849 else
6850 {
6851 IT_CHARPOS (*it) = it->bidi_it.charpos;
6852 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6853 }
6854
6855 if (STRINGP (it->string) || !it->s)
6856 {
6857 EMACS_INT stop, charpos, bytepos;
6858
6859 if (STRINGP (it->string))
6860 {
6861 xassert (!it->s);
6862 stop = SCHARS (it->string);
6863 if (stop > it->end_charpos)
6864 stop = it->end_charpos;
6865 charpos = IT_STRING_CHARPOS (*it);
6866 bytepos = IT_STRING_BYTEPOS (*it);
6867 }
6868 else
6869 {
6870 stop = it->end_charpos;
6871 charpos = IT_CHARPOS (*it);
6872 bytepos = IT_BYTEPOS (*it);
6873 }
6874 if (it->bidi_it.scan_dir < 0)
6875 stop = -1;
6876 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
6877 it->string);
6878 }
6879 }
6880
6881 /* Load IT with the next display element from Lisp string IT->string.
6882 IT->current.string_pos is the current position within the string.
6883 If IT->current.overlay_string_index >= 0, the Lisp string is an
6884 overlay string. */
6885
6886 static int
6887 next_element_from_string (struct it *it)
6888 {
6889 struct text_pos position;
6890
6891 xassert (STRINGP (it->string));
6892 xassert (!it->bidi_p || it->string == it->bidi_it.string.lstring);
6893 xassert (IT_STRING_CHARPOS (*it) >= 0);
6894 position = it->current.string_pos;
6895
6896 /* With bidi reordering, the character to display might not be the
6897 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
6898 that we were reseat()ed to a new string, whose paragraph
6899 direction is not known. */
6900 if (it->bidi_p && it->bidi_it.first_elt)
6901 {
6902 get_visually_first_element (it);
6903 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
6904 }
6905
6906 /* Time to check for invisible text? */
6907 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
6908 {
6909 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
6910 {
6911 if (!(!it->bidi_p
6912 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6913 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
6914 {
6915 /* With bidi non-linear iteration, we could find
6916 ourselves far beyond the last computed stop_charpos,
6917 with several other stop positions in between that we
6918 missed. Scan them all now, in buffer's logical
6919 order, until we find and handle the last stop_charpos
6920 that precedes our current position. */
6921 handle_stop_backwards (it, it->stop_charpos);
6922 return GET_NEXT_DISPLAY_ELEMENT (it);
6923 }
6924 else
6925 {
6926 if (it->bidi_p)
6927 {
6928 /* Take note of the stop position we just moved
6929 across, for when we will move back across it. */
6930 it->prev_stop = it->stop_charpos;
6931 /* If we are at base paragraph embedding level, take
6932 note of the last stop position seen at this
6933 level. */
6934 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6935 it->base_level_stop = it->stop_charpos;
6936 }
6937 handle_stop (it);
6938
6939 /* Since a handler may have changed IT->method, we must
6940 recurse here. */
6941 return GET_NEXT_DISPLAY_ELEMENT (it);
6942 }
6943 }
6944 else if (it->bidi_p
6945 /* If we are before prev_stop, we may have overstepped
6946 on our way backwards a stop_pos, and if so, we need
6947 to handle that stop_pos. */
6948 && IT_STRING_CHARPOS (*it) < it->prev_stop
6949 /* We can sometimes back up for reasons that have nothing
6950 to do with bidi reordering. E.g., compositions. The
6951 code below is only needed when we are above the base
6952 embedding level, so test for that explicitly. */
6953 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
6954 {
6955 /* If we lost track of base_level_stop, we have no better place
6956 for handle_stop_backwards to start from than BEGV. This
6957 happens, e.g., when we were reseated to the previous
6958 screenful of text by vertical-motion. */
6959 if (it->base_level_stop <= 0
6960 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
6961 it->base_level_stop = 0;
6962 handle_stop_backwards (it, it->base_level_stop);
6963 return GET_NEXT_DISPLAY_ELEMENT (it);
6964 }
6965 }
6966
6967 if (it->current.overlay_string_index >= 0)
6968 {
6969 /* Get the next character from an overlay string. In overlay
6970 strings, There is no field width or padding with spaces to
6971 do. */
6972 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6973 {
6974 it->what = IT_EOB;
6975 return 0;
6976 }
6977 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6978 IT_STRING_BYTEPOS (*it),
6979 it->bidi_it.scan_dir < 0
6980 ? -1
6981 : SCHARS (it->string))
6982 && next_element_from_composition (it))
6983 {
6984 return 1;
6985 }
6986 else if (STRING_MULTIBYTE (it->string))
6987 {
6988 const unsigned char *s = (SDATA (it->string)
6989 + IT_STRING_BYTEPOS (*it));
6990 it->c = string_char_and_length (s, &it->len);
6991 }
6992 else
6993 {
6994 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6995 it->len = 1;
6996 }
6997 }
6998 else
6999 {
7000 /* Get the next character from a Lisp string that is not an
7001 overlay string. Such strings come from the mode line, for
7002 example. We may have to pad with spaces, or truncate the
7003 string. See also next_element_from_c_string. */
7004 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7005 {
7006 it->what = IT_EOB;
7007 return 0;
7008 }
7009 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7010 {
7011 /* Pad with spaces. */
7012 it->c = ' ', it->len = 1;
7013 CHARPOS (position) = BYTEPOS (position) = -1;
7014 }
7015 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7016 IT_STRING_BYTEPOS (*it),
7017 it->bidi_it.scan_dir < 0
7018 ? -1
7019 : it->string_nchars)
7020 && next_element_from_composition (it))
7021 {
7022 return 1;
7023 }
7024 else if (STRING_MULTIBYTE (it->string))
7025 {
7026 const unsigned char *s = (SDATA (it->string)
7027 + IT_STRING_BYTEPOS (*it));
7028 it->c = string_char_and_length (s, &it->len);
7029 }
7030 else
7031 {
7032 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7033 it->len = 1;
7034 }
7035 }
7036
7037 /* Record what we have and where it came from. */
7038 it->what = IT_CHARACTER;
7039 it->object = it->string;
7040 it->position = position;
7041 return 1;
7042 }
7043
7044
7045 /* Load IT with next display element from C string IT->s.
7046 IT->string_nchars is the maximum number of characters to return
7047 from the string. IT->end_charpos may be greater than
7048 IT->string_nchars when this function is called, in which case we
7049 may have to return padding spaces. Value is zero if end of string
7050 reached, including padding spaces. */
7051
7052 static int
7053 next_element_from_c_string (struct it *it)
7054 {
7055 int success_p = 1;
7056
7057 xassert (it->s);
7058 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7059 it->what = IT_CHARACTER;
7060 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7061 it->object = Qnil;
7062
7063 /* With bidi reordering, the character to display might not be the
7064 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7065 we were reseated to a new string, whose paragraph direction is
7066 not known. */
7067 if (it->bidi_p && it->bidi_it.first_elt)
7068 get_visually_first_element (it);
7069
7070 /* IT's position can be greater than IT->string_nchars in case a
7071 field width or precision has been specified when the iterator was
7072 initialized. */
7073 if (IT_CHARPOS (*it) >= it->end_charpos)
7074 {
7075 /* End of the game. */
7076 it->what = IT_EOB;
7077 success_p = 0;
7078 }
7079 else if (IT_CHARPOS (*it) >= it->string_nchars)
7080 {
7081 /* Pad with spaces. */
7082 it->c = ' ', it->len = 1;
7083 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7084 }
7085 else if (it->multibyte_p)
7086 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7087 else
7088 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7089
7090 return success_p;
7091 }
7092
7093
7094 /* Set up IT to return characters from an ellipsis, if appropriate.
7095 The definition of the ellipsis glyphs may come from a display table
7096 entry. This function fills IT with the first glyph from the
7097 ellipsis if an ellipsis is to be displayed. */
7098
7099 static int
7100 next_element_from_ellipsis (struct it *it)
7101 {
7102 if (it->selective_display_ellipsis_p)
7103 setup_for_ellipsis (it, it->len);
7104 else
7105 {
7106 /* The face at the current position may be different from the
7107 face we find after the invisible text. Remember what it
7108 was in IT->saved_face_id, and signal that it's there by
7109 setting face_before_selective_p. */
7110 it->saved_face_id = it->face_id;
7111 it->method = GET_FROM_BUFFER;
7112 it->object = it->w->buffer;
7113 reseat_at_next_visible_line_start (it, 1);
7114 it->face_before_selective_p = 1;
7115 }
7116
7117 return GET_NEXT_DISPLAY_ELEMENT (it);
7118 }
7119
7120
7121 /* Deliver an image display element. The iterator IT is already
7122 filled with image information (done in handle_display_prop). Value
7123 is always 1. */
7124
7125
7126 static int
7127 next_element_from_image (struct it *it)
7128 {
7129 it->what = IT_IMAGE;
7130 it->ignore_overlay_strings_at_pos_p = 0;
7131 return 1;
7132 }
7133
7134
7135 /* Fill iterator IT with next display element from a stretch glyph
7136 property. IT->object is the value of the text property. Value is
7137 always 1. */
7138
7139 static int
7140 next_element_from_stretch (struct it *it)
7141 {
7142 it->what = IT_STRETCH;
7143 return 1;
7144 }
7145
7146 /* Scan forward from CHARPOS in the current buffer/string, until we
7147 find a stop position > current IT's position. Then handle the stop
7148 position before that. This is called when we bump into a stop
7149 position while reordering bidirectional text. CHARPOS should be
7150 the last previously processed stop_pos (or BEGV/0, if none were
7151 processed yet) whose position is less that IT's current
7152 position. */
7153
7154 static void
7155 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7156 {
7157 int bufp = !STRINGP (it->string);
7158 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7159 struct display_pos save_current = it->current;
7160 struct text_pos save_position = it->position;
7161 struct text_pos pos1;
7162 EMACS_INT next_stop;
7163
7164 /* Scan in strict logical order. */
7165 it->bidi_p = 0;
7166 do
7167 {
7168 it->prev_stop = charpos;
7169 if (bufp)
7170 {
7171 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7172 reseat_1 (it, pos1, 0);
7173 }
7174 else
7175 it->current.string_pos = string_pos (charpos, it->string);
7176 compute_stop_pos (it);
7177 /* We must advance forward, right? */
7178 if (it->stop_charpos <= it->prev_stop)
7179 abort ();
7180 charpos = it->stop_charpos;
7181 }
7182 while (charpos <= where_we_are);
7183
7184 next_stop = it->stop_charpos;
7185 it->stop_charpos = it->prev_stop;
7186 it->bidi_p = 1;
7187 it->current = save_current;
7188 it->position = save_position;
7189 handle_stop (it);
7190 it->stop_charpos = next_stop;
7191 }
7192
7193 /* Load IT with the next display element from current_buffer. Value
7194 is zero if end of buffer reached. IT->stop_charpos is the next
7195 position at which to stop and check for text properties or buffer
7196 end. */
7197
7198 static int
7199 next_element_from_buffer (struct it *it)
7200 {
7201 int success_p = 1;
7202
7203 xassert (IT_CHARPOS (*it) >= BEGV);
7204 xassert (NILP (it->string) && !it->s);
7205 xassert (!it->bidi_p
7206 || (it->bidi_it.string.lstring == Qnil
7207 && it->bidi_it.string.s == NULL));
7208
7209 /* With bidi reordering, the character to display might not be the
7210 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7211 we were reseat()ed to a new buffer position, which is potentially
7212 a different paragraph. */
7213 if (it->bidi_p && it->bidi_it.first_elt)
7214 {
7215 get_visually_first_element (it);
7216 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7217 }
7218
7219 if (IT_CHARPOS (*it) >= it->stop_charpos)
7220 {
7221 if (IT_CHARPOS (*it) >= it->end_charpos)
7222 {
7223 int overlay_strings_follow_p;
7224
7225 /* End of the game, except when overlay strings follow that
7226 haven't been returned yet. */
7227 if (it->overlay_strings_at_end_processed_p)
7228 overlay_strings_follow_p = 0;
7229 else
7230 {
7231 it->overlay_strings_at_end_processed_p = 1;
7232 overlay_strings_follow_p = get_overlay_strings (it, 0);
7233 }
7234
7235 if (overlay_strings_follow_p)
7236 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7237 else
7238 {
7239 it->what = IT_EOB;
7240 it->position = it->current.pos;
7241 success_p = 0;
7242 }
7243 }
7244 else if (!(!it->bidi_p
7245 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7246 || IT_CHARPOS (*it) == it->stop_charpos))
7247 {
7248 /* With bidi non-linear iteration, we could find ourselves
7249 far beyond the last computed stop_charpos, with several
7250 other stop positions in between that we missed. Scan
7251 them all now, in buffer's logical order, until we find
7252 and handle the last stop_charpos that precedes our
7253 current position. */
7254 handle_stop_backwards (it, it->stop_charpos);
7255 return GET_NEXT_DISPLAY_ELEMENT (it);
7256 }
7257 else
7258 {
7259 if (it->bidi_p)
7260 {
7261 /* Take note of the stop position we just moved across,
7262 for when we will move back across it. */
7263 it->prev_stop = it->stop_charpos;
7264 /* If we are at base paragraph embedding level, take
7265 note of the last stop position seen at this
7266 level. */
7267 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7268 it->base_level_stop = it->stop_charpos;
7269 }
7270 handle_stop (it);
7271 return GET_NEXT_DISPLAY_ELEMENT (it);
7272 }
7273 }
7274 else if (it->bidi_p
7275 /* If we are before prev_stop, we may have overstepped on
7276 our way backwards a stop_pos, and if so, we need to
7277 handle that stop_pos. */
7278 && IT_CHARPOS (*it) < it->prev_stop
7279 /* We can sometimes back up for reasons that have nothing
7280 to do with bidi reordering. E.g., compositions. The
7281 code below is only needed when we are above the base
7282 embedding level, so test for that explicitly. */
7283 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7284 {
7285 /* If we lost track of base_level_stop, we have no better place
7286 for handle_stop_backwards to start from than BEGV. This
7287 happens, e.g., when we were reseated to the previous
7288 screenful of text by vertical-motion. */
7289 if (it->base_level_stop <= 0
7290 || IT_CHARPOS (*it) < it->base_level_stop)
7291 it->base_level_stop = BEGV;
7292 handle_stop_backwards (it, it->base_level_stop);
7293 return GET_NEXT_DISPLAY_ELEMENT (it);
7294 }
7295 else
7296 {
7297 /* No face changes, overlays etc. in sight, so just return a
7298 character from current_buffer. */
7299 unsigned char *p;
7300 EMACS_INT stop;
7301
7302 /* Maybe run the redisplay end trigger hook. Performance note:
7303 This doesn't seem to cost measurable time. */
7304 if (it->redisplay_end_trigger_charpos
7305 && it->glyph_row
7306 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7307 run_redisplay_end_trigger_hook (it);
7308
7309 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7310 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7311 stop)
7312 && next_element_from_composition (it))
7313 {
7314 return 1;
7315 }
7316
7317 /* Get the next character, maybe multibyte. */
7318 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7319 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7320 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7321 else
7322 it->c = *p, it->len = 1;
7323
7324 /* Record what we have and where it came from. */
7325 it->what = IT_CHARACTER;
7326 it->object = it->w->buffer;
7327 it->position = it->current.pos;
7328
7329 /* Normally we return the character found above, except when we
7330 really want to return an ellipsis for selective display. */
7331 if (it->selective)
7332 {
7333 if (it->c == '\n')
7334 {
7335 /* A value of selective > 0 means hide lines indented more
7336 than that number of columns. */
7337 if (it->selective > 0
7338 && IT_CHARPOS (*it) + 1 < ZV
7339 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7340 IT_BYTEPOS (*it) + 1,
7341 (double) it->selective)) /* iftc */
7342 {
7343 success_p = next_element_from_ellipsis (it);
7344 it->dpvec_char_len = -1;
7345 }
7346 }
7347 else if (it->c == '\r' && it->selective == -1)
7348 {
7349 /* A value of selective == -1 means that everything from the
7350 CR to the end of the line is invisible, with maybe an
7351 ellipsis displayed for it. */
7352 success_p = next_element_from_ellipsis (it);
7353 it->dpvec_char_len = -1;
7354 }
7355 }
7356 }
7357
7358 /* Value is zero if end of buffer reached. */
7359 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7360 return success_p;
7361 }
7362
7363
7364 /* Run the redisplay end trigger hook for IT. */
7365
7366 static void
7367 run_redisplay_end_trigger_hook (struct it *it)
7368 {
7369 Lisp_Object args[3];
7370
7371 /* IT->glyph_row should be non-null, i.e. we should be actually
7372 displaying something, or otherwise we should not run the hook. */
7373 xassert (it->glyph_row);
7374
7375 /* Set up hook arguments. */
7376 args[0] = Qredisplay_end_trigger_functions;
7377 args[1] = it->window;
7378 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7379 it->redisplay_end_trigger_charpos = 0;
7380
7381 /* Since we are *trying* to run these functions, don't try to run
7382 them again, even if they get an error. */
7383 it->w->redisplay_end_trigger = Qnil;
7384 Frun_hook_with_args (3, args);
7385
7386 /* Notice if it changed the face of the character we are on. */
7387 handle_face_prop (it);
7388 }
7389
7390
7391 /* Deliver a composition display element. Unlike the other
7392 next_element_from_XXX, this function is not registered in the array
7393 get_next_element[]. It is called from next_element_from_buffer and
7394 next_element_from_string when necessary. */
7395
7396 static int
7397 next_element_from_composition (struct it *it)
7398 {
7399 it->what = IT_COMPOSITION;
7400 it->len = it->cmp_it.nbytes;
7401 if (STRINGP (it->string))
7402 {
7403 if (it->c < 0)
7404 {
7405 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7406 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7407 return 0;
7408 }
7409 it->position = it->current.string_pos;
7410 it->object = it->string;
7411 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7412 IT_STRING_BYTEPOS (*it), it->string);
7413 }
7414 else
7415 {
7416 if (it->c < 0)
7417 {
7418 IT_CHARPOS (*it) += it->cmp_it.nchars;
7419 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7420 if (it->bidi_p)
7421 {
7422 if (it->bidi_it.new_paragraph)
7423 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7424 /* Resync the bidi iterator with IT's new position.
7425 FIXME: this doesn't support bidirectional text. */
7426 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7427 bidi_move_to_visually_next (&it->bidi_it);
7428 }
7429 return 0;
7430 }
7431 it->position = it->current.pos;
7432 it->object = it->w->buffer;
7433 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7434 IT_BYTEPOS (*it), Qnil);
7435 }
7436 return 1;
7437 }
7438
7439
7440 \f
7441 /***********************************************************************
7442 Moving an iterator without producing glyphs
7443 ***********************************************************************/
7444
7445 /* Check if iterator is at a position corresponding to a valid buffer
7446 position after some move_it_ call. */
7447
7448 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7449 ((it)->method == GET_FROM_STRING \
7450 ? IT_STRING_CHARPOS (*it) == 0 \
7451 : 1)
7452
7453
7454 /* Move iterator IT to a specified buffer or X position within one
7455 line on the display without producing glyphs.
7456
7457 OP should be a bit mask including some or all of these bits:
7458 MOVE_TO_X: Stop upon reaching x-position TO_X.
7459 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7460 Regardless of OP's value, stop upon reaching the end of the display line.
7461
7462 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7463 This means, in particular, that TO_X includes window's horizontal
7464 scroll amount.
7465
7466 The return value has several possible values that
7467 say what condition caused the scan to stop:
7468
7469 MOVE_POS_MATCH_OR_ZV
7470 - when TO_POS or ZV was reached.
7471
7472 MOVE_X_REACHED
7473 -when TO_X was reached before TO_POS or ZV were reached.
7474
7475 MOVE_LINE_CONTINUED
7476 - when we reached the end of the display area and the line must
7477 be continued.
7478
7479 MOVE_LINE_TRUNCATED
7480 - when we reached the end of the display area and the line is
7481 truncated.
7482
7483 MOVE_NEWLINE_OR_CR
7484 - when we stopped at a line end, i.e. a newline or a CR and selective
7485 display is on. */
7486
7487 static enum move_it_result
7488 move_it_in_display_line_to (struct it *it,
7489 EMACS_INT to_charpos, int to_x,
7490 enum move_operation_enum op)
7491 {
7492 enum move_it_result result = MOVE_UNDEFINED;
7493 struct glyph_row *saved_glyph_row;
7494 struct it wrap_it, atpos_it, atx_it;
7495 int may_wrap = 0;
7496 enum it_method prev_method = it->method;
7497 EMACS_INT prev_pos = IT_CHARPOS (*it);
7498
7499 /* Don't produce glyphs in produce_glyphs. */
7500 saved_glyph_row = it->glyph_row;
7501 it->glyph_row = NULL;
7502
7503 /* Use wrap_it to save a copy of IT wherever a word wrap could
7504 occur. Use atpos_it to save a copy of IT at the desired buffer
7505 position, if found, so that we can scan ahead and check if the
7506 word later overshoots the window edge. Use atx_it similarly, for
7507 pixel positions. */
7508 wrap_it.sp = -1;
7509 atpos_it.sp = -1;
7510 atx_it.sp = -1;
7511
7512 #define BUFFER_POS_REACHED_P() \
7513 ((op & MOVE_TO_POS) != 0 \
7514 && BUFFERP (it->object) \
7515 && (IT_CHARPOS (*it) == to_charpos \
7516 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7517 && (it->method == GET_FROM_BUFFER \
7518 || (it->method == GET_FROM_DISPLAY_VECTOR \
7519 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7520
7521 /* If there's a line-/wrap-prefix, handle it. */
7522 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7523 && it->current_y < it->last_visible_y)
7524 handle_line_prefix (it);
7525
7526 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7527 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7528
7529 while (1)
7530 {
7531 int x, i, ascent = 0, descent = 0;
7532
7533 /* Utility macro to reset an iterator with x, ascent, and descent. */
7534 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7535 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7536 (IT)->max_descent = descent)
7537
7538 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7539 glyph). */
7540 if ((op & MOVE_TO_POS) != 0
7541 && BUFFERP (it->object)
7542 && it->method == GET_FROM_BUFFER
7543 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7544 || (it->bidi_p
7545 && (prev_method == GET_FROM_IMAGE
7546 || prev_method == GET_FROM_STRETCH)
7547 /* Passed TO_CHARPOS from left to right. */
7548 && ((prev_pos < to_charpos
7549 && IT_CHARPOS (*it) > to_charpos)
7550 /* Passed TO_CHARPOS from right to left. */
7551 || (prev_pos > to_charpos
7552 && IT_CHARPOS (*it) < to_charpos)))))
7553 {
7554 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7555 {
7556 result = MOVE_POS_MATCH_OR_ZV;
7557 break;
7558 }
7559 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7560 /* If wrap_it is valid, the current position might be in a
7561 word that is wrapped. So, save the iterator in
7562 atpos_it and continue to see if wrapping happens. */
7563 atpos_it = *it;
7564 }
7565
7566 prev_method = it->method;
7567 if (it->method == GET_FROM_BUFFER)
7568 prev_pos = IT_CHARPOS (*it);
7569 /* Stop when ZV reached.
7570 We used to stop here when TO_CHARPOS reached as well, but that is
7571 too soon if this glyph does not fit on this line. So we handle it
7572 explicitly below. */
7573 if (!get_next_display_element (it))
7574 {
7575 result = MOVE_POS_MATCH_OR_ZV;
7576 break;
7577 }
7578
7579 if (it->line_wrap == TRUNCATE)
7580 {
7581 if (BUFFER_POS_REACHED_P ())
7582 {
7583 result = MOVE_POS_MATCH_OR_ZV;
7584 break;
7585 }
7586 }
7587 else
7588 {
7589 if (it->line_wrap == WORD_WRAP)
7590 {
7591 if (IT_DISPLAYING_WHITESPACE (it))
7592 may_wrap = 1;
7593 else if (may_wrap)
7594 {
7595 /* We have reached a glyph that follows one or more
7596 whitespace characters. If the position is
7597 already found, we are done. */
7598 if (atpos_it.sp >= 0)
7599 {
7600 *it = atpos_it;
7601 result = MOVE_POS_MATCH_OR_ZV;
7602 goto done;
7603 }
7604 if (atx_it.sp >= 0)
7605 {
7606 *it = atx_it;
7607 result = MOVE_X_REACHED;
7608 goto done;
7609 }
7610 /* Otherwise, we can wrap here. */
7611 wrap_it = *it;
7612 may_wrap = 0;
7613 }
7614 }
7615 }
7616
7617 /* Remember the line height for the current line, in case
7618 the next element doesn't fit on the line. */
7619 ascent = it->max_ascent;
7620 descent = it->max_descent;
7621
7622 /* The call to produce_glyphs will get the metrics of the
7623 display element IT is loaded with. Record the x-position
7624 before this display element, in case it doesn't fit on the
7625 line. */
7626 x = it->current_x;
7627
7628 PRODUCE_GLYPHS (it);
7629
7630 if (it->area != TEXT_AREA)
7631 {
7632 set_iterator_to_next (it, 1);
7633 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7634 SET_TEXT_POS (this_line_min_pos,
7635 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7636 continue;
7637 }
7638
7639 /* The number of glyphs we get back in IT->nglyphs will normally
7640 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7641 character on a terminal frame, or (iii) a line end. For the
7642 second case, IT->nglyphs - 1 padding glyphs will be present.
7643 (On X frames, there is only one glyph produced for a
7644 composite character.)
7645
7646 The behavior implemented below means, for continuation lines,
7647 that as many spaces of a TAB as fit on the current line are
7648 displayed there. For terminal frames, as many glyphs of a
7649 multi-glyph character are displayed in the current line, too.
7650 This is what the old redisplay code did, and we keep it that
7651 way. Under X, the whole shape of a complex character must
7652 fit on the line or it will be completely displayed in the
7653 next line.
7654
7655 Note that both for tabs and padding glyphs, all glyphs have
7656 the same width. */
7657 if (it->nglyphs)
7658 {
7659 /* More than one glyph or glyph doesn't fit on line. All
7660 glyphs have the same width. */
7661 int single_glyph_width = it->pixel_width / it->nglyphs;
7662 int new_x;
7663 int x_before_this_char = x;
7664 int hpos_before_this_char = it->hpos;
7665
7666 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7667 {
7668 new_x = x + single_glyph_width;
7669
7670 /* We want to leave anything reaching TO_X to the caller. */
7671 if ((op & MOVE_TO_X) && new_x > to_x)
7672 {
7673 if (BUFFER_POS_REACHED_P ())
7674 {
7675 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7676 goto buffer_pos_reached;
7677 if (atpos_it.sp < 0)
7678 {
7679 atpos_it = *it;
7680 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7681 }
7682 }
7683 else
7684 {
7685 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7686 {
7687 it->current_x = x;
7688 result = MOVE_X_REACHED;
7689 break;
7690 }
7691 if (atx_it.sp < 0)
7692 {
7693 atx_it = *it;
7694 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7695 }
7696 }
7697 }
7698
7699 if (/* Lines are continued. */
7700 it->line_wrap != TRUNCATE
7701 && (/* And glyph doesn't fit on the line. */
7702 new_x > it->last_visible_x
7703 /* Or it fits exactly and we're on a window
7704 system frame. */
7705 || (new_x == it->last_visible_x
7706 && FRAME_WINDOW_P (it->f))))
7707 {
7708 if (/* IT->hpos == 0 means the very first glyph
7709 doesn't fit on the line, e.g. a wide image. */
7710 it->hpos == 0
7711 || (new_x == it->last_visible_x
7712 && FRAME_WINDOW_P (it->f)))
7713 {
7714 ++it->hpos;
7715 it->current_x = new_x;
7716
7717 /* The character's last glyph just barely fits
7718 in this row. */
7719 if (i == it->nglyphs - 1)
7720 {
7721 /* If this is the destination position,
7722 return a position *before* it in this row,
7723 now that we know it fits in this row. */
7724 if (BUFFER_POS_REACHED_P ())
7725 {
7726 if (it->line_wrap != WORD_WRAP
7727 || wrap_it.sp < 0)
7728 {
7729 it->hpos = hpos_before_this_char;
7730 it->current_x = x_before_this_char;
7731 result = MOVE_POS_MATCH_OR_ZV;
7732 break;
7733 }
7734 if (it->line_wrap == WORD_WRAP
7735 && atpos_it.sp < 0)
7736 {
7737 atpos_it = *it;
7738 atpos_it.current_x = x_before_this_char;
7739 atpos_it.hpos = hpos_before_this_char;
7740 }
7741 }
7742
7743 set_iterator_to_next (it, 1);
7744 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7745 SET_TEXT_POS (this_line_min_pos,
7746 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7747 /* On graphical terminals, newlines may
7748 "overflow" into the fringe if
7749 overflow-newline-into-fringe is non-nil.
7750 On text-only terminals, newlines may
7751 overflow into the last glyph on the
7752 display line.*/
7753 if (!FRAME_WINDOW_P (it->f)
7754 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7755 {
7756 if (!get_next_display_element (it))
7757 {
7758 result = MOVE_POS_MATCH_OR_ZV;
7759 break;
7760 }
7761 if (BUFFER_POS_REACHED_P ())
7762 {
7763 if (ITERATOR_AT_END_OF_LINE_P (it))
7764 result = MOVE_POS_MATCH_OR_ZV;
7765 else
7766 result = MOVE_LINE_CONTINUED;
7767 break;
7768 }
7769 if (ITERATOR_AT_END_OF_LINE_P (it))
7770 {
7771 result = MOVE_NEWLINE_OR_CR;
7772 break;
7773 }
7774 }
7775 }
7776 }
7777 else
7778 IT_RESET_X_ASCENT_DESCENT (it);
7779
7780 if (wrap_it.sp >= 0)
7781 {
7782 *it = wrap_it;
7783 atpos_it.sp = -1;
7784 atx_it.sp = -1;
7785 }
7786
7787 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7788 IT_CHARPOS (*it)));
7789 result = MOVE_LINE_CONTINUED;
7790 break;
7791 }
7792
7793 if (BUFFER_POS_REACHED_P ())
7794 {
7795 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7796 goto buffer_pos_reached;
7797 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7798 {
7799 atpos_it = *it;
7800 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7801 }
7802 }
7803
7804 if (new_x > it->first_visible_x)
7805 {
7806 /* Glyph is visible. Increment number of glyphs that
7807 would be displayed. */
7808 ++it->hpos;
7809 }
7810 }
7811
7812 if (result != MOVE_UNDEFINED)
7813 break;
7814 }
7815 else if (BUFFER_POS_REACHED_P ())
7816 {
7817 buffer_pos_reached:
7818 IT_RESET_X_ASCENT_DESCENT (it);
7819 result = MOVE_POS_MATCH_OR_ZV;
7820 break;
7821 }
7822 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7823 {
7824 /* Stop when TO_X specified and reached. This check is
7825 necessary here because of lines consisting of a line end,
7826 only. The line end will not produce any glyphs and we
7827 would never get MOVE_X_REACHED. */
7828 xassert (it->nglyphs == 0);
7829 result = MOVE_X_REACHED;
7830 break;
7831 }
7832
7833 /* Is this a line end? If yes, we're done. */
7834 if (ITERATOR_AT_END_OF_LINE_P (it))
7835 {
7836 result = MOVE_NEWLINE_OR_CR;
7837 break;
7838 }
7839
7840 if (it->method == GET_FROM_BUFFER)
7841 prev_pos = IT_CHARPOS (*it);
7842 /* The current display element has been consumed. Advance
7843 to the next. */
7844 set_iterator_to_next (it, 1);
7845 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7846 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7847
7848 /* Stop if lines are truncated and IT's current x-position is
7849 past the right edge of the window now. */
7850 if (it->line_wrap == TRUNCATE
7851 && it->current_x >= it->last_visible_x)
7852 {
7853 if (!FRAME_WINDOW_P (it->f)
7854 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7855 {
7856 if (!get_next_display_element (it)
7857 || BUFFER_POS_REACHED_P ())
7858 {
7859 result = MOVE_POS_MATCH_OR_ZV;
7860 break;
7861 }
7862 if (ITERATOR_AT_END_OF_LINE_P (it))
7863 {
7864 result = MOVE_NEWLINE_OR_CR;
7865 break;
7866 }
7867 }
7868 result = MOVE_LINE_TRUNCATED;
7869 break;
7870 }
7871 #undef IT_RESET_X_ASCENT_DESCENT
7872 }
7873
7874 #undef BUFFER_POS_REACHED_P
7875
7876 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7877 restore the saved iterator. */
7878 if (atpos_it.sp >= 0)
7879 *it = atpos_it;
7880 else if (atx_it.sp >= 0)
7881 *it = atx_it;
7882
7883 done:
7884
7885 /* Restore the iterator settings altered at the beginning of this
7886 function. */
7887 it->glyph_row = saved_glyph_row;
7888 return result;
7889 }
7890
7891 /* For external use. */
7892 void
7893 move_it_in_display_line (struct it *it,
7894 EMACS_INT to_charpos, int to_x,
7895 enum move_operation_enum op)
7896 {
7897 if (it->line_wrap == WORD_WRAP
7898 && (op & MOVE_TO_X))
7899 {
7900 struct it save_it = *it;
7901 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7902 /* When word-wrap is on, TO_X may lie past the end
7903 of a wrapped line. Then it->current is the
7904 character on the next line, so backtrack to the
7905 space before the wrap point. */
7906 if (skip == MOVE_LINE_CONTINUED)
7907 {
7908 int prev_x = max (it->current_x - 1, 0);
7909 *it = save_it;
7910 move_it_in_display_line_to
7911 (it, -1, prev_x, MOVE_TO_X);
7912 }
7913 }
7914 else
7915 move_it_in_display_line_to (it, to_charpos, to_x, op);
7916 }
7917
7918
7919 /* Move IT forward until it satisfies one or more of the criteria in
7920 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7921
7922 OP is a bit-mask that specifies where to stop, and in particular,
7923 which of those four position arguments makes a difference. See the
7924 description of enum move_operation_enum.
7925
7926 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7927 screen line, this function will set IT to the next position >
7928 TO_CHARPOS. */
7929
7930 void
7931 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7932 {
7933 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7934 int line_height, line_start_x = 0, reached = 0;
7935
7936 for (;;)
7937 {
7938 if (op & MOVE_TO_VPOS)
7939 {
7940 /* If no TO_CHARPOS and no TO_X specified, stop at the
7941 start of the line TO_VPOS. */
7942 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7943 {
7944 if (it->vpos == to_vpos)
7945 {
7946 reached = 1;
7947 break;
7948 }
7949 else
7950 skip = move_it_in_display_line_to (it, -1, -1, 0);
7951 }
7952 else
7953 {
7954 /* TO_VPOS >= 0 means stop at TO_X in the line at
7955 TO_VPOS, or at TO_POS, whichever comes first. */
7956 if (it->vpos == to_vpos)
7957 {
7958 reached = 2;
7959 break;
7960 }
7961
7962 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7963
7964 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7965 {
7966 reached = 3;
7967 break;
7968 }
7969 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7970 {
7971 /* We have reached TO_X but not in the line we want. */
7972 skip = move_it_in_display_line_to (it, to_charpos,
7973 -1, MOVE_TO_POS);
7974 if (skip == MOVE_POS_MATCH_OR_ZV)
7975 {
7976 reached = 4;
7977 break;
7978 }
7979 }
7980 }
7981 }
7982 else if (op & MOVE_TO_Y)
7983 {
7984 struct it it_backup;
7985
7986 if (it->line_wrap == WORD_WRAP)
7987 it_backup = *it;
7988
7989 /* TO_Y specified means stop at TO_X in the line containing
7990 TO_Y---or at TO_CHARPOS if this is reached first. The
7991 problem is that we can't really tell whether the line
7992 contains TO_Y before we have completely scanned it, and
7993 this may skip past TO_X. What we do is to first scan to
7994 TO_X.
7995
7996 If TO_X is not specified, use a TO_X of zero. The reason
7997 is to make the outcome of this function more predictable.
7998 If we didn't use TO_X == 0, we would stop at the end of
7999 the line which is probably not what a caller would expect
8000 to happen. */
8001 skip = move_it_in_display_line_to
8002 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8003 (MOVE_TO_X | (op & MOVE_TO_POS)));
8004
8005 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8006 if (skip == MOVE_POS_MATCH_OR_ZV)
8007 reached = 5;
8008 else if (skip == MOVE_X_REACHED)
8009 {
8010 /* If TO_X was reached, we want to know whether TO_Y is
8011 in the line. We know this is the case if the already
8012 scanned glyphs make the line tall enough. Otherwise,
8013 we must check by scanning the rest of the line. */
8014 line_height = it->max_ascent + it->max_descent;
8015 if (to_y >= it->current_y
8016 && to_y < it->current_y + line_height)
8017 {
8018 reached = 6;
8019 break;
8020 }
8021 it_backup = *it;
8022 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8023 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8024 op & MOVE_TO_POS);
8025 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8026 line_height = it->max_ascent + it->max_descent;
8027 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8028
8029 if (to_y >= it->current_y
8030 && to_y < it->current_y + line_height)
8031 {
8032 /* If TO_Y is in this line and TO_X was reached
8033 above, we scanned too far. We have to restore
8034 IT's settings to the ones before skipping. */
8035 *it = it_backup;
8036 reached = 6;
8037 }
8038 else
8039 {
8040 skip = skip2;
8041 if (skip == MOVE_POS_MATCH_OR_ZV)
8042 reached = 7;
8043 }
8044 }
8045 else
8046 {
8047 /* Check whether TO_Y is in this line. */
8048 line_height = it->max_ascent + it->max_descent;
8049 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8050
8051 if (to_y >= it->current_y
8052 && to_y < it->current_y + line_height)
8053 {
8054 /* When word-wrap is on, TO_X may lie past the end
8055 of a wrapped line. Then it->current is the
8056 character on the next line, so backtrack to the
8057 space before the wrap point. */
8058 if (skip == MOVE_LINE_CONTINUED
8059 && it->line_wrap == WORD_WRAP)
8060 {
8061 int prev_x = max (it->current_x - 1, 0);
8062 *it = it_backup;
8063 skip = move_it_in_display_line_to
8064 (it, -1, prev_x, MOVE_TO_X);
8065 }
8066 reached = 6;
8067 }
8068 }
8069
8070 if (reached)
8071 break;
8072 }
8073 else if (BUFFERP (it->object)
8074 && (it->method == GET_FROM_BUFFER
8075 || it->method == GET_FROM_STRETCH)
8076 && IT_CHARPOS (*it) >= to_charpos)
8077 skip = MOVE_POS_MATCH_OR_ZV;
8078 else
8079 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8080
8081 switch (skip)
8082 {
8083 case MOVE_POS_MATCH_OR_ZV:
8084 reached = 8;
8085 goto out;
8086
8087 case MOVE_NEWLINE_OR_CR:
8088 set_iterator_to_next (it, 1);
8089 it->continuation_lines_width = 0;
8090 break;
8091
8092 case MOVE_LINE_TRUNCATED:
8093 it->continuation_lines_width = 0;
8094 reseat_at_next_visible_line_start (it, 0);
8095 if ((op & MOVE_TO_POS) != 0
8096 && IT_CHARPOS (*it) > to_charpos)
8097 {
8098 reached = 9;
8099 goto out;
8100 }
8101 break;
8102
8103 case MOVE_LINE_CONTINUED:
8104 /* For continued lines ending in a tab, some of the glyphs
8105 associated with the tab are displayed on the current
8106 line. Since it->current_x does not include these glyphs,
8107 we use it->last_visible_x instead. */
8108 if (it->c == '\t')
8109 {
8110 it->continuation_lines_width += it->last_visible_x;
8111 /* When moving by vpos, ensure that the iterator really
8112 advances to the next line (bug#847, bug#969). Fixme:
8113 do we need to do this in other circumstances? */
8114 if (it->current_x != it->last_visible_x
8115 && (op & MOVE_TO_VPOS)
8116 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8117 {
8118 line_start_x = it->current_x + it->pixel_width
8119 - it->last_visible_x;
8120 set_iterator_to_next (it, 0);
8121 }
8122 }
8123 else
8124 it->continuation_lines_width += it->current_x;
8125 break;
8126
8127 default:
8128 abort ();
8129 }
8130
8131 /* Reset/increment for the next run. */
8132 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8133 it->current_x = line_start_x;
8134 line_start_x = 0;
8135 it->hpos = 0;
8136 it->current_y += it->max_ascent + it->max_descent;
8137 ++it->vpos;
8138 last_height = it->max_ascent + it->max_descent;
8139 last_max_ascent = it->max_ascent;
8140 it->max_ascent = it->max_descent = 0;
8141 }
8142
8143 out:
8144
8145 /* On text terminals, we may stop at the end of a line in the middle
8146 of a multi-character glyph. If the glyph itself is continued,
8147 i.e. it is actually displayed on the next line, don't treat this
8148 stopping point as valid; move to the next line instead (unless
8149 that brings us offscreen). */
8150 if (!FRAME_WINDOW_P (it->f)
8151 && op & MOVE_TO_POS
8152 && IT_CHARPOS (*it) == to_charpos
8153 && it->what == IT_CHARACTER
8154 && it->nglyphs > 1
8155 && it->line_wrap == WINDOW_WRAP
8156 && it->current_x == it->last_visible_x - 1
8157 && it->c != '\n'
8158 && it->c != '\t'
8159 && it->vpos < XFASTINT (it->w->window_end_vpos))
8160 {
8161 it->continuation_lines_width += it->current_x;
8162 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8163 it->current_y += it->max_ascent + it->max_descent;
8164 ++it->vpos;
8165 last_height = it->max_ascent + it->max_descent;
8166 last_max_ascent = it->max_ascent;
8167 }
8168
8169 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8170 }
8171
8172
8173 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8174
8175 If DY > 0, move IT backward at least that many pixels. DY = 0
8176 means move IT backward to the preceding line start or BEGV. This
8177 function may move over more than DY pixels if IT->current_y - DY
8178 ends up in the middle of a line; in this case IT->current_y will be
8179 set to the top of the line moved to. */
8180
8181 void
8182 move_it_vertically_backward (struct it *it, int dy)
8183 {
8184 int nlines, h;
8185 struct it it2, it3;
8186 EMACS_INT start_pos;
8187
8188 move_further_back:
8189 xassert (dy >= 0);
8190
8191 start_pos = IT_CHARPOS (*it);
8192
8193 /* Estimate how many newlines we must move back. */
8194 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8195
8196 /* Set the iterator's position that many lines back. */
8197 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8198 back_to_previous_visible_line_start (it);
8199
8200 /* Reseat the iterator here. When moving backward, we don't want
8201 reseat to skip forward over invisible text, set up the iterator
8202 to deliver from overlay strings at the new position etc. So,
8203 use reseat_1 here. */
8204 reseat_1 (it, it->current.pos, 1);
8205
8206 /* We are now surely at a line start. */
8207 it->current_x = it->hpos = 0;
8208 it->continuation_lines_width = 0;
8209
8210 /* Move forward and see what y-distance we moved. First move to the
8211 start of the next line so that we get its height. We need this
8212 height to be able to tell whether we reached the specified
8213 y-distance. */
8214 it2 = *it;
8215 it2.max_ascent = it2.max_descent = 0;
8216 do
8217 {
8218 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8219 MOVE_TO_POS | MOVE_TO_VPOS);
8220 }
8221 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8222 xassert (IT_CHARPOS (*it) >= BEGV);
8223 it3 = it2;
8224
8225 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8226 xassert (IT_CHARPOS (*it) >= BEGV);
8227 /* H is the actual vertical distance from the position in *IT
8228 and the starting position. */
8229 h = it2.current_y - it->current_y;
8230 /* NLINES is the distance in number of lines. */
8231 nlines = it2.vpos - it->vpos;
8232
8233 /* Correct IT's y and vpos position
8234 so that they are relative to the starting point. */
8235 it->vpos -= nlines;
8236 it->current_y -= h;
8237
8238 if (dy == 0)
8239 {
8240 /* DY == 0 means move to the start of the screen line. The
8241 value of nlines is > 0 if continuation lines were involved. */
8242 if (nlines > 0)
8243 move_it_by_lines (it, nlines);
8244 }
8245 else
8246 {
8247 /* The y-position we try to reach, relative to *IT.
8248 Note that H has been subtracted in front of the if-statement. */
8249 int target_y = it->current_y + h - dy;
8250 int y0 = it3.current_y;
8251 int y1 = line_bottom_y (&it3);
8252 int line_height = y1 - y0;
8253
8254 /* If we did not reach target_y, try to move further backward if
8255 we can. If we moved too far backward, try to move forward. */
8256 if (target_y < it->current_y
8257 /* This is heuristic. In a window that's 3 lines high, with
8258 a line height of 13 pixels each, recentering with point
8259 on the bottom line will try to move -39/2 = 19 pixels
8260 backward. Try to avoid moving into the first line. */
8261 && (it->current_y - target_y
8262 > min (window_box_height (it->w), line_height * 2 / 3))
8263 && IT_CHARPOS (*it) > BEGV)
8264 {
8265 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8266 target_y - it->current_y));
8267 dy = it->current_y - target_y;
8268 goto move_further_back;
8269 }
8270 else if (target_y >= it->current_y + line_height
8271 && IT_CHARPOS (*it) < ZV)
8272 {
8273 /* Should move forward by at least one line, maybe more.
8274
8275 Note: Calling move_it_by_lines can be expensive on
8276 terminal frames, where compute_motion is used (via
8277 vmotion) to do the job, when there are very long lines
8278 and truncate-lines is nil. That's the reason for
8279 treating terminal frames specially here. */
8280
8281 if (!FRAME_WINDOW_P (it->f))
8282 move_it_vertically (it, target_y - (it->current_y + line_height));
8283 else
8284 {
8285 do
8286 {
8287 move_it_by_lines (it, 1);
8288 }
8289 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8290 }
8291 }
8292 }
8293 }
8294
8295
8296 /* Move IT by a specified amount of pixel lines DY. DY negative means
8297 move backwards. DY = 0 means move to start of screen line. At the
8298 end, IT will be on the start of a screen line. */
8299
8300 void
8301 move_it_vertically (struct it *it, int dy)
8302 {
8303 if (dy <= 0)
8304 move_it_vertically_backward (it, -dy);
8305 else
8306 {
8307 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8308 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8309 MOVE_TO_POS | MOVE_TO_Y);
8310 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8311
8312 /* If buffer ends in ZV without a newline, move to the start of
8313 the line to satisfy the post-condition. */
8314 if (IT_CHARPOS (*it) == ZV
8315 && ZV > BEGV
8316 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8317 move_it_by_lines (it, 0);
8318 }
8319 }
8320
8321
8322 /* Move iterator IT past the end of the text line it is in. */
8323
8324 void
8325 move_it_past_eol (struct it *it)
8326 {
8327 enum move_it_result rc;
8328
8329 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8330 if (rc == MOVE_NEWLINE_OR_CR)
8331 set_iterator_to_next (it, 0);
8332 }
8333
8334
8335 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8336 negative means move up. DVPOS == 0 means move to the start of the
8337 screen line.
8338
8339 Optimization idea: If we would know that IT->f doesn't use
8340 a face with proportional font, we could be faster for
8341 truncate-lines nil. */
8342
8343 void
8344 move_it_by_lines (struct it *it, int dvpos)
8345 {
8346
8347 /* The commented-out optimization uses vmotion on terminals. This
8348 gives bad results, because elements like it->what, on which
8349 callers such as pos_visible_p rely, aren't updated. */
8350 /* struct position pos;
8351 if (!FRAME_WINDOW_P (it->f))
8352 {
8353 struct text_pos textpos;
8354
8355 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8356 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8357 reseat (it, textpos, 1);
8358 it->vpos += pos.vpos;
8359 it->current_y += pos.vpos;
8360 }
8361 else */
8362
8363 if (dvpos == 0)
8364 {
8365 /* DVPOS == 0 means move to the start of the screen line. */
8366 move_it_vertically_backward (it, 0);
8367 xassert (it->current_x == 0 && it->hpos == 0);
8368 /* Let next call to line_bottom_y calculate real line height */
8369 last_height = 0;
8370 }
8371 else if (dvpos > 0)
8372 {
8373 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8374 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8375 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8376 }
8377 else
8378 {
8379 struct it it2;
8380 EMACS_INT start_charpos, i;
8381
8382 /* Start at the beginning of the screen line containing IT's
8383 position. This may actually move vertically backwards,
8384 in case of overlays, so adjust dvpos accordingly. */
8385 dvpos += it->vpos;
8386 move_it_vertically_backward (it, 0);
8387 dvpos -= it->vpos;
8388
8389 /* Go back -DVPOS visible lines and reseat the iterator there. */
8390 start_charpos = IT_CHARPOS (*it);
8391 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8392 back_to_previous_visible_line_start (it);
8393 reseat (it, it->current.pos, 1);
8394
8395 /* Move further back if we end up in a string or an image. */
8396 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8397 {
8398 /* First try to move to start of display line. */
8399 dvpos += it->vpos;
8400 move_it_vertically_backward (it, 0);
8401 dvpos -= it->vpos;
8402 if (IT_POS_VALID_AFTER_MOVE_P (it))
8403 break;
8404 /* If start of line is still in string or image,
8405 move further back. */
8406 back_to_previous_visible_line_start (it);
8407 reseat (it, it->current.pos, 1);
8408 dvpos--;
8409 }
8410
8411 it->current_x = it->hpos = 0;
8412
8413 /* Above call may have moved too far if continuation lines
8414 are involved. Scan forward and see if it did. */
8415 it2 = *it;
8416 it2.vpos = it2.current_y = 0;
8417 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8418 it->vpos -= it2.vpos;
8419 it->current_y -= it2.current_y;
8420 it->current_x = it->hpos = 0;
8421
8422 /* If we moved too far back, move IT some lines forward. */
8423 if (it2.vpos > -dvpos)
8424 {
8425 int delta = it2.vpos + dvpos;
8426 it2 = *it;
8427 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8428 /* Move back again if we got too far ahead. */
8429 if (IT_CHARPOS (*it) >= start_charpos)
8430 *it = it2;
8431 }
8432 }
8433 }
8434
8435 /* Return 1 if IT points into the middle of a display vector. */
8436
8437 int
8438 in_display_vector_p (struct it *it)
8439 {
8440 return (it->method == GET_FROM_DISPLAY_VECTOR
8441 && it->current.dpvec_index > 0
8442 && it->dpvec + it->current.dpvec_index != it->dpend);
8443 }
8444
8445 \f
8446 /***********************************************************************
8447 Messages
8448 ***********************************************************************/
8449
8450
8451 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8452 to *Messages*. */
8453
8454 void
8455 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8456 {
8457 Lisp_Object args[3];
8458 Lisp_Object msg, fmt;
8459 char *buffer;
8460 EMACS_INT len;
8461 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8462 USE_SAFE_ALLOCA;
8463
8464 /* Do nothing if called asynchronously. Inserting text into
8465 a buffer may call after-change-functions and alike and
8466 that would means running Lisp asynchronously. */
8467 if (handling_signal)
8468 return;
8469
8470 fmt = msg = Qnil;
8471 GCPRO4 (fmt, msg, arg1, arg2);
8472
8473 args[0] = fmt = build_string (format);
8474 args[1] = arg1;
8475 args[2] = arg2;
8476 msg = Fformat (3, args);
8477
8478 len = SBYTES (msg) + 1;
8479 SAFE_ALLOCA (buffer, char *, len);
8480 memcpy (buffer, SDATA (msg), len);
8481
8482 message_dolog (buffer, len - 1, 1, 0);
8483 SAFE_FREE ();
8484
8485 UNGCPRO;
8486 }
8487
8488
8489 /* Output a newline in the *Messages* buffer if "needs" one. */
8490
8491 void
8492 message_log_maybe_newline (void)
8493 {
8494 if (message_log_need_newline)
8495 message_dolog ("", 0, 1, 0);
8496 }
8497
8498
8499 /* Add a string M of length NBYTES to the message log, optionally
8500 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8501 nonzero, means interpret the contents of M as multibyte. This
8502 function calls low-level routines in order to bypass text property
8503 hooks, etc. which might not be safe to run.
8504
8505 This may GC (insert may run before/after change hooks),
8506 so the buffer M must NOT point to a Lisp string. */
8507
8508 void
8509 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8510 {
8511 const unsigned char *msg = (const unsigned char *) m;
8512
8513 if (!NILP (Vmemory_full))
8514 return;
8515
8516 if (!NILP (Vmessage_log_max))
8517 {
8518 struct buffer *oldbuf;
8519 Lisp_Object oldpoint, oldbegv, oldzv;
8520 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8521 EMACS_INT point_at_end = 0;
8522 EMACS_INT zv_at_end = 0;
8523 Lisp_Object old_deactivate_mark, tem;
8524 struct gcpro gcpro1;
8525
8526 old_deactivate_mark = Vdeactivate_mark;
8527 oldbuf = current_buffer;
8528 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8529 BVAR (current_buffer, undo_list) = Qt;
8530
8531 oldpoint = message_dolog_marker1;
8532 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8533 oldbegv = message_dolog_marker2;
8534 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8535 oldzv = message_dolog_marker3;
8536 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8537 GCPRO1 (old_deactivate_mark);
8538
8539 if (PT == Z)
8540 point_at_end = 1;
8541 if (ZV == Z)
8542 zv_at_end = 1;
8543
8544 BEGV = BEG;
8545 BEGV_BYTE = BEG_BYTE;
8546 ZV = Z;
8547 ZV_BYTE = Z_BYTE;
8548 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8549
8550 /* Insert the string--maybe converting multibyte to single byte
8551 or vice versa, so that all the text fits the buffer. */
8552 if (multibyte
8553 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8554 {
8555 EMACS_INT i;
8556 int c, char_bytes;
8557 char work[1];
8558
8559 /* Convert a multibyte string to single-byte
8560 for the *Message* buffer. */
8561 for (i = 0; i < nbytes; i += char_bytes)
8562 {
8563 c = string_char_and_length (msg + i, &char_bytes);
8564 work[0] = (ASCII_CHAR_P (c)
8565 ? c
8566 : multibyte_char_to_unibyte (c));
8567 insert_1_both (work, 1, 1, 1, 0, 0);
8568 }
8569 }
8570 else if (! multibyte
8571 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8572 {
8573 EMACS_INT i;
8574 int c, char_bytes;
8575 unsigned char str[MAX_MULTIBYTE_LENGTH];
8576 /* Convert a single-byte string to multibyte
8577 for the *Message* buffer. */
8578 for (i = 0; i < nbytes; i++)
8579 {
8580 c = msg[i];
8581 MAKE_CHAR_MULTIBYTE (c);
8582 char_bytes = CHAR_STRING (c, str);
8583 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8584 }
8585 }
8586 else if (nbytes)
8587 insert_1 (m, nbytes, 1, 0, 0);
8588
8589 if (nlflag)
8590 {
8591 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8592 unsigned long int dups;
8593 insert_1 ("\n", 1, 1, 0, 0);
8594
8595 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8596 this_bol = PT;
8597 this_bol_byte = PT_BYTE;
8598
8599 /* See if this line duplicates the previous one.
8600 If so, combine duplicates. */
8601 if (this_bol > BEG)
8602 {
8603 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8604 prev_bol = PT;
8605 prev_bol_byte = PT_BYTE;
8606
8607 dups = message_log_check_duplicate (prev_bol_byte,
8608 this_bol_byte);
8609 if (dups)
8610 {
8611 del_range_both (prev_bol, prev_bol_byte,
8612 this_bol, this_bol_byte, 0);
8613 if (dups > 1)
8614 {
8615 char dupstr[40];
8616 int duplen;
8617
8618 /* If you change this format, don't forget to also
8619 change message_log_check_duplicate. */
8620 sprintf (dupstr, " [%lu times]", dups);
8621 duplen = strlen (dupstr);
8622 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8623 insert_1 (dupstr, duplen, 1, 0, 1);
8624 }
8625 }
8626 }
8627
8628 /* If we have more than the desired maximum number of lines
8629 in the *Messages* buffer now, delete the oldest ones.
8630 This is safe because we don't have undo in this buffer. */
8631
8632 if (NATNUMP (Vmessage_log_max))
8633 {
8634 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8635 -XFASTINT (Vmessage_log_max) - 1, 0);
8636 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8637 }
8638 }
8639 BEGV = XMARKER (oldbegv)->charpos;
8640 BEGV_BYTE = marker_byte_position (oldbegv);
8641
8642 if (zv_at_end)
8643 {
8644 ZV = Z;
8645 ZV_BYTE = Z_BYTE;
8646 }
8647 else
8648 {
8649 ZV = XMARKER (oldzv)->charpos;
8650 ZV_BYTE = marker_byte_position (oldzv);
8651 }
8652
8653 if (point_at_end)
8654 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8655 else
8656 /* We can't do Fgoto_char (oldpoint) because it will run some
8657 Lisp code. */
8658 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8659 XMARKER (oldpoint)->bytepos);
8660
8661 UNGCPRO;
8662 unchain_marker (XMARKER (oldpoint));
8663 unchain_marker (XMARKER (oldbegv));
8664 unchain_marker (XMARKER (oldzv));
8665
8666 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8667 set_buffer_internal (oldbuf);
8668 if (NILP (tem))
8669 windows_or_buffers_changed = old_windows_or_buffers_changed;
8670 message_log_need_newline = !nlflag;
8671 Vdeactivate_mark = old_deactivate_mark;
8672 }
8673 }
8674
8675
8676 /* We are at the end of the buffer after just having inserted a newline.
8677 (Note: We depend on the fact we won't be crossing the gap.)
8678 Check to see if the most recent message looks a lot like the previous one.
8679 Return 0 if different, 1 if the new one should just replace it, or a
8680 value N > 1 if we should also append " [N times]". */
8681
8682 static unsigned long int
8683 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8684 {
8685 EMACS_INT i;
8686 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8687 int seen_dots = 0;
8688 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8689 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8690
8691 for (i = 0; i < len; i++)
8692 {
8693 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8694 seen_dots = 1;
8695 if (p1[i] != p2[i])
8696 return seen_dots;
8697 }
8698 p1 += len;
8699 if (*p1 == '\n')
8700 return 2;
8701 if (*p1++ == ' ' && *p1++ == '[')
8702 {
8703 char *pend;
8704 unsigned long int n = strtoul ((char *) p1, &pend, 10);
8705 if (strncmp (pend, " times]\n", 8) == 0)
8706 return n+1;
8707 }
8708 return 0;
8709 }
8710 \f
8711
8712 /* Display an echo area message M with a specified length of NBYTES
8713 bytes. The string may include null characters. If M is 0, clear
8714 out any existing message, and let the mini-buffer text show
8715 through.
8716
8717 This may GC, so the buffer M must NOT point to a Lisp string. */
8718
8719 void
8720 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8721 {
8722 /* First flush out any partial line written with print. */
8723 message_log_maybe_newline ();
8724 if (m)
8725 message_dolog (m, nbytes, 1, multibyte);
8726 message2_nolog (m, nbytes, multibyte);
8727 }
8728
8729
8730 /* The non-logging counterpart of message2. */
8731
8732 void
8733 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8734 {
8735 struct frame *sf = SELECTED_FRAME ();
8736 message_enable_multibyte = multibyte;
8737
8738 if (FRAME_INITIAL_P (sf))
8739 {
8740 if (noninteractive_need_newline)
8741 putc ('\n', stderr);
8742 noninteractive_need_newline = 0;
8743 if (m)
8744 fwrite (m, nbytes, 1, stderr);
8745 if (cursor_in_echo_area == 0)
8746 fprintf (stderr, "\n");
8747 fflush (stderr);
8748 }
8749 /* A null message buffer means that the frame hasn't really been
8750 initialized yet. Error messages get reported properly by
8751 cmd_error, so this must be just an informative message; toss it. */
8752 else if (INTERACTIVE
8753 && sf->glyphs_initialized_p
8754 && FRAME_MESSAGE_BUF (sf))
8755 {
8756 Lisp_Object mini_window;
8757 struct frame *f;
8758
8759 /* Get the frame containing the mini-buffer
8760 that the selected frame is using. */
8761 mini_window = FRAME_MINIBUF_WINDOW (sf);
8762 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8763
8764 FRAME_SAMPLE_VISIBILITY (f);
8765 if (FRAME_VISIBLE_P (sf)
8766 && ! FRAME_VISIBLE_P (f))
8767 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8768
8769 if (m)
8770 {
8771 set_message (m, Qnil, nbytes, multibyte);
8772 if (minibuffer_auto_raise)
8773 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8774 }
8775 else
8776 clear_message (1, 1);
8777
8778 do_pending_window_change (0);
8779 echo_area_display (1);
8780 do_pending_window_change (0);
8781 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8782 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8783 }
8784 }
8785
8786
8787 /* Display an echo area message M with a specified length of NBYTES
8788 bytes. The string may include null characters. If M is not a
8789 string, clear out any existing message, and let the mini-buffer
8790 text show through.
8791
8792 This function cancels echoing. */
8793
8794 void
8795 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8796 {
8797 struct gcpro gcpro1;
8798
8799 GCPRO1 (m);
8800 clear_message (1,1);
8801 cancel_echoing ();
8802
8803 /* First flush out any partial line written with print. */
8804 message_log_maybe_newline ();
8805 if (STRINGP (m))
8806 {
8807 char *buffer;
8808 USE_SAFE_ALLOCA;
8809
8810 SAFE_ALLOCA (buffer, char *, nbytes);
8811 memcpy (buffer, SDATA (m), nbytes);
8812 message_dolog (buffer, nbytes, 1, multibyte);
8813 SAFE_FREE ();
8814 }
8815 message3_nolog (m, nbytes, multibyte);
8816
8817 UNGCPRO;
8818 }
8819
8820
8821 /* The non-logging version of message3.
8822 This does not cancel echoing, because it is used for echoing.
8823 Perhaps we need to make a separate function for echoing
8824 and make this cancel echoing. */
8825
8826 void
8827 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8828 {
8829 struct frame *sf = SELECTED_FRAME ();
8830 message_enable_multibyte = multibyte;
8831
8832 if (FRAME_INITIAL_P (sf))
8833 {
8834 if (noninteractive_need_newline)
8835 putc ('\n', stderr);
8836 noninteractive_need_newline = 0;
8837 if (STRINGP (m))
8838 fwrite (SDATA (m), nbytes, 1, stderr);
8839 if (cursor_in_echo_area == 0)
8840 fprintf (stderr, "\n");
8841 fflush (stderr);
8842 }
8843 /* A null message buffer means that the frame hasn't really been
8844 initialized yet. Error messages get reported properly by
8845 cmd_error, so this must be just an informative message; toss it. */
8846 else if (INTERACTIVE
8847 && sf->glyphs_initialized_p
8848 && FRAME_MESSAGE_BUF (sf))
8849 {
8850 Lisp_Object mini_window;
8851 Lisp_Object frame;
8852 struct frame *f;
8853
8854 /* Get the frame containing the mini-buffer
8855 that the selected frame is using. */
8856 mini_window = FRAME_MINIBUF_WINDOW (sf);
8857 frame = XWINDOW (mini_window)->frame;
8858 f = XFRAME (frame);
8859
8860 FRAME_SAMPLE_VISIBILITY (f);
8861 if (FRAME_VISIBLE_P (sf)
8862 && !FRAME_VISIBLE_P (f))
8863 Fmake_frame_visible (frame);
8864
8865 if (STRINGP (m) && SCHARS (m) > 0)
8866 {
8867 set_message (NULL, m, nbytes, multibyte);
8868 if (minibuffer_auto_raise)
8869 Fraise_frame (frame);
8870 /* Assume we are not echoing.
8871 (If we are, echo_now will override this.) */
8872 echo_message_buffer = Qnil;
8873 }
8874 else
8875 clear_message (1, 1);
8876
8877 do_pending_window_change (0);
8878 echo_area_display (1);
8879 do_pending_window_change (0);
8880 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8881 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8882 }
8883 }
8884
8885
8886 /* Display a null-terminated echo area message M. If M is 0, clear
8887 out any existing message, and let the mini-buffer text show through.
8888
8889 The buffer M must continue to exist until after the echo area gets
8890 cleared or some other message gets displayed there. Do not pass
8891 text that is stored in a Lisp string. Do not pass text in a buffer
8892 that was alloca'd. */
8893
8894 void
8895 message1 (const char *m)
8896 {
8897 message2 (m, (m ? strlen (m) : 0), 0);
8898 }
8899
8900
8901 /* The non-logging counterpart of message1. */
8902
8903 void
8904 message1_nolog (const char *m)
8905 {
8906 message2_nolog (m, (m ? strlen (m) : 0), 0);
8907 }
8908
8909 /* Display a message M which contains a single %s
8910 which gets replaced with STRING. */
8911
8912 void
8913 message_with_string (const char *m, Lisp_Object string, int log)
8914 {
8915 CHECK_STRING (string);
8916
8917 if (noninteractive)
8918 {
8919 if (m)
8920 {
8921 if (noninteractive_need_newline)
8922 putc ('\n', stderr);
8923 noninteractive_need_newline = 0;
8924 fprintf (stderr, m, SDATA (string));
8925 if (!cursor_in_echo_area)
8926 fprintf (stderr, "\n");
8927 fflush (stderr);
8928 }
8929 }
8930 else if (INTERACTIVE)
8931 {
8932 /* The frame whose minibuffer we're going to display the message on.
8933 It may be larger than the selected frame, so we need
8934 to use its buffer, not the selected frame's buffer. */
8935 Lisp_Object mini_window;
8936 struct frame *f, *sf = SELECTED_FRAME ();
8937
8938 /* Get the frame containing the minibuffer
8939 that the selected frame is using. */
8940 mini_window = FRAME_MINIBUF_WINDOW (sf);
8941 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8942
8943 /* A null message buffer means that the frame hasn't really been
8944 initialized yet. Error messages get reported properly by
8945 cmd_error, so this must be just an informative message; toss it. */
8946 if (FRAME_MESSAGE_BUF (f))
8947 {
8948 Lisp_Object args[2], msg;
8949 struct gcpro gcpro1, gcpro2;
8950
8951 args[0] = build_string (m);
8952 args[1] = msg = string;
8953 GCPRO2 (args[0], msg);
8954 gcpro1.nvars = 2;
8955
8956 msg = Fformat (2, args);
8957
8958 if (log)
8959 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8960 else
8961 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8962
8963 UNGCPRO;
8964
8965 /* Print should start at the beginning of the message
8966 buffer next time. */
8967 message_buf_print = 0;
8968 }
8969 }
8970 }
8971
8972
8973 /* Dump an informative message to the minibuf. If M is 0, clear out
8974 any existing message, and let the mini-buffer text show through. */
8975
8976 static void
8977 vmessage (const char *m, va_list ap)
8978 {
8979 if (noninteractive)
8980 {
8981 if (m)
8982 {
8983 if (noninteractive_need_newline)
8984 putc ('\n', stderr);
8985 noninteractive_need_newline = 0;
8986 vfprintf (stderr, m, ap);
8987 if (cursor_in_echo_area == 0)
8988 fprintf (stderr, "\n");
8989 fflush (stderr);
8990 }
8991 }
8992 else if (INTERACTIVE)
8993 {
8994 /* The frame whose mini-buffer we're going to display the message
8995 on. It may be larger than the selected frame, so we need to
8996 use its buffer, not the selected frame's buffer. */
8997 Lisp_Object mini_window;
8998 struct frame *f, *sf = SELECTED_FRAME ();
8999
9000 /* Get the frame containing the mini-buffer
9001 that the selected frame is using. */
9002 mini_window = FRAME_MINIBUF_WINDOW (sf);
9003 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9004
9005 /* A null message buffer means that the frame hasn't really been
9006 initialized yet. Error messages get reported properly by
9007 cmd_error, so this must be just an informative message; toss
9008 it. */
9009 if (FRAME_MESSAGE_BUF (f))
9010 {
9011 if (m)
9012 {
9013 size_t len;
9014
9015 len = doprnt (FRAME_MESSAGE_BUF (f),
9016 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9017
9018 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9019 }
9020 else
9021 message1 (0);
9022
9023 /* Print should start at the beginning of the message
9024 buffer next time. */
9025 message_buf_print = 0;
9026 }
9027 }
9028 }
9029
9030 void
9031 message (const char *m, ...)
9032 {
9033 va_list ap;
9034 va_start (ap, m);
9035 vmessage (m, ap);
9036 va_end (ap);
9037 }
9038
9039
9040 #if 0
9041 /* The non-logging version of message. */
9042
9043 void
9044 message_nolog (const char *m, ...)
9045 {
9046 Lisp_Object old_log_max;
9047 va_list ap;
9048 va_start (ap, m);
9049 old_log_max = Vmessage_log_max;
9050 Vmessage_log_max = Qnil;
9051 vmessage (m, ap);
9052 Vmessage_log_max = old_log_max;
9053 va_end (ap);
9054 }
9055 #endif
9056
9057
9058 /* Display the current message in the current mini-buffer. This is
9059 only called from error handlers in process.c, and is not time
9060 critical. */
9061
9062 void
9063 update_echo_area (void)
9064 {
9065 if (!NILP (echo_area_buffer[0]))
9066 {
9067 Lisp_Object string;
9068 string = Fcurrent_message ();
9069 message3 (string, SBYTES (string),
9070 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9071 }
9072 }
9073
9074
9075 /* Make sure echo area buffers in `echo_buffers' are live.
9076 If they aren't, make new ones. */
9077
9078 static void
9079 ensure_echo_area_buffers (void)
9080 {
9081 int i;
9082
9083 for (i = 0; i < 2; ++i)
9084 if (!BUFFERP (echo_buffer[i])
9085 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9086 {
9087 char name[30];
9088 Lisp_Object old_buffer;
9089 int j;
9090
9091 old_buffer = echo_buffer[i];
9092 sprintf (name, " *Echo Area %d*", i);
9093 echo_buffer[i] = Fget_buffer_create (build_string (name));
9094 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9095 /* to force word wrap in echo area -
9096 it was decided to postpone this*/
9097 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9098
9099 for (j = 0; j < 2; ++j)
9100 if (EQ (old_buffer, echo_area_buffer[j]))
9101 echo_area_buffer[j] = echo_buffer[i];
9102 }
9103 }
9104
9105
9106 /* Call FN with args A1..A4 with either the current or last displayed
9107 echo_area_buffer as current buffer.
9108
9109 WHICH zero means use the current message buffer
9110 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9111 from echo_buffer[] and clear it.
9112
9113 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9114 suitable buffer from echo_buffer[] and clear it.
9115
9116 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9117 that the current message becomes the last displayed one, make
9118 choose a suitable buffer for echo_area_buffer[0], and clear it.
9119
9120 Value is what FN returns. */
9121
9122 static int
9123 with_echo_area_buffer (struct window *w, int which,
9124 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9125 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9126 {
9127 Lisp_Object buffer;
9128 int this_one, the_other, clear_buffer_p, rc;
9129 int count = SPECPDL_INDEX ();
9130
9131 /* If buffers aren't live, make new ones. */
9132 ensure_echo_area_buffers ();
9133
9134 clear_buffer_p = 0;
9135
9136 if (which == 0)
9137 this_one = 0, the_other = 1;
9138 else if (which > 0)
9139 this_one = 1, the_other = 0;
9140 else
9141 {
9142 this_one = 0, the_other = 1;
9143 clear_buffer_p = 1;
9144
9145 /* We need a fresh one in case the current echo buffer equals
9146 the one containing the last displayed echo area message. */
9147 if (!NILP (echo_area_buffer[this_one])
9148 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9149 echo_area_buffer[this_one] = Qnil;
9150 }
9151
9152 /* Choose a suitable buffer from echo_buffer[] is we don't
9153 have one. */
9154 if (NILP (echo_area_buffer[this_one]))
9155 {
9156 echo_area_buffer[this_one]
9157 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9158 ? echo_buffer[the_other]
9159 : echo_buffer[this_one]);
9160 clear_buffer_p = 1;
9161 }
9162
9163 buffer = echo_area_buffer[this_one];
9164
9165 /* Don't get confused by reusing the buffer used for echoing
9166 for a different purpose. */
9167 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9168 cancel_echoing ();
9169
9170 record_unwind_protect (unwind_with_echo_area_buffer,
9171 with_echo_area_buffer_unwind_data (w));
9172
9173 /* Make the echo area buffer current. Note that for display
9174 purposes, it is not necessary that the displayed window's buffer
9175 == current_buffer, except for text property lookup. So, let's
9176 only set that buffer temporarily here without doing a full
9177 Fset_window_buffer. We must also change w->pointm, though,
9178 because otherwise an assertions in unshow_buffer fails, and Emacs
9179 aborts. */
9180 set_buffer_internal_1 (XBUFFER (buffer));
9181 if (w)
9182 {
9183 w->buffer = buffer;
9184 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9185 }
9186
9187 BVAR (current_buffer, undo_list) = Qt;
9188 BVAR (current_buffer, read_only) = Qnil;
9189 specbind (Qinhibit_read_only, Qt);
9190 specbind (Qinhibit_modification_hooks, Qt);
9191
9192 if (clear_buffer_p && Z > BEG)
9193 del_range (BEG, Z);
9194
9195 xassert (BEGV >= BEG);
9196 xassert (ZV <= Z && ZV >= BEGV);
9197
9198 rc = fn (a1, a2, a3, a4);
9199
9200 xassert (BEGV >= BEG);
9201 xassert (ZV <= Z && ZV >= BEGV);
9202
9203 unbind_to (count, Qnil);
9204 return rc;
9205 }
9206
9207
9208 /* Save state that should be preserved around the call to the function
9209 FN called in with_echo_area_buffer. */
9210
9211 static Lisp_Object
9212 with_echo_area_buffer_unwind_data (struct window *w)
9213 {
9214 int i = 0;
9215 Lisp_Object vector, tmp;
9216
9217 /* Reduce consing by keeping one vector in
9218 Vwith_echo_area_save_vector. */
9219 vector = Vwith_echo_area_save_vector;
9220 Vwith_echo_area_save_vector = Qnil;
9221
9222 if (NILP (vector))
9223 vector = Fmake_vector (make_number (7), Qnil);
9224
9225 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9226 ASET (vector, i, Vdeactivate_mark); ++i;
9227 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9228
9229 if (w)
9230 {
9231 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9232 ASET (vector, i, w->buffer); ++i;
9233 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9234 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9235 }
9236 else
9237 {
9238 int end = i + 4;
9239 for (; i < end; ++i)
9240 ASET (vector, i, Qnil);
9241 }
9242
9243 xassert (i == ASIZE (vector));
9244 return vector;
9245 }
9246
9247
9248 /* Restore global state from VECTOR which was created by
9249 with_echo_area_buffer_unwind_data. */
9250
9251 static Lisp_Object
9252 unwind_with_echo_area_buffer (Lisp_Object vector)
9253 {
9254 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9255 Vdeactivate_mark = AREF (vector, 1);
9256 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9257
9258 if (WINDOWP (AREF (vector, 3)))
9259 {
9260 struct window *w;
9261 Lisp_Object buffer, charpos, bytepos;
9262
9263 w = XWINDOW (AREF (vector, 3));
9264 buffer = AREF (vector, 4);
9265 charpos = AREF (vector, 5);
9266 bytepos = AREF (vector, 6);
9267
9268 w->buffer = buffer;
9269 set_marker_both (w->pointm, buffer,
9270 XFASTINT (charpos), XFASTINT (bytepos));
9271 }
9272
9273 Vwith_echo_area_save_vector = vector;
9274 return Qnil;
9275 }
9276
9277
9278 /* Set up the echo area for use by print functions. MULTIBYTE_P
9279 non-zero means we will print multibyte. */
9280
9281 void
9282 setup_echo_area_for_printing (int multibyte_p)
9283 {
9284 /* If we can't find an echo area any more, exit. */
9285 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9286 Fkill_emacs (Qnil);
9287
9288 ensure_echo_area_buffers ();
9289
9290 if (!message_buf_print)
9291 {
9292 /* A message has been output since the last time we printed.
9293 Choose a fresh echo area buffer. */
9294 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9295 echo_area_buffer[0] = echo_buffer[1];
9296 else
9297 echo_area_buffer[0] = echo_buffer[0];
9298
9299 /* Switch to that buffer and clear it. */
9300 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9301 BVAR (current_buffer, truncate_lines) = Qnil;
9302
9303 if (Z > BEG)
9304 {
9305 int count = SPECPDL_INDEX ();
9306 specbind (Qinhibit_read_only, Qt);
9307 /* Note that undo recording is always disabled. */
9308 del_range (BEG, Z);
9309 unbind_to (count, Qnil);
9310 }
9311 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9312
9313 /* Set up the buffer for the multibyteness we need. */
9314 if (multibyte_p
9315 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9316 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9317
9318 /* Raise the frame containing the echo area. */
9319 if (minibuffer_auto_raise)
9320 {
9321 struct frame *sf = SELECTED_FRAME ();
9322 Lisp_Object mini_window;
9323 mini_window = FRAME_MINIBUF_WINDOW (sf);
9324 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9325 }
9326
9327 message_log_maybe_newline ();
9328 message_buf_print = 1;
9329 }
9330 else
9331 {
9332 if (NILP (echo_area_buffer[0]))
9333 {
9334 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9335 echo_area_buffer[0] = echo_buffer[1];
9336 else
9337 echo_area_buffer[0] = echo_buffer[0];
9338 }
9339
9340 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9341 {
9342 /* Someone switched buffers between print requests. */
9343 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9344 BVAR (current_buffer, truncate_lines) = Qnil;
9345 }
9346 }
9347 }
9348
9349
9350 /* Display an echo area message in window W. Value is non-zero if W's
9351 height is changed. If display_last_displayed_message_p is
9352 non-zero, display the message that was last displayed, otherwise
9353 display the current message. */
9354
9355 static int
9356 display_echo_area (struct window *w)
9357 {
9358 int i, no_message_p, window_height_changed_p, count;
9359
9360 /* Temporarily disable garbage collections while displaying the echo
9361 area. This is done because a GC can print a message itself.
9362 That message would modify the echo area buffer's contents while a
9363 redisplay of the buffer is going on, and seriously confuse
9364 redisplay. */
9365 count = inhibit_garbage_collection ();
9366
9367 /* If there is no message, we must call display_echo_area_1
9368 nevertheless because it resizes the window. But we will have to
9369 reset the echo_area_buffer in question to nil at the end because
9370 with_echo_area_buffer will sets it to an empty buffer. */
9371 i = display_last_displayed_message_p ? 1 : 0;
9372 no_message_p = NILP (echo_area_buffer[i]);
9373
9374 window_height_changed_p
9375 = with_echo_area_buffer (w, display_last_displayed_message_p,
9376 display_echo_area_1,
9377 (intptr_t) w, Qnil, 0, 0);
9378
9379 if (no_message_p)
9380 echo_area_buffer[i] = Qnil;
9381
9382 unbind_to (count, Qnil);
9383 return window_height_changed_p;
9384 }
9385
9386
9387 /* Helper for display_echo_area. Display the current buffer which
9388 contains the current echo area message in window W, a mini-window,
9389 a pointer to which is passed in A1. A2..A4 are currently not used.
9390 Change the height of W so that all of the message is displayed.
9391 Value is non-zero if height of W was changed. */
9392
9393 static int
9394 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9395 {
9396 intptr_t i1 = a1;
9397 struct window *w = (struct window *) i1;
9398 Lisp_Object window;
9399 struct text_pos start;
9400 int window_height_changed_p = 0;
9401
9402 /* Do this before displaying, so that we have a large enough glyph
9403 matrix for the display. If we can't get enough space for the
9404 whole text, display the last N lines. That works by setting w->start. */
9405 window_height_changed_p = resize_mini_window (w, 0);
9406
9407 /* Use the starting position chosen by resize_mini_window. */
9408 SET_TEXT_POS_FROM_MARKER (start, w->start);
9409
9410 /* Display. */
9411 clear_glyph_matrix (w->desired_matrix);
9412 XSETWINDOW (window, w);
9413 try_window (window, start, 0);
9414
9415 return window_height_changed_p;
9416 }
9417
9418
9419 /* Resize the echo area window to exactly the size needed for the
9420 currently displayed message, if there is one. If a mini-buffer
9421 is active, don't shrink it. */
9422
9423 void
9424 resize_echo_area_exactly (void)
9425 {
9426 if (BUFFERP (echo_area_buffer[0])
9427 && WINDOWP (echo_area_window))
9428 {
9429 struct window *w = XWINDOW (echo_area_window);
9430 int resized_p;
9431 Lisp_Object resize_exactly;
9432
9433 if (minibuf_level == 0)
9434 resize_exactly = Qt;
9435 else
9436 resize_exactly = Qnil;
9437
9438 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9439 (intptr_t) w, resize_exactly,
9440 0, 0);
9441 if (resized_p)
9442 {
9443 ++windows_or_buffers_changed;
9444 ++update_mode_lines;
9445 redisplay_internal ();
9446 }
9447 }
9448 }
9449
9450
9451 /* Callback function for with_echo_area_buffer, when used from
9452 resize_echo_area_exactly. A1 contains a pointer to the window to
9453 resize, EXACTLY non-nil means resize the mini-window exactly to the
9454 size of the text displayed. A3 and A4 are not used. Value is what
9455 resize_mini_window returns. */
9456
9457 static int
9458 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9459 {
9460 intptr_t i1 = a1;
9461 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9462 }
9463
9464
9465 /* Resize mini-window W to fit the size of its contents. EXACT_P
9466 means size the window exactly to the size needed. Otherwise, it's
9467 only enlarged until W's buffer is empty.
9468
9469 Set W->start to the right place to begin display. If the whole
9470 contents fit, start at the beginning. Otherwise, start so as
9471 to make the end of the contents appear. This is particularly
9472 important for y-or-n-p, but seems desirable generally.
9473
9474 Value is non-zero if the window height has been changed. */
9475
9476 int
9477 resize_mini_window (struct window *w, int exact_p)
9478 {
9479 struct frame *f = XFRAME (w->frame);
9480 int window_height_changed_p = 0;
9481
9482 xassert (MINI_WINDOW_P (w));
9483
9484 /* By default, start display at the beginning. */
9485 set_marker_both (w->start, w->buffer,
9486 BUF_BEGV (XBUFFER (w->buffer)),
9487 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9488
9489 /* Don't resize windows while redisplaying a window; it would
9490 confuse redisplay functions when the size of the window they are
9491 displaying changes from under them. Such a resizing can happen,
9492 for instance, when which-func prints a long message while
9493 we are running fontification-functions. We're running these
9494 functions with safe_call which binds inhibit-redisplay to t. */
9495 if (!NILP (Vinhibit_redisplay))
9496 return 0;
9497
9498 /* Nil means don't try to resize. */
9499 if (NILP (Vresize_mini_windows)
9500 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9501 return 0;
9502
9503 if (!FRAME_MINIBUF_ONLY_P (f))
9504 {
9505 struct it it;
9506 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9507 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9508 int height, max_height;
9509 int unit = FRAME_LINE_HEIGHT (f);
9510 struct text_pos start;
9511 struct buffer *old_current_buffer = NULL;
9512
9513 if (current_buffer != XBUFFER (w->buffer))
9514 {
9515 old_current_buffer = current_buffer;
9516 set_buffer_internal (XBUFFER (w->buffer));
9517 }
9518
9519 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9520
9521 /* Compute the max. number of lines specified by the user. */
9522 if (FLOATP (Vmax_mini_window_height))
9523 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9524 else if (INTEGERP (Vmax_mini_window_height))
9525 max_height = XINT (Vmax_mini_window_height);
9526 else
9527 max_height = total_height / 4;
9528
9529 /* Correct that max. height if it's bogus. */
9530 max_height = max (1, max_height);
9531 max_height = min (total_height, max_height);
9532
9533 /* Find out the height of the text in the window. */
9534 if (it.line_wrap == TRUNCATE)
9535 height = 1;
9536 else
9537 {
9538 last_height = 0;
9539 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9540 if (it.max_ascent == 0 && it.max_descent == 0)
9541 height = it.current_y + last_height;
9542 else
9543 height = it.current_y + it.max_ascent + it.max_descent;
9544 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9545 height = (height + unit - 1) / unit;
9546 }
9547
9548 /* Compute a suitable window start. */
9549 if (height > max_height)
9550 {
9551 height = max_height;
9552 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9553 move_it_vertically_backward (&it, (height - 1) * unit);
9554 start = it.current.pos;
9555 }
9556 else
9557 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9558 SET_MARKER_FROM_TEXT_POS (w->start, start);
9559
9560 if (EQ (Vresize_mini_windows, Qgrow_only))
9561 {
9562 /* Let it grow only, until we display an empty message, in which
9563 case the window shrinks again. */
9564 if (height > WINDOW_TOTAL_LINES (w))
9565 {
9566 int old_height = WINDOW_TOTAL_LINES (w);
9567 freeze_window_starts (f, 1);
9568 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9569 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9570 }
9571 else if (height < WINDOW_TOTAL_LINES (w)
9572 && (exact_p || BEGV == ZV))
9573 {
9574 int old_height = WINDOW_TOTAL_LINES (w);
9575 freeze_window_starts (f, 0);
9576 shrink_mini_window (w);
9577 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9578 }
9579 }
9580 else
9581 {
9582 /* Always resize to exact size needed. */
9583 if (height > WINDOW_TOTAL_LINES (w))
9584 {
9585 int old_height = WINDOW_TOTAL_LINES (w);
9586 freeze_window_starts (f, 1);
9587 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9588 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9589 }
9590 else if (height < WINDOW_TOTAL_LINES (w))
9591 {
9592 int old_height = WINDOW_TOTAL_LINES (w);
9593 freeze_window_starts (f, 0);
9594 shrink_mini_window (w);
9595
9596 if (height)
9597 {
9598 freeze_window_starts (f, 1);
9599 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9600 }
9601
9602 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9603 }
9604 }
9605
9606 if (old_current_buffer)
9607 set_buffer_internal (old_current_buffer);
9608 }
9609
9610 return window_height_changed_p;
9611 }
9612
9613
9614 /* Value is the current message, a string, or nil if there is no
9615 current message. */
9616
9617 Lisp_Object
9618 current_message (void)
9619 {
9620 Lisp_Object msg;
9621
9622 if (!BUFFERP (echo_area_buffer[0]))
9623 msg = Qnil;
9624 else
9625 {
9626 with_echo_area_buffer (0, 0, current_message_1,
9627 (intptr_t) &msg, Qnil, 0, 0);
9628 if (NILP (msg))
9629 echo_area_buffer[0] = Qnil;
9630 }
9631
9632 return msg;
9633 }
9634
9635
9636 static int
9637 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9638 {
9639 intptr_t i1 = a1;
9640 Lisp_Object *msg = (Lisp_Object *) i1;
9641
9642 if (Z > BEG)
9643 *msg = make_buffer_string (BEG, Z, 1);
9644 else
9645 *msg = Qnil;
9646 return 0;
9647 }
9648
9649
9650 /* Push the current message on Vmessage_stack for later restauration
9651 by restore_message. Value is non-zero if the current message isn't
9652 empty. This is a relatively infrequent operation, so it's not
9653 worth optimizing. */
9654
9655 int
9656 push_message (void)
9657 {
9658 Lisp_Object msg;
9659 msg = current_message ();
9660 Vmessage_stack = Fcons (msg, Vmessage_stack);
9661 return STRINGP (msg);
9662 }
9663
9664
9665 /* Restore message display from the top of Vmessage_stack. */
9666
9667 void
9668 restore_message (void)
9669 {
9670 Lisp_Object msg;
9671
9672 xassert (CONSP (Vmessage_stack));
9673 msg = XCAR (Vmessage_stack);
9674 if (STRINGP (msg))
9675 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9676 else
9677 message3_nolog (msg, 0, 0);
9678 }
9679
9680
9681 /* Handler for record_unwind_protect calling pop_message. */
9682
9683 Lisp_Object
9684 pop_message_unwind (Lisp_Object dummy)
9685 {
9686 pop_message ();
9687 return Qnil;
9688 }
9689
9690 /* Pop the top-most entry off Vmessage_stack. */
9691
9692 static void
9693 pop_message (void)
9694 {
9695 xassert (CONSP (Vmessage_stack));
9696 Vmessage_stack = XCDR (Vmessage_stack);
9697 }
9698
9699
9700 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9701 exits. If the stack is not empty, we have a missing pop_message
9702 somewhere. */
9703
9704 void
9705 check_message_stack (void)
9706 {
9707 if (!NILP (Vmessage_stack))
9708 abort ();
9709 }
9710
9711
9712 /* Truncate to NCHARS what will be displayed in the echo area the next
9713 time we display it---but don't redisplay it now. */
9714
9715 void
9716 truncate_echo_area (EMACS_INT nchars)
9717 {
9718 if (nchars == 0)
9719 echo_area_buffer[0] = Qnil;
9720 /* A null message buffer means that the frame hasn't really been
9721 initialized yet. Error messages get reported properly by
9722 cmd_error, so this must be just an informative message; toss it. */
9723 else if (!noninteractive
9724 && INTERACTIVE
9725 && !NILP (echo_area_buffer[0]))
9726 {
9727 struct frame *sf = SELECTED_FRAME ();
9728 if (FRAME_MESSAGE_BUF (sf))
9729 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9730 }
9731 }
9732
9733
9734 /* Helper function for truncate_echo_area. Truncate the current
9735 message to at most NCHARS characters. */
9736
9737 static int
9738 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9739 {
9740 if (BEG + nchars < Z)
9741 del_range (BEG + nchars, Z);
9742 if (Z == BEG)
9743 echo_area_buffer[0] = Qnil;
9744 return 0;
9745 }
9746
9747
9748 /* Set the current message to a substring of S or STRING.
9749
9750 If STRING is a Lisp string, set the message to the first NBYTES
9751 bytes from STRING. NBYTES zero means use the whole string. If
9752 STRING is multibyte, the message will be displayed multibyte.
9753
9754 If S is not null, set the message to the first LEN bytes of S. LEN
9755 zero means use the whole string. MULTIBYTE_P non-zero means S is
9756 multibyte. Display the message multibyte in that case.
9757
9758 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9759 to t before calling set_message_1 (which calls insert).
9760 */
9761
9762 static void
9763 set_message (const char *s, Lisp_Object string,
9764 EMACS_INT nbytes, int multibyte_p)
9765 {
9766 message_enable_multibyte
9767 = ((s && multibyte_p)
9768 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9769
9770 with_echo_area_buffer (0, -1, set_message_1,
9771 (intptr_t) s, string, nbytes, multibyte_p);
9772 message_buf_print = 0;
9773 help_echo_showing_p = 0;
9774 }
9775
9776
9777 /* Helper function for set_message. Arguments have the same meaning
9778 as there, with A1 corresponding to S and A2 corresponding to STRING
9779 This function is called with the echo area buffer being
9780 current. */
9781
9782 static int
9783 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9784 {
9785 intptr_t i1 = a1;
9786 const char *s = (const char *) i1;
9787 const unsigned char *msg = (const unsigned char *) s;
9788 Lisp_Object string = a2;
9789
9790 /* Change multibyteness of the echo buffer appropriately. */
9791 if (message_enable_multibyte
9792 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9793 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9794
9795 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9796 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9797 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9798
9799 /* Insert new message at BEG. */
9800 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9801
9802 if (STRINGP (string))
9803 {
9804 EMACS_INT nchars;
9805
9806 if (nbytes == 0)
9807 nbytes = SBYTES (string);
9808 nchars = string_byte_to_char (string, nbytes);
9809
9810 /* This function takes care of single/multibyte conversion. We
9811 just have to ensure that the echo area buffer has the right
9812 setting of enable_multibyte_characters. */
9813 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9814 }
9815 else if (s)
9816 {
9817 if (nbytes == 0)
9818 nbytes = strlen (s);
9819
9820 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9821 {
9822 /* Convert from multi-byte to single-byte. */
9823 EMACS_INT i;
9824 int c, n;
9825 char work[1];
9826
9827 /* Convert a multibyte string to single-byte. */
9828 for (i = 0; i < nbytes; i += n)
9829 {
9830 c = string_char_and_length (msg + i, &n);
9831 work[0] = (ASCII_CHAR_P (c)
9832 ? c
9833 : multibyte_char_to_unibyte (c));
9834 insert_1_both (work, 1, 1, 1, 0, 0);
9835 }
9836 }
9837 else if (!multibyte_p
9838 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9839 {
9840 /* Convert from single-byte to multi-byte. */
9841 EMACS_INT i;
9842 int c, n;
9843 unsigned char str[MAX_MULTIBYTE_LENGTH];
9844
9845 /* Convert a single-byte string to multibyte. */
9846 for (i = 0; i < nbytes; i++)
9847 {
9848 c = msg[i];
9849 MAKE_CHAR_MULTIBYTE (c);
9850 n = CHAR_STRING (c, str);
9851 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9852 }
9853 }
9854 else
9855 insert_1 (s, nbytes, 1, 0, 0);
9856 }
9857
9858 return 0;
9859 }
9860
9861
9862 /* Clear messages. CURRENT_P non-zero means clear the current
9863 message. LAST_DISPLAYED_P non-zero means clear the message
9864 last displayed. */
9865
9866 void
9867 clear_message (int current_p, int last_displayed_p)
9868 {
9869 if (current_p)
9870 {
9871 echo_area_buffer[0] = Qnil;
9872 message_cleared_p = 1;
9873 }
9874
9875 if (last_displayed_p)
9876 echo_area_buffer[1] = Qnil;
9877
9878 message_buf_print = 0;
9879 }
9880
9881 /* Clear garbaged frames.
9882
9883 This function is used where the old redisplay called
9884 redraw_garbaged_frames which in turn called redraw_frame which in
9885 turn called clear_frame. The call to clear_frame was a source of
9886 flickering. I believe a clear_frame is not necessary. It should
9887 suffice in the new redisplay to invalidate all current matrices,
9888 and ensure a complete redisplay of all windows. */
9889
9890 static void
9891 clear_garbaged_frames (void)
9892 {
9893 if (frame_garbaged)
9894 {
9895 Lisp_Object tail, frame;
9896 int changed_count = 0;
9897
9898 FOR_EACH_FRAME (tail, frame)
9899 {
9900 struct frame *f = XFRAME (frame);
9901
9902 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9903 {
9904 if (f->resized_p)
9905 {
9906 Fredraw_frame (frame);
9907 f->force_flush_display_p = 1;
9908 }
9909 clear_current_matrices (f);
9910 changed_count++;
9911 f->garbaged = 0;
9912 f->resized_p = 0;
9913 }
9914 }
9915
9916 frame_garbaged = 0;
9917 if (changed_count)
9918 ++windows_or_buffers_changed;
9919 }
9920 }
9921
9922
9923 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9924 is non-zero update selected_frame. Value is non-zero if the
9925 mini-windows height has been changed. */
9926
9927 static int
9928 echo_area_display (int update_frame_p)
9929 {
9930 Lisp_Object mini_window;
9931 struct window *w;
9932 struct frame *f;
9933 int window_height_changed_p = 0;
9934 struct frame *sf = SELECTED_FRAME ();
9935
9936 mini_window = FRAME_MINIBUF_WINDOW (sf);
9937 w = XWINDOW (mini_window);
9938 f = XFRAME (WINDOW_FRAME (w));
9939
9940 /* Don't display if frame is invisible or not yet initialized. */
9941 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9942 return 0;
9943
9944 #ifdef HAVE_WINDOW_SYSTEM
9945 /* When Emacs starts, selected_frame may be the initial terminal
9946 frame. If we let this through, a message would be displayed on
9947 the terminal. */
9948 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9949 return 0;
9950 #endif /* HAVE_WINDOW_SYSTEM */
9951
9952 /* Redraw garbaged frames. */
9953 if (frame_garbaged)
9954 clear_garbaged_frames ();
9955
9956 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9957 {
9958 echo_area_window = mini_window;
9959 window_height_changed_p = display_echo_area (w);
9960 w->must_be_updated_p = 1;
9961
9962 /* Update the display, unless called from redisplay_internal.
9963 Also don't update the screen during redisplay itself. The
9964 update will happen at the end of redisplay, and an update
9965 here could cause confusion. */
9966 if (update_frame_p && !redisplaying_p)
9967 {
9968 int n = 0;
9969
9970 /* If the display update has been interrupted by pending
9971 input, update mode lines in the frame. Due to the
9972 pending input, it might have been that redisplay hasn't
9973 been called, so that mode lines above the echo area are
9974 garbaged. This looks odd, so we prevent it here. */
9975 if (!display_completed)
9976 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9977
9978 if (window_height_changed_p
9979 /* Don't do this if Emacs is shutting down. Redisplay
9980 needs to run hooks. */
9981 && !NILP (Vrun_hooks))
9982 {
9983 /* Must update other windows. Likewise as in other
9984 cases, don't let this update be interrupted by
9985 pending input. */
9986 int count = SPECPDL_INDEX ();
9987 specbind (Qredisplay_dont_pause, Qt);
9988 windows_or_buffers_changed = 1;
9989 redisplay_internal ();
9990 unbind_to (count, Qnil);
9991 }
9992 else if (FRAME_WINDOW_P (f) && n == 0)
9993 {
9994 /* Window configuration is the same as before.
9995 Can do with a display update of the echo area,
9996 unless we displayed some mode lines. */
9997 update_single_window (w, 1);
9998 FRAME_RIF (f)->flush_display (f);
9999 }
10000 else
10001 update_frame (f, 1, 1);
10002
10003 /* If cursor is in the echo area, make sure that the next
10004 redisplay displays the minibuffer, so that the cursor will
10005 be replaced with what the minibuffer wants. */
10006 if (cursor_in_echo_area)
10007 ++windows_or_buffers_changed;
10008 }
10009 }
10010 else if (!EQ (mini_window, selected_window))
10011 windows_or_buffers_changed++;
10012
10013 /* Last displayed message is now the current message. */
10014 echo_area_buffer[1] = echo_area_buffer[0];
10015 /* Inform read_char that we're not echoing. */
10016 echo_message_buffer = Qnil;
10017
10018 /* Prevent redisplay optimization in redisplay_internal by resetting
10019 this_line_start_pos. This is done because the mini-buffer now
10020 displays the message instead of its buffer text. */
10021 if (EQ (mini_window, selected_window))
10022 CHARPOS (this_line_start_pos) = 0;
10023
10024 return window_height_changed_p;
10025 }
10026
10027
10028 \f
10029 /***********************************************************************
10030 Mode Lines and Frame Titles
10031 ***********************************************************************/
10032
10033 /* A buffer for constructing non-propertized mode-line strings and
10034 frame titles in it; allocated from the heap in init_xdisp and
10035 resized as needed in store_mode_line_noprop_char. */
10036
10037 static char *mode_line_noprop_buf;
10038
10039 /* The buffer's end, and a current output position in it. */
10040
10041 static char *mode_line_noprop_buf_end;
10042 static char *mode_line_noprop_ptr;
10043
10044 #define MODE_LINE_NOPROP_LEN(start) \
10045 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10046
10047 static enum {
10048 MODE_LINE_DISPLAY = 0,
10049 MODE_LINE_TITLE,
10050 MODE_LINE_NOPROP,
10051 MODE_LINE_STRING
10052 } mode_line_target;
10053
10054 /* Alist that caches the results of :propertize.
10055 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10056 static Lisp_Object mode_line_proptrans_alist;
10057
10058 /* List of strings making up the mode-line. */
10059 static Lisp_Object mode_line_string_list;
10060
10061 /* Base face property when building propertized mode line string. */
10062 static Lisp_Object mode_line_string_face;
10063 static Lisp_Object mode_line_string_face_prop;
10064
10065
10066 /* Unwind data for mode line strings */
10067
10068 static Lisp_Object Vmode_line_unwind_vector;
10069
10070 static Lisp_Object
10071 format_mode_line_unwind_data (struct buffer *obuf,
10072 Lisp_Object owin,
10073 int save_proptrans)
10074 {
10075 Lisp_Object vector, tmp;
10076
10077 /* Reduce consing by keeping one vector in
10078 Vwith_echo_area_save_vector. */
10079 vector = Vmode_line_unwind_vector;
10080 Vmode_line_unwind_vector = Qnil;
10081
10082 if (NILP (vector))
10083 vector = Fmake_vector (make_number (8), Qnil);
10084
10085 ASET (vector, 0, make_number (mode_line_target));
10086 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10087 ASET (vector, 2, mode_line_string_list);
10088 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10089 ASET (vector, 4, mode_line_string_face);
10090 ASET (vector, 5, mode_line_string_face_prop);
10091
10092 if (obuf)
10093 XSETBUFFER (tmp, obuf);
10094 else
10095 tmp = Qnil;
10096 ASET (vector, 6, tmp);
10097 ASET (vector, 7, owin);
10098
10099 return vector;
10100 }
10101
10102 static Lisp_Object
10103 unwind_format_mode_line (Lisp_Object vector)
10104 {
10105 mode_line_target = XINT (AREF (vector, 0));
10106 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10107 mode_line_string_list = AREF (vector, 2);
10108 if (! EQ (AREF (vector, 3), Qt))
10109 mode_line_proptrans_alist = AREF (vector, 3);
10110 mode_line_string_face = AREF (vector, 4);
10111 mode_line_string_face_prop = AREF (vector, 5);
10112
10113 if (!NILP (AREF (vector, 7)))
10114 /* Select window before buffer, since it may change the buffer. */
10115 Fselect_window (AREF (vector, 7), Qt);
10116
10117 if (!NILP (AREF (vector, 6)))
10118 {
10119 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10120 ASET (vector, 6, Qnil);
10121 }
10122
10123 Vmode_line_unwind_vector = vector;
10124 return Qnil;
10125 }
10126
10127
10128 /* Store a single character C for the frame title in mode_line_noprop_buf.
10129 Re-allocate mode_line_noprop_buf if necessary. */
10130
10131 static void
10132 store_mode_line_noprop_char (char c)
10133 {
10134 /* If output position has reached the end of the allocated buffer,
10135 double the buffer's size. */
10136 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10137 {
10138 int len = MODE_LINE_NOPROP_LEN (0);
10139 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10140 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10141 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10142 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10143 }
10144
10145 *mode_line_noprop_ptr++ = c;
10146 }
10147
10148
10149 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10150 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10151 characters that yield more columns than PRECISION; PRECISION <= 0
10152 means copy the whole string. Pad with spaces until FIELD_WIDTH
10153 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10154 pad. Called from display_mode_element when it is used to build a
10155 frame title. */
10156
10157 static int
10158 store_mode_line_noprop (const char *string, int field_width, int precision)
10159 {
10160 const unsigned char *str = (const unsigned char *) string;
10161 int n = 0;
10162 EMACS_INT dummy, nbytes;
10163
10164 /* Copy at most PRECISION chars from STR. */
10165 nbytes = strlen (string);
10166 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10167 while (nbytes--)
10168 store_mode_line_noprop_char (*str++);
10169
10170 /* Fill up with spaces until FIELD_WIDTH reached. */
10171 while (field_width > 0
10172 && n < field_width)
10173 {
10174 store_mode_line_noprop_char (' ');
10175 ++n;
10176 }
10177
10178 return n;
10179 }
10180
10181 /***********************************************************************
10182 Frame Titles
10183 ***********************************************************************/
10184
10185 #ifdef HAVE_WINDOW_SYSTEM
10186
10187 /* Set the title of FRAME, if it has changed. The title format is
10188 Vicon_title_format if FRAME is iconified, otherwise it is
10189 frame_title_format. */
10190
10191 static void
10192 x_consider_frame_title (Lisp_Object frame)
10193 {
10194 struct frame *f = XFRAME (frame);
10195
10196 if (FRAME_WINDOW_P (f)
10197 || FRAME_MINIBUF_ONLY_P (f)
10198 || f->explicit_name)
10199 {
10200 /* Do we have more than one visible frame on this X display? */
10201 Lisp_Object tail;
10202 Lisp_Object fmt;
10203 int title_start;
10204 char *title;
10205 int len;
10206 struct it it;
10207 int count = SPECPDL_INDEX ();
10208
10209 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10210 {
10211 Lisp_Object other_frame = XCAR (tail);
10212 struct frame *tf = XFRAME (other_frame);
10213
10214 if (tf != f
10215 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10216 && !FRAME_MINIBUF_ONLY_P (tf)
10217 && !EQ (other_frame, tip_frame)
10218 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10219 break;
10220 }
10221
10222 /* Set global variable indicating that multiple frames exist. */
10223 multiple_frames = CONSP (tail);
10224
10225 /* Switch to the buffer of selected window of the frame. Set up
10226 mode_line_target so that display_mode_element will output into
10227 mode_line_noprop_buf; then display the title. */
10228 record_unwind_protect (unwind_format_mode_line,
10229 format_mode_line_unwind_data
10230 (current_buffer, selected_window, 0));
10231
10232 Fselect_window (f->selected_window, Qt);
10233 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10234 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10235
10236 mode_line_target = MODE_LINE_TITLE;
10237 title_start = MODE_LINE_NOPROP_LEN (0);
10238 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10239 NULL, DEFAULT_FACE_ID);
10240 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10241 len = MODE_LINE_NOPROP_LEN (title_start);
10242 title = mode_line_noprop_buf + title_start;
10243 unbind_to (count, Qnil);
10244
10245 /* Set the title only if it's changed. This avoids consing in
10246 the common case where it hasn't. (If it turns out that we've
10247 already wasted too much time by walking through the list with
10248 display_mode_element, then we might need to optimize at a
10249 higher level than this.) */
10250 if (! STRINGP (f->name)
10251 || SBYTES (f->name) != len
10252 || memcmp (title, SDATA (f->name), len) != 0)
10253 x_implicitly_set_name (f, make_string (title, len), Qnil);
10254 }
10255 }
10256
10257 #endif /* not HAVE_WINDOW_SYSTEM */
10258
10259
10260
10261 \f
10262 /***********************************************************************
10263 Menu Bars
10264 ***********************************************************************/
10265
10266
10267 /* Prepare for redisplay by updating menu-bar item lists when
10268 appropriate. This can call eval. */
10269
10270 void
10271 prepare_menu_bars (void)
10272 {
10273 int all_windows;
10274 struct gcpro gcpro1, gcpro2;
10275 struct frame *f;
10276 Lisp_Object tooltip_frame;
10277
10278 #ifdef HAVE_WINDOW_SYSTEM
10279 tooltip_frame = tip_frame;
10280 #else
10281 tooltip_frame = Qnil;
10282 #endif
10283
10284 /* Update all frame titles based on their buffer names, etc. We do
10285 this before the menu bars so that the buffer-menu will show the
10286 up-to-date frame titles. */
10287 #ifdef HAVE_WINDOW_SYSTEM
10288 if (windows_or_buffers_changed || update_mode_lines)
10289 {
10290 Lisp_Object tail, frame;
10291
10292 FOR_EACH_FRAME (tail, frame)
10293 {
10294 f = XFRAME (frame);
10295 if (!EQ (frame, tooltip_frame)
10296 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10297 x_consider_frame_title (frame);
10298 }
10299 }
10300 #endif /* HAVE_WINDOW_SYSTEM */
10301
10302 /* Update the menu bar item lists, if appropriate. This has to be
10303 done before any actual redisplay or generation of display lines. */
10304 all_windows = (update_mode_lines
10305 || buffer_shared > 1
10306 || windows_or_buffers_changed);
10307 if (all_windows)
10308 {
10309 Lisp_Object tail, frame;
10310 int count = SPECPDL_INDEX ();
10311 /* 1 means that update_menu_bar has run its hooks
10312 so any further calls to update_menu_bar shouldn't do so again. */
10313 int menu_bar_hooks_run = 0;
10314
10315 record_unwind_save_match_data ();
10316
10317 FOR_EACH_FRAME (tail, frame)
10318 {
10319 f = XFRAME (frame);
10320
10321 /* Ignore tooltip frame. */
10322 if (EQ (frame, tooltip_frame))
10323 continue;
10324
10325 /* If a window on this frame changed size, report that to
10326 the user and clear the size-change flag. */
10327 if (FRAME_WINDOW_SIZES_CHANGED (f))
10328 {
10329 Lisp_Object functions;
10330
10331 /* Clear flag first in case we get an error below. */
10332 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10333 functions = Vwindow_size_change_functions;
10334 GCPRO2 (tail, functions);
10335
10336 while (CONSP (functions))
10337 {
10338 if (!EQ (XCAR (functions), Qt))
10339 call1 (XCAR (functions), frame);
10340 functions = XCDR (functions);
10341 }
10342 UNGCPRO;
10343 }
10344
10345 GCPRO1 (tail);
10346 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10347 #ifdef HAVE_WINDOW_SYSTEM
10348 update_tool_bar (f, 0);
10349 #endif
10350 #ifdef HAVE_NS
10351 if (windows_or_buffers_changed
10352 && FRAME_NS_P (f))
10353 ns_set_doc_edited (f, Fbuffer_modified_p
10354 (XWINDOW (f->selected_window)->buffer));
10355 #endif
10356 UNGCPRO;
10357 }
10358
10359 unbind_to (count, Qnil);
10360 }
10361 else
10362 {
10363 struct frame *sf = SELECTED_FRAME ();
10364 update_menu_bar (sf, 1, 0);
10365 #ifdef HAVE_WINDOW_SYSTEM
10366 update_tool_bar (sf, 1);
10367 #endif
10368 }
10369 }
10370
10371
10372 /* Update the menu bar item list for frame F. This has to be done
10373 before we start to fill in any display lines, because it can call
10374 eval.
10375
10376 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10377
10378 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10379 already ran the menu bar hooks for this redisplay, so there
10380 is no need to run them again. The return value is the
10381 updated value of this flag, to pass to the next call. */
10382
10383 static int
10384 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10385 {
10386 Lisp_Object window;
10387 register struct window *w;
10388
10389 /* If called recursively during a menu update, do nothing. This can
10390 happen when, for instance, an activate-menubar-hook causes a
10391 redisplay. */
10392 if (inhibit_menubar_update)
10393 return hooks_run;
10394
10395 window = FRAME_SELECTED_WINDOW (f);
10396 w = XWINDOW (window);
10397
10398 if (FRAME_WINDOW_P (f)
10399 ?
10400 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10401 || defined (HAVE_NS) || defined (USE_GTK)
10402 FRAME_EXTERNAL_MENU_BAR (f)
10403 #else
10404 FRAME_MENU_BAR_LINES (f) > 0
10405 #endif
10406 : FRAME_MENU_BAR_LINES (f) > 0)
10407 {
10408 /* If the user has switched buffers or windows, we need to
10409 recompute to reflect the new bindings. But we'll
10410 recompute when update_mode_lines is set too; that means
10411 that people can use force-mode-line-update to request
10412 that the menu bar be recomputed. The adverse effect on
10413 the rest of the redisplay algorithm is about the same as
10414 windows_or_buffers_changed anyway. */
10415 if (windows_or_buffers_changed
10416 /* This used to test w->update_mode_line, but we believe
10417 there is no need to recompute the menu in that case. */
10418 || update_mode_lines
10419 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10420 < BUF_MODIFF (XBUFFER (w->buffer)))
10421 != !NILP (w->last_had_star))
10422 || ((!NILP (Vtransient_mark_mode)
10423 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10424 != !NILP (w->region_showing)))
10425 {
10426 struct buffer *prev = current_buffer;
10427 int count = SPECPDL_INDEX ();
10428
10429 specbind (Qinhibit_menubar_update, Qt);
10430
10431 set_buffer_internal_1 (XBUFFER (w->buffer));
10432 if (save_match_data)
10433 record_unwind_save_match_data ();
10434 if (NILP (Voverriding_local_map_menu_flag))
10435 {
10436 specbind (Qoverriding_terminal_local_map, Qnil);
10437 specbind (Qoverriding_local_map, Qnil);
10438 }
10439
10440 if (!hooks_run)
10441 {
10442 /* Run the Lucid hook. */
10443 safe_run_hooks (Qactivate_menubar_hook);
10444
10445 /* If it has changed current-menubar from previous value,
10446 really recompute the menu-bar from the value. */
10447 if (! NILP (Vlucid_menu_bar_dirty_flag))
10448 call0 (Qrecompute_lucid_menubar);
10449
10450 safe_run_hooks (Qmenu_bar_update_hook);
10451
10452 hooks_run = 1;
10453 }
10454
10455 XSETFRAME (Vmenu_updating_frame, f);
10456 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10457
10458 /* Redisplay the menu bar in case we changed it. */
10459 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10460 || defined (HAVE_NS) || defined (USE_GTK)
10461 if (FRAME_WINDOW_P (f))
10462 {
10463 #if defined (HAVE_NS)
10464 /* All frames on Mac OS share the same menubar. So only
10465 the selected frame should be allowed to set it. */
10466 if (f == SELECTED_FRAME ())
10467 #endif
10468 set_frame_menubar (f, 0, 0);
10469 }
10470 else
10471 /* On a terminal screen, the menu bar is an ordinary screen
10472 line, and this makes it get updated. */
10473 w->update_mode_line = Qt;
10474 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10475 /* In the non-toolkit version, the menu bar is an ordinary screen
10476 line, and this makes it get updated. */
10477 w->update_mode_line = Qt;
10478 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10479
10480 unbind_to (count, Qnil);
10481 set_buffer_internal_1 (prev);
10482 }
10483 }
10484
10485 return hooks_run;
10486 }
10487
10488
10489 \f
10490 /***********************************************************************
10491 Output Cursor
10492 ***********************************************************************/
10493
10494 #ifdef HAVE_WINDOW_SYSTEM
10495
10496 /* EXPORT:
10497 Nominal cursor position -- where to draw output.
10498 HPOS and VPOS are window relative glyph matrix coordinates.
10499 X and Y are window relative pixel coordinates. */
10500
10501 struct cursor_pos output_cursor;
10502
10503
10504 /* EXPORT:
10505 Set the global variable output_cursor to CURSOR. All cursor
10506 positions are relative to updated_window. */
10507
10508 void
10509 set_output_cursor (struct cursor_pos *cursor)
10510 {
10511 output_cursor.hpos = cursor->hpos;
10512 output_cursor.vpos = cursor->vpos;
10513 output_cursor.x = cursor->x;
10514 output_cursor.y = cursor->y;
10515 }
10516
10517
10518 /* EXPORT for RIF:
10519 Set a nominal cursor position.
10520
10521 HPOS and VPOS are column/row positions in a window glyph matrix. X
10522 and Y are window text area relative pixel positions.
10523
10524 If this is done during an update, updated_window will contain the
10525 window that is being updated and the position is the future output
10526 cursor position for that window. If updated_window is null, use
10527 selected_window and display the cursor at the given position. */
10528
10529 void
10530 x_cursor_to (int vpos, int hpos, int y, int x)
10531 {
10532 struct window *w;
10533
10534 /* If updated_window is not set, work on selected_window. */
10535 if (updated_window)
10536 w = updated_window;
10537 else
10538 w = XWINDOW (selected_window);
10539
10540 /* Set the output cursor. */
10541 output_cursor.hpos = hpos;
10542 output_cursor.vpos = vpos;
10543 output_cursor.x = x;
10544 output_cursor.y = y;
10545
10546 /* If not called as part of an update, really display the cursor.
10547 This will also set the cursor position of W. */
10548 if (updated_window == NULL)
10549 {
10550 BLOCK_INPUT;
10551 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10552 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10553 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10554 UNBLOCK_INPUT;
10555 }
10556 }
10557
10558 #endif /* HAVE_WINDOW_SYSTEM */
10559
10560 \f
10561 /***********************************************************************
10562 Tool-bars
10563 ***********************************************************************/
10564
10565 #ifdef HAVE_WINDOW_SYSTEM
10566
10567 /* Where the mouse was last time we reported a mouse event. */
10568
10569 FRAME_PTR last_mouse_frame;
10570
10571 /* Tool-bar item index of the item on which a mouse button was pressed
10572 or -1. */
10573
10574 int last_tool_bar_item;
10575
10576
10577 static Lisp_Object
10578 update_tool_bar_unwind (Lisp_Object frame)
10579 {
10580 selected_frame = frame;
10581 return Qnil;
10582 }
10583
10584 /* Update the tool-bar item list for frame F. This has to be done
10585 before we start to fill in any display lines. Called from
10586 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10587 and restore it here. */
10588
10589 static void
10590 update_tool_bar (struct frame *f, int save_match_data)
10591 {
10592 #if defined (USE_GTK) || defined (HAVE_NS)
10593 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10594 #else
10595 int do_update = WINDOWP (f->tool_bar_window)
10596 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10597 #endif
10598
10599 if (do_update)
10600 {
10601 Lisp_Object window;
10602 struct window *w;
10603
10604 window = FRAME_SELECTED_WINDOW (f);
10605 w = XWINDOW (window);
10606
10607 /* If the user has switched buffers or windows, we need to
10608 recompute to reflect the new bindings. But we'll
10609 recompute when update_mode_lines is set too; that means
10610 that people can use force-mode-line-update to request
10611 that the menu bar be recomputed. The adverse effect on
10612 the rest of the redisplay algorithm is about the same as
10613 windows_or_buffers_changed anyway. */
10614 if (windows_or_buffers_changed
10615 || !NILP (w->update_mode_line)
10616 || update_mode_lines
10617 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10618 < BUF_MODIFF (XBUFFER (w->buffer)))
10619 != !NILP (w->last_had_star))
10620 || ((!NILP (Vtransient_mark_mode)
10621 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10622 != !NILP (w->region_showing)))
10623 {
10624 struct buffer *prev = current_buffer;
10625 int count = SPECPDL_INDEX ();
10626 Lisp_Object frame, new_tool_bar;
10627 int new_n_tool_bar;
10628 struct gcpro gcpro1;
10629
10630 /* Set current_buffer to the buffer of the selected
10631 window of the frame, so that we get the right local
10632 keymaps. */
10633 set_buffer_internal_1 (XBUFFER (w->buffer));
10634
10635 /* Save match data, if we must. */
10636 if (save_match_data)
10637 record_unwind_save_match_data ();
10638
10639 /* Make sure that we don't accidentally use bogus keymaps. */
10640 if (NILP (Voverriding_local_map_menu_flag))
10641 {
10642 specbind (Qoverriding_terminal_local_map, Qnil);
10643 specbind (Qoverriding_local_map, Qnil);
10644 }
10645
10646 GCPRO1 (new_tool_bar);
10647
10648 /* We must temporarily set the selected frame to this frame
10649 before calling tool_bar_items, because the calculation of
10650 the tool-bar keymap uses the selected frame (see
10651 `tool-bar-make-keymap' in tool-bar.el). */
10652 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10653 XSETFRAME (frame, f);
10654 selected_frame = frame;
10655
10656 /* Build desired tool-bar items from keymaps. */
10657 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10658 &new_n_tool_bar);
10659
10660 /* Redisplay the tool-bar if we changed it. */
10661 if (new_n_tool_bar != f->n_tool_bar_items
10662 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10663 {
10664 /* Redisplay that happens asynchronously due to an expose event
10665 may access f->tool_bar_items. Make sure we update both
10666 variables within BLOCK_INPUT so no such event interrupts. */
10667 BLOCK_INPUT;
10668 f->tool_bar_items = new_tool_bar;
10669 f->n_tool_bar_items = new_n_tool_bar;
10670 w->update_mode_line = Qt;
10671 UNBLOCK_INPUT;
10672 }
10673
10674 UNGCPRO;
10675
10676 unbind_to (count, Qnil);
10677 set_buffer_internal_1 (prev);
10678 }
10679 }
10680 }
10681
10682
10683 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10684 F's desired tool-bar contents. F->tool_bar_items must have
10685 been set up previously by calling prepare_menu_bars. */
10686
10687 static void
10688 build_desired_tool_bar_string (struct frame *f)
10689 {
10690 int i, size, size_needed;
10691 struct gcpro gcpro1, gcpro2, gcpro3;
10692 Lisp_Object image, plist, props;
10693
10694 image = plist = props = Qnil;
10695 GCPRO3 (image, plist, props);
10696
10697 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10698 Otherwise, make a new string. */
10699
10700 /* The size of the string we might be able to reuse. */
10701 size = (STRINGP (f->desired_tool_bar_string)
10702 ? SCHARS (f->desired_tool_bar_string)
10703 : 0);
10704
10705 /* We need one space in the string for each image. */
10706 size_needed = f->n_tool_bar_items;
10707
10708 /* Reuse f->desired_tool_bar_string, if possible. */
10709 if (size < size_needed || NILP (f->desired_tool_bar_string))
10710 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10711 make_number (' '));
10712 else
10713 {
10714 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10715 Fremove_text_properties (make_number (0), make_number (size),
10716 props, f->desired_tool_bar_string);
10717 }
10718
10719 /* Put a `display' property on the string for the images to display,
10720 put a `menu_item' property on tool-bar items with a value that
10721 is the index of the item in F's tool-bar item vector. */
10722 for (i = 0; i < f->n_tool_bar_items; ++i)
10723 {
10724 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10725
10726 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10727 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10728 int hmargin, vmargin, relief, idx, end;
10729
10730 /* If image is a vector, choose the image according to the
10731 button state. */
10732 image = PROP (TOOL_BAR_ITEM_IMAGES);
10733 if (VECTORP (image))
10734 {
10735 if (enabled_p)
10736 idx = (selected_p
10737 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10738 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10739 else
10740 idx = (selected_p
10741 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10742 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10743
10744 xassert (ASIZE (image) >= idx);
10745 image = AREF (image, idx);
10746 }
10747 else
10748 idx = -1;
10749
10750 /* Ignore invalid image specifications. */
10751 if (!valid_image_p (image))
10752 continue;
10753
10754 /* Display the tool-bar button pressed, or depressed. */
10755 plist = Fcopy_sequence (XCDR (image));
10756
10757 /* Compute margin and relief to draw. */
10758 relief = (tool_bar_button_relief >= 0
10759 ? tool_bar_button_relief
10760 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10761 hmargin = vmargin = relief;
10762
10763 if (INTEGERP (Vtool_bar_button_margin)
10764 && XINT (Vtool_bar_button_margin) > 0)
10765 {
10766 hmargin += XFASTINT (Vtool_bar_button_margin);
10767 vmargin += XFASTINT (Vtool_bar_button_margin);
10768 }
10769 else if (CONSP (Vtool_bar_button_margin))
10770 {
10771 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10772 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10773 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10774
10775 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10776 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10777 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10778 }
10779
10780 if (auto_raise_tool_bar_buttons_p)
10781 {
10782 /* Add a `:relief' property to the image spec if the item is
10783 selected. */
10784 if (selected_p)
10785 {
10786 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10787 hmargin -= relief;
10788 vmargin -= relief;
10789 }
10790 }
10791 else
10792 {
10793 /* If image is selected, display it pressed, i.e. with a
10794 negative relief. If it's not selected, display it with a
10795 raised relief. */
10796 plist = Fplist_put (plist, QCrelief,
10797 (selected_p
10798 ? make_number (-relief)
10799 : make_number (relief)));
10800 hmargin -= relief;
10801 vmargin -= relief;
10802 }
10803
10804 /* Put a margin around the image. */
10805 if (hmargin || vmargin)
10806 {
10807 if (hmargin == vmargin)
10808 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10809 else
10810 plist = Fplist_put (plist, QCmargin,
10811 Fcons (make_number (hmargin),
10812 make_number (vmargin)));
10813 }
10814
10815 /* If button is not enabled, and we don't have special images
10816 for the disabled state, make the image appear disabled by
10817 applying an appropriate algorithm to it. */
10818 if (!enabled_p && idx < 0)
10819 plist = Fplist_put (plist, QCconversion, Qdisabled);
10820
10821 /* Put a `display' text property on the string for the image to
10822 display. Put a `menu-item' property on the string that gives
10823 the start of this item's properties in the tool-bar items
10824 vector. */
10825 image = Fcons (Qimage, plist);
10826 props = list4 (Qdisplay, image,
10827 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10828
10829 /* Let the last image hide all remaining spaces in the tool bar
10830 string. The string can be longer than needed when we reuse a
10831 previous string. */
10832 if (i + 1 == f->n_tool_bar_items)
10833 end = SCHARS (f->desired_tool_bar_string);
10834 else
10835 end = i + 1;
10836 Fadd_text_properties (make_number (i), make_number (end),
10837 props, f->desired_tool_bar_string);
10838 #undef PROP
10839 }
10840
10841 UNGCPRO;
10842 }
10843
10844
10845 /* Display one line of the tool-bar of frame IT->f.
10846
10847 HEIGHT specifies the desired height of the tool-bar line.
10848 If the actual height of the glyph row is less than HEIGHT, the
10849 row's height is increased to HEIGHT, and the icons are centered
10850 vertically in the new height.
10851
10852 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10853 count a final empty row in case the tool-bar width exactly matches
10854 the window width.
10855 */
10856
10857 static void
10858 display_tool_bar_line (struct it *it, int height)
10859 {
10860 struct glyph_row *row = it->glyph_row;
10861 int max_x = it->last_visible_x;
10862 struct glyph *last;
10863
10864 prepare_desired_row (row);
10865 row->y = it->current_y;
10866
10867 /* Note that this isn't made use of if the face hasn't a box,
10868 so there's no need to check the face here. */
10869 it->start_of_box_run_p = 1;
10870
10871 while (it->current_x < max_x)
10872 {
10873 int x, n_glyphs_before, i, nglyphs;
10874 struct it it_before;
10875
10876 /* Get the next display element. */
10877 if (!get_next_display_element (it))
10878 {
10879 /* Don't count empty row if we are counting needed tool-bar lines. */
10880 if (height < 0 && !it->hpos)
10881 return;
10882 break;
10883 }
10884
10885 /* Produce glyphs. */
10886 n_glyphs_before = row->used[TEXT_AREA];
10887 it_before = *it;
10888
10889 PRODUCE_GLYPHS (it);
10890
10891 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10892 i = 0;
10893 x = it_before.current_x;
10894 while (i < nglyphs)
10895 {
10896 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10897
10898 if (x + glyph->pixel_width > max_x)
10899 {
10900 /* Glyph doesn't fit on line. Backtrack. */
10901 row->used[TEXT_AREA] = n_glyphs_before;
10902 *it = it_before;
10903 /* If this is the only glyph on this line, it will never fit on the
10904 tool-bar, so skip it. But ensure there is at least one glyph,
10905 so we don't accidentally disable the tool-bar. */
10906 if (n_glyphs_before == 0
10907 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10908 break;
10909 goto out;
10910 }
10911
10912 ++it->hpos;
10913 x += glyph->pixel_width;
10914 ++i;
10915 }
10916
10917 /* Stop at line end. */
10918 if (ITERATOR_AT_END_OF_LINE_P (it))
10919 break;
10920
10921 set_iterator_to_next (it, 1);
10922 }
10923
10924 out:;
10925
10926 row->displays_text_p = row->used[TEXT_AREA] != 0;
10927
10928 /* Use default face for the border below the tool bar.
10929
10930 FIXME: When auto-resize-tool-bars is grow-only, there is
10931 no additional border below the possibly empty tool-bar lines.
10932 So to make the extra empty lines look "normal", we have to
10933 use the tool-bar face for the border too. */
10934 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10935 it->face_id = DEFAULT_FACE_ID;
10936
10937 extend_face_to_end_of_line (it);
10938 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10939 last->right_box_line_p = 1;
10940 if (last == row->glyphs[TEXT_AREA])
10941 last->left_box_line_p = 1;
10942
10943 /* Make line the desired height and center it vertically. */
10944 if ((height -= it->max_ascent + it->max_descent) > 0)
10945 {
10946 /* Don't add more than one line height. */
10947 height %= FRAME_LINE_HEIGHT (it->f);
10948 it->max_ascent += height / 2;
10949 it->max_descent += (height + 1) / 2;
10950 }
10951
10952 compute_line_metrics (it);
10953
10954 /* If line is empty, make it occupy the rest of the tool-bar. */
10955 if (!row->displays_text_p)
10956 {
10957 row->height = row->phys_height = it->last_visible_y - row->y;
10958 row->visible_height = row->height;
10959 row->ascent = row->phys_ascent = 0;
10960 row->extra_line_spacing = 0;
10961 }
10962
10963 row->full_width_p = 1;
10964 row->continued_p = 0;
10965 row->truncated_on_left_p = 0;
10966 row->truncated_on_right_p = 0;
10967
10968 it->current_x = it->hpos = 0;
10969 it->current_y += row->height;
10970 ++it->vpos;
10971 ++it->glyph_row;
10972 }
10973
10974
10975 /* Max tool-bar height. */
10976
10977 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10978 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10979
10980 /* Value is the number of screen lines needed to make all tool-bar
10981 items of frame F visible. The number of actual rows needed is
10982 returned in *N_ROWS if non-NULL. */
10983
10984 static int
10985 tool_bar_lines_needed (struct frame *f, int *n_rows)
10986 {
10987 struct window *w = XWINDOW (f->tool_bar_window);
10988 struct it it;
10989 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10990 the desired matrix, so use (unused) mode-line row as temporary row to
10991 avoid destroying the first tool-bar row. */
10992 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10993
10994 /* Initialize an iterator for iteration over
10995 F->desired_tool_bar_string in the tool-bar window of frame F. */
10996 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10997 it.first_visible_x = 0;
10998 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10999 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11000 it.paragraph_embedding = L2R;
11001
11002 while (!ITERATOR_AT_END_P (&it))
11003 {
11004 clear_glyph_row (temp_row);
11005 it.glyph_row = temp_row;
11006 display_tool_bar_line (&it, -1);
11007 }
11008 clear_glyph_row (temp_row);
11009
11010 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11011 if (n_rows)
11012 *n_rows = it.vpos > 0 ? it.vpos : -1;
11013
11014 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11015 }
11016
11017
11018 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11019 0, 1, 0,
11020 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11021 (Lisp_Object frame)
11022 {
11023 struct frame *f;
11024 struct window *w;
11025 int nlines = 0;
11026
11027 if (NILP (frame))
11028 frame = selected_frame;
11029 else
11030 CHECK_FRAME (frame);
11031 f = XFRAME (frame);
11032
11033 if (WINDOWP (f->tool_bar_window)
11034 || (w = XWINDOW (f->tool_bar_window),
11035 WINDOW_TOTAL_LINES (w) > 0))
11036 {
11037 update_tool_bar (f, 1);
11038 if (f->n_tool_bar_items)
11039 {
11040 build_desired_tool_bar_string (f);
11041 nlines = tool_bar_lines_needed (f, NULL);
11042 }
11043 }
11044
11045 return make_number (nlines);
11046 }
11047
11048
11049 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11050 height should be changed. */
11051
11052 static int
11053 redisplay_tool_bar (struct frame *f)
11054 {
11055 struct window *w;
11056 struct it it;
11057 struct glyph_row *row;
11058
11059 #if defined (USE_GTK) || defined (HAVE_NS)
11060 if (FRAME_EXTERNAL_TOOL_BAR (f))
11061 update_frame_tool_bar (f);
11062 return 0;
11063 #endif
11064
11065 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11066 do anything. This means you must start with tool-bar-lines
11067 non-zero to get the auto-sizing effect. Or in other words, you
11068 can turn off tool-bars by specifying tool-bar-lines zero. */
11069 if (!WINDOWP (f->tool_bar_window)
11070 || (w = XWINDOW (f->tool_bar_window),
11071 WINDOW_TOTAL_LINES (w) == 0))
11072 return 0;
11073
11074 /* Set up an iterator for the tool-bar window. */
11075 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11076 it.first_visible_x = 0;
11077 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11078 row = it.glyph_row;
11079
11080 /* Build a string that represents the contents of the tool-bar. */
11081 build_desired_tool_bar_string (f);
11082 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11083 /* FIXME: This should be controlled by a user option. But it
11084 doesn't make sense to have an R2L tool bar if the menu bar cannot
11085 be drawn also R2L, and making the menu bar R2L is tricky due
11086 toolkit-specific code that implements it. If an R2L tool bar is
11087 ever supported, display_tool_bar_line should also be augmented to
11088 call unproduce_glyphs like display_line and display_string
11089 do. */
11090 it.paragraph_embedding = L2R;
11091
11092 if (f->n_tool_bar_rows == 0)
11093 {
11094 int nlines;
11095
11096 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11097 nlines != WINDOW_TOTAL_LINES (w)))
11098 {
11099 Lisp_Object frame;
11100 int old_height = WINDOW_TOTAL_LINES (w);
11101
11102 XSETFRAME (frame, f);
11103 Fmodify_frame_parameters (frame,
11104 Fcons (Fcons (Qtool_bar_lines,
11105 make_number (nlines)),
11106 Qnil));
11107 if (WINDOW_TOTAL_LINES (w) != old_height)
11108 {
11109 clear_glyph_matrix (w->desired_matrix);
11110 fonts_changed_p = 1;
11111 return 1;
11112 }
11113 }
11114 }
11115
11116 /* Display as many lines as needed to display all tool-bar items. */
11117
11118 if (f->n_tool_bar_rows > 0)
11119 {
11120 int border, rows, height, extra;
11121
11122 if (INTEGERP (Vtool_bar_border))
11123 border = XINT (Vtool_bar_border);
11124 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11125 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11126 else if (EQ (Vtool_bar_border, Qborder_width))
11127 border = f->border_width;
11128 else
11129 border = 0;
11130 if (border < 0)
11131 border = 0;
11132
11133 rows = f->n_tool_bar_rows;
11134 height = max (1, (it.last_visible_y - border) / rows);
11135 extra = it.last_visible_y - border - height * rows;
11136
11137 while (it.current_y < it.last_visible_y)
11138 {
11139 int h = 0;
11140 if (extra > 0 && rows-- > 0)
11141 {
11142 h = (extra + rows - 1) / rows;
11143 extra -= h;
11144 }
11145 display_tool_bar_line (&it, height + h);
11146 }
11147 }
11148 else
11149 {
11150 while (it.current_y < it.last_visible_y)
11151 display_tool_bar_line (&it, 0);
11152 }
11153
11154 /* It doesn't make much sense to try scrolling in the tool-bar
11155 window, so don't do it. */
11156 w->desired_matrix->no_scrolling_p = 1;
11157 w->must_be_updated_p = 1;
11158
11159 if (!NILP (Vauto_resize_tool_bars))
11160 {
11161 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11162 int change_height_p = 0;
11163
11164 /* If we couldn't display everything, change the tool-bar's
11165 height if there is room for more. */
11166 if (IT_STRING_CHARPOS (it) < it.end_charpos
11167 && it.current_y < max_tool_bar_height)
11168 change_height_p = 1;
11169
11170 row = it.glyph_row - 1;
11171
11172 /* If there are blank lines at the end, except for a partially
11173 visible blank line at the end that is smaller than
11174 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11175 if (!row->displays_text_p
11176 && row->height >= FRAME_LINE_HEIGHT (f))
11177 change_height_p = 1;
11178
11179 /* If row displays tool-bar items, but is partially visible,
11180 change the tool-bar's height. */
11181 if (row->displays_text_p
11182 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11183 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11184 change_height_p = 1;
11185
11186 /* Resize windows as needed by changing the `tool-bar-lines'
11187 frame parameter. */
11188 if (change_height_p)
11189 {
11190 Lisp_Object frame;
11191 int old_height = WINDOW_TOTAL_LINES (w);
11192 int nrows;
11193 int nlines = tool_bar_lines_needed (f, &nrows);
11194
11195 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11196 && !f->minimize_tool_bar_window_p)
11197 ? (nlines > old_height)
11198 : (nlines != old_height));
11199 f->minimize_tool_bar_window_p = 0;
11200
11201 if (change_height_p)
11202 {
11203 XSETFRAME (frame, f);
11204 Fmodify_frame_parameters (frame,
11205 Fcons (Fcons (Qtool_bar_lines,
11206 make_number (nlines)),
11207 Qnil));
11208 if (WINDOW_TOTAL_LINES (w) != old_height)
11209 {
11210 clear_glyph_matrix (w->desired_matrix);
11211 f->n_tool_bar_rows = nrows;
11212 fonts_changed_p = 1;
11213 return 1;
11214 }
11215 }
11216 }
11217 }
11218
11219 f->minimize_tool_bar_window_p = 0;
11220 return 0;
11221 }
11222
11223
11224 /* Get information about the tool-bar item which is displayed in GLYPH
11225 on frame F. Return in *PROP_IDX the index where tool-bar item
11226 properties start in F->tool_bar_items. Value is zero if
11227 GLYPH doesn't display a tool-bar item. */
11228
11229 static int
11230 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11231 {
11232 Lisp_Object prop;
11233 int success_p;
11234 int charpos;
11235
11236 /* This function can be called asynchronously, which means we must
11237 exclude any possibility that Fget_text_property signals an
11238 error. */
11239 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11240 charpos = max (0, charpos);
11241
11242 /* Get the text property `menu-item' at pos. The value of that
11243 property is the start index of this item's properties in
11244 F->tool_bar_items. */
11245 prop = Fget_text_property (make_number (charpos),
11246 Qmenu_item, f->current_tool_bar_string);
11247 if (INTEGERP (prop))
11248 {
11249 *prop_idx = XINT (prop);
11250 success_p = 1;
11251 }
11252 else
11253 success_p = 0;
11254
11255 return success_p;
11256 }
11257
11258 \f
11259 /* Get information about the tool-bar item at position X/Y on frame F.
11260 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11261 the current matrix of the tool-bar window of F, or NULL if not
11262 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11263 item in F->tool_bar_items. Value is
11264
11265 -1 if X/Y is not on a tool-bar item
11266 0 if X/Y is on the same item that was highlighted before.
11267 1 otherwise. */
11268
11269 static int
11270 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11271 int *hpos, int *vpos, int *prop_idx)
11272 {
11273 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11274 struct window *w = XWINDOW (f->tool_bar_window);
11275 int area;
11276
11277 /* Find the glyph under X/Y. */
11278 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11279 if (*glyph == NULL)
11280 return -1;
11281
11282 /* Get the start of this tool-bar item's properties in
11283 f->tool_bar_items. */
11284 if (!tool_bar_item_info (f, *glyph, prop_idx))
11285 return -1;
11286
11287 /* Is mouse on the highlighted item? */
11288 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11289 && *vpos >= hlinfo->mouse_face_beg_row
11290 && *vpos <= hlinfo->mouse_face_end_row
11291 && (*vpos > hlinfo->mouse_face_beg_row
11292 || *hpos >= hlinfo->mouse_face_beg_col)
11293 && (*vpos < hlinfo->mouse_face_end_row
11294 || *hpos < hlinfo->mouse_face_end_col
11295 || hlinfo->mouse_face_past_end))
11296 return 0;
11297
11298 return 1;
11299 }
11300
11301
11302 /* EXPORT:
11303 Handle mouse button event on the tool-bar of frame F, at
11304 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11305 0 for button release. MODIFIERS is event modifiers for button
11306 release. */
11307
11308 void
11309 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11310 unsigned int modifiers)
11311 {
11312 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11313 struct window *w = XWINDOW (f->tool_bar_window);
11314 int hpos, vpos, prop_idx;
11315 struct glyph *glyph;
11316 Lisp_Object enabled_p;
11317
11318 /* If not on the highlighted tool-bar item, return. */
11319 frame_to_window_pixel_xy (w, &x, &y);
11320 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11321 return;
11322
11323 /* If item is disabled, do nothing. */
11324 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11325 if (NILP (enabled_p))
11326 return;
11327
11328 if (down_p)
11329 {
11330 /* Show item in pressed state. */
11331 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11332 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11333 last_tool_bar_item = prop_idx;
11334 }
11335 else
11336 {
11337 Lisp_Object key, frame;
11338 struct input_event event;
11339 EVENT_INIT (event);
11340
11341 /* Show item in released state. */
11342 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11343 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11344
11345 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11346
11347 XSETFRAME (frame, f);
11348 event.kind = TOOL_BAR_EVENT;
11349 event.frame_or_window = frame;
11350 event.arg = frame;
11351 kbd_buffer_store_event (&event);
11352
11353 event.kind = TOOL_BAR_EVENT;
11354 event.frame_or_window = frame;
11355 event.arg = key;
11356 event.modifiers = modifiers;
11357 kbd_buffer_store_event (&event);
11358 last_tool_bar_item = -1;
11359 }
11360 }
11361
11362
11363 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11364 tool-bar window-relative coordinates X/Y. Called from
11365 note_mouse_highlight. */
11366
11367 static void
11368 note_tool_bar_highlight (struct frame *f, int x, int y)
11369 {
11370 Lisp_Object window = f->tool_bar_window;
11371 struct window *w = XWINDOW (window);
11372 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11373 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11374 int hpos, vpos;
11375 struct glyph *glyph;
11376 struct glyph_row *row;
11377 int i;
11378 Lisp_Object enabled_p;
11379 int prop_idx;
11380 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11381 int mouse_down_p, rc;
11382
11383 /* Function note_mouse_highlight is called with negative X/Y
11384 values when mouse moves outside of the frame. */
11385 if (x <= 0 || y <= 0)
11386 {
11387 clear_mouse_face (hlinfo);
11388 return;
11389 }
11390
11391 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11392 if (rc < 0)
11393 {
11394 /* Not on tool-bar item. */
11395 clear_mouse_face (hlinfo);
11396 return;
11397 }
11398 else if (rc == 0)
11399 /* On same tool-bar item as before. */
11400 goto set_help_echo;
11401
11402 clear_mouse_face (hlinfo);
11403
11404 /* Mouse is down, but on different tool-bar item? */
11405 mouse_down_p = (dpyinfo->grabbed
11406 && f == last_mouse_frame
11407 && FRAME_LIVE_P (f));
11408 if (mouse_down_p
11409 && last_tool_bar_item != prop_idx)
11410 return;
11411
11412 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11413 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11414
11415 /* If tool-bar item is not enabled, don't highlight it. */
11416 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11417 if (!NILP (enabled_p))
11418 {
11419 /* Compute the x-position of the glyph. In front and past the
11420 image is a space. We include this in the highlighted area. */
11421 row = MATRIX_ROW (w->current_matrix, vpos);
11422 for (i = x = 0; i < hpos; ++i)
11423 x += row->glyphs[TEXT_AREA][i].pixel_width;
11424
11425 /* Record this as the current active region. */
11426 hlinfo->mouse_face_beg_col = hpos;
11427 hlinfo->mouse_face_beg_row = vpos;
11428 hlinfo->mouse_face_beg_x = x;
11429 hlinfo->mouse_face_beg_y = row->y;
11430 hlinfo->mouse_face_past_end = 0;
11431
11432 hlinfo->mouse_face_end_col = hpos + 1;
11433 hlinfo->mouse_face_end_row = vpos;
11434 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11435 hlinfo->mouse_face_end_y = row->y;
11436 hlinfo->mouse_face_window = window;
11437 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11438
11439 /* Display it as active. */
11440 show_mouse_face (hlinfo, draw);
11441 hlinfo->mouse_face_image_state = draw;
11442 }
11443
11444 set_help_echo:
11445
11446 /* Set help_echo_string to a help string to display for this tool-bar item.
11447 XTread_socket does the rest. */
11448 help_echo_object = help_echo_window = Qnil;
11449 help_echo_pos = -1;
11450 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11451 if (NILP (help_echo_string))
11452 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11453 }
11454
11455 #endif /* HAVE_WINDOW_SYSTEM */
11456
11457
11458 \f
11459 /************************************************************************
11460 Horizontal scrolling
11461 ************************************************************************/
11462
11463 static int hscroll_window_tree (Lisp_Object);
11464 static int hscroll_windows (Lisp_Object);
11465
11466 /* For all leaf windows in the window tree rooted at WINDOW, set their
11467 hscroll value so that PT is (i) visible in the window, and (ii) so
11468 that it is not within a certain margin at the window's left and
11469 right border. Value is non-zero if any window's hscroll has been
11470 changed. */
11471
11472 static int
11473 hscroll_window_tree (Lisp_Object window)
11474 {
11475 int hscrolled_p = 0;
11476 int hscroll_relative_p = FLOATP (Vhscroll_step);
11477 int hscroll_step_abs = 0;
11478 double hscroll_step_rel = 0;
11479
11480 if (hscroll_relative_p)
11481 {
11482 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11483 if (hscroll_step_rel < 0)
11484 {
11485 hscroll_relative_p = 0;
11486 hscroll_step_abs = 0;
11487 }
11488 }
11489 else if (INTEGERP (Vhscroll_step))
11490 {
11491 hscroll_step_abs = XINT (Vhscroll_step);
11492 if (hscroll_step_abs < 0)
11493 hscroll_step_abs = 0;
11494 }
11495 else
11496 hscroll_step_abs = 0;
11497
11498 while (WINDOWP (window))
11499 {
11500 struct window *w = XWINDOW (window);
11501
11502 if (WINDOWP (w->hchild))
11503 hscrolled_p |= hscroll_window_tree (w->hchild);
11504 else if (WINDOWP (w->vchild))
11505 hscrolled_p |= hscroll_window_tree (w->vchild);
11506 else if (w->cursor.vpos >= 0)
11507 {
11508 int h_margin;
11509 int text_area_width;
11510 struct glyph_row *current_cursor_row
11511 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11512 struct glyph_row *desired_cursor_row
11513 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11514 struct glyph_row *cursor_row
11515 = (desired_cursor_row->enabled_p
11516 ? desired_cursor_row
11517 : current_cursor_row);
11518
11519 text_area_width = window_box_width (w, TEXT_AREA);
11520
11521 /* Scroll when cursor is inside this scroll margin. */
11522 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11523
11524 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11525 && ((XFASTINT (w->hscroll)
11526 && w->cursor.x <= h_margin)
11527 || (cursor_row->enabled_p
11528 && cursor_row->truncated_on_right_p
11529 && (w->cursor.x >= text_area_width - h_margin))))
11530 {
11531 struct it it;
11532 int hscroll;
11533 struct buffer *saved_current_buffer;
11534 EMACS_INT pt;
11535 int wanted_x;
11536
11537 /* Find point in a display of infinite width. */
11538 saved_current_buffer = current_buffer;
11539 current_buffer = XBUFFER (w->buffer);
11540
11541 if (w == XWINDOW (selected_window))
11542 pt = PT;
11543 else
11544 {
11545 pt = marker_position (w->pointm);
11546 pt = max (BEGV, pt);
11547 pt = min (ZV, pt);
11548 }
11549
11550 /* Move iterator to pt starting at cursor_row->start in
11551 a line with infinite width. */
11552 init_to_row_start (&it, w, cursor_row);
11553 it.last_visible_x = INFINITY;
11554 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11555 current_buffer = saved_current_buffer;
11556
11557 /* Position cursor in window. */
11558 if (!hscroll_relative_p && hscroll_step_abs == 0)
11559 hscroll = max (0, (it.current_x
11560 - (ITERATOR_AT_END_OF_LINE_P (&it)
11561 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11562 : (text_area_width / 2))))
11563 / FRAME_COLUMN_WIDTH (it.f);
11564 else if (w->cursor.x >= text_area_width - h_margin)
11565 {
11566 if (hscroll_relative_p)
11567 wanted_x = text_area_width * (1 - hscroll_step_rel)
11568 - h_margin;
11569 else
11570 wanted_x = text_area_width
11571 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11572 - h_margin;
11573 hscroll
11574 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11575 }
11576 else
11577 {
11578 if (hscroll_relative_p)
11579 wanted_x = text_area_width * hscroll_step_rel
11580 + h_margin;
11581 else
11582 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11583 + h_margin;
11584 hscroll
11585 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11586 }
11587 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11588
11589 /* Don't call Fset_window_hscroll if value hasn't
11590 changed because it will prevent redisplay
11591 optimizations. */
11592 if (XFASTINT (w->hscroll) != hscroll)
11593 {
11594 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11595 w->hscroll = make_number (hscroll);
11596 hscrolled_p = 1;
11597 }
11598 }
11599 }
11600
11601 window = w->next;
11602 }
11603
11604 /* Value is non-zero if hscroll of any leaf window has been changed. */
11605 return hscrolled_p;
11606 }
11607
11608
11609 /* Set hscroll so that cursor is visible and not inside horizontal
11610 scroll margins for all windows in the tree rooted at WINDOW. See
11611 also hscroll_window_tree above. Value is non-zero if any window's
11612 hscroll has been changed. If it has, desired matrices on the frame
11613 of WINDOW are cleared. */
11614
11615 static int
11616 hscroll_windows (Lisp_Object window)
11617 {
11618 int hscrolled_p = hscroll_window_tree (window);
11619 if (hscrolled_p)
11620 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11621 return hscrolled_p;
11622 }
11623
11624
11625 \f
11626 /************************************************************************
11627 Redisplay
11628 ************************************************************************/
11629
11630 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11631 to a non-zero value. This is sometimes handy to have in a debugger
11632 session. */
11633
11634 #if GLYPH_DEBUG
11635
11636 /* First and last unchanged row for try_window_id. */
11637
11638 int debug_first_unchanged_at_end_vpos;
11639 int debug_last_unchanged_at_beg_vpos;
11640
11641 /* Delta vpos and y. */
11642
11643 int debug_dvpos, debug_dy;
11644
11645 /* Delta in characters and bytes for try_window_id. */
11646
11647 EMACS_INT debug_delta, debug_delta_bytes;
11648
11649 /* Values of window_end_pos and window_end_vpos at the end of
11650 try_window_id. */
11651
11652 EMACS_INT debug_end_vpos;
11653
11654 /* Append a string to W->desired_matrix->method. FMT is a printf
11655 format string. A1...A9 are a supplement for a variable-length
11656 argument list. If trace_redisplay_p is non-zero also printf the
11657 resulting string to stderr. */
11658
11659 static void
11660 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11661 struct window *w;
11662 char *fmt;
11663 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11664 {
11665 char buffer[512];
11666 char *method = w->desired_matrix->method;
11667 int len = strlen (method);
11668 int size = sizeof w->desired_matrix->method;
11669 int remaining = size - len - 1;
11670
11671 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11672 if (len && remaining)
11673 {
11674 method[len] = '|';
11675 --remaining, ++len;
11676 }
11677
11678 strncpy (method + len, buffer, remaining);
11679
11680 if (trace_redisplay_p)
11681 fprintf (stderr, "%p (%s): %s\n",
11682 w,
11683 ((BUFFERP (w->buffer)
11684 && STRINGP (XBUFFER (w->buffer)->name))
11685 ? SSDATA (XBUFFER (w->buffer)->name)
11686 : "no buffer"),
11687 buffer);
11688 }
11689
11690 #endif /* GLYPH_DEBUG */
11691
11692
11693 /* Value is non-zero if all changes in window W, which displays
11694 current_buffer, are in the text between START and END. START is a
11695 buffer position, END is given as a distance from Z. Used in
11696 redisplay_internal for display optimization. */
11697
11698 static INLINE int
11699 text_outside_line_unchanged_p (struct window *w,
11700 EMACS_INT start, EMACS_INT end)
11701 {
11702 int unchanged_p = 1;
11703
11704 /* If text or overlays have changed, see where. */
11705 if (XFASTINT (w->last_modified) < MODIFF
11706 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11707 {
11708 /* Gap in the line? */
11709 if (GPT < start || Z - GPT < end)
11710 unchanged_p = 0;
11711
11712 /* Changes start in front of the line, or end after it? */
11713 if (unchanged_p
11714 && (BEG_UNCHANGED < start - 1
11715 || END_UNCHANGED < end))
11716 unchanged_p = 0;
11717
11718 /* If selective display, can't optimize if changes start at the
11719 beginning of the line. */
11720 if (unchanged_p
11721 && INTEGERP (BVAR (current_buffer, selective_display))
11722 && XINT (BVAR (current_buffer, selective_display)) > 0
11723 && (BEG_UNCHANGED < start || GPT <= start))
11724 unchanged_p = 0;
11725
11726 /* If there are overlays at the start or end of the line, these
11727 may have overlay strings with newlines in them. A change at
11728 START, for instance, may actually concern the display of such
11729 overlay strings as well, and they are displayed on different
11730 lines. So, quickly rule out this case. (For the future, it
11731 might be desirable to implement something more telling than
11732 just BEG/END_UNCHANGED.) */
11733 if (unchanged_p)
11734 {
11735 if (BEG + BEG_UNCHANGED == start
11736 && overlay_touches_p (start))
11737 unchanged_p = 0;
11738 if (END_UNCHANGED == end
11739 && overlay_touches_p (Z - end))
11740 unchanged_p = 0;
11741 }
11742
11743 /* Under bidi reordering, adding or deleting a character in the
11744 beginning of a paragraph, before the first strong directional
11745 character, can change the base direction of the paragraph (unless
11746 the buffer specifies a fixed paragraph direction), which will
11747 require to redisplay the whole paragraph. It might be worthwhile
11748 to find the paragraph limits and widen the range of redisplayed
11749 lines to that, but for now just give up this optimization. */
11750 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11751 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11752 unchanged_p = 0;
11753 }
11754
11755 return unchanged_p;
11756 }
11757
11758
11759 /* Do a frame update, taking possible shortcuts into account. This is
11760 the main external entry point for redisplay.
11761
11762 If the last redisplay displayed an echo area message and that message
11763 is no longer requested, we clear the echo area or bring back the
11764 mini-buffer if that is in use. */
11765
11766 void
11767 redisplay (void)
11768 {
11769 redisplay_internal ();
11770 }
11771
11772
11773 static Lisp_Object
11774 overlay_arrow_string_or_property (Lisp_Object var)
11775 {
11776 Lisp_Object val;
11777
11778 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11779 return val;
11780
11781 return Voverlay_arrow_string;
11782 }
11783
11784 /* Return 1 if there are any overlay-arrows in current_buffer. */
11785 static int
11786 overlay_arrow_in_current_buffer_p (void)
11787 {
11788 Lisp_Object vlist;
11789
11790 for (vlist = Voverlay_arrow_variable_list;
11791 CONSP (vlist);
11792 vlist = XCDR (vlist))
11793 {
11794 Lisp_Object var = XCAR (vlist);
11795 Lisp_Object val;
11796
11797 if (!SYMBOLP (var))
11798 continue;
11799 val = find_symbol_value (var);
11800 if (MARKERP (val)
11801 && current_buffer == XMARKER (val)->buffer)
11802 return 1;
11803 }
11804 return 0;
11805 }
11806
11807
11808 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11809 has changed. */
11810
11811 static int
11812 overlay_arrows_changed_p (void)
11813 {
11814 Lisp_Object vlist;
11815
11816 for (vlist = Voverlay_arrow_variable_list;
11817 CONSP (vlist);
11818 vlist = XCDR (vlist))
11819 {
11820 Lisp_Object var = XCAR (vlist);
11821 Lisp_Object val, pstr;
11822
11823 if (!SYMBOLP (var))
11824 continue;
11825 val = find_symbol_value (var);
11826 if (!MARKERP (val))
11827 continue;
11828 if (! EQ (COERCE_MARKER (val),
11829 Fget (var, Qlast_arrow_position))
11830 || ! (pstr = overlay_arrow_string_or_property (var),
11831 EQ (pstr, Fget (var, Qlast_arrow_string))))
11832 return 1;
11833 }
11834 return 0;
11835 }
11836
11837 /* Mark overlay arrows to be updated on next redisplay. */
11838
11839 static void
11840 update_overlay_arrows (int up_to_date)
11841 {
11842 Lisp_Object vlist;
11843
11844 for (vlist = Voverlay_arrow_variable_list;
11845 CONSP (vlist);
11846 vlist = XCDR (vlist))
11847 {
11848 Lisp_Object var = XCAR (vlist);
11849
11850 if (!SYMBOLP (var))
11851 continue;
11852
11853 if (up_to_date > 0)
11854 {
11855 Lisp_Object val = find_symbol_value (var);
11856 Fput (var, Qlast_arrow_position,
11857 COERCE_MARKER (val));
11858 Fput (var, Qlast_arrow_string,
11859 overlay_arrow_string_or_property (var));
11860 }
11861 else if (up_to_date < 0
11862 || !NILP (Fget (var, Qlast_arrow_position)))
11863 {
11864 Fput (var, Qlast_arrow_position, Qt);
11865 Fput (var, Qlast_arrow_string, Qt);
11866 }
11867 }
11868 }
11869
11870
11871 /* Return overlay arrow string to display at row.
11872 Return integer (bitmap number) for arrow bitmap in left fringe.
11873 Return nil if no overlay arrow. */
11874
11875 static Lisp_Object
11876 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11877 {
11878 Lisp_Object vlist;
11879
11880 for (vlist = Voverlay_arrow_variable_list;
11881 CONSP (vlist);
11882 vlist = XCDR (vlist))
11883 {
11884 Lisp_Object var = XCAR (vlist);
11885 Lisp_Object val;
11886
11887 if (!SYMBOLP (var))
11888 continue;
11889
11890 val = find_symbol_value (var);
11891
11892 if (MARKERP (val)
11893 && current_buffer == XMARKER (val)->buffer
11894 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11895 {
11896 if (FRAME_WINDOW_P (it->f)
11897 /* FIXME: if ROW->reversed_p is set, this should test
11898 the right fringe, not the left one. */
11899 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11900 {
11901 #ifdef HAVE_WINDOW_SYSTEM
11902 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11903 {
11904 int fringe_bitmap;
11905 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11906 return make_number (fringe_bitmap);
11907 }
11908 #endif
11909 return make_number (-1); /* Use default arrow bitmap */
11910 }
11911 return overlay_arrow_string_or_property (var);
11912 }
11913 }
11914
11915 return Qnil;
11916 }
11917
11918 /* Return 1 if point moved out of or into a composition. Otherwise
11919 return 0. PREV_BUF and PREV_PT are the last point buffer and
11920 position. BUF and PT are the current point buffer and position. */
11921
11922 static int
11923 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11924 struct buffer *buf, EMACS_INT pt)
11925 {
11926 EMACS_INT start, end;
11927 Lisp_Object prop;
11928 Lisp_Object buffer;
11929
11930 XSETBUFFER (buffer, buf);
11931 /* Check a composition at the last point if point moved within the
11932 same buffer. */
11933 if (prev_buf == buf)
11934 {
11935 if (prev_pt == pt)
11936 /* Point didn't move. */
11937 return 0;
11938
11939 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11940 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11941 && COMPOSITION_VALID_P (start, end, prop)
11942 && start < prev_pt && end > prev_pt)
11943 /* The last point was within the composition. Return 1 iff
11944 point moved out of the composition. */
11945 return (pt <= start || pt >= end);
11946 }
11947
11948 /* Check a composition at the current point. */
11949 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11950 && find_composition (pt, -1, &start, &end, &prop, buffer)
11951 && COMPOSITION_VALID_P (start, end, prop)
11952 && start < pt && end > pt);
11953 }
11954
11955
11956 /* Reconsider the setting of B->clip_changed which is displayed
11957 in window W. */
11958
11959 static INLINE void
11960 reconsider_clip_changes (struct window *w, struct buffer *b)
11961 {
11962 if (b->clip_changed
11963 && !NILP (w->window_end_valid)
11964 && w->current_matrix->buffer == b
11965 && w->current_matrix->zv == BUF_ZV (b)
11966 && w->current_matrix->begv == BUF_BEGV (b))
11967 b->clip_changed = 0;
11968
11969 /* If display wasn't paused, and W is not a tool bar window, see if
11970 point has been moved into or out of a composition. In that case,
11971 we set b->clip_changed to 1 to force updating the screen. If
11972 b->clip_changed has already been set to 1, we can skip this
11973 check. */
11974 if (!b->clip_changed
11975 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11976 {
11977 EMACS_INT pt;
11978
11979 if (w == XWINDOW (selected_window))
11980 pt = PT;
11981 else
11982 pt = marker_position (w->pointm);
11983
11984 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11985 || pt != XINT (w->last_point))
11986 && check_point_in_composition (w->current_matrix->buffer,
11987 XINT (w->last_point),
11988 XBUFFER (w->buffer), pt))
11989 b->clip_changed = 1;
11990 }
11991 }
11992 \f
11993
11994 /* Select FRAME to forward the values of frame-local variables into C
11995 variables so that the redisplay routines can access those values
11996 directly. */
11997
11998 static void
11999 select_frame_for_redisplay (Lisp_Object frame)
12000 {
12001 Lisp_Object tail, tem;
12002 Lisp_Object old = selected_frame;
12003 struct Lisp_Symbol *sym;
12004
12005 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12006
12007 selected_frame = frame;
12008
12009 do {
12010 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12011 if (CONSP (XCAR (tail))
12012 && (tem = XCAR (XCAR (tail)),
12013 SYMBOLP (tem))
12014 && (sym = indirect_variable (XSYMBOL (tem)),
12015 sym->redirect == SYMBOL_LOCALIZED)
12016 && sym->val.blv->frame_local)
12017 /* Use find_symbol_value rather than Fsymbol_value
12018 to avoid an error if it is void. */
12019 find_symbol_value (tem);
12020 } while (!EQ (frame, old) && (frame = old, 1));
12021 }
12022
12023
12024 #define STOP_POLLING \
12025 do { if (! polling_stopped_here) stop_polling (); \
12026 polling_stopped_here = 1; } while (0)
12027
12028 #define RESUME_POLLING \
12029 do { if (polling_stopped_here) start_polling (); \
12030 polling_stopped_here = 0; } while (0)
12031
12032
12033 /* Perhaps in the future avoid recentering windows if it
12034 is not necessary; currently that causes some problems. */
12035
12036 static void
12037 redisplay_internal (void)
12038 {
12039 struct window *w = XWINDOW (selected_window);
12040 struct window *sw;
12041 struct frame *fr;
12042 int pending;
12043 int must_finish = 0;
12044 struct text_pos tlbufpos, tlendpos;
12045 int number_of_visible_frames;
12046 int count, count1;
12047 struct frame *sf;
12048 int polling_stopped_here = 0;
12049 Lisp_Object old_frame = selected_frame;
12050
12051 /* Non-zero means redisplay has to consider all windows on all
12052 frames. Zero means, only selected_window is considered. */
12053 int consider_all_windows_p;
12054
12055 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12056
12057 /* No redisplay if running in batch mode or frame is not yet fully
12058 initialized, or redisplay is explicitly turned off by setting
12059 Vinhibit_redisplay. */
12060 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12061 || !NILP (Vinhibit_redisplay))
12062 return;
12063
12064 /* Don't examine these until after testing Vinhibit_redisplay.
12065 When Emacs is shutting down, perhaps because its connection to
12066 X has dropped, we should not look at them at all. */
12067 fr = XFRAME (w->frame);
12068 sf = SELECTED_FRAME ();
12069
12070 if (!fr->glyphs_initialized_p)
12071 return;
12072
12073 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12074 if (popup_activated ())
12075 return;
12076 #endif
12077
12078 /* I don't think this happens but let's be paranoid. */
12079 if (redisplaying_p)
12080 return;
12081
12082 /* Record a function that resets redisplaying_p to its old value
12083 when we leave this function. */
12084 count = SPECPDL_INDEX ();
12085 record_unwind_protect (unwind_redisplay,
12086 Fcons (make_number (redisplaying_p), selected_frame));
12087 ++redisplaying_p;
12088 specbind (Qinhibit_free_realized_faces, Qnil);
12089
12090 {
12091 Lisp_Object tail, frame;
12092
12093 FOR_EACH_FRAME (tail, frame)
12094 {
12095 struct frame *f = XFRAME (frame);
12096 f->already_hscrolled_p = 0;
12097 }
12098 }
12099
12100 retry:
12101 /* Remember the currently selected window. */
12102 sw = w;
12103
12104 if (!EQ (old_frame, selected_frame)
12105 && FRAME_LIVE_P (XFRAME (old_frame)))
12106 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12107 selected_frame and selected_window to be temporarily out-of-sync so
12108 when we come back here via `goto retry', we need to resync because we
12109 may need to run Elisp code (via prepare_menu_bars). */
12110 select_frame_for_redisplay (old_frame);
12111
12112 pending = 0;
12113 reconsider_clip_changes (w, current_buffer);
12114 last_escape_glyph_frame = NULL;
12115 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12116 last_glyphless_glyph_frame = NULL;
12117 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12118
12119 /* If new fonts have been loaded that make a glyph matrix adjustment
12120 necessary, do it. */
12121 if (fonts_changed_p)
12122 {
12123 adjust_glyphs (NULL);
12124 ++windows_or_buffers_changed;
12125 fonts_changed_p = 0;
12126 }
12127
12128 /* If face_change_count is non-zero, init_iterator will free all
12129 realized faces, which includes the faces referenced from current
12130 matrices. So, we can't reuse current matrices in this case. */
12131 if (face_change_count)
12132 ++windows_or_buffers_changed;
12133
12134 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12135 && FRAME_TTY (sf)->previous_frame != sf)
12136 {
12137 /* Since frames on a single ASCII terminal share the same
12138 display area, displaying a different frame means redisplay
12139 the whole thing. */
12140 windows_or_buffers_changed++;
12141 SET_FRAME_GARBAGED (sf);
12142 #ifndef DOS_NT
12143 set_tty_color_mode (FRAME_TTY (sf), sf);
12144 #endif
12145 FRAME_TTY (sf)->previous_frame = sf;
12146 }
12147
12148 /* Set the visible flags for all frames. Do this before checking
12149 for resized or garbaged frames; they want to know if their frames
12150 are visible. See the comment in frame.h for
12151 FRAME_SAMPLE_VISIBILITY. */
12152 {
12153 Lisp_Object tail, frame;
12154
12155 number_of_visible_frames = 0;
12156
12157 FOR_EACH_FRAME (tail, frame)
12158 {
12159 struct frame *f = XFRAME (frame);
12160
12161 FRAME_SAMPLE_VISIBILITY (f);
12162 if (FRAME_VISIBLE_P (f))
12163 ++number_of_visible_frames;
12164 clear_desired_matrices (f);
12165 }
12166 }
12167
12168 /* Notice any pending interrupt request to change frame size. */
12169 do_pending_window_change (1);
12170
12171 /* do_pending_window_change could change the selected_window due to
12172 frame resizing which makes the selected window too small. */
12173 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12174 {
12175 sw = w;
12176 reconsider_clip_changes (w, current_buffer);
12177 }
12178
12179 /* Clear frames marked as garbaged. */
12180 if (frame_garbaged)
12181 clear_garbaged_frames ();
12182
12183 /* Build menubar and tool-bar items. */
12184 if (NILP (Vmemory_full))
12185 prepare_menu_bars ();
12186
12187 if (windows_or_buffers_changed)
12188 update_mode_lines++;
12189
12190 /* Detect case that we need to write or remove a star in the mode line. */
12191 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12192 {
12193 w->update_mode_line = Qt;
12194 if (buffer_shared > 1)
12195 update_mode_lines++;
12196 }
12197
12198 /* Avoid invocation of point motion hooks by `current_column' below. */
12199 count1 = SPECPDL_INDEX ();
12200 specbind (Qinhibit_point_motion_hooks, Qt);
12201
12202 /* If %c is in the mode line, update it if needed. */
12203 if (!NILP (w->column_number_displayed)
12204 /* This alternative quickly identifies a common case
12205 where no change is needed. */
12206 && !(PT == XFASTINT (w->last_point)
12207 && XFASTINT (w->last_modified) >= MODIFF
12208 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12209 && (XFASTINT (w->column_number_displayed) != current_column ()))
12210 w->update_mode_line = Qt;
12211
12212 unbind_to (count1, Qnil);
12213
12214 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12215
12216 /* The variable buffer_shared is set in redisplay_window and
12217 indicates that we redisplay a buffer in different windows. See
12218 there. */
12219 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12220 || cursor_type_changed);
12221
12222 /* If specs for an arrow have changed, do thorough redisplay
12223 to ensure we remove any arrow that should no longer exist. */
12224 if (overlay_arrows_changed_p ())
12225 consider_all_windows_p = windows_or_buffers_changed = 1;
12226
12227 /* Normally the message* functions will have already displayed and
12228 updated the echo area, but the frame may have been trashed, or
12229 the update may have been preempted, so display the echo area
12230 again here. Checking message_cleared_p captures the case that
12231 the echo area should be cleared. */
12232 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12233 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12234 || (message_cleared_p
12235 && minibuf_level == 0
12236 /* If the mini-window is currently selected, this means the
12237 echo-area doesn't show through. */
12238 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12239 {
12240 int window_height_changed_p = echo_area_display (0);
12241 must_finish = 1;
12242
12243 /* If we don't display the current message, don't clear the
12244 message_cleared_p flag, because, if we did, we wouldn't clear
12245 the echo area in the next redisplay which doesn't preserve
12246 the echo area. */
12247 if (!display_last_displayed_message_p)
12248 message_cleared_p = 0;
12249
12250 if (fonts_changed_p)
12251 goto retry;
12252 else if (window_height_changed_p)
12253 {
12254 consider_all_windows_p = 1;
12255 ++update_mode_lines;
12256 ++windows_or_buffers_changed;
12257
12258 /* If window configuration was changed, frames may have been
12259 marked garbaged. Clear them or we will experience
12260 surprises wrt scrolling. */
12261 if (frame_garbaged)
12262 clear_garbaged_frames ();
12263 }
12264 }
12265 else if (EQ (selected_window, minibuf_window)
12266 && (current_buffer->clip_changed
12267 || XFASTINT (w->last_modified) < MODIFF
12268 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12269 && resize_mini_window (w, 0))
12270 {
12271 /* Resized active mini-window to fit the size of what it is
12272 showing if its contents might have changed. */
12273 must_finish = 1;
12274 /* FIXME: this causes all frames to be updated, which seems unnecessary
12275 since only the current frame needs to be considered. This function needs
12276 to be rewritten with two variables, consider_all_windows and
12277 consider_all_frames. */
12278 consider_all_windows_p = 1;
12279 ++windows_or_buffers_changed;
12280 ++update_mode_lines;
12281
12282 /* If window configuration was changed, frames may have been
12283 marked garbaged. Clear them or we will experience
12284 surprises wrt scrolling. */
12285 if (frame_garbaged)
12286 clear_garbaged_frames ();
12287 }
12288
12289
12290 /* If showing the region, and mark has changed, we must redisplay
12291 the whole window. The assignment to this_line_start_pos prevents
12292 the optimization directly below this if-statement. */
12293 if (((!NILP (Vtransient_mark_mode)
12294 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12295 != !NILP (w->region_showing))
12296 || (!NILP (w->region_showing)
12297 && !EQ (w->region_showing,
12298 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12299 CHARPOS (this_line_start_pos) = 0;
12300
12301 /* Optimize the case that only the line containing the cursor in the
12302 selected window has changed. Variables starting with this_ are
12303 set in display_line and record information about the line
12304 containing the cursor. */
12305 tlbufpos = this_line_start_pos;
12306 tlendpos = this_line_end_pos;
12307 if (!consider_all_windows_p
12308 && CHARPOS (tlbufpos) > 0
12309 && NILP (w->update_mode_line)
12310 && !current_buffer->clip_changed
12311 && !current_buffer->prevent_redisplay_optimizations_p
12312 && FRAME_VISIBLE_P (XFRAME (w->frame))
12313 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12314 /* Make sure recorded data applies to current buffer, etc. */
12315 && this_line_buffer == current_buffer
12316 && current_buffer == XBUFFER (w->buffer)
12317 && NILP (w->force_start)
12318 && NILP (w->optional_new_start)
12319 /* Point must be on the line that we have info recorded about. */
12320 && PT >= CHARPOS (tlbufpos)
12321 && PT <= Z - CHARPOS (tlendpos)
12322 /* All text outside that line, including its final newline,
12323 must be unchanged. */
12324 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12325 CHARPOS (tlendpos)))
12326 {
12327 if (CHARPOS (tlbufpos) > BEGV
12328 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12329 && (CHARPOS (tlbufpos) == ZV
12330 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12331 /* Former continuation line has disappeared by becoming empty. */
12332 goto cancel;
12333 else if (XFASTINT (w->last_modified) < MODIFF
12334 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12335 || MINI_WINDOW_P (w))
12336 {
12337 /* We have to handle the case of continuation around a
12338 wide-column character (see the comment in indent.c around
12339 line 1340).
12340
12341 For instance, in the following case:
12342
12343 -------- Insert --------
12344 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12345 J_I_ ==> J_I_ `^^' are cursors.
12346 ^^ ^^
12347 -------- --------
12348
12349 As we have to redraw the line above, we cannot use this
12350 optimization. */
12351
12352 struct it it;
12353 int line_height_before = this_line_pixel_height;
12354
12355 /* Note that start_display will handle the case that the
12356 line starting at tlbufpos is a continuation line. */
12357 start_display (&it, w, tlbufpos);
12358
12359 /* Implementation note: It this still necessary? */
12360 if (it.current_x != this_line_start_x)
12361 goto cancel;
12362
12363 TRACE ((stderr, "trying display optimization 1\n"));
12364 w->cursor.vpos = -1;
12365 overlay_arrow_seen = 0;
12366 it.vpos = this_line_vpos;
12367 it.current_y = this_line_y;
12368 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12369 display_line (&it);
12370
12371 /* If line contains point, is not continued,
12372 and ends at same distance from eob as before, we win. */
12373 if (w->cursor.vpos >= 0
12374 /* Line is not continued, otherwise this_line_start_pos
12375 would have been set to 0 in display_line. */
12376 && CHARPOS (this_line_start_pos)
12377 /* Line ends as before. */
12378 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12379 /* Line has same height as before. Otherwise other lines
12380 would have to be shifted up or down. */
12381 && this_line_pixel_height == line_height_before)
12382 {
12383 /* If this is not the window's last line, we must adjust
12384 the charstarts of the lines below. */
12385 if (it.current_y < it.last_visible_y)
12386 {
12387 struct glyph_row *row
12388 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12389 EMACS_INT delta, delta_bytes;
12390
12391 /* We used to distinguish between two cases here,
12392 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12393 when the line ends in a newline or the end of the
12394 buffer's accessible portion. But both cases did
12395 the same, so they were collapsed. */
12396 delta = (Z
12397 - CHARPOS (tlendpos)
12398 - MATRIX_ROW_START_CHARPOS (row));
12399 delta_bytes = (Z_BYTE
12400 - BYTEPOS (tlendpos)
12401 - MATRIX_ROW_START_BYTEPOS (row));
12402
12403 increment_matrix_positions (w->current_matrix,
12404 this_line_vpos + 1,
12405 w->current_matrix->nrows,
12406 delta, delta_bytes);
12407 }
12408
12409 /* If this row displays text now but previously didn't,
12410 or vice versa, w->window_end_vpos may have to be
12411 adjusted. */
12412 if ((it.glyph_row - 1)->displays_text_p)
12413 {
12414 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12415 XSETINT (w->window_end_vpos, this_line_vpos);
12416 }
12417 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12418 && this_line_vpos > 0)
12419 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12420 w->window_end_valid = Qnil;
12421
12422 /* Update hint: No need to try to scroll in update_window. */
12423 w->desired_matrix->no_scrolling_p = 1;
12424
12425 #if GLYPH_DEBUG
12426 *w->desired_matrix->method = 0;
12427 debug_method_add (w, "optimization 1");
12428 #endif
12429 #ifdef HAVE_WINDOW_SYSTEM
12430 update_window_fringes (w, 0);
12431 #endif
12432 goto update;
12433 }
12434 else
12435 goto cancel;
12436 }
12437 else if (/* Cursor position hasn't changed. */
12438 PT == XFASTINT (w->last_point)
12439 /* Make sure the cursor was last displayed
12440 in this window. Otherwise we have to reposition it. */
12441 && 0 <= w->cursor.vpos
12442 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12443 {
12444 if (!must_finish)
12445 {
12446 do_pending_window_change (1);
12447 /* If selected_window changed, redisplay again. */
12448 if (WINDOWP (selected_window)
12449 && (w = XWINDOW (selected_window)) != sw)
12450 goto retry;
12451
12452 /* We used to always goto end_of_redisplay here, but this
12453 isn't enough if we have a blinking cursor. */
12454 if (w->cursor_off_p == w->last_cursor_off_p)
12455 goto end_of_redisplay;
12456 }
12457 goto update;
12458 }
12459 /* If highlighting the region, or if the cursor is in the echo area,
12460 then we can't just move the cursor. */
12461 else if (! (!NILP (Vtransient_mark_mode)
12462 && !NILP (BVAR (current_buffer, mark_active)))
12463 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12464 || highlight_nonselected_windows)
12465 && NILP (w->region_showing)
12466 && NILP (Vshow_trailing_whitespace)
12467 && !cursor_in_echo_area)
12468 {
12469 struct it it;
12470 struct glyph_row *row;
12471
12472 /* Skip from tlbufpos to PT and see where it is. Note that
12473 PT may be in invisible text. If so, we will end at the
12474 next visible position. */
12475 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12476 NULL, DEFAULT_FACE_ID);
12477 it.current_x = this_line_start_x;
12478 it.current_y = this_line_y;
12479 it.vpos = this_line_vpos;
12480
12481 /* The call to move_it_to stops in front of PT, but
12482 moves over before-strings. */
12483 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12484
12485 if (it.vpos == this_line_vpos
12486 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12487 row->enabled_p))
12488 {
12489 xassert (this_line_vpos == it.vpos);
12490 xassert (this_line_y == it.current_y);
12491 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12492 #if GLYPH_DEBUG
12493 *w->desired_matrix->method = 0;
12494 debug_method_add (w, "optimization 3");
12495 #endif
12496 goto update;
12497 }
12498 else
12499 goto cancel;
12500 }
12501
12502 cancel:
12503 /* Text changed drastically or point moved off of line. */
12504 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12505 }
12506
12507 CHARPOS (this_line_start_pos) = 0;
12508 consider_all_windows_p |= buffer_shared > 1;
12509 ++clear_face_cache_count;
12510 #ifdef HAVE_WINDOW_SYSTEM
12511 ++clear_image_cache_count;
12512 #endif
12513
12514 /* Build desired matrices, and update the display. If
12515 consider_all_windows_p is non-zero, do it for all windows on all
12516 frames. Otherwise do it for selected_window, only. */
12517
12518 if (consider_all_windows_p)
12519 {
12520 Lisp_Object tail, frame;
12521
12522 FOR_EACH_FRAME (tail, frame)
12523 XFRAME (frame)->updated_p = 0;
12524
12525 /* Recompute # windows showing selected buffer. This will be
12526 incremented each time such a window is displayed. */
12527 buffer_shared = 0;
12528
12529 FOR_EACH_FRAME (tail, frame)
12530 {
12531 struct frame *f = XFRAME (frame);
12532
12533 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12534 {
12535 if (! EQ (frame, selected_frame))
12536 /* Select the frame, for the sake of frame-local
12537 variables. */
12538 select_frame_for_redisplay (frame);
12539
12540 /* Mark all the scroll bars to be removed; we'll redeem
12541 the ones we want when we redisplay their windows. */
12542 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12543 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12544
12545 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12546 redisplay_windows (FRAME_ROOT_WINDOW (f));
12547
12548 /* The X error handler may have deleted that frame. */
12549 if (!FRAME_LIVE_P (f))
12550 continue;
12551
12552 /* Any scroll bars which redisplay_windows should have
12553 nuked should now go away. */
12554 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12555 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12556
12557 /* If fonts changed, display again. */
12558 /* ??? rms: I suspect it is a mistake to jump all the way
12559 back to retry here. It should just retry this frame. */
12560 if (fonts_changed_p)
12561 goto retry;
12562
12563 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12564 {
12565 /* See if we have to hscroll. */
12566 if (!f->already_hscrolled_p)
12567 {
12568 f->already_hscrolled_p = 1;
12569 if (hscroll_windows (f->root_window))
12570 goto retry;
12571 }
12572
12573 /* Prevent various kinds of signals during display
12574 update. stdio is not robust about handling
12575 signals, which can cause an apparent I/O
12576 error. */
12577 if (interrupt_input)
12578 unrequest_sigio ();
12579 STOP_POLLING;
12580
12581 /* Update the display. */
12582 set_window_update_flags (XWINDOW (f->root_window), 1);
12583 pending |= update_frame (f, 0, 0);
12584 f->updated_p = 1;
12585 }
12586 }
12587 }
12588
12589 if (!EQ (old_frame, selected_frame)
12590 && FRAME_LIVE_P (XFRAME (old_frame)))
12591 /* We played a bit fast-and-loose above and allowed selected_frame
12592 and selected_window to be temporarily out-of-sync but let's make
12593 sure this stays contained. */
12594 select_frame_for_redisplay (old_frame);
12595 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12596
12597 if (!pending)
12598 {
12599 /* Do the mark_window_display_accurate after all windows have
12600 been redisplayed because this call resets flags in buffers
12601 which are needed for proper redisplay. */
12602 FOR_EACH_FRAME (tail, frame)
12603 {
12604 struct frame *f = XFRAME (frame);
12605 if (f->updated_p)
12606 {
12607 mark_window_display_accurate (f->root_window, 1);
12608 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12609 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12610 }
12611 }
12612 }
12613 }
12614 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12615 {
12616 Lisp_Object mini_window;
12617 struct frame *mini_frame;
12618
12619 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12620 /* Use list_of_error, not Qerror, so that
12621 we catch only errors and don't run the debugger. */
12622 internal_condition_case_1 (redisplay_window_1, selected_window,
12623 list_of_error,
12624 redisplay_window_error);
12625
12626 /* Compare desired and current matrices, perform output. */
12627
12628 update:
12629 /* If fonts changed, display again. */
12630 if (fonts_changed_p)
12631 goto retry;
12632
12633 /* Prevent various kinds of signals during display update.
12634 stdio is not robust about handling signals,
12635 which can cause an apparent I/O error. */
12636 if (interrupt_input)
12637 unrequest_sigio ();
12638 STOP_POLLING;
12639
12640 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12641 {
12642 if (hscroll_windows (selected_window))
12643 goto retry;
12644
12645 XWINDOW (selected_window)->must_be_updated_p = 1;
12646 pending = update_frame (sf, 0, 0);
12647 }
12648
12649 /* We may have called echo_area_display at the top of this
12650 function. If the echo area is on another frame, that may
12651 have put text on a frame other than the selected one, so the
12652 above call to update_frame would not have caught it. Catch
12653 it here. */
12654 mini_window = FRAME_MINIBUF_WINDOW (sf);
12655 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12656
12657 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12658 {
12659 XWINDOW (mini_window)->must_be_updated_p = 1;
12660 pending |= update_frame (mini_frame, 0, 0);
12661 if (!pending && hscroll_windows (mini_window))
12662 goto retry;
12663 }
12664 }
12665
12666 /* If display was paused because of pending input, make sure we do a
12667 thorough update the next time. */
12668 if (pending)
12669 {
12670 /* Prevent the optimization at the beginning of
12671 redisplay_internal that tries a single-line update of the
12672 line containing the cursor in the selected window. */
12673 CHARPOS (this_line_start_pos) = 0;
12674
12675 /* Let the overlay arrow be updated the next time. */
12676 update_overlay_arrows (0);
12677
12678 /* If we pause after scrolling, some rows in the current
12679 matrices of some windows are not valid. */
12680 if (!WINDOW_FULL_WIDTH_P (w)
12681 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12682 update_mode_lines = 1;
12683 }
12684 else
12685 {
12686 if (!consider_all_windows_p)
12687 {
12688 /* This has already been done above if
12689 consider_all_windows_p is set. */
12690 mark_window_display_accurate_1 (w, 1);
12691
12692 /* Say overlay arrows are up to date. */
12693 update_overlay_arrows (1);
12694
12695 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12696 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12697 }
12698
12699 update_mode_lines = 0;
12700 windows_or_buffers_changed = 0;
12701 cursor_type_changed = 0;
12702 }
12703
12704 /* Start SIGIO interrupts coming again. Having them off during the
12705 code above makes it less likely one will discard output, but not
12706 impossible, since there might be stuff in the system buffer here.
12707 But it is much hairier to try to do anything about that. */
12708 if (interrupt_input)
12709 request_sigio ();
12710 RESUME_POLLING;
12711
12712 /* If a frame has become visible which was not before, redisplay
12713 again, so that we display it. Expose events for such a frame
12714 (which it gets when becoming visible) don't call the parts of
12715 redisplay constructing glyphs, so simply exposing a frame won't
12716 display anything in this case. So, we have to display these
12717 frames here explicitly. */
12718 if (!pending)
12719 {
12720 Lisp_Object tail, frame;
12721 int new_count = 0;
12722
12723 FOR_EACH_FRAME (tail, frame)
12724 {
12725 int this_is_visible = 0;
12726
12727 if (XFRAME (frame)->visible)
12728 this_is_visible = 1;
12729 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12730 if (XFRAME (frame)->visible)
12731 this_is_visible = 1;
12732
12733 if (this_is_visible)
12734 new_count++;
12735 }
12736
12737 if (new_count != number_of_visible_frames)
12738 windows_or_buffers_changed++;
12739 }
12740
12741 /* Change frame size now if a change is pending. */
12742 do_pending_window_change (1);
12743
12744 /* If we just did a pending size change, or have additional
12745 visible frames, or selected_window changed, redisplay again. */
12746 if ((windows_or_buffers_changed && !pending)
12747 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12748 goto retry;
12749
12750 /* Clear the face and image caches.
12751
12752 We used to do this only if consider_all_windows_p. But the cache
12753 needs to be cleared if a timer creates images in the current
12754 buffer (e.g. the test case in Bug#6230). */
12755
12756 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12757 {
12758 clear_face_cache (0);
12759 clear_face_cache_count = 0;
12760 }
12761
12762 #ifdef HAVE_WINDOW_SYSTEM
12763 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12764 {
12765 clear_image_caches (Qnil);
12766 clear_image_cache_count = 0;
12767 }
12768 #endif /* HAVE_WINDOW_SYSTEM */
12769
12770 end_of_redisplay:
12771 unbind_to (count, Qnil);
12772 RESUME_POLLING;
12773 }
12774
12775
12776 /* Redisplay, but leave alone any recent echo area message unless
12777 another message has been requested in its place.
12778
12779 This is useful in situations where you need to redisplay but no
12780 user action has occurred, making it inappropriate for the message
12781 area to be cleared. See tracking_off and
12782 wait_reading_process_output for examples of these situations.
12783
12784 FROM_WHERE is an integer saying from where this function was
12785 called. This is useful for debugging. */
12786
12787 void
12788 redisplay_preserve_echo_area (int from_where)
12789 {
12790 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12791
12792 if (!NILP (echo_area_buffer[1]))
12793 {
12794 /* We have a previously displayed message, but no current
12795 message. Redisplay the previous message. */
12796 display_last_displayed_message_p = 1;
12797 redisplay_internal ();
12798 display_last_displayed_message_p = 0;
12799 }
12800 else
12801 redisplay_internal ();
12802
12803 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12804 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12805 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12806 }
12807
12808
12809 /* Function registered with record_unwind_protect in
12810 redisplay_internal. Reset redisplaying_p to the value it had
12811 before redisplay_internal was called, and clear
12812 prevent_freeing_realized_faces_p. It also selects the previously
12813 selected frame, unless it has been deleted (by an X connection
12814 failure during redisplay, for example). */
12815
12816 static Lisp_Object
12817 unwind_redisplay (Lisp_Object val)
12818 {
12819 Lisp_Object old_redisplaying_p, old_frame;
12820
12821 old_redisplaying_p = XCAR (val);
12822 redisplaying_p = XFASTINT (old_redisplaying_p);
12823 old_frame = XCDR (val);
12824 if (! EQ (old_frame, selected_frame)
12825 && FRAME_LIVE_P (XFRAME (old_frame)))
12826 select_frame_for_redisplay (old_frame);
12827 return Qnil;
12828 }
12829
12830
12831 /* Mark the display of window W as accurate or inaccurate. If
12832 ACCURATE_P is non-zero mark display of W as accurate. If
12833 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12834 redisplay_internal is called. */
12835
12836 static void
12837 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12838 {
12839 if (BUFFERP (w->buffer))
12840 {
12841 struct buffer *b = XBUFFER (w->buffer);
12842
12843 w->last_modified
12844 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12845 w->last_overlay_modified
12846 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12847 w->last_had_star
12848 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12849
12850 if (accurate_p)
12851 {
12852 b->clip_changed = 0;
12853 b->prevent_redisplay_optimizations_p = 0;
12854
12855 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12856 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12857 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12858 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12859
12860 w->current_matrix->buffer = b;
12861 w->current_matrix->begv = BUF_BEGV (b);
12862 w->current_matrix->zv = BUF_ZV (b);
12863
12864 w->last_cursor = w->cursor;
12865 w->last_cursor_off_p = w->cursor_off_p;
12866
12867 if (w == XWINDOW (selected_window))
12868 w->last_point = make_number (BUF_PT (b));
12869 else
12870 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12871 }
12872 }
12873
12874 if (accurate_p)
12875 {
12876 w->window_end_valid = w->buffer;
12877 w->update_mode_line = Qnil;
12878 }
12879 }
12880
12881
12882 /* Mark the display of windows in the window tree rooted at WINDOW as
12883 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12884 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12885 be redisplayed the next time redisplay_internal is called. */
12886
12887 void
12888 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12889 {
12890 struct window *w;
12891
12892 for (; !NILP (window); window = w->next)
12893 {
12894 w = XWINDOW (window);
12895 mark_window_display_accurate_1 (w, accurate_p);
12896
12897 if (!NILP (w->vchild))
12898 mark_window_display_accurate (w->vchild, accurate_p);
12899 if (!NILP (w->hchild))
12900 mark_window_display_accurate (w->hchild, accurate_p);
12901 }
12902
12903 if (accurate_p)
12904 {
12905 update_overlay_arrows (1);
12906 }
12907 else
12908 {
12909 /* Force a thorough redisplay the next time by setting
12910 last_arrow_position and last_arrow_string to t, which is
12911 unequal to any useful value of Voverlay_arrow_... */
12912 update_overlay_arrows (-1);
12913 }
12914 }
12915
12916
12917 /* Return value in display table DP (Lisp_Char_Table *) for character
12918 C. Since a display table doesn't have any parent, we don't have to
12919 follow parent. Do not call this function directly but use the
12920 macro DISP_CHAR_VECTOR. */
12921
12922 Lisp_Object
12923 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12924 {
12925 Lisp_Object val;
12926
12927 if (ASCII_CHAR_P (c))
12928 {
12929 val = dp->ascii;
12930 if (SUB_CHAR_TABLE_P (val))
12931 val = XSUB_CHAR_TABLE (val)->contents[c];
12932 }
12933 else
12934 {
12935 Lisp_Object table;
12936
12937 XSETCHAR_TABLE (table, dp);
12938 val = char_table_ref (table, c);
12939 }
12940 if (NILP (val))
12941 val = dp->defalt;
12942 return val;
12943 }
12944
12945
12946 \f
12947 /***********************************************************************
12948 Window Redisplay
12949 ***********************************************************************/
12950
12951 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12952
12953 static void
12954 redisplay_windows (Lisp_Object window)
12955 {
12956 while (!NILP (window))
12957 {
12958 struct window *w = XWINDOW (window);
12959
12960 if (!NILP (w->hchild))
12961 redisplay_windows (w->hchild);
12962 else if (!NILP (w->vchild))
12963 redisplay_windows (w->vchild);
12964 else if (!NILP (w->buffer))
12965 {
12966 displayed_buffer = XBUFFER (w->buffer);
12967 /* Use list_of_error, not Qerror, so that
12968 we catch only errors and don't run the debugger. */
12969 internal_condition_case_1 (redisplay_window_0, window,
12970 list_of_error,
12971 redisplay_window_error);
12972 }
12973
12974 window = w->next;
12975 }
12976 }
12977
12978 static Lisp_Object
12979 redisplay_window_error (Lisp_Object ignore)
12980 {
12981 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12982 return Qnil;
12983 }
12984
12985 static Lisp_Object
12986 redisplay_window_0 (Lisp_Object window)
12987 {
12988 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12989 redisplay_window (window, 0);
12990 return Qnil;
12991 }
12992
12993 static Lisp_Object
12994 redisplay_window_1 (Lisp_Object window)
12995 {
12996 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12997 redisplay_window (window, 1);
12998 return Qnil;
12999 }
13000 \f
13001
13002 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13003 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13004 which positions recorded in ROW differ from current buffer
13005 positions.
13006
13007 Return 0 if cursor is not on this row, 1 otherwise. */
13008
13009 static int
13010 set_cursor_from_row (struct window *w, struct glyph_row *row,
13011 struct glyph_matrix *matrix,
13012 EMACS_INT delta, EMACS_INT delta_bytes,
13013 int dy, int dvpos)
13014 {
13015 struct glyph *glyph = row->glyphs[TEXT_AREA];
13016 struct glyph *end = glyph + row->used[TEXT_AREA];
13017 struct glyph *cursor = NULL;
13018 /* The last known character position in row. */
13019 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13020 int x = row->x;
13021 EMACS_INT pt_old = PT - delta;
13022 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13023 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13024 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13025 /* A glyph beyond the edge of TEXT_AREA which we should never
13026 touch. */
13027 struct glyph *glyphs_end = end;
13028 /* Non-zero means we've found a match for cursor position, but that
13029 glyph has the avoid_cursor_p flag set. */
13030 int match_with_avoid_cursor = 0;
13031 /* Non-zero means we've seen at least one glyph that came from a
13032 display string. */
13033 int string_seen = 0;
13034 /* Largest and smalles buffer positions seen so far during scan of
13035 glyph row. */
13036 EMACS_INT bpos_max = pos_before;
13037 EMACS_INT bpos_min = pos_after;
13038 /* Last buffer position covered by an overlay string with an integer
13039 `cursor' property. */
13040 EMACS_INT bpos_covered = 0;
13041
13042 /* Skip over glyphs not having an object at the start and the end of
13043 the row. These are special glyphs like truncation marks on
13044 terminal frames. */
13045 if (row->displays_text_p)
13046 {
13047 if (!row->reversed_p)
13048 {
13049 while (glyph < end
13050 && INTEGERP (glyph->object)
13051 && glyph->charpos < 0)
13052 {
13053 x += glyph->pixel_width;
13054 ++glyph;
13055 }
13056 while (end > glyph
13057 && INTEGERP ((end - 1)->object)
13058 /* CHARPOS is zero for blanks and stretch glyphs
13059 inserted by extend_face_to_end_of_line. */
13060 && (end - 1)->charpos <= 0)
13061 --end;
13062 glyph_before = glyph - 1;
13063 glyph_after = end;
13064 }
13065 else
13066 {
13067 struct glyph *g;
13068
13069 /* If the glyph row is reversed, we need to process it from back
13070 to front, so swap the edge pointers. */
13071 glyphs_end = end = glyph - 1;
13072 glyph += row->used[TEXT_AREA] - 1;
13073
13074 while (glyph > end + 1
13075 && INTEGERP (glyph->object)
13076 && glyph->charpos < 0)
13077 {
13078 --glyph;
13079 x -= glyph->pixel_width;
13080 }
13081 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13082 --glyph;
13083 /* By default, in reversed rows we put the cursor on the
13084 rightmost (first in the reading order) glyph. */
13085 for (g = end + 1; g < glyph; g++)
13086 x += g->pixel_width;
13087 while (end < glyph
13088 && INTEGERP ((end + 1)->object)
13089 && (end + 1)->charpos <= 0)
13090 ++end;
13091 glyph_before = glyph + 1;
13092 glyph_after = end;
13093 }
13094 }
13095 else if (row->reversed_p)
13096 {
13097 /* In R2L rows that don't display text, put the cursor on the
13098 rightmost glyph. Case in point: an empty last line that is
13099 part of an R2L paragraph. */
13100 cursor = end - 1;
13101 /* Avoid placing the cursor on the last glyph of the row, where
13102 on terminal frames we hold the vertical border between
13103 adjacent windows. */
13104 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13105 && !WINDOW_RIGHTMOST_P (w)
13106 && cursor == row->glyphs[LAST_AREA] - 1)
13107 cursor--;
13108 x = -1; /* will be computed below, at label compute_x */
13109 }
13110
13111 /* Step 1: Try to find the glyph whose character position
13112 corresponds to point. If that's not possible, find 2 glyphs
13113 whose character positions are the closest to point, one before
13114 point, the other after it. */
13115 if (!row->reversed_p)
13116 while (/* not marched to end of glyph row */
13117 glyph < end
13118 /* glyph was not inserted by redisplay for internal purposes */
13119 && !INTEGERP (glyph->object))
13120 {
13121 if (BUFFERP (glyph->object))
13122 {
13123 EMACS_INT dpos = glyph->charpos - pt_old;
13124
13125 if (glyph->charpos > bpos_max)
13126 bpos_max = glyph->charpos;
13127 if (glyph->charpos < bpos_min)
13128 bpos_min = glyph->charpos;
13129 if (!glyph->avoid_cursor_p)
13130 {
13131 /* If we hit point, we've found the glyph on which to
13132 display the cursor. */
13133 if (dpos == 0)
13134 {
13135 match_with_avoid_cursor = 0;
13136 break;
13137 }
13138 /* See if we've found a better approximation to
13139 POS_BEFORE or to POS_AFTER. Note that we want the
13140 first (leftmost) glyph of all those that are the
13141 closest from below, and the last (rightmost) of all
13142 those from above. */
13143 if (0 > dpos && dpos > pos_before - pt_old)
13144 {
13145 pos_before = glyph->charpos;
13146 glyph_before = glyph;
13147 }
13148 else if (0 < dpos && dpos <= pos_after - pt_old)
13149 {
13150 pos_after = glyph->charpos;
13151 glyph_after = glyph;
13152 }
13153 }
13154 else if (dpos == 0)
13155 match_with_avoid_cursor = 1;
13156 }
13157 else if (STRINGP (glyph->object))
13158 {
13159 Lisp_Object chprop;
13160 EMACS_INT glyph_pos = glyph->charpos;
13161
13162 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13163 glyph->object);
13164 if (INTEGERP (chprop))
13165 {
13166 bpos_covered = bpos_max + XINT (chprop);
13167 /* If the `cursor' property covers buffer positions up
13168 to and including point, we should display cursor on
13169 this glyph. Note that overlays and text properties
13170 with string values stop bidi reordering, so every
13171 buffer position to the left of the string is always
13172 smaller than any position to the right of the
13173 string. Therefore, if a `cursor' property on one
13174 of the string's characters has an integer value, we
13175 will break out of the loop below _before_ we get to
13176 the position match above. IOW, integer values of
13177 the `cursor' property override the "exact match for
13178 point" strategy of positioning the cursor. */
13179 /* Implementation note: bpos_max == pt_old when, e.g.,
13180 we are in an empty line, where bpos_max is set to
13181 MATRIX_ROW_START_CHARPOS, see above. */
13182 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13183 {
13184 cursor = glyph;
13185 break;
13186 }
13187 }
13188
13189 string_seen = 1;
13190 }
13191 x += glyph->pixel_width;
13192 ++glyph;
13193 }
13194 else if (glyph > end) /* row is reversed */
13195 while (!INTEGERP (glyph->object))
13196 {
13197 if (BUFFERP (glyph->object))
13198 {
13199 EMACS_INT dpos = glyph->charpos - pt_old;
13200
13201 if (glyph->charpos > bpos_max)
13202 bpos_max = glyph->charpos;
13203 if (glyph->charpos < bpos_min)
13204 bpos_min = glyph->charpos;
13205 if (!glyph->avoid_cursor_p)
13206 {
13207 if (dpos == 0)
13208 {
13209 match_with_avoid_cursor = 0;
13210 break;
13211 }
13212 if (0 > dpos && dpos > pos_before - pt_old)
13213 {
13214 pos_before = glyph->charpos;
13215 glyph_before = glyph;
13216 }
13217 else if (0 < dpos && dpos <= pos_after - pt_old)
13218 {
13219 pos_after = glyph->charpos;
13220 glyph_after = glyph;
13221 }
13222 }
13223 else if (dpos == 0)
13224 match_with_avoid_cursor = 1;
13225 }
13226 else if (STRINGP (glyph->object))
13227 {
13228 Lisp_Object chprop;
13229 EMACS_INT glyph_pos = glyph->charpos;
13230
13231 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13232 glyph->object);
13233 if (INTEGERP (chprop))
13234 {
13235 bpos_covered = bpos_max + XINT (chprop);
13236 /* If the `cursor' property covers buffer positions up
13237 to and including point, we should display cursor on
13238 this glyph. */
13239 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13240 {
13241 cursor = glyph;
13242 break;
13243 }
13244 }
13245 string_seen = 1;
13246 }
13247 --glyph;
13248 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13249 {
13250 x--; /* can't use any pixel_width */
13251 break;
13252 }
13253 x -= glyph->pixel_width;
13254 }
13255
13256 /* Step 2: If we didn't find an exact match for point, we need to
13257 look for a proper place to put the cursor among glyphs between
13258 GLYPH_BEFORE and GLYPH_AFTER. */
13259 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13260 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13261 && bpos_covered < pt_old)
13262 {
13263 /* An empty line has a single glyph whose OBJECT is zero and
13264 whose CHARPOS is the position of a newline on that line.
13265 Note that on a TTY, there are more glyphs after that, which
13266 were produced by extend_face_to_end_of_line, but their
13267 CHARPOS is zero or negative. */
13268 int empty_line_p =
13269 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13270 && INTEGERP (glyph->object) && glyph->charpos > 0;
13271
13272 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13273 {
13274 EMACS_INT ellipsis_pos;
13275
13276 /* Scan back over the ellipsis glyphs. */
13277 if (!row->reversed_p)
13278 {
13279 ellipsis_pos = (glyph - 1)->charpos;
13280 while (glyph > row->glyphs[TEXT_AREA]
13281 && (glyph - 1)->charpos == ellipsis_pos)
13282 glyph--, x -= glyph->pixel_width;
13283 /* That loop always goes one position too far, including
13284 the glyph before the ellipsis. So scan forward over
13285 that one. */
13286 x += glyph->pixel_width;
13287 glyph++;
13288 }
13289 else /* row is reversed */
13290 {
13291 ellipsis_pos = (glyph + 1)->charpos;
13292 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13293 && (glyph + 1)->charpos == ellipsis_pos)
13294 glyph++, x += glyph->pixel_width;
13295 x -= glyph->pixel_width;
13296 glyph--;
13297 }
13298 }
13299 else if (match_with_avoid_cursor
13300 /* A truncated row may not include PT among its
13301 character positions. Setting the cursor inside the
13302 scroll margin will trigger recalculation of hscroll
13303 in hscroll_window_tree. */
13304 || (row->truncated_on_left_p && pt_old < bpos_min)
13305 || (row->truncated_on_right_p && pt_old > bpos_max)
13306 /* Zero-width characters produce no glyphs. */
13307 || (!string_seen
13308 && !empty_line_p
13309 && (row->reversed_p
13310 ? glyph_after > glyphs_end
13311 : glyph_after < glyphs_end)))
13312 {
13313 cursor = glyph_after;
13314 x = -1;
13315 }
13316 else if (string_seen)
13317 {
13318 int incr = row->reversed_p ? -1 : +1;
13319
13320 /* Need to find the glyph that came out of a string which is
13321 present at point. That glyph is somewhere between
13322 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13323 positioned between POS_BEFORE and POS_AFTER in the
13324 buffer. */
13325 struct glyph *start, *stop;
13326 EMACS_INT pos = pos_before;
13327
13328 x = -1;
13329
13330 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13331 correspond to POS_BEFORE and POS_AFTER, respectively. We
13332 need START and STOP in the order that corresponds to the
13333 row's direction as given by its reversed_p flag. If the
13334 directionality of characters between POS_BEFORE and
13335 POS_AFTER is the opposite of the row's base direction,
13336 these characters will have been reordered for display,
13337 and we need to reverse START and STOP. */
13338 if (!row->reversed_p)
13339 {
13340 start = min (glyph_before, glyph_after);
13341 stop = max (glyph_before, glyph_after);
13342 }
13343 else
13344 {
13345 start = max (glyph_before, glyph_after);
13346 stop = min (glyph_before, glyph_after);
13347 }
13348 for (glyph = start + incr;
13349 row->reversed_p ? glyph > stop : glyph < stop; )
13350 {
13351
13352 /* Any glyphs that come from the buffer are here because
13353 of bidi reordering. Skip them, and only pay
13354 attention to glyphs that came from some string. */
13355 if (STRINGP (glyph->object))
13356 {
13357 Lisp_Object str;
13358 EMACS_INT tem;
13359
13360 str = glyph->object;
13361 tem = string_buffer_position_lim (str, pos, pos_after, 0);
13362 if (tem == 0 /* from overlay */
13363 || pos <= tem)
13364 {
13365 /* If the string from which this glyph came is
13366 found in the buffer at point, then we've
13367 found the glyph we've been looking for. If
13368 it comes from an overlay (tem == 0), and it
13369 has the `cursor' property on one of its
13370 glyphs, record that glyph as a candidate for
13371 displaying the cursor. (As in the
13372 unidirectional version, we will display the
13373 cursor on the last candidate we find.) */
13374 if (tem == 0 || tem == pt_old)
13375 {
13376 /* The glyphs from this string could have
13377 been reordered. Find the one with the
13378 smallest string position. Or there could
13379 be a character in the string with the
13380 `cursor' property, which means display
13381 cursor on that character's glyph. */
13382 EMACS_INT strpos = glyph->charpos;
13383
13384 if (tem)
13385 cursor = glyph;
13386 for ( ;
13387 (row->reversed_p ? glyph > stop : glyph < stop)
13388 && EQ (glyph->object, str);
13389 glyph += incr)
13390 {
13391 Lisp_Object cprop;
13392 EMACS_INT gpos = glyph->charpos;
13393
13394 cprop = Fget_char_property (make_number (gpos),
13395 Qcursor,
13396 glyph->object);
13397 if (!NILP (cprop))
13398 {
13399 cursor = glyph;
13400 break;
13401 }
13402 if (tem && glyph->charpos < strpos)
13403 {
13404 strpos = glyph->charpos;
13405 cursor = glyph;
13406 }
13407 }
13408
13409 if (tem == pt_old)
13410 goto compute_x;
13411 }
13412 if (tem)
13413 pos = tem + 1; /* don't find previous instances */
13414 }
13415 /* This string is not what we want; skip all of the
13416 glyphs that came from it. */
13417 while ((row->reversed_p ? glyph > stop : glyph < stop)
13418 && EQ (glyph->object, str))
13419 glyph += incr;
13420 }
13421 else
13422 glyph += incr;
13423 }
13424
13425 /* If we reached the end of the line, and END was from a string,
13426 the cursor is not on this line. */
13427 if (cursor == NULL
13428 && (row->reversed_p ? glyph <= end : glyph >= end)
13429 && STRINGP (end->object)
13430 && row->continued_p)
13431 return 0;
13432 }
13433 }
13434
13435 compute_x:
13436 if (cursor != NULL)
13437 glyph = cursor;
13438 if (x < 0)
13439 {
13440 struct glyph *g;
13441
13442 /* Need to compute x that corresponds to GLYPH. */
13443 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13444 {
13445 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13446 abort ();
13447 x += g->pixel_width;
13448 }
13449 }
13450
13451 /* ROW could be part of a continued line, which, under bidi
13452 reordering, might have other rows whose start and end charpos
13453 occlude point. Only set w->cursor if we found a better
13454 approximation to the cursor position than we have from previously
13455 examined candidate rows belonging to the same continued line. */
13456 if (/* we already have a candidate row */
13457 w->cursor.vpos >= 0
13458 /* that candidate is not the row we are processing */
13459 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13460 /* the row we are processing is part of a continued line */
13461 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13462 /* Make sure cursor.vpos specifies a row whose start and end
13463 charpos occlude point. This is because some callers of this
13464 function leave cursor.vpos at the row where the cursor was
13465 displayed during the last redisplay cycle. */
13466 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13467 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13468 {
13469 struct glyph *g1 =
13470 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13471
13472 /* Don't consider glyphs that are outside TEXT_AREA. */
13473 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13474 return 0;
13475 /* Keep the candidate whose buffer position is the closest to
13476 point. */
13477 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13478 w->cursor.hpos >= 0
13479 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13480 && BUFFERP (g1->object)
13481 && (g1->charpos == pt_old /* an exact match always wins */
13482 || (BUFFERP (glyph->object)
13483 && eabs (g1->charpos - pt_old)
13484 < eabs (glyph->charpos - pt_old))))
13485 return 0;
13486 /* If this candidate gives an exact match, use that. */
13487 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13488 /* Otherwise, keep the candidate that comes from a row
13489 spanning less buffer positions. This may win when one or
13490 both candidate positions are on glyphs that came from
13491 display strings, for which we cannot compare buffer
13492 positions. */
13493 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13494 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13495 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13496 return 0;
13497 }
13498 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13499 w->cursor.x = x;
13500 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13501 w->cursor.y = row->y + dy;
13502
13503 if (w == XWINDOW (selected_window))
13504 {
13505 if (!row->continued_p
13506 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13507 && row->x == 0)
13508 {
13509 this_line_buffer = XBUFFER (w->buffer);
13510
13511 CHARPOS (this_line_start_pos)
13512 = MATRIX_ROW_START_CHARPOS (row) + delta;
13513 BYTEPOS (this_line_start_pos)
13514 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13515
13516 CHARPOS (this_line_end_pos)
13517 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13518 BYTEPOS (this_line_end_pos)
13519 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13520
13521 this_line_y = w->cursor.y;
13522 this_line_pixel_height = row->height;
13523 this_line_vpos = w->cursor.vpos;
13524 this_line_start_x = row->x;
13525 }
13526 else
13527 CHARPOS (this_line_start_pos) = 0;
13528 }
13529
13530 return 1;
13531 }
13532
13533
13534 /* Run window scroll functions, if any, for WINDOW with new window
13535 start STARTP. Sets the window start of WINDOW to that position.
13536
13537 We assume that the window's buffer is really current. */
13538
13539 static INLINE struct text_pos
13540 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13541 {
13542 struct window *w = XWINDOW (window);
13543 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13544
13545 if (current_buffer != XBUFFER (w->buffer))
13546 abort ();
13547
13548 if (!NILP (Vwindow_scroll_functions))
13549 {
13550 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13551 make_number (CHARPOS (startp)));
13552 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13553 /* In case the hook functions switch buffers. */
13554 if (current_buffer != XBUFFER (w->buffer))
13555 set_buffer_internal_1 (XBUFFER (w->buffer));
13556 }
13557
13558 return startp;
13559 }
13560
13561
13562 /* Make sure the line containing the cursor is fully visible.
13563 A value of 1 means there is nothing to be done.
13564 (Either the line is fully visible, or it cannot be made so,
13565 or we cannot tell.)
13566
13567 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13568 is higher than window.
13569
13570 A value of 0 means the caller should do scrolling
13571 as if point had gone off the screen. */
13572
13573 static int
13574 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13575 {
13576 struct glyph_matrix *matrix;
13577 struct glyph_row *row;
13578 int window_height;
13579
13580 if (!make_cursor_line_fully_visible_p)
13581 return 1;
13582
13583 /* It's not always possible to find the cursor, e.g, when a window
13584 is full of overlay strings. Don't do anything in that case. */
13585 if (w->cursor.vpos < 0)
13586 return 1;
13587
13588 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13589 row = MATRIX_ROW (matrix, w->cursor.vpos);
13590
13591 /* If the cursor row is not partially visible, there's nothing to do. */
13592 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13593 return 1;
13594
13595 /* If the row the cursor is in is taller than the window's height,
13596 it's not clear what to do, so do nothing. */
13597 window_height = window_box_height (w);
13598 if (row->height >= window_height)
13599 {
13600 if (!force_p || MINI_WINDOW_P (w)
13601 || w->vscroll || w->cursor.vpos == 0)
13602 return 1;
13603 }
13604 return 0;
13605 }
13606
13607
13608 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13609 non-zero means only WINDOW is redisplayed in redisplay_internal.
13610 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13611 in redisplay_window to bring a partially visible line into view in
13612 the case that only the cursor has moved.
13613
13614 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13615 last screen line's vertical height extends past the end of the screen.
13616
13617 Value is
13618
13619 1 if scrolling succeeded
13620
13621 0 if scrolling didn't find point.
13622
13623 -1 if new fonts have been loaded so that we must interrupt
13624 redisplay, adjust glyph matrices, and try again. */
13625
13626 enum
13627 {
13628 SCROLLING_SUCCESS,
13629 SCROLLING_FAILED,
13630 SCROLLING_NEED_LARGER_MATRICES
13631 };
13632
13633 /* If scroll-conservatively is more than this, never recenter.
13634
13635 If you change this, don't forget to update the doc string of
13636 `scroll-conservatively' and the Emacs manual. */
13637 #define SCROLL_LIMIT 100
13638
13639 static int
13640 try_scrolling (Lisp_Object window, int just_this_one_p,
13641 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13642 int temp_scroll_step, int last_line_misfit)
13643 {
13644 struct window *w = XWINDOW (window);
13645 struct frame *f = XFRAME (w->frame);
13646 struct text_pos pos, startp;
13647 struct it it;
13648 int this_scroll_margin, scroll_max, rc, height;
13649 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13650 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13651 Lisp_Object aggressive;
13652 /* We will never try scrolling more than this number of lines. */
13653 int scroll_limit = SCROLL_LIMIT;
13654
13655 #if GLYPH_DEBUG
13656 debug_method_add (w, "try_scrolling");
13657 #endif
13658
13659 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13660
13661 /* Compute scroll margin height in pixels. We scroll when point is
13662 within this distance from the top or bottom of the window. */
13663 if (scroll_margin > 0)
13664 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13665 * FRAME_LINE_HEIGHT (f);
13666 else
13667 this_scroll_margin = 0;
13668
13669 /* Force arg_scroll_conservatively to have a reasonable value, to
13670 avoid scrolling too far away with slow move_it_* functions. Note
13671 that the user can supply scroll-conservatively equal to
13672 `most-positive-fixnum', which can be larger than INT_MAX. */
13673 if (arg_scroll_conservatively > scroll_limit)
13674 {
13675 arg_scroll_conservatively = scroll_limit + 1;
13676 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13677 }
13678 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13679 /* Compute how much we should try to scroll maximally to bring
13680 point into view. */
13681 scroll_max = (max (scroll_step,
13682 max (arg_scroll_conservatively, temp_scroll_step))
13683 * FRAME_LINE_HEIGHT (f));
13684 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13685 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13686 /* We're trying to scroll because of aggressive scrolling but no
13687 scroll_step is set. Choose an arbitrary one. */
13688 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13689 else
13690 scroll_max = 0;
13691
13692 too_near_end:
13693
13694 /* Decide whether to scroll down. */
13695 if (PT > CHARPOS (startp))
13696 {
13697 int scroll_margin_y;
13698
13699 /* Compute the pixel ypos of the scroll margin, then move it to
13700 either that ypos or PT, whichever comes first. */
13701 start_display (&it, w, startp);
13702 scroll_margin_y = it.last_visible_y - this_scroll_margin
13703 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13704 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13705 (MOVE_TO_POS | MOVE_TO_Y));
13706
13707 if (PT > CHARPOS (it.current.pos))
13708 {
13709 int y0 = line_bottom_y (&it);
13710 /* Compute how many pixels below window bottom to stop searching
13711 for PT. This avoids costly search for PT that is far away if
13712 the user limited scrolling by a small number of lines, but
13713 always finds PT if scroll_conservatively is set to a large
13714 number, such as most-positive-fixnum. */
13715 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13716 int y_to_move = it.last_visible_y + slack;
13717
13718 /* Compute the distance from the scroll margin to PT or to
13719 the scroll limit, whichever comes first. This should
13720 include the height of the cursor line, to make that line
13721 fully visible. */
13722 move_it_to (&it, PT, -1, y_to_move,
13723 -1, MOVE_TO_POS | MOVE_TO_Y);
13724 dy = line_bottom_y (&it) - y0;
13725
13726 if (dy > scroll_max)
13727 return SCROLLING_FAILED;
13728
13729 scroll_down_p = 1;
13730 }
13731 }
13732
13733 if (scroll_down_p)
13734 {
13735 /* Point is in or below the bottom scroll margin, so move the
13736 window start down. If scrolling conservatively, move it just
13737 enough down to make point visible. If scroll_step is set,
13738 move it down by scroll_step. */
13739 if (arg_scroll_conservatively)
13740 amount_to_scroll
13741 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13742 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13743 else if (scroll_step || temp_scroll_step)
13744 amount_to_scroll = scroll_max;
13745 else
13746 {
13747 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13748 height = WINDOW_BOX_TEXT_HEIGHT (w);
13749 if (NUMBERP (aggressive))
13750 {
13751 double float_amount = XFLOATINT (aggressive) * height;
13752 amount_to_scroll = float_amount;
13753 if (amount_to_scroll == 0 && float_amount > 0)
13754 amount_to_scroll = 1;
13755 /* Don't let point enter the scroll margin near top of
13756 the window. */
13757 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13758 amount_to_scroll = height - 2*this_scroll_margin + dy;
13759 }
13760 }
13761
13762 if (amount_to_scroll <= 0)
13763 return SCROLLING_FAILED;
13764
13765 start_display (&it, w, startp);
13766 if (arg_scroll_conservatively <= scroll_limit)
13767 move_it_vertically (&it, amount_to_scroll);
13768 else
13769 {
13770 /* Extra precision for users who set scroll-conservatively
13771 to a large number: make sure the amount we scroll
13772 the window start is never less than amount_to_scroll,
13773 which was computed as distance from window bottom to
13774 point. This matters when lines at window top and lines
13775 below window bottom have different height. */
13776 struct it it1 = it;
13777 /* We use a temporary it1 because line_bottom_y can modify
13778 its argument, if it moves one line down; see there. */
13779 int start_y = line_bottom_y (&it1);
13780
13781 do {
13782 move_it_by_lines (&it, 1);
13783 it1 = it;
13784 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13785 }
13786
13787 /* If STARTP is unchanged, move it down another screen line. */
13788 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13789 move_it_by_lines (&it, 1);
13790 startp = it.current.pos;
13791 }
13792 else
13793 {
13794 struct text_pos scroll_margin_pos = startp;
13795
13796 /* See if point is inside the scroll margin at the top of the
13797 window. */
13798 if (this_scroll_margin)
13799 {
13800 start_display (&it, w, startp);
13801 move_it_vertically (&it, this_scroll_margin);
13802 scroll_margin_pos = it.current.pos;
13803 }
13804
13805 if (PT < CHARPOS (scroll_margin_pos))
13806 {
13807 /* Point is in the scroll margin at the top of the window or
13808 above what is displayed in the window. */
13809 int y0, y_to_move;
13810
13811 /* Compute the vertical distance from PT to the scroll
13812 margin position. Move as far as scroll_max allows, or
13813 one screenful, or 10 screen lines, whichever is largest.
13814 Give up if distance is greater than scroll_max. */
13815 SET_TEXT_POS (pos, PT, PT_BYTE);
13816 start_display (&it, w, pos);
13817 y0 = it.current_y;
13818 y_to_move = max (it.last_visible_y,
13819 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13820 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13821 y_to_move, -1,
13822 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13823 dy = it.current_y - y0;
13824 if (dy > scroll_max)
13825 return SCROLLING_FAILED;
13826
13827 /* Compute new window start. */
13828 start_display (&it, w, startp);
13829
13830 if (arg_scroll_conservatively)
13831 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13832 max (scroll_step, temp_scroll_step));
13833 else if (scroll_step || temp_scroll_step)
13834 amount_to_scroll = scroll_max;
13835 else
13836 {
13837 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13838 height = WINDOW_BOX_TEXT_HEIGHT (w);
13839 if (NUMBERP (aggressive))
13840 {
13841 double float_amount = XFLOATINT (aggressive) * height;
13842 amount_to_scroll = float_amount;
13843 if (amount_to_scroll == 0 && float_amount > 0)
13844 amount_to_scroll = 1;
13845 amount_to_scroll -=
13846 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13847 /* Don't let point enter the scroll margin near
13848 bottom of the window. */
13849 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13850 amount_to_scroll = height - 2*this_scroll_margin + dy;
13851 }
13852 }
13853
13854 if (amount_to_scroll <= 0)
13855 return SCROLLING_FAILED;
13856
13857 move_it_vertically_backward (&it, amount_to_scroll);
13858 startp = it.current.pos;
13859 }
13860 }
13861
13862 /* Run window scroll functions. */
13863 startp = run_window_scroll_functions (window, startp);
13864
13865 /* Display the window. Give up if new fonts are loaded, or if point
13866 doesn't appear. */
13867 if (!try_window (window, startp, 0))
13868 rc = SCROLLING_NEED_LARGER_MATRICES;
13869 else if (w->cursor.vpos < 0)
13870 {
13871 clear_glyph_matrix (w->desired_matrix);
13872 rc = SCROLLING_FAILED;
13873 }
13874 else
13875 {
13876 /* Maybe forget recorded base line for line number display. */
13877 if (!just_this_one_p
13878 || current_buffer->clip_changed
13879 || BEG_UNCHANGED < CHARPOS (startp))
13880 w->base_line_number = Qnil;
13881
13882 /* If cursor ends up on a partially visible line,
13883 treat that as being off the bottom of the screen. */
13884 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13885 /* It's possible that the cursor is on the first line of the
13886 buffer, which is partially obscured due to a vscroll
13887 (Bug#7537). In that case, avoid looping forever . */
13888 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13889 {
13890 clear_glyph_matrix (w->desired_matrix);
13891 ++extra_scroll_margin_lines;
13892 goto too_near_end;
13893 }
13894 rc = SCROLLING_SUCCESS;
13895 }
13896
13897 return rc;
13898 }
13899
13900
13901 /* Compute a suitable window start for window W if display of W starts
13902 on a continuation line. Value is non-zero if a new window start
13903 was computed.
13904
13905 The new window start will be computed, based on W's width, starting
13906 from the start of the continued line. It is the start of the
13907 screen line with the minimum distance from the old start W->start. */
13908
13909 static int
13910 compute_window_start_on_continuation_line (struct window *w)
13911 {
13912 struct text_pos pos, start_pos;
13913 int window_start_changed_p = 0;
13914
13915 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13916
13917 /* If window start is on a continuation line... Window start may be
13918 < BEGV in case there's invisible text at the start of the
13919 buffer (M-x rmail, for example). */
13920 if (CHARPOS (start_pos) > BEGV
13921 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13922 {
13923 struct it it;
13924 struct glyph_row *row;
13925
13926 /* Handle the case that the window start is out of range. */
13927 if (CHARPOS (start_pos) < BEGV)
13928 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13929 else if (CHARPOS (start_pos) > ZV)
13930 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13931
13932 /* Find the start of the continued line. This should be fast
13933 because scan_buffer is fast (newline cache). */
13934 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13935 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13936 row, DEFAULT_FACE_ID);
13937 reseat_at_previous_visible_line_start (&it);
13938
13939 /* If the line start is "too far" away from the window start,
13940 say it takes too much time to compute a new window start. */
13941 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13942 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13943 {
13944 int min_distance, distance;
13945
13946 /* Move forward by display lines to find the new window
13947 start. If window width was enlarged, the new start can
13948 be expected to be > the old start. If window width was
13949 decreased, the new window start will be < the old start.
13950 So, we're looking for the display line start with the
13951 minimum distance from the old window start. */
13952 pos = it.current.pos;
13953 min_distance = INFINITY;
13954 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13955 distance < min_distance)
13956 {
13957 min_distance = distance;
13958 pos = it.current.pos;
13959 move_it_by_lines (&it, 1);
13960 }
13961
13962 /* Set the window start there. */
13963 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13964 window_start_changed_p = 1;
13965 }
13966 }
13967
13968 return window_start_changed_p;
13969 }
13970
13971
13972 /* Try cursor movement in case text has not changed in window WINDOW,
13973 with window start STARTP. Value is
13974
13975 CURSOR_MOVEMENT_SUCCESS if successful
13976
13977 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13978
13979 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13980 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13981 we want to scroll as if scroll-step were set to 1. See the code.
13982
13983 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13984 which case we have to abort this redisplay, and adjust matrices
13985 first. */
13986
13987 enum
13988 {
13989 CURSOR_MOVEMENT_SUCCESS,
13990 CURSOR_MOVEMENT_CANNOT_BE_USED,
13991 CURSOR_MOVEMENT_MUST_SCROLL,
13992 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13993 };
13994
13995 static int
13996 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13997 {
13998 struct window *w = XWINDOW (window);
13999 struct frame *f = XFRAME (w->frame);
14000 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14001
14002 #if GLYPH_DEBUG
14003 if (inhibit_try_cursor_movement)
14004 return rc;
14005 #endif
14006
14007 /* Handle case where text has not changed, only point, and it has
14008 not moved off the frame. */
14009 if (/* Point may be in this window. */
14010 PT >= CHARPOS (startp)
14011 /* Selective display hasn't changed. */
14012 && !current_buffer->clip_changed
14013 /* Function force-mode-line-update is used to force a thorough
14014 redisplay. It sets either windows_or_buffers_changed or
14015 update_mode_lines. So don't take a shortcut here for these
14016 cases. */
14017 && !update_mode_lines
14018 && !windows_or_buffers_changed
14019 && !cursor_type_changed
14020 /* Can't use this case if highlighting a region. When a
14021 region exists, cursor movement has to do more than just
14022 set the cursor. */
14023 && !(!NILP (Vtransient_mark_mode)
14024 && !NILP (BVAR (current_buffer, mark_active)))
14025 && NILP (w->region_showing)
14026 && NILP (Vshow_trailing_whitespace)
14027 /* Right after splitting windows, last_point may be nil. */
14028 && INTEGERP (w->last_point)
14029 /* This code is not used for mini-buffer for the sake of the case
14030 of redisplaying to replace an echo area message; since in
14031 that case the mini-buffer contents per se are usually
14032 unchanged. This code is of no real use in the mini-buffer
14033 since the handling of this_line_start_pos, etc., in redisplay
14034 handles the same cases. */
14035 && !EQ (window, minibuf_window)
14036 /* When splitting windows or for new windows, it happens that
14037 redisplay is called with a nil window_end_vpos or one being
14038 larger than the window. This should really be fixed in
14039 window.c. I don't have this on my list, now, so we do
14040 approximately the same as the old redisplay code. --gerd. */
14041 && INTEGERP (w->window_end_vpos)
14042 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14043 && (FRAME_WINDOW_P (f)
14044 || !overlay_arrow_in_current_buffer_p ()))
14045 {
14046 int this_scroll_margin, top_scroll_margin;
14047 struct glyph_row *row = NULL;
14048
14049 #if GLYPH_DEBUG
14050 debug_method_add (w, "cursor movement");
14051 #endif
14052
14053 /* Scroll if point within this distance from the top or bottom
14054 of the window. This is a pixel value. */
14055 if (scroll_margin > 0)
14056 {
14057 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14058 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14059 }
14060 else
14061 this_scroll_margin = 0;
14062
14063 top_scroll_margin = this_scroll_margin;
14064 if (WINDOW_WANTS_HEADER_LINE_P (w))
14065 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14066
14067 /* Start with the row the cursor was displayed during the last
14068 not paused redisplay. Give up if that row is not valid. */
14069 if (w->last_cursor.vpos < 0
14070 || w->last_cursor.vpos >= w->current_matrix->nrows)
14071 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14072 else
14073 {
14074 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14075 if (row->mode_line_p)
14076 ++row;
14077 if (!row->enabled_p)
14078 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14079 }
14080
14081 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14082 {
14083 int scroll_p = 0, must_scroll = 0;
14084 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14085
14086 if (PT > XFASTINT (w->last_point))
14087 {
14088 /* Point has moved forward. */
14089 while (MATRIX_ROW_END_CHARPOS (row) < PT
14090 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14091 {
14092 xassert (row->enabled_p);
14093 ++row;
14094 }
14095
14096 /* If the end position of a row equals the start
14097 position of the next row, and PT is at that position,
14098 we would rather display cursor in the next line. */
14099 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14100 && MATRIX_ROW_END_CHARPOS (row) == PT
14101 && row < w->current_matrix->rows
14102 + w->current_matrix->nrows - 1
14103 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14104 && !cursor_row_p (row))
14105 ++row;
14106
14107 /* If within the scroll margin, scroll. Note that
14108 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14109 the next line would be drawn, and that
14110 this_scroll_margin can be zero. */
14111 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14112 || PT > MATRIX_ROW_END_CHARPOS (row)
14113 /* Line is completely visible last line in window
14114 and PT is to be set in the next line. */
14115 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14116 && PT == MATRIX_ROW_END_CHARPOS (row)
14117 && !row->ends_at_zv_p
14118 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14119 scroll_p = 1;
14120 }
14121 else if (PT < XFASTINT (w->last_point))
14122 {
14123 /* Cursor has to be moved backward. Note that PT >=
14124 CHARPOS (startp) because of the outer if-statement. */
14125 while (!row->mode_line_p
14126 && (MATRIX_ROW_START_CHARPOS (row) > PT
14127 || (MATRIX_ROW_START_CHARPOS (row) == PT
14128 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14129 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14130 row > w->current_matrix->rows
14131 && (row-1)->ends_in_newline_from_string_p))))
14132 && (row->y > top_scroll_margin
14133 || CHARPOS (startp) == BEGV))
14134 {
14135 xassert (row->enabled_p);
14136 --row;
14137 }
14138
14139 /* Consider the following case: Window starts at BEGV,
14140 there is invisible, intangible text at BEGV, so that
14141 display starts at some point START > BEGV. It can
14142 happen that we are called with PT somewhere between
14143 BEGV and START. Try to handle that case. */
14144 if (row < w->current_matrix->rows
14145 || row->mode_line_p)
14146 {
14147 row = w->current_matrix->rows;
14148 if (row->mode_line_p)
14149 ++row;
14150 }
14151
14152 /* Due to newlines in overlay strings, we may have to
14153 skip forward over overlay strings. */
14154 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14155 && MATRIX_ROW_END_CHARPOS (row) == PT
14156 && !cursor_row_p (row))
14157 ++row;
14158
14159 /* If within the scroll margin, scroll. */
14160 if (row->y < top_scroll_margin
14161 && CHARPOS (startp) != BEGV)
14162 scroll_p = 1;
14163 }
14164 else
14165 {
14166 /* Cursor did not move. So don't scroll even if cursor line
14167 is partially visible, as it was so before. */
14168 rc = CURSOR_MOVEMENT_SUCCESS;
14169 }
14170
14171 if (PT < MATRIX_ROW_START_CHARPOS (row)
14172 || PT > MATRIX_ROW_END_CHARPOS (row))
14173 {
14174 /* if PT is not in the glyph row, give up. */
14175 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14176 must_scroll = 1;
14177 }
14178 else if (rc != CURSOR_MOVEMENT_SUCCESS
14179 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14180 {
14181 /* If rows are bidi-reordered and point moved, back up
14182 until we find a row that does not belong to a
14183 continuation line. This is because we must consider
14184 all rows of a continued line as candidates for the
14185 new cursor positioning, since row start and end
14186 positions change non-linearly with vertical position
14187 in such rows. */
14188 /* FIXME: Revisit this when glyph ``spilling'' in
14189 continuation lines' rows is implemented for
14190 bidi-reordered rows. */
14191 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14192 {
14193 xassert (row->enabled_p);
14194 --row;
14195 /* If we hit the beginning of the displayed portion
14196 without finding the first row of a continued
14197 line, give up. */
14198 if (row <= w->current_matrix->rows)
14199 {
14200 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14201 break;
14202 }
14203
14204 }
14205 }
14206 if (must_scroll)
14207 ;
14208 else if (rc != CURSOR_MOVEMENT_SUCCESS
14209 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14210 && make_cursor_line_fully_visible_p)
14211 {
14212 if (PT == MATRIX_ROW_END_CHARPOS (row)
14213 && !row->ends_at_zv_p
14214 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14215 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14216 else if (row->height > window_box_height (w))
14217 {
14218 /* If we end up in a partially visible line, let's
14219 make it fully visible, except when it's taller
14220 than the window, in which case we can't do much
14221 about it. */
14222 *scroll_step = 1;
14223 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14224 }
14225 else
14226 {
14227 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14228 if (!cursor_row_fully_visible_p (w, 0, 1))
14229 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14230 else
14231 rc = CURSOR_MOVEMENT_SUCCESS;
14232 }
14233 }
14234 else if (scroll_p)
14235 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14236 else if (rc != CURSOR_MOVEMENT_SUCCESS
14237 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14238 {
14239 /* With bidi-reordered rows, there could be more than
14240 one candidate row whose start and end positions
14241 occlude point. We need to let set_cursor_from_row
14242 find the best candidate. */
14243 /* FIXME: Revisit this when glyph ``spilling'' in
14244 continuation lines' rows is implemented for
14245 bidi-reordered rows. */
14246 int rv = 0;
14247
14248 do
14249 {
14250 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14251 && PT <= MATRIX_ROW_END_CHARPOS (row)
14252 && cursor_row_p (row))
14253 rv |= set_cursor_from_row (w, row, w->current_matrix,
14254 0, 0, 0, 0);
14255 /* As soon as we've found the first suitable row
14256 whose ends_at_zv_p flag is set, we are done. */
14257 if (rv
14258 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14259 {
14260 rc = CURSOR_MOVEMENT_SUCCESS;
14261 break;
14262 }
14263 ++row;
14264 }
14265 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14266 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14267 || (MATRIX_ROW_START_CHARPOS (row) == PT
14268 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14269 /* If we didn't find any candidate rows, or exited the
14270 loop before all the candidates were examined, signal
14271 to the caller that this method failed. */
14272 if (rc != CURSOR_MOVEMENT_SUCCESS
14273 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14274 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14275 else if (rv)
14276 rc = CURSOR_MOVEMENT_SUCCESS;
14277 }
14278 else
14279 {
14280 do
14281 {
14282 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14283 {
14284 rc = CURSOR_MOVEMENT_SUCCESS;
14285 break;
14286 }
14287 ++row;
14288 }
14289 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14290 && MATRIX_ROW_START_CHARPOS (row) == PT
14291 && cursor_row_p (row));
14292 }
14293 }
14294 }
14295
14296 return rc;
14297 }
14298
14299 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14300 static
14301 #endif
14302 void
14303 set_vertical_scroll_bar (struct window *w)
14304 {
14305 EMACS_INT start, end, whole;
14306
14307 /* Calculate the start and end positions for the current window.
14308 At some point, it would be nice to choose between scrollbars
14309 which reflect the whole buffer size, with special markers
14310 indicating narrowing, and scrollbars which reflect only the
14311 visible region.
14312
14313 Note that mini-buffers sometimes aren't displaying any text. */
14314 if (!MINI_WINDOW_P (w)
14315 || (w == XWINDOW (minibuf_window)
14316 && NILP (echo_area_buffer[0])))
14317 {
14318 struct buffer *buf = XBUFFER (w->buffer);
14319 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14320 start = marker_position (w->start) - BUF_BEGV (buf);
14321 /* I don't think this is guaranteed to be right. For the
14322 moment, we'll pretend it is. */
14323 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14324
14325 if (end < start)
14326 end = start;
14327 if (whole < (end - start))
14328 whole = end - start;
14329 }
14330 else
14331 start = end = whole = 0;
14332
14333 /* Indicate what this scroll bar ought to be displaying now. */
14334 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14335 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14336 (w, end - start, whole, start);
14337 }
14338
14339
14340 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14341 selected_window is redisplayed.
14342
14343 We can return without actually redisplaying the window if
14344 fonts_changed_p is nonzero. In that case, redisplay_internal will
14345 retry. */
14346
14347 static void
14348 redisplay_window (Lisp_Object window, int just_this_one_p)
14349 {
14350 struct window *w = XWINDOW (window);
14351 struct frame *f = XFRAME (w->frame);
14352 struct buffer *buffer = XBUFFER (w->buffer);
14353 struct buffer *old = current_buffer;
14354 struct text_pos lpoint, opoint, startp;
14355 int update_mode_line;
14356 int tem;
14357 struct it it;
14358 /* Record it now because it's overwritten. */
14359 int current_matrix_up_to_date_p = 0;
14360 int used_current_matrix_p = 0;
14361 /* This is less strict than current_matrix_up_to_date_p.
14362 It indictes that the buffer contents and narrowing are unchanged. */
14363 int buffer_unchanged_p = 0;
14364 int temp_scroll_step = 0;
14365 int count = SPECPDL_INDEX ();
14366 int rc;
14367 int centering_position = -1;
14368 int last_line_misfit = 0;
14369 EMACS_INT beg_unchanged, end_unchanged;
14370
14371 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14372 opoint = lpoint;
14373
14374 /* W must be a leaf window here. */
14375 xassert (!NILP (w->buffer));
14376 #if GLYPH_DEBUG
14377 *w->desired_matrix->method = 0;
14378 #endif
14379
14380 restart:
14381 reconsider_clip_changes (w, buffer);
14382
14383 /* Has the mode line to be updated? */
14384 update_mode_line = (!NILP (w->update_mode_line)
14385 || update_mode_lines
14386 || buffer->clip_changed
14387 || buffer->prevent_redisplay_optimizations_p);
14388
14389 if (MINI_WINDOW_P (w))
14390 {
14391 if (w == XWINDOW (echo_area_window)
14392 && !NILP (echo_area_buffer[0]))
14393 {
14394 if (update_mode_line)
14395 /* We may have to update a tty frame's menu bar or a
14396 tool-bar. Example `M-x C-h C-h C-g'. */
14397 goto finish_menu_bars;
14398 else
14399 /* We've already displayed the echo area glyphs in this window. */
14400 goto finish_scroll_bars;
14401 }
14402 else if ((w != XWINDOW (minibuf_window)
14403 || minibuf_level == 0)
14404 /* When buffer is nonempty, redisplay window normally. */
14405 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14406 /* Quail displays non-mini buffers in minibuffer window.
14407 In that case, redisplay the window normally. */
14408 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14409 {
14410 /* W is a mini-buffer window, but it's not active, so clear
14411 it. */
14412 int yb = window_text_bottom_y (w);
14413 struct glyph_row *row;
14414 int y;
14415
14416 for (y = 0, row = w->desired_matrix->rows;
14417 y < yb;
14418 y += row->height, ++row)
14419 blank_row (w, row, y);
14420 goto finish_scroll_bars;
14421 }
14422
14423 clear_glyph_matrix (w->desired_matrix);
14424 }
14425
14426 /* Otherwise set up data on this window; select its buffer and point
14427 value. */
14428 /* Really select the buffer, for the sake of buffer-local
14429 variables. */
14430 set_buffer_internal_1 (XBUFFER (w->buffer));
14431
14432 current_matrix_up_to_date_p
14433 = (!NILP (w->window_end_valid)
14434 && !current_buffer->clip_changed
14435 && !current_buffer->prevent_redisplay_optimizations_p
14436 && XFASTINT (w->last_modified) >= MODIFF
14437 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14438
14439 /* Run the window-bottom-change-functions
14440 if it is possible that the text on the screen has changed
14441 (either due to modification of the text, or any other reason). */
14442 if (!current_matrix_up_to_date_p
14443 && !NILP (Vwindow_text_change_functions))
14444 {
14445 safe_run_hooks (Qwindow_text_change_functions);
14446 goto restart;
14447 }
14448
14449 beg_unchanged = BEG_UNCHANGED;
14450 end_unchanged = END_UNCHANGED;
14451
14452 SET_TEXT_POS (opoint, PT, PT_BYTE);
14453
14454 specbind (Qinhibit_point_motion_hooks, Qt);
14455
14456 buffer_unchanged_p
14457 = (!NILP (w->window_end_valid)
14458 && !current_buffer->clip_changed
14459 && XFASTINT (w->last_modified) >= MODIFF
14460 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14461
14462 /* When windows_or_buffers_changed is non-zero, we can't rely on
14463 the window end being valid, so set it to nil there. */
14464 if (windows_or_buffers_changed)
14465 {
14466 /* If window starts on a continuation line, maybe adjust the
14467 window start in case the window's width changed. */
14468 if (XMARKER (w->start)->buffer == current_buffer)
14469 compute_window_start_on_continuation_line (w);
14470
14471 w->window_end_valid = Qnil;
14472 }
14473
14474 /* Some sanity checks. */
14475 CHECK_WINDOW_END (w);
14476 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14477 abort ();
14478 if (BYTEPOS (opoint) < CHARPOS (opoint))
14479 abort ();
14480
14481 /* If %c is in mode line, update it if needed. */
14482 if (!NILP (w->column_number_displayed)
14483 /* This alternative quickly identifies a common case
14484 where no change is needed. */
14485 && !(PT == XFASTINT (w->last_point)
14486 && XFASTINT (w->last_modified) >= MODIFF
14487 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14488 && (XFASTINT (w->column_number_displayed) != current_column ()))
14489 update_mode_line = 1;
14490
14491 /* Count number of windows showing the selected buffer. An indirect
14492 buffer counts as its base buffer. */
14493 if (!just_this_one_p)
14494 {
14495 struct buffer *current_base, *window_base;
14496 current_base = current_buffer;
14497 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14498 if (current_base->base_buffer)
14499 current_base = current_base->base_buffer;
14500 if (window_base->base_buffer)
14501 window_base = window_base->base_buffer;
14502 if (current_base == window_base)
14503 buffer_shared++;
14504 }
14505
14506 /* Point refers normally to the selected window. For any other
14507 window, set up appropriate value. */
14508 if (!EQ (window, selected_window))
14509 {
14510 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14511 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14512 if (new_pt < BEGV)
14513 {
14514 new_pt = BEGV;
14515 new_pt_byte = BEGV_BYTE;
14516 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14517 }
14518 else if (new_pt > (ZV - 1))
14519 {
14520 new_pt = ZV;
14521 new_pt_byte = ZV_BYTE;
14522 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14523 }
14524
14525 /* We don't use SET_PT so that the point-motion hooks don't run. */
14526 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14527 }
14528
14529 /* If any of the character widths specified in the display table
14530 have changed, invalidate the width run cache. It's true that
14531 this may be a bit late to catch such changes, but the rest of
14532 redisplay goes (non-fatally) haywire when the display table is
14533 changed, so why should we worry about doing any better? */
14534 if (current_buffer->width_run_cache)
14535 {
14536 struct Lisp_Char_Table *disptab = buffer_display_table ();
14537
14538 if (! disptab_matches_widthtab (disptab,
14539 XVECTOR (BVAR (current_buffer, width_table))))
14540 {
14541 invalidate_region_cache (current_buffer,
14542 current_buffer->width_run_cache,
14543 BEG, Z);
14544 recompute_width_table (current_buffer, disptab);
14545 }
14546 }
14547
14548 /* If window-start is screwed up, choose a new one. */
14549 if (XMARKER (w->start)->buffer != current_buffer)
14550 goto recenter;
14551
14552 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14553
14554 /* If someone specified a new starting point but did not insist,
14555 check whether it can be used. */
14556 if (!NILP (w->optional_new_start)
14557 && CHARPOS (startp) >= BEGV
14558 && CHARPOS (startp) <= ZV)
14559 {
14560 w->optional_new_start = Qnil;
14561 start_display (&it, w, startp);
14562 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14563 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14564 if (IT_CHARPOS (it) == PT)
14565 w->force_start = Qt;
14566 /* IT may overshoot PT if text at PT is invisible. */
14567 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14568 w->force_start = Qt;
14569 }
14570
14571 force_start:
14572
14573 /* Handle case where place to start displaying has been specified,
14574 unless the specified location is outside the accessible range. */
14575 if (!NILP (w->force_start)
14576 || w->frozen_window_start_p)
14577 {
14578 /* We set this later on if we have to adjust point. */
14579 int new_vpos = -1;
14580
14581 w->force_start = Qnil;
14582 w->vscroll = 0;
14583 w->window_end_valid = Qnil;
14584
14585 /* Forget any recorded base line for line number display. */
14586 if (!buffer_unchanged_p)
14587 w->base_line_number = Qnil;
14588
14589 /* Redisplay the mode line. Select the buffer properly for that.
14590 Also, run the hook window-scroll-functions
14591 because we have scrolled. */
14592 /* Note, we do this after clearing force_start because
14593 if there's an error, it is better to forget about force_start
14594 than to get into an infinite loop calling the hook functions
14595 and having them get more errors. */
14596 if (!update_mode_line
14597 || ! NILP (Vwindow_scroll_functions))
14598 {
14599 update_mode_line = 1;
14600 w->update_mode_line = Qt;
14601 startp = run_window_scroll_functions (window, startp);
14602 }
14603
14604 w->last_modified = make_number (0);
14605 w->last_overlay_modified = make_number (0);
14606 if (CHARPOS (startp) < BEGV)
14607 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14608 else if (CHARPOS (startp) > ZV)
14609 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14610
14611 /* Redisplay, then check if cursor has been set during the
14612 redisplay. Give up if new fonts were loaded. */
14613 /* We used to issue a CHECK_MARGINS argument to try_window here,
14614 but this causes scrolling to fail when point begins inside
14615 the scroll margin (bug#148) -- cyd */
14616 if (!try_window (window, startp, 0))
14617 {
14618 w->force_start = Qt;
14619 clear_glyph_matrix (w->desired_matrix);
14620 goto need_larger_matrices;
14621 }
14622
14623 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14624 {
14625 /* If point does not appear, try to move point so it does
14626 appear. The desired matrix has been built above, so we
14627 can use it here. */
14628 new_vpos = window_box_height (w) / 2;
14629 }
14630
14631 if (!cursor_row_fully_visible_p (w, 0, 0))
14632 {
14633 /* Point does appear, but on a line partly visible at end of window.
14634 Move it back to a fully-visible line. */
14635 new_vpos = window_box_height (w);
14636 }
14637
14638 /* If we need to move point for either of the above reasons,
14639 now actually do it. */
14640 if (new_vpos >= 0)
14641 {
14642 struct glyph_row *row;
14643
14644 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14645 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14646 ++row;
14647
14648 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14649 MATRIX_ROW_START_BYTEPOS (row));
14650
14651 if (w != XWINDOW (selected_window))
14652 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14653 else if (current_buffer == old)
14654 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14655
14656 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14657
14658 /* If we are highlighting the region, then we just changed
14659 the region, so redisplay to show it. */
14660 if (!NILP (Vtransient_mark_mode)
14661 && !NILP (BVAR (current_buffer, mark_active)))
14662 {
14663 clear_glyph_matrix (w->desired_matrix);
14664 if (!try_window (window, startp, 0))
14665 goto need_larger_matrices;
14666 }
14667 }
14668
14669 #if GLYPH_DEBUG
14670 debug_method_add (w, "forced window start");
14671 #endif
14672 goto done;
14673 }
14674
14675 /* Handle case where text has not changed, only point, and it has
14676 not moved off the frame, and we are not retrying after hscroll.
14677 (current_matrix_up_to_date_p is nonzero when retrying.) */
14678 if (current_matrix_up_to_date_p
14679 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14680 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14681 {
14682 switch (rc)
14683 {
14684 case CURSOR_MOVEMENT_SUCCESS:
14685 used_current_matrix_p = 1;
14686 goto done;
14687
14688 case CURSOR_MOVEMENT_MUST_SCROLL:
14689 goto try_to_scroll;
14690
14691 default:
14692 abort ();
14693 }
14694 }
14695 /* If current starting point was originally the beginning of a line
14696 but no longer is, find a new starting point. */
14697 else if (!NILP (w->start_at_line_beg)
14698 && !(CHARPOS (startp) <= BEGV
14699 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14700 {
14701 #if GLYPH_DEBUG
14702 debug_method_add (w, "recenter 1");
14703 #endif
14704 goto recenter;
14705 }
14706
14707 /* Try scrolling with try_window_id. Value is > 0 if update has
14708 been done, it is -1 if we know that the same window start will
14709 not work. It is 0 if unsuccessful for some other reason. */
14710 else if ((tem = try_window_id (w)) != 0)
14711 {
14712 #if GLYPH_DEBUG
14713 debug_method_add (w, "try_window_id %d", tem);
14714 #endif
14715
14716 if (fonts_changed_p)
14717 goto need_larger_matrices;
14718 if (tem > 0)
14719 goto done;
14720
14721 /* Otherwise try_window_id has returned -1 which means that we
14722 don't want the alternative below this comment to execute. */
14723 }
14724 else if (CHARPOS (startp) >= BEGV
14725 && CHARPOS (startp) <= ZV
14726 && PT >= CHARPOS (startp)
14727 && (CHARPOS (startp) < ZV
14728 /* Avoid starting at end of buffer. */
14729 || CHARPOS (startp) == BEGV
14730 || (XFASTINT (w->last_modified) >= MODIFF
14731 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14732 {
14733
14734 /* If first window line is a continuation line, and window start
14735 is inside the modified region, but the first change is before
14736 current window start, we must select a new window start.
14737
14738 However, if this is the result of a down-mouse event (e.g. by
14739 extending the mouse-drag-overlay), we don't want to select a
14740 new window start, since that would change the position under
14741 the mouse, resulting in an unwanted mouse-movement rather
14742 than a simple mouse-click. */
14743 if (NILP (w->start_at_line_beg)
14744 && NILP (do_mouse_tracking)
14745 && CHARPOS (startp) > BEGV
14746 && CHARPOS (startp) > BEG + beg_unchanged
14747 && CHARPOS (startp) <= Z - end_unchanged
14748 /* Even if w->start_at_line_beg is nil, a new window may
14749 start at a line_beg, since that's how set_buffer_window
14750 sets it. So, we need to check the return value of
14751 compute_window_start_on_continuation_line. (See also
14752 bug#197). */
14753 && XMARKER (w->start)->buffer == current_buffer
14754 && compute_window_start_on_continuation_line (w))
14755 {
14756 w->force_start = Qt;
14757 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14758 goto force_start;
14759 }
14760
14761 #if GLYPH_DEBUG
14762 debug_method_add (w, "same window start");
14763 #endif
14764
14765 /* Try to redisplay starting at same place as before.
14766 If point has not moved off frame, accept the results. */
14767 if (!current_matrix_up_to_date_p
14768 /* Don't use try_window_reusing_current_matrix in this case
14769 because a window scroll function can have changed the
14770 buffer. */
14771 || !NILP (Vwindow_scroll_functions)
14772 || MINI_WINDOW_P (w)
14773 || !(used_current_matrix_p
14774 = try_window_reusing_current_matrix (w)))
14775 {
14776 IF_DEBUG (debug_method_add (w, "1"));
14777 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14778 /* -1 means we need to scroll.
14779 0 means we need new matrices, but fonts_changed_p
14780 is set in that case, so we will detect it below. */
14781 goto try_to_scroll;
14782 }
14783
14784 if (fonts_changed_p)
14785 goto need_larger_matrices;
14786
14787 if (w->cursor.vpos >= 0)
14788 {
14789 if (!just_this_one_p
14790 || current_buffer->clip_changed
14791 || BEG_UNCHANGED < CHARPOS (startp))
14792 /* Forget any recorded base line for line number display. */
14793 w->base_line_number = Qnil;
14794
14795 if (!cursor_row_fully_visible_p (w, 1, 0))
14796 {
14797 clear_glyph_matrix (w->desired_matrix);
14798 last_line_misfit = 1;
14799 }
14800 /* Drop through and scroll. */
14801 else
14802 goto done;
14803 }
14804 else
14805 clear_glyph_matrix (w->desired_matrix);
14806 }
14807
14808 try_to_scroll:
14809
14810 w->last_modified = make_number (0);
14811 w->last_overlay_modified = make_number (0);
14812
14813 /* Redisplay the mode line. Select the buffer properly for that. */
14814 if (!update_mode_line)
14815 {
14816 update_mode_line = 1;
14817 w->update_mode_line = Qt;
14818 }
14819
14820 /* Try to scroll by specified few lines. */
14821 if ((scroll_conservatively
14822 || emacs_scroll_step
14823 || temp_scroll_step
14824 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14825 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14826 && CHARPOS (startp) >= BEGV
14827 && CHARPOS (startp) <= ZV)
14828 {
14829 /* The function returns -1 if new fonts were loaded, 1 if
14830 successful, 0 if not successful. */
14831 int ss = try_scrolling (window, just_this_one_p,
14832 scroll_conservatively,
14833 emacs_scroll_step,
14834 temp_scroll_step, last_line_misfit);
14835 switch (ss)
14836 {
14837 case SCROLLING_SUCCESS:
14838 goto done;
14839
14840 case SCROLLING_NEED_LARGER_MATRICES:
14841 goto need_larger_matrices;
14842
14843 case SCROLLING_FAILED:
14844 break;
14845
14846 default:
14847 abort ();
14848 }
14849 }
14850
14851 /* Finally, just choose a place to start which positions point
14852 according to user preferences. */
14853
14854 recenter:
14855
14856 #if GLYPH_DEBUG
14857 debug_method_add (w, "recenter");
14858 #endif
14859
14860 /* w->vscroll = 0; */
14861
14862 /* Forget any previously recorded base line for line number display. */
14863 if (!buffer_unchanged_p)
14864 w->base_line_number = Qnil;
14865
14866 /* Determine the window start relative to point. */
14867 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14868 it.current_y = it.last_visible_y;
14869 if (centering_position < 0)
14870 {
14871 int margin =
14872 scroll_margin > 0
14873 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14874 : 0;
14875 EMACS_INT margin_pos = CHARPOS (startp);
14876 int scrolling_up;
14877 Lisp_Object aggressive;
14878
14879 /* If there is a scroll margin at the top of the window, find
14880 its character position. */
14881 if (margin
14882 /* Cannot call start_display if startp is not in the
14883 accessible region of the buffer. This can happen when we
14884 have just switched to a different buffer and/or changed
14885 its restriction. In that case, startp is initialized to
14886 the character position 1 (BEG) because we did not yet
14887 have chance to display the buffer even once. */
14888 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14889 {
14890 struct it it1;
14891
14892 start_display (&it1, w, startp);
14893 move_it_vertically (&it1, margin);
14894 margin_pos = IT_CHARPOS (it1);
14895 }
14896 scrolling_up = PT > margin_pos;
14897 aggressive =
14898 scrolling_up
14899 ? BVAR (current_buffer, scroll_up_aggressively)
14900 : BVAR (current_buffer, scroll_down_aggressively);
14901
14902 if (!MINI_WINDOW_P (w)
14903 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
14904 {
14905 int pt_offset = 0;
14906
14907 /* Setting scroll-conservatively overrides
14908 scroll-*-aggressively. */
14909 if (!scroll_conservatively && NUMBERP (aggressive))
14910 {
14911 double float_amount = XFLOATINT (aggressive);
14912
14913 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14914 if (pt_offset == 0 && float_amount > 0)
14915 pt_offset = 1;
14916 if (pt_offset)
14917 margin -= 1;
14918 }
14919 /* Compute how much to move the window start backward from
14920 point so that point will be displayed where the user
14921 wants it. */
14922 if (scrolling_up)
14923 {
14924 centering_position = it.last_visible_y;
14925 if (pt_offset)
14926 centering_position -= pt_offset;
14927 centering_position -=
14928 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
14929 /* Don't let point enter the scroll margin near top of
14930 the window. */
14931 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
14932 centering_position = margin * FRAME_LINE_HEIGHT (f);
14933 }
14934 else
14935 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
14936 }
14937 else
14938 /* Set the window start half the height of the window backward
14939 from point. */
14940 centering_position = window_box_height (w) / 2;
14941 }
14942 move_it_vertically_backward (&it, centering_position);
14943
14944 xassert (IT_CHARPOS (it) >= BEGV);
14945
14946 /* The function move_it_vertically_backward may move over more
14947 than the specified y-distance. If it->w is small, e.g. a
14948 mini-buffer window, we may end up in front of the window's
14949 display area. Start displaying at the start of the line
14950 containing PT in this case. */
14951 if (it.current_y <= 0)
14952 {
14953 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14954 move_it_vertically_backward (&it, 0);
14955 it.current_y = 0;
14956 }
14957
14958 it.current_x = it.hpos = 0;
14959
14960 /* Set the window start position here explicitly, to avoid an
14961 infinite loop in case the functions in window-scroll-functions
14962 get errors. */
14963 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14964
14965 /* Run scroll hooks. */
14966 startp = run_window_scroll_functions (window, it.current.pos);
14967
14968 /* Redisplay the window. */
14969 if (!current_matrix_up_to_date_p
14970 || windows_or_buffers_changed
14971 || cursor_type_changed
14972 /* Don't use try_window_reusing_current_matrix in this case
14973 because it can have changed the buffer. */
14974 || !NILP (Vwindow_scroll_functions)
14975 || !just_this_one_p
14976 || MINI_WINDOW_P (w)
14977 || !(used_current_matrix_p
14978 = try_window_reusing_current_matrix (w)))
14979 try_window (window, startp, 0);
14980
14981 /* If new fonts have been loaded (due to fontsets), give up. We
14982 have to start a new redisplay since we need to re-adjust glyph
14983 matrices. */
14984 if (fonts_changed_p)
14985 goto need_larger_matrices;
14986
14987 /* If cursor did not appear assume that the middle of the window is
14988 in the first line of the window. Do it again with the next line.
14989 (Imagine a window of height 100, displaying two lines of height
14990 60. Moving back 50 from it->last_visible_y will end in the first
14991 line.) */
14992 if (w->cursor.vpos < 0)
14993 {
14994 if (!NILP (w->window_end_valid)
14995 && PT >= Z - XFASTINT (w->window_end_pos))
14996 {
14997 clear_glyph_matrix (w->desired_matrix);
14998 move_it_by_lines (&it, 1);
14999 try_window (window, it.current.pos, 0);
15000 }
15001 else if (PT < IT_CHARPOS (it))
15002 {
15003 clear_glyph_matrix (w->desired_matrix);
15004 move_it_by_lines (&it, -1);
15005 try_window (window, it.current.pos, 0);
15006 }
15007 else
15008 {
15009 /* Not much we can do about it. */
15010 }
15011 }
15012
15013 /* Consider the following case: Window starts at BEGV, there is
15014 invisible, intangible text at BEGV, so that display starts at
15015 some point START > BEGV. It can happen that we are called with
15016 PT somewhere between BEGV and START. Try to handle that case. */
15017 if (w->cursor.vpos < 0)
15018 {
15019 struct glyph_row *row = w->current_matrix->rows;
15020 if (row->mode_line_p)
15021 ++row;
15022 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15023 }
15024
15025 if (!cursor_row_fully_visible_p (w, 0, 0))
15026 {
15027 /* If vscroll is enabled, disable it and try again. */
15028 if (w->vscroll)
15029 {
15030 w->vscroll = 0;
15031 clear_glyph_matrix (w->desired_matrix);
15032 goto recenter;
15033 }
15034
15035 /* If centering point failed to make the whole line visible,
15036 put point at the top instead. That has to make the whole line
15037 visible, if it can be done. */
15038 if (centering_position == 0)
15039 goto done;
15040
15041 clear_glyph_matrix (w->desired_matrix);
15042 centering_position = 0;
15043 goto recenter;
15044 }
15045
15046 done:
15047
15048 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15049 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15050 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15051 ? Qt : Qnil);
15052
15053 /* Display the mode line, if we must. */
15054 if ((update_mode_line
15055 /* If window not full width, must redo its mode line
15056 if (a) the window to its side is being redone and
15057 (b) we do a frame-based redisplay. This is a consequence
15058 of how inverted lines are drawn in frame-based redisplay. */
15059 || (!just_this_one_p
15060 && !FRAME_WINDOW_P (f)
15061 && !WINDOW_FULL_WIDTH_P (w))
15062 /* Line number to display. */
15063 || INTEGERP (w->base_line_pos)
15064 /* Column number is displayed and different from the one displayed. */
15065 || (!NILP (w->column_number_displayed)
15066 && (XFASTINT (w->column_number_displayed) != current_column ())))
15067 /* This means that the window has a mode line. */
15068 && (WINDOW_WANTS_MODELINE_P (w)
15069 || WINDOW_WANTS_HEADER_LINE_P (w)))
15070 {
15071 display_mode_lines (w);
15072
15073 /* If mode line height has changed, arrange for a thorough
15074 immediate redisplay using the correct mode line height. */
15075 if (WINDOW_WANTS_MODELINE_P (w)
15076 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15077 {
15078 fonts_changed_p = 1;
15079 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15080 = DESIRED_MODE_LINE_HEIGHT (w);
15081 }
15082
15083 /* If header line height has changed, arrange for a thorough
15084 immediate redisplay using the correct header line height. */
15085 if (WINDOW_WANTS_HEADER_LINE_P (w)
15086 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15087 {
15088 fonts_changed_p = 1;
15089 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15090 = DESIRED_HEADER_LINE_HEIGHT (w);
15091 }
15092
15093 if (fonts_changed_p)
15094 goto need_larger_matrices;
15095 }
15096
15097 if (!line_number_displayed
15098 && !BUFFERP (w->base_line_pos))
15099 {
15100 w->base_line_pos = Qnil;
15101 w->base_line_number = Qnil;
15102 }
15103
15104 finish_menu_bars:
15105
15106 /* When we reach a frame's selected window, redo the frame's menu bar. */
15107 if (update_mode_line
15108 && EQ (FRAME_SELECTED_WINDOW (f), window))
15109 {
15110 int redisplay_menu_p = 0;
15111
15112 if (FRAME_WINDOW_P (f))
15113 {
15114 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15115 || defined (HAVE_NS) || defined (USE_GTK)
15116 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15117 #else
15118 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15119 #endif
15120 }
15121 else
15122 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15123
15124 if (redisplay_menu_p)
15125 display_menu_bar (w);
15126
15127 #ifdef HAVE_WINDOW_SYSTEM
15128 if (FRAME_WINDOW_P (f))
15129 {
15130 #if defined (USE_GTK) || defined (HAVE_NS)
15131 if (FRAME_EXTERNAL_TOOL_BAR (f))
15132 redisplay_tool_bar (f);
15133 #else
15134 if (WINDOWP (f->tool_bar_window)
15135 && (FRAME_TOOL_BAR_LINES (f) > 0
15136 || !NILP (Vauto_resize_tool_bars))
15137 && redisplay_tool_bar (f))
15138 ignore_mouse_drag_p = 1;
15139 #endif
15140 }
15141 #endif
15142 }
15143
15144 #ifdef HAVE_WINDOW_SYSTEM
15145 if (FRAME_WINDOW_P (f)
15146 && update_window_fringes (w, (just_this_one_p
15147 || (!used_current_matrix_p && !overlay_arrow_seen)
15148 || w->pseudo_window_p)))
15149 {
15150 update_begin (f);
15151 BLOCK_INPUT;
15152 if (draw_window_fringes (w, 1))
15153 x_draw_vertical_border (w);
15154 UNBLOCK_INPUT;
15155 update_end (f);
15156 }
15157 #endif /* HAVE_WINDOW_SYSTEM */
15158
15159 /* We go to this label, with fonts_changed_p nonzero,
15160 if it is necessary to try again using larger glyph matrices.
15161 We have to redeem the scroll bar even in this case,
15162 because the loop in redisplay_internal expects that. */
15163 need_larger_matrices:
15164 ;
15165 finish_scroll_bars:
15166
15167 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15168 {
15169 /* Set the thumb's position and size. */
15170 set_vertical_scroll_bar (w);
15171
15172 /* Note that we actually used the scroll bar attached to this
15173 window, so it shouldn't be deleted at the end of redisplay. */
15174 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15175 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15176 }
15177
15178 /* Restore current_buffer and value of point in it. The window
15179 update may have changed the buffer, so first make sure `opoint'
15180 is still valid (Bug#6177). */
15181 if (CHARPOS (opoint) < BEGV)
15182 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15183 else if (CHARPOS (opoint) > ZV)
15184 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15185 else
15186 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15187
15188 set_buffer_internal_1 (old);
15189 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15190 shorter. This can be caused by log truncation in *Messages*. */
15191 if (CHARPOS (lpoint) <= ZV)
15192 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15193
15194 unbind_to (count, Qnil);
15195 }
15196
15197
15198 /* Build the complete desired matrix of WINDOW with a window start
15199 buffer position POS.
15200
15201 Value is 1 if successful. It is zero if fonts were loaded during
15202 redisplay which makes re-adjusting glyph matrices necessary, and -1
15203 if point would appear in the scroll margins.
15204 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15205 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15206 set in FLAGS.) */
15207
15208 int
15209 try_window (Lisp_Object window, struct text_pos pos, int flags)
15210 {
15211 struct window *w = XWINDOW (window);
15212 struct it it;
15213 struct glyph_row *last_text_row = NULL;
15214 struct frame *f = XFRAME (w->frame);
15215
15216 /* Make POS the new window start. */
15217 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15218
15219 /* Mark cursor position as unknown. No overlay arrow seen. */
15220 w->cursor.vpos = -1;
15221 overlay_arrow_seen = 0;
15222
15223 /* Initialize iterator and info to start at POS. */
15224 start_display (&it, w, pos);
15225
15226 /* Display all lines of W. */
15227 while (it.current_y < it.last_visible_y)
15228 {
15229 if (display_line (&it))
15230 last_text_row = it.glyph_row - 1;
15231 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15232 return 0;
15233 }
15234
15235 /* Don't let the cursor end in the scroll margins. */
15236 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15237 && !MINI_WINDOW_P (w))
15238 {
15239 int this_scroll_margin;
15240
15241 if (scroll_margin > 0)
15242 {
15243 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15244 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15245 }
15246 else
15247 this_scroll_margin = 0;
15248
15249 if ((w->cursor.y >= 0 /* not vscrolled */
15250 && w->cursor.y < this_scroll_margin
15251 && CHARPOS (pos) > BEGV
15252 && IT_CHARPOS (it) < ZV)
15253 /* rms: considering make_cursor_line_fully_visible_p here
15254 seems to give wrong results. We don't want to recenter
15255 when the last line is partly visible, we want to allow
15256 that case to be handled in the usual way. */
15257 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15258 {
15259 w->cursor.vpos = -1;
15260 clear_glyph_matrix (w->desired_matrix);
15261 return -1;
15262 }
15263 }
15264
15265 /* If bottom moved off end of frame, change mode line percentage. */
15266 if (XFASTINT (w->window_end_pos) <= 0
15267 && Z != IT_CHARPOS (it))
15268 w->update_mode_line = Qt;
15269
15270 /* Set window_end_pos to the offset of the last character displayed
15271 on the window from the end of current_buffer. Set
15272 window_end_vpos to its row number. */
15273 if (last_text_row)
15274 {
15275 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15276 w->window_end_bytepos
15277 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15278 w->window_end_pos
15279 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15280 w->window_end_vpos
15281 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15282 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15283 ->displays_text_p);
15284 }
15285 else
15286 {
15287 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15288 w->window_end_pos = make_number (Z - ZV);
15289 w->window_end_vpos = make_number (0);
15290 }
15291
15292 /* But that is not valid info until redisplay finishes. */
15293 w->window_end_valid = Qnil;
15294 return 1;
15295 }
15296
15297
15298 \f
15299 /************************************************************************
15300 Window redisplay reusing current matrix when buffer has not changed
15301 ************************************************************************/
15302
15303 /* Try redisplay of window W showing an unchanged buffer with a
15304 different window start than the last time it was displayed by
15305 reusing its current matrix. Value is non-zero if successful.
15306 W->start is the new window start. */
15307
15308 static int
15309 try_window_reusing_current_matrix (struct window *w)
15310 {
15311 struct frame *f = XFRAME (w->frame);
15312 struct glyph_row *bottom_row;
15313 struct it it;
15314 struct run run;
15315 struct text_pos start, new_start;
15316 int nrows_scrolled, i;
15317 struct glyph_row *last_text_row;
15318 struct glyph_row *last_reused_text_row;
15319 struct glyph_row *start_row;
15320 int start_vpos, min_y, max_y;
15321
15322 #if GLYPH_DEBUG
15323 if (inhibit_try_window_reusing)
15324 return 0;
15325 #endif
15326
15327 if (/* This function doesn't handle terminal frames. */
15328 !FRAME_WINDOW_P (f)
15329 /* Don't try to reuse the display if windows have been split
15330 or such. */
15331 || windows_or_buffers_changed
15332 || cursor_type_changed)
15333 return 0;
15334
15335 /* Can't do this if region may have changed. */
15336 if ((!NILP (Vtransient_mark_mode)
15337 && !NILP (BVAR (current_buffer, mark_active)))
15338 || !NILP (w->region_showing)
15339 || !NILP (Vshow_trailing_whitespace))
15340 return 0;
15341
15342 /* If top-line visibility has changed, give up. */
15343 if (WINDOW_WANTS_HEADER_LINE_P (w)
15344 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15345 return 0;
15346
15347 /* Give up if old or new display is scrolled vertically. We could
15348 make this function handle this, but right now it doesn't. */
15349 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15350 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15351 return 0;
15352
15353 /* The variable new_start now holds the new window start. The old
15354 start `start' can be determined from the current matrix. */
15355 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15356 start = start_row->minpos;
15357 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15358
15359 /* Clear the desired matrix for the display below. */
15360 clear_glyph_matrix (w->desired_matrix);
15361
15362 if (CHARPOS (new_start) <= CHARPOS (start))
15363 {
15364 /* Don't use this method if the display starts with an ellipsis
15365 displayed for invisible text. It's not easy to handle that case
15366 below, and it's certainly not worth the effort since this is
15367 not a frequent case. */
15368 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15369 return 0;
15370
15371 IF_DEBUG (debug_method_add (w, "twu1"));
15372
15373 /* Display up to a row that can be reused. The variable
15374 last_text_row is set to the last row displayed that displays
15375 text. Note that it.vpos == 0 if or if not there is a
15376 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15377 start_display (&it, w, new_start);
15378 w->cursor.vpos = -1;
15379 last_text_row = last_reused_text_row = NULL;
15380
15381 while (it.current_y < it.last_visible_y
15382 && !fonts_changed_p)
15383 {
15384 /* If we have reached into the characters in the START row,
15385 that means the line boundaries have changed. So we
15386 can't start copying with the row START. Maybe it will
15387 work to start copying with the following row. */
15388 while (IT_CHARPOS (it) > CHARPOS (start))
15389 {
15390 /* Advance to the next row as the "start". */
15391 start_row++;
15392 start = start_row->minpos;
15393 /* If there are no more rows to try, or just one, give up. */
15394 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15395 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15396 || CHARPOS (start) == ZV)
15397 {
15398 clear_glyph_matrix (w->desired_matrix);
15399 return 0;
15400 }
15401
15402 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15403 }
15404 /* If we have reached alignment,
15405 we can copy the rest of the rows. */
15406 if (IT_CHARPOS (it) == CHARPOS (start))
15407 break;
15408
15409 if (display_line (&it))
15410 last_text_row = it.glyph_row - 1;
15411 }
15412
15413 /* A value of current_y < last_visible_y means that we stopped
15414 at the previous window start, which in turn means that we
15415 have at least one reusable row. */
15416 if (it.current_y < it.last_visible_y)
15417 {
15418 struct glyph_row *row;
15419
15420 /* IT.vpos always starts from 0; it counts text lines. */
15421 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15422
15423 /* Find PT if not already found in the lines displayed. */
15424 if (w->cursor.vpos < 0)
15425 {
15426 int dy = it.current_y - start_row->y;
15427
15428 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15429 row = row_containing_pos (w, PT, row, NULL, dy);
15430 if (row)
15431 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15432 dy, nrows_scrolled);
15433 else
15434 {
15435 clear_glyph_matrix (w->desired_matrix);
15436 return 0;
15437 }
15438 }
15439
15440 /* Scroll the display. Do it before the current matrix is
15441 changed. The problem here is that update has not yet
15442 run, i.e. part of the current matrix is not up to date.
15443 scroll_run_hook will clear the cursor, and use the
15444 current matrix to get the height of the row the cursor is
15445 in. */
15446 run.current_y = start_row->y;
15447 run.desired_y = it.current_y;
15448 run.height = it.last_visible_y - it.current_y;
15449
15450 if (run.height > 0 && run.current_y != run.desired_y)
15451 {
15452 update_begin (f);
15453 FRAME_RIF (f)->update_window_begin_hook (w);
15454 FRAME_RIF (f)->clear_window_mouse_face (w);
15455 FRAME_RIF (f)->scroll_run_hook (w, &run);
15456 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15457 update_end (f);
15458 }
15459
15460 /* Shift current matrix down by nrows_scrolled lines. */
15461 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15462 rotate_matrix (w->current_matrix,
15463 start_vpos,
15464 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15465 nrows_scrolled);
15466
15467 /* Disable lines that must be updated. */
15468 for (i = 0; i < nrows_scrolled; ++i)
15469 (start_row + i)->enabled_p = 0;
15470
15471 /* Re-compute Y positions. */
15472 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15473 max_y = it.last_visible_y;
15474 for (row = start_row + nrows_scrolled;
15475 row < bottom_row;
15476 ++row)
15477 {
15478 row->y = it.current_y;
15479 row->visible_height = row->height;
15480
15481 if (row->y < min_y)
15482 row->visible_height -= min_y - row->y;
15483 if (row->y + row->height > max_y)
15484 row->visible_height -= row->y + row->height - max_y;
15485 row->redraw_fringe_bitmaps_p = 1;
15486
15487 it.current_y += row->height;
15488
15489 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15490 last_reused_text_row = row;
15491 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15492 break;
15493 }
15494
15495 /* Disable lines in the current matrix which are now
15496 below the window. */
15497 for (++row; row < bottom_row; ++row)
15498 row->enabled_p = row->mode_line_p = 0;
15499 }
15500
15501 /* Update window_end_pos etc.; last_reused_text_row is the last
15502 reused row from the current matrix containing text, if any.
15503 The value of last_text_row is the last displayed line
15504 containing text. */
15505 if (last_reused_text_row)
15506 {
15507 w->window_end_bytepos
15508 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15509 w->window_end_pos
15510 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15511 w->window_end_vpos
15512 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15513 w->current_matrix));
15514 }
15515 else if (last_text_row)
15516 {
15517 w->window_end_bytepos
15518 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15519 w->window_end_pos
15520 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15521 w->window_end_vpos
15522 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15523 }
15524 else
15525 {
15526 /* This window must be completely empty. */
15527 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15528 w->window_end_pos = make_number (Z - ZV);
15529 w->window_end_vpos = make_number (0);
15530 }
15531 w->window_end_valid = Qnil;
15532
15533 /* Update hint: don't try scrolling again in update_window. */
15534 w->desired_matrix->no_scrolling_p = 1;
15535
15536 #if GLYPH_DEBUG
15537 debug_method_add (w, "try_window_reusing_current_matrix 1");
15538 #endif
15539 return 1;
15540 }
15541 else if (CHARPOS (new_start) > CHARPOS (start))
15542 {
15543 struct glyph_row *pt_row, *row;
15544 struct glyph_row *first_reusable_row;
15545 struct glyph_row *first_row_to_display;
15546 int dy;
15547 int yb = window_text_bottom_y (w);
15548
15549 /* Find the row starting at new_start, if there is one. Don't
15550 reuse a partially visible line at the end. */
15551 first_reusable_row = start_row;
15552 while (first_reusable_row->enabled_p
15553 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15554 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15555 < CHARPOS (new_start)))
15556 ++first_reusable_row;
15557
15558 /* Give up if there is no row to reuse. */
15559 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15560 || !first_reusable_row->enabled_p
15561 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15562 != CHARPOS (new_start)))
15563 return 0;
15564
15565 /* We can reuse fully visible rows beginning with
15566 first_reusable_row to the end of the window. Set
15567 first_row_to_display to the first row that cannot be reused.
15568 Set pt_row to the row containing point, if there is any. */
15569 pt_row = NULL;
15570 for (first_row_to_display = first_reusable_row;
15571 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15572 ++first_row_to_display)
15573 {
15574 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15575 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15576 pt_row = first_row_to_display;
15577 }
15578
15579 /* Start displaying at the start of first_row_to_display. */
15580 xassert (first_row_to_display->y < yb);
15581 init_to_row_start (&it, w, first_row_to_display);
15582
15583 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15584 - start_vpos);
15585 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15586 - nrows_scrolled);
15587 it.current_y = (first_row_to_display->y - first_reusable_row->y
15588 + WINDOW_HEADER_LINE_HEIGHT (w));
15589
15590 /* Display lines beginning with first_row_to_display in the
15591 desired matrix. Set last_text_row to the last row displayed
15592 that displays text. */
15593 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15594 if (pt_row == NULL)
15595 w->cursor.vpos = -1;
15596 last_text_row = NULL;
15597 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15598 if (display_line (&it))
15599 last_text_row = it.glyph_row - 1;
15600
15601 /* If point is in a reused row, adjust y and vpos of the cursor
15602 position. */
15603 if (pt_row)
15604 {
15605 w->cursor.vpos -= nrows_scrolled;
15606 w->cursor.y -= first_reusable_row->y - start_row->y;
15607 }
15608
15609 /* Give up if point isn't in a row displayed or reused. (This
15610 also handles the case where w->cursor.vpos < nrows_scrolled
15611 after the calls to display_line, which can happen with scroll
15612 margins. See bug#1295.) */
15613 if (w->cursor.vpos < 0)
15614 {
15615 clear_glyph_matrix (w->desired_matrix);
15616 return 0;
15617 }
15618
15619 /* Scroll the display. */
15620 run.current_y = first_reusable_row->y;
15621 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15622 run.height = it.last_visible_y - run.current_y;
15623 dy = run.current_y - run.desired_y;
15624
15625 if (run.height)
15626 {
15627 update_begin (f);
15628 FRAME_RIF (f)->update_window_begin_hook (w);
15629 FRAME_RIF (f)->clear_window_mouse_face (w);
15630 FRAME_RIF (f)->scroll_run_hook (w, &run);
15631 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15632 update_end (f);
15633 }
15634
15635 /* Adjust Y positions of reused rows. */
15636 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15637 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15638 max_y = it.last_visible_y;
15639 for (row = first_reusable_row; row < first_row_to_display; ++row)
15640 {
15641 row->y -= dy;
15642 row->visible_height = row->height;
15643 if (row->y < min_y)
15644 row->visible_height -= min_y - row->y;
15645 if (row->y + row->height > max_y)
15646 row->visible_height -= row->y + row->height - max_y;
15647 row->redraw_fringe_bitmaps_p = 1;
15648 }
15649
15650 /* Scroll the current matrix. */
15651 xassert (nrows_scrolled > 0);
15652 rotate_matrix (w->current_matrix,
15653 start_vpos,
15654 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15655 -nrows_scrolled);
15656
15657 /* Disable rows not reused. */
15658 for (row -= nrows_scrolled; row < bottom_row; ++row)
15659 row->enabled_p = 0;
15660
15661 /* Point may have moved to a different line, so we cannot assume that
15662 the previous cursor position is valid; locate the correct row. */
15663 if (pt_row)
15664 {
15665 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15666 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15667 row++)
15668 {
15669 w->cursor.vpos++;
15670 w->cursor.y = row->y;
15671 }
15672 if (row < bottom_row)
15673 {
15674 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15675 struct glyph *end = glyph + row->used[TEXT_AREA];
15676
15677 /* Can't use this optimization with bidi-reordered glyph
15678 rows, unless cursor is already at point. */
15679 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15680 {
15681 if (!(w->cursor.hpos >= 0
15682 && w->cursor.hpos < row->used[TEXT_AREA]
15683 && BUFFERP (glyph->object)
15684 && glyph->charpos == PT))
15685 return 0;
15686 }
15687 else
15688 for (; glyph < end
15689 && (!BUFFERP (glyph->object)
15690 || glyph->charpos < PT);
15691 glyph++)
15692 {
15693 w->cursor.hpos++;
15694 w->cursor.x += glyph->pixel_width;
15695 }
15696 }
15697 }
15698
15699 /* Adjust window end. A null value of last_text_row means that
15700 the window end is in reused rows which in turn means that
15701 only its vpos can have changed. */
15702 if (last_text_row)
15703 {
15704 w->window_end_bytepos
15705 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15706 w->window_end_pos
15707 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15708 w->window_end_vpos
15709 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15710 }
15711 else
15712 {
15713 w->window_end_vpos
15714 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15715 }
15716
15717 w->window_end_valid = Qnil;
15718 w->desired_matrix->no_scrolling_p = 1;
15719
15720 #if GLYPH_DEBUG
15721 debug_method_add (w, "try_window_reusing_current_matrix 2");
15722 #endif
15723 return 1;
15724 }
15725
15726 return 0;
15727 }
15728
15729
15730 \f
15731 /************************************************************************
15732 Window redisplay reusing current matrix when buffer has changed
15733 ************************************************************************/
15734
15735 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15736 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15737 EMACS_INT *, EMACS_INT *);
15738 static struct glyph_row *
15739 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15740 struct glyph_row *);
15741
15742
15743 /* Return the last row in MATRIX displaying text. If row START is
15744 non-null, start searching with that row. IT gives the dimensions
15745 of the display. Value is null if matrix is empty; otherwise it is
15746 a pointer to the row found. */
15747
15748 static struct glyph_row *
15749 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15750 struct glyph_row *start)
15751 {
15752 struct glyph_row *row, *row_found;
15753
15754 /* Set row_found to the last row in IT->w's current matrix
15755 displaying text. The loop looks funny but think of partially
15756 visible lines. */
15757 row_found = NULL;
15758 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15759 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15760 {
15761 xassert (row->enabled_p);
15762 row_found = row;
15763 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15764 break;
15765 ++row;
15766 }
15767
15768 return row_found;
15769 }
15770
15771
15772 /* Return the last row in the current matrix of W that is not affected
15773 by changes at the start of current_buffer that occurred since W's
15774 current matrix was built. Value is null if no such row exists.
15775
15776 BEG_UNCHANGED us the number of characters unchanged at the start of
15777 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15778 first changed character in current_buffer. Characters at positions <
15779 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15780 when the current matrix was built. */
15781
15782 static struct glyph_row *
15783 find_last_unchanged_at_beg_row (struct window *w)
15784 {
15785 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15786 struct glyph_row *row;
15787 struct glyph_row *row_found = NULL;
15788 int yb = window_text_bottom_y (w);
15789
15790 /* Find the last row displaying unchanged text. */
15791 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15792 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15793 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15794 ++row)
15795 {
15796 if (/* If row ends before first_changed_pos, it is unchanged,
15797 except in some case. */
15798 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15799 /* When row ends in ZV and we write at ZV it is not
15800 unchanged. */
15801 && !row->ends_at_zv_p
15802 /* When first_changed_pos is the end of a continued line,
15803 row is not unchanged because it may be no longer
15804 continued. */
15805 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15806 && (row->continued_p
15807 || row->exact_window_width_line_p)))
15808 row_found = row;
15809
15810 /* Stop if last visible row. */
15811 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15812 break;
15813 }
15814
15815 return row_found;
15816 }
15817
15818
15819 /* Find the first glyph row in the current matrix of W that is not
15820 affected by changes at the end of current_buffer since the
15821 time W's current matrix was built.
15822
15823 Return in *DELTA the number of chars by which buffer positions in
15824 unchanged text at the end of current_buffer must be adjusted.
15825
15826 Return in *DELTA_BYTES the corresponding number of bytes.
15827
15828 Value is null if no such row exists, i.e. all rows are affected by
15829 changes. */
15830
15831 static struct glyph_row *
15832 find_first_unchanged_at_end_row (struct window *w,
15833 EMACS_INT *delta, EMACS_INT *delta_bytes)
15834 {
15835 struct glyph_row *row;
15836 struct glyph_row *row_found = NULL;
15837
15838 *delta = *delta_bytes = 0;
15839
15840 /* Display must not have been paused, otherwise the current matrix
15841 is not up to date. */
15842 eassert (!NILP (w->window_end_valid));
15843
15844 /* A value of window_end_pos >= END_UNCHANGED means that the window
15845 end is in the range of changed text. If so, there is no
15846 unchanged row at the end of W's current matrix. */
15847 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15848 return NULL;
15849
15850 /* Set row to the last row in W's current matrix displaying text. */
15851 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15852
15853 /* If matrix is entirely empty, no unchanged row exists. */
15854 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15855 {
15856 /* The value of row is the last glyph row in the matrix having a
15857 meaningful buffer position in it. The end position of row
15858 corresponds to window_end_pos. This allows us to translate
15859 buffer positions in the current matrix to current buffer
15860 positions for characters not in changed text. */
15861 EMACS_INT Z_old =
15862 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15863 EMACS_INT Z_BYTE_old =
15864 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15865 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15866 struct glyph_row *first_text_row
15867 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15868
15869 *delta = Z - Z_old;
15870 *delta_bytes = Z_BYTE - Z_BYTE_old;
15871
15872 /* Set last_unchanged_pos to the buffer position of the last
15873 character in the buffer that has not been changed. Z is the
15874 index + 1 of the last character in current_buffer, i.e. by
15875 subtracting END_UNCHANGED we get the index of the last
15876 unchanged character, and we have to add BEG to get its buffer
15877 position. */
15878 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15879 last_unchanged_pos_old = last_unchanged_pos - *delta;
15880
15881 /* Search backward from ROW for a row displaying a line that
15882 starts at a minimum position >= last_unchanged_pos_old. */
15883 for (; row > first_text_row; --row)
15884 {
15885 /* This used to abort, but it can happen.
15886 It is ok to just stop the search instead here. KFS. */
15887 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15888 break;
15889
15890 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15891 row_found = row;
15892 }
15893 }
15894
15895 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15896
15897 return row_found;
15898 }
15899
15900
15901 /* Make sure that glyph rows in the current matrix of window W
15902 reference the same glyph memory as corresponding rows in the
15903 frame's frame matrix. This function is called after scrolling W's
15904 current matrix on a terminal frame in try_window_id and
15905 try_window_reusing_current_matrix. */
15906
15907 static void
15908 sync_frame_with_window_matrix_rows (struct window *w)
15909 {
15910 struct frame *f = XFRAME (w->frame);
15911 struct glyph_row *window_row, *window_row_end, *frame_row;
15912
15913 /* Preconditions: W must be a leaf window and full-width. Its frame
15914 must have a frame matrix. */
15915 xassert (NILP (w->hchild) && NILP (w->vchild));
15916 xassert (WINDOW_FULL_WIDTH_P (w));
15917 xassert (!FRAME_WINDOW_P (f));
15918
15919 /* If W is a full-width window, glyph pointers in W's current matrix
15920 have, by definition, to be the same as glyph pointers in the
15921 corresponding frame matrix. Note that frame matrices have no
15922 marginal areas (see build_frame_matrix). */
15923 window_row = w->current_matrix->rows;
15924 window_row_end = window_row + w->current_matrix->nrows;
15925 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15926 while (window_row < window_row_end)
15927 {
15928 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15929 struct glyph *end = window_row->glyphs[LAST_AREA];
15930
15931 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15932 frame_row->glyphs[TEXT_AREA] = start;
15933 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15934 frame_row->glyphs[LAST_AREA] = end;
15935
15936 /* Disable frame rows whose corresponding window rows have
15937 been disabled in try_window_id. */
15938 if (!window_row->enabled_p)
15939 frame_row->enabled_p = 0;
15940
15941 ++window_row, ++frame_row;
15942 }
15943 }
15944
15945
15946 /* Find the glyph row in window W containing CHARPOS. Consider all
15947 rows between START and END (not inclusive). END null means search
15948 all rows to the end of the display area of W. Value is the row
15949 containing CHARPOS or null. */
15950
15951 struct glyph_row *
15952 row_containing_pos (struct window *w, EMACS_INT charpos,
15953 struct glyph_row *start, struct glyph_row *end, int dy)
15954 {
15955 struct glyph_row *row = start;
15956 struct glyph_row *best_row = NULL;
15957 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15958 int last_y;
15959
15960 /* If we happen to start on a header-line, skip that. */
15961 if (row->mode_line_p)
15962 ++row;
15963
15964 if ((end && row >= end) || !row->enabled_p)
15965 return NULL;
15966
15967 last_y = window_text_bottom_y (w) - dy;
15968
15969 while (1)
15970 {
15971 /* Give up if we have gone too far. */
15972 if (end && row >= end)
15973 return NULL;
15974 /* This formerly returned if they were equal.
15975 I think that both quantities are of a "last plus one" type;
15976 if so, when they are equal, the row is within the screen. -- rms. */
15977 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15978 return NULL;
15979
15980 /* If it is in this row, return this row. */
15981 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15982 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15983 /* The end position of a row equals the start
15984 position of the next row. If CHARPOS is there, we
15985 would rather display it in the next line, except
15986 when this line ends in ZV. */
15987 && !row->ends_at_zv_p
15988 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15989 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15990 {
15991 struct glyph *g;
15992
15993 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15994 || (!best_row && !row->continued_p))
15995 return row;
15996 /* In bidi-reordered rows, there could be several rows
15997 occluding point, all of them belonging to the same
15998 continued line. We need to find the row which fits
15999 CHARPOS the best. */
16000 for (g = row->glyphs[TEXT_AREA];
16001 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16002 g++)
16003 {
16004 if (!STRINGP (g->object))
16005 {
16006 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16007 {
16008 mindif = eabs (g->charpos - charpos);
16009 best_row = row;
16010 /* Exact match always wins. */
16011 if (mindif == 0)
16012 return best_row;
16013 }
16014 }
16015 }
16016 }
16017 else if (best_row && !row->continued_p)
16018 return best_row;
16019 ++row;
16020 }
16021 }
16022
16023
16024 /* Try to redisplay window W by reusing its existing display. W's
16025 current matrix must be up to date when this function is called,
16026 i.e. window_end_valid must not be nil.
16027
16028 Value is
16029
16030 1 if display has been updated
16031 0 if otherwise unsuccessful
16032 -1 if redisplay with same window start is known not to succeed
16033
16034 The following steps are performed:
16035
16036 1. Find the last row in the current matrix of W that is not
16037 affected by changes at the start of current_buffer. If no such row
16038 is found, give up.
16039
16040 2. Find the first row in W's current matrix that is not affected by
16041 changes at the end of current_buffer. Maybe there is no such row.
16042
16043 3. Display lines beginning with the row + 1 found in step 1 to the
16044 row found in step 2 or, if step 2 didn't find a row, to the end of
16045 the window.
16046
16047 4. If cursor is not known to appear on the window, give up.
16048
16049 5. If display stopped at the row found in step 2, scroll the
16050 display and current matrix as needed.
16051
16052 6. Maybe display some lines at the end of W, if we must. This can
16053 happen under various circumstances, like a partially visible line
16054 becoming fully visible, or because newly displayed lines are displayed
16055 in smaller font sizes.
16056
16057 7. Update W's window end information. */
16058
16059 static int
16060 try_window_id (struct window *w)
16061 {
16062 struct frame *f = XFRAME (w->frame);
16063 struct glyph_matrix *current_matrix = w->current_matrix;
16064 struct glyph_matrix *desired_matrix = w->desired_matrix;
16065 struct glyph_row *last_unchanged_at_beg_row;
16066 struct glyph_row *first_unchanged_at_end_row;
16067 struct glyph_row *row;
16068 struct glyph_row *bottom_row;
16069 int bottom_vpos;
16070 struct it it;
16071 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16072 int dvpos, dy;
16073 struct text_pos start_pos;
16074 struct run run;
16075 int first_unchanged_at_end_vpos = 0;
16076 struct glyph_row *last_text_row, *last_text_row_at_end;
16077 struct text_pos start;
16078 EMACS_INT first_changed_charpos, last_changed_charpos;
16079
16080 #if GLYPH_DEBUG
16081 if (inhibit_try_window_id)
16082 return 0;
16083 #endif
16084
16085 /* This is handy for debugging. */
16086 #if 0
16087 #define GIVE_UP(X) \
16088 do { \
16089 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16090 return 0; \
16091 } while (0)
16092 #else
16093 #define GIVE_UP(X) return 0
16094 #endif
16095
16096 SET_TEXT_POS_FROM_MARKER (start, w->start);
16097
16098 /* Don't use this for mini-windows because these can show
16099 messages and mini-buffers, and we don't handle that here. */
16100 if (MINI_WINDOW_P (w))
16101 GIVE_UP (1);
16102
16103 /* This flag is used to prevent redisplay optimizations. */
16104 if (windows_or_buffers_changed || cursor_type_changed)
16105 GIVE_UP (2);
16106
16107 /* Verify that narrowing has not changed.
16108 Also verify that we were not told to prevent redisplay optimizations.
16109 It would be nice to further
16110 reduce the number of cases where this prevents try_window_id. */
16111 if (current_buffer->clip_changed
16112 || current_buffer->prevent_redisplay_optimizations_p)
16113 GIVE_UP (3);
16114
16115 /* Window must either use window-based redisplay or be full width. */
16116 if (!FRAME_WINDOW_P (f)
16117 && (!FRAME_LINE_INS_DEL_OK (f)
16118 || !WINDOW_FULL_WIDTH_P (w)))
16119 GIVE_UP (4);
16120
16121 /* Give up if point is known NOT to appear in W. */
16122 if (PT < CHARPOS (start))
16123 GIVE_UP (5);
16124
16125 /* Another way to prevent redisplay optimizations. */
16126 if (XFASTINT (w->last_modified) == 0)
16127 GIVE_UP (6);
16128
16129 /* Verify that window is not hscrolled. */
16130 if (XFASTINT (w->hscroll) != 0)
16131 GIVE_UP (7);
16132
16133 /* Verify that display wasn't paused. */
16134 if (NILP (w->window_end_valid))
16135 GIVE_UP (8);
16136
16137 /* Can't use this if highlighting a region because a cursor movement
16138 will do more than just set the cursor. */
16139 if (!NILP (Vtransient_mark_mode)
16140 && !NILP (BVAR (current_buffer, mark_active)))
16141 GIVE_UP (9);
16142
16143 /* Likewise if highlighting trailing whitespace. */
16144 if (!NILP (Vshow_trailing_whitespace))
16145 GIVE_UP (11);
16146
16147 /* Likewise if showing a region. */
16148 if (!NILP (w->region_showing))
16149 GIVE_UP (10);
16150
16151 /* Can't use this if overlay arrow position and/or string have
16152 changed. */
16153 if (overlay_arrows_changed_p ())
16154 GIVE_UP (12);
16155
16156 /* When word-wrap is on, adding a space to the first word of a
16157 wrapped line can change the wrap position, altering the line
16158 above it. It might be worthwhile to handle this more
16159 intelligently, but for now just redisplay from scratch. */
16160 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16161 GIVE_UP (21);
16162
16163 /* Under bidi reordering, adding or deleting a character in the
16164 beginning of a paragraph, before the first strong directional
16165 character, can change the base direction of the paragraph (unless
16166 the buffer specifies a fixed paragraph direction), which will
16167 require to redisplay the whole paragraph. It might be worthwhile
16168 to find the paragraph limits and widen the range of redisplayed
16169 lines to that, but for now just give up this optimization and
16170 redisplay from scratch. */
16171 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16172 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16173 GIVE_UP (22);
16174
16175 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16176 only if buffer has really changed. The reason is that the gap is
16177 initially at Z for freshly visited files. The code below would
16178 set end_unchanged to 0 in that case. */
16179 if (MODIFF > SAVE_MODIFF
16180 /* This seems to happen sometimes after saving a buffer. */
16181 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16182 {
16183 if (GPT - BEG < BEG_UNCHANGED)
16184 BEG_UNCHANGED = GPT - BEG;
16185 if (Z - GPT < END_UNCHANGED)
16186 END_UNCHANGED = Z - GPT;
16187 }
16188
16189 /* The position of the first and last character that has been changed. */
16190 first_changed_charpos = BEG + BEG_UNCHANGED;
16191 last_changed_charpos = Z - END_UNCHANGED;
16192
16193 /* If window starts after a line end, and the last change is in
16194 front of that newline, then changes don't affect the display.
16195 This case happens with stealth-fontification. Note that although
16196 the display is unchanged, glyph positions in the matrix have to
16197 be adjusted, of course. */
16198 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16199 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16200 && ((last_changed_charpos < CHARPOS (start)
16201 && CHARPOS (start) == BEGV)
16202 || (last_changed_charpos < CHARPOS (start) - 1
16203 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16204 {
16205 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16206 struct glyph_row *r0;
16207
16208 /* Compute how many chars/bytes have been added to or removed
16209 from the buffer. */
16210 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16211 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16212 Z_delta = Z - Z_old;
16213 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16214
16215 /* Give up if PT is not in the window. Note that it already has
16216 been checked at the start of try_window_id that PT is not in
16217 front of the window start. */
16218 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16219 GIVE_UP (13);
16220
16221 /* If window start is unchanged, we can reuse the whole matrix
16222 as is, after adjusting glyph positions. No need to compute
16223 the window end again, since its offset from Z hasn't changed. */
16224 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16225 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16226 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16227 /* PT must not be in a partially visible line. */
16228 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16229 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16230 {
16231 /* Adjust positions in the glyph matrix. */
16232 if (Z_delta || Z_delta_bytes)
16233 {
16234 struct glyph_row *r1
16235 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16236 increment_matrix_positions (w->current_matrix,
16237 MATRIX_ROW_VPOS (r0, current_matrix),
16238 MATRIX_ROW_VPOS (r1, current_matrix),
16239 Z_delta, Z_delta_bytes);
16240 }
16241
16242 /* Set the cursor. */
16243 row = row_containing_pos (w, PT, r0, NULL, 0);
16244 if (row)
16245 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16246 else
16247 abort ();
16248 return 1;
16249 }
16250 }
16251
16252 /* Handle the case that changes are all below what is displayed in
16253 the window, and that PT is in the window. This shortcut cannot
16254 be taken if ZV is visible in the window, and text has been added
16255 there that is visible in the window. */
16256 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16257 /* ZV is not visible in the window, or there are no
16258 changes at ZV, actually. */
16259 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16260 || first_changed_charpos == last_changed_charpos))
16261 {
16262 struct glyph_row *r0;
16263
16264 /* Give up if PT is not in the window. Note that it already has
16265 been checked at the start of try_window_id that PT is not in
16266 front of the window start. */
16267 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16268 GIVE_UP (14);
16269
16270 /* If window start is unchanged, we can reuse the whole matrix
16271 as is, without changing glyph positions since no text has
16272 been added/removed in front of the window end. */
16273 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16274 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16275 /* PT must not be in a partially visible line. */
16276 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16277 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16278 {
16279 /* We have to compute the window end anew since text
16280 could have been added/removed after it. */
16281 w->window_end_pos
16282 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16283 w->window_end_bytepos
16284 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16285
16286 /* Set the cursor. */
16287 row = row_containing_pos (w, PT, r0, NULL, 0);
16288 if (row)
16289 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16290 else
16291 abort ();
16292 return 2;
16293 }
16294 }
16295
16296 /* Give up if window start is in the changed area.
16297
16298 The condition used to read
16299
16300 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16301
16302 but why that was tested escapes me at the moment. */
16303 if (CHARPOS (start) >= first_changed_charpos
16304 && CHARPOS (start) <= last_changed_charpos)
16305 GIVE_UP (15);
16306
16307 /* Check that window start agrees with the start of the first glyph
16308 row in its current matrix. Check this after we know the window
16309 start is not in changed text, otherwise positions would not be
16310 comparable. */
16311 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16312 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16313 GIVE_UP (16);
16314
16315 /* Give up if the window ends in strings. Overlay strings
16316 at the end are difficult to handle, so don't try. */
16317 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16318 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16319 GIVE_UP (20);
16320
16321 /* Compute the position at which we have to start displaying new
16322 lines. Some of the lines at the top of the window might be
16323 reusable because they are not displaying changed text. Find the
16324 last row in W's current matrix not affected by changes at the
16325 start of current_buffer. Value is null if changes start in the
16326 first line of window. */
16327 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16328 if (last_unchanged_at_beg_row)
16329 {
16330 /* Avoid starting to display in the moddle of a character, a TAB
16331 for instance. This is easier than to set up the iterator
16332 exactly, and it's not a frequent case, so the additional
16333 effort wouldn't really pay off. */
16334 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16335 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16336 && last_unchanged_at_beg_row > w->current_matrix->rows)
16337 --last_unchanged_at_beg_row;
16338
16339 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16340 GIVE_UP (17);
16341
16342 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16343 GIVE_UP (18);
16344 start_pos = it.current.pos;
16345
16346 /* Start displaying new lines in the desired matrix at the same
16347 vpos we would use in the current matrix, i.e. below
16348 last_unchanged_at_beg_row. */
16349 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16350 current_matrix);
16351 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16352 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16353
16354 xassert (it.hpos == 0 && it.current_x == 0);
16355 }
16356 else
16357 {
16358 /* There are no reusable lines at the start of the window.
16359 Start displaying in the first text line. */
16360 start_display (&it, w, start);
16361 it.vpos = it.first_vpos;
16362 start_pos = it.current.pos;
16363 }
16364
16365 /* Find the first row that is not affected by changes at the end of
16366 the buffer. Value will be null if there is no unchanged row, in
16367 which case we must redisplay to the end of the window. delta
16368 will be set to the value by which buffer positions beginning with
16369 first_unchanged_at_end_row have to be adjusted due to text
16370 changes. */
16371 first_unchanged_at_end_row
16372 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16373 IF_DEBUG (debug_delta = delta);
16374 IF_DEBUG (debug_delta_bytes = delta_bytes);
16375
16376 /* Set stop_pos to the buffer position up to which we will have to
16377 display new lines. If first_unchanged_at_end_row != NULL, this
16378 is the buffer position of the start of the line displayed in that
16379 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16380 that we don't stop at a buffer position. */
16381 stop_pos = 0;
16382 if (first_unchanged_at_end_row)
16383 {
16384 xassert (last_unchanged_at_beg_row == NULL
16385 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16386
16387 /* If this is a continuation line, move forward to the next one
16388 that isn't. Changes in lines above affect this line.
16389 Caution: this may move first_unchanged_at_end_row to a row
16390 not displaying text. */
16391 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16392 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16393 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16394 < it.last_visible_y))
16395 ++first_unchanged_at_end_row;
16396
16397 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16398 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16399 >= it.last_visible_y))
16400 first_unchanged_at_end_row = NULL;
16401 else
16402 {
16403 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16404 + delta);
16405 first_unchanged_at_end_vpos
16406 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16407 xassert (stop_pos >= Z - END_UNCHANGED);
16408 }
16409 }
16410 else if (last_unchanged_at_beg_row == NULL)
16411 GIVE_UP (19);
16412
16413
16414 #if GLYPH_DEBUG
16415
16416 /* Either there is no unchanged row at the end, or the one we have
16417 now displays text. This is a necessary condition for the window
16418 end pos calculation at the end of this function. */
16419 xassert (first_unchanged_at_end_row == NULL
16420 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16421
16422 debug_last_unchanged_at_beg_vpos
16423 = (last_unchanged_at_beg_row
16424 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16425 : -1);
16426 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16427
16428 #endif /* GLYPH_DEBUG != 0 */
16429
16430
16431 /* Display new lines. Set last_text_row to the last new line
16432 displayed which has text on it, i.e. might end up as being the
16433 line where the window_end_vpos is. */
16434 w->cursor.vpos = -1;
16435 last_text_row = NULL;
16436 overlay_arrow_seen = 0;
16437 while (it.current_y < it.last_visible_y
16438 && !fonts_changed_p
16439 && (first_unchanged_at_end_row == NULL
16440 || IT_CHARPOS (it) < stop_pos))
16441 {
16442 if (display_line (&it))
16443 last_text_row = it.glyph_row - 1;
16444 }
16445
16446 if (fonts_changed_p)
16447 return -1;
16448
16449
16450 /* Compute differences in buffer positions, y-positions etc. for
16451 lines reused at the bottom of the window. Compute what we can
16452 scroll. */
16453 if (first_unchanged_at_end_row
16454 /* No lines reused because we displayed everything up to the
16455 bottom of the window. */
16456 && it.current_y < it.last_visible_y)
16457 {
16458 dvpos = (it.vpos
16459 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16460 current_matrix));
16461 dy = it.current_y - first_unchanged_at_end_row->y;
16462 run.current_y = first_unchanged_at_end_row->y;
16463 run.desired_y = run.current_y + dy;
16464 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16465 }
16466 else
16467 {
16468 delta = delta_bytes = dvpos = dy
16469 = run.current_y = run.desired_y = run.height = 0;
16470 first_unchanged_at_end_row = NULL;
16471 }
16472 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16473
16474
16475 /* Find the cursor if not already found. We have to decide whether
16476 PT will appear on this window (it sometimes doesn't, but this is
16477 not a very frequent case.) This decision has to be made before
16478 the current matrix is altered. A value of cursor.vpos < 0 means
16479 that PT is either in one of the lines beginning at
16480 first_unchanged_at_end_row or below the window. Don't care for
16481 lines that might be displayed later at the window end; as
16482 mentioned, this is not a frequent case. */
16483 if (w->cursor.vpos < 0)
16484 {
16485 /* Cursor in unchanged rows at the top? */
16486 if (PT < CHARPOS (start_pos)
16487 && last_unchanged_at_beg_row)
16488 {
16489 row = row_containing_pos (w, PT,
16490 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16491 last_unchanged_at_beg_row + 1, 0);
16492 if (row)
16493 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16494 }
16495
16496 /* Start from first_unchanged_at_end_row looking for PT. */
16497 else if (first_unchanged_at_end_row)
16498 {
16499 row = row_containing_pos (w, PT - delta,
16500 first_unchanged_at_end_row, NULL, 0);
16501 if (row)
16502 set_cursor_from_row (w, row, w->current_matrix, delta,
16503 delta_bytes, dy, dvpos);
16504 }
16505
16506 /* Give up if cursor was not found. */
16507 if (w->cursor.vpos < 0)
16508 {
16509 clear_glyph_matrix (w->desired_matrix);
16510 return -1;
16511 }
16512 }
16513
16514 /* Don't let the cursor end in the scroll margins. */
16515 {
16516 int this_scroll_margin, cursor_height;
16517
16518 this_scroll_margin = max (0, scroll_margin);
16519 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16520 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16521 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16522
16523 if ((w->cursor.y < this_scroll_margin
16524 && CHARPOS (start) > BEGV)
16525 /* Old redisplay didn't take scroll margin into account at the bottom,
16526 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16527 || (w->cursor.y + (make_cursor_line_fully_visible_p
16528 ? cursor_height + this_scroll_margin
16529 : 1)) > it.last_visible_y)
16530 {
16531 w->cursor.vpos = -1;
16532 clear_glyph_matrix (w->desired_matrix);
16533 return -1;
16534 }
16535 }
16536
16537 /* Scroll the display. Do it before changing the current matrix so
16538 that xterm.c doesn't get confused about where the cursor glyph is
16539 found. */
16540 if (dy && run.height)
16541 {
16542 update_begin (f);
16543
16544 if (FRAME_WINDOW_P (f))
16545 {
16546 FRAME_RIF (f)->update_window_begin_hook (w);
16547 FRAME_RIF (f)->clear_window_mouse_face (w);
16548 FRAME_RIF (f)->scroll_run_hook (w, &run);
16549 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16550 }
16551 else
16552 {
16553 /* Terminal frame. In this case, dvpos gives the number of
16554 lines to scroll by; dvpos < 0 means scroll up. */
16555 int from_vpos
16556 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16557 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16558 int end = (WINDOW_TOP_EDGE_LINE (w)
16559 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16560 + window_internal_height (w));
16561
16562 #if defined (HAVE_GPM) || defined (MSDOS)
16563 x_clear_window_mouse_face (w);
16564 #endif
16565 /* Perform the operation on the screen. */
16566 if (dvpos > 0)
16567 {
16568 /* Scroll last_unchanged_at_beg_row to the end of the
16569 window down dvpos lines. */
16570 set_terminal_window (f, end);
16571
16572 /* On dumb terminals delete dvpos lines at the end
16573 before inserting dvpos empty lines. */
16574 if (!FRAME_SCROLL_REGION_OK (f))
16575 ins_del_lines (f, end - dvpos, -dvpos);
16576
16577 /* Insert dvpos empty lines in front of
16578 last_unchanged_at_beg_row. */
16579 ins_del_lines (f, from, dvpos);
16580 }
16581 else if (dvpos < 0)
16582 {
16583 /* Scroll up last_unchanged_at_beg_vpos to the end of
16584 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16585 set_terminal_window (f, end);
16586
16587 /* Delete dvpos lines in front of
16588 last_unchanged_at_beg_vpos. ins_del_lines will set
16589 the cursor to the given vpos and emit |dvpos| delete
16590 line sequences. */
16591 ins_del_lines (f, from + dvpos, dvpos);
16592
16593 /* On a dumb terminal insert dvpos empty lines at the
16594 end. */
16595 if (!FRAME_SCROLL_REGION_OK (f))
16596 ins_del_lines (f, end + dvpos, -dvpos);
16597 }
16598
16599 set_terminal_window (f, 0);
16600 }
16601
16602 update_end (f);
16603 }
16604
16605 /* Shift reused rows of the current matrix to the right position.
16606 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16607 text. */
16608 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16609 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16610 if (dvpos < 0)
16611 {
16612 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16613 bottom_vpos, dvpos);
16614 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16615 bottom_vpos, 0);
16616 }
16617 else if (dvpos > 0)
16618 {
16619 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16620 bottom_vpos, dvpos);
16621 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16622 first_unchanged_at_end_vpos + dvpos, 0);
16623 }
16624
16625 /* For frame-based redisplay, make sure that current frame and window
16626 matrix are in sync with respect to glyph memory. */
16627 if (!FRAME_WINDOW_P (f))
16628 sync_frame_with_window_matrix_rows (w);
16629
16630 /* Adjust buffer positions in reused rows. */
16631 if (delta || delta_bytes)
16632 increment_matrix_positions (current_matrix,
16633 first_unchanged_at_end_vpos + dvpos,
16634 bottom_vpos, delta, delta_bytes);
16635
16636 /* Adjust Y positions. */
16637 if (dy)
16638 shift_glyph_matrix (w, current_matrix,
16639 first_unchanged_at_end_vpos + dvpos,
16640 bottom_vpos, dy);
16641
16642 if (first_unchanged_at_end_row)
16643 {
16644 first_unchanged_at_end_row += dvpos;
16645 if (first_unchanged_at_end_row->y >= it.last_visible_y
16646 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16647 first_unchanged_at_end_row = NULL;
16648 }
16649
16650 /* If scrolling up, there may be some lines to display at the end of
16651 the window. */
16652 last_text_row_at_end = NULL;
16653 if (dy < 0)
16654 {
16655 /* Scrolling up can leave for example a partially visible line
16656 at the end of the window to be redisplayed. */
16657 /* Set last_row to the glyph row in the current matrix where the
16658 window end line is found. It has been moved up or down in
16659 the matrix by dvpos. */
16660 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16661 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16662
16663 /* If last_row is the window end line, it should display text. */
16664 xassert (last_row->displays_text_p);
16665
16666 /* If window end line was partially visible before, begin
16667 displaying at that line. Otherwise begin displaying with the
16668 line following it. */
16669 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16670 {
16671 init_to_row_start (&it, w, last_row);
16672 it.vpos = last_vpos;
16673 it.current_y = last_row->y;
16674 }
16675 else
16676 {
16677 init_to_row_end (&it, w, last_row);
16678 it.vpos = 1 + last_vpos;
16679 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16680 ++last_row;
16681 }
16682
16683 /* We may start in a continuation line. If so, we have to
16684 get the right continuation_lines_width and current_x. */
16685 it.continuation_lines_width = last_row->continuation_lines_width;
16686 it.hpos = it.current_x = 0;
16687
16688 /* Display the rest of the lines at the window end. */
16689 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16690 while (it.current_y < it.last_visible_y
16691 && !fonts_changed_p)
16692 {
16693 /* Is it always sure that the display agrees with lines in
16694 the current matrix? I don't think so, so we mark rows
16695 displayed invalid in the current matrix by setting their
16696 enabled_p flag to zero. */
16697 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16698 if (display_line (&it))
16699 last_text_row_at_end = it.glyph_row - 1;
16700 }
16701 }
16702
16703 /* Update window_end_pos and window_end_vpos. */
16704 if (first_unchanged_at_end_row
16705 && !last_text_row_at_end)
16706 {
16707 /* Window end line if one of the preserved rows from the current
16708 matrix. Set row to the last row displaying text in current
16709 matrix starting at first_unchanged_at_end_row, after
16710 scrolling. */
16711 xassert (first_unchanged_at_end_row->displays_text_p);
16712 row = find_last_row_displaying_text (w->current_matrix, &it,
16713 first_unchanged_at_end_row);
16714 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16715
16716 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16717 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16718 w->window_end_vpos
16719 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16720 xassert (w->window_end_bytepos >= 0);
16721 IF_DEBUG (debug_method_add (w, "A"));
16722 }
16723 else if (last_text_row_at_end)
16724 {
16725 w->window_end_pos
16726 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16727 w->window_end_bytepos
16728 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16729 w->window_end_vpos
16730 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16731 xassert (w->window_end_bytepos >= 0);
16732 IF_DEBUG (debug_method_add (w, "B"));
16733 }
16734 else if (last_text_row)
16735 {
16736 /* We have displayed either to the end of the window or at the
16737 end of the window, i.e. the last row with text is to be found
16738 in the desired matrix. */
16739 w->window_end_pos
16740 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16741 w->window_end_bytepos
16742 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16743 w->window_end_vpos
16744 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16745 xassert (w->window_end_bytepos >= 0);
16746 }
16747 else if (first_unchanged_at_end_row == NULL
16748 && last_text_row == NULL
16749 && last_text_row_at_end == NULL)
16750 {
16751 /* Displayed to end of window, but no line containing text was
16752 displayed. Lines were deleted at the end of the window. */
16753 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16754 int vpos = XFASTINT (w->window_end_vpos);
16755 struct glyph_row *current_row = current_matrix->rows + vpos;
16756 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16757
16758 for (row = NULL;
16759 row == NULL && vpos >= first_vpos;
16760 --vpos, --current_row, --desired_row)
16761 {
16762 if (desired_row->enabled_p)
16763 {
16764 if (desired_row->displays_text_p)
16765 row = desired_row;
16766 }
16767 else if (current_row->displays_text_p)
16768 row = current_row;
16769 }
16770
16771 xassert (row != NULL);
16772 w->window_end_vpos = make_number (vpos + 1);
16773 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16774 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16775 xassert (w->window_end_bytepos >= 0);
16776 IF_DEBUG (debug_method_add (w, "C"));
16777 }
16778 else
16779 abort ();
16780
16781 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16782 debug_end_vpos = XFASTINT (w->window_end_vpos));
16783
16784 /* Record that display has not been completed. */
16785 w->window_end_valid = Qnil;
16786 w->desired_matrix->no_scrolling_p = 1;
16787 return 3;
16788
16789 #undef GIVE_UP
16790 }
16791
16792
16793 \f
16794 /***********************************************************************
16795 More debugging support
16796 ***********************************************************************/
16797
16798 #if GLYPH_DEBUG
16799
16800 void dump_glyph_row (struct glyph_row *, int, int);
16801 void dump_glyph_matrix (struct glyph_matrix *, int);
16802 void dump_glyph (struct glyph_row *, struct glyph *, int);
16803
16804
16805 /* Dump the contents of glyph matrix MATRIX on stderr.
16806
16807 GLYPHS 0 means don't show glyph contents.
16808 GLYPHS 1 means show glyphs in short form
16809 GLYPHS > 1 means show glyphs in long form. */
16810
16811 void
16812 dump_glyph_matrix (matrix, glyphs)
16813 struct glyph_matrix *matrix;
16814 int glyphs;
16815 {
16816 int i;
16817 for (i = 0; i < matrix->nrows; ++i)
16818 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16819 }
16820
16821
16822 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16823 the glyph row and area where the glyph comes from. */
16824
16825 void
16826 dump_glyph (row, glyph, area)
16827 struct glyph_row *row;
16828 struct glyph *glyph;
16829 int area;
16830 {
16831 if (glyph->type == CHAR_GLYPH)
16832 {
16833 fprintf (stderr,
16834 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16835 glyph - row->glyphs[TEXT_AREA],
16836 'C',
16837 glyph->charpos,
16838 (BUFFERP (glyph->object)
16839 ? 'B'
16840 : (STRINGP (glyph->object)
16841 ? 'S'
16842 : '-')),
16843 glyph->pixel_width,
16844 glyph->u.ch,
16845 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16846 ? glyph->u.ch
16847 : '.'),
16848 glyph->face_id,
16849 glyph->left_box_line_p,
16850 glyph->right_box_line_p);
16851 }
16852 else if (glyph->type == STRETCH_GLYPH)
16853 {
16854 fprintf (stderr,
16855 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16856 glyph - row->glyphs[TEXT_AREA],
16857 'S',
16858 glyph->charpos,
16859 (BUFFERP (glyph->object)
16860 ? 'B'
16861 : (STRINGP (glyph->object)
16862 ? 'S'
16863 : '-')),
16864 glyph->pixel_width,
16865 0,
16866 '.',
16867 glyph->face_id,
16868 glyph->left_box_line_p,
16869 glyph->right_box_line_p);
16870 }
16871 else if (glyph->type == IMAGE_GLYPH)
16872 {
16873 fprintf (stderr,
16874 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16875 glyph - row->glyphs[TEXT_AREA],
16876 'I',
16877 glyph->charpos,
16878 (BUFFERP (glyph->object)
16879 ? 'B'
16880 : (STRINGP (glyph->object)
16881 ? 'S'
16882 : '-')),
16883 glyph->pixel_width,
16884 glyph->u.img_id,
16885 '.',
16886 glyph->face_id,
16887 glyph->left_box_line_p,
16888 glyph->right_box_line_p);
16889 }
16890 else if (glyph->type == COMPOSITE_GLYPH)
16891 {
16892 fprintf (stderr,
16893 " %5d %4c %6d %c %3d 0x%05x",
16894 glyph - row->glyphs[TEXT_AREA],
16895 '+',
16896 glyph->charpos,
16897 (BUFFERP (glyph->object)
16898 ? 'B'
16899 : (STRINGP (glyph->object)
16900 ? 'S'
16901 : '-')),
16902 glyph->pixel_width,
16903 glyph->u.cmp.id);
16904 if (glyph->u.cmp.automatic)
16905 fprintf (stderr,
16906 "[%d-%d]",
16907 glyph->slice.cmp.from, glyph->slice.cmp.to);
16908 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16909 glyph->face_id,
16910 glyph->left_box_line_p,
16911 glyph->right_box_line_p);
16912 }
16913 }
16914
16915
16916 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16917 GLYPHS 0 means don't show glyph contents.
16918 GLYPHS 1 means show glyphs in short form
16919 GLYPHS > 1 means show glyphs in long form. */
16920
16921 void
16922 dump_glyph_row (row, vpos, glyphs)
16923 struct glyph_row *row;
16924 int vpos, glyphs;
16925 {
16926 if (glyphs != 1)
16927 {
16928 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16929 fprintf (stderr, "======================================================================\n");
16930
16931 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16932 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16933 vpos,
16934 MATRIX_ROW_START_CHARPOS (row),
16935 MATRIX_ROW_END_CHARPOS (row),
16936 row->used[TEXT_AREA],
16937 row->contains_overlapping_glyphs_p,
16938 row->enabled_p,
16939 row->truncated_on_left_p,
16940 row->truncated_on_right_p,
16941 row->continued_p,
16942 MATRIX_ROW_CONTINUATION_LINE_P (row),
16943 row->displays_text_p,
16944 row->ends_at_zv_p,
16945 row->fill_line_p,
16946 row->ends_in_middle_of_char_p,
16947 row->starts_in_middle_of_char_p,
16948 row->mouse_face_p,
16949 row->x,
16950 row->y,
16951 row->pixel_width,
16952 row->height,
16953 row->visible_height,
16954 row->ascent,
16955 row->phys_ascent);
16956 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16957 row->end.overlay_string_index,
16958 row->continuation_lines_width);
16959 fprintf (stderr, "%9d %5d\n",
16960 CHARPOS (row->start.string_pos),
16961 CHARPOS (row->end.string_pos));
16962 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16963 row->end.dpvec_index);
16964 }
16965
16966 if (glyphs > 1)
16967 {
16968 int area;
16969
16970 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16971 {
16972 struct glyph *glyph = row->glyphs[area];
16973 struct glyph *glyph_end = glyph + row->used[area];
16974
16975 /* Glyph for a line end in text. */
16976 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16977 ++glyph_end;
16978
16979 if (glyph < glyph_end)
16980 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16981
16982 for (; glyph < glyph_end; ++glyph)
16983 dump_glyph (row, glyph, area);
16984 }
16985 }
16986 else if (glyphs == 1)
16987 {
16988 int area;
16989
16990 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16991 {
16992 char *s = (char *) alloca (row->used[area] + 1);
16993 int i;
16994
16995 for (i = 0; i < row->used[area]; ++i)
16996 {
16997 struct glyph *glyph = row->glyphs[area] + i;
16998 if (glyph->type == CHAR_GLYPH
16999 && glyph->u.ch < 0x80
17000 && glyph->u.ch >= ' ')
17001 s[i] = glyph->u.ch;
17002 else
17003 s[i] = '.';
17004 }
17005
17006 s[i] = '\0';
17007 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17008 }
17009 }
17010 }
17011
17012
17013 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17014 Sdump_glyph_matrix, 0, 1, "p",
17015 doc: /* Dump the current matrix of the selected window to stderr.
17016 Shows contents of glyph row structures. With non-nil
17017 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17018 glyphs in short form, otherwise show glyphs in long form. */)
17019 (Lisp_Object glyphs)
17020 {
17021 struct window *w = XWINDOW (selected_window);
17022 struct buffer *buffer = XBUFFER (w->buffer);
17023
17024 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
17025 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17026 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17027 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17028 fprintf (stderr, "=============================================\n");
17029 dump_glyph_matrix (w->current_matrix,
17030 NILP (glyphs) ? 0 : XINT (glyphs));
17031 return Qnil;
17032 }
17033
17034
17035 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17036 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17037 (void)
17038 {
17039 struct frame *f = XFRAME (selected_frame);
17040 dump_glyph_matrix (f->current_matrix, 1);
17041 return Qnil;
17042 }
17043
17044
17045 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17046 doc: /* Dump glyph row ROW to stderr.
17047 GLYPH 0 means don't dump glyphs.
17048 GLYPH 1 means dump glyphs in short form.
17049 GLYPH > 1 or omitted means dump glyphs in long form. */)
17050 (Lisp_Object row, Lisp_Object glyphs)
17051 {
17052 struct glyph_matrix *matrix;
17053 int vpos;
17054
17055 CHECK_NUMBER (row);
17056 matrix = XWINDOW (selected_window)->current_matrix;
17057 vpos = XINT (row);
17058 if (vpos >= 0 && vpos < matrix->nrows)
17059 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17060 vpos,
17061 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17062 return Qnil;
17063 }
17064
17065
17066 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17067 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17068 GLYPH 0 means don't dump glyphs.
17069 GLYPH 1 means dump glyphs in short form.
17070 GLYPH > 1 or omitted means dump glyphs in long form. */)
17071 (Lisp_Object row, Lisp_Object glyphs)
17072 {
17073 struct frame *sf = SELECTED_FRAME ();
17074 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17075 int vpos;
17076
17077 CHECK_NUMBER (row);
17078 vpos = XINT (row);
17079 if (vpos >= 0 && vpos < m->nrows)
17080 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17081 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17082 return Qnil;
17083 }
17084
17085
17086 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17087 doc: /* Toggle tracing of redisplay.
17088 With ARG, turn tracing on if and only if ARG is positive. */)
17089 (Lisp_Object arg)
17090 {
17091 if (NILP (arg))
17092 trace_redisplay_p = !trace_redisplay_p;
17093 else
17094 {
17095 arg = Fprefix_numeric_value (arg);
17096 trace_redisplay_p = XINT (arg) > 0;
17097 }
17098
17099 return Qnil;
17100 }
17101
17102
17103 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17104 doc: /* Like `format', but print result to stderr.
17105 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17106 (size_t nargs, Lisp_Object *args)
17107 {
17108 Lisp_Object s = Fformat (nargs, args);
17109 fprintf (stderr, "%s", SDATA (s));
17110 return Qnil;
17111 }
17112
17113 #endif /* GLYPH_DEBUG */
17114
17115
17116 \f
17117 /***********************************************************************
17118 Building Desired Matrix Rows
17119 ***********************************************************************/
17120
17121 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17122 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17123
17124 static struct glyph_row *
17125 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17126 {
17127 struct frame *f = XFRAME (WINDOW_FRAME (w));
17128 struct buffer *buffer = XBUFFER (w->buffer);
17129 struct buffer *old = current_buffer;
17130 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17131 int arrow_len = SCHARS (overlay_arrow_string);
17132 const unsigned char *arrow_end = arrow_string + arrow_len;
17133 const unsigned char *p;
17134 struct it it;
17135 int multibyte_p;
17136 int n_glyphs_before;
17137
17138 set_buffer_temp (buffer);
17139 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17140 it.glyph_row->used[TEXT_AREA] = 0;
17141 SET_TEXT_POS (it.position, 0, 0);
17142
17143 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17144 p = arrow_string;
17145 while (p < arrow_end)
17146 {
17147 Lisp_Object face, ilisp;
17148
17149 /* Get the next character. */
17150 if (multibyte_p)
17151 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17152 else
17153 {
17154 it.c = it.char_to_display = *p, it.len = 1;
17155 if (! ASCII_CHAR_P (it.c))
17156 it.char_to_display = BYTE8_TO_CHAR (it.c);
17157 }
17158 p += it.len;
17159
17160 /* Get its face. */
17161 ilisp = make_number (p - arrow_string);
17162 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17163 it.face_id = compute_char_face (f, it.char_to_display, face);
17164
17165 /* Compute its width, get its glyphs. */
17166 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17167 SET_TEXT_POS (it.position, -1, -1);
17168 PRODUCE_GLYPHS (&it);
17169
17170 /* If this character doesn't fit any more in the line, we have
17171 to remove some glyphs. */
17172 if (it.current_x > it.last_visible_x)
17173 {
17174 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17175 break;
17176 }
17177 }
17178
17179 set_buffer_temp (old);
17180 return it.glyph_row;
17181 }
17182
17183
17184 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17185 glyphs are only inserted for terminal frames since we can't really
17186 win with truncation glyphs when partially visible glyphs are
17187 involved. Which glyphs to insert is determined by
17188 produce_special_glyphs. */
17189
17190 static void
17191 insert_left_trunc_glyphs (struct it *it)
17192 {
17193 struct it truncate_it;
17194 struct glyph *from, *end, *to, *toend;
17195
17196 xassert (!FRAME_WINDOW_P (it->f));
17197
17198 /* Get the truncation glyphs. */
17199 truncate_it = *it;
17200 truncate_it.current_x = 0;
17201 truncate_it.face_id = DEFAULT_FACE_ID;
17202 truncate_it.glyph_row = &scratch_glyph_row;
17203 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17204 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17205 truncate_it.object = make_number (0);
17206 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17207
17208 /* Overwrite glyphs from IT with truncation glyphs. */
17209 if (!it->glyph_row->reversed_p)
17210 {
17211 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17212 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17213 to = it->glyph_row->glyphs[TEXT_AREA];
17214 toend = to + it->glyph_row->used[TEXT_AREA];
17215
17216 while (from < end)
17217 *to++ = *from++;
17218
17219 /* There may be padding glyphs left over. Overwrite them too. */
17220 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17221 {
17222 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17223 while (from < end)
17224 *to++ = *from++;
17225 }
17226
17227 if (to > toend)
17228 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17229 }
17230 else
17231 {
17232 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17233 that back to front. */
17234 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17235 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17236 toend = it->glyph_row->glyphs[TEXT_AREA];
17237 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17238
17239 while (from >= end && to >= toend)
17240 *to-- = *from--;
17241 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17242 {
17243 from =
17244 truncate_it.glyph_row->glyphs[TEXT_AREA]
17245 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17246 while (from >= end && to >= toend)
17247 *to-- = *from--;
17248 }
17249 if (from >= end)
17250 {
17251 /* Need to free some room before prepending additional
17252 glyphs. */
17253 int move_by = from - end + 1;
17254 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17255 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17256
17257 for ( ; g >= g0; g--)
17258 g[move_by] = *g;
17259 while (from >= end)
17260 *to-- = *from--;
17261 it->glyph_row->used[TEXT_AREA] += move_by;
17262 }
17263 }
17264 }
17265
17266
17267 /* Compute the pixel height and width of IT->glyph_row.
17268
17269 Most of the time, ascent and height of a display line will be equal
17270 to the max_ascent and max_height values of the display iterator
17271 structure. This is not the case if
17272
17273 1. We hit ZV without displaying anything. In this case, max_ascent
17274 and max_height will be zero.
17275
17276 2. We have some glyphs that don't contribute to the line height.
17277 (The glyph row flag contributes_to_line_height_p is for future
17278 pixmap extensions).
17279
17280 The first case is easily covered by using default values because in
17281 these cases, the line height does not really matter, except that it
17282 must not be zero. */
17283
17284 static void
17285 compute_line_metrics (struct it *it)
17286 {
17287 struct glyph_row *row = it->glyph_row;
17288
17289 if (FRAME_WINDOW_P (it->f))
17290 {
17291 int i, min_y, max_y;
17292
17293 /* The line may consist of one space only, that was added to
17294 place the cursor on it. If so, the row's height hasn't been
17295 computed yet. */
17296 if (row->height == 0)
17297 {
17298 if (it->max_ascent + it->max_descent == 0)
17299 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17300 row->ascent = it->max_ascent;
17301 row->height = it->max_ascent + it->max_descent;
17302 row->phys_ascent = it->max_phys_ascent;
17303 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17304 row->extra_line_spacing = it->max_extra_line_spacing;
17305 }
17306
17307 /* Compute the width of this line. */
17308 row->pixel_width = row->x;
17309 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17310 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17311
17312 xassert (row->pixel_width >= 0);
17313 xassert (row->ascent >= 0 && row->height > 0);
17314
17315 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17316 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17317
17318 /* If first line's physical ascent is larger than its logical
17319 ascent, use the physical ascent, and make the row taller.
17320 This makes accented characters fully visible. */
17321 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17322 && row->phys_ascent > row->ascent)
17323 {
17324 row->height += row->phys_ascent - row->ascent;
17325 row->ascent = row->phys_ascent;
17326 }
17327
17328 /* Compute how much of the line is visible. */
17329 row->visible_height = row->height;
17330
17331 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17332 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17333
17334 if (row->y < min_y)
17335 row->visible_height -= min_y - row->y;
17336 if (row->y + row->height > max_y)
17337 row->visible_height -= row->y + row->height - max_y;
17338 }
17339 else
17340 {
17341 row->pixel_width = row->used[TEXT_AREA];
17342 if (row->continued_p)
17343 row->pixel_width -= it->continuation_pixel_width;
17344 else if (row->truncated_on_right_p)
17345 row->pixel_width -= it->truncation_pixel_width;
17346 row->ascent = row->phys_ascent = 0;
17347 row->height = row->phys_height = row->visible_height = 1;
17348 row->extra_line_spacing = 0;
17349 }
17350
17351 /* Compute a hash code for this row. */
17352 {
17353 int area, i;
17354 row->hash = 0;
17355 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17356 for (i = 0; i < row->used[area]; ++i)
17357 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17358 + row->glyphs[area][i].u.val
17359 + row->glyphs[area][i].face_id
17360 + row->glyphs[area][i].padding_p
17361 + (row->glyphs[area][i].type << 2));
17362 }
17363
17364 it->max_ascent = it->max_descent = 0;
17365 it->max_phys_ascent = it->max_phys_descent = 0;
17366 }
17367
17368
17369 /* Append one space to the glyph row of iterator IT if doing a
17370 window-based redisplay. The space has the same face as
17371 IT->face_id. Value is non-zero if a space was added.
17372
17373 This function is called to make sure that there is always one glyph
17374 at the end of a glyph row that the cursor can be set on under
17375 window-systems. (If there weren't such a glyph we would not know
17376 how wide and tall a box cursor should be displayed).
17377
17378 At the same time this space let's a nicely handle clearing to the
17379 end of the line if the row ends in italic text. */
17380
17381 static int
17382 append_space_for_newline (struct it *it, int default_face_p)
17383 {
17384 if (FRAME_WINDOW_P (it->f))
17385 {
17386 int n = it->glyph_row->used[TEXT_AREA];
17387
17388 if (it->glyph_row->glyphs[TEXT_AREA] + n
17389 < it->glyph_row->glyphs[1 + TEXT_AREA])
17390 {
17391 /* Save some values that must not be changed.
17392 Must save IT->c and IT->len because otherwise
17393 ITERATOR_AT_END_P wouldn't work anymore after
17394 append_space_for_newline has been called. */
17395 enum display_element_type saved_what = it->what;
17396 int saved_c = it->c, saved_len = it->len;
17397 int saved_char_to_display = it->char_to_display;
17398 int saved_x = it->current_x;
17399 int saved_face_id = it->face_id;
17400 struct text_pos saved_pos;
17401 Lisp_Object saved_object;
17402 struct face *face;
17403
17404 saved_object = it->object;
17405 saved_pos = it->position;
17406
17407 it->what = IT_CHARACTER;
17408 memset (&it->position, 0, sizeof it->position);
17409 it->object = make_number (0);
17410 it->c = it->char_to_display = ' ';
17411 it->len = 1;
17412
17413 if (default_face_p)
17414 it->face_id = DEFAULT_FACE_ID;
17415 else if (it->face_before_selective_p)
17416 it->face_id = it->saved_face_id;
17417 face = FACE_FROM_ID (it->f, it->face_id);
17418 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17419
17420 PRODUCE_GLYPHS (it);
17421
17422 it->override_ascent = -1;
17423 it->constrain_row_ascent_descent_p = 0;
17424 it->current_x = saved_x;
17425 it->object = saved_object;
17426 it->position = saved_pos;
17427 it->what = saved_what;
17428 it->face_id = saved_face_id;
17429 it->len = saved_len;
17430 it->c = saved_c;
17431 it->char_to_display = saved_char_to_display;
17432 return 1;
17433 }
17434 }
17435
17436 return 0;
17437 }
17438
17439
17440 /* Extend the face of the last glyph in the text area of IT->glyph_row
17441 to the end of the display line. Called from display_line. If the
17442 glyph row is empty, add a space glyph to it so that we know the
17443 face to draw. Set the glyph row flag fill_line_p. If the glyph
17444 row is R2L, prepend a stretch glyph to cover the empty space to the
17445 left of the leftmost glyph. */
17446
17447 static void
17448 extend_face_to_end_of_line (struct it *it)
17449 {
17450 struct face *face;
17451 struct frame *f = it->f;
17452
17453 /* If line is already filled, do nothing. Non window-system frames
17454 get a grace of one more ``pixel'' because their characters are
17455 1-``pixel'' wide, so they hit the equality too early. This grace
17456 is needed only for R2L rows that are not continued, to produce
17457 one extra blank where we could display the cursor. */
17458 if (it->current_x >= it->last_visible_x
17459 + (!FRAME_WINDOW_P (f)
17460 && it->glyph_row->reversed_p
17461 && !it->glyph_row->continued_p))
17462 return;
17463
17464 /* Face extension extends the background and box of IT->face_id
17465 to the end of the line. If the background equals the background
17466 of the frame, we don't have to do anything. */
17467 if (it->face_before_selective_p)
17468 face = FACE_FROM_ID (f, it->saved_face_id);
17469 else
17470 face = FACE_FROM_ID (f, it->face_id);
17471
17472 if (FRAME_WINDOW_P (f)
17473 && it->glyph_row->displays_text_p
17474 && face->box == FACE_NO_BOX
17475 && face->background == FRAME_BACKGROUND_PIXEL (f)
17476 && !face->stipple
17477 && !it->glyph_row->reversed_p)
17478 return;
17479
17480 /* Set the glyph row flag indicating that the face of the last glyph
17481 in the text area has to be drawn to the end of the text area. */
17482 it->glyph_row->fill_line_p = 1;
17483
17484 /* If current character of IT is not ASCII, make sure we have the
17485 ASCII face. This will be automatically undone the next time
17486 get_next_display_element returns a multibyte character. Note
17487 that the character will always be single byte in unibyte
17488 text. */
17489 if (!ASCII_CHAR_P (it->c))
17490 {
17491 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17492 }
17493
17494 if (FRAME_WINDOW_P (f))
17495 {
17496 /* If the row is empty, add a space with the current face of IT,
17497 so that we know which face to draw. */
17498 if (it->glyph_row->used[TEXT_AREA] == 0)
17499 {
17500 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17501 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17502 it->glyph_row->used[TEXT_AREA] = 1;
17503 }
17504 #ifdef HAVE_WINDOW_SYSTEM
17505 if (it->glyph_row->reversed_p)
17506 {
17507 /* Prepend a stretch glyph to the row, such that the
17508 rightmost glyph will be drawn flushed all the way to the
17509 right margin of the window. The stretch glyph that will
17510 occupy the empty space, if any, to the left of the
17511 glyphs. */
17512 struct font *font = face->font ? face->font : FRAME_FONT (f);
17513 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17514 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17515 struct glyph *g;
17516 int row_width, stretch_ascent, stretch_width;
17517 struct text_pos saved_pos;
17518 int saved_face_id, saved_avoid_cursor;
17519
17520 for (row_width = 0, g = row_start; g < row_end; g++)
17521 row_width += g->pixel_width;
17522 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17523 if (stretch_width > 0)
17524 {
17525 stretch_ascent =
17526 (((it->ascent + it->descent)
17527 * FONT_BASE (font)) / FONT_HEIGHT (font));
17528 saved_pos = it->position;
17529 memset (&it->position, 0, sizeof it->position);
17530 saved_avoid_cursor = it->avoid_cursor_p;
17531 it->avoid_cursor_p = 1;
17532 saved_face_id = it->face_id;
17533 /* The last row's stretch glyph should get the default
17534 face, to avoid painting the rest of the window with
17535 the region face, if the region ends at ZV. */
17536 if (it->glyph_row->ends_at_zv_p)
17537 it->face_id = DEFAULT_FACE_ID;
17538 else
17539 it->face_id = face->id;
17540 append_stretch_glyph (it, make_number (0), stretch_width,
17541 it->ascent + it->descent, stretch_ascent);
17542 it->position = saved_pos;
17543 it->avoid_cursor_p = saved_avoid_cursor;
17544 it->face_id = saved_face_id;
17545 }
17546 }
17547 #endif /* HAVE_WINDOW_SYSTEM */
17548 }
17549 else
17550 {
17551 /* Save some values that must not be changed. */
17552 int saved_x = it->current_x;
17553 struct text_pos saved_pos;
17554 Lisp_Object saved_object;
17555 enum display_element_type saved_what = it->what;
17556 int saved_face_id = it->face_id;
17557
17558 saved_object = it->object;
17559 saved_pos = it->position;
17560
17561 it->what = IT_CHARACTER;
17562 memset (&it->position, 0, sizeof it->position);
17563 it->object = make_number (0);
17564 it->c = it->char_to_display = ' ';
17565 it->len = 1;
17566 /* The last row's blank glyphs should get the default face, to
17567 avoid painting the rest of the window with the region face,
17568 if the region ends at ZV. */
17569 if (it->glyph_row->ends_at_zv_p)
17570 it->face_id = DEFAULT_FACE_ID;
17571 else
17572 it->face_id = face->id;
17573
17574 PRODUCE_GLYPHS (it);
17575
17576 while (it->current_x <= it->last_visible_x)
17577 PRODUCE_GLYPHS (it);
17578
17579 /* Don't count these blanks really. It would let us insert a left
17580 truncation glyph below and make us set the cursor on them, maybe. */
17581 it->current_x = saved_x;
17582 it->object = saved_object;
17583 it->position = saved_pos;
17584 it->what = saved_what;
17585 it->face_id = saved_face_id;
17586 }
17587 }
17588
17589
17590 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17591 trailing whitespace. */
17592
17593 static int
17594 trailing_whitespace_p (EMACS_INT charpos)
17595 {
17596 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17597 int c = 0;
17598
17599 while (bytepos < ZV_BYTE
17600 && (c = FETCH_CHAR (bytepos),
17601 c == ' ' || c == '\t'))
17602 ++bytepos;
17603
17604 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17605 {
17606 if (bytepos != PT_BYTE)
17607 return 1;
17608 }
17609 return 0;
17610 }
17611
17612
17613 /* Highlight trailing whitespace, if any, in ROW. */
17614
17615 static void
17616 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17617 {
17618 int used = row->used[TEXT_AREA];
17619
17620 if (used)
17621 {
17622 struct glyph *start = row->glyphs[TEXT_AREA];
17623 struct glyph *glyph = start + used - 1;
17624
17625 if (row->reversed_p)
17626 {
17627 /* Right-to-left rows need to be processed in the opposite
17628 direction, so swap the edge pointers. */
17629 glyph = start;
17630 start = row->glyphs[TEXT_AREA] + used - 1;
17631 }
17632
17633 /* Skip over glyphs inserted to display the cursor at the
17634 end of a line, for extending the face of the last glyph
17635 to the end of the line on terminals, and for truncation
17636 and continuation glyphs. */
17637 if (!row->reversed_p)
17638 {
17639 while (glyph >= start
17640 && glyph->type == CHAR_GLYPH
17641 && INTEGERP (glyph->object))
17642 --glyph;
17643 }
17644 else
17645 {
17646 while (glyph <= start
17647 && glyph->type == CHAR_GLYPH
17648 && INTEGERP (glyph->object))
17649 ++glyph;
17650 }
17651
17652 /* If last glyph is a space or stretch, and it's trailing
17653 whitespace, set the face of all trailing whitespace glyphs in
17654 IT->glyph_row to `trailing-whitespace'. */
17655 if ((row->reversed_p ? glyph <= start : glyph >= start)
17656 && BUFFERP (glyph->object)
17657 && (glyph->type == STRETCH_GLYPH
17658 || (glyph->type == CHAR_GLYPH
17659 && glyph->u.ch == ' '))
17660 && trailing_whitespace_p (glyph->charpos))
17661 {
17662 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17663 if (face_id < 0)
17664 return;
17665
17666 if (!row->reversed_p)
17667 {
17668 while (glyph >= start
17669 && BUFFERP (glyph->object)
17670 && (glyph->type == STRETCH_GLYPH
17671 || (glyph->type == CHAR_GLYPH
17672 && glyph->u.ch == ' ')))
17673 (glyph--)->face_id = face_id;
17674 }
17675 else
17676 {
17677 while (glyph <= start
17678 && BUFFERP (glyph->object)
17679 && (glyph->type == STRETCH_GLYPH
17680 || (glyph->type == CHAR_GLYPH
17681 && glyph->u.ch == ' ')))
17682 (glyph++)->face_id = face_id;
17683 }
17684 }
17685 }
17686 }
17687
17688
17689 /* Value is non-zero if glyph row ROW should be
17690 used to hold the cursor. */
17691
17692 static int
17693 cursor_row_p (struct glyph_row *row)
17694 {
17695 int result = 1;
17696
17697 if (PT == CHARPOS (row->end.pos))
17698 {
17699 /* Suppose the row ends on a string.
17700 Unless the row is continued, that means it ends on a newline
17701 in the string. If it's anything other than a display string
17702 (e.g. a before-string from an overlay), we don't want the
17703 cursor there. (This heuristic seems to give the optimal
17704 behavior for the various types of multi-line strings.) */
17705 if (CHARPOS (row->end.string_pos) >= 0)
17706 {
17707 if (row->continued_p)
17708 result = 1;
17709 else
17710 {
17711 /* Check for `display' property. */
17712 struct glyph *beg = row->glyphs[TEXT_AREA];
17713 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17714 struct glyph *glyph;
17715
17716 result = 0;
17717 for (glyph = end; glyph >= beg; --glyph)
17718 if (STRINGP (glyph->object))
17719 {
17720 Lisp_Object prop
17721 = Fget_char_property (make_number (PT),
17722 Qdisplay, Qnil);
17723 result =
17724 (!NILP (prop)
17725 && display_prop_string_p (prop, glyph->object));
17726 break;
17727 }
17728 }
17729 }
17730 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17731 {
17732 /* If the row ends in middle of a real character,
17733 and the line is continued, we want the cursor here.
17734 That's because CHARPOS (ROW->end.pos) would equal
17735 PT if PT is before the character. */
17736 if (!row->ends_in_ellipsis_p)
17737 result = row->continued_p;
17738 else
17739 /* If the row ends in an ellipsis, then
17740 CHARPOS (ROW->end.pos) will equal point after the
17741 invisible text. We want that position to be displayed
17742 after the ellipsis. */
17743 result = 0;
17744 }
17745 /* If the row ends at ZV, display the cursor at the end of that
17746 row instead of at the start of the row below. */
17747 else if (row->ends_at_zv_p)
17748 result = 1;
17749 else
17750 result = 0;
17751 }
17752
17753 return result;
17754 }
17755
17756 \f
17757
17758 /* Push the display property PROP so that it will be rendered at the
17759 current position in IT. Return 1 if PROP was successfully pushed,
17760 0 otherwise. */
17761
17762 static int
17763 push_display_prop (struct it *it, Lisp_Object prop)
17764 {
17765 xassert (it->method == GET_FROM_BUFFER);
17766
17767 push_it (it, NULL);
17768
17769 if (STRINGP (prop))
17770 {
17771 if (SCHARS (prop) == 0)
17772 {
17773 pop_it (it);
17774 return 0;
17775 }
17776
17777 it->string = prop;
17778 it->multibyte_p = STRING_MULTIBYTE (it->string);
17779 it->current.overlay_string_index = -1;
17780 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17781 it->end_charpos = it->string_nchars = SCHARS (it->string);
17782 it->method = GET_FROM_STRING;
17783 it->stop_charpos = 0;
17784 it->prev_stop = 0;
17785 it->base_level_stop = 0;
17786 it->string_from_display_prop_p = 1;
17787 it->from_disp_prop_p = 1;
17788
17789 /* Force paragraph direction to be that of the parent
17790 buffer. */
17791 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
17792 it->paragraph_embedding = it->bidi_it.paragraph_dir;
17793 else
17794 it->paragraph_embedding = L2R;
17795
17796 /* Set up the bidi iterator for this display string. */
17797 if (it->bidi_p)
17798 {
17799 it->bidi_it.string.lstring = it->string;
17800 it->bidi_it.string.s = NULL;
17801 it->bidi_it.string.schars = it->end_charpos;
17802 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
17803 it->bidi_it.string.from_disp_str = 1;
17804 it->bidi_it.string.unibyte = !it->multibyte_p;
17805 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
17806 }
17807 }
17808 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17809 {
17810 it->method = GET_FROM_STRETCH;
17811 it->object = prop;
17812 }
17813 #ifdef HAVE_WINDOW_SYSTEM
17814 else if (IMAGEP (prop))
17815 {
17816 it->what = IT_IMAGE;
17817 it->image_id = lookup_image (it->f, prop);
17818 it->method = GET_FROM_IMAGE;
17819 }
17820 #endif /* HAVE_WINDOW_SYSTEM */
17821 else
17822 {
17823 pop_it (it); /* bogus display property, give up */
17824 return 0;
17825 }
17826
17827 return 1;
17828 }
17829
17830 /* Return the character-property PROP at the current position in IT. */
17831
17832 static Lisp_Object
17833 get_it_property (struct it *it, Lisp_Object prop)
17834 {
17835 Lisp_Object position;
17836
17837 if (STRINGP (it->object))
17838 position = make_number (IT_STRING_CHARPOS (*it));
17839 else if (BUFFERP (it->object))
17840 position = make_number (IT_CHARPOS (*it));
17841 else
17842 return Qnil;
17843
17844 return Fget_char_property (position, prop, it->object);
17845 }
17846
17847 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17848
17849 static void
17850 handle_line_prefix (struct it *it)
17851 {
17852 Lisp_Object prefix;
17853
17854 if (it->continuation_lines_width > 0)
17855 {
17856 prefix = get_it_property (it, Qwrap_prefix);
17857 if (NILP (prefix))
17858 prefix = Vwrap_prefix;
17859 }
17860 else
17861 {
17862 prefix = get_it_property (it, Qline_prefix);
17863 if (NILP (prefix))
17864 prefix = Vline_prefix;
17865 }
17866 if (! NILP (prefix) && push_display_prop (it, prefix))
17867 {
17868 /* If the prefix is wider than the window, and we try to wrap
17869 it, it would acquire its own wrap prefix, and so on till the
17870 iterator stack overflows. So, don't wrap the prefix. */
17871 it->line_wrap = TRUNCATE;
17872 it->avoid_cursor_p = 1;
17873 }
17874 }
17875
17876 \f
17877
17878 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17879 only for R2L lines from display_line and display_string, when they
17880 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
17881 the line/string needs to be continued on the next glyph row. */
17882 static void
17883 unproduce_glyphs (struct it *it, int n)
17884 {
17885 struct glyph *glyph, *end;
17886
17887 xassert (it->glyph_row);
17888 xassert (it->glyph_row->reversed_p);
17889 xassert (it->area == TEXT_AREA);
17890 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17891
17892 if (n > it->glyph_row->used[TEXT_AREA])
17893 n = it->glyph_row->used[TEXT_AREA];
17894 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17895 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17896 for ( ; glyph < end; glyph++)
17897 glyph[-n] = *glyph;
17898 }
17899
17900 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17901 and ROW->maxpos. */
17902 static void
17903 find_row_edges (struct it *it, struct glyph_row *row,
17904 EMACS_INT min_pos, EMACS_INT min_bpos,
17905 EMACS_INT max_pos, EMACS_INT max_bpos)
17906 {
17907 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17908 lines' rows is implemented for bidi-reordered rows. */
17909
17910 /* ROW->minpos is the value of min_pos, the minimal buffer position
17911 we have in ROW. */
17912 if (min_pos <= ZV)
17913 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17914 else
17915 /* We didn't find _any_ valid buffer positions in any of the
17916 glyphs, so we must trust the iterator's computed positions. */
17917 row->minpos = row->start.pos;
17918 if (max_pos <= 0)
17919 {
17920 max_pos = CHARPOS (it->current.pos);
17921 max_bpos = BYTEPOS (it->current.pos);
17922 }
17923
17924 /* Here are the various use-cases for ending the row, and the
17925 corresponding values for ROW->maxpos:
17926
17927 Line ends in a newline from buffer eol_pos + 1
17928 Line is continued from buffer max_pos + 1
17929 Line is truncated on right it->current.pos
17930 Line ends in a newline from string max_pos
17931 Line is continued from string max_pos
17932 Line is continued from display vector max_pos
17933 Line is entirely from a string min_pos == max_pos
17934 Line is entirely from a display vector min_pos == max_pos
17935 Line that ends at ZV ZV
17936
17937 If you discover other use-cases, please add them here as
17938 appropriate. */
17939 if (row->ends_at_zv_p)
17940 row->maxpos = it->current.pos;
17941 else if (row->used[TEXT_AREA])
17942 {
17943 if (row->ends_in_newline_from_string_p)
17944 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17945 else if (CHARPOS (it->eol_pos) > 0)
17946 SET_TEXT_POS (row->maxpos,
17947 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17948 else if (row->continued_p)
17949 {
17950 /* If max_pos is different from IT's current position, it
17951 means IT->method does not belong to the display element
17952 at max_pos. However, it also means that the display
17953 element at max_pos was displayed in its entirety on this
17954 line, which is equivalent to saying that the next line
17955 starts at the next buffer position. */
17956 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17957 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17958 else
17959 {
17960 INC_BOTH (max_pos, max_bpos);
17961 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17962 }
17963 }
17964 else if (row->truncated_on_right_p)
17965 /* display_line already called reseat_at_next_visible_line_start,
17966 which puts the iterator at the beginning of the next line, in
17967 the logical order. */
17968 row->maxpos = it->current.pos;
17969 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17970 /* A line that is entirely from a string/image/stretch... */
17971 row->maxpos = row->minpos;
17972 else
17973 abort ();
17974 }
17975 else
17976 row->maxpos = it->current.pos;
17977 }
17978
17979 /* Construct the glyph row IT->glyph_row in the desired matrix of
17980 IT->w from text at the current position of IT. See dispextern.h
17981 for an overview of struct it. Value is non-zero if
17982 IT->glyph_row displays text, as opposed to a line displaying ZV
17983 only. */
17984
17985 static int
17986 display_line (struct it *it)
17987 {
17988 struct glyph_row *row = it->glyph_row;
17989 Lisp_Object overlay_arrow_string;
17990 struct it wrap_it;
17991 int may_wrap = 0, wrap_x IF_LINT (= 0);
17992 int wrap_row_used = -1;
17993 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
17994 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
17995 int wrap_row_extra_line_spacing IF_LINT (= 0);
17996 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
17997 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
17998 int cvpos;
17999 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18000 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18001
18002 /* We always start displaying at hpos zero even if hscrolled. */
18003 xassert (it->hpos == 0 && it->current_x == 0);
18004
18005 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18006 >= it->w->desired_matrix->nrows)
18007 {
18008 it->w->nrows_scale_factor++;
18009 fonts_changed_p = 1;
18010 return 0;
18011 }
18012
18013 /* Is IT->w showing the region? */
18014 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18015
18016 /* Clear the result glyph row and enable it. */
18017 prepare_desired_row (row);
18018
18019 row->y = it->current_y;
18020 row->start = it->start;
18021 row->continuation_lines_width = it->continuation_lines_width;
18022 row->displays_text_p = 1;
18023 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18024 it->starts_in_middle_of_char_p = 0;
18025
18026 /* Arrange the overlays nicely for our purposes. Usually, we call
18027 display_line on only one line at a time, in which case this
18028 can't really hurt too much, or we call it on lines which appear
18029 one after another in the buffer, in which case all calls to
18030 recenter_overlay_lists but the first will be pretty cheap. */
18031 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18032
18033 /* Move over display elements that are not visible because we are
18034 hscrolled. This may stop at an x-position < IT->first_visible_x
18035 if the first glyph is partially visible or if we hit a line end. */
18036 if (it->current_x < it->first_visible_x)
18037 {
18038 this_line_min_pos = row->start.pos;
18039 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18040 MOVE_TO_POS | MOVE_TO_X);
18041 /* Record the smallest positions seen while we moved over
18042 display elements that are not visible. This is needed by
18043 redisplay_internal for optimizing the case where the cursor
18044 stays inside the same line. The rest of this function only
18045 considers positions that are actually displayed, so
18046 RECORD_MAX_MIN_POS will not otherwise record positions that
18047 are hscrolled to the left of the left edge of the window. */
18048 min_pos = CHARPOS (this_line_min_pos);
18049 min_bpos = BYTEPOS (this_line_min_pos);
18050 }
18051 else
18052 {
18053 /* We only do this when not calling `move_it_in_display_line_to'
18054 above, because move_it_in_display_line_to calls
18055 handle_line_prefix itself. */
18056 handle_line_prefix (it);
18057 }
18058
18059 /* Get the initial row height. This is either the height of the
18060 text hscrolled, if there is any, or zero. */
18061 row->ascent = it->max_ascent;
18062 row->height = it->max_ascent + it->max_descent;
18063 row->phys_ascent = it->max_phys_ascent;
18064 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18065 row->extra_line_spacing = it->max_extra_line_spacing;
18066
18067 /* Utility macro to record max and min buffer positions seen until now. */
18068 #define RECORD_MAX_MIN_POS(IT) \
18069 do \
18070 { \
18071 if (IT_CHARPOS (*(IT)) < min_pos) \
18072 { \
18073 min_pos = IT_CHARPOS (*(IT)); \
18074 min_bpos = IT_BYTEPOS (*(IT)); \
18075 } \
18076 if (IT_CHARPOS (*(IT)) > max_pos) \
18077 { \
18078 max_pos = IT_CHARPOS (*(IT)); \
18079 max_bpos = IT_BYTEPOS (*(IT)); \
18080 } \
18081 } \
18082 while (0)
18083
18084 /* Loop generating characters. The loop is left with IT on the next
18085 character to display. */
18086 while (1)
18087 {
18088 int n_glyphs_before, hpos_before, x_before;
18089 int x, nglyphs;
18090 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18091
18092 /* Retrieve the next thing to display. Value is zero if end of
18093 buffer reached. */
18094 if (!get_next_display_element (it))
18095 {
18096 /* Maybe add a space at the end of this line that is used to
18097 display the cursor there under X. Set the charpos of the
18098 first glyph of blank lines not corresponding to any text
18099 to -1. */
18100 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18101 row->exact_window_width_line_p = 1;
18102 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18103 || row->used[TEXT_AREA] == 0)
18104 {
18105 row->glyphs[TEXT_AREA]->charpos = -1;
18106 row->displays_text_p = 0;
18107
18108 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18109 && (!MINI_WINDOW_P (it->w)
18110 || (minibuf_level && EQ (it->window, minibuf_window))))
18111 row->indicate_empty_line_p = 1;
18112 }
18113
18114 it->continuation_lines_width = 0;
18115 row->ends_at_zv_p = 1;
18116 /* A row that displays right-to-left text must always have
18117 its last face extended all the way to the end of line,
18118 even if this row ends in ZV, because we still write to
18119 the screen left to right. */
18120 if (row->reversed_p)
18121 extend_face_to_end_of_line (it);
18122 break;
18123 }
18124
18125 /* Now, get the metrics of what we want to display. This also
18126 generates glyphs in `row' (which is IT->glyph_row). */
18127 n_glyphs_before = row->used[TEXT_AREA];
18128 x = it->current_x;
18129
18130 /* Remember the line height so far in case the next element doesn't
18131 fit on the line. */
18132 if (it->line_wrap != TRUNCATE)
18133 {
18134 ascent = it->max_ascent;
18135 descent = it->max_descent;
18136 phys_ascent = it->max_phys_ascent;
18137 phys_descent = it->max_phys_descent;
18138
18139 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18140 {
18141 if (IT_DISPLAYING_WHITESPACE (it))
18142 may_wrap = 1;
18143 else if (may_wrap)
18144 {
18145 wrap_it = *it;
18146 wrap_x = x;
18147 wrap_row_used = row->used[TEXT_AREA];
18148 wrap_row_ascent = row->ascent;
18149 wrap_row_height = row->height;
18150 wrap_row_phys_ascent = row->phys_ascent;
18151 wrap_row_phys_height = row->phys_height;
18152 wrap_row_extra_line_spacing = row->extra_line_spacing;
18153 wrap_row_min_pos = min_pos;
18154 wrap_row_min_bpos = min_bpos;
18155 wrap_row_max_pos = max_pos;
18156 wrap_row_max_bpos = max_bpos;
18157 may_wrap = 0;
18158 }
18159 }
18160 }
18161
18162 PRODUCE_GLYPHS (it);
18163
18164 /* If this display element was in marginal areas, continue with
18165 the next one. */
18166 if (it->area != TEXT_AREA)
18167 {
18168 row->ascent = max (row->ascent, it->max_ascent);
18169 row->height = max (row->height, it->max_ascent + it->max_descent);
18170 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18171 row->phys_height = max (row->phys_height,
18172 it->max_phys_ascent + it->max_phys_descent);
18173 row->extra_line_spacing = max (row->extra_line_spacing,
18174 it->max_extra_line_spacing);
18175 set_iterator_to_next (it, 1);
18176 continue;
18177 }
18178
18179 /* Does the display element fit on the line? If we truncate
18180 lines, we should draw past the right edge of the window. If
18181 we don't truncate, we want to stop so that we can display the
18182 continuation glyph before the right margin. If lines are
18183 continued, there are two possible strategies for characters
18184 resulting in more than 1 glyph (e.g. tabs): Display as many
18185 glyphs as possible in this line and leave the rest for the
18186 continuation line, or display the whole element in the next
18187 line. Original redisplay did the former, so we do it also. */
18188 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18189 hpos_before = it->hpos;
18190 x_before = x;
18191
18192 if (/* Not a newline. */
18193 nglyphs > 0
18194 /* Glyphs produced fit entirely in the line. */
18195 && it->current_x < it->last_visible_x)
18196 {
18197 it->hpos += nglyphs;
18198 row->ascent = max (row->ascent, it->max_ascent);
18199 row->height = max (row->height, it->max_ascent + it->max_descent);
18200 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18201 row->phys_height = max (row->phys_height,
18202 it->max_phys_ascent + it->max_phys_descent);
18203 row->extra_line_spacing = max (row->extra_line_spacing,
18204 it->max_extra_line_spacing);
18205 if (it->current_x - it->pixel_width < it->first_visible_x)
18206 row->x = x - it->first_visible_x;
18207 /* Record the maximum and minimum buffer positions seen so
18208 far in glyphs that will be displayed by this row. */
18209 if (it->bidi_p)
18210 RECORD_MAX_MIN_POS (it);
18211 }
18212 else
18213 {
18214 int i, new_x;
18215 struct glyph *glyph;
18216
18217 for (i = 0; i < nglyphs; ++i, x = new_x)
18218 {
18219 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18220 new_x = x + glyph->pixel_width;
18221
18222 if (/* Lines are continued. */
18223 it->line_wrap != TRUNCATE
18224 && (/* Glyph doesn't fit on the line. */
18225 new_x > it->last_visible_x
18226 /* Or it fits exactly on a window system frame. */
18227 || (new_x == it->last_visible_x
18228 && FRAME_WINDOW_P (it->f))))
18229 {
18230 /* End of a continued line. */
18231
18232 if (it->hpos == 0
18233 || (new_x == it->last_visible_x
18234 && FRAME_WINDOW_P (it->f)))
18235 {
18236 /* Current glyph is the only one on the line or
18237 fits exactly on the line. We must continue
18238 the line because we can't draw the cursor
18239 after the glyph. */
18240 row->continued_p = 1;
18241 it->current_x = new_x;
18242 it->continuation_lines_width += new_x;
18243 ++it->hpos;
18244 /* Record the maximum and minimum buffer
18245 positions seen so far in glyphs that will be
18246 displayed by this row. */
18247 if (it->bidi_p)
18248 RECORD_MAX_MIN_POS (it);
18249 if (i == nglyphs - 1)
18250 {
18251 /* If line-wrap is on, check if a previous
18252 wrap point was found. */
18253 if (wrap_row_used > 0
18254 /* Even if there is a previous wrap
18255 point, continue the line here as
18256 usual, if (i) the previous character
18257 was a space or tab AND (ii) the
18258 current character is not. */
18259 && (!may_wrap
18260 || IT_DISPLAYING_WHITESPACE (it)))
18261 goto back_to_wrap;
18262
18263 set_iterator_to_next (it, 1);
18264 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18265 {
18266 if (!get_next_display_element (it))
18267 {
18268 row->exact_window_width_line_p = 1;
18269 it->continuation_lines_width = 0;
18270 row->continued_p = 0;
18271 row->ends_at_zv_p = 1;
18272 }
18273 else if (ITERATOR_AT_END_OF_LINE_P (it))
18274 {
18275 row->continued_p = 0;
18276 row->exact_window_width_line_p = 1;
18277 }
18278 }
18279 }
18280 }
18281 else if (CHAR_GLYPH_PADDING_P (*glyph)
18282 && !FRAME_WINDOW_P (it->f))
18283 {
18284 /* A padding glyph that doesn't fit on this line.
18285 This means the whole character doesn't fit
18286 on the line. */
18287 if (row->reversed_p)
18288 unproduce_glyphs (it, row->used[TEXT_AREA]
18289 - n_glyphs_before);
18290 row->used[TEXT_AREA] = n_glyphs_before;
18291
18292 /* Fill the rest of the row with continuation
18293 glyphs like in 20.x. */
18294 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18295 < row->glyphs[1 + TEXT_AREA])
18296 produce_special_glyphs (it, IT_CONTINUATION);
18297
18298 row->continued_p = 1;
18299 it->current_x = x_before;
18300 it->continuation_lines_width += x_before;
18301
18302 /* Restore the height to what it was before the
18303 element not fitting on the line. */
18304 it->max_ascent = ascent;
18305 it->max_descent = descent;
18306 it->max_phys_ascent = phys_ascent;
18307 it->max_phys_descent = phys_descent;
18308 }
18309 else if (wrap_row_used > 0)
18310 {
18311 back_to_wrap:
18312 if (row->reversed_p)
18313 unproduce_glyphs (it,
18314 row->used[TEXT_AREA] - wrap_row_used);
18315 *it = wrap_it;
18316 it->continuation_lines_width += wrap_x;
18317 row->used[TEXT_AREA] = wrap_row_used;
18318 row->ascent = wrap_row_ascent;
18319 row->height = wrap_row_height;
18320 row->phys_ascent = wrap_row_phys_ascent;
18321 row->phys_height = wrap_row_phys_height;
18322 row->extra_line_spacing = wrap_row_extra_line_spacing;
18323 min_pos = wrap_row_min_pos;
18324 min_bpos = wrap_row_min_bpos;
18325 max_pos = wrap_row_max_pos;
18326 max_bpos = wrap_row_max_bpos;
18327 row->continued_p = 1;
18328 row->ends_at_zv_p = 0;
18329 row->exact_window_width_line_p = 0;
18330 it->continuation_lines_width += x;
18331
18332 /* Make sure that a non-default face is extended
18333 up to the right margin of the window. */
18334 extend_face_to_end_of_line (it);
18335 }
18336 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18337 {
18338 /* A TAB that extends past the right edge of the
18339 window. This produces a single glyph on
18340 window system frames. We leave the glyph in
18341 this row and let it fill the row, but don't
18342 consume the TAB. */
18343 it->continuation_lines_width += it->last_visible_x;
18344 row->ends_in_middle_of_char_p = 1;
18345 row->continued_p = 1;
18346 glyph->pixel_width = it->last_visible_x - x;
18347 it->starts_in_middle_of_char_p = 1;
18348 }
18349 else
18350 {
18351 /* Something other than a TAB that draws past
18352 the right edge of the window. Restore
18353 positions to values before the element. */
18354 if (row->reversed_p)
18355 unproduce_glyphs (it, row->used[TEXT_AREA]
18356 - (n_glyphs_before + i));
18357 row->used[TEXT_AREA] = n_glyphs_before + i;
18358
18359 /* Display continuation glyphs. */
18360 if (!FRAME_WINDOW_P (it->f))
18361 produce_special_glyphs (it, IT_CONTINUATION);
18362 row->continued_p = 1;
18363
18364 it->current_x = x_before;
18365 it->continuation_lines_width += x;
18366 extend_face_to_end_of_line (it);
18367
18368 if (nglyphs > 1 && i > 0)
18369 {
18370 row->ends_in_middle_of_char_p = 1;
18371 it->starts_in_middle_of_char_p = 1;
18372 }
18373
18374 /* Restore the height to what it was before the
18375 element not fitting on the line. */
18376 it->max_ascent = ascent;
18377 it->max_descent = descent;
18378 it->max_phys_ascent = phys_ascent;
18379 it->max_phys_descent = phys_descent;
18380 }
18381
18382 break;
18383 }
18384 else if (new_x > it->first_visible_x)
18385 {
18386 /* Increment number of glyphs actually displayed. */
18387 ++it->hpos;
18388
18389 /* Record the maximum and minimum buffer positions
18390 seen so far in glyphs that will be displayed by
18391 this row. */
18392 if (it->bidi_p)
18393 RECORD_MAX_MIN_POS (it);
18394
18395 if (x < it->first_visible_x)
18396 /* Glyph is partially visible, i.e. row starts at
18397 negative X position. */
18398 row->x = x - it->first_visible_x;
18399 }
18400 else
18401 {
18402 /* Glyph is completely off the left margin of the
18403 window. This should not happen because of the
18404 move_it_in_display_line at the start of this
18405 function, unless the text display area of the
18406 window is empty. */
18407 xassert (it->first_visible_x <= it->last_visible_x);
18408 }
18409 }
18410
18411 row->ascent = max (row->ascent, it->max_ascent);
18412 row->height = max (row->height, it->max_ascent + it->max_descent);
18413 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18414 row->phys_height = max (row->phys_height,
18415 it->max_phys_ascent + it->max_phys_descent);
18416 row->extra_line_spacing = max (row->extra_line_spacing,
18417 it->max_extra_line_spacing);
18418
18419 /* End of this display line if row is continued. */
18420 if (row->continued_p || row->ends_at_zv_p)
18421 break;
18422 }
18423
18424 at_end_of_line:
18425 /* Is this a line end? If yes, we're also done, after making
18426 sure that a non-default face is extended up to the right
18427 margin of the window. */
18428 if (ITERATOR_AT_END_OF_LINE_P (it))
18429 {
18430 int used_before = row->used[TEXT_AREA];
18431
18432 row->ends_in_newline_from_string_p = STRINGP (it->object);
18433
18434 /* Add a space at the end of the line that is used to
18435 display the cursor there. */
18436 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18437 append_space_for_newline (it, 0);
18438
18439 /* Extend the face to the end of the line. */
18440 extend_face_to_end_of_line (it);
18441
18442 /* Make sure we have the position. */
18443 if (used_before == 0)
18444 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18445
18446 /* Record the position of the newline, for use in
18447 find_row_edges. */
18448 it->eol_pos = it->current.pos;
18449
18450 /* Consume the line end. This skips over invisible lines. */
18451 set_iterator_to_next (it, 1);
18452 it->continuation_lines_width = 0;
18453 break;
18454 }
18455
18456 /* Proceed with next display element. Note that this skips
18457 over lines invisible because of selective display. */
18458 set_iterator_to_next (it, 1);
18459
18460 /* If we truncate lines, we are done when the last displayed
18461 glyphs reach past the right margin of the window. */
18462 if (it->line_wrap == TRUNCATE
18463 && (FRAME_WINDOW_P (it->f)
18464 ? (it->current_x >= it->last_visible_x)
18465 : (it->current_x > it->last_visible_x)))
18466 {
18467 /* Maybe add truncation glyphs. */
18468 if (!FRAME_WINDOW_P (it->f))
18469 {
18470 int i, n;
18471
18472 if (!row->reversed_p)
18473 {
18474 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18475 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18476 break;
18477 }
18478 else
18479 {
18480 for (i = 0; i < row->used[TEXT_AREA]; i++)
18481 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18482 break;
18483 /* Remove any padding glyphs at the front of ROW, to
18484 make room for the truncation glyphs we will be
18485 adding below. The loop below always inserts at
18486 least one truncation glyph, so also remove the
18487 last glyph added to ROW. */
18488 unproduce_glyphs (it, i + 1);
18489 /* Adjust i for the loop below. */
18490 i = row->used[TEXT_AREA] - (i + 1);
18491 }
18492
18493 for (n = row->used[TEXT_AREA]; i < n; ++i)
18494 {
18495 row->used[TEXT_AREA] = i;
18496 produce_special_glyphs (it, IT_TRUNCATION);
18497 }
18498 }
18499 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18500 {
18501 /* Don't truncate if we can overflow newline into fringe. */
18502 if (!get_next_display_element (it))
18503 {
18504 it->continuation_lines_width = 0;
18505 row->ends_at_zv_p = 1;
18506 row->exact_window_width_line_p = 1;
18507 break;
18508 }
18509 if (ITERATOR_AT_END_OF_LINE_P (it))
18510 {
18511 row->exact_window_width_line_p = 1;
18512 goto at_end_of_line;
18513 }
18514 }
18515
18516 row->truncated_on_right_p = 1;
18517 it->continuation_lines_width = 0;
18518 reseat_at_next_visible_line_start (it, 0);
18519 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18520 it->hpos = hpos_before;
18521 it->current_x = x_before;
18522 break;
18523 }
18524 }
18525
18526 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18527 at the left window margin. */
18528 if (it->first_visible_x
18529 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18530 {
18531 if (!FRAME_WINDOW_P (it->f))
18532 insert_left_trunc_glyphs (it);
18533 row->truncated_on_left_p = 1;
18534 }
18535
18536 /* Remember the position at which this line ends.
18537
18538 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18539 cannot be before the call to find_row_edges below, since that is
18540 where these positions are determined. */
18541 row->end = it->current;
18542 if (!it->bidi_p)
18543 {
18544 row->minpos = row->start.pos;
18545 row->maxpos = row->end.pos;
18546 }
18547 else
18548 {
18549 /* ROW->minpos and ROW->maxpos must be the smallest and
18550 `1 + the largest' buffer positions in ROW. But if ROW was
18551 bidi-reordered, these two positions can be anywhere in the
18552 row, so we must determine them now. */
18553 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18554 }
18555
18556 /* If the start of this line is the overlay arrow-position, then
18557 mark this glyph row as the one containing the overlay arrow.
18558 This is clearly a mess with variable size fonts. It would be
18559 better to let it be displayed like cursors under X. */
18560 if ((row->displays_text_p || !overlay_arrow_seen)
18561 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18562 !NILP (overlay_arrow_string)))
18563 {
18564 /* Overlay arrow in window redisplay is a fringe bitmap. */
18565 if (STRINGP (overlay_arrow_string))
18566 {
18567 struct glyph_row *arrow_row
18568 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18569 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18570 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18571 struct glyph *p = row->glyphs[TEXT_AREA];
18572 struct glyph *p2, *end;
18573
18574 /* Copy the arrow glyphs. */
18575 while (glyph < arrow_end)
18576 *p++ = *glyph++;
18577
18578 /* Throw away padding glyphs. */
18579 p2 = p;
18580 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18581 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18582 ++p2;
18583 if (p2 > p)
18584 {
18585 while (p2 < end)
18586 *p++ = *p2++;
18587 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18588 }
18589 }
18590 else
18591 {
18592 xassert (INTEGERP (overlay_arrow_string));
18593 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18594 }
18595 overlay_arrow_seen = 1;
18596 }
18597
18598 /* Compute pixel dimensions of this line. */
18599 compute_line_metrics (it);
18600
18601 /* Record whether this row ends inside an ellipsis. */
18602 row->ends_in_ellipsis_p
18603 = (it->method == GET_FROM_DISPLAY_VECTOR
18604 && it->ellipsis_p);
18605
18606 /* Save fringe bitmaps in this row. */
18607 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18608 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18609 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18610 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18611
18612 it->left_user_fringe_bitmap = 0;
18613 it->left_user_fringe_face_id = 0;
18614 it->right_user_fringe_bitmap = 0;
18615 it->right_user_fringe_face_id = 0;
18616
18617 /* Maybe set the cursor. */
18618 cvpos = it->w->cursor.vpos;
18619 if ((cvpos < 0
18620 /* In bidi-reordered rows, keep checking for proper cursor
18621 position even if one has been found already, because buffer
18622 positions in such rows change non-linearly with ROW->VPOS,
18623 when a line is continued. One exception: when we are at ZV,
18624 display cursor on the first suitable glyph row, since all
18625 the empty rows after that also have their position set to ZV. */
18626 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18627 lines' rows is implemented for bidi-reordered rows. */
18628 || (it->bidi_p
18629 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18630 && PT >= MATRIX_ROW_START_CHARPOS (row)
18631 && PT <= MATRIX_ROW_END_CHARPOS (row)
18632 && cursor_row_p (row))
18633 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18634
18635 /* Highlight trailing whitespace. */
18636 if (!NILP (Vshow_trailing_whitespace))
18637 highlight_trailing_whitespace (it->f, it->glyph_row);
18638
18639 /* Prepare for the next line. This line starts horizontally at (X
18640 HPOS) = (0 0). Vertical positions are incremented. As a
18641 convenience for the caller, IT->glyph_row is set to the next
18642 row to be used. */
18643 it->current_x = it->hpos = 0;
18644 it->current_y += row->height;
18645 SET_TEXT_POS (it->eol_pos, 0, 0);
18646 ++it->vpos;
18647 ++it->glyph_row;
18648 /* The next row should by default use the same value of the
18649 reversed_p flag as this one. set_iterator_to_next decides when
18650 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18651 the flag accordingly. */
18652 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18653 it->glyph_row->reversed_p = row->reversed_p;
18654 it->start = row->end;
18655 return row->displays_text_p;
18656
18657 #undef RECORD_MAX_MIN_POS
18658 }
18659
18660 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18661 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18662 doc: /* Return paragraph direction at point in BUFFER.
18663 Value is either `left-to-right' or `right-to-left'.
18664 If BUFFER is omitted or nil, it defaults to the current buffer.
18665
18666 Paragraph direction determines how the text in the paragraph is displayed.
18667 In left-to-right paragraphs, text begins at the left margin of the window
18668 and the reading direction is generally left to right. In right-to-left
18669 paragraphs, text begins at the right margin and is read from right to left.
18670
18671 See also `bidi-paragraph-direction'. */)
18672 (Lisp_Object buffer)
18673 {
18674 struct buffer *buf = current_buffer;
18675 struct buffer *old = buf;
18676
18677 if (! NILP (buffer))
18678 {
18679 CHECK_BUFFER (buffer);
18680 buf = XBUFFER (buffer);
18681 }
18682
18683 if (NILP (BVAR (buf, bidi_display_reordering)))
18684 return Qleft_to_right;
18685 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18686 return BVAR (buf, bidi_paragraph_direction);
18687 else
18688 {
18689 /* Determine the direction from buffer text. We could try to
18690 use current_matrix if it is up to date, but this seems fast
18691 enough as it is. */
18692 struct bidi_it itb;
18693 EMACS_INT pos = BUF_PT (buf);
18694 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18695 int c;
18696
18697 set_buffer_temp (buf);
18698 /* bidi_paragraph_init finds the base direction of the paragraph
18699 by searching forward from paragraph start. We need the base
18700 direction of the current or _previous_ paragraph, so we need
18701 to make sure we are within that paragraph. To that end, find
18702 the previous non-empty line. */
18703 if (pos >= ZV && pos > BEGV)
18704 {
18705 pos--;
18706 bytepos = CHAR_TO_BYTE (pos);
18707 }
18708 while ((c = FETCH_BYTE (bytepos)) == '\n'
18709 || c == ' ' || c == '\t' || c == '\f')
18710 {
18711 if (bytepos <= BEGV_BYTE)
18712 break;
18713 bytepos--;
18714 pos--;
18715 }
18716 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18717 bytepos--;
18718 itb.charpos = pos;
18719 itb.bytepos = bytepos;
18720 itb.nchars = -1;
18721 itb.string.s = NULL;
18722 itb.string.lstring = Qnil;
18723 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18724 itb.first_elt = 1;
18725 itb.separator_limit = -1;
18726 itb.paragraph_dir = NEUTRAL_DIR;
18727
18728 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18729 set_buffer_temp (old);
18730 switch (itb.paragraph_dir)
18731 {
18732 case L2R:
18733 return Qleft_to_right;
18734 break;
18735 case R2L:
18736 return Qright_to_left;
18737 break;
18738 default:
18739 abort ();
18740 }
18741 }
18742 }
18743
18744
18745 \f
18746 /***********************************************************************
18747 Menu Bar
18748 ***********************************************************************/
18749
18750 /* Redisplay the menu bar in the frame for window W.
18751
18752 The menu bar of X frames that don't have X toolkit support is
18753 displayed in a special window W->frame->menu_bar_window.
18754
18755 The menu bar of terminal frames is treated specially as far as
18756 glyph matrices are concerned. Menu bar lines are not part of
18757 windows, so the update is done directly on the frame matrix rows
18758 for the menu bar. */
18759
18760 static void
18761 display_menu_bar (struct window *w)
18762 {
18763 struct frame *f = XFRAME (WINDOW_FRAME (w));
18764 struct it it;
18765 Lisp_Object items;
18766 int i;
18767
18768 /* Don't do all this for graphical frames. */
18769 #ifdef HAVE_NTGUI
18770 if (FRAME_W32_P (f))
18771 return;
18772 #endif
18773 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18774 if (FRAME_X_P (f))
18775 return;
18776 #endif
18777
18778 #ifdef HAVE_NS
18779 if (FRAME_NS_P (f))
18780 return;
18781 #endif /* HAVE_NS */
18782
18783 #ifdef USE_X_TOOLKIT
18784 xassert (!FRAME_WINDOW_P (f));
18785 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18786 it.first_visible_x = 0;
18787 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18788 #else /* not USE_X_TOOLKIT */
18789 if (FRAME_WINDOW_P (f))
18790 {
18791 /* Menu bar lines are displayed in the desired matrix of the
18792 dummy window menu_bar_window. */
18793 struct window *menu_w;
18794 xassert (WINDOWP (f->menu_bar_window));
18795 menu_w = XWINDOW (f->menu_bar_window);
18796 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18797 MENU_FACE_ID);
18798 it.first_visible_x = 0;
18799 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18800 }
18801 else
18802 {
18803 /* This is a TTY frame, i.e. character hpos/vpos are used as
18804 pixel x/y. */
18805 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18806 MENU_FACE_ID);
18807 it.first_visible_x = 0;
18808 it.last_visible_x = FRAME_COLS (f);
18809 }
18810 #endif /* not USE_X_TOOLKIT */
18811
18812 /* FIXME: This should be controlled by a user option. See the
18813 comments in redisplay_tool_bar and display_mode_line about
18814 this. */
18815 it.paragraph_embedding = L2R;
18816
18817 if (! mode_line_inverse_video)
18818 /* Force the menu-bar to be displayed in the default face. */
18819 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18820
18821 /* Clear all rows of the menu bar. */
18822 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18823 {
18824 struct glyph_row *row = it.glyph_row + i;
18825 clear_glyph_row (row);
18826 row->enabled_p = 1;
18827 row->full_width_p = 1;
18828 }
18829
18830 /* Display all items of the menu bar. */
18831 items = FRAME_MENU_BAR_ITEMS (it.f);
18832 for (i = 0; i < ASIZE (items); i += 4)
18833 {
18834 Lisp_Object string;
18835
18836 /* Stop at nil string. */
18837 string = AREF (items, i + 1);
18838 if (NILP (string))
18839 break;
18840
18841 /* Remember where item was displayed. */
18842 ASET (items, i + 3, make_number (it.hpos));
18843
18844 /* Display the item, pad with one space. */
18845 if (it.current_x < it.last_visible_x)
18846 display_string (NULL, string, Qnil, 0, 0, &it,
18847 SCHARS (string) + 1, 0, 0, -1);
18848 }
18849
18850 /* Fill out the line with spaces. */
18851 if (it.current_x < it.last_visible_x)
18852 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18853
18854 /* Compute the total height of the lines. */
18855 compute_line_metrics (&it);
18856 }
18857
18858
18859 \f
18860 /***********************************************************************
18861 Mode Line
18862 ***********************************************************************/
18863
18864 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18865 FORCE is non-zero, redisplay mode lines unconditionally.
18866 Otherwise, redisplay only mode lines that are garbaged. Value is
18867 the number of windows whose mode lines were redisplayed. */
18868
18869 static int
18870 redisplay_mode_lines (Lisp_Object window, int force)
18871 {
18872 int nwindows = 0;
18873
18874 while (!NILP (window))
18875 {
18876 struct window *w = XWINDOW (window);
18877
18878 if (WINDOWP (w->hchild))
18879 nwindows += redisplay_mode_lines (w->hchild, force);
18880 else if (WINDOWP (w->vchild))
18881 nwindows += redisplay_mode_lines (w->vchild, force);
18882 else if (force
18883 || FRAME_GARBAGED_P (XFRAME (w->frame))
18884 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18885 {
18886 struct text_pos lpoint;
18887 struct buffer *old = current_buffer;
18888
18889 /* Set the window's buffer for the mode line display. */
18890 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18891 set_buffer_internal_1 (XBUFFER (w->buffer));
18892
18893 /* Point refers normally to the selected window. For any
18894 other window, set up appropriate value. */
18895 if (!EQ (window, selected_window))
18896 {
18897 struct text_pos pt;
18898
18899 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18900 if (CHARPOS (pt) < BEGV)
18901 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18902 else if (CHARPOS (pt) > (ZV - 1))
18903 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18904 else
18905 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18906 }
18907
18908 /* Display mode lines. */
18909 clear_glyph_matrix (w->desired_matrix);
18910 if (display_mode_lines (w))
18911 {
18912 ++nwindows;
18913 w->must_be_updated_p = 1;
18914 }
18915
18916 /* Restore old settings. */
18917 set_buffer_internal_1 (old);
18918 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18919 }
18920
18921 window = w->next;
18922 }
18923
18924 return nwindows;
18925 }
18926
18927
18928 /* Display the mode and/or header line of window W. Value is the
18929 sum number of mode lines and header lines displayed. */
18930
18931 static int
18932 display_mode_lines (struct window *w)
18933 {
18934 Lisp_Object old_selected_window, old_selected_frame;
18935 int n = 0;
18936
18937 old_selected_frame = selected_frame;
18938 selected_frame = w->frame;
18939 old_selected_window = selected_window;
18940 XSETWINDOW (selected_window, w);
18941
18942 /* These will be set while the mode line specs are processed. */
18943 line_number_displayed = 0;
18944 w->column_number_displayed = Qnil;
18945
18946 if (WINDOW_WANTS_MODELINE_P (w))
18947 {
18948 struct window *sel_w = XWINDOW (old_selected_window);
18949
18950 /* Select mode line face based on the real selected window. */
18951 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18952 BVAR (current_buffer, mode_line_format));
18953 ++n;
18954 }
18955
18956 if (WINDOW_WANTS_HEADER_LINE_P (w))
18957 {
18958 display_mode_line (w, HEADER_LINE_FACE_ID,
18959 BVAR (current_buffer, header_line_format));
18960 ++n;
18961 }
18962
18963 selected_frame = old_selected_frame;
18964 selected_window = old_selected_window;
18965 return n;
18966 }
18967
18968
18969 /* Display mode or header line of window W. FACE_ID specifies which
18970 line to display; it is either MODE_LINE_FACE_ID or
18971 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18972 display. Value is the pixel height of the mode/header line
18973 displayed. */
18974
18975 static int
18976 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18977 {
18978 struct it it;
18979 struct face *face;
18980 int count = SPECPDL_INDEX ();
18981
18982 init_iterator (&it, w, -1, -1, NULL, face_id);
18983 /* Don't extend on a previously drawn mode-line.
18984 This may happen if called from pos_visible_p. */
18985 it.glyph_row->enabled_p = 0;
18986 prepare_desired_row (it.glyph_row);
18987
18988 it.glyph_row->mode_line_p = 1;
18989
18990 if (! mode_line_inverse_video)
18991 /* Force the mode-line to be displayed in the default face. */
18992 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18993
18994 /* FIXME: This should be controlled by a user option. But
18995 supporting such an option is not trivial, since the mode line is
18996 made up of many separate strings. */
18997 it.paragraph_embedding = L2R;
18998
18999 record_unwind_protect (unwind_format_mode_line,
19000 format_mode_line_unwind_data (NULL, Qnil, 0));
19001
19002 mode_line_target = MODE_LINE_DISPLAY;
19003
19004 /* Temporarily make frame's keyboard the current kboard so that
19005 kboard-local variables in the mode_line_format will get the right
19006 values. */
19007 push_kboard (FRAME_KBOARD (it.f));
19008 record_unwind_save_match_data ();
19009 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19010 pop_kboard ();
19011
19012 unbind_to (count, Qnil);
19013
19014 /* Fill up with spaces. */
19015 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19016
19017 compute_line_metrics (&it);
19018 it.glyph_row->full_width_p = 1;
19019 it.glyph_row->continued_p = 0;
19020 it.glyph_row->truncated_on_left_p = 0;
19021 it.glyph_row->truncated_on_right_p = 0;
19022
19023 /* Make a 3D mode-line have a shadow at its right end. */
19024 face = FACE_FROM_ID (it.f, face_id);
19025 extend_face_to_end_of_line (&it);
19026 if (face->box != FACE_NO_BOX)
19027 {
19028 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19029 + it.glyph_row->used[TEXT_AREA] - 1);
19030 last->right_box_line_p = 1;
19031 }
19032
19033 return it.glyph_row->height;
19034 }
19035
19036 /* Move element ELT in LIST to the front of LIST.
19037 Return the updated list. */
19038
19039 static Lisp_Object
19040 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19041 {
19042 register Lisp_Object tail, prev;
19043 register Lisp_Object tem;
19044
19045 tail = list;
19046 prev = Qnil;
19047 while (CONSP (tail))
19048 {
19049 tem = XCAR (tail);
19050
19051 if (EQ (elt, tem))
19052 {
19053 /* Splice out the link TAIL. */
19054 if (NILP (prev))
19055 list = XCDR (tail);
19056 else
19057 Fsetcdr (prev, XCDR (tail));
19058
19059 /* Now make it the first. */
19060 Fsetcdr (tail, list);
19061 return tail;
19062 }
19063 else
19064 prev = tail;
19065 tail = XCDR (tail);
19066 QUIT;
19067 }
19068
19069 /* Not found--return unchanged LIST. */
19070 return list;
19071 }
19072
19073 /* Contribute ELT to the mode line for window IT->w. How it
19074 translates into text depends on its data type.
19075
19076 IT describes the display environment in which we display, as usual.
19077
19078 DEPTH is the depth in recursion. It is used to prevent
19079 infinite recursion here.
19080
19081 FIELD_WIDTH is the number of characters the display of ELT should
19082 occupy in the mode line, and PRECISION is the maximum number of
19083 characters to display from ELT's representation. See
19084 display_string for details.
19085
19086 Returns the hpos of the end of the text generated by ELT.
19087
19088 PROPS is a property list to add to any string we encounter.
19089
19090 If RISKY is nonzero, remove (disregard) any properties in any string
19091 we encounter, and ignore :eval and :propertize.
19092
19093 The global variable `mode_line_target' determines whether the
19094 output is passed to `store_mode_line_noprop',
19095 `store_mode_line_string', or `display_string'. */
19096
19097 static int
19098 display_mode_element (struct it *it, int depth, int field_width, int precision,
19099 Lisp_Object elt, Lisp_Object props, int risky)
19100 {
19101 int n = 0, field, prec;
19102 int literal = 0;
19103
19104 tail_recurse:
19105 if (depth > 100)
19106 elt = build_string ("*too-deep*");
19107
19108 depth++;
19109
19110 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19111 {
19112 case Lisp_String:
19113 {
19114 /* A string: output it and check for %-constructs within it. */
19115 unsigned char c;
19116 EMACS_INT offset = 0;
19117
19118 if (SCHARS (elt) > 0
19119 && (!NILP (props) || risky))
19120 {
19121 Lisp_Object oprops, aelt;
19122 oprops = Ftext_properties_at (make_number (0), elt);
19123
19124 /* If the starting string's properties are not what
19125 we want, translate the string. Also, if the string
19126 is risky, do that anyway. */
19127
19128 if (NILP (Fequal (props, oprops)) || risky)
19129 {
19130 /* If the starting string has properties,
19131 merge the specified ones onto the existing ones. */
19132 if (! NILP (oprops) && !risky)
19133 {
19134 Lisp_Object tem;
19135
19136 oprops = Fcopy_sequence (oprops);
19137 tem = props;
19138 while (CONSP (tem))
19139 {
19140 oprops = Fplist_put (oprops, XCAR (tem),
19141 XCAR (XCDR (tem)));
19142 tem = XCDR (XCDR (tem));
19143 }
19144 props = oprops;
19145 }
19146
19147 aelt = Fassoc (elt, mode_line_proptrans_alist);
19148 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19149 {
19150 /* AELT is what we want. Move it to the front
19151 without consing. */
19152 elt = XCAR (aelt);
19153 mode_line_proptrans_alist
19154 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19155 }
19156 else
19157 {
19158 Lisp_Object tem;
19159
19160 /* If AELT has the wrong props, it is useless.
19161 so get rid of it. */
19162 if (! NILP (aelt))
19163 mode_line_proptrans_alist
19164 = Fdelq (aelt, mode_line_proptrans_alist);
19165
19166 elt = Fcopy_sequence (elt);
19167 Fset_text_properties (make_number (0), Flength (elt),
19168 props, elt);
19169 /* Add this item to mode_line_proptrans_alist. */
19170 mode_line_proptrans_alist
19171 = Fcons (Fcons (elt, props),
19172 mode_line_proptrans_alist);
19173 /* Truncate mode_line_proptrans_alist
19174 to at most 50 elements. */
19175 tem = Fnthcdr (make_number (50),
19176 mode_line_proptrans_alist);
19177 if (! NILP (tem))
19178 XSETCDR (tem, Qnil);
19179 }
19180 }
19181 }
19182
19183 offset = 0;
19184
19185 if (literal)
19186 {
19187 prec = precision - n;
19188 switch (mode_line_target)
19189 {
19190 case MODE_LINE_NOPROP:
19191 case MODE_LINE_TITLE:
19192 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19193 break;
19194 case MODE_LINE_STRING:
19195 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19196 break;
19197 case MODE_LINE_DISPLAY:
19198 n += display_string (NULL, elt, Qnil, 0, 0, it,
19199 0, prec, 0, STRING_MULTIBYTE (elt));
19200 break;
19201 }
19202
19203 break;
19204 }
19205
19206 /* Handle the non-literal case. */
19207
19208 while ((precision <= 0 || n < precision)
19209 && SREF (elt, offset) != 0
19210 && (mode_line_target != MODE_LINE_DISPLAY
19211 || it->current_x < it->last_visible_x))
19212 {
19213 EMACS_INT last_offset = offset;
19214
19215 /* Advance to end of string or next format specifier. */
19216 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19217 ;
19218
19219 if (offset - 1 != last_offset)
19220 {
19221 EMACS_INT nchars, nbytes;
19222
19223 /* Output to end of string or up to '%'. Field width
19224 is length of string. Don't output more than
19225 PRECISION allows us. */
19226 offset--;
19227
19228 prec = c_string_width (SDATA (elt) + last_offset,
19229 offset - last_offset, precision - n,
19230 &nchars, &nbytes);
19231
19232 switch (mode_line_target)
19233 {
19234 case MODE_LINE_NOPROP:
19235 case MODE_LINE_TITLE:
19236 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19237 break;
19238 case MODE_LINE_STRING:
19239 {
19240 EMACS_INT bytepos = last_offset;
19241 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19242 EMACS_INT endpos = (precision <= 0
19243 ? string_byte_to_char (elt, offset)
19244 : charpos + nchars);
19245
19246 n += store_mode_line_string (NULL,
19247 Fsubstring (elt, make_number (charpos),
19248 make_number (endpos)),
19249 0, 0, 0, Qnil);
19250 }
19251 break;
19252 case MODE_LINE_DISPLAY:
19253 {
19254 EMACS_INT bytepos = last_offset;
19255 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19256
19257 if (precision <= 0)
19258 nchars = string_byte_to_char (elt, offset) - charpos;
19259 n += display_string (NULL, elt, Qnil, 0, charpos,
19260 it, 0, nchars, 0,
19261 STRING_MULTIBYTE (elt));
19262 }
19263 break;
19264 }
19265 }
19266 else /* c == '%' */
19267 {
19268 EMACS_INT percent_position = offset;
19269
19270 /* Get the specified minimum width. Zero means
19271 don't pad. */
19272 field = 0;
19273 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19274 field = field * 10 + c - '0';
19275
19276 /* Don't pad beyond the total padding allowed. */
19277 if (field_width - n > 0 && field > field_width - n)
19278 field = field_width - n;
19279
19280 /* Note that either PRECISION <= 0 or N < PRECISION. */
19281 prec = precision - n;
19282
19283 if (c == 'M')
19284 n += display_mode_element (it, depth, field, prec,
19285 Vglobal_mode_string, props,
19286 risky);
19287 else if (c != 0)
19288 {
19289 int multibyte;
19290 EMACS_INT bytepos, charpos;
19291 const char *spec;
19292 Lisp_Object string;
19293
19294 bytepos = percent_position;
19295 charpos = (STRING_MULTIBYTE (elt)
19296 ? string_byte_to_char (elt, bytepos)
19297 : bytepos);
19298 spec = decode_mode_spec (it->w, c, field, &string);
19299 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19300
19301 switch (mode_line_target)
19302 {
19303 case MODE_LINE_NOPROP:
19304 case MODE_LINE_TITLE:
19305 n += store_mode_line_noprop (spec, field, prec);
19306 break;
19307 case MODE_LINE_STRING:
19308 {
19309 int len = strlen (spec);
19310 Lisp_Object tem = make_string (spec, len);
19311 props = Ftext_properties_at (make_number (charpos), elt);
19312 /* Should only keep face property in props */
19313 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19314 }
19315 break;
19316 case MODE_LINE_DISPLAY:
19317 {
19318 int nglyphs_before, nwritten;
19319
19320 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19321 nwritten = display_string (spec, string, elt,
19322 charpos, 0, it,
19323 field, prec, 0,
19324 multibyte);
19325
19326 /* Assign to the glyphs written above the
19327 string where the `%x' came from, position
19328 of the `%'. */
19329 if (nwritten > 0)
19330 {
19331 struct glyph *glyph
19332 = (it->glyph_row->glyphs[TEXT_AREA]
19333 + nglyphs_before);
19334 int i;
19335
19336 for (i = 0; i < nwritten; ++i)
19337 {
19338 glyph[i].object = elt;
19339 glyph[i].charpos = charpos;
19340 }
19341
19342 n += nwritten;
19343 }
19344 }
19345 break;
19346 }
19347 }
19348 else /* c == 0 */
19349 break;
19350 }
19351 }
19352 }
19353 break;
19354
19355 case Lisp_Symbol:
19356 /* A symbol: process the value of the symbol recursively
19357 as if it appeared here directly. Avoid error if symbol void.
19358 Special case: if value of symbol is a string, output the string
19359 literally. */
19360 {
19361 register Lisp_Object tem;
19362
19363 /* If the variable is not marked as risky to set
19364 then its contents are risky to use. */
19365 if (NILP (Fget (elt, Qrisky_local_variable)))
19366 risky = 1;
19367
19368 tem = Fboundp (elt);
19369 if (!NILP (tem))
19370 {
19371 tem = Fsymbol_value (elt);
19372 /* If value is a string, output that string literally:
19373 don't check for % within it. */
19374 if (STRINGP (tem))
19375 literal = 1;
19376
19377 if (!EQ (tem, elt))
19378 {
19379 /* Give up right away for nil or t. */
19380 elt = tem;
19381 goto tail_recurse;
19382 }
19383 }
19384 }
19385 break;
19386
19387 case Lisp_Cons:
19388 {
19389 register Lisp_Object car, tem;
19390
19391 /* A cons cell: five distinct cases.
19392 If first element is :eval or :propertize, do something special.
19393 If first element is a string or a cons, process all the elements
19394 and effectively concatenate them.
19395 If first element is a negative number, truncate displaying cdr to
19396 at most that many characters. If positive, pad (with spaces)
19397 to at least that many characters.
19398 If first element is a symbol, process the cadr or caddr recursively
19399 according to whether the symbol's value is non-nil or nil. */
19400 car = XCAR (elt);
19401 if (EQ (car, QCeval))
19402 {
19403 /* An element of the form (:eval FORM) means evaluate FORM
19404 and use the result as mode line elements. */
19405
19406 if (risky)
19407 break;
19408
19409 if (CONSP (XCDR (elt)))
19410 {
19411 Lisp_Object spec;
19412 spec = safe_eval (XCAR (XCDR (elt)));
19413 n += display_mode_element (it, depth, field_width - n,
19414 precision - n, spec, props,
19415 risky);
19416 }
19417 }
19418 else if (EQ (car, QCpropertize))
19419 {
19420 /* An element of the form (:propertize ELT PROPS...)
19421 means display ELT but applying properties PROPS. */
19422
19423 if (risky)
19424 break;
19425
19426 if (CONSP (XCDR (elt)))
19427 n += display_mode_element (it, depth, field_width - n,
19428 precision - n, XCAR (XCDR (elt)),
19429 XCDR (XCDR (elt)), risky);
19430 }
19431 else if (SYMBOLP (car))
19432 {
19433 tem = Fboundp (car);
19434 elt = XCDR (elt);
19435 if (!CONSP (elt))
19436 goto invalid;
19437 /* elt is now the cdr, and we know it is a cons cell.
19438 Use its car if CAR has a non-nil value. */
19439 if (!NILP (tem))
19440 {
19441 tem = Fsymbol_value (car);
19442 if (!NILP (tem))
19443 {
19444 elt = XCAR (elt);
19445 goto tail_recurse;
19446 }
19447 }
19448 /* Symbol's value is nil (or symbol is unbound)
19449 Get the cddr of the original list
19450 and if possible find the caddr and use that. */
19451 elt = XCDR (elt);
19452 if (NILP (elt))
19453 break;
19454 else if (!CONSP (elt))
19455 goto invalid;
19456 elt = XCAR (elt);
19457 goto tail_recurse;
19458 }
19459 else if (INTEGERP (car))
19460 {
19461 register int lim = XINT (car);
19462 elt = XCDR (elt);
19463 if (lim < 0)
19464 {
19465 /* Negative int means reduce maximum width. */
19466 if (precision <= 0)
19467 precision = -lim;
19468 else
19469 precision = min (precision, -lim);
19470 }
19471 else if (lim > 0)
19472 {
19473 /* Padding specified. Don't let it be more than
19474 current maximum. */
19475 if (precision > 0)
19476 lim = min (precision, lim);
19477
19478 /* If that's more padding than already wanted, queue it.
19479 But don't reduce padding already specified even if
19480 that is beyond the current truncation point. */
19481 field_width = max (lim, field_width);
19482 }
19483 goto tail_recurse;
19484 }
19485 else if (STRINGP (car) || CONSP (car))
19486 {
19487 Lisp_Object halftail = elt;
19488 int len = 0;
19489
19490 while (CONSP (elt)
19491 && (precision <= 0 || n < precision))
19492 {
19493 n += display_mode_element (it, depth,
19494 /* Do padding only after the last
19495 element in the list. */
19496 (! CONSP (XCDR (elt))
19497 ? field_width - n
19498 : 0),
19499 precision - n, XCAR (elt),
19500 props, risky);
19501 elt = XCDR (elt);
19502 len++;
19503 if ((len & 1) == 0)
19504 halftail = XCDR (halftail);
19505 /* Check for cycle. */
19506 if (EQ (halftail, elt))
19507 break;
19508 }
19509 }
19510 }
19511 break;
19512
19513 default:
19514 invalid:
19515 elt = build_string ("*invalid*");
19516 goto tail_recurse;
19517 }
19518
19519 /* Pad to FIELD_WIDTH. */
19520 if (field_width > 0 && n < field_width)
19521 {
19522 switch (mode_line_target)
19523 {
19524 case MODE_LINE_NOPROP:
19525 case MODE_LINE_TITLE:
19526 n += store_mode_line_noprop ("", field_width - n, 0);
19527 break;
19528 case MODE_LINE_STRING:
19529 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19530 break;
19531 case MODE_LINE_DISPLAY:
19532 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19533 0, 0, 0);
19534 break;
19535 }
19536 }
19537
19538 return n;
19539 }
19540
19541 /* Store a mode-line string element in mode_line_string_list.
19542
19543 If STRING is non-null, display that C string. Otherwise, the Lisp
19544 string LISP_STRING is displayed.
19545
19546 FIELD_WIDTH is the minimum number of output glyphs to produce.
19547 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19548 with spaces. FIELD_WIDTH <= 0 means don't pad.
19549
19550 PRECISION is the maximum number of characters to output from
19551 STRING. PRECISION <= 0 means don't truncate the string.
19552
19553 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19554 properties to the string.
19555
19556 PROPS are the properties to add to the string.
19557 The mode_line_string_face face property is always added to the string.
19558 */
19559
19560 static int
19561 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19562 int field_width, int precision, Lisp_Object props)
19563 {
19564 EMACS_INT len;
19565 int n = 0;
19566
19567 if (string != NULL)
19568 {
19569 len = strlen (string);
19570 if (precision > 0 && len > precision)
19571 len = precision;
19572 lisp_string = make_string (string, len);
19573 if (NILP (props))
19574 props = mode_line_string_face_prop;
19575 else if (!NILP (mode_line_string_face))
19576 {
19577 Lisp_Object face = Fplist_get (props, Qface);
19578 props = Fcopy_sequence (props);
19579 if (NILP (face))
19580 face = mode_line_string_face;
19581 else
19582 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19583 props = Fplist_put (props, Qface, face);
19584 }
19585 Fadd_text_properties (make_number (0), make_number (len),
19586 props, lisp_string);
19587 }
19588 else
19589 {
19590 len = XFASTINT (Flength (lisp_string));
19591 if (precision > 0 && len > precision)
19592 {
19593 len = precision;
19594 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19595 precision = -1;
19596 }
19597 if (!NILP (mode_line_string_face))
19598 {
19599 Lisp_Object face;
19600 if (NILP (props))
19601 props = Ftext_properties_at (make_number (0), lisp_string);
19602 face = Fplist_get (props, Qface);
19603 if (NILP (face))
19604 face = mode_line_string_face;
19605 else
19606 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19607 props = Fcons (Qface, Fcons (face, Qnil));
19608 if (copy_string)
19609 lisp_string = Fcopy_sequence (lisp_string);
19610 }
19611 if (!NILP (props))
19612 Fadd_text_properties (make_number (0), make_number (len),
19613 props, lisp_string);
19614 }
19615
19616 if (len > 0)
19617 {
19618 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19619 n += len;
19620 }
19621
19622 if (field_width > len)
19623 {
19624 field_width -= len;
19625 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19626 if (!NILP (props))
19627 Fadd_text_properties (make_number (0), make_number (field_width),
19628 props, lisp_string);
19629 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19630 n += field_width;
19631 }
19632
19633 return n;
19634 }
19635
19636
19637 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19638 1, 4, 0,
19639 doc: /* Format a string out of a mode line format specification.
19640 First arg FORMAT specifies the mode line format (see `mode-line-format'
19641 for details) to use.
19642
19643 By default, the format is evaluated for the currently selected window.
19644
19645 Optional second arg FACE specifies the face property to put on all
19646 characters for which no face is specified. The value nil means the
19647 default face. The value t means whatever face the window's mode line
19648 currently uses (either `mode-line' or `mode-line-inactive',
19649 depending on whether the window is the selected window or not).
19650 An integer value means the value string has no text
19651 properties.
19652
19653 Optional third and fourth args WINDOW and BUFFER specify the window
19654 and buffer to use as the context for the formatting (defaults
19655 are the selected window and the WINDOW's buffer). */)
19656 (Lisp_Object format, Lisp_Object face,
19657 Lisp_Object window, Lisp_Object buffer)
19658 {
19659 struct it it;
19660 int len;
19661 struct window *w;
19662 struct buffer *old_buffer = NULL;
19663 int face_id;
19664 int no_props = INTEGERP (face);
19665 int count = SPECPDL_INDEX ();
19666 Lisp_Object str;
19667 int string_start = 0;
19668
19669 if (NILP (window))
19670 window = selected_window;
19671 CHECK_WINDOW (window);
19672 w = XWINDOW (window);
19673
19674 if (NILP (buffer))
19675 buffer = w->buffer;
19676 CHECK_BUFFER (buffer);
19677
19678 /* Make formatting the modeline a non-op when noninteractive, otherwise
19679 there will be problems later caused by a partially initialized frame. */
19680 if (NILP (format) || noninteractive)
19681 return empty_unibyte_string;
19682
19683 if (no_props)
19684 face = Qnil;
19685
19686 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19687 : EQ (face, Qt) ? (EQ (window, selected_window)
19688 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19689 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19690 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19691 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19692 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19693 : DEFAULT_FACE_ID;
19694
19695 if (XBUFFER (buffer) != current_buffer)
19696 old_buffer = current_buffer;
19697
19698 /* Save things including mode_line_proptrans_alist,
19699 and set that to nil so that we don't alter the outer value. */
19700 record_unwind_protect (unwind_format_mode_line,
19701 format_mode_line_unwind_data
19702 (old_buffer, selected_window, 1));
19703 mode_line_proptrans_alist = Qnil;
19704
19705 Fselect_window (window, Qt);
19706 if (old_buffer)
19707 set_buffer_internal_1 (XBUFFER (buffer));
19708
19709 init_iterator (&it, w, -1, -1, NULL, face_id);
19710
19711 if (no_props)
19712 {
19713 mode_line_target = MODE_LINE_NOPROP;
19714 mode_line_string_face_prop = Qnil;
19715 mode_line_string_list = Qnil;
19716 string_start = MODE_LINE_NOPROP_LEN (0);
19717 }
19718 else
19719 {
19720 mode_line_target = MODE_LINE_STRING;
19721 mode_line_string_list = Qnil;
19722 mode_line_string_face = face;
19723 mode_line_string_face_prop
19724 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19725 }
19726
19727 push_kboard (FRAME_KBOARD (it.f));
19728 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19729 pop_kboard ();
19730
19731 if (no_props)
19732 {
19733 len = MODE_LINE_NOPROP_LEN (string_start);
19734 str = make_string (mode_line_noprop_buf + string_start, len);
19735 }
19736 else
19737 {
19738 mode_line_string_list = Fnreverse (mode_line_string_list);
19739 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19740 empty_unibyte_string);
19741 }
19742
19743 unbind_to (count, Qnil);
19744 return str;
19745 }
19746
19747 /* Write a null-terminated, right justified decimal representation of
19748 the positive integer D to BUF using a minimal field width WIDTH. */
19749
19750 static void
19751 pint2str (register char *buf, register int width, register EMACS_INT d)
19752 {
19753 register char *p = buf;
19754
19755 if (d <= 0)
19756 *p++ = '0';
19757 else
19758 {
19759 while (d > 0)
19760 {
19761 *p++ = d % 10 + '0';
19762 d /= 10;
19763 }
19764 }
19765
19766 for (width -= (int) (p - buf); width > 0; --width)
19767 *p++ = ' ';
19768 *p-- = '\0';
19769 while (p > buf)
19770 {
19771 d = *buf;
19772 *buf++ = *p;
19773 *p-- = d;
19774 }
19775 }
19776
19777 /* Write a null-terminated, right justified decimal and "human
19778 readable" representation of the nonnegative integer D to BUF using
19779 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19780
19781 static const char power_letter[] =
19782 {
19783 0, /* no letter */
19784 'k', /* kilo */
19785 'M', /* mega */
19786 'G', /* giga */
19787 'T', /* tera */
19788 'P', /* peta */
19789 'E', /* exa */
19790 'Z', /* zetta */
19791 'Y' /* yotta */
19792 };
19793
19794 static void
19795 pint2hrstr (char *buf, int width, EMACS_INT d)
19796 {
19797 /* We aim to represent the nonnegative integer D as
19798 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19799 EMACS_INT quotient = d;
19800 int remainder = 0;
19801 /* -1 means: do not use TENTHS. */
19802 int tenths = -1;
19803 int exponent = 0;
19804
19805 /* Length of QUOTIENT.TENTHS as a string. */
19806 int length;
19807
19808 char * psuffix;
19809 char * p;
19810
19811 if (1000 <= quotient)
19812 {
19813 /* Scale to the appropriate EXPONENT. */
19814 do
19815 {
19816 remainder = quotient % 1000;
19817 quotient /= 1000;
19818 exponent++;
19819 }
19820 while (1000 <= quotient);
19821
19822 /* Round to nearest and decide whether to use TENTHS or not. */
19823 if (quotient <= 9)
19824 {
19825 tenths = remainder / 100;
19826 if (50 <= remainder % 100)
19827 {
19828 if (tenths < 9)
19829 tenths++;
19830 else
19831 {
19832 quotient++;
19833 if (quotient == 10)
19834 tenths = -1;
19835 else
19836 tenths = 0;
19837 }
19838 }
19839 }
19840 else
19841 if (500 <= remainder)
19842 {
19843 if (quotient < 999)
19844 quotient++;
19845 else
19846 {
19847 quotient = 1;
19848 exponent++;
19849 tenths = 0;
19850 }
19851 }
19852 }
19853
19854 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19855 if (tenths == -1 && quotient <= 99)
19856 if (quotient <= 9)
19857 length = 1;
19858 else
19859 length = 2;
19860 else
19861 length = 3;
19862 p = psuffix = buf + max (width, length);
19863
19864 /* Print EXPONENT. */
19865 *psuffix++ = power_letter[exponent];
19866 *psuffix = '\0';
19867
19868 /* Print TENTHS. */
19869 if (tenths >= 0)
19870 {
19871 *--p = '0' + tenths;
19872 *--p = '.';
19873 }
19874
19875 /* Print QUOTIENT. */
19876 do
19877 {
19878 int digit = quotient % 10;
19879 *--p = '0' + digit;
19880 }
19881 while ((quotient /= 10) != 0);
19882
19883 /* Print leading spaces. */
19884 while (buf < p)
19885 *--p = ' ';
19886 }
19887
19888 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19889 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19890 type of CODING_SYSTEM. Return updated pointer into BUF. */
19891
19892 static unsigned char invalid_eol_type[] = "(*invalid*)";
19893
19894 static char *
19895 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19896 {
19897 Lisp_Object val;
19898 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19899 const unsigned char *eol_str;
19900 int eol_str_len;
19901 /* The EOL conversion we are using. */
19902 Lisp_Object eoltype;
19903
19904 val = CODING_SYSTEM_SPEC (coding_system);
19905 eoltype = Qnil;
19906
19907 if (!VECTORP (val)) /* Not yet decided. */
19908 {
19909 if (multibyte)
19910 *buf++ = '-';
19911 if (eol_flag)
19912 eoltype = eol_mnemonic_undecided;
19913 /* Don't mention EOL conversion if it isn't decided. */
19914 }
19915 else
19916 {
19917 Lisp_Object attrs;
19918 Lisp_Object eolvalue;
19919
19920 attrs = AREF (val, 0);
19921 eolvalue = AREF (val, 2);
19922
19923 if (multibyte)
19924 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19925
19926 if (eol_flag)
19927 {
19928 /* The EOL conversion that is normal on this system. */
19929
19930 if (NILP (eolvalue)) /* Not yet decided. */
19931 eoltype = eol_mnemonic_undecided;
19932 else if (VECTORP (eolvalue)) /* Not yet decided. */
19933 eoltype = eol_mnemonic_undecided;
19934 else /* eolvalue is Qunix, Qdos, or Qmac. */
19935 eoltype = (EQ (eolvalue, Qunix)
19936 ? eol_mnemonic_unix
19937 : (EQ (eolvalue, Qdos) == 1
19938 ? eol_mnemonic_dos : eol_mnemonic_mac));
19939 }
19940 }
19941
19942 if (eol_flag)
19943 {
19944 /* Mention the EOL conversion if it is not the usual one. */
19945 if (STRINGP (eoltype))
19946 {
19947 eol_str = SDATA (eoltype);
19948 eol_str_len = SBYTES (eoltype);
19949 }
19950 else if (CHARACTERP (eoltype))
19951 {
19952 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19953 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19954 eol_str = tmp;
19955 }
19956 else
19957 {
19958 eol_str = invalid_eol_type;
19959 eol_str_len = sizeof (invalid_eol_type) - 1;
19960 }
19961 memcpy (buf, eol_str, eol_str_len);
19962 buf += eol_str_len;
19963 }
19964
19965 return buf;
19966 }
19967
19968 /* Return a string for the output of a mode line %-spec for window W,
19969 generated by character C. FIELD_WIDTH > 0 means pad the string
19970 returned with spaces to that value. Return a Lisp string in
19971 *STRING if the resulting string is taken from that Lisp string.
19972
19973 Note we operate on the current buffer for most purposes,
19974 the exception being w->base_line_pos. */
19975
19976 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19977
19978 static const char *
19979 decode_mode_spec (struct window *w, register int c, int field_width,
19980 Lisp_Object *string)
19981 {
19982 Lisp_Object obj;
19983 struct frame *f = XFRAME (WINDOW_FRAME (w));
19984 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19985 struct buffer *b = current_buffer;
19986
19987 obj = Qnil;
19988 *string = Qnil;
19989
19990 switch (c)
19991 {
19992 case '*':
19993 if (!NILP (BVAR (b, read_only)))
19994 return "%";
19995 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19996 return "*";
19997 return "-";
19998
19999 case '+':
20000 /* This differs from %* only for a modified read-only buffer. */
20001 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20002 return "*";
20003 if (!NILP (BVAR (b, read_only)))
20004 return "%";
20005 return "-";
20006
20007 case '&':
20008 /* This differs from %* in ignoring read-only-ness. */
20009 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20010 return "*";
20011 return "-";
20012
20013 case '%':
20014 return "%";
20015
20016 case '[':
20017 {
20018 int i;
20019 char *p;
20020
20021 if (command_loop_level > 5)
20022 return "[[[... ";
20023 p = decode_mode_spec_buf;
20024 for (i = 0; i < command_loop_level; i++)
20025 *p++ = '[';
20026 *p = 0;
20027 return decode_mode_spec_buf;
20028 }
20029
20030 case ']':
20031 {
20032 int i;
20033 char *p;
20034
20035 if (command_loop_level > 5)
20036 return " ...]]]";
20037 p = decode_mode_spec_buf;
20038 for (i = 0; i < command_loop_level; i++)
20039 *p++ = ']';
20040 *p = 0;
20041 return decode_mode_spec_buf;
20042 }
20043
20044 case '-':
20045 {
20046 register int i;
20047
20048 /* Let lots_of_dashes be a string of infinite length. */
20049 if (mode_line_target == MODE_LINE_NOPROP ||
20050 mode_line_target == MODE_LINE_STRING)
20051 return "--";
20052 if (field_width <= 0
20053 || field_width > sizeof (lots_of_dashes))
20054 {
20055 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20056 decode_mode_spec_buf[i] = '-';
20057 decode_mode_spec_buf[i] = '\0';
20058 return decode_mode_spec_buf;
20059 }
20060 else
20061 return lots_of_dashes;
20062 }
20063
20064 case 'b':
20065 obj = BVAR (b, name);
20066 break;
20067
20068 case 'c':
20069 /* %c and %l are ignored in `frame-title-format'.
20070 (In redisplay_internal, the frame title is drawn _before_ the
20071 windows are updated, so the stuff which depends on actual
20072 window contents (such as %l) may fail to render properly, or
20073 even crash emacs.) */
20074 if (mode_line_target == MODE_LINE_TITLE)
20075 return "";
20076 else
20077 {
20078 EMACS_INT col = current_column ();
20079 w->column_number_displayed = make_number (col);
20080 pint2str (decode_mode_spec_buf, field_width, col);
20081 return decode_mode_spec_buf;
20082 }
20083
20084 case 'e':
20085 #ifndef SYSTEM_MALLOC
20086 {
20087 if (NILP (Vmemory_full))
20088 return "";
20089 else
20090 return "!MEM FULL! ";
20091 }
20092 #else
20093 return "";
20094 #endif
20095
20096 case 'F':
20097 /* %F displays the frame name. */
20098 if (!NILP (f->title))
20099 return SSDATA (f->title);
20100 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20101 return SSDATA (f->name);
20102 return "Emacs";
20103
20104 case 'f':
20105 obj = BVAR (b, filename);
20106 break;
20107
20108 case 'i':
20109 {
20110 EMACS_INT size = ZV - BEGV;
20111 pint2str (decode_mode_spec_buf, field_width, size);
20112 return decode_mode_spec_buf;
20113 }
20114
20115 case 'I':
20116 {
20117 EMACS_INT size = ZV - BEGV;
20118 pint2hrstr (decode_mode_spec_buf, field_width, size);
20119 return decode_mode_spec_buf;
20120 }
20121
20122 case 'l':
20123 {
20124 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20125 EMACS_INT topline, nlines, height;
20126 EMACS_INT junk;
20127
20128 /* %c and %l are ignored in `frame-title-format'. */
20129 if (mode_line_target == MODE_LINE_TITLE)
20130 return "";
20131
20132 startpos = XMARKER (w->start)->charpos;
20133 startpos_byte = marker_byte_position (w->start);
20134 height = WINDOW_TOTAL_LINES (w);
20135
20136 /* If we decided that this buffer isn't suitable for line numbers,
20137 don't forget that too fast. */
20138 if (EQ (w->base_line_pos, w->buffer))
20139 goto no_value;
20140 /* But do forget it, if the window shows a different buffer now. */
20141 else if (BUFFERP (w->base_line_pos))
20142 w->base_line_pos = Qnil;
20143
20144 /* If the buffer is very big, don't waste time. */
20145 if (INTEGERP (Vline_number_display_limit)
20146 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20147 {
20148 w->base_line_pos = Qnil;
20149 w->base_line_number = Qnil;
20150 goto no_value;
20151 }
20152
20153 if (INTEGERP (w->base_line_number)
20154 && INTEGERP (w->base_line_pos)
20155 && XFASTINT (w->base_line_pos) <= startpos)
20156 {
20157 line = XFASTINT (w->base_line_number);
20158 linepos = XFASTINT (w->base_line_pos);
20159 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20160 }
20161 else
20162 {
20163 line = 1;
20164 linepos = BUF_BEGV (b);
20165 linepos_byte = BUF_BEGV_BYTE (b);
20166 }
20167
20168 /* Count lines from base line to window start position. */
20169 nlines = display_count_lines (linepos_byte,
20170 startpos_byte,
20171 startpos, &junk);
20172
20173 topline = nlines + line;
20174
20175 /* Determine a new base line, if the old one is too close
20176 or too far away, or if we did not have one.
20177 "Too close" means it's plausible a scroll-down would
20178 go back past it. */
20179 if (startpos == BUF_BEGV (b))
20180 {
20181 w->base_line_number = make_number (topline);
20182 w->base_line_pos = make_number (BUF_BEGV (b));
20183 }
20184 else if (nlines < height + 25 || nlines > height * 3 + 50
20185 || linepos == BUF_BEGV (b))
20186 {
20187 EMACS_INT limit = BUF_BEGV (b);
20188 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20189 EMACS_INT position;
20190 EMACS_INT distance =
20191 (height * 2 + 30) * line_number_display_limit_width;
20192
20193 if (startpos - distance > limit)
20194 {
20195 limit = startpos - distance;
20196 limit_byte = CHAR_TO_BYTE (limit);
20197 }
20198
20199 nlines = display_count_lines (startpos_byte,
20200 limit_byte,
20201 - (height * 2 + 30),
20202 &position);
20203 /* If we couldn't find the lines we wanted within
20204 line_number_display_limit_width chars per line,
20205 give up on line numbers for this window. */
20206 if (position == limit_byte && limit == startpos - distance)
20207 {
20208 w->base_line_pos = w->buffer;
20209 w->base_line_number = Qnil;
20210 goto no_value;
20211 }
20212
20213 w->base_line_number = make_number (topline - nlines);
20214 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20215 }
20216
20217 /* Now count lines from the start pos to point. */
20218 nlines = display_count_lines (startpos_byte,
20219 PT_BYTE, PT, &junk);
20220
20221 /* Record that we did display the line number. */
20222 line_number_displayed = 1;
20223
20224 /* Make the string to show. */
20225 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20226 return decode_mode_spec_buf;
20227 no_value:
20228 {
20229 char* p = decode_mode_spec_buf;
20230 int pad = field_width - 2;
20231 while (pad-- > 0)
20232 *p++ = ' ';
20233 *p++ = '?';
20234 *p++ = '?';
20235 *p = '\0';
20236 return decode_mode_spec_buf;
20237 }
20238 }
20239 break;
20240
20241 case 'm':
20242 obj = BVAR (b, mode_name);
20243 break;
20244
20245 case 'n':
20246 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20247 return " Narrow";
20248 break;
20249
20250 case 'p':
20251 {
20252 EMACS_INT pos = marker_position (w->start);
20253 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20254
20255 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20256 {
20257 if (pos <= BUF_BEGV (b))
20258 return "All";
20259 else
20260 return "Bottom";
20261 }
20262 else if (pos <= BUF_BEGV (b))
20263 return "Top";
20264 else
20265 {
20266 if (total > 1000000)
20267 /* Do it differently for a large value, to avoid overflow. */
20268 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20269 else
20270 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20271 /* We can't normally display a 3-digit number,
20272 so get us a 2-digit number that is close. */
20273 if (total == 100)
20274 total = 99;
20275 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20276 return decode_mode_spec_buf;
20277 }
20278 }
20279
20280 /* Display percentage of size above the bottom of the screen. */
20281 case 'P':
20282 {
20283 EMACS_INT toppos = marker_position (w->start);
20284 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20285 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20286
20287 if (botpos >= BUF_ZV (b))
20288 {
20289 if (toppos <= BUF_BEGV (b))
20290 return "All";
20291 else
20292 return "Bottom";
20293 }
20294 else
20295 {
20296 if (total > 1000000)
20297 /* Do it differently for a large value, to avoid overflow. */
20298 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20299 else
20300 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20301 /* We can't normally display a 3-digit number,
20302 so get us a 2-digit number that is close. */
20303 if (total == 100)
20304 total = 99;
20305 if (toppos <= BUF_BEGV (b))
20306 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20307 else
20308 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20309 return decode_mode_spec_buf;
20310 }
20311 }
20312
20313 case 's':
20314 /* status of process */
20315 obj = Fget_buffer_process (Fcurrent_buffer ());
20316 if (NILP (obj))
20317 return "no process";
20318 #ifndef MSDOS
20319 obj = Fsymbol_name (Fprocess_status (obj));
20320 #endif
20321 break;
20322
20323 case '@':
20324 {
20325 int count = inhibit_garbage_collection ();
20326 Lisp_Object val = call1 (intern ("file-remote-p"),
20327 BVAR (current_buffer, directory));
20328 unbind_to (count, Qnil);
20329
20330 if (NILP (val))
20331 return "-";
20332 else
20333 return "@";
20334 }
20335
20336 case 't': /* indicate TEXT or BINARY */
20337 return "T";
20338
20339 case 'z':
20340 /* coding-system (not including end-of-line format) */
20341 case 'Z':
20342 /* coding-system (including end-of-line type) */
20343 {
20344 int eol_flag = (c == 'Z');
20345 char *p = decode_mode_spec_buf;
20346
20347 if (! FRAME_WINDOW_P (f))
20348 {
20349 /* No need to mention EOL here--the terminal never needs
20350 to do EOL conversion. */
20351 p = decode_mode_spec_coding (CODING_ID_NAME
20352 (FRAME_KEYBOARD_CODING (f)->id),
20353 p, 0);
20354 p = decode_mode_spec_coding (CODING_ID_NAME
20355 (FRAME_TERMINAL_CODING (f)->id),
20356 p, 0);
20357 }
20358 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20359 p, eol_flag);
20360
20361 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20362 #ifdef subprocesses
20363 obj = Fget_buffer_process (Fcurrent_buffer ());
20364 if (PROCESSP (obj))
20365 {
20366 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20367 p, eol_flag);
20368 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20369 p, eol_flag);
20370 }
20371 #endif /* subprocesses */
20372 #endif /* 0 */
20373 *p = 0;
20374 return decode_mode_spec_buf;
20375 }
20376 }
20377
20378 if (STRINGP (obj))
20379 {
20380 *string = obj;
20381 return SSDATA (obj);
20382 }
20383 else
20384 return "";
20385 }
20386
20387
20388 /* Count up to COUNT lines starting from START_BYTE.
20389 But don't go beyond LIMIT_BYTE.
20390 Return the number of lines thus found (always nonnegative).
20391
20392 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20393
20394 static EMACS_INT
20395 display_count_lines (EMACS_INT start_byte,
20396 EMACS_INT limit_byte, EMACS_INT count,
20397 EMACS_INT *byte_pos_ptr)
20398 {
20399 register unsigned char *cursor;
20400 unsigned char *base;
20401
20402 register EMACS_INT ceiling;
20403 register unsigned char *ceiling_addr;
20404 EMACS_INT orig_count = count;
20405
20406 /* If we are not in selective display mode,
20407 check only for newlines. */
20408 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20409 && !INTEGERP (BVAR (current_buffer, selective_display)));
20410
20411 if (count > 0)
20412 {
20413 while (start_byte < limit_byte)
20414 {
20415 ceiling = BUFFER_CEILING_OF (start_byte);
20416 ceiling = min (limit_byte - 1, ceiling);
20417 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20418 base = (cursor = BYTE_POS_ADDR (start_byte));
20419 while (1)
20420 {
20421 if (selective_display)
20422 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20423 ;
20424 else
20425 while (*cursor != '\n' && ++cursor != ceiling_addr)
20426 ;
20427
20428 if (cursor != ceiling_addr)
20429 {
20430 if (--count == 0)
20431 {
20432 start_byte += cursor - base + 1;
20433 *byte_pos_ptr = start_byte;
20434 return orig_count;
20435 }
20436 else
20437 if (++cursor == ceiling_addr)
20438 break;
20439 }
20440 else
20441 break;
20442 }
20443 start_byte += cursor - base;
20444 }
20445 }
20446 else
20447 {
20448 while (start_byte > limit_byte)
20449 {
20450 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20451 ceiling = max (limit_byte, ceiling);
20452 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20453 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20454 while (1)
20455 {
20456 if (selective_display)
20457 while (--cursor != ceiling_addr
20458 && *cursor != '\n' && *cursor != 015)
20459 ;
20460 else
20461 while (--cursor != ceiling_addr && *cursor != '\n')
20462 ;
20463
20464 if (cursor != ceiling_addr)
20465 {
20466 if (++count == 0)
20467 {
20468 start_byte += cursor - base + 1;
20469 *byte_pos_ptr = start_byte;
20470 /* When scanning backwards, we should
20471 not count the newline posterior to which we stop. */
20472 return - orig_count - 1;
20473 }
20474 }
20475 else
20476 break;
20477 }
20478 /* Here we add 1 to compensate for the last decrement
20479 of CURSOR, which took it past the valid range. */
20480 start_byte += cursor - base + 1;
20481 }
20482 }
20483
20484 *byte_pos_ptr = limit_byte;
20485
20486 if (count < 0)
20487 return - orig_count + count;
20488 return orig_count - count;
20489
20490 }
20491
20492
20493 \f
20494 /***********************************************************************
20495 Displaying strings
20496 ***********************************************************************/
20497
20498 /* Display a NUL-terminated string, starting with index START.
20499
20500 If STRING is non-null, display that C string. Otherwise, the Lisp
20501 string LISP_STRING is displayed. There's a case that STRING is
20502 non-null and LISP_STRING is not nil. It means STRING is a string
20503 data of LISP_STRING. In that case, we display LISP_STRING while
20504 ignoring its text properties.
20505
20506 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20507 FACE_STRING. Display STRING or LISP_STRING with the face at
20508 FACE_STRING_POS in FACE_STRING:
20509
20510 Display the string in the environment given by IT, but use the
20511 standard display table, temporarily.
20512
20513 FIELD_WIDTH is the minimum number of output glyphs to produce.
20514 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20515 with spaces. If STRING has more characters, more than FIELD_WIDTH
20516 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20517
20518 PRECISION is the maximum number of characters to output from
20519 STRING. PRECISION < 0 means don't truncate the string.
20520
20521 This is roughly equivalent to printf format specifiers:
20522
20523 FIELD_WIDTH PRECISION PRINTF
20524 ----------------------------------------
20525 -1 -1 %s
20526 -1 10 %.10s
20527 10 -1 %10s
20528 20 10 %20.10s
20529
20530 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20531 display them, and < 0 means obey the current buffer's value of
20532 enable_multibyte_characters.
20533
20534 Value is the number of columns displayed. */
20535
20536 static int
20537 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20538 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20539 int field_width, int precision, int max_x, int multibyte)
20540 {
20541 int hpos_at_start = it->hpos;
20542 int saved_face_id = it->face_id;
20543 struct glyph_row *row = it->glyph_row;
20544 EMACS_INT it_charpos;
20545
20546 /* Initialize the iterator IT for iteration over STRING beginning
20547 with index START. */
20548 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20549 precision, field_width, multibyte);
20550 if (string && STRINGP (lisp_string))
20551 /* LISP_STRING is the one returned by decode_mode_spec. We should
20552 ignore its text properties. */
20553 it->stop_charpos = it->end_charpos;
20554
20555 /* If displaying STRING, set up the face of the iterator from
20556 FACE_STRING, if that's given. */
20557 if (STRINGP (face_string))
20558 {
20559 EMACS_INT endptr;
20560 struct face *face;
20561
20562 it->face_id
20563 = face_at_string_position (it->w, face_string, face_string_pos,
20564 0, it->region_beg_charpos,
20565 it->region_end_charpos,
20566 &endptr, it->base_face_id, 0);
20567 face = FACE_FROM_ID (it->f, it->face_id);
20568 it->face_box_p = face->box != FACE_NO_BOX;
20569 }
20570
20571 /* Set max_x to the maximum allowed X position. Don't let it go
20572 beyond the right edge of the window. */
20573 if (max_x <= 0)
20574 max_x = it->last_visible_x;
20575 else
20576 max_x = min (max_x, it->last_visible_x);
20577
20578 /* Skip over display elements that are not visible. because IT->w is
20579 hscrolled. */
20580 if (it->current_x < it->first_visible_x)
20581 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20582 MOVE_TO_POS | MOVE_TO_X);
20583
20584 row->ascent = it->max_ascent;
20585 row->height = it->max_ascent + it->max_descent;
20586 row->phys_ascent = it->max_phys_ascent;
20587 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20588 row->extra_line_spacing = it->max_extra_line_spacing;
20589
20590 if (STRINGP (it->string))
20591 it_charpos = IT_STRING_CHARPOS (*it);
20592 else
20593 it_charpos = IT_CHARPOS (*it);
20594
20595 /* This condition is for the case that we are called with current_x
20596 past last_visible_x. */
20597 while (it->current_x < max_x)
20598 {
20599 int x_before, x, n_glyphs_before, i, nglyphs;
20600
20601 /* Get the next display element. */
20602 if (!get_next_display_element (it))
20603 break;
20604
20605 /* Produce glyphs. */
20606 x_before = it->current_x;
20607 n_glyphs_before = row->used[TEXT_AREA];
20608 PRODUCE_GLYPHS (it);
20609
20610 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20611 i = 0;
20612 x = x_before;
20613 while (i < nglyphs)
20614 {
20615 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20616
20617 if (it->line_wrap != TRUNCATE
20618 && x + glyph->pixel_width > max_x)
20619 {
20620 /* End of continued line or max_x reached. */
20621 if (CHAR_GLYPH_PADDING_P (*glyph))
20622 {
20623 /* A wide character is unbreakable. */
20624 if (row->reversed_p)
20625 unproduce_glyphs (it, row->used[TEXT_AREA]
20626 - n_glyphs_before);
20627 row->used[TEXT_AREA] = n_glyphs_before;
20628 it->current_x = x_before;
20629 }
20630 else
20631 {
20632 if (row->reversed_p)
20633 unproduce_glyphs (it, row->used[TEXT_AREA]
20634 - (n_glyphs_before + i));
20635 row->used[TEXT_AREA] = n_glyphs_before + i;
20636 it->current_x = x;
20637 }
20638 break;
20639 }
20640 else if (x + glyph->pixel_width >= it->first_visible_x)
20641 {
20642 /* Glyph is at least partially visible. */
20643 ++it->hpos;
20644 if (x < it->first_visible_x)
20645 row->x = x - it->first_visible_x;
20646 }
20647 else
20648 {
20649 /* Glyph is off the left margin of the display area.
20650 Should not happen. */
20651 abort ();
20652 }
20653
20654 row->ascent = max (row->ascent, it->max_ascent);
20655 row->height = max (row->height, it->max_ascent + it->max_descent);
20656 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20657 row->phys_height = max (row->phys_height,
20658 it->max_phys_ascent + it->max_phys_descent);
20659 row->extra_line_spacing = max (row->extra_line_spacing,
20660 it->max_extra_line_spacing);
20661 x += glyph->pixel_width;
20662 ++i;
20663 }
20664
20665 /* Stop if max_x reached. */
20666 if (i < nglyphs)
20667 break;
20668
20669 /* Stop at line ends. */
20670 if (ITERATOR_AT_END_OF_LINE_P (it))
20671 {
20672 it->continuation_lines_width = 0;
20673 break;
20674 }
20675
20676 set_iterator_to_next (it, 1);
20677 if (STRINGP (it->string))
20678 it_charpos = IT_STRING_CHARPOS (*it);
20679 else
20680 it_charpos = IT_CHARPOS (*it);
20681
20682 /* Stop if truncating at the right edge. */
20683 if (it->line_wrap == TRUNCATE
20684 && it->current_x >= it->last_visible_x)
20685 {
20686 /* Add truncation mark, but don't do it if the line is
20687 truncated at a padding space. */
20688 if (it_charpos < it->string_nchars)
20689 {
20690 if (!FRAME_WINDOW_P (it->f))
20691 {
20692 int ii, n;
20693
20694 if (it->current_x > it->last_visible_x)
20695 {
20696 if (!row->reversed_p)
20697 {
20698 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20699 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20700 break;
20701 }
20702 else
20703 {
20704 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
20705 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20706 break;
20707 unproduce_glyphs (it, ii + 1);
20708 ii = row->used[TEXT_AREA] - (ii + 1);
20709 }
20710 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20711 {
20712 row->used[TEXT_AREA] = ii;
20713 produce_special_glyphs (it, IT_TRUNCATION);
20714 }
20715 }
20716 produce_special_glyphs (it, IT_TRUNCATION);
20717 }
20718 row->truncated_on_right_p = 1;
20719 }
20720 break;
20721 }
20722 }
20723
20724 /* Maybe insert a truncation at the left. */
20725 if (it->first_visible_x
20726 && it_charpos > 0)
20727 {
20728 if (!FRAME_WINDOW_P (it->f))
20729 insert_left_trunc_glyphs (it);
20730 row->truncated_on_left_p = 1;
20731 }
20732
20733 it->face_id = saved_face_id;
20734
20735 /* Value is number of columns displayed. */
20736 return it->hpos - hpos_at_start;
20737 }
20738
20739
20740 \f
20741 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20742 appears as an element of LIST or as the car of an element of LIST.
20743 If PROPVAL is a list, compare each element against LIST in that
20744 way, and return 1/2 if any element of PROPVAL is found in LIST.
20745 Otherwise return 0. This function cannot quit.
20746 The return value is 2 if the text is invisible but with an ellipsis
20747 and 1 if it's invisible and without an ellipsis. */
20748
20749 int
20750 invisible_p (register Lisp_Object propval, Lisp_Object list)
20751 {
20752 register Lisp_Object tail, proptail;
20753
20754 for (tail = list; CONSP (tail); tail = XCDR (tail))
20755 {
20756 register Lisp_Object tem;
20757 tem = XCAR (tail);
20758 if (EQ (propval, tem))
20759 return 1;
20760 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20761 return NILP (XCDR (tem)) ? 1 : 2;
20762 }
20763
20764 if (CONSP (propval))
20765 {
20766 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20767 {
20768 Lisp_Object propelt;
20769 propelt = XCAR (proptail);
20770 for (tail = list; CONSP (tail); tail = XCDR (tail))
20771 {
20772 register Lisp_Object tem;
20773 tem = XCAR (tail);
20774 if (EQ (propelt, tem))
20775 return 1;
20776 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20777 return NILP (XCDR (tem)) ? 1 : 2;
20778 }
20779 }
20780 }
20781
20782 return 0;
20783 }
20784
20785 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20786 doc: /* Non-nil if the property makes the text invisible.
20787 POS-OR-PROP can be a marker or number, in which case it is taken to be
20788 a position in the current buffer and the value of the `invisible' property
20789 is checked; or it can be some other value, which is then presumed to be the
20790 value of the `invisible' property of the text of interest.
20791 The non-nil value returned can be t for truly invisible text or something
20792 else if the text is replaced by an ellipsis. */)
20793 (Lisp_Object pos_or_prop)
20794 {
20795 Lisp_Object prop
20796 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20797 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20798 : pos_or_prop);
20799 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20800 return (invis == 0 ? Qnil
20801 : invis == 1 ? Qt
20802 : make_number (invis));
20803 }
20804
20805 /* Calculate a width or height in pixels from a specification using
20806 the following elements:
20807
20808 SPEC ::=
20809 NUM - a (fractional) multiple of the default font width/height
20810 (NUM) - specifies exactly NUM pixels
20811 UNIT - a fixed number of pixels, see below.
20812 ELEMENT - size of a display element in pixels, see below.
20813 (NUM . SPEC) - equals NUM * SPEC
20814 (+ SPEC SPEC ...) - add pixel values
20815 (- SPEC SPEC ...) - subtract pixel values
20816 (- SPEC) - negate pixel value
20817
20818 NUM ::=
20819 INT or FLOAT - a number constant
20820 SYMBOL - use symbol's (buffer local) variable binding.
20821
20822 UNIT ::=
20823 in - pixels per inch *)
20824 mm - pixels per 1/1000 meter *)
20825 cm - pixels per 1/100 meter *)
20826 width - width of current font in pixels.
20827 height - height of current font in pixels.
20828
20829 *) using the ratio(s) defined in display-pixels-per-inch.
20830
20831 ELEMENT ::=
20832
20833 left-fringe - left fringe width in pixels
20834 right-fringe - right fringe width in pixels
20835
20836 left-margin - left margin width in pixels
20837 right-margin - right margin width in pixels
20838
20839 scroll-bar - scroll-bar area width in pixels
20840
20841 Examples:
20842
20843 Pixels corresponding to 5 inches:
20844 (5 . in)
20845
20846 Total width of non-text areas on left side of window (if scroll-bar is on left):
20847 '(space :width (+ left-fringe left-margin scroll-bar))
20848
20849 Align to first text column (in header line):
20850 '(space :align-to 0)
20851
20852 Align to middle of text area minus half the width of variable `my-image'
20853 containing a loaded image:
20854 '(space :align-to (0.5 . (- text my-image)))
20855
20856 Width of left margin minus width of 1 character in the default font:
20857 '(space :width (- left-margin 1))
20858
20859 Width of left margin minus width of 2 characters in the current font:
20860 '(space :width (- left-margin (2 . width)))
20861
20862 Center 1 character over left-margin (in header line):
20863 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20864
20865 Different ways to express width of left fringe plus left margin minus one pixel:
20866 '(space :width (- (+ left-fringe left-margin) (1)))
20867 '(space :width (+ left-fringe left-margin (- (1))))
20868 '(space :width (+ left-fringe left-margin (-1)))
20869
20870 */
20871
20872 #define NUMVAL(X) \
20873 ((INTEGERP (X) || FLOATP (X)) \
20874 ? XFLOATINT (X) \
20875 : - 1)
20876
20877 int
20878 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20879 struct font *font, int width_p, int *align_to)
20880 {
20881 double pixels;
20882
20883 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20884 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20885
20886 if (NILP (prop))
20887 return OK_PIXELS (0);
20888
20889 xassert (FRAME_LIVE_P (it->f));
20890
20891 if (SYMBOLP (prop))
20892 {
20893 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20894 {
20895 char *unit = SSDATA (SYMBOL_NAME (prop));
20896
20897 if (unit[0] == 'i' && unit[1] == 'n')
20898 pixels = 1.0;
20899 else if (unit[0] == 'm' && unit[1] == 'm')
20900 pixels = 25.4;
20901 else if (unit[0] == 'c' && unit[1] == 'm')
20902 pixels = 2.54;
20903 else
20904 pixels = 0;
20905 if (pixels > 0)
20906 {
20907 double ppi;
20908 #ifdef HAVE_WINDOW_SYSTEM
20909 if (FRAME_WINDOW_P (it->f)
20910 && (ppi = (width_p
20911 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20912 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20913 ppi > 0))
20914 return OK_PIXELS (ppi / pixels);
20915 #endif
20916
20917 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20918 || (CONSP (Vdisplay_pixels_per_inch)
20919 && (ppi = (width_p
20920 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20921 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20922 ppi > 0)))
20923 return OK_PIXELS (ppi / pixels);
20924
20925 return 0;
20926 }
20927 }
20928
20929 #ifdef HAVE_WINDOW_SYSTEM
20930 if (EQ (prop, Qheight))
20931 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20932 if (EQ (prop, Qwidth))
20933 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20934 #else
20935 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20936 return OK_PIXELS (1);
20937 #endif
20938
20939 if (EQ (prop, Qtext))
20940 return OK_PIXELS (width_p
20941 ? window_box_width (it->w, TEXT_AREA)
20942 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20943
20944 if (align_to && *align_to < 0)
20945 {
20946 *res = 0;
20947 if (EQ (prop, Qleft))
20948 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20949 if (EQ (prop, Qright))
20950 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20951 if (EQ (prop, Qcenter))
20952 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20953 + window_box_width (it->w, TEXT_AREA) / 2);
20954 if (EQ (prop, Qleft_fringe))
20955 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20956 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20957 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20958 if (EQ (prop, Qright_fringe))
20959 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20960 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20961 : window_box_right_offset (it->w, TEXT_AREA));
20962 if (EQ (prop, Qleft_margin))
20963 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20964 if (EQ (prop, Qright_margin))
20965 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20966 if (EQ (prop, Qscroll_bar))
20967 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20968 ? 0
20969 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20970 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20971 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20972 : 0)));
20973 }
20974 else
20975 {
20976 if (EQ (prop, Qleft_fringe))
20977 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20978 if (EQ (prop, Qright_fringe))
20979 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20980 if (EQ (prop, Qleft_margin))
20981 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20982 if (EQ (prop, Qright_margin))
20983 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20984 if (EQ (prop, Qscroll_bar))
20985 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20986 }
20987
20988 prop = Fbuffer_local_value (prop, it->w->buffer);
20989 }
20990
20991 if (INTEGERP (prop) || FLOATP (prop))
20992 {
20993 int base_unit = (width_p
20994 ? FRAME_COLUMN_WIDTH (it->f)
20995 : FRAME_LINE_HEIGHT (it->f));
20996 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20997 }
20998
20999 if (CONSP (prop))
21000 {
21001 Lisp_Object car = XCAR (prop);
21002 Lisp_Object cdr = XCDR (prop);
21003
21004 if (SYMBOLP (car))
21005 {
21006 #ifdef HAVE_WINDOW_SYSTEM
21007 if (FRAME_WINDOW_P (it->f)
21008 && valid_image_p (prop))
21009 {
21010 int id = lookup_image (it->f, prop);
21011 struct image *img = IMAGE_FROM_ID (it->f, id);
21012
21013 return OK_PIXELS (width_p ? img->width : img->height);
21014 }
21015 #endif
21016 if (EQ (car, Qplus) || EQ (car, Qminus))
21017 {
21018 int first = 1;
21019 double px;
21020
21021 pixels = 0;
21022 while (CONSP (cdr))
21023 {
21024 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21025 font, width_p, align_to))
21026 return 0;
21027 if (first)
21028 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21029 else
21030 pixels += px;
21031 cdr = XCDR (cdr);
21032 }
21033 if (EQ (car, Qminus))
21034 pixels = -pixels;
21035 return OK_PIXELS (pixels);
21036 }
21037
21038 car = Fbuffer_local_value (car, it->w->buffer);
21039 }
21040
21041 if (INTEGERP (car) || FLOATP (car))
21042 {
21043 double fact;
21044 pixels = XFLOATINT (car);
21045 if (NILP (cdr))
21046 return OK_PIXELS (pixels);
21047 if (calc_pixel_width_or_height (&fact, it, cdr,
21048 font, width_p, align_to))
21049 return OK_PIXELS (pixels * fact);
21050 return 0;
21051 }
21052
21053 return 0;
21054 }
21055
21056 return 0;
21057 }
21058
21059 \f
21060 /***********************************************************************
21061 Glyph Display
21062 ***********************************************************************/
21063
21064 #ifdef HAVE_WINDOW_SYSTEM
21065
21066 #if GLYPH_DEBUG
21067
21068 void
21069 dump_glyph_string (s)
21070 struct glyph_string *s;
21071 {
21072 fprintf (stderr, "glyph string\n");
21073 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21074 s->x, s->y, s->width, s->height);
21075 fprintf (stderr, " ybase = %d\n", s->ybase);
21076 fprintf (stderr, " hl = %d\n", s->hl);
21077 fprintf (stderr, " left overhang = %d, right = %d\n",
21078 s->left_overhang, s->right_overhang);
21079 fprintf (stderr, " nchars = %d\n", s->nchars);
21080 fprintf (stderr, " extends to end of line = %d\n",
21081 s->extends_to_end_of_line_p);
21082 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21083 fprintf (stderr, " bg width = %d\n", s->background_width);
21084 }
21085
21086 #endif /* GLYPH_DEBUG */
21087
21088 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21089 of XChar2b structures for S; it can't be allocated in
21090 init_glyph_string because it must be allocated via `alloca'. W
21091 is the window on which S is drawn. ROW and AREA are the glyph row
21092 and area within the row from which S is constructed. START is the
21093 index of the first glyph structure covered by S. HL is a
21094 face-override for drawing S. */
21095
21096 #ifdef HAVE_NTGUI
21097 #define OPTIONAL_HDC(hdc) HDC hdc,
21098 #define DECLARE_HDC(hdc) HDC hdc;
21099 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21100 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21101 #endif
21102
21103 #ifndef OPTIONAL_HDC
21104 #define OPTIONAL_HDC(hdc)
21105 #define DECLARE_HDC(hdc)
21106 #define ALLOCATE_HDC(hdc, f)
21107 #define RELEASE_HDC(hdc, f)
21108 #endif
21109
21110 static void
21111 init_glyph_string (struct glyph_string *s,
21112 OPTIONAL_HDC (hdc)
21113 XChar2b *char2b, struct window *w, struct glyph_row *row,
21114 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21115 {
21116 memset (s, 0, sizeof *s);
21117 s->w = w;
21118 s->f = XFRAME (w->frame);
21119 #ifdef HAVE_NTGUI
21120 s->hdc = hdc;
21121 #endif
21122 s->display = FRAME_X_DISPLAY (s->f);
21123 s->window = FRAME_X_WINDOW (s->f);
21124 s->char2b = char2b;
21125 s->hl = hl;
21126 s->row = row;
21127 s->area = area;
21128 s->first_glyph = row->glyphs[area] + start;
21129 s->height = row->height;
21130 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21131 s->ybase = s->y + row->ascent;
21132 }
21133
21134
21135 /* Append the list of glyph strings with head H and tail T to the list
21136 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21137
21138 static INLINE void
21139 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21140 struct glyph_string *h, struct glyph_string *t)
21141 {
21142 if (h)
21143 {
21144 if (*head)
21145 (*tail)->next = h;
21146 else
21147 *head = h;
21148 h->prev = *tail;
21149 *tail = t;
21150 }
21151 }
21152
21153
21154 /* Prepend the list of glyph strings with head H and tail T to the
21155 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21156 result. */
21157
21158 static INLINE void
21159 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21160 struct glyph_string *h, struct glyph_string *t)
21161 {
21162 if (h)
21163 {
21164 if (*head)
21165 (*head)->prev = t;
21166 else
21167 *tail = t;
21168 t->next = *head;
21169 *head = h;
21170 }
21171 }
21172
21173
21174 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21175 Set *HEAD and *TAIL to the resulting list. */
21176
21177 static INLINE void
21178 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21179 struct glyph_string *s)
21180 {
21181 s->next = s->prev = NULL;
21182 append_glyph_string_lists (head, tail, s, s);
21183 }
21184
21185
21186 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21187 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21188 make sure that X resources for the face returned are allocated.
21189 Value is a pointer to a realized face that is ready for display if
21190 DISPLAY_P is non-zero. */
21191
21192 static INLINE struct face *
21193 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21194 XChar2b *char2b, int display_p)
21195 {
21196 struct face *face = FACE_FROM_ID (f, face_id);
21197
21198 if (face->font)
21199 {
21200 unsigned code = face->font->driver->encode_char (face->font, c);
21201
21202 if (code != FONT_INVALID_CODE)
21203 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21204 else
21205 STORE_XCHAR2B (char2b, 0, 0);
21206 }
21207
21208 /* Make sure X resources of the face are allocated. */
21209 #ifdef HAVE_X_WINDOWS
21210 if (display_p)
21211 #endif
21212 {
21213 xassert (face != NULL);
21214 PREPARE_FACE_FOR_DISPLAY (f, face);
21215 }
21216
21217 return face;
21218 }
21219
21220
21221 /* Get face and two-byte form of character glyph GLYPH on frame F.
21222 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21223 a pointer to a realized face that is ready for display. */
21224
21225 static INLINE struct face *
21226 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21227 XChar2b *char2b, int *two_byte_p)
21228 {
21229 struct face *face;
21230
21231 xassert (glyph->type == CHAR_GLYPH);
21232 face = FACE_FROM_ID (f, glyph->face_id);
21233
21234 if (two_byte_p)
21235 *two_byte_p = 0;
21236
21237 if (face->font)
21238 {
21239 unsigned code;
21240
21241 if (CHAR_BYTE8_P (glyph->u.ch))
21242 code = CHAR_TO_BYTE8 (glyph->u.ch);
21243 else
21244 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21245
21246 if (code != FONT_INVALID_CODE)
21247 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21248 else
21249 STORE_XCHAR2B (char2b, 0, 0);
21250 }
21251
21252 /* Make sure X resources of the face are allocated. */
21253 xassert (face != NULL);
21254 PREPARE_FACE_FOR_DISPLAY (f, face);
21255 return face;
21256 }
21257
21258
21259 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21260 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21261
21262 static INLINE int
21263 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21264 {
21265 unsigned code;
21266
21267 if (CHAR_BYTE8_P (c))
21268 code = CHAR_TO_BYTE8 (c);
21269 else
21270 code = font->driver->encode_char (font, c);
21271
21272 if (code == FONT_INVALID_CODE)
21273 return 0;
21274 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21275 return 1;
21276 }
21277
21278
21279 /* Fill glyph string S with composition components specified by S->cmp.
21280
21281 BASE_FACE is the base face of the composition.
21282 S->cmp_from is the index of the first component for S.
21283
21284 OVERLAPS non-zero means S should draw the foreground only, and use
21285 its physical height for clipping. See also draw_glyphs.
21286
21287 Value is the index of a component not in S. */
21288
21289 static int
21290 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21291 int overlaps)
21292 {
21293 int i;
21294 /* For all glyphs of this composition, starting at the offset
21295 S->cmp_from, until we reach the end of the definition or encounter a
21296 glyph that requires the different face, add it to S. */
21297 struct face *face;
21298
21299 xassert (s);
21300
21301 s->for_overlaps = overlaps;
21302 s->face = NULL;
21303 s->font = NULL;
21304 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21305 {
21306 int c = COMPOSITION_GLYPH (s->cmp, i);
21307
21308 if (c != '\t')
21309 {
21310 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21311 -1, Qnil);
21312
21313 face = get_char_face_and_encoding (s->f, c, face_id,
21314 s->char2b + i, 1);
21315 if (face)
21316 {
21317 if (! s->face)
21318 {
21319 s->face = face;
21320 s->font = s->face->font;
21321 }
21322 else if (s->face != face)
21323 break;
21324 }
21325 }
21326 ++s->nchars;
21327 }
21328 s->cmp_to = i;
21329
21330 /* All glyph strings for the same composition has the same width,
21331 i.e. the width set for the first component of the composition. */
21332 s->width = s->first_glyph->pixel_width;
21333
21334 /* If the specified font could not be loaded, use the frame's
21335 default font, but record the fact that we couldn't load it in
21336 the glyph string so that we can draw rectangles for the
21337 characters of the glyph string. */
21338 if (s->font == NULL)
21339 {
21340 s->font_not_found_p = 1;
21341 s->font = FRAME_FONT (s->f);
21342 }
21343
21344 /* Adjust base line for subscript/superscript text. */
21345 s->ybase += s->first_glyph->voffset;
21346
21347 /* This glyph string must always be drawn with 16-bit functions. */
21348 s->two_byte_p = 1;
21349
21350 return s->cmp_to;
21351 }
21352
21353 static int
21354 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21355 int start, int end, int overlaps)
21356 {
21357 struct glyph *glyph, *last;
21358 Lisp_Object lgstring;
21359 int i;
21360
21361 s->for_overlaps = overlaps;
21362 glyph = s->row->glyphs[s->area] + start;
21363 last = s->row->glyphs[s->area] + end;
21364 s->cmp_id = glyph->u.cmp.id;
21365 s->cmp_from = glyph->slice.cmp.from;
21366 s->cmp_to = glyph->slice.cmp.to + 1;
21367 s->face = FACE_FROM_ID (s->f, face_id);
21368 lgstring = composition_gstring_from_id (s->cmp_id);
21369 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21370 glyph++;
21371 while (glyph < last
21372 && glyph->u.cmp.automatic
21373 && glyph->u.cmp.id == s->cmp_id
21374 && s->cmp_to == glyph->slice.cmp.from)
21375 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21376
21377 for (i = s->cmp_from; i < s->cmp_to; i++)
21378 {
21379 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21380 unsigned code = LGLYPH_CODE (lglyph);
21381
21382 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21383 }
21384 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21385 return glyph - s->row->glyphs[s->area];
21386 }
21387
21388
21389 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21390 See the comment of fill_glyph_string for arguments.
21391 Value is the index of the first glyph not in S. */
21392
21393
21394 static int
21395 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21396 int start, int end, int overlaps)
21397 {
21398 struct glyph *glyph, *last;
21399 int voffset;
21400
21401 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21402 s->for_overlaps = overlaps;
21403 glyph = s->row->glyphs[s->area] + start;
21404 last = s->row->glyphs[s->area] + end;
21405 voffset = glyph->voffset;
21406 s->face = FACE_FROM_ID (s->f, face_id);
21407 s->font = s->face->font;
21408 s->nchars = 1;
21409 s->width = glyph->pixel_width;
21410 glyph++;
21411 while (glyph < last
21412 && glyph->type == GLYPHLESS_GLYPH
21413 && glyph->voffset == voffset
21414 && glyph->face_id == face_id)
21415 {
21416 s->nchars++;
21417 s->width += glyph->pixel_width;
21418 glyph++;
21419 }
21420 s->ybase += voffset;
21421 return glyph - s->row->glyphs[s->area];
21422 }
21423
21424
21425 /* Fill glyph string S from a sequence of character glyphs.
21426
21427 FACE_ID is the face id of the string. START is the index of the
21428 first glyph to consider, END is the index of the last + 1.
21429 OVERLAPS non-zero means S should draw the foreground only, and use
21430 its physical height for clipping. See also draw_glyphs.
21431
21432 Value is the index of the first glyph not in S. */
21433
21434 static int
21435 fill_glyph_string (struct glyph_string *s, int face_id,
21436 int start, int end, int overlaps)
21437 {
21438 struct glyph *glyph, *last;
21439 int voffset;
21440 int glyph_not_available_p;
21441
21442 xassert (s->f == XFRAME (s->w->frame));
21443 xassert (s->nchars == 0);
21444 xassert (start >= 0 && end > start);
21445
21446 s->for_overlaps = overlaps;
21447 glyph = s->row->glyphs[s->area] + start;
21448 last = s->row->glyphs[s->area] + end;
21449 voffset = glyph->voffset;
21450 s->padding_p = glyph->padding_p;
21451 glyph_not_available_p = glyph->glyph_not_available_p;
21452
21453 while (glyph < last
21454 && glyph->type == CHAR_GLYPH
21455 && glyph->voffset == voffset
21456 /* Same face id implies same font, nowadays. */
21457 && glyph->face_id == face_id
21458 && glyph->glyph_not_available_p == glyph_not_available_p)
21459 {
21460 int two_byte_p;
21461
21462 s->face = get_glyph_face_and_encoding (s->f, glyph,
21463 s->char2b + s->nchars,
21464 &two_byte_p);
21465 s->two_byte_p = two_byte_p;
21466 ++s->nchars;
21467 xassert (s->nchars <= end - start);
21468 s->width += glyph->pixel_width;
21469 if (glyph++->padding_p != s->padding_p)
21470 break;
21471 }
21472
21473 s->font = s->face->font;
21474
21475 /* If the specified font could not be loaded, use the frame's font,
21476 but record the fact that we couldn't load it in
21477 S->font_not_found_p so that we can draw rectangles for the
21478 characters of the glyph string. */
21479 if (s->font == NULL || glyph_not_available_p)
21480 {
21481 s->font_not_found_p = 1;
21482 s->font = FRAME_FONT (s->f);
21483 }
21484
21485 /* Adjust base line for subscript/superscript text. */
21486 s->ybase += voffset;
21487
21488 xassert (s->face && s->face->gc);
21489 return glyph - s->row->glyphs[s->area];
21490 }
21491
21492
21493 /* Fill glyph string S from image glyph S->first_glyph. */
21494
21495 static void
21496 fill_image_glyph_string (struct glyph_string *s)
21497 {
21498 xassert (s->first_glyph->type == IMAGE_GLYPH);
21499 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21500 xassert (s->img);
21501 s->slice = s->first_glyph->slice.img;
21502 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21503 s->font = s->face->font;
21504 s->width = s->first_glyph->pixel_width;
21505
21506 /* Adjust base line for subscript/superscript text. */
21507 s->ybase += s->first_glyph->voffset;
21508 }
21509
21510
21511 /* Fill glyph string S from a sequence of stretch glyphs.
21512
21513 START is the index of the first glyph to consider,
21514 END is the index of the last + 1.
21515
21516 Value is the index of the first glyph not in S. */
21517
21518 static int
21519 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21520 {
21521 struct glyph *glyph, *last;
21522 int voffset, face_id;
21523
21524 xassert (s->first_glyph->type == STRETCH_GLYPH);
21525
21526 glyph = s->row->glyphs[s->area] + start;
21527 last = s->row->glyphs[s->area] + end;
21528 face_id = glyph->face_id;
21529 s->face = FACE_FROM_ID (s->f, face_id);
21530 s->font = s->face->font;
21531 s->width = glyph->pixel_width;
21532 s->nchars = 1;
21533 voffset = glyph->voffset;
21534
21535 for (++glyph;
21536 (glyph < last
21537 && glyph->type == STRETCH_GLYPH
21538 && glyph->voffset == voffset
21539 && glyph->face_id == face_id);
21540 ++glyph)
21541 s->width += glyph->pixel_width;
21542
21543 /* Adjust base line for subscript/superscript text. */
21544 s->ybase += voffset;
21545
21546 /* The case that face->gc == 0 is handled when drawing the glyph
21547 string by calling PREPARE_FACE_FOR_DISPLAY. */
21548 xassert (s->face);
21549 return glyph - s->row->glyphs[s->area];
21550 }
21551
21552 static struct font_metrics *
21553 get_per_char_metric (struct font *font, XChar2b *char2b)
21554 {
21555 static struct font_metrics metrics;
21556 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21557
21558 if (! font || code == FONT_INVALID_CODE)
21559 return NULL;
21560 font->driver->text_extents (font, &code, 1, &metrics);
21561 return &metrics;
21562 }
21563
21564 /* EXPORT for RIF:
21565 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21566 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21567 assumed to be zero. */
21568
21569 void
21570 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21571 {
21572 *left = *right = 0;
21573
21574 if (glyph->type == CHAR_GLYPH)
21575 {
21576 struct face *face;
21577 XChar2b char2b;
21578 struct font_metrics *pcm;
21579
21580 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21581 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21582 {
21583 if (pcm->rbearing > pcm->width)
21584 *right = pcm->rbearing - pcm->width;
21585 if (pcm->lbearing < 0)
21586 *left = -pcm->lbearing;
21587 }
21588 }
21589 else if (glyph->type == COMPOSITE_GLYPH)
21590 {
21591 if (! glyph->u.cmp.automatic)
21592 {
21593 struct composition *cmp = composition_table[glyph->u.cmp.id];
21594
21595 if (cmp->rbearing > cmp->pixel_width)
21596 *right = cmp->rbearing - cmp->pixel_width;
21597 if (cmp->lbearing < 0)
21598 *left = - cmp->lbearing;
21599 }
21600 else
21601 {
21602 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21603 struct font_metrics metrics;
21604
21605 composition_gstring_width (gstring, glyph->slice.cmp.from,
21606 glyph->slice.cmp.to + 1, &metrics);
21607 if (metrics.rbearing > metrics.width)
21608 *right = metrics.rbearing - metrics.width;
21609 if (metrics.lbearing < 0)
21610 *left = - metrics.lbearing;
21611 }
21612 }
21613 }
21614
21615
21616 /* Return the index of the first glyph preceding glyph string S that
21617 is overwritten by S because of S's left overhang. Value is -1
21618 if no glyphs are overwritten. */
21619
21620 static int
21621 left_overwritten (struct glyph_string *s)
21622 {
21623 int k;
21624
21625 if (s->left_overhang)
21626 {
21627 int x = 0, i;
21628 struct glyph *glyphs = s->row->glyphs[s->area];
21629 int first = s->first_glyph - glyphs;
21630
21631 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21632 x -= glyphs[i].pixel_width;
21633
21634 k = i + 1;
21635 }
21636 else
21637 k = -1;
21638
21639 return k;
21640 }
21641
21642
21643 /* Return the index of the first glyph preceding glyph string S that
21644 is overwriting S because of its right overhang. Value is -1 if no
21645 glyph in front of S overwrites S. */
21646
21647 static int
21648 left_overwriting (struct glyph_string *s)
21649 {
21650 int i, k, x;
21651 struct glyph *glyphs = s->row->glyphs[s->area];
21652 int first = s->first_glyph - glyphs;
21653
21654 k = -1;
21655 x = 0;
21656 for (i = first - 1; i >= 0; --i)
21657 {
21658 int left, right;
21659 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21660 if (x + right > 0)
21661 k = i;
21662 x -= glyphs[i].pixel_width;
21663 }
21664
21665 return k;
21666 }
21667
21668
21669 /* Return the index of the last glyph following glyph string S that is
21670 overwritten by S because of S's right overhang. Value is -1 if
21671 no such glyph is found. */
21672
21673 static int
21674 right_overwritten (struct glyph_string *s)
21675 {
21676 int k = -1;
21677
21678 if (s->right_overhang)
21679 {
21680 int x = 0, i;
21681 struct glyph *glyphs = s->row->glyphs[s->area];
21682 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21683 int end = s->row->used[s->area];
21684
21685 for (i = first; i < end && s->right_overhang > x; ++i)
21686 x += glyphs[i].pixel_width;
21687
21688 k = i;
21689 }
21690
21691 return k;
21692 }
21693
21694
21695 /* Return the index of the last glyph following glyph string S that
21696 overwrites S because of its left overhang. Value is negative
21697 if no such glyph is found. */
21698
21699 static int
21700 right_overwriting (struct glyph_string *s)
21701 {
21702 int i, k, x;
21703 int end = s->row->used[s->area];
21704 struct glyph *glyphs = s->row->glyphs[s->area];
21705 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21706
21707 k = -1;
21708 x = 0;
21709 for (i = first; i < end; ++i)
21710 {
21711 int left, right;
21712 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21713 if (x - left < 0)
21714 k = i;
21715 x += glyphs[i].pixel_width;
21716 }
21717
21718 return k;
21719 }
21720
21721
21722 /* Set background width of glyph string S. START is the index of the
21723 first glyph following S. LAST_X is the right-most x-position + 1
21724 in the drawing area. */
21725
21726 static INLINE void
21727 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21728 {
21729 /* If the face of this glyph string has to be drawn to the end of
21730 the drawing area, set S->extends_to_end_of_line_p. */
21731
21732 if (start == s->row->used[s->area]
21733 && s->area == TEXT_AREA
21734 && ((s->row->fill_line_p
21735 && (s->hl == DRAW_NORMAL_TEXT
21736 || s->hl == DRAW_IMAGE_RAISED
21737 || s->hl == DRAW_IMAGE_SUNKEN))
21738 || s->hl == DRAW_MOUSE_FACE))
21739 s->extends_to_end_of_line_p = 1;
21740
21741 /* If S extends its face to the end of the line, set its
21742 background_width to the distance to the right edge of the drawing
21743 area. */
21744 if (s->extends_to_end_of_line_p)
21745 s->background_width = last_x - s->x + 1;
21746 else
21747 s->background_width = s->width;
21748 }
21749
21750
21751 /* Compute overhangs and x-positions for glyph string S and its
21752 predecessors, or successors. X is the starting x-position for S.
21753 BACKWARD_P non-zero means process predecessors. */
21754
21755 static void
21756 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21757 {
21758 if (backward_p)
21759 {
21760 while (s)
21761 {
21762 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21763 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21764 x -= s->width;
21765 s->x = x;
21766 s = s->prev;
21767 }
21768 }
21769 else
21770 {
21771 while (s)
21772 {
21773 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21774 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21775 s->x = x;
21776 x += s->width;
21777 s = s->next;
21778 }
21779 }
21780 }
21781
21782
21783
21784 /* The following macros are only called from draw_glyphs below.
21785 They reference the following parameters of that function directly:
21786 `w', `row', `area', and `overlap_p'
21787 as well as the following local variables:
21788 `s', `f', and `hdc' (in W32) */
21789
21790 #ifdef HAVE_NTGUI
21791 /* On W32, silently add local `hdc' variable to argument list of
21792 init_glyph_string. */
21793 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21794 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21795 #else
21796 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21797 init_glyph_string (s, char2b, w, row, area, start, hl)
21798 #endif
21799
21800 /* Add a glyph string for a stretch glyph to the list of strings
21801 between HEAD and TAIL. START is the index of the stretch glyph in
21802 row area AREA of glyph row ROW. END is the index of the last glyph
21803 in that glyph row area. X is the current output position assigned
21804 to the new glyph string constructed. HL overrides that face of the
21805 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21806 is the right-most x-position of the drawing area. */
21807
21808 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21809 and below -- keep them on one line. */
21810 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21811 do \
21812 { \
21813 s = (struct glyph_string *) alloca (sizeof *s); \
21814 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21815 START = fill_stretch_glyph_string (s, START, END); \
21816 append_glyph_string (&HEAD, &TAIL, s); \
21817 s->x = (X); \
21818 } \
21819 while (0)
21820
21821
21822 /* Add a glyph string for an image glyph to the list of strings
21823 between HEAD and TAIL. START is the index of the image glyph in
21824 row area AREA of glyph row ROW. END is the index of the last glyph
21825 in that glyph row area. X is the current output position assigned
21826 to the new glyph string constructed. HL overrides that face of the
21827 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21828 is the right-most x-position of the drawing area. */
21829
21830 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21831 do \
21832 { \
21833 s = (struct glyph_string *) alloca (sizeof *s); \
21834 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21835 fill_image_glyph_string (s); \
21836 append_glyph_string (&HEAD, &TAIL, s); \
21837 ++START; \
21838 s->x = (X); \
21839 } \
21840 while (0)
21841
21842
21843 /* Add a glyph string for a sequence of character glyphs to the list
21844 of strings between HEAD and TAIL. START is the index of the first
21845 glyph in row area AREA of glyph row ROW that is part of the new
21846 glyph string. END is the index of the last glyph in that glyph row
21847 area. X is the current output position assigned to the new glyph
21848 string constructed. HL overrides that face of the glyph; e.g. it
21849 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21850 right-most x-position of the drawing area. */
21851
21852 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21853 do \
21854 { \
21855 int face_id; \
21856 XChar2b *char2b; \
21857 \
21858 face_id = (row)->glyphs[area][START].face_id; \
21859 \
21860 s = (struct glyph_string *) alloca (sizeof *s); \
21861 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21862 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21863 append_glyph_string (&HEAD, &TAIL, s); \
21864 s->x = (X); \
21865 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21866 } \
21867 while (0)
21868
21869
21870 /* Add a glyph string for a composite sequence to the list of strings
21871 between HEAD and TAIL. START is the index of the first glyph in
21872 row area AREA of glyph row ROW that is part of the new glyph
21873 string. END is the index of the last glyph in that glyph row area.
21874 X is the current output position assigned to the new glyph string
21875 constructed. HL overrides that face of the glyph; e.g. it is
21876 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21877 x-position of the drawing area. */
21878
21879 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21880 do { \
21881 int face_id = (row)->glyphs[area][START].face_id; \
21882 struct face *base_face = FACE_FROM_ID (f, face_id); \
21883 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21884 struct composition *cmp = composition_table[cmp_id]; \
21885 XChar2b *char2b; \
21886 struct glyph_string *first_s IF_LINT (= NULL); \
21887 int n; \
21888 \
21889 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21890 \
21891 /* Make glyph_strings for each glyph sequence that is drawable by \
21892 the same face, and append them to HEAD/TAIL. */ \
21893 for (n = 0; n < cmp->glyph_len;) \
21894 { \
21895 s = (struct glyph_string *) alloca (sizeof *s); \
21896 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21897 append_glyph_string (&(HEAD), &(TAIL), s); \
21898 s->cmp = cmp; \
21899 s->cmp_from = n; \
21900 s->x = (X); \
21901 if (n == 0) \
21902 first_s = s; \
21903 n = fill_composite_glyph_string (s, base_face, overlaps); \
21904 } \
21905 \
21906 ++START; \
21907 s = first_s; \
21908 } while (0)
21909
21910
21911 /* Add a glyph string for a glyph-string sequence to the list of strings
21912 between HEAD and TAIL. */
21913
21914 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21915 do { \
21916 int face_id; \
21917 XChar2b *char2b; \
21918 Lisp_Object gstring; \
21919 \
21920 face_id = (row)->glyphs[area][START].face_id; \
21921 gstring = (composition_gstring_from_id \
21922 ((row)->glyphs[area][START].u.cmp.id)); \
21923 s = (struct glyph_string *) alloca (sizeof *s); \
21924 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21925 * LGSTRING_GLYPH_LEN (gstring)); \
21926 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21927 append_glyph_string (&(HEAD), &(TAIL), s); \
21928 s->x = (X); \
21929 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21930 } while (0)
21931
21932
21933 /* Add a glyph string for a sequence of glyphless character's glyphs
21934 to the list of strings between HEAD and TAIL. The meanings of
21935 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21936
21937 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21938 do \
21939 { \
21940 int face_id; \
21941 \
21942 face_id = (row)->glyphs[area][START].face_id; \
21943 \
21944 s = (struct glyph_string *) alloca (sizeof *s); \
21945 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21946 append_glyph_string (&HEAD, &TAIL, s); \
21947 s->x = (X); \
21948 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21949 overlaps); \
21950 } \
21951 while (0)
21952
21953
21954 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21955 of AREA of glyph row ROW on window W between indices START and END.
21956 HL overrides the face for drawing glyph strings, e.g. it is
21957 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21958 x-positions of the drawing area.
21959
21960 This is an ugly monster macro construct because we must use alloca
21961 to allocate glyph strings (because draw_glyphs can be called
21962 asynchronously). */
21963
21964 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21965 do \
21966 { \
21967 HEAD = TAIL = NULL; \
21968 while (START < END) \
21969 { \
21970 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21971 switch (first_glyph->type) \
21972 { \
21973 case CHAR_GLYPH: \
21974 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21975 HL, X, LAST_X); \
21976 break; \
21977 \
21978 case COMPOSITE_GLYPH: \
21979 if (first_glyph->u.cmp.automatic) \
21980 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21981 HL, X, LAST_X); \
21982 else \
21983 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21984 HL, X, LAST_X); \
21985 break; \
21986 \
21987 case STRETCH_GLYPH: \
21988 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21989 HL, X, LAST_X); \
21990 break; \
21991 \
21992 case IMAGE_GLYPH: \
21993 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21994 HL, X, LAST_X); \
21995 break; \
21996 \
21997 case GLYPHLESS_GLYPH: \
21998 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21999 HL, X, LAST_X); \
22000 break; \
22001 \
22002 default: \
22003 abort (); \
22004 } \
22005 \
22006 if (s) \
22007 { \
22008 set_glyph_string_background_width (s, START, LAST_X); \
22009 (X) += s->width; \
22010 } \
22011 } \
22012 } while (0)
22013
22014
22015 /* Draw glyphs between START and END in AREA of ROW on window W,
22016 starting at x-position X. X is relative to AREA in W. HL is a
22017 face-override with the following meaning:
22018
22019 DRAW_NORMAL_TEXT draw normally
22020 DRAW_CURSOR draw in cursor face
22021 DRAW_MOUSE_FACE draw in mouse face.
22022 DRAW_INVERSE_VIDEO draw in mode line face
22023 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22024 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22025
22026 If OVERLAPS is non-zero, draw only the foreground of characters and
22027 clip to the physical height of ROW. Non-zero value also defines
22028 the overlapping part to be drawn:
22029
22030 OVERLAPS_PRED overlap with preceding rows
22031 OVERLAPS_SUCC overlap with succeeding rows
22032 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22033 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22034
22035 Value is the x-position reached, relative to AREA of W. */
22036
22037 static int
22038 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22039 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22040 enum draw_glyphs_face hl, int overlaps)
22041 {
22042 struct glyph_string *head, *tail;
22043 struct glyph_string *s;
22044 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22045 int i, j, x_reached, last_x, area_left = 0;
22046 struct frame *f = XFRAME (WINDOW_FRAME (w));
22047 DECLARE_HDC (hdc);
22048
22049 ALLOCATE_HDC (hdc, f);
22050
22051 /* Let's rather be paranoid than getting a SEGV. */
22052 end = min (end, row->used[area]);
22053 start = max (0, start);
22054 start = min (end, start);
22055
22056 /* Translate X to frame coordinates. Set last_x to the right
22057 end of the drawing area. */
22058 if (row->full_width_p)
22059 {
22060 /* X is relative to the left edge of W, without scroll bars
22061 or fringes. */
22062 area_left = WINDOW_LEFT_EDGE_X (w);
22063 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22064 }
22065 else
22066 {
22067 area_left = window_box_left (w, area);
22068 last_x = area_left + window_box_width (w, area);
22069 }
22070 x += area_left;
22071
22072 /* Build a doubly-linked list of glyph_string structures between
22073 head and tail from what we have to draw. Note that the macro
22074 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22075 the reason we use a separate variable `i'. */
22076 i = start;
22077 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22078 if (tail)
22079 x_reached = tail->x + tail->background_width;
22080 else
22081 x_reached = x;
22082
22083 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22084 the row, redraw some glyphs in front or following the glyph
22085 strings built above. */
22086 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22087 {
22088 struct glyph_string *h, *t;
22089 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22090 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22091 int check_mouse_face = 0;
22092 int dummy_x = 0;
22093
22094 /* If mouse highlighting is on, we may need to draw adjacent
22095 glyphs using mouse-face highlighting. */
22096 if (area == TEXT_AREA && row->mouse_face_p)
22097 {
22098 struct glyph_row *mouse_beg_row, *mouse_end_row;
22099
22100 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22101 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22102
22103 if (row >= mouse_beg_row && row <= mouse_end_row)
22104 {
22105 check_mouse_face = 1;
22106 mouse_beg_col = (row == mouse_beg_row)
22107 ? hlinfo->mouse_face_beg_col : 0;
22108 mouse_end_col = (row == mouse_end_row)
22109 ? hlinfo->mouse_face_end_col
22110 : row->used[TEXT_AREA];
22111 }
22112 }
22113
22114 /* Compute overhangs for all glyph strings. */
22115 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22116 for (s = head; s; s = s->next)
22117 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22118
22119 /* Prepend glyph strings for glyphs in front of the first glyph
22120 string that are overwritten because of the first glyph
22121 string's left overhang. The background of all strings
22122 prepended must be drawn because the first glyph string
22123 draws over it. */
22124 i = left_overwritten (head);
22125 if (i >= 0)
22126 {
22127 enum draw_glyphs_face overlap_hl;
22128
22129 /* If this row contains mouse highlighting, attempt to draw
22130 the overlapped glyphs with the correct highlight. This
22131 code fails if the overlap encompasses more than one glyph
22132 and mouse-highlight spans only some of these glyphs.
22133 However, making it work perfectly involves a lot more
22134 code, and I don't know if the pathological case occurs in
22135 practice, so we'll stick to this for now. --- cyd */
22136 if (check_mouse_face
22137 && mouse_beg_col < start && mouse_end_col > i)
22138 overlap_hl = DRAW_MOUSE_FACE;
22139 else
22140 overlap_hl = DRAW_NORMAL_TEXT;
22141
22142 j = i;
22143 BUILD_GLYPH_STRINGS (j, start, h, t,
22144 overlap_hl, dummy_x, last_x);
22145 start = i;
22146 compute_overhangs_and_x (t, head->x, 1);
22147 prepend_glyph_string_lists (&head, &tail, h, t);
22148 clip_head = head;
22149 }
22150
22151 /* Prepend glyph strings for glyphs in front of the first glyph
22152 string that overwrite that glyph string because of their
22153 right overhang. For these strings, only the foreground must
22154 be drawn, because it draws over the glyph string at `head'.
22155 The background must not be drawn because this would overwrite
22156 right overhangs of preceding glyphs for which no glyph
22157 strings exist. */
22158 i = left_overwriting (head);
22159 if (i >= 0)
22160 {
22161 enum draw_glyphs_face overlap_hl;
22162
22163 if (check_mouse_face
22164 && mouse_beg_col < start && mouse_end_col > i)
22165 overlap_hl = DRAW_MOUSE_FACE;
22166 else
22167 overlap_hl = DRAW_NORMAL_TEXT;
22168
22169 clip_head = head;
22170 BUILD_GLYPH_STRINGS (i, start, h, t,
22171 overlap_hl, dummy_x, last_x);
22172 for (s = h; s; s = s->next)
22173 s->background_filled_p = 1;
22174 compute_overhangs_and_x (t, head->x, 1);
22175 prepend_glyph_string_lists (&head, &tail, h, t);
22176 }
22177
22178 /* Append glyphs strings for glyphs following the last glyph
22179 string tail that are overwritten by tail. The background of
22180 these strings has to be drawn because tail's foreground draws
22181 over it. */
22182 i = right_overwritten (tail);
22183 if (i >= 0)
22184 {
22185 enum draw_glyphs_face overlap_hl;
22186
22187 if (check_mouse_face
22188 && mouse_beg_col < i && mouse_end_col > end)
22189 overlap_hl = DRAW_MOUSE_FACE;
22190 else
22191 overlap_hl = DRAW_NORMAL_TEXT;
22192
22193 BUILD_GLYPH_STRINGS (end, i, h, t,
22194 overlap_hl, x, last_x);
22195 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22196 we don't have `end = i;' here. */
22197 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22198 append_glyph_string_lists (&head, &tail, h, t);
22199 clip_tail = tail;
22200 }
22201
22202 /* Append glyph strings for glyphs following the last glyph
22203 string tail that overwrite tail. The foreground of such
22204 glyphs has to be drawn because it writes into the background
22205 of tail. The background must not be drawn because it could
22206 paint over the foreground of following glyphs. */
22207 i = right_overwriting (tail);
22208 if (i >= 0)
22209 {
22210 enum draw_glyphs_face overlap_hl;
22211 if (check_mouse_face
22212 && mouse_beg_col < i && mouse_end_col > end)
22213 overlap_hl = DRAW_MOUSE_FACE;
22214 else
22215 overlap_hl = DRAW_NORMAL_TEXT;
22216
22217 clip_tail = tail;
22218 i++; /* We must include the Ith glyph. */
22219 BUILD_GLYPH_STRINGS (end, i, h, t,
22220 overlap_hl, x, last_x);
22221 for (s = h; s; s = s->next)
22222 s->background_filled_p = 1;
22223 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22224 append_glyph_string_lists (&head, &tail, h, t);
22225 }
22226 if (clip_head || clip_tail)
22227 for (s = head; s; s = s->next)
22228 {
22229 s->clip_head = clip_head;
22230 s->clip_tail = clip_tail;
22231 }
22232 }
22233
22234 /* Draw all strings. */
22235 for (s = head; s; s = s->next)
22236 FRAME_RIF (f)->draw_glyph_string (s);
22237
22238 #ifndef HAVE_NS
22239 /* When focus a sole frame and move horizontally, this sets on_p to 0
22240 causing a failure to erase prev cursor position. */
22241 if (area == TEXT_AREA
22242 && !row->full_width_p
22243 /* When drawing overlapping rows, only the glyph strings'
22244 foreground is drawn, which doesn't erase a cursor
22245 completely. */
22246 && !overlaps)
22247 {
22248 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22249 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22250 : (tail ? tail->x + tail->background_width : x));
22251 x0 -= area_left;
22252 x1 -= area_left;
22253
22254 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22255 row->y, MATRIX_ROW_BOTTOM_Y (row));
22256 }
22257 #endif
22258
22259 /* Value is the x-position up to which drawn, relative to AREA of W.
22260 This doesn't include parts drawn because of overhangs. */
22261 if (row->full_width_p)
22262 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22263 else
22264 x_reached -= area_left;
22265
22266 RELEASE_HDC (hdc, f);
22267
22268 return x_reached;
22269 }
22270
22271 /* Expand row matrix if too narrow. Don't expand if area
22272 is not present. */
22273
22274 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22275 { \
22276 if (!fonts_changed_p \
22277 && (it->glyph_row->glyphs[area] \
22278 < it->glyph_row->glyphs[area + 1])) \
22279 { \
22280 it->w->ncols_scale_factor++; \
22281 fonts_changed_p = 1; \
22282 } \
22283 }
22284
22285 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22286 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22287
22288 static INLINE void
22289 append_glyph (struct it *it)
22290 {
22291 struct glyph *glyph;
22292 enum glyph_row_area area = it->area;
22293
22294 xassert (it->glyph_row);
22295 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22296
22297 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22298 if (glyph < it->glyph_row->glyphs[area + 1])
22299 {
22300 /* If the glyph row is reversed, we need to prepend the glyph
22301 rather than append it. */
22302 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22303 {
22304 struct glyph *g;
22305
22306 /* Make room for the additional glyph. */
22307 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22308 g[1] = *g;
22309 glyph = it->glyph_row->glyphs[area];
22310 }
22311 glyph->charpos = CHARPOS (it->position);
22312 glyph->object = it->object;
22313 if (it->pixel_width > 0)
22314 {
22315 glyph->pixel_width = it->pixel_width;
22316 glyph->padding_p = 0;
22317 }
22318 else
22319 {
22320 /* Assure at least 1-pixel width. Otherwise, cursor can't
22321 be displayed correctly. */
22322 glyph->pixel_width = 1;
22323 glyph->padding_p = 1;
22324 }
22325 glyph->ascent = it->ascent;
22326 glyph->descent = it->descent;
22327 glyph->voffset = it->voffset;
22328 glyph->type = CHAR_GLYPH;
22329 glyph->avoid_cursor_p = it->avoid_cursor_p;
22330 glyph->multibyte_p = it->multibyte_p;
22331 glyph->left_box_line_p = it->start_of_box_run_p;
22332 glyph->right_box_line_p = it->end_of_box_run_p;
22333 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22334 || it->phys_descent > it->descent);
22335 glyph->glyph_not_available_p = it->glyph_not_available_p;
22336 glyph->face_id = it->face_id;
22337 glyph->u.ch = it->char_to_display;
22338 glyph->slice.img = null_glyph_slice;
22339 glyph->font_type = FONT_TYPE_UNKNOWN;
22340 if (it->bidi_p)
22341 {
22342 glyph->resolved_level = it->bidi_it.resolved_level;
22343 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22344 abort ();
22345 glyph->bidi_type = it->bidi_it.type;
22346 }
22347 else
22348 {
22349 glyph->resolved_level = 0;
22350 glyph->bidi_type = UNKNOWN_BT;
22351 }
22352 ++it->glyph_row->used[area];
22353 }
22354 else
22355 IT_EXPAND_MATRIX_WIDTH (it, area);
22356 }
22357
22358 /* Store one glyph for the composition IT->cmp_it.id in
22359 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22360 non-null. */
22361
22362 static INLINE void
22363 append_composite_glyph (struct it *it)
22364 {
22365 struct glyph *glyph;
22366 enum glyph_row_area area = it->area;
22367
22368 xassert (it->glyph_row);
22369
22370 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22371 if (glyph < it->glyph_row->glyphs[area + 1])
22372 {
22373 /* If the glyph row is reversed, we need to prepend the glyph
22374 rather than append it. */
22375 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22376 {
22377 struct glyph *g;
22378
22379 /* Make room for the new glyph. */
22380 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22381 g[1] = *g;
22382 glyph = it->glyph_row->glyphs[it->area];
22383 }
22384 glyph->charpos = it->cmp_it.charpos;
22385 glyph->object = it->object;
22386 glyph->pixel_width = it->pixel_width;
22387 glyph->ascent = it->ascent;
22388 glyph->descent = it->descent;
22389 glyph->voffset = it->voffset;
22390 glyph->type = COMPOSITE_GLYPH;
22391 if (it->cmp_it.ch < 0)
22392 {
22393 glyph->u.cmp.automatic = 0;
22394 glyph->u.cmp.id = it->cmp_it.id;
22395 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22396 }
22397 else
22398 {
22399 glyph->u.cmp.automatic = 1;
22400 glyph->u.cmp.id = it->cmp_it.id;
22401 glyph->slice.cmp.from = it->cmp_it.from;
22402 glyph->slice.cmp.to = it->cmp_it.to - 1;
22403 }
22404 glyph->avoid_cursor_p = it->avoid_cursor_p;
22405 glyph->multibyte_p = it->multibyte_p;
22406 glyph->left_box_line_p = it->start_of_box_run_p;
22407 glyph->right_box_line_p = it->end_of_box_run_p;
22408 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22409 || it->phys_descent > it->descent);
22410 glyph->padding_p = 0;
22411 glyph->glyph_not_available_p = 0;
22412 glyph->face_id = it->face_id;
22413 glyph->font_type = FONT_TYPE_UNKNOWN;
22414 if (it->bidi_p)
22415 {
22416 glyph->resolved_level = it->bidi_it.resolved_level;
22417 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22418 abort ();
22419 glyph->bidi_type = it->bidi_it.type;
22420 }
22421 ++it->glyph_row->used[area];
22422 }
22423 else
22424 IT_EXPAND_MATRIX_WIDTH (it, area);
22425 }
22426
22427
22428 /* Change IT->ascent and IT->height according to the setting of
22429 IT->voffset. */
22430
22431 static INLINE void
22432 take_vertical_position_into_account (struct it *it)
22433 {
22434 if (it->voffset)
22435 {
22436 if (it->voffset < 0)
22437 /* Increase the ascent so that we can display the text higher
22438 in the line. */
22439 it->ascent -= it->voffset;
22440 else
22441 /* Increase the descent so that we can display the text lower
22442 in the line. */
22443 it->descent += it->voffset;
22444 }
22445 }
22446
22447
22448 /* Produce glyphs/get display metrics for the image IT is loaded with.
22449 See the description of struct display_iterator in dispextern.h for
22450 an overview of struct display_iterator. */
22451
22452 static void
22453 produce_image_glyph (struct it *it)
22454 {
22455 struct image *img;
22456 struct face *face;
22457 int glyph_ascent, crop;
22458 struct glyph_slice slice;
22459
22460 xassert (it->what == IT_IMAGE);
22461
22462 face = FACE_FROM_ID (it->f, it->face_id);
22463 xassert (face);
22464 /* Make sure X resources of the face is loaded. */
22465 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22466
22467 if (it->image_id < 0)
22468 {
22469 /* Fringe bitmap. */
22470 it->ascent = it->phys_ascent = 0;
22471 it->descent = it->phys_descent = 0;
22472 it->pixel_width = 0;
22473 it->nglyphs = 0;
22474 return;
22475 }
22476
22477 img = IMAGE_FROM_ID (it->f, it->image_id);
22478 xassert (img);
22479 /* Make sure X resources of the image is loaded. */
22480 prepare_image_for_display (it->f, img);
22481
22482 slice.x = slice.y = 0;
22483 slice.width = img->width;
22484 slice.height = img->height;
22485
22486 if (INTEGERP (it->slice.x))
22487 slice.x = XINT (it->slice.x);
22488 else if (FLOATP (it->slice.x))
22489 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22490
22491 if (INTEGERP (it->slice.y))
22492 slice.y = XINT (it->slice.y);
22493 else if (FLOATP (it->slice.y))
22494 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22495
22496 if (INTEGERP (it->slice.width))
22497 slice.width = XINT (it->slice.width);
22498 else if (FLOATP (it->slice.width))
22499 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22500
22501 if (INTEGERP (it->slice.height))
22502 slice.height = XINT (it->slice.height);
22503 else if (FLOATP (it->slice.height))
22504 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22505
22506 if (slice.x >= img->width)
22507 slice.x = img->width;
22508 if (slice.y >= img->height)
22509 slice.y = img->height;
22510 if (slice.x + slice.width >= img->width)
22511 slice.width = img->width - slice.x;
22512 if (slice.y + slice.height > img->height)
22513 slice.height = img->height - slice.y;
22514
22515 if (slice.width == 0 || slice.height == 0)
22516 return;
22517
22518 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22519
22520 it->descent = slice.height - glyph_ascent;
22521 if (slice.y == 0)
22522 it->descent += img->vmargin;
22523 if (slice.y + slice.height == img->height)
22524 it->descent += img->vmargin;
22525 it->phys_descent = it->descent;
22526
22527 it->pixel_width = slice.width;
22528 if (slice.x == 0)
22529 it->pixel_width += img->hmargin;
22530 if (slice.x + slice.width == img->width)
22531 it->pixel_width += img->hmargin;
22532
22533 /* It's quite possible for images to have an ascent greater than
22534 their height, so don't get confused in that case. */
22535 if (it->descent < 0)
22536 it->descent = 0;
22537
22538 it->nglyphs = 1;
22539
22540 if (face->box != FACE_NO_BOX)
22541 {
22542 if (face->box_line_width > 0)
22543 {
22544 if (slice.y == 0)
22545 it->ascent += face->box_line_width;
22546 if (slice.y + slice.height == img->height)
22547 it->descent += face->box_line_width;
22548 }
22549
22550 if (it->start_of_box_run_p && slice.x == 0)
22551 it->pixel_width += eabs (face->box_line_width);
22552 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22553 it->pixel_width += eabs (face->box_line_width);
22554 }
22555
22556 take_vertical_position_into_account (it);
22557
22558 /* Automatically crop wide image glyphs at right edge so we can
22559 draw the cursor on same display row. */
22560 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22561 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22562 {
22563 it->pixel_width -= crop;
22564 slice.width -= crop;
22565 }
22566
22567 if (it->glyph_row)
22568 {
22569 struct glyph *glyph;
22570 enum glyph_row_area area = it->area;
22571
22572 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22573 if (glyph < it->glyph_row->glyphs[area + 1])
22574 {
22575 glyph->charpos = CHARPOS (it->position);
22576 glyph->object = it->object;
22577 glyph->pixel_width = it->pixel_width;
22578 glyph->ascent = glyph_ascent;
22579 glyph->descent = it->descent;
22580 glyph->voffset = it->voffset;
22581 glyph->type = IMAGE_GLYPH;
22582 glyph->avoid_cursor_p = it->avoid_cursor_p;
22583 glyph->multibyte_p = it->multibyte_p;
22584 glyph->left_box_line_p = it->start_of_box_run_p;
22585 glyph->right_box_line_p = it->end_of_box_run_p;
22586 glyph->overlaps_vertically_p = 0;
22587 glyph->padding_p = 0;
22588 glyph->glyph_not_available_p = 0;
22589 glyph->face_id = it->face_id;
22590 glyph->u.img_id = img->id;
22591 glyph->slice.img = slice;
22592 glyph->font_type = FONT_TYPE_UNKNOWN;
22593 if (it->bidi_p)
22594 {
22595 glyph->resolved_level = it->bidi_it.resolved_level;
22596 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22597 abort ();
22598 glyph->bidi_type = it->bidi_it.type;
22599 }
22600 ++it->glyph_row->used[area];
22601 }
22602 else
22603 IT_EXPAND_MATRIX_WIDTH (it, area);
22604 }
22605 }
22606
22607
22608 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22609 of the glyph, WIDTH and HEIGHT are the width and height of the
22610 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22611
22612 static void
22613 append_stretch_glyph (struct it *it, Lisp_Object object,
22614 int width, int height, int ascent)
22615 {
22616 struct glyph *glyph;
22617 enum glyph_row_area area = it->area;
22618
22619 xassert (ascent >= 0 && ascent <= height);
22620
22621 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22622 if (glyph < it->glyph_row->glyphs[area + 1])
22623 {
22624 /* If the glyph row is reversed, we need to prepend the glyph
22625 rather than append it. */
22626 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22627 {
22628 struct glyph *g;
22629
22630 /* Make room for the additional glyph. */
22631 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22632 g[1] = *g;
22633 glyph = it->glyph_row->glyphs[area];
22634 }
22635 glyph->charpos = CHARPOS (it->position);
22636 glyph->object = object;
22637 glyph->pixel_width = width;
22638 glyph->ascent = ascent;
22639 glyph->descent = height - ascent;
22640 glyph->voffset = it->voffset;
22641 glyph->type = STRETCH_GLYPH;
22642 glyph->avoid_cursor_p = it->avoid_cursor_p;
22643 glyph->multibyte_p = it->multibyte_p;
22644 glyph->left_box_line_p = it->start_of_box_run_p;
22645 glyph->right_box_line_p = it->end_of_box_run_p;
22646 glyph->overlaps_vertically_p = 0;
22647 glyph->padding_p = 0;
22648 glyph->glyph_not_available_p = 0;
22649 glyph->face_id = it->face_id;
22650 glyph->u.stretch.ascent = ascent;
22651 glyph->u.stretch.height = height;
22652 glyph->slice.img = null_glyph_slice;
22653 glyph->font_type = FONT_TYPE_UNKNOWN;
22654 if (it->bidi_p)
22655 {
22656 glyph->resolved_level = it->bidi_it.resolved_level;
22657 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22658 abort ();
22659 glyph->bidi_type = it->bidi_it.type;
22660 }
22661 else
22662 {
22663 glyph->resolved_level = 0;
22664 glyph->bidi_type = UNKNOWN_BT;
22665 }
22666 ++it->glyph_row->used[area];
22667 }
22668 else
22669 IT_EXPAND_MATRIX_WIDTH (it, area);
22670 }
22671
22672
22673 /* Produce a stretch glyph for iterator IT. IT->object is the value
22674 of the glyph property displayed. The value must be a list
22675 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22676 being recognized:
22677
22678 1. `:width WIDTH' specifies that the space should be WIDTH *
22679 canonical char width wide. WIDTH may be an integer or floating
22680 point number.
22681
22682 2. `:relative-width FACTOR' specifies that the width of the stretch
22683 should be computed from the width of the first character having the
22684 `glyph' property, and should be FACTOR times that width.
22685
22686 3. `:align-to HPOS' specifies that the space should be wide enough
22687 to reach HPOS, a value in canonical character units.
22688
22689 Exactly one of the above pairs must be present.
22690
22691 4. `:height HEIGHT' specifies that the height of the stretch produced
22692 should be HEIGHT, measured in canonical character units.
22693
22694 5. `:relative-height FACTOR' specifies that the height of the
22695 stretch should be FACTOR times the height of the characters having
22696 the glyph property.
22697
22698 Either none or exactly one of 4 or 5 must be present.
22699
22700 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22701 of the stretch should be used for the ascent of the stretch.
22702 ASCENT must be in the range 0 <= ASCENT <= 100. */
22703
22704 static void
22705 produce_stretch_glyph (struct it *it)
22706 {
22707 /* (space :width WIDTH :height HEIGHT ...) */
22708 Lisp_Object prop, plist;
22709 int width = 0, height = 0, align_to = -1;
22710 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22711 int ascent = 0;
22712 double tem;
22713 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22714 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22715
22716 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22717
22718 /* List should start with `space'. */
22719 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22720 plist = XCDR (it->object);
22721
22722 /* Compute the width of the stretch. */
22723 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22724 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22725 {
22726 /* Absolute width `:width WIDTH' specified and valid. */
22727 zero_width_ok_p = 1;
22728 width = (int)tem;
22729 }
22730 else if (prop = Fplist_get (plist, QCrelative_width),
22731 NUMVAL (prop) > 0)
22732 {
22733 /* Relative width `:relative-width FACTOR' specified and valid.
22734 Compute the width of the characters having the `glyph'
22735 property. */
22736 struct it it2;
22737 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22738
22739 it2 = *it;
22740 if (it->multibyte_p)
22741 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22742 else
22743 {
22744 it2.c = it2.char_to_display = *p, it2.len = 1;
22745 if (! ASCII_CHAR_P (it2.c))
22746 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22747 }
22748
22749 it2.glyph_row = NULL;
22750 it2.what = IT_CHARACTER;
22751 x_produce_glyphs (&it2);
22752 width = NUMVAL (prop) * it2.pixel_width;
22753 }
22754 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22755 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22756 {
22757 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22758 align_to = (align_to < 0
22759 ? 0
22760 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22761 else if (align_to < 0)
22762 align_to = window_box_left_offset (it->w, TEXT_AREA);
22763 width = max (0, (int)tem + align_to - it->current_x);
22764 zero_width_ok_p = 1;
22765 }
22766 else
22767 /* Nothing specified -> width defaults to canonical char width. */
22768 width = FRAME_COLUMN_WIDTH (it->f);
22769
22770 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22771 width = 1;
22772
22773 /* Compute height. */
22774 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22775 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22776 {
22777 height = (int)tem;
22778 zero_height_ok_p = 1;
22779 }
22780 else if (prop = Fplist_get (plist, QCrelative_height),
22781 NUMVAL (prop) > 0)
22782 height = FONT_HEIGHT (font) * NUMVAL (prop);
22783 else
22784 height = FONT_HEIGHT (font);
22785
22786 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22787 height = 1;
22788
22789 /* Compute percentage of height used for ascent. If
22790 `:ascent ASCENT' is present and valid, use that. Otherwise,
22791 derive the ascent from the font in use. */
22792 if (prop = Fplist_get (plist, QCascent),
22793 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22794 ascent = height * NUMVAL (prop) / 100.0;
22795 else if (!NILP (prop)
22796 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22797 ascent = min (max (0, (int)tem), height);
22798 else
22799 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22800
22801 if (width > 0 && it->line_wrap != TRUNCATE
22802 && it->current_x + width > it->last_visible_x)
22803 width = it->last_visible_x - it->current_x - 1;
22804
22805 if (width > 0 && height > 0 && it->glyph_row)
22806 {
22807 Lisp_Object object = it->stack[it->sp - 1].string;
22808 if (!STRINGP (object))
22809 object = it->w->buffer;
22810 append_stretch_glyph (it, object, width, height, ascent);
22811 }
22812
22813 it->pixel_width = width;
22814 it->ascent = it->phys_ascent = ascent;
22815 it->descent = it->phys_descent = height - it->ascent;
22816 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22817
22818 take_vertical_position_into_account (it);
22819 }
22820
22821 /* Calculate line-height and line-spacing properties.
22822 An integer value specifies explicit pixel value.
22823 A float value specifies relative value to current face height.
22824 A cons (float . face-name) specifies relative value to
22825 height of specified face font.
22826
22827 Returns height in pixels, or nil. */
22828
22829
22830 static Lisp_Object
22831 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22832 int boff, int override)
22833 {
22834 Lisp_Object face_name = Qnil;
22835 int ascent, descent, height;
22836
22837 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22838 return val;
22839
22840 if (CONSP (val))
22841 {
22842 face_name = XCAR (val);
22843 val = XCDR (val);
22844 if (!NUMBERP (val))
22845 val = make_number (1);
22846 if (NILP (face_name))
22847 {
22848 height = it->ascent + it->descent;
22849 goto scale;
22850 }
22851 }
22852
22853 if (NILP (face_name))
22854 {
22855 font = FRAME_FONT (it->f);
22856 boff = FRAME_BASELINE_OFFSET (it->f);
22857 }
22858 else if (EQ (face_name, Qt))
22859 {
22860 override = 0;
22861 }
22862 else
22863 {
22864 int face_id;
22865 struct face *face;
22866
22867 face_id = lookup_named_face (it->f, face_name, 0);
22868 if (face_id < 0)
22869 return make_number (-1);
22870
22871 face = FACE_FROM_ID (it->f, face_id);
22872 font = face->font;
22873 if (font == NULL)
22874 return make_number (-1);
22875 boff = font->baseline_offset;
22876 if (font->vertical_centering)
22877 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22878 }
22879
22880 ascent = FONT_BASE (font) + boff;
22881 descent = FONT_DESCENT (font) - boff;
22882
22883 if (override)
22884 {
22885 it->override_ascent = ascent;
22886 it->override_descent = descent;
22887 it->override_boff = boff;
22888 }
22889
22890 height = ascent + descent;
22891
22892 scale:
22893 if (FLOATP (val))
22894 height = (int)(XFLOAT_DATA (val) * height);
22895 else if (INTEGERP (val))
22896 height *= XINT (val);
22897
22898 return make_number (height);
22899 }
22900
22901
22902 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22903 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22904 and only if this is for a character for which no font was found.
22905
22906 If the display method (it->glyphless_method) is
22907 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22908 length of the acronym or the hexadecimal string, UPPER_XOFF and
22909 UPPER_YOFF are pixel offsets for the upper part of the string,
22910 LOWER_XOFF and LOWER_YOFF are for the lower part.
22911
22912 For the other display methods, LEN through LOWER_YOFF are zero. */
22913
22914 static void
22915 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22916 short upper_xoff, short upper_yoff,
22917 short lower_xoff, short lower_yoff)
22918 {
22919 struct glyph *glyph;
22920 enum glyph_row_area area = it->area;
22921
22922 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22923 if (glyph < it->glyph_row->glyphs[area + 1])
22924 {
22925 /* If the glyph row is reversed, we need to prepend the glyph
22926 rather than append it. */
22927 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22928 {
22929 struct glyph *g;
22930
22931 /* Make room for the additional glyph. */
22932 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22933 g[1] = *g;
22934 glyph = it->glyph_row->glyphs[area];
22935 }
22936 glyph->charpos = CHARPOS (it->position);
22937 glyph->object = it->object;
22938 glyph->pixel_width = it->pixel_width;
22939 glyph->ascent = it->ascent;
22940 glyph->descent = it->descent;
22941 glyph->voffset = it->voffset;
22942 glyph->type = GLYPHLESS_GLYPH;
22943 glyph->u.glyphless.method = it->glyphless_method;
22944 glyph->u.glyphless.for_no_font = for_no_font;
22945 glyph->u.glyphless.len = len;
22946 glyph->u.glyphless.ch = it->c;
22947 glyph->slice.glyphless.upper_xoff = upper_xoff;
22948 glyph->slice.glyphless.upper_yoff = upper_yoff;
22949 glyph->slice.glyphless.lower_xoff = lower_xoff;
22950 glyph->slice.glyphless.lower_yoff = lower_yoff;
22951 glyph->avoid_cursor_p = it->avoid_cursor_p;
22952 glyph->multibyte_p = it->multibyte_p;
22953 glyph->left_box_line_p = it->start_of_box_run_p;
22954 glyph->right_box_line_p = it->end_of_box_run_p;
22955 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22956 || it->phys_descent > it->descent);
22957 glyph->padding_p = 0;
22958 glyph->glyph_not_available_p = 0;
22959 glyph->face_id = face_id;
22960 glyph->font_type = FONT_TYPE_UNKNOWN;
22961 if (it->bidi_p)
22962 {
22963 glyph->resolved_level = it->bidi_it.resolved_level;
22964 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22965 abort ();
22966 glyph->bidi_type = it->bidi_it.type;
22967 }
22968 ++it->glyph_row->used[area];
22969 }
22970 else
22971 IT_EXPAND_MATRIX_WIDTH (it, area);
22972 }
22973
22974
22975 /* Produce a glyph for a glyphless character for iterator IT.
22976 IT->glyphless_method specifies which method to use for displaying
22977 the character. See the description of enum
22978 glyphless_display_method in dispextern.h for the detail.
22979
22980 FOR_NO_FONT is nonzero if and only if this is for a character for
22981 which no font was found. ACRONYM, if non-nil, is an acronym string
22982 for the character. */
22983
22984 static void
22985 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22986 {
22987 int face_id;
22988 struct face *face;
22989 struct font *font;
22990 int base_width, base_height, width, height;
22991 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22992 int len;
22993
22994 /* Get the metrics of the base font. We always refer to the current
22995 ASCII face. */
22996 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22997 font = face->font ? face->font : FRAME_FONT (it->f);
22998 it->ascent = FONT_BASE (font) + font->baseline_offset;
22999 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23000 base_height = it->ascent + it->descent;
23001 base_width = font->average_width;
23002
23003 /* Get a face ID for the glyph by utilizing a cache (the same way as
23004 doen for `escape-glyph' in get_next_display_element). */
23005 if (it->f == last_glyphless_glyph_frame
23006 && it->face_id == last_glyphless_glyph_face_id)
23007 {
23008 face_id = last_glyphless_glyph_merged_face_id;
23009 }
23010 else
23011 {
23012 /* Merge the `glyphless-char' face into the current face. */
23013 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23014 last_glyphless_glyph_frame = it->f;
23015 last_glyphless_glyph_face_id = it->face_id;
23016 last_glyphless_glyph_merged_face_id = face_id;
23017 }
23018
23019 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23020 {
23021 it->pixel_width = THIN_SPACE_WIDTH;
23022 len = 0;
23023 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23024 }
23025 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23026 {
23027 width = CHAR_WIDTH (it->c);
23028 if (width == 0)
23029 width = 1;
23030 else if (width > 4)
23031 width = 4;
23032 it->pixel_width = base_width * width;
23033 len = 0;
23034 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23035 }
23036 else
23037 {
23038 char buf[7];
23039 const char *str;
23040 unsigned int code[6];
23041 int upper_len;
23042 int ascent, descent;
23043 struct font_metrics metrics_upper, metrics_lower;
23044
23045 face = FACE_FROM_ID (it->f, face_id);
23046 font = face->font ? face->font : FRAME_FONT (it->f);
23047 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23048
23049 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23050 {
23051 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23052 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23053 if (CONSP (acronym))
23054 acronym = XCAR (acronym);
23055 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23056 }
23057 else
23058 {
23059 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23060 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23061 str = buf;
23062 }
23063 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23064 code[len] = font->driver->encode_char (font, str[len]);
23065 upper_len = (len + 1) / 2;
23066 font->driver->text_extents (font, code, upper_len,
23067 &metrics_upper);
23068 font->driver->text_extents (font, code + upper_len, len - upper_len,
23069 &metrics_lower);
23070
23071
23072
23073 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23074 width = max (metrics_upper.width, metrics_lower.width) + 4;
23075 upper_xoff = upper_yoff = 2; /* the typical case */
23076 if (base_width >= width)
23077 {
23078 /* Align the upper to the left, the lower to the right. */
23079 it->pixel_width = base_width;
23080 lower_xoff = base_width - 2 - metrics_lower.width;
23081 }
23082 else
23083 {
23084 /* Center the shorter one. */
23085 it->pixel_width = width;
23086 if (metrics_upper.width >= metrics_lower.width)
23087 lower_xoff = (width - metrics_lower.width) / 2;
23088 else
23089 {
23090 /* FIXME: This code doesn't look right. It formerly was
23091 missing the "lower_xoff = 0;", which couldn't have
23092 been right since it left lower_xoff uninitialized. */
23093 lower_xoff = 0;
23094 upper_xoff = (width - metrics_upper.width) / 2;
23095 }
23096 }
23097
23098 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23099 top, bottom, and between upper and lower strings. */
23100 height = (metrics_upper.ascent + metrics_upper.descent
23101 + metrics_lower.ascent + metrics_lower.descent) + 5;
23102 /* Center vertically.
23103 H:base_height, D:base_descent
23104 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23105
23106 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23107 descent = D - H/2 + h/2;
23108 lower_yoff = descent - 2 - ld;
23109 upper_yoff = lower_yoff - la - 1 - ud; */
23110 ascent = - (it->descent - (base_height + height + 1) / 2);
23111 descent = it->descent - (base_height - height) / 2;
23112 lower_yoff = descent - 2 - metrics_lower.descent;
23113 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23114 - metrics_upper.descent);
23115 /* Don't make the height shorter than the base height. */
23116 if (height > base_height)
23117 {
23118 it->ascent = ascent;
23119 it->descent = descent;
23120 }
23121 }
23122
23123 it->phys_ascent = it->ascent;
23124 it->phys_descent = it->descent;
23125 if (it->glyph_row)
23126 append_glyphless_glyph (it, face_id, for_no_font, len,
23127 upper_xoff, upper_yoff,
23128 lower_xoff, lower_yoff);
23129 it->nglyphs = 1;
23130 take_vertical_position_into_account (it);
23131 }
23132
23133
23134 /* RIF:
23135 Produce glyphs/get display metrics for the display element IT is
23136 loaded with. See the description of struct it in dispextern.h
23137 for an overview of struct it. */
23138
23139 void
23140 x_produce_glyphs (struct it *it)
23141 {
23142 int extra_line_spacing = it->extra_line_spacing;
23143
23144 it->glyph_not_available_p = 0;
23145
23146 if (it->what == IT_CHARACTER)
23147 {
23148 XChar2b char2b;
23149 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23150 struct font *font = face->font;
23151 struct font_metrics *pcm = NULL;
23152 int boff; /* baseline offset */
23153
23154 if (font == NULL)
23155 {
23156 /* When no suitable font is found, display this character by
23157 the method specified in the first extra slot of
23158 Vglyphless_char_display. */
23159 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23160
23161 xassert (it->what == IT_GLYPHLESS);
23162 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23163 goto done;
23164 }
23165
23166 boff = font->baseline_offset;
23167 if (font->vertical_centering)
23168 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23169
23170 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23171 {
23172 int stretched_p;
23173
23174 it->nglyphs = 1;
23175
23176 if (it->override_ascent >= 0)
23177 {
23178 it->ascent = it->override_ascent;
23179 it->descent = it->override_descent;
23180 boff = it->override_boff;
23181 }
23182 else
23183 {
23184 it->ascent = FONT_BASE (font) + boff;
23185 it->descent = FONT_DESCENT (font) - boff;
23186 }
23187
23188 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23189 {
23190 pcm = get_per_char_metric (font, &char2b);
23191 if (pcm->width == 0
23192 && pcm->rbearing == 0 && pcm->lbearing == 0)
23193 pcm = NULL;
23194 }
23195
23196 if (pcm)
23197 {
23198 it->phys_ascent = pcm->ascent + boff;
23199 it->phys_descent = pcm->descent - boff;
23200 it->pixel_width = pcm->width;
23201 }
23202 else
23203 {
23204 it->glyph_not_available_p = 1;
23205 it->phys_ascent = it->ascent;
23206 it->phys_descent = it->descent;
23207 it->pixel_width = font->space_width;
23208 }
23209
23210 if (it->constrain_row_ascent_descent_p)
23211 {
23212 if (it->descent > it->max_descent)
23213 {
23214 it->ascent += it->descent - it->max_descent;
23215 it->descent = it->max_descent;
23216 }
23217 if (it->ascent > it->max_ascent)
23218 {
23219 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23220 it->ascent = it->max_ascent;
23221 }
23222 it->phys_ascent = min (it->phys_ascent, it->ascent);
23223 it->phys_descent = min (it->phys_descent, it->descent);
23224 extra_line_spacing = 0;
23225 }
23226
23227 /* If this is a space inside a region of text with
23228 `space-width' property, change its width. */
23229 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23230 if (stretched_p)
23231 it->pixel_width *= XFLOATINT (it->space_width);
23232
23233 /* If face has a box, add the box thickness to the character
23234 height. If character has a box line to the left and/or
23235 right, add the box line width to the character's width. */
23236 if (face->box != FACE_NO_BOX)
23237 {
23238 int thick = face->box_line_width;
23239
23240 if (thick > 0)
23241 {
23242 it->ascent += thick;
23243 it->descent += thick;
23244 }
23245 else
23246 thick = -thick;
23247
23248 if (it->start_of_box_run_p)
23249 it->pixel_width += thick;
23250 if (it->end_of_box_run_p)
23251 it->pixel_width += thick;
23252 }
23253
23254 /* If face has an overline, add the height of the overline
23255 (1 pixel) and a 1 pixel margin to the character height. */
23256 if (face->overline_p)
23257 it->ascent += overline_margin;
23258
23259 if (it->constrain_row_ascent_descent_p)
23260 {
23261 if (it->ascent > it->max_ascent)
23262 it->ascent = it->max_ascent;
23263 if (it->descent > it->max_descent)
23264 it->descent = it->max_descent;
23265 }
23266
23267 take_vertical_position_into_account (it);
23268
23269 /* If we have to actually produce glyphs, do it. */
23270 if (it->glyph_row)
23271 {
23272 if (stretched_p)
23273 {
23274 /* Translate a space with a `space-width' property
23275 into a stretch glyph. */
23276 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23277 / FONT_HEIGHT (font));
23278 append_stretch_glyph (it, it->object, it->pixel_width,
23279 it->ascent + it->descent, ascent);
23280 }
23281 else
23282 append_glyph (it);
23283
23284 /* If characters with lbearing or rbearing are displayed
23285 in this line, record that fact in a flag of the
23286 glyph row. This is used to optimize X output code. */
23287 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23288 it->glyph_row->contains_overlapping_glyphs_p = 1;
23289 }
23290 if (! stretched_p && it->pixel_width == 0)
23291 /* We assure that all visible glyphs have at least 1-pixel
23292 width. */
23293 it->pixel_width = 1;
23294 }
23295 else if (it->char_to_display == '\n')
23296 {
23297 /* A newline has no width, but we need the height of the
23298 line. But if previous part of the line sets a height,
23299 don't increase that height */
23300
23301 Lisp_Object height;
23302 Lisp_Object total_height = Qnil;
23303
23304 it->override_ascent = -1;
23305 it->pixel_width = 0;
23306 it->nglyphs = 0;
23307
23308 height = get_it_property (it, Qline_height);
23309 /* Split (line-height total-height) list */
23310 if (CONSP (height)
23311 && CONSP (XCDR (height))
23312 && NILP (XCDR (XCDR (height))))
23313 {
23314 total_height = XCAR (XCDR (height));
23315 height = XCAR (height);
23316 }
23317 height = calc_line_height_property (it, height, font, boff, 1);
23318
23319 if (it->override_ascent >= 0)
23320 {
23321 it->ascent = it->override_ascent;
23322 it->descent = it->override_descent;
23323 boff = it->override_boff;
23324 }
23325 else
23326 {
23327 it->ascent = FONT_BASE (font) + boff;
23328 it->descent = FONT_DESCENT (font) - boff;
23329 }
23330
23331 if (EQ (height, Qt))
23332 {
23333 if (it->descent > it->max_descent)
23334 {
23335 it->ascent += it->descent - it->max_descent;
23336 it->descent = it->max_descent;
23337 }
23338 if (it->ascent > it->max_ascent)
23339 {
23340 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23341 it->ascent = it->max_ascent;
23342 }
23343 it->phys_ascent = min (it->phys_ascent, it->ascent);
23344 it->phys_descent = min (it->phys_descent, it->descent);
23345 it->constrain_row_ascent_descent_p = 1;
23346 extra_line_spacing = 0;
23347 }
23348 else
23349 {
23350 Lisp_Object spacing;
23351
23352 it->phys_ascent = it->ascent;
23353 it->phys_descent = it->descent;
23354
23355 if ((it->max_ascent > 0 || it->max_descent > 0)
23356 && face->box != FACE_NO_BOX
23357 && face->box_line_width > 0)
23358 {
23359 it->ascent += face->box_line_width;
23360 it->descent += face->box_line_width;
23361 }
23362 if (!NILP (height)
23363 && XINT (height) > it->ascent + it->descent)
23364 it->ascent = XINT (height) - it->descent;
23365
23366 if (!NILP (total_height))
23367 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23368 else
23369 {
23370 spacing = get_it_property (it, Qline_spacing);
23371 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23372 }
23373 if (INTEGERP (spacing))
23374 {
23375 extra_line_spacing = XINT (spacing);
23376 if (!NILP (total_height))
23377 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23378 }
23379 }
23380 }
23381 else /* i.e. (it->char_to_display == '\t') */
23382 {
23383 if (font->space_width > 0)
23384 {
23385 int tab_width = it->tab_width * font->space_width;
23386 int x = it->current_x + it->continuation_lines_width;
23387 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23388
23389 /* If the distance from the current position to the next tab
23390 stop is less than a space character width, use the
23391 tab stop after that. */
23392 if (next_tab_x - x < font->space_width)
23393 next_tab_x += tab_width;
23394
23395 it->pixel_width = next_tab_x - x;
23396 it->nglyphs = 1;
23397 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23398 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23399
23400 if (it->glyph_row)
23401 {
23402 append_stretch_glyph (it, it->object, it->pixel_width,
23403 it->ascent + it->descent, it->ascent);
23404 }
23405 }
23406 else
23407 {
23408 it->pixel_width = 0;
23409 it->nglyphs = 1;
23410 }
23411 }
23412 }
23413 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23414 {
23415 /* A static composition.
23416
23417 Note: A composition is represented as one glyph in the
23418 glyph matrix. There are no padding glyphs.
23419
23420 Important note: pixel_width, ascent, and descent are the
23421 values of what is drawn by draw_glyphs (i.e. the values of
23422 the overall glyphs composed). */
23423 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23424 int boff; /* baseline offset */
23425 struct composition *cmp = composition_table[it->cmp_it.id];
23426 int glyph_len = cmp->glyph_len;
23427 struct font *font = face->font;
23428
23429 it->nglyphs = 1;
23430
23431 /* If we have not yet calculated pixel size data of glyphs of
23432 the composition for the current face font, calculate them
23433 now. Theoretically, we have to check all fonts for the
23434 glyphs, but that requires much time and memory space. So,
23435 here we check only the font of the first glyph. This may
23436 lead to incorrect display, but it's very rare, and C-l
23437 (recenter-top-bottom) can correct the display anyway. */
23438 if (! cmp->font || cmp->font != font)
23439 {
23440 /* Ascent and descent of the font of the first character
23441 of this composition (adjusted by baseline offset).
23442 Ascent and descent of overall glyphs should not be less
23443 than these, respectively. */
23444 int font_ascent, font_descent, font_height;
23445 /* Bounding box of the overall glyphs. */
23446 int leftmost, rightmost, lowest, highest;
23447 int lbearing, rbearing;
23448 int i, width, ascent, descent;
23449 int left_padded = 0, right_padded = 0;
23450 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23451 XChar2b char2b;
23452 struct font_metrics *pcm;
23453 int font_not_found_p;
23454 EMACS_INT pos;
23455
23456 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23457 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23458 break;
23459 if (glyph_len < cmp->glyph_len)
23460 right_padded = 1;
23461 for (i = 0; i < glyph_len; i++)
23462 {
23463 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23464 break;
23465 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23466 }
23467 if (i > 0)
23468 left_padded = 1;
23469
23470 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23471 : IT_CHARPOS (*it));
23472 /* If no suitable font is found, use the default font. */
23473 font_not_found_p = font == NULL;
23474 if (font_not_found_p)
23475 {
23476 face = face->ascii_face;
23477 font = face->font;
23478 }
23479 boff = font->baseline_offset;
23480 if (font->vertical_centering)
23481 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23482 font_ascent = FONT_BASE (font) + boff;
23483 font_descent = FONT_DESCENT (font) - boff;
23484 font_height = FONT_HEIGHT (font);
23485
23486 cmp->font = (void *) font;
23487
23488 pcm = NULL;
23489 if (! font_not_found_p)
23490 {
23491 get_char_face_and_encoding (it->f, c, it->face_id,
23492 &char2b, 0);
23493 pcm = get_per_char_metric (font, &char2b);
23494 }
23495
23496 /* Initialize the bounding box. */
23497 if (pcm)
23498 {
23499 width = pcm->width;
23500 ascent = pcm->ascent;
23501 descent = pcm->descent;
23502 lbearing = pcm->lbearing;
23503 rbearing = pcm->rbearing;
23504 }
23505 else
23506 {
23507 width = font->space_width;
23508 ascent = FONT_BASE (font);
23509 descent = FONT_DESCENT (font);
23510 lbearing = 0;
23511 rbearing = width;
23512 }
23513
23514 rightmost = width;
23515 leftmost = 0;
23516 lowest = - descent + boff;
23517 highest = ascent + boff;
23518
23519 if (! font_not_found_p
23520 && font->default_ascent
23521 && CHAR_TABLE_P (Vuse_default_ascent)
23522 && !NILP (Faref (Vuse_default_ascent,
23523 make_number (it->char_to_display))))
23524 highest = font->default_ascent + boff;
23525
23526 /* Draw the first glyph at the normal position. It may be
23527 shifted to right later if some other glyphs are drawn
23528 at the left. */
23529 cmp->offsets[i * 2] = 0;
23530 cmp->offsets[i * 2 + 1] = boff;
23531 cmp->lbearing = lbearing;
23532 cmp->rbearing = rbearing;
23533
23534 /* Set cmp->offsets for the remaining glyphs. */
23535 for (i++; i < glyph_len; i++)
23536 {
23537 int left, right, btm, top;
23538 int ch = COMPOSITION_GLYPH (cmp, i);
23539 int face_id;
23540 struct face *this_face;
23541
23542 if (ch == '\t')
23543 ch = ' ';
23544 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23545 this_face = FACE_FROM_ID (it->f, face_id);
23546 font = this_face->font;
23547
23548 if (font == NULL)
23549 pcm = NULL;
23550 else
23551 {
23552 get_char_face_and_encoding (it->f, ch, face_id,
23553 &char2b, 0);
23554 pcm = get_per_char_metric (font, &char2b);
23555 }
23556 if (! pcm)
23557 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23558 else
23559 {
23560 width = pcm->width;
23561 ascent = pcm->ascent;
23562 descent = pcm->descent;
23563 lbearing = pcm->lbearing;
23564 rbearing = pcm->rbearing;
23565 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23566 {
23567 /* Relative composition with or without
23568 alternate chars. */
23569 left = (leftmost + rightmost - width) / 2;
23570 btm = - descent + boff;
23571 if (font->relative_compose
23572 && (! CHAR_TABLE_P (Vignore_relative_composition)
23573 || NILP (Faref (Vignore_relative_composition,
23574 make_number (ch)))))
23575 {
23576
23577 if (- descent >= font->relative_compose)
23578 /* One extra pixel between two glyphs. */
23579 btm = highest + 1;
23580 else if (ascent <= 0)
23581 /* One extra pixel between two glyphs. */
23582 btm = lowest - 1 - ascent - descent;
23583 }
23584 }
23585 else
23586 {
23587 /* A composition rule is specified by an integer
23588 value that encodes global and new reference
23589 points (GREF and NREF). GREF and NREF are
23590 specified by numbers as below:
23591
23592 0---1---2 -- ascent
23593 | |
23594 | |
23595 | |
23596 9--10--11 -- center
23597 | |
23598 ---3---4---5--- baseline
23599 | |
23600 6---7---8 -- descent
23601 */
23602 int rule = COMPOSITION_RULE (cmp, i);
23603 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23604
23605 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23606 grefx = gref % 3, nrefx = nref % 3;
23607 grefy = gref / 3, nrefy = nref / 3;
23608 if (xoff)
23609 xoff = font_height * (xoff - 128) / 256;
23610 if (yoff)
23611 yoff = font_height * (yoff - 128) / 256;
23612
23613 left = (leftmost
23614 + grefx * (rightmost - leftmost) / 2
23615 - nrefx * width / 2
23616 + xoff);
23617
23618 btm = ((grefy == 0 ? highest
23619 : grefy == 1 ? 0
23620 : grefy == 2 ? lowest
23621 : (highest + lowest) / 2)
23622 - (nrefy == 0 ? ascent + descent
23623 : nrefy == 1 ? descent - boff
23624 : nrefy == 2 ? 0
23625 : (ascent + descent) / 2)
23626 + yoff);
23627 }
23628
23629 cmp->offsets[i * 2] = left;
23630 cmp->offsets[i * 2 + 1] = btm + descent;
23631
23632 /* Update the bounding box of the overall glyphs. */
23633 if (width > 0)
23634 {
23635 right = left + width;
23636 if (left < leftmost)
23637 leftmost = left;
23638 if (right > rightmost)
23639 rightmost = right;
23640 }
23641 top = btm + descent + ascent;
23642 if (top > highest)
23643 highest = top;
23644 if (btm < lowest)
23645 lowest = btm;
23646
23647 if (cmp->lbearing > left + lbearing)
23648 cmp->lbearing = left + lbearing;
23649 if (cmp->rbearing < left + rbearing)
23650 cmp->rbearing = left + rbearing;
23651 }
23652 }
23653
23654 /* If there are glyphs whose x-offsets are negative,
23655 shift all glyphs to the right and make all x-offsets
23656 non-negative. */
23657 if (leftmost < 0)
23658 {
23659 for (i = 0; i < cmp->glyph_len; i++)
23660 cmp->offsets[i * 2] -= leftmost;
23661 rightmost -= leftmost;
23662 cmp->lbearing -= leftmost;
23663 cmp->rbearing -= leftmost;
23664 }
23665
23666 if (left_padded && cmp->lbearing < 0)
23667 {
23668 for (i = 0; i < cmp->glyph_len; i++)
23669 cmp->offsets[i * 2] -= cmp->lbearing;
23670 rightmost -= cmp->lbearing;
23671 cmp->rbearing -= cmp->lbearing;
23672 cmp->lbearing = 0;
23673 }
23674 if (right_padded && rightmost < cmp->rbearing)
23675 {
23676 rightmost = cmp->rbearing;
23677 }
23678
23679 cmp->pixel_width = rightmost;
23680 cmp->ascent = highest;
23681 cmp->descent = - lowest;
23682 if (cmp->ascent < font_ascent)
23683 cmp->ascent = font_ascent;
23684 if (cmp->descent < font_descent)
23685 cmp->descent = font_descent;
23686 }
23687
23688 if (it->glyph_row
23689 && (cmp->lbearing < 0
23690 || cmp->rbearing > cmp->pixel_width))
23691 it->glyph_row->contains_overlapping_glyphs_p = 1;
23692
23693 it->pixel_width = cmp->pixel_width;
23694 it->ascent = it->phys_ascent = cmp->ascent;
23695 it->descent = it->phys_descent = cmp->descent;
23696 if (face->box != FACE_NO_BOX)
23697 {
23698 int thick = face->box_line_width;
23699
23700 if (thick > 0)
23701 {
23702 it->ascent += thick;
23703 it->descent += thick;
23704 }
23705 else
23706 thick = - thick;
23707
23708 if (it->start_of_box_run_p)
23709 it->pixel_width += thick;
23710 if (it->end_of_box_run_p)
23711 it->pixel_width += thick;
23712 }
23713
23714 /* If face has an overline, add the height of the overline
23715 (1 pixel) and a 1 pixel margin to the character height. */
23716 if (face->overline_p)
23717 it->ascent += overline_margin;
23718
23719 take_vertical_position_into_account (it);
23720 if (it->ascent < 0)
23721 it->ascent = 0;
23722 if (it->descent < 0)
23723 it->descent = 0;
23724
23725 if (it->glyph_row)
23726 append_composite_glyph (it);
23727 }
23728 else if (it->what == IT_COMPOSITION)
23729 {
23730 /* A dynamic (automatic) composition. */
23731 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23732 Lisp_Object gstring;
23733 struct font_metrics metrics;
23734
23735 gstring = composition_gstring_from_id (it->cmp_it.id);
23736 it->pixel_width
23737 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23738 &metrics);
23739 if (it->glyph_row
23740 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23741 it->glyph_row->contains_overlapping_glyphs_p = 1;
23742 it->ascent = it->phys_ascent = metrics.ascent;
23743 it->descent = it->phys_descent = metrics.descent;
23744 if (face->box != FACE_NO_BOX)
23745 {
23746 int thick = face->box_line_width;
23747
23748 if (thick > 0)
23749 {
23750 it->ascent += thick;
23751 it->descent += thick;
23752 }
23753 else
23754 thick = - thick;
23755
23756 if (it->start_of_box_run_p)
23757 it->pixel_width += thick;
23758 if (it->end_of_box_run_p)
23759 it->pixel_width += thick;
23760 }
23761 /* If face has an overline, add the height of the overline
23762 (1 pixel) and a 1 pixel margin to the character height. */
23763 if (face->overline_p)
23764 it->ascent += overline_margin;
23765 take_vertical_position_into_account (it);
23766 if (it->ascent < 0)
23767 it->ascent = 0;
23768 if (it->descent < 0)
23769 it->descent = 0;
23770
23771 if (it->glyph_row)
23772 append_composite_glyph (it);
23773 }
23774 else if (it->what == IT_GLYPHLESS)
23775 produce_glyphless_glyph (it, 0, Qnil);
23776 else if (it->what == IT_IMAGE)
23777 produce_image_glyph (it);
23778 else if (it->what == IT_STRETCH)
23779 produce_stretch_glyph (it);
23780
23781 done:
23782 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23783 because this isn't true for images with `:ascent 100'. */
23784 xassert (it->ascent >= 0 && it->descent >= 0);
23785 if (it->area == TEXT_AREA)
23786 it->current_x += it->pixel_width;
23787
23788 if (extra_line_spacing > 0)
23789 {
23790 it->descent += extra_line_spacing;
23791 if (extra_line_spacing > it->max_extra_line_spacing)
23792 it->max_extra_line_spacing = extra_line_spacing;
23793 }
23794
23795 it->max_ascent = max (it->max_ascent, it->ascent);
23796 it->max_descent = max (it->max_descent, it->descent);
23797 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23798 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23799 }
23800
23801 /* EXPORT for RIF:
23802 Output LEN glyphs starting at START at the nominal cursor position.
23803 Advance the nominal cursor over the text. The global variable
23804 updated_window contains the window being updated, updated_row is
23805 the glyph row being updated, and updated_area is the area of that
23806 row being updated. */
23807
23808 void
23809 x_write_glyphs (struct glyph *start, int len)
23810 {
23811 int x, hpos;
23812
23813 xassert (updated_window && updated_row);
23814 BLOCK_INPUT;
23815
23816 /* Write glyphs. */
23817
23818 hpos = start - updated_row->glyphs[updated_area];
23819 x = draw_glyphs (updated_window, output_cursor.x,
23820 updated_row, updated_area,
23821 hpos, hpos + len,
23822 DRAW_NORMAL_TEXT, 0);
23823
23824 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23825 if (updated_area == TEXT_AREA
23826 && updated_window->phys_cursor_on_p
23827 && updated_window->phys_cursor.vpos == output_cursor.vpos
23828 && updated_window->phys_cursor.hpos >= hpos
23829 && updated_window->phys_cursor.hpos < hpos + len)
23830 updated_window->phys_cursor_on_p = 0;
23831
23832 UNBLOCK_INPUT;
23833
23834 /* Advance the output cursor. */
23835 output_cursor.hpos += len;
23836 output_cursor.x = x;
23837 }
23838
23839
23840 /* EXPORT for RIF:
23841 Insert LEN glyphs from START at the nominal cursor position. */
23842
23843 void
23844 x_insert_glyphs (struct glyph *start, int len)
23845 {
23846 struct frame *f;
23847 struct window *w;
23848 int line_height, shift_by_width, shifted_region_width;
23849 struct glyph_row *row;
23850 struct glyph *glyph;
23851 int frame_x, frame_y;
23852 EMACS_INT hpos;
23853
23854 xassert (updated_window && updated_row);
23855 BLOCK_INPUT;
23856 w = updated_window;
23857 f = XFRAME (WINDOW_FRAME (w));
23858
23859 /* Get the height of the line we are in. */
23860 row = updated_row;
23861 line_height = row->height;
23862
23863 /* Get the width of the glyphs to insert. */
23864 shift_by_width = 0;
23865 for (glyph = start; glyph < start + len; ++glyph)
23866 shift_by_width += glyph->pixel_width;
23867
23868 /* Get the width of the region to shift right. */
23869 shifted_region_width = (window_box_width (w, updated_area)
23870 - output_cursor.x
23871 - shift_by_width);
23872
23873 /* Shift right. */
23874 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23875 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23876
23877 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23878 line_height, shift_by_width);
23879
23880 /* Write the glyphs. */
23881 hpos = start - row->glyphs[updated_area];
23882 draw_glyphs (w, output_cursor.x, row, updated_area,
23883 hpos, hpos + len,
23884 DRAW_NORMAL_TEXT, 0);
23885
23886 /* Advance the output cursor. */
23887 output_cursor.hpos += len;
23888 output_cursor.x += shift_by_width;
23889 UNBLOCK_INPUT;
23890 }
23891
23892
23893 /* EXPORT for RIF:
23894 Erase the current text line from the nominal cursor position
23895 (inclusive) to pixel column TO_X (exclusive). The idea is that
23896 everything from TO_X onward is already erased.
23897
23898 TO_X is a pixel position relative to updated_area of
23899 updated_window. TO_X == -1 means clear to the end of this area. */
23900
23901 void
23902 x_clear_end_of_line (int to_x)
23903 {
23904 struct frame *f;
23905 struct window *w = updated_window;
23906 int max_x, min_y, max_y;
23907 int from_x, from_y, to_y;
23908
23909 xassert (updated_window && updated_row);
23910 f = XFRAME (w->frame);
23911
23912 if (updated_row->full_width_p)
23913 max_x = WINDOW_TOTAL_WIDTH (w);
23914 else
23915 max_x = window_box_width (w, updated_area);
23916 max_y = window_text_bottom_y (w);
23917
23918 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23919 of window. For TO_X > 0, truncate to end of drawing area. */
23920 if (to_x == 0)
23921 return;
23922 else if (to_x < 0)
23923 to_x = max_x;
23924 else
23925 to_x = min (to_x, max_x);
23926
23927 to_y = min (max_y, output_cursor.y + updated_row->height);
23928
23929 /* Notice if the cursor will be cleared by this operation. */
23930 if (!updated_row->full_width_p)
23931 notice_overwritten_cursor (w, updated_area,
23932 output_cursor.x, -1,
23933 updated_row->y,
23934 MATRIX_ROW_BOTTOM_Y (updated_row));
23935
23936 from_x = output_cursor.x;
23937
23938 /* Translate to frame coordinates. */
23939 if (updated_row->full_width_p)
23940 {
23941 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23942 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23943 }
23944 else
23945 {
23946 int area_left = window_box_left (w, updated_area);
23947 from_x += area_left;
23948 to_x += area_left;
23949 }
23950
23951 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23952 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23953 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23954
23955 /* Prevent inadvertently clearing to end of the X window. */
23956 if (to_x > from_x && to_y > from_y)
23957 {
23958 BLOCK_INPUT;
23959 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23960 to_x - from_x, to_y - from_y);
23961 UNBLOCK_INPUT;
23962 }
23963 }
23964
23965 #endif /* HAVE_WINDOW_SYSTEM */
23966
23967
23968 \f
23969 /***********************************************************************
23970 Cursor types
23971 ***********************************************************************/
23972
23973 /* Value is the internal representation of the specified cursor type
23974 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23975 of the bar cursor. */
23976
23977 static enum text_cursor_kinds
23978 get_specified_cursor_type (Lisp_Object arg, int *width)
23979 {
23980 enum text_cursor_kinds type;
23981
23982 if (NILP (arg))
23983 return NO_CURSOR;
23984
23985 if (EQ (arg, Qbox))
23986 return FILLED_BOX_CURSOR;
23987
23988 if (EQ (arg, Qhollow))
23989 return HOLLOW_BOX_CURSOR;
23990
23991 if (EQ (arg, Qbar))
23992 {
23993 *width = 2;
23994 return BAR_CURSOR;
23995 }
23996
23997 if (CONSP (arg)
23998 && EQ (XCAR (arg), Qbar)
23999 && INTEGERP (XCDR (arg))
24000 && XINT (XCDR (arg)) >= 0)
24001 {
24002 *width = XINT (XCDR (arg));
24003 return BAR_CURSOR;
24004 }
24005
24006 if (EQ (arg, Qhbar))
24007 {
24008 *width = 2;
24009 return HBAR_CURSOR;
24010 }
24011
24012 if (CONSP (arg)
24013 && EQ (XCAR (arg), Qhbar)
24014 && INTEGERP (XCDR (arg))
24015 && XINT (XCDR (arg)) >= 0)
24016 {
24017 *width = XINT (XCDR (arg));
24018 return HBAR_CURSOR;
24019 }
24020
24021 /* Treat anything unknown as "hollow box cursor".
24022 It was bad to signal an error; people have trouble fixing
24023 .Xdefaults with Emacs, when it has something bad in it. */
24024 type = HOLLOW_BOX_CURSOR;
24025
24026 return type;
24027 }
24028
24029 /* Set the default cursor types for specified frame. */
24030 void
24031 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24032 {
24033 int width = 1;
24034 Lisp_Object tem;
24035
24036 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24037 FRAME_CURSOR_WIDTH (f) = width;
24038
24039 /* By default, set up the blink-off state depending on the on-state. */
24040
24041 tem = Fassoc (arg, Vblink_cursor_alist);
24042 if (!NILP (tem))
24043 {
24044 FRAME_BLINK_OFF_CURSOR (f)
24045 = get_specified_cursor_type (XCDR (tem), &width);
24046 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24047 }
24048 else
24049 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24050 }
24051
24052
24053 #ifdef HAVE_WINDOW_SYSTEM
24054
24055 /* Return the cursor we want to be displayed in window W. Return
24056 width of bar/hbar cursor through WIDTH arg. Return with
24057 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24058 (i.e. if the `system caret' should track this cursor).
24059
24060 In a mini-buffer window, we want the cursor only to appear if we
24061 are reading input from this window. For the selected window, we
24062 want the cursor type given by the frame parameter or buffer local
24063 setting of cursor-type. If explicitly marked off, draw no cursor.
24064 In all other cases, we want a hollow box cursor. */
24065
24066 static enum text_cursor_kinds
24067 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24068 int *active_cursor)
24069 {
24070 struct frame *f = XFRAME (w->frame);
24071 struct buffer *b = XBUFFER (w->buffer);
24072 int cursor_type = DEFAULT_CURSOR;
24073 Lisp_Object alt_cursor;
24074 int non_selected = 0;
24075
24076 *active_cursor = 1;
24077
24078 /* Echo area */
24079 if (cursor_in_echo_area
24080 && FRAME_HAS_MINIBUF_P (f)
24081 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24082 {
24083 if (w == XWINDOW (echo_area_window))
24084 {
24085 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24086 {
24087 *width = FRAME_CURSOR_WIDTH (f);
24088 return FRAME_DESIRED_CURSOR (f);
24089 }
24090 else
24091 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24092 }
24093
24094 *active_cursor = 0;
24095 non_selected = 1;
24096 }
24097
24098 /* Detect a nonselected window or nonselected frame. */
24099 else if (w != XWINDOW (f->selected_window)
24100 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24101 {
24102 *active_cursor = 0;
24103
24104 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24105 return NO_CURSOR;
24106
24107 non_selected = 1;
24108 }
24109
24110 /* Never display a cursor in a window in which cursor-type is nil. */
24111 if (NILP (BVAR (b, cursor_type)))
24112 return NO_CURSOR;
24113
24114 /* Get the normal cursor type for this window. */
24115 if (EQ (BVAR (b, cursor_type), Qt))
24116 {
24117 cursor_type = FRAME_DESIRED_CURSOR (f);
24118 *width = FRAME_CURSOR_WIDTH (f);
24119 }
24120 else
24121 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24122
24123 /* Use cursor-in-non-selected-windows instead
24124 for non-selected window or frame. */
24125 if (non_selected)
24126 {
24127 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24128 if (!EQ (Qt, alt_cursor))
24129 return get_specified_cursor_type (alt_cursor, width);
24130 /* t means modify the normal cursor type. */
24131 if (cursor_type == FILLED_BOX_CURSOR)
24132 cursor_type = HOLLOW_BOX_CURSOR;
24133 else if (cursor_type == BAR_CURSOR && *width > 1)
24134 --*width;
24135 return cursor_type;
24136 }
24137
24138 /* Use normal cursor if not blinked off. */
24139 if (!w->cursor_off_p)
24140 {
24141 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24142 {
24143 if (cursor_type == FILLED_BOX_CURSOR)
24144 {
24145 /* Using a block cursor on large images can be very annoying.
24146 So use a hollow cursor for "large" images.
24147 If image is not transparent (no mask), also use hollow cursor. */
24148 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24149 if (img != NULL && IMAGEP (img->spec))
24150 {
24151 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24152 where N = size of default frame font size.
24153 This should cover most of the "tiny" icons people may use. */
24154 if (!img->mask
24155 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24156 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24157 cursor_type = HOLLOW_BOX_CURSOR;
24158 }
24159 }
24160 else if (cursor_type != NO_CURSOR)
24161 {
24162 /* Display current only supports BOX and HOLLOW cursors for images.
24163 So for now, unconditionally use a HOLLOW cursor when cursor is
24164 not a solid box cursor. */
24165 cursor_type = HOLLOW_BOX_CURSOR;
24166 }
24167 }
24168 return cursor_type;
24169 }
24170
24171 /* Cursor is blinked off, so determine how to "toggle" it. */
24172
24173 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24174 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24175 return get_specified_cursor_type (XCDR (alt_cursor), width);
24176
24177 /* Then see if frame has specified a specific blink off cursor type. */
24178 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24179 {
24180 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24181 return FRAME_BLINK_OFF_CURSOR (f);
24182 }
24183
24184 #if 0
24185 /* Some people liked having a permanently visible blinking cursor,
24186 while others had very strong opinions against it. So it was
24187 decided to remove it. KFS 2003-09-03 */
24188
24189 /* Finally perform built-in cursor blinking:
24190 filled box <-> hollow box
24191 wide [h]bar <-> narrow [h]bar
24192 narrow [h]bar <-> no cursor
24193 other type <-> no cursor */
24194
24195 if (cursor_type == FILLED_BOX_CURSOR)
24196 return HOLLOW_BOX_CURSOR;
24197
24198 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24199 {
24200 *width = 1;
24201 return cursor_type;
24202 }
24203 #endif
24204
24205 return NO_CURSOR;
24206 }
24207
24208
24209 /* Notice when the text cursor of window W has been completely
24210 overwritten by a drawing operation that outputs glyphs in AREA
24211 starting at X0 and ending at X1 in the line starting at Y0 and
24212 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24213 the rest of the line after X0 has been written. Y coordinates
24214 are window-relative. */
24215
24216 static void
24217 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24218 int x0, int x1, int y0, int y1)
24219 {
24220 int cx0, cx1, cy0, cy1;
24221 struct glyph_row *row;
24222
24223 if (!w->phys_cursor_on_p)
24224 return;
24225 if (area != TEXT_AREA)
24226 return;
24227
24228 if (w->phys_cursor.vpos < 0
24229 || w->phys_cursor.vpos >= w->current_matrix->nrows
24230 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24231 !(row->enabled_p && row->displays_text_p)))
24232 return;
24233
24234 if (row->cursor_in_fringe_p)
24235 {
24236 row->cursor_in_fringe_p = 0;
24237 draw_fringe_bitmap (w, row, row->reversed_p);
24238 w->phys_cursor_on_p = 0;
24239 return;
24240 }
24241
24242 cx0 = w->phys_cursor.x;
24243 cx1 = cx0 + w->phys_cursor_width;
24244 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24245 return;
24246
24247 /* The cursor image will be completely removed from the
24248 screen if the output area intersects the cursor area in
24249 y-direction. When we draw in [y0 y1[, and some part of
24250 the cursor is at y < y0, that part must have been drawn
24251 before. When scrolling, the cursor is erased before
24252 actually scrolling, so we don't come here. When not
24253 scrolling, the rows above the old cursor row must have
24254 changed, and in this case these rows must have written
24255 over the cursor image.
24256
24257 Likewise if part of the cursor is below y1, with the
24258 exception of the cursor being in the first blank row at
24259 the buffer and window end because update_text_area
24260 doesn't draw that row. (Except when it does, but
24261 that's handled in update_text_area.) */
24262
24263 cy0 = w->phys_cursor.y;
24264 cy1 = cy0 + w->phys_cursor_height;
24265 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24266 return;
24267
24268 w->phys_cursor_on_p = 0;
24269 }
24270
24271 #endif /* HAVE_WINDOW_SYSTEM */
24272
24273 \f
24274 /************************************************************************
24275 Mouse Face
24276 ************************************************************************/
24277
24278 #ifdef HAVE_WINDOW_SYSTEM
24279
24280 /* EXPORT for RIF:
24281 Fix the display of area AREA of overlapping row ROW in window W
24282 with respect to the overlapping part OVERLAPS. */
24283
24284 void
24285 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24286 enum glyph_row_area area, int overlaps)
24287 {
24288 int i, x;
24289
24290 BLOCK_INPUT;
24291
24292 x = 0;
24293 for (i = 0; i < row->used[area];)
24294 {
24295 if (row->glyphs[area][i].overlaps_vertically_p)
24296 {
24297 int start = i, start_x = x;
24298
24299 do
24300 {
24301 x += row->glyphs[area][i].pixel_width;
24302 ++i;
24303 }
24304 while (i < row->used[area]
24305 && row->glyphs[area][i].overlaps_vertically_p);
24306
24307 draw_glyphs (w, start_x, row, area,
24308 start, i,
24309 DRAW_NORMAL_TEXT, overlaps);
24310 }
24311 else
24312 {
24313 x += row->glyphs[area][i].pixel_width;
24314 ++i;
24315 }
24316 }
24317
24318 UNBLOCK_INPUT;
24319 }
24320
24321
24322 /* EXPORT:
24323 Draw the cursor glyph of window W in glyph row ROW. See the
24324 comment of draw_glyphs for the meaning of HL. */
24325
24326 void
24327 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24328 enum draw_glyphs_face hl)
24329 {
24330 /* If cursor hpos is out of bounds, don't draw garbage. This can
24331 happen in mini-buffer windows when switching between echo area
24332 glyphs and mini-buffer. */
24333 if ((row->reversed_p
24334 ? (w->phys_cursor.hpos >= 0)
24335 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24336 {
24337 int on_p = w->phys_cursor_on_p;
24338 int x1;
24339 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24340 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24341 hl, 0);
24342 w->phys_cursor_on_p = on_p;
24343
24344 if (hl == DRAW_CURSOR)
24345 w->phys_cursor_width = x1 - w->phys_cursor.x;
24346 /* When we erase the cursor, and ROW is overlapped by other
24347 rows, make sure that these overlapping parts of other rows
24348 are redrawn. */
24349 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24350 {
24351 w->phys_cursor_width = x1 - w->phys_cursor.x;
24352
24353 if (row > w->current_matrix->rows
24354 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24355 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24356 OVERLAPS_ERASED_CURSOR);
24357
24358 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24359 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24360 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24361 OVERLAPS_ERASED_CURSOR);
24362 }
24363 }
24364 }
24365
24366
24367 /* EXPORT:
24368 Erase the image of a cursor of window W from the screen. */
24369
24370 void
24371 erase_phys_cursor (struct window *w)
24372 {
24373 struct frame *f = XFRAME (w->frame);
24374 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24375 int hpos = w->phys_cursor.hpos;
24376 int vpos = w->phys_cursor.vpos;
24377 int mouse_face_here_p = 0;
24378 struct glyph_matrix *active_glyphs = w->current_matrix;
24379 struct glyph_row *cursor_row;
24380 struct glyph *cursor_glyph;
24381 enum draw_glyphs_face hl;
24382
24383 /* No cursor displayed or row invalidated => nothing to do on the
24384 screen. */
24385 if (w->phys_cursor_type == NO_CURSOR)
24386 goto mark_cursor_off;
24387
24388 /* VPOS >= active_glyphs->nrows means that window has been resized.
24389 Don't bother to erase the cursor. */
24390 if (vpos >= active_glyphs->nrows)
24391 goto mark_cursor_off;
24392
24393 /* If row containing cursor is marked invalid, there is nothing we
24394 can do. */
24395 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24396 if (!cursor_row->enabled_p)
24397 goto mark_cursor_off;
24398
24399 /* If line spacing is > 0, old cursor may only be partially visible in
24400 window after split-window. So adjust visible height. */
24401 cursor_row->visible_height = min (cursor_row->visible_height,
24402 window_text_bottom_y (w) - cursor_row->y);
24403
24404 /* If row is completely invisible, don't attempt to delete a cursor which
24405 isn't there. This can happen if cursor is at top of a window, and
24406 we switch to a buffer with a header line in that window. */
24407 if (cursor_row->visible_height <= 0)
24408 goto mark_cursor_off;
24409
24410 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24411 if (cursor_row->cursor_in_fringe_p)
24412 {
24413 cursor_row->cursor_in_fringe_p = 0;
24414 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24415 goto mark_cursor_off;
24416 }
24417
24418 /* This can happen when the new row is shorter than the old one.
24419 In this case, either draw_glyphs or clear_end_of_line
24420 should have cleared the cursor. Note that we wouldn't be
24421 able to erase the cursor in this case because we don't have a
24422 cursor glyph at hand. */
24423 if ((cursor_row->reversed_p
24424 ? (w->phys_cursor.hpos < 0)
24425 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24426 goto mark_cursor_off;
24427
24428 /* If the cursor is in the mouse face area, redisplay that when
24429 we clear the cursor. */
24430 if (! NILP (hlinfo->mouse_face_window)
24431 && coords_in_mouse_face_p (w, hpos, vpos)
24432 /* Don't redraw the cursor's spot in mouse face if it is at the
24433 end of a line (on a newline). The cursor appears there, but
24434 mouse highlighting does not. */
24435 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24436 mouse_face_here_p = 1;
24437
24438 /* Maybe clear the display under the cursor. */
24439 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24440 {
24441 int x, y, left_x;
24442 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24443 int width;
24444
24445 cursor_glyph = get_phys_cursor_glyph (w);
24446 if (cursor_glyph == NULL)
24447 goto mark_cursor_off;
24448
24449 width = cursor_glyph->pixel_width;
24450 left_x = window_box_left_offset (w, TEXT_AREA);
24451 x = w->phys_cursor.x;
24452 if (x < left_x)
24453 width -= left_x - x;
24454 width = min (width, window_box_width (w, TEXT_AREA) - x);
24455 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24456 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24457
24458 if (width > 0)
24459 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24460 }
24461
24462 /* Erase the cursor by redrawing the character underneath it. */
24463 if (mouse_face_here_p)
24464 hl = DRAW_MOUSE_FACE;
24465 else
24466 hl = DRAW_NORMAL_TEXT;
24467 draw_phys_cursor_glyph (w, cursor_row, hl);
24468
24469 mark_cursor_off:
24470 w->phys_cursor_on_p = 0;
24471 w->phys_cursor_type = NO_CURSOR;
24472 }
24473
24474
24475 /* EXPORT:
24476 Display or clear cursor of window W. If ON is zero, clear the
24477 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24478 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24479
24480 void
24481 display_and_set_cursor (struct window *w, int on,
24482 int hpos, int vpos, int x, int y)
24483 {
24484 struct frame *f = XFRAME (w->frame);
24485 int new_cursor_type;
24486 int new_cursor_width;
24487 int active_cursor;
24488 struct glyph_row *glyph_row;
24489 struct glyph *glyph;
24490
24491 /* This is pointless on invisible frames, and dangerous on garbaged
24492 windows and frames; in the latter case, the frame or window may
24493 be in the midst of changing its size, and x and y may be off the
24494 window. */
24495 if (! FRAME_VISIBLE_P (f)
24496 || FRAME_GARBAGED_P (f)
24497 || vpos >= w->current_matrix->nrows
24498 || hpos >= w->current_matrix->matrix_w)
24499 return;
24500
24501 /* If cursor is off and we want it off, return quickly. */
24502 if (!on && !w->phys_cursor_on_p)
24503 return;
24504
24505 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24506 /* If cursor row is not enabled, we don't really know where to
24507 display the cursor. */
24508 if (!glyph_row->enabled_p)
24509 {
24510 w->phys_cursor_on_p = 0;
24511 return;
24512 }
24513
24514 glyph = NULL;
24515 if (!glyph_row->exact_window_width_line_p
24516 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24517 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24518
24519 xassert (interrupt_input_blocked);
24520
24521 /* Set new_cursor_type to the cursor we want to be displayed. */
24522 new_cursor_type = get_window_cursor_type (w, glyph,
24523 &new_cursor_width, &active_cursor);
24524
24525 /* If cursor is currently being shown and we don't want it to be or
24526 it is in the wrong place, or the cursor type is not what we want,
24527 erase it. */
24528 if (w->phys_cursor_on_p
24529 && (!on
24530 || w->phys_cursor.x != x
24531 || w->phys_cursor.y != y
24532 || new_cursor_type != w->phys_cursor_type
24533 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24534 && new_cursor_width != w->phys_cursor_width)))
24535 erase_phys_cursor (w);
24536
24537 /* Don't check phys_cursor_on_p here because that flag is only set
24538 to zero in some cases where we know that the cursor has been
24539 completely erased, to avoid the extra work of erasing the cursor
24540 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24541 still not be visible, or it has only been partly erased. */
24542 if (on)
24543 {
24544 w->phys_cursor_ascent = glyph_row->ascent;
24545 w->phys_cursor_height = glyph_row->height;
24546
24547 /* Set phys_cursor_.* before x_draw_.* is called because some
24548 of them may need the information. */
24549 w->phys_cursor.x = x;
24550 w->phys_cursor.y = glyph_row->y;
24551 w->phys_cursor.hpos = hpos;
24552 w->phys_cursor.vpos = vpos;
24553 }
24554
24555 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24556 new_cursor_type, new_cursor_width,
24557 on, active_cursor);
24558 }
24559
24560
24561 /* Switch the display of W's cursor on or off, according to the value
24562 of ON. */
24563
24564 static void
24565 update_window_cursor (struct window *w, int on)
24566 {
24567 /* Don't update cursor in windows whose frame is in the process
24568 of being deleted. */
24569 if (w->current_matrix)
24570 {
24571 BLOCK_INPUT;
24572 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24573 w->phys_cursor.x, w->phys_cursor.y);
24574 UNBLOCK_INPUT;
24575 }
24576 }
24577
24578
24579 /* Call update_window_cursor with parameter ON_P on all leaf windows
24580 in the window tree rooted at W. */
24581
24582 static void
24583 update_cursor_in_window_tree (struct window *w, int on_p)
24584 {
24585 while (w)
24586 {
24587 if (!NILP (w->hchild))
24588 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24589 else if (!NILP (w->vchild))
24590 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24591 else
24592 update_window_cursor (w, on_p);
24593
24594 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24595 }
24596 }
24597
24598
24599 /* EXPORT:
24600 Display the cursor on window W, or clear it, according to ON_P.
24601 Don't change the cursor's position. */
24602
24603 void
24604 x_update_cursor (struct frame *f, int on_p)
24605 {
24606 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24607 }
24608
24609
24610 /* EXPORT:
24611 Clear the cursor of window W to background color, and mark the
24612 cursor as not shown. This is used when the text where the cursor
24613 is about to be rewritten. */
24614
24615 void
24616 x_clear_cursor (struct window *w)
24617 {
24618 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24619 update_window_cursor (w, 0);
24620 }
24621
24622 #endif /* HAVE_WINDOW_SYSTEM */
24623
24624 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24625 and MSDOS. */
24626 static void
24627 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24628 int start_hpos, int end_hpos,
24629 enum draw_glyphs_face draw)
24630 {
24631 #ifdef HAVE_WINDOW_SYSTEM
24632 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24633 {
24634 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24635 return;
24636 }
24637 #endif
24638 #if defined (HAVE_GPM) || defined (MSDOS)
24639 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24640 #endif
24641 }
24642
24643 /* Display the active region described by mouse_face_* according to DRAW. */
24644
24645 static void
24646 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24647 {
24648 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24649 struct frame *f = XFRAME (WINDOW_FRAME (w));
24650
24651 if (/* If window is in the process of being destroyed, don't bother
24652 to do anything. */
24653 w->current_matrix != NULL
24654 /* Don't update mouse highlight if hidden */
24655 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24656 /* Recognize when we are called to operate on rows that don't exist
24657 anymore. This can happen when a window is split. */
24658 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24659 {
24660 int phys_cursor_on_p = w->phys_cursor_on_p;
24661 struct glyph_row *row, *first, *last;
24662
24663 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24664 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24665
24666 for (row = first; row <= last && row->enabled_p; ++row)
24667 {
24668 int start_hpos, end_hpos, start_x;
24669
24670 /* For all but the first row, the highlight starts at column 0. */
24671 if (row == first)
24672 {
24673 /* R2L rows have BEG and END in reversed order, but the
24674 screen drawing geometry is always left to right. So
24675 we need to mirror the beginning and end of the
24676 highlighted area in R2L rows. */
24677 if (!row->reversed_p)
24678 {
24679 start_hpos = hlinfo->mouse_face_beg_col;
24680 start_x = hlinfo->mouse_face_beg_x;
24681 }
24682 else if (row == last)
24683 {
24684 start_hpos = hlinfo->mouse_face_end_col;
24685 start_x = hlinfo->mouse_face_end_x;
24686 }
24687 else
24688 {
24689 start_hpos = 0;
24690 start_x = 0;
24691 }
24692 }
24693 else if (row->reversed_p && row == last)
24694 {
24695 start_hpos = hlinfo->mouse_face_end_col;
24696 start_x = hlinfo->mouse_face_end_x;
24697 }
24698 else
24699 {
24700 start_hpos = 0;
24701 start_x = 0;
24702 }
24703
24704 if (row == last)
24705 {
24706 if (!row->reversed_p)
24707 end_hpos = hlinfo->mouse_face_end_col;
24708 else if (row == first)
24709 end_hpos = hlinfo->mouse_face_beg_col;
24710 else
24711 {
24712 end_hpos = row->used[TEXT_AREA];
24713 if (draw == DRAW_NORMAL_TEXT)
24714 row->fill_line_p = 1; /* Clear to end of line */
24715 }
24716 }
24717 else if (row->reversed_p && row == first)
24718 end_hpos = hlinfo->mouse_face_beg_col;
24719 else
24720 {
24721 end_hpos = row->used[TEXT_AREA];
24722 if (draw == DRAW_NORMAL_TEXT)
24723 row->fill_line_p = 1; /* Clear to end of line */
24724 }
24725
24726 if (end_hpos > start_hpos)
24727 {
24728 draw_row_with_mouse_face (w, start_x, row,
24729 start_hpos, end_hpos, draw);
24730
24731 row->mouse_face_p
24732 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24733 }
24734 }
24735
24736 #ifdef HAVE_WINDOW_SYSTEM
24737 /* When we've written over the cursor, arrange for it to
24738 be displayed again. */
24739 if (FRAME_WINDOW_P (f)
24740 && phys_cursor_on_p && !w->phys_cursor_on_p)
24741 {
24742 BLOCK_INPUT;
24743 display_and_set_cursor (w, 1,
24744 w->phys_cursor.hpos, w->phys_cursor.vpos,
24745 w->phys_cursor.x, w->phys_cursor.y);
24746 UNBLOCK_INPUT;
24747 }
24748 #endif /* HAVE_WINDOW_SYSTEM */
24749 }
24750
24751 #ifdef HAVE_WINDOW_SYSTEM
24752 /* Change the mouse cursor. */
24753 if (FRAME_WINDOW_P (f))
24754 {
24755 if (draw == DRAW_NORMAL_TEXT
24756 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24757 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24758 else if (draw == DRAW_MOUSE_FACE)
24759 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24760 else
24761 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24762 }
24763 #endif /* HAVE_WINDOW_SYSTEM */
24764 }
24765
24766 /* EXPORT:
24767 Clear out the mouse-highlighted active region.
24768 Redraw it un-highlighted first. Value is non-zero if mouse
24769 face was actually drawn unhighlighted. */
24770
24771 int
24772 clear_mouse_face (Mouse_HLInfo *hlinfo)
24773 {
24774 int cleared = 0;
24775
24776 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24777 {
24778 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24779 cleared = 1;
24780 }
24781
24782 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24783 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24784 hlinfo->mouse_face_window = Qnil;
24785 hlinfo->mouse_face_overlay = Qnil;
24786 return cleared;
24787 }
24788
24789 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24790 within the mouse face on that window. */
24791 static int
24792 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24793 {
24794 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24795
24796 /* Quickly resolve the easy cases. */
24797 if (!(WINDOWP (hlinfo->mouse_face_window)
24798 && XWINDOW (hlinfo->mouse_face_window) == w))
24799 return 0;
24800 if (vpos < hlinfo->mouse_face_beg_row
24801 || vpos > hlinfo->mouse_face_end_row)
24802 return 0;
24803 if (vpos > hlinfo->mouse_face_beg_row
24804 && vpos < hlinfo->mouse_face_end_row)
24805 return 1;
24806
24807 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24808 {
24809 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24810 {
24811 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24812 return 1;
24813 }
24814 else if ((vpos == hlinfo->mouse_face_beg_row
24815 && hpos >= hlinfo->mouse_face_beg_col)
24816 || (vpos == hlinfo->mouse_face_end_row
24817 && hpos < hlinfo->mouse_face_end_col))
24818 return 1;
24819 }
24820 else
24821 {
24822 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24823 {
24824 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24825 return 1;
24826 }
24827 else if ((vpos == hlinfo->mouse_face_beg_row
24828 && hpos <= hlinfo->mouse_face_beg_col)
24829 || (vpos == hlinfo->mouse_face_end_row
24830 && hpos > hlinfo->mouse_face_end_col))
24831 return 1;
24832 }
24833 return 0;
24834 }
24835
24836
24837 /* EXPORT:
24838 Non-zero if physical cursor of window W is within mouse face. */
24839
24840 int
24841 cursor_in_mouse_face_p (struct window *w)
24842 {
24843 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24844 }
24845
24846
24847 \f
24848 /* Find the glyph rows START_ROW and END_ROW of window W that display
24849 characters between buffer positions START_CHARPOS and END_CHARPOS
24850 (excluding END_CHARPOS). This is similar to row_containing_pos,
24851 but is more accurate when bidi reordering makes buffer positions
24852 change non-linearly with glyph rows. */
24853 static void
24854 rows_from_pos_range (struct window *w,
24855 EMACS_INT start_charpos, EMACS_INT end_charpos,
24856 struct glyph_row **start, struct glyph_row **end)
24857 {
24858 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24859 int last_y = window_text_bottom_y (w);
24860 struct glyph_row *row;
24861
24862 *start = NULL;
24863 *end = NULL;
24864
24865 while (!first->enabled_p
24866 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24867 first++;
24868
24869 /* Find the START row. */
24870 for (row = first;
24871 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24872 row++)
24873 {
24874 /* A row can potentially be the START row if the range of the
24875 characters it displays intersects the range
24876 [START_CHARPOS..END_CHARPOS). */
24877 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24878 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24879 /* See the commentary in row_containing_pos, for the
24880 explanation of the complicated way to check whether
24881 some position is beyond the end of the characters
24882 displayed by a row. */
24883 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24884 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24885 && !row->ends_at_zv_p
24886 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24887 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24888 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24889 && !row->ends_at_zv_p
24890 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24891 {
24892 /* Found a candidate row. Now make sure at least one of the
24893 glyphs it displays has a charpos from the range
24894 [START_CHARPOS..END_CHARPOS).
24895
24896 This is not obvious because bidi reordering could make
24897 buffer positions of a row be 1,2,3,102,101,100, and if we
24898 want to highlight characters in [50..60), we don't want
24899 this row, even though [50..60) does intersect [1..103),
24900 the range of character positions given by the row's start
24901 and end positions. */
24902 struct glyph *g = row->glyphs[TEXT_AREA];
24903 struct glyph *e = g + row->used[TEXT_AREA];
24904
24905 while (g < e)
24906 {
24907 if (BUFFERP (g->object)
24908 && start_charpos <= g->charpos && g->charpos < end_charpos)
24909 *start = row;
24910 g++;
24911 }
24912 if (*start)
24913 break;
24914 }
24915 }
24916
24917 /* Find the END row. */
24918 if (!*start
24919 /* If the last row is partially visible, start looking for END
24920 from that row, instead of starting from FIRST. */
24921 && !(row->enabled_p
24922 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24923 row = first;
24924 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24925 {
24926 struct glyph_row *next = row + 1;
24927
24928 if (!next->enabled_p
24929 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24930 /* The first row >= START whose range of displayed characters
24931 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24932 is the row END + 1. */
24933 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24934 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24935 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24936 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24937 && !next->ends_at_zv_p
24938 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24939 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24940 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24941 && !next->ends_at_zv_p
24942 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24943 {
24944 *end = row;
24945 break;
24946 }
24947 else
24948 {
24949 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24950 but none of the characters it displays are in the range, it is
24951 also END + 1. */
24952 struct glyph *g = next->glyphs[TEXT_AREA];
24953 struct glyph *e = g + next->used[TEXT_AREA];
24954
24955 while (g < e)
24956 {
24957 if (BUFFERP (g->object)
24958 && start_charpos <= g->charpos && g->charpos < end_charpos)
24959 break;
24960 g++;
24961 }
24962 if (g == e)
24963 {
24964 *end = row;
24965 break;
24966 }
24967 }
24968 }
24969 }
24970
24971 /* This function sets the mouse_face_* elements of HLINFO, assuming
24972 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24973 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24974 for the overlay or run of text properties specifying the mouse
24975 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24976 before-string and after-string that must also be highlighted.
24977 COVER_STRING, if non-nil, is a display string that may cover some
24978 or all of the highlighted text. */
24979
24980 static void
24981 mouse_face_from_buffer_pos (Lisp_Object window,
24982 Mouse_HLInfo *hlinfo,
24983 EMACS_INT mouse_charpos,
24984 EMACS_INT start_charpos,
24985 EMACS_INT end_charpos,
24986 Lisp_Object before_string,
24987 Lisp_Object after_string,
24988 Lisp_Object cover_string)
24989 {
24990 struct window *w = XWINDOW (window);
24991 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24992 struct glyph_row *r1, *r2;
24993 struct glyph *glyph, *end;
24994 EMACS_INT ignore, pos;
24995 int x;
24996
24997 xassert (NILP (cover_string) || STRINGP (cover_string));
24998 xassert (NILP (before_string) || STRINGP (before_string));
24999 xassert (NILP (after_string) || STRINGP (after_string));
25000
25001 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25002 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25003 if (r1 == NULL)
25004 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25005 /* If the before-string or display-string contains newlines,
25006 rows_from_pos_range skips to its last row. Move back. */
25007 if (!NILP (before_string) || !NILP (cover_string))
25008 {
25009 struct glyph_row *prev;
25010 while ((prev = r1 - 1, prev >= first)
25011 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25012 && prev->used[TEXT_AREA] > 0)
25013 {
25014 struct glyph *beg = prev->glyphs[TEXT_AREA];
25015 glyph = beg + prev->used[TEXT_AREA];
25016 while (--glyph >= beg && INTEGERP (glyph->object));
25017 if (glyph < beg
25018 || !(EQ (glyph->object, before_string)
25019 || EQ (glyph->object, cover_string)))
25020 break;
25021 r1 = prev;
25022 }
25023 }
25024 if (r2 == NULL)
25025 {
25026 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25027 hlinfo->mouse_face_past_end = 1;
25028 }
25029 else if (!NILP (after_string))
25030 {
25031 /* If the after-string has newlines, advance to its last row. */
25032 struct glyph_row *next;
25033 struct glyph_row *last
25034 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25035
25036 for (next = r2 + 1;
25037 next <= last
25038 && next->used[TEXT_AREA] > 0
25039 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25040 ++next)
25041 r2 = next;
25042 }
25043 /* The rest of the display engine assumes that mouse_face_beg_row is
25044 either above below mouse_face_end_row or identical to it. But
25045 with bidi-reordered continued lines, the row for START_CHARPOS
25046 could be below the row for END_CHARPOS. If so, swap the rows and
25047 store them in correct order. */
25048 if (r1->y > r2->y)
25049 {
25050 struct glyph_row *tem = r2;
25051
25052 r2 = r1;
25053 r1 = tem;
25054 }
25055
25056 hlinfo->mouse_face_beg_y = r1->y;
25057 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25058 hlinfo->mouse_face_end_y = r2->y;
25059 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25060
25061 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25062 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25063 could be anywhere in the row and in any order. The strategy
25064 below is to find the leftmost and the rightmost glyph that
25065 belongs to either of these 3 strings, or whose position is
25066 between START_CHARPOS and END_CHARPOS, and highlight all the
25067 glyphs between those two. This may cover more than just the text
25068 between START_CHARPOS and END_CHARPOS if the range of characters
25069 strides the bidi level boundary, e.g. if the beginning is in R2L
25070 text while the end is in L2R text or vice versa. */
25071 if (!r1->reversed_p)
25072 {
25073 /* This row is in a left to right paragraph. Scan it left to
25074 right. */
25075 glyph = r1->glyphs[TEXT_AREA];
25076 end = glyph + r1->used[TEXT_AREA];
25077 x = r1->x;
25078
25079 /* Skip truncation glyphs at the start of the glyph row. */
25080 if (r1->displays_text_p)
25081 for (; glyph < end
25082 && INTEGERP (glyph->object)
25083 && glyph->charpos < 0;
25084 ++glyph)
25085 x += glyph->pixel_width;
25086
25087 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25088 or COVER_STRING, and the first glyph from buffer whose
25089 position is between START_CHARPOS and END_CHARPOS. */
25090 for (; glyph < end
25091 && !INTEGERP (glyph->object)
25092 && !EQ (glyph->object, cover_string)
25093 && !(BUFFERP (glyph->object)
25094 && (glyph->charpos >= start_charpos
25095 && glyph->charpos < end_charpos));
25096 ++glyph)
25097 {
25098 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25099 are present at buffer positions between START_CHARPOS and
25100 END_CHARPOS, or if they come from an overlay. */
25101 if (EQ (glyph->object, before_string))
25102 {
25103 pos = string_buffer_position (before_string,
25104 start_charpos);
25105 /* If pos == 0, it means before_string came from an
25106 overlay, not from a buffer position. */
25107 if (!pos || (pos >= start_charpos && pos < end_charpos))
25108 break;
25109 }
25110 else if (EQ (glyph->object, after_string))
25111 {
25112 pos = string_buffer_position (after_string, end_charpos);
25113 if (!pos || (pos >= start_charpos && pos < end_charpos))
25114 break;
25115 }
25116 x += glyph->pixel_width;
25117 }
25118 hlinfo->mouse_face_beg_x = x;
25119 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25120 }
25121 else
25122 {
25123 /* This row is in a right to left paragraph. Scan it right to
25124 left. */
25125 struct glyph *g;
25126
25127 end = r1->glyphs[TEXT_AREA] - 1;
25128 glyph = end + r1->used[TEXT_AREA];
25129
25130 /* Skip truncation glyphs at the start of the glyph row. */
25131 if (r1->displays_text_p)
25132 for (; glyph > end
25133 && INTEGERP (glyph->object)
25134 && glyph->charpos < 0;
25135 --glyph)
25136 ;
25137
25138 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25139 or COVER_STRING, and the first glyph from buffer whose
25140 position is between START_CHARPOS and END_CHARPOS. */
25141 for (; glyph > end
25142 && !INTEGERP (glyph->object)
25143 && !EQ (glyph->object, cover_string)
25144 && !(BUFFERP (glyph->object)
25145 && (glyph->charpos >= start_charpos
25146 && glyph->charpos < end_charpos));
25147 --glyph)
25148 {
25149 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25150 are present at buffer positions between START_CHARPOS and
25151 END_CHARPOS, or if they come from an overlay. */
25152 if (EQ (glyph->object, before_string))
25153 {
25154 pos = string_buffer_position (before_string, start_charpos);
25155 /* If pos == 0, it means before_string came from an
25156 overlay, not from a buffer position. */
25157 if (!pos || (pos >= start_charpos && pos < end_charpos))
25158 break;
25159 }
25160 else if (EQ (glyph->object, after_string))
25161 {
25162 pos = string_buffer_position (after_string, end_charpos);
25163 if (!pos || (pos >= start_charpos && pos < end_charpos))
25164 break;
25165 }
25166 }
25167
25168 glyph++; /* first glyph to the right of the highlighted area */
25169 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25170 x += g->pixel_width;
25171 hlinfo->mouse_face_beg_x = x;
25172 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25173 }
25174
25175 /* If the highlight ends in a different row, compute GLYPH and END
25176 for the end row. Otherwise, reuse the values computed above for
25177 the row where the highlight begins. */
25178 if (r2 != r1)
25179 {
25180 if (!r2->reversed_p)
25181 {
25182 glyph = r2->glyphs[TEXT_AREA];
25183 end = glyph + r2->used[TEXT_AREA];
25184 x = r2->x;
25185 }
25186 else
25187 {
25188 end = r2->glyphs[TEXT_AREA] - 1;
25189 glyph = end + r2->used[TEXT_AREA];
25190 }
25191 }
25192
25193 if (!r2->reversed_p)
25194 {
25195 /* Skip truncation and continuation glyphs near the end of the
25196 row, and also blanks and stretch glyphs inserted by
25197 extend_face_to_end_of_line. */
25198 while (end > glyph
25199 && INTEGERP ((end - 1)->object)
25200 && (end - 1)->charpos <= 0)
25201 --end;
25202 /* Scan the rest of the glyph row from the end, looking for the
25203 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25204 COVER_STRING, or whose position is between START_CHARPOS
25205 and END_CHARPOS */
25206 for (--end;
25207 end > glyph
25208 && !INTEGERP (end->object)
25209 && !EQ (end->object, cover_string)
25210 && !(BUFFERP (end->object)
25211 && (end->charpos >= start_charpos
25212 && end->charpos < end_charpos));
25213 --end)
25214 {
25215 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25216 are present at buffer positions between START_CHARPOS and
25217 END_CHARPOS, or if they come from an overlay. */
25218 if (EQ (end->object, before_string))
25219 {
25220 pos = string_buffer_position (before_string, start_charpos);
25221 if (!pos || (pos >= start_charpos && pos < end_charpos))
25222 break;
25223 }
25224 else if (EQ (end->object, after_string))
25225 {
25226 pos = string_buffer_position (after_string, end_charpos);
25227 if (!pos || (pos >= start_charpos && pos < end_charpos))
25228 break;
25229 }
25230 }
25231 /* Find the X coordinate of the last glyph to be highlighted. */
25232 for (; glyph <= end; ++glyph)
25233 x += glyph->pixel_width;
25234
25235 hlinfo->mouse_face_end_x = x;
25236 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25237 }
25238 else
25239 {
25240 /* Skip truncation and continuation glyphs near the end of the
25241 row, and also blanks and stretch glyphs inserted by
25242 extend_face_to_end_of_line. */
25243 x = r2->x;
25244 end++;
25245 while (end < glyph
25246 && INTEGERP (end->object)
25247 && end->charpos <= 0)
25248 {
25249 x += end->pixel_width;
25250 ++end;
25251 }
25252 /* Scan the rest of the glyph row from the end, looking for the
25253 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25254 COVER_STRING, or whose position is between START_CHARPOS
25255 and END_CHARPOS */
25256 for ( ;
25257 end < glyph
25258 && !INTEGERP (end->object)
25259 && !EQ (end->object, cover_string)
25260 && !(BUFFERP (end->object)
25261 && (end->charpos >= start_charpos
25262 && end->charpos < end_charpos));
25263 ++end)
25264 {
25265 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25266 are present at buffer positions between START_CHARPOS and
25267 END_CHARPOS, or if they come from an overlay. */
25268 if (EQ (end->object, before_string))
25269 {
25270 pos = string_buffer_position (before_string, start_charpos);
25271 if (!pos || (pos >= start_charpos && pos < end_charpos))
25272 break;
25273 }
25274 else if (EQ (end->object, after_string))
25275 {
25276 pos = string_buffer_position (after_string, end_charpos);
25277 if (!pos || (pos >= start_charpos && pos < end_charpos))
25278 break;
25279 }
25280 x += end->pixel_width;
25281 }
25282 hlinfo->mouse_face_end_x = x;
25283 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25284 }
25285
25286 hlinfo->mouse_face_window = window;
25287 hlinfo->mouse_face_face_id
25288 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25289 mouse_charpos + 1,
25290 !hlinfo->mouse_face_hidden, -1);
25291 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25292 }
25293
25294 /* The following function is not used anymore (replaced with
25295 mouse_face_from_string_pos), but I leave it here for the time
25296 being, in case someone would. */
25297
25298 #if 0 /* not used */
25299
25300 /* Find the position of the glyph for position POS in OBJECT in
25301 window W's current matrix, and return in *X, *Y the pixel
25302 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25303
25304 RIGHT_P non-zero means return the position of the right edge of the
25305 glyph, RIGHT_P zero means return the left edge position.
25306
25307 If no glyph for POS exists in the matrix, return the position of
25308 the glyph with the next smaller position that is in the matrix, if
25309 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25310 exists in the matrix, return the position of the glyph with the
25311 next larger position in OBJECT.
25312
25313 Value is non-zero if a glyph was found. */
25314
25315 static int
25316 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25317 int *hpos, int *vpos, int *x, int *y, int right_p)
25318 {
25319 int yb = window_text_bottom_y (w);
25320 struct glyph_row *r;
25321 struct glyph *best_glyph = NULL;
25322 struct glyph_row *best_row = NULL;
25323 int best_x = 0;
25324
25325 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25326 r->enabled_p && r->y < yb;
25327 ++r)
25328 {
25329 struct glyph *g = r->glyphs[TEXT_AREA];
25330 struct glyph *e = g + r->used[TEXT_AREA];
25331 int gx;
25332
25333 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25334 if (EQ (g->object, object))
25335 {
25336 if (g->charpos == pos)
25337 {
25338 best_glyph = g;
25339 best_x = gx;
25340 best_row = r;
25341 goto found;
25342 }
25343 else if (best_glyph == NULL
25344 || ((eabs (g->charpos - pos)
25345 < eabs (best_glyph->charpos - pos))
25346 && (right_p
25347 ? g->charpos < pos
25348 : g->charpos > pos)))
25349 {
25350 best_glyph = g;
25351 best_x = gx;
25352 best_row = r;
25353 }
25354 }
25355 }
25356
25357 found:
25358
25359 if (best_glyph)
25360 {
25361 *x = best_x;
25362 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25363
25364 if (right_p)
25365 {
25366 *x += best_glyph->pixel_width;
25367 ++*hpos;
25368 }
25369
25370 *y = best_row->y;
25371 *vpos = best_row - w->current_matrix->rows;
25372 }
25373
25374 return best_glyph != NULL;
25375 }
25376 #endif /* not used */
25377
25378 /* Find the positions of the first and the last glyphs in window W's
25379 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25380 (assumed to be a string), and return in HLINFO's mouse_face_*
25381 members the pixel and column/row coordinates of those glyphs. */
25382
25383 static void
25384 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25385 Lisp_Object object,
25386 EMACS_INT startpos, EMACS_INT endpos)
25387 {
25388 int yb = window_text_bottom_y (w);
25389 struct glyph_row *r;
25390 struct glyph *g, *e;
25391 int gx;
25392 int found = 0;
25393
25394 /* Find the glyph row with at least one position in the range
25395 [STARTPOS..ENDPOS], and the first glyph in that row whose
25396 position belongs to that range. */
25397 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25398 r->enabled_p && r->y < yb;
25399 ++r)
25400 {
25401 if (!r->reversed_p)
25402 {
25403 g = r->glyphs[TEXT_AREA];
25404 e = g + r->used[TEXT_AREA];
25405 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25406 if (EQ (g->object, object)
25407 && startpos <= g->charpos && g->charpos <= endpos)
25408 {
25409 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25410 hlinfo->mouse_face_beg_y = r->y;
25411 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25412 hlinfo->mouse_face_beg_x = gx;
25413 found = 1;
25414 break;
25415 }
25416 }
25417 else
25418 {
25419 struct glyph *g1;
25420
25421 e = r->glyphs[TEXT_AREA];
25422 g = e + r->used[TEXT_AREA];
25423 for ( ; g > e; --g)
25424 if (EQ ((g-1)->object, object)
25425 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25426 {
25427 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25428 hlinfo->mouse_face_beg_y = r->y;
25429 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25430 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25431 gx += g1->pixel_width;
25432 hlinfo->mouse_face_beg_x = gx;
25433 found = 1;
25434 break;
25435 }
25436 }
25437 if (found)
25438 break;
25439 }
25440
25441 if (!found)
25442 return;
25443
25444 /* Starting with the next row, look for the first row which does NOT
25445 include any glyphs whose positions are in the range. */
25446 for (++r; r->enabled_p && r->y < yb; ++r)
25447 {
25448 g = r->glyphs[TEXT_AREA];
25449 e = g + r->used[TEXT_AREA];
25450 found = 0;
25451 for ( ; g < e; ++g)
25452 if (EQ (g->object, object)
25453 && startpos <= g->charpos && g->charpos <= endpos)
25454 {
25455 found = 1;
25456 break;
25457 }
25458 if (!found)
25459 break;
25460 }
25461
25462 /* The highlighted region ends on the previous row. */
25463 r--;
25464
25465 /* Set the end row and its vertical pixel coordinate. */
25466 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25467 hlinfo->mouse_face_end_y = r->y;
25468
25469 /* Compute and set the end column and the end column's horizontal
25470 pixel coordinate. */
25471 if (!r->reversed_p)
25472 {
25473 g = r->glyphs[TEXT_AREA];
25474 e = g + r->used[TEXT_AREA];
25475 for ( ; e > g; --e)
25476 if (EQ ((e-1)->object, object)
25477 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25478 break;
25479 hlinfo->mouse_face_end_col = e - g;
25480
25481 for (gx = r->x; g < e; ++g)
25482 gx += g->pixel_width;
25483 hlinfo->mouse_face_end_x = gx;
25484 }
25485 else
25486 {
25487 e = r->glyphs[TEXT_AREA];
25488 g = e + r->used[TEXT_AREA];
25489 for (gx = r->x ; e < g; ++e)
25490 {
25491 if (EQ (e->object, object)
25492 && startpos <= e->charpos && e->charpos <= endpos)
25493 break;
25494 gx += e->pixel_width;
25495 }
25496 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25497 hlinfo->mouse_face_end_x = gx;
25498 }
25499 }
25500
25501 #ifdef HAVE_WINDOW_SYSTEM
25502
25503 /* See if position X, Y is within a hot-spot of an image. */
25504
25505 static int
25506 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25507 {
25508 if (!CONSP (hot_spot))
25509 return 0;
25510
25511 if (EQ (XCAR (hot_spot), Qrect))
25512 {
25513 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25514 Lisp_Object rect = XCDR (hot_spot);
25515 Lisp_Object tem;
25516 if (!CONSP (rect))
25517 return 0;
25518 if (!CONSP (XCAR (rect)))
25519 return 0;
25520 if (!CONSP (XCDR (rect)))
25521 return 0;
25522 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25523 return 0;
25524 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25525 return 0;
25526 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25527 return 0;
25528 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25529 return 0;
25530 return 1;
25531 }
25532 else if (EQ (XCAR (hot_spot), Qcircle))
25533 {
25534 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25535 Lisp_Object circ = XCDR (hot_spot);
25536 Lisp_Object lr, lx0, ly0;
25537 if (CONSP (circ)
25538 && CONSP (XCAR (circ))
25539 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25540 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25541 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25542 {
25543 double r = XFLOATINT (lr);
25544 double dx = XINT (lx0) - x;
25545 double dy = XINT (ly0) - y;
25546 return (dx * dx + dy * dy <= r * r);
25547 }
25548 }
25549 else if (EQ (XCAR (hot_spot), Qpoly))
25550 {
25551 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25552 if (VECTORP (XCDR (hot_spot)))
25553 {
25554 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25555 Lisp_Object *poly = v->contents;
25556 int n = v->header.size;
25557 int i;
25558 int inside = 0;
25559 Lisp_Object lx, ly;
25560 int x0, y0;
25561
25562 /* Need an even number of coordinates, and at least 3 edges. */
25563 if (n < 6 || n & 1)
25564 return 0;
25565
25566 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25567 If count is odd, we are inside polygon. Pixels on edges
25568 may or may not be included depending on actual geometry of the
25569 polygon. */
25570 if ((lx = poly[n-2], !INTEGERP (lx))
25571 || (ly = poly[n-1], !INTEGERP (lx)))
25572 return 0;
25573 x0 = XINT (lx), y0 = XINT (ly);
25574 for (i = 0; i < n; i += 2)
25575 {
25576 int x1 = x0, y1 = y0;
25577 if ((lx = poly[i], !INTEGERP (lx))
25578 || (ly = poly[i+1], !INTEGERP (ly)))
25579 return 0;
25580 x0 = XINT (lx), y0 = XINT (ly);
25581
25582 /* Does this segment cross the X line? */
25583 if (x0 >= x)
25584 {
25585 if (x1 >= x)
25586 continue;
25587 }
25588 else if (x1 < x)
25589 continue;
25590 if (y > y0 && y > y1)
25591 continue;
25592 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25593 inside = !inside;
25594 }
25595 return inside;
25596 }
25597 }
25598 return 0;
25599 }
25600
25601 Lisp_Object
25602 find_hot_spot (Lisp_Object map, int x, int y)
25603 {
25604 while (CONSP (map))
25605 {
25606 if (CONSP (XCAR (map))
25607 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25608 return XCAR (map);
25609 map = XCDR (map);
25610 }
25611
25612 return Qnil;
25613 }
25614
25615 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25616 3, 3, 0,
25617 doc: /* Lookup in image map MAP coordinates X and Y.
25618 An image map is an alist where each element has the format (AREA ID PLIST).
25619 An AREA is specified as either a rectangle, a circle, or a polygon:
25620 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25621 pixel coordinates of the upper left and bottom right corners.
25622 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25623 and the radius of the circle; r may be a float or integer.
25624 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25625 vector describes one corner in the polygon.
25626 Returns the alist element for the first matching AREA in MAP. */)
25627 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25628 {
25629 if (NILP (map))
25630 return Qnil;
25631
25632 CHECK_NUMBER (x);
25633 CHECK_NUMBER (y);
25634
25635 return find_hot_spot (map, XINT (x), XINT (y));
25636 }
25637
25638
25639 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25640 static void
25641 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25642 {
25643 /* Do not change cursor shape while dragging mouse. */
25644 if (!NILP (do_mouse_tracking))
25645 return;
25646
25647 if (!NILP (pointer))
25648 {
25649 if (EQ (pointer, Qarrow))
25650 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25651 else if (EQ (pointer, Qhand))
25652 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25653 else if (EQ (pointer, Qtext))
25654 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25655 else if (EQ (pointer, intern ("hdrag")))
25656 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25657 #ifdef HAVE_X_WINDOWS
25658 else if (EQ (pointer, intern ("vdrag")))
25659 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25660 #endif
25661 else if (EQ (pointer, intern ("hourglass")))
25662 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25663 else if (EQ (pointer, Qmodeline))
25664 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25665 else
25666 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25667 }
25668
25669 if (cursor != No_Cursor)
25670 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25671 }
25672
25673 #endif /* HAVE_WINDOW_SYSTEM */
25674
25675 /* Take proper action when mouse has moved to the mode or header line
25676 or marginal area AREA of window W, x-position X and y-position Y.
25677 X is relative to the start of the text display area of W, so the
25678 width of bitmap areas and scroll bars must be subtracted to get a
25679 position relative to the start of the mode line. */
25680
25681 static void
25682 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25683 enum window_part area)
25684 {
25685 struct window *w = XWINDOW (window);
25686 struct frame *f = XFRAME (w->frame);
25687 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25688 #ifdef HAVE_WINDOW_SYSTEM
25689 Display_Info *dpyinfo;
25690 #endif
25691 Cursor cursor = No_Cursor;
25692 Lisp_Object pointer = Qnil;
25693 int dx, dy, width, height;
25694 EMACS_INT charpos;
25695 Lisp_Object string, object = Qnil;
25696 Lisp_Object pos, help;
25697
25698 Lisp_Object mouse_face;
25699 int original_x_pixel = x;
25700 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25701 struct glyph_row *row;
25702
25703 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25704 {
25705 int x0;
25706 struct glyph *end;
25707
25708 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25709 returns them in row/column units! */
25710 string = mode_line_string (w, area, &x, &y, &charpos,
25711 &object, &dx, &dy, &width, &height);
25712
25713 row = (area == ON_MODE_LINE
25714 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25715 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25716
25717 /* Find the glyph under the mouse pointer. */
25718 if (row->mode_line_p && row->enabled_p)
25719 {
25720 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25721 end = glyph + row->used[TEXT_AREA];
25722
25723 for (x0 = original_x_pixel;
25724 glyph < end && x0 >= glyph->pixel_width;
25725 ++glyph)
25726 x0 -= glyph->pixel_width;
25727
25728 if (glyph >= end)
25729 glyph = NULL;
25730 }
25731 }
25732 else
25733 {
25734 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25735 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25736 returns them in row/column units! */
25737 string = marginal_area_string (w, area, &x, &y, &charpos,
25738 &object, &dx, &dy, &width, &height);
25739 }
25740
25741 help = Qnil;
25742
25743 #ifdef HAVE_WINDOW_SYSTEM
25744 if (IMAGEP (object))
25745 {
25746 Lisp_Object image_map, hotspot;
25747 if ((image_map = Fplist_get (XCDR (object), QCmap),
25748 !NILP (image_map))
25749 && (hotspot = find_hot_spot (image_map, dx, dy),
25750 CONSP (hotspot))
25751 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25752 {
25753 Lisp_Object plist;
25754
25755 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25756 If so, we could look for mouse-enter, mouse-leave
25757 properties in PLIST (and do something...). */
25758 hotspot = XCDR (hotspot);
25759 if (CONSP (hotspot)
25760 && (plist = XCAR (hotspot), CONSP (plist)))
25761 {
25762 pointer = Fplist_get (plist, Qpointer);
25763 if (NILP (pointer))
25764 pointer = Qhand;
25765 help = Fplist_get (plist, Qhelp_echo);
25766 if (!NILP (help))
25767 {
25768 help_echo_string = help;
25769 /* Is this correct? ++kfs */
25770 XSETWINDOW (help_echo_window, w);
25771 help_echo_object = w->buffer;
25772 help_echo_pos = charpos;
25773 }
25774 }
25775 }
25776 if (NILP (pointer))
25777 pointer = Fplist_get (XCDR (object), QCpointer);
25778 }
25779 #endif /* HAVE_WINDOW_SYSTEM */
25780
25781 if (STRINGP (string))
25782 {
25783 pos = make_number (charpos);
25784 /* If we're on a string with `help-echo' text property, arrange
25785 for the help to be displayed. This is done by setting the
25786 global variable help_echo_string to the help string. */
25787 if (NILP (help))
25788 {
25789 help = Fget_text_property (pos, Qhelp_echo, string);
25790 if (!NILP (help))
25791 {
25792 help_echo_string = help;
25793 XSETWINDOW (help_echo_window, w);
25794 help_echo_object = string;
25795 help_echo_pos = charpos;
25796 }
25797 }
25798
25799 #ifdef HAVE_WINDOW_SYSTEM
25800 if (FRAME_WINDOW_P (f))
25801 {
25802 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25803 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25804 if (NILP (pointer))
25805 pointer = Fget_text_property (pos, Qpointer, string);
25806
25807 /* Change the mouse pointer according to what is under X/Y. */
25808 if (NILP (pointer)
25809 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25810 {
25811 Lisp_Object map;
25812 map = Fget_text_property (pos, Qlocal_map, string);
25813 if (!KEYMAPP (map))
25814 map = Fget_text_property (pos, Qkeymap, string);
25815 if (!KEYMAPP (map))
25816 cursor = dpyinfo->vertical_scroll_bar_cursor;
25817 }
25818 }
25819 #endif
25820
25821 /* Change the mouse face according to what is under X/Y. */
25822 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25823 if (!NILP (mouse_face)
25824 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25825 && glyph)
25826 {
25827 Lisp_Object b, e;
25828
25829 struct glyph * tmp_glyph;
25830
25831 int gpos;
25832 int gseq_length;
25833 int total_pixel_width;
25834 EMACS_INT begpos, endpos, ignore;
25835
25836 int vpos, hpos;
25837
25838 b = Fprevious_single_property_change (make_number (charpos + 1),
25839 Qmouse_face, string, Qnil);
25840 if (NILP (b))
25841 begpos = 0;
25842 else
25843 begpos = XINT (b);
25844
25845 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25846 if (NILP (e))
25847 endpos = SCHARS (string);
25848 else
25849 endpos = XINT (e);
25850
25851 /* Calculate the glyph position GPOS of GLYPH in the
25852 displayed string, relative to the beginning of the
25853 highlighted part of the string.
25854
25855 Note: GPOS is different from CHARPOS. CHARPOS is the
25856 position of GLYPH in the internal string object. A mode
25857 line string format has structures which are converted to
25858 a flattened string by the Emacs Lisp interpreter. The
25859 internal string is an element of those structures. The
25860 displayed string is the flattened string. */
25861 tmp_glyph = row_start_glyph;
25862 while (tmp_glyph < glyph
25863 && (!(EQ (tmp_glyph->object, glyph->object)
25864 && begpos <= tmp_glyph->charpos
25865 && tmp_glyph->charpos < endpos)))
25866 tmp_glyph++;
25867 gpos = glyph - tmp_glyph;
25868
25869 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25870 the highlighted part of the displayed string to which
25871 GLYPH belongs. Note: GSEQ_LENGTH is different from
25872 SCHARS (STRING), because the latter returns the length of
25873 the internal string. */
25874 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25875 tmp_glyph > glyph
25876 && (!(EQ (tmp_glyph->object, glyph->object)
25877 && begpos <= tmp_glyph->charpos
25878 && tmp_glyph->charpos < endpos));
25879 tmp_glyph--)
25880 ;
25881 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25882
25883 /* Calculate the total pixel width of all the glyphs between
25884 the beginning of the highlighted area and GLYPH. */
25885 total_pixel_width = 0;
25886 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25887 total_pixel_width += tmp_glyph->pixel_width;
25888
25889 /* Pre calculation of re-rendering position. Note: X is in
25890 column units here, after the call to mode_line_string or
25891 marginal_area_string. */
25892 hpos = x - gpos;
25893 vpos = (area == ON_MODE_LINE
25894 ? (w->current_matrix)->nrows - 1
25895 : 0);
25896
25897 /* If GLYPH's position is included in the region that is
25898 already drawn in mouse face, we have nothing to do. */
25899 if ( EQ (window, hlinfo->mouse_face_window)
25900 && (!row->reversed_p
25901 ? (hlinfo->mouse_face_beg_col <= hpos
25902 && hpos < hlinfo->mouse_face_end_col)
25903 /* In R2L rows we swap BEG and END, see below. */
25904 : (hlinfo->mouse_face_end_col <= hpos
25905 && hpos < hlinfo->mouse_face_beg_col))
25906 && hlinfo->mouse_face_beg_row == vpos )
25907 return;
25908
25909 if (clear_mouse_face (hlinfo))
25910 cursor = No_Cursor;
25911
25912 if (!row->reversed_p)
25913 {
25914 hlinfo->mouse_face_beg_col = hpos;
25915 hlinfo->mouse_face_beg_x = original_x_pixel
25916 - (total_pixel_width + dx);
25917 hlinfo->mouse_face_end_col = hpos + gseq_length;
25918 hlinfo->mouse_face_end_x = 0;
25919 }
25920 else
25921 {
25922 /* In R2L rows, show_mouse_face expects BEG and END
25923 coordinates to be swapped. */
25924 hlinfo->mouse_face_end_col = hpos;
25925 hlinfo->mouse_face_end_x = original_x_pixel
25926 - (total_pixel_width + dx);
25927 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25928 hlinfo->mouse_face_beg_x = 0;
25929 }
25930
25931 hlinfo->mouse_face_beg_row = vpos;
25932 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25933 hlinfo->mouse_face_beg_y = 0;
25934 hlinfo->mouse_face_end_y = 0;
25935 hlinfo->mouse_face_past_end = 0;
25936 hlinfo->mouse_face_window = window;
25937
25938 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25939 charpos,
25940 0, 0, 0,
25941 &ignore,
25942 glyph->face_id,
25943 1);
25944 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25945
25946 if (NILP (pointer))
25947 pointer = Qhand;
25948 }
25949 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25950 clear_mouse_face (hlinfo);
25951 }
25952 #ifdef HAVE_WINDOW_SYSTEM
25953 if (FRAME_WINDOW_P (f))
25954 define_frame_cursor1 (f, cursor, pointer);
25955 #endif
25956 }
25957
25958
25959 /* EXPORT:
25960 Take proper action when the mouse has moved to position X, Y on
25961 frame F as regards highlighting characters that have mouse-face
25962 properties. Also de-highlighting chars where the mouse was before.
25963 X and Y can be negative or out of range. */
25964
25965 void
25966 note_mouse_highlight (struct frame *f, int x, int y)
25967 {
25968 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25969 enum window_part part;
25970 Lisp_Object window;
25971 struct window *w;
25972 Cursor cursor = No_Cursor;
25973 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25974 struct buffer *b;
25975
25976 /* When a menu is active, don't highlight because this looks odd. */
25977 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25978 if (popup_activated ())
25979 return;
25980 #endif
25981
25982 if (NILP (Vmouse_highlight)
25983 || !f->glyphs_initialized_p
25984 || f->pointer_invisible)
25985 return;
25986
25987 hlinfo->mouse_face_mouse_x = x;
25988 hlinfo->mouse_face_mouse_y = y;
25989 hlinfo->mouse_face_mouse_frame = f;
25990
25991 if (hlinfo->mouse_face_defer)
25992 return;
25993
25994 if (gc_in_progress)
25995 {
25996 hlinfo->mouse_face_deferred_gc = 1;
25997 return;
25998 }
25999
26000 /* Which window is that in? */
26001 window = window_from_coordinates (f, x, y, &part, 1);
26002
26003 /* If we were displaying active text in another window, clear that.
26004 Also clear if we move out of text area in same window. */
26005 if (! EQ (window, hlinfo->mouse_face_window)
26006 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26007 && !NILP (hlinfo->mouse_face_window)))
26008 clear_mouse_face (hlinfo);
26009
26010 /* Not on a window -> return. */
26011 if (!WINDOWP (window))
26012 return;
26013
26014 /* Reset help_echo_string. It will get recomputed below. */
26015 help_echo_string = Qnil;
26016
26017 /* Convert to window-relative pixel coordinates. */
26018 w = XWINDOW (window);
26019 frame_to_window_pixel_xy (w, &x, &y);
26020
26021 #ifdef HAVE_WINDOW_SYSTEM
26022 /* Handle tool-bar window differently since it doesn't display a
26023 buffer. */
26024 if (EQ (window, f->tool_bar_window))
26025 {
26026 note_tool_bar_highlight (f, x, y);
26027 return;
26028 }
26029 #endif
26030
26031 /* Mouse is on the mode, header line or margin? */
26032 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26033 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26034 {
26035 note_mode_line_or_margin_highlight (window, x, y, part);
26036 return;
26037 }
26038
26039 #ifdef HAVE_WINDOW_SYSTEM
26040 if (part == ON_VERTICAL_BORDER)
26041 {
26042 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26043 help_echo_string = build_string ("drag-mouse-1: resize");
26044 }
26045 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26046 || part == ON_SCROLL_BAR)
26047 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26048 else
26049 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26050 #endif
26051
26052 /* Are we in a window whose display is up to date?
26053 And verify the buffer's text has not changed. */
26054 b = XBUFFER (w->buffer);
26055 if (part == ON_TEXT
26056 && EQ (w->window_end_valid, w->buffer)
26057 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26058 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26059 {
26060 int hpos, vpos, i, dx, dy, area;
26061 EMACS_INT pos;
26062 struct glyph *glyph;
26063 Lisp_Object object;
26064 Lisp_Object mouse_face = Qnil, position;
26065 Lisp_Object *overlay_vec = NULL;
26066 int noverlays;
26067 struct buffer *obuf;
26068 EMACS_INT obegv, ozv;
26069 int same_region;
26070
26071 /* Find the glyph under X/Y. */
26072 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26073
26074 #ifdef HAVE_WINDOW_SYSTEM
26075 /* Look for :pointer property on image. */
26076 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26077 {
26078 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26079 if (img != NULL && IMAGEP (img->spec))
26080 {
26081 Lisp_Object image_map, hotspot;
26082 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26083 !NILP (image_map))
26084 && (hotspot = find_hot_spot (image_map,
26085 glyph->slice.img.x + dx,
26086 glyph->slice.img.y + dy),
26087 CONSP (hotspot))
26088 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26089 {
26090 Lisp_Object plist;
26091
26092 /* Could check XCAR (hotspot) to see if we enter/leave
26093 this hot-spot.
26094 If so, we could look for mouse-enter, mouse-leave
26095 properties in PLIST (and do something...). */
26096 hotspot = XCDR (hotspot);
26097 if (CONSP (hotspot)
26098 && (plist = XCAR (hotspot), CONSP (plist)))
26099 {
26100 pointer = Fplist_get (plist, Qpointer);
26101 if (NILP (pointer))
26102 pointer = Qhand;
26103 help_echo_string = Fplist_get (plist, Qhelp_echo);
26104 if (!NILP (help_echo_string))
26105 {
26106 help_echo_window = window;
26107 help_echo_object = glyph->object;
26108 help_echo_pos = glyph->charpos;
26109 }
26110 }
26111 }
26112 if (NILP (pointer))
26113 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26114 }
26115 }
26116 #endif /* HAVE_WINDOW_SYSTEM */
26117
26118 /* Clear mouse face if X/Y not over text. */
26119 if (glyph == NULL
26120 || area != TEXT_AREA
26121 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26122 /* Glyph's OBJECT is an integer for glyphs inserted by the
26123 display engine for its internal purposes, like truncation
26124 and continuation glyphs and blanks beyond the end of
26125 line's text on text terminals. If we are over such a
26126 glyph, we are not over any text. */
26127 || INTEGERP (glyph->object)
26128 /* R2L rows have a stretch glyph at their front, which
26129 stands for no text, whereas L2R rows have no glyphs at
26130 all beyond the end of text. Treat such stretch glyphs
26131 like we do with NULL glyphs in L2R rows. */
26132 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26133 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26134 && glyph->type == STRETCH_GLYPH
26135 && glyph->avoid_cursor_p))
26136 {
26137 if (clear_mouse_face (hlinfo))
26138 cursor = No_Cursor;
26139 #ifdef HAVE_WINDOW_SYSTEM
26140 if (FRAME_WINDOW_P (f) && NILP (pointer))
26141 {
26142 if (area != TEXT_AREA)
26143 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26144 else
26145 pointer = Vvoid_text_area_pointer;
26146 }
26147 #endif
26148 goto set_cursor;
26149 }
26150
26151 pos = glyph->charpos;
26152 object = glyph->object;
26153 if (!STRINGP (object) && !BUFFERP (object))
26154 goto set_cursor;
26155
26156 /* If we get an out-of-range value, return now; avoid an error. */
26157 if (BUFFERP (object) && pos > BUF_Z (b))
26158 goto set_cursor;
26159
26160 /* Make the window's buffer temporarily current for
26161 overlays_at and compute_char_face. */
26162 obuf = current_buffer;
26163 current_buffer = b;
26164 obegv = BEGV;
26165 ozv = ZV;
26166 BEGV = BEG;
26167 ZV = Z;
26168
26169 /* Is this char mouse-active or does it have help-echo? */
26170 position = make_number (pos);
26171
26172 if (BUFFERP (object))
26173 {
26174 /* Put all the overlays we want in a vector in overlay_vec. */
26175 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26176 /* Sort overlays into increasing priority order. */
26177 noverlays = sort_overlays (overlay_vec, noverlays, w);
26178 }
26179 else
26180 noverlays = 0;
26181
26182 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26183
26184 if (same_region)
26185 cursor = No_Cursor;
26186
26187 /* Check mouse-face highlighting. */
26188 if (! same_region
26189 /* If there exists an overlay with mouse-face overlapping
26190 the one we are currently highlighting, we have to
26191 check if we enter the overlapping overlay, and then
26192 highlight only that. */
26193 || (OVERLAYP (hlinfo->mouse_face_overlay)
26194 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26195 {
26196 /* Find the highest priority overlay with a mouse-face. */
26197 Lisp_Object overlay = Qnil;
26198 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26199 {
26200 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26201 if (!NILP (mouse_face))
26202 overlay = overlay_vec[i];
26203 }
26204
26205 /* If we're highlighting the same overlay as before, there's
26206 no need to do that again. */
26207 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26208 goto check_help_echo;
26209 hlinfo->mouse_face_overlay = overlay;
26210
26211 /* Clear the display of the old active region, if any. */
26212 if (clear_mouse_face (hlinfo))
26213 cursor = No_Cursor;
26214
26215 /* If no overlay applies, get a text property. */
26216 if (NILP (overlay))
26217 mouse_face = Fget_text_property (position, Qmouse_face, object);
26218
26219 /* Next, compute the bounds of the mouse highlighting and
26220 display it. */
26221 if (!NILP (mouse_face) && STRINGP (object))
26222 {
26223 /* The mouse-highlighting comes from a display string
26224 with a mouse-face. */
26225 Lisp_Object s, e;
26226 EMACS_INT ignore;
26227
26228 s = Fprevious_single_property_change
26229 (make_number (pos + 1), Qmouse_face, object, Qnil);
26230 e = Fnext_single_property_change
26231 (position, Qmouse_face, object, Qnil);
26232 if (NILP (s))
26233 s = make_number (0);
26234 if (NILP (e))
26235 e = make_number (SCHARS (object) - 1);
26236 mouse_face_from_string_pos (w, hlinfo, object,
26237 XINT (s), XINT (e));
26238 hlinfo->mouse_face_past_end = 0;
26239 hlinfo->mouse_face_window = window;
26240 hlinfo->mouse_face_face_id
26241 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26242 glyph->face_id, 1);
26243 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26244 cursor = No_Cursor;
26245 }
26246 else
26247 {
26248 /* The mouse-highlighting, if any, comes from an overlay
26249 or text property in the buffer. */
26250 Lisp_Object buffer IF_LINT (= Qnil);
26251 Lisp_Object cover_string IF_LINT (= Qnil);
26252
26253 if (STRINGP (object))
26254 {
26255 /* If we are on a display string with no mouse-face,
26256 check if the text under it has one. */
26257 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26258 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26259 pos = string_buffer_position (object, start);
26260 if (pos > 0)
26261 {
26262 mouse_face = get_char_property_and_overlay
26263 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26264 buffer = w->buffer;
26265 cover_string = object;
26266 }
26267 }
26268 else
26269 {
26270 buffer = object;
26271 cover_string = Qnil;
26272 }
26273
26274 if (!NILP (mouse_face))
26275 {
26276 Lisp_Object before, after;
26277 Lisp_Object before_string, after_string;
26278 /* To correctly find the limits of mouse highlight
26279 in a bidi-reordered buffer, we must not use the
26280 optimization of limiting the search in
26281 previous-single-property-change and
26282 next-single-property-change, because
26283 rows_from_pos_range needs the real start and end
26284 positions to DTRT in this case. That's because
26285 the first row visible in a window does not
26286 necessarily display the character whose position
26287 is the smallest. */
26288 Lisp_Object lim1 =
26289 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26290 ? Fmarker_position (w->start)
26291 : Qnil;
26292 Lisp_Object lim2 =
26293 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26294 ? make_number (BUF_Z (XBUFFER (buffer))
26295 - XFASTINT (w->window_end_pos))
26296 : Qnil;
26297
26298 if (NILP (overlay))
26299 {
26300 /* Handle the text property case. */
26301 before = Fprevious_single_property_change
26302 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26303 after = Fnext_single_property_change
26304 (make_number (pos), Qmouse_face, buffer, lim2);
26305 before_string = after_string = Qnil;
26306 }
26307 else
26308 {
26309 /* Handle the overlay case. */
26310 before = Foverlay_start (overlay);
26311 after = Foverlay_end (overlay);
26312 before_string = Foverlay_get (overlay, Qbefore_string);
26313 after_string = Foverlay_get (overlay, Qafter_string);
26314
26315 if (!STRINGP (before_string)) before_string = Qnil;
26316 if (!STRINGP (after_string)) after_string = Qnil;
26317 }
26318
26319 mouse_face_from_buffer_pos (window, hlinfo, pos,
26320 XFASTINT (before),
26321 XFASTINT (after),
26322 before_string, after_string,
26323 cover_string);
26324 cursor = No_Cursor;
26325 }
26326 }
26327 }
26328
26329 check_help_echo:
26330
26331 /* Look for a `help-echo' property. */
26332 if (NILP (help_echo_string)) {
26333 Lisp_Object help, overlay;
26334
26335 /* Check overlays first. */
26336 help = overlay = Qnil;
26337 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26338 {
26339 overlay = overlay_vec[i];
26340 help = Foverlay_get (overlay, Qhelp_echo);
26341 }
26342
26343 if (!NILP (help))
26344 {
26345 help_echo_string = help;
26346 help_echo_window = window;
26347 help_echo_object = overlay;
26348 help_echo_pos = pos;
26349 }
26350 else
26351 {
26352 Lisp_Object obj = glyph->object;
26353 EMACS_INT charpos = glyph->charpos;
26354
26355 /* Try text properties. */
26356 if (STRINGP (obj)
26357 && charpos >= 0
26358 && charpos < SCHARS (obj))
26359 {
26360 help = Fget_text_property (make_number (charpos),
26361 Qhelp_echo, obj);
26362 if (NILP (help))
26363 {
26364 /* If the string itself doesn't specify a help-echo,
26365 see if the buffer text ``under'' it does. */
26366 struct glyph_row *r
26367 = MATRIX_ROW (w->current_matrix, vpos);
26368 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26369 EMACS_INT p = string_buffer_position (obj, start);
26370 if (p > 0)
26371 {
26372 help = Fget_char_property (make_number (p),
26373 Qhelp_echo, w->buffer);
26374 if (!NILP (help))
26375 {
26376 charpos = p;
26377 obj = w->buffer;
26378 }
26379 }
26380 }
26381 }
26382 else if (BUFFERP (obj)
26383 && charpos >= BEGV
26384 && charpos < ZV)
26385 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26386 obj);
26387
26388 if (!NILP (help))
26389 {
26390 help_echo_string = help;
26391 help_echo_window = window;
26392 help_echo_object = obj;
26393 help_echo_pos = charpos;
26394 }
26395 }
26396 }
26397
26398 #ifdef HAVE_WINDOW_SYSTEM
26399 /* Look for a `pointer' property. */
26400 if (FRAME_WINDOW_P (f) && NILP (pointer))
26401 {
26402 /* Check overlays first. */
26403 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26404 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26405
26406 if (NILP (pointer))
26407 {
26408 Lisp_Object obj = glyph->object;
26409 EMACS_INT charpos = glyph->charpos;
26410
26411 /* Try text properties. */
26412 if (STRINGP (obj)
26413 && charpos >= 0
26414 && charpos < SCHARS (obj))
26415 {
26416 pointer = Fget_text_property (make_number (charpos),
26417 Qpointer, obj);
26418 if (NILP (pointer))
26419 {
26420 /* If the string itself doesn't specify a pointer,
26421 see if the buffer text ``under'' it does. */
26422 struct glyph_row *r
26423 = MATRIX_ROW (w->current_matrix, vpos);
26424 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26425 EMACS_INT p = string_buffer_position (obj, start);
26426 if (p > 0)
26427 pointer = Fget_char_property (make_number (p),
26428 Qpointer, w->buffer);
26429 }
26430 }
26431 else if (BUFFERP (obj)
26432 && charpos >= BEGV
26433 && charpos < ZV)
26434 pointer = Fget_text_property (make_number (charpos),
26435 Qpointer, obj);
26436 }
26437 }
26438 #endif /* HAVE_WINDOW_SYSTEM */
26439
26440 BEGV = obegv;
26441 ZV = ozv;
26442 current_buffer = obuf;
26443 }
26444
26445 set_cursor:
26446
26447 #ifdef HAVE_WINDOW_SYSTEM
26448 if (FRAME_WINDOW_P (f))
26449 define_frame_cursor1 (f, cursor, pointer);
26450 #else
26451 /* This is here to prevent a compiler error, about "label at end of
26452 compound statement". */
26453 return;
26454 #endif
26455 }
26456
26457
26458 /* EXPORT for RIF:
26459 Clear any mouse-face on window W. This function is part of the
26460 redisplay interface, and is called from try_window_id and similar
26461 functions to ensure the mouse-highlight is off. */
26462
26463 void
26464 x_clear_window_mouse_face (struct window *w)
26465 {
26466 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26467 Lisp_Object window;
26468
26469 BLOCK_INPUT;
26470 XSETWINDOW (window, w);
26471 if (EQ (window, hlinfo->mouse_face_window))
26472 clear_mouse_face (hlinfo);
26473 UNBLOCK_INPUT;
26474 }
26475
26476
26477 /* EXPORT:
26478 Just discard the mouse face information for frame F, if any.
26479 This is used when the size of F is changed. */
26480
26481 void
26482 cancel_mouse_face (struct frame *f)
26483 {
26484 Lisp_Object window;
26485 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26486
26487 window = hlinfo->mouse_face_window;
26488 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26489 {
26490 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26491 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26492 hlinfo->mouse_face_window = Qnil;
26493 }
26494 }
26495
26496
26497 \f
26498 /***********************************************************************
26499 Exposure Events
26500 ***********************************************************************/
26501
26502 #ifdef HAVE_WINDOW_SYSTEM
26503
26504 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26505 which intersects rectangle R. R is in window-relative coordinates. */
26506
26507 static void
26508 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26509 enum glyph_row_area area)
26510 {
26511 struct glyph *first = row->glyphs[area];
26512 struct glyph *end = row->glyphs[area] + row->used[area];
26513 struct glyph *last;
26514 int first_x, start_x, x;
26515
26516 if (area == TEXT_AREA && row->fill_line_p)
26517 /* If row extends face to end of line write the whole line. */
26518 draw_glyphs (w, 0, row, area,
26519 0, row->used[area],
26520 DRAW_NORMAL_TEXT, 0);
26521 else
26522 {
26523 /* Set START_X to the window-relative start position for drawing glyphs of
26524 AREA. The first glyph of the text area can be partially visible.
26525 The first glyphs of other areas cannot. */
26526 start_x = window_box_left_offset (w, area);
26527 x = start_x;
26528 if (area == TEXT_AREA)
26529 x += row->x;
26530
26531 /* Find the first glyph that must be redrawn. */
26532 while (first < end
26533 && x + first->pixel_width < r->x)
26534 {
26535 x += first->pixel_width;
26536 ++first;
26537 }
26538
26539 /* Find the last one. */
26540 last = first;
26541 first_x = x;
26542 while (last < end
26543 && x < r->x + r->width)
26544 {
26545 x += last->pixel_width;
26546 ++last;
26547 }
26548
26549 /* Repaint. */
26550 if (last > first)
26551 draw_glyphs (w, first_x - start_x, row, area,
26552 first - row->glyphs[area], last - row->glyphs[area],
26553 DRAW_NORMAL_TEXT, 0);
26554 }
26555 }
26556
26557
26558 /* Redraw the parts of the glyph row ROW on window W intersecting
26559 rectangle R. R is in window-relative coordinates. Value is
26560 non-zero if mouse-face was overwritten. */
26561
26562 static int
26563 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26564 {
26565 xassert (row->enabled_p);
26566
26567 if (row->mode_line_p || w->pseudo_window_p)
26568 draw_glyphs (w, 0, row, TEXT_AREA,
26569 0, row->used[TEXT_AREA],
26570 DRAW_NORMAL_TEXT, 0);
26571 else
26572 {
26573 if (row->used[LEFT_MARGIN_AREA])
26574 expose_area (w, row, r, LEFT_MARGIN_AREA);
26575 if (row->used[TEXT_AREA])
26576 expose_area (w, row, r, TEXT_AREA);
26577 if (row->used[RIGHT_MARGIN_AREA])
26578 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26579 draw_row_fringe_bitmaps (w, row);
26580 }
26581
26582 return row->mouse_face_p;
26583 }
26584
26585
26586 /* Redraw those parts of glyphs rows during expose event handling that
26587 overlap other rows. Redrawing of an exposed line writes over parts
26588 of lines overlapping that exposed line; this function fixes that.
26589
26590 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26591 row in W's current matrix that is exposed and overlaps other rows.
26592 LAST_OVERLAPPING_ROW is the last such row. */
26593
26594 static void
26595 expose_overlaps (struct window *w,
26596 struct glyph_row *first_overlapping_row,
26597 struct glyph_row *last_overlapping_row,
26598 XRectangle *r)
26599 {
26600 struct glyph_row *row;
26601
26602 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26603 if (row->overlapping_p)
26604 {
26605 xassert (row->enabled_p && !row->mode_line_p);
26606
26607 row->clip = r;
26608 if (row->used[LEFT_MARGIN_AREA])
26609 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26610
26611 if (row->used[TEXT_AREA])
26612 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26613
26614 if (row->used[RIGHT_MARGIN_AREA])
26615 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26616 row->clip = NULL;
26617 }
26618 }
26619
26620
26621 /* Return non-zero if W's cursor intersects rectangle R. */
26622
26623 static int
26624 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26625 {
26626 XRectangle cr, result;
26627 struct glyph *cursor_glyph;
26628 struct glyph_row *row;
26629
26630 if (w->phys_cursor.vpos >= 0
26631 && w->phys_cursor.vpos < w->current_matrix->nrows
26632 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26633 row->enabled_p)
26634 && row->cursor_in_fringe_p)
26635 {
26636 /* Cursor is in the fringe. */
26637 cr.x = window_box_right_offset (w,
26638 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26639 ? RIGHT_MARGIN_AREA
26640 : TEXT_AREA));
26641 cr.y = row->y;
26642 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26643 cr.height = row->height;
26644 return x_intersect_rectangles (&cr, r, &result);
26645 }
26646
26647 cursor_glyph = get_phys_cursor_glyph (w);
26648 if (cursor_glyph)
26649 {
26650 /* r is relative to W's box, but w->phys_cursor.x is relative
26651 to left edge of W's TEXT area. Adjust it. */
26652 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26653 cr.y = w->phys_cursor.y;
26654 cr.width = cursor_glyph->pixel_width;
26655 cr.height = w->phys_cursor_height;
26656 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26657 I assume the effect is the same -- and this is portable. */
26658 return x_intersect_rectangles (&cr, r, &result);
26659 }
26660 /* If we don't understand the format, pretend we're not in the hot-spot. */
26661 return 0;
26662 }
26663
26664
26665 /* EXPORT:
26666 Draw a vertical window border to the right of window W if W doesn't
26667 have vertical scroll bars. */
26668
26669 void
26670 x_draw_vertical_border (struct window *w)
26671 {
26672 struct frame *f = XFRAME (WINDOW_FRAME (w));
26673
26674 /* We could do better, if we knew what type of scroll-bar the adjacent
26675 windows (on either side) have... But we don't :-(
26676 However, I think this works ok. ++KFS 2003-04-25 */
26677
26678 /* Redraw borders between horizontally adjacent windows. Don't
26679 do it for frames with vertical scroll bars because either the
26680 right scroll bar of a window, or the left scroll bar of its
26681 neighbor will suffice as a border. */
26682 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26683 return;
26684
26685 if (!WINDOW_RIGHTMOST_P (w)
26686 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26687 {
26688 int x0, x1, y0, y1;
26689
26690 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26691 y1 -= 1;
26692
26693 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26694 x1 -= 1;
26695
26696 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26697 }
26698 else if (!WINDOW_LEFTMOST_P (w)
26699 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26700 {
26701 int x0, x1, y0, y1;
26702
26703 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26704 y1 -= 1;
26705
26706 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26707 x0 -= 1;
26708
26709 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26710 }
26711 }
26712
26713
26714 /* Redraw the part of window W intersection rectangle FR. Pixel
26715 coordinates in FR are frame-relative. Call this function with
26716 input blocked. Value is non-zero if the exposure overwrites
26717 mouse-face. */
26718
26719 static int
26720 expose_window (struct window *w, XRectangle *fr)
26721 {
26722 struct frame *f = XFRAME (w->frame);
26723 XRectangle wr, r;
26724 int mouse_face_overwritten_p = 0;
26725
26726 /* If window is not yet fully initialized, do nothing. This can
26727 happen when toolkit scroll bars are used and a window is split.
26728 Reconfiguring the scroll bar will generate an expose for a newly
26729 created window. */
26730 if (w->current_matrix == NULL)
26731 return 0;
26732
26733 /* When we're currently updating the window, display and current
26734 matrix usually don't agree. Arrange for a thorough display
26735 later. */
26736 if (w == updated_window)
26737 {
26738 SET_FRAME_GARBAGED (f);
26739 return 0;
26740 }
26741
26742 /* Frame-relative pixel rectangle of W. */
26743 wr.x = WINDOW_LEFT_EDGE_X (w);
26744 wr.y = WINDOW_TOP_EDGE_Y (w);
26745 wr.width = WINDOW_TOTAL_WIDTH (w);
26746 wr.height = WINDOW_TOTAL_HEIGHT (w);
26747
26748 if (x_intersect_rectangles (fr, &wr, &r))
26749 {
26750 int yb = window_text_bottom_y (w);
26751 struct glyph_row *row;
26752 int cursor_cleared_p;
26753 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26754
26755 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26756 r.x, r.y, r.width, r.height));
26757
26758 /* Convert to window coordinates. */
26759 r.x -= WINDOW_LEFT_EDGE_X (w);
26760 r.y -= WINDOW_TOP_EDGE_Y (w);
26761
26762 /* Turn off the cursor. */
26763 if (!w->pseudo_window_p
26764 && phys_cursor_in_rect_p (w, &r))
26765 {
26766 x_clear_cursor (w);
26767 cursor_cleared_p = 1;
26768 }
26769 else
26770 cursor_cleared_p = 0;
26771
26772 /* Update lines intersecting rectangle R. */
26773 first_overlapping_row = last_overlapping_row = NULL;
26774 for (row = w->current_matrix->rows;
26775 row->enabled_p;
26776 ++row)
26777 {
26778 int y0 = row->y;
26779 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26780
26781 if ((y0 >= r.y && y0 < r.y + r.height)
26782 || (y1 > r.y && y1 < r.y + r.height)
26783 || (r.y >= y0 && r.y < y1)
26784 || (r.y + r.height > y0 && r.y + r.height < y1))
26785 {
26786 /* A header line may be overlapping, but there is no need
26787 to fix overlapping areas for them. KFS 2005-02-12 */
26788 if (row->overlapping_p && !row->mode_line_p)
26789 {
26790 if (first_overlapping_row == NULL)
26791 first_overlapping_row = row;
26792 last_overlapping_row = row;
26793 }
26794
26795 row->clip = fr;
26796 if (expose_line (w, row, &r))
26797 mouse_face_overwritten_p = 1;
26798 row->clip = NULL;
26799 }
26800 else if (row->overlapping_p)
26801 {
26802 /* We must redraw a row overlapping the exposed area. */
26803 if (y0 < r.y
26804 ? y0 + row->phys_height > r.y
26805 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26806 {
26807 if (first_overlapping_row == NULL)
26808 first_overlapping_row = row;
26809 last_overlapping_row = row;
26810 }
26811 }
26812
26813 if (y1 >= yb)
26814 break;
26815 }
26816
26817 /* Display the mode line if there is one. */
26818 if (WINDOW_WANTS_MODELINE_P (w)
26819 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26820 row->enabled_p)
26821 && row->y < r.y + r.height)
26822 {
26823 if (expose_line (w, row, &r))
26824 mouse_face_overwritten_p = 1;
26825 }
26826
26827 if (!w->pseudo_window_p)
26828 {
26829 /* Fix the display of overlapping rows. */
26830 if (first_overlapping_row)
26831 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26832 fr);
26833
26834 /* Draw border between windows. */
26835 x_draw_vertical_border (w);
26836
26837 /* Turn the cursor on again. */
26838 if (cursor_cleared_p)
26839 update_window_cursor (w, 1);
26840 }
26841 }
26842
26843 return mouse_face_overwritten_p;
26844 }
26845
26846
26847
26848 /* Redraw (parts) of all windows in the window tree rooted at W that
26849 intersect R. R contains frame pixel coordinates. Value is
26850 non-zero if the exposure overwrites mouse-face. */
26851
26852 static int
26853 expose_window_tree (struct window *w, XRectangle *r)
26854 {
26855 struct frame *f = XFRAME (w->frame);
26856 int mouse_face_overwritten_p = 0;
26857
26858 while (w && !FRAME_GARBAGED_P (f))
26859 {
26860 if (!NILP (w->hchild))
26861 mouse_face_overwritten_p
26862 |= expose_window_tree (XWINDOW (w->hchild), r);
26863 else if (!NILP (w->vchild))
26864 mouse_face_overwritten_p
26865 |= expose_window_tree (XWINDOW (w->vchild), r);
26866 else
26867 mouse_face_overwritten_p |= expose_window (w, r);
26868
26869 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26870 }
26871
26872 return mouse_face_overwritten_p;
26873 }
26874
26875
26876 /* EXPORT:
26877 Redisplay an exposed area of frame F. X and Y are the upper-left
26878 corner of the exposed rectangle. W and H are width and height of
26879 the exposed area. All are pixel values. W or H zero means redraw
26880 the entire frame. */
26881
26882 void
26883 expose_frame (struct frame *f, int x, int y, int w, int h)
26884 {
26885 XRectangle r;
26886 int mouse_face_overwritten_p = 0;
26887
26888 TRACE ((stderr, "expose_frame "));
26889
26890 /* No need to redraw if frame will be redrawn soon. */
26891 if (FRAME_GARBAGED_P (f))
26892 {
26893 TRACE ((stderr, " garbaged\n"));
26894 return;
26895 }
26896
26897 /* If basic faces haven't been realized yet, there is no point in
26898 trying to redraw anything. This can happen when we get an expose
26899 event while Emacs is starting, e.g. by moving another window. */
26900 if (FRAME_FACE_CACHE (f) == NULL
26901 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26902 {
26903 TRACE ((stderr, " no faces\n"));
26904 return;
26905 }
26906
26907 if (w == 0 || h == 0)
26908 {
26909 r.x = r.y = 0;
26910 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26911 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26912 }
26913 else
26914 {
26915 r.x = x;
26916 r.y = y;
26917 r.width = w;
26918 r.height = h;
26919 }
26920
26921 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26922 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26923
26924 if (WINDOWP (f->tool_bar_window))
26925 mouse_face_overwritten_p
26926 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26927
26928 #ifdef HAVE_X_WINDOWS
26929 #ifndef MSDOS
26930 #ifndef USE_X_TOOLKIT
26931 if (WINDOWP (f->menu_bar_window))
26932 mouse_face_overwritten_p
26933 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26934 #endif /* not USE_X_TOOLKIT */
26935 #endif
26936 #endif
26937
26938 /* Some window managers support a focus-follows-mouse style with
26939 delayed raising of frames. Imagine a partially obscured frame,
26940 and moving the mouse into partially obscured mouse-face on that
26941 frame. The visible part of the mouse-face will be highlighted,
26942 then the WM raises the obscured frame. With at least one WM, KDE
26943 2.1, Emacs is not getting any event for the raising of the frame
26944 (even tried with SubstructureRedirectMask), only Expose events.
26945 These expose events will draw text normally, i.e. not
26946 highlighted. Which means we must redo the highlight here.
26947 Subsume it under ``we love X''. --gerd 2001-08-15 */
26948 /* Included in Windows version because Windows most likely does not
26949 do the right thing if any third party tool offers
26950 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26951 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26952 {
26953 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26954 if (f == hlinfo->mouse_face_mouse_frame)
26955 {
26956 int mouse_x = hlinfo->mouse_face_mouse_x;
26957 int mouse_y = hlinfo->mouse_face_mouse_y;
26958 clear_mouse_face (hlinfo);
26959 note_mouse_highlight (f, mouse_x, mouse_y);
26960 }
26961 }
26962 }
26963
26964
26965 /* EXPORT:
26966 Determine the intersection of two rectangles R1 and R2. Return
26967 the intersection in *RESULT. Value is non-zero if RESULT is not
26968 empty. */
26969
26970 int
26971 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26972 {
26973 XRectangle *left, *right;
26974 XRectangle *upper, *lower;
26975 int intersection_p = 0;
26976
26977 /* Rearrange so that R1 is the left-most rectangle. */
26978 if (r1->x < r2->x)
26979 left = r1, right = r2;
26980 else
26981 left = r2, right = r1;
26982
26983 /* X0 of the intersection is right.x0, if this is inside R1,
26984 otherwise there is no intersection. */
26985 if (right->x <= left->x + left->width)
26986 {
26987 result->x = right->x;
26988
26989 /* The right end of the intersection is the minimum of the
26990 the right ends of left and right. */
26991 result->width = (min (left->x + left->width, right->x + right->width)
26992 - result->x);
26993
26994 /* Same game for Y. */
26995 if (r1->y < r2->y)
26996 upper = r1, lower = r2;
26997 else
26998 upper = r2, lower = r1;
26999
27000 /* The upper end of the intersection is lower.y0, if this is inside
27001 of upper. Otherwise, there is no intersection. */
27002 if (lower->y <= upper->y + upper->height)
27003 {
27004 result->y = lower->y;
27005
27006 /* The lower end of the intersection is the minimum of the lower
27007 ends of upper and lower. */
27008 result->height = (min (lower->y + lower->height,
27009 upper->y + upper->height)
27010 - result->y);
27011 intersection_p = 1;
27012 }
27013 }
27014
27015 return intersection_p;
27016 }
27017
27018 #endif /* HAVE_WINDOW_SYSTEM */
27019
27020 \f
27021 /***********************************************************************
27022 Initialization
27023 ***********************************************************************/
27024
27025 void
27026 syms_of_xdisp (void)
27027 {
27028 Vwith_echo_area_save_vector = Qnil;
27029 staticpro (&Vwith_echo_area_save_vector);
27030
27031 Vmessage_stack = Qnil;
27032 staticpro (&Vmessage_stack);
27033
27034 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
27035 staticpro (&Qinhibit_redisplay);
27036
27037 message_dolog_marker1 = Fmake_marker ();
27038 staticpro (&message_dolog_marker1);
27039 message_dolog_marker2 = Fmake_marker ();
27040 staticpro (&message_dolog_marker2);
27041 message_dolog_marker3 = Fmake_marker ();
27042 staticpro (&message_dolog_marker3);
27043
27044 #if GLYPH_DEBUG
27045 defsubr (&Sdump_frame_glyph_matrix);
27046 defsubr (&Sdump_glyph_matrix);
27047 defsubr (&Sdump_glyph_row);
27048 defsubr (&Sdump_tool_bar_row);
27049 defsubr (&Strace_redisplay);
27050 defsubr (&Strace_to_stderr);
27051 #endif
27052 #ifdef HAVE_WINDOW_SYSTEM
27053 defsubr (&Stool_bar_lines_needed);
27054 defsubr (&Slookup_image_map);
27055 #endif
27056 defsubr (&Sformat_mode_line);
27057 defsubr (&Sinvisible_p);
27058 defsubr (&Scurrent_bidi_paragraph_direction);
27059
27060 staticpro (&Qmenu_bar_update_hook);
27061 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
27062
27063 staticpro (&Qoverriding_terminal_local_map);
27064 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
27065
27066 staticpro (&Qoverriding_local_map);
27067 Qoverriding_local_map = intern_c_string ("overriding-local-map");
27068
27069 staticpro (&Qwindow_scroll_functions);
27070 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
27071
27072 staticpro (&Qwindow_text_change_functions);
27073 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
27074
27075 staticpro (&Qredisplay_end_trigger_functions);
27076 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
27077
27078 staticpro (&Qinhibit_point_motion_hooks);
27079 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
27080
27081 Qeval = intern_c_string ("eval");
27082 staticpro (&Qeval);
27083
27084 QCdata = intern_c_string (":data");
27085 staticpro (&QCdata);
27086 Qdisplay = intern_c_string ("display");
27087 staticpro (&Qdisplay);
27088 Qspace_width = intern_c_string ("space-width");
27089 staticpro (&Qspace_width);
27090 Qraise = intern_c_string ("raise");
27091 staticpro (&Qraise);
27092 Qslice = intern_c_string ("slice");
27093 staticpro (&Qslice);
27094 Qspace = intern_c_string ("space");
27095 staticpro (&Qspace);
27096 Qmargin = intern_c_string ("margin");
27097 staticpro (&Qmargin);
27098 Qpointer = intern_c_string ("pointer");
27099 staticpro (&Qpointer);
27100 Qleft_margin = intern_c_string ("left-margin");
27101 staticpro (&Qleft_margin);
27102 Qright_margin = intern_c_string ("right-margin");
27103 staticpro (&Qright_margin);
27104 Qcenter = intern_c_string ("center");
27105 staticpro (&Qcenter);
27106 Qline_height = intern_c_string ("line-height");
27107 staticpro (&Qline_height);
27108 QCalign_to = intern_c_string (":align-to");
27109 staticpro (&QCalign_to);
27110 QCrelative_width = intern_c_string (":relative-width");
27111 staticpro (&QCrelative_width);
27112 QCrelative_height = intern_c_string (":relative-height");
27113 staticpro (&QCrelative_height);
27114 QCeval = intern_c_string (":eval");
27115 staticpro (&QCeval);
27116 QCpropertize = intern_c_string (":propertize");
27117 staticpro (&QCpropertize);
27118 QCfile = intern_c_string (":file");
27119 staticpro (&QCfile);
27120 Qfontified = intern_c_string ("fontified");
27121 staticpro (&Qfontified);
27122 Qfontification_functions = intern_c_string ("fontification-functions");
27123 staticpro (&Qfontification_functions);
27124 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
27125 staticpro (&Qtrailing_whitespace);
27126 Qescape_glyph = intern_c_string ("escape-glyph");
27127 staticpro (&Qescape_glyph);
27128 Qnobreak_space = intern_c_string ("nobreak-space");
27129 staticpro (&Qnobreak_space);
27130 Qimage = intern_c_string ("image");
27131 staticpro (&Qimage);
27132 Qtext = intern_c_string ("text");
27133 staticpro (&Qtext);
27134 Qboth = intern_c_string ("both");
27135 staticpro (&Qboth);
27136 Qboth_horiz = intern_c_string ("both-horiz");
27137 staticpro (&Qboth_horiz);
27138 Qtext_image_horiz = intern_c_string ("text-image-horiz");
27139 staticpro (&Qtext_image_horiz);
27140 QCmap = intern_c_string (":map");
27141 staticpro (&QCmap);
27142 QCpointer = intern_c_string (":pointer");
27143 staticpro (&QCpointer);
27144 Qrect = intern_c_string ("rect");
27145 staticpro (&Qrect);
27146 Qcircle = intern_c_string ("circle");
27147 staticpro (&Qcircle);
27148 Qpoly = intern_c_string ("poly");
27149 staticpro (&Qpoly);
27150 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
27151 staticpro (&Qmessage_truncate_lines);
27152 Qgrow_only = intern_c_string ("grow-only");
27153 staticpro (&Qgrow_only);
27154 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
27155 staticpro (&Qinhibit_menubar_update);
27156 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
27157 staticpro (&Qinhibit_eval_during_redisplay);
27158 Qposition = intern_c_string ("position");
27159 staticpro (&Qposition);
27160 Qbuffer_position = intern_c_string ("buffer-position");
27161 staticpro (&Qbuffer_position);
27162 Qobject = intern_c_string ("object");
27163 staticpro (&Qobject);
27164 Qbar = intern_c_string ("bar");
27165 staticpro (&Qbar);
27166 Qhbar = intern_c_string ("hbar");
27167 staticpro (&Qhbar);
27168 Qbox = intern_c_string ("box");
27169 staticpro (&Qbox);
27170 Qhollow = intern_c_string ("hollow");
27171 staticpro (&Qhollow);
27172 Qhand = intern_c_string ("hand");
27173 staticpro (&Qhand);
27174 Qarrow = intern_c_string ("arrow");
27175 staticpro (&Qarrow);
27176 Qtext = intern_c_string ("text");
27177 staticpro (&Qtext);
27178 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
27179 staticpro (&Qinhibit_free_realized_faces);
27180
27181 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27182 Fcons (intern_c_string ("void-variable"), Qnil)),
27183 Qnil);
27184 staticpro (&list_of_error);
27185
27186 Qlast_arrow_position = intern_c_string ("last-arrow-position");
27187 staticpro (&Qlast_arrow_position);
27188 Qlast_arrow_string = intern_c_string ("last-arrow-string");
27189 staticpro (&Qlast_arrow_string);
27190
27191 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
27192 staticpro (&Qoverlay_arrow_string);
27193 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
27194 staticpro (&Qoverlay_arrow_bitmap);
27195
27196 echo_buffer[0] = echo_buffer[1] = Qnil;
27197 staticpro (&echo_buffer[0]);
27198 staticpro (&echo_buffer[1]);
27199
27200 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27201 staticpro (&echo_area_buffer[0]);
27202 staticpro (&echo_area_buffer[1]);
27203
27204 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27205 staticpro (&Vmessages_buffer_name);
27206
27207 mode_line_proptrans_alist = Qnil;
27208 staticpro (&mode_line_proptrans_alist);
27209 mode_line_string_list = Qnil;
27210 staticpro (&mode_line_string_list);
27211 mode_line_string_face = Qnil;
27212 staticpro (&mode_line_string_face);
27213 mode_line_string_face_prop = Qnil;
27214 staticpro (&mode_line_string_face_prop);
27215 Vmode_line_unwind_vector = Qnil;
27216 staticpro (&Vmode_line_unwind_vector);
27217
27218 help_echo_string = Qnil;
27219 staticpro (&help_echo_string);
27220 help_echo_object = Qnil;
27221 staticpro (&help_echo_object);
27222 help_echo_window = Qnil;
27223 staticpro (&help_echo_window);
27224 previous_help_echo_string = Qnil;
27225 staticpro (&previous_help_echo_string);
27226 help_echo_pos = -1;
27227
27228 Qright_to_left = intern_c_string ("right-to-left");
27229 staticpro (&Qright_to_left);
27230 Qleft_to_right = intern_c_string ("left-to-right");
27231 staticpro (&Qleft_to_right);
27232
27233 #ifdef HAVE_WINDOW_SYSTEM
27234 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27235 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27236 For example, if a block cursor is over a tab, it will be drawn as
27237 wide as that tab on the display. */);
27238 x_stretch_cursor_p = 0;
27239 #endif
27240
27241 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27242 doc: /* *Non-nil means highlight trailing whitespace.
27243 The face used for trailing whitespace is `trailing-whitespace'. */);
27244 Vshow_trailing_whitespace = Qnil;
27245
27246 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27247 doc: /* *Control highlighting of nobreak space and soft hyphen.
27248 A value of t means highlight the character itself (for nobreak space,
27249 use face `nobreak-space').
27250 A value of nil means no highlighting.
27251 Other values mean display the escape glyph followed by an ordinary
27252 space or ordinary hyphen. */);
27253 Vnobreak_char_display = Qt;
27254
27255 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27256 doc: /* *The pointer shape to show in void text areas.
27257 A value of nil means to show the text pointer. Other options are `arrow',
27258 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27259 Vvoid_text_area_pointer = Qarrow;
27260
27261 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27262 doc: /* Non-nil means don't actually do any redisplay.
27263 This is used for internal purposes. */);
27264 Vinhibit_redisplay = Qnil;
27265
27266 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27267 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27268 Vglobal_mode_string = Qnil;
27269
27270 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27271 doc: /* Marker for where to display an arrow on top of the buffer text.
27272 This must be the beginning of a line in order to work.
27273 See also `overlay-arrow-string'. */);
27274 Voverlay_arrow_position = Qnil;
27275
27276 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27277 doc: /* String to display as an arrow in non-window frames.
27278 See also `overlay-arrow-position'. */);
27279 Voverlay_arrow_string = make_pure_c_string ("=>");
27280
27281 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27282 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27283 The symbols on this list are examined during redisplay to determine
27284 where to display overlay arrows. */);
27285 Voverlay_arrow_variable_list
27286 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27287
27288 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27289 doc: /* *The number of lines to try scrolling a window by when point moves out.
27290 If that fails to bring point back on frame, point is centered instead.
27291 If this is zero, point is always centered after it moves off frame.
27292 If you want scrolling to always be a line at a time, you should set
27293 `scroll-conservatively' to a large value rather than set this to 1. */);
27294
27295 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27296 doc: /* *Scroll up to this many lines, to bring point back on screen.
27297 If point moves off-screen, redisplay will scroll by up to
27298 `scroll-conservatively' lines in order to bring point just barely
27299 onto the screen again. If that cannot be done, then redisplay
27300 recenters point as usual.
27301
27302 If the value is greater than 100, redisplay will never recenter point,
27303 but will always scroll just enough text to bring point into view, even
27304 if you move far away.
27305
27306 A value of zero means always recenter point if it moves off screen. */);
27307 scroll_conservatively = 0;
27308
27309 DEFVAR_INT ("scroll-margin", scroll_margin,
27310 doc: /* *Number of lines of margin at the top and bottom of a window.
27311 Recenter the window whenever point gets within this many lines
27312 of the top or bottom of the window. */);
27313 scroll_margin = 0;
27314
27315 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27316 doc: /* Pixels per inch value for non-window system displays.
27317 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27318 Vdisplay_pixels_per_inch = make_float (72.0);
27319
27320 #if GLYPH_DEBUG
27321 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27322 #endif
27323
27324 DEFVAR_LISP ("truncate-partial-width-windows",
27325 Vtruncate_partial_width_windows,
27326 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27327 For an integer value, truncate lines in each window narrower than the
27328 full frame width, provided the window width is less than that integer;
27329 otherwise, respect the value of `truncate-lines'.
27330
27331 For any other non-nil value, truncate lines in all windows that do
27332 not span the full frame width.
27333
27334 A value of nil means to respect the value of `truncate-lines'.
27335
27336 If `word-wrap' is enabled, you might want to reduce this. */);
27337 Vtruncate_partial_width_windows = make_number (50);
27338
27339 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27340 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27341 Any other value means to use the appropriate face, `mode-line',
27342 `header-line', or `menu' respectively. */);
27343 mode_line_inverse_video = 1;
27344
27345 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27346 doc: /* *Maximum buffer size for which line number should be displayed.
27347 If the buffer is bigger than this, the line number does not appear
27348 in the mode line. A value of nil means no limit. */);
27349 Vline_number_display_limit = Qnil;
27350
27351 DEFVAR_INT ("line-number-display-limit-width",
27352 line_number_display_limit_width,
27353 doc: /* *Maximum line width (in characters) for line number display.
27354 If the average length of the lines near point is bigger than this, then the
27355 line number may be omitted from the mode line. */);
27356 line_number_display_limit_width = 200;
27357
27358 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27359 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27360 highlight_nonselected_windows = 0;
27361
27362 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27363 doc: /* Non-nil if more than one frame is visible on this display.
27364 Minibuffer-only frames don't count, but iconified frames do.
27365 This variable is not guaranteed to be accurate except while processing
27366 `frame-title-format' and `icon-title-format'. */);
27367
27368 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27369 doc: /* Template for displaying the title bar of visible frames.
27370 \(Assuming the window manager supports this feature.)
27371
27372 This variable has the same structure as `mode-line-format', except that
27373 the %c and %l constructs are ignored. It is used only on frames for
27374 which no explicit name has been set \(see `modify-frame-parameters'). */);
27375
27376 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27377 doc: /* Template for displaying the title bar of an iconified frame.
27378 \(Assuming the window manager supports this feature.)
27379 This variable has the same structure as `mode-line-format' (which see),
27380 and is used only on frames for which no explicit name has been set
27381 \(see `modify-frame-parameters'). */);
27382 Vicon_title_format
27383 = Vframe_title_format
27384 = pure_cons (intern_c_string ("multiple-frames"),
27385 pure_cons (make_pure_c_string ("%b"),
27386 pure_cons (pure_cons (empty_unibyte_string,
27387 pure_cons (intern_c_string ("invocation-name"),
27388 pure_cons (make_pure_c_string ("@"),
27389 pure_cons (intern_c_string ("system-name"),
27390 Qnil)))),
27391 Qnil)));
27392
27393 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27394 doc: /* Maximum number of lines to keep in the message log buffer.
27395 If nil, disable message logging. If t, log messages but don't truncate
27396 the buffer when it becomes large. */);
27397 Vmessage_log_max = make_number (100);
27398
27399 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27400 doc: /* Functions called before redisplay, if window sizes have changed.
27401 The value should be a list of functions that take one argument.
27402 Just before redisplay, for each frame, if any of its windows have changed
27403 size since the last redisplay, or have been split or deleted,
27404 all the functions in the list are called, with the frame as argument. */);
27405 Vwindow_size_change_functions = Qnil;
27406
27407 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27408 doc: /* List of functions to call before redisplaying a window with scrolling.
27409 Each function is called with two arguments, the window and its new
27410 display-start position. Note that these functions are also called by
27411 `set-window-buffer'. Also note that the value of `window-end' is not
27412 valid when these functions are called. */);
27413 Vwindow_scroll_functions = Qnil;
27414
27415 DEFVAR_LISP ("window-text-change-functions",
27416 Vwindow_text_change_functions,
27417 doc: /* Functions to call in redisplay when text in the window might change. */);
27418 Vwindow_text_change_functions = Qnil;
27419
27420 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27421 doc: /* Functions called when redisplay of a window reaches the end trigger.
27422 Each function is called with two arguments, the window and the end trigger value.
27423 See `set-window-redisplay-end-trigger'. */);
27424 Vredisplay_end_trigger_functions = Qnil;
27425
27426 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27427 doc: /* *Non-nil means autoselect window with mouse pointer.
27428 If nil, do not autoselect windows.
27429 A positive number means delay autoselection by that many seconds: a
27430 window is autoselected only after the mouse has remained in that
27431 window for the duration of the delay.
27432 A negative number has a similar effect, but causes windows to be
27433 autoselected only after the mouse has stopped moving. \(Because of
27434 the way Emacs compares mouse events, you will occasionally wait twice
27435 that time before the window gets selected.\)
27436 Any other value means to autoselect window instantaneously when the
27437 mouse pointer enters it.
27438
27439 Autoselection selects the minibuffer only if it is active, and never
27440 unselects the minibuffer if it is active.
27441
27442 When customizing this variable make sure that the actual value of
27443 `focus-follows-mouse' matches the behavior of your window manager. */);
27444 Vmouse_autoselect_window = Qnil;
27445
27446 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27447 doc: /* *Non-nil means automatically resize tool-bars.
27448 This dynamically changes the tool-bar's height to the minimum height
27449 that is needed to make all tool-bar items visible.
27450 If value is `grow-only', the tool-bar's height is only increased
27451 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27452 Vauto_resize_tool_bars = Qt;
27453
27454 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27455 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27456 auto_raise_tool_bar_buttons_p = 1;
27457
27458 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27459 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27460 make_cursor_line_fully_visible_p = 1;
27461
27462 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27463 doc: /* *Border below tool-bar in pixels.
27464 If an integer, use it as the height of the border.
27465 If it is one of `internal-border-width' or `border-width', use the
27466 value of the corresponding frame parameter.
27467 Otherwise, no border is added below the tool-bar. */);
27468 Vtool_bar_border = Qinternal_border_width;
27469
27470 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27471 doc: /* *Margin around tool-bar buttons in pixels.
27472 If an integer, use that for both horizontal and vertical margins.
27473 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27474 HORZ specifying the horizontal margin, and VERT specifying the
27475 vertical margin. */);
27476 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27477
27478 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27479 doc: /* *Relief thickness of tool-bar buttons. */);
27480 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27481
27482 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27483 doc: /* Tool bar style to use.
27484 It can be one of
27485 image - show images only
27486 text - show text only
27487 both - show both, text below image
27488 both-horiz - show text to the right of the image
27489 text-image-horiz - show text to the left of the image
27490 any other - use system default or image if no system default. */);
27491 Vtool_bar_style = Qnil;
27492
27493 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27494 doc: /* *Maximum number of characters a label can have to be shown.
27495 The tool bar style must also show labels for this to have any effect, see
27496 `tool-bar-style'. */);
27497 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27498
27499 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27500 doc: /* List of functions to call to fontify regions of text.
27501 Each function is called with one argument POS. Functions must
27502 fontify a region starting at POS in the current buffer, and give
27503 fontified regions the property `fontified'. */);
27504 Vfontification_functions = Qnil;
27505 Fmake_variable_buffer_local (Qfontification_functions);
27506
27507 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27508 unibyte_display_via_language_environment,
27509 doc: /* *Non-nil means display unibyte text according to language environment.
27510 Specifically, this means that raw bytes in the range 160-255 decimal
27511 are displayed by converting them to the equivalent multibyte characters
27512 according to the current language environment. As a result, they are
27513 displayed according to the current fontset.
27514
27515 Note that this variable affects only how these bytes are displayed,
27516 but does not change the fact they are interpreted as raw bytes. */);
27517 unibyte_display_via_language_environment = 0;
27518
27519 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27520 doc: /* *Maximum height for resizing mini-windows.
27521 If a float, it specifies a fraction of the mini-window frame's height.
27522 If an integer, it specifies a number of lines. */);
27523 Vmax_mini_window_height = make_float (0.25);
27524
27525 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27526 doc: /* *How to resize mini-windows.
27527 A value of nil means don't automatically resize mini-windows.
27528 A value of t means resize them to fit the text displayed in them.
27529 A value of `grow-only', the default, means let mini-windows grow
27530 only, until their display becomes empty, at which point the windows
27531 go back to their normal size. */);
27532 Vresize_mini_windows = Qgrow_only;
27533
27534 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27535 doc: /* Alist specifying how to blink the cursor off.
27536 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27537 `cursor-type' frame-parameter or variable equals ON-STATE,
27538 comparing using `equal', Emacs uses OFF-STATE to specify
27539 how to blink it off. ON-STATE and OFF-STATE are values for
27540 the `cursor-type' frame parameter.
27541
27542 If a frame's ON-STATE has no entry in this list,
27543 the frame's other specifications determine how to blink the cursor off. */);
27544 Vblink_cursor_alist = Qnil;
27545
27546 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27547 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27548 If non-nil, windows are automatically scrolled horizontally to make
27549 point visible. */);
27550 automatic_hscrolling_p = 1;
27551 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
27552 staticpro (&Qauto_hscroll_mode);
27553
27554 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27555 doc: /* *How many columns away from the window edge point is allowed to get
27556 before automatic hscrolling will horizontally scroll the window. */);
27557 hscroll_margin = 5;
27558
27559 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27560 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27561 When point is less than `hscroll-margin' columns from the window
27562 edge, automatic hscrolling will scroll the window by the amount of columns
27563 determined by this variable. If its value is a positive integer, scroll that
27564 many columns. If it's a positive floating-point number, it specifies the
27565 fraction of the window's width to scroll. If it's nil or zero, point will be
27566 centered horizontally after the scroll. Any other value, including negative
27567 numbers, are treated as if the value were zero.
27568
27569 Automatic hscrolling always moves point outside the scroll margin, so if
27570 point was more than scroll step columns inside the margin, the window will
27571 scroll more than the value given by the scroll step.
27572
27573 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27574 and `scroll-right' overrides this variable's effect. */);
27575 Vhscroll_step = make_number (0);
27576
27577 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27578 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27579 Bind this around calls to `message' to let it take effect. */);
27580 message_truncate_lines = 0;
27581
27582 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27583 doc: /* Normal hook run to update the menu bar definitions.
27584 Redisplay runs this hook before it redisplays the menu bar.
27585 This is used to update submenus such as Buffers,
27586 whose contents depend on various data. */);
27587 Vmenu_bar_update_hook = Qnil;
27588
27589 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27590 doc: /* Frame for which we are updating a menu.
27591 The enable predicate for a menu binding should check this variable. */);
27592 Vmenu_updating_frame = Qnil;
27593
27594 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27595 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27596 inhibit_menubar_update = 0;
27597
27598 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27599 doc: /* Prefix prepended to all continuation lines at display time.
27600 The value may be a string, an image, or a stretch-glyph; it is
27601 interpreted in the same way as the value of a `display' text property.
27602
27603 This variable is overridden by any `wrap-prefix' text or overlay
27604 property.
27605
27606 To add a prefix to non-continuation lines, use `line-prefix'. */);
27607 Vwrap_prefix = Qnil;
27608 staticpro (&Qwrap_prefix);
27609 Qwrap_prefix = intern_c_string ("wrap-prefix");
27610 Fmake_variable_buffer_local (Qwrap_prefix);
27611
27612 DEFVAR_LISP ("line-prefix", Vline_prefix,
27613 doc: /* Prefix prepended to all non-continuation lines at display time.
27614 The value may be a string, an image, or a stretch-glyph; it is
27615 interpreted in the same way as the value of a `display' text property.
27616
27617 This variable is overridden by any `line-prefix' text or overlay
27618 property.
27619
27620 To add a prefix to continuation lines, use `wrap-prefix'. */);
27621 Vline_prefix = Qnil;
27622 staticpro (&Qline_prefix);
27623 Qline_prefix = intern_c_string ("line-prefix");
27624 Fmake_variable_buffer_local (Qline_prefix);
27625
27626 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27627 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27628 inhibit_eval_during_redisplay = 0;
27629
27630 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27631 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27632 inhibit_free_realized_faces = 0;
27633
27634 #if GLYPH_DEBUG
27635 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27636 doc: /* Inhibit try_window_id display optimization. */);
27637 inhibit_try_window_id = 0;
27638
27639 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27640 doc: /* Inhibit try_window_reusing display optimization. */);
27641 inhibit_try_window_reusing = 0;
27642
27643 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27644 doc: /* Inhibit try_cursor_movement display optimization. */);
27645 inhibit_try_cursor_movement = 0;
27646 #endif /* GLYPH_DEBUG */
27647
27648 DEFVAR_INT ("overline-margin", overline_margin,
27649 doc: /* *Space between overline and text, in pixels.
27650 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27651 margin to the caracter height. */);
27652 overline_margin = 2;
27653
27654 DEFVAR_INT ("underline-minimum-offset",
27655 underline_minimum_offset,
27656 doc: /* Minimum distance between baseline and underline.
27657 This can improve legibility of underlined text at small font sizes,
27658 particularly when using variable `x-use-underline-position-properties'
27659 with fonts that specify an UNDERLINE_POSITION relatively close to the
27660 baseline. The default value is 1. */);
27661 underline_minimum_offset = 1;
27662
27663 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27664 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27665 This feature only works when on a window system that can change
27666 cursor shapes. */);
27667 display_hourglass_p = 1;
27668
27669 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27670 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27671 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27672
27673 hourglass_atimer = NULL;
27674 hourglass_shown_p = 0;
27675
27676 DEFSYM (Qglyphless_char, "glyphless-char");
27677 DEFSYM (Qhex_code, "hex-code");
27678 DEFSYM (Qempty_box, "empty-box");
27679 DEFSYM (Qthin_space, "thin-space");
27680 DEFSYM (Qzero_width, "zero-width");
27681
27682 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27683 /* Intern this now in case it isn't already done.
27684 Setting this variable twice is harmless.
27685 But don't staticpro it here--that is done in alloc.c. */
27686 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27687 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27688
27689 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27690 doc: /* Char-table defining glyphless characters.
27691 Each element, if non-nil, should be one of the following:
27692 an ASCII acronym string: display this string in a box
27693 `hex-code': display the hexadecimal code of a character in a box
27694 `empty-box': display as an empty box
27695 `thin-space': display as 1-pixel width space
27696 `zero-width': don't display
27697 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27698 display method for graphical terminals and text terminals respectively.
27699 GRAPHICAL and TEXT should each have one of the values listed above.
27700
27701 The char-table has one extra slot to control the display of a character for
27702 which no font is found. This slot only takes effect on graphical terminals.
27703 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27704 `thin-space'. The default is `empty-box'. */);
27705 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27706 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27707 Qempty_box);
27708 }
27709
27710
27711 /* Initialize this module when Emacs starts. */
27712
27713 void
27714 init_xdisp (void)
27715 {
27716 Lisp_Object root_window;
27717 struct window *mini_w;
27718
27719 current_header_line_height = current_mode_line_height = -1;
27720
27721 CHARPOS (this_line_start_pos) = 0;
27722
27723 mini_w = XWINDOW (minibuf_window);
27724 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27725 echo_area_window = minibuf_window;
27726
27727 if (!noninteractive)
27728 {
27729 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27730 int i;
27731
27732 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27733 set_window_height (root_window,
27734 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27735 0);
27736 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27737 set_window_height (minibuf_window, 1, 0);
27738
27739 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27740 mini_w->total_cols = make_number (FRAME_COLS (f));
27741
27742 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27743 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27744 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27745
27746 /* The default ellipsis glyphs `...'. */
27747 for (i = 0; i < 3; ++i)
27748 default_invis_vector[i] = make_number ('.');
27749 }
27750
27751 {
27752 /* Allocate the buffer for frame titles.
27753 Also used for `format-mode-line'. */
27754 int size = 100;
27755 mode_line_noprop_buf = (char *) xmalloc (size);
27756 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27757 mode_line_noprop_ptr = mode_line_noprop_buf;
27758 mode_line_target = MODE_LINE_DISPLAY;
27759 }
27760
27761 help_echo_showing_p = 0;
27762 }
27763
27764 /* Since w32 does not support atimers, it defines its own implementation of
27765 the following three functions in w32fns.c. */
27766 #ifndef WINDOWSNT
27767
27768 /* Platform-independent portion of hourglass implementation. */
27769
27770 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27771 int
27772 hourglass_started (void)
27773 {
27774 return hourglass_shown_p || hourglass_atimer != NULL;
27775 }
27776
27777 /* Cancel a currently active hourglass timer, and start a new one. */
27778 void
27779 start_hourglass (void)
27780 {
27781 #if defined (HAVE_WINDOW_SYSTEM)
27782 EMACS_TIME delay;
27783 int secs, usecs = 0;
27784
27785 cancel_hourglass ();
27786
27787 if (INTEGERP (Vhourglass_delay)
27788 && XINT (Vhourglass_delay) > 0)
27789 secs = XFASTINT (Vhourglass_delay);
27790 else if (FLOATP (Vhourglass_delay)
27791 && XFLOAT_DATA (Vhourglass_delay) > 0)
27792 {
27793 Lisp_Object tem;
27794 tem = Ftruncate (Vhourglass_delay, Qnil);
27795 secs = XFASTINT (tem);
27796 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27797 }
27798 else
27799 secs = DEFAULT_HOURGLASS_DELAY;
27800
27801 EMACS_SET_SECS_USECS (delay, secs, usecs);
27802 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27803 show_hourglass, NULL);
27804 #endif
27805 }
27806
27807
27808 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27809 shown. */
27810 void
27811 cancel_hourglass (void)
27812 {
27813 #if defined (HAVE_WINDOW_SYSTEM)
27814 if (hourglass_atimer)
27815 {
27816 cancel_atimer (hourglass_atimer);
27817 hourglass_atimer = NULL;
27818 }
27819
27820 if (hourglass_shown_p)
27821 hide_hourglass ();
27822 #endif
27823 }
27824 #endif /* ! WINDOWSNT */