Adapt compute_display_string_pos to iteration over strings.
[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 int single_display_spec_intangible_p (Lisp_Object);
787 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
788 static void ensure_echo_area_buffers (void);
789 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
790 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
791 static int with_echo_area_buffer (struct window *, int,
792 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
793 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
794 static void clear_garbaged_frames (void);
795 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
796 static void pop_message (void);
797 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
798 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
799 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
800 static int display_echo_area (struct window *);
801 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
802 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
803 static Lisp_Object unwind_redisplay (Lisp_Object);
804 static int string_char_and_length (const unsigned char *, int *);
805 static struct text_pos display_prop_end (struct it *, Lisp_Object,
806 struct text_pos);
807 static int compute_window_start_on_continuation_line (struct window *);
808 static Lisp_Object safe_eval_handler (Lisp_Object);
809 static void insert_left_trunc_glyphs (struct it *);
810 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
811 Lisp_Object);
812 static void extend_face_to_end_of_line (struct it *);
813 static int append_space_for_newline (struct it *, int);
814 static int cursor_row_fully_visible_p (struct window *, int, int);
815 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
816 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
817 static int trailing_whitespace_p (EMACS_INT);
818 static unsigned long int message_log_check_duplicate (EMACS_INT, EMACS_INT);
819 static void push_it (struct it *, struct text_pos *);
820 static void pop_it (struct it *);
821 static void sync_frame_with_window_matrix_rows (struct window *);
822 static void select_frame_for_redisplay (Lisp_Object);
823 static void redisplay_internal (void);
824 static int echo_area_display (int);
825 static void redisplay_windows (Lisp_Object);
826 static void redisplay_window (Lisp_Object, int);
827 static Lisp_Object redisplay_window_error (Lisp_Object);
828 static Lisp_Object redisplay_window_0 (Lisp_Object);
829 static Lisp_Object redisplay_window_1 (Lisp_Object);
830 static int set_cursor_from_row (struct window *, struct glyph_row *,
831 struct glyph_matrix *, EMACS_INT, EMACS_INT,
832 int, int);
833 static int update_menu_bar (struct frame *, int, int);
834 static int try_window_reusing_current_matrix (struct window *);
835 static int try_window_id (struct window *);
836 static int display_line (struct it *);
837 static int display_mode_lines (struct window *);
838 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
839 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
840 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
841 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
842 static void display_menu_bar (struct window *);
843 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
844 EMACS_INT *);
845 static int display_string (const char *, Lisp_Object, Lisp_Object,
846 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
847 static void compute_line_metrics (struct it *);
848 static void run_redisplay_end_trigger_hook (struct it *);
849 static int get_overlay_strings (struct it *, EMACS_INT);
850 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
851 static void next_overlay_string (struct it *);
852 static void reseat (struct it *, struct text_pos, int);
853 static void reseat_1 (struct it *, struct text_pos, int);
854 static void back_to_previous_visible_line_start (struct it *);
855 void reseat_at_previous_visible_line_start (struct it *);
856 static void reseat_at_next_visible_line_start (struct it *, int);
857 static int next_element_from_ellipsis (struct it *);
858 static int next_element_from_display_vector (struct it *);
859 static int next_element_from_string (struct it *);
860 static int next_element_from_c_string (struct it *);
861 static int next_element_from_buffer (struct it *);
862 static int next_element_from_composition (struct it *);
863 static int next_element_from_image (struct it *);
864 static int next_element_from_stretch (struct it *);
865 static void load_overlay_strings (struct it *, EMACS_INT);
866 static int init_from_display_pos (struct it *, struct window *,
867 struct display_pos *);
868 static void reseat_to_string (struct it *, const char *,
869 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
870 static int get_next_display_element (struct it *);
871 static enum move_it_result
872 move_it_in_display_line_to (struct it *, EMACS_INT, int,
873 enum move_operation_enum);
874 void move_it_vertically_backward (struct it *, int);
875 static void init_to_row_start (struct it *, struct window *,
876 struct glyph_row *);
877 static int init_to_row_end (struct it *, struct window *,
878 struct glyph_row *);
879 static void back_to_previous_line_start (struct it *);
880 static int forward_to_next_line_start (struct it *, int *);
881 static struct text_pos string_pos_nchars_ahead (struct text_pos,
882 Lisp_Object, EMACS_INT);
883 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
884 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
885 static EMACS_INT number_of_chars (const char *, int);
886 static void compute_stop_pos (struct it *);
887 static void compute_string_pos (struct text_pos *, struct text_pos,
888 Lisp_Object);
889 static int face_before_or_after_it_pos (struct it *, int);
890 static EMACS_INT next_overlay_change (EMACS_INT);
891 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
892 Lisp_Object, struct text_pos *, EMACS_INT, int);
893 static int handle_single_display_spec (struct it *, Lisp_Object,
894 Lisp_Object, Lisp_Object,
895 struct text_pos *, EMACS_INT, int, int);
896 static int underlying_face_id (struct it *);
897 static int in_ellipses_for_invisible_text_p (struct display_pos *,
898 struct window *);
899
900 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
901 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
902
903 #ifdef HAVE_WINDOW_SYSTEM
904
905 static void x_consider_frame_title (Lisp_Object);
906 static int tool_bar_lines_needed (struct frame *, int *);
907 static void update_tool_bar (struct frame *, int);
908 static void build_desired_tool_bar_string (struct frame *f);
909 static int redisplay_tool_bar (struct frame *);
910 static void display_tool_bar_line (struct it *, int);
911 static void notice_overwritten_cursor (struct window *,
912 enum glyph_row_area,
913 int, int, int, int);
914 static void append_stretch_glyph (struct it *, Lisp_Object,
915 int, int, int);
916
917
918 #endif /* HAVE_WINDOW_SYSTEM */
919
920 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
921 static int coords_in_mouse_face_p (struct window *, int, int);
922
923
924 \f
925 /***********************************************************************
926 Window display dimensions
927 ***********************************************************************/
928
929 /* Return the bottom boundary y-position for text lines in window W.
930 This is the first y position at which a line cannot start.
931 It is relative to the top of the window.
932
933 This is the height of W minus the height of a mode line, if any. */
934
935 INLINE int
936 window_text_bottom_y (struct window *w)
937 {
938 int height = WINDOW_TOTAL_HEIGHT (w);
939
940 if (WINDOW_WANTS_MODELINE_P (w))
941 height -= CURRENT_MODE_LINE_HEIGHT (w);
942 return height;
943 }
944
945 /* Return the pixel width of display area AREA of window W. AREA < 0
946 means return the total width of W, not including fringes to
947 the left and right of the window. */
948
949 INLINE int
950 window_box_width (struct window *w, int area)
951 {
952 int cols = XFASTINT (w->total_cols);
953 int pixels = 0;
954
955 if (!w->pseudo_window_p)
956 {
957 cols -= WINDOW_SCROLL_BAR_COLS (w);
958
959 if (area == TEXT_AREA)
960 {
961 if (INTEGERP (w->left_margin_cols))
962 cols -= XFASTINT (w->left_margin_cols);
963 if (INTEGERP (w->right_margin_cols))
964 cols -= XFASTINT (w->right_margin_cols);
965 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
966 }
967 else if (area == LEFT_MARGIN_AREA)
968 {
969 cols = (INTEGERP (w->left_margin_cols)
970 ? XFASTINT (w->left_margin_cols) : 0);
971 pixels = 0;
972 }
973 else if (area == RIGHT_MARGIN_AREA)
974 {
975 cols = (INTEGERP (w->right_margin_cols)
976 ? XFASTINT (w->right_margin_cols) : 0);
977 pixels = 0;
978 }
979 }
980
981 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
982 }
983
984
985 /* Return the pixel height of the display area of window W, not
986 including mode lines of W, if any. */
987
988 INLINE int
989 window_box_height (struct window *w)
990 {
991 struct frame *f = XFRAME (w->frame);
992 int height = WINDOW_TOTAL_HEIGHT (w);
993
994 xassert (height >= 0);
995
996 /* Note: the code below that determines the mode-line/header-line
997 height is essentially the same as that contained in the macro
998 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
999 the appropriate glyph row has its `mode_line_p' flag set,
1000 and if it doesn't, uses estimate_mode_line_height instead. */
1001
1002 if (WINDOW_WANTS_MODELINE_P (w))
1003 {
1004 struct glyph_row *ml_row
1005 = (w->current_matrix && w->current_matrix->rows
1006 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1007 : 0);
1008 if (ml_row && ml_row->mode_line_p)
1009 height -= ml_row->height;
1010 else
1011 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1012 }
1013
1014 if (WINDOW_WANTS_HEADER_LINE_P (w))
1015 {
1016 struct glyph_row *hl_row
1017 = (w->current_matrix && w->current_matrix->rows
1018 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1019 : 0);
1020 if (hl_row && hl_row->mode_line_p)
1021 height -= hl_row->height;
1022 else
1023 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1024 }
1025
1026 /* With a very small font and a mode-line that's taller than
1027 default, we might end up with a negative height. */
1028 return max (0, height);
1029 }
1030
1031 /* Return the window-relative coordinate of the left edge of display
1032 area AREA of window W. AREA < 0 means return the left edge of the
1033 whole window, to the right of the left fringe of W. */
1034
1035 INLINE int
1036 window_box_left_offset (struct window *w, int area)
1037 {
1038 int x;
1039
1040 if (w->pseudo_window_p)
1041 return 0;
1042
1043 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1044
1045 if (area == TEXT_AREA)
1046 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1047 + window_box_width (w, LEFT_MARGIN_AREA));
1048 else if (area == RIGHT_MARGIN_AREA)
1049 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1050 + window_box_width (w, LEFT_MARGIN_AREA)
1051 + window_box_width (w, TEXT_AREA)
1052 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1053 ? 0
1054 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1055 else if (area == LEFT_MARGIN_AREA
1056 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1057 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1058
1059 return x;
1060 }
1061
1062
1063 /* Return the window-relative coordinate of the right edge of display
1064 area AREA of window W. AREA < 0 means return the right edge of the
1065 whole window, to the left of the right fringe of W. */
1066
1067 INLINE int
1068 window_box_right_offset (struct window *w, int area)
1069 {
1070 return window_box_left_offset (w, area) + window_box_width (w, area);
1071 }
1072
1073 /* Return the frame-relative coordinate of the left edge of display
1074 area AREA of window W. AREA < 0 means return the left edge of the
1075 whole window, to the right of the left fringe of W. */
1076
1077 INLINE int
1078 window_box_left (struct window *w, int area)
1079 {
1080 struct frame *f = XFRAME (w->frame);
1081 int x;
1082
1083 if (w->pseudo_window_p)
1084 return FRAME_INTERNAL_BORDER_WIDTH (f);
1085
1086 x = (WINDOW_LEFT_EDGE_X (w)
1087 + window_box_left_offset (w, area));
1088
1089 return x;
1090 }
1091
1092
1093 /* Return the frame-relative coordinate of the right edge of display
1094 area AREA of window W. AREA < 0 means return the right edge of the
1095 whole window, to the left of the right fringe of W. */
1096
1097 INLINE int
1098 window_box_right (struct window *w, int area)
1099 {
1100 return window_box_left (w, area) + window_box_width (w, area);
1101 }
1102
1103 /* Get the bounding box of the display area AREA of window W, without
1104 mode lines, in frame-relative coordinates. AREA < 0 means the
1105 whole window, not including the left and right fringes of
1106 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1107 coordinates of the upper-left corner of the box. Return in
1108 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1109
1110 INLINE void
1111 window_box (struct window *w, int area, int *box_x, int *box_y,
1112 int *box_width, int *box_height)
1113 {
1114 if (box_width)
1115 *box_width = window_box_width (w, area);
1116 if (box_height)
1117 *box_height = window_box_height (w);
1118 if (box_x)
1119 *box_x = window_box_left (w, area);
1120 if (box_y)
1121 {
1122 *box_y = WINDOW_TOP_EDGE_Y (w);
1123 if (WINDOW_WANTS_HEADER_LINE_P (w))
1124 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1125 }
1126 }
1127
1128
1129 /* Get the bounding box of the display area AREA of window W, without
1130 mode lines. AREA < 0 means the whole window, not including the
1131 left and right fringe of the window. Return in *TOP_LEFT_X
1132 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1133 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1134 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1135 box. */
1136
1137 static INLINE void
1138 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1139 int *bottom_right_x, int *bottom_right_y)
1140 {
1141 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1142 bottom_right_y);
1143 *bottom_right_x += *top_left_x;
1144 *bottom_right_y += *top_left_y;
1145 }
1146
1147
1148 \f
1149 /***********************************************************************
1150 Utilities
1151 ***********************************************************************/
1152
1153 /* Return the bottom y-position of the line the iterator IT is in.
1154 This can modify IT's settings. */
1155
1156 int
1157 line_bottom_y (struct it *it)
1158 {
1159 int line_height = it->max_ascent + it->max_descent;
1160 int line_top_y = it->current_y;
1161
1162 if (line_height == 0)
1163 {
1164 if (last_height)
1165 line_height = last_height;
1166 else if (IT_CHARPOS (*it) < ZV)
1167 {
1168 move_it_by_lines (it, 1);
1169 line_height = (it->max_ascent || it->max_descent
1170 ? it->max_ascent + it->max_descent
1171 : last_height);
1172 }
1173 else
1174 {
1175 struct glyph_row *row = it->glyph_row;
1176
1177 /* Use the default character height. */
1178 it->glyph_row = NULL;
1179 it->what = IT_CHARACTER;
1180 it->c = ' ';
1181 it->len = 1;
1182 PRODUCE_GLYPHS (it);
1183 line_height = it->ascent + it->descent;
1184 it->glyph_row = row;
1185 }
1186 }
1187
1188 return line_top_y + line_height;
1189 }
1190
1191
1192 /* Return 1 if position CHARPOS is visible in window W.
1193 CHARPOS < 0 means return info about WINDOW_END position.
1194 If visible, set *X and *Y to pixel coordinates of top left corner.
1195 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1196 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1197
1198 int
1199 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1200 int *rtop, int *rbot, int *rowh, int *vpos)
1201 {
1202 struct it it;
1203 struct text_pos top;
1204 int visible_p = 0;
1205 struct buffer *old_buffer = NULL;
1206
1207 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1208 return visible_p;
1209
1210 if (XBUFFER (w->buffer) != current_buffer)
1211 {
1212 old_buffer = current_buffer;
1213 set_buffer_internal_1 (XBUFFER (w->buffer));
1214 }
1215
1216 SET_TEXT_POS_FROM_MARKER (top, w->start);
1217
1218 /* Compute exact mode line heights. */
1219 if (WINDOW_WANTS_MODELINE_P (w))
1220 current_mode_line_height
1221 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1222 BVAR (current_buffer, mode_line_format));
1223
1224 if (WINDOW_WANTS_HEADER_LINE_P (w))
1225 current_header_line_height
1226 = display_mode_line (w, HEADER_LINE_FACE_ID,
1227 BVAR (current_buffer, header_line_format));
1228
1229 start_display (&it, w, top);
1230 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1231 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1232
1233 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1234 {
1235 /* We have reached CHARPOS, or passed it. How the call to
1236 move_it_to can overshoot: (i) If CHARPOS is on invisible
1237 text, move_it_to stops at the end of the invisible text,
1238 after CHARPOS. (ii) If CHARPOS is in a display vector,
1239 move_it_to stops on its last glyph. */
1240 int top_x = it.current_x;
1241 int top_y = it.current_y;
1242 enum it_method it_method = it.method;
1243 /* Calling line_bottom_y may change it.method, it.position, etc. */
1244 int bottom_y = (last_height = 0, line_bottom_y (&it));
1245 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1246
1247 if (top_y < window_top_y)
1248 visible_p = bottom_y > window_top_y;
1249 else if (top_y < it.last_visible_y)
1250 visible_p = 1;
1251 if (visible_p)
1252 {
1253 if (it_method == GET_FROM_DISPLAY_VECTOR)
1254 {
1255 /* We stopped on the last glyph of a display vector.
1256 Try and recompute. Hack alert! */
1257 if (charpos < 2 || top.charpos >= charpos)
1258 top_x = it.glyph_row->x;
1259 else
1260 {
1261 struct it it2;
1262 start_display (&it2, w, top);
1263 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1264 get_next_display_element (&it2);
1265 PRODUCE_GLYPHS (&it2);
1266 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1267 || it2.current_x > it2.last_visible_x)
1268 top_x = it.glyph_row->x;
1269 else
1270 {
1271 top_x = it2.current_x;
1272 top_y = it2.current_y;
1273 }
1274 }
1275 }
1276
1277 *x = top_x;
1278 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1279 *rtop = max (0, window_top_y - top_y);
1280 *rbot = max (0, bottom_y - it.last_visible_y);
1281 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1282 - max (top_y, window_top_y)));
1283 *vpos = it.vpos;
1284 }
1285 }
1286 else
1287 {
1288 struct it it2;
1289
1290 it2 = it;
1291 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1292 move_it_by_lines (&it, 1);
1293 if (charpos < IT_CHARPOS (it)
1294 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1295 {
1296 visible_p = 1;
1297 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1298 *x = it2.current_x;
1299 *y = it2.current_y + it2.max_ascent - it2.ascent;
1300 *rtop = max (0, -it2.current_y);
1301 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1302 - it.last_visible_y));
1303 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1304 it.last_visible_y)
1305 - max (it2.current_y,
1306 WINDOW_HEADER_LINE_HEIGHT (w))));
1307 *vpos = it2.vpos;
1308 }
1309 }
1310
1311 if (old_buffer)
1312 set_buffer_internal_1 (old_buffer);
1313
1314 current_header_line_height = current_mode_line_height = -1;
1315
1316 if (visible_p && XFASTINT (w->hscroll) > 0)
1317 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1318
1319 #if 0
1320 /* Debugging code. */
1321 if (visible_p)
1322 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1323 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1324 else
1325 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1326 #endif
1327
1328 return visible_p;
1329 }
1330
1331
1332 /* Return the next character from STR. Return in *LEN the length of
1333 the character. This is like STRING_CHAR_AND_LENGTH but never
1334 returns an invalid character. If we find one, we return a `?', but
1335 with the length of the invalid character. */
1336
1337 static INLINE int
1338 string_char_and_length (const unsigned char *str, int *len)
1339 {
1340 int c;
1341
1342 c = STRING_CHAR_AND_LENGTH (str, *len);
1343 if (!CHAR_VALID_P (c, 1))
1344 /* We may not change the length here because other places in Emacs
1345 don't use this function, i.e. they silently accept invalid
1346 characters. */
1347 c = '?';
1348
1349 return c;
1350 }
1351
1352
1353
1354 /* Given a position POS containing a valid character and byte position
1355 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1356
1357 static struct text_pos
1358 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1359 {
1360 xassert (STRINGP (string) && nchars >= 0);
1361
1362 if (STRING_MULTIBYTE (string))
1363 {
1364 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1365 int len;
1366
1367 while (nchars--)
1368 {
1369 string_char_and_length (p, &len);
1370 p += len;
1371 CHARPOS (pos) += 1;
1372 BYTEPOS (pos) += len;
1373 }
1374 }
1375 else
1376 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1377
1378 return pos;
1379 }
1380
1381
1382 /* Value is the text position, i.e. character and byte position,
1383 for character position CHARPOS in STRING. */
1384
1385 static INLINE struct text_pos
1386 string_pos (EMACS_INT charpos, Lisp_Object string)
1387 {
1388 struct text_pos pos;
1389 xassert (STRINGP (string));
1390 xassert (charpos >= 0);
1391 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1392 return pos;
1393 }
1394
1395
1396 /* Value is a text position, i.e. character and byte position, for
1397 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1398 means recognize multibyte characters. */
1399
1400 static struct text_pos
1401 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1402 {
1403 struct text_pos pos;
1404
1405 xassert (s != NULL);
1406 xassert (charpos >= 0);
1407
1408 if (multibyte_p)
1409 {
1410 int len;
1411
1412 SET_TEXT_POS (pos, 0, 0);
1413 while (charpos--)
1414 {
1415 string_char_and_length ((const unsigned char *) s, &len);
1416 s += len;
1417 CHARPOS (pos) += 1;
1418 BYTEPOS (pos) += len;
1419 }
1420 }
1421 else
1422 SET_TEXT_POS (pos, charpos, charpos);
1423
1424 return pos;
1425 }
1426
1427
1428 /* Value is the number of characters in C string S. MULTIBYTE_P
1429 non-zero means recognize multibyte characters. */
1430
1431 static EMACS_INT
1432 number_of_chars (const char *s, int multibyte_p)
1433 {
1434 EMACS_INT nchars;
1435
1436 if (multibyte_p)
1437 {
1438 EMACS_INT rest = strlen (s);
1439 int len;
1440 const unsigned char *p = (const unsigned char *) s;
1441
1442 for (nchars = 0; rest > 0; ++nchars)
1443 {
1444 string_char_and_length (p, &len);
1445 rest -= len, p += len;
1446 }
1447 }
1448 else
1449 nchars = strlen (s);
1450
1451 return nchars;
1452 }
1453
1454
1455 /* Compute byte position NEWPOS->bytepos corresponding to
1456 NEWPOS->charpos. POS is a known position in string STRING.
1457 NEWPOS->charpos must be >= POS.charpos. */
1458
1459 static void
1460 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1461 {
1462 xassert (STRINGP (string));
1463 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1464
1465 if (STRING_MULTIBYTE (string))
1466 *newpos = string_pos_nchars_ahead (pos, string,
1467 CHARPOS (*newpos) - CHARPOS (pos));
1468 else
1469 BYTEPOS (*newpos) = CHARPOS (*newpos);
1470 }
1471
1472 /* EXPORT:
1473 Return an estimation of the pixel height of mode or header lines on
1474 frame F. FACE_ID specifies what line's height to estimate. */
1475
1476 int
1477 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1478 {
1479 #ifdef HAVE_WINDOW_SYSTEM
1480 if (FRAME_WINDOW_P (f))
1481 {
1482 int height = FONT_HEIGHT (FRAME_FONT (f));
1483
1484 /* This function is called so early when Emacs starts that the face
1485 cache and mode line face are not yet initialized. */
1486 if (FRAME_FACE_CACHE (f))
1487 {
1488 struct face *face = FACE_FROM_ID (f, face_id);
1489 if (face)
1490 {
1491 if (face->font)
1492 height = FONT_HEIGHT (face->font);
1493 if (face->box_line_width > 0)
1494 height += 2 * face->box_line_width;
1495 }
1496 }
1497
1498 return height;
1499 }
1500 #endif
1501
1502 return 1;
1503 }
1504
1505 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1506 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1507 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1508 not force the value into range. */
1509
1510 void
1511 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1512 int *x, int *y, NativeRectangle *bounds, int noclip)
1513 {
1514
1515 #ifdef HAVE_WINDOW_SYSTEM
1516 if (FRAME_WINDOW_P (f))
1517 {
1518 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1519 even for negative values. */
1520 if (pix_x < 0)
1521 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1522 if (pix_y < 0)
1523 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1524
1525 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1526 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1527
1528 if (bounds)
1529 STORE_NATIVE_RECT (*bounds,
1530 FRAME_COL_TO_PIXEL_X (f, pix_x),
1531 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1532 FRAME_COLUMN_WIDTH (f) - 1,
1533 FRAME_LINE_HEIGHT (f) - 1);
1534
1535 if (!noclip)
1536 {
1537 if (pix_x < 0)
1538 pix_x = 0;
1539 else if (pix_x > FRAME_TOTAL_COLS (f))
1540 pix_x = FRAME_TOTAL_COLS (f);
1541
1542 if (pix_y < 0)
1543 pix_y = 0;
1544 else if (pix_y > FRAME_LINES (f))
1545 pix_y = FRAME_LINES (f);
1546 }
1547 }
1548 #endif
1549
1550 *x = pix_x;
1551 *y = pix_y;
1552 }
1553
1554
1555 /* Find the glyph under window-relative coordinates X/Y in window W.
1556 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1557 strings. Return in *HPOS and *VPOS the row and column number of
1558 the glyph found. Return in *AREA the glyph area containing X.
1559 Value is a pointer to the glyph found or null if X/Y is not on
1560 text, or we can't tell because W's current matrix is not up to
1561 date. */
1562
1563 static
1564 struct glyph *
1565 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1566 int *dx, int *dy, int *area)
1567 {
1568 struct glyph *glyph, *end;
1569 struct glyph_row *row = NULL;
1570 int x0, i;
1571
1572 /* Find row containing Y. Give up if some row is not enabled. */
1573 for (i = 0; i < w->current_matrix->nrows; ++i)
1574 {
1575 row = MATRIX_ROW (w->current_matrix, i);
1576 if (!row->enabled_p)
1577 return NULL;
1578 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1579 break;
1580 }
1581
1582 *vpos = i;
1583 *hpos = 0;
1584
1585 /* Give up if Y is not in the window. */
1586 if (i == w->current_matrix->nrows)
1587 return NULL;
1588
1589 /* Get the glyph area containing X. */
1590 if (w->pseudo_window_p)
1591 {
1592 *area = TEXT_AREA;
1593 x0 = 0;
1594 }
1595 else
1596 {
1597 if (x < window_box_left_offset (w, TEXT_AREA))
1598 {
1599 *area = LEFT_MARGIN_AREA;
1600 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1601 }
1602 else if (x < window_box_right_offset (w, TEXT_AREA))
1603 {
1604 *area = TEXT_AREA;
1605 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1606 }
1607 else
1608 {
1609 *area = RIGHT_MARGIN_AREA;
1610 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1611 }
1612 }
1613
1614 /* Find glyph containing X. */
1615 glyph = row->glyphs[*area];
1616 end = glyph + row->used[*area];
1617 x -= x0;
1618 while (glyph < end && x >= glyph->pixel_width)
1619 {
1620 x -= glyph->pixel_width;
1621 ++glyph;
1622 }
1623
1624 if (glyph == end)
1625 return NULL;
1626
1627 if (dx)
1628 {
1629 *dx = x;
1630 *dy = y - (row->y + row->ascent - glyph->ascent);
1631 }
1632
1633 *hpos = glyph - row->glyphs[*area];
1634 return glyph;
1635 }
1636
1637 /* Convert frame-relative x/y to coordinates relative to window W.
1638 Takes pseudo-windows into account. */
1639
1640 static void
1641 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1642 {
1643 if (w->pseudo_window_p)
1644 {
1645 /* A pseudo-window is always full-width, and starts at the
1646 left edge of the frame, plus a frame border. */
1647 struct frame *f = XFRAME (w->frame);
1648 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1649 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1650 }
1651 else
1652 {
1653 *x -= WINDOW_LEFT_EDGE_X (w);
1654 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1655 }
1656 }
1657
1658 #ifdef HAVE_WINDOW_SYSTEM
1659
1660 /* EXPORT:
1661 Return in RECTS[] at most N clipping rectangles for glyph string S.
1662 Return the number of stored rectangles. */
1663
1664 int
1665 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1666 {
1667 XRectangle r;
1668
1669 if (n <= 0)
1670 return 0;
1671
1672 if (s->row->full_width_p)
1673 {
1674 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1675 r.x = WINDOW_LEFT_EDGE_X (s->w);
1676 r.width = WINDOW_TOTAL_WIDTH (s->w);
1677
1678 /* Unless displaying a mode or menu bar line, which are always
1679 fully visible, clip to the visible part of the row. */
1680 if (s->w->pseudo_window_p)
1681 r.height = s->row->visible_height;
1682 else
1683 r.height = s->height;
1684 }
1685 else
1686 {
1687 /* This is a text line that may be partially visible. */
1688 r.x = window_box_left (s->w, s->area);
1689 r.width = window_box_width (s->w, s->area);
1690 r.height = s->row->visible_height;
1691 }
1692
1693 if (s->clip_head)
1694 if (r.x < s->clip_head->x)
1695 {
1696 if (r.width >= s->clip_head->x - r.x)
1697 r.width -= s->clip_head->x - r.x;
1698 else
1699 r.width = 0;
1700 r.x = s->clip_head->x;
1701 }
1702 if (s->clip_tail)
1703 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1704 {
1705 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1706 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1707 else
1708 r.width = 0;
1709 }
1710
1711 /* If S draws overlapping rows, it's sufficient to use the top and
1712 bottom of the window for clipping because this glyph string
1713 intentionally draws over other lines. */
1714 if (s->for_overlaps)
1715 {
1716 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1717 r.height = window_text_bottom_y (s->w) - r.y;
1718
1719 /* Alas, the above simple strategy does not work for the
1720 environments with anti-aliased text: if the same text is
1721 drawn onto the same place multiple times, it gets thicker.
1722 If the overlap we are processing is for the erased cursor, we
1723 take the intersection with the rectagle of the cursor. */
1724 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1725 {
1726 XRectangle rc, r_save = r;
1727
1728 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1729 rc.y = s->w->phys_cursor.y;
1730 rc.width = s->w->phys_cursor_width;
1731 rc.height = s->w->phys_cursor_height;
1732
1733 x_intersect_rectangles (&r_save, &rc, &r);
1734 }
1735 }
1736 else
1737 {
1738 /* Don't use S->y for clipping because it doesn't take partially
1739 visible lines into account. For example, it can be negative for
1740 partially visible lines at the top of a window. */
1741 if (!s->row->full_width_p
1742 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1743 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1744 else
1745 r.y = max (0, s->row->y);
1746 }
1747
1748 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1749
1750 /* If drawing the cursor, don't let glyph draw outside its
1751 advertised boundaries. Cleartype does this under some circumstances. */
1752 if (s->hl == DRAW_CURSOR)
1753 {
1754 struct glyph *glyph = s->first_glyph;
1755 int height, max_y;
1756
1757 if (s->x > r.x)
1758 {
1759 r.width -= s->x - r.x;
1760 r.x = s->x;
1761 }
1762 r.width = min (r.width, glyph->pixel_width);
1763
1764 /* If r.y is below window bottom, ensure that we still see a cursor. */
1765 height = min (glyph->ascent + glyph->descent,
1766 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1767 max_y = window_text_bottom_y (s->w) - height;
1768 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1769 if (s->ybase - glyph->ascent > max_y)
1770 {
1771 r.y = max_y;
1772 r.height = height;
1773 }
1774 else
1775 {
1776 /* Don't draw cursor glyph taller than our actual glyph. */
1777 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1778 if (height < r.height)
1779 {
1780 max_y = r.y + r.height;
1781 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1782 r.height = min (max_y - r.y, height);
1783 }
1784 }
1785 }
1786
1787 if (s->row->clip)
1788 {
1789 XRectangle r_save = r;
1790
1791 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1792 r.width = 0;
1793 }
1794
1795 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1796 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1797 {
1798 #ifdef CONVERT_FROM_XRECT
1799 CONVERT_FROM_XRECT (r, *rects);
1800 #else
1801 *rects = r;
1802 #endif
1803 return 1;
1804 }
1805 else
1806 {
1807 /* If we are processing overlapping and allowed to return
1808 multiple clipping rectangles, we exclude the row of the glyph
1809 string from the clipping rectangle. This is to avoid drawing
1810 the same text on the environment with anti-aliasing. */
1811 #ifdef CONVERT_FROM_XRECT
1812 XRectangle rs[2];
1813 #else
1814 XRectangle *rs = rects;
1815 #endif
1816 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1817
1818 if (s->for_overlaps & OVERLAPS_PRED)
1819 {
1820 rs[i] = r;
1821 if (r.y + r.height > row_y)
1822 {
1823 if (r.y < row_y)
1824 rs[i].height = row_y - r.y;
1825 else
1826 rs[i].height = 0;
1827 }
1828 i++;
1829 }
1830 if (s->for_overlaps & OVERLAPS_SUCC)
1831 {
1832 rs[i] = r;
1833 if (r.y < row_y + s->row->visible_height)
1834 {
1835 if (r.y + r.height > row_y + s->row->visible_height)
1836 {
1837 rs[i].y = row_y + s->row->visible_height;
1838 rs[i].height = r.y + r.height - rs[i].y;
1839 }
1840 else
1841 rs[i].height = 0;
1842 }
1843 i++;
1844 }
1845
1846 n = i;
1847 #ifdef CONVERT_FROM_XRECT
1848 for (i = 0; i < n; i++)
1849 CONVERT_FROM_XRECT (rs[i], rects[i]);
1850 #endif
1851 return n;
1852 }
1853 }
1854
1855 /* EXPORT:
1856 Return in *NR the clipping rectangle for glyph string S. */
1857
1858 void
1859 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1860 {
1861 get_glyph_string_clip_rects (s, nr, 1);
1862 }
1863
1864
1865 /* EXPORT:
1866 Return the position and height of the phys cursor in window W.
1867 Set w->phys_cursor_width to width of phys cursor.
1868 */
1869
1870 void
1871 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1872 struct glyph *glyph, int *xp, int *yp, int *heightp)
1873 {
1874 struct frame *f = XFRAME (WINDOW_FRAME (w));
1875 int x, y, wd, h, h0, y0;
1876
1877 /* Compute the width of the rectangle to draw. If on a stretch
1878 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1879 rectangle as wide as the glyph, but use a canonical character
1880 width instead. */
1881 wd = glyph->pixel_width - 1;
1882 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1883 wd++; /* Why? */
1884 #endif
1885
1886 x = w->phys_cursor.x;
1887 if (x < 0)
1888 {
1889 wd += x;
1890 x = 0;
1891 }
1892
1893 if (glyph->type == STRETCH_GLYPH
1894 && !x_stretch_cursor_p)
1895 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1896 w->phys_cursor_width = wd;
1897
1898 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1899
1900 /* If y is below window bottom, ensure that we still see a cursor. */
1901 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1902
1903 h = max (h0, glyph->ascent + glyph->descent);
1904 h0 = min (h0, glyph->ascent + glyph->descent);
1905
1906 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1907 if (y < y0)
1908 {
1909 h = max (h - (y0 - y) + 1, h0);
1910 y = y0 - 1;
1911 }
1912 else
1913 {
1914 y0 = window_text_bottom_y (w) - h0;
1915 if (y > y0)
1916 {
1917 h += y - y0;
1918 y = y0;
1919 }
1920 }
1921
1922 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1923 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1924 *heightp = h;
1925 }
1926
1927 /*
1928 * Remember which glyph the mouse is over.
1929 */
1930
1931 void
1932 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1933 {
1934 Lisp_Object window;
1935 struct window *w;
1936 struct glyph_row *r, *gr, *end_row;
1937 enum window_part part;
1938 enum glyph_row_area area;
1939 int x, y, width, height;
1940
1941 /* Try to determine frame pixel position and size of the glyph under
1942 frame pixel coordinates X/Y on frame F. */
1943
1944 if (!f->glyphs_initialized_p
1945 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1946 NILP (window)))
1947 {
1948 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1949 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1950 goto virtual_glyph;
1951 }
1952
1953 w = XWINDOW (window);
1954 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1955 height = WINDOW_FRAME_LINE_HEIGHT (w);
1956
1957 x = window_relative_x_coord (w, part, gx);
1958 y = gy - WINDOW_TOP_EDGE_Y (w);
1959
1960 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1961 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1962
1963 if (w->pseudo_window_p)
1964 {
1965 area = TEXT_AREA;
1966 part = ON_MODE_LINE; /* Don't adjust margin. */
1967 goto text_glyph;
1968 }
1969
1970 switch (part)
1971 {
1972 case ON_LEFT_MARGIN:
1973 area = LEFT_MARGIN_AREA;
1974 goto text_glyph;
1975
1976 case ON_RIGHT_MARGIN:
1977 area = RIGHT_MARGIN_AREA;
1978 goto text_glyph;
1979
1980 case ON_HEADER_LINE:
1981 case ON_MODE_LINE:
1982 gr = (part == ON_HEADER_LINE
1983 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1984 : MATRIX_MODE_LINE_ROW (w->current_matrix));
1985 gy = gr->y;
1986 area = TEXT_AREA;
1987 goto text_glyph_row_found;
1988
1989 case ON_TEXT:
1990 area = TEXT_AREA;
1991
1992 text_glyph:
1993 gr = 0; gy = 0;
1994 for (; r <= end_row && r->enabled_p; ++r)
1995 if (r->y + r->height > y)
1996 {
1997 gr = r; gy = r->y;
1998 break;
1999 }
2000
2001 text_glyph_row_found:
2002 if (gr && gy <= y)
2003 {
2004 struct glyph *g = gr->glyphs[area];
2005 struct glyph *end = g + gr->used[area];
2006
2007 height = gr->height;
2008 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2009 if (gx + g->pixel_width > x)
2010 break;
2011
2012 if (g < end)
2013 {
2014 if (g->type == IMAGE_GLYPH)
2015 {
2016 /* Don't remember when mouse is over image, as
2017 image may have hot-spots. */
2018 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2019 return;
2020 }
2021 width = g->pixel_width;
2022 }
2023 else
2024 {
2025 /* Use nominal char spacing at end of line. */
2026 x -= gx;
2027 gx += (x / width) * width;
2028 }
2029
2030 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2031 gx += window_box_left_offset (w, area);
2032 }
2033 else
2034 {
2035 /* Use nominal line height at end of window. */
2036 gx = (x / width) * width;
2037 y -= gy;
2038 gy += (y / height) * height;
2039 }
2040 break;
2041
2042 case ON_LEFT_FRINGE:
2043 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2044 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2045 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2046 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2047 goto row_glyph;
2048
2049 case ON_RIGHT_FRINGE:
2050 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2051 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2052 : window_box_right_offset (w, TEXT_AREA));
2053 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2054 goto row_glyph;
2055
2056 case ON_SCROLL_BAR:
2057 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2058 ? 0
2059 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2060 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2061 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2062 : 0)));
2063 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2064
2065 row_glyph:
2066 gr = 0, gy = 0;
2067 for (; r <= end_row && r->enabled_p; ++r)
2068 if (r->y + r->height > y)
2069 {
2070 gr = r; gy = r->y;
2071 break;
2072 }
2073
2074 if (gr && gy <= y)
2075 height = gr->height;
2076 else
2077 {
2078 /* Use nominal line height at end of window. */
2079 y -= gy;
2080 gy += (y / height) * height;
2081 }
2082 break;
2083
2084 default:
2085 ;
2086 virtual_glyph:
2087 /* If there is no glyph under the mouse, then we divide the screen
2088 into a grid of the smallest glyph in the frame, and use that
2089 as our "glyph". */
2090
2091 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2092 round down even for negative values. */
2093 if (gx < 0)
2094 gx -= width - 1;
2095 if (gy < 0)
2096 gy -= height - 1;
2097
2098 gx = (gx / width) * width;
2099 gy = (gy / height) * height;
2100
2101 goto store_rect;
2102 }
2103
2104 gx += WINDOW_LEFT_EDGE_X (w);
2105 gy += WINDOW_TOP_EDGE_Y (w);
2106
2107 store_rect:
2108 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2109
2110 /* Visible feedback for debugging. */
2111 #if 0
2112 #if HAVE_X_WINDOWS
2113 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2114 f->output_data.x->normal_gc,
2115 gx, gy, width, height);
2116 #endif
2117 #endif
2118 }
2119
2120
2121 #endif /* HAVE_WINDOW_SYSTEM */
2122
2123 \f
2124 /***********************************************************************
2125 Lisp form evaluation
2126 ***********************************************************************/
2127
2128 /* Error handler for safe_eval and safe_call. */
2129
2130 static Lisp_Object
2131 safe_eval_handler (Lisp_Object arg)
2132 {
2133 add_to_log ("Error during redisplay: %S", arg, Qnil);
2134 return Qnil;
2135 }
2136
2137
2138 /* Evaluate SEXPR and return the result, or nil if something went
2139 wrong. Prevent redisplay during the evaluation. */
2140
2141 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2142 Return the result, or nil if something went wrong. Prevent
2143 redisplay during the evaluation. */
2144
2145 Lisp_Object
2146 safe_call (size_t nargs, Lisp_Object *args)
2147 {
2148 Lisp_Object val;
2149
2150 if (inhibit_eval_during_redisplay)
2151 val = Qnil;
2152 else
2153 {
2154 int count = SPECPDL_INDEX ();
2155 struct gcpro gcpro1;
2156
2157 GCPRO1 (args[0]);
2158 gcpro1.nvars = nargs;
2159 specbind (Qinhibit_redisplay, Qt);
2160 /* Use Qt to ensure debugger does not run,
2161 so there is no possibility of wanting to redisplay. */
2162 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2163 safe_eval_handler);
2164 UNGCPRO;
2165 val = unbind_to (count, val);
2166 }
2167
2168 return val;
2169 }
2170
2171
2172 /* Call function FN with one argument ARG.
2173 Return the result, or nil if something went wrong. */
2174
2175 Lisp_Object
2176 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2177 {
2178 Lisp_Object args[2];
2179 args[0] = fn;
2180 args[1] = arg;
2181 return safe_call (2, args);
2182 }
2183
2184 static Lisp_Object Qeval;
2185
2186 Lisp_Object
2187 safe_eval (Lisp_Object sexpr)
2188 {
2189 return safe_call1 (Qeval, sexpr);
2190 }
2191
2192 /* Call function FN with one argument ARG.
2193 Return the result, or nil if something went wrong. */
2194
2195 Lisp_Object
2196 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2197 {
2198 Lisp_Object args[3];
2199 args[0] = fn;
2200 args[1] = arg1;
2201 args[2] = arg2;
2202 return safe_call (3, args);
2203 }
2204
2205
2206 \f
2207 /***********************************************************************
2208 Debugging
2209 ***********************************************************************/
2210
2211 #if 0
2212
2213 /* Define CHECK_IT to perform sanity checks on iterators.
2214 This is for debugging. It is too slow to do unconditionally. */
2215
2216 static void
2217 check_it (it)
2218 struct it *it;
2219 {
2220 if (it->method == GET_FROM_STRING)
2221 {
2222 xassert (STRINGP (it->string));
2223 xassert (IT_STRING_CHARPOS (*it) >= 0);
2224 }
2225 else
2226 {
2227 xassert (IT_STRING_CHARPOS (*it) < 0);
2228 if (it->method == GET_FROM_BUFFER)
2229 {
2230 /* Check that character and byte positions agree. */
2231 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2232 }
2233 }
2234
2235 if (it->dpvec)
2236 xassert (it->current.dpvec_index >= 0);
2237 else
2238 xassert (it->current.dpvec_index < 0);
2239 }
2240
2241 #define CHECK_IT(IT) check_it ((IT))
2242
2243 #else /* not 0 */
2244
2245 #define CHECK_IT(IT) (void) 0
2246
2247 #endif /* not 0 */
2248
2249
2250 #if GLYPH_DEBUG
2251
2252 /* Check that the window end of window W is what we expect it
2253 to be---the last row in the current matrix displaying text. */
2254
2255 static void
2256 check_window_end (w)
2257 struct window *w;
2258 {
2259 if (!MINI_WINDOW_P (w)
2260 && !NILP (w->window_end_valid))
2261 {
2262 struct glyph_row *row;
2263 xassert ((row = MATRIX_ROW (w->current_matrix,
2264 XFASTINT (w->window_end_vpos)),
2265 !row->enabled_p
2266 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2267 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2268 }
2269 }
2270
2271 #define CHECK_WINDOW_END(W) check_window_end ((W))
2272
2273 #else /* not GLYPH_DEBUG */
2274
2275 #define CHECK_WINDOW_END(W) (void) 0
2276
2277 #endif /* not GLYPH_DEBUG */
2278
2279
2280 \f
2281 /***********************************************************************
2282 Iterator initialization
2283 ***********************************************************************/
2284
2285 /* Initialize IT for displaying current_buffer in window W, starting
2286 at character position CHARPOS. CHARPOS < 0 means that no buffer
2287 position is specified which is useful when the iterator is assigned
2288 a position later. BYTEPOS is the byte position corresponding to
2289 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2290
2291 If ROW is not null, calls to produce_glyphs with IT as parameter
2292 will produce glyphs in that row.
2293
2294 BASE_FACE_ID is the id of a base face to use. It must be one of
2295 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2296 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2297 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2298
2299 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2300 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2301 will be initialized to use the corresponding mode line glyph row of
2302 the desired matrix of W. */
2303
2304 void
2305 init_iterator (struct it *it, struct window *w,
2306 EMACS_INT charpos, EMACS_INT bytepos,
2307 struct glyph_row *row, enum face_id base_face_id)
2308 {
2309 int highlight_region_p;
2310 enum face_id remapped_base_face_id = base_face_id;
2311
2312 /* Some precondition checks. */
2313 xassert (w != NULL && it != NULL);
2314 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2315 && charpos <= ZV));
2316
2317 /* If face attributes have been changed since the last redisplay,
2318 free realized faces now because they depend on face definitions
2319 that might have changed. Don't free faces while there might be
2320 desired matrices pending which reference these faces. */
2321 if (face_change_count && !inhibit_free_realized_faces)
2322 {
2323 face_change_count = 0;
2324 free_all_realized_faces (Qnil);
2325 }
2326
2327 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2328 if (! NILP (Vface_remapping_alist))
2329 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2330
2331 /* Use one of the mode line rows of W's desired matrix if
2332 appropriate. */
2333 if (row == NULL)
2334 {
2335 if (base_face_id == MODE_LINE_FACE_ID
2336 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2337 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2338 else if (base_face_id == HEADER_LINE_FACE_ID)
2339 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2340 }
2341
2342 /* Clear IT. */
2343 memset (it, 0, sizeof *it);
2344 it->current.overlay_string_index = -1;
2345 it->current.dpvec_index = -1;
2346 it->base_face_id = remapped_base_face_id;
2347 it->string = Qnil;
2348 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2349 it->paragraph_embedding = L2R;
2350 it->bidi_it.string.lstring = Qnil;
2351 it->bidi_it.string.s = NULL;
2352 it->bidi_it.string.bufpos = 0;
2353
2354 /* The window in which we iterate over current_buffer: */
2355 XSETWINDOW (it->window, w);
2356 it->w = w;
2357 it->f = XFRAME (w->frame);
2358
2359 it->cmp_it.id = -1;
2360
2361 /* Extra space between lines (on window systems only). */
2362 if (base_face_id == DEFAULT_FACE_ID
2363 && FRAME_WINDOW_P (it->f))
2364 {
2365 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2366 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2367 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2368 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2369 * FRAME_LINE_HEIGHT (it->f));
2370 else if (it->f->extra_line_spacing > 0)
2371 it->extra_line_spacing = it->f->extra_line_spacing;
2372 it->max_extra_line_spacing = 0;
2373 }
2374
2375 /* If realized faces have been removed, e.g. because of face
2376 attribute changes of named faces, recompute them. When running
2377 in batch mode, the face cache of the initial frame is null. If
2378 we happen to get called, make a dummy face cache. */
2379 if (FRAME_FACE_CACHE (it->f) == NULL)
2380 init_frame_faces (it->f);
2381 if (FRAME_FACE_CACHE (it->f)->used == 0)
2382 recompute_basic_faces (it->f);
2383
2384 /* Current value of the `slice', `space-width', and 'height' properties. */
2385 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2386 it->space_width = Qnil;
2387 it->font_height = Qnil;
2388 it->override_ascent = -1;
2389
2390 /* Are control characters displayed as `^C'? */
2391 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2392
2393 /* -1 means everything between a CR and the following line end
2394 is invisible. >0 means lines indented more than this value are
2395 invisible. */
2396 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2397 ? XFASTINT (BVAR (current_buffer, selective_display))
2398 : (!NILP (BVAR (current_buffer, selective_display))
2399 ? -1 : 0));
2400 it->selective_display_ellipsis_p
2401 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2402
2403 /* Display table to use. */
2404 it->dp = window_display_table (w);
2405
2406 /* Are multibyte characters enabled in current_buffer? */
2407 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2408
2409 /* Do we need to reorder bidirectional text? Not if this is a
2410 unibyte buffer: by definition, none of the single-byte characters
2411 are strong R2L, so no reordering is needed. And bidi.c doesn't
2412 support unibyte buffers anyway. */
2413 it->bidi_p
2414 = !NILP (BVAR (current_buffer, bidi_display_reordering)) && it->multibyte_p;
2415
2416 /* Non-zero if we should highlight the region. */
2417 highlight_region_p
2418 = (!NILP (Vtransient_mark_mode)
2419 && !NILP (BVAR (current_buffer, mark_active))
2420 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2421
2422 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2423 start and end of a visible region in window IT->w. Set both to
2424 -1 to indicate no region. */
2425 if (highlight_region_p
2426 /* Maybe highlight only in selected window. */
2427 && (/* Either show region everywhere. */
2428 highlight_nonselected_windows
2429 /* Or show region in the selected window. */
2430 || w == XWINDOW (selected_window)
2431 /* Or show the region if we are in the mini-buffer and W is
2432 the window the mini-buffer refers to. */
2433 || (MINI_WINDOW_P (XWINDOW (selected_window))
2434 && WINDOWP (minibuf_selected_window)
2435 && w == XWINDOW (minibuf_selected_window))))
2436 {
2437 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2438 it->region_beg_charpos = min (PT, markpos);
2439 it->region_end_charpos = max (PT, markpos);
2440 }
2441 else
2442 it->region_beg_charpos = it->region_end_charpos = -1;
2443
2444 /* Get the position at which the redisplay_end_trigger hook should
2445 be run, if it is to be run at all. */
2446 if (MARKERP (w->redisplay_end_trigger)
2447 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2448 it->redisplay_end_trigger_charpos
2449 = marker_position (w->redisplay_end_trigger);
2450 else if (INTEGERP (w->redisplay_end_trigger))
2451 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2452
2453 /* Correct bogus values of tab_width. */
2454 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2455 if (it->tab_width <= 0 || it->tab_width > 1000)
2456 it->tab_width = 8;
2457
2458 /* Are lines in the display truncated? */
2459 if (base_face_id != DEFAULT_FACE_ID
2460 || XINT (it->w->hscroll)
2461 || (! WINDOW_FULL_WIDTH_P (it->w)
2462 && ((!NILP (Vtruncate_partial_width_windows)
2463 && !INTEGERP (Vtruncate_partial_width_windows))
2464 || (INTEGERP (Vtruncate_partial_width_windows)
2465 && (WINDOW_TOTAL_COLS (it->w)
2466 < XINT (Vtruncate_partial_width_windows))))))
2467 it->line_wrap = TRUNCATE;
2468 else if (NILP (BVAR (current_buffer, truncate_lines)))
2469 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2470 ? WINDOW_WRAP : WORD_WRAP;
2471 else
2472 it->line_wrap = TRUNCATE;
2473
2474 /* Get dimensions of truncation and continuation glyphs. These are
2475 displayed as fringe bitmaps under X, so we don't need them for such
2476 frames. */
2477 if (!FRAME_WINDOW_P (it->f))
2478 {
2479 if (it->line_wrap == TRUNCATE)
2480 {
2481 /* We will need the truncation glyph. */
2482 xassert (it->glyph_row == NULL);
2483 produce_special_glyphs (it, IT_TRUNCATION);
2484 it->truncation_pixel_width = it->pixel_width;
2485 }
2486 else
2487 {
2488 /* We will need the continuation glyph. */
2489 xassert (it->glyph_row == NULL);
2490 produce_special_glyphs (it, IT_CONTINUATION);
2491 it->continuation_pixel_width = it->pixel_width;
2492 }
2493
2494 /* Reset these values to zero because the produce_special_glyphs
2495 above has changed them. */
2496 it->pixel_width = it->ascent = it->descent = 0;
2497 it->phys_ascent = it->phys_descent = 0;
2498 }
2499
2500 /* Set this after getting the dimensions of truncation and
2501 continuation glyphs, so that we don't produce glyphs when calling
2502 produce_special_glyphs, above. */
2503 it->glyph_row = row;
2504 it->area = TEXT_AREA;
2505
2506 /* Forget any previous info about this row being reversed. */
2507 if (it->glyph_row)
2508 it->glyph_row->reversed_p = 0;
2509
2510 /* Get the dimensions of the display area. The display area
2511 consists of the visible window area plus a horizontally scrolled
2512 part to the left of the window. All x-values are relative to the
2513 start of this total display area. */
2514 if (base_face_id != DEFAULT_FACE_ID)
2515 {
2516 /* Mode lines, menu bar in terminal frames. */
2517 it->first_visible_x = 0;
2518 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2519 }
2520 else
2521 {
2522 it->first_visible_x
2523 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2524 it->last_visible_x = (it->first_visible_x
2525 + window_box_width (w, TEXT_AREA));
2526
2527 /* If we truncate lines, leave room for the truncator glyph(s) at
2528 the right margin. Otherwise, leave room for the continuation
2529 glyph(s). Truncation and continuation glyphs are not inserted
2530 for window-based redisplay. */
2531 if (!FRAME_WINDOW_P (it->f))
2532 {
2533 if (it->line_wrap == TRUNCATE)
2534 it->last_visible_x -= it->truncation_pixel_width;
2535 else
2536 it->last_visible_x -= it->continuation_pixel_width;
2537 }
2538
2539 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2540 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2541 }
2542
2543 /* Leave room for a border glyph. */
2544 if (!FRAME_WINDOW_P (it->f)
2545 && !WINDOW_RIGHTMOST_P (it->w))
2546 it->last_visible_x -= 1;
2547
2548 it->last_visible_y = window_text_bottom_y (w);
2549
2550 /* For mode lines and alike, arrange for the first glyph having a
2551 left box line if the face specifies a box. */
2552 if (base_face_id != DEFAULT_FACE_ID)
2553 {
2554 struct face *face;
2555
2556 it->face_id = remapped_base_face_id;
2557
2558 /* If we have a boxed mode line, make the first character appear
2559 with a left box line. */
2560 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2561 if (face->box != FACE_NO_BOX)
2562 it->start_of_box_run_p = 1;
2563 }
2564
2565 /* If a buffer position was specified, set the iterator there,
2566 getting overlays and face properties from that position. */
2567 if (charpos >= BUF_BEG (current_buffer))
2568 {
2569 it->end_charpos = ZV;
2570 it->face_id = -1;
2571 IT_CHARPOS (*it) = charpos;
2572
2573 /* Compute byte position if not specified. */
2574 if (bytepos < charpos)
2575 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2576 else
2577 IT_BYTEPOS (*it) = bytepos;
2578
2579 it->start = it->current;
2580
2581 /* If we are to reorder bidirectional text, init the bidi
2582 iterator. */
2583 if (it->bidi_p)
2584 {
2585 /* Note the paragraph direction that this buffer wants to
2586 use. */
2587 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2588 Qleft_to_right))
2589 it->paragraph_embedding = L2R;
2590 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2591 Qright_to_left))
2592 it->paragraph_embedding = R2L;
2593 else
2594 it->paragraph_embedding = NEUTRAL_DIR;
2595 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2596 &it->bidi_it);
2597 }
2598
2599 /* Compute faces etc. */
2600 reseat (it, it->current.pos, 1);
2601 }
2602
2603 CHECK_IT (it);
2604 }
2605
2606
2607 /* Initialize IT for the display of window W with window start POS. */
2608
2609 void
2610 start_display (struct it *it, struct window *w, struct text_pos pos)
2611 {
2612 struct glyph_row *row;
2613 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2614
2615 row = w->desired_matrix->rows + first_vpos;
2616 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2617 it->first_vpos = first_vpos;
2618
2619 /* Don't reseat to previous visible line start if current start
2620 position is in a string or image. */
2621 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2622 {
2623 int start_at_line_beg_p;
2624 int first_y = it->current_y;
2625
2626 /* If window start is not at a line start, skip forward to POS to
2627 get the correct continuation lines width. */
2628 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2629 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2630 if (!start_at_line_beg_p)
2631 {
2632 int new_x;
2633
2634 reseat_at_previous_visible_line_start (it);
2635 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2636
2637 new_x = it->current_x + it->pixel_width;
2638
2639 /* If lines are continued, this line may end in the middle
2640 of a multi-glyph character (e.g. a control character
2641 displayed as \003, or in the middle of an overlay
2642 string). In this case move_it_to above will not have
2643 taken us to the start of the continuation line but to the
2644 end of the continued line. */
2645 if (it->current_x > 0
2646 && it->line_wrap != TRUNCATE /* Lines are continued. */
2647 && (/* And glyph doesn't fit on the line. */
2648 new_x > it->last_visible_x
2649 /* Or it fits exactly and we're on a window
2650 system frame. */
2651 || (new_x == it->last_visible_x
2652 && FRAME_WINDOW_P (it->f))))
2653 {
2654 if (it->current.dpvec_index >= 0
2655 || it->current.overlay_string_index >= 0)
2656 {
2657 set_iterator_to_next (it, 1);
2658 move_it_in_display_line_to (it, -1, -1, 0);
2659 }
2660
2661 it->continuation_lines_width += it->current_x;
2662 }
2663
2664 /* We're starting a new display line, not affected by the
2665 height of the continued line, so clear the appropriate
2666 fields in the iterator structure. */
2667 it->max_ascent = it->max_descent = 0;
2668 it->max_phys_ascent = it->max_phys_descent = 0;
2669
2670 it->current_y = first_y;
2671 it->vpos = 0;
2672 it->current_x = it->hpos = 0;
2673 }
2674 }
2675 }
2676
2677
2678 /* Return 1 if POS is a position in ellipses displayed for invisible
2679 text. W is the window we display, for text property lookup. */
2680
2681 static int
2682 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2683 {
2684 Lisp_Object prop, window;
2685 int ellipses_p = 0;
2686 EMACS_INT charpos = CHARPOS (pos->pos);
2687
2688 /* If POS specifies a position in a display vector, this might
2689 be for an ellipsis displayed for invisible text. We won't
2690 get the iterator set up for delivering that ellipsis unless
2691 we make sure that it gets aware of the invisible text. */
2692 if (pos->dpvec_index >= 0
2693 && pos->overlay_string_index < 0
2694 && CHARPOS (pos->string_pos) < 0
2695 && charpos > BEGV
2696 && (XSETWINDOW (window, w),
2697 prop = Fget_char_property (make_number (charpos),
2698 Qinvisible, window),
2699 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2700 {
2701 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2702 window);
2703 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2704 }
2705
2706 return ellipses_p;
2707 }
2708
2709
2710 /* Initialize IT for stepping through current_buffer in window W,
2711 starting at position POS that includes overlay string and display
2712 vector/ control character translation position information. Value
2713 is zero if there are overlay strings with newlines at POS. */
2714
2715 static int
2716 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2717 {
2718 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2719 int i, overlay_strings_with_newlines = 0;
2720
2721 /* If POS specifies a position in a display vector, this might
2722 be for an ellipsis displayed for invisible text. We won't
2723 get the iterator set up for delivering that ellipsis unless
2724 we make sure that it gets aware of the invisible text. */
2725 if (in_ellipses_for_invisible_text_p (pos, w))
2726 {
2727 --charpos;
2728 bytepos = 0;
2729 }
2730
2731 /* Keep in mind: the call to reseat in init_iterator skips invisible
2732 text, so we might end up at a position different from POS. This
2733 is only a problem when POS is a row start after a newline and an
2734 overlay starts there with an after-string, and the overlay has an
2735 invisible property. Since we don't skip invisible text in
2736 display_line and elsewhere immediately after consuming the
2737 newline before the row start, such a POS will not be in a string,
2738 but the call to init_iterator below will move us to the
2739 after-string. */
2740 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2741
2742 /* This only scans the current chunk -- it should scan all chunks.
2743 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2744 to 16 in 22.1 to make this a lesser problem. */
2745 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2746 {
2747 const char *s = SSDATA (it->overlay_strings[i]);
2748 const char *e = s + SBYTES (it->overlay_strings[i]);
2749
2750 while (s < e && *s != '\n')
2751 ++s;
2752
2753 if (s < e)
2754 {
2755 overlay_strings_with_newlines = 1;
2756 break;
2757 }
2758 }
2759
2760 /* If position is within an overlay string, set up IT to the right
2761 overlay string. */
2762 if (pos->overlay_string_index >= 0)
2763 {
2764 int relative_index;
2765
2766 /* If the first overlay string happens to have a `display'
2767 property for an image, the iterator will be set up for that
2768 image, and we have to undo that setup first before we can
2769 correct the overlay string index. */
2770 if (it->method == GET_FROM_IMAGE)
2771 pop_it (it);
2772
2773 /* We already have the first chunk of overlay strings in
2774 IT->overlay_strings. Load more until the one for
2775 pos->overlay_string_index is in IT->overlay_strings. */
2776 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2777 {
2778 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2779 it->current.overlay_string_index = 0;
2780 while (n--)
2781 {
2782 load_overlay_strings (it, 0);
2783 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2784 }
2785 }
2786
2787 it->current.overlay_string_index = pos->overlay_string_index;
2788 relative_index = (it->current.overlay_string_index
2789 % OVERLAY_STRING_CHUNK_SIZE);
2790 it->string = it->overlay_strings[relative_index];
2791 xassert (STRINGP (it->string));
2792 it->current.string_pos = pos->string_pos;
2793 it->method = GET_FROM_STRING;
2794 }
2795
2796 if (CHARPOS (pos->string_pos) >= 0)
2797 {
2798 /* Recorded position is not in an overlay string, but in another
2799 string. This can only be a string from a `display' property.
2800 IT should already be filled with that string. */
2801 it->current.string_pos = pos->string_pos;
2802 xassert (STRINGP (it->string));
2803 }
2804
2805 /* Restore position in display vector translations, control
2806 character translations or ellipses. */
2807 if (pos->dpvec_index >= 0)
2808 {
2809 if (it->dpvec == NULL)
2810 get_next_display_element (it);
2811 xassert (it->dpvec && it->current.dpvec_index == 0);
2812 it->current.dpvec_index = pos->dpvec_index;
2813 }
2814
2815 CHECK_IT (it);
2816 return !overlay_strings_with_newlines;
2817 }
2818
2819
2820 /* Initialize IT for stepping through current_buffer in window W
2821 starting at ROW->start. */
2822
2823 static void
2824 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2825 {
2826 init_from_display_pos (it, w, &row->start);
2827 it->start = row->start;
2828 it->continuation_lines_width = row->continuation_lines_width;
2829 CHECK_IT (it);
2830 }
2831
2832
2833 /* Initialize IT for stepping through current_buffer in window W
2834 starting in the line following ROW, i.e. starting at ROW->end.
2835 Value is zero if there are overlay strings with newlines at ROW's
2836 end position. */
2837
2838 static int
2839 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2840 {
2841 int success = 0;
2842
2843 if (init_from_display_pos (it, w, &row->end))
2844 {
2845 if (row->continued_p)
2846 it->continuation_lines_width
2847 = row->continuation_lines_width + row->pixel_width;
2848 CHECK_IT (it);
2849 success = 1;
2850 }
2851
2852 return success;
2853 }
2854
2855
2856
2857 \f
2858 /***********************************************************************
2859 Text properties
2860 ***********************************************************************/
2861
2862 /* Called when IT reaches IT->stop_charpos. Handle text property and
2863 overlay changes. Set IT->stop_charpos to the next position where
2864 to stop. */
2865
2866 static void
2867 handle_stop (struct it *it)
2868 {
2869 enum prop_handled handled;
2870 int handle_overlay_change_p;
2871 struct props *p;
2872
2873 it->dpvec = NULL;
2874 it->current.dpvec_index = -1;
2875 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2876 it->ignore_overlay_strings_at_pos_p = 0;
2877 it->ellipsis_p = 0;
2878
2879 /* Use face of preceding text for ellipsis (if invisible) */
2880 if (it->selective_display_ellipsis_p)
2881 it->saved_face_id = it->face_id;
2882
2883 do
2884 {
2885 handled = HANDLED_NORMALLY;
2886
2887 /* Call text property handlers. */
2888 for (p = it_props; p->handler; ++p)
2889 {
2890 handled = p->handler (it);
2891
2892 if (handled == HANDLED_RECOMPUTE_PROPS)
2893 break;
2894 else if (handled == HANDLED_RETURN)
2895 {
2896 /* We still want to show before and after strings from
2897 overlays even if the actual buffer text is replaced. */
2898 if (!handle_overlay_change_p
2899 || it->sp > 1
2900 || !get_overlay_strings_1 (it, 0, 0))
2901 {
2902 if (it->ellipsis_p)
2903 setup_for_ellipsis (it, 0);
2904 /* When handling a display spec, we might load an
2905 empty string. In that case, discard it here. We
2906 used to discard it in handle_single_display_spec,
2907 but that causes get_overlay_strings_1, above, to
2908 ignore overlay strings that we must check. */
2909 if (STRINGP (it->string) && !SCHARS (it->string))
2910 pop_it (it);
2911 return;
2912 }
2913 else if (STRINGP (it->string) && !SCHARS (it->string))
2914 pop_it (it);
2915 else
2916 {
2917 it->ignore_overlay_strings_at_pos_p = 1;
2918 it->string_from_display_prop_p = 0;
2919 handle_overlay_change_p = 0;
2920 }
2921 handled = HANDLED_RECOMPUTE_PROPS;
2922 break;
2923 }
2924 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2925 handle_overlay_change_p = 0;
2926 }
2927
2928 if (handled != HANDLED_RECOMPUTE_PROPS)
2929 {
2930 /* Don't check for overlay strings below when set to deliver
2931 characters from a display vector. */
2932 if (it->method == GET_FROM_DISPLAY_VECTOR)
2933 handle_overlay_change_p = 0;
2934
2935 /* Handle overlay changes.
2936 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2937 if it finds overlays. */
2938 if (handle_overlay_change_p)
2939 handled = handle_overlay_change (it);
2940 }
2941
2942 if (it->ellipsis_p)
2943 {
2944 setup_for_ellipsis (it, 0);
2945 break;
2946 }
2947 }
2948 while (handled == HANDLED_RECOMPUTE_PROPS);
2949
2950 /* Determine where to stop next. */
2951 if (handled == HANDLED_NORMALLY)
2952 compute_stop_pos (it);
2953 }
2954
2955
2956 /* Compute IT->stop_charpos from text property and overlay change
2957 information for IT's current position. */
2958
2959 static void
2960 compute_stop_pos (struct it *it)
2961 {
2962 register INTERVAL iv, next_iv;
2963 Lisp_Object object, limit, position;
2964 EMACS_INT charpos, bytepos;
2965
2966 /* If nowhere else, stop at the end. */
2967 it->stop_charpos = it->end_charpos;
2968
2969 if (STRINGP (it->string))
2970 {
2971 /* Strings are usually short, so don't limit the search for
2972 properties. */
2973 object = it->string;
2974 limit = Qnil;
2975 charpos = IT_STRING_CHARPOS (*it);
2976 bytepos = IT_STRING_BYTEPOS (*it);
2977 }
2978 else
2979 {
2980 EMACS_INT pos;
2981
2982 /* If next overlay change is in front of the current stop pos
2983 (which is IT->end_charpos), stop there. Note: value of
2984 next_overlay_change is point-max if no overlay change
2985 follows. */
2986 charpos = IT_CHARPOS (*it);
2987 bytepos = IT_BYTEPOS (*it);
2988 pos = next_overlay_change (charpos);
2989 if (pos < it->stop_charpos)
2990 it->stop_charpos = pos;
2991
2992 /* If showing the region, we have to stop at the region
2993 start or end because the face might change there. */
2994 if (it->region_beg_charpos > 0)
2995 {
2996 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2997 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2998 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2999 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3000 }
3001
3002 /* Set up variables for computing the stop position from text
3003 property changes. */
3004 XSETBUFFER (object, current_buffer);
3005 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3006 }
3007
3008 /* Get the interval containing IT's position. Value is a null
3009 interval if there isn't such an interval. */
3010 position = make_number (charpos);
3011 iv = validate_interval_range (object, &position, &position, 0);
3012 if (!NULL_INTERVAL_P (iv))
3013 {
3014 Lisp_Object values_here[LAST_PROP_IDX];
3015 struct props *p;
3016
3017 /* Get properties here. */
3018 for (p = it_props; p->handler; ++p)
3019 values_here[p->idx] = textget (iv->plist, *p->name);
3020
3021 /* Look for an interval following iv that has different
3022 properties. */
3023 for (next_iv = next_interval (iv);
3024 (!NULL_INTERVAL_P (next_iv)
3025 && (NILP (limit)
3026 || XFASTINT (limit) > next_iv->position));
3027 next_iv = next_interval (next_iv))
3028 {
3029 for (p = it_props; p->handler; ++p)
3030 {
3031 Lisp_Object new_value;
3032
3033 new_value = textget (next_iv->plist, *p->name);
3034 if (!EQ (values_here[p->idx], new_value))
3035 break;
3036 }
3037
3038 if (p->handler)
3039 break;
3040 }
3041
3042 if (!NULL_INTERVAL_P (next_iv))
3043 {
3044 if (INTEGERP (limit)
3045 && next_iv->position >= XFASTINT (limit))
3046 /* No text property change up to limit. */
3047 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3048 else
3049 /* Text properties change in next_iv. */
3050 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3051 }
3052 }
3053
3054 if (it->cmp_it.id < 0)
3055 {
3056 EMACS_INT stoppos = it->end_charpos;
3057
3058 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3059 stoppos = -1;
3060 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3061 stoppos, it->string);
3062 }
3063
3064 xassert (STRINGP (it->string)
3065 || (it->stop_charpos >= BEGV
3066 && it->stop_charpos >= IT_CHARPOS (*it)));
3067 }
3068
3069
3070 /* Return the position of the next overlay change after POS in
3071 current_buffer. Value is point-max if no overlay change
3072 follows. This is like `next-overlay-change' but doesn't use
3073 xmalloc. */
3074
3075 static EMACS_INT
3076 next_overlay_change (EMACS_INT pos)
3077 {
3078 int noverlays;
3079 EMACS_INT endpos;
3080 Lisp_Object *overlays;
3081 int i;
3082
3083 /* Get all overlays at the given position. */
3084 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3085
3086 /* If any of these overlays ends before endpos,
3087 use its ending point instead. */
3088 for (i = 0; i < noverlays; ++i)
3089 {
3090 Lisp_Object oend;
3091 EMACS_INT oendpos;
3092
3093 oend = OVERLAY_END (overlays[i]);
3094 oendpos = OVERLAY_POSITION (oend);
3095 endpos = min (endpos, oendpos);
3096 }
3097
3098 return endpos;
3099 }
3100
3101 /* Return the character position of a display string at or after
3102 position specified by POSITION. If no display string exists at or
3103 after POSITION, return ZV. A display string is either an overlay
3104 with `display' property whose value is a string, or a `display'
3105 text property whose value is a string. STRING is data about the
3106 string to iterate; if STRING->lstring is nil, we are iterating a
3107 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3108 on a GUI frame. */
3109 EMACS_INT
3110 compute_display_string_pos (struct text_pos *position,
3111 struct bidi_string_data *string, int frame_window_p)
3112 {
3113 /* OBJECT = nil means current buffer. */
3114 Lisp_Object object = string ? string->lstring : Qnil;
3115 Lisp_Object pos, spec;
3116 EMACS_INT eob = STRINGP (object) ? string->schars : ZV;
3117 EMACS_INT begb = STRINGP (object) ? 0 : BEGV;
3118 EMACS_INT bufpos, charpos = CHARPOS (*position);
3119 struct text_pos tpos;
3120
3121 if (charpos >= eob
3122 /* We don't support display properties whose values are strings
3123 that have display string properties. */
3124 || string->from_disp_str
3125 /* C strings cannot have display properties. */
3126 || (string->s && !STRINGP (object)))
3127 return eob;
3128
3129 /* If the character at CHARPOS is where the display string begins,
3130 return CHARPOS. */
3131 pos = make_number (charpos);
3132 if (STRINGP (object))
3133 bufpos = string->bufpos;
3134 else
3135 bufpos = charpos;
3136 tpos = *position;
3137 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3138 && (charpos <= begb
3139 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3140 object),
3141 spec))
3142 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3143 frame_window_p))
3144 return charpos;
3145
3146 /* Look forward for the first character with a `display' property
3147 that will replace the underlying text when displayed. */
3148 do {
3149 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3150 CHARPOS (tpos) = XFASTINT (pos);
3151 if (STRINGP (object))
3152 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3153 else
3154 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3155 if (CHARPOS (tpos) >= eob)
3156 break;
3157 spec = Fget_char_property (pos, Qdisplay, object);
3158 if (!STRINGP (object))
3159 bufpos = CHARPOS (tpos);
3160 } while (NILP (spec)
3161 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3162 frame_window_p));
3163
3164 return CHARPOS (tpos);
3165 }
3166
3167 /* Return the character position of the end of the display string that
3168 started at CHARPOS. A display string is either an overlay with
3169 `display' property whose value is a string or a `display' text
3170 property whose value is a string. */
3171 EMACS_INT
3172 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3173 {
3174 /* OBJECT = nil means current buffer. */
3175 Lisp_Object object = string ? string->lstring : Qnil;
3176 Lisp_Object pos = make_number (charpos);
3177 EMACS_INT eob = STRINGP (object) ? string->schars : ZV;
3178
3179 if (charpos >= eob)
3180 return eob;
3181
3182 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3183 abort ();
3184
3185 /* Look forward for the first character where the `display' property
3186 changes. */
3187 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3188
3189 return XFASTINT (pos);
3190 }
3191
3192
3193 \f
3194 /***********************************************************************
3195 Fontification
3196 ***********************************************************************/
3197
3198 /* Handle changes in the `fontified' property of the current buffer by
3199 calling hook functions from Qfontification_functions to fontify
3200 regions of text. */
3201
3202 static enum prop_handled
3203 handle_fontified_prop (struct it *it)
3204 {
3205 Lisp_Object prop, pos;
3206 enum prop_handled handled = HANDLED_NORMALLY;
3207
3208 if (!NILP (Vmemory_full))
3209 return handled;
3210
3211 /* Get the value of the `fontified' property at IT's current buffer
3212 position. (The `fontified' property doesn't have a special
3213 meaning in strings.) If the value is nil, call functions from
3214 Qfontification_functions. */
3215 if (!STRINGP (it->string)
3216 && it->s == NULL
3217 && !NILP (Vfontification_functions)
3218 && !NILP (Vrun_hooks)
3219 && (pos = make_number (IT_CHARPOS (*it)),
3220 prop = Fget_char_property (pos, Qfontified, Qnil),
3221 /* Ignore the special cased nil value always present at EOB since
3222 no amount of fontifying will be able to change it. */
3223 NILP (prop) && IT_CHARPOS (*it) < Z))
3224 {
3225 int count = SPECPDL_INDEX ();
3226 Lisp_Object val;
3227 struct buffer *obuf = current_buffer;
3228 int begv = BEGV, zv = ZV;
3229 int old_clip_changed = current_buffer->clip_changed;
3230
3231 val = Vfontification_functions;
3232 specbind (Qfontification_functions, Qnil);
3233
3234 xassert (it->end_charpos == ZV);
3235
3236 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3237 safe_call1 (val, pos);
3238 else
3239 {
3240 Lisp_Object fns, fn;
3241 struct gcpro gcpro1, gcpro2;
3242
3243 fns = Qnil;
3244 GCPRO2 (val, fns);
3245
3246 for (; CONSP (val); val = XCDR (val))
3247 {
3248 fn = XCAR (val);
3249
3250 if (EQ (fn, Qt))
3251 {
3252 /* A value of t indicates this hook has a local
3253 binding; it means to run the global binding too.
3254 In a global value, t should not occur. If it
3255 does, we must ignore it to avoid an endless
3256 loop. */
3257 for (fns = Fdefault_value (Qfontification_functions);
3258 CONSP (fns);
3259 fns = XCDR (fns))
3260 {
3261 fn = XCAR (fns);
3262 if (!EQ (fn, Qt))
3263 safe_call1 (fn, pos);
3264 }
3265 }
3266 else
3267 safe_call1 (fn, pos);
3268 }
3269
3270 UNGCPRO;
3271 }
3272
3273 unbind_to (count, Qnil);
3274
3275 /* Fontification functions routinely call `save-restriction'.
3276 Normally, this tags clip_changed, which can confuse redisplay
3277 (see discussion in Bug#6671). Since we don't perform any
3278 special handling of fontification changes in the case where
3279 `save-restriction' isn't called, there's no point doing so in
3280 this case either. So, if the buffer's restrictions are
3281 actually left unchanged, reset clip_changed. */
3282 if (obuf == current_buffer)
3283 {
3284 if (begv == BEGV && zv == ZV)
3285 current_buffer->clip_changed = old_clip_changed;
3286 }
3287 /* There isn't much we can reasonably do to protect against
3288 misbehaving fontification, but here's a fig leaf. */
3289 else if (!NILP (BVAR (obuf, name)))
3290 set_buffer_internal_1 (obuf);
3291
3292 /* The fontification code may have added/removed text.
3293 It could do even a lot worse, but let's at least protect against
3294 the most obvious case where only the text past `pos' gets changed',
3295 as is/was done in grep.el where some escapes sequences are turned
3296 into face properties (bug#7876). */
3297 it->end_charpos = ZV;
3298
3299 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3300 something. This avoids an endless loop if they failed to
3301 fontify the text for which reason ever. */
3302 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3303 handled = HANDLED_RECOMPUTE_PROPS;
3304 }
3305
3306 return handled;
3307 }
3308
3309
3310 \f
3311 /***********************************************************************
3312 Faces
3313 ***********************************************************************/
3314
3315 /* Set up iterator IT from face properties at its current position.
3316 Called from handle_stop. */
3317
3318 static enum prop_handled
3319 handle_face_prop (struct it *it)
3320 {
3321 int new_face_id;
3322 EMACS_INT next_stop;
3323
3324 if (!STRINGP (it->string))
3325 {
3326 new_face_id
3327 = face_at_buffer_position (it->w,
3328 IT_CHARPOS (*it),
3329 it->region_beg_charpos,
3330 it->region_end_charpos,
3331 &next_stop,
3332 (IT_CHARPOS (*it)
3333 + TEXT_PROP_DISTANCE_LIMIT),
3334 0, it->base_face_id);
3335
3336 /* Is this a start of a run of characters with box face?
3337 Caveat: this can be called for a freshly initialized
3338 iterator; face_id is -1 in this case. We know that the new
3339 face will not change until limit, i.e. if the new face has a
3340 box, all characters up to limit will have one. But, as
3341 usual, we don't know whether limit is really the end. */
3342 if (new_face_id != it->face_id)
3343 {
3344 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3345
3346 /* If new face has a box but old face has not, this is
3347 the start of a run of characters with box, i.e. it has
3348 a shadow on the left side. The value of face_id of the
3349 iterator will be -1 if this is the initial call that gets
3350 the face. In this case, we have to look in front of IT's
3351 position and see whether there is a face != new_face_id. */
3352 it->start_of_box_run_p
3353 = (new_face->box != FACE_NO_BOX
3354 && (it->face_id >= 0
3355 || IT_CHARPOS (*it) == BEG
3356 || new_face_id != face_before_it_pos (it)));
3357 it->face_box_p = new_face->box != FACE_NO_BOX;
3358 }
3359 }
3360 else
3361 {
3362 int base_face_id;
3363 EMACS_INT bufpos;
3364 int i;
3365 Lisp_Object from_overlay
3366 = (it->current.overlay_string_index >= 0
3367 ? it->string_overlays[it->current.overlay_string_index]
3368 : Qnil);
3369
3370 /* See if we got to this string directly or indirectly from
3371 an overlay property. That includes the before-string or
3372 after-string of an overlay, strings in display properties
3373 provided by an overlay, their text properties, etc.
3374
3375 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3376 if (! NILP (from_overlay))
3377 for (i = it->sp - 1; i >= 0; i--)
3378 {
3379 if (it->stack[i].current.overlay_string_index >= 0)
3380 from_overlay
3381 = it->string_overlays[it->stack[i].current.overlay_string_index];
3382 else if (! NILP (it->stack[i].from_overlay))
3383 from_overlay = it->stack[i].from_overlay;
3384
3385 if (!NILP (from_overlay))
3386 break;
3387 }
3388
3389 if (! NILP (from_overlay))
3390 {
3391 bufpos = IT_CHARPOS (*it);
3392 /* For a string from an overlay, the base face depends
3393 only on text properties and ignores overlays. */
3394 base_face_id
3395 = face_for_overlay_string (it->w,
3396 IT_CHARPOS (*it),
3397 it->region_beg_charpos,
3398 it->region_end_charpos,
3399 &next_stop,
3400 (IT_CHARPOS (*it)
3401 + TEXT_PROP_DISTANCE_LIMIT),
3402 0,
3403 from_overlay);
3404 }
3405 else
3406 {
3407 bufpos = 0;
3408
3409 /* For strings from a `display' property, use the face at
3410 IT's current buffer position as the base face to merge
3411 with, so that overlay strings appear in the same face as
3412 surrounding text, unless they specify their own
3413 faces. */
3414 base_face_id = underlying_face_id (it);
3415 }
3416
3417 new_face_id = face_at_string_position (it->w,
3418 it->string,
3419 IT_STRING_CHARPOS (*it),
3420 bufpos,
3421 it->region_beg_charpos,
3422 it->region_end_charpos,
3423 &next_stop,
3424 base_face_id, 0);
3425
3426 /* Is this a start of a run of characters with box? Caveat:
3427 this can be called for a freshly allocated iterator; face_id
3428 is -1 is this case. We know that the new face will not
3429 change until the next check pos, i.e. if the new face has a
3430 box, all characters up to that position will have a
3431 box. But, as usual, we don't know whether that position
3432 is really the end. */
3433 if (new_face_id != it->face_id)
3434 {
3435 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3436 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3437
3438 /* If new face has a box but old face hasn't, this is the
3439 start of a run of characters with box, i.e. it has a
3440 shadow on the left side. */
3441 it->start_of_box_run_p
3442 = new_face->box && (old_face == NULL || !old_face->box);
3443 it->face_box_p = new_face->box != FACE_NO_BOX;
3444 }
3445 }
3446
3447 it->face_id = new_face_id;
3448 return HANDLED_NORMALLY;
3449 }
3450
3451
3452 /* Return the ID of the face ``underlying'' IT's current position,
3453 which is in a string. If the iterator is associated with a
3454 buffer, return the face at IT's current buffer position.
3455 Otherwise, use the iterator's base_face_id. */
3456
3457 static int
3458 underlying_face_id (struct it *it)
3459 {
3460 int face_id = it->base_face_id, i;
3461
3462 xassert (STRINGP (it->string));
3463
3464 for (i = it->sp - 1; i >= 0; --i)
3465 if (NILP (it->stack[i].string))
3466 face_id = it->stack[i].face_id;
3467
3468 return face_id;
3469 }
3470
3471
3472 /* Compute the face one character before or after the current position
3473 of IT. BEFORE_P non-zero means get the face in front of IT's
3474 position. Value is the id of the face. */
3475
3476 static int
3477 face_before_or_after_it_pos (struct it *it, int before_p)
3478 {
3479 int face_id, limit;
3480 EMACS_INT next_check_charpos;
3481 struct text_pos pos;
3482
3483 xassert (it->s == NULL);
3484
3485 if (STRINGP (it->string))
3486 {
3487 EMACS_INT bufpos;
3488 int base_face_id;
3489
3490 /* No face change past the end of the string (for the case
3491 we are padding with spaces). No face change before the
3492 string start. */
3493 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3494 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3495 return it->face_id;
3496
3497 /* Set pos to the position before or after IT's current position. */
3498 if (before_p)
3499 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3500 else
3501 /* For composition, we must check the character after the
3502 composition. */
3503 pos = (it->what == IT_COMPOSITION
3504 ? string_pos (IT_STRING_CHARPOS (*it)
3505 + it->cmp_it.nchars, it->string)
3506 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3507
3508 if (it->current.overlay_string_index >= 0)
3509 bufpos = IT_CHARPOS (*it);
3510 else
3511 bufpos = 0;
3512
3513 base_face_id = underlying_face_id (it);
3514
3515 /* Get the face for ASCII, or unibyte. */
3516 face_id = face_at_string_position (it->w,
3517 it->string,
3518 CHARPOS (pos),
3519 bufpos,
3520 it->region_beg_charpos,
3521 it->region_end_charpos,
3522 &next_check_charpos,
3523 base_face_id, 0);
3524
3525 /* Correct the face for charsets different from ASCII. Do it
3526 for the multibyte case only. The face returned above is
3527 suitable for unibyte text if IT->string is unibyte. */
3528 if (STRING_MULTIBYTE (it->string))
3529 {
3530 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3531 int c, len;
3532 struct face *face = FACE_FROM_ID (it->f, face_id);
3533
3534 c = string_char_and_length (p, &len);
3535 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3536 }
3537 }
3538 else
3539 {
3540 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3541 || (IT_CHARPOS (*it) <= BEGV && before_p))
3542 return it->face_id;
3543
3544 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3545 pos = it->current.pos;
3546
3547 if (before_p)
3548 DEC_TEXT_POS (pos, it->multibyte_p);
3549 else
3550 {
3551 if (it->what == IT_COMPOSITION)
3552 /* For composition, we must check the position after the
3553 composition. */
3554 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3555 else
3556 INC_TEXT_POS (pos, it->multibyte_p);
3557 }
3558
3559 /* Determine face for CHARSET_ASCII, or unibyte. */
3560 face_id = face_at_buffer_position (it->w,
3561 CHARPOS (pos),
3562 it->region_beg_charpos,
3563 it->region_end_charpos,
3564 &next_check_charpos,
3565 limit, 0, -1);
3566
3567 /* Correct the face for charsets different from ASCII. Do it
3568 for the multibyte case only. The face returned above is
3569 suitable for unibyte text if current_buffer is unibyte. */
3570 if (it->multibyte_p)
3571 {
3572 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3573 struct face *face = FACE_FROM_ID (it->f, face_id);
3574 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3575 }
3576 }
3577
3578 return face_id;
3579 }
3580
3581
3582 \f
3583 /***********************************************************************
3584 Invisible text
3585 ***********************************************************************/
3586
3587 /* Set up iterator IT from invisible properties at its current
3588 position. Called from handle_stop. */
3589
3590 static enum prop_handled
3591 handle_invisible_prop (struct it *it)
3592 {
3593 enum prop_handled handled = HANDLED_NORMALLY;
3594
3595 if (STRINGP (it->string))
3596 {
3597 Lisp_Object prop, end_charpos, limit, charpos;
3598
3599 /* Get the value of the invisible text property at the
3600 current position. Value will be nil if there is no such
3601 property. */
3602 charpos = make_number (IT_STRING_CHARPOS (*it));
3603 prop = Fget_text_property (charpos, Qinvisible, it->string);
3604
3605 if (!NILP (prop)
3606 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3607 {
3608 handled = HANDLED_RECOMPUTE_PROPS;
3609
3610 /* Get the position at which the next change of the
3611 invisible text property can be found in IT->string.
3612 Value will be nil if the property value is the same for
3613 all the rest of IT->string. */
3614 XSETINT (limit, SCHARS (it->string));
3615 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3616 it->string, limit);
3617
3618 /* Text at current position is invisible. The next
3619 change in the property is at position end_charpos.
3620 Move IT's current position to that position. */
3621 if (INTEGERP (end_charpos)
3622 && XFASTINT (end_charpos) < XFASTINT (limit))
3623 {
3624 struct text_pos old;
3625 old = it->current.string_pos;
3626 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3627 compute_string_pos (&it->current.string_pos, old, it->string);
3628 }
3629 else
3630 {
3631 /* The rest of the string is invisible. If this is an
3632 overlay string, proceed with the next overlay string
3633 or whatever comes and return a character from there. */
3634 if (it->current.overlay_string_index >= 0)
3635 {
3636 next_overlay_string (it);
3637 /* Don't check for overlay strings when we just
3638 finished processing them. */
3639 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3640 }
3641 else
3642 {
3643 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3644 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3645 }
3646 }
3647 }
3648 }
3649 else
3650 {
3651 int invis_p;
3652 EMACS_INT newpos, next_stop, start_charpos, tem;
3653 Lisp_Object pos, prop, overlay;
3654
3655 /* First of all, is there invisible text at this position? */
3656 tem = start_charpos = IT_CHARPOS (*it);
3657 pos = make_number (tem);
3658 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3659 &overlay);
3660 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3661
3662 /* If we are on invisible text, skip over it. */
3663 if (invis_p && start_charpos < it->end_charpos)
3664 {
3665 /* Record whether we have to display an ellipsis for the
3666 invisible text. */
3667 int display_ellipsis_p = invis_p == 2;
3668
3669 handled = HANDLED_RECOMPUTE_PROPS;
3670
3671 /* Loop skipping over invisible text. The loop is left at
3672 ZV or with IT on the first char being visible again. */
3673 do
3674 {
3675 /* Try to skip some invisible text. Return value is the
3676 position reached which can be equal to where we start
3677 if there is nothing invisible there. This skips both
3678 over invisible text properties and overlays with
3679 invisible property. */
3680 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3681
3682 /* If we skipped nothing at all we weren't at invisible
3683 text in the first place. If everything to the end of
3684 the buffer was skipped, end the loop. */
3685 if (newpos == tem || newpos >= ZV)
3686 invis_p = 0;
3687 else
3688 {
3689 /* We skipped some characters but not necessarily
3690 all there are. Check if we ended up on visible
3691 text. Fget_char_property returns the property of
3692 the char before the given position, i.e. if we
3693 get invis_p = 0, this means that the char at
3694 newpos is visible. */
3695 pos = make_number (newpos);
3696 prop = Fget_char_property (pos, Qinvisible, it->window);
3697 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3698 }
3699
3700 /* If we ended up on invisible text, proceed to
3701 skip starting with next_stop. */
3702 if (invis_p)
3703 tem = next_stop;
3704
3705 /* If there are adjacent invisible texts, don't lose the
3706 second one's ellipsis. */
3707 if (invis_p == 2)
3708 display_ellipsis_p = 1;
3709 }
3710 while (invis_p);
3711
3712 /* The position newpos is now either ZV or on visible text. */
3713 if (it->bidi_p && newpos < ZV)
3714 {
3715 /* With bidi iteration, the region of invisible text
3716 could start and/or end in the middle of a non-base
3717 embedding level. Therefore, we need to skip
3718 invisible text using the bidi iterator, starting at
3719 IT's current position, until we find ourselves
3720 outside the invisible text. Skipping invisible text
3721 _after_ bidi iteration avoids affecting the visual
3722 order of the displayed text when invisible properties
3723 are added or removed. */
3724 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3725 {
3726 /* If we were `reseat'ed to a new paragraph,
3727 determine the paragraph base direction. We need
3728 to do it now because next_element_from_buffer may
3729 not have a chance to do it, if we are going to
3730 skip any text at the beginning, which resets the
3731 FIRST_ELT flag. */
3732 bidi_paragraph_init (it->paragraph_embedding,
3733 &it->bidi_it, 1);
3734 }
3735 do
3736 {
3737 bidi_move_to_visually_next (&it->bidi_it);
3738 }
3739 while (it->stop_charpos <= it->bidi_it.charpos
3740 && it->bidi_it.charpos < newpos);
3741 IT_CHARPOS (*it) = it->bidi_it.charpos;
3742 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3743 /* If we overstepped NEWPOS, record its position in the
3744 iterator, so that we skip invisible text if later the
3745 bidi iteration lands us in the invisible region
3746 again. */
3747 if (IT_CHARPOS (*it) >= newpos)
3748 it->prev_stop = newpos;
3749 }
3750 else
3751 {
3752 IT_CHARPOS (*it) = newpos;
3753 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3754 }
3755
3756 /* If there are before-strings at the start of invisible
3757 text, and the text is invisible because of a text
3758 property, arrange to show before-strings because 20.x did
3759 it that way. (If the text is invisible because of an
3760 overlay property instead of a text property, this is
3761 already handled in the overlay code.) */
3762 if (NILP (overlay)
3763 && get_overlay_strings (it, it->stop_charpos))
3764 {
3765 handled = HANDLED_RECOMPUTE_PROPS;
3766 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3767 }
3768 else if (display_ellipsis_p)
3769 {
3770 /* Make sure that the glyphs of the ellipsis will get
3771 correct `charpos' values. If we would not update
3772 it->position here, the glyphs would belong to the
3773 last visible character _before_ the invisible
3774 text, which confuses `set_cursor_from_row'.
3775
3776 We use the last invisible position instead of the
3777 first because this way the cursor is always drawn on
3778 the first "." of the ellipsis, whenever PT is inside
3779 the invisible text. Otherwise the cursor would be
3780 placed _after_ the ellipsis when the point is after the
3781 first invisible character. */
3782 if (!STRINGP (it->object))
3783 {
3784 it->position.charpos = newpos - 1;
3785 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3786 }
3787 it->ellipsis_p = 1;
3788 /* Let the ellipsis display before
3789 considering any properties of the following char.
3790 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3791 handled = HANDLED_RETURN;
3792 }
3793 }
3794 }
3795
3796 return handled;
3797 }
3798
3799
3800 /* Make iterator IT return `...' next.
3801 Replaces LEN characters from buffer. */
3802
3803 static void
3804 setup_for_ellipsis (struct it *it, int len)
3805 {
3806 /* Use the display table definition for `...'. Invalid glyphs
3807 will be handled by the method returning elements from dpvec. */
3808 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3809 {
3810 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3811 it->dpvec = v->contents;
3812 it->dpend = v->contents + v->header.size;
3813 }
3814 else
3815 {
3816 /* Default `...'. */
3817 it->dpvec = default_invis_vector;
3818 it->dpend = default_invis_vector + 3;
3819 }
3820
3821 it->dpvec_char_len = len;
3822 it->current.dpvec_index = 0;
3823 it->dpvec_face_id = -1;
3824
3825 /* Remember the current face id in case glyphs specify faces.
3826 IT's face is restored in set_iterator_to_next.
3827 saved_face_id was set to preceding char's face in handle_stop. */
3828 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3829 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3830
3831 it->method = GET_FROM_DISPLAY_VECTOR;
3832 it->ellipsis_p = 1;
3833 }
3834
3835
3836 \f
3837 /***********************************************************************
3838 'display' property
3839 ***********************************************************************/
3840
3841 /* Set up iterator IT from `display' property at its current position.
3842 Called from handle_stop.
3843 We return HANDLED_RETURN if some part of the display property
3844 overrides the display of the buffer text itself.
3845 Otherwise we return HANDLED_NORMALLY. */
3846
3847 static enum prop_handled
3848 handle_display_prop (struct it *it)
3849 {
3850 Lisp_Object propval, object, overlay;
3851 struct text_pos *position;
3852 EMACS_INT bufpos;
3853 /* Nonzero if some property replaces the display of the text itself. */
3854 int display_replaced_p = 0;
3855
3856 if (STRINGP (it->string))
3857 {
3858 object = it->string;
3859 position = &it->current.string_pos;
3860 bufpos = CHARPOS (it->current.pos);
3861 }
3862 else
3863 {
3864 XSETWINDOW (object, it->w);
3865 position = &it->current.pos;
3866 bufpos = CHARPOS (*position);
3867 }
3868
3869 /* Reset those iterator values set from display property values. */
3870 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3871 it->space_width = Qnil;
3872 it->font_height = Qnil;
3873 it->voffset = 0;
3874
3875 /* We don't support recursive `display' properties, i.e. string
3876 values that have a string `display' property, that have a string
3877 `display' property etc. */
3878 if (!it->string_from_display_prop_p)
3879 it->area = TEXT_AREA;
3880
3881 propval = get_char_property_and_overlay (make_number (position->charpos),
3882 Qdisplay, object, &overlay);
3883 if (NILP (propval))
3884 return HANDLED_NORMALLY;
3885 /* Now OVERLAY is the overlay that gave us this property, or nil
3886 if it was a text property. */
3887
3888 if (!STRINGP (it->string))
3889 object = it->w->buffer;
3890
3891 display_replaced_p = handle_display_spec (it, propval, object, overlay,
3892 position, bufpos,
3893 FRAME_WINDOW_P (it->f));
3894
3895 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3896 }
3897
3898 /* Subroutine of handle_display_prop. Returns non-zero if the display
3899 specification in SPEC is a replacing specification, i.e. it would
3900 replace the text covered by `display' property with something else,
3901 such as an image or a display string.
3902
3903 See handle_single_display_spec for documentation of arguments.
3904 frame_window_p is non-zero if the window being redisplayed is on a
3905 GUI frame; this argument is used only if IT is NULL, see below.
3906
3907 IT can be NULL, if this is called by the bidi reordering code
3908 through compute_display_string_pos, which see. In that case, this
3909 function only examines SPEC, but does not otherwise "handle" it, in
3910 the sense that it doesn't set up members of IT from the display
3911 spec. */
3912 static int
3913 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
3914 Lisp_Object overlay, struct text_pos *position,
3915 EMACS_INT bufpos, int frame_window_p)
3916 {
3917 int replacing_p = 0;
3918
3919 if (CONSP (spec)
3920 /* Simple specerties. */
3921 && !EQ (XCAR (spec), Qimage)
3922 && !EQ (XCAR (spec), Qspace)
3923 && !EQ (XCAR (spec), Qwhen)
3924 && !EQ (XCAR (spec), Qslice)
3925 && !EQ (XCAR (spec), Qspace_width)
3926 && !EQ (XCAR (spec), Qheight)
3927 && !EQ (XCAR (spec), Qraise)
3928 /* Marginal area specifications. */
3929 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
3930 && !EQ (XCAR (spec), Qleft_fringe)
3931 && !EQ (XCAR (spec), Qright_fringe)
3932 && !NILP (XCAR (spec)))
3933 {
3934 for (; CONSP (spec); spec = XCDR (spec))
3935 {
3936 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
3937 position, bufpos, replacing_p,
3938 frame_window_p))
3939 {
3940 replacing_p = 1;
3941 /* If some text in a string is replaced, `position' no
3942 longer points to the position of `object'. */
3943 if (!it || STRINGP (object))
3944 break;
3945 }
3946 }
3947 }
3948 else if (VECTORP (spec))
3949 {
3950 int i;
3951 for (i = 0; i < ASIZE (spec); ++i)
3952 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
3953 position, bufpos, replacing_p,
3954 frame_window_p))
3955 {
3956 replacing_p = 1;
3957 /* If some text in a string is replaced, `position' no
3958 longer points to the position of `object'. */
3959 if (!it || STRINGP (object))
3960 break;
3961 }
3962 }
3963 else
3964 {
3965 if (handle_single_display_spec (it, spec, object, overlay,
3966 position, bufpos, 0, frame_window_p))
3967 replacing_p = 1;
3968 }
3969
3970 return replacing_p;
3971 }
3972
3973 /* Value is the position of the end of the `display' property starting
3974 at START_POS in OBJECT. */
3975
3976 static struct text_pos
3977 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
3978 {
3979 Lisp_Object end;
3980 struct text_pos end_pos;
3981
3982 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3983 Qdisplay, object, Qnil);
3984 CHARPOS (end_pos) = XFASTINT (end);
3985 if (STRINGP (object))
3986 compute_string_pos (&end_pos, start_pos, it->string);
3987 else
3988 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3989
3990 return end_pos;
3991 }
3992
3993
3994 /* Set up IT from a single `display' property specification SPEC. OBJECT
3995 is the object in which the `display' property was found. *POSITION
3996 is the position in OBJECT at which the `display' property was found.
3997 BUFPOS is the buffer position of OBJECT (different from POSITION if
3998 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
3999 previously saw a display specification which already replaced text
4000 display with something else, for example an image; we ignore such
4001 properties after the first one has been processed.
4002
4003 OVERLAY is the overlay this `display' property came from,
4004 or nil if it was a text property.
4005
4006 If SPEC is a `space' or `image' specification, and in some other
4007 cases too, set *POSITION to the position where the `display'
4008 property ends.
4009
4010 If IT is NULL, only examine the property specification in SPEC, but
4011 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4012 is intended to be displayed in a window on a GUI frame.
4013
4014 Value is non-zero if something was found which replaces the display
4015 of buffer or string text. */
4016
4017 static int
4018 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4019 Lisp_Object overlay, struct text_pos *position,
4020 EMACS_INT bufpos, int display_replaced_p,
4021 int frame_window_p)
4022 {
4023 Lisp_Object form;
4024 Lisp_Object location, value;
4025 struct text_pos start_pos = *position;
4026 int valid_p;
4027
4028 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4029 If the result is non-nil, use VALUE instead of SPEC. */
4030 form = Qt;
4031 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4032 {
4033 spec = XCDR (spec);
4034 if (!CONSP (spec))
4035 return 0;
4036 form = XCAR (spec);
4037 spec = XCDR (spec);
4038 }
4039
4040 if (!NILP (form) && !EQ (form, Qt))
4041 {
4042 int count = SPECPDL_INDEX ();
4043 struct gcpro gcpro1;
4044
4045 /* Bind `object' to the object having the `display' property, a
4046 buffer or string. Bind `position' to the position in the
4047 object where the property was found, and `buffer-position'
4048 to the current position in the buffer. */
4049
4050 if (NILP (object))
4051 XSETBUFFER (object, current_buffer);
4052 specbind (Qobject, object);
4053 specbind (Qposition, make_number (CHARPOS (*position)));
4054 specbind (Qbuffer_position, make_number (bufpos));
4055 GCPRO1 (form);
4056 form = safe_eval (form);
4057 UNGCPRO;
4058 unbind_to (count, Qnil);
4059 }
4060
4061 if (NILP (form))
4062 return 0;
4063
4064 /* Handle `(height HEIGHT)' specifications. */
4065 if (CONSP (spec)
4066 && EQ (XCAR (spec), Qheight)
4067 && CONSP (XCDR (spec)))
4068 {
4069 if (it)
4070 {
4071 if (!FRAME_WINDOW_P (it->f))
4072 return 0;
4073
4074 it->font_height = XCAR (XCDR (spec));
4075 if (!NILP (it->font_height))
4076 {
4077 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4078 int new_height = -1;
4079
4080 if (CONSP (it->font_height)
4081 && (EQ (XCAR (it->font_height), Qplus)
4082 || EQ (XCAR (it->font_height), Qminus))
4083 && CONSP (XCDR (it->font_height))
4084 && INTEGERP (XCAR (XCDR (it->font_height))))
4085 {
4086 /* `(+ N)' or `(- N)' where N is an integer. */
4087 int steps = XINT (XCAR (XCDR (it->font_height)));
4088 if (EQ (XCAR (it->font_height), Qplus))
4089 steps = - steps;
4090 it->face_id = smaller_face (it->f, it->face_id, steps);
4091 }
4092 else if (FUNCTIONP (it->font_height))
4093 {
4094 /* Call function with current height as argument.
4095 Value is the new height. */
4096 Lisp_Object height;
4097 height = safe_call1 (it->font_height,
4098 face->lface[LFACE_HEIGHT_INDEX]);
4099 if (NUMBERP (height))
4100 new_height = XFLOATINT (height);
4101 }
4102 else if (NUMBERP (it->font_height))
4103 {
4104 /* Value is a multiple of the canonical char height. */
4105 struct face *f;
4106
4107 f = FACE_FROM_ID (it->f,
4108 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4109 new_height = (XFLOATINT (it->font_height)
4110 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4111 }
4112 else
4113 {
4114 /* Evaluate IT->font_height with `height' bound to the
4115 current specified height to get the new height. */
4116 int count = SPECPDL_INDEX ();
4117
4118 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4119 value = safe_eval (it->font_height);
4120 unbind_to (count, Qnil);
4121
4122 if (NUMBERP (value))
4123 new_height = XFLOATINT (value);
4124 }
4125
4126 if (new_height > 0)
4127 it->face_id = face_with_height (it->f, it->face_id, new_height);
4128 }
4129 }
4130
4131 return 0;
4132 }
4133
4134 /* Handle `(space-width WIDTH)'. */
4135 if (CONSP (spec)
4136 && EQ (XCAR (spec), Qspace_width)
4137 && CONSP (XCDR (spec)))
4138 {
4139 if (it)
4140 {
4141 if (!FRAME_WINDOW_P (it->f))
4142 return 0;
4143
4144 value = XCAR (XCDR (spec));
4145 if (NUMBERP (value) && XFLOATINT (value) > 0)
4146 it->space_width = value;
4147 }
4148
4149 return 0;
4150 }
4151
4152 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4153 if (CONSP (spec)
4154 && EQ (XCAR (spec), Qslice))
4155 {
4156 Lisp_Object tem;
4157
4158 if (it)
4159 {
4160 if (!FRAME_WINDOW_P (it->f))
4161 return 0;
4162
4163 if (tem = XCDR (spec), CONSP (tem))
4164 {
4165 it->slice.x = XCAR (tem);
4166 if (tem = XCDR (tem), CONSP (tem))
4167 {
4168 it->slice.y = XCAR (tem);
4169 if (tem = XCDR (tem), CONSP (tem))
4170 {
4171 it->slice.width = XCAR (tem);
4172 if (tem = XCDR (tem), CONSP (tem))
4173 it->slice.height = XCAR (tem);
4174 }
4175 }
4176 }
4177 }
4178
4179 return 0;
4180 }
4181
4182 /* Handle `(raise FACTOR)'. */
4183 if (CONSP (spec)
4184 && EQ (XCAR (spec), Qraise)
4185 && CONSP (XCDR (spec)))
4186 {
4187 if (it)
4188 {
4189 if (!FRAME_WINDOW_P (it->f))
4190 return 0;
4191
4192 #ifdef HAVE_WINDOW_SYSTEM
4193 value = XCAR (XCDR (spec));
4194 if (NUMBERP (value))
4195 {
4196 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4197 it->voffset = - (XFLOATINT (value)
4198 * (FONT_HEIGHT (face->font)));
4199 }
4200 #endif /* HAVE_WINDOW_SYSTEM */
4201 }
4202
4203 return 0;
4204 }
4205
4206 /* Don't handle the other kinds of display specifications
4207 inside a string that we got from a `display' property. */
4208 if (it && it->string_from_display_prop_p)
4209 return 0;
4210
4211 /* Characters having this form of property are not displayed, so
4212 we have to find the end of the property. */
4213 if (it)
4214 {
4215 start_pos = *position;
4216 *position = display_prop_end (it, object, start_pos);
4217 }
4218 value = Qnil;
4219
4220 /* Stop the scan at that end position--we assume that all
4221 text properties change there. */
4222 if (it)
4223 it->stop_charpos = position->charpos;
4224
4225 /* Handle `(left-fringe BITMAP [FACE])'
4226 and `(right-fringe BITMAP [FACE])'. */
4227 if (CONSP (spec)
4228 && (EQ (XCAR (spec), Qleft_fringe)
4229 || EQ (XCAR (spec), Qright_fringe))
4230 && CONSP (XCDR (spec)))
4231 {
4232 int fringe_bitmap;
4233
4234 if (it)
4235 {
4236 if (!FRAME_WINDOW_P (it->f))
4237 /* If we return here, POSITION has been advanced
4238 across the text with this property. */
4239 return 0;
4240 }
4241 else if (!frame_window_p)
4242 return 0;
4243
4244 #ifdef HAVE_WINDOW_SYSTEM
4245 value = XCAR (XCDR (spec));
4246 if (!SYMBOLP (value)
4247 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4248 /* If we return here, POSITION has been advanced
4249 across the text with this property. */
4250 return 0;
4251
4252 if (it)
4253 {
4254 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4255
4256 if (CONSP (XCDR (XCDR (spec))))
4257 {
4258 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4259 int face_id2 = lookup_derived_face (it->f, face_name,
4260 FRINGE_FACE_ID, 0);
4261 if (face_id2 >= 0)
4262 face_id = face_id2;
4263 }
4264
4265 /* Save current settings of IT so that we can restore them
4266 when we are finished with the glyph property value. */
4267 push_it (it, position);
4268
4269 it->area = TEXT_AREA;
4270 it->what = IT_IMAGE;
4271 it->image_id = -1; /* no image */
4272 it->position = start_pos;
4273 it->object = NILP (object) ? it->w->buffer : object;
4274 it->method = GET_FROM_IMAGE;
4275 it->from_overlay = Qnil;
4276 it->face_id = face_id;
4277
4278 /* Say that we haven't consumed the characters with
4279 `display' property yet. The call to pop_it in
4280 set_iterator_to_next will clean this up. */
4281 *position = start_pos;
4282
4283 if (EQ (XCAR (spec), Qleft_fringe))
4284 {
4285 it->left_user_fringe_bitmap = fringe_bitmap;
4286 it->left_user_fringe_face_id = face_id;
4287 }
4288 else
4289 {
4290 it->right_user_fringe_bitmap = fringe_bitmap;
4291 it->right_user_fringe_face_id = face_id;
4292 }
4293 }
4294 #endif /* HAVE_WINDOW_SYSTEM */
4295 return 1;
4296 }
4297
4298 /* Prepare to handle `((margin left-margin) ...)',
4299 `((margin right-margin) ...)' and `((margin nil) ...)'
4300 prefixes for display specifications. */
4301 location = Qunbound;
4302 if (CONSP (spec) && CONSP (XCAR (spec)))
4303 {
4304 Lisp_Object tem;
4305
4306 value = XCDR (spec);
4307 if (CONSP (value))
4308 value = XCAR (value);
4309
4310 tem = XCAR (spec);
4311 if (EQ (XCAR (tem), Qmargin)
4312 && (tem = XCDR (tem),
4313 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4314 (NILP (tem)
4315 || EQ (tem, Qleft_margin)
4316 || EQ (tem, Qright_margin))))
4317 location = tem;
4318 }
4319
4320 if (EQ (location, Qunbound))
4321 {
4322 location = Qnil;
4323 value = spec;
4324 }
4325
4326 /* After this point, VALUE is the property after any
4327 margin prefix has been stripped. It must be a string,
4328 an image specification, or `(space ...)'.
4329
4330 LOCATION specifies where to display: `left-margin',
4331 `right-margin' or nil. */
4332
4333 valid_p = (STRINGP (value)
4334 #ifdef HAVE_WINDOW_SYSTEM
4335 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4336 && valid_image_p (value))
4337 #endif /* not HAVE_WINDOW_SYSTEM */
4338 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4339
4340 if (valid_p && !display_replaced_p)
4341 {
4342 if (!it)
4343 return 1;
4344
4345 /* Save current settings of IT so that we can restore them
4346 when we are finished with the glyph property value. */
4347 push_it (it, position);
4348 it->from_overlay = overlay;
4349
4350 if (NILP (location))
4351 it->area = TEXT_AREA;
4352 else if (EQ (location, Qleft_margin))
4353 it->area = LEFT_MARGIN_AREA;
4354 else
4355 it->area = RIGHT_MARGIN_AREA;
4356
4357 if (STRINGP (value))
4358 {
4359 it->string = value;
4360 it->multibyte_p = STRING_MULTIBYTE (it->string);
4361 it->current.overlay_string_index = -1;
4362 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4363 it->end_charpos = it->string_nchars = SCHARS (it->string);
4364 it->method = GET_FROM_STRING;
4365 it->stop_charpos = 0;
4366 it->string_from_display_prop_p = 1;
4367 /* Say that we haven't consumed the characters with
4368 `display' property yet. The call to pop_it in
4369 set_iterator_to_next will clean this up. */
4370 if (BUFFERP (object))
4371 *position = start_pos;
4372 }
4373 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4374 {
4375 it->method = GET_FROM_STRETCH;
4376 it->object = value;
4377 *position = it->position = start_pos;
4378 }
4379 #ifdef HAVE_WINDOW_SYSTEM
4380 else
4381 {
4382 it->what = IT_IMAGE;
4383 it->image_id = lookup_image (it->f, value);
4384 it->position = start_pos;
4385 it->object = NILP (object) ? it->w->buffer : object;
4386 it->method = GET_FROM_IMAGE;
4387
4388 /* Say that we haven't consumed the characters with
4389 `display' property yet. The call to pop_it in
4390 set_iterator_to_next will clean this up. */
4391 *position = start_pos;
4392 }
4393 #endif /* HAVE_WINDOW_SYSTEM */
4394
4395 return 1;
4396 }
4397
4398 /* Invalid property or property not supported. Restore
4399 POSITION to what it was before. */
4400 *position = start_pos;
4401 return 0;
4402 }
4403
4404 /* Check if PROP is a display property value whose text should be
4405 treated as intangible. OVERLAY is the overlay from which PROP
4406 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4407 specify the buffer position covered by PROP. */
4408
4409 int
4410 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4411 EMACS_INT charpos, EMACS_INT bytepos)
4412 {
4413 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4414 struct text_pos position;
4415
4416 SET_TEXT_POS (position, charpos, bytepos);
4417 return handle_display_spec (NULL, prop, Qnil, overlay,
4418 &position, charpos, frame_window_p);
4419 }
4420
4421
4422 /* Return 1 if PROP is a display sub-property value containing STRING.
4423
4424 Implementation note: this and the following function are really
4425 special cases of handle_display_spec and
4426 handle_single_display_spec, and should ideally use the same code.
4427 Until they do, these two pairs must be consistent and must be
4428 modified in sync. */
4429
4430 static int
4431 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4432 {
4433 if (EQ (string, prop))
4434 return 1;
4435
4436 /* Skip over `when FORM'. */
4437 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4438 {
4439 prop = XCDR (prop);
4440 if (!CONSP (prop))
4441 return 0;
4442 /* Actually, the condition following `when' should be eval'ed,
4443 like handle_single_display_spec does, and we should return
4444 zero if it evaluates to nil. However, this function is
4445 called only when the buffer was already displayed and some
4446 glyph in the glyph matrix was found to come from a display
4447 string. Therefore, the condition was already evaluated, and
4448 the result was non-nil, otherwise the display string wouldn't
4449 have been displayed and we would have never been called for
4450 this property. Thus, we can skip the evaluation and assume
4451 its result is non-nil. */
4452 prop = XCDR (prop);
4453 }
4454
4455 if (CONSP (prop))
4456 /* Skip over `margin LOCATION'. */
4457 if (EQ (XCAR (prop), Qmargin))
4458 {
4459 prop = XCDR (prop);
4460 if (!CONSP (prop))
4461 return 0;
4462
4463 prop = XCDR (prop);
4464 if (!CONSP (prop))
4465 return 0;
4466 }
4467
4468 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4469 }
4470
4471
4472 /* Return 1 if STRING appears in the `display' property PROP. */
4473
4474 static int
4475 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4476 {
4477 if (CONSP (prop)
4478 && !EQ (XCAR (prop), Qwhen)
4479 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4480 {
4481 /* A list of sub-properties. */
4482 while (CONSP (prop))
4483 {
4484 if (single_display_spec_string_p (XCAR (prop), string))
4485 return 1;
4486 prop = XCDR (prop);
4487 }
4488 }
4489 else if (VECTORP (prop))
4490 {
4491 /* A vector of sub-properties. */
4492 int i;
4493 for (i = 0; i < ASIZE (prop); ++i)
4494 if (single_display_spec_string_p (AREF (prop, i), string))
4495 return 1;
4496 }
4497 else
4498 return single_display_spec_string_p (prop, string);
4499
4500 return 0;
4501 }
4502
4503 /* Look for STRING in overlays and text properties in the current
4504 buffer, between character positions FROM and TO (excluding TO).
4505 BACK_P non-zero means look back (in this case, TO is supposed to be
4506 less than FROM).
4507 Value is the first character position where STRING was found, or
4508 zero if it wasn't found before hitting TO.
4509
4510 This function may only use code that doesn't eval because it is
4511 called asynchronously from note_mouse_highlight. */
4512
4513 static EMACS_INT
4514 string_buffer_position_lim (Lisp_Object string,
4515 EMACS_INT from, EMACS_INT to, int back_p)
4516 {
4517 Lisp_Object limit, prop, pos;
4518 int found = 0;
4519
4520 pos = make_number (from);
4521
4522 if (!back_p) /* looking forward */
4523 {
4524 limit = make_number (min (to, ZV));
4525 while (!found && !EQ (pos, limit))
4526 {
4527 prop = Fget_char_property (pos, Qdisplay, Qnil);
4528 if (!NILP (prop) && display_prop_string_p (prop, string))
4529 found = 1;
4530 else
4531 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4532 limit);
4533 }
4534 }
4535 else /* looking back */
4536 {
4537 limit = make_number (max (to, BEGV));
4538 while (!found && !EQ (pos, limit))
4539 {
4540 prop = Fget_char_property (pos, Qdisplay, Qnil);
4541 if (!NILP (prop) && display_prop_string_p (prop, string))
4542 found = 1;
4543 else
4544 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4545 limit);
4546 }
4547 }
4548
4549 return found ? XINT (pos) : 0;
4550 }
4551
4552 /* Determine which buffer position in current buffer STRING comes from.
4553 AROUND_CHARPOS is an approximate position where it could come from.
4554 Value is the buffer position or 0 if it couldn't be determined.
4555
4556 This function is necessary because we don't record buffer positions
4557 in glyphs generated from strings (to keep struct glyph small).
4558 This function may only use code that doesn't eval because it is
4559 called asynchronously from note_mouse_highlight. */
4560
4561 static EMACS_INT
4562 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4563 {
4564 const int MAX_DISTANCE = 1000;
4565 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4566 around_charpos + MAX_DISTANCE,
4567 0);
4568
4569 if (!found)
4570 found = string_buffer_position_lim (string, around_charpos,
4571 around_charpos - MAX_DISTANCE, 1);
4572 return found;
4573 }
4574
4575
4576 \f
4577 /***********************************************************************
4578 `composition' property
4579 ***********************************************************************/
4580
4581 /* Set up iterator IT from `composition' property at its current
4582 position. Called from handle_stop. */
4583
4584 static enum prop_handled
4585 handle_composition_prop (struct it *it)
4586 {
4587 Lisp_Object prop, string;
4588 EMACS_INT pos, pos_byte, start, end;
4589
4590 if (STRINGP (it->string))
4591 {
4592 unsigned char *s;
4593
4594 pos = IT_STRING_CHARPOS (*it);
4595 pos_byte = IT_STRING_BYTEPOS (*it);
4596 string = it->string;
4597 s = SDATA (string) + pos_byte;
4598 it->c = STRING_CHAR (s);
4599 }
4600 else
4601 {
4602 pos = IT_CHARPOS (*it);
4603 pos_byte = IT_BYTEPOS (*it);
4604 string = Qnil;
4605 it->c = FETCH_CHAR (pos_byte);
4606 }
4607
4608 /* If there's a valid composition and point is not inside of the
4609 composition (in the case that the composition is from the current
4610 buffer), draw a glyph composed from the composition components. */
4611 if (find_composition (pos, -1, &start, &end, &prop, string)
4612 && COMPOSITION_VALID_P (start, end, prop)
4613 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4614 {
4615 if (start != pos)
4616 {
4617 if (STRINGP (it->string))
4618 pos_byte = string_char_to_byte (it->string, start);
4619 else
4620 pos_byte = CHAR_TO_BYTE (start);
4621 }
4622 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4623 prop, string);
4624
4625 if (it->cmp_it.id >= 0)
4626 {
4627 it->cmp_it.ch = -1;
4628 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4629 it->cmp_it.nglyphs = -1;
4630 }
4631 }
4632
4633 return HANDLED_NORMALLY;
4634 }
4635
4636
4637 \f
4638 /***********************************************************************
4639 Overlay strings
4640 ***********************************************************************/
4641
4642 /* The following structure is used to record overlay strings for
4643 later sorting in load_overlay_strings. */
4644
4645 struct overlay_entry
4646 {
4647 Lisp_Object overlay;
4648 Lisp_Object string;
4649 int priority;
4650 int after_string_p;
4651 };
4652
4653
4654 /* Set up iterator IT from overlay strings at its current position.
4655 Called from handle_stop. */
4656
4657 static enum prop_handled
4658 handle_overlay_change (struct it *it)
4659 {
4660 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4661 return HANDLED_RECOMPUTE_PROPS;
4662 else
4663 return HANDLED_NORMALLY;
4664 }
4665
4666
4667 /* Set up the next overlay string for delivery by IT, if there is an
4668 overlay string to deliver. Called by set_iterator_to_next when the
4669 end of the current overlay string is reached. If there are more
4670 overlay strings to display, IT->string and
4671 IT->current.overlay_string_index are set appropriately here.
4672 Otherwise IT->string is set to nil. */
4673
4674 static void
4675 next_overlay_string (struct it *it)
4676 {
4677 ++it->current.overlay_string_index;
4678 if (it->current.overlay_string_index == it->n_overlay_strings)
4679 {
4680 /* No more overlay strings. Restore IT's settings to what
4681 they were before overlay strings were processed, and
4682 continue to deliver from current_buffer. */
4683
4684 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4685 pop_it (it);
4686 xassert (it->sp > 0
4687 || (NILP (it->string)
4688 && it->method == GET_FROM_BUFFER
4689 && it->stop_charpos >= BEGV
4690 && it->stop_charpos <= it->end_charpos));
4691 it->current.overlay_string_index = -1;
4692 it->n_overlay_strings = 0;
4693 it->overlay_strings_charpos = -1;
4694
4695 /* If we're at the end of the buffer, record that we have
4696 processed the overlay strings there already, so that
4697 next_element_from_buffer doesn't try it again. */
4698 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4699 it->overlay_strings_at_end_processed_p = 1;
4700 }
4701 else
4702 {
4703 /* There are more overlay strings to process. If
4704 IT->current.overlay_string_index has advanced to a position
4705 where we must load IT->overlay_strings with more strings, do
4706 it. We must load at the IT->overlay_strings_charpos where
4707 IT->n_overlay_strings was originally computed; when invisible
4708 text is present, this might not be IT_CHARPOS (Bug#7016). */
4709 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4710
4711 if (it->current.overlay_string_index && i == 0)
4712 load_overlay_strings (it, it->overlay_strings_charpos);
4713
4714 /* Initialize IT to deliver display elements from the overlay
4715 string. */
4716 it->string = it->overlay_strings[i];
4717 it->multibyte_p = STRING_MULTIBYTE (it->string);
4718 SET_TEXT_POS (it->current.string_pos, 0, 0);
4719 it->method = GET_FROM_STRING;
4720 it->stop_charpos = 0;
4721 if (it->cmp_it.stop_pos >= 0)
4722 it->cmp_it.stop_pos = 0;
4723 }
4724
4725 CHECK_IT (it);
4726 }
4727
4728
4729 /* Compare two overlay_entry structures E1 and E2. Used as a
4730 comparison function for qsort in load_overlay_strings. Overlay
4731 strings for the same position are sorted so that
4732
4733 1. All after-strings come in front of before-strings, except
4734 when they come from the same overlay.
4735
4736 2. Within after-strings, strings are sorted so that overlay strings
4737 from overlays with higher priorities come first.
4738
4739 2. Within before-strings, strings are sorted so that overlay
4740 strings from overlays with higher priorities come last.
4741
4742 Value is analogous to strcmp. */
4743
4744
4745 static int
4746 compare_overlay_entries (const void *e1, const void *e2)
4747 {
4748 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4749 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4750 int result;
4751
4752 if (entry1->after_string_p != entry2->after_string_p)
4753 {
4754 /* Let after-strings appear in front of before-strings if
4755 they come from different overlays. */
4756 if (EQ (entry1->overlay, entry2->overlay))
4757 result = entry1->after_string_p ? 1 : -1;
4758 else
4759 result = entry1->after_string_p ? -1 : 1;
4760 }
4761 else if (entry1->after_string_p)
4762 /* After-strings sorted in order of decreasing priority. */
4763 result = entry2->priority - entry1->priority;
4764 else
4765 /* Before-strings sorted in order of increasing priority. */
4766 result = entry1->priority - entry2->priority;
4767
4768 return result;
4769 }
4770
4771
4772 /* Load the vector IT->overlay_strings with overlay strings from IT's
4773 current buffer position, or from CHARPOS if that is > 0. Set
4774 IT->n_overlays to the total number of overlay strings found.
4775
4776 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4777 a time. On entry into load_overlay_strings,
4778 IT->current.overlay_string_index gives the number of overlay
4779 strings that have already been loaded by previous calls to this
4780 function.
4781
4782 IT->add_overlay_start contains an additional overlay start
4783 position to consider for taking overlay strings from, if non-zero.
4784 This position comes into play when the overlay has an `invisible'
4785 property, and both before and after-strings. When we've skipped to
4786 the end of the overlay, because of its `invisible' property, we
4787 nevertheless want its before-string to appear.
4788 IT->add_overlay_start will contain the overlay start position
4789 in this case.
4790
4791 Overlay strings are sorted so that after-string strings come in
4792 front of before-string strings. Within before and after-strings,
4793 strings are sorted by overlay priority. See also function
4794 compare_overlay_entries. */
4795
4796 static void
4797 load_overlay_strings (struct it *it, EMACS_INT charpos)
4798 {
4799 Lisp_Object overlay, window, str, invisible;
4800 struct Lisp_Overlay *ov;
4801 EMACS_INT start, end;
4802 int size = 20;
4803 int n = 0, i, j, invis_p;
4804 struct overlay_entry *entries
4805 = (struct overlay_entry *) alloca (size * sizeof *entries);
4806
4807 if (charpos <= 0)
4808 charpos = IT_CHARPOS (*it);
4809
4810 /* Append the overlay string STRING of overlay OVERLAY to vector
4811 `entries' which has size `size' and currently contains `n'
4812 elements. AFTER_P non-zero means STRING is an after-string of
4813 OVERLAY. */
4814 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4815 do \
4816 { \
4817 Lisp_Object priority; \
4818 \
4819 if (n == size) \
4820 { \
4821 int new_size = 2 * size; \
4822 struct overlay_entry *old = entries; \
4823 entries = \
4824 (struct overlay_entry *) alloca (new_size \
4825 * sizeof *entries); \
4826 memcpy (entries, old, size * sizeof *entries); \
4827 size = new_size; \
4828 } \
4829 \
4830 entries[n].string = (STRING); \
4831 entries[n].overlay = (OVERLAY); \
4832 priority = Foverlay_get ((OVERLAY), Qpriority); \
4833 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4834 entries[n].after_string_p = (AFTER_P); \
4835 ++n; \
4836 } \
4837 while (0)
4838
4839 /* Process overlay before the overlay center. */
4840 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4841 {
4842 XSETMISC (overlay, ov);
4843 xassert (OVERLAYP (overlay));
4844 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4845 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4846
4847 if (end < charpos)
4848 break;
4849
4850 /* Skip this overlay if it doesn't start or end at IT's current
4851 position. */
4852 if (end != charpos && start != charpos)
4853 continue;
4854
4855 /* Skip this overlay if it doesn't apply to IT->w. */
4856 window = Foverlay_get (overlay, Qwindow);
4857 if (WINDOWP (window) && XWINDOW (window) != it->w)
4858 continue;
4859
4860 /* If the text ``under'' the overlay is invisible, both before-
4861 and after-strings from this overlay are visible; start and
4862 end position are indistinguishable. */
4863 invisible = Foverlay_get (overlay, Qinvisible);
4864 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4865
4866 /* If overlay has a non-empty before-string, record it. */
4867 if ((start == charpos || (end == charpos && invis_p))
4868 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4869 && SCHARS (str))
4870 RECORD_OVERLAY_STRING (overlay, str, 0);
4871
4872 /* If overlay has a non-empty after-string, record it. */
4873 if ((end == charpos || (start == charpos && invis_p))
4874 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4875 && SCHARS (str))
4876 RECORD_OVERLAY_STRING (overlay, str, 1);
4877 }
4878
4879 /* Process overlays after the overlay center. */
4880 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4881 {
4882 XSETMISC (overlay, ov);
4883 xassert (OVERLAYP (overlay));
4884 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4885 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4886
4887 if (start > charpos)
4888 break;
4889
4890 /* Skip this overlay if it doesn't start or end at IT's current
4891 position. */
4892 if (end != charpos && start != charpos)
4893 continue;
4894
4895 /* Skip this overlay if it doesn't apply to IT->w. */
4896 window = Foverlay_get (overlay, Qwindow);
4897 if (WINDOWP (window) && XWINDOW (window) != it->w)
4898 continue;
4899
4900 /* If the text ``under'' the overlay is invisible, it has a zero
4901 dimension, and both before- and after-strings apply. */
4902 invisible = Foverlay_get (overlay, Qinvisible);
4903 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4904
4905 /* If overlay has a non-empty before-string, record it. */
4906 if ((start == charpos || (end == charpos && invis_p))
4907 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4908 && SCHARS (str))
4909 RECORD_OVERLAY_STRING (overlay, str, 0);
4910
4911 /* If overlay has a non-empty after-string, record it. */
4912 if ((end == charpos || (start == charpos && invis_p))
4913 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4914 && SCHARS (str))
4915 RECORD_OVERLAY_STRING (overlay, str, 1);
4916 }
4917
4918 #undef RECORD_OVERLAY_STRING
4919
4920 /* Sort entries. */
4921 if (n > 1)
4922 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4923
4924 /* Record number of overlay strings, and where we computed it. */
4925 it->n_overlay_strings = n;
4926 it->overlay_strings_charpos = charpos;
4927
4928 /* IT->current.overlay_string_index is the number of overlay strings
4929 that have already been consumed by IT. Copy some of the
4930 remaining overlay strings to IT->overlay_strings. */
4931 i = 0;
4932 j = it->current.overlay_string_index;
4933 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4934 {
4935 it->overlay_strings[i] = entries[j].string;
4936 it->string_overlays[i++] = entries[j++].overlay;
4937 }
4938
4939 CHECK_IT (it);
4940 }
4941
4942
4943 /* Get the first chunk of overlay strings at IT's current buffer
4944 position, or at CHARPOS if that is > 0. Value is non-zero if at
4945 least one overlay string was found. */
4946
4947 static int
4948 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
4949 {
4950 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4951 process. This fills IT->overlay_strings with strings, and sets
4952 IT->n_overlay_strings to the total number of strings to process.
4953 IT->pos.overlay_string_index has to be set temporarily to zero
4954 because load_overlay_strings needs this; it must be set to -1
4955 when no overlay strings are found because a zero value would
4956 indicate a position in the first overlay string. */
4957 it->current.overlay_string_index = 0;
4958 load_overlay_strings (it, charpos);
4959
4960 /* If we found overlay strings, set up IT to deliver display
4961 elements from the first one. Otherwise set up IT to deliver
4962 from current_buffer. */
4963 if (it->n_overlay_strings)
4964 {
4965 /* Make sure we know settings in current_buffer, so that we can
4966 restore meaningful values when we're done with the overlay
4967 strings. */
4968 if (compute_stop_p)
4969 compute_stop_pos (it);
4970 xassert (it->face_id >= 0);
4971
4972 /* Save IT's settings. They are restored after all overlay
4973 strings have been processed. */
4974 xassert (!compute_stop_p || it->sp == 0);
4975
4976 /* When called from handle_stop, there might be an empty display
4977 string loaded. In that case, don't bother saving it. */
4978 if (!STRINGP (it->string) || SCHARS (it->string))
4979 push_it (it, NULL);
4980
4981 /* Set up IT to deliver display elements from the first overlay
4982 string. */
4983 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4984 it->string = it->overlay_strings[0];
4985 it->from_overlay = Qnil;
4986 it->stop_charpos = 0;
4987 xassert (STRINGP (it->string));
4988 it->end_charpos = SCHARS (it->string);
4989 it->multibyte_p = STRING_MULTIBYTE (it->string);
4990 it->method = GET_FROM_STRING;
4991 return 1;
4992 }
4993
4994 it->current.overlay_string_index = -1;
4995 return 0;
4996 }
4997
4998 static int
4999 get_overlay_strings (struct it *it, EMACS_INT charpos)
5000 {
5001 it->string = Qnil;
5002 it->method = GET_FROM_BUFFER;
5003
5004 (void) get_overlay_strings_1 (it, charpos, 1);
5005
5006 CHECK_IT (it);
5007
5008 /* Value is non-zero if we found at least one overlay string. */
5009 return STRINGP (it->string);
5010 }
5011
5012
5013 \f
5014 /***********************************************************************
5015 Saving and restoring state
5016 ***********************************************************************/
5017
5018 /* Save current settings of IT on IT->stack. Called, for example,
5019 before setting up IT for an overlay string, to be able to restore
5020 IT's settings to what they were after the overlay string has been
5021 processed. If POSITION is non-NULL, it is the position to save on
5022 the stack instead of IT->position. */
5023
5024 static void
5025 push_it (struct it *it, struct text_pos *position)
5026 {
5027 struct iterator_stack_entry *p;
5028
5029 xassert (it->sp < IT_STACK_SIZE);
5030 p = it->stack + it->sp;
5031
5032 p->stop_charpos = it->stop_charpos;
5033 p->prev_stop = it->prev_stop;
5034 p->base_level_stop = it->base_level_stop;
5035 p->cmp_it = it->cmp_it;
5036 xassert (it->face_id >= 0);
5037 p->face_id = it->face_id;
5038 p->string = it->string;
5039 p->method = it->method;
5040 p->from_overlay = it->from_overlay;
5041 switch (p->method)
5042 {
5043 case GET_FROM_IMAGE:
5044 p->u.image.object = it->object;
5045 p->u.image.image_id = it->image_id;
5046 p->u.image.slice = it->slice;
5047 break;
5048 case GET_FROM_STRETCH:
5049 p->u.stretch.object = it->object;
5050 break;
5051 }
5052 p->position = position ? *position : it->position;
5053 p->current = it->current;
5054 p->end_charpos = it->end_charpos;
5055 p->string_nchars = it->string_nchars;
5056 p->area = it->area;
5057 p->multibyte_p = it->multibyte_p;
5058 p->avoid_cursor_p = it->avoid_cursor_p;
5059 p->space_width = it->space_width;
5060 p->font_height = it->font_height;
5061 p->voffset = it->voffset;
5062 p->string_from_display_prop_p = it->string_from_display_prop_p;
5063 p->display_ellipsis_p = 0;
5064 p->line_wrap = it->line_wrap;
5065 ++it->sp;
5066 }
5067
5068 static void
5069 iterate_out_of_display_property (struct it *it)
5070 {
5071 /* Maybe initialize paragraph direction. If we are at the beginning
5072 of a new paragraph, next_element_from_buffer may not have a
5073 chance to do that. */
5074 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5075 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5076 /* prev_stop can be zero, so check against BEGV as well. */
5077 while (it->bidi_it.charpos >= BEGV
5078 && it->prev_stop <= it->bidi_it.charpos
5079 && it->bidi_it.charpos < CHARPOS (it->position))
5080 bidi_move_to_visually_next (&it->bidi_it);
5081 /* Record the stop_pos we just crossed, for when we cross it
5082 back, maybe. */
5083 if (it->bidi_it.charpos > CHARPOS (it->position))
5084 it->prev_stop = CHARPOS (it->position);
5085 /* If we ended up not where pop_it put us, resync IT's
5086 positional members with the bidi iterator. */
5087 if (it->bidi_it.charpos != CHARPOS (it->position))
5088 {
5089 SET_TEXT_POS (it->position,
5090 it->bidi_it.charpos, it->bidi_it.bytepos);
5091 it->current.pos = it->position;
5092 }
5093 }
5094
5095 /* Restore IT's settings from IT->stack. Called, for example, when no
5096 more overlay strings must be processed, and we return to delivering
5097 display elements from a buffer, or when the end of a string from a
5098 `display' property is reached and we return to delivering display
5099 elements from an overlay string, or from a buffer. */
5100
5101 static void
5102 pop_it (struct it *it)
5103 {
5104 struct iterator_stack_entry *p;
5105
5106 xassert (it->sp > 0);
5107 --it->sp;
5108 p = it->stack + it->sp;
5109 it->stop_charpos = p->stop_charpos;
5110 it->prev_stop = p->prev_stop;
5111 it->base_level_stop = p->base_level_stop;
5112 it->cmp_it = p->cmp_it;
5113 it->face_id = p->face_id;
5114 it->current = p->current;
5115 it->position = p->position;
5116 it->string = p->string;
5117 it->from_overlay = p->from_overlay;
5118 if (NILP (it->string))
5119 SET_TEXT_POS (it->current.string_pos, -1, -1);
5120 it->method = p->method;
5121 switch (it->method)
5122 {
5123 case GET_FROM_IMAGE:
5124 it->image_id = p->u.image.image_id;
5125 it->object = p->u.image.object;
5126 it->slice = p->u.image.slice;
5127 break;
5128 case GET_FROM_STRETCH:
5129 it->object = p->u.comp.object;
5130 break;
5131 case GET_FROM_BUFFER:
5132 it->object = it->w->buffer;
5133 if (it->bidi_p)
5134 {
5135 /* Bidi-iterate until we get out of the portion of text, if
5136 any, covered by a `display' text property or an overlay
5137 with `display' property. (We cannot just jump there,
5138 because the internal coherency of the bidi iterator state
5139 can not be preserved across such jumps.) We also must
5140 determine the paragraph base direction if the overlay we
5141 just processed is at the beginning of a new
5142 paragraph. */
5143 iterate_out_of_display_property (it);
5144 }
5145 break;
5146 case GET_FROM_STRING:
5147 it->object = it->string;
5148 break;
5149 case GET_FROM_DISPLAY_VECTOR:
5150 if (it->s)
5151 it->method = GET_FROM_C_STRING;
5152 else if (STRINGP (it->string))
5153 it->method = GET_FROM_STRING;
5154 else
5155 {
5156 it->method = GET_FROM_BUFFER;
5157 it->object = it->w->buffer;
5158 }
5159 }
5160 it->end_charpos = p->end_charpos;
5161 it->string_nchars = p->string_nchars;
5162 it->area = p->area;
5163 it->multibyte_p = p->multibyte_p;
5164 it->avoid_cursor_p = p->avoid_cursor_p;
5165 it->space_width = p->space_width;
5166 it->font_height = p->font_height;
5167 it->voffset = p->voffset;
5168 it->string_from_display_prop_p = p->string_from_display_prop_p;
5169 it->line_wrap = p->line_wrap;
5170 }
5171
5172
5173 \f
5174 /***********************************************************************
5175 Moving over lines
5176 ***********************************************************************/
5177
5178 /* Set IT's current position to the previous line start. */
5179
5180 static void
5181 back_to_previous_line_start (struct it *it)
5182 {
5183 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5184 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5185 }
5186
5187
5188 /* Move IT to the next line start.
5189
5190 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5191 we skipped over part of the text (as opposed to moving the iterator
5192 continuously over the text). Otherwise, don't change the value
5193 of *SKIPPED_P.
5194
5195 Newlines may come from buffer text, overlay strings, or strings
5196 displayed via the `display' property. That's the reason we can't
5197 simply use find_next_newline_no_quit.
5198
5199 Note that this function may not skip over invisible text that is so
5200 because of text properties and immediately follows a newline. If
5201 it would, function reseat_at_next_visible_line_start, when called
5202 from set_iterator_to_next, would effectively make invisible
5203 characters following a newline part of the wrong glyph row, which
5204 leads to wrong cursor motion. */
5205
5206 static int
5207 forward_to_next_line_start (struct it *it, int *skipped_p)
5208 {
5209 int old_selective, newline_found_p, n;
5210 const int MAX_NEWLINE_DISTANCE = 500;
5211
5212 /* If already on a newline, just consume it to avoid unintended
5213 skipping over invisible text below. */
5214 if (it->what == IT_CHARACTER
5215 && it->c == '\n'
5216 && CHARPOS (it->position) == IT_CHARPOS (*it))
5217 {
5218 set_iterator_to_next (it, 0);
5219 it->c = 0;
5220 return 1;
5221 }
5222
5223 /* Don't handle selective display in the following. It's (a)
5224 unnecessary because it's done by the caller, and (b) leads to an
5225 infinite recursion because next_element_from_ellipsis indirectly
5226 calls this function. */
5227 old_selective = it->selective;
5228 it->selective = 0;
5229
5230 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5231 from buffer text. */
5232 for (n = newline_found_p = 0;
5233 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5234 n += STRINGP (it->string) ? 0 : 1)
5235 {
5236 if (!get_next_display_element (it))
5237 return 0;
5238 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5239 set_iterator_to_next (it, 0);
5240 }
5241
5242 /* If we didn't find a newline near enough, see if we can use a
5243 short-cut. */
5244 if (!newline_found_p)
5245 {
5246 EMACS_INT start = IT_CHARPOS (*it);
5247 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5248 Lisp_Object pos;
5249
5250 xassert (!STRINGP (it->string));
5251
5252 /* If there isn't any `display' property in sight, and no
5253 overlays, we can just use the position of the newline in
5254 buffer text. */
5255 if (it->stop_charpos >= limit
5256 || ((pos = Fnext_single_property_change (make_number (start),
5257 Qdisplay,
5258 Qnil, make_number (limit)),
5259 NILP (pos))
5260 && next_overlay_change (start) == ZV))
5261 {
5262 IT_CHARPOS (*it) = limit;
5263 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5264 *skipped_p = newline_found_p = 1;
5265 }
5266 else
5267 {
5268 while (get_next_display_element (it)
5269 && !newline_found_p)
5270 {
5271 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5272 set_iterator_to_next (it, 0);
5273 }
5274 }
5275 }
5276
5277 it->selective = old_selective;
5278 return newline_found_p;
5279 }
5280
5281
5282 /* Set IT's current position to the previous visible line start. Skip
5283 invisible text that is so either due to text properties or due to
5284 selective display. Caution: this does not change IT->current_x and
5285 IT->hpos. */
5286
5287 static void
5288 back_to_previous_visible_line_start (struct it *it)
5289 {
5290 while (IT_CHARPOS (*it) > BEGV)
5291 {
5292 back_to_previous_line_start (it);
5293
5294 if (IT_CHARPOS (*it) <= BEGV)
5295 break;
5296
5297 /* If selective > 0, then lines indented more than its value are
5298 invisible. */
5299 if (it->selective > 0
5300 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5301 (double) it->selective)) /* iftc */
5302 continue;
5303
5304 /* Check the newline before point for invisibility. */
5305 {
5306 Lisp_Object prop;
5307 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5308 Qinvisible, it->window);
5309 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5310 continue;
5311 }
5312
5313 if (IT_CHARPOS (*it) <= BEGV)
5314 break;
5315
5316 {
5317 struct it it2;
5318 EMACS_INT pos;
5319 EMACS_INT beg, end;
5320 Lisp_Object val, overlay;
5321
5322 /* If newline is part of a composition, continue from start of composition */
5323 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5324 && beg < IT_CHARPOS (*it))
5325 goto replaced;
5326
5327 /* If newline is replaced by a display property, find start of overlay
5328 or interval and continue search from that point. */
5329 it2 = *it;
5330 pos = --IT_CHARPOS (it2);
5331 --IT_BYTEPOS (it2);
5332 it2.sp = 0;
5333 it2.string_from_display_prop_p = 0;
5334 if (handle_display_prop (&it2) == HANDLED_RETURN
5335 && !NILP (val = get_char_property_and_overlay
5336 (make_number (pos), Qdisplay, Qnil, &overlay))
5337 && (OVERLAYP (overlay)
5338 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5339 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5340 goto replaced;
5341
5342 /* Newline is not replaced by anything -- so we are done. */
5343 break;
5344
5345 replaced:
5346 if (beg < BEGV)
5347 beg = BEGV;
5348 IT_CHARPOS (*it) = beg;
5349 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5350 }
5351 }
5352
5353 it->continuation_lines_width = 0;
5354
5355 xassert (IT_CHARPOS (*it) >= BEGV);
5356 xassert (IT_CHARPOS (*it) == BEGV
5357 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5358 CHECK_IT (it);
5359 }
5360
5361
5362 /* Reseat iterator IT at the previous visible line start. Skip
5363 invisible text that is so either due to text properties or due to
5364 selective display. At the end, update IT's overlay information,
5365 face information etc. */
5366
5367 void
5368 reseat_at_previous_visible_line_start (struct it *it)
5369 {
5370 back_to_previous_visible_line_start (it);
5371 reseat (it, it->current.pos, 1);
5372 CHECK_IT (it);
5373 }
5374
5375
5376 /* Reseat iterator IT on the next visible line start in the current
5377 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5378 preceding the line start. Skip over invisible text that is so
5379 because of selective display. Compute faces, overlays etc at the
5380 new position. Note that this function does not skip over text that
5381 is invisible because of text properties. */
5382
5383 static void
5384 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5385 {
5386 int newline_found_p, skipped_p = 0;
5387
5388 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5389
5390 /* Skip over lines that are invisible because they are indented
5391 more than the value of IT->selective. */
5392 if (it->selective > 0)
5393 while (IT_CHARPOS (*it) < ZV
5394 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5395 (double) it->selective)) /* iftc */
5396 {
5397 xassert (IT_BYTEPOS (*it) == BEGV
5398 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5399 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5400 }
5401
5402 /* Position on the newline if that's what's requested. */
5403 if (on_newline_p && newline_found_p)
5404 {
5405 if (STRINGP (it->string))
5406 {
5407 if (IT_STRING_CHARPOS (*it) > 0)
5408 {
5409 --IT_STRING_CHARPOS (*it);
5410 --IT_STRING_BYTEPOS (*it);
5411 }
5412 }
5413 else if (IT_CHARPOS (*it) > BEGV)
5414 {
5415 --IT_CHARPOS (*it);
5416 --IT_BYTEPOS (*it);
5417 reseat (it, it->current.pos, 0);
5418 }
5419 }
5420 else if (skipped_p)
5421 reseat (it, it->current.pos, 0);
5422
5423 CHECK_IT (it);
5424 }
5425
5426
5427 \f
5428 /***********************************************************************
5429 Changing an iterator's position
5430 ***********************************************************************/
5431
5432 /* Change IT's current position to POS in current_buffer. If FORCE_P
5433 is non-zero, always check for text properties at the new position.
5434 Otherwise, text properties are only looked up if POS >=
5435 IT->check_charpos of a property. */
5436
5437 static void
5438 reseat (struct it *it, struct text_pos pos, int force_p)
5439 {
5440 EMACS_INT original_pos = IT_CHARPOS (*it);
5441
5442 reseat_1 (it, pos, 0);
5443
5444 /* Determine where to check text properties. Avoid doing it
5445 where possible because text property lookup is very expensive. */
5446 if (force_p
5447 || CHARPOS (pos) > it->stop_charpos
5448 || CHARPOS (pos) < original_pos)
5449 {
5450 if (it->bidi_p)
5451 {
5452 /* For bidi iteration, we need to prime prev_stop and
5453 base_level_stop with our best estimations. */
5454 if (CHARPOS (pos) < it->prev_stop)
5455 {
5456 handle_stop_backwards (it, BEGV);
5457 if (CHARPOS (pos) < it->base_level_stop)
5458 it->base_level_stop = 0;
5459 }
5460 else if (CHARPOS (pos) > it->stop_charpos
5461 && it->stop_charpos >= BEGV)
5462 handle_stop_backwards (it, it->stop_charpos);
5463 else /* force_p */
5464 handle_stop (it);
5465 }
5466 else
5467 {
5468 handle_stop (it);
5469 it->prev_stop = it->base_level_stop = 0;
5470 }
5471
5472 }
5473
5474 CHECK_IT (it);
5475 }
5476
5477
5478 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5479 IT->stop_pos to POS, also. */
5480
5481 static void
5482 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5483 {
5484 /* Don't call this function when scanning a C string. */
5485 xassert (it->s == NULL);
5486
5487 /* POS must be a reasonable value. */
5488 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5489
5490 it->current.pos = it->position = pos;
5491 it->end_charpos = ZV;
5492 it->dpvec = NULL;
5493 it->current.dpvec_index = -1;
5494 it->current.overlay_string_index = -1;
5495 IT_STRING_CHARPOS (*it) = -1;
5496 IT_STRING_BYTEPOS (*it) = -1;
5497 it->string = Qnil;
5498 it->string_from_display_prop_p = 0;
5499 it->method = GET_FROM_BUFFER;
5500 it->object = it->w->buffer;
5501 it->area = TEXT_AREA;
5502 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5503 it->sp = 0;
5504 it->string_from_display_prop_p = 0;
5505 it->face_before_selective_p = 0;
5506 if (it->bidi_p)
5507 {
5508 it->bidi_it.first_elt = 1;
5509 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5510 it->bidi_it.disp_pos = -1;
5511 it->bidi_it.string.s = NULL;
5512 it->bidi_it.string.lstring = Qnil;
5513 it->bidi_it.string.bufpos = 0;
5514 }
5515
5516 if (set_stop_p)
5517 {
5518 it->stop_charpos = CHARPOS (pos);
5519 it->base_level_stop = CHARPOS (pos);
5520 }
5521 }
5522
5523
5524 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5525 If S is non-null, it is a C string to iterate over. Otherwise,
5526 STRING gives a Lisp string to iterate over.
5527
5528 If PRECISION > 0, don't return more then PRECISION number of
5529 characters from the string.
5530
5531 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5532 characters have been returned. FIELD_WIDTH < 0 means an infinite
5533 field width.
5534
5535 MULTIBYTE = 0 means disable processing of multibyte characters,
5536 MULTIBYTE > 0 means enable it,
5537 MULTIBYTE < 0 means use IT->multibyte_p.
5538
5539 IT must be initialized via a prior call to init_iterator before
5540 calling this function. */
5541
5542 static void
5543 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5544 EMACS_INT charpos, EMACS_INT precision, int field_width,
5545 int multibyte)
5546 {
5547 /* No region in strings. */
5548 it->region_beg_charpos = it->region_end_charpos = -1;
5549
5550 /* No text property checks performed by default, but see below. */
5551 it->stop_charpos = -1;
5552
5553 /* Set iterator position and end position. */
5554 memset (&it->current, 0, sizeof it->current);
5555 it->current.overlay_string_index = -1;
5556 it->current.dpvec_index = -1;
5557 xassert (charpos >= 0);
5558
5559 /* If STRING is specified, use its multibyteness, otherwise use the
5560 setting of MULTIBYTE, if specified. */
5561 if (multibyte >= 0)
5562 it->multibyte_p = multibyte > 0;
5563 #if 0
5564 /* String reordering is controlled by the default value of
5565 bidi-display-reordering. */
5566 it->bidi_p =
5567 it->multibyte_p && BVAR (&buffer_defaults, bidi_display_reordering);
5568 #endif
5569
5570 if (s == NULL)
5571 {
5572 xassert (STRINGP (string));
5573 it->string = string;
5574 it->s = NULL;
5575 it->end_charpos = it->string_nchars = SCHARS (string);
5576 it->method = GET_FROM_STRING;
5577 it->current.string_pos = string_pos (charpos, string);
5578 #if 0
5579 if (it->bidi_p)
5580 {
5581 it->paragraph_embedding = NEUTRAL_DIR;
5582 it->bidi_it.string.lstring = string;
5583 it->bidi_it.string.s = SDATA (string);
5584 it->bidi_it.string.schars = it->end_charpos;
5585 it->bidi_it.string.bufpos = 0;
5586 it->bidi_it.string.from_disp_str = 0;
5587 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5588 FRAME_WINDOW_P (it->f), &it->bidi_it);
5589 }
5590 #endif
5591 }
5592 else
5593 {
5594 it->s = (const unsigned char *) s;
5595 it->string = Qnil;
5596
5597 /* Note that we use IT->current.pos, not it->current.string_pos,
5598 for displaying C strings. */
5599 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5600 if (it->multibyte_p)
5601 {
5602 it->current.pos = c_string_pos (charpos, s, 1);
5603 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5604 #if 0
5605 if (it->bidi_p)
5606 {
5607 it->paragraph_embedding = NEUTRAL_DIR;
5608 it->bidi_it.string.lstring = Qnil;
5609 it->bidi_it.string.s = s;
5610 it->bidi_it.string.schars = it->end_charpos;
5611 it->bidi_it.string.bufpos = 0;
5612 it->bidi_it.string.from_disp_str = 0;
5613 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5614 &it->bidi_it);
5615 }
5616 #endif
5617 }
5618 else
5619 {
5620 /* Unibyte (a.k.a. ASCII) C strings are never bidi-reordered. */
5621 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5622 it->end_charpos = it->string_nchars = strlen (s);
5623 }
5624
5625 it->method = GET_FROM_C_STRING;
5626 }
5627
5628 /* PRECISION > 0 means don't return more than PRECISION characters
5629 from the string. */
5630 if (precision > 0 && it->end_charpos - charpos > precision)
5631 {
5632 it->end_charpos = it->string_nchars = charpos + precision;
5633 #if 0
5634 if (it->bidi_p)
5635 it->bidi_it.string.schars = it->end_charpos;
5636 #endif
5637 }
5638
5639 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5640 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5641 FIELD_WIDTH < 0 means infinite field width. This is useful for
5642 padding with `-' at the end of a mode line. */
5643 if (field_width < 0)
5644 field_width = INFINITY;
5645 /* Implementation note: We deliberately don't enlarge
5646 it->bidi_it.string.schars here to fit it->end_charpos, because
5647 the bidi iterator cannot produce characters out of thin air. */
5648 if (field_width > it->end_charpos - charpos)
5649 it->end_charpos = charpos + field_width;
5650
5651 /* Use the standard display table for displaying strings. */
5652 if (DISP_TABLE_P (Vstandard_display_table))
5653 it->dp = XCHAR_TABLE (Vstandard_display_table);
5654
5655 it->stop_charpos = charpos;
5656 #if 0
5657 it->prev_stop = charpos;
5658 it->base_level_stop = 0;
5659 if (it->bidi_p)
5660 {
5661 it->bidi_it.first_elt = 1;
5662 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5663 it->bidi_it.disp_pos = -1;
5664 }
5665 #endif
5666 if (s == NULL && it->multibyte_p)
5667 {
5668 EMACS_INT endpos = SCHARS (it->string);
5669 if (endpos > it->end_charpos)
5670 endpos = it->end_charpos;
5671 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5672 it->string);
5673 }
5674 CHECK_IT (it);
5675 }
5676
5677
5678 \f
5679 /***********************************************************************
5680 Iteration
5681 ***********************************************************************/
5682
5683 /* Map enum it_method value to corresponding next_element_from_* function. */
5684
5685 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5686 {
5687 next_element_from_buffer,
5688 next_element_from_display_vector,
5689 next_element_from_string,
5690 next_element_from_c_string,
5691 next_element_from_image,
5692 next_element_from_stretch
5693 };
5694
5695 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5696
5697
5698 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5699 (possibly with the following characters). */
5700
5701 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5702 ((IT)->cmp_it.id >= 0 \
5703 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5704 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5705 END_CHARPOS, (IT)->w, \
5706 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5707 (IT)->string)))
5708
5709
5710 /* Lookup the char-table Vglyphless_char_display for character C (-1
5711 if we want information for no-font case), and return the display
5712 method symbol. By side-effect, update it->what and
5713 it->glyphless_method. This function is called from
5714 get_next_display_element for each character element, and from
5715 x_produce_glyphs when no suitable font was found. */
5716
5717 Lisp_Object
5718 lookup_glyphless_char_display (int c, struct it *it)
5719 {
5720 Lisp_Object glyphless_method = Qnil;
5721
5722 if (CHAR_TABLE_P (Vglyphless_char_display)
5723 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5724 {
5725 if (c >= 0)
5726 {
5727 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
5728 if (CONSP (glyphless_method))
5729 glyphless_method = FRAME_WINDOW_P (it->f)
5730 ? XCAR (glyphless_method)
5731 : XCDR (glyphless_method);
5732 }
5733 else
5734 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
5735 }
5736
5737 retry:
5738 if (NILP (glyphless_method))
5739 {
5740 if (c >= 0)
5741 /* The default is to display the character by a proper font. */
5742 return Qnil;
5743 /* The default for the no-font case is to display an empty box. */
5744 glyphless_method = Qempty_box;
5745 }
5746 if (EQ (glyphless_method, Qzero_width))
5747 {
5748 if (c >= 0)
5749 return glyphless_method;
5750 /* This method can't be used for the no-font case. */
5751 glyphless_method = Qempty_box;
5752 }
5753 if (EQ (glyphless_method, Qthin_space))
5754 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5755 else if (EQ (glyphless_method, Qempty_box))
5756 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5757 else if (EQ (glyphless_method, Qhex_code))
5758 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5759 else if (STRINGP (glyphless_method))
5760 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5761 else
5762 {
5763 /* Invalid value. We use the default method. */
5764 glyphless_method = Qnil;
5765 goto retry;
5766 }
5767 it->what = IT_GLYPHLESS;
5768 return glyphless_method;
5769 }
5770
5771 /* Load IT's display element fields with information about the next
5772 display element from the current position of IT. Value is zero if
5773 end of buffer (or C string) is reached. */
5774
5775 static struct frame *last_escape_glyph_frame = NULL;
5776 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5777 static int last_escape_glyph_merged_face_id = 0;
5778
5779 struct frame *last_glyphless_glyph_frame = NULL;
5780 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5781 int last_glyphless_glyph_merged_face_id = 0;
5782
5783 static int
5784 get_next_display_element (struct it *it)
5785 {
5786 /* Non-zero means that we found a display element. Zero means that
5787 we hit the end of what we iterate over. Performance note: the
5788 function pointer `method' used here turns out to be faster than
5789 using a sequence of if-statements. */
5790 int success_p;
5791
5792 get_next:
5793 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5794
5795 if (it->what == IT_CHARACTER)
5796 {
5797 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5798 and only if (a) the resolved directionality of that character
5799 is R..." */
5800 /* FIXME: Do we need an exception for characters from display
5801 tables? */
5802 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5803 it->c = bidi_mirror_char (it->c);
5804 /* Map via display table or translate control characters.
5805 IT->c, IT->len etc. have been set to the next character by
5806 the function call above. If we have a display table, and it
5807 contains an entry for IT->c, translate it. Don't do this if
5808 IT->c itself comes from a display table, otherwise we could
5809 end up in an infinite recursion. (An alternative could be to
5810 count the recursion depth of this function and signal an
5811 error when a certain maximum depth is reached.) Is it worth
5812 it? */
5813 if (success_p && it->dpvec == NULL)
5814 {
5815 Lisp_Object dv;
5816 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5817 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5818 nbsp_or_shy = char_is_other;
5819 int c = it->c; /* This is the character to display. */
5820
5821 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5822 {
5823 xassert (SINGLE_BYTE_CHAR_P (c));
5824 if (unibyte_display_via_language_environment)
5825 {
5826 c = DECODE_CHAR (unibyte, c);
5827 if (c < 0)
5828 c = BYTE8_TO_CHAR (it->c);
5829 }
5830 else
5831 c = BYTE8_TO_CHAR (it->c);
5832 }
5833
5834 if (it->dp
5835 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5836 VECTORP (dv)))
5837 {
5838 struct Lisp_Vector *v = XVECTOR (dv);
5839
5840 /* Return the first character from the display table
5841 entry, if not empty. If empty, don't display the
5842 current character. */
5843 if (v->header.size)
5844 {
5845 it->dpvec_char_len = it->len;
5846 it->dpvec = v->contents;
5847 it->dpend = v->contents + v->header.size;
5848 it->current.dpvec_index = 0;
5849 it->dpvec_face_id = -1;
5850 it->saved_face_id = it->face_id;
5851 it->method = GET_FROM_DISPLAY_VECTOR;
5852 it->ellipsis_p = 0;
5853 }
5854 else
5855 {
5856 set_iterator_to_next (it, 0);
5857 }
5858 goto get_next;
5859 }
5860
5861 if (! NILP (lookup_glyphless_char_display (c, it)))
5862 {
5863 if (it->what == IT_GLYPHLESS)
5864 goto done;
5865 /* Don't display this character. */
5866 set_iterator_to_next (it, 0);
5867 goto get_next;
5868 }
5869
5870 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5871 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5872 : c == 0xAD ? char_is_soft_hyphen
5873 : char_is_other);
5874
5875 /* Translate control characters into `\003' or `^C' form.
5876 Control characters coming from a display table entry are
5877 currently not translated because we use IT->dpvec to hold
5878 the translation. This could easily be changed but I
5879 don't believe that it is worth doing.
5880
5881 NBSP and SOFT-HYPEN are property translated too.
5882
5883 Non-printable characters and raw-byte characters are also
5884 translated to octal form. */
5885 if (((c < ' ' || c == 127) /* ASCII control chars */
5886 ? (it->area != TEXT_AREA
5887 /* In mode line, treat \n, \t like other crl chars. */
5888 || (c != '\t'
5889 && it->glyph_row
5890 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5891 || (c != '\n' && c != '\t'))
5892 : (nbsp_or_shy
5893 || CHAR_BYTE8_P (c)
5894 || ! CHAR_PRINTABLE_P (c))))
5895 {
5896 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5897 or a non-printable character which must be displayed
5898 either as '\003' or as `^C' where the '\\' and '^'
5899 can be defined in the display table. Fill
5900 IT->ctl_chars with glyphs for what we have to
5901 display. Then, set IT->dpvec to these glyphs. */
5902 Lisp_Object gc;
5903 int ctl_len;
5904 int face_id, lface_id = 0 ;
5905 int escape_glyph;
5906
5907 /* Handle control characters with ^. */
5908
5909 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5910 {
5911 int g;
5912
5913 g = '^'; /* default glyph for Control */
5914 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5915 if (it->dp
5916 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5917 && GLYPH_CODE_CHAR_VALID_P (gc))
5918 {
5919 g = GLYPH_CODE_CHAR (gc);
5920 lface_id = GLYPH_CODE_FACE (gc);
5921 }
5922 if (lface_id)
5923 {
5924 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5925 }
5926 else if (it->f == last_escape_glyph_frame
5927 && it->face_id == last_escape_glyph_face_id)
5928 {
5929 face_id = last_escape_glyph_merged_face_id;
5930 }
5931 else
5932 {
5933 /* Merge the escape-glyph face into the current face. */
5934 face_id = merge_faces (it->f, Qescape_glyph, 0,
5935 it->face_id);
5936 last_escape_glyph_frame = it->f;
5937 last_escape_glyph_face_id = it->face_id;
5938 last_escape_glyph_merged_face_id = face_id;
5939 }
5940
5941 XSETINT (it->ctl_chars[0], g);
5942 XSETINT (it->ctl_chars[1], c ^ 0100);
5943 ctl_len = 2;
5944 goto display_control;
5945 }
5946
5947 /* Handle non-break space in the mode where it only gets
5948 highlighting. */
5949
5950 if (EQ (Vnobreak_char_display, Qt)
5951 && nbsp_or_shy == char_is_nbsp)
5952 {
5953 /* Merge the no-break-space face into the current face. */
5954 face_id = merge_faces (it->f, Qnobreak_space, 0,
5955 it->face_id);
5956
5957 c = ' ';
5958 XSETINT (it->ctl_chars[0], ' ');
5959 ctl_len = 1;
5960 goto display_control;
5961 }
5962
5963 /* Handle sequences that start with the "escape glyph". */
5964
5965 /* the default escape glyph is \. */
5966 escape_glyph = '\\';
5967
5968 if (it->dp
5969 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5970 && GLYPH_CODE_CHAR_VALID_P (gc))
5971 {
5972 escape_glyph = GLYPH_CODE_CHAR (gc);
5973 lface_id = GLYPH_CODE_FACE (gc);
5974 }
5975 if (lface_id)
5976 {
5977 /* The display table specified a face.
5978 Merge it into face_id and also into escape_glyph. */
5979 face_id = merge_faces (it->f, Qt, lface_id,
5980 it->face_id);
5981 }
5982 else if (it->f == last_escape_glyph_frame
5983 && it->face_id == last_escape_glyph_face_id)
5984 {
5985 face_id = last_escape_glyph_merged_face_id;
5986 }
5987 else
5988 {
5989 /* Merge the escape-glyph face into the current face. */
5990 face_id = merge_faces (it->f, Qescape_glyph, 0,
5991 it->face_id);
5992 last_escape_glyph_frame = it->f;
5993 last_escape_glyph_face_id = it->face_id;
5994 last_escape_glyph_merged_face_id = face_id;
5995 }
5996
5997 /* Handle soft hyphens in the mode where they only get
5998 highlighting. */
5999
6000 if (EQ (Vnobreak_char_display, Qt)
6001 && nbsp_or_shy == char_is_soft_hyphen)
6002 {
6003 XSETINT (it->ctl_chars[0], '-');
6004 ctl_len = 1;
6005 goto display_control;
6006 }
6007
6008 /* Handle non-break space and soft hyphen
6009 with the escape glyph. */
6010
6011 if (nbsp_or_shy)
6012 {
6013 XSETINT (it->ctl_chars[0], escape_glyph);
6014 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6015 XSETINT (it->ctl_chars[1], c);
6016 ctl_len = 2;
6017 goto display_control;
6018 }
6019
6020 {
6021 char str[10];
6022 int len, i;
6023
6024 if (CHAR_BYTE8_P (c))
6025 /* Display \200 instead of \17777600. */
6026 c = CHAR_TO_BYTE8 (c);
6027 len = sprintf (str, "%03o", c);
6028
6029 XSETINT (it->ctl_chars[0], escape_glyph);
6030 for (i = 0; i < len; i++)
6031 XSETINT (it->ctl_chars[i + 1], str[i]);
6032 ctl_len = len + 1;
6033 }
6034
6035 display_control:
6036 /* Set up IT->dpvec and return first character from it. */
6037 it->dpvec_char_len = it->len;
6038 it->dpvec = it->ctl_chars;
6039 it->dpend = it->dpvec + ctl_len;
6040 it->current.dpvec_index = 0;
6041 it->dpvec_face_id = face_id;
6042 it->saved_face_id = it->face_id;
6043 it->method = GET_FROM_DISPLAY_VECTOR;
6044 it->ellipsis_p = 0;
6045 goto get_next;
6046 }
6047 it->char_to_display = c;
6048 }
6049 else if (success_p)
6050 {
6051 it->char_to_display = it->c;
6052 }
6053 }
6054
6055 /* Adjust face id for a multibyte character. There are no multibyte
6056 character in unibyte text. */
6057 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6058 && it->multibyte_p
6059 && success_p
6060 && FRAME_WINDOW_P (it->f))
6061 {
6062 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6063
6064 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6065 {
6066 /* Automatic composition with glyph-string. */
6067 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6068
6069 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6070 }
6071 else
6072 {
6073 EMACS_INT pos = (it->s ? -1
6074 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6075 : IT_CHARPOS (*it));
6076
6077 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6078 it->string);
6079 }
6080 }
6081
6082 done:
6083 /* Is this character the last one of a run of characters with
6084 box? If yes, set IT->end_of_box_run_p to 1. */
6085 if (it->face_box_p
6086 && it->s == NULL)
6087 {
6088 if (it->method == GET_FROM_STRING && it->sp)
6089 {
6090 int face_id = underlying_face_id (it);
6091 struct face *face = FACE_FROM_ID (it->f, face_id);
6092
6093 if (face)
6094 {
6095 if (face->box == FACE_NO_BOX)
6096 {
6097 /* If the box comes from face properties in a
6098 display string, check faces in that string. */
6099 int string_face_id = face_after_it_pos (it);
6100 it->end_of_box_run_p
6101 = (FACE_FROM_ID (it->f, string_face_id)->box
6102 == FACE_NO_BOX);
6103 }
6104 /* Otherwise, the box comes from the underlying face.
6105 If this is the last string character displayed, check
6106 the next buffer location. */
6107 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6108 && (it->current.overlay_string_index
6109 == it->n_overlay_strings - 1))
6110 {
6111 EMACS_INT ignore;
6112 int next_face_id;
6113 struct text_pos pos = it->current.pos;
6114 INC_TEXT_POS (pos, it->multibyte_p);
6115
6116 next_face_id = face_at_buffer_position
6117 (it->w, CHARPOS (pos), it->region_beg_charpos,
6118 it->region_end_charpos, &ignore,
6119 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6120 -1);
6121 it->end_of_box_run_p
6122 = (FACE_FROM_ID (it->f, next_face_id)->box
6123 == FACE_NO_BOX);
6124 }
6125 }
6126 }
6127 else
6128 {
6129 int face_id = face_after_it_pos (it);
6130 it->end_of_box_run_p
6131 = (face_id != it->face_id
6132 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6133 }
6134 }
6135
6136 /* Value is 0 if end of buffer or string reached. */
6137 return success_p;
6138 }
6139
6140
6141 /* Move IT to the next display element.
6142
6143 RESEAT_P non-zero means if called on a newline in buffer text,
6144 skip to the next visible line start.
6145
6146 Functions get_next_display_element and set_iterator_to_next are
6147 separate because I find this arrangement easier to handle than a
6148 get_next_display_element function that also increments IT's
6149 position. The way it is we can first look at an iterator's current
6150 display element, decide whether it fits on a line, and if it does,
6151 increment the iterator position. The other way around we probably
6152 would either need a flag indicating whether the iterator has to be
6153 incremented the next time, or we would have to implement a
6154 decrement position function which would not be easy to write. */
6155
6156 void
6157 set_iterator_to_next (struct it *it, int reseat_p)
6158 {
6159 /* Reset flags indicating start and end of a sequence of characters
6160 with box. Reset them at the start of this function because
6161 moving the iterator to a new position might set them. */
6162 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6163
6164 switch (it->method)
6165 {
6166 case GET_FROM_BUFFER:
6167 /* The current display element of IT is a character from
6168 current_buffer. Advance in the buffer, and maybe skip over
6169 invisible lines that are so because of selective display. */
6170 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6171 reseat_at_next_visible_line_start (it, 0);
6172 else if (it->cmp_it.id >= 0)
6173 {
6174 /* We are currently getting glyphs from a composition. */
6175 int i;
6176
6177 if (! it->bidi_p)
6178 {
6179 IT_CHARPOS (*it) += it->cmp_it.nchars;
6180 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6181 if (it->cmp_it.to < it->cmp_it.nglyphs)
6182 {
6183 it->cmp_it.from = it->cmp_it.to;
6184 }
6185 else
6186 {
6187 it->cmp_it.id = -1;
6188 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6189 IT_BYTEPOS (*it),
6190 it->end_charpos, Qnil);
6191 }
6192 }
6193 else if (! it->cmp_it.reversed_p)
6194 {
6195 /* Composition created while scanning forward. */
6196 /* Update IT's char/byte positions to point to the first
6197 character of the next grapheme cluster, or to the
6198 character visually after the current composition. */
6199 for (i = 0; i < it->cmp_it.nchars; i++)
6200 bidi_move_to_visually_next (&it->bidi_it);
6201 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6202 IT_CHARPOS (*it) = it->bidi_it.charpos;
6203
6204 if (it->cmp_it.to < it->cmp_it.nglyphs)
6205 {
6206 /* Proceed to the next grapheme cluster. */
6207 it->cmp_it.from = it->cmp_it.to;
6208 }
6209 else
6210 {
6211 /* No more grapheme clusters in this composition.
6212 Find the next stop position. */
6213 EMACS_INT stop = it->end_charpos;
6214 if (it->bidi_it.scan_dir < 0)
6215 /* Now we are scanning backward and don't know
6216 where to stop. */
6217 stop = -1;
6218 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6219 IT_BYTEPOS (*it), stop, Qnil);
6220 }
6221 }
6222 else
6223 {
6224 /* Composition created while scanning backward. */
6225 /* Update IT's char/byte positions to point to the last
6226 character of the previous grapheme cluster, or the
6227 character visually after the current composition. */
6228 for (i = 0; i < it->cmp_it.nchars; i++)
6229 bidi_move_to_visually_next (&it->bidi_it);
6230 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6231 IT_CHARPOS (*it) = it->bidi_it.charpos;
6232 if (it->cmp_it.from > 0)
6233 {
6234 /* Proceed to the previous grapheme cluster. */
6235 it->cmp_it.to = it->cmp_it.from;
6236 }
6237 else
6238 {
6239 /* No more grapheme clusters in this composition.
6240 Find the next stop position. */
6241 EMACS_INT stop = it->end_charpos;
6242 if (it->bidi_it.scan_dir < 0)
6243 /* Now we are scanning backward and don't know
6244 where to stop. */
6245 stop = -1;
6246 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6247 IT_BYTEPOS (*it), stop, Qnil);
6248 }
6249 }
6250 }
6251 else
6252 {
6253 xassert (it->len != 0);
6254
6255 if (!it->bidi_p)
6256 {
6257 IT_BYTEPOS (*it) += it->len;
6258 IT_CHARPOS (*it) += 1;
6259 }
6260 else
6261 {
6262 int prev_scan_dir = it->bidi_it.scan_dir;
6263 /* If this is a new paragraph, determine its base
6264 direction (a.k.a. its base embedding level). */
6265 if (it->bidi_it.new_paragraph)
6266 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6267 bidi_move_to_visually_next (&it->bidi_it);
6268 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6269 IT_CHARPOS (*it) = it->bidi_it.charpos;
6270 if (prev_scan_dir != it->bidi_it.scan_dir)
6271 {
6272 /* As the scan direction was changed, we must
6273 re-compute the stop position for composition. */
6274 EMACS_INT stop = it->end_charpos;
6275 if (it->bidi_it.scan_dir < 0)
6276 stop = -1;
6277 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6278 IT_BYTEPOS (*it), stop, Qnil);
6279 }
6280 }
6281 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6282 }
6283 break;
6284
6285 case GET_FROM_C_STRING:
6286 /* Current display element of IT is from a C string. */
6287 IT_BYTEPOS (*it) += it->len;
6288 IT_CHARPOS (*it) += 1;
6289 break;
6290
6291 case GET_FROM_DISPLAY_VECTOR:
6292 /* Current display element of IT is from a display table entry.
6293 Advance in the display table definition. Reset it to null if
6294 end reached, and continue with characters from buffers/
6295 strings. */
6296 ++it->current.dpvec_index;
6297
6298 /* Restore face of the iterator to what they were before the
6299 display vector entry (these entries may contain faces). */
6300 it->face_id = it->saved_face_id;
6301
6302 if (it->dpvec + it->current.dpvec_index == it->dpend)
6303 {
6304 int recheck_faces = it->ellipsis_p;
6305
6306 if (it->s)
6307 it->method = GET_FROM_C_STRING;
6308 else if (STRINGP (it->string))
6309 it->method = GET_FROM_STRING;
6310 else
6311 {
6312 it->method = GET_FROM_BUFFER;
6313 it->object = it->w->buffer;
6314 }
6315
6316 it->dpvec = NULL;
6317 it->current.dpvec_index = -1;
6318
6319 /* Skip over characters which were displayed via IT->dpvec. */
6320 if (it->dpvec_char_len < 0)
6321 reseat_at_next_visible_line_start (it, 1);
6322 else if (it->dpvec_char_len > 0)
6323 {
6324 if (it->method == GET_FROM_STRING
6325 && it->n_overlay_strings > 0)
6326 it->ignore_overlay_strings_at_pos_p = 1;
6327 it->len = it->dpvec_char_len;
6328 set_iterator_to_next (it, reseat_p);
6329 }
6330
6331 /* Maybe recheck faces after display vector */
6332 if (recheck_faces)
6333 it->stop_charpos = IT_CHARPOS (*it);
6334 }
6335 break;
6336
6337 case GET_FROM_STRING:
6338 /* Current display element is a character from a Lisp string. */
6339 xassert (it->s == NULL && STRINGP (it->string));
6340 if (it->cmp_it.id >= 0)
6341 {
6342 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6343 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6344 if (it->cmp_it.to < it->cmp_it.nglyphs)
6345 it->cmp_it.from = it->cmp_it.to;
6346 else
6347 {
6348 it->cmp_it.id = -1;
6349 composition_compute_stop_pos (&it->cmp_it,
6350 IT_STRING_CHARPOS (*it),
6351 IT_STRING_BYTEPOS (*it),
6352 it->end_charpos, it->string);
6353 }
6354 }
6355 else
6356 {
6357 IT_STRING_BYTEPOS (*it) += it->len;
6358 IT_STRING_CHARPOS (*it) += 1;
6359 }
6360
6361 consider_string_end:
6362
6363 if (it->current.overlay_string_index >= 0)
6364 {
6365 /* IT->string is an overlay string. Advance to the
6366 next, if there is one. */
6367 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6368 {
6369 it->ellipsis_p = 0;
6370 next_overlay_string (it);
6371 if (it->ellipsis_p)
6372 setup_for_ellipsis (it, 0);
6373 }
6374 }
6375 else
6376 {
6377 /* IT->string is not an overlay string. If we reached
6378 its end, and there is something on IT->stack, proceed
6379 with what is on the stack. This can be either another
6380 string, this time an overlay string, or a buffer. */
6381 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6382 && it->sp > 0)
6383 {
6384 pop_it (it);
6385 if (it->method == GET_FROM_STRING)
6386 goto consider_string_end;
6387 }
6388 }
6389 break;
6390
6391 case GET_FROM_IMAGE:
6392 case GET_FROM_STRETCH:
6393 /* The position etc with which we have to proceed are on
6394 the stack. The position may be at the end of a string,
6395 if the `display' property takes up the whole string. */
6396 xassert (it->sp > 0);
6397 pop_it (it);
6398 if (it->method == GET_FROM_STRING)
6399 goto consider_string_end;
6400 break;
6401
6402 default:
6403 /* There are no other methods defined, so this should be a bug. */
6404 abort ();
6405 }
6406
6407 xassert (it->method != GET_FROM_STRING
6408 || (STRINGP (it->string)
6409 && IT_STRING_CHARPOS (*it) >= 0));
6410 }
6411
6412 /* Load IT's display element fields with information about the next
6413 display element which comes from a display table entry or from the
6414 result of translating a control character to one of the forms `^C'
6415 or `\003'.
6416
6417 IT->dpvec holds the glyphs to return as characters.
6418 IT->saved_face_id holds the face id before the display vector--it
6419 is restored into IT->face_id in set_iterator_to_next. */
6420
6421 static int
6422 next_element_from_display_vector (struct it *it)
6423 {
6424 Lisp_Object gc;
6425
6426 /* Precondition. */
6427 xassert (it->dpvec && it->current.dpvec_index >= 0);
6428
6429 it->face_id = it->saved_face_id;
6430
6431 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6432 That seemed totally bogus - so I changed it... */
6433 gc = it->dpvec[it->current.dpvec_index];
6434
6435 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6436 {
6437 it->c = GLYPH_CODE_CHAR (gc);
6438 it->len = CHAR_BYTES (it->c);
6439
6440 /* The entry may contain a face id to use. Such a face id is
6441 the id of a Lisp face, not a realized face. A face id of
6442 zero means no face is specified. */
6443 if (it->dpvec_face_id >= 0)
6444 it->face_id = it->dpvec_face_id;
6445 else
6446 {
6447 int lface_id = GLYPH_CODE_FACE (gc);
6448 if (lface_id > 0)
6449 it->face_id = merge_faces (it->f, Qt, lface_id,
6450 it->saved_face_id);
6451 }
6452 }
6453 else
6454 /* Display table entry is invalid. Return a space. */
6455 it->c = ' ', it->len = 1;
6456
6457 /* Don't change position and object of the iterator here. They are
6458 still the values of the character that had this display table
6459 entry or was translated, and that's what we want. */
6460 it->what = IT_CHARACTER;
6461 return 1;
6462 }
6463
6464
6465 /* Load IT with the next display element from Lisp string IT->string.
6466 IT->current.string_pos is the current position within the string.
6467 If IT->current.overlay_string_index >= 0, the Lisp string is an
6468 overlay string. */
6469
6470 static int
6471 next_element_from_string (struct it *it)
6472 {
6473 struct text_pos position;
6474
6475 xassert (STRINGP (it->string));
6476 xassert (IT_STRING_CHARPOS (*it) >= 0);
6477 position = it->current.string_pos;
6478
6479 /* Time to check for invisible text? */
6480 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6481 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6482 {
6483 handle_stop (it);
6484
6485 /* Since a handler may have changed IT->method, we must
6486 recurse here. */
6487 return GET_NEXT_DISPLAY_ELEMENT (it);
6488 }
6489
6490 if (it->current.overlay_string_index >= 0)
6491 {
6492 /* Get the next character from an overlay string. In overlay
6493 strings, There is no field width or padding with spaces to
6494 do. */
6495 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6496 {
6497 it->what = IT_EOB;
6498 return 0;
6499 }
6500 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6501 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6502 && next_element_from_composition (it))
6503 {
6504 return 1;
6505 }
6506 else if (STRING_MULTIBYTE (it->string))
6507 {
6508 const unsigned char *s = (SDATA (it->string)
6509 + IT_STRING_BYTEPOS (*it));
6510 it->c = string_char_and_length (s, &it->len);
6511 }
6512 else
6513 {
6514 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6515 it->len = 1;
6516 }
6517 }
6518 else
6519 {
6520 /* Get the next character from a Lisp string that is not an
6521 overlay string. Such strings come from the mode line, for
6522 example. We may have to pad with spaces, or truncate the
6523 string. See also next_element_from_c_string. */
6524 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6525 {
6526 it->what = IT_EOB;
6527 return 0;
6528 }
6529 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6530 {
6531 /* Pad with spaces. */
6532 it->c = ' ', it->len = 1;
6533 CHARPOS (position) = BYTEPOS (position) = -1;
6534 }
6535 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6536 IT_STRING_BYTEPOS (*it), it->string_nchars)
6537 && next_element_from_composition (it))
6538 {
6539 return 1;
6540 }
6541 else if (STRING_MULTIBYTE (it->string))
6542 {
6543 const unsigned char *s = (SDATA (it->string)
6544 + IT_STRING_BYTEPOS (*it));
6545 it->c = string_char_and_length (s, &it->len);
6546 }
6547 else
6548 {
6549 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6550 it->len = 1;
6551 }
6552 }
6553
6554 /* Record what we have and where it came from. */
6555 it->what = IT_CHARACTER;
6556 it->object = it->string;
6557 it->position = position;
6558 return 1;
6559 }
6560
6561
6562 /* Load IT with next display element from C string IT->s.
6563 IT->string_nchars is the maximum number of characters to return
6564 from the string. IT->end_charpos may be greater than
6565 IT->string_nchars when this function is called, in which case we
6566 may have to return padding spaces. Value is zero if end of string
6567 reached, including padding spaces. */
6568
6569 static int
6570 next_element_from_c_string (struct it *it)
6571 {
6572 int success_p = 1;
6573
6574 xassert (it->s);
6575 it->what = IT_CHARACTER;
6576 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6577 it->object = Qnil;
6578
6579 /* IT's position can be greater IT->string_nchars in case a field
6580 width or precision has been specified when the iterator was
6581 initialized. */
6582 if (IT_CHARPOS (*it) >= it->end_charpos)
6583 {
6584 /* End of the game. */
6585 it->what = IT_EOB;
6586 success_p = 0;
6587 }
6588 else if (IT_CHARPOS (*it) >= it->string_nchars)
6589 {
6590 /* Pad with spaces. */
6591 it->c = ' ', it->len = 1;
6592 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6593 }
6594 else if (it->multibyte_p)
6595 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6596 else
6597 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6598
6599 return success_p;
6600 }
6601
6602
6603 /* Set up IT to return characters from an ellipsis, if appropriate.
6604 The definition of the ellipsis glyphs may come from a display table
6605 entry. This function fills IT with the first glyph from the
6606 ellipsis if an ellipsis is to be displayed. */
6607
6608 static int
6609 next_element_from_ellipsis (struct it *it)
6610 {
6611 if (it->selective_display_ellipsis_p)
6612 setup_for_ellipsis (it, it->len);
6613 else
6614 {
6615 /* The face at the current position may be different from the
6616 face we find after the invisible text. Remember what it
6617 was in IT->saved_face_id, and signal that it's there by
6618 setting face_before_selective_p. */
6619 it->saved_face_id = it->face_id;
6620 it->method = GET_FROM_BUFFER;
6621 it->object = it->w->buffer;
6622 reseat_at_next_visible_line_start (it, 1);
6623 it->face_before_selective_p = 1;
6624 }
6625
6626 return GET_NEXT_DISPLAY_ELEMENT (it);
6627 }
6628
6629
6630 /* Deliver an image display element. The iterator IT is already
6631 filled with image information (done in handle_display_prop). Value
6632 is always 1. */
6633
6634
6635 static int
6636 next_element_from_image (struct it *it)
6637 {
6638 it->what = IT_IMAGE;
6639 it->ignore_overlay_strings_at_pos_p = 0;
6640 return 1;
6641 }
6642
6643
6644 /* Fill iterator IT with next display element from a stretch glyph
6645 property. IT->object is the value of the text property. Value is
6646 always 1. */
6647
6648 static int
6649 next_element_from_stretch (struct it *it)
6650 {
6651 it->what = IT_STRETCH;
6652 return 1;
6653 }
6654
6655 /* Scan forward from CHARPOS in the current buffer, until we find a
6656 stop position > current IT's position. Then handle the stop
6657 position before that. This is called when we bump into a stop
6658 position while reordering bidirectional text. CHARPOS should be
6659 the last previously processed stop_pos (or BEGV, if none were
6660 processed yet) whose position is less that IT's current
6661 position. */
6662
6663 static void
6664 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6665 {
6666 EMACS_INT where_we_are = IT_CHARPOS (*it);
6667 struct display_pos save_current = it->current;
6668 struct text_pos save_position = it->position;
6669 struct text_pos pos1;
6670 EMACS_INT next_stop;
6671
6672 /* Scan in strict logical order. */
6673 it->bidi_p = 0;
6674 do
6675 {
6676 it->prev_stop = charpos;
6677 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6678 reseat_1 (it, pos1, 0);
6679 compute_stop_pos (it);
6680 /* We must advance forward, right? */
6681 if (it->stop_charpos <= it->prev_stop)
6682 abort ();
6683 charpos = it->stop_charpos;
6684 }
6685 while (charpos <= where_we_are);
6686
6687 next_stop = it->stop_charpos;
6688 it->stop_charpos = it->prev_stop;
6689 it->bidi_p = 1;
6690 it->current = save_current;
6691 it->position = save_position;
6692 handle_stop (it);
6693 it->stop_charpos = next_stop;
6694 }
6695
6696 /* Load IT with the next display element from current_buffer. Value
6697 is zero if end of buffer reached. IT->stop_charpos is the next
6698 position at which to stop and check for text properties or buffer
6699 end. */
6700
6701 static int
6702 next_element_from_buffer (struct it *it)
6703 {
6704 int success_p = 1;
6705
6706 xassert (IT_CHARPOS (*it) >= BEGV);
6707
6708 /* With bidi reordering, the character to display might not be the
6709 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6710 we were reseat()ed to a new buffer position, which is potentially
6711 a different paragraph. */
6712 if (it->bidi_p && it->bidi_it.first_elt)
6713 {
6714 it->bidi_it.charpos = IT_CHARPOS (*it);
6715 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6716 if (it->bidi_it.bytepos == ZV_BYTE)
6717 {
6718 /* Nothing to do, but reset the FIRST_ELT flag, like
6719 bidi_paragraph_init does, because we are not going to
6720 call it. */
6721 it->bidi_it.first_elt = 0;
6722 }
6723 else if (it->bidi_it.bytepos == BEGV_BYTE
6724 /* FIXME: Should support all Unicode line separators. */
6725 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6726 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6727 {
6728 /* If we are at the beginning of a line, we can produce the
6729 next element right away. */
6730 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6731 bidi_move_to_visually_next (&it->bidi_it);
6732 }
6733 else
6734 {
6735 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6736
6737 /* We need to prime the bidi iterator starting at the line's
6738 beginning, before we will be able to produce the next
6739 element. */
6740 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6741 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6742 it->bidi_it.charpos = IT_CHARPOS (*it);
6743 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6744 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6745 do
6746 {
6747 /* Now return to buffer position where we were asked to
6748 get the next display element, and produce that. */
6749 bidi_move_to_visually_next (&it->bidi_it);
6750 }
6751 while (it->bidi_it.bytepos != orig_bytepos
6752 && it->bidi_it.bytepos < ZV_BYTE);
6753 }
6754
6755 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6756 /* Adjust IT's position information to where we ended up. */
6757 IT_CHARPOS (*it) = it->bidi_it.charpos;
6758 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6759 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6760 {
6761 EMACS_INT stop = it->end_charpos;
6762 if (it->bidi_it.scan_dir < 0)
6763 stop = -1;
6764 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6765 IT_BYTEPOS (*it), stop, Qnil);
6766 }
6767 }
6768
6769 if (IT_CHARPOS (*it) >= it->stop_charpos)
6770 {
6771 if (IT_CHARPOS (*it) >= it->end_charpos)
6772 {
6773 int overlay_strings_follow_p;
6774
6775 /* End of the game, except when overlay strings follow that
6776 haven't been returned yet. */
6777 if (it->overlay_strings_at_end_processed_p)
6778 overlay_strings_follow_p = 0;
6779 else
6780 {
6781 it->overlay_strings_at_end_processed_p = 1;
6782 overlay_strings_follow_p = get_overlay_strings (it, 0);
6783 }
6784
6785 if (overlay_strings_follow_p)
6786 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6787 else
6788 {
6789 it->what = IT_EOB;
6790 it->position = it->current.pos;
6791 success_p = 0;
6792 }
6793 }
6794 else if (!(!it->bidi_p
6795 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6796 || IT_CHARPOS (*it) == it->stop_charpos))
6797 {
6798 /* With bidi non-linear iteration, we could find ourselves
6799 far beyond the last computed stop_charpos, with several
6800 other stop positions in between that we missed. Scan
6801 them all now, in buffer's logical order, until we find
6802 and handle the last stop_charpos that precedes our
6803 current position. */
6804 handle_stop_backwards (it, it->stop_charpos);
6805 return GET_NEXT_DISPLAY_ELEMENT (it);
6806 }
6807 else
6808 {
6809 if (it->bidi_p)
6810 {
6811 /* Take note of the stop position we just moved across,
6812 for when we will move back across it. */
6813 it->prev_stop = it->stop_charpos;
6814 /* If we are at base paragraph embedding level, take
6815 note of the last stop position seen at this
6816 level. */
6817 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6818 it->base_level_stop = it->stop_charpos;
6819 }
6820 handle_stop (it);
6821 return GET_NEXT_DISPLAY_ELEMENT (it);
6822 }
6823 }
6824 else if (it->bidi_p
6825 /* We can sometimes back up for reasons that have nothing
6826 to do with bidi reordering. E.g., compositions. The
6827 code below is only needed when we are above the base
6828 embedding level, so test for that explicitly. */
6829 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6830 && IT_CHARPOS (*it) < it->prev_stop)
6831 {
6832 if (it->base_level_stop <= 0)
6833 it->base_level_stop = BEGV;
6834 if (IT_CHARPOS (*it) < it->base_level_stop)
6835 abort ();
6836 handle_stop_backwards (it, it->base_level_stop);
6837 return GET_NEXT_DISPLAY_ELEMENT (it);
6838 }
6839 else
6840 {
6841 /* No face changes, overlays etc. in sight, so just return a
6842 character from current_buffer. */
6843 unsigned char *p;
6844 EMACS_INT stop;
6845
6846 /* Maybe run the redisplay end trigger hook. Performance note:
6847 This doesn't seem to cost measurable time. */
6848 if (it->redisplay_end_trigger_charpos
6849 && it->glyph_row
6850 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6851 run_redisplay_end_trigger_hook (it);
6852
6853 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6854 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6855 stop)
6856 && next_element_from_composition (it))
6857 {
6858 return 1;
6859 }
6860
6861 /* Get the next character, maybe multibyte. */
6862 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6863 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6864 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6865 else
6866 it->c = *p, it->len = 1;
6867
6868 /* Record what we have and where it came from. */
6869 it->what = IT_CHARACTER;
6870 it->object = it->w->buffer;
6871 it->position = it->current.pos;
6872
6873 /* Normally we return the character found above, except when we
6874 really want to return an ellipsis for selective display. */
6875 if (it->selective)
6876 {
6877 if (it->c == '\n')
6878 {
6879 /* A value of selective > 0 means hide lines indented more
6880 than that number of columns. */
6881 if (it->selective > 0
6882 && IT_CHARPOS (*it) + 1 < ZV
6883 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6884 IT_BYTEPOS (*it) + 1,
6885 (double) it->selective)) /* iftc */
6886 {
6887 success_p = next_element_from_ellipsis (it);
6888 it->dpvec_char_len = -1;
6889 }
6890 }
6891 else if (it->c == '\r' && it->selective == -1)
6892 {
6893 /* A value of selective == -1 means that everything from the
6894 CR to the end of the line is invisible, with maybe an
6895 ellipsis displayed for it. */
6896 success_p = next_element_from_ellipsis (it);
6897 it->dpvec_char_len = -1;
6898 }
6899 }
6900 }
6901
6902 /* Value is zero if end of buffer reached. */
6903 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6904 return success_p;
6905 }
6906
6907
6908 /* Run the redisplay end trigger hook for IT. */
6909
6910 static void
6911 run_redisplay_end_trigger_hook (struct it *it)
6912 {
6913 Lisp_Object args[3];
6914
6915 /* IT->glyph_row should be non-null, i.e. we should be actually
6916 displaying something, or otherwise we should not run the hook. */
6917 xassert (it->glyph_row);
6918
6919 /* Set up hook arguments. */
6920 args[0] = Qredisplay_end_trigger_functions;
6921 args[1] = it->window;
6922 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6923 it->redisplay_end_trigger_charpos = 0;
6924
6925 /* Since we are *trying* to run these functions, don't try to run
6926 them again, even if they get an error. */
6927 it->w->redisplay_end_trigger = Qnil;
6928 Frun_hook_with_args (3, args);
6929
6930 /* Notice if it changed the face of the character we are on. */
6931 handle_face_prop (it);
6932 }
6933
6934
6935 /* Deliver a composition display element. Unlike the other
6936 next_element_from_XXX, this function is not registered in the array
6937 get_next_element[]. It is called from next_element_from_buffer and
6938 next_element_from_string when necessary. */
6939
6940 static int
6941 next_element_from_composition (struct it *it)
6942 {
6943 it->what = IT_COMPOSITION;
6944 it->len = it->cmp_it.nbytes;
6945 if (STRINGP (it->string))
6946 {
6947 if (it->c < 0)
6948 {
6949 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6950 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6951 return 0;
6952 }
6953 it->position = it->current.string_pos;
6954 it->object = it->string;
6955 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6956 IT_STRING_BYTEPOS (*it), it->string);
6957 }
6958 else
6959 {
6960 if (it->c < 0)
6961 {
6962 IT_CHARPOS (*it) += it->cmp_it.nchars;
6963 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6964 if (it->bidi_p)
6965 {
6966 if (it->bidi_it.new_paragraph)
6967 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6968 /* Resync the bidi iterator with IT's new position.
6969 FIXME: this doesn't support bidirectional text. */
6970 while (it->bidi_it.charpos < IT_CHARPOS (*it))
6971 bidi_move_to_visually_next (&it->bidi_it);
6972 }
6973 return 0;
6974 }
6975 it->position = it->current.pos;
6976 it->object = it->w->buffer;
6977 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6978 IT_BYTEPOS (*it), Qnil);
6979 }
6980 return 1;
6981 }
6982
6983
6984 \f
6985 /***********************************************************************
6986 Moving an iterator without producing glyphs
6987 ***********************************************************************/
6988
6989 /* Check if iterator is at a position corresponding to a valid buffer
6990 position after some move_it_ call. */
6991
6992 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6993 ((it)->method == GET_FROM_STRING \
6994 ? IT_STRING_CHARPOS (*it) == 0 \
6995 : 1)
6996
6997
6998 /* Move iterator IT to a specified buffer or X position within one
6999 line on the display without producing glyphs.
7000
7001 OP should be a bit mask including some or all of these bits:
7002 MOVE_TO_X: Stop upon reaching x-position TO_X.
7003 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7004 Regardless of OP's value, stop upon reaching the end of the display line.
7005
7006 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7007 This means, in particular, that TO_X includes window's horizontal
7008 scroll amount.
7009
7010 The return value has several possible values that
7011 say what condition caused the scan to stop:
7012
7013 MOVE_POS_MATCH_OR_ZV
7014 - when TO_POS or ZV was reached.
7015
7016 MOVE_X_REACHED
7017 -when TO_X was reached before TO_POS or ZV were reached.
7018
7019 MOVE_LINE_CONTINUED
7020 - when we reached the end of the display area and the line must
7021 be continued.
7022
7023 MOVE_LINE_TRUNCATED
7024 - when we reached the end of the display area and the line is
7025 truncated.
7026
7027 MOVE_NEWLINE_OR_CR
7028 - when we stopped at a line end, i.e. a newline or a CR and selective
7029 display is on. */
7030
7031 static enum move_it_result
7032 move_it_in_display_line_to (struct it *it,
7033 EMACS_INT to_charpos, int to_x,
7034 enum move_operation_enum op)
7035 {
7036 enum move_it_result result = MOVE_UNDEFINED;
7037 struct glyph_row *saved_glyph_row;
7038 struct it wrap_it, atpos_it, atx_it;
7039 int may_wrap = 0;
7040 enum it_method prev_method = it->method;
7041 EMACS_INT prev_pos = IT_CHARPOS (*it);
7042
7043 /* Don't produce glyphs in produce_glyphs. */
7044 saved_glyph_row = it->glyph_row;
7045 it->glyph_row = NULL;
7046
7047 /* Use wrap_it to save a copy of IT wherever a word wrap could
7048 occur. Use atpos_it to save a copy of IT at the desired buffer
7049 position, if found, so that we can scan ahead and check if the
7050 word later overshoots the window edge. Use atx_it similarly, for
7051 pixel positions. */
7052 wrap_it.sp = -1;
7053 atpos_it.sp = -1;
7054 atx_it.sp = -1;
7055
7056 #define BUFFER_POS_REACHED_P() \
7057 ((op & MOVE_TO_POS) != 0 \
7058 && BUFFERP (it->object) \
7059 && (IT_CHARPOS (*it) == to_charpos \
7060 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7061 && (it->method == GET_FROM_BUFFER \
7062 || (it->method == GET_FROM_DISPLAY_VECTOR \
7063 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7064
7065 /* If there's a line-/wrap-prefix, handle it. */
7066 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7067 && it->current_y < it->last_visible_y)
7068 handle_line_prefix (it);
7069
7070 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7071 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7072
7073 while (1)
7074 {
7075 int x, i, ascent = 0, descent = 0;
7076
7077 /* Utility macro to reset an iterator with x, ascent, and descent. */
7078 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7079 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7080 (IT)->max_descent = descent)
7081
7082 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7083 glyph). */
7084 if ((op & MOVE_TO_POS) != 0
7085 && BUFFERP (it->object)
7086 && it->method == GET_FROM_BUFFER
7087 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7088 || (it->bidi_p
7089 && (prev_method == GET_FROM_IMAGE
7090 || prev_method == GET_FROM_STRETCH)
7091 /* Passed TO_CHARPOS from left to right. */
7092 && ((prev_pos < to_charpos
7093 && IT_CHARPOS (*it) > to_charpos)
7094 /* Passed TO_CHARPOS from right to left. */
7095 || (prev_pos > to_charpos
7096 && IT_CHARPOS (*it) < to_charpos)))))
7097 {
7098 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7099 {
7100 result = MOVE_POS_MATCH_OR_ZV;
7101 break;
7102 }
7103 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7104 /* If wrap_it is valid, the current position might be in a
7105 word that is wrapped. So, save the iterator in
7106 atpos_it and continue to see if wrapping happens. */
7107 atpos_it = *it;
7108 }
7109
7110 prev_method = it->method;
7111 if (it->method == GET_FROM_BUFFER)
7112 prev_pos = IT_CHARPOS (*it);
7113 /* Stop when ZV reached.
7114 We used to stop here when TO_CHARPOS reached as well, but that is
7115 too soon if this glyph does not fit on this line. So we handle it
7116 explicitly below. */
7117 if (!get_next_display_element (it))
7118 {
7119 result = MOVE_POS_MATCH_OR_ZV;
7120 break;
7121 }
7122
7123 if (it->line_wrap == TRUNCATE)
7124 {
7125 if (BUFFER_POS_REACHED_P ())
7126 {
7127 result = MOVE_POS_MATCH_OR_ZV;
7128 break;
7129 }
7130 }
7131 else
7132 {
7133 if (it->line_wrap == WORD_WRAP)
7134 {
7135 if (IT_DISPLAYING_WHITESPACE (it))
7136 may_wrap = 1;
7137 else if (may_wrap)
7138 {
7139 /* We have reached a glyph that follows one or more
7140 whitespace characters. If the position is
7141 already found, we are done. */
7142 if (atpos_it.sp >= 0)
7143 {
7144 *it = atpos_it;
7145 result = MOVE_POS_MATCH_OR_ZV;
7146 goto done;
7147 }
7148 if (atx_it.sp >= 0)
7149 {
7150 *it = atx_it;
7151 result = MOVE_X_REACHED;
7152 goto done;
7153 }
7154 /* Otherwise, we can wrap here. */
7155 wrap_it = *it;
7156 may_wrap = 0;
7157 }
7158 }
7159 }
7160
7161 /* Remember the line height for the current line, in case
7162 the next element doesn't fit on the line. */
7163 ascent = it->max_ascent;
7164 descent = it->max_descent;
7165
7166 /* The call to produce_glyphs will get the metrics of the
7167 display element IT is loaded with. Record the x-position
7168 before this display element, in case it doesn't fit on the
7169 line. */
7170 x = it->current_x;
7171
7172 PRODUCE_GLYPHS (it);
7173
7174 if (it->area != TEXT_AREA)
7175 {
7176 set_iterator_to_next (it, 1);
7177 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7178 SET_TEXT_POS (this_line_min_pos,
7179 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7180 continue;
7181 }
7182
7183 /* The number of glyphs we get back in IT->nglyphs will normally
7184 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7185 character on a terminal frame, or (iii) a line end. For the
7186 second case, IT->nglyphs - 1 padding glyphs will be present.
7187 (On X frames, there is only one glyph produced for a
7188 composite character.)
7189
7190 The behavior implemented below means, for continuation lines,
7191 that as many spaces of a TAB as fit on the current line are
7192 displayed there. For terminal frames, as many glyphs of a
7193 multi-glyph character are displayed in the current line, too.
7194 This is what the old redisplay code did, and we keep it that
7195 way. Under X, the whole shape of a complex character must
7196 fit on the line or it will be completely displayed in the
7197 next line.
7198
7199 Note that both for tabs and padding glyphs, all glyphs have
7200 the same width. */
7201 if (it->nglyphs)
7202 {
7203 /* More than one glyph or glyph doesn't fit on line. All
7204 glyphs have the same width. */
7205 int single_glyph_width = it->pixel_width / it->nglyphs;
7206 int new_x;
7207 int x_before_this_char = x;
7208 int hpos_before_this_char = it->hpos;
7209
7210 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7211 {
7212 new_x = x + single_glyph_width;
7213
7214 /* We want to leave anything reaching TO_X to the caller. */
7215 if ((op & MOVE_TO_X) && new_x > to_x)
7216 {
7217 if (BUFFER_POS_REACHED_P ())
7218 {
7219 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7220 goto buffer_pos_reached;
7221 if (atpos_it.sp < 0)
7222 {
7223 atpos_it = *it;
7224 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7225 }
7226 }
7227 else
7228 {
7229 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7230 {
7231 it->current_x = x;
7232 result = MOVE_X_REACHED;
7233 break;
7234 }
7235 if (atx_it.sp < 0)
7236 {
7237 atx_it = *it;
7238 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7239 }
7240 }
7241 }
7242
7243 if (/* Lines are continued. */
7244 it->line_wrap != TRUNCATE
7245 && (/* And glyph doesn't fit on the line. */
7246 new_x > it->last_visible_x
7247 /* Or it fits exactly and we're on a window
7248 system frame. */
7249 || (new_x == it->last_visible_x
7250 && FRAME_WINDOW_P (it->f))))
7251 {
7252 if (/* IT->hpos == 0 means the very first glyph
7253 doesn't fit on the line, e.g. a wide image. */
7254 it->hpos == 0
7255 || (new_x == it->last_visible_x
7256 && FRAME_WINDOW_P (it->f)))
7257 {
7258 ++it->hpos;
7259 it->current_x = new_x;
7260
7261 /* The character's last glyph just barely fits
7262 in this row. */
7263 if (i == it->nglyphs - 1)
7264 {
7265 /* If this is the destination position,
7266 return a position *before* it in this row,
7267 now that we know it fits in this row. */
7268 if (BUFFER_POS_REACHED_P ())
7269 {
7270 if (it->line_wrap != WORD_WRAP
7271 || wrap_it.sp < 0)
7272 {
7273 it->hpos = hpos_before_this_char;
7274 it->current_x = x_before_this_char;
7275 result = MOVE_POS_MATCH_OR_ZV;
7276 break;
7277 }
7278 if (it->line_wrap == WORD_WRAP
7279 && atpos_it.sp < 0)
7280 {
7281 atpos_it = *it;
7282 atpos_it.current_x = x_before_this_char;
7283 atpos_it.hpos = hpos_before_this_char;
7284 }
7285 }
7286
7287 set_iterator_to_next (it, 1);
7288 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7289 SET_TEXT_POS (this_line_min_pos,
7290 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7291 /* On graphical terminals, newlines may
7292 "overflow" into the fringe if
7293 overflow-newline-into-fringe is non-nil.
7294 On text-only terminals, newlines may
7295 overflow into the last glyph on the
7296 display line.*/
7297 if (!FRAME_WINDOW_P (it->f)
7298 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7299 {
7300 if (!get_next_display_element (it))
7301 {
7302 result = MOVE_POS_MATCH_OR_ZV;
7303 break;
7304 }
7305 if (BUFFER_POS_REACHED_P ())
7306 {
7307 if (ITERATOR_AT_END_OF_LINE_P (it))
7308 result = MOVE_POS_MATCH_OR_ZV;
7309 else
7310 result = MOVE_LINE_CONTINUED;
7311 break;
7312 }
7313 if (ITERATOR_AT_END_OF_LINE_P (it))
7314 {
7315 result = MOVE_NEWLINE_OR_CR;
7316 break;
7317 }
7318 }
7319 }
7320 }
7321 else
7322 IT_RESET_X_ASCENT_DESCENT (it);
7323
7324 if (wrap_it.sp >= 0)
7325 {
7326 *it = wrap_it;
7327 atpos_it.sp = -1;
7328 atx_it.sp = -1;
7329 }
7330
7331 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7332 IT_CHARPOS (*it)));
7333 result = MOVE_LINE_CONTINUED;
7334 break;
7335 }
7336
7337 if (BUFFER_POS_REACHED_P ())
7338 {
7339 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7340 goto buffer_pos_reached;
7341 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7342 {
7343 atpos_it = *it;
7344 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7345 }
7346 }
7347
7348 if (new_x > it->first_visible_x)
7349 {
7350 /* Glyph is visible. Increment number of glyphs that
7351 would be displayed. */
7352 ++it->hpos;
7353 }
7354 }
7355
7356 if (result != MOVE_UNDEFINED)
7357 break;
7358 }
7359 else if (BUFFER_POS_REACHED_P ())
7360 {
7361 buffer_pos_reached:
7362 IT_RESET_X_ASCENT_DESCENT (it);
7363 result = MOVE_POS_MATCH_OR_ZV;
7364 break;
7365 }
7366 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7367 {
7368 /* Stop when TO_X specified and reached. This check is
7369 necessary here because of lines consisting of a line end,
7370 only. The line end will not produce any glyphs and we
7371 would never get MOVE_X_REACHED. */
7372 xassert (it->nglyphs == 0);
7373 result = MOVE_X_REACHED;
7374 break;
7375 }
7376
7377 /* Is this a line end? If yes, we're done. */
7378 if (ITERATOR_AT_END_OF_LINE_P (it))
7379 {
7380 result = MOVE_NEWLINE_OR_CR;
7381 break;
7382 }
7383
7384 if (it->method == GET_FROM_BUFFER)
7385 prev_pos = IT_CHARPOS (*it);
7386 /* The current display element has been consumed. Advance
7387 to the next. */
7388 set_iterator_to_next (it, 1);
7389 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7390 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7391
7392 /* Stop if lines are truncated and IT's current x-position is
7393 past the right edge of the window now. */
7394 if (it->line_wrap == TRUNCATE
7395 && it->current_x >= it->last_visible_x)
7396 {
7397 if (!FRAME_WINDOW_P (it->f)
7398 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7399 {
7400 if (!get_next_display_element (it)
7401 || BUFFER_POS_REACHED_P ())
7402 {
7403 result = MOVE_POS_MATCH_OR_ZV;
7404 break;
7405 }
7406 if (ITERATOR_AT_END_OF_LINE_P (it))
7407 {
7408 result = MOVE_NEWLINE_OR_CR;
7409 break;
7410 }
7411 }
7412 result = MOVE_LINE_TRUNCATED;
7413 break;
7414 }
7415 #undef IT_RESET_X_ASCENT_DESCENT
7416 }
7417
7418 #undef BUFFER_POS_REACHED_P
7419
7420 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7421 restore the saved iterator. */
7422 if (atpos_it.sp >= 0)
7423 *it = atpos_it;
7424 else if (atx_it.sp >= 0)
7425 *it = atx_it;
7426
7427 done:
7428
7429 /* Restore the iterator settings altered at the beginning of this
7430 function. */
7431 it->glyph_row = saved_glyph_row;
7432 return result;
7433 }
7434
7435 /* For external use. */
7436 void
7437 move_it_in_display_line (struct it *it,
7438 EMACS_INT to_charpos, int to_x,
7439 enum move_operation_enum op)
7440 {
7441 if (it->line_wrap == WORD_WRAP
7442 && (op & MOVE_TO_X))
7443 {
7444 struct it save_it = *it;
7445 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7446 /* When word-wrap is on, TO_X may lie past the end
7447 of a wrapped line. Then it->current is the
7448 character on the next line, so backtrack to the
7449 space before the wrap point. */
7450 if (skip == MOVE_LINE_CONTINUED)
7451 {
7452 int prev_x = max (it->current_x - 1, 0);
7453 *it = save_it;
7454 move_it_in_display_line_to
7455 (it, -1, prev_x, MOVE_TO_X);
7456 }
7457 }
7458 else
7459 move_it_in_display_line_to (it, to_charpos, to_x, op);
7460 }
7461
7462
7463 /* Move IT forward until it satisfies one or more of the criteria in
7464 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7465
7466 OP is a bit-mask that specifies where to stop, and in particular,
7467 which of those four position arguments makes a difference. See the
7468 description of enum move_operation_enum.
7469
7470 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7471 screen line, this function will set IT to the next position >
7472 TO_CHARPOS. */
7473
7474 void
7475 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7476 {
7477 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7478 int line_height, line_start_x = 0, reached = 0;
7479
7480 for (;;)
7481 {
7482 if (op & MOVE_TO_VPOS)
7483 {
7484 /* If no TO_CHARPOS and no TO_X specified, stop at the
7485 start of the line TO_VPOS. */
7486 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7487 {
7488 if (it->vpos == to_vpos)
7489 {
7490 reached = 1;
7491 break;
7492 }
7493 else
7494 skip = move_it_in_display_line_to (it, -1, -1, 0);
7495 }
7496 else
7497 {
7498 /* TO_VPOS >= 0 means stop at TO_X in the line at
7499 TO_VPOS, or at TO_POS, whichever comes first. */
7500 if (it->vpos == to_vpos)
7501 {
7502 reached = 2;
7503 break;
7504 }
7505
7506 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7507
7508 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7509 {
7510 reached = 3;
7511 break;
7512 }
7513 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7514 {
7515 /* We have reached TO_X but not in the line we want. */
7516 skip = move_it_in_display_line_to (it, to_charpos,
7517 -1, MOVE_TO_POS);
7518 if (skip == MOVE_POS_MATCH_OR_ZV)
7519 {
7520 reached = 4;
7521 break;
7522 }
7523 }
7524 }
7525 }
7526 else if (op & MOVE_TO_Y)
7527 {
7528 struct it it_backup;
7529
7530 if (it->line_wrap == WORD_WRAP)
7531 it_backup = *it;
7532
7533 /* TO_Y specified means stop at TO_X in the line containing
7534 TO_Y---or at TO_CHARPOS if this is reached first. The
7535 problem is that we can't really tell whether the line
7536 contains TO_Y before we have completely scanned it, and
7537 this may skip past TO_X. What we do is to first scan to
7538 TO_X.
7539
7540 If TO_X is not specified, use a TO_X of zero. The reason
7541 is to make the outcome of this function more predictable.
7542 If we didn't use TO_X == 0, we would stop at the end of
7543 the line which is probably not what a caller would expect
7544 to happen. */
7545 skip = move_it_in_display_line_to
7546 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7547 (MOVE_TO_X | (op & MOVE_TO_POS)));
7548
7549 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7550 if (skip == MOVE_POS_MATCH_OR_ZV)
7551 reached = 5;
7552 else if (skip == MOVE_X_REACHED)
7553 {
7554 /* If TO_X was reached, we want to know whether TO_Y is
7555 in the line. We know this is the case if the already
7556 scanned glyphs make the line tall enough. Otherwise,
7557 we must check by scanning the rest of the line. */
7558 line_height = it->max_ascent + it->max_descent;
7559 if (to_y >= it->current_y
7560 && to_y < it->current_y + line_height)
7561 {
7562 reached = 6;
7563 break;
7564 }
7565 it_backup = *it;
7566 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7567 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7568 op & MOVE_TO_POS);
7569 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7570 line_height = it->max_ascent + it->max_descent;
7571 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7572
7573 if (to_y >= it->current_y
7574 && to_y < it->current_y + line_height)
7575 {
7576 /* If TO_Y is in this line and TO_X was reached
7577 above, we scanned too far. We have to restore
7578 IT's settings to the ones before skipping. */
7579 *it = it_backup;
7580 reached = 6;
7581 }
7582 else
7583 {
7584 skip = skip2;
7585 if (skip == MOVE_POS_MATCH_OR_ZV)
7586 reached = 7;
7587 }
7588 }
7589 else
7590 {
7591 /* Check whether TO_Y is in this line. */
7592 line_height = it->max_ascent + it->max_descent;
7593 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7594
7595 if (to_y >= it->current_y
7596 && to_y < it->current_y + line_height)
7597 {
7598 /* When word-wrap is on, TO_X may lie past the end
7599 of a wrapped line. Then it->current is the
7600 character on the next line, so backtrack to the
7601 space before the wrap point. */
7602 if (skip == MOVE_LINE_CONTINUED
7603 && it->line_wrap == WORD_WRAP)
7604 {
7605 int prev_x = max (it->current_x - 1, 0);
7606 *it = it_backup;
7607 skip = move_it_in_display_line_to
7608 (it, -1, prev_x, MOVE_TO_X);
7609 }
7610 reached = 6;
7611 }
7612 }
7613
7614 if (reached)
7615 break;
7616 }
7617 else if (BUFFERP (it->object)
7618 && (it->method == GET_FROM_BUFFER
7619 || it->method == GET_FROM_STRETCH)
7620 && IT_CHARPOS (*it) >= to_charpos)
7621 skip = MOVE_POS_MATCH_OR_ZV;
7622 else
7623 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7624
7625 switch (skip)
7626 {
7627 case MOVE_POS_MATCH_OR_ZV:
7628 reached = 8;
7629 goto out;
7630
7631 case MOVE_NEWLINE_OR_CR:
7632 set_iterator_to_next (it, 1);
7633 it->continuation_lines_width = 0;
7634 break;
7635
7636 case MOVE_LINE_TRUNCATED:
7637 it->continuation_lines_width = 0;
7638 reseat_at_next_visible_line_start (it, 0);
7639 if ((op & MOVE_TO_POS) != 0
7640 && IT_CHARPOS (*it) > to_charpos)
7641 {
7642 reached = 9;
7643 goto out;
7644 }
7645 break;
7646
7647 case MOVE_LINE_CONTINUED:
7648 /* For continued lines ending in a tab, some of the glyphs
7649 associated with the tab are displayed on the current
7650 line. Since it->current_x does not include these glyphs,
7651 we use it->last_visible_x instead. */
7652 if (it->c == '\t')
7653 {
7654 it->continuation_lines_width += it->last_visible_x;
7655 /* When moving by vpos, ensure that the iterator really
7656 advances to the next line (bug#847, bug#969). Fixme:
7657 do we need to do this in other circumstances? */
7658 if (it->current_x != it->last_visible_x
7659 && (op & MOVE_TO_VPOS)
7660 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7661 {
7662 line_start_x = it->current_x + it->pixel_width
7663 - it->last_visible_x;
7664 set_iterator_to_next (it, 0);
7665 }
7666 }
7667 else
7668 it->continuation_lines_width += it->current_x;
7669 break;
7670
7671 default:
7672 abort ();
7673 }
7674
7675 /* Reset/increment for the next run. */
7676 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7677 it->current_x = line_start_x;
7678 line_start_x = 0;
7679 it->hpos = 0;
7680 it->current_y += it->max_ascent + it->max_descent;
7681 ++it->vpos;
7682 last_height = it->max_ascent + it->max_descent;
7683 last_max_ascent = it->max_ascent;
7684 it->max_ascent = it->max_descent = 0;
7685 }
7686
7687 out:
7688
7689 /* On text terminals, we may stop at the end of a line in the middle
7690 of a multi-character glyph. If the glyph itself is continued,
7691 i.e. it is actually displayed on the next line, don't treat this
7692 stopping point as valid; move to the next line instead (unless
7693 that brings us offscreen). */
7694 if (!FRAME_WINDOW_P (it->f)
7695 && op & MOVE_TO_POS
7696 && IT_CHARPOS (*it) == to_charpos
7697 && it->what == IT_CHARACTER
7698 && it->nglyphs > 1
7699 && it->line_wrap == WINDOW_WRAP
7700 && it->current_x == it->last_visible_x - 1
7701 && it->c != '\n'
7702 && it->c != '\t'
7703 && it->vpos < XFASTINT (it->w->window_end_vpos))
7704 {
7705 it->continuation_lines_width += it->current_x;
7706 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7707 it->current_y += it->max_ascent + it->max_descent;
7708 ++it->vpos;
7709 last_height = it->max_ascent + it->max_descent;
7710 last_max_ascent = it->max_ascent;
7711 }
7712
7713 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7714 }
7715
7716
7717 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7718
7719 If DY > 0, move IT backward at least that many pixels. DY = 0
7720 means move IT backward to the preceding line start or BEGV. This
7721 function may move over more than DY pixels if IT->current_y - DY
7722 ends up in the middle of a line; in this case IT->current_y will be
7723 set to the top of the line moved to. */
7724
7725 void
7726 move_it_vertically_backward (struct it *it, int dy)
7727 {
7728 int nlines, h;
7729 struct it it2, it3;
7730 EMACS_INT start_pos;
7731
7732 move_further_back:
7733 xassert (dy >= 0);
7734
7735 start_pos = IT_CHARPOS (*it);
7736
7737 /* Estimate how many newlines we must move back. */
7738 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7739
7740 /* Set the iterator's position that many lines back. */
7741 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7742 back_to_previous_visible_line_start (it);
7743
7744 /* Reseat the iterator here. When moving backward, we don't want
7745 reseat to skip forward over invisible text, set up the iterator
7746 to deliver from overlay strings at the new position etc. So,
7747 use reseat_1 here. */
7748 reseat_1 (it, it->current.pos, 1);
7749
7750 /* We are now surely at a line start. */
7751 it->current_x = it->hpos = 0;
7752 it->continuation_lines_width = 0;
7753
7754 /* Move forward and see what y-distance we moved. First move to the
7755 start of the next line so that we get its height. We need this
7756 height to be able to tell whether we reached the specified
7757 y-distance. */
7758 it2 = *it;
7759 it2.max_ascent = it2.max_descent = 0;
7760 do
7761 {
7762 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7763 MOVE_TO_POS | MOVE_TO_VPOS);
7764 }
7765 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7766 xassert (IT_CHARPOS (*it) >= BEGV);
7767 it3 = it2;
7768
7769 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7770 xassert (IT_CHARPOS (*it) >= BEGV);
7771 /* H is the actual vertical distance from the position in *IT
7772 and the starting position. */
7773 h = it2.current_y - it->current_y;
7774 /* NLINES is the distance in number of lines. */
7775 nlines = it2.vpos - it->vpos;
7776
7777 /* Correct IT's y and vpos position
7778 so that they are relative to the starting point. */
7779 it->vpos -= nlines;
7780 it->current_y -= h;
7781
7782 if (dy == 0)
7783 {
7784 /* DY == 0 means move to the start of the screen line. The
7785 value of nlines is > 0 if continuation lines were involved. */
7786 if (nlines > 0)
7787 move_it_by_lines (it, nlines);
7788 }
7789 else
7790 {
7791 /* The y-position we try to reach, relative to *IT.
7792 Note that H has been subtracted in front of the if-statement. */
7793 int target_y = it->current_y + h - dy;
7794 int y0 = it3.current_y;
7795 int y1 = line_bottom_y (&it3);
7796 int line_height = y1 - y0;
7797
7798 /* If we did not reach target_y, try to move further backward if
7799 we can. If we moved too far backward, try to move forward. */
7800 if (target_y < it->current_y
7801 /* This is heuristic. In a window that's 3 lines high, with
7802 a line height of 13 pixels each, recentering with point
7803 on the bottom line will try to move -39/2 = 19 pixels
7804 backward. Try to avoid moving into the first line. */
7805 && (it->current_y - target_y
7806 > min (window_box_height (it->w), line_height * 2 / 3))
7807 && IT_CHARPOS (*it) > BEGV)
7808 {
7809 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7810 target_y - it->current_y));
7811 dy = it->current_y - target_y;
7812 goto move_further_back;
7813 }
7814 else if (target_y >= it->current_y + line_height
7815 && IT_CHARPOS (*it) < ZV)
7816 {
7817 /* Should move forward by at least one line, maybe more.
7818
7819 Note: Calling move_it_by_lines can be expensive on
7820 terminal frames, where compute_motion is used (via
7821 vmotion) to do the job, when there are very long lines
7822 and truncate-lines is nil. That's the reason for
7823 treating terminal frames specially here. */
7824
7825 if (!FRAME_WINDOW_P (it->f))
7826 move_it_vertically (it, target_y - (it->current_y + line_height));
7827 else
7828 {
7829 do
7830 {
7831 move_it_by_lines (it, 1);
7832 }
7833 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7834 }
7835 }
7836 }
7837 }
7838
7839
7840 /* Move IT by a specified amount of pixel lines DY. DY negative means
7841 move backwards. DY = 0 means move to start of screen line. At the
7842 end, IT will be on the start of a screen line. */
7843
7844 void
7845 move_it_vertically (struct it *it, int dy)
7846 {
7847 if (dy <= 0)
7848 move_it_vertically_backward (it, -dy);
7849 else
7850 {
7851 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7852 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7853 MOVE_TO_POS | MOVE_TO_Y);
7854 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7855
7856 /* If buffer ends in ZV without a newline, move to the start of
7857 the line to satisfy the post-condition. */
7858 if (IT_CHARPOS (*it) == ZV
7859 && ZV > BEGV
7860 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7861 move_it_by_lines (it, 0);
7862 }
7863 }
7864
7865
7866 /* Move iterator IT past the end of the text line it is in. */
7867
7868 void
7869 move_it_past_eol (struct it *it)
7870 {
7871 enum move_it_result rc;
7872
7873 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7874 if (rc == MOVE_NEWLINE_OR_CR)
7875 set_iterator_to_next (it, 0);
7876 }
7877
7878
7879 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7880 negative means move up. DVPOS == 0 means move to the start of the
7881 screen line.
7882
7883 Optimization idea: If we would know that IT->f doesn't use
7884 a face with proportional font, we could be faster for
7885 truncate-lines nil. */
7886
7887 void
7888 move_it_by_lines (struct it *it, int dvpos)
7889 {
7890
7891 /* The commented-out optimization uses vmotion on terminals. This
7892 gives bad results, because elements like it->what, on which
7893 callers such as pos_visible_p rely, aren't updated. */
7894 /* struct position pos;
7895 if (!FRAME_WINDOW_P (it->f))
7896 {
7897 struct text_pos textpos;
7898
7899 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7900 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7901 reseat (it, textpos, 1);
7902 it->vpos += pos.vpos;
7903 it->current_y += pos.vpos;
7904 }
7905 else */
7906
7907 if (dvpos == 0)
7908 {
7909 /* DVPOS == 0 means move to the start of the screen line. */
7910 move_it_vertically_backward (it, 0);
7911 xassert (it->current_x == 0 && it->hpos == 0);
7912 /* Let next call to line_bottom_y calculate real line height */
7913 last_height = 0;
7914 }
7915 else if (dvpos > 0)
7916 {
7917 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7918 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7919 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7920 }
7921 else
7922 {
7923 struct it it2;
7924 EMACS_INT start_charpos, i;
7925
7926 /* Start at the beginning of the screen line containing IT's
7927 position. This may actually move vertically backwards,
7928 in case of overlays, so adjust dvpos accordingly. */
7929 dvpos += it->vpos;
7930 move_it_vertically_backward (it, 0);
7931 dvpos -= it->vpos;
7932
7933 /* Go back -DVPOS visible lines and reseat the iterator there. */
7934 start_charpos = IT_CHARPOS (*it);
7935 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7936 back_to_previous_visible_line_start (it);
7937 reseat (it, it->current.pos, 1);
7938
7939 /* Move further back if we end up in a string or an image. */
7940 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7941 {
7942 /* First try to move to start of display line. */
7943 dvpos += it->vpos;
7944 move_it_vertically_backward (it, 0);
7945 dvpos -= it->vpos;
7946 if (IT_POS_VALID_AFTER_MOVE_P (it))
7947 break;
7948 /* If start of line is still in string or image,
7949 move further back. */
7950 back_to_previous_visible_line_start (it);
7951 reseat (it, it->current.pos, 1);
7952 dvpos--;
7953 }
7954
7955 it->current_x = it->hpos = 0;
7956
7957 /* Above call may have moved too far if continuation lines
7958 are involved. Scan forward and see if it did. */
7959 it2 = *it;
7960 it2.vpos = it2.current_y = 0;
7961 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7962 it->vpos -= it2.vpos;
7963 it->current_y -= it2.current_y;
7964 it->current_x = it->hpos = 0;
7965
7966 /* If we moved too far back, move IT some lines forward. */
7967 if (it2.vpos > -dvpos)
7968 {
7969 int delta = it2.vpos + dvpos;
7970 it2 = *it;
7971 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7972 /* Move back again if we got too far ahead. */
7973 if (IT_CHARPOS (*it) >= start_charpos)
7974 *it = it2;
7975 }
7976 }
7977 }
7978
7979 /* Return 1 if IT points into the middle of a display vector. */
7980
7981 int
7982 in_display_vector_p (struct it *it)
7983 {
7984 return (it->method == GET_FROM_DISPLAY_VECTOR
7985 && it->current.dpvec_index > 0
7986 && it->dpvec + it->current.dpvec_index != it->dpend);
7987 }
7988
7989 \f
7990 /***********************************************************************
7991 Messages
7992 ***********************************************************************/
7993
7994
7995 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7996 to *Messages*. */
7997
7998 void
7999 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8000 {
8001 Lisp_Object args[3];
8002 Lisp_Object msg, fmt;
8003 char *buffer;
8004 EMACS_INT len;
8005 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8006 USE_SAFE_ALLOCA;
8007
8008 /* Do nothing if called asynchronously. Inserting text into
8009 a buffer may call after-change-functions and alike and
8010 that would means running Lisp asynchronously. */
8011 if (handling_signal)
8012 return;
8013
8014 fmt = msg = Qnil;
8015 GCPRO4 (fmt, msg, arg1, arg2);
8016
8017 args[0] = fmt = build_string (format);
8018 args[1] = arg1;
8019 args[2] = arg2;
8020 msg = Fformat (3, args);
8021
8022 len = SBYTES (msg) + 1;
8023 SAFE_ALLOCA (buffer, char *, len);
8024 memcpy (buffer, SDATA (msg), len);
8025
8026 message_dolog (buffer, len - 1, 1, 0);
8027 SAFE_FREE ();
8028
8029 UNGCPRO;
8030 }
8031
8032
8033 /* Output a newline in the *Messages* buffer if "needs" one. */
8034
8035 void
8036 message_log_maybe_newline (void)
8037 {
8038 if (message_log_need_newline)
8039 message_dolog ("", 0, 1, 0);
8040 }
8041
8042
8043 /* Add a string M of length NBYTES to the message log, optionally
8044 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8045 nonzero, means interpret the contents of M as multibyte. This
8046 function calls low-level routines in order to bypass text property
8047 hooks, etc. which might not be safe to run.
8048
8049 This may GC (insert may run before/after change hooks),
8050 so the buffer M must NOT point to a Lisp string. */
8051
8052 void
8053 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8054 {
8055 const unsigned char *msg = (const unsigned char *) m;
8056
8057 if (!NILP (Vmemory_full))
8058 return;
8059
8060 if (!NILP (Vmessage_log_max))
8061 {
8062 struct buffer *oldbuf;
8063 Lisp_Object oldpoint, oldbegv, oldzv;
8064 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8065 EMACS_INT point_at_end = 0;
8066 EMACS_INT zv_at_end = 0;
8067 Lisp_Object old_deactivate_mark, tem;
8068 struct gcpro gcpro1;
8069
8070 old_deactivate_mark = Vdeactivate_mark;
8071 oldbuf = current_buffer;
8072 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8073 BVAR (current_buffer, undo_list) = Qt;
8074
8075 oldpoint = message_dolog_marker1;
8076 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8077 oldbegv = message_dolog_marker2;
8078 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8079 oldzv = message_dolog_marker3;
8080 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8081 GCPRO1 (old_deactivate_mark);
8082
8083 if (PT == Z)
8084 point_at_end = 1;
8085 if (ZV == Z)
8086 zv_at_end = 1;
8087
8088 BEGV = BEG;
8089 BEGV_BYTE = BEG_BYTE;
8090 ZV = Z;
8091 ZV_BYTE = Z_BYTE;
8092 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8093
8094 /* Insert the string--maybe converting multibyte to single byte
8095 or vice versa, so that all the text fits the buffer. */
8096 if (multibyte
8097 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8098 {
8099 EMACS_INT i;
8100 int c, char_bytes;
8101 char work[1];
8102
8103 /* Convert a multibyte string to single-byte
8104 for the *Message* buffer. */
8105 for (i = 0; i < nbytes; i += char_bytes)
8106 {
8107 c = string_char_and_length (msg + i, &char_bytes);
8108 work[0] = (ASCII_CHAR_P (c)
8109 ? c
8110 : multibyte_char_to_unibyte (c));
8111 insert_1_both (work, 1, 1, 1, 0, 0);
8112 }
8113 }
8114 else if (! multibyte
8115 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8116 {
8117 EMACS_INT i;
8118 int c, char_bytes;
8119 unsigned char str[MAX_MULTIBYTE_LENGTH];
8120 /* Convert a single-byte string to multibyte
8121 for the *Message* buffer. */
8122 for (i = 0; i < nbytes; i++)
8123 {
8124 c = msg[i];
8125 MAKE_CHAR_MULTIBYTE (c);
8126 char_bytes = CHAR_STRING (c, str);
8127 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8128 }
8129 }
8130 else if (nbytes)
8131 insert_1 (m, nbytes, 1, 0, 0);
8132
8133 if (nlflag)
8134 {
8135 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8136 unsigned long int dups;
8137 insert_1 ("\n", 1, 1, 0, 0);
8138
8139 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8140 this_bol = PT;
8141 this_bol_byte = PT_BYTE;
8142
8143 /* See if this line duplicates the previous one.
8144 If so, combine duplicates. */
8145 if (this_bol > BEG)
8146 {
8147 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8148 prev_bol = PT;
8149 prev_bol_byte = PT_BYTE;
8150
8151 dups = message_log_check_duplicate (prev_bol_byte,
8152 this_bol_byte);
8153 if (dups)
8154 {
8155 del_range_both (prev_bol, prev_bol_byte,
8156 this_bol, this_bol_byte, 0);
8157 if (dups > 1)
8158 {
8159 char dupstr[40];
8160 int duplen;
8161
8162 /* If you change this format, don't forget to also
8163 change message_log_check_duplicate. */
8164 sprintf (dupstr, " [%lu times]", dups);
8165 duplen = strlen (dupstr);
8166 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8167 insert_1 (dupstr, duplen, 1, 0, 1);
8168 }
8169 }
8170 }
8171
8172 /* If we have more than the desired maximum number of lines
8173 in the *Messages* buffer now, delete the oldest ones.
8174 This is safe because we don't have undo in this buffer. */
8175
8176 if (NATNUMP (Vmessage_log_max))
8177 {
8178 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8179 -XFASTINT (Vmessage_log_max) - 1, 0);
8180 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8181 }
8182 }
8183 BEGV = XMARKER (oldbegv)->charpos;
8184 BEGV_BYTE = marker_byte_position (oldbegv);
8185
8186 if (zv_at_end)
8187 {
8188 ZV = Z;
8189 ZV_BYTE = Z_BYTE;
8190 }
8191 else
8192 {
8193 ZV = XMARKER (oldzv)->charpos;
8194 ZV_BYTE = marker_byte_position (oldzv);
8195 }
8196
8197 if (point_at_end)
8198 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8199 else
8200 /* We can't do Fgoto_char (oldpoint) because it will run some
8201 Lisp code. */
8202 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8203 XMARKER (oldpoint)->bytepos);
8204
8205 UNGCPRO;
8206 unchain_marker (XMARKER (oldpoint));
8207 unchain_marker (XMARKER (oldbegv));
8208 unchain_marker (XMARKER (oldzv));
8209
8210 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8211 set_buffer_internal (oldbuf);
8212 if (NILP (tem))
8213 windows_or_buffers_changed = old_windows_or_buffers_changed;
8214 message_log_need_newline = !nlflag;
8215 Vdeactivate_mark = old_deactivate_mark;
8216 }
8217 }
8218
8219
8220 /* We are at the end of the buffer after just having inserted a newline.
8221 (Note: We depend on the fact we won't be crossing the gap.)
8222 Check to see if the most recent message looks a lot like the previous one.
8223 Return 0 if different, 1 if the new one should just replace it, or a
8224 value N > 1 if we should also append " [N times]". */
8225
8226 static unsigned long int
8227 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8228 {
8229 EMACS_INT i;
8230 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8231 int seen_dots = 0;
8232 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8233 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8234
8235 for (i = 0; i < len; i++)
8236 {
8237 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8238 seen_dots = 1;
8239 if (p1[i] != p2[i])
8240 return seen_dots;
8241 }
8242 p1 += len;
8243 if (*p1 == '\n')
8244 return 2;
8245 if (*p1++ == ' ' && *p1++ == '[')
8246 {
8247 char *pend;
8248 unsigned long int n = strtoul ((char *) p1, &pend, 10);
8249 if (strncmp (pend, " times]\n", 8) == 0)
8250 return n+1;
8251 }
8252 return 0;
8253 }
8254 \f
8255
8256 /* Display an echo area message M with a specified length of NBYTES
8257 bytes. The string may include null characters. If M is 0, clear
8258 out any existing message, and let the mini-buffer text show
8259 through.
8260
8261 This may GC, so the buffer M must NOT point to a Lisp string. */
8262
8263 void
8264 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8265 {
8266 /* First flush out any partial line written with print. */
8267 message_log_maybe_newline ();
8268 if (m)
8269 message_dolog (m, nbytes, 1, multibyte);
8270 message2_nolog (m, nbytes, multibyte);
8271 }
8272
8273
8274 /* The non-logging counterpart of message2. */
8275
8276 void
8277 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8278 {
8279 struct frame *sf = SELECTED_FRAME ();
8280 message_enable_multibyte = multibyte;
8281
8282 if (FRAME_INITIAL_P (sf))
8283 {
8284 if (noninteractive_need_newline)
8285 putc ('\n', stderr);
8286 noninteractive_need_newline = 0;
8287 if (m)
8288 fwrite (m, nbytes, 1, stderr);
8289 if (cursor_in_echo_area == 0)
8290 fprintf (stderr, "\n");
8291 fflush (stderr);
8292 }
8293 /* A null message buffer means that the frame hasn't really been
8294 initialized yet. Error messages get reported properly by
8295 cmd_error, so this must be just an informative message; toss it. */
8296 else if (INTERACTIVE
8297 && sf->glyphs_initialized_p
8298 && FRAME_MESSAGE_BUF (sf))
8299 {
8300 Lisp_Object mini_window;
8301 struct frame *f;
8302
8303 /* Get the frame containing the mini-buffer
8304 that the selected frame is using. */
8305 mini_window = FRAME_MINIBUF_WINDOW (sf);
8306 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8307
8308 FRAME_SAMPLE_VISIBILITY (f);
8309 if (FRAME_VISIBLE_P (sf)
8310 && ! FRAME_VISIBLE_P (f))
8311 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8312
8313 if (m)
8314 {
8315 set_message (m, Qnil, nbytes, multibyte);
8316 if (minibuffer_auto_raise)
8317 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8318 }
8319 else
8320 clear_message (1, 1);
8321
8322 do_pending_window_change (0);
8323 echo_area_display (1);
8324 do_pending_window_change (0);
8325 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8326 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8327 }
8328 }
8329
8330
8331 /* Display an echo area message M with a specified length of NBYTES
8332 bytes. The string may include null characters. If M is not a
8333 string, clear out any existing message, and let the mini-buffer
8334 text show through.
8335
8336 This function cancels echoing. */
8337
8338 void
8339 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8340 {
8341 struct gcpro gcpro1;
8342
8343 GCPRO1 (m);
8344 clear_message (1,1);
8345 cancel_echoing ();
8346
8347 /* First flush out any partial line written with print. */
8348 message_log_maybe_newline ();
8349 if (STRINGP (m))
8350 {
8351 char *buffer;
8352 USE_SAFE_ALLOCA;
8353
8354 SAFE_ALLOCA (buffer, char *, nbytes);
8355 memcpy (buffer, SDATA (m), nbytes);
8356 message_dolog (buffer, nbytes, 1, multibyte);
8357 SAFE_FREE ();
8358 }
8359 message3_nolog (m, nbytes, multibyte);
8360
8361 UNGCPRO;
8362 }
8363
8364
8365 /* The non-logging version of message3.
8366 This does not cancel echoing, because it is used for echoing.
8367 Perhaps we need to make a separate function for echoing
8368 and make this cancel echoing. */
8369
8370 void
8371 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8372 {
8373 struct frame *sf = SELECTED_FRAME ();
8374 message_enable_multibyte = multibyte;
8375
8376 if (FRAME_INITIAL_P (sf))
8377 {
8378 if (noninteractive_need_newline)
8379 putc ('\n', stderr);
8380 noninteractive_need_newline = 0;
8381 if (STRINGP (m))
8382 fwrite (SDATA (m), nbytes, 1, stderr);
8383 if (cursor_in_echo_area == 0)
8384 fprintf (stderr, "\n");
8385 fflush (stderr);
8386 }
8387 /* A null message buffer means that the frame hasn't really been
8388 initialized yet. Error messages get reported properly by
8389 cmd_error, so this must be just an informative message; toss it. */
8390 else if (INTERACTIVE
8391 && sf->glyphs_initialized_p
8392 && FRAME_MESSAGE_BUF (sf))
8393 {
8394 Lisp_Object mini_window;
8395 Lisp_Object frame;
8396 struct frame *f;
8397
8398 /* Get the frame containing the mini-buffer
8399 that the selected frame is using. */
8400 mini_window = FRAME_MINIBUF_WINDOW (sf);
8401 frame = XWINDOW (mini_window)->frame;
8402 f = XFRAME (frame);
8403
8404 FRAME_SAMPLE_VISIBILITY (f);
8405 if (FRAME_VISIBLE_P (sf)
8406 && !FRAME_VISIBLE_P (f))
8407 Fmake_frame_visible (frame);
8408
8409 if (STRINGP (m) && SCHARS (m) > 0)
8410 {
8411 set_message (NULL, m, nbytes, multibyte);
8412 if (minibuffer_auto_raise)
8413 Fraise_frame (frame);
8414 /* Assume we are not echoing.
8415 (If we are, echo_now will override this.) */
8416 echo_message_buffer = Qnil;
8417 }
8418 else
8419 clear_message (1, 1);
8420
8421 do_pending_window_change (0);
8422 echo_area_display (1);
8423 do_pending_window_change (0);
8424 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8425 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8426 }
8427 }
8428
8429
8430 /* Display a null-terminated echo area message M. If M is 0, clear
8431 out any existing message, and let the mini-buffer text show through.
8432
8433 The buffer M must continue to exist until after the echo area gets
8434 cleared or some other message gets displayed there. Do not pass
8435 text that is stored in a Lisp string. Do not pass text in a buffer
8436 that was alloca'd. */
8437
8438 void
8439 message1 (const char *m)
8440 {
8441 message2 (m, (m ? strlen (m) : 0), 0);
8442 }
8443
8444
8445 /* The non-logging counterpart of message1. */
8446
8447 void
8448 message1_nolog (const char *m)
8449 {
8450 message2_nolog (m, (m ? strlen (m) : 0), 0);
8451 }
8452
8453 /* Display a message M which contains a single %s
8454 which gets replaced with STRING. */
8455
8456 void
8457 message_with_string (const char *m, Lisp_Object string, int log)
8458 {
8459 CHECK_STRING (string);
8460
8461 if (noninteractive)
8462 {
8463 if (m)
8464 {
8465 if (noninteractive_need_newline)
8466 putc ('\n', stderr);
8467 noninteractive_need_newline = 0;
8468 fprintf (stderr, m, SDATA (string));
8469 if (!cursor_in_echo_area)
8470 fprintf (stderr, "\n");
8471 fflush (stderr);
8472 }
8473 }
8474 else if (INTERACTIVE)
8475 {
8476 /* The frame whose minibuffer we're going to display the message on.
8477 It may be larger than the selected frame, so we need
8478 to use its buffer, not the selected frame's buffer. */
8479 Lisp_Object mini_window;
8480 struct frame *f, *sf = SELECTED_FRAME ();
8481
8482 /* Get the frame containing the minibuffer
8483 that the selected frame is using. */
8484 mini_window = FRAME_MINIBUF_WINDOW (sf);
8485 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8486
8487 /* A null message buffer means that the frame hasn't really been
8488 initialized yet. Error messages get reported properly by
8489 cmd_error, so this must be just an informative message; toss it. */
8490 if (FRAME_MESSAGE_BUF (f))
8491 {
8492 Lisp_Object args[2], msg;
8493 struct gcpro gcpro1, gcpro2;
8494
8495 args[0] = build_string (m);
8496 args[1] = msg = string;
8497 GCPRO2 (args[0], msg);
8498 gcpro1.nvars = 2;
8499
8500 msg = Fformat (2, args);
8501
8502 if (log)
8503 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8504 else
8505 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8506
8507 UNGCPRO;
8508
8509 /* Print should start at the beginning of the message
8510 buffer next time. */
8511 message_buf_print = 0;
8512 }
8513 }
8514 }
8515
8516
8517 /* Dump an informative message to the minibuf. If M is 0, clear out
8518 any existing message, and let the mini-buffer text show through. */
8519
8520 static void
8521 vmessage (const char *m, va_list ap)
8522 {
8523 if (noninteractive)
8524 {
8525 if (m)
8526 {
8527 if (noninteractive_need_newline)
8528 putc ('\n', stderr);
8529 noninteractive_need_newline = 0;
8530 vfprintf (stderr, m, ap);
8531 if (cursor_in_echo_area == 0)
8532 fprintf (stderr, "\n");
8533 fflush (stderr);
8534 }
8535 }
8536 else if (INTERACTIVE)
8537 {
8538 /* The frame whose mini-buffer we're going to display the message
8539 on. It may be larger than the selected frame, so we need to
8540 use its buffer, not the selected frame's buffer. */
8541 Lisp_Object mini_window;
8542 struct frame *f, *sf = SELECTED_FRAME ();
8543
8544 /* Get the frame containing the mini-buffer
8545 that the selected frame is using. */
8546 mini_window = FRAME_MINIBUF_WINDOW (sf);
8547 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8548
8549 /* A null message buffer means that the frame hasn't really been
8550 initialized yet. Error messages get reported properly by
8551 cmd_error, so this must be just an informative message; toss
8552 it. */
8553 if (FRAME_MESSAGE_BUF (f))
8554 {
8555 if (m)
8556 {
8557 size_t len;
8558
8559 len = doprnt (FRAME_MESSAGE_BUF (f),
8560 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8561
8562 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8563 }
8564 else
8565 message1 (0);
8566
8567 /* Print should start at the beginning of the message
8568 buffer next time. */
8569 message_buf_print = 0;
8570 }
8571 }
8572 }
8573
8574 void
8575 message (const char *m, ...)
8576 {
8577 va_list ap;
8578 va_start (ap, m);
8579 vmessage (m, ap);
8580 va_end (ap);
8581 }
8582
8583
8584 #if 0
8585 /* The non-logging version of message. */
8586
8587 void
8588 message_nolog (const char *m, ...)
8589 {
8590 Lisp_Object old_log_max;
8591 va_list ap;
8592 va_start (ap, m);
8593 old_log_max = Vmessage_log_max;
8594 Vmessage_log_max = Qnil;
8595 vmessage (m, ap);
8596 Vmessage_log_max = old_log_max;
8597 va_end (ap);
8598 }
8599 #endif
8600
8601
8602 /* Display the current message in the current mini-buffer. This is
8603 only called from error handlers in process.c, and is not time
8604 critical. */
8605
8606 void
8607 update_echo_area (void)
8608 {
8609 if (!NILP (echo_area_buffer[0]))
8610 {
8611 Lisp_Object string;
8612 string = Fcurrent_message ();
8613 message3 (string, SBYTES (string),
8614 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
8615 }
8616 }
8617
8618
8619 /* Make sure echo area buffers in `echo_buffers' are live.
8620 If they aren't, make new ones. */
8621
8622 static void
8623 ensure_echo_area_buffers (void)
8624 {
8625 int i;
8626
8627 for (i = 0; i < 2; ++i)
8628 if (!BUFFERP (echo_buffer[i])
8629 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
8630 {
8631 char name[30];
8632 Lisp_Object old_buffer;
8633 int j;
8634
8635 old_buffer = echo_buffer[i];
8636 sprintf (name, " *Echo Area %d*", i);
8637 echo_buffer[i] = Fget_buffer_create (build_string (name));
8638 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
8639 /* to force word wrap in echo area -
8640 it was decided to postpone this*/
8641 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8642
8643 for (j = 0; j < 2; ++j)
8644 if (EQ (old_buffer, echo_area_buffer[j]))
8645 echo_area_buffer[j] = echo_buffer[i];
8646 }
8647 }
8648
8649
8650 /* Call FN with args A1..A4 with either the current or last displayed
8651 echo_area_buffer as current buffer.
8652
8653 WHICH zero means use the current message buffer
8654 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8655 from echo_buffer[] and clear it.
8656
8657 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8658 suitable buffer from echo_buffer[] and clear it.
8659
8660 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8661 that the current message becomes the last displayed one, make
8662 choose a suitable buffer for echo_area_buffer[0], and clear it.
8663
8664 Value is what FN returns. */
8665
8666 static int
8667 with_echo_area_buffer (struct window *w, int which,
8668 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8669 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8670 {
8671 Lisp_Object buffer;
8672 int this_one, the_other, clear_buffer_p, rc;
8673 int count = SPECPDL_INDEX ();
8674
8675 /* If buffers aren't live, make new ones. */
8676 ensure_echo_area_buffers ();
8677
8678 clear_buffer_p = 0;
8679
8680 if (which == 0)
8681 this_one = 0, the_other = 1;
8682 else if (which > 0)
8683 this_one = 1, the_other = 0;
8684 else
8685 {
8686 this_one = 0, the_other = 1;
8687 clear_buffer_p = 1;
8688
8689 /* We need a fresh one in case the current echo buffer equals
8690 the one containing the last displayed echo area message. */
8691 if (!NILP (echo_area_buffer[this_one])
8692 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8693 echo_area_buffer[this_one] = Qnil;
8694 }
8695
8696 /* Choose a suitable buffer from echo_buffer[] is we don't
8697 have one. */
8698 if (NILP (echo_area_buffer[this_one]))
8699 {
8700 echo_area_buffer[this_one]
8701 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8702 ? echo_buffer[the_other]
8703 : echo_buffer[this_one]);
8704 clear_buffer_p = 1;
8705 }
8706
8707 buffer = echo_area_buffer[this_one];
8708
8709 /* Don't get confused by reusing the buffer used for echoing
8710 for a different purpose. */
8711 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8712 cancel_echoing ();
8713
8714 record_unwind_protect (unwind_with_echo_area_buffer,
8715 with_echo_area_buffer_unwind_data (w));
8716
8717 /* Make the echo area buffer current. Note that for display
8718 purposes, it is not necessary that the displayed window's buffer
8719 == current_buffer, except for text property lookup. So, let's
8720 only set that buffer temporarily here without doing a full
8721 Fset_window_buffer. We must also change w->pointm, though,
8722 because otherwise an assertions in unshow_buffer fails, and Emacs
8723 aborts. */
8724 set_buffer_internal_1 (XBUFFER (buffer));
8725 if (w)
8726 {
8727 w->buffer = buffer;
8728 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8729 }
8730
8731 BVAR (current_buffer, undo_list) = Qt;
8732 BVAR (current_buffer, read_only) = Qnil;
8733 specbind (Qinhibit_read_only, Qt);
8734 specbind (Qinhibit_modification_hooks, Qt);
8735
8736 if (clear_buffer_p && Z > BEG)
8737 del_range (BEG, Z);
8738
8739 xassert (BEGV >= BEG);
8740 xassert (ZV <= Z && ZV >= BEGV);
8741
8742 rc = fn (a1, a2, a3, a4);
8743
8744 xassert (BEGV >= BEG);
8745 xassert (ZV <= Z && ZV >= BEGV);
8746
8747 unbind_to (count, Qnil);
8748 return rc;
8749 }
8750
8751
8752 /* Save state that should be preserved around the call to the function
8753 FN called in with_echo_area_buffer. */
8754
8755 static Lisp_Object
8756 with_echo_area_buffer_unwind_data (struct window *w)
8757 {
8758 int i = 0;
8759 Lisp_Object vector, tmp;
8760
8761 /* Reduce consing by keeping one vector in
8762 Vwith_echo_area_save_vector. */
8763 vector = Vwith_echo_area_save_vector;
8764 Vwith_echo_area_save_vector = Qnil;
8765
8766 if (NILP (vector))
8767 vector = Fmake_vector (make_number (7), Qnil);
8768
8769 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8770 ASET (vector, i, Vdeactivate_mark); ++i;
8771 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8772
8773 if (w)
8774 {
8775 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8776 ASET (vector, i, w->buffer); ++i;
8777 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8778 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8779 }
8780 else
8781 {
8782 int end = i + 4;
8783 for (; i < end; ++i)
8784 ASET (vector, i, Qnil);
8785 }
8786
8787 xassert (i == ASIZE (vector));
8788 return vector;
8789 }
8790
8791
8792 /* Restore global state from VECTOR which was created by
8793 with_echo_area_buffer_unwind_data. */
8794
8795 static Lisp_Object
8796 unwind_with_echo_area_buffer (Lisp_Object vector)
8797 {
8798 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8799 Vdeactivate_mark = AREF (vector, 1);
8800 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8801
8802 if (WINDOWP (AREF (vector, 3)))
8803 {
8804 struct window *w;
8805 Lisp_Object buffer, charpos, bytepos;
8806
8807 w = XWINDOW (AREF (vector, 3));
8808 buffer = AREF (vector, 4);
8809 charpos = AREF (vector, 5);
8810 bytepos = AREF (vector, 6);
8811
8812 w->buffer = buffer;
8813 set_marker_both (w->pointm, buffer,
8814 XFASTINT (charpos), XFASTINT (bytepos));
8815 }
8816
8817 Vwith_echo_area_save_vector = vector;
8818 return Qnil;
8819 }
8820
8821
8822 /* Set up the echo area for use by print functions. MULTIBYTE_P
8823 non-zero means we will print multibyte. */
8824
8825 void
8826 setup_echo_area_for_printing (int multibyte_p)
8827 {
8828 /* If we can't find an echo area any more, exit. */
8829 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8830 Fkill_emacs (Qnil);
8831
8832 ensure_echo_area_buffers ();
8833
8834 if (!message_buf_print)
8835 {
8836 /* A message has been output since the last time we printed.
8837 Choose a fresh echo area buffer. */
8838 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8839 echo_area_buffer[0] = echo_buffer[1];
8840 else
8841 echo_area_buffer[0] = echo_buffer[0];
8842
8843 /* Switch to that buffer and clear it. */
8844 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8845 BVAR (current_buffer, truncate_lines) = Qnil;
8846
8847 if (Z > BEG)
8848 {
8849 int count = SPECPDL_INDEX ();
8850 specbind (Qinhibit_read_only, Qt);
8851 /* Note that undo recording is always disabled. */
8852 del_range (BEG, Z);
8853 unbind_to (count, Qnil);
8854 }
8855 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8856
8857 /* Set up the buffer for the multibyteness we need. */
8858 if (multibyte_p
8859 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
8860 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8861
8862 /* Raise the frame containing the echo area. */
8863 if (minibuffer_auto_raise)
8864 {
8865 struct frame *sf = SELECTED_FRAME ();
8866 Lisp_Object mini_window;
8867 mini_window = FRAME_MINIBUF_WINDOW (sf);
8868 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8869 }
8870
8871 message_log_maybe_newline ();
8872 message_buf_print = 1;
8873 }
8874 else
8875 {
8876 if (NILP (echo_area_buffer[0]))
8877 {
8878 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8879 echo_area_buffer[0] = echo_buffer[1];
8880 else
8881 echo_area_buffer[0] = echo_buffer[0];
8882 }
8883
8884 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8885 {
8886 /* Someone switched buffers between print requests. */
8887 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8888 BVAR (current_buffer, truncate_lines) = Qnil;
8889 }
8890 }
8891 }
8892
8893
8894 /* Display an echo area message in window W. Value is non-zero if W's
8895 height is changed. If display_last_displayed_message_p is
8896 non-zero, display the message that was last displayed, otherwise
8897 display the current message. */
8898
8899 static int
8900 display_echo_area (struct window *w)
8901 {
8902 int i, no_message_p, window_height_changed_p, count;
8903
8904 /* Temporarily disable garbage collections while displaying the echo
8905 area. This is done because a GC can print a message itself.
8906 That message would modify the echo area buffer's contents while a
8907 redisplay of the buffer is going on, and seriously confuse
8908 redisplay. */
8909 count = inhibit_garbage_collection ();
8910
8911 /* If there is no message, we must call display_echo_area_1
8912 nevertheless because it resizes the window. But we will have to
8913 reset the echo_area_buffer in question to nil at the end because
8914 with_echo_area_buffer will sets it to an empty buffer. */
8915 i = display_last_displayed_message_p ? 1 : 0;
8916 no_message_p = NILP (echo_area_buffer[i]);
8917
8918 window_height_changed_p
8919 = with_echo_area_buffer (w, display_last_displayed_message_p,
8920 display_echo_area_1,
8921 (intptr_t) w, Qnil, 0, 0);
8922
8923 if (no_message_p)
8924 echo_area_buffer[i] = Qnil;
8925
8926 unbind_to (count, Qnil);
8927 return window_height_changed_p;
8928 }
8929
8930
8931 /* Helper for display_echo_area. Display the current buffer which
8932 contains the current echo area message in window W, a mini-window,
8933 a pointer to which is passed in A1. A2..A4 are currently not used.
8934 Change the height of W so that all of the message is displayed.
8935 Value is non-zero if height of W was changed. */
8936
8937 static int
8938 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8939 {
8940 intptr_t i1 = a1;
8941 struct window *w = (struct window *) i1;
8942 Lisp_Object window;
8943 struct text_pos start;
8944 int window_height_changed_p = 0;
8945
8946 /* Do this before displaying, so that we have a large enough glyph
8947 matrix for the display. If we can't get enough space for the
8948 whole text, display the last N lines. That works by setting w->start. */
8949 window_height_changed_p = resize_mini_window (w, 0);
8950
8951 /* Use the starting position chosen by resize_mini_window. */
8952 SET_TEXT_POS_FROM_MARKER (start, w->start);
8953
8954 /* Display. */
8955 clear_glyph_matrix (w->desired_matrix);
8956 XSETWINDOW (window, w);
8957 try_window (window, start, 0);
8958
8959 return window_height_changed_p;
8960 }
8961
8962
8963 /* Resize the echo area window to exactly the size needed for the
8964 currently displayed message, if there is one. If a mini-buffer
8965 is active, don't shrink it. */
8966
8967 void
8968 resize_echo_area_exactly (void)
8969 {
8970 if (BUFFERP (echo_area_buffer[0])
8971 && WINDOWP (echo_area_window))
8972 {
8973 struct window *w = XWINDOW (echo_area_window);
8974 int resized_p;
8975 Lisp_Object resize_exactly;
8976
8977 if (minibuf_level == 0)
8978 resize_exactly = Qt;
8979 else
8980 resize_exactly = Qnil;
8981
8982 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8983 (intptr_t) w, resize_exactly,
8984 0, 0);
8985 if (resized_p)
8986 {
8987 ++windows_or_buffers_changed;
8988 ++update_mode_lines;
8989 redisplay_internal ();
8990 }
8991 }
8992 }
8993
8994
8995 /* Callback function for with_echo_area_buffer, when used from
8996 resize_echo_area_exactly. A1 contains a pointer to the window to
8997 resize, EXACTLY non-nil means resize the mini-window exactly to the
8998 size of the text displayed. A3 and A4 are not used. Value is what
8999 resize_mini_window returns. */
9000
9001 static int
9002 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9003 {
9004 intptr_t i1 = a1;
9005 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9006 }
9007
9008
9009 /* Resize mini-window W to fit the size of its contents. EXACT_P
9010 means size the window exactly to the size needed. Otherwise, it's
9011 only enlarged until W's buffer is empty.
9012
9013 Set W->start to the right place to begin display. If the whole
9014 contents fit, start at the beginning. Otherwise, start so as
9015 to make the end of the contents appear. This is particularly
9016 important for y-or-n-p, but seems desirable generally.
9017
9018 Value is non-zero if the window height has been changed. */
9019
9020 int
9021 resize_mini_window (struct window *w, int exact_p)
9022 {
9023 struct frame *f = XFRAME (w->frame);
9024 int window_height_changed_p = 0;
9025
9026 xassert (MINI_WINDOW_P (w));
9027
9028 /* By default, start display at the beginning. */
9029 set_marker_both (w->start, w->buffer,
9030 BUF_BEGV (XBUFFER (w->buffer)),
9031 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9032
9033 /* Don't resize windows while redisplaying a window; it would
9034 confuse redisplay functions when the size of the window they are
9035 displaying changes from under them. Such a resizing can happen,
9036 for instance, when which-func prints a long message while
9037 we are running fontification-functions. We're running these
9038 functions with safe_call which binds inhibit-redisplay to t. */
9039 if (!NILP (Vinhibit_redisplay))
9040 return 0;
9041
9042 /* Nil means don't try to resize. */
9043 if (NILP (Vresize_mini_windows)
9044 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9045 return 0;
9046
9047 if (!FRAME_MINIBUF_ONLY_P (f))
9048 {
9049 struct it it;
9050 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9051 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9052 int height, max_height;
9053 int unit = FRAME_LINE_HEIGHT (f);
9054 struct text_pos start;
9055 struct buffer *old_current_buffer = NULL;
9056
9057 if (current_buffer != XBUFFER (w->buffer))
9058 {
9059 old_current_buffer = current_buffer;
9060 set_buffer_internal (XBUFFER (w->buffer));
9061 }
9062
9063 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9064
9065 /* Compute the max. number of lines specified by the user. */
9066 if (FLOATP (Vmax_mini_window_height))
9067 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9068 else if (INTEGERP (Vmax_mini_window_height))
9069 max_height = XINT (Vmax_mini_window_height);
9070 else
9071 max_height = total_height / 4;
9072
9073 /* Correct that max. height if it's bogus. */
9074 max_height = max (1, max_height);
9075 max_height = min (total_height, max_height);
9076
9077 /* Find out the height of the text in the window. */
9078 if (it.line_wrap == TRUNCATE)
9079 height = 1;
9080 else
9081 {
9082 last_height = 0;
9083 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9084 if (it.max_ascent == 0 && it.max_descent == 0)
9085 height = it.current_y + last_height;
9086 else
9087 height = it.current_y + it.max_ascent + it.max_descent;
9088 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9089 height = (height + unit - 1) / unit;
9090 }
9091
9092 /* Compute a suitable window start. */
9093 if (height > max_height)
9094 {
9095 height = max_height;
9096 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9097 move_it_vertically_backward (&it, (height - 1) * unit);
9098 start = it.current.pos;
9099 }
9100 else
9101 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9102 SET_MARKER_FROM_TEXT_POS (w->start, start);
9103
9104 if (EQ (Vresize_mini_windows, Qgrow_only))
9105 {
9106 /* Let it grow only, until we display an empty message, in which
9107 case the window shrinks again. */
9108 if (height > WINDOW_TOTAL_LINES (w))
9109 {
9110 int old_height = WINDOW_TOTAL_LINES (w);
9111 freeze_window_starts (f, 1);
9112 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9113 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9114 }
9115 else if (height < WINDOW_TOTAL_LINES (w)
9116 && (exact_p || BEGV == ZV))
9117 {
9118 int old_height = WINDOW_TOTAL_LINES (w);
9119 freeze_window_starts (f, 0);
9120 shrink_mini_window (w);
9121 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9122 }
9123 }
9124 else
9125 {
9126 /* Always resize to exact size needed. */
9127 if (height > WINDOW_TOTAL_LINES (w))
9128 {
9129 int old_height = WINDOW_TOTAL_LINES (w);
9130 freeze_window_starts (f, 1);
9131 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9132 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9133 }
9134 else if (height < WINDOW_TOTAL_LINES (w))
9135 {
9136 int old_height = WINDOW_TOTAL_LINES (w);
9137 freeze_window_starts (f, 0);
9138 shrink_mini_window (w);
9139
9140 if (height)
9141 {
9142 freeze_window_starts (f, 1);
9143 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9144 }
9145
9146 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9147 }
9148 }
9149
9150 if (old_current_buffer)
9151 set_buffer_internal (old_current_buffer);
9152 }
9153
9154 return window_height_changed_p;
9155 }
9156
9157
9158 /* Value is the current message, a string, or nil if there is no
9159 current message. */
9160
9161 Lisp_Object
9162 current_message (void)
9163 {
9164 Lisp_Object msg;
9165
9166 if (!BUFFERP (echo_area_buffer[0]))
9167 msg = Qnil;
9168 else
9169 {
9170 with_echo_area_buffer (0, 0, current_message_1,
9171 (intptr_t) &msg, Qnil, 0, 0);
9172 if (NILP (msg))
9173 echo_area_buffer[0] = Qnil;
9174 }
9175
9176 return msg;
9177 }
9178
9179
9180 static int
9181 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9182 {
9183 intptr_t i1 = a1;
9184 Lisp_Object *msg = (Lisp_Object *) i1;
9185
9186 if (Z > BEG)
9187 *msg = make_buffer_string (BEG, Z, 1);
9188 else
9189 *msg = Qnil;
9190 return 0;
9191 }
9192
9193
9194 /* Push the current message on Vmessage_stack for later restauration
9195 by restore_message. Value is non-zero if the current message isn't
9196 empty. This is a relatively infrequent operation, so it's not
9197 worth optimizing. */
9198
9199 int
9200 push_message (void)
9201 {
9202 Lisp_Object msg;
9203 msg = current_message ();
9204 Vmessage_stack = Fcons (msg, Vmessage_stack);
9205 return STRINGP (msg);
9206 }
9207
9208
9209 /* Restore message display from the top of Vmessage_stack. */
9210
9211 void
9212 restore_message (void)
9213 {
9214 Lisp_Object msg;
9215
9216 xassert (CONSP (Vmessage_stack));
9217 msg = XCAR (Vmessage_stack);
9218 if (STRINGP (msg))
9219 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9220 else
9221 message3_nolog (msg, 0, 0);
9222 }
9223
9224
9225 /* Handler for record_unwind_protect calling pop_message. */
9226
9227 Lisp_Object
9228 pop_message_unwind (Lisp_Object dummy)
9229 {
9230 pop_message ();
9231 return Qnil;
9232 }
9233
9234 /* Pop the top-most entry off Vmessage_stack. */
9235
9236 static void
9237 pop_message (void)
9238 {
9239 xassert (CONSP (Vmessage_stack));
9240 Vmessage_stack = XCDR (Vmessage_stack);
9241 }
9242
9243
9244 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9245 exits. If the stack is not empty, we have a missing pop_message
9246 somewhere. */
9247
9248 void
9249 check_message_stack (void)
9250 {
9251 if (!NILP (Vmessage_stack))
9252 abort ();
9253 }
9254
9255
9256 /* Truncate to NCHARS what will be displayed in the echo area the next
9257 time we display it---but don't redisplay it now. */
9258
9259 void
9260 truncate_echo_area (EMACS_INT nchars)
9261 {
9262 if (nchars == 0)
9263 echo_area_buffer[0] = Qnil;
9264 /* A null message buffer means that the frame hasn't really been
9265 initialized yet. Error messages get reported properly by
9266 cmd_error, so this must be just an informative message; toss it. */
9267 else if (!noninteractive
9268 && INTERACTIVE
9269 && !NILP (echo_area_buffer[0]))
9270 {
9271 struct frame *sf = SELECTED_FRAME ();
9272 if (FRAME_MESSAGE_BUF (sf))
9273 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9274 }
9275 }
9276
9277
9278 /* Helper function for truncate_echo_area. Truncate the current
9279 message to at most NCHARS characters. */
9280
9281 static int
9282 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9283 {
9284 if (BEG + nchars < Z)
9285 del_range (BEG + nchars, Z);
9286 if (Z == BEG)
9287 echo_area_buffer[0] = Qnil;
9288 return 0;
9289 }
9290
9291
9292 /* Set the current message to a substring of S or STRING.
9293
9294 If STRING is a Lisp string, set the message to the first NBYTES
9295 bytes from STRING. NBYTES zero means use the whole string. If
9296 STRING is multibyte, the message will be displayed multibyte.
9297
9298 If S is not null, set the message to the first LEN bytes of S. LEN
9299 zero means use the whole string. MULTIBYTE_P non-zero means S is
9300 multibyte. Display the message multibyte in that case.
9301
9302 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9303 to t before calling set_message_1 (which calls insert).
9304 */
9305
9306 static void
9307 set_message (const char *s, Lisp_Object string,
9308 EMACS_INT nbytes, int multibyte_p)
9309 {
9310 message_enable_multibyte
9311 = ((s && multibyte_p)
9312 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9313
9314 with_echo_area_buffer (0, -1, set_message_1,
9315 (intptr_t) s, string, nbytes, multibyte_p);
9316 message_buf_print = 0;
9317 help_echo_showing_p = 0;
9318 }
9319
9320
9321 /* Helper function for set_message. Arguments have the same meaning
9322 as there, with A1 corresponding to S and A2 corresponding to STRING
9323 This function is called with the echo area buffer being
9324 current. */
9325
9326 static int
9327 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9328 {
9329 intptr_t i1 = a1;
9330 const char *s = (const char *) i1;
9331 const unsigned char *msg = (const unsigned char *) s;
9332 Lisp_Object string = a2;
9333
9334 /* Change multibyteness of the echo buffer appropriately. */
9335 if (message_enable_multibyte
9336 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9337 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9338
9339 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9340 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9341 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9342
9343 /* Insert new message at BEG. */
9344 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9345
9346 if (STRINGP (string))
9347 {
9348 EMACS_INT nchars;
9349
9350 if (nbytes == 0)
9351 nbytes = SBYTES (string);
9352 nchars = string_byte_to_char (string, nbytes);
9353
9354 /* This function takes care of single/multibyte conversion. We
9355 just have to ensure that the echo area buffer has the right
9356 setting of enable_multibyte_characters. */
9357 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9358 }
9359 else if (s)
9360 {
9361 if (nbytes == 0)
9362 nbytes = strlen (s);
9363
9364 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9365 {
9366 /* Convert from multi-byte to single-byte. */
9367 EMACS_INT i;
9368 int c, n;
9369 char work[1];
9370
9371 /* Convert a multibyte string to single-byte. */
9372 for (i = 0; i < nbytes; i += n)
9373 {
9374 c = string_char_and_length (msg + i, &n);
9375 work[0] = (ASCII_CHAR_P (c)
9376 ? c
9377 : multibyte_char_to_unibyte (c));
9378 insert_1_both (work, 1, 1, 1, 0, 0);
9379 }
9380 }
9381 else if (!multibyte_p
9382 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9383 {
9384 /* Convert from single-byte to multi-byte. */
9385 EMACS_INT i;
9386 int c, n;
9387 unsigned char str[MAX_MULTIBYTE_LENGTH];
9388
9389 /* Convert a single-byte string to multibyte. */
9390 for (i = 0; i < nbytes; i++)
9391 {
9392 c = msg[i];
9393 MAKE_CHAR_MULTIBYTE (c);
9394 n = CHAR_STRING (c, str);
9395 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9396 }
9397 }
9398 else
9399 insert_1 (s, nbytes, 1, 0, 0);
9400 }
9401
9402 return 0;
9403 }
9404
9405
9406 /* Clear messages. CURRENT_P non-zero means clear the current
9407 message. LAST_DISPLAYED_P non-zero means clear the message
9408 last displayed. */
9409
9410 void
9411 clear_message (int current_p, int last_displayed_p)
9412 {
9413 if (current_p)
9414 {
9415 echo_area_buffer[0] = Qnil;
9416 message_cleared_p = 1;
9417 }
9418
9419 if (last_displayed_p)
9420 echo_area_buffer[1] = Qnil;
9421
9422 message_buf_print = 0;
9423 }
9424
9425 /* Clear garbaged frames.
9426
9427 This function is used where the old redisplay called
9428 redraw_garbaged_frames which in turn called redraw_frame which in
9429 turn called clear_frame. The call to clear_frame was a source of
9430 flickering. I believe a clear_frame is not necessary. It should
9431 suffice in the new redisplay to invalidate all current matrices,
9432 and ensure a complete redisplay of all windows. */
9433
9434 static void
9435 clear_garbaged_frames (void)
9436 {
9437 if (frame_garbaged)
9438 {
9439 Lisp_Object tail, frame;
9440 int changed_count = 0;
9441
9442 FOR_EACH_FRAME (tail, frame)
9443 {
9444 struct frame *f = XFRAME (frame);
9445
9446 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9447 {
9448 if (f->resized_p)
9449 {
9450 Fredraw_frame (frame);
9451 f->force_flush_display_p = 1;
9452 }
9453 clear_current_matrices (f);
9454 changed_count++;
9455 f->garbaged = 0;
9456 f->resized_p = 0;
9457 }
9458 }
9459
9460 frame_garbaged = 0;
9461 if (changed_count)
9462 ++windows_or_buffers_changed;
9463 }
9464 }
9465
9466
9467 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9468 is non-zero update selected_frame. Value is non-zero if the
9469 mini-windows height has been changed. */
9470
9471 static int
9472 echo_area_display (int update_frame_p)
9473 {
9474 Lisp_Object mini_window;
9475 struct window *w;
9476 struct frame *f;
9477 int window_height_changed_p = 0;
9478 struct frame *sf = SELECTED_FRAME ();
9479
9480 mini_window = FRAME_MINIBUF_WINDOW (sf);
9481 w = XWINDOW (mini_window);
9482 f = XFRAME (WINDOW_FRAME (w));
9483
9484 /* Don't display if frame is invisible or not yet initialized. */
9485 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9486 return 0;
9487
9488 #ifdef HAVE_WINDOW_SYSTEM
9489 /* When Emacs starts, selected_frame may be the initial terminal
9490 frame. If we let this through, a message would be displayed on
9491 the terminal. */
9492 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9493 return 0;
9494 #endif /* HAVE_WINDOW_SYSTEM */
9495
9496 /* Redraw garbaged frames. */
9497 if (frame_garbaged)
9498 clear_garbaged_frames ();
9499
9500 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9501 {
9502 echo_area_window = mini_window;
9503 window_height_changed_p = display_echo_area (w);
9504 w->must_be_updated_p = 1;
9505
9506 /* Update the display, unless called from redisplay_internal.
9507 Also don't update the screen during redisplay itself. The
9508 update will happen at the end of redisplay, and an update
9509 here could cause confusion. */
9510 if (update_frame_p && !redisplaying_p)
9511 {
9512 int n = 0;
9513
9514 /* If the display update has been interrupted by pending
9515 input, update mode lines in the frame. Due to the
9516 pending input, it might have been that redisplay hasn't
9517 been called, so that mode lines above the echo area are
9518 garbaged. This looks odd, so we prevent it here. */
9519 if (!display_completed)
9520 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9521
9522 if (window_height_changed_p
9523 /* Don't do this if Emacs is shutting down. Redisplay
9524 needs to run hooks. */
9525 && !NILP (Vrun_hooks))
9526 {
9527 /* Must update other windows. Likewise as in other
9528 cases, don't let this update be interrupted by
9529 pending input. */
9530 int count = SPECPDL_INDEX ();
9531 specbind (Qredisplay_dont_pause, Qt);
9532 windows_or_buffers_changed = 1;
9533 redisplay_internal ();
9534 unbind_to (count, Qnil);
9535 }
9536 else if (FRAME_WINDOW_P (f) && n == 0)
9537 {
9538 /* Window configuration is the same as before.
9539 Can do with a display update of the echo area,
9540 unless we displayed some mode lines. */
9541 update_single_window (w, 1);
9542 FRAME_RIF (f)->flush_display (f);
9543 }
9544 else
9545 update_frame (f, 1, 1);
9546
9547 /* If cursor is in the echo area, make sure that the next
9548 redisplay displays the minibuffer, so that the cursor will
9549 be replaced with what the minibuffer wants. */
9550 if (cursor_in_echo_area)
9551 ++windows_or_buffers_changed;
9552 }
9553 }
9554 else if (!EQ (mini_window, selected_window))
9555 windows_or_buffers_changed++;
9556
9557 /* Last displayed message is now the current message. */
9558 echo_area_buffer[1] = echo_area_buffer[0];
9559 /* Inform read_char that we're not echoing. */
9560 echo_message_buffer = Qnil;
9561
9562 /* Prevent redisplay optimization in redisplay_internal by resetting
9563 this_line_start_pos. This is done because the mini-buffer now
9564 displays the message instead of its buffer text. */
9565 if (EQ (mini_window, selected_window))
9566 CHARPOS (this_line_start_pos) = 0;
9567
9568 return window_height_changed_p;
9569 }
9570
9571
9572 \f
9573 /***********************************************************************
9574 Mode Lines and Frame Titles
9575 ***********************************************************************/
9576
9577 /* A buffer for constructing non-propertized mode-line strings and
9578 frame titles in it; allocated from the heap in init_xdisp and
9579 resized as needed in store_mode_line_noprop_char. */
9580
9581 static char *mode_line_noprop_buf;
9582
9583 /* The buffer's end, and a current output position in it. */
9584
9585 static char *mode_line_noprop_buf_end;
9586 static char *mode_line_noprop_ptr;
9587
9588 #define MODE_LINE_NOPROP_LEN(start) \
9589 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9590
9591 static enum {
9592 MODE_LINE_DISPLAY = 0,
9593 MODE_LINE_TITLE,
9594 MODE_LINE_NOPROP,
9595 MODE_LINE_STRING
9596 } mode_line_target;
9597
9598 /* Alist that caches the results of :propertize.
9599 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9600 static Lisp_Object mode_line_proptrans_alist;
9601
9602 /* List of strings making up the mode-line. */
9603 static Lisp_Object mode_line_string_list;
9604
9605 /* Base face property when building propertized mode line string. */
9606 static Lisp_Object mode_line_string_face;
9607 static Lisp_Object mode_line_string_face_prop;
9608
9609
9610 /* Unwind data for mode line strings */
9611
9612 static Lisp_Object Vmode_line_unwind_vector;
9613
9614 static Lisp_Object
9615 format_mode_line_unwind_data (struct buffer *obuf,
9616 Lisp_Object owin,
9617 int save_proptrans)
9618 {
9619 Lisp_Object vector, tmp;
9620
9621 /* Reduce consing by keeping one vector in
9622 Vwith_echo_area_save_vector. */
9623 vector = Vmode_line_unwind_vector;
9624 Vmode_line_unwind_vector = Qnil;
9625
9626 if (NILP (vector))
9627 vector = Fmake_vector (make_number (8), Qnil);
9628
9629 ASET (vector, 0, make_number (mode_line_target));
9630 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9631 ASET (vector, 2, mode_line_string_list);
9632 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9633 ASET (vector, 4, mode_line_string_face);
9634 ASET (vector, 5, mode_line_string_face_prop);
9635
9636 if (obuf)
9637 XSETBUFFER (tmp, obuf);
9638 else
9639 tmp = Qnil;
9640 ASET (vector, 6, tmp);
9641 ASET (vector, 7, owin);
9642
9643 return vector;
9644 }
9645
9646 static Lisp_Object
9647 unwind_format_mode_line (Lisp_Object vector)
9648 {
9649 mode_line_target = XINT (AREF (vector, 0));
9650 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9651 mode_line_string_list = AREF (vector, 2);
9652 if (! EQ (AREF (vector, 3), Qt))
9653 mode_line_proptrans_alist = AREF (vector, 3);
9654 mode_line_string_face = AREF (vector, 4);
9655 mode_line_string_face_prop = AREF (vector, 5);
9656
9657 if (!NILP (AREF (vector, 7)))
9658 /* Select window before buffer, since it may change the buffer. */
9659 Fselect_window (AREF (vector, 7), Qt);
9660
9661 if (!NILP (AREF (vector, 6)))
9662 {
9663 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9664 ASET (vector, 6, Qnil);
9665 }
9666
9667 Vmode_line_unwind_vector = vector;
9668 return Qnil;
9669 }
9670
9671
9672 /* Store a single character C for the frame title in mode_line_noprop_buf.
9673 Re-allocate mode_line_noprop_buf if necessary. */
9674
9675 static void
9676 store_mode_line_noprop_char (char c)
9677 {
9678 /* If output position has reached the end of the allocated buffer,
9679 double the buffer's size. */
9680 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9681 {
9682 int len = MODE_LINE_NOPROP_LEN (0);
9683 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9684 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9685 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9686 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9687 }
9688
9689 *mode_line_noprop_ptr++ = c;
9690 }
9691
9692
9693 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9694 mode_line_noprop_ptr. STRING is the string to store. Do not copy
9695 characters that yield more columns than PRECISION; PRECISION <= 0
9696 means copy the whole string. Pad with spaces until FIELD_WIDTH
9697 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9698 pad. Called from display_mode_element when it is used to build a
9699 frame title. */
9700
9701 static int
9702 store_mode_line_noprop (const char *string, int field_width, int precision)
9703 {
9704 const unsigned char *str = (const unsigned char *) string;
9705 int n = 0;
9706 EMACS_INT dummy, nbytes;
9707
9708 /* Copy at most PRECISION chars from STR. */
9709 nbytes = strlen (string);
9710 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9711 while (nbytes--)
9712 store_mode_line_noprop_char (*str++);
9713
9714 /* Fill up with spaces until FIELD_WIDTH reached. */
9715 while (field_width > 0
9716 && n < field_width)
9717 {
9718 store_mode_line_noprop_char (' ');
9719 ++n;
9720 }
9721
9722 return n;
9723 }
9724
9725 /***********************************************************************
9726 Frame Titles
9727 ***********************************************************************/
9728
9729 #ifdef HAVE_WINDOW_SYSTEM
9730
9731 /* Set the title of FRAME, if it has changed. The title format is
9732 Vicon_title_format if FRAME is iconified, otherwise it is
9733 frame_title_format. */
9734
9735 static void
9736 x_consider_frame_title (Lisp_Object frame)
9737 {
9738 struct frame *f = XFRAME (frame);
9739
9740 if (FRAME_WINDOW_P (f)
9741 || FRAME_MINIBUF_ONLY_P (f)
9742 || f->explicit_name)
9743 {
9744 /* Do we have more than one visible frame on this X display? */
9745 Lisp_Object tail;
9746 Lisp_Object fmt;
9747 int title_start;
9748 char *title;
9749 int len;
9750 struct it it;
9751 int count = SPECPDL_INDEX ();
9752
9753 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9754 {
9755 Lisp_Object other_frame = XCAR (tail);
9756 struct frame *tf = XFRAME (other_frame);
9757
9758 if (tf != f
9759 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9760 && !FRAME_MINIBUF_ONLY_P (tf)
9761 && !EQ (other_frame, tip_frame)
9762 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9763 break;
9764 }
9765
9766 /* Set global variable indicating that multiple frames exist. */
9767 multiple_frames = CONSP (tail);
9768
9769 /* Switch to the buffer of selected window of the frame. Set up
9770 mode_line_target so that display_mode_element will output into
9771 mode_line_noprop_buf; then display the title. */
9772 record_unwind_protect (unwind_format_mode_line,
9773 format_mode_line_unwind_data
9774 (current_buffer, selected_window, 0));
9775
9776 Fselect_window (f->selected_window, Qt);
9777 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9778 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9779
9780 mode_line_target = MODE_LINE_TITLE;
9781 title_start = MODE_LINE_NOPROP_LEN (0);
9782 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9783 NULL, DEFAULT_FACE_ID);
9784 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9785 len = MODE_LINE_NOPROP_LEN (title_start);
9786 title = mode_line_noprop_buf + title_start;
9787 unbind_to (count, Qnil);
9788
9789 /* Set the title only if it's changed. This avoids consing in
9790 the common case where it hasn't. (If it turns out that we've
9791 already wasted too much time by walking through the list with
9792 display_mode_element, then we might need to optimize at a
9793 higher level than this.) */
9794 if (! STRINGP (f->name)
9795 || SBYTES (f->name) != len
9796 || memcmp (title, SDATA (f->name), len) != 0)
9797 x_implicitly_set_name (f, make_string (title, len), Qnil);
9798 }
9799 }
9800
9801 #endif /* not HAVE_WINDOW_SYSTEM */
9802
9803
9804
9805 \f
9806 /***********************************************************************
9807 Menu Bars
9808 ***********************************************************************/
9809
9810
9811 /* Prepare for redisplay by updating menu-bar item lists when
9812 appropriate. This can call eval. */
9813
9814 void
9815 prepare_menu_bars (void)
9816 {
9817 int all_windows;
9818 struct gcpro gcpro1, gcpro2;
9819 struct frame *f;
9820 Lisp_Object tooltip_frame;
9821
9822 #ifdef HAVE_WINDOW_SYSTEM
9823 tooltip_frame = tip_frame;
9824 #else
9825 tooltip_frame = Qnil;
9826 #endif
9827
9828 /* Update all frame titles based on their buffer names, etc. We do
9829 this before the menu bars so that the buffer-menu will show the
9830 up-to-date frame titles. */
9831 #ifdef HAVE_WINDOW_SYSTEM
9832 if (windows_or_buffers_changed || update_mode_lines)
9833 {
9834 Lisp_Object tail, frame;
9835
9836 FOR_EACH_FRAME (tail, frame)
9837 {
9838 f = XFRAME (frame);
9839 if (!EQ (frame, tooltip_frame)
9840 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9841 x_consider_frame_title (frame);
9842 }
9843 }
9844 #endif /* HAVE_WINDOW_SYSTEM */
9845
9846 /* Update the menu bar item lists, if appropriate. This has to be
9847 done before any actual redisplay or generation of display lines. */
9848 all_windows = (update_mode_lines
9849 || buffer_shared > 1
9850 || windows_or_buffers_changed);
9851 if (all_windows)
9852 {
9853 Lisp_Object tail, frame;
9854 int count = SPECPDL_INDEX ();
9855 /* 1 means that update_menu_bar has run its hooks
9856 so any further calls to update_menu_bar shouldn't do so again. */
9857 int menu_bar_hooks_run = 0;
9858
9859 record_unwind_save_match_data ();
9860
9861 FOR_EACH_FRAME (tail, frame)
9862 {
9863 f = XFRAME (frame);
9864
9865 /* Ignore tooltip frame. */
9866 if (EQ (frame, tooltip_frame))
9867 continue;
9868
9869 /* If a window on this frame changed size, report that to
9870 the user and clear the size-change flag. */
9871 if (FRAME_WINDOW_SIZES_CHANGED (f))
9872 {
9873 Lisp_Object functions;
9874
9875 /* Clear flag first in case we get an error below. */
9876 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9877 functions = Vwindow_size_change_functions;
9878 GCPRO2 (tail, functions);
9879
9880 while (CONSP (functions))
9881 {
9882 if (!EQ (XCAR (functions), Qt))
9883 call1 (XCAR (functions), frame);
9884 functions = XCDR (functions);
9885 }
9886 UNGCPRO;
9887 }
9888
9889 GCPRO1 (tail);
9890 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9891 #ifdef HAVE_WINDOW_SYSTEM
9892 update_tool_bar (f, 0);
9893 #endif
9894 #ifdef HAVE_NS
9895 if (windows_or_buffers_changed
9896 && FRAME_NS_P (f))
9897 ns_set_doc_edited (f, Fbuffer_modified_p
9898 (XWINDOW (f->selected_window)->buffer));
9899 #endif
9900 UNGCPRO;
9901 }
9902
9903 unbind_to (count, Qnil);
9904 }
9905 else
9906 {
9907 struct frame *sf = SELECTED_FRAME ();
9908 update_menu_bar (sf, 1, 0);
9909 #ifdef HAVE_WINDOW_SYSTEM
9910 update_tool_bar (sf, 1);
9911 #endif
9912 }
9913 }
9914
9915
9916 /* Update the menu bar item list for frame F. This has to be done
9917 before we start to fill in any display lines, because it can call
9918 eval.
9919
9920 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9921
9922 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9923 already ran the menu bar hooks for this redisplay, so there
9924 is no need to run them again. The return value is the
9925 updated value of this flag, to pass to the next call. */
9926
9927 static int
9928 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9929 {
9930 Lisp_Object window;
9931 register struct window *w;
9932
9933 /* If called recursively during a menu update, do nothing. This can
9934 happen when, for instance, an activate-menubar-hook causes a
9935 redisplay. */
9936 if (inhibit_menubar_update)
9937 return hooks_run;
9938
9939 window = FRAME_SELECTED_WINDOW (f);
9940 w = XWINDOW (window);
9941
9942 if (FRAME_WINDOW_P (f)
9943 ?
9944 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9945 || defined (HAVE_NS) || defined (USE_GTK)
9946 FRAME_EXTERNAL_MENU_BAR (f)
9947 #else
9948 FRAME_MENU_BAR_LINES (f) > 0
9949 #endif
9950 : FRAME_MENU_BAR_LINES (f) > 0)
9951 {
9952 /* If the user has switched buffers or windows, we need to
9953 recompute to reflect the new bindings. But we'll
9954 recompute when update_mode_lines is set too; that means
9955 that people can use force-mode-line-update to request
9956 that the menu bar be recomputed. The adverse effect on
9957 the rest of the redisplay algorithm is about the same as
9958 windows_or_buffers_changed anyway. */
9959 if (windows_or_buffers_changed
9960 /* This used to test w->update_mode_line, but we believe
9961 there is no need to recompute the menu in that case. */
9962 || update_mode_lines
9963 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9964 < BUF_MODIFF (XBUFFER (w->buffer)))
9965 != !NILP (w->last_had_star))
9966 || ((!NILP (Vtransient_mark_mode)
9967 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
9968 != !NILP (w->region_showing)))
9969 {
9970 struct buffer *prev = current_buffer;
9971 int count = SPECPDL_INDEX ();
9972
9973 specbind (Qinhibit_menubar_update, Qt);
9974
9975 set_buffer_internal_1 (XBUFFER (w->buffer));
9976 if (save_match_data)
9977 record_unwind_save_match_data ();
9978 if (NILP (Voverriding_local_map_menu_flag))
9979 {
9980 specbind (Qoverriding_terminal_local_map, Qnil);
9981 specbind (Qoverriding_local_map, Qnil);
9982 }
9983
9984 if (!hooks_run)
9985 {
9986 /* Run the Lucid hook. */
9987 safe_run_hooks (Qactivate_menubar_hook);
9988
9989 /* If it has changed current-menubar from previous value,
9990 really recompute the menu-bar from the value. */
9991 if (! NILP (Vlucid_menu_bar_dirty_flag))
9992 call0 (Qrecompute_lucid_menubar);
9993
9994 safe_run_hooks (Qmenu_bar_update_hook);
9995
9996 hooks_run = 1;
9997 }
9998
9999 XSETFRAME (Vmenu_updating_frame, f);
10000 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10001
10002 /* Redisplay the menu bar in case we changed it. */
10003 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10004 || defined (HAVE_NS) || defined (USE_GTK)
10005 if (FRAME_WINDOW_P (f))
10006 {
10007 #if defined (HAVE_NS)
10008 /* All frames on Mac OS share the same menubar. So only
10009 the selected frame should be allowed to set it. */
10010 if (f == SELECTED_FRAME ())
10011 #endif
10012 set_frame_menubar (f, 0, 0);
10013 }
10014 else
10015 /* On a terminal screen, the menu bar is an ordinary screen
10016 line, and this makes it get updated. */
10017 w->update_mode_line = Qt;
10018 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10019 /* In the non-toolkit version, the menu bar is an ordinary screen
10020 line, and this makes it get updated. */
10021 w->update_mode_line = Qt;
10022 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10023
10024 unbind_to (count, Qnil);
10025 set_buffer_internal_1 (prev);
10026 }
10027 }
10028
10029 return hooks_run;
10030 }
10031
10032
10033 \f
10034 /***********************************************************************
10035 Output Cursor
10036 ***********************************************************************/
10037
10038 #ifdef HAVE_WINDOW_SYSTEM
10039
10040 /* EXPORT:
10041 Nominal cursor position -- where to draw output.
10042 HPOS and VPOS are window relative glyph matrix coordinates.
10043 X and Y are window relative pixel coordinates. */
10044
10045 struct cursor_pos output_cursor;
10046
10047
10048 /* EXPORT:
10049 Set the global variable output_cursor to CURSOR. All cursor
10050 positions are relative to updated_window. */
10051
10052 void
10053 set_output_cursor (struct cursor_pos *cursor)
10054 {
10055 output_cursor.hpos = cursor->hpos;
10056 output_cursor.vpos = cursor->vpos;
10057 output_cursor.x = cursor->x;
10058 output_cursor.y = cursor->y;
10059 }
10060
10061
10062 /* EXPORT for RIF:
10063 Set a nominal cursor position.
10064
10065 HPOS and VPOS are column/row positions in a window glyph matrix. X
10066 and Y are window text area relative pixel positions.
10067
10068 If this is done during an update, updated_window will contain the
10069 window that is being updated and the position is the future output
10070 cursor position for that window. If updated_window is null, use
10071 selected_window and display the cursor at the given position. */
10072
10073 void
10074 x_cursor_to (int vpos, int hpos, int y, int x)
10075 {
10076 struct window *w;
10077
10078 /* If updated_window is not set, work on selected_window. */
10079 if (updated_window)
10080 w = updated_window;
10081 else
10082 w = XWINDOW (selected_window);
10083
10084 /* Set the output cursor. */
10085 output_cursor.hpos = hpos;
10086 output_cursor.vpos = vpos;
10087 output_cursor.x = x;
10088 output_cursor.y = y;
10089
10090 /* If not called as part of an update, really display the cursor.
10091 This will also set the cursor position of W. */
10092 if (updated_window == NULL)
10093 {
10094 BLOCK_INPUT;
10095 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10096 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10097 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10098 UNBLOCK_INPUT;
10099 }
10100 }
10101
10102 #endif /* HAVE_WINDOW_SYSTEM */
10103
10104 \f
10105 /***********************************************************************
10106 Tool-bars
10107 ***********************************************************************/
10108
10109 #ifdef HAVE_WINDOW_SYSTEM
10110
10111 /* Where the mouse was last time we reported a mouse event. */
10112
10113 FRAME_PTR last_mouse_frame;
10114
10115 /* Tool-bar item index of the item on which a mouse button was pressed
10116 or -1. */
10117
10118 int last_tool_bar_item;
10119
10120
10121 static Lisp_Object
10122 update_tool_bar_unwind (Lisp_Object frame)
10123 {
10124 selected_frame = frame;
10125 return Qnil;
10126 }
10127
10128 /* Update the tool-bar item list for frame F. This has to be done
10129 before we start to fill in any display lines. Called from
10130 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10131 and restore it here. */
10132
10133 static void
10134 update_tool_bar (struct frame *f, int save_match_data)
10135 {
10136 #if defined (USE_GTK) || defined (HAVE_NS)
10137 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10138 #else
10139 int do_update = WINDOWP (f->tool_bar_window)
10140 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10141 #endif
10142
10143 if (do_update)
10144 {
10145 Lisp_Object window;
10146 struct window *w;
10147
10148 window = FRAME_SELECTED_WINDOW (f);
10149 w = XWINDOW (window);
10150
10151 /* If the user has switched buffers or windows, we need to
10152 recompute to reflect the new bindings. But we'll
10153 recompute when update_mode_lines is set too; that means
10154 that people can use force-mode-line-update to request
10155 that the menu bar be recomputed. The adverse effect on
10156 the rest of the redisplay algorithm is about the same as
10157 windows_or_buffers_changed anyway. */
10158 if (windows_or_buffers_changed
10159 || !NILP (w->update_mode_line)
10160 || update_mode_lines
10161 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10162 < BUF_MODIFF (XBUFFER (w->buffer)))
10163 != !NILP (w->last_had_star))
10164 || ((!NILP (Vtransient_mark_mode)
10165 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10166 != !NILP (w->region_showing)))
10167 {
10168 struct buffer *prev = current_buffer;
10169 int count = SPECPDL_INDEX ();
10170 Lisp_Object frame, new_tool_bar;
10171 int new_n_tool_bar;
10172 struct gcpro gcpro1;
10173
10174 /* Set current_buffer to the buffer of the selected
10175 window of the frame, so that we get the right local
10176 keymaps. */
10177 set_buffer_internal_1 (XBUFFER (w->buffer));
10178
10179 /* Save match data, if we must. */
10180 if (save_match_data)
10181 record_unwind_save_match_data ();
10182
10183 /* Make sure that we don't accidentally use bogus keymaps. */
10184 if (NILP (Voverriding_local_map_menu_flag))
10185 {
10186 specbind (Qoverriding_terminal_local_map, Qnil);
10187 specbind (Qoverriding_local_map, Qnil);
10188 }
10189
10190 GCPRO1 (new_tool_bar);
10191
10192 /* We must temporarily set the selected frame to this frame
10193 before calling tool_bar_items, because the calculation of
10194 the tool-bar keymap uses the selected frame (see
10195 `tool-bar-make-keymap' in tool-bar.el). */
10196 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10197 XSETFRAME (frame, f);
10198 selected_frame = frame;
10199
10200 /* Build desired tool-bar items from keymaps. */
10201 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10202 &new_n_tool_bar);
10203
10204 /* Redisplay the tool-bar if we changed it. */
10205 if (new_n_tool_bar != f->n_tool_bar_items
10206 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10207 {
10208 /* Redisplay that happens asynchronously due to an expose event
10209 may access f->tool_bar_items. Make sure we update both
10210 variables within BLOCK_INPUT so no such event interrupts. */
10211 BLOCK_INPUT;
10212 f->tool_bar_items = new_tool_bar;
10213 f->n_tool_bar_items = new_n_tool_bar;
10214 w->update_mode_line = Qt;
10215 UNBLOCK_INPUT;
10216 }
10217
10218 UNGCPRO;
10219
10220 unbind_to (count, Qnil);
10221 set_buffer_internal_1 (prev);
10222 }
10223 }
10224 }
10225
10226
10227 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10228 F's desired tool-bar contents. F->tool_bar_items must have
10229 been set up previously by calling prepare_menu_bars. */
10230
10231 static void
10232 build_desired_tool_bar_string (struct frame *f)
10233 {
10234 int i, size, size_needed;
10235 struct gcpro gcpro1, gcpro2, gcpro3;
10236 Lisp_Object image, plist, props;
10237
10238 image = plist = props = Qnil;
10239 GCPRO3 (image, plist, props);
10240
10241 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10242 Otherwise, make a new string. */
10243
10244 /* The size of the string we might be able to reuse. */
10245 size = (STRINGP (f->desired_tool_bar_string)
10246 ? SCHARS (f->desired_tool_bar_string)
10247 : 0);
10248
10249 /* We need one space in the string for each image. */
10250 size_needed = f->n_tool_bar_items;
10251
10252 /* Reuse f->desired_tool_bar_string, if possible. */
10253 if (size < size_needed || NILP (f->desired_tool_bar_string))
10254 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10255 make_number (' '));
10256 else
10257 {
10258 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10259 Fremove_text_properties (make_number (0), make_number (size),
10260 props, f->desired_tool_bar_string);
10261 }
10262
10263 /* Put a `display' property on the string for the images to display,
10264 put a `menu_item' property on tool-bar items with a value that
10265 is the index of the item in F's tool-bar item vector. */
10266 for (i = 0; i < f->n_tool_bar_items; ++i)
10267 {
10268 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10269
10270 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10271 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10272 int hmargin, vmargin, relief, idx, end;
10273
10274 /* If image is a vector, choose the image according to the
10275 button state. */
10276 image = PROP (TOOL_BAR_ITEM_IMAGES);
10277 if (VECTORP (image))
10278 {
10279 if (enabled_p)
10280 idx = (selected_p
10281 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10282 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10283 else
10284 idx = (selected_p
10285 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10286 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10287
10288 xassert (ASIZE (image) >= idx);
10289 image = AREF (image, idx);
10290 }
10291 else
10292 idx = -1;
10293
10294 /* Ignore invalid image specifications. */
10295 if (!valid_image_p (image))
10296 continue;
10297
10298 /* Display the tool-bar button pressed, or depressed. */
10299 plist = Fcopy_sequence (XCDR (image));
10300
10301 /* Compute margin and relief to draw. */
10302 relief = (tool_bar_button_relief >= 0
10303 ? tool_bar_button_relief
10304 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10305 hmargin = vmargin = relief;
10306
10307 if (INTEGERP (Vtool_bar_button_margin)
10308 && XINT (Vtool_bar_button_margin) > 0)
10309 {
10310 hmargin += XFASTINT (Vtool_bar_button_margin);
10311 vmargin += XFASTINT (Vtool_bar_button_margin);
10312 }
10313 else if (CONSP (Vtool_bar_button_margin))
10314 {
10315 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10316 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10317 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10318
10319 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10320 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10321 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10322 }
10323
10324 if (auto_raise_tool_bar_buttons_p)
10325 {
10326 /* Add a `:relief' property to the image spec if the item is
10327 selected. */
10328 if (selected_p)
10329 {
10330 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10331 hmargin -= relief;
10332 vmargin -= relief;
10333 }
10334 }
10335 else
10336 {
10337 /* If image is selected, display it pressed, i.e. with a
10338 negative relief. If it's not selected, display it with a
10339 raised relief. */
10340 plist = Fplist_put (plist, QCrelief,
10341 (selected_p
10342 ? make_number (-relief)
10343 : make_number (relief)));
10344 hmargin -= relief;
10345 vmargin -= relief;
10346 }
10347
10348 /* Put a margin around the image. */
10349 if (hmargin || vmargin)
10350 {
10351 if (hmargin == vmargin)
10352 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10353 else
10354 plist = Fplist_put (plist, QCmargin,
10355 Fcons (make_number (hmargin),
10356 make_number (vmargin)));
10357 }
10358
10359 /* If button is not enabled, and we don't have special images
10360 for the disabled state, make the image appear disabled by
10361 applying an appropriate algorithm to it. */
10362 if (!enabled_p && idx < 0)
10363 plist = Fplist_put (plist, QCconversion, Qdisabled);
10364
10365 /* Put a `display' text property on the string for the image to
10366 display. Put a `menu-item' property on the string that gives
10367 the start of this item's properties in the tool-bar items
10368 vector. */
10369 image = Fcons (Qimage, plist);
10370 props = list4 (Qdisplay, image,
10371 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10372
10373 /* Let the last image hide all remaining spaces in the tool bar
10374 string. The string can be longer than needed when we reuse a
10375 previous string. */
10376 if (i + 1 == f->n_tool_bar_items)
10377 end = SCHARS (f->desired_tool_bar_string);
10378 else
10379 end = i + 1;
10380 Fadd_text_properties (make_number (i), make_number (end),
10381 props, f->desired_tool_bar_string);
10382 #undef PROP
10383 }
10384
10385 UNGCPRO;
10386 }
10387
10388
10389 /* Display one line of the tool-bar of frame IT->f.
10390
10391 HEIGHT specifies the desired height of the tool-bar line.
10392 If the actual height of the glyph row is less than HEIGHT, the
10393 row's height is increased to HEIGHT, and the icons are centered
10394 vertically in the new height.
10395
10396 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10397 count a final empty row in case the tool-bar width exactly matches
10398 the window width.
10399 */
10400
10401 static void
10402 display_tool_bar_line (struct it *it, int height)
10403 {
10404 struct glyph_row *row = it->glyph_row;
10405 int max_x = it->last_visible_x;
10406 struct glyph *last;
10407
10408 prepare_desired_row (row);
10409 row->y = it->current_y;
10410
10411 /* Note that this isn't made use of if the face hasn't a box,
10412 so there's no need to check the face here. */
10413 it->start_of_box_run_p = 1;
10414
10415 while (it->current_x < max_x)
10416 {
10417 int x, n_glyphs_before, i, nglyphs;
10418 struct it it_before;
10419
10420 /* Get the next display element. */
10421 if (!get_next_display_element (it))
10422 {
10423 /* Don't count empty row if we are counting needed tool-bar lines. */
10424 if (height < 0 && !it->hpos)
10425 return;
10426 break;
10427 }
10428
10429 /* Produce glyphs. */
10430 n_glyphs_before = row->used[TEXT_AREA];
10431 it_before = *it;
10432
10433 PRODUCE_GLYPHS (it);
10434
10435 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10436 i = 0;
10437 x = it_before.current_x;
10438 while (i < nglyphs)
10439 {
10440 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10441
10442 if (x + glyph->pixel_width > max_x)
10443 {
10444 /* Glyph doesn't fit on line. Backtrack. */
10445 row->used[TEXT_AREA] = n_glyphs_before;
10446 *it = it_before;
10447 /* If this is the only glyph on this line, it will never fit on the
10448 tool-bar, so skip it. But ensure there is at least one glyph,
10449 so we don't accidentally disable the tool-bar. */
10450 if (n_glyphs_before == 0
10451 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10452 break;
10453 goto out;
10454 }
10455
10456 ++it->hpos;
10457 x += glyph->pixel_width;
10458 ++i;
10459 }
10460
10461 /* Stop at line ends. */
10462 if (ITERATOR_AT_END_OF_LINE_P (it))
10463 break;
10464
10465 set_iterator_to_next (it, 1);
10466 }
10467
10468 out:;
10469
10470 row->displays_text_p = row->used[TEXT_AREA] != 0;
10471
10472 /* Use default face for the border below the tool bar.
10473
10474 FIXME: When auto-resize-tool-bars is grow-only, there is
10475 no additional border below the possibly empty tool-bar lines.
10476 So to make the extra empty lines look "normal", we have to
10477 use the tool-bar face for the border too. */
10478 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10479 it->face_id = DEFAULT_FACE_ID;
10480
10481 extend_face_to_end_of_line (it);
10482 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10483 last->right_box_line_p = 1;
10484 if (last == row->glyphs[TEXT_AREA])
10485 last->left_box_line_p = 1;
10486
10487 /* Make line the desired height and center it vertically. */
10488 if ((height -= it->max_ascent + it->max_descent) > 0)
10489 {
10490 /* Don't add more than one line height. */
10491 height %= FRAME_LINE_HEIGHT (it->f);
10492 it->max_ascent += height / 2;
10493 it->max_descent += (height + 1) / 2;
10494 }
10495
10496 compute_line_metrics (it);
10497
10498 /* If line is empty, make it occupy the rest of the tool-bar. */
10499 if (!row->displays_text_p)
10500 {
10501 row->height = row->phys_height = it->last_visible_y - row->y;
10502 row->visible_height = row->height;
10503 row->ascent = row->phys_ascent = 0;
10504 row->extra_line_spacing = 0;
10505 }
10506
10507 row->full_width_p = 1;
10508 row->continued_p = 0;
10509 row->truncated_on_left_p = 0;
10510 row->truncated_on_right_p = 0;
10511
10512 it->current_x = it->hpos = 0;
10513 it->current_y += row->height;
10514 ++it->vpos;
10515 ++it->glyph_row;
10516 }
10517
10518
10519 /* Max tool-bar height. */
10520
10521 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10522 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10523
10524 /* Value is the number of screen lines needed to make all tool-bar
10525 items of frame F visible. The number of actual rows needed is
10526 returned in *N_ROWS if non-NULL. */
10527
10528 static int
10529 tool_bar_lines_needed (struct frame *f, int *n_rows)
10530 {
10531 struct window *w = XWINDOW (f->tool_bar_window);
10532 struct it it;
10533 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10534 the desired matrix, so use (unused) mode-line row as temporary row to
10535 avoid destroying the first tool-bar row. */
10536 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10537
10538 /* Initialize an iterator for iteration over
10539 F->desired_tool_bar_string in the tool-bar window of frame F. */
10540 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10541 it.first_visible_x = 0;
10542 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10543 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10544
10545 while (!ITERATOR_AT_END_P (&it))
10546 {
10547 clear_glyph_row (temp_row);
10548 it.glyph_row = temp_row;
10549 display_tool_bar_line (&it, -1);
10550 }
10551 clear_glyph_row (temp_row);
10552
10553 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10554 if (n_rows)
10555 *n_rows = it.vpos > 0 ? it.vpos : -1;
10556
10557 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10558 }
10559
10560
10561 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10562 0, 1, 0,
10563 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10564 (Lisp_Object frame)
10565 {
10566 struct frame *f;
10567 struct window *w;
10568 int nlines = 0;
10569
10570 if (NILP (frame))
10571 frame = selected_frame;
10572 else
10573 CHECK_FRAME (frame);
10574 f = XFRAME (frame);
10575
10576 if (WINDOWP (f->tool_bar_window)
10577 || (w = XWINDOW (f->tool_bar_window),
10578 WINDOW_TOTAL_LINES (w) > 0))
10579 {
10580 update_tool_bar (f, 1);
10581 if (f->n_tool_bar_items)
10582 {
10583 build_desired_tool_bar_string (f);
10584 nlines = tool_bar_lines_needed (f, NULL);
10585 }
10586 }
10587
10588 return make_number (nlines);
10589 }
10590
10591
10592 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10593 height should be changed. */
10594
10595 static int
10596 redisplay_tool_bar (struct frame *f)
10597 {
10598 struct window *w;
10599 struct it it;
10600 struct glyph_row *row;
10601
10602 #if defined (USE_GTK) || defined (HAVE_NS)
10603 if (FRAME_EXTERNAL_TOOL_BAR (f))
10604 update_frame_tool_bar (f);
10605 return 0;
10606 #endif
10607
10608 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10609 do anything. This means you must start with tool-bar-lines
10610 non-zero to get the auto-sizing effect. Or in other words, you
10611 can turn off tool-bars by specifying tool-bar-lines zero. */
10612 if (!WINDOWP (f->tool_bar_window)
10613 || (w = XWINDOW (f->tool_bar_window),
10614 WINDOW_TOTAL_LINES (w) == 0))
10615 return 0;
10616
10617 /* Set up an iterator for the tool-bar window. */
10618 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10619 it.first_visible_x = 0;
10620 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10621 row = it.glyph_row;
10622
10623 /* Build a string that represents the contents of the tool-bar. */
10624 build_desired_tool_bar_string (f);
10625 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10626
10627 if (f->n_tool_bar_rows == 0)
10628 {
10629 int nlines;
10630
10631 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10632 nlines != WINDOW_TOTAL_LINES (w)))
10633 {
10634 Lisp_Object frame;
10635 int old_height = WINDOW_TOTAL_LINES (w);
10636
10637 XSETFRAME (frame, f);
10638 Fmodify_frame_parameters (frame,
10639 Fcons (Fcons (Qtool_bar_lines,
10640 make_number (nlines)),
10641 Qnil));
10642 if (WINDOW_TOTAL_LINES (w) != old_height)
10643 {
10644 clear_glyph_matrix (w->desired_matrix);
10645 fonts_changed_p = 1;
10646 return 1;
10647 }
10648 }
10649 }
10650
10651 /* Display as many lines as needed to display all tool-bar items. */
10652
10653 if (f->n_tool_bar_rows > 0)
10654 {
10655 int border, rows, height, extra;
10656
10657 if (INTEGERP (Vtool_bar_border))
10658 border = XINT (Vtool_bar_border);
10659 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10660 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10661 else if (EQ (Vtool_bar_border, Qborder_width))
10662 border = f->border_width;
10663 else
10664 border = 0;
10665 if (border < 0)
10666 border = 0;
10667
10668 rows = f->n_tool_bar_rows;
10669 height = max (1, (it.last_visible_y - border) / rows);
10670 extra = it.last_visible_y - border - height * rows;
10671
10672 while (it.current_y < it.last_visible_y)
10673 {
10674 int h = 0;
10675 if (extra > 0 && rows-- > 0)
10676 {
10677 h = (extra + rows - 1) / rows;
10678 extra -= h;
10679 }
10680 display_tool_bar_line (&it, height + h);
10681 }
10682 }
10683 else
10684 {
10685 while (it.current_y < it.last_visible_y)
10686 display_tool_bar_line (&it, 0);
10687 }
10688
10689 /* It doesn't make much sense to try scrolling in the tool-bar
10690 window, so don't do it. */
10691 w->desired_matrix->no_scrolling_p = 1;
10692 w->must_be_updated_p = 1;
10693
10694 if (!NILP (Vauto_resize_tool_bars))
10695 {
10696 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10697 int change_height_p = 0;
10698
10699 /* If we couldn't display everything, change the tool-bar's
10700 height if there is room for more. */
10701 if (IT_STRING_CHARPOS (it) < it.end_charpos
10702 && it.current_y < max_tool_bar_height)
10703 change_height_p = 1;
10704
10705 row = it.glyph_row - 1;
10706
10707 /* If there are blank lines at the end, except for a partially
10708 visible blank line at the end that is smaller than
10709 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10710 if (!row->displays_text_p
10711 && row->height >= FRAME_LINE_HEIGHT (f))
10712 change_height_p = 1;
10713
10714 /* If row displays tool-bar items, but is partially visible,
10715 change the tool-bar's height. */
10716 if (row->displays_text_p
10717 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10718 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10719 change_height_p = 1;
10720
10721 /* Resize windows as needed by changing the `tool-bar-lines'
10722 frame parameter. */
10723 if (change_height_p)
10724 {
10725 Lisp_Object frame;
10726 int old_height = WINDOW_TOTAL_LINES (w);
10727 int nrows;
10728 int nlines = tool_bar_lines_needed (f, &nrows);
10729
10730 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10731 && !f->minimize_tool_bar_window_p)
10732 ? (nlines > old_height)
10733 : (nlines != old_height));
10734 f->minimize_tool_bar_window_p = 0;
10735
10736 if (change_height_p)
10737 {
10738 XSETFRAME (frame, f);
10739 Fmodify_frame_parameters (frame,
10740 Fcons (Fcons (Qtool_bar_lines,
10741 make_number (nlines)),
10742 Qnil));
10743 if (WINDOW_TOTAL_LINES (w) != old_height)
10744 {
10745 clear_glyph_matrix (w->desired_matrix);
10746 f->n_tool_bar_rows = nrows;
10747 fonts_changed_p = 1;
10748 return 1;
10749 }
10750 }
10751 }
10752 }
10753
10754 f->minimize_tool_bar_window_p = 0;
10755 return 0;
10756 }
10757
10758
10759 /* Get information about the tool-bar item which is displayed in GLYPH
10760 on frame F. Return in *PROP_IDX the index where tool-bar item
10761 properties start in F->tool_bar_items. Value is zero if
10762 GLYPH doesn't display a tool-bar item. */
10763
10764 static int
10765 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10766 {
10767 Lisp_Object prop;
10768 int success_p;
10769 int charpos;
10770
10771 /* This function can be called asynchronously, which means we must
10772 exclude any possibility that Fget_text_property signals an
10773 error. */
10774 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10775 charpos = max (0, charpos);
10776
10777 /* Get the text property `menu-item' at pos. The value of that
10778 property is the start index of this item's properties in
10779 F->tool_bar_items. */
10780 prop = Fget_text_property (make_number (charpos),
10781 Qmenu_item, f->current_tool_bar_string);
10782 if (INTEGERP (prop))
10783 {
10784 *prop_idx = XINT (prop);
10785 success_p = 1;
10786 }
10787 else
10788 success_p = 0;
10789
10790 return success_p;
10791 }
10792
10793 \f
10794 /* Get information about the tool-bar item at position X/Y on frame F.
10795 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10796 the current matrix of the tool-bar window of F, or NULL if not
10797 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10798 item in F->tool_bar_items. Value is
10799
10800 -1 if X/Y is not on a tool-bar item
10801 0 if X/Y is on the same item that was highlighted before.
10802 1 otherwise. */
10803
10804 static int
10805 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10806 int *hpos, int *vpos, int *prop_idx)
10807 {
10808 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10809 struct window *w = XWINDOW (f->tool_bar_window);
10810 int area;
10811
10812 /* Find the glyph under X/Y. */
10813 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10814 if (*glyph == NULL)
10815 return -1;
10816
10817 /* Get the start of this tool-bar item's properties in
10818 f->tool_bar_items. */
10819 if (!tool_bar_item_info (f, *glyph, prop_idx))
10820 return -1;
10821
10822 /* Is mouse on the highlighted item? */
10823 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10824 && *vpos >= hlinfo->mouse_face_beg_row
10825 && *vpos <= hlinfo->mouse_face_end_row
10826 && (*vpos > hlinfo->mouse_face_beg_row
10827 || *hpos >= hlinfo->mouse_face_beg_col)
10828 && (*vpos < hlinfo->mouse_face_end_row
10829 || *hpos < hlinfo->mouse_face_end_col
10830 || hlinfo->mouse_face_past_end))
10831 return 0;
10832
10833 return 1;
10834 }
10835
10836
10837 /* EXPORT:
10838 Handle mouse button event on the tool-bar of frame F, at
10839 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10840 0 for button release. MODIFIERS is event modifiers for button
10841 release. */
10842
10843 void
10844 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10845 unsigned int modifiers)
10846 {
10847 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10848 struct window *w = XWINDOW (f->tool_bar_window);
10849 int hpos, vpos, prop_idx;
10850 struct glyph *glyph;
10851 Lisp_Object enabled_p;
10852
10853 /* If not on the highlighted tool-bar item, return. */
10854 frame_to_window_pixel_xy (w, &x, &y);
10855 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10856 return;
10857
10858 /* If item is disabled, do nothing. */
10859 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10860 if (NILP (enabled_p))
10861 return;
10862
10863 if (down_p)
10864 {
10865 /* Show item in pressed state. */
10866 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10867 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10868 last_tool_bar_item = prop_idx;
10869 }
10870 else
10871 {
10872 Lisp_Object key, frame;
10873 struct input_event event;
10874 EVENT_INIT (event);
10875
10876 /* Show item in released state. */
10877 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10878 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10879
10880 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10881
10882 XSETFRAME (frame, f);
10883 event.kind = TOOL_BAR_EVENT;
10884 event.frame_or_window = frame;
10885 event.arg = frame;
10886 kbd_buffer_store_event (&event);
10887
10888 event.kind = TOOL_BAR_EVENT;
10889 event.frame_or_window = frame;
10890 event.arg = key;
10891 event.modifiers = modifiers;
10892 kbd_buffer_store_event (&event);
10893 last_tool_bar_item = -1;
10894 }
10895 }
10896
10897
10898 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10899 tool-bar window-relative coordinates X/Y. Called from
10900 note_mouse_highlight. */
10901
10902 static void
10903 note_tool_bar_highlight (struct frame *f, int x, int y)
10904 {
10905 Lisp_Object window = f->tool_bar_window;
10906 struct window *w = XWINDOW (window);
10907 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10908 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10909 int hpos, vpos;
10910 struct glyph *glyph;
10911 struct glyph_row *row;
10912 int i;
10913 Lisp_Object enabled_p;
10914 int prop_idx;
10915 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10916 int mouse_down_p, rc;
10917
10918 /* Function note_mouse_highlight is called with negative X/Y
10919 values when mouse moves outside of the frame. */
10920 if (x <= 0 || y <= 0)
10921 {
10922 clear_mouse_face (hlinfo);
10923 return;
10924 }
10925
10926 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10927 if (rc < 0)
10928 {
10929 /* Not on tool-bar item. */
10930 clear_mouse_face (hlinfo);
10931 return;
10932 }
10933 else if (rc == 0)
10934 /* On same tool-bar item as before. */
10935 goto set_help_echo;
10936
10937 clear_mouse_face (hlinfo);
10938
10939 /* Mouse is down, but on different tool-bar item? */
10940 mouse_down_p = (dpyinfo->grabbed
10941 && f == last_mouse_frame
10942 && FRAME_LIVE_P (f));
10943 if (mouse_down_p
10944 && last_tool_bar_item != prop_idx)
10945 return;
10946
10947 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10948 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10949
10950 /* If tool-bar item is not enabled, don't highlight it. */
10951 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10952 if (!NILP (enabled_p))
10953 {
10954 /* Compute the x-position of the glyph. In front and past the
10955 image is a space. We include this in the highlighted area. */
10956 row = MATRIX_ROW (w->current_matrix, vpos);
10957 for (i = x = 0; i < hpos; ++i)
10958 x += row->glyphs[TEXT_AREA][i].pixel_width;
10959
10960 /* Record this as the current active region. */
10961 hlinfo->mouse_face_beg_col = hpos;
10962 hlinfo->mouse_face_beg_row = vpos;
10963 hlinfo->mouse_face_beg_x = x;
10964 hlinfo->mouse_face_beg_y = row->y;
10965 hlinfo->mouse_face_past_end = 0;
10966
10967 hlinfo->mouse_face_end_col = hpos + 1;
10968 hlinfo->mouse_face_end_row = vpos;
10969 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
10970 hlinfo->mouse_face_end_y = row->y;
10971 hlinfo->mouse_face_window = window;
10972 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10973
10974 /* Display it as active. */
10975 show_mouse_face (hlinfo, draw);
10976 hlinfo->mouse_face_image_state = draw;
10977 }
10978
10979 set_help_echo:
10980
10981 /* Set help_echo_string to a help string to display for this tool-bar item.
10982 XTread_socket does the rest. */
10983 help_echo_object = help_echo_window = Qnil;
10984 help_echo_pos = -1;
10985 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10986 if (NILP (help_echo_string))
10987 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10988 }
10989
10990 #endif /* HAVE_WINDOW_SYSTEM */
10991
10992
10993 \f
10994 /************************************************************************
10995 Horizontal scrolling
10996 ************************************************************************/
10997
10998 static int hscroll_window_tree (Lisp_Object);
10999 static int hscroll_windows (Lisp_Object);
11000
11001 /* For all leaf windows in the window tree rooted at WINDOW, set their
11002 hscroll value so that PT is (i) visible in the window, and (ii) so
11003 that it is not within a certain margin at the window's left and
11004 right border. Value is non-zero if any window's hscroll has been
11005 changed. */
11006
11007 static int
11008 hscroll_window_tree (Lisp_Object window)
11009 {
11010 int hscrolled_p = 0;
11011 int hscroll_relative_p = FLOATP (Vhscroll_step);
11012 int hscroll_step_abs = 0;
11013 double hscroll_step_rel = 0;
11014
11015 if (hscroll_relative_p)
11016 {
11017 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11018 if (hscroll_step_rel < 0)
11019 {
11020 hscroll_relative_p = 0;
11021 hscroll_step_abs = 0;
11022 }
11023 }
11024 else if (INTEGERP (Vhscroll_step))
11025 {
11026 hscroll_step_abs = XINT (Vhscroll_step);
11027 if (hscroll_step_abs < 0)
11028 hscroll_step_abs = 0;
11029 }
11030 else
11031 hscroll_step_abs = 0;
11032
11033 while (WINDOWP (window))
11034 {
11035 struct window *w = XWINDOW (window);
11036
11037 if (WINDOWP (w->hchild))
11038 hscrolled_p |= hscroll_window_tree (w->hchild);
11039 else if (WINDOWP (w->vchild))
11040 hscrolled_p |= hscroll_window_tree (w->vchild);
11041 else if (w->cursor.vpos >= 0)
11042 {
11043 int h_margin;
11044 int text_area_width;
11045 struct glyph_row *current_cursor_row
11046 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11047 struct glyph_row *desired_cursor_row
11048 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11049 struct glyph_row *cursor_row
11050 = (desired_cursor_row->enabled_p
11051 ? desired_cursor_row
11052 : current_cursor_row);
11053
11054 text_area_width = window_box_width (w, TEXT_AREA);
11055
11056 /* Scroll when cursor is inside this scroll margin. */
11057 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11058
11059 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11060 && ((XFASTINT (w->hscroll)
11061 && w->cursor.x <= h_margin)
11062 || (cursor_row->enabled_p
11063 && cursor_row->truncated_on_right_p
11064 && (w->cursor.x >= text_area_width - h_margin))))
11065 {
11066 struct it it;
11067 int hscroll;
11068 struct buffer *saved_current_buffer;
11069 EMACS_INT pt;
11070 int wanted_x;
11071
11072 /* Find point in a display of infinite width. */
11073 saved_current_buffer = current_buffer;
11074 current_buffer = XBUFFER (w->buffer);
11075
11076 if (w == XWINDOW (selected_window))
11077 pt = PT;
11078 else
11079 {
11080 pt = marker_position (w->pointm);
11081 pt = max (BEGV, pt);
11082 pt = min (ZV, pt);
11083 }
11084
11085 /* Move iterator to pt starting at cursor_row->start in
11086 a line with infinite width. */
11087 init_to_row_start (&it, w, cursor_row);
11088 it.last_visible_x = INFINITY;
11089 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11090 current_buffer = saved_current_buffer;
11091
11092 /* Position cursor in window. */
11093 if (!hscroll_relative_p && hscroll_step_abs == 0)
11094 hscroll = max (0, (it.current_x
11095 - (ITERATOR_AT_END_OF_LINE_P (&it)
11096 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11097 : (text_area_width / 2))))
11098 / FRAME_COLUMN_WIDTH (it.f);
11099 else if (w->cursor.x >= text_area_width - h_margin)
11100 {
11101 if (hscroll_relative_p)
11102 wanted_x = text_area_width * (1 - hscroll_step_rel)
11103 - h_margin;
11104 else
11105 wanted_x = text_area_width
11106 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11107 - h_margin;
11108 hscroll
11109 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11110 }
11111 else
11112 {
11113 if (hscroll_relative_p)
11114 wanted_x = text_area_width * hscroll_step_rel
11115 + h_margin;
11116 else
11117 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11118 + h_margin;
11119 hscroll
11120 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11121 }
11122 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11123
11124 /* Don't call Fset_window_hscroll if value hasn't
11125 changed because it will prevent redisplay
11126 optimizations. */
11127 if (XFASTINT (w->hscroll) != hscroll)
11128 {
11129 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11130 w->hscroll = make_number (hscroll);
11131 hscrolled_p = 1;
11132 }
11133 }
11134 }
11135
11136 window = w->next;
11137 }
11138
11139 /* Value is non-zero if hscroll of any leaf window has been changed. */
11140 return hscrolled_p;
11141 }
11142
11143
11144 /* Set hscroll so that cursor is visible and not inside horizontal
11145 scroll margins for all windows in the tree rooted at WINDOW. See
11146 also hscroll_window_tree above. Value is non-zero if any window's
11147 hscroll has been changed. If it has, desired matrices on the frame
11148 of WINDOW are cleared. */
11149
11150 static int
11151 hscroll_windows (Lisp_Object window)
11152 {
11153 int hscrolled_p = hscroll_window_tree (window);
11154 if (hscrolled_p)
11155 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11156 return hscrolled_p;
11157 }
11158
11159
11160 \f
11161 /************************************************************************
11162 Redisplay
11163 ************************************************************************/
11164
11165 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11166 to a non-zero value. This is sometimes handy to have in a debugger
11167 session. */
11168
11169 #if GLYPH_DEBUG
11170
11171 /* First and last unchanged row for try_window_id. */
11172
11173 int debug_first_unchanged_at_end_vpos;
11174 int debug_last_unchanged_at_beg_vpos;
11175
11176 /* Delta vpos and y. */
11177
11178 int debug_dvpos, debug_dy;
11179
11180 /* Delta in characters and bytes for try_window_id. */
11181
11182 EMACS_INT debug_delta, debug_delta_bytes;
11183
11184 /* Values of window_end_pos and window_end_vpos at the end of
11185 try_window_id. */
11186
11187 EMACS_INT debug_end_vpos;
11188
11189 /* Append a string to W->desired_matrix->method. FMT is a printf
11190 format string. A1...A9 are a supplement for a variable-length
11191 argument list. If trace_redisplay_p is non-zero also printf the
11192 resulting string to stderr. */
11193
11194 static void
11195 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11196 struct window *w;
11197 char *fmt;
11198 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11199 {
11200 char buffer[512];
11201 char *method = w->desired_matrix->method;
11202 int len = strlen (method);
11203 int size = sizeof w->desired_matrix->method;
11204 int remaining = size - len - 1;
11205
11206 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11207 if (len && remaining)
11208 {
11209 method[len] = '|';
11210 --remaining, ++len;
11211 }
11212
11213 strncpy (method + len, buffer, remaining);
11214
11215 if (trace_redisplay_p)
11216 fprintf (stderr, "%p (%s): %s\n",
11217 w,
11218 ((BUFFERP (w->buffer)
11219 && STRINGP (XBUFFER (w->buffer)->name))
11220 ? SSDATA (XBUFFER (w->buffer)->name)
11221 : "no buffer"),
11222 buffer);
11223 }
11224
11225 #endif /* GLYPH_DEBUG */
11226
11227
11228 /* Value is non-zero if all changes in window W, which displays
11229 current_buffer, are in the text between START and END. START is a
11230 buffer position, END is given as a distance from Z. Used in
11231 redisplay_internal for display optimization. */
11232
11233 static INLINE int
11234 text_outside_line_unchanged_p (struct window *w,
11235 EMACS_INT start, EMACS_INT end)
11236 {
11237 int unchanged_p = 1;
11238
11239 /* If text or overlays have changed, see where. */
11240 if (XFASTINT (w->last_modified) < MODIFF
11241 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11242 {
11243 /* Gap in the line? */
11244 if (GPT < start || Z - GPT < end)
11245 unchanged_p = 0;
11246
11247 /* Changes start in front of the line, or end after it? */
11248 if (unchanged_p
11249 && (BEG_UNCHANGED < start - 1
11250 || END_UNCHANGED < end))
11251 unchanged_p = 0;
11252
11253 /* If selective display, can't optimize if changes start at the
11254 beginning of the line. */
11255 if (unchanged_p
11256 && INTEGERP (BVAR (current_buffer, selective_display))
11257 && XINT (BVAR (current_buffer, selective_display)) > 0
11258 && (BEG_UNCHANGED < start || GPT <= start))
11259 unchanged_p = 0;
11260
11261 /* If there are overlays at the start or end of the line, these
11262 may have overlay strings with newlines in them. A change at
11263 START, for instance, may actually concern the display of such
11264 overlay strings as well, and they are displayed on different
11265 lines. So, quickly rule out this case. (For the future, it
11266 might be desirable to implement something more telling than
11267 just BEG/END_UNCHANGED.) */
11268 if (unchanged_p)
11269 {
11270 if (BEG + BEG_UNCHANGED == start
11271 && overlay_touches_p (start))
11272 unchanged_p = 0;
11273 if (END_UNCHANGED == end
11274 && overlay_touches_p (Z - end))
11275 unchanged_p = 0;
11276 }
11277
11278 /* Under bidi reordering, adding or deleting a character in the
11279 beginning of a paragraph, before the first strong directional
11280 character, can change the base direction of the paragraph (unless
11281 the buffer specifies a fixed paragraph direction), which will
11282 require to redisplay the whole paragraph. It might be worthwhile
11283 to find the paragraph limits and widen the range of redisplayed
11284 lines to that, but for now just give up this optimization. */
11285 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11286 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11287 unchanged_p = 0;
11288 }
11289
11290 return unchanged_p;
11291 }
11292
11293
11294 /* Do a frame update, taking possible shortcuts into account. This is
11295 the main external entry point for redisplay.
11296
11297 If the last redisplay displayed an echo area message and that message
11298 is no longer requested, we clear the echo area or bring back the
11299 mini-buffer if that is in use. */
11300
11301 void
11302 redisplay (void)
11303 {
11304 redisplay_internal ();
11305 }
11306
11307
11308 static Lisp_Object
11309 overlay_arrow_string_or_property (Lisp_Object var)
11310 {
11311 Lisp_Object val;
11312
11313 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11314 return val;
11315
11316 return Voverlay_arrow_string;
11317 }
11318
11319 /* Return 1 if there are any overlay-arrows in current_buffer. */
11320 static int
11321 overlay_arrow_in_current_buffer_p (void)
11322 {
11323 Lisp_Object vlist;
11324
11325 for (vlist = Voverlay_arrow_variable_list;
11326 CONSP (vlist);
11327 vlist = XCDR (vlist))
11328 {
11329 Lisp_Object var = XCAR (vlist);
11330 Lisp_Object val;
11331
11332 if (!SYMBOLP (var))
11333 continue;
11334 val = find_symbol_value (var);
11335 if (MARKERP (val)
11336 && current_buffer == XMARKER (val)->buffer)
11337 return 1;
11338 }
11339 return 0;
11340 }
11341
11342
11343 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11344 has changed. */
11345
11346 static int
11347 overlay_arrows_changed_p (void)
11348 {
11349 Lisp_Object vlist;
11350
11351 for (vlist = Voverlay_arrow_variable_list;
11352 CONSP (vlist);
11353 vlist = XCDR (vlist))
11354 {
11355 Lisp_Object var = XCAR (vlist);
11356 Lisp_Object val, pstr;
11357
11358 if (!SYMBOLP (var))
11359 continue;
11360 val = find_symbol_value (var);
11361 if (!MARKERP (val))
11362 continue;
11363 if (! EQ (COERCE_MARKER (val),
11364 Fget (var, Qlast_arrow_position))
11365 || ! (pstr = overlay_arrow_string_or_property (var),
11366 EQ (pstr, Fget (var, Qlast_arrow_string))))
11367 return 1;
11368 }
11369 return 0;
11370 }
11371
11372 /* Mark overlay arrows to be updated on next redisplay. */
11373
11374 static void
11375 update_overlay_arrows (int up_to_date)
11376 {
11377 Lisp_Object vlist;
11378
11379 for (vlist = Voverlay_arrow_variable_list;
11380 CONSP (vlist);
11381 vlist = XCDR (vlist))
11382 {
11383 Lisp_Object var = XCAR (vlist);
11384
11385 if (!SYMBOLP (var))
11386 continue;
11387
11388 if (up_to_date > 0)
11389 {
11390 Lisp_Object val = find_symbol_value (var);
11391 Fput (var, Qlast_arrow_position,
11392 COERCE_MARKER (val));
11393 Fput (var, Qlast_arrow_string,
11394 overlay_arrow_string_or_property (var));
11395 }
11396 else if (up_to_date < 0
11397 || !NILP (Fget (var, Qlast_arrow_position)))
11398 {
11399 Fput (var, Qlast_arrow_position, Qt);
11400 Fput (var, Qlast_arrow_string, Qt);
11401 }
11402 }
11403 }
11404
11405
11406 /* Return overlay arrow string to display at row.
11407 Return integer (bitmap number) for arrow bitmap in left fringe.
11408 Return nil if no overlay arrow. */
11409
11410 static Lisp_Object
11411 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11412 {
11413 Lisp_Object vlist;
11414
11415 for (vlist = Voverlay_arrow_variable_list;
11416 CONSP (vlist);
11417 vlist = XCDR (vlist))
11418 {
11419 Lisp_Object var = XCAR (vlist);
11420 Lisp_Object val;
11421
11422 if (!SYMBOLP (var))
11423 continue;
11424
11425 val = find_symbol_value (var);
11426
11427 if (MARKERP (val)
11428 && current_buffer == XMARKER (val)->buffer
11429 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11430 {
11431 if (FRAME_WINDOW_P (it->f)
11432 /* FIXME: if ROW->reversed_p is set, this should test
11433 the right fringe, not the left one. */
11434 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11435 {
11436 #ifdef HAVE_WINDOW_SYSTEM
11437 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11438 {
11439 int fringe_bitmap;
11440 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11441 return make_number (fringe_bitmap);
11442 }
11443 #endif
11444 return make_number (-1); /* Use default arrow bitmap */
11445 }
11446 return overlay_arrow_string_or_property (var);
11447 }
11448 }
11449
11450 return Qnil;
11451 }
11452
11453 /* Return 1 if point moved out of or into a composition. Otherwise
11454 return 0. PREV_BUF and PREV_PT are the last point buffer and
11455 position. BUF and PT are the current point buffer and position. */
11456
11457 static int
11458 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11459 struct buffer *buf, EMACS_INT pt)
11460 {
11461 EMACS_INT start, end;
11462 Lisp_Object prop;
11463 Lisp_Object buffer;
11464
11465 XSETBUFFER (buffer, buf);
11466 /* Check a composition at the last point if point moved within the
11467 same buffer. */
11468 if (prev_buf == buf)
11469 {
11470 if (prev_pt == pt)
11471 /* Point didn't move. */
11472 return 0;
11473
11474 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11475 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11476 && COMPOSITION_VALID_P (start, end, prop)
11477 && start < prev_pt && end > prev_pt)
11478 /* The last point was within the composition. Return 1 iff
11479 point moved out of the composition. */
11480 return (pt <= start || pt >= end);
11481 }
11482
11483 /* Check a composition at the current point. */
11484 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11485 && find_composition (pt, -1, &start, &end, &prop, buffer)
11486 && COMPOSITION_VALID_P (start, end, prop)
11487 && start < pt && end > pt);
11488 }
11489
11490
11491 /* Reconsider the setting of B->clip_changed which is displayed
11492 in window W. */
11493
11494 static INLINE void
11495 reconsider_clip_changes (struct window *w, struct buffer *b)
11496 {
11497 if (b->clip_changed
11498 && !NILP (w->window_end_valid)
11499 && w->current_matrix->buffer == b
11500 && w->current_matrix->zv == BUF_ZV (b)
11501 && w->current_matrix->begv == BUF_BEGV (b))
11502 b->clip_changed = 0;
11503
11504 /* If display wasn't paused, and W is not a tool bar window, see if
11505 point has been moved into or out of a composition. In that case,
11506 we set b->clip_changed to 1 to force updating the screen. If
11507 b->clip_changed has already been set to 1, we can skip this
11508 check. */
11509 if (!b->clip_changed
11510 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11511 {
11512 EMACS_INT pt;
11513
11514 if (w == XWINDOW (selected_window))
11515 pt = PT;
11516 else
11517 pt = marker_position (w->pointm);
11518
11519 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11520 || pt != XINT (w->last_point))
11521 && check_point_in_composition (w->current_matrix->buffer,
11522 XINT (w->last_point),
11523 XBUFFER (w->buffer), pt))
11524 b->clip_changed = 1;
11525 }
11526 }
11527 \f
11528
11529 /* Select FRAME to forward the values of frame-local variables into C
11530 variables so that the redisplay routines can access those values
11531 directly. */
11532
11533 static void
11534 select_frame_for_redisplay (Lisp_Object frame)
11535 {
11536 Lisp_Object tail, tem;
11537 Lisp_Object old = selected_frame;
11538 struct Lisp_Symbol *sym;
11539
11540 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11541
11542 selected_frame = frame;
11543
11544 do {
11545 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11546 if (CONSP (XCAR (tail))
11547 && (tem = XCAR (XCAR (tail)),
11548 SYMBOLP (tem))
11549 && (sym = indirect_variable (XSYMBOL (tem)),
11550 sym->redirect == SYMBOL_LOCALIZED)
11551 && sym->val.blv->frame_local)
11552 /* Use find_symbol_value rather than Fsymbol_value
11553 to avoid an error if it is void. */
11554 find_symbol_value (tem);
11555 } while (!EQ (frame, old) && (frame = old, 1));
11556 }
11557
11558
11559 #define STOP_POLLING \
11560 do { if (! polling_stopped_here) stop_polling (); \
11561 polling_stopped_here = 1; } while (0)
11562
11563 #define RESUME_POLLING \
11564 do { if (polling_stopped_here) start_polling (); \
11565 polling_stopped_here = 0; } while (0)
11566
11567
11568 /* Perhaps in the future avoid recentering windows if it
11569 is not necessary; currently that causes some problems. */
11570
11571 static void
11572 redisplay_internal (void)
11573 {
11574 struct window *w = XWINDOW (selected_window);
11575 struct window *sw;
11576 struct frame *fr;
11577 int pending;
11578 int must_finish = 0;
11579 struct text_pos tlbufpos, tlendpos;
11580 int number_of_visible_frames;
11581 int count, count1;
11582 struct frame *sf;
11583 int polling_stopped_here = 0;
11584 Lisp_Object old_frame = selected_frame;
11585
11586 /* Non-zero means redisplay has to consider all windows on all
11587 frames. Zero means, only selected_window is considered. */
11588 int consider_all_windows_p;
11589
11590 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11591
11592 /* No redisplay if running in batch mode or frame is not yet fully
11593 initialized, or redisplay is explicitly turned off by setting
11594 Vinhibit_redisplay. */
11595 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11596 || !NILP (Vinhibit_redisplay))
11597 return;
11598
11599 /* Don't examine these until after testing Vinhibit_redisplay.
11600 When Emacs is shutting down, perhaps because its connection to
11601 X has dropped, we should not look at them at all. */
11602 fr = XFRAME (w->frame);
11603 sf = SELECTED_FRAME ();
11604
11605 if (!fr->glyphs_initialized_p)
11606 return;
11607
11608 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11609 if (popup_activated ())
11610 return;
11611 #endif
11612
11613 /* I don't think this happens but let's be paranoid. */
11614 if (redisplaying_p)
11615 return;
11616
11617 /* Record a function that resets redisplaying_p to its old value
11618 when we leave this function. */
11619 count = SPECPDL_INDEX ();
11620 record_unwind_protect (unwind_redisplay,
11621 Fcons (make_number (redisplaying_p), selected_frame));
11622 ++redisplaying_p;
11623 specbind (Qinhibit_free_realized_faces, Qnil);
11624
11625 {
11626 Lisp_Object tail, frame;
11627
11628 FOR_EACH_FRAME (tail, frame)
11629 {
11630 struct frame *f = XFRAME (frame);
11631 f->already_hscrolled_p = 0;
11632 }
11633 }
11634
11635 retry:
11636 /* Remember the currently selected window. */
11637 sw = w;
11638
11639 if (!EQ (old_frame, selected_frame)
11640 && FRAME_LIVE_P (XFRAME (old_frame)))
11641 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11642 selected_frame and selected_window to be temporarily out-of-sync so
11643 when we come back here via `goto retry', we need to resync because we
11644 may need to run Elisp code (via prepare_menu_bars). */
11645 select_frame_for_redisplay (old_frame);
11646
11647 pending = 0;
11648 reconsider_clip_changes (w, current_buffer);
11649 last_escape_glyph_frame = NULL;
11650 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11651 last_glyphless_glyph_frame = NULL;
11652 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11653
11654 /* If new fonts have been loaded that make a glyph matrix adjustment
11655 necessary, do it. */
11656 if (fonts_changed_p)
11657 {
11658 adjust_glyphs (NULL);
11659 ++windows_or_buffers_changed;
11660 fonts_changed_p = 0;
11661 }
11662
11663 /* If face_change_count is non-zero, init_iterator will free all
11664 realized faces, which includes the faces referenced from current
11665 matrices. So, we can't reuse current matrices in this case. */
11666 if (face_change_count)
11667 ++windows_or_buffers_changed;
11668
11669 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11670 && FRAME_TTY (sf)->previous_frame != sf)
11671 {
11672 /* Since frames on a single ASCII terminal share the same
11673 display area, displaying a different frame means redisplay
11674 the whole thing. */
11675 windows_or_buffers_changed++;
11676 SET_FRAME_GARBAGED (sf);
11677 #ifndef DOS_NT
11678 set_tty_color_mode (FRAME_TTY (sf), sf);
11679 #endif
11680 FRAME_TTY (sf)->previous_frame = sf;
11681 }
11682
11683 /* Set the visible flags for all frames. Do this before checking
11684 for resized or garbaged frames; they want to know if their frames
11685 are visible. See the comment in frame.h for
11686 FRAME_SAMPLE_VISIBILITY. */
11687 {
11688 Lisp_Object tail, frame;
11689
11690 number_of_visible_frames = 0;
11691
11692 FOR_EACH_FRAME (tail, frame)
11693 {
11694 struct frame *f = XFRAME (frame);
11695
11696 FRAME_SAMPLE_VISIBILITY (f);
11697 if (FRAME_VISIBLE_P (f))
11698 ++number_of_visible_frames;
11699 clear_desired_matrices (f);
11700 }
11701 }
11702
11703 /* Notice any pending interrupt request to change frame size. */
11704 do_pending_window_change (1);
11705
11706 /* do_pending_window_change could change the selected_window due to
11707 frame resizing which makes the selected window too small. */
11708 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
11709 {
11710 sw = w;
11711 reconsider_clip_changes (w, current_buffer);
11712 }
11713
11714 /* Clear frames marked as garbaged. */
11715 if (frame_garbaged)
11716 clear_garbaged_frames ();
11717
11718 /* Build menubar and tool-bar items. */
11719 if (NILP (Vmemory_full))
11720 prepare_menu_bars ();
11721
11722 if (windows_or_buffers_changed)
11723 update_mode_lines++;
11724
11725 /* Detect case that we need to write or remove a star in the mode line. */
11726 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11727 {
11728 w->update_mode_line = Qt;
11729 if (buffer_shared > 1)
11730 update_mode_lines++;
11731 }
11732
11733 /* Avoid invocation of point motion hooks by `current_column' below. */
11734 count1 = SPECPDL_INDEX ();
11735 specbind (Qinhibit_point_motion_hooks, Qt);
11736
11737 /* If %c is in the mode line, update it if needed. */
11738 if (!NILP (w->column_number_displayed)
11739 /* This alternative quickly identifies a common case
11740 where no change is needed. */
11741 && !(PT == XFASTINT (w->last_point)
11742 && XFASTINT (w->last_modified) >= MODIFF
11743 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11744 && (XFASTINT (w->column_number_displayed) != current_column ()))
11745 w->update_mode_line = Qt;
11746
11747 unbind_to (count1, Qnil);
11748
11749 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11750
11751 /* The variable buffer_shared is set in redisplay_window and
11752 indicates that we redisplay a buffer in different windows. See
11753 there. */
11754 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11755 || cursor_type_changed);
11756
11757 /* If specs for an arrow have changed, do thorough redisplay
11758 to ensure we remove any arrow that should no longer exist. */
11759 if (overlay_arrows_changed_p ())
11760 consider_all_windows_p = windows_or_buffers_changed = 1;
11761
11762 /* Normally the message* functions will have already displayed and
11763 updated the echo area, but the frame may have been trashed, or
11764 the update may have been preempted, so display the echo area
11765 again here. Checking message_cleared_p captures the case that
11766 the echo area should be cleared. */
11767 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11768 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11769 || (message_cleared_p
11770 && minibuf_level == 0
11771 /* If the mini-window is currently selected, this means the
11772 echo-area doesn't show through. */
11773 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11774 {
11775 int window_height_changed_p = echo_area_display (0);
11776 must_finish = 1;
11777
11778 /* If we don't display the current message, don't clear the
11779 message_cleared_p flag, because, if we did, we wouldn't clear
11780 the echo area in the next redisplay which doesn't preserve
11781 the echo area. */
11782 if (!display_last_displayed_message_p)
11783 message_cleared_p = 0;
11784
11785 if (fonts_changed_p)
11786 goto retry;
11787 else if (window_height_changed_p)
11788 {
11789 consider_all_windows_p = 1;
11790 ++update_mode_lines;
11791 ++windows_or_buffers_changed;
11792
11793 /* If window configuration was changed, frames may have been
11794 marked garbaged. Clear them or we will experience
11795 surprises wrt scrolling. */
11796 if (frame_garbaged)
11797 clear_garbaged_frames ();
11798 }
11799 }
11800 else if (EQ (selected_window, minibuf_window)
11801 && (current_buffer->clip_changed
11802 || XFASTINT (w->last_modified) < MODIFF
11803 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11804 && resize_mini_window (w, 0))
11805 {
11806 /* Resized active mini-window to fit the size of what it is
11807 showing if its contents might have changed. */
11808 must_finish = 1;
11809 /* FIXME: this causes all frames to be updated, which seems unnecessary
11810 since only the current frame needs to be considered. This function needs
11811 to be rewritten with two variables, consider_all_windows and
11812 consider_all_frames. */
11813 consider_all_windows_p = 1;
11814 ++windows_or_buffers_changed;
11815 ++update_mode_lines;
11816
11817 /* If window configuration was changed, frames may have been
11818 marked garbaged. Clear them or we will experience
11819 surprises wrt scrolling. */
11820 if (frame_garbaged)
11821 clear_garbaged_frames ();
11822 }
11823
11824
11825 /* If showing the region, and mark has changed, we must redisplay
11826 the whole window. The assignment to this_line_start_pos prevents
11827 the optimization directly below this if-statement. */
11828 if (((!NILP (Vtransient_mark_mode)
11829 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11830 != !NILP (w->region_showing))
11831 || (!NILP (w->region_showing)
11832 && !EQ (w->region_showing,
11833 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
11834 CHARPOS (this_line_start_pos) = 0;
11835
11836 /* Optimize the case that only the line containing the cursor in the
11837 selected window has changed. Variables starting with this_ are
11838 set in display_line and record information about the line
11839 containing the cursor. */
11840 tlbufpos = this_line_start_pos;
11841 tlendpos = this_line_end_pos;
11842 if (!consider_all_windows_p
11843 && CHARPOS (tlbufpos) > 0
11844 && NILP (w->update_mode_line)
11845 && !current_buffer->clip_changed
11846 && !current_buffer->prevent_redisplay_optimizations_p
11847 && FRAME_VISIBLE_P (XFRAME (w->frame))
11848 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11849 /* Make sure recorded data applies to current buffer, etc. */
11850 && this_line_buffer == current_buffer
11851 && current_buffer == XBUFFER (w->buffer)
11852 && NILP (w->force_start)
11853 && NILP (w->optional_new_start)
11854 /* Point must be on the line that we have info recorded about. */
11855 && PT >= CHARPOS (tlbufpos)
11856 && PT <= Z - CHARPOS (tlendpos)
11857 /* All text outside that line, including its final newline,
11858 must be unchanged. */
11859 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11860 CHARPOS (tlendpos)))
11861 {
11862 if (CHARPOS (tlbufpos) > BEGV
11863 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11864 && (CHARPOS (tlbufpos) == ZV
11865 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11866 /* Former continuation line has disappeared by becoming empty. */
11867 goto cancel;
11868 else if (XFASTINT (w->last_modified) < MODIFF
11869 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11870 || MINI_WINDOW_P (w))
11871 {
11872 /* We have to handle the case of continuation around a
11873 wide-column character (see the comment in indent.c around
11874 line 1340).
11875
11876 For instance, in the following case:
11877
11878 -------- Insert --------
11879 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11880 J_I_ ==> J_I_ `^^' are cursors.
11881 ^^ ^^
11882 -------- --------
11883
11884 As we have to redraw the line above, we cannot use this
11885 optimization. */
11886
11887 struct it it;
11888 int line_height_before = this_line_pixel_height;
11889
11890 /* Note that start_display will handle the case that the
11891 line starting at tlbufpos is a continuation line. */
11892 start_display (&it, w, tlbufpos);
11893
11894 /* Implementation note: It this still necessary? */
11895 if (it.current_x != this_line_start_x)
11896 goto cancel;
11897
11898 TRACE ((stderr, "trying display optimization 1\n"));
11899 w->cursor.vpos = -1;
11900 overlay_arrow_seen = 0;
11901 it.vpos = this_line_vpos;
11902 it.current_y = this_line_y;
11903 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11904 display_line (&it);
11905
11906 /* If line contains point, is not continued,
11907 and ends at same distance from eob as before, we win. */
11908 if (w->cursor.vpos >= 0
11909 /* Line is not continued, otherwise this_line_start_pos
11910 would have been set to 0 in display_line. */
11911 && CHARPOS (this_line_start_pos)
11912 /* Line ends as before. */
11913 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11914 /* Line has same height as before. Otherwise other lines
11915 would have to be shifted up or down. */
11916 && this_line_pixel_height == line_height_before)
11917 {
11918 /* If this is not the window's last line, we must adjust
11919 the charstarts of the lines below. */
11920 if (it.current_y < it.last_visible_y)
11921 {
11922 struct glyph_row *row
11923 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11924 EMACS_INT delta, delta_bytes;
11925
11926 /* We used to distinguish between two cases here,
11927 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11928 when the line ends in a newline or the end of the
11929 buffer's accessible portion. But both cases did
11930 the same, so they were collapsed. */
11931 delta = (Z
11932 - CHARPOS (tlendpos)
11933 - MATRIX_ROW_START_CHARPOS (row));
11934 delta_bytes = (Z_BYTE
11935 - BYTEPOS (tlendpos)
11936 - MATRIX_ROW_START_BYTEPOS (row));
11937
11938 increment_matrix_positions (w->current_matrix,
11939 this_line_vpos + 1,
11940 w->current_matrix->nrows,
11941 delta, delta_bytes);
11942 }
11943
11944 /* If this row displays text now but previously didn't,
11945 or vice versa, w->window_end_vpos may have to be
11946 adjusted. */
11947 if ((it.glyph_row - 1)->displays_text_p)
11948 {
11949 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11950 XSETINT (w->window_end_vpos, this_line_vpos);
11951 }
11952 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11953 && this_line_vpos > 0)
11954 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11955 w->window_end_valid = Qnil;
11956
11957 /* Update hint: No need to try to scroll in update_window. */
11958 w->desired_matrix->no_scrolling_p = 1;
11959
11960 #if GLYPH_DEBUG
11961 *w->desired_matrix->method = 0;
11962 debug_method_add (w, "optimization 1");
11963 #endif
11964 #ifdef HAVE_WINDOW_SYSTEM
11965 update_window_fringes (w, 0);
11966 #endif
11967 goto update;
11968 }
11969 else
11970 goto cancel;
11971 }
11972 else if (/* Cursor position hasn't changed. */
11973 PT == XFASTINT (w->last_point)
11974 /* Make sure the cursor was last displayed
11975 in this window. Otherwise we have to reposition it. */
11976 && 0 <= w->cursor.vpos
11977 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11978 {
11979 if (!must_finish)
11980 {
11981 do_pending_window_change (1);
11982 /* If selected_window changed, redisplay again. */
11983 if (WINDOWP (selected_window)
11984 && (w = XWINDOW (selected_window)) != sw)
11985 goto retry;
11986
11987 /* We used to always goto end_of_redisplay here, but this
11988 isn't enough if we have a blinking cursor. */
11989 if (w->cursor_off_p == w->last_cursor_off_p)
11990 goto end_of_redisplay;
11991 }
11992 goto update;
11993 }
11994 /* If highlighting the region, or if the cursor is in the echo area,
11995 then we can't just move the cursor. */
11996 else if (! (!NILP (Vtransient_mark_mode)
11997 && !NILP (BVAR (current_buffer, mark_active)))
11998 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
11999 || highlight_nonselected_windows)
12000 && NILP (w->region_showing)
12001 && NILP (Vshow_trailing_whitespace)
12002 && !cursor_in_echo_area)
12003 {
12004 struct it it;
12005 struct glyph_row *row;
12006
12007 /* Skip from tlbufpos to PT and see where it is. Note that
12008 PT may be in invisible text. If so, we will end at the
12009 next visible position. */
12010 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12011 NULL, DEFAULT_FACE_ID);
12012 it.current_x = this_line_start_x;
12013 it.current_y = this_line_y;
12014 it.vpos = this_line_vpos;
12015
12016 /* The call to move_it_to stops in front of PT, but
12017 moves over before-strings. */
12018 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12019
12020 if (it.vpos == this_line_vpos
12021 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12022 row->enabled_p))
12023 {
12024 xassert (this_line_vpos == it.vpos);
12025 xassert (this_line_y == it.current_y);
12026 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12027 #if GLYPH_DEBUG
12028 *w->desired_matrix->method = 0;
12029 debug_method_add (w, "optimization 3");
12030 #endif
12031 goto update;
12032 }
12033 else
12034 goto cancel;
12035 }
12036
12037 cancel:
12038 /* Text changed drastically or point moved off of line. */
12039 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12040 }
12041
12042 CHARPOS (this_line_start_pos) = 0;
12043 consider_all_windows_p |= buffer_shared > 1;
12044 ++clear_face_cache_count;
12045 #ifdef HAVE_WINDOW_SYSTEM
12046 ++clear_image_cache_count;
12047 #endif
12048
12049 /* Build desired matrices, and update the display. If
12050 consider_all_windows_p is non-zero, do it for all windows on all
12051 frames. Otherwise do it for selected_window, only. */
12052
12053 if (consider_all_windows_p)
12054 {
12055 Lisp_Object tail, frame;
12056
12057 FOR_EACH_FRAME (tail, frame)
12058 XFRAME (frame)->updated_p = 0;
12059
12060 /* Recompute # windows showing selected buffer. This will be
12061 incremented each time such a window is displayed. */
12062 buffer_shared = 0;
12063
12064 FOR_EACH_FRAME (tail, frame)
12065 {
12066 struct frame *f = XFRAME (frame);
12067
12068 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12069 {
12070 if (! EQ (frame, selected_frame))
12071 /* Select the frame, for the sake of frame-local
12072 variables. */
12073 select_frame_for_redisplay (frame);
12074
12075 /* Mark all the scroll bars to be removed; we'll redeem
12076 the ones we want when we redisplay their windows. */
12077 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12078 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12079
12080 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12081 redisplay_windows (FRAME_ROOT_WINDOW (f));
12082
12083 /* The X error handler may have deleted that frame. */
12084 if (!FRAME_LIVE_P (f))
12085 continue;
12086
12087 /* Any scroll bars which redisplay_windows should have
12088 nuked should now go away. */
12089 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12090 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12091
12092 /* If fonts changed, display again. */
12093 /* ??? rms: I suspect it is a mistake to jump all the way
12094 back to retry here. It should just retry this frame. */
12095 if (fonts_changed_p)
12096 goto retry;
12097
12098 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12099 {
12100 /* See if we have to hscroll. */
12101 if (!f->already_hscrolled_p)
12102 {
12103 f->already_hscrolled_p = 1;
12104 if (hscroll_windows (f->root_window))
12105 goto retry;
12106 }
12107
12108 /* Prevent various kinds of signals during display
12109 update. stdio is not robust about handling
12110 signals, which can cause an apparent I/O
12111 error. */
12112 if (interrupt_input)
12113 unrequest_sigio ();
12114 STOP_POLLING;
12115
12116 /* Update the display. */
12117 set_window_update_flags (XWINDOW (f->root_window), 1);
12118 pending |= update_frame (f, 0, 0);
12119 f->updated_p = 1;
12120 }
12121 }
12122 }
12123
12124 if (!EQ (old_frame, selected_frame)
12125 && FRAME_LIVE_P (XFRAME (old_frame)))
12126 /* We played a bit fast-and-loose above and allowed selected_frame
12127 and selected_window to be temporarily out-of-sync but let's make
12128 sure this stays contained. */
12129 select_frame_for_redisplay (old_frame);
12130 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12131
12132 if (!pending)
12133 {
12134 /* Do the mark_window_display_accurate after all windows have
12135 been redisplayed because this call resets flags in buffers
12136 which are needed for proper redisplay. */
12137 FOR_EACH_FRAME (tail, frame)
12138 {
12139 struct frame *f = XFRAME (frame);
12140 if (f->updated_p)
12141 {
12142 mark_window_display_accurate (f->root_window, 1);
12143 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12144 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12145 }
12146 }
12147 }
12148 }
12149 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12150 {
12151 Lisp_Object mini_window;
12152 struct frame *mini_frame;
12153
12154 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12155 /* Use list_of_error, not Qerror, so that
12156 we catch only errors and don't run the debugger. */
12157 internal_condition_case_1 (redisplay_window_1, selected_window,
12158 list_of_error,
12159 redisplay_window_error);
12160
12161 /* Compare desired and current matrices, perform output. */
12162
12163 update:
12164 /* If fonts changed, display again. */
12165 if (fonts_changed_p)
12166 goto retry;
12167
12168 /* Prevent various kinds of signals during display update.
12169 stdio is not robust about handling signals,
12170 which can cause an apparent I/O error. */
12171 if (interrupt_input)
12172 unrequest_sigio ();
12173 STOP_POLLING;
12174
12175 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12176 {
12177 if (hscroll_windows (selected_window))
12178 goto retry;
12179
12180 XWINDOW (selected_window)->must_be_updated_p = 1;
12181 pending = update_frame (sf, 0, 0);
12182 }
12183
12184 /* We may have called echo_area_display at the top of this
12185 function. If the echo area is on another frame, that may
12186 have put text on a frame other than the selected one, so the
12187 above call to update_frame would not have caught it. Catch
12188 it here. */
12189 mini_window = FRAME_MINIBUF_WINDOW (sf);
12190 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12191
12192 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12193 {
12194 XWINDOW (mini_window)->must_be_updated_p = 1;
12195 pending |= update_frame (mini_frame, 0, 0);
12196 if (!pending && hscroll_windows (mini_window))
12197 goto retry;
12198 }
12199 }
12200
12201 /* If display was paused because of pending input, make sure we do a
12202 thorough update the next time. */
12203 if (pending)
12204 {
12205 /* Prevent the optimization at the beginning of
12206 redisplay_internal that tries a single-line update of the
12207 line containing the cursor in the selected window. */
12208 CHARPOS (this_line_start_pos) = 0;
12209
12210 /* Let the overlay arrow be updated the next time. */
12211 update_overlay_arrows (0);
12212
12213 /* If we pause after scrolling, some rows in the current
12214 matrices of some windows are not valid. */
12215 if (!WINDOW_FULL_WIDTH_P (w)
12216 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12217 update_mode_lines = 1;
12218 }
12219 else
12220 {
12221 if (!consider_all_windows_p)
12222 {
12223 /* This has already been done above if
12224 consider_all_windows_p is set. */
12225 mark_window_display_accurate_1 (w, 1);
12226
12227 /* Say overlay arrows are up to date. */
12228 update_overlay_arrows (1);
12229
12230 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12231 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12232 }
12233
12234 update_mode_lines = 0;
12235 windows_or_buffers_changed = 0;
12236 cursor_type_changed = 0;
12237 }
12238
12239 /* Start SIGIO interrupts coming again. Having them off during the
12240 code above makes it less likely one will discard output, but not
12241 impossible, since there might be stuff in the system buffer here.
12242 But it is much hairier to try to do anything about that. */
12243 if (interrupt_input)
12244 request_sigio ();
12245 RESUME_POLLING;
12246
12247 /* If a frame has become visible which was not before, redisplay
12248 again, so that we display it. Expose events for such a frame
12249 (which it gets when becoming visible) don't call the parts of
12250 redisplay constructing glyphs, so simply exposing a frame won't
12251 display anything in this case. So, we have to display these
12252 frames here explicitly. */
12253 if (!pending)
12254 {
12255 Lisp_Object tail, frame;
12256 int new_count = 0;
12257
12258 FOR_EACH_FRAME (tail, frame)
12259 {
12260 int this_is_visible = 0;
12261
12262 if (XFRAME (frame)->visible)
12263 this_is_visible = 1;
12264 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12265 if (XFRAME (frame)->visible)
12266 this_is_visible = 1;
12267
12268 if (this_is_visible)
12269 new_count++;
12270 }
12271
12272 if (new_count != number_of_visible_frames)
12273 windows_or_buffers_changed++;
12274 }
12275
12276 /* Change frame size now if a change is pending. */
12277 do_pending_window_change (1);
12278
12279 /* If we just did a pending size change, or have additional
12280 visible frames, or selected_window changed, redisplay again. */
12281 if ((windows_or_buffers_changed && !pending)
12282 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12283 goto retry;
12284
12285 /* Clear the face and image caches.
12286
12287 We used to do this only if consider_all_windows_p. But the cache
12288 needs to be cleared if a timer creates images in the current
12289 buffer (e.g. the test case in Bug#6230). */
12290
12291 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12292 {
12293 clear_face_cache (0);
12294 clear_face_cache_count = 0;
12295 }
12296
12297 #ifdef HAVE_WINDOW_SYSTEM
12298 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12299 {
12300 clear_image_caches (Qnil);
12301 clear_image_cache_count = 0;
12302 }
12303 #endif /* HAVE_WINDOW_SYSTEM */
12304
12305 end_of_redisplay:
12306 unbind_to (count, Qnil);
12307 RESUME_POLLING;
12308 }
12309
12310
12311 /* Redisplay, but leave alone any recent echo area message unless
12312 another message has been requested in its place.
12313
12314 This is useful in situations where you need to redisplay but no
12315 user action has occurred, making it inappropriate for the message
12316 area to be cleared. See tracking_off and
12317 wait_reading_process_output for examples of these situations.
12318
12319 FROM_WHERE is an integer saying from where this function was
12320 called. This is useful for debugging. */
12321
12322 void
12323 redisplay_preserve_echo_area (int from_where)
12324 {
12325 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12326
12327 if (!NILP (echo_area_buffer[1]))
12328 {
12329 /* We have a previously displayed message, but no current
12330 message. Redisplay the previous message. */
12331 display_last_displayed_message_p = 1;
12332 redisplay_internal ();
12333 display_last_displayed_message_p = 0;
12334 }
12335 else
12336 redisplay_internal ();
12337
12338 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12339 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12340 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12341 }
12342
12343
12344 /* Function registered with record_unwind_protect in
12345 redisplay_internal. Reset redisplaying_p to the value it had
12346 before redisplay_internal was called, and clear
12347 prevent_freeing_realized_faces_p. It also selects the previously
12348 selected frame, unless it has been deleted (by an X connection
12349 failure during redisplay, for example). */
12350
12351 static Lisp_Object
12352 unwind_redisplay (Lisp_Object val)
12353 {
12354 Lisp_Object old_redisplaying_p, old_frame;
12355
12356 old_redisplaying_p = XCAR (val);
12357 redisplaying_p = XFASTINT (old_redisplaying_p);
12358 old_frame = XCDR (val);
12359 if (! EQ (old_frame, selected_frame)
12360 && FRAME_LIVE_P (XFRAME (old_frame)))
12361 select_frame_for_redisplay (old_frame);
12362 return Qnil;
12363 }
12364
12365
12366 /* Mark the display of window W as accurate or inaccurate. If
12367 ACCURATE_P is non-zero mark display of W as accurate. If
12368 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12369 redisplay_internal is called. */
12370
12371 static void
12372 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12373 {
12374 if (BUFFERP (w->buffer))
12375 {
12376 struct buffer *b = XBUFFER (w->buffer);
12377
12378 w->last_modified
12379 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12380 w->last_overlay_modified
12381 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12382 w->last_had_star
12383 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12384
12385 if (accurate_p)
12386 {
12387 b->clip_changed = 0;
12388 b->prevent_redisplay_optimizations_p = 0;
12389
12390 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12391 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12392 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12393 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12394
12395 w->current_matrix->buffer = b;
12396 w->current_matrix->begv = BUF_BEGV (b);
12397 w->current_matrix->zv = BUF_ZV (b);
12398
12399 w->last_cursor = w->cursor;
12400 w->last_cursor_off_p = w->cursor_off_p;
12401
12402 if (w == XWINDOW (selected_window))
12403 w->last_point = make_number (BUF_PT (b));
12404 else
12405 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12406 }
12407 }
12408
12409 if (accurate_p)
12410 {
12411 w->window_end_valid = w->buffer;
12412 w->update_mode_line = Qnil;
12413 }
12414 }
12415
12416
12417 /* Mark the display of windows in the window tree rooted at WINDOW as
12418 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12419 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12420 be redisplayed the next time redisplay_internal is called. */
12421
12422 void
12423 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12424 {
12425 struct window *w;
12426
12427 for (; !NILP (window); window = w->next)
12428 {
12429 w = XWINDOW (window);
12430 mark_window_display_accurate_1 (w, accurate_p);
12431
12432 if (!NILP (w->vchild))
12433 mark_window_display_accurate (w->vchild, accurate_p);
12434 if (!NILP (w->hchild))
12435 mark_window_display_accurate (w->hchild, accurate_p);
12436 }
12437
12438 if (accurate_p)
12439 {
12440 update_overlay_arrows (1);
12441 }
12442 else
12443 {
12444 /* Force a thorough redisplay the next time by setting
12445 last_arrow_position and last_arrow_string to t, which is
12446 unequal to any useful value of Voverlay_arrow_... */
12447 update_overlay_arrows (-1);
12448 }
12449 }
12450
12451
12452 /* Return value in display table DP (Lisp_Char_Table *) for character
12453 C. Since a display table doesn't have any parent, we don't have to
12454 follow parent. Do not call this function directly but use the
12455 macro DISP_CHAR_VECTOR. */
12456
12457 Lisp_Object
12458 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12459 {
12460 Lisp_Object val;
12461
12462 if (ASCII_CHAR_P (c))
12463 {
12464 val = dp->ascii;
12465 if (SUB_CHAR_TABLE_P (val))
12466 val = XSUB_CHAR_TABLE (val)->contents[c];
12467 }
12468 else
12469 {
12470 Lisp_Object table;
12471
12472 XSETCHAR_TABLE (table, dp);
12473 val = char_table_ref (table, c);
12474 }
12475 if (NILP (val))
12476 val = dp->defalt;
12477 return val;
12478 }
12479
12480
12481 \f
12482 /***********************************************************************
12483 Window Redisplay
12484 ***********************************************************************/
12485
12486 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12487
12488 static void
12489 redisplay_windows (Lisp_Object window)
12490 {
12491 while (!NILP (window))
12492 {
12493 struct window *w = XWINDOW (window);
12494
12495 if (!NILP (w->hchild))
12496 redisplay_windows (w->hchild);
12497 else if (!NILP (w->vchild))
12498 redisplay_windows (w->vchild);
12499 else if (!NILP (w->buffer))
12500 {
12501 displayed_buffer = XBUFFER (w->buffer);
12502 /* Use list_of_error, not Qerror, so that
12503 we catch only errors and don't run the debugger. */
12504 internal_condition_case_1 (redisplay_window_0, window,
12505 list_of_error,
12506 redisplay_window_error);
12507 }
12508
12509 window = w->next;
12510 }
12511 }
12512
12513 static Lisp_Object
12514 redisplay_window_error (Lisp_Object ignore)
12515 {
12516 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12517 return Qnil;
12518 }
12519
12520 static Lisp_Object
12521 redisplay_window_0 (Lisp_Object window)
12522 {
12523 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12524 redisplay_window (window, 0);
12525 return Qnil;
12526 }
12527
12528 static Lisp_Object
12529 redisplay_window_1 (Lisp_Object window)
12530 {
12531 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12532 redisplay_window (window, 1);
12533 return Qnil;
12534 }
12535 \f
12536
12537 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12538 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12539 which positions recorded in ROW differ from current buffer
12540 positions.
12541
12542 Return 0 if cursor is not on this row, 1 otherwise. */
12543
12544 static int
12545 set_cursor_from_row (struct window *w, struct glyph_row *row,
12546 struct glyph_matrix *matrix,
12547 EMACS_INT delta, EMACS_INT delta_bytes,
12548 int dy, int dvpos)
12549 {
12550 struct glyph *glyph = row->glyphs[TEXT_AREA];
12551 struct glyph *end = glyph + row->used[TEXT_AREA];
12552 struct glyph *cursor = NULL;
12553 /* The last known character position in row. */
12554 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12555 int x = row->x;
12556 EMACS_INT pt_old = PT - delta;
12557 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12558 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12559 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12560 /* A glyph beyond the edge of TEXT_AREA which we should never
12561 touch. */
12562 struct glyph *glyphs_end = end;
12563 /* Non-zero means we've found a match for cursor position, but that
12564 glyph has the avoid_cursor_p flag set. */
12565 int match_with_avoid_cursor = 0;
12566 /* Non-zero means we've seen at least one glyph that came from a
12567 display string. */
12568 int string_seen = 0;
12569 /* Largest and smalles buffer positions seen so far during scan of
12570 glyph row. */
12571 EMACS_INT bpos_max = pos_before;
12572 EMACS_INT bpos_min = pos_after;
12573 /* Last buffer position covered by an overlay string with an integer
12574 `cursor' property. */
12575 EMACS_INT bpos_covered = 0;
12576
12577 /* Skip over glyphs not having an object at the start and the end of
12578 the row. These are special glyphs like truncation marks on
12579 terminal frames. */
12580 if (row->displays_text_p)
12581 {
12582 if (!row->reversed_p)
12583 {
12584 while (glyph < end
12585 && INTEGERP (glyph->object)
12586 && glyph->charpos < 0)
12587 {
12588 x += glyph->pixel_width;
12589 ++glyph;
12590 }
12591 while (end > glyph
12592 && INTEGERP ((end - 1)->object)
12593 /* CHARPOS is zero for blanks and stretch glyphs
12594 inserted by extend_face_to_end_of_line. */
12595 && (end - 1)->charpos <= 0)
12596 --end;
12597 glyph_before = glyph - 1;
12598 glyph_after = end;
12599 }
12600 else
12601 {
12602 struct glyph *g;
12603
12604 /* If the glyph row is reversed, we need to process it from back
12605 to front, so swap the edge pointers. */
12606 glyphs_end = end = glyph - 1;
12607 glyph += row->used[TEXT_AREA] - 1;
12608
12609 while (glyph > end + 1
12610 && INTEGERP (glyph->object)
12611 && glyph->charpos < 0)
12612 {
12613 --glyph;
12614 x -= glyph->pixel_width;
12615 }
12616 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12617 --glyph;
12618 /* By default, in reversed rows we put the cursor on the
12619 rightmost (first in the reading order) glyph. */
12620 for (g = end + 1; g < glyph; g++)
12621 x += g->pixel_width;
12622 while (end < glyph
12623 && INTEGERP ((end + 1)->object)
12624 && (end + 1)->charpos <= 0)
12625 ++end;
12626 glyph_before = glyph + 1;
12627 glyph_after = end;
12628 }
12629 }
12630 else if (row->reversed_p)
12631 {
12632 /* In R2L rows that don't display text, put the cursor on the
12633 rightmost glyph. Case in point: an empty last line that is
12634 part of an R2L paragraph. */
12635 cursor = end - 1;
12636 /* Avoid placing the cursor on the last glyph of the row, where
12637 on terminal frames we hold the vertical border between
12638 adjacent windows. */
12639 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12640 && !WINDOW_RIGHTMOST_P (w)
12641 && cursor == row->glyphs[LAST_AREA] - 1)
12642 cursor--;
12643 x = -1; /* will be computed below, at label compute_x */
12644 }
12645
12646 /* Step 1: Try to find the glyph whose character position
12647 corresponds to point. If that's not possible, find 2 glyphs
12648 whose character positions are the closest to point, one before
12649 point, the other after it. */
12650 if (!row->reversed_p)
12651 while (/* not marched to end of glyph row */
12652 glyph < end
12653 /* glyph was not inserted by redisplay for internal purposes */
12654 && !INTEGERP (glyph->object))
12655 {
12656 if (BUFFERP (glyph->object))
12657 {
12658 EMACS_INT dpos = glyph->charpos - pt_old;
12659
12660 if (glyph->charpos > bpos_max)
12661 bpos_max = glyph->charpos;
12662 if (glyph->charpos < bpos_min)
12663 bpos_min = glyph->charpos;
12664 if (!glyph->avoid_cursor_p)
12665 {
12666 /* If we hit point, we've found the glyph on which to
12667 display the cursor. */
12668 if (dpos == 0)
12669 {
12670 match_with_avoid_cursor = 0;
12671 break;
12672 }
12673 /* See if we've found a better approximation to
12674 POS_BEFORE or to POS_AFTER. Note that we want the
12675 first (leftmost) glyph of all those that are the
12676 closest from below, and the last (rightmost) of all
12677 those from above. */
12678 if (0 > dpos && dpos > pos_before - pt_old)
12679 {
12680 pos_before = glyph->charpos;
12681 glyph_before = glyph;
12682 }
12683 else if (0 < dpos && dpos <= pos_after - pt_old)
12684 {
12685 pos_after = glyph->charpos;
12686 glyph_after = glyph;
12687 }
12688 }
12689 else if (dpos == 0)
12690 match_with_avoid_cursor = 1;
12691 }
12692 else if (STRINGP (glyph->object))
12693 {
12694 Lisp_Object chprop;
12695 EMACS_INT glyph_pos = glyph->charpos;
12696
12697 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12698 glyph->object);
12699 if (INTEGERP (chprop))
12700 {
12701 bpos_covered = bpos_max + XINT (chprop);
12702 /* If the `cursor' property covers buffer positions up
12703 to and including point, we should display cursor on
12704 this glyph. Note that overlays and text properties
12705 with string values stop bidi reordering, so every
12706 buffer position to the left of the string is always
12707 smaller than any position to the right of the
12708 string. Therefore, if a `cursor' property on one
12709 of the string's characters has an integer value, we
12710 will break out of the loop below _before_ we get to
12711 the position match above. IOW, integer values of
12712 the `cursor' property override the "exact match for
12713 point" strategy of positioning the cursor. */
12714 /* Implementation note: bpos_max == pt_old when, e.g.,
12715 we are in an empty line, where bpos_max is set to
12716 MATRIX_ROW_START_CHARPOS, see above. */
12717 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12718 {
12719 cursor = glyph;
12720 break;
12721 }
12722 }
12723
12724 string_seen = 1;
12725 }
12726 x += glyph->pixel_width;
12727 ++glyph;
12728 }
12729 else if (glyph > end) /* row is reversed */
12730 while (!INTEGERP (glyph->object))
12731 {
12732 if (BUFFERP (glyph->object))
12733 {
12734 EMACS_INT dpos = glyph->charpos - pt_old;
12735
12736 if (glyph->charpos > bpos_max)
12737 bpos_max = glyph->charpos;
12738 if (glyph->charpos < bpos_min)
12739 bpos_min = glyph->charpos;
12740 if (!glyph->avoid_cursor_p)
12741 {
12742 if (dpos == 0)
12743 {
12744 match_with_avoid_cursor = 0;
12745 break;
12746 }
12747 if (0 > dpos && dpos > pos_before - pt_old)
12748 {
12749 pos_before = glyph->charpos;
12750 glyph_before = glyph;
12751 }
12752 else if (0 < dpos && dpos <= pos_after - pt_old)
12753 {
12754 pos_after = glyph->charpos;
12755 glyph_after = glyph;
12756 }
12757 }
12758 else if (dpos == 0)
12759 match_with_avoid_cursor = 1;
12760 }
12761 else if (STRINGP (glyph->object))
12762 {
12763 Lisp_Object chprop;
12764 EMACS_INT glyph_pos = glyph->charpos;
12765
12766 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12767 glyph->object);
12768 if (INTEGERP (chprop))
12769 {
12770 bpos_covered = bpos_max + XINT (chprop);
12771 /* If the `cursor' property covers buffer positions up
12772 to and including point, we should display cursor on
12773 this glyph. */
12774 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12775 {
12776 cursor = glyph;
12777 break;
12778 }
12779 }
12780 string_seen = 1;
12781 }
12782 --glyph;
12783 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12784 {
12785 x--; /* can't use any pixel_width */
12786 break;
12787 }
12788 x -= glyph->pixel_width;
12789 }
12790
12791 /* Step 2: If we didn't find an exact match for point, we need to
12792 look for a proper place to put the cursor among glyphs between
12793 GLYPH_BEFORE and GLYPH_AFTER. */
12794 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12795 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12796 && bpos_covered < pt_old)
12797 {
12798 /* An empty line has a single glyph whose OBJECT is zero and
12799 whose CHARPOS is the position of a newline on that line.
12800 Note that on a TTY, there are more glyphs after that, which
12801 were produced by extend_face_to_end_of_line, but their
12802 CHARPOS is zero or negative. */
12803 int empty_line_p =
12804 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12805 && INTEGERP (glyph->object) && glyph->charpos > 0;
12806
12807 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12808 {
12809 EMACS_INT ellipsis_pos;
12810
12811 /* Scan back over the ellipsis glyphs. */
12812 if (!row->reversed_p)
12813 {
12814 ellipsis_pos = (glyph - 1)->charpos;
12815 while (glyph > row->glyphs[TEXT_AREA]
12816 && (glyph - 1)->charpos == ellipsis_pos)
12817 glyph--, x -= glyph->pixel_width;
12818 /* That loop always goes one position too far, including
12819 the glyph before the ellipsis. So scan forward over
12820 that one. */
12821 x += glyph->pixel_width;
12822 glyph++;
12823 }
12824 else /* row is reversed */
12825 {
12826 ellipsis_pos = (glyph + 1)->charpos;
12827 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12828 && (glyph + 1)->charpos == ellipsis_pos)
12829 glyph++, x += glyph->pixel_width;
12830 x -= glyph->pixel_width;
12831 glyph--;
12832 }
12833 }
12834 else if (match_with_avoid_cursor
12835 /* A truncated row may not include PT among its
12836 character positions. Setting the cursor inside the
12837 scroll margin will trigger recalculation of hscroll
12838 in hscroll_window_tree. */
12839 || (row->truncated_on_left_p && pt_old < bpos_min)
12840 || (row->truncated_on_right_p && pt_old > bpos_max)
12841 /* Zero-width characters produce no glyphs. */
12842 || (!string_seen
12843 && !empty_line_p
12844 && (row->reversed_p
12845 ? glyph_after > glyphs_end
12846 : glyph_after < glyphs_end)))
12847 {
12848 cursor = glyph_after;
12849 x = -1;
12850 }
12851 else if (string_seen)
12852 {
12853 int incr = row->reversed_p ? -1 : +1;
12854
12855 /* Need to find the glyph that came out of a string which is
12856 present at point. That glyph is somewhere between
12857 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12858 positioned between POS_BEFORE and POS_AFTER in the
12859 buffer. */
12860 struct glyph *start, *stop;
12861 EMACS_INT pos = pos_before;
12862
12863 x = -1;
12864
12865 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
12866 correspond to POS_BEFORE and POS_AFTER, respectively. We
12867 need START and STOP in the order that corresponds to the
12868 row's direction as given by its reversed_p flag. If the
12869 directionality of characters between POS_BEFORE and
12870 POS_AFTER is the opposite of the row's base direction,
12871 these characters will have been reordered for display,
12872 and we need to reverse START and STOP. */
12873 if (!row->reversed_p)
12874 {
12875 start = min (glyph_before, glyph_after);
12876 stop = max (glyph_before, glyph_after);
12877 }
12878 else
12879 {
12880 start = max (glyph_before, glyph_after);
12881 stop = min (glyph_before, glyph_after);
12882 }
12883 for (glyph = start + incr;
12884 row->reversed_p ? glyph > stop : glyph < stop; )
12885 {
12886
12887 /* Any glyphs that come from the buffer are here because
12888 of bidi reordering. Skip them, and only pay
12889 attention to glyphs that came from some string. */
12890 if (STRINGP (glyph->object))
12891 {
12892 Lisp_Object str;
12893 EMACS_INT tem;
12894
12895 str = glyph->object;
12896 tem = string_buffer_position_lim (str, pos, pos_after, 0);
12897 if (tem == 0 /* from overlay */
12898 || pos <= tem)
12899 {
12900 /* If the string from which this glyph came is
12901 found in the buffer at point, then we've
12902 found the glyph we've been looking for. If
12903 it comes from an overlay (tem == 0), and it
12904 has the `cursor' property on one of its
12905 glyphs, record that glyph as a candidate for
12906 displaying the cursor. (As in the
12907 unidirectional version, we will display the
12908 cursor on the last candidate we find.) */
12909 if (tem == 0 || tem == pt_old)
12910 {
12911 /* The glyphs from this string could have
12912 been reordered. Find the one with the
12913 smallest string position. Or there could
12914 be a character in the string with the
12915 `cursor' property, which means display
12916 cursor on that character's glyph. */
12917 EMACS_INT strpos = glyph->charpos;
12918
12919 if (tem)
12920 cursor = glyph;
12921 for ( ;
12922 (row->reversed_p ? glyph > stop : glyph < stop)
12923 && EQ (glyph->object, str);
12924 glyph += incr)
12925 {
12926 Lisp_Object cprop;
12927 EMACS_INT gpos = glyph->charpos;
12928
12929 cprop = Fget_char_property (make_number (gpos),
12930 Qcursor,
12931 glyph->object);
12932 if (!NILP (cprop))
12933 {
12934 cursor = glyph;
12935 break;
12936 }
12937 if (tem && glyph->charpos < strpos)
12938 {
12939 strpos = glyph->charpos;
12940 cursor = glyph;
12941 }
12942 }
12943
12944 if (tem == pt_old)
12945 goto compute_x;
12946 }
12947 if (tem)
12948 pos = tem + 1; /* don't find previous instances */
12949 }
12950 /* This string is not what we want; skip all of the
12951 glyphs that came from it. */
12952 while ((row->reversed_p ? glyph > stop : glyph < stop)
12953 && EQ (glyph->object, str))
12954 glyph += incr;
12955 }
12956 else
12957 glyph += incr;
12958 }
12959
12960 /* If we reached the end of the line, and END was from a string,
12961 the cursor is not on this line. */
12962 if (cursor == NULL
12963 && (row->reversed_p ? glyph <= end : glyph >= end)
12964 && STRINGP (end->object)
12965 && row->continued_p)
12966 return 0;
12967 }
12968 }
12969
12970 compute_x:
12971 if (cursor != NULL)
12972 glyph = cursor;
12973 if (x < 0)
12974 {
12975 struct glyph *g;
12976
12977 /* Need to compute x that corresponds to GLYPH. */
12978 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12979 {
12980 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12981 abort ();
12982 x += g->pixel_width;
12983 }
12984 }
12985
12986 /* ROW could be part of a continued line, which, under bidi
12987 reordering, might have other rows whose start and end charpos
12988 occlude point. Only set w->cursor if we found a better
12989 approximation to the cursor position than we have from previously
12990 examined candidate rows belonging to the same continued line. */
12991 if (/* we already have a candidate row */
12992 w->cursor.vpos >= 0
12993 /* that candidate is not the row we are processing */
12994 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12995 /* the row we are processing is part of a continued line */
12996 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
12997 /* Make sure cursor.vpos specifies a row whose start and end
12998 charpos occlude point. This is because some callers of this
12999 function leave cursor.vpos at the row where the cursor was
13000 displayed during the last redisplay cycle. */
13001 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13002 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13003 {
13004 struct glyph *g1 =
13005 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13006
13007 /* Don't consider glyphs that are outside TEXT_AREA. */
13008 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13009 return 0;
13010 /* Keep the candidate whose buffer position is the closest to
13011 point. */
13012 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13013 w->cursor.hpos >= 0
13014 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13015 && BUFFERP (g1->object)
13016 && (g1->charpos == pt_old /* an exact match always wins */
13017 || (BUFFERP (glyph->object)
13018 && eabs (g1->charpos - pt_old)
13019 < eabs (glyph->charpos - pt_old))))
13020 return 0;
13021 /* If this candidate gives an exact match, use that. */
13022 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13023 /* Otherwise, keep the candidate that comes from a row
13024 spanning less buffer positions. This may win when one or
13025 both candidate positions are on glyphs that came from
13026 display strings, for which we cannot compare buffer
13027 positions. */
13028 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13029 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13030 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13031 return 0;
13032 }
13033 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13034 w->cursor.x = x;
13035 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13036 w->cursor.y = row->y + dy;
13037
13038 if (w == XWINDOW (selected_window))
13039 {
13040 if (!row->continued_p
13041 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13042 && row->x == 0)
13043 {
13044 this_line_buffer = XBUFFER (w->buffer);
13045
13046 CHARPOS (this_line_start_pos)
13047 = MATRIX_ROW_START_CHARPOS (row) + delta;
13048 BYTEPOS (this_line_start_pos)
13049 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13050
13051 CHARPOS (this_line_end_pos)
13052 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13053 BYTEPOS (this_line_end_pos)
13054 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13055
13056 this_line_y = w->cursor.y;
13057 this_line_pixel_height = row->height;
13058 this_line_vpos = w->cursor.vpos;
13059 this_line_start_x = row->x;
13060 }
13061 else
13062 CHARPOS (this_line_start_pos) = 0;
13063 }
13064
13065 return 1;
13066 }
13067
13068
13069 /* Run window scroll functions, if any, for WINDOW with new window
13070 start STARTP. Sets the window start of WINDOW to that position.
13071
13072 We assume that the window's buffer is really current. */
13073
13074 static INLINE struct text_pos
13075 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13076 {
13077 struct window *w = XWINDOW (window);
13078 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13079
13080 if (current_buffer != XBUFFER (w->buffer))
13081 abort ();
13082
13083 if (!NILP (Vwindow_scroll_functions))
13084 {
13085 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13086 make_number (CHARPOS (startp)));
13087 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13088 /* In case the hook functions switch buffers. */
13089 if (current_buffer != XBUFFER (w->buffer))
13090 set_buffer_internal_1 (XBUFFER (w->buffer));
13091 }
13092
13093 return startp;
13094 }
13095
13096
13097 /* Make sure the line containing the cursor is fully visible.
13098 A value of 1 means there is nothing to be done.
13099 (Either the line is fully visible, or it cannot be made so,
13100 or we cannot tell.)
13101
13102 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13103 is higher than window.
13104
13105 A value of 0 means the caller should do scrolling
13106 as if point had gone off the screen. */
13107
13108 static int
13109 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13110 {
13111 struct glyph_matrix *matrix;
13112 struct glyph_row *row;
13113 int window_height;
13114
13115 if (!make_cursor_line_fully_visible_p)
13116 return 1;
13117
13118 /* It's not always possible to find the cursor, e.g, when a window
13119 is full of overlay strings. Don't do anything in that case. */
13120 if (w->cursor.vpos < 0)
13121 return 1;
13122
13123 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13124 row = MATRIX_ROW (matrix, w->cursor.vpos);
13125
13126 /* If the cursor row is not partially visible, there's nothing to do. */
13127 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13128 return 1;
13129
13130 /* If the row the cursor is in is taller than the window's height,
13131 it's not clear what to do, so do nothing. */
13132 window_height = window_box_height (w);
13133 if (row->height >= window_height)
13134 {
13135 if (!force_p || MINI_WINDOW_P (w)
13136 || w->vscroll || w->cursor.vpos == 0)
13137 return 1;
13138 }
13139 return 0;
13140 }
13141
13142
13143 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13144 non-zero means only WINDOW is redisplayed in redisplay_internal.
13145 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13146 in redisplay_window to bring a partially visible line into view in
13147 the case that only the cursor has moved.
13148
13149 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13150 last screen line's vertical height extends past the end of the screen.
13151
13152 Value is
13153
13154 1 if scrolling succeeded
13155
13156 0 if scrolling didn't find point.
13157
13158 -1 if new fonts have been loaded so that we must interrupt
13159 redisplay, adjust glyph matrices, and try again. */
13160
13161 enum
13162 {
13163 SCROLLING_SUCCESS,
13164 SCROLLING_FAILED,
13165 SCROLLING_NEED_LARGER_MATRICES
13166 };
13167
13168 /* If scroll-conservatively is more than this, never recenter.
13169
13170 If you change this, don't forget to update the doc string of
13171 `scroll-conservatively' and the Emacs manual. */
13172 #define SCROLL_LIMIT 100
13173
13174 static int
13175 try_scrolling (Lisp_Object window, int just_this_one_p,
13176 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13177 int temp_scroll_step, int last_line_misfit)
13178 {
13179 struct window *w = XWINDOW (window);
13180 struct frame *f = XFRAME (w->frame);
13181 struct text_pos pos, startp;
13182 struct it it;
13183 int this_scroll_margin, scroll_max, rc, height;
13184 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13185 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13186 Lisp_Object aggressive;
13187 /* We will never try scrolling more than this number of lines. */
13188 int scroll_limit = SCROLL_LIMIT;
13189
13190 #if GLYPH_DEBUG
13191 debug_method_add (w, "try_scrolling");
13192 #endif
13193
13194 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13195
13196 /* Compute scroll margin height in pixels. We scroll when point is
13197 within this distance from the top or bottom of the window. */
13198 if (scroll_margin > 0)
13199 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13200 * FRAME_LINE_HEIGHT (f);
13201 else
13202 this_scroll_margin = 0;
13203
13204 /* Force arg_scroll_conservatively to have a reasonable value, to
13205 avoid scrolling too far away with slow move_it_* functions. Note
13206 that the user can supply scroll-conservatively equal to
13207 `most-positive-fixnum', which can be larger than INT_MAX. */
13208 if (arg_scroll_conservatively > scroll_limit)
13209 {
13210 arg_scroll_conservatively = scroll_limit + 1;
13211 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13212 }
13213 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13214 /* Compute how much we should try to scroll maximally to bring
13215 point into view. */
13216 scroll_max = (max (scroll_step,
13217 max (arg_scroll_conservatively, temp_scroll_step))
13218 * FRAME_LINE_HEIGHT (f));
13219 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13220 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13221 /* We're trying to scroll because of aggressive scrolling but no
13222 scroll_step is set. Choose an arbitrary one. */
13223 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13224 else
13225 scroll_max = 0;
13226
13227 too_near_end:
13228
13229 /* Decide whether to scroll down. */
13230 if (PT > CHARPOS (startp))
13231 {
13232 int scroll_margin_y;
13233
13234 /* Compute the pixel ypos of the scroll margin, then move it to
13235 either that ypos or PT, whichever comes first. */
13236 start_display (&it, w, startp);
13237 scroll_margin_y = it.last_visible_y - this_scroll_margin
13238 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13239 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13240 (MOVE_TO_POS | MOVE_TO_Y));
13241
13242 if (PT > CHARPOS (it.current.pos))
13243 {
13244 int y0 = line_bottom_y (&it);
13245 /* Compute how many pixels below window bottom to stop searching
13246 for PT. This avoids costly search for PT that is far away if
13247 the user limited scrolling by a small number of lines, but
13248 always finds PT if scroll_conservatively is set to a large
13249 number, such as most-positive-fixnum. */
13250 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13251 int y_to_move = it.last_visible_y + slack;
13252
13253 /* Compute the distance from the scroll margin to PT or to
13254 the scroll limit, whichever comes first. This should
13255 include the height of the cursor line, to make that line
13256 fully visible. */
13257 move_it_to (&it, PT, -1, y_to_move,
13258 -1, MOVE_TO_POS | MOVE_TO_Y);
13259 dy = line_bottom_y (&it) - y0;
13260
13261 if (dy > scroll_max)
13262 return SCROLLING_FAILED;
13263
13264 scroll_down_p = 1;
13265 }
13266 }
13267
13268 if (scroll_down_p)
13269 {
13270 /* Point is in or below the bottom scroll margin, so move the
13271 window start down. If scrolling conservatively, move it just
13272 enough down to make point visible. If scroll_step is set,
13273 move it down by scroll_step. */
13274 if (arg_scroll_conservatively)
13275 amount_to_scroll
13276 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13277 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13278 else if (scroll_step || temp_scroll_step)
13279 amount_to_scroll = scroll_max;
13280 else
13281 {
13282 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13283 height = WINDOW_BOX_TEXT_HEIGHT (w);
13284 if (NUMBERP (aggressive))
13285 {
13286 double float_amount = XFLOATINT (aggressive) * height;
13287 amount_to_scroll = float_amount;
13288 if (amount_to_scroll == 0 && float_amount > 0)
13289 amount_to_scroll = 1;
13290 /* Don't let point enter the scroll margin near top of
13291 the window. */
13292 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13293 amount_to_scroll = height - 2*this_scroll_margin + dy;
13294 }
13295 }
13296
13297 if (amount_to_scroll <= 0)
13298 return SCROLLING_FAILED;
13299
13300 start_display (&it, w, startp);
13301 if (arg_scroll_conservatively <= scroll_limit)
13302 move_it_vertically (&it, amount_to_scroll);
13303 else
13304 {
13305 /* Extra precision for users who set scroll-conservatively
13306 to a large number: make sure the amount we scroll
13307 the window start is never less than amount_to_scroll,
13308 which was computed as distance from window bottom to
13309 point. This matters when lines at window top and lines
13310 below window bottom have different height. */
13311 struct it it1 = it;
13312 /* We use a temporary it1 because line_bottom_y can modify
13313 its argument, if it moves one line down; see there. */
13314 int start_y = line_bottom_y (&it1);
13315
13316 do {
13317 move_it_by_lines (&it, 1);
13318 it1 = it;
13319 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13320 }
13321
13322 /* If STARTP is unchanged, move it down another screen line. */
13323 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13324 move_it_by_lines (&it, 1);
13325 startp = it.current.pos;
13326 }
13327 else
13328 {
13329 struct text_pos scroll_margin_pos = startp;
13330
13331 /* See if point is inside the scroll margin at the top of the
13332 window. */
13333 if (this_scroll_margin)
13334 {
13335 start_display (&it, w, startp);
13336 move_it_vertically (&it, this_scroll_margin);
13337 scroll_margin_pos = it.current.pos;
13338 }
13339
13340 if (PT < CHARPOS (scroll_margin_pos))
13341 {
13342 /* Point is in the scroll margin at the top of the window or
13343 above what is displayed in the window. */
13344 int y0, y_to_move;
13345
13346 /* Compute the vertical distance from PT to the scroll
13347 margin position. Move as far as scroll_max allows, or
13348 one screenful, or 10 screen lines, whichever is largest.
13349 Give up if distance is greater than scroll_max. */
13350 SET_TEXT_POS (pos, PT, PT_BYTE);
13351 start_display (&it, w, pos);
13352 y0 = it.current_y;
13353 y_to_move = max (it.last_visible_y,
13354 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13355 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13356 y_to_move, -1,
13357 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13358 dy = it.current_y - y0;
13359 if (dy > scroll_max)
13360 return SCROLLING_FAILED;
13361
13362 /* Compute new window start. */
13363 start_display (&it, w, startp);
13364
13365 if (arg_scroll_conservatively)
13366 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13367 max (scroll_step, temp_scroll_step));
13368 else if (scroll_step || temp_scroll_step)
13369 amount_to_scroll = scroll_max;
13370 else
13371 {
13372 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13373 height = WINDOW_BOX_TEXT_HEIGHT (w);
13374 if (NUMBERP (aggressive))
13375 {
13376 double float_amount = XFLOATINT (aggressive) * height;
13377 amount_to_scroll = float_amount;
13378 if (amount_to_scroll == 0 && float_amount > 0)
13379 amount_to_scroll = 1;
13380 amount_to_scroll -=
13381 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13382 /* Don't let point enter the scroll margin near
13383 bottom of the window. */
13384 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13385 amount_to_scroll = height - 2*this_scroll_margin + dy;
13386 }
13387 }
13388
13389 if (amount_to_scroll <= 0)
13390 return SCROLLING_FAILED;
13391
13392 move_it_vertically_backward (&it, amount_to_scroll);
13393 startp = it.current.pos;
13394 }
13395 }
13396
13397 /* Run window scroll functions. */
13398 startp = run_window_scroll_functions (window, startp);
13399
13400 /* Display the window. Give up if new fonts are loaded, or if point
13401 doesn't appear. */
13402 if (!try_window (window, startp, 0))
13403 rc = SCROLLING_NEED_LARGER_MATRICES;
13404 else if (w->cursor.vpos < 0)
13405 {
13406 clear_glyph_matrix (w->desired_matrix);
13407 rc = SCROLLING_FAILED;
13408 }
13409 else
13410 {
13411 /* Maybe forget recorded base line for line number display. */
13412 if (!just_this_one_p
13413 || current_buffer->clip_changed
13414 || BEG_UNCHANGED < CHARPOS (startp))
13415 w->base_line_number = Qnil;
13416
13417 /* If cursor ends up on a partially visible line,
13418 treat that as being off the bottom of the screen. */
13419 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13420 /* It's possible that the cursor is on the first line of the
13421 buffer, which is partially obscured due to a vscroll
13422 (Bug#7537). In that case, avoid looping forever . */
13423 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13424 {
13425 clear_glyph_matrix (w->desired_matrix);
13426 ++extra_scroll_margin_lines;
13427 goto too_near_end;
13428 }
13429 rc = SCROLLING_SUCCESS;
13430 }
13431
13432 return rc;
13433 }
13434
13435
13436 /* Compute a suitable window start for window W if display of W starts
13437 on a continuation line. Value is non-zero if a new window start
13438 was computed.
13439
13440 The new window start will be computed, based on W's width, starting
13441 from the start of the continued line. It is the start of the
13442 screen line with the minimum distance from the old start W->start. */
13443
13444 static int
13445 compute_window_start_on_continuation_line (struct window *w)
13446 {
13447 struct text_pos pos, start_pos;
13448 int window_start_changed_p = 0;
13449
13450 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13451
13452 /* If window start is on a continuation line... Window start may be
13453 < BEGV in case there's invisible text at the start of the
13454 buffer (M-x rmail, for example). */
13455 if (CHARPOS (start_pos) > BEGV
13456 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13457 {
13458 struct it it;
13459 struct glyph_row *row;
13460
13461 /* Handle the case that the window start is out of range. */
13462 if (CHARPOS (start_pos) < BEGV)
13463 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13464 else if (CHARPOS (start_pos) > ZV)
13465 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13466
13467 /* Find the start of the continued line. This should be fast
13468 because scan_buffer is fast (newline cache). */
13469 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13470 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13471 row, DEFAULT_FACE_ID);
13472 reseat_at_previous_visible_line_start (&it);
13473
13474 /* If the line start is "too far" away from the window start,
13475 say it takes too much time to compute a new window start. */
13476 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13477 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13478 {
13479 int min_distance, distance;
13480
13481 /* Move forward by display lines to find the new window
13482 start. If window width was enlarged, the new start can
13483 be expected to be > the old start. If window width was
13484 decreased, the new window start will be < the old start.
13485 So, we're looking for the display line start with the
13486 minimum distance from the old window start. */
13487 pos = it.current.pos;
13488 min_distance = INFINITY;
13489 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13490 distance < min_distance)
13491 {
13492 min_distance = distance;
13493 pos = it.current.pos;
13494 move_it_by_lines (&it, 1);
13495 }
13496
13497 /* Set the window start there. */
13498 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13499 window_start_changed_p = 1;
13500 }
13501 }
13502
13503 return window_start_changed_p;
13504 }
13505
13506
13507 /* Try cursor movement in case text has not changed in window WINDOW,
13508 with window start STARTP. Value is
13509
13510 CURSOR_MOVEMENT_SUCCESS if successful
13511
13512 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13513
13514 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13515 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13516 we want to scroll as if scroll-step were set to 1. See the code.
13517
13518 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13519 which case we have to abort this redisplay, and adjust matrices
13520 first. */
13521
13522 enum
13523 {
13524 CURSOR_MOVEMENT_SUCCESS,
13525 CURSOR_MOVEMENT_CANNOT_BE_USED,
13526 CURSOR_MOVEMENT_MUST_SCROLL,
13527 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13528 };
13529
13530 static int
13531 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13532 {
13533 struct window *w = XWINDOW (window);
13534 struct frame *f = XFRAME (w->frame);
13535 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13536
13537 #if GLYPH_DEBUG
13538 if (inhibit_try_cursor_movement)
13539 return rc;
13540 #endif
13541
13542 /* Handle case where text has not changed, only point, and it has
13543 not moved off the frame. */
13544 if (/* Point may be in this window. */
13545 PT >= CHARPOS (startp)
13546 /* Selective display hasn't changed. */
13547 && !current_buffer->clip_changed
13548 /* Function force-mode-line-update is used to force a thorough
13549 redisplay. It sets either windows_or_buffers_changed or
13550 update_mode_lines. So don't take a shortcut here for these
13551 cases. */
13552 && !update_mode_lines
13553 && !windows_or_buffers_changed
13554 && !cursor_type_changed
13555 /* Can't use this case if highlighting a region. When a
13556 region exists, cursor movement has to do more than just
13557 set the cursor. */
13558 && !(!NILP (Vtransient_mark_mode)
13559 && !NILP (BVAR (current_buffer, mark_active)))
13560 && NILP (w->region_showing)
13561 && NILP (Vshow_trailing_whitespace)
13562 /* Right after splitting windows, last_point may be nil. */
13563 && INTEGERP (w->last_point)
13564 /* This code is not used for mini-buffer for the sake of the case
13565 of redisplaying to replace an echo area message; since in
13566 that case the mini-buffer contents per se are usually
13567 unchanged. This code is of no real use in the mini-buffer
13568 since the handling of this_line_start_pos, etc., in redisplay
13569 handles the same cases. */
13570 && !EQ (window, minibuf_window)
13571 /* When splitting windows or for new windows, it happens that
13572 redisplay is called with a nil window_end_vpos or one being
13573 larger than the window. This should really be fixed in
13574 window.c. I don't have this on my list, now, so we do
13575 approximately the same as the old redisplay code. --gerd. */
13576 && INTEGERP (w->window_end_vpos)
13577 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13578 && (FRAME_WINDOW_P (f)
13579 || !overlay_arrow_in_current_buffer_p ()))
13580 {
13581 int this_scroll_margin, top_scroll_margin;
13582 struct glyph_row *row = NULL;
13583
13584 #if GLYPH_DEBUG
13585 debug_method_add (w, "cursor movement");
13586 #endif
13587
13588 /* Scroll if point within this distance from the top or bottom
13589 of the window. This is a pixel value. */
13590 if (scroll_margin > 0)
13591 {
13592 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13593 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13594 }
13595 else
13596 this_scroll_margin = 0;
13597
13598 top_scroll_margin = this_scroll_margin;
13599 if (WINDOW_WANTS_HEADER_LINE_P (w))
13600 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13601
13602 /* Start with the row the cursor was displayed during the last
13603 not paused redisplay. Give up if that row is not valid. */
13604 if (w->last_cursor.vpos < 0
13605 || w->last_cursor.vpos >= w->current_matrix->nrows)
13606 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13607 else
13608 {
13609 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13610 if (row->mode_line_p)
13611 ++row;
13612 if (!row->enabled_p)
13613 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13614 }
13615
13616 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13617 {
13618 int scroll_p = 0, must_scroll = 0;
13619 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13620
13621 if (PT > XFASTINT (w->last_point))
13622 {
13623 /* Point has moved forward. */
13624 while (MATRIX_ROW_END_CHARPOS (row) < PT
13625 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13626 {
13627 xassert (row->enabled_p);
13628 ++row;
13629 }
13630
13631 /* If the end position of a row equals the start
13632 position of the next row, and PT is at that position,
13633 we would rather display cursor in the next line. */
13634 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13635 && MATRIX_ROW_END_CHARPOS (row) == PT
13636 && row < w->current_matrix->rows
13637 + w->current_matrix->nrows - 1
13638 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13639 && !cursor_row_p (row))
13640 ++row;
13641
13642 /* If within the scroll margin, scroll. Note that
13643 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13644 the next line would be drawn, and that
13645 this_scroll_margin can be zero. */
13646 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13647 || PT > MATRIX_ROW_END_CHARPOS (row)
13648 /* Line is completely visible last line in window
13649 and PT is to be set in the next line. */
13650 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13651 && PT == MATRIX_ROW_END_CHARPOS (row)
13652 && !row->ends_at_zv_p
13653 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13654 scroll_p = 1;
13655 }
13656 else if (PT < XFASTINT (w->last_point))
13657 {
13658 /* Cursor has to be moved backward. Note that PT >=
13659 CHARPOS (startp) because of the outer if-statement. */
13660 while (!row->mode_line_p
13661 && (MATRIX_ROW_START_CHARPOS (row) > PT
13662 || (MATRIX_ROW_START_CHARPOS (row) == PT
13663 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13664 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13665 row > w->current_matrix->rows
13666 && (row-1)->ends_in_newline_from_string_p))))
13667 && (row->y > top_scroll_margin
13668 || CHARPOS (startp) == BEGV))
13669 {
13670 xassert (row->enabled_p);
13671 --row;
13672 }
13673
13674 /* Consider the following case: Window starts at BEGV,
13675 there is invisible, intangible text at BEGV, so that
13676 display starts at some point START > BEGV. It can
13677 happen that we are called with PT somewhere between
13678 BEGV and START. Try to handle that case. */
13679 if (row < w->current_matrix->rows
13680 || row->mode_line_p)
13681 {
13682 row = w->current_matrix->rows;
13683 if (row->mode_line_p)
13684 ++row;
13685 }
13686
13687 /* Due to newlines in overlay strings, we may have to
13688 skip forward over overlay strings. */
13689 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13690 && MATRIX_ROW_END_CHARPOS (row) == PT
13691 && !cursor_row_p (row))
13692 ++row;
13693
13694 /* If within the scroll margin, scroll. */
13695 if (row->y < top_scroll_margin
13696 && CHARPOS (startp) != BEGV)
13697 scroll_p = 1;
13698 }
13699 else
13700 {
13701 /* Cursor did not move. So don't scroll even if cursor line
13702 is partially visible, as it was so before. */
13703 rc = CURSOR_MOVEMENT_SUCCESS;
13704 }
13705
13706 if (PT < MATRIX_ROW_START_CHARPOS (row)
13707 || PT > MATRIX_ROW_END_CHARPOS (row))
13708 {
13709 /* if PT is not in the glyph row, give up. */
13710 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13711 must_scroll = 1;
13712 }
13713 else if (rc != CURSOR_MOVEMENT_SUCCESS
13714 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
13715 {
13716 /* If rows are bidi-reordered and point moved, back up
13717 until we find a row that does not belong to a
13718 continuation line. This is because we must consider
13719 all rows of a continued line as candidates for the
13720 new cursor positioning, since row start and end
13721 positions change non-linearly with vertical position
13722 in such rows. */
13723 /* FIXME: Revisit this when glyph ``spilling'' in
13724 continuation lines' rows is implemented for
13725 bidi-reordered rows. */
13726 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13727 {
13728 xassert (row->enabled_p);
13729 --row;
13730 /* If we hit the beginning of the displayed portion
13731 without finding the first row of a continued
13732 line, give up. */
13733 if (row <= w->current_matrix->rows)
13734 {
13735 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13736 break;
13737 }
13738
13739 }
13740 }
13741 if (must_scroll)
13742 ;
13743 else if (rc != CURSOR_MOVEMENT_SUCCESS
13744 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13745 && make_cursor_line_fully_visible_p)
13746 {
13747 if (PT == MATRIX_ROW_END_CHARPOS (row)
13748 && !row->ends_at_zv_p
13749 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13750 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13751 else if (row->height > window_box_height (w))
13752 {
13753 /* If we end up in a partially visible line, let's
13754 make it fully visible, except when it's taller
13755 than the window, in which case we can't do much
13756 about it. */
13757 *scroll_step = 1;
13758 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13759 }
13760 else
13761 {
13762 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13763 if (!cursor_row_fully_visible_p (w, 0, 1))
13764 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13765 else
13766 rc = CURSOR_MOVEMENT_SUCCESS;
13767 }
13768 }
13769 else if (scroll_p)
13770 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13771 else if (rc != CURSOR_MOVEMENT_SUCCESS
13772 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
13773 {
13774 /* With bidi-reordered rows, there could be more than
13775 one candidate row whose start and end positions
13776 occlude point. We need to let set_cursor_from_row
13777 find the best candidate. */
13778 /* FIXME: Revisit this when glyph ``spilling'' in
13779 continuation lines' rows is implemented for
13780 bidi-reordered rows. */
13781 int rv = 0;
13782
13783 do
13784 {
13785 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13786 && PT <= MATRIX_ROW_END_CHARPOS (row)
13787 && cursor_row_p (row))
13788 rv |= set_cursor_from_row (w, row, w->current_matrix,
13789 0, 0, 0, 0);
13790 /* As soon as we've found the first suitable row
13791 whose ends_at_zv_p flag is set, we are done. */
13792 if (rv
13793 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13794 {
13795 rc = CURSOR_MOVEMENT_SUCCESS;
13796 break;
13797 }
13798 ++row;
13799 }
13800 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13801 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13802 || (MATRIX_ROW_START_CHARPOS (row) == PT
13803 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13804 /* If we didn't find any candidate rows, or exited the
13805 loop before all the candidates were examined, signal
13806 to the caller that this method failed. */
13807 if (rc != CURSOR_MOVEMENT_SUCCESS
13808 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13809 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13810 else if (rv)
13811 rc = CURSOR_MOVEMENT_SUCCESS;
13812 }
13813 else
13814 {
13815 do
13816 {
13817 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13818 {
13819 rc = CURSOR_MOVEMENT_SUCCESS;
13820 break;
13821 }
13822 ++row;
13823 }
13824 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13825 && MATRIX_ROW_START_CHARPOS (row) == PT
13826 && cursor_row_p (row));
13827 }
13828 }
13829 }
13830
13831 return rc;
13832 }
13833
13834 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
13835 static
13836 #endif
13837 void
13838 set_vertical_scroll_bar (struct window *w)
13839 {
13840 EMACS_INT start, end, whole;
13841
13842 /* Calculate the start and end positions for the current window.
13843 At some point, it would be nice to choose between scrollbars
13844 which reflect the whole buffer size, with special markers
13845 indicating narrowing, and scrollbars which reflect only the
13846 visible region.
13847
13848 Note that mini-buffers sometimes aren't displaying any text. */
13849 if (!MINI_WINDOW_P (w)
13850 || (w == XWINDOW (minibuf_window)
13851 && NILP (echo_area_buffer[0])))
13852 {
13853 struct buffer *buf = XBUFFER (w->buffer);
13854 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13855 start = marker_position (w->start) - BUF_BEGV (buf);
13856 /* I don't think this is guaranteed to be right. For the
13857 moment, we'll pretend it is. */
13858 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13859
13860 if (end < start)
13861 end = start;
13862 if (whole < (end - start))
13863 whole = end - start;
13864 }
13865 else
13866 start = end = whole = 0;
13867
13868 /* Indicate what this scroll bar ought to be displaying now. */
13869 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13870 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13871 (w, end - start, whole, start);
13872 }
13873
13874
13875 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13876 selected_window is redisplayed.
13877
13878 We can return without actually redisplaying the window if
13879 fonts_changed_p is nonzero. In that case, redisplay_internal will
13880 retry. */
13881
13882 static void
13883 redisplay_window (Lisp_Object window, int just_this_one_p)
13884 {
13885 struct window *w = XWINDOW (window);
13886 struct frame *f = XFRAME (w->frame);
13887 struct buffer *buffer = XBUFFER (w->buffer);
13888 struct buffer *old = current_buffer;
13889 struct text_pos lpoint, opoint, startp;
13890 int update_mode_line;
13891 int tem;
13892 struct it it;
13893 /* Record it now because it's overwritten. */
13894 int current_matrix_up_to_date_p = 0;
13895 int used_current_matrix_p = 0;
13896 /* This is less strict than current_matrix_up_to_date_p.
13897 It indictes that the buffer contents and narrowing are unchanged. */
13898 int buffer_unchanged_p = 0;
13899 int temp_scroll_step = 0;
13900 int count = SPECPDL_INDEX ();
13901 int rc;
13902 int centering_position = -1;
13903 int last_line_misfit = 0;
13904 EMACS_INT beg_unchanged, end_unchanged;
13905
13906 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13907 opoint = lpoint;
13908
13909 /* W must be a leaf window here. */
13910 xassert (!NILP (w->buffer));
13911 #if GLYPH_DEBUG
13912 *w->desired_matrix->method = 0;
13913 #endif
13914
13915 restart:
13916 reconsider_clip_changes (w, buffer);
13917
13918 /* Has the mode line to be updated? */
13919 update_mode_line = (!NILP (w->update_mode_line)
13920 || update_mode_lines
13921 || buffer->clip_changed
13922 || buffer->prevent_redisplay_optimizations_p);
13923
13924 if (MINI_WINDOW_P (w))
13925 {
13926 if (w == XWINDOW (echo_area_window)
13927 && !NILP (echo_area_buffer[0]))
13928 {
13929 if (update_mode_line)
13930 /* We may have to update a tty frame's menu bar or a
13931 tool-bar. Example `M-x C-h C-h C-g'. */
13932 goto finish_menu_bars;
13933 else
13934 /* We've already displayed the echo area glyphs in this window. */
13935 goto finish_scroll_bars;
13936 }
13937 else if ((w != XWINDOW (minibuf_window)
13938 || minibuf_level == 0)
13939 /* When buffer is nonempty, redisplay window normally. */
13940 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13941 /* Quail displays non-mini buffers in minibuffer window.
13942 In that case, redisplay the window normally. */
13943 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13944 {
13945 /* W is a mini-buffer window, but it's not active, so clear
13946 it. */
13947 int yb = window_text_bottom_y (w);
13948 struct glyph_row *row;
13949 int y;
13950
13951 for (y = 0, row = w->desired_matrix->rows;
13952 y < yb;
13953 y += row->height, ++row)
13954 blank_row (w, row, y);
13955 goto finish_scroll_bars;
13956 }
13957
13958 clear_glyph_matrix (w->desired_matrix);
13959 }
13960
13961 /* Otherwise set up data on this window; select its buffer and point
13962 value. */
13963 /* Really select the buffer, for the sake of buffer-local
13964 variables. */
13965 set_buffer_internal_1 (XBUFFER (w->buffer));
13966
13967 current_matrix_up_to_date_p
13968 = (!NILP (w->window_end_valid)
13969 && !current_buffer->clip_changed
13970 && !current_buffer->prevent_redisplay_optimizations_p
13971 && XFASTINT (w->last_modified) >= MODIFF
13972 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13973
13974 /* Run the window-bottom-change-functions
13975 if it is possible that the text on the screen has changed
13976 (either due to modification of the text, or any other reason). */
13977 if (!current_matrix_up_to_date_p
13978 && !NILP (Vwindow_text_change_functions))
13979 {
13980 safe_run_hooks (Qwindow_text_change_functions);
13981 goto restart;
13982 }
13983
13984 beg_unchanged = BEG_UNCHANGED;
13985 end_unchanged = END_UNCHANGED;
13986
13987 SET_TEXT_POS (opoint, PT, PT_BYTE);
13988
13989 specbind (Qinhibit_point_motion_hooks, Qt);
13990
13991 buffer_unchanged_p
13992 = (!NILP (w->window_end_valid)
13993 && !current_buffer->clip_changed
13994 && XFASTINT (w->last_modified) >= MODIFF
13995 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13996
13997 /* When windows_or_buffers_changed is non-zero, we can't rely on
13998 the window end being valid, so set it to nil there. */
13999 if (windows_or_buffers_changed)
14000 {
14001 /* If window starts on a continuation line, maybe adjust the
14002 window start in case the window's width changed. */
14003 if (XMARKER (w->start)->buffer == current_buffer)
14004 compute_window_start_on_continuation_line (w);
14005
14006 w->window_end_valid = Qnil;
14007 }
14008
14009 /* Some sanity checks. */
14010 CHECK_WINDOW_END (w);
14011 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14012 abort ();
14013 if (BYTEPOS (opoint) < CHARPOS (opoint))
14014 abort ();
14015
14016 /* If %c is in mode line, update it if needed. */
14017 if (!NILP (w->column_number_displayed)
14018 /* This alternative quickly identifies a common case
14019 where no change is needed. */
14020 && !(PT == XFASTINT (w->last_point)
14021 && XFASTINT (w->last_modified) >= MODIFF
14022 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14023 && (XFASTINT (w->column_number_displayed) != current_column ()))
14024 update_mode_line = 1;
14025
14026 /* Count number of windows showing the selected buffer. An indirect
14027 buffer counts as its base buffer. */
14028 if (!just_this_one_p)
14029 {
14030 struct buffer *current_base, *window_base;
14031 current_base = current_buffer;
14032 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14033 if (current_base->base_buffer)
14034 current_base = current_base->base_buffer;
14035 if (window_base->base_buffer)
14036 window_base = window_base->base_buffer;
14037 if (current_base == window_base)
14038 buffer_shared++;
14039 }
14040
14041 /* Point refers normally to the selected window. For any other
14042 window, set up appropriate value. */
14043 if (!EQ (window, selected_window))
14044 {
14045 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14046 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14047 if (new_pt < BEGV)
14048 {
14049 new_pt = BEGV;
14050 new_pt_byte = BEGV_BYTE;
14051 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14052 }
14053 else if (new_pt > (ZV - 1))
14054 {
14055 new_pt = ZV;
14056 new_pt_byte = ZV_BYTE;
14057 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14058 }
14059
14060 /* We don't use SET_PT so that the point-motion hooks don't run. */
14061 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14062 }
14063
14064 /* If any of the character widths specified in the display table
14065 have changed, invalidate the width run cache. It's true that
14066 this may be a bit late to catch such changes, but the rest of
14067 redisplay goes (non-fatally) haywire when the display table is
14068 changed, so why should we worry about doing any better? */
14069 if (current_buffer->width_run_cache)
14070 {
14071 struct Lisp_Char_Table *disptab = buffer_display_table ();
14072
14073 if (! disptab_matches_widthtab (disptab,
14074 XVECTOR (BVAR (current_buffer, width_table))))
14075 {
14076 invalidate_region_cache (current_buffer,
14077 current_buffer->width_run_cache,
14078 BEG, Z);
14079 recompute_width_table (current_buffer, disptab);
14080 }
14081 }
14082
14083 /* If window-start is screwed up, choose a new one. */
14084 if (XMARKER (w->start)->buffer != current_buffer)
14085 goto recenter;
14086
14087 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14088
14089 /* If someone specified a new starting point but did not insist,
14090 check whether it can be used. */
14091 if (!NILP (w->optional_new_start)
14092 && CHARPOS (startp) >= BEGV
14093 && CHARPOS (startp) <= ZV)
14094 {
14095 w->optional_new_start = Qnil;
14096 start_display (&it, w, startp);
14097 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14098 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14099 if (IT_CHARPOS (it) == PT)
14100 w->force_start = Qt;
14101 /* IT may overshoot PT if text at PT is invisible. */
14102 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14103 w->force_start = Qt;
14104 }
14105
14106 force_start:
14107
14108 /* Handle case where place to start displaying has been specified,
14109 unless the specified location is outside the accessible range. */
14110 if (!NILP (w->force_start)
14111 || w->frozen_window_start_p)
14112 {
14113 /* We set this later on if we have to adjust point. */
14114 int new_vpos = -1;
14115
14116 w->force_start = Qnil;
14117 w->vscroll = 0;
14118 w->window_end_valid = Qnil;
14119
14120 /* Forget any recorded base line for line number display. */
14121 if (!buffer_unchanged_p)
14122 w->base_line_number = Qnil;
14123
14124 /* Redisplay the mode line. Select the buffer properly for that.
14125 Also, run the hook window-scroll-functions
14126 because we have scrolled. */
14127 /* Note, we do this after clearing force_start because
14128 if there's an error, it is better to forget about force_start
14129 than to get into an infinite loop calling the hook functions
14130 and having them get more errors. */
14131 if (!update_mode_line
14132 || ! NILP (Vwindow_scroll_functions))
14133 {
14134 update_mode_line = 1;
14135 w->update_mode_line = Qt;
14136 startp = run_window_scroll_functions (window, startp);
14137 }
14138
14139 w->last_modified = make_number (0);
14140 w->last_overlay_modified = make_number (0);
14141 if (CHARPOS (startp) < BEGV)
14142 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14143 else if (CHARPOS (startp) > ZV)
14144 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14145
14146 /* Redisplay, then check if cursor has been set during the
14147 redisplay. Give up if new fonts were loaded. */
14148 /* We used to issue a CHECK_MARGINS argument to try_window here,
14149 but this causes scrolling to fail when point begins inside
14150 the scroll margin (bug#148) -- cyd */
14151 if (!try_window (window, startp, 0))
14152 {
14153 w->force_start = Qt;
14154 clear_glyph_matrix (w->desired_matrix);
14155 goto need_larger_matrices;
14156 }
14157
14158 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14159 {
14160 /* If point does not appear, try to move point so it does
14161 appear. The desired matrix has been built above, so we
14162 can use it here. */
14163 new_vpos = window_box_height (w) / 2;
14164 }
14165
14166 if (!cursor_row_fully_visible_p (w, 0, 0))
14167 {
14168 /* Point does appear, but on a line partly visible at end of window.
14169 Move it back to a fully-visible line. */
14170 new_vpos = window_box_height (w);
14171 }
14172
14173 /* If we need to move point for either of the above reasons,
14174 now actually do it. */
14175 if (new_vpos >= 0)
14176 {
14177 struct glyph_row *row;
14178
14179 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14180 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14181 ++row;
14182
14183 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14184 MATRIX_ROW_START_BYTEPOS (row));
14185
14186 if (w != XWINDOW (selected_window))
14187 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14188 else if (current_buffer == old)
14189 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14190
14191 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14192
14193 /* If we are highlighting the region, then we just changed
14194 the region, so redisplay to show it. */
14195 if (!NILP (Vtransient_mark_mode)
14196 && !NILP (BVAR (current_buffer, mark_active)))
14197 {
14198 clear_glyph_matrix (w->desired_matrix);
14199 if (!try_window (window, startp, 0))
14200 goto need_larger_matrices;
14201 }
14202 }
14203
14204 #if GLYPH_DEBUG
14205 debug_method_add (w, "forced window start");
14206 #endif
14207 goto done;
14208 }
14209
14210 /* Handle case where text has not changed, only point, and it has
14211 not moved off the frame, and we are not retrying after hscroll.
14212 (current_matrix_up_to_date_p is nonzero when retrying.) */
14213 if (current_matrix_up_to_date_p
14214 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14215 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14216 {
14217 switch (rc)
14218 {
14219 case CURSOR_MOVEMENT_SUCCESS:
14220 used_current_matrix_p = 1;
14221 goto done;
14222
14223 case CURSOR_MOVEMENT_MUST_SCROLL:
14224 goto try_to_scroll;
14225
14226 default:
14227 abort ();
14228 }
14229 }
14230 /* If current starting point was originally the beginning of a line
14231 but no longer is, find a new starting point. */
14232 else if (!NILP (w->start_at_line_beg)
14233 && !(CHARPOS (startp) <= BEGV
14234 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14235 {
14236 #if GLYPH_DEBUG
14237 debug_method_add (w, "recenter 1");
14238 #endif
14239 goto recenter;
14240 }
14241
14242 /* Try scrolling with try_window_id. Value is > 0 if update has
14243 been done, it is -1 if we know that the same window start will
14244 not work. It is 0 if unsuccessful for some other reason. */
14245 else if ((tem = try_window_id (w)) != 0)
14246 {
14247 #if GLYPH_DEBUG
14248 debug_method_add (w, "try_window_id %d", tem);
14249 #endif
14250
14251 if (fonts_changed_p)
14252 goto need_larger_matrices;
14253 if (tem > 0)
14254 goto done;
14255
14256 /* Otherwise try_window_id has returned -1 which means that we
14257 don't want the alternative below this comment to execute. */
14258 }
14259 else if (CHARPOS (startp) >= BEGV
14260 && CHARPOS (startp) <= ZV
14261 && PT >= CHARPOS (startp)
14262 && (CHARPOS (startp) < ZV
14263 /* Avoid starting at end of buffer. */
14264 || CHARPOS (startp) == BEGV
14265 || (XFASTINT (w->last_modified) >= MODIFF
14266 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14267 {
14268
14269 /* If first window line is a continuation line, and window start
14270 is inside the modified region, but the first change is before
14271 current window start, we must select a new window start.
14272
14273 However, if this is the result of a down-mouse event (e.g. by
14274 extending the mouse-drag-overlay), we don't want to select a
14275 new window start, since that would change the position under
14276 the mouse, resulting in an unwanted mouse-movement rather
14277 than a simple mouse-click. */
14278 if (NILP (w->start_at_line_beg)
14279 && NILP (do_mouse_tracking)
14280 && CHARPOS (startp) > BEGV
14281 && CHARPOS (startp) > BEG + beg_unchanged
14282 && CHARPOS (startp) <= Z - end_unchanged
14283 /* Even if w->start_at_line_beg is nil, a new window may
14284 start at a line_beg, since that's how set_buffer_window
14285 sets it. So, we need to check the return value of
14286 compute_window_start_on_continuation_line. (See also
14287 bug#197). */
14288 && XMARKER (w->start)->buffer == current_buffer
14289 && compute_window_start_on_continuation_line (w))
14290 {
14291 w->force_start = Qt;
14292 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14293 goto force_start;
14294 }
14295
14296 #if GLYPH_DEBUG
14297 debug_method_add (w, "same window start");
14298 #endif
14299
14300 /* Try to redisplay starting at same place as before.
14301 If point has not moved off frame, accept the results. */
14302 if (!current_matrix_up_to_date_p
14303 /* Don't use try_window_reusing_current_matrix in this case
14304 because a window scroll function can have changed the
14305 buffer. */
14306 || !NILP (Vwindow_scroll_functions)
14307 || MINI_WINDOW_P (w)
14308 || !(used_current_matrix_p
14309 = try_window_reusing_current_matrix (w)))
14310 {
14311 IF_DEBUG (debug_method_add (w, "1"));
14312 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14313 /* -1 means we need to scroll.
14314 0 means we need new matrices, but fonts_changed_p
14315 is set in that case, so we will detect it below. */
14316 goto try_to_scroll;
14317 }
14318
14319 if (fonts_changed_p)
14320 goto need_larger_matrices;
14321
14322 if (w->cursor.vpos >= 0)
14323 {
14324 if (!just_this_one_p
14325 || current_buffer->clip_changed
14326 || BEG_UNCHANGED < CHARPOS (startp))
14327 /* Forget any recorded base line for line number display. */
14328 w->base_line_number = Qnil;
14329
14330 if (!cursor_row_fully_visible_p (w, 1, 0))
14331 {
14332 clear_glyph_matrix (w->desired_matrix);
14333 last_line_misfit = 1;
14334 }
14335 /* Drop through and scroll. */
14336 else
14337 goto done;
14338 }
14339 else
14340 clear_glyph_matrix (w->desired_matrix);
14341 }
14342
14343 try_to_scroll:
14344
14345 w->last_modified = make_number (0);
14346 w->last_overlay_modified = make_number (0);
14347
14348 /* Redisplay the mode line. Select the buffer properly for that. */
14349 if (!update_mode_line)
14350 {
14351 update_mode_line = 1;
14352 w->update_mode_line = Qt;
14353 }
14354
14355 /* Try to scroll by specified few lines. */
14356 if ((scroll_conservatively
14357 || emacs_scroll_step
14358 || temp_scroll_step
14359 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14360 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14361 && CHARPOS (startp) >= BEGV
14362 && CHARPOS (startp) <= ZV)
14363 {
14364 /* The function returns -1 if new fonts were loaded, 1 if
14365 successful, 0 if not successful. */
14366 int ss = try_scrolling (window, just_this_one_p,
14367 scroll_conservatively,
14368 emacs_scroll_step,
14369 temp_scroll_step, last_line_misfit);
14370 switch (ss)
14371 {
14372 case SCROLLING_SUCCESS:
14373 goto done;
14374
14375 case SCROLLING_NEED_LARGER_MATRICES:
14376 goto need_larger_matrices;
14377
14378 case SCROLLING_FAILED:
14379 break;
14380
14381 default:
14382 abort ();
14383 }
14384 }
14385
14386 /* Finally, just choose a place to start which positions point
14387 according to user preferences. */
14388
14389 recenter:
14390
14391 #if GLYPH_DEBUG
14392 debug_method_add (w, "recenter");
14393 #endif
14394
14395 /* w->vscroll = 0; */
14396
14397 /* Forget any previously recorded base line for line number display. */
14398 if (!buffer_unchanged_p)
14399 w->base_line_number = Qnil;
14400
14401 /* Determine the window start relative to point. */
14402 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14403 it.current_y = it.last_visible_y;
14404 if (centering_position < 0)
14405 {
14406 int margin =
14407 scroll_margin > 0
14408 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14409 : 0;
14410 EMACS_INT margin_pos = CHARPOS (startp);
14411 int scrolling_up;
14412 Lisp_Object aggressive;
14413
14414 /* If there is a scroll margin at the top of the window, find
14415 its character position. */
14416 if (margin
14417 /* Cannot call start_display if startp is not in the
14418 accessible region of the buffer. This can happen when we
14419 have just switched to a different buffer and/or changed
14420 its restriction. In that case, startp is initialized to
14421 the character position 1 (BEG) because we did not yet
14422 have chance to display the buffer even once. */
14423 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14424 {
14425 struct it it1;
14426
14427 start_display (&it1, w, startp);
14428 move_it_vertically (&it1, margin);
14429 margin_pos = IT_CHARPOS (it1);
14430 }
14431 scrolling_up = PT > margin_pos;
14432 aggressive =
14433 scrolling_up
14434 ? BVAR (current_buffer, scroll_up_aggressively)
14435 : BVAR (current_buffer, scroll_down_aggressively);
14436
14437 if (!MINI_WINDOW_P (w)
14438 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
14439 {
14440 int pt_offset = 0;
14441
14442 /* Setting scroll-conservatively overrides
14443 scroll-*-aggressively. */
14444 if (!scroll_conservatively && NUMBERP (aggressive))
14445 {
14446 double float_amount = XFLOATINT (aggressive);
14447
14448 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14449 if (pt_offset == 0 && float_amount > 0)
14450 pt_offset = 1;
14451 if (pt_offset)
14452 margin -= 1;
14453 }
14454 /* Compute how much to move the window start backward from
14455 point so that point will be displayed where the user
14456 wants it. */
14457 if (scrolling_up)
14458 {
14459 centering_position = it.last_visible_y;
14460 if (pt_offset)
14461 centering_position -= pt_offset;
14462 centering_position -=
14463 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
14464 /* Don't let point enter the scroll margin near top of
14465 the window. */
14466 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
14467 centering_position = margin * FRAME_LINE_HEIGHT (f);
14468 }
14469 else
14470 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
14471 }
14472 else
14473 /* Set the window start half the height of the window backward
14474 from point. */
14475 centering_position = window_box_height (w) / 2;
14476 }
14477 move_it_vertically_backward (&it, centering_position);
14478
14479 xassert (IT_CHARPOS (it) >= BEGV);
14480
14481 /* The function move_it_vertically_backward may move over more
14482 than the specified y-distance. If it->w is small, e.g. a
14483 mini-buffer window, we may end up in front of the window's
14484 display area. Start displaying at the start of the line
14485 containing PT in this case. */
14486 if (it.current_y <= 0)
14487 {
14488 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14489 move_it_vertically_backward (&it, 0);
14490 it.current_y = 0;
14491 }
14492
14493 it.current_x = it.hpos = 0;
14494
14495 /* Set the window start position here explicitly, to avoid an
14496 infinite loop in case the functions in window-scroll-functions
14497 get errors. */
14498 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14499
14500 /* Run scroll hooks. */
14501 startp = run_window_scroll_functions (window, it.current.pos);
14502
14503 /* Redisplay the window. */
14504 if (!current_matrix_up_to_date_p
14505 || windows_or_buffers_changed
14506 || cursor_type_changed
14507 /* Don't use try_window_reusing_current_matrix in this case
14508 because it can have changed the buffer. */
14509 || !NILP (Vwindow_scroll_functions)
14510 || !just_this_one_p
14511 || MINI_WINDOW_P (w)
14512 || !(used_current_matrix_p
14513 = try_window_reusing_current_matrix (w)))
14514 try_window (window, startp, 0);
14515
14516 /* If new fonts have been loaded (due to fontsets), give up. We
14517 have to start a new redisplay since we need to re-adjust glyph
14518 matrices. */
14519 if (fonts_changed_p)
14520 goto need_larger_matrices;
14521
14522 /* If cursor did not appear assume that the middle of the window is
14523 in the first line of the window. Do it again with the next line.
14524 (Imagine a window of height 100, displaying two lines of height
14525 60. Moving back 50 from it->last_visible_y will end in the first
14526 line.) */
14527 if (w->cursor.vpos < 0)
14528 {
14529 if (!NILP (w->window_end_valid)
14530 && PT >= Z - XFASTINT (w->window_end_pos))
14531 {
14532 clear_glyph_matrix (w->desired_matrix);
14533 move_it_by_lines (&it, 1);
14534 try_window (window, it.current.pos, 0);
14535 }
14536 else if (PT < IT_CHARPOS (it))
14537 {
14538 clear_glyph_matrix (w->desired_matrix);
14539 move_it_by_lines (&it, -1);
14540 try_window (window, it.current.pos, 0);
14541 }
14542 else
14543 {
14544 /* Not much we can do about it. */
14545 }
14546 }
14547
14548 /* Consider the following case: Window starts at BEGV, there is
14549 invisible, intangible text at BEGV, so that display starts at
14550 some point START > BEGV. It can happen that we are called with
14551 PT somewhere between BEGV and START. Try to handle that case. */
14552 if (w->cursor.vpos < 0)
14553 {
14554 struct glyph_row *row = w->current_matrix->rows;
14555 if (row->mode_line_p)
14556 ++row;
14557 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14558 }
14559
14560 if (!cursor_row_fully_visible_p (w, 0, 0))
14561 {
14562 /* If vscroll is enabled, disable it and try again. */
14563 if (w->vscroll)
14564 {
14565 w->vscroll = 0;
14566 clear_glyph_matrix (w->desired_matrix);
14567 goto recenter;
14568 }
14569
14570 /* If centering point failed to make the whole line visible,
14571 put point at the top instead. That has to make the whole line
14572 visible, if it can be done. */
14573 if (centering_position == 0)
14574 goto done;
14575
14576 clear_glyph_matrix (w->desired_matrix);
14577 centering_position = 0;
14578 goto recenter;
14579 }
14580
14581 done:
14582
14583 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14584 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14585 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14586 ? Qt : Qnil);
14587
14588 /* Display the mode line, if we must. */
14589 if ((update_mode_line
14590 /* If window not full width, must redo its mode line
14591 if (a) the window to its side is being redone and
14592 (b) we do a frame-based redisplay. This is a consequence
14593 of how inverted lines are drawn in frame-based redisplay. */
14594 || (!just_this_one_p
14595 && !FRAME_WINDOW_P (f)
14596 && !WINDOW_FULL_WIDTH_P (w))
14597 /* Line number to display. */
14598 || INTEGERP (w->base_line_pos)
14599 /* Column number is displayed and different from the one displayed. */
14600 || (!NILP (w->column_number_displayed)
14601 && (XFASTINT (w->column_number_displayed) != current_column ())))
14602 /* This means that the window has a mode line. */
14603 && (WINDOW_WANTS_MODELINE_P (w)
14604 || WINDOW_WANTS_HEADER_LINE_P (w)))
14605 {
14606 display_mode_lines (w);
14607
14608 /* If mode line height has changed, arrange for a thorough
14609 immediate redisplay using the correct mode line height. */
14610 if (WINDOW_WANTS_MODELINE_P (w)
14611 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14612 {
14613 fonts_changed_p = 1;
14614 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14615 = DESIRED_MODE_LINE_HEIGHT (w);
14616 }
14617
14618 /* If header line height has changed, arrange for a thorough
14619 immediate redisplay using the correct header line height. */
14620 if (WINDOW_WANTS_HEADER_LINE_P (w)
14621 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14622 {
14623 fonts_changed_p = 1;
14624 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14625 = DESIRED_HEADER_LINE_HEIGHT (w);
14626 }
14627
14628 if (fonts_changed_p)
14629 goto need_larger_matrices;
14630 }
14631
14632 if (!line_number_displayed
14633 && !BUFFERP (w->base_line_pos))
14634 {
14635 w->base_line_pos = Qnil;
14636 w->base_line_number = Qnil;
14637 }
14638
14639 finish_menu_bars:
14640
14641 /* When we reach a frame's selected window, redo the frame's menu bar. */
14642 if (update_mode_line
14643 && EQ (FRAME_SELECTED_WINDOW (f), window))
14644 {
14645 int redisplay_menu_p = 0;
14646
14647 if (FRAME_WINDOW_P (f))
14648 {
14649 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14650 || defined (HAVE_NS) || defined (USE_GTK)
14651 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14652 #else
14653 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14654 #endif
14655 }
14656 else
14657 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14658
14659 if (redisplay_menu_p)
14660 display_menu_bar (w);
14661
14662 #ifdef HAVE_WINDOW_SYSTEM
14663 if (FRAME_WINDOW_P (f))
14664 {
14665 #if defined (USE_GTK) || defined (HAVE_NS)
14666 if (FRAME_EXTERNAL_TOOL_BAR (f))
14667 redisplay_tool_bar (f);
14668 #else
14669 if (WINDOWP (f->tool_bar_window)
14670 && (FRAME_TOOL_BAR_LINES (f) > 0
14671 || !NILP (Vauto_resize_tool_bars))
14672 && redisplay_tool_bar (f))
14673 ignore_mouse_drag_p = 1;
14674 #endif
14675 }
14676 #endif
14677 }
14678
14679 #ifdef HAVE_WINDOW_SYSTEM
14680 if (FRAME_WINDOW_P (f)
14681 && update_window_fringes (w, (just_this_one_p
14682 || (!used_current_matrix_p && !overlay_arrow_seen)
14683 || w->pseudo_window_p)))
14684 {
14685 update_begin (f);
14686 BLOCK_INPUT;
14687 if (draw_window_fringes (w, 1))
14688 x_draw_vertical_border (w);
14689 UNBLOCK_INPUT;
14690 update_end (f);
14691 }
14692 #endif /* HAVE_WINDOW_SYSTEM */
14693
14694 /* We go to this label, with fonts_changed_p nonzero,
14695 if it is necessary to try again using larger glyph matrices.
14696 We have to redeem the scroll bar even in this case,
14697 because the loop in redisplay_internal expects that. */
14698 need_larger_matrices:
14699 ;
14700 finish_scroll_bars:
14701
14702 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14703 {
14704 /* Set the thumb's position and size. */
14705 set_vertical_scroll_bar (w);
14706
14707 /* Note that we actually used the scroll bar attached to this
14708 window, so it shouldn't be deleted at the end of redisplay. */
14709 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14710 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14711 }
14712
14713 /* Restore current_buffer and value of point in it. The window
14714 update may have changed the buffer, so first make sure `opoint'
14715 is still valid (Bug#6177). */
14716 if (CHARPOS (opoint) < BEGV)
14717 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14718 else if (CHARPOS (opoint) > ZV)
14719 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14720 else
14721 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14722
14723 set_buffer_internal_1 (old);
14724 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14725 shorter. This can be caused by log truncation in *Messages*. */
14726 if (CHARPOS (lpoint) <= ZV)
14727 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14728
14729 unbind_to (count, Qnil);
14730 }
14731
14732
14733 /* Build the complete desired matrix of WINDOW with a window start
14734 buffer position POS.
14735
14736 Value is 1 if successful. It is zero if fonts were loaded during
14737 redisplay which makes re-adjusting glyph matrices necessary, and -1
14738 if point would appear in the scroll margins.
14739 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14740 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14741 set in FLAGS.) */
14742
14743 int
14744 try_window (Lisp_Object window, struct text_pos pos, int flags)
14745 {
14746 struct window *w = XWINDOW (window);
14747 struct it it;
14748 struct glyph_row *last_text_row = NULL;
14749 struct frame *f = XFRAME (w->frame);
14750
14751 /* Make POS the new window start. */
14752 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14753
14754 /* Mark cursor position as unknown. No overlay arrow seen. */
14755 w->cursor.vpos = -1;
14756 overlay_arrow_seen = 0;
14757
14758 /* Initialize iterator and info to start at POS. */
14759 start_display (&it, w, pos);
14760
14761 /* Display all lines of W. */
14762 while (it.current_y < it.last_visible_y)
14763 {
14764 if (display_line (&it))
14765 last_text_row = it.glyph_row - 1;
14766 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14767 return 0;
14768 }
14769
14770 /* Don't let the cursor end in the scroll margins. */
14771 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14772 && !MINI_WINDOW_P (w))
14773 {
14774 int this_scroll_margin;
14775
14776 if (scroll_margin > 0)
14777 {
14778 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14779 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14780 }
14781 else
14782 this_scroll_margin = 0;
14783
14784 if ((w->cursor.y >= 0 /* not vscrolled */
14785 && w->cursor.y < this_scroll_margin
14786 && CHARPOS (pos) > BEGV
14787 && IT_CHARPOS (it) < ZV)
14788 /* rms: considering make_cursor_line_fully_visible_p here
14789 seems to give wrong results. We don't want to recenter
14790 when the last line is partly visible, we want to allow
14791 that case to be handled in the usual way. */
14792 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14793 {
14794 w->cursor.vpos = -1;
14795 clear_glyph_matrix (w->desired_matrix);
14796 return -1;
14797 }
14798 }
14799
14800 /* If bottom moved off end of frame, change mode line percentage. */
14801 if (XFASTINT (w->window_end_pos) <= 0
14802 && Z != IT_CHARPOS (it))
14803 w->update_mode_line = Qt;
14804
14805 /* Set window_end_pos to the offset of the last character displayed
14806 on the window from the end of current_buffer. Set
14807 window_end_vpos to its row number. */
14808 if (last_text_row)
14809 {
14810 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14811 w->window_end_bytepos
14812 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14813 w->window_end_pos
14814 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14815 w->window_end_vpos
14816 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14817 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14818 ->displays_text_p);
14819 }
14820 else
14821 {
14822 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14823 w->window_end_pos = make_number (Z - ZV);
14824 w->window_end_vpos = make_number (0);
14825 }
14826
14827 /* But that is not valid info until redisplay finishes. */
14828 w->window_end_valid = Qnil;
14829 return 1;
14830 }
14831
14832
14833 \f
14834 /************************************************************************
14835 Window redisplay reusing current matrix when buffer has not changed
14836 ************************************************************************/
14837
14838 /* Try redisplay of window W showing an unchanged buffer with a
14839 different window start than the last time it was displayed by
14840 reusing its current matrix. Value is non-zero if successful.
14841 W->start is the new window start. */
14842
14843 static int
14844 try_window_reusing_current_matrix (struct window *w)
14845 {
14846 struct frame *f = XFRAME (w->frame);
14847 struct glyph_row *bottom_row;
14848 struct it it;
14849 struct run run;
14850 struct text_pos start, new_start;
14851 int nrows_scrolled, i;
14852 struct glyph_row *last_text_row;
14853 struct glyph_row *last_reused_text_row;
14854 struct glyph_row *start_row;
14855 int start_vpos, min_y, max_y;
14856
14857 #if GLYPH_DEBUG
14858 if (inhibit_try_window_reusing)
14859 return 0;
14860 #endif
14861
14862 if (/* This function doesn't handle terminal frames. */
14863 !FRAME_WINDOW_P (f)
14864 /* Don't try to reuse the display if windows have been split
14865 or such. */
14866 || windows_or_buffers_changed
14867 || cursor_type_changed)
14868 return 0;
14869
14870 /* Can't do this if region may have changed. */
14871 if ((!NILP (Vtransient_mark_mode)
14872 && !NILP (BVAR (current_buffer, mark_active)))
14873 || !NILP (w->region_showing)
14874 || !NILP (Vshow_trailing_whitespace))
14875 return 0;
14876
14877 /* If top-line visibility has changed, give up. */
14878 if (WINDOW_WANTS_HEADER_LINE_P (w)
14879 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14880 return 0;
14881
14882 /* Give up if old or new display is scrolled vertically. We could
14883 make this function handle this, but right now it doesn't. */
14884 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14885 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14886 return 0;
14887
14888 /* The variable new_start now holds the new window start. The old
14889 start `start' can be determined from the current matrix. */
14890 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14891 start = start_row->minpos;
14892 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14893
14894 /* Clear the desired matrix for the display below. */
14895 clear_glyph_matrix (w->desired_matrix);
14896
14897 if (CHARPOS (new_start) <= CHARPOS (start))
14898 {
14899 /* Don't use this method if the display starts with an ellipsis
14900 displayed for invisible text. It's not easy to handle that case
14901 below, and it's certainly not worth the effort since this is
14902 not a frequent case. */
14903 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14904 return 0;
14905
14906 IF_DEBUG (debug_method_add (w, "twu1"));
14907
14908 /* Display up to a row that can be reused. The variable
14909 last_text_row is set to the last row displayed that displays
14910 text. Note that it.vpos == 0 if or if not there is a
14911 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14912 start_display (&it, w, new_start);
14913 w->cursor.vpos = -1;
14914 last_text_row = last_reused_text_row = NULL;
14915
14916 while (it.current_y < it.last_visible_y
14917 && !fonts_changed_p)
14918 {
14919 /* If we have reached into the characters in the START row,
14920 that means the line boundaries have changed. So we
14921 can't start copying with the row START. Maybe it will
14922 work to start copying with the following row. */
14923 while (IT_CHARPOS (it) > CHARPOS (start))
14924 {
14925 /* Advance to the next row as the "start". */
14926 start_row++;
14927 start = start_row->minpos;
14928 /* If there are no more rows to try, or just one, give up. */
14929 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14930 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14931 || CHARPOS (start) == ZV)
14932 {
14933 clear_glyph_matrix (w->desired_matrix);
14934 return 0;
14935 }
14936
14937 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14938 }
14939 /* If we have reached alignment,
14940 we can copy the rest of the rows. */
14941 if (IT_CHARPOS (it) == CHARPOS (start))
14942 break;
14943
14944 if (display_line (&it))
14945 last_text_row = it.glyph_row - 1;
14946 }
14947
14948 /* A value of current_y < last_visible_y means that we stopped
14949 at the previous window start, which in turn means that we
14950 have at least one reusable row. */
14951 if (it.current_y < it.last_visible_y)
14952 {
14953 struct glyph_row *row;
14954
14955 /* IT.vpos always starts from 0; it counts text lines. */
14956 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14957
14958 /* Find PT if not already found in the lines displayed. */
14959 if (w->cursor.vpos < 0)
14960 {
14961 int dy = it.current_y - start_row->y;
14962
14963 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14964 row = row_containing_pos (w, PT, row, NULL, dy);
14965 if (row)
14966 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14967 dy, nrows_scrolled);
14968 else
14969 {
14970 clear_glyph_matrix (w->desired_matrix);
14971 return 0;
14972 }
14973 }
14974
14975 /* Scroll the display. Do it before the current matrix is
14976 changed. The problem here is that update has not yet
14977 run, i.e. part of the current matrix is not up to date.
14978 scroll_run_hook will clear the cursor, and use the
14979 current matrix to get the height of the row the cursor is
14980 in. */
14981 run.current_y = start_row->y;
14982 run.desired_y = it.current_y;
14983 run.height = it.last_visible_y - it.current_y;
14984
14985 if (run.height > 0 && run.current_y != run.desired_y)
14986 {
14987 update_begin (f);
14988 FRAME_RIF (f)->update_window_begin_hook (w);
14989 FRAME_RIF (f)->clear_window_mouse_face (w);
14990 FRAME_RIF (f)->scroll_run_hook (w, &run);
14991 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14992 update_end (f);
14993 }
14994
14995 /* Shift current matrix down by nrows_scrolled lines. */
14996 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14997 rotate_matrix (w->current_matrix,
14998 start_vpos,
14999 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15000 nrows_scrolled);
15001
15002 /* Disable lines that must be updated. */
15003 for (i = 0; i < nrows_scrolled; ++i)
15004 (start_row + i)->enabled_p = 0;
15005
15006 /* Re-compute Y positions. */
15007 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15008 max_y = it.last_visible_y;
15009 for (row = start_row + nrows_scrolled;
15010 row < bottom_row;
15011 ++row)
15012 {
15013 row->y = it.current_y;
15014 row->visible_height = row->height;
15015
15016 if (row->y < min_y)
15017 row->visible_height -= min_y - row->y;
15018 if (row->y + row->height > max_y)
15019 row->visible_height -= row->y + row->height - max_y;
15020 row->redraw_fringe_bitmaps_p = 1;
15021
15022 it.current_y += row->height;
15023
15024 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15025 last_reused_text_row = row;
15026 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15027 break;
15028 }
15029
15030 /* Disable lines in the current matrix which are now
15031 below the window. */
15032 for (++row; row < bottom_row; ++row)
15033 row->enabled_p = row->mode_line_p = 0;
15034 }
15035
15036 /* Update window_end_pos etc.; last_reused_text_row is the last
15037 reused row from the current matrix containing text, if any.
15038 The value of last_text_row is the last displayed line
15039 containing text. */
15040 if (last_reused_text_row)
15041 {
15042 w->window_end_bytepos
15043 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15044 w->window_end_pos
15045 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15046 w->window_end_vpos
15047 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15048 w->current_matrix));
15049 }
15050 else if (last_text_row)
15051 {
15052 w->window_end_bytepos
15053 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15054 w->window_end_pos
15055 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15056 w->window_end_vpos
15057 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15058 }
15059 else
15060 {
15061 /* This window must be completely empty. */
15062 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15063 w->window_end_pos = make_number (Z - ZV);
15064 w->window_end_vpos = make_number (0);
15065 }
15066 w->window_end_valid = Qnil;
15067
15068 /* Update hint: don't try scrolling again in update_window. */
15069 w->desired_matrix->no_scrolling_p = 1;
15070
15071 #if GLYPH_DEBUG
15072 debug_method_add (w, "try_window_reusing_current_matrix 1");
15073 #endif
15074 return 1;
15075 }
15076 else if (CHARPOS (new_start) > CHARPOS (start))
15077 {
15078 struct glyph_row *pt_row, *row;
15079 struct glyph_row *first_reusable_row;
15080 struct glyph_row *first_row_to_display;
15081 int dy;
15082 int yb = window_text_bottom_y (w);
15083
15084 /* Find the row starting at new_start, if there is one. Don't
15085 reuse a partially visible line at the end. */
15086 first_reusable_row = start_row;
15087 while (first_reusable_row->enabled_p
15088 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15089 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15090 < CHARPOS (new_start)))
15091 ++first_reusable_row;
15092
15093 /* Give up if there is no row to reuse. */
15094 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15095 || !first_reusable_row->enabled_p
15096 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15097 != CHARPOS (new_start)))
15098 return 0;
15099
15100 /* We can reuse fully visible rows beginning with
15101 first_reusable_row to the end of the window. Set
15102 first_row_to_display to the first row that cannot be reused.
15103 Set pt_row to the row containing point, if there is any. */
15104 pt_row = NULL;
15105 for (first_row_to_display = first_reusable_row;
15106 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15107 ++first_row_to_display)
15108 {
15109 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15110 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15111 pt_row = first_row_to_display;
15112 }
15113
15114 /* Start displaying at the start of first_row_to_display. */
15115 xassert (first_row_to_display->y < yb);
15116 init_to_row_start (&it, w, first_row_to_display);
15117
15118 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15119 - start_vpos);
15120 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15121 - nrows_scrolled);
15122 it.current_y = (first_row_to_display->y - first_reusable_row->y
15123 + WINDOW_HEADER_LINE_HEIGHT (w));
15124
15125 /* Display lines beginning with first_row_to_display in the
15126 desired matrix. Set last_text_row to the last row displayed
15127 that displays text. */
15128 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15129 if (pt_row == NULL)
15130 w->cursor.vpos = -1;
15131 last_text_row = NULL;
15132 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15133 if (display_line (&it))
15134 last_text_row = it.glyph_row - 1;
15135
15136 /* If point is in a reused row, adjust y and vpos of the cursor
15137 position. */
15138 if (pt_row)
15139 {
15140 w->cursor.vpos -= nrows_scrolled;
15141 w->cursor.y -= first_reusable_row->y - start_row->y;
15142 }
15143
15144 /* Give up if point isn't in a row displayed or reused. (This
15145 also handles the case where w->cursor.vpos < nrows_scrolled
15146 after the calls to display_line, which can happen with scroll
15147 margins. See bug#1295.) */
15148 if (w->cursor.vpos < 0)
15149 {
15150 clear_glyph_matrix (w->desired_matrix);
15151 return 0;
15152 }
15153
15154 /* Scroll the display. */
15155 run.current_y = first_reusable_row->y;
15156 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15157 run.height = it.last_visible_y - run.current_y;
15158 dy = run.current_y - run.desired_y;
15159
15160 if (run.height)
15161 {
15162 update_begin (f);
15163 FRAME_RIF (f)->update_window_begin_hook (w);
15164 FRAME_RIF (f)->clear_window_mouse_face (w);
15165 FRAME_RIF (f)->scroll_run_hook (w, &run);
15166 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15167 update_end (f);
15168 }
15169
15170 /* Adjust Y positions of reused rows. */
15171 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15172 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15173 max_y = it.last_visible_y;
15174 for (row = first_reusable_row; row < first_row_to_display; ++row)
15175 {
15176 row->y -= dy;
15177 row->visible_height = row->height;
15178 if (row->y < min_y)
15179 row->visible_height -= min_y - row->y;
15180 if (row->y + row->height > max_y)
15181 row->visible_height -= row->y + row->height - max_y;
15182 row->redraw_fringe_bitmaps_p = 1;
15183 }
15184
15185 /* Scroll the current matrix. */
15186 xassert (nrows_scrolled > 0);
15187 rotate_matrix (w->current_matrix,
15188 start_vpos,
15189 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15190 -nrows_scrolled);
15191
15192 /* Disable rows not reused. */
15193 for (row -= nrows_scrolled; row < bottom_row; ++row)
15194 row->enabled_p = 0;
15195
15196 /* Point may have moved to a different line, so we cannot assume that
15197 the previous cursor position is valid; locate the correct row. */
15198 if (pt_row)
15199 {
15200 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15201 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15202 row++)
15203 {
15204 w->cursor.vpos++;
15205 w->cursor.y = row->y;
15206 }
15207 if (row < bottom_row)
15208 {
15209 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15210 struct glyph *end = glyph + row->used[TEXT_AREA];
15211
15212 /* Can't use this optimization with bidi-reordered glyph
15213 rows, unless cursor is already at point. */
15214 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15215 {
15216 if (!(w->cursor.hpos >= 0
15217 && w->cursor.hpos < row->used[TEXT_AREA]
15218 && BUFFERP (glyph->object)
15219 && glyph->charpos == PT))
15220 return 0;
15221 }
15222 else
15223 for (; glyph < end
15224 && (!BUFFERP (glyph->object)
15225 || glyph->charpos < PT);
15226 glyph++)
15227 {
15228 w->cursor.hpos++;
15229 w->cursor.x += glyph->pixel_width;
15230 }
15231 }
15232 }
15233
15234 /* Adjust window end. A null value of last_text_row means that
15235 the window end is in reused rows which in turn means that
15236 only its vpos can have changed. */
15237 if (last_text_row)
15238 {
15239 w->window_end_bytepos
15240 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15241 w->window_end_pos
15242 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15243 w->window_end_vpos
15244 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15245 }
15246 else
15247 {
15248 w->window_end_vpos
15249 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15250 }
15251
15252 w->window_end_valid = Qnil;
15253 w->desired_matrix->no_scrolling_p = 1;
15254
15255 #if GLYPH_DEBUG
15256 debug_method_add (w, "try_window_reusing_current_matrix 2");
15257 #endif
15258 return 1;
15259 }
15260
15261 return 0;
15262 }
15263
15264
15265 \f
15266 /************************************************************************
15267 Window redisplay reusing current matrix when buffer has changed
15268 ************************************************************************/
15269
15270 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15271 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15272 EMACS_INT *, EMACS_INT *);
15273 static struct glyph_row *
15274 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15275 struct glyph_row *);
15276
15277
15278 /* Return the last row in MATRIX displaying text. If row START is
15279 non-null, start searching with that row. IT gives the dimensions
15280 of the display. Value is null if matrix is empty; otherwise it is
15281 a pointer to the row found. */
15282
15283 static struct glyph_row *
15284 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15285 struct glyph_row *start)
15286 {
15287 struct glyph_row *row, *row_found;
15288
15289 /* Set row_found to the last row in IT->w's current matrix
15290 displaying text. The loop looks funny but think of partially
15291 visible lines. */
15292 row_found = NULL;
15293 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15294 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15295 {
15296 xassert (row->enabled_p);
15297 row_found = row;
15298 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15299 break;
15300 ++row;
15301 }
15302
15303 return row_found;
15304 }
15305
15306
15307 /* Return the last row in the current matrix of W that is not affected
15308 by changes at the start of current_buffer that occurred since W's
15309 current matrix was built. Value is null if no such row exists.
15310
15311 BEG_UNCHANGED us the number of characters unchanged at the start of
15312 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15313 first changed character in current_buffer. Characters at positions <
15314 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15315 when the current matrix was built. */
15316
15317 static struct glyph_row *
15318 find_last_unchanged_at_beg_row (struct window *w)
15319 {
15320 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15321 struct glyph_row *row;
15322 struct glyph_row *row_found = NULL;
15323 int yb = window_text_bottom_y (w);
15324
15325 /* Find the last row displaying unchanged text. */
15326 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15327 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15328 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15329 ++row)
15330 {
15331 if (/* If row ends before first_changed_pos, it is unchanged,
15332 except in some case. */
15333 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15334 /* When row ends in ZV and we write at ZV it is not
15335 unchanged. */
15336 && !row->ends_at_zv_p
15337 /* When first_changed_pos is the end of a continued line,
15338 row is not unchanged because it may be no longer
15339 continued. */
15340 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15341 && (row->continued_p
15342 || row->exact_window_width_line_p)))
15343 row_found = row;
15344
15345 /* Stop if last visible row. */
15346 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15347 break;
15348 }
15349
15350 return row_found;
15351 }
15352
15353
15354 /* Find the first glyph row in the current matrix of W that is not
15355 affected by changes at the end of current_buffer since the
15356 time W's current matrix was built.
15357
15358 Return in *DELTA the number of chars by which buffer positions in
15359 unchanged text at the end of current_buffer must be adjusted.
15360
15361 Return in *DELTA_BYTES the corresponding number of bytes.
15362
15363 Value is null if no such row exists, i.e. all rows are affected by
15364 changes. */
15365
15366 static struct glyph_row *
15367 find_first_unchanged_at_end_row (struct window *w,
15368 EMACS_INT *delta, EMACS_INT *delta_bytes)
15369 {
15370 struct glyph_row *row;
15371 struct glyph_row *row_found = NULL;
15372
15373 *delta = *delta_bytes = 0;
15374
15375 /* Display must not have been paused, otherwise the current matrix
15376 is not up to date. */
15377 eassert (!NILP (w->window_end_valid));
15378
15379 /* A value of window_end_pos >= END_UNCHANGED means that the window
15380 end is in the range of changed text. If so, there is no
15381 unchanged row at the end of W's current matrix. */
15382 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15383 return NULL;
15384
15385 /* Set row to the last row in W's current matrix displaying text. */
15386 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15387
15388 /* If matrix is entirely empty, no unchanged row exists. */
15389 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15390 {
15391 /* The value of row is the last glyph row in the matrix having a
15392 meaningful buffer position in it. The end position of row
15393 corresponds to window_end_pos. This allows us to translate
15394 buffer positions in the current matrix to current buffer
15395 positions for characters not in changed text. */
15396 EMACS_INT Z_old =
15397 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15398 EMACS_INT Z_BYTE_old =
15399 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15400 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15401 struct glyph_row *first_text_row
15402 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15403
15404 *delta = Z - Z_old;
15405 *delta_bytes = Z_BYTE - Z_BYTE_old;
15406
15407 /* Set last_unchanged_pos to the buffer position of the last
15408 character in the buffer that has not been changed. Z is the
15409 index + 1 of the last character in current_buffer, i.e. by
15410 subtracting END_UNCHANGED we get the index of the last
15411 unchanged character, and we have to add BEG to get its buffer
15412 position. */
15413 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15414 last_unchanged_pos_old = last_unchanged_pos - *delta;
15415
15416 /* Search backward from ROW for a row displaying a line that
15417 starts at a minimum position >= last_unchanged_pos_old. */
15418 for (; row > first_text_row; --row)
15419 {
15420 /* This used to abort, but it can happen.
15421 It is ok to just stop the search instead here. KFS. */
15422 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15423 break;
15424
15425 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15426 row_found = row;
15427 }
15428 }
15429
15430 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15431
15432 return row_found;
15433 }
15434
15435
15436 /* Make sure that glyph rows in the current matrix of window W
15437 reference the same glyph memory as corresponding rows in the
15438 frame's frame matrix. This function is called after scrolling W's
15439 current matrix on a terminal frame in try_window_id and
15440 try_window_reusing_current_matrix. */
15441
15442 static void
15443 sync_frame_with_window_matrix_rows (struct window *w)
15444 {
15445 struct frame *f = XFRAME (w->frame);
15446 struct glyph_row *window_row, *window_row_end, *frame_row;
15447
15448 /* Preconditions: W must be a leaf window and full-width. Its frame
15449 must have a frame matrix. */
15450 xassert (NILP (w->hchild) && NILP (w->vchild));
15451 xassert (WINDOW_FULL_WIDTH_P (w));
15452 xassert (!FRAME_WINDOW_P (f));
15453
15454 /* If W is a full-width window, glyph pointers in W's current matrix
15455 have, by definition, to be the same as glyph pointers in the
15456 corresponding frame matrix. Note that frame matrices have no
15457 marginal areas (see build_frame_matrix). */
15458 window_row = w->current_matrix->rows;
15459 window_row_end = window_row + w->current_matrix->nrows;
15460 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15461 while (window_row < window_row_end)
15462 {
15463 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15464 struct glyph *end = window_row->glyphs[LAST_AREA];
15465
15466 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15467 frame_row->glyphs[TEXT_AREA] = start;
15468 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15469 frame_row->glyphs[LAST_AREA] = end;
15470
15471 /* Disable frame rows whose corresponding window rows have
15472 been disabled in try_window_id. */
15473 if (!window_row->enabled_p)
15474 frame_row->enabled_p = 0;
15475
15476 ++window_row, ++frame_row;
15477 }
15478 }
15479
15480
15481 /* Find the glyph row in window W containing CHARPOS. Consider all
15482 rows between START and END (not inclusive). END null means search
15483 all rows to the end of the display area of W. Value is the row
15484 containing CHARPOS or null. */
15485
15486 struct glyph_row *
15487 row_containing_pos (struct window *w, EMACS_INT charpos,
15488 struct glyph_row *start, struct glyph_row *end, int dy)
15489 {
15490 struct glyph_row *row = start;
15491 struct glyph_row *best_row = NULL;
15492 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15493 int last_y;
15494
15495 /* If we happen to start on a header-line, skip that. */
15496 if (row->mode_line_p)
15497 ++row;
15498
15499 if ((end && row >= end) || !row->enabled_p)
15500 return NULL;
15501
15502 last_y = window_text_bottom_y (w) - dy;
15503
15504 while (1)
15505 {
15506 /* Give up if we have gone too far. */
15507 if (end && row >= end)
15508 return NULL;
15509 /* This formerly returned if they were equal.
15510 I think that both quantities are of a "last plus one" type;
15511 if so, when they are equal, the row is within the screen. -- rms. */
15512 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15513 return NULL;
15514
15515 /* If it is in this row, return this row. */
15516 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15517 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15518 /* The end position of a row equals the start
15519 position of the next row. If CHARPOS is there, we
15520 would rather display it in the next line, except
15521 when this line ends in ZV. */
15522 && !row->ends_at_zv_p
15523 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15524 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15525 {
15526 struct glyph *g;
15527
15528 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15529 || (!best_row && !row->continued_p))
15530 return row;
15531 /* In bidi-reordered rows, there could be several rows
15532 occluding point, all of them belonging to the same
15533 continued line. We need to find the row which fits
15534 CHARPOS the best. */
15535 for (g = row->glyphs[TEXT_AREA];
15536 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15537 g++)
15538 {
15539 if (!STRINGP (g->object))
15540 {
15541 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15542 {
15543 mindif = eabs (g->charpos - charpos);
15544 best_row = row;
15545 /* Exact match always wins. */
15546 if (mindif == 0)
15547 return best_row;
15548 }
15549 }
15550 }
15551 }
15552 else if (best_row && !row->continued_p)
15553 return best_row;
15554 ++row;
15555 }
15556 }
15557
15558
15559 /* Try to redisplay window W by reusing its existing display. W's
15560 current matrix must be up to date when this function is called,
15561 i.e. window_end_valid must not be nil.
15562
15563 Value is
15564
15565 1 if display has been updated
15566 0 if otherwise unsuccessful
15567 -1 if redisplay with same window start is known not to succeed
15568
15569 The following steps are performed:
15570
15571 1. Find the last row in the current matrix of W that is not
15572 affected by changes at the start of current_buffer. If no such row
15573 is found, give up.
15574
15575 2. Find the first row in W's current matrix that is not affected by
15576 changes at the end of current_buffer. Maybe there is no such row.
15577
15578 3. Display lines beginning with the row + 1 found in step 1 to the
15579 row found in step 2 or, if step 2 didn't find a row, to the end of
15580 the window.
15581
15582 4. If cursor is not known to appear on the window, give up.
15583
15584 5. If display stopped at the row found in step 2, scroll the
15585 display and current matrix as needed.
15586
15587 6. Maybe display some lines at the end of W, if we must. This can
15588 happen under various circumstances, like a partially visible line
15589 becoming fully visible, or because newly displayed lines are displayed
15590 in smaller font sizes.
15591
15592 7. Update W's window end information. */
15593
15594 static int
15595 try_window_id (struct window *w)
15596 {
15597 struct frame *f = XFRAME (w->frame);
15598 struct glyph_matrix *current_matrix = w->current_matrix;
15599 struct glyph_matrix *desired_matrix = w->desired_matrix;
15600 struct glyph_row *last_unchanged_at_beg_row;
15601 struct glyph_row *first_unchanged_at_end_row;
15602 struct glyph_row *row;
15603 struct glyph_row *bottom_row;
15604 int bottom_vpos;
15605 struct it it;
15606 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15607 int dvpos, dy;
15608 struct text_pos start_pos;
15609 struct run run;
15610 int first_unchanged_at_end_vpos = 0;
15611 struct glyph_row *last_text_row, *last_text_row_at_end;
15612 struct text_pos start;
15613 EMACS_INT first_changed_charpos, last_changed_charpos;
15614
15615 #if GLYPH_DEBUG
15616 if (inhibit_try_window_id)
15617 return 0;
15618 #endif
15619
15620 /* This is handy for debugging. */
15621 #if 0
15622 #define GIVE_UP(X) \
15623 do { \
15624 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15625 return 0; \
15626 } while (0)
15627 #else
15628 #define GIVE_UP(X) return 0
15629 #endif
15630
15631 SET_TEXT_POS_FROM_MARKER (start, w->start);
15632
15633 /* Don't use this for mini-windows because these can show
15634 messages and mini-buffers, and we don't handle that here. */
15635 if (MINI_WINDOW_P (w))
15636 GIVE_UP (1);
15637
15638 /* This flag is used to prevent redisplay optimizations. */
15639 if (windows_or_buffers_changed || cursor_type_changed)
15640 GIVE_UP (2);
15641
15642 /* Verify that narrowing has not changed.
15643 Also verify that we were not told to prevent redisplay optimizations.
15644 It would be nice to further
15645 reduce the number of cases where this prevents try_window_id. */
15646 if (current_buffer->clip_changed
15647 || current_buffer->prevent_redisplay_optimizations_p)
15648 GIVE_UP (3);
15649
15650 /* Window must either use window-based redisplay or be full width. */
15651 if (!FRAME_WINDOW_P (f)
15652 && (!FRAME_LINE_INS_DEL_OK (f)
15653 || !WINDOW_FULL_WIDTH_P (w)))
15654 GIVE_UP (4);
15655
15656 /* Give up if point is known NOT to appear in W. */
15657 if (PT < CHARPOS (start))
15658 GIVE_UP (5);
15659
15660 /* Another way to prevent redisplay optimizations. */
15661 if (XFASTINT (w->last_modified) == 0)
15662 GIVE_UP (6);
15663
15664 /* Verify that window is not hscrolled. */
15665 if (XFASTINT (w->hscroll) != 0)
15666 GIVE_UP (7);
15667
15668 /* Verify that display wasn't paused. */
15669 if (NILP (w->window_end_valid))
15670 GIVE_UP (8);
15671
15672 /* Can't use this if highlighting a region because a cursor movement
15673 will do more than just set the cursor. */
15674 if (!NILP (Vtransient_mark_mode)
15675 && !NILP (BVAR (current_buffer, mark_active)))
15676 GIVE_UP (9);
15677
15678 /* Likewise if highlighting trailing whitespace. */
15679 if (!NILP (Vshow_trailing_whitespace))
15680 GIVE_UP (11);
15681
15682 /* Likewise if showing a region. */
15683 if (!NILP (w->region_showing))
15684 GIVE_UP (10);
15685
15686 /* Can't use this if overlay arrow position and/or string have
15687 changed. */
15688 if (overlay_arrows_changed_p ())
15689 GIVE_UP (12);
15690
15691 /* When word-wrap is on, adding a space to the first word of a
15692 wrapped line can change the wrap position, altering the line
15693 above it. It might be worthwhile to handle this more
15694 intelligently, but for now just redisplay from scratch. */
15695 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
15696 GIVE_UP (21);
15697
15698 /* Under bidi reordering, adding or deleting a character in the
15699 beginning of a paragraph, before the first strong directional
15700 character, can change the base direction of the paragraph (unless
15701 the buffer specifies a fixed paragraph direction), which will
15702 require to redisplay the whole paragraph. It might be worthwhile
15703 to find the paragraph limits and widen the range of redisplayed
15704 lines to that, but for now just give up this optimization and
15705 redisplay from scratch. */
15706 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15707 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
15708 GIVE_UP (22);
15709
15710 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15711 only if buffer has really changed. The reason is that the gap is
15712 initially at Z for freshly visited files. The code below would
15713 set end_unchanged to 0 in that case. */
15714 if (MODIFF > SAVE_MODIFF
15715 /* This seems to happen sometimes after saving a buffer. */
15716 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15717 {
15718 if (GPT - BEG < BEG_UNCHANGED)
15719 BEG_UNCHANGED = GPT - BEG;
15720 if (Z - GPT < END_UNCHANGED)
15721 END_UNCHANGED = Z - GPT;
15722 }
15723
15724 /* The position of the first and last character that has been changed. */
15725 first_changed_charpos = BEG + BEG_UNCHANGED;
15726 last_changed_charpos = Z - END_UNCHANGED;
15727
15728 /* If window starts after a line end, and the last change is in
15729 front of that newline, then changes don't affect the display.
15730 This case happens with stealth-fontification. Note that although
15731 the display is unchanged, glyph positions in the matrix have to
15732 be adjusted, of course. */
15733 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15734 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15735 && ((last_changed_charpos < CHARPOS (start)
15736 && CHARPOS (start) == BEGV)
15737 || (last_changed_charpos < CHARPOS (start) - 1
15738 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15739 {
15740 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
15741 struct glyph_row *r0;
15742
15743 /* Compute how many chars/bytes have been added to or removed
15744 from the buffer. */
15745 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15746 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15747 Z_delta = Z - Z_old;
15748 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
15749
15750 /* Give up if PT is not in the window. Note that it already has
15751 been checked at the start of try_window_id that PT is not in
15752 front of the window start. */
15753 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
15754 GIVE_UP (13);
15755
15756 /* If window start is unchanged, we can reuse the whole matrix
15757 as is, after adjusting glyph positions. No need to compute
15758 the window end again, since its offset from Z hasn't changed. */
15759 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15760 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
15761 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
15762 /* PT must not be in a partially visible line. */
15763 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
15764 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15765 {
15766 /* Adjust positions in the glyph matrix. */
15767 if (Z_delta || Z_delta_bytes)
15768 {
15769 struct glyph_row *r1
15770 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15771 increment_matrix_positions (w->current_matrix,
15772 MATRIX_ROW_VPOS (r0, current_matrix),
15773 MATRIX_ROW_VPOS (r1, current_matrix),
15774 Z_delta, Z_delta_bytes);
15775 }
15776
15777 /* Set the cursor. */
15778 row = row_containing_pos (w, PT, r0, NULL, 0);
15779 if (row)
15780 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15781 else
15782 abort ();
15783 return 1;
15784 }
15785 }
15786
15787 /* Handle the case that changes are all below what is displayed in
15788 the window, and that PT is in the window. This shortcut cannot
15789 be taken if ZV is visible in the window, and text has been added
15790 there that is visible in the window. */
15791 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15792 /* ZV is not visible in the window, or there are no
15793 changes at ZV, actually. */
15794 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15795 || first_changed_charpos == last_changed_charpos))
15796 {
15797 struct glyph_row *r0;
15798
15799 /* Give up if PT is not in the window. Note that it already has
15800 been checked at the start of try_window_id that PT is not in
15801 front of the window start. */
15802 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15803 GIVE_UP (14);
15804
15805 /* If window start is unchanged, we can reuse the whole matrix
15806 as is, without changing glyph positions since no text has
15807 been added/removed in front of the window end. */
15808 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15809 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15810 /* PT must not be in a partially visible line. */
15811 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15812 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15813 {
15814 /* We have to compute the window end anew since text
15815 could have been added/removed after it. */
15816 w->window_end_pos
15817 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15818 w->window_end_bytepos
15819 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15820
15821 /* Set the cursor. */
15822 row = row_containing_pos (w, PT, r0, NULL, 0);
15823 if (row)
15824 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15825 else
15826 abort ();
15827 return 2;
15828 }
15829 }
15830
15831 /* Give up if window start is in the changed area.
15832
15833 The condition used to read
15834
15835 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15836
15837 but why that was tested escapes me at the moment. */
15838 if (CHARPOS (start) >= first_changed_charpos
15839 && CHARPOS (start) <= last_changed_charpos)
15840 GIVE_UP (15);
15841
15842 /* Check that window start agrees with the start of the first glyph
15843 row in its current matrix. Check this after we know the window
15844 start is not in changed text, otherwise positions would not be
15845 comparable. */
15846 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15847 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15848 GIVE_UP (16);
15849
15850 /* Give up if the window ends in strings. Overlay strings
15851 at the end are difficult to handle, so don't try. */
15852 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15853 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15854 GIVE_UP (20);
15855
15856 /* Compute the position at which we have to start displaying new
15857 lines. Some of the lines at the top of the window might be
15858 reusable because they are not displaying changed text. Find the
15859 last row in W's current matrix not affected by changes at the
15860 start of current_buffer. Value is null if changes start in the
15861 first line of window. */
15862 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15863 if (last_unchanged_at_beg_row)
15864 {
15865 /* Avoid starting to display in the moddle of a character, a TAB
15866 for instance. This is easier than to set up the iterator
15867 exactly, and it's not a frequent case, so the additional
15868 effort wouldn't really pay off. */
15869 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15870 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15871 && last_unchanged_at_beg_row > w->current_matrix->rows)
15872 --last_unchanged_at_beg_row;
15873
15874 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15875 GIVE_UP (17);
15876
15877 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15878 GIVE_UP (18);
15879 start_pos = it.current.pos;
15880
15881 /* Start displaying new lines in the desired matrix at the same
15882 vpos we would use in the current matrix, i.e. below
15883 last_unchanged_at_beg_row. */
15884 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15885 current_matrix);
15886 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15887 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15888
15889 xassert (it.hpos == 0 && it.current_x == 0);
15890 }
15891 else
15892 {
15893 /* There are no reusable lines at the start of the window.
15894 Start displaying in the first text line. */
15895 start_display (&it, w, start);
15896 it.vpos = it.first_vpos;
15897 start_pos = it.current.pos;
15898 }
15899
15900 /* Find the first row that is not affected by changes at the end of
15901 the buffer. Value will be null if there is no unchanged row, in
15902 which case we must redisplay to the end of the window. delta
15903 will be set to the value by which buffer positions beginning with
15904 first_unchanged_at_end_row have to be adjusted due to text
15905 changes. */
15906 first_unchanged_at_end_row
15907 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15908 IF_DEBUG (debug_delta = delta);
15909 IF_DEBUG (debug_delta_bytes = delta_bytes);
15910
15911 /* Set stop_pos to the buffer position up to which we will have to
15912 display new lines. If first_unchanged_at_end_row != NULL, this
15913 is the buffer position of the start of the line displayed in that
15914 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15915 that we don't stop at a buffer position. */
15916 stop_pos = 0;
15917 if (first_unchanged_at_end_row)
15918 {
15919 xassert (last_unchanged_at_beg_row == NULL
15920 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15921
15922 /* If this is a continuation line, move forward to the next one
15923 that isn't. Changes in lines above affect this line.
15924 Caution: this may move first_unchanged_at_end_row to a row
15925 not displaying text. */
15926 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15927 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15928 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15929 < it.last_visible_y))
15930 ++first_unchanged_at_end_row;
15931
15932 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15933 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15934 >= it.last_visible_y))
15935 first_unchanged_at_end_row = NULL;
15936 else
15937 {
15938 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15939 + delta);
15940 first_unchanged_at_end_vpos
15941 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15942 xassert (stop_pos >= Z - END_UNCHANGED);
15943 }
15944 }
15945 else if (last_unchanged_at_beg_row == NULL)
15946 GIVE_UP (19);
15947
15948
15949 #if GLYPH_DEBUG
15950
15951 /* Either there is no unchanged row at the end, or the one we have
15952 now displays text. This is a necessary condition for the window
15953 end pos calculation at the end of this function. */
15954 xassert (first_unchanged_at_end_row == NULL
15955 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15956
15957 debug_last_unchanged_at_beg_vpos
15958 = (last_unchanged_at_beg_row
15959 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15960 : -1);
15961 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15962
15963 #endif /* GLYPH_DEBUG != 0 */
15964
15965
15966 /* Display new lines. Set last_text_row to the last new line
15967 displayed which has text on it, i.e. might end up as being the
15968 line where the window_end_vpos is. */
15969 w->cursor.vpos = -1;
15970 last_text_row = NULL;
15971 overlay_arrow_seen = 0;
15972 while (it.current_y < it.last_visible_y
15973 && !fonts_changed_p
15974 && (first_unchanged_at_end_row == NULL
15975 || IT_CHARPOS (it) < stop_pos))
15976 {
15977 if (display_line (&it))
15978 last_text_row = it.glyph_row - 1;
15979 }
15980
15981 if (fonts_changed_p)
15982 return -1;
15983
15984
15985 /* Compute differences in buffer positions, y-positions etc. for
15986 lines reused at the bottom of the window. Compute what we can
15987 scroll. */
15988 if (first_unchanged_at_end_row
15989 /* No lines reused because we displayed everything up to the
15990 bottom of the window. */
15991 && it.current_y < it.last_visible_y)
15992 {
15993 dvpos = (it.vpos
15994 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15995 current_matrix));
15996 dy = it.current_y - first_unchanged_at_end_row->y;
15997 run.current_y = first_unchanged_at_end_row->y;
15998 run.desired_y = run.current_y + dy;
15999 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16000 }
16001 else
16002 {
16003 delta = delta_bytes = dvpos = dy
16004 = run.current_y = run.desired_y = run.height = 0;
16005 first_unchanged_at_end_row = NULL;
16006 }
16007 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16008
16009
16010 /* Find the cursor if not already found. We have to decide whether
16011 PT will appear on this window (it sometimes doesn't, but this is
16012 not a very frequent case.) This decision has to be made before
16013 the current matrix is altered. A value of cursor.vpos < 0 means
16014 that PT is either in one of the lines beginning at
16015 first_unchanged_at_end_row or below the window. Don't care for
16016 lines that might be displayed later at the window end; as
16017 mentioned, this is not a frequent case. */
16018 if (w->cursor.vpos < 0)
16019 {
16020 /* Cursor in unchanged rows at the top? */
16021 if (PT < CHARPOS (start_pos)
16022 && last_unchanged_at_beg_row)
16023 {
16024 row = row_containing_pos (w, PT,
16025 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16026 last_unchanged_at_beg_row + 1, 0);
16027 if (row)
16028 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16029 }
16030
16031 /* Start from first_unchanged_at_end_row looking for PT. */
16032 else if (first_unchanged_at_end_row)
16033 {
16034 row = row_containing_pos (w, PT - delta,
16035 first_unchanged_at_end_row, NULL, 0);
16036 if (row)
16037 set_cursor_from_row (w, row, w->current_matrix, delta,
16038 delta_bytes, dy, dvpos);
16039 }
16040
16041 /* Give up if cursor was not found. */
16042 if (w->cursor.vpos < 0)
16043 {
16044 clear_glyph_matrix (w->desired_matrix);
16045 return -1;
16046 }
16047 }
16048
16049 /* Don't let the cursor end in the scroll margins. */
16050 {
16051 int this_scroll_margin, cursor_height;
16052
16053 this_scroll_margin = max (0, scroll_margin);
16054 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16055 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16056 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16057
16058 if ((w->cursor.y < this_scroll_margin
16059 && CHARPOS (start) > BEGV)
16060 /* Old redisplay didn't take scroll margin into account at the bottom,
16061 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16062 || (w->cursor.y + (make_cursor_line_fully_visible_p
16063 ? cursor_height + this_scroll_margin
16064 : 1)) > it.last_visible_y)
16065 {
16066 w->cursor.vpos = -1;
16067 clear_glyph_matrix (w->desired_matrix);
16068 return -1;
16069 }
16070 }
16071
16072 /* Scroll the display. Do it before changing the current matrix so
16073 that xterm.c doesn't get confused about where the cursor glyph is
16074 found. */
16075 if (dy && run.height)
16076 {
16077 update_begin (f);
16078
16079 if (FRAME_WINDOW_P (f))
16080 {
16081 FRAME_RIF (f)->update_window_begin_hook (w);
16082 FRAME_RIF (f)->clear_window_mouse_face (w);
16083 FRAME_RIF (f)->scroll_run_hook (w, &run);
16084 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16085 }
16086 else
16087 {
16088 /* Terminal frame. In this case, dvpos gives the number of
16089 lines to scroll by; dvpos < 0 means scroll up. */
16090 int from_vpos
16091 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16092 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16093 int end = (WINDOW_TOP_EDGE_LINE (w)
16094 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16095 + window_internal_height (w));
16096
16097 #if defined (HAVE_GPM) || defined (MSDOS)
16098 x_clear_window_mouse_face (w);
16099 #endif
16100 /* Perform the operation on the screen. */
16101 if (dvpos > 0)
16102 {
16103 /* Scroll last_unchanged_at_beg_row to the end of the
16104 window down dvpos lines. */
16105 set_terminal_window (f, end);
16106
16107 /* On dumb terminals delete dvpos lines at the end
16108 before inserting dvpos empty lines. */
16109 if (!FRAME_SCROLL_REGION_OK (f))
16110 ins_del_lines (f, end - dvpos, -dvpos);
16111
16112 /* Insert dvpos empty lines in front of
16113 last_unchanged_at_beg_row. */
16114 ins_del_lines (f, from, dvpos);
16115 }
16116 else if (dvpos < 0)
16117 {
16118 /* Scroll up last_unchanged_at_beg_vpos to the end of
16119 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16120 set_terminal_window (f, end);
16121
16122 /* Delete dvpos lines in front of
16123 last_unchanged_at_beg_vpos. ins_del_lines will set
16124 the cursor to the given vpos and emit |dvpos| delete
16125 line sequences. */
16126 ins_del_lines (f, from + dvpos, dvpos);
16127
16128 /* On a dumb terminal insert dvpos empty lines at the
16129 end. */
16130 if (!FRAME_SCROLL_REGION_OK (f))
16131 ins_del_lines (f, end + dvpos, -dvpos);
16132 }
16133
16134 set_terminal_window (f, 0);
16135 }
16136
16137 update_end (f);
16138 }
16139
16140 /* Shift reused rows of the current matrix to the right position.
16141 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16142 text. */
16143 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16144 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16145 if (dvpos < 0)
16146 {
16147 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16148 bottom_vpos, dvpos);
16149 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16150 bottom_vpos, 0);
16151 }
16152 else if (dvpos > 0)
16153 {
16154 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16155 bottom_vpos, dvpos);
16156 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16157 first_unchanged_at_end_vpos + dvpos, 0);
16158 }
16159
16160 /* For frame-based redisplay, make sure that current frame and window
16161 matrix are in sync with respect to glyph memory. */
16162 if (!FRAME_WINDOW_P (f))
16163 sync_frame_with_window_matrix_rows (w);
16164
16165 /* Adjust buffer positions in reused rows. */
16166 if (delta || delta_bytes)
16167 increment_matrix_positions (current_matrix,
16168 first_unchanged_at_end_vpos + dvpos,
16169 bottom_vpos, delta, delta_bytes);
16170
16171 /* Adjust Y positions. */
16172 if (dy)
16173 shift_glyph_matrix (w, current_matrix,
16174 first_unchanged_at_end_vpos + dvpos,
16175 bottom_vpos, dy);
16176
16177 if (first_unchanged_at_end_row)
16178 {
16179 first_unchanged_at_end_row += dvpos;
16180 if (first_unchanged_at_end_row->y >= it.last_visible_y
16181 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16182 first_unchanged_at_end_row = NULL;
16183 }
16184
16185 /* If scrolling up, there may be some lines to display at the end of
16186 the window. */
16187 last_text_row_at_end = NULL;
16188 if (dy < 0)
16189 {
16190 /* Scrolling up can leave for example a partially visible line
16191 at the end of the window to be redisplayed. */
16192 /* Set last_row to the glyph row in the current matrix where the
16193 window end line is found. It has been moved up or down in
16194 the matrix by dvpos. */
16195 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16196 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16197
16198 /* If last_row is the window end line, it should display text. */
16199 xassert (last_row->displays_text_p);
16200
16201 /* If window end line was partially visible before, begin
16202 displaying at that line. Otherwise begin displaying with the
16203 line following it. */
16204 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16205 {
16206 init_to_row_start (&it, w, last_row);
16207 it.vpos = last_vpos;
16208 it.current_y = last_row->y;
16209 }
16210 else
16211 {
16212 init_to_row_end (&it, w, last_row);
16213 it.vpos = 1 + last_vpos;
16214 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16215 ++last_row;
16216 }
16217
16218 /* We may start in a continuation line. If so, we have to
16219 get the right continuation_lines_width and current_x. */
16220 it.continuation_lines_width = last_row->continuation_lines_width;
16221 it.hpos = it.current_x = 0;
16222
16223 /* Display the rest of the lines at the window end. */
16224 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16225 while (it.current_y < it.last_visible_y
16226 && !fonts_changed_p)
16227 {
16228 /* Is it always sure that the display agrees with lines in
16229 the current matrix? I don't think so, so we mark rows
16230 displayed invalid in the current matrix by setting their
16231 enabled_p flag to zero. */
16232 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16233 if (display_line (&it))
16234 last_text_row_at_end = it.glyph_row - 1;
16235 }
16236 }
16237
16238 /* Update window_end_pos and window_end_vpos. */
16239 if (first_unchanged_at_end_row
16240 && !last_text_row_at_end)
16241 {
16242 /* Window end line if one of the preserved rows from the current
16243 matrix. Set row to the last row displaying text in current
16244 matrix starting at first_unchanged_at_end_row, after
16245 scrolling. */
16246 xassert (first_unchanged_at_end_row->displays_text_p);
16247 row = find_last_row_displaying_text (w->current_matrix, &it,
16248 first_unchanged_at_end_row);
16249 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16250
16251 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16252 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16253 w->window_end_vpos
16254 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16255 xassert (w->window_end_bytepos >= 0);
16256 IF_DEBUG (debug_method_add (w, "A"));
16257 }
16258 else if (last_text_row_at_end)
16259 {
16260 w->window_end_pos
16261 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16262 w->window_end_bytepos
16263 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16264 w->window_end_vpos
16265 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16266 xassert (w->window_end_bytepos >= 0);
16267 IF_DEBUG (debug_method_add (w, "B"));
16268 }
16269 else if (last_text_row)
16270 {
16271 /* We have displayed either to the end of the window or at the
16272 end of the window, i.e. the last row with text is to be found
16273 in the desired matrix. */
16274 w->window_end_pos
16275 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16276 w->window_end_bytepos
16277 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16278 w->window_end_vpos
16279 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16280 xassert (w->window_end_bytepos >= 0);
16281 }
16282 else if (first_unchanged_at_end_row == NULL
16283 && last_text_row == NULL
16284 && last_text_row_at_end == NULL)
16285 {
16286 /* Displayed to end of window, but no line containing text was
16287 displayed. Lines were deleted at the end of the window. */
16288 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16289 int vpos = XFASTINT (w->window_end_vpos);
16290 struct glyph_row *current_row = current_matrix->rows + vpos;
16291 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16292
16293 for (row = NULL;
16294 row == NULL && vpos >= first_vpos;
16295 --vpos, --current_row, --desired_row)
16296 {
16297 if (desired_row->enabled_p)
16298 {
16299 if (desired_row->displays_text_p)
16300 row = desired_row;
16301 }
16302 else if (current_row->displays_text_p)
16303 row = current_row;
16304 }
16305
16306 xassert (row != NULL);
16307 w->window_end_vpos = make_number (vpos + 1);
16308 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16309 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16310 xassert (w->window_end_bytepos >= 0);
16311 IF_DEBUG (debug_method_add (w, "C"));
16312 }
16313 else
16314 abort ();
16315
16316 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16317 debug_end_vpos = XFASTINT (w->window_end_vpos));
16318
16319 /* Record that display has not been completed. */
16320 w->window_end_valid = Qnil;
16321 w->desired_matrix->no_scrolling_p = 1;
16322 return 3;
16323
16324 #undef GIVE_UP
16325 }
16326
16327
16328 \f
16329 /***********************************************************************
16330 More debugging support
16331 ***********************************************************************/
16332
16333 #if GLYPH_DEBUG
16334
16335 void dump_glyph_row (struct glyph_row *, int, int);
16336 void dump_glyph_matrix (struct glyph_matrix *, int);
16337 void dump_glyph (struct glyph_row *, struct glyph *, int);
16338
16339
16340 /* Dump the contents of glyph matrix MATRIX on stderr.
16341
16342 GLYPHS 0 means don't show glyph contents.
16343 GLYPHS 1 means show glyphs in short form
16344 GLYPHS > 1 means show glyphs in long form. */
16345
16346 void
16347 dump_glyph_matrix (matrix, glyphs)
16348 struct glyph_matrix *matrix;
16349 int glyphs;
16350 {
16351 int i;
16352 for (i = 0; i < matrix->nrows; ++i)
16353 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16354 }
16355
16356
16357 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16358 the glyph row and area where the glyph comes from. */
16359
16360 void
16361 dump_glyph (row, glyph, area)
16362 struct glyph_row *row;
16363 struct glyph *glyph;
16364 int area;
16365 {
16366 if (glyph->type == CHAR_GLYPH)
16367 {
16368 fprintf (stderr,
16369 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16370 glyph - row->glyphs[TEXT_AREA],
16371 'C',
16372 glyph->charpos,
16373 (BUFFERP (glyph->object)
16374 ? 'B'
16375 : (STRINGP (glyph->object)
16376 ? 'S'
16377 : '-')),
16378 glyph->pixel_width,
16379 glyph->u.ch,
16380 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16381 ? glyph->u.ch
16382 : '.'),
16383 glyph->face_id,
16384 glyph->left_box_line_p,
16385 glyph->right_box_line_p);
16386 }
16387 else if (glyph->type == STRETCH_GLYPH)
16388 {
16389 fprintf (stderr,
16390 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16391 glyph - row->glyphs[TEXT_AREA],
16392 'S',
16393 glyph->charpos,
16394 (BUFFERP (glyph->object)
16395 ? 'B'
16396 : (STRINGP (glyph->object)
16397 ? 'S'
16398 : '-')),
16399 glyph->pixel_width,
16400 0,
16401 '.',
16402 glyph->face_id,
16403 glyph->left_box_line_p,
16404 glyph->right_box_line_p);
16405 }
16406 else if (glyph->type == IMAGE_GLYPH)
16407 {
16408 fprintf (stderr,
16409 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16410 glyph - row->glyphs[TEXT_AREA],
16411 'I',
16412 glyph->charpos,
16413 (BUFFERP (glyph->object)
16414 ? 'B'
16415 : (STRINGP (glyph->object)
16416 ? 'S'
16417 : '-')),
16418 glyph->pixel_width,
16419 glyph->u.img_id,
16420 '.',
16421 glyph->face_id,
16422 glyph->left_box_line_p,
16423 glyph->right_box_line_p);
16424 }
16425 else if (glyph->type == COMPOSITE_GLYPH)
16426 {
16427 fprintf (stderr,
16428 " %5d %4c %6d %c %3d 0x%05x",
16429 glyph - row->glyphs[TEXT_AREA],
16430 '+',
16431 glyph->charpos,
16432 (BUFFERP (glyph->object)
16433 ? 'B'
16434 : (STRINGP (glyph->object)
16435 ? 'S'
16436 : '-')),
16437 glyph->pixel_width,
16438 glyph->u.cmp.id);
16439 if (glyph->u.cmp.automatic)
16440 fprintf (stderr,
16441 "[%d-%d]",
16442 glyph->slice.cmp.from, glyph->slice.cmp.to);
16443 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16444 glyph->face_id,
16445 glyph->left_box_line_p,
16446 glyph->right_box_line_p);
16447 }
16448 }
16449
16450
16451 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16452 GLYPHS 0 means don't show glyph contents.
16453 GLYPHS 1 means show glyphs in short form
16454 GLYPHS > 1 means show glyphs in long form. */
16455
16456 void
16457 dump_glyph_row (row, vpos, glyphs)
16458 struct glyph_row *row;
16459 int vpos, glyphs;
16460 {
16461 if (glyphs != 1)
16462 {
16463 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16464 fprintf (stderr, "======================================================================\n");
16465
16466 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16467 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16468 vpos,
16469 MATRIX_ROW_START_CHARPOS (row),
16470 MATRIX_ROW_END_CHARPOS (row),
16471 row->used[TEXT_AREA],
16472 row->contains_overlapping_glyphs_p,
16473 row->enabled_p,
16474 row->truncated_on_left_p,
16475 row->truncated_on_right_p,
16476 row->continued_p,
16477 MATRIX_ROW_CONTINUATION_LINE_P (row),
16478 row->displays_text_p,
16479 row->ends_at_zv_p,
16480 row->fill_line_p,
16481 row->ends_in_middle_of_char_p,
16482 row->starts_in_middle_of_char_p,
16483 row->mouse_face_p,
16484 row->x,
16485 row->y,
16486 row->pixel_width,
16487 row->height,
16488 row->visible_height,
16489 row->ascent,
16490 row->phys_ascent);
16491 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16492 row->end.overlay_string_index,
16493 row->continuation_lines_width);
16494 fprintf (stderr, "%9d %5d\n",
16495 CHARPOS (row->start.string_pos),
16496 CHARPOS (row->end.string_pos));
16497 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16498 row->end.dpvec_index);
16499 }
16500
16501 if (glyphs > 1)
16502 {
16503 int area;
16504
16505 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16506 {
16507 struct glyph *glyph = row->glyphs[area];
16508 struct glyph *glyph_end = glyph + row->used[area];
16509
16510 /* Glyph for a line end in text. */
16511 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16512 ++glyph_end;
16513
16514 if (glyph < glyph_end)
16515 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16516
16517 for (; glyph < glyph_end; ++glyph)
16518 dump_glyph (row, glyph, area);
16519 }
16520 }
16521 else if (glyphs == 1)
16522 {
16523 int area;
16524
16525 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16526 {
16527 char *s = (char *) alloca (row->used[area] + 1);
16528 int i;
16529
16530 for (i = 0; i < row->used[area]; ++i)
16531 {
16532 struct glyph *glyph = row->glyphs[area] + i;
16533 if (glyph->type == CHAR_GLYPH
16534 && glyph->u.ch < 0x80
16535 && glyph->u.ch >= ' ')
16536 s[i] = glyph->u.ch;
16537 else
16538 s[i] = '.';
16539 }
16540
16541 s[i] = '\0';
16542 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16543 }
16544 }
16545 }
16546
16547
16548 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16549 Sdump_glyph_matrix, 0, 1, "p",
16550 doc: /* Dump the current matrix of the selected window to stderr.
16551 Shows contents of glyph row structures. With non-nil
16552 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16553 glyphs in short form, otherwise show glyphs in long form. */)
16554 (Lisp_Object glyphs)
16555 {
16556 struct window *w = XWINDOW (selected_window);
16557 struct buffer *buffer = XBUFFER (w->buffer);
16558
16559 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16560 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16561 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16562 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16563 fprintf (stderr, "=============================================\n");
16564 dump_glyph_matrix (w->current_matrix,
16565 NILP (glyphs) ? 0 : XINT (glyphs));
16566 return Qnil;
16567 }
16568
16569
16570 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16571 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16572 (void)
16573 {
16574 struct frame *f = XFRAME (selected_frame);
16575 dump_glyph_matrix (f->current_matrix, 1);
16576 return Qnil;
16577 }
16578
16579
16580 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16581 doc: /* Dump glyph row ROW to stderr.
16582 GLYPH 0 means don't dump glyphs.
16583 GLYPH 1 means dump glyphs in short form.
16584 GLYPH > 1 or omitted means dump glyphs in long form. */)
16585 (Lisp_Object row, Lisp_Object glyphs)
16586 {
16587 struct glyph_matrix *matrix;
16588 int vpos;
16589
16590 CHECK_NUMBER (row);
16591 matrix = XWINDOW (selected_window)->current_matrix;
16592 vpos = XINT (row);
16593 if (vpos >= 0 && vpos < matrix->nrows)
16594 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16595 vpos,
16596 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16597 return Qnil;
16598 }
16599
16600
16601 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16602 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16603 GLYPH 0 means don't dump glyphs.
16604 GLYPH 1 means dump glyphs in short form.
16605 GLYPH > 1 or omitted means dump glyphs in long form. */)
16606 (Lisp_Object row, Lisp_Object glyphs)
16607 {
16608 struct frame *sf = SELECTED_FRAME ();
16609 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16610 int vpos;
16611
16612 CHECK_NUMBER (row);
16613 vpos = XINT (row);
16614 if (vpos >= 0 && vpos < m->nrows)
16615 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16616 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16617 return Qnil;
16618 }
16619
16620
16621 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16622 doc: /* Toggle tracing of redisplay.
16623 With ARG, turn tracing on if and only if ARG is positive. */)
16624 (Lisp_Object arg)
16625 {
16626 if (NILP (arg))
16627 trace_redisplay_p = !trace_redisplay_p;
16628 else
16629 {
16630 arg = Fprefix_numeric_value (arg);
16631 trace_redisplay_p = XINT (arg) > 0;
16632 }
16633
16634 return Qnil;
16635 }
16636
16637
16638 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16639 doc: /* Like `format', but print result to stderr.
16640 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16641 (size_t nargs, Lisp_Object *args)
16642 {
16643 Lisp_Object s = Fformat (nargs, args);
16644 fprintf (stderr, "%s", SDATA (s));
16645 return Qnil;
16646 }
16647
16648 #endif /* GLYPH_DEBUG */
16649
16650
16651 \f
16652 /***********************************************************************
16653 Building Desired Matrix Rows
16654 ***********************************************************************/
16655
16656 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16657 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16658
16659 static struct glyph_row *
16660 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16661 {
16662 struct frame *f = XFRAME (WINDOW_FRAME (w));
16663 struct buffer *buffer = XBUFFER (w->buffer);
16664 struct buffer *old = current_buffer;
16665 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16666 int arrow_len = SCHARS (overlay_arrow_string);
16667 const unsigned char *arrow_end = arrow_string + arrow_len;
16668 const unsigned char *p;
16669 struct it it;
16670 int multibyte_p;
16671 int n_glyphs_before;
16672
16673 set_buffer_temp (buffer);
16674 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16675 it.glyph_row->used[TEXT_AREA] = 0;
16676 SET_TEXT_POS (it.position, 0, 0);
16677
16678 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
16679 p = arrow_string;
16680 while (p < arrow_end)
16681 {
16682 Lisp_Object face, ilisp;
16683
16684 /* Get the next character. */
16685 if (multibyte_p)
16686 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16687 else
16688 {
16689 it.c = it.char_to_display = *p, it.len = 1;
16690 if (! ASCII_CHAR_P (it.c))
16691 it.char_to_display = BYTE8_TO_CHAR (it.c);
16692 }
16693 p += it.len;
16694
16695 /* Get its face. */
16696 ilisp = make_number (p - arrow_string);
16697 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16698 it.face_id = compute_char_face (f, it.char_to_display, face);
16699
16700 /* Compute its width, get its glyphs. */
16701 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16702 SET_TEXT_POS (it.position, -1, -1);
16703 PRODUCE_GLYPHS (&it);
16704
16705 /* If this character doesn't fit any more in the line, we have
16706 to remove some glyphs. */
16707 if (it.current_x > it.last_visible_x)
16708 {
16709 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16710 break;
16711 }
16712 }
16713
16714 set_buffer_temp (old);
16715 return it.glyph_row;
16716 }
16717
16718
16719 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16720 glyphs are only inserted for terminal frames since we can't really
16721 win with truncation glyphs when partially visible glyphs are
16722 involved. Which glyphs to insert is determined by
16723 produce_special_glyphs. */
16724
16725 static void
16726 insert_left_trunc_glyphs (struct it *it)
16727 {
16728 struct it truncate_it;
16729 struct glyph *from, *end, *to, *toend;
16730
16731 xassert (!FRAME_WINDOW_P (it->f));
16732
16733 /* Get the truncation glyphs. */
16734 truncate_it = *it;
16735 truncate_it.current_x = 0;
16736 truncate_it.face_id = DEFAULT_FACE_ID;
16737 truncate_it.glyph_row = &scratch_glyph_row;
16738 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16739 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16740 truncate_it.object = make_number (0);
16741 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16742
16743 /* Overwrite glyphs from IT with truncation glyphs. */
16744 if (!it->glyph_row->reversed_p)
16745 {
16746 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16747 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16748 to = it->glyph_row->glyphs[TEXT_AREA];
16749 toend = to + it->glyph_row->used[TEXT_AREA];
16750
16751 while (from < end)
16752 *to++ = *from++;
16753
16754 /* There may be padding glyphs left over. Overwrite them too. */
16755 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16756 {
16757 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16758 while (from < end)
16759 *to++ = *from++;
16760 }
16761
16762 if (to > toend)
16763 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16764 }
16765 else
16766 {
16767 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16768 that back to front. */
16769 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16770 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16771 toend = it->glyph_row->glyphs[TEXT_AREA];
16772 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16773
16774 while (from >= end && to >= toend)
16775 *to-- = *from--;
16776 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16777 {
16778 from =
16779 truncate_it.glyph_row->glyphs[TEXT_AREA]
16780 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16781 while (from >= end && to >= toend)
16782 *to-- = *from--;
16783 }
16784 if (from >= end)
16785 {
16786 /* Need to free some room before prepending additional
16787 glyphs. */
16788 int move_by = from - end + 1;
16789 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16790 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16791
16792 for ( ; g >= g0; g--)
16793 g[move_by] = *g;
16794 while (from >= end)
16795 *to-- = *from--;
16796 it->glyph_row->used[TEXT_AREA] += move_by;
16797 }
16798 }
16799 }
16800
16801
16802 /* Compute the pixel height and width of IT->glyph_row.
16803
16804 Most of the time, ascent and height of a display line will be equal
16805 to the max_ascent and max_height values of the display iterator
16806 structure. This is not the case if
16807
16808 1. We hit ZV without displaying anything. In this case, max_ascent
16809 and max_height will be zero.
16810
16811 2. We have some glyphs that don't contribute to the line height.
16812 (The glyph row flag contributes_to_line_height_p is for future
16813 pixmap extensions).
16814
16815 The first case is easily covered by using default values because in
16816 these cases, the line height does not really matter, except that it
16817 must not be zero. */
16818
16819 static void
16820 compute_line_metrics (struct it *it)
16821 {
16822 struct glyph_row *row = it->glyph_row;
16823
16824 if (FRAME_WINDOW_P (it->f))
16825 {
16826 int i, min_y, max_y;
16827
16828 /* The line may consist of one space only, that was added to
16829 place the cursor on it. If so, the row's height hasn't been
16830 computed yet. */
16831 if (row->height == 0)
16832 {
16833 if (it->max_ascent + it->max_descent == 0)
16834 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16835 row->ascent = it->max_ascent;
16836 row->height = it->max_ascent + it->max_descent;
16837 row->phys_ascent = it->max_phys_ascent;
16838 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16839 row->extra_line_spacing = it->max_extra_line_spacing;
16840 }
16841
16842 /* Compute the width of this line. */
16843 row->pixel_width = row->x;
16844 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16845 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16846
16847 xassert (row->pixel_width >= 0);
16848 xassert (row->ascent >= 0 && row->height > 0);
16849
16850 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16851 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16852
16853 /* If first line's physical ascent is larger than its logical
16854 ascent, use the physical ascent, and make the row taller.
16855 This makes accented characters fully visible. */
16856 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16857 && row->phys_ascent > row->ascent)
16858 {
16859 row->height += row->phys_ascent - row->ascent;
16860 row->ascent = row->phys_ascent;
16861 }
16862
16863 /* Compute how much of the line is visible. */
16864 row->visible_height = row->height;
16865
16866 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16867 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16868
16869 if (row->y < min_y)
16870 row->visible_height -= min_y - row->y;
16871 if (row->y + row->height > max_y)
16872 row->visible_height -= row->y + row->height - max_y;
16873 }
16874 else
16875 {
16876 row->pixel_width = row->used[TEXT_AREA];
16877 if (row->continued_p)
16878 row->pixel_width -= it->continuation_pixel_width;
16879 else if (row->truncated_on_right_p)
16880 row->pixel_width -= it->truncation_pixel_width;
16881 row->ascent = row->phys_ascent = 0;
16882 row->height = row->phys_height = row->visible_height = 1;
16883 row->extra_line_spacing = 0;
16884 }
16885
16886 /* Compute a hash code for this row. */
16887 {
16888 int area, i;
16889 row->hash = 0;
16890 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16891 for (i = 0; i < row->used[area]; ++i)
16892 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16893 + row->glyphs[area][i].u.val
16894 + row->glyphs[area][i].face_id
16895 + row->glyphs[area][i].padding_p
16896 + (row->glyphs[area][i].type << 2));
16897 }
16898
16899 it->max_ascent = it->max_descent = 0;
16900 it->max_phys_ascent = it->max_phys_descent = 0;
16901 }
16902
16903
16904 /* Append one space to the glyph row of iterator IT if doing a
16905 window-based redisplay. The space has the same face as
16906 IT->face_id. Value is non-zero if a space was added.
16907
16908 This function is called to make sure that there is always one glyph
16909 at the end of a glyph row that the cursor can be set on under
16910 window-systems. (If there weren't such a glyph we would not know
16911 how wide and tall a box cursor should be displayed).
16912
16913 At the same time this space let's a nicely handle clearing to the
16914 end of the line if the row ends in italic text. */
16915
16916 static int
16917 append_space_for_newline (struct it *it, int default_face_p)
16918 {
16919 if (FRAME_WINDOW_P (it->f))
16920 {
16921 int n = it->glyph_row->used[TEXT_AREA];
16922
16923 if (it->glyph_row->glyphs[TEXT_AREA] + n
16924 < it->glyph_row->glyphs[1 + TEXT_AREA])
16925 {
16926 /* Save some values that must not be changed.
16927 Must save IT->c and IT->len because otherwise
16928 ITERATOR_AT_END_P wouldn't work anymore after
16929 append_space_for_newline has been called. */
16930 enum display_element_type saved_what = it->what;
16931 int saved_c = it->c, saved_len = it->len;
16932 int saved_char_to_display = it->char_to_display;
16933 int saved_x = it->current_x;
16934 int saved_face_id = it->face_id;
16935 struct text_pos saved_pos;
16936 Lisp_Object saved_object;
16937 struct face *face;
16938
16939 saved_object = it->object;
16940 saved_pos = it->position;
16941
16942 it->what = IT_CHARACTER;
16943 memset (&it->position, 0, sizeof it->position);
16944 it->object = make_number (0);
16945 it->c = it->char_to_display = ' ';
16946 it->len = 1;
16947
16948 if (default_face_p)
16949 it->face_id = DEFAULT_FACE_ID;
16950 else if (it->face_before_selective_p)
16951 it->face_id = it->saved_face_id;
16952 face = FACE_FROM_ID (it->f, it->face_id);
16953 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16954
16955 PRODUCE_GLYPHS (it);
16956
16957 it->override_ascent = -1;
16958 it->constrain_row_ascent_descent_p = 0;
16959 it->current_x = saved_x;
16960 it->object = saved_object;
16961 it->position = saved_pos;
16962 it->what = saved_what;
16963 it->face_id = saved_face_id;
16964 it->len = saved_len;
16965 it->c = saved_c;
16966 it->char_to_display = saved_char_to_display;
16967 return 1;
16968 }
16969 }
16970
16971 return 0;
16972 }
16973
16974
16975 /* Extend the face of the last glyph in the text area of IT->glyph_row
16976 to the end of the display line. Called from display_line. If the
16977 glyph row is empty, add a space glyph to it so that we know the
16978 face to draw. Set the glyph row flag fill_line_p. If the glyph
16979 row is R2L, prepend a stretch glyph to cover the empty space to the
16980 left of the leftmost glyph. */
16981
16982 static void
16983 extend_face_to_end_of_line (struct it *it)
16984 {
16985 struct face *face;
16986 struct frame *f = it->f;
16987
16988 /* If line is already filled, do nothing. Non window-system frames
16989 get a grace of one more ``pixel'' because their characters are
16990 1-``pixel'' wide, so they hit the equality too early. This grace
16991 is needed only for R2L rows that are not continued, to produce
16992 one extra blank where we could display the cursor. */
16993 if (it->current_x >= it->last_visible_x
16994 + (!FRAME_WINDOW_P (f)
16995 && it->glyph_row->reversed_p
16996 && !it->glyph_row->continued_p))
16997 return;
16998
16999 /* Face extension extends the background and box of IT->face_id
17000 to the end of the line. If the background equals the background
17001 of the frame, we don't have to do anything. */
17002 if (it->face_before_selective_p)
17003 face = FACE_FROM_ID (f, it->saved_face_id);
17004 else
17005 face = FACE_FROM_ID (f, it->face_id);
17006
17007 if (FRAME_WINDOW_P (f)
17008 && it->glyph_row->displays_text_p
17009 && face->box == FACE_NO_BOX
17010 && face->background == FRAME_BACKGROUND_PIXEL (f)
17011 && !face->stipple
17012 && !it->glyph_row->reversed_p)
17013 return;
17014
17015 /* Set the glyph row flag indicating that the face of the last glyph
17016 in the text area has to be drawn to the end of the text area. */
17017 it->glyph_row->fill_line_p = 1;
17018
17019 /* If current character of IT is not ASCII, make sure we have the
17020 ASCII face. This will be automatically undone the next time
17021 get_next_display_element returns a multibyte character. Note
17022 that the character will always be single byte in unibyte
17023 text. */
17024 if (!ASCII_CHAR_P (it->c))
17025 {
17026 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17027 }
17028
17029 if (FRAME_WINDOW_P (f))
17030 {
17031 /* If the row is empty, add a space with the current face of IT,
17032 so that we know which face to draw. */
17033 if (it->glyph_row->used[TEXT_AREA] == 0)
17034 {
17035 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17036 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17037 it->glyph_row->used[TEXT_AREA] = 1;
17038 }
17039 #ifdef HAVE_WINDOW_SYSTEM
17040 if (it->glyph_row->reversed_p)
17041 {
17042 /* Prepend a stretch glyph to the row, such that the
17043 rightmost glyph will be drawn flushed all the way to the
17044 right margin of the window. The stretch glyph that will
17045 occupy the empty space, if any, to the left of the
17046 glyphs. */
17047 struct font *font = face->font ? face->font : FRAME_FONT (f);
17048 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17049 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17050 struct glyph *g;
17051 int row_width, stretch_ascent, stretch_width;
17052 struct text_pos saved_pos;
17053 int saved_face_id, saved_avoid_cursor;
17054
17055 for (row_width = 0, g = row_start; g < row_end; g++)
17056 row_width += g->pixel_width;
17057 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17058 if (stretch_width > 0)
17059 {
17060 stretch_ascent =
17061 (((it->ascent + it->descent)
17062 * FONT_BASE (font)) / FONT_HEIGHT (font));
17063 saved_pos = it->position;
17064 memset (&it->position, 0, sizeof it->position);
17065 saved_avoid_cursor = it->avoid_cursor_p;
17066 it->avoid_cursor_p = 1;
17067 saved_face_id = it->face_id;
17068 /* The last row's stretch glyph should get the default
17069 face, to avoid painting the rest of the window with
17070 the region face, if the region ends at ZV. */
17071 if (it->glyph_row->ends_at_zv_p)
17072 it->face_id = DEFAULT_FACE_ID;
17073 else
17074 it->face_id = face->id;
17075 append_stretch_glyph (it, make_number (0), stretch_width,
17076 it->ascent + it->descent, stretch_ascent);
17077 it->position = saved_pos;
17078 it->avoid_cursor_p = saved_avoid_cursor;
17079 it->face_id = saved_face_id;
17080 }
17081 }
17082 #endif /* HAVE_WINDOW_SYSTEM */
17083 }
17084 else
17085 {
17086 /* Save some values that must not be changed. */
17087 int saved_x = it->current_x;
17088 struct text_pos saved_pos;
17089 Lisp_Object saved_object;
17090 enum display_element_type saved_what = it->what;
17091 int saved_face_id = it->face_id;
17092
17093 saved_object = it->object;
17094 saved_pos = it->position;
17095
17096 it->what = IT_CHARACTER;
17097 memset (&it->position, 0, sizeof it->position);
17098 it->object = make_number (0);
17099 it->c = it->char_to_display = ' ';
17100 it->len = 1;
17101 /* The last row's blank glyphs should get the default face, to
17102 avoid painting the rest of the window with the region face,
17103 if the region ends at ZV. */
17104 if (it->glyph_row->ends_at_zv_p)
17105 it->face_id = DEFAULT_FACE_ID;
17106 else
17107 it->face_id = face->id;
17108
17109 PRODUCE_GLYPHS (it);
17110
17111 while (it->current_x <= it->last_visible_x)
17112 PRODUCE_GLYPHS (it);
17113
17114 /* Don't count these blanks really. It would let us insert a left
17115 truncation glyph below and make us set the cursor on them, maybe. */
17116 it->current_x = saved_x;
17117 it->object = saved_object;
17118 it->position = saved_pos;
17119 it->what = saved_what;
17120 it->face_id = saved_face_id;
17121 }
17122 }
17123
17124
17125 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17126 trailing whitespace. */
17127
17128 static int
17129 trailing_whitespace_p (EMACS_INT charpos)
17130 {
17131 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17132 int c = 0;
17133
17134 while (bytepos < ZV_BYTE
17135 && (c = FETCH_CHAR (bytepos),
17136 c == ' ' || c == '\t'))
17137 ++bytepos;
17138
17139 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17140 {
17141 if (bytepos != PT_BYTE)
17142 return 1;
17143 }
17144 return 0;
17145 }
17146
17147
17148 /* Highlight trailing whitespace, if any, in ROW. */
17149
17150 static void
17151 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17152 {
17153 int used = row->used[TEXT_AREA];
17154
17155 if (used)
17156 {
17157 struct glyph *start = row->glyphs[TEXT_AREA];
17158 struct glyph *glyph = start + used - 1;
17159
17160 if (row->reversed_p)
17161 {
17162 /* Right-to-left rows need to be processed in the opposite
17163 direction, so swap the edge pointers. */
17164 glyph = start;
17165 start = row->glyphs[TEXT_AREA] + used - 1;
17166 }
17167
17168 /* Skip over glyphs inserted to display the cursor at the
17169 end of a line, for extending the face of the last glyph
17170 to the end of the line on terminals, and for truncation
17171 and continuation glyphs. */
17172 if (!row->reversed_p)
17173 {
17174 while (glyph >= start
17175 && glyph->type == CHAR_GLYPH
17176 && INTEGERP (glyph->object))
17177 --glyph;
17178 }
17179 else
17180 {
17181 while (glyph <= start
17182 && glyph->type == CHAR_GLYPH
17183 && INTEGERP (glyph->object))
17184 ++glyph;
17185 }
17186
17187 /* If last glyph is a space or stretch, and it's trailing
17188 whitespace, set the face of all trailing whitespace glyphs in
17189 IT->glyph_row to `trailing-whitespace'. */
17190 if ((row->reversed_p ? glyph <= start : glyph >= start)
17191 && BUFFERP (glyph->object)
17192 && (glyph->type == STRETCH_GLYPH
17193 || (glyph->type == CHAR_GLYPH
17194 && glyph->u.ch == ' '))
17195 && trailing_whitespace_p (glyph->charpos))
17196 {
17197 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17198 if (face_id < 0)
17199 return;
17200
17201 if (!row->reversed_p)
17202 {
17203 while (glyph >= start
17204 && BUFFERP (glyph->object)
17205 && (glyph->type == STRETCH_GLYPH
17206 || (glyph->type == CHAR_GLYPH
17207 && glyph->u.ch == ' ')))
17208 (glyph--)->face_id = face_id;
17209 }
17210 else
17211 {
17212 while (glyph <= start
17213 && BUFFERP (glyph->object)
17214 && (glyph->type == STRETCH_GLYPH
17215 || (glyph->type == CHAR_GLYPH
17216 && glyph->u.ch == ' ')))
17217 (glyph++)->face_id = face_id;
17218 }
17219 }
17220 }
17221 }
17222
17223
17224 /* Value is non-zero if glyph row ROW should be
17225 used to hold the cursor. */
17226
17227 static int
17228 cursor_row_p (struct glyph_row *row)
17229 {
17230 int result = 1;
17231
17232 if (PT == CHARPOS (row->end.pos))
17233 {
17234 /* Suppose the row ends on a string.
17235 Unless the row is continued, that means it ends on a newline
17236 in the string. If it's anything other than a display string
17237 (e.g. a before-string from an overlay), we don't want the
17238 cursor there. (This heuristic seems to give the optimal
17239 behavior for the various types of multi-line strings.) */
17240 if (CHARPOS (row->end.string_pos) >= 0)
17241 {
17242 if (row->continued_p)
17243 result = 1;
17244 else
17245 {
17246 /* Check for `display' property. */
17247 struct glyph *beg = row->glyphs[TEXT_AREA];
17248 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17249 struct glyph *glyph;
17250
17251 result = 0;
17252 for (glyph = end; glyph >= beg; --glyph)
17253 if (STRINGP (glyph->object))
17254 {
17255 Lisp_Object prop
17256 = Fget_char_property (make_number (PT),
17257 Qdisplay, Qnil);
17258 result =
17259 (!NILP (prop)
17260 && display_prop_string_p (prop, glyph->object));
17261 break;
17262 }
17263 }
17264 }
17265 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17266 {
17267 /* If the row ends in middle of a real character,
17268 and the line is continued, we want the cursor here.
17269 That's because CHARPOS (ROW->end.pos) would equal
17270 PT if PT is before the character. */
17271 if (!row->ends_in_ellipsis_p)
17272 result = row->continued_p;
17273 else
17274 /* If the row ends in an ellipsis, then
17275 CHARPOS (ROW->end.pos) will equal point after the
17276 invisible text. We want that position to be displayed
17277 after the ellipsis. */
17278 result = 0;
17279 }
17280 /* If the row ends at ZV, display the cursor at the end of that
17281 row instead of at the start of the row below. */
17282 else if (row->ends_at_zv_p)
17283 result = 1;
17284 else
17285 result = 0;
17286 }
17287
17288 return result;
17289 }
17290
17291 \f
17292
17293 /* Push the display property PROP so that it will be rendered at the
17294 current position in IT. Return 1 if PROP was successfully pushed,
17295 0 otherwise. */
17296
17297 static int
17298 push_display_prop (struct it *it, Lisp_Object prop)
17299 {
17300 push_it (it, NULL);
17301
17302 if (STRINGP (prop))
17303 {
17304 if (SCHARS (prop) == 0)
17305 {
17306 pop_it (it);
17307 return 0;
17308 }
17309
17310 it->string = prop;
17311 it->multibyte_p = STRING_MULTIBYTE (it->string);
17312 it->current.overlay_string_index = -1;
17313 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17314 it->end_charpos = it->string_nchars = SCHARS (it->string);
17315 it->method = GET_FROM_STRING;
17316 it->stop_charpos = 0;
17317 }
17318 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17319 {
17320 it->method = GET_FROM_STRETCH;
17321 it->object = prop;
17322 }
17323 #ifdef HAVE_WINDOW_SYSTEM
17324 else if (IMAGEP (prop))
17325 {
17326 it->what = IT_IMAGE;
17327 it->image_id = lookup_image (it->f, prop);
17328 it->method = GET_FROM_IMAGE;
17329 }
17330 #endif /* HAVE_WINDOW_SYSTEM */
17331 else
17332 {
17333 pop_it (it); /* bogus display property, give up */
17334 return 0;
17335 }
17336
17337 return 1;
17338 }
17339
17340 /* Return the character-property PROP at the current position in IT. */
17341
17342 static Lisp_Object
17343 get_it_property (struct it *it, Lisp_Object prop)
17344 {
17345 Lisp_Object position;
17346
17347 if (STRINGP (it->object))
17348 position = make_number (IT_STRING_CHARPOS (*it));
17349 else if (BUFFERP (it->object))
17350 position = make_number (IT_CHARPOS (*it));
17351 else
17352 return Qnil;
17353
17354 return Fget_char_property (position, prop, it->object);
17355 }
17356
17357 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17358
17359 static void
17360 handle_line_prefix (struct it *it)
17361 {
17362 Lisp_Object prefix;
17363 if (it->continuation_lines_width > 0)
17364 {
17365 prefix = get_it_property (it, Qwrap_prefix);
17366 if (NILP (prefix))
17367 prefix = Vwrap_prefix;
17368 }
17369 else
17370 {
17371 prefix = get_it_property (it, Qline_prefix);
17372 if (NILP (prefix))
17373 prefix = Vline_prefix;
17374 }
17375 if (! NILP (prefix) && push_display_prop (it, prefix))
17376 {
17377 /* If the prefix is wider than the window, and we try to wrap
17378 it, it would acquire its own wrap prefix, and so on till the
17379 iterator stack overflows. So, don't wrap the prefix. */
17380 it->line_wrap = TRUNCATE;
17381 it->avoid_cursor_p = 1;
17382 }
17383 }
17384
17385 \f
17386
17387 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17388 only for R2L lines from display_line, when it decides that too many
17389 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17390 continued. */
17391 static void
17392 unproduce_glyphs (struct it *it, int n)
17393 {
17394 struct glyph *glyph, *end;
17395
17396 xassert (it->glyph_row);
17397 xassert (it->glyph_row->reversed_p);
17398 xassert (it->area == TEXT_AREA);
17399 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17400
17401 if (n > it->glyph_row->used[TEXT_AREA])
17402 n = it->glyph_row->used[TEXT_AREA];
17403 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17404 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17405 for ( ; glyph < end; glyph++)
17406 glyph[-n] = *glyph;
17407 }
17408
17409 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17410 and ROW->maxpos. */
17411 static void
17412 find_row_edges (struct it *it, struct glyph_row *row,
17413 EMACS_INT min_pos, EMACS_INT min_bpos,
17414 EMACS_INT max_pos, EMACS_INT max_bpos)
17415 {
17416 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17417 lines' rows is implemented for bidi-reordered rows. */
17418
17419 /* ROW->minpos is the value of min_pos, the minimal buffer position
17420 we have in ROW. */
17421 if (min_pos <= ZV)
17422 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17423 else
17424 /* We didn't find _any_ valid buffer positions in any of the
17425 glyphs, so we must trust the iterator's computed positions. */
17426 row->minpos = row->start.pos;
17427 if (max_pos <= 0)
17428 {
17429 max_pos = CHARPOS (it->current.pos);
17430 max_bpos = BYTEPOS (it->current.pos);
17431 }
17432
17433 /* Here are the various use-cases for ending the row, and the
17434 corresponding values for ROW->maxpos:
17435
17436 Line ends in a newline from buffer eol_pos + 1
17437 Line is continued from buffer max_pos + 1
17438 Line is truncated on right it->current.pos
17439 Line ends in a newline from string max_pos
17440 Line is continued from string max_pos
17441 Line is continued from display vector max_pos
17442 Line is entirely from a string min_pos == max_pos
17443 Line is entirely from a display vector min_pos == max_pos
17444 Line that ends at ZV ZV
17445
17446 If you discover other use-cases, please add them here as
17447 appropriate. */
17448 if (row->ends_at_zv_p)
17449 row->maxpos = it->current.pos;
17450 else if (row->used[TEXT_AREA])
17451 {
17452 if (row->ends_in_newline_from_string_p)
17453 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17454 else if (CHARPOS (it->eol_pos) > 0)
17455 SET_TEXT_POS (row->maxpos,
17456 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17457 else if (row->continued_p)
17458 {
17459 /* If max_pos is different from IT's current position, it
17460 means IT->method does not belong to the display element
17461 at max_pos. However, it also means that the display
17462 element at max_pos was displayed in its entirety on this
17463 line, which is equivalent to saying that the next line
17464 starts at the next buffer position. */
17465 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17466 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17467 else
17468 {
17469 INC_BOTH (max_pos, max_bpos);
17470 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17471 }
17472 }
17473 else if (row->truncated_on_right_p)
17474 /* display_line already called reseat_at_next_visible_line_start,
17475 which puts the iterator at the beginning of the next line, in
17476 the logical order. */
17477 row->maxpos = it->current.pos;
17478 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17479 /* A line that is entirely from a string/image/stretch... */
17480 row->maxpos = row->minpos;
17481 else
17482 abort ();
17483 }
17484 else
17485 row->maxpos = it->current.pos;
17486 }
17487
17488 /* Construct the glyph row IT->glyph_row in the desired matrix of
17489 IT->w from text at the current position of IT. See dispextern.h
17490 for an overview of struct it. Value is non-zero if
17491 IT->glyph_row displays text, as opposed to a line displaying ZV
17492 only. */
17493
17494 static int
17495 display_line (struct it *it)
17496 {
17497 struct glyph_row *row = it->glyph_row;
17498 Lisp_Object overlay_arrow_string;
17499 struct it wrap_it;
17500 int may_wrap = 0, wrap_x IF_LINT (= 0);
17501 int wrap_row_used = -1;
17502 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
17503 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
17504 int wrap_row_extra_line_spacing IF_LINT (= 0);
17505 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
17506 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
17507 int cvpos;
17508 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17509 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
17510
17511 /* We always start displaying at hpos zero even if hscrolled. */
17512 xassert (it->hpos == 0 && it->current_x == 0);
17513
17514 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17515 >= it->w->desired_matrix->nrows)
17516 {
17517 it->w->nrows_scale_factor++;
17518 fonts_changed_p = 1;
17519 return 0;
17520 }
17521
17522 /* Is IT->w showing the region? */
17523 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17524
17525 /* Clear the result glyph row and enable it. */
17526 prepare_desired_row (row);
17527
17528 row->y = it->current_y;
17529 row->start = it->start;
17530 row->continuation_lines_width = it->continuation_lines_width;
17531 row->displays_text_p = 1;
17532 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17533 it->starts_in_middle_of_char_p = 0;
17534
17535 /* Arrange the overlays nicely for our purposes. Usually, we call
17536 display_line on only one line at a time, in which case this
17537 can't really hurt too much, or we call it on lines which appear
17538 one after another in the buffer, in which case all calls to
17539 recenter_overlay_lists but the first will be pretty cheap. */
17540 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17541
17542 /* Move over display elements that are not visible because we are
17543 hscrolled. This may stop at an x-position < IT->first_visible_x
17544 if the first glyph is partially visible or if we hit a line end. */
17545 if (it->current_x < it->first_visible_x)
17546 {
17547 this_line_min_pos = row->start.pos;
17548 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17549 MOVE_TO_POS | MOVE_TO_X);
17550 /* Record the smallest positions seen while we moved over
17551 display elements that are not visible. This is needed by
17552 redisplay_internal for optimizing the case where the cursor
17553 stays inside the same line. The rest of this function only
17554 considers positions that are actually displayed, so
17555 RECORD_MAX_MIN_POS will not otherwise record positions that
17556 are hscrolled to the left of the left edge of the window. */
17557 min_pos = CHARPOS (this_line_min_pos);
17558 min_bpos = BYTEPOS (this_line_min_pos);
17559 }
17560 else
17561 {
17562 /* We only do this when not calling `move_it_in_display_line_to'
17563 above, because move_it_in_display_line_to calls
17564 handle_line_prefix itself. */
17565 handle_line_prefix (it);
17566 }
17567
17568 /* Get the initial row height. This is either the height of the
17569 text hscrolled, if there is any, or zero. */
17570 row->ascent = it->max_ascent;
17571 row->height = it->max_ascent + it->max_descent;
17572 row->phys_ascent = it->max_phys_ascent;
17573 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17574 row->extra_line_spacing = it->max_extra_line_spacing;
17575
17576 /* Utility macro to record max and min buffer positions seen until now. */
17577 #define RECORD_MAX_MIN_POS(IT) \
17578 do \
17579 { \
17580 if (IT_CHARPOS (*(IT)) < min_pos) \
17581 { \
17582 min_pos = IT_CHARPOS (*(IT)); \
17583 min_bpos = IT_BYTEPOS (*(IT)); \
17584 } \
17585 if (IT_CHARPOS (*(IT)) > max_pos) \
17586 { \
17587 max_pos = IT_CHARPOS (*(IT)); \
17588 max_bpos = IT_BYTEPOS (*(IT)); \
17589 } \
17590 } \
17591 while (0)
17592
17593 /* Loop generating characters. The loop is left with IT on the next
17594 character to display. */
17595 while (1)
17596 {
17597 int n_glyphs_before, hpos_before, x_before;
17598 int x, nglyphs;
17599 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17600
17601 /* Retrieve the next thing to display. Value is zero if end of
17602 buffer reached. */
17603 if (!get_next_display_element (it))
17604 {
17605 /* Maybe add a space at the end of this line that is used to
17606 display the cursor there under X. Set the charpos of the
17607 first glyph of blank lines not corresponding to any text
17608 to -1. */
17609 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17610 row->exact_window_width_line_p = 1;
17611 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17612 || row->used[TEXT_AREA] == 0)
17613 {
17614 row->glyphs[TEXT_AREA]->charpos = -1;
17615 row->displays_text_p = 0;
17616
17617 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
17618 && (!MINI_WINDOW_P (it->w)
17619 || (minibuf_level && EQ (it->window, minibuf_window))))
17620 row->indicate_empty_line_p = 1;
17621 }
17622
17623 it->continuation_lines_width = 0;
17624 row->ends_at_zv_p = 1;
17625 /* A row that displays right-to-left text must always have
17626 its last face extended all the way to the end of line,
17627 even if this row ends in ZV, because we still write to
17628 the screen left to right. */
17629 if (row->reversed_p)
17630 extend_face_to_end_of_line (it);
17631 break;
17632 }
17633
17634 /* Now, get the metrics of what we want to display. This also
17635 generates glyphs in `row' (which is IT->glyph_row). */
17636 n_glyphs_before = row->used[TEXT_AREA];
17637 x = it->current_x;
17638
17639 /* Remember the line height so far in case the next element doesn't
17640 fit on the line. */
17641 if (it->line_wrap != TRUNCATE)
17642 {
17643 ascent = it->max_ascent;
17644 descent = it->max_descent;
17645 phys_ascent = it->max_phys_ascent;
17646 phys_descent = it->max_phys_descent;
17647
17648 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17649 {
17650 if (IT_DISPLAYING_WHITESPACE (it))
17651 may_wrap = 1;
17652 else if (may_wrap)
17653 {
17654 wrap_it = *it;
17655 wrap_x = x;
17656 wrap_row_used = row->used[TEXT_AREA];
17657 wrap_row_ascent = row->ascent;
17658 wrap_row_height = row->height;
17659 wrap_row_phys_ascent = row->phys_ascent;
17660 wrap_row_phys_height = row->phys_height;
17661 wrap_row_extra_line_spacing = row->extra_line_spacing;
17662 wrap_row_min_pos = min_pos;
17663 wrap_row_min_bpos = min_bpos;
17664 wrap_row_max_pos = max_pos;
17665 wrap_row_max_bpos = max_bpos;
17666 may_wrap = 0;
17667 }
17668 }
17669 }
17670
17671 PRODUCE_GLYPHS (it);
17672
17673 /* If this display element was in marginal areas, continue with
17674 the next one. */
17675 if (it->area != TEXT_AREA)
17676 {
17677 row->ascent = max (row->ascent, it->max_ascent);
17678 row->height = max (row->height, it->max_ascent + it->max_descent);
17679 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17680 row->phys_height = max (row->phys_height,
17681 it->max_phys_ascent + it->max_phys_descent);
17682 row->extra_line_spacing = max (row->extra_line_spacing,
17683 it->max_extra_line_spacing);
17684 set_iterator_to_next (it, 1);
17685 continue;
17686 }
17687
17688 /* Does the display element fit on the line? If we truncate
17689 lines, we should draw past the right edge of the window. If
17690 we don't truncate, we want to stop so that we can display the
17691 continuation glyph before the right margin. If lines are
17692 continued, there are two possible strategies for characters
17693 resulting in more than 1 glyph (e.g. tabs): Display as many
17694 glyphs as possible in this line and leave the rest for the
17695 continuation line, or display the whole element in the next
17696 line. Original redisplay did the former, so we do it also. */
17697 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17698 hpos_before = it->hpos;
17699 x_before = x;
17700
17701 if (/* Not a newline. */
17702 nglyphs > 0
17703 /* Glyphs produced fit entirely in the line. */
17704 && it->current_x < it->last_visible_x)
17705 {
17706 it->hpos += nglyphs;
17707 row->ascent = max (row->ascent, it->max_ascent);
17708 row->height = max (row->height, it->max_ascent + it->max_descent);
17709 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17710 row->phys_height = max (row->phys_height,
17711 it->max_phys_ascent + it->max_phys_descent);
17712 row->extra_line_spacing = max (row->extra_line_spacing,
17713 it->max_extra_line_spacing);
17714 if (it->current_x - it->pixel_width < it->first_visible_x)
17715 row->x = x - it->first_visible_x;
17716 /* Record the maximum and minimum buffer positions seen so
17717 far in glyphs that will be displayed by this row. */
17718 if (it->bidi_p)
17719 RECORD_MAX_MIN_POS (it);
17720 }
17721 else
17722 {
17723 int i, new_x;
17724 struct glyph *glyph;
17725
17726 for (i = 0; i < nglyphs; ++i, x = new_x)
17727 {
17728 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17729 new_x = x + glyph->pixel_width;
17730
17731 if (/* Lines are continued. */
17732 it->line_wrap != TRUNCATE
17733 && (/* Glyph doesn't fit on the line. */
17734 new_x > it->last_visible_x
17735 /* Or it fits exactly on a window system frame. */
17736 || (new_x == it->last_visible_x
17737 && FRAME_WINDOW_P (it->f))))
17738 {
17739 /* End of a continued line. */
17740
17741 if (it->hpos == 0
17742 || (new_x == it->last_visible_x
17743 && FRAME_WINDOW_P (it->f)))
17744 {
17745 /* Current glyph is the only one on the line or
17746 fits exactly on the line. We must continue
17747 the line because we can't draw the cursor
17748 after the glyph. */
17749 row->continued_p = 1;
17750 it->current_x = new_x;
17751 it->continuation_lines_width += new_x;
17752 ++it->hpos;
17753 /* Record the maximum and minimum buffer
17754 positions seen so far in glyphs that will be
17755 displayed by this row. */
17756 if (it->bidi_p)
17757 RECORD_MAX_MIN_POS (it);
17758 if (i == nglyphs - 1)
17759 {
17760 /* If line-wrap is on, check if a previous
17761 wrap point was found. */
17762 if (wrap_row_used > 0
17763 /* Even if there is a previous wrap
17764 point, continue the line here as
17765 usual, if (i) the previous character
17766 was a space or tab AND (ii) the
17767 current character is not. */
17768 && (!may_wrap
17769 || IT_DISPLAYING_WHITESPACE (it)))
17770 goto back_to_wrap;
17771
17772 set_iterator_to_next (it, 1);
17773 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17774 {
17775 if (!get_next_display_element (it))
17776 {
17777 row->exact_window_width_line_p = 1;
17778 it->continuation_lines_width = 0;
17779 row->continued_p = 0;
17780 row->ends_at_zv_p = 1;
17781 }
17782 else if (ITERATOR_AT_END_OF_LINE_P (it))
17783 {
17784 row->continued_p = 0;
17785 row->exact_window_width_line_p = 1;
17786 }
17787 }
17788 }
17789 }
17790 else if (CHAR_GLYPH_PADDING_P (*glyph)
17791 && !FRAME_WINDOW_P (it->f))
17792 {
17793 /* A padding glyph that doesn't fit on this line.
17794 This means the whole character doesn't fit
17795 on the line. */
17796 if (row->reversed_p)
17797 unproduce_glyphs (it, row->used[TEXT_AREA]
17798 - n_glyphs_before);
17799 row->used[TEXT_AREA] = n_glyphs_before;
17800
17801 /* Fill the rest of the row with continuation
17802 glyphs like in 20.x. */
17803 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17804 < row->glyphs[1 + TEXT_AREA])
17805 produce_special_glyphs (it, IT_CONTINUATION);
17806
17807 row->continued_p = 1;
17808 it->current_x = x_before;
17809 it->continuation_lines_width += x_before;
17810
17811 /* Restore the height to what it was before the
17812 element not fitting on the line. */
17813 it->max_ascent = ascent;
17814 it->max_descent = descent;
17815 it->max_phys_ascent = phys_ascent;
17816 it->max_phys_descent = phys_descent;
17817 }
17818 else if (wrap_row_used > 0)
17819 {
17820 back_to_wrap:
17821 if (row->reversed_p)
17822 unproduce_glyphs (it,
17823 row->used[TEXT_AREA] - wrap_row_used);
17824 *it = wrap_it;
17825 it->continuation_lines_width += wrap_x;
17826 row->used[TEXT_AREA] = wrap_row_used;
17827 row->ascent = wrap_row_ascent;
17828 row->height = wrap_row_height;
17829 row->phys_ascent = wrap_row_phys_ascent;
17830 row->phys_height = wrap_row_phys_height;
17831 row->extra_line_spacing = wrap_row_extra_line_spacing;
17832 min_pos = wrap_row_min_pos;
17833 min_bpos = wrap_row_min_bpos;
17834 max_pos = wrap_row_max_pos;
17835 max_bpos = wrap_row_max_bpos;
17836 row->continued_p = 1;
17837 row->ends_at_zv_p = 0;
17838 row->exact_window_width_line_p = 0;
17839 it->continuation_lines_width += x;
17840
17841 /* Make sure that a non-default face is extended
17842 up to the right margin of the window. */
17843 extend_face_to_end_of_line (it);
17844 }
17845 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17846 {
17847 /* A TAB that extends past the right edge of the
17848 window. This produces a single glyph on
17849 window system frames. We leave the glyph in
17850 this row and let it fill the row, but don't
17851 consume the TAB. */
17852 it->continuation_lines_width += it->last_visible_x;
17853 row->ends_in_middle_of_char_p = 1;
17854 row->continued_p = 1;
17855 glyph->pixel_width = it->last_visible_x - x;
17856 it->starts_in_middle_of_char_p = 1;
17857 }
17858 else
17859 {
17860 /* Something other than a TAB that draws past
17861 the right edge of the window. Restore
17862 positions to values before the element. */
17863 if (row->reversed_p)
17864 unproduce_glyphs (it, row->used[TEXT_AREA]
17865 - (n_glyphs_before + i));
17866 row->used[TEXT_AREA] = n_glyphs_before + i;
17867
17868 /* Display continuation glyphs. */
17869 if (!FRAME_WINDOW_P (it->f))
17870 produce_special_glyphs (it, IT_CONTINUATION);
17871 row->continued_p = 1;
17872
17873 it->current_x = x_before;
17874 it->continuation_lines_width += x;
17875 extend_face_to_end_of_line (it);
17876
17877 if (nglyphs > 1 && i > 0)
17878 {
17879 row->ends_in_middle_of_char_p = 1;
17880 it->starts_in_middle_of_char_p = 1;
17881 }
17882
17883 /* Restore the height to what it was before the
17884 element not fitting on the line. */
17885 it->max_ascent = ascent;
17886 it->max_descent = descent;
17887 it->max_phys_ascent = phys_ascent;
17888 it->max_phys_descent = phys_descent;
17889 }
17890
17891 break;
17892 }
17893 else if (new_x > it->first_visible_x)
17894 {
17895 /* Increment number of glyphs actually displayed. */
17896 ++it->hpos;
17897
17898 /* Record the maximum and minimum buffer positions
17899 seen so far in glyphs that will be displayed by
17900 this row. */
17901 if (it->bidi_p)
17902 RECORD_MAX_MIN_POS (it);
17903
17904 if (x < it->first_visible_x)
17905 /* Glyph is partially visible, i.e. row starts at
17906 negative X position. */
17907 row->x = x - it->first_visible_x;
17908 }
17909 else
17910 {
17911 /* Glyph is completely off the left margin of the
17912 window. This should not happen because of the
17913 move_it_in_display_line at the start of this
17914 function, unless the text display area of the
17915 window is empty. */
17916 xassert (it->first_visible_x <= it->last_visible_x);
17917 }
17918 }
17919
17920 row->ascent = max (row->ascent, it->max_ascent);
17921 row->height = max (row->height, it->max_ascent + it->max_descent);
17922 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17923 row->phys_height = max (row->phys_height,
17924 it->max_phys_ascent + it->max_phys_descent);
17925 row->extra_line_spacing = max (row->extra_line_spacing,
17926 it->max_extra_line_spacing);
17927
17928 /* End of this display line if row is continued. */
17929 if (row->continued_p || row->ends_at_zv_p)
17930 break;
17931 }
17932
17933 at_end_of_line:
17934 /* Is this a line end? If yes, we're also done, after making
17935 sure that a non-default face is extended up to the right
17936 margin of the window. */
17937 if (ITERATOR_AT_END_OF_LINE_P (it))
17938 {
17939 int used_before = row->used[TEXT_AREA];
17940
17941 row->ends_in_newline_from_string_p = STRINGP (it->object);
17942
17943 /* Add a space at the end of the line that is used to
17944 display the cursor there. */
17945 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17946 append_space_for_newline (it, 0);
17947
17948 /* Extend the face to the end of the line. */
17949 extend_face_to_end_of_line (it);
17950
17951 /* Make sure we have the position. */
17952 if (used_before == 0)
17953 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17954
17955 /* Record the position of the newline, for use in
17956 find_row_edges. */
17957 it->eol_pos = it->current.pos;
17958
17959 /* Consume the line end. This skips over invisible lines. */
17960 set_iterator_to_next (it, 1);
17961 it->continuation_lines_width = 0;
17962 break;
17963 }
17964
17965 /* Proceed with next display element. Note that this skips
17966 over lines invisible because of selective display. */
17967 set_iterator_to_next (it, 1);
17968
17969 /* If we truncate lines, we are done when the last displayed
17970 glyphs reach past the right margin of the window. */
17971 if (it->line_wrap == TRUNCATE
17972 && (FRAME_WINDOW_P (it->f)
17973 ? (it->current_x >= it->last_visible_x)
17974 : (it->current_x > it->last_visible_x)))
17975 {
17976 /* Maybe add truncation glyphs. */
17977 if (!FRAME_WINDOW_P (it->f))
17978 {
17979 int i, n;
17980
17981 if (!row->reversed_p)
17982 {
17983 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17984 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17985 break;
17986 }
17987 else
17988 {
17989 for (i = 0; i < row->used[TEXT_AREA]; i++)
17990 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17991 break;
17992 /* Remove any padding glyphs at the front of ROW, to
17993 make room for the truncation glyphs we will be
17994 adding below. The loop below always inserts at
17995 least one truncation glyph, so also remove the
17996 last glyph added to ROW. */
17997 unproduce_glyphs (it, i + 1);
17998 /* Adjust i for the loop below. */
17999 i = row->used[TEXT_AREA] - (i + 1);
18000 }
18001
18002 for (n = row->used[TEXT_AREA]; i < n; ++i)
18003 {
18004 row->used[TEXT_AREA] = i;
18005 produce_special_glyphs (it, IT_TRUNCATION);
18006 }
18007 }
18008 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18009 {
18010 /* Don't truncate if we can overflow newline into fringe. */
18011 if (!get_next_display_element (it))
18012 {
18013 it->continuation_lines_width = 0;
18014 row->ends_at_zv_p = 1;
18015 row->exact_window_width_line_p = 1;
18016 break;
18017 }
18018 if (ITERATOR_AT_END_OF_LINE_P (it))
18019 {
18020 row->exact_window_width_line_p = 1;
18021 goto at_end_of_line;
18022 }
18023 }
18024
18025 row->truncated_on_right_p = 1;
18026 it->continuation_lines_width = 0;
18027 reseat_at_next_visible_line_start (it, 0);
18028 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18029 it->hpos = hpos_before;
18030 it->current_x = x_before;
18031 break;
18032 }
18033 }
18034
18035 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18036 at the left window margin. */
18037 if (it->first_visible_x
18038 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18039 {
18040 if (!FRAME_WINDOW_P (it->f))
18041 insert_left_trunc_glyphs (it);
18042 row->truncated_on_left_p = 1;
18043 }
18044
18045 /* Remember the position at which this line ends.
18046
18047 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18048 cannot be before the call to find_row_edges below, since that is
18049 where these positions are determined. */
18050 row->end = it->current;
18051 if (!it->bidi_p)
18052 {
18053 row->minpos = row->start.pos;
18054 row->maxpos = row->end.pos;
18055 }
18056 else
18057 {
18058 /* ROW->minpos and ROW->maxpos must be the smallest and
18059 `1 + the largest' buffer positions in ROW. But if ROW was
18060 bidi-reordered, these two positions can be anywhere in the
18061 row, so we must determine them now. */
18062 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18063 }
18064
18065 /* If the start of this line is the overlay arrow-position, then
18066 mark this glyph row as the one containing the overlay arrow.
18067 This is clearly a mess with variable size fonts. It would be
18068 better to let it be displayed like cursors under X. */
18069 if ((row->displays_text_p || !overlay_arrow_seen)
18070 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18071 !NILP (overlay_arrow_string)))
18072 {
18073 /* Overlay arrow in window redisplay is a fringe bitmap. */
18074 if (STRINGP (overlay_arrow_string))
18075 {
18076 struct glyph_row *arrow_row
18077 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18078 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18079 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18080 struct glyph *p = row->glyphs[TEXT_AREA];
18081 struct glyph *p2, *end;
18082
18083 /* Copy the arrow glyphs. */
18084 while (glyph < arrow_end)
18085 *p++ = *glyph++;
18086
18087 /* Throw away padding glyphs. */
18088 p2 = p;
18089 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18090 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18091 ++p2;
18092 if (p2 > p)
18093 {
18094 while (p2 < end)
18095 *p++ = *p2++;
18096 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18097 }
18098 }
18099 else
18100 {
18101 xassert (INTEGERP (overlay_arrow_string));
18102 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18103 }
18104 overlay_arrow_seen = 1;
18105 }
18106
18107 /* Compute pixel dimensions of this line. */
18108 compute_line_metrics (it);
18109
18110 /* Record whether this row ends inside an ellipsis. */
18111 row->ends_in_ellipsis_p
18112 = (it->method == GET_FROM_DISPLAY_VECTOR
18113 && it->ellipsis_p);
18114
18115 /* Save fringe bitmaps in this row. */
18116 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18117 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18118 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18119 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18120
18121 it->left_user_fringe_bitmap = 0;
18122 it->left_user_fringe_face_id = 0;
18123 it->right_user_fringe_bitmap = 0;
18124 it->right_user_fringe_face_id = 0;
18125
18126 /* Maybe set the cursor. */
18127 cvpos = it->w->cursor.vpos;
18128 if ((cvpos < 0
18129 /* In bidi-reordered rows, keep checking for proper cursor
18130 position even if one has been found already, because buffer
18131 positions in such rows change non-linearly with ROW->VPOS,
18132 when a line is continued. One exception: when we are at ZV,
18133 display cursor on the first suitable glyph row, since all
18134 the empty rows after that also have their position set to ZV. */
18135 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18136 lines' rows is implemented for bidi-reordered rows. */
18137 || (it->bidi_p
18138 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18139 && PT >= MATRIX_ROW_START_CHARPOS (row)
18140 && PT <= MATRIX_ROW_END_CHARPOS (row)
18141 && cursor_row_p (row))
18142 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18143
18144 /* Highlight trailing whitespace. */
18145 if (!NILP (Vshow_trailing_whitespace))
18146 highlight_trailing_whitespace (it->f, it->glyph_row);
18147
18148 /* Prepare for the next line. This line starts horizontally at (X
18149 HPOS) = (0 0). Vertical positions are incremented. As a
18150 convenience for the caller, IT->glyph_row is set to the next
18151 row to be used. */
18152 it->current_x = it->hpos = 0;
18153 it->current_y += row->height;
18154 SET_TEXT_POS (it->eol_pos, 0, 0);
18155 ++it->vpos;
18156 ++it->glyph_row;
18157 /* The next row should by default use the same value of the
18158 reversed_p flag as this one. set_iterator_to_next decides when
18159 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18160 the flag accordingly. */
18161 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18162 it->glyph_row->reversed_p = row->reversed_p;
18163 it->start = row->end;
18164 return row->displays_text_p;
18165
18166 #undef RECORD_MAX_MIN_POS
18167 }
18168
18169 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18170 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18171 doc: /* Return paragraph direction at point in BUFFER.
18172 Value is either `left-to-right' or `right-to-left'.
18173 If BUFFER is omitted or nil, it defaults to the current buffer.
18174
18175 Paragraph direction determines how the text in the paragraph is displayed.
18176 In left-to-right paragraphs, text begins at the left margin of the window
18177 and the reading direction is generally left to right. In right-to-left
18178 paragraphs, text begins at the right margin and is read from right to left.
18179
18180 See also `bidi-paragraph-direction'. */)
18181 (Lisp_Object buffer)
18182 {
18183 struct buffer *buf = current_buffer;
18184 struct buffer *old = buf;
18185
18186 if (! NILP (buffer))
18187 {
18188 CHECK_BUFFER (buffer);
18189 buf = XBUFFER (buffer);
18190 }
18191
18192 if (NILP (BVAR (buf, bidi_display_reordering)))
18193 return Qleft_to_right;
18194 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18195 return BVAR (buf, bidi_paragraph_direction);
18196 else
18197 {
18198 /* Determine the direction from buffer text. We could try to
18199 use current_matrix if it is up to date, but this seems fast
18200 enough as it is. */
18201 struct bidi_it itb;
18202 EMACS_INT pos = BUF_PT (buf);
18203 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18204 int c;
18205
18206 set_buffer_temp (buf);
18207 /* bidi_paragraph_init finds the base direction of the paragraph
18208 by searching forward from paragraph start. We need the base
18209 direction of the current or _previous_ paragraph, so we need
18210 to make sure we are within that paragraph. To that end, find
18211 the previous non-empty line. */
18212 if (pos >= ZV && pos > BEGV)
18213 {
18214 pos--;
18215 bytepos = CHAR_TO_BYTE (pos);
18216 }
18217 while ((c = FETCH_BYTE (bytepos)) == '\n'
18218 || c == ' ' || c == '\t' || c == '\f')
18219 {
18220 if (bytepos <= BEGV_BYTE)
18221 break;
18222 bytepos--;
18223 pos--;
18224 }
18225 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18226 bytepos--;
18227 itb.charpos = pos;
18228 itb.bytepos = bytepos;
18229 itb.nchars = -1;
18230 itb.string.s = NULL;
18231 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18232 itb.first_elt = 1;
18233 itb.separator_limit = -1;
18234 itb.paragraph_dir = NEUTRAL_DIR;
18235
18236 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18237 set_buffer_temp (old);
18238 switch (itb.paragraph_dir)
18239 {
18240 case L2R:
18241 return Qleft_to_right;
18242 break;
18243 case R2L:
18244 return Qright_to_left;
18245 break;
18246 default:
18247 abort ();
18248 }
18249 }
18250 }
18251
18252
18253 \f
18254 /***********************************************************************
18255 Menu Bar
18256 ***********************************************************************/
18257
18258 /* Redisplay the menu bar in the frame for window W.
18259
18260 The menu bar of X frames that don't have X toolkit support is
18261 displayed in a special window W->frame->menu_bar_window.
18262
18263 The menu bar of terminal frames is treated specially as far as
18264 glyph matrices are concerned. Menu bar lines are not part of
18265 windows, so the update is done directly on the frame matrix rows
18266 for the menu bar. */
18267
18268 static void
18269 display_menu_bar (struct window *w)
18270 {
18271 struct frame *f = XFRAME (WINDOW_FRAME (w));
18272 struct it it;
18273 Lisp_Object items;
18274 int i;
18275
18276 /* Don't do all this for graphical frames. */
18277 #ifdef HAVE_NTGUI
18278 if (FRAME_W32_P (f))
18279 return;
18280 #endif
18281 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18282 if (FRAME_X_P (f))
18283 return;
18284 #endif
18285
18286 #ifdef HAVE_NS
18287 if (FRAME_NS_P (f))
18288 return;
18289 #endif /* HAVE_NS */
18290
18291 #ifdef USE_X_TOOLKIT
18292 xassert (!FRAME_WINDOW_P (f));
18293 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18294 it.first_visible_x = 0;
18295 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18296 #else /* not USE_X_TOOLKIT */
18297 if (FRAME_WINDOW_P (f))
18298 {
18299 /* Menu bar lines are displayed in the desired matrix of the
18300 dummy window menu_bar_window. */
18301 struct window *menu_w;
18302 xassert (WINDOWP (f->menu_bar_window));
18303 menu_w = XWINDOW (f->menu_bar_window);
18304 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18305 MENU_FACE_ID);
18306 it.first_visible_x = 0;
18307 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18308 }
18309 else
18310 {
18311 /* This is a TTY frame, i.e. character hpos/vpos are used as
18312 pixel x/y. */
18313 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18314 MENU_FACE_ID);
18315 it.first_visible_x = 0;
18316 it.last_visible_x = FRAME_COLS (f);
18317 }
18318 #endif /* not USE_X_TOOLKIT */
18319
18320 if (! mode_line_inverse_video)
18321 /* Force the menu-bar to be displayed in the default face. */
18322 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18323
18324 /* Clear all rows of the menu bar. */
18325 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18326 {
18327 struct glyph_row *row = it.glyph_row + i;
18328 clear_glyph_row (row);
18329 row->enabled_p = 1;
18330 row->full_width_p = 1;
18331 }
18332
18333 /* Display all items of the menu bar. */
18334 items = FRAME_MENU_BAR_ITEMS (it.f);
18335 for (i = 0; i < ASIZE (items); i += 4)
18336 {
18337 Lisp_Object string;
18338
18339 /* Stop at nil string. */
18340 string = AREF (items, i + 1);
18341 if (NILP (string))
18342 break;
18343
18344 /* Remember where item was displayed. */
18345 ASET (items, i + 3, make_number (it.hpos));
18346
18347 /* Display the item, pad with one space. */
18348 if (it.current_x < it.last_visible_x)
18349 display_string (NULL, string, Qnil, 0, 0, &it,
18350 SCHARS (string) + 1, 0, 0, -1);
18351 }
18352
18353 /* Fill out the line with spaces. */
18354 if (it.current_x < it.last_visible_x)
18355 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18356
18357 /* Compute the total height of the lines. */
18358 compute_line_metrics (&it);
18359 }
18360
18361
18362 \f
18363 /***********************************************************************
18364 Mode Line
18365 ***********************************************************************/
18366
18367 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18368 FORCE is non-zero, redisplay mode lines unconditionally.
18369 Otherwise, redisplay only mode lines that are garbaged. Value is
18370 the number of windows whose mode lines were redisplayed. */
18371
18372 static int
18373 redisplay_mode_lines (Lisp_Object window, int force)
18374 {
18375 int nwindows = 0;
18376
18377 while (!NILP (window))
18378 {
18379 struct window *w = XWINDOW (window);
18380
18381 if (WINDOWP (w->hchild))
18382 nwindows += redisplay_mode_lines (w->hchild, force);
18383 else if (WINDOWP (w->vchild))
18384 nwindows += redisplay_mode_lines (w->vchild, force);
18385 else if (force
18386 || FRAME_GARBAGED_P (XFRAME (w->frame))
18387 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18388 {
18389 struct text_pos lpoint;
18390 struct buffer *old = current_buffer;
18391
18392 /* Set the window's buffer for the mode line display. */
18393 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18394 set_buffer_internal_1 (XBUFFER (w->buffer));
18395
18396 /* Point refers normally to the selected window. For any
18397 other window, set up appropriate value. */
18398 if (!EQ (window, selected_window))
18399 {
18400 struct text_pos pt;
18401
18402 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18403 if (CHARPOS (pt) < BEGV)
18404 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18405 else if (CHARPOS (pt) > (ZV - 1))
18406 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18407 else
18408 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18409 }
18410
18411 /* Display mode lines. */
18412 clear_glyph_matrix (w->desired_matrix);
18413 if (display_mode_lines (w))
18414 {
18415 ++nwindows;
18416 w->must_be_updated_p = 1;
18417 }
18418
18419 /* Restore old settings. */
18420 set_buffer_internal_1 (old);
18421 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18422 }
18423
18424 window = w->next;
18425 }
18426
18427 return nwindows;
18428 }
18429
18430
18431 /* Display the mode and/or header line of window W. Value is the
18432 sum number of mode lines and header lines displayed. */
18433
18434 static int
18435 display_mode_lines (struct window *w)
18436 {
18437 Lisp_Object old_selected_window, old_selected_frame;
18438 int n = 0;
18439
18440 old_selected_frame = selected_frame;
18441 selected_frame = w->frame;
18442 old_selected_window = selected_window;
18443 XSETWINDOW (selected_window, w);
18444
18445 /* These will be set while the mode line specs are processed. */
18446 line_number_displayed = 0;
18447 w->column_number_displayed = Qnil;
18448
18449 if (WINDOW_WANTS_MODELINE_P (w))
18450 {
18451 struct window *sel_w = XWINDOW (old_selected_window);
18452
18453 /* Select mode line face based on the real selected window. */
18454 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18455 BVAR (current_buffer, mode_line_format));
18456 ++n;
18457 }
18458
18459 if (WINDOW_WANTS_HEADER_LINE_P (w))
18460 {
18461 display_mode_line (w, HEADER_LINE_FACE_ID,
18462 BVAR (current_buffer, header_line_format));
18463 ++n;
18464 }
18465
18466 selected_frame = old_selected_frame;
18467 selected_window = old_selected_window;
18468 return n;
18469 }
18470
18471
18472 /* Display mode or header line of window W. FACE_ID specifies which
18473 line to display; it is either MODE_LINE_FACE_ID or
18474 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18475 display. Value is the pixel height of the mode/header line
18476 displayed. */
18477
18478 static int
18479 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18480 {
18481 struct it it;
18482 struct face *face;
18483 int count = SPECPDL_INDEX ();
18484
18485 init_iterator (&it, w, -1, -1, NULL, face_id);
18486 /* Don't extend on a previously drawn mode-line.
18487 This may happen if called from pos_visible_p. */
18488 it.glyph_row->enabled_p = 0;
18489 prepare_desired_row (it.glyph_row);
18490
18491 it.glyph_row->mode_line_p = 1;
18492
18493 if (! mode_line_inverse_video)
18494 /* Force the mode-line to be displayed in the default face. */
18495 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18496
18497 record_unwind_protect (unwind_format_mode_line,
18498 format_mode_line_unwind_data (NULL, Qnil, 0));
18499
18500 mode_line_target = MODE_LINE_DISPLAY;
18501
18502 /* Temporarily make frame's keyboard the current kboard so that
18503 kboard-local variables in the mode_line_format will get the right
18504 values. */
18505 push_kboard (FRAME_KBOARD (it.f));
18506 record_unwind_save_match_data ();
18507 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18508 pop_kboard ();
18509
18510 unbind_to (count, Qnil);
18511
18512 /* Fill up with spaces. */
18513 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18514
18515 compute_line_metrics (&it);
18516 it.glyph_row->full_width_p = 1;
18517 it.glyph_row->continued_p = 0;
18518 it.glyph_row->truncated_on_left_p = 0;
18519 it.glyph_row->truncated_on_right_p = 0;
18520
18521 /* Make a 3D mode-line have a shadow at its right end. */
18522 face = FACE_FROM_ID (it.f, face_id);
18523 extend_face_to_end_of_line (&it);
18524 if (face->box != FACE_NO_BOX)
18525 {
18526 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18527 + it.glyph_row->used[TEXT_AREA] - 1);
18528 last->right_box_line_p = 1;
18529 }
18530
18531 return it.glyph_row->height;
18532 }
18533
18534 /* Move element ELT in LIST to the front of LIST.
18535 Return the updated list. */
18536
18537 static Lisp_Object
18538 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18539 {
18540 register Lisp_Object tail, prev;
18541 register Lisp_Object tem;
18542
18543 tail = list;
18544 prev = Qnil;
18545 while (CONSP (tail))
18546 {
18547 tem = XCAR (tail);
18548
18549 if (EQ (elt, tem))
18550 {
18551 /* Splice out the link TAIL. */
18552 if (NILP (prev))
18553 list = XCDR (tail);
18554 else
18555 Fsetcdr (prev, XCDR (tail));
18556
18557 /* Now make it the first. */
18558 Fsetcdr (tail, list);
18559 return tail;
18560 }
18561 else
18562 prev = tail;
18563 tail = XCDR (tail);
18564 QUIT;
18565 }
18566
18567 /* Not found--return unchanged LIST. */
18568 return list;
18569 }
18570
18571 /* Contribute ELT to the mode line for window IT->w. How it
18572 translates into text depends on its data type.
18573
18574 IT describes the display environment in which we display, as usual.
18575
18576 DEPTH is the depth in recursion. It is used to prevent
18577 infinite recursion here.
18578
18579 FIELD_WIDTH is the number of characters the display of ELT should
18580 occupy in the mode line, and PRECISION is the maximum number of
18581 characters to display from ELT's representation. See
18582 display_string for details.
18583
18584 Returns the hpos of the end of the text generated by ELT.
18585
18586 PROPS is a property list to add to any string we encounter.
18587
18588 If RISKY is nonzero, remove (disregard) any properties in any string
18589 we encounter, and ignore :eval and :propertize.
18590
18591 The global variable `mode_line_target' determines whether the
18592 output is passed to `store_mode_line_noprop',
18593 `store_mode_line_string', or `display_string'. */
18594
18595 static int
18596 display_mode_element (struct it *it, int depth, int field_width, int precision,
18597 Lisp_Object elt, Lisp_Object props, int risky)
18598 {
18599 int n = 0, field, prec;
18600 int literal = 0;
18601
18602 tail_recurse:
18603 if (depth > 100)
18604 elt = build_string ("*too-deep*");
18605
18606 depth++;
18607
18608 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18609 {
18610 case Lisp_String:
18611 {
18612 /* A string: output it and check for %-constructs within it. */
18613 unsigned char c;
18614 EMACS_INT offset = 0;
18615
18616 if (SCHARS (elt) > 0
18617 && (!NILP (props) || risky))
18618 {
18619 Lisp_Object oprops, aelt;
18620 oprops = Ftext_properties_at (make_number (0), elt);
18621
18622 /* If the starting string's properties are not what
18623 we want, translate the string. Also, if the string
18624 is risky, do that anyway. */
18625
18626 if (NILP (Fequal (props, oprops)) || risky)
18627 {
18628 /* If the starting string has properties,
18629 merge the specified ones onto the existing ones. */
18630 if (! NILP (oprops) && !risky)
18631 {
18632 Lisp_Object tem;
18633
18634 oprops = Fcopy_sequence (oprops);
18635 tem = props;
18636 while (CONSP (tem))
18637 {
18638 oprops = Fplist_put (oprops, XCAR (tem),
18639 XCAR (XCDR (tem)));
18640 tem = XCDR (XCDR (tem));
18641 }
18642 props = oprops;
18643 }
18644
18645 aelt = Fassoc (elt, mode_line_proptrans_alist);
18646 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18647 {
18648 /* AELT is what we want. Move it to the front
18649 without consing. */
18650 elt = XCAR (aelt);
18651 mode_line_proptrans_alist
18652 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18653 }
18654 else
18655 {
18656 Lisp_Object tem;
18657
18658 /* If AELT has the wrong props, it is useless.
18659 so get rid of it. */
18660 if (! NILP (aelt))
18661 mode_line_proptrans_alist
18662 = Fdelq (aelt, mode_line_proptrans_alist);
18663
18664 elt = Fcopy_sequence (elt);
18665 Fset_text_properties (make_number (0), Flength (elt),
18666 props, elt);
18667 /* Add this item to mode_line_proptrans_alist. */
18668 mode_line_proptrans_alist
18669 = Fcons (Fcons (elt, props),
18670 mode_line_proptrans_alist);
18671 /* Truncate mode_line_proptrans_alist
18672 to at most 50 elements. */
18673 tem = Fnthcdr (make_number (50),
18674 mode_line_proptrans_alist);
18675 if (! NILP (tem))
18676 XSETCDR (tem, Qnil);
18677 }
18678 }
18679 }
18680
18681 offset = 0;
18682
18683 if (literal)
18684 {
18685 prec = precision - n;
18686 switch (mode_line_target)
18687 {
18688 case MODE_LINE_NOPROP:
18689 case MODE_LINE_TITLE:
18690 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
18691 break;
18692 case MODE_LINE_STRING:
18693 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18694 break;
18695 case MODE_LINE_DISPLAY:
18696 n += display_string (NULL, elt, Qnil, 0, 0, it,
18697 0, prec, 0, STRING_MULTIBYTE (elt));
18698 break;
18699 }
18700
18701 break;
18702 }
18703
18704 /* Handle the non-literal case. */
18705
18706 while ((precision <= 0 || n < precision)
18707 && SREF (elt, offset) != 0
18708 && (mode_line_target != MODE_LINE_DISPLAY
18709 || it->current_x < it->last_visible_x))
18710 {
18711 EMACS_INT last_offset = offset;
18712
18713 /* Advance to end of string or next format specifier. */
18714 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18715 ;
18716
18717 if (offset - 1 != last_offset)
18718 {
18719 EMACS_INT nchars, nbytes;
18720
18721 /* Output to end of string or up to '%'. Field width
18722 is length of string. Don't output more than
18723 PRECISION allows us. */
18724 offset--;
18725
18726 prec = c_string_width (SDATA (elt) + last_offset,
18727 offset - last_offset, precision - n,
18728 &nchars, &nbytes);
18729
18730 switch (mode_line_target)
18731 {
18732 case MODE_LINE_NOPROP:
18733 case MODE_LINE_TITLE:
18734 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
18735 break;
18736 case MODE_LINE_STRING:
18737 {
18738 EMACS_INT bytepos = last_offset;
18739 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18740 EMACS_INT endpos = (precision <= 0
18741 ? string_byte_to_char (elt, offset)
18742 : charpos + nchars);
18743
18744 n += store_mode_line_string (NULL,
18745 Fsubstring (elt, make_number (charpos),
18746 make_number (endpos)),
18747 0, 0, 0, Qnil);
18748 }
18749 break;
18750 case MODE_LINE_DISPLAY:
18751 {
18752 EMACS_INT bytepos = last_offset;
18753 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18754
18755 if (precision <= 0)
18756 nchars = string_byte_to_char (elt, offset) - charpos;
18757 n += display_string (NULL, elt, Qnil, 0, charpos,
18758 it, 0, nchars, 0,
18759 STRING_MULTIBYTE (elt));
18760 }
18761 break;
18762 }
18763 }
18764 else /* c == '%' */
18765 {
18766 EMACS_INT percent_position = offset;
18767
18768 /* Get the specified minimum width. Zero means
18769 don't pad. */
18770 field = 0;
18771 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18772 field = field * 10 + c - '0';
18773
18774 /* Don't pad beyond the total padding allowed. */
18775 if (field_width - n > 0 && field > field_width - n)
18776 field = field_width - n;
18777
18778 /* Note that either PRECISION <= 0 or N < PRECISION. */
18779 prec = precision - n;
18780
18781 if (c == 'M')
18782 n += display_mode_element (it, depth, field, prec,
18783 Vglobal_mode_string, props,
18784 risky);
18785 else if (c != 0)
18786 {
18787 int multibyte;
18788 EMACS_INT bytepos, charpos;
18789 const char *spec;
18790 Lisp_Object string;
18791
18792 bytepos = percent_position;
18793 charpos = (STRING_MULTIBYTE (elt)
18794 ? string_byte_to_char (elt, bytepos)
18795 : bytepos);
18796 spec = decode_mode_spec (it->w, c, field, &string);
18797 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18798
18799 switch (mode_line_target)
18800 {
18801 case MODE_LINE_NOPROP:
18802 case MODE_LINE_TITLE:
18803 n += store_mode_line_noprop (spec, field, prec);
18804 break;
18805 case MODE_LINE_STRING:
18806 {
18807 int len = strlen (spec);
18808 Lisp_Object tem = make_string (spec, len);
18809 props = Ftext_properties_at (make_number (charpos), elt);
18810 /* Should only keep face property in props */
18811 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18812 }
18813 break;
18814 case MODE_LINE_DISPLAY:
18815 {
18816 int nglyphs_before, nwritten;
18817
18818 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18819 nwritten = display_string (spec, string, elt,
18820 charpos, 0, it,
18821 field, prec, 0,
18822 multibyte);
18823
18824 /* Assign to the glyphs written above the
18825 string where the `%x' came from, position
18826 of the `%'. */
18827 if (nwritten > 0)
18828 {
18829 struct glyph *glyph
18830 = (it->glyph_row->glyphs[TEXT_AREA]
18831 + nglyphs_before);
18832 int i;
18833
18834 for (i = 0; i < nwritten; ++i)
18835 {
18836 glyph[i].object = elt;
18837 glyph[i].charpos = charpos;
18838 }
18839
18840 n += nwritten;
18841 }
18842 }
18843 break;
18844 }
18845 }
18846 else /* c == 0 */
18847 break;
18848 }
18849 }
18850 }
18851 break;
18852
18853 case Lisp_Symbol:
18854 /* A symbol: process the value of the symbol recursively
18855 as if it appeared here directly. Avoid error if symbol void.
18856 Special case: if value of symbol is a string, output the string
18857 literally. */
18858 {
18859 register Lisp_Object tem;
18860
18861 /* If the variable is not marked as risky to set
18862 then its contents are risky to use. */
18863 if (NILP (Fget (elt, Qrisky_local_variable)))
18864 risky = 1;
18865
18866 tem = Fboundp (elt);
18867 if (!NILP (tem))
18868 {
18869 tem = Fsymbol_value (elt);
18870 /* If value is a string, output that string literally:
18871 don't check for % within it. */
18872 if (STRINGP (tem))
18873 literal = 1;
18874
18875 if (!EQ (tem, elt))
18876 {
18877 /* Give up right away for nil or t. */
18878 elt = tem;
18879 goto tail_recurse;
18880 }
18881 }
18882 }
18883 break;
18884
18885 case Lisp_Cons:
18886 {
18887 register Lisp_Object car, tem;
18888
18889 /* A cons cell: five distinct cases.
18890 If first element is :eval or :propertize, do something special.
18891 If first element is a string or a cons, process all the elements
18892 and effectively concatenate them.
18893 If first element is a negative number, truncate displaying cdr to
18894 at most that many characters. If positive, pad (with spaces)
18895 to at least that many characters.
18896 If first element is a symbol, process the cadr or caddr recursively
18897 according to whether the symbol's value is non-nil or nil. */
18898 car = XCAR (elt);
18899 if (EQ (car, QCeval))
18900 {
18901 /* An element of the form (:eval FORM) means evaluate FORM
18902 and use the result as mode line elements. */
18903
18904 if (risky)
18905 break;
18906
18907 if (CONSP (XCDR (elt)))
18908 {
18909 Lisp_Object spec;
18910 spec = safe_eval (XCAR (XCDR (elt)));
18911 n += display_mode_element (it, depth, field_width - n,
18912 precision - n, spec, props,
18913 risky);
18914 }
18915 }
18916 else if (EQ (car, QCpropertize))
18917 {
18918 /* An element of the form (:propertize ELT PROPS...)
18919 means display ELT but applying properties PROPS. */
18920
18921 if (risky)
18922 break;
18923
18924 if (CONSP (XCDR (elt)))
18925 n += display_mode_element (it, depth, field_width - n,
18926 precision - n, XCAR (XCDR (elt)),
18927 XCDR (XCDR (elt)), risky);
18928 }
18929 else if (SYMBOLP (car))
18930 {
18931 tem = Fboundp (car);
18932 elt = XCDR (elt);
18933 if (!CONSP (elt))
18934 goto invalid;
18935 /* elt is now the cdr, and we know it is a cons cell.
18936 Use its car if CAR has a non-nil value. */
18937 if (!NILP (tem))
18938 {
18939 tem = Fsymbol_value (car);
18940 if (!NILP (tem))
18941 {
18942 elt = XCAR (elt);
18943 goto tail_recurse;
18944 }
18945 }
18946 /* Symbol's value is nil (or symbol is unbound)
18947 Get the cddr of the original list
18948 and if possible find the caddr and use that. */
18949 elt = XCDR (elt);
18950 if (NILP (elt))
18951 break;
18952 else if (!CONSP (elt))
18953 goto invalid;
18954 elt = XCAR (elt);
18955 goto tail_recurse;
18956 }
18957 else if (INTEGERP (car))
18958 {
18959 register int lim = XINT (car);
18960 elt = XCDR (elt);
18961 if (lim < 0)
18962 {
18963 /* Negative int means reduce maximum width. */
18964 if (precision <= 0)
18965 precision = -lim;
18966 else
18967 precision = min (precision, -lim);
18968 }
18969 else if (lim > 0)
18970 {
18971 /* Padding specified. Don't let it be more than
18972 current maximum. */
18973 if (precision > 0)
18974 lim = min (precision, lim);
18975
18976 /* If that's more padding than already wanted, queue it.
18977 But don't reduce padding already specified even if
18978 that is beyond the current truncation point. */
18979 field_width = max (lim, field_width);
18980 }
18981 goto tail_recurse;
18982 }
18983 else if (STRINGP (car) || CONSP (car))
18984 {
18985 Lisp_Object halftail = elt;
18986 int len = 0;
18987
18988 while (CONSP (elt)
18989 && (precision <= 0 || n < precision))
18990 {
18991 n += display_mode_element (it, depth,
18992 /* Do padding only after the last
18993 element in the list. */
18994 (! CONSP (XCDR (elt))
18995 ? field_width - n
18996 : 0),
18997 precision - n, XCAR (elt),
18998 props, risky);
18999 elt = XCDR (elt);
19000 len++;
19001 if ((len & 1) == 0)
19002 halftail = XCDR (halftail);
19003 /* Check for cycle. */
19004 if (EQ (halftail, elt))
19005 break;
19006 }
19007 }
19008 }
19009 break;
19010
19011 default:
19012 invalid:
19013 elt = build_string ("*invalid*");
19014 goto tail_recurse;
19015 }
19016
19017 /* Pad to FIELD_WIDTH. */
19018 if (field_width > 0 && n < field_width)
19019 {
19020 switch (mode_line_target)
19021 {
19022 case MODE_LINE_NOPROP:
19023 case MODE_LINE_TITLE:
19024 n += store_mode_line_noprop ("", field_width - n, 0);
19025 break;
19026 case MODE_LINE_STRING:
19027 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19028 break;
19029 case MODE_LINE_DISPLAY:
19030 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19031 0, 0, 0);
19032 break;
19033 }
19034 }
19035
19036 return n;
19037 }
19038
19039 /* Store a mode-line string element in mode_line_string_list.
19040
19041 If STRING is non-null, display that C string. Otherwise, the Lisp
19042 string LISP_STRING is displayed.
19043
19044 FIELD_WIDTH is the minimum number of output glyphs to produce.
19045 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19046 with spaces. FIELD_WIDTH <= 0 means don't pad.
19047
19048 PRECISION is the maximum number of characters to output from
19049 STRING. PRECISION <= 0 means don't truncate the string.
19050
19051 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19052 properties to the string.
19053
19054 PROPS are the properties to add to the string.
19055 The mode_line_string_face face property is always added to the string.
19056 */
19057
19058 static int
19059 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19060 int field_width, int precision, Lisp_Object props)
19061 {
19062 EMACS_INT len;
19063 int n = 0;
19064
19065 if (string != NULL)
19066 {
19067 len = strlen (string);
19068 if (precision > 0 && len > precision)
19069 len = precision;
19070 lisp_string = make_string (string, len);
19071 if (NILP (props))
19072 props = mode_line_string_face_prop;
19073 else if (!NILP (mode_line_string_face))
19074 {
19075 Lisp_Object face = Fplist_get (props, Qface);
19076 props = Fcopy_sequence (props);
19077 if (NILP (face))
19078 face = mode_line_string_face;
19079 else
19080 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19081 props = Fplist_put (props, Qface, face);
19082 }
19083 Fadd_text_properties (make_number (0), make_number (len),
19084 props, lisp_string);
19085 }
19086 else
19087 {
19088 len = XFASTINT (Flength (lisp_string));
19089 if (precision > 0 && len > precision)
19090 {
19091 len = precision;
19092 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19093 precision = -1;
19094 }
19095 if (!NILP (mode_line_string_face))
19096 {
19097 Lisp_Object face;
19098 if (NILP (props))
19099 props = Ftext_properties_at (make_number (0), lisp_string);
19100 face = Fplist_get (props, Qface);
19101 if (NILP (face))
19102 face = mode_line_string_face;
19103 else
19104 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19105 props = Fcons (Qface, Fcons (face, Qnil));
19106 if (copy_string)
19107 lisp_string = Fcopy_sequence (lisp_string);
19108 }
19109 if (!NILP (props))
19110 Fadd_text_properties (make_number (0), make_number (len),
19111 props, lisp_string);
19112 }
19113
19114 if (len > 0)
19115 {
19116 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19117 n += len;
19118 }
19119
19120 if (field_width > len)
19121 {
19122 field_width -= len;
19123 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19124 if (!NILP (props))
19125 Fadd_text_properties (make_number (0), make_number (field_width),
19126 props, lisp_string);
19127 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19128 n += field_width;
19129 }
19130
19131 return n;
19132 }
19133
19134
19135 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19136 1, 4, 0,
19137 doc: /* Format a string out of a mode line format specification.
19138 First arg FORMAT specifies the mode line format (see `mode-line-format'
19139 for details) to use.
19140
19141 By default, the format is evaluated for the currently selected window.
19142
19143 Optional second arg FACE specifies the face property to put on all
19144 characters for which no face is specified. The value nil means the
19145 default face. The value t means whatever face the window's mode line
19146 currently uses (either `mode-line' or `mode-line-inactive',
19147 depending on whether the window is the selected window or not).
19148 An integer value means the value string has no text
19149 properties.
19150
19151 Optional third and fourth args WINDOW and BUFFER specify the window
19152 and buffer to use as the context for the formatting (defaults
19153 are the selected window and the WINDOW's buffer). */)
19154 (Lisp_Object format, Lisp_Object face,
19155 Lisp_Object window, Lisp_Object buffer)
19156 {
19157 struct it it;
19158 int len;
19159 struct window *w;
19160 struct buffer *old_buffer = NULL;
19161 int face_id;
19162 int no_props = INTEGERP (face);
19163 int count = SPECPDL_INDEX ();
19164 Lisp_Object str;
19165 int string_start = 0;
19166
19167 if (NILP (window))
19168 window = selected_window;
19169 CHECK_WINDOW (window);
19170 w = XWINDOW (window);
19171
19172 if (NILP (buffer))
19173 buffer = w->buffer;
19174 CHECK_BUFFER (buffer);
19175
19176 /* Make formatting the modeline a non-op when noninteractive, otherwise
19177 there will be problems later caused by a partially initialized frame. */
19178 if (NILP (format) || noninteractive)
19179 return empty_unibyte_string;
19180
19181 if (no_props)
19182 face = Qnil;
19183
19184 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19185 : EQ (face, Qt) ? (EQ (window, selected_window)
19186 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19187 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19188 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19189 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19190 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19191 : DEFAULT_FACE_ID;
19192
19193 if (XBUFFER (buffer) != current_buffer)
19194 old_buffer = current_buffer;
19195
19196 /* Save things including mode_line_proptrans_alist,
19197 and set that to nil so that we don't alter the outer value. */
19198 record_unwind_protect (unwind_format_mode_line,
19199 format_mode_line_unwind_data
19200 (old_buffer, selected_window, 1));
19201 mode_line_proptrans_alist = Qnil;
19202
19203 Fselect_window (window, Qt);
19204 if (old_buffer)
19205 set_buffer_internal_1 (XBUFFER (buffer));
19206
19207 init_iterator (&it, w, -1, -1, NULL, face_id);
19208
19209 if (no_props)
19210 {
19211 mode_line_target = MODE_LINE_NOPROP;
19212 mode_line_string_face_prop = Qnil;
19213 mode_line_string_list = Qnil;
19214 string_start = MODE_LINE_NOPROP_LEN (0);
19215 }
19216 else
19217 {
19218 mode_line_target = MODE_LINE_STRING;
19219 mode_line_string_list = Qnil;
19220 mode_line_string_face = face;
19221 mode_line_string_face_prop
19222 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19223 }
19224
19225 push_kboard (FRAME_KBOARD (it.f));
19226 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19227 pop_kboard ();
19228
19229 if (no_props)
19230 {
19231 len = MODE_LINE_NOPROP_LEN (string_start);
19232 str = make_string (mode_line_noprop_buf + string_start, len);
19233 }
19234 else
19235 {
19236 mode_line_string_list = Fnreverse (mode_line_string_list);
19237 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19238 empty_unibyte_string);
19239 }
19240
19241 unbind_to (count, Qnil);
19242 return str;
19243 }
19244
19245 /* Write a null-terminated, right justified decimal representation of
19246 the positive integer D to BUF using a minimal field width WIDTH. */
19247
19248 static void
19249 pint2str (register char *buf, register int width, register EMACS_INT d)
19250 {
19251 register char *p = buf;
19252
19253 if (d <= 0)
19254 *p++ = '0';
19255 else
19256 {
19257 while (d > 0)
19258 {
19259 *p++ = d % 10 + '0';
19260 d /= 10;
19261 }
19262 }
19263
19264 for (width -= (int) (p - buf); width > 0; --width)
19265 *p++ = ' ';
19266 *p-- = '\0';
19267 while (p > buf)
19268 {
19269 d = *buf;
19270 *buf++ = *p;
19271 *p-- = d;
19272 }
19273 }
19274
19275 /* Write a null-terminated, right justified decimal and "human
19276 readable" representation of the nonnegative integer D to BUF using
19277 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19278
19279 static const char power_letter[] =
19280 {
19281 0, /* no letter */
19282 'k', /* kilo */
19283 'M', /* mega */
19284 'G', /* giga */
19285 'T', /* tera */
19286 'P', /* peta */
19287 'E', /* exa */
19288 'Z', /* zetta */
19289 'Y' /* yotta */
19290 };
19291
19292 static void
19293 pint2hrstr (char *buf, int width, EMACS_INT d)
19294 {
19295 /* We aim to represent the nonnegative integer D as
19296 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19297 EMACS_INT quotient = d;
19298 int remainder = 0;
19299 /* -1 means: do not use TENTHS. */
19300 int tenths = -1;
19301 int exponent = 0;
19302
19303 /* Length of QUOTIENT.TENTHS as a string. */
19304 int length;
19305
19306 char * psuffix;
19307 char * p;
19308
19309 if (1000 <= quotient)
19310 {
19311 /* Scale to the appropriate EXPONENT. */
19312 do
19313 {
19314 remainder = quotient % 1000;
19315 quotient /= 1000;
19316 exponent++;
19317 }
19318 while (1000 <= quotient);
19319
19320 /* Round to nearest and decide whether to use TENTHS or not. */
19321 if (quotient <= 9)
19322 {
19323 tenths = remainder / 100;
19324 if (50 <= remainder % 100)
19325 {
19326 if (tenths < 9)
19327 tenths++;
19328 else
19329 {
19330 quotient++;
19331 if (quotient == 10)
19332 tenths = -1;
19333 else
19334 tenths = 0;
19335 }
19336 }
19337 }
19338 else
19339 if (500 <= remainder)
19340 {
19341 if (quotient < 999)
19342 quotient++;
19343 else
19344 {
19345 quotient = 1;
19346 exponent++;
19347 tenths = 0;
19348 }
19349 }
19350 }
19351
19352 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19353 if (tenths == -1 && quotient <= 99)
19354 if (quotient <= 9)
19355 length = 1;
19356 else
19357 length = 2;
19358 else
19359 length = 3;
19360 p = psuffix = buf + max (width, length);
19361
19362 /* Print EXPONENT. */
19363 *psuffix++ = power_letter[exponent];
19364 *psuffix = '\0';
19365
19366 /* Print TENTHS. */
19367 if (tenths >= 0)
19368 {
19369 *--p = '0' + tenths;
19370 *--p = '.';
19371 }
19372
19373 /* Print QUOTIENT. */
19374 do
19375 {
19376 int digit = quotient % 10;
19377 *--p = '0' + digit;
19378 }
19379 while ((quotient /= 10) != 0);
19380
19381 /* Print leading spaces. */
19382 while (buf < p)
19383 *--p = ' ';
19384 }
19385
19386 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19387 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19388 type of CODING_SYSTEM. Return updated pointer into BUF. */
19389
19390 static unsigned char invalid_eol_type[] = "(*invalid*)";
19391
19392 static char *
19393 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19394 {
19395 Lisp_Object val;
19396 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19397 const unsigned char *eol_str;
19398 int eol_str_len;
19399 /* The EOL conversion we are using. */
19400 Lisp_Object eoltype;
19401
19402 val = CODING_SYSTEM_SPEC (coding_system);
19403 eoltype = Qnil;
19404
19405 if (!VECTORP (val)) /* Not yet decided. */
19406 {
19407 if (multibyte)
19408 *buf++ = '-';
19409 if (eol_flag)
19410 eoltype = eol_mnemonic_undecided;
19411 /* Don't mention EOL conversion if it isn't decided. */
19412 }
19413 else
19414 {
19415 Lisp_Object attrs;
19416 Lisp_Object eolvalue;
19417
19418 attrs = AREF (val, 0);
19419 eolvalue = AREF (val, 2);
19420
19421 if (multibyte)
19422 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19423
19424 if (eol_flag)
19425 {
19426 /* The EOL conversion that is normal on this system. */
19427
19428 if (NILP (eolvalue)) /* Not yet decided. */
19429 eoltype = eol_mnemonic_undecided;
19430 else if (VECTORP (eolvalue)) /* Not yet decided. */
19431 eoltype = eol_mnemonic_undecided;
19432 else /* eolvalue is Qunix, Qdos, or Qmac. */
19433 eoltype = (EQ (eolvalue, Qunix)
19434 ? eol_mnemonic_unix
19435 : (EQ (eolvalue, Qdos) == 1
19436 ? eol_mnemonic_dos : eol_mnemonic_mac));
19437 }
19438 }
19439
19440 if (eol_flag)
19441 {
19442 /* Mention the EOL conversion if it is not the usual one. */
19443 if (STRINGP (eoltype))
19444 {
19445 eol_str = SDATA (eoltype);
19446 eol_str_len = SBYTES (eoltype);
19447 }
19448 else if (CHARACTERP (eoltype))
19449 {
19450 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19451 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19452 eol_str = tmp;
19453 }
19454 else
19455 {
19456 eol_str = invalid_eol_type;
19457 eol_str_len = sizeof (invalid_eol_type) - 1;
19458 }
19459 memcpy (buf, eol_str, eol_str_len);
19460 buf += eol_str_len;
19461 }
19462
19463 return buf;
19464 }
19465
19466 /* Return a string for the output of a mode line %-spec for window W,
19467 generated by character C. FIELD_WIDTH > 0 means pad the string
19468 returned with spaces to that value. Return a Lisp string in
19469 *STRING if the resulting string is taken from that Lisp string.
19470
19471 Note we operate on the current buffer for most purposes,
19472 the exception being w->base_line_pos. */
19473
19474 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19475
19476 static const char *
19477 decode_mode_spec (struct window *w, register int c, int field_width,
19478 Lisp_Object *string)
19479 {
19480 Lisp_Object obj;
19481 struct frame *f = XFRAME (WINDOW_FRAME (w));
19482 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19483 struct buffer *b = current_buffer;
19484
19485 obj = Qnil;
19486 *string = Qnil;
19487
19488 switch (c)
19489 {
19490 case '*':
19491 if (!NILP (BVAR (b, read_only)))
19492 return "%";
19493 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19494 return "*";
19495 return "-";
19496
19497 case '+':
19498 /* This differs from %* only for a modified read-only buffer. */
19499 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19500 return "*";
19501 if (!NILP (BVAR (b, read_only)))
19502 return "%";
19503 return "-";
19504
19505 case '&':
19506 /* This differs from %* in ignoring read-only-ness. */
19507 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19508 return "*";
19509 return "-";
19510
19511 case '%':
19512 return "%";
19513
19514 case '[':
19515 {
19516 int i;
19517 char *p;
19518
19519 if (command_loop_level > 5)
19520 return "[[[... ";
19521 p = decode_mode_spec_buf;
19522 for (i = 0; i < command_loop_level; i++)
19523 *p++ = '[';
19524 *p = 0;
19525 return decode_mode_spec_buf;
19526 }
19527
19528 case ']':
19529 {
19530 int i;
19531 char *p;
19532
19533 if (command_loop_level > 5)
19534 return " ...]]]";
19535 p = decode_mode_spec_buf;
19536 for (i = 0; i < command_loop_level; i++)
19537 *p++ = ']';
19538 *p = 0;
19539 return decode_mode_spec_buf;
19540 }
19541
19542 case '-':
19543 {
19544 register int i;
19545
19546 /* Let lots_of_dashes be a string of infinite length. */
19547 if (mode_line_target == MODE_LINE_NOPROP ||
19548 mode_line_target == MODE_LINE_STRING)
19549 return "--";
19550 if (field_width <= 0
19551 || field_width > sizeof (lots_of_dashes))
19552 {
19553 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19554 decode_mode_spec_buf[i] = '-';
19555 decode_mode_spec_buf[i] = '\0';
19556 return decode_mode_spec_buf;
19557 }
19558 else
19559 return lots_of_dashes;
19560 }
19561
19562 case 'b':
19563 obj = BVAR (b, name);
19564 break;
19565
19566 case 'c':
19567 /* %c and %l are ignored in `frame-title-format'.
19568 (In redisplay_internal, the frame title is drawn _before_ the
19569 windows are updated, so the stuff which depends on actual
19570 window contents (such as %l) may fail to render properly, or
19571 even crash emacs.) */
19572 if (mode_line_target == MODE_LINE_TITLE)
19573 return "";
19574 else
19575 {
19576 EMACS_INT col = current_column ();
19577 w->column_number_displayed = make_number (col);
19578 pint2str (decode_mode_spec_buf, field_width, col);
19579 return decode_mode_spec_buf;
19580 }
19581
19582 case 'e':
19583 #ifndef SYSTEM_MALLOC
19584 {
19585 if (NILP (Vmemory_full))
19586 return "";
19587 else
19588 return "!MEM FULL! ";
19589 }
19590 #else
19591 return "";
19592 #endif
19593
19594 case 'F':
19595 /* %F displays the frame name. */
19596 if (!NILP (f->title))
19597 return SSDATA (f->title);
19598 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19599 return SSDATA (f->name);
19600 return "Emacs";
19601
19602 case 'f':
19603 obj = BVAR (b, filename);
19604 break;
19605
19606 case 'i':
19607 {
19608 EMACS_INT size = ZV - BEGV;
19609 pint2str (decode_mode_spec_buf, field_width, size);
19610 return decode_mode_spec_buf;
19611 }
19612
19613 case 'I':
19614 {
19615 EMACS_INT size = ZV - BEGV;
19616 pint2hrstr (decode_mode_spec_buf, field_width, size);
19617 return decode_mode_spec_buf;
19618 }
19619
19620 case 'l':
19621 {
19622 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19623 EMACS_INT topline, nlines, height;
19624 EMACS_INT junk;
19625
19626 /* %c and %l are ignored in `frame-title-format'. */
19627 if (mode_line_target == MODE_LINE_TITLE)
19628 return "";
19629
19630 startpos = XMARKER (w->start)->charpos;
19631 startpos_byte = marker_byte_position (w->start);
19632 height = WINDOW_TOTAL_LINES (w);
19633
19634 /* If we decided that this buffer isn't suitable for line numbers,
19635 don't forget that too fast. */
19636 if (EQ (w->base_line_pos, w->buffer))
19637 goto no_value;
19638 /* But do forget it, if the window shows a different buffer now. */
19639 else if (BUFFERP (w->base_line_pos))
19640 w->base_line_pos = Qnil;
19641
19642 /* If the buffer is very big, don't waste time. */
19643 if (INTEGERP (Vline_number_display_limit)
19644 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19645 {
19646 w->base_line_pos = Qnil;
19647 w->base_line_number = Qnil;
19648 goto no_value;
19649 }
19650
19651 if (INTEGERP (w->base_line_number)
19652 && INTEGERP (w->base_line_pos)
19653 && XFASTINT (w->base_line_pos) <= startpos)
19654 {
19655 line = XFASTINT (w->base_line_number);
19656 linepos = XFASTINT (w->base_line_pos);
19657 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19658 }
19659 else
19660 {
19661 line = 1;
19662 linepos = BUF_BEGV (b);
19663 linepos_byte = BUF_BEGV_BYTE (b);
19664 }
19665
19666 /* Count lines from base line to window start position. */
19667 nlines = display_count_lines (linepos_byte,
19668 startpos_byte,
19669 startpos, &junk);
19670
19671 topline = nlines + line;
19672
19673 /* Determine a new base line, if the old one is too close
19674 or too far away, or if we did not have one.
19675 "Too close" means it's plausible a scroll-down would
19676 go back past it. */
19677 if (startpos == BUF_BEGV (b))
19678 {
19679 w->base_line_number = make_number (topline);
19680 w->base_line_pos = make_number (BUF_BEGV (b));
19681 }
19682 else if (nlines < height + 25 || nlines > height * 3 + 50
19683 || linepos == BUF_BEGV (b))
19684 {
19685 EMACS_INT limit = BUF_BEGV (b);
19686 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19687 EMACS_INT position;
19688 EMACS_INT distance =
19689 (height * 2 + 30) * line_number_display_limit_width;
19690
19691 if (startpos - distance > limit)
19692 {
19693 limit = startpos - distance;
19694 limit_byte = CHAR_TO_BYTE (limit);
19695 }
19696
19697 nlines = display_count_lines (startpos_byte,
19698 limit_byte,
19699 - (height * 2 + 30),
19700 &position);
19701 /* If we couldn't find the lines we wanted within
19702 line_number_display_limit_width chars per line,
19703 give up on line numbers for this window. */
19704 if (position == limit_byte && limit == startpos - distance)
19705 {
19706 w->base_line_pos = w->buffer;
19707 w->base_line_number = Qnil;
19708 goto no_value;
19709 }
19710
19711 w->base_line_number = make_number (topline - nlines);
19712 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19713 }
19714
19715 /* Now count lines from the start pos to point. */
19716 nlines = display_count_lines (startpos_byte,
19717 PT_BYTE, PT, &junk);
19718
19719 /* Record that we did display the line number. */
19720 line_number_displayed = 1;
19721
19722 /* Make the string to show. */
19723 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19724 return decode_mode_spec_buf;
19725 no_value:
19726 {
19727 char* p = decode_mode_spec_buf;
19728 int pad = field_width - 2;
19729 while (pad-- > 0)
19730 *p++ = ' ';
19731 *p++ = '?';
19732 *p++ = '?';
19733 *p = '\0';
19734 return decode_mode_spec_buf;
19735 }
19736 }
19737 break;
19738
19739 case 'm':
19740 obj = BVAR (b, mode_name);
19741 break;
19742
19743 case 'n':
19744 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19745 return " Narrow";
19746 break;
19747
19748 case 'p':
19749 {
19750 EMACS_INT pos = marker_position (w->start);
19751 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19752
19753 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19754 {
19755 if (pos <= BUF_BEGV (b))
19756 return "All";
19757 else
19758 return "Bottom";
19759 }
19760 else if (pos <= BUF_BEGV (b))
19761 return "Top";
19762 else
19763 {
19764 if (total > 1000000)
19765 /* Do it differently for a large value, to avoid overflow. */
19766 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19767 else
19768 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19769 /* We can't normally display a 3-digit number,
19770 so get us a 2-digit number that is close. */
19771 if (total == 100)
19772 total = 99;
19773 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
19774 return decode_mode_spec_buf;
19775 }
19776 }
19777
19778 /* Display percentage of size above the bottom of the screen. */
19779 case 'P':
19780 {
19781 EMACS_INT toppos = marker_position (w->start);
19782 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19783 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19784
19785 if (botpos >= BUF_ZV (b))
19786 {
19787 if (toppos <= BUF_BEGV (b))
19788 return "All";
19789 else
19790 return "Bottom";
19791 }
19792 else
19793 {
19794 if (total > 1000000)
19795 /* Do it differently for a large value, to avoid overflow. */
19796 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19797 else
19798 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19799 /* We can't normally display a 3-digit number,
19800 so get us a 2-digit number that is close. */
19801 if (total == 100)
19802 total = 99;
19803 if (toppos <= BUF_BEGV (b))
19804 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
19805 else
19806 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
19807 return decode_mode_spec_buf;
19808 }
19809 }
19810
19811 case 's':
19812 /* status of process */
19813 obj = Fget_buffer_process (Fcurrent_buffer ());
19814 if (NILP (obj))
19815 return "no process";
19816 #ifndef MSDOS
19817 obj = Fsymbol_name (Fprocess_status (obj));
19818 #endif
19819 break;
19820
19821 case '@':
19822 {
19823 int count = inhibit_garbage_collection ();
19824 Lisp_Object val = call1 (intern ("file-remote-p"),
19825 BVAR (current_buffer, directory));
19826 unbind_to (count, Qnil);
19827
19828 if (NILP (val))
19829 return "-";
19830 else
19831 return "@";
19832 }
19833
19834 case 't': /* indicate TEXT or BINARY */
19835 return "T";
19836
19837 case 'z':
19838 /* coding-system (not including end-of-line format) */
19839 case 'Z':
19840 /* coding-system (including end-of-line type) */
19841 {
19842 int eol_flag = (c == 'Z');
19843 char *p = decode_mode_spec_buf;
19844
19845 if (! FRAME_WINDOW_P (f))
19846 {
19847 /* No need to mention EOL here--the terminal never needs
19848 to do EOL conversion. */
19849 p = decode_mode_spec_coding (CODING_ID_NAME
19850 (FRAME_KEYBOARD_CODING (f)->id),
19851 p, 0);
19852 p = decode_mode_spec_coding (CODING_ID_NAME
19853 (FRAME_TERMINAL_CODING (f)->id),
19854 p, 0);
19855 }
19856 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
19857 p, eol_flag);
19858
19859 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19860 #ifdef subprocesses
19861 obj = Fget_buffer_process (Fcurrent_buffer ());
19862 if (PROCESSP (obj))
19863 {
19864 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19865 p, eol_flag);
19866 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19867 p, eol_flag);
19868 }
19869 #endif /* subprocesses */
19870 #endif /* 0 */
19871 *p = 0;
19872 return decode_mode_spec_buf;
19873 }
19874 }
19875
19876 if (STRINGP (obj))
19877 {
19878 *string = obj;
19879 return SSDATA (obj);
19880 }
19881 else
19882 return "";
19883 }
19884
19885
19886 /* Count up to COUNT lines starting from START_BYTE.
19887 But don't go beyond LIMIT_BYTE.
19888 Return the number of lines thus found (always nonnegative).
19889
19890 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19891
19892 static EMACS_INT
19893 display_count_lines (EMACS_INT start_byte,
19894 EMACS_INT limit_byte, EMACS_INT count,
19895 EMACS_INT *byte_pos_ptr)
19896 {
19897 register unsigned char *cursor;
19898 unsigned char *base;
19899
19900 register EMACS_INT ceiling;
19901 register unsigned char *ceiling_addr;
19902 EMACS_INT orig_count = count;
19903
19904 /* If we are not in selective display mode,
19905 check only for newlines. */
19906 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
19907 && !INTEGERP (BVAR (current_buffer, selective_display)));
19908
19909 if (count > 0)
19910 {
19911 while (start_byte < limit_byte)
19912 {
19913 ceiling = BUFFER_CEILING_OF (start_byte);
19914 ceiling = min (limit_byte - 1, ceiling);
19915 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19916 base = (cursor = BYTE_POS_ADDR (start_byte));
19917 while (1)
19918 {
19919 if (selective_display)
19920 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19921 ;
19922 else
19923 while (*cursor != '\n' && ++cursor != ceiling_addr)
19924 ;
19925
19926 if (cursor != ceiling_addr)
19927 {
19928 if (--count == 0)
19929 {
19930 start_byte += cursor - base + 1;
19931 *byte_pos_ptr = start_byte;
19932 return orig_count;
19933 }
19934 else
19935 if (++cursor == ceiling_addr)
19936 break;
19937 }
19938 else
19939 break;
19940 }
19941 start_byte += cursor - base;
19942 }
19943 }
19944 else
19945 {
19946 while (start_byte > limit_byte)
19947 {
19948 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19949 ceiling = max (limit_byte, ceiling);
19950 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19951 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19952 while (1)
19953 {
19954 if (selective_display)
19955 while (--cursor != ceiling_addr
19956 && *cursor != '\n' && *cursor != 015)
19957 ;
19958 else
19959 while (--cursor != ceiling_addr && *cursor != '\n')
19960 ;
19961
19962 if (cursor != ceiling_addr)
19963 {
19964 if (++count == 0)
19965 {
19966 start_byte += cursor - base + 1;
19967 *byte_pos_ptr = start_byte;
19968 /* When scanning backwards, we should
19969 not count the newline posterior to which we stop. */
19970 return - orig_count - 1;
19971 }
19972 }
19973 else
19974 break;
19975 }
19976 /* Here we add 1 to compensate for the last decrement
19977 of CURSOR, which took it past the valid range. */
19978 start_byte += cursor - base + 1;
19979 }
19980 }
19981
19982 *byte_pos_ptr = limit_byte;
19983
19984 if (count < 0)
19985 return - orig_count + count;
19986 return orig_count - count;
19987
19988 }
19989
19990
19991 \f
19992 /***********************************************************************
19993 Displaying strings
19994 ***********************************************************************/
19995
19996 /* Display a NUL-terminated string, starting with index START.
19997
19998 If STRING is non-null, display that C string. Otherwise, the Lisp
19999 string LISP_STRING is displayed. There's a case that STRING is
20000 non-null and LISP_STRING is not nil. It means STRING is a string
20001 data of LISP_STRING. In that case, we display LISP_STRING while
20002 ignoring its text properties.
20003
20004 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20005 FACE_STRING. Display STRING or LISP_STRING with the face at
20006 FACE_STRING_POS in FACE_STRING:
20007
20008 Display the string in the environment given by IT, but use the
20009 standard display table, temporarily.
20010
20011 FIELD_WIDTH is the minimum number of output glyphs to produce.
20012 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20013 with spaces. If STRING has more characters, more than FIELD_WIDTH
20014 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20015
20016 PRECISION is the maximum number of characters to output from
20017 STRING. PRECISION < 0 means don't truncate the string.
20018
20019 This is roughly equivalent to printf format specifiers:
20020
20021 FIELD_WIDTH PRECISION PRINTF
20022 ----------------------------------------
20023 -1 -1 %s
20024 -1 10 %.10s
20025 10 -1 %10s
20026 20 10 %20.10s
20027
20028 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20029 display them, and < 0 means obey the current buffer's value of
20030 enable_multibyte_characters.
20031
20032 Value is the number of columns displayed. */
20033
20034 static int
20035 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20036 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20037 int field_width, int precision, int max_x, int multibyte)
20038 {
20039 int hpos_at_start = it->hpos;
20040 int saved_face_id = it->face_id;
20041 struct glyph_row *row = it->glyph_row;
20042
20043 /* Initialize the iterator IT for iteration over STRING beginning
20044 with index START. */
20045 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20046 precision, field_width, multibyte);
20047 if (string && STRINGP (lisp_string))
20048 /* LISP_STRING is the one returned by decode_mode_spec. We should
20049 ignore its text properties. */
20050 it->stop_charpos = it->end_charpos;
20051
20052 /* If displaying STRING, set up the face of the iterator from
20053 FACE_STRING, if that's given. */
20054 if (STRINGP (face_string))
20055 {
20056 EMACS_INT endptr;
20057 struct face *face;
20058
20059 it->face_id
20060 = face_at_string_position (it->w, face_string, face_string_pos,
20061 0, it->region_beg_charpos,
20062 it->region_end_charpos,
20063 &endptr, it->base_face_id, 0);
20064 face = FACE_FROM_ID (it->f, it->face_id);
20065 it->face_box_p = face->box != FACE_NO_BOX;
20066 }
20067
20068 /* Set max_x to the maximum allowed X position. Don't let it go
20069 beyond the right edge of the window. */
20070 if (max_x <= 0)
20071 max_x = it->last_visible_x;
20072 else
20073 max_x = min (max_x, it->last_visible_x);
20074
20075 /* Skip over display elements that are not visible. because IT->w is
20076 hscrolled. */
20077 if (it->current_x < it->first_visible_x)
20078 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20079 MOVE_TO_POS | MOVE_TO_X);
20080
20081 row->ascent = it->max_ascent;
20082 row->height = it->max_ascent + it->max_descent;
20083 row->phys_ascent = it->max_phys_ascent;
20084 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20085 row->extra_line_spacing = it->max_extra_line_spacing;
20086
20087 /* This condition is for the case that we are called with current_x
20088 past last_visible_x. */
20089 while (it->current_x < max_x)
20090 {
20091 int x_before, x, n_glyphs_before, i, nglyphs;
20092
20093 /* Get the next display element. */
20094 if (!get_next_display_element (it))
20095 break;
20096
20097 /* Produce glyphs. */
20098 x_before = it->current_x;
20099 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
20100 PRODUCE_GLYPHS (it);
20101
20102 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
20103 i = 0;
20104 x = x_before;
20105 while (i < nglyphs)
20106 {
20107 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20108
20109 if (it->line_wrap != TRUNCATE
20110 && x + glyph->pixel_width > max_x)
20111 {
20112 /* End of continued line or max_x reached. */
20113 if (CHAR_GLYPH_PADDING_P (*glyph))
20114 {
20115 /* A wide character is unbreakable. */
20116 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
20117 it->current_x = x_before;
20118 }
20119 else
20120 {
20121 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
20122 it->current_x = x;
20123 }
20124 break;
20125 }
20126 else if (x + glyph->pixel_width >= it->first_visible_x)
20127 {
20128 /* Glyph is at least partially visible. */
20129 ++it->hpos;
20130 if (x < it->first_visible_x)
20131 it->glyph_row->x = x - it->first_visible_x;
20132 }
20133 else
20134 {
20135 /* Glyph is off the left margin of the display area.
20136 Should not happen. */
20137 abort ();
20138 }
20139
20140 row->ascent = max (row->ascent, it->max_ascent);
20141 row->height = max (row->height, it->max_ascent + it->max_descent);
20142 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20143 row->phys_height = max (row->phys_height,
20144 it->max_phys_ascent + it->max_phys_descent);
20145 row->extra_line_spacing = max (row->extra_line_spacing,
20146 it->max_extra_line_spacing);
20147 x += glyph->pixel_width;
20148 ++i;
20149 }
20150
20151 /* Stop if max_x reached. */
20152 if (i < nglyphs)
20153 break;
20154
20155 /* Stop at line ends. */
20156 if (ITERATOR_AT_END_OF_LINE_P (it))
20157 {
20158 it->continuation_lines_width = 0;
20159 break;
20160 }
20161
20162 set_iterator_to_next (it, 1);
20163
20164 /* Stop if truncating at the right edge. */
20165 if (it->line_wrap == TRUNCATE
20166 && it->current_x >= it->last_visible_x)
20167 {
20168 /* Add truncation mark, but don't do it if the line is
20169 truncated at a padding space. */
20170 if (IT_CHARPOS (*it) < it->string_nchars)
20171 {
20172 if (!FRAME_WINDOW_P (it->f))
20173 {
20174 int ii, n;
20175
20176 if (it->current_x > it->last_visible_x)
20177 {
20178 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20179 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20180 break;
20181 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20182 {
20183 row->used[TEXT_AREA] = ii;
20184 produce_special_glyphs (it, IT_TRUNCATION);
20185 }
20186 }
20187 produce_special_glyphs (it, IT_TRUNCATION);
20188 }
20189 it->glyph_row->truncated_on_right_p = 1;
20190 }
20191 break;
20192 }
20193 }
20194
20195 /* Maybe insert a truncation at the left. */
20196 if (it->first_visible_x
20197 && IT_CHARPOS (*it) > 0)
20198 {
20199 if (!FRAME_WINDOW_P (it->f))
20200 insert_left_trunc_glyphs (it);
20201 it->glyph_row->truncated_on_left_p = 1;
20202 }
20203
20204 it->face_id = saved_face_id;
20205
20206 /* Value is number of columns displayed. */
20207 return it->hpos - hpos_at_start;
20208 }
20209
20210
20211 \f
20212 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20213 appears as an element of LIST or as the car of an element of LIST.
20214 If PROPVAL is a list, compare each element against LIST in that
20215 way, and return 1/2 if any element of PROPVAL is found in LIST.
20216 Otherwise return 0. This function cannot quit.
20217 The return value is 2 if the text is invisible but with an ellipsis
20218 and 1 if it's invisible and without an ellipsis. */
20219
20220 int
20221 invisible_p (register Lisp_Object propval, Lisp_Object list)
20222 {
20223 register Lisp_Object tail, proptail;
20224
20225 for (tail = list; CONSP (tail); tail = XCDR (tail))
20226 {
20227 register Lisp_Object tem;
20228 tem = XCAR (tail);
20229 if (EQ (propval, tem))
20230 return 1;
20231 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20232 return NILP (XCDR (tem)) ? 1 : 2;
20233 }
20234
20235 if (CONSP (propval))
20236 {
20237 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20238 {
20239 Lisp_Object propelt;
20240 propelt = XCAR (proptail);
20241 for (tail = list; CONSP (tail); tail = XCDR (tail))
20242 {
20243 register Lisp_Object tem;
20244 tem = XCAR (tail);
20245 if (EQ (propelt, tem))
20246 return 1;
20247 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20248 return NILP (XCDR (tem)) ? 1 : 2;
20249 }
20250 }
20251 }
20252
20253 return 0;
20254 }
20255
20256 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20257 doc: /* Non-nil if the property makes the text invisible.
20258 POS-OR-PROP can be a marker or number, in which case it is taken to be
20259 a position in the current buffer and the value of the `invisible' property
20260 is checked; or it can be some other value, which is then presumed to be the
20261 value of the `invisible' property of the text of interest.
20262 The non-nil value returned can be t for truly invisible text or something
20263 else if the text is replaced by an ellipsis. */)
20264 (Lisp_Object pos_or_prop)
20265 {
20266 Lisp_Object prop
20267 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20268 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20269 : pos_or_prop);
20270 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20271 return (invis == 0 ? Qnil
20272 : invis == 1 ? Qt
20273 : make_number (invis));
20274 }
20275
20276 /* Calculate a width or height in pixels from a specification using
20277 the following elements:
20278
20279 SPEC ::=
20280 NUM - a (fractional) multiple of the default font width/height
20281 (NUM) - specifies exactly NUM pixels
20282 UNIT - a fixed number of pixels, see below.
20283 ELEMENT - size of a display element in pixels, see below.
20284 (NUM . SPEC) - equals NUM * SPEC
20285 (+ SPEC SPEC ...) - add pixel values
20286 (- SPEC SPEC ...) - subtract pixel values
20287 (- SPEC) - negate pixel value
20288
20289 NUM ::=
20290 INT or FLOAT - a number constant
20291 SYMBOL - use symbol's (buffer local) variable binding.
20292
20293 UNIT ::=
20294 in - pixels per inch *)
20295 mm - pixels per 1/1000 meter *)
20296 cm - pixels per 1/100 meter *)
20297 width - width of current font in pixels.
20298 height - height of current font in pixels.
20299
20300 *) using the ratio(s) defined in display-pixels-per-inch.
20301
20302 ELEMENT ::=
20303
20304 left-fringe - left fringe width in pixels
20305 right-fringe - right fringe width in pixels
20306
20307 left-margin - left margin width in pixels
20308 right-margin - right margin width in pixels
20309
20310 scroll-bar - scroll-bar area width in pixels
20311
20312 Examples:
20313
20314 Pixels corresponding to 5 inches:
20315 (5 . in)
20316
20317 Total width of non-text areas on left side of window (if scroll-bar is on left):
20318 '(space :width (+ left-fringe left-margin scroll-bar))
20319
20320 Align to first text column (in header line):
20321 '(space :align-to 0)
20322
20323 Align to middle of text area minus half the width of variable `my-image'
20324 containing a loaded image:
20325 '(space :align-to (0.5 . (- text my-image)))
20326
20327 Width of left margin minus width of 1 character in the default font:
20328 '(space :width (- left-margin 1))
20329
20330 Width of left margin minus width of 2 characters in the current font:
20331 '(space :width (- left-margin (2 . width)))
20332
20333 Center 1 character over left-margin (in header line):
20334 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20335
20336 Different ways to express width of left fringe plus left margin minus one pixel:
20337 '(space :width (- (+ left-fringe left-margin) (1)))
20338 '(space :width (+ left-fringe left-margin (- (1))))
20339 '(space :width (+ left-fringe left-margin (-1)))
20340
20341 */
20342
20343 #define NUMVAL(X) \
20344 ((INTEGERP (X) || FLOATP (X)) \
20345 ? XFLOATINT (X) \
20346 : - 1)
20347
20348 int
20349 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20350 struct font *font, int width_p, int *align_to)
20351 {
20352 double pixels;
20353
20354 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20355 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20356
20357 if (NILP (prop))
20358 return OK_PIXELS (0);
20359
20360 xassert (FRAME_LIVE_P (it->f));
20361
20362 if (SYMBOLP (prop))
20363 {
20364 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20365 {
20366 char *unit = SSDATA (SYMBOL_NAME (prop));
20367
20368 if (unit[0] == 'i' && unit[1] == 'n')
20369 pixels = 1.0;
20370 else if (unit[0] == 'm' && unit[1] == 'm')
20371 pixels = 25.4;
20372 else if (unit[0] == 'c' && unit[1] == 'm')
20373 pixels = 2.54;
20374 else
20375 pixels = 0;
20376 if (pixels > 0)
20377 {
20378 double ppi;
20379 #ifdef HAVE_WINDOW_SYSTEM
20380 if (FRAME_WINDOW_P (it->f)
20381 && (ppi = (width_p
20382 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20383 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20384 ppi > 0))
20385 return OK_PIXELS (ppi / pixels);
20386 #endif
20387
20388 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20389 || (CONSP (Vdisplay_pixels_per_inch)
20390 && (ppi = (width_p
20391 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20392 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20393 ppi > 0)))
20394 return OK_PIXELS (ppi / pixels);
20395
20396 return 0;
20397 }
20398 }
20399
20400 #ifdef HAVE_WINDOW_SYSTEM
20401 if (EQ (prop, Qheight))
20402 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20403 if (EQ (prop, Qwidth))
20404 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20405 #else
20406 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20407 return OK_PIXELS (1);
20408 #endif
20409
20410 if (EQ (prop, Qtext))
20411 return OK_PIXELS (width_p
20412 ? window_box_width (it->w, TEXT_AREA)
20413 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20414
20415 if (align_to && *align_to < 0)
20416 {
20417 *res = 0;
20418 if (EQ (prop, Qleft))
20419 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20420 if (EQ (prop, Qright))
20421 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20422 if (EQ (prop, Qcenter))
20423 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20424 + window_box_width (it->w, TEXT_AREA) / 2);
20425 if (EQ (prop, Qleft_fringe))
20426 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20427 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20428 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20429 if (EQ (prop, Qright_fringe))
20430 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20431 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20432 : window_box_right_offset (it->w, TEXT_AREA));
20433 if (EQ (prop, Qleft_margin))
20434 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20435 if (EQ (prop, Qright_margin))
20436 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20437 if (EQ (prop, Qscroll_bar))
20438 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20439 ? 0
20440 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20441 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20442 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20443 : 0)));
20444 }
20445 else
20446 {
20447 if (EQ (prop, Qleft_fringe))
20448 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20449 if (EQ (prop, Qright_fringe))
20450 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20451 if (EQ (prop, Qleft_margin))
20452 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20453 if (EQ (prop, Qright_margin))
20454 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20455 if (EQ (prop, Qscroll_bar))
20456 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20457 }
20458
20459 prop = Fbuffer_local_value (prop, it->w->buffer);
20460 }
20461
20462 if (INTEGERP (prop) || FLOATP (prop))
20463 {
20464 int base_unit = (width_p
20465 ? FRAME_COLUMN_WIDTH (it->f)
20466 : FRAME_LINE_HEIGHT (it->f));
20467 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20468 }
20469
20470 if (CONSP (prop))
20471 {
20472 Lisp_Object car = XCAR (prop);
20473 Lisp_Object cdr = XCDR (prop);
20474
20475 if (SYMBOLP (car))
20476 {
20477 #ifdef HAVE_WINDOW_SYSTEM
20478 if (FRAME_WINDOW_P (it->f)
20479 && valid_image_p (prop))
20480 {
20481 int id = lookup_image (it->f, prop);
20482 struct image *img = IMAGE_FROM_ID (it->f, id);
20483
20484 return OK_PIXELS (width_p ? img->width : img->height);
20485 }
20486 #endif
20487 if (EQ (car, Qplus) || EQ (car, Qminus))
20488 {
20489 int first = 1;
20490 double px;
20491
20492 pixels = 0;
20493 while (CONSP (cdr))
20494 {
20495 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20496 font, width_p, align_to))
20497 return 0;
20498 if (first)
20499 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20500 else
20501 pixels += px;
20502 cdr = XCDR (cdr);
20503 }
20504 if (EQ (car, Qminus))
20505 pixels = -pixels;
20506 return OK_PIXELS (pixels);
20507 }
20508
20509 car = Fbuffer_local_value (car, it->w->buffer);
20510 }
20511
20512 if (INTEGERP (car) || FLOATP (car))
20513 {
20514 double fact;
20515 pixels = XFLOATINT (car);
20516 if (NILP (cdr))
20517 return OK_PIXELS (pixels);
20518 if (calc_pixel_width_or_height (&fact, it, cdr,
20519 font, width_p, align_to))
20520 return OK_PIXELS (pixels * fact);
20521 return 0;
20522 }
20523
20524 return 0;
20525 }
20526
20527 return 0;
20528 }
20529
20530 \f
20531 /***********************************************************************
20532 Glyph Display
20533 ***********************************************************************/
20534
20535 #ifdef HAVE_WINDOW_SYSTEM
20536
20537 #if GLYPH_DEBUG
20538
20539 void
20540 dump_glyph_string (s)
20541 struct glyph_string *s;
20542 {
20543 fprintf (stderr, "glyph string\n");
20544 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20545 s->x, s->y, s->width, s->height);
20546 fprintf (stderr, " ybase = %d\n", s->ybase);
20547 fprintf (stderr, " hl = %d\n", s->hl);
20548 fprintf (stderr, " left overhang = %d, right = %d\n",
20549 s->left_overhang, s->right_overhang);
20550 fprintf (stderr, " nchars = %d\n", s->nchars);
20551 fprintf (stderr, " extends to end of line = %d\n",
20552 s->extends_to_end_of_line_p);
20553 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20554 fprintf (stderr, " bg width = %d\n", s->background_width);
20555 }
20556
20557 #endif /* GLYPH_DEBUG */
20558
20559 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20560 of XChar2b structures for S; it can't be allocated in
20561 init_glyph_string because it must be allocated via `alloca'. W
20562 is the window on which S is drawn. ROW and AREA are the glyph row
20563 and area within the row from which S is constructed. START is the
20564 index of the first glyph structure covered by S. HL is a
20565 face-override for drawing S. */
20566
20567 #ifdef HAVE_NTGUI
20568 #define OPTIONAL_HDC(hdc) HDC hdc,
20569 #define DECLARE_HDC(hdc) HDC hdc;
20570 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20571 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20572 #endif
20573
20574 #ifndef OPTIONAL_HDC
20575 #define OPTIONAL_HDC(hdc)
20576 #define DECLARE_HDC(hdc)
20577 #define ALLOCATE_HDC(hdc, f)
20578 #define RELEASE_HDC(hdc, f)
20579 #endif
20580
20581 static void
20582 init_glyph_string (struct glyph_string *s,
20583 OPTIONAL_HDC (hdc)
20584 XChar2b *char2b, struct window *w, struct glyph_row *row,
20585 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20586 {
20587 memset (s, 0, sizeof *s);
20588 s->w = w;
20589 s->f = XFRAME (w->frame);
20590 #ifdef HAVE_NTGUI
20591 s->hdc = hdc;
20592 #endif
20593 s->display = FRAME_X_DISPLAY (s->f);
20594 s->window = FRAME_X_WINDOW (s->f);
20595 s->char2b = char2b;
20596 s->hl = hl;
20597 s->row = row;
20598 s->area = area;
20599 s->first_glyph = row->glyphs[area] + start;
20600 s->height = row->height;
20601 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20602 s->ybase = s->y + row->ascent;
20603 }
20604
20605
20606 /* Append the list of glyph strings with head H and tail T to the list
20607 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20608
20609 static INLINE void
20610 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20611 struct glyph_string *h, struct glyph_string *t)
20612 {
20613 if (h)
20614 {
20615 if (*head)
20616 (*tail)->next = h;
20617 else
20618 *head = h;
20619 h->prev = *tail;
20620 *tail = t;
20621 }
20622 }
20623
20624
20625 /* Prepend the list of glyph strings with head H and tail T to the
20626 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20627 result. */
20628
20629 static INLINE void
20630 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20631 struct glyph_string *h, struct glyph_string *t)
20632 {
20633 if (h)
20634 {
20635 if (*head)
20636 (*head)->prev = t;
20637 else
20638 *tail = t;
20639 t->next = *head;
20640 *head = h;
20641 }
20642 }
20643
20644
20645 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20646 Set *HEAD and *TAIL to the resulting list. */
20647
20648 static INLINE void
20649 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20650 struct glyph_string *s)
20651 {
20652 s->next = s->prev = NULL;
20653 append_glyph_string_lists (head, tail, s, s);
20654 }
20655
20656
20657 /* Get face and two-byte form of character C in face FACE_ID on frame F.
20658 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
20659 make sure that X resources for the face returned are allocated.
20660 Value is a pointer to a realized face that is ready for display if
20661 DISPLAY_P is non-zero. */
20662
20663 static INLINE struct face *
20664 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20665 XChar2b *char2b, int display_p)
20666 {
20667 struct face *face = FACE_FROM_ID (f, face_id);
20668
20669 if (face->font)
20670 {
20671 unsigned code = face->font->driver->encode_char (face->font, c);
20672
20673 if (code != FONT_INVALID_CODE)
20674 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20675 else
20676 STORE_XCHAR2B (char2b, 0, 0);
20677 }
20678
20679 /* Make sure X resources of the face are allocated. */
20680 #ifdef HAVE_X_WINDOWS
20681 if (display_p)
20682 #endif
20683 {
20684 xassert (face != NULL);
20685 PREPARE_FACE_FOR_DISPLAY (f, face);
20686 }
20687
20688 return face;
20689 }
20690
20691
20692 /* Get face and two-byte form of character glyph GLYPH on frame F.
20693 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20694 a pointer to a realized face that is ready for display. */
20695
20696 static INLINE struct face *
20697 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20698 XChar2b *char2b, int *two_byte_p)
20699 {
20700 struct face *face;
20701
20702 xassert (glyph->type == CHAR_GLYPH);
20703 face = FACE_FROM_ID (f, glyph->face_id);
20704
20705 if (two_byte_p)
20706 *two_byte_p = 0;
20707
20708 if (face->font)
20709 {
20710 unsigned code;
20711
20712 if (CHAR_BYTE8_P (glyph->u.ch))
20713 code = CHAR_TO_BYTE8 (glyph->u.ch);
20714 else
20715 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20716
20717 if (code != FONT_INVALID_CODE)
20718 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20719 else
20720 STORE_XCHAR2B (char2b, 0, 0);
20721 }
20722
20723 /* Make sure X resources of the face are allocated. */
20724 xassert (face != NULL);
20725 PREPARE_FACE_FOR_DISPLAY (f, face);
20726 return face;
20727 }
20728
20729
20730 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20731 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20732
20733 static INLINE int
20734 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20735 {
20736 unsigned code;
20737
20738 if (CHAR_BYTE8_P (c))
20739 code = CHAR_TO_BYTE8 (c);
20740 else
20741 code = font->driver->encode_char (font, c);
20742
20743 if (code == FONT_INVALID_CODE)
20744 return 0;
20745 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20746 return 1;
20747 }
20748
20749
20750 /* Fill glyph string S with composition components specified by S->cmp.
20751
20752 BASE_FACE is the base face of the composition.
20753 S->cmp_from is the index of the first component for S.
20754
20755 OVERLAPS non-zero means S should draw the foreground only, and use
20756 its physical height for clipping. See also draw_glyphs.
20757
20758 Value is the index of a component not in S. */
20759
20760 static int
20761 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20762 int overlaps)
20763 {
20764 int i;
20765 /* For all glyphs of this composition, starting at the offset
20766 S->cmp_from, until we reach the end of the definition or encounter a
20767 glyph that requires the different face, add it to S. */
20768 struct face *face;
20769
20770 xassert (s);
20771
20772 s->for_overlaps = overlaps;
20773 s->face = NULL;
20774 s->font = NULL;
20775 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20776 {
20777 int c = COMPOSITION_GLYPH (s->cmp, i);
20778
20779 if (c != '\t')
20780 {
20781 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20782 -1, Qnil);
20783
20784 face = get_char_face_and_encoding (s->f, c, face_id,
20785 s->char2b + i, 1);
20786 if (face)
20787 {
20788 if (! s->face)
20789 {
20790 s->face = face;
20791 s->font = s->face->font;
20792 }
20793 else if (s->face != face)
20794 break;
20795 }
20796 }
20797 ++s->nchars;
20798 }
20799 s->cmp_to = i;
20800
20801 /* All glyph strings for the same composition has the same width,
20802 i.e. the width set for the first component of the composition. */
20803 s->width = s->first_glyph->pixel_width;
20804
20805 /* If the specified font could not be loaded, use the frame's
20806 default font, but record the fact that we couldn't load it in
20807 the glyph string so that we can draw rectangles for the
20808 characters of the glyph string. */
20809 if (s->font == NULL)
20810 {
20811 s->font_not_found_p = 1;
20812 s->font = FRAME_FONT (s->f);
20813 }
20814
20815 /* Adjust base line for subscript/superscript text. */
20816 s->ybase += s->first_glyph->voffset;
20817
20818 /* This glyph string must always be drawn with 16-bit functions. */
20819 s->two_byte_p = 1;
20820
20821 return s->cmp_to;
20822 }
20823
20824 static int
20825 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20826 int start, int end, int overlaps)
20827 {
20828 struct glyph *glyph, *last;
20829 Lisp_Object lgstring;
20830 int i;
20831
20832 s->for_overlaps = overlaps;
20833 glyph = s->row->glyphs[s->area] + start;
20834 last = s->row->glyphs[s->area] + end;
20835 s->cmp_id = glyph->u.cmp.id;
20836 s->cmp_from = glyph->slice.cmp.from;
20837 s->cmp_to = glyph->slice.cmp.to + 1;
20838 s->face = FACE_FROM_ID (s->f, face_id);
20839 lgstring = composition_gstring_from_id (s->cmp_id);
20840 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20841 glyph++;
20842 while (glyph < last
20843 && glyph->u.cmp.automatic
20844 && glyph->u.cmp.id == s->cmp_id
20845 && s->cmp_to == glyph->slice.cmp.from)
20846 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20847
20848 for (i = s->cmp_from; i < s->cmp_to; i++)
20849 {
20850 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20851 unsigned code = LGLYPH_CODE (lglyph);
20852
20853 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20854 }
20855 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20856 return glyph - s->row->glyphs[s->area];
20857 }
20858
20859
20860 /* Fill glyph string S from a sequence glyphs for glyphless characters.
20861 See the comment of fill_glyph_string for arguments.
20862 Value is the index of the first glyph not in S. */
20863
20864
20865 static int
20866 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
20867 int start, int end, int overlaps)
20868 {
20869 struct glyph *glyph, *last;
20870 int voffset;
20871
20872 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
20873 s->for_overlaps = overlaps;
20874 glyph = s->row->glyphs[s->area] + start;
20875 last = s->row->glyphs[s->area] + end;
20876 voffset = glyph->voffset;
20877 s->face = FACE_FROM_ID (s->f, face_id);
20878 s->font = s->face->font;
20879 s->nchars = 1;
20880 s->width = glyph->pixel_width;
20881 glyph++;
20882 while (glyph < last
20883 && glyph->type == GLYPHLESS_GLYPH
20884 && glyph->voffset == voffset
20885 && glyph->face_id == face_id)
20886 {
20887 s->nchars++;
20888 s->width += glyph->pixel_width;
20889 glyph++;
20890 }
20891 s->ybase += voffset;
20892 return glyph - s->row->glyphs[s->area];
20893 }
20894
20895
20896 /* Fill glyph string S from a sequence of character glyphs.
20897
20898 FACE_ID is the face id of the string. START is the index of the
20899 first glyph to consider, END is the index of the last + 1.
20900 OVERLAPS non-zero means S should draw the foreground only, and use
20901 its physical height for clipping. See also draw_glyphs.
20902
20903 Value is the index of the first glyph not in S. */
20904
20905 static int
20906 fill_glyph_string (struct glyph_string *s, int face_id,
20907 int start, int end, int overlaps)
20908 {
20909 struct glyph *glyph, *last;
20910 int voffset;
20911 int glyph_not_available_p;
20912
20913 xassert (s->f == XFRAME (s->w->frame));
20914 xassert (s->nchars == 0);
20915 xassert (start >= 0 && end > start);
20916
20917 s->for_overlaps = overlaps;
20918 glyph = s->row->glyphs[s->area] + start;
20919 last = s->row->glyphs[s->area] + end;
20920 voffset = glyph->voffset;
20921 s->padding_p = glyph->padding_p;
20922 glyph_not_available_p = glyph->glyph_not_available_p;
20923
20924 while (glyph < last
20925 && glyph->type == CHAR_GLYPH
20926 && glyph->voffset == voffset
20927 /* Same face id implies same font, nowadays. */
20928 && glyph->face_id == face_id
20929 && glyph->glyph_not_available_p == glyph_not_available_p)
20930 {
20931 int two_byte_p;
20932
20933 s->face = get_glyph_face_and_encoding (s->f, glyph,
20934 s->char2b + s->nchars,
20935 &two_byte_p);
20936 s->two_byte_p = two_byte_p;
20937 ++s->nchars;
20938 xassert (s->nchars <= end - start);
20939 s->width += glyph->pixel_width;
20940 if (glyph++->padding_p != s->padding_p)
20941 break;
20942 }
20943
20944 s->font = s->face->font;
20945
20946 /* If the specified font could not be loaded, use the frame's font,
20947 but record the fact that we couldn't load it in
20948 S->font_not_found_p so that we can draw rectangles for the
20949 characters of the glyph string. */
20950 if (s->font == NULL || glyph_not_available_p)
20951 {
20952 s->font_not_found_p = 1;
20953 s->font = FRAME_FONT (s->f);
20954 }
20955
20956 /* Adjust base line for subscript/superscript text. */
20957 s->ybase += voffset;
20958
20959 xassert (s->face && s->face->gc);
20960 return glyph - s->row->glyphs[s->area];
20961 }
20962
20963
20964 /* Fill glyph string S from image glyph S->first_glyph. */
20965
20966 static void
20967 fill_image_glyph_string (struct glyph_string *s)
20968 {
20969 xassert (s->first_glyph->type == IMAGE_GLYPH);
20970 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20971 xassert (s->img);
20972 s->slice = s->first_glyph->slice.img;
20973 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20974 s->font = s->face->font;
20975 s->width = s->first_glyph->pixel_width;
20976
20977 /* Adjust base line for subscript/superscript text. */
20978 s->ybase += s->first_glyph->voffset;
20979 }
20980
20981
20982 /* Fill glyph string S from a sequence of stretch glyphs.
20983
20984 START is the index of the first glyph to consider,
20985 END is the index of the last + 1.
20986
20987 Value is the index of the first glyph not in S. */
20988
20989 static int
20990 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
20991 {
20992 struct glyph *glyph, *last;
20993 int voffset, face_id;
20994
20995 xassert (s->first_glyph->type == STRETCH_GLYPH);
20996
20997 glyph = s->row->glyphs[s->area] + start;
20998 last = s->row->glyphs[s->area] + end;
20999 face_id = glyph->face_id;
21000 s->face = FACE_FROM_ID (s->f, face_id);
21001 s->font = s->face->font;
21002 s->width = glyph->pixel_width;
21003 s->nchars = 1;
21004 voffset = glyph->voffset;
21005
21006 for (++glyph;
21007 (glyph < last
21008 && glyph->type == STRETCH_GLYPH
21009 && glyph->voffset == voffset
21010 && glyph->face_id == face_id);
21011 ++glyph)
21012 s->width += glyph->pixel_width;
21013
21014 /* Adjust base line for subscript/superscript text. */
21015 s->ybase += voffset;
21016
21017 /* The case that face->gc == 0 is handled when drawing the glyph
21018 string by calling PREPARE_FACE_FOR_DISPLAY. */
21019 xassert (s->face);
21020 return glyph - s->row->glyphs[s->area];
21021 }
21022
21023 static struct font_metrics *
21024 get_per_char_metric (struct font *font, XChar2b *char2b)
21025 {
21026 static struct font_metrics metrics;
21027 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21028
21029 if (! font || code == FONT_INVALID_CODE)
21030 return NULL;
21031 font->driver->text_extents (font, &code, 1, &metrics);
21032 return &metrics;
21033 }
21034
21035 /* EXPORT for RIF:
21036 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21037 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21038 assumed to be zero. */
21039
21040 void
21041 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21042 {
21043 *left = *right = 0;
21044
21045 if (glyph->type == CHAR_GLYPH)
21046 {
21047 struct face *face;
21048 XChar2b char2b;
21049 struct font_metrics *pcm;
21050
21051 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21052 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21053 {
21054 if (pcm->rbearing > pcm->width)
21055 *right = pcm->rbearing - pcm->width;
21056 if (pcm->lbearing < 0)
21057 *left = -pcm->lbearing;
21058 }
21059 }
21060 else if (glyph->type == COMPOSITE_GLYPH)
21061 {
21062 if (! glyph->u.cmp.automatic)
21063 {
21064 struct composition *cmp = composition_table[glyph->u.cmp.id];
21065
21066 if (cmp->rbearing > cmp->pixel_width)
21067 *right = cmp->rbearing - cmp->pixel_width;
21068 if (cmp->lbearing < 0)
21069 *left = - cmp->lbearing;
21070 }
21071 else
21072 {
21073 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21074 struct font_metrics metrics;
21075
21076 composition_gstring_width (gstring, glyph->slice.cmp.from,
21077 glyph->slice.cmp.to + 1, &metrics);
21078 if (metrics.rbearing > metrics.width)
21079 *right = metrics.rbearing - metrics.width;
21080 if (metrics.lbearing < 0)
21081 *left = - metrics.lbearing;
21082 }
21083 }
21084 }
21085
21086
21087 /* Return the index of the first glyph preceding glyph string S that
21088 is overwritten by S because of S's left overhang. Value is -1
21089 if no glyphs are overwritten. */
21090
21091 static int
21092 left_overwritten (struct glyph_string *s)
21093 {
21094 int k;
21095
21096 if (s->left_overhang)
21097 {
21098 int x = 0, i;
21099 struct glyph *glyphs = s->row->glyphs[s->area];
21100 int first = s->first_glyph - glyphs;
21101
21102 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21103 x -= glyphs[i].pixel_width;
21104
21105 k = i + 1;
21106 }
21107 else
21108 k = -1;
21109
21110 return k;
21111 }
21112
21113
21114 /* Return the index of the first glyph preceding glyph string S that
21115 is overwriting S because of its right overhang. Value is -1 if no
21116 glyph in front of S overwrites S. */
21117
21118 static int
21119 left_overwriting (struct glyph_string *s)
21120 {
21121 int i, k, x;
21122 struct glyph *glyphs = s->row->glyphs[s->area];
21123 int first = s->first_glyph - glyphs;
21124
21125 k = -1;
21126 x = 0;
21127 for (i = first - 1; i >= 0; --i)
21128 {
21129 int left, right;
21130 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21131 if (x + right > 0)
21132 k = i;
21133 x -= glyphs[i].pixel_width;
21134 }
21135
21136 return k;
21137 }
21138
21139
21140 /* Return the index of the last glyph following glyph string S that is
21141 overwritten by S because of S's right overhang. Value is -1 if
21142 no such glyph is found. */
21143
21144 static int
21145 right_overwritten (struct glyph_string *s)
21146 {
21147 int k = -1;
21148
21149 if (s->right_overhang)
21150 {
21151 int x = 0, i;
21152 struct glyph *glyphs = s->row->glyphs[s->area];
21153 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21154 int end = s->row->used[s->area];
21155
21156 for (i = first; i < end && s->right_overhang > x; ++i)
21157 x += glyphs[i].pixel_width;
21158
21159 k = i;
21160 }
21161
21162 return k;
21163 }
21164
21165
21166 /* Return the index of the last glyph following glyph string S that
21167 overwrites S because of its left overhang. Value is negative
21168 if no such glyph is found. */
21169
21170 static int
21171 right_overwriting (struct glyph_string *s)
21172 {
21173 int i, k, x;
21174 int end = s->row->used[s->area];
21175 struct glyph *glyphs = s->row->glyphs[s->area];
21176 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21177
21178 k = -1;
21179 x = 0;
21180 for (i = first; i < end; ++i)
21181 {
21182 int left, right;
21183 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21184 if (x - left < 0)
21185 k = i;
21186 x += glyphs[i].pixel_width;
21187 }
21188
21189 return k;
21190 }
21191
21192
21193 /* Set background width of glyph string S. START is the index of the
21194 first glyph following S. LAST_X is the right-most x-position + 1
21195 in the drawing area. */
21196
21197 static INLINE void
21198 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21199 {
21200 /* If the face of this glyph string has to be drawn to the end of
21201 the drawing area, set S->extends_to_end_of_line_p. */
21202
21203 if (start == s->row->used[s->area]
21204 && s->area == TEXT_AREA
21205 && ((s->row->fill_line_p
21206 && (s->hl == DRAW_NORMAL_TEXT
21207 || s->hl == DRAW_IMAGE_RAISED
21208 || s->hl == DRAW_IMAGE_SUNKEN))
21209 || s->hl == DRAW_MOUSE_FACE))
21210 s->extends_to_end_of_line_p = 1;
21211
21212 /* If S extends its face to the end of the line, set its
21213 background_width to the distance to the right edge of the drawing
21214 area. */
21215 if (s->extends_to_end_of_line_p)
21216 s->background_width = last_x - s->x + 1;
21217 else
21218 s->background_width = s->width;
21219 }
21220
21221
21222 /* Compute overhangs and x-positions for glyph string S and its
21223 predecessors, or successors. X is the starting x-position for S.
21224 BACKWARD_P non-zero means process predecessors. */
21225
21226 static void
21227 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21228 {
21229 if (backward_p)
21230 {
21231 while (s)
21232 {
21233 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21234 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21235 x -= s->width;
21236 s->x = x;
21237 s = s->prev;
21238 }
21239 }
21240 else
21241 {
21242 while (s)
21243 {
21244 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21245 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21246 s->x = x;
21247 x += s->width;
21248 s = s->next;
21249 }
21250 }
21251 }
21252
21253
21254
21255 /* The following macros are only called from draw_glyphs below.
21256 They reference the following parameters of that function directly:
21257 `w', `row', `area', and `overlap_p'
21258 as well as the following local variables:
21259 `s', `f', and `hdc' (in W32) */
21260
21261 #ifdef HAVE_NTGUI
21262 /* On W32, silently add local `hdc' variable to argument list of
21263 init_glyph_string. */
21264 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21265 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21266 #else
21267 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21268 init_glyph_string (s, char2b, w, row, area, start, hl)
21269 #endif
21270
21271 /* Add a glyph string for a stretch glyph to the list of strings
21272 between HEAD and TAIL. START is the index of the stretch glyph in
21273 row area AREA of glyph row ROW. END is the index of the last glyph
21274 in that glyph row area. X is the current output position assigned
21275 to the new glyph string constructed. HL overrides that face of the
21276 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21277 is the right-most x-position of the drawing area. */
21278
21279 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21280 and below -- keep them on one line. */
21281 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21282 do \
21283 { \
21284 s = (struct glyph_string *) alloca (sizeof *s); \
21285 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21286 START = fill_stretch_glyph_string (s, START, END); \
21287 append_glyph_string (&HEAD, &TAIL, s); \
21288 s->x = (X); \
21289 } \
21290 while (0)
21291
21292
21293 /* Add a glyph string for an image glyph to the list of strings
21294 between HEAD and TAIL. START is the index of the image glyph in
21295 row area AREA of glyph row ROW. END is the index of the last glyph
21296 in that glyph row area. X is the current output position assigned
21297 to the new glyph string constructed. HL overrides that face of the
21298 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21299 is the right-most x-position of the drawing area. */
21300
21301 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21302 do \
21303 { \
21304 s = (struct glyph_string *) alloca (sizeof *s); \
21305 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21306 fill_image_glyph_string (s); \
21307 append_glyph_string (&HEAD, &TAIL, s); \
21308 ++START; \
21309 s->x = (X); \
21310 } \
21311 while (0)
21312
21313
21314 /* Add a glyph string for a sequence of character glyphs to the list
21315 of strings between HEAD and TAIL. START is the index of the first
21316 glyph in row area AREA of glyph row ROW that is part of the new
21317 glyph string. END is the index of the last glyph in that glyph row
21318 area. X is the current output position assigned to the new glyph
21319 string constructed. HL overrides that face of the glyph; e.g. it
21320 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21321 right-most x-position of the drawing area. */
21322
21323 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21324 do \
21325 { \
21326 int face_id; \
21327 XChar2b *char2b; \
21328 \
21329 face_id = (row)->glyphs[area][START].face_id; \
21330 \
21331 s = (struct glyph_string *) alloca (sizeof *s); \
21332 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21333 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21334 append_glyph_string (&HEAD, &TAIL, s); \
21335 s->x = (X); \
21336 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21337 } \
21338 while (0)
21339
21340
21341 /* Add a glyph string for a composite sequence to the list of strings
21342 between HEAD and TAIL. START is the index of the first glyph in
21343 row area AREA of glyph row ROW that is part of the new glyph
21344 string. END is the index of the last glyph in that glyph row area.
21345 X is the current output position assigned to the new glyph string
21346 constructed. HL overrides that face of the glyph; e.g. it is
21347 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21348 x-position of the drawing area. */
21349
21350 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21351 do { \
21352 int face_id = (row)->glyphs[area][START].face_id; \
21353 struct face *base_face = FACE_FROM_ID (f, face_id); \
21354 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21355 struct composition *cmp = composition_table[cmp_id]; \
21356 XChar2b *char2b; \
21357 struct glyph_string *first_s IF_LINT (= NULL); \
21358 int n; \
21359 \
21360 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21361 \
21362 /* Make glyph_strings for each glyph sequence that is drawable by \
21363 the same face, and append them to HEAD/TAIL. */ \
21364 for (n = 0; n < cmp->glyph_len;) \
21365 { \
21366 s = (struct glyph_string *) alloca (sizeof *s); \
21367 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21368 append_glyph_string (&(HEAD), &(TAIL), s); \
21369 s->cmp = cmp; \
21370 s->cmp_from = n; \
21371 s->x = (X); \
21372 if (n == 0) \
21373 first_s = s; \
21374 n = fill_composite_glyph_string (s, base_face, overlaps); \
21375 } \
21376 \
21377 ++START; \
21378 s = first_s; \
21379 } while (0)
21380
21381
21382 /* Add a glyph string for a glyph-string sequence to the list of strings
21383 between HEAD and TAIL. */
21384
21385 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21386 do { \
21387 int face_id; \
21388 XChar2b *char2b; \
21389 Lisp_Object gstring; \
21390 \
21391 face_id = (row)->glyphs[area][START].face_id; \
21392 gstring = (composition_gstring_from_id \
21393 ((row)->glyphs[area][START].u.cmp.id)); \
21394 s = (struct glyph_string *) alloca (sizeof *s); \
21395 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21396 * LGSTRING_GLYPH_LEN (gstring)); \
21397 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21398 append_glyph_string (&(HEAD), &(TAIL), s); \
21399 s->x = (X); \
21400 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21401 } while (0)
21402
21403
21404 /* Add a glyph string for a sequence of glyphless character's glyphs
21405 to the list of strings between HEAD and TAIL. The meanings of
21406 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21407
21408 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21409 do \
21410 { \
21411 int face_id; \
21412 \
21413 face_id = (row)->glyphs[area][START].face_id; \
21414 \
21415 s = (struct glyph_string *) alloca (sizeof *s); \
21416 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21417 append_glyph_string (&HEAD, &TAIL, s); \
21418 s->x = (X); \
21419 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21420 overlaps); \
21421 } \
21422 while (0)
21423
21424
21425 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21426 of AREA of glyph row ROW on window W between indices START and END.
21427 HL overrides the face for drawing glyph strings, e.g. it is
21428 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21429 x-positions of the drawing area.
21430
21431 This is an ugly monster macro construct because we must use alloca
21432 to allocate glyph strings (because draw_glyphs can be called
21433 asynchronously). */
21434
21435 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21436 do \
21437 { \
21438 HEAD = TAIL = NULL; \
21439 while (START < END) \
21440 { \
21441 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21442 switch (first_glyph->type) \
21443 { \
21444 case CHAR_GLYPH: \
21445 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21446 HL, X, LAST_X); \
21447 break; \
21448 \
21449 case COMPOSITE_GLYPH: \
21450 if (first_glyph->u.cmp.automatic) \
21451 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21452 HL, X, LAST_X); \
21453 else \
21454 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21455 HL, X, LAST_X); \
21456 break; \
21457 \
21458 case STRETCH_GLYPH: \
21459 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21460 HL, X, LAST_X); \
21461 break; \
21462 \
21463 case IMAGE_GLYPH: \
21464 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21465 HL, X, LAST_X); \
21466 break; \
21467 \
21468 case GLYPHLESS_GLYPH: \
21469 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21470 HL, X, LAST_X); \
21471 break; \
21472 \
21473 default: \
21474 abort (); \
21475 } \
21476 \
21477 if (s) \
21478 { \
21479 set_glyph_string_background_width (s, START, LAST_X); \
21480 (X) += s->width; \
21481 } \
21482 } \
21483 } while (0)
21484
21485
21486 /* Draw glyphs between START and END in AREA of ROW on window W,
21487 starting at x-position X. X is relative to AREA in W. HL is a
21488 face-override with the following meaning:
21489
21490 DRAW_NORMAL_TEXT draw normally
21491 DRAW_CURSOR draw in cursor face
21492 DRAW_MOUSE_FACE draw in mouse face.
21493 DRAW_INVERSE_VIDEO draw in mode line face
21494 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21495 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21496
21497 If OVERLAPS is non-zero, draw only the foreground of characters and
21498 clip to the physical height of ROW. Non-zero value also defines
21499 the overlapping part to be drawn:
21500
21501 OVERLAPS_PRED overlap with preceding rows
21502 OVERLAPS_SUCC overlap with succeeding rows
21503 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21504 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21505
21506 Value is the x-position reached, relative to AREA of W. */
21507
21508 static int
21509 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21510 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21511 enum draw_glyphs_face hl, int overlaps)
21512 {
21513 struct glyph_string *head, *tail;
21514 struct glyph_string *s;
21515 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21516 int i, j, x_reached, last_x, area_left = 0;
21517 struct frame *f = XFRAME (WINDOW_FRAME (w));
21518 DECLARE_HDC (hdc);
21519
21520 ALLOCATE_HDC (hdc, f);
21521
21522 /* Let's rather be paranoid than getting a SEGV. */
21523 end = min (end, row->used[area]);
21524 start = max (0, start);
21525 start = min (end, start);
21526
21527 /* Translate X to frame coordinates. Set last_x to the right
21528 end of the drawing area. */
21529 if (row->full_width_p)
21530 {
21531 /* X is relative to the left edge of W, without scroll bars
21532 or fringes. */
21533 area_left = WINDOW_LEFT_EDGE_X (w);
21534 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21535 }
21536 else
21537 {
21538 area_left = window_box_left (w, area);
21539 last_x = area_left + window_box_width (w, area);
21540 }
21541 x += area_left;
21542
21543 /* Build a doubly-linked list of glyph_string structures between
21544 head and tail from what we have to draw. Note that the macro
21545 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21546 the reason we use a separate variable `i'. */
21547 i = start;
21548 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21549 if (tail)
21550 x_reached = tail->x + tail->background_width;
21551 else
21552 x_reached = x;
21553
21554 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21555 the row, redraw some glyphs in front or following the glyph
21556 strings built above. */
21557 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21558 {
21559 struct glyph_string *h, *t;
21560 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21561 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
21562 int check_mouse_face = 0;
21563 int dummy_x = 0;
21564
21565 /* If mouse highlighting is on, we may need to draw adjacent
21566 glyphs using mouse-face highlighting. */
21567 if (area == TEXT_AREA && row->mouse_face_p)
21568 {
21569 struct glyph_row *mouse_beg_row, *mouse_end_row;
21570
21571 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21572 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21573
21574 if (row >= mouse_beg_row && row <= mouse_end_row)
21575 {
21576 check_mouse_face = 1;
21577 mouse_beg_col = (row == mouse_beg_row)
21578 ? hlinfo->mouse_face_beg_col : 0;
21579 mouse_end_col = (row == mouse_end_row)
21580 ? hlinfo->mouse_face_end_col
21581 : row->used[TEXT_AREA];
21582 }
21583 }
21584
21585 /* Compute overhangs for all glyph strings. */
21586 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21587 for (s = head; s; s = s->next)
21588 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21589
21590 /* Prepend glyph strings for glyphs in front of the first glyph
21591 string that are overwritten because of the first glyph
21592 string's left overhang. The background of all strings
21593 prepended must be drawn because the first glyph string
21594 draws over it. */
21595 i = left_overwritten (head);
21596 if (i >= 0)
21597 {
21598 enum draw_glyphs_face overlap_hl;
21599
21600 /* If this row contains mouse highlighting, attempt to draw
21601 the overlapped glyphs with the correct highlight. This
21602 code fails if the overlap encompasses more than one glyph
21603 and mouse-highlight spans only some of these glyphs.
21604 However, making it work perfectly involves a lot more
21605 code, and I don't know if the pathological case occurs in
21606 practice, so we'll stick to this for now. --- cyd */
21607 if (check_mouse_face
21608 && mouse_beg_col < start && mouse_end_col > i)
21609 overlap_hl = DRAW_MOUSE_FACE;
21610 else
21611 overlap_hl = DRAW_NORMAL_TEXT;
21612
21613 j = i;
21614 BUILD_GLYPH_STRINGS (j, start, h, t,
21615 overlap_hl, dummy_x, last_x);
21616 start = i;
21617 compute_overhangs_and_x (t, head->x, 1);
21618 prepend_glyph_string_lists (&head, &tail, h, t);
21619 clip_head = head;
21620 }
21621
21622 /* Prepend glyph strings for glyphs in front of the first glyph
21623 string that overwrite that glyph string because of their
21624 right overhang. For these strings, only the foreground must
21625 be drawn, because it draws over the glyph string at `head'.
21626 The background must not be drawn because this would overwrite
21627 right overhangs of preceding glyphs for which no glyph
21628 strings exist. */
21629 i = left_overwriting (head);
21630 if (i >= 0)
21631 {
21632 enum draw_glyphs_face overlap_hl;
21633
21634 if (check_mouse_face
21635 && mouse_beg_col < start && mouse_end_col > i)
21636 overlap_hl = DRAW_MOUSE_FACE;
21637 else
21638 overlap_hl = DRAW_NORMAL_TEXT;
21639
21640 clip_head = head;
21641 BUILD_GLYPH_STRINGS (i, start, h, t,
21642 overlap_hl, dummy_x, last_x);
21643 for (s = h; s; s = s->next)
21644 s->background_filled_p = 1;
21645 compute_overhangs_and_x (t, head->x, 1);
21646 prepend_glyph_string_lists (&head, &tail, h, t);
21647 }
21648
21649 /* Append glyphs strings for glyphs following the last glyph
21650 string tail that are overwritten by tail. The background of
21651 these strings has to be drawn because tail's foreground draws
21652 over it. */
21653 i = right_overwritten (tail);
21654 if (i >= 0)
21655 {
21656 enum draw_glyphs_face overlap_hl;
21657
21658 if (check_mouse_face
21659 && mouse_beg_col < i && mouse_end_col > end)
21660 overlap_hl = DRAW_MOUSE_FACE;
21661 else
21662 overlap_hl = DRAW_NORMAL_TEXT;
21663
21664 BUILD_GLYPH_STRINGS (end, i, h, t,
21665 overlap_hl, x, last_x);
21666 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21667 we don't have `end = i;' here. */
21668 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21669 append_glyph_string_lists (&head, &tail, h, t);
21670 clip_tail = tail;
21671 }
21672
21673 /* Append glyph strings for glyphs following the last glyph
21674 string tail that overwrite tail. The foreground of such
21675 glyphs has to be drawn because it writes into the background
21676 of tail. The background must not be drawn because it could
21677 paint over the foreground of following glyphs. */
21678 i = right_overwriting (tail);
21679 if (i >= 0)
21680 {
21681 enum draw_glyphs_face overlap_hl;
21682 if (check_mouse_face
21683 && mouse_beg_col < i && mouse_end_col > end)
21684 overlap_hl = DRAW_MOUSE_FACE;
21685 else
21686 overlap_hl = DRAW_NORMAL_TEXT;
21687
21688 clip_tail = tail;
21689 i++; /* We must include the Ith glyph. */
21690 BUILD_GLYPH_STRINGS (end, i, h, t,
21691 overlap_hl, x, last_x);
21692 for (s = h; s; s = s->next)
21693 s->background_filled_p = 1;
21694 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21695 append_glyph_string_lists (&head, &tail, h, t);
21696 }
21697 if (clip_head || clip_tail)
21698 for (s = head; s; s = s->next)
21699 {
21700 s->clip_head = clip_head;
21701 s->clip_tail = clip_tail;
21702 }
21703 }
21704
21705 /* Draw all strings. */
21706 for (s = head; s; s = s->next)
21707 FRAME_RIF (f)->draw_glyph_string (s);
21708
21709 #ifndef HAVE_NS
21710 /* When focus a sole frame and move horizontally, this sets on_p to 0
21711 causing a failure to erase prev cursor position. */
21712 if (area == TEXT_AREA
21713 && !row->full_width_p
21714 /* When drawing overlapping rows, only the glyph strings'
21715 foreground is drawn, which doesn't erase a cursor
21716 completely. */
21717 && !overlaps)
21718 {
21719 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21720 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21721 : (tail ? tail->x + tail->background_width : x));
21722 x0 -= area_left;
21723 x1 -= area_left;
21724
21725 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21726 row->y, MATRIX_ROW_BOTTOM_Y (row));
21727 }
21728 #endif
21729
21730 /* Value is the x-position up to which drawn, relative to AREA of W.
21731 This doesn't include parts drawn because of overhangs. */
21732 if (row->full_width_p)
21733 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21734 else
21735 x_reached -= area_left;
21736
21737 RELEASE_HDC (hdc, f);
21738
21739 return x_reached;
21740 }
21741
21742 /* Expand row matrix if too narrow. Don't expand if area
21743 is not present. */
21744
21745 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21746 { \
21747 if (!fonts_changed_p \
21748 && (it->glyph_row->glyphs[area] \
21749 < it->glyph_row->glyphs[area + 1])) \
21750 { \
21751 it->w->ncols_scale_factor++; \
21752 fonts_changed_p = 1; \
21753 } \
21754 }
21755
21756 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21757 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21758
21759 static INLINE void
21760 append_glyph (struct it *it)
21761 {
21762 struct glyph *glyph;
21763 enum glyph_row_area area = it->area;
21764
21765 xassert (it->glyph_row);
21766 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21767
21768 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21769 if (glyph < it->glyph_row->glyphs[area + 1])
21770 {
21771 /* If the glyph row is reversed, we need to prepend the glyph
21772 rather than append it. */
21773 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21774 {
21775 struct glyph *g;
21776
21777 /* Make room for the additional glyph. */
21778 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21779 g[1] = *g;
21780 glyph = it->glyph_row->glyphs[area];
21781 }
21782 glyph->charpos = CHARPOS (it->position);
21783 glyph->object = it->object;
21784 if (it->pixel_width > 0)
21785 {
21786 glyph->pixel_width = it->pixel_width;
21787 glyph->padding_p = 0;
21788 }
21789 else
21790 {
21791 /* Assure at least 1-pixel width. Otherwise, cursor can't
21792 be displayed correctly. */
21793 glyph->pixel_width = 1;
21794 glyph->padding_p = 1;
21795 }
21796 glyph->ascent = it->ascent;
21797 glyph->descent = it->descent;
21798 glyph->voffset = it->voffset;
21799 glyph->type = CHAR_GLYPH;
21800 glyph->avoid_cursor_p = it->avoid_cursor_p;
21801 glyph->multibyte_p = it->multibyte_p;
21802 glyph->left_box_line_p = it->start_of_box_run_p;
21803 glyph->right_box_line_p = it->end_of_box_run_p;
21804 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21805 || it->phys_descent > it->descent);
21806 glyph->glyph_not_available_p = it->glyph_not_available_p;
21807 glyph->face_id = it->face_id;
21808 glyph->u.ch = it->char_to_display;
21809 glyph->slice.img = null_glyph_slice;
21810 glyph->font_type = FONT_TYPE_UNKNOWN;
21811 if (it->bidi_p)
21812 {
21813 glyph->resolved_level = it->bidi_it.resolved_level;
21814 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21815 abort ();
21816 glyph->bidi_type = it->bidi_it.type;
21817 }
21818 else
21819 {
21820 glyph->resolved_level = 0;
21821 glyph->bidi_type = UNKNOWN_BT;
21822 }
21823 ++it->glyph_row->used[area];
21824 }
21825 else
21826 IT_EXPAND_MATRIX_WIDTH (it, area);
21827 }
21828
21829 /* Store one glyph for the composition IT->cmp_it.id in
21830 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21831 non-null. */
21832
21833 static INLINE void
21834 append_composite_glyph (struct it *it)
21835 {
21836 struct glyph *glyph;
21837 enum glyph_row_area area = it->area;
21838
21839 xassert (it->glyph_row);
21840
21841 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21842 if (glyph < it->glyph_row->glyphs[area + 1])
21843 {
21844 /* If the glyph row is reversed, we need to prepend the glyph
21845 rather than append it. */
21846 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21847 {
21848 struct glyph *g;
21849
21850 /* Make room for the new glyph. */
21851 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21852 g[1] = *g;
21853 glyph = it->glyph_row->glyphs[it->area];
21854 }
21855 glyph->charpos = it->cmp_it.charpos;
21856 glyph->object = it->object;
21857 glyph->pixel_width = it->pixel_width;
21858 glyph->ascent = it->ascent;
21859 glyph->descent = it->descent;
21860 glyph->voffset = it->voffset;
21861 glyph->type = COMPOSITE_GLYPH;
21862 if (it->cmp_it.ch < 0)
21863 {
21864 glyph->u.cmp.automatic = 0;
21865 glyph->u.cmp.id = it->cmp_it.id;
21866 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21867 }
21868 else
21869 {
21870 glyph->u.cmp.automatic = 1;
21871 glyph->u.cmp.id = it->cmp_it.id;
21872 glyph->slice.cmp.from = it->cmp_it.from;
21873 glyph->slice.cmp.to = it->cmp_it.to - 1;
21874 }
21875 glyph->avoid_cursor_p = it->avoid_cursor_p;
21876 glyph->multibyte_p = it->multibyte_p;
21877 glyph->left_box_line_p = it->start_of_box_run_p;
21878 glyph->right_box_line_p = it->end_of_box_run_p;
21879 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21880 || it->phys_descent > it->descent);
21881 glyph->padding_p = 0;
21882 glyph->glyph_not_available_p = 0;
21883 glyph->face_id = it->face_id;
21884 glyph->font_type = FONT_TYPE_UNKNOWN;
21885 if (it->bidi_p)
21886 {
21887 glyph->resolved_level = it->bidi_it.resolved_level;
21888 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21889 abort ();
21890 glyph->bidi_type = it->bidi_it.type;
21891 }
21892 ++it->glyph_row->used[area];
21893 }
21894 else
21895 IT_EXPAND_MATRIX_WIDTH (it, area);
21896 }
21897
21898
21899 /* Change IT->ascent and IT->height according to the setting of
21900 IT->voffset. */
21901
21902 static INLINE void
21903 take_vertical_position_into_account (struct it *it)
21904 {
21905 if (it->voffset)
21906 {
21907 if (it->voffset < 0)
21908 /* Increase the ascent so that we can display the text higher
21909 in the line. */
21910 it->ascent -= it->voffset;
21911 else
21912 /* Increase the descent so that we can display the text lower
21913 in the line. */
21914 it->descent += it->voffset;
21915 }
21916 }
21917
21918
21919 /* Produce glyphs/get display metrics for the image IT is loaded with.
21920 See the description of struct display_iterator in dispextern.h for
21921 an overview of struct display_iterator. */
21922
21923 static void
21924 produce_image_glyph (struct it *it)
21925 {
21926 struct image *img;
21927 struct face *face;
21928 int glyph_ascent, crop;
21929 struct glyph_slice slice;
21930
21931 xassert (it->what == IT_IMAGE);
21932
21933 face = FACE_FROM_ID (it->f, it->face_id);
21934 xassert (face);
21935 /* Make sure X resources of the face is loaded. */
21936 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21937
21938 if (it->image_id < 0)
21939 {
21940 /* Fringe bitmap. */
21941 it->ascent = it->phys_ascent = 0;
21942 it->descent = it->phys_descent = 0;
21943 it->pixel_width = 0;
21944 it->nglyphs = 0;
21945 return;
21946 }
21947
21948 img = IMAGE_FROM_ID (it->f, it->image_id);
21949 xassert (img);
21950 /* Make sure X resources of the image is loaded. */
21951 prepare_image_for_display (it->f, img);
21952
21953 slice.x = slice.y = 0;
21954 slice.width = img->width;
21955 slice.height = img->height;
21956
21957 if (INTEGERP (it->slice.x))
21958 slice.x = XINT (it->slice.x);
21959 else if (FLOATP (it->slice.x))
21960 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21961
21962 if (INTEGERP (it->slice.y))
21963 slice.y = XINT (it->slice.y);
21964 else if (FLOATP (it->slice.y))
21965 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21966
21967 if (INTEGERP (it->slice.width))
21968 slice.width = XINT (it->slice.width);
21969 else if (FLOATP (it->slice.width))
21970 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21971
21972 if (INTEGERP (it->slice.height))
21973 slice.height = XINT (it->slice.height);
21974 else if (FLOATP (it->slice.height))
21975 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21976
21977 if (slice.x >= img->width)
21978 slice.x = img->width;
21979 if (slice.y >= img->height)
21980 slice.y = img->height;
21981 if (slice.x + slice.width >= img->width)
21982 slice.width = img->width - slice.x;
21983 if (slice.y + slice.height > img->height)
21984 slice.height = img->height - slice.y;
21985
21986 if (slice.width == 0 || slice.height == 0)
21987 return;
21988
21989 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21990
21991 it->descent = slice.height - glyph_ascent;
21992 if (slice.y == 0)
21993 it->descent += img->vmargin;
21994 if (slice.y + slice.height == img->height)
21995 it->descent += img->vmargin;
21996 it->phys_descent = it->descent;
21997
21998 it->pixel_width = slice.width;
21999 if (slice.x == 0)
22000 it->pixel_width += img->hmargin;
22001 if (slice.x + slice.width == img->width)
22002 it->pixel_width += img->hmargin;
22003
22004 /* It's quite possible for images to have an ascent greater than
22005 their height, so don't get confused in that case. */
22006 if (it->descent < 0)
22007 it->descent = 0;
22008
22009 it->nglyphs = 1;
22010
22011 if (face->box != FACE_NO_BOX)
22012 {
22013 if (face->box_line_width > 0)
22014 {
22015 if (slice.y == 0)
22016 it->ascent += face->box_line_width;
22017 if (slice.y + slice.height == img->height)
22018 it->descent += face->box_line_width;
22019 }
22020
22021 if (it->start_of_box_run_p && slice.x == 0)
22022 it->pixel_width += eabs (face->box_line_width);
22023 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22024 it->pixel_width += eabs (face->box_line_width);
22025 }
22026
22027 take_vertical_position_into_account (it);
22028
22029 /* Automatically crop wide image glyphs at right edge so we can
22030 draw the cursor on same display row. */
22031 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22032 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22033 {
22034 it->pixel_width -= crop;
22035 slice.width -= crop;
22036 }
22037
22038 if (it->glyph_row)
22039 {
22040 struct glyph *glyph;
22041 enum glyph_row_area area = it->area;
22042
22043 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22044 if (glyph < it->glyph_row->glyphs[area + 1])
22045 {
22046 glyph->charpos = CHARPOS (it->position);
22047 glyph->object = it->object;
22048 glyph->pixel_width = it->pixel_width;
22049 glyph->ascent = glyph_ascent;
22050 glyph->descent = it->descent;
22051 glyph->voffset = it->voffset;
22052 glyph->type = IMAGE_GLYPH;
22053 glyph->avoid_cursor_p = it->avoid_cursor_p;
22054 glyph->multibyte_p = it->multibyte_p;
22055 glyph->left_box_line_p = it->start_of_box_run_p;
22056 glyph->right_box_line_p = it->end_of_box_run_p;
22057 glyph->overlaps_vertically_p = 0;
22058 glyph->padding_p = 0;
22059 glyph->glyph_not_available_p = 0;
22060 glyph->face_id = it->face_id;
22061 glyph->u.img_id = img->id;
22062 glyph->slice.img = slice;
22063 glyph->font_type = FONT_TYPE_UNKNOWN;
22064 if (it->bidi_p)
22065 {
22066 glyph->resolved_level = it->bidi_it.resolved_level;
22067 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22068 abort ();
22069 glyph->bidi_type = it->bidi_it.type;
22070 }
22071 ++it->glyph_row->used[area];
22072 }
22073 else
22074 IT_EXPAND_MATRIX_WIDTH (it, area);
22075 }
22076 }
22077
22078
22079 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22080 of the glyph, WIDTH and HEIGHT are the width and height of the
22081 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22082
22083 static void
22084 append_stretch_glyph (struct it *it, Lisp_Object object,
22085 int width, int height, int ascent)
22086 {
22087 struct glyph *glyph;
22088 enum glyph_row_area area = it->area;
22089
22090 xassert (ascent >= 0 && ascent <= height);
22091
22092 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22093 if (glyph < it->glyph_row->glyphs[area + 1])
22094 {
22095 /* If the glyph row is reversed, we need to prepend the glyph
22096 rather than append it. */
22097 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22098 {
22099 struct glyph *g;
22100
22101 /* Make room for the additional glyph. */
22102 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22103 g[1] = *g;
22104 glyph = it->glyph_row->glyphs[area];
22105 }
22106 glyph->charpos = CHARPOS (it->position);
22107 glyph->object = object;
22108 glyph->pixel_width = width;
22109 glyph->ascent = ascent;
22110 glyph->descent = height - ascent;
22111 glyph->voffset = it->voffset;
22112 glyph->type = STRETCH_GLYPH;
22113 glyph->avoid_cursor_p = it->avoid_cursor_p;
22114 glyph->multibyte_p = it->multibyte_p;
22115 glyph->left_box_line_p = it->start_of_box_run_p;
22116 glyph->right_box_line_p = it->end_of_box_run_p;
22117 glyph->overlaps_vertically_p = 0;
22118 glyph->padding_p = 0;
22119 glyph->glyph_not_available_p = 0;
22120 glyph->face_id = it->face_id;
22121 glyph->u.stretch.ascent = ascent;
22122 glyph->u.stretch.height = height;
22123 glyph->slice.img = null_glyph_slice;
22124 glyph->font_type = FONT_TYPE_UNKNOWN;
22125 if (it->bidi_p)
22126 {
22127 glyph->resolved_level = it->bidi_it.resolved_level;
22128 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22129 abort ();
22130 glyph->bidi_type = it->bidi_it.type;
22131 }
22132 else
22133 {
22134 glyph->resolved_level = 0;
22135 glyph->bidi_type = UNKNOWN_BT;
22136 }
22137 ++it->glyph_row->used[area];
22138 }
22139 else
22140 IT_EXPAND_MATRIX_WIDTH (it, area);
22141 }
22142
22143
22144 /* Produce a stretch glyph for iterator IT. IT->object is the value
22145 of the glyph property displayed. The value must be a list
22146 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22147 being recognized:
22148
22149 1. `:width WIDTH' specifies that the space should be WIDTH *
22150 canonical char width wide. WIDTH may be an integer or floating
22151 point number.
22152
22153 2. `:relative-width FACTOR' specifies that the width of the stretch
22154 should be computed from the width of the first character having the
22155 `glyph' property, and should be FACTOR times that width.
22156
22157 3. `:align-to HPOS' specifies that the space should be wide enough
22158 to reach HPOS, a value in canonical character units.
22159
22160 Exactly one of the above pairs must be present.
22161
22162 4. `:height HEIGHT' specifies that the height of the stretch produced
22163 should be HEIGHT, measured in canonical character units.
22164
22165 5. `:relative-height FACTOR' specifies that the height of the
22166 stretch should be FACTOR times the height of the characters having
22167 the glyph property.
22168
22169 Either none or exactly one of 4 or 5 must be present.
22170
22171 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22172 of the stretch should be used for the ascent of the stretch.
22173 ASCENT must be in the range 0 <= ASCENT <= 100. */
22174
22175 static void
22176 produce_stretch_glyph (struct it *it)
22177 {
22178 /* (space :width WIDTH :height HEIGHT ...) */
22179 Lisp_Object prop, plist;
22180 int width = 0, height = 0, align_to = -1;
22181 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22182 int ascent = 0;
22183 double tem;
22184 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22185 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22186
22187 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22188
22189 /* List should start with `space'. */
22190 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22191 plist = XCDR (it->object);
22192
22193 /* Compute the width of the stretch. */
22194 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22195 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22196 {
22197 /* Absolute width `:width WIDTH' specified and valid. */
22198 zero_width_ok_p = 1;
22199 width = (int)tem;
22200 }
22201 else if (prop = Fplist_get (plist, QCrelative_width),
22202 NUMVAL (prop) > 0)
22203 {
22204 /* Relative width `:relative-width FACTOR' specified and valid.
22205 Compute the width of the characters having the `glyph'
22206 property. */
22207 struct it it2;
22208 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22209
22210 it2 = *it;
22211 if (it->multibyte_p)
22212 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22213 else
22214 {
22215 it2.c = it2.char_to_display = *p, it2.len = 1;
22216 if (! ASCII_CHAR_P (it2.c))
22217 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22218 }
22219
22220 it2.glyph_row = NULL;
22221 it2.what = IT_CHARACTER;
22222 x_produce_glyphs (&it2);
22223 width = NUMVAL (prop) * it2.pixel_width;
22224 }
22225 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22226 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22227 {
22228 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22229 align_to = (align_to < 0
22230 ? 0
22231 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22232 else if (align_to < 0)
22233 align_to = window_box_left_offset (it->w, TEXT_AREA);
22234 width = max (0, (int)tem + align_to - it->current_x);
22235 zero_width_ok_p = 1;
22236 }
22237 else
22238 /* Nothing specified -> width defaults to canonical char width. */
22239 width = FRAME_COLUMN_WIDTH (it->f);
22240
22241 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22242 width = 1;
22243
22244 /* Compute height. */
22245 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22246 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22247 {
22248 height = (int)tem;
22249 zero_height_ok_p = 1;
22250 }
22251 else if (prop = Fplist_get (plist, QCrelative_height),
22252 NUMVAL (prop) > 0)
22253 height = FONT_HEIGHT (font) * NUMVAL (prop);
22254 else
22255 height = FONT_HEIGHT (font);
22256
22257 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22258 height = 1;
22259
22260 /* Compute percentage of height used for ascent. If
22261 `:ascent ASCENT' is present and valid, use that. Otherwise,
22262 derive the ascent from the font in use. */
22263 if (prop = Fplist_get (plist, QCascent),
22264 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22265 ascent = height * NUMVAL (prop) / 100.0;
22266 else if (!NILP (prop)
22267 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22268 ascent = min (max (0, (int)tem), height);
22269 else
22270 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22271
22272 if (width > 0 && it->line_wrap != TRUNCATE
22273 && it->current_x + width > it->last_visible_x)
22274 width = it->last_visible_x - it->current_x - 1;
22275
22276 if (width > 0 && height > 0 && it->glyph_row)
22277 {
22278 Lisp_Object object = it->stack[it->sp - 1].string;
22279 if (!STRINGP (object))
22280 object = it->w->buffer;
22281 append_stretch_glyph (it, object, width, height, ascent);
22282 }
22283
22284 it->pixel_width = width;
22285 it->ascent = it->phys_ascent = ascent;
22286 it->descent = it->phys_descent = height - it->ascent;
22287 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22288
22289 take_vertical_position_into_account (it);
22290 }
22291
22292 /* Calculate line-height and line-spacing properties.
22293 An integer value specifies explicit pixel value.
22294 A float value specifies relative value to current face height.
22295 A cons (float . face-name) specifies relative value to
22296 height of specified face font.
22297
22298 Returns height in pixels, or nil. */
22299
22300
22301 static Lisp_Object
22302 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22303 int boff, int override)
22304 {
22305 Lisp_Object face_name = Qnil;
22306 int ascent, descent, height;
22307
22308 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22309 return val;
22310
22311 if (CONSP (val))
22312 {
22313 face_name = XCAR (val);
22314 val = XCDR (val);
22315 if (!NUMBERP (val))
22316 val = make_number (1);
22317 if (NILP (face_name))
22318 {
22319 height = it->ascent + it->descent;
22320 goto scale;
22321 }
22322 }
22323
22324 if (NILP (face_name))
22325 {
22326 font = FRAME_FONT (it->f);
22327 boff = FRAME_BASELINE_OFFSET (it->f);
22328 }
22329 else if (EQ (face_name, Qt))
22330 {
22331 override = 0;
22332 }
22333 else
22334 {
22335 int face_id;
22336 struct face *face;
22337
22338 face_id = lookup_named_face (it->f, face_name, 0);
22339 if (face_id < 0)
22340 return make_number (-1);
22341
22342 face = FACE_FROM_ID (it->f, face_id);
22343 font = face->font;
22344 if (font == NULL)
22345 return make_number (-1);
22346 boff = font->baseline_offset;
22347 if (font->vertical_centering)
22348 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22349 }
22350
22351 ascent = FONT_BASE (font) + boff;
22352 descent = FONT_DESCENT (font) - boff;
22353
22354 if (override)
22355 {
22356 it->override_ascent = ascent;
22357 it->override_descent = descent;
22358 it->override_boff = boff;
22359 }
22360
22361 height = ascent + descent;
22362
22363 scale:
22364 if (FLOATP (val))
22365 height = (int)(XFLOAT_DATA (val) * height);
22366 else if (INTEGERP (val))
22367 height *= XINT (val);
22368
22369 return make_number (height);
22370 }
22371
22372
22373 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22374 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22375 and only if this is for a character for which no font was found.
22376
22377 If the display method (it->glyphless_method) is
22378 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22379 length of the acronym or the hexadecimal string, UPPER_XOFF and
22380 UPPER_YOFF are pixel offsets for the upper part of the string,
22381 LOWER_XOFF and LOWER_YOFF are for the lower part.
22382
22383 For the other display methods, LEN through LOWER_YOFF are zero. */
22384
22385 static void
22386 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22387 short upper_xoff, short upper_yoff,
22388 short lower_xoff, short lower_yoff)
22389 {
22390 struct glyph *glyph;
22391 enum glyph_row_area area = it->area;
22392
22393 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22394 if (glyph < it->glyph_row->glyphs[area + 1])
22395 {
22396 /* If the glyph row is reversed, we need to prepend the glyph
22397 rather than append it. */
22398 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22399 {
22400 struct glyph *g;
22401
22402 /* Make room for the additional glyph. */
22403 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22404 g[1] = *g;
22405 glyph = it->glyph_row->glyphs[area];
22406 }
22407 glyph->charpos = CHARPOS (it->position);
22408 glyph->object = it->object;
22409 glyph->pixel_width = it->pixel_width;
22410 glyph->ascent = it->ascent;
22411 glyph->descent = it->descent;
22412 glyph->voffset = it->voffset;
22413 glyph->type = GLYPHLESS_GLYPH;
22414 glyph->u.glyphless.method = it->glyphless_method;
22415 glyph->u.glyphless.for_no_font = for_no_font;
22416 glyph->u.glyphless.len = len;
22417 glyph->u.glyphless.ch = it->c;
22418 glyph->slice.glyphless.upper_xoff = upper_xoff;
22419 glyph->slice.glyphless.upper_yoff = upper_yoff;
22420 glyph->slice.glyphless.lower_xoff = lower_xoff;
22421 glyph->slice.glyphless.lower_yoff = lower_yoff;
22422 glyph->avoid_cursor_p = it->avoid_cursor_p;
22423 glyph->multibyte_p = it->multibyte_p;
22424 glyph->left_box_line_p = it->start_of_box_run_p;
22425 glyph->right_box_line_p = it->end_of_box_run_p;
22426 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22427 || it->phys_descent > it->descent);
22428 glyph->padding_p = 0;
22429 glyph->glyph_not_available_p = 0;
22430 glyph->face_id = face_id;
22431 glyph->font_type = FONT_TYPE_UNKNOWN;
22432 if (it->bidi_p)
22433 {
22434 glyph->resolved_level = it->bidi_it.resolved_level;
22435 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22436 abort ();
22437 glyph->bidi_type = it->bidi_it.type;
22438 }
22439 ++it->glyph_row->used[area];
22440 }
22441 else
22442 IT_EXPAND_MATRIX_WIDTH (it, area);
22443 }
22444
22445
22446 /* Produce a glyph for a glyphless character for iterator IT.
22447 IT->glyphless_method specifies which method to use for displaying
22448 the character. See the description of enum
22449 glyphless_display_method in dispextern.h for the detail.
22450
22451 FOR_NO_FONT is nonzero if and only if this is for a character for
22452 which no font was found. ACRONYM, if non-nil, is an acronym string
22453 for the character. */
22454
22455 static void
22456 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22457 {
22458 int face_id;
22459 struct face *face;
22460 struct font *font;
22461 int base_width, base_height, width, height;
22462 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22463 int len;
22464
22465 /* Get the metrics of the base font. We always refer to the current
22466 ASCII face. */
22467 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22468 font = face->font ? face->font : FRAME_FONT (it->f);
22469 it->ascent = FONT_BASE (font) + font->baseline_offset;
22470 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22471 base_height = it->ascent + it->descent;
22472 base_width = font->average_width;
22473
22474 /* Get a face ID for the glyph by utilizing a cache (the same way as
22475 doen for `escape-glyph' in get_next_display_element). */
22476 if (it->f == last_glyphless_glyph_frame
22477 && it->face_id == last_glyphless_glyph_face_id)
22478 {
22479 face_id = last_glyphless_glyph_merged_face_id;
22480 }
22481 else
22482 {
22483 /* Merge the `glyphless-char' face into the current face. */
22484 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22485 last_glyphless_glyph_frame = it->f;
22486 last_glyphless_glyph_face_id = it->face_id;
22487 last_glyphless_glyph_merged_face_id = face_id;
22488 }
22489
22490 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22491 {
22492 it->pixel_width = THIN_SPACE_WIDTH;
22493 len = 0;
22494 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22495 }
22496 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22497 {
22498 width = CHAR_WIDTH (it->c);
22499 if (width == 0)
22500 width = 1;
22501 else if (width > 4)
22502 width = 4;
22503 it->pixel_width = base_width * width;
22504 len = 0;
22505 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22506 }
22507 else
22508 {
22509 char buf[7];
22510 const char *str;
22511 unsigned int code[6];
22512 int upper_len;
22513 int ascent, descent;
22514 struct font_metrics metrics_upper, metrics_lower;
22515
22516 face = FACE_FROM_ID (it->f, face_id);
22517 font = face->font ? face->font : FRAME_FONT (it->f);
22518 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22519
22520 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22521 {
22522 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22523 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22524 if (CONSP (acronym))
22525 acronym = XCAR (acronym);
22526 str = STRINGP (acronym) ? SSDATA (acronym) : "";
22527 }
22528 else
22529 {
22530 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22531 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22532 str = buf;
22533 }
22534 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22535 code[len] = font->driver->encode_char (font, str[len]);
22536 upper_len = (len + 1) / 2;
22537 font->driver->text_extents (font, code, upper_len,
22538 &metrics_upper);
22539 font->driver->text_extents (font, code + upper_len, len - upper_len,
22540 &metrics_lower);
22541
22542
22543
22544 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22545 width = max (metrics_upper.width, metrics_lower.width) + 4;
22546 upper_xoff = upper_yoff = 2; /* the typical case */
22547 if (base_width >= width)
22548 {
22549 /* Align the upper to the left, the lower to the right. */
22550 it->pixel_width = base_width;
22551 lower_xoff = base_width - 2 - metrics_lower.width;
22552 }
22553 else
22554 {
22555 /* Center the shorter one. */
22556 it->pixel_width = width;
22557 if (metrics_upper.width >= metrics_lower.width)
22558 lower_xoff = (width - metrics_lower.width) / 2;
22559 else
22560 {
22561 /* FIXME: This code doesn't look right. It formerly was
22562 missing the "lower_xoff = 0;", which couldn't have
22563 been right since it left lower_xoff uninitialized. */
22564 lower_xoff = 0;
22565 upper_xoff = (width - metrics_upper.width) / 2;
22566 }
22567 }
22568
22569 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22570 top, bottom, and between upper and lower strings. */
22571 height = (metrics_upper.ascent + metrics_upper.descent
22572 + metrics_lower.ascent + metrics_lower.descent) + 5;
22573 /* Center vertically.
22574 H:base_height, D:base_descent
22575 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22576
22577 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22578 descent = D - H/2 + h/2;
22579 lower_yoff = descent - 2 - ld;
22580 upper_yoff = lower_yoff - la - 1 - ud; */
22581 ascent = - (it->descent - (base_height + height + 1) / 2);
22582 descent = it->descent - (base_height - height) / 2;
22583 lower_yoff = descent - 2 - metrics_lower.descent;
22584 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22585 - metrics_upper.descent);
22586 /* Don't make the height shorter than the base height. */
22587 if (height > base_height)
22588 {
22589 it->ascent = ascent;
22590 it->descent = descent;
22591 }
22592 }
22593
22594 it->phys_ascent = it->ascent;
22595 it->phys_descent = it->descent;
22596 if (it->glyph_row)
22597 append_glyphless_glyph (it, face_id, for_no_font, len,
22598 upper_xoff, upper_yoff,
22599 lower_xoff, lower_yoff);
22600 it->nglyphs = 1;
22601 take_vertical_position_into_account (it);
22602 }
22603
22604
22605 /* RIF:
22606 Produce glyphs/get display metrics for the display element IT is
22607 loaded with. See the description of struct it in dispextern.h
22608 for an overview of struct it. */
22609
22610 void
22611 x_produce_glyphs (struct it *it)
22612 {
22613 int extra_line_spacing = it->extra_line_spacing;
22614
22615 it->glyph_not_available_p = 0;
22616
22617 if (it->what == IT_CHARACTER)
22618 {
22619 XChar2b char2b;
22620 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22621 struct font *font = face->font;
22622 struct font_metrics *pcm = NULL;
22623 int boff; /* baseline offset */
22624
22625 if (font == NULL)
22626 {
22627 /* When no suitable font is found, display this character by
22628 the method specified in the first extra slot of
22629 Vglyphless_char_display. */
22630 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22631
22632 xassert (it->what == IT_GLYPHLESS);
22633 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22634 goto done;
22635 }
22636
22637 boff = font->baseline_offset;
22638 if (font->vertical_centering)
22639 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22640
22641 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22642 {
22643 int stretched_p;
22644
22645 it->nglyphs = 1;
22646
22647 if (it->override_ascent >= 0)
22648 {
22649 it->ascent = it->override_ascent;
22650 it->descent = it->override_descent;
22651 boff = it->override_boff;
22652 }
22653 else
22654 {
22655 it->ascent = FONT_BASE (font) + boff;
22656 it->descent = FONT_DESCENT (font) - boff;
22657 }
22658
22659 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22660 {
22661 pcm = get_per_char_metric (font, &char2b);
22662 if (pcm->width == 0
22663 && pcm->rbearing == 0 && pcm->lbearing == 0)
22664 pcm = NULL;
22665 }
22666
22667 if (pcm)
22668 {
22669 it->phys_ascent = pcm->ascent + boff;
22670 it->phys_descent = pcm->descent - boff;
22671 it->pixel_width = pcm->width;
22672 }
22673 else
22674 {
22675 it->glyph_not_available_p = 1;
22676 it->phys_ascent = it->ascent;
22677 it->phys_descent = it->descent;
22678 it->pixel_width = font->space_width;
22679 }
22680
22681 if (it->constrain_row_ascent_descent_p)
22682 {
22683 if (it->descent > it->max_descent)
22684 {
22685 it->ascent += it->descent - it->max_descent;
22686 it->descent = it->max_descent;
22687 }
22688 if (it->ascent > it->max_ascent)
22689 {
22690 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22691 it->ascent = it->max_ascent;
22692 }
22693 it->phys_ascent = min (it->phys_ascent, it->ascent);
22694 it->phys_descent = min (it->phys_descent, it->descent);
22695 extra_line_spacing = 0;
22696 }
22697
22698 /* If this is a space inside a region of text with
22699 `space-width' property, change its width. */
22700 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22701 if (stretched_p)
22702 it->pixel_width *= XFLOATINT (it->space_width);
22703
22704 /* If face has a box, add the box thickness to the character
22705 height. If character has a box line to the left and/or
22706 right, add the box line width to the character's width. */
22707 if (face->box != FACE_NO_BOX)
22708 {
22709 int thick = face->box_line_width;
22710
22711 if (thick > 0)
22712 {
22713 it->ascent += thick;
22714 it->descent += thick;
22715 }
22716 else
22717 thick = -thick;
22718
22719 if (it->start_of_box_run_p)
22720 it->pixel_width += thick;
22721 if (it->end_of_box_run_p)
22722 it->pixel_width += thick;
22723 }
22724
22725 /* If face has an overline, add the height of the overline
22726 (1 pixel) and a 1 pixel margin to the character height. */
22727 if (face->overline_p)
22728 it->ascent += overline_margin;
22729
22730 if (it->constrain_row_ascent_descent_p)
22731 {
22732 if (it->ascent > it->max_ascent)
22733 it->ascent = it->max_ascent;
22734 if (it->descent > it->max_descent)
22735 it->descent = it->max_descent;
22736 }
22737
22738 take_vertical_position_into_account (it);
22739
22740 /* If we have to actually produce glyphs, do it. */
22741 if (it->glyph_row)
22742 {
22743 if (stretched_p)
22744 {
22745 /* Translate a space with a `space-width' property
22746 into a stretch glyph. */
22747 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22748 / FONT_HEIGHT (font));
22749 append_stretch_glyph (it, it->object, it->pixel_width,
22750 it->ascent + it->descent, ascent);
22751 }
22752 else
22753 append_glyph (it);
22754
22755 /* If characters with lbearing or rbearing are displayed
22756 in this line, record that fact in a flag of the
22757 glyph row. This is used to optimize X output code. */
22758 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22759 it->glyph_row->contains_overlapping_glyphs_p = 1;
22760 }
22761 if (! stretched_p && it->pixel_width == 0)
22762 /* We assure that all visible glyphs have at least 1-pixel
22763 width. */
22764 it->pixel_width = 1;
22765 }
22766 else if (it->char_to_display == '\n')
22767 {
22768 /* A newline has no width, but we need the height of the
22769 line. But if previous part of the line sets a height,
22770 don't increase that height */
22771
22772 Lisp_Object height;
22773 Lisp_Object total_height = Qnil;
22774
22775 it->override_ascent = -1;
22776 it->pixel_width = 0;
22777 it->nglyphs = 0;
22778
22779 height = get_it_property (it, Qline_height);
22780 /* Split (line-height total-height) list */
22781 if (CONSP (height)
22782 && CONSP (XCDR (height))
22783 && NILP (XCDR (XCDR (height))))
22784 {
22785 total_height = XCAR (XCDR (height));
22786 height = XCAR (height);
22787 }
22788 height = calc_line_height_property (it, height, font, boff, 1);
22789
22790 if (it->override_ascent >= 0)
22791 {
22792 it->ascent = it->override_ascent;
22793 it->descent = it->override_descent;
22794 boff = it->override_boff;
22795 }
22796 else
22797 {
22798 it->ascent = FONT_BASE (font) + boff;
22799 it->descent = FONT_DESCENT (font) - boff;
22800 }
22801
22802 if (EQ (height, Qt))
22803 {
22804 if (it->descent > it->max_descent)
22805 {
22806 it->ascent += it->descent - it->max_descent;
22807 it->descent = it->max_descent;
22808 }
22809 if (it->ascent > it->max_ascent)
22810 {
22811 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22812 it->ascent = it->max_ascent;
22813 }
22814 it->phys_ascent = min (it->phys_ascent, it->ascent);
22815 it->phys_descent = min (it->phys_descent, it->descent);
22816 it->constrain_row_ascent_descent_p = 1;
22817 extra_line_spacing = 0;
22818 }
22819 else
22820 {
22821 Lisp_Object spacing;
22822
22823 it->phys_ascent = it->ascent;
22824 it->phys_descent = it->descent;
22825
22826 if ((it->max_ascent > 0 || it->max_descent > 0)
22827 && face->box != FACE_NO_BOX
22828 && face->box_line_width > 0)
22829 {
22830 it->ascent += face->box_line_width;
22831 it->descent += face->box_line_width;
22832 }
22833 if (!NILP (height)
22834 && XINT (height) > it->ascent + it->descent)
22835 it->ascent = XINT (height) - it->descent;
22836
22837 if (!NILP (total_height))
22838 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22839 else
22840 {
22841 spacing = get_it_property (it, Qline_spacing);
22842 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22843 }
22844 if (INTEGERP (spacing))
22845 {
22846 extra_line_spacing = XINT (spacing);
22847 if (!NILP (total_height))
22848 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22849 }
22850 }
22851 }
22852 else /* i.e. (it->char_to_display == '\t') */
22853 {
22854 if (font->space_width > 0)
22855 {
22856 int tab_width = it->tab_width * font->space_width;
22857 int x = it->current_x + it->continuation_lines_width;
22858 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22859
22860 /* If the distance from the current position to the next tab
22861 stop is less than a space character width, use the
22862 tab stop after that. */
22863 if (next_tab_x - x < font->space_width)
22864 next_tab_x += tab_width;
22865
22866 it->pixel_width = next_tab_x - x;
22867 it->nglyphs = 1;
22868 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22869 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22870
22871 if (it->glyph_row)
22872 {
22873 append_stretch_glyph (it, it->object, it->pixel_width,
22874 it->ascent + it->descent, it->ascent);
22875 }
22876 }
22877 else
22878 {
22879 it->pixel_width = 0;
22880 it->nglyphs = 1;
22881 }
22882 }
22883 }
22884 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22885 {
22886 /* A static composition.
22887
22888 Note: A composition is represented as one glyph in the
22889 glyph matrix. There are no padding glyphs.
22890
22891 Important note: pixel_width, ascent, and descent are the
22892 values of what is drawn by draw_glyphs (i.e. the values of
22893 the overall glyphs composed). */
22894 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22895 int boff; /* baseline offset */
22896 struct composition *cmp = composition_table[it->cmp_it.id];
22897 int glyph_len = cmp->glyph_len;
22898 struct font *font = face->font;
22899
22900 it->nglyphs = 1;
22901
22902 /* If we have not yet calculated pixel size data of glyphs of
22903 the composition for the current face font, calculate them
22904 now. Theoretically, we have to check all fonts for the
22905 glyphs, but that requires much time and memory space. So,
22906 here we check only the font of the first glyph. This may
22907 lead to incorrect display, but it's very rare, and C-l
22908 (recenter-top-bottom) can correct the display anyway. */
22909 if (! cmp->font || cmp->font != font)
22910 {
22911 /* Ascent and descent of the font of the first character
22912 of this composition (adjusted by baseline offset).
22913 Ascent and descent of overall glyphs should not be less
22914 than these, respectively. */
22915 int font_ascent, font_descent, font_height;
22916 /* Bounding box of the overall glyphs. */
22917 int leftmost, rightmost, lowest, highest;
22918 int lbearing, rbearing;
22919 int i, width, ascent, descent;
22920 int left_padded = 0, right_padded = 0;
22921 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
22922 XChar2b char2b;
22923 struct font_metrics *pcm;
22924 int font_not_found_p;
22925 EMACS_INT pos;
22926
22927 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22928 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22929 break;
22930 if (glyph_len < cmp->glyph_len)
22931 right_padded = 1;
22932 for (i = 0; i < glyph_len; i++)
22933 {
22934 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22935 break;
22936 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22937 }
22938 if (i > 0)
22939 left_padded = 1;
22940
22941 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22942 : IT_CHARPOS (*it));
22943 /* If no suitable font is found, use the default font. */
22944 font_not_found_p = font == NULL;
22945 if (font_not_found_p)
22946 {
22947 face = face->ascii_face;
22948 font = face->font;
22949 }
22950 boff = font->baseline_offset;
22951 if (font->vertical_centering)
22952 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22953 font_ascent = FONT_BASE (font) + boff;
22954 font_descent = FONT_DESCENT (font) - boff;
22955 font_height = FONT_HEIGHT (font);
22956
22957 cmp->font = (void *) font;
22958
22959 pcm = NULL;
22960 if (! font_not_found_p)
22961 {
22962 get_char_face_and_encoding (it->f, c, it->face_id,
22963 &char2b, 0);
22964 pcm = get_per_char_metric (font, &char2b);
22965 }
22966
22967 /* Initialize the bounding box. */
22968 if (pcm)
22969 {
22970 width = pcm->width;
22971 ascent = pcm->ascent;
22972 descent = pcm->descent;
22973 lbearing = pcm->lbearing;
22974 rbearing = pcm->rbearing;
22975 }
22976 else
22977 {
22978 width = font->space_width;
22979 ascent = FONT_BASE (font);
22980 descent = FONT_DESCENT (font);
22981 lbearing = 0;
22982 rbearing = width;
22983 }
22984
22985 rightmost = width;
22986 leftmost = 0;
22987 lowest = - descent + boff;
22988 highest = ascent + boff;
22989
22990 if (! font_not_found_p
22991 && font->default_ascent
22992 && CHAR_TABLE_P (Vuse_default_ascent)
22993 && !NILP (Faref (Vuse_default_ascent,
22994 make_number (it->char_to_display))))
22995 highest = font->default_ascent + boff;
22996
22997 /* Draw the first glyph at the normal position. It may be
22998 shifted to right later if some other glyphs are drawn
22999 at the left. */
23000 cmp->offsets[i * 2] = 0;
23001 cmp->offsets[i * 2 + 1] = boff;
23002 cmp->lbearing = lbearing;
23003 cmp->rbearing = rbearing;
23004
23005 /* Set cmp->offsets for the remaining glyphs. */
23006 for (i++; i < glyph_len; i++)
23007 {
23008 int left, right, btm, top;
23009 int ch = COMPOSITION_GLYPH (cmp, i);
23010 int face_id;
23011 struct face *this_face;
23012
23013 if (ch == '\t')
23014 ch = ' ';
23015 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23016 this_face = FACE_FROM_ID (it->f, face_id);
23017 font = this_face->font;
23018
23019 if (font == NULL)
23020 pcm = NULL;
23021 else
23022 {
23023 get_char_face_and_encoding (it->f, ch, face_id,
23024 &char2b, 0);
23025 pcm = get_per_char_metric (font, &char2b);
23026 }
23027 if (! pcm)
23028 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23029 else
23030 {
23031 width = pcm->width;
23032 ascent = pcm->ascent;
23033 descent = pcm->descent;
23034 lbearing = pcm->lbearing;
23035 rbearing = pcm->rbearing;
23036 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23037 {
23038 /* Relative composition with or without
23039 alternate chars. */
23040 left = (leftmost + rightmost - width) / 2;
23041 btm = - descent + boff;
23042 if (font->relative_compose
23043 && (! CHAR_TABLE_P (Vignore_relative_composition)
23044 || NILP (Faref (Vignore_relative_composition,
23045 make_number (ch)))))
23046 {
23047
23048 if (- descent >= font->relative_compose)
23049 /* One extra pixel between two glyphs. */
23050 btm = highest + 1;
23051 else if (ascent <= 0)
23052 /* One extra pixel between two glyphs. */
23053 btm = lowest - 1 - ascent - descent;
23054 }
23055 }
23056 else
23057 {
23058 /* A composition rule is specified by an integer
23059 value that encodes global and new reference
23060 points (GREF and NREF). GREF and NREF are
23061 specified by numbers as below:
23062
23063 0---1---2 -- ascent
23064 | |
23065 | |
23066 | |
23067 9--10--11 -- center
23068 | |
23069 ---3---4---5--- baseline
23070 | |
23071 6---7---8 -- descent
23072 */
23073 int rule = COMPOSITION_RULE (cmp, i);
23074 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23075
23076 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23077 grefx = gref % 3, nrefx = nref % 3;
23078 grefy = gref / 3, nrefy = nref / 3;
23079 if (xoff)
23080 xoff = font_height * (xoff - 128) / 256;
23081 if (yoff)
23082 yoff = font_height * (yoff - 128) / 256;
23083
23084 left = (leftmost
23085 + grefx * (rightmost - leftmost) / 2
23086 - nrefx * width / 2
23087 + xoff);
23088
23089 btm = ((grefy == 0 ? highest
23090 : grefy == 1 ? 0
23091 : grefy == 2 ? lowest
23092 : (highest + lowest) / 2)
23093 - (nrefy == 0 ? ascent + descent
23094 : nrefy == 1 ? descent - boff
23095 : nrefy == 2 ? 0
23096 : (ascent + descent) / 2)
23097 + yoff);
23098 }
23099
23100 cmp->offsets[i * 2] = left;
23101 cmp->offsets[i * 2 + 1] = btm + descent;
23102
23103 /* Update the bounding box of the overall glyphs. */
23104 if (width > 0)
23105 {
23106 right = left + width;
23107 if (left < leftmost)
23108 leftmost = left;
23109 if (right > rightmost)
23110 rightmost = right;
23111 }
23112 top = btm + descent + ascent;
23113 if (top > highest)
23114 highest = top;
23115 if (btm < lowest)
23116 lowest = btm;
23117
23118 if (cmp->lbearing > left + lbearing)
23119 cmp->lbearing = left + lbearing;
23120 if (cmp->rbearing < left + rbearing)
23121 cmp->rbearing = left + rbearing;
23122 }
23123 }
23124
23125 /* If there are glyphs whose x-offsets are negative,
23126 shift all glyphs to the right and make all x-offsets
23127 non-negative. */
23128 if (leftmost < 0)
23129 {
23130 for (i = 0; i < cmp->glyph_len; i++)
23131 cmp->offsets[i * 2] -= leftmost;
23132 rightmost -= leftmost;
23133 cmp->lbearing -= leftmost;
23134 cmp->rbearing -= leftmost;
23135 }
23136
23137 if (left_padded && cmp->lbearing < 0)
23138 {
23139 for (i = 0; i < cmp->glyph_len; i++)
23140 cmp->offsets[i * 2] -= cmp->lbearing;
23141 rightmost -= cmp->lbearing;
23142 cmp->rbearing -= cmp->lbearing;
23143 cmp->lbearing = 0;
23144 }
23145 if (right_padded && rightmost < cmp->rbearing)
23146 {
23147 rightmost = cmp->rbearing;
23148 }
23149
23150 cmp->pixel_width = rightmost;
23151 cmp->ascent = highest;
23152 cmp->descent = - lowest;
23153 if (cmp->ascent < font_ascent)
23154 cmp->ascent = font_ascent;
23155 if (cmp->descent < font_descent)
23156 cmp->descent = font_descent;
23157 }
23158
23159 if (it->glyph_row
23160 && (cmp->lbearing < 0
23161 || cmp->rbearing > cmp->pixel_width))
23162 it->glyph_row->contains_overlapping_glyphs_p = 1;
23163
23164 it->pixel_width = cmp->pixel_width;
23165 it->ascent = it->phys_ascent = cmp->ascent;
23166 it->descent = it->phys_descent = cmp->descent;
23167 if (face->box != FACE_NO_BOX)
23168 {
23169 int thick = face->box_line_width;
23170
23171 if (thick > 0)
23172 {
23173 it->ascent += thick;
23174 it->descent += thick;
23175 }
23176 else
23177 thick = - thick;
23178
23179 if (it->start_of_box_run_p)
23180 it->pixel_width += thick;
23181 if (it->end_of_box_run_p)
23182 it->pixel_width += thick;
23183 }
23184
23185 /* If face has an overline, add the height of the overline
23186 (1 pixel) and a 1 pixel margin to the character height. */
23187 if (face->overline_p)
23188 it->ascent += overline_margin;
23189
23190 take_vertical_position_into_account (it);
23191 if (it->ascent < 0)
23192 it->ascent = 0;
23193 if (it->descent < 0)
23194 it->descent = 0;
23195
23196 if (it->glyph_row)
23197 append_composite_glyph (it);
23198 }
23199 else if (it->what == IT_COMPOSITION)
23200 {
23201 /* A dynamic (automatic) composition. */
23202 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23203 Lisp_Object gstring;
23204 struct font_metrics metrics;
23205
23206 gstring = composition_gstring_from_id (it->cmp_it.id);
23207 it->pixel_width
23208 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23209 &metrics);
23210 if (it->glyph_row
23211 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23212 it->glyph_row->contains_overlapping_glyphs_p = 1;
23213 it->ascent = it->phys_ascent = metrics.ascent;
23214 it->descent = it->phys_descent = metrics.descent;
23215 if (face->box != FACE_NO_BOX)
23216 {
23217 int thick = face->box_line_width;
23218
23219 if (thick > 0)
23220 {
23221 it->ascent += thick;
23222 it->descent += thick;
23223 }
23224 else
23225 thick = - thick;
23226
23227 if (it->start_of_box_run_p)
23228 it->pixel_width += thick;
23229 if (it->end_of_box_run_p)
23230 it->pixel_width += thick;
23231 }
23232 /* If face has an overline, add the height of the overline
23233 (1 pixel) and a 1 pixel margin to the character height. */
23234 if (face->overline_p)
23235 it->ascent += overline_margin;
23236 take_vertical_position_into_account (it);
23237 if (it->ascent < 0)
23238 it->ascent = 0;
23239 if (it->descent < 0)
23240 it->descent = 0;
23241
23242 if (it->glyph_row)
23243 append_composite_glyph (it);
23244 }
23245 else if (it->what == IT_GLYPHLESS)
23246 produce_glyphless_glyph (it, 0, Qnil);
23247 else if (it->what == IT_IMAGE)
23248 produce_image_glyph (it);
23249 else if (it->what == IT_STRETCH)
23250 produce_stretch_glyph (it);
23251
23252 done:
23253 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23254 because this isn't true for images with `:ascent 100'. */
23255 xassert (it->ascent >= 0 && it->descent >= 0);
23256 if (it->area == TEXT_AREA)
23257 it->current_x += it->pixel_width;
23258
23259 if (extra_line_spacing > 0)
23260 {
23261 it->descent += extra_line_spacing;
23262 if (extra_line_spacing > it->max_extra_line_spacing)
23263 it->max_extra_line_spacing = extra_line_spacing;
23264 }
23265
23266 it->max_ascent = max (it->max_ascent, it->ascent);
23267 it->max_descent = max (it->max_descent, it->descent);
23268 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23269 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23270 }
23271
23272 /* EXPORT for RIF:
23273 Output LEN glyphs starting at START at the nominal cursor position.
23274 Advance the nominal cursor over the text. The global variable
23275 updated_window contains the window being updated, updated_row is
23276 the glyph row being updated, and updated_area is the area of that
23277 row being updated. */
23278
23279 void
23280 x_write_glyphs (struct glyph *start, int len)
23281 {
23282 int x, hpos;
23283
23284 xassert (updated_window && updated_row);
23285 BLOCK_INPUT;
23286
23287 /* Write glyphs. */
23288
23289 hpos = start - updated_row->glyphs[updated_area];
23290 x = draw_glyphs (updated_window, output_cursor.x,
23291 updated_row, updated_area,
23292 hpos, hpos + len,
23293 DRAW_NORMAL_TEXT, 0);
23294
23295 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23296 if (updated_area == TEXT_AREA
23297 && updated_window->phys_cursor_on_p
23298 && updated_window->phys_cursor.vpos == output_cursor.vpos
23299 && updated_window->phys_cursor.hpos >= hpos
23300 && updated_window->phys_cursor.hpos < hpos + len)
23301 updated_window->phys_cursor_on_p = 0;
23302
23303 UNBLOCK_INPUT;
23304
23305 /* Advance the output cursor. */
23306 output_cursor.hpos += len;
23307 output_cursor.x = x;
23308 }
23309
23310
23311 /* EXPORT for RIF:
23312 Insert LEN glyphs from START at the nominal cursor position. */
23313
23314 void
23315 x_insert_glyphs (struct glyph *start, int len)
23316 {
23317 struct frame *f;
23318 struct window *w;
23319 int line_height, shift_by_width, shifted_region_width;
23320 struct glyph_row *row;
23321 struct glyph *glyph;
23322 int frame_x, frame_y;
23323 EMACS_INT hpos;
23324
23325 xassert (updated_window && updated_row);
23326 BLOCK_INPUT;
23327 w = updated_window;
23328 f = XFRAME (WINDOW_FRAME (w));
23329
23330 /* Get the height of the line we are in. */
23331 row = updated_row;
23332 line_height = row->height;
23333
23334 /* Get the width of the glyphs to insert. */
23335 shift_by_width = 0;
23336 for (glyph = start; glyph < start + len; ++glyph)
23337 shift_by_width += glyph->pixel_width;
23338
23339 /* Get the width of the region to shift right. */
23340 shifted_region_width = (window_box_width (w, updated_area)
23341 - output_cursor.x
23342 - shift_by_width);
23343
23344 /* Shift right. */
23345 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23346 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23347
23348 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23349 line_height, shift_by_width);
23350
23351 /* Write the glyphs. */
23352 hpos = start - row->glyphs[updated_area];
23353 draw_glyphs (w, output_cursor.x, row, updated_area,
23354 hpos, hpos + len,
23355 DRAW_NORMAL_TEXT, 0);
23356
23357 /* Advance the output cursor. */
23358 output_cursor.hpos += len;
23359 output_cursor.x += shift_by_width;
23360 UNBLOCK_INPUT;
23361 }
23362
23363
23364 /* EXPORT for RIF:
23365 Erase the current text line from the nominal cursor position
23366 (inclusive) to pixel column TO_X (exclusive). The idea is that
23367 everything from TO_X onward is already erased.
23368
23369 TO_X is a pixel position relative to updated_area of
23370 updated_window. TO_X == -1 means clear to the end of this area. */
23371
23372 void
23373 x_clear_end_of_line (int to_x)
23374 {
23375 struct frame *f;
23376 struct window *w = updated_window;
23377 int max_x, min_y, max_y;
23378 int from_x, from_y, to_y;
23379
23380 xassert (updated_window && updated_row);
23381 f = XFRAME (w->frame);
23382
23383 if (updated_row->full_width_p)
23384 max_x = WINDOW_TOTAL_WIDTH (w);
23385 else
23386 max_x = window_box_width (w, updated_area);
23387 max_y = window_text_bottom_y (w);
23388
23389 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23390 of window. For TO_X > 0, truncate to end of drawing area. */
23391 if (to_x == 0)
23392 return;
23393 else if (to_x < 0)
23394 to_x = max_x;
23395 else
23396 to_x = min (to_x, max_x);
23397
23398 to_y = min (max_y, output_cursor.y + updated_row->height);
23399
23400 /* Notice if the cursor will be cleared by this operation. */
23401 if (!updated_row->full_width_p)
23402 notice_overwritten_cursor (w, updated_area,
23403 output_cursor.x, -1,
23404 updated_row->y,
23405 MATRIX_ROW_BOTTOM_Y (updated_row));
23406
23407 from_x = output_cursor.x;
23408
23409 /* Translate to frame coordinates. */
23410 if (updated_row->full_width_p)
23411 {
23412 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23413 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23414 }
23415 else
23416 {
23417 int area_left = window_box_left (w, updated_area);
23418 from_x += area_left;
23419 to_x += area_left;
23420 }
23421
23422 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23423 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23424 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23425
23426 /* Prevent inadvertently clearing to end of the X window. */
23427 if (to_x > from_x && to_y > from_y)
23428 {
23429 BLOCK_INPUT;
23430 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23431 to_x - from_x, to_y - from_y);
23432 UNBLOCK_INPUT;
23433 }
23434 }
23435
23436 #endif /* HAVE_WINDOW_SYSTEM */
23437
23438
23439 \f
23440 /***********************************************************************
23441 Cursor types
23442 ***********************************************************************/
23443
23444 /* Value is the internal representation of the specified cursor type
23445 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23446 of the bar cursor. */
23447
23448 static enum text_cursor_kinds
23449 get_specified_cursor_type (Lisp_Object arg, int *width)
23450 {
23451 enum text_cursor_kinds type;
23452
23453 if (NILP (arg))
23454 return NO_CURSOR;
23455
23456 if (EQ (arg, Qbox))
23457 return FILLED_BOX_CURSOR;
23458
23459 if (EQ (arg, Qhollow))
23460 return HOLLOW_BOX_CURSOR;
23461
23462 if (EQ (arg, Qbar))
23463 {
23464 *width = 2;
23465 return BAR_CURSOR;
23466 }
23467
23468 if (CONSP (arg)
23469 && EQ (XCAR (arg), Qbar)
23470 && INTEGERP (XCDR (arg))
23471 && XINT (XCDR (arg)) >= 0)
23472 {
23473 *width = XINT (XCDR (arg));
23474 return BAR_CURSOR;
23475 }
23476
23477 if (EQ (arg, Qhbar))
23478 {
23479 *width = 2;
23480 return HBAR_CURSOR;
23481 }
23482
23483 if (CONSP (arg)
23484 && EQ (XCAR (arg), Qhbar)
23485 && INTEGERP (XCDR (arg))
23486 && XINT (XCDR (arg)) >= 0)
23487 {
23488 *width = XINT (XCDR (arg));
23489 return HBAR_CURSOR;
23490 }
23491
23492 /* Treat anything unknown as "hollow box cursor".
23493 It was bad to signal an error; people have trouble fixing
23494 .Xdefaults with Emacs, when it has something bad in it. */
23495 type = HOLLOW_BOX_CURSOR;
23496
23497 return type;
23498 }
23499
23500 /* Set the default cursor types for specified frame. */
23501 void
23502 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23503 {
23504 int width = 1;
23505 Lisp_Object tem;
23506
23507 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23508 FRAME_CURSOR_WIDTH (f) = width;
23509
23510 /* By default, set up the blink-off state depending on the on-state. */
23511
23512 tem = Fassoc (arg, Vblink_cursor_alist);
23513 if (!NILP (tem))
23514 {
23515 FRAME_BLINK_OFF_CURSOR (f)
23516 = get_specified_cursor_type (XCDR (tem), &width);
23517 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23518 }
23519 else
23520 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23521 }
23522
23523
23524 #ifdef HAVE_WINDOW_SYSTEM
23525
23526 /* Return the cursor we want to be displayed in window W. Return
23527 width of bar/hbar cursor through WIDTH arg. Return with
23528 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23529 (i.e. if the `system caret' should track this cursor).
23530
23531 In a mini-buffer window, we want the cursor only to appear if we
23532 are reading input from this window. For the selected window, we
23533 want the cursor type given by the frame parameter or buffer local
23534 setting of cursor-type. If explicitly marked off, draw no cursor.
23535 In all other cases, we want a hollow box cursor. */
23536
23537 static enum text_cursor_kinds
23538 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23539 int *active_cursor)
23540 {
23541 struct frame *f = XFRAME (w->frame);
23542 struct buffer *b = XBUFFER (w->buffer);
23543 int cursor_type = DEFAULT_CURSOR;
23544 Lisp_Object alt_cursor;
23545 int non_selected = 0;
23546
23547 *active_cursor = 1;
23548
23549 /* Echo area */
23550 if (cursor_in_echo_area
23551 && FRAME_HAS_MINIBUF_P (f)
23552 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23553 {
23554 if (w == XWINDOW (echo_area_window))
23555 {
23556 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
23557 {
23558 *width = FRAME_CURSOR_WIDTH (f);
23559 return FRAME_DESIRED_CURSOR (f);
23560 }
23561 else
23562 return get_specified_cursor_type (BVAR (b, cursor_type), width);
23563 }
23564
23565 *active_cursor = 0;
23566 non_selected = 1;
23567 }
23568
23569 /* Detect a nonselected window or nonselected frame. */
23570 else if (w != XWINDOW (f->selected_window)
23571 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23572 {
23573 *active_cursor = 0;
23574
23575 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23576 return NO_CURSOR;
23577
23578 non_selected = 1;
23579 }
23580
23581 /* Never display a cursor in a window in which cursor-type is nil. */
23582 if (NILP (BVAR (b, cursor_type)))
23583 return NO_CURSOR;
23584
23585 /* Get the normal cursor type for this window. */
23586 if (EQ (BVAR (b, cursor_type), Qt))
23587 {
23588 cursor_type = FRAME_DESIRED_CURSOR (f);
23589 *width = FRAME_CURSOR_WIDTH (f);
23590 }
23591 else
23592 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
23593
23594 /* Use cursor-in-non-selected-windows instead
23595 for non-selected window or frame. */
23596 if (non_selected)
23597 {
23598 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
23599 if (!EQ (Qt, alt_cursor))
23600 return get_specified_cursor_type (alt_cursor, width);
23601 /* t means modify the normal cursor type. */
23602 if (cursor_type == FILLED_BOX_CURSOR)
23603 cursor_type = HOLLOW_BOX_CURSOR;
23604 else if (cursor_type == BAR_CURSOR && *width > 1)
23605 --*width;
23606 return cursor_type;
23607 }
23608
23609 /* Use normal cursor if not blinked off. */
23610 if (!w->cursor_off_p)
23611 {
23612 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23613 {
23614 if (cursor_type == FILLED_BOX_CURSOR)
23615 {
23616 /* Using a block cursor on large images can be very annoying.
23617 So use a hollow cursor for "large" images.
23618 If image is not transparent (no mask), also use hollow cursor. */
23619 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23620 if (img != NULL && IMAGEP (img->spec))
23621 {
23622 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23623 where N = size of default frame font size.
23624 This should cover most of the "tiny" icons people may use. */
23625 if (!img->mask
23626 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23627 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23628 cursor_type = HOLLOW_BOX_CURSOR;
23629 }
23630 }
23631 else if (cursor_type != NO_CURSOR)
23632 {
23633 /* Display current only supports BOX and HOLLOW cursors for images.
23634 So for now, unconditionally use a HOLLOW cursor when cursor is
23635 not a solid box cursor. */
23636 cursor_type = HOLLOW_BOX_CURSOR;
23637 }
23638 }
23639 return cursor_type;
23640 }
23641
23642 /* Cursor is blinked off, so determine how to "toggle" it. */
23643
23644 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23645 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
23646 return get_specified_cursor_type (XCDR (alt_cursor), width);
23647
23648 /* Then see if frame has specified a specific blink off cursor type. */
23649 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23650 {
23651 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23652 return FRAME_BLINK_OFF_CURSOR (f);
23653 }
23654
23655 #if 0
23656 /* Some people liked having a permanently visible blinking cursor,
23657 while others had very strong opinions against it. So it was
23658 decided to remove it. KFS 2003-09-03 */
23659
23660 /* Finally perform built-in cursor blinking:
23661 filled box <-> hollow box
23662 wide [h]bar <-> narrow [h]bar
23663 narrow [h]bar <-> no cursor
23664 other type <-> no cursor */
23665
23666 if (cursor_type == FILLED_BOX_CURSOR)
23667 return HOLLOW_BOX_CURSOR;
23668
23669 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23670 {
23671 *width = 1;
23672 return cursor_type;
23673 }
23674 #endif
23675
23676 return NO_CURSOR;
23677 }
23678
23679
23680 /* Notice when the text cursor of window W has been completely
23681 overwritten by a drawing operation that outputs glyphs in AREA
23682 starting at X0 and ending at X1 in the line starting at Y0 and
23683 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23684 the rest of the line after X0 has been written. Y coordinates
23685 are window-relative. */
23686
23687 static void
23688 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23689 int x0, int x1, int y0, int y1)
23690 {
23691 int cx0, cx1, cy0, cy1;
23692 struct glyph_row *row;
23693
23694 if (!w->phys_cursor_on_p)
23695 return;
23696 if (area != TEXT_AREA)
23697 return;
23698
23699 if (w->phys_cursor.vpos < 0
23700 || w->phys_cursor.vpos >= w->current_matrix->nrows
23701 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23702 !(row->enabled_p && row->displays_text_p)))
23703 return;
23704
23705 if (row->cursor_in_fringe_p)
23706 {
23707 row->cursor_in_fringe_p = 0;
23708 draw_fringe_bitmap (w, row, row->reversed_p);
23709 w->phys_cursor_on_p = 0;
23710 return;
23711 }
23712
23713 cx0 = w->phys_cursor.x;
23714 cx1 = cx0 + w->phys_cursor_width;
23715 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23716 return;
23717
23718 /* The cursor image will be completely removed from the
23719 screen if the output area intersects the cursor area in
23720 y-direction. When we draw in [y0 y1[, and some part of
23721 the cursor is at y < y0, that part must have been drawn
23722 before. When scrolling, the cursor is erased before
23723 actually scrolling, so we don't come here. When not
23724 scrolling, the rows above the old cursor row must have
23725 changed, and in this case these rows must have written
23726 over the cursor image.
23727
23728 Likewise if part of the cursor is below y1, with the
23729 exception of the cursor being in the first blank row at
23730 the buffer and window end because update_text_area
23731 doesn't draw that row. (Except when it does, but
23732 that's handled in update_text_area.) */
23733
23734 cy0 = w->phys_cursor.y;
23735 cy1 = cy0 + w->phys_cursor_height;
23736 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23737 return;
23738
23739 w->phys_cursor_on_p = 0;
23740 }
23741
23742 #endif /* HAVE_WINDOW_SYSTEM */
23743
23744 \f
23745 /************************************************************************
23746 Mouse Face
23747 ************************************************************************/
23748
23749 #ifdef HAVE_WINDOW_SYSTEM
23750
23751 /* EXPORT for RIF:
23752 Fix the display of area AREA of overlapping row ROW in window W
23753 with respect to the overlapping part OVERLAPS. */
23754
23755 void
23756 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23757 enum glyph_row_area area, int overlaps)
23758 {
23759 int i, x;
23760
23761 BLOCK_INPUT;
23762
23763 x = 0;
23764 for (i = 0; i < row->used[area];)
23765 {
23766 if (row->glyphs[area][i].overlaps_vertically_p)
23767 {
23768 int start = i, start_x = x;
23769
23770 do
23771 {
23772 x += row->glyphs[area][i].pixel_width;
23773 ++i;
23774 }
23775 while (i < row->used[area]
23776 && row->glyphs[area][i].overlaps_vertically_p);
23777
23778 draw_glyphs (w, start_x, row, area,
23779 start, i,
23780 DRAW_NORMAL_TEXT, overlaps);
23781 }
23782 else
23783 {
23784 x += row->glyphs[area][i].pixel_width;
23785 ++i;
23786 }
23787 }
23788
23789 UNBLOCK_INPUT;
23790 }
23791
23792
23793 /* EXPORT:
23794 Draw the cursor glyph of window W in glyph row ROW. See the
23795 comment of draw_glyphs for the meaning of HL. */
23796
23797 void
23798 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23799 enum draw_glyphs_face hl)
23800 {
23801 /* If cursor hpos is out of bounds, don't draw garbage. This can
23802 happen in mini-buffer windows when switching between echo area
23803 glyphs and mini-buffer. */
23804 if ((row->reversed_p
23805 ? (w->phys_cursor.hpos >= 0)
23806 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23807 {
23808 int on_p = w->phys_cursor_on_p;
23809 int x1;
23810 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23811 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23812 hl, 0);
23813 w->phys_cursor_on_p = on_p;
23814
23815 if (hl == DRAW_CURSOR)
23816 w->phys_cursor_width = x1 - w->phys_cursor.x;
23817 /* When we erase the cursor, and ROW is overlapped by other
23818 rows, make sure that these overlapping parts of other rows
23819 are redrawn. */
23820 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23821 {
23822 w->phys_cursor_width = x1 - w->phys_cursor.x;
23823
23824 if (row > w->current_matrix->rows
23825 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23826 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23827 OVERLAPS_ERASED_CURSOR);
23828
23829 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23830 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23831 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23832 OVERLAPS_ERASED_CURSOR);
23833 }
23834 }
23835 }
23836
23837
23838 /* EXPORT:
23839 Erase the image of a cursor of window W from the screen. */
23840
23841 void
23842 erase_phys_cursor (struct window *w)
23843 {
23844 struct frame *f = XFRAME (w->frame);
23845 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23846 int hpos = w->phys_cursor.hpos;
23847 int vpos = w->phys_cursor.vpos;
23848 int mouse_face_here_p = 0;
23849 struct glyph_matrix *active_glyphs = w->current_matrix;
23850 struct glyph_row *cursor_row;
23851 struct glyph *cursor_glyph;
23852 enum draw_glyphs_face hl;
23853
23854 /* No cursor displayed or row invalidated => nothing to do on the
23855 screen. */
23856 if (w->phys_cursor_type == NO_CURSOR)
23857 goto mark_cursor_off;
23858
23859 /* VPOS >= active_glyphs->nrows means that window has been resized.
23860 Don't bother to erase the cursor. */
23861 if (vpos >= active_glyphs->nrows)
23862 goto mark_cursor_off;
23863
23864 /* If row containing cursor is marked invalid, there is nothing we
23865 can do. */
23866 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23867 if (!cursor_row->enabled_p)
23868 goto mark_cursor_off;
23869
23870 /* If line spacing is > 0, old cursor may only be partially visible in
23871 window after split-window. So adjust visible height. */
23872 cursor_row->visible_height = min (cursor_row->visible_height,
23873 window_text_bottom_y (w) - cursor_row->y);
23874
23875 /* If row is completely invisible, don't attempt to delete a cursor which
23876 isn't there. This can happen if cursor is at top of a window, and
23877 we switch to a buffer with a header line in that window. */
23878 if (cursor_row->visible_height <= 0)
23879 goto mark_cursor_off;
23880
23881 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23882 if (cursor_row->cursor_in_fringe_p)
23883 {
23884 cursor_row->cursor_in_fringe_p = 0;
23885 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23886 goto mark_cursor_off;
23887 }
23888
23889 /* This can happen when the new row is shorter than the old one.
23890 In this case, either draw_glyphs or clear_end_of_line
23891 should have cleared the cursor. Note that we wouldn't be
23892 able to erase the cursor in this case because we don't have a
23893 cursor glyph at hand. */
23894 if ((cursor_row->reversed_p
23895 ? (w->phys_cursor.hpos < 0)
23896 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23897 goto mark_cursor_off;
23898
23899 /* If the cursor is in the mouse face area, redisplay that when
23900 we clear the cursor. */
23901 if (! NILP (hlinfo->mouse_face_window)
23902 && coords_in_mouse_face_p (w, hpos, vpos)
23903 /* Don't redraw the cursor's spot in mouse face if it is at the
23904 end of a line (on a newline). The cursor appears there, but
23905 mouse highlighting does not. */
23906 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23907 mouse_face_here_p = 1;
23908
23909 /* Maybe clear the display under the cursor. */
23910 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23911 {
23912 int x, y, left_x;
23913 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23914 int width;
23915
23916 cursor_glyph = get_phys_cursor_glyph (w);
23917 if (cursor_glyph == NULL)
23918 goto mark_cursor_off;
23919
23920 width = cursor_glyph->pixel_width;
23921 left_x = window_box_left_offset (w, TEXT_AREA);
23922 x = w->phys_cursor.x;
23923 if (x < left_x)
23924 width -= left_x - x;
23925 width = min (width, window_box_width (w, TEXT_AREA) - x);
23926 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23927 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23928
23929 if (width > 0)
23930 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23931 }
23932
23933 /* Erase the cursor by redrawing the character underneath it. */
23934 if (mouse_face_here_p)
23935 hl = DRAW_MOUSE_FACE;
23936 else
23937 hl = DRAW_NORMAL_TEXT;
23938 draw_phys_cursor_glyph (w, cursor_row, hl);
23939
23940 mark_cursor_off:
23941 w->phys_cursor_on_p = 0;
23942 w->phys_cursor_type = NO_CURSOR;
23943 }
23944
23945
23946 /* EXPORT:
23947 Display or clear cursor of window W. If ON is zero, clear the
23948 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23949 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23950
23951 void
23952 display_and_set_cursor (struct window *w, int on,
23953 int hpos, int vpos, int x, int y)
23954 {
23955 struct frame *f = XFRAME (w->frame);
23956 int new_cursor_type;
23957 int new_cursor_width;
23958 int active_cursor;
23959 struct glyph_row *glyph_row;
23960 struct glyph *glyph;
23961
23962 /* This is pointless on invisible frames, and dangerous on garbaged
23963 windows and frames; in the latter case, the frame or window may
23964 be in the midst of changing its size, and x and y may be off the
23965 window. */
23966 if (! FRAME_VISIBLE_P (f)
23967 || FRAME_GARBAGED_P (f)
23968 || vpos >= w->current_matrix->nrows
23969 || hpos >= w->current_matrix->matrix_w)
23970 return;
23971
23972 /* If cursor is off and we want it off, return quickly. */
23973 if (!on && !w->phys_cursor_on_p)
23974 return;
23975
23976 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23977 /* If cursor row is not enabled, we don't really know where to
23978 display the cursor. */
23979 if (!glyph_row->enabled_p)
23980 {
23981 w->phys_cursor_on_p = 0;
23982 return;
23983 }
23984
23985 glyph = NULL;
23986 if (!glyph_row->exact_window_width_line_p
23987 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23988 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23989
23990 xassert (interrupt_input_blocked);
23991
23992 /* Set new_cursor_type to the cursor we want to be displayed. */
23993 new_cursor_type = get_window_cursor_type (w, glyph,
23994 &new_cursor_width, &active_cursor);
23995
23996 /* If cursor is currently being shown and we don't want it to be or
23997 it is in the wrong place, or the cursor type is not what we want,
23998 erase it. */
23999 if (w->phys_cursor_on_p
24000 && (!on
24001 || w->phys_cursor.x != x
24002 || w->phys_cursor.y != y
24003 || new_cursor_type != w->phys_cursor_type
24004 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24005 && new_cursor_width != w->phys_cursor_width)))
24006 erase_phys_cursor (w);
24007
24008 /* Don't check phys_cursor_on_p here because that flag is only set
24009 to zero in some cases where we know that the cursor has been
24010 completely erased, to avoid the extra work of erasing the cursor
24011 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24012 still not be visible, or it has only been partly erased. */
24013 if (on)
24014 {
24015 w->phys_cursor_ascent = glyph_row->ascent;
24016 w->phys_cursor_height = glyph_row->height;
24017
24018 /* Set phys_cursor_.* before x_draw_.* is called because some
24019 of them may need the information. */
24020 w->phys_cursor.x = x;
24021 w->phys_cursor.y = glyph_row->y;
24022 w->phys_cursor.hpos = hpos;
24023 w->phys_cursor.vpos = vpos;
24024 }
24025
24026 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24027 new_cursor_type, new_cursor_width,
24028 on, active_cursor);
24029 }
24030
24031
24032 /* Switch the display of W's cursor on or off, according to the value
24033 of ON. */
24034
24035 static void
24036 update_window_cursor (struct window *w, int on)
24037 {
24038 /* Don't update cursor in windows whose frame is in the process
24039 of being deleted. */
24040 if (w->current_matrix)
24041 {
24042 BLOCK_INPUT;
24043 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24044 w->phys_cursor.x, w->phys_cursor.y);
24045 UNBLOCK_INPUT;
24046 }
24047 }
24048
24049
24050 /* Call update_window_cursor with parameter ON_P on all leaf windows
24051 in the window tree rooted at W. */
24052
24053 static void
24054 update_cursor_in_window_tree (struct window *w, int on_p)
24055 {
24056 while (w)
24057 {
24058 if (!NILP (w->hchild))
24059 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24060 else if (!NILP (w->vchild))
24061 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24062 else
24063 update_window_cursor (w, on_p);
24064
24065 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24066 }
24067 }
24068
24069
24070 /* EXPORT:
24071 Display the cursor on window W, or clear it, according to ON_P.
24072 Don't change the cursor's position. */
24073
24074 void
24075 x_update_cursor (struct frame *f, int on_p)
24076 {
24077 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24078 }
24079
24080
24081 /* EXPORT:
24082 Clear the cursor of window W to background color, and mark the
24083 cursor as not shown. This is used when the text where the cursor
24084 is about to be rewritten. */
24085
24086 void
24087 x_clear_cursor (struct window *w)
24088 {
24089 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24090 update_window_cursor (w, 0);
24091 }
24092
24093 #endif /* HAVE_WINDOW_SYSTEM */
24094
24095 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24096 and MSDOS. */
24097 static void
24098 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24099 int start_hpos, int end_hpos,
24100 enum draw_glyphs_face draw)
24101 {
24102 #ifdef HAVE_WINDOW_SYSTEM
24103 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24104 {
24105 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24106 return;
24107 }
24108 #endif
24109 #if defined (HAVE_GPM) || defined (MSDOS)
24110 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24111 #endif
24112 }
24113
24114 /* Display the active region described by mouse_face_* according to DRAW. */
24115
24116 static void
24117 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24118 {
24119 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24120 struct frame *f = XFRAME (WINDOW_FRAME (w));
24121
24122 if (/* If window is in the process of being destroyed, don't bother
24123 to do anything. */
24124 w->current_matrix != NULL
24125 /* Don't update mouse highlight if hidden */
24126 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24127 /* Recognize when we are called to operate on rows that don't exist
24128 anymore. This can happen when a window is split. */
24129 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24130 {
24131 int phys_cursor_on_p = w->phys_cursor_on_p;
24132 struct glyph_row *row, *first, *last;
24133
24134 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24135 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24136
24137 for (row = first; row <= last && row->enabled_p; ++row)
24138 {
24139 int start_hpos, end_hpos, start_x;
24140
24141 /* For all but the first row, the highlight starts at column 0. */
24142 if (row == first)
24143 {
24144 /* R2L rows have BEG and END in reversed order, but the
24145 screen drawing geometry is always left to right. So
24146 we need to mirror the beginning and end of the
24147 highlighted area in R2L rows. */
24148 if (!row->reversed_p)
24149 {
24150 start_hpos = hlinfo->mouse_face_beg_col;
24151 start_x = hlinfo->mouse_face_beg_x;
24152 }
24153 else if (row == last)
24154 {
24155 start_hpos = hlinfo->mouse_face_end_col;
24156 start_x = hlinfo->mouse_face_end_x;
24157 }
24158 else
24159 {
24160 start_hpos = 0;
24161 start_x = 0;
24162 }
24163 }
24164 else if (row->reversed_p && row == last)
24165 {
24166 start_hpos = hlinfo->mouse_face_end_col;
24167 start_x = hlinfo->mouse_face_end_x;
24168 }
24169 else
24170 {
24171 start_hpos = 0;
24172 start_x = 0;
24173 }
24174
24175 if (row == last)
24176 {
24177 if (!row->reversed_p)
24178 end_hpos = hlinfo->mouse_face_end_col;
24179 else if (row == first)
24180 end_hpos = hlinfo->mouse_face_beg_col;
24181 else
24182 {
24183 end_hpos = row->used[TEXT_AREA];
24184 if (draw == DRAW_NORMAL_TEXT)
24185 row->fill_line_p = 1; /* Clear to end of line */
24186 }
24187 }
24188 else if (row->reversed_p && row == first)
24189 end_hpos = hlinfo->mouse_face_beg_col;
24190 else
24191 {
24192 end_hpos = row->used[TEXT_AREA];
24193 if (draw == DRAW_NORMAL_TEXT)
24194 row->fill_line_p = 1; /* Clear to end of line */
24195 }
24196
24197 if (end_hpos > start_hpos)
24198 {
24199 draw_row_with_mouse_face (w, start_x, row,
24200 start_hpos, end_hpos, draw);
24201
24202 row->mouse_face_p
24203 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24204 }
24205 }
24206
24207 #ifdef HAVE_WINDOW_SYSTEM
24208 /* When we've written over the cursor, arrange for it to
24209 be displayed again. */
24210 if (FRAME_WINDOW_P (f)
24211 && phys_cursor_on_p && !w->phys_cursor_on_p)
24212 {
24213 BLOCK_INPUT;
24214 display_and_set_cursor (w, 1,
24215 w->phys_cursor.hpos, w->phys_cursor.vpos,
24216 w->phys_cursor.x, w->phys_cursor.y);
24217 UNBLOCK_INPUT;
24218 }
24219 #endif /* HAVE_WINDOW_SYSTEM */
24220 }
24221
24222 #ifdef HAVE_WINDOW_SYSTEM
24223 /* Change the mouse cursor. */
24224 if (FRAME_WINDOW_P (f))
24225 {
24226 if (draw == DRAW_NORMAL_TEXT
24227 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24228 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24229 else if (draw == DRAW_MOUSE_FACE)
24230 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24231 else
24232 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24233 }
24234 #endif /* HAVE_WINDOW_SYSTEM */
24235 }
24236
24237 /* EXPORT:
24238 Clear out the mouse-highlighted active region.
24239 Redraw it un-highlighted first. Value is non-zero if mouse
24240 face was actually drawn unhighlighted. */
24241
24242 int
24243 clear_mouse_face (Mouse_HLInfo *hlinfo)
24244 {
24245 int cleared = 0;
24246
24247 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24248 {
24249 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24250 cleared = 1;
24251 }
24252
24253 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24254 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24255 hlinfo->mouse_face_window = Qnil;
24256 hlinfo->mouse_face_overlay = Qnil;
24257 return cleared;
24258 }
24259
24260 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24261 within the mouse face on that window. */
24262 static int
24263 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24264 {
24265 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24266
24267 /* Quickly resolve the easy cases. */
24268 if (!(WINDOWP (hlinfo->mouse_face_window)
24269 && XWINDOW (hlinfo->mouse_face_window) == w))
24270 return 0;
24271 if (vpos < hlinfo->mouse_face_beg_row
24272 || vpos > hlinfo->mouse_face_end_row)
24273 return 0;
24274 if (vpos > hlinfo->mouse_face_beg_row
24275 && vpos < hlinfo->mouse_face_end_row)
24276 return 1;
24277
24278 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24279 {
24280 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24281 {
24282 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24283 return 1;
24284 }
24285 else if ((vpos == hlinfo->mouse_face_beg_row
24286 && hpos >= hlinfo->mouse_face_beg_col)
24287 || (vpos == hlinfo->mouse_face_end_row
24288 && hpos < hlinfo->mouse_face_end_col))
24289 return 1;
24290 }
24291 else
24292 {
24293 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24294 {
24295 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24296 return 1;
24297 }
24298 else if ((vpos == hlinfo->mouse_face_beg_row
24299 && hpos <= hlinfo->mouse_face_beg_col)
24300 || (vpos == hlinfo->mouse_face_end_row
24301 && hpos > hlinfo->mouse_face_end_col))
24302 return 1;
24303 }
24304 return 0;
24305 }
24306
24307
24308 /* EXPORT:
24309 Non-zero if physical cursor of window W is within mouse face. */
24310
24311 int
24312 cursor_in_mouse_face_p (struct window *w)
24313 {
24314 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24315 }
24316
24317
24318 \f
24319 /* Find the glyph rows START_ROW and END_ROW of window W that display
24320 characters between buffer positions START_CHARPOS and END_CHARPOS
24321 (excluding END_CHARPOS). This is similar to row_containing_pos,
24322 but is more accurate when bidi reordering makes buffer positions
24323 change non-linearly with glyph rows. */
24324 static void
24325 rows_from_pos_range (struct window *w,
24326 EMACS_INT start_charpos, EMACS_INT end_charpos,
24327 struct glyph_row **start, struct glyph_row **end)
24328 {
24329 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24330 int last_y = window_text_bottom_y (w);
24331 struct glyph_row *row;
24332
24333 *start = NULL;
24334 *end = NULL;
24335
24336 while (!first->enabled_p
24337 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24338 first++;
24339
24340 /* Find the START row. */
24341 for (row = first;
24342 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24343 row++)
24344 {
24345 /* A row can potentially be the START row if the range of the
24346 characters it displays intersects the range
24347 [START_CHARPOS..END_CHARPOS). */
24348 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24349 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24350 /* See the commentary in row_containing_pos, for the
24351 explanation of the complicated way to check whether
24352 some position is beyond the end of the characters
24353 displayed by a row. */
24354 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24355 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24356 && !row->ends_at_zv_p
24357 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24358 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24359 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24360 && !row->ends_at_zv_p
24361 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24362 {
24363 /* Found a candidate row. Now make sure at least one of the
24364 glyphs it displays has a charpos from the range
24365 [START_CHARPOS..END_CHARPOS).
24366
24367 This is not obvious because bidi reordering could make
24368 buffer positions of a row be 1,2,3,102,101,100, and if we
24369 want to highlight characters in [50..60), we don't want
24370 this row, even though [50..60) does intersect [1..103),
24371 the range of character positions given by the row's start
24372 and end positions. */
24373 struct glyph *g = row->glyphs[TEXT_AREA];
24374 struct glyph *e = g + row->used[TEXT_AREA];
24375
24376 while (g < e)
24377 {
24378 if (BUFFERP (g->object)
24379 && start_charpos <= g->charpos && g->charpos < end_charpos)
24380 *start = row;
24381 g++;
24382 }
24383 if (*start)
24384 break;
24385 }
24386 }
24387
24388 /* Find the END row. */
24389 if (!*start
24390 /* If the last row is partially visible, start looking for END
24391 from that row, instead of starting from FIRST. */
24392 && !(row->enabled_p
24393 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24394 row = first;
24395 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24396 {
24397 struct glyph_row *next = row + 1;
24398
24399 if (!next->enabled_p
24400 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24401 /* The first row >= START whose range of displayed characters
24402 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24403 is the row END + 1. */
24404 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24405 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24406 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24407 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24408 && !next->ends_at_zv_p
24409 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24410 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24411 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24412 && !next->ends_at_zv_p
24413 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24414 {
24415 *end = row;
24416 break;
24417 }
24418 else
24419 {
24420 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24421 but none of the characters it displays are in the range, it is
24422 also END + 1. */
24423 struct glyph *g = next->glyphs[TEXT_AREA];
24424 struct glyph *e = g + next->used[TEXT_AREA];
24425
24426 while (g < e)
24427 {
24428 if (BUFFERP (g->object)
24429 && start_charpos <= g->charpos && g->charpos < end_charpos)
24430 break;
24431 g++;
24432 }
24433 if (g == e)
24434 {
24435 *end = row;
24436 break;
24437 }
24438 }
24439 }
24440 }
24441
24442 /* This function sets the mouse_face_* elements of HLINFO, assuming
24443 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24444 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24445 for the overlay or run of text properties specifying the mouse
24446 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24447 before-string and after-string that must also be highlighted.
24448 COVER_STRING, if non-nil, is a display string that may cover some
24449 or all of the highlighted text. */
24450
24451 static void
24452 mouse_face_from_buffer_pos (Lisp_Object window,
24453 Mouse_HLInfo *hlinfo,
24454 EMACS_INT mouse_charpos,
24455 EMACS_INT start_charpos,
24456 EMACS_INT end_charpos,
24457 Lisp_Object before_string,
24458 Lisp_Object after_string,
24459 Lisp_Object cover_string)
24460 {
24461 struct window *w = XWINDOW (window);
24462 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24463 struct glyph_row *r1, *r2;
24464 struct glyph *glyph, *end;
24465 EMACS_INT ignore, pos;
24466 int x;
24467
24468 xassert (NILP (cover_string) || STRINGP (cover_string));
24469 xassert (NILP (before_string) || STRINGP (before_string));
24470 xassert (NILP (after_string) || STRINGP (after_string));
24471
24472 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24473 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24474 if (r1 == NULL)
24475 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24476 /* If the before-string or display-string contains newlines,
24477 rows_from_pos_range skips to its last row. Move back. */
24478 if (!NILP (before_string) || !NILP (cover_string))
24479 {
24480 struct glyph_row *prev;
24481 while ((prev = r1 - 1, prev >= first)
24482 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24483 && prev->used[TEXT_AREA] > 0)
24484 {
24485 struct glyph *beg = prev->glyphs[TEXT_AREA];
24486 glyph = beg + prev->used[TEXT_AREA];
24487 while (--glyph >= beg && INTEGERP (glyph->object));
24488 if (glyph < beg
24489 || !(EQ (glyph->object, before_string)
24490 || EQ (glyph->object, cover_string)))
24491 break;
24492 r1 = prev;
24493 }
24494 }
24495 if (r2 == NULL)
24496 {
24497 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24498 hlinfo->mouse_face_past_end = 1;
24499 }
24500 else if (!NILP (after_string))
24501 {
24502 /* If the after-string has newlines, advance to its last row. */
24503 struct glyph_row *next;
24504 struct glyph_row *last
24505 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24506
24507 for (next = r2 + 1;
24508 next <= last
24509 && next->used[TEXT_AREA] > 0
24510 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24511 ++next)
24512 r2 = next;
24513 }
24514 /* The rest of the display engine assumes that mouse_face_beg_row is
24515 either above below mouse_face_end_row or identical to it. But
24516 with bidi-reordered continued lines, the row for START_CHARPOS
24517 could be below the row for END_CHARPOS. If so, swap the rows and
24518 store them in correct order. */
24519 if (r1->y > r2->y)
24520 {
24521 struct glyph_row *tem = r2;
24522
24523 r2 = r1;
24524 r1 = tem;
24525 }
24526
24527 hlinfo->mouse_face_beg_y = r1->y;
24528 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24529 hlinfo->mouse_face_end_y = r2->y;
24530 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24531
24532 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24533 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
24534 could be anywhere in the row and in any order. The strategy
24535 below is to find the leftmost and the rightmost glyph that
24536 belongs to either of these 3 strings, or whose position is
24537 between START_CHARPOS and END_CHARPOS, and highlight all the
24538 glyphs between those two. This may cover more than just the text
24539 between START_CHARPOS and END_CHARPOS if the range of characters
24540 strides the bidi level boundary, e.g. if the beginning is in R2L
24541 text while the end is in L2R text or vice versa. */
24542 if (!r1->reversed_p)
24543 {
24544 /* This row is in a left to right paragraph. Scan it left to
24545 right. */
24546 glyph = r1->glyphs[TEXT_AREA];
24547 end = glyph + r1->used[TEXT_AREA];
24548 x = r1->x;
24549
24550 /* Skip truncation glyphs at the start of the glyph row. */
24551 if (r1->displays_text_p)
24552 for (; glyph < end
24553 && INTEGERP (glyph->object)
24554 && glyph->charpos < 0;
24555 ++glyph)
24556 x += glyph->pixel_width;
24557
24558 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24559 or COVER_STRING, and the first glyph from buffer whose
24560 position is between START_CHARPOS and END_CHARPOS. */
24561 for (; glyph < end
24562 && !INTEGERP (glyph->object)
24563 && !EQ (glyph->object, cover_string)
24564 && !(BUFFERP (glyph->object)
24565 && (glyph->charpos >= start_charpos
24566 && glyph->charpos < end_charpos));
24567 ++glyph)
24568 {
24569 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24570 are present at buffer positions between START_CHARPOS and
24571 END_CHARPOS, or if they come from an overlay. */
24572 if (EQ (glyph->object, before_string))
24573 {
24574 pos = string_buffer_position (before_string,
24575 start_charpos);
24576 /* If pos == 0, it means before_string came from an
24577 overlay, not from a buffer position. */
24578 if (!pos || (pos >= start_charpos && pos < end_charpos))
24579 break;
24580 }
24581 else if (EQ (glyph->object, after_string))
24582 {
24583 pos = string_buffer_position (after_string, end_charpos);
24584 if (!pos || (pos >= start_charpos && pos < end_charpos))
24585 break;
24586 }
24587 x += glyph->pixel_width;
24588 }
24589 hlinfo->mouse_face_beg_x = x;
24590 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24591 }
24592 else
24593 {
24594 /* This row is in a right to left paragraph. Scan it right to
24595 left. */
24596 struct glyph *g;
24597
24598 end = r1->glyphs[TEXT_AREA] - 1;
24599 glyph = end + r1->used[TEXT_AREA];
24600
24601 /* Skip truncation glyphs at the start of the glyph row. */
24602 if (r1->displays_text_p)
24603 for (; glyph > end
24604 && INTEGERP (glyph->object)
24605 && glyph->charpos < 0;
24606 --glyph)
24607 ;
24608
24609 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24610 or COVER_STRING, and the first glyph from buffer whose
24611 position is between START_CHARPOS and END_CHARPOS. */
24612 for (; glyph > end
24613 && !INTEGERP (glyph->object)
24614 && !EQ (glyph->object, cover_string)
24615 && !(BUFFERP (glyph->object)
24616 && (glyph->charpos >= start_charpos
24617 && glyph->charpos < end_charpos));
24618 --glyph)
24619 {
24620 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24621 are present at buffer positions between START_CHARPOS and
24622 END_CHARPOS, or if they come from an overlay. */
24623 if (EQ (glyph->object, before_string))
24624 {
24625 pos = string_buffer_position (before_string, start_charpos);
24626 /* If pos == 0, it means before_string came from an
24627 overlay, not from a buffer position. */
24628 if (!pos || (pos >= start_charpos && pos < end_charpos))
24629 break;
24630 }
24631 else if (EQ (glyph->object, after_string))
24632 {
24633 pos = string_buffer_position (after_string, end_charpos);
24634 if (!pos || (pos >= start_charpos && pos < end_charpos))
24635 break;
24636 }
24637 }
24638
24639 glyph++; /* first glyph to the right of the highlighted area */
24640 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24641 x += g->pixel_width;
24642 hlinfo->mouse_face_beg_x = x;
24643 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24644 }
24645
24646 /* If the highlight ends in a different row, compute GLYPH and END
24647 for the end row. Otherwise, reuse the values computed above for
24648 the row where the highlight begins. */
24649 if (r2 != r1)
24650 {
24651 if (!r2->reversed_p)
24652 {
24653 glyph = r2->glyphs[TEXT_AREA];
24654 end = glyph + r2->used[TEXT_AREA];
24655 x = r2->x;
24656 }
24657 else
24658 {
24659 end = r2->glyphs[TEXT_AREA] - 1;
24660 glyph = end + r2->used[TEXT_AREA];
24661 }
24662 }
24663
24664 if (!r2->reversed_p)
24665 {
24666 /* Skip truncation and continuation glyphs near the end of the
24667 row, and also blanks and stretch glyphs inserted by
24668 extend_face_to_end_of_line. */
24669 while (end > glyph
24670 && INTEGERP ((end - 1)->object)
24671 && (end - 1)->charpos <= 0)
24672 --end;
24673 /* Scan the rest of the glyph row from the end, looking for the
24674 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24675 COVER_STRING, or whose position is between START_CHARPOS
24676 and END_CHARPOS */
24677 for (--end;
24678 end > glyph
24679 && !INTEGERP (end->object)
24680 && !EQ (end->object, cover_string)
24681 && !(BUFFERP (end->object)
24682 && (end->charpos >= start_charpos
24683 && end->charpos < end_charpos));
24684 --end)
24685 {
24686 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24687 are present at buffer positions between START_CHARPOS and
24688 END_CHARPOS, or if they come from an overlay. */
24689 if (EQ (end->object, before_string))
24690 {
24691 pos = string_buffer_position (before_string, start_charpos);
24692 if (!pos || (pos >= start_charpos && pos < end_charpos))
24693 break;
24694 }
24695 else if (EQ (end->object, after_string))
24696 {
24697 pos = string_buffer_position (after_string, end_charpos);
24698 if (!pos || (pos >= start_charpos && pos < end_charpos))
24699 break;
24700 }
24701 }
24702 /* Find the X coordinate of the last glyph to be highlighted. */
24703 for (; glyph <= end; ++glyph)
24704 x += glyph->pixel_width;
24705
24706 hlinfo->mouse_face_end_x = x;
24707 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24708 }
24709 else
24710 {
24711 /* Skip truncation and continuation glyphs near the end of the
24712 row, and also blanks and stretch glyphs inserted by
24713 extend_face_to_end_of_line. */
24714 x = r2->x;
24715 end++;
24716 while (end < glyph
24717 && INTEGERP (end->object)
24718 && end->charpos <= 0)
24719 {
24720 x += end->pixel_width;
24721 ++end;
24722 }
24723 /* Scan the rest of the glyph row from the end, looking for the
24724 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24725 COVER_STRING, or whose position is between START_CHARPOS
24726 and END_CHARPOS */
24727 for ( ;
24728 end < glyph
24729 && !INTEGERP (end->object)
24730 && !EQ (end->object, cover_string)
24731 && !(BUFFERP (end->object)
24732 && (end->charpos >= start_charpos
24733 && end->charpos < end_charpos));
24734 ++end)
24735 {
24736 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24737 are present at buffer positions between START_CHARPOS and
24738 END_CHARPOS, or if they come from an overlay. */
24739 if (EQ (end->object, before_string))
24740 {
24741 pos = string_buffer_position (before_string, start_charpos);
24742 if (!pos || (pos >= start_charpos && pos < end_charpos))
24743 break;
24744 }
24745 else if (EQ (end->object, after_string))
24746 {
24747 pos = string_buffer_position (after_string, end_charpos);
24748 if (!pos || (pos >= start_charpos && pos < end_charpos))
24749 break;
24750 }
24751 x += end->pixel_width;
24752 }
24753 hlinfo->mouse_face_end_x = x;
24754 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24755 }
24756
24757 hlinfo->mouse_face_window = window;
24758 hlinfo->mouse_face_face_id
24759 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24760 mouse_charpos + 1,
24761 !hlinfo->mouse_face_hidden, -1);
24762 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24763 }
24764
24765 /* The following function is not used anymore (replaced with
24766 mouse_face_from_string_pos), but I leave it here for the time
24767 being, in case someone would. */
24768
24769 #if 0 /* not used */
24770
24771 /* Find the position of the glyph for position POS in OBJECT in
24772 window W's current matrix, and return in *X, *Y the pixel
24773 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24774
24775 RIGHT_P non-zero means return the position of the right edge of the
24776 glyph, RIGHT_P zero means return the left edge position.
24777
24778 If no glyph for POS exists in the matrix, return the position of
24779 the glyph with the next smaller position that is in the matrix, if
24780 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24781 exists in the matrix, return the position of the glyph with the
24782 next larger position in OBJECT.
24783
24784 Value is non-zero if a glyph was found. */
24785
24786 static int
24787 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24788 int *hpos, int *vpos, int *x, int *y, int right_p)
24789 {
24790 int yb = window_text_bottom_y (w);
24791 struct glyph_row *r;
24792 struct glyph *best_glyph = NULL;
24793 struct glyph_row *best_row = NULL;
24794 int best_x = 0;
24795
24796 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24797 r->enabled_p && r->y < yb;
24798 ++r)
24799 {
24800 struct glyph *g = r->glyphs[TEXT_AREA];
24801 struct glyph *e = g + r->used[TEXT_AREA];
24802 int gx;
24803
24804 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24805 if (EQ (g->object, object))
24806 {
24807 if (g->charpos == pos)
24808 {
24809 best_glyph = g;
24810 best_x = gx;
24811 best_row = r;
24812 goto found;
24813 }
24814 else if (best_glyph == NULL
24815 || ((eabs (g->charpos - pos)
24816 < eabs (best_glyph->charpos - pos))
24817 && (right_p
24818 ? g->charpos < pos
24819 : g->charpos > pos)))
24820 {
24821 best_glyph = g;
24822 best_x = gx;
24823 best_row = r;
24824 }
24825 }
24826 }
24827
24828 found:
24829
24830 if (best_glyph)
24831 {
24832 *x = best_x;
24833 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24834
24835 if (right_p)
24836 {
24837 *x += best_glyph->pixel_width;
24838 ++*hpos;
24839 }
24840
24841 *y = best_row->y;
24842 *vpos = best_row - w->current_matrix->rows;
24843 }
24844
24845 return best_glyph != NULL;
24846 }
24847 #endif /* not used */
24848
24849 /* Find the positions of the first and the last glyphs in window W's
24850 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24851 (assumed to be a string), and return in HLINFO's mouse_face_*
24852 members the pixel and column/row coordinates of those glyphs. */
24853
24854 static void
24855 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24856 Lisp_Object object,
24857 EMACS_INT startpos, EMACS_INT endpos)
24858 {
24859 int yb = window_text_bottom_y (w);
24860 struct glyph_row *r;
24861 struct glyph *g, *e;
24862 int gx;
24863 int found = 0;
24864
24865 /* Find the glyph row with at least one position in the range
24866 [STARTPOS..ENDPOS], and the first glyph in that row whose
24867 position belongs to that range. */
24868 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24869 r->enabled_p && r->y < yb;
24870 ++r)
24871 {
24872 if (!r->reversed_p)
24873 {
24874 g = r->glyphs[TEXT_AREA];
24875 e = g + r->used[TEXT_AREA];
24876 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24877 if (EQ (g->object, object)
24878 && startpos <= g->charpos && g->charpos <= endpos)
24879 {
24880 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24881 hlinfo->mouse_face_beg_y = r->y;
24882 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24883 hlinfo->mouse_face_beg_x = gx;
24884 found = 1;
24885 break;
24886 }
24887 }
24888 else
24889 {
24890 struct glyph *g1;
24891
24892 e = r->glyphs[TEXT_AREA];
24893 g = e + r->used[TEXT_AREA];
24894 for ( ; g > e; --g)
24895 if (EQ ((g-1)->object, object)
24896 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24897 {
24898 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24899 hlinfo->mouse_face_beg_y = r->y;
24900 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24901 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24902 gx += g1->pixel_width;
24903 hlinfo->mouse_face_beg_x = gx;
24904 found = 1;
24905 break;
24906 }
24907 }
24908 if (found)
24909 break;
24910 }
24911
24912 if (!found)
24913 return;
24914
24915 /* Starting with the next row, look for the first row which does NOT
24916 include any glyphs whose positions are in the range. */
24917 for (++r; r->enabled_p && r->y < yb; ++r)
24918 {
24919 g = r->glyphs[TEXT_AREA];
24920 e = g + r->used[TEXT_AREA];
24921 found = 0;
24922 for ( ; g < e; ++g)
24923 if (EQ (g->object, object)
24924 && startpos <= g->charpos && g->charpos <= endpos)
24925 {
24926 found = 1;
24927 break;
24928 }
24929 if (!found)
24930 break;
24931 }
24932
24933 /* The highlighted region ends on the previous row. */
24934 r--;
24935
24936 /* Set the end row and its vertical pixel coordinate. */
24937 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24938 hlinfo->mouse_face_end_y = r->y;
24939
24940 /* Compute and set the end column and the end column's horizontal
24941 pixel coordinate. */
24942 if (!r->reversed_p)
24943 {
24944 g = r->glyphs[TEXT_AREA];
24945 e = g + r->used[TEXT_AREA];
24946 for ( ; e > g; --e)
24947 if (EQ ((e-1)->object, object)
24948 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24949 break;
24950 hlinfo->mouse_face_end_col = e - g;
24951
24952 for (gx = r->x; g < e; ++g)
24953 gx += g->pixel_width;
24954 hlinfo->mouse_face_end_x = gx;
24955 }
24956 else
24957 {
24958 e = r->glyphs[TEXT_AREA];
24959 g = e + r->used[TEXT_AREA];
24960 for (gx = r->x ; e < g; ++e)
24961 {
24962 if (EQ (e->object, object)
24963 && startpos <= e->charpos && e->charpos <= endpos)
24964 break;
24965 gx += e->pixel_width;
24966 }
24967 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24968 hlinfo->mouse_face_end_x = gx;
24969 }
24970 }
24971
24972 #ifdef HAVE_WINDOW_SYSTEM
24973
24974 /* See if position X, Y is within a hot-spot of an image. */
24975
24976 static int
24977 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24978 {
24979 if (!CONSP (hot_spot))
24980 return 0;
24981
24982 if (EQ (XCAR (hot_spot), Qrect))
24983 {
24984 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24985 Lisp_Object rect = XCDR (hot_spot);
24986 Lisp_Object tem;
24987 if (!CONSP (rect))
24988 return 0;
24989 if (!CONSP (XCAR (rect)))
24990 return 0;
24991 if (!CONSP (XCDR (rect)))
24992 return 0;
24993 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24994 return 0;
24995 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24996 return 0;
24997 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24998 return 0;
24999 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25000 return 0;
25001 return 1;
25002 }
25003 else if (EQ (XCAR (hot_spot), Qcircle))
25004 {
25005 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25006 Lisp_Object circ = XCDR (hot_spot);
25007 Lisp_Object lr, lx0, ly0;
25008 if (CONSP (circ)
25009 && CONSP (XCAR (circ))
25010 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25011 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25012 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25013 {
25014 double r = XFLOATINT (lr);
25015 double dx = XINT (lx0) - x;
25016 double dy = XINT (ly0) - y;
25017 return (dx * dx + dy * dy <= r * r);
25018 }
25019 }
25020 else if (EQ (XCAR (hot_spot), Qpoly))
25021 {
25022 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25023 if (VECTORP (XCDR (hot_spot)))
25024 {
25025 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25026 Lisp_Object *poly = v->contents;
25027 int n = v->header.size;
25028 int i;
25029 int inside = 0;
25030 Lisp_Object lx, ly;
25031 int x0, y0;
25032
25033 /* Need an even number of coordinates, and at least 3 edges. */
25034 if (n < 6 || n & 1)
25035 return 0;
25036
25037 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25038 If count is odd, we are inside polygon. Pixels on edges
25039 may or may not be included depending on actual geometry of the
25040 polygon. */
25041 if ((lx = poly[n-2], !INTEGERP (lx))
25042 || (ly = poly[n-1], !INTEGERP (lx)))
25043 return 0;
25044 x0 = XINT (lx), y0 = XINT (ly);
25045 for (i = 0; i < n; i += 2)
25046 {
25047 int x1 = x0, y1 = y0;
25048 if ((lx = poly[i], !INTEGERP (lx))
25049 || (ly = poly[i+1], !INTEGERP (ly)))
25050 return 0;
25051 x0 = XINT (lx), y0 = XINT (ly);
25052
25053 /* Does this segment cross the X line? */
25054 if (x0 >= x)
25055 {
25056 if (x1 >= x)
25057 continue;
25058 }
25059 else if (x1 < x)
25060 continue;
25061 if (y > y0 && y > y1)
25062 continue;
25063 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25064 inside = !inside;
25065 }
25066 return inside;
25067 }
25068 }
25069 return 0;
25070 }
25071
25072 Lisp_Object
25073 find_hot_spot (Lisp_Object map, int x, int y)
25074 {
25075 while (CONSP (map))
25076 {
25077 if (CONSP (XCAR (map))
25078 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25079 return XCAR (map);
25080 map = XCDR (map);
25081 }
25082
25083 return Qnil;
25084 }
25085
25086 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25087 3, 3, 0,
25088 doc: /* Lookup in image map MAP coordinates X and Y.
25089 An image map is an alist where each element has the format (AREA ID PLIST).
25090 An AREA is specified as either a rectangle, a circle, or a polygon:
25091 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25092 pixel coordinates of the upper left and bottom right corners.
25093 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25094 and the radius of the circle; r may be a float or integer.
25095 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25096 vector describes one corner in the polygon.
25097 Returns the alist element for the first matching AREA in MAP. */)
25098 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25099 {
25100 if (NILP (map))
25101 return Qnil;
25102
25103 CHECK_NUMBER (x);
25104 CHECK_NUMBER (y);
25105
25106 return find_hot_spot (map, XINT (x), XINT (y));
25107 }
25108
25109
25110 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25111 static void
25112 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25113 {
25114 /* Do not change cursor shape while dragging mouse. */
25115 if (!NILP (do_mouse_tracking))
25116 return;
25117
25118 if (!NILP (pointer))
25119 {
25120 if (EQ (pointer, Qarrow))
25121 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25122 else if (EQ (pointer, Qhand))
25123 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25124 else if (EQ (pointer, Qtext))
25125 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25126 else if (EQ (pointer, intern ("hdrag")))
25127 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25128 #ifdef HAVE_X_WINDOWS
25129 else if (EQ (pointer, intern ("vdrag")))
25130 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25131 #endif
25132 else if (EQ (pointer, intern ("hourglass")))
25133 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25134 else if (EQ (pointer, Qmodeline))
25135 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25136 else
25137 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25138 }
25139
25140 if (cursor != No_Cursor)
25141 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25142 }
25143
25144 #endif /* HAVE_WINDOW_SYSTEM */
25145
25146 /* Take proper action when mouse has moved to the mode or header line
25147 or marginal area AREA of window W, x-position X and y-position Y.
25148 X is relative to the start of the text display area of W, so the
25149 width of bitmap areas and scroll bars must be subtracted to get a
25150 position relative to the start of the mode line. */
25151
25152 static void
25153 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25154 enum window_part area)
25155 {
25156 struct window *w = XWINDOW (window);
25157 struct frame *f = XFRAME (w->frame);
25158 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25159 #ifdef HAVE_WINDOW_SYSTEM
25160 Display_Info *dpyinfo;
25161 #endif
25162 Cursor cursor = No_Cursor;
25163 Lisp_Object pointer = Qnil;
25164 int dx, dy, width, height;
25165 EMACS_INT charpos;
25166 Lisp_Object string, object = Qnil;
25167 Lisp_Object pos, help;
25168
25169 Lisp_Object mouse_face;
25170 int original_x_pixel = x;
25171 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25172 struct glyph_row *row;
25173
25174 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25175 {
25176 int x0;
25177 struct glyph *end;
25178
25179 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25180 returns them in row/column units! */
25181 string = mode_line_string (w, area, &x, &y, &charpos,
25182 &object, &dx, &dy, &width, &height);
25183
25184 row = (area == ON_MODE_LINE
25185 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25186 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25187
25188 /* Find the glyph under the mouse pointer. */
25189 if (row->mode_line_p && row->enabled_p)
25190 {
25191 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25192 end = glyph + row->used[TEXT_AREA];
25193
25194 for (x0 = original_x_pixel;
25195 glyph < end && x0 >= glyph->pixel_width;
25196 ++glyph)
25197 x0 -= glyph->pixel_width;
25198
25199 if (glyph >= end)
25200 glyph = NULL;
25201 }
25202 }
25203 else
25204 {
25205 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25206 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25207 returns them in row/column units! */
25208 string = marginal_area_string (w, area, &x, &y, &charpos,
25209 &object, &dx, &dy, &width, &height);
25210 }
25211
25212 help = Qnil;
25213
25214 #ifdef HAVE_WINDOW_SYSTEM
25215 if (IMAGEP (object))
25216 {
25217 Lisp_Object image_map, hotspot;
25218 if ((image_map = Fplist_get (XCDR (object), QCmap),
25219 !NILP (image_map))
25220 && (hotspot = find_hot_spot (image_map, dx, dy),
25221 CONSP (hotspot))
25222 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25223 {
25224 Lisp_Object plist;
25225
25226 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25227 If so, we could look for mouse-enter, mouse-leave
25228 properties in PLIST (and do something...). */
25229 hotspot = XCDR (hotspot);
25230 if (CONSP (hotspot)
25231 && (plist = XCAR (hotspot), CONSP (plist)))
25232 {
25233 pointer = Fplist_get (plist, Qpointer);
25234 if (NILP (pointer))
25235 pointer = Qhand;
25236 help = Fplist_get (plist, Qhelp_echo);
25237 if (!NILP (help))
25238 {
25239 help_echo_string = help;
25240 /* Is this correct? ++kfs */
25241 XSETWINDOW (help_echo_window, w);
25242 help_echo_object = w->buffer;
25243 help_echo_pos = charpos;
25244 }
25245 }
25246 }
25247 if (NILP (pointer))
25248 pointer = Fplist_get (XCDR (object), QCpointer);
25249 }
25250 #endif /* HAVE_WINDOW_SYSTEM */
25251
25252 if (STRINGP (string))
25253 {
25254 pos = make_number (charpos);
25255 /* If we're on a string with `help-echo' text property, arrange
25256 for the help to be displayed. This is done by setting the
25257 global variable help_echo_string to the help string. */
25258 if (NILP (help))
25259 {
25260 help = Fget_text_property (pos, Qhelp_echo, string);
25261 if (!NILP (help))
25262 {
25263 help_echo_string = help;
25264 XSETWINDOW (help_echo_window, w);
25265 help_echo_object = string;
25266 help_echo_pos = charpos;
25267 }
25268 }
25269
25270 #ifdef HAVE_WINDOW_SYSTEM
25271 if (FRAME_WINDOW_P (f))
25272 {
25273 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25274 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25275 if (NILP (pointer))
25276 pointer = Fget_text_property (pos, Qpointer, string);
25277
25278 /* Change the mouse pointer according to what is under X/Y. */
25279 if (NILP (pointer)
25280 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25281 {
25282 Lisp_Object map;
25283 map = Fget_text_property (pos, Qlocal_map, string);
25284 if (!KEYMAPP (map))
25285 map = Fget_text_property (pos, Qkeymap, string);
25286 if (!KEYMAPP (map))
25287 cursor = dpyinfo->vertical_scroll_bar_cursor;
25288 }
25289 }
25290 #endif
25291
25292 /* Change the mouse face according to what is under X/Y. */
25293 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25294 if (!NILP (mouse_face)
25295 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25296 && glyph)
25297 {
25298 Lisp_Object b, e;
25299
25300 struct glyph * tmp_glyph;
25301
25302 int gpos;
25303 int gseq_length;
25304 int total_pixel_width;
25305 EMACS_INT begpos, endpos, ignore;
25306
25307 int vpos, hpos;
25308
25309 b = Fprevious_single_property_change (make_number (charpos + 1),
25310 Qmouse_face, string, Qnil);
25311 if (NILP (b))
25312 begpos = 0;
25313 else
25314 begpos = XINT (b);
25315
25316 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25317 if (NILP (e))
25318 endpos = SCHARS (string);
25319 else
25320 endpos = XINT (e);
25321
25322 /* Calculate the glyph position GPOS of GLYPH in the
25323 displayed string, relative to the beginning of the
25324 highlighted part of the string.
25325
25326 Note: GPOS is different from CHARPOS. CHARPOS is the
25327 position of GLYPH in the internal string object. A mode
25328 line string format has structures which are converted to
25329 a flattened string by the Emacs Lisp interpreter. The
25330 internal string is an element of those structures. The
25331 displayed string is the flattened string. */
25332 tmp_glyph = row_start_glyph;
25333 while (tmp_glyph < glyph
25334 && (!(EQ (tmp_glyph->object, glyph->object)
25335 && begpos <= tmp_glyph->charpos
25336 && tmp_glyph->charpos < endpos)))
25337 tmp_glyph++;
25338 gpos = glyph - tmp_glyph;
25339
25340 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25341 the highlighted part of the displayed string to which
25342 GLYPH belongs. Note: GSEQ_LENGTH is different from
25343 SCHARS (STRING), because the latter returns the length of
25344 the internal string. */
25345 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25346 tmp_glyph > glyph
25347 && (!(EQ (tmp_glyph->object, glyph->object)
25348 && begpos <= tmp_glyph->charpos
25349 && tmp_glyph->charpos < endpos));
25350 tmp_glyph--)
25351 ;
25352 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25353
25354 /* Calculate the total pixel width of all the glyphs between
25355 the beginning of the highlighted area and GLYPH. */
25356 total_pixel_width = 0;
25357 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25358 total_pixel_width += tmp_glyph->pixel_width;
25359
25360 /* Pre calculation of re-rendering position. Note: X is in
25361 column units here, after the call to mode_line_string or
25362 marginal_area_string. */
25363 hpos = x - gpos;
25364 vpos = (area == ON_MODE_LINE
25365 ? (w->current_matrix)->nrows - 1
25366 : 0);
25367
25368 /* If GLYPH's position is included in the region that is
25369 already drawn in mouse face, we have nothing to do. */
25370 if ( EQ (window, hlinfo->mouse_face_window)
25371 && (!row->reversed_p
25372 ? (hlinfo->mouse_face_beg_col <= hpos
25373 && hpos < hlinfo->mouse_face_end_col)
25374 /* In R2L rows we swap BEG and END, see below. */
25375 : (hlinfo->mouse_face_end_col <= hpos
25376 && hpos < hlinfo->mouse_face_beg_col))
25377 && hlinfo->mouse_face_beg_row == vpos )
25378 return;
25379
25380 if (clear_mouse_face (hlinfo))
25381 cursor = No_Cursor;
25382
25383 if (!row->reversed_p)
25384 {
25385 hlinfo->mouse_face_beg_col = hpos;
25386 hlinfo->mouse_face_beg_x = original_x_pixel
25387 - (total_pixel_width + dx);
25388 hlinfo->mouse_face_end_col = hpos + gseq_length;
25389 hlinfo->mouse_face_end_x = 0;
25390 }
25391 else
25392 {
25393 /* In R2L rows, show_mouse_face expects BEG and END
25394 coordinates to be swapped. */
25395 hlinfo->mouse_face_end_col = hpos;
25396 hlinfo->mouse_face_end_x = original_x_pixel
25397 - (total_pixel_width + dx);
25398 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25399 hlinfo->mouse_face_beg_x = 0;
25400 }
25401
25402 hlinfo->mouse_face_beg_row = vpos;
25403 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25404 hlinfo->mouse_face_beg_y = 0;
25405 hlinfo->mouse_face_end_y = 0;
25406 hlinfo->mouse_face_past_end = 0;
25407 hlinfo->mouse_face_window = window;
25408
25409 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25410 charpos,
25411 0, 0, 0,
25412 &ignore,
25413 glyph->face_id,
25414 1);
25415 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25416
25417 if (NILP (pointer))
25418 pointer = Qhand;
25419 }
25420 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25421 clear_mouse_face (hlinfo);
25422 }
25423 #ifdef HAVE_WINDOW_SYSTEM
25424 if (FRAME_WINDOW_P (f))
25425 define_frame_cursor1 (f, cursor, pointer);
25426 #endif
25427 }
25428
25429
25430 /* EXPORT:
25431 Take proper action when the mouse has moved to position X, Y on
25432 frame F as regards highlighting characters that have mouse-face
25433 properties. Also de-highlighting chars where the mouse was before.
25434 X and Y can be negative or out of range. */
25435
25436 void
25437 note_mouse_highlight (struct frame *f, int x, int y)
25438 {
25439 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25440 enum window_part part;
25441 Lisp_Object window;
25442 struct window *w;
25443 Cursor cursor = No_Cursor;
25444 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25445 struct buffer *b;
25446
25447 /* When a menu is active, don't highlight because this looks odd. */
25448 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25449 if (popup_activated ())
25450 return;
25451 #endif
25452
25453 if (NILP (Vmouse_highlight)
25454 || !f->glyphs_initialized_p
25455 || f->pointer_invisible)
25456 return;
25457
25458 hlinfo->mouse_face_mouse_x = x;
25459 hlinfo->mouse_face_mouse_y = y;
25460 hlinfo->mouse_face_mouse_frame = f;
25461
25462 if (hlinfo->mouse_face_defer)
25463 return;
25464
25465 if (gc_in_progress)
25466 {
25467 hlinfo->mouse_face_deferred_gc = 1;
25468 return;
25469 }
25470
25471 /* Which window is that in? */
25472 window = window_from_coordinates (f, x, y, &part, 1);
25473
25474 /* If we were displaying active text in another window, clear that.
25475 Also clear if we move out of text area in same window. */
25476 if (! EQ (window, hlinfo->mouse_face_window)
25477 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25478 && !NILP (hlinfo->mouse_face_window)))
25479 clear_mouse_face (hlinfo);
25480
25481 /* Not on a window -> return. */
25482 if (!WINDOWP (window))
25483 return;
25484
25485 /* Reset help_echo_string. It will get recomputed below. */
25486 help_echo_string = Qnil;
25487
25488 /* Convert to window-relative pixel coordinates. */
25489 w = XWINDOW (window);
25490 frame_to_window_pixel_xy (w, &x, &y);
25491
25492 #ifdef HAVE_WINDOW_SYSTEM
25493 /* Handle tool-bar window differently since it doesn't display a
25494 buffer. */
25495 if (EQ (window, f->tool_bar_window))
25496 {
25497 note_tool_bar_highlight (f, x, y);
25498 return;
25499 }
25500 #endif
25501
25502 /* Mouse is on the mode, header line or margin? */
25503 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25504 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25505 {
25506 note_mode_line_or_margin_highlight (window, x, y, part);
25507 return;
25508 }
25509
25510 #ifdef HAVE_WINDOW_SYSTEM
25511 if (part == ON_VERTICAL_BORDER)
25512 {
25513 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25514 help_echo_string = build_string ("drag-mouse-1: resize");
25515 }
25516 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25517 || part == ON_SCROLL_BAR)
25518 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25519 else
25520 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25521 #endif
25522
25523 /* Are we in a window whose display is up to date?
25524 And verify the buffer's text has not changed. */
25525 b = XBUFFER (w->buffer);
25526 if (part == ON_TEXT
25527 && EQ (w->window_end_valid, w->buffer)
25528 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25529 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25530 {
25531 int hpos, vpos, i, dx, dy, area;
25532 EMACS_INT pos;
25533 struct glyph *glyph;
25534 Lisp_Object object;
25535 Lisp_Object mouse_face = Qnil, position;
25536 Lisp_Object *overlay_vec = NULL;
25537 int noverlays;
25538 struct buffer *obuf;
25539 EMACS_INT obegv, ozv;
25540 int same_region;
25541
25542 /* Find the glyph under X/Y. */
25543 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25544
25545 #ifdef HAVE_WINDOW_SYSTEM
25546 /* Look for :pointer property on image. */
25547 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25548 {
25549 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25550 if (img != NULL && IMAGEP (img->spec))
25551 {
25552 Lisp_Object image_map, hotspot;
25553 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25554 !NILP (image_map))
25555 && (hotspot = find_hot_spot (image_map,
25556 glyph->slice.img.x + dx,
25557 glyph->slice.img.y + dy),
25558 CONSP (hotspot))
25559 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25560 {
25561 Lisp_Object plist;
25562
25563 /* Could check XCAR (hotspot) to see if we enter/leave
25564 this hot-spot.
25565 If so, we could look for mouse-enter, mouse-leave
25566 properties in PLIST (and do something...). */
25567 hotspot = XCDR (hotspot);
25568 if (CONSP (hotspot)
25569 && (plist = XCAR (hotspot), CONSP (plist)))
25570 {
25571 pointer = Fplist_get (plist, Qpointer);
25572 if (NILP (pointer))
25573 pointer = Qhand;
25574 help_echo_string = Fplist_get (plist, Qhelp_echo);
25575 if (!NILP (help_echo_string))
25576 {
25577 help_echo_window = window;
25578 help_echo_object = glyph->object;
25579 help_echo_pos = glyph->charpos;
25580 }
25581 }
25582 }
25583 if (NILP (pointer))
25584 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25585 }
25586 }
25587 #endif /* HAVE_WINDOW_SYSTEM */
25588
25589 /* Clear mouse face if X/Y not over text. */
25590 if (glyph == NULL
25591 || area != TEXT_AREA
25592 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25593 /* Glyph's OBJECT is an integer for glyphs inserted by the
25594 display engine for its internal purposes, like truncation
25595 and continuation glyphs and blanks beyond the end of
25596 line's text on text terminals. If we are over such a
25597 glyph, we are not over any text. */
25598 || INTEGERP (glyph->object)
25599 /* R2L rows have a stretch glyph at their front, which
25600 stands for no text, whereas L2R rows have no glyphs at
25601 all beyond the end of text. Treat such stretch glyphs
25602 like we do with NULL glyphs in L2R rows. */
25603 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25604 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25605 && glyph->type == STRETCH_GLYPH
25606 && glyph->avoid_cursor_p))
25607 {
25608 if (clear_mouse_face (hlinfo))
25609 cursor = No_Cursor;
25610 #ifdef HAVE_WINDOW_SYSTEM
25611 if (FRAME_WINDOW_P (f) && NILP (pointer))
25612 {
25613 if (area != TEXT_AREA)
25614 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25615 else
25616 pointer = Vvoid_text_area_pointer;
25617 }
25618 #endif
25619 goto set_cursor;
25620 }
25621
25622 pos = glyph->charpos;
25623 object = glyph->object;
25624 if (!STRINGP (object) && !BUFFERP (object))
25625 goto set_cursor;
25626
25627 /* If we get an out-of-range value, return now; avoid an error. */
25628 if (BUFFERP (object) && pos > BUF_Z (b))
25629 goto set_cursor;
25630
25631 /* Make the window's buffer temporarily current for
25632 overlays_at and compute_char_face. */
25633 obuf = current_buffer;
25634 current_buffer = b;
25635 obegv = BEGV;
25636 ozv = ZV;
25637 BEGV = BEG;
25638 ZV = Z;
25639
25640 /* Is this char mouse-active or does it have help-echo? */
25641 position = make_number (pos);
25642
25643 if (BUFFERP (object))
25644 {
25645 /* Put all the overlays we want in a vector in overlay_vec. */
25646 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25647 /* Sort overlays into increasing priority order. */
25648 noverlays = sort_overlays (overlay_vec, noverlays, w);
25649 }
25650 else
25651 noverlays = 0;
25652
25653 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25654
25655 if (same_region)
25656 cursor = No_Cursor;
25657
25658 /* Check mouse-face highlighting. */
25659 if (! same_region
25660 /* If there exists an overlay with mouse-face overlapping
25661 the one we are currently highlighting, we have to
25662 check if we enter the overlapping overlay, and then
25663 highlight only that. */
25664 || (OVERLAYP (hlinfo->mouse_face_overlay)
25665 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25666 {
25667 /* Find the highest priority overlay with a mouse-face. */
25668 Lisp_Object overlay = Qnil;
25669 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25670 {
25671 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25672 if (!NILP (mouse_face))
25673 overlay = overlay_vec[i];
25674 }
25675
25676 /* If we're highlighting the same overlay as before, there's
25677 no need to do that again. */
25678 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25679 goto check_help_echo;
25680 hlinfo->mouse_face_overlay = overlay;
25681
25682 /* Clear the display of the old active region, if any. */
25683 if (clear_mouse_face (hlinfo))
25684 cursor = No_Cursor;
25685
25686 /* If no overlay applies, get a text property. */
25687 if (NILP (overlay))
25688 mouse_face = Fget_text_property (position, Qmouse_face, object);
25689
25690 /* Next, compute the bounds of the mouse highlighting and
25691 display it. */
25692 if (!NILP (mouse_face) && STRINGP (object))
25693 {
25694 /* The mouse-highlighting comes from a display string
25695 with a mouse-face. */
25696 Lisp_Object s, e;
25697 EMACS_INT ignore;
25698
25699 s = Fprevious_single_property_change
25700 (make_number (pos + 1), Qmouse_face, object, Qnil);
25701 e = Fnext_single_property_change
25702 (position, Qmouse_face, object, Qnil);
25703 if (NILP (s))
25704 s = make_number (0);
25705 if (NILP (e))
25706 e = make_number (SCHARS (object) - 1);
25707 mouse_face_from_string_pos (w, hlinfo, object,
25708 XINT (s), XINT (e));
25709 hlinfo->mouse_face_past_end = 0;
25710 hlinfo->mouse_face_window = window;
25711 hlinfo->mouse_face_face_id
25712 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25713 glyph->face_id, 1);
25714 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25715 cursor = No_Cursor;
25716 }
25717 else
25718 {
25719 /* The mouse-highlighting, if any, comes from an overlay
25720 or text property in the buffer. */
25721 Lisp_Object buffer IF_LINT (= Qnil);
25722 Lisp_Object cover_string IF_LINT (= Qnil);
25723
25724 if (STRINGP (object))
25725 {
25726 /* If we are on a display string with no mouse-face,
25727 check if the text under it has one. */
25728 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25729 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25730 pos = string_buffer_position (object, start);
25731 if (pos > 0)
25732 {
25733 mouse_face = get_char_property_and_overlay
25734 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25735 buffer = w->buffer;
25736 cover_string = object;
25737 }
25738 }
25739 else
25740 {
25741 buffer = object;
25742 cover_string = Qnil;
25743 }
25744
25745 if (!NILP (mouse_face))
25746 {
25747 Lisp_Object before, after;
25748 Lisp_Object before_string, after_string;
25749 /* To correctly find the limits of mouse highlight
25750 in a bidi-reordered buffer, we must not use the
25751 optimization of limiting the search in
25752 previous-single-property-change and
25753 next-single-property-change, because
25754 rows_from_pos_range needs the real start and end
25755 positions to DTRT in this case. That's because
25756 the first row visible in a window does not
25757 necessarily display the character whose position
25758 is the smallest. */
25759 Lisp_Object lim1 =
25760 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
25761 ? Fmarker_position (w->start)
25762 : Qnil;
25763 Lisp_Object lim2 =
25764 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
25765 ? make_number (BUF_Z (XBUFFER (buffer))
25766 - XFASTINT (w->window_end_pos))
25767 : Qnil;
25768
25769 if (NILP (overlay))
25770 {
25771 /* Handle the text property case. */
25772 before = Fprevious_single_property_change
25773 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25774 after = Fnext_single_property_change
25775 (make_number (pos), Qmouse_face, buffer, lim2);
25776 before_string = after_string = Qnil;
25777 }
25778 else
25779 {
25780 /* Handle the overlay case. */
25781 before = Foverlay_start (overlay);
25782 after = Foverlay_end (overlay);
25783 before_string = Foverlay_get (overlay, Qbefore_string);
25784 after_string = Foverlay_get (overlay, Qafter_string);
25785
25786 if (!STRINGP (before_string)) before_string = Qnil;
25787 if (!STRINGP (after_string)) after_string = Qnil;
25788 }
25789
25790 mouse_face_from_buffer_pos (window, hlinfo, pos,
25791 XFASTINT (before),
25792 XFASTINT (after),
25793 before_string, after_string,
25794 cover_string);
25795 cursor = No_Cursor;
25796 }
25797 }
25798 }
25799
25800 check_help_echo:
25801
25802 /* Look for a `help-echo' property. */
25803 if (NILP (help_echo_string)) {
25804 Lisp_Object help, overlay;
25805
25806 /* Check overlays first. */
25807 help = overlay = Qnil;
25808 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25809 {
25810 overlay = overlay_vec[i];
25811 help = Foverlay_get (overlay, Qhelp_echo);
25812 }
25813
25814 if (!NILP (help))
25815 {
25816 help_echo_string = help;
25817 help_echo_window = window;
25818 help_echo_object = overlay;
25819 help_echo_pos = pos;
25820 }
25821 else
25822 {
25823 Lisp_Object obj = glyph->object;
25824 EMACS_INT charpos = glyph->charpos;
25825
25826 /* Try text properties. */
25827 if (STRINGP (obj)
25828 && charpos >= 0
25829 && charpos < SCHARS (obj))
25830 {
25831 help = Fget_text_property (make_number (charpos),
25832 Qhelp_echo, obj);
25833 if (NILP (help))
25834 {
25835 /* If the string itself doesn't specify a help-echo,
25836 see if the buffer text ``under'' it does. */
25837 struct glyph_row *r
25838 = MATRIX_ROW (w->current_matrix, vpos);
25839 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25840 EMACS_INT p = string_buffer_position (obj, start);
25841 if (p > 0)
25842 {
25843 help = Fget_char_property (make_number (p),
25844 Qhelp_echo, w->buffer);
25845 if (!NILP (help))
25846 {
25847 charpos = p;
25848 obj = w->buffer;
25849 }
25850 }
25851 }
25852 }
25853 else if (BUFFERP (obj)
25854 && charpos >= BEGV
25855 && charpos < ZV)
25856 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25857 obj);
25858
25859 if (!NILP (help))
25860 {
25861 help_echo_string = help;
25862 help_echo_window = window;
25863 help_echo_object = obj;
25864 help_echo_pos = charpos;
25865 }
25866 }
25867 }
25868
25869 #ifdef HAVE_WINDOW_SYSTEM
25870 /* Look for a `pointer' property. */
25871 if (FRAME_WINDOW_P (f) && NILP (pointer))
25872 {
25873 /* Check overlays first. */
25874 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25875 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25876
25877 if (NILP (pointer))
25878 {
25879 Lisp_Object obj = glyph->object;
25880 EMACS_INT charpos = glyph->charpos;
25881
25882 /* Try text properties. */
25883 if (STRINGP (obj)
25884 && charpos >= 0
25885 && charpos < SCHARS (obj))
25886 {
25887 pointer = Fget_text_property (make_number (charpos),
25888 Qpointer, obj);
25889 if (NILP (pointer))
25890 {
25891 /* If the string itself doesn't specify a pointer,
25892 see if the buffer text ``under'' it does. */
25893 struct glyph_row *r
25894 = MATRIX_ROW (w->current_matrix, vpos);
25895 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25896 EMACS_INT p = string_buffer_position (obj, start);
25897 if (p > 0)
25898 pointer = Fget_char_property (make_number (p),
25899 Qpointer, w->buffer);
25900 }
25901 }
25902 else if (BUFFERP (obj)
25903 && charpos >= BEGV
25904 && charpos < ZV)
25905 pointer = Fget_text_property (make_number (charpos),
25906 Qpointer, obj);
25907 }
25908 }
25909 #endif /* HAVE_WINDOW_SYSTEM */
25910
25911 BEGV = obegv;
25912 ZV = ozv;
25913 current_buffer = obuf;
25914 }
25915
25916 set_cursor:
25917
25918 #ifdef HAVE_WINDOW_SYSTEM
25919 if (FRAME_WINDOW_P (f))
25920 define_frame_cursor1 (f, cursor, pointer);
25921 #else
25922 /* This is here to prevent a compiler error, about "label at end of
25923 compound statement". */
25924 return;
25925 #endif
25926 }
25927
25928
25929 /* EXPORT for RIF:
25930 Clear any mouse-face on window W. This function is part of the
25931 redisplay interface, and is called from try_window_id and similar
25932 functions to ensure the mouse-highlight is off. */
25933
25934 void
25935 x_clear_window_mouse_face (struct window *w)
25936 {
25937 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25938 Lisp_Object window;
25939
25940 BLOCK_INPUT;
25941 XSETWINDOW (window, w);
25942 if (EQ (window, hlinfo->mouse_face_window))
25943 clear_mouse_face (hlinfo);
25944 UNBLOCK_INPUT;
25945 }
25946
25947
25948 /* EXPORT:
25949 Just discard the mouse face information for frame F, if any.
25950 This is used when the size of F is changed. */
25951
25952 void
25953 cancel_mouse_face (struct frame *f)
25954 {
25955 Lisp_Object window;
25956 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25957
25958 window = hlinfo->mouse_face_window;
25959 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25960 {
25961 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25962 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25963 hlinfo->mouse_face_window = Qnil;
25964 }
25965 }
25966
25967
25968 \f
25969 /***********************************************************************
25970 Exposure Events
25971 ***********************************************************************/
25972
25973 #ifdef HAVE_WINDOW_SYSTEM
25974
25975 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25976 which intersects rectangle R. R is in window-relative coordinates. */
25977
25978 static void
25979 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25980 enum glyph_row_area area)
25981 {
25982 struct glyph *first = row->glyphs[area];
25983 struct glyph *end = row->glyphs[area] + row->used[area];
25984 struct glyph *last;
25985 int first_x, start_x, x;
25986
25987 if (area == TEXT_AREA && row->fill_line_p)
25988 /* If row extends face to end of line write the whole line. */
25989 draw_glyphs (w, 0, row, area,
25990 0, row->used[area],
25991 DRAW_NORMAL_TEXT, 0);
25992 else
25993 {
25994 /* Set START_X to the window-relative start position for drawing glyphs of
25995 AREA. The first glyph of the text area can be partially visible.
25996 The first glyphs of other areas cannot. */
25997 start_x = window_box_left_offset (w, area);
25998 x = start_x;
25999 if (area == TEXT_AREA)
26000 x += row->x;
26001
26002 /* Find the first glyph that must be redrawn. */
26003 while (first < end
26004 && x + first->pixel_width < r->x)
26005 {
26006 x += first->pixel_width;
26007 ++first;
26008 }
26009
26010 /* Find the last one. */
26011 last = first;
26012 first_x = x;
26013 while (last < end
26014 && x < r->x + r->width)
26015 {
26016 x += last->pixel_width;
26017 ++last;
26018 }
26019
26020 /* Repaint. */
26021 if (last > first)
26022 draw_glyphs (w, first_x - start_x, row, area,
26023 first - row->glyphs[area], last - row->glyphs[area],
26024 DRAW_NORMAL_TEXT, 0);
26025 }
26026 }
26027
26028
26029 /* Redraw the parts of the glyph row ROW on window W intersecting
26030 rectangle R. R is in window-relative coordinates. Value is
26031 non-zero if mouse-face was overwritten. */
26032
26033 static int
26034 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26035 {
26036 xassert (row->enabled_p);
26037
26038 if (row->mode_line_p || w->pseudo_window_p)
26039 draw_glyphs (w, 0, row, TEXT_AREA,
26040 0, row->used[TEXT_AREA],
26041 DRAW_NORMAL_TEXT, 0);
26042 else
26043 {
26044 if (row->used[LEFT_MARGIN_AREA])
26045 expose_area (w, row, r, LEFT_MARGIN_AREA);
26046 if (row->used[TEXT_AREA])
26047 expose_area (w, row, r, TEXT_AREA);
26048 if (row->used[RIGHT_MARGIN_AREA])
26049 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26050 draw_row_fringe_bitmaps (w, row);
26051 }
26052
26053 return row->mouse_face_p;
26054 }
26055
26056
26057 /* Redraw those parts of glyphs rows during expose event handling that
26058 overlap other rows. Redrawing of an exposed line writes over parts
26059 of lines overlapping that exposed line; this function fixes that.
26060
26061 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26062 row in W's current matrix that is exposed and overlaps other rows.
26063 LAST_OVERLAPPING_ROW is the last such row. */
26064
26065 static void
26066 expose_overlaps (struct window *w,
26067 struct glyph_row *first_overlapping_row,
26068 struct glyph_row *last_overlapping_row,
26069 XRectangle *r)
26070 {
26071 struct glyph_row *row;
26072
26073 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26074 if (row->overlapping_p)
26075 {
26076 xassert (row->enabled_p && !row->mode_line_p);
26077
26078 row->clip = r;
26079 if (row->used[LEFT_MARGIN_AREA])
26080 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26081
26082 if (row->used[TEXT_AREA])
26083 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26084
26085 if (row->used[RIGHT_MARGIN_AREA])
26086 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26087 row->clip = NULL;
26088 }
26089 }
26090
26091
26092 /* Return non-zero if W's cursor intersects rectangle R. */
26093
26094 static int
26095 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26096 {
26097 XRectangle cr, result;
26098 struct glyph *cursor_glyph;
26099 struct glyph_row *row;
26100
26101 if (w->phys_cursor.vpos >= 0
26102 && w->phys_cursor.vpos < w->current_matrix->nrows
26103 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26104 row->enabled_p)
26105 && row->cursor_in_fringe_p)
26106 {
26107 /* Cursor is in the fringe. */
26108 cr.x = window_box_right_offset (w,
26109 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26110 ? RIGHT_MARGIN_AREA
26111 : TEXT_AREA));
26112 cr.y = row->y;
26113 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26114 cr.height = row->height;
26115 return x_intersect_rectangles (&cr, r, &result);
26116 }
26117
26118 cursor_glyph = get_phys_cursor_glyph (w);
26119 if (cursor_glyph)
26120 {
26121 /* r is relative to W's box, but w->phys_cursor.x is relative
26122 to left edge of W's TEXT area. Adjust it. */
26123 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26124 cr.y = w->phys_cursor.y;
26125 cr.width = cursor_glyph->pixel_width;
26126 cr.height = w->phys_cursor_height;
26127 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26128 I assume the effect is the same -- and this is portable. */
26129 return x_intersect_rectangles (&cr, r, &result);
26130 }
26131 /* If we don't understand the format, pretend we're not in the hot-spot. */
26132 return 0;
26133 }
26134
26135
26136 /* EXPORT:
26137 Draw a vertical window border to the right of window W if W doesn't
26138 have vertical scroll bars. */
26139
26140 void
26141 x_draw_vertical_border (struct window *w)
26142 {
26143 struct frame *f = XFRAME (WINDOW_FRAME (w));
26144
26145 /* We could do better, if we knew what type of scroll-bar the adjacent
26146 windows (on either side) have... But we don't :-(
26147 However, I think this works ok. ++KFS 2003-04-25 */
26148
26149 /* Redraw borders between horizontally adjacent windows. Don't
26150 do it for frames with vertical scroll bars because either the
26151 right scroll bar of a window, or the left scroll bar of its
26152 neighbor will suffice as a border. */
26153 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26154 return;
26155
26156 if (!WINDOW_RIGHTMOST_P (w)
26157 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26158 {
26159 int x0, x1, y0, y1;
26160
26161 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26162 y1 -= 1;
26163
26164 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26165 x1 -= 1;
26166
26167 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26168 }
26169 else if (!WINDOW_LEFTMOST_P (w)
26170 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26171 {
26172 int x0, x1, y0, y1;
26173
26174 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26175 y1 -= 1;
26176
26177 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26178 x0 -= 1;
26179
26180 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26181 }
26182 }
26183
26184
26185 /* Redraw the part of window W intersection rectangle FR. Pixel
26186 coordinates in FR are frame-relative. Call this function with
26187 input blocked. Value is non-zero if the exposure overwrites
26188 mouse-face. */
26189
26190 static int
26191 expose_window (struct window *w, XRectangle *fr)
26192 {
26193 struct frame *f = XFRAME (w->frame);
26194 XRectangle wr, r;
26195 int mouse_face_overwritten_p = 0;
26196
26197 /* If window is not yet fully initialized, do nothing. This can
26198 happen when toolkit scroll bars are used and a window is split.
26199 Reconfiguring the scroll bar will generate an expose for a newly
26200 created window. */
26201 if (w->current_matrix == NULL)
26202 return 0;
26203
26204 /* When we're currently updating the window, display and current
26205 matrix usually don't agree. Arrange for a thorough display
26206 later. */
26207 if (w == updated_window)
26208 {
26209 SET_FRAME_GARBAGED (f);
26210 return 0;
26211 }
26212
26213 /* Frame-relative pixel rectangle of W. */
26214 wr.x = WINDOW_LEFT_EDGE_X (w);
26215 wr.y = WINDOW_TOP_EDGE_Y (w);
26216 wr.width = WINDOW_TOTAL_WIDTH (w);
26217 wr.height = WINDOW_TOTAL_HEIGHT (w);
26218
26219 if (x_intersect_rectangles (fr, &wr, &r))
26220 {
26221 int yb = window_text_bottom_y (w);
26222 struct glyph_row *row;
26223 int cursor_cleared_p;
26224 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26225
26226 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26227 r.x, r.y, r.width, r.height));
26228
26229 /* Convert to window coordinates. */
26230 r.x -= WINDOW_LEFT_EDGE_X (w);
26231 r.y -= WINDOW_TOP_EDGE_Y (w);
26232
26233 /* Turn off the cursor. */
26234 if (!w->pseudo_window_p
26235 && phys_cursor_in_rect_p (w, &r))
26236 {
26237 x_clear_cursor (w);
26238 cursor_cleared_p = 1;
26239 }
26240 else
26241 cursor_cleared_p = 0;
26242
26243 /* Update lines intersecting rectangle R. */
26244 first_overlapping_row = last_overlapping_row = NULL;
26245 for (row = w->current_matrix->rows;
26246 row->enabled_p;
26247 ++row)
26248 {
26249 int y0 = row->y;
26250 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26251
26252 if ((y0 >= r.y && y0 < r.y + r.height)
26253 || (y1 > r.y && y1 < r.y + r.height)
26254 || (r.y >= y0 && r.y < y1)
26255 || (r.y + r.height > y0 && r.y + r.height < y1))
26256 {
26257 /* A header line may be overlapping, but there is no need
26258 to fix overlapping areas for them. KFS 2005-02-12 */
26259 if (row->overlapping_p && !row->mode_line_p)
26260 {
26261 if (first_overlapping_row == NULL)
26262 first_overlapping_row = row;
26263 last_overlapping_row = row;
26264 }
26265
26266 row->clip = fr;
26267 if (expose_line (w, row, &r))
26268 mouse_face_overwritten_p = 1;
26269 row->clip = NULL;
26270 }
26271 else if (row->overlapping_p)
26272 {
26273 /* We must redraw a row overlapping the exposed area. */
26274 if (y0 < r.y
26275 ? y0 + row->phys_height > r.y
26276 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26277 {
26278 if (first_overlapping_row == NULL)
26279 first_overlapping_row = row;
26280 last_overlapping_row = row;
26281 }
26282 }
26283
26284 if (y1 >= yb)
26285 break;
26286 }
26287
26288 /* Display the mode line if there is one. */
26289 if (WINDOW_WANTS_MODELINE_P (w)
26290 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26291 row->enabled_p)
26292 && row->y < r.y + r.height)
26293 {
26294 if (expose_line (w, row, &r))
26295 mouse_face_overwritten_p = 1;
26296 }
26297
26298 if (!w->pseudo_window_p)
26299 {
26300 /* Fix the display of overlapping rows. */
26301 if (first_overlapping_row)
26302 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26303 fr);
26304
26305 /* Draw border between windows. */
26306 x_draw_vertical_border (w);
26307
26308 /* Turn the cursor on again. */
26309 if (cursor_cleared_p)
26310 update_window_cursor (w, 1);
26311 }
26312 }
26313
26314 return mouse_face_overwritten_p;
26315 }
26316
26317
26318
26319 /* Redraw (parts) of all windows in the window tree rooted at W that
26320 intersect R. R contains frame pixel coordinates. Value is
26321 non-zero if the exposure overwrites mouse-face. */
26322
26323 static int
26324 expose_window_tree (struct window *w, XRectangle *r)
26325 {
26326 struct frame *f = XFRAME (w->frame);
26327 int mouse_face_overwritten_p = 0;
26328
26329 while (w && !FRAME_GARBAGED_P (f))
26330 {
26331 if (!NILP (w->hchild))
26332 mouse_face_overwritten_p
26333 |= expose_window_tree (XWINDOW (w->hchild), r);
26334 else if (!NILP (w->vchild))
26335 mouse_face_overwritten_p
26336 |= expose_window_tree (XWINDOW (w->vchild), r);
26337 else
26338 mouse_face_overwritten_p |= expose_window (w, r);
26339
26340 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26341 }
26342
26343 return mouse_face_overwritten_p;
26344 }
26345
26346
26347 /* EXPORT:
26348 Redisplay an exposed area of frame F. X and Y are the upper-left
26349 corner of the exposed rectangle. W and H are width and height of
26350 the exposed area. All are pixel values. W or H zero means redraw
26351 the entire frame. */
26352
26353 void
26354 expose_frame (struct frame *f, int x, int y, int w, int h)
26355 {
26356 XRectangle r;
26357 int mouse_face_overwritten_p = 0;
26358
26359 TRACE ((stderr, "expose_frame "));
26360
26361 /* No need to redraw if frame will be redrawn soon. */
26362 if (FRAME_GARBAGED_P (f))
26363 {
26364 TRACE ((stderr, " garbaged\n"));
26365 return;
26366 }
26367
26368 /* If basic faces haven't been realized yet, there is no point in
26369 trying to redraw anything. This can happen when we get an expose
26370 event while Emacs is starting, e.g. by moving another window. */
26371 if (FRAME_FACE_CACHE (f) == NULL
26372 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26373 {
26374 TRACE ((stderr, " no faces\n"));
26375 return;
26376 }
26377
26378 if (w == 0 || h == 0)
26379 {
26380 r.x = r.y = 0;
26381 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26382 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26383 }
26384 else
26385 {
26386 r.x = x;
26387 r.y = y;
26388 r.width = w;
26389 r.height = h;
26390 }
26391
26392 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26393 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26394
26395 if (WINDOWP (f->tool_bar_window))
26396 mouse_face_overwritten_p
26397 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26398
26399 #ifdef HAVE_X_WINDOWS
26400 #ifndef MSDOS
26401 #ifndef USE_X_TOOLKIT
26402 if (WINDOWP (f->menu_bar_window))
26403 mouse_face_overwritten_p
26404 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26405 #endif /* not USE_X_TOOLKIT */
26406 #endif
26407 #endif
26408
26409 /* Some window managers support a focus-follows-mouse style with
26410 delayed raising of frames. Imagine a partially obscured frame,
26411 and moving the mouse into partially obscured mouse-face on that
26412 frame. The visible part of the mouse-face will be highlighted,
26413 then the WM raises the obscured frame. With at least one WM, KDE
26414 2.1, Emacs is not getting any event for the raising of the frame
26415 (even tried with SubstructureRedirectMask), only Expose events.
26416 These expose events will draw text normally, i.e. not
26417 highlighted. Which means we must redo the highlight here.
26418 Subsume it under ``we love X''. --gerd 2001-08-15 */
26419 /* Included in Windows version because Windows most likely does not
26420 do the right thing if any third party tool offers
26421 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26422 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26423 {
26424 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26425 if (f == hlinfo->mouse_face_mouse_frame)
26426 {
26427 int mouse_x = hlinfo->mouse_face_mouse_x;
26428 int mouse_y = hlinfo->mouse_face_mouse_y;
26429 clear_mouse_face (hlinfo);
26430 note_mouse_highlight (f, mouse_x, mouse_y);
26431 }
26432 }
26433 }
26434
26435
26436 /* EXPORT:
26437 Determine the intersection of two rectangles R1 and R2. Return
26438 the intersection in *RESULT. Value is non-zero if RESULT is not
26439 empty. */
26440
26441 int
26442 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26443 {
26444 XRectangle *left, *right;
26445 XRectangle *upper, *lower;
26446 int intersection_p = 0;
26447
26448 /* Rearrange so that R1 is the left-most rectangle. */
26449 if (r1->x < r2->x)
26450 left = r1, right = r2;
26451 else
26452 left = r2, right = r1;
26453
26454 /* X0 of the intersection is right.x0, if this is inside R1,
26455 otherwise there is no intersection. */
26456 if (right->x <= left->x + left->width)
26457 {
26458 result->x = right->x;
26459
26460 /* The right end of the intersection is the minimum of the
26461 the right ends of left and right. */
26462 result->width = (min (left->x + left->width, right->x + right->width)
26463 - result->x);
26464
26465 /* Same game for Y. */
26466 if (r1->y < r2->y)
26467 upper = r1, lower = r2;
26468 else
26469 upper = r2, lower = r1;
26470
26471 /* The upper end of the intersection is lower.y0, if this is inside
26472 of upper. Otherwise, there is no intersection. */
26473 if (lower->y <= upper->y + upper->height)
26474 {
26475 result->y = lower->y;
26476
26477 /* The lower end of the intersection is the minimum of the lower
26478 ends of upper and lower. */
26479 result->height = (min (lower->y + lower->height,
26480 upper->y + upper->height)
26481 - result->y);
26482 intersection_p = 1;
26483 }
26484 }
26485
26486 return intersection_p;
26487 }
26488
26489 #endif /* HAVE_WINDOW_SYSTEM */
26490
26491 \f
26492 /***********************************************************************
26493 Initialization
26494 ***********************************************************************/
26495
26496 void
26497 syms_of_xdisp (void)
26498 {
26499 Vwith_echo_area_save_vector = Qnil;
26500 staticpro (&Vwith_echo_area_save_vector);
26501
26502 Vmessage_stack = Qnil;
26503 staticpro (&Vmessage_stack);
26504
26505 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26506 staticpro (&Qinhibit_redisplay);
26507
26508 message_dolog_marker1 = Fmake_marker ();
26509 staticpro (&message_dolog_marker1);
26510 message_dolog_marker2 = Fmake_marker ();
26511 staticpro (&message_dolog_marker2);
26512 message_dolog_marker3 = Fmake_marker ();
26513 staticpro (&message_dolog_marker3);
26514
26515 #if GLYPH_DEBUG
26516 defsubr (&Sdump_frame_glyph_matrix);
26517 defsubr (&Sdump_glyph_matrix);
26518 defsubr (&Sdump_glyph_row);
26519 defsubr (&Sdump_tool_bar_row);
26520 defsubr (&Strace_redisplay);
26521 defsubr (&Strace_to_stderr);
26522 #endif
26523 #ifdef HAVE_WINDOW_SYSTEM
26524 defsubr (&Stool_bar_lines_needed);
26525 defsubr (&Slookup_image_map);
26526 #endif
26527 defsubr (&Sformat_mode_line);
26528 defsubr (&Sinvisible_p);
26529 defsubr (&Scurrent_bidi_paragraph_direction);
26530
26531 staticpro (&Qmenu_bar_update_hook);
26532 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26533
26534 staticpro (&Qoverriding_terminal_local_map);
26535 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26536
26537 staticpro (&Qoverriding_local_map);
26538 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26539
26540 staticpro (&Qwindow_scroll_functions);
26541 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26542
26543 staticpro (&Qwindow_text_change_functions);
26544 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26545
26546 staticpro (&Qredisplay_end_trigger_functions);
26547 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26548
26549 staticpro (&Qinhibit_point_motion_hooks);
26550 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26551
26552 Qeval = intern_c_string ("eval");
26553 staticpro (&Qeval);
26554
26555 QCdata = intern_c_string (":data");
26556 staticpro (&QCdata);
26557 Qdisplay = intern_c_string ("display");
26558 staticpro (&Qdisplay);
26559 Qspace_width = intern_c_string ("space-width");
26560 staticpro (&Qspace_width);
26561 Qraise = intern_c_string ("raise");
26562 staticpro (&Qraise);
26563 Qslice = intern_c_string ("slice");
26564 staticpro (&Qslice);
26565 Qspace = intern_c_string ("space");
26566 staticpro (&Qspace);
26567 Qmargin = intern_c_string ("margin");
26568 staticpro (&Qmargin);
26569 Qpointer = intern_c_string ("pointer");
26570 staticpro (&Qpointer);
26571 Qleft_margin = intern_c_string ("left-margin");
26572 staticpro (&Qleft_margin);
26573 Qright_margin = intern_c_string ("right-margin");
26574 staticpro (&Qright_margin);
26575 Qcenter = intern_c_string ("center");
26576 staticpro (&Qcenter);
26577 Qline_height = intern_c_string ("line-height");
26578 staticpro (&Qline_height);
26579 QCalign_to = intern_c_string (":align-to");
26580 staticpro (&QCalign_to);
26581 QCrelative_width = intern_c_string (":relative-width");
26582 staticpro (&QCrelative_width);
26583 QCrelative_height = intern_c_string (":relative-height");
26584 staticpro (&QCrelative_height);
26585 QCeval = intern_c_string (":eval");
26586 staticpro (&QCeval);
26587 QCpropertize = intern_c_string (":propertize");
26588 staticpro (&QCpropertize);
26589 QCfile = intern_c_string (":file");
26590 staticpro (&QCfile);
26591 Qfontified = intern_c_string ("fontified");
26592 staticpro (&Qfontified);
26593 Qfontification_functions = intern_c_string ("fontification-functions");
26594 staticpro (&Qfontification_functions);
26595 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26596 staticpro (&Qtrailing_whitespace);
26597 Qescape_glyph = intern_c_string ("escape-glyph");
26598 staticpro (&Qescape_glyph);
26599 Qnobreak_space = intern_c_string ("nobreak-space");
26600 staticpro (&Qnobreak_space);
26601 Qimage = intern_c_string ("image");
26602 staticpro (&Qimage);
26603 Qtext = intern_c_string ("text");
26604 staticpro (&Qtext);
26605 Qboth = intern_c_string ("both");
26606 staticpro (&Qboth);
26607 Qboth_horiz = intern_c_string ("both-horiz");
26608 staticpro (&Qboth_horiz);
26609 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26610 staticpro (&Qtext_image_horiz);
26611 QCmap = intern_c_string (":map");
26612 staticpro (&QCmap);
26613 QCpointer = intern_c_string (":pointer");
26614 staticpro (&QCpointer);
26615 Qrect = intern_c_string ("rect");
26616 staticpro (&Qrect);
26617 Qcircle = intern_c_string ("circle");
26618 staticpro (&Qcircle);
26619 Qpoly = intern_c_string ("poly");
26620 staticpro (&Qpoly);
26621 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26622 staticpro (&Qmessage_truncate_lines);
26623 Qgrow_only = intern_c_string ("grow-only");
26624 staticpro (&Qgrow_only);
26625 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26626 staticpro (&Qinhibit_menubar_update);
26627 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26628 staticpro (&Qinhibit_eval_during_redisplay);
26629 Qposition = intern_c_string ("position");
26630 staticpro (&Qposition);
26631 Qbuffer_position = intern_c_string ("buffer-position");
26632 staticpro (&Qbuffer_position);
26633 Qobject = intern_c_string ("object");
26634 staticpro (&Qobject);
26635 Qbar = intern_c_string ("bar");
26636 staticpro (&Qbar);
26637 Qhbar = intern_c_string ("hbar");
26638 staticpro (&Qhbar);
26639 Qbox = intern_c_string ("box");
26640 staticpro (&Qbox);
26641 Qhollow = intern_c_string ("hollow");
26642 staticpro (&Qhollow);
26643 Qhand = intern_c_string ("hand");
26644 staticpro (&Qhand);
26645 Qarrow = intern_c_string ("arrow");
26646 staticpro (&Qarrow);
26647 Qtext = intern_c_string ("text");
26648 staticpro (&Qtext);
26649 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26650 staticpro (&Qinhibit_free_realized_faces);
26651
26652 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26653 Fcons (intern_c_string ("void-variable"), Qnil)),
26654 Qnil);
26655 staticpro (&list_of_error);
26656
26657 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26658 staticpro (&Qlast_arrow_position);
26659 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26660 staticpro (&Qlast_arrow_string);
26661
26662 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26663 staticpro (&Qoverlay_arrow_string);
26664 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26665 staticpro (&Qoverlay_arrow_bitmap);
26666
26667 echo_buffer[0] = echo_buffer[1] = Qnil;
26668 staticpro (&echo_buffer[0]);
26669 staticpro (&echo_buffer[1]);
26670
26671 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26672 staticpro (&echo_area_buffer[0]);
26673 staticpro (&echo_area_buffer[1]);
26674
26675 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26676 staticpro (&Vmessages_buffer_name);
26677
26678 mode_line_proptrans_alist = Qnil;
26679 staticpro (&mode_line_proptrans_alist);
26680 mode_line_string_list = Qnil;
26681 staticpro (&mode_line_string_list);
26682 mode_line_string_face = Qnil;
26683 staticpro (&mode_line_string_face);
26684 mode_line_string_face_prop = Qnil;
26685 staticpro (&mode_line_string_face_prop);
26686 Vmode_line_unwind_vector = Qnil;
26687 staticpro (&Vmode_line_unwind_vector);
26688
26689 help_echo_string = Qnil;
26690 staticpro (&help_echo_string);
26691 help_echo_object = Qnil;
26692 staticpro (&help_echo_object);
26693 help_echo_window = Qnil;
26694 staticpro (&help_echo_window);
26695 previous_help_echo_string = Qnil;
26696 staticpro (&previous_help_echo_string);
26697 help_echo_pos = -1;
26698
26699 Qright_to_left = intern_c_string ("right-to-left");
26700 staticpro (&Qright_to_left);
26701 Qleft_to_right = intern_c_string ("left-to-right");
26702 staticpro (&Qleft_to_right);
26703
26704 #ifdef HAVE_WINDOW_SYSTEM
26705 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
26706 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26707 For example, if a block cursor is over a tab, it will be drawn as
26708 wide as that tab on the display. */);
26709 x_stretch_cursor_p = 0;
26710 #endif
26711
26712 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
26713 doc: /* *Non-nil means highlight trailing whitespace.
26714 The face used for trailing whitespace is `trailing-whitespace'. */);
26715 Vshow_trailing_whitespace = Qnil;
26716
26717 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
26718 doc: /* *Control highlighting of nobreak space and soft hyphen.
26719 A value of t means highlight the character itself (for nobreak space,
26720 use face `nobreak-space').
26721 A value of nil means no highlighting.
26722 Other values mean display the escape glyph followed by an ordinary
26723 space or ordinary hyphen. */);
26724 Vnobreak_char_display = Qt;
26725
26726 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
26727 doc: /* *The pointer shape to show in void text areas.
26728 A value of nil means to show the text pointer. Other options are `arrow',
26729 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26730 Vvoid_text_area_pointer = Qarrow;
26731
26732 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
26733 doc: /* Non-nil means don't actually do any redisplay.
26734 This is used for internal purposes. */);
26735 Vinhibit_redisplay = Qnil;
26736
26737 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
26738 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26739 Vglobal_mode_string = Qnil;
26740
26741 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
26742 doc: /* Marker for where to display an arrow on top of the buffer text.
26743 This must be the beginning of a line in order to work.
26744 See also `overlay-arrow-string'. */);
26745 Voverlay_arrow_position = Qnil;
26746
26747 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
26748 doc: /* String to display as an arrow in non-window frames.
26749 See also `overlay-arrow-position'. */);
26750 Voverlay_arrow_string = make_pure_c_string ("=>");
26751
26752 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
26753 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26754 The symbols on this list are examined during redisplay to determine
26755 where to display overlay arrows. */);
26756 Voverlay_arrow_variable_list
26757 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26758
26759 DEFVAR_INT ("scroll-step", emacs_scroll_step,
26760 doc: /* *The number of lines to try scrolling a window by when point moves out.
26761 If that fails to bring point back on frame, point is centered instead.
26762 If this is zero, point is always centered after it moves off frame.
26763 If you want scrolling to always be a line at a time, you should set
26764 `scroll-conservatively' to a large value rather than set this to 1. */);
26765
26766 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
26767 doc: /* *Scroll up to this many lines, to bring point back on screen.
26768 If point moves off-screen, redisplay will scroll by up to
26769 `scroll-conservatively' lines in order to bring point just barely
26770 onto the screen again. If that cannot be done, then redisplay
26771 recenters point as usual.
26772
26773 If the value is greater than 100, redisplay will never recenter point,
26774 but will always scroll just enough text to bring point into view, even
26775 if you move far away.
26776
26777 A value of zero means always recenter point if it moves off screen. */);
26778 scroll_conservatively = 0;
26779
26780 DEFVAR_INT ("scroll-margin", scroll_margin,
26781 doc: /* *Number of lines of margin at the top and bottom of a window.
26782 Recenter the window whenever point gets within this many lines
26783 of the top or bottom of the window. */);
26784 scroll_margin = 0;
26785
26786 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
26787 doc: /* Pixels per inch value for non-window system displays.
26788 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26789 Vdisplay_pixels_per_inch = make_float (72.0);
26790
26791 #if GLYPH_DEBUG
26792 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
26793 #endif
26794
26795 DEFVAR_LISP ("truncate-partial-width-windows",
26796 Vtruncate_partial_width_windows,
26797 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26798 For an integer value, truncate lines in each window narrower than the
26799 full frame width, provided the window width is less than that integer;
26800 otherwise, respect the value of `truncate-lines'.
26801
26802 For any other non-nil value, truncate lines in all windows that do
26803 not span the full frame width.
26804
26805 A value of nil means to respect the value of `truncate-lines'.
26806
26807 If `word-wrap' is enabled, you might want to reduce this. */);
26808 Vtruncate_partial_width_windows = make_number (50);
26809
26810 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
26811 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26812 Any other value means to use the appropriate face, `mode-line',
26813 `header-line', or `menu' respectively. */);
26814 mode_line_inverse_video = 1;
26815
26816 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
26817 doc: /* *Maximum buffer size for which line number should be displayed.
26818 If the buffer is bigger than this, the line number does not appear
26819 in the mode line. A value of nil means no limit. */);
26820 Vline_number_display_limit = Qnil;
26821
26822 DEFVAR_INT ("line-number-display-limit-width",
26823 line_number_display_limit_width,
26824 doc: /* *Maximum line width (in characters) for line number display.
26825 If the average length of the lines near point is bigger than this, then the
26826 line number may be omitted from the mode line. */);
26827 line_number_display_limit_width = 200;
26828
26829 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
26830 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26831 highlight_nonselected_windows = 0;
26832
26833 DEFVAR_BOOL ("multiple-frames", multiple_frames,
26834 doc: /* Non-nil if more than one frame is visible on this display.
26835 Minibuffer-only frames don't count, but iconified frames do.
26836 This variable is not guaranteed to be accurate except while processing
26837 `frame-title-format' and `icon-title-format'. */);
26838
26839 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
26840 doc: /* Template for displaying the title bar of visible frames.
26841 \(Assuming the window manager supports this feature.)
26842
26843 This variable has the same structure as `mode-line-format', except that
26844 the %c and %l constructs are ignored. It is used only on frames for
26845 which no explicit name has been set \(see `modify-frame-parameters'). */);
26846
26847 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
26848 doc: /* Template for displaying the title bar of an iconified frame.
26849 \(Assuming the window manager supports this feature.)
26850 This variable has the same structure as `mode-line-format' (which see),
26851 and is used only on frames for which no explicit name has been set
26852 \(see `modify-frame-parameters'). */);
26853 Vicon_title_format
26854 = Vframe_title_format
26855 = pure_cons (intern_c_string ("multiple-frames"),
26856 pure_cons (make_pure_c_string ("%b"),
26857 pure_cons (pure_cons (empty_unibyte_string,
26858 pure_cons (intern_c_string ("invocation-name"),
26859 pure_cons (make_pure_c_string ("@"),
26860 pure_cons (intern_c_string ("system-name"),
26861 Qnil)))),
26862 Qnil)));
26863
26864 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
26865 doc: /* Maximum number of lines to keep in the message log buffer.
26866 If nil, disable message logging. If t, log messages but don't truncate
26867 the buffer when it becomes large. */);
26868 Vmessage_log_max = make_number (100);
26869
26870 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
26871 doc: /* Functions called before redisplay, if window sizes have changed.
26872 The value should be a list of functions that take one argument.
26873 Just before redisplay, for each frame, if any of its windows have changed
26874 size since the last redisplay, or have been split or deleted,
26875 all the functions in the list are called, with the frame as argument. */);
26876 Vwindow_size_change_functions = Qnil;
26877
26878 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
26879 doc: /* List of functions to call before redisplaying a window with scrolling.
26880 Each function is called with two arguments, the window and its new
26881 display-start position. Note that these functions are also called by
26882 `set-window-buffer'. Also note that the value of `window-end' is not
26883 valid when these functions are called. */);
26884 Vwindow_scroll_functions = Qnil;
26885
26886 DEFVAR_LISP ("window-text-change-functions",
26887 Vwindow_text_change_functions,
26888 doc: /* Functions to call in redisplay when text in the window might change. */);
26889 Vwindow_text_change_functions = Qnil;
26890
26891 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
26892 doc: /* Functions called when redisplay of a window reaches the end trigger.
26893 Each function is called with two arguments, the window and the end trigger value.
26894 See `set-window-redisplay-end-trigger'. */);
26895 Vredisplay_end_trigger_functions = Qnil;
26896
26897 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
26898 doc: /* *Non-nil means autoselect window with mouse pointer.
26899 If nil, do not autoselect windows.
26900 A positive number means delay autoselection by that many seconds: a
26901 window is autoselected only after the mouse has remained in that
26902 window for the duration of the delay.
26903 A negative number has a similar effect, but causes windows to be
26904 autoselected only after the mouse has stopped moving. \(Because of
26905 the way Emacs compares mouse events, you will occasionally wait twice
26906 that time before the window gets selected.\)
26907 Any other value means to autoselect window instantaneously when the
26908 mouse pointer enters it.
26909
26910 Autoselection selects the minibuffer only if it is active, and never
26911 unselects the minibuffer if it is active.
26912
26913 When customizing this variable make sure that the actual value of
26914 `focus-follows-mouse' matches the behavior of your window manager. */);
26915 Vmouse_autoselect_window = Qnil;
26916
26917 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
26918 doc: /* *Non-nil means automatically resize tool-bars.
26919 This dynamically changes the tool-bar's height to the minimum height
26920 that is needed to make all tool-bar items visible.
26921 If value is `grow-only', the tool-bar's height is only increased
26922 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26923 Vauto_resize_tool_bars = Qt;
26924
26925 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
26926 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26927 auto_raise_tool_bar_buttons_p = 1;
26928
26929 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
26930 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26931 make_cursor_line_fully_visible_p = 1;
26932
26933 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
26934 doc: /* *Border below tool-bar in pixels.
26935 If an integer, use it as the height of the border.
26936 If it is one of `internal-border-width' or `border-width', use the
26937 value of the corresponding frame parameter.
26938 Otherwise, no border is added below the tool-bar. */);
26939 Vtool_bar_border = Qinternal_border_width;
26940
26941 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
26942 doc: /* *Margin around tool-bar buttons in pixels.
26943 If an integer, use that for both horizontal and vertical margins.
26944 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26945 HORZ specifying the horizontal margin, and VERT specifying the
26946 vertical margin. */);
26947 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26948
26949 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
26950 doc: /* *Relief thickness of tool-bar buttons. */);
26951 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26952
26953 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
26954 doc: /* Tool bar style to use.
26955 It can be one of
26956 image - show images only
26957 text - show text only
26958 both - show both, text below image
26959 both-horiz - show text to the right of the image
26960 text-image-horiz - show text to the left of the image
26961 any other - use system default or image if no system default. */);
26962 Vtool_bar_style = Qnil;
26963
26964 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
26965 doc: /* *Maximum number of characters a label can have to be shown.
26966 The tool bar style must also show labels for this to have any effect, see
26967 `tool-bar-style'. */);
26968 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26969
26970 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
26971 doc: /* List of functions to call to fontify regions of text.
26972 Each function is called with one argument POS. Functions must
26973 fontify a region starting at POS in the current buffer, and give
26974 fontified regions the property `fontified'. */);
26975 Vfontification_functions = Qnil;
26976 Fmake_variable_buffer_local (Qfontification_functions);
26977
26978 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26979 unibyte_display_via_language_environment,
26980 doc: /* *Non-nil means display unibyte text according to language environment.
26981 Specifically, this means that raw bytes in the range 160-255 decimal
26982 are displayed by converting them to the equivalent multibyte characters
26983 according to the current language environment. As a result, they are
26984 displayed according to the current fontset.
26985
26986 Note that this variable affects only how these bytes are displayed,
26987 but does not change the fact they are interpreted as raw bytes. */);
26988 unibyte_display_via_language_environment = 0;
26989
26990 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
26991 doc: /* *Maximum height for resizing mini-windows.
26992 If a float, it specifies a fraction of the mini-window frame's height.
26993 If an integer, it specifies a number of lines. */);
26994 Vmax_mini_window_height = make_float (0.25);
26995
26996 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
26997 doc: /* *How to resize mini-windows.
26998 A value of nil means don't automatically resize mini-windows.
26999 A value of t means resize them to fit the text displayed in them.
27000 A value of `grow-only', the default, means let mini-windows grow
27001 only, until their display becomes empty, at which point the windows
27002 go back to their normal size. */);
27003 Vresize_mini_windows = Qgrow_only;
27004
27005 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27006 doc: /* Alist specifying how to blink the cursor off.
27007 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27008 `cursor-type' frame-parameter or variable equals ON-STATE,
27009 comparing using `equal', Emacs uses OFF-STATE to specify
27010 how to blink it off. ON-STATE and OFF-STATE are values for
27011 the `cursor-type' frame parameter.
27012
27013 If a frame's ON-STATE has no entry in this list,
27014 the frame's other specifications determine how to blink the cursor off. */);
27015 Vblink_cursor_alist = Qnil;
27016
27017 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27018 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27019 If non-nil, windows are automatically scrolled horizontally to make
27020 point visible. */);
27021 automatic_hscrolling_p = 1;
27022 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
27023 staticpro (&Qauto_hscroll_mode);
27024
27025 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27026 doc: /* *How many columns away from the window edge point is allowed to get
27027 before automatic hscrolling will horizontally scroll the window. */);
27028 hscroll_margin = 5;
27029
27030 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27031 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27032 When point is less than `hscroll-margin' columns from the window
27033 edge, automatic hscrolling will scroll the window by the amount of columns
27034 determined by this variable. If its value is a positive integer, scroll that
27035 many columns. If it's a positive floating-point number, it specifies the
27036 fraction of the window's width to scroll. If it's nil or zero, point will be
27037 centered horizontally after the scroll. Any other value, including negative
27038 numbers, are treated as if the value were zero.
27039
27040 Automatic hscrolling always moves point outside the scroll margin, so if
27041 point was more than scroll step columns inside the margin, the window will
27042 scroll more than the value given by the scroll step.
27043
27044 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27045 and `scroll-right' overrides this variable's effect. */);
27046 Vhscroll_step = make_number (0);
27047
27048 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27049 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27050 Bind this around calls to `message' to let it take effect. */);
27051 message_truncate_lines = 0;
27052
27053 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27054 doc: /* Normal hook run to update the menu bar definitions.
27055 Redisplay runs this hook before it redisplays the menu bar.
27056 This is used to update submenus such as Buffers,
27057 whose contents depend on various data. */);
27058 Vmenu_bar_update_hook = Qnil;
27059
27060 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27061 doc: /* Frame for which we are updating a menu.
27062 The enable predicate for a menu binding should check this variable. */);
27063 Vmenu_updating_frame = Qnil;
27064
27065 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27066 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27067 inhibit_menubar_update = 0;
27068
27069 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27070 doc: /* Prefix prepended to all continuation lines at display time.
27071 The value may be a string, an image, or a stretch-glyph; it is
27072 interpreted in the same way as the value of a `display' text property.
27073
27074 This variable is overridden by any `wrap-prefix' text or overlay
27075 property.
27076
27077 To add a prefix to non-continuation lines, use `line-prefix'. */);
27078 Vwrap_prefix = Qnil;
27079 staticpro (&Qwrap_prefix);
27080 Qwrap_prefix = intern_c_string ("wrap-prefix");
27081 Fmake_variable_buffer_local (Qwrap_prefix);
27082
27083 DEFVAR_LISP ("line-prefix", Vline_prefix,
27084 doc: /* Prefix prepended to all non-continuation lines at display time.
27085 The value may be a string, an image, or a stretch-glyph; it is
27086 interpreted in the same way as the value of a `display' text property.
27087
27088 This variable is overridden by any `line-prefix' text or overlay
27089 property.
27090
27091 To add a prefix to continuation lines, use `wrap-prefix'. */);
27092 Vline_prefix = Qnil;
27093 staticpro (&Qline_prefix);
27094 Qline_prefix = intern_c_string ("line-prefix");
27095 Fmake_variable_buffer_local (Qline_prefix);
27096
27097 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27098 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27099 inhibit_eval_during_redisplay = 0;
27100
27101 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27102 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27103 inhibit_free_realized_faces = 0;
27104
27105 #if GLYPH_DEBUG
27106 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27107 doc: /* Inhibit try_window_id display optimization. */);
27108 inhibit_try_window_id = 0;
27109
27110 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27111 doc: /* Inhibit try_window_reusing display optimization. */);
27112 inhibit_try_window_reusing = 0;
27113
27114 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27115 doc: /* Inhibit try_cursor_movement display optimization. */);
27116 inhibit_try_cursor_movement = 0;
27117 #endif /* GLYPH_DEBUG */
27118
27119 DEFVAR_INT ("overline-margin", overline_margin,
27120 doc: /* *Space between overline and text, in pixels.
27121 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27122 margin to the caracter height. */);
27123 overline_margin = 2;
27124
27125 DEFVAR_INT ("underline-minimum-offset",
27126 underline_minimum_offset,
27127 doc: /* Minimum distance between baseline and underline.
27128 This can improve legibility of underlined text at small font sizes,
27129 particularly when using variable `x-use-underline-position-properties'
27130 with fonts that specify an UNDERLINE_POSITION relatively close to the
27131 baseline. The default value is 1. */);
27132 underline_minimum_offset = 1;
27133
27134 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27135 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27136 This feature only works when on a window system that can change
27137 cursor shapes. */);
27138 display_hourglass_p = 1;
27139
27140 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27141 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27142 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27143
27144 hourglass_atimer = NULL;
27145 hourglass_shown_p = 0;
27146
27147 DEFSYM (Qglyphless_char, "glyphless-char");
27148 DEFSYM (Qhex_code, "hex-code");
27149 DEFSYM (Qempty_box, "empty-box");
27150 DEFSYM (Qthin_space, "thin-space");
27151 DEFSYM (Qzero_width, "zero-width");
27152
27153 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27154 /* Intern this now in case it isn't already done.
27155 Setting this variable twice is harmless.
27156 But don't staticpro it here--that is done in alloc.c. */
27157 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27158 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27159
27160 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27161 doc: /* Char-table defining glyphless characters.
27162 Each element, if non-nil, should be one of the following:
27163 an ASCII acronym string: display this string in a box
27164 `hex-code': display the hexadecimal code of a character in a box
27165 `empty-box': display as an empty box
27166 `thin-space': display as 1-pixel width space
27167 `zero-width': don't display
27168 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27169 display method for graphical terminals and text terminals respectively.
27170 GRAPHICAL and TEXT should each have one of the values listed above.
27171
27172 The char-table has one extra slot to control the display of a character for
27173 which no font is found. This slot only takes effect on graphical terminals.
27174 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27175 `thin-space'. The default is `empty-box'. */);
27176 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27177 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27178 Qempty_box);
27179 }
27180
27181
27182 /* Initialize this module when Emacs starts. */
27183
27184 void
27185 init_xdisp (void)
27186 {
27187 Lisp_Object root_window;
27188 struct window *mini_w;
27189
27190 current_header_line_height = current_mode_line_height = -1;
27191
27192 CHARPOS (this_line_start_pos) = 0;
27193
27194 mini_w = XWINDOW (minibuf_window);
27195 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27196 echo_area_window = minibuf_window;
27197
27198 if (!noninteractive)
27199 {
27200 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27201 int i;
27202
27203 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27204 set_window_height (root_window,
27205 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27206 0);
27207 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27208 set_window_height (minibuf_window, 1, 0);
27209
27210 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27211 mini_w->total_cols = make_number (FRAME_COLS (f));
27212
27213 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27214 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27215 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27216
27217 /* The default ellipsis glyphs `...'. */
27218 for (i = 0; i < 3; ++i)
27219 default_invis_vector[i] = make_number ('.');
27220 }
27221
27222 {
27223 /* Allocate the buffer for frame titles.
27224 Also used for `format-mode-line'. */
27225 int size = 100;
27226 mode_line_noprop_buf = (char *) xmalloc (size);
27227 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27228 mode_line_noprop_ptr = mode_line_noprop_buf;
27229 mode_line_target = MODE_LINE_DISPLAY;
27230 }
27231
27232 help_echo_showing_p = 0;
27233 }
27234
27235 /* Since w32 does not support atimers, it defines its own implementation of
27236 the following three functions in w32fns.c. */
27237 #ifndef WINDOWSNT
27238
27239 /* Platform-independent portion of hourglass implementation. */
27240
27241 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27242 int
27243 hourglass_started (void)
27244 {
27245 return hourglass_shown_p || hourglass_atimer != NULL;
27246 }
27247
27248 /* Cancel a currently active hourglass timer, and start a new one. */
27249 void
27250 start_hourglass (void)
27251 {
27252 #if defined (HAVE_WINDOW_SYSTEM)
27253 EMACS_TIME delay;
27254 int secs, usecs = 0;
27255
27256 cancel_hourglass ();
27257
27258 if (INTEGERP (Vhourglass_delay)
27259 && XINT (Vhourglass_delay) > 0)
27260 secs = XFASTINT (Vhourglass_delay);
27261 else if (FLOATP (Vhourglass_delay)
27262 && XFLOAT_DATA (Vhourglass_delay) > 0)
27263 {
27264 Lisp_Object tem;
27265 tem = Ftruncate (Vhourglass_delay, Qnil);
27266 secs = XFASTINT (tem);
27267 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27268 }
27269 else
27270 secs = DEFAULT_HOURGLASS_DELAY;
27271
27272 EMACS_SET_SECS_USECS (delay, secs, usecs);
27273 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27274 show_hourglass, NULL);
27275 #endif
27276 }
27277
27278
27279 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27280 shown. */
27281 void
27282 cancel_hourglass (void)
27283 {
27284 #if defined (HAVE_WINDOW_SYSTEM)
27285 if (hourglass_atimer)
27286 {
27287 cancel_atimer (hourglass_atimer);
27288 hourglass_atimer = NULL;
27289 }
27290
27291 if (hourglass_shown_p)
27292 hide_hourglass ();
27293 #endif
27294 }
27295 #endif /* ! WINDOWSNT */