Initial version of display/overlay strings is working.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT is a space or tab
387 character. This is used to determine word wrapping. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
391
392 /* Name of the face used to highlight trailing whitespace. */
393
394 static Lisp_Object Qtrailing_whitespace;
395
396 /* Name and number of the face used to highlight escape glyphs. */
397
398 static Lisp_Object Qescape_glyph;
399
400 /* Name and number of the face used to highlight non-breaking spaces. */
401
402 static Lisp_Object Qnobreak_space;
403
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
406
407 Lisp_Object Qimage;
408
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
413
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
419
420 int noninteractive_need_newline;
421
422 /* Non-zero means print newline to message log before next message. */
423
424 static int message_log_need_newline;
425
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
432 \f
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
437
438 static struct text_pos this_line_start_pos;
439
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
442
443 static struct text_pos this_line_end_pos;
444
445 /* The vertical positions and the height of this line. */
446
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
450
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
453
454 static int this_line_start_x;
455
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
459
460 static struct text_pos this_line_min_pos;
461
462 /* Buffer that this_line_.* variables are referring to. */
463
464 static struct buffer *this_line_buffer;
465
466
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
471
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
476
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478
479 Lisp_Object Qmenu_bar_update_hook;
480
481 /* Nonzero if an overlay arrow has been displayed in this window. */
482
483 static int overlay_arrow_seen;
484
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
488
489 int buffer_shared;
490
491 /* Vector containing glyphs for an ellipsis `...'. */
492
493 static Lisp_Object default_invis_vector[3];
494
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
498
499 Lisp_Object echo_area_window;
500
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
505
506 static Lisp_Object Vmessage_stack;
507
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
510
511 static int message_enable_multibyte;
512
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
514
515 int update_mode_lines;
516
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero means a frame's cursor type has been changed. */
523
524 int cursor_type_changed;
525
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
528
529 static int line_number_displayed;
530
531 /* The name of the *Messages* buffer, a string. */
532
533 static Lisp_Object Vmessages_buffer_name;
534
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
537
538 Lisp_Object echo_area_buffer[2];
539
540 /* The buffers referenced from echo_area_buffer. */
541
542 static Lisp_Object echo_buffer[2];
543
544 /* A vector saved used in with_area_buffer to reduce consing. */
545
546 static Lisp_Object Vwith_echo_area_save_vector;
547
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
550
551 static int display_last_displayed_message_p;
552
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
555
556 static int message_buf_print;
557
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
559
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
562
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
565
566 static int message_cleared_p;
567
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
570
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
574
575 /* Ascent and height of the last line processed by move_it_to. */
576
577 static int last_max_ascent, last_height;
578
579 /* Non-zero if there's a help-echo in the echo area. */
580
581 int help_echo_showing_p;
582
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
586
587 int current_mode_line_height, current_header_line_height;
588
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
594
595 #define TEXT_PROP_DISTANCE_LIMIT 100
596
597 #if GLYPH_DEBUG
598
599 /* Non-zero means print traces of redisplay if compiled with
600 GLYPH_DEBUG != 0. */
601
602 int trace_redisplay_p;
603
604 #endif /* GLYPH_DEBUG */
605
606 #ifdef DEBUG_TRACE_MOVE
607 /* Non-zero means trace with TRACE_MOVE to stderr. */
608 int trace_move;
609
610 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
611 #else
612 #define TRACE_MOVE(x) (void) 0
613 #endif
614
615 static Lisp_Object Qauto_hscroll_mode;
616
617 /* Buffer being redisplayed -- for redisplay_window_error. */
618
619 static struct buffer *displayed_buffer;
620
621 /* Value returned from text property handlers (see below). */
622
623 enum prop_handled
624 {
625 HANDLED_NORMALLY,
626 HANDLED_RECOMPUTE_PROPS,
627 HANDLED_OVERLAY_STRING_CONSUMED,
628 HANDLED_RETURN
629 };
630
631 /* A description of text properties that redisplay is interested
632 in. */
633
634 struct props
635 {
636 /* The name of the property. */
637 Lisp_Object *name;
638
639 /* A unique index for the property. */
640 enum prop_idx idx;
641
642 /* A handler function called to set up iterator IT from the property
643 at IT's current position. Value is used to steer handle_stop. */
644 enum prop_handled (*handler) (struct it *it);
645 };
646
647 static enum prop_handled handle_face_prop (struct it *);
648 static enum prop_handled handle_invisible_prop (struct it *);
649 static enum prop_handled handle_display_prop (struct it *);
650 static enum prop_handled handle_composition_prop (struct it *);
651 static enum prop_handled handle_overlay_change (struct it *);
652 static enum prop_handled handle_fontified_prop (struct it *);
653
654 /* Properties handled by iterators. */
655
656 static struct props it_props[] =
657 {
658 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
659 /* Handle `face' before `display' because some sub-properties of
660 `display' need to know the face. */
661 {&Qface, FACE_PROP_IDX, handle_face_prop},
662 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
663 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
664 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
665 {NULL, 0, NULL}
666 };
667
668 /* Value is the position described by X. If X is a marker, value is
669 the marker_position of X. Otherwise, value is X. */
670
671 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
672
673 /* Enumeration returned by some move_it_.* functions internally. */
674
675 enum move_it_result
676 {
677 /* Not used. Undefined value. */
678 MOVE_UNDEFINED,
679
680 /* Move ended at the requested buffer position or ZV. */
681 MOVE_POS_MATCH_OR_ZV,
682
683 /* Move ended at the requested X pixel position. */
684 MOVE_X_REACHED,
685
686 /* Move within a line ended at the end of a line that must be
687 continued. */
688 MOVE_LINE_CONTINUED,
689
690 /* Move within a line ended at the end of a line that would
691 be displayed truncated. */
692 MOVE_LINE_TRUNCATED,
693
694 /* Move within a line ended at a line end. */
695 MOVE_NEWLINE_OR_CR
696 };
697
698 /* This counter is used to clear the face cache every once in a while
699 in redisplay_internal. It is incremented for each redisplay.
700 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
701 cleared. */
702
703 #define CLEAR_FACE_CACHE_COUNT 500
704 static int clear_face_cache_count;
705
706 /* Similarly for the image cache. */
707
708 #ifdef HAVE_WINDOW_SYSTEM
709 #define CLEAR_IMAGE_CACHE_COUNT 101
710 static int clear_image_cache_count;
711
712 /* Null glyph slice */
713 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
714 #endif
715
716 /* Non-zero while redisplay_internal is in progress. */
717
718 int redisplaying_p;
719
720 static Lisp_Object Qinhibit_free_realized_faces;
721
722 /* If a string, XTread_socket generates an event to display that string.
723 (The display is done in read_char.) */
724
725 Lisp_Object help_echo_string;
726 Lisp_Object help_echo_window;
727 Lisp_Object help_echo_object;
728 EMACS_INT help_echo_pos;
729
730 /* Temporary variable for XTread_socket. */
731
732 Lisp_Object previous_help_echo_string;
733
734 /* Platform-independent portion of hourglass implementation. */
735
736 /* Non-zero means an hourglass cursor is currently shown. */
737 int hourglass_shown_p;
738
739 /* If non-null, an asynchronous timer that, when it expires, displays
740 an hourglass cursor on all frames. */
741 struct atimer *hourglass_atimer;
742
743 /* Name of the face used to display glyphless characters. */
744 Lisp_Object Qglyphless_char;
745
746 /* Symbol for the purpose of Vglyphless_char_display. */
747 static Lisp_Object Qglyphless_char_display;
748
749 /* Method symbols for Vglyphless_char_display. */
750 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
751
752 /* Default pixel width of `thin-space' display method. */
753 #define THIN_SPACE_WIDTH 1
754
755 /* Default number of seconds to wait before displaying an hourglass
756 cursor. */
757 #define DEFAULT_HOURGLASS_DELAY 1
758
759 \f
760 /* Function prototypes. */
761
762 static void setup_for_ellipsis (struct it *, int);
763 static void set_iterator_to_next (struct it *, int);
764 static void mark_window_display_accurate_1 (struct window *, int);
765 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
766 static int display_prop_string_p (Lisp_Object, Lisp_Object);
767 static int cursor_row_p (struct glyph_row *);
768 static int redisplay_mode_lines (Lisp_Object, int);
769 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
770
771 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
772
773 static void handle_line_prefix (struct it *);
774
775 static void pint2str (char *, int, EMACS_INT);
776 static void pint2hrstr (char *, int, EMACS_INT);
777 static struct text_pos run_window_scroll_functions (Lisp_Object,
778 struct text_pos);
779 static void reconsider_clip_changes (struct window *, struct buffer *);
780 static int text_outside_line_unchanged_p (struct window *,
781 EMACS_INT, EMACS_INT);
782 static void store_mode_line_noprop_char (char);
783 static int store_mode_line_noprop (const char *, int, int);
784 static void handle_stop (struct it *);
785 static void handle_stop_backwards (struct it *, EMACS_INT);
786 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
787 static void ensure_echo_area_buffers (void);
788 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
789 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
790 static int with_echo_area_buffer (struct window *, int,
791 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
792 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
793 static void clear_garbaged_frames (void);
794 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
795 static void pop_message (void);
796 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
797 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
798 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
799 static int display_echo_area (struct window *);
800 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
801 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
802 static Lisp_Object unwind_redisplay (Lisp_Object);
803 static int string_char_and_length (const unsigned char *, int *);
804 static struct text_pos display_prop_end (struct it *, Lisp_Object,
805 struct text_pos);
806 static int compute_window_start_on_continuation_line (struct window *);
807 static Lisp_Object safe_eval_handler (Lisp_Object);
808 static void insert_left_trunc_glyphs (struct it *);
809 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
810 Lisp_Object);
811 static void extend_face_to_end_of_line (struct it *);
812 static int append_space_for_newline (struct it *, int);
813 static int cursor_row_fully_visible_p (struct window *, int, int);
814 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
815 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
816 static int trailing_whitespace_p (EMACS_INT);
817 static unsigned long int message_log_check_duplicate (EMACS_INT, EMACS_INT);
818 static void push_it (struct it *, struct text_pos *);
819 static void pop_it (struct it *);
820 static void sync_frame_with_window_matrix_rows (struct window *);
821 static void select_frame_for_redisplay (Lisp_Object);
822 static void redisplay_internal (void);
823 static int echo_area_display (int);
824 static void redisplay_windows (Lisp_Object);
825 static void redisplay_window (Lisp_Object, int);
826 static Lisp_Object redisplay_window_error (Lisp_Object);
827 static Lisp_Object redisplay_window_0 (Lisp_Object);
828 static Lisp_Object redisplay_window_1 (Lisp_Object);
829 static int set_cursor_from_row (struct window *, struct glyph_row *,
830 struct glyph_matrix *, EMACS_INT, EMACS_INT,
831 int, int);
832 static int update_menu_bar (struct frame *, int, int);
833 static int try_window_reusing_current_matrix (struct window *);
834 static int try_window_id (struct window *);
835 static int display_line (struct it *);
836 static int display_mode_lines (struct window *);
837 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
838 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
839 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
840 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
841 static void display_menu_bar (struct window *);
842 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
843 EMACS_INT *);
844 static int display_string (const char *, Lisp_Object, Lisp_Object,
845 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
846 static void compute_line_metrics (struct it *);
847 static void run_redisplay_end_trigger_hook (struct it *);
848 static int get_overlay_strings (struct it *, EMACS_INT);
849 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
850 static void next_overlay_string (struct it *);
851 static void reseat (struct it *, struct text_pos, int);
852 static void reseat_1 (struct it *, struct text_pos, int);
853 static void back_to_previous_visible_line_start (struct it *);
854 void reseat_at_previous_visible_line_start (struct it *);
855 static void reseat_at_next_visible_line_start (struct it *, int);
856 static int next_element_from_ellipsis (struct it *);
857 static int next_element_from_display_vector (struct it *);
858 static int next_element_from_string (struct it *);
859 static int next_element_from_c_string (struct it *);
860 static int next_element_from_buffer (struct it *);
861 static int next_element_from_composition (struct it *);
862 static int next_element_from_image (struct it *);
863 static int next_element_from_stretch (struct it *);
864 static void load_overlay_strings (struct it *, EMACS_INT);
865 static int init_from_display_pos (struct it *, struct window *,
866 struct display_pos *);
867 static void reseat_to_string (struct it *, const char *,
868 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
869 static int get_next_display_element (struct it *);
870 static enum move_it_result
871 move_it_in_display_line_to (struct it *, EMACS_INT, int,
872 enum move_operation_enum);
873 void move_it_vertically_backward (struct it *, int);
874 static void init_to_row_start (struct it *, struct window *,
875 struct glyph_row *);
876 static int init_to_row_end (struct it *, struct window *,
877 struct glyph_row *);
878 static void back_to_previous_line_start (struct it *);
879 static int forward_to_next_line_start (struct it *, int *);
880 static struct text_pos string_pos_nchars_ahead (struct text_pos,
881 Lisp_Object, EMACS_INT);
882 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
883 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
884 static EMACS_INT number_of_chars (const char *, int);
885 static void compute_stop_pos (struct it *);
886 static void compute_string_pos (struct text_pos *, struct text_pos,
887 Lisp_Object);
888 static int face_before_or_after_it_pos (struct it *, int);
889 static EMACS_INT next_overlay_change (EMACS_INT);
890 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
891 Lisp_Object, struct text_pos *, EMACS_INT, int);
892 static int handle_single_display_spec (struct it *, Lisp_Object,
893 Lisp_Object, Lisp_Object,
894 struct text_pos *, EMACS_INT, int, int);
895 static int underlying_face_id (struct it *);
896 static int in_ellipses_for_invisible_text_p (struct display_pos *,
897 struct window *);
898
899 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
900 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
901
902 #ifdef HAVE_WINDOW_SYSTEM
903
904 static void x_consider_frame_title (Lisp_Object);
905 static int tool_bar_lines_needed (struct frame *, int *);
906 static void update_tool_bar (struct frame *, int);
907 static void build_desired_tool_bar_string (struct frame *f);
908 static int redisplay_tool_bar (struct frame *);
909 static void display_tool_bar_line (struct it *, int);
910 static void notice_overwritten_cursor (struct window *,
911 enum glyph_row_area,
912 int, int, int, int);
913 static void append_stretch_glyph (struct it *, Lisp_Object,
914 int, int, int);
915
916
917 #endif /* HAVE_WINDOW_SYSTEM */
918
919 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
920 static int coords_in_mouse_face_p (struct window *, int, int);
921
922
923 \f
924 /***********************************************************************
925 Window display dimensions
926 ***********************************************************************/
927
928 /* Return the bottom boundary y-position for text lines in window W.
929 This is the first y position at which a line cannot start.
930 It is relative to the top of the window.
931
932 This is the height of W minus the height of a mode line, if any. */
933
934 INLINE int
935 window_text_bottom_y (struct window *w)
936 {
937 int height = WINDOW_TOTAL_HEIGHT (w);
938
939 if (WINDOW_WANTS_MODELINE_P (w))
940 height -= CURRENT_MODE_LINE_HEIGHT (w);
941 return height;
942 }
943
944 /* Return the pixel width of display area AREA of window W. AREA < 0
945 means return the total width of W, not including fringes to
946 the left and right of the window. */
947
948 INLINE int
949 window_box_width (struct window *w, int area)
950 {
951 int cols = XFASTINT (w->total_cols);
952 int pixels = 0;
953
954 if (!w->pseudo_window_p)
955 {
956 cols -= WINDOW_SCROLL_BAR_COLS (w);
957
958 if (area == TEXT_AREA)
959 {
960 if (INTEGERP (w->left_margin_cols))
961 cols -= XFASTINT (w->left_margin_cols);
962 if (INTEGERP (w->right_margin_cols))
963 cols -= XFASTINT (w->right_margin_cols);
964 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
965 }
966 else if (area == LEFT_MARGIN_AREA)
967 {
968 cols = (INTEGERP (w->left_margin_cols)
969 ? XFASTINT (w->left_margin_cols) : 0);
970 pixels = 0;
971 }
972 else if (area == RIGHT_MARGIN_AREA)
973 {
974 cols = (INTEGERP (w->right_margin_cols)
975 ? XFASTINT (w->right_margin_cols) : 0);
976 pixels = 0;
977 }
978 }
979
980 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
981 }
982
983
984 /* Return the pixel height of the display area of window W, not
985 including mode lines of W, if any. */
986
987 INLINE int
988 window_box_height (struct window *w)
989 {
990 struct frame *f = XFRAME (w->frame);
991 int height = WINDOW_TOTAL_HEIGHT (w);
992
993 xassert (height >= 0);
994
995 /* Note: the code below that determines the mode-line/header-line
996 height is essentially the same as that contained in the macro
997 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
998 the appropriate glyph row has its `mode_line_p' flag set,
999 and if it doesn't, uses estimate_mode_line_height instead. */
1000
1001 if (WINDOW_WANTS_MODELINE_P (w))
1002 {
1003 struct glyph_row *ml_row
1004 = (w->current_matrix && w->current_matrix->rows
1005 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1006 : 0);
1007 if (ml_row && ml_row->mode_line_p)
1008 height -= ml_row->height;
1009 else
1010 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1011 }
1012
1013 if (WINDOW_WANTS_HEADER_LINE_P (w))
1014 {
1015 struct glyph_row *hl_row
1016 = (w->current_matrix && w->current_matrix->rows
1017 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1018 : 0);
1019 if (hl_row && hl_row->mode_line_p)
1020 height -= hl_row->height;
1021 else
1022 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1023 }
1024
1025 /* With a very small font and a mode-line that's taller than
1026 default, we might end up with a negative height. */
1027 return max (0, height);
1028 }
1029
1030 /* Return the window-relative coordinate of the left edge of display
1031 area AREA of window W. AREA < 0 means return the left edge of the
1032 whole window, to the right of the left fringe of W. */
1033
1034 INLINE int
1035 window_box_left_offset (struct window *w, int area)
1036 {
1037 int x;
1038
1039 if (w->pseudo_window_p)
1040 return 0;
1041
1042 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1043
1044 if (area == TEXT_AREA)
1045 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1046 + window_box_width (w, LEFT_MARGIN_AREA));
1047 else if (area == RIGHT_MARGIN_AREA)
1048 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1049 + window_box_width (w, LEFT_MARGIN_AREA)
1050 + window_box_width (w, TEXT_AREA)
1051 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1052 ? 0
1053 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1054 else if (area == LEFT_MARGIN_AREA
1055 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1056 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1057
1058 return x;
1059 }
1060
1061
1062 /* Return the window-relative coordinate of the right edge of display
1063 area AREA of window W. AREA < 0 means return the right edge of the
1064 whole window, to the left of the right fringe of W. */
1065
1066 INLINE int
1067 window_box_right_offset (struct window *w, int area)
1068 {
1069 return window_box_left_offset (w, area) + window_box_width (w, area);
1070 }
1071
1072 /* Return the frame-relative coordinate of the left edge of display
1073 area AREA of window W. AREA < 0 means return the left edge of the
1074 whole window, to the right of the left fringe of W. */
1075
1076 INLINE int
1077 window_box_left (struct window *w, int area)
1078 {
1079 struct frame *f = XFRAME (w->frame);
1080 int x;
1081
1082 if (w->pseudo_window_p)
1083 return FRAME_INTERNAL_BORDER_WIDTH (f);
1084
1085 x = (WINDOW_LEFT_EDGE_X (w)
1086 + window_box_left_offset (w, area));
1087
1088 return x;
1089 }
1090
1091
1092 /* Return the frame-relative coordinate of the right edge of display
1093 area AREA of window W. AREA < 0 means return the right edge of the
1094 whole window, to the left of the right fringe of W. */
1095
1096 INLINE int
1097 window_box_right (struct window *w, int area)
1098 {
1099 return window_box_left (w, area) + window_box_width (w, area);
1100 }
1101
1102 /* Get the bounding box of the display area AREA of window W, without
1103 mode lines, in frame-relative coordinates. AREA < 0 means the
1104 whole window, not including the left and right fringes of
1105 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1106 coordinates of the upper-left corner of the box. Return in
1107 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1108
1109 INLINE void
1110 window_box (struct window *w, int area, int *box_x, int *box_y,
1111 int *box_width, int *box_height)
1112 {
1113 if (box_width)
1114 *box_width = window_box_width (w, area);
1115 if (box_height)
1116 *box_height = window_box_height (w);
1117 if (box_x)
1118 *box_x = window_box_left (w, area);
1119 if (box_y)
1120 {
1121 *box_y = WINDOW_TOP_EDGE_Y (w);
1122 if (WINDOW_WANTS_HEADER_LINE_P (w))
1123 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1124 }
1125 }
1126
1127
1128 /* Get the bounding box of the display area AREA of window W, without
1129 mode lines. AREA < 0 means the whole window, not including the
1130 left and right fringe of the window. Return in *TOP_LEFT_X
1131 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1132 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1133 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1134 box. */
1135
1136 static INLINE void
1137 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1138 int *bottom_right_x, int *bottom_right_y)
1139 {
1140 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1141 bottom_right_y);
1142 *bottom_right_x += *top_left_x;
1143 *bottom_right_y += *top_left_y;
1144 }
1145
1146
1147 \f
1148 /***********************************************************************
1149 Utilities
1150 ***********************************************************************/
1151
1152 /* Return the bottom y-position of the line the iterator IT is in.
1153 This can modify IT's settings. */
1154
1155 int
1156 line_bottom_y (struct it *it)
1157 {
1158 int line_height = it->max_ascent + it->max_descent;
1159 int line_top_y = it->current_y;
1160
1161 if (line_height == 0)
1162 {
1163 if (last_height)
1164 line_height = last_height;
1165 else if (IT_CHARPOS (*it) < ZV)
1166 {
1167 move_it_by_lines (it, 1);
1168 line_height = (it->max_ascent || it->max_descent
1169 ? it->max_ascent + it->max_descent
1170 : last_height);
1171 }
1172 else
1173 {
1174 struct glyph_row *row = it->glyph_row;
1175
1176 /* Use the default character height. */
1177 it->glyph_row = NULL;
1178 it->what = IT_CHARACTER;
1179 it->c = ' ';
1180 it->len = 1;
1181 PRODUCE_GLYPHS (it);
1182 line_height = it->ascent + it->descent;
1183 it->glyph_row = row;
1184 }
1185 }
1186
1187 return line_top_y + line_height;
1188 }
1189
1190
1191 /* Return 1 if position CHARPOS is visible in window W.
1192 CHARPOS < 0 means return info about WINDOW_END position.
1193 If visible, set *X and *Y to pixel coordinates of top left corner.
1194 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1195 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1196
1197 int
1198 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1199 int *rtop, int *rbot, int *rowh, int *vpos)
1200 {
1201 struct it it;
1202 struct text_pos top;
1203 int visible_p = 0;
1204 struct buffer *old_buffer = NULL;
1205
1206 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1207 return visible_p;
1208
1209 if (XBUFFER (w->buffer) != current_buffer)
1210 {
1211 old_buffer = current_buffer;
1212 set_buffer_internal_1 (XBUFFER (w->buffer));
1213 }
1214
1215 SET_TEXT_POS_FROM_MARKER (top, w->start);
1216
1217 /* Compute exact mode line heights. */
1218 if (WINDOW_WANTS_MODELINE_P (w))
1219 current_mode_line_height
1220 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1221 BVAR (current_buffer, mode_line_format));
1222
1223 if (WINDOW_WANTS_HEADER_LINE_P (w))
1224 current_header_line_height
1225 = display_mode_line (w, HEADER_LINE_FACE_ID,
1226 BVAR (current_buffer, header_line_format));
1227
1228 start_display (&it, w, top);
1229 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1230 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1231
1232 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1233 {
1234 /* We have reached CHARPOS, or passed it. How the call to
1235 move_it_to can overshoot: (i) If CHARPOS is on invisible
1236 text, move_it_to stops at the end of the invisible text,
1237 after CHARPOS. (ii) If CHARPOS is in a display vector,
1238 move_it_to stops on its last glyph. */
1239 int top_x = it.current_x;
1240 int top_y = it.current_y;
1241 enum it_method it_method = it.method;
1242 /* Calling line_bottom_y may change it.method, it.position, etc. */
1243 int bottom_y = (last_height = 0, line_bottom_y (&it));
1244 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1245
1246 if (top_y < window_top_y)
1247 visible_p = bottom_y > window_top_y;
1248 else if (top_y < it.last_visible_y)
1249 visible_p = 1;
1250 if (visible_p)
1251 {
1252 if (it_method == GET_FROM_DISPLAY_VECTOR)
1253 {
1254 /* We stopped on the last glyph of a display vector.
1255 Try and recompute. Hack alert! */
1256 if (charpos < 2 || top.charpos >= charpos)
1257 top_x = it.glyph_row->x;
1258 else
1259 {
1260 struct it it2;
1261 start_display (&it2, w, top);
1262 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1263 get_next_display_element (&it2);
1264 PRODUCE_GLYPHS (&it2);
1265 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1266 || it2.current_x > it2.last_visible_x)
1267 top_x = it.glyph_row->x;
1268 else
1269 {
1270 top_x = it2.current_x;
1271 top_y = it2.current_y;
1272 }
1273 }
1274 }
1275
1276 *x = top_x;
1277 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1278 *rtop = max (0, window_top_y - top_y);
1279 *rbot = max (0, bottom_y - it.last_visible_y);
1280 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1281 - max (top_y, window_top_y)));
1282 *vpos = it.vpos;
1283 }
1284 }
1285 else
1286 {
1287 struct it it2;
1288
1289 it2 = it;
1290 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1291 move_it_by_lines (&it, 1);
1292 if (charpos < IT_CHARPOS (it)
1293 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1294 {
1295 visible_p = 1;
1296 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1297 *x = it2.current_x;
1298 *y = it2.current_y + it2.max_ascent - it2.ascent;
1299 *rtop = max (0, -it2.current_y);
1300 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1301 - it.last_visible_y));
1302 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1303 it.last_visible_y)
1304 - max (it2.current_y,
1305 WINDOW_HEADER_LINE_HEIGHT (w))));
1306 *vpos = it2.vpos;
1307 }
1308 }
1309
1310 if (old_buffer)
1311 set_buffer_internal_1 (old_buffer);
1312
1313 current_header_line_height = current_mode_line_height = -1;
1314
1315 if (visible_p && XFASTINT (w->hscroll) > 0)
1316 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1317
1318 #if 0
1319 /* Debugging code. */
1320 if (visible_p)
1321 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1322 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1323 else
1324 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1325 #endif
1326
1327 return visible_p;
1328 }
1329
1330
1331 /* Return the next character from STR. Return in *LEN the length of
1332 the character. This is like STRING_CHAR_AND_LENGTH but never
1333 returns an invalid character. If we find one, we return a `?', but
1334 with the length of the invalid character. */
1335
1336 static INLINE int
1337 string_char_and_length (const unsigned char *str, int *len)
1338 {
1339 int c;
1340
1341 c = STRING_CHAR_AND_LENGTH (str, *len);
1342 if (!CHAR_VALID_P (c, 1))
1343 /* We may not change the length here because other places in Emacs
1344 don't use this function, i.e. they silently accept invalid
1345 characters. */
1346 c = '?';
1347
1348 return c;
1349 }
1350
1351
1352
1353 /* Given a position POS containing a valid character and byte position
1354 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1355
1356 static struct text_pos
1357 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1358 {
1359 xassert (STRINGP (string) && nchars >= 0);
1360
1361 if (STRING_MULTIBYTE (string))
1362 {
1363 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1364 int len;
1365
1366 while (nchars--)
1367 {
1368 string_char_and_length (p, &len);
1369 p += len;
1370 CHARPOS (pos) += 1;
1371 BYTEPOS (pos) += len;
1372 }
1373 }
1374 else
1375 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1376
1377 return pos;
1378 }
1379
1380
1381 /* Value is the text position, i.e. character and byte position,
1382 for character position CHARPOS in STRING. */
1383
1384 static INLINE struct text_pos
1385 string_pos (EMACS_INT charpos, Lisp_Object string)
1386 {
1387 struct text_pos pos;
1388 xassert (STRINGP (string));
1389 xassert (charpos >= 0);
1390 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1391 return pos;
1392 }
1393
1394
1395 /* Value is a text position, i.e. character and byte position, for
1396 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1397 means recognize multibyte characters. */
1398
1399 static struct text_pos
1400 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1401 {
1402 struct text_pos pos;
1403
1404 xassert (s != NULL);
1405 xassert (charpos >= 0);
1406
1407 if (multibyte_p)
1408 {
1409 int len;
1410
1411 SET_TEXT_POS (pos, 0, 0);
1412 while (charpos--)
1413 {
1414 string_char_and_length ((const unsigned char *) s, &len);
1415 s += len;
1416 CHARPOS (pos) += 1;
1417 BYTEPOS (pos) += len;
1418 }
1419 }
1420 else
1421 SET_TEXT_POS (pos, charpos, charpos);
1422
1423 return pos;
1424 }
1425
1426
1427 /* Value is the number of characters in C string S. MULTIBYTE_P
1428 non-zero means recognize multibyte characters. */
1429
1430 static EMACS_INT
1431 number_of_chars (const char *s, int multibyte_p)
1432 {
1433 EMACS_INT nchars;
1434
1435 if (multibyte_p)
1436 {
1437 EMACS_INT rest = strlen (s);
1438 int len;
1439 const unsigned char *p = (const unsigned char *) s;
1440
1441 for (nchars = 0; rest > 0; ++nchars)
1442 {
1443 string_char_and_length (p, &len);
1444 rest -= len, p += len;
1445 }
1446 }
1447 else
1448 nchars = strlen (s);
1449
1450 return nchars;
1451 }
1452
1453
1454 /* Compute byte position NEWPOS->bytepos corresponding to
1455 NEWPOS->charpos. POS is a known position in string STRING.
1456 NEWPOS->charpos must be >= POS.charpos. */
1457
1458 static void
1459 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1460 {
1461 xassert (STRINGP (string));
1462 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1463
1464 if (STRING_MULTIBYTE (string))
1465 *newpos = string_pos_nchars_ahead (pos, string,
1466 CHARPOS (*newpos) - CHARPOS (pos));
1467 else
1468 BYTEPOS (*newpos) = CHARPOS (*newpos);
1469 }
1470
1471 /* EXPORT:
1472 Return an estimation of the pixel height of mode or header lines on
1473 frame F. FACE_ID specifies what line's height to estimate. */
1474
1475 int
1476 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1477 {
1478 #ifdef HAVE_WINDOW_SYSTEM
1479 if (FRAME_WINDOW_P (f))
1480 {
1481 int height = FONT_HEIGHT (FRAME_FONT (f));
1482
1483 /* This function is called so early when Emacs starts that the face
1484 cache and mode line face are not yet initialized. */
1485 if (FRAME_FACE_CACHE (f))
1486 {
1487 struct face *face = FACE_FROM_ID (f, face_id);
1488 if (face)
1489 {
1490 if (face->font)
1491 height = FONT_HEIGHT (face->font);
1492 if (face->box_line_width > 0)
1493 height += 2 * face->box_line_width;
1494 }
1495 }
1496
1497 return height;
1498 }
1499 #endif
1500
1501 return 1;
1502 }
1503
1504 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1505 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1506 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1507 not force the value into range. */
1508
1509 void
1510 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1511 int *x, int *y, NativeRectangle *bounds, int noclip)
1512 {
1513
1514 #ifdef HAVE_WINDOW_SYSTEM
1515 if (FRAME_WINDOW_P (f))
1516 {
1517 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1518 even for negative values. */
1519 if (pix_x < 0)
1520 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1521 if (pix_y < 0)
1522 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1523
1524 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1525 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1526
1527 if (bounds)
1528 STORE_NATIVE_RECT (*bounds,
1529 FRAME_COL_TO_PIXEL_X (f, pix_x),
1530 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1531 FRAME_COLUMN_WIDTH (f) - 1,
1532 FRAME_LINE_HEIGHT (f) - 1);
1533
1534 if (!noclip)
1535 {
1536 if (pix_x < 0)
1537 pix_x = 0;
1538 else if (pix_x > FRAME_TOTAL_COLS (f))
1539 pix_x = FRAME_TOTAL_COLS (f);
1540
1541 if (pix_y < 0)
1542 pix_y = 0;
1543 else if (pix_y > FRAME_LINES (f))
1544 pix_y = FRAME_LINES (f);
1545 }
1546 }
1547 #endif
1548
1549 *x = pix_x;
1550 *y = pix_y;
1551 }
1552
1553
1554 /* Find the glyph under window-relative coordinates X/Y in window W.
1555 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1556 strings. Return in *HPOS and *VPOS the row and column number of
1557 the glyph found. Return in *AREA the glyph area containing X.
1558 Value is a pointer to the glyph found or null if X/Y is not on
1559 text, or we can't tell because W's current matrix is not up to
1560 date. */
1561
1562 static
1563 struct glyph *
1564 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1565 int *dx, int *dy, int *area)
1566 {
1567 struct glyph *glyph, *end;
1568 struct glyph_row *row = NULL;
1569 int x0, i;
1570
1571 /* Find row containing Y. Give up if some row is not enabled. */
1572 for (i = 0; i < w->current_matrix->nrows; ++i)
1573 {
1574 row = MATRIX_ROW (w->current_matrix, i);
1575 if (!row->enabled_p)
1576 return NULL;
1577 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1578 break;
1579 }
1580
1581 *vpos = i;
1582 *hpos = 0;
1583
1584 /* Give up if Y is not in the window. */
1585 if (i == w->current_matrix->nrows)
1586 return NULL;
1587
1588 /* Get the glyph area containing X. */
1589 if (w->pseudo_window_p)
1590 {
1591 *area = TEXT_AREA;
1592 x0 = 0;
1593 }
1594 else
1595 {
1596 if (x < window_box_left_offset (w, TEXT_AREA))
1597 {
1598 *area = LEFT_MARGIN_AREA;
1599 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1600 }
1601 else if (x < window_box_right_offset (w, TEXT_AREA))
1602 {
1603 *area = TEXT_AREA;
1604 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1605 }
1606 else
1607 {
1608 *area = RIGHT_MARGIN_AREA;
1609 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1610 }
1611 }
1612
1613 /* Find glyph containing X. */
1614 glyph = row->glyphs[*area];
1615 end = glyph + row->used[*area];
1616 x -= x0;
1617 while (glyph < end && x >= glyph->pixel_width)
1618 {
1619 x -= glyph->pixel_width;
1620 ++glyph;
1621 }
1622
1623 if (glyph == end)
1624 return NULL;
1625
1626 if (dx)
1627 {
1628 *dx = x;
1629 *dy = y - (row->y + row->ascent - glyph->ascent);
1630 }
1631
1632 *hpos = glyph - row->glyphs[*area];
1633 return glyph;
1634 }
1635
1636 /* Convert frame-relative x/y to coordinates relative to window W.
1637 Takes pseudo-windows into account. */
1638
1639 static void
1640 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1641 {
1642 if (w->pseudo_window_p)
1643 {
1644 /* A pseudo-window is always full-width, and starts at the
1645 left edge of the frame, plus a frame border. */
1646 struct frame *f = XFRAME (w->frame);
1647 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1648 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1649 }
1650 else
1651 {
1652 *x -= WINDOW_LEFT_EDGE_X (w);
1653 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1654 }
1655 }
1656
1657 #ifdef HAVE_WINDOW_SYSTEM
1658
1659 /* EXPORT:
1660 Return in RECTS[] at most N clipping rectangles for glyph string S.
1661 Return the number of stored rectangles. */
1662
1663 int
1664 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1665 {
1666 XRectangle r;
1667
1668 if (n <= 0)
1669 return 0;
1670
1671 if (s->row->full_width_p)
1672 {
1673 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1674 r.x = WINDOW_LEFT_EDGE_X (s->w);
1675 r.width = WINDOW_TOTAL_WIDTH (s->w);
1676
1677 /* Unless displaying a mode or menu bar line, which are always
1678 fully visible, clip to the visible part of the row. */
1679 if (s->w->pseudo_window_p)
1680 r.height = s->row->visible_height;
1681 else
1682 r.height = s->height;
1683 }
1684 else
1685 {
1686 /* This is a text line that may be partially visible. */
1687 r.x = window_box_left (s->w, s->area);
1688 r.width = window_box_width (s->w, s->area);
1689 r.height = s->row->visible_height;
1690 }
1691
1692 if (s->clip_head)
1693 if (r.x < s->clip_head->x)
1694 {
1695 if (r.width >= s->clip_head->x - r.x)
1696 r.width -= s->clip_head->x - r.x;
1697 else
1698 r.width = 0;
1699 r.x = s->clip_head->x;
1700 }
1701 if (s->clip_tail)
1702 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1703 {
1704 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1705 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1706 else
1707 r.width = 0;
1708 }
1709
1710 /* If S draws overlapping rows, it's sufficient to use the top and
1711 bottom of the window for clipping because this glyph string
1712 intentionally draws over other lines. */
1713 if (s->for_overlaps)
1714 {
1715 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1716 r.height = window_text_bottom_y (s->w) - r.y;
1717
1718 /* Alas, the above simple strategy does not work for the
1719 environments with anti-aliased text: if the same text is
1720 drawn onto the same place multiple times, it gets thicker.
1721 If the overlap we are processing is for the erased cursor, we
1722 take the intersection with the rectagle of the cursor. */
1723 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1724 {
1725 XRectangle rc, r_save = r;
1726
1727 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1728 rc.y = s->w->phys_cursor.y;
1729 rc.width = s->w->phys_cursor_width;
1730 rc.height = s->w->phys_cursor_height;
1731
1732 x_intersect_rectangles (&r_save, &rc, &r);
1733 }
1734 }
1735 else
1736 {
1737 /* Don't use S->y for clipping because it doesn't take partially
1738 visible lines into account. For example, it can be negative for
1739 partially visible lines at the top of a window. */
1740 if (!s->row->full_width_p
1741 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1742 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1743 else
1744 r.y = max (0, s->row->y);
1745 }
1746
1747 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1748
1749 /* If drawing the cursor, don't let glyph draw outside its
1750 advertised boundaries. Cleartype does this under some circumstances. */
1751 if (s->hl == DRAW_CURSOR)
1752 {
1753 struct glyph *glyph = s->first_glyph;
1754 int height, max_y;
1755
1756 if (s->x > r.x)
1757 {
1758 r.width -= s->x - r.x;
1759 r.x = s->x;
1760 }
1761 r.width = min (r.width, glyph->pixel_width);
1762
1763 /* If r.y is below window bottom, ensure that we still see a cursor. */
1764 height = min (glyph->ascent + glyph->descent,
1765 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1766 max_y = window_text_bottom_y (s->w) - height;
1767 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1768 if (s->ybase - glyph->ascent > max_y)
1769 {
1770 r.y = max_y;
1771 r.height = height;
1772 }
1773 else
1774 {
1775 /* Don't draw cursor glyph taller than our actual glyph. */
1776 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1777 if (height < r.height)
1778 {
1779 max_y = r.y + r.height;
1780 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1781 r.height = min (max_y - r.y, height);
1782 }
1783 }
1784 }
1785
1786 if (s->row->clip)
1787 {
1788 XRectangle r_save = r;
1789
1790 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1791 r.width = 0;
1792 }
1793
1794 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1795 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1796 {
1797 #ifdef CONVERT_FROM_XRECT
1798 CONVERT_FROM_XRECT (r, *rects);
1799 #else
1800 *rects = r;
1801 #endif
1802 return 1;
1803 }
1804 else
1805 {
1806 /* If we are processing overlapping and allowed to return
1807 multiple clipping rectangles, we exclude the row of the glyph
1808 string from the clipping rectangle. This is to avoid drawing
1809 the same text on the environment with anti-aliasing. */
1810 #ifdef CONVERT_FROM_XRECT
1811 XRectangle rs[2];
1812 #else
1813 XRectangle *rs = rects;
1814 #endif
1815 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1816
1817 if (s->for_overlaps & OVERLAPS_PRED)
1818 {
1819 rs[i] = r;
1820 if (r.y + r.height > row_y)
1821 {
1822 if (r.y < row_y)
1823 rs[i].height = row_y - r.y;
1824 else
1825 rs[i].height = 0;
1826 }
1827 i++;
1828 }
1829 if (s->for_overlaps & OVERLAPS_SUCC)
1830 {
1831 rs[i] = r;
1832 if (r.y < row_y + s->row->visible_height)
1833 {
1834 if (r.y + r.height > row_y + s->row->visible_height)
1835 {
1836 rs[i].y = row_y + s->row->visible_height;
1837 rs[i].height = r.y + r.height - rs[i].y;
1838 }
1839 else
1840 rs[i].height = 0;
1841 }
1842 i++;
1843 }
1844
1845 n = i;
1846 #ifdef CONVERT_FROM_XRECT
1847 for (i = 0; i < n; i++)
1848 CONVERT_FROM_XRECT (rs[i], rects[i]);
1849 #endif
1850 return n;
1851 }
1852 }
1853
1854 /* EXPORT:
1855 Return in *NR the clipping rectangle for glyph string S. */
1856
1857 void
1858 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1859 {
1860 get_glyph_string_clip_rects (s, nr, 1);
1861 }
1862
1863
1864 /* EXPORT:
1865 Return the position and height of the phys cursor in window W.
1866 Set w->phys_cursor_width to width of phys cursor.
1867 */
1868
1869 void
1870 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1871 struct glyph *glyph, int *xp, int *yp, int *heightp)
1872 {
1873 struct frame *f = XFRAME (WINDOW_FRAME (w));
1874 int x, y, wd, h, h0, y0;
1875
1876 /* Compute the width of the rectangle to draw. If on a stretch
1877 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1878 rectangle as wide as the glyph, but use a canonical character
1879 width instead. */
1880 wd = glyph->pixel_width - 1;
1881 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1882 wd++; /* Why? */
1883 #endif
1884
1885 x = w->phys_cursor.x;
1886 if (x < 0)
1887 {
1888 wd += x;
1889 x = 0;
1890 }
1891
1892 if (glyph->type == STRETCH_GLYPH
1893 && !x_stretch_cursor_p)
1894 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1895 w->phys_cursor_width = wd;
1896
1897 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1898
1899 /* If y is below window bottom, ensure that we still see a cursor. */
1900 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1901
1902 h = max (h0, glyph->ascent + glyph->descent);
1903 h0 = min (h0, glyph->ascent + glyph->descent);
1904
1905 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1906 if (y < y0)
1907 {
1908 h = max (h - (y0 - y) + 1, h0);
1909 y = y0 - 1;
1910 }
1911 else
1912 {
1913 y0 = window_text_bottom_y (w) - h0;
1914 if (y > y0)
1915 {
1916 h += y - y0;
1917 y = y0;
1918 }
1919 }
1920
1921 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1922 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1923 *heightp = h;
1924 }
1925
1926 /*
1927 * Remember which glyph the mouse is over.
1928 */
1929
1930 void
1931 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1932 {
1933 Lisp_Object window;
1934 struct window *w;
1935 struct glyph_row *r, *gr, *end_row;
1936 enum window_part part;
1937 enum glyph_row_area area;
1938 int x, y, width, height;
1939
1940 /* Try to determine frame pixel position and size of the glyph under
1941 frame pixel coordinates X/Y on frame F. */
1942
1943 if (!f->glyphs_initialized_p
1944 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1945 NILP (window)))
1946 {
1947 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1948 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1949 goto virtual_glyph;
1950 }
1951
1952 w = XWINDOW (window);
1953 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1954 height = WINDOW_FRAME_LINE_HEIGHT (w);
1955
1956 x = window_relative_x_coord (w, part, gx);
1957 y = gy - WINDOW_TOP_EDGE_Y (w);
1958
1959 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1960 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1961
1962 if (w->pseudo_window_p)
1963 {
1964 area = TEXT_AREA;
1965 part = ON_MODE_LINE; /* Don't adjust margin. */
1966 goto text_glyph;
1967 }
1968
1969 switch (part)
1970 {
1971 case ON_LEFT_MARGIN:
1972 area = LEFT_MARGIN_AREA;
1973 goto text_glyph;
1974
1975 case ON_RIGHT_MARGIN:
1976 area = RIGHT_MARGIN_AREA;
1977 goto text_glyph;
1978
1979 case ON_HEADER_LINE:
1980 case ON_MODE_LINE:
1981 gr = (part == ON_HEADER_LINE
1982 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1983 : MATRIX_MODE_LINE_ROW (w->current_matrix));
1984 gy = gr->y;
1985 area = TEXT_AREA;
1986 goto text_glyph_row_found;
1987
1988 case ON_TEXT:
1989 area = TEXT_AREA;
1990
1991 text_glyph:
1992 gr = 0; gy = 0;
1993 for (; r <= end_row && r->enabled_p; ++r)
1994 if (r->y + r->height > y)
1995 {
1996 gr = r; gy = r->y;
1997 break;
1998 }
1999
2000 text_glyph_row_found:
2001 if (gr && gy <= y)
2002 {
2003 struct glyph *g = gr->glyphs[area];
2004 struct glyph *end = g + gr->used[area];
2005
2006 height = gr->height;
2007 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2008 if (gx + g->pixel_width > x)
2009 break;
2010
2011 if (g < end)
2012 {
2013 if (g->type == IMAGE_GLYPH)
2014 {
2015 /* Don't remember when mouse is over image, as
2016 image may have hot-spots. */
2017 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2018 return;
2019 }
2020 width = g->pixel_width;
2021 }
2022 else
2023 {
2024 /* Use nominal char spacing at end of line. */
2025 x -= gx;
2026 gx += (x / width) * width;
2027 }
2028
2029 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2030 gx += window_box_left_offset (w, area);
2031 }
2032 else
2033 {
2034 /* Use nominal line height at end of window. */
2035 gx = (x / width) * width;
2036 y -= gy;
2037 gy += (y / height) * height;
2038 }
2039 break;
2040
2041 case ON_LEFT_FRINGE:
2042 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2043 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2044 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2045 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2046 goto row_glyph;
2047
2048 case ON_RIGHT_FRINGE:
2049 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2050 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2051 : window_box_right_offset (w, TEXT_AREA));
2052 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2053 goto row_glyph;
2054
2055 case ON_SCROLL_BAR:
2056 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2057 ? 0
2058 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2059 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2060 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2061 : 0)));
2062 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2063
2064 row_glyph:
2065 gr = 0, gy = 0;
2066 for (; r <= end_row && r->enabled_p; ++r)
2067 if (r->y + r->height > y)
2068 {
2069 gr = r; gy = r->y;
2070 break;
2071 }
2072
2073 if (gr && gy <= y)
2074 height = gr->height;
2075 else
2076 {
2077 /* Use nominal line height at end of window. */
2078 y -= gy;
2079 gy += (y / height) * height;
2080 }
2081 break;
2082
2083 default:
2084 ;
2085 virtual_glyph:
2086 /* If there is no glyph under the mouse, then we divide the screen
2087 into a grid of the smallest glyph in the frame, and use that
2088 as our "glyph". */
2089
2090 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2091 round down even for negative values. */
2092 if (gx < 0)
2093 gx -= width - 1;
2094 if (gy < 0)
2095 gy -= height - 1;
2096
2097 gx = (gx / width) * width;
2098 gy = (gy / height) * height;
2099
2100 goto store_rect;
2101 }
2102
2103 gx += WINDOW_LEFT_EDGE_X (w);
2104 gy += WINDOW_TOP_EDGE_Y (w);
2105
2106 store_rect:
2107 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2108
2109 /* Visible feedback for debugging. */
2110 #if 0
2111 #if HAVE_X_WINDOWS
2112 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2113 f->output_data.x->normal_gc,
2114 gx, gy, width, height);
2115 #endif
2116 #endif
2117 }
2118
2119
2120 #endif /* HAVE_WINDOW_SYSTEM */
2121
2122 \f
2123 /***********************************************************************
2124 Lisp form evaluation
2125 ***********************************************************************/
2126
2127 /* Error handler for safe_eval and safe_call. */
2128
2129 static Lisp_Object
2130 safe_eval_handler (Lisp_Object arg)
2131 {
2132 add_to_log ("Error during redisplay: %S", arg, Qnil);
2133 return Qnil;
2134 }
2135
2136
2137 /* Evaluate SEXPR and return the result, or nil if something went
2138 wrong. Prevent redisplay during the evaluation. */
2139
2140 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2141 Return the result, or nil if something went wrong. Prevent
2142 redisplay during the evaluation. */
2143
2144 Lisp_Object
2145 safe_call (size_t nargs, Lisp_Object *args)
2146 {
2147 Lisp_Object val;
2148
2149 if (inhibit_eval_during_redisplay)
2150 val = Qnil;
2151 else
2152 {
2153 int count = SPECPDL_INDEX ();
2154 struct gcpro gcpro1;
2155
2156 GCPRO1 (args[0]);
2157 gcpro1.nvars = nargs;
2158 specbind (Qinhibit_redisplay, Qt);
2159 /* Use Qt to ensure debugger does not run,
2160 so there is no possibility of wanting to redisplay. */
2161 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2162 safe_eval_handler);
2163 UNGCPRO;
2164 val = unbind_to (count, val);
2165 }
2166
2167 return val;
2168 }
2169
2170
2171 /* Call function FN with one argument ARG.
2172 Return the result, or nil if something went wrong. */
2173
2174 Lisp_Object
2175 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2176 {
2177 Lisp_Object args[2];
2178 args[0] = fn;
2179 args[1] = arg;
2180 return safe_call (2, args);
2181 }
2182
2183 static Lisp_Object Qeval;
2184
2185 Lisp_Object
2186 safe_eval (Lisp_Object sexpr)
2187 {
2188 return safe_call1 (Qeval, sexpr);
2189 }
2190
2191 /* Call function FN with one argument ARG.
2192 Return the result, or nil if something went wrong. */
2193
2194 Lisp_Object
2195 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2196 {
2197 Lisp_Object args[3];
2198 args[0] = fn;
2199 args[1] = arg1;
2200 args[2] = arg2;
2201 return safe_call (3, args);
2202 }
2203
2204
2205 \f
2206 /***********************************************************************
2207 Debugging
2208 ***********************************************************************/
2209
2210 #if 0
2211
2212 /* Define CHECK_IT to perform sanity checks on iterators.
2213 This is for debugging. It is too slow to do unconditionally. */
2214
2215 static void
2216 check_it (it)
2217 struct it *it;
2218 {
2219 if (it->method == GET_FROM_STRING)
2220 {
2221 xassert (STRINGP (it->string));
2222 xassert (IT_STRING_CHARPOS (*it) >= 0);
2223 }
2224 else
2225 {
2226 xassert (IT_STRING_CHARPOS (*it) < 0);
2227 if (it->method == GET_FROM_BUFFER)
2228 {
2229 /* Check that character and byte positions agree. */
2230 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2231 }
2232 }
2233
2234 if (it->dpvec)
2235 xassert (it->current.dpvec_index >= 0);
2236 else
2237 xassert (it->current.dpvec_index < 0);
2238 }
2239
2240 #define CHECK_IT(IT) check_it ((IT))
2241
2242 #else /* not 0 */
2243
2244 #define CHECK_IT(IT) (void) 0
2245
2246 #endif /* not 0 */
2247
2248
2249 #if GLYPH_DEBUG
2250
2251 /* Check that the window end of window W is what we expect it
2252 to be---the last row in the current matrix displaying text. */
2253
2254 static void
2255 check_window_end (w)
2256 struct window *w;
2257 {
2258 if (!MINI_WINDOW_P (w)
2259 && !NILP (w->window_end_valid))
2260 {
2261 struct glyph_row *row;
2262 xassert ((row = MATRIX_ROW (w->current_matrix,
2263 XFASTINT (w->window_end_vpos)),
2264 !row->enabled_p
2265 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2266 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2267 }
2268 }
2269
2270 #define CHECK_WINDOW_END(W) check_window_end ((W))
2271
2272 #else /* not GLYPH_DEBUG */
2273
2274 #define CHECK_WINDOW_END(W) (void) 0
2275
2276 #endif /* not GLYPH_DEBUG */
2277
2278
2279 \f
2280 /***********************************************************************
2281 Iterator initialization
2282 ***********************************************************************/
2283
2284 /* Initialize IT for displaying current_buffer in window W, starting
2285 at character position CHARPOS. CHARPOS < 0 means that no buffer
2286 position is specified which is useful when the iterator is assigned
2287 a position later. BYTEPOS is the byte position corresponding to
2288 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2289
2290 If ROW is not null, calls to produce_glyphs with IT as parameter
2291 will produce glyphs in that row.
2292
2293 BASE_FACE_ID is the id of a base face to use. It must be one of
2294 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2295 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2296 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2297
2298 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2299 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2300 will be initialized to use the corresponding mode line glyph row of
2301 the desired matrix of W. */
2302
2303 void
2304 init_iterator (struct it *it, struct window *w,
2305 EMACS_INT charpos, EMACS_INT bytepos,
2306 struct glyph_row *row, enum face_id base_face_id)
2307 {
2308 int highlight_region_p;
2309 enum face_id remapped_base_face_id = base_face_id;
2310
2311 /* Some precondition checks. */
2312 xassert (w != NULL && it != NULL);
2313 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2314 && charpos <= ZV));
2315
2316 /* If face attributes have been changed since the last redisplay,
2317 free realized faces now because they depend on face definitions
2318 that might have changed. Don't free faces while there might be
2319 desired matrices pending which reference these faces. */
2320 if (face_change_count && !inhibit_free_realized_faces)
2321 {
2322 face_change_count = 0;
2323 free_all_realized_faces (Qnil);
2324 }
2325
2326 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2327 if (! NILP (Vface_remapping_alist))
2328 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2329
2330 /* Use one of the mode line rows of W's desired matrix if
2331 appropriate. */
2332 if (row == NULL)
2333 {
2334 if (base_face_id == MODE_LINE_FACE_ID
2335 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2336 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2337 else if (base_face_id == HEADER_LINE_FACE_ID)
2338 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2339 }
2340
2341 /* Clear IT. */
2342 memset (it, 0, sizeof *it);
2343 it->current.overlay_string_index = -1;
2344 it->current.dpvec_index = -1;
2345 it->base_face_id = remapped_base_face_id;
2346 it->string = Qnil;
2347 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2348 it->paragraph_embedding = L2R;
2349 it->bidi_it.string.lstring = Qnil;
2350 it->bidi_it.string.s = NULL;
2351 it->bidi_it.string.bufpos = 0;
2352
2353 /* The window in which we iterate over current_buffer: */
2354 XSETWINDOW (it->window, w);
2355 it->w = w;
2356 it->f = XFRAME (w->frame);
2357
2358 it->cmp_it.id = -1;
2359
2360 /* Extra space between lines (on window systems only). */
2361 if (base_face_id == DEFAULT_FACE_ID
2362 && FRAME_WINDOW_P (it->f))
2363 {
2364 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2365 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2366 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2367 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2368 * FRAME_LINE_HEIGHT (it->f));
2369 else if (it->f->extra_line_spacing > 0)
2370 it->extra_line_spacing = it->f->extra_line_spacing;
2371 it->max_extra_line_spacing = 0;
2372 }
2373
2374 /* If realized faces have been removed, e.g. because of face
2375 attribute changes of named faces, recompute them. When running
2376 in batch mode, the face cache of the initial frame is null. If
2377 we happen to get called, make a dummy face cache. */
2378 if (FRAME_FACE_CACHE (it->f) == NULL)
2379 init_frame_faces (it->f);
2380 if (FRAME_FACE_CACHE (it->f)->used == 0)
2381 recompute_basic_faces (it->f);
2382
2383 /* Current value of the `slice', `space-width', and 'height' properties. */
2384 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2385 it->space_width = Qnil;
2386 it->font_height = Qnil;
2387 it->override_ascent = -1;
2388
2389 /* Are control characters displayed as `^C'? */
2390 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2391
2392 /* -1 means everything between a CR and the following line end
2393 is invisible. >0 means lines indented more than this value are
2394 invisible. */
2395 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2396 ? XFASTINT (BVAR (current_buffer, selective_display))
2397 : (!NILP (BVAR (current_buffer, selective_display))
2398 ? -1 : 0));
2399 it->selective_display_ellipsis_p
2400 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2401
2402 /* Display table to use. */
2403 it->dp = window_display_table (w);
2404
2405 /* Are multibyte characters enabled in current_buffer? */
2406 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2407
2408 /* Non-zero if we should highlight the region. */
2409 highlight_region_p
2410 = (!NILP (Vtransient_mark_mode)
2411 && !NILP (BVAR (current_buffer, mark_active))
2412 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2413
2414 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2415 start and end of a visible region in window IT->w. Set both to
2416 -1 to indicate no region. */
2417 if (highlight_region_p
2418 /* Maybe highlight only in selected window. */
2419 && (/* Either show region everywhere. */
2420 highlight_nonselected_windows
2421 /* Or show region in the selected window. */
2422 || w == XWINDOW (selected_window)
2423 /* Or show the region if we are in the mini-buffer and W is
2424 the window the mini-buffer refers to. */
2425 || (MINI_WINDOW_P (XWINDOW (selected_window))
2426 && WINDOWP (minibuf_selected_window)
2427 && w == XWINDOW (minibuf_selected_window))))
2428 {
2429 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2430 it->region_beg_charpos = min (PT, markpos);
2431 it->region_end_charpos = max (PT, markpos);
2432 }
2433 else
2434 it->region_beg_charpos = it->region_end_charpos = -1;
2435
2436 /* Get the position at which the redisplay_end_trigger hook should
2437 be run, if it is to be run at all. */
2438 if (MARKERP (w->redisplay_end_trigger)
2439 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2440 it->redisplay_end_trigger_charpos
2441 = marker_position (w->redisplay_end_trigger);
2442 else if (INTEGERP (w->redisplay_end_trigger))
2443 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2444
2445 /* Correct bogus values of tab_width. */
2446 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2447 if (it->tab_width <= 0 || it->tab_width > 1000)
2448 it->tab_width = 8;
2449
2450 /* Are lines in the display truncated? */
2451 if (base_face_id != DEFAULT_FACE_ID
2452 || XINT (it->w->hscroll)
2453 || (! WINDOW_FULL_WIDTH_P (it->w)
2454 && ((!NILP (Vtruncate_partial_width_windows)
2455 && !INTEGERP (Vtruncate_partial_width_windows))
2456 || (INTEGERP (Vtruncate_partial_width_windows)
2457 && (WINDOW_TOTAL_COLS (it->w)
2458 < XINT (Vtruncate_partial_width_windows))))))
2459 it->line_wrap = TRUNCATE;
2460 else if (NILP (BVAR (current_buffer, truncate_lines)))
2461 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2462 ? WINDOW_WRAP : WORD_WRAP;
2463 else
2464 it->line_wrap = TRUNCATE;
2465
2466 /* Get dimensions of truncation and continuation glyphs. These are
2467 displayed as fringe bitmaps under X, so we don't need them for such
2468 frames. */
2469 if (!FRAME_WINDOW_P (it->f))
2470 {
2471 if (it->line_wrap == TRUNCATE)
2472 {
2473 /* We will need the truncation glyph. */
2474 xassert (it->glyph_row == NULL);
2475 produce_special_glyphs (it, IT_TRUNCATION);
2476 it->truncation_pixel_width = it->pixel_width;
2477 }
2478 else
2479 {
2480 /* We will need the continuation glyph. */
2481 xassert (it->glyph_row == NULL);
2482 produce_special_glyphs (it, IT_CONTINUATION);
2483 it->continuation_pixel_width = it->pixel_width;
2484 }
2485
2486 /* Reset these values to zero because the produce_special_glyphs
2487 above has changed them. */
2488 it->pixel_width = it->ascent = it->descent = 0;
2489 it->phys_ascent = it->phys_descent = 0;
2490 }
2491
2492 /* Set this after getting the dimensions of truncation and
2493 continuation glyphs, so that we don't produce glyphs when calling
2494 produce_special_glyphs, above. */
2495 it->glyph_row = row;
2496 it->area = TEXT_AREA;
2497
2498 /* Forget any previous info about this row being reversed. */
2499 if (it->glyph_row)
2500 it->glyph_row->reversed_p = 0;
2501
2502 /* Get the dimensions of the display area. The display area
2503 consists of the visible window area plus a horizontally scrolled
2504 part to the left of the window. All x-values are relative to the
2505 start of this total display area. */
2506 if (base_face_id != DEFAULT_FACE_ID)
2507 {
2508 /* Mode lines, menu bar in terminal frames. */
2509 it->first_visible_x = 0;
2510 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2511 }
2512 else
2513 {
2514 it->first_visible_x
2515 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2516 it->last_visible_x = (it->first_visible_x
2517 + window_box_width (w, TEXT_AREA));
2518
2519 /* If we truncate lines, leave room for the truncator glyph(s) at
2520 the right margin. Otherwise, leave room for the continuation
2521 glyph(s). Truncation and continuation glyphs are not inserted
2522 for window-based redisplay. */
2523 if (!FRAME_WINDOW_P (it->f))
2524 {
2525 if (it->line_wrap == TRUNCATE)
2526 it->last_visible_x -= it->truncation_pixel_width;
2527 else
2528 it->last_visible_x -= it->continuation_pixel_width;
2529 }
2530
2531 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2532 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2533 }
2534
2535 /* Leave room for a border glyph. */
2536 if (!FRAME_WINDOW_P (it->f)
2537 && !WINDOW_RIGHTMOST_P (it->w))
2538 it->last_visible_x -= 1;
2539
2540 it->last_visible_y = window_text_bottom_y (w);
2541
2542 /* For mode lines and alike, arrange for the first glyph having a
2543 left box line if the face specifies a box. */
2544 if (base_face_id != DEFAULT_FACE_ID)
2545 {
2546 struct face *face;
2547
2548 it->face_id = remapped_base_face_id;
2549
2550 /* If we have a boxed mode line, make the first character appear
2551 with a left box line. */
2552 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2553 if (face->box != FACE_NO_BOX)
2554 it->start_of_box_run_p = 1;
2555 }
2556
2557 /* If a buffer position was specified, set the iterator there,
2558 getting overlays and face properties from that position. */
2559 if (charpos >= BUF_BEG (current_buffer))
2560 {
2561 it->end_charpos = ZV;
2562 it->face_id = -1;
2563 IT_CHARPOS (*it) = charpos;
2564
2565 /* Compute byte position if not specified. */
2566 if (bytepos < charpos)
2567 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2568 else
2569 IT_BYTEPOS (*it) = bytepos;
2570
2571 it->start = it->current;
2572 /* Do we need to reorder bidirectional text? Not if this is a
2573 unibyte buffer: by definition, none of the single-byte
2574 characters are strong R2L, so no reordering is needed. And
2575 bidi.c doesn't support unibyte buffers anyway. */
2576 it->bidi_p =
2577 !NILP (BVAR (current_buffer, bidi_display_reordering))
2578 && it->multibyte_p;
2579
2580 /* If we are to reorder bidirectional text, init the bidi
2581 iterator. */
2582 if (it->bidi_p)
2583 {
2584 /* Note the paragraph direction that this buffer wants to
2585 use. */
2586 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2587 Qleft_to_right))
2588 it->paragraph_embedding = L2R;
2589 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2590 Qright_to_left))
2591 it->paragraph_embedding = R2L;
2592 else
2593 it->paragraph_embedding = NEUTRAL_DIR;
2594 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2595 &it->bidi_it);
2596 }
2597
2598 /* Compute faces etc. */
2599 reseat (it, it->current.pos, 1);
2600 }
2601
2602 CHECK_IT (it);
2603 }
2604
2605
2606 /* Initialize IT for the display of window W with window start POS. */
2607
2608 void
2609 start_display (struct it *it, struct window *w, struct text_pos pos)
2610 {
2611 struct glyph_row *row;
2612 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2613
2614 row = w->desired_matrix->rows + first_vpos;
2615 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2616 it->first_vpos = first_vpos;
2617
2618 /* Don't reseat to previous visible line start if current start
2619 position is in a string or image. */
2620 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2621 {
2622 int start_at_line_beg_p;
2623 int first_y = it->current_y;
2624
2625 /* If window start is not at a line start, skip forward to POS to
2626 get the correct continuation lines width. */
2627 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2628 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2629 if (!start_at_line_beg_p)
2630 {
2631 int new_x;
2632
2633 reseat_at_previous_visible_line_start (it);
2634 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2635
2636 new_x = it->current_x + it->pixel_width;
2637
2638 /* If lines are continued, this line may end in the middle
2639 of a multi-glyph character (e.g. a control character
2640 displayed as \003, or in the middle of an overlay
2641 string). In this case move_it_to above will not have
2642 taken us to the start of the continuation line but to the
2643 end of the continued line. */
2644 if (it->current_x > 0
2645 && it->line_wrap != TRUNCATE /* Lines are continued. */
2646 && (/* And glyph doesn't fit on the line. */
2647 new_x > it->last_visible_x
2648 /* Or it fits exactly and we're on a window
2649 system frame. */
2650 || (new_x == it->last_visible_x
2651 && FRAME_WINDOW_P (it->f))))
2652 {
2653 if (it->current.dpvec_index >= 0
2654 || it->current.overlay_string_index >= 0)
2655 {
2656 set_iterator_to_next (it, 1);
2657 move_it_in_display_line_to (it, -1, -1, 0);
2658 }
2659
2660 it->continuation_lines_width += it->current_x;
2661 }
2662
2663 /* We're starting a new display line, not affected by the
2664 height of the continued line, so clear the appropriate
2665 fields in the iterator structure. */
2666 it->max_ascent = it->max_descent = 0;
2667 it->max_phys_ascent = it->max_phys_descent = 0;
2668
2669 it->current_y = first_y;
2670 it->vpos = 0;
2671 it->current_x = it->hpos = 0;
2672 }
2673 }
2674 }
2675
2676
2677 /* Return 1 if POS is a position in ellipses displayed for invisible
2678 text. W is the window we display, for text property lookup. */
2679
2680 static int
2681 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2682 {
2683 Lisp_Object prop, window;
2684 int ellipses_p = 0;
2685 EMACS_INT charpos = CHARPOS (pos->pos);
2686
2687 /* If POS specifies a position in a display vector, this might
2688 be for an ellipsis displayed for invisible text. We won't
2689 get the iterator set up for delivering that ellipsis unless
2690 we make sure that it gets aware of the invisible text. */
2691 if (pos->dpvec_index >= 0
2692 && pos->overlay_string_index < 0
2693 && CHARPOS (pos->string_pos) < 0
2694 && charpos > BEGV
2695 && (XSETWINDOW (window, w),
2696 prop = Fget_char_property (make_number (charpos),
2697 Qinvisible, window),
2698 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2699 {
2700 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2701 window);
2702 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2703 }
2704
2705 return ellipses_p;
2706 }
2707
2708
2709 /* Initialize IT for stepping through current_buffer in window W,
2710 starting at position POS that includes overlay string and display
2711 vector/ control character translation position information. Value
2712 is zero if there are overlay strings with newlines at POS. */
2713
2714 static int
2715 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2716 {
2717 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2718 int i, overlay_strings_with_newlines = 0;
2719
2720 /* If POS specifies a position in a display vector, this might
2721 be for an ellipsis displayed for invisible text. We won't
2722 get the iterator set up for delivering that ellipsis unless
2723 we make sure that it gets aware of the invisible text. */
2724 if (in_ellipses_for_invisible_text_p (pos, w))
2725 {
2726 --charpos;
2727 bytepos = 0;
2728 }
2729
2730 /* Keep in mind: the call to reseat in init_iterator skips invisible
2731 text, so we might end up at a position different from POS. This
2732 is only a problem when POS is a row start after a newline and an
2733 overlay starts there with an after-string, and the overlay has an
2734 invisible property. Since we don't skip invisible text in
2735 display_line and elsewhere immediately after consuming the
2736 newline before the row start, such a POS will not be in a string,
2737 but the call to init_iterator below will move us to the
2738 after-string. */
2739 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2740
2741 /* This only scans the current chunk -- it should scan all chunks.
2742 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2743 to 16 in 22.1 to make this a lesser problem. */
2744 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2745 {
2746 const char *s = SSDATA (it->overlay_strings[i]);
2747 const char *e = s + SBYTES (it->overlay_strings[i]);
2748
2749 while (s < e && *s != '\n')
2750 ++s;
2751
2752 if (s < e)
2753 {
2754 overlay_strings_with_newlines = 1;
2755 break;
2756 }
2757 }
2758
2759 /* If position is within an overlay string, set up IT to the right
2760 overlay string. */
2761 if (pos->overlay_string_index >= 0)
2762 {
2763 int relative_index;
2764
2765 /* If the first overlay string happens to have a `display'
2766 property for an image, the iterator will be set up for that
2767 image, and we have to undo that setup first before we can
2768 correct the overlay string index. */
2769 if (it->method == GET_FROM_IMAGE)
2770 pop_it (it);
2771
2772 /* We already have the first chunk of overlay strings in
2773 IT->overlay_strings. Load more until the one for
2774 pos->overlay_string_index is in IT->overlay_strings. */
2775 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2776 {
2777 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2778 it->current.overlay_string_index = 0;
2779 while (n--)
2780 {
2781 load_overlay_strings (it, 0);
2782 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2783 }
2784 }
2785
2786 it->current.overlay_string_index = pos->overlay_string_index;
2787 relative_index = (it->current.overlay_string_index
2788 % OVERLAY_STRING_CHUNK_SIZE);
2789 it->string = it->overlay_strings[relative_index];
2790 xassert (STRINGP (it->string));
2791 it->current.string_pos = pos->string_pos;
2792 it->method = GET_FROM_STRING;
2793 }
2794
2795 if (CHARPOS (pos->string_pos) >= 0)
2796 {
2797 /* Recorded position is not in an overlay string, but in another
2798 string. This can only be a string from a `display' property.
2799 IT should already be filled with that string. */
2800 it->current.string_pos = pos->string_pos;
2801 xassert (STRINGP (it->string));
2802 }
2803
2804 /* Restore position in display vector translations, control
2805 character translations or ellipses. */
2806 if (pos->dpvec_index >= 0)
2807 {
2808 if (it->dpvec == NULL)
2809 get_next_display_element (it);
2810 xassert (it->dpvec && it->current.dpvec_index == 0);
2811 it->current.dpvec_index = pos->dpvec_index;
2812 }
2813
2814 CHECK_IT (it);
2815 return !overlay_strings_with_newlines;
2816 }
2817
2818
2819 /* Initialize IT for stepping through current_buffer in window W
2820 starting at ROW->start. */
2821
2822 static void
2823 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2824 {
2825 init_from_display_pos (it, w, &row->start);
2826 it->start = row->start;
2827 it->continuation_lines_width = row->continuation_lines_width;
2828 CHECK_IT (it);
2829 }
2830
2831
2832 /* Initialize IT for stepping through current_buffer in window W
2833 starting in the line following ROW, i.e. starting at ROW->end.
2834 Value is zero if there are overlay strings with newlines at ROW's
2835 end position. */
2836
2837 static int
2838 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2839 {
2840 int success = 0;
2841
2842 if (init_from_display_pos (it, w, &row->end))
2843 {
2844 if (row->continued_p)
2845 it->continuation_lines_width
2846 = row->continuation_lines_width + row->pixel_width;
2847 CHECK_IT (it);
2848 success = 1;
2849 }
2850
2851 return success;
2852 }
2853
2854
2855
2856 \f
2857 /***********************************************************************
2858 Text properties
2859 ***********************************************************************/
2860
2861 /* Called when IT reaches IT->stop_charpos. Handle text property and
2862 overlay changes. Set IT->stop_charpos to the next position where
2863 to stop. */
2864
2865 static void
2866 handle_stop (struct it *it)
2867 {
2868 enum prop_handled handled;
2869 int handle_overlay_change_p;
2870 struct props *p;
2871
2872 it->dpvec = NULL;
2873 it->current.dpvec_index = -1;
2874 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2875 it->ignore_overlay_strings_at_pos_p = 0;
2876 it->ellipsis_p = 0;
2877
2878 /* Use face of preceding text for ellipsis (if invisible) */
2879 if (it->selective_display_ellipsis_p)
2880 it->saved_face_id = it->face_id;
2881
2882 do
2883 {
2884 handled = HANDLED_NORMALLY;
2885
2886 /* Call text property handlers. */
2887 for (p = it_props; p->handler; ++p)
2888 {
2889 handled = p->handler (it);
2890
2891 if (handled == HANDLED_RECOMPUTE_PROPS)
2892 break;
2893 else if (handled == HANDLED_RETURN)
2894 {
2895 /* We still want to show before and after strings from
2896 overlays even if the actual buffer text is replaced. */
2897 if (!handle_overlay_change_p
2898 || it->sp > 1
2899 || !get_overlay_strings_1 (it, 0, 0))
2900 {
2901 if (it->ellipsis_p)
2902 setup_for_ellipsis (it, 0);
2903 /* When handling a display spec, we might load an
2904 empty string. In that case, discard it here. We
2905 used to discard it in handle_single_display_spec,
2906 but that causes get_overlay_strings_1, above, to
2907 ignore overlay strings that we must check. */
2908 if (STRINGP (it->string) && !SCHARS (it->string))
2909 pop_it (it);
2910 return;
2911 }
2912 else if (STRINGP (it->string) && !SCHARS (it->string))
2913 pop_it (it);
2914 else
2915 {
2916 it->ignore_overlay_strings_at_pos_p = 1;
2917 it->string_from_display_prop_p = 0;
2918 handle_overlay_change_p = 0;
2919 }
2920 handled = HANDLED_RECOMPUTE_PROPS;
2921 break;
2922 }
2923 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2924 handle_overlay_change_p = 0;
2925 }
2926
2927 if (handled != HANDLED_RECOMPUTE_PROPS)
2928 {
2929 /* Don't check for overlay strings below when set to deliver
2930 characters from a display vector. */
2931 if (it->method == GET_FROM_DISPLAY_VECTOR)
2932 handle_overlay_change_p = 0;
2933
2934 /* Handle overlay changes.
2935 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2936 if it finds overlays. */
2937 if (handle_overlay_change_p)
2938 handled = handle_overlay_change (it);
2939 }
2940
2941 if (it->ellipsis_p)
2942 {
2943 setup_for_ellipsis (it, 0);
2944 break;
2945 }
2946 }
2947 while (handled == HANDLED_RECOMPUTE_PROPS);
2948
2949 /* Determine where to stop next. */
2950 if (handled == HANDLED_NORMALLY)
2951 compute_stop_pos (it);
2952 }
2953
2954
2955 /* Compute IT->stop_charpos from text property and overlay change
2956 information for IT's current position. */
2957
2958 static void
2959 compute_stop_pos (struct it *it)
2960 {
2961 register INTERVAL iv, next_iv;
2962 Lisp_Object object, limit, position;
2963 EMACS_INT charpos, bytepos;
2964
2965 /* If nowhere else, stop at the end. */
2966 it->stop_charpos = it->end_charpos;
2967
2968 if (STRINGP (it->string))
2969 {
2970 /* Strings are usually short, so don't limit the search for
2971 properties. */
2972 object = it->string;
2973 limit = Qnil;
2974 charpos = IT_STRING_CHARPOS (*it);
2975 bytepos = IT_STRING_BYTEPOS (*it);
2976 }
2977 else
2978 {
2979 EMACS_INT pos;
2980
2981 /* If next overlay change is in front of the current stop pos
2982 (which is IT->end_charpos), stop there. Note: value of
2983 next_overlay_change is point-max if no overlay change
2984 follows. */
2985 charpos = IT_CHARPOS (*it);
2986 bytepos = IT_BYTEPOS (*it);
2987 pos = next_overlay_change (charpos);
2988 if (pos < it->stop_charpos)
2989 it->stop_charpos = pos;
2990
2991 /* If showing the region, we have to stop at the region
2992 start or end because the face might change there. */
2993 if (it->region_beg_charpos > 0)
2994 {
2995 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2996 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2997 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2998 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2999 }
3000
3001 /* Set up variables for computing the stop position from text
3002 property changes. */
3003 XSETBUFFER (object, current_buffer);
3004 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3005 }
3006
3007 /* Get the interval containing IT's position. Value is a null
3008 interval if there isn't such an interval. */
3009 position = make_number (charpos);
3010 iv = validate_interval_range (object, &position, &position, 0);
3011 if (!NULL_INTERVAL_P (iv))
3012 {
3013 Lisp_Object values_here[LAST_PROP_IDX];
3014 struct props *p;
3015
3016 /* Get properties here. */
3017 for (p = it_props; p->handler; ++p)
3018 values_here[p->idx] = textget (iv->plist, *p->name);
3019
3020 /* Look for an interval following iv that has different
3021 properties. */
3022 for (next_iv = next_interval (iv);
3023 (!NULL_INTERVAL_P (next_iv)
3024 && (NILP (limit)
3025 || XFASTINT (limit) > next_iv->position));
3026 next_iv = next_interval (next_iv))
3027 {
3028 for (p = it_props; p->handler; ++p)
3029 {
3030 Lisp_Object new_value;
3031
3032 new_value = textget (next_iv->plist, *p->name);
3033 if (!EQ (values_here[p->idx], new_value))
3034 break;
3035 }
3036
3037 if (p->handler)
3038 break;
3039 }
3040
3041 if (!NULL_INTERVAL_P (next_iv))
3042 {
3043 if (INTEGERP (limit)
3044 && next_iv->position >= XFASTINT (limit))
3045 /* No text property change up to limit. */
3046 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3047 else
3048 /* Text properties change in next_iv. */
3049 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3050 }
3051 }
3052
3053 if (it->cmp_it.id < 0)
3054 {
3055 EMACS_INT stoppos = it->end_charpos;
3056
3057 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3058 stoppos = -1;
3059 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3060 stoppos, it->string);
3061 }
3062
3063 xassert (STRINGP (it->string)
3064 || (it->stop_charpos >= BEGV
3065 && it->stop_charpos >= IT_CHARPOS (*it)));
3066 }
3067
3068
3069 /* Return the position of the next overlay change after POS in
3070 current_buffer. Value is point-max if no overlay change
3071 follows. This is like `next-overlay-change' but doesn't use
3072 xmalloc. */
3073
3074 static EMACS_INT
3075 next_overlay_change (EMACS_INT pos)
3076 {
3077 int noverlays;
3078 EMACS_INT endpos;
3079 Lisp_Object *overlays;
3080 int i;
3081
3082 /* Get all overlays at the given position. */
3083 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3084
3085 /* If any of these overlays ends before endpos,
3086 use its ending point instead. */
3087 for (i = 0; i < noverlays; ++i)
3088 {
3089 Lisp_Object oend;
3090 EMACS_INT oendpos;
3091
3092 oend = OVERLAY_END (overlays[i]);
3093 oendpos = OVERLAY_POSITION (oend);
3094 endpos = min (endpos, oendpos);
3095 }
3096
3097 return endpos;
3098 }
3099
3100 /* Return the character position of a display string at or after
3101 position specified by POSITION. If no display string exists at or
3102 after POSITION, return ZV. A display string is either an overlay
3103 with `display' property whose value is a string, or a `display'
3104 text property whose value is a string. STRING is data about the
3105 string to iterate; if STRING->lstring is nil, we are iterating a
3106 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3107 on a GUI frame. */
3108 EMACS_INT
3109 compute_display_string_pos (struct text_pos *position,
3110 struct bidi_string_data *string, int frame_window_p)
3111 {
3112 /* OBJECT = nil means current buffer. */
3113 Lisp_Object object =
3114 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3115 Lisp_Object pos, spec;
3116 EMACS_INT eob = STRINGP (object) ? string->schars : ZV;
3117 EMACS_INT begb = STRINGP (object) ? 0 : BEGV;
3118 EMACS_INT bufpos, charpos = CHARPOS (*position);
3119 struct text_pos tpos;
3120
3121 if (charpos >= eob
3122 /* We don't support display properties whose values are strings
3123 that have display string properties. */
3124 || string->from_disp_str
3125 /* C strings cannot have display properties. */
3126 || (string->s && !STRINGP (object)))
3127 return eob;
3128
3129 /* If the character at CHARPOS is where the display string begins,
3130 return CHARPOS. */
3131 pos = make_number (charpos);
3132 if (STRINGP (object))
3133 bufpos = string->bufpos;
3134 else
3135 bufpos = charpos;
3136 tpos = *position;
3137 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3138 && (charpos <= begb
3139 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3140 object),
3141 spec))
3142 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3143 frame_window_p))
3144 return charpos;
3145
3146 /* Look forward for the first character with a `display' property
3147 that will replace the underlying text when displayed. */
3148 do {
3149 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3150 CHARPOS (tpos) = XFASTINT (pos);
3151 if (STRINGP (object))
3152 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3153 else
3154 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3155 if (CHARPOS (tpos) >= eob)
3156 break;
3157 spec = Fget_char_property (pos, Qdisplay, object);
3158 if (!STRINGP (object))
3159 bufpos = CHARPOS (tpos);
3160 } while (NILP (spec)
3161 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3162 frame_window_p));
3163
3164 return CHARPOS (tpos);
3165 }
3166
3167 /* Return the character position of the end of the display string that
3168 started at CHARPOS. A display string is either an overlay with
3169 `display' property whose value is a string or a `display' text
3170 property whose value is a string. */
3171 EMACS_INT
3172 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3173 {
3174 /* OBJECT = nil means current buffer. */
3175 Lisp_Object object =
3176 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3177 Lisp_Object pos = make_number (charpos);
3178 EMACS_INT eob = STRINGP (object) ? string->schars : ZV;
3179
3180 if (charpos >= eob || (string->s && !STRINGP (object)))
3181 return eob;
3182
3183 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3184 abort ();
3185
3186 /* Look forward for the first character where the `display' property
3187 changes. */
3188 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3189
3190 return XFASTINT (pos);
3191 }
3192
3193
3194 \f
3195 /***********************************************************************
3196 Fontification
3197 ***********************************************************************/
3198
3199 /* Handle changes in the `fontified' property of the current buffer by
3200 calling hook functions from Qfontification_functions to fontify
3201 regions of text. */
3202
3203 static enum prop_handled
3204 handle_fontified_prop (struct it *it)
3205 {
3206 Lisp_Object prop, pos;
3207 enum prop_handled handled = HANDLED_NORMALLY;
3208
3209 if (!NILP (Vmemory_full))
3210 return handled;
3211
3212 /* Get the value of the `fontified' property at IT's current buffer
3213 position. (The `fontified' property doesn't have a special
3214 meaning in strings.) If the value is nil, call functions from
3215 Qfontification_functions. */
3216 if (!STRINGP (it->string)
3217 && it->s == NULL
3218 && !NILP (Vfontification_functions)
3219 && !NILP (Vrun_hooks)
3220 && (pos = make_number (IT_CHARPOS (*it)),
3221 prop = Fget_char_property (pos, Qfontified, Qnil),
3222 /* Ignore the special cased nil value always present at EOB since
3223 no amount of fontifying will be able to change it. */
3224 NILP (prop) && IT_CHARPOS (*it) < Z))
3225 {
3226 int count = SPECPDL_INDEX ();
3227 Lisp_Object val;
3228 struct buffer *obuf = current_buffer;
3229 int begv = BEGV, zv = ZV;
3230 int old_clip_changed = current_buffer->clip_changed;
3231
3232 val = Vfontification_functions;
3233 specbind (Qfontification_functions, Qnil);
3234
3235 xassert (it->end_charpos == ZV);
3236
3237 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3238 safe_call1 (val, pos);
3239 else
3240 {
3241 Lisp_Object fns, fn;
3242 struct gcpro gcpro1, gcpro2;
3243
3244 fns = Qnil;
3245 GCPRO2 (val, fns);
3246
3247 for (; CONSP (val); val = XCDR (val))
3248 {
3249 fn = XCAR (val);
3250
3251 if (EQ (fn, Qt))
3252 {
3253 /* A value of t indicates this hook has a local
3254 binding; it means to run the global binding too.
3255 In a global value, t should not occur. If it
3256 does, we must ignore it to avoid an endless
3257 loop. */
3258 for (fns = Fdefault_value (Qfontification_functions);
3259 CONSP (fns);
3260 fns = XCDR (fns))
3261 {
3262 fn = XCAR (fns);
3263 if (!EQ (fn, Qt))
3264 safe_call1 (fn, pos);
3265 }
3266 }
3267 else
3268 safe_call1 (fn, pos);
3269 }
3270
3271 UNGCPRO;
3272 }
3273
3274 unbind_to (count, Qnil);
3275
3276 /* Fontification functions routinely call `save-restriction'.
3277 Normally, this tags clip_changed, which can confuse redisplay
3278 (see discussion in Bug#6671). Since we don't perform any
3279 special handling of fontification changes in the case where
3280 `save-restriction' isn't called, there's no point doing so in
3281 this case either. So, if the buffer's restrictions are
3282 actually left unchanged, reset clip_changed. */
3283 if (obuf == current_buffer)
3284 {
3285 if (begv == BEGV && zv == ZV)
3286 current_buffer->clip_changed = old_clip_changed;
3287 }
3288 /* There isn't much we can reasonably do to protect against
3289 misbehaving fontification, but here's a fig leaf. */
3290 else if (!NILP (BVAR (obuf, name)))
3291 set_buffer_internal_1 (obuf);
3292
3293 /* The fontification code may have added/removed text.
3294 It could do even a lot worse, but let's at least protect against
3295 the most obvious case where only the text past `pos' gets changed',
3296 as is/was done in grep.el where some escapes sequences are turned
3297 into face properties (bug#7876). */
3298 it->end_charpos = ZV;
3299
3300 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3301 something. This avoids an endless loop if they failed to
3302 fontify the text for which reason ever. */
3303 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3304 handled = HANDLED_RECOMPUTE_PROPS;
3305 }
3306
3307 return handled;
3308 }
3309
3310
3311 \f
3312 /***********************************************************************
3313 Faces
3314 ***********************************************************************/
3315
3316 /* Set up iterator IT from face properties at its current position.
3317 Called from handle_stop. */
3318
3319 static enum prop_handled
3320 handle_face_prop (struct it *it)
3321 {
3322 int new_face_id;
3323 EMACS_INT next_stop;
3324
3325 if (!STRINGP (it->string))
3326 {
3327 new_face_id
3328 = face_at_buffer_position (it->w,
3329 IT_CHARPOS (*it),
3330 it->region_beg_charpos,
3331 it->region_end_charpos,
3332 &next_stop,
3333 (IT_CHARPOS (*it)
3334 + TEXT_PROP_DISTANCE_LIMIT),
3335 0, it->base_face_id);
3336
3337 /* Is this a start of a run of characters with box face?
3338 Caveat: this can be called for a freshly initialized
3339 iterator; face_id is -1 in this case. We know that the new
3340 face will not change until limit, i.e. if the new face has a
3341 box, all characters up to limit will have one. But, as
3342 usual, we don't know whether limit is really the end. */
3343 if (new_face_id != it->face_id)
3344 {
3345 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3346
3347 /* If new face has a box but old face has not, this is
3348 the start of a run of characters with box, i.e. it has
3349 a shadow on the left side. The value of face_id of the
3350 iterator will be -1 if this is the initial call that gets
3351 the face. In this case, we have to look in front of IT's
3352 position and see whether there is a face != new_face_id. */
3353 it->start_of_box_run_p
3354 = (new_face->box != FACE_NO_BOX
3355 && (it->face_id >= 0
3356 || IT_CHARPOS (*it) == BEG
3357 || new_face_id != face_before_it_pos (it)));
3358 it->face_box_p = new_face->box != FACE_NO_BOX;
3359 }
3360 }
3361 else
3362 {
3363 int base_face_id;
3364 EMACS_INT bufpos;
3365 int i;
3366 Lisp_Object from_overlay
3367 = (it->current.overlay_string_index >= 0
3368 ? it->string_overlays[it->current.overlay_string_index]
3369 : Qnil);
3370
3371 /* See if we got to this string directly or indirectly from
3372 an overlay property. That includes the before-string or
3373 after-string of an overlay, strings in display properties
3374 provided by an overlay, their text properties, etc.
3375
3376 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3377 if (! NILP (from_overlay))
3378 for (i = it->sp - 1; i >= 0; i--)
3379 {
3380 if (it->stack[i].current.overlay_string_index >= 0)
3381 from_overlay
3382 = it->string_overlays[it->stack[i].current.overlay_string_index];
3383 else if (! NILP (it->stack[i].from_overlay))
3384 from_overlay = it->stack[i].from_overlay;
3385
3386 if (!NILP (from_overlay))
3387 break;
3388 }
3389
3390 if (! NILP (from_overlay))
3391 {
3392 bufpos = IT_CHARPOS (*it);
3393 /* For a string from an overlay, the base face depends
3394 only on text properties and ignores overlays. */
3395 base_face_id
3396 = face_for_overlay_string (it->w,
3397 IT_CHARPOS (*it),
3398 it->region_beg_charpos,
3399 it->region_end_charpos,
3400 &next_stop,
3401 (IT_CHARPOS (*it)
3402 + TEXT_PROP_DISTANCE_LIMIT),
3403 0,
3404 from_overlay);
3405 }
3406 else
3407 {
3408 bufpos = 0;
3409
3410 /* For strings from a `display' property, use the face at
3411 IT's current buffer position as the base face to merge
3412 with, so that overlay strings appear in the same face as
3413 surrounding text, unless they specify their own
3414 faces. */
3415 base_face_id = underlying_face_id (it);
3416 }
3417
3418 new_face_id = face_at_string_position (it->w,
3419 it->string,
3420 IT_STRING_CHARPOS (*it),
3421 bufpos,
3422 it->region_beg_charpos,
3423 it->region_end_charpos,
3424 &next_stop,
3425 base_face_id, 0);
3426
3427 /* Is this a start of a run of characters with box? Caveat:
3428 this can be called for a freshly allocated iterator; face_id
3429 is -1 is this case. We know that the new face will not
3430 change until the next check pos, i.e. if the new face has a
3431 box, all characters up to that position will have a
3432 box. But, as usual, we don't know whether that position
3433 is really the end. */
3434 if (new_face_id != it->face_id)
3435 {
3436 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3437 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3438
3439 /* If new face has a box but old face hasn't, this is the
3440 start of a run of characters with box, i.e. it has a
3441 shadow on the left side. */
3442 it->start_of_box_run_p
3443 = new_face->box && (old_face == NULL || !old_face->box);
3444 it->face_box_p = new_face->box != FACE_NO_BOX;
3445 }
3446 }
3447
3448 it->face_id = new_face_id;
3449 return HANDLED_NORMALLY;
3450 }
3451
3452
3453 /* Return the ID of the face ``underlying'' IT's current position,
3454 which is in a string. If the iterator is associated with a
3455 buffer, return the face at IT's current buffer position.
3456 Otherwise, use the iterator's base_face_id. */
3457
3458 static int
3459 underlying_face_id (struct it *it)
3460 {
3461 int face_id = it->base_face_id, i;
3462
3463 xassert (STRINGP (it->string));
3464
3465 for (i = it->sp - 1; i >= 0; --i)
3466 if (NILP (it->stack[i].string))
3467 face_id = it->stack[i].face_id;
3468
3469 return face_id;
3470 }
3471
3472
3473 /* Compute the face one character before or after the current position
3474 of IT, in the visual order. BEFORE_P non-zero means get the face
3475 in front (to the left in L2R paragraphs, to the right in R2L
3476 paragraphs) of IT's screen position. Value is the ID of the face. */
3477
3478 static int
3479 face_before_or_after_it_pos (struct it *it, int before_p)
3480 {
3481 int face_id, limit;
3482 EMACS_INT next_check_charpos;
3483 struct it it_copy;
3484
3485 xassert (it->s == NULL);
3486
3487 if (STRINGP (it->string))
3488 {
3489 EMACS_INT bufpos, charpos;
3490 int base_face_id;
3491
3492 /* No face change past the end of the string (for the case
3493 we are padding with spaces). No face change before the
3494 string start. */
3495 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3496 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3497 return it->face_id;
3498
3499 if (!it->bidi_p)
3500 {
3501 /* Set charpos to the position before or after IT's current
3502 position, in the logical order, which in the non-bidi
3503 case is the same as the visual order. */
3504 if (before_p)
3505 charpos = IT_STRING_CHARPOS (*it) - 1;
3506 else if (it->what == IT_COMPOSITION)
3507 /* For composition, we must check the character after the
3508 composition. */
3509 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3510 else
3511 charpos = IT_STRING_CHARPOS (*it) + 1;
3512 }
3513 else
3514 {
3515 if (before_p)
3516 {
3517 /* With bidi iteration, the character before the current
3518 in the visual order cannot be found by simple
3519 iteration, because "reverse" reordering is not
3520 supported. Instead, we need to use the move_it_*
3521 family of functions. */
3522 /* Ignore face changes before the first visible
3523 character on this display line. */
3524 if (it->current_x <= it->first_visible_x)
3525 return it->face_id;
3526 it_copy = *it;
3527 /* Implementation note: Since move_it_in_display_line
3528 works in the iterator geometry, and thinks the first
3529 character is always the leftmost, even in R2L lines,
3530 we don't need to distinguish between the R2L and L2R
3531 cases here. */
3532 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3533 it_copy.current_x - 1, MOVE_TO_X);
3534 charpos = IT_STRING_CHARPOS (it_copy);
3535 }
3536 else
3537 {
3538 /* Set charpos to the string position of the character
3539 that comes after IT's current position in the visual
3540 order. */
3541 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3542
3543 it_copy = *it;
3544 while (n--)
3545 bidi_move_to_visually_next (&it_copy.bidi_it);
3546
3547 charpos = it_copy.bidi_it.charpos;
3548 }
3549 }
3550 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3551
3552 if (it->current.overlay_string_index >= 0)
3553 bufpos = IT_CHARPOS (*it);
3554 else
3555 bufpos = 0;
3556
3557 base_face_id = underlying_face_id (it);
3558
3559 /* Get the face for ASCII, or unibyte. */
3560 face_id = face_at_string_position (it->w,
3561 it->string,
3562 charpos,
3563 bufpos,
3564 it->region_beg_charpos,
3565 it->region_end_charpos,
3566 &next_check_charpos,
3567 base_face_id, 0);
3568
3569 /* Correct the face for charsets different from ASCII. Do it
3570 for the multibyte case only. The face returned above is
3571 suitable for unibyte text if IT->string is unibyte. */
3572 if (STRING_MULTIBYTE (it->string))
3573 {
3574 struct text_pos pos1 = string_pos (charpos, it->string);
3575 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3576 int c, len;
3577 struct face *face = FACE_FROM_ID (it->f, face_id);
3578
3579 c = string_char_and_length (p, &len);
3580 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3581 }
3582 }
3583 else
3584 {
3585 struct text_pos pos;
3586
3587 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3588 || (IT_CHARPOS (*it) <= BEGV && before_p))
3589 return it->face_id;
3590
3591 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3592 pos = it->current.pos;
3593
3594 if (!it->bidi_p)
3595 {
3596 if (before_p)
3597 DEC_TEXT_POS (pos, it->multibyte_p);
3598 else
3599 {
3600 if (it->what == IT_COMPOSITION)
3601 {
3602 /* For composition, we must check the position after
3603 the composition. */
3604 pos.charpos += it->cmp_it.nchars;
3605 pos.bytepos += it->len;
3606 }
3607 else
3608 INC_TEXT_POS (pos, it->multibyte_p);
3609 }
3610 }
3611 else
3612 {
3613 if (before_p)
3614 {
3615 /* With bidi iteration, the character before the current
3616 in the visual order cannot be found by simple
3617 iteration, because "reverse" reordering is not
3618 supported. Instead, we need to use the move_it_*
3619 family of functions. */
3620 /* Ignore face changes before the first visible
3621 character on this display line. */
3622 if (it->current_x <= it->first_visible_x)
3623 return it->face_id;
3624 it_copy = *it;
3625 /* Implementation note: Since move_it_in_display_line
3626 works in the iterator geometry, and thinks the first
3627 character is always the leftmost, even in R2L lines,
3628 we don't need to distinguish between the R2L and L2R
3629 cases here. */
3630 move_it_in_display_line (&it_copy, ZV,
3631 it_copy.current_x - 1, MOVE_TO_X);
3632 pos = it_copy.current.pos;
3633 }
3634 else
3635 {
3636 /* Set charpos to the buffer position of the character
3637 that comes after IT's current position in the visual
3638 order. */
3639 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3640
3641 it_copy = *it;
3642 while (n--)
3643 bidi_move_to_visually_next (&it_copy.bidi_it);
3644
3645 SET_TEXT_POS (pos,
3646 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3647 }
3648 }
3649 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3650
3651 /* Determine face for CHARSET_ASCII, or unibyte. */
3652 face_id = face_at_buffer_position (it->w,
3653 CHARPOS (pos),
3654 it->region_beg_charpos,
3655 it->region_end_charpos,
3656 &next_check_charpos,
3657 limit, 0, -1);
3658
3659 /* Correct the face for charsets different from ASCII. Do it
3660 for the multibyte case only. The face returned above is
3661 suitable for unibyte text if current_buffer is unibyte. */
3662 if (it->multibyte_p)
3663 {
3664 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3665 struct face *face = FACE_FROM_ID (it->f, face_id);
3666 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3667 }
3668 }
3669
3670 return face_id;
3671 }
3672
3673
3674 \f
3675 /***********************************************************************
3676 Invisible text
3677 ***********************************************************************/
3678
3679 /* Set up iterator IT from invisible properties at its current
3680 position. Called from handle_stop. */
3681
3682 static enum prop_handled
3683 handle_invisible_prop (struct it *it)
3684 {
3685 enum prop_handled handled = HANDLED_NORMALLY;
3686
3687 if (STRINGP (it->string))
3688 {
3689 Lisp_Object prop, end_charpos, limit, charpos;
3690
3691 /* Get the value of the invisible text property at the
3692 current position. Value will be nil if there is no such
3693 property. */
3694 charpos = make_number (IT_STRING_CHARPOS (*it));
3695 prop = Fget_text_property (charpos, Qinvisible, it->string);
3696
3697 if (!NILP (prop)
3698 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3699 {
3700 EMACS_INT endpos;
3701
3702 handled = HANDLED_RECOMPUTE_PROPS;
3703
3704 /* Get the position at which the next change of the
3705 invisible text property can be found in IT->string.
3706 Value will be nil if the property value is the same for
3707 all the rest of IT->string. */
3708 XSETINT (limit, SCHARS (it->string));
3709 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3710 it->string, limit);
3711
3712 /* Text at current position is invisible. The next
3713 change in the property is at position end_charpos.
3714 Move IT's current position to that position. */
3715 if (INTEGERP (end_charpos)
3716 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3717 {
3718 struct text_pos old;
3719 EMACS_INT oldpos;
3720
3721 old = it->current.string_pos;
3722 oldpos = CHARPOS (old);
3723 if (it->bidi_p)
3724 {
3725 if (it->bidi_it.first_elt
3726 && it->bidi_it.charpos < SCHARS (it->string))
3727 bidi_paragraph_init (it->paragraph_embedding,
3728 &it->bidi_it, 1);
3729 /* Bidi-iterate out of the invisible text. */
3730 do
3731 {
3732 bidi_move_to_visually_next (&it->bidi_it);
3733 }
3734 while (oldpos <= it->bidi_it.charpos
3735 && it->bidi_it.charpos < endpos);
3736
3737 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3738 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3739 if (IT_CHARPOS (*it) >= endpos)
3740 it->prev_stop = endpos;
3741 }
3742 else
3743 {
3744 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3745 compute_string_pos (&it->current.string_pos, old, it->string);
3746 }
3747 }
3748 else
3749 {
3750 /* The rest of the string is invisible. If this is an
3751 overlay string, proceed with the next overlay string
3752 or whatever comes and return a character from there. */
3753 if (it->current.overlay_string_index >= 0)
3754 {
3755 next_overlay_string (it);
3756 /* Don't check for overlay strings when we just
3757 finished processing them. */
3758 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3759 }
3760 else
3761 {
3762 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3763 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3764 }
3765 }
3766 }
3767 }
3768 else
3769 {
3770 int invis_p;
3771 EMACS_INT newpos, next_stop, start_charpos, tem;
3772 Lisp_Object pos, prop, overlay;
3773
3774 /* First of all, is there invisible text at this position? */
3775 tem = start_charpos = IT_CHARPOS (*it);
3776 pos = make_number (tem);
3777 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3778 &overlay);
3779 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3780
3781 /* If we are on invisible text, skip over it. */
3782 if (invis_p && start_charpos < it->end_charpos)
3783 {
3784 /* Record whether we have to display an ellipsis for the
3785 invisible text. */
3786 int display_ellipsis_p = invis_p == 2;
3787
3788 handled = HANDLED_RECOMPUTE_PROPS;
3789
3790 /* Loop skipping over invisible text. The loop is left at
3791 ZV or with IT on the first char being visible again. */
3792 do
3793 {
3794 /* Try to skip some invisible text. Return value is the
3795 position reached which can be equal to where we start
3796 if there is nothing invisible there. This skips both
3797 over invisible text properties and overlays with
3798 invisible property. */
3799 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3800
3801 /* If we skipped nothing at all we weren't at invisible
3802 text in the first place. If everything to the end of
3803 the buffer was skipped, end the loop. */
3804 if (newpos == tem || newpos >= ZV)
3805 invis_p = 0;
3806 else
3807 {
3808 /* We skipped some characters but not necessarily
3809 all there are. Check if we ended up on visible
3810 text. Fget_char_property returns the property of
3811 the char before the given position, i.e. if we
3812 get invis_p = 0, this means that the char at
3813 newpos is visible. */
3814 pos = make_number (newpos);
3815 prop = Fget_char_property (pos, Qinvisible, it->window);
3816 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3817 }
3818
3819 /* If we ended up on invisible text, proceed to
3820 skip starting with next_stop. */
3821 if (invis_p)
3822 tem = next_stop;
3823
3824 /* If there are adjacent invisible texts, don't lose the
3825 second one's ellipsis. */
3826 if (invis_p == 2)
3827 display_ellipsis_p = 1;
3828 }
3829 while (invis_p);
3830
3831 /* The position newpos is now either ZV or on visible text. */
3832 if (it->bidi_p && newpos < ZV)
3833 {
3834 /* With bidi iteration, the region of invisible text
3835 could start and/or end in the middle of a non-base
3836 embedding level. Therefore, we need to skip
3837 invisible text using the bidi iterator, starting at
3838 IT's current position, until we find ourselves
3839 outside the invisible text. Skipping invisible text
3840 _after_ bidi iteration avoids affecting the visual
3841 order of the displayed text when invisible properties
3842 are added or removed. */
3843 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3844 {
3845 /* If we were `reseat'ed to a new paragraph,
3846 determine the paragraph base direction. We need
3847 to do it now because next_element_from_buffer may
3848 not have a chance to do it, if we are going to
3849 skip any text at the beginning, which resets the
3850 FIRST_ELT flag. */
3851 bidi_paragraph_init (it->paragraph_embedding,
3852 &it->bidi_it, 1);
3853 }
3854 do
3855 {
3856 bidi_move_to_visually_next (&it->bidi_it);
3857 }
3858 while (it->stop_charpos <= it->bidi_it.charpos
3859 && it->bidi_it.charpos < newpos);
3860 IT_CHARPOS (*it) = it->bidi_it.charpos;
3861 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3862 /* If we overstepped NEWPOS, record its position in the
3863 iterator, so that we skip invisible text if later the
3864 bidi iteration lands us in the invisible region
3865 again. */
3866 if (IT_CHARPOS (*it) >= newpos)
3867 it->prev_stop = newpos;
3868 }
3869 else
3870 {
3871 IT_CHARPOS (*it) = newpos;
3872 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3873 }
3874
3875 /* If there are before-strings at the start of invisible
3876 text, and the text is invisible because of a text
3877 property, arrange to show before-strings because 20.x did
3878 it that way. (If the text is invisible because of an
3879 overlay property instead of a text property, this is
3880 already handled in the overlay code.) */
3881 if (NILP (overlay)
3882 && get_overlay_strings (it, it->stop_charpos))
3883 {
3884 handled = HANDLED_RECOMPUTE_PROPS;
3885 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3886 }
3887 else if (display_ellipsis_p)
3888 {
3889 /* Make sure that the glyphs of the ellipsis will get
3890 correct `charpos' values. If we would not update
3891 it->position here, the glyphs would belong to the
3892 last visible character _before_ the invisible
3893 text, which confuses `set_cursor_from_row'.
3894
3895 We use the last invisible position instead of the
3896 first because this way the cursor is always drawn on
3897 the first "." of the ellipsis, whenever PT is inside
3898 the invisible text. Otherwise the cursor would be
3899 placed _after_ the ellipsis when the point is after the
3900 first invisible character. */
3901 if (!STRINGP (it->object))
3902 {
3903 it->position.charpos = newpos - 1;
3904 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3905 }
3906 it->ellipsis_p = 1;
3907 /* Let the ellipsis display before
3908 considering any properties of the following char.
3909 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3910 handled = HANDLED_RETURN;
3911 }
3912 }
3913 }
3914
3915 return handled;
3916 }
3917
3918
3919 /* Make iterator IT return `...' next.
3920 Replaces LEN characters from buffer. */
3921
3922 static void
3923 setup_for_ellipsis (struct it *it, int len)
3924 {
3925 /* Use the display table definition for `...'. Invalid glyphs
3926 will be handled by the method returning elements from dpvec. */
3927 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3928 {
3929 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3930 it->dpvec = v->contents;
3931 it->dpend = v->contents + v->header.size;
3932 }
3933 else
3934 {
3935 /* Default `...'. */
3936 it->dpvec = default_invis_vector;
3937 it->dpend = default_invis_vector + 3;
3938 }
3939
3940 it->dpvec_char_len = len;
3941 it->current.dpvec_index = 0;
3942 it->dpvec_face_id = -1;
3943
3944 /* Remember the current face id in case glyphs specify faces.
3945 IT's face is restored in set_iterator_to_next.
3946 saved_face_id was set to preceding char's face in handle_stop. */
3947 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3948 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3949
3950 it->method = GET_FROM_DISPLAY_VECTOR;
3951 it->ellipsis_p = 1;
3952 }
3953
3954
3955 \f
3956 /***********************************************************************
3957 'display' property
3958 ***********************************************************************/
3959
3960 /* Set up iterator IT from `display' property at its current position.
3961 Called from handle_stop.
3962 We return HANDLED_RETURN if some part of the display property
3963 overrides the display of the buffer text itself.
3964 Otherwise we return HANDLED_NORMALLY. */
3965
3966 static enum prop_handled
3967 handle_display_prop (struct it *it)
3968 {
3969 Lisp_Object propval, object, overlay;
3970 struct text_pos *position;
3971 EMACS_INT bufpos;
3972 /* Nonzero if some property replaces the display of the text itself. */
3973 int display_replaced_p = 0;
3974
3975 if (STRINGP (it->string))
3976 {
3977 object = it->string;
3978 position = &it->current.string_pos;
3979 bufpos = CHARPOS (it->current.pos);
3980 }
3981 else
3982 {
3983 XSETWINDOW (object, it->w);
3984 position = &it->current.pos;
3985 bufpos = CHARPOS (*position);
3986 }
3987
3988 /* Reset those iterator values set from display property values. */
3989 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3990 it->space_width = Qnil;
3991 it->font_height = Qnil;
3992 it->voffset = 0;
3993
3994 /* We don't support recursive `display' properties, i.e. string
3995 values that have a string `display' property, that have a string
3996 `display' property etc. */
3997 if (!it->string_from_display_prop_p)
3998 it->area = TEXT_AREA;
3999
4000 propval = get_char_property_and_overlay (make_number (position->charpos),
4001 Qdisplay, object, &overlay);
4002 if (NILP (propval))
4003 return HANDLED_NORMALLY;
4004 /* Now OVERLAY is the overlay that gave us this property, or nil
4005 if it was a text property. */
4006
4007 if (!STRINGP (it->string))
4008 object = it->w->buffer;
4009
4010 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4011 position, bufpos,
4012 FRAME_WINDOW_P (it->f));
4013
4014 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4015 }
4016
4017 /* Subroutine of handle_display_prop. Returns non-zero if the display
4018 specification in SPEC is a replacing specification, i.e. it would
4019 replace the text covered by `display' property with something else,
4020 such as an image or a display string.
4021
4022 See handle_single_display_spec for documentation of arguments.
4023 frame_window_p is non-zero if the window being redisplayed is on a
4024 GUI frame; this argument is used only if IT is NULL, see below.
4025
4026 IT can be NULL, if this is called by the bidi reordering code
4027 through compute_display_string_pos, which see. In that case, this
4028 function only examines SPEC, but does not otherwise "handle" it, in
4029 the sense that it doesn't set up members of IT from the display
4030 spec. */
4031 static int
4032 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4033 Lisp_Object overlay, struct text_pos *position,
4034 EMACS_INT bufpos, int frame_window_p)
4035 {
4036 int replacing_p = 0;
4037
4038 if (CONSP (spec)
4039 /* Simple specerties. */
4040 && !EQ (XCAR (spec), Qimage)
4041 && !EQ (XCAR (spec), Qspace)
4042 && !EQ (XCAR (spec), Qwhen)
4043 && !EQ (XCAR (spec), Qslice)
4044 && !EQ (XCAR (spec), Qspace_width)
4045 && !EQ (XCAR (spec), Qheight)
4046 && !EQ (XCAR (spec), Qraise)
4047 /* Marginal area specifications. */
4048 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4049 && !EQ (XCAR (spec), Qleft_fringe)
4050 && !EQ (XCAR (spec), Qright_fringe)
4051 && !NILP (XCAR (spec)))
4052 {
4053 for (; CONSP (spec); spec = XCDR (spec))
4054 {
4055 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
4056 position, bufpos, replacing_p,
4057 frame_window_p))
4058 {
4059 replacing_p = 1;
4060 /* If some text in a string is replaced, `position' no
4061 longer points to the position of `object'. */
4062 if (!it || STRINGP (object))
4063 break;
4064 }
4065 }
4066 }
4067 else if (VECTORP (spec))
4068 {
4069 int i;
4070 for (i = 0; i < ASIZE (spec); ++i)
4071 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
4072 position, bufpos, replacing_p,
4073 frame_window_p))
4074 {
4075 replacing_p = 1;
4076 /* If some text in a string is replaced, `position' no
4077 longer points to the position of `object'. */
4078 if (!it || STRINGP (object))
4079 break;
4080 }
4081 }
4082 else
4083 {
4084 if (handle_single_display_spec (it, spec, object, overlay,
4085 position, bufpos, 0, frame_window_p))
4086 replacing_p = 1;
4087 }
4088
4089 return replacing_p;
4090 }
4091
4092 /* Value is the position of the end of the `display' property starting
4093 at START_POS in OBJECT. */
4094
4095 static struct text_pos
4096 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4097 {
4098 Lisp_Object end;
4099 struct text_pos end_pos;
4100
4101 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4102 Qdisplay, object, Qnil);
4103 CHARPOS (end_pos) = XFASTINT (end);
4104 if (STRINGP (object))
4105 compute_string_pos (&end_pos, start_pos, it->string);
4106 else
4107 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4108
4109 return end_pos;
4110 }
4111
4112
4113 /* Set up IT from a single `display' property specification SPEC. OBJECT
4114 is the object in which the `display' property was found. *POSITION
4115 is the position in OBJECT at which the `display' property was found.
4116 BUFPOS is the buffer position of OBJECT (different from POSITION if
4117 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4118 previously saw a display specification which already replaced text
4119 display with something else, for example an image; we ignore such
4120 properties after the first one has been processed.
4121
4122 OVERLAY is the overlay this `display' property came from,
4123 or nil if it was a text property.
4124
4125 If SPEC is a `space' or `image' specification, and in some other
4126 cases too, set *POSITION to the position where the `display'
4127 property ends.
4128
4129 If IT is NULL, only examine the property specification in SPEC, but
4130 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4131 is intended to be displayed in a window on a GUI frame.
4132
4133 Value is non-zero if something was found which replaces the display
4134 of buffer or string text. */
4135
4136 static int
4137 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4138 Lisp_Object overlay, struct text_pos *position,
4139 EMACS_INT bufpos, int display_replaced_p,
4140 int frame_window_p)
4141 {
4142 Lisp_Object form;
4143 Lisp_Object location, value;
4144 struct text_pos start_pos = *position;
4145 int valid_p;
4146
4147 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4148 If the result is non-nil, use VALUE instead of SPEC. */
4149 form = Qt;
4150 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4151 {
4152 spec = XCDR (spec);
4153 if (!CONSP (spec))
4154 return 0;
4155 form = XCAR (spec);
4156 spec = XCDR (spec);
4157 }
4158
4159 if (!NILP (form) && !EQ (form, Qt))
4160 {
4161 int count = SPECPDL_INDEX ();
4162 struct gcpro gcpro1;
4163
4164 /* Bind `object' to the object having the `display' property, a
4165 buffer or string. Bind `position' to the position in the
4166 object where the property was found, and `buffer-position'
4167 to the current position in the buffer. */
4168
4169 if (NILP (object))
4170 XSETBUFFER (object, current_buffer);
4171 specbind (Qobject, object);
4172 specbind (Qposition, make_number (CHARPOS (*position)));
4173 specbind (Qbuffer_position, make_number (bufpos));
4174 GCPRO1 (form);
4175 form = safe_eval (form);
4176 UNGCPRO;
4177 unbind_to (count, Qnil);
4178 }
4179
4180 if (NILP (form))
4181 return 0;
4182
4183 /* Handle `(height HEIGHT)' specifications. */
4184 if (CONSP (spec)
4185 && EQ (XCAR (spec), Qheight)
4186 && CONSP (XCDR (spec)))
4187 {
4188 if (it)
4189 {
4190 if (!FRAME_WINDOW_P (it->f))
4191 return 0;
4192
4193 it->font_height = XCAR (XCDR (spec));
4194 if (!NILP (it->font_height))
4195 {
4196 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4197 int new_height = -1;
4198
4199 if (CONSP (it->font_height)
4200 && (EQ (XCAR (it->font_height), Qplus)
4201 || EQ (XCAR (it->font_height), Qminus))
4202 && CONSP (XCDR (it->font_height))
4203 && INTEGERP (XCAR (XCDR (it->font_height))))
4204 {
4205 /* `(+ N)' or `(- N)' where N is an integer. */
4206 int steps = XINT (XCAR (XCDR (it->font_height)));
4207 if (EQ (XCAR (it->font_height), Qplus))
4208 steps = - steps;
4209 it->face_id = smaller_face (it->f, it->face_id, steps);
4210 }
4211 else if (FUNCTIONP (it->font_height))
4212 {
4213 /* Call function with current height as argument.
4214 Value is the new height. */
4215 Lisp_Object height;
4216 height = safe_call1 (it->font_height,
4217 face->lface[LFACE_HEIGHT_INDEX]);
4218 if (NUMBERP (height))
4219 new_height = XFLOATINT (height);
4220 }
4221 else if (NUMBERP (it->font_height))
4222 {
4223 /* Value is a multiple of the canonical char height. */
4224 struct face *f;
4225
4226 f = FACE_FROM_ID (it->f,
4227 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4228 new_height = (XFLOATINT (it->font_height)
4229 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4230 }
4231 else
4232 {
4233 /* Evaluate IT->font_height with `height' bound to the
4234 current specified height to get the new height. */
4235 int count = SPECPDL_INDEX ();
4236
4237 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4238 value = safe_eval (it->font_height);
4239 unbind_to (count, Qnil);
4240
4241 if (NUMBERP (value))
4242 new_height = XFLOATINT (value);
4243 }
4244
4245 if (new_height > 0)
4246 it->face_id = face_with_height (it->f, it->face_id, new_height);
4247 }
4248 }
4249
4250 return 0;
4251 }
4252
4253 /* Handle `(space-width WIDTH)'. */
4254 if (CONSP (spec)
4255 && EQ (XCAR (spec), Qspace_width)
4256 && CONSP (XCDR (spec)))
4257 {
4258 if (it)
4259 {
4260 if (!FRAME_WINDOW_P (it->f))
4261 return 0;
4262
4263 value = XCAR (XCDR (spec));
4264 if (NUMBERP (value) && XFLOATINT (value) > 0)
4265 it->space_width = value;
4266 }
4267
4268 return 0;
4269 }
4270
4271 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4272 if (CONSP (spec)
4273 && EQ (XCAR (spec), Qslice))
4274 {
4275 Lisp_Object tem;
4276
4277 if (it)
4278 {
4279 if (!FRAME_WINDOW_P (it->f))
4280 return 0;
4281
4282 if (tem = XCDR (spec), CONSP (tem))
4283 {
4284 it->slice.x = XCAR (tem);
4285 if (tem = XCDR (tem), CONSP (tem))
4286 {
4287 it->slice.y = XCAR (tem);
4288 if (tem = XCDR (tem), CONSP (tem))
4289 {
4290 it->slice.width = XCAR (tem);
4291 if (tem = XCDR (tem), CONSP (tem))
4292 it->slice.height = XCAR (tem);
4293 }
4294 }
4295 }
4296 }
4297
4298 return 0;
4299 }
4300
4301 /* Handle `(raise FACTOR)'. */
4302 if (CONSP (spec)
4303 && EQ (XCAR (spec), Qraise)
4304 && CONSP (XCDR (spec)))
4305 {
4306 if (it)
4307 {
4308 if (!FRAME_WINDOW_P (it->f))
4309 return 0;
4310
4311 #ifdef HAVE_WINDOW_SYSTEM
4312 value = XCAR (XCDR (spec));
4313 if (NUMBERP (value))
4314 {
4315 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4316 it->voffset = - (XFLOATINT (value)
4317 * (FONT_HEIGHT (face->font)));
4318 }
4319 #endif /* HAVE_WINDOW_SYSTEM */
4320 }
4321
4322 return 0;
4323 }
4324
4325 /* Don't handle the other kinds of display specifications
4326 inside a string that we got from a `display' property. */
4327 if (it && it->string_from_display_prop_p)
4328 return 0;
4329
4330 /* Characters having this form of property are not displayed, so
4331 we have to find the end of the property. */
4332 if (it)
4333 {
4334 start_pos = *position;
4335 *position = display_prop_end (it, object, start_pos);
4336 }
4337 value = Qnil;
4338
4339 /* Stop the scan at that end position--we assume that all
4340 text properties change there. */
4341 if (it)
4342 it->stop_charpos = position->charpos;
4343
4344 /* Handle `(left-fringe BITMAP [FACE])'
4345 and `(right-fringe BITMAP [FACE])'. */
4346 if (CONSP (spec)
4347 && (EQ (XCAR (spec), Qleft_fringe)
4348 || EQ (XCAR (spec), Qright_fringe))
4349 && CONSP (XCDR (spec)))
4350 {
4351 int fringe_bitmap;
4352
4353 if (it)
4354 {
4355 if (!FRAME_WINDOW_P (it->f))
4356 /* If we return here, POSITION has been advanced
4357 across the text with this property. */
4358 return 0;
4359 }
4360 else if (!frame_window_p)
4361 return 0;
4362
4363 #ifdef HAVE_WINDOW_SYSTEM
4364 value = XCAR (XCDR (spec));
4365 if (!SYMBOLP (value)
4366 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4367 /* If we return here, POSITION has been advanced
4368 across the text with this property. */
4369 return 0;
4370
4371 if (it)
4372 {
4373 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4374
4375 if (CONSP (XCDR (XCDR (spec))))
4376 {
4377 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4378 int face_id2 = lookup_derived_face (it->f, face_name,
4379 FRINGE_FACE_ID, 0);
4380 if (face_id2 >= 0)
4381 face_id = face_id2;
4382 }
4383
4384 /* Save current settings of IT so that we can restore them
4385 when we are finished with the glyph property value. */
4386 push_it (it, position);
4387
4388 it->area = TEXT_AREA;
4389 it->what = IT_IMAGE;
4390 it->image_id = -1; /* no image */
4391 it->position = start_pos;
4392 it->object = NILP (object) ? it->w->buffer : object;
4393 it->method = GET_FROM_IMAGE;
4394 it->from_overlay = Qnil;
4395 it->face_id = face_id;
4396
4397 /* Say that we haven't consumed the characters with
4398 `display' property yet. The call to pop_it in
4399 set_iterator_to_next will clean this up. */
4400 *position = start_pos;
4401
4402 if (EQ (XCAR (spec), Qleft_fringe))
4403 {
4404 it->left_user_fringe_bitmap = fringe_bitmap;
4405 it->left_user_fringe_face_id = face_id;
4406 }
4407 else
4408 {
4409 it->right_user_fringe_bitmap = fringe_bitmap;
4410 it->right_user_fringe_face_id = face_id;
4411 }
4412 }
4413 #endif /* HAVE_WINDOW_SYSTEM */
4414 return 1;
4415 }
4416
4417 /* Prepare to handle `((margin left-margin) ...)',
4418 `((margin right-margin) ...)' and `((margin nil) ...)'
4419 prefixes for display specifications. */
4420 location = Qunbound;
4421 if (CONSP (spec) && CONSP (XCAR (spec)))
4422 {
4423 Lisp_Object tem;
4424
4425 value = XCDR (spec);
4426 if (CONSP (value))
4427 value = XCAR (value);
4428
4429 tem = XCAR (spec);
4430 if (EQ (XCAR (tem), Qmargin)
4431 && (tem = XCDR (tem),
4432 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4433 (NILP (tem)
4434 || EQ (tem, Qleft_margin)
4435 || EQ (tem, Qright_margin))))
4436 location = tem;
4437 }
4438
4439 if (EQ (location, Qunbound))
4440 {
4441 location = Qnil;
4442 value = spec;
4443 }
4444
4445 /* After this point, VALUE is the property after any
4446 margin prefix has been stripped. It must be a string,
4447 an image specification, or `(space ...)'.
4448
4449 LOCATION specifies where to display: `left-margin',
4450 `right-margin' or nil. */
4451
4452 valid_p = (STRINGP (value)
4453 #ifdef HAVE_WINDOW_SYSTEM
4454 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4455 && valid_image_p (value))
4456 #endif /* not HAVE_WINDOW_SYSTEM */
4457 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4458
4459 if (valid_p && !display_replaced_p)
4460 {
4461 if (!it)
4462 return 1;
4463
4464 /* Save current settings of IT so that we can restore them
4465 when we are finished with the glyph property value. */
4466 push_it (it, position);
4467 it->from_overlay = overlay;
4468
4469 if (NILP (location))
4470 it->area = TEXT_AREA;
4471 else if (EQ (location, Qleft_margin))
4472 it->area = LEFT_MARGIN_AREA;
4473 else
4474 it->area = RIGHT_MARGIN_AREA;
4475
4476 if (STRINGP (value))
4477 {
4478 it->string = value;
4479 it->multibyte_p = STRING_MULTIBYTE (it->string);
4480 it->current.overlay_string_index = -1;
4481 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4482 it->end_charpos = it->string_nchars = SCHARS (it->string);
4483 it->method = GET_FROM_STRING;
4484 it->stop_charpos = 0;
4485 it->prev_stop = 0;
4486 it->base_level_stop = 0;
4487 it->string_from_display_prop_p = 1;
4488 /* Say that we haven't consumed the characters with
4489 `display' property yet. The call to pop_it in
4490 set_iterator_to_next will clean this up. */
4491 if (BUFFERP (object))
4492 *position = start_pos;
4493
4494 /* Force paragraph direction to be that of the parent
4495 object. */
4496 it->paragraph_embedding =
4497 (it->bidi_p ? it->bidi_it.paragraph_dir : L2R);
4498
4499 /* Do we need to reorder this display string? */
4500 if (it->multibyte_p)
4501 {
4502 if (BUFFERP (object))
4503 it->bidi_p =
4504 !NILP (BVAR (XBUFFER (object), bidi_display_reordering));
4505 else
4506 it->bidi_p =
4507 !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
4508 }
4509 else
4510 it->bidi_p = 0;
4511
4512 /* Set up the bidi iterator for this display string. */
4513 if (it->bidi_p)
4514 {
4515 it->bidi_it.string.lstring = it->string;
4516 it->bidi_it.string.s = NULL;
4517 it->bidi_it.string.schars = it->end_charpos;
4518 it->bidi_it.string.bufpos = bufpos;
4519 it->bidi_it.string.from_disp_str = 1;
4520 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4521 }
4522 }
4523 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4524 {
4525 it->method = GET_FROM_STRETCH;
4526 it->object = value;
4527 *position = it->position = start_pos;
4528 }
4529 #ifdef HAVE_WINDOW_SYSTEM
4530 else
4531 {
4532 it->what = IT_IMAGE;
4533 it->image_id = lookup_image (it->f, value);
4534 it->position = start_pos;
4535 it->object = NILP (object) ? it->w->buffer : object;
4536 it->method = GET_FROM_IMAGE;
4537
4538 /* Say that we haven't consumed the characters with
4539 `display' property yet. The call to pop_it in
4540 set_iterator_to_next will clean this up. */
4541 *position = start_pos;
4542 }
4543 #endif /* HAVE_WINDOW_SYSTEM */
4544
4545 return 1;
4546 }
4547
4548 /* Invalid property or property not supported. Restore
4549 POSITION to what it was before. */
4550 *position = start_pos;
4551 return 0;
4552 }
4553
4554 /* Check if PROP is a display property value whose text should be
4555 treated as intangible. OVERLAY is the overlay from which PROP
4556 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4557 specify the buffer position covered by PROP. */
4558
4559 int
4560 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4561 EMACS_INT charpos, EMACS_INT bytepos)
4562 {
4563 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4564 struct text_pos position;
4565
4566 SET_TEXT_POS (position, charpos, bytepos);
4567 return handle_display_spec (NULL, prop, Qnil, overlay,
4568 &position, charpos, frame_window_p);
4569 }
4570
4571
4572 /* Return 1 if PROP is a display sub-property value containing STRING.
4573
4574 Implementation note: this and the following function are really
4575 special cases of handle_display_spec and
4576 handle_single_display_spec, and should ideally use the same code.
4577 Until they do, these two pairs must be consistent and must be
4578 modified in sync. */
4579
4580 static int
4581 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4582 {
4583 if (EQ (string, prop))
4584 return 1;
4585
4586 /* Skip over `when FORM'. */
4587 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4588 {
4589 prop = XCDR (prop);
4590 if (!CONSP (prop))
4591 return 0;
4592 /* Actually, the condition following `when' should be eval'ed,
4593 like handle_single_display_spec does, and we should return
4594 zero if it evaluates to nil. However, this function is
4595 called only when the buffer was already displayed and some
4596 glyph in the glyph matrix was found to come from a display
4597 string. Therefore, the condition was already evaluated, and
4598 the result was non-nil, otherwise the display string wouldn't
4599 have been displayed and we would have never been called for
4600 this property. Thus, we can skip the evaluation and assume
4601 its result is non-nil. */
4602 prop = XCDR (prop);
4603 }
4604
4605 if (CONSP (prop))
4606 /* Skip over `margin LOCATION'. */
4607 if (EQ (XCAR (prop), Qmargin))
4608 {
4609 prop = XCDR (prop);
4610 if (!CONSP (prop))
4611 return 0;
4612
4613 prop = XCDR (prop);
4614 if (!CONSP (prop))
4615 return 0;
4616 }
4617
4618 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4619 }
4620
4621
4622 /* Return 1 if STRING appears in the `display' property PROP. */
4623
4624 static int
4625 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4626 {
4627 if (CONSP (prop)
4628 && !EQ (XCAR (prop), Qwhen)
4629 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4630 {
4631 /* A list of sub-properties. */
4632 while (CONSP (prop))
4633 {
4634 if (single_display_spec_string_p (XCAR (prop), string))
4635 return 1;
4636 prop = XCDR (prop);
4637 }
4638 }
4639 else if (VECTORP (prop))
4640 {
4641 /* A vector of sub-properties. */
4642 int i;
4643 for (i = 0; i < ASIZE (prop); ++i)
4644 if (single_display_spec_string_p (AREF (prop, i), string))
4645 return 1;
4646 }
4647 else
4648 return single_display_spec_string_p (prop, string);
4649
4650 return 0;
4651 }
4652
4653 /* Look for STRING in overlays and text properties in the current
4654 buffer, between character positions FROM and TO (excluding TO).
4655 BACK_P non-zero means look back (in this case, TO is supposed to be
4656 less than FROM).
4657 Value is the first character position where STRING was found, or
4658 zero if it wasn't found before hitting TO.
4659
4660 This function may only use code that doesn't eval because it is
4661 called asynchronously from note_mouse_highlight. */
4662
4663 static EMACS_INT
4664 string_buffer_position_lim (Lisp_Object string,
4665 EMACS_INT from, EMACS_INT to, int back_p)
4666 {
4667 Lisp_Object limit, prop, pos;
4668 int found = 0;
4669
4670 pos = make_number (from);
4671
4672 if (!back_p) /* looking forward */
4673 {
4674 limit = make_number (min (to, ZV));
4675 while (!found && !EQ (pos, limit))
4676 {
4677 prop = Fget_char_property (pos, Qdisplay, Qnil);
4678 if (!NILP (prop) && display_prop_string_p (prop, string))
4679 found = 1;
4680 else
4681 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4682 limit);
4683 }
4684 }
4685 else /* looking back */
4686 {
4687 limit = make_number (max (to, BEGV));
4688 while (!found && !EQ (pos, limit))
4689 {
4690 prop = Fget_char_property (pos, Qdisplay, Qnil);
4691 if (!NILP (prop) && display_prop_string_p (prop, string))
4692 found = 1;
4693 else
4694 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4695 limit);
4696 }
4697 }
4698
4699 return found ? XINT (pos) : 0;
4700 }
4701
4702 /* Determine which buffer position in current buffer STRING comes from.
4703 AROUND_CHARPOS is an approximate position where it could come from.
4704 Value is the buffer position or 0 if it couldn't be determined.
4705
4706 This function is necessary because we don't record buffer positions
4707 in glyphs generated from strings (to keep struct glyph small).
4708 This function may only use code that doesn't eval because it is
4709 called asynchronously from note_mouse_highlight. */
4710
4711 static EMACS_INT
4712 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4713 {
4714 const int MAX_DISTANCE = 1000;
4715 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4716 around_charpos + MAX_DISTANCE,
4717 0);
4718
4719 if (!found)
4720 found = string_buffer_position_lim (string, around_charpos,
4721 around_charpos - MAX_DISTANCE, 1);
4722 return found;
4723 }
4724
4725
4726 \f
4727 /***********************************************************************
4728 `composition' property
4729 ***********************************************************************/
4730
4731 /* Set up iterator IT from `composition' property at its current
4732 position. Called from handle_stop. */
4733
4734 static enum prop_handled
4735 handle_composition_prop (struct it *it)
4736 {
4737 Lisp_Object prop, string;
4738 EMACS_INT pos, pos_byte, start, end;
4739
4740 if (STRINGP (it->string))
4741 {
4742 unsigned char *s;
4743
4744 pos = IT_STRING_CHARPOS (*it);
4745 pos_byte = IT_STRING_BYTEPOS (*it);
4746 string = it->string;
4747 s = SDATA (string) + pos_byte;
4748 it->c = STRING_CHAR (s);
4749 }
4750 else
4751 {
4752 pos = IT_CHARPOS (*it);
4753 pos_byte = IT_BYTEPOS (*it);
4754 string = Qnil;
4755 it->c = FETCH_CHAR (pos_byte);
4756 }
4757
4758 /* If there's a valid composition and point is not inside of the
4759 composition (in the case that the composition is from the current
4760 buffer), draw a glyph composed from the composition components. */
4761 if (find_composition (pos, -1, &start, &end, &prop, string)
4762 && COMPOSITION_VALID_P (start, end, prop)
4763 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4764 {
4765 if (start != pos)
4766 {
4767 if (STRINGP (it->string))
4768 pos_byte = string_char_to_byte (it->string, start);
4769 else
4770 pos_byte = CHAR_TO_BYTE (start);
4771 }
4772 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4773 prop, string);
4774
4775 if (it->cmp_it.id >= 0)
4776 {
4777 it->cmp_it.ch = -1;
4778 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4779 it->cmp_it.nglyphs = -1;
4780 }
4781 }
4782
4783 return HANDLED_NORMALLY;
4784 }
4785
4786
4787 \f
4788 /***********************************************************************
4789 Overlay strings
4790 ***********************************************************************/
4791
4792 /* The following structure is used to record overlay strings for
4793 later sorting in load_overlay_strings. */
4794
4795 struct overlay_entry
4796 {
4797 Lisp_Object overlay;
4798 Lisp_Object string;
4799 int priority;
4800 int after_string_p;
4801 };
4802
4803
4804 /* Set up iterator IT from overlay strings at its current position.
4805 Called from handle_stop. */
4806
4807 static enum prop_handled
4808 handle_overlay_change (struct it *it)
4809 {
4810 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4811 return HANDLED_RECOMPUTE_PROPS;
4812 else
4813 return HANDLED_NORMALLY;
4814 }
4815
4816
4817 /* Set up the next overlay string for delivery by IT, if there is an
4818 overlay string to deliver. Called by set_iterator_to_next when the
4819 end of the current overlay string is reached. If there are more
4820 overlay strings to display, IT->string and
4821 IT->current.overlay_string_index are set appropriately here.
4822 Otherwise IT->string is set to nil. */
4823
4824 static void
4825 next_overlay_string (struct it *it)
4826 {
4827 ++it->current.overlay_string_index;
4828 if (it->current.overlay_string_index == it->n_overlay_strings)
4829 {
4830 /* No more overlay strings. Restore IT's settings to what
4831 they were before overlay strings were processed, and
4832 continue to deliver from current_buffer. */
4833
4834 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4835 pop_it (it);
4836 xassert (it->sp > 0
4837 || (NILP (it->string)
4838 && it->method == GET_FROM_BUFFER
4839 && it->stop_charpos >= BEGV
4840 && it->stop_charpos <= it->end_charpos));
4841 it->current.overlay_string_index = -1;
4842 it->n_overlay_strings = 0;
4843 it->overlay_strings_charpos = -1;
4844
4845 /* If we're at the end of the buffer, record that we have
4846 processed the overlay strings there already, so that
4847 next_element_from_buffer doesn't try it again. */
4848 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4849 it->overlay_strings_at_end_processed_p = 1;
4850 }
4851 else
4852 {
4853 /* There are more overlay strings to process. If
4854 IT->current.overlay_string_index has advanced to a position
4855 where we must load IT->overlay_strings with more strings, do
4856 it. We must load at the IT->overlay_strings_charpos where
4857 IT->n_overlay_strings was originally computed; when invisible
4858 text is present, this might not be IT_CHARPOS (Bug#7016). */
4859 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4860
4861 if (it->current.overlay_string_index && i == 0)
4862 load_overlay_strings (it, it->overlay_strings_charpos);
4863
4864 /* Initialize IT to deliver display elements from the overlay
4865 string. */
4866 it->string = it->overlay_strings[i];
4867 it->multibyte_p = STRING_MULTIBYTE (it->string);
4868 SET_TEXT_POS (it->current.string_pos, 0, 0);
4869 it->method = GET_FROM_STRING;
4870 it->stop_charpos = 0;
4871 if (it->cmp_it.stop_pos >= 0)
4872 it->cmp_it.stop_pos = 0;
4873 it->prev_stop = 0;
4874 it->base_level_stop = 0;
4875
4876 /* Do we need to reorder this overlay string? */
4877 it->bidi_p =
4878 it->multibyte_p
4879 && !NILP (BVAR (current_buffer, bidi_display_reordering));
4880
4881 /* Set up the bidi iterator for this overlay string. */
4882 if (it->bidi_p)
4883 {
4884 it->bidi_it.string.lstring = it->string;
4885 it->bidi_it.string.s = NULL;
4886 it->bidi_it.string.schars = SCHARS (it->string);
4887 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4888 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4889 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4890 }
4891 }
4892
4893 CHECK_IT (it);
4894 }
4895
4896
4897 /* Compare two overlay_entry structures E1 and E2. Used as a
4898 comparison function for qsort in load_overlay_strings. Overlay
4899 strings for the same position are sorted so that
4900
4901 1. All after-strings come in front of before-strings, except
4902 when they come from the same overlay.
4903
4904 2. Within after-strings, strings are sorted so that overlay strings
4905 from overlays with higher priorities come first.
4906
4907 2. Within before-strings, strings are sorted so that overlay
4908 strings from overlays with higher priorities come last.
4909
4910 Value is analogous to strcmp. */
4911
4912
4913 static int
4914 compare_overlay_entries (const void *e1, const void *e2)
4915 {
4916 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4917 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4918 int result;
4919
4920 if (entry1->after_string_p != entry2->after_string_p)
4921 {
4922 /* Let after-strings appear in front of before-strings if
4923 they come from different overlays. */
4924 if (EQ (entry1->overlay, entry2->overlay))
4925 result = entry1->after_string_p ? 1 : -1;
4926 else
4927 result = entry1->after_string_p ? -1 : 1;
4928 }
4929 else if (entry1->after_string_p)
4930 /* After-strings sorted in order of decreasing priority. */
4931 result = entry2->priority - entry1->priority;
4932 else
4933 /* Before-strings sorted in order of increasing priority. */
4934 result = entry1->priority - entry2->priority;
4935
4936 return result;
4937 }
4938
4939
4940 /* Load the vector IT->overlay_strings with overlay strings from IT's
4941 current buffer position, or from CHARPOS if that is > 0. Set
4942 IT->n_overlays to the total number of overlay strings found.
4943
4944 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4945 a time. On entry into load_overlay_strings,
4946 IT->current.overlay_string_index gives the number of overlay
4947 strings that have already been loaded by previous calls to this
4948 function.
4949
4950 IT->add_overlay_start contains an additional overlay start
4951 position to consider for taking overlay strings from, if non-zero.
4952 This position comes into play when the overlay has an `invisible'
4953 property, and both before and after-strings. When we've skipped to
4954 the end of the overlay, because of its `invisible' property, we
4955 nevertheless want its before-string to appear.
4956 IT->add_overlay_start will contain the overlay start position
4957 in this case.
4958
4959 Overlay strings are sorted so that after-string strings come in
4960 front of before-string strings. Within before and after-strings,
4961 strings are sorted by overlay priority. See also function
4962 compare_overlay_entries. */
4963
4964 static void
4965 load_overlay_strings (struct it *it, EMACS_INT charpos)
4966 {
4967 Lisp_Object overlay, window, str, invisible;
4968 struct Lisp_Overlay *ov;
4969 EMACS_INT start, end;
4970 int size = 20;
4971 int n = 0, i, j, invis_p;
4972 struct overlay_entry *entries
4973 = (struct overlay_entry *) alloca (size * sizeof *entries);
4974
4975 if (charpos <= 0)
4976 charpos = IT_CHARPOS (*it);
4977
4978 /* Append the overlay string STRING of overlay OVERLAY to vector
4979 `entries' which has size `size' and currently contains `n'
4980 elements. AFTER_P non-zero means STRING is an after-string of
4981 OVERLAY. */
4982 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4983 do \
4984 { \
4985 Lisp_Object priority; \
4986 \
4987 if (n == size) \
4988 { \
4989 int new_size = 2 * size; \
4990 struct overlay_entry *old = entries; \
4991 entries = \
4992 (struct overlay_entry *) alloca (new_size \
4993 * sizeof *entries); \
4994 memcpy (entries, old, size * sizeof *entries); \
4995 size = new_size; \
4996 } \
4997 \
4998 entries[n].string = (STRING); \
4999 entries[n].overlay = (OVERLAY); \
5000 priority = Foverlay_get ((OVERLAY), Qpriority); \
5001 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5002 entries[n].after_string_p = (AFTER_P); \
5003 ++n; \
5004 } \
5005 while (0)
5006
5007 /* Process overlay before the overlay center. */
5008 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5009 {
5010 XSETMISC (overlay, ov);
5011 xassert (OVERLAYP (overlay));
5012 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5013 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5014
5015 if (end < charpos)
5016 break;
5017
5018 /* Skip this overlay if it doesn't start or end at IT's current
5019 position. */
5020 if (end != charpos && start != charpos)
5021 continue;
5022
5023 /* Skip this overlay if it doesn't apply to IT->w. */
5024 window = Foverlay_get (overlay, Qwindow);
5025 if (WINDOWP (window) && XWINDOW (window) != it->w)
5026 continue;
5027
5028 /* If the text ``under'' the overlay is invisible, both before-
5029 and after-strings from this overlay are visible; start and
5030 end position are indistinguishable. */
5031 invisible = Foverlay_get (overlay, Qinvisible);
5032 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5033
5034 /* If overlay has a non-empty before-string, record it. */
5035 if ((start == charpos || (end == charpos && invis_p))
5036 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5037 && SCHARS (str))
5038 RECORD_OVERLAY_STRING (overlay, str, 0);
5039
5040 /* If overlay has a non-empty after-string, record it. */
5041 if ((end == charpos || (start == charpos && invis_p))
5042 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5043 && SCHARS (str))
5044 RECORD_OVERLAY_STRING (overlay, str, 1);
5045 }
5046
5047 /* Process overlays after the overlay center. */
5048 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5049 {
5050 XSETMISC (overlay, ov);
5051 xassert (OVERLAYP (overlay));
5052 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5053 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5054
5055 if (start > charpos)
5056 break;
5057
5058 /* Skip this overlay if it doesn't start or end at IT's current
5059 position. */
5060 if (end != charpos && start != charpos)
5061 continue;
5062
5063 /* Skip this overlay if it doesn't apply to IT->w. */
5064 window = Foverlay_get (overlay, Qwindow);
5065 if (WINDOWP (window) && XWINDOW (window) != it->w)
5066 continue;
5067
5068 /* If the text ``under'' the overlay is invisible, it has a zero
5069 dimension, and both before- and after-strings apply. */
5070 invisible = Foverlay_get (overlay, Qinvisible);
5071 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5072
5073 /* If overlay has a non-empty before-string, record it. */
5074 if ((start == charpos || (end == charpos && invis_p))
5075 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5076 && SCHARS (str))
5077 RECORD_OVERLAY_STRING (overlay, str, 0);
5078
5079 /* If overlay has a non-empty after-string, record it. */
5080 if ((end == charpos || (start == charpos && invis_p))
5081 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5082 && SCHARS (str))
5083 RECORD_OVERLAY_STRING (overlay, str, 1);
5084 }
5085
5086 #undef RECORD_OVERLAY_STRING
5087
5088 /* Sort entries. */
5089 if (n > 1)
5090 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5091
5092 /* Record number of overlay strings, and where we computed it. */
5093 it->n_overlay_strings = n;
5094 it->overlay_strings_charpos = charpos;
5095
5096 /* IT->current.overlay_string_index is the number of overlay strings
5097 that have already been consumed by IT. Copy some of the
5098 remaining overlay strings to IT->overlay_strings. */
5099 i = 0;
5100 j = it->current.overlay_string_index;
5101 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5102 {
5103 it->overlay_strings[i] = entries[j].string;
5104 it->string_overlays[i++] = entries[j++].overlay;
5105 }
5106
5107 CHECK_IT (it);
5108 }
5109
5110
5111 /* Get the first chunk of overlay strings at IT's current buffer
5112 position, or at CHARPOS if that is > 0. Value is non-zero if at
5113 least one overlay string was found. */
5114
5115 static int
5116 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5117 {
5118 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5119 process. This fills IT->overlay_strings with strings, and sets
5120 IT->n_overlay_strings to the total number of strings to process.
5121 IT->pos.overlay_string_index has to be set temporarily to zero
5122 because load_overlay_strings needs this; it must be set to -1
5123 when no overlay strings are found because a zero value would
5124 indicate a position in the first overlay string. */
5125 it->current.overlay_string_index = 0;
5126 load_overlay_strings (it, charpos);
5127
5128 /* If we found overlay strings, set up IT to deliver display
5129 elements from the first one. Otherwise set up IT to deliver
5130 from current_buffer. */
5131 if (it->n_overlay_strings)
5132 {
5133 /* Make sure we know settings in current_buffer, so that we can
5134 restore meaningful values when we're done with the overlay
5135 strings. */
5136 if (compute_stop_p)
5137 compute_stop_pos (it);
5138 xassert (it->face_id >= 0);
5139
5140 /* Save IT's settings. They are restored after all overlay
5141 strings have been processed. */
5142 xassert (!compute_stop_p || it->sp == 0);
5143
5144 /* When called from handle_stop, there might be an empty display
5145 string loaded. In that case, don't bother saving it. */
5146 if (!STRINGP (it->string) || SCHARS (it->string))
5147 push_it (it, NULL);
5148
5149 /* Set up IT to deliver display elements from the first overlay
5150 string. */
5151 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5152 it->string = it->overlay_strings[0];
5153 it->from_overlay = Qnil;
5154 it->stop_charpos = 0;
5155 xassert (STRINGP (it->string));
5156 it->end_charpos = SCHARS (it->string);
5157 it->prev_stop = 0;
5158 it->base_level_stop = 0;
5159 it->multibyte_p = STRING_MULTIBYTE (it->string);
5160 it->method = GET_FROM_STRING;
5161
5162 /* Do we need to reorder this overlay string? */
5163 it->bidi_p =
5164 it->multibyte_p
5165 && !NILP (BVAR (current_buffer, bidi_display_reordering));
5166
5167 /* Force paragraph direction to be that of the parent
5168 buffer. */
5169 it->paragraph_embedding = (it->bidi_p ? it->bidi_it.paragraph_dir : L2R);
5170
5171 /* Set up the bidi iterator for this overlay string. */
5172 if (it->bidi_p)
5173 {
5174 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5175
5176 it->bidi_it.string.lstring = it->string;
5177 it->bidi_it.string.s = NULL;
5178 it->bidi_it.string.schars = SCHARS (it->string);
5179 it->bidi_it.string.bufpos = pos;
5180 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5181 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5182 }
5183 return 1;
5184 }
5185
5186 it->current.overlay_string_index = -1;
5187 return 0;
5188 }
5189
5190 static int
5191 get_overlay_strings (struct it *it, EMACS_INT charpos)
5192 {
5193 it->string = Qnil;
5194 it->method = GET_FROM_BUFFER;
5195
5196 (void) get_overlay_strings_1 (it, charpos, 1);
5197
5198 CHECK_IT (it);
5199
5200 /* Value is non-zero if we found at least one overlay string. */
5201 return STRINGP (it->string);
5202 }
5203
5204
5205 \f
5206 /***********************************************************************
5207 Saving and restoring state
5208 ***********************************************************************/
5209
5210 /* Save current settings of IT on IT->stack. Called, for example,
5211 before setting up IT for an overlay string, to be able to restore
5212 IT's settings to what they were after the overlay string has been
5213 processed. If POSITION is non-NULL, it is the position to save on
5214 the stack instead of IT->position. */
5215
5216 static void
5217 push_it (struct it *it, struct text_pos *position)
5218 {
5219 struct iterator_stack_entry *p;
5220
5221 xassert (it->sp < IT_STACK_SIZE);
5222 p = it->stack + it->sp;
5223
5224 p->stop_charpos = it->stop_charpos;
5225 p->prev_stop = it->prev_stop;
5226 p->base_level_stop = it->base_level_stop;
5227 p->cmp_it = it->cmp_it;
5228 xassert (it->face_id >= 0);
5229 p->face_id = it->face_id;
5230 p->string = it->string;
5231 p->method = it->method;
5232 p->from_overlay = it->from_overlay;
5233 switch (p->method)
5234 {
5235 case GET_FROM_IMAGE:
5236 p->u.image.object = it->object;
5237 p->u.image.image_id = it->image_id;
5238 p->u.image.slice = it->slice;
5239 break;
5240 case GET_FROM_STRETCH:
5241 p->u.stretch.object = it->object;
5242 break;
5243 }
5244 p->position = position ? *position : it->position;
5245 p->current = it->current;
5246 p->end_charpos = it->end_charpos;
5247 p->string_nchars = it->string_nchars;
5248 p->area = it->area;
5249 p->multibyte_p = it->multibyte_p;
5250 p->avoid_cursor_p = it->avoid_cursor_p;
5251 p->space_width = it->space_width;
5252 p->font_height = it->font_height;
5253 p->voffset = it->voffset;
5254 p->string_from_display_prop_p = it->string_from_display_prop_p;
5255 p->display_ellipsis_p = 0;
5256 p->line_wrap = it->line_wrap;
5257 p->bidi_p = it->bidi_p;
5258 p->paragraph_embedding = it->paragraph_embedding;
5259 ++it->sp;
5260
5261 /* Save the state of the bidi iterator as well. */
5262 if (it->bidi_p)
5263 bidi_push_it (&it->bidi_it);
5264 }
5265
5266 static void
5267 iterate_out_of_display_property (struct it *it)
5268 {
5269 int buffer_p = BUFFERP (it->object);
5270 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5271 EMACS_INT bob = (buffer_p ? BEGV : 0);
5272
5273 /* Maybe initialize paragraph direction. If we are at the beginning
5274 of a new paragraph, next_element_from_buffer may not have a
5275 chance to do that. */
5276 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5277 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5278 /* prev_stop can be zero, so check against BEGV as well. */
5279 while (it->bidi_it.charpos >= bob
5280 && it->prev_stop <= it->bidi_it.charpos
5281 && it->bidi_it.charpos < CHARPOS (it->position))
5282 bidi_move_to_visually_next (&it->bidi_it);
5283 /* Record the stop_pos we just crossed, for when we cross it
5284 back, maybe. */
5285 if (it->bidi_it.charpos > CHARPOS (it->position))
5286 it->prev_stop = CHARPOS (it->position);
5287 /* If we ended up not where pop_it put us, resync IT's
5288 positional members with the bidi iterator. */
5289 if (it->bidi_it.charpos != CHARPOS (it->position))
5290 {
5291 SET_TEXT_POS (it->position,
5292 it->bidi_it.charpos, it->bidi_it.bytepos);
5293 if (buffer_p)
5294 it->current.pos = it->position;
5295 else
5296 it->current.string_pos = it->position;
5297 }
5298 }
5299
5300 /* Restore IT's settings from IT->stack. Called, for example, when no
5301 more overlay strings must be processed, and we return to delivering
5302 display elements from a buffer, or when the end of a string from a
5303 `display' property is reached and we return to delivering display
5304 elements from an overlay string, or from a buffer. */
5305
5306 static void
5307 pop_it (struct it *it)
5308 {
5309 struct iterator_stack_entry *p;
5310
5311 xassert (it->sp > 0);
5312 --it->sp;
5313 p = it->stack + it->sp;
5314 it->stop_charpos = p->stop_charpos;
5315 it->prev_stop = p->prev_stop;
5316 it->base_level_stop = p->base_level_stop;
5317 it->cmp_it = p->cmp_it;
5318 it->face_id = p->face_id;
5319 it->current = p->current;
5320 it->position = p->position;
5321 it->string = p->string;
5322 it->from_overlay = p->from_overlay;
5323 if (NILP (it->string))
5324 SET_TEXT_POS (it->current.string_pos, -1, -1);
5325 it->method = p->method;
5326 switch (it->method)
5327 {
5328 case GET_FROM_IMAGE:
5329 it->image_id = p->u.image.image_id;
5330 it->object = p->u.image.object;
5331 it->slice = p->u.image.slice;
5332 break;
5333 case GET_FROM_STRETCH:
5334 it->object = p->u.comp.object;
5335 break;
5336 case GET_FROM_BUFFER:
5337 it->object = it->w->buffer;
5338 break;
5339 case GET_FROM_STRING:
5340 it->object = it->string;
5341 break;
5342 case GET_FROM_DISPLAY_VECTOR:
5343 if (it->s)
5344 it->method = GET_FROM_C_STRING;
5345 else if (STRINGP (it->string))
5346 it->method = GET_FROM_STRING;
5347 else
5348 {
5349 it->method = GET_FROM_BUFFER;
5350 it->object = it->w->buffer;
5351 }
5352 }
5353 it->end_charpos = p->end_charpos;
5354 it->string_nchars = p->string_nchars;
5355 it->area = p->area;
5356 it->multibyte_p = p->multibyte_p;
5357 it->avoid_cursor_p = p->avoid_cursor_p;
5358 it->space_width = p->space_width;
5359 it->font_height = p->font_height;
5360 it->voffset = p->voffset;
5361 it->string_from_display_prop_p = p->string_from_display_prop_p;
5362 it->line_wrap = p->line_wrap;
5363 it->bidi_p = p->bidi_p;
5364 it->paragraph_embedding = p->paragraph_embedding;
5365 if (it->bidi_p)
5366 {
5367 bidi_pop_it (&it->bidi_it);
5368 /* Bidi-iterate until we get out of the portion of text, if any,
5369 covered by a `display' text property or by an overlay with
5370 `display' property. (We cannot just jump there, because the
5371 internal coherency of the bidi iterator state can not be
5372 preserved across such jumps.) We also must determine the
5373 paragraph base direction if the overlay we just processed is
5374 at the beginning of a new paragraph. */
5375 if (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING)
5376 iterate_out_of_display_property (it);
5377 }
5378 }
5379
5380
5381 \f
5382 /***********************************************************************
5383 Moving over lines
5384 ***********************************************************************/
5385
5386 /* Set IT's current position to the previous line start. */
5387
5388 static void
5389 back_to_previous_line_start (struct it *it)
5390 {
5391 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5392 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5393 }
5394
5395
5396 /* Move IT to the next line start.
5397
5398 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5399 we skipped over part of the text (as opposed to moving the iterator
5400 continuously over the text). Otherwise, don't change the value
5401 of *SKIPPED_P.
5402
5403 Newlines may come from buffer text, overlay strings, or strings
5404 displayed via the `display' property. That's the reason we can't
5405 simply use find_next_newline_no_quit.
5406
5407 Note that this function may not skip over invisible text that is so
5408 because of text properties and immediately follows a newline. If
5409 it would, function reseat_at_next_visible_line_start, when called
5410 from set_iterator_to_next, would effectively make invisible
5411 characters following a newline part of the wrong glyph row, which
5412 leads to wrong cursor motion. */
5413
5414 static int
5415 forward_to_next_line_start (struct it *it, int *skipped_p)
5416 {
5417 int old_selective, newline_found_p, n;
5418 const int MAX_NEWLINE_DISTANCE = 500;
5419
5420 /* If already on a newline, just consume it to avoid unintended
5421 skipping over invisible text below. */
5422 if (it->what == IT_CHARACTER
5423 && it->c == '\n'
5424 && CHARPOS (it->position) == IT_CHARPOS (*it))
5425 {
5426 set_iterator_to_next (it, 0);
5427 it->c = 0;
5428 return 1;
5429 }
5430
5431 /* Don't handle selective display in the following. It's (a)
5432 unnecessary because it's done by the caller, and (b) leads to an
5433 infinite recursion because next_element_from_ellipsis indirectly
5434 calls this function. */
5435 old_selective = it->selective;
5436 it->selective = 0;
5437
5438 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5439 from buffer text. */
5440 for (n = newline_found_p = 0;
5441 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5442 n += STRINGP (it->string) ? 0 : 1)
5443 {
5444 if (!get_next_display_element (it))
5445 return 0;
5446 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5447 set_iterator_to_next (it, 0);
5448 }
5449
5450 /* If we didn't find a newline near enough, see if we can use a
5451 short-cut. */
5452 if (!newline_found_p)
5453 {
5454 EMACS_INT start = IT_CHARPOS (*it);
5455 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5456 Lisp_Object pos;
5457
5458 xassert (!STRINGP (it->string));
5459
5460 /* If we are not bidi-reordering, and there isn't any `display'
5461 property in sight, and no overlays, we can just use the
5462 position of the newline in buffer text. */
5463 if (!it->bidi_p
5464 && (it->stop_charpos >= limit
5465 || ((pos = Fnext_single_property_change (make_number (start),
5466 Qdisplay, Qnil,
5467 make_number (limit)),
5468 NILP (pos))
5469 && next_overlay_change (start) == ZV)))
5470 {
5471 IT_CHARPOS (*it) = limit;
5472 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5473 *skipped_p = newline_found_p = 1;
5474 }
5475 else
5476 {
5477 while (get_next_display_element (it)
5478 && !newline_found_p)
5479 {
5480 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5481 set_iterator_to_next (it, 0);
5482 }
5483 }
5484 }
5485
5486 it->selective = old_selective;
5487 return newline_found_p;
5488 }
5489
5490
5491 /* Set IT's current position to the previous visible line start. Skip
5492 invisible text that is so either due to text properties or due to
5493 selective display. Caution: this does not change IT->current_x and
5494 IT->hpos. */
5495
5496 static void
5497 back_to_previous_visible_line_start (struct it *it)
5498 {
5499 while (IT_CHARPOS (*it) > BEGV)
5500 {
5501 back_to_previous_line_start (it);
5502
5503 if (IT_CHARPOS (*it) <= BEGV)
5504 break;
5505
5506 /* If selective > 0, then lines indented more than its value are
5507 invisible. */
5508 if (it->selective > 0
5509 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5510 (double) it->selective)) /* iftc */
5511 continue;
5512
5513 /* Check the newline before point for invisibility. */
5514 {
5515 Lisp_Object prop;
5516 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5517 Qinvisible, it->window);
5518 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5519 continue;
5520 }
5521
5522 if (IT_CHARPOS (*it) <= BEGV)
5523 break;
5524
5525 {
5526 struct it it2;
5527 EMACS_INT pos;
5528 EMACS_INT beg, end;
5529 Lisp_Object val, overlay;
5530
5531 /* If newline is part of a composition, continue from start of composition */
5532 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5533 && beg < IT_CHARPOS (*it))
5534 goto replaced;
5535
5536 /* If newline is replaced by a display property, find start of overlay
5537 or interval and continue search from that point. */
5538 it2 = *it;
5539 pos = --IT_CHARPOS (it2);
5540 --IT_BYTEPOS (it2);
5541 it2.sp = 0;
5542 it2.string_from_display_prop_p = 0;
5543 if (handle_display_prop (&it2) == HANDLED_RETURN
5544 && !NILP (val = get_char_property_and_overlay
5545 (make_number (pos), Qdisplay, Qnil, &overlay))
5546 && (OVERLAYP (overlay)
5547 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5548 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5549 {
5550 /* If the call to handle_display_prop above pushed the
5551 iterator state, that causes side effects for the bidi
5552 iterator by calling bidi_push_it. Undo those side
5553 effects. */
5554 while (it2.sp > 0)
5555 {
5556 /* push_it calls bidi_push_it only if the bidi_p flag
5557 is set in the iterator being pushed. */
5558 if (it2.stack[--it2.sp].bidi_p)
5559 bidi_pop_it (&it2.bidi_it);
5560 }
5561 goto replaced;
5562 }
5563
5564 /* Newline is not replaced by anything -- so we are done. */
5565 break;
5566
5567 replaced:
5568 if (beg < BEGV)
5569 beg = BEGV;
5570 IT_CHARPOS (*it) = beg;
5571 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5572 }
5573 }
5574
5575 it->continuation_lines_width = 0;
5576
5577 xassert (IT_CHARPOS (*it) >= BEGV);
5578 xassert (IT_CHARPOS (*it) == BEGV
5579 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5580 CHECK_IT (it);
5581 }
5582
5583
5584 /* Reseat iterator IT at the previous visible line start. Skip
5585 invisible text that is so either due to text properties or due to
5586 selective display. At the end, update IT's overlay information,
5587 face information etc. */
5588
5589 void
5590 reseat_at_previous_visible_line_start (struct it *it)
5591 {
5592 back_to_previous_visible_line_start (it);
5593 reseat (it, it->current.pos, 1);
5594 CHECK_IT (it);
5595 }
5596
5597
5598 /* Reseat iterator IT on the next visible line start in the current
5599 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5600 preceding the line start. Skip over invisible text that is so
5601 because of selective display. Compute faces, overlays etc at the
5602 new position. Note that this function does not skip over text that
5603 is invisible because of text properties. */
5604
5605 static void
5606 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5607 {
5608 int newline_found_p, skipped_p = 0;
5609
5610 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5611
5612 /* Skip over lines that are invisible because they are indented
5613 more than the value of IT->selective. */
5614 if (it->selective > 0)
5615 while (IT_CHARPOS (*it) < ZV
5616 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5617 (double) it->selective)) /* iftc */
5618 {
5619 xassert (IT_BYTEPOS (*it) == BEGV
5620 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5621 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5622 }
5623
5624 /* Position on the newline if that's what's requested. */
5625 if (on_newline_p && newline_found_p)
5626 {
5627 if (STRINGP (it->string))
5628 {
5629 if (IT_STRING_CHARPOS (*it) > 0)
5630 {
5631 if (!it->bidi_p)
5632 {
5633 --IT_STRING_CHARPOS (*it);
5634 --IT_STRING_BYTEPOS (*it);
5635 }
5636 else
5637 /* Setting this flag will cause
5638 bidi_move_to_visually_next not to advance, but
5639 instead deliver the current character (newline),
5640 which is what the ON_NEWLINE_P flag wants. */
5641 it->bidi_it.first_elt = 1;
5642 }
5643 }
5644 else if (IT_CHARPOS (*it) > BEGV)
5645 {
5646 if (!it->bidi_p)
5647 {
5648 --IT_CHARPOS (*it);
5649 --IT_BYTEPOS (*it);
5650 }
5651 /* With bidi iteration, the call to `reseat' will cause
5652 bidi_move_to_visually_next deliver the current character,
5653 the newline, instead of advancing. */
5654 reseat (it, it->current.pos, 0);
5655 }
5656 }
5657 else if (skipped_p)
5658 reseat (it, it->current.pos, 0);
5659
5660 CHECK_IT (it);
5661 }
5662
5663
5664 \f
5665 /***********************************************************************
5666 Changing an iterator's position
5667 ***********************************************************************/
5668
5669 /* Change IT's current position to POS in current_buffer. If FORCE_P
5670 is non-zero, always check for text properties at the new position.
5671 Otherwise, text properties are only looked up if POS >=
5672 IT->check_charpos of a property. */
5673
5674 static void
5675 reseat (struct it *it, struct text_pos pos, int force_p)
5676 {
5677 EMACS_INT original_pos = IT_CHARPOS (*it);
5678
5679 reseat_1 (it, pos, 0);
5680
5681 /* Determine where to check text properties. Avoid doing it
5682 where possible because text property lookup is very expensive. */
5683 if (force_p
5684 || CHARPOS (pos) > it->stop_charpos
5685 || CHARPOS (pos) < original_pos)
5686 {
5687 if (it->bidi_p)
5688 {
5689 /* For bidi iteration, we need to prime prev_stop and
5690 base_level_stop with our best estimations. */
5691 if (CHARPOS (pos) < it->prev_stop)
5692 {
5693 handle_stop_backwards (it, BEGV);
5694 if (CHARPOS (pos) < it->base_level_stop)
5695 it->base_level_stop = 0;
5696 }
5697 else if (CHARPOS (pos) > it->stop_charpos
5698 && it->stop_charpos >= BEGV)
5699 handle_stop_backwards (it, it->stop_charpos);
5700 else /* force_p */
5701 handle_stop (it);
5702 }
5703 else
5704 {
5705 handle_stop (it);
5706 it->prev_stop = it->base_level_stop = 0;
5707 }
5708
5709 }
5710
5711 CHECK_IT (it);
5712 }
5713
5714
5715 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5716 IT->stop_pos to POS, also. */
5717
5718 static void
5719 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5720 {
5721 /* Don't call this function when scanning a C string. */
5722 xassert (it->s == NULL);
5723
5724 /* POS must be a reasonable value. */
5725 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5726
5727 it->current.pos = it->position = pos;
5728 it->end_charpos = ZV;
5729 it->dpvec = NULL;
5730 it->current.dpvec_index = -1;
5731 it->current.overlay_string_index = -1;
5732 IT_STRING_CHARPOS (*it) = -1;
5733 IT_STRING_BYTEPOS (*it) = -1;
5734 it->string = Qnil;
5735 it->method = GET_FROM_BUFFER;
5736 it->object = it->w->buffer;
5737 it->area = TEXT_AREA;
5738 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5739 it->sp = 0;
5740 it->string_from_display_prop_p = 0;
5741 it->face_before_selective_p = 0;
5742 if (it->bidi_p)
5743 {
5744 it->bidi_it.first_elt = 1;
5745 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5746 it->bidi_it.disp_pos = -1;
5747 it->bidi_it.string.s = NULL;
5748 it->bidi_it.string.lstring = Qnil;
5749 it->bidi_it.string.bufpos = 0;
5750 }
5751
5752 if (set_stop_p)
5753 {
5754 it->stop_charpos = CHARPOS (pos);
5755 it->base_level_stop = CHARPOS (pos);
5756 }
5757 }
5758
5759
5760 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5761 If S is non-null, it is a C string to iterate over. Otherwise,
5762 STRING gives a Lisp string to iterate over.
5763
5764 If PRECISION > 0, don't return more then PRECISION number of
5765 characters from the string.
5766
5767 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5768 characters have been returned. FIELD_WIDTH < 0 means an infinite
5769 field width.
5770
5771 MULTIBYTE = 0 means disable processing of multibyte characters,
5772 MULTIBYTE > 0 means enable it,
5773 MULTIBYTE < 0 means use IT->multibyte_p.
5774
5775 IT must be initialized via a prior call to init_iterator before
5776 calling this function. */
5777
5778 static void
5779 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5780 EMACS_INT charpos, EMACS_INT precision, int field_width,
5781 int multibyte)
5782 {
5783 /* No region in strings. */
5784 it->region_beg_charpos = it->region_end_charpos = -1;
5785
5786 /* No text property checks performed by default, but see below. */
5787 it->stop_charpos = -1;
5788
5789 /* Set iterator position and end position. */
5790 memset (&it->current, 0, sizeof it->current);
5791 it->current.overlay_string_index = -1;
5792 it->current.dpvec_index = -1;
5793 xassert (charpos >= 0);
5794
5795 /* If STRING is specified, use its multibyteness, otherwise use the
5796 setting of MULTIBYTE, if specified. */
5797 if (multibyte >= 0)
5798 it->multibyte_p = multibyte > 0;
5799
5800 /* Bidirectional reordering of strings is controlled by the default
5801 value of bidi-display-reordering. */
5802 it->bidi_p =
5803 !NILP (BVAR (&buffer_defaults, bidi_display_reordering))
5804 && it->multibyte_p;
5805
5806 if (s == NULL)
5807 {
5808 xassert (STRINGP (string));
5809 it->string = string;
5810 it->s = NULL;
5811 it->end_charpos = it->string_nchars = SCHARS (string);
5812 it->method = GET_FROM_STRING;
5813 it->current.string_pos = string_pos (charpos, string);
5814
5815 if (it->bidi_p)
5816 {
5817 it->bidi_it.string.lstring = string;
5818 it->bidi_it.string.s = NULL;
5819 it->bidi_it.string.schars = it->end_charpos;
5820 it->bidi_it.string.bufpos = 0;
5821 it->bidi_it.string.from_disp_str = 0;
5822 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5823 FRAME_WINDOW_P (it->f), &it->bidi_it);
5824 }
5825 }
5826 else
5827 {
5828 it->s = (const unsigned char *) s;
5829 it->string = Qnil;
5830
5831 /* Note that we use IT->current.pos, not it->current.string_pos,
5832 for displaying C strings. */
5833 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5834 if (it->multibyte_p)
5835 {
5836 it->current.pos = c_string_pos (charpos, s, 1);
5837 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5838
5839 if (it->bidi_p)
5840 {
5841 it->bidi_it.string.lstring = Qnil;
5842 it->bidi_it.string.s = s;
5843 it->bidi_it.string.schars = it->end_charpos;
5844 it->bidi_it.string.bufpos = 0;
5845 it->bidi_it.string.from_disp_str = 0;
5846 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5847 &it->bidi_it);
5848 }
5849 }
5850 else
5851 {
5852 /* Unibyte (a.k.a. ASCII) C strings are never bidi-reordered. */
5853 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5854 it->end_charpos = it->string_nchars = strlen (s);
5855 }
5856
5857 it->method = GET_FROM_C_STRING;
5858 }
5859
5860 /* PRECISION > 0 means don't return more than PRECISION characters
5861 from the string. */
5862 if (precision > 0 && it->end_charpos - charpos > precision)
5863 {
5864 it->end_charpos = it->string_nchars = charpos + precision;
5865 if (it->bidi_p)
5866 it->bidi_it.string.schars = it->end_charpos;
5867 }
5868
5869 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5870 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5871 FIELD_WIDTH < 0 means infinite field width. This is useful for
5872 padding with `-' at the end of a mode line. */
5873 if (field_width < 0)
5874 field_width = INFINITY;
5875 /* Implementation note: We deliberately don't enlarge
5876 it->bidi_it.string.schars here to fit it->end_charpos, because
5877 the bidi iterator cannot produce characters out of thin air. */
5878 if (field_width > it->end_charpos - charpos)
5879 it->end_charpos = charpos + field_width;
5880
5881 /* Use the standard display table for displaying strings. */
5882 if (DISP_TABLE_P (Vstandard_display_table))
5883 it->dp = XCHAR_TABLE (Vstandard_display_table);
5884
5885 it->stop_charpos = charpos;
5886 it->prev_stop = charpos;
5887 it->base_level_stop = 0;
5888 if (it->bidi_p)
5889 {
5890 it->bidi_it.first_elt = 1;
5891 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5892 it->bidi_it.disp_pos = -1;
5893 }
5894 if (s == NULL && it->multibyte_p)
5895 {
5896 EMACS_INT endpos = SCHARS (it->string);
5897 if (endpos > it->end_charpos)
5898 endpos = it->end_charpos;
5899 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5900 it->string);
5901 }
5902 CHECK_IT (it);
5903 }
5904
5905
5906 \f
5907 /***********************************************************************
5908 Iteration
5909 ***********************************************************************/
5910
5911 /* Map enum it_method value to corresponding next_element_from_* function. */
5912
5913 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5914 {
5915 next_element_from_buffer,
5916 next_element_from_display_vector,
5917 next_element_from_string,
5918 next_element_from_c_string,
5919 next_element_from_image,
5920 next_element_from_stretch
5921 };
5922
5923 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5924
5925
5926 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5927 (possibly with the following characters). */
5928
5929 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5930 ((IT)->cmp_it.id >= 0 \
5931 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5932 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5933 END_CHARPOS, (IT)->w, \
5934 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5935 (IT)->string)))
5936
5937
5938 /* Lookup the char-table Vglyphless_char_display for character C (-1
5939 if we want information for no-font case), and return the display
5940 method symbol. By side-effect, update it->what and
5941 it->glyphless_method. This function is called from
5942 get_next_display_element for each character element, and from
5943 x_produce_glyphs when no suitable font was found. */
5944
5945 Lisp_Object
5946 lookup_glyphless_char_display (int c, struct it *it)
5947 {
5948 Lisp_Object glyphless_method = Qnil;
5949
5950 if (CHAR_TABLE_P (Vglyphless_char_display)
5951 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5952 {
5953 if (c >= 0)
5954 {
5955 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
5956 if (CONSP (glyphless_method))
5957 glyphless_method = FRAME_WINDOW_P (it->f)
5958 ? XCAR (glyphless_method)
5959 : XCDR (glyphless_method);
5960 }
5961 else
5962 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
5963 }
5964
5965 retry:
5966 if (NILP (glyphless_method))
5967 {
5968 if (c >= 0)
5969 /* The default is to display the character by a proper font. */
5970 return Qnil;
5971 /* The default for the no-font case is to display an empty box. */
5972 glyphless_method = Qempty_box;
5973 }
5974 if (EQ (glyphless_method, Qzero_width))
5975 {
5976 if (c >= 0)
5977 return glyphless_method;
5978 /* This method can't be used for the no-font case. */
5979 glyphless_method = Qempty_box;
5980 }
5981 if (EQ (glyphless_method, Qthin_space))
5982 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5983 else if (EQ (glyphless_method, Qempty_box))
5984 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5985 else if (EQ (glyphless_method, Qhex_code))
5986 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5987 else if (STRINGP (glyphless_method))
5988 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5989 else
5990 {
5991 /* Invalid value. We use the default method. */
5992 glyphless_method = Qnil;
5993 goto retry;
5994 }
5995 it->what = IT_GLYPHLESS;
5996 return glyphless_method;
5997 }
5998
5999 /* Load IT's display element fields with information about the next
6000 display element from the current position of IT. Value is zero if
6001 end of buffer (or C string) is reached. */
6002
6003 static struct frame *last_escape_glyph_frame = NULL;
6004 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6005 static int last_escape_glyph_merged_face_id = 0;
6006
6007 struct frame *last_glyphless_glyph_frame = NULL;
6008 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6009 int last_glyphless_glyph_merged_face_id = 0;
6010
6011 static int
6012 get_next_display_element (struct it *it)
6013 {
6014 /* Non-zero means that we found a display element. Zero means that
6015 we hit the end of what we iterate over. Performance note: the
6016 function pointer `method' used here turns out to be faster than
6017 using a sequence of if-statements. */
6018 int success_p;
6019
6020 get_next:
6021 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6022
6023 if (it->what == IT_CHARACTER)
6024 {
6025 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6026 and only if (a) the resolved directionality of that character
6027 is R..." */
6028 /* FIXME: Do we need an exception for characters from display
6029 tables? */
6030 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6031 it->c = bidi_mirror_char (it->c);
6032 /* Map via display table or translate control characters.
6033 IT->c, IT->len etc. have been set to the next character by
6034 the function call above. If we have a display table, and it
6035 contains an entry for IT->c, translate it. Don't do this if
6036 IT->c itself comes from a display table, otherwise we could
6037 end up in an infinite recursion. (An alternative could be to
6038 count the recursion depth of this function and signal an
6039 error when a certain maximum depth is reached.) Is it worth
6040 it? */
6041 if (success_p && it->dpvec == NULL)
6042 {
6043 Lisp_Object dv;
6044 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6045 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6046 nbsp_or_shy = char_is_other;
6047 int c = it->c; /* This is the character to display. */
6048
6049 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6050 {
6051 xassert (SINGLE_BYTE_CHAR_P (c));
6052 if (unibyte_display_via_language_environment)
6053 {
6054 c = DECODE_CHAR (unibyte, c);
6055 if (c < 0)
6056 c = BYTE8_TO_CHAR (it->c);
6057 }
6058 else
6059 c = BYTE8_TO_CHAR (it->c);
6060 }
6061
6062 if (it->dp
6063 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6064 VECTORP (dv)))
6065 {
6066 struct Lisp_Vector *v = XVECTOR (dv);
6067
6068 /* Return the first character from the display table
6069 entry, if not empty. If empty, don't display the
6070 current character. */
6071 if (v->header.size)
6072 {
6073 it->dpvec_char_len = it->len;
6074 it->dpvec = v->contents;
6075 it->dpend = v->contents + v->header.size;
6076 it->current.dpvec_index = 0;
6077 it->dpvec_face_id = -1;
6078 it->saved_face_id = it->face_id;
6079 it->method = GET_FROM_DISPLAY_VECTOR;
6080 it->ellipsis_p = 0;
6081 }
6082 else
6083 {
6084 set_iterator_to_next (it, 0);
6085 }
6086 goto get_next;
6087 }
6088
6089 if (! NILP (lookup_glyphless_char_display (c, it)))
6090 {
6091 if (it->what == IT_GLYPHLESS)
6092 goto done;
6093 /* Don't display this character. */
6094 set_iterator_to_next (it, 0);
6095 goto get_next;
6096 }
6097
6098 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6099 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6100 : c == 0xAD ? char_is_soft_hyphen
6101 : char_is_other);
6102
6103 /* Translate control characters into `\003' or `^C' form.
6104 Control characters coming from a display table entry are
6105 currently not translated because we use IT->dpvec to hold
6106 the translation. This could easily be changed but I
6107 don't believe that it is worth doing.
6108
6109 NBSP and SOFT-HYPEN are property translated too.
6110
6111 Non-printable characters and raw-byte characters are also
6112 translated to octal form. */
6113 if (((c < ' ' || c == 127) /* ASCII control chars */
6114 ? (it->area != TEXT_AREA
6115 /* In mode line, treat \n, \t like other crl chars. */
6116 || (c != '\t'
6117 && it->glyph_row
6118 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6119 || (c != '\n' && c != '\t'))
6120 : (nbsp_or_shy
6121 || CHAR_BYTE8_P (c)
6122 || ! CHAR_PRINTABLE_P (c))))
6123 {
6124 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6125 or a non-printable character which must be displayed
6126 either as '\003' or as `^C' where the '\\' and '^'
6127 can be defined in the display table. Fill
6128 IT->ctl_chars with glyphs for what we have to
6129 display. Then, set IT->dpvec to these glyphs. */
6130 Lisp_Object gc;
6131 int ctl_len;
6132 int face_id, lface_id = 0 ;
6133 int escape_glyph;
6134
6135 /* Handle control characters with ^. */
6136
6137 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6138 {
6139 int g;
6140
6141 g = '^'; /* default glyph for Control */
6142 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6143 if (it->dp
6144 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6145 && GLYPH_CODE_CHAR_VALID_P (gc))
6146 {
6147 g = GLYPH_CODE_CHAR (gc);
6148 lface_id = GLYPH_CODE_FACE (gc);
6149 }
6150 if (lface_id)
6151 {
6152 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6153 }
6154 else if (it->f == last_escape_glyph_frame
6155 && it->face_id == last_escape_glyph_face_id)
6156 {
6157 face_id = last_escape_glyph_merged_face_id;
6158 }
6159 else
6160 {
6161 /* Merge the escape-glyph face into the current face. */
6162 face_id = merge_faces (it->f, Qescape_glyph, 0,
6163 it->face_id);
6164 last_escape_glyph_frame = it->f;
6165 last_escape_glyph_face_id = it->face_id;
6166 last_escape_glyph_merged_face_id = face_id;
6167 }
6168
6169 XSETINT (it->ctl_chars[0], g);
6170 XSETINT (it->ctl_chars[1], c ^ 0100);
6171 ctl_len = 2;
6172 goto display_control;
6173 }
6174
6175 /* Handle non-break space in the mode where it only gets
6176 highlighting. */
6177
6178 if (EQ (Vnobreak_char_display, Qt)
6179 && nbsp_or_shy == char_is_nbsp)
6180 {
6181 /* Merge the no-break-space face into the current face. */
6182 face_id = merge_faces (it->f, Qnobreak_space, 0,
6183 it->face_id);
6184
6185 c = ' ';
6186 XSETINT (it->ctl_chars[0], ' ');
6187 ctl_len = 1;
6188 goto display_control;
6189 }
6190
6191 /* Handle sequences that start with the "escape glyph". */
6192
6193 /* the default escape glyph is \. */
6194 escape_glyph = '\\';
6195
6196 if (it->dp
6197 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6198 && GLYPH_CODE_CHAR_VALID_P (gc))
6199 {
6200 escape_glyph = GLYPH_CODE_CHAR (gc);
6201 lface_id = GLYPH_CODE_FACE (gc);
6202 }
6203 if (lface_id)
6204 {
6205 /* The display table specified a face.
6206 Merge it into face_id and also into escape_glyph. */
6207 face_id = merge_faces (it->f, Qt, lface_id,
6208 it->face_id);
6209 }
6210 else if (it->f == last_escape_glyph_frame
6211 && it->face_id == last_escape_glyph_face_id)
6212 {
6213 face_id = last_escape_glyph_merged_face_id;
6214 }
6215 else
6216 {
6217 /* Merge the escape-glyph face into the current face. */
6218 face_id = merge_faces (it->f, Qescape_glyph, 0,
6219 it->face_id);
6220 last_escape_glyph_frame = it->f;
6221 last_escape_glyph_face_id = it->face_id;
6222 last_escape_glyph_merged_face_id = face_id;
6223 }
6224
6225 /* Handle soft hyphens in the mode where they only get
6226 highlighting. */
6227
6228 if (EQ (Vnobreak_char_display, Qt)
6229 && nbsp_or_shy == char_is_soft_hyphen)
6230 {
6231 XSETINT (it->ctl_chars[0], '-');
6232 ctl_len = 1;
6233 goto display_control;
6234 }
6235
6236 /* Handle non-break space and soft hyphen
6237 with the escape glyph. */
6238
6239 if (nbsp_or_shy)
6240 {
6241 XSETINT (it->ctl_chars[0], escape_glyph);
6242 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6243 XSETINT (it->ctl_chars[1], c);
6244 ctl_len = 2;
6245 goto display_control;
6246 }
6247
6248 {
6249 char str[10];
6250 int len, i;
6251
6252 if (CHAR_BYTE8_P (c))
6253 /* Display \200 instead of \17777600. */
6254 c = CHAR_TO_BYTE8 (c);
6255 len = sprintf (str, "%03o", c);
6256
6257 XSETINT (it->ctl_chars[0], escape_glyph);
6258 for (i = 0; i < len; i++)
6259 XSETINT (it->ctl_chars[i + 1], str[i]);
6260 ctl_len = len + 1;
6261 }
6262
6263 display_control:
6264 /* Set up IT->dpvec and return first character from it. */
6265 it->dpvec_char_len = it->len;
6266 it->dpvec = it->ctl_chars;
6267 it->dpend = it->dpvec + ctl_len;
6268 it->current.dpvec_index = 0;
6269 it->dpvec_face_id = face_id;
6270 it->saved_face_id = it->face_id;
6271 it->method = GET_FROM_DISPLAY_VECTOR;
6272 it->ellipsis_p = 0;
6273 goto get_next;
6274 }
6275 it->char_to_display = c;
6276 }
6277 else if (success_p)
6278 {
6279 it->char_to_display = it->c;
6280 }
6281 }
6282
6283 /* Adjust face id for a multibyte character. There are no multibyte
6284 character in unibyte text. */
6285 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6286 && it->multibyte_p
6287 && success_p
6288 && FRAME_WINDOW_P (it->f))
6289 {
6290 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6291
6292 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6293 {
6294 /* Automatic composition with glyph-string. */
6295 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6296
6297 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6298 }
6299 else
6300 {
6301 EMACS_INT pos = (it->s ? -1
6302 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6303 : IT_CHARPOS (*it));
6304
6305 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6306 it->string);
6307 }
6308 }
6309
6310 done:
6311 /* Is this character the last one of a run of characters with
6312 box? If yes, set IT->end_of_box_run_p to 1. */
6313 if (it->face_box_p
6314 && it->s == NULL)
6315 {
6316 if (it->method == GET_FROM_STRING && it->sp)
6317 {
6318 int face_id = underlying_face_id (it);
6319 struct face *face = FACE_FROM_ID (it->f, face_id);
6320
6321 if (face)
6322 {
6323 if (face->box == FACE_NO_BOX)
6324 {
6325 /* If the box comes from face properties in a
6326 display string, check faces in that string. */
6327 int string_face_id = face_after_it_pos (it);
6328 it->end_of_box_run_p
6329 = (FACE_FROM_ID (it->f, string_face_id)->box
6330 == FACE_NO_BOX);
6331 }
6332 /* Otherwise, the box comes from the underlying face.
6333 If this is the last string character displayed, check
6334 the next buffer location. */
6335 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6336 && (it->current.overlay_string_index
6337 == it->n_overlay_strings - 1))
6338 {
6339 EMACS_INT ignore;
6340 int next_face_id;
6341 struct text_pos pos = it->current.pos;
6342 INC_TEXT_POS (pos, it->multibyte_p);
6343
6344 next_face_id = face_at_buffer_position
6345 (it->w, CHARPOS (pos), it->region_beg_charpos,
6346 it->region_end_charpos, &ignore,
6347 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6348 -1);
6349 it->end_of_box_run_p
6350 = (FACE_FROM_ID (it->f, next_face_id)->box
6351 == FACE_NO_BOX);
6352 }
6353 }
6354 }
6355 else
6356 {
6357 int face_id = face_after_it_pos (it);
6358 it->end_of_box_run_p
6359 = (face_id != it->face_id
6360 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6361 }
6362 }
6363
6364 /* Value is 0 if end of buffer or string reached. */
6365 return success_p;
6366 }
6367
6368
6369 /* Move IT to the next display element.
6370
6371 RESEAT_P non-zero means if called on a newline in buffer text,
6372 skip to the next visible line start.
6373
6374 Functions get_next_display_element and set_iterator_to_next are
6375 separate because I find this arrangement easier to handle than a
6376 get_next_display_element function that also increments IT's
6377 position. The way it is we can first look at an iterator's current
6378 display element, decide whether it fits on a line, and if it does,
6379 increment the iterator position. The other way around we probably
6380 would either need a flag indicating whether the iterator has to be
6381 incremented the next time, or we would have to implement a
6382 decrement position function which would not be easy to write. */
6383
6384 void
6385 set_iterator_to_next (struct it *it, int reseat_p)
6386 {
6387 /* Reset flags indicating start and end of a sequence of characters
6388 with box. Reset them at the start of this function because
6389 moving the iterator to a new position might set them. */
6390 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6391
6392 switch (it->method)
6393 {
6394 case GET_FROM_BUFFER:
6395 /* The current display element of IT is a character from
6396 current_buffer. Advance in the buffer, and maybe skip over
6397 invisible lines that are so because of selective display. */
6398 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6399 reseat_at_next_visible_line_start (it, 0);
6400 else if (it->cmp_it.id >= 0)
6401 {
6402 /* We are currently getting glyphs from a composition. */
6403 int i;
6404
6405 if (! it->bidi_p)
6406 {
6407 IT_CHARPOS (*it) += it->cmp_it.nchars;
6408 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6409 if (it->cmp_it.to < it->cmp_it.nglyphs)
6410 {
6411 it->cmp_it.from = it->cmp_it.to;
6412 }
6413 else
6414 {
6415 it->cmp_it.id = -1;
6416 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6417 IT_BYTEPOS (*it),
6418 it->end_charpos, Qnil);
6419 }
6420 }
6421 else if (! it->cmp_it.reversed_p)
6422 {
6423 /* Composition created while scanning forward. */
6424 /* Update IT's char/byte positions to point to the first
6425 character of the next grapheme cluster, or to the
6426 character visually after the current composition. */
6427 for (i = 0; i < it->cmp_it.nchars; i++)
6428 bidi_move_to_visually_next (&it->bidi_it);
6429 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6430 IT_CHARPOS (*it) = it->bidi_it.charpos;
6431
6432 if (it->cmp_it.to < it->cmp_it.nglyphs)
6433 {
6434 /* Proceed to the next grapheme cluster. */
6435 it->cmp_it.from = it->cmp_it.to;
6436 }
6437 else
6438 {
6439 /* No more grapheme clusters in this composition.
6440 Find the next stop position. */
6441 EMACS_INT stop = it->end_charpos;
6442 if (it->bidi_it.scan_dir < 0)
6443 /* Now we are scanning backward and don't know
6444 where to stop. */
6445 stop = -1;
6446 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6447 IT_BYTEPOS (*it), stop, Qnil);
6448 }
6449 }
6450 else
6451 {
6452 /* Composition created while scanning backward. */
6453 /* Update IT's char/byte positions to point to the last
6454 character of the previous grapheme cluster, or the
6455 character visually after the current composition. */
6456 for (i = 0; i < it->cmp_it.nchars; i++)
6457 bidi_move_to_visually_next (&it->bidi_it);
6458 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6459 IT_CHARPOS (*it) = it->bidi_it.charpos;
6460 if (it->cmp_it.from > 0)
6461 {
6462 /* Proceed to the previous grapheme cluster. */
6463 it->cmp_it.to = it->cmp_it.from;
6464 }
6465 else
6466 {
6467 /* No more grapheme clusters in this composition.
6468 Find the next stop position. */
6469 EMACS_INT stop = it->end_charpos;
6470 if (it->bidi_it.scan_dir < 0)
6471 /* Now we are scanning backward and don't know
6472 where to stop. */
6473 stop = -1;
6474 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6475 IT_BYTEPOS (*it), stop, Qnil);
6476 }
6477 }
6478 }
6479 else
6480 {
6481 xassert (it->len != 0);
6482
6483 if (!it->bidi_p)
6484 {
6485 IT_BYTEPOS (*it) += it->len;
6486 IT_CHARPOS (*it) += 1;
6487 }
6488 else
6489 {
6490 int prev_scan_dir = it->bidi_it.scan_dir;
6491 /* If this is a new paragraph, determine its base
6492 direction (a.k.a. its base embedding level). */
6493 if (it->bidi_it.new_paragraph)
6494 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6495 bidi_move_to_visually_next (&it->bidi_it);
6496 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6497 IT_CHARPOS (*it) = it->bidi_it.charpos;
6498 if (prev_scan_dir != it->bidi_it.scan_dir)
6499 {
6500 /* As the scan direction was changed, we must
6501 re-compute the stop position for composition. */
6502 EMACS_INT stop = it->end_charpos;
6503 if (it->bidi_it.scan_dir < 0)
6504 stop = -1;
6505 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6506 IT_BYTEPOS (*it), stop, Qnil);
6507 }
6508 }
6509 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6510 }
6511 break;
6512
6513 case GET_FROM_C_STRING:
6514 /* Current display element of IT is from a C string. */
6515 if (!it->bidi_p
6516 /* If the string position is beyond string's end, it means
6517 next_element_from_c_string is padding the string with
6518 blanks, in which case we bypass the bidi iterator,
6519 because it cannot deal with such virtual characters. */
6520 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6521 {
6522 IT_BYTEPOS (*it) += it->len;
6523 IT_CHARPOS (*it) += 1;
6524 }
6525 else
6526 {
6527 bidi_move_to_visually_next (&it->bidi_it);
6528 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6529 IT_CHARPOS (*it) = it->bidi_it.charpos;
6530 }
6531 break;
6532
6533 case GET_FROM_DISPLAY_VECTOR:
6534 /* Current display element of IT is from a display table entry.
6535 Advance in the display table definition. Reset it to null if
6536 end reached, and continue with characters from buffers/
6537 strings. */
6538 ++it->current.dpvec_index;
6539
6540 /* Restore face of the iterator to what they were before the
6541 display vector entry (these entries may contain faces). */
6542 it->face_id = it->saved_face_id;
6543
6544 if (it->dpvec + it->current.dpvec_index == it->dpend)
6545 {
6546 int recheck_faces = it->ellipsis_p;
6547
6548 if (it->s)
6549 it->method = GET_FROM_C_STRING;
6550 else if (STRINGP (it->string))
6551 it->method = GET_FROM_STRING;
6552 else
6553 {
6554 it->method = GET_FROM_BUFFER;
6555 it->object = it->w->buffer;
6556 }
6557
6558 it->dpvec = NULL;
6559 it->current.dpvec_index = -1;
6560
6561 /* Skip over characters which were displayed via IT->dpvec. */
6562 if (it->dpvec_char_len < 0)
6563 reseat_at_next_visible_line_start (it, 1);
6564 else if (it->dpvec_char_len > 0)
6565 {
6566 if (it->method == GET_FROM_STRING
6567 && it->n_overlay_strings > 0)
6568 it->ignore_overlay_strings_at_pos_p = 1;
6569 it->len = it->dpvec_char_len;
6570 set_iterator_to_next (it, reseat_p);
6571 }
6572
6573 /* Maybe recheck faces after display vector */
6574 if (recheck_faces)
6575 it->stop_charpos = IT_CHARPOS (*it);
6576 }
6577 break;
6578
6579 case GET_FROM_STRING:
6580 /* Current display element is a character from a Lisp string. */
6581 xassert (it->s == NULL && STRINGP (it->string));
6582 if (it->cmp_it.id >= 0)
6583 {
6584 int i;
6585
6586 if (! it->bidi_p)
6587 {
6588 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6589 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6590 if (it->cmp_it.to < it->cmp_it.nglyphs)
6591 it->cmp_it.from = it->cmp_it.to;
6592 else
6593 {
6594 it->cmp_it.id = -1;
6595 composition_compute_stop_pos (&it->cmp_it,
6596 IT_STRING_CHARPOS (*it),
6597 IT_STRING_BYTEPOS (*it),
6598 it->end_charpos, it->string);
6599 }
6600 }
6601 else if (! it->cmp_it.reversed_p)
6602 {
6603 for (i = 0; i < it->cmp_it.nchars; i++)
6604 bidi_move_to_visually_next (&it->bidi_it);
6605 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6606 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6607
6608 if (it->cmp_it.to < it->cmp_it.nglyphs)
6609 it->cmp_it.from = it->cmp_it.to;
6610 else
6611 {
6612 EMACS_INT stop = it->end_charpos;
6613 if (it->bidi_it.scan_dir < 0)
6614 stop = -1;
6615 composition_compute_stop_pos (&it->cmp_it,
6616 IT_STRING_CHARPOS (*it),
6617 IT_STRING_BYTEPOS (*it), stop,
6618 it->string);
6619 }
6620 }
6621 else
6622 {
6623 for (i = 0; i < it->cmp_it.nchars; i++)
6624 bidi_move_to_visually_next (&it->bidi_it);
6625 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6626 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6627 if (it->cmp_it.from > 0)
6628 it->cmp_it.to = it->cmp_it.from;
6629 else
6630 {
6631 EMACS_INT stop = it->end_charpos;
6632 if (it->bidi_it.scan_dir < 0)
6633 stop = -1;
6634 composition_compute_stop_pos (&it->cmp_it,
6635 IT_STRING_CHARPOS (*it),
6636 IT_STRING_BYTEPOS (*it), stop,
6637 it->string);
6638 }
6639 }
6640 }
6641 else
6642 {
6643 if (!it->bidi_p
6644 /* If the string position is beyond string's end, it
6645 means next_element_from_string is padding the string
6646 with blanks, in which case we bypass the bidi
6647 iterator, because it cannot deal with such virtual
6648 characters. */
6649 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
6650 {
6651 IT_STRING_BYTEPOS (*it) += it->len;
6652 IT_STRING_CHARPOS (*it) += 1;
6653 }
6654 else
6655 {
6656 int prev_scan_dir = it->bidi_it.scan_dir;
6657
6658 bidi_move_to_visually_next (&it->bidi_it);
6659 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6660 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6661 if (prev_scan_dir != it->bidi_it.scan_dir)
6662 {
6663 EMACS_INT stop = it->end_charpos;
6664
6665 if (it->bidi_it.scan_dir < 0)
6666 stop = -1;
6667 composition_compute_stop_pos (&it->cmp_it,
6668 IT_STRING_CHARPOS (*it),
6669 IT_STRING_BYTEPOS (*it), stop,
6670 it->string);
6671 }
6672 }
6673 }
6674
6675 consider_string_end:
6676
6677 if (it->current.overlay_string_index >= 0)
6678 {
6679 /* IT->string is an overlay string. Advance to the
6680 next, if there is one. */
6681 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6682 {
6683 it->ellipsis_p = 0;
6684 next_overlay_string (it);
6685 if (it->ellipsis_p)
6686 setup_for_ellipsis (it, 0);
6687 }
6688 }
6689 else
6690 {
6691 /* IT->string is not an overlay string. If we reached
6692 its end, and there is something on IT->stack, proceed
6693 with what is on the stack. This can be either another
6694 string, this time an overlay string, or a buffer. */
6695 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6696 && it->sp > 0)
6697 {
6698 pop_it (it);
6699 if (it->method == GET_FROM_STRING)
6700 goto consider_string_end;
6701 }
6702 }
6703 break;
6704
6705 case GET_FROM_IMAGE:
6706 case GET_FROM_STRETCH:
6707 /* The position etc with which we have to proceed are on
6708 the stack. The position may be at the end of a string,
6709 if the `display' property takes up the whole string. */
6710 xassert (it->sp > 0);
6711 pop_it (it);
6712 if (it->method == GET_FROM_STRING)
6713 goto consider_string_end;
6714 break;
6715
6716 default:
6717 /* There are no other methods defined, so this should be a bug. */
6718 abort ();
6719 }
6720
6721 xassert (it->method != GET_FROM_STRING
6722 || (STRINGP (it->string)
6723 && IT_STRING_CHARPOS (*it) >= 0));
6724 }
6725
6726 /* Load IT's display element fields with information about the next
6727 display element which comes from a display table entry or from the
6728 result of translating a control character to one of the forms `^C'
6729 or `\003'.
6730
6731 IT->dpvec holds the glyphs to return as characters.
6732 IT->saved_face_id holds the face id before the display vector--it
6733 is restored into IT->face_id in set_iterator_to_next. */
6734
6735 static int
6736 next_element_from_display_vector (struct it *it)
6737 {
6738 Lisp_Object gc;
6739
6740 /* Precondition. */
6741 xassert (it->dpvec && it->current.dpvec_index >= 0);
6742
6743 it->face_id = it->saved_face_id;
6744
6745 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6746 That seemed totally bogus - so I changed it... */
6747 gc = it->dpvec[it->current.dpvec_index];
6748
6749 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6750 {
6751 it->c = GLYPH_CODE_CHAR (gc);
6752 it->len = CHAR_BYTES (it->c);
6753
6754 /* The entry may contain a face id to use. Such a face id is
6755 the id of a Lisp face, not a realized face. A face id of
6756 zero means no face is specified. */
6757 if (it->dpvec_face_id >= 0)
6758 it->face_id = it->dpvec_face_id;
6759 else
6760 {
6761 int lface_id = GLYPH_CODE_FACE (gc);
6762 if (lface_id > 0)
6763 it->face_id = merge_faces (it->f, Qt, lface_id,
6764 it->saved_face_id);
6765 }
6766 }
6767 else
6768 /* Display table entry is invalid. Return a space. */
6769 it->c = ' ', it->len = 1;
6770
6771 /* Don't change position and object of the iterator here. They are
6772 still the values of the character that had this display table
6773 entry or was translated, and that's what we want. */
6774 it->what = IT_CHARACTER;
6775 return 1;
6776 }
6777
6778 /* Get the first element of string/buffer in the visual order, after
6779 being reseated to a new position in a string or a buffer. */
6780 static void
6781 get_visually_first_element (struct it *it)
6782 {
6783 int string_p = STRINGP (it->string) || it->s;
6784 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
6785 EMACS_INT bob = (string_p ? 0 : BEGV);
6786
6787 if (STRINGP (it->string))
6788 {
6789 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6790 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6791 }
6792 else
6793 {
6794 it->bidi_it.charpos = IT_CHARPOS (*it);
6795 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6796 }
6797
6798 if (it->bidi_it.charpos == eob)
6799 {
6800 /* Nothing to do, but reset the FIRST_ELT flag, like
6801 bidi_paragraph_init does, because we are not going to
6802 call it. */
6803 it->bidi_it.first_elt = 0;
6804 }
6805 else if (it->bidi_it.charpos == bob
6806 || (!string_p
6807 /* FIXME: Should support all Unicode line separators. */
6808 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6809 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6810 {
6811 /* If we are at the beginning of a line/string, we can produce
6812 the next element right away. */
6813 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6814 bidi_move_to_visually_next (&it->bidi_it);
6815 }
6816 else
6817 {
6818 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6819
6820 /* We need to prime the bidi iterator starting at the line's or
6821 string's beginning, before we will be able to produce the
6822 next element. */
6823 if (string_p)
6824 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6825 else
6826 {
6827 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6828 -1);
6829 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6830 }
6831 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6832 do
6833 {
6834 /* Now return to buffer/string position where we were asked
6835 to get the next display element, and produce that. */
6836 bidi_move_to_visually_next (&it->bidi_it);
6837 }
6838 while (it->bidi_it.bytepos != orig_bytepos
6839 && it->bidi_it.charpos < eob);
6840 }
6841
6842 /* Adjust IT's position information to where we ended up. */
6843 if (STRINGP (it->string))
6844 {
6845 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6846 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6847 }
6848 else
6849 {
6850 IT_CHARPOS (*it) = it->bidi_it.charpos;
6851 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6852 }
6853
6854 if (STRINGP (it->string) || !it->s)
6855 {
6856 EMACS_INT stop, charpos, bytepos;
6857
6858 if (STRINGP (it->string))
6859 {
6860 xassert (!it->s);
6861 stop = SCHARS (it->string);
6862 if (stop > it->end_charpos)
6863 stop = it->end_charpos;
6864 charpos = IT_STRING_CHARPOS (*it);
6865 bytepos = IT_STRING_BYTEPOS (*it);
6866 }
6867 else
6868 {
6869 stop = it->end_charpos;
6870 charpos = IT_CHARPOS (*it);
6871 bytepos = IT_BYTEPOS (*it);
6872 }
6873 if (it->bidi_it.scan_dir < 0)
6874 stop = -1;
6875 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
6876 it->string);
6877 }
6878 }
6879
6880 /* Load IT with the next display element from Lisp string IT->string.
6881 IT->current.string_pos is the current position within the string.
6882 If IT->current.overlay_string_index >= 0, the Lisp string is an
6883 overlay string. */
6884
6885 static int
6886 next_element_from_string (struct it *it)
6887 {
6888 struct text_pos position;
6889
6890 xassert (STRINGP (it->string));
6891 xassert (!it->bidi_p || it->string == it->bidi_it.string.lstring);
6892 xassert (IT_STRING_CHARPOS (*it) >= 0);
6893 position = it->current.string_pos;
6894
6895 /* With bidi reordering, the character to display might not be the
6896 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
6897 that we were reseat()ed to a new string, whose paragraph
6898 direction is not known. */
6899 if (it->bidi_p && it->bidi_it.first_elt)
6900 {
6901 get_visually_first_element (it);
6902 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
6903 }
6904
6905 /* Time to check for invisible text? */
6906 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
6907 {
6908 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
6909 {
6910 if (!(!it->bidi_p
6911 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6912 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
6913 {
6914 /* With bidi non-linear iteration, we could find
6915 ourselves far beyond the last computed stop_charpos,
6916 with several other stop positions in between that we
6917 missed. Scan them all now, in buffer's logical
6918 order, until we find and handle the last stop_charpos
6919 that precedes our current position. */
6920 handle_stop_backwards (it, it->stop_charpos);
6921 return GET_NEXT_DISPLAY_ELEMENT (it);
6922 }
6923 else
6924 {
6925 if (it->bidi_p)
6926 {
6927 /* Take note of the stop position we just moved
6928 across, for when we will move back across it. */
6929 it->prev_stop = it->stop_charpos;
6930 /* If we are at base paragraph embedding level, take
6931 note of the last stop position seen at this
6932 level. */
6933 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6934 it->base_level_stop = it->stop_charpos;
6935 }
6936 handle_stop (it);
6937
6938 /* Since a handler may have changed IT->method, we must
6939 recurse here. */
6940 return GET_NEXT_DISPLAY_ELEMENT (it);
6941 }
6942 }
6943 else if (it->bidi_p
6944 /* If we are before prev_stop, we may have overstepped
6945 on our way backwards a stop_pos, and if so, we need
6946 to handle that stop_pos. */
6947 && IT_STRING_CHARPOS (*it) < it->prev_stop
6948 /* We can sometimes back up for reasons that have nothing
6949 to do with bidi reordering. E.g., compositions. The
6950 code below is only needed when we are above the base
6951 embedding level, so test for that explicitly. */
6952 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
6953 {
6954 /* If we lost track of base_level_stop, we have no better place
6955 for handle_stop_backwards to start from than BEGV. This
6956 happens, e.g., when we were reseated to the previous
6957 screenful of text by vertical-motion. */
6958 if (it->base_level_stop <= 0
6959 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
6960 it->base_level_stop = 0;
6961 handle_stop_backwards (it, it->base_level_stop);
6962 return GET_NEXT_DISPLAY_ELEMENT (it);
6963 }
6964 }
6965
6966 if (it->current.overlay_string_index >= 0)
6967 {
6968 /* Get the next character from an overlay string. In overlay
6969 strings, There is no field width or padding with spaces to
6970 do. */
6971 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6972 {
6973 it->what = IT_EOB;
6974 return 0;
6975 }
6976 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6977 IT_STRING_BYTEPOS (*it),
6978 it->bidi_it.scan_dir < 0
6979 ? -1
6980 : SCHARS (it->string))
6981 && next_element_from_composition (it))
6982 {
6983 return 1;
6984 }
6985 else if (STRING_MULTIBYTE (it->string))
6986 {
6987 const unsigned char *s = (SDATA (it->string)
6988 + IT_STRING_BYTEPOS (*it));
6989 it->c = string_char_and_length (s, &it->len);
6990 }
6991 else
6992 {
6993 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6994 it->len = 1;
6995 }
6996 }
6997 else
6998 {
6999 /* Get the next character from a Lisp string that is not an
7000 overlay string. Such strings come from the mode line, for
7001 example. We may have to pad with spaces, or truncate the
7002 string. See also next_element_from_c_string. */
7003 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7004 {
7005 it->what = IT_EOB;
7006 return 0;
7007 }
7008 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7009 {
7010 /* Pad with spaces. */
7011 it->c = ' ', it->len = 1;
7012 CHARPOS (position) = BYTEPOS (position) = -1;
7013 }
7014 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7015 IT_STRING_BYTEPOS (*it),
7016 it->bidi_it.scan_dir < 0
7017 ? -1
7018 : it->string_nchars)
7019 && next_element_from_composition (it))
7020 {
7021 return 1;
7022 }
7023 else if (STRING_MULTIBYTE (it->string))
7024 {
7025 const unsigned char *s = (SDATA (it->string)
7026 + IT_STRING_BYTEPOS (*it));
7027 it->c = string_char_and_length (s, &it->len);
7028 }
7029 else
7030 {
7031 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7032 it->len = 1;
7033 }
7034 }
7035
7036 /* Record what we have and where it came from. */
7037 it->what = IT_CHARACTER;
7038 it->object = it->string;
7039 it->position = position;
7040 return 1;
7041 }
7042
7043
7044 /* Load IT with next display element from C string IT->s.
7045 IT->string_nchars is the maximum number of characters to return
7046 from the string. IT->end_charpos may be greater than
7047 IT->string_nchars when this function is called, in which case we
7048 may have to return padding spaces. Value is zero if end of string
7049 reached, including padding spaces. */
7050
7051 static int
7052 next_element_from_c_string (struct it *it)
7053 {
7054 int success_p = 1;
7055
7056 xassert (it->s);
7057 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7058 it->what = IT_CHARACTER;
7059 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7060 it->object = Qnil;
7061
7062 /* With bidi reordering, the character to display might not be the
7063 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7064 we were reseated to a new string, whose paragraph direction is
7065 not known. */
7066 if (it->bidi_p && it->bidi_it.first_elt)
7067 get_visually_first_element (it);
7068
7069 /* IT's position can be greater than IT->string_nchars in case a
7070 field width or precision has been specified when the iterator was
7071 initialized. */
7072 if (IT_CHARPOS (*it) >= it->end_charpos)
7073 {
7074 /* End of the game. */
7075 it->what = IT_EOB;
7076 success_p = 0;
7077 }
7078 else if (IT_CHARPOS (*it) >= it->string_nchars)
7079 {
7080 /* Pad with spaces. */
7081 it->c = ' ', it->len = 1;
7082 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7083 }
7084 else if (it->multibyte_p)
7085 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7086 else
7087 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7088
7089 return success_p;
7090 }
7091
7092
7093 /* Set up IT to return characters from an ellipsis, if appropriate.
7094 The definition of the ellipsis glyphs may come from a display table
7095 entry. This function fills IT with the first glyph from the
7096 ellipsis if an ellipsis is to be displayed. */
7097
7098 static int
7099 next_element_from_ellipsis (struct it *it)
7100 {
7101 if (it->selective_display_ellipsis_p)
7102 setup_for_ellipsis (it, it->len);
7103 else
7104 {
7105 /* The face at the current position may be different from the
7106 face we find after the invisible text. Remember what it
7107 was in IT->saved_face_id, and signal that it's there by
7108 setting face_before_selective_p. */
7109 it->saved_face_id = it->face_id;
7110 it->method = GET_FROM_BUFFER;
7111 it->object = it->w->buffer;
7112 reseat_at_next_visible_line_start (it, 1);
7113 it->face_before_selective_p = 1;
7114 }
7115
7116 return GET_NEXT_DISPLAY_ELEMENT (it);
7117 }
7118
7119
7120 /* Deliver an image display element. The iterator IT is already
7121 filled with image information (done in handle_display_prop). Value
7122 is always 1. */
7123
7124
7125 static int
7126 next_element_from_image (struct it *it)
7127 {
7128 it->what = IT_IMAGE;
7129 it->ignore_overlay_strings_at_pos_p = 0;
7130 return 1;
7131 }
7132
7133
7134 /* Fill iterator IT with next display element from a stretch glyph
7135 property. IT->object is the value of the text property. Value is
7136 always 1. */
7137
7138 static int
7139 next_element_from_stretch (struct it *it)
7140 {
7141 it->what = IT_STRETCH;
7142 return 1;
7143 }
7144
7145 /* Scan forward from CHARPOS in the current buffer/string, until we
7146 find a stop position > current IT's position. Then handle the stop
7147 position before that. This is called when we bump into a stop
7148 position while reordering bidirectional text. CHARPOS should be
7149 the last previously processed stop_pos (or BEGV/0, if none were
7150 processed yet) whose position is less that IT's current
7151 position. */
7152
7153 static void
7154 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7155 {
7156 int bufp = !STRINGP (it->string);
7157 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7158 struct display_pos save_current = it->current;
7159 struct text_pos save_position = it->position;
7160 struct text_pos pos1;
7161 EMACS_INT next_stop;
7162
7163 /* Scan in strict logical order. */
7164 it->bidi_p = 0;
7165 do
7166 {
7167 it->prev_stop = charpos;
7168 if (bufp)
7169 {
7170 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7171 reseat_1 (it, pos1, 0);
7172 }
7173 else
7174 it->current.string_pos = string_pos (charpos, it->string);
7175 compute_stop_pos (it);
7176 /* We must advance forward, right? */
7177 if (it->stop_charpos <= it->prev_stop)
7178 abort ();
7179 charpos = it->stop_charpos;
7180 }
7181 while (charpos <= where_we_are);
7182
7183 next_stop = it->stop_charpos;
7184 it->stop_charpos = it->prev_stop;
7185 it->bidi_p = 1;
7186 it->current = save_current;
7187 it->position = save_position;
7188 handle_stop (it);
7189 it->stop_charpos = next_stop;
7190 }
7191
7192 /* Load IT with the next display element from current_buffer. Value
7193 is zero if end of buffer reached. IT->stop_charpos is the next
7194 position at which to stop and check for text properties or buffer
7195 end. */
7196
7197 static int
7198 next_element_from_buffer (struct it *it)
7199 {
7200 int success_p = 1;
7201
7202 xassert (IT_CHARPOS (*it) >= BEGV);
7203 xassert (NILP (it->string) && !it->s);
7204 xassert (!it->bidi_p
7205 || (it->bidi_it.string.lstring == Qnil
7206 && it->bidi_it.string.s == NULL));
7207
7208 /* With bidi reordering, the character to display might not be the
7209 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7210 we were reseat()ed to a new buffer position, which is potentially
7211 a different paragraph. */
7212 if (it->bidi_p && it->bidi_it.first_elt)
7213 {
7214 get_visually_first_element (it);
7215 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7216 }
7217
7218 if (IT_CHARPOS (*it) >= it->stop_charpos)
7219 {
7220 if (IT_CHARPOS (*it) >= it->end_charpos)
7221 {
7222 int overlay_strings_follow_p;
7223
7224 /* End of the game, except when overlay strings follow that
7225 haven't been returned yet. */
7226 if (it->overlay_strings_at_end_processed_p)
7227 overlay_strings_follow_p = 0;
7228 else
7229 {
7230 it->overlay_strings_at_end_processed_p = 1;
7231 overlay_strings_follow_p = get_overlay_strings (it, 0);
7232 }
7233
7234 if (overlay_strings_follow_p)
7235 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7236 else
7237 {
7238 it->what = IT_EOB;
7239 it->position = it->current.pos;
7240 success_p = 0;
7241 }
7242 }
7243 else if (!(!it->bidi_p
7244 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7245 || IT_CHARPOS (*it) == it->stop_charpos))
7246 {
7247 /* With bidi non-linear iteration, we could find ourselves
7248 far beyond the last computed stop_charpos, with several
7249 other stop positions in between that we missed. Scan
7250 them all now, in buffer's logical order, until we find
7251 and handle the last stop_charpos that precedes our
7252 current position. */
7253 handle_stop_backwards (it, it->stop_charpos);
7254 return GET_NEXT_DISPLAY_ELEMENT (it);
7255 }
7256 else
7257 {
7258 if (it->bidi_p)
7259 {
7260 /* Take note of the stop position we just moved across,
7261 for when we will move back across it. */
7262 it->prev_stop = it->stop_charpos;
7263 /* If we are at base paragraph embedding level, take
7264 note of the last stop position seen at this
7265 level. */
7266 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7267 it->base_level_stop = it->stop_charpos;
7268 }
7269 handle_stop (it);
7270 return GET_NEXT_DISPLAY_ELEMENT (it);
7271 }
7272 }
7273 else if (it->bidi_p
7274 /* If we are before prev_stop, we may have overstepped on
7275 our way backwards a stop_pos, and if so, we need to
7276 handle that stop_pos. */
7277 && IT_CHARPOS (*it) < it->prev_stop
7278 /* We can sometimes back up for reasons that have nothing
7279 to do with bidi reordering. E.g., compositions. The
7280 code below is only needed when we are above the base
7281 embedding level, so test for that explicitly. */
7282 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7283 {
7284 /* If we lost track of base_level_stop, we have no better place
7285 for handle_stop_backwards to start from than BEGV. This
7286 happens, e.g., when we were reseated to the previous
7287 screenful of text by vertical-motion. */
7288 if (it->base_level_stop <= 0
7289 || IT_CHARPOS (*it) < it->base_level_stop)
7290 it->base_level_stop = BEGV;
7291 handle_stop_backwards (it, it->base_level_stop);
7292 return GET_NEXT_DISPLAY_ELEMENT (it);
7293 }
7294 else
7295 {
7296 /* No face changes, overlays etc. in sight, so just return a
7297 character from current_buffer. */
7298 unsigned char *p;
7299 EMACS_INT stop;
7300
7301 /* Maybe run the redisplay end trigger hook. Performance note:
7302 This doesn't seem to cost measurable time. */
7303 if (it->redisplay_end_trigger_charpos
7304 && it->glyph_row
7305 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7306 run_redisplay_end_trigger_hook (it);
7307
7308 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7309 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7310 stop)
7311 && next_element_from_composition (it))
7312 {
7313 return 1;
7314 }
7315
7316 /* Get the next character, maybe multibyte. */
7317 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7318 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7319 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7320 else
7321 it->c = *p, it->len = 1;
7322
7323 /* Record what we have and where it came from. */
7324 it->what = IT_CHARACTER;
7325 it->object = it->w->buffer;
7326 it->position = it->current.pos;
7327
7328 /* Normally we return the character found above, except when we
7329 really want to return an ellipsis for selective display. */
7330 if (it->selective)
7331 {
7332 if (it->c == '\n')
7333 {
7334 /* A value of selective > 0 means hide lines indented more
7335 than that number of columns. */
7336 if (it->selective > 0
7337 && IT_CHARPOS (*it) + 1 < ZV
7338 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7339 IT_BYTEPOS (*it) + 1,
7340 (double) it->selective)) /* iftc */
7341 {
7342 success_p = next_element_from_ellipsis (it);
7343 it->dpvec_char_len = -1;
7344 }
7345 }
7346 else if (it->c == '\r' && it->selective == -1)
7347 {
7348 /* A value of selective == -1 means that everything from the
7349 CR to the end of the line is invisible, with maybe an
7350 ellipsis displayed for it. */
7351 success_p = next_element_from_ellipsis (it);
7352 it->dpvec_char_len = -1;
7353 }
7354 }
7355 }
7356
7357 /* Value is zero if end of buffer reached. */
7358 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7359 return success_p;
7360 }
7361
7362
7363 /* Run the redisplay end trigger hook for IT. */
7364
7365 static void
7366 run_redisplay_end_trigger_hook (struct it *it)
7367 {
7368 Lisp_Object args[3];
7369
7370 /* IT->glyph_row should be non-null, i.e. we should be actually
7371 displaying something, or otherwise we should not run the hook. */
7372 xassert (it->glyph_row);
7373
7374 /* Set up hook arguments. */
7375 args[0] = Qredisplay_end_trigger_functions;
7376 args[1] = it->window;
7377 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7378 it->redisplay_end_trigger_charpos = 0;
7379
7380 /* Since we are *trying* to run these functions, don't try to run
7381 them again, even if they get an error. */
7382 it->w->redisplay_end_trigger = Qnil;
7383 Frun_hook_with_args (3, args);
7384
7385 /* Notice if it changed the face of the character we are on. */
7386 handle_face_prop (it);
7387 }
7388
7389
7390 /* Deliver a composition display element. Unlike the other
7391 next_element_from_XXX, this function is not registered in the array
7392 get_next_element[]. It is called from next_element_from_buffer and
7393 next_element_from_string when necessary. */
7394
7395 static int
7396 next_element_from_composition (struct it *it)
7397 {
7398 it->what = IT_COMPOSITION;
7399 it->len = it->cmp_it.nbytes;
7400 if (STRINGP (it->string))
7401 {
7402 if (it->c < 0)
7403 {
7404 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7405 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7406 return 0;
7407 }
7408 it->position = it->current.string_pos;
7409 it->object = it->string;
7410 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7411 IT_STRING_BYTEPOS (*it), it->string);
7412 }
7413 else
7414 {
7415 if (it->c < 0)
7416 {
7417 IT_CHARPOS (*it) += it->cmp_it.nchars;
7418 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7419 if (it->bidi_p)
7420 {
7421 if (it->bidi_it.new_paragraph)
7422 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7423 /* Resync the bidi iterator with IT's new position.
7424 FIXME: this doesn't support bidirectional text. */
7425 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7426 bidi_move_to_visually_next (&it->bidi_it);
7427 }
7428 return 0;
7429 }
7430 it->position = it->current.pos;
7431 it->object = it->w->buffer;
7432 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7433 IT_BYTEPOS (*it), Qnil);
7434 }
7435 return 1;
7436 }
7437
7438
7439 \f
7440 /***********************************************************************
7441 Moving an iterator without producing glyphs
7442 ***********************************************************************/
7443
7444 /* Check if iterator is at a position corresponding to a valid buffer
7445 position after some move_it_ call. */
7446
7447 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7448 ((it)->method == GET_FROM_STRING \
7449 ? IT_STRING_CHARPOS (*it) == 0 \
7450 : 1)
7451
7452
7453 /* Move iterator IT to a specified buffer or X position within one
7454 line on the display without producing glyphs.
7455
7456 OP should be a bit mask including some or all of these bits:
7457 MOVE_TO_X: Stop upon reaching x-position TO_X.
7458 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7459 Regardless of OP's value, stop upon reaching the end of the display line.
7460
7461 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7462 This means, in particular, that TO_X includes window's horizontal
7463 scroll amount.
7464
7465 The return value has several possible values that
7466 say what condition caused the scan to stop:
7467
7468 MOVE_POS_MATCH_OR_ZV
7469 - when TO_POS or ZV was reached.
7470
7471 MOVE_X_REACHED
7472 -when TO_X was reached before TO_POS or ZV were reached.
7473
7474 MOVE_LINE_CONTINUED
7475 - when we reached the end of the display area and the line must
7476 be continued.
7477
7478 MOVE_LINE_TRUNCATED
7479 - when we reached the end of the display area and the line is
7480 truncated.
7481
7482 MOVE_NEWLINE_OR_CR
7483 - when we stopped at a line end, i.e. a newline or a CR and selective
7484 display is on. */
7485
7486 static enum move_it_result
7487 move_it_in_display_line_to (struct it *it,
7488 EMACS_INT to_charpos, int to_x,
7489 enum move_operation_enum op)
7490 {
7491 enum move_it_result result = MOVE_UNDEFINED;
7492 struct glyph_row *saved_glyph_row;
7493 struct it wrap_it, atpos_it, atx_it;
7494 int may_wrap = 0;
7495 enum it_method prev_method = it->method;
7496 EMACS_INT prev_pos = IT_CHARPOS (*it);
7497
7498 /* Don't produce glyphs in produce_glyphs. */
7499 saved_glyph_row = it->glyph_row;
7500 it->glyph_row = NULL;
7501
7502 /* Use wrap_it to save a copy of IT wherever a word wrap could
7503 occur. Use atpos_it to save a copy of IT at the desired buffer
7504 position, if found, so that we can scan ahead and check if the
7505 word later overshoots the window edge. Use atx_it similarly, for
7506 pixel positions. */
7507 wrap_it.sp = -1;
7508 atpos_it.sp = -1;
7509 atx_it.sp = -1;
7510
7511 #define BUFFER_POS_REACHED_P() \
7512 ((op & MOVE_TO_POS) != 0 \
7513 && BUFFERP (it->object) \
7514 && (IT_CHARPOS (*it) == to_charpos \
7515 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7516 && (it->method == GET_FROM_BUFFER \
7517 || (it->method == GET_FROM_DISPLAY_VECTOR \
7518 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7519
7520 /* If there's a line-/wrap-prefix, handle it. */
7521 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7522 && it->current_y < it->last_visible_y)
7523 handle_line_prefix (it);
7524
7525 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7526 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7527
7528 while (1)
7529 {
7530 int x, i, ascent = 0, descent = 0;
7531
7532 /* Utility macro to reset an iterator with x, ascent, and descent. */
7533 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7534 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7535 (IT)->max_descent = descent)
7536
7537 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7538 glyph). */
7539 if ((op & MOVE_TO_POS) != 0
7540 && BUFFERP (it->object)
7541 && it->method == GET_FROM_BUFFER
7542 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7543 || (it->bidi_p
7544 && (prev_method == GET_FROM_IMAGE
7545 || prev_method == GET_FROM_STRETCH)
7546 /* Passed TO_CHARPOS from left to right. */
7547 && ((prev_pos < to_charpos
7548 && IT_CHARPOS (*it) > to_charpos)
7549 /* Passed TO_CHARPOS from right to left. */
7550 || (prev_pos > to_charpos
7551 && IT_CHARPOS (*it) < to_charpos)))))
7552 {
7553 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7554 {
7555 result = MOVE_POS_MATCH_OR_ZV;
7556 break;
7557 }
7558 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7559 /* If wrap_it is valid, the current position might be in a
7560 word that is wrapped. So, save the iterator in
7561 atpos_it and continue to see if wrapping happens. */
7562 atpos_it = *it;
7563 }
7564
7565 prev_method = it->method;
7566 if (it->method == GET_FROM_BUFFER)
7567 prev_pos = IT_CHARPOS (*it);
7568 /* Stop when ZV reached.
7569 We used to stop here when TO_CHARPOS reached as well, but that is
7570 too soon if this glyph does not fit on this line. So we handle it
7571 explicitly below. */
7572 if (!get_next_display_element (it))
7573 {
7574 result = MOVE_POS_MATCH_OR_ZV;
7575 break;
7576 }
7577
7578 if (it->line_wrap == TRUNCATE)
7579 {
7580 if (BUFFER_POS_REACHED_P ())
7581 {
7582 result = MOVE_POS_MATCH_OR_ZV;
7583 break;
7584 }
7585 }
7586 else
7587 {
7588 if (it->line_wrap == WORD_WRAP)
7589 {
7590 if (IT_DISPLAYING_WHITESPACE (it))
7591 may_wrap = 1;
7592 else if (may_wrap)
7593 {
7594 /* We have reached a glyph that follows one or more
7595 whitespace characters. If the position is
7596 already found, we are done. */
7597 if (atpos_it.sp >= 0)
7598 {
7599 *it = atpos_it;
7600 result = MOVE_POS_MATCH_OR_ZV;
7601 goto done;
7602 }
7603 if (atx_it.sp >= 0)
7604 {
7605 *it = atx_it;
7606 result = MOVE_X_REACHED;
7607 goto done;
7608 }
7609 /* Otherwise, we can wrap here. */
7610 wrap_it = *it;
7611 may_wrap = 0;
7612 }
7613 }
7614 }
7615
7616 /* Remember the line height for the current line, in case
7617 the next element doesn't fit on the line. */
7618 ascent = it->max_ascent;
7619 descent = it->max_descent;
7620
7621 /* The call to produce_glyphs will get the metrics of the
7622 display element IT is loaded with. Record the x-position
7623 before this display element, in case it doesn't fit on the
7624 line. */
7625 x = it->current_x;
7626
7627 PRODUCE_GLYPHS (it);
7628
7629 if (it->area != TEXT_AREA)
7630 {
7631 set_iterator_to_next (it, 1);
7632 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7633 SET_TEXT_POS (this_line_min_pos,
7634 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7635 continue;
7636 }
7637
7638 /* The number of glyphs we get back in IT->nglyphs will normally
7639 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7640 character on a terminal frame, or (iii) a line end. For the
7641 second case, IT->nglyphs - 1 padding glyphs will be present.
7642 (On X frames, there is only one glyph produced for a
7643 composite character.)
7644
7645 The behavior implemented below means, for continuation lines,
7646 that as many spaces of a TAB as fit on the current line are
7647 displayed there. For terminal frames, as many glyphs of a
7648 multi-glyph character are displayed in the current line, too.
7649 This is what the old redisplay code did, and we keep it that
7650 way. Under X, the whole shape of a complex character must
7651 fit on the line or it will be completely displayed in the
7652 next line.
7653
7654 Note that both for tabs and padding glyphs, all glyphs have
7655 the same width. */
7656 if (it->nglyphs)
7657 {
7658 /* More than one glyph or glyph doesn't fit on line. All
7659 glyphs have the same width. */
7660 int single_glyph_width = it->pixel_width / it->nglyphs;
7661 int new_x;
7662 int x_before_this_char = x;
7663 int hpos_before_this_char = it->hpos;
7664
7665 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7666 {
7667 new_x = x + single_glyph_width;
7668
7669 /* We want to leave anything reaching TO_X to the caller. */
7670 if ((op & MOVE_TO_X) && new_x > to_x)
7671 {
7672 if (BUFFER_POS_REACHED_P ())
7673 {
7674 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7675 goto buffer_pos_reached;
7676 if (atpos_it.sp < 0)
7677 {
7678 atpos_it = *it;
7679 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7680 }
7681 }
7682 else
7683 {
7684 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7685 {
7686 it->current_x = x;
7687 result = MOVE_X_REACHED;
7688 break;
7689 }
7690 if (atx_it.sp < 0)
7691 {
7692 atx_it = *it;
7693 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7694 }
7695 }
7696 }
7697
7698 if (/* Lines are continued. */
7699 it->line_wrap != TRUNCATE
7700 && (/* And glyph doesn't fit on the line. */
7701 new_x > it->last_visible_x
7702 /* Or it fits exactly and we're on a window
7703 system frame. */
7704 || (new_x == it->last_visible_x
7705 && FRAME_WINDOW_P (it->f))))
7706 {
7707 if (/* IT->hpos == 0 means the very first glyph
7708 doesn't fit on the line, e.g. a wide image. */
7709 it->hpos == 0
7710 || (new_x == it->last_visible_x
7711 && FRAME_WINDOW_P (it->f)))
7712 {
7713 ++it->hpos;
7714 it->current_x = new_x;
7715
7716 /* The character's last glyph just barely fits
7717 in this row. */
7718 if (i == it->nglyphs - 1)
7719 {
7720 /* If this is the destination position,
7721 return a position *before* it in this row,
7722 now that we know it fits in this row. */
7723 if (BUFFER_POS_REACHED_P ())
7724 {
7725 if (it->line_wrap != WORD_WRAP
7726 || wrap_it.sp < 0)
7727 {
7728 it->hpos = hpos_before_this_char;
7729 it->current_x = x_before_this_char;
7730 result = MOVE_POS_MATCH_OR_ZV;
7731 break;
7732 }
7733 if (it->line_wrap == WORD_WRAP
7734 && atpos_it.sp < 0)
7735 {
7736 atpos_it = *it;
7737 atpos_it.current_x = x_before_this_char;
7738 atpos_it.hpos = hpos_before_this_char;
7739 }
7740 }
7741
7742 set_iterator_to_next (it, 1);
7743 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7744 SET_TEXT_POS (this_line_min_pos,
7745 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7746 /* On graphical terminals, newlines may
7747 "overflow" into the fringe if
7748 overflow-newline-into-fringe is non-nil.
7749 On text-only terminals, newlines may
7750 overflow into the last glyph on the
7751 display line.*/
7752 if (!FRAME_WINDOW_P (it->f)
7753 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7754 {
7755 if (!get_next_display_element (it))
7756 {
7757 result = MOVE_POS_MATCH_OR_ZV;
7758 break;
7759 }
7760 if (BUFFER_POS_REACHED_P ())
7761 {
7762 if (ITERATOR_AT_END_OF_LINE_P (it))
7763 result = MOVE_POS_MATCH_OR_ZV;
7764 else
7765 result = MOVE_LINE_CONTINUED;
7766 break;
7767 }
7768 if (ITERATOR_AT_END_OF_LINE_P (it))
7769 {
7770 result = MOVE_NEWLINE_OR_CR;
7771 break;
7772 }
7773 }
7774 }
7775 }
7776 else
7777 IT_RESET_X_ASCENT_DESCENT (it);
7778
7779 if (wrap_it.sp >= 0)
7780 {
7781 *it = wrap_it;
7782 atpos_it.sp = -1;
7783 atx_it.sp = -1;
7784 }
7785
7786 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7787 IT_CHARPOS (*it)));
7788 result = MOVE_LINE_CONTINUED;
7789 break;
7790 }
7791
7792 if (BUFFER_POS_REACHED_P ())
7793 {
7794 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7795 goto buffer_pos_reached;
7796 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7797 {
7798 atpos_it = *it;
7799 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7800 }
7801 }
7802
7803 if (new_x > it->first_visible_x)
7804 {
7805 /* Glyph is visible. Increment number of glyphs that
7806 would be displayed. */
7807 ++it->hpos;
7808 }
7809 }
7810
7811 if (result != MOVE_UNDEFINED)
7812 break;
7813 }
7814 else if (BUFFER_POS_REACHED_P ())
7815 {
7816 buffer_pos_reached:
7817 IT_RESET_X_ASCENT_DESCENT (it);
7818 result = MOVE_POS_MATCH_OR_ZV;
7819 break;
7820 }
7821 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7822 {
7823 /* Stop when TO_X specified and reached. This check is
7824 necessary here because of lines consisting of a line end,
7825 only. The line end will not produce any glyphs and we
7826 would never get MOVE_X_REACHED. */
7827 xassert (it->nglyphs == 0);
7828 result = MOVE_X_REACHED;
7829 break;
7830 }
7831
7832 /* Is this a line end? If yes, we're done. */
7833 if (ITERATOR_AT_END_OF_LINE_P (it))
7834 {
7835 result = MOVE_NEWLINE_OR_CR;
7836 break;
7837 }
7838
7839 if (it->method == GET_FROM_BUFFER)
7840 prev_pos = IT_CHARPOS (*it);
7841 /* The current display element has been consumed. Advance
7842 to the next. */
7843 set_iterator_to_next (it, 1);
7844 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7845 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7846
7847 /* Stop if lines are truncated and IT's current x-position is
7848 past the right edge of the window now. */
7849 if (it->line_wrap == TRUNCATE
7850 && it->current_x >= it->last_visible_x)
7851 {
7852 if (!FRAME_WINDOW_P (it->f)
7853 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7854 {
7855 if (!get_next_display_element (it)
7856 || BUFFER_POS_REACHED_P ())
7857 {
7858 result = MOVE_POS_MATCH_OR_ZV;
7859 break;
7860 }
7861 if (ITERATOR_AT_END_OF_LINE_P (it))
7862 {
7863 result = MOVE_NEWLINE_OR_CR;
7864 break;
7865 }
7866 }
7867 result = MOVE_LINE_TRUNCATED;
7868 break;
7869 }
7870 #undef IT_RESET_X_ASCENT_DESCENT
7871 }
7872
7873 #undef BUFFER_POS_REACHED_P
7874
7875 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7876 restore the saved iterator. */
7877 if (atpos_it.sp >= 0)
7878 *it = atpos_it;
7879 else if (atx_it.sp >= 0)
7880 *it = atx_it;
7881
7882 done:
7883
7884 /* Restore the iterator settings altered at the beginning of this
7885 function. */
7886 it->glyph_row = saved_glyph_row;
7887 return result;
7888 }
7889
7890 /* For external use. */
7891 void
7892 move_it_in_display_line (struct it *it,
7893 EMACS_INT to_charpos, int to_x,
7894 enum move_operation_enum op)
7895 {
7896 if (it->line_wrap == WORD_WRAP
7897 && (op & MOVE_TO_X))
7898 {
7899 struct it save_it = *it;
7900 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7901 /* When word-wrap is on, TO_X may lie past the end
7902 of a wrapped line. Then it->current is the
7903 character on the next line, so backtrack to the
7904 space before the wrap point. */
7905 if (skip == MOVE_LINE_CONTINUED)
7906 {
7907 int prev_x = max (it->current_x - 1, 0);
7908 *it = save_it;
7909 move_it_in_display_line_to
7910 (it, -1, prev_x, MOVE_TO_X);
7911 }
7912 }
7913 else
7914 move_it_in_display_line_to (it, to_charpos, to_x, op);
7915 }
7916
7917
7918 /* Move IT forward until it satisfies one or more of the criteria in
7919 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7920
7921 OP is a bit-mask that specifies where to stop, and in particular,
7922 which of those four position arguments makes a difference. See the
7923 description of enum move_operation_enum.
7924
7925 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7926 screen line, this function will set IT to the next position >
7927 TO_CHARPOS. */
7928
7929 void
7930 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7931 {
7932 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7933 int line_height, line_start_x = 0, reached = 0;
7934
7935 for (;;)
7936 {
7937 if (op & MOVE_TO_VPOS)
7938 {
7939 /* If no TO_CHARPOS and no TO_X specified, stop at the
7940 start of the line TO_VPOS. */
7941 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7942 {
7943 if (it->vpos == to_vpos)
7944 {
7945 reached = 1;
7946 break;
7947 }
7948 else
7949 skip = move_it_in_display_line_to (it, -1, -1, 0);
7950 }
7951 else
7952 {
7953 /* TO_VPOS >= 0 means stop at TO_X in the line at
7954 TO_VPOS, or at TO_POS, whichever comes first. */
7955 if (it->vpos == to_vpos)
7956 {
7957 reached = 2;
7958 break;
7959 }
7960
7961 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7962
7963 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7964 {
7965 reached = 3;
7966 break;
7967 }
7968 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7969 {
7970 /* We have reached TO_X but not in the line we want. */
7971 skip = move_it_in_display_line_to (it, to_charpos,
7972 -1, MOVE_TO_POS);
7973 if (skip == MOVE_POS_MATCH_OR_ZV)
7974 {
7975 reached = 4;
7976 break;
7977 }
7978 }
7979 }
7980 }
7981 else if (op & MOVE_TO_Y)
7982 {
7983 struct it it_backup;
7984
7985 if (it->line_wrap == WORD_WRAP)
7986 it_backup = *it;
7987
7988 /* TO_Y specified means stop at TO_X in the line containing
7989 TO_Y---or at TO_CHARPOS if this is reached first. The
7990 problem is that we can't really tell whether the line
7991 contains TO_Y before we have completely scanned it, and
7992 this may skip past TO_X. What we do is to first scan to
7993 TO_X.
7994
7995 If TO_X is not specified, use a TO_X of zero. The reason
7996 is to make the outcome of this function more predictable.
7997 If we didn't use TO_X == 0, we would stop at the end of
7998 the line which is probably not what a caller would expect
7999 to happen. */
8000 skip = move_it_in_display_line_to
8001 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8002 (MOVE_TO_X | (op & MOVE_TO_POS)));
8003
8004 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8005 if (skip == MOVE_POS_MATCH_OR_ZV)
8006 reached = 5;
8007 else if (skip == MOVE_X_REACHED)
8008 {
8009 /* If TO_X was reached, we want to know whether TO_Y is
8010 in the line. We know this is the case if the already
8011 scanned glyphs make the line tall enough. Otherwise,
8012 we must check by scanning the rest of the line. */
8013 line_height = it->max_ascent + it->max_descent;
8014 if (to_y >= it->current_y
8015 && to_y < it->current_y + line_height)
8016 {
8017 reached = 6;
8018 break;
8019 }
8020 it_backup = *it;
8021 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8022 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8023 op & MOVE_TO_POS);
8024 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8025 line_height = it->max_ascent + it->max_descent;
8026 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8027
8028 if (to_y >= it->current_y
8029 && to_y < it->current_y + line_height)
8030 {
8031 /* If TO_Y is in this line and TO_X was reached
8032 above, we scanned too far. We have to restore
8033 IT's settings to the ones before skipping. */
8034 *it = it_backup;
8035 reached = 6;
8036 }
8037 else
8038 {
8039 skip = skip2;
8040 if (skip == MOVE_POS_MATCH_OR_ZV)
8041 reached = 7;
8042 }
8043 }
8044 else
8045 {
8046 /* Check whether TO_Y is in this line. */
8047 line_height = it->max_ascent + it->max_descent;
8048 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8049
8050 if (to_y >= it->current_y
8051 && to_y < it->current_y + line_height)
8052 {
8053 /* When word-wrap is on, TO_X may lie past the end
8054 of a wrapped line. Then it->current is the
8055 character on the next line, so backtrack to the
8056 space before the wrap point. */
8057 if (skip == MOVE_LINE_CONTINUED
8058 && it->line_wrap == WORD_WRAP)
8059 {
8060 int prev_x = max (it->current_x - 1, 0);
8061 *it = it_backup;
8062 skip = move_it_in_display_line_to
8063 (it, -1, prev_x, MOVE_TO_X);
8064 }
8065 reached = 6;
8066 }
8067 }
8068
8069 if (reached)
8070 break;
8071 }
8072 else if (BUFFERP (it->object)
8073 && (it->method == GET_FROM_BUFFER
8074 || it->method == GET_FROM_STRETCH)
8075 && IT_CHARPOS (*it) >= to_charpos)
8076 skip = MOVE_POS_MATCH_OR_ZV;
8077 else
8078 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8079
8080 switch (skip)
8081 {
8082 case MOVE_POS_MATCH_OR_ZV:
8083 reached = 8;
8084 goto out;
8085
8086 case MOVE_NEWLINE_OR_CR:
8087 set_iterator_to_next (it, 1);
8088 it->continuation_lines_width = 0;
8089 break;
8090
8091 case MOVE_LINE_TRUNCATED:
8092 it->continuation_lines_width = 0;
8093 reseat_at_next_visible_line_start (it, 0);
8094 if ((op & MOVE_TO_POS) != 0
8095 && IT_CHARPOS (*it) > to_charpos)
8096 {
8097 reached = 9;
8098 goto out;
8099 }
8100 break;
8101
8102 case MOVE_LINE_CONTINUED:
8103 /* For continued lines ending in a tab, some of the glyphs
8104 associated with the tab are displayed on the current
8105 line. Since it->current_x does not include these glyphs,
8106 we use it->last_visible_x instead. */
8107 if (it->c == '\t')
8108 {
8109 it->continuation_lines_width += it->last_visible_x;
8110 /* When moving by vpos, ensure that the iterator really
8111 advances to the next line (bug#847, bug#969). Fixme:
8112 do we need to do this in other circumstances? */
8113 if (it->current_x != it->last_visible_x
8114 && (op & MOVE_TO_VPOS)
8115 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8116 {
8117 line_start_x = it->current_x + it->pixel_width
8118 - it->last_visible_x;
8119 set_iterator_to_next (it, 0);
8120 }
8121 }
8122 else
8123 it->continuation_lines_width += it->current_x;
8124 break;
8125
8126 default:
8127 abort ();
8128 }
8129
8130 /* Reset/increment for the next run. */
8131 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8132 it->current_x = line_start_x;
8133 line_start_x = 0;
8134 it->hpos = 0;
8135 it->current_y += it->max_ascent + it->max_descent;
8136 ++it->vpos;
8137 last_height = it->max_ascent + it->max_descent;
8138 last_max_ascent = it->max_ascent;
8139 it->max_ascent = it->max_descent = 0;
8140 }
8141
8142 out:
8143
8144 /* On text terminals, we may stop at the end of a line in the middle
8145 of a multi-character glyph. If the glyph itself is continued,
8146 i.e. it is actually displayed on the next line, don't treat this
8147 stopping point as valid; move to the next line instead (unless
8148 that brings us offscreen). */
8149 if (!FRAME_WINDOW_P (it->f)
8150 && op & MOVE_TO_POS
8151 && IT_CHARPOS (*it) == to_charpos
8152 && it->what == IT_CHARACTER
8153 && it->nglyphs > 1
8154 && it->line_wrap == WINDOW_WRAP
8155 && it->current_x == it->last_visible_x - 1
8156 && it->c != '\n'
8157 && it->c != '\t'
8158 && it->vpos < XFASTINT (it->w->window_end_vpos))
8159 {
8160 it->continuation_lines_width += it->current_x;
8161 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8162 it->current_y += it->max_ascent + it->max_descent;
8163 ++it->vpos;
8164 last_height = it->max_ascent + it->max_descent;
8165 last_max_ascent = it->max_ascent;
8166 }
8167
8168 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8169 }
8170
8171
8172 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8173
8174 If DY > 0, move IT backward at least that many pixels. DY = 0
8175 means move IT backward to the preceding line start or BEGV. This
8176 function may move over more than DY pixels if IT->current_y - DY
8177 ends up in the middle of a line; in this case IT->current_y will be
8178 set to the top of the line moved to. */
8179
8180 void
8181 move_it_vertically_backward (struct it *it, int dy)
8182 {
8183 int nlines, h;
8184 struct it it2, it3;
8185 EMACS_INT start_pos;
8186
8187 move_further_back:
8188 xassert (dy >= 0);
8189
8190 start_pos = IT_CHARPOS (*it);
8191
8192 /* Estimate how many newlines we must move back. */
8193 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8194
8195 /* Set the iterator's position that many lines back. */
8196 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8197 back_to_previous_visible_line_start (it);
8198
8199 /* Reseat the iterator here. When moving backward, we don't want
8200 reseat to skip forward over invisible text, set up the iterator
8201 to deliver from overlay strings at the new position etc. So,
8202 use reseat_1 here. */
8203 reseat_1 (it, it->current.pos, 1);
8204
8205 /* We are now surely at a line start. */
8206 it->current_x = it->hpos = 0;
8207 it->continuation_lines_width = 0;
8208
8209 /* Move forward and see what y-distance we moved. First move to the
8210 start of the next line so that we get its height. We need this
8211 height to be able to tell whether we reached the specified
8212 y-distance. */
8213 it2 = *it;
8214 it2.max_ascent = it2.max_descent = 0;
8215 do
8216 {
8217 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8218 MOVE_TO_POS | MOVE_TO_VPOS);
8219 }
8220 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8221 xassert (IT_CHARPOS (*it) >= BEGV);
8222 it3 = it2;
8223
8224 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8225 xassert (IT_CHARPOS (*it) >= BEGV);
8226 /* H is the actual vertical distance from the position in *IT
8227 and the starting position. */
8228 h = it2.current_y - it->current_y;
8229 /* NLINES is the distance in number of lines. */
8230 nlines = it2.vpos - it->vpos;
8231
8232 /* Correct IT's y and vpos position
8233 so that they are relative to the starting point. */
8234 it->vpos -= nlines;
8235 it->current_y -= h;
8236
8237 if (dy == 0)
8238 {
8239 /* DY == 0 means move to the start of the screen line. The
8240 value of nlines is > 0 if continuation lines were involved. */
8241 if (nlines > 0)
8242 move_it_by_lines (it, nlines);
8243 }
8244 else
8245 {
8246 /* The y-position we try to reach, relative to *IT.
8247 Note that H has been subtracted in front of the if-statement. */
8248 int target_y = it->current_y + h - dy;
8249 int y0 = it3.current_y;
8250 int y1 = line_bottom_y (&it3);
8251 int line_height = y1 - y0;
8252
8253 /* If we did not reach target_y, try to move further backward if
8254 we can. If we moved too far backward, try to move forward. */
8255 if (target_y < it->current_y
8256 /* This is heuristic. In a window that's 3 lines high, with
8257 a line height of 13 pixels each, recentering with point
8258 on the bottom line will try to move -39/2 = 19 pixels
8259 backward. Try to avoid moving into the first line. */
8260 && (it->current_y - target_y
8261 > min (window_box_height (it->w), line_height * 2 / 3))
8262 && IT_CHARPOS (*it) > BEGV)
8263 {
8264 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8265 target_y - it->current_y));
8266 dy = it->current_y - target_y;
8267 goto move_further_back;
8268 }
8269 else if (target_y >= it->current_y + line_height
8270 && IT_CHARPOS (*it) < ZV)
8271 {
8272 /* Should move forward by at least one line, maybe more.
8273
8274 Note: Calling move_it_by_lines can be expensive on
8275 terminal frames, where compute_motion is used (via
8276 vmotion) to do the job, when there are very long lines
8277 and truncate-lines is nil. That's the reason for
8278 treating terminal frames specially here. */
8279
8280 if (!FRAME_WINDOW_P (it->f))
8281 move_it_vertically (it, target_y - (it->current_y + line_height));
8282 else
8283 {
8284 do
8285 {
8286 move_it_by_lines (it, 1);
8287 }
8288 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8289 }
8290 }
8291 }
8292 }
8293
8294
8295 /* Move IT by a specified amount of pixel lines DY. DY negative means
8296 move backwards. DY = 0 means move to start of screen line. At the
8297 end, IT will be on the start of a screen line. */
8298
8299 void
8300 move_it_vertically (struct it *it, int dy)
8301 {
8302 if (dy <= 0)
8303 move_it_vertically_backward (it, -dy);
8304 else
8305 {
8306 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8307 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8308 MOVE_TO_POS | MOVE_TO_Y);
8309 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8310
8311 /* If buffer ends in ZV without a newline, move to the start of
8312 the line to satisfy the post-condition. */
8313 if (IT_CHARPOS (*it) == ZV
8314 && ZV > BEGV
8315 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8316 move_it_by_lines (it, 0);
8317 }
8318 }
8319
8320
8321 /* Move iterator IT past the end of the text line it is in. */
8322
8323 void
8324 move_it_past_eol (struct it *it)
8325 {
8326 enum move_it_result rc;
8327
8328 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8329 if (rc == MOVE_NEWLINE_OR_CR)
8330 set_iterator_to_next (it, 0);
8331 }
8332
8333
8334 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8335 negative means move up. DVPOS == 0 means move to the start of the
8336 screen line.
8337
8338 Optimization idea: If we would know that IT->f doesn't use
8339 a face with proportional font, we could be faster for
8340 truncate-lines nil. */
8341
8342 void
8343 move_it_by_lines (struct it *it, int dvpos)
8344 {
8345
8346 /* The commented-out optimization uses vmotion on terminals. This
8347 gives bad results, because elements like it->what, on which
8348 callers such as pos_visible_p rely, aren't updated. */
8349 /* struct position pos;
8350 if (!FRAME_WINDOW_P (it->f))
8351 {
8352 struct text_pos textpos;
8353
8354 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8355 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8356 reseat (it, textpos, 1);
8357 it->vpos += pos.vpos;
8358 it->current_y += pos.vpos;
8359 }
8360 else */
8361
8362 if (dvpos == 0)
8363 {
8364 /* DVPOS == 0 means move to the start of the screen line. */
8365 move_it_vertically_backward (it, 0);
8366 xassert (it->current_x == 0 && it->hpos == 0);
8367 /* Let next call to line_bottom_y calculate real line height */
8368 last_height = 0;
8369 }
8370 else if (dvpos > 0)
8371 {
8372 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8373 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8374 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8375 }
8376 else
8377 {
8378 struct it it2;
8379 EMACS_INT start_charpos, i;
8380
8381 /* Start at the beginning of the screen line containing IT's
8382 position. This may actually move vertically backwards,
8383 in case of overlays, so adjust dvpos accordingly. */
8384 dvpos += it->vpos;
8385 move_it_vertically_backward (it, 0);
8386 dvpos -= it->vpos;
8387
8388 /* Go back -DVPOS visible lines and reseat the iterator there. */
8389 start_charpos = IT_CHARPOS (*it);
8390 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8391 back_to_previous_visible_line_start (it);
8392 reseat (it, it->current.pos, 1);
8393
8394 /* Move further back if we end up in a string or an image. */
8395 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8396 {
8397 /* First try to move to start of display line. */
8398 dvpos += it->vpos;
8399 move_it_vertically_backward (it, 0);
8400 dvpos -= it->vpos;
8401 if (IT_POS_VALID_AFTER_MOVE_P (it))
8402 break;
8403 /* If start of line is still in string or image,
8404 move further back. */
8405 back_to_previous_visible_line_start (it);
8406 reseat (it, it->current.pos, 1);
8407 dvpos--;
8408 }
8409
8410 it->current_x = it->hpos = 0;
8411
8412 /* Above call may have moved too far if continuation lines
8413 are involved. Scan forward and see if it did. */
8414 it2 = *it;
8415 it2.vpos = it2.current_y = 0;
8416 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8417 it->vpos -= it2.vpos;
8418 it->current_y -= it2.current_y;
8419 it->current_x = it->hpos = 0;
8420
8421 /* If we moved too far back, move IT some lines forward. */
8422 if (it2.vpos > -dvpos)
8423 {
8424 int delta = it2.vpos + dvpos;
8425 it2 = *it;
8426 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8427 /* Move back again if we got too far ahead. */
8428 if (IT_CHARPOS (*it) >= start_charpos)
8429 *it = it2;
8430 }
8431 }
8432 }
8433
8434 /* Return 1 if IT points into the middle of a display vector. */
8435
8436 int
8437 in_display_vector_p (struct it *it)
8438 {
8439 return (it->method == GET_FROM_DISPLAY_VECTOR
8440 && it->current.dpvec_index > 0
8441 && it->dpvec + it->current.dpvec_index != it->dpend);
8442 }
8443
8444 \f
8445 /***********************************************************************
8446 Messages
8447 ***********************************************************************/
8448
8449
8450 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8451 to *Messages*. */
8452
8453 void
8454 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8455 {
8456 Lisp_Object args[3];
8457 Lisp_Object msg, fmt;
8458 char *buffer;
8459 EMACS_INT len;
8460 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8461 USE_SAFE_ALLOCA;
8462
8463 /* Do nothing if called asynchronously. Inserting text into
8464 a buffer may call after-change-functions and alike and
8465 that would means running Lisp asynchronously. */
8466 if (handling_signal)
8467 return;
8468
8469 fmt = msg = Qnil;
8470 GCPRO4 (fmt, msg, arg1, arg2);
8471
8472 args[0] = fmt = build_string (format);
8473 args[1] = arg1;
8474 args[2] = arg2;
8475 msg = Fformat (3, args);
8476
8477 len = SBYTES (msg) + 1;
8478 SAFE_ALLOCA (buffer, char *, len);
8479 memcpy (buffer, SDATA (msg), len);
8480
8481 message_dolog (buffer, len - 1, 1, 0);
8482 SAFE_FREE ();
8483
8484 UNGCPRO;
8485 }
8486
8487
8488 /* Output a newline in the *Messages* buffer if "needs" one. */
8489
8490 void
8491 message_log_maybe_newline (void)
8492 {
8493 if (message_log_need_newline)
8494 message_dolog ("", 0, 1, 0);
8495 }
8496
8497
8498 /* Add a string M of length NBYTES to the message log, optionally
8499 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8500 nonzero, means interpret the contents of M as multibyte. This
8501 function calls low-level routines in order to bypass text property
8502 hooks, etc. which might not be safe to run.
8503
8504 This may GC (insert may run before/after change hooks),
8505 so the buffer M must NOT point to a Lisp string. */
8506
8507 void
8508 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8509 {
8510 const unsigned char *msg = (const unsigned char *) m;
8511
8512 if (!NILP (Vmemory_full))
8513 return;
8514
8515 if (!NILP (Vmessage_log_max))
8516 {
8517 struct buffer *oldbuf;
8518 Lisp_Object oldpoint, oldbegv, oldzv;
8519 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8520 EMACS_INT point_at_end = 0;
8521 EMACS_INT zv_at_end = 0;
8522 Lisp_Object old_deactivate_mark, tem;
8523 struct gcpro gcpro1;
8524
8525 old_deactivate_mark = Vdeactivate_mark;
8526 oldbuf = current_buffer;
8527 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8528 BVAR (current_buffer, undo_list) = Qt;
8529
8530 oldpoint = message_dolog_marker1;
8531 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8532 oldbegv = message_dolog_marker2;
8533 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8534 oldzv = message_dolog_marker3;
8535 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8536 GCPRO1 (old_deactivate_mark);
8537
8538 if (PT == Z)
8539 point_at_end = 1;
8540 if (ZV == Z)
8541 zv_at_end = 1;
8542
8543 BEGV = BEG;
8544 BEGV_BYTE = BEG_BYTE;
8545 ZV = Z;
8546 ZV_BYTE = Z_BYTE;
8547 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8548
8549 /* Insert the string--maybe converting multibyte to single byte
8550 or vice versa, so that all the text fits the buffer. */
8551 if (multibyte
8552 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8553 {
8554 EMACS_INT i;
8555 int c, char_bytes;
8556 char work[1];
8557
8558 /* Convert a multibyte string to single-byte
8559 for the *Message* buffer. */
8560 for (i = 0; i < nbytes; i += char_bytes)
8561 {
8562 c = string_char_and_length (msg + i, &char_bytes);
8563 work[0] = (ASCII_CHAR_P (c)
8564 ? c
8565 : multibyte_char_to_unibyte (c));
8566 insert_1_both (work, 1, 1, 1, 0, 0);
8567 }
8568 }
8569 else if (! multibyte
8570 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8571 {
8572 EMACS_INT i;
8573 int c, char_bytes;
8574 unsigned char str[MAX_MULTIBYTE_LENGTH];
8575 /* Convert a single-byte string to multibyte
8576 for the *Message* buffer. */
8577 for (i = 0; i < nbytes; i++)
8578 {
8579 c = msg[i];
8580 MAKE_CHAR_MULTIBYTE (c);
8581 char_bytes = CHAR_STRING (c, str);
8582 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8583 }
8584 }
8585 else if (nbytes)
8586 insert_1 (m, nbytes, 1, 0, 0);
8587
8588 if (nlflag)
8589 {
8590 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8591 unsigned long int dups;
8592 insert_1 ("\n", 1, 1, 0, 0);
8593
8594 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8595 this_bol = PT;
8596 this_bol_byte = PT_BYTE;
8597
8598 /* See if this line duplicates the previous one.
8599 If so, combine duplicates. */
8600 if (this_bol > BEG)
8601 {
8602 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8603 prev_bol = PT;
8604 prev_bol_byte = PT_BYTE;
8605
8606 dups = message_log_check_duplicate (prev_bol_byte,
8607 this_bol_byte);
8608 if (dups)
8609 {
8610 del_range_both (prev_bol, prev_bol_byte,
8611 this_bol, this_bol_byte, 0);
8612 if (dups > 1)
8613 {
8614 char dupstr[40];
8615 int duplen;
8616
8617 /* If you change this format, don't forget to also
8618 change message_log_check_duplicate. */
8619 sprintf (dupstr, " [%lu times]", dups);
8620 duplen = strlen (dupstr);
8621 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8622 insert_1 (dupstr, duplen, 1, 0, 1);
8623 }
8624 }
8625 }
8626
8627 /* If we have more than the desired maximum number of lines
8628 in the *Messages* buffer now, delete the oldest ones.
8629 This is safe because we don't have undo in this buffer. */
8630
8631 if (NATNUMP (Vmessage_log_max))
8632 {
8633 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8634 -XFASTINT (Vmessage_log_max) - 1, 0);
8635 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8636 }
8637 }
8638 BEGV = XMARKER (oldbegv)->charpos;
8639 BEGV_BYTE = marker_byte_position (oldbegv);
8640
8641 if (zv_at_end)
8642 {
8643 ZV = Z;
8644 ZV_BYTE = Z_BYTE;
8645 }
8646 else
8647 {
8648 ZV = XMARKER (oldzv)->charpos;
8649 ZV_BYTE = marker_byte_position (oldzv);
8650 }
8651
8652 if (point_at_end)
8653 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8654 else
8655 /* We can't do Fgoto_char (oldpoint) because it will run some
8656 Lisp code. */
8657 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8658 XMARKER (oldpoint)->bytepos);
8659
8660 UNGCPRO;
8661 unchain_marker (XMARKER (oldpoint));
8662 unchain_marker (XMARKER (oldbegv));
8663 unchain_marker (XMARKER (oldzv));
8664
8665 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8666 set_buffer_internal (oldbuf);
8667 if (NILP (tem))
8668 windows_or_buffers_changed = old_windows_or_buffers_changed;
8669 message_log_need_newline = !nlflag;
8670 Vdeactivate_mark = old_deactivate_mark;
8671 }
8672 }
8673
8674
8675 /* We are at the end of the buffer after just having inserted a newline.
8676 (Note: We depend on the fact we won't be crossing the gap.)
8677 Check to see if the most recent message looks a lot like the previous one.
8678 Return 0 if different, 1 if the new one should just replace it, or a
8679 value N > 1 if we should also append " [N times]". */
8680
8681 static unsigned long int
8682 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8683 {
8684 EMACS_INT i;
8685 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8686 int seen_dots = 0;
8687 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8688 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8689
8690 for (i = 0; i < len; i++)
8691 {
8692 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8693 seen_dots = 1;
8694 if (p1[i] != p2[i])
8695 return seen_dots;
8696 }
8697 p1 += len;
8698 if (*p1 == '\n')
8699 return 2;
8700 if (*p1++ == ' ' && *p1++ == '[')
8701 {
8702 char *pend;
8703 unsigned long int n = strtoul ((char *) p1, &pend, 10);
8704 if (strncmp (pend, " times]\n", 8) == 0)
8705 return n+1;
8706 }
8707 return 0;
8708 }
8709 \f
8710
8711 /* Display an echo area message M with a specified length of NBYTES
8712 bytes. The string may include null characters. If M is 0, clear
8713 out any existing message, and let the mini-buffer text show
8714 through.
8715
8716 This may GC, so the buffer M must NOT point to a Lisp string. */
8717
8718 void
8719 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8720 {
8721 /* First flush out any partial line written with print. */
8722 message_log_maybe_newline ();
8723 if (m)
8724 message_dolog (m, nbytes, 1, multibyte);
8725 message2_nolog (m, nbytes, multibyte);
8726 }
8727
8728
8729 /* The non-logging counterpart of message2. */
8730
8731 void
8732 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8733 {
8734 struct frame *sf = SELECTED_FRAME ();
8735 message_enable_multibyte = multibyte;
8736
8737 if (FRAME_INITIAL_P (sf))
8738 {
8739 if (noninteractive_need_newline)
8740 putc ('\n', stderr);
8741 noninteractive_need_newline = 0;
8742 if (m)
8743 fwrite (m, nbytes, 1, stderr);
8744 if (cursor_in_echo_area == 0)
8745 fprintf (stderr, "\n");
8746 fflush (stderr);
8747 }
8748 /* A null message buffer means that the frame hasn't really been
8749 initialized yet. Error messages get reported properly by
8750 cmd_error, so this must be just an informative message; toss it. */
8751 else if (INTERACTIVE
8752 && sf->glyphs_initialized_p
8753 && FRAME_MESSAGE_BUF (sf))
8754 {
8755 Lisp_Object mini_window;
8756 struct frame *f;
8757
8758 /* Get the frame containing the mini-buffer
8759 that the selected frame is using. */
8760 mini_window = FRAME_MINIBUF_WINDOW (sf);
8761 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8762
8763 FRAME_SAMPLE_VISIBILITY (f);
8764 if (FRAME_VISIBLE_P (sf)
8765 && ! FRAME_VISIBLE_P (f))
8766 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8767
8768 if (m)
8769 {
8770 set_message (m, Qnil, nbytes, multibyte);
8771 if (minibuffer_auto_raise)
8772 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8773 }
8774 else
8775 clear_message (1, 1);
8776
8777 do_pending_window_change (0);
8778 echo_area_display (1);
8779 do_pending_window_change (0);
8780 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8781 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8782 }
8783 }
8784
8785
8786 /* Display an echo area message M with a specified length of NBYTES
8787 bytes. The string may include null characters. If M is not a
8788 string, clear out any existing message, and let the mini-buffer
8789 text show through.
8790
8791 This function cancels echoing. */
8792
8793 void
8794 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8795 {
8796 struct gcpro gcpro1;
8797
8798 GCPRO1 (m);
8799 clear_message (1,1);
8800 cancel_echoing ();
8801
8802 /* First flush out any partial line written with print. */
8803 message_log_maybe_newline ();
8804 if (STRINGP (m))
8805 {
8806 char *buffer;
8807 USE_SAFE_ALLOCA;
8808
8809 SAFE_ALLOCA (buffer, char *, nbytes);
8810 memcpy (buffer, SDATA (m), nbytes);
8811 message_dolog (buffer, nbytes, 1, multibyte);
8812 SAFE_FREE ();
8813 }
8814 message3_nolog (m, nbytes, multibyte);
8815
8816 UNGCPRO;
8817 }
8818
8819
8820 /* The non-logging version of message3.
8821 This does not cancel echoing, because it is used for echoing.
8822 Perhaps we need to make a separate function for echoing
8823 and make this cancel echoing. */
8824
8825 void
8826 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8827 {
8828 struct frame *sf = SELECTED_FRAME ();
8829 message_enable_multibyte = multibyte;
8830
8831 if (FRAME_INITIAL_P (sf))
8832 {
8833 if (noninteractive_need_newline)
8834 putc ('\n', stderr);
8835 noninteractive_need_newline = 0;
8836 if (STRINGP (m))
8837 fwrite (SDATA (m), nbytes, 1, stderr);
8838 if (cursor_in_echo_area == 0)
8839 fprintf (stderr, "\n");
8840 fflush (stderr);
8841 }
8842 /* A null message buffer means that the frame hasn't really been
8843 initialized yet. Error messages get reported properly by
8844 cmd_error, so this must be just an informative message; toss it. */
8845 else if (INTERACTIVE
8846 && sf->glyphs_initialized_p
8847 && FRAME_MESSAGE_BUF (sf))
8848 {
8849 Lisp_Object mini_window;
8850 Lisp_Object frame;
8851 struct frame *f;
8852
8853 /* Get the frame containing the mini-buffer
8854 that the selected frame is using. */
8855 mini_window = FRAME_MINIBUF_WINDOW (sf);
8856 frame = XWINDOW (mini_window)->frame;
8857 f = XFRAME (frame);
8858
8859 FRAME_SAMPLE_VISIBILITY (f);
8860 if (FRAME_VISIBLE_P (sf)
8861 && !FRAME_VISIBLE_P (f))
8862 Fmake_frame_visible (frame);
8863
8864 if (STRINGP (m) && SCHARS (m) > 0)
8865 {
8866 set_message (NULL, m, nbytes, multibyte);
8867 if (minibuffer_auto_raise)
8868 Fraise_frame (frame);
8869 /* Assume we are not echoing.
8870 (If we are, echo_now will override this.) */
8871 echo_message_buffer = Qnil;
8872 }
8873 else
8874 clear_message (1, 1);
8875
8876 do_pending_window_change (0);
8877 echo_area_display (1);
8878 do_pending_window_change (0);
8879 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8880 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8881 }
8882 }
8883
8884
8885 /* Display a null-terminated echo area message M. If M is 0, clear
8886 out any existing message, and let the mini-buffer text show through.
8887
8888 The buffer M must continue to exist until after the echo area gets
8889 cleared or some other message gets displayed there. Do not pass
8890 text that is stored in a Lisp string. Do not pass text in a buffer
8891 that was alloca'd. */
8892
8893 void
8894 message1 (const char *m)
8895 {
8896 message2 (m, (m ? strlen (m) : 0), 0);
8897 }
8898
8899
8900 /* The non-logging counterpart of message1. */
8901
8902 void
8903 message1_nolog (const char *m)
8904 {
8905 message2_nolog (m, (m ? strlen (m) : 0), 0);
8906 }
8907
8908 /* Display a message M which contains a single %s
8909 which gets replaced with STRING. */
8910
8911 void
8912 message_with_string (const char *m, Lisp_Object string, int log)
8913 {
8914 CHECK_STRING (string);
8915
8916 if (noninteractive)
8917 {
8918 if (m)
8919 {
8920 if (noninteractive_need_newline)
8921 putc ('\n', stderr);
8922 noninteractive_need_newline = 0;
8923 fprintf (stderr, m, SDATA (string));
8924 if (!cursor_in_echo_area)
8925 fprintf (stderr, "\n");
8926 fflush (stderr);
8927 }
8928 }
8929 else if (INTERACTIVE)
8930 {
8931 /* The frame whose minibuffer we're going to display the message on.
8932 It may be larger than the selected frame, so we need
8933 to use its buffer, not the selected frame's buffer. */
8934 Lisp_Object mini_window;
8935 struct frame *f, *sf = SELECTED_FRAME ();
8936
8937 /* Get the frame containing the minibuffer
8938 that the selected frame is using. */
8939 mini_window = FRAME_MINIBUF_WINDOW (sf);
8940 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8941
8942 /* A null message buffer means that the frame hasn't really been
8943 initialized yet. Error messages get reported properly by
8944 cmd_error, so this must be just an informative message; toss it. */
8945 if (FRAME_MESSAGE_BUF (f))
8946 {
8947 Lisp_Object args[2], msg;
8948 struct gcpro gcpro1, gcpro2;
8949
8950 args[0] = build_string (m);
8951 args[1] = msg = string;
8952 GCPRO2 (args[0], msg);
8953 gcpro1.nvars = 2;
8954
8955 msg = Fformat (2, args);
8956
8957 if (log)
8958 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8959 else
8960 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8961
8962 UNGCPRO;
8963
8964 /* Print should start at the beginning of the message
8965 buffer next time. */
8966 message_buf_print = 0;
8967 }
8968 }
8969 }
8970
8971
8972 /* Dump an informative message to the minibuf. If M is 0, clear out
8973 any existing message, and let the mini-buffer text show through. */
8974
8975 static void
8976 vmessage (const char *m, va_list ap)
8977 {
8978 if (noninteractive)
8979 {
8980 if (m)
8981 {
8982 if (noninteractive_need_newline)
8983 putc ('\n', stderr);
8984 noninteractive_need_newline = 0;
8985 vfprintf (stderr, m, ap);
8986 if (cursor_in_echo_area == 0)
8987 fprintf (stderr, "\n");
8988 fflush (stderr);
8989 }
8990 }
8991 else if (INTERACTIVE)
8992 {
8993 /* The frame whose mini-buffer we're going to display the message
8994 on. It may be larger than the selected frame, so we need to
8995 use its buffer, not the selected frame's buffer. */
8996 Lisp_Object mini_window;
8997 struct frame *f, *sf = SELECTED_FRAME ();
8998
8999 /* Get the frame containing the mini-buffer
9000 that the selected frame is using. */
9001 mini_window = FRAME_MINIBUF_WINDOW (sf);
9002 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9003
9004 /* A null message buffer means that the frame hasn't really been
9005 initialized yet. Error messages get reported properly by
9006 cmd_error, so this must be just an informative message; toss
9007 it. */
9008 if (FRAME_MESSAGE_BUF (f))
9009 {
9010 if (m)
9011 {
9012 size_t len;
9013
9014 len = doprnt (FRAME_MESSAGE_BUF (f),
9015 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9016
9017 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9018 }
9019 else
9020 message1 (0);
9021
9022 /* Print should start at the beginning of the message
9023 buffer next time. */
9024 message_buf_print = 0;
9025 }
9026 }
9027 }
9028
9029 void
9030 message (const char *m, ...)
9031 {
9032 va_list ap;
9033 va_start (ap, m);
9034 vmessage (m, ap);
9035 va_end (ap);
9036 }
9037
9038
9039 #if 0
9040 /* The non-logging version of message. */
9041
9042 void
9043 message_nolog (const char *m, ...)
9044 {
9045 Lisp_Object old_log_max;
9046 va_list ap;
9047 va_start (ap, m);
9048 old_log_max = Vmessage_log_max;
9049 Vmessage_log_max = Qnil;
9050 vmessage (m, ap);
9051 Vmessage_log_max = old_log_max;
9052 va_end (ap);
9053 }
9054 #endif
9055
9056
9057 /* Display the current message in the current mini-buffer. This is
9058 only called from error handlers in process.c, and is not time
9059 critical. */
9060
9061 void
9062 update_echo_area (void)
9063 {
9064 if (!NILP (echo_area_buffer[0]))
9065 {
9066 Lisp_Object string;
9067 string = Fcurrent_message ();
9068 message3 (string, SBYTES (string),
9069 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9070 }
9071 }
9072
9073
9074 /* Make sure echo area buffers in `echo_buffers' are live.
9075 If they aren't, make new ones. */
9076
9077 static void
9078 ensure_echo_area_buffers (void)
9079 {
9080 int i;
9081
9082 for (i = 0; i < 2; ++i)
9083 if (!BUFFERP (echo_buffer[i])
9084 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9085 {
9086 char name[30];
9087 Lisp_Object old_buffer;
9088 int j;
9089
9090 old_buffer = echo_buffer[i];
9091 sprintf (name, " *Echo Area %d*", i);
9092 echo_buffer[i] = Fget_buffer_create (build_string (name));
9093 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9094 /* to force word wrap in echo area -
9095 it was decided to postpone this*/
9096 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9097
9098 for (j = 0; j < 2; ++j)
9099 if (EQ (old_buffer, echo_area_buffer[j]))
9100 echo_area_buffer[j] = echo_buffer[i];
9101 }
9102 }
9103
9104
9105 /* Call FN with args A1..A4 with either the current or last displayed
9106 echo_area_buffer as current buffer.
9107
9108 WHICH zero means use the current message buffer
9109 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9110 from echo_buffer[] and clear it.
9111
9112 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9113 suitable buffer from echo_buffer[] and clear it.
9114
9115 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9116 that the current message becomes the last displayed one, make
9117 choose a suitable buffer for echo_area_buffer[0], and clear it.
9118
9119 Value is what FN returns. */
9120
9121 static int
9122 with_echo_area_buffer (struct window *w, int which,
9123 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9124 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9125 {
9126 Lisp_Object buffer;
9127 int this_one, the_other, clear_buffer_p, rc;
9128 int count = SPECPDL_INDEX ();
9129
9130 /* If buffers aren't live, make new ones. */
9131 ensure_echo_area_buffers ();
9132
9133 clear_buffer_p = 0;
9134
9135 if (which == 0)
9136 this_one = 0, the_other = 1;
9137 else if (which > 0)
9138 this_one = 1, the_other = 0;
9139 else
9140 {
9141 this_one = 0, the_other = 1;
9142 clear_buffer_p = 1;
9143
9144 /* We need a fresh one in case the current echo buffer equals
9145 the one containing the last displayed echo area message. */
9146 if (!NILP (echo_area_buffer[this_one])
9147 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9148 echo_area_buffer[this_one] = Qnil;
9149 }
9150
9151 /* Choose a suitable buffer from echo_buffer[] is we don't
9152 have one. */
9153 if (NILP (echo_area_buffer[this_one]))
9154 {
9155 echo_area_buffer[this_one]
9156 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9157 ? echo_buffer[the_other]
9158 : echo_buffer[this_one]);
9159 clear_buffer_p = 1;
9160 }
9161
9162 buffer = echo_area_buffer[this_one];
9163
9164 /* Don't get confused by reusing the buffer used for echoing
9165 for a different purpose. */
9166 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9167 cancel_echoing ();
9168
9169 record_unwind_protect (unwind_with_echo_area_buffer,
9170 with_echo_area_buffer_unwind_data (w));
9171
9172 /* Make the echo area buffer current. Note that for display
9173 purposes, it is not necessary that the displayed window's buffer
9174 == current_buffer, except for text property lookup. So, let's
9175 only set that buffer temporarily here without doing a full
9176 Fset_window_buffer. We must also change w->pointm, though,
9177 because otherwise an assertions in unshow_buffer fails, and Emacs
9178 aborts. */
9179 set_buffer_internal_1 (XBUFFER (buffer));
9180 if (w)
9181 {
9182 w->buffer = buffer;
9183 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9184 }
9185
9186 BVAR (current_buffer, undo_list) = Qt;
9187 BVAR (current_buffer, read_only) = Qnil;
9188 specbind (Qinhibit_read_only, Qt);
9189 specbind (Qinhibit_modification_hooks, Qt);
9190
9191 if (clear_buffer_p && Z > BEG)
9192 del_range (BEG, Z);
9193
9194 xassert (BEGV >= BEG);
9195 xassert (ZV <= Z && ZV >= BEGV);
9196
9197 rc = fn (a1, a2, a3, a4);
9198
9199 xassert (BEGV >= BEG);
9200 xassert (ZV <= Z && ZV >= BEGV);
9201
9202 unbind_to (count, Qnil);
9203 return rc;
9204 }
9205
9206
9207 /* Save state that should be preserved around the call to the function
9208 FN called in with_echo_area_buffer. */
9209
9210 static Lisp_Object
9211 with_echo_area_buffer_unwind_data (struct window *w)
9212 {
9213 int i = 0;
9214 Lisp_Object vector, tmp;
9215
9216 /* Reduce consing by keeping one vector in
9217 Vwith_echo_area_save_vector. */
9218 vector = Vwith_echo_area_save_vector;
9219 Vwith_echo_area_save_vector = Qnil;
9220
9221 if (NILP (vector))
9222 vector = Fmake_vector (make_number (7), Qnil);
9223
9224 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9225 ASET (vector, i, Vdeactivate_mark); ++i;
9226 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9227
9228 if (w)
9229 {
9230 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9231 ASET (vector, i, w->buffer); ++i;
9232 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9233 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9234 }
9235 else
9236 {
9237 int end = i + 4;
9238 for (; i < end; ++i)
9239 ASET (vector, i, Qnil);
9240 }
9241
9242 xassert (i == ASIZE (vector));
9243 return vector;
9244 }
9245
9246
9247 /* Restore global state from VECTOR which was created by
9248 with_echo_area_buffer_unwind_data. */
9249
9250 static Lisp_Object
9251 unwind_with_echo_area_buffer (Lisp_Object vector)
9252 {
9253 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9254 Vdeactivate_mark = AREF (vector, 1);
9255 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9256
9257 if (WINDOWP (AREF (vector, 3)))
9258 {
9259 struct window *w;
9260 Lisp_Object buffer, charpos, bytepos;
9261
9262 w = XWINDOW (AREF (vector, 3));
9263 buffer = AREF (vector, 4);
9264 charpos = AREF (vector, 5);
9265 bytepos = AREF (vector, 6);
9266
9267 w->buffer = buffer;
9268 set_marker_both (w->pointm, buffer,
9269 XFASTINT (charpos), XFASTINT (bytepos));
9270 }
9271
9272 Vwith_echo_area_save_vector = vector;
9273 return Qnil;
9274 }
9275
9276
9277 /* Set up the echo area for use by print functions. MULTIBYTE_P
9278 non-zero means we will print multibyte. */
9279
9280 void
9281 setup_echo_area_for_printing (int multibyte_p)
9282 {
9283 /* If we can't find an echo area any more, exit. */
9284 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9285 Fkill_emacs (Qnil);
9286
9287 ensure_echo_area_buffers ();
9288
9289 if (!message_buf_print)
9290 {
9291 /* A message has been output since the last time we printed.
9292 Choose a fresh echo area buffer. */
9293 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9294 echo_area_buffer[0] = echo_buffer[1];
9295 else
9296 echo_area_buffer[0] = echo_buffer[0];
9297
9298 /* Switch to that buffer and clear it. */
9299 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9300 BVAR (current_buffer, truncate_lines) = Qnil;
9301
9302 if (Z > BEG)
9303 {
9304 int count = SPECPDL_INDEX ();
9305 specbind (Qinhibit_read_only, Qt);
9306 /* Note that undo recording is always disabled. */
9307 del_range (BEG, Z);
9308 unbind_to (count, Qnil);
9309 }
9310 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9311
9312 /* Set up the buffer for the multibyteness we need. */
9313 if (multibyte_p
9314 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9315 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9316
9317 /* Raise the frame containing the echo area. */
9318 if (minibuffer_auto_raise)
9319 {
9320 struct frame *sf = SELECTED_FRAME ();
9321 Lisp_Object mini_window;
9322 mini_window = FRAME_MINIBUF_WINDOW (sf);
9323 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9324 }
9325
9326 message_log_maybe_newline ();
9327 message_buf_print = 1;
9328 }
9329 else
9330 {
9331 if (NILP (echo_area_buffer[0]))
9332 {
9333 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9334 echo_area_buffer[0] = echo_buffer[1];
9335 else
9336 echo_area_buffer[0] = echo_buffer[0];
9337 }
9338
9339 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9340 {
9341 /* Someone switched buffers between print requests. */
9342 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9343 BVAR (current_buffer, truncate_lines) = Qnil;
9344 }
9345 }
9346 }
9347
9348
9349 /* Display an echo area message in window W. Value is non-zero if W's
9350 height is changed. If display_last_displayed_message_p is
9351 non-zero, display the message that was last displayed, otherwise
9352 display the current message. */
9353
9354 static int
9355 display_echo_area (struct window *w)
9356 {
9357 int i, no_message_p, window_height_changed_p, count;
9358
9359 /* Temporarily disable garbage collections while displaying the echo
9360 area. This is done because a GC can print a message itself.
9361 That message would modify the echo area buffer's contents while a
9362 redisplay of the buffer is going on, and seriously confuse
9363 redisplay. */
9364 count = inhibit_garbage_collection ();
9365
9366 /* If there is no message, we must call display_echo_area_1
9367 nevertheless because it resizes the window. But we will have to
9368 reset the echo_area_buffer in question to nil at the end because
9369 with_echo_area_buffer will sets it to an empty buffer. */
9370 i = display_last_displayed_message_p ? 1 : 0;
9371 no_message_p = NILP (echo_area_buffer[i]);
9372
9373 window_height_changed_p
9374 = with_echo_area_buffer (w, display_last_displayed_message_p,
9375 display_echo_area_1,
9376 (intptr_t) w, Qnil, 0, 0);
9377
9378 if (no_message_p)
9379 echo_area_buffer[i] = Qnil;
9380
9381 unbind_to (count, Qnil);
9382 return window_height_changed_p;
9383 }
9384
9385
9386 /* Helper for display_echo_area. Display the current buffer which
9387 contains the current echo area message in window W, a mini-window,
9388 a pointer to which is passed in A1. A2..A4 are currently not used.
9389 Change the height of W so that all of the message is displayed.
9390 Value is non-zero if height of W was changed. */
9391
9392 static int
9393 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9394 {
9395 intptr_t i1 = a1;
9396 struct window *w = (struct window *) i1;
9397 Lisp_Object window;
9398 struct text_pos start;
9399 int window_height_changed_p = 0;
9400
9401 /* Do this before displaying, so that we have a large enough glyph
9402 matrix for the display. If we can't get enough space for the
9403 whole text, display the last N lines. That works by setting w->start. */
9404 window_height_changed_p = resize_mini_window (w, 0);
9405
9406 /* Use the starting position chosen by resize_mini_window. */
9407 SET_TEXT_POS_FROM_MARKER (start, w->start);
9408
9409 /* Display. */
9410 clear_glyph_matrix (w->desired_matrix);
9411 XSETWINDOW (window, w);
9412 try_window (window, start, 0);
9413
9414 return window_height_changed_p;
9415 }
9416
9417
9418 /* Resize the echo area window to exactly the size needed for the
9419 currently displayed message, if there is one. If a mini-buffer
9420 is active, don't shrink it. */
9421
9422 void
9423 resize_echo_area_exactly (void)
9424 {
9425 if (BUFFERP (echo_area_buffer[0])
9426 && WINDOWP (echo_area_window))
9427 {
9428 struct window *w = XWINDOW (echo_area_window);
9429 int resized_p;
9430 Lisp_Object resize_exactly;
9431
9432 if (minibuf_level == 0)
9433 resize_exactly = Qt;
9434 else
9435 resize_exactly = Qnil;
9436
9437 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9438 (intptr_t) w, resize_exactly,
9439 0, 0);
9440 if (resized_p)
9441 {
9442 ++windows_or_buffers_changed;
9443 ++update_mode_lines;
9444 redisplay_internal ();
9445 }
9446 }
9447 }
9448
9449
9450 /* Callback function for with_echo_area_buffer, when used from
9451 resize_echo_area_exactly. A1 contains a pointer to the window to
9452 resize, EXACTLY non-nil means resize the mini-window exactly to the
9453 size of the text displayed. A3 and A4 are not used. Value is what
9454 resize_mini_window returns. */
9455
9456 static int
9457 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9458 {
9459 intptr_t i1 = a1;
9460 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9461 }
9462
9463
9464 /* Resize mini-window W to fit the size of its contents. EXACT_P
9465 means size the window exactly to the size needed. Otherwise, it's
9466 only enlarged until W's buffer is empty.
9467
9468 Set W->start to the right place to begin display. If the whole
9469 contents fit, start at the beginning. Otherwise, start so as
9470 to make the end of the contents appear. This is particularly
9471 important for y-or-n-p, but seems desirable generally.
9472
9473 Value is non-zero if the window height has been changed. */
9474
9475 int
9476 resize_mini_window (struct window *w, int exact_p)
9477 {
9478 struct frame *f = XFRAME (w->frame);
9479 int window_height_changed_p = 0;
9480
9481 xassert (MINI_WINDOW_P (w));
9482
9483 /* By default, start display at the beginning. */
9484 set_marker_both (w->start, w->buffer,
9485 BUF_BEGV (XBUFFER (w->buffer)),
9486 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9487
9488 /* Don't resize windows while redisplaying a window; it would
9489 confuse redisplay functions when the size of the window they are
9490 displaying changes from under them. Such a resizing can happen,
9491 for instance, when which-func prints a long message while
9492 we are running fontification-functions. We're running these
9493 functions with safe_call which binds inhibit-redisplay to t. */
9494 if (!NILP (Vinhibit_redisplay))
9495 return 0;
9496
9497 /* Nil means don't try to resize. */
9498 if (NILP (Vresize_mini_windows)
9499 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9500 return 0;
9501
9502 if (!FRAME_MINIBUF_ONLY_P (f))
9503 {
9504 struct it it;
9505 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9506 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9507 int height, max_height;
9508 int unit = FRAME_LINE_HEIGHT (f);
9509 struct text_pos start;
9510 struct buffer *old_current_buffer = NULL;
9511
9512 if (current_buffer != XBUFFER (w->buffer))
9513 {
9514 old_current_buffer = current_buffer;
9515 set_buffer_internal (XBUFFER (w->buffer));
9516 }
9517
9518 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9519
9520 /* Compute the max. number of lines specified by the user. */
9521 if (FLOATP (Vmax_mini_window_height))
9522 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9523 else if (INTEGERP (Vmax_mini_window_height))
9524 max_height = XINT (Vmax_mini_window_height);
9525 else
9526 max_height = total_height / 4;
9527
9528 /* Correct that max. height if it's bogus. */
9529 max_height = max (1, max_height);
9530 max_height = min (total_height, max_height);
9531
9532 /* Find out the height of the text in the window. */
9533 if (it.line_wrap == TRUNCATE)
9534 height = 1;
9535 else
9536 {
9537 last_height = 0;
9538 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9539 if (it.max_ascent == 0 && it.max_descent == 0)
9540 height = it.current_y + last_height;
9541 else
9542 height = it.current_y + it.max_ascent + it.max_descent;
9543 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9544 height = (height + unit - 1) / unit;
9545 }
9546
9547 /* Compute a suitable window start. */
9548 if (height > max_height)
9549 {
9550 height = max_height;
9551 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9552 move_it_vertically_backward (&it, (height - 1) * unit);
9553 start = it.current.pos;
9554 }
9555 else
9556 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9557 SET_MARKER_FROM_TEXT_POS (w->start, start);
9558
9559 if (EQ (Vresize_mini_windows, Qgrow_only))
9560 {
9561 /* Let it grow only, until we display an empty message, in which
9562 case the window shrinks again. */
9563 if (height > WINDOW_TOTAL_LINES (w))
9564 {
9565 int old_height = WINDOW_TOTAL_LINES (w);
9566 freeze_window_starts (f, 1);
9567 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9568 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9569 }
9570 else if (height < WINDOW_TOTAL_LINES (w)
9571 && (exact_p || BEGV == ZV))
9572 {
9573 int old_height = WINDOW_TOTAL_LINES (w);
9574 freeze_window_starts (f, 0);
9575 shrink_mini_window (w);
9576 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9577 }
9578 }
9579 else
9580 {
9581 /* Always resize to exact size needed. */
9582 if (height > WINDOW_TOTAL_LINES (w))
9583 {
9584 int old_height = WINDOW_TOTAL_LINES (w);
9585 freeze_window_starts (f, 1);
9586 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9587 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9588 }
9589 else if (height < WINDOW_TOTAL_LINES (w))
9590 {
9591 int old_height = WINDOW_TOTAL_LINES (w);
9592 freeze_window_starts (f, 0);
9593 shrink_mini_window (w);
9594
9595 if (height)
9596 {
9597 freeze_window_starts (f, 1);
9598 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9599 }
9600
9601 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9602 }
9603 }
9604
9605 if (old_current_buffer)
9606 set_buffer_internal (old_current_buffer);
9607 }
9608
9609 return window_height_changed_p;
9610 }
9611
9612
9613 /* Value is the current message, a string, or nil if there is no
9614 current message. */
9615
9616 Lisp_Object
9617 current_message (void)
9618 {
9619 Lisp_Object msg;
9620
9621 if (!BUFFERP (echo_area_buffer[0]))
9622 msg = Qnil;
9623 else
9624 {
9625 with_echo_area_buffer (0, 0, current_message_1,
9626 (intptr_t) &msg, Qnil, 0, 0);
9627 if (NILP (msg))
9628 echo_area_buffer[0] = Qnil;
9629 }
9630
9631 return msg;
9632 }
9633
9634
9635 static int
9636 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9637 {
9638 intptr_t i1 = a1;
9639 Lisp_Object *msg = (Lisp_Object *) i1;
9640
9641 if (Z > BEG)
9642 *msg = make_buffer_string (BEG, Z, 1);
9643 else
9644 *msg = Qnil;
9645 return 0;
9646 }
9647
9648
9649 /* Push the current message on Vmessage_stack for later restauration
9650 by restore_message. Value is non-zero if the current message isn't
9651 empty. This is a relatively infrequent operation, so it's not
9652 worth optimizing. */
9653
9654 int
9655 push_message (void)
9656 {
9657 Lisp_Object msg;
9658 msg = current_message ();
9659 Vmessage_stack = Fcons (msg, Vmessage_stack);
9660 return STRINGP (msg);
9661 }
9662
9663
9664 /* Restore message display from the top of Vmessage_stack. */
9665
9666 void
9667 restore_message (void)
9668 {
9669 Lisp_Object msg;
9670
9671 xassert (CONSP (Vmessage_stack));
9672 msg = XCAR (Vmessage_stack);
9673 if (STRINGP (msg))
9674 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9675 else
9676 message3_nolog (msg, 0, 0);
9677 }
9678
9679
9680 /* Handler for record_unwind_protect calling pop_message. */
9681
9682 Lisp_Object
9683 pop_message_unwind (Lisp_Object dummy)
9684 {
9685 pop_message ();
9686 return Qnil;
9687 }
9688
9689 /* Pop the top-most entry off Vmessage_stack. */
9690
9691 static void
9692 pop_message (void)
9693 {
9694 xassert (CONSP (Vmessage_stack));
9695 Vmessage_stack = XCDR (Vmessage_stack);
9696 }
9697
9698
9699 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9700 exits. If the stack is not empty, we have a missing pop_message
9701 somewhere. */
9702
9703 void
9704 check_message_stack (void)
9705 {
9706 if (!NILP (Vmessage_stack))
9707 abort ();
9708 }
9709
9710
9711 /* Truncate to NCHARS what will be displayed in the echo area the next
9712 time we display it---but don't redisplay it now. */
9713
9714 void
9715 truncate_echo_area (EMACS_INT nchars)
9716 {
9717 if (nchars == 0)
9718 echo_area_buffer[0] = Qnil;
9719 /* A null message buffer means that the frame hasn't really been
9720 initialized yet. Error messages get reported properly by
9721 cmd_error, so this must be just an informative message; toss it. */
9722 else if (!noninteractive
9723 && INTERACTIVE
9724 && !NILP (echo_area_buffer[0]))
9725 {
9726 struct frame *sf = SELECTED_FRAME ();
9727 if (FRAME_MESSAGE_BUF (sf))
9728 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9729 }
9730 }
9731
9732
9733 /* Helper function for truncate_echo_area. Truncate the current
9734 message to at most NCHARS characters. */
9735
9736 static int
9737 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9738 {
9739 if (BEG + nchars < Z)
9740 del_range (BEG + nchars, Z);
9741 if (Z == BEG)
9742 echo_area_buffer[0] = Qnil;
9743 return 0;
9744 }
9745
9746
9747 /* Set the current message to a substring of S or STRING.
9748
9749 If STRING is a Lisp string, set the message to the first NBYTES
9750 bytes from STRING. NBYTES zero means use the whole string. If
9751 STRING is multibyte, the message will be displayed multibyte.
9752
9753 If S is not null, set the message to the first LEN bytes of S. LEN
9754 zero means use the whole string. MULTIBYTE_P non-zero means S is
9755 multibyte. Display the message multibyte in that case.
9756
9757 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9758 to t before calling set_message_1 (which calls insert).
9759 */
9760
9761 static void
9762 set_message (const char *s, Lisp_Object string,
9763 EMACS_INT nbytes, int multibyte_p)
9764 {
9765 message_enable_multibyte
9766 = ((s && multibyte_p)
9767 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9768
9769 with_echo_area_buffer (0, -1, set_message_1,
9770 (intptr_t) s, string, nbytes, multibyte_p);
9771 message_buf_print = 0;
9772 help_echo_showing_p = 0;
9773 }
9774
9775
9776 /* Helper function for set_message. Arguments have the same meaning
9777 as there, with A1 corresponding to S and A2 corresponding to STRING
9778 This function is called with the echo area buffer being
9779 current. */
9780
9781 static int
9782 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9783 {
9784 intptr_t i1 = a1;
9785 const char *s = (const char *) i1;
9786 const unsigned char *msg = (const unsigned char *) s;
9787 Lisp_Object string = a2;
9788
9789 /* Change multibyteness of the echo buffer appropriately. */
9790 if (message_enable_multibyte
9791 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9792 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9793
9794 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9795 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9796 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9797
9798 /* Insert new message at BEG. */
9799 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9800
9801 if (STRINGP (string))
9802 {
9803 EMACS_INT nchars;
9804
9805 if (nbytes == 0)
9806 nbytes = SBYTES (string);
9807 nchars = string_byte_to_char (string, nbytes);
9808
9809 /* This function takes care of single/multibyte conversion. We
9810 just have to ensure that the echo area buffer has the right
9811 setting of enable_multibyte_characters. */
9812 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9813 }
9814 else if (s)
9815 {
9816 if (nbytes == 0)
9817 nbytes = strlen (s);
9818
9819 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9820 {
9821 /* Convert from multi-byte to single-byte. */
9822 EMACS_INT i;
9823 int c, n;
9824 char work[1];
9825
9826 /* Convert a multibyte string to single-byte. */
9827 for (i = 0; i < nbytes; i += n)
9828 {
9829 c = string_char_and_length (msg + i, &n);
9830 work[0] = (ASCII_CHAR_P (c)
9831 ? c
9832 : multibyte_char_to_unibyte (c));
9833 insert_1_both (work, 1, 1, 1, 0, 0);
9834 }
9835 }
9836 else if (!multibyte_p
9837 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9838 {
9839 /* Convert from single-byte to multi-byte. */
9840 EMACS_INT i;
9841 int c, n;
9842 unsigned char str[MAX_MULTIBYTE_LENGTH];
9843
9844 /* Convert a single-byte string to multibyte. */
9845 for (i = 0; i < nbytes; i++)
9846 {
9847 c = msg[i];
9848 MAKE_CHAR_MULTIBYTE (c);
9849 n = CHAR_STRING (c, str);
9850 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9851 }
9852 }
9853 else
9854 insert_1 (s, nbytes, 1, 0, 0);
9855 }
9856
9857 return 0;
9858 }
9859
9860
9861 /* Clear messages. CURRENT_P non-zero means clear the current
9862 message. LAST_DISPLAYED_P non-zero means clear the message
9863 last displayed. */
9864
9865 void
9866 clear_message (int current_p, int last_displayed_p)
9867 {
9868 if (current_p)
9869 {
9870 echo_area_buffer[0] = Qnil;
9871 message_cleared_p = 1;
9872 }
9873
9874 if (last_displayed_p)
9875 echo_area_buffer[1] = Qnil;
9876
9877 message_buf_print = 0;
9878 }
9879
9880 /* Clear garbaged frames.
9881
9882 This function is used where the old redisplay called
9883 redraw_garbaged_frames which in turn called redraw_frame which in
9884 turn called clear_frame. The call to clear_frame was a source of
9885 flickering. I believe a clear_frame is not necessary. It should
9886 suffice in the new redisplay to invalidate all current matrices,
9887 and ensure a complete redisplay of all windows. */
9888
9889 static void
9890 clear_garbaged_frames (void)
9891 {
9892 if (frame_garbaged)
9893 {
9894 Lisp_Object tail, frame;
9895 int changed_count = 0;
9896
9897 FOR_EACH_FRAME (tail, frame)
9898 {
9899 struct frame *f = XFRAME (frame);
9900
9901 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9902 {
9903 if (f->resized_p)
9904 {
9905 Fredraw_frame (frame);
9906 f->force_flush_display_p = 1;
9907 }
9908 clear_current_matrices (f);
9909 changed_count++;
9910 f->garbaged = 0;
9911 f->resized_p = 0;
9912 }
9913 }
9914
9915 frame_garbaged = 0;
9916 if (changed_count)
9917 ++windows_or_buffers_changed;
9918 }
9919 }
9920
9921
9922 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9923 is non-zero update selected_frame. Value is non-zero if the
9924 mini-windows height has been changed. */
9925
9926 static int
9927 echo_area_display (int update_frame_p)
9928 {
9929 Lisp_Object mini_window;
9930 struct window *w;
9931 struct frame *f;
9932 int window_height_changed_p = 0;
9933 struct frame *sf = SELECTED_FRAME ();
9934
9935 mini_window = FRAME_MINIBUF_WINDOW (sf);
9936 w = XWINDOW (mini_window);
9937 f = XFRAME (WINDOW_FRAME (w));
9938
9939 /* Don't display if frame is invisible or not yet initialized. */
9940 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9941 return 0;
9942
9943 #ifdef HAVE_WINDOW_SYSTEM
9944 /* When Emacs starts, selected_frame may be the initial terminal
9945 frame. If we let this through, a message would be displayed on
9946 the terminal. */
9947 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9948 return 0;
9949 #endif /* HAVE_WINDOW_SYSTEM */
9950
9951 /* Redraw garbaged frames. */
9952 if (frame_garbaged)
9953 clear_garbaged_frames ();
9954
9955 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9956 {
9957 echo_area_window = mini_window;
9958 window_height_changed_p = display_echo_area (w);
9959 w->must_be_updated_p = 1;
9960
9961 /* Update the display, unless called from redisplay_internal.
9962 Also don't update the screen during redisplay itself. The
9963 update will happen at the end of redisplay, and an update
9964 here could cause confusion. */
9965 if (update_frame_p && !redisplaying_p)
9966 {
9967 int n = 0;
9968
9969 /* If the display update has been interrupted by pending
9970 input, update mode lines in the frame. Due to the
9971 pending input, it might have been that redisplay hasn't
9972 been called, so that mode lines above the echo area are
9973 garbaged. This looks odd, so we prevent it here. */
9974 if (!display_completed)
9975 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9976
9977 if (window_height_changed_p
9978 /* Don't do this if Emacs is shutting down. Redisplay
9979 needs to run hooks. */
9980 && !NILP (Vrun_hooks))
9981 {
9982 /* Must update other windows. Likewise as in other
9983 cases, don't let this update be interrupted by
9984 pending input. */
9985 int count = SPECPDL_INDEX ();
9986 specbind (Qredisplay_dont_pause, Qt);
9987 windows_or_buffers_changed = 1;
9988 redisplay_internal ();
9989 unbind_to (count, Qnil);
9990 }
9991 else if (FRAME_WINDOW_P (f) && n == 0)
9992 {
9993 /* Window configuration is the same as before.
9994 Can do with a display update of the echo area,
9995 unless we displayed some mode lines. */
9996 update_single_window (w, 1);
9997 FRAME_RIF (f)->flush_display (f);
9998 }
9999 else
10000 update_frame (f, 1, 1);
10001
10002 /* If cursor is in the echo area, make sure that the next
10003 redisplay displays the minibuffer, so that the cursor will
10004 be replaced with what the minibuffer wants. */
10005 if (cursor_in_echo_area)
10006 ++windows_or_buffers_changed;
10007 }
10008 }
10009 else if (!EQ (mini_window, selected_window))
10010 windows_or_buffers_changed++;
10011
10012 /* Last displayed message is now the current message. */
10013 echo_area_buffer[1] = echo_area_buffer[0];
10014 /* Inform read_char that we're not echoing. */
10015 echo_message_buffer = Qnil;
10016
10017 /* Prevent redisplay optimization in redisplay_internal by resetting
10018 this_line_start_pos. This is done because the mini-buffer now
10019 displays the message instead of its buffer text. */
10020 if (EQ (mini_window, selected_window))
10021 CHARPOS (this_line_start_pos) = 0;
10022
10023 return window_height_changed_p;
10024 }
10025
10026
10027 \f
10028 /***********************************************************************
10029 Mode Lines and Frame Titles
10030 ***********************************************************************/
10031
10032 /* A buffer for constructing non-propertized mode-line strings and
10033 frame titles in it; allocated from the heap in init_xdisp and
10034 resized as needed in store_mode_line_noprop_char. */
10035
10036 static char *mode_line_noprop_buf;
10037
10038 /* The buffer's end, and a current output position in it. */
10039
10040 static char *mode_line_noprop_buf_end;
10041 static char *mode_line_noprop_ptr;
10042
10043 #define MODE_LINE_NOPROP_LEN(start) \
10044 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10045
10046 static enum {
10047 MODE_LINE_DISPLAY = 0,
10048 MODE_LINE_TITLE,
10049 MODE_LINE_NOPROP,
10050 MODE_LINE_STRING
10051 } mode_line_target;
10052
10053 /* Alist that caches the results of :propertize.
10054 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10055 static Lisp_Object mode_line_proptrans_alist;
10056
10057 /* List of strings making up the mode-line. */
10058 static Lisp_Object mode_line_string_list;
10059
10060 /* Base face property when building propertized mode line string. */
10061 static Lisp_Object mode_line_string_face;
10062 static Lisp_Object mode_line_string_face_prop;
10063
10064
10065 /* Unwind data for mode line strings */
10066
10067 static Lisp_Object Vmode_line_unwind_vector;
10068
10069 static Lisp_Object
10070 format_mode_line_unwind_data (struct buffer *obuf,
10071 Lisp_Object owin,
10072 int save_proptrans)
10073 {
10074 Lisp_Object vector, tmp;
10075
10076 /* Reduce consing by keeping one vector in
10077 Vwith_echo_area_save_vector. */
10078 vector = Vmode_line_unwind_vector;
10079 Vmode_line_unwind_vector = Qnil;
10080
10081 if (NILP (vector))
10082 vector = Fmake_vector (make_number (8), Qnil);
10083
10084 ASET (vector, 0, make_number (mode_line_target));
10085 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10086 ASET (vector, 2, mode_line_string_list);
10087 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10088 ASET (vector, 4, mode_line_string_face);
10089 ASET (vector, 5, mode_line_string_face_prop);
10090
10091 if (obuf)
10092 XSETBUFFER (tmp, obuf);
10093 else
10094 tmp = Qnil;
10095 ASET (vector, 6, tmp);
10096 ASET (vector, 7, owin);
10097
10098 return vector;
10099 }
10100
10101 static Lisp_Object
10102 unwind_format_mode_line (Lisp_Object vector)
10103 {
10104 mode_line_target = XINT (AREF (vector, 0));
10105 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10106 mode_line_string_list = AREF (vector, 2);
10107 if (! EQ (AREF (vector, 3), Qt))
10108 mode_line_proptrans_alist = AREF (vector, 3);
10109 mode_line_string_face = AREF (vector, 4);
10110 mode_line_string_face_prop = AREF (vector, 5);
10111
10112 if (!NILP (AREF (vector, 7)))
10113 /* Select window before buffer, since it may change the buffer. */
10114 Fselect_window (AREF (vector, 7), Qt);
10115
10116 if (!NILP (AREF (vector, 6)))
10117 {
10118 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10119 ASET (vector, 6, Qnil);
10120 }
10121
10122 Vmode_line_unwind_vector = vector;
10123 return Qnil;
10124 }
10125
10126
10127 /* Store a single character C for the frame title in mode_line_noprop_buf.
10128 Re-allocate mode_line_noprop_buf if necessary. */
10129
10130 static void
10131 store_mode_line_noprop_char (char c)
10132 {
10133 /* If output position has reached the end of the allocated buffer,
10134 double the buffer's size. */
10135 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10136 {
10137 int len = MODE_LINE_NOPROP_LEN (0);
10138 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10139 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10140 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10141 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10142 }
10143
10144 *mode_line_noprop_ptr++ = c;
10145 }
10146
10147
10148 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10149 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10150 characters that yield more columns than PRECISION; PRECISION <= 0
10151 means copy the whole string. Pad with spaces until FIELD_WIDTH
10152 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10153 pad. Called from display_mode_element when it is used to build a
10154 frame title. */
10155
10156 static int
10157 store_mode_line_noprop (const char *string, int field_width, int precision)
10158 {
10159 const unsigned char *str = (const unsigned char *) string;
10160 int n = 0;
10161 EMACS_INT dummy, nbytes;
10162
10163 /* Copy at most PRECISION chars from STR. */
10164 nbytes = strlen (string);
10165 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10166 while (nbytes--)
10167 store_mode_line_noprop_char (*str++);
10168
10169 /* Fill up with spaces until FIELD_WIDTH reached. */
10170 while (field_width > 0
10171 && n < field_width)
10172 {
10173 store_mode_line_noprop_char (' ');
10174 ++n;
10175 }
10176
10177 return n;
10178 }
10179
10180 /***********************************************************************
10181 Frame Titles
10182 ***********************************************************************/
10183
10184 #ifdef HAVE_WINDOW_SYSTEM
10185
10186 /* Set the title of FRAME, if it has changed. The title format is
10187 Vicon_title_format if FRAME is iconified, otherwise it is
10188 frame_title_format. */
10189
10190 static void
10191 x_consider_frame_title (Lisp_Object frame)
10192 {
10193 struct frame *f = XFRAME (frame);
10194
10195 if (FRAME_WINDOW_P (f)
10196 || FRAME_MINIBUF_ONLY_P (f)
10197 || f->explicit_name)
10198 {
10199 /* Do we have more than one visible frame on this X display? */
10200 Lisp_Object tail;
10201 Lisp_Object fmt;
10202 int title_start;
10203 char *title;
10204 int len;
10205 struct it it;
10206 int count = SPECPDL_INDEX ();
10207
10208 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10209 {
10210 Lisp_Object other_frame = XCAR (tail);
10211 struct frame *tf = XFRAME (other_frame);
10212
10213 if (tf != f
10214 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10215 && !FRAME_MINIBUF_ONLY_P (tf)
10216 && !EQ (other_frame, tip_frame)
10217 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10218 break;
10219 }
10220
10221 /* Set global variable indicating that multiple frames exist. */
10222 multiple_frames = CONSP (tail);
10223
10224 /* Switch to the buffer of selected window of the frame. Set up
10225 mode_line_target so that display_mode_element will output into
10226 mode_line_noprop_buf; then display the title. */
10227 record_unwind_protect (unwind_format_mode_line,
10228 format_mode_line_unwind_data
10229 (current_buffer, selected_window, 0));
10230
10231 Fselect_window (f->selected_window, Qt);
10232 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10233 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10234
10235 mode_line_target = MODE_LINE_TITLE;
10236 title_start = MODE_LINE_NOPROP_LEN (0);
10237 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10238 NULL, DEFAULT_FACE_ID);
10239 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10240 len = MODE_LINE_NOPROP_LEN (title_start);
10241 title = mode_line_noprop_buf + title_start;
10242 unbind_to (count, Qnil);
10243
10244 /* Set the title only if it's changed. This avoids consing in
10245 the common case where it hasn't. (If it turns out that we've
10246 already wasted too much time by walking through the list with
10247 display_mode_element, then we might need to optimize at a
10248 higher level than this.) */
10249 if (! STRINGP (f->name)
10250 || SBYTES (f->name) != len
10251 || memcmp (title, SDATA (f->name), len) != 0)
10252 x_implicitly_set_name (f, make_string (title, len), Qnil);
10253 }
10254 }
10255
10256 #endif /* not HAVE_WINDOW_SYSTEM */
10257
10258
10259
10260 \f
10261 /***********************************************************************
10262 Menu Bars
10263 ***********************************************************************/
10264
10265
10266 /* Prepare for redisplay by updating menu-bar item lists when
10267 appropriate. This can call eval. */
10268
10269 void
10270 prepare_menu_bars (void)
10271 {
10272 int all_windows;
10273 struct gcpro gcpro1, gcpro2;
10274 struct frame *f;
10275 Lisp_Object tooltip_frame;
10276
10277 #ifdef HAVE_WINDOW_SYSTEM
10278 tooltip_frame = tip_frame;
10279 #else
10280 tooltip_frame = Qnil;
10281 #endif
10282
10283 /* Update all frame titles based on their buffer names, etc. We do
10284 this before the menu bars so that the buffer-menu will show the
10285 up-to-date frame titles. */
10286 #ifdef HAVE_WINDOW_SYSTEM
10287 if (windows_or_buffers_changed || update_mode_lines)
10288 {
10289 Lisp_Object tail, frame;
10290
10291 FOR_EACH_FRAME (tail, frame)
10292 {
10293 f = XFRAME (frame);
10294 if (!EQ (frame, tooltip_frame)
10295 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10296 x_consider_frame_title (frame);
10297 }
10298 }
10299 #endif /* HAVE_WINDOW_SYSTEM */
10300
10301 /* Update the menu bar item lists, if appropriate. This has to be
10302 done before any actual redisplay or generation of display lines. */
10303 all_windows = (update_mode_lines
10304 || buffer_shared > 1
10305 || windows_or_buffers_changed);
10306 if (all_windows)
10307 {
10308 Lisp_Object tail, frame;
10309 int count = SPECPDL_INDEX ();
10310 /* 1 means that update_menu_bar has run its hooks
10311 so any further calls to update_menu_bar shouldn't do so again. */
10312 int menu_bar_hooks_run = 0;
10313
10314 record_unwind_save_match_data ();
10315
10316 FOR_EACH_FRAME (tail, frame)
10317 {
10318 f = XFRAME (frame);
10319
10320 /* Ignore tooltip frame. */
10321 if (EQ (frame, tooltip_frame))
10322 continue;
10323
10324 /* If a window on this frame changed size, report that to
10325 the user and clear the size-change flag. */
10326 if (FRAME_WINDOW_SIZES_CHANGED (f))
10327 {
10328 Lisp_Object functions;
10329
10330 /* Clear flag first in case we get an error below. */
10331 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10332 functions = Vwindow_size_change_functions;
10333 GCPRO2 (tail, functions);
10334
10335 while (CONSP (functions))
10336 {
10337 if (!EQ (XCAR (functions), Qt))
10338 call1 (XCAR (functions), frame);
10339 functions = XCDR (functions);
10340 }
10341 UNGCPRO;
10342 }
10343
10344 GCPRO1 (tail);
10345 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10346 #ifdef HAVE_WINDOW_SYSTEM
10347 update_tool_bar (f, 0);
10348 #endif
10349 #ifdef HAVE_NS
10350 if (windows_or_buffers_changed
10351 && FRAME_NS_P (f))
10352 ns_set_doc_edited (f, Fbuffer_modified_p
10353 (XWINDOW (f->selected_window)->buffer));
10354 #endif
10355 UNGCPRO;
10356 }
10357
10358 unbind_to (count, Qnil);
10359 }
10360 else
10361 {
10362 struct frame *sf = SELECTED_FRAME ();
10363 update_menu_bar (sf, 1, 0);
10364 #ifdef HAVE_WINDOW_SYSTEM
10365 update_tool_bar (sf, 1);
10366 #endif
10367 }
10368 }
10369
10370
10371 /* Update the menu bar item list for frame F. This has to be done
10372 before we start to fill in any display lines, because it can call
10373 eval.
10374
10375 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10376
10377 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10378 already ran the menu bar hooks for this redisplay, so there
10379 is no need to run them again. The return value is the
10380 updated value of this flag, to pass to the next call. */
10381
10382 static int
10383 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10384 {
10385 Lisp_Object window;
10386 register struct window *w;
10387
10388 /* If called recursively during a menu update, do nothing. This can
10389 happen when, for instance, an activate-menubar-hook causes a
10390 redisplay. */
10391 if (inhibit_menubar_update)
10392 return hooks_run;
10393
10394 window = FRAME_SELECTED_WINDOW (f);
10395 w = XWINDOW (window);
10396
10397 if (FRAME_WINDOW_P (f)
10398 ?
10399 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10400 || defined (HAVE_NS) || defined (USE_GTK)
10401 FRAME_EXTERNAL_MENU_BAR (f)
10402 #else
10403 FRAME_MENU_BAR_LINES (f) > 0
10404 #endif
10405 : FRAME_MENU_BAR_LINES (f) > 0)
10406 {
10407 /* If the user has switched buffers or windows, we need to
10408 recompute to reflect the new bindings. But we'll
10409 recompute when update_mode_lines is set too; that means
10410 that people can use force-mode-line-update to request
10411 that the menu bar be recomputed. The adverse effect on
10412 the rest of the redisplay algorithm is about the same as
10413 windows_or_buffers_changed anyway. */
10414 if (windows_or_buffers_changed
10415 /* This used to test w->update_mode_line, but we believe
10416 there is no need to recompute the menu in that case. */
10417 || update_mode_lines
10418 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10419 < BUF_MODIFF (XBUFFER (w->buffer)))
10420 != !NILP (w->last_had_star))
10421 || ((!NILP (Vtransient_mark_mode)
10422 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10423 != !NILP (w->region_showing)))
10424 {
10425 struct buffer *prev = current_buffer;
10426 int count = SPECPDL_INDEX ();
10427
10428 specbind (Qinhibit_menubar_update, Qt);
10429
10430 set_buffer_internal_1 (XBUFFER (w->buffer));
10431 if (save_match_data)
10432 record_unwind_save_match_data ();
10433 if (NILP (Voverriding_local_map_menu_flag))
10434 {
10435 specbind (Qoverriding_terminal_local_map, Qnil);
10436 specbind (Qoverriding_local_map, Qnil);
10437 }
10438
10439 if (!hooks_run)
10440 {
10441 /* Run the Lucid hook. */
10442 safe_run_hooks (Qactivate_menubar_hook);
10443
10444 /* If it has changed current-menubar from previous value,
10445 really recompute the menu-bar from the value. */
10446 if (! NILP (Vlucid_menu_bar_dirty_flag))
10447 call0 (Qrecompute_lucid_menubar);
10448
10449 safe_run_hooks (Qmenu_bar_update_hook);
10450
10451 hooks_run = 1;
10452 }
10453
10454 XSETFRAME (Vmenu_updating_frame, f);
10455 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10456
10457 /* Redisplay the menu bar in case we changed it. */
10458 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10459 || defined (HAVE_NS) || defined (USE_GTK)
10460 if (FRAME_WINDOW_P (f))
10461 {
10462 #if defined (HAVE_NS)
10463 /* All frames on Mac OS share the same menubar. So only
10464 the selected frame should be allowed to set it. */
10465 if (f == SELECTED_FRAME ())
10466 #endif
10467 set_frame_menubar (f, 0, 0);
10468 }
10469 else
10470 /* On a terminal screen, the menu bar is an ordinary screen
10471 line, and this makes it get updated. */
10472 w->update_mode_line = Qt;
10473 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10474 /* In the non-toolkit version, the menu bar is an ordinary screen
10475 line, and this makes it get updated. */
10476 w->update_mode_line = Qt;
10477 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10478
10479 unbind_to (count, Qnil);
10480 set_buffer_internal_1 (prev);
10481 }
10482 }
10483
10484 return hooks_run;
10485 }
10486
10487
10488 \f
10489 /***********************************************************************
10490 Output Cursor
10491 ***********************************************************************/
10492
10493 #ifdef HAVE_WINDOW_SYSTEM
10494
10495 /* EXPORT:
10496 Nominal cursor position -- where to draw output.
10497 HPOS and VPOS are window relative glyph matrix coordinates.
10498 X and Y are window relative pixel coordinates. */
10499
10500 struct cursor_pos output_cursor;
10501
10502
10503 /* EXPORT:
10504 Set the global variable output_cursor to CURSOR. All cursor
10505 positions are relative to updated_window. */
10506
10507 void
10508 set_output_cursor (struct cursor_pos *cursor)
10509 {
10510 output_cursor.hpos = cursor->hpos;
10511 output_cursor.vpos = cursor->vpos;
10512 output_cursor.x = cursor->x;
10513 output_cursor.y = cursor->y;
10514 }
10515
10516
10517 /* EXPORT for RIF:
10518 Set a nominal cursor position.
10519
10520 HPOS and VPOS are column/row positions in a window glyph matrix. X
10521 and Y are window text area relative pixel positions.
10522
10523 If this is done during an update, updated_window will contain the
10524 window that is being updated and the position is the future output
10525 cursor position for that window. If updated_window is null, use
10526 selected_window and display the cursor at the given position. */
10527
10528 void
10529 x_cursor_to (int vpos, int hpos, int y, int x)
10530 {
10531 struct window *w;
10532
10533 /* If updated_window is not set, work on selected_window. */
10534 if (updated_window)
10535 w = updated_window;
10536 else
10537 w = XWINDOW (selected_window);
10538
10539 /* Set the output cursor. */
10540 output_cursor.hpos = hpos;
10541 output_cursor.vpos = vpos;
10542 output_cursor.x = x;
10543 output_cursor.y = y;
10544
10545 /* If not called as part of an update, really display the cursor.
10546 This will also set the cursor position of W. */
10547 if (updated_window == NULL)
10548 {
10549 BLOCK_INPUT;
10550 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10551 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10552 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10553 UNBLOCK_INPUT;
10554 }
10555 }
10556
10557 #endif /* HAVE_WINDOW_SYSTEM */
10558
10559 \f
10560 /***********************************************************************
10561 Tool-bars
10562 ***********************************************************************/
10563
10564 #ifdef HAVE_WINDOW_SYSTEM
10565
10566 /* Where the mouse was last time we reported a mouse event. */
10567
10568 FRAME_PTR last_mouse_frame;
10569
10570 /* Tool-bar item index of the item on which a mouse button was pressed
10571 or -1. */
10572
10573 int last_tool_bar_item;
10574
10575
10576 static Lisp_Object
10577 update_tool_bar_unwind (Lisp_Object frame)
10578 {
10579 selected_frame = frame;
10580 return Qnil;
10581 }
10582
10583 /* Update the tool-bar item list for frame F. This has to be done
10584 before we start to fill in any display lines. Called from
10585 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10586 and restore it here. */
10587
10588 static void
10589 update_tool_bar (struct frame *f, int save_match_data)
10590 {
10591 #if defined (USE_GTK) || defined (HAVE_NS)
10592 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10593 #else
10594 int do_update = WINDOWP (f->tool_bar_window)
10595 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10596 #endif
10597
10598 if (do_update)
10599 {
10600 Lisp_Object window;
10601 struct window *w;
10602
10603 window = FRAME_SELECTED_WINDOW (f);
10604 w = XWINDOW (window);
10605
10606 /* If the user has switched buffers or windows, we need to
10607 recompute to reflect the new bindings. But we'll
10608 recompute when update_mode_lines is set too; that means
10609 that people can use force-mode-line-update to request
10610 that the menu bar be recomputed. The adverse effect on
10611 the rest of the redisplay algorithm is about the same as
10612 windows_or_buffers_changed anyway. */
10613 if (windows_or_buffers_changed
10614 || !NILP (w->update_mode_line)
10615 || update_mode_lines
10616 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10617 < BUF_MODIFF (XBUFFER (w->buffer)))
10618 != !NILP (w->last_had_star))
10619 || ((!NILP (Vtransient_mark_mode)
10620 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10621 != !NILP (w->region_showing)))
10622 {
10623 struct buffer *prev = current_buffer;
10624 int count = SPECPDL_INDEX ();
10625 Lisp_Object frame, new_tool_bar;
10626 int new_n_tool_bar;
10627 struct gcpro gcpro1;
10628
10629 /* Set current_buffer to the buffer of the selected
10630 window of the frame, so that we get the right local
10631 keymaps. */
10632 set_buffer_internal_1 (XBUFFER (w->buffer));
10633
10634 /* Save match data, if we must. */
10635 if (save_match_data)
10636 record_unwind_save_match_data ();
10637
10638 /* Make sure that we don't accidentally use bogus keymaps. */
10639 if (NILP (Voverriding_local_map_menu_flag))
10640 {
10641 specbind (Qoverriding_terminal_local_map, Qnil);
10642 specbind (Qoverriding_local_map, Qnil);
10643 }
10644
10645 GCPRO1 (new_tool_bar);
10646
10647 /* We must temporarily set the selected frame to this frame
10648 before calling tool_bar_items, because the calculation of
10649 the tool-bar keymap uses the selected frame (see
10650 `tool-bar-make-keymap' in tool-bar.el). */
10651 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10652 XSETFRAME (frame, f);
10653 selected_frame = frame;
10654
10655 /* Build desired tool-bar items from keymaps. */
10656 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10657 &new_n_tool_bar);
10658
10659 /* Redisplay the tool-bar if we changed it. */
10660 if (new_n_tool_bar != f->n_tool_bar_items
10661 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10662 {
10663 /* Redisplay that happens asynchronously due to an expose event
10664 may access f->tool_bar_items. Make sure we update both
10665 variables within BLOCK_INPUT so no such event interrupts. */
10666 BLOCK_INPUT;
10667 f->tool_bar_items = new_tool_bar;
10668 f->n_tool_bar_items = new_n_tool_bar;
10669 w->update_mode_line = Qt;
10670 UNBLOCK_INPUT;
10671 }
10672
10673 UNGCPRO;
10674
10675 unbind_to (count, Qnil);
10676 set_buffer_internal_1 (prev);
10677 }
10678 }
10679 }
10680
10681
10682 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10683 F's desired tool-bar contents. F->tool_bar_items must have
10684 been set up previously by calling prepare_menu_bars. */
10685
10686 static void
10687 build_desired_tool_bar_string (struct frame *f)
10688 {
10689 int i, size, size_needed;
10690 struct gcpro gcpro1, gcpro2, gcpro3;
10691 Lisp_Object image, plist, props;
10692
10693 image = plist = props = Qnil;
10694 GCPRO3 (image, plist, props);
10695
10696 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10697 Otherwise, make a new string. */
10698
10699 /* The size of the string we might be able to reuse. */
10700 size = (STRINGP (f->desired_tool_bar_string)
10701 ? SCHARS (f->desired_tool_bar_string)
10702 : 0);
10703
10704 /* We need one space in the string for each image. */
10705 size_needed = f->n_tool_bar_items;
10706
10707 /* Reuse f->desired_tool_bar_string, if possible. */
10708 if (size < size_needed || NILP (f->desired_tool_bar_string))
10709 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10710 make_number (' '));
10711 else
10712 {
10713 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10714 Fremove_text_properties (make_number (0), make_number (size),
10715 props, f->desired_tool_bar_string);
10716 }
10717
10718 /* Put a `display' property on the string for the images to display,
10719 put a `menu_item' property on tool-bar items with a value that
10720 is the index of the item in F's tool-bar item vector. */
10721 for (i = 0; i < f->n_tool_bar_items; ++i)
10722 {
10723 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10724
10725 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10726 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10727 int hmargin, vmargin, relief, idx, end;
10728
10729 /* If image is a vector, choose the image according to the
10730 button state. */
10731 image = PROP (TOOL_BAR_ITEM_IMAGES);
10732 if (VECTORP (image))
10733 {
10734 if (enabled_p)
10735 idx = (selected_p
10736 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10737 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10738 else
10739 idx = (selected_p
10740 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10741 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10742
10743 xassert (ASIZE (image) >= idx);
10744 image = AREF (image, idx);
10745 }
10746 else
10747 idx = -1;
10748
10749 /* Ignore invalid image specifications. */
10750 if (!valid_image_p (image))
10751 continue;
10752
10753 /* Display the tool-bar button pressed, or depressed. */
10754 plist = Fcopy_sequence (XCDR (image));
10755
10756 /* Compute margin and relief to draw. */
10757 relief = (tool_bar_button_relief >= 0
10758 ? tool_bar_button_relief
10759 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10760 hmargin = vmargin = relief;
10761
10762 if (INTEGERP (Vtool_bar_button_margin)
10763 && XINT (Vtool_bar_button_margin) > 0)
10764 {
10765 hmargin += XFASTINT (Vtool_bar_button_margin);
10766 vmargin += XFASTINT (Vtool_bar_button_margin);
10767 }
10768 else if (CONSP (Vtool_bar_button_margin))
10769 {
10770 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10771 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10772 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10773
10774 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10775 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10776 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10777 }
10778
10779 if (auto_raise_tool_bar_buttons_p)
10780 {
10781 /* Add a `:relief' property to the image spec if the item is
10782 selected. */
10783 if (selected_p)
10784 {
10785 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10786 hmargin -= relief;
10787 vmargin -= relief;
10788 }
10789 }
10790 else
10791 {
10792 /* If image is selected, display it pressed, i.e. with a
10793 negative relief. If it's not selected, display it with a
10794 raised relief. */
10795 plist = Fplist_put (plist, QCrelief,
10796 (selected_p
10797 ? make_number (-relief)
10798 : make_number (relief)));
10799 hmargin -= relief;
10800 vmargin -= relief;
10801 }
10802
10803 /* Put a margin around the image. */
10804 if (hmargin || vmargin)
10805 {
10806 if (hmargin == vmargin)
10807 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10808 else
10809 plist = Fplist_put (plist, QCmargin,
10810 Fcons (make_number (hmargin),
10811 make_number (vmargin)));
10812 }
10813
10814 /* If button is not enabled, and we don't have special images
10815 for the disabled state, make the image appear disabled by
10816 applying an appropriate algorithm to it. */
10817 if (!enabled_p && idx < 0)
10818 plist = Fplist_put (plist, QCconversion, Qdisabled);
10819
10820 /* Put a `display' text property on the string for the image to
10821 display. Put a `menu-item' property on the string that gives
10822 the start of this item's properties in the tool-bar items
10823 vector. */
10824 image = Fcons (Qimage, plist);
10825 props = list4 (Qdisplay, image,
10826 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10827
10828 /* Let the last image hide all remaining spaces in the tool bar
10829 string. The string can be longer than needed when we reuse a
10830 previous string. */
10831 if (i + 1 == f->n_tool_bar_items)
10832 end = SCHARS (f->desired_tool_bar_string);
10833 else
10834 end = i + 1;
10835 Fadd_text_properties (make_number (i), make_number (end),
10836 props, f->desired_tool_bar_string);
10837 #undef PROP
10838 }
10839
10840 UNGCPRO;
10841 }
10842
10843
10844 /* Display one line of the tool-bar of frame IT->f.
10845
10846 HEIGHT specifies the desired height of the tool-bar line.
10847 If the actual height of the glyph row is less than HEIGHT, the
10848 row's height is increased to HEIGHT, and the icons are centered
10849 vertically in the new height.
10850
10851 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10852 count a final empty row in case the tool-bar width exactly matches
10853 the window width.
10854 */
10855
10856 static void
10857 display_tool_bar_line (struct it *it, int height)
10858 {
10859 struct glyph_row *row = it->glyph_row;
10860 int max_x = it->last_visible_x;
10861 struct glyph *last;
10862
10863 prepare_desired_row (row);
10864 row->y = it->current_y;
10865
10866 /* Note that this isn't made use of if the face hasn't a box,
10867 so there's no need to check the face here. */
10868 it->start_of_box_run_p = 1;
10869
10870 while (it->current_x < max_x)
10871 {
10872 int x, n_glyphs_before, i, nglyphs;
10873 struct it it_before;
10874
10875 /* Get the next display element. */
10876 if (!get_next_display_element (it))
10877 {
10878 /* Don't count empty row if we are counting needed tool-bar lines. */
10879 if (height < 0 && !it->hpos)
10880 return;
10881 break;
10882 }
10883
10884 /* Produce glyphs. */
10885 n_glyphs_before = row->used[TEXT_AREA];
10886 it_before = *it;
10887
10888 PRODUCE_GLYPHS (it);
10889
10890 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10891 i = 0;
10892 x = it_before.current_x;
10893 while (i < nglyphs)
10894 {
10895 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10896
10897 if (x + glyph->pixel_width > max_x)
10898 {
10899 /* Glyph doesn't fit on line. Backtrack. */
10900 row->used[TEXT_AREA] = n_glyphs_before;
10901 *it = it_before;
10902 /* If this is the only glyph on this line, it will never fit on the
10903 tool-bar, so skip it. But ensure there is at least one glyph,
10904 so we don't accidentally disable the tool-bar. */
10905 if (n_glyphs_before == 0
10906 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10907 break;
10908 goto out;
10909 }
10910
10911 ++it->hpos;
10912 x += glyph->pixel_width;
10913 ++i;
10914 }
10915
10916 /* Stop at line end. */
10917 if (ITERATOR_AT_END_OF_LINE_P (it))
10918 break;
10919
10920 set_iterator_to_next (it, 1);
10921 }
10922
10923 out:;
10924
10925 row->displays_text_p = row->used[TEXT_AREA] != 0;
10926
10927 /* Use default face for the border below the tool bar.
10928
10929 FIXME: When auto-resize-tool-bars is grow-only, there is
10930 no additional border below the possibly empty tool-bar lines.
10931 So to make the extra empty lines look "normal", we have to
10932 use the tool-bar face for the border too. */
10933 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10934 it->face_id = DEFAULT_FACE_ID;
10935
10936 extend_face_to_end_of_line (it);
10937 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10938 last->right_box_line_p = 1;
10939 if (last == row->glyphs[TEXT_AREA])
10940 last->left_box_line_p = 1;
10941
10942 /* Make line the desired height and center it vertically. */
10943 if ((height -= it->max_ascent + it->max_descent) > 0)
10944 {
10945 /* Don't add more than one line height. */
10946 height %= FRAME_LINE_HEIGHT (it->f);
10947 it->max_ascent += height / 2;
10948 it->max_descent += (height + 1) / 2;
10949 }
10950
10951 compute_line_metrics (it);
10952
10953 /* If line is empty, make it occupy the rest of the tool-bar. */
10954 if (!row->displays_text_p)
10955 {
10956 row->height = row->phys_height = it->last_visible_y - row->y;
10957 row->visible_height = row->height;
10958 row->ascent = row->phys_ascent = 0;
10959 row->extra_line_spacing = 0;
10960 }
10961
10962 row->full_width_p = 1;
10963 row->continued_p = 0;
10964 row->truncated_on_left_p = 0;
10965 row->truncated_on_right_p = 0;
10966
10967 it->current_x = it->hpos = 0;
10968 it->current_y += row->height;
10969 ++it->vpos;
10970 ++it->glyph_row;
10971 }
10972
10973
10974 /* Max tool-bar height. */
10975
10976 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10977 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10978
10979 /* Value is the number of screen lines needed to make all tool-bar
10980 items of frame F visible. The number of actual rows needed is
10981 returned in *N_ROWS if non-NULL. */
10982
10983 static int
10984 tool_bar_lines_needed (struct frame *f, int *n_rows)
10985 {
10986 struct window *w = XWINDOW (f->tool_bar_window);
10987 struct it it;
10988 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10989 the desired matrix, so use (unused) mode-line row as temporary row to
10990 avoid destroying the first tool-bar row. */
10991 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10992
10993 /* Initialize an iterator for iteration over
10994 F->desired_tool_bar_string in the tool-bar window of frame F. */
10995 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10996 it.first_visible_x = 0;
10997 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10998 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10999 it.paragraph_embedding = L2R;
11000
11001 while (!ITERATOR_AT_END_P (&it))
11002 {
11003 clear_glyph_row (temp_row);
11004 it.glyph_row = temp_row;
11005 display_tool_bar_line (&it, -1);
11006 }
11007 clear_glyph_row (temp_row);
11008
11009 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11010 if (n_rows)
11011 *n_rows = it.vpos > 0 ? it.vpos : -1;
11012
11013 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11014 }
11015
11016
11017 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11018 0, 1, 0,
11019 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11020 (Lisp_Object frame)
11021 {
11022 struct frame *f;
11023 struct window *w;
11024 int nlines = 0;
11025
11026 if (NILP (frame))
11027 frame = selected_frame;
11028 else
11029 CHECK_FRAME (frame);
11030 f = XFRAME (frame);
11031
11032 if (WINDOWP (f->tool_bar_window)
11033 || (w = XWINDOW (f->tool_bar_window),
11034 WINDOW_TOTAL_LINES (w) > 0))
11035 {
11036 update_tool_bar (f, 1);
11037 if (f->n_tool_bar_items)
11038 {
11039 build_desired_tool_bar_string (f);
11040 nlines = tool_bar_lines_needed (f, NULL);
11041 }
11042 }
11043
11044 return make_number (nlines);
11045 }
11046
11047
11048 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11049 height should be changed. */
11050
11051 static int
11052 redisplay_tool_bar (struct frame *f)
11053 {
11054 struct window *w;
11055 struct it it;
11056 struct glyph_row *row;
11057
11058 #if defined (USE_GTK) || defined (HAVE_NS)
11059 if (FRAME_EXTERNAL_TOOL_BAR (f))
11060 update_frame_tool_bar (f);
11061 return 0;
11062 #endif
11063
11064 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11065 do anything. This means you must start with tool-bar-lines
11066 non-zero to get the auto-sizing effect. Or in other words, you
11067 can turn off tool-bars by specifying tool-bar-lines zero. */
11068 if (!WINDOWP (f->tool_bar_window)
11069 || (w = XWINDOW (f->tool_bar_window),
11070 WINDOW_TOTAL_LINES (w) == 0))
11071 return 0;
11072
11073 /* Set up an iterator for the tool-bar window. */
11074 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11075 it.first_visible_x = 0;
11076 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11077 row = it.glyph_row;
11078
11079 /* Build a string that represents the contents of the tool-bar. */
11080 build_desired_tool_bar_string (f);
11081 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11082 /* FIXME: This should be controlled by a user option. But it
11083 doesn't make sense to have an R2L tool bar if the menu bar cannot
11084 be drawn also R2L, and making the menu bar R2L is tricky due to
11085 unibyte strings it uses and toolkit-specific code that implements
11086 it. If an R2L tool bar is ever supported, display_tool_bar_line
11087 should also be augmented to call unproduce_glyphs like
11088 display_line and display_string do. */
11089 it.paragraph_embedding = L2R;
11090
11091 if (f->n_tool_bar_rows == 0)
11092 {
11093 int nlines;
11094
11095 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11096 nlines != WINDOW_TOTAL_LINES (w)))
11097 {
11098 Lisp_Object frame;
11099 int old_height = WINDOW_TOTAL_LINES (w);
11100
11101 XSETFRAME (frame, f);
11102 Fmodify_frame_parameters (frame,
11103 Fcons (Fcons (Qtool_bar_lines,
11104 make_number (nlines)),
11105 Qnil));
11106 if (WINDOW_TOTAL_LINES (w) != old_height)
11107 {
11108 clear_glyph_matrix (w->desired_matrix);
11109 fonts_changed_p = 1;
11110 return 1;
11111 }
11112 }
11113 }
11114
11115 /* Display as many lines as needed to display all tool-bar items. */
11116
11117 if (f->n_tool_bar_rows > 0)
11118 {
11119 int border, rows, height, extra;
11120
11121 if (INTEGERP (Vtool_bar_border))
11122 border = XINT (Vtool_bar_border);
11123 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11124 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11125 else if (EQ (Vtool_bar_border, Qborder_width))
11126 border = f->border_width;
11127 else
11128 border = 0;
11129 if (border < 0)
11130 border = 0;
11131
11132 rows = f->n_tool_bar_rows;
11133 height = max (1, (it.last_visible_y - border) / rows);
11134 extra = it.last_visible_y - border - height * rows;
11135
11136 while (it.current_y < it.last_visible_y)
11137 {
11138 int h = 0;
11139 if (extra > 0 && rows-- > 0)
11140 {
11141 h = (extra + rows - 1) / rows;
11142 extra -= h;
11143 }
11144 display_tool_bar_line (&it, height + h);
11145 }
11146 }
11147 else
11148 {
11149 while (it.current_y < it.last_visible_y)
11150 display_tool_bar_line (&it, 0);
11151 }
11152
11153 /* It doesn't make much sense to try scrolling in the tool-bar
11154 window, so don't do it. */
11155 w->desired_matrix->no_scrolling_p = 1;
11156 w->must_be_updated_p = 1;
11157
11158 if (!NILP (Vauto_resize_tool_bars))
11159 {
11160 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11161 int change_height_p = 0;
11162
11163 /* If we couldn't display everything, change the tool-bar's
11164 height if there is room for more. */
11165 if (IT_STRING_CHARPOS (it) < it.end_charpos
11166 && it.current_y < max_tool_bar_height)
11167 change_height_p = 1;
11168
11169 row = it.glyph_row - 1;
11170
11171 /* If there are blank lines at the end, except for a partially
11172 visible blank line at the end that is smaller than
11173 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11174 if (!row->displays_text_p
11175 && row->height >= FRAME_LINE_HEIGHT (f))
11176 change_height_p = 1;
11177
11178 /* If row displays tool-bar items, but is partially visible,
11179 change the tool-bar's height. */
11180 if (row->displays_text_p
11181 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11182 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11183 change_height_p = 1;
11184
11185 /* Resize windows as needed by changing the `tool-bar-lines'
11186 frame parameter. */
11187 if (change_height_p)
11188 {
11189 Lisp_Object frame;
11190 int old_height = WINDOW_TOTAL_LINES (w);
11191 int nrows;
11192 int nlines = tool_bar_lines_needed (f, &nrows);
11193
11194 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11195 && !f->minimize_tool_bar_window_p)
11196 ? (nlines > old_height)
11197 : (nlines != old_height));
11198 f->minimize_tool_bar_window_p = 0;
11199
11200 if (change_height_p)
11201 {
11202 XSETFRAME (frame, f);
11203 Fmodify_frame_parameters (frame,
11204 Fcons (Fcons (Qtool_bar_lines,
11205 make_number (nlines)),
11206 Qnil));
11207 if (WINDOW_TOTAL_LINES (w) != old_height)
11208 {
11209 clear_glyph_matrix (w->desired_matrix);
11210 f->n_tool_bar_rows = nrows;
11211 fonts_changed_p = 1;
11212 return 1;
11213 }
11214 }
11215 }
11216 }
11217
11218 f->minimize_tool_bar_window_p = 0;
11219 return 0;
11220 }
11221
11222
11223 /* Get information about the tool-bar item which is displayed in GLYPH
11224 on frame F. Return in *PROP_IDX the index where tool-bar item
11225 properties start in F->tool_bar_items. Value is zero if
11226 GLYPH doesn't display a tool-bar item. */
11227
11228 static int
11229 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11230 {
11231 Lisp_Object prop;
11232 int success_p;
11233 int charpos;
11234
11235 /* This function can be called asynchronously, which means we must
11236 exclude any possibility that Fget_text_property signals an
11237 error. */
11238 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11239 charpos = max (0, charpos);
11240
11241 /* Get the text property `menu-item' at pos. The value of that
11242 property is the start index of this item's properties in
11243 F->tool_bar_items. */
11244 prop = Fget_text_property (make_number (charpos),
11245 Qmenu_item, f->current_tool_bar_string);
11246 if (INTEGERP (prop))
11247 {
11248 *prop_idx = XINT (prop);
11249 success_p = 1;
11250 }
11251 else
11252 success_p = 0;
11253
11254 return success_p;
11255 }
11256
11257 \f
11258 /* Get information about the tool-bar item at position X/Y on frame F.
11259 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11260 the current matrix of the tool-bar window of F, or NULL if not
11261 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11262 item in F->tool_bar_items. Value is
11263
11264 -1 if X/Y is not on a tool-bar item
11265 0 if X/Y is on the same item that was highlighted before.
11266 1 otherwise. */
11267
11268 static int
11269 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11270 int *hpos, int *vpos, int *prop_idx)
11271 {
11272 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11273 struct window *w = XWINDOW (f->tool_bar_window);
11274 int area;
11275
11276 /* Find the glyph under X/Y. */
11277 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11278 if (*glyph == NULL)
11279 return -1;
11280
11281 /* Get the start of this tool-bar item's properties in
11282 f->tool_bar_items. */
11283 if (!tool_bar_item_info (f, *glyph, prop_idx))
11284 return -1;
11285
11286 /* Is mouse on the highlighted item? */
11287 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11288 && *vpos >= hlinfo->mouse_face_beg_row
11289 && *vpos <= hlinfo->mouse_face_end_row
11290 && (*vpos > hlinfo->mouse_face_beg_row
11291 || *hpos >= hlinfo->mouse_face_beg_col)
11292 && (*vpos < hlinfo->mouse_face_end_row
11293 || *hpos < hlinfo->mouse_face_end_col
11294 || hlinfo->mouse_face_past_end))
11295 return 0;
11296
11297 return 1;
11298 }
11299
11300
11301 /* EXPORT:
11302 Handle mouse button event on the tool-bar of frame F, at
11303 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11304 0 for button release. MODIFIERS is event modifiers for button
11305 release. */
11306
11307 void
11308 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11309 unsigned int modifiers)
11310 {
11311 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11312 struct window *w = XWINDOW (f->tool_bar_window);
11313 int hpos, vpos, prop_idx;
11314 struct glyph *glyph;
11315 Lisp_Object enabled_p;
11316
11317 /* If not on the highlighted tool-bar item, return. */
11318 frame_to_window_pixel_xy (w, &x, &y);
11319 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11320 return;
11321
11322 /* If item is disabled, do nothing. */
11323 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11324 if (NILP (enabled_p))
11325 return;
11326
11327 if (down_p)
11328 {
11329 /* Show item in pressed state. */
11330 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11331 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11332 last_tool_bar_item = prop_idx;
11333 }
11334 else
11335 {
11336 Lisp_Object key, frame;
11337 struct input_event event;
11338 EVENT_INIT (event);
11339
11340 /* Show item in released state. */
11341 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11342 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11343
11344 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11345
11346 XSETFRAME (frame, f);
11347 event.kind = TOOL_BAR_EVENT;
11348 event.frame_or_window = frame;
11349 event.arg = frame;
11350 kbd_buffer_store_event (&event);
11351
11352 event.kind = TOOL_BAR_EVENT;
11353 event.frame_or_window = frame;
11354 event.arg = key;
11355 event.modifiers = modifiers;
11356 kbd_buffer_store_event (&event);
11357 last_tool_bar_item = -1;
11358 }
11359 }
11360
11361
11362 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11363 tool-bar window-relative coordinates X/Y. Called from
11364 note_mouse_highlight. */
11365
11366 static void
11367 note_tool_bar_highlight (struct frame *f, int x, int y)
11368 {
11369 Lisp_Object window = f->tool_bar_window;
11370 struct window *w = XWINDOW (window);
11371 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11372 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11373 int hpos, vpos;
11374 struct glyph *glyph;
11375 struct glyph_row *row;
11376 int i;
11377 Lisp_Object enabled_p;
11378 int prop_idx;
11379 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11380 int mouse_down_p, rc;
11381
11382 /* Function note_mouse_highlight is called with negative X/Y
11383 values when mouse moves outside of the frame. */
11384 if (x <= 0 || y <= 0)
11385 {
11386 clear_mouse_face (hlinfo);
11387 return;
11388 }
11389
11390 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11391 if (rc < 0)
11392 {
11393 /* Not on tool-bar item. */
11394 clear_mouse_face (hlinfo);
11395 return;
11396 }
11397 else if (rc == 0)
11398 /* On same tool-bar item as before. */
11399 goto set_help_echo;
11400
11401 clear_mouse_face (hlinfo);
11402
11403 /* Mouse is down, but on different tool-bar item? */
11404 mouse_down_p = (dpyinfo->grabbed
11405 && f == last_mouse_frame
11406 && FRAME_LIVE_P (f));
11407 if (mouse_down_p
11408 && last_tool_bar_item != prop_idx)
11409 return;
11410
11411 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11412 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11413
11414 /* If tool-bar item is not enabled, don't highlight it. */
11415 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11416 if (!NILP (enabled_p))
11417 {
11418 /* Compute the x-position of the glyph. In front and past the
11419 image is a space. We include this in the highlighted area. */
11420 row = MATRIX_ROW (w->current_matrix, vpos);
11421 for (i = x = 0; i < hpos; ++i)
11422 x += row->glyphs[TEXT_AREA][i].pixel_width;
11423
11424 /* Record this as the current active region. */
11425 hlinfo->mouse_face_beg_col = hpos;
11426 hlinfo->mouse_face_beg_row = vpos;
11427 hlinfo->mouse_face_beg_x = x;
11428 hlinfo->mouse_face_beg_y = row->y;
11429 hlinfo->mouse_face_past_end = 0;
11430
11431 hlinfo->mouse_face_end_col = hpos + 1;
11432 hlinfo->mouse_face_end_row = vpos;
11433 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11434 hlinfo->mouse_face_end_y = row->y;
11435 hlinfo->mouse_face_window = window;
11436 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11437
11438 /* Display it as active. */
11439 show_mouse_face (hlinfo, draw);
11440 hlinfo->mouse_face_image_state = draw;
11441 }
11442
11443 set_help_echo:
11444
11445 /* Set help_echo_string to a help string to display for this tool-bar item.
11446 XTread_socket does the rest. */
11447 help_echo_object = help_echo_window = Qnil;
11448 help_echo_pos = -1;
11449 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11450 if (NILP (help_echo_string))
11451 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11452 }
11453
11454 #endif /* HAVE_WINDOW_SYSTEM */
11455
11456
11457 \f
11458 /************************************************************************
11459 Horizontal scrolling
11460 ************************************************************************/
11461
11462 static int hscroll_window_tree (Lisp_Object);
11463 static int hscroll_windows (Lisp_Object);
11464
11465 /* For all leaf windows in the window tree rooted at WINDOW, set their
11466 hscroll value so that PT is (i) visible in the window, and (ii) so
11467 that it is not within a certain margin at the window's left and
11468 right border. Value is non-zero if any window's hscroll has been
11469 changed. */
11470
11471 static int
11472 hscroll_window_tree (Lisp_Object window)
11473 {
11474 int hscrolled_p = 0;
11475 int hscroll_relative_p = FLOATP (Vhscroll_step);
11476 int hscroll_step_abs = 0;
11477 double hscroll_step_rel = 0;
11478
11479 if (hscroll_relative_p)
11480 {
11481 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11482 if (hscroll_step_rel < 0)
11483 {
11484 hscroll_relative_p = 0;
11485 hscroll_step_abs = 0;
11486 }
11487 }
11488 else if (INTEGERP (Vhscroll_step))
11489 {
11490 hscroll_step_abs = XINT (Vhscroll_step);
11491 if (hscroll_step_abs < 0)
11492 hscroll_step_abs = 0;
11493 }
11494 else
11495 hscroll_step_abs = 0;
11496
11497 while (WINDOWP (window))
11498 {
11499 struct window *w = XWINDOW (window);
11500
11501 if (WINDOWP (w->hchild))
11502 hscrolled_p |= hscroll_window_tree (w->hchild);
11503 else if (WINDOWP (w->vchild))
11504 hscrolled_p |= hscroll_window_tree (w->vchild);
11505 else if (w->cursor.vpos >= 0)
11506 {
11507 int h_margin;
11508 int text_area_width;
11509 struct glyph_row *current_cursor_row
11510 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11511 struct glyph_row *desired_cursor_row
11512 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11513 struct glyph_row *cursor_row
11514 = (desired_cursor_row->enabled_p
11515 ? desired_cursor_row
11516 : current_cursor_row);
11517
11518 text_area_width = window_box_width (w, TEXT_AREA);
11519
11520 /* Scroll when cursor is inside this scroll margin. */
11521 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11522
11523 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11524 && ((XFASTINT (w->hscroll)
11525 && w->cursor.x <= h_margin)
11526 || (cursor_row->enabled_p
11527 && cursor_row->truncated_on_right_p
11528 && (w->cursor.x >= text_area_width - h_margin))))
11529 {
11530 struct it it;
11531 int hscroll;
11532 struct buffer *saved_current_buffer;
11533 EMACS_INT pt;
11534 int wanted_x;
11535
11536 /* Find point in a display of infinite width. */
11537 saved_current_buffer = current_buffer;
11538 current_buffer = XBUFFER (w->buffer);
11539
11540 if (w == XWINDOW (selected_window))
11541 pt = PT;
11542 else
11543 {
11544 pt = marker_position (w->pointm);
11545 pt = max (BEGV, pt);
11546 pt = min (ZV, pt);
11547 }
11548
11549 /* Move iterator to pt starting at cursor_row->start in
11550 a line with infinite width. */
11551 init_to_row_start (&it, w, cursor_row);
11552 it.last_visible_x = INFINITY;
11553 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11554 current_buffer = saved_current_buffer;
11555
11556 /* Position cursor in window. */
11557 if (!hscroll_relative_p && hscroll_step_abs == 0)
11558 hscroll = max (0, (it.current_x
11559 - (ITERATOR_AT_END_OF_LINE_P (&it)
11560 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11561 : (text_area_width / 2))))
11562 / FRAME_COLUMN_WIDTH (it.f);
11563 else if (w->cursor.x >= text_area_width - h_margin)
11564 {
11565 if (hscroll_relative_p)
11566 wanted_x = text_area_width * (1 - hscroll_step_rel)
11567 - h_margin;
11568 else
11569 wanted_x = text_area_width
11570 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11571 - h_margin;
11572 hscroll
11573 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11574 }
11575 else
11576 {
11577 if (hscroll_relative_p)
11578 wanted_x = text_area_width * hscroll_step_rel
11579 + h_margin;
11580 else
11581 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11582 + h_margin;
11583 hscroll
11584 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11585 }
11586 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11587
11588 /* Don't call Fset_window_hscroll if value hasn't
11589 changed because it will prevent redisplay
11590 optimizations. */
11591 if (XFASTINT (w->hscroll) != hscroll)
11592 {
11593 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11594 w->hscroll = make_number (hscroll);
11595 hscrolled_p = 1;
11596 }
11597 }
11598 }
11599
11600 window = w->next;
11601 }
11602
11603 /* Value is non-zero if hscroll of any leaf window has been changed. */
11604 return hscrolled_p;
11605 }
11606
11607
11608 /* Set hscroll so that cursor is visible and not inside horizontal
11609 scroll margins for all windows in the tree rooted at WINDOW. See
11610 also hscroll_window_tree above. Value is non-zero if any window's
11611 hscroll has been changed. If it has, desired matrices on the frame
11612 of WINDOW are cleared. */
11613
11614 static int
11615 hscroll_windows (Lisp_Object window)
11616 {
11617 int hscrolled_p = hscroll_window_tree (window);
11618 if (hscrolled_p)
11619 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11620 return hscrolled_p;
11621 }
11622
11623
11624 \f
11625 /************************************************************************
11626 Redisplay
11627 ************************************************************************/
11628
11629 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11630 to a non-zero value. This is sometimes handy to have in a debugger
11631 session. */
11632
11633 #if GLYPH_DEBUG
11634
11635 /* First and last unchanged row for try_window_id. */
11636
11637 int debug_first_unchanged_at_end_vpos;
11638 int debug_last_unchanged_at_beg_vpos;
11639
11640 /* Delta vpos and y. */
11641
11642 int debug_dvpos, debug_dy;
11643
11644 /* Delta in characters and bytes for try_window_id. */
11645
11646 EMACS_INT debug_delta, debug_delta_bytes;
11647
11648 /* Values of window_end_pos and window_end_vpos at the end of
11649 try_window_id. */
11650
11651 EMACS_INT debug_end_vpos;
11652
11653 /* Append a string to W->desired_matrix->method. FMT is a printf
11654 format string. A1...A9 are a supplement for a variable-length
11655 argument list. If trace_redisplay_p is non-zero also printf the
11656 resulting string to stderr. */
11657
11658 static void
11659 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11660 struct window *w;
11661 char *fmt;
11662 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11663 {
11664 char buffer[512];
11665 char *method = w->desired_matrix->method;
11666 int len = strlen (method);
11667 int size = sizeof w->desired_matrix->method;
11668 int remaining = size - len - 1;
11669
11670 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11671 if (len && remaining)
11672 {
11673 method[len] = '|';
11674 --remaining, ++len;
11675 }
11676
11677 strncpy (method + len, buffer, remaining);
11678
11679 if (trace_redisplay_p)
11680 fprintf (stderr, "%p (%s): %s\n",
11681 w,
11682 ((BUFFERP (w->buffer)
11683 && STRINGP (XBUFFER (w->buffer)->name))
11684 ? SSDATA (XBUFFER (w->buffer)->name)
11685 : "no buffer"),
11686 buffer);
11687 }
11688
11689 #endif /* GLYPH_DEBUG */
11690
11691
11692 /* Value is non-zero if all changes in window W, which displays
11693 current_buffer, are in the text between START and END. START is a
11694 buffer position, END is given as a distance from Z. Used in
11695 redisplay_internal for display optimization. */
11696
11697 static INLINE int
11698 text_outside_line_unchanged_p (struct window *w,
11699 EMACS_INT start, EMACS_INT end)
11700 {
11701 int unchanged_p = 1;
11702
11703 /* If text or overlays have changed, see where. */
11704 if (XFASTINT (w->last_modified) < MODIFF
11705 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11706 {
11707 /* Gap in the line? */
11708 if (GPT < start || Z - GPT < end)
11709 unchanged_p = 0;
11710
11711 /* Changes start in front of the line, or end after it? */
11712 if (unchanged_p
11713 && (BEG_UNCHANGED < start - 1
11714 || END_UNCHANGED < end))
11715 unchanged_p = 0;
11716
11717 /* If selective display, can't optimize if changes start at the
11718 beginning of the line. */
11719 if (unchanged_p
11720 && INTEGERP (BVAR (current_buffer, selective_display))
11721 && XINT (BVAR (current_buffer, selective_display)) > 0
11722 && (BEG_UNCHANGED < start || GPT <= start))
11723 unchanged_p = 0;
11724
11725 /* If there are overlays at the start or end of the line, these
11726 may have overlay strings with newlines in them. A change at
11727 START, for instance, may actually concern the display of such
11728 overlay strings as well, and they are displayed on different
11729 lines. So, quickly rule out this case. (For the future, it
11730 might be desirable to implement something more telling than
11731 just BEG/END_UNCHANGED.) */
11732 if (unchanged_p)
11733 {
11734 if (BEG + BEG_UNCHANGED == start
11735 && overlay_touches_p (start))
11736 unchanged_p = 0;
11737 if (END_UNCHANGED == end
11738 && overlay_touches_p (Z - end))
11739 unchanged_p = 0;
11740 }
11741
11742 /* Under bidi reordering, adding or deleting a character in the
11743 beginning of a paragraph, before the first strong directional
11744 character, can change the base direction of the paragraph (unless
11745 the buffer specifies a fixed paragraph direction), which will
11746 require to redisplay the whole paragraph. It might be worthwhile
11747 to find the paragraph limits and widen the range of redisplayed
11748 lines to that, but for now just give up this optimization. */
11749 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11750 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11751 unchanged_p = 0;
11752 }
11753
11754 return unchanged_p;
11755 }
11756
11757
11758 /* Do a frame update, taking possible shortcuts into account. This is
11759 the main external entry point for redisplay.
11760
11761 If the last redisplay displayed an echo area message and that message
11762 is no longer requested, we clear the echo area or bring back the
11763 mini-buffer if that is in use. */
11764
11765 void
11766 redisplay (void)
11767 {
11768 redisplay_internal ();
11769 }
11770
11771
11772 static Lisp_Object
11773 overlay_arrow_string_or_property (Lisp_Object var)
11774 {
11775 Lisp_Object val;
11776
11777 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11778 return val;
11779
11780 return Voverlay_arrow_string;
11781 }
11782
11783 /* Return 1 if there are any overlay-arrows in current_buffer. */
11784 static int
11785 overlay_arrow_in_current_buffer_p (void)
11786 {
11787 Lisp_Object vlist;
11788
11789 for (vlist = Voverlay_arrow_variable_list;
11790 CONSP (vlist);
11791 vlist = XCDR (vlist))
11792 {
11793 Lisp_Object var = XCAR (vlist);
11794 Lisp_Object val;
11795
11796 if (!SYMBOLP (var))
11797 continue;
11798 val = find_symbol_value (var);
11799 if (MARKERP (val)
11800 && current_buffer == XMARKER (val)->buffer)
11801 return 1;
11802 }
11803 return 0;
11804 }
11805
11806
11807 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11808 has changed. */
11809
11810 static int
11811 overlay_arrows_changed_p (void)
11812 {
11813 Lisp_Object vlist;
11814
11815 for (vlist = Voverlay_arrow_variable_list;
11816 CONSP (vlist);
11817 vlist = XCDR (vlist))
11818 {
11819 Lisp_Object var = XCAR (vlist);
11820 Lisp_Object val, pstr;
11821
11822 if (!SYMBOLP (var))
11823 continue;
11824 val = find_symbol_value (var);
11825 if (!MARKERP (val))
11826 continue;
11827 if (! EQ (COERCE_MARKER (val),
11828 Fget (var, Qlast_arrow_position))
11829 || ! (pstr = overlay_arrow_string_or_property (var),
11830 EQ (pstr, Fget (var, Qlast_arrow_string))))
11831 return 1;
11832 }
11833 return 0;
11834 }
11835
11836 /* Mark overlay arrows to be updated on next redisplay. */
11837
11838 static void
11839 update_overlay_arrows (int up_to_date)
11840 {
11841 Lisp_Object vlist;
11842
11843 for (vlist = Voverlay_arrow_variable_list;
11844 CONSP (vlist);
11845 vlist = XCDR (vlist))
11846 {
11847 Lisp_Object var = XCAR (vlist);
11848
11849 if (!SYMBOLP (var))
11850 continue;
11851
11852 if (up_to_date > 0)
11853 {
11854 Lisp_Object val = find_symbol_value (var);
11855 Fput (var, Qlast_arrow_position,
11856 COERCE_MARKER (val));
11857 Fput (var, Qlast_arrow_string,
11858 overlay_arrow_string_or_property (var));
11859 }
11860 else if (up_to_date < 0
11861 || !NILP (Fget (var, Qlast_arrow_position)))
11862 {
11863 Fput (var, Qlast_arrow_position, Qt);
11864 Fput (var, Qlast_arrow_string, Qt);
11865 }
11866 }
11867 }
11868
11869
11870 /* Return overlay arrow string to display at row.
11871 Return integer (bitmap number) for arrow bitmap in left fringe.
11872 Return nil if no overlay arrow. */
11873
11874 static Lisp_Object
11875 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11876 {
11877 Lisp_Object vlist;
11878
11879 for (vlist = Voverlay_arrow_variable_list;
11880 CONSP (vlist);
11881 vlist = XCDR (vlist))
11882 {
11883 Lisp_Object var = XCAR (vlist);
11884 Lisp_Object val;
11885
11886 if (!SYMBOLP (var))
11887 continue;
11888
11889 val = find_symbol_value (var);
11890
11891 if (MARKERP (val)
11892 && current_buffer == XMARKER (val)->buffer
11893 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11894 {
11895 if (FRAME_WINDOW_P (it->f)
11896 /* FIXME: if ROW->reversed_p is set, this should test
11897 the right fringe, not the left one. */
11898 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11899 {
11900 #ifdef HAVE_WINDOW_SYSTEM
11901 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11902 {
11903 int fringe_bitmap;
11904 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11905 return make_number (fringe_bitmap);
11906 }
11907 #endif
11908 return make_number (-1); /* Use default arrow bitmap */
11909 }
11910 return overlay_arrow_string_or_property (var);
11911 }
11912 }
11913
11914 return Qnil;
11915 }
11916
11917 /* Return 1 if point moved out of or into a composition. Otherwise
11918 return 0. PREV_BUF and PREV_PT are the last point buffer and
11919 position. BUF and PT are the current point buffer and position. */
11920
11921 static int
11922 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11923 struct buffer *buf, EMACS_INT pt)
11924 {
11925 EMACS_INT start, end;
11926 Lisp_Object prop;
11927 Lisp_Object buffer;
11928
11929 XSETBUFFER (buffer, buf);
11930 /* Check a composition at the last point if point moved within the
11931 same buffer. */
11932 if (prev_buf == buf)
11933 {
11934 if (prev_pt == pt)
11935 /* Point didn't move. */
11936 return 0;
11937
11938 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11939 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11940 && COMPOSITION_VALID_P (start, end, prop)
11941 && start < prev_pt && end > prev_pt)
11942 /* The last point was within the composition. Return 1 iff
11943 point moved out of the composition. */
11944 return (pt <= start || pt >= end);
11945 }
11946
11947 /* Check a composition at the current point. */
11948 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11949 && find_composition (pt, -1, &start, &end, &prop, buffer)
11950 && COMPOSITION_VALID_P (start, end, prop)
11951 && start < pt && end > pt);
11952 }
11953
11954
11955 /* Reconsider the setting of B->clip_changed which is displayed
11956 in window W. */
11957
11958 static INLINE void
11959 reconsider_clip_changes (struct window *w, struct buffer *b)
11960 {
11961 if (b->clip_changed
11962 && !NILP (w->window_end_valid)
11963 && w->current_matrix->buffer == b
11964 && w->current_matrix->zv == BUF_ZV (b)
11965 && w->current_matrix->begv == BUF_BEGV (b))
11966 b->clip_changed = 0;
11967
11968 /* If display wasn't paused, and W is not a tool bar window, see if
11969 point has been moved into or out of a composition. In that case,
11970 we set b->clip_changed to 1 to force updating the screen. If
11971 b->clip_changed has already been set to 1, we can skip this
11972 check. */
11973 if (!b->clip_changed
11974 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11975 {
11976 EMACS_INT pt;
11977
11978 if (w == XWINDOW (selected_window))
11979 pt = PT;
11980 else
11981 pt = marker_position (w->pointm);
11982
11983 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11984 || pt != XINT (w->last_point))
11985 && check_point_in_composition (w->current_matrix->buffer,
11986 XINT (w->last_point),
11987 XBUFFER (w->buffer), pt))
11988 b->clip_changed = 1;
11989 }
11990 }
11991 \f
11992
11993 /* Select FRAME to forward the values of frame-local variables into C
11994 variables so that the redisplay routines can access those values
11995 directly. */
11996
11997 static void
11998 select_frame_for_redisplay (Lisp_Object frame)
11999 {
12000 Lisp_Object tail, tem;
12001 Lisp_Object old = selected_frame;
12002 struct Lisp_Symbol *sym;
12003
12004 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12005
12006 selected_frame = frame;
12007
12008 do {
12009 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12010 if (CONSP (XCAR (tail))
12011 && (tem = XCAR (XCAR (tail)),
12012 SYMBOLP (tem))
12013 && (sym = indirect_variable (XSYMBOL (tem)),
12014 sym->redirect == SYMBOL_LOCALIZED)
12015 && sym->val.blv->frame_local)
12016 /* Use find_symbol_value rather than Fsymbol_value
12017 to avoid an error if it is void. */
12018 find_symbol_value (tem);
12019 } while (!EQ (frame, old) && (frame = old, 1));
12020 }
12021
12022
12023 #define STOP_POLLING \
12024 do { if (! polling_stopped_here) stop_polling (); \
12025 polling_stopped_here = 1; } while (0)
12026
12027 #define RESUME_POLLING \
12028 do { if (polling_stopped_here) start_polling (); \
12029 polling_stopped_here = 0; } while (0)
12030
12031
12032 /* Perhaps in the future avoid recentering windows if it
12033 is not necessary; currently that causes some problems. */
12034
12035 static void
12036 redisplay_internal (void)
12037 {
12038 struct window *w = XWINDOW (selected_window);
12039 struct window *sw;
12040 struct frame *fr;
12041 int pending;
12042 int must_finish = 0;
12043 struct text_pos tlbufpos, tlendpos;
12044 int number_of_visible_frames;
12045 int count, count1;
12046 struct frame *sf;
12047 int polling_stopped_here = 0;
12048 Lisp_Object old_frame = selected_frame;
12049
12050 /* Non-zero means redisplay has to consider all windows on all
12051 frames. Zero means, only selected_window is considered. */
12052 int consider_all_windows_p;
12053
12054 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12055
12056 /* No redisplay if running in batch mode or frame is not yet fully
12057 initialized, or redisplay is explicitly turned off by setting
12058 Vinhibit_redisplay. */
12059 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12060 || !NILP (Vinhibit_redisplay))
12061 return;
12062
12063 /* Don't examine these until after testing Vinhibit_redisplay.
12064 When Emacs is shutting down, perhaps because its connection to
12065 X has dropped, we should not look at them at all. */
12066 fr = XFRAME (w->frame);
12067 sf = SELECTED_FRAME ();
12068
12069 if (!fr->glyphs_initialized_p)
12070 return;
12071
12072 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12073 if (popup_activated ())
12074 return;
12075 #endif
12076
12077 /* I don't think this happens but let's be paranoid. */
12078 if (redisplaying_p)
12079 return;
12080
12081 /* Record a function that resets redisplaying_p to its old value
12082 when we leave this function. */
12083 count = SPECPDL_INDEX ();
12084 record_unwind_protect (unwind_redisplay,
12085 Fcons (make_number (redisplaying_p), selected_frame));
12086 ++redisplaying_p;
12087 specbind (Qinhibit_free_realized_faces, Qnil);
12088
12089 {
12090 Lisp_Object tail, frame;
12091
12092 FOR_EACH_FRAME (tail, frame)
12093 {
12094 struct frame *f = XFRAME (frame);
12095 f->already_hscrolled_p = 0;
12096 }
12097 }
12098
12099 retry:
12100 /* Remember the currently selected window. */
12101 sw = w;
12102
12103 if (!EQ (old_frame, selected_frame)
12104 && FRAME_LIVE_P (XFRAME (old_frame)))
12105 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12106 selected_frame and selected_window to be temporarily out-of-sync so
12107 when we come back here via `goto retry', we need to resync because we
12108 may need to run Elisp code (via prepare_menu_bars). */
12109 select_frame_for_redisplay (old_frame);
12110
12111 pending = 0;
12112 reconsider_clip_changes (w, current_buffer);
12113 last_escape_glyph_frame = NULL;
12114 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12115 last_glyphless_glyph_frame = NULL;
12116 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12117
12118 /* If new fonts have been loaded that make a glyph matrix adjustment
12119 necessary, do it. */
12120 if (fonts_changed_p)
12121 {
12122 adjust_glyphs (NULL);
12123 ++windows_or_buffers_changed;
12124 fonts_changed_p = 0;
12125 }
12126
12127 /* If face_change_count is non-zero, init_iterator will free all
12128 realized faces, which includes the faces referenced from current
12129 matrices. So, we can't reuse current matrices in this case. */
12130 if (face_change_count)
12131 ++windows_or_buffers_changed;
12132
12133 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12134 && FRAME_TTY (sf)->previous_frame != sf)
12135 {
12136 /* Since frames on a single ASCII terminal share the same
12137 display area, displaying a different frame means redisplay
12138 the whole thing. */
12139 windows_or_buffers_changed++;
12140 SET_FRAME_GARBAGED (sf);
12141 #ifndef DOS_NT
12142 set_tty_color_mode (FRAME_TTY (sf), sf);
12143 #endif
12144 FRAME_TTY (sf)->previous_frame = sf;
12145 }
12146
12147 /* Set the visible flags for all frames. Do this before checking
12148 for resized or garbaged frames; they want to know if their frames
12149 are visible. See the comment in frame.h for
12150 FRAME_SAMPLE_VISIBILITY. */
12151 {
12152 Lisp_Object tail, frame;
12153
12154 number_of_visible_frames = 0;
12155
12156 FOR_EACH_FRAME (tail, frame)
12157 {
12158 struct frame *f = XFRAME (frame);
12159
12160 FRAME_SAMPLE_VISIBILITY (f);
12161 if (FRAME_VISIBLE_P (f))
12162 ++number_of_visible_frames;
12163 clear_desired_matrices (f);
12164 }
12165 }
12166
12167 /* Notice any pending interrupt request to change frame size. */
12168 do_pending_window_change (1);
12169
12170 /* do_pending_window_change could change the selected_window due to
12171 frame resizing which makes the selected window too small. */
12172 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12173 {
12174 sw = w;
12175 reconsider_clip_changes (w, current_buffer);
12176 }
12177
12178 /* Clear frames marked as garbaged. */
12179 if (frame_garbaged)
12180 clear_garbaged_frames ();
12181
12182 /* Build menubar and tool-bar items. */
12183 if (NILP (Vmemory_full))
12184 prepare_menu_bars ();
12185
12186 if (windows_or_buffers_changed)
12187 update_mode_lines++;
12188
12189 /* Detect case that we need to write or remove a star in the mode line. */
12190 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12191 {
12192 w->update_mode_line = Qt;
12193 if (buffer_shared > 1)
12194 update_mode_lines++;
12195 }
12196
12197 /* Avoid invocation of point motion hooks by `current_column' below. */
12198 count1 = SPECPDL_INDEX ();
12199 specbind (Qinhibit_point_motion_hooks, Qt);
12200
12201 /* If %c is in the mode line, update it if needed. */
12202 if (!NILP (w->column_number_displayed)
12203 /* This alternative quickly identifies a common case
12204 where no change is needed. */
12205 && !(PT == XFASTINT (w->last_point)
12206 && XFASTINT (w->last_modified) >= MODIFF
12207 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12208 && (XFASTINT (w->column_number_displayed) != current_column ()))
12209 w->update_mode_line = Qt;
12210
12211 unbind_to (count1, Qnil);
12212
12213 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12214
12215 /* The variable buffer_shared is set in redisplay_window and
12216 indicates that we redisplay a buffer in different windows. See
12217 there. */
12218 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12219 || cursor_type_changed);
12220
12221 /* If specs for an arrow have changed, do thorough redisplay
12222 to ensure we remove any arrow that should no longer exist. */
12223 if (overlay_arrows_changed_p ())
12224 consider_all_windows_p = windows_or_buffers_changed = 1;
12225
12226 /* Normally the message* functions will have already displayed and
12227 updated the echo area, but the frame may have been trashed, or
12228 the update may have been preempted, so display the echo area
12229 again here. Checking message_cleared_p captures the case that
12230 the echo area should be cleared. */
12231 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12232 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12233 || (message_cleared_p
12234 && minibuf_level == 0
12235 /* If the mini-window is currently selected, this means the
12236 echo-area doesn't show through. */
12237 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12238 {
12239 int window_height_changed_p = echo_area_display (0);
12240 must_finish = 1;
12241
12242 /* If we don't display the current message, don't clear the
12243 message_cleared_p flag, because, if we did, we wouldn't clear
12244 the echo area in the next redisplay which doesn't preserve
12245 the echo area. */
12246 if (!display_last_displayed_message_p)
12247 message_cleared_p = 0;
12248
12249 if (fonts_changed_p)
12250 goto retry;
12251 else if (window_height_changed_p)
12252 {
12253 consider_all_windows_p = 1;
12254 ++update_mode_lines;
12255 ++windows_or_buffers_changed;
12256
12257 /* If window configuration was changed, frames may have been
12258 marked garbaged. Clear them or we will experience
12259 surprises wrt scrolling. */
12260 if (frame_garbaged)
12261 clear_garbaged_frames ();
12262 }
12263 }
12264 else if (EQ (selected_window, minibuf_window)
12265 && (current_buffer->clip_changed
12266 || XFASTINT (w->last_modified) < MODIFF
12267 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12268 && resize_mini_window (w, 0))
12269 {
12270 /* Resized active mini-window to fit the size of what it is
12271 showing if its contents might have changed. */
12272 must_finish = 1;
12273 /* FIXME: this causes all frames to be updated, which seems unnecessary
12274 since only the current frame needs to be considered. This function needs
12275 to be rewritten with two variables, consider_all_windows and
12276 consider_all_frames. */
12277 consider_all_windows_p = 1;
12278 ++windows_or_buffers_changed;
12279 ++update_mode_lines;
12280
12281 /* If window configuration was changed, frames may have been
12282 marked garbaged. Clear them or we will experience
12283 surprises wrt scrolling. */
12284 if (frame_garbaged)
12285 clear_garbaged_frames ();
12286 }
12287
12288
12289 /* If showing the region, and mark has changed, we must redisplay
12290 the whole window. The assignment to this_line_start_pos prevents
12291 the optimization directly below this if-statement. */
12292 if (((!NILP (Vtransient_mark_mode)
12293 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12294 != !NILP (w->region_showing))
12295 || (!NILP (w->region_showing)
12296 && !EQ (w->region_showing,
12297 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12298 CHARPOS (this_line_start_pos) = 0;
12299
12300 /* Optimize the case that only the line containing the cursor in the
12301 selected window has changed. Variables starting with this_ are
12302 set in display_line and record information about the line
12303 containing the cursor. */
12304 tlbufpos = this_line_start_pos;
12305 tlendpos = this_line_end_pos;
12306 if (!consider_all_windows_p
12307 && CHARPOS (tlbufpos) > 0
12308 && NILP (w->update_mode_line)
12309 && !current_buffer->clip_changed
12310 && !current_buffer->prevent_redisplay_optimizations_p
12311 && FRAME_VISIBLE_P (XFRAME (w->frame))
12312 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12313 /* Make sure recorded data applies to current buffer, etc. */
12314 && this_line_buffer == current_buffer
12315 && current_buffer == XBUFFER (w->buffer)
12316 && NILP (w->force_start)
12317 && NILP (w->optional_new_start)
12318 /* Point must be on the line that we have info recorded about. */
12319 && PT >= CHARPOS (tlbufpos)
12320 && PT <= Z - CHARPOS (tlendpos)
12321 /* All text outside that line, including its final newline,
12322 must be unchanged. */
12323 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12324 CHARPOS (tlendpos)))
12325 {
12326 if (CHARPOS (tlbufpos) > BEGV
12327 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12328 && (CHARPOS (tlbufpos) == ZV
12329 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12330 /* Former continuation line has disappeared by becoming empty. */
12331 goto cancel;
12332 else if (XFASTINT (w->last_modified) < MODIFF
12333 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12334 || MINI_WINDOW_P (w))
12335 {
12336 /* We have to handle the case of continuation around a
12337 wide-column character (see the comment in indent.c around
12338 line 1340).
12339
12340 For instance, in the following case:
12341
12342 -------- Insert --------
12343 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12344 J_I_ ==> J_I_ `^^' are cursors.
12345 ^^ ^^
12346 -------- --------
12347
12348 As we have to redraw the line above, we cannot use this
12349 optimization. */
12350
12351 struct it it;
12352 int line_height_before = this_line_pixel_height;
12353
12354 /* Note that start_display will handle the case that the
12355 line starting at tlbufpos is a continuation line. */
12356 start_display (&it, w, tlbufpos);
12357
12358 /* Implementation note: It this still necessary? */
12359 if (it.current_x != this_line_start_x)
12360 goto cancel;
12361
12362 TRACE ((stderr, "trying display optimization 1\n"));
12363 w->cursor.vpos = -1;
12364 overlay_arrow_seen = 0;
12365 it.vpos = this_line_vpos;
12366 it.current_y = this_line_y;
12367 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12368 display_line (&it);
12369
12370 /* If line contains point, is not continued,
12371 and ends at same distance from eob as before, we win. */
12372 if (w->cursor.vpos >= 0
12373 /* Line is not continued, otherwise this_line_start_pos
12374 would have been set to 0 in display_line. */
12375 && CHARPOS (this_line_start_pos)
12376 /* Line ends as before. */
12377 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12378 /* Line has same height as before. Otherwise other lines
12379 would have to be shifted up or down. */
12380 && this_line_pixel_height == line_height_before)
12381 {
12382 /* If this is not the window's last line, we must adjust
12383 the charstarts of the lines below. */
12384 if (it.current_y < it.last_visible_y)
12385 {
12386 struct glyph_row *row
12387 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12388 EMACS_INT delta, delta_bytes;
12389
12390 /* We used to distinguish between two cases here,
12391 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12392 when the line ends in a newline or the end of the
12393 buffer's accessible portion. But both cases did
12394 the same, so they were collapsed. */
12395 delta = (Z
12396 - CHARPOS (tlendpos)
12397 - MATRIX_ROW_START_CHARPOS (row));
12398 delta_bytes = (Z_BYTE
12399 - BYTEPOS (tlendpos)
12400 - MATRIX_ROW_START_BYTEPOS (row));
12401
12402 increment_matrix_positions (w->current_matrix,
12403 this_line_vpos + 1,
12404 w->current_matrix->nrows,
12405 delta, delta_bytes);
12406 }
12407
12408 /* If this row displays text now but previously didn't,
12409 or vice versa, w->window_end_vpos may have to be
12410 adjusted. */
12411 if ((it.glyph_row - 1)->displays_text_p)
12412 {
12413 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12414 XSETINT (w->window_end_vpos, this_line_vpos);
12415 }
12416 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12417 && this_line_vpos > 0)
12418 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12419 w->window_end_valid = Qnil;
12420
12421 /* Update hint: No need to try to scroll in update_window. */
12422 w->desired_matrix->no_scrolling_p = 1;
12423
12424 #if GLYPH_DEBUG
12425 *w->desired_matrix->method = 0;
12426 debug_method_add (w, "optimization 1");
12427 #endif
12428 #ifdef HAVE_WINDOW_SYSTEM
12429 update_window_fringes (w, 0);
12430 #endif
12431 goto update;
12432 }
12433 else
12434 goto cancel;
12435 }
12436 else if (/* Cursor position hasn't changed. */
12437 PT == XFASTINT (w->last_point)
12438 /* Make sure the cursor was last displayed
12439 in this window. Otherwise we have to reposition it. */
12440 && 0 <= w->cursor.vpos
12441 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12442 {
12443 if (!must_finish)
12444 {
12445 do_pending_window_change (1);
12446 /* If selected_window changed, redisplay again. */
12447 if (WINDOWP (selected_window)
12448 && (w = XWINDOW (selected_window)) != sw)
12449 goto retry;
12450
12451 /* We used to always goto end_of_redisplay here, but this
12452 isn't enough if we have a blinking cursor. */
12453 if (w->cursor_off_p == w->last_cursor_off_p)
12454 goto end_of_redisplay;
12455 }
12456 goto update;
12457 }
12458 /* If highlighting the region, or if the cursor is in the echo area,
12459 then we can't just move the cursor. */
12460 else if (! (!NILP (Vtransient_mark_mode)
12461 && !NILP (BVAR (current_buffer, mark_active)))
12462 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12463 || highlight_nonselected_windows)
12464 && NILP (w->region_showing)
12465 && NILP (Vshow_trailing_whitespace)
12466 && !cursor_in_echo_area)
12467 {
12468 struct it it;
12469 struct glyph_row *row;
12470
12471 /* Skip from tlbufpos to PT and see where it is. Note that
12472 PT may be in invisible text. If so, we will end at the
12473 next visible position. */
12474 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12475 NULL, DEFAULT_FACE_ID);
12476 it.current_x = this_line_start_x;
12477 it.current_y = this_line_y;
12478 it.vpos = this_line_vpos;
12479
12480 /* The call to move_it_to stops in front of PT, but
12481 moves over before-strings. */
12482 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12483
12484 if (it.vpos == this_line_vpos
12485 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12486 row->enabled_p))
12487 {
12488 xassert (this_line_vpos == it.vpos);
12489 xassert (this_line_y == it.current_y);
12490 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12491 #if GLYPH_DEBUG
12492 *w->desired_matrix->method = 0;
12493 debug_method_add (w, "optimization 3");
12494 #endif
12495 goto update;
12496 }
12497 else
12498 goto cancel;
12499 }
12500
12501 cancel:
12502 /* Text changed drastically or point moved off of line. */
12503 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12504 }
12505
12506 CHARPOS (this_line_start_pos) = 0;
12507 consider_all_windows_p |= buffer_shared > 1;
12508 ++clear_face_cache_count;
12509 #ifdef HAVE_WINDOW_SYSTEM
12510 ++clear_image_cache_count;
12511 #endif
12512
12513 /* Build desired matrices, and update the display. If
12514 consider_all_windows_p is non-zero, do it for all windows on all
12515 frames. Otherwise do it for selected_window, only. */
12516
12517 if (consider_all_windows_p)
12518 {
12519 Lisp_Object tail, frame;
12520
12521 FOR_EACH_FRAME (tail, frame)
12522 XFRAME (frame)->updated_p = 0;
12523
12524 /* Recompute # windows showing selected buffer. This will be
12525 incremented each time such a window is displayed. */
12526 buffer_shared = 0;
12527
12528 FOR_EACH_FRAME (tail, frame)
12529 {
12530 struct frame *f = XFRAME (frame);
12531
12532 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12533 {
12534 if (! EQ (frame, selected_frame))
12535 /* Select the frame, for the sake of frame-local
12536 variables. */
12537 select_frame_for_redisplay (frame);
12538
12539 /* Mark all the scroll bars to be removed; we'll redeem
12540 the ones we want when we redisplay their windows. */
12541 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12542 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12543
12544 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12545 redisplay_windows (FRAME_ROOT_WINDOW (f));
12546
12547 /* The X error handler may have deleted that frame. */
12548 if (!FRAME_LIVE_P (f))
12549 continue;
12550
12551 /* Any scroll bars which redisplay_windows should have
12552 nuked should now go away. */
12553 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12554 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12555
12556 /* If fonts changed, display again. */
12557 /* ??? rms: I suspect it is a mistake to jump all the way
12558 back to retry here. It should just retry this frame. */
12559 if (fonts_changed_p)
12560 goto retry;
12561
12562 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12563 {
12564 /* See if we have to hscroll. */
12565 if (!f->already_hscrolled_p)
12566 {
12567 f->already_hscrolled_p = 1;
12568 if (hscroll_windows (f->root_window))
12569 goto retry;
12570 }
12571
12572 /* Prevent various kinds of signals during display
12573 update. stdio is not robust about handling
12574 signals, which can cause an apparent I/O
12575 error. */
12576 if (interrupt_input)
12577 unrequest_sigio ();
12578 STOP_POLLING;
12579
12580 /* Update the display. */
12581 set_window_update_flags (XWINDOW (f->root_window), 1);
12582 pending |= update_frame (f, 0, 0);
12583 f->updated_p = 1;
12584 }
12585 }
12586 }
12587
12588 if (!EQ (old_frame, selected_frame)
12589 && FRAME_LIVE_P (XFRAME (old_frame)))
12590 /* We played a bit fast-and-loose above and allowed selected_frame
12591 and selected_window to be temporarily out-of-sync but let's make
12592 sure this stays contained. */
12593 select_frame_for_redisplay (old_frame);
12594 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12595
12596 if (!pending)
12597 {
12598 /* Do the mark_window_display_accurate after all windows have
12599 been redisplayed because this call resets flags in buffers
12600 which are needed for proper redisplay. */
12601 FOR_EACH_FRAME (tail, frame)
12602 {
12603 struct frame *f = XFRAME (frame);
12604 if (f->updated_p)
12605 {
12606 mark_window_display_accurate (f->root_window, 1);
12607 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12608 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12609 }
12610 }
12611 }
12612 }
12613 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12614 {
12615 Lisp_Object mini_window;
12616 struct frame *mini_frame;
12617
12618 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12619 /* Use list_of_error, not Qerror, so that
12620 we catch only errors and don't run the debugger. */
12621 internal_condition_case_1 (redisplay_window_1, selected_window,
12622 list_of_error,
12623 redisplay_window_error);
12624
12625 /* Compare desired and current matrices, perform output. */
12626
12627 update:
12628 /* If fonts changed, display again. */
12629 if (fonts_changed_p)
12630 goto retry;
12631
12632 /* Prevent various kinds of signals during display update.
12633 stdio is not robust about handling signals,
12634 which can cause an apparent I/O error. */
12635 if (interrupt_input)
12636 unrequest_sigio ();
12637 STOP_POLLING;
12638
12639 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12640 {
12641 if (hscroll_windows (selected_window))
12642 goto retry;
12643
12644 XWINDOW (selected_window)->must_be_updated_p = 1;
12645 pending = update_frame (sf, 0, 0);
12646 }
12647
12648 /* We may have called echo_area_display at the top of this
12649 function. If the echo area is on another frame, that may
12650 have put text on a frame other than the selected one, so the
12651 above call to update_frame would not have caught it. Catch
12652 it here. */
12653 mini_window = FRAME_MINIBUF_WINDOW (sf);
12654 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12655
12656 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12657 {
12658 XWINDOW (mini_window)->must_be_updated_p = 1;
12659 pending |= update_frame (mini_frame, 0, 0);
12660 if (!pending && hscroll_windows (mini_window))
12661 goto retry;
12662 }
12663 }
12664
12665 /* If display was paused because of pending input, make sure we do a
12666 thorough update the next time. */
12667 if (pending)
12668 {
12669 /* Prevent the optimization at the beginning of
12670 redisplay_internal that tries a single-line update of the
12671 line containing the cursor in the selected window. */
12672 CHARPOS (this_line_start_pos) = 0;
12673
12674 /* Let the overlay arrow be updated the next time. */
12675 update_overlay_arrows (0);
12676
12677 /* If we pause after scrolling, some rows in the current
12678 matrices of some windows are not valid. */
12679 if (!WINDOW_FULL_WIDTH_P (w)
12680 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12681 update_mode_lines = 1;
12682 }
12683 else
12684 {
12685 if (!consider_all_windows_p)
12686 {
12687 /* This has already been done above if
12688 consider_all_windows_p is set. */
12689 mark_window_display_accurate_1 (w, 1);
12690
12691 /* Say overlay arrows are up to date. */
12692 update_overlay_arrows (1);
12693
12694 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12695 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12696 }
12697
12698 update_mode_lines = 0;
12699 windows_or_buffers_changed = 0;
12700 cursor_type_changed = 0;
12701 }
12702
12703 /* Start SIGIO interrupts coming again. Having them off during the
12704 code above makes it less likely one will discard output, but not
12705 impossible, since there might be stuff in the system buffer here.
12706 But it is much hairier to try to do anything about that. */
12707 if (interrupt_input)
12708 request_sigio ();
12709 RESUME_POLLING;
12710
12711 /* If a frame has become visible which was not before, redisplay
12712 again, so that we display it. Expose events for such a frame
12713 (which it gets when becoming visible) don't call the parts of
12714 redisplay constructing glyphs, so simply exposing a frame won't
12715 display anything in this case. So, we have to display these
12716 frames here explicitly. */
12717 if (!pending)
12718 {
12719 Lisp_Object tail, frame;
12720 int new_count = 0;
12721
12722 FOR_EACH_FRAME (tail, frame)
12723 {
12724 int this_is_visible = 0;
12725
12726 if (XFRAME (frame)->visible)
12727 this_is_visible = 1;
12728 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12729 if (XFRAME (frame)->visible)
12730 this_is_visible = 1;
12731
12732 if (this_is_visible)
12733 new_count++;
12734 }
12735
12736 if (new_count != number_of_visible_frames)
12737 windows_or_buffers_changed++;
12738 }
12739
12740 /* Change frame size now if a change is pending. */
12741 do_pending_window_change (1);
12742
12743 /* If we just did a pending size change, or have additional
12744 visible frames, or selected_window changed, redisplay again. */
12745 if ((windows_or_buffers_changed && !pending)
12746 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12747 goto retry;
12748
12749 /* Clear the face and image caches.
12750
12751 We used to do this only if consider_all_windows_p. But the cache
12752 needs to be cleared if a timer creates images in the current
12753 buffer (e.g. the test case in Bug#6230). */
12754
12755 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12756 {
12757 clear_face_cache (0);
12758 clear_face_cache_count = 0;
12759 }
12760
12761 #ifdef HAVE_WINDOW_SYSTEM
12762 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12763 {
12764 clear_image_caches (Qnil);
12765 clear_image_cache_count = 0;
12766 }
12767 #endif /* HAVE_WINDOW_SYSTEM */
12768
12769 end_of_redisplay:
12770 unbind_to (count, Qnil);
12771 RESUME_POLLING;
12772 }
12773
12774
12775 /* Redisplay, but leave alone any recent echo area message unless
12776 another message has been requested in its place.
12777
12778 This is useful in situations where you need to redisplay but no
12779 user action has occurred, making it inappropriate for the message
12780 area to be cleared. See tracking_off and
12781 wait_reading_process_output for examples of these situations.
12782
12783 FROM_WHERE is an integer saying from where this function was
12784 called. This is useful for debugging. */
12785
12786 void
12787 redisplay_preserve_echo_area (int from_where)
12788 {
12789 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12790
12791 if (!NILP (echo_area_buffer[1]))
12792 {
12793 /* We have a previously displayed message, but no current
12794 message. Redisplay the previous message. */
12795 display_last_displayed_message_p = 1;
12796 redisplay_internal ();
12797 display_last_displayed_message_p = 0;
12798 }
12799 else
12800 redisplay_internal ();
12801
12802 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12803 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12804 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12805 }
12806
12807
12808 /* Function registered with record_unwind_protect in
12809 redisplay_internal. Reset redisplaying_p to the value it had
12810 before redisplay_internal was called, and clear
12811 prevent_freeing_realized_faces_p. It also selects the previously
12812 selected frame, unless it has been deleted (by an X connection
12813 failure during redisplay, for example). */
12814
12815 static Lisp_Object
12816 unwind_redisplay (Lisp_Object val)
12817 {
12818 Lisp_Object old_redisplaying_p, old_frame;
12819
12820 old_redisplaying_p = XCAR (val);
12821 redisplaying_p = XFASTINT (old_redisplaying_p);
12822 old_frame = XCDR (val);
12823 if (! EQ (old_frame, selected_frame)
12824 && FRAME_LIVE_P (XFRAME (old_frame)))
12825 select_frame_for_redisplay (old_frame);
12826 return Qnil;
12827 }
12828
12829
12830 /* Mark the display of window W as accurate or inaccurate. If
12831 ACCURATE_P is non-zero mark display of W as accurate. If
12832 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12833 redisplay_internal is called. */
12834
12835 static void
12836 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12837 {
12838 if (BUFFERP (w->buffer))
12839 {
12840 struct buffer *b = XBUFFER (w->buffer);
12841
12842 w->last_modified
12843 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12844 w->last_overlay_modified
12845 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12846 w->last_had_star
12847 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12848
12849 if (accurate_p)
12850 {
12851 b->clip_changed = 0;
12852 b->prevent_redisplay_optimizations_p = 0;
12853
12854 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12855 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12856 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12857 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12858
12859 w->current_matrix->buffer = b;
12860 w->current_matrix->begv = BUF_BEGV (b);
12861 w->current_matrix->zv = BUF_ZV (b);
12862
12863 w->last_cursor = w->cursor;
12864 w->last_cursor_off_p = w->cursor_off_p;
12865
12866 if (w == XWINDOW (selected_window))
12867 w->last_point = make_number (BUF_PT (b));
12868 else
12869 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12870 }
12871 }
12872
12873 if (accurate_p)
12874 {
12875 w->window_end_valid = w->buffer;
12876 w->update_mode_line = Qnil;
12877 }
12878 }
12879
12880
12881 /* Mark the display of windows in the window tree rooted at WINDOW as
12882 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12883 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12884 be redisplayed the next time redisplay_internal is called. */
12885
12886 void
12887 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12888 {
12889 struct window *w;
12890
12891 for (; !NILP (window); window = w->next)
12892 {
12893 w = XWINDOW (window);
12894 mark_window_display_accurate_1 (w, accurate_p);
12895
12896 if (!NILP (w->vchild))
12897 mark_window_display_accurate (w->vchild, accurate_p);
12898 if (!NILP (w->hchild))
12899 mark_window_display_accurate (w->hchild, accurate_p);
12900 }
12901
12902 if (accurate_p)
12903 {
12904 update_overlay_arrows (1);
12905 }
12906 else
12907 {
12908 /* Force a thorough redisplay the next time by setting
12909 last_arrow_position and last_arrow_string to t, which is
12910 unequal to any useful value of Voverlay_arrow_... */
12911 update_overlay_arrows (-1);
12912 }
12913 }
12914
12915
12916 /* Return value in display table DP (Lisp_Char_Table *) for character
12917 C. Since a display table doesn't have any parent, we don't have to
12918 follow parent. Do not call this function directly but use the
12919 macro DISP_CHAR_VECTOR. */
12920
12921 Lisp_Object
12922 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12923 {
12924 Lisp_Object val;
12925
12926 if (ASCII_CHAR_P (c))
12927 {
12928 val = dp->ascii;
12929 if (SUB_CHAR_TABLE_P (val))
12930 val = XSUB_CHAR_TABLE (val)->contents[c];
12931 }
12932 else
12933 {
12934 Lisp_Object table;
12935
12936 XSETCHAR_TABLE (table, dp);
12937 val = char_table_ref (table, c);
12938 }
12939 if (NILP (val))
12940 val = dp->defalt;
12941 return val;
12942 }
12943
12944
12945 \f
12946 /***********************************************************************
12947 Window Redisplay
12948 ***********************************************************************/
12949
12950 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12951
12952 static void
12953 redisplay_windows (Lisp_Object window)
12954 {
12955 while (!NILP (window))
12956 {
12957 struct window *w = XWINDOW (window);
12958
12959 if (!NILP (w->hchild))
12960 redisplay_windows (w->hchild);
12961 else if (!NILP (w->vchild))
12962 redisplay_windows (w->vchild);
12963 else if (!NILP (w->buffer))
12964 {
12965 displayed_buffer = XBUFFER (w->buffer);
12966 /* Use list_of_error, not Qerror, so that
12967 we catch only errors and don't run the debugger. */
12968 internal_condition_case_1 (redisplay_window_0, window,
12969 list_of_error,
12970 redisplay_window_error);
12971 }
12972
12973 window = w->next;
12974 }
12975 }
12976
12977 static Lisp_Object
12978 redisplay_window_error (Lisp_Object ignore)
12979 {
12980 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12981 return Qnil;
12982 }
12983
12984 static Lisp_Object
12985 redisplay_window_0 (Lisp_Object window)
12986 {
12987 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12988 redisplay_window (window, 0);
12989 return Qnil;
12990 }
12991
12992 static Lisp_Object
12993 redisplay_window_1 (Lisp_Object window)
12994 {
12995 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12996 redisplay_window (window, 1);
12997 return Qnil;
12998 }
12999 \f
13000
13001 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13002 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13003 which positions recorded in ROW differ from current buffer
13004 positions.
13005
13006 Return 0 if cursor is not on this row, 1 otherwise. */
13007
13008 static int
13009 set_cursor_from_row (struct window *w, struct glyph_row *row,
13010 struct glyph_matrix *matrix,
13011 EMACS_INT delta, EMACS_INT delta_bytes,
13012 int dy, int dvpos)
13013 {
13014 struct glyph *glyph = row->glyphs[TEXT_AREA];
13015 struct glyph *end = glyph + row->used[TEXT_AREA];
13016 struct glyph *cursor = NULL;
13017 /* The last known character position in row. */
13018 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13019 int x = row->x;
13020 EMACS_INT pt_old = PT - delta;
13021 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13022 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13023 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13024 /* A glyph beyond the edge of TEXT_AREA which we should never
13025 touch. */
13026 struct glyph *glyphs_end = end;
13027 /* Non-zero means we've found a match for cursor position, but that
13028 glyph has the avoid_cursor_p flag set. */
13029 int match_with_avoid_cursor = 0;
13030 /* Non-zero means we've seen at least one glyph that came from a
13031 display string. */
13032 int string_seen = 0;
13033 /* Largest and smalles buffer positions seen so far during scan of
13034 glyph row. */
13035 EMACS_INT bpos_max = pos_before;
13036 EMACS_INT bpos_min = pos_after;
13037 /* Last buffer position covered by an overlay string with an integer
13038 `cursor' property. */
13039 EMACS_INT bpos_covered = 0;
13040
13041 /* Skip over glyphs not having an object at the start and the end of
13042 the row. These are special glyphs like truncation marks on
13043 terminal frames. */
13044 if (row->displays_text_p)
13045 {
13046 if (!row->reversed_p)
13047 {
13048 while (glyph < end
13049 && INTEGERP (glyph->object)
13050 && glyph->charpos < 0)
13051 {
13052 x += glyph->pixel_width;
13053 ++glyph;
13054 }
13055 while (end > glyph
13056 && INTEGERP ((end - 1)->object)
13057 /* CHARPOS is zero for blanks and stretch glyphs
13058 inserted by extend_face_to_end_of_line. */
13059 && (end - 1)->charpos <= 0)
13060 --end;
13061 glyph_before = glyph - 1;
13062 glyph_after = end;
13063 }
13064 else
13065 {
13066 struct glyph *g;
13067
13068 /* If the glyph row is reversed, we need to process it from back
13069 to front, so swap the edge pointers. */
13070 glyphs_end = end = glyph - 1;
13071 glyph += row->used[TEXT_AREA] - 1;
13072
13073 while (glyph > end + 1
13074 && INTEGERP (glyph->object)
13075 && glyph->charpos < 0)
13076 {
13077 --glyph;
13078 x -= glyph->pixel_width;
13079 }
13080 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13081 --glyph;
13082 /* By default, in reversed rows we put the cursor on the
13083 rightmost (first in the reading order) glyph. */
13084 for (g = end + 1; g < glyph; g++)
13085 x += g->pixel_width;
13086 while (end < glyph
13087 && INTEGERP ((end + 1)->object)
13088 && (end + 1)->charpos <= 0)
13089 ++end;
13090 glyph_before = glyph + 1;
13091 glyph_after = end;
13092 }
13093 }
13094 else if (row->reversed_p)
13095 {
13096 /* In R2L rows that don't display text, put the cursor on the
13097 rightmost glyph. Case in point: an empty last line that is
13098 part of an R2L paragraph. */
13099 cursor = end - 1;
13100 /* Avoid placing the cursor on the last glyph of the row, where
13101 on terminal frames we hold the vertical border between
13102 adjacent windows. */
13103 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13104 && !WINDOW_RIGHTMOST_P (w)
13105 && cursor == row->glyphs[LAST_AREA] - 1)
13106 cursor--;
13107 x = -1; /* will be computed below, at label compute_x */
13108 }
13109
13110 /* Step 1: Try to find the glyph whose character position
13111 corresponds to point. If that's not possible, find 2 glyphs
13112 whose character positions are the closest to point, one before
13113 point, the other after it. */
13114 if (!row->reversed_p)
13115 while (/* not marched to end of glyph row */
13116 glyph < end
13117 /* glyph was not inserted by redisplay for internal purposes */
13118 && !INTEGERP (glyph->object))
13119 {
13120 if (BUFFERP (glyph->object))
13121 {
13122 EMACS_INT dpos = glyph->charpos - pt_old;
13123
13124 if (glyph->charpos > bpos_max)
13125 bpos_max = glyph->charpos;
13126 if (glyph->charpos < bpos_min)
13127 bpos_min = glyph->charpos;
13128 if (!glyph->avoid_cursor_p)
13129 {
13130 /* If we hit point, we've found the glyph on which to
13131 display the cursor. */
13132 if (dpos == 0)
13133 {
13134 match_with_avoid_cursor = 0;
13135 break;
13136 }
13137 /* See if we've found a better approximation to
13138 POS_BEFORE or to POS_AFTER. Note that we want the
13139 first (leftmost) glyph of all those that are the
13140 closest from below, and the last (rightmost) of all
13141 those from above. */
13142 if (0 > dpos && dpos > pos_before - pt_old)
13143 {
13144 pos_before = glyph->charpos;
13145 glyph_before = glyph;
13146 }
13147 else if (0 < dpos && dpos <= pos_after - pt_old)
13148 {
13149 pos_after = glyph->charpos;
13150 glyph_after = glyph;
13151 }
13152 }
13153 else if (dpos == 0)
13154 match_with_avoid_cursor = 1;
13155 }
13156 else if (STRINGP (glyph->object))
13157 {
13158 Lisp_Object chprop;
13159 EMACS_INT glyph_pos = glyph->charpos;
13160
13161 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13162 glyph->object);
13163 if (INTEGERP (chprop))
13164 {
13165 bpos_covered = bpos_max + XINT (chprop);
13166 /* If the `cursor' property covers buffer positions up
13167 to and including point, we should display cursor on
13168 this glyph. Note that overlays and text properties
13169 with string values stop bidi reordering, so every
13170 buffer position to the left of the string is always
13171 smaller than any position to the right of the
13172 string. Therefore, if a `cursor' property on one
13173 of the string's characters has an integer value, we
13174 will break out of the loop below _before_ we get to
13175 the position match above. IOW, integer values of
13176 the `cursor' property override the "exact match for
13177 point" strategy of positioning the cursor. */
13178 /* Implementation note: bpos_max == pt_old when, e.g.,
13179 we are in an empty line, where bpos_max is set to
13180 MATRIX_ROW_START_CHARPOS, see above. */
13181 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13182 {
13183 cursor = glyph;
13184 break;
13185 }
13186 }
13187
13188 string_seen = 1;
13189 }
13190 x += glyph->pixel_width;
13191 ++glyph;
13192 }
13193 else if (glyph > end) /* row is reversed */
13194 while (!INTEGERP (glyph->object))
13195 {
13196 if (BUFFERP (glyph->object))
13197 {
13198 EMACS_INT dpos = glyph->charpos - pt_old;
13199
13200 if (glyph->charpos > bpos_max)
13201 bpos_max = glyph->charpos;
13202 if (glyph->charpos < bpos_min)
13203 bpos_min = glyph->charpos;
13204 if (!glyph->avoid_cursor_p)
13205 {
13206 if (dpos == 0)
13207 {
13208 match_with_avoid_cursor = 0;
13209 break;
13210 }
13211 if (0 > dpos && dpos > pos_before - pt_old)
13212 {
13213 pos_before = glyph->charpos;
13214 glyph_before = glyph;
13215 }
13216 else if (0 < dpos && dpos <= pos_after - pt_old)
13217 {
13218 pos_after = glyph->charpos;
13219 glyph_after = glyph;
13220 }
13221 }
13222 else if (dpos == 0)
13223 match_with_avoid_cursor = 1;
13224 }
13225 else if (STRINGP (glyph->object))
13226 {
13227 Lisp_Object chprop;
13228 EMACS_INT glyph_pos = glyph->charpos;
13229
13230 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13231 glyph->object);
13232 if (INTEGERP (chprop))
13233 {
13234 bpos_covered = bpos_max + XINT (chprop);
13235 /* If the `cursor' property covers buffer positions up
13236 to and including point, we should display cursor on
13237 this glyph. */
13238 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13239 {
13240 cursor = glyph;
13241 break;
13242 }
13243 }
13244 string_seen = 1;
13245 }
13246 --glyph;
13247 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13248 {
13249 x--; /* can't use any pixel_width */
13250 break;
13251 }
13252 x -= glyph->pixel_width;
13253 }
13254
13255 /* Step 2: If we didn't find an exact match for point, we need to
13256 look for a proper place to put the cursor among glyphs between
13257 GLYPH_BEFORE and GLYPH_AFTER. */
13258 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13259 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13260 && bpos_covered < pt_old)
13261 {
13262 /* An empty line has a single glyph whose OBJECT is zero and
13263 whose CHARPOS is the position of a newline on that line.
13264 Note that on a TTY, there are more glyphs after that, which
13265 were produced by extend_face_to_end_of_line, but their
13266 CHARPOS is zero or negative. */
13267 int empty_line_p =
13268 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13269 && INTEGERP (glyph->object) && glyph->charpos > 0;
13270
13271 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13272 {
13273 EMACS_INT ellipsis_pos;
13274
13275 /* Scan back over the ellipsis glyphs. */
13276 if (!row->reversed_p)
13277 {
13278 ellipsis_pos = (glyph - 1)->charpos;
13279 while (glyph > row->glyphs[TEXT_AREA]
13280 && (glyph - 1)->charpos == ellipsis_pos)
13281 glyph--, x -= glyph->pixel_width;
13282 /* That loop always goes one position too far, including
13283 the glyph before the ellipsis. So scan forward over
13284 that one. */
13285 x += glyph->pixel_width;
13286 glyph++;
13287 }
13288 else /* row is reversed */
13289 {
13290 ellipsis_pos = (glyph + 1)->charpos;
13291 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13292 && (glyph + 1)->charpos == ellipsis_pos)
13293 glyph++, x += glyph->pixel_width;
13294 x -= glyph->pixel_width;
13295 glyph--;
13296 }
13297 }
13298 else if (match_with_avoid_cursor
13299 /* A truncated row may not include PT among its
13300 character positions. Setting the cursor inside the
13301 scroll margin will trigger recalculation of hscroll
13302 in hscroll_window_tree. */
13303 || (row->truncated_on_left_p && pt_old < bpos_min)
13304 || (row->truncated_on_right_p && pt_old > bpos_max)
13305 /* Zero-width characters produce no glyphs. */
13306 || (!string_seen
13307 && !empty_line_p
13308 && (row->reversed_p
13309 ? glyph_after > glyphs_end
13310 : glyph_after < glyphs_end)))
13311 {
13312 cursor = glyph_after;
13313 x = -1;
13314 }
13315 else if (string_seen)
13316 {
13317 int incr = row->reversed_p ? -1 : +1;
13318
13319 /* Need to find the glyph that came out of a string which is
13320 present at point. That glyph is somewhere between
13321 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13322 positioned between POS_BEFORE and POS_AFTER in the
13323 buffer. */
13324 struct glyph *start, *stop;
13325 EMACS_INT pos = pos_before;
13326
13327 x = -1;
13328
13329 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13330 correspond to POS_BEFORE and POS_AFTER, respectively. We
13331 need START and STOP in the order that corresponds to the
13332 row's direction as given by its reversed_p flag. If the
13333 directionality of characters between POS_BEFORE and
13334 POS_AFTER is the opposite of the row's base direction,
13335 these characters will have been reordered for display,
13336 and we need to reverse START and STOP. */
13337 if (!row->reversed_p)
13338 {
13339 start = min (glyph_before, glyph_after);
13340 stop = max (glyph_before, glyph_after);
13341 }
13342 else
13343 {
13344 start = max (glyph_before, glyph_after);
13345 stop = min (glyph_before, glyph_after);
13346 }
13347 for (glyph = start + incr;
13348 row->reversed_p ? glyph > stop : glyph < stop; )
13349 {
13350
13351 /* Any glyphs that come from the buffer are here because
13352 of bidi reordering. Skip them, and only pay
13353 attention to glyphs that came from some string. */
13354 if (STRINGP (glyph->object))
13355 {
13356 Lisp_Object str;
13357 EMACS_INT tem;
13358
13359 str = glyph->object;
13360 tem = string_buffer_position_lim (str, pos, pos_after, 0);
13361 if (tem == 0 /* from overlay */
13362 || pos <= tem)
13363 {
13364 /* If the string from which this glyph came is
13365 found in the buffer at point, then we've
13366 found the glyph we've been looking for. If
13367 it comes from an overlay (tem == 0), and it
13368 has the `cursor' property on one of its
13369 glyphs, record that glyph as a candidate for
13370 displaying the cursor. (As in the
13371 unidirectional version, we will display the
13372 cursor on the last candidate we find.) */
13373 if (tem == 0 || tem == pt_old)
13374 {
13375 /* The glyphs from this string could have
13376 been reordered. Find the one with the
13377 smallest string position. Or there could
13378 be a character in the string with the
13379 `cursor' property, which means display
13380 cursor on that character's glyph. */
13381 EMACS_INT strpos = glyph->charpos;
13382
13383 if (tem)
13384 cursor = glyph;
13385 for ( ;
13386 (row->reversed_p ? glyph > stop : glyph < stop)
13387 && EQ (glyph->object, str);
13388 glyph += incr)
13389 {
13390 Lisp_Object cprop;
13391 EMACS_INT gpos = glyph->charpos;
13392
13393 cprop = Fget_char_property (make_number (gpos),
13394 Qcursor,
13395 glyph->object);
13396 if (!NILP (cprop))
13397 {
13398 cursor = glyph;
13399 break;
13400 }
13401 if (tem && glyph->charpos < strpos)
13402 {
13403 strpos = glyph->charpos;
13404 cursor = glyph;
13405 }
13406 }
13407
13408 if (tem == pt_old)
13409 goto compute_x;
13410 }
13411 if (tem)
13412 pos = tem + 1; /* don't find previous instances */
13413 }
13414 /* This string is not what we want; skip all of the
13415 glyphs that came from it. */
13416 while ((row->reversed_p ? glyph > stop : glyph < stop)
13417 && EQ (glyph->object, str))
13418 glyph += incr;
13419 }
13420 else
13421 glyph += incr;
13422 }
13423
13424 /* If we reached the end of the line, and END was from a string,
13425 the cursor is not on this line. */
13426 if (cursor == NULL
13427 && (row->reversed_p ? glyph <= end : glyph >= end)
13428 && STRINGP (end->object)
13429 && row->continued_p)
13430 return 0;
13431 }
13432 }
13433
13434 compute_x:
13435 if (cursor != NULL)
13436 glyph = cursor;
13437 if (x < 0)
13438 {
13439 struct glyph *g;
13440
13441 /* Need to compute x that corresponds to GLYPH. */
13442 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13443 {
13444 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13445 abort ();
13446 x += g->pixel_width;
13447 }
13448 }
13449
13450 /* ROW could be part of a continued line, which, under bidi
13451 reordering, might have other rows whose start and end charpos
13452 occlude point. Only set w->cursor if we found a better
13453 approximation to the cursor position than we have from previously
13454 examined candidate rows belonging to the same continued line. */
13455 if (/* we already have a candidate row */
13456 w->cursor.vpos >= 0
13457 /* that candidate is not the row we are processing */
13458 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13459 /* the row we are processing is part of a continued line */
13460 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13461 /* Make sure cursor.vpos specifies a row whose start and end
13462 charpos occlude point. This is because some callers of this
13463 function leave cursor.vpos at the row where the cursor was
13464 displayed during the last redisplay cycle. */
13465 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13466 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13467 {
13468 struct glyph *g1 =
13469 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13470
13471 /* Don't consider glyphs that are outside TEXT_AREA. */
13472 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13473 return 0;
13474 /* Keep the candidate whose buffer position is the closest to
13475 point. */
13476 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13477 w->cursor.hpos >= 0
13478 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13479 && BUFFERP (g1->object)
13480 && (g1->charpos == pt_old /* an exact match always wins */
13481 || (BUFFERP (glyph->object)
13482 && eabs (g1->charpos - pt_old)
13483 < eabs (glyph->charpos - pt_old))))
13484 return 0;
13485 /* If this candidate gives an exact match, use that. */
13486 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13487 /* Otherwise, keep the candidate that comes from a row
13488 spanning less buffer positions. This may win when one or
13489 both candidate positions are on glyphs that came from
13490 display strings, for which we cannot compare buffer
13491 positions. */
13492 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13493 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13494 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13495 return 0;
13496 }
13497 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13498 w->cursor.x = x;
13499 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13500 w->cursor.y = row->y + dy;
13501
13502 if (w == XWINDOW (selected_window))
13503 {
13504 if (!row->continued_p
13505 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13506 && row->x == 0)
13507 {
13508 this_line_buffer = XBUFFER (w->buffer);
13509
13510 CHARPOS (this_line_start_pos)
13511 = MATRIX_ROW_START_CHARPOS (row) + delta;
13512 BYTEPOS (this_line_start_pos)
13513 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13514
13515 CHARPOS (this_line_end_pos)
13516 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13517 BYTEPOS (this_line_end_pos)
13518 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13519
13520 this_line_y = w->cursor.y;
13521 this_line_pixel_height = row->height;
13522 this_line_vpos = w->cursor.vpos;
13523 this_line_start_x = row->x;
13524 }
13525 else
13526 CHARPOS (this_line_start_pos) = 0;
13527 }
13528
13529 return 1;
13530 }
13531
13532
13533 /* Run window scroll functions, if any, for WINDOW with new window
13534 start STARTP. Sets the window start of WINDOW to that position.
13535
13536 We assume that the window's buffer is really current. */
13537
13538 static INLINE struct text_pos
13539 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13540 {
13541 struct window *w = XWINDOW (window);
13542 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13543
13544 if (current_buffer != XBUFFER (w->buffer))
13545 abort ();
13546
13547 if (!NILP (Vwindow_scroll_functions))
13548 {
13549 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13550 make_number (CHARPOS (startp)));
13551 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13552 /* In case the hook functions switch buffers. */
13553 if (current_buffer != XBUFFER (w->buffer))
13554 set_buffer_internal_1 (XBUFFER (w->buffer));
13555 }
13556
13557 return startp;
13558 }
13559
13560
13561 /* Make sure the line containing the cursor is fully visible.
13562 A value of 1 means there is nothing to be done.
13563 (Either the line is fully visible, or it cannot be made so,
13564 or we cannot tell.)
13565
13566 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13567 is higher than window.
13568
13569 A value of 0 means the caller should do scrolling
13570 as if point had gone off the screen. */
13571
13572 static int
13573 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13574 {
13575 struct glyph_matrix *matrix;
13576 struct glyph_row *row;
13577 int window_height;
13578
13579 if (!make_cursor_line_fully_visible_p)
13580 return 1;
13581
13582 /* It's not always possible to find the cursor, e.g, when a window
13583 is full of overlay strings. Don't do anything in that case. */
13584 if (w->cursor.vpos < 0)
13585 return 1;
13586
13587 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13588 row = MATRIX_ROW (matrix, w->cursor.vpos);
13589
13590 /* If the cursor row is not partially visible, there's nothing to do. */
13591 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13592 return 1;
13593
13594 /* If the row the cursor is in is taller than the window's height,
13595 it's not clear what to do, so do nothing. */
13596 window_height = window_box_height (w);
13597 if (row->height >= window_height)
13598 {
13599 if (!force_p || MINI_WINDOW_P (w)
13600 || w->vscroll || w->cursor.vpos == 0)
13601 return 1;
13602 }
13603 return 0;
13604 }
13605
13606
13607 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13608 non-zero means only WINDOW is redisplayed in redisplay_internal.
13609 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13610 in redisplay_window to bring a partially visible line into view in
13611 the case that only the cursor has moved.
13612
13613 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13614 last screen line's vertical height extends past the end of the screen.
13615
13616 Value is
13617
13618 1 if scrolling succeeded
13619
13620 0 if scrolling didn't find point.
13621
13622 -1 if new fonts have been loaded so that we must interrupt
13623 redisplay, adjust glyph matrices, and try again. */
13624
13625 enum
13626 {
13627 SCROLLING_SUCCESS,
13628 SCROLLING_FAILED,
13629 SCROLLING_NEED_LARGER_MATRICES
13630 };
13631
13632 /* If scroll-conservatively is more than this, never recenter.
13633
13634 If you change this, don't forget to update the doc string of
13635 `scroll-conservatively' and the Emacs manual. */
13636 #define SCROLL_LIMIT 100
13637
13638 static int
13639 try_scrolling (Lisp_Object window, int just_this_one_p,
13640 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13641 int temp_scroll_step, int last_line_misfit)
13642 {
13643 struct window *w = XWINDOW (window);
13644 struct frame *f = XFRAME (w->frame);
13645 struct text_pos pos, startp;
13646 struct it it;
13647 int this_scroll_margin, scroll_max, rc, height;
13648 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13649 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13650 Lisp_Object aggressive;
13651 /* We will never try scrolling more than this number of lines. */
13652 int scroll_limit = SCROLL_LIMIT;
13653
13654 #if GLYPH_DEBUG
13655 debug_method_add (w, "try_scrolling");
13656 #endif
13657
13658 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13659
13660 /* Compute scroll margin height in pixels. We scroll when point is
13661 within this distance from the top or bottom of the window. */
13662 if (scroll_margin > 0)
13663 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13664 * FRAME_LINE_HEIGHT (f);
13665 else
13666 this_scroll_margin = 0;
13667
13668 /* Force arg_scroll_conservatively to have a reasonable value, to
13669 avoid scrolling too far away with slow move_it_* functions. Note
13670 that the user can supply scroll-conservatively equal to
13671 `most-positive-fixnum', which can be larger than INT_MAX. */
13672 if (arg_scroll_conservatively > scroll_limit)
13673 {
13674 arg_scroll_conservatively = scroll_limit + 1;
13675 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13676 }
13677 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13678 /* Compute how much we should try to scroll maximally to bring
13679 point into view. */
13680 scroll_max = (max (scroll_step,
13681 max (arg_scroll_conservatively, temp_scroll_step))
13682 * FRAME_LINE_HEIGHT (f));
13683 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13684 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13685 /* We're trying to scroll because of aggressive scrolling but no
13686 scroll_step is set. Choose an arbitrary one. */
13687 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13688 else
13689 scroll_max = 0;
13690
13691 too_near_end:
13692
13693 /* Decide whether to scroll down. */
13694 if (PT > CHARPOS (startp))
13695 {
13696 int scroll_margin_y;
13697
13698 /* Compute the pixel ypos of the scroll margin, then move it to
13699 either that ypos or PT, whichever comes first. */
13700 start_display (&it, w, startp);
13701 scroll_margin_y = it.last_visible_y - this_scroll_margin
13702 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13703 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13704 (MOVE_TO_POS | MOVE_TO_Y));
13705
13706 if (PT > CHARPOS (it.current.pos))
13707 {
13708 int y0 = line_bottom_y (&it);
13709 /* Compute how many pixels below window bottom to stop searching
13710 for PT. This avoids costly search for PT that is far away if
13711 the user limited scrolling by a small number of lines, but
13712 always finds PT if scroll_conservatively is set to a large
13713 number, such as most-positive-fixnum. */
13714 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13715 int y_to_move = it.last_visible_y + slack;
13716
13717 /* Compute the distance from the scroll margin to PT or to
13718 the scroll limit, whichever comes first. This should
13719 include the height of the cursor line, to make that line
13720 fully visible. */
13721 move_it_to (&it, PT, -1, y_to_move,
13722 -1, MOVE_TO_POS | MOVE_TO_Y);
13723 dy = line_bottom_y (&it) - y0;
13724
13725 if (dy > scroll_max)
13726 return SCROLLING_FAILED;
13727
13728 scroll_down_p = 1;
13729 }
13730 }
13731
13732 if (scroll_down_p)
13733 {
13734 /* Point is in or below the bottom scroll margin, so move the
13735 window start down. If scrolling conservatively, move it just
13736 enough down to make point visible. If scroll_step is set,
13737 move it down by scroll_step. */
13738 if (arg_scroll_conservatively)
13739 amount_to_scroll
13740 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13741 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13742 else if (scroll_step || temp_scroll_step)
13743 amount_to_scroll = scroll_max;
13744 else
13745 {
13746 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13747 height = WINDOW_BOX_TEXT_HEIGHT (w);
13748 if (NUMBERP (aggressive))
13749 {
13750 double float_amount = XFLOATINT (aggressive) * height;
13751 amount_to_scroll = float_amount;
13752 if (amount_to_scroll == 0 && float_amount > 0)
13753 amount_to_scroll = 1;
13754 /* Don't let point enter the scroll margin near top of
13755 the window. */
13756 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13757 amount_to_scroll = height - 2*this_scroll_margin + dy;
13758 }
13759 }
13760
13761 if (amount_to_scroll <= 0)
13762 return SCROLLING_FAILED;
13763
13764 start_display (&it, w, startp);
13765 if (arg_scroll_conservatively <= scroll_limit)
13766 move_it_vertically (&it, amount_to_scroll);
13767 else
13768 {
13769 /* Extra precision for users who set scroll-conservatively
13770 to a large number: make sure the amount we scroll
13771 the window start is never less than amount_to_scroll,
13772 which was computed as distance from window bottom to
13773 point. This matters when lines at window top and lines
13774 below window bottom have different height. */
13775 struct it it1 = it;
13776 /* We use a temporary it1 because line_bottom_y can modify
13777 its argument, if it moves one line down; see there. */
13778 int start_y = line_bottom_y (&it1);
13779
13780 do {
13781 move_it_by_lines (&it, 1);
13782 it1 = it;
13783 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13784 }
13785
13786 /* If STARTP is unchanged, move it down another screen line. */
13787 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13788 move_it_by_lines (&it, 1);
13789 startp = it.current.pos;
13790 }
13791 else
13792 {
13793 struct text_pos scroll_margin_pos = startp;
13794
13795 /* See if point is inside the scroll margin at the top of the
13796 window. */
13797 if (this_scroll_margin)
13798 {
13799 start_display (&it, w, startp);
13800 move_it_vertically (&it, this_scroll_margin);
13801 scroll_margin_pos = it.current.pos;
13802 }
13803
13804 if (PT < CHARPOS (scroll_margin_pos))
13805 {
13806 /* Point is in the scroll margin at the top of the window or
13807 above what is displayed in the window. */
13808 int y0, y_to_move;
13809
13810 /* Compute the vertical distance from PT to the scroll
13811 margin position. Move as far as scroll_max allows, or
13812 one screenful, or 10 screen lines, whichever is largest.
13813 Give up if distance is greater than scroll_max. */
13814 SET_TEXT_POS (pos, PT, PT_BYTE);
13815 start_display (&it, w, pos);
13816 y0 = it.current_y;
13817 y_to_move = max (it.last_visible_y,
13818 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13819 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13820 y_to_move, -1,
13821 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13822 dy = it.current_y - y0;
13823 if (dy > scroll_max)
13824 return SCROLLING_FAILED;
13825
13826 /* Compute new window start. */
13827 start_display (&it, w, startp);
13828
13829 if (arg_scroll_conservatively)
13830 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13831 max (scroll_step, temp_scroll_step));
13832 else if (scroll_step || temp_scroll_step)
13833 amount_to_scroll = scroll_max;
13834 else
13835 {
13836 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13837 height = WINDOW_BOX_TEXT_HEIGHT (w);
13838 if (NUMBERP (aggressive))
13839 {
13840 double float_amount = XFLOATINT (aggressive) * height;
13841 amount_to_scroll = float_amount;
13842 if (amount_to_scroll == 0 && float_amount > 0)
13843 amount_to_scroll = 1;
13844 amount_to_scroll -=
13845 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13846 /* Don't let point enter the scroll margin near
13847 bottom of the window. */
13848 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13849 amount_to_scroll = height - 2*this_scroll_margin + dy;
13850 }
13851 }
13852
13853 if (amount_to_scroll <= 0)
13854 return SCROLLING_FAILED;
13855
13856 move_it_vertically_backward (&it, amount_to_scroll);
13857 startp = it.current.pos;
13858 }
13859 }
13860
13861 /* Run window scroll functions. */
13862 startp = run_window_scroll_functions (window, startp);
13863
13864 /* Display the window. Give up if new fonts are loaded, or if point
13865 doesn't appear. */
13866 if (!try_window (window, startp, 0))
13867 rc = SCROLLING_NEED_LARGER_MATRICES;
13868 else if (w->cursor.vpos < 0)
13869 {
13870 clear_glyph_matrix (w->desired_matrix);
13871 rc = SCROLLING_FAILED;
13872 }
13873 else
13874 {
13875 /* Maybe forget recorded base line for line number display. */
13876 if (!just_this_one_p
13877 || current_buffer->clip_changed
13878 || BEG_UNCHANGED < CHARPOS (startp))
13879 w->base_line_number = Qnil;
13880
13881 /* If cursor ends up on a partially visible line,
13882 treat that as being off the bottom of the screen. */
13883 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13884 /* It's possible that the cursor is on the first line of the
13885 buffer, which is partially obscured due to a vscroll
13886 (Bug#7537). In that case, avoid looping forever . */
13887 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13888 {
13889 clear_glyph_matrix (w->desired_matrix);
13890 ++extra_scroll_margin_lines;
13891 goto too_near_end;
13892 }
13893 rc = SCROLLING_SUCCESS;
13894 }
13895
13896 return rc;
13897 }
13898
13899
13900 /* Compute a suitable window start for window W if display of W starts
13901 on a continuation line. Value is non-zero if a new window start
13902 was computed.
13903
13904 The new window start will be computed, based on W's width, starting
13905 from the start of the continued line. It is the start of the
13906 screen line with the minimum distance from the old start W->start. */
13907
13908 static int
13909 compute_window_start_on_continuation_line (struct window *w)
13910 {
13911 struct text_pos pos, start_pos;
13912 int window_start_changed_p = 0;
13913
13914 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13915
13916 /* If window start is on a continuation line... Window start may be
13917 < BEGV in case there's invisible text at the start of the
13918 buffer (M-x rmail, for example). */
13919 if (CHARPOS (start_pos) > BEGV
13920 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13921 {
13922 struct it it;
13923 struct glyph_row *row;
13924
13925 /* Handle the case that the window start is out of range. */
13926 if (CHARPOS (start_pos) < BEGV)
13927 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13928 else if (CHARPOS (start_pos) > ZV)
13929 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13930
13931 /* Find the start of the continued line. This should be fast
13932 because scan_buffer is fast (newline cache). */
13933 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13934 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13935 row, DEFAULT_FACE_ID);
13936 reseat_at_previous_visible_line_start (&it);
13937
13938 /* If the line start is "too far" away from the window start,
13939 say it takes too much time to compute a new window start. */
13940 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13941 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13942 {
13943 int min_distance, distance;
13944
13945 /* Move forward by display lines to find the new window
13946 start. If window width was enlarged, the new start can
13947 be expected to be > the old start. If window width was
13948 decreased, the new window start will be < the old start.
13949 So, we're looking for the display line start with the
13950 minimum distance from the old window start. */
13951 pos = it.current.pos;
13952 min_distance = INFINITY;
13953 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13954 distance < min_distance)
13955 {
13956 min_distance = distance;
13957 pos = it.current.pos;
13958 move_it_by_lines (&it, 1);
13959 }
13960
13961 /* Set the window start there. */
13962 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13963 window_start_changed_p = 1;
13964 }
13965 }
13966
13967 return window_start_changed_p;
13968 }
13969
13970
13971 /* Try cursor movement in case text has not changed in window WINDOW,
13972 with window start STARTP. Value is
13973
13974 CURSOR_MOVEMENT_SUCCESS if successful
13975
13976 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13977
13978 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13979 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13980 we want to scroll as if scroll-step were set to 1. See the code.
13981
13982 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13983 which case we have to abort this redisplay, and adjust matrices
13984 first. */
13985
13986 enum
13987 {
13988 CURSOR_MOVEMENT_SUCCESS,
13989 CURSOR_MOVEMENT_CANNOT_BE_USED,
13990 CURSOR_MOVEMENT_MUST_SCROLL,
13991 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13992 };
13993
13994 static int
13995 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13996 {
13997 struct window *w = XWINDOW (window);
13998 struct frame *f = XFRAME (w->frame);
13999 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14000
14001 #if GLYPH_DEBUG
14002 if (inhibit_try_cursor_movement)
14003 return rc;
14004 #endif
14005
14006 /* Handle case where text has not changed, only point, and it has
14007 not moved off the frame. */
14008 if (/* Point may be in this window. */
14009 PT >= CHARPOS (startp)
14010 /* Selective display hasn't changed. */
14011 && !current_buffer->clip_changed
14012 /* Function force-mode-line-update is used to force a thorough
14013 redisplay. It sets either windows_or_buffers_changed or
14014 update_mode_lines. So don't take a shortcut here for these
14015 cases. */
14016 && !update_mode_lines
14017 && !windows_or_buffers_changed
14018 && !cursor_type_changed
14019 /* Can't use this case if highlighting a region. When a
14020 region exists, cursor movement has to do more than just
14021 set the cursor. */
14022 && !(!NILP (Vtransient_mark_mode)
14023 && !NILP (BVAR (current_buffer, mark_active)))
14024 && NILP (w->region_showing)
14025 && NILP (Vshow_trailing_whitespace)
14026 /* Right after splitting windows, last_point may be nil. */
14027 && INTEGERP (w->last_point)
14028 /* This code is not used for mini-buffer for the sake of the case
14029 of redisplaying to replace an echo area message; since in
14030 that case the mini-buffer contents per se are usually
14031 unchanged. This code is of no real use in the mini-buffer
14032 since the handling of this_line_start_pos, etc., in redisplay
14033 handles the same cases. */
14034 && !EQ (window, minibuf_window)
14035 /* When splitting windows or for new windows, it happens that
14036 redisplay is called with a nil window_end_vpos or one being
14037 larger than the window. This should really be fixed in
14038 window.c. I don't have this on my list, now, so we do
14039 approximately the same as the old redisplay code. --gerd. */
14040 && INTEGERP (w->window_end_vpos)
14041 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14042 && (FRAME_WINDOW_P (f)
14043 || !overlay_arrow_in_current_buffer_p ()))
14044 {
14045 int this_scroll_margin, top_scroll_margin;
14046 struct glyph_row *row = NULL;
14047
14048 #if GLYPH_DEBUG
14049 debug_method_add (w, "cursor movement");
14050 #endif
14051
14052 /* Scroll if point within this distance from the top or bottom
14053 of the window. This is a pixel value. */
14054 if (scroll_margin > 0)
14055 {
14056 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14057 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14058 }
14059 else
14060 this_scroll_margin = 0;
14061
14062 top_scroll_margin = this_scroll_margin;
14063 if (WINDOW_WANTS_HEADER_LINE_P (w))
14064 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14065
14066 /* Start with the row the cursor was displayed during the last
14067 not paused redisplay. Give up if that row is not valid. */
14068 if (w->last_cursor.vpos < 0
14069 || w->last_cursor.vpos >= w->current_matrix->nrows)
14070 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14071 else
14072 {
14073 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14074 if (row->mode_line_p)
14075 ++row;
14076 if (!row->enabled_p)
14077 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14078 }
14079
14080 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14081 {
14082 int scroll_p = 0, must_scroll = 0;
14083 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14084
14085 if (PT > XFASTINT (w->last_point))
14086 {
14087 /* Point has moved forward. */
14088 while (MATRIX_ROW_END_CHARPOS (row) < PT
14089 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14090 {
14091 xassert (row->enabled_p);
14092 ++row;
14093 }
14094
14095 /* If the end position of a row equals the start
14096 position of the next row, and PT is at that position,
14097 we would rather display cursor in the next line. */
14098 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14099 && MATRIX_ROW_END_CHARPOS (row) == PT
14100 && row < w->current_matrix->rows
14101 + w->current_matrix->nrows - 1
14102 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14103 && !cursor_row_p (row))
14104 ++row;
14105
14106 /* If within the scroll margin, scroll. Note that
14107 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14108 the next line would be drawn, and that
14109 this_scroll_margin can be zero. */
14110 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14111 || PT > MATRIX_ROW_END_CHARPOS (row)
14112 /* Line is completely visible last line in window
14113 and PT is to be set in the next line. */
14114 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14115 && PT == MATRIX_ROW_END_CHARPOS (row)
14116 && !row->ends_at_zv_p
14117 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14118 scroll_p = 1;
14119 }
14120 else if (PT < XFASTINT (w->last_point))
14121 {
14122 /* Cursor has to be moved backward. Note that PT >=
14123 CHARPOS (startp) because of the outer if-statement. */
14124 while (!row->mode_line_p
14125 && (MATRIX_ROW_START_CHARPOS (row) > PT
14126 || (MATRIX_ROW_START_CHARPOS (row) == PT
14127 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14128 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14129 row > w->current_matrix->rows
14130 && (row-1)->ends_in_newline_from_string_p))))
14131 && (row->y > top_scroll_margin
14132 || CHARPOS (startp) == BEGV))
14133 {
14134 xassert (row->enabled_p);
14135 --row;
14136 }
14137
14138 /* Consider the following case: Window starts at BEGV,
14139 there is invisible, intangible text at BEGV, so that
14140 display starts at some point START > BEGV. It can
14141 happen that we are called with PT somewhere between
14142 BEGV and START. Try to handle that case. */
14143 if (row < w->current_matrix->rows
14144 || row->mode_line_p)
14145 {
14146 row = w->current_matrix->rows;
14147 if (row->mode_line_p)
14148 ++row;
14149 }
14150
14151 /* Due to newlines in overlay strings, we may have to
14152 skip forward over overlay strings. */
14153 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14154 && MATRIX_ROW_END_CHARPOS (row) == PT
14155 && !cursor_row_p (row))
14156 ++row;
14157
14158 /* If within the scroll margin, scroll. */
14159 if (row->y < top_scroll_margin
14160 && CHARPOS (startp) != BEGV)
14161 scroll_p = 1;
14162 }
14163 else
14164 {
14165 /* Cursor did not move. So don't scroll even if cursor line
14166 is partially visible, as it was so before. */
14167 rc = CURSOR_MOVEMENT_SUCCESS;
14168 }
14169
14170 if (PT < MATRIX_ROW_START_CHARPOS (row)
14171 || PT > MATRIX_ROW_END_CHARPOS (row))
14172 {
14173 /* if PT is not in the glyph row, give up. */
14174 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14175 must_scroll = 1;
14176 }
14177 else if (rc != CURSOR_MOVEMENT_SUCCESS
14178 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14179 {
14180 /* If rows are bidi-reordered and point moved, back up
14181 until we find a row that does not belong to a
14182 continuation line. This is because we must consider
14183 all rows of a continued line as candidates for the
14184 new cursor positioning, since row start and end
14185 positions change non-linearly with vertical position
14186 in such rows. */
14187 /* FIXME: Revisit this when glyph ``spilling'' in
14188 continuation lines' rows is implemented for
14189 bidi-reordered rows. */
14190 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14191 {
14192 xassert (row->enabled_p);
14193 --row;
14194 /* If we hit the beginning of the displayed portion
14195 without finding the first row of a continued
14196 line, give up. */
14197 if (row <= w->current_matrix->rows)
14198 {
14199 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14200 break;
14201 }
14202
14203 }
14204 }
14205 if (must_scroll)
14206 ;
14207 else if (rc != CURSOR_MOVEMENT_SUCCESS
14208 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14209 && make_cursor_line_fully_visible_p)
14210 {
14211 if (PT == MATRIX_ROW_END_CHARPOS (row)
14212 && !row->ends_at_zv_p
14213 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14214 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14215 else if (row->height > window_box_height (w))
14216 {
14217 /* If we end up in a partially visible line, let's
14218 make it fully visible, except when it's taller
14219 than the window, in which case we can't do much
14220 about it. */
14221 *scroll_step = 1;
14222 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14223 }
14224 else
14225 {
14226 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14227 if (!cursor_row_fully_visible_p (w, 0, 1))
14228 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14229 else
14230 rc = CURSOR_MOVEMENT_SUCCESS;
14231 }
14232 }
14233 else if (scroll_p)
14234 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14235 else if (rc != CURSOR_MOVEMENT_SUCCESS
14236 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14237 {
14238 /* With bidi-reordered rows, there could be more than
14239 one candidate row whose start and end positions
14240 occlude point. We need to let set_cursor_from_row
14241 find the best candidate. */
14242 /* FIXME: Revisit this when glyph ``spilling'' in
14243 continuation lines' rows is implemented for
14244 bidi-reordered rows. */
14245 int rv = 0;
14246
14247 do
14248 {
14249 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14250 && PT <= MATRIX_ROW_END_CHARPOS (row)
14251 && cursor_row_p (row))
14252 rv |= set_cursor_from_row (w, row, w->current_matrix,
14253 0, 0, 0, 0);
14254 /* As soon as we've found the first suitable row
14255 whose ends_at_zv_p flag is set, we are done. */
14256 if (rv
14257 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14258 {
14259 rc = CURSOR_MOVEMENT_SUCCESS;
14260 break;
14261 }
14262 ++row;
14263 }
14264 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14265 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14266 || (MATRIX_ROW_START_CHARPOS (row) == PT
14267 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14268 /* If we didn't find any candidate rows, or exited the
14269 loop before all the candidates were examined, signal
14270 to the caller that this method failed. */
14271 if (rc != CURSOR_MOVEMENT_SUCCESS
14272 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14273 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14274 else if (rv)
14275 rc = CURSOR_MOVEMENT_SUCCESS;
14276 }
14277 else
14278 {
14279 do
14280 {
14281 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14282 {
14283 rc = CURSOR_MOVEMENT_SUCCESS;
14284 break;
14285 }
14286 ++row;
14287 }
14288 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14289 && MATRIX_ROW_START_CHARPOS (row) == PT
14290 && cursor_row_p (row));
14291 }
14292 }
14293 }
14294
14295 return rc;
14296 }
14297
14298 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14299 static
14300 #endif
14301 void
14302 set_vertical_scroll_bar (struct window *w)
14303 {
14304 EMACS_INT start, end, whole;
14305
14306 /* Calculate the start and end positions for the current window.
14307 At some point, it would be nice to choose between scrollbars
14308 which reflect the whole buffer size, with special markers
14309 indicating narrowing, and scrollbars which reflect only the
14310 visible region.
14311
14312 Note that mini-buffers sometimes aren't displaying any text. */
14313 if (!MINI_WINDOW_P (w)
14314 || (w == XWINDOW (minibuf_window)
14315 && NILP (echo_area_buffer[0])))
14316 {
14317 struct buffer *buf = XBUFFER (w->buffer);
14318 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14319 start = marker_position (w->start) - BUF_BEGV (buf);
14320 /* I don't think this is guaranteed to be right. For the
14321 moment, we'll pretend it is. */
14322 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14323
14324 if (end < start)
14325 end = start;
14326 if (whole < (end - start))
14327 whole = end - start;
14328 }
14329 else
14330 start = end = whole = 0;
14331
14332 /* Indicate what this scroll bar ought to be displaying now. */
14333 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14334 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14335 (w, end - start, whole, start);
14336 }
14337
14338
14339 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14340 selected_window is redisplayed.
14341
14342 We can return without actually redisplaying the window if
14343 fonts_changed_p is nonzero. In that case, redisplay_internal will
14344 retry. */
14345
14346 static void
14347 redisplay_window (Lisp_Object window, int just_this_one_p)
14348 {
14349 struct window *w = XWINDOW (window);
14350 struct frame *f = XFRAME (w->frame);
14351 struct buffer *buffer = XBUFFER (w->buffer);
14352 struct buffer *old = current_buffer;
14353 struct text_pos lpoint, opoint, startp;
14354 int update_mode_line;
14355 int tem;
14356 struct it it;
14357 /* Record it now because it's overwritten. */
14358 int current_matrix_up_to_date_p = 0;
14359 int used_current_matrix_p = 0;
14360 /* This is less strict than current_matrix_up_to_date_p.
14361 It indictes that the buffer contents and narrowing are unchanged. */
14362 int buffer_unchanged_p = 0;
14363 int temp_scroll_step = 0;
14364 int count = SPECPDL_INDEX ();
14365 int rc;
14366 int centering_position = -1;
14367 int last_line_misfit = 0;
14368 EMACS_INT beg_unchanged, end_unchanged;
14369
14370 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14371 opoint = lpoint;
14372
14373 /* W must be a leaf window here. */
14374 xassert (!NILP (w->buffer));
14375 #if GLYPH_DEBUG
14376 *w->desired_matrix->method = 0;
14377 #endif
14378
14379 restart:
14380 reconsider_clip_changes (w, buffer);
14381
14382 /* Has the mode line to be updated? */
14383 update_mode_line = (!NILP (w->update_mode_line)
14384 || update_mode_lines
14385 || buffer->clip_changed
14386 || buffer->prevent_redisplay_optimizations_p);
14387
14388 if (MINI_WINDOW_P (w))
14389 {
14390 if (w == XWINDOW (echo_area_window)
14391 && !NILP (echo_area_buffer[0]))
14392 {
14393 if (update_mode_line)
14394 /* We may have to update a tty frame's menu bar or a
14395 tool-bar. Example `M-x C-h C-h C-g'. */
14396 goto finish_menu_bars;
14397 else
14398 /* We've already displayed the echo area glyphs in this window. */
14399 goto finish_scroll_bars;
14400 }
14401 else if ((w != XWINDOW (minibuf_window)
14402 || minibuf_level == 0)
14403 /* When buffer is nonempty, redisplay window normally. */
14404 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14405 /* Quail displays non-mini buffers in minibuffer window.
14406 In that case, redisplay the window normally. */
14407 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14408 {
14409 /* W is a mini-buffer window, but it's not active, so clear
14410 it. */
14411 int yb = window_text_bottom_y (w);
14412 struct glyph_row *row;
14413 int y;
14414
14415 for (y = 0, row = w->desired_matrix->rows;
14416 y < yb;
14417 y += row->height, ++row)
14418 blank_row (w, row, y);
14419 goto finish_scroll_bars;
14420 }
14421
14422 clear_glyph_matrix (w->desired_matrix);
14423 }
14424
14425 /* Otherwise set up data on this window; select its buffer and point
14426 value. */
14427 /* Really select the buffer, for the sake of buffer-local
14428 variables. */
14429 set_buffer_internal_1 (XBUFFER (w->buffer));
14430
14431 current_matrix_up_to_date_p
14432 = (!NILP (w->window_end_valid)
14433 && !current_buffer->clip_changed
14434 && !current_buffer->prevent_redisplay_optimizations_p
14435 && XFASTINT (w->last_modified) >= MODIFF
14436 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14437
14438 /* Run the window-bottom-change-functions
14439 if it is possible that the text on the screen has changed
14440 (either due to modification of the text, or any other reason). */
14441 if (!current_matrix_up_to_date_p
14442 && !NILP (Vwindow_text_change_functions))
14443 {
14444 safe_run_hooks (Qwindow_text_change_functions);
14445 goto restart;
14446 }
14447
14448 beg_unchanged = BEG_UNCHANGED;
14449 end_unchanged = END_UNCHANGED;
14450
14451 SET_TEXT_POS (opoint, PT, PT_BYTE);
14452
14453 specbind (Qinhibit_point_motion_hooks, Qt);
14454
14455 buffer_unchanged_p
14456 = (!NILP (w->window_end_valid)
14457 && !current_buffer->clip_changed
14458 && XFASTINT (w->last_modified) >= MODIFF
14459 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14460
14461 /* When windows_or_buffers_changed is non-zero, we can't rely on
14462 the window end being valid, so set it to nil there. */
14463 if (windows_or_buffers_changed)
14464 {
14465 /* If window starts on a continuation line, maybe adjust the
14466 window start in case the window's width changed. */
14467 if (XMARKER (w->start)->buffer == current_buffer)
14468 compute_window_start_on_continuation_line (w);
14469
14470 w->window_end_valid = Qnil;
14471 }
14472
14473 /* Some sanity checks. */
14474 CHECK_WINDOW_END (w);
14475 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14476 abort ();
14477 if (BYTEPOS (opoint) < CHARPOS (opoint))
14478 abort ();
14479
14480 /* If %c is in mode line, update it if needed. */
14481 if (!NILP (w->column_number_displayed)
14482 /* This alternative quickly identifies a common case
14483 where no change is needed. */
14484 && !(PT == XFASTINT (w->last_point)
14485 && XFASTINT (w->last_modified) >= MODIFF
14486 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14487 && (XFASTINT (w->column_number_displayed) != current_column ()))
14488 update_mode_line = 1;
14489
14490 /* Count number of windows showing the selected buffer. An indirect
14491 buffer counts as its base buffer. */
14492 if (!just_this_one_p)
14493 {
14494 struct buffer *current_base, *window_base;
14495 current_base = current_buffer;
14496 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14497 if (current_base->base_buffer)
14498 current_base = current_base->base_buffer;
14499 if (window_base->base_buffer)
14500 window_base = window_base->base_buffer;
14501 if (current_base == window_base)
14502 buffer_shared++;
14503 }
14504
14505 /* Point refers normally to the selected window. For any other
14506 window, set up appropriate value. */
14507 if (!EQ (window, selected_window))
14508 {
14509 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14510 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14511 if (new_pt < BEGV)
14512 {
14513 new_pt = BEGV;
14514 new_pt_byte = BEGV_BYTE;
14515 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14516 }
14517 else if (new_pt > (ZV - 1))
14518 {
14519 new_pt = ZV;
14520 new_pt_byte = ZV_BYTE;
14521 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14522 }
14523
14524 /* We don't use SET_PT so that the point-motion hooks don't run. */
14525 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14526 }
14527
14528 /* If any of the character widths specified in the display table
14529 have changed, invalidate the width run cache. It's true that
14530 this may be a bit late to catch such changes, but the rest of
14531 redisplay goes (non-fatally) haywire when the display table is
14532 changed, so why should we worry about doing any better? */
14533 if (current_buffer->width_run_cache)
14534 {
14535 struct Lisp_Char_Table *disptab = buffer_display_table ();
14536
14537 if (! disptab_matches_widthtab (disptab,
14538 XVECTOR (BVAR (current_buffer, width_table))))
14539 {
14540 invalidate_region_cache (current_buffer,
14541 current_buffer->width_run_cache,
14542 BEG, Z);
14543 recompute_width_table (current_buffer, disptab);
14544 }
14545 }
14546
14547 /* If window-start is screwed up, choose a new one. */
14548 if (XMARKER (w->start)->buffer != current_buffer)
14549 goto recenter;
14550
14551 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14552
14553 /* If someone specified a new starting point but did not insist,
14554 check whether it can be used. */
14555 if (!NILP (w->optional_new_start)
14556 && CHARPOS (startp) >= BEGV
14557 && CHARPOS (startp) <= ZV)
14558 {
14559 w->optional_new_start = Qnil;
14560 start_display (&it, w, startp);
14561 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14562 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14563 if (IT_CHARPOS (it) == PT)
14564 w->force_start = Qt;
14565 /* IT may overshoot PT if text at PT is invisible. */
14566 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14567 w->force_start = Qt;
14568 }
14569
14570 force_start:
14571
14572 /* Handle case where place to start displaying has been specified,
14573 unless the specified location is outside the accessible range. */
14574 if (!NILP (w->force_start)
14575 || w->frozen_window_start_p)
14576 {
14577 /* We set this later on if we have to adjust point. */
14578 int new_vpos = -1;
14579
14580 w->force_start = Qnil;
14581 w->vscroll = 0;
14582 w->window_end_valid = Qnil;
14583
14584 /* Forget any recorded base line for line number display. */
14585 if (!buffer_unchanged_p)
14586 w->base_line_number = Qnil;
14587
14588 /* Redisplay the mode line. Select the buffer properly for that.
14589 Also, run the hook window-scroll-functions
14590 because we have scrolled. */
14591 /* Note, we do this after clearing force_start because
14592 if there's an error, it is better to forget about force_start
14593 than to get into an infinite loop calling the hook functions
14594 and having them get more errors. */
14595 if (!update_mode_line
14596 || ! NILP (Vwindow_scroll_functions))
14597 {
14598 update_mode_line = 1;
14599 w->update_mode_line = Qt;
14600 startp = run_window_scroll_functions (window, startp);
14601 }
14602
14603 w->last_modified = make_number (0);
14604 w->last_overlay_modified = make_number (0);
14605 if (CHARPOS (startp) < BEGV)
14606 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14607 else if (CHARPOS (startp) > ZV)
14608 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14609
14610 /* Redisplay, then check if cursor has been set during the
14611 redisplay. Give up if new fonts were loaded. */
14612 /* We used to issue a CHECK_MARGINS argument to try_window here,
14613 but this causes scrolling to fail when point begins inside
14614 the scroll margin (bug#148) -- cyd */
14615 if (!try_window (window, startp, 0))
14616 {
14617 w->force_start = Qt;
14618 clear_glyph_matrix (w->desired_matrix);
14619 goto need_larger_matrices;
14620 }
14621
14622 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14623 {
14624 /* If point does not appear, try to move point so it does
14625 appear. The desired matrix has been built above, so we
14626 can use it here. */
14627 new_vpos = window_box_height (w) / 2;
14628 }
14629
14630 if (!cursor_row_fully_visible_p (w, 0, 0))
14631 {
14632 /* Point does appear, but on a line partly visible at end of window.
14633 Move it back to a fully-visible line. */
14634 new_vpos = window_box_height (w);
14635 }
14636
14637 /* If we need to move point for either of the above reasons,
14638 now actually do it. */
14639 if (new_vpos >= 0)
14640 {
14641 struct glyph_row *row;
14642
14643 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14644 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14645 ++row;
14646
14647 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14648 MATRIX_ROW_START_BYTEPOS (row));
14649
14650 if (w != XWINDOW (selected_window))
14651 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14652 else if (current_buffer == old)
14653 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14654
14655 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14656
14657 /* If we are highlighting the region, then we just changed
14658 the region, so redisplay to show it. */
14659 if (!NILP (Vtransient_mark_mode)
14660 && !NILP (BVAR (current_buffer, mark_active)))
14661 {
14662 clear_glyph_matrix (w->desired_matrix);
14663 if (!try_window (window, startp, 0))
14664 goto need_larger_matrices;
14665 }
14666 }
14667
14668 #if GLYPH_DEBUG
14669 debug_method_add (w, "forced window start");
14670 #endif
14671 goto done;
14672 }
14673
14674 /* Handle case where text has not changed, only point, and it has
14675 not moved off the frame, and we are not retrying after hscroll.
14676 (current_matrix_up_to_date_p is nonzero when retrying.) */
14677 if (current_matrix_up_to_date_p
14678 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14679 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14680 {
14681 switch (rc)
14682 {
14683 case CURSOR_MOVEMENT_SUCCESS:
14684 used_current_matrix_p = 1;
14685 goto done;
14686
14687 case CURSOR_MOVEMENT_MUST_SCROLL:
14688 goto try_to_scroll;
14689
14690 default:
14691 abort ();
14692 }
14693 }
14694 /* If current starting point was originally the beginning of a line
14695 but no longer is, find a new starting point. */
14696 else if (!NILP (w->start_at_line_beg)
14697 && !(CHARPOS (startp) <= BEGV
14698 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14699 {
14700 #if GLYPH_DEBUG
14701 debug_method_add (w, "recenter 1");
14702 #endif
14703 goto recenter;
14704 }
14705
14706 /* Try scrolling with try_window_id. Value is > 0 if update has
14707 been done, it is -1 if we know that the same window start will
14708 not work. It is 0 if unsuccessful for some other reason. */
14709 else if ((tem = try_window_id (w)) != 0)
14710 {
14711 #if GLYPH_DEBUG
14712 debug_method_add (w, "try_window_id %d", tem);
14713 #endif
14714
14715 if (fonts_changed_p)
14716 goto need_larger_matrices;
14717 if (tem > 0)
14718 goto done;
14719
14720 /* Otherwise try_window_id has returned -1 which means that we
14721 don't want the alternative below this comment to execute. */
14722 }
14723 else if (CHARPOS (startp) >= BEGV
14724 && CHARPOS (startp) <= ZV
14725 && PT >= CHARPOS (startp)
14726 && (CHARPOS (startp) < ZV
14727 /* Avoid starting at end of buffer. */
14728 || CHARPOS (startp) == BEGV
14729 || (XFASTINT (w->last_modified) >= MODIFF
14730 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14731 {
14732
14733 /* If first window line is a continuation line, and window start
14734 is inside the modified region, but the first change is before
14735 current window start, we must select a new window start.
14736
14737 However, if this is the result of a down-mouse event (e.g. by
14738 extending the mouse-drag-overlay), we don't want to select a
14739 new window start, since that would change the position under
14740 the mouse, resulting in an unwanted mouse-movement rather
14741 than a simple mouse-click. */
14742 if (NILP (w->start_at_line_beg)
14743 && NILP (do_mouse_tracking)
14744 && CHARPOS (startp) > BEGV
14745 && CHARPOS (startp) > BEG + beg_unchanged
14746 && CHARPOS (startp) <= Z - end_unchanged
14747 /* Even if w->start_at_line_beg is nil, a new window may
14748 start at a line_beg, since that's how set_buffer_window
14749 sets it. So, we need to check the return value of
14750 compute_window_start_on_continuation_line. (See also
14751 bug#197). */
14752 && XMARKER (w->start)->buffer == current_buffer
14753 && compute_window_start_on_continuation_line (w))
14754 {
14755 w->force_start = Qt;
14756 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14757 goto force_start;
14758 }
14759
14760 #if GLYPH_DEBUG
14761 debug_method_add (w, "same window start");
14762 #endif
14763
14764 /* Try to redisplay starting at same place as before.
14765 If point has not moved off frame, accept the results. */
14766 if (!current_matrix_up_to_date_p
14767 /* Don't use try_window_reusing_current_matrix in this case
14768 because a window scroll function can have changed the
14769 buffer. */
14770 || !NILP (Vwindow_scroll_functions)
14771 || MINI_WINDOW_P (w)
14772 || !(used_current_matrix_p
14773 = try_window_reusing_current_matrix (w)))
14774 {
14775 IF_DEBUG (debug_method_add (w, "1"));
14776 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14777 /* -1 means we need to scroll.
14778 0 means we need new matrices, but fonts_changed_p
14779 is set in that case, so we will detect it below. */
14780 goto try_to_scroll;
14781 }
14782
14783 if (fonts_changed_p)
14784 goto need_larger_matrices;
14785
14786 if (w->cursor.vpos >= 0)
14787 {
14788 if (!just_this_one_p
14789 || current_buffer->clip_changed
14790 || BEG_UNCHANGED < CHARPOS (startp))
14791 /* Forget any recorded base line for line number display. */
14792 w->base_line_number = Qnil;
14793
14794 if (!cursor_row_fully_visible_p (w, 1, 0))
14795 {
14796 clear_glyph_matrix (w->desired_matrix);
14797 last_line_misfit = 1;
14798 }
14799 /* Drop through and scroll. */
14800 else
14801 goto done;
14802 }
14803 else
14804 clear_glyph_matrix (w->desired_matrix);
14805 }
14806
14807 try_to_scroll:
14808
14809 w->last_modified = make_number (0);
14810 w->last_overlay_modified = make_number (0);
14811
14812 /* Redisplay the mode line. Select the buffer properly for that. */
14813 if (!update_mode_line)
14814 {
14815 update_mode_line = 1;
14816 w->update_mode_line = Qt;
14817 }
14818
14819 /* Try to scroll by specified few lines. */
14820 if ((scroll_conservatively
14821 || emacs_scroll_step
14822 || temp_scroll_step
14823 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14824 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14825 && CHARPOS (startp) >= BEGV
14826 && CHARPOS (startp) <= ZV)
14827 {
14828 /* The function returns -1 if new fonts were loaded, 1 if
14829 successful, 0 if not successful. */
14830 int ss = try_scrolling (window, just_this_one_p,
14831 scroll_conservatively,
14832 emacs_scroll_step,
14833 temp_scroll_step, last_line_misfit);
14834 switch (ss)
14835 {
14836 case SCROLLING_SUCCESS:
14837 goto done;
14838
14839 case SCROLLING_NEED_LARGER_MATRICES:
14840 goto need_larger_matrices;
14841
14842 case SCROLLING_FAILED:
14843 break;
14844
14845 default:
14846 abort ();
14847 }
14848 }
14849
14850 /* Finally, just choose a place to start which positions point
14851 according to user preferences. */
14852
14853 recenter:
14854
14855 #if GLYPH_DEBUG
14856 debug_method_add (w, "recenter");
14857 #endif
14858
14859 /* w->vscroll = 0; */
14860
14861 /* Forget any previously recorded base line for line number display. */
14862 if (!buffer_unchanged_p)
14863 w->base_line_number = Qnil;
14864
14865 /* Determine the window start relative to point. */
14866 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14867 it.current_y = it.last_visible_y;
14868 if (centering_position < 0)
14869 {
14870 int margin =
14871 scroll_margin > 0
14872 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14873 : 0;
14874 EMACS_INT margin_pos = CHARPOS (startp);
14875 int scrolling_up;
14876 Lisp_Object aggressive;
14877
14878 /* If there is a scroll margin at the top of the window, find
14879 its character position. */
14880 if (margin
14881 /* Cannot call start_display if startp is not in the
14882 accessible region of the buffer. This can happen when we
14883 have just switched to a different buffer and/or changed
14884 its restriction. In that case, startp is initialized to
14885 the character position 1 (BEG) because we did not yet
14886 have chance to display the buffer even once. */
14887 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14888 {
14889 struct it it1;
14890
14891 start_display (&it1, w, startp);
14892 move_it_vertically (&it1, margin);
14893 margin_pos = IT_CHARPOS (it1);
14894 }
14895 scrolling_up = PT > margin_pos;
14896 aggressive =
14897 scrolling_up
14898 ? BVAR (current_buffer, scroll_up_aggressively)
14899 : BVAR (current_buffer, scroll_down_aggressively);
14900
14901 if (!MINI_WINDOW_P (w)
14902 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
14903 {
14904 int pt_offset = 0;
14905
14906 /* Setting scroll-conservatively overrides
14907 scroll-*-aggressively. */
14908 if (!scroll_conservatively && NUMBERP (aggressive))
14909 {
14910 double float_amount = XFLOATINT (aggressive);
14911
14912 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14913 if (pt_offset == 0 && float_amount > 0)
14914 pt_offset = 1;
14915 if (pt_offset)
14916 margin -= 1;
14917 }
14918 /* Compute how much to move the window start backward from
14919 point so that point will be displayed where the user
14920 wants it. */
14921 if (scrolling_up)
14922 {
14923 centering_position = it.last_visible_y;
14924 if (pt_offset)
14925 centering_position -= pt_offset;
14926 centering_position -=
14927 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
14928 /* Don't let point enter the scroll margin near top of
14929 the window. */
14930 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
14931 centering_position = margin * FRAME_LINE_HEIGHT (f);
14932 }
14933 else
14934 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
14935 }
14936 else
14937 /* Set the window start half the height of the window backward
14938 from point. */
14939 centering_position = window_box_height (w) / 2;
14940 }
14941 move_it_vertically_backward (&it, centering_position);
14942
14943 xassert (IT_CHARPOS (it) >= BEGV);
14944
14945 /* The function move_it_vertically_backward may move over more
14946 than the specified y-distance. If it->w is small, e.g. a
14947 mini-buffer window, we may end up in front of the window's
14948 display area. Start displaying at the start of the line
14949 containing PT in this case. */
14950 if (it.current_y <= 0)
14951 {
14952 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14953 move_it_vertically_backward (&it, 0);
14954 it.current_y = 0;
14955 }
14956
14957 it.current_x = it.hpos = 0;
14958
14959 /* Set the window start position here explicitly, to avoid an
14960 infinite loop in case the functions in window-scroll-functions
14961 get errors. */
14962 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14963
14964 /* Run scroll hooks. */
14965 startp = run_window_scroll_functions (window, it.current.pos);
14966
14967 /* Redisplay the window. */
14968 if (!current_matrix_up_to_date_p
14969 || windows_or_buffers_changed
14970 || cursor_type_changed
14971 /* Don't use try_window_reusing_current_matrix in this case
14972 because it can have changed the buffer. */
14973 || !NILP (Vwindow_scroll_functions)
14974 || !just_this_one_p
14975 || MINI_WINDOW_P (w)
14976 || !(used_current_matrix_p
14977 = try_window_reusing_current_matrix (w)))
14978 try_window (window, startp, 0);
14979
14980 /* If new fonts have been loaded (due to fontsets), give up. We
14981 have to start a new redisplay since we need to re-adjust glyph
14982 matrices. */
14983 if (fonts_changed_p)
14984 goto need_larger_matrices;
14985
14986 /* If cursor did not appear assume that the middle of the window is
14987 in the first line of the window. Do it again with the next line.
14988 (Imagine a window of height 100, displaying two lines of height
14989 60. Moving back 50 from it->last_visible_y will end in the first
14990 line.) */
14991 if (w->cursor.vpos < 0)
14992 {
14993 if (!NILP (w->window_end_valid)
14994 && PT >= Z - XFASTINT (w->window_end_pos))
14995 {
14996 clear_glyph_matrix (w->desired_matrix);
14997 move_it_by_lines (&it, 1);
14998 try_window (window, it.current.pos, 0);
14999 }
15000 else if (PT < IT_CHARPOS (it))
15001 {
15002 clear_glyph_matrix (w->desired_matrix);
15003 move_it_by_lines (&it, -1);
15004 try_window (window, it.current.pos, 0);
15005 }
15006 else
15007 {
15008 /* Not much we can do about it. */
15009 }
15010 }
15011
15012 /* Consider the following case: Window starts at BEGV, there is
15013 invisible, intangible text at BEGV, so that display starts at
15014 some point START > BEGV. It can happen that we are called with
15015 PT somewhere between BEGV and START. Try to handle that case. */
15016 if (w->cursor.vpos < 0)
15017 {
15018 struct glyph_row *row = w->current_matrix->rows;
15019 if (row->mode_line_p)
15020 ++row;
15021 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15022 }
15023
15024 if (!cursor_row_fully_visible_p (w, 0, 0))
15025 {
15026 /* If vscroll is enabled, disable it and try again. */
15027 if (w->vscroll)
15028 {
15029 w->vscroll = 0;
15030 clear_glyph_matrix (w->desired_matrix);
15031 goto recenter;
15032 }
15033
15034 /* If centering point failed to make the whole line visible,
15035 put point at the top instead. That has to make the whole line
15036 visible, if it can be done. */
15037 if (centering_position == 0)
15038 goto done;
15039
15040 clear_glyph_matrix (w->desired_matrix);
15041 centering_position = 0;
15042 goto recenter;
15043 }
15044
15045 done:
15046
15047 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15048 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15049 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15050 ? Qt : Qnil);
15051
15052 /* Display the mode line, if we must. */
15053 if ((update_mode_line
15054 /* If window not full width, must redo its mode line
15055 if (a) the window to its side is being redone and
15056 (b) we do a frame-based redisplay. This is a consequence
15057 of how inverted lines are drawn in frame-based redisplay. */
15058 || (!just_this_one_p
15059 && !FRAME_WINDOW_P (f)
15060 && !WINDOW_FULL_WIDTH_P (w))
15061 /* Line number to display. */
15062 || INTEGERP (w->base_line_pos)
15063 /* Column number is displayed and different from the one displayed. */
15064 || (!NILP (w->column_number_displayed)
15065 && (XFASTINT (w->column_number_displayed) != current_column ())))
15066 /* This means that the window has a mode line. */
15067 && (WINDOW_WANTS_MODELINE_P (w)
15068 || WINDOW_WANTS_HEADER_LINE_P (w)))
15069 {
15070 display_mode_lines (w);
15071
15072 /* If mode line height has changed, arrange for a thorough
15073 immediate redisplay using the correct mode line height. */
15074 if (WINDOW_WANTS_MODELINE_P (w)
15075 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15076 {
15077 fonts_changed_p = 1;
15078 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15079 = DESIRED_MODE_LINE_HEIGHT (w);
15080 }
15081
15082 /* If header line height has changed, arrange for a thorough
15083 immediate redisplay using the correct header line height. */
15084 if (WINDOW_WANTS_HEADER_LINE_P (w)
15085 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15086 {
15087 fonts_changed_p = 1;
15088 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15089 = DESIRED_HEADER_LINE_HEIGHT (w);
15090 }
15091
15092 if (fonts_changed_p)
15093 goto need_larger_matrices;
15094 }
15095
15096 if (!line_number_displayed
15097 && !BUFFERP (w->base_line_pos))
15098 {
15099 w->base_line_pos = Qnil;
15100 w->base_line_number = Qnil;
15101 }
15102
15103 finish_menu_bars:
15104
15105 /* When we reach a frame's selected window, redo the frame's menu bar. */
15106 if (update_mode_line
15107 && EQ (FRAME_SELECTED_WINDOW (f), window))
15108 {
15109 int redisplay_menu_p = 0;
15110
15111 if (FRAME_WINDOW_P (f))
15112 {
15113 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15114 || defined (HAVE_NS) || defined (USE_GTK)
15115 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15116 #else
15117 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15118 #endif
15119 }
15120 else
15121 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15122
15123 if (redisplay_menu_p)
15124 display_menu_bar (w);
15125
15126 #ifdef HAVE_WINDOW_SYSTEM
15127 if (FRAME_WINDOW_P (f))
15128 {
15129 #if defined (USE_GTK) || defined (HAVE_NS)
15130 if (FRAME_EXTERNAL_TOOL_BAR (f))
15131 redisplay_tool_bar (f);
15132 #else
15133 if (WINDOWP (f->tool_bar_window)
15134 && (FRAME_TOOL_BAR_LINES (f) > 0
15135 || !NILP (Vauto_resize_tool_bars))
15136 && redisplay_tool_bar (f))
15137 ignore_mouse_drag_p = 1;
15138 #endif
15139 }
15140 #endif
15141 }
15142
15143 #ifdef HAVE_WINDOW_SYSTEM
15144 if (FRAME_WINDOW_P (f)
15145 && update_window_fringes (w, (just_this_one_p
15146 || (!used_current_matrix_p && !overlay_arrow_seen)
15147 || w->pseudo_window_p)))
15148 {
15149 update_begin (f);
15150 BLOCK_INPUT;
15151 if (draw_window_fringes (w, 1))
15152 x_draw_vertical_border (w);
15153 UNBLOCK_INPUT;
15154 update_end (f);
15155 }
15156 #endif /* HAVE_WINDOW_SYSTEM */
15157
15158 /* We go to this label, with fonts_changed_p nonzero,
15159 if it is necessary to try again using larger glyph matrices.
15160 We have to redeem the scroll bar even in this case,
15161 because the loop in redisplay_internal expects that. */
15162 need_larger_matrices:
15163 ;
15164 finish_scroll_bars:
15165
15166 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15167 {
15168 /* Set the thumb's position and size. */
15169 set_vertical_scroll_bar (w);
15170
15171 /* Note that we actually used the scroll bar attached to this
15172 window, so it shouldn't be deleted at the end of redisplay. */
15173 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15174 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15175 }
15176
15177 /* Restore current_buffer and value of point in it. The window
15178 update may have changed the buffer, so first make sure `opoint'
15179 is still valid (Bug#6177). */
15180 if (CHARPOS (opoint) < BEGV)
15181 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15182 else if (CHARPOS (opoint) > ZV)
15183 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15184 else
15185 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15186
15187 set_buffer_internal_1 (old);
15188 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15189 shorter. This can be caused by log truncation in *Messages*. */
15190 if (CHARPOS (lpoint) <= ZV)
15191 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15192
15193 unbind_to (count, Qnil);
15194 }
15195
15196
15197 /* Build the complete desired matrix of WINDOW with a window start
15198 buffer position POS.
15199
15200 Value is 1 if successful. It is zero if fonts were loaded during
15201 redisplay which makes re-adjusting glyph matrices necessary, and -1
15202 if point would appear in the scroll margins.
15203 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15204 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15205 set in FLAGS.) */
15206
15207 int
15208 try_window (Lisp_Object window, struct text_pos pos, int flags)
15209 {
15210 struct window *w = XWINDOW (window);
15211 struct it it;
15212 struct glyph_row *last_text_row = NULL;
15213 struct frame *f = XFRAME (w->frame);
15214
15215 /* Make POS the new window start. */
15216 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15217
15218 /* Mark cursor position as unknown. No overlay arrow seen. */
15219 w->cursor.vpos = -1;
15220 overlay_arrow_seen = 0;
15221
15222 /* Initialize iterator and info to start at POS. */
15223 start_display (&it, w, pos);
15224
15225 /* Display all lines of W. */
15226 while (it.current_y < it.last_visible_y)
15227 {
15228 if (display_line (&it))
15229 last_text_row = it.glyph_row - 1;
15230 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15231 return 0;
15232 }
15233
15234 /* Don't let the cursor end in the scroll margins. */
15235 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15236 && !MINI_WINDOW_P (w))
15237 {
15238 int this_scroll_margin;
15239
15240 if (scroll_margin > 0)
15241 {
15242 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15243 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15244 }
15245 else
15246 this_scroll_margin = 0;
15247
15248 if ((w->cursor.y >= 0 /* not vscrolled */
15249 && w->cursor.y < this_scroll_margin
15250 && CHARPOS (pos) > BEGV
15251 && IT_CHARPOS (it) < ZV)
15252 /* rms: considering make_cursor_line_fully_visible_p here
15253 seems to give wrong results. We don't want to recenter
15254 when the last line is partly visible, we want to allow
15255 that case to be handled in the usual way. */
15256 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15257 {
15258 w->cursor.vpos = -1;
15259 clear_glyph_matrix (w->desired_matrix);
15260 return -1;
15261 }
15262 }
15263
15264 /* If bottom moved off end of frame, change mode line percentage. */
15265 if (XFASTINT (w->window_end_pos) <= 0
15266 && Z != IT_CHARPOS (it))
15267 w->update_mode_line = Qt;
15268
15269 /* Set window_end_pos to the offset of the last character displayed
15270 on the window from the end of current_buffer. Set
15271 window_end_vpos to its row number. */
15272 if (last_text_row)
15273 {
15274 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15275 w->window_end_bytepos
15276 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15277 w->window_end_pos
15278 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15279 w->window_end_vpos
15280 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15281 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15282 ->displays_text_p);
15283 }
15284 else
15285 {
15286 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15287 w->window_end_pos = make_number (Z - ZV);
15288 w->window_end_vpos = make_number (0);
15289 }
15290
15291 /* But that is not valid info until redisplay finishes. */
15292 w->window_end_valid = Qnil;
15293 return 1;
15294 }
15295
15296
15297 \f
15298 /************************************************************************
15299 Window redisplay reusing current matrix when buffer has not changed
15300 ************************************************************************/
15301
15302 /* Try redisplay of window W showing an unchanged buffer with a
15303 different window start than the last time it was displayed by
15304 reusing its current matrix. Value is non-zero if successful.
15305 W->start is the new window start. */
15306
15307 static int
15308 try_window_reusing_current_matrix (struct window *w)
15309 {
15310 struct frame *f = XFRAME (w->frame);
15311 struct glyph_row *bottom_row;
15312 struct it it;
15313 struct run run;
15314 struct text_pos start, new_start;
15315 int nrows_scrolled, i;
15316 struct glyph_row *last_text_row;
15317 struct glyph_row *last_reused_text_row;
15318 struct glyph_row *start_row;
15319 int start_vpos, min_y, max_y;
15320
15321 #if GLYPH_DEBUG
15322 if (inhibit_try_window_reusing)
15323 return 0;
15324 #endif
15325
15326 if (/* This function doesn't handle terminal frames. */
15327 !FRAME_WINDOW_P (f)
15328 /* Don't try to reuse the display if windows have been split
15329 or such. */
15330 || windows_or_buffers_changed
15331 || cursor_type_changed)
15332 return 0;
15333
15334 /* Can't do this if region may have changed. */
15335 if ((!NILP (Vtransient_mark_mode)
15336 && !NILP (BVAR (current_buffer, mark_active)))
15337 || !NILP (w->region_showing)
15338 || !NILP (Vshow_trailing_whitespace))
15339 return 0;
15340
15341 /* If top-line visibility has changed, give up. */
15342 if (WINDOW_WANTS_HEADER_LINE_P (w)
15343 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15344 return 0;
15345
15346 /* Give up if old or new display is scrolled vertically. We could
15347 make this function handle this, but right now it doesn't. */
15348 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15349 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15350 return 0;
15351
15352 /* The variable new_start now holds the new window start. The old
15353 start `start' can be determined from the current matrix. */
15354 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15355 start = start_row->minpos;
15356 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15357
15358 /* Clear the desired matrix for the display below. */
15359 clear_glyph_matrix (w->desired_matrix);
15360
15361 if (CHARPOS (new_start) <= CHARPOS (start))
15362 {
15363 /* Don't use this method if the display starts with an ellipsis
15364 displayed for invisible text. It's not easy to handle that case
15365 below, and it's certainly not worth the effort since this is
15366 not a frequent case. */
15367 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15368 return 0;
15369
15370 IF_DEBUG (debug_method_add (w, "twu1"));
15371
15372 /* Display up to a row that can be reused. The variable
15373 last_text_row is set to the last row displayed that displays
15374 text. Note that it.vpos == 0 if or if not there is a
15375 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15376 start_display (&it, w, new_start);
15377 w->cursor.vpos = -1;
15378 last_text_row = last_reused_text_row = NULL;
15379
15380 while (it.current_y < it.last_visible_y
15381 && !fonts_changed_p)
15382 {
15383 /* If we have reached into the characters in the START row,
15384 that means the line boundaries have changed. So we
15385 can't start copying with the row START. Maybe it will
15386 work to start copying with the following row. */
15387 while (IT_CHARPOS (it) > CHARPOS (start))
15388 {
15389 /* Advance to the next row as the "start". */
15390 start_row++;
15391 start = start_row->minpos;
15392 /* If there are no more rows to try, or just one, give up. */
15393 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15394 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15395 || CHARPOS (start) == ZV)
15396 {
15397 clear_glyph_matrix (w->desired_matrix);
15398 return 0;
15399 }
15400
15401 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15402 }
15403 /* If we have reached alignment,
15404 we can copy the rest of the rows. */
15405 if (IT_CHARPOS (it) == CHARPOS (start))
15406 break;
15407
15408 if (display_line (&it))
15409 last_text_row = it.glyph_row - 1;
15410 }
15411
15412 /* A value of current_y < last_visible_y means that we stopped
15413 at the previous window start, which in turn means that we
15414 have at least one reusable row. */
15415 if (it.current_y < it.last_visible_y)
15416 {
15417 struct glyph_row *row;
15418
15419 /* IT.vpos always starts from 0; it counts text lines. */
15420 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15421
15422 /* Find PT if not already found in the lines displayed. */
15423 if (w->cursor.vpos < 0)
15424 {
15425 int dy = it.current_y - start_row->y;
15426
15427 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15428 row = row_containing_pos (w, PT, row, NULL, dy);
15429 if (row)
15430 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15431 dy, nrows_scrolled);
15432 else
15433 {
15434 clear_glyph_matrix (w->desired_matrix);
15435 return 0;
15436 }
15437 }
15438
15439 /* Scroll the display. Do it before the current matrix is
15440 changed. The problem here is that update has not yet
15441 run, i.e. part of the current matrix is not up to date.
15442 scroll_run_hook will clear the cursor, and use the
15443 current matrix to get the height of the row the cursor is
15444 in. */
15445 run.current_y = start_row->y;
15446 run.desired_y = it.current_y;
15447 run.height = it.last_visible_y - it.current_y;
15448
15449 if (run.height > 0 && run.current_y != run.desired_y)
15450 {
15451 update_begin (f);
15452 FRAME_RIF (f)->update_window_begin_hook (w);
15453 FRAME_RIF (f)->clear_window_mouse_face (w);
15454 FRAME_RIF (f)->scroll_run_hook (w, &run);
15455 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15456 update_end (f);
15457 }
15458
15459 /* Shift current matrix down by nrows_scrolled lines. */
15460 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15461 rotate_matrix (w->current_matrix,
15462 start_vpos,
15463 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15464 nrows_scrolled);
15465
15466 /* Disable lines that must be updated. */
15467 for (i = 0; i < nrows_scrolled; ++i)
15468 (start_row + i)->enabled_p = 0;
15469
15470 /* Re-compute Y positions. */
15471 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15472 max_y = it.last_visible_y;
15473 for (row = start_row + nrows_scrolled;
15474 row < bottom_row;
15475 ++row)
15476 {
15477 row->y = it.current_y;
15478 row->visible_height = row->height;
15479
15480 if (row->y < min_y)
15481 row->visible_height -= min_y - row->y;
15482 if (row->y + row->height > max_y)
15483 row->visible_height -= row->y + row->height - max_y;
15484 row->redraw_fringe_bitmaps_p = 1;
15485
15486 it.current_y += row->height;
15487
15488 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15489 last_reused_text_row = row;
15490 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15491 break;
15492 }
15493
15494 /* Disable lines in the current matrix which are now
15495 below the window. */
15496 for (++row; row < bottom_row; ++row)
15497 row->enabled_p = row->mode_line_p = 0;
15498 }
15499
15500 /* Update window_end_pos etc.; last_reused_text_row is the last
15501 reused row from the current matrix containing text, if any.
15502 The value of last_text_row is the last displayed line
15503 containing text. */
15504 if (last_reused_text_row)
15505 {
15506 w->window_end_bytepos
15507 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15508 w->window_end_pos
15509 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15510 w->window_end_vpos
15511 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15512 w->current_matrix));
15513 }
15514 else if (last_text_row)
15515 {
15516 w->window_end_bytepos
15517 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15518 w->window_end_pos
15519 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15520 w->window_end_vpos
15521 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15522 }
15523 else
15524 {
15525 /* This window must be completely empty. */
15526 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15527 w->window_end_pos = make_number (Z - ZV);
15528 w->window_end_vpos = make_number (0);
15529 }
15530 w->window_end_valid = Qnil;
15531
15532 /* Update hint: don't try scrolling again in update_window. */
15533 w->desired_matrix->no_scrolling_p = 1;
15534
15535 #if GLYPH_DEBUG
15536 debug_method_add (w, "try_window_reusing_current_matrix 1");
15537 #endif
15538 return 1;
15539 }
15540 else if (CHARPOS (new_start) > CHARPOS (start))
15541 {
15542 struct glyph_row *pt_row, *row;
15543 struct glyph_row *first_reusable_row;
15544 struct glyph_row *first_row_to_display;
15545 int dy;
15546 int yb = window_text_bottom_y (w);
15547
15548 /* Find the row starting at new_start, if there is one. Don't
15549 reuse a partially visible line at the end. */
15550 first_reusable_row = start_row;
15551 while (first_reusable_row->enabled_p
15552 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15553 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15554 < CHARPOS (new_start)))
15555 ++first_reusable_row;
15556
15557 /* Give up if there is no row to reuse. */
15558 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15559 || !first_reusable_row->enabled_p
15560 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15561 != CHARPOS (new_start)))
15562 return 0;
15563
15564 /* We can reuse fully visible rows beginning with
15565 first_reusable_row to the end of the window. Set
15566 first_row_to_display to the first row that cannot be reused.
15567 Set pt_row to the row containing point, if there is any. */
15568 pt_row = NULL;
15569 for (first_row_to_display = first_reusable_row;
15570 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15571 ++first_row_to_display)
15572 {
15573 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15574 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15575 pt_row = first_row_to_display;
15576 }
15577
15578 /* Start displaying at the start of first_row_to_display. */
15579 xassert (first_row_to_display->y < yb);
15580 init_to_row_start (&it, w, first_row_to_display);
15581
15582 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15583 - start_vpos);
15584 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15585 - nrows_scrolled);
15586 it.current_y = (first_row_to_display->y - first_reusable_row->y
15587 + WINDOW_HEADER_LINE_HEIGHT (w));
15588
15589 /* Display lines beginning with first_row_to_display in the
15590 desired matrix. Set last_text_row to the last row displayed
15591 that displays text. */
15592 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15593 if (pt_row == NULL)
15594 w->cursor.vpos = -1;
15595 last_text_row = NULL;
15596 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15597 if (display_line (&it))
15598 last_text_row = it.glyph_row - 1;
15599
15600 /* If point is in a reused row, adjust y and vpos of the cursor
15601 position. */
15602 if (pt_row)
15603 {
15604 w->cursor.vpos -= nrows_scrolled;
15605 w->cursor.y -= first_reusable_row->y - start_row->y;
15606 }
15607
15608 /* Give up if point isn't in a row displayed or reused. (This
15609 also handles the case where w->cursor.vpos < nrows_scrolled
15610 after the calls to display_line, which can happen with scroll
15611 margins. See bug#1295.) */
15612 if (w->cursor.vpos < 0)
15613 {
15614 clear_glyph_matrix (w->desired_matrix);
15615 return 0;
15616 }
15617
15618 /* Scroll the display. */
15619 run.current_y = first_reusable_row->y;
15620 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15621 run.height = it.last_visible_y - run.current_y;
15622 dy = run.current_y - run.desired_y;
15623
15624 if (run.height)
15625 {
15626 update_begin (f);
15627 FRAME_RIF (f)->update_window_begin_hook (w);
15628 FRAME_RIF (f)->clear_window_mouse_face (w);
15629 FRAME_RIF (f)->scroll_run_hook (w, &run);
15630 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15631 update_end (f);
15632 }
15633
15634 /* Adjust Y positions of reused rows. */
15635 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15636 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15637 max_y = it.last_visible_y;
15638 for (row = first_reusable_row; row < first_row_to_display; ++row)
15639 {
15640 row->y -= dy;
15641 row->visible_height = row->height;
15642 if (row->y < min_y)
15643 row->visible_height -= min_y - row->y;
15644 if (row->y + row->height > max_y)
15645 row->visible_height -= row->y + row->height - max_y;
15646 row->redraw_fringe_bitmaps_p = 1;
15647 }
15648
15649 /* Scroll the current matrix. */
15650 xassert (nrows_scrolled > 0);
15651 rotate_matrix (w->current_matrix,
15652 start_vpos,
15653 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15654 -nrows_scrolled);
15655
15656 /* Disable rows not reused. */
15657 for (row -= nrows_scrolled; row < bottom_row; ++row)
15658 row->enabled_p = 0;
15659
15660 /* Point may have moved to a different line, so we cannot assume that
15661 the previous cursor position is valid; locate the correct row. */
15662 if (pt_row)
15663 {
15664 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15665 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15666 row++)
15667 {
15668 w->cursor.vpos++;
15669 w->cursor.y = row->y;
15670 }
15671 if (row < bottom_row)
15672 {
15673 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15674 struct glyph *end = glyph + row->used[TEXT_AREA];
15675
15676 /* Can't use this optimization with bidi-reordered glyph
15677 rows, unless cursor is already at point. */
15678 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15679 {
15680 if (!(w->cursor.hpos >= 0
15681 && w->cursor.hpos < row->used[TEXT_AREA]
15682 && BUFFERP (glyph->object)
15683 && glyph->charpos == PT))
15684 return 0;
15685 }
15686 else
15687 for (; glyph < end
15688 && (!BUFFERP (glyph->object)
15689 || glyph->charpos < PT);
15690 glyph++)
15691 {
15692 w->cursor.hpos++;
15693 w->cursor.x += glyph->pixel_width;
15694 }
15695 }
15696 }
15697
15698 /* Adjust window end. A null value of last_text_row means that
15699 the window end is in reused rows which in turn means that
15700 only its vpos can have changed. */
15701 if (last_text_row)
15702 {
15703 w->window_end_bytepos
15704 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15705 w->window_end_pos
15706 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15707 w->window_end_vpos
15708 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15709 }
15710 else
15711 {
15712 w->window_end_vpos
15713 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15714 }
15715
15716 w->window_end_valid = Qnil;
15717 w->desired_matrix->no_scrolling_p = 1;
15718
15719 #if GLYPH_DEBUG
15720 debug_method_add (w, "try_window_reusing_current_matrix 2");
15721 #endif
15722 return 1;
15723 }
15724
15725 return 0;
15726 }
15727
15728
15729 \f
15730 /************************************************************************
15731 Window redisplay reusing current matrix when buffer has changed
15732 ************************************************************************/
15733
15734 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15735 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15736 EMACS_INT *, EMACS_INT *);
15737 static struct glyph_row *
15738 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15739 struct glyph_row *);
15740
15741
15742 /* Return the last row in MATRIX displaying text. If row START is
15743 non-null, start searching with that row. IT gives the dimensions
15744 of the display. Value is null if matrix is empty; otherwise it is
15745 a pointer to the row found. */
15746
15747 static struct glyph_row *
15748 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15749 struct glyph_row *start)
15750 {
15751 struct glyph_row *row, *row_found;
15752
15753 /* Set row_found to the last row in IT->w's current matrix
15754 displaying text. The loop looks funny but think of partially
15755 visible lines. */
15756 row_found = NULL;
15757 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15758 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15759 {
15760 xassert (row->enabled_p);
15761 row_found = row;
15762 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15763 break;
15764 ++row;
15765 }
15766
15767 return row_found;
15768 }
15769
15770
15771 /* Return the last row in the current matrix of W that is not affected
15772 by changes at the start of current_buffer that occurred since W's
15773 current matrix was built. Value is null if no such row exists.
15774
15775 BEG_UNCHANGED us the number of characters unchanged at the start of
15776 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15777 first changed character in current_buffer. Characters at positions <
15778 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15779 when the current matrix was built. */
15780
15781 static struct glyph_row *
15782 find_last_unchanged_at_beg_row (struct window *w)
15783 {
15784 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15785 struct glyph_row *row;
15786 struct glyph_row *row_found = NULL;
15787 int yb = window_text_bottom_y (w);
15788
15789 /* Find the last row displaying unchanged text. */
15790 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15791 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15792 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15793 ++row)
15794 {
15795 if (/* If row ends before first_changed_pos, it is unchanged,
15796 except in some case. */
15797 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15798 /* When row ends in ZV and we write at ZV it is not
15799 unchanged. */
15800 && !row->ends_at_zv_p
15801 /* When first_changed_pos is the end of a continued line,
15802 row is not unchanged because it may be no longer
15803 continued. */
15804 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15805 && (row->continued_p
15806 || row->exact_window_width_line_p)))
15807 row_found = row;
15808
15809 /* Stop if last visible row. */
15810 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15811 break;
15812 }
15813
15814 return row_found;
15815 }
15816
15817
15818 /* Find the first glyph row in the current matrix of W that is not
15819 affected by changes at the end of current_buffer since the
15820 time W's current matrix was built.
15821
15822 Return in *DELTA the number of chars by which buffer positions in
15823 unchanged text at the end of current_buffer must be adjusted.
15824
15825 Return in *DELTA_BYTES the corresponding number of bytes.
15826
15827 Value is null if no such row exists, i.e. all rows are affected by
15828 changes. */
15829
15830 static struct glyph_row *
15831 find_first_unchanged_at_end_row (struct window *w,
15832 EMACS_INT *delta, EMACS_INT *delta_bytes)
15833 {
15834 struct glyph_row *row;
15835 struct glyph_row *row_found = NULL;
15836
15837 *delta = *delta_bytes = 0;
15838
15839 /* Display must not have been paused, otherwise the current matrix
15840 is not up to date. */
15841 eassert (!NILP (w->window_end_valid));
15842
15843 /* A value of window_end_pos >= END_UNCHANGED means that the window
15844 end is in the range of changed text. If so, there is no
15845 unchanged row at the end of W's current matrix. */
15846 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15847 return NULL;
15848
15849 /* Set row to the last row in W's current matrix displaying text. */
15850 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15851
15852 /* If matrix is entirely empty, no unchanged row exists. */
15853 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15854 {
15855 /* The value of row is the last glyph row in the matrix having a
15856 meaningful buffer position in it. The end position of row
15857 corresponds to window_end_pos. This allows us to translate
15858 buffer positions in the current matrix to current buffer
15859 positions for characters not in changed text. */
15860 EMACS_INT Z_old =
15861 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15862 EMACS_INT Z_BYTE_old =
15863 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15864 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15865 struct glyph_row *first_text_row
15866 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15867
15868 *delta = Z - Z_old;
15869 *delta_bytes = Z_BYTE - Z_BYTE_old;
15870
15871 /* Set last_unchanged_pos to the buffer position of the last
15872 character in the buffer that has not been changed. Z is the
15873 index + 1 of the last character in current_buffer, i.e. by
15874 subtracting END_UNCHANGED we get the index of the last
15875 unchanged character, and we have to add BEG to get its buffer
15876 position. */
15877 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15878 last_unchanged_pos_old = last_unchanged_pos - *delta;
15879
15880 /* Search backward from ROW for a row displaying a line that
15881 starts at a minimum position >= last_unchanged_pos_old. */
15882 for (; row > first_text_row; --row)
15883 {
15884 /* This used to abort, but it can happen.
15885 It is ok to just stop the search instead here. KFS. */
15886 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15887 break;
15888
15889 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15890 row_found = row;
15891 }
15892 }
15893
15894 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15895
15896 return row_found;
15897 }
15898
15899
15900 /* Make sure that glyph rows in the current matrix of window W
15901 reference the same glyph memory as corresponding rows in the
15902 frame's frame matrix. This function is called after scrolling W's
15903 current matrix on a terminal frame in try_window_id and
15904 try_window_reusing_current_matrix. */
15905
15906 static void
15907 sync_frame_with_window_matrix_rows (struct window *w)
15908 {
15909 struct frame *f = XFRAME (w->frame);
15910 struct glyph_row *window_row, *window_row_end, *frame_row;
15911
15912 /* Preconditions: W must be a leaf window and full-width. Its frame
15913 must have a frame matrix. */
15914 xassert (NILP (w->hchild) && NILP (w->vchild));
15915 xassert (WINDOW_FULL_WIDTH_P (w));
15916 xassert (!FRAME_WINDOW_P (f));
15917
15918 /* If W is a full-width window, glyph pointers in W's current matrix
15919 have, by definition, to be the same as glyph pointers in the
15920 corresponding frame matrix. Note that frame matrices have no
15921 marginal areas (see build_frame_matrix). */
15922 window_row = w->current_matrix->rows;
15923 window_row_end = window_row + w->current_matrix->nrows;
15924 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15925 while (window_row < window_row_end)
15926 {
15927 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15928 struct glyph *end = window_row->glyphs[LAST_AREA];
15929
15930 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15931 frame_row->glyphs[TEXT_AREA] = start;
15932 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15933 frame_row->glyphs[LAST_AREA] = end;
15934
15935 /* Disable frame rows whose corresponding window rows have
15936 been disabled in try_window_id. */
15937 if (!window_row->enabled_p)
15938 frame_row->enabled_p = 0;
15939
15940 ++window_row, ++frame_row;
15941 }
15942 }
15943
15944
15945 /* Find the glyph row in window W containing CHARPOS. Consider all
15946 rows between START and END (not inclusive). END null means search
15947 all rows to the end of the display area of W. Value is the row
15948 containing CHARPOS or null. */
15949
15950 struct glyph_row *
15951 row_containing_pos (struct window *w, EMACS_INT charpos,
15952 struct glyph_row *start, struct glyph_row *end, int dy)
15953 {
15954 struct glyph_row *row = start;
15955 struct glyph_row *best_row = NULL;
15956 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15957 int last_y;
15958
15959 /* If we happen to start on a header-line, skip that. */
15960 if (row->mode_line_p)
15961 ++row;
15962
15963 if ((end && row >= end) || !row->enabled_p)
15964 return NULL;
15965
15966 last_y = window_text_bottom_y (w) - dy;
15967
15968 while (1)
15969 {
15970 /* Give up if we have gone too far. */
15971 if (end && row >= end)
15972 return NULL;
15973 /* This formerly returned if they were equal.
15974 I think that both quantities are of a "last plus one" type;
15975 if so, when they are equal, the row is within the screen. -- rms. */
15976 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15977 return NULL;
15978
15979 /* If it is in this row, return this row. */
15980 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15981 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15982 /* The end position of a row equals the start
15983 position of the next row. If CHARPOS is there, we
15984 would rather display it in the next line, except
15985 when this line ends in ZV. */
15986 && !row->ends_at_zv_p
15987 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15988 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15989 {
15990 struct glyph *g;
15991
15992 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15993 || (!best_row && !row->continued_p))
15994 return row;
15995 /* In bidi-reordered rows, there could be several rows
15996 occluding point, all of them belonging to the same
15997 continued line. We need to find the row which fits
15998 CHARPOS the best. */
15999 for (g = row->glyphs[TEXT_AREA];
16000 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16001 g++)
16002 {
16003 if (!STRINGP (g->object))
16004 {
16005 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16006 {
16007 mindif = eabs (g->charpos - charpos);
16008 best_row = row;
16009 /* Exact match always wins. */
16010 if (mindif == 0)
16011 return best_row;
16012 }
16013 }
16014 }
16015 }
16016 else if (best_row && !row->continued_p)
16017 return best_row;
16018 ++row;
16019 }
16020 }
16021
16022
16023 /* Try to redisplay window W by reusing its existing display. W's
16024 current matrix must be up to date when this function is called,
16025 i.e. window_end_valid must not be nil.
16026
16027 Value is
16028
16029 1 if display has been updated
16030 0 if otherwise unsuccessful
16031 -1 if redisplay with same window start is known not to succeed
16032
16033 The following steps are performed:
16034
16035 1. Find the last row in the current matrix of W that is not
16036 affected by changes at the start of current_buffer. If no such row
16037 is found, give up.
16038
16039 2. Find the first row in W's current matrix that is not affected by
16040 changes at the end of current_buffer. Maybe there is no such row.
16041
16042 3. Display lines beginning with the row + 1 found in step 1 to the
16043 row found in step 2 or, if step 2 didn't find a row, to the end of
16044 the window.
16045
16046 4. If cursor is not known to appear on the window, give up.
16047
16048 5. If display stopped at the row found in step 2, scroll the
16049 display and current matrix as needed.
16050
16051 6. Maybe display some lines at the end of W, if we must. This can
16052 happen under various circumstances, like a partially visible line
16053 becoming fully visible, or because newly displayed lines are displayed
16054 in smaller font sizes.
16055
16056 7. Update W's window end information. */
16057
16058 static int
16059 try_window_id (struct window *w)
16060 {
16061 struct frame *f = XFRAME (w->frame);
16062 struct glyph_matrix *current_matrix = w->current_matrix;
16063 struct glyph_matrix *desired_matrix = w->desired_matrix;
16064 struct glyph_row *last_unchanged_at_beg_row;
16065 struct glyph_row *first_unchanged_at_end_row;
16066 struct glyph_row *row;
16067 struct glyph_row *bottom_row;
16068 int bottom_vpos;
16069 struct it it;
16070 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16071 int dvpos, dy;
16072 struct text_pos start_pos;
16073 struct run run;
16074 int first_unchanged_at_end_vpos = 0;
16075 struct glyph_row *last_text_row, *last_text_row_at_end;
16076 struct text_pos start;
16077 EMACS_INT first_changed_charpos, last_changed_charpos;
16078
16079 #if GLYPH_DEBUG
16080 if (inhibit_try_window_id)
16081 return 0;
16082 #endif
16083
16084 /* This is handy for debugging. */
16085 #if 0
16086 #define GIVE_UP(X) \
16087 do { \
16088 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16089 return 0; \
16090 } while (0)
16091 #else
16092 #define GIVE_UP(X) return 0
16093 #endif
16094
16095 SET_TEXT_POS_FROM_MARKER (start, w->start);
16096
16097 /* Don't use this for mini-windows because these can show
16098 messages and mini-buffers, and we don't handle that here. */
16099 if (MINI_WINDOW_P (w))
16100 GIVE_UP (1);
16101
16102 /* This flag is used to prevent redisplay optimizations. */
16103 if (windows_or_buffers_changed || cursor_type_changed)
16104 GIVE_UP (2);
16105
16106 /* Verify that narrowing has not changed.
16107 Also verify that we were not told to prevent redisplay optimizations.
16108 It would be nice to further
16109 reduce the number of cases where this prevents try_window_id. */
16110 if (current_buffer->clip_changed
16111 || current_buffer->prevent_redisplay_optimizations_p)
16112 GIVE_UP (3);
16113
16114 /* Window must either use window-based redisplay or be full width. */
16115 if (!FRAME_WINDOW_P (f)
16116 && (!FRAME_LINE_INS_DEL_OK (f)
16117 || !WINDOW_FULL_WIDTH_P (w)))
16118 GIVE_UP (4);
16119
16120 /* Give up if point is known NOT to appear in W. */
16121 if (PT < CHARPOS (start))
16122 GIVE_UP (5);
16123
16124 /* Another way to prevent redisplay optimizations. */
16125 if (XFASTINT (w->last_modified) == 0)
16126 GIVE_UP (6);
16127
16128 /* Verify that window is not hscrolled. */
16129 if (XFASTINT (w->hscroll) != 0)
16130 GIVE_UP (7);
16131
16132 /* Verify that display wasn't paused. */
16133 if (NILP (w->window_end_valid))
16134 GIVE_UP (8);
16135
16136 /* Can't use this if highlighting a region because a cursor movement
16137 will do more than just set the cursor. */
16138 if (!NILP (Vtransient_mark_mode)
16139 && !NILP (BVAR (current_buffer, mark_active)))
16140 GIVE_UP (9);
16141
16142 /* Likewise if highlighting trailing whitespace. */
16143 if (!NILP (Vshow_trailing_whitespace))
16144 GIVE_UP (11);
16145
16146 /* Likewise if showing a region. */
16147 if (!NILP (w->region_showing))
16148 GIVE_UP (10);
16149
16150 /* Can't use this if overlay arrow position and/or string have
16151 changed. */
16152 if (overlay_arrows_changed_p ())
16153 GIVE_UP (12);
16154
16155 /* When word-wrap is on, adding a space to the first word of a
16156 wrapped line can change the wrap position, altering the line
16157 above it. It might be worthwhile to handle this more
16158 intelligently, but for now just redisplay from scratch. */
16159 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16160 GIVE_UP (21);
16161
16162 /* Under bidi reordering, adding or deleting a character in the
16163 beginning of a paragraph, before the first strong directional
16164 character, can change the base direction of the paragraph (unless
16165 the buffer specifies a fixed paragraph direction), which will
16166 require to redisplay the whole paragraph. It might be worthwhile
16167 to find the paragraph limits and widen the range of redisplayed
16168 lines to that, but for now just give up this optimization and
16169 redisplay from scratch. */
16170 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16171 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16172 GIVE_UP (22);
16173
16174 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16175 only if buffer has really changed. The reason is that the gap is
16176 initially at Z for freshly visited files. The code below would
16177 set end_unchanged to 0 in that case. */
16178 if (MODIFF > SAVE_MODIFF
16179 /* This seems to happen sometimes after saving a buffer. */
16180 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16181 {
16182 if (GPT - BEG < BEG_UNCHANGED)
16183 BEG_UNCHANGED = GPT - BEG;
16184 if (Z - GPT < END_UNCHANGED)
16185 END_UNCHANGED = Z - GPT;
16186 }
16187
16188 /* The position of the first and last character that has been changed. */
16189 first_changed_charpos = BEG + BEG_UNCHANGED;
16190 last_changed_charpos = Z - END_UNCHANGED;
16191
16192 /* If window starts after a line end, and the last change is in
16193 front of that newline, then changes don't affect the display.
16194 This case happens with stealth-fontification. Note that although
16195 the display is unchanged, glyph positions in the matrix have to
16196 be adjusted, of course. */
16197 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16198 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16199 && ((last_changed_charpos < CHARPOS (start)
16200 && CHARPOS (start) == BEGV)
16201 || (last_changed_charpos < CHARPOS (start) - 1
16202 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16203 {
16204 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16205 struct glyph_row *r0;
16206
16207 /* Compute how many chars/bytes have been added to or removed
16208 from the buffer. */
16209 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16210 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16211 Z_delta = Z - Z_old;
16212 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16213
16214 /* Give up if PT is not in the window. Note that it already has
16215 been checked at the start of try_window_id that PT is not in
16216 front of the window start. */
16217 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16218 GIVE_UP (13);
16219
16220 /* If window start is unchanged, we can reuse the whole matrix
16221 as is, after adjusting glyph positions. No need to compute
16222 the window end again, since its offset from Z hasn't changed. */
16223 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16224 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16225 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16226 /* PT must not be in a partially visible line. */
16227 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16228 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16229 {
16230 /* Adjust positions in the glyph matrix. */
16231 if (Z_delta || Z_delta_bytes)
16232 {
16233 struct glyph_row *r1
16234 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16235 increment_matrix_positions (w->current_matrix,
16236 MATRIX_ROW_VPOS (r0, current_matrix),
16237 MATRIX_ROW_VPOS (r1, current_matrix),
16238 Z_delta, Z_delta_bytes);
16239 }
16240
16241 /* Set the cursor. */
16242 row = row_containing_pos (w, PT, r0, NULL, 0);
16243 if (row)
16244 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16245 else
16246 abort ();
16247 return 1;
16248 }
16249 }
16250
16251 /* Handle the case that changes are all below what is displayed in
16252 the window, and that PT is in the window. This shortcut cannot
16253 be taken if ZV is visible in the window, and text has been added
16254 there that is visible in the window. */
16255 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16256 /* ZV is not visible in the window, or there are no
16257 changes at ZV, actually. */
16258 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16259 || first_changed_charpos == last_changed_charpos))
16260 {
16261 struct glyph_row *r0;
16262
16263 /* Give up if PT is not in the window. Note that it already has
16264 been checked at the start of try_window_id that PT is not in
16265 front of the window start. */
16266 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16267 GIVE_UP (14);
16268
16269 /* If window start is unchanged, we can reuse the whole matrix
16270 as is, without changing glyph positions since no text has
16271 been added/removed in front of the window end. */
16272 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16273 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16274 /* PT must not be in a partially visible line. */
16275 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16276 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16277 {
16278 /* We have to compute the window end anew since text
16279 could have been added/removed after it. */
16280 w->window_end_pos
16281 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16282 w->window_end_bytepos
16283 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16284
16285 /* Set the cursor. */
16286 row = row_containing_pos (w, PT, r0, NULL, 0);
16287 if (row)
16288 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16289 else
16290 abort ();
16291 return 2;
16292 }
16293 }
16294
16295 /* Give up if window start is in the changed area.
16296
16297 The condition used to read
16298
16299 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16300
16301 but why that was tested escapes me at the moment. */
16302 if (CHARPOS (start) >= first_changed_charpos
16303 && CHARPOS (start) <= last_changed_charpos)
16304 GIVE_UP (15);
16305
16306 /* Check that window start agrees with the start of the first glyph
16307 row in its current matrix. Check this after we know the window
16308 start is not in changed text, otherwise positions would not be
16309 comparable. */
16310 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16311 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16312 GIVE_UP (16);
16313
16314 /* Give up if the window ends in strings. Overlay strings
16315 at the end are difficult to handle, so don't try. */
16316 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16317 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16318 GIVE_UP (20);
16319
16320 /* Compute the position at which we have to start displaying new
16321 lines. Some of the lines at the top of the window might be
16322 reusable because they are not displaying changed text. Find the
16323 last row in W's current matrix not affected by changes at the
16324 start of current_buffer. Value is null if changes start in the
16325 first line of window. */
16326 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16327 if (last_unchanged_at_beg_row)
16328 {
16329 /* Avoid starting to display in the moddle of a character, a TAB
16330 for instance. This is easier than to set up the iterator
16331 exactly, and it's not a frequent case, so the additional
16332 effort wouldn't really pay off. */
16333 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16334 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16335 && last_unchanged_at_beg_row > w->current_matrix->rows)
16336 --last_unchanged_at_beg_row;
16337
16338 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16339 GIVE_UP (17);
16340
16341 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16342 GIVE_UP (18);
16343 start_pos = it.current.pos;
16344
16345 /* Start displaying new lines in the desired matrix at the same
16346 vpos we would use in the current matrix, i.e. below
16347 last_unchanged_at_beg_row. */
16348 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16349 current_matrix);
16350 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16351 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16352
16353 xassert (it.hpos == 0 && it.current_x == 0);
16354 }
16355 else
16356 {
16357 /* There are no reusable lines at the start of the window.
16358 Start displaying in the first text line. */
16359 start_display (&it, w, start);
16360 it.vpos = it.first_vpos;
16361 start_pos = it.current.pos;
16362 }
16363
16364 /* Find the first row that is not affected by changes at the end of
16365 the buffer. Value will be null if there is no unchanged row, in
16366 which case we must redisplay to the end of the window. delta
16367 will be set to the value by which buffer positions beginning with
16368 first_unchanged_at_end_row have to be adjusted due to text
16369 changes. */
16370 first_unchanged_at_end_row
16371 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16372 IF_DEBUG (debug_delta = delta);
16373 IF_DEBUG (debug_delta_bytes = delta_bytes);
16374
16375 /* Set stop_pos to the buffer position up to which we will have to
16376 display new lines. If first_unchanged_at_end_row != NULL, this
16377 is the buffer position of the start of the line displayed in that
16378 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16379 that we don't stop at a buffer position. */
16380 stop_pos = 0;
16381 if (first_unchanged_at_end_row)
16382 {
16383 xassert (last_unchanged_at_beg_row == NULL
16384 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16385
16386 /* If this is a continuation line, move forward to the next one
16387 that isn't. Changes in lines above affect this line.
16388 Caution: this may move first_unchanged_at_end_row to a row
16389 not displaying text. */
16390 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16391 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16392 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16393 < it.last_visible_y))
16394 ++first_unchanged_at_end_row;
16395
16396 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16397 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16398 >= it.last_visible_y))
16399 first_unchanged_at_end_row = NULL;
16400 else
16401 {
16402 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16403 + delta);
16404 first_unchanged_at_end_vpos
16405 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16406 xassert (stop_pos >= Z - END_UNCHANGED);
16407 }
16408 }
16409 else if (last_unchanged_at_beg_row == NULL)
16410 GIVE_UP (19);
16411
16412
16413 #if GLYPH_DEBUG
16414
16415 /* Either there is no unchanged row at the end, or the one we have
16416 now displays text. This is a necessary condition for the window
16417 end pos calculation at the end of this function. */
16418 xassert (first_unchanged_at_end_row == NULL
16419 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16420
16421 debug_last_unchanged_at_beg_vpos
16422 = (last_unchanged_at_beg_row
16423 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16424 : -1);
16425 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16426
16427 #endif /* GLYPH_DEBUG != 0 */
16428
16429
16430 /* Display new lines. Set last_text_row to the last new line
16431 displayed which has text on it, i.e. might end up as being the
16432 line where the window_end_vpos is. */
16433 w->cursor.vpos = -1;
16434 last_text_row = NULL;
16435 overlay_arrow_seen = 0;
16436 while (it.current_y < it.last_visible_y
16437 && !fonts_changed_p
16438 && (first_unchanged_at_end_row == NULL
16439 || IT_CHARPOS (it) < stop_pos))
16440 {
16441 if (display_line (&it))
16442 last_text_row = it.glyph_row - 1;
16443 }
16444
16445 if (fonts_changed_p)
16446 return -1;
16447
16448
16449 /* Compute differences in buffer positions, y-positions etc. for
16450 lines reused at the bottom of the window. Compute what we can
16451 scroll. */
16452 if (first_unchanged_at_end_row
16453 /* No lines reused because we displayed everything up to the
16454 bottom of the window. */
16455 && it.current_y < it.last_visible_y)
16456 {
16457 dvpos = (it.vpos
16458 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16459 current_matrix));
16460 dy = it.current_y - first_unchanged_at_end_row->y;
16461 run.current_y = first_unchanged_at_end_row->y;
16462 run.desired_y = run.current_y + dy;
16463 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16464 }
16465 else
16466 {
16467 delta = delta_bytes = dvpos = dy
16468 = run.current_y = run.desired_y = run.height = 0;
16469 first_unchanged_at_end_row = NULL;
16470 }
16471 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16472
16473
16474 /* Find the cursor if not already found. We have to decide whether
16475 PT will appear on this window (it sometimes doesn't, but this is
16476 not a very frequent case.) This decision has to be made before
16477 the current matrix is altered. A value of cursor.vpos < 0 means
16478 that PT is either in one of the lines beginning at
16479 first_unchanged_at_end_row or below the window. Don't care for
16480 lines that might be displayed later at the window end; as
16481 mentioned, this is not a frequent case. */
16482 if (w->cursor.vpos < 0)
16483 {
16484 /* Cursor in unchanged rows at the top? */
16485 if (PT < CHARPOS (start_pos)
16486 && last_unchanged_at_beg_row)
16487 {
16488 row = row_containing_pos (w, PT,
16489 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16490 last_unchanged_at_beg_row + 1, 0);
16491 if (row)
16492 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16493 }
16494
16495 /* Start from first_unchanged_at_end_row looking for PT. */
16496 else if (first_unchanged_at_end_row)
16497 {
16498 row = row_containing_pos (w, PT - delta,
16499 first_unchanged_at_end_row, NULL, 0);
16500 if (row)
16501 set_cursor_from_row (w, row, w->current_matrix, delta,
16502 delta_bytes, dy, dvpos);
16503 }
16504
16505 /* Give up if cursor was not found. */
16506 if (w->cursor.vpos < 0)
16507 {
16508 clear_glyph_matrix (w->desired_matrix);
16509 return -1;
16510 }
16511 }
16512
16513 /* Don't let the cursor end in the scroll margins. */
16514 {
16515 int this_scroll_margin, cursor_height;
16516
16517 this_scroll_margin = max (0, scroll_margin);
16518 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16519 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16520 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16521
16522 if ((w->cursor.y < this_scroll_margin
16523 && CHARPOS (start) > BEGV)
16524 /* Old redisplay didn't take scroll margin into account at the bottom,
16525 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16526 || (w->cursor.y + (make_cursor_line_fully_visible_p
16527 ? cursor_height + this_scroll_margin
16528 : 1)) > it.last_visible_y)
16529 {
16530 w->cursor.vpos = -1;
16531 clear_glyph_matrix (w->desired_matrix);
16532 return -1;
16533 }
16534 }
16535
16536 /* Scroll the display. Do it before changing the current matrix so
16537 that xterm.c doesn't get confused about where the cursor glyph is
16538 found. */
16539 if (dy && run.height)
16540 {
16541 update_begin (f);
16542
16543 if (FRAME_WINDOW_P (f))
16544 {
16545 FRAME_RIF (f)->update_window_begin_hook (w);
16546 FRAME_RIF (f)->clear_window_mouse_face (w);
16547 FRAME_RIF (f)->scroll_run_hook (w, &run);
16548 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16549 }
16550 else
16551 {
16552 /* Terminal frame. In this case, dvpos gives the number of
16553 lines to scroll by; dvpos < 0 means scroll up. */
16554 int from_vpos
16555 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16556 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16557 int end = (WINDOW_TOP_EDGE_LINE (w)
16558 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16559 + window_internal_height (w));
16560
16561 #if defined (HAVE_GPM) || defined (MSDOS)
16562 x_clear_window_mouse_face (w);
16563 #endif
16564 /* Perform the operation on the screen. */
16565 if (dvpos > 0)
16566 {
16567 /* Scroll last_unchanged_at_beg_row to the end of the
16568 window down dvpos lines. */
16569 set_terminal_window (f, end);
16570
16571 /* On dumb terminals delete dvpos lines at the end
16572 before inserting dvpos empty lines. */
16573 if (!FRAME_SCROLL_REGION_OK (f))
16574 ins_del_lines (f, end - dvpos, -dvpos);
16575
16576 /* Insert dvpos empty lines in front of
16577 last_unchanged_at_beg_row. */
16578 ins_del_lines (f, from, dvpos);
16579 }
16580 else if (dvpos < 0)
16581 {
16582 /* Scroll up last_unchanged_at_beg_vpos to the end of
16583 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16584 set_terminal_window (f, end);
16585
16586 /* Delete dvpos lines in front of
16587 last_unchanged_at_beg_vpos. ins_del_lines will set
16588 the cursor to the given vpos and emit |dvpos| delete
16589 line sequences. */
16590 ins_del_lines (f, from + dvpos, dvpos);
16591
16592 /* On a dumb terminal insert dvpos empty lines at the
16593 end. */
16594 if (!FRAME_SCROLL_REGION_OK (f))
16595 ins_del_lines (f, end + dvpos, -dvpos);
16596 }
16597
16598 set_terminal_window (f, 0);
16599 }
16600
16601 update_end (f);
16602 }
16603
16604 /* Shift reused rows of the current matrix to the right position.
16605 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16606 text. */
16607 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16608 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16609 if (dvpos < 0)
16610 {
16611 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16612 bottom_vpos, dvpos);
16613 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16614 bottom_vpos, 0);
16615 }
16616 else if (dvpos > 0)
16617 {
16618 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16619 bottom_vpos, dvpos);
16620 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16621 first_unchanged_at_end_vpos + dvpos, 0);
16622 }
16623
16624 /* For frame-based redisplay, make sure that current frame and window
16625 matrix are in sync with respect to glyph memory. */
16626 if (!FRAME_WINDOW_P (f))
16627 sync_frame_with_window_matrix_rows (w);
16628
16629 /* Adjust buffer positions in reused rows. */
16630 if (delta || delta_bytes)
16631 increment_matrix_positions (current_matrix,
16632 first_unchanged_at_end_vpos + dvpos,
16633 bottom_vpos, delta, delta_bytes);
16634
16635 /* Adjust Y positions. */
16636 if (dy)
16637 shift_glyph_matrix (w, current_matrix,
16638 first_unchanged_at_end_vpos + dvpos,
16639 bottom_vpos, dy);
16640
16641 if (first_unchanged_at_end_row)
16642 {
16643 first_unchanged_at_end_row += dvpos;
16644 if (first_unchanged_at_end_row->y >= it.last_visible_y
16645 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16646 first_unchanged_at_end_row = NULL;
16647 }
16648
16649 /* If scrolling up, there may be some lines to display at the end of
16650 the window. */
16651 last_text_row_at_end = NULL;
16652 if (dy < 0)
16653 {
16654 /* Scrolling up can leave for example a partially visible line
16655 at the end of the window to be redisplayed. */
16656 /* Set last_row to the glyph row in the current matrix where the
16657 window end line is found. It has been moved up or down in
16658 the matrix by dvpos. */
16659 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16660 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16661
16662 /* If last_row is the window end line, it should display text. */
16663 xassert (last_row->displays_text_p);
16664
16665 /* If window end line was partially visible before, begin
16666 displaying at that line. Otherwise begin displaying with the
16667 line following it. */
16668 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16669 {
16670 init_to_row_start (&it, w, last_row);
16671 it.vpos = last_vpos;
16672 it.current_y = last_row->y;
16673 }
16674 else
16675 {
16676 init_to_row_end (&it, w, last_row);
16677 it.vpos = 1 + last_vpos;
16678 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16679 ++last_row;
16680 }
16681
16682 /* We may start in a continuation line. If so, we have to
16683 get the right continuation_lines_width and current_x. */
16684 it.continuation_lines_width = last_row->continuation_lines_width;
16685 it.hpos = it.current_x = 0;
16686
16687 /* Display the rest of the lines at the window end. */
16688 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16689 while (it.current_y < it.last_visible_y
16690 && !fonts_changed_p)
16691 {
16692 /* Is it always sure that the display agrees with lines in
16693 the current matrix? I don't think so, so we mark rows
16694 displayed invalid in the current matrix by setting their
16695 enabled_p flag to zero. */
16696 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16697 if (display_line (&it))
16698 last_text_row_at_end = it.glyph_row - 1;
16699 }
16700 }
16701
16702 /* Update window_end_pos and window_end_vpos. */
16703 if (first_unchanged_at_end_row
16704 && !last_text_row_at_end)
16705 {
16706 /* Window end line if one of the preserved rows from the current
16707 matrix. Set row to the last row displaying text in current
16708 matrix starting at first_unchanged_at_end_row, after
16709 scrolling. */
16710 xassert (first_unchanged_at_end_row->displays_text_p);
16711 row = find_last_row_displaying_text (w->current_matrix, &it,
16712 first_unchanged_at_end_row);
16713 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16714
16715 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16716 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16717 w->window_end_vpos
16718 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16719 xassert (w->window_end_bytepos >= 0);
16720 IF_DEBUG (debug_method_add (w, "A"));
16721 }
16722 else if (last_text_row_at_end)
16723 {
16724 w->window_end_pos
16725 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16726 w->window_end_bytepos
16727 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16728 w->window_end_vpos
16729 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16730 xassert (w->window_end_bytepos >= 0);
16731 IF_DEBUG (debug_method_add (w, "B"));
16732 }
16733 else if (last_text_row)
16734 {
16735 /* We have displayed either to the end of the window or at the
16736 end of the window, i.e. the last row with text is to be found
16737 in the desired matrix. */
16738 w->window_end_pos
16739 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16740 w->window_end_bytepos
16741 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16742 w->window_end_vpos
16743 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16744 xassert (w->window_end_bytepos >= 0);
16745 }
16746 else if (first_unchanged_at_end_row == NULL
16747 && last_text_row == NULL
16748 && last_text_row_at_end == NULL)
16749 {
16750 /* Displayed to end of window, but no line containing text was
16751 displayed. Lines were deleted at the end of the window. */
16752 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16753 int vpos = XFASTINT (w->window_end_vpos);
16754 struct glyph_row *current_row = current_matrix->rows + vpos;
16755 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16756
16757 for (row = NULL;
16758 row == NULL && vpos >= first_vpos;
16759 --vpos, --current_row, --desired_row)
16760 {
16761 if (desired_row->enabled_p)
16762 {
16763 if (desired_row->displays_text_p)
16764 row = desired_row;
16765 }
16766 else if (current_row->displays_text_p)
16767 row = current_row;
16768 }
16769
16770 xassert (row != NULL);
16771 w->window_end_vpos = make_number (vpos + 1);
16772 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16773 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16774 xassert (w->window_end_bytepos >= 0);
16775 IF_DEBUG (debug_method_add (w, "C"));
16776 }
16777 else
16778 abort ();
16779
16780 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16781 debug_end_vpos = XFASTINT (w->window_end_vpos));
16782
16783 /* Record that display has not been completed. */
16784 w->window_end_valid = Qnil;
16785 w->desired_matrix->no_scrolling_p = 1;
16786 return 3;
16787
16788 #undef GIVE_UP
16789 }
16790
16791
16792 \f
16793 /***********************************************************************
16794 More debugging support
16795 ***********************************************************************/
16796
16797 #if GLYPH_DEBUG
16798
16799 void dump_glyph_row (struct glyph_row *, int, int);
16800 void dump_glyph_matrix (struct glyph_matrix *, int);
16801 void dump_glyph (struct glyph_row *, struct glyph *, int);
16802
16803
16804 /* Dump the contents of glyph matrix MATRIX on stderr.
16805
16806 GLYPHS 0 means don't show glyph contents.
16807 GLYPHS 1 means show glyphs in short form
16808 GLYPHS > 1 means show glyphs in long form. */
16809
16810 void
16811 dump_glyph_matrix (matrix, glyphs)
16812 struct glyph_matrix *matrix;
16813 int glyphs;
16814 {
16815 int i;
16816 for (i = 0; i < matrix->nrows; ++i)
16817 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16818 }
16819
16820
16821 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16822 the glyph row and area where the glyph comes from. */
16823
16824 void
16825 dump_glyph (row, glyph, area)
16826 struct glyph_row *row;
16827 struct glyph *glyph;
16828 int area;
16829 {
16830 if (glyph->type == CHAR_GLYPH)
16831 {
16832 fprintf (stderr,
16833 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16834 glyph - row->glyphs[TEXT_AREA],
16835 'C',
16836 glyph->charpos,
16837 (BUFFERP (glyph->object)
16838 ? 'B'
16839 : (STRINGP (glyph->object)
16840 ? 'S'
16841 : '-')),
16842 glyph->pixel_width,
16843 glyph->u.ch,
16844 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16845 ? glyph->u.ch
16846 : '.'),
16847 glyph->face_id,
16848 glyph->left_box_line_p,
16849 glyph->right_box_line_p);
16850 }
16851 else if (glyph->type == STRETCH_GLYPH)
16852 {
16853 fprintf (stderr,
16854 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16855 glyph - row->glyphs[TEXT_AREA],
16856 'S',
16857 glyph->charpos,
16858 (BUFFERP (glyph->object)
16859 ? 'B'
16860 : (STRINGP (glyph->object)
16861 ? 'S'
16862 : '-')),
16863 glyph->pixel_width,
16864 0,
16865 '.',
16866 glyph->face_id,
16867 glyph->left_box_line_p,
16868 glyph->right_box_line_p);
16869 }
16870 else if (glyph->type == IMAGE_GLYPH)
16871 {
16872 fprintf (stderr,
16873 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16874 glyph - row->glyphs[TEXT_AREA],
16875 'I',
16876 glyph->charpos,
16877 (BUFFERP (glyph->object)
16878 ? 'B'
16879 : (STRINGP (glyph->object)
16880 ? 'S'
16881 : '-')),
16882 glyph->pixel_width,
16883 glyph->u.img_id,
16884 '.',
16885 glyph->face_id,
16886 glyph->left_box_line_p,
16887 glyph->right_box_line_p);
16888 }
16889 else if (glyph->type == COMPOSITE_GLYPH)
16890 {
16891 fprintf (stderr,
16892 " %5d %4c %6d %c %3d 0x%05x",
16893 glyph - row->glyphs[TEXT_AREA],
16894 '+',
16895 glyph->charpos,
16896 (BUFFERP (glyph->object)
16897 ? 'B'
16898 : (STRINGP (glyph->object)
16899 ? 'S'
16900 : '-')),
16901 glyph->pixel_width,
16902 glyph->u.cmp.id);
16903 if (glyph->u.cmp.automatic)
16904 fprintf (stderr,
16905 "[%d-%d]",
16906 glyph->slice.cmp.from, glyph->slice.cmp.to);
16907 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16908 glyph->face_id,
16909 glyph->left_box_line_p,
16910 glyph->right_box_line_p);
16911 }
16912 }
16913
16914
16915 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16916 GLYPHS 0 means don't show glyph contents.
16917 GLYPHS 1 means show glyphs in short form
16918 GLYPHS > 1 means show glyphs in long form. */
16919
16920 void
16921 dump_glyph_row (row, vpos, glyphs)
16922 struct glyph_row *row;
16923 int vpos, glyphs;
16924 {
16925 if (glyphs != 1)
16926 {
16927 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16928 fprintf (stderr, "======================================================================\n");
16929
16930 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16931 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16932 vpos,
16933 MATRIX_ROW_START_CHARPOS (row),
16934 MATRIX_ROW_END_CHARPOS (row),
16935 row->used[TEXT_AREA],
16936 row->contains_overlapping_glyphs_p,
16937 row->enabled_p,
16938 row->truncated_on_left_p,
16939 row->truncated_on_right_p,
16940 row->continued_p,
16941 MATRIX_ROW_CONTINUATION_LINE_P (row),
16942 row->displays_text_p,
16943 row->ends_at_zv_p,
16944 row->fill_line_p,
16945 row->ends_in_middle_of_char_p,
16946 row->starts_in_middle_of_char_p,
16947 row->mouse_face_p,
16948 row->x,
16949 row->y,
16950 row->pixel_width,
16951 row->height,
16952 row->visible_height,
16953 row->ascent,
16954 row->phys_ascent);
16955 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16956 row->end.overlay_string_index,
16957 row->continuation_lines_width);
16958 fprintf (stderr, "%9d %5d\n",
16959 CHARPOS (row->start.string_pos),
16960 CHARPOS (row->end.string_pos));
16961 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16962 row->end.dpvec_index);
16963 }
16964
16965 if (glyphs > 1)
16966 {
16967 int area;
16968
16969 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16970 {
16971 struct glyph *glyph = row->glyphs[area];
16972 struct glyph *glyph_end = glyph + row->used[area];
16973
16974 /* Glyph for a line end in text. */
16975 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16976 ++glyph_end;
16977
16978 if (glyph < glyph_end)
16979 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16980
16981 for (; glyph < glyph_end; ++glyph)
16982 dump_glyph (row, glyph, area);
16983 }
16984 }
16985 else if (glyphs == 1)
16986 {
16987 int area;
16988
16989 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16990 {
16991 char *s = (char *) alloca (row->used[area] + 1);
16992 int i;
16993
16994 for (i = 0; i < row->used[area]; ++i)
16995 {
16996 struct glyph *glyph = row->glyphs[area] + i;
16997 if (glyph->type == CHAR_GLYPH
16998 && glyph->u.ch < 0x80
16999 && glyph->u.ch >= ' ')
17000 s[i] = glyph->u.ch;
17001 else
17002 s[i] = '.';
17003 }
17004
17005 s[i] = '\0';
17006 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17007 }
17008 }
17009 }
17010
17011
17012 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17013 Sdump_glyph_matrix, 0, 1, "p",
17014 doc: /* Dump the current matrix of the selected window to stderr.
17015 Shows contents of glyph row structures. With non-nil
17016 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17017 glyphs in short form, otherwise show glyphs in long form. */)
17018 (Lisp_Object glyphs)
17019 {
17020 struct window *w = XWINDOW (selected_window);
17021 struct buffer *buffer = XBUFFER (w->buffer);
17022
17023 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
17024 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17025 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17026 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17027 fprintf (stderr, "=============================================\n");
17028 dump_glyph_matrix (w->current_matrix,
17029 NILP (glyphs) ? 0 : XINT (glyphs));
17030 return Qnil;
17031 }
17032
17033
17034 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17035 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17036 (void)
17037 {
17038 struct frame *f = XFRAME (selected_frame);
17039 dump_glyph_matrix (f->current_matrix, 1);
17040 return Qnil;
17041 }
17042
17043
17044 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17045 doc: /* Dump glyph row ROW to stderr.
17046 GLYPH 0 means don't dump glyphs.
17047 GLYPH 1 means dump glyphs in short form.
17048 GLYPH > 1 or omitted means dump glyphs in long form. */)
17049 (Lisp_Object row, Lisp_Object glyphs)
17050 {
17051 struct glyph_matrix *matrix;
17052 int vpos;
17053
17054 CHECK_NUMBER (row);
17055 matrix = XWINDOW (selected_window)->current_matrix;
17056 vpos = XINT (row);
17057 if (vpos >= 0 && vpos < matrix->nrows)
17058 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17059 vpos,
17060 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17061 return Qnil;
17062 }
17063
17064
17065 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17066 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17067 GLYPH 0 means don't dump glyphs.
17068 GLYPH 1 means dump glyphs in short form.
17069 GLYPH > 1 or omitted means dump glyphs in long form. */)
17070 (Lisp_Object row, Lisp_Object glyphs)
17071 {
17072 struct frame *sf = SELECTED_FRAME ();
17073 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17074 int vpos;
17075
17076 CHECK_NUMBER (row);
17077 vpos = XINT (row);
17078 if (vpos >= 0 && vpos < m->nrows)
17079 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17080 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17081 return Qnil;
17082 }
17083
17084
17085 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17086 doc: /* Toggle tracing of redisplay.
17087 With ARG, turn tracing on if and only if ARG is positive. */)
17088 (Lisp_Object arg)
17089 {
17090 if (NILP (arg))
17091 trace_redisplay_p = !trace_redisplay_p;
17092 else
17093 {
17094 arg = Fprefix_numeric_value (arg);
17095 trace_redisplay_p = XINT (arg) > 0;
17096 }
17097
17098 return Qnil;
17099 }
17100
17101
17102 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17103 doc: /* Like `format', but print result to stderr.
17104 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17105 (size_t nargs, Lisp_Object *args)
17106 {
17107 Lisp_Object s = Fformat (nargs, args);
17108 fprintf (stderr, "%s", SDATA (s));
17109 return Qnil;
17110 }
17111
17112 #endif /* GLYPH_DEBUG */
17113
17114
17115 \f
17116 /***********************************************************************
17117 Building Desired Matrix Rows
17118 ***********************************************************************/
17119
17120 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17121 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17122
17123 static struct glyph_row *
17124 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17125 {
17126 struct frame *f = XFRAME (WINDOW_FRAME (w));
17127 struct buffer *buffer = XBUFFER (w->buffer);
17128 struct buffer *old = current_buffer;
17129 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17130 int arrow_len = SCHARS (overlay_arrow_string);
17131 const unsigned char *arrow_end = arrow_string + arrow_len;
17132 const unsigned char *p;
17133 struct it it;
17134 int multibyte_p;
17135 int n_glyphs_before;
17136
17137 set_buffer_temp (buffer);
17138 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17139 it.glyph_row->used[TEXT_AREA] = 0;
17140 SET_TEXT_POS (it.position, 0, 0);
17141
17142 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17143 p = arrow_string;
17144 while (p < arrow_end)
17145 {
17146 Lisp_Object face, ilisp;
17147
17148 /* Get the next character. */
17149 if (multibyte_p)
17150 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17151 else
17152 {
17153 it.c = it.char_to_display = *p, it.len = 1;
17154 if (! ASCII_CHAR_P (it.c))
17155 it.char_to_display = BYTE8_TO_CHAR (it.c);
17156 }
17157 p += it.len;
17158
17159 /* Get its face. */
17160 ilisp = make_number (p - arrow_string);
17161 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17162 it.face_id = compute_char_face (f, it.char_to_display, face);
17163
17164 /* Compute its width, get its glyphs. */
17165 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17166 SET_TEXT_POS (it.position, -1, -1);
17167 PRODUCE_GLYPHS (&it);
17168
17169 /* If this character doesn't fit any more in the line, we have
17170 to remove some glyphs. */
17171 if (it.current_x > it.last_visible_x)
17172 {
17173 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17174 break;
17175 }
17176 }
17177
17178 set_buffer_temp (old);
17179 return it.glyph_row;
17180 }
17181
17182
17183 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17184 glyphs are only inserted for terminal frames since we can't really
17185 win with truncation glyphs when partially visible glyphs are
17186 involved. Which glyphs to insert is determined by
17187 produce_special_glyphs. */
17188
17189 static void
17190 insert_left_trunc_glyphs (struct it *it)
17191 {
17192 struct it truncate_it;
17193 struct glyph *from, *end, *to, *toend;
17194
17195 xassert (!FRAME_WINDOW_P (it->f));
17196
17197 /* Get the truncation glyphs. */
17198 truncate_it = *it;
17199 truncate_it.current_x = 0;
17200 truncate_it.face_id = DEFAULT_FACE_ID;
17201 truncate_it.glyph_row = &scratch_glyph_row;
17202 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17203 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17204 truncate_it.object = make_number (0);
17205 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17206
17207 /* Overwrite glyphs from IT with truncation glyphs. */
17208 if (!it->glyph_row->reversed_p)
17209 {
17210 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17211 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17212 to = it->glyph_row->glyphs[TEXT_AREA];
17213 toend = to + it->glyph_row->used[TEXT_AREA];
17214
17215 while (from < end)
17216 *to++ = *from++;
17217
17218 /* There may be padding glyphs left over. Overwrite them too. */
17219 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17220 {
17221 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17222 while (from < end)
17223 *to++ = *from++;
17224 }
17225
17226 if (to > toend)
17227 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17228 }
17229 else
17230 {
17231 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17232 that back to front. */
17233 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17234 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17235 toend = it->glyph_row->glyphs[TEXT_AREA];
17236 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17237
17238 while (from >= end && to >= toend)
17239 *to-- = *from--;
17240 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17241 {
17242 from =
17243 truncate_it.glyph_row->glyphs[TEXT_AREA]
17244 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17245 while (from >= end && to >= toend)
17246 *to-- = *from--;
17247 }
17248 if (from >= end)
17249 {
17250 /* Need to free some room before prepending additional
17251 glyphs. */
17252 int move_by = from - end + 1;
17253 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17254 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17255
17256 for ( ; g >= g0; g--)
17257 g[move_by] = *g;
17258 while (from >= end)
17259 *to-- = *from--;
17260 it->glyph_row->used[TEXT_AREA] += move_by;
17261 }
17262 }
17263 }
17264
17265
17266 /* Compute the pixel height and width of IT->glyph_row.
17267
17268 Most of the time, ascent and height of a display line will be equal
17269 to the max_ascent and max_height values of the display iterator
17270 structure. This is not the case if
17271
17272 1. We hit ZV without displaying anything. In this case, max_ascent
17273 and max_height will be zero.
17274
17275 2. We have some glyphs that don't contribute to the line height.
17276 (The glyph row flag contributes_to_line_height_p is for future
17277 pixmap extensions).
17278
17279 The first case is easily covered by using default values because in
17280 these cases, the line height does not really matter, except that it
17281 must not be zero. */
17282
17283 static void
17284 compute_line_metrics (struct it *it)
17285 {
17286 struct glyph_row *row = it->glyph_row;
17287
17288 if (FRAME_WINDOW_P (it->f))
17289 {
17290 int i, min_y, max_y;
17291
17292 /* The line may consist of one space only, that was added to
17293 place the cursor on it. If so, the row's height hasn't been
17294 computed yet. */
17295 if (row->height == 0)
17296 {
17297 if (it->max_ascent + it->max_descent == 0)
17298 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17299 row->ascent = it->max_ascent;
17300 row->height = it->max_ascent + it->max_descent;
17301 row->phys_ascent = it->max_phys_ascent;
17302 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17303 row->extra_line_spacing = it->max_extra_line_spacing;
17304 }
17305
17306 /* Compute the width of this line. */
17307 row->pixel_width = row->x;
17308 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17309 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17310
17311 xassert (row->pixel_width >= 0);
17312 xassert (row->ascent >= 0 && row->height > 0);
17313
17314 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17315 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17316
17317 /* If first line's physical ascent is larger than its logical
17318 ascent, use the physical ascent, and make the row taller.
17319 This makes accented characters fully visible. */
17320 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17321 && row->phys_ascent > row->ascent)
17322 {
17323 row->height += row->phys_ascent - row->ascent;
17324 row->ascent = row->phys_ascent;
17325 }
17326
17327 /* Compute how much of the line is visible. */
17328 row->visible_height = row->height;
17329
17330 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17331 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17332
17333 if (row->y < min_y)
17334 row->visible_height -= min_y - row->y;
17335 if (row->y + row->height > max_y)
17336 row->visible_height -= row->y + row->height - max_y;
17337 }
17338 else
17339 {
17340 row->pixel_width = row->used[TEXT_AREA];
17341 if (row->continued_p)
17342 row->pixel_width -= it->continuation_pixel_width;
17343 else if (row->truncated_on_right_p)
17344 row->pixel_width -= it->truncation_pixel_width;
17345 row->ascent = row->phys_ascent = 0;
17346 row->height = row->phys_height = row->visible_height = 1;
17347 row->extra_line_spacing = 0;
17348 }
17349
17350 /* Compute a hash code for this row. */
17351 {
17352 int area, i;
17353 row->hash = 0;
17354 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17355 for (i = 0; i < row->used[area]; ++i)
17356 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17357 + row->glyphs[area][i].u.val
17358 + row->glyphs[area][i].face_id
17359 + row->glyphs[area][i].padding_p
17360 + (row->glyphs[area][i].type << 2));
17361 }
17362
17363 it->max_ascent = it->max_descent = 0;
17364 it->max_phys_ascent = it->max_phys_descent = 0;
17365 }
17366
17367
17368 /* Append one space to the glyph row of iterator IT if doing a
17369 window-based redisplay. The space has the same face as
17370 IT->face_id. Value is non-zero if a space was added.
17371
17372 This function is called to make sure that there is always one glyph
17373 at the end of a glyph row that the cursor can be set on under
17374 window-systems. (If there weren't such a glyph we would not know
17375 how wide and tall a box cursor should be displayed).
17376
17377 At the same time this space let's a nicely handle clearing to the
17378 end of the line if the row ends in italic text. */
17379
17380 static int
17381 append_space_for_newline (struct it *it, int default_face_p)
17382 {
17383 if (FRAME_WINDOW_P (it->f))
17384 {
17385 int n = it->glyph_row->used[TEXT_AREA];
17386
17387 if (it->glyph_row->glyphs[TEXT_AREA] + n
17388 < it->glyph_row->glyphs[1 + TEXT_AREA])
17389 {
17390 /* Save some values that must not be changed.
17391 Must save IT->c and IT->len because otherwise
17392 ITERATOR_AT_END_P wouldn't work anymore after
17393 append_space_for_newline has been called. */
17394 enum display_element_type saved_what = it->what;
17395 int saved_c = it->c, saved_len = it->len;
17396 int saved_char_to_display = it->char_to_display;
17397 int saved_x = it->current_x;
17398 int saved_face_id = it->face_id;
17399 struct text_pos saved_pos;
17400 Lisp_Object saved_object;
17401 struct face *face;
17402
17403 saved_object = it->object;
17404 saved_pos = it->position;
17405
17406 it->what = IT_CHARACTER;
17407 memset (&it->position, 0, sizeof it->position);
17408 it->object = make_number (0);
17409 it->c = it->char_to_display = ' ';
17410 it->len = 1;
17411
17412 if (default_face_p)
17413 it->face_id = DEFAULT_FACE_ID;
17414 else if (it->face_before_selective_p)
17415 it->face_id = it->saved_face_id;
17416 face = FACE_FROM_ID (it->f, it->face_id);
17417 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17418
17419 PRODUCE_GLYPHS (it);
17420
17421 it->override_ascent = -1;
17422 it->constrain_row_ascent_descent_p = 0;
17423 it->current_x = saved_x;
17424 it->object = saved_object;
17425 it->position = saved_pos;
17426 it->what = saved_what;
17427 it->face_id = saved_face_id;
17428 it->len = saved_len;
17429 it->c = saved_c;
17430 it->char_to_display = saved_char_to_display;
17431 return 1;
17432 }
17433 }
17434
17435 return 0;
17436 }
17437
17438
17439 /* Extend the face of the last glyph in the text area of IT->glyph_row
17440 to the end of the display line. Called from display_line. If the
17441 glyph row is empty, add a space glyph to it so that we know the
17442 face to draw. Set the glyph row flag fill_line_p. If the glyph
17443 row is R2L, prepend a stretch glyph to cover the empty space to the
17444 left of the leftmost glyph. */
17445
17446 static void
17447 extend_face_to_end_of_line (struct it *it)
17448 {
17449 struct face *face;
17450 struct frame *f = it->f;
17451
17452 /* If line is already filled, do nothing. Non window-system frames
17453 get a grace of one more ``pixel'' because their characters are
17454 1-``pixel'' wide, so they hit the equality too early. This grace
17455 is needed only for R2L rows that are not continued, to produce
17456 one extra blank where we could display the cursor. */
17457 if (it->current_x >= it->last_visible_x
17458 + (!FRAME_WINDOW_P (f)
17459 && it->glyph_row->reversed_p
17460 && !it->glyph_row->continued_p))
17461 return;
17462
17463 /* Face extension extends the background and box of IT->face_id
17464 to the end of the line. If the background equals the background
17465 of the frame, we don't have to do anything. */
17466 if (it->face_before_selective_p)
17467 face = FACE_FROM_ID (f, it->saved_face_id);
17468 else
17469 face = FACE_FROM_ID (f, it->face_id);
17470
17471 if (FRAME_WINDOW_P (f)
17472 && it->glyph_row->displays_text_p
17473 && face->box == FACE_NO_BOX
17474 && face->background == FRAME_BACKGROUND_PIXEL (f)
17475 && !face->stipple
17476 && !it->glyph_row->reversed_p)
17477 return;
17478
17479 /* Set the glyph row flag indicating that the face of the last glyph
17480 in the text area has to be drawn to the end of the text area. */
17481 it->glyph_row->fill_line_p = 1;
17482
17483 /* If current character of IT is not ASCII, make sure we have the
17484 ASCII face. This will be automatically undone the next time
17485 get_next_display_element returns a multibyte character. Note
17486 that the character will always be single byte in unibyte
17487 text. */
17488 if (!ASCII_CHAR_P (it->c))
17489 {
17490 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17491 }
17492
17493 if (FRAME_WINDOW_P (f))
17494 {
17495 /* If the row is empty, add a space with the current face of IT,
17496 so that we know which face to draw. */
17497 if (it->glyph_row->used[TEXT_AREA] == 0)
17498 {
17499 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17500 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17501 it->glyph_row->used[TEXT_AREA] = 1;
17502 }
17503 #ifdef HAVE_WINDOW_SYSTEM
17504 if (it->glyph_row->reversed_p)
17505 {
17506 /* Prepend a stretch glyph to the row, such that the
17507 rightmost glyph will be drawn flushed all the way to the
17508 right margin of the window. The stretch glyph that will
17509 occupy the empty space, if any, to the left of the
17510 glyphs. */
17511 struct font *font = face->font ? face->font : FRAME_FONT (f);
17512 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17513 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17514 struct glyph *g;
17515 int row_width, stretch_ascent, stretch_width;
17516 struct text_pos saved_pos;
17517 int saved_face_id, saved_avoid_cursor;
17518
17519 for (row_width = 0, g = row_start; g < row_end; g++)
17520 row_width += g->pixel_width;
17521 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17522 if (stretch_width > 0)
17523 {
17524 stretch_ascent =
17525 (((it->ascent + it->descent)
17526 * FONT_BASE (font)) / FONT_HEIGHT (font));
17527 saved_pos = it->position;
17528 memset (&it->position, 0, sizeof it->position);
17529 saved_avoid_cursor = it->avoid_cursor_p;
17530 it->avoid_cursor_p = 1;
17531 saved_face_id = it->face_id;
17532 /* The last row's stretch glyph should get the default
17533 face, to avoid painting the rest of the window with
17534 the region face, if the region ends at ZV. */
17535 if (it->glyph_row->ends_at_zv_p)
17536 it->face_id = DEFAULT_FACE_ID;
17537 else
17538 it->face_id = face->id;
17539 append_stretch_glyph (it, make_number (0), stretch_width,
17540 it->ascent + it->descent, stretch_ascent);
17541 it->position = saved_pos;
17542 it->avoid_cursor_p = saved_avoid_cursor;
17543 it->face_id = saved_face_id;
17544 }
17545 }
17546 #endif /* HAVE_WINDOW_SYSTEM */
17547 }
17548 else
17549 {
17550 /* Save some values that must not be changed. */
17551 int saved_x = it->current_x;
17552 struct text_pos saved_pos;
17553 Lisp_Object saved_object;
17554 enum display_element_type saved_what = it->what;
17555 int saved_face_id = it->face_id;
17556
17557 saved_object = it->object;
17558 saved_pos = it->position;
17559
17560 it->what = IT_CHARACTER;
17561 memset (&it->position, 0, sizeof it->position);
17562 it->object = make_number (0);
17563 it->c = it->char_to_display = ' ';
17564 it->len = 1;
17565 /* The last row's blank glyphs should get the default face, to
17566 avoid painting the rest of the window with the region face,
17567 if the region ends at ZV. */
17568 if (it->glyph_row->ends_at_zv_p)
17569 it->face_id = DEFAULT_FACE_ID;
17570 else
17571 it->face_id = face->id;
17572
17573 PRODUCE_GLYPHS (it);
17574
17575 while (it->current_x <= it->last_visible_x)
17576 PRODUCE_GLYPHS (it);
17577
17578 /* Don't count these blanks really. It would let us insert a left
17579 truncation glyph below and make us set the cursor on them, maybe. */
17580 it->current_x = saved_x;
17581 it->object = saved_object;
17582 it->position = saved_pos;
17583 it->what = saved_what;
17584 it->face_id = saved_face_id;
17585 }
17586 }
17587
17588
17589 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17590 trailing whitespace. */
17591
17592 static int
17593 trailing_whitespace_p (EMACS_INT charpos)
17594 {
17595 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17596 int c = 0;
17597
17598 while (bytepos < ZV_BYTE
17599 && (c = FETCH_CHAR (bytepos),
17600 c == ' ' || c == '\t'))
17601 ++bytepos;
17602
17603 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17604 {
17605 if (bytepos != PT_BYTE)
17606 return 1;
17607 }
17608 return 0;
17609 }
17610
17611
17612 /* Highlight trailing whitespace, if any, in ROW. */
17613
17614 static void
17615 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17616 {
17617 int used = row->used[TEXT_AREA];
17618
17619 if (used)
17620 {
17621 struct glyph *start = row->glyphs[TEXT_AREA];
17622 struct glyph *glyph = start + used - 1;
17623
17624 if (row->reversed_p)
17625 {
17626 /* Right-to-left rows need to be processed in the opposite
17627 direction, so swap the edge pointers. */
17628 glyph = start;
17629 start = row->glyphs[TEXT_AREA] + used - 1;
17630 }
17631
17632 /* Skip over glyphs inserted to display the cursor at the
17633 end of a line, for extending the face of the last glyph
17634 to the end of the line on terminals, and for truncation
17635 and continuation glyphs. */
17636 if (!row->reversed_p)
17637 {
17638 while (glyph >= start
17639 && glyph->type == CHAR_GLYPH
17640 && INTEGERP (glyph->object))
17641 --glyph;
17642 }
17643 else
17644 {
17645 while (glyph <= start
17646 && glyph->type == CHAR_GLYPH
17647 && INTEGERP (glyph->object))
17648 ++glyph;
17649 }
17650
17651 /* If last glyph is a space or stretch, and it's trailing
17652 whitespace, set the face of all trailing whitespace glyphs in
17653 IT->glyph_row to `trailing-whitespace'. */
17654 if ((row->reversed_p ? glyph <= start : glyph >= start)
17655 && BUFFERP (glyph->object)
17656 && (glyph->type == STRETCH_GLYPH
17657 || (glyph->type == CHAR_GLYPH
17658 && glyph->u.ch == ' '))
17659 && trailing_whitespace_p (glyph->charpos))
17660 {
17661 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17662 if (face_id < 0)
17663 return;
17664
17665 if (!row->reversed_p)
17666 {
17667 while (glyph >= start
17668 && BUFFERP (glyph->object)
17669 && (glyph->type == STRETCH_GLYPH
17670 || (glyph->type == CHAR_GLYPH
17671 && glyph->u.ch == ' ')))
17672 (glyph--)->face_id = face_id;
17673 }
17674 else
17675 {
17676 while (glyph <= start
17677 && BUFFERP (glyph->object)
17678 && (glyph->type == STRETCH_GLYPH
17679 || (glyph->type == CHAR_GLYPH
17680 && glyph->u.ch == ' ')))
17681 (glyph++)->face_id = face_id;
17682 }
17683 }
17684 }
17685 }
17686
17687
17688 /* Value is non-zero if glyph row ROW should be
17689 used to hold the cursor. */
17690
17691 static int
17692 cursor_row_p (struct glyph_row *row)
17693 {
17694 int result = 1;
17695
17696 if (PT == CHARPOS (row->end.pos))
17697 {
17698 /* Suppose the row ends on a string.
17699 Unless the row is continued, that means it ends on a newline
17700 in the string. If it's anything other than a display string
17701 (e.g. a before-string from an overlay), we don't want the
17702 cursor there. (This heuristic seems to give the optimal
17703 behavior for the various types of multi-line strings.) */
17704 if (CHARPOS (row->end.string_pos) >= 0)
17705 {
17706 if (row->continued_p)
17707 result = 1;
17708 else
17709 {
17710 /* Check for `display' property. */
17711 struct glyph *beg = row->glyphs[TEXT_AREA];
17712 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17713 struct glyph *glyph;
17714
17715 result = 0;
17716 for (glyph = end; glyph >= beg; --glyph)
17717 if (STRINGP (glyph->object))
17718 {
17719 Lisp_Object prop
17720 = Fget_char_property (make_number (PT),
17721 Qdisplay, Qnil);
17722 result =
17723 (!NILP (prop)
17724 && display_prop_string_p (prop, glyph->object));
17725 break;
17726 }
17727 }
17728 }
17729 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17730 {
17731 /* If the row ends in middle of a real character,
17732 and the line is continued, we want the cursor here.
17733 That's because CHARPOS (ROW->end.pos) would equal
17734 PT if PT is before the character. */
17735 if (!row->ends_in_ellipsis_p)
17736 result = row->continued_p;
17737 else
17738 /* If the row ends in an ellipsis, then
17739 CHARPOS (ROW->end.pos) will equal point after the
17740 invisible text. We want that position to be displayed
17741 after the ellipsis. */
17742 result = 0;
17743 }
17744 /* If the row ends at ZV, display the cursor at the end of that
17745 row instead of at the start of the row below. */
17746 else if (row->ends_at_zv_p)
17747 result = 1;
17748 else
17749 result = 0;
17750 }
17751
17752 return result;
17753 }
17754
17755 \f
17756
17757 /* Push the display property PROP so that it will be rendered at the
17758 current position in IT. Return 1 if PROP was successfully pushed,
17759 0 otherwise. */
17760
17761 static int
17762 push_display_prop (struct it *it, Lisp_Object prop)
17763 {
17764 xassert (it->method == GET_FROM_BUFFER);
17765
17766 push_it (it, NULL);
17767
17768 if (STRINGP (prop))
17769 {
17770 if (SCHARS (prop) == 0)
17771 {
17772 pop_it (it);
17773 return 0;
17774 }
17775
17776 it->string = prop;
17777 it->multibyte_p = STRING_MULTIBYTE (it->string);
17778 it->current.overlay_string_index = -1;
17779 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17780 it->end_charpos = it->string_nchars = SCHARS (it->string);
17781 it->method = GET_FROM_STRING;
17782 it->stop_charpos = 0;
17783 it->prev_stop = 0;
17784 it->base_level_stop = 0;
17785 it->string_from_display_prop_p = 1;
17786
17787 /* Force paragraph direction to be that of the parent
17788 buffer. */
17789 it->paragraph_embedding = (it->bidi_p ? it->bidi_it.paragraph_dir : L2R);
17790
17791 /* Do we need to reorder this string? */
17792 if (it->multibyte_p)
17793 it->bidi_p = !NILP (BVAR (current_buffer, bidi_display_reordering));
17794 else
17795 it->bidi_p = 0;
17796
17797 /* Set up the bidi iterator for this display string. */
17798 if (it->bidi_p)
17799 {
17800 it->bidi_it.string.lstring = it->string;
17801 it->bidi_it.string.s = NULL;
17802 it->bidi_it.string.schars = it->end_charpos;
17803 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
17804 it->bidi_it.string.from_disp_str = 1;
17805 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
17806 }
17807 }
17808 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17809 {
17810 it->method = GET_FROM_STRETCH;
17811 it->object = prop;
17812 }
17813 #ifdef HAVE_WINDOW_SYSTEM
17814 else if (IMAGEP (prop))
17815 {
17816 it->what = IT_IMAGE;
17817 it->image_id = lookup_image (it->f, prop);
17818 it->method = GET_FROM_IMAGE;
17819 }
17820 #endif /* HAVE_WINDOW_SYSTEM */
17821 else
17822 {
17823 pop_it (it); /* bogus display property, give up */
17824 return 0;
17825 }
17826
17827 return 1;
17828 }
17829
17830 /* Return the character-property PROP at the current position in IT. */
17831
17832 static Lisp_Object
17833 get_it_property (struct it *it, Lisp_Object prop)
17834 {
17835 Lisp_Object position;
17836
17837 if (STRINGP (it->object))
17838 position = make_number (IT_STRING_CHARPOS (*it));
17839 else if (BUFFERP (it->object))
17840 position = make_number (IT_CHARPOS (*it));
17841 else
17842 return Qnil;
17843
17844 return Fget_char_property (position, prop, it->object);
17845 }
17846
17847 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17848
17849 static void
17850 handle_line_prefix (struct it *it)
17851 {
17852 Lisp_Object prefix;
17853
17854 if (it->continuation_lines_width > 0)
17855 {
17856 prefix = get_it_property (it, Qwrap_prefix);
17857 if (NILP (prefix))
17858 prefix = Vwrap_prefix;
17859 }
17860 else
17861 {
17862 prefix = get_it_property (it, Qline_prefix);
17863 if (NILP (prefix))
17864 prefix = Vline_prefix;
17865 }
17866 if (! NILP (prefix) && push_display_prop (it, prefix))
17867 {
17868 /* If the prefix is wider than the window, and we try to wrap
17869 it, it would acquire its own wrap prefix, and so on till the
17870 iterator stack overflows. So, don't wrap the prefix. */
17871 it->line_wrap = TRUNCATE;
17872 it->avoid_cursor_p = 1;
17873 }
17874 }
17875
17876 \f
17877
17878 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17879 only for R2L lines from display_line and display_string, when they
17880 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
17881 the line/string needs to be continued on the next glyph row. */
17882 static void
17883 unproduce_glyphs (struct it *it, int n)
17884 {
17885 struct glyph *glyph, *end;
17886
17887 xassert (it->glyph_row);
17888 xassert (it->glyph_row->reversed_p);
17889 xassert (it->area == TEXT_AREA);
17890 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17891
17892 if (n > it->glyph_row->used[TEXT_AREA])
17893 n = it->glyph_row->used[TEXT_AREA];
17894 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17895 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17896 for ( ; glyph < end; glyph++)
17897 glyph[-n] = *glyph;
17898 }
17899
17900 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17901 and ROW->maxpos. */
17902 static void
17903 find_row_edges (struct it *it, struct glyph_row *row,
17904 EMACS_INT min_pos, EMACS_INT min_bpos,
17905 EMACS_INT max_pos, EMACS_INT max_bpos)
17906 {
17907 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17908 lines' rows is implemented for bidi-reordered rows. */
17909
17910 /* ROW->minpos is the value of min_pos, the minimal buffer position
17911 we have in ROW. */
17912 if (min_pos <= ZV)
17913 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17914 else
17915 /* We didn't find _any_ valid buffer positions in any of the
17916 glyphs, so we must trust the iterator's computed positions. */
17917 row->minpos = row->start.pos;
17918 if (max_pos <= 0)
17919 {
17920 max_pos = CHARPOS (it->current.pos);
17921 max_bpos = BYTEPOS (it->current.pos);
17922 }
17923
17924 /* Here are the various use-cases for ending the row, and the
17925 corresponding values for ROW->maxpos:
17926
17927 Line ends in a newline from buffer eol_pos + 1
17928 Line is continued from buffer max_pos + 1
17929 Line is truncated on right it->current.pos
17930 Line ends in a newline from string max_pos
17931 Line is continued from string max_pos
17932 Line is continued from display vector max_pos
17933 Line is entirely from a string min_pos == max_pos
17934 Line is entirely from a display vector min_pos == max_pos
17935 Line that ends at ZV ZV
17936
17937 If you discover other use-cases, please add them here as
17938 appropriate. */
17939 if (row->ends_at_zv_p)
17940 row->maxpos = it->current.pos;
17941 else if (row->used[TEXT_AREA])
17942 {
17943 if (row->ends_in_newline_from_string_p)
17944 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17945 else if (CHARPOS (it->eol_pos) > 0)
17946 SET_TEXT_POS (row->maxpos,
17947 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17948 else if (row->continued_p)
17949 {
17950 /* If max_pos is different from IT's current position, it
17951 means IT->method does not belong to the display element
17952 at max_pos. However, it also means that the display
17953 element at max_pos was displayed in its entirety on this
17954 line, which is equivalent to saying that the next line
17955 starts at the next buffer position. */
17956 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17957 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17958 else
17959 {
17960 INC_BOTH (max_pos, max_bpos);
17961 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17962 }
17963 }
17964 else if (row->truncated_on_right_p)
17965 /* display_line already called reseat_at_next_visible_line_start,
17966 which puts the iterator at the beginning of the next line, in
17967 the logical order. */
17968 row->maxpos = it->current.pos;
17969 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17970 /* A line that is entirely from a string/image/stretch... */
17971 row->maxpos = row->minpos;
17972 else
17973 abort ();
17974 }
17975 else
17976 row->maxpos = it->current.pos;
17977 }
17978
17979 /* Construct the glyph row IT->glyph_row in the desired matrix of
17980 IT->w from text at the current position of IT. See dispextern.h
17981 for an overview of struct it. Value is non-zero if
17982 IT->glyph_row displays text, as opposed to a line displaying ZV
17983 only. */
17984
17985 static int
17986 display_line (struct it *it)
17987 {
17988 struct glyph_row *row = it->glyph_row;
17989 Lisp_Object overlay_arrow_string;
17990 struct it wrap_it;
17991 int may_wrap = 0, wrap_x IF_LINT (= 0);
17992 int wrap_row_used = -1;
17993 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
17994 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
17995 int wrap_row_extra_line_spacing IF_LINT (= 0);
17996 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
17997 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
17998 int cvpos;
17999 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18000 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18001
18002 /* We always start displaying at hpos zero even if hscrolled. */
18003 xassert (it->hpos == 0 && it->current_x == 0);
18004
18005 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18006 >= it->w->desired_matrix->nrows)
18007 {
18008 it->w->nrows_scale_factor++;
18009 fonts_changed_p = 1;
18010 return 0;
18011 }
18012
18013 /* Is IT->w showing the region? */
18014 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18015
18016 /* Clear the result glyph row and enable it. */
18017 prepare_desired_row (row);
18018
18019 row->y = it->current_y;
18020 row->start = it->start;
18021 row->continuation_lines_width = it->continuation_lines_width;
18022 row->displays_text_p = 1;
18023 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18024 it->starts_in_middle_of_char_p = 0;
18025
18026 /* Arrange the overlays nicely for our purposes. Usually, we call
18027 display_line on only one line at a time, in which case this
18028 can't really hurt too much, or we call it on lines which appear
18029 one after another in the buffer, in which case all calls to
18030 recenter_overlay_lists but the first will be pretty cheap. */
18031 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18032
18033 /* Move over display elements that are not visible because we are
18034 hscrolled. This may stop at an x-position < IT->first_visible_x
18035 if the first glyph is partially visible or if we hit a line end. */
18036 if (it->current_x < it->first_visible_x)
18037 {
18038 this_line_min_pos = row->start.pos;
18039 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18040 MOVE_TO_POS | MOVE_TO_X);
18041 /* Record the smallest positions seen while we moved over
18042 display elements that are not visible. This is needed by
18043 redisplay_internal for optimizing the case where the cursor
18044 stays inside the same line. The rest of this function only
18045 considers positions that are actually displayed, so
18046 RECORD_MAX_MIN_POS will not otherwise record positions that
18047 are hscrolled to the left of the left edge of the window. */
18048 min_pos = CHARPOS (this_line_min_pos);
18049 min_bpos = BYTEPOS (this_line_min_pos);
18050 }
18051 else
18052 {
18053 /* We only do this when not calling `move_it_in_display_line_to'
18054 above, because move_it_in_display_line_to calls
18055 handle_line_prefix itself. */
18056 handle_line_prefix (it);
18057 }
18058
18059 /* Get the initial row height. This is either the height of the
18060 text hscrolled, if there is any, or zero. */
18061 row->ascent = it->max_ascent;
18062 row->height = it->max_ascent + it->max_descent;
18063 row->phys_ascent = it->max_phys_ascent;
18064 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18065 row->extra_line_spacing = it->max_extra_line_spacing;
18066
18067 /* Utility macro to record max and min buffer positions seen until now. */
18068 #define RECORD_MAX_MIN_POS(IT) \
18069 do \
18070 { \
18071 if (IT_CHARPOS (*(IT)) < min_pos) \
18072 { \
18073 min_pos = IT_CHARPOS (*(IT)); \
18074 min_bpos = IT_BYTEPOS (*(IT)); \
18075 } \
18076 if (IT_CHARPOS (*(IT)) > max_pos) \
18077 { \
18078 max_pos = IT_CHARPOS (*(IT)); \
18079 max_bpos = IT_BYTEPOS (*(IT)); \
18080 } \
18081 } \
18082 while (0)
18083
18084 /* Loop generating characters. The loop is left with IT on the next
18085 character to display. */
18086 while (1)
18087 {
18088 int n_glyphs_before, hpos_before, x_before;
18089 int x, nglyphs;
18090 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18091
18092 /* Retrieve the next thing to display. Value is zero if end of
18093 buffer reached. */
18094 if (!get_next_display_element (it))
18095 {
18096 /* Maybe add a space at the end of this line that is used to
18097 display the cursor there under X. Set the charpos of the
18098 first glyph of blank lines not corresponding to any text
18099 to -1. */
18100 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18101 row->exact_window_width_line_p = 1;
18102 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18103 || row->used[TEXT_AREA] == 0)
18104 {
18105 row->glyphs[TEXT_AREA]->charpos = -1;
18106 row->displays_text_p = 0;
18107
18108 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18109 && (!MINI_WINDOW_P (it->w)
18110 || (minibuf_level && EQ (it->window, minibuf_window))))
18111 row->indicate_empty_line_p = 1;
18112 }
18113
18114 it->continuation_lines_width = 0;
18115 row->ends_at_zv_p = 1;
18116 /* A row that displays right-to-left text must always have
18117 its last face extended all the way to the end of line,
18118 even if this row ends in ZV, because we still write to
18119 the screen left to right. */
18120 if (row->reversed_p)
18121 extend_face_to_end_of_line (it);
18122 break;
18123 }
18124
18125 /* Now, get the metrics of what we want to display. This also
18126 generates glyphs in `row' (which is IT->glyph_row). */
18127 n_glyphs_before = row->used[TEXT_AREA];
18128 x = it->current_x;
18129
18130 /* Remember the line height so far in case the next element doesn't
18131 fit on the line. */
18132 if (it->line_wrap != TRUNCATE)
18133 {
18134 ascent = it->max_ascent;
18135 descent = it->max_descent;
18136 phys_ascent = it->max_phys_ascent;
18137 phys_descent = it->max_phys_descent;
18138
18139 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18140 {
18141 if (IT_DISPLAYING_WHITESPACE (it))
18142 may_wrap = 1;
18143 else if (may_wrap)
18144 {
18145 wrap_it = *it;
18146 wrap_x = x;
18147 wrap_row_used = row->used[TEXT_AREA];
18148 wrap_row_ascent = row->ascent;
18149 wrap_row_height = row->height;
18150 wrap_row_phys_ascent = row->phys_ascent;
18151 wrap_row_phys_height = row->phys_height;
18152 wrap_row_extra_line_spacing = row->extra_line_spacing;
18153 wrap_row_min_pos = min_pos;
18154 wrap_row_min_bpos = min_bpos;
18155 wrap_row_max_pos = max_pos;
18156 wrap_row_max_bpos = max_bpos;
18157 may_wrap = 0;
18158 }
18159 }
18160 }
18161
18162 PRODUCE_GLYPHS (it);
18163
18164 /* If this display element was in marginal areas, continue with
18165 the next one. */
18166 if (it->area != TEXT_AREA)
18167 {
18168 row->ascent = max (row->ascent, it->max_ascent);
18169 row->height = max (row->height, it->max_ascent + it->max_descent);
18170 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18171 row->phys_height = max (row->phys_height,
18172 it->max_phys_ascent + it->max_phys_descent);
18173 row->extra_line_spacing = max (row->extra_line_spacing,
18174 it->max_extra_line_spacing);
18175 set_iterator_to_next (it, 1);
18176 continue;
18177 }
18178
18179 /* Does the display element fit on the line? If we truncate
18180 lines, we should draw past the right edge of the window. If
18181 we don't truncate, we want to stop so that we can display the
18182 continuation glyph before the right margin. If lines are
18183 continued, there are two possible strategies for characters
18184 resulting in more than 1 glyph (e.g. tabs): Display as many
18185 glyphs as possible in this line and leave the rest for the
18186 continuation line, or display the whole element in the next
18187 line. Original redisplay did the former, so we do it also. */
18188 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18189 hpos_before = it->hpos;
18190 x_before = x;
18191
18192 if (/* Not a newline. */
18193 nglyphs > 0
18194 /* Glyphs produced fit entirely in the line. */
18195 && it->current_x < it->last_visible_x)
18196 {
18197 it->hpos += nglyphs;
18198 row->ascent = max (row->ascent, it->max_ascent);
18199 row->height = max (row->height, it->max_ascent + it->max_descent);
18200 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18201 row->phys_height = max (row->phys_height,
18202 it->max_phys_ascent + it->max_phys_descent);
18203 row->extra_line_spacing = max (row->extra_line_spacing,
18204 it->max_extra_line_spacing);
18205 if (it->current_x - it->pixel_width < it->first_visible_x)
18206 row->x = x - it->first_visible_x;
18207 /* Record the maximum and minimum buffer positions seen so
18208 far in glyphs that will be displayed by this row. */
18209 if (it->bidi_p)
18210 RECORD_MAX_MIN_POS (it);
18211 }
18212 else
18213 {
18214 int i, new_x;
18215 struct glyph *glyph;
18216
18217 for (i = 0; i < nglyphs; ++i, x = new_x)
18218 {
18219 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18220 new_x = x + glyph->pixel_width;
18221
18222 if (/* Lines are continued. */
18223 it->line_wrap != TRUNCATE
18224 && (/* Glyph doesn't fit on the line. */
18225 new_x > it->last_visible_x
18226 /* Or it fits exactly on a window system frame. */
18227 || (new_x == it->last_visible_x
18228 && FRAME_WINDOW_P (it->f))))
18229 {
18230 /* End of a continued line. */
18231
18232 if (it->hpos == 0
18233 || (new_x == it->last_visible_x
18234 && FRAME_WINDOW_P (it->f)))
18235 {
18236 /* Current glyph is the only one on the line or
18237 fits exactly on the line. We must continue
18238 the line because we can't draw the cursor
18239 after the glyph. */
18240 row->continued_p = 1;
18241 it->current_x = new_x;
18242 it->continuation_lines_width += new_x;
18243 ++it->hpos;
18244 /* Record the maximum and minimum buffer
18245 positions seen so far in glyphs that will be
18246 displayed by this row. */
18247 if (it->bidi_p)
18248 RECORD_MAX_MIN_POS (it);
18249 if (i == nglyphs - 1)
18250 {
18251 /* If line-wrap is on, check if a previous
18252 wrap point was found. */
18253 if (wrap_row_used > 0
18254 /* Even if there is a previous wrap
18255 point, continue the line here as
18256 usual, if (i) the previous character
18257 was a space or tab AND (ii) the
18258 current character is not. */
18259 && (!may_wrap
18260 || IT_DISPLAYING_WHITESPACE (it)))
18261 goto back_to_wrap;
18262
18263 set_iterator_to_next (it, 1);
18264 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18265 {
18266 if (!get_next_display_element (it))
18267 {
18268 row->exact_window_width_line_p = 1;
18269 it->continuation_lines_width = 0;
18270 row->continued_p = 0;
18271 row->ends_at_zv_p = 1;
18272 }
18273 else if (ITERATOR_AT_END_OF_LINE_P (it))
18274 {
18275 row->continued_p = 0;
18276 row->exact_window_width_line_p = 1;
18277 }
18278 }
18279 }
18280 }
18281 else if (CHAR_GLYPH_PADDING_P (*glyph)
18282 && !FRAME_WINDOW_P (it->f))
18283 {
18284 /* A padding glyph that doesn't fit on this line.
18285 This means the whole character doesn't fit
18286 on the line. */
18287 if (row->reversed_p)
18288 unproduce_glyphs (it, row->used[TEXT_AREA]
18289 - n_glyphs_before);
18290 row->used[TEXT_AREA] = n_glyphs_before;
18291
18292 /* Fill the rest of the row with continuation
18293 glyphs like in 20.x. */
18294 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18295 < row->glyphs[1 + TEXT_AREA])
18296 produce_special_glyphs (it, IT_CONTINUATION);
18297
18298 row->continued_p = 1;
18299 it->current_x = x_before;
18300 it->continuation_lines_width += x_before;
18301
18302 /* Restore the height to what it was before the
18303 element not fitting on the line. */
18304 it->max_ascent = ascent;
18305 it->max_descent = descent;
18306 it->max_phys_ascent = phys_ascent;
18307 it->max_phys_descent = phys_descent;
18308 }
18309 else if (wrap_row_used > 0)
18310 {
18311 back_to_wrap:
18312 if (row->reversed_p)
18313 unproduce_glyphs (it,
18314 row->used[TEXT_AREA] - wrap_row_used);
18315 *it = wrap_it;
18316 it->continuation_lines_width += wrap_x;
18317 row->used[TEXT_AREA] = wrap_row_used;
18318 row->ascent = wrap_row_ascent;
18319 row->height = wrap_row_height;
18320 row->phys_ascent = wrap_row_phys_ascent;
18321 row->phys_height = wrap_row_phys_height;
18322 row->extra_line_spacing = wrap_row_extra_line_spacing;
18323 min_pos = wrap_row_min_pos;
18324 min_bpos = wrap_row_min_bpos;
18325 max_pos = wrap_row_max_pos;
18326 max_bpos = wrap_row_max_bpos;
18327 row->continued_p = 1;
18328 row->ends_at_zv_p = 0;
18329 row->exact_window_width_line_p = 0;
18330 it->continuation_lines_width += x;
18331
18332 /* Make sure that a non-default face is extended
18333 up to the right margin of the window. */
18334 extend_face_to_end_of_line (it);
18335 }
18336 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18337 {
18338 /* A TAB that extends past the right edge of the
18339 window. This produces a single glyph on
18340 window system frames. We leave the glyph in
18341 this row and let it fill the row, but don't
18342 consume the TAB. */
18343 it->continuation_lines_width += it->last_visible_x;
18344 row->ends_in_middle_of_char_p = 1;
18345 row->continued_p = 1;
18346 glyph->pixel_width = it->last_visible_x - x;
18347 it->starts_in_middle_of_char_p = 1;
18348 }
18349 else
18350 {
18351 /* Something other than a TAB that draws past
18352 the right edge of the window. Restore
18353 positions to values before the element. */
18354 if (row->reversed_p)
18355 unproduce_glyphs (it, row->used[TEXT_AREA]
18356 - (n_glyphs_before + i));
18357 row->used[TEXT_AREA] = n_glyphs_before + i;
18358
18359 /* Display continuation glyphs. */
18360 if (!FRAME_WINDOW_P (it->f))
18361 produce_special_glyphs (it, IT_CONTINUATION);
18362 row->continued_p = 1;
18363
18364 it->current_x = x_before;
18365 it->continuation_lines_width += x;
18366 extend_face_to_end_of_line (it);
18367
18368 if (nglyphs > 1 && i > 0)
18369 {
18370 row->ends_in_middle_of_char_p = 1;
18371 it->starts_in_middle_of_char_p = 1;
18372 }
18373
18374 /* Restore the height to what it was before the
18375 element not fitting on the line. */
18376 it->max_ascent = ascent;
18377 it->max_descent = descent;
18378 it->max_phys_ascent = phys_ascent;
18379 it->max_phys_descent = phys_descent;
18380 }
18381
18382 break;
18383 }
18384 else if (new_x > it->first_visible_x)
18385 {
18386 /* Increment number of glyphs actually displayed. */
18387 ++it->hpos;
18388
18389 /* Record the maximum and minimum buffer positions
18390 seen so far in glyphs that will be displayed by
18391 this row. */
18392 if (it->bidi_p)
18393 RECORD_MAX_MIN_POS (it);
18394
18395 if (x < it->first_visible_x)
18396 /* Glyph is partially visible, i.e. row starts at
18397 negative X position. */
18398 row->x = x - it->first_visible_x;
18399 }
18400 else
18401 {
18402 /* Glyph is completely off the left margin of the
18403 window. This should not happen because of the
18404 move_it_in_display_line at the start of this
18405 function, unless the text display area of the
18406 window is empty. */
18407 xassert (it->first_visible_x <= it->last_visible_x);
18408 }
18409 }
18410
18411 row->ascent = max (row->ascent, it->max_ascent);
18412 row->height = max (row->height, it->max_ascent + it->max_descent);
18413 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18414 row->phys_height = max (row->phys_height,
18415 it->max_phys_ascent + it->max_phys_descent);
18416 row->extra_line_spacing = max (row->extra_line_spacing,
18417 it->max_extra_line_spacing);
18418
18419 /* End of this display line if row is continued. */
18420 if (row->continued_p || row->ends_at_zv_p)
18421 break;
18422 }
18423
18424 at_end_of_line:
18425 /* Is this a line end? If yes, we're also done, after making
18426 sure that a non-default face is extended up to the right
18427 margin of the window. */
18428 if (ITERATOR_AT_END_OF_LINE_P (it))
18429 {
18430 int used_before = row->used[TEXT_AREA];
18431
18432 row->ends_in_newline_from_string_p = STRINGP (it->object);
18433
18434 /* Add a space at the end of the line that is used to
18435 display the cursor there. */
18436 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18437 append_space_for_newline (it, 0);
18438
18439 /* Extend the face to the end of the line. */
18440 extend_face_to_end_of_line (it);
18441
18442 /* Make sure we have the position. */
18443 if (used_before == 0)
18444 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18445
18446 /* Record the position of the newline, for use in
18447 find_row_edges. */
18448 it->eol_pos = it->current.pos;
18449
18450 /* Consume the line end. This skips over invisible lines. */
18451 set_iterator_to_next (it, 1);
18452 it->continuation_lines_width = 0;
18453 break;
18454 }
18455
18456 /* Proceed with next display element. Note that this skips
18457 over lines invisible because of selective display. */
18458 set_iterator_to_next (it, 1);
18459
18460 /* If we truncate lines, we are done when the last displayed
18461 glyphs reach past the right margin of the window. */
18462 if (it->line_wrap == TRUNCATE
18463 && (FRAME_WINDOW_P (it->f)
18464 ? (it->current_x >= it->last_visible_x)
18465 : (it->current_x > it->last_visible_x)))
18466 {
18467 /* Maybe add truncation glyphs. */
18468 if (!FRAME_WINDOW_P (it->f))
18469 {
18470 int i, n;
18471
18472 if (!row->reversed_p)
18473 {
18474 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18475 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18476 break;
18477 }
18478 else
18479 {
18480 for (i = 0; i < row->used[TEXT_AREA]; i++)
18481 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18482 break;
18483 /* Remove any padding glyphs at the front of ROW, to
18484 make room for the truncation glyphs we will be
18485 adding below. The loop below always inserts at
18486 least one truncation glyph, so also remove the
18487 last glyph added to ROW. */
18488 unproduce_glyphs (it, i + 1);
18489 /* Adjust i for the loop below. */
18490 i = row->used[TEXT_AREA] - (i + 1);
18491 }
18492
18493 for (n = row->used[TEXT_AREA]; i < n; ++i)
18494 {
18495 row->used[TEXT_AREA] = i;
18496 produce_special_glyphs (it, IT_TRUNCATION);
18497 }
18498 }
18499 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18500 {
18501 /* Don't truncate if we can overflow newline into fringe. */
18502 if (!get_next_display_element (it))
18503 {
18504 it->continuation_lines_width = 0;
18505 row->ends_at_zv_p = 1;
18506 row->exact_window_width_line_p = 1;
18507 break;
18508 }
18509 if (ITERATOR_AT_END_OF_LINE_P (it))
18510 {
18511 row->exact_window_width_line_p = 1;
18512 goto at_end_of_line;
18513 }
18514 }
18515
18516 row->truncated_on_right_p = 1;
18517 it->continuation_lines_width = 0;
18518 reseat_at_next_visible_line_start (it, 0);
18519 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18520 it->hpos = hpos_before;
18521 it->current_x = x_before;
18522 break;
18523 }
18524 }
18525
18526 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18527 at the left window margin. */
18528 if (it->first_visible_x
18529 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18530 {
18531 if (!FRAME_WINDOW_P (it->f))
18532 insert_left_trunc_glyphs (it);
18533 row->truncated_on_left_p = 1;
18534 }
18535
18536 /* Remember the position at which this line ends.
18537
18538 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18539 cannot be before the call to find_row_edges below, since that is
18540 where these positions are determined. */
18541 row->end = it->current;
18542 if (!it->bidi_p)
18543 {
18544 row->minpos = row->start.pos;
18545 row->maxpos = row->end.pos;
18546 }
18547 else
18548 {
18549 /* ROW->minpos and ROW->maxpos must be the smallest and
18550 `1 + the largest' buffer positions in ROW. But if ROW was
18551 bidi-reordered, these two positions can be anywhere in the
18552 row, so we must determine them now. */
18553 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18554 }
18555
18556 /* If the start of this line is the overlay arrow-position, then
18557 mark this glyph row as the one containing the overlay arrow.
18558 This is clearly a mess with variable size fonts. It would be
18559 better to let it be displayed like cursors under X. */
18560 if ((row->displays_text_p || !overlay_arrow_seen)
18561 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18562 !NILP (overlay_arrow_string)))
18563 {
18564 /* Overlay arrow in window redisplay is a fringe bitmap. */
18565 if (STRINGP (overlay_arrow_string))
18566 {
18567 struct glyph_row *arrow_row
18568 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18569 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18570 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18571 struct glyph *p = row->glyphs[TEXT_AREA];
18572 struct glyph *p2, *end;
18573
18574 /* Copy the arrow glyphs. */
18575 while (glyph < arrow_end)
18576 *p++ = *glyph++;
18577
18578 /* Throw away padding glyphs. */
18579 p2 = p;
18580 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18581 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18582 ++p2;
18583 if (p2 > p)
18584 {
18585 while (p2 < end)
18586 *p++ = *p2++;
18587 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18588 }
18589 }
18590 else
18591 {
18592 xassert (INTEGERP (overlay_arrow_string));
18593 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18594 }
18595 overlay_arrow_seen = 1;
18596 }
18597
18598 /* Compute pixel dimensions of this line. */
18599 compute_line_metrics (it);
18600
18601 /* Record whether this row ends inside an ellipsis. */
18602 row->ends_in_ellipsis_p
18603 = (it->method == GET_FROM_DISPLAY_VECTOR
18604 && it->ellipsis_p);
18605
18606 /* Save fringe bitmaps in this row. */
18607 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18608 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18609 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18610 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18611
18612 it->left_user_fringe_bitmap = 0;
18613 it->left_user_fringe_face_id = 0;
18614 it->right_user_fringe_bitmap = 0;
18615 it->right_user_fringe_face_id = 0;
18616
18617 /* Maybe set the cursor. */
18618 cvpos = it->w->cursor.vpos;
18619 if ((cvpos < 0
18620 /* In bidi-reordered rows, keep checking for proper cursor
18621 position even if one has been found already, because buffer
18622 positions in such rows change non-linearly with ROW->VPOS,
18623 when a line is continued. One exception: when we are at ZV,
18624 display cursor on the first suitable glyph row, since all
18625 the empty rows after that also have their position set to ZV. */
18626 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18627 lines' rows is implemented for bidi-reordered rows. */
18628 || (it->bidi_p
18629 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18630 && PT >= MATRIX_ROW_START_CHARPOS (row)
18631 && PT <= MATRIX_ROW_END_CHARPOS (row)
18632 && cursor_row_p (row))
18633 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18634
18635 /* Highlight trailing whitespace. */
18636 if (!NILP (Vshow_trailing_whitespace))
18637 highlight_trailing_whitespace (it->f, it->glyph_row);
18638
18639 /* Prepare for the next line. This line starts horizontally at (X
18640 HPOS) = (0 0). Vertical positions are incremented. As a
18641 convenience for the caller, IT->glyph_row is set to the next
18642 row to be used. */
18643 it->current_x = it->hpos = 0;
18644 it->current_y += row->height;
18645 SET_TEXT_POS (it->eol_pos, 0, 0);
18646 ++it->vpos;
18647 ++it->glyph_row;
18648 /* The next row should by default use the same value of the
18649 reversed_p flag as this one. set_iterator_to_next decides when
18650 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18651 the flag accordingly. */
18652 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18653 it->glyph_row->reversed_p = row->reversed_p;
18654 it->start = row->end;
18655 return row->displays_text_p;
18656
18657 #undef RECORD_MAX_MIN_POS
18658 }
18659
18660 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18661 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18662 doc: /* Return paragraph direction at point in BUFFER.
18663 Value is either `left-to-right' or `right-to-left'.
18664 If BUFFER is omitted or nil, it defaults to the current buffer.
18665
18666 Paragraph direction determines how the text in the paragraph is displayed.
18667 In left-to-right paragraphs, text begins at the left margin of the window
18668 and the reading direction is generally left to right. In right-to-left
18669 paragraphs, text begins at the right margin and is read from right to left.
18670
18671 See also `bidi-paragraph-direction'. */)
18672 (Lisp_Object buffer)
18673 {
18674 struct buffer *buf = current_buffer;
18675 struct buffer *old = buf;
18676
18677 if (! NILP (buffer))
18678 {
18679 CHECK_BUFFER (buffer);
18680 buf = XBUFFER (buffer);
18681 }
18682
18683 if (NILP (BVAR (buf, bidi_display_reordering)))
18684 return Qleft_to_right;
18685 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18686 return BVAR (buf, bidi_paragraph_direction);
18687 else
18688 {
18689 /* Determine the direction from buffer text. We could try to
18690 use current_matrix if it is up to date, but this seems fast
18691 enough as it is. */
18692 struct bidi_it itb;
18693 EMACS_INT pos = BUF_PT (buf);
18694 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18695 int c;
18696
18697 set_buffer_temp (buf);
18698 /* bidi_paragraph_init finds the base direction of the paragraph
18699 by searching forward from paragraph start. We need the base
18700 direction of the current or _previous_ paragraph, so we need
18701 to make sure we are within that paragraph. To that end, find
18702 the previous non-empty line. */
18703 if (pos >= ZV && pos > BEGV)
18704 {
18705 pos--;
18706 bytepos = CHAR_TO_BYTE (pos);
18707 }
18708 while ((c = FETCH_BYTE (bytepos)) == '\n'
18709 || c == ' ' || c == '\t' || c == '\f')
18710 {
18711 if (bytepos <= BEGV_BYTE)
18712 break;
18713 bytepos--;
18714 pos--;
18715 }
18716 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18717 bytepos--;
18718 itb.charpos = pos;
18719 itb.bytepos = bytepos;
18720 itb.nchars = -1;
18721 itb.string.s = NULL;
18722 itb.string.lstring = Qnil;
18723 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18724 itb.first_elt = 1;
18725 itb.separator_limit = -1;
18726 itb.paragraph_dir = NEUTRAL_DIR;
18727
18728 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18729 set_buffer_temp (old);
18730 switch (itb.paragraph_dir)
18731 {
18732 case L2R:
18733 return Qleft_to_right;
18734 break;
18735 case R2L:
18736 return Qright_to_left;
18737 break;
18738 default:
18739 abort ();
18740 }
18741 }
18742 }
18743
18744
18745 \f
18746 /***********************************************************************
18747 Menu Bar
18748 ***********************************************************************/
18749
18750 /* Redisplay the menu bar in the frame for window W.
18751
18752 The menu bar of X frames that don't have X toolkit support is
18753 displayed in a special window W->frame->menu_bar_window.
18754
18755 The menu bar of terminal frames is treated specially as far as
18756 glyph matrices are concerned. Menu bar lines are not part of
18757 windows, so the update is done directly on the frame matrix rows
18758 for the menu bar. */
18759
18760 static void
18761 display_menu_bar (struct window *w)
18762 {
18763 struct frame *f = XFRAME (WINDOW_FRAME (w));
18764 struct it it;
18765 Lisp_Object items;
18766 int i;
18767
18768 /* Don't do all this for graphical frames. */
18769 #ifdef HAVE_NTGUI
18770 if (FRAME_W32_P (f))
18771 return;
18772 #endif
18773 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18774 if (FRAME_X_P (f))
18775 return;
18776 #endif
18777
18778 #ifdef HAVE_NS
18779 if (FRAME_NS_P (f))
18780 return;
18781 #endif /* HAVE_NS */
18782
18783 #ifdef USE_X_TOOLKIT
18784 xassert (!FRAME_WINDOW_P (f));
18785 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18786 it.first_visible_x = 0;
18787 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18788 #else /* not USE_X_TOOLKIT */
18789 if (FRAME_WINDOW_P (f))
18790 {
18791 /* Menu bar lines are displayed in the desired matrix of the
18792 dummy window menu_bar_window. */
18793 struct window *menu_w;
18794 xassert (WINDOWP (f->menu_bar_window));
18795 menu_w = XWINDOW (f->menu_bar_window);
18796 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18797 MENU_FACE_ID);
18798 it.first_visible_x = 0;
18799 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18800 }
18801 else
18802 {
18803 /* This is a TTY frame, i.e. character hpos/vpos are used as
18804 pixel x/y. */
18805 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18806 MENU_FACE_ID);
18807 it.first_visible_x = 0;
18808 it.last_visible_x = FRAME_COLS (f);
18809 }
18810 #endif /* not USE_X_TOOLKIT */
18811
18812 /* FIXME: This should be controlled by a user option. See the
18813 comments in redisplay_tool_bar and display_mode_line about
18814 this. */
18815 it.paragraph_embedding = L2R;
18816
18817 if (! mode_line_inverse_video)
18818 /* Force the menu-bar to be displayed in the default face. */
18819 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18820
18821 /* Clear all rows of the menu bar. */
18822 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18823 {
18824 struct glyph_row *row = it.glyph_row + i;
18825 clear_glyph_row (row);
18826 row->enabled_p = 1;
18827 row->full_width_p = 1;
18828 }
18829
18830 /* Display all items of the menu bar. */
18831 items = FRAME_MENU_BAR_ITEMS (it.f);
18832 for (i = 0; i < ASIZE (items); i += 4)
18833 {
18834 Lisp_Object string;
18835
18836 /* Stop at nil string. */
18837 string = AREF (items, i + 1);
18838 if (NILP (string))
18839 break;
18840
18841 /* Remember where item was displayed. */
18842 ASET (items, i + 3, make_number (it.hpos));
18843
18844 /* Display the item, pad with one space. */
18845 if (it.current_x < it.last_visible_x)
18846 display_string (NULL, string, Qnil, 0, 0, &it,
18847 SCHARS (string) + 1, 0, 0, -1);
18848 }
18849
18850 /* Fill out the line with spaces. */
18851 if (it.current_x < it.last_visible_x)
18852 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18853
18854 /* Compute the total height of the lines. */
18855 compute_line_metrics (&it);
18856 }
18857
18858
18859 \f
18860 /***********************************************************************
18861 Mode Line
18862 ***********************************************************************/
18863
18864 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18865 FORCE is non-zero, redisplay mode lines unconditionally.
18866 Otherwise, redisplay only mode lines that are garbaged. Value is
18867 the number of windows whose mode lines were redisplayed. */
18868
18869 static int
18870 redisplay_mode_lines (Lisp_Object window, int force)
18871 {
18872 int nwindows = 0;
18873
18874 while (!NILP (window))
18875 {
18876 struct window *w = XWINDOW (window);
18877
18878 if (WINDOWP (w->hchild))
18879 nwindows += redisplay_mode_lines (w->hchild, force);
18880 else if (WINDOWP (w->vchild))
18881 nwindows += redisplay_mode_lines (w->vchild, force);
18882 else if (force
18883 || FRAME_GARBAGED_P (XFRAME (w->frame))
18884 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18885 {
18886 struct text_pos lpoint;
18887 struct buffer *old = current_buffer;
18888
18889 /* Set the window's buffer for the mode line display. */
18890 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18891 set_buffer_internal_1 (XBUFFER (w->buffer));
18892
18893 /* Point refers normally to the selected window. For any
18894 other window, set up appropriate value. */
18895 if (!EQ (window, selected_window))
18896 {
18897 struct text_pos pt;
18898
18899 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18900 if (CHARPOS (pt) < BEGV)
18901 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18902 else if (CHARPOS (pt) > (ZV - 1))
18903 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18904 else
18905 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18906 }
18907
18908 /* Display mode lines. */
18909 clear_glyph_matrix (w->desired_matrix);
18910 if (display_mode_lines (w))
18911 {
18912 ++nwindows;
18913 w->must_be_updated_p = 1;
18914 }
18915
18916 /* Restore old settings. */
18917 set_buffer_internal_1 (old);
18918 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18919 }
18920
18921 window = w->next;
18922 }
18923
18924 return nwindows;
18925 }
18926
18927
18928 /* Display the mode and/or header line of window W. Value is the
18929 sum number of mode lines and header lines displayed. */
18930
18931 static int
18932 display_mode_lines (struct window *w)
18933 {
18934 Lisp_Object old_selected_window, old_selected_frame;
18935 int n = 0;
18936
18937 old_selected_frame = selected_frame;
18938 selected_frame = w->frame;
18939 old_selected_window = selected_window;
18940 XSETWINDOW (selected_window, w);
18941
18942 /* These will be set while the mode line specs are processed. */
18943 line_number_displayed = 0;
18944 w->column_number_displayed = Qnil;
18945
18946 if (WINDOW_WANTS_MODELINE_P (w))
18947 {
18948 struct window *sel_w = XWINDOW (old_selected_window);
18949
18950 /* Select mode line face based on the real selected window. */
18951 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18952 BVAR (current_buffer, mode_line_format));
18953 ++n;
18954 }
18955
18956 if (WINDOW_WANTS_HEADER_LINE_P (w))
18957 {
18958 display_mode_line (w, HEADER_LINE_FACE_ID,
18959 BVAR (current_buffer, header_line_format));
18960 ++n;
18961 }
18962
18963 selected_frame = old_selected_frame;
18964 selected_window = old_selected_window;
18965 return n;
18966 }
18967
18968
18969 /* Display mode or header line of window W. FACE_ID specifies which
18970 line to display; it is either MODE_LINE_FACE_ID or
18971 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18972 display. Value is the pixel height of the mode/header line
18973 displayed. */
18974
18975 static int
18976 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18977 {
18978 struct it it;
18979 struct face *face;
18980 int count = SPECPDL_INDEX ();
18981
18982 init_iterator (&it, w, -1, -1, NULL, face_id);
18983 /* Don't extend on a previously drawn mode-line.
18984 This may happen if called from pos_visible_p. */
18985 it.glyph_row->enabled_p = 0;
18986 prepare_desired_row (it.glyph_row);
18987
18988 it.glyph_row->mode_line_p = 1;
18989
18990 if (! mode_line_inverse_video)
18991 /* Force the mode-line to be displayed in the default face. */
18992 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18993
18994 /* FIXME: This should be controlled by a user option. But
18995 supporting such an option is not trivial, since the mode line is
18996 made up of many separate strings, most of which are normally
18997 unibyte, and unibyte strings currently don't get reordered for
18998 display. */
18999 it.paragraph_embedding = L2R;
19000
19001 record_unwind_protect (unwind_format_mode_line,
19002 format_mode_line_unwind_data (NULL, Qnil, 0));
19003
19004 mode_line_target = MODE_LINE_DISPLAY;
19005
19006 /* Temporarily make frame's keyboard the current kboard so that
19007 kboard-local variables in the mode_line_format will get the right
19008 values. */
19009 push_kboard (FRAME_KBOARD (it.f));
19010 record_unwind_save_match_data ();
19011 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19012 pop_kboard ();
19013
19014 unbind_to (count, Qnil);
19015
19016 /* Fill up with spaces. */
19017 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19018
19019 compute_line_metrics (&it);
19020 it.glyph_row->full_width_p = 1;
19021 it.glyph_row->continued_p = 0;
19022 it.glyph_row->truncated_on_left_p = 0;
19023 it.glyph_row->truncated_on_right_p = 0;
19024
19025 /* Make a 3D mode-line have a shadow at its right end. */
19026 face = FACE_FROM_ID (it.f, face_id);
19027 extend_face_to_end_of_line (&it);
19028 if (face->box != FACE_NO_BOX)
19029 {
19030 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19031 + it.glyph_row->used[TEXT_AREA] - 1);
19032 last->right_box_line_p = 1;
19033 }
19034
19035 return it.glyph_row->height;
19036 }
19037
19038 /* Move element ELT in LIST to the front of LIST.
19039 Return the updated list. */
19040
19041 static Lisp_Object
19042 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19043 {
19044 register Lisp_Object tail, prev;
19045 register Lisp_Object tem;
19046
19047 tail = list;
19048 prev = Qnil;
19049 while (CONSP (tail))
19050 {
19051 tem = XCAR (tail);
19052
19053 if (EQ (elt, tem))
19054 {
19055 /* Splice out the link TAIL. */
19056 if (NILP (prev))
19057 list = XCDR (tail);
19058 else
19059 Fsetcdr (prev, XCDR (tail));
19060
19061 /* Now make it the first. */
19062 Fsetcdr (tail, list);
19063 return tail;
19064 }
19065 else
19066 prev = tail;
19067 tail = XCDR (tail);
19068 QUIT;
19069 }
19070
19071 /* Not found--return unchanged LIST. */
19072 return list;
19073 }
19074
19075 /* Contribute ELT to the mode line for window IT->w. How it
19076 translates into text depends on its data type.
19077
19078 IT describes the display environment in which we display, as usual.
19079
19080 DEPTH is the depth in recursion. It is used to prevent
19081 infinite recursion here.
19082
19083 FIELD_WIDTH is the number of characters the display of ELT should
19084 occupy in the mode line, and PRECISION is the maximum number of
19085 characters to display from ELT's representation. See
19086 display_string for details.
19087
19088 Returns the hpos of the end of the text generated by ELT.
19089
19090 PROPS is a property list to add to any string we encounter.
19091
19092 If RISKY is nonzero, remove (disregard) any properties in any string
19093 we encounter, and ignore :eval and :propertize.
19094
19095 The global variable `mode_line_target' determines whether the
19096 output is passed to `store_mode_line_noprop',
19097 `store_mode_line_string', or `display_string'. */
19098
19099 static int
19100 display_mode_element (struct it *it, int depth, int field_width, int precision,
19101 Lisp_Object elt, Lisp_Object props, int risky)
19102 {
19103 int n = 0, field, prec;
19104 int literal = 0;
19105
19106 tail_recurse:
19107 if (depth > 100)
19108 elt = build_string ("*too-deep*");
19109
19110 depth++;
19111
19112 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19113 {
19114 case Lisp_String:
19115 {
19116 /* A string: output it and check for %-constructs within it. */
19117 unsigned char c;
19118 EMACS_INT offset = 0;
19119
19120 if (SCHARS (elt) > 0
19121 && (!NILP (props) || risky))
19122 {
19123 Lisp_Object oprops, aelt;
19124 oprops = Ftext_properties_at (make_number (0), elt);
19125
19126 /* If the starting string's properties are not what
19127 we want, translate the string. Also, if the string
19128 is risky, do that anyway. */
19129
19130 if (NILP (Fequal (props, oprops)) || risky)
19131 {
19132 /* If the starting string has properties,
19133 merge the specified ones onto the existing ones. */
19134 if (! NILP (oprops) && !risky)
19135 {
19136 Lisp_Object tem;
19137
19138 oprops = Fcopy_sequence (oprops);
19139 tem = props;
19140 while (CONSP (tem))
19141 {
19142 oprops = Fplist_put (oprops, XCAR (tem),
19143 XCAR (XCDR (tem)));
19144 tem = XCDR (XCDR (tem));
19145 }
19146 props = oprops;
19147 }
19148
19149 aelt = Fassoc (elt, mode_line_proptrans_alist);
19150 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19151 {
19152 /* AELT is what we want. Move it to the front
19153 without consing. */
19154 elt = XCAR (aelt);
19155 mode_line_proptrans_alist
19156 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19157 }
19158 else
19159 {
19160 Lisp_Object tem;
19161
19162 /* If AELT has the wrong props, it is useless.
19163 so get rid of it. */
19164 if (! NILP (aelt))
19165 mode_line_proptrans_alist
19166 = Fdelq (aelt, mode_line_proptrans_alist);
19167
19168 elt = Fcopy_sequence (elt);
19169 Fset_text_properties (make_number (0), Flength (elt),
19170 props, elt);
19171 /* Add this item to mode_line_proptrans_alist. */
19172 mode_line_proptrans_alist
19173 = Fcons (Fcons (elt, props),
19174 mode_line_proptrans_alist);
19175 /* Truncate mode_line_proptrans_alist
19176 to at most 50 elements. */
19177 tem = Fnthcdr (make_number (50),
19178 mode_line_proptrans_alist);
19179 if (! NILP (tem))
19180 XSETCDR (tem, Qnil);
19181 }
19182 }
19183 }
19184
19185 offset = 0;
19186
19187 if (literal)
19188 {
19189 prec = precision - n;
19190 switch (mode_line_target)
19191 {
19192 case MODE_LINE_NOPROP:
19193 case MODE_LINE_TITLE:
19194 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19195 break;
19196 case MODE_LINE_STRING:
19197 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19198 break;
19199 case MODE_LINE_DISPLAY:
19200 n += display_string (NULL, elt, Qnil, 0, 0, it,
19201 0, prec, 0, STRING_MULTIBYTE (elt));
19202 break;
19203 }
19204
19205 break;
19206 }
19207
19208 /* Handle the non-literal case. */
19209
19210 while ((precision <= 0 || n < precision)
19211 && SREF (elt, offset) != 0
19212 && (mode_line_target != MODE_LINE_DISPLAY
19213 || it->current_x < it->last_visible_x))
19214 {
19215 EMACS_INT last_offset = offset;
19216
19217 /* Advance to end of string or next format specifier. */
19218 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19219 ;
19220
19221 if (offset - 1 != last_offset)
19222 {
19223 EMACS_INT nchars, nbytes;
19224
19225 /* Output to end of string or up to '%'. Field width
19226 is length of string. Don't output more than
19227 PRECISION allows us. */
19228 offset--;
19229
19230 prec = c_string_width (SDATA (elt) + last_offset,
19231 offset - last_offset, precision - n,
19232 &nchars, &nbytes);
19233
19234 switch (mode_line_target)
19235 {
19236 case MODE_LINE_NOPROP:
19237 case MODE_LINE_TITLE:
19238 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19239 break;
19240 case MODE_LINE_STRING:
19241 {
19242 EMACS_INT bytepos = last_offset;
19243 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19244 EMACS_INT endpos = (precision <= 0
19245 ? string_byte_to_char (elt, offset)
19246 : charpos + nchars);
19247
19248 n += store_mode_line_string (NULL,
19249 Fsubstring (elt, make_number (charpos),
19250 make_number (endpos)),
19251 0, 0, 0, Qnil);
19252 }
19253 break;
19254 case MODE_LINE_DISPLAY:
19255 {
19256 EMACS_INT bytepos = last_offset;
19257 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19258
19259 if (precision <= 0)
19260 nchars = string_byte_to_char (elt, offset) - charpos;
19261 n += display_string (NULL, elt, Qnil, 0, charpos,
19262 it, 0, nchars, 0,
19263 STRING_MULTIBYTE (elt));
19264 }
19265 break;
19266 }
19267 }
19268 else /* c == '%' */
19269 {
19270 EMACS_INT percent_position = offset;
19271
19272 /* Get the specified minimum width. Zero means
19273 don't pad. */
19274 field = 0;
19275 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19276 field = field * 10 + c - '0';
19277
19278 /* Don't pad beyond the total padding allowed. */
19279 if (field_width - n > 0 && field > field_width - n)
19280 field = field_width - n;
19281
19282 /* Note that either PRECISION <= 0 or N < PRECISION. */
19283 prec = precision - n;
19284
19285 if (c == 'M')
19286 n += display_mode_element (it, depth, field, prec,
19287 Vglobal_mode_string, props,
19288 risky);
19289 else if (c != 0)
19290 {
19291 int multibyte;
19292 EMACS_INT bytepos, charpos;
19293 const char *spec;
19294 Lisp_Object string;
19295
19296 bytepos = percent_position;
19297 charpos = (STRING_MULTIBYTE (elt)
19298 ? string_byte_to_char (elt, bytepos)
19299 : bytepos);
19300 spec = decode_mode_spec (it->w, c, field, &string);
19301 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19302
19303 switch (mode_line_target)
19304 {
19305 case MODE_LINE_NOPROP:
19306 case MODE_LINE_TITLE:
19307 n += store_mode_line_noprop (spec, field, prec);
19308 break;
19309 case MODE_LINE_STRING:
19310 {
19311 int len = strlen (spec);
19312 Lisp_Object tem = make_string (spec, len);
19313 props = Ftext_properties_at (make_number (charpos), elt);
19314 /* Should only keep face property in props */
19315 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19316 }
19317 break;
19318 case MODE_LINE_DISPLAY:
19319 {
19320 int nglyphs_before, nwritten;
19321
19322 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19323 nwritten = display_string (spec, string, elt,
19324 charpos, 0, it,
19325 field, prec, 0,
19326 multibyte);
19327
19328 /* Assign to the glyphs written above the
19329 string where the `%x' came from, position
19330 of the `%'. */
19331 if (nwritten > 0)
19332 {
19333 struct glyph *glyph
19334 = (it->glyph_row->glyphs[TEXT_AREA]
19335 + nglyphs_before);
19336 int i;
19337
19338 for (i = 0; i < nwritten; ++i)
19339 {
19340 glyph[i].object = elt;
19341 glyph[i].charpos = charpos;
19342 }
19343
19344 n += nwritten;
19345 }
19346 }
19347 break;
19348 }
19349 }
19350 else /* c == 0 */
19351 break;
19352 }
19353 }
19354 }
19355 break;
19356
19357 case Lisp_Symbol:
19358 /* A symbol: process the value of the symbol recursively
19359 as if it appeared here directly. Avoid error if symbol void.
19360 Special case: if value of symbol is a string, output the string
19361 literally. */
19362 {
19363 register Lisp_Object tem;
19364
19365 /* If the variable is not marked as risky to set
19366 then its contents are risky to use. */
19367 if (NILP (Fget (elt, Qrisky_local_variable)))
19368 risky = 1;
19369
19370 tem = Fboundp (elt);
19371 if (!NILP (tem))
19372 {
19373 tem = Fsymbol_value (elt);
19374 /* If value is a string, output that string literally:
19375 don't check for % within it. */
19376 if (STRINGP (tem))
19377 literal = 1;
19378
19379 if (!EQ (tem, elt))
19380 {
19381 /* Give up right away for nil or t. */
19382 elt = tem;
19383 goto tail_recurse;
19384 }
19385 }
19386 }
19387 break;
19388
19389 case Lisp_Cons:
19390 {
19391 register Lisp_Object car, tem;
19392
19393 /* A cons cell: five distinct cases.
19394 If first element is :eval or :propertize, do something special.
19395 If first element is a string or a cons, process all the elements
19396 and effectively concatenate them.
19397 If first element is a negative number, truncate displaying cdr to
19398 at most that many characters. If positive, pad (with spaces)
19399 to at least that many characters.
19400 If first element is a symbol, process the cadr or caddr recursively
19401 according to whether the symbol's value is non-nil or nil. */
19402 car = XCAR (elt);
19403 if (EQ (car, QCeval))
19404 {
19405 /* An element of the form (:eval FORM) means evaluate FORM
19406 and use the result as mode line elements. */
19407
19408 if (risky)
19409 break;
19410
19411 if (CONSP (XCDR (elt)))
19412 {
19413 Lisp_Object spec;
19414 spec = safe_eval (XCAR (XCDR (elt)));
19415 n += display_mode_element (it, depth, field_width - n,
19416 precision - n, spec, props,
19417 risky);
19418 }
19419 }
19420 else if (EQ (car, QCpropertize))
19421 {
19422 /* An element of the form (:propertize ELT PROPS...)
19423 means display ELT but applying properties PROPS. */
19424
19425 if (risky)
19426 break;
19427
19428 if (CONSP (XCDR (elt)))
19429 n += display_mode_element (it, depth, field_width - n,
19430 precision - n, XCAR (XCDR (elt)),
19431 XCDR (XCDR (elt)), risky);
19432 }
19433 else if (SYMBOLP (car))
19434 {
19435 tem = Fboundp (car);
19436 elt = XCDR (elt);
19437 if (!CONSP (elt))
19438 goto invalid;
19439 /* elt is now the cdr, and we know it is a cons cell.
19440 Use its car if CAR has a non-nil value. */
19441 if (!NILP (tem))
19442 {
19443 tem = Fsymbol_value (car);
19444 if (!NILP (tem))
19445 {
19446 elt = XCAR (elt);
19447 goto tail_recurse;
19448 }
19449 }
19450 /* Symbol's value is nil (or symbol is unbound)
19451 Get the cddr of the original list
19452 and if possible find the caddr and use that. */
19453 elt = XCDR (elt);
19454 if (NILP (elt))
19455 break;
19456 else if (!CONSP (elt))
19457 goto invalid;
19458 elt = XCAR (elt);
19459 goto tail_recurse;
19460 }
19461 else if (INTEGERP (car))
19462 {
19463 register int lim = XINT (car);
19464 elt = XCDR (elt);
19465 if (lim < 0)
19466 {
19467 /* Negative int means reduce maximum width. */
19468 if (precision <= 0)
19469 precision = -lim;
19470 else
19471 precision = min (precision, -lim);
19472 }
19473 else if (lim > 0)
19474 {
19475 /* Padding specified. Don't let it be more than
19476 current maximum. */
19477 if (precision > 0)
19478 lim = min (precision, lim);
19479
19480 /* If that's more padding than already wanted, queue it.
19481 But don't reduce padding already specified even if
19482 that is beyond the current truncation point. */
19483 field_width = max (lim, field_width);
19484 }
19485 goto tail_recurse;
19486 }
19487 else if (STRINGP (car) || CONSP (car))
19488 {
19489 Lisp_Object halftail = elt;
19490 int len = 0;
19491
19492 while (CONSP (elt)
19493 && (precision <= 0 || n < precision))
19494 {
19495 n += display_mode_element (it, depth,
19496 /* Do padding only after the last
19497 element in the list. */
19498 (! CONSP (XCDR (elt))
19499 ? field_width - n
19500 : 0),
19501 precision - n, XCAR (elt),
19502 props, risky);
19503 elt = XCDR (elt);
19504 len++;
19505 if ((len & 1) == 0)
19506 halftail = XCDR (halftail);
19507 /* Check for cycle. */
19508 if (EQ (halftail, elt))
19509 break;
19510 }
19511 }
19512 }
19513 break;
19514
19515 default:
19516 invalid:
19517 elt = build_string ("*invalid*");
19518 goto tail_recurse;
19519 }
19520
19521 /* Pad to FIELD_WIDTH. */
19522 if (field_width > 0 && n < field_width)
19523 {
19524 switch (mode_line_target)
19525 {
19526 case MODE_LINE_NOPROP:
19527 case MODE_LINE_TITLE:
19528 n += store_mode_line_noprop ("", field_width - n, 0);
19529 break;
19530 case MODE_LINE_STRING:
19531 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19532 break;
19533 case MODE_LINE_DISPLAY:
19534 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19535 0, 0, 0);
19536 break;
19537 }
19538 }
19539
19540 return n;
19541 }
19542
19543 /* Store a mode-line string element in mode_line_string_list.
19544
19545 If STRING is non-null, display that C string. Otherwise, the Lisp
19546 string LISP_STRING is displayed.
19547
19548 FIELD_WIDTH is the minimum number of output glyphs to produce.
19549 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19550 with spaces. FIELD_WIDTH <= 0 means don't pad.
19551
19552 PRECISION is the maximum number of characters to output from
19553 STRING. PRECISION <= 0 means don't truncate the string.
19554
19555 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19556 properties to the string.
19557
19558 PROPS are the properties to add to the string.
19559 The mode_line_string_face face property is always added to the string.
19560 */
19561
19562 static int
19563 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19564 int field_width, int precision, Lisp_Object props)
19565 {
19566 EMACS_INT len;
19567 int n = 0;
19568
19569 if (string != NULL)
19570 {
19571 len = strlen (string);
19572 if (precision > 0 && len > precision)
19573 len = precision;
19574 lisp_string = make_string (string, len);
19575 if (NILP (props))
19576 props = mode_line_string_face_prop;
19577 else if (!NILP (mode_line_string_face))
19578 {
19579 Lisp_Object face = Fplist_get (props, Qface);
19580 props = Fcopy_sequence (props);
19581 if (NILP (face))
19582 face = mode_line_string_face;
19583 else
19584 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19585 props = Fplist_put (props, Qface, face);
19586 }
19587 Fadd_text_properties (make_number (0), make_number (len),
19588 props, lisp_string);
19589 }
19590 else
19591 {
19592 len = XFASTINT (Flength (lisp_string));
19593 if (precision > 0 && len > precision)
19594 {
19595 len = precision;
19596 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19597 precision = -1;
19598 }
19599 if (!NILP (mode_line_string_face))
19600 {
19601 Lisp_Object face;
19602 if (NILP (props))
19603 props = Ftext_properties_at (make_number (0), lisp_string);
19604 face = Fplist_get (props, Qface);
19605 if (NILP (face))
19606 face = mode_line_string_face;
19607 else
19608 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19609 props = Fcons (Qface, Fcons (face, Qnil));
19610 if (copy_string)
19611 lisp_string = Fcopy_sequence (lisp_string);
19612 }
19613 if (!NILP (props))
19614 Fadd_text_properties (make_number (0), make_number (len),
19615 props, lisp_string);
19616 }
19617
19618 if (len > 0)
19619 {
19620 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19621 n += len;
19622 }
19623
19624 if (field_width > len)
19625 {
19626 field_width -= len;
19627 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19628 if (!NILP (props))
19629 Fadd_text_properties (make_number (0), make_number (field_width),
19630 props, lisp_string);
19631 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19632 n += field_width;
19633 }
19634
19635 return n;
19636 }
19637
19638
19639 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19640 1, 4, 0,
19641 doc: /* Format a string out of a mode line format specification.
19642 First arg FORMAT specifies the mode line format (see `mode-line-format'
19643 for details) to use.
19644
19645 By default, the format is evaluated for the currently selected window.
19646
19647 Optional second arg FACE specifies the face property to put on all
19648 characters for which no face is specified. The value nil means the
19649 default face. The value t means whatever face the window's mode line
19650 currently uses (either `mode-line' or `mode-line-inactive',
19651 depending on whether the window is the selected window or not).
19652 An integer value means the value string has no text
19653 properties.
19654
19655 Optional third and fourth args WINDOW and BUFFER specify the window
19656 and buffer to use as the context for the formatting (defaults
19657 are the selected window and the WINDOW's buffer). */)
19658 (Lisp_Object format, Lisp_Object face,
19659 Lisp_Object window, Lisp_Object buffer)
19660 {
19661 struct it it;
19662 int len;
19663 struct window *w;
19664 struct buffer *old_buffer = NULL;
19665 int face_id;
19666 int no_props = INTEGERP (face);
19667 int count = SPECPDL_INDEX ();
19668 Lisp_Object str;
19669 int string_start = 0;
19670
19671 if (NILP (window))
19672 window = selected_window;
19673 CHECK_WINDOW (window);
19674 w = XWINDOW (window);
19675
19676 if (NILP (buffer))
19677 buffer = w->buffer;
19678 CHECK_BUFFER (buffer);
19679
19680 /* Make formatting the modeline a non-op when noninteractive, otherwise
19681 there will be problems later caused by a partially initialized frame. */
19682 if (NILP (format) || noninteractive)
19683 return empty_unibyte_string;
19684
19685 if (no_props)
19686 face = Qnil;
19687
19688 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19689 : EQ (face, Qt) ? (EQ (window, selected_window)
19690 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19691 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19692 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19693 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19694 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19695 : DEFAULT_FACE_ID;
19696
19697 if (XBUFFER (buffer) != current_buffer)
19698 old_buffer = current_buffer;
19699
19700 /* Save things including mode_line_proptrans_alist,
19701 and set that to nil so that we don't alter the outer value. */
19702 record_unwind_protect (unwind_format_mode_line,
19703 format_mode_line_unwind_data
19704 (old_buffer, selected_window, 1));
19705 mode_line_proptrans_alist = Qnil;
19706
19707 Fselect_window (window, Qt);
19708 if (old_buffer)
19709 set_buffer_internal_1 (XBUFFER (buffer));
19710
19711 init_iterator (&it, w, -1, -1, NULL, face_id);
19712
19713 if (no_props)
19714 {
19715 mode_line_target = MODE_LINE_NOPROP;
19716 mode_line_string_face_prop = Qnil;
19717 mode_line_string_list = Qnil;
19718 string_start = MODE_LINE_NOPROP_LEN (0);
19719 }
19720 else
19721 {
19722 mode_line_target = MODE_LINE_STRING;
19723 mode_line_string_list = Qnil;
19724 mode_line_string_face = face;
19725 mode_line_string_face_prop
19726 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19727 }
19728
19729 push_kboard (FRAME_KBOARD (it.f));
19730 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19731 pop_kboard ();
19732
19733 if (no_props)
19734 {
19735 len = MODE_LINE_NOPROP_LEN (string_start);
19736 str = make_string (mode_line_noprop_buf + string_start, len);
19737 }
19738 else
19739 {
19740 mode_line_string_list = Fnreverse (mode_line_string_list);
19741 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19742 empty_unibyte_string);
19743 }
19744
19745 unbind_to (count, Qnil);
19746 return str;
19747 }
19748
19749 /* Write a null-terminated, right justified decimal representation of
19750 the positive integer D to BUF using a minimal field width WIDTH. */
19751
19752 static void
19753 pint2str (register char *buf, register int width, register EMACS_INT d)
19754 {
19755 register char *p = buf;
19756
19757 if (d <= 0)
19758 *p++ = '0';
19759 else
19760 {
19761 while (d > 0)
19762 {
19763 *p++ = d % 10 + '0';
19764 d /= 10;
19765 }
19766 }
19767
19768 for (width -= (int) (p - buf); width > 0; --width)
19769 *p++ = ' ';
19770 *p-- = '\0';
19771 while (p > buf)
19772 {
19773 d = *buf;
19774 *buf++ = *p;
19775 *p-- = d;
19776 }
19777 }
19778
19779 /* Write a null-terminated, right justified decimal and "human
19780 readable" representation of the nonnegative integer D to BUF using
19781 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19782
19783 static const char power_letter[] =
19784 {
19785 0, /* no letter */
19786 'k', /* kilo */
19787 'M', /* mega */
19788 'G', /* giga */
19789 'T', /* tera */
19790 'P', /* peta */
19791 'E', /* exa */
19792 'Z', /* zetta */
19793 'Y' /* yotta */
19794 };
19795
19796 static void
19797 pint2hrstr (char *buf, int width, EMACS_INT d)
19798 {
19799 /* We aim to represent the nonnegative integer D as
19800 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19801 EMACS_INT quotient = d;
19802 int remainder = 0;
19803 /* -1 means: do not use TENTHS. */
19804 int tenths = -1;
19805 int exponent = 0;
19806
19807 /* Length of QUOTIENT.TENTHS as a string. */
19808 int length;
19809
19810 char * psuffix;
19811 char * p;
19812
19813 if (1000 <= quotient)
19814 {
19815 /* Scale to the appropriate EXPONENT. */
19816 do
19817 {
19818 remainder = quotient % 1000;
19819 quotient /= 1000;
19820 exponent++;
19821 }
19822 while (1000 <= quotient);
19823
19824 /* Round to nearest and decide whether to use TENTHS or not. */
19825 if (quotient <= 9)
19826 {
19827 tenths = remainder / 100;
19828 if (50 <= remainder % 100)
19829 {
19830 if (tenths < 9)
19831 tenths++;
19832 else
19833 {
19834 quotient++;
19835 if (quotient == 10)
19836 tenths = -1;
19837 else
19838 tenths = 0;
19839 }
19840 }
19841 }
19842 else
19843 if (500 <= remainder)
19844 {
19845 if (quotient < 999)
19846 quotient++;
19847 else
19848 {
19849 quotient = 1;
19850 exponent++;
19851 tenths = 0;
19852 }
19853 }
19854 }
19855
19856 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19857 if (tenths == -1 && quotient <= 99)
19858 if (quotient <= 9)
19859 length = 1;
19860 else
19861 length = 2;
19862 else
19863 length = 3;
19864 p = psuffix = buf + max (width, length);
19865
19866 /* Print EXPONENT. */
19867 *psuffix++ = power_letter[exponent];
19868 *psuffix = '\0';
19869
19870 /* Print TENTHS. */
19871 if (tenths >= 0)
19872 {
19873 *--p = '0' + tenths;
19874 *--p = '.';
19875 }
19876
19877 /* Print QUOTIENT. */
19878 do
19879 {
19880 int digit = quotient % 10;
19881 *--p = '0' + digit;
19882 }
19883 while ((quotient /= 10) != 0);
19884
19885 /* Print leading spaces. */
19886 while (buf < p)
19887 *--p = ' ';
19888 }
19889
19890 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19891 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19892 type of CODING_SYSTEM. Return updated pointer into BUF. */
19893
19894 static unsigned char invalid_eol_type[] = "(*invalid*)";
19895
19896 static char *
19897 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19898 {
19899 Lisp_Object val;
19900 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19901 const unsigned char *eol_str;
19902 int eol_str_len;
19903 /* The EOL conversion we are using. */
19904 Lisp_Object eoltype;
19905
19906 val = CODING_SYSTEM_SPEC (coding_system);
19907 eoltype = Qnil;
19908
19909 if (!VECTORP (val)) /* Not yet decided. */
19910 {
19911 if (multibyte)
19912 *buf++ = '-';
19913 if (eol_flag)
19914 eoltype = eol_mnemonic_undecided;
19915 /* Don't mention EOL conversion if it isn't decided. */
19916 }
19917 else
19918 {
19919 Lisp_Object attrs;
19920 Lisp_Object eolvalue;
19921
19922 attrs = AREF (val, 0);
19923 eolvalue = AREF (val, 2);
19924
19925 if (multibyte)
19926 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19927
19928 if (eol_flag)
19929 {
19930 /* The EOL conversion that is normal on this system. */
19931
19932 if (NILP (eolvalue)) /* Not yet decided. */
19933 eoltype = eol_mnemonic_undecided;
19934 else if (VECTORP (eolvalue)) /* Not yet decided. */
19935 eoltype = eol_mnemonic_undecided;
19936 else /* eolvalue is Qunix, Qdos, or Qmac. */
19937 eoltype = (EQ (eolvalue, Qunix)
19938 ? eol_mnemonic_unix
19939 : (EQ (eolvalue, Qdos) == 1
19940 ? eol_mnemonic_dos : eol_mnemonic_mac));
19941 }
19942 }
19943
19944 if (eol_flag)
19945 {
19946 /* Mention the EOL conversion if it is not the usual one. */
19947 if (STRINGP (eoltype))
19948 {
19949 eol_str = SDATA (eoltype);
19950 eol_str_len = SBYTES (eoltype);
19951 }
19952 else if (CHARACTERP (eoltype))
19953 {
19954 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19955 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19956 eol_str = tmp;
19957 }
19958 else
19959 {
19960 eol_str = invalid_eol_type;
19961 eol_str_len = sizeof (invalid_eol_type) - 1;
19962 }
19963 memcpy (buf, eol_str, eol_str_len);
19964 buf += eol_str_len;
19965 }
19966
19967 return buf;
19968 }
19969
19970 /* Return a string for the output of a mode line %-spec for window W,
19971 generated by character C. FIELD_WIDTH > 0 means pad the string
19972 returned with spaces to that value. Return a Lisp string in
19973 *STRING if the resulting string is taken from that Lisp string.
19974
19975 Note we operate on the current buffer for most purposes,
19976 the exception being w->base_line_pos. */
19977
19978 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19979
19980 static const char *
19981 decode_mode_spec (struct window *w, register int c, int field_width,
19982 Lisp_Object *string)
19983 {
19984 Lisp_Object obj;
19985 struct frame *f = XFRAME (WINDOW_FRAME (w));
19986 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19987 struct buffer *b = current_buffer;
19988
19989 obj = Qnil;
19990 *string = Qnil;
19991
19992 switch (c)
19993 {
19994 case '*':
19995 if (!NILP (BVAR (b, read_only)))
19996 return "%";
19997 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19998 return "*";
19999 return "-";
20000
20001 case '+':
20002 /* This differs from %* only for a modified read-only buffer. */
20003 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20004 return "*";
20005 if (!NILP (BVAR (b, read_only)))
20006 return "%";
20007 return "-";
20008
20009 case '&':
20010 /* This differs from %* in ignoring read-only-ness. */
20011 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20012 return "*";
20013 return "-";
20014
20015 case '%':
20016 return "%";
20017
20018 case '[':
20019 {
20020 int i;
20021 char *p;
20022
20023 if (command_loop_level > 5)
20024 return "[[[... ";
20025 p = decode_mode_spec_buf;
20026 for (i = 0; i < command_loop_level; i++)
20027 *p++ = '[';
20028 *p = 0;
20029 return decode_mode_spec_buf;
20030 }
20031
20032 case ']':
20033 {
20034 int i;
20035 char *p;
20036
20037 if (command_loop_level > 5)
20038 return " ...]]]";
20039 p = decode_mode_spec_buf;
20040 for (i = 0; i < command_loop_level; i++)
20041 *p++ = ']';
20042 *p = 0;
20043 return decode_mode_spec_buf;
20044 }
20045
20046 case '-':
20047 {
20048 register int i;
20049
20050 /* Let lots_of_dashes be a string of infinite length. */
20051 if (mode_line_target == MODE_LINE_NOPROP ||
20052 mode_line_target == MODE_LINE_STRING)
20053 return "--";
20054 if (field_width <= 0
20055 || field_width > sizeof (lots_of_dashes))
20056 {
20057 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20058 decode_mode_spec_buf[i] = '-';
20059 decode_mode_spec_buf[i] = '\0';
20060 return decode_mode_spec_buf;
20061 }
20062 else
20063 return lots_of_dashes;
20064 }
20065
20066 case 'b':
20067 obj = BVAR (b, name);
20068 break;
20069
20070 case 'c':
20071 /* %c and %l are ignored in `frame-title-format'.
20072 (In redisplay_internal, the frame title is drawn _before_ the
20073 windows are updated, so the stuff which depends on actual
20074 window contents (such as %l) may fail to render properly, or
20075 even crash emacs.) */
20076 if (mode_line_target == MODE_LINE_TITLE)
20077 return "";
20078 else
20079 {
20080 EMACS_INT col = current_column ();
20081 w->column_number_displayed = make_number (col);
20082 pint2str (decode_mode_spec_buf, field_width, col);
20083 return decode_mode_spec_buf;
20084 }
20085
20086 case 'e':
20087 #ifndef SYSTEM_MALLOC
20088 {
20089 if (NILP (Vmemory_full))
20090 return "";
20091 else
20092 return "!MEM FULL! ";
20093 }
20094 #else
20095 return "";
20096 #endif
20097
20098 case 'F':
20099 /* %F displays the frame name. */
20100 if (!NILP (f->title))
20101 return SSDATA (f->title);
20102 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20103 return SSDATA (f->name);
20104 return "Emacs";
20105
20106 case 'f':
20107 obj = BVAR (b, filename);
20108 break;
20109
20110 case 'i':
20111 {
20112 EMACS_INT size = ZV - BEGV;
20113 pint2str (decode_mode_spec_buf, field_width, size);
20114 return decode_mode_spec_buf;
20115 }
20116
20117 case 'I':
20118 {
20119 EMACS_INT size = ZV - BEGV;
20120 pint2hrstr (decode_mode_spec_buf, field_width, size);
20121 return decode_mode_spec_buf;
20122 }
20123
20124 case 'l':
20125 {
20126 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20127 EMACS_INT topline, nlines, height;
20128 EMACS_INT junk;
20129
20130 /* %c and %l are ignored in `frame-title-format'. */
20131 if (mode_line_target == MODE_LINE_TITLE)
20132 return "";
20133
20134 startpos = XMARKER (w->start)->charpos;
20135 startpos_byte = marker_byte_position (w->start);
20136 height = WINDOW_TOTAL_LINES (w);
20137
20138 /* If we decided that this buffer isn't suitable for line numbers,
20139 don't forget that too fast. */
20140 if (EQ (w->base_line_pos, w->buffer))
20141 goto no_value;
20142 /* But do forget it, if the window shows a different buffer now. */
20143 else if (BUFFERP (w->base_line_pos))
20144 w->base_line_pos = Qnil;
20145
20146 /* If the buffer is very big, don't waste time. */
20147 if (INTEGERP (Vline_number_display_limit)
20148 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20149 {
20150 w->base_line_pos = Qnil;
20151 w->base_line_number = Qnil;
20152 goto no_value;
20153 }
20154
20155 if (INTEGERP (w->base_line_number)
20156 && INTEGERP (w->base_line_pos)
20157 && XFASTINT (w->base_line_pos) <= startpos)
20158 {
20159 line = XFASTINT (w->base_line_number);
20160 linepos = XFASTINT (w->base_line_pos);
20161 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20162 }
20163 else
20164 {
20165 line = 1;
20166 linepos = BUF_BEGV (b);
20167 linepos_byte = BUF_BEGV_BYTE (b);
20168 }
20169
20170 /* Count lines from base line to window start position. */
20171 nlines = display_count_lines (linepos_byte,
20172 startpos_byte,
20173 startpos, &junk);
20174
20175 topline = nlines + line;
20176
20177 /* Determine a new base line, if the old one is too close
20178 or too far away, or if we did not have one.
20179 "Too close" means it's plausible a scroll-down would
20180 go back past it. */
20181 if (startpos == BUF_BEGV (b))
20182 {
20183 w->base_line_number = make_number (topline);
20184 w->base_line_pos = make_number (BUF_BEGV (b));
20185 }
20186 else if (nlines < height + 25 || nlines > height * 3 + 50
20187 || linepos == BUF_BEGV (b))
20188 {
20189 EMACS_INT limit = BUF_BEGV (b);
20190 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20191 EMACS_INT position;
20192 EMACS_INT distance =
20193 (height * 2 + 30) * line_number_display_limit_width;
20194
20195 if (startpos - distance > limit)
20196 {
20197 limit = startpos - distance;
20198 limit_byte = CHAR_TO_BYTE (limit);
20199 }
20200
20201 nlines = display_count_lines (startpos_byte,
20202 limit_byte,
20203 - (height * 2 + 30),
20204 &position);
20205 /* If we couldn't find the lines we wanted within
20206 line_number_display_limit_width chars per line,
20207 give up on line numbers for this window. */
20208 if (position == limit_byte && limit == startpos - distance)
20209 {
20210 w->base_line_pos = w->buffer;
20211 w->base_line_number = Qnil;
20212 goto no_value;
20213 }
20214
20215 w->base_line_number = make_number (topline - nlines);
20216 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20217 }
20218
20219 /* Now count lines from the start pos to point. */
20220 nlines = display_count_lines (startpos_byte,
20221 PT_BYTE, PT, &junk);
20222
20223 /* Record that we did display the line number. */
20224 line_number_displayed = 1;
20225
20226 /* Make the string to show. */
20227 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20228 return decode_mode_spec_buf;
20229 no_value:
20230 {
20231 char* p = decode_mode_spec_buf;
20232 int pad = field_width - 2;
20233 while (pad-- > 0)
20234 *p++ = ' ';
20235 *p++ = '?';
20236 *p++ = '?';
20237 *p = '\0';
20238 return decode_mode_spec_buf;
20239 }
20240 }
20241 break;
20242
20243 case 'm':
20244 obj = BVAR (b, mode_name);
20245 break;
20246
20247 case 'n':
20248 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20249 return " Narrow";
20250 break;
20251
20252 case 'p':
20253 {
20254 EMACS_INT pos = marker_position (w->start);
20255 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20256
20257 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20258 {
20259 if (pos <= BUF_BEGV (b))
20260 return "All";
20261 else
20262 return "Bottom";
20263 }
20264 else if (pos <= BUF_BEGV (b))
20265 return "Top";
20266 else
20267 {
20268 if (total > 1000000)
20269 /* Do it differently for a large value, to avoid overflow. */
20270 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20271 else
20272 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20273 /* We can't normally display a 3-digit number,
20274 so get us a 2-digit number that is close. */
20275 if (total == 100)
20276 total = 99;
20277 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20278 return decode_mode_spec_buf;
20279 }
20280 }
20281
20282 /* Display percentage of size above the bottom of the screen. */
20283 case 'P':
20284 {
20285 EMACS_INT toppos = marker_position (w->start);
20286 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20287 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20288
20289 if (botpos >= BUF_ZV (b))
20290 {
20291 if (toppos <= BUF_BEGV (b))
20292 return "All";
20293 else
20294 return "Bottom";
20295 }
20296 else
20297 {
20298 if (total > 1000000)
20299 /* Do it differently for a large value, to avoid overflow. */
20300 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20301 else
20302 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20303 /* We can't normally display a 3-digit number,
20304 so get us a 2-digit number that is close. */
20305 if (total == 100)
20306 total = 99;
20307 if (toppos <= BUF_BEGV (b))
20308 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20309 else
20310 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20311 return decode_mode_spec_buf;
20312 }
20313 }
20314
20315 case 's':
20316 /* status of process */
20317 obj = Fget_buffer_process (Fcurrent_buffer ());
20318 if (NILP (obj))
20319 return "no process";
20320 #ifndef MSDOS
20321 obj = Fsymbol_name (Fprocess_status (obj));
20322 #endif
20323 break;
20324
20325 case '@':
20326 {
20327 int count = inhibit_garbage_collection ();
20328 Lisp_Object val = call1 (intern ("file-remote-p"),
20329 BVAR (current_buffer, directory));
20330 unbind_to (count, Qnil);
20331
20332 if (NILP (val))
20333 return "-";
20334 else
20335 return "@";
20336 }
20337
20338 case 't': /* indicate TEXT or BINARY */
20339 return "T";
20340
20341 case 'z':
20342 /* coding-system (not including end-of-line format) */
20343 case 'Z':
20344 /* coding-system (including end-of-line type) */
20345 {
20346 int eol_flag = (c == 'Z');
20347 char *p = decode_mode_spec_buf;
20348
20349 if (! FRAME_WINDOW_P (f))
20350 {
20351 /* No need to mention EOL here--the terminal never needs
20352 to do EOL conversion. */
20353 p = decode_mode_spec_coding (CODING_ID_NAME
20354 (FRAME_KEYBOARD_CODING (f)->id),
20355 p, 0);
20356 p = decode_mode_spec_coding (CODING_ID_NAME
20357 (FRAME_TERMINAL_CODING (f)->id),
20358 p, 0);
20359 }
20360 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20361 p, eol_flag);
20362
20363 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20364 #ifdef subprocesses
20365 obj = Fget_buffer_process (Fcurrent_buffer ());
20366 if (PROCESSP (obj))
20367 {
20368 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20369 p, eol_flag);
20370 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20371 p, eol_flag);
20372 }
20373 #endif /* subprocesses */
20374 #endif /* 0 */
20375 *p = 0;
20376 return decode_mode_spec_buf;
20377 }
20378 }
20379
20380 if (STRINGP (obj))
20381 {
20382 *string = obj;
20383 return SSDATA (obj);
20384 }
20385 else
20386 return "";
20387 }
20388
20389
20390 /* Count up to COUNT lines starting from START_BYTE.
20391 But don't go beyond LIMIT_BYTE.
20392 Return the number of lines thus found (always nonnegative).
20393
20394 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20395
20396 static EMACS_INT
20397 display_count_lines (EMACS_INT start_byte,
20398 EMACS_INT limit_byte, EMACS_INT count,
20399 EMACS_INT *byte_pos_ptr)
20400 {
20401 register unsigned char *cursor;
20402 unsigned char *base;
20403
20404 register EMACS_INT ceiling;
20405 register unsigned char *ceiling_addr;
20406 EMACS_INT orig_count = count;
20407
20408 /* If we are not in selective display mode,
20409 check only for newlines. */
20410 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20411 && !INTEGERP (BVAR (current_buffer, selective_display)));
20412
20413 if (count > 0)
20414 {
20415 while (start_byte < limit_byte)
20416 {
20417 ceiling = BUFFER_CEILING_OF (start_byte);
20418 ceiling = min (limit_byte - 1, ceiling);
20419 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20420 base = (cursor = BYTE_POS_ADDR (start_byte));
20421 while (1)
20422 {
20423 if (selective_display)
20424 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20425 ;
20426 else
20427 while (*cursor != '\n' && ++cursor != ceiling_addr)
20428 ;
20429
20430 if (cursor != ceiling_addr)
20431 {
20432 if (--count == 0)
20433 {
20434 start_byte += cursor - base + 1;
20435 *byte_pos_ptr = start_byte;
20436 return orig_count;
20437 }
20438 else
20439 if (++cursor == ceiling_addr)
20440 break;
20441 }
20442 else
20443 break;
20444 }
20445 start_byte += cursor - base;
20446 }
20447 }
20448 else
20449 {
20450 while (start_byte > limit_byte)
20451 {
20452 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20453 ceiling = max (limit_byte, ceiling);
20454 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20455 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20456 while (1)
20457 {
20458 if (selective_display)
20459 while (--cursor != ceiling_addr
20460 && *cursor != '\n' && *cursor != 015)
20461 ;
20462 else
20463 while (--cursor != ceiling_addr && *cursor != '\n')
20464 ;
20465
20466 if (cursor != ceiling_addr)
20467 {
20468 if (++count == 0)
20469 {
20470 start_byte += cursor - base + 1;
20471 *byte_pos_ptr = start_byte;
20472 /* When scanning backwards, we should
20473 not count the newline posterior to which we stop. */
20474 return - orig_count - 1;
20475 }
20476 }
20477 else
20478 break;
20479 }
20480 /* Here we add 1 to compensate for the last decrement
20481 of CURSOR, which took it past the valid range. */
20482 start_byte += cursor - base + 1;
20483 }
20484 }
20485
20486 *byte_pos_ptr = limit_byte;
20487
20488 if (count < 0)
20489 return - orig_count + count;
20490 return orig_count - count;
20491
20492 }
20493
20494
20495 \f
20496 /***********************************************************************
20497 Displaying strings
20498 ***********************************************************************/
20499
20500 /* Display a NUL-terminated string, starting with index START.
20501
20502 If STRING is non-null, display that C string. Otherwise, the Lisp
20503 string LISP_STRING is displayed. There's a case that STRING is
20504 non-null and LISP_STRING is not nil. It means STRING is a string
20505 data of LISP_STRING. In that case, we display LISP_STRING while
20506 ignoring its text properties.
20507
20508 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20509 FACE_STRING. Display STRING or LISP_STRING with the face at
20510 FACE_STRING_POS in FACE_STRING:
20511
20512 Display the string in the environment given by IT, but use the
20513 standard display table, temporarily.
20514
20515 FIELD_WIDTH is the minimum number of output glyphs to produce.
20516 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20517 with spaces. If STRING has more characters, more than FIELD_WIDTH
20518 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20519
20520 PRECISION is the maximum number of characters to output from
20521 STRING. PRECISION < 0 means don't truncate the string.
20522
20523 This is roughly equivalent to printf format specifiers:
20524
20525 FIELD_WIDTH PRECISION PRINTF
20526 ----------------------------------------
20527 -1 -1 %s
20528 -1 10 %.10s
20529 10 -1 %10s
20530 20 10 %20.10s
20531
20532 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20533 display them, and < 0 means obey the current buffer's value of
20534 enable_multibyte_characters.
20535
20536 Value is the number of columns displayed. */
20537
20538 static int
20539 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20540 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20541 int field_width, int precision, int max_x, int multibyte)
20542 {
20543 int hpos_at_start = it->hpos;
20544 int saved_face_id = it->face_id;
20545 struct glyph_row *row = it->glyph_row;
20546 EMACS_INT it_charpos;
20547
20548 /* Initialize the iterator IT for iteration over STRING beginning
20549 with index START. */
20550 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20551 precision, field_width, multibyte);
20552 if (string && STRINGP (lisp_string))
20553 /* LISP_STRING is the one returned by decode_mode_spec. We should
20554 ignore its text properties. */
20555 it->stop_charpos = it->end_charpos;
20556
20557 /* If displaying STRING, set up the face of the iterator from
20558 FACE_STRING, if that's given. */
20559 if (STRINGP (face_string))
20560 {
20561 EMACS_INT endptr;
20562 struct face *face;
20563
20564 it->face_id
20565 = face_at_string_position (it->w, face_string, face_string_pos,
20566 0, it->region_beg_charpos,
20567 it->region_end_charpos,
20568 &endptr, it->base_face_id, 0);
20569 face = FACE_FROM_ID (it->f, it->face_id);
20570 it->face_box_p = face->box != FACE_NO_BOX;
20571 }
20572
20573 /* Set max_x to the maximum allowed X position. Don't let it go
20574 beyond the right edge of the window. */
20575 if (max_x <= 0)
20576 max_x = it->last_visible_x;
20577 else
20578 max_x = min (max_x, it->last_visible_x);
20579
20580 /* Skip over display elements that are not visible. because IT->w is
20581 hscrolled. */
20582 if (it->current_x < it->first_visible_x)
20583 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20584 MOVE_TO_POS | MOVE_TO_X);
20585
20586 row->ascent = it->max_ascent;
20587 row->height = it->max_ascent + it->max_descent;
20588 row->phys_ascent = it->max_phys_ascent;
20589 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20590 row->extra_line_spacing = it->max_extra_line_spacing;
20591
20592 if (STRINGP (it->string))
20593 it_charpos = IT_STRING_CHARPOS (*it);
20594 else
20595 it_charpos = IT_CHARPOS (*it);
20596
20597 /* This condition is for the case that we are called with current_x
20598 past last_visible_x. */
20599 while (it->current_x < max_x)
20600 {
20601 int x_before, x, n_glyphs_before, i, nglyphs;
20602
20603 /* Get the next display element. */
20604 if (!get_next_display_element (it))
20605 break;
20606
20607 /* Produce glyphs. */
20608 x_before = it->current_x;
20609 n_glyphs_before = row->used[TEXT_AREA];
20610 PRODUCE_GLYPHS (it);
20611
20612 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20613 i = 0;
20614 x = x_before;
20615 while (i < nglyphs)
20616 {
20617 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20618
20619 if (it->line_wrap != TRUNCATE
20620 && x + glyph->pixel_width > max_x)
20621 {
20622 /* End of continued line or max_x reached. */
20623 if (CHAR_GLYPH_PADDING_P (*glyph))
20624 {
20625 /* A wide character is unbreakable. */
20626 if (row->reversed_p)
20627 unproduce_glyphs (it, row->used[TEXT_AREA]
20628 - n_glyphs_before);
20629 row->used[TEXT_AREA] = n_glyphs_before;
20630 it->current_x = x_before;
20631 }
20632 else
20633 {
20634 if (row->reversed_p)
20635 unproduce_glyphs (it, row->used[TEXT_AREA]
20636 - (n_glyphs_before + i));
20637 row->used[TEXT_AREA] = n_glyphs_before + i;
20638 it->current_x = x;
20639 }
20640 break;
20641 }
20642 else if (x + glyph->pixel_width >= it->first_visible_x)
20643 {
20644 /* Glyph is at least partially visible. */
20645 ++it->hpos;
20646 if (x < it->first_visible_x)
20647 row->x = x - it->first_visible_x;
20648 }
20649 else
20650 {
20651 /* Glyph is off the left margin of the display area.
20652 Should not happen. */
20653 abort ();
20654 }
20655
20656 row->ascent = max (row->ascent, it->max_ascent);
20657 row->height = max (row->height, it->max_ascent + it->max_descent);
20658 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20659 row->phys_height = max (row->phys_height,
20660 it->max_phys_ascent + it->max_phys_descent);
20661 row->extra_line_spacing = max (row->extra_line_spacing,
20662 it->max_extra_line_spacing);
20663 x += glyph->pixel_width;
20664 ++i;
20665 }
20666
20667 /* Stop if max_x reached. */
20668 if (i < nglyphs)
20669 break;
20670
20671 /* Stop at line ends. */
20672 if (ITERATOR_AT_END_OF_LINE_P (it))
20673 {
20674 it->continuation_lines_width = 0;
20675 break;
20676 }
20677
20678 set_iterator_to_next (it, 1);
20679 if (STRINGP (it->string))
20680 it_charpos = IT_STRING_CHARPOS (*it);
20681 else
20682 it_charpos = IT_CHARPOS (*it);
20683
20684 /* Stop if truncating at the right edge. */
20685 if (it->line_wrap == TRUNCATE
20686 && it->current_x >= it->last_visible_x)
20687 {
20688 /* Add truncation mark, but don't do it if the line is
20689 truncated at a padding space. */
20690 if (it_charpos < it->string_nchars)
20691 {
20692 if (!FRAME_WINDOW_P (it->f))
20693 {
20694 int ii, n;
20695
20696 if (it->current_x > it->last_visible_x)
20697 {
20698 if (!row->reversed_p)
20699 {
20700 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20701 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20702 break;
20703 }
20704 else
20705 {
20706 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
20707 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20708 break;
20709 unproduce_glyphs (it, ii + 1);
20710 ii = row->used[TEXT_AREA] - (ii + 1);
20711 }
20712 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20713 {
20714 row->used[TEXT_AREA] = ii;
20715 produce_special_glyphs (it, IT_TRUNCATION);
20716 }
20717 }
20718 produce_special_glyphs (it, IT_TRUNCATION);
20719 }
20720 row->truncated_on_right_p = 1;
20721 }
20722 break;
20723 }
20724 }
20725
20726 /* Maybe insert a truncation at the left. */
20727 if (it->first_visible_x
20728 && it_charpos > 0)
20729 {
20730 if (!FRAME_WINDOW_P (it->f))
20731 insert_left_trunc_glyphs (it);
20732 row->truncated_on_left_p = 1;
20733 }
20734
20735 it->face_id = saved_face_id;
20736
20737 /* Value is number of columns displayed. */
20738 return it->hpos - hpos_at_start;
20739 }
20740
20741
20742 \f
20743 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20744 appears as an element of LIST or as the car of an element of LIST.
20745 If PROPVAL is a list, compare each element against LIST in that
20746 way, and return 1/2 if any element of PROPVAL is found in LIST.
20747 Otherwise return 0. This function cannot quit.
20748 The return value is 2 if the text is invisible but with an ellipsis
20749 and 1 if it's invisible and without an ellipsis. */
20750
20751 int
20752 invisible_p (register Lisp_Object propval, Lisp_Object list)
20753 {
20754 register Lisp_Object tail, proptail;
20755
20756 for (tail = list; CONSP (tail); tail = XCDR (tail))
20757 {
20758 register Lisp_Object tem;
20759 tem = XCAR (tail);
20760 if (EQ (propval, tem))
20761 return 1;
20762 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20763 return NILP (XCDR (tem)) ? 1 : 2;
20764 }
20765
20766 if (CONSP (propval))
20767 {
20768 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20769 {
20770 Lisp_Object propelt;
20771 propelt = XCAR (proptail);
20772 for (tail = list; CONSP (tail); tail = XCDR (tail))
20773 {
20774 register Lisp_Object tem;
20775 tem = XCAR (tail);
20776 if (EQ (propelt, tem))
20777 return 1;
20778 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20779 return NILP (XCDR (tem)) ? 1 : 2;
20780 }
20781 }
20782 }
20783
20784 return 0;
20785 }
20786
20787 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20788 doc: /* Non-nil if the property makes the text invisible.
20789 POS-OR-PROP can be a marker or number, in which case it is taken to be
20790 a position in the current buffer and the value of the `invisible' property
20791 is checked; or it can be some other value, which is then presumed to be the
20792 value of the `invisible' property of the text of interest.
20793 The non-nil value returned can be t for truly invisible text or something
20794 else if the text is replaced by an ellipsis. */)
20795 (Lisp_Object pos_or_prop)
20796 {
20797 Lisp_Object prop
20798 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20799 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20800 : pos_or_prop);
20801 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20802 return (invis == 0 ? Qnil
20803 : invis == 1 ? Qt
20804 : make_number (invis));
20805 }
20806
20807 /* Calculate a width or height in pixels from a specification using
20808 the following elements:
20809
20810 SPEC ::=
20811 NUM - a (fractional) multiple of the default font width/height
20812 (NUM) - specifies exactly NUM pixels
20813 UNIT - a fixed number of pixels, see below.
20814 ELEMENT - size of a display element in pixels, see below.
20815 (NUM . SPEC) - equals NUM * SPEC
20816 (+ SPEC SPEC ...) - add pixel values
20817 (- SPEC SPEC ...) - subtract pixel values
20818 (- SPEC) - negate pixel value
20819
20820 NUM ::=
20821 INT or FLOAT - a number constant
20822 SYMBOL - use symbol's (buffer local) variable binding.
20823
20824 UNIT ::=
20825 in - pixels per inch *)
20826 mm - pixels per 1/1000 meter *)
20827 cm - pixels per 1/100 meter *)
20828 width - width of current font in pixels.
20829 height - height of current font in pixels.
20830
20831 *) using the ratio(s) defined in display-pixels-per-inch.
20832
20833 ELEMENT ::=
20834
20835 left-fringe - left fringe width in pixels
20836 right-fringe - right fringe width in pixels
20837
20838 left-margin - left margin width in pixels
20839 right-margin - right margin width in pixels
20840
20841 scroll-bar - scroll-bar area width in pixels
20842
20843 Examples:
20844
20845 Pixels corresponding to 5 inches:
20846 (5 . in)
20847
20848 Total width of non-text areas on left side of window (if scroll-bar is on left):
20849 '(space :width (+ left-fringe left-margin scroll-bar))
20850
20851 Align to first text column (in header line):
20852 '(space :align-to 0)
20853
20854 Align to middle of text area minus half the width of variable `my-image'
20855 containing a loaded image:
20856 '(space :align-to (0.5 . (- text my-image)))
20857
20858 Width of left margin minus width of 1 character in the default font:
20859 '(space :width (- left-margin 1))
20860
20861 Width of left margin minus width of 2 characters in the current font:
20862 '(space :width (- left-margin (2 . width)))
20863
20864 Center 1 character over left-margin (in header line):
20865 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20866
20867 Different ways to express width of left fringe plus left margin minus one pixel:
20868 '(space :width (- (+ left-fringe left-margin) (1)))
20869 '(space :width (+ left-fringe left-margin (- (1))))
20870 '(space :width (+ left-fringe left-margin (-1)))
20871
20872 */
20873
20874 #define NUMVAL(X) \
20875 ((INTEGERP (X) || FLOATP (X)) \
20876 ? XFLOATINT (X) \
20877 : - 1)
20878
20879 int
20880 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20881 struct font *font, int width_p, int *align_to)
20882 {
20883 double pixels;
20884
20885 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20886 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20887
20888 if (NILP (prop))
20889 return OK_PIXELS (0);
20890
20891 xassert (FRAME_LIVE_P (it->f));
20892
20893 if (SYMBOLP (prop))
20894 {
20895 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20896 {
20897 char *unit = SSDATA (SYMBOL_NAME (prop));
20898
20899 if (unit[0] == 'i' && unit[1] == 'n')
20900 pixels = 1.0;
20901 else if (unit[0] == 'm' && unit[1] == 'm')
20902 pixels = 25.4;
20903 else if (unit[0] == 'c' && unit[1] == 'm')
20904 pixels = 2.54;
20905 else
20906 pixels = 0;
20907 if (pixels > 0)
20908 {
20909 double ppi;
20910 #ifdef HAVE_WINDOW_SYSTEM
20911 if (FRAME_WINDOW_P (it->f)
20912 && (ppi = (width_p
20913 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20914 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20915 ppi > 0))
20916 return OK_PIXELS (ppi / pixels);
20917 #endif
20918
20919 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20920 || (CONSP (Vdisplay_pixels_per_inch)
20921 && (ppi = (width_p
20922 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20923 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20924 ppi > 0)))
20925 return OK_PIXELS (ppi / pixels);
20926
20927 return 0;
20928 }
20929 }
20930
20931 #ifdef HAVE_WINDOW_SYSTEM
20932 if (EQ (prop, Qheight))
20933 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20934 if (EQ (prop, Qwidth))
20935 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20936 #else
20937 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20938 return OK_PIXELS (1);
20939 #endif
20940
20941 if (EQ (prop, Qtext))
20942 return OK_PIXELS (width_p
20943 ? window_box_width (it->w, TEXT_AREA)
20944 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20945
20946 if (align_to && *align_to < 0)
20947 {
20948 *res = 0;
20949 if (EQ (prop, Qleft))
20950 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20951 if (EQ (prop, Qright))
20952 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20953 if (EQ (prop, Qcenter))
20954 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20955 + window_box_width (it->w, TEXT_AREA) / 2);
20956 if (EQ (prop, Qleft_fringe))
20957 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20958 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20959 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20960 if (EQ (prop, Qright_fringe))
20961 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20962 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20963 : window_box_right_offset (it->w, TEXT_AREA));
20964 if (EQ (prop, Qleft_margin))
20965 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20966 if (EQ (prop, Qright_margin))
20967 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20968 if (EQ (prop, Qscroll_bar))
20969 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20970 ? 0
20971 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20972 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20973 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20974 : 0)));
20975 }
20976 else
20977 {
20978 if (EQ (prop, Qleft_fringe))
20979 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20980 if (EQ (prop, Qright_fringe))
20981 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20982 if (EQ (prop, Qleft_margin))
20983 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20984 if (EQ (prop, Qright_margin))
20985 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20986 if (EQ (prop, Qscroll_bar))
20987 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20988 }
20989
20990 prop = Fbuffer_local_value (prop, it->w->buffer);
20991 }
20992
20993 if (INTEGERP (prop) || FLOATP (prop))
20994 {
20995 int base_unit = (width_p
20996 ? FRAME_COLUMN_WIDTH (it->f)
20997 : FRAME_LINE_HEIGHT (it->f));
20998 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20999 }
21000
21001 if (CONSP (prop))
21002 {
21003 Lisp_Object car = XCAR (prop);
21004 Lisp_Object cdr = XCDR (prop);
21005
21006 if (SYMBOLP (car))
21007 {
21008 #ifdef HAVE_WINDOW_SYSTEM
21009 if (FRAME_WINDOW_P (it->f)
21010 && valid_image_p (prop))
21011 {
21012 int id = lookup_image (it->f, prop);
21013 struct image *img = IMAGE_FROM_ID (it->f, id);
21014
21015 return OK_PIXELS (width_p ? img->width : img->height);
21016 }
21017 #endif
21018 if (EQ (car, Qplus) || EQ (car, Qminus))
21019 {
21020 int first = 1;
21021 double px;
21022
21023 pixels = 0;
21024 while (CONSP (cdr))
21025 {
21026 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21027 font, width_p, align_to))
21028 return 0;
21029 if (first)
21030 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21031 else
21032 pixels += px;
21033 cdr = XCDR (cdr);
21034 }
21035 if (EQ (car, Qminus))
21036 pixels = -pixels;
21037 return OK_PIXELS (pixels);
21038 }
21039
21040 car = Fbuffer_local_value (car, it->w->buffer);
21041 }
21042
21043 if (INTEGERP (car) || FLOATP (car))
21044 {
21045 double fact;
21046 pixels = XFLOATINT (car);
21047 if (NILP (cdr))
21048 return OK_PIXELS (pixels);
21049 if (calc_pixel_width_or_height (&fact, it, cdr,
21050 font, width_p, align_to))
21051 return OK_PIXELS (pixels * fact);
21052 return 0;
21053 }
21054
21055 return 0;
21056 }
21057
21058 return 0;
21059 }
21060
21061 \f
21062 /***********************************************************************
21063 Glyph Display
21064 ***********************************************************************/
21065
21066 #ifdef HAVE_WINDOW_SYSTEM
21067
21068 #if GLYPH_DEBUG
21069
21070 void
21071 dump_glyph_string (s)
21072 struct glyph_string *s;
21073 {
21074 fprintf (stderr, "glyph string\n");
21075 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21076 s->x, s->y, s->width, s->height);
21077 fprintf (stderr, " ybase = %d\n", s->ybase);
21078 fprintf (stderr, " hl = %d\n", s->hl);
21079 fprintf (stderr, " left overhang = %d, right = %d\n",
21080 s->left_overhang, s->right_overhang);
21081 fprintf (stderr, " nchars = %d\n", s->nchars);
21082 fprintf (stderr, " extends to end of line = %d\n",
21083 s->extends_to_end_of_line_p);
21084 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21085 fprintf (stderr, " bg width = %d\n", s->background_width);
21086 }
21087
21088 #endif /* GLYPH_DEBUG */
21089
21090 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21091 of XChar2b structures for S; it can't be allocated in
21092 init_glyph_string because it must be allocated via `alloca'. W
21093 is the window on which S is drawn. ROW and AREA are the glyph row
21094 and area within the row from which S is constructed. START is the
21095 index of the first glyph structure covered by S. HL is a
21096 face-override for drawing S. */
21097
21098 #ifdef HAVE_NTGUI
21099 #define OPTIONAL_HDC(hdc) HDC hdc,
21100 #define DECLARE_HDC(hdc) HDC hdc;
21101 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21102 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21103 #endif
21104
21105 #ifndef OPTIONAL_HDC
21106 #define OPTIONAL_HDC(hdc)
21107 #define DECLARE_HDC(hdc)
21108 #define ALLOCATE_HDC(hdc, f)
21109 #define RELEASE_HDC(hdc, f)
21110 #endif
21111
21112 static void
21113 init_glyph_string (struct glyph_string *s,
21114 OPTIONAL_HDC (hdc)
21115 XChar2b *char2b, struct window *w, struct glyph_row *row,
21116 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21117 {
21118 memset (s, 0, sizeof *s);
21119 s->w = w;
21120 s->f = XFRAME (w->frame);
21121 #ifdef HAVE_NTGUI
21122 s->hdc = hdc;
21123 #endif
21124 s->display = FRAME_X_DISPLAY (s->f);
21125 s->window = FRAME_X_WINDOW (s->f);
21126 s->char2b = char2b;
21127 s->hl = hl;
21128 s->row = row;
21129 s->area = area;
21130 s->first_glyph = row->glyphs[area] + start;
21131 s->height = row->height;
21132 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21133 s->ybase = s->y + row->ascent;
21134 }
21135
21136
21137 /* Append the list of glyph strings with head H and tail T to the list
21138 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21139
21140 static INLINE void
21141 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21142 struct glyph_string *h, struct glyph_string *t)
21143 {
21144 if (h)
21145 {
21146 if (*head)
21147 (*tail)->next = h;
21148 else
21149 *head = h;
21150 h->prev = *tail;
21151 *tail = t;
21152 }
21153 }
21154
21155
21156 /* Prepend the list of glyph strings with head H and tail T to the
21157 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21158 result. */
21159
21160 static INLINE void
21161 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21162 struct glyph_string *h, struct glyph_string *t)
21163 {
21164 if (h)
21165 {
21166 if (*head)
21167 (*head)->prev = t;
21168 else
21169 *tail = t;
21170 t->next = *head;
21171 *head = h;
21172 }
21173 }
21174
21175
21176 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21177 Set *HEAD and *TAIL to the resulting list. */
21178
21179 static INLINE void
21180 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21181 struct glyph_string *s)
21182 {
21183 s->next = s->prev = NULL;
21184 append_glyph_string_lists (head, tail, s, s);
21185 }
21186
21187
21188 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21189 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21190 make sure that X resources for the face returned are allocated.
21191 Value is a pointer to a realized face that is ready for display if
21192 DISPLAY_P is non-zero. */
21193
21194 static INLINE struct face *
21195 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21196 XChar2b *char2b, int display_p)
21197 {
21198 struct face *face = FACE_FROM_ID (f, face_id);
21199
21200 if (face->font)
21201 {
21202 unsigned code = face->font->driver->encode_char (face->font, c);
21203
21204 if (code != FONT_INVALID_CODE)
21205 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21206 else
21207 STORE_XCHAR2B (char2b, 0, 0);
21208 }
21209
21210 /* Make sure X resources of the face are allocated. */
21211 #ifdef HAVE_X_WINDOWS
21212 if (display_p)
21213 #endif
21214 {
21215 xassert (face != NULL);
21216 PREPARE_FACE_FOR_DISPLAY (f, face);
21217 }
21218
21219 return face;
21220 }
21221
21222
21223 /* Get face and two-byte form of character glyph GLYPH on frame F.
21224 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21225 a pointer to a realized face that is ready for display. */
21226
21227 static INLINE struct face *
21228 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21229 XChar2b *char2b, int *two_byte_p)
21230 {
21231 struct face *face;
21232
21233 xassert (glyph->type == CHAR_GLYPH);
21234 face = FACE_FROM_ID (f, glyph->face_id);
21235
21236 if (two_byte_p)
21237 *two_byte_p = 0;
21238
21239 if (face->font)
21240 {
21241 unsigned code;
21242
21243 if (CHAR_BYTE8_P (glyph->u.ch))
21244 code = CHAR_TO_BYTE8 (glyph->u.ch);
21245 else
21246 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21247
21248 if (code != FONT_INVALID_CODE)
21249 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21250 else
21251 STORE_XCHAR2B (char2b, 0, 0);
21252 }
21253
21254 /* Make sure X resources of the face are allocated. */
21255 xassert (face != NULL);
21256 PREPARE_FACE_FOR_DISPLAY (f, face);
21257 return face;
21258 }
21259
21260
21261 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21262 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21263
21264 static INLINE int
21265 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21266 {
21267 unsigned code;
21268
21269 if (CHAR_BYTE8_P (c))
21270 code = CHAR_TO_BYTE8 (c);
21271 else
21272 code = font->driver->encode_char (font, c);
21273
21274 if (code == FONT_INVALID_CODE)
21275 return 0;
21276 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21277 return 1;
21278 }
21279
21280
21281 /* Fill glyph string S with composition components specified by S->cmp.
21282
21283 BASE_FACE is the base face of the composition.
21284 S->cmp_from is the index of the first component for S.
21285
21286 OVERLAPS non-zero means S should draw the foreground only, and use
21287 its physical height for clipping. See also draw_glyphs.
21288
21289 Value is the index of a component not in S. */
21290
21291 static int
21292 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21293 int overlaps)
21294 {
21295 int i;
21296 /* For all glyphs of this composition, starting at the offset
21297 S->cmp_from, until we reach the end of the definition or encounter a
21298 glyph that requires the different face, add it to S. */
21299 struct face *face;
21300
21301 xassert (s);
21302
21303 s->for_overlaps = overlaps;
21304 s->face = NULL;
21305 s->font = NULL;
21306 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21307 {
21308 int c = COMPOSITION_GLYPH (s->cmp, i);
21309
21310 if (c != '\t')
21311 {
21312 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21313 -1, Qnil);
21314
21315 face = get_char_face_and_encoding (s->f, c, face_id,
21316 s->char2b + i, 1);
21317 if (face)
21318 {
21319 if (! s->face)
21320 {
21321 s->face = face;
21322 s->font = s->face->font;
21323 }
21324 else if (s->face != face)
21325 break;
21326 }
21327 }
21328 ++s->nchars;
21329 }
21330 s->cmp_to = i;
21331
21332 /* All glyph strings for the same composition has the same width,
21333 i.e. the width set for the first component of the composition. */
21334 s->width = s->first_glyph->pixel_width;
21335
21336 /* If the specified font could not be loaded, use the frame's
21337 default font, but record the fact that we couldn't load it in
21338 the glyph string so that we can draw rectangles for the
21339 characters of the glyph string. */
21340 if (s->font == NULL)
21341 {
21342 s->font_not_found_p = 1;
21343 s->font = FRAME_FONT (s->f);
21344 }
21345
21346 /* Adjust base line for subscript/superscript text. */
21347 s->ybase += s->first_glyph->voffset;
21348
21349 /* This glyph string must always be drawn with 16-bit functions. */
21350 s->two_byte_p = 1;
21351
21352 return s->cmp_to;
21353 }
21354
21355 static int
21356 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21357 int start, int end, int overlaps)
21358 {
21359 struct glyph *glyph, *last;
21360 Lisp_Object lgstring;
21361 int i;
21362
21363 s->for_overlaps = overlaps;
21364 glyph = s->row->glyphs[s->area] + start;
21365 last = s->row->glyphs[s->area] + end;
21366 s->cmp_id = glyph->u.cmp.id;
21367 s->cmp_from = glyph->slice.cmp.from;
21368 s->cmp_to = glyph->slice.cmp.to + 1;
21369 s->face = FACE_FROM_ID (s->f, face_id);
21370 lgstring = composition_gstring_from_id (s->cmp_id);
21371 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21372 glyph++;
21373 while (glyph < last
21374 && glyph->u.cmp.automatic
21375 && glyph->u.cmp.id == s->cmp_id
21376 && s->cmp_to == glyph->slice.cmp.from)
21377 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21378
21379 for (i = s->cmp_from; i < s->cmp_to; i++)
21380 {
21381 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21382 unsigned code = LGLYPH_CODE (lglyph);
21383
21384 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21385 }
21386 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21387 return glyph - s->row->glyphs[s->area];
21388 }
21389
21390
21391 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21392 See the comment of fill_glyph_string for arguments.
21393 Value is the index of the first glyph not in S. */
21394
21395
21396 static int
21397 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21398 int start, int end, int overlaps)
21399 {
21400 struct glyph *glyph, *last;
21401 int voffset;
21402
21403 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21404 s->for_overlaps = overlaps;
21405 glyph = s->row->glyphs[s->area] + start;
21406 last = s->row->glyphs[s->area] + end;
21407 voffset = glyph->voffset;
21408 s->face = FACE_FROM_ID (s->f, face_id);
21409 s->font = s->face->font;
21410 s->nchars = 1;
21411 s->width = glyph->pixel_width;
21412 glyph++;
21413 while (glyph < last
21414 && glyph->type == GLYPHLESS_GLYPH
21415 && glyph->voffset == voffset
21416 && glyph->face_id == face_id)
21417 {
21418 s->nchars++;
21419 s->width += glyph->pixel_width;
21420 glyph++;
21421 }
21422 s->ybase += voffset;
21423 return glyph - s->row->glyphs[s->area];
21424 }
21425
21426
21427 /* Fill glyph string S from a sequence of character glyphs.
21428
21429 FACE_ID is the face id of the string. START is the index of the
21430 first glyph to consider, END is the index of the last + 1.
21431 OVERLAPS non-zero means S should draw the foreground only, and use
21432 its physical height for clipping. See also draw_glyphs.
21433
21434 Value is the index of the first glyph not in S. */
21435
21436 static int
21437 fill_glyph_string (struct glyph_string *s, int face_id,
21438 int start, int end, int overlaps)
21439 {
21440 struct glyph *glyph, *last;
21441 int voffset;
21442 int glyph_not_available_p;
21443
21444 xassert (s->f == XFRAME (s->w->frame));
21445 xassert (s->nchars == 0);
21446 xassert (start >= 0 && end > start);
21447
21448 s->for_overlaps = overlaps;
21449 glyph = s->row->glyphs[s->area] + start;
21450 last = s->row->glyphs[s->area] + end;
21451 voffset = glyph->voffset;
21452 s->padding_p = glyph->padding_p;
21453 glyph_not_available_p = glyph->glyph_not_available_p;
21454
21455 while (glyph < last
21456 && glyph->type == CHAR_GLYPH
21457 && glyph->voffset == voffset
21458 /* Same face id implies same font, nowadays. */
21459 && glyph->face_id == face_id
21460 && glyph->glyph_not_available_p == glyph_not_available_p)
21461 {
21462 int two_byte_p;
21463
21464 s->face = get_glyph_face_and_encoding (s->f, glyph,
21465 s->char2b + s->nchars,
21466 &two_byte_p);
21467 s->two_byte_p = two_byte_p;
21468 ++s->nchars;
21469 xassert (s->nchars <= end - start);
21470 s->width += glyph->pixel_width;
21471 if (glyph++->padding_p != s->padding_p)
21472 break;
21473 }
21474
21475 s->font = s->face->font;
21476
21477 /* If the specified font could not be loaded, use the frame's font,
21478 but record the fact that we couldn't load it in
21479 S->font_not_found_p so that we can draw rectangles for the
21480 characters of the glyph string. */
21481 if (s->font == NULL || glyph_not_available_p)
21482 {
21483 s->font_not_found_p = 1;
21484 s->font = FRAME_FONT (s->f);
21485 }
21486
21487 /* Adjust base line for subscript/superscript text. */
21488 s->ybase += voffset;
21489
21490 xassert (s->face && s->face->gc);
21491 return glyph - s->row->glyphs[s->area];
21492 }
21493
21494
21495 /* Fill glyph string S from image glyph S->first_glyph. */
21496
21497 static void
21498 fill_image_glyph_string (struct glyph_string *s)
21499 {
21500 xassert (s->first_glyph->type == IMAGE_GLYPH);
21501 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21502 xassert (s->img);
21503 s->slice = s->first_glyph->slice.img;
21504 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21505 s->font = s->face->font;
21506 s->width = s->first_glyph->pixel_width;
21507
21508 /* Adjust base line for subscript/superscript text. */
21509 s->ybase += s->first_glyph->voffset;
21510 }
21511
21512
21513 /* Fill glyph string S from a sequence of stretch glyphs.
21514
21515 START is the index of the first glyph to consider,
21516 END is the index of the last + 1.
21517
21518 Value is the index of the first glyph not in S. */
21519
21520 static int
21521 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21522 {
21523 struct glyph *glyph, *last;
21524 int voffset, face_id;
21525
21526 xassert (s->first_glyph->type == STRETCH_GLYPH);
21527
21528 glyph = s->row->glyphs[s->area] + start;
21529 last = s->row->glyphs[s->area] + end;
21530 face_id = glyph->face_id;
21531 s->face = FACE_FROM_ID (s->f, face_id);
21532 s->font = s->face->font;
21533 s->width = glyph->pixel_width;
21534 s->nchars = 1;
21535 voffset = glyph->voffset;
21536
21537 for (++glyph;
21538 (glyph < last
21539 && glyph->type == STRETCH_GLYPH
21540 && glyph->voffset == voffset
21541 && glyph->face_id == face_id);
21542 ++glyph)
21543 s->width += glyph->pixel_width;
21544
21545 /* Adjust base line for subscript/superscript text. */
21546 s->ybase += voffset;
21547
21548 /* The case that face->gc == 0 is handled when drawing the glyph
21549 string by calling PREPARE_FACE_FOR_DISPLAY. */
21550 xassert (s->face);
21551 return glyph - s->row->glyphs[s->area];
21552 }
21553
21554 static struct font_metrics *
21555 get_per_char_metric (struct font *font, XChar2b *char2b)
21556 {
21557 static struct font_metrics metrics;
21558 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21559
21560 if (! font || code == FONT_INVALID_CODE)
21561 return NULL;
21562 font->driver->text_extents (font, &code, 1, &metrics);
21563 return &metrics;
21564 }
21565
21566 /* EXPORT for RIF:
21567 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21568 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21569 assumed to be zero. */
21570
21571 void
21572 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21573 {
21574 *left = *right = 0;
21575
21576 if (glyph->type == CHAR_GLYPH)
21577 {
21578 struct face *face;
21579 XChar2b char2b;
21580 struct font_metrics *pcm;
21581
21582 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21583 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21584 {
21585 if (pcm->rbearing > pcm->width)
21586 *right = pcm->rbearing - pcm->width;
21587 if (pcm->lbearing < 0)
21588 *left = -pcm->lbearing;
21589 }
21590 }
21591 else if (glyph->type == COMPOSITE_GLYPH)
21592 {
21593 if (! glyph->u.cmp.automatic)
21594 {
21595 struct composition *cmp = composition_table[glyph->u.cmp.id];
21596
21597 if (cmp->rbearing > cmp->pixel_width)
21598 *right = cmp->rbearing - cmp->pixel_width;
21599 if (cmp->lbearing < 0)
21600 *left = - cmp->lbearing;
21601 }
21602 else
21603 {
21604 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21605 struct font_metrics metrics;
21606
21607 composition_gstring_width (gstring, glyph->slice.cmp.from,
21608 glyph->slice.cmp.to + 1, &metrics);
21609 if (metrics.rbearing > metrics.width)
21610 *right = metrics.rbearing - metrics.width;
21611 if (metrics.lbearing < 0)
21612 *left = - metrics.lbearing;
21613 }
21614 }
21615 }
21616
21617
21618 /* Return the index of the first glyph preceding glyph string S that
21619 is overwritten by S because of S's left overhang. Value is -1
21620 if no glyphs are overwritten. */
21621
21622 static int
21623 left_overwritten (struct glyph_string *s)
21624 {
21625 int k;
21626
21627 if (s->left_overhang)
21628 {
21629 int x = 0, i;
21630 struct glyph *glyphs = s->row->glyphs[s->area];
21631 int first = s->first_glyph - glyphs;
21632
21633 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21634 x -= glyphs[i].pixel_width;
21635
21636 k = i + 1;
21637 }
21638 else
21639 k = -1;
21640
21641 return k;
21642 }
21643
21644
21645 /* Return the index of the first glyph preceding glyph string S that
21646 is overwriting S because of its right overhang. Value is -1 if no
21647 glyph in front of S overwrites S. */
21648
21649 static int
21650 left_overwriting (struct glyph_string *s)
21651 {
21652 int i, k, x;
21653 struct glyph *glyphs = s->row->glyphs[s->area];
21654 int first = s->first_glyph - glyphs;
21655
21656 k = -1;
21657 x = 0;
21658 for (i = first - 1; i >= 0; --i)
21659 {
21660 int left, right;
21661 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21662 if (x + right > 0)
21663 k = i;
21664 x -= glyphs[i].pixel_width;
21665 }
21666
21667 return k;
21668 }
21669
21670
21671 /* Return the index of the last glyph following glyph string S that is
21672 overwritten by S because of S's right overhang. Value is -1 if
21673 no such glyph is found. */
21674
21675 static int
21676 right_overwritten (struct glyph_string *s)
21677 {
21678 int k = -1;
21679
21680 if (s->right_overhang)
21681 {
21682 int x = 0, i;
21683 struct glyph *glyphs = s->row->glyphs[s->area];
21684 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21685 int end = s->row->used[s->area];
21686
21687 for (i = first; i < end && s->right_overhang > x; ++i)
21688 x += glyphs[i].pixel_width;
21689
21690 k = i;
21691 }
21692
21693 return k;
21694 }
21695
21696
21697 /* Return the index of the last glyph following glyph string S that
21698 overwrites S because of its left overhang. Value is negative
21699 if no such glyph is found. */
21700
21701 static int
21702 right_overwriting (struct glyph_string *s)
21703 {
21704 int i, k, x;
21705 int end = s->row->used[s->area];
21706 struct glyph *glyphs = s->row->glyphs[s->area];
21707 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21708
21709 k = -1;
21710 x = 0;
21711 for (i = first; i < end; ++i)
21712 {
21713 int left, right;
21714 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21715 if (x - left < 0)
21716 k = i;
21717 x += glyphs[i].pixel_width;
21718 }
21719
21720 return k;
21721 }
21722
21723
21724 /* Set background width of glyph string S. START is the index of the
21725 first glyph following S. LAST_X is the right-most x-position + 1
21726 in the drawing area. */
21727
21728 static INLINE void
21729 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21730 {
21731 /* If the face of this glyph string has to be drawn to the end of
21732 the drawing area, set S->extends_to_end_of_line_p. */
21733
21734 if (start == s->row->used[s->area]
21735 && s->area == TEXT_AREA
21736 && ((s->row->fill_line_p
21737 && (s->hl == DRAW_NORMAL_TEXT
21738 || s->hl == DRAW_IMAGE_RAISED
21739 || s->hl == DRAW_IMAGE_SUNKEN))
21740 || s->hl == DRAW_MOUSE_FACE))
21741 s->extends_to_end_of_line_p = 1;
21742
21743 /* If S extends its face to the end of the line, set its
21744 background_width to the distance to the right edge of the drawing
21745 area. */
21746 if (s->extends_to_end_of_line_p)
21747 s->background_width = last_x - s->x + 1;
21748 else
21749 s->background_width = s->width;
21750 }
21751
21752
21753 /* Compute overhangs and x-positions for glyph string S and its
21754 predecessors, or successors. X is the starting x-position for S.
21755 BACKWARD_P non-zero means process predecessors. */
21756
21757 static void
21758 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21759 {
21760 if (backward_p)
21761 {
21762 while (s)
21763 {
21764 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21765 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21766 x -= s->width;
21767 s->x = x;
21768 s = s->prev;
21769 }
21770 }
21771 else
21772 {
21773 while (s)
21774 {
21775 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21776 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21777 s->x = x;
21778 x += s->width;
21779 s = s->next;
21780 }
21781 }
21782 }
21783
21784
21785
21786 /* The following macros are only called from draw_glyphs below.
21787 They reference the following parameters of that function directly:
21788 `w', `row', `area', and `overlap_p'
21789 as well as the following local variables:
21790 `s', `f', and `hdc' (in W32) */
21791
21792 #ifdef HAVE_NTGUI
21793 /* On W32, silently add local `hdc' variable to argument list of
21794 init_glyph_string. */
21795 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21796 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21797 #else
21798 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21799 init_glyph_string (s, char2b, w, row, area, start, hl)
21800 #endif
21801
21802 /* Add a glyph string for a stretch glyph to the list of strings
21803 between HEAD and TAIL. START is the index of the stretch glyph in
21804 row area AREA of glyph row ROW. END is the index of the last glyph
21805 in that glyph row area. X is the current output position assigned
21806 to the new glyph string constructed. HL overrides that face of the
21807 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21808 is the right-most x-position of the drawing area. */
21809
21810 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21811 and below -- keep them on one line. */
21812 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21813 do \
21814 { \
21815 s = (struct glyph_string *) alloca (sizeof *s); \
21816 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21817 START = fill_stretch_glyph_string (s, START, END); \
21818 append_glyph_string (&HEAD, &TAIL, s); \
21819 s->x = (X); \
21820 } \
21821 while (0)
21822
21823
21824 /* Add a glyph string for an image glyph to the list of strings
21825 between HEAD and TAIL. START is the index of the image glyph in
21826 row area AREA of glyph row ROW. END is the index of the last glyph
21827 in that glyph row area. X is the current output position assigned
21828 to the new glyph string constructed. HL overrides that face of the
21829 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21830 is the right-most x-position of the drawing area. */
21831
21832 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21833 do \
21834 { \
21835 s = (struct glyph_string *) alloca (sizeof *s); \
21836 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21837 fill_image_glyph_string (s); \
21838 append_glyph_string (&HEAD, &TAIL, s); \
21839 ++START; \
21840 s->x = (X); \
21841 } \
21842 while (0)
21843
21844
21845 /* Add a glyph string for a sequence of character glyphs to the list
21846 of strings between HEAD and TAIL. START is the index of the first
21847 glyph in row area AREA of glyph row ROW that is part of the new
21848 glyph string. END is the index of the last glyph in that glyph row
21849 area. X is the current output position assigned to the new glyph
21850 string constructed. HL overrides that face of the glyph; e.g. it
21851 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21852 right-most x-position of the drawing area. */
21853
21854 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21855 do \
21856 { \
21857 int face_id; \
21858 XChar2b *char2b; \
21859 \
21860 face_id = (row)->glyphs[area][START].face_id; \
21861 \
21862 s = (struct glyph_string *) alloca (sizeof *s); \
21863 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21864 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21865 append_glyph_string (&HEAD, &TAIL, s); \
21866 s->x = (X); \
21867 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21868 } \
21869 while (0)
21870
21871
21872 /* Add a glyph string for a composite sequence to the list of strings
21873 between HEAD and TAIL. START is the index of the first glyph in
21874 row area AREA of glyph row ROW that is part of the new glyph
21875 string. END is the index of the last glyph in that glyph row area.
21876 X is the current output position assigned to the new glyph string
21877 constructed. HL overrides that face of the glyph; e.g. it is
21878 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21879 x-position of the drawing area. */
21880
21881 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21882 do { \
21883 int face_id = (row)->glyphs[area][START].face_id; \
21884 struct face *base_face = FACE_FROM_ID (f, face_id); \
21885 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21886 struct composition *cmp = composition_table[cmp_id]; \
21887 XChar2b *char2b; \
21888 struct glyph_string *first_s IF_LINT (= NULL); \
21889 int n; \
21890 \
21891 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21892 \
21893 /* Make glyph_strings for each glyph sequence that is drawable by \
21894 the same face, and append them to HEAD/TAIL. */ \
21895 for (n = 0; n < cmp->glyph_len;) \
21896 { \
21897 s = (struct glyph_string *) alloca (sizeof *s); \
21898 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21899 append_glyph_string (&(HEAD), &(TAIL), s); \
21900 s->cmp = cmp; \
21901 s->cmp_from = n; \
21902 s->x = (X); \
21903 if (n == 0) \
21904 first_s = s; \
21905 n = fill_composite_glyph_string (s, base_face, overlaps); \
21906 } \
21907 \
21908 ++START; \
21909 s = first_s; \
21910 } while (0)
21911
21912
21913 /* Add a glyph string for a glyph-string sequence to the list of strings
21914 between HEAD and TAIL. */
21915
21916 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21917 do { \
21918 int face_id; \
21919 XChar2b *char2b; \
21920 Lisp_Object gstring; \
21921 \
21922 face_id = (row)->glyphs[area][START].face_id; \
21923 gstring = (composition_gstring_from_id \
21924 ((row)->glyphs[area][START].u.cmp.id)); \
21925 s = (struct glyph_string *) alloca (sizeof *s); \
21926 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21927 * LGSTRING_GLYPH_LEN (gstring)); \
21928 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21929 append_glyph_string (&(HEAD), &(TAIL), s); \
21930 s->x = (X); \
21931 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21932 } while (0)
21933
21934
21935 /* Add a glyph string for a sequence of glyphless character's glyphs
21936 to the list of strings between HEAD and TAIL. The meanings of
21937 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21938
21939 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21940 do \
21941 { \
21942 int face_id; \
21943 \
21944 face_id = (row)->glyphs[area][START].face_id; \
21945 \
21946 s = (struct glyph_string *) alloca (sizeof *s); \
21947 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21948 append_glyph_string (&HEAD, &TAIL, s); \
21949 s->x = (X); \
21950 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21951 overlaps); \
21952 } \
21953 while (0)
21954
21955
21956 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21957 of AREA of glyph row ROW on window W between indices START and END.
21958 HL overrides the face for drawing glyph strings, e.g. it is
21959 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21960 x-positions of the drawing area.
21961
21962 This is an ugly monster macro construct because we must use alloca
21963 to allocate glyph strings (because draw_glyphs can be called
21964 asynchronously). */
21965
21966 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21967 do \
21968 { \
21969 HEAD = TAIL = NULL; \
21970 while (START < END) \
21971 { \
21972 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21973 switch (first_glyph->type) \
21974 { \
21975 case CHAR_GLYPH: \
21976 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21977 HL, X, LAST_X); \
21978 break; \
21979 \
21980 case COMPOSITE_GLYPH: \
21981 if (first_glyph->u.cmp.automatic) \
21982 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21983 HL, X, LAST_X); \
21984 else \
21985 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21986 HL, X, LAST_X); \
21987 break; \
21988 \
21989 case STRETCH_GLYPH: \
21990 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21991 HL, X, LAST_X); \
21992 break; \
21993 \
21994 case IMAGE_GLYPH: \
21995 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21996 HL, X, LAST_X); \
21997 break; \
21998 \
21999 case GLYPHLESS_GLYPH: \
22000 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22001 HL, X, LAST_X); \
22002 break; \
22003 \
22004 default: \
22005 abort (); \
22006 } \
22007 \
22008 if (s) \
22009 { \
22010 set_glyph_string_background_width (s, START, LAST_X); \
22011 (X) += s->width; \
22012 } \
22013 } \
22014 } while (0)
22015
22016
22017 /* Draw glyphs between START and END in AREA of ROW on window W,
22018 starting at x-position X. X is relative to AREA in W. HL is a
22019 face-override with the following meaning:
22020
22021 DRAW_NORMAL_TEXT draw normally
22022 DRAW_CURSOR draw in cursor face
22023 DRAW_MOUSE_FACE draw in mouse face.
22024 DRAW_INVERSE_VIDEO draw in mode line face
22025 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22026 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22027
22028 If OVERLAPS is non-zero, draw only the foreground of characters and
22029 clip to the physical height of ROW. Non-zero value also defines
22030 the overlapping part to be drawn:
22031
22032 OVERLAPS_PRED overlap with preceding rows
22033 OVERLAPS_SUCC overlap with succeeding rows
22034 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22035 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22036
22037 Value is the x-position reached, relative to AREA of W. */
22038
22039 static int
22040 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22041 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22042 enum draw_glyphs_face hl, int overlaps)
22043 {
22044 struct glyph_string *head, *tail;
22045 struct glyph_string *s;
22046 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22047 int i, j, x_reached, last_x, area_left = 0;
22048 struct frame *f = XFRAME (WINDOW_FRAME (w));
22049 DECLARE_HDC (hdc);
22050
22051 ALLOCATE_HDC (hdc, f);
22052
22053 /* Let's rather be paranoid than getting a SEGV. */
22054 end = min (end, row->used[area]);
22055 start = max (0, start);
22056 start = min (end, start);
22057
22058 /* Translate X to frame coordinates. Set last_x to the right
22059 end of the drawing area. */
22060 if (row->full_width_p)
22061 {
22062 /* X is relative to the left edge of W, without scroll bars
22063 or fringes. */
22064 area_left = WINDOW_LEFT_EDGE_X (w);
22065 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22066 }
22067 else
22068 {
22069 area_left = window_box_left (w, area);
22070 last_x = area_left + window_box_width (w, area);
22071 }
22072 x += area_left;
22073
22074 /* Build a doubly-linked list of glyph_string structures between
22075 head and tail from what we have to draw. Note that the macro
22076 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22077 the reason we use a separate variable `i'. */
22078 i = start;
22079 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22080 if (tail)
22081 x_reached = tail->x + tail->background_width;
22082 else
22083 x_reached = x;
22084
22085 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22086 the row, redraw some glyphs in front or following the glyph
22087 strings built above. */
22088 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22089 {
22090 struct glyph_string *h, *t;
22091 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22092 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22093 int check_mouse_face = 0;
22094 int dummy_x = 0;
22095
22096 /* If mouse highlighting is on, we may need to draw adjacent
22097 glyphs using mouse-face highlighting. */
22098 if (area == TEXT_AREA && row->mouse_face_p)
22099 {
22100 struct glyph_row *mouse_beg_row, *mouse_end_row;
22101
22102 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22103 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22104
22105 if (row >= mouse_beg_row && row <= mouse_end_row)
22106 {
22107 check_mouse_face = 1;
22108 mouse_beg_col = (row == mouse_beg_row)
22109 ? hlinfo->mouse_face_beg_col : 0;
22110 mouse_end_col = (row == mouse_end_row)
22111 ? hlinfo->mouse_face_end_col
22112 : row->used[TEXT_AREA];
22113 }
22114 }
22115
22116 /* Compute overhangs for all glyph strings. */
22117 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22118 for (s = head; s; s = s->next)
22119 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22120
22121 /* Prepend glyph strings for glyphs in front of the first glyph
22122 string that are overwritten because of the first glyph
22123 string's left overhang. The background of all strings
22124 prepended must be drawn because the first glyph string
22125 draws over it. */
22126 i = left_overwritten (head);
22127 if (i >= 0)
22128 {
22129 enum draw_glyphs_face overlap_hl;
22130
22131 /* If this row contains mouse highlighting, attempt to draw
22132 the overlapped glyphs with the correct highlight. This
22133 code fails if the overlap encompasses more than one glyph
22134 and mouse-highlight spans only some of these glyphs.
22135 However, making it work perfectly involves a lot more
22136 code, and I don't know if the pathological case occurs in
22137 practice, so we'll stick to this for now. --- cyd */
22138 if (check_mouse_face
22139 && mouse_beg_col < start && mouse_end_col > i)
22140 overlap_hl = DRAW_MOUSE_FACE;
22141 else
22142 overlap_hl = DRAW_NORMAL_TEXT;
22143
22144 j = i;
22145 BUILD_GLYPH_STRINGS (j, start, h, t,
22146 overlap_hl, dummy_x, last_x);
22147 start = i;
22148 compute_overhangs_and_x (t, head->x, 1);
22149 prepend_glyph_string_lists (&head, &tail, h, t);
22150 clip_head = head;
22151 }
22152
22153 /* Prepend glyph strings for glyphs in front of the first glyph
22154 string that overwrite that glyph string because of their
22155 right overhang. For these strings, only the foreground must
22156 be drawn, because it draws over the glyph string at `head'.
22157 The background must not be drawn because this would overwrite
22158 right overhangs of preceding glyphs for which no glyph
22159 strings exist. */
22160 i = left_overwriting (head);
22161 if (i >= 0)
22162 {
22163 enum draw_glyphs_face overlap_hl;
22164
22165 if (check_mouse_face
22166 && mouse_beg_col < start && mouse_end_col > i)
22167 overlap_hl = DRAW_MOUSE_FACE;
22168 else
22169 overlap_hl = DRAW_NORMAL_TEXT;
22170
22171 clip_head = head;
22172 BUILD_GLYPH_STRINGS (i, start, h, t,
22173 overlap_hl, dummy_x, last_x);
22174 for (s = h; s; s = s->next)
22175 s->background_filled_p = 1;
22176 compute_overhangs_and_x (t, head->x, 1);
22177 prepend_glyph_string_lists (&head, &tail, h, t);
22178 }
22179
22180 /* Append glyphs strings for glyphs following the last glyph
22181 string tail that are overwritten by tail. The background of
22182 these strings has to be drawn because tail's foreground draws
22183 over it. */
22184 i = right_overwritten (tail);
22185 if (i >= 0)
22186 {
22187 enum draw_glyphs_face overlap_hl;
22188
22189 if (check_mouse_face
22190 && mouse_beg_col < i && mouse_end_col > end)
22191 overlap_hl = DRAW_MOUSE_FACE;
22192 else
22193 overlap_hl = DRAW_NORMAL_TEXT;
22194
22195 BUILD_GLYPH_STRINGS (end, i, h, t,
22196 overlap_hl, x, last_x);
22197 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22198 we don't have `end = i;' here. */
22199 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22200 append_glyph_string_lists (&head, &tail, h, t);
22201 clip_tail = tail;
22202 }
22203
22204 /* Append glyph strings for glyphs following the last glyph
22205 string tail that overwrite tail. The foreground of such
22206 glyphs has to be drawn because it writes into the background
22207 of tail. The background must not be drawn because it could
22208 paint over the foreground of following glyphs. */
22209 i = right_overwriting (tail);
22210 if (i >= 0)
22211 {
22212 enum draw_glyphs_face overlap_hl;
22213 if (check_mouse_face
22214 && mouse_beg_col < i && mouse_end_col > end)
22215 overlap_hl = DRAW_MOUSE_FACE;
22216 else
22217 overlap_hl = DRAW_NORMAL_TEXT;
22218
22219 clip_tail = tail;
22220 i++; /* We must include the Ith glyph. */
22221 BUILD_GLYPH_STRINGS (end, i, h, t,
22222 overlap_hl, x, last_x);
22223 for (s = h; s; s = s->next)
22224 s->background_filled_p = 1;
22225 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22226 append_glyph_string_lists (&head, &tail, h, t);
22227 }
22228 if (clip_head || clip_tail)
22229 for (s = head; s; s = s->next)
22230 {
22231 s->clip_head = clip_head;
22232 s->clip_tail = clip_tail;
22233 }
22234 }
22235
22236 /* Draw all strings. */
22237 for (s = head; s; s = s->next)
22238 FRAME_RIF (f)->draw_glyph_string (s);
22239
22240 #ifndef HAVE_NS
22241 /* When focus a sole frame and move horizontally, this sets on_p to 0
22242 causing a failure to erase prev cursor position. */
22243 if (area == TEXT_AREA
22244 && !row->full_width_p
22245 /* When drawing overlapping rows, only the glyph strings'
22246 foreground is drawn, which doesn't erase a cursor
22247 completely. */
22248 && !overlaps)
22249 {
22250 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22251 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22252 : (tail ? tail->x + tail->background_width : x));
22253 x0 -= area_left;
22254 x1 -= area_left;
22255
22256 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22257 row->y, MATRIX_ROW_BOTTOM_Y (row));
22258 }
22259 #endif
22260
22261 /* Value is the x-position up to which drawn, relative to AREA of W.
22262 This doesn't include parts drawn because of overhangs. */
22263 if (row->full_width_p)
22264 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22265 else
22266 x_reached -= area_left;
22267
22268 RELEASE_HDC (hdc, f);
22269
22270 return x_reached;
22271 }
22272
22273 /* Expand row matrix if too narrow. Don't expand if area
22274 is not present. */
22275
22276 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22277 { \
22278 if (!fonts_changed_p \
22279 && (it->glyph_row->glyphs[area] \
22280 < it->glyph_row->glyphs[area + 1])) \
22281 { \
22282 it->w->ncols_scale_factor++; \
22283 fonts_changed_p = 1; \
22284 } \
22285 }
22286
22287 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22288 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22289
22290 static INLINE void
22291 append_glyph (struct it *it)
22292 {
22293 struct glyph *glyph;
22294 enum glyph_row_area area = it->area;
22295
22296 xassert (it->glyph_row);
22297 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22298
22299 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22300 if (glyph < it->glyph_row->glyphs[area + 1])
22301 {
22302 /* If the glyph row is reversed, we need to prepend the glyph
22303 rather than append it. */
22304 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22305 {
22306 struct glyph *g;
22307
22308 /* Make room for the additional glyph. */
22309 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22310 g[1] = *g;
22311 glyph = it->glyph_row->glyphs[area];
22312 }
22313 glyph->charpos = CHARPOS (it->position);
22314 glyph->object = it->object;
22315 if (it->pixel_width > 0)
22316 {
22317 glyph->pixel_width = it->pixel_width;
22318 glyph->padding_p = 0;
22319 }
22320 else
22321 {
22322 /* Assure at least 1-pixel width. Otherwise, cursor can't
22323 be displayed correctly. */
22324 glyph->pixel_width = 1;
22325 glyph->padding_p = 1;
22326 }
22327 glyph->ascent = it->ascent;
22328 glyph->descent = it->descent;
22329 glyph->voffset = it->voffset;
22330 glyph->type = CHAR_GLYPH;
22331 glyph->avoid_cursor_p = it->avoid_cursor_p;
22332 glyph->multibyte_p = it->multibyte_p;
22333 glyph->left_box_line_p = it->start_of_box_run_p;
22334 glyph->right_box_line_p = it->end_of_box_run_p;
22335 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22336 || it->phys_descent > it->descent);
22337 glyph->glyph_not_available_p = it->glyph_not_available_p;
22338 glyph->face_id = it->face_id;
22339 glyph->u.ch = it->char_to_display;
22340 glyph->slice.img = null_glyph_slice;
22341 glyph->font_type = FONT_TYPE_UNKNOWN;
22342 if (it->bidi_p)
22343 {
22344 glyph->resolved_level = it->bidi_it.resolved_level;
22345 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22346 abort ();
22347 glyph->bidi_type = it->bidi_it.type;
22348 }
22349 else
22350 {
22351 glyph->resolved_level = 0;
22352 glyph->bidi_type = UNKNOWN_BT;
22353 }
22354 ++it->glyph_row->used[area];
22355 }
22356 else
22357 IT_EXPAND_MATRIX_WIDTH (it, area);
22358 }
22359
22360 /* Store one glyph for the composition IT->cmp_it.id in
22361 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22362 non-null. */
22363
22364 static INLINE void
22365 append_composite_glyph (struct it *it)
22366 {
22367 struct glyph *glyph;
22368 enum glyph_row_area area = it->area;
22369
22370 xassert (it->glyph_row);
22371
22372 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22373 if (glyph < it->glyph_row->glyphs[area + 1])
22374 {
22375 /* If the glyph row is reversed, we need to prepend the glyph
22376 rather than append it. */
22377 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22378 {
22379 struct glyph *g;
22380
22381 /* Make room for the new glyph. */
22382 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22383 g[1] = *g;
22384 glyph = it->glyph_row->glyphs[it->area];
22385 }
22386 glyph->charpos = it->cmp_it.charpos;
22387 glyph->object = it->object;
22388 glyph->pixel_width = it->pixel_width;
22389 glyph->ascent = it->ascent;
22390 glyph->descent = it->descent;
22391 glyph->voffset = it->voffset;
22392 glyph->type = COMPOSITE_GLYPH;
22393 if (it->cmp_it.ch < 0)
22394 {
22395 glyph->u.cmp.automatic = 0;
22396 glyph->u.cmp.id = it->cmp_it.id;
22397 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22398 }
22399 else
22400 {
22401 glyph->u.cmp.automatic = 1;
22402 glyph->u.cmp.id = it->cmp_it.id;
22403 glyph->slice.cmp.from = it->cmp_it.from;
22404 glyph->slice.cmp.to = it->cmp_it.to - 1;
22405 }
22406 glyph->avoid_cursor_p = it->avoid_cursor_p;
22407 glyph->multibyte_p = it->multibyte_p;
22408 glyph->left_box_line_p = it->start_of_box_run_p;
22409 glyph->right_box_line_p = it->end_of_box_run_p;
22410 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22411 || it->phys_descent > it->descent);
22412 glyph->padding_p = 0;
22413 glyph->glyph_not_available_p = 0;
22414 glyph->face_id = it->face_id;
22415 glyph->font_type = FONT_TYPE_UNKNOWN;
22416 if (it->bidi_p)
22417 {
22418 glyph->resolved_level = it->bidi_it.resolved_level;
22419 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22420 abort ();
22421 glyph->bidi_type = it->bidi_it.type;
22422 }
22423 ++it->glyph_row->used[area];
22424 }
22425 else
22426 IT_EXPAND_MATRIX_WIDTH (it, area);
22427 }
22428
22429
22430 /* Change IT->ascent and IT->height according to the setting of
22431 IT->voffset. */
22432
22433 static INLINE void
22434 take_vertical_position_into_account (struct it *it)
22435 {
22436 if (it->voffset)
22437 {
22438 if (it->voffset < 0)
22439 /* Increase the ascent so that we can display the text higher
22440 in the line. */
22441 it->ascent -= it->voffset;
22442 else
22443 /* Increase the descent so that we can display the text lower
22444 in the line. */
22445 it->descent += it->voffset;
22446 }
22447 }
22448
22449
22450 /* Produce glyphs/get display metrics for the image IT is loaded with.
22451 See the description of struct display_iterator in dispextern.h for
22452 an overview of struct display_iterator. */
22453
22454 static void
22455 produce_image_glyph (struct it *it)
22456 {
22457 struct image *img;
22458 struct face *face;
22459 int glyph_ascent, crop;
22460 struct glyph_slice slice;
22461
22462 xassert (it->what == IT_IMAGE);
22463
22464 face = FACE_FROM_ID (it->f, it->face_id);
22465 xassert (face);
22466 /* Make sure X resources of the face is loaded. */
22467 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22468
22469 if (it->image_id < 0)
22470 {
22471 /* Fringe bitmap. */
22472 it->ascent = it->phys_ascent = 0;
22473 it->descent = it->phys_descent = 0;
22474 it->pixel_width = 0;
22475 it->nglyphs = 0;
22476 return;
22477 }
22478
22479 img = IMAGE_FROM_ID (it->f, it->image_id);
22480 xassert (img);
22481 /* Make sure X resources of the image is loaded. */
22482 prepare_image_for_display (it->f, img);
22483
22484 slice.x = slice.y = 0;
22485 slice.width = img->width;
22486 slice.height = img->height;
22487
22488 if (INTEGERP (it->slice.x))
22489 slice.x = XINT (it->slice.x);
22490 else if (FLOATP (it->slice.x))
22491 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22492
22493 if (INTEGERP (it->slice.y))
22494 slice.y = XINT (it->slice.y);
22495 else if (FLOATP (it->slice.y))
22496 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22497
22498 if (INTEGERP (it->slice.width))
22499 slice.width = XINT (it->slice.width);
22500 else if (FLOATP (it->slice.width))
22501 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22502
22503 if (INTEGERP (it->slice.height))
22504 slice.height = XINT (it->slice.height);
22505 else if (FLOATP (it->slice.height))
22506 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22507
22508 if (slice.x >= img->width)
22509 slice.x = img->width;
22510 if (slice.y >= img->height)
22511 slice.y = img->height;
22512 if (slice.x + slice.width >= img->width)
22513 slice.width = img->width - slice.x;
22514 if (slice.y + slice.height > img->height)
22515 slice.height = img->height - slice.y;
22516
22517 if (slice.width == 0 || slice.height == 0)
22518 return;
22519
22520 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22521
22522 it->descent = slice.height - glyph_ascent;
22523 if (slice.y == 0)
22524 it->descent += img->vmargin;
22525 if (slice.y + slice.height == img->height)
22526 it->descent += img->vmargin;
22527 it->phys_descent = it->descent;
22528
22529 it->pixel_width = slice.width;
22530 if (slice.x == 0)
22531 it->pixel_width += img->hmargin;
22532 if (slice.x + slice.width == img->width)
22533 it->pixel_width += img->hmargin;
22534
22535 /* It's quite possible for images to have an ascent greater than
22536 their height, so don't get confused in that case. */
22537 if (it->descent < 0)
22538 it->descent = 0;
22539
22540 it->nglyphs = 1;
22541
22542 if (face->box != FACE_NO_BOX)
22543 {
22544 if (face->box_line_width > 0)
22545 {
22546 if (slice.y == 0)
22547 it->ascent += face->box_line_width;
22548 if (slice.y + slice.height == img->height)
22549 it->descent += face->box_line_width;
22550 }
22551
22552 if (it->start_of_box_run_p && slice.x == 0)
22553 it->pixel_width += eabs (face->box_line_width);
22554 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22555 it->pixel_width += eabs (face->box_line_width);
22556 }
22557
22558 take_vertical_position_into_account (it);
22559
22560 /* Automatically crop wide image glyphs at right edge so we can
22561 draw the cursor on same display row. */
22562 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22563 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22564 {
22565 it->pixel_width -= crop;
22566 slice.width -= crop;
22567 }
22568
22569 if (it->glyph_row)
22570 {
22571 struct glyph *glyph;
22572 enum glyph_row_area area = it->area;
22573
22574 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22575 if (glyph < it->glyph_row->glyphs[area + 1])
22576 {
22577 glyph->charpos = CHARPOS (it->position);
22578 glyph->object = it->object;
22579 glyph->pixel_width = it->pixel_width;
22580 glyph->ascent = glyph_ascent;
22581 glyph->descent = it->descent;
22582 glyph->voffset = it->voffset;
22583 glyph->type = IMAGE_GLYPH;
22584 glyph->avoid_cursor_p = it->avoid_cursor_p;
22585 glyph->multibyte_p = it->multibyte_p;
22586 glyph->left_box_line_p = it->start_of_box_run_p;
22587 glyph->right_box_line_p = it->end_of_box_run_p;
22588 glyph->overlaps_vertically_p = 0;
22589 glyph->padding_p = 0;
22590 glyph->glyph_not_available_p = 0;
22591 glyph->face_id = it->face_id;
22592 glyph->u.img_id = img->id;
22593 glyph->slice.img = slice;
22594 glyph->font_type = FONT_TYPE_UNKNOWN;
22595 if (it->bidi_p)
22596 {
22597 glyph->resolved_level = it->bidi_it.resolved_level;
22598 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22599 abort ();
22600 glyph->bidi_type = it->bidi_it.type;
22601 }
22602 ++it->glyph_row->used[area];
22603 }
22604 else
22605 IT_EXPAND_MATRIX_WIDTH (it, area);
22606 }
22607 }
22608
22609
22610 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22611 of the glyph, WIDTH and HEIGHT are the width and height of the
22612 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22613
22614 static void
22615 append_stretch_glyph (struct it *it, Lisp_Object object,
22616 int width, int height, int ascent)
22617 {
22618 struct glyph *glyph;
22619 enum glyph_row_area area = it->area;
22620
22621 xassert (ascent >= 0 && ascent <= height);
22622
22623 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22624 if (glyph < it->glyph_row->glyphs[area + 1])
22625 {
22626 /* If the glyph row is reversed, we need to prepend the glyph
22627 rather than append it. */
22628 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22629 {
22630 struct glyph *g;
22631
22632 /* Make room for the additional glyph. */
22633 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22634 g[1] = *g;
22635 glyph = it->glyph_row->glyphs[area];
22636 }
22637 glyph->charpos = CHARPOS (it->position);
22638 glyph->object = object;
22639 glyph->pixel_width = width;
22640 glyph->ascent = ascent;
22641 glyph->descent = height - ascent;
22642 glyph->voffset = it->voffset;
22643 glyph->type = STRETCH_GLYPH;
22644 glyph->avoid_cursor_p = it->avoid_cursor_p;
22645 glyph->multibyte_p = it->multibyte_p;
22646 glyph->left_box_line_p = it->start_of_box_run_p;
22647 glyph->right_box_line_p = it->end_of_box_run_p;
22648 glyph->overlaps_vertically_p = 0;
22649 glyph->padding_p = 0;
22650 glyph->glyph_not_available_p = 0;
22651 glyph->face_id = it->face_id;
22652 glyph->u.stretch.ascent = ascent;
22653 glyph->u.stretch.height = height;
22654 glyph->slice.img = null_glyph_slice;
22655 glyph->font_type = FONT_TYPE_UNKNOWN;
22656 if (it->bidi_p)
22657 {
22658 glyph->resolved_level = it->bidi_it.resolved_level;
22659 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22660 abort ();
22661 glyph->bidi_type = it->bidi_it.type;
22662 }
22663 else
22664 {
22665 glyph->resolved_level = 0;
22666 glyph->bidi_type = UNKNOWN_BT;
22667 }
22668 ++it->glyph_row->used[area];
22669 }
22670 else
22671 IT_EXPAND_MATRIX_WIDTH (it, area);
22672 }
22673
22674
22675 /* Produce a stretch glyph for iterator IT. IT->object is the value
22676 of the glyph property displayed. The value must be a list
22677 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22678 being recognized:
22679
22680 1. `:width WIDTH' specifies that the space should be WIDTH *
22681 canonical char width wide. WIDTH may be an integer or floating
22682 point number.
22683
22684 2. `:relative-width FACTOR' specifies that the width of the stretch
22685 should be computed from the width of the first character having the
22686 `glyph' property, and should be FACTOR times that width.
22687
22688 3. `:align-to HPOS' specifies that the space should be wide enough
22689 to reach HPOS, a value in canonical character units.
22690
22691 Exactly one of the above pairs must be present.
22692
22693 4. `:height HEIGHT' specifies that the height of the stretch produced
22694 should be HEIGHT, measured in canonical character units.
22695
22696 5. `:relative-height FACTOR' specifies that the height of the
22697 stretch should be FACTOR times the height of the characters having
22698 the glyph property.
22699
22700 Either none or exactly one of 4 or 5 must be present.
22701
22702 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22703 of the stretch should be used for the ascent of the stretch.
22704 ASCENT must be in the range 0 <= ASCENT <= 100. */
22705
22706 static void
22707 produce_stretch_glyph (struct it *it)
22708 {
22709 /* (space :width WIDTH :height HEIGHT ...) */
22710 Lisp_Object prop, plist;
22711 int width = 0, height = 0, align_to = -1;
22712 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22713 int ascent = 0;
22714 double tem;
22715 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22716 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22717
22718 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22719
22720 /* List should start with `space'. */
22721 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22722 plist = XCDR (it->object);
22723
22724 /* Compute the width of the stretch. */
22725 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22726 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22727 {
22728 /* Absolute width `:width WIDTH' specified and valid. */
22729 zero_width_ok_p = 1;
22730 width = (int)tem;
22731 }
22732 else if (prop = Fplist_get (plist, QCrelative_width),
22733 NUMVAL (prop) > 0)
22734 {
22735 /* Relative width `:relative-width FACTOR' specified and valid.
22736 Compute the width of the characters having the `glyph'
22737 property. */
22738 struct it it2;
22739 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22740
22741 it2 = *it;
22742 if (it->multibyte_p)
22743 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22744 else
22745 {
22746 it2.c = it2.char_to_display = *p, it2.len = 1;
22747 if (! ASCII_CHAR_P (it2.c))
22748 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22749 }
22750
22751 it2.glyph_row = NULL;
22752 it2.what = IT_CHARACTER;
22753 x_produce_glyphs (&it2);
22754 width = NUMVAL (prop) * it2.pixel_width;
22755 }
22756 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22757 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22758 {
22759 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22760 align_to = (align_to < 0
22761 ? 0
22762 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22763 else if (align_to < 0)
22764 align_to = window_box_left_offset (it->w, TEXT_AREA);
22765 width = max (0, (int)tem + align_to - it->current_x);
22766 zero_width_ok_p = 1;
22767 }
22768 else
22769 /* Nothing specified -> width defaults to canonical char width. */
22770 width = FRAME_COLUMN_WIDTH (it->f);
22771
22772 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22773 width = 1;
22774
22775 /* Compute height. */
22776 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22777 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22778 {
22779 height = (int)tem;
22780 zero_height_ok_p = 1;
22781 }
22782 else if (prop = Fplist_get (plist, QCrelative_height),
22783 NUMVAL (prop) > 0)
22784 height = FONT_HEIGHT (font) * NUMVAL (prop);
22785 else
22786 height = FONT_HEIGHT (font);
22787
22788 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22789 height = 1;
22790
22791 /* Compute percentage of height used for ascent. If
22792 `:ascent ASCENT' is present and valid, use that. Otherwise,
22793 derive the ascent from the font in use. */
22794 if (prop = Fplist_get (plist, QCascent),
22795 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22796 ascent = height * NUMVAL (prop) / 100.0;
22797 else if (!NILP (prop)
22798 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22799 ascent = min (max (0, (int)tem), height);
22800 else
22801 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22802
22803 if (width > 0 && it->line_wrap != TRUNCATE
22804 && it->current_x + width > it->last_visible_x)
22805 width = it->last_visible_x - it->current_x - 1;
22806
22807 if (width > 0 && height > 0 && it->glyph_row)
22808 {
22809 Lisp_Object object = it->stack[it->sp - 1].string;
22810 if (!STRINGP (object))
22811 object = it->w->buffer;
22812 append_stretch_glyph (it, object, width, height, ascent);
22813 }
22814
22815 it->pixel_width = width;
22816 it->ascent = it->phys_ascent = ascent;
22817 it->descent = it->phys_descent = height - it->ascent;
22818 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22819
22820 take_vertical_position_into_account (it);
22821 }
22822
22823 /* Calculate line-height and line-spacing properties.
22824 An integer value specifies explicit pixel value.
22825 A float value specifies relative value to current face height.
22826 A cons (float . face-name) specifies relative value to
22827 height of specified face font.
22828
22829 Returns height in pixels, or nil. */
22830
22831
22832 static Lisp_Object
22833 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22834 int boff, int override)
22835 {
22836 Lisp_Object face_name = Qnil;
22837 int ascent, descent, height;
22838
22839 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22840 return val;
22841
22842 if (CONSP (val))
22843 {
22844 face_name = XCAR (val);
22845 val = XCDR (val);
22846 if (!NUMBERP (val))
22847 val = make_number (1);
22848 if (NILP (face_name))
22849 {
22850 height = it->ascent + it->descent;
22851 goto scale;
22852 }
22853 }
22854
22855 if (NILP (face_name))
22856 {
22857 font = FRAME_FONT (it->f);
22858 boff = FRAME_BASELINE_OFFSET (it->f);
22859 }
22860 else if (EQ (face_name, Qt))
22861 {
22862 override = 0;
22863 }
22864 else
22865 {
22866 int face_id;
22867 struct face *face;
22868
22869 face_id = lookup_named_face (it->f, face_name, 0);
22870 if (face_id < 0)
22871 return make_number (-1);
22872
22873 face = FACE_FROM_ID (it->f, face_id);
22874 font = face->font;
22875 if (font == NULL)
22876 return make_number (-1);
22877 boff = font->baseline_offset;
22878 if (font->vertical_centering)
22879 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22880 }
22881
22882 ascent = FONT_BASE (font) + boff;
22883 descent = FONT_DESCENT (font) - boff;
22884
22885 if (override)
22886 {
22887 it->override_ascent = ascent;
22888 it->override_descent = descent;
22889 it->override_boff = boff;
22890 }
22891
22892 height = ascent + descent;
22893
22894 scale:
22895 if (FLOATP (val))
22896 height = (int)(XFLOAT_DATA (val) * height);
22897 else if (INTEGERP (val))
22898 height *= XINT (val);
22899
22900 return make_number (height);
22901 }
22902
22903
22904 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22905 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22906 and only if this is for a character for which no font was found.
22907
22908 If the display method (it->glyphless_method) is
22909 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22910 length of the acronym or the hexadecimal string, UPPER_XOFF and
22911 UPPER_YOFF are pixel offsets for the upper part of the string,
22912 LOWER_XOFF and LOWER_YOFF are for the lower part.
22913
22914 For the other display methods, LEN through LOWER_YOFF are zero. */
22915
22916 static void
22917 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22918 short upper_xoff, short upper_yoff,
22919 short lower_xoff, short lower_yoff)
22920 {
22921 struct glyph *glyph;
22922 enum glyph_row_area area = it->area;
22923
22924 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22925 if (glyph < it->glyph_row->glyphs[area + 1])
22926 {
22927 /* If the glyph row is reversed, we need to prepend the glyph
22928 rather than append it. */
22929 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22930 {
22931 struct glyph *g;
22932
22933 /* Make room for the additional glyph. */
22934 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22935 g[1] = *g;
22936 glyph = it->glyph_row->glyphs[area];
22937 }
22938 glyph->charpos = CHARPOS (it->position);
22939 glyph->object = it->object;
22940 glyph->pixel_width = it->pixel_width;
22941 glyph->ascent = it->ascent;
22942 glyph->descent = it->descent;
22943 glyph->voffset = it->voffset;
22944 glyph->type = GLYPHLESS_GLYPH;
22945 glyph->u.glyphless.method = it->glyphless_method;
22946 glyph->u.glyphless.for_no_font = for_no_font;
22947 glyph->u.glyphless.len = len;
22948 glyph->u.glyphless.ch = it->c;
22949 glyph->slice.glyphless.upper_xoff = upper_xoff;
22950 glyph->slice.glyphless.upper_yoff = upper_yoff;
22951 glyph->slice.glyphless.lower_xoff = lower_xoff;
22952 glyph->slice.glyphless.lower_yoff = lower_yoff;
22953 glyph->avoid_cursor_p = it->avoid_cursor_p;
22954 glyph->multibyte_p = it->multibyte_p;
22955 glyph->left_box_line_p = it->start_of_box_run_p;
22956 glyph->right_box_line_p = it->end_of_box_run_p;
22957 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22958 || it->phys_descent > it->descent);
22959 glyph->padding_p = 0;
22960 glyph->glyph_not_available_p = 0;
22961 glyph->face_id = face_id;
22962 glyph->font_type = FONT_TYPE_UNKNOWN;
22963 if (it->bidi_p)
22964 {
22965 glyph->resolved_level = it->bidi_it.resolved_level;
22966 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22967 abort ();
22968 glyph->bidi_type = it->bidi_it.type;
22969 }
22970 ++it->glyph_row->used[area];
22971 }
22972 else
22973 IT_EXPAND_MATRIX_WIDTH (it, area);
22974 }
22975
22976
22977 /* Produce a glyph for a glyphless character for iterator IT.
22978 IT->glyphless_method specifies which method to use for displaying
22979 the character. See the description of enum
22980 glyphless_display_method in dispextern.h for the detail.
22981
22982 FOR_NO_FONT is nonzero if and only if this is for a character for
22983 which no font was found. ACRONYM, if non-nil, is an acronym string
22984 for the character. */
22985
22986 static void
22987 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22988 {
22989 int face_id;
22990 struct face *face;
22991 struct font *font;
22992 int base_width, base_height, width, height;
22993 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22994 int len;
22995
22996 /* Get the metrics of the base font. We always refer to the current
22997 ASCII face. */
22998 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22999 font = face->font ? face->font : FRAME_FONT (it->f);
23000 it->ascent = FONT_BASE (font) + font->baseline_offset;
23001 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23002 base_height = it->ascent + it->descent;
23003 base_width = font->average_width;
23004
23005 /* Get a face ID for the glyph by utilizing a cache (the same way as
23006 doen for `escape-glyph' in get_next_display_element). */
23007 if (it->f == last_glyphless_glyph_frame
23008 && it->face_id == last_glyphless_glyph_face_id)
23009 {
23010 face_id = last_glyphless_glyph_merged_face_id;
23011 }
23012 else
23013 {
23014 /* Merge the `glyphless-char' face into the current face. */
23015 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23016 last_glyphless_glyph_frame = it->f;
23017 last_glyphless_glyph_face_id = it->face_id;
23018 last_glyphless_glyph_merged_face_id = face_id;
23019 }
23020
23021 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23022 {
23023 it->pixel_width = THIN_SPACE_WIDTH;
23024 len = 0;
23025 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23026 }
23027 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23028 {
23029 width = CHAR_WIDTH (it->c);
23030 if (width == 0)
23031 width = 1;
23032 else if (width > 4)
23033 width = 4;
23034 it->pixel_width = base_width * width;
23035 len = 0;
23036 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23037 }
23038 else
23039 {
23040 char buf[7];
23041 const char *str;
23042 unsigned int code[6];
23043 int upper_len;
23044 int ascent, descent;
23045 struct font_metrics metrics_upper, metrics_lower;
23046
23047 face = FACE_FROM_ID (it->f, face_id);
23048 font = face->font ? face->font : FRAME_FONT (it->f);
23049 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23050
23051 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23052 {
23053 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23054 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23055 if (CONSP (acronym))
23056 acronym = XCAR (acronym);
23057 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23058 }
23059 else
23060 {
23061 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23062 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23063 str = buf;
23064 }
23065 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23066 code[len] = font->driver->encode_char (font, str[len]);
23067 upper_len = (len + 1) / 2;
23068 font->driver->text_extents (font, code, upper_len,
23069 &metrics_upper);
23070 font->driver->text_extents (font, code + upper_len, len - upper_len,
23071 &metrics_lower);
23072
23073
23074
23075 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23076 width = max (metrics_upper.width, metrics_lower.width) + 4;
23077 upper_xoff = upper_yoff = 2; /* the typical case */
23078 if (base_width >= width)
23079 {
23080 /* Align the upper to the left, the lower to the right. */
23081 it->pixel_width = base_width;
23082 lower_xoff = base_width - 2 - metrics_lower.width;
23083 }
23084 else
23085 {
23086 /* Center the shorter one. */
23087 it->pixel_width = width;
23088 if (metrics_upper.width >= metrics_lower.width)
23089 lower_xoff = (width - metrics_lower.width) / 2;
23090 else
23091 {
23092 /* FIXME: This code doesn't look right. It formerly was
23093 missing the "lower_xoff = 0;", which couldn't have
23094 been right since it left lower_xoff uninitialized. */
23095 lower_xoff = 0;
23096 upper_xoff = (width - metrics_upper.width) / 2;
23097 }
23098 }
23099
23100 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23101 top, bottom, and between upper and lower strings. */
23102 height = (metrics_upper.ascent + metrics_upper.descent
23103 + metrics_lower.ascent + metrics_lower.descent) + 5;
23104 /* Center vertically.
23105 H:base_height, D:base_descent
23106 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23107
23108 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23109 descent = D - H/2 + h/2;
23110 lower_yoff = descent - 2 - ld;
23111 upper_yoff = lower_yoff - la - 1 - ud; */
23112 ascent = - (it->descent - (base_height + height + 1) / 2);
23113 descent = it->descent - (base_height - height) / 2;
23114 lower_yoff = descent - 2 - metrics_lower.descent;
23115 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23116 - metrics_upper.descent);
23117 /* Don't make the height shorter than the base height. */
23118 if (height > base_height)
23119 {
23120 it->ascent = ascent;
23121 it->descent = descent;
23122 }
23123 }
23124
23125 it->phys_ascent = it->ascent;
23126 it->phys_descent = it->descent;
23127 if (it->glyph_row)
23128 append_glyphless_glyph (it, face_id, for_no_font, len,
23129 upper_xoff, upper_yoff,
23130 lower_xoff, lower_yoff);
23131 it->nglyphs = 1;
23132 take_vertical_position_into_account (it);
23133 }
23134
23135
23136 /* RIF:
23137 Produce glyphs/get display metrics for the display element IT is
23138 loaded with. See the description of struct it in dispextern.h
23139 for an overview of struct it. */
23140
23141 void
23142 x_produce_glyphs (struct it *it)
23143 {
23144 int extra_line_spacing = it->extra_line_spacing;
23145
23146 it->glyph_not_available_p = 0;
23147
23148 if (it->what == IT_CHARACTER)
23149 {
23150 XChar2b char2b;
23151 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23152 struct font *font = face->font;
23153 struct font_metrics *pcm = NULL;
23154 int boff; /* baseline offset */
23155
23156 if (font == NULL)
23157 {
23158 /* When no suitable font is found, display this character by
23159 the method specified in the first extra slot of
23160 Vglyphless_char_display. */
23161 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23162
23163 xassert (it->what == IT_GLYPHLESS);
23164 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23165 goto done;
23166 }
23167
23168 boff = font->baseline_offset;
23169 if (font->vertical_centering)
23170 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23171
23172 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23173 {
23174 int stretched_p;
23175
23176 it->nglyphs = 1;
23177
23178 if (it->override_ascent >= 0)
23179 {
23180 it->ascent = it->override_ascent;
23181 it->descent = it->override_descent;
23182 boff = it->override_boff;
23183 }
23184 else
23185 {
23186 it->ascent = FONT_BASE (font) + boff;
23187 it->descent = FONT_DESCENT (font) - boff;
23188 }
23189
23190 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23191 {
23192 pcm = get_per_char_metric (font, &char2b);
23193 if (pcm->width == 0
23194 && pcm->rbearing == 0 && pcm->lbearing == 0)
23195 pcm = NULL;
23196 }
23197
23198 if (pcm)
23199 {
23200 it->phys_ascent = pcm->ascent + boff;
23201 it->phys_descent = pcm->descent - boff;
23202 it->pixel_width = pcm->width;
23203 }
23204 else
23205 {
23206 it->glyph_not_available_p = 1;
23207 it->phys_ascent = it->ascent;
23208 it->phys_descent = it->descent;
23209 it->pixel_width = font->space_width;
23210 }
23211
23212 if (it->constrain_row_ascent_descent_p)
23213 {
23214 if (it->descent > it->max_descent)
23215 {
23216 it->ascent += it->descent - it->max_descent;
23217 it->descent = it->max_descent;
23218 }
23219 if (it->ascent > it->max_ascent)
23220 {
23221 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23222 it->ascent = it->max_ascent;
23223 }
23224 it->phys_ascent = min (it->phys_ascent, it->ascent);
23225 it->phys_descent = min (it->phys_descent, it->descent);
23226 extra_line_spacing = 0;
23227 }
23228
23229 /* If this is a space inside a region of text with
23230 `space-width' property, change its width. */
23231 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23232 if (stretched_p)
23233 it->pixel_width *= XFLOATINT (it->space_width);
23234
23235 /* If face has a box, add the box thickness to the character
23236 height. If character has a box line to the left and/or
23237 right, add the box line width to the character's width. */
23238 if (face->box != FACE_NO_BOX)
23239 {
23240 int thick = face->box_line_width;
23241
23242 if (thick > 0)
23243 {
23244 it->ascent += thick;
23245 it->descent += thick;
23246 }
23247 else
23248 thick = -thick;
23249
23250 if (it->start_of_box_run_p)
23251 it->pixel_width += thick;
23252 if (it->end_of_box_run_p)
23253 it->pixel_width += thick;
23254 }
23255
23256 /* If face has an overline, add the height of the overline
23257 (1 pixel) and a 1 pixel margin to the character height. */
23258 if (face->overline_p)
23259 it->ascent += overline_margin;
23260
23261 if (it->constrain_row_ascent_descent_p)
23262 {
23263 if (it->ascent > it->max_ascent)
23264 it->ascent = it->max_ascent;
23265 if (it->descent > it->max_descent)
23266 it->descent = it->max_descent;
23267 }
23268
23269 take_vertical_position_into_account (it);
23270
23271 /* If we have to actually produce glyphs, do it. */
23272 if (it->glyph_row)
23273 {
23274 if (stretched_p)
23275 {
23276 /* Translate a space with a `space-width' property
23277 into a stretch glyph. */
23278 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23279 / FONT_HEIGHT (font));
23280 append_stretch_glyph (it, it->object, it->pixel_width,
23281 it->ascent + it->descent, ascent);
23282 }
23283 else
23284 append_glyph (it);
23285
23286 /* If characters with lbearing or rbearing are displayed
23287 in this line, record that fact in a flag of the
23288 glyph row. This is used to optimize X output code. */
23289 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23290 it->glyph_row->contains_overlapping_glyphs_p = 1;
23291 }
23292 if (! stretched_p && it->pixel_width == 0)
23293 /* We assure that all visible glyphs have at least 1-pixel
23294 width. */
23295 it->pixel_width = 1;
23296 }
23297 else if (it->char_to_display == '\n')
23298 {
23299 /* A newline has no width, but we need the height of the
23300 line. But if previous part of the line sets a height,
23301 don't increase that height */
23302
23303 Lisp_Object height;
23304 Lisp_Object total_height = Qnil;
23305
23306 it->override_ascent = -1;
23307 it->pixel_width = 0;
23308 it->nglyphs = 0;
23309
23310 height = get_it_property (it, Qline_height);
23311 /* Split (line-height total-height) list */
23312 if (CONSP (height)
23313 && CONSP (XCDR (height))
23314 && NILP (XCDR (XCDR (height))))
23315 {
23316 total_height = XCAR (XCDR (height));
23317 height = XCAR (height);
23318 }
23319 height = calc_line_height_property (it, height, font, boff, 1);
23320
23321 if (it->override_ascent >= 0)
23322 {
23323 it->ascent = it->override_ascent;
23324 it->descent = it->override_descent;
23325 boff = it->override_boff;
23326 }
23327 else
23328 {
23329 it->ascent = FONT_BASE (font) + boff;
23330 it->descent = FONT_DESCENT (font) - boff;
23331 }
23332
23333 if (EQ (height, Qt))
23334 {
23335 if (it->descent > it->max_descent)
23336 {
23337 it->ascent += it->descent - it->max_descent;
23338 it->descent = it->max_descent;
23339 }
23340 if (it->ascent > it->max_ascent)
23341 {
23342 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23343 it->ascent = it->max_ascent;
23344 }
23345 it->phys_ascent = min (it->phys_ascent, it->ascent);
23346 it->phys_descent = min (it->phys_descent, it->descent);
23347 it->constrain_row_ascent_descent_p = 1;
23348 extra_line_spacing = 0;
23349 }
23350 else
23351 {
23352 Lisp_Object spacing;
23353
23354 it->phys_ascent = it->ascent;
23355 it->phys_descent = it->descent;
23356
23357 if ((it->max_ascent > 0 || it->max_descent > 0)
23358 && face->box != FACE_NO_BOX
23359 && face->box_line_width > 0)
23360 {
23361 it->ascent += face->box_line_width;
23362 it->descent += face->box_line_width;
23363 }
23364 if (!NILP (height)
23365 && XINT (height) > it->ascent + it->descent)
23366 it->ascent = XINT (height) - it->descent;
23367
23368 if (!NILP (total_height))
23369 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23370 else
23371 {
23372 spacing = get_it_property (it, Qline_spacing);
23373 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23374 }
23375 if (INTEGERP (spacing))
23376 {
23377 extra_line_spacing = XINT (spacing);
23378 if (!NILP (total_height))
23379 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23380 }
23381 }
23382 }
23383 else /* i.e. (it->char_to_display == '\t') */
23384 {
23385 if (font->space_width > 0)
23386 {
23387 int tab_width = it->tab_width * font->space_width;
23388 int x = it->current_x + it->continuation_lines_width;
23389 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23390
23391 /* If the distance from the current position to the next tab
23392 stop is less than a space character width, use the
23393 tab stop after that. */
23394 if (next_tab_x - x < font->space_width)
23395 next_tab_x += tab_width;
23396
23397 it->pixel_width = next_tab_x - x;
23398 it->nglyphs = 1;
23399 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23400 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23401
23402 if (it->glyph_row)
23403 {
23404 append_stretch_glyph (it, it->object, it->pixel_width,
23405 it->ascent + it->descent, it->ascent);
23406 }
23407 }
23408 else
23409 {
23410 it->pixel_width = 0;
23411 it->nglyphs = 1;
23412 }
23413 }
23414 }
23415 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23416 {
23417 /* A static composition.
23418
23419 Note: A composition is represented as one glyph in the
23420 glyph matrix. There are no padding glyphs.
23421
23422 Important note: pixel_width, ascent, and descent are the
23423 values of what is drawn by draw_glyphs (i.e. the values of
23424 the overall glyphs composed). */
23425 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23426 int boff; /* baseline offset */
23427 struct composition *cmp = composition_table[it->cmp_it.id];
23428 int glyph_len = cmp->glyph_len;
23429 struct font *font = face->font;
23430
23431 it->nglyphs = 1;
23432
23433 /* If we have not yet calculated pixel size data of glyphs of
23434 the composition for the current face font, calculate them
23435 now. Theoretically, we have to check all fonts for the
23436 glyphs, but that requires much time and memory space. So,
23437 here we check only the font of the first glyph. This may
23438 lead to incorrect display, but it's very rare, and C-l
23439 (recenter-top-bottom) can correct the display anyway. */
23440 if (! cmp->font || cmp->font != font)
23441 {
23442 /* Ascent and descent of the font of the first character
23443 of this composition (adjusted by baseline offset).
23444 Ascent and descent of overall glyphs should not be less
23445 than these, respectively. */
23446 int font_ascent, font_descent, font_height;
23447 /* Bounding box of the overall glyphs. */
23448 int leftmost, rightmost, lowest, highest;
23449 int lbearing, rbearing;
23450 int i, width, ascent, descent;
23451 int left_padded = 0, right_padded = 0;
23452 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23453 XChar2b char2b;
23454 struct font_metrics *pcm;
23455 int font_not_found_p;
23456 EMACS_INT pos;
23457
23458 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23459 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23460 break;
23461 if (glyph_len < cmp->glyph_len)
23462 right_padded = 1;
23463 for (i = 0; i < glyph_len; i++)
23464 {
23465 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23466 break;
23467 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23468 }
23469 if (i > 0)
23470 left_padded = 1;
23471
23472 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23473 : IT_CHARPOS (*it));
23474 /* If no suitable font is found, use the default font. */
23475 font_not_found_p = font == NULL;
23476 if (font_not_found_p)
23477 {
23478 face = face->ascii_face;
23479 font = face->font;
23480 }
23481 boff = font->baseline_offset;
23482 if (font->vertical_centering)
23483 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23484 font_ascent = FONT_BASE (font) + boff;
23485 font_descent = FONT_DESCENT (font) - boff;
23486 font_height = FONT_HEIGHT (font);
23487
23488 cmp->font = (void *) font;
23489
23490 pcm = NULL;
23491 if (! font_not_found_p)
23492 {
23493 get_char_face_and_encoding (it->f, c, it->face_id,
23494 &char2b, 0);
23495 pcm = get_per_char_metric (font, &char2b);
23496 }
23497
23498 /* Initialize the bounding box. */
23499 if (pcm)
23500 {
23501 width = pcm->width;
23502 ascent = pcm->ascent;
23503 descent = pcm->descent;
23504 lbearing = pcm->lbearing;
23505 rbearing = pcm->rbearing;
23506 }
23507 else
23508 {
23509 width = font->space_width;
23510 ascent = FONT_BASE (font);
23511 descent = FONT_DESCENT (font);
23512 lbearing = 0;
23513 rbearing = width;
23514 }
23515
23516 rightmost = width;
23517 leftmost = 0;
23518 lowest = - descent + boff;
23519 highest = ascent + boff;
23520
23521 if (! font_not_found_p
23522 && font->default_ascent
23523 && CHAR_TABLE_P (Vuse_default_ascent)
23524 && !NILP (Faref (Vuse_default_ascent,
23525 make_number (it->char_to_display))))
23526 highest = font->default_ascent + boff;
23527
23528 /* Draw the first glyph at the normal position. It may be
23529 shifted to right later if some other glyphs are drawn
23530 at the left. */
23531 cmp->offsets[i * 2] = 0;
23532 cmp->offsets[i * 2 + 1] = boff;
23533 cmp->lbearing = lbearing;
23534 cmp->rbearing = rbearing;
23535
23536 /* Set cmp->offsets for the remaining glyphs. */
23537 for (i++; i < glyph_len; i++)
23538 {
23539 int left, right, btm, top;
23540 int ch = COMPOSITION_GLYPH (cmp, i);
23541 int face_id;
23542 struct face *this_face;
23543
23544 if (ch == '\t')
23545 ch = ' ';
23546 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23547 this_face = FACE_FROM_ID (it->f, face_id);
23548 font = this_face->font;
23549
23550 if (font == NULL)
23551 pcm = NULL;
23552 else
23553 {
23554 get_char_face_and_encoding (it->f, ch, face_id,
23555 &char2b, 0);
23556 pcm = get_per_char_metric (font, &char2b);
23557 }
23558 if (! pcm)
23559 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23560 else
23561 {
23562 width = pcm->width;
23563 ascent = pcm->ascent;
23564 descent = pcm->descent;
23565 lbearing = pcm->lbearing;
23566 rbearing = pcm->rbearing;
23567 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23568 {
23569 /* Relative composition with or without
23570 alternate chars. */
23571 left = (leftmost + rightmost - width) / 2;
23572 btm = - descent + boff;
23573 if (font->relative_compose
23574 && (! CHAR_TABLE_P (Vignore_relative_composition)
23575 || NILP (Faref (Vignore_relative_composition,
23576 make_number (ch)))))
23577 {
23578
23579 if (- descent >= font->relative_compose)
23580 /* One extra pixel between two glyphs. */
23581 btm = highest + 1;
23582 else if (ascent <= 0)
23583 /* One extra pixel between two glyphs. */
23584 btm = lowest - 1 - ascent - descent;
23585 }
23586 }
23587 else
23588 {
23589 /* A composition rule is specified by an integer
23590 value that encodes global and new reference
23591 points (GREF and NREF). GREF and NREF are
23592 specified by numbers as below:
23593
23594 0---1---2 -- ascent
23595 | |
23596 | |
23597 | |
23598 9--10--11 -- center
23599 | |
23600 ---3---4---5--- baseline
23601 | |
23602 6---7---8 -- descent
23603 */
23604 int rule = COMPOSITION_RULE (cmp, i);
23605 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23606
23607 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23608 grefx = gref % 3, nrefx = nref % 3;
23609 grefy = gref / 3, nrefy = nref / 3;
23610 if (xoff)
23611 xoff = font_height * (xoff - 128) / 256;
23612 if (yoff)
23613 yoff = font_height * (yoff - 128) / 256;
23614
23615 left = (leftmost
23616 + grefx * (rightmost - leftmost) / 2
23617 - nrefx * width / 2
23618 + xoff);
23619
23620 btm = ((grefy == 0 ? highest
23621 : grefy == 1 ? 0
23622 : grefy == 2 ? lowest
23623 : (highest + lowest) / 2)
23624 - (nrefy == 0 ? ascent + descent
23625 : nrefy == 1 ? descent - boff
23626 : nrefy == 2 ? 0
23627 : (ascent + descent) / 2)
23628 + yoff);
23629 }
23630
23631 cmp->offsets[i * 2] = left;
23632 cmp->offsets[i * 2 + 1] = btm + descent;
23633
23634 /* Update the bounding box of the overall glyphs. */
23635 if (width > 0)
23636 {
23637 right = left + width;
23638 if (left < leftmost)
23639 leftmost = left;
23640 if (right > rightmost)
23641 rightmost = right;
23642 }
23643 top = btm + descent + ascent;
23644 if (top > highest)
23645 highest = top;
23646 if (btm < lowest)
23647 lowest = btm;
23648
23649 if (cmp->lbearing > left + lbearing)
23650 cmp->lbearing = left + lbearing;
23651 if (cmp->rbearing < left + rbearing)
23652 cmp->rbearing = left + rbearing;
23653 }
23654 }
23655
23656 /* If there are glyphs whose x-offsets are negative,
23657 shift all glyphs to the right and make all x-offsets
23658 non-negative. */
23659 if (leftmost < 0)
23660 {
23661 for (i = 0; i < cmp->glyph_len; i++)
23662 cmp->offsets[i * 2] -= leftmost;
23663 rightmost -= leftmost;
23664 cmp->lbearing -= leftmost;
23665 cmp->rbearing -= leftmost;
23666 }
23667
23668 if (left_padded && cmp->lbearing < 0)
23669 {
23670 for (i = 0; i < cmp->glyph_len; i++)
23671 cmp->offsets[i * 2] -= cmp->lbearing;
23672 rightmost -= cmp->lbearing;
23673 cmp->rbearing -= cmp->lbearing;
23674 cmp->lbearing = 0;
23675 }
23676 if (right_padded && rightmost < cmp->rbearing)
23677 {
23678 rightmost = cmp->rbearing;
23679 }
23680
23681 cmp->pixel_width = rightmost;
23682 cmp->ascent = highest;
23683 cmp->descent = - lowest;
23684 if (cmp->ascent < font_ascent)
23685 cmp->ascent = font_ascent;
23686 if (cmp->descent < font_descent)
23687 cmp->descent = font_descent;
23688 }
23689
23690 if (it->glyph_row
23691 && (cmp->lbearing < 0
23692 || cmp->rbearing > cmp->pixel_width))
23693 it->glyph_row->contains_overlapping_glyphs_p = 1;
23694
23695 it->pixel_width = cmp->pixel_width;
23696 it->ascent = it->phys_ascent = cmp->ascent;
23697 it->descent = it->phys_descent = cmp->descent;
23698 if (face->box != FACE_NO_BOX)
23699 {
23700 int thick = face->box_line_width;
23701
23702 if (thick > 0)
23703 {
23704 it->ascent += thick;
23705 it->descent += thick;
23706 }
23707 else
23708 thick = - thick;
23709
23710 if (it->start_of_box_run_p)
23711 it->pixel_width += thick;
23712 if (it->end_of_box_run_p)
23713 it->pixel_width += thick;
23714 }
23715
23716 /* If face has an overline, add the height of the overline
23717 (1 pixel) and a 1 pixel margin to the character height. */
23718 if (face->overline_p)
23719 it->ascent += overline_margin;
23720
23721 take_vertical_position_into_account (it);
23722 if (it->ascent < 0)
23723 it->ascent = 0;
23724 if (it->descent < 0)
23725 it->descent = 0;
23726
23727 if (it->glyph_row)
23728 append_composite_glyph (it);
23729 }
23730 else if (it->what == IT_COMPOSITION)
23731 {
23732 /* A dynamic (automatic) composition. */
23733 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23734 Lisp_Object gstring;
23735 struct font_metrics metrics;
23736
23737 gstring = composition_gstring_from_id (it->cmp_it.id);
23738 it->pixel_width
23739 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23740 &metrics);
23741 if (it->glyph_row
23742 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23743 it->glyph_row->contains_overlapping_glyphs_p = 1;
23744 it->ascent = it->phys_ascent = metrics.ascent;
23745 it->descent = it->phys_descent = metrics.descent;
23746 if (face->box != FACE_NO_BOX)
23747 {
23748 int thick = face->box_line_width;
23749
23750 if (thick > 0)
23751 {
23752 it->ascent += thick;
23753 it->descent += thick;
23754 }
23755 else
23756 thick = - thick;
23757
23758 if (it->start_of_box_run_p)
23759 it->pixel_width += thick;
23760 if (it->end_of_box_run_p)
23761 it->pixel_width += thick;
23762 }
23763 /* If face has an overline, add the height of the overline
23764 (1 pixel) and a 1 pixel margin to the character height. */
23765 if (face->overline_p)
23766 it->ascent += overline_margin;
23767 take_vertical_position_into_account (it);
23768 if (it->ascent < 0)
23769 it->ascent = 0;
23770 if (it->descent < 0)
23771 it->descent = 0;
23772
23773 if (it->glyph_row)
23774 append_composite_glyph (it);
23775 }
23776 else if (it->what == IT_GLYPHLESS)
23777 produce_glyphless_glyph (it, 0, Qnil);
23778 else if (it->what == IT_IMAGE)
23779 produce_image_glyph (it);
23780 else if (it->what == IT_STRETCH)
23781 produce_stretch_glyph (it);
23782
23783 done:
23784 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23785 because this isn't true for images with `:ascent 100'. */
23786 xassert (it->ascent >= 0 && it->descent >= 0);
23787 if (it->area == TEXT_AREA)
23788 it->current_x += it->pixel_width;
23789
23790 if (extra_line_spacing > 0)
23791 {
23792 it->descent += extra_line_spacing;
23793 if (extra_line_spacing > it->max_extra_line_spacing)
23794 it->max_extra_line_spacing = extra_line_spacing;
23795 }
23796
23797 it->max_ascent = max (it->max_ascent, it->ascent);
23798 it->max_descent = max (it->max_descent, it->descent);
23799 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23800 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23801 }
23802
23803 /* EXPORT for RIF:
23804 Output LEN glyphs starting at START at the nominal cursor position.
23805 Advance the nominal cursor over the text. The global variable
23806 updated_window contains the window being updated, updated_row is
23807 the glyph row being updated, and updated_area is the area of that
23808 row being updated. */
23809
23810 void
23811 x_write_glyphs (struct glyph *start, int len)
23812 {
23813 int x, hpos;
23814
23815 xassert (updated_window && updated_row);
23816 BLOCK_INPUT;
23817
23818 /* Write glyphs. */
23819
23820 hpos = start - updated_row->glyphs[updated_area];
23821 x = draw_glyphs (updated_window, output_cursor.x,
23822 updated_row, updated_area,
23823 hpos, hpos + len,
23824 DRAW_NORMAL_TEXT, 0);
23825
23826 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23827 if (updated_area == TEXT_AREA
23828 && updated_window->phys_cursor_on_p
23829 && updated_window->phys_cursor.vpos == output_cursor.vpos
23830 && updated_window->phys_cursor.hpos >= hpos
23831 && updated_window->phys_cursor.hpos < hpos + len)
23832 updated_window->phys_cursor_on_p = 0;
23833
23834 UNBLOCK_INPUT;
23835
23836 /* Advance the output cursor. */
23837 output_cursor.hpos += len;
23838 output_cursor.x = x;
23839 }
23840
23841
23842 /* EXPORT for RIF:
23843 Insert LEN glyphs from START at the nominal cursor position. */
23844
23845 void
23846 x_insert_glyphs (struct glyph *start, int len)
23847 {
23848 struct frame *f;
23849 struct window *w;
23850 int line_height, shift_by_width, shifted_region_width;
23851 struct glyph_row *row;
23852 struct glyph *glyph;
23853 int frame_x, frame_y;
23854 EMACS_INT hpos;
23855
23856 xassert (updated_window && updated_row);
23857 BLOCK_INPUT;
23858 w = updated_window;
23859 f = XFRAME (WINDOW_FRAME (w));
23860
23861 /* Get the height of the line we are in. */
23862 row = updated_row;
23863 line_height = row->height;
23864
23865 /* Get the width of the glyphs to insert. */
23866 shift_by_width = 0;
23867 for (glyph = start; glyph < start + len; ++glyph)
23868 shift_by_width += glyph->pixel_width;
23869
23870 /* Get the width of the region to shift right. */
23871 shifted_region_width = (window_box_width (w, updated_area)
23872 - output_cursor.x
23873 - shift_by_width);
23874
23875 /* Shift right. */
23876 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23877 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23878
23879 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23880 line_height, shift_by_width);
23881
23882 /* Write the glyphs. */
23883 hpos = start - row->glyphs[updated_area];
23884 draw_glyphs (w, output_cursor.x, row, updated_area,
23885 hpos, hpos + len,
23886 DRAW_NORMAL_TEXT, 0);
23887
23888 /* Advance the output cursor. */
23889 output_cursor.hpos += len;
23890 output_cursor.x += shift_by_width;
23891 UNBLOCK_INPUT;
23892 }
23893
23894
23895 /* EXPORT for RIF:
23896 Erase the current text line from the nominal cursor position
23897 (inclusive) to pixel column TO_X (exclusive). The idea is that
23898 everything from TO_X onward is already erased.
23899
23900 TO_X is a pixel position relative to updated_area of
23901 updated_window. TO_X == -1 means clear to the end of this area. */
23902
23903 void
23904 x_clear_end_of_line (int to_x)
23905 {
23906 struct frame *f;
23907 struct window *w = updated_window;
23908 int max_x, min_y, max_y;
23909 int from_x, from_y, to_y;
23910
23911 xassert (updated_window && updated_row);
23912 f = XFRAME (w->frame);
23913
23914 if (updated_row->full_width_p)
23915 max_x = WINDOW_TOTAL_WIDTH (w);
23916 else
23917 max_x = window_box_width (w, updated_area);
23918 max_y = window_text_bottom_y (w);
23919
23920 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23921 of window. For TO_X > 0, truncate to end of drawing area. */
23922 if (to_x == 0)
23923 return;
23924 else if (to_x < 0)
23925 to_x = max_x;
23926 else
23927 to_x = min (to_x, max_x);
23928
23929 to_y = min (max_y, output_cursor.y + updated_row->height);
23930
23931 /* Notice if the cursor will be cleared by this operation. */
23932 if (!updated_row->full_width_p)
23933 notice_overwritten_cursor (w, updated_area,
23934 output_cursor.x, -1,
23935 updated_row->y,
23936 MATRIX_ROW_BOTTOM_Y (updated_row));
23937
23938 from_x = output_cursor.x;
23939
23940 /* Translate to frame coordinates. */
23941 if (updated_row->full_width_p)
23942 {
23943 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23944 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23945 }
23946 else
23947 {
23948 int area_left = window_box_left (w, updated_area);
23949 from_x += area_left;
23950 to_x += area_left;
23951 }
23952
23953 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23954 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23955 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23956
23957 /* Prevent inadvertently clearing to end of the X window. */
23958 if (to_x > from_x && to_y > from_y)
23959 {
23960 BLOCK_INPUT;
23961 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23962 to_x - from_x, to_y - from_y);
23963 UNBLOCK_INPUT;
23964 }
23965 }
23966
23967 #endif /* HAVE_WINDOW_SYSTEM */
23968
23969
23970 \f
23971 /***********************************************************************
23972 Cursor types
23973 ***********************************************************************/
23974
23975 /* Value is the internal representation of the specified cursor type
23976 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23977 of the bar cursor. */
23978
23979 static enum text_cursor_kinds
23980 get_specified_cursor_type (Lisp_Object arg, int *width)
23981 {
23982 enum text_cursor_kinds type;
23983
23984 if (NILP (arg))
23985 return NO_CURSOR;
23986
23987 if (EQ (arg, Qbox))
23988 return FILLED_BOX_CURSOR;
23989
23990 if (EQ (arg, Qhollow))
23991 return HOLLOW_BOX_CURSOR;
23992
23993 if (EQ (arg, Qbar))
23994 {
23995 *width = 2;
23996 return BAR_CURSOR;
23997 }
23998
23999 if (CONSP (arg)
24000 && EQ (XCAR (arg), Qbar)
24001 && INTEGERP (XCDR (arg))
24002 && XINT (XCDR (arg)) >= 0)
24003 {
24004 *width = XINT (XCDR (arg));
24005 return BAR_CURSOR;
24006 }
24007
24008 if (EQ (arg, Qhbar))
24009 {
24010 *width = 2;
24011 return HBAR_CURSOR;
24012 }
24013
24014 if (CONSP (arg)
24015 && EQ (XCAR (arg), Qhbar)
24016 && INTEGERP (XCDR (arg))
24017 && XINT (XCDR (arg)) >= 0)
24018 {
24019 *width = XINT (XCDR (arg));
24020 return HBAR_CURSOR;
24021 }
24022
24023 /* Treat anything unknown as "hollow box cursor".
24024 It was bad to signal an error; people have trouble fixing
24025 .Xdefaults with Emacs, when it has something bad in it. */
24026 type = HOLLOW_BOX_CURSOR;
24027
24028 return type;
24029 }
24030
24031 /* Set the default cursor types for specified frame. */
24032 void
24033 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24034 {
24035 int width = 1;
24036 Lisp_Object tem;
24037
24038 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24039 FRAME_CURSOR_WIDTH (f) = width;
24040
24041 /* By default, set up the blink-off state depending on the on-state. */
24042
24043 tem = Fassoc (arg, Vblink_cursor_alist);
24044 if (!NILP (tem))
24045 {
24046 FRAME_BLINK_OFF_CURSOR (f)
24047 = get_specified_cursor_type (XCDR (tem), &width);
24048 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24049 }
24050 else
24051 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24052 }
24053
24054
24055 #ifdef HAVE_WINDOW_SYSTEM
24056
24057 /* Return the cursor we want to be displayed in window W. Return
24058 width of bar/hbar cursor through WIDTH arg. Return with
24059 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24060 (i.e. if the `system caret' should track this cursor).
24061
24062 In a mini-buffer window, we want the cursor only to appear if we
24063 are reading input from this window. For the selected window, we
24064 want the cursor type given by the frame parameter or buffer local
24065 setting of cursor-type. If explicitly marked off, draw no cursor.
24066 In all other cases, we want a hollow box cursor. */
24067
24068 static enum text_cursor_kinds
24069 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24070 int *active_cursor)
24071 {
24072 struct frame *f = XFRAME (w->frame);
24073 struct buffer *b = XBUFFER (w->buffer);
24074 int cursor_type = DEFAULT_CURSOR;
24075 Lisp_Object alt_cursor;
24076 int non_selected = 0;
24077
24078 *active_cursor = 1;
24079
24080 /* Echo area */
24081 if (cursor_in_echo_area
24082 && FRAME_HAS_MINIBUF_P (f)
24083 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24084 {
24085 if (w == XWINDOW (echo_area_window))
24086 {
24087 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24088 {
24089 *width = FRAME_CURSOR_WIDTH (f);
24090 return FRAME_DESIRED_CURSOR (f);
24091 }
24092 else
24093 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24094 }
24095
24096 *active_cursor = 0;
24097 non_selected = 1;
24098 }
24099
24100 /* Detect a nonselected window or nonselected frame. */
24101 else if (w != XWINDOW (f->selected_window)
24102 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24103 {
24104 *active_cursor = 0;
24105
24106 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24107 return NO_CURSOR;
24108
24109 non_selected = 1;
24110 }
24111
24112 /* Never display a cursor in a window in which cursor-type is nil. */
24113 if (NILP (BVAR (b, cursor_type)))
24114 return NO_CURSOR;
24115
24116 /* Get the normal cursor type for this window. */
24117 if (EQ (BVAR (b, cursor_type), Qt))
24118 {
24119 cursor_type = FRAME_DESIRED_CURSOR (f);
24120 *width = FRAME_CURSOR_WIDTH (f);
24121 }
24122 else
24123 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24124
24125 /* Use cursor-in-non-selected-windows instead
24126 for non-selected window or frame. */
24127 if (non_selected)
24128 {
24129 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24130 if (!EQ (Qt, alt_cursor))
24131 return get_specified_cursor_type (alt_cursor, width);
24132 /* t means modify the normal cursor type. */
24133 if (cursor_type == FILLED_BOX_CURSOR)
24134 cursor_type = HOLLOW_BOX_CURSOR;
24135 else if (cursor_type == BAR_CURSOR && *width > 1)
24136 --*width;
24137 return cursor_type;
24138 }
24139
24140 /* Use normal cursor if not blinked off. */
24141 if (!w->cursor_off_p)
24142 {
24143 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24144 {
24145 if (cursor_type == FILLED_BOX_CURSOR)
24146 {
24147 /* Using a block cursor on large images can be very annoying.
24148 So use a hollow cursor for "large" images.
24149 If image is not transparent (no mask), also use hollow cursor. */
24150 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24151 if (img != NULL && IMAGEP (img->spec))
24152 {
24153 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24154 where N = size of default frame font size.
24155 This should cover most of the "tiny" icons people may use. */
24156 if (!img->mask
24157 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24158 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24159 cursor_type = HOLLOW_BOX_CURSOR;
24160 }
24161 }
24162 else if (cursor_type != NO_CURSOR)
24163 {
24164 /* Display current only supports BOX and HOLLOW cursors for images.
24165 So for now, unconditionally use a HOLLOW cursor when cursor is
24166 not a solid box cursor. */
24167 cursor_type = HOLLOW_BOX_CURSOR;
24168 }
24169 }
24170 return cursor_type;
24171 }
24172
24173 /* Cursor is blinked off, so determine how to "toggle" it. */
24174
24175 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24176 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24177 return get_specified_cursor_type (XCDR (alt_cursor), width);
24178
24179 /* Then see if frame has specified a specific blink off cursor type. */
24180 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24181 {
24182 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24183 return FRAME_BLINK_OFF_CURSOR (f);
24184 }
24185
24186 #if 0
24187 /* Some people liked having a permanently visible blinking cursor,
24188 while others had very strong opinions against it. So it was
24189 decided to remove it. KFS 2003-09-03 */
24190
24191 /* Finally perform built-in cursor blinking:
24192 filled box <-> hollow box
24193 wide [h]bar <-> narrow [h]bar
24194 narrow [h]bar <-> no cursor
24195 other type <-> no cursor */
24196
24197 if (cursor_type == FILLED_BOX_CURSOR)
24198 return HOLLOW_BOX_CURSOR;
24199
24200 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24201 {
24202 *width = 1;
24203 return cursor_type;
24204 }
24205 #endif
24206
24207 return NO_CURSOR;
24208 }
24209
24210
24211 /* Notice when the text cursor of window W has been completely
24212 overwritten by a drawing operation that outputs glyphs in AREA
24213 starting at X0 and ending at X1 in the line starting at Y0 and
24214 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24215 the rest of the line after X0 has been written. Y coordinates
24216 are window-relative. */
24217
24218 static void
24219 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24220 int x0, int x1, int y0, int y1)
24221 {
24222 int cx0, cx1, cy0, cy1;
24223 struct glyph_row *row;
24224
24225 if (!w->phys_cursor_on_p)
24226 return;
24227 if (area != TEXT_AREA)
24228 return;
24229
24230 if (w->phys_cursor.vpos < 0
24231 || w->phys_cursor.vpos >= w->current_matrix->nrows
24232 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24233 !(row->enabled_p && row->displays_text_p)))
24234 return;
24235
24236 if (row->cursor_in_fringe_p)
24237 {
24238 row->cursor_in_fringe_p = 0;
24239 draw_fringe_bitmap (w, row, row->reversed_p);
24240 w->phys_cursor_on_p = 0;
24241 return;
24242 }
24243
24244 cx0 = w->phys_cursor.x;
24245 cx1 = cx0 + w->phys_cursor_width;
24246 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24247 return;
24248
24249 /* The cursor image will be completely removed from the
24250 screen if the output area intersects the cursor area in
24251 y-direction. When we draw in [y0 y1[, and some part of
24252 the cursor is at y < y0, that part must have been drawn
24253 before. When scrolling, the cursor is erased before
24254 actually scrolling, so we don't come here. When not
24255 scrolling, the rows above the old cursor row must have
24256 changed, and in this case these rows must have written
24257 over the cursor image.
24258
24259 Likewise if part of the cursor is below y1, with the
24260 exception of the cursor being in the first blank row at
24261 the buffer and window end because update_text_area
24262 doesn't draw that row. (Except when it does, but
24263 that's handled in update_text_area.) */
24264
24265 cy0 = w->phys_cursor.y;
24266 cy1 = cy0 + w->phys_cursor_height;
24267 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24268 return;
24269
24270 w->phys_cursor_on_p = 0;
24271 }
24272
24273 #endif /* HAVE_WINDOW_SYSTEM */
24274
24275 \f
24276 /************************************************************************
24277 Mouse Face
24278 ************************************************************************/
24279
24280 #ifdef HAVE_WINDOW_SYSTEM
24281
24282 /* EXPORT for RIF:
24283 Fix the display of area AREA of overlapping row ROW in window W
24284 with respect to the overlapping part OVERLAPS. */
24285
24286 void
24287 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24288 enum glyph_row_area area, int overlaps)
24289 {
24290 int i, x;
24291
24292 BLOCK_INPUT;
24293
24294 x = 0;
24295 for (i = 0; i < row->used[area];)
24296 {
24297 if (row->glyphs[area][i].overlaps_vertically_p)
24298 {
24299 int start = i, start_x = x;
24300
24301 do
24302 {
24303 x += row->glyphs[area][i].pixel_width;
24304 ++i;
24305 }
24306 while (i < row->used[area]
24307 && row->glyphs[area][i].overlaps_vertically_p);
24308
24309 draw_glyphs (w, start_x, row, area,
24310 start, i,
24311 DRAW_NORMAL_TEXT, overlaps);
24312 }
24313 else
24314 {
24315 x += row->glyphs[area][i].pixel_width;
24316 ++i;
24317 }
24318 }
24319
24320 UNBLOCK_INPUT;
24321 }
24322
24323
24324 /* EXPORT:
24325 Draw the cursor glyph of window W in glyph row ROW. See the
24326 comment of draw_glyphs for the meaning of HL. */
24327
24328 void
24329 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24330 enum draw_glyphs_face hl)
24331 {
24332 /* If cursor hpos is out of bounds, don't draw garbage. This can
24333 happen in mini-buffer windows when switching between echo area
24334 glyphs and mini-buffer. */
24335 if ((row->reversed_p
24336 ? (w->phys_cursor.hpos >= 0)
24337 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24338 {
24339 int on_p = w->phys_cursor_on_p;
24340 int x1;
24341 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24342 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24343 hl, 0);
24344 w->phys_cursor_on_p = on_p;
24345
24346 if (hl == DRAW_CURSOR)
24347 w->phys_cursor_width = x1 - w->phys_cursor.x;
24348 /* When we erase the cursor, and ROW is overlapped by other
24349 rows, make sure that these overlapping parts of other rows
24350 are redrawn. */
24351 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24352 {
24353 w->phys_cursor_width = x1 - w->phys_cursor.x;
24354
24355 if (row > w->current_matrix->rows
24356 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24357 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24358 OVERLAPS_ERASED_CURSOR);
24359
24360 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24361 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24362 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24363 OVERLAPS_ERASED_CURSOR);
24364 }
24365 }
24366 }
24367
24368
24369 /* EXPORT:
24370 Erase the image of a cursor of window W from the screen. */
24371
24372 void
24373 erase_phys_cursor (struct window *w)
24374 {
24375 struct frame *f = XFRAME (w->frame);
24376 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24377 int hpos = w->phys_cursor.hpos;
24378 int vpos = w->phys_cursor.vpos;
24379 int mouse_face_here_p = 0;
24380 struct glyph_matrix *active_glyphs = w->current_matrix;
24381 struct glyph_row *cursor_row;
24382 struct glyph *cursor_glyph;
24383 enum draw_glyphs_face hl;
24384
24385 /* No cursor displayed or row invalidated => nothing to do on the
24386 screen. */
24387 if (w->phys_cursor_type == NO_CURSOR)
24388 goto mark_cursor_off;
24389
24390 /* VPOS >= active_glyphs->nrows means that window has been resized.
24391 Don't bother to erase the cursor. */
24392 if (vpos >= active_glyphs->nrows)
24393 goto mark_cursor_off;
24394
24395 /* If row containing cursor is marked invalid, there is nothing we
24396 can do. */
24397 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24398 if (!cursor_row->enabled_p)
24399 goto mark_cursor_off;
24400
24401 /* If line spacing is > 0, old cursor may only be partially visible in
24402 window after split-window. So adjust visible height. */
24403 cursor_row->visible_height = min (cursor_row->visible_height,
24404 window_text_bottom_y (w) - cursor_row->y);
24405
24406 /* If row is completely invisible, don't attempt to delete a cursor which
24407 isn't there. This can happen if cursor is at top of a window, and
24408 we switch to a buffer with a header line in that window. */
24409 if (cursor_row->visible_height <= 0)
24410 goto mark_cursor_off;
24411
24412 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24413 if (cursor_row->cursor_in_fringe_p)
24414 {
24415 cursor_row->cursor_in_fringe_p = 0;
24416 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24417 goto mark_cursor_off;
24418 }
24419
24420 /* This can happen when the new row is shorter than the old one.
24421 In this case, either draw_glyphs or clear_end_of_line
24422 should have cleared the cursor. Note that we wouldn't be
24423 able to erase the cursor in this case because we don't have a
24424 cursor glyph at hand. */
24425 if ((cursor_row->reversed_p
24426 ? (w->phys_cursor.hpos < 0)
24427 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24428 goto mark_cursor_off;
24429
24430 /* If the cursor is in the mouse face area, redisplay that when
24431 we clear the cursor. */
24432 if (! NILP (hlinfo->mouse_face_window)
24433 && coords_in_mouse_face_p (w, hpos, vpos)
24434 /* Don't redraw the cursor's spot in mouse face if it is at the
24435 end of a line (on a newline). The cursor appears there, but
24436 mouse highlighting does not. */
24437 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24438 mouse_face_here_p = 1;
24439
24440 /* Maybe clear the display under the cursor. */
24441 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24442 {
24443 int x, y, left_x;
24444 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24445 int width;
24446
24447 cursor_glyph = get_phys_cursor_glyph (w);
24448 if (cursor_glyph == NULL)
24449 goto mark_cursor_off;
24450
24451 width = cursor_glyph->pixel_width;
24452 left_x = window_box_left_offset (w, TEXT_AREA);
24453 x = w->phys_cursor.x;
24454 if (x < left_x)
24455 width -= left_x - x;
24456 width = min (width, window_box_width (w, TEXT_AREA) - x);
24457 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24458 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24459
24460 if (width > 0)
24461 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24462 }
24463
24464 /* Erase the cursor by redrawing the character underneath it. */
24465 if (mouse_face_here_p)
24466 hl = DRAW_MOUSE_FACE;
24467 else
24468 hl = DRAW_NORMAL_TEXT;
24469 draw_phys_cursor_glyph (w, cursor_row, hl);
24470
24471 mark_cursor_off:
24472 w->phys_cursor_on_p = 0;
24473 w->phys_cursor_type = NO_CURSOR;
24474 }
24475
24476
24477 /* EXPORT:
24478 Display or clear cursor of window W. If ON is zero, clear the
24479 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24480 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24481
24482 void
24483 display_and_set_cursor (struct window *w, int on,
24484 int hpos, int vpos, int x, int y)
24485 {
24486 struct frame *f = XFRAME (w->frame);
24487 int new_cursor_type;
24488 int new_cursor_width;
24489 int active_cursor;
24490 struct glyph_row *glyph_row;
24491 struct glyph *glyph;
24492
24493 /* This is pointless on invisible frames, and dangerous on garbaged
24494 windows and frames; in the latter case, the frame or window may
24495 be in the midst of changing its size, and x and y may be off the
24496 window. */
24497 if (! FRAME_VISIBLE_P (f)
24498 || FRAME_GARBAGED_P (f)
24499 || vpos >= w->current_matrix->nrows
24500 || hpos >= w->current_matrix->matrix_w)
24501 return;
24502
24503 /* If cursor is off and we want it off, return quickly. */
24504 if (!on && !w->phys_cursor_on_p)
24505 return;
24506
24507 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24508 /* If cursor row is not enabled, we don't really know where to
24509 display the cursor. */
24510 if (!glyph_row->enabled_p)
24511 {
24512 w->phys_cursor_on_p = 0;
24513 return;
24514 }
24515
24516 glyph = NULL;
24517 if (!glyph_row->exact_window_width_line_p
24518 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24519 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24520
24521 xassert (interrupt_input_blocked);
24522
24523 /* Set new_cursor_type to the cursor we want to be displayed. */
24524 new_cursor_type = get_window_cursor_type (w, glyph,
24525 &new_cursor_width, &active_cursor);
24526
24527 /* If cursor is currently being shown and we don't want it to be or
24528 it is in the wrong place, or the cursor type is not what we want,
24529 erase it. */
24530 if (w->phys_cursor_on_p
24531 && (!on
24532 || w->phys_cursor.x != x
24533 || w->phys_cursor.y != y
24534 || new_cursor_type != w->phys_cursor_type
24535 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24536 && new_cursor_width != w->phys_cursor_width)))
24537 erase_phys_cursor (w);
24538
24539 /* Don't check phys_cursor_on_p here because that flag is only set
24540 to zero in some cases where we know that the cursor has been
24541 completely erased, to avoid the extra work of erasing the cursor
24542 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24543 still not be visible, or it has only been partly erased. */
24544 if (on)
24545 {
24546 w->phys_cursor_ascent = glyph_row->ascent;
24547 w->phys_cursor_height = glyph_row->height;
24548
24549 /* Set phys_cursor_.* before x_draw_.* is called because some
24550 of them may need the information. */
24551 w->phys_cursor.x = x;
24552 w->phys_cursor.y = glyph_row->y;
24553 w->phys_cursor.hpos = hpos;
24554 w->phys_cursor.vpos = vpos;
24555 }
24556
24557 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24558 new_cursor_type, new_cursor_width,
24559 on, active_cursor);
24560 }
24561
24562
24563 /* Switch the display of W's cursor on or off, according to the value
24564 of ON. */
24565
24566 static void
24567 update_window_cursor (struct window *w, int on)
24568 {
24569 /* Don't update cursor in windows whose frame is in the process
24570 of being deleted. */
24571 if (w->current_matrix)
24572 {
24573 BLOCK_INPUT;
24574 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24575 w->phys_cursor.x, w->phys_cursor.y);
24576 UNBLOCK_INPUT;
24577 }
24578 }
24579
24580
24581 /* Call update_window_cursor with parameter ON_P on all leaf windows
24582 in the window tree rooted at W. */
24583
24584 static void
24585 update_cursor_in_window_tree (struct window *w, int on_p)
24586 {
24587 while (w)
24588 {
24589 if (!NILP (w->hchild))
24590 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24591 else if (!NILP (w->vchild))
24592 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24593 else
24594 update_window_cursor (w, on_p);
24595
24596 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24597 }
24598 }
24599
24600
24601 /* EXPORT:
24602 Display the cursor on window W, or clear it, according to ON_P.
24603 Don't change the cursor's position. */
24604
24605 void
24606 x_update_cursor (struct frame *f, int on_p)
24607 {
24608 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24609 }
24610
24611
24612 /* EXPORT:
24613 Clear the cursor of window W to background color, and mark the
24614 cursor as not shown. This is used when the text where the cursor
24615 is about to be rewritten. */
24616
24617 void
24618 x_clear_cursor (struct window *w)
24619 {
24620 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24621 update_window_cursor (w, 0);
24622 }
24623
24624 #endif /* HAVE_WINDOW_SYSTEM */
24625
24626 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24627 and MSDOS. */
24628 static void
24629 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24630 int start_hpos, int end_hpos,
24631 enum draw_glyphs_face draw)
24632 {
24633 #ifdef HAVE_WINDOW_SYSTEM
24634 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24635 {
24636 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24637 return;
24638 }
24639 #endif
24640 #if defined (HAVE_GPM) || defined (MSDOS)
24641 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24642 #endif
24643 }
24644
24645 /* Display the active region described by mouse_face_* according to DRAW. */
24646
24647 static void
24648 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24649 {
24650 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24651 struct frame *f = XFRAME (WINDOW_FRAME (w));
24652
24653 if (/* If window is in the process of being destroyed, don't bother
24654 to do anything. */
24655 w->current_matrix != NULL
24656 /* Don't update mouse highlight if hidden */
24657 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24658 /* Recognize when we are called to operate on rows that don't exist
24659 anymore. This can happen when a window is split. */
24660 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24661 {
24662 int phys_cursor_on_p = w->phys_cursor_on_p;
24663 struct glyph_row *row, *first, *last;
24664
24665 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24666 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24667
24668 for (row = first; row <= last && row->enabled_p; ++row)
24669 {
24670 int start_hpos, end_hpos, start_x;
24671
24672 /* For all but the first row, the highlight starts at column 0. */
24673 if (row == first)
24674 {
24675 /* R2L rows have BEG and END in reversed order, but the
24676 screen drawing geometry is always left to right. So
24677 we need to mirror the beginning and end of the
24678 highlighted area in R2L rows. */
24679 if (!row->reversed_p)
24680 {
24681 start_hpos = hlinfo->mouse_face_beg_col;
24682 start_x = hlinfo->mouse_face_beg_x;
24683 }
24684 else if (row == last)
24685 {
24686 start_hpos = hlinfo->mouse_face_end_col;
24687 start_x = hlinfo->mouse_face_end_x;
24688 }
24689 else
24690 {
24691 start_hpos = 0;
24692 start_x = 0;
24693 }
24694 }
24695 else if (row->reversed_p && row == last)
24696 {
24697 start_hpos = hlinfo->mouse_face_end_col;
24698 start_x = hlinfo->mouse_face_end_x;
24699 }
24700 else
24701 {
24702 start_hpos = 0;
24703 start_x = 0;
24704 }
24705
24706 if (row == last)
24707 {
24708 if (!row->reversed_p)
24709 end_hpos = hlinfo->mouse_face_end_col;
24710 else if (row == first)
24711 end_hpos = hlinfo->mouse_face_beg_col;
24712 else
24713 {
24714 end_hpos = row->used[TEXT_AREA];
24715 if (draw == DRAW_NORMAL_TEXT)
24716 row->fill_line_p = 1; /* Clear to end of line */
24717 }
24718 }
24719 else if (row->reversed_p && row == first)
24720 end_hpos = hlinfo->mouse_face_beg_col;
24721 else
24722 {
24723 end_hpos = row->used[TEXT_AREA];
24724 if (draw == DRAW_NORMAL_TEXT)
24725 row->fill_line_p = 1; /* Clear to end of line */
24726 }
24727
24728 if (end_hpos > start_hpos)
24729 {
24730 draw_row_with_mouse_face (w, start_x, row,
24731 start_hpos, end_hpos, draw);
24732
24733 row->mouse_face_p
24734 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24735 }
24736 }
24737
24738 #ifdef HAVE_WINDOW_SYSTEM
24739 /* When we've written over the cursor, arrange for it to
24740 be displayed again. */
24741 if (FRAME_WINDOW_P (f)
24742 && phys_cursor_on_p && !w->phys_cursor_on_p)
24743 {
24744 BLOCK_INPUT;
24745 display_and_set_cursor (w, 1,
24746 w->phys_cursor.hpos, w->phys_cursor.vpos,
24747 w->phys_cursor.x, w->phys_cursor.y);
24748 UNBLOCK_INPUT;
24749 }
24750 #endif /* HAVE_WINDOW_SYSTEM */
24751 }
24752
24753 #ifdef HAVE_WINDOW_SYSTEM
24754 /* Change the mouse cursor. */
24755 if (FRAME_WINDOW_P (f))
24756 {
24757 if (draw == DRAW_NORMAL_TEXT
24758 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24759 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24760 else if (draw == DRAW_MOUSE_FACE)
24761 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24762 else
24763 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24764 }
24765 #endif /* HAVE_WINDOW_SYSTEM */
24766 }
24767
24768 /* EXPORT:
24769 Clear out the mouse-highlighted active region.
24770 Redraw it un-highlighted first. Value is non-zero if mouse
24771 face was actually drawn unhighlighted. */
24772
24773 int
24774 clear_mouse_face (Mouse_HLInfo *hlinfo)
24775 {
24776 int cleared = 0;
24777
24778 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24779 {
24780 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24781 cleared = 1;
24782 }
24783
24784 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24785 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24786 hlinfo->mouse_face_window = Qnil;
24787 hlinfo->mouse_face_overlay = Qnil;
24788 return cleared;
24789 }
24790
24791 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24792 within the mouse face on that window. */
24793 static int
24794 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24795 {
24796 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24797
24798 /* Quickly resolve the easy cases. */
24799 if (!(WINDOWP (hlinfo->mouse_face_window)
24800 && XWINDOW (hlinfo->mouse_face_window) == w))
24801 return 0;
24802 if (vpos < hlinfo->mouse_face_beg_row
24803 || vpos > hlinfo->mouse_face_end_row)
24804 return 0;
24805 if (vpos > hlinfo->mouse_face_beg_row
24806 && vpos < hlinfo->mouse_face_end_row)
24807 return 1;
24808
24809 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24810 {
24811 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24812 {
24813 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24814 return 1;
24815 }
24816 else if ((vpos == hlinfo->mouse_face_beg_row
24817 && hpos >= hlinfo->mouse_face_beg_col)
24818 || (vpos == hlinfo->mouse_face_end_row
24819 && hpos < hlinfo->mouse_face_end_col))
24820 return 1;
24821 }
24822 else
24823 {
24824 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24825 {
24826 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24827 return 1;
24828 }
24829 else if ((vpos == hlinfo->mouse_face_beg_row
24830 && hpos <= hlinfo->mouse_face_beg_col)
24831 || (vpos == hlinfo->mouse_face_end_row
24832 && hpos > hlinfo->mouse_face_end_col))
24833 return 1;
24834 }
24835 return 0;
24836 }
24837
24838
24839 /* EXPORT:
24840 Non-zero if physical cursor of window W is within mouse face. */
24841
24842 int
24843 cursor_in_mouse_face_p (struct window *w)
24844 {
24845 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24846 }
24847
24848
24849 \f
24850 /* Find the glyph rows START_ROW and END_ROW of window W that display
24851 characters between buffer positions START_CHARPOS and END_CHARPOS
24852 (excluding END_CHARPOS). This is similar to row_containing_pos,
24853 but is more accurate when bidi reordering makes buffer positions
24854 change non-linearly with glyph rows. */
24855 static void
24856 rows_from_pos_range (struct window *w,
24857 EMACS_INT start_charpos, EMACS_INT end_charpos,
24858 struct glyph_row **start, struct glyph_row **end)
24859 {
24860 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24861 int last_y = window_text_bottom_y (w);
24862 struct glyph_row *row;
24863
24864 *start = NULL;
24865 *end = NULL;
24866
24867 while (!first->enabled_p
24868 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24869 first++;
24870
24871 /* Find the START row. */
24872 for (row = first;
24873 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24874 row++)
24875 {
24876 /* A row can potentially be the START row if the range of the
24877 characters it displays intersects the range
24878 [START_CHARPOS..END_CHARPOS). */
24879 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24880 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24881 /* See the commentary in row_containing_pos, for the
24882 explanation of the complicated way to check whether
24883 some position is beyond the end of the characters
24884 displayed by a row. */
24885 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24886 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24887 && !row->ends_at_zv_p
24888 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24889 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24890 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24891 && !row->ends_at_zv_p
24892 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24893 {
24894 /* Found a candidate row. Now make sure at least one of the
24895 glyphs it displays has a charpos from the range
24896 [START_CHARPOS..END_CHARPOS).
24897
24898 This is not obvious because bidi reordering could make
24899 buffer positions of a row be 1,2,3,102,101,100, and if we
24900 want to highlight characters in [50..60), we don't want
24901 this row, even though [50..60) does intersect [1..103),
24902 the range of character positions given by the row's start
24903 and end positions. */
24904 struct glyph *g = row->glyphs[TEXT_AREA];
24905 struct glyph *e = g + row->used[TEXT_AREA];
24906
24907 while (g < e)
24908 {
24909 if (BUFFERP (g->object)
24910 && start_charpos <= g->charpos && g->charpos < end_charpos)
24911 *start = row;
24912 g++;
24913 }
24914 if (*start)
24915 break;
24916 }
24917 }
24918
24919 /* Find the END row. */
24920 if (!*start
24921 /* If the last row is partially visible, start looking for END
24922 from that row, instead of starting from FIRST. */
24923 && !(row->enabled_p
24924 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24925 row = first;
24926 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24927 {
24928 struct glyph_row *next = row + 1;
24929
24930 if (!next->enabled_p
24931 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24932 /* The first row >= START whose range of displayed characters
24933 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24934 is the row END + 1. */
24935 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24936 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24937 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24938 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24939 && !next->ends_at_zv_p
24940 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24941 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24942 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24943 && !next->ends_at_zv_p
24944 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24945 {
24946 *end = row;
24947 break;
24948 }
24949 else
24950 {
24951 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24952 but none of the characters it displays are in the range, it is
24953 also END + 1. */
24954 struct glyph *g = next->glyphs[TEXT_AREA];
24955 struct glyph *e = g + next->used[TEXT_AREA];
24956
24957 while (g < e)
24958 {
24959 if (BUFFERP (g->object)
24960 && start_charpos <= g->charpos && g->charpos < end_charpos)
24961 break;
24962 g++;
24963 }
24964 if (g == e)
24965 {
24966 *end = row;
24967 break;
24968 }
24969 }
24970 }
24971 }
24972
24973 /* This function sets the mouse_face_* elements of HLINFO, assuming
24974 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24975 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24976 for the overlay or run of text properties specifying the mouse
24977 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24978 before-string and after-string that must also be highlighted.
24979 COVER_STRING, if non-nil, is a display string that may cover some
24980 or all of the highlighted text. */
24981
24982 static void
24983 mouse_face_from_buffer_pos (Lisp_Object window,
24984 Mouse_HLInfo *hlinfo,
24985 EMACS_INT mouse_charpos,
24986 EMACS_INT start_charpos,
24987 EMACS_INT end_charpos,
24988 Lisp_Object before_string,
24989 Lisp_Object after_string,
24990 Lisp_Object cover_string)
24991 {
24992 struct window *w = XWINDOW (window);
24993 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24994 struct glyph_row *r1, *r2;
24995 struct glyph *glyph, *end;
24996 EMACS_INT ignore, pos;
24997 int x;
24998
24999 xassert (NILP (cover_string) || STRINGP (cover_string));
25000 xassert (NILP (before_string) || STRINGP (before_string));
25001 xassert (NILP (after_string) || STRINGP (after_string));
25002
25003 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25004 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25005 if (r1 == NULL)
25006 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25007 /* If the before-string or display-string contains newlines,
25008 rows_from_pos_range skips to its last row. Move back. */
25009 if (!NILP (before_string) || !NILP (cover_string))
25010 {
25011 struct glyph_row *prev;
25012 while ((prev = r1 - 1, prev >= first)
25013 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25014 && prev->used[TEXT_AREA] > 0)
25015 {
25016 struct glyph *beg = prev->glyphs[TEXT_AREA];
25017 glyph = beg + prev->used[TEXT_AREA];
25018 while (--glyph >= beg && INTEGERP (glyph->object));
25019 if (glyph < beg
25020 || !(EQ (glyph->object, before_string)
25021 || EQ (glyph->object, cover_string)))
25022 break;
25023 r1 = prev;
25024 }
25025 }
25026 if (r2 == NULL)
25027 {
25028 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25029 hlinfo->mouse_face_past_end = 1;
25030 }
25031 else if (!NILP (after_string))
25032 {
25033 /* If the after-string has newlines, advance to its last row. */
25034 struct glyph_row *next;
25035 struct glyph_row *last
25036 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25037
25038 for (next = r2 + 1;
25039 next <= last
25040 && next->used[TEXT_AREA] > 0
25041 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25042 ++next)
25043 r2 = next;
25044 }
25045 /* The rest of the display engine assumes that mouse_face_beg_row is
25046 either above below mouse_face_end_row or identical to it. But
25047 with bidi-reordered continued lines, the row for START_CHARPOS
25048 could be below the row for END_CHARPOS. If so, swap the rows and
25049 store them in correct order. */
25050 if (r1->y > r2->y)
25051 {
25052 struct glyph_row *tem = r2;
25053
25054 r2 = r1;
25055 r1 = tem;
25056 }
25057
25058 hlinfo->mouse_face_beg_y = r1->y;
25059 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25060 hlinfo->mouse_face_end_y = r2->y;
25061 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25062
25063 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25064 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25065 could be anywhere in the row and in any order. The strategy
25066 below is to find the leftmost and the rightmost glyph that
25067 belongs to either of these 3 strings, or whose position is
25068 between START_CHARPOS and END_CHARPOS, and highlight all the
25069 glyphs between those two. This may cover more than just the text
25070 between START_CHARPOS and END_CHARPOS if the range of characters
25071 strides the bidi level boundary, e.g. if the beginning is in R2L
25072 text while the end is in L2R text or vice versa. */
25073 if (!r1->reversed_p)
25074 {
25075 /* This row is in a left to right paragraph. Scan it left to
25076 right. */
25077 glyph = r1->glyphs[TEXT_AREA];
25078 end = glyph + r1->used[TEXT_AREA];
25079 x = r1->x;
25080
25081 /* Skip truncation glyphs at the start of the glyph row. */
25082 if (r1->displays_text_p)
25083 for (; glyph < end
25084 && INTEGERP (glyph->object)
25085 && glyph->charpos < 0;
25086 ++glyph)
25087 x += glyph->pixel_width;
25088
25089 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25090 or COVER_STRING, and the first glyph from buffer whose
25091 position is between START_CHARPOS and END_CHARPOS. */
25092 for (; glyph < end
25093 && !INTEGERP (glyph->object)
25094 && !EQ (glyph->object, cover_string)
25095 && !(BUFFERP (glyph->object)
25096 && (glyph->charpos >= start_charpos
25097 && glyph->charpos < end_charpos));
25098 ++glyph)
25099 {
25100 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25101 are present at buffer positions between START_CHARPOS and
25102 END_CHARPOS, or if they come from an overlay. */
25103 if (EQ (glyph->object, before_string))
25104 {
25105 pos = string_buffer_position (before_string,
25106 start_charpos);
25107 /* If pos == 0, it means before_string came from an
25108 overlay, not from a buffer position. */
25109 if (!pos || (pos >= start_charpos && pos < end_charpos))
25110 break;
25111 }
25112 else if (EQ (glyph->object, after_string))
25113 {
25114 pos = string_buffer_position (after_string, end_charpos);
25115 if (!pos || (pos >= start_charpos && pos < end_charpos))
25116 break;
25117 }
25118 x += glyph->pixel_width;
25119 }
25120 hlinfo->mouse_face_beg_x = x;
25121 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25122 }
25123 else
25124 {
25125 /* This row is in a right to left paragraph. Scan it right to
25126 left. */
25127 struct glyph *g;
25128
25129 end = r1->glyphs[TEXT_AREA] - 1;
25130 glyph = end + r1->used[TEXT_AREA];
25131
25132 /* Skip truncation glyphs at the start of the glyph row. */
25133 if (r1->displays_text_p)
25134 for (; glyph > end
25135 && INTEGERP (glyph->object)
25136 && glyph->charpos < 0;
25137 --glyph)
25138 ;
25139
25140 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25141 or COVER_STRING, and the first glyph from buffer whose
25142 position is between START_CHARPOS and END_CHARPOS. */
25143 for (; glyph > end
25144 && !INTEGERP (glyph->object)
25145 && !EQ (glyph->object, cover_string)
25146 && !(BUFFERP (glyph->object)
25147 && (glyph->charpos >= start_charpos
25148 && glyph->charpos < end_charpos));
25149 --glyph)
25150 {
25151 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25152 are present at buffer positions between START_CHARPOS and
25153 END_CHARPOS, or if they come from an overlay. */
25154 if (EQ (glyph->object, before_string))
25155 {
25156 pos = string_buffer_position (before_string, start_charpos);
25157 /* If pos == 0, it means before_string came from an
25158 overlay, not from a buffer position. */
25159 if (!pos || (pos >= start_charpos && pos < end_charpos))
25160 break;
25161 }
25162 else if (EQ (glyph->object, after_string))
25163 {
25164 pos = string_buffer_position (after_string, end_charpos);
25165 if (!pos || (pos >= start_charpos && pos < end_charpos))
25166 break;
25167 }
25168 }
25169
25170 glyph++; /* first glyph to the right of the highlighted area */
25171 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25172 x += g->pixel_width;
25173 hlinfo->mouse_face_beg_x = x;
25174 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25175 }
25176
25177 /* If the highlight ends in a different row, compute GLYPH and END
25178 for the end row. Otherwise, reuse the values computed above for
25179 the row where the highlight begins. */
25180 if (r2 != r1)
25181 {
25182 if (!r2->reversed_p)
25183 {
25184 glyph = r2->glyphs[TEXT_AREA];
25185 end = glyph + r2->used[TEXT_AREA];
25186 x = r2->x;
25187 }
25188 else
25189 {
25190 end = r2->glyphs[TEXT_AREA] - 1;
25191 glyph = end + r2->used[TEXT_AREA];
25192 }
25193 }
25194
25195 if (!r2->reversed_p)
25196 {
25197 /* Skip truncation and continuation glyphs near the end of the
25198 row, and also blanks and stretch glyphs inserted by
25199 extend_face_to_end_of_line. */
25200 while (end > glyph
25201 && INTEGERP ((end - 1)->object)
25202 && (end - 1)->charpos <= 0)
25203 --end;
25204 /* Scan the rest of the glyph row from the end, looking for the
25205 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25206 COVER_STRING, or whose position is between START_CHARPOS
25207 and END_CHARPOS */
25208 for (--end;
25209 end > glyph
25210 && !INTEGERP (end->object)
25211 && !EQ (end->object, cover_string)
25212 && !(BUFFERP (end->object)
25213 && (end->charpos >= start_charpos
25214 && end->charpos < end_charpos));
25215 --end)
25216 {
25217 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25218 are present at buffer positions between START_CHARPOS and
25219 END_CHARPOS, or if they come from an overlay. */
25220 if (EQ (end->object, before_string))
25221 {
25222 pos = string_buffer_position (before_string, start_charpos);
25223 if (!pos || (pos >= start_charpos && pos < end_charpos))
25224 break;
25225 }
25226 else if (EQ (end->object, after_string))
25227 {
25228 pos = string_buffer_position (after_string, end_charpos);
25229 if (!pos || (pos >= start_charpos && pos < end_charpos))
25230 break;
25231 }
25232 }
25233 /* Find the X coordinate of the last glyph to be highlighted. */
25234 for (; glyph <= end; ++glyph)
25235 x += glyph->pixel_width;
25236
25237 hlinfo->mouse_face_end_x = x;
25238 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25239 }
25240 else
25241 {
25242 /* Skip truncation and continuation glyphs near the end of the
25243 row, and also blanks and stretch glyphs inserted by
25244 extend_face_to_end_of_line. */
25245 x = r2->x;
25246 end++;
25247 while (end < glyph
25248 && INTEGERP (end->object)
25249 && end->charpos <= 0)
25250 {
25251 x += end->pixel_width;
25252 ++end;
25253 }
25254 /* Scan the rest of the glyph row from the end, looking for the
25255 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25256 COVER_STRING, or whose position is between START_CHARPOS
25257 and END_CHARPOS */
25258 for ( ;
25259 end < glyph
25260 && !INTEGERP (end->object)
25261 && !EQ (end->object, cover_string)
25262 && !(BUFFERP (end->object)
25263 && (end->charpos >= start_charpos
25264 && end->charpos < end_charpos));
25265 ++end)
25266 {
25267 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25268 are present at buffer positions between START_CHARPOS and
25269 END_CHARPOS, or if they come from an overlay. */
25270 if (EQ (end->object, before_string))
25271 {
25272 pos = string_buffer_position (before_string, start_charpos);
25273 if (!pos || (pos >= start_charpos && pos < end_charpos))
25274 break;
25275 }
25276 else if (EQ (end->object, after_string))
25277 {
25278 pos = string_buffer_position (after_string, end_charpos);
25279 if (!pos || (pos >= start_charpos && pos < end_charpos))
25280 break;
25281 }
25282 x += end->pixel_width;
25283 }
25284 hlinfo->mouse_face_end_x = x;
25285 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25286 }
25287
25288 hlinfo->mouse_face_window = window;
25289 hlinfo->mouse_face_face_id
25290 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25291 mouse_charpos + 1,
25292 !hlinfo->mouse_face_hidden, -1);
25293 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25294 }
25295
25296 /* The following function is not used anymore (replaced with
25297 mouse_face_from_string_pos), but I leave it here for the time
25298 being, in case someone would. */
25299
25300 #if 0 /* not used */
25301
25302 /* Find the position of the glyph for position POS in OBJECT in
25303 window W's current matrix, and return in *X, *Y the pixel
25304 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25305
25306 RIGHT_P non-zero means return the position of the right edge of the
25307 glyph, RIGHT_P zero means return the left edge position.
25308
25309 If no glyph for POS exists in the matrix, return the position of
25310 the glyph with the next smaller position that is in the matrix, if
25311 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25312 exists in the matrix, return the position of the glyph with the
25313 next larger position in OBJECT.
25314
25315 Value is non-zero if a glyph was found. */
25316
25317 static int
25318 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25319 int *hpos, int *vpos, int *x, int *y, int right_p)
25320 {
25321 int yb = window_text_bottom_y (w);
25322 struct glyph_row *r;
25323 struct glyph *best_glyph = NULL;
25324 struct glyph_row *best_row = NULL;
25325 int best_x = 0;
25326
25327 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25328 r->enabled_p && r->y < yb;
25329 ++r)
25330 {
25331 struct glyph *g = r->glyphs[TEXT_AREA];
25332 struct glyph *e = g + r->used[TEXT_AREA];
25333 int gx;
25334
25335 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25336 if (EQ (g->object, object))
25337 {
25338 if (g->charpos == pos)
25339 {
25340 best_glyph = g;
25341 best_x = gx;
25342 best_row = r;
25343 goto found;
25344 }
25345 else if (best_glyph == NULL
25346 || ((eabs (g->charpos - pos)
25347 < eabs (best_glyph->charpos - pos))
25348 && (right_p
25349 ? g->charpos < pos
25350 : g->charpos > pos)))
25351 {
25352 best_glyph = g;
25353 best_x = gx;
25354 best_row = r;
25355 }
25356 }
25357 }
25358
25359 found:
25360
25361 if (best_glyph)
25362 {
25363 *x = best_x;
25364 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25365
25366 if (right_p)
25367 {
25368 *x += best_glyph->pixel_width;
25369 ++*hpos;
25370 }
25371
25372 *y = best_row->y;
25373 *vpos = best_row - w->current_matrix->rows;
25374 }
25375
25376 return best_glyph != NULL;
25377 }
25378 #endif /* not used */
25379
25380 /* Find the positions of the first and the last glyphs in window W's
25381 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25382 (assumed to be a string), and return in HLINFO's mouse_face_*
25383 members the pixel and column/row coordinates of those glyphs. */
25384
25385 static void
25386 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25387 Lisp_Object object,
25388 EMACS_INT startpos, EMACS_INT endpos)
25389 {
25390 int yb = window_text_bottom_y (w);
25391 struct glyph_row *r;
25392 struct glyph *g, *e;
25393 int gx;
25394 int found = 0;
25395
25396 /* Find the glyph row with at least one position in the range
25397 [STARTPOS..ENDPOS], and the first glyph in that row whose
25398 position belongs to that range. */
25399 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25400 r->enabled_p && r->y < yb;
25401 ++r)
25402 {
25403 if (!r->reversed_p)
25404 {
25405 g = r->glyphs[TEXT_AREA];
25406 e = g + r->used[TEXT_AREA];
25407 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25408 if (EQ (g->object, object)
25409 && startpos <= g->charpos && g->charpos <= endpos)
25410 {
25411 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25412 hlinfo->mouse_face_beg_y = r->y;
25413 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25414 hlinfo->mouse_face_beg_x = gx;
25415 found = 1;
25416 break;
25417 }
25418 }
25419 else
25420 {
25421 struct glyph *g1;
25422
25423 e = r->glyphs[TEXT_AREA];
25424 g = e + r->used[TEXT_AREA];
25425 for ( ; g > e; --g)
25426 if (EQ ((g-1)->object, object)
25427 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25428 {
25429 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25430 hlinfo->mouse_face_beg_y = r->y;
25431 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25432 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25433 gx += g1->pixel_width;
25434 hlinfo->mouse_face_beg_x = gx;
25435 found = 1;
25436 break;
25437 }
25438 }
25439 if (found)
25440 break;
25441 }
25442
25443 if (!found)
25444 return;
25445
25446 /* Starting with the next row, look for the first row which does NOT
25447 include any glyphs whose positions are in the range. */
25448 for (++r; r->enabled_p && r->y < yb; ++r)
25449 {
25450 g = r->glyphs[TEXT_AREA];
25451 e = g + r->used[TEXT_AREA];
25452 found = 0;
25453 for ( ; g < e; ++g)
25454 if (EQ (g->object, object)
25455 && startpos <= g->charpos && g->charpos <= endpos)
25456 {
25457 found = 1;
25458 break;
25459 }
25460 if (!found)
25461 break;
25462 }
25463
25464 /* The highlighted region ends on the previous row. */
25465 r--;
25466
25467 /* Set the end row and its vertical pixel coordinate. */
25468 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25469 hlinfo->mouse_face_end_y = r->y;
25470
25471 /* Compute and set the end column and the end column's horizontal
25472 pixel coordinate. */
25473 if (!r->reversed_p)
25474 {
25475 g = r->glyphs[TEXT_AREA];
25476 e = g + r->used[TEXT_AREA];
25477 for ( ; e > g; --e)
25478 if (EQ ((e-1)->object, object)
25479 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25480 break;
25481 hlinfo->mouse_face_end_col = e - g;
25482
25483 for (gx = r->x; g < e; ++g)
25484 gx += g->pixel_width;
25485 hlinfo->mouse_face_end_x = gx;
25486 }
25487 else
25488 {
25489 e = r->glyphs[TEXT_AREA];
25490 g = e + r->used[TEXT_AREA];
25491 for (gx = r->x ; e < g; ++e)
25492 {
25493 if (EQ (e->object, object)
25494 && startpos <= e->charpos && e->charpos <= endpos)
25495 break;
25496 gx += e->pixel_width;
25497 }
25498 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25499 hlinfo->mouse_face_end_x = gx;
25500 }
25501 }
25502
25503 #ifdef HAVE_WINDOW_SYSTEM
25504
25505 /* See if position X, Y is within a hot-spot of an image. */
25506
25507 static int
25508 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25509 {
25510 if (!CONSP (hot_spot))
25511 return 0;
25512
25513 if (EQ (XCAR (hot_spot), Qrect))
25514 {
25515 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25516 Lisp_Object rect = XCDR (hot_spot);
25517 Lisp_Object tem;
25518 if (!CONSP (rect))
25519 return 0;
25520 if (!CONSP (XCAR (rect)))
25521 return 0;
25522 if (!CONSP (XCDR (rect)))
25523 return 0;
25524 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25525 return 0;
25526 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25527 return 0;
25528 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25529 return 0;
25530 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25531 return 0;
25532 return 1;
25533 }
25534 else if (EQ (XCAR (hot_spot), Qcircle))
25535 {
25536 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25537 Lisp_Object circ = XCDR (hot_spot);
25538 Lisp_Object lr, lx0, ly0;
25539 if (CONSP (circ)
25540 && CONSP (XCAR (circ))
25541 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25542 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25543 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25544 {
25545 double r = XFLOATINT (lr);
25546 double dx = XINT (lx0) - x;
25547 double dy = XINT (ly0) - y;
25548 return (dx * dx + dy * dy <= r * r);
25549 }
25550 }
25551 else if (EQ (XCAR (hot_spot), Qpoly))
25552 {
25553 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25554 if (VECTORP (XCDR (hot_spot)))
25555 {
25556 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25557 Lisp_Object *poly = v->contents;
25558 int n = v->header.size;
25559 int i;
25560 int inside = 0;
25561 Lisp_Object lx, ly;
25562 int x0, y0;
25563
25564 /* Need an even number of coordinates, and at least 3 edges. */
25565 if (n < 6 || n & 1)
25566 return 0;
25567
25568 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25569 If count is odd, we are inside polygon. Pixels on edges
25570 may or may not be included depending on actual geometry of the
25571 polygon. */
25572 if ((lx = poly[n-2], !INTEGERP (lx))
25573 || (ly = poly[n-1], !INTEGERP (lx)))
25574 return 0;
25575 x0 = XINT (lx), y0 = XINT (ly);
25576 for (i = 0; i < n; i += 2)
25577 {
25578 int x1 = x0, y1 = y0;
25579 if ((lx = poly[i], !INTEGERP (lx))
25580 || (ly = poly[i+1], !INTEGERP (ly)))
25581 return 0;
25582 x0 = XINT (lx), y0 = XINT (ly);
25583
25584 /* Does this segment cross the X line? */
25585 if (x0 >= x)
25586 {
25587 if (x1 >= x)
25588 continue;
25589 }
25590 else if (x1 < x)
25591 continue;
25592 if (y > y0 && y > y1)
25593 continue;
25594 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25595 inside = !inside;
25596 }
25597 return inside;
25598 }
25599 }
25600 return 0;
25601 }
25602
25603 Lisp_Object
25604 find_hot_spot (Lisp_Object map, int x, int y)
25605 {
25606 while (CONSP (map))
25607 {
25608 if (CONSP (XCAR (map))
25609 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25610 return XCAR (map);
25611 map = XCDR (map);
25612 }
25613
25614 return Qnil;
25615 }
25616
25617 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25618 3, 3, 0,
25619 doc: /* Lookup in image map MAP coordinates X and Y.
25620 An image map is an alist where each element has the format (AREA ID PLIST).
25621 An AREA is specified as either a rectangle, a circle, or a polygon:
25622 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25623 pixel coordinates of the upper left and bottom right corners.
25624 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25625 and the radius of the circle; r may be a float or integer.
25626 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25627 vector describes one corner in the polygon.
25628 Returns the alist element for the first matching AREA in MAP. */)
25629 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25630 {
25631 if (NILP (map))
25632 return Qnil;
25633
25634 CHECK_NUMBER (x);
25635 CHECK_NUMBER (y);
25636
25637 return find_hot_spot (map, XINT (x), XINT (y));
25638 }
25639
25640
25641 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25642 static void
25643 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25644 {
25645 /* Do not change cursor shape while dragging mouse. */
25646 if (!NILP (do_mouse_tracking))
25647 return;
25648
25649 if (!NILP (pointer))
25650 {
25651 if (EQ (pointer, Qarrow))
25652 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25653 else if (EQ (pointer, Qhand))
25654 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25655 else if (EQ (pointer, Qtext))
25656 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25657 else if (EQ (pointer, intern ("hdrag")))
25658 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25659 #ifdef HAVE_X_WINDOWS
25660 else if (EQ (pointer, intern ("vdrag")))
25661 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25662 #endif
25663 else if (EQ (pointer, intern ("hourglass")))
25664 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25665 else if (EQ (pointer, Qmodeline))
25666 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25667 else
25668 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25669 }
25670
25671 if (cursor != No_Cursor)
25672 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25673 }
25674
25675 #endif /* HAVE_WINDOW_SYSTEM */
25676
25677 /* Take proper action when mouse has moved to the mode or header line
25678 or marginal area AREA of window W, x-position X and y-position Y.
25679 X is relative to the start of the text display area of W, so the
25680 width of bitmap areas and scroll bars must be subtracted to get a
25681 position relative to the start of the mode line. */
25682
25683 static void
25684 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25685 enum window_part area)
25686 {
25687 struct window *w = XWINDOW (window);
25688 struct frame *f = XFRAME (w->frame);
25689 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25690 #ifdef HAVE_WINDOW_SYSTEM
25691 Display_Info *dpyinfo;
25692 #endif
25693 Cursor cursor = No_Cursor;
25694 Lisp_Object pointer = Qnil;
25695 int dx, dy, width, height;
25696 EMACS_INT charpos;
25697 Lisp_Object string, object = Qnil;
25698 Lisp_Object pos, help;
25699
25700 Lisp_Object mouse_face;
25701 int original_x_pixel = x;
25702 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25703 struct glyph_row *row;
25704
25705 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25706 {
25707 int x0;
25708 struct glyph *end;
25709
25710 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25711 returns them in row/column units! */
25712 string = mode_line_string (w, area, &x, &y, &charpos,
25713 &object, &dx, &dy, &width, &height);
25714
25715 row = (area == ON_MODE_LINE
25716 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25717 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25718
25719 /* Find the glyph under the mouse pointer. */
25720 if (row->mode_line_p && row->enabled_p)
25721 {
25722 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25723 end = glyph + row->used[TEXT_AREA];
25724
25725 for (x0 = original_x_pixel;
25726 glyph < end && x0 >= glyph->pixel_width;
25727 ++glyph)
25728 x0 -= glyph->pixel_width;
25729
25730 if (glyph >= end)
25731 glyph = NULL;
25732 }
25733 }
25734 else
25735 {
25736 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25737 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25738 returns them in row/column units! */
25739 string = marginal_area_string (w, area, &x, &y, &charpos,
25740 &object, &dx, &dy, &width, &height);
25741 }
25742
25743 help = Qnil;
25744
25745 #ifdef HAVE_WINDOW_SYSTEM
25746 if (IMAGEP (object))
25747 {
25748 Lisp_Object image_map, hotspot;
25749 if ((image_map = Fplist_get (XCDR (object), QCmap),
25750 !NILP (image_map))
25751 && (hotspot = find_hot_spot (image_map, dx, dy),
25752 CONSP (hotspot))
25753 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25754 {
25755 Lisp_Object plist;
25756
25757 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25758 If so, we could look for mouse-enter, mouse-leave
25759 properties in PLIST (and do something...). */
25760 hotspot = XCDR (hotspot);
25761 if (CONSP (hotspot)
25762 && (plist = XCAR (hotspot), CONSP (plist)))
25763 {
25764 pointer = Fplist_get (plist, Qpointer);
25765 if (NILP (pointer))
25766 pointer = Qhand;
25767 help = Fplist_get (plist, Qhelp_echo);
25768 if (!NILP (help))
25769 {
25770 help_echo_string = help;
25771 /* Is this correct? ++kfs */
25772 XSETWINDOW (help_echo_window, w);
25773 help_echo_object = w->buffer;
25774 help_echo_pos = charpos;
25775 }
25776 }
25777 }
25778 if (NILP (pointer))
25779 pointer = Fplist_get (XCDR (object), QCpointer);
25780 }
25781 #endif /* HAVE_WINDOW_SYSTEM */
25782
25783 if (STRINGP (string))
25784 {
25785 pos = make_number (charpos);
25786 /* If we're on a string with `help-echo' text property, arrange
25787 for the help to be displayed. This is done by setting the
25788 global variable help_echo_string to the help string. */
25789 if (NILP (help))
25790 {
25791 help = Fget_text_property (pos, Qhelp_echo, string);
25792 if (!NILP (help))
25793 {
25794 help_echo_string = help;
25795 XSETWINDOW (help_echo_window, w);
25796 help_echo_object = string;
25797 help_echo_pos = charpos;
25798 }
25799 }
25800
25801 #ifdef HAVE_WINDOW_SYSTEM
25802 if (FRAME_WINDOW_P (f))
25803 {
25804 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25805 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25806 if (NILP (pointer))
25807 pointer = Fget_text_property (pos, Qpointer, string);
25808
25809 /* Change the mouse pointer according to what is under X/Y. */
25810 if (NILP (pointer)
25811 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25812 {
25813 Lisp_Object map;
25814 map = Fget_text_property (pos, Qlocal_map, string);
25815 if (!KEYMAPP (map))
25816 map = Fget_text_property (pos, Qkeymap, string);
25817 if (!KEYMAPP (map))
25818 cursor = dpyinfo->vertical_scroll_bar_cursor;
25819 }
25820 }
25821 #endif
25822
25823 /* Change the mouse face according to what is under X/Y. */
25824 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25825 if (!NILP (mouse_face)
25826 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25827 && glyph)
25828 {
25829 Lisp_Object b, e;
25830
25831 struct glyph * tmp_glyph;
25832
25833 int gpos;
25834 int gseq_length;
25835 int total_pixel_width;
25836 EMACS_INT begpos, endpos, ignore;
25837
25838 int vpos, hpos;
25839
25840 b = Fprevious_single_property_change (make_number (charpos + 1),
25841 Qmouse_face, string, Qnil);
25842 if (NILP (b))
25843 begpos = 0;
25844 else
25845 begpos = XINT (b);
25846
25847 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25848 if (NILP (e))
25849 endpos = SCHARS (string);
25850 else
25851 endpos = XINT (e);
25852
25853 /* Calculate the glyph position GPOS of GLYPH in the
25854 displayed string, relative to the beginning of the
25855 highlighted part of the string.
25856
25857 Note: GPOS is different from CHARPOS. CHARPOS is the
25858 position of GLYPH in the internal string object. A mode
25859 line string format has structures which are converted to
25860 a flattened string by the Emacs Lisp interpreter. The
25861 internal string is an element of those structures. The
25862 displayed string is the flattened string. */
25863 tmp_glyph = row_start_glyph;
25864 while (tmp_glyph < glyph
25865 && (!(EQ (tmp_glyph->object, glyph->object)
25866 && begpos <= tmp_glyph->charpos
25867 && tmp_glyph->charpos < endpos)))
25868 tmp_glyph++;
25869 gpos = glyph - tmp_glyph;
25870
25871 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25872 the highlighted part of the displayed string to which
25873 GLYPH belongs. Note: GSEQ_LENGTH is different from
25874 SCHARS (STRING), because the latter returns the length of
25875 the internal string. */
25876 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25877 tmp_glyph > glyph
25878 && (!(EQ (tmp_glyph->object, glyph->object)
25879 && begpos <= tmp_glyph->charpos
25880 && tmp_glyph->charpos < endpos));
25881 tmp_glyph--)
25882 ;
25883 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25884
25885 /* Calculate the total pixel width of all the glyphs between
25886 the beginning of the highlighted area and GLYPH. */
25887 total_pixel_width = 0;
25888 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25889 total_pixel_width += tmp_glyph->pixel_width;
25890
25891 /* Pre calculation of re-rendering position. Note: X is in
25892 column units here, after the call to mode_line_string or
25893 marginal_area_string. */
25894 hpos = x - gpos;
25895 vpos = (area == ON_MODE_LINE
25896 ? (w->current_matrix)->nrows - 1
25897 : 0);
25898
25899 /* If GLYPH's position is included in the region that is
25900 already drawn in mouse face, we have nothing to do. */
25901 if ( EQ (window, hlinfo->mouse_face_window)
25902 && (!row->reversed_p
25903 ? (hlinfo->mouse_face_beg_col <= hpos
25904 && hpos < hlinfo->mouse_face_end_col)
25905 /* In R2L rows we swap BEG and END, see below. */
25906 : (hlinfo->mouse_face_end_col <= hpos
25907 && hpos < hlinfo->mouse_face_beg_col))
25908 && hlinfo->mouse_face_beg_row == vpos )
25909 return;
25910
25911 if (clear_mouse_face (hlinfo))
25912 cursor = No_Cursor;
25913
25914 if (!row->reversed_p)
25915 {
25916 hlinfo->mouse_face_beg_col = hpos;
25917 hlinfo->mouse_face_beg_x = original_x_pixel
25918 - (total_pixel_width + dx);
25919 hlinfo->mouse_face_end_col = hpos + gseq_length;
25920 hlinfo->mouse_face_end_x = 0;
25921 }
25922 else
25923 {
25924 /* In R2L rows, show_mouse_face expects BEG and END
25925 coordinates to be swapped. */
25926 hlinfo->mouse_face_end_col = hpos;
25927 hlinfo->mouse_face_end_x = original_x_pixel
25928 - (total_pixel_width + dx);
25929 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25930 hlinfo->mouse_face_beg_x = 0;
25931 }
25932
25933 hlinfo->mouse_face_beg_row = vpos;
25934 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25935 hlinfo->mouse_face_beg_y = 0;
25936 hlinfo->mouse_face_end_y = 0;
25937 hlinfo->mouse_face_past_end = 0;
25938 hlinfo->mouse_face_window = window;
25939
25940 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25941 charpos,
25942 0, 0, 0,
25943 &ignore,
25944 glyph->face_id,
25945 1);
25946 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25947
25948 if (NILP (pointer))
25949 pointer = Qhand;
25950 }
25951 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25952 clear_mouse_face (hlinfo);
25953 }
25954 #ifdef HAVE_WINDOW_SYSTEM
25955 if (FRAME_WINDOW_P (f))
25956 define_frame_cursor1 (f, cursor, pointer);
25957 #endif
25958 }
25959
25960
25961 /* EXPORT:
25962 Take proper action when the mouse has moved to position X, Y on
25963 frame F as regards highlighting characters that have mouse-face
25964 properties. Also de-highlighting chars where the mouse was before.
25965 X and Y can be negative or out of range. */
25966
25967 void
25968 note_mouse_highlight (struct frame *f, int x, int y)
25969 {
25970 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25971 enum window_part part;
25972 Lisp_Object window;
25973 struct window *w;
25974 Cursor cursor = No_Cursor;
25975 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25976 struct buffer *b;
25977
25978 /* When a menu is active, don't highlight because this looks odd. */
25979 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25980 if (popup_activated ())
25981 return;
25982 #endif
25983
25984 if (NILP (Vmouse_highlight)
25985 || !f->glyphs_initialized_p
25986 || f->pointer_invisible)
25987 return;
25988
25989 hlinfo->mouse_face_mouse_x = x;
25990 hlinfo->mouse_face_mouse_y = y;
25991 hlinfo->mouse_face_mouse_frame = f;
25992
25993 if (hlinfo->mouse_face_defer)
25994 return;
25995
25996 if (gc_in_progress)
25997 {
25998 hlinfo->mouse_face_deferred_gc = 1;
25999 return;
26000 }
26001
26002 /* Which window is that in? */
26003 window = window_from_coordinates (f, x, y, &part, 1);
26004
26005 /* If we were displaying active text in another window, clear that.
26006 Also clear if we move out of text area in same window. */
26007 if (! EQ (window, hlinfo->mouse_face_window)
26008 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26009 && !NILP (hlinfo->mouse_face_window)))
26010 clear_mouse_face (hlinfo);
26011
26012 /* Not on a window -> return. */
26013 if (!WINDOWP (window))
26014 return;
26015
26016 /* Reset help_echo_string. It will get recomputed below. */
26017 help_echo_string = Qnil;
26018
26019 /* Convert to window-relative pixel coordinates. */
26020 w = XWINDOW (window);
26021 frame_to_window_pixel_xy (w, &x, &y);
26022
26023 #ifdef HAVE_WINDOW_SYSTEM
26024 /* Handle tool-bar window differently since it doesn't display a
26025 buffer. */
26026 if (EQ (window, f->tool_bar_window))
26027 {
26028 note_tool_bar_highlight (f, x, y);
26029 return;
26030 }
26031 #endif
26032
26033 /* Mouse is on the mode, header line or margin? */
26034 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26035 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26036 {
26037 note_mode_line_or_margin_highlight (window, x, y, part);
26038 return;
26039 }
26040
26041 #ifdef HAVE_WINDOW_SYSTEM
26042 if (part == ON_VERTICAL_BORDER)
26043 {
26044 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26045 help_echo_string = build_string ("drag-mouse-1: resize");
26046 }
26047 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26048 || part == ON_SCROLL_BAR)
26049 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26050 else
26051 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26052 #endif
26053
26054 /* Are we in a window whose display is up to date?
26055 And verify the buffer's text has not changed. */
26056 b = XBUFFER (w->buffer);
26057 if (part == ON_TEXT
26058 && EQ (w->window_end_valid, w->buffer)
26059 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26060 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26061 {
26062 int hpos, vpos, i, dx, dy, area;
26063 EMACS_INT pos;
26064 struct glyph *glyph;
26065 Lisp_Object object;
26066 Lisp_Object mouse_face = Qnil, position;
26067 Lisp_Object *overlay_vec = NULL;
26068 int noverlays;
26069 struct buffer *obuf;
26070 EMACS_INT obegv, ozv;
26071 int same_region;
26072
26073 /* Find the glyph under X/Y. */
26074 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26075
26076 #ifdef HAVE_WINDOW_SYSTEM
26077 /* Look for :pointer property on image. */
26078 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26079 {
26080 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26081 if (img != NULL && IMAGEP (img->spec))
26082 {
26083 Lisp_Object image_map, hotspot;
26084 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26085 !NILP (image_map))
26086 && (hotspot = find_hot_spot (image_map,
26087 glyph->slice.img.x + dx,
26088 glyph->slice.img.y + dy),
26089 CONSP (hotspot))
26090 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26091 {
26092 Lisp_Object plist;
26093
26094 /* Could check XCAR (hotspot) to see if we enter/leave
26095 this hot-spot.
26096 If so, we could look for mouse-enter, mouse-leave
26097 properties in PLIST (and do something...). */
26098 hotspot = XCDR (hotspot);
26099 if (CONSP (hotspot)
26100 && (plist = XCAR (hotspot), CONSP (plist)))
26101 {
26102 pointer = Fplist_get (plist, Qpointer);
26103 if (NILP (pointer))
26104 pointer = Qhand;
26105 help_echo_string = Fplist_get (plist, Qhelp_echo);
26106 if (!NILP (help_echo_string))
26107 {
26108 help_echo_window = window;
26109 help_echo_object = glyph->object;
26110 help_echo_pos = glyph->charpos;
26111 }
26112 }
26113 }
26114 if (NILP (pointer))
26115 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26116 }
26117 }
26118 #endif /* HAVE_WINDOW_SYSTEM */
26119
26120 /* Clear mouse face if X/Y not over text. */
26121 if (glyph == NULL
26122 || area != TEXT_AREA
26123 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26124 /* Glyph's OBJECT is an integer for glyphs inserted by the
26125 display engine for its internal purposes, like truncation
26126 and continuation glyphs and blanks beyond the end of
26127 line's text on text terminals. If we are over such a
26128 glyph, we are not over any text. */
26129 || INTEGERP (glyph->object)
26130 /* R2L rows have a stretch glyph at their front, which
26131 stands for no text, whereas L2R rows have no glyphs at
26132 all beyond the end of text. Treat such stretch glyphs
26133 like we do with NULL glyphs in L2R rows. */
26134 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26135 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26136 && glyph->type == STRETCH_GLYPH
26137 && glyph->avoid_cursor_p))
26138 {
26139 if (clear_mouse_face (hlinfo))
26140 cursor = No_Cursor;
26141 #ifdef HAVE_WINDOW_SYSTEM
26142 if (FRAME_WINDOW_P (f) && NILP (pointer))
26143 {
26144 if (area != TEXT_AREA)
26145 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26146 else
26147 pointer = Vvoid_text_area_pointer;
26148 }
26149 #endif
26150 goto set_cursor;
26151 }
26152
26153 pos = glyph->charpos;
26154 object = glyph->object;
26155 if (!STRINGP (object) && !BUFFERP (object))
26156 goto set_cursor;
26157
26158 /* If we get an out-of-range value, return now; avoid an error. */
26159 if (BUFFERP (object) && pos > BUF_Z (b))
26160 goto set_cursor;
26161
26162 /* Make the window's buffer temporarily current for
26163 overlays_at and compute_char_face. */
26164 obuf = current_buffer;
26165 current_buffer = b;
26166 obegv = BEGV;
26167 ozv = ZV;
26168 BEGV = BEG;
26169 ZV = Z;
26170
26171 /* Is this char mouse-active or does it have help-echo? */
26172 position = make_number (pos);
26173
26174 if (BUFFERP (object))
26175 {
26176 /* Put all the overlays we want in a vector in overlay_vec. */
26177 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26178 /* Sort overlays into increasing priority order. */
26179 noverlays = sort_overlays (overlay_vec, noverlays, w);
26180 }
26181 else
26182 noverlays = 0;
26183
26184 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26185
26186 if (same_region)
26187 cursor = No_Cursor;
26188
26189 /* Check mouse-face highlighting. */
26190 if (! same_region
26191 /* If there exists an overlay with mouse-face overlapping
26192 the one we are currently highlighting, we have to
26193 check if we enter the overlapping overlay, and then
26194 highlight only that. */
26195 || (OVERLAYP (hlinfo->mouse_face_overlay)
26196 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26197 {
26198 /* Find the highest priority overlay with a mouse-face. */
26199 Lisp_Object overlay = Qnil;
26200 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26201 {
26202 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26203 if (!NILP (mouse_face))
26204 overlay = overlay_vec[i];
26205 }
26206
26207 /* If we're highlighting the same overlay as before, there's
26208 no need to do that again. */
26209 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26210 goto check_help_echo;
26211 hlinfo->mouse_face_overlay = overlay;
26212
26213 /* Clear the display of the old active region, if any. */
26214 if (clear_mouse_face (hlinfo))
26215 cursor = No_Cursor;
26216
26217 /* If no overlay applies, get a text property. */
26218 if (NILP (overlay))
26219 mouse_face = Fget_text_property (position, Qmouse_face, object);
26220
26221 /* Next, compute the bounds of the mouse highlighting and
26222 display it. */
26223 if (!NILP (mouse_face) && STRINGP (object))
26224 {
26225 /* The mouse-highlighting comes from a display string
26226 with a mouse-face. */
26227 Lisp_Object s, e;
26228 EMACS_INT ignore;
26229
26230 s = Fprevious_single_property_change
26231 (make_number (pos + 1), Qmouse_face, object, Qnil);
26232 e = Fnext_single_property_change
26233 (position, Qmouse_face, object, Qnil);
26234 if (NILP (s))
26235 s = make_number (0);
26236 if (NILP (e))
26237 e = make_number (SCHARS (object) - 1);
26238 mouse_face_from_string_pos (w, hlinfo, object,
26239 XINT (s), XINT (e));
26240 hlinfo->mouse_face_past_end = 0;
26241 hlinfo->mouse_face_window = window;
26242 hlinfo->mouse_face_face_id
26243 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26244 glyph->face_id, 1);
26245 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26246 cursor = No_Cursor;
26247 }
26248 else
26249 {
26250 /* The mouse-highlighting, if any, comes from an overlay
26251 or text property in the buffer. */
26252 Lisp_Object buffer IF_LINT (= Qnil);
26253 Lisp_Object cover_string IF_LINT (= Qnil);
26254
26255 if (STRINGP (object))
26256 {
26257 /* If we are on a display string with no mouse-face,
26258 check if the text under it has one. */
26259 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26260 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26261 pos = string_buffer_position (object, start);
26262 if (pos > 0)
26263 {
26264 mouse_face = get_char_property_and_overlay
26265 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26266 buffer = w->buffer;
26267 cover_string = object;
26268 }
26269 }
26270 else
26271 {
26272 buffer = object;
26273 cover_string = Qnil;
26274 }
26275
26276 if (!NILP (mouse_face))
26277 {
26278 Lisp_Object before, after;
26279 Lisp_Object before_string, after_string;
26280 /* To correctly find the limits of mouse highlight
26281 in a bidi-reordered buffer, we must not use the
26282 optimization of limiting the search in
26283 previous-single-property-change and
26284 next-single-property-change, because
26285 rows_from_pos_range needs the real start and end
26286 positions to DTRT in this case. That's because
26287 the first row visible in a window does not
26288 necessarily display the character whose position
26289 is the smallest. */
26290 Lisp_Object lim1 =
26291 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26292 ? Fmarker_position (w->start)
26293 : Qnil;
26294 Lisp_Object lim2 =
26295 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26296 ? make_number (BUF_Z (XBUFFER (buffer))
26297 - XFASTINT (w->window_end_pos))
26298 : Qnil;
26299
26300 if (NILP (overlay))
26301 {
26302 /* Handle the text property case. */
26303 before = Fprevious_single_property_change
26304 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26305 after = Fnext_single_property_change
26306 (make_number (pos), Qmouse_face, buffer, lim2);
26307 before_string = after_string = Qnil;
26308 }
26309 else
26310 {
26311 /* Handle the overlay case. */
26312 before = Foverlay_start (overlay);
26313 after = Foverlay_end (overlay);
26314 before_string = Foverlay_get (overlay, Qbefore_string);
26315 after_string = Foverlay_get (overlay, Qafter_string);
26316
26317 if (!STRINGP (before_string)) before_string = Qnil;
26318 if (!STRINGP (after_string)) after_string = Qnil;
26319 }
26320
26321 mouse_face_from_buffer_pos (window, hlinfo, pos,
26322 XFASTINT (before),
26323 XFASTINT (after),
26324 before_string, after_string,
26325 cover_string);
26326 cursor = No_Cursor;
26327 }
26328 }
26329 }
26330
26331 check_help_echo:
26332
26333 /* Look for a `help-echo' property. */
26334 if (NILP (help_echo_string)) {
26335 Lisp_Object help, overlay;
26336
26337 /* Check overlays first. */
26338 help = overlay = Qnil;
26339 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26340 {
26341 overlay = overlay_vec[i];
26342 help = Foverlay_get (overlay, Qhelp_echo);
26343 }
26344
26345 if (!NILP (help))
26346 {
26347 help_echo_string = help;
26348 help_echo_window = window;
26349 help_echo_object = overlay;
26350 help_echo_pos = pos;
26351 }
26352 else
26353 {
26354 Lisp_Object obj = glyph->object;
26355 EMACS_INT charpos = glyph->charpos;
26356
26357 /* Try text properties. */
26358 if (STRINGP (obj)
26359 && charpos >= 0
26360 && charpos < SCHARS (obj))
26361 {
26362 help = Fget_text_property (make_number (charpos),
26363 Qhelp_echo, obj);
26364 if (NILP (help))
26365 {
26366 /* If the string itself doesn't specify a help-echo,
26367 see if the buffer text ``under'' it does. */
26368 struct glyph_row *r
26369 = MATRIX_ROW (w->current_matrix, vpos);
26370 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26371 EMACS_INT p = string_buffer_position (obj, start);
26372 if (p > 0)
26373 {
26374 help = Fget_char_property (make_number (p),
26375 Qhelp_echo, w->buffer);
26376 if (!NILP (help))
26377 {
26378 charpos = p;
26379 obj = w->buffer;
26380 }
26381 }
26382 }
26383 }
26384 else if (BUFFERP (obj)
26385 && charpos >= BEGV
26386 && charpos < ZV)
26387 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26388 obj);
26389
26390 if (!NILP (help))
26391 {
26392 help_echo_string = help;
26393 help_echo_window = window;
26394 help_echo_object = obj;
26395 help_echo_pos = charpos;
26396 }
26397 }
26398 }
26399
26400 #ifdef HAVE_WINDOW_SYSTEM
26401 /* Look for a `pointer' property. */
26402 if (FRAME_WINDOW_P (f) && NILP (pointer))
26403 {
26404 /* Check overlays first. */
26405 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26406 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26407
26408 if (NILP (pointer))
26409 {
26410 Lisp_Object obj = glyph->object;
26411 EMACS_INT charpos = glyph->charpos;
26412
26413 /* Try text properties. */
26414 if (STRINGP (obj)
26415 && charpos >= 0
26416 && charpos < SCHARS (obj))
26417 {
26418 pointer = Fget_text_property (make_number (charpos),
26419 Qpointer, obj);
26420 if (NILP (pointer))
26421 {
26422 /* If the string itself doesn't specify a pointer,
26423 see if the buffer text ``under'' it does. */
26424 struct glyph_row *r
26425 = MATRIX_ROW (w->current_matrix, vpos);
26426 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26427 EMACS_INT p = string_buffer_position (obj, start);
26428 if (p > 0)
26429 pointer = Fget_char_property (make_number (p),
26430 Qpointer, w->buffer);
26431 }
26432 }
26433 else if (BUFFERP (obj)
26434 && charpos >= BEGV
26435 && charpos < ZV)
26436 pointer = Fget_text_property (make_number (charpos),
26437 Qpointer, obj);
26438 }
26439 }
26440 #endif /* HAVE_WINDOW_SYSTEM */
26441
26442 BEGV = obegv;
26443 ZV = ozv;
26444 current_buffer = obuf;
26445 }
26446
26447 set_cursor:
26448
26449 #ifdef HAVE_WINDOW_SYSTEM
26450 if (FRAME_WINDOW_P (f))
26451 define_frame_cursor1 (f, cursor, pointer);
26452 #else
26453 /* This is here to prevent a compiler error, about "label at end of
26454 compound statement". */
26455 return;
26456 #endif
26457 }
26458
26459
26460 /* EXPORT for RIF:
26461 Clear any mouse-face on window W. This function is part of the
26462 redisplay interface, and is called from try_window_id and similar
26463 functions to ensure the mouse-highlight is off. */
26464
26465 void
26466 x_clear_window_mouse_face (struct window *w)
26467 {
26468 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26469 Lisp_Object window;
26470
26471 BLOCK_INPUT;
26472 XSETWINDOW (window, w);
26473 if (EQ (window, hlinfo->mouse_face_window))
26474 clear_mouse_face (hlinfo);
26475 UNBLOCK_INPUT;
26476 }
26477
26478
26479 /* EXPORT:
26480 Just discard the mouse face information for frame F, if any.
26481 This is used when the size of F is changed. */
26482
26483 void
26484 cancel_mouse_face (struct frame *f)
26485 {
26486 Lisp_Object window;
26487 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26488
26489 window = hlinfo->mouse_face_window;
26490 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26491 {
26492 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26493 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26494 hlinfo->mouse_face_window = Qnil;
26495 }
26496 }
26497
26498
26499 \f
26500 /***********************************************************************
26501 Exposure Events
26502 ***********************************************************************/
26503
26504 #ifdef HAVE_WINDOW_SYSTEM
26505
26506 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26507 which intersects rectangle R. R is in window-relative coordinates. */
26508
26509 static void
26510 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26511 enum glyph_row_area area)
26512 {
26513 struct glyph *first = row->glyphs[area];
26514 struct glyph *end = row->glyphs[area] + row->used[area];
26515 struct glyph *last;
26516 int first_x, start_x, x;
26517
26518 if (area == TEXT_AREA && row->fill_line_p)
26519 /* If row extends face to end of line write the whole line. */
26520 draw_glyphs (w, 0, row, area,
26521 0, row->used[area],
26522 DRAW_NORMAL_TEXT, 0);
26523 else
26524 {
26525 /* Set START_X to the window-relative start position for drawing glyphs of
26526 AREA. The first glyph of the text area can be partially visible.
26527 The first glyphs of other areas cannot. */
26528 start_x = window_box_left_offset (w, area);
26529 x = start_x;
26530 if (area == TEXT_AREA)
26531 x += row->x;
26532
26533 /* Find the first glyph that must be redrawn. */
26534 while (first < end
26535 && x + first->pixel_width < r->x)
26536 {
26537 x += first->pixel_width;
26538 ++first;
26539 }
26540
26541 /* Find the last one. */
26542 last = first;
26543 first_x = x;
26544 while (last < end
26545 && x < r->x + r->width)
26546 {
26547 x += last->pixel_width;
26548 ++last;
26549 }
26550
26551 /* Repaint. */
26552 if (last > first)
26553 draw_glyphs (w, first_x - start_x, row, area,
26554 first - row->glyphs[area], last - row->glyphs[area],
26555 DRAW_NORMAL_TEXT, 0);
26556 }
26557 }
26558
26559
26560 /* Redraw the parts of the glyph row ROW on window W intersecting
26561 rectangle R. R is in window-relative coordinates. Value is
26562 non-zero if mouse-face was overwritten. */
26563
26564 static int
26565 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26566 {
26567 xassert (row->enabled_p);
26568
26569 if (row->mode_line_p || w->pseudo_window_p)
26570 draw_glyphs (w, 0, row, TEXT_AREA,
26571 0, row->used[TEXT_AREA],
26572 DRAW_NORMAL_TEXT, 0);
26573 else
26574 {
26575 if (row->used[LEFT_MARGIN_AREA])
26576 expose_area (w, row, r, LEFT_MARGIN_AREA);
26577 if (row->used[TEXT_AREA])
26578 expose_area (w, row, r, TEXT_AREA);
26579 if (row->used[RIGHT_MARGIN_AREA])
26580 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26581 draw_row_fringe_bitmaps (w, row);
26582 }
26583
26584 return row->mouse_face_p;
26585 }
26586
26587
26588 /* Redraw those parts of glyphs rows during expose event handling that
26589 overlap other rows. Redrawing of an exposed line writes over parts
26590 of lines overlapping that exposed line; this function fixes that.
26591
26592 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26593 row in W's current matrix that is exposed and overlaps other rows.
26594 LAST_OVERLAPPING_ROW is the last such row. */
26595
26596 static void
26597 expose_overlaps (struct window *w,
26598 struct glyph_row *first_overlapping_row,
26599 struct glyph_row *last_overlapping_row,
26600 XRectangle *r)
26601 {
26602 struct glyph_row *row;
26603
26604 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26605 if (row->overlapping_p)
26606 {
26607 xassert (row->enabled_p && !row->mode_line_p);
26608
26609 row->clip = r;
26610 if (row->used[LEFT_MARGIN_AREA])
26611 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26612
26613 if (row->used[TEXT_AREA])
26614 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26615
26616 if (row->used[RIGHT_MARGIN_AREA])
26617 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26618 row->clip = NULL;
26619 }
26620 }
26621
26622
26623 /* Return non-zero if W's cursor intersects rectangle R. */
26624
26625 static int
26626 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26627 {
26628 XRectangle cr, result;
26629 struct glyph *cursor_glyph;
26630 struct glyph_row *row;
26631
26632 if (w->phys_cursor.vpos >= 0
26633 && w->phys_cursor.vpos < w->current_matrix->nrows
26634 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26635 row->enabled_p)
26636 && row->cursor_in_fringe_p)
26637 {
26638 /* Cursor is in the fringe. */
26639 cr.x = window_box_right_offset (w,
26640 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26641 ? RIGHT_MARGIN_AREA
26642 : TEXT_AREA));
26643 cr.y = row->y;
26644 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26645 cr.height = row->height;
26646 return x_intersect_rectangles (&cr, r, &result);
26647 }
26648
26649 cursor_glyph = get_phys_cursor_glyph (w);
26650 if (cursor_glyph)
26651 {
26652 /* r is relative to W's box, but w->phys_cursor.x is relative
26653 to left edge of W's TEXT area. Adjust it. */
26654 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26655 cr.y = w->phys_cursor.y;
26656 cr.width = cursor_glyph->pixel_width;
26657 cr.height = w->phys_cursor_height;
26658 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26659 I assume the effect is the same -- and this is portable. */
26660 return x_intersect_rectangles (&cr, r, &result);
26661 }
26662 /* If we don't understand the format, pretend we're not in the hot-spot. */
26663 return 0;
26664 }
26665
26666
26667 /* EXPORT:
26668 Draw a vertical window border to the right of window W if W doesn't
26669 have vertical scroll bars. */
26670
26671 void
26672 x_draw_vertical_border (struct window *w)
26673 {
26674 struct frame *f = XFRAME (WINDOW_FRAME (w));
26675
26676 /* We could do better, if we knew what type of scroll-bar the adjacent
26677 windows (on either side) have... But we don't :-(
26678 However, I think this works ok. ++KFS 2003-04-25 */
26679
26680 /* Redraw borders between horizontally adjacent windows. Don't
26681 do it for frames with vertical scroll bars because either the
26682 right scroll bar of a window, or the left scroll bar of its
26683 neighbor will suffice as a border. */
26684 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26685 return;
26686
26687 if (!WINDOW_RIGHTMOST_P (w)
26688 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26689 {
26690 int x0, x1, y0, y1;
26691
26692 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26693 y1 -= 1;
26694
26695 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26696 x1 -= 1;
26697
26698 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26699 }
26700 else if (!WINDOW_LEFTMOST_P (w)
26701 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26702 {
26703 int x0, x1, y0, y1;
26704
26705 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26706 y1 -= 1;
26707
26708 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26709 x0 -= 1;
26710
26711 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26712 }
26713 }
26714
26715
26716 /* Redraw the part of window W intersection rectangle FR. Pixel
26717 coordinates in FR are frame-relative. Call this function with
26718 input blocked. Value is non-zero if the exposure overwrites
26719 mouse-face. */
26720
26721 static int
26722 expose_window (struct window *w, XRectangle *fr)
26723 {
26724 struct frame *f = XFRAME (w->frame);
26725 XRectangle wr, r;
26726 int mouse_face_overwritten_p = 0;
26727
26728 /* If window is not yet fully initialized, do nothing. This can
26729 happen when toolkit scroll bars are used and a window is split.
26730 Reconfiguring the scroll bar will generate an expose for a newly
26731 created window. */
26732 if (w->current_matrix == NULL)
26733 return 0;
26734
26735 /* When we're currently updating the window, display and current
26736 matrix usually don't agree. Arrange for a thorough display
26737 later. */
26738 if (w == updated_window)
26739 {
26740 SET_FRAME_GARBAGED (f);
26741 return 0;
26742 }
26743
26744 /* Frame-relative pixel rectangle of W. */
26745 wr.x = WINDOW_LEFT_EDGE_X (w);
26746 wr.y = WINDOW_TOP_EDGE_Y (w);
26747 wr.width = WINDOW_TOTAL_WIDTH (w);
26748 wr.height = WINDOW_TOTAL_HEIGHT (w);
26749
26750 if (x_intersect_rectangles (fr, &wr, &r))
26751 {
26752 int yb = window_text_bottom_y (w);
26753 struct glyph_row *row;
26754 int cursor_cleared_p;
26755 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26756
26757 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26758 r.x, r.y, r.width, r.height));
26759
26760 /* Convert to window coordinates. */
26761 r.x -= WINDOW_LEFT_EDGE_X (w);
26762 r.y -= WINDOW_TOP_EDGE_Y (w);
26763
26764 /* Turn off the cursor. */
26765 if (!w->pseudo_window_p
26766 && phys_cursor_in_rect_p (w, &r))
26767 {
26768 x_clear_cursor (w);
26769 cursor_cleared_p = 1;
26770 }
26771 else
26772 cursor_cleared_p = 0;
26773
26774 /* Update lines intersecting rectangle R. */
26775 first_overlapping_row = last_overlapping_row = NULL;
26776 for (row = w->current_matrix->rows;
26777 row->enabled_p;
26778 ++row)
26779 {
26780 int y0 = row->y;
26781 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26782
26783 if ((y0 >= r.y && y0 < r.y + r.height)
26784 || (y1 > r.y && y1 < r.y + r.height)
26785 || (r.y >= y0 && r.y < y1)
26786 || (r.y + r.height > y0 && r.y + r.height < y1))
26787 {
26788 /* A header line may be overlapping, but there is no need
26789 to fix overlapping areas for them. KFS 2005-02-12 */
26790 if (row->overlapping_p && !row->mode_line_p)
26791 {
26792 if (first_overlapping_row == NULL)
26793 first_overlapping_row = row;
26794 last_overlapping_row = row;
26795 }
26796
26797 row->clip = fr;
26798 if (expose_line (w, row, &r))
26799 mouse_face_overwritten_p = 1;
26800 row->clip = NULL;
26801 }
26802 else if (row->overlapping_p)
26803 {
26804 /* We must redraw a row overlapping the exposed area. */
26805 if (y0 < r.y
26806 ? y0 + row->phys_height > r.y
26807 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26808 {
26809 if (first_overlapping_row == NULL)
26810 first_overlapping_row = row;
26811 last_overlapping_row = row;
26812 }
26813 }
26814
26815 if (y1 >= yb)
26816 break;
26817 }
26818
26819 /* Display the mode line if there is one. */
26820 if (WINDOW_WANTS_MODELINE_P (w)
26821 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26822 row->enabled_p)
26823 && row->y < r.y + r.height)
26824 {
26825 if (expose_line (w, row, &r))
26826 mouse_face_overwritten_p = 1;
26827 }
26828
26829 if (!w->pseudo_window_p)
26830 {
26831 /* Fix the display of overlapping rows. */
26832 if (first_overlapping_row)
26833 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26834 fr);
26835
26836 /* Draw border between windows. */
26837 x_draw_vertical_border (w);
26838
26839 /* Turn the cursor on again. */
26840 if (cursor_cleared_p)
26841 update_window_cursor (w, 1);
26842 }
26843 }
26844
26845 return mouse_face_overwritten_p;
26846 }
26847
26848
26849
26850 /* Redraw (parts) of all windows in the window tree rooted at W that
26851 intersect R. R contains frame pixel coordinates. Value is
26852 non-zero if the exposure overwrites mouse-face. */
26853
26854 static int
26855 expose_window_tree (struct window *w, XRectangle *r)
26856 {
26857 struct frame *f = XFRAME (w->frame);
26858 int mouse_face_overwritten_p = 0;
26859
26860 while (w && !FRAME_GARBAGED_P (f))
26861 {
26862 if (!NILP (w->hchild))
26863 mouse_face_overwritten_p
26864 |= expose_window_tree (XWINDOW (w->hchild), r);
26865 else if (!NILP (w->vchild))
26866 mouse_face_overwritten_p
26867 |= expose_window_tree (XWINDOW (w->vchild), r);
26868 else
26869 mouse_face_overwritten_p |= expose_window (w, r);
26870
26871 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26872 }
26873
26874 return mouse_face_overwritten_p;
26875 }
26876
26877
26878 /* EXPORT:
26879 Redisplay an exposed area of frame F. X and Y are the upper-left
26880 corner of the exposed rectangle. W and H are width and height of
26881 the exposed area. All are pixel values. W or H zero means redraw
26882 the entire frame. */
26883
26884 void
26885 expose_frame (struct frame *f, int x, int y, int w, int h)
26886 {
26887 XRectangle r;
26888 int mouse_face_overwritten_p = 0;
26889
26890 TRACE ((stderr, "expose_frame "));
26891
26892 /* No need to redraw if frame will be redrawn soon. */
26893 if (FRAME_GARBAGED_P (f))
26894 {
26895 TRACE ((stderr, " garbaged\n"));
26896 return;
26897 }
26898
26899 /* If basic faces haven't been realized yet, there is no point in
26900 trying to redraw anything. This can happen when we get an expose
26901 event while Emacs is starting, e.g. by moving another window. */
26902 if (FRAME_FACE_CACHE (f) == NULL
26903 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26904 {
26905 TRACE ((stderr, " no faces\n"));
26906 return;
26907 }
26908
26909 if (w == 0 || h == 0)
26910 {
26911 r.x = r.y = 0;
26912 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26913 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26914 }
26915 else
26916 {
26917 r.x = x;
26918 r.y = y;
26919 r.width = w;
26920 r.height = h;
26921 }
26922
26923 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26924 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26925
26926 if (WINDOWP (f->tool_bar_window))
26927 mouse_face_overwritten_p
26928 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26929
26930 #ifdef HAVE_X_WINDOWS
26931 #ifndef MSDOS
26932 #ifndef USE_X_TOOLKIT
26933 if (WINDOWP (f->menu_bar_window))
26934 mouse_face_overwritten_p
26935 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26936 #endif /* not USE_X_TOOLKIT */
26937 #endif
26938 #endif
26939
26940 /* Some window managers support a focus-follows-mouse style with
26941 delayed raising of frames. Imagine a partially obscured frame,
26942 and moving the mouse into partially obscured mouse-face on that
26943 frame. The visible part of the mouse-face will be highlighted,
26944 then the WM raises the obscured frame. With at least one WM, KDE
26945 2.1, Emacs is not getting any event for the raising of the frame
26946 (even tried with SubstructureRedirectMask), only Expose events.
26947 These expose events will draw text normally, i.e. not
26948 highlighted. Which means we must redo the highlight here.
26949 Subsume it under ``we love X''. --gerd 2001-08-15 */
26950 /* Included in Windows version because Windows most likely does not
26951 do the right thing if any third party tool offers
26952 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26953 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26954 {
26955 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26956 if (f == hlinfo->mouse_face_mouse_frame)
26957 {
26958 int mouse_x = hlinfo->mouse_face_mouse_x;
26959 int mouse_y = hlinfo->mouse_face_mouse_y;
26960 clear_mouse_face (hlinfo);
26961 note_mouse_highlight (f, mouse_x, mouse_y);
26962 }
26963 }
26964 }
26965
26966
26967 /* EXPORT:
26968 Determine the intersection of two rectangles R1 and R2. Return
26969 the intersection in *RESULT. Value is non-zero if RESULT is not
26970 empty. */
26971
26972 int
26973 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26974 {
26975 XRectangle *left, *right;
26976 XRectangle *upper, *lower;
26977 int intersection_p = 0;
26978
26979 /* Rearrange so that R1 is the left-most rectangle. */
26980 if (r1->x < r2->x)
26981 left = r1, right = r2;
26982 else
26983 left = r2, right = r1;
26984
26985 /* X0 of the intersection is right.x0, if this is inside R1,
26986 otherwise there is no intersection. */
26987 if (right->x <= left->x + left->width)
26988 {
26989 result->x = right->x;
26990
26991 /* The right end of the intersection is the minimum of the
26992 the right ends of left and right. */
26993 result->width = (min (left->x + left->width, right->x + right->width)
26994 - result->x);
26995
26996 /* Same game for Y. */
26997 if (r1->y < r2->y)
26998 upper = r1, lower = r2;
26999 else
27000 upper = r2, lower = r1;
27001
27002 /* The upper end of the intersection is lower.y0, if this is inside
27003 of upper. Otherwise, there is no intersection. */
27004 if (lower->y <= upper->y + upper->height)
27005 {
27006 result->y = lower->y;
27007
27008 /* The lower end of the intersection is the minimum of the lower
27009 ends of upper and lower. */
27010 result->height = (min (lower->y + lower->height,
27011 upper->y + upper->height)
27012 - result->y);
27013 intersection_p = 1;
27014 }
27015 }
27016
27017 return intersection_p;
27018 }
27019
27020 #endif /* HAVE_WINDOW_SYSTEM */
27021
27022 \f
27023 /***********************************************************************
27024 Initialization
27025 ***********************************************************************/
27026
27027 void
27028 syms_of_xdisp (void)
27029 {
27030 Vwith_echo_area_save_vector = Qnil;
27031 staticpro (&Vwith_echo_area_save_vector);
27032
27033 Vmessage_stack = Qnil;
27034 staticpro (&Vmessage_stack);
27035
27036 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
27037 staticpro (&Qinhibit_redisplay);
27038
27039 message_dolog_marker1 = Fmake_marker ();
27040 staticpro (&message_dolog_marker1);
27041 message_dolog_marker2 = Fmake_marker ();
27042 staticpro (&message_dolog_marker2);
27043 message_dolog_marker3 = Fmake_marker ();
27044 staticpro (&message_dolog_marker3);
27045
27046 #if GLYPH_DEBUG
27047 defsubr (&Sdump_frame_glyph_matrix);
27048 defsubr (&Sdump_glyph_matrix);
27049 defsubr (&Sdump_glyph_row);
27050 defsubr (&Sdump_tool_bar_row);
27051 defsubr (&Strace_redisplay);
27052 defsubr (&Strace_to_stderr);
27053 #endif
27054 #ifdef HAVE_WINDOW_SYSTEM
27055 defsubr (&Stool_bar_lines_needed);
27056 defsubr (&Slookup_image_map);
27057 #endif
27058 defsubr (&Sformat_mode_line);
27059 defsubr (&Sinvisible_p);
27060 defsubr (&Scurrent_bidi_paragraph_direction);
27061
27062 staticpro (&Qmenu_bar_update_hook);
27063 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
27064
27065 staticpro (&Qoverriding_terminal_local_map);
27066 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
27067
27068 staticpro (&Qoverriding_local_map);
27069 Qoverriding_local_map = intern_c_string ("overriding-local-map");
27070
27071 staticpro (&Qwindow_scroll_functions);
27072 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
27073
27074 staticpro (&Qwindow_text_change_functions);
27075 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
27076
27077 staticpro (&Qredisplay_end_trigger_functions);
27078 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
27079
27080 staticpro (&Qinhibit_point_motion_hooks);
27081 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
27082
27083 Qeval = intern_c_string ("eval");
27084 staticpro (&Qeval);
27085
27086 QCdata = intern_c_string (":data");
27087 staticpro (&QCdata);
27088 Qdisplay = intern_c_string ("display");
27089 staticpro (&Qdisplay);
27090 Qspace_width = intern_c_string ("space-width");
27091 staticpro (&Qspace_width);
27092 Qraise = intern_c_string ("raise");
27093 staticpro (&Qraise);
27094 Qslice = intern_c_string ("slice");
27095 staticpro (&Qslice);
27096 Qspace = intern_c_string ("space");
27097 staticpro (&Qspace);
27098 Qmargin = intern_c_string ("margin");
27099 staticpro (&Qmargin);
27100 Qpointer = intern_c_string ("pointer");
27101 staticpro (&Qpointer);
27102 Qleft_margin = intern_c_string ("left-margin");
27103 staticpro (&Qleft_margin);
27104 Qright_margin = intern_c_string ("right-margin");
27105 staticpro (&Qright_margin);
27106 Qcenter = intern_c_string ("center");
27107 staticpro (&Qcenter);
27108 Qline_height = intern_c_string ("line-height");
27109 staticpro (&Qline_height);
27110 QCalign_to = intern_c_string (":align-to");
27111 staticpro (&QCalign_to);
27112 QCrelative_width = intern_c_string (":relative-width");
27113 staticpro (&QCrelative_width);
27114 QCrelative_height = intern_c_string (":relative-height");
27115 staticpro (&QCrelative_height);
27116 QCeval = intern_c_string (":eval");
27117 staticpro (&QCeval);
27118 QCpropertize = intern_c_string (":propertize");
27119 staticpro (&QCpropertize);
27120 QCfile = intern_c_string (":file");
27121 staticpro (&QCfile);
27122 Qfontified = intern_c_string ("fontified");
27123 staticpro (&Qfontified);
27124 Qfontification_functions = intern_c_string ("fontification-functions");
27125 staticpro (&Qfontification_functions);
27126 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
27127 staticpro (&Qtrailing_whitespace);
27128 Qescape_glyph = intern_c_string ("escape-glyph");
27129 staticpro (&Qescape_glyph);
27130 Qnobreak_space = intern_c_string ("nobreak-space");
27131 staticpro (&Qnobreak_space);
27132 Qimage = intern_c_string ("image");
27133 staticpro (&Qimage);
27134 Qtext = intern_c_string ("text");
27135 staticpro (&Qtext);
27136 Qboth = intern_c_string ("both");
27137 staticpro (&Qboth);
27138 Qboth_horiz = intern_c_string ("both-horiz");
27139 staticpro (&Qboth_horiz);
27140 Qtext_image_horiz = intern_c_string ("text-image-horiz");
27141 staticpro (&Qtext_image_horiz);
27142 QCmap = intern_c_string (":map");
27143 staticpro (&QCmap);
27144 QCpointer = intern_c_string (":pointer");
27145 staticpro (&QCpointer);
27146 Qrect = intern_c_string ("rect");
27147 staticpro (&Qrect);
27148 Qcircle = intern_c_string ("circle");
27149 staticpro (&Qcircle);
27150 Qpoly = intern_c_string ("poly");
27151 staticpro (&Qpoly);
27152 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
27153 staticpro (&Qmessage_truncate_lines);
27154 Qgrow_only = intern_c_string ("grow-only");
27155 staticpro (&Qgrow_only);
27156 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
27157 staticpro (&Qinhibit_menubar_update);
27158 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
27159 staticpro (&Qinhibit_eval_during_redisplay);
27160 Qposition = intern_c_string ("position");
27161 staticpro (&Qposition);
27162 Qbuffer_position = intern_c_string ("buffer-position");
27163 staticpro (&Qbuffer_position);
27164 Qobject = intern_c_string ("object");
27165 staticpro (&Qobject);
27166 Qbar = intern_c_string ("bar");
27167 staticpro (&Qbar);
27168 Qhbar = intern_c_string ("hbar");
27169 staticpro (&Qhbar);
27170 Qbox = intern_c_string ("box");
27171 staticpro (&Qbox);
27172 Qhollow = intern_c_string ("hollow");
27173 staticpro (&Qhollow);
27174 Qhand = intern_c_string ("hand");
27175 staticpro (&Qhand);
27176 Qarrow = intern_c_string ("arrow");
27177 staticpro (&Qarrow);
27178 Qtext = intern_c_string ("text");
27179 staticpro (&Qtext);
27180 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
27181 staticpro (&Qinhibit_free_realized_faces);
27182
27183 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27184 Fcons (intern_c_string ("void-variable"), Qnil)),
27185 Qnil);
27186 staticpro (&list_of_error);
27187
27188 Qlast_arrow_position = intern_c_string ("last-arrow-position");
27189 staticpro (&Qlast_arrow_position);
27190 Qlast_arrow_string = intern_c_string ("last-arrow-string");
27191 staticpro (&Qlast_arrow_string);
27192
27193 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
27194 staticpro (&Qoverlay_arrow_string);
27195 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
27196 staticpro (&Qoverlay_arrow_bitmap);
27197
27198 echo_buffer[0] = echo_buffer[1] = Qnil;
27199 staticpro (&echo_buffer[0]);
27200 staticpro (&echo_buffer[1]);
27201
27202 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27203 staticpro (&echo_area_buffer[0]);
27204 staticpro (&echo_area_buffer[1]);
27205
27206 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27207 staticpro (&Vmessages_buffer_name);
27208
27209 mode_line_proptrans_alist = Qnil;
27210 staticpro (&mode_line_proptrans_alist);
27211 mode_line_string_list = Qnil;
27212 staticpro (&mode_line_string_list);
27213 mode_line_string_face = Qnil;
27214 staticpro (&mode_line_string_face);
27215 mode_line_string_face_prop = Qnil;
27216 staticpro (&mode_line_string_face_prop);
27217 Vmode_line_unwind_vector = Qnil;
27218 staticpro (&Vmode_line_unwind_vector);
27219
27220 help_echo_string = Qnil;
27221 staticpro (&help_echo_string);
27222 help_echo_object = Qnil;
27223 staticpro (&help_echo_object);
27224 help_echo_window = Qnil;
27225 staticpro (&help_echo_window);
27226 previous_help_echo_string = Qnil;
27227 staticpro (&previous_help_echo_string);
27228 help_echo_pos = -1;
27229
27230 Qright_to_left = intern_c_string ("right-to-left");
27231 staticpro (&Qright_to_left);
27232 Qleft_to_right = intern_c_string ("left-to-right");
27233 staticpro (&Qleft_to_right);
27234
27235 #ifdef HAVE_WINDOW_SYSTEM
27236 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27237 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27238 For example, if a block cursor is over a tab, it will be drawn as
27239 wide as that tab on the display. */);
27240 x_stretch_cursor_p = 0;
27241 #endif
27242
27243 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27244 doc: /* *Non-nil means highlight trailing whitespace.
27245 The face used for trailing whitespace is `trailing-whitespace'. */);
27246 Vshow_trailing_whitespace = Qnil;
27247
27248 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27249 doc: /* *Control highlighting of nobreak space and soft hyphen.
27250 A value of t means highlight the character itself (for nobreak space,
27251 use face `nobreak-space').
27252 A value of nil means no highlighting.
27253 Other values mean display the escape glyph followed by an ordinary
27254 space or ordinary hyphen. */);
27255 Vnobreak_char_display = Qt;
27256
27257 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27258 doc: /* *The pointer shape to show in void text areas.
27259 A value of nil means to show the text pointer. Other options are `arrow',
27260 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27261 Vvoid_text_area_pointer = Qarrow;
27262
27263 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27264 doc: /* Non-nil means don't actually do any redisplay.
27265 This is used for internal purposes. */);
27266 Vinhibit_redisplay = Qnil;
27267
27268 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27269 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27270 Vglobal_mode_string = Qnil;
27271
27272 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27273 doc: /* Marker for where to display an arrow on top of the buffer text.
27274 This must be the beginning of a line in order to work.
27275 See also `overlay-arrow-string'. */);
27276 Voverlay_arrow_position = Qnil;
27277
27278 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27279 doc: /* String to display as an arrow in non-window frames.
27280 See also `overlay-arrow-position'. */);
27281 Voverlay_arrow_string = make_pure_c_string ("=>");
27282
27283 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27284 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27285 The symbols on this list are examined during redisplay to determine
27286 where to display overlay arrows. */);
27287 Voverlay_arrow_variable_list
27288 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27289
27290 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27291 doc: /* *The number of lines to try scrolling a window by when point moves out.
27292 If that fails to bring point back on frame, point is centered instead.
27293 If this is zero, point is always centered after it moves off frame.
27294 If you want scrolling to always be a line at a time, you should set
27295 `scroll-conservatively' to a large value rather than set this to 1. */);
27296
27297 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27298 doc: /* *Scroll up to this many lines, to bring point back on screen.
27299 If point moves off-screen, redisplay will scroll by up to
27300 `scroll-conservatively' lines in order to bring point just barely
27301 onto the screen again. If that cannot be done, then redisplay
27302 recenters point as usual.
27303
27304 If the value is greater than 100, redisplay will never recenter point,
27305 but will always scroll just enough text to bring point into view, even
27306 if you move far away.
27307
27308 A value of zero means always recenter point if it moves off screen. */);
27309 scroll_conservatively = 0;
27310
27311 DEFVAR_INT ("scroll-margin", scroll_margin,
27312 doc: /* *Number of lines of margin at the top and bottom of a window.
27313 Recenter the window whenever point gets within this many lines
27314 of the top or bottom of the window. */);
27315 scroll_margin = 0;
27316
27317 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27318 doc: /* Pixels per inch value for non-window system displays.
27319 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27320 Vdisplay_pixels_per_inch = make_float (72.0);
27321
27322 #if GLYPH_DEBUG
27323 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27324 #endif
27325
27326 DEFVAR_LISP ("truncate-partial-width-windows",
27327 Vtruncate_partial_width_windows,
27328 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27329 For an integer value, truncate lines in each window narrower than the
27330 full frame width, provided the window width is less than that integer;
27331 otherwise, respect the value of `truncate-lines'.
27332
27333 For any other non-nil value, truncate lines in all windows that do
27334 not span the full frame width.
27335
27336 A value of nil means to respect the value of `truncate-lines'.
27337
27338 If `word-wrap' is enabled, you might want to reduce this. */);
27339 Vtruncate_partial_width_windows = make_number (50);
27340
27341 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27342 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27343 Any other value means to use the appropriate face, `mode-line',
27344 `header-line', or `menu' respectively. */);
27345 mode_line_inverse_video = 1;
27346
27347 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27348 doc: /* *Maximum buffer size for which line number should be displayed.
27349 If the buffer is bigger than this, the line number does not appear
27350 in the mode line. A value of nil means no limit. */);
27351 Vline_number_display_limit = Qnil;
27352
27353 DEFVAR_INT ("line-number-display-limit-width",
27354 line_number_display_limit_width,
27355 doc: /* *Maximum line width (in characters) for line number display.
27356 If the average length of the lines near point is bigger than this, then the
27357 line number may be omitted from the mode line. */);
27358 line_number_display_limit_width = 200;
27359
27360 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27361 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27362 highlight_nonselected_windows = 0;
27363
27364 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27365 doc: /* Non-nil if more than one frame is visible on this display.
27366 Minibuffer-only frames don't count, but iconified frames do.
27367 This variable is not guaranteed to be accurate except while processing
27368 `frame-title-format' and `icon-title-format'. */);
27369
27370 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27371 doc: /* Template for displaying the title bar of visible frames.
27372 \(Assuming the window manager supports this feature.)
27373
27374 This variable has the same structure as `mode-line-format', except that
27375 the %c and %l constructs are ignored. It is used only on frames for
27376 which no explicit name has been set \(see `modify-frame-parameters'). */);
27377
27378 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27379 doc: /* Template for displaying the title bar of an iconified frame.
27380 \(Assuming the window manager supports this feature.)
27381 This variable has the same structure as `mode-line-format' (which see),
27382 and is used only on frames for which no explicit name has been set
27383 \(see `modify-frame-parameters'). */);
27384 Vicon_title_format
27385 = Vframe_title_format
27386 = pure_cons (intern_c_string ("multiple-frames"),
27387 pure_cons (make_pure_c_string ("%b"),
27388 pure_cons (pure_cons (empty_unibyte_string,
27389 pure_cons (intern_c_string ("invocation-name"),
27390 pure_cons (make_pure_c_string ("@"),
27391 pure_cons (intern_c_string ("system-name"),
27392 Qnil)))),
27393 Qnil)));
27394
27395 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27396 doc: /* Maximum number of lines to keep in the message log buffer.
27397 If nil, disable message logging. If t, log messages but don't truncate
27398 the buffer when it becomes large. */);
27399 Vmessage_log_max = make_number (100);
27400
27401 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27402 doc: /* Functions called before redisplay, if window sizes have changed.
27403 The value should be a list of functions that take one argument.
27404 Just before redisplay, for each frame, if any of its windows have changed
27405 size since the last redisplay, or have been split or deleted,
27406 all the functions in the list are called, with the frame as argument. */);
27407 Vwindow_size_change_functions = Qnil;
27408
27409 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27410 doc: /* List of functions to call before redisplaying a window with scrolling.
27411 Each function is called with two arguments, the window and its new
27412 display-start position. Note that these functions are also called by
27413 `set-window-buffer'. Also note that the value of `window-end' is not
27414 valid when these functions are called. */);
27415 Vwindow_scroll_functions = Qnil;
27416
27417 DEFVAR_LISP ("window-text-change-functions",
27418 Vwindow_text_change_functions,
27419 doc: /* Functions to call in redisplay when text in the window might change. */);
27420 Vwindow_text_change_functions = Qnil;
27421
27422 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27423 doc: /* Functions called when redisplay of a window reaches the end trigger.
27424 Each function is called with two arguments, the window and the end trigger value.
27425 See `set-window-redisplay-end-trigger'. */);
27426 Vredisplay_end_trigger_functions = Qnil;
27427
27428 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27429 doc: /* *Non-nil means autoselect window with mouse pointer.
27430 If nil, do not autoselect windows.
27431 A positive number means delay autoselection by that many seconds: a
27432 window is autoselected only after the mouse has remained in that
27433 window for the duration of the delay.
27434 A negative number has a similar effect, but causes windows to be
27435 autoselected only after the mouse has stopped moving. \(Because of
27436 the way Emacs compares mouse events, you will occasionally wait twice
27437 that time before the window gets selected.\)
27438 Any other value means to autoselect window instantaneously when the
27439 mouse pointer enters it.
27440
27441 Autoselection selects the minibuffer only if it is active, and never
27442 unselects the minibuffer if it is active.
27443
27444 When customizing this variable make sure that the actual value of
27445 `focus-follows-mouse' matches the behavior of your window manager. */);
27446 Vmouse_autoselect_window = Qnil;
27447
27448 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27449 doc: /* *Non-nil means automatically resize tool-bars.
27450 This dynamically changes the tool-bar's height to the minimum height
27451 that is needed to make all tool-bar items visible.
27452 If value is `grow-only', the tool-bar's height is only increased
27453 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27454 Vauto_resize_tool_bars = Qt;
27455
27456 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27457 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27458 auto_raise_tool_bar_buttons_p = 1;
27459
27460 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27461 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27462 make_cursor_line_fully_visible_p = 1;
27463
27464 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27465 doc: /* *Border below tool-bar in pixels.
27466 If an integer, use it as the height of the border.
27467 If it is one of `internal-border-width' or `border-width', use the
27468 value of the corresponding frame parameter.
27469 Otherwise, no border is added below the tool-bar. */);
27470 Vtool_bar_border = Qinternal_border_width;
27471
27472 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27473 doc: /* *Margin around tool-bar buttons in pixels.
27474 If an integer, use that for both horizontal and vertical margins.
27475 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27476 HORZ specifying the horizontal margin, and VERT specifying the
27477 vertical margin. */);
27478 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27479
27480 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27481 doc: /* *Relief thickness of tool-bar buttons. */);
27482 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27483
27484 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27485 doc: /* Tool bar style to use.
27486 It can be one of
27487 image - show images only
27488 text - show text only
27489 both - show both, text below image
27490 both-horiz - show text to the right of the image
27491 text-image-horiz - show text to the left of the image
27492 any other - use system default or image if no system default. */);
27493 Vtool_bar_style = Qnil;
27494
27495 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27496 doc: /* *Maximum number of characters a label can have to be shown.
27497 The tool bar style must also show labels for this to have any effect, see
27498 `tool-bar-style'. */);
27499 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27500
27501 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27502 doc: /* List of functions to call to fontify regions of text.
27503 Each function is called with one argument POS. Functions must
27504 fontify a region starting at POS in the current buffer, and give
27505 fontified regions the property `fontified'. */);
27506 Vfontification_functions = Qnil;
27507 Fmake_variable_buffer_local (Qfontification_functions);
27508
27509 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27510 unibyte_display_via_language_environment,
27511 doc: /* *Non-nil means display unibyte text according to language environment.
27512 Specifically, this means that raw bytes in the range 160-255 decimal
27513 are displayed by converting them to the equivalent multibyte characters
27514 according to the current language environment. As a result, they are
27515 displayed according to the current fontset.
27516
27517 Note that this variable affects only how these bytes are displayed,
27518 but does not change the fact they are interpreted as raw bytes. */);
27519 unibyte_display_via_language_environment = 0;
27520
27521 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27522 doc: /* *Maximum height for resizing mini-windows.
27523 If a float, it specifies a fraction of the mini-window frame's height.
27524 If an integer, it specifies a number of lines. */);
27525 Vmax_mini_window_height = make_float (0.25);
27526
27527 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27528 doc: /* *How to resize mini-windows.
27529 A value of nil means don't automatically resize mini-windows.
27530 A value of t means resize them to fit the text displayed in them.
27531 A value of `grow-only', the default, means let mini-windows grow
27532 only, until their display becomes empty, at which point the windows
27533 go back to their normal size. */);
27534 Vresize_mini_windows = Qgrow_only;
27535
27536 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27537 doc: /* Alist specifying how to blink the cursor off.
27538 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27539 `cursor-type' frame-parameter or variable equals ON-STATE,
27540 comparing using `equal', Emacs uses OFF-STATE to specify
27541 how to blink it off. ON-STATE and OFF-STATE are values for
27542 the `cursor-type' frame parameter.
27543
27544 If a frame's ON-STATE has no entry in this list,
27545 the frame's other specifications determine how to blink the cursor off. */);
27546 Vblink_cursor_alist = Qnil;
27547
27548 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27549 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27550 If non-nil, windows are automatically scrolled horizontally to make
27551 point visible. */);
27552 automatic_hscrolling_p = 1;
27553 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
27554 staticpro (&Qauto_hscroll_mode);
27555
27556 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27557 doc: /* *How many columns away from the window edge point is allowed to get
27558 before automatic hscrolling will horizontally scroll the window. */);
27559 hscroll_margin = 5;
27560
27561 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27562 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27563 When point is less than `hscroll-margin' columns from the window
27564 edge, automatic hscrolling will scroll the window by the amount of columns
27565 determined by this variable. If its value is a positive integer, scroll that
27566 many columns. If it's a positive floating-point number, it specifies the
27567 fraction of the window's width to scroll. If it's nil or zero, point will be
27568 centered horizontally after the scroll. Any other value, including negative
27569 numbers, are treated as if the value were zero.
27570
27571 Automatic hscrolling always moves point outside the scroll margin, so if
27572 point was more than scroll step columns inside the margin, the window will
27573 scroll more than the value given by the scroll step.
27574
27575 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27576 and `scroll-right' overrides this variable's effect. */);
27577 Vhscroll_step = make_number (0);
27578
27579 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27580 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27581 Bind this around calls to `message' to let it take effect. */);
27582 message_truncate_lines = 0;
27583
27584 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27585 doc: /* Normal hook run to update the menu bar definitions.
27586 Redisplay runs this hook before it redisplays the menu bar.
27587 This is used to update submenus such as Buffers,
27588 whose contents depend on various data. */);
27589 Vmenu_bar_update_hook = Qnil;
27590
27591 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27592 doc: /* Frame for which we are updating a menu.
27593 The enable predicate for a menu binding should check this variable. */);
27594 Vmenu_updating_frame = Qnil;
27595
27596 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27597 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27598 inhibit_menubar_update = 0;
27599
27600 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27601 doc: /* Prefix prepended to all continuation lines at display time.
27602 The value may be a string, an image, or a stretch-glyph; it is
27603 interpreted in the same way as the value of a `display' text property.
27604
27605 This variable is overridden by any `wrap-prefix' text or overlay
27606 property.
27607
27608 To add a prefix to non-continuation lines, use `line-prefix'. */);
27609 Vwrap_prefix = Qnil;
27610 staticpro (&Qwrap_prefix);
27611 Qwrap_prefix = intern_c_string ("wrap-prefix");
27612 Fmake_variable_buffer_local (Qwrap_prefix);
27613
27614 DEFVAR_LISP ("line-prefix", Vline_prefix,
27615 doc: /* Prefix prepended to all non-continuation lines at display time.
27616 The value may be a string, an image, or a stretch-glyph; it is
27617 interpreted in the same way as the value of a `display' text property.
27618
27619 This variable is overridden by any `line-prefix' text or overlay
27620 property.
27621
27622 To add a prefix to continuation lines, use `wrap-prefix'. */);
27623 Vline_prefix = Qnil;
27624 staticpro (&Qline_prefix);
27625 Qline_prefix = intern_c_string ("line-prefix");
27626 Fmake_variable_buffer_local (Qline_prefix);
27627
27628 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27629 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27630 inhibit_eval_during_redisplay = 0;
27631
27632 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27633 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27634 inhibit_free_realized_faces = 0;
27635
27636 #if GLYPH_DEBUG
27637 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27638 doc: /* Inhibit try_window_id display optimization. */);
27639 inhibit_try_window_id = 0;
27640
27641 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27642 doc: /* Inhibit try_window_reusing display optimization. */);
27643 inhibit_try_window_reusing = 0;
27644
27645 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27646 doc: /* Inhibit try_cursor_movement display optimization. */);
27647 inhibit_try_cursor_movement = 0;
27648 #endif /* GLYPH_DEBUG */
27649
27650 DEFVAR_INT ("overline-margin", overline_margin,
27651 doc: /* *Space between overline and text, in pixels.
27652 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27653 margin to the caracter height. */);
27654 overline_margin = 2;
27655
27656 DEFVAR_INT ("underline-minimum-offset",
27657 underline_minimum_offset,
27658 doc: /* Minimum distance between baseline and underline.
27659 This can improve legibility of underlined text at small font sizes,
27660 particularly when using variable `x-use-underline-position-properties'
27661 with fonts that specify an UNDERLINE_POSITION relatively close to the
27662 baseline. The default value is 1. */);
27663 underline_minimum_offset = 1;
27664
27665 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27666 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27667 This feature only works when on a window system that can change
27668 cursor shapes. */);
27669 display_hourglass_p = 1;
27670
27671 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27672 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27673 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27674
27675 hourglass_atimer = NULL;
27676 hourglass_shown_p = 0;
27677
27678 DEFSYM (Qglyphless_char, "glyphless-char");
27679 DEFSYM (Qhex_code, "hex-code");
27680 DEFSYM (Qempty_box, "empty-box");
27681 DEFSYM (Qthin_space, "thin-space");
27682 DEFSYM (Qzero_width, "zero-width");
27683
27684 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27685 /* Intern this now in case it isn't already done.
27686 Setting this variable twice is harmless.
27687 But don't staticpro it here--that is done in alloc.c. */
27688 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27689 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27690
27691 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27692 doc: /* Char-table defining glyphless characters.
27693 Each element, if non-nil, should be one of the following:
27694 an ASCII acronym string: display this string in a box
27695 `hex-code': display the hexadecimal code of a character in a box
27696 `empty-box': display as an empty box
27697 `thin-space': display as 1-pixel width space
27698 `zero-width': don't display
27699 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27700 display method for graphical terminals and text terminals respectively.
27701 GRAPHICAL and TEXT should each have one of the values listed above.
27702
27703 The char-table has one extra slot to control the display of a character for
27704 which no font is found. This slot only takes effect on graphical terminals.
27705 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27706 `thin-space'. The default is `empty-box'. */);
27707 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27708 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27709 Qempty_box);
27710 }
27711
27712
27713 /* Initialize this module when Emacs starts. */
27714
27715 void
27716 init_xdisp (void)
27717 {
27718 Lisp_Object root_window;
27719 struct window *mini_w;
27720
27721 current_header_line_height = current_mode_line_height = -1;
27722
27723 CHARPOS (this_line_start_pos) = 0;
27724
27725 mini_w = XWINDOW (minibuf_window);
27726 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27727 echo_area_window = minibuf_window;
27728
27729 if (!noninteractive)
27730 {
27731 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27732 int i;
27733
27734 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27735 set_window_height (root_window,
27736 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27737 0);
27738 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27739 set_window_height (minibuf_window, 1, 0);
27740
27741 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27742 mini_w->total_cols = make_number (FRAME_COLS (f));
27743
27744 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27745 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27746 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27747
27748 /* The default ellipsis glyphs `...'. */
27749 for (i = 0; i < 3; ++i)
27750 default_invis_vector[i] = make_number ('.');
27751 }
27752
27753 {
27754 /* Allocate the buffer for frame titles.
27755 Also used for `format-mode-line'. */
27756 int size = 100;
27757 mode_line_noprop_buf = (char *) xmalloc (size);
27758 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27759 mode_line_noprop_ptr = mode_line_noprop_buf;
27760 mode_line_target = MODE_LINE_DISPLAY;
27761 }
27762
27763 help_echo_showing_p = 0;
27764 }
27765
27766 /* Since w32 does not support atimers, it defines its own implementation of
27767 the following three functions in w32fns.c. */
27768 #ifndef WINDOWSNT
27769
27770 /* Platform-independent portion of hourglass implementation. */
27771
27772 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27773 int
27774 hourglass_started (void)
27775 {
27776 return hourglass_shown_p || hourglass_atimer != NULL;
27777 }
27778
27779 /* Cancel a currently active hourglass timer, and start a new one. */
27780 void
27781 start_hourglass (void)
27782 {
27783 #if defined (HAVE_WINDOW_SYSTEM)
27784 EMACS_TIME delay;
27785 int secs, usecs = 0;
27786
27787 cancel_hourglass ();
27788
27789 if (INTEGERP (Vhourglass_delay)
27790 && XINT (Vhourglass_delay) > 0)
27791 secs = XFASTINT (Vhourglass_delay);
27792 else if (FLOATP (Vhourglass_delay)
27793 && XFLOAT_DATA (Vhourglass_delay) > 0)
27794 {
27795 Lisp_Object tem;
27796 tem = Ftruncate (Vhourglass_delay, Qnil);
27797 secs = XFASTINT (tem);
27798 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27799 }
27800 else
27801 secs = DEFAULT_HOURGLASS_DELAY;
27802
27803 EMACS_SET_SECS_USECS (delay, secs, usecs);
27804 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27805 show_hourglass, NULL);
27806 #endif
27807 }
27808
27809
27810 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27811 shown. */
27812 void
27813 cancel_hourglass (void)
27814 {
27815 #if defined (HAVE_WINDOW_SYSTEM)
27816 if (hourglass_atimer)
27817 {
27818 cancel_atimer (hourglass_atimer);
27819 hourglass_atimer = NULL;
27820 }
27821
27822 if (hourglass_shown_p)
27823 hide_hourglass ();
27824 #endif
27825 }
27826 #endif /* ! WINDOWSNT */