Add comments for forced L2R directions of menu bar and tool bar.
[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->string_from_display_prop_p = 1;
4486 /* Say that we haven't consumed the characters with
4487 `display' property yet. The call to pop_it in
4488 set_iterator_to_next will clean this up. */
4489 if (BUFFERP (object))
4490 *position = start_pos;
4491 }
4492 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4493 {
4494 it->method = GET_FROM_STRETCH;
4495 it->object = value;
4496 *position = it->position = start_pos;
4497 }
4498 #ifdef HAVE_WINDOW_SYSTEM
4499 else
4500 {
4501 it->what = IT_IMAGE;
4502 it->image_id = lookup_image (it->f, value);
4503 it->position = start_pos;
4504 it->object = NILP (object) ? it->w->buffer : object;
4505 it->method = GET_FROM_IMAGE;
4506
4507 /* Say that we haven't consumed the characters with
4508 `display' property yet. The call to pop_it in
4509 set_iterator_to_next will clean this up. */
4510 *position = start_pos;
4511 }
4512 #endif /* HAVE_WINDOW_SYSTEM */
4513
4514 return 1;
4515 }
4516
4517 /* Invalid property or property not supported. Restore
4518 POSITION to what it was before. */
4519 *position = start_pos;
4520 return 0;
4521 }
4522
4523 /* Check if PROP is a display property value whose text should be
4524 treated as intangible. OVERLAY is the overlay from which PROP
4525 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4526 specify the buffer position covered by PROP. */
4527
4528 int
4529 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4530 EMACS_INT charpos, EMACS_INT bytepos)
4531 {
4532 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4533 struct text_pos position;
4534
4535 SET_TEXT_POS (position, charpos, bytepos);
4536 return handle_display_spec (NULL, prop, Qnil, overlay,
4537 &position, charpos, frame_window_p);
4538 }
4539
4540
4541 /* Return 1 if PROP is a display sub-property value containing STRING.
4542
4543 Implementation note: this and the following function are really
4544 special cases of handle_display_spec and
4545 handle_single_display_spec, and should ideally use the same code.
4546 Until they do, these two pairs must be consistent and must be
4547 modified in sync. */
4548
4549 static int
4550 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4551 {
4552 if (EQ (string, prop))
4553 return 1;
4554
4555 /* Skip over `when FORM'. */
4556 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4557 {
4558 prop = XCDR (prop);
4559 if (!CONSP (prop))
4560 return 0;
4561 /* Actually, the condition following `when' should be eval'ed,
4562 like handle_single_display_spec does, and we should return
4563 zero if it evaluates to nil. However, this function is
4564 called only when the buffer was already displayed and some
4565 glyph in the glyph matrix was found to come from a display
4566 string. Therefore, the condition was already evaluated, and
4567 the result was non-nil, otherwise the display string wouldn't
4568 have been displayed and we would have never been called for
4569 this property. Thus, we can skip the evaluation and assume
4570 its result is non-nil. */
4571 prop = XCDR (prop);
4572 }
4573
4574 if (CONSP (prop))
4575 /* Skip over `margin LOCATION'. */
4576 if (EQ (XCAR (prop), Qmargin))
4577 {
4578 prop = XCDR (prop);
4579 if (!CONSP (prop))
4580 return 0;
4581
4582 prop = XCDR (prop);
4583 if (!CONSP (prop))
4584 return 0;
4585 }
4586
4587 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4588 }
4589
4590
4591 /* Return 1 if STRING appears in the `display' property PROP. */
4592
4593 static int
4594 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4595 {
4596 if (CONSP (prop)
4597 && !EQ (XCAR (prop), Qwhen)
4598 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4599 {
4600 /* A list of sub-properties. */
4601 while (CONSP (prop))
4602 {
4603 if (single_display_spec_string_p (XCAR (prop), string))
4604 return 1;
4605 prop = XCDR (prop);
4606 }
4607 }
4608 else if (VECTORP (prop))
4609 {
4610 /* A vector of sub-properties. */
4611 int i;
4612 for (i = 0; i < ASIZE (prop); ++i)
4613 if (single_display_spec_string_p (AREF (prop, i), string))
4614 return 1;
4615 }
4616 else
4617 return single_display_spec_string_p (prop, string);
4618
4619 return 0;
4620 }
4621
4622 /* Look for STRING in overlays and text properties in the current
4623 buffer, between character positions FROM and TO (excluding TO).
4624 BACK_P non-zero means look back (in this case, TO is supposed to be
4625 less than FROM).
4626 Value is the first character position where STRING was found, or
4627 zero if it wasn't found before hitting TO.
4628
4629 This function may only use code that doesn't eval because it is
4630 called asynchronously from note_mouse_highlight. */
4631
4632 static EMACS_INT
4633 string_buffer_position_lim (Lisp_Object string,
4634 EMACS_INT from, EMACS_INT to, int back_p)
4635 {
4636 Lisp_Object limit, prop, pos;
4637 int found = 0;
4638
4639 pos = make_number (from);
4640
4641 if (!back_p) /* looking forward */
4642 {
4643 limit = make_number (min (to, ZV));
4644 while (!found && !EQ (pos, limit))
4645 {
4646 prop = Fget_char_property (pos, Qdisplay, Qnil);
4647 if (!NILP (prop) && display_prop_string_p (prop, string))
4648 found = 1;
4649 else
4650 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4651 limit);
4652 }
4653 }
4654 else /* looking back */
4655 {
4656 limit = make_number (max (to, BEGV));
4657 while (!found && !EQ (pos, limit))
4658 {
4659 prop = Fget_char_property (pos, Qdisplay, Qnil);
4660 if (!NILP (prop) && display_prop_string_p (prop, string))
4661 found = 1;
4662 else
4663 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4664 limit);
4665 }
4666 }
4667
4668 return found ? XINT (pos) : 0;
4669 }
4670
4671 /* Determine which buffer position in current buffer STRING comes from.
4672 AROUND_CHARPOS is an approximate position where it could come from.
4673 Value is the buffer position or 0 if it couldn't be determined.
4674
4675 This function is necessary because we don't record buffer positions
4676 in glyphs generated from strings (to keep struct glyph small).
4677 This function may only use code that doesn't eval because it is
4678 called asynchronously from note_mouse_highlight. */
4679
4680 static EMACS_INT
4681 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4682 {
4683 const int MAX_DISTANCE = 1000;
4684 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4685 around_charpos + MAX_DISTANCE,
4686 0);
4687
4688 if (!found)
4689 found = string_buffer_position_lim (string, around_charpos,
4690 around_charpos - MAX_DISTANCE, 1);
4691 return found;
4692 }
4693
4694
4695 \f
4696 /***********************************************************************
4697 `composition' property
4698 ***********************************************************************/
4699
4700 /* Set up iterator IT from `composition' property at its current
4701 position. Called from handle_stop. */
4702
4703 static enum prop_handled
4704 handle_composition_prop (struct it *it)
4705 {
4706 Lisp_Object prop, string;
4707 EMACS_INT pos, pos_byte, start, end;
4708
4709 if (STRINGP (it->string))
4710 {
4711 unsigned char *s;
4712
4713 pos = IT_STRING_CHARPOS (*it);
4714 pos_byte = IT_STRING_BYTEPOS (*it);
4715 string = it->string;
4716 s = SDATA (string) + pos_byte;
4717 it->c = STRING_CHAR (s);
4718 }
4719 else
4720 {
4721 pos = IT_CHARPOS (*it);
4722 pos_byte = IT_BYTEPOS (*it);
4723 string = Qnil;
4724 it->c = FETCH_CHAR (pos_byte);
4725 }
4726
4727 /* If there's a valid composition and point is not inside of the
4728 composition (in the case that the composition is from the current
4729 buffer), draw a glyph composed from the composition components. */
4730 if (find_composition (pos, -1, &start, &end, &prop, string)
4731 && COMPOSITION_VALID_P (start, end, prop)
4732 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4733 {
4734 if (start != pos)
4735 {
4736 if (STRINGP (it->string))
4737 pos_byte = string_char_to_byte (it->string, start);
4738 else
4739 pos_byte = CHAR_TO_BYTE (start);
4740 }
4741 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4742 prop, string);
4743
4744 if (it->cmp_it.id >= 0)
4745 {
4746 it->cmp_it.ch = -1;
4747 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4748 it->cmp_it.nglyphs = -1;
4749 }
4750 }
4751
4752 return HANDLED_NORMALLY;
4753 }
4754
4755
4756 \f
4757 /***********************************************************************
4758 Overlay strings
4759 ***********************************************************************/
4760
4761 /* The following structure is used to record overlay strings for
4762 later sorting in load_overlay_strings. */
4763
4764 struct overlay_entry
4765 {
4766 Lisp_Object overlay;
4767 Lisp_Object string;
4768 int priority;
4769 int after_string_p;
4770 };
4771
4772
4773 /* Set up iterator IT from overlay strings at its current position.
4774 Called from handle_stop. */
4775
4776 static enum prop_handled
4777 handle_overlay_change (struct it *it)
4778 {
4779 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4780 return HANDLED_RECOMPUTE_PROPS;
4781 else
4782 return HANDLED_NORMALLY;
4783 }
4784
4785
4786 /* Set up the next overlay string for delivery by IT, if there is an
4787 overlay string to deliver. Called by set_iterator_to_next when the
4788 end of the current overlay string is reached. If there are more
4789 overlay strings to display, IT->string and
4790 IT->current.overlay_string_index are set appropriately here.
4791 Otherwise IT->string is set to nil. */
4792
4793 static void
4794 next_overlay_string (struct it *it)
4795 {
4796 ++it->current.overlay_string_index;
4797 if (it->current.overlay_string_index == it->n_overlay_strings)
4798 {
4799 /* No more overlay strings. Restore IT's settings to what
4800 they were before overlay strings were processed, and
4801 continue to deliver from current_buffer. */
4802
4803 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4804 pop_it (it);
4805 xassert (it->sp > 0
4806 || (NILP (it->string)
4807 && it->method == GET_FROM_BUFFER
4808 && it->stop_charpos >= BEGV
4809 && it->stop_charpos <= it->end_charpos));
4810 it->current.overlay_string_index = -1;
4811 it->n_overlay_strings = 0;
4812 it->overlay_strings_charpos = -1;
4813
4814 /* If we're at the end of the buffer, record that we have
4815 processed the overlay strings there already, so that
4816 next_element_from_buffer doesn't try it again. */
4817 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4818 it->overlay_strings_at_end_processed_p = 1;
4819 }
4820 else
4821 {
4822 /* There are more overlay strings to process. If
4823 IT->current.overlay_string_index has advanced to a position
4824 where we must load IT->overlay_strings with more strings, do
4825 it. We must load at the IT->overlay_strings_charpos where
4826 IT->n_overlay_strings was originally computed; when invisible
4827 text is present, this might not be IT_CHARPOS (Bug#7016). */
4828 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4829
4830 if (it->current.overlay_string_index && i == 0)
4831 load_overlay_strings (it, it->overlay_strings_charpos);
4832
4833 /* Initialize IT to deliver display elements from the overlay
4834 string. */
4835 it->string = it->overlay_strings[i];
4836 it->multibyte_p = STRING_MULTIBYTE (it->string);
4837 SET_TEXT_POS (it->current.string_pos, 0, 0);
4838 it->method = GET_FROM_STRING;
4839 it->stop_charpos = 0;
4840 if (it->cmp_it.stop_pos >= 0)
4841 it->cmp_it.stop_pos = 0;
4842 }
4843
4844 CHECK_IT (it);
4845 }
4846
4847
4848 /* Compare two overlay_entry structures E1 and E2. Used as a
4849 comparison function for qsort in load_overlay_strings. Overlay
4850 strings for the same position are sorted so that
4851
4852 1. All after-strings come in front of before-strings, except
4853 when they come from the same overlay.
4854
4855 2. Within after-strings, strings are sorted so that overlay strings
4856 from overlays with higher priorities come first.
4857
4858 2. Within before-strings, strings are sorted so that overlay
4859 strings from overlays with higher priorities come last.
4860
4861 Value is analogous to strcmp. */
4862
4863
4864 static int
4865 compare_overlay_entries (const void *e1, const void *e2)
4866 {
4867 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4868 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4869 int result;
4870
4871 if (entry1->after_string_p != entry2->after_string_p)
4872 {
4873 /* Let after-strings appear in front of before-strings if
4874 they come from different overlays. */
4875 if (EQ (entry1->overlay, entry2->overlay))
4876 result = entry1->after_string_p ? 1 : -1;
4877 else
4878 result = entry1->after_string_p ? -1 : 1;
4879 }
4880 else if (entry1->after_string_p)
4881 /* After-strings sorted in order of decreasing priority. */
4882 result = entry2->priority - entry1->priority;
4883 else
4884 /* Before-strings sorted in order of increasing priority. */
4885 result = entry1->priority - entry2->priority;
4886
4887 return result;
4888 }
4889
4890
4891 /* Load the vector IT->overlay_strings with overlay strings from IT's
4892 current buffer position, or from CHARPOS if that is > 0. Set
4893 IT->n_overlays to the total number of overlay strings found.
4894
4895 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4896 a time. On entry into load_overlay_strings,
4897 IT->current.overlay_string_index gives the number of overlay
4898 strings that have already been loaded by previous calls to this
4899 function.
4900
4901 IT->add_overlay_start contains an additional overlay start
4902 position to consider for taking overlay strings from, if non-zero.
4903 This position comes into play when the overlay has an `invisible'
4904 property, and both before and after-strings. When we've skipped to
4905 the end of the overlay, because of its `invisible' property, we
4906 nevertheless want its before-string to appear.
4907 IT->add_overlay_start will contain the overlay start position
4908 in this case.
4909
4910 Overlay strings are sorted so that after-string strings come in
4911 front of before-string strings. Within before and after-strings,
4912 strings are sorted by overlay priority. See also function
4913 compare_overlay_entries. */
4914
4915 static void
4916 load_overlay_strings (struct it *it, EMACS_INT charpos)
4917 {
4918 Lisp_Object overlay, window, str, invisible;
4919 struct Lisp_Overlay *ov;
4920 EMACS_INT start, end;
4921 int size = 20;
4922 int n = 0, i, j, invis_p;
4923 struct overlay_entry *entries
4924 = (struct overlay_entry *) alloca (size * sizeof *entries);
4925
4926 if (charpos <= 0)
4927 charpos = IT_CHARPOS (*it);
4928
4929 /* Append the overlay string STRING of overlay OVERLAY to vector
4930 `entries' which has size `size' and currently contains `n'
4931 elements. AFTER_P non-zero means STRING is an after-string of
4932 OVERLAY. */
4933 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4934 do \
4935 { \
4936 Lisp_Object priority; \
4937 \
4938 if (n == size) \
4939 { \
4940 int new_size = 2 * size; \
4941 struct overlay_entry *old = entries; \
4942 entries = \
4943 (struct overlay_entry *) alloca (new_size \
4944 * sizeof *entries); \
4945 memcpy (entries, old, size * sizeof *entries); \
4946 size = new_size; \
4947 } \
4948 \
4949 entries[n].string = (STRING); \
4950 entries[n].overlay = (OVERLAY); \
4951 priority = Foverlay_get ((OVERLAY), Qpriority); \
4952 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4953 entries[n].after_string_p = (AFTER_P); \
4954 ++n; \
4955 } \
4956 while (0)
4957
4958 /* Process overlay before the overlay center. */
4959 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4960 {
4961 XSETMISC (overlay, ov);
4962 xassert (OVERLAYP (overlay));
4963 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4964 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4965
4966 if (end < charpos)
4967 break;
4968
4969 /* Skip this overlay if it doesn't start or end at IT's current
4970 position. */
4971 if (end != charpos && start != charpos)
4972 continue;
4973
4974 /* Skip this overlay if it doesn't apply to IT->w. */
4975 window = Foverlay_get (overlay, Qwindow);
4976 if (WINDOWP (window) && XWINDOW (window) != it->w)
4977 continue;
4978
4979 /* If the text ``under'' the overlay is invisible, both before-
4980 and after-strings from this overlay are visible; start and
4981 end position are indistinguishable. */
4982 invisible = Foverlay_get (overlay, Qinvisible);
4983 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4984
4985 /* If overlay has a non-empty before-string, record it. */
4986 if ((start == charpos || (end == charpos && invis_p))
4987 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4988 && SCHARS (str))
4989 RECORD_OVERLAY_STRING (overlay, str, 0);
4990
4991 /* If overlay has a non-empty after-string, record it. */
4992 if ((end == charpos || (start == charpos && invis_p))
4993 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4994 && SCHARS (str))
4995 RECORD_OVERLAY_STRING (overlay, str, 1);
4996 }
4997
4998 /* Process overlays after the overlay center. */
4999 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5000 {
5001 XSETMISC (overlay, ov);
5002 xassert (OVERLAYP (overlay));
5003 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5004 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5005
5006 if (start > charpos)
5007 break;
5008
5009 /* Skip this overlay if it doesn't start or end at IT's current
5010 position. */
5011 if (end != charpos && start != charpos)
5012 continue;
5013
5014 /* Skip this overlay if it doesn't apply to IT->w. */
5015 window = Foverlay_get (overlay, Qwindow);
5016 if (WINDOWP (window) && XWINDOW (window) != it->w)
5017 continue;
5018
5019 /* If the text ``under'' the overlay is invisible, it has a zero
5020 dimension, and both before- and after-strings apply. */
5021 invisible = Foverlay_get (overlay, Qinvisible);
5022 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5023
5024 /* If overlay has a non-empty before-string, record it. */
5025 if ((start == charpos || (end == charpos && invis_p))
5026 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5027 && SCHARS (str))
5028 RECORD_OVERLAY_STRING (overlay, str, 0);
5029
5030 /* If overlay has a non-empty after-string, record it. */
5031 if ((end == charpos || (start == charpos && invis_p))
5032 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5033 && SCHARS (str))
5034 RECORD_OVERLAY_STRING (overlay, str, 1);
5035 }
5036
5037 #undef RECORD_OVERLAY_STRING
5038
5039 /* Sort entries. */
5040 if (n > 1)
5041 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5042
5043 /* Record number of overlay strings, and where we computed it. */
5044 it->n_overlay_strings = n;
5045 it->overlay_strings_charpos = charpos;
5046
5047 /* IT->current.overlay_string_index is the number of overlay strings
5048 that have already been consumed by IT. Copy some of the
5049 remaining overlay strings to IT->overlay_strings. */
5050 i = 0;
5051 j = it->current.overlay_string_index;
5052 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5053 {
5054 it->overlay_strings[i] = entries[j].string;
5055 it->string_overlays[i++] = entries[j++].overlay;
5056 }
5057
5058 CHECK_IT (it);
5059 }
5060
5061
5062 /* Get the first chunk of overlay strings at IT's current buffer
5063 position, or at CHARPOS if that is > 0. Value is non-zero if at
5064 least one overlay string was found. */
5065
5066 static int
5067 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5068 {
5069 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5070 process. This fills IT->overlay_strings with strings, and sets
5071 IT->n_overlay_strings to the total number of strings to process.
5072 IT->pos.overlay_string_index has to be set temporarily to zero
5073 because load_overlay_strings needs this; it must be set to -1
5074 when no overlay strings are found because a zero value would
5075 indicate a position in the first overlay string. */
5076 it->current.overlay_string_index = 0;
5077 load_overlay_strings (it, charpos);
5078
5079 /* If we found overlay strings, set up IT to deliver display
5080 elements from the first one. Otherwise set up IT to deliver
5081 from current_buffer. */
5082 if (it->n_overlay_strings)
5083 {
5084 /* Make sure we know settings in current_buffer, so that we can
5085 restore meaningful values when we're done with the overlay
5086 strings. */
5087 if (compute_stop_p)
5088 compute_stop_pos (it);
5089 xassert (it->face_id >= 0);
5090
5091 /* Save IT's settings. They are restored after all overlay
5092 strings have been processed. */
5093 xassert (!compute_stop_p || it->sp == 0);
5094
5095 /* When called from handle_stop, there might be an empty display
5096 string loaded. In that case, don't bother saving it. */
5097 if (!STRINGP (it->string) || SCHARS (it->string))
5098 push_it (it, NULL);
5099
5100 /* Set up IT to deliver display elements from the first overlay
5101 string. */
5102 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5103 it->string = it->overlay_strings[0];
5104 it->from_overlay = Qnil;
5105 it->stop_charpos = 0;
5106 xassert (STRINGP (it->string));
5107 it->end_charpos = SCHARS (it->string);
5108 it->multibyte_p = STRING_MULTIBYTE (it->string);
5109 it->method = GET_FROM_STRING;
5110 return 1;
5111 }
5112
5113 it->current.overlay_string_index = -1;
5114 return 0;
5115 }
5116
5117 static int
5118 get_overlay_strings (struct it *it, EMACS_INT charpos)
5119 {
5120 it->string = Qnil;
5121 it->method = GET_FROM_BUFFER;
5122
5123 (void) get_overlay_strings_1 (it, charpos, 1);
5124
5125 CHECK_IT (it);
5126
5127 /* Value is non-zero if we found at least one overlay string. */
5128 return STRINGP (it->string);
5129 }
5130
5131
5132 \f
5133 /***********************************************************************
5134 Saving and restoring state
5135 ***********************************************************************/
5136
5137 /* Save current settings of IT on IT->stack. Called, for example,
5138 before setting up IT for an overlay string, to be able to restore
5139 IT's settings to what they were after the overlay string has been
5140 processed. If POSITION is non-NULL, it is the position to save on
5141 the stack instead of IT->position. */
5142
5143 static void
5144 push_it (struct it *it, struct text_pos *position)
5145 {
5146 struct iterator_stack_entry *p;
5147
5148 xassert (it->sp < IT_STACK_SIZE);
5149 p = it->stack + it->sp;
5150
5151 p->stop_charpos = it->stop_charpos;
5152 p->prev_stop = it->prev_stop;
5153 p->base_level_stop = it->base_level_stop;
5154 p->cmp_it = it->cmp_it;
5155 xassert (it->face_id >= 0);
5156 p->face_id = it->face_id;
5157 p->string = it->string;
5158 p->method = it->method;
5159 p->from_overlay = it->from_overlay;
5160 switch (p->method)
5161 {
5162 case GET_FROM_IMAGE:
5163 p->u.image.object = it->object;
5164 p->u.image.image_id = it->image_id;
5165 p->u.image.slice = it->slice;
5166 break;
5167 case GET_FROM_STRETCH:
5168 p->u.stretch.object = it->object;
5169 break;
5170 }
5171 p->position = position ? *position : it->position;
5172 p->current = it->current;
5173 p->end_charpos = it->end_charpos;
5174 p->string_nchars = it->string_nchars;
5175 p->area = it->area;
5176 p->multibyte_p = it->multibyte_p;
5177 p->avoid_cursor_p = it->avoid_cursor_p;
5178 p->space_width = it->space_width;
5179 p->font_height = it->font_height;
5180 p->voffset = it->voffset;
5181 p->string_from_display_prop_p = it->string_from_display_prop_p;
5182 p->display_ellipsis_p = 0;
5183 p->line_wrap = it->line_wrap;
5184 ++it->sp;
5185 }
5186
5187 static void
5188 iterate_out_of_display_property (struct it *it)
5189 {
5190 /* Maybe initialize paragraph direction. If we are at the beginning
5191 of a new paragraph, next_element_from_buffer may not have a
5192 chance to do that. */
5193 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5194 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5195 /* prev_stop can be zero, so check against BEGV as well. */
5196 while (it->bidi_it.charpos >= BEGV
5197 && it->prev_stop <= it->bidi_it.charpos
5198 && it->bidi_it.charpos < CHARPOS (it->position))
5199 bidi_move_to_visually_next (&it->bidi_it);
5200 /* Record the stop_pos we just crossed, for when we cross it
5201 back, maybe. */
5202 if (it->bidi_it.charpos > CHARPOS (it->position))
5203 it->prev_stop = CHARPOS (it->position);
5204 /* If we ended up not where pop_it put us, resync IT's
5205 positional members with the bidi iterator. */
5206 if (it->bidi_it.charpos != CHARPOS (it->position))
5207 {
5208 SET_TEXT_POS (it->position,
5209 it->bidi_it.charpos, it->bidi_it.bytepos);
5210 it->current.pos = it->position;
5211 }
5212 }
5213
5214 /* Restore IT's settings from IT->stack. Called, for example, when no
5215 more overlay strings must be processed, and we return to delivering
5216 display elements from a buffer, or when the end of a string from a
5217 `display' property is reached and we return to delivering display
5218 elements from an overlay string, or from a buffer. */
5219
5220 static void
5221 pop_it (struct it *it)
5222 {
5223 struct iterator_stack_entry *p;
5224
5225 xassert (it->sp > 0);
5226 --it->sp;
5227 p = it->stack + it->sp;
5228 it->stop_charpos = p->stop_charpos;
5229 it->prev_stop = p->prev_stop;
5230 it->base_level_stop = p->base_level_stop;
5231 it->cmp_it = p->cmp_it;
5232 it->face_id = p->face_id;
5233 it->current = p->current;
5234 it->position = p->position;
5235 it->string = p->string;
5236 it->from_overlay = p->from_overlay;
5237 if (NILP (it->string))
5238 SET_TEXT_POS (it->current.string_pos, -1, -1);
5239 it->method = p->method;
5240 switch (it->method)
5241 {
5242 case GET_FROM_IMAGE:
5243 it->image_id = p->u.image.image_id;
5244 it->object = p->u.image.object;
5245 it->slice = p->u.image.slice;
5246 break;
5247 case GET_FROM_STRETCH:
5248 it->object = p->u.comp.object;
5249 break;
5250 case GET_FROM_BUFFER:
5251 it->object = it->w->buffer;
5252 if (it->bidi_p)
5253 {
5254 /* Bidi-iterate until we get out of the portion of text, if
5255 any, covered by a `display' text property or an overlay
5256 with `display' property. (We cannot just jump there,
5257 because the internal coherency of the bidi iterator state
5258 can not be preserved across such jumps.) We also must
5259 determine the paragraph base direction if the overlay we
5260 just processed is at the beginning of a new
5261 paragraph. */
5262 iterate_out_of_display_property (it);
5263 }
5264 break;
5265 case GET_FROM_STRING:
5266 it->object = it->string;
5267 break;
5268 case GET_FROM_DISPLAY_VECTOR:
5269 if (it->s)
5270 it->method = GET_FROM_C_STRING;
5271 else if (STRINGP (it->string))
5272 it->method = GET_FROM_STRING;
5273 else
5274 {
5275 it->method = GET_FROM_BUFFER;
5276 it->object = it->w->buffer;
5277 }
5278 }
5279 it->end_charpos = p->end_charpos;
5280 it->string_nchars = p->string_nchars;
5281 it->area = p->area;
5282 it->multibyte_p = p->multibyte_p;
5283 it->avoid_cursor_p = p->avoid_cursor_p;
5284 it->space_width = p->space_width;
5285 it->font_height = p->font_height;
5286 it->voffset = p->voffset;
5287 it->string_from_display_prop_p = p->string_from_display_prop_p;
5288 it->line_wrap = p->line_wrap;
5289 }
5290
5291
5292 \f
5293 /***********************************************************************
5294 Moving over lines
5295 ***********************************************************************/
5296
5297 /* Set IT's current position to the previous line start. */
5298
5299 static void
5300 back_to_previous_line_start (struct it *it)
5301 {
5302 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5303 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5304 }
5305
5306
5307 /* Move IT to the next line start.
5308
5309 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5310 we skipped over part of the text (as opposed to moving the iterator
5311 continuously over the text). Otherwise, don't change the value
5312 of *SKIPPED_P.
5313
5314 Newlines may come from buffer text, overlay strings, or strings
5315 displayed via the `display' property. That's the reason we can't
5316 simply use find_next_newline_no_quit.
5317
5318 Note that this function may not skip over invisible text that is so
5319 because of text properties and immediately follows a newline. If
5320 it would, function reseat_at_next_visible_line_start, when called
5321 from set_iterator_to_next, would effectively make invisible
5322 characters following a newline part of the wrong glyph row, which
5323 leads to wrong cursor motion. */
5324
5325 static int
5326 forward_to_next_line_start (struct it *it, int *skipped_p)
5327 {
5328 int old_selective, newline_found_p, n;
5329 const int MAX_NEWLINE_DISTANCE = 500;
5330
5331 /* If already on a newline, just consume it to avoid unintended
5332 skipping over invisible text below. */
5333 if (it->what == IT_CHARACTER
5334 && it->c == '\n'
5335 && CHARPOS (it->position) == IT_CHARPOS (*it))
5336 {
5337 set_iterator_to_next (it, 0);
5338 it->c = 0;
5339 return 1;
5340 }
5341
5342 /* Don't handle selective display in the following. It's (a)
5343 unnecessary because it's done by the caller, and (b) leads to an
5344 infinite recursion because next_element_from_ellipsis indirectly
5345 calls this function. */
5346 old_selective = it->selective;
5347 it->selective = 0;
5348
5349 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5350 from buffer text. */
5351 for (n = newline_found_p = 0;
5352 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5353 n += STRINGP (it->string) ? 0 : 1)
5354 {
5355 if (!get_next_display_element (it))
5356 return 0;
5357 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5358 set_iterator_to_next (it, 0);
5359 }
5360
5361 /* If we didn't find a newline near enough, see if we can use a
5362 short-cut. */
5363 if (!newline_found_p)
5364 {
5365 EMACS_INT start = IT_CHARPOS (*it);
5366 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5367 Lisp_Object pos;
5368
5369 xassert (!STRINGP (it->string));
5370
5371 /* If there isn't any `display' property in sight, and no
5372 overlays, we can just use the position of the newline in
5373 buffer text. */
5374 if (it->stop_charpos >= limit
5375 || ((pos = Fnext_single_property_change (make_number (start),
5376 Qdisplay,
5377 Qnil, make_number (limit)),
5378 NILP (pos))
5379 && next_overlay_change (start) == ZV))
5380 {
5381 IT_CHARPOS (*it) = limit;
5382 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5383 *skipped_p = newline_found_p = 1;
5384 }
5385 else
5386 {
5387 while (get_next_display_element (it)
5388 && !newline_found_p)
5389 {
5390 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5391 set_iterator_to_next (it, 0);
5392 }
5393 }
5394 }
5395
5396 it->selective = old_selective;
5397 return newline_found_p;
5398 }
5399
5400
5401 /* Set IT's current position to the previous visible line start. Skip
5402 invisible text that is so either due to text properties or due to
5403 selective display. Caution: this does not change IT->current_x and
5404 IT->hpos. */
5405
5406 static void
5407 back_to_previous_visible_line_start (struct it *it)
5408 {
5409 while (IT_CHARPOS (*it) > BEGV)
5410 {
5411 back_to_previous_line_start (it);
5412
5413 if (IT_CHARPOS (*it) <= BEGV)
5414 break;
5415
5416 /* If selective > 0, then lines indented more than its value are
5417 invisible. */
5418 if (it->selective > 0
5419 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5420 (double) it->selective)) /* iftc */
5421 continue;
5422
5423 /* Check the newline before point for invisibility. */
5424 {
5425 Lisp_Object prop;
5426 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5427 Qinvisible, it->window);
5428 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5429 continue;
5430 }
5431
5432 if (IT_CHARPOS (*it) <= BEGV)
5433 break;
5434
5435 {
5436 struct it it2;
5437 EMACS_INT pos;
5438 EMACS_INT beg, end;
5439 Lisp_Object val, overlay;
5440
5441 /* If newline is part of a composition, continue from start of composition */
5442 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5443 && beg < IT_CHARPOS (*it))
5444 goto replaced;
5445
5446 /* If newline is replaced by a display property, find start of overlay
5447 or interval and continue search from that point. */
5448 it2 = *it;
5449 pos = --IT_CHARPOS (it2);
5450 --IT_BYTEPOS (it2);
5451 it2.sp = 0;
5452 it2.string_from_display_prop_p = 0;
5453 if (handle_display_prop (&it2) == HANDLED_RETURN
5454 && !NILP (val = get_char_property_and_overlay
5455 (make_number (pos), Qdisplay, Qnil, &overlay))
5456 && (OVERLAYP (overlay)
5457 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5458 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5459 goto replaced;
5460
5461 /* Newline is not replaced by anything -- so we are done. */
5462 break;
5463
5464 replaced:
5465 if (beg < BEGV)
5466 beg = BEGV;
5467 IT_CHARPOS (*it) = beg;
5468 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5469 }
5470 }
5471
5472 it->continuation_lines_width = 0;
5473
5474 xassert (IT_CHARPOS (*it) >= BEGV);
5475 xassert (IT_CHARPOS (*it) == BEGV
5476 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5477 CHECK_IT (it);
5478 }
5479
5480
5481 /* Reseat iterator IT at the previous visible line start. Skip
5482 invisible text that is so either due to text properties or due to
5483 selective display. At the end, update IT's overlay information,
5484 face information etc. */
5485
5486 void
5487 reseat_at_previous_visible_line_start (struct it *it)
5488 {
5489 back_to_previous_visible_line_start (it);
5490 reseat (it, it->current.pos, 1);
5491 CHECK_IT (it);
5492 }
5493
5494
5495 /* Reseat iterator IT on the next visible line start in the current
5496 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5497 preceding the line start. Skip over invisible text that is so
5498 because of selective display. Compute faces, overlays etc at the
5499 new position. Note that this function does not skip over text that
5500 is invisible because of text properties. */
5501
5502 static void
5503 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5504 {
5505 int newline_found_p, skipped_p = 0;
5506
5507 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5508
5509 /* Skip over lines that are invisible because they are indented
5510 more than the value of IT->selective. */
5511 if (it->selective > 0)
5512 while (IT_CHARPOS (*it) < ZV
5513 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5514 (double) it->selective)) /* iftc */
5515 {
5516 xassert (IT_BYTEPOS (*it) == BEGV
5517 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5518 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5519 }
5520
5521 /* Position on the newline if that's what's requested. */
5522 if (on_newline_p && newline_found_p)
5523 {
5524 if (STRINGP (it->string))
5525 {
5526 if (IT_STRING_CHARPOS (*it) > 0)
5527 {
5528 --IT_STRING_CHARPOS (*it);
5529 --IT_STRING_BYTEPOS (*it);
5530 }
5531 }
5532 else if (IT_CHARPOS (*it) > BEGV)
5533 {
5534 --IT_CHARPOS (*it);
5535 --IT_BYTEPOS (*it);
5536 reseat (it, it->current.pos, 0);
5537 }
5538 }
5539 else if (skipped_p)
5540 reseat (it, it->current.pos, 0);
5541
5542 CHECK_IT (it);
5543 }
5544
5545
5546 \f
5547 /***********************************************************************
5548 Changing an iterator's position
5549 ***********************************************************************/
5550
5551 /* Change IT's current position to POS in current_buffer. If FORCE_P
5552 is non-zero, always check for text properties at the new position.
5553 Otherwise, text properties are only looked up if POS >=
5554 IT->check_charpos of a property. */
5555
5556 static void
5557 reseat (struct it *it, struct text_pos pos, int force_p)
5558 {
5559 EMACS_INT original_pos = IT_CHARPOS (*it);
5560
5561 reseat_1 (it, pos, 0);
5562
5563 /* Determine where to check text properties. Avoid doing it
5564 where possible because text property lookup is very expensive. */
5565 if (force_p
5566 || CHARPOS (pos) > it->stop_charpos
5567 || CHARPOS (pos) < original_pos)
5568 {
5569 if (it->bidi_p)
5570 {
5571 /* For bidi iteration, we need to prime prev_stop and
5572 base_level_stop with our best estimations. */
5573 if (CHARPOS (pos) < it->prev_stop)
5574 {
5575 handle_stop_backwards (it, BEGV);
5576 if (CHARPOS (pos) < it->base_level_stop)
5577 it->base_level_stop = 0;
5578 }
5579 else if (CHARPOS (pos) > it->stop_charpos
5580 && it->stop_charpos >= BEGV)
5581 handle_stop_backwards (it, it->stop_charpos);
5582 else /* force_p */
5583 handle_stop (it);
5584 }
5585 else
5586 {
5587 handle_stop (it);
5588 it->prev_stop = it->base_level_stop = 0;
5589 }
5590
5591 }
5592
5593 CHECK_IT (it);
5594 }
5595
5596
5597 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5598 IT->stop_pos to POS, also. */
5599
5600 static void
5601 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5602 {
5603 /* Don't call this function when scanning a C string. */
5604 xassert (it->s == NULL);
5605
5606 /* POS must be a reasonable value. */
5607 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5608
5609 it->current.pos = it->position = pos;
5610 it->end_charpos = ZV;
5611 it->dpvec = NULL;
5612 it->current.dpvec_index = -1;
5613 it->current.overlay_string_index = -1;
5614 IT_STRING_CHARPOS (*it) = -1;
5615 IT_STRING_BYTEPOS (*it) = -1;
5616 it->string = Qnil;
5617 it->string_from_display_prop_p = 0;
5618 it->method = GET_FROM_BUFFER;
5619 it->object = it->w->buffer;
5620 it->area = TEXT_AREA;
5621 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5622 it->sp = 0;
5623 it->string_from_display_prop_p = 0;
5624 it->face_before_selective_p = 0;
5625 if (it->bidi_p)
5626 {
5627 it->bidi_it.first_elt = 1;
5628 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5629 it->bidi_it.disp_pos = -1;
5630 it->bidi_it.string.s = NULL;
5631 it->bidi_it.string.lstring = Qnil;
5632 it->bidi_it.string.bufpos = 0;
5633 }
5634
5635 if (set_stop_p)
5636 {
5637 it->stop_charpos = CHARPOS (pos);
5638 it->base_level_stop = CHARPOS (pos);
5639 }
5640 }
5641
5642
5643 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5644 If S is non-null, it is a C string to iterate over. Otherwise,
5645 STRING gives a Lisp string to iterate over.
5646
5647 If PRECISION > 0, don't return more then PRECISION number of
5648 characters from the string.
5649
5650 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5651 characters have been returned. FIELD_WIDTH < 0 means an infinite
5652 field width.
5653
5654 MULTIBYTE = 0 means disable processing of multibyte characters,
5655 MULTIBYTE > 0 means enable it,
5656 MULTIBYTE < 0 means use IT->multibyte_p.
5657
5658 IT must be initialized via a prior call to init_iterator before
5659 calling this function. */
5660
5661 static void
5662 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5663 EMACS_INT charpos, EMACS_INT precision, int field_width,
5664 int multibyte)
5665 {
5666 /* No region in strings. */
5667 it->region_beg_charpos = it->region_end_charpos = -1;
5668
5669 /* No text property checks performed by default, but see below. */
5670 it->stop_charpos = -1;
5671
5672 /* Set iterator position and end position. */
5673 memset (&it->current, 0, sizeof it->current);
5674 it->current.overlay_string_index = -1;
5675 it->current.dpvec_index = -1;
5676 xassert (charpos >= 0);
5677
5678 /* If STRING is specified, use its multibyteness, otherwise use the
5679 setting of MULTIBYTE, if specified. */
5680 if (multibyte >= 0)
5681 it->multibyte_p = multibyte > 0;
5682
5683 /* Bidirectional reordering of strings is controlled by the default
5684 value of bidi-display-reordering. */
5685 it->bidi_p =
5686 !NILP (BVAR (&buffer_defaults, bidi_display_reordering))
5687 && it->multibyte_p;
5688
5689 if (s == NULL)
5690 {
5691 xassert (STRINGP (string));
5692 it->string = string;
5693 it->s = NULL;
5694 it->end_charpos = it->string_nchars = SCHARS (string);
5695 it->method = GET_FROM_STRING;
5696 it->current.string_pos = string_pos (charpos, string);
5697
5698 if (it->bidi_p)
5699 {
5700 it->bidi_it.string.lstring = string;
5701 it->bidi_it.string.s = NULL;
5702 it->bidi_it.string.schars = it->end_charpos;
5703 it->bidi_it.string.bufpos = 0;
5704 it->bidi_it.string.from_disp_str = 0;
5705 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5706 FRAME_WINDOW_P (it->f), &it->bidi_it);
5707 }
5708 }
5709 else
5710 {
5711 it->s = (const unsigned char *) s;
5712 it->string = Qnil;
5713
5714 /* Note that we use IT->current.pos, not it->current.string_pos,
5715 for displaying C strings. */
5716 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5717 if (it->multibyte_p)
5718 {
5719 it->current.pos = c_string_pos (charpos, s, 1);
5720 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5721
5722 if (it->bidi_p)
5723 {
5724 it->bidi_it.string.lstring = Qnil;
5725 it->bidi_it.string.s = s;
5726 it->bidi_it.string.schars = it->end_charpos;
5727 it->bidi_it.string.bufpos = 0;
5728 it->bidi_it.string.from_disp_str = 0;
5729 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5730 &it->bidi_it);
5731 }
5732 }
5733 else
5734 {
5735 /* Unibyte (a.k.a. ASCII) C strings are never bidi-reordered. */
5736 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5737 it->end_charpos = it->string_nchars = strlen (s);
5738 }
5739
5740 it->method = GET_FROM_C_STRING;
5741 }
5742
5743 /* PRECISION > 0 means don't return more than PRECISION characters
5744 from the string. */
5745 if (precision > 0 && it->end_charpos - charpos > precision)
5746 {
5747 it->end_charpos = it->string_nchars = charpos + precision;
5748 if (it->bidi_p)
5749 it->bidi_it.string.schars = it->end_charpos;
5750 }
5751
5752 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5753 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5754 FIELD_WIDTH < 0 means infinite field width. This is useful for
5755 padding with `-' at the end of a mode line. */
5756 if (field_width < 0)
5757 field_width = INFINITY;
5758 /* Implementation note: We deliberately don't enlarge
5759 it->bidi_it.string.schars here to fit it->end_charpos, because
5760 the bidi iterator cannot produce characters out of thin air. */
5761 if (field_width > it->end_charpos - charpos)
5762 it->end_charpos = charpos + field_width;
5763
5764 /* Use the standard display table for displaying strings. */
5765 if (DISP_TABLE_P (Vstandard_display_table))
5766 it->dp = XCHAR_TABLE (Vstandard_display_table);
5767
5768 it->stop_charpos = charpos;
5769 it->prev_stop = charpos;
5770 it->base_level_stop = 0;
5771 if (it->bidi_p)
5772 {
5773 it->bidi_it.first_elt = 1;
5774 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5775 it->bidi_it.disp_pos = -1;
5776 }
5777 if (s == NULL && it->multibyte_p)
5778 {
5779 EMACS_INT endpos = SCHARS (it->string);
5780 if (endpos > it->end_charpos)
5781 endpos = it->end_charpos;
5782 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5783 it->string);
5784 }
5785 CHECK_IT (it);
5786 }
5787
5788
5789 \f
5790 /***********************************************************************
5791 Iteration
5792 ***********************************************************************/
5793
5794 /* Map enum it_method value to corresponding next_element_from_* function. */
5795
5796 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5797 {
5798 next_element_from_buffer,
5799 next_element_from_display_vector,
5800 next_element_from_string,
5801 next_element_from_c_string,
5802 next_element_from_image,
5803 next_element_from_stretch
5804 };
5805
5806 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5807
5808
5809 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5810 (possibly with the following characters). */
5811
5812 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5813 ((IT)->cmp_it.id >= 0 \
5814 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5815 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5816 END_CHARPOS, (IT)->w, \
5817 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5818 (IT)->string)))
5819
5820
5821 /* Lookup the char-table Vglyphless_char_display for character C (-1
5822 if we want information for no-font case), and return the display
5823 method symbol. By side-effect, update it->what and
5824 it->glyphless_method. This function is called from
5825 get_next_display_element for each character element, and from
5826 x_produce_glyphs when no suitable font was found. */
5827
5828 Lisp_Object
5829 lookup_glyphless_char_display (int c, struct it *it)
5830 {
5831 Lisp_Object glyphless_method = Qnil;
5832
5833 if (CHAR_TABLE_P (Vglyphless_char_display)
5834 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5835 {
5836 if (c >= 0)
5837 {
5838 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
5839 if (CONSP (glyphless_method))
5840 glyphless_method = FRAME_WINDOW_P (it->f)
5841 ? XCAR (glyphless_method)
5842 : XCDR (glyphless_method);
5843 }
5844 else
5845 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
5846 }
5847
5848 retry:
5849 if (NILP (glyphless_method))
5850 {
5851 if (c >= 0)
5852 /* The default is to display the character by a proper font. */
5853 return Qnil;
5854 /* The default for the no-font case is to display an empty box. */
5855 glyphless_method = Qempty_box;
5856 }
5857 if (EQ (glyphless_method, Qzero_width))
5858 {
5859 if (c >= 0)
5860 return glyphless_method;
5861 /* This method can't be used for the no-font case. */
5862 glyphless_method = Qempty_box;
5863 }
5864 if (EQ (glyphless_method, Qthin_space))
5865 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5866 else if (EQ (glyphless_method, Qempty_box))
5867 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5868 else if (EQ (glyphless_method, Qhex_code))
5869 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5870 else if (STRINGP (glyphless_method))
5871 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5872 else
5873 {
5874 /* Invalid value. We use the default method. */
5875 glyphless_method = Qnil;
5876 goto retry;
5877 }
5878 it->what = IT_GLYPHLESS;
5879 return glyphless_method;
5880 }
5881
5882 /* Load IT's display element fields with information about the next
5883 display element from the current position of IT. Value is zero if
5884 end of buffer (or C string) is reached. */
5885
5886 static struct frame *last_escape_glyph_frame = NULL;
5887 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5888 static int last_escape_glyph_merged_face_id = 0;
5889
5890 struct frame *last_glyphless_glyph_frame = NULL;
5891 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5892 int last_glyphless_glyph_merged_face_id = 0;
5893
5894 static int
5895 get_next_display_element (struct it *it)
5896 {
5897 /* Non-zero means that we found a display element. Zero means that
5898 we hit the end of what we iterate over. Performance note: the
5899 function pointer `method' used here turns out to be faster than
5900 using a sequence of if-statements. */
5901 int success_p;
5902
5903 get_next:
5904 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5905
5906 if (it->what == IT_CHARACTER)
5907 {
5908 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5909 and only if (a) the resolved directionality of that character
5910 is R..." */
5911 /* FIXME: Do we need an exception for characters from display
5912 tables? */
5913 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5914 it->c = bidi_mirror_char (it->c);
5915 /* Map via display table or translate control characters.
5916 IT->c, IT->len etc. have been set to the next character by
5917 the function call above. If we have a display table, and it
5918 contains an entry for IT->c, translate it. Don't do this if
5919 IT->c itself comes from a display table, otherwise we could
5920 end up in an infinite recursion. (An alternative could be to
5921 count the recursion depth of this function and signal an
5922 error when a certain maximum depth is reached.) Is it worth
5923 it? */
5924 if (success_p && it->dpvec == NULL)
5925 {
5926 Lisp_Object dv;
5927 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5928 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5929 nbsp_or_shy = char_is_other;
5930 int c = it->c; /* This is the character to display. */
5931
5932 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5933 {
5934 xassert (SINGLE_BYTE_CHAR_P (c));
5935 if (unibyte_display_via_language_environment)
5936 {
5937 c = DECODE_CHAR (unibyte, c);
5938 if (c < 0)
5939 c = BYTE8_TO_CHAR (it->c);
5940 }
5941 else
5942 c = BYTE8_TO_CHAR (it->c);
5943 }
5944
5945 if (it->dp
5946 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5947 VECTORP (dv)))
5948 {
5949 struct Lisp_Vector *v = XVECTOR (dv);
5950
5951 /* Return the first character from the display table
5952 entry, if not empty. If empty, don't display the
5953 current character. */
5954 if (v->header.size)
5955 {
5956 it->dpvec_char_len = it->len;
5957 it->dpvec = v->contents;
5958 it->dpend = v->contents + v->header.size;
5959 it->current.dpvec_index = 0;
5960 it->dpvec_face_id = -1;
5961 it->saved_face_id = it->face_id;
5962 it->method = GET_FROM_DISPLAY_VECTOR;
5963 it->ellipsis_p = 0;
5964 }
5965 else
5966 {
5967 set_iterator_to_next (it, 0);
5968 }
5969 goto get_next;
5970 }
5971
5972 if (! NILP (lookup_glyphless_char_display (c, it)))
5973 {
5974 if (it->what == IT_GLYPHLESS)
5975 goto done;
5976 /* Don't display this character. */
5977 set_iterator_to_next (it, 0);
5978 goto get_next;
5979 }
5980
5981 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5982 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5983 : c == 0xAD ? char_is_soft_hyphen
5984 : char_is_other);
5985
5986 /* Translate control characters into `\003' or `^C' form.
5987 Control characters coming from a display table entry are
5988 currently not translated because we use IT->dpvec to hold
5989 the translation. This could easily be changed but I
5990 don't believe that it is worth doing.
5991
5992 NBSP and SOFT-HYPEN are property translated too.
5993
5994 Non-printable characters and raw-byte characters are also
5995 translated to octal form. */
5996 if (((c < ' ' || c == 127) /* ASCII control chars */
5997 ? (it->area != TEXT_AREA
5998 /* In mode line, treat \n, \t like other crl chars. */
5999 || (c != '\t'
6000 && it->glyph_row
6001 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6002 || (c != '\n' && c != '\t'))
6003 : (nbsp_or_shy
6004 || CHAR_BYTE8_P (c)
6005 || ! CHAR_PRINTABLE_P (c))))
6006 {
6007 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6008 or a non-printable character which must be displayed
6009 either as '\003' or as `^C' where the '\\' and '^'
6010 can be defined in the display table. Fill
6011 IT->ctl_chars with glyphs for what we have to
6012 display. Then, set IT->dpvec to these glyphs. */
6013 Lisp_Object gc;
6014 int ctl_len;
6015 int face_id, lface_id = 0 ;
6016 int escape_glyph;
6017
6018 /* Handle control characters with ^. */
6019
6020 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6021 {
6022 int g;
6023
6024 g = '^'; /* default glyph for Control */
6025 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6026 if (it->dp
6027 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6028 && GLYPH_CODE_CHAR_VALID_P (gc))
6029 {
6030 g = GLYPH_CODE_CHAR (gc);
6031 lface_id = GLYPH_CODE_FACE (gc);
6032 }
6033 if (lface_id)
6034 {
6035 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6036 }
6037 else if (it->f == last_escape_glyph_frame
6038 && it->face_id == last_escape_glyph_face_id)
6039 {
6040 face_id = last_escape_glyph_merged_face_id;
6041 }
6042 else
6043 {
6044 /* Merge the escape-glyph face into the current face. */
6045 face_id = merge_faces (it->f, Qescape_glyph, 0,
6046 it->face_id);
6047 last_escape_glyph_frame = it->f;
6048 last_escape_glyph_face_id = it->face_id;
6049 last_escape_glyph_merged_face_id = face_id;
6050 }
6051
6052 XSETINT (it->ctl_chars[0], g);
6053 XSETINT (it->ctl_chars[1], c ^ 0100);
6054 ctl_len = 2;
6055 goto display_control;
6056 }
6057
6058 /* Handle non-break space in the mode where it only gets
6059 highlighting. */
6060
6061 if (EQ (Vnobreak_char_display, Qt)
6062 && nbsp_or_shy == char_is_nbsp)
6063 {
6064 /* Merge the no-break-space face into the current face. */
6065 face_id = merge_faces (it->f, Qnobreak_space, 0,
6066 it->face_id);
6067
6068 c = ' ';
6069 XSETINT (it->ctl_chars[0], ' ');
6070 ctl_len = 1;
6071 goto display_control;
6072 }
6073
6074 /* Handle sequences that start with the "escape glyph". */
6075
6076 /* the default escape glyph is \. */
6077 escape_glyph = '\\';
6078
6079 if (it->dp
6080 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6081 && GLYPH_CODE_CHAR_VALID_P (gc))
6082 {
6083 escape_glyph = GLYPH_CODE_CHAR (gc);
6084 lface_id = GLYPH_CODE_FACE (gc);
6085 }
6086 if (lface_id)
6087 {
6088 /* The display table specified a face.
6089 Merge it into face_id and also into escape_glyph. */
6090 face_id = merge_faces (it->f, Qt, lface_id,
6091 it->face_id);
6092 }
6093 else if (it->f == last_escape_glyph_frame
6094 && it->face_id == last_escape_glyph_face_id)
6095 {
6096 face_id = last_escape_glyph_merged_face_id;
6097 }
6098 else
6099 {
6100 /* Merge the escape-glyph face into the current face. */
6101 face_id = merge_faces (it->f, Qescape_glyph, 0,
6102 it->face_id);
6103 last_escape_glyph_frame = it->f;
6104 last_escape_glyph_face_id = it->face_id;
6105 last_escape_glyph_merged_face_id = face_id;
6106 }
6107
6108 /* Handle soft hyphens in the mode where they only get
6109 highlighting. */
6110
6111 if (EQ (Vnobreak_char_display, Qt)
6112 && nbsp_or_shy == char_is_soft_hyphen)
6113 {
6114 XSETINT (it->ctl_chars[0], '-');
6115 ctl_len = 1;
6116 goto display_control;
6117 }
6118
6119 /* Handle non-break space and soft hyphen
6120 with the escape glyph. */
6121
6122 if (nbsp_or_shy)
6123 {
6124 XSETINT (it->ctl_chars[0], escape_glyph);
6125 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6126 XSETINT (it->ctl_chars[1], c);
6127 ctl_len = 2;
6128 goto display_control;
6129 }
6130
6131 {
6132 char str[10];
6133 int len, i;
6134
6135 if (CHAR_BYTE8_P (c))
6136 /* Display \200 instead of \17777600. */
6137 c = CHAR_TO_BYTE8 (c);
6138 len = sprintf (str, "%03o", c);
6139
6140 XSETINT (it->ctl_chars[0], escape_glyph);
6141 for (i = 0; i < len; i++)
6142 XSETINT (it->ctl_chars[i + 1], str[i]);
6143 ctl_len = len + 1;
6144 }
6145
6146 display_control:
6147 /* Set up IT->dpvec and return first character from it. */
6148 it->dpvec_char_len = it->len;
6149 it->dpvec = it->ctl_chars;
6150 it->dpend = it->dpvec + ctl_len;
6151 it->current.dpvec_index = 0;
6152 it->dpvec_face_id = face_id;
6153 it->saved_face_id = it->face_id;
6154 it->method = GET_FROM_DISPLAY_VECTOR;
6155 it->ellipsis_p = 0;
6156 goto get_next;
6157 }
6158 it->char_to_display = c;
6159 }
6160 else if (success_p)
6161 {
6162 it->char_to_display = it->c;
6163 }
6164 }
6165
6166 /* Adjust face id for a multibyte character. There are no multibyte
6167 character in unibyte text. */
6168 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6169 && it->multibyte_p
6170 && success_p
6171 && FRAME_WINDOW_P (it->f))
6172 {
6173 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6174
6175 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6176 {
6177 /* Automatic composition with glyph-string. */
6178 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6179
6180 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6181 }
6182 else
6183 {
6184 EMACS_INT pos = (it->s ? -1
6185 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6186 : IT_CHARPOS (*it));
6187
6188 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6189 it->string);
6190 }
6191 }
6192
6193 done:
6194 /* Is this character the last one of a run of characters with
6195 box? If yes, set IT->end_of_box_run_p to 1. */
6196 if (it->face_box_p
6197 && it->s == NULL)
6198 {
6199 if (it->method == GET_FROM_STRING && it->sp)
6200 {
6201 int face_id = underlying_face_id (it);
6202 struct face *face = FACE_FROM_ID (it->f, face_id);
6203
6204 if (face)
6205 {
6206 if (face->box == FACE_NO_BOX)
6207 {
6208 /* If the box comes from face properties in a
6209 display string, check faces in that string. */
6210 int string_face_id = face_after_it_pos (it);
6211 it->end_of_box_run_p
6212 = (FACE_FROM_ID (it->f, string_face_id)->box
6213 == FACE_NO_BOX);
6214 }
6215 /* Otherwise, the box comes from the underlying face.
6216 If this is the last string character displayed, check
6217 the next buffer location. */
6218 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6219 && (it->current.overlay_string_index
6220 == it->n_overlay_strings - 1))
6221 {
6222 EMACS_INT ignore;
6223 int next_face_id;
6224 struct text_pos pos = it->current.pos;
6225 INC_TEXT_POS (pos, it->multibyte_p);
6226
6227 next_face_id = face_at_buffer_position
6228 (it->w, CHARPOS (pos), it->region_beg_charpos,
6229 it->region_end_charpos, &ignore,
6230 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6231 -1);
6232 it->end_of_box_run_p
6233 = (FACE_FROM_ID (it->f, next_face_id)->box
6234 == FACE_NO_BOX);
6235 }
6236 }
6237 }
6238 else
6239 {
6240 int face_id = face_after_it_pos (it);
6241 it->end_of_box_run_p
6242 = (face_id != it->face_id
6243 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6244 }
6245 }
6246
6247 /* Value is 0 if end of buffer or string reached. */
6248 return success_p;
6249 }
6250
6251
6252 /* Move IT to the next display element.
6253
6254 RESEAT_P non-zero means if called on a newline in buffer text,
6255 skip to the next visible line start.
6256
6257 Functions get_next_display_element and set_iterator_to_next are
6258 separate because I find this arrangement easier to handle than a
6259 get_next_display_element function that also increments IT's
6260 position. The way it is we can first look at an iterator's current
6261 display element, decide whether it fits on a line, and if it does,
6262 increment the iterator position. The other way around we probably
6263 would either need a flag indicating whether the iterator has to be
6264 incremented the next time, or we would have to implement a
6265 decrement position function which would not be easy to write. */
6266
6267 void
6268 set_iterator_to_next (struct it *it, int reseat_p)
6269 {
6270 /* Reset flags indicating start and end of a sequence of characters
6271 with box. Reset them at the start of this function because
6272 moving the iterator to a new position might set them. */
6273 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6274
6275 switch (it->method)
6276 {
6277 case GET_FROM_BUFFER:
6278 /* The current display element of IT is a character from
6279 current_buffer. Advance in the buffer, and maybe skip over
6280 invisible lines that are so because of selective display. */
6281 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6282 reseat_at_next_visible_line_start (it, 0);
6283 else if (it->cmp_it.id >= 0)
6284 {
6285 /* We are currently getting glyphs from a composition. */
6286 int i;
6287
6288 if (! it->bidi_p)
6289 {
6290 IT_CHARPOS (*it) += it->cmp_it.nchars;
6291 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6292 if (it->cmp_it.to < it->cmp_it.nglyphs)
6293 {
6294 it->cmp_it.from = it->cmp_it.to;
6295 }
6296 else
6297 {
6298 it->cmp_it.id = -1;
6299 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6300 IT_BYTEPOS (*it),
6301 it->end_charpos, Qnil);
6302 }
6303 }
6304 else if (! it->cmp_it.reversed_p)
6305 {
6306 /* Composition created while scanning forward. */
6307 /* Update IT's char/byte positions to point to the first
6308 character of the next grapheme cluster, or to the
6309 character visually after the current composition. */
6310 for (i = 0; i < it->cmp_it.nchars; i++)
6311 bidi_move_to_visually_next (&it->bidi_it);
6312 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6313 IT_CHARPOS (*it) = it->bidi_it.charpos;
6314
6315 if (it->cmp_it.to < it->cmp_it.nglyphs)
6316 {
6317 /* Proceed to the next grapheme cluster. */
6318 it->cmp_it.from = it->cmp_it.to;
6319 }
6320 else
6321 {
6322 /* No more grapheme clusters in this composition.
6323 Find the next stop position. */
6324 EMACS_INT stop = it->end_charpos;
6325 if (it->bidi_it.scan_dir < 0)
6326 /* Now we are scanning backward and don't know
6327 where to stop. */
6328 stop = -1;
6329 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6330 IT_BYTEPOS (*it), stop, Qnil);
6331 }
6332 }
6333 else
6334 {
6335 /* Composition created while scanning backward. */
6336 /* Update IT's char/byte positions to point to the last
6337 character of the previous grapheme cluster, or the
6338 character visually after the current composition. */
6339 for (i = 0; i < it->cmp_it.nchars; i++)
6340 bidi_move_to_visually_next (&it->bidi_it);
6341 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6342 IT_CHARPOS (*it) = it->bidi_it.charpos;
6343 if (it->cmp_it.from > 0)
6344 {
6345 /* Proceed to the previous grapheme cluster. */
6346 it->cmp_it.to = it->cmp_it.from;
6347 }
6348 else
6349 {
6350 /* No more grapheme clusters in this composition.
6351 Find the next stop position. */
6352 EMACS_INT stop = it->end_charpos;
6353 if (it->bidi_it.scan_dir < 0)
6354 /* Now we are scanning backward and don't know
6355 where to stop. */
6356 stop = -1;
6357 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6358 IT_BYTEPOS (*it), stop, Qnil);
6359 }
6360 }
6361 }
6362 else
6363 {
6364 xassert (it->len != 0);
6365
6366 if (!it->bidi_p)
6367 {
6368 IT_BYTEPOS (*it) += it->len;
6369 IT_CHARPOS (*it) += 1;
6370 }
6371 else
6372 {
6373 int prev_scan_dir = it->bidi_it.scan_dir;
6374 /* If this is a new paragraph, determine its base
6375 direction (a.k.a. its base embedding level). */
6376 if (it->bidi_it.new_paragraph)
6377 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6378 bidi_move_to_visually_next (&it->bidi_it);
6379 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6380 IT_CHARPOS (*it) = it->bidi_it.charpos;
6381 if (prev_scan_dir != it->bidi_it.scan_dir)
6382 {
6383 /* As the scan direction was changed, we must
6384 re-compute the stop position for composition. */
6385 EMACS_INT stop = it->end_charpos;
6386 if (it->bidi_it.scan_dir < 0)
6387 stop = -1;
6388 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6389 IT_BYTEPOS (*it), stop, Qnil);
6390 }
6391 }
6392 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6393 }
6394 break;
6395
6396 case GET_FROM_C_STRING:
6397 /* Current display element of IT is from a C string. */
6398 if (!it->bidi_p
6399 /* If the string position is beyond string_nchars, it means
6400 next_element_from_c_string is padding the string with
6401 blanks, in which case we bypass the bidi iterator,
6402 because it cannot deal with such virtual characters. */
6403 || IT_CHARPOS (*it) >= it->string_nchars)
6404 {
6405 IT_BYTEPOS (*it) += it->len;
6406 IT_CHARPOS (*it) += 1;
6407 }
6408 else
6409 {
6410 bidi_move_to_visually_next (&it->bidi_it);
6411 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6412 IT_CHARPOS (*it) = it->bidi_it.charpos;
6413 }
6414 break;
6415
6416 case GET_FROM_DISPLAY_VECTOR:
6417 /* Current display element of IT is from a display table entry.
6418 Advance in the display table definition. Reset it to null if
6419 end reached, and continue with characters from buffers/
6420 strings. */
6421 ++it->current.dpvec_index;
6422
6423 /* Restore face of the iterator to what they were before the
6424 display vector entry (these entries may contain faces). */
6425 it->face_id = it->saved_face_id;
6426
6427 if (it->dpvec + it->current.dpvec_index == it->dpend)
6428 {
6429 int recheck_faces = it->ellipsis_p;
6430
6431 if (it->s)
6432 it->method = GET_FROM_C_STRING;
6433 else if (STRINGP (it->string))
6434 it->method = GET_FROM_STRING;
6435 else
6436 {
6437 it->method = GET_FROM_BUFFER;
6438 it->object = it->w->buffer;
6439 }
6440
6441 it->dpvec = NULL;
6442 it->current.dpvec_index = -1;
6443
6444 /* Skip over characters which were displayed via IT->dpvec. */
6445 if (it->dpvec_char_len < 0)
6446 reseat_at_next_visible_line_start (it, 1);
6447 else if (it->dpvec_char_len > 0)
6448 {
6449 if (it->method == GET_FROM_STRING
6450 && it->n_overlay_strings > 0)
6451 it->ignore_overlay_strings_at_pos_p = 1;
6452 it->len = it->dpvec_char_len;
6453 set_iterator_to_next (it, reseat_p);
6454 }
6455
6456 /* Maybe recheck faces after display vector */
6457 if (recheck_faces)
6458 it->stop_charpos = IT_CHARPOS (*it);
6459 }
6460 break;
6461
6462 case GET_FROM_STRING:
6463 /* Current display element is a character from a Lisp string. */
6464 xassert (it->s == NULL && STRINGP (it->string));
6465 if (it->cmp_it.id >= 0)
6466 {
6467 int i;
6468
6469 if (! it->bidi_p)
6470 {
6471 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6472 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6473 if (it->cmp_it.to < it->cmp_it.nglyphs)
6474 it->cmp_it.from = it->cmp_it.to;
6475 else
6476 {
6477 it->cmp_it.id = -1;
6478 composition_compute_stop_pos (&it->cmp_it,
6479 IT_STRING_CHARPOS (*it),
6480 IT_STRING_BYTEPOS (*it),
6481 it->end_charpos, it->string);
6482 }
6483 }
6484 else if (! it->cmp_it.reversed_p)
6485 {
6486 for (i = 0; i < it->cmp_it.nchars; i++)
6487 bidi_move_to_visually_next (&it->bidi_it);
6488 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6489 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6490
6491 if (it->cmp_it.to < it->cmp_it.nglyphs)
6492 it->cmp_it.from = it->cmp_it.to;
6493 else
6494 {
6495 EMACS_INT stop = it->end_charpos;
6496 if (it->bidi_it.scan_dir < 0)
6497 stop = -1;
6498 composition_compute_stop_pos (&it->cmp_it,
6499 IT_STRING_CHARPOS (*it),
6500 IT_STRING_BYTEPOS (*it), stop,
6501 it->string);
6502 }
6503 }
6504 else
6505 {
6506 for (i = 0; i < it->cmp_it.nchars; i++)
6507 bidi_move_to_visually_next (&it->bidi_it);
6508 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6509 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6510 if (it->cmp_it.from > 0)
6511 it->cmp_it.to = it->cmp_it.from;
6512 else
6513 {
6514 EMACS_INT stop = it->end_charpos;
6515 if (it->bidi_it.scan_dir < 0)
6516 stop = -1;
6517 composition_compute_stop_pos (&it->cmp_it,
6518 IT_STRING_CHARPOS (*it),
6519 IT_STRING_BYTEPOS (*it), stop,
6520 it->string);
6521 }
6522 }
6523 }
6524 else
6525 {
6526 if (!it->bidi_p
6527 /* If the string position is beyond string_nchars, it
6528 means next_element_from_string is padding the string
6529 with blanks, in which case we bypass the bidi
6530 iterator, because it cannot deal with such virtual
6531 characters. */
6532 || IT_STRING_CHARPOS (*it) >= it->string_nchars)
6533 {
6534 IT_STRING_BYTEPOS (*it) += it->len;
6535 IT_STRING_CHARPOS (*it) += 1;
6536 }
6537 else
6538 {
6539 int prev_scan_dir = it->bidi_it.scan_dir;
6540
6541 bidi_move_to_visually_next (&it->bidi_it);
6542 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6543 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6544 if (prev_scan_dir != it->bidi_it.scan_dir)
6545 {
6546 EMACS_INT stop = it->end_charpos;
6547
6548 if (it->bidi_it.scan_dir < 0)
6549 stop = -1;
6550 composition_compute_stop_pos (&it->cmp_it,
6551 IT_STRING_CHARPOS (*it),
6552 IT_STRING_BYTEPOS (*it), stop,
6553 it->string);
6554 }
6555 }
6556 }
6557
6558 consider_string_end:
6559
6560 if (it->current.overlay_string_index >= 0)
6561 {
6562 /* IT->string is an overlay string. Advance to the
6563 next, if there is one. */
6564 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6565 {
6566 it->ellipsis_p = 0;
6567 next_overlay_string (it);
6568 if (it->ellipsis_p)
6569 setup_for_ellipsis (it, 0);
6570 }
6571 }
6572 else
6573 {
6574 /* IT->string is not an overlay string. If we reached
6575 its end, and there is something on IT->stack, proceed
6576 with what is on the stack. This can be either another
6577 string, this time an overlay string, or a buffer. */
6578 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6579 && it->sp > 0)
6580 {
6581 pop_it (it);
6582 if (it->method == GET_FROM_STRING)
6583 goto consider_string_end;
6584 }
6585 }
6586 break;
6587
6588 case GET_FROM_IMAGE:
6589 case GET_FROM_STRETCH:
6590 /* The position etc with which we have to proceed are on
6591 the stack. The position may be at the end of a string,
6592 if the `display' property takes up the whole string. */
6593 xassert (it->sp > 0);
6594 pop_it (it);
6595 if (it->method == GET_FROM_STRING)
6596 goto consider_string_end;
6597 break;
6598
6599 default:
6600 /* There are no other methods defined, so this should be a bug. */
6601 abort ();
6602 }
6603
6604 xassert (it->method != GET_FROM_STRING
6605 || (STRINGP (it->string)
6606 && IT_STRING_CHARPOS (*it) >= 0));
6607 }
6608
6609 /* Load IT's display element fields with information about the next
6610 display element which comes from a display table entry or from the
6611 result of translating a control character to one of the forms `^C'
6612 or `\003'.
6613
6614 IT->dpvec holds the glyphs to return as characters.
6615 IT->saved_face_id holds the face id before the display vector--it
6616 is restored into IT->face_id in set_iterator_to_next. */
6617
6618 static int
6619 next_element_from_display_vector (struct it *it)
6620 {
6621 Lisp_Object gc;
6622
6623 /* Precondition. */
6624 xassert (it->dpvec && it->current.dpvec_index >= 0);
6625
6626 it->face_id = it->saved_face_id;
6627
6628 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6629 That seemed totally bogus - so I changed it... */
6630 gc = it->dpvec[it->current.dpvec_index];
6631
6632 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6633 {
6634 it->c = GLYPH_CODE_CHAR (gc);
6635 it->len = CHAR_BYTES (it->c);
6636
6637 /* The entry may contain a face id to use. Such a face id is
6638 the id of a Lisp face, not a realized face. A face id of
6639 zero means no face is specified. */
6640 if (it->dpvec_face_id >= 0)
6641 it->face_id = it->dpvec_face_id;
6642 else
6643 {
6644 int lface_id = GLYPH_CODE_FACE (gc);
6645 if (lface_id > 0)
6646 it->face_id = merge_faces (it->f, Qt, lface_id,
6647 it->saved_face_id);
6648 }
6649 }
6650 else
6651 /* Display table entry is invalid. Return a space. */
6652 it->c = ' ', it->len = 1;
6653
6654 /* Don't change position and object of the iterator here. They are
6655 still the values of the character that had this display table
6656 entry or was translated, and that's what we want. */
6657 it->what = IT_CHARACTER;
6658 return 1;
6659 }
6660
6661 /* Get the first element of string/buffer in the visual order, after
6662 being reseated to a new position in a string or a buffer. */
6663 static void
6664 get_visually_first_element (struct it *it)
6665 {
6666 int string_p = STRINGP (it->string) || it->s;
6667 EMACS_INT eob = (string_p ? it->string_nchars : ZV);
6668 EMACS_INT bob = (string_p ? 0 : BEGV);
6669
6670 if (STRINGP (it->string))
6671 {
6672 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6673 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6674 }
6675 else
6676 {
6677 it->bidi_it.charpos = IT_CHARPOS (*it);
6678 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6679 }
6680
6681 if (it->bidi_it.charpos == eob)
6682 {
6683 /* Nothing to do, but reset the FIRST_ELT flag, like
6684 bidi_paragraph_init does, because we are not going to
6685 call it. */
6686 it->bidi_it.first_elt = 0;
6687 }
6688 else if (it->bidi_it.charpos == bob
6689 || (!string_p
6690 /* FIXME: Should support all Unicode line separators. */
6691 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6692 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6693 {
6694 /* If we are at the beginning of a line/string, we can produce
6695 the next element right away. */
6696 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6697 bidi_move_to_visually_next (&it->bidi_it);
6698 }
6699 else
6700 {
6701 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6702
6703 /* We need to prime the bidi iterator starting at the line's or
6704 string's beginning, before we will be able to produce the
6705 next element. */
6706 if (string_p)
6707 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6708 else
6709 {
6710 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6711 -1);
6712 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6713 }
6714 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6715 do
6716 {
6717 /* Now return to buffer/string position where we were asked
6718 to get the next display element, and produce that. */
6719 bidi_move_to_visually_next (&it->bidi_it);
6720 }
6721 while (it->bidi_it.bytepos != orig_bytepos
6722 && it->bidi_it.charpos < eob);
6723 }
6724
6725 /* Adjust IT's position information to where we ended up. */
6726 if (STRINGP (it->string))
6727 {
6728 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6729 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6730 }
6731 else
6732 {
6733 IT_CHARPOS (*it) = it->bidi_it.charpos;
6734 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6735 }
6736
6737 if (STRINGP (it->string) || !it->s)
6738 {
6739 EMACS_INT stop, charpos, bytepos;
6740
6741 if (STRINGP (it->string))
6742 {
6743 xassert (!it->s);
6744 stop = SCHARS (it->string);
6745 if (stop > it->end_charpos)
6746 stop = it->end_charpos;
6747 charpos = IT_STRING_CHARPOS (*it);
6748 bytepos = IT_STRING_BYTEPOS (*it);
6749 }
6750 else
6751 {
6752 stop = it->end_charpos;
6753 charpos = IT_CHARPOS (*it);
6754 bytepos = IT_BYTEPOS (*it);
6755 }
6756 if (it->bidi_it.scan_dir < 0)
6757 stop = -1;
6758 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
6759 it->string);
6760 }
6761 }
6762
6763 /* Load IT with the next display element from Lisp string IT->string.
6764 IT->current.string_pos is the current position within the string.
6765 If IT->current.overlay_string_index >= 0, the Lisp string is an
6766 overlay string. */
6767
6768 static int
6769 next_element_from_string (struct it *it)
6770 {
6771 struct text_pos position;
6772
6773 xassert (STRINGP (it->string));
6774 xassert (!it->bidi_p || it->string == it->bidi_it.string.lstring);
6775 xassert (IT_STRING_CHARPOS (*it) >= 0);
6776 position = it->current.string_pos;
6777
6778 /* With bidi reordering, the character to display might not be the
6779 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
6780 that we were reseat()ed to a new string, whose paragraph
6781 direction is not known. */
6782 if (it->bidi_p && it->bidi_it.first_elt)
6783 {
6784 get_visually_first_element (it);
6785 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
6786 }
6787
6788 /* Time to check for invisible text? */
6789 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
6790 {
6791 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
6792 {
6793 if (!(!it->bidi_p
6794 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6795 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
6796 {
6797 /* With bidi non-linear iteration, we could find
6798 ourselves far beyond the last computed stop_charpos,
6799 with several other stop positions in between that we
6800 missed. Scan them all now, in buffer's logical
6801 order, until we find and handle the last stop_charpos
6802 that precedes our current position. */
6803 handle_stop_backwards (it, it->stop_charpos);
6804 return GET_NEXT_DISPLAY_ELEMENT (it);
6805 }
6806 else
6807 {
6808 if (it->bidi_p)
6809 {
6810 /* Take note of the stop position we just moved
6811 across, for when we will move back across it. */
6812 it->prev_stop = it->stop_charpos;
6813 /* If we are at base paragraph embedding level, take
6814 note of the last stop position seen at this
6815 level. */
6816 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6817 it->base_level_stop = it->stop_charpos;
6818 }
6819 handle_stop (it);
6820
6821 /* Since a handler may have changed IT->method, we must
6822 recurse here. */
6823 return GET_NEXT_DISPLAY_ELEMENT (it);
6824 }
6825 }
6826 else if (it->bidi_p
6827 /* If we are before prev_stop, we may have overstepped
6828 on our way backwards a stop_pos, and if so, we need
6829 to handle that stop_pos. */
6830 && IT_STRING_CHARPOS (*it) < it->prev_stop
6831 /* We can sometimes back up for reasons that have nothing
6832 to do with bidi reordering. E.g., compositions. The
6833 code below is only needed when we are above the base
6834 embedding level, so test for that explicitly. */
6835 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
6836 {
6837 /* If we lost track of base_level_stop, we have no better place
6838 for handle_stop_backwards to start from than BEGV. This
6839 happens, e.g., when we were reseated to the previous
6840 screenful of text by vertical-motion. */
6841 if (it->base_level_stop <= 0
6842 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
6843 it->base_level_stop = 0;
6844 handle_stop_backwards (it, it->base_level_stop);
6845 return GET_NEXT_DISPLAY_ELEMENT (it);
6846 }
6847 }
6848
6849 if (it->current.overlay_string_index >= 0)
6850 {
6851 /* Get the next character from an overlay string. In overlay
6852 strings, There is no field width or padding with spaces to
6853 do. */
6854 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6855 {
6856 it->what = IT_EOB;
6857 return 0;
6858 }
6859 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6860 IT_STRING_BYTEPOS (*it),
6861 it->bidi_it.scan_dir < 0
6862 ? -1
6863 : SCHARS (it->string))
6864 && next_element_from_composition (it))
6865 {
6866 return 1;
6867 }
6868 else if (STRING_MULTIBYTE (it->string))
6869 {
6870 const unsigned char *s = (SDATA (it->string)
6871 + IT_STRING_BYTEPOS (*it));
6872 it->c = string_char_and_length (s, &it->len);
6873 }
6874 else
6875 {
6876 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6877 it->len = 1;
6878 }
6879 }
6880 else
6881 {
6882 /* Get the next character from a Lisp string that is not an
6883 overlay string. Such strings come from the mode line, for
6884 example. We may have to pad with spaces, or truncate the
6885 string. See also next_element_from_c_string. */
6886 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6887 {
6888 it->what = IT_EOB;
6889 return 0;
6890 }
6891 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6892 {
6893 /* Pad with spaces. */
6894 it->c = ' ', it->len = 1;
6895 CHARPOS (position) = BYTEPOS (position) = -1;
6896 }
6897 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6898 IT_STRING_BYTEPOS (*it),
6899 it->bidi_it.scan_dir < 0
6900 ? -1
6901 : it->string_nchars)
6902 && next_element_from_composition (it))
6903 {
6904 return 1;
6905 }
6906 else if (STRING_MULTIBYTE (it->string))
6907 {
6908 const unsigned char *s = (SDATA (it->string)
6909 + IT_STRING_BYTEPOS (*it));
6910 it->c = string_char_and_length (s, &it->len);
6911 }
6912 else
6913 {
6914 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6915 it->len = 1;
6916 }
6917 }
6918
6919 /* Record what we have and where it came from. */
6920 it->what = IT_CHARACTER;
6921 it->object = it->string;
6922 it->position = position;
6923 return 1;
6924 }
6925
6926
6927 /* Load IT with next display element from C string IT->s.
6928 IT->string_nchars is the maximum number of characters to return
6929 from the string. IT->end_charpos may be greater than
6930 IT->string_nchars when this function is called, in which case we
6931 may have to return padding spaces. Value is zero if end of string
6932 reached, including padding spaces. */
6933
6934 static int
6935 next_element_from_c_string (struct it *it)
6936 {
6937 int success_p = 1;
6938
6939 xassert (it->s);
6940 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
6941 it->what = IT_CHARACTER;
6942 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6943 it->object = Qnil;
6944
6945 /* With bidi reordering, the character to display might not be the
6946 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6947 we were reseated to a new string, whose paragraph direction is
6948 not known. */
6949 if (it->bidi_p && it->bidi_it.first_elt)
6950 get_visually_first_element (it);
6951
6952 /* IT's position can be greater than IT->string_nchars in case a
6953 field width or precision has been specified when the iterator was
6954 initialized. */
6955 if (IT_CHARPOS (*it) >= it->end_charpos)
6956 {
6957 /* End of the game. */
6958 it->what = IT_EOB;
6959 success_p = 0;
6960 }
6961 else if (IT_CHARPOS (*it) >= it->string_nchars)
6962 {
6963 /* Pad with spaces. */
6964 it->c = ' ', it->len = 1;
6965 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6966 }
6967 else if (it->multibyte_p)
6968 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6969 else
6970 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6971
6972 return success_p;
6973 }
6974
6975
6976 /* Set up IT to return characters from an ellipsis, if appropriate.
6977 The definition of the ellipsis glyphs may come from a display table
6978 entry. This function fills IT with the first glyph from the
6979 ellipsis if an ellipsis is to be displayed. */
6980
6981 static int
6982 next_element_from_ellipsis (struct it *it)
6983 {
6984 if (it->selective_display_ellipsis_p)
6985 setup_for_ellipsis (it, it->len);
6986 else
6987 {
6988 /* The face at the current position may be different from the
6989 face we find after the invisible text. Remember what it
6990 was in IT->saved_face_id, and signal that it's there by
6991 setting face_before_selective_p. */
6992 it->saved_face_id = it->face_id;
6993 it->method = GET_FROM_BUFFER;
6994 it->object = it->w->buffer;
6995 reseat_at_next_visible_line_start (it, 1);
6996 it->face_before_selective_p = 1;
6997 }
6998
6999 return GET_NEXT_DISPLAY_ELEMENT (it);
7000 }
7001
7002
7003 /* Deliver an image display element. The iterator IT is already
7004 filled with image information (done in handle_display_prop). Value
7005 is always 1. */
7006
7007
7008 static int
7009 next_element_from_image (struct it *it)
7010 {
7011 it->what = IT_IMAGE;
7012 it->ignore_overlay_strings_at_pos_p = 0;
7013 return 1;
7014 }
7015
7016
7017 /* Fill iterator IT with next display element from a stretch glyph
7018 property. IT->object is the value of the text property. Value is
7019 always 1. */
7020
7021 static int
7022 next_element_from_stretch (struct it *it)
7023 {
7024 it->what = IT_STRETCH;
7025 return 1;
7026 }
7027
7028 /* Scan forward from CHARPOS in the current buffer/string, until we
7029 find a stop position > current IT's position. Then handle the stop
7030 position before that. This is called when we bump into a stop
7031 position while reordering bidirectional text. CHARPOS should be
7032 the last previously processed stop_pos (or BEGV/0, if none were
7033 processed yet) whose position is less that IT's current
7034 position. */
7035
7036 static void
7037 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7038 {
7039 int bufp = !STRINGP (it->string);
7040 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7041 struct display_pos save_current = it->current;
7042 struct text_pos save_position = it->position;
7043 struct text_pos pos1;
7044 EMACS_INT next_stop;
7045
7046 /* Scan in strict logical order. */
7047 it->bidi_p = 0;
7048 do
7049 {
7050 it->prev_stop = charpos;
7051 if (bufp)
7052 {
7053 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7054 reseat_1 (it, pos1, 0);
7055 }
7056 else
7057 it->current.string_pos = string_pos (charpos, it->string);
7058 compute_stop_pos (it);
7059 /* We must advance forward, right? */
7060 if (it->stop_charpos <= it->prev_stop)
7061 abort ();
7062 charpos = it->stop_charpos;
7063 }
7064 while (charpos <= where_we_are);
7065
7066 next_stop = it->stop_charpos;
7067 it->stop_charpos = it->prev_stop;
7068 it->bidi_p = 1;
7069 it->current = save_current;
7070 it->position = save_position;
7071 handle_stop (it);
7072 it->stop_charpos = next_stop;
7073 }
7074
7075 /* Load IT with the next display element from current_buffer. Value
7076 is zero if end of buffer reached. IT->stop_charpos is the next
7077 position at which to stop and check for text properties or buffer
7078 end. */
7079
7080 static int
7081 next_element_from_buffer (struct it *it)
7082 {
7083 int success_p = 1;
7084
7085 xassert (IT_CHARPOS (*it) >= BEGV);
7086 xassert (NILP (it->string) && !it->s);
7087 xassert (!it->bidi_p
7088 || (it->bidi_it.string.lstring == Qnil
7089 && it->bidi_it.string.s == NULL));
7090
7091 /* With bidi reordering, the character to display might not be the
7092 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7093 we were reseat()ed to a new buffer position, which is potentially
7094 a different paragraph. */
7095 if (it->bidi_p && it->bidi_it.first_elt)
7096 {
7097 get_visually_first_element (it);
7098 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7099 }
7100
7101 if (IT_CHARPOS (*it) >= it->stop_charpos)
7102 {
7103 if (IT_CHARPOS (*it) >= it->end_charpos)
7104 {
7105 int overlay_strings_follow_p;
7106
7107 /* End of the game, except when overlay strings follow that
7108 haven't been returned yet. */
7109 if (it->overlay_strings_at_end_processed_p)
7110 overlay_strings_follow_p = 0;
7111 else
7112 {
7113 it->overlay_strings_at_end_processed_p = 1;
7114 overlay_strings_follow_p = get_overlay_strings (it, 0);
7115 }
7116
7117 if (overlay_strings_follow_p)
7118 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7119 else
7120 {
7121 it->what = IT_EOB;
7122 it->position = it->current.pos;
7123 success_p = 0;
7124 }
7125 }
7126 else if (!(!it->bidi_p
7127 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7128 || IT_CHARPOS (*it) == it->stop_charpos))
7129 {
7130 /* With bidi non-linear iteration, we could find ourselves
7131 far beyond the last computed stop_charpos, with several
7132 other stop positions in between that we missed. Scan
7133 them all now, in buffer's logical order, until we find
7134 and handle the last stop_charpos that precedes our
7135 current position. */
7136 handle_stop_backwards (it, it->stop_charpos);
7137 return GET_NEXT_DISPLAY_ELEMENT (it);
7138 }
7139 else
7140 {
7141 if (it->bidi_p)
7142 {
7143 /* Take note of the stop position we just moved across,
7144 for when we will move back across it. */
7145 it->prev_stop = it->stop_charpos;
7146 /* If we are at base paragraph embedding level, take
7147 note of the last stop position seen at this
7148 level. */
7149 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7150 it->base_level_stop = it->stop_charpos;
7151 }
7152 handle_stop (it);
7153 return GET_NEXT_DISPLAY_ELEMENT (it);
7154 }
7155 }
7156 else if (it->bidi_p
7157 /* If we are before prev_stop, we may have overstepped on
7158 our way backwards a stop_pos, and if so, we need to
7159 handle that stop_pos. */
7160 && IT_CHARPOS (*it) < it->prev_stop
7161 /* We can sometimes back up for reasons that have nothing
7162 to do with bidi reordering. E.g., compositions. The
7163 code below is only needed when we are above the base
7164 embedding level, so test for that explicitly. */
7165 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7166 {
7167 /* If we lost track of base_level_stop, we have no better place
7168 for handle_stop_backwards to start from than BEGV. This
7169 happens, e.g., when we were reseated to the previous
7170 screenful of text by vertical-motion. */
7171 if (it->base_level_stop <= 0
7172 || IT_CHARPOS (*it) < it->base_level_stop)
7173 it->base_level_stop = BEGV;
7174 handle_stop_backwards (it, it->base_level_stop);
7175 return GET_NEXT_DISPLAY_ELEMENT (it);
7176 }
7177 else
7178 {
7179 /* No face changes, overlays etc. in sight, so just return a
7180 character from current_buffer. */
7181 unsigned char *p;
7182 EMACS_INT stop;
7183
7184 /* Maybe run the redisplay end trigger hook. Performance note:
7185 This doesn't seem to cost measurable time. */
7186 if (it->redisplay_end_trigger_charpos
7187 && it->glyph_row
7188 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7189 run_redisplay_end_trigger_hook (it);
7190
7191 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7192 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7193 stop)
7194 && next_element_from_composition (it))
7195 {
7196 return 1;
7197 }
7198
7199 /* Get the next character, maybe multibyte. */
7200 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7201 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7202 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7203 else
7204 it->c = *p, it->len = 1;
7205
7206 /* Record what we have and where it came from. */
7207 it->what = IT_CHARACTER;
7208 it->object = it->w->buffer;
7209 it->position = it->current.pos;
7210
7211 /* Normally we return the character found above, except when we
7212 really want to return an ellipsis for selective display. */
7213 if (it->selective)
7214 {
7215 if (it->c == '\n')
7216 {
7217 /* A value of selective > 0 means hide lines indented more
7218 than that number of columns. */
7219 if (it->selective > 0
7220 && IT_CHARPOS (*it) + 1 < ZV
7221 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7222 IT_BYTEPOS (*it) + 1,
7223 (double) it->selective)) /* iftc */
7224 {
7225 success_p = next_element_from_ellipsis (it);
7226 it->dpvec_char_len = -1;
7227 }
7228 }
7229 else if (it->c == '\r' && it->selective == -1)
7230 {
7231 /* A value of selective == -1 means that everything from the
7232 CR to the end of the line is invisible, with maybe an
7233 ellipsis displayed for it. */
7234 success_p = next_element_from_ellipsis (it);
7235 it->dpvec_char_len = -1;
7236 }
7237 }
7238 }
7239
7240 /* Value is zero if end of buffer reached. */
7241 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7242 return success_p;
7243 }
7244
7245
7246 /* Run the redisplay end trigger hook for IT. */
7247
7248 static void
7249 run_redisplay_end_trigger_hook (struct it *it)
7250 {
7251 Lisp_Object args[3];
7252
7253 /* IT->glyph_row should be non-null, i.e. we should be actually
7254 displaying something, or otherwise we should not run the hook. */
7255 xassert (it->glyph_row);
7256
7257 /* Set up hook arguments. */
7258 args[0] = Qredisplay_end_trigger_functions;
7259 args[1] = it->window;
7260 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7261 it->redisplay_end_trigger_charpos = 0;
7262
7263 /* Since we are *trying* to run these functions, don't try to run
7264 them again, even if they get an error. */
7265 it->w->redisplay_end_trigger = Qnil;
7266 Frun_hook_with_args (3, args);
7267
7268 /* Notice if it changed the face of the character we are on. */
7269 handle_face_prop (it);
7270 }
7271
7272
7273 /* Deliver a composition display element. Unlike the other
7274 next_element_from_XXX, this function is not registered in the array
7275 get_next_element[]. It is called from next_element_from_buffer and
7276 next_element_from_string when necessary. */
7277
7278 static int
7279 next_element_from_composition (struct it *it)
7280 {
7281 it->what = IT_COMPOSITION;
7282 it->len = it->cmp_it.nbytes;
7283 if (STRINGP (it->string))
7284 {
7285 if (it->c < 0)
7286 {
7287 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7288 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7289 return 0;
7290 }
7291 it->position = it->current.string_pos;
7292 it->object = it->string;
7293 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7294 IT_STRING_BYTEPOS (*it), it->string);
7295 }
7296 else
7297 {
7298 if (it->c < 0)
7299 {
7300 IT_CHARPOS (*it) += it->cmp_it.nchars;
7301 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7302 if (it->bidi_p)
7303 {
7304 if (it->bidi_it.new_paragraph)
7305 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7306 /* Resync the bidi iterator with IT's new position.
7307 FIXME: this doesn't support bidirectional text. */
7308 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7309 bidi_move_to_visually_next (&it->bidi_it);
7310 }
7311 return 0;
7312 }
7313 it->position = it->current.pos;
7314 it->object = it->w->buffer;
7315 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7316 IT_BYTEPOS (*it), Qnil);
7317 }
7318 return 1;
7319 }
7320
7321
7322 \f
7323 /***********************************************************************
7324 Moving an iterator without producing glyphs
7325 ***********************************************************************/
7326
7327 /* Check if iterator is at a position corresponding to a valid buffer
7328 position after some move_it_ call. */
7329
7330 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7331 ((it)->method == GET_FROM_STRING \
7332 ? IT_STRING_CHARPOS (*it) == 0 \
7333 : 1)
7334
7335
7336 /* Move iterator IT to a specified buffer or X position within one
7337 line on the display without producing glyphs.
7338
7339 OP should be a bit mask including some or all of these bits:
7340 MOVE_TO_X: Stop upon reaching x-position TO_X.
7341 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7342 Regardless of OP's value, stop upon reaching the end of the display line.
7343
7344 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7345 This means, in particular, that TO_X includes window's horizontal
7346 scroll amount.
7347
7348 The return value has several possible values that
7349 say what condition caused the scan to stop:
7350
7351 MOVE_POS_MATCH_OR_ZV
7352 - when TO_POS or ZV was reached.
7353
7354 MOVE_X_REACHED
7355 -when TO_X was reached before TO_POS or ZV were reached.
7356
7357 MOVE_LINE_CONTINUED
7358 - when we reached the end of the display area and the line must
7359 be continued.
7360
7361 MOVE_LINE_TRUNCATED
7362 - when we reached the end of the display area and the line is
7363 truncated.
7364
7365 MOVE_NEWLINE_OR_CR
7366 - when we stopped at a line end, i.e. a newline or a CR and selective
7367 display is on. */
7368
7369 static enum move_it_result
7370 move_it_in_display_line_to (struct it *it,
7371 EMACS_INT to_charpos, int to_x,
7372 enum move_operation_enum op)
7373 {
7374 enum move_it_result result = MOVE_UNDEFINED;
7375 struct glyph_row *saved_glyph_row;
7376 struct it wrap_it, atpos_it, atx_it;
7377 int may_wrap = 0;
7378 enum it_method prev_method = it->method;
7379 EMACS_INT prev_pos = IT_CHARPOS (*it);
7380
7381 /* Don't produce glyphs in produce_glyphs. */
7382 saved_glyph_row = it->glyph_row;
7383 it->glyph_row = NULL;
7384
7385 /* Use wrap_it to save a copy of IT wherever a word wrap could
7386 occur. Use atpos_it to save a copy of IT at the desired buffer
7387 position, if found, so that we can scan ahead and check if the
7388 word later overshoots the window edge. Use atx_it similarly, for
7389 pixel positions. */
7390 wrap_it.sp = -1;
7391 atpos_it.sp = -1;
7392 atx_it.sp = -1;
7393
7394 #define BUFFER_POS_REACHED_P() \
7395 ((op & MOVE_TO_POS) != 0 \
7396 && BUFFERP (it->object) \
7397 && (IT_CHARPOS (*it) == to_charpos \
7398 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7399 && (it->method == GET_FROM_BUFFER \
7400 || (it->method == GET_FROM_DISPLAY_VECTOR \
7401 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7402
7403 /* If there's a line-/wrap-prefix, handle it. */
7404 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7405 && it->current_y < it->last_visible_y)
7406 handle_line_prefix (it);
7407
7408 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7409 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7410
7411 while (1)
7412 {
7413 int x, i, ascent = 0, descent = 0;
7414
7415 /* Utility macro to reset an iterator with x, ascent, and descent. */
7416 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7417 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7418 (IT)->max_descent = descent)
7419
7420 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7421 glyph). */
7422 if ((op & MOVE_TO_POS) != 0
7423 && BUFFERP (it->object)
7424 && it->method == GET_FROM_BUFFER
7425 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7426 || (it->bidi_p
7427 && (prev_method == GET_FROM_IMAGE
7428 || prev_method == GET_FROM_STRETCH)
7429 /* Passed TO_CHARPOS from left to right. */
7430 && ((prev_pos < to_charpos
7431 && IT_CHARPOS (*it) > to_charpos)
7432 /* Passed TO_CHARPOS from right to left. */
7433 || (prev_pos > to_charpos
7434 && IT_CHARPOS (*it) < to_charpos)))))
7435 {
7436 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7437 {
7438 result = MOVE_POS_MATCH_OR_ZV;
7439 break;
7440 }
7441 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7442 /* If wrap_it is valid, the current position might be in a
7443 word that is wrapped. So, save the iterator in
7444 atpos_it and continue to see if wrapping happens. */
7445 atpos_it = *it;
7446 }
7447
7448 prev_method = it->method;
7449 if (it->method == GET_FROM_BUFFER)
7450 prev_pos = IT_CHARPOS (*it);
7451 /* Stop when ZV reached.
7452 We used to stop here when TO_CHARPOS reached as well, but that is
7453 too soon if this glyph does not fit on this line. So we handle it
7454 explicitly below. */
7455 if (!get_next_display_element (it))
7456 {
7457 result = MOVE_POS_MATCH_OR_ZV;
7458 break;
7459 }
7460
7461 if (it->line_wrap == TRUNCATE)
7462 {
7463 if (BUFFER_POS_REACHED_P ())
7464 {
7465 result = MOVE_POS_MATCH_OR_ZV;
7466 break;
7467 }
7468 }
7469 else
7470 {
7471 if (it->line_wrap == WORD_WRAP)
7472 {
7473 if (IT_DISPLAYING_WHITESPACE (it))
7474 may_wrap = 1;
7475 else if (may_wrap)
7476 {
7477 /* We have reached a glyph that follows one or more
7478 whitespace characters. If the position is
7479 already found, we are done. */
7480 if (atpos_it.sp >= 0)
7481 {
7482 *it = atpos_it;
7483 result = MOVE_POS_MATCH_OR_ZV;
7484 goto done;
7485 }
7486 if (atx_it.sp >= 0)
7487 {
7488 *it = atx_it;
7489 result = MOVE_X_REACHED;
7490 goto done;
7491 }
7492 /* Otherwise, we can wrap here. */
7493 wrap_it = *it;
7494 may_wrap = 0;
7495 }
7496 }
7497 }
7498
7499 /* Remember the line height for the current line, in case
7500 the next element doesn't fit on the line. */
7501 ascent = it->max_ascent;
7502 descent = it->max_descent;
7503
7504 /* The call to produce_glyphs will get the metrics of the
7505 display element IT is loaded with. Record the x-position
7506 before this display element, in case it doesn't fit on the
7507 line. */
7508 x = it->current_x;
7509
7510 PRODUCE_GLYPHS (it);
7511
7512 if (it->area != TEXT_AREA)
7513 {
7514 set_iterator_to_next (it, 1);
7515 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7516 SET_TEXT_POS (this_line_min_pos,
7517 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7518 continue;
7519 }
7520
7521 /* The number of glyphs we get back in IT->nglyphs will normally
7522 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7523 character on a terminal frame, or (iii) a line end. For the
7524 second case, IT->nglyphs - 1 padding glyphs will be present.
7525 (On X frames, there is only one glyph produced for a
7526 composite character.)
7527
7528 The behavior implemented below means, for continuation lines,
7529 that as many spaces of a TAB as fit on the current line are
7530 displayed there. For terminal frames, as many glyphs of a
7531 multi-glyph character are displayed in the current line, too.
7532 This is what the old redisplay code did, and we keep it that
7533 way. Under X, the whole shape of a complex character must
7534 fit on the line or it will be completely displayed in the
7535 next line.
7536
7537 Note that both for tabs and padding glyphs, all glyphs have
7538 the same width. */
7539 if (it->nglyphs)
7540 {
7541 /* More than one glyph or glyph doesn't fit on line. All
7542 glyphs have the same width. */
7543 int single_glyph_width = it->pixel_width / it->nglyphs;
7544 int new_x;
7545 int x_before_this_char = x;
7546 int hpos_before_this_char = it->hpos;
7547
7548 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7549 {
7550 new_x = x + single_glyph_width;
7551
7552 /* We want to leave anything reaching TO_X to the caller. */
7553 if ((op & MOVE_TO_X) && new_x > to_x)
7554 {
7555 if (BUFFER_POS_REACHED_P ())
7556 {
7557 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7558 goto buffer_pos_reached;
7559 if (atpos_it.sp < 0)
7560 {
7561 atpos_it = *it;
7562 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7563 }
7564 }
7565 else
7566 {
7567 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7568 {
7569 it->current_x = x;
7570 result = MOVE_X_REACHED;
7571 break;
7572 }
7573 if (atx_it.sp < 0)
7574 {
7575 atx_it = *it;
7576 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7577 }
7578 }
7579 }
7580
7581 if (/* Lines are continued. */
7582 it->line_wrap != TRUNCATE
7583 && (/* And glyph doesn't fit on the line. */
7584 new_x > it->last_visible_x
7585 /* Or it fits exactly and we're on a window
7586 system frame. */
7587 || (new_x == it->last_visible_x
7588 && FRAME_WINDOW_P (it->f))))
7589 {
7590 if (/* IT->hpos == 0 means the very first glyph
7591 doesn't fit on the line, e.g. a wide image. */
7592 it->hpos == 0
7593 || (new_x == it->last_visible_x
7594 && FRAME_WINDOW_P (it->f)))
7595 {
7596 ++it->hpos;
7597 it->current_x = new_x;
7598
7599 /* The character's last glyph just barely fits
7600 in this row. */
7601 if (i == it->nglyphs - 1)
7602 {
7603 /* If this is the destination position,
7604 return a position *before* it in this row,
7605 now that we know it fits in this row. */
7606 if (BUFFER_POS_REACHED_P ())
7607 {
7608 if (it->line_wrap != WORD_WRAP
7609 || wrap_it.sp < 0)
7610 {
7611 it->hpos = hpos_before_this_char;
7612 it->current_x = x_before_this_char;
7613 result = MOVE_POS_MATCH_OR_ZV;
7614 break;
7615 }
7616 if (it->line_wrap == WORD_WRAP
7617 && atpos_it.sp < 0)
7618 {
7619 atpos_it = *it;
7620 atpos_it.current_x = x_before_this_char;
7621 atpos_it.hpos = hpos_before_this_char;
7622 }
7623 }
7624
7625 set_iterator_to_next (it, 1);
7626 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7627 SET_TEXT_POS (this_line_min_pos,
7628 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7629 /* On graphical terminals, newlines may
7630 "overflow" into the fringe if
7631 overflow-newline-into-fringe is non-nil.
7632 On text-only terminals, newlines may
7633 overflow into the last glyph on the
7634 display line.*/
7635 if (!FRAME_WINDOW_P (it->f)
7636 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7637 {
7638 if (!get_next_display_element (it))
7639 {
7640 result = MOVE_POS_MATCH_OR_ZV;
7641 break;
7642 }
7643 if (BUFFER_POS_REACHED_P ())
7644 {
7645 if (ITERATOR_AT_END_OF_LINE_P (it))
7646 result = MOVE_POS_MATCH_OR_ZV;
7647 else
7648 result = MOVE_LINE_CONTINUED;
7649 break;
7650 }
7651 if (ITERATOR_AT_END_OF_LINE_P (it))
7652 {
7653 result = MOVE_NEWLINE_OR_CR;
7654 break;
7655 }
7656 }
7657 }
7658 }
7659 else
7660 IT_RESET_X_ASCENT_DESCENT (it);
7661
7662 if (wrap_it.sp >= 0)
7663 {
7664 *it = wrap_it;
7665 atpos_it.sp = -1;
7666 atx_it.sp = -1;
7667 }
7668
7669 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7670 IT_CHARPOS (*it)));
7671 result = MOVE_LINE_CONTINUED;
7672 break;
7673 }
7674
7675 if (BUFFER_POS_REACHED_P ())
7676 {
7677 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7678 goto buffer_pos_reached;
7679 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7680 {
7681 atpos_it = *it;
7682 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7683 }
7684 }
7685
7686 if (new_x > it->first_visible_x)
7687 {
7688 /* Glyph is visible. Increment number of glyphs that
7689 would be displayed. */
7690 ++it->hpos;
7691 }
7692 }
7693
7694 if (result != MOVE_UNDEFINED)
7695 break;
7696 }
7697 else if (BUFFER_POS_REACHED_P ())
7698 {
7699 buffer_pos_reached:
7700 IT_RESET_X_ASCENT_DESCENT (it);
7701 result = MOVE_POS_MATCH_OR_ZV;
7702 break;
7703 }
7704 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7705 {
7706 /* Stop when TO_X specified and reached. This check is
7707 necessary here because of lines consisting of a line end,
7708 only. The line end will not produce any glyphs and we
7709 would never get MOVE_X_REACHED. */
7710 xassert (it->nglyphs == 0);
7711 result = MOVE_X_REACHED;
7712 break;
7713 }
7714
7715 /* Is this a line end? If yes, we're done. */
7716 if (ITERATOR_AT_END_OF_LINE_P (it))
7717 {
7718 result = MOVE_NEWLINE_OR_CR;
7719 break;
7720 }
7721
7722 if (it->method == GET_FROM_BUFFER)
7723 prev_pos = IT_CHARPOS (*it);
7724 /* The current display element has been consumed. Advance
7725 to the next. */
7726 set_iterator_to_next (it, 1);
7727 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7728 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7729
7730 /* Stop if lines are truncated and IT's current x-position is
7731 past the right edge of the window now. */
7732 if (it->line_wrap == TRUNCATE
7733 && it->current_x >= it->last_visible_x)
7734 {
7735 if (!FRAME_WINDOW_P (it->f)
7736 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7737 {
7738 if (!get_next_display_element (it)
7739 || BUFFER_POS_REACHED_P ())
7740 {
7741 result = MOVE_POS_MATCH_OR_ZV;
7742 break;
7743 }
7744 if (ITERATOR_AT_END_OF_LINE_P (it))
7745 {
7746 result = MOVE_NEWLINE_OR_CR;
7747 break;
7748 }
7749 }
7750 result = MOVE_LINE_TRUNCATED;
7751 break;
7752 }
7753 #undef IT_RESET_X_ASCENT_DESCENT
7754 }
7755
7756 #undef BUFFER_POS_REACHED_P
7757
7758 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7759 restore the saved iterator. */
7760 if (atpos_it.sp >= 0)
7761 *it = atpos_it;
7762 else if (atx_it.sp >= 0)
7763 *it = atx_it;
7764
7765 done:
7766
7767 /* Restore the iterator settings altered at the beginning of this
7768 function. */
7769 it->glyph_row = saved_glyph_row;
7770 return result;
7771 }
7772
7773 /* For external use. */
7774 void
7775 move_it_in_display_line (struct it *it,
7776 EMACS_INT to_charpos, int to_x,
7777 enum move_operation_enum op)
7778 {
7779 if (it->line_wrap == WORD_WRAP
7780 && (op & MOVE_TO_X))
7781 {
7782 struct it save_it = *it;
7783 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7784 /* When word-wrap is on, TO_X may lie past the end
7785 of a wrapped line. Then it->current is the
7786 character on the next line, so backtrack to the
7787 space before the wrap point. */
7788 if (skip == MOVE_LINE_CONTINUED)
7789 {
7790 int prev_x = max (it->current_x - 1, 0);
7791 *it = save_it;
7792 move_it_in_display_line_to
7793 (it, -1, prev_x, MOVE_TO_X);
7794 }
7795 }
7796 else
7797 move_it_in_display_line_to (it, to_charpos, to_x, op);
7798 }
7799
7800
7801 /* Move IT forward until it satisfies one or more of the criteria in
7802 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7803
7804 OP is a bit-mask that specifies where to stop, and in particular,
7805 which of those four position arguments makes a difference. See the
7806 description of enum move_operation_enum.
7807
7808 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7809 screen line, this function will set IT to the next position >
7810 TO_CHARPOS. */
7811
7812 void
7813 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7814 {
7815 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7816 int line_height, line_start_x = 0, reached = 0;
7817
7818 for (;;)
7819 {
7820 if (op & MOVE_TO_VPOS)
7821 {
7822 /* If no TO_CHARPOS and no TO_X specified, stop at the
7823 start of the line TO_VPOS. */
7824 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7825 {
7826 if (it->vpos == to_vpos)
7827 {
7828 reached = 1;
7829 break;
7830 }
7831 else
7832 skip = move_it_in_display_line_to (it, -1, -1, 0);
7833 }
7834 else
7835 {
7836 /* TO_VPOS >= 0 means stop at TO_X in the line at
7837 TO_VPOS, or at TO_POS, whichever comes first. */
7838 if (it->vpos == to_vpos)
7839 {
7840 reached = 2;
7841 break;
7842 }
7843
7844 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7845
7846 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7847 {
7848 reached = 3;
7849 break;
7850 }
7851 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7852 {
7853 /* We have reached TO_X but not in the line we want. */
7854 skip = move_it_in_display_line_to (it, to_charpos,
7855 -1, MOVE_TO_POS);
7856 if (skip == MOVE_POS_MATCH_OR_ZV)
7857 {
7858 reached = 4;
7859 break;
7860 }
7861 }
7862 }
7863 }
7864 else if (op & MOVE_TO_Y)
7865 {
7866 struct it it_backup;
7867
7868 if (it->line_wrap == WORD_WRAP)
7869 it_backup = *it;
7870
7871 /* TO_Y specified means stop at TO_X in the line containing
7872 TO_Y---or at TO_CHARPOS if this is reached first. The
7873 problem is that we can't really tell whether the line
7874 contains TO_Y before we have completely scanned it, and
7875 this may skip past TO_X. What we do is to first scan to
7876 TO_X.
7877
7878 If TO_X is not specified, use a TO_X of zero. The reason
7879 is to make the outcome of this function more predictable.
7880 If we didn't use TO_X == 0, we would stop at the end of
7881 the line which is probably not what a caller would expect
7882 to happen. */
7883 skip = move_it_in_display_line_to
7884 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7885 (MOVE_TO_X | (op & MOVE_TO_POS)));
7886
7887 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7888 if (skip == MOVE_POS_MATCH_OR_ZV)
7889 reached = 5;
7890 else if (skip == MOVE_X_REACHED)
7891 {
7892 /* If TO_X was reached, we want to know whether TO_Y is
7893 in the line. We know this is the case if the already
7894 scanned glyphs make the line tall enough. Otherwise,
7895 we must check by scanning the rest of the line. */
7896 line_height = it->max_ascent + it->max_descent;
7897 if (to_y >= it->current_y
7898 && to_y < it->current_y + line_height)
7899 {
7900 reached = 6;
7901 break;
7902 }
7903 it_backup = *it;
7904 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7905 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7906 op & MOVE_TO_POS);
7907 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7908 line_height = it->max_ascent + it->max_descent;
7909 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7910
7911 if (to_y >= it->current_y
7912 && to_y < it->current_y + line_height)
7913 {
7914 /* If TO_Y is in this line and TO_X was reached
7915 above, we scanned too far. We have to restore
7916 IT's settings to the ones before skipping. */
7917 *it = it_backup;
7918 reached = 6;
7919 }
7920 else
7921 {
7922 skip = skip2;
7923 if (skip == MOVE_POS_MATCH_OR_ZV)
7924 reached = 7;
7925 }
7926 }
7927 else
7928 {
7929 /* Check whether TO_Y is in this line. */
7930 line_height = it->max_ascent + it->max_descent;
7931 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7932
7933 if (to_y >= it->current_y
7934 && to_y < it->current_y + line_height)
7935 {
7936 /* When word-wrap is on, TO_X may lie past the end
7937 of a wrapped line. Then it->current is the
7938 character on the next line, so backtrack to the
7939 space before the wrap point. */
7940 if (skip == MOVE_LINE_CONTINUED
7941 && it->line_wrap == WORD_WRAP)
7942 {
7943 int prev_x = max (it->current_x - 1, 0);
7944 *it = it_backup;
7945 skip = move_it_in_display_line_to
7946 (it, -1, prev_x, MOVE_TO_X);
7947 }
7948 reached = 6;
7949 }
7950 }
7951
7952 if (reached)
7953 break;
7954 }
7955 else if (BUFFERP (it->object)
7956 && (it->method == GET_FROM_BUFFER
7957 || it->method == GET_FROM_STRETCH)
7958 && IT_CHARPOS (*it) >= to_charpos)
7959 skip = MOVE_POS_MATCH_OR_ZV;
7960 else
7961 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7962
7963 switch (skip)
7964 {
7965 case MOVE_POS_MATCH_OR_ZV:
7966 reached = 8;
7967 goto out;
7968
7969 case MOVE_NEWLINE_OR_CR:
7970 set_iterator_to_next (it, 1);
7971 it->continuation_lines_width = 0;
7972 break;
7973
7974 case MOVE_LINE_TRUNCATED:
7975 it->continuation_lines_width = 0;
7976 reseat_at_next_visible_line_start (it, 0);
7977 if ((op & MOVE_TO_POS) != 0
7978 && IT_CHARPOS (*it) > to_charpos)
7979 {
7980 reached = 9;
7981 goto out;
7982 }
7983 break;
7984
7985 case MOVE_LINE_CONTINUED:
7986 /* For continued lines ending in a tab, some of the glyphs
7987 associated with the tab are displayed on the current
7988 line. Since it->current_x does not include these glyphs,
7989 we use it->last_visible_x instead. */
7990 if (it->c == '\t')
7991 {
7992 it->continuation_lines_width += it->last_visible_x;
7993 /* When moving by vpos, ensure that the iterator really
7994 advances to the next line (bug#847, bug#969). Fixme:
7995 do we need to do this in other circumstances? */
7996 if (it->current_x != it->last_visible_x
7997 && (op & MOVE_TO_VPOS)
7998 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7999 {
8000 line_start_x = it->current_x + it->pixel_width
8001 - it->last_visible_x;
8002 set_iterator_to_next (it, 0);
8003 }
8004 }
8005 else
8006 it->continuation_lines_width += it->current_x;
8007 break;
8008
8009 default:
8010 abort ();
8011 }
8012
8013 /* Reset/increment for the next run. */
8014 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8015 it->current_x = line_start_x;
8016 line_start_x = 0;
8017 it->hpos = 0;
8018 it->current_y += it->max_ascent + it->max_descent;
8019 ++it->vpos;
8020 last_height = it->max_ascent + it->max_descent;
8021 last_max_ascent = it->max_ascent;
8022 it->max_ascent = it->max_descent = 0;
8023 }
8024
8025 out:
8026
8027 /* On text terminals, we may stop at the end of a line in the middle
8028 of a multi-character glyph. If the glyph itself is continued,
8029 i.e. it is actually displayed on the next line, don't treat this
8030 stopping point as valid; move to the next line instead (unless
8031 that brings us offscreen). */
8032 if (!FRAME_WINDOW_P (it->f)
8033 && op & MOVE_TO_POS
8034 && IT_CHARPOS (*it) == to_charpos
8035 && it->what == IT_CHARACTER
8036 && it->nglyphs > 1
8037 && it->line_wrap == WINDOW_WRAP
8038 && it->current_x == it->last_visible_x - 1
8039 && it->c != '\n'
8040 && it->c != '\t'
8041 && it->vpos < XFASTINT (it->w->window_end_vpos))
8042 {
8043 it->continuation_lines_width += it->current_x;
8044 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8045 it->current_y += it->max_ascent + it->max_descent;
8046 ++it->vpos;
8047 last_height = it->max_ascent + it->max_descent;
8048 last_max_ascent = it->max_ascent;
8049 }
8050
8051 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8052 }
8053
8054
8055 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8056
8057 If DY > 0, move IT backward at least that many pixels. DY = 0
8058 means move IT backward to the preceding line start or BEGV. This
8059 function may move over more than DY pixels if IT->current_y - DY
8060 ends up in the middle of a line; in this case IT->current_y will be
8061 set to the top of the line moved to. */
8062
8063 void
8064 move_it_vertically_backward (struct it *it, int dy)
8065 {
8066 int nlines, h;
8067 struct it it2, it3;
8068 EMACS_INT start_pos;
8069
8070 move_further_back:
8071 xassert (dy >= 0);
8072
8073 start_pos = IT_CHARPOS (*it);
8074
8075 /* Estimate how many newlines we must move back. */
8076 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8077
8078 /* Set the iterator's position that many lines back. */
8079 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8080 back_to_previous_visible_line_start (it);
8081
8082 /* Reseat the iterator here. When moving backward, we don't want
8083 reseat to skip forward over invisible text, set up the iterator
8084 to deliver from overlay strings at the new position etc. So,
8085 use reseat_1 here. */
8086 reseat_1 (it, it->current.pos, 1);
8087
8088 /* We are now surely at a line start. */
8089 it->current_x = it->hpos = 0;
8090 it->continuation_lines_width = 0;
8091
8092 /* Move forward and see what y-distance we moved. First move to the
8093 start of the next line so that we get its height. We need this
8094 height to be able to tell whether we reached the specified
8095 y-distance. */
8096 it2 = *it;
8097 it2.max_ascent = it2.max_descent = 0;
8098 do
8099 {
8100 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8101 MOVE_TO_POS | MOVE_TO_VPOS);
8102 }
8103 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8104 xassert (IT_CHARPOS (*it) >= BEGV);
8105 it3 = it2;
8106
8107 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8108 xassert (IT_CHARPOS (*it) >= BEGV);
8109 /* H is the actual vertical distance from the position in *IT
8110 and the starting position. */
8111 h = it2.current_y - it->current_y;
8112 /* NLINES is the distance in number of lines. */
8113 nlines = it2.vpos - it->vpos;
8114
8115 /* Correct IT's y and vpos position
8116 so that they are relative to the starting point. */
8117 it->vpos -= nlines;
8118 it->current_y -= h;
8119
8120 if (dy == 0)
8121 {
8122 /* DY == 0 means move to the start of the screen line. The
8123 value of nlines is > 0 if continuation lines were involved. */
8124 if (nlines > 0)
8125 move_it_by_lines (it, nlines);
8126 }
8127 else
8128 {
8129 /* The y-position we try to reach, relative to *IT.
8130 Note that H has been subtracted in front of the if-statement. */
8131 int target_y = it->current_y + h - dy;
8132 int y0 = it3.current_y;
8133 int y1 = line_bottom_y (&it3);
8134 int line_height = y1 - y0;
8135
8136 /* If we did not reach target_y, try to move further backward if
8137 we can. If we moved too far backward, try to move forward. */
8138 if (target_y < it->current_y
8139 /* This is heuristic. In a window that's 3 lines high, with
8140 a line height of 13 pixels each, recentering with point
8141 on the bottom line will try to move -39/2 = 19 pixels
8142 backward. Try to avoid moving into the first line. */
8143 && (it->current_y - target_y
8144 > min (window_box_height (it->w), line_height * 2 / 3))
8145 && IT_CHARPOS (*it) > BEGV)
8146 {
8147 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8148 target_y - it->current_y));
8149 dy = it->current_y - target_y;
8150 goto move_further_back;
8151 }
8152 else if (target_y >= it->current_y + line_height
8153 && IT_CHARPOS (*it) < ZV)
8154 {
8155 /* Should move forward by at least one line, maybe more.
8156
8157 Note: Calling move_it_by_lines can be expensive on
8158 terminal frames, where compute_motion is used (via
8159 vmotion) to do the job, when there are very long lines
8160 and truncate-lines is nil. That's the reason for
8161 treating terminal frames specially here. */
8162
8163 if (!FRAME_WINDOW_P (it->f))
8164 move_it_vertically (it, target_y - (it->current_y + line_height));
8165 else
8166 {
8167 do
8168 {
8169 move_it_by_lines (it, 1);
8170 }
8171 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8172 }
8173 }
8174 }
8175 }
8176
8177
8178 /* Move IT by a specified amount of pixel lines DY. DY negative means
8179 move backwards. DY = 0 means move to start of screen line. At the
8180 end, IT will be on the start of a screen line. */
8181
8182 void
8183 move_it_vertically (struct it *it, int dy)
8184 {
8185 if (dy <= 0)
8186 move_it_vertically_backward (it, -dy);
8187 else
8188 {
8189 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8190 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8191 MOVE_TO_POS | MOVE_TO_Y);
8192 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8193
8194 /* If buffer ends in ZV without a newline, move to the start of
8195 the line to satisfy the post-condition. */
8196 if (IT_CHARPOS (*it) == ZV
8197 && ZV > BEGV
8198 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8199 move_it_by_lines (it, 0);
8200 }
8201 }
8202
8203
8204 /* Move iterator IT past the end of the text line it is in. */
8205
8206 void
8207 move_it_past_eol (struct it *it)
8208 {
8209 enum move_it_result rc;
8210
8211 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8212 if (rc == MOVE_NEWLINE_OR_CR)
8213 set_iterator_to_next (it, 0);
8214 }
8215
8216
8217 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8218 negative means move up. DVPOS == 0 means move to the start of the
8219 screen line.
8220
8221 Optimization idea: If we would know that IT->f doesn't use
8222 a face with proportional font, we could be faster for
8223 truncate-lines nil. */
8224
8225 void
8226 move_it_by_lines (struct it *it, int dvpos)
8227 {
8228
8229 /* The commented-out optimization uses vmotion on terminals. This
8230 gives bad results, because elements like it->what, on which
8231 callers such as pos_visible_p rely, aren't updated. */
8232 /* struct position pos;
8233 if (!FRAME_WINDOW_P (it->f))
8234 {
8235 struct text_pos textpos;
8236
8237 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8238 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8239 reseat (it, textpos, 1);
8240 it->vpos += pos.vpos;
8241 it->current_y += pos.vpos;
8242 }
8243 else */
8244
8245 if (dvpos == 0)
8246 {
8247 /* DVPOS == 0 means move to the start of the screen line. */
8248 move_it_vertically_backward (it, 0);
8249 xassert (it->current_x == 0 && it->hpos == 0);
8250 /* Let next call to line_bottom_y calculate real line height */
8251 last_height = 0;
8252 }
8253 else if (dvpos > 0)
8254 {
8255 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8256 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8257 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8258 }
8259 else
8260 {
8261 struct it it2;
8262 EMACS_INT start_charpos, i;
8263
8264 /* Start at the beginning of the screen line containing IT's
8265 position. This may actually move vertically backwards,
8266 in case of overlays, so adjust dvpos accordingly. */
8267 dvpos += it->vpos;
8268 move_it_vertically_backward (it, 0);
8269 dvpos -= it->vpos;
8270
8271 /* Go back -DVPOS visible lines and reseat the iterator there. */
8272 start_charpos = IT_CHARPOS (*it);
8273 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8274 back_to_previous_visible_line_start (it);
8275 reseat (it, it->current.pos, 1);
8276
8277 /* Move further back if we end up in a string or an image. */
8278 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8279 {
8280 /* First try to move to start of display line. */
8281 dvpos += it->vpos;
8282 move_it_vertically_backward (it, 0);
8283 dvpos -= it->vpos;
8284 if (IT_POS_VALID_AFTER_MOVE_P (it))
8285 break;
8286 /* If start of line is still in string or image,
8287 move further back. */
8288 back_to_previous_visible_line_start (it);
8289 reseat (it, it->current.pos, 1);
8290 dvpos--;
8291 }
8292
8293 it->current_x = it->hpos = 0;
8294
8295 /* Above call may have moved too far if continuation lines
8296 are involved. Scan forward and see if it did. */
8297 it2 = *it;
8298 it2.vpos = it2.current_y = 0;
8299 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8300 it->vpos -= it2.vpos;
8301 it->current_y -= it2.current_y;
8302 it->current_x = it->hpos = 0;
8303
8304 /* If we moved too far back, move IT some lines forward. */
8305 if (it2.vpos > -dvpos)
8306 {
8307 int delta = it2.vpos + dvpos;
8308 it2 = *it;
8309 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8310 /* Move back again if we got too far ahead. */
8311 if (IT_CHARPOS (*it) >= start_charpos)
8312 *it = it2;
8313 }
8314 }
8315 }
8316
8317 /* Return 1 if IT points into the middle of a display vector. */
8318
8319 int
8320 in_display_vector_p (struct it *it)
8321 {
8322 return (it->method == GET_FROM_DISPLAY_VECTOR
8323 && it->current.dpvec_index > 0
8324 && it->dpvec + it->current.dpvec_index != it->dpend);
8325 }
8326
8327 \f
8328 /***********************************************************************
8329 Messages
8330 ***********************************************************************/
8331
8332
8333 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8334 to *Messages*. */
8335
8336 void
8337 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8338 {
8339 Lisp_Object args[3];
8340 Lisp_Object msg, fmt;
8341 char *buffer;
8342 EMACS_INT len;
8343 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8344 USE_SAFE_ALLOCA;
8345
8346 /* Do nothing if called asynchronously. Inserting text into
8347 a buffer may call after-change-functions and alike and
8348 that would means running Lisp asynchronously. */
8349 if (handling_signal)
8350 return;
8351
8352 fmt = msg = Qnil;
8353 GCPRO4 (fmt, msg, arg1, arg2);
8354
8355 args[0] = fmt = build_string (format);
8356 args[1] = arg1;
8357 args[2] = arg2;
8358 msg = Fformat (3, args);
8359
8360 len = SBYTES (msg) + 1;
8361 SAFE_ALLOCA (buffer, char *, len);
8362 memcpy (buffer, SDATA (msg), len);
8363
8364 message_dolog (buffer, len - 1, 1, 0);
8365 SAFE_FREE ();
8366
8367 UNGCPRO;
8368 }
8369
8370
8371 /* Output a newline in the *Messages* buffer if "needs" one. */
8372
8373 void
8374 message_log_maybe_newline (void)
8375 {
8376 if (message_log_need_newline)
8377 message_dolog ("", 0, 1, 0);
8378 }
8379
8380
8381 /* Add a string M of length NBYTES to the message log, optionally
8382 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8383 nonzero, means interpret the contents of M as multibyte. This
8384 function calls low-level routines in order to bypass text property
8385 hooks, etc. which might not be safe to run.
8386
8387 This may GC (insert may run before/after change hooks),
8388 so the buffer M must NOT point to a Lisp string. */
8389
8390 void
8391 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8392 {
8393 const unsigned char *msg = (const unsigned char *) m;
8394
8395 if (!NILP (Vmemory_full))
8396 return;
8397
8398 if (!NILP (Vmessage_log_max))
8399 {
8400 struct buffer *oldbuf;
8401 Lisp_Object oldpoint, oldbegv, oldzv;
8402 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8403 EMACS_INT point_at_end = 0;
8404 EMACS_INT zv_at_end = 0;
8405 Lisp_Object old_deactivate_mark, tem;
8406 struct gcpro gcpro1;
8407
8408 old_deactivate_mark = Vdeactivate_mark;
8409 oldbuf = current_buffer;
8410 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8411 BVAR (current_buffer, undo_list) = Qt;
8412
8413 oldpoint = message_dolog_marker1;
8414 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8415 oldbegv = message_dolog_marker2;
8416 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8417 oldzv = message_dolog_marker3;
8418 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8419 GCPRO1 (old_deactivate_mark);
8420
8421 if (PT == Z)
8422 point_at_end = 1;
8423 if (ZV == Z)
8424 zv_at_end = 1;
8425
8426 BEGV = BEG;
8427 BEGV_BYTE = BEG_BYTE;
8428 ZV = Z;
8429 ZV_BYTE = Z_BYTE;
8430 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8431
8432 /* Insert the string--maybe converting multibyte to single byte
8433 or vice versa, so that all the text fits the buffer. */
8434 if (multibyte
8435 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8436 {
8437 EMACS_INT i;
8438 int c, char_bytes;
8439 char work[1];
8440
8441 /* Convert a multibyte string to single-byte
8442 for the *Message* buffer. */
8443 for (i = 0; i < nbytes; i += char_bytes)
8444 {
8445 c = string_char_and_length (msg + i, &char_bytes);
8446 work[0] = (ASCII_CHAR_P (c)
8447 ? c
8448 : multibyte_char_to_unibyte (c));
8449 insert_1_both (work, 1, 1, 1, 0, 0);
8450 }
8451 }
8452 else if (! multibyte
8453 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8454 {
8455 EMACS_INT i;
8456 int c, char_bytes;
8457 unsigned char str[MAX_MULTIBYTE_LENGTH];
8458 /* Convert a single-byte string to multibyte
8459 for the *Message* buffer. */
8460 for (i = 0; i < nbytes; i++)
8461 {
8462 c = msg[i];
8463 MAKE_CHAR_MULTIBYTE (c);
8464 char_bytes = CHAR_STRING (c, str);
8465 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8466 }
8467 }
8468 else if (nbytes)
8469 insert_1 (m, nbytes, 1, 0, 0);
8470
8471 if (nlflag)
8472 {
8473 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8474 unsigned long int dups;
8475 insert_1 ("\n", 1, 1, 0, 0);
8476
8477 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8478 this_bol = PT;
8479 this_bol_byte = PT_BYTE;
8480
8481 /* See if this line duplicates the previous one.
8482 If so, combine duplicates. */
8483 if (this_bol > BEG)
8484 {
8485 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8486 prev_bol = PT;
8487 prev_bol_byte = PT_BYTE;
8488
8489 dups = message_log_check_duplicate (prev_bol_byte,
8490 this_bol_byte);
8491 if (dups)
8492 {
8493 del_range_both (prev_bol, prev_bol_byte,
8494 this_bol, this_bol_byte, 0);
8495 if (dups > 1)
8496 {
8497 char dupstr[40];
8498 int duplen;
8499
8500 /* If you change this format, don't forget to also
8501 change message_log_check_duplicate. */
8502 sprintf (dupstr, " [%lu times]", dups);
8503 duplen = strlen (dupstr);
8504 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8505 insert_1 (dupstr, duplen, 1, 0, 1);
8506 }
8507 }
8508 }
8509
8510 /* If we have more than the desired maximum number of lines
8511 in the *Messages* buffer now, delete the oldest ones.
8512 This is safe because we don't have undo in this buffer. */
8513
8514 if (NATNUMP (Vmessage_log_max))
8515 {
8516 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8517 -XFASTINT (Vmessage_log_max) - 1, 0);
8518 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8519 }
8520 }
8521 BEGV = XMARKER (oldbegv)->charpos;
8522 BEGV_BYTE = marker_byte_position (oldbegv);
8523
8524 if (zv_at_end)
8525 {
8526 ZV = Z;
8527 ZV_BYTE = Z_BYTE;
8528 }
8529 else
8530 {
8531 ZV = XMARKER (oldzv)->charpos;
8532 ZV_BYTE = marker_byte_position (oldzv);
8533 }
8534
8535 if (point_at_end)
8536 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8537 else
8538 /* We can't do Fgoto_char (oldpoint) because it will run some
8539 Lisp code. */
8540 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8541 XMARKER (oldpoint)->bytepos);
8542
8543 UNGCPRO;
8544 unchain_marker (XMARKER (oldpoint));
8545 unchain_marker (XMARKER (oldbegv));
8546 unchain_marker (XMARKER (oldzv));
8547
8548 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8549 set_buffer_internal (oldbuf);
8550 if (NILP (tem))
8551 windows_or_buffers_changed = old_windows_or_buffers_changed;
8552 message_log_need_newline = !nlflag;
8553 Vdeactivate_mark = old_deactivate_mark;
8554 }
8555 }
8556
8557
8558 /* We are at the end of the buffer after just having inserted a newline.
8559 (Note: We depend on the fact we won't be crossing the gap.)
8560 Check to see if the most recent message looks a lot like the previous one.
8561 Return 0 if different, 1 if the new one should just replace it, or a
8562 value N > 1 if we should also append " [N times]". */
8563
8564 static unsigned long int
8565 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8566 {
8567 EMACS_INT i;
8568 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8569 int seen_dots = 0;
8570 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8571 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8572
8573 for (i = 0; i < len; i++)
8574 {
8575 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8576 seen_dots = 1;
8577 if (p1[i] != p2[i])
8578 return seen_dots;
8579 }
8580 p1 += len;
8581 if (*p1 == '\n')
8582 return 2;
8583 if (*p1++ == ' ' && *p1++ == '[')
8584 {
8585 char *pend;
8586 unsigned long int n = strtoul ((char *) p1, &pend, 10);
8587 if (strncmp (pend, " times]\n", 8) == 0)
8588 return n+1;
8589 }
8590 return 0;
8591 }
8592 \f
8593
8594 /* Display an echo area message M with a specified length of NBYTES
8595 bytes. The string may include null characters. If M is 0, clear
8596 out any existing message, and let the mini-buffer text show
8597 through.
8598
8599 This may GC, so the buffer M must NOT point to a Lisp string. */
8600
8601 void
8602 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8603 {
8604 /* First flush out any partial line written with print. */
8605 message_log_maybe_newline ();
8606 if (m)
8607 message_dolog (m, nbytes, 1, multibyte);
8608 message2_nolog (m, nbytes, multibyte);
8609 }
8610
8611
8612 /* The non-logging counterpart of message2. */
8613
8614 void
8615 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8616 {
8617 struct frame *sf = SELECTED_FRAME ();
8618 message_enable_multibyte = multibyte;
8619
8620 if (FRAME_INITIAL_P (sf))
8621 {
8622 if (noninteractive_need_newline)
8623 putc ('\n', stderr);
8624 noninteractive_need_newline = 0;
8625 if (m)
8626 fwrite (m, nbytes, 1, stderr);
8627 if (cursor_in_echo_area == 0)
8628 fprintf (stderr, "\n");
8629 fflush (stderr);
8630 }
8631 /* A null message buffer means that the frame hasn't really been
8632 initialized yet. Error messages get reported properly by
8633 cmd_error, so this must be just an informative message; toss it. */
8634 else if (INTERACTIVE
8635 && sf->glyphs_initialized_p
8636 && FRAME_MESSAGE_BUF (sf))
8637 {
8638 Lisp_Object mini_window;
8639 struct frame *f;
8640
8641 /* Get the frame containing the mini-buffer
8642 that the selected frame is using. */
8643 mini_window = FRAME_MINIBUF_WINDOW (sf);
8644 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8645
8646 FRAME_SAMPLE_VISIBILITY (f);
8647 if (FRAME_VISIBLE_P (sf)
8648 && ! FRAME_VISIBLE_P (f))
8649 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8650
8651 if (m)
8652 {
8653 set_message (m, Qnil, nbytes, multibyte);
8654 if (minibuffer_auto_raise)
8655 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8656 }
8657 else
8658 clear_message (1, 1);
8659
8660 do_pending_window_change (0);
8661 echo_area_display (1);
8662 do_pending_window_change (0);
8663 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8664 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8665 }
8666 }
8667
8668
8669 /* Display an echo area message M with a specified length of NBYTES
8670 bytes. The string may include null characters. If M is not a
8671 string, clear out any existing message, and let the mini-buffer
8672 text show through.
8673
8674 This function cancels echoing. */
8675
8676 void
8677 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8678 {
8679 struct gcpro gcpro1;
8680
8681 GCPRO1 (m);
8682 clear_message (1,1);
8683 cancel_echoing ();
8684
8685 /* First flush out any partial line written with print. */
8686 message_log_maybe_newline ();
8687 if (STRINGP (m))
8688 {
8689 char *buffer;
8690 USE_SAFE_ALLOCA;
8691
8692 SAFE_ALLOCA (buffer, char *, nbytes);
8693 memcpy (buffer, SDATA (m), nbytes);
8694 message_dolog (buffer, nbytes, 1, multibyte);
8695 SAFE_FREE ();
8696 }
8697 message3_nolog (m, nbytes, multibyte);
8698
8699 UNGCPRO;
8700 }
8701
8702
8703 /* The non-logging version of message3.
8704 This does not cancel echoing, because it is used for echoing.
8705 Perhaps we need to make a separate function for echoing
8706 and make this cancel echoing. */
8707
8708 void
8709 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8710 {
8711 struct frame *sf = SELECTED_FRAME ();
8712 message_enable_multibyte = multibyte;
8713
8714 if (FRAME_INITIAL_P (sf))
8715 {
8716 if (noninteractive_need_newline)
8717 putc ('\n', stderr);
8718 noninteractive_need_newline = 0;
8719 if (STRINGP (m))
8720 fwrite (SDATA (m), nbytes, 1, stderr);
8721 if (cursor_in_echo_area == 0)
8722 fprintf (stderr, "\n");
8723 fflush (stderr);
8724 }
8725 /* A null message buffer means that the frame hasn't really been
8726 initialized yet. Error messages get reported properly by
8727 cmd_error, so this must be just an informative message; toss it. */
8728 else if (INTERACTIVE
8729 && sf->glyphs_initialized_p
8730 && FRAME_MESSAGE_BUF (sf))
8731 {
8732 Lisp_Object mini_window;
8733 Lisp_Object frame;
8734 struct frame *f;
8735
8736 /* Get the frame containing the mini-buffer
8737 that the selected frame is using. */
8738 mini_window = FRAME_MINIBUF_WINDOW (sf);
8739 frame = XWINDOW (mini_window)->frame;
8740 f = XFRAME (frame);
8741
8742 FRAME_SAMPLE_VISIBILITY (f);
8743 if (FRAME_VISIBLE_P (sf)
8744 && !FRAME_VISIBLE_P (f))
8745 Fmake_frame_visible (frame);
8746
8747 if (STRINGP (m) && SCHARS (m) > 0)
8748 {
8749 set_message (NULL, m, nbytes, multibyte);
8750 if (minibuffer_auto_raise)
8751 Fraise_frame (frame);
8752 /* Assume we are not echoing.
8753 (If we are, echo_now will override this.) */
8754 echo_message_buffer = Qnil;
8755 }
8756 else
8757 clear_message (1, 1);
8758
8759 do_pending_window_change (0);
8760 echo_area_display (1);
8761 do_pending_window_change (0);
8762 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8763 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8764 }
8765 }
8766
8767
8768 /* Display a null-terminated echo area message M. If M is 0, clear
8769 out any existing message, and let the mini-buffer text show through.
8770
8771 The buffer M must continue to exist until after the echo area gets
8772 cleared or some other message gets displayed there. Do not pass
8773 text that is stored in a Lisp string. Do not pass text in a buffer
8774 that was alloca'd. */
8775
8776 void
8777 message1 (const char *m)
8778 {
8779 message2 (m, (m ? strlen (m) : 0), 0);
8780 }
8781
8782
8783 /* The non-logging counterpart of message1. */
8784
8785 void
8786 message1_nolog (const char *m)
8787 {
8788 message2_nolog (m, (m ? strlen (m) : 0), 0);
8789 }
8790
8791 /* Display a message M which contains a single %s
8792 which gets replaced with STRING. */
8793
8794 void
8795 message_with_string (const char *m, Lisp_Object string, int log)
8796 {
8797 CHECK_STRING (string);
8798
8799 if (noninteractive)
8800 {
8801 if (m)
8802 {
8803 if (noninteractive_need_newline)
8804 putc ('\n', stderr);
8805 noninteractive_need_newline = 0;
8806 fprintf (stderr, m, SDATA (string));
8807 if (!cursor_in_echo_area)
8808 fprintf (stderr, "\n");
8809 fflush (stderr);
8810 }
8811 }
8812 else if (INTERACTIVE)
8813 {
8814 /* The frame whose minibuffer we're going to display the message on.
8815 It may be larger than the selected frame, so we need
8816 to use its buffer, not the selected frame's buffer. */
8817 Lisp_Object mini_window;
8818 struct frame *f, *sf = SELECTED_FRAME ();
8819
8820 /* Get the frame containing the minibuffer
8821 that the selected frame is using. */
8822 mini_window = FRAME_MINIBUF_WINDOW (sf);
8823 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8824
8825 /* A null message buffer means that the frame hasn't really been
8826 initialized yet. Error messages get reported properly by
8827 cmd_error, so this must be just an informative message; toss it. */
8828 if (FRAME_MESSAGE_BUF (f))
8829 {
8830 Lisp_Object args[2], msg;
8831 struct gcpro gcpro1, gcpro2;
8832
8833 args[0] = build_string (m);
8834 args[1] = msg = string;
8835 GCPRO2 (args[0], msg);
8836 gcpro1.nvars = 2;
8837
8838 msg = Fformat (2, args);
8839
8840 if (log)
8841 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8842 else
8843 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8844
8845 UNGCPRO;
8846
8847 /* Print should start at the beginning of the message
8848 buffer next time. */
8849 message_buf_print = 0;
8850 }
8851 }
8852 }
8853
8854
8855 /* Dump an informative message to the minibuf. If M is 0, clear out
8856 any existing message, and let the mini-buffer text show through. */
8857
8858 static void
8859 vmessage (const char *m, va_list ap)
8860 {
8861 if (noninteractive)
8862 {
8863 if (m)
8864 {
8865 if (noninteractive_need_newline)
8866 putc ('\n', stderr);
8867 noninteractive_need_newline = 0;
8868 vfprintf (stderr, m, ap);
8869 if (cursor_in_echo_area == 0)
8870 fprintf (stderr, "\n");
8871 fflush (stderr);
8872 }
8873 }
8874 else if (INTERACTIVE)
8875 {
8876 /* The frame whose mini-buffer we're going to display the message
8877 on. It may be larger than the selected frame, so we need to
8878 use its buffer, not the selected frame's buffer. */
8879 Lisp_Object mini_window;
8880 struct frame *f, *sf = SELECTED_FRAME ();
8881
8882 /* Get the frame containing the mini-buffer
8883 that the selected frame is using. */
8884 mini_window = FRAME_MINIBUF_WINDOW (sf);
8885 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8886
8887 /* A null message buffer means that the frame hasn't really been
8888 initialized yet. Error messages get reported properly by
8889 cmd_error, so this must be just an informative message; toss
8890 it. */
8891 if (FRAME_MESSAGE_BUF (f))
8892 {
8893 if (m)
8894 {
8895 size_t len;
8896
8897 len = doprnt (FRAME_MESSAGE_BUF (f),
8898 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8899
8900 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8901 }
8902 else
8903 message1 (0);
8904
8905 /* Print should start at the beginning of the message
8906 buffer next time. */
8907 message_buf_print = 0;
8908 }
8909 }
8910 }
8911
8912 void
8913 message (const char *m, ...)
8914 {
8915 va_list ap;
8916 va_start (ap, m);
8917 vmessage (m, ap);
8918 va_end (ap);
8919 }
8920
8921
8922 #if 0
8923 /* The non-logging version of message. */
8924
8925 void
8926 message_nolog (const char *m, ...)
8927 {
8928 Lisp_Object old_log_max;
8929 va_list ap;
8930 va_start (ap, m);
8931 old_log_max = Vmessage_log_max;
8932 Vmessage_log_max = Qnil;
8933 vmessage (m, ap);
8934 Vmessage_log_max = old_log_max;
8935 va_end (ap);
8936 }
8937 #endif
8938
8939
8940 /* Display the current message in the current mini-buffer. This is
8941 only called from error handlers in process.c, and is not time
8942 critical. */
8943
8944 void
8945 update_echo_area (void)
8946 {
8947 if (!NILP (echo_area_buffer[0]))
8948 {
8949 Lisp_Object string;
8950 string = Fcurrent_message ();
8951 message3 (string, SBYTES (string),
8952 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
8953 }
8954 }
8955
8956
8957 /* Make sure echo area buffers in `echo_buffers' are live.
8958 If they aren't, make new ones. */
8959
8960 static void
8961 ensure_echo_area_buffers (void)
8962 {
8963 int i;
8964
8965 for (i = 0; i < 2; ++i)
8966 if (!BUFFERP (echo_buffer[i])
8967 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
8968 {
8969 char name[30];
8970 Lisp_Object old_buffer;
8971 int j;
8972
8973 old_buffer = echo_buffer[i];
8974 sprintf (name, " *Echo Area %d*", i);
8975 echo_buffer[i] = Fget_buffer_create (build_string (name));
8976 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
8977 /* to force word wrap in echo area -
8978 it was decided to postpone this*/
8979 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8980
8981 for (j = 0; j < 2; ++j)
8982 if (EQ (old_buffer, echo_area_buffer[j]))
8983 echo_area_buffer[j] = echo_buffer[i];
8984 }
8985 }
8986
8987
8988 /* Call FN with args A1..A4 with either the current or last displayed
8989 echo_area_buffer as current buffer.
8990
8991 WHICH zero means use the current message buffer
8992 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8993 from echo_buffer[] and clear it.
8994
8995 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8996 suitable buffer from echo_buffer[] and clear it.
8997
8998 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8999 that the current message becomes the last displayed one, make
9000 choose a suitable buffer for echo_area_buffer[0], and clear it.
9001
9002 Value is what FN returns. */
9003
9004 static int
9005 with_echo_area_buffer (struct window *w, int which,
9006 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9007 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9008 {
9009 Lisp_Object buffer;
9010 int this_one, the_other, clear_buffer_p, rc;
9011 int count = SPECPDL_INDEX ();
9012
9013 /* If buffers aren't live, make new ones. */
9014 ensure_echo_area_buffers ();
9015
9016 clear_buffer_p = 0;
9017
9018 if (which == 0)
9019 this_one = 0, the_other = 1;
9020 else if (which > 0)
9021 this_one = 1, the_other = 0;
9022 else
9023 {
9024 this_one = 0, the_other = 1;
9025 clear_buffer_p = 1;
9026
9027 /* We need a fresh one in case the current echo buffer equals
9028 the one containing the last displayed echo area message. */
9029 if (!NILP (echo_area_buffer[this_one])
9030 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9031 echo_area_buffer[this_one] = Qnil;
9032 }
9033
9034 /* Choose a suitable buffer from echo_buffer[] is we don't
9035 have one. */
9036 if (NILP (echo_area_buffer[this_one]))
9037 {
9038 echo_area_buffer[this_one]
9039 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9040 ? echo_buffer[the_other]
9041 : echo_buffer[this_one]);
9042 clear_buffer_p = 1;
9043 }
9044
9045 buffer = echo_area_buffer[this_one];
9046
9047 /* Don't get confused by reusing the buffer used for echoing
9048 for a different purpose. */
9049 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9050 cancel_echoing ();
9051
9052 record_unwind_protect (unwind_with_echo_area_buffer,
9053 with_echo_area_buffer_unwind_data (w));
9054
9055 /* Make the echo area buffer current. Note that for display
9056 purposes, it is not necessary that the displayed window's buffer
9057 == current_buffer, except for text property lookup. So, let's
9058 only set that buffer temporarily here without doing a full
9059 Fset_window_buffer. We must also change w->pointm, though,
9060 because otherwise an assertions in unshow_buffer fails, and Emacs
9061 aborts. */
9062 set_buffer_internal_1 (XBUFFER (buffer));
9063 if (w)
9064 {
9065 w->buffer = buffer;
9066 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9067 }
9068
9069 BVAR (current_buffer, undo_list) = Qt;
9070 BVAR (current_buffer, read_only) = Qnil;
9071 specbind (Qinhibit_read_only, Qt);
9072 specbind (Qinhibit_modification_hooks, Qt);
9073
9074 if (clear_buffer_p && Z > BEG)
9075 del_range (BEG, Z);
9076
9077 xassert (BEGV >= BEG);
9078 xassert (ZV <= Z && ZV >= BEGV);
9079
9080 rc = fn (a1, a2, a3, a4);
9081
9082 xassert (BEGV >= BEG);
9083 xassert (ZV <= Z && ZV >= BEGV);
9084
9085 unbind_to (count, Qnil);
9086 return rc;
9087 }
9088
9089
9090 /* Save state that should be preserved around the call to the function
9091 FN called in with_echo_area_buffer. */
9092
9093 static Lisp_Object
9094 with_echo_area_buffer_unwind_data (struct window *w)
9095 {
9096 int i = 0;
9097 Lisp_Object vector, tmp;
9098
9099 /* Reduce consing by keeping one vector in
9100 Vwith_echo_area_save_vector. */
9101 vector = Vwith_echo_area_save_vector;
9102 Vwith_echo_area_save_vector = Qnil;
9103
9104 if (NILP (vector))
9105 vector = Fmake_vector (make_number (7), Qnil);
9106
9107 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9108 ASET (vector, i, Vdeactivate_mark); ++i;
9109 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9110
9111 if (w)
9112 {
9113 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9114 ASET (vector, i, w->buffer); ++i;
9115 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9116 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9117 }
9118 else
9119 {
9120 int end = i + 4;
9121 for (; i < end; ++i)
9122 ASET (vector, i, Qnil);
9123 }
9124
9125 xassert (i == ASIZE (vector));
9126 return vector;
9127 }
9128
9129
9130 /* Restore global state from VECTOR which was created by
9131 with_echo_area_buffer_unwind_data. */
9132
9133 static Lisp_Object
9134 unwind_with_echo_area_buffer (Lisp_Object vector)
9135 {
9136 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9137 Vdeactivate_mark = AREF (vector, 1);
9138 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9139
9140 if (WINDOWP (AREF (vector, 3)))
9141 {
9142 struct window *w;
9143 Lisp_Object buffer, charpos, bytepos;
9144
9145 w = XWINDOW (AREF (vector, 3));
9146 buffer = AREF (vector, 4);
9147 charpos = AREF (vector, 5);
9148 bytepos = AREF (vector, 6);
9149
9150 w->buffer = buffer;
9151 set_marker_both (w->pointm, buffer,
9152 XFASTINT (charpos), XFASTINT (bytepos));
9153 }
9154
9155 Vwith_echo_area_save_vector = vector;
9156 return Qnil;
9157 }
9158
9159
9160 /* Set up the echo area for use by print functions. MULTIBYTE_P
9161 non-zero means we will print multibyte. */
9162
9163 void
9164 setup_echo_area_for_printing (int multibyte_p)
9165 {
9166 /* If we can't find an echo area any more, exit. */
9167 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9168 Fkill_emacs (Qnil);
9169
9170 ensure_echo_area_buffers ();
9171
9172 if (!message_buf_print)
9173 {
9174 /* A message has been output since the last time we printed.
9175 Choose a fresh echo area buffer. */
9176 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9177 echo_area_buffer[0] = echo_buffer[1];
9178 else
9179 echo_area_buffer[0] = echo_buffer[0];
9180
9181 /* Switch to that buffer and clear it. */
9182 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9183 BVAR (current_buffer, truncate_lines) = Qnil;
9184
9185 if (Z > BEG)
9186 {
9187 int count = SPECPDL_INDEX ();
9188 specbind (Qinhibit_read_only, Qt);
9189 /* Note that undo recording is always disabled. */
9190 del_range (BEG, Z);
9191 unbind_to (count, Qnil);
9192 }
9193 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9194
9195 /* Set up the buffer for the multibyteness we need. */
9196 if (multibyte_p
9197 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9198 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9199
9200 /* Raise the frame containing the echo area. */
9201 if (minibuffer_auto_raise)
9202 {
9203 struct frame *sf = SELECTED_FRAME ();
9204 Lisp_Object mini_window;
9205 mini_window = FRAME_MINIBUF_WINDOW (sf);
9206 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9207 }
9208
9209 message_log_maybe_newline ();
9210 message_buf_print = 1;
9211 }
9212 else
9213 {
9214 if (NILP (echo_area_buffer[0]))
9215 {
9216 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9217 echo_area_buffer[0] = echo_buffer[1];
9218 else
9219 echo_area_buffer[0] = echo_buffer[0];
9220 }
9221
9222 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9223 {
9224 /* Someone switched buffers between print requests. */
9225 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9226 BVAR (current_buffer, truncate_lines) = Qnil;
9227 }
9228 }
9229 }
9230
9231
9232 /* Display an echo area message in window W. Value is non-zero if W's
9233 height is changed. If display_last_displayed_message_p is
9234 non-zero, display the message that was last displayed, otherwise
9235 display the current message. */
9236
9237 static int
9238 display_echo_area (struct window *w)
9239 {
9240 int i, no_message_p, window_height_changed_p, count;
9241
9242 /* Temporarily disable garbage collections while displaying the echo
9243 area. This is done because a GC can print a message itself.
9244 That message would modify the echo area buffer's contents while a
9245 redisplay of the buffer is going on, and seriously confuse
9246 redisplay. */
9247 count = inhibit_garbage_collection ();
9248
9249 /* If there is no message, we must call display_echo_area_1
9250 nevertheless because it resizes the window. But we will have to
9251 reset the echo_area_buffer in question to nil at the end because
9252 with_echo_area_buffer will sets it to an empty buffer. */
9253 i = display_last_displayed_message_p ? 1 : 0;
9254 no_message_p = NILP (echo_area_buffer[i]);
9255
9256 window_height_changed_p
9257 = with_echo_area_buffer (w, display_last_displayed_message_p,
9258 display_echo_area_1,
9259 (intptr_t) w, Qnil, 0, 0);
9260
9261 if (no_message_p)
9262 echo_area_buffer[i] = Qnil;
9263
9264 unbind_to (count, Qnil);
9265 return window_height_changed_p;
9266 }
9267
9268
9269 /* Helper for display_echo_area. Display the current buffer which
9270 contains the current echo area message in window W, a mini-window,
9271 a pointer to which is passed in A1. A2..A4 are currently not used.
9272 Change the height of W so that all of the message is displayed.
9273 Value is non-zero if height of W was changed. */
9274
9275 static int
9276 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9277 {
9278 intptr_t i1 = a1;
9279 struct window *w = (struct window *) i1;
9280 Lisp_Object window;
9281 struct text_pos start;
9282 int window_height_changed_p = 0;
9283
9284 /* Do this before displaying, so that we have a large enough glyph
9285 matrix for the display. If we can't get enough space for the
9286 whole text, display the last N lines. That works by setting w->start. */
9287 window_height_changed_p = resize_mini_window (w, 0);
9288
9289 /* Use the starting position chosen by resize_mini_window. */
9290 SET_TEXT_POS_FROM_MARKER (start, w->start);
9291
9292 /* Display. */
9293 clear_glyph_matrix (w->desired_matrix);
9294 XSETWINDOW (window, w);
9295 try_window (window, start, 0);
9296
9297 return window_height_changed_p;
9298 }
9299
9300
9301 /* Resize the echo area window to exactly the size needed for the
9302 currently displayed message, if there is one. If a mini-buffer
9303 is active, don't shrink it. */
9304
9305 void
9306 resize_echo_area_exactly (void)
9307 {
9308 if (BUFFERP (echo_area_buffer[0])
9309 && WINDOWP (echo_area_window))
9310 {
9311 struct window *w = XWINDOW (echo_area_window);
9312 int resized_p;
9313 Lisp_Object resize_exactly;
9314
9315 if (minibuf_level == 0)
9316 resize_exactly = Qt;
9317 else
9318 resize_exactly = Qnil;
9319
9320 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9321 (intptr_t) w, resize_exactly,
9322 0, 0);
9323 if (resized_p)
9324 {
9325 ++windows_or_buffers_changed;
9326 ++update_mode_lines;
9327 redisplay_internal ();
9328 }
9329 }
9330 }
9331
9332
9333 /* Callback function for with_echo_area_buffer, when used from
9334 resize_echo_area_exactly. A1 contains a pointer to the window to
9335 resize, EXACTLY non-nil means resize the mini-window exactly to the
9336 size of the text displayed. A3 and A4 are not used. Value is what
9337 resize_mini_window returns. */
9338
9339 static int
9340 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9341 {
9342 intptr_t i1 = a1;
9343 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9344 }
9345
9346
9347 /* Resize mini-window W to fit the size of its contents. EXACT_P
9348 means size the window exactly to the size needed. Otherwise, it's
9349 only enlarged until W's buffer is empty.
9350
9351 Set W->start to the right place to begin display. If the whole
9352 contents fit, start at the beginning. Otherwise, start so as
9353 to make the end of the contents appear. This is particularly
9354 important for y-or-n-p, but seems desirable generally.
9355
9356 Value is non-zero if the window height has been changed. */
9357
9358 int
9359 resize_mini_window (struct window *w, int exact_p)
9360 {
9361 struct frame *f = XFRAME (w->frame);
9362 int window_height_changed_p = 0;
9363
9364 xassert (MINI_WINDOW_P (w));
9365
9366 /* By default, start display at the beginning. */
9367 set_marker_both (w->start, w->buffer,
9368 BUF_BEGV (XBUFFER (w->buffer)),
9369 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9370
9371 /* Don't resize windows while redisplaying a window; it would
9372 confuse redisplay functions when the size of the window they are
9373 displaying changes from under them. Such a resizing can happen,
9374 for instance, when which-func prints a long message while
9375 we are running fontification-functions. We're running these
9376 functions with safe_call which binds inhibit-redisplay to t. */
9377 if (!NILP (Vinhibit_redisplay))
9378 return 0;
9379
9380 /* Nil means don't try to resize. */
9381 if (NILP (Vresize_mini_windows)
9382 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9383 return 0;
9384
9385 if (!FRAME_MINIBUF_ONLY_P (f))
9386 {
9387 struct it it;
9388 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9389 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9390 int height, max_height;
9391 int unit = FRAME_LINE_HEIGHT (f);
9392 struct text_pos start;
9393 struct buffer *old_current_buffer = NULL;
9394
9395 if (current_buffer != XBUFFER (w->buffer))
9396 {
9397 old_current_buffer = current_buffer;
9398 set_buffer_internal (XBUFFER (w->buffer));
9399 }
9400
9401 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9402
9403 /* Compute the max. number of lines specified by the user. */
9404 if (FLOATP (Vmax_mini_window_height))
9405 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9406 else if (INTEGERP (Vmax_mini_window_height))
9407 max_height = XINT (Vmax_mini_window_height);
9408 else
9409 max_height = total_height / 4;
9410
9411 /* Correct that max. height if it's bogus. */
9412 max_height = max (1, max_height);
9413 max_height = min (total_height, max_height);
9414
9415 /* Find out the height of the text in the window. */
9416 if (it.line_wrap == TRUNCATE)
9417 height = 1;
9418 else
9419 {
9420 last_height = 0;
9421 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9422 if (it.max_ascent == 0 && it.max_descent == 0)
9423 height = it.current_y + last_height;
9424 else
9425 height = it.current_y + it.max_ascent + it.max_descent;
9426 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9427 height = (height + unit - 1) / unit;
9428 }
9429
9430 /* Compute a suitable window start. */
9431 if (height > max_height)
9432 {
9433 height = max_height;
9434 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9435 move_it_vertically_backward (&it, (height - 1) * unit);
9436 start = it.current.pos;
9437 }
9438 else
9439 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9440 SET_MARKER_FROM_TEXT_POS (w->start, start);
9441
9442 if (EQ (Vresize_mini_windows, Qgrow_only))
9443 {
9444 /* Let it grow only, until we display an empty message, in which
9445 case the window shrinks again. */
9446 if (height > WINDOW_TOTAL_LINES (w))
9447 {
9448 int old_height = WINDOW_TOTAL_LINES (w);
9449 freeze_window_starts (f, 1);
9450 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9451 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9452 }
9453 else if (height < WINDOW_TOTAL_LINES (w)
9454 && (exact_p || BEGV == ZV))
9455 {
9456 int old_height = WINDOW_TOTAL_LINES (w);
9457 freeze_window_starts (f, 0);
9458 shrink_mini_window (w);
9459 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9460 }
9461 }
9462 else
9463 {
9464 /* Always resize to exact size needed. */
9465 if (height > WINDOW_TOTAL_LINES (w))
9466 {
9467 int old_height = WINDOW_TOTAL_LINES (w);
9468 freeze_window_starts (f, 1);
9469 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9470 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9471 }
9472 else if (height < WINDOW_TOTAL_LINES (w))
9473 {
9474 int old_height = WINDOW_TOTAL_LINES (w);
9475 freeze_window_starts (f, 0);
9476 shrink_mini_window (w);
9477
9478 if (height)
9479 {
9480 freeze_window_starts (f, 1);
9481 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9482 }
9483
9484 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9485 }
9486 }
9487
9488 if (old_current_buffer)
9489 set_buffer_internal (old_current_buffer);
9490 }
9491
9492 return window_height_changed_p;
9493 }
9494
9495
9496 /* Value is the current message, a string, or nil if there is no
9497 current message. */
9498
9499 Lisp_Object
9500 current_message (void)
9501 {
9502 Lisp_Object msg;
9503
9504 if (!BUFFERP (echo_area_buffer[0]))
9505 msg = Qnil;
9506 else
9507 {
9508 with_echo_area_buffer (0, 0, current_message_1,
9509 (intptr_t) &msg, Qnil, 0, 0);
9510 if (NILP (msg))
9511 echo_area_buffer[0] = Qnil;
9512 }
9513
9514 return msg;
9515 }
9516
9517
9518 static int
9519 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9520 {
9521 intptr_t i1 = a1;
9522 Lisp_Object *msg = (Lisp_Object *) i1;
9523
9524 if (Z > BEG)
9525 *msg = make_buffer_string (BEG, Z, 1);
9526 else
9527 *msg = Qnil;
9528 return 0;
9529 }
9530
9531
9532 /* Push the current message on Vmessage_stack for later restauration
9533 by restore_message. Value is non-zero if the current message isn't
9534 empty. This is a relatively infrequent operation, so it's not
9535 worth optimizing. */
9536
9537 int
9538 push_message (void)
9539 {
9540 Lisp_Object msg;
9541 msg = current_message ();
9542 Vmessage_stack = Fcons (msg, Vmessage_stack);
9543 return STRINGP (msg);
9544 }
9545
9546
9547 /* Restore message display from the top of Vmessage_stack. */
9548
9549 void
9550 restore_message (void)
9551 {
9552 Lisp_Object msg;
9553
9554 xassert (CONSP (Vmessage_stack));
9555 msg = XCAR (Vmessage_stack);
9556 if (STRINGP (msg))
9557 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9558 else
9559 message3_nolog (msg, 0, 0);
9560 }
9561
9562
9563 /* Handler for record_unwind_protect calling pop_message. */
9564
9565 Lisp_Object
9566 pop_message_unwind (Lisp_Object dummy)
9567 {
9568 pop_message ();
9569 return Qnil;
9570 }
9571
9572 /* Pop the top-most entry off Vmessage_stack. */
9573
9574 static void
9575 pop_message (void)
9576 {
9577 xassert (CONSP (Vmessage_stack));
9578 Vmessage_stack = XCDR (Vmessage_stack);
9579 }
9580
9581
9582 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9583 exits. If the stack is not empty, we have a missing pop_message
9584 somewhere. */
9585
9586 void
9587 check_message_stack (void)
9588 {
9589 if (!NILP (Vmessage_stack))
9590 abort ();
9591 }
9592
9593
9594 /* Truncate to NCHARS what will be displayed in the echo area the next
9595 time we display it---but don't redisplay it now. */
9596
9597 void
9598 truncate_echo_area (EMACS_INT nchars)
9599 {
9600 if (nchars == 0)
9601 echo_area_buffer[0] = Qnil;
9602 /* A null message buffer means that the frame hasn't really been
9603 initialized yet. Error messages get reported properly by
9604 cmd_error, so this must be just an informative message; toss it. */
9605 else if (!noninteractive
9606 && INTERACTIVE
9607 && !NILP (echo_area_buffer[0]))
9608 {
9609 struct frame *sf = SELECTED_FRAME ();
9610 if (FRAME_MESSAGE_BUF (sf))
9611 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9612 }
9613 }
9614
9615
9616 /* Helper function for truncate_echo_area. Truncate the current
9617 message to at most NCHARS characters. */
9618
9619 static int
9620 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9621 {
9622 if (BEG + nchars < Z)
9623 del_range (BEG + nchars, Z);
9624 if (Z == BEG)
9625 echo_area_buffer[0] = Qnil;
9626 return 0;
9627 }
9628
9629
9630 /* Set the current message to a substring of S or STRING.
9631
9632 If STRING is a Lisp string, set the message to the first NBYTES
9633 bytes from STRING. NBYTES zero means use the whole string. If
9634 STRING is multibyte, the message will be displayed multibyte.
9635
9636 If S is not null, set the message to the first LEN bytes of S. LEN
9637 zero means use the whole string. MULTIBYTE_P non-zero means S is
9638 multibyte. Display the message multibyte in that case.
9639
9640 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9641 to t before calling set_message_1 (which calls insert).
9642 */
9643
9644 static void
9645 set_message (const char *s, Lisp_Object string,
9646 EMACS_INT nbytes, int multibyte_p)
9647 {
9648 message_enable_multibyte
9649 = ((s && multibyte_p)
9650 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9651
9652 with_echo_area_buffer (0, -1, set_message_1,
9653 (intptr_t) s, string, nbytes, multibyte_p);
9654 message_buf_print = 0;
9655 help_echo_showing_p = 0;
9656 }
9657
9658
9659 /* Helper function for set_message. Arguments have the same meaning
9660 as there, with A1 corresponding to S and A2 corresponding to STRING
9661 This function is called with the echo area buffer being
9662 current. */
9663
9664 static int
9665 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9666 {
9667 intptr_t i1 = a1;
9668 const char *s = (const char *) i1;
9669 const unsigned char *msg = (const unsigned char *) s;
9670 Lisp_Object string = a2;
9671
9672 /* Change multibyteness of the echo buffer appropriately. */
9673 if (message_enable_multibyte
9674 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9675 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9676
9677 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9678 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9679 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9680
9681 /* Insert new message at BEG. */
9682 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9683
9684 if (STRINGP (string))
9685 {
9686 EMACS_INT nchars;
9687
9688 if (nbytes == 0)
9689 nbytes = SBYTES (string);
9690 nchars = string_byte_to_char (string, nbytes);
9691
9692 /* This function takes care of single/multibyte conversion. We
9693 just have to ensure that the echo area buffer has the right
9694 setting of enable_multibyte_characters. */
9695 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9696 }
9697 else if (s)
9698 {
9699 if (nbytes == 0)
9700 nbytes = strlen (s);
9701
9702 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9703 {
9704 /* Convert from multi-byte to single-byte. */
9705 EMACS_INT i;
9706 int c, n;
9707 char work[1];
9708
9709 /* Convert a multibyte string to single-byte. */
9710 for (i = 0; i < nbytes; i += n)
9711 {
9712 c = string_char_and_length (msg + i, &n);
9713 work[0] = (ASCII_CHAR_P (c)
9714 ? c
9715 : multibyte_char_to_unibyte (c));
9716 insert_1_both (work, 1, 1, 1, 0, 0);
9717 }
9718 }
9719 else if (!multibyte_p
9720 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9721 {
9722 /* Convert from single-byte to multi-byte. */
9723 EMACS_INT i;
9724 int c, n;
9725 unsigned char str[MAX_MULTIBYTE_LENGTH];
9726
9727 /* Convert a single-byte string to multibyte. */
9728 for (i = 0; i < nbytes; i++)
9729 {
9730 c = msg[i];
9731 MAKE_CHAR_MULTIBYTE (c);
9732 n = CHAR_STRING (c, str);
9733 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9734 }
9735 }
9736 else
9737 insert_1 (s, nbytes, 1, 0, 0);
9738 }
9739
9740 return 0;
9741 }
9742
9743
9744 /* Clear messages. CURRENT_P non-zero means clear the current
9745 message. LAST_DISPLAYED_P non-zero means clear the message
9746 last displayed. */
9747
9748 void
9749 clear_message (int current_p, int last_displayed_p)
9750 {
9751 if (current_p)
9752 {
9753 echo_area_buffer[0] = Qnil;
9754 message_cleared_p = 1;
9755 }
9756
9757 if (last_displayed_p)
9758 echo_area_buffer[1] = Qnil;
9759
9760 message_buf_print = 0;
9761 }
9762
9763 /* Clear garbaged frames.
9764
9765 This function is used where the old redisplay called
9766 redraw_garbaged_frames which in turn called redraw_frame which in
9767 turn called clear_frame. The call to clear_frame was a source of
9768 flickering. I believe a clear_frame is not necessary. It should
9769 suffice in the new redisplay to invalidate all current matrices,
9770 and ensure a complete redisplay of all windows. */
9771
9772 static void
9773 clear_garbaged_frames (void)
9774 {
9775 if (frame_garbaged)
9776 {
9777 Lisp_Object tail, frame;
9778 int changed_count = 0;
9779
9780 FOR_EACH_FRAME (tail, frame)
9781 {
9782 struct frame *f = XFRAME (frame);
9783
9784 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9785 {
9786 if (f->resized_p)
9787 {
9788 Fredraw_frame (frame);
9789 f->force_flush_display_p = 1;
9790 }
9791 clear_current_matrices (f);
9792 changed_count++;
9793 f->garbaged = 0;
9794 f->resized_p = 0;
9795 }
9796 }
9797
9798 frame_garbaged = 0;
9799 if (changed_count)
9800 ++windows_or_buffers_changed;
9801 }
9802 }
9803
9804
9805 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9806 is non-zero update selected_frame. Value is non-zero if the
9807 mini-windows height has been changed. */
9808
9809 static int
9810 echo_area_display (int update_frame_p)
9811 {
9812 Lisp_Object mini_window;
9813 struct window *w;
9814 struct frame *f;
9815 int window_height_changed_p = 0;
9816 struct frame *sf = SELECTED_FRAME ();
9817
9818 mini_window = FRAME_MINIBUF_WINDOW (sf);
9819 w = XWINDOW (mini_window);
9820 f = XFRAME (WINDOW_FRAME (w));
9821
9822 /* Don't display if frame is invisible or not yet initialized. */
9823 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9824 return 0;
9825
9826 #ifdef HAVE_WINDOW_SYSTEM
9827 /* When Emacs starts, selected_frame may be the initial terminal
9828 frame. If we let this through, a message would be displayed on
9829 the terminal. */
9830 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9831 return 0;
9832 #endif /* HAVE_WINDOW_SYSTEM */
9833
9834 /* Redraw garbaged frames. */
9835 if (frame_garbaged)
9836 clear_garbaged_frames ();
9837
9838 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9839 {
9840 echo_area_window = mini_window;
9841 window_height_changed_p = display_echo_area (w);
9842 w->must_be_updated_p = 1;
9843
9844 /* Update the display, unless called from redisplay_internal.
9845 Also don't update the screen during redisplay itself. The
9846 update will happen at the end of redisplay, and an update
9847 here could cause confusion. */
9848 if (update_frame_p && !redisplaying_p)
9849 {
9850 int n = 0;
9851
9852 /* If the display update has been interrupted by pending
9853 input, update mode lines in the frame. Due to the
9854 pending input, it might have been that redisplay hasn't
9855 been called, so that mode lines above the echo area are
9856 garbaged. This looks odd, so we prevent it here. */
9857 if (!display_completed)
9858 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9859
9860 if (window_height_changed_p
9861 /* Don't do this if Emacs is shutting down. Redisplay
9862 needs to run hooks. */
9863 && !NILP (Vrun_hooks))
9864 {
9865 /* Must update other windows. Likewise as in other
9866 cases, don't let this update be interrupted by
9867 pending input. */
9868 int count = SPECPDL_INDEX ();
9869 specbind (Qredisplay_dont_pause, Qt);
9870 windows_or_buffers_changed = 1;
9871 redisplay_internal ();
9872 unbind_to (count, Qnil);
9873 }
9874 else if (FRAME_WINDOW_P (f) && n == 0)
9875 {
9876 /* Window configuration is the same as before.
9877 Can do with a display update of the echo area,
9878 unless we displayed some mode lines. */
9879 update_single_window (w, 1);
9880 FRAME_RIF (f)->flush_display (f);
9881 }
9882 else
9883 update_frame (f, 1, 1);
9884
9885 /* If cursor is in the echo area, make sure that the next
9886 redisplay displays the minibuffer, so that the cursor will
9887 be replaced with what the minibuffer wants. */
9888 if (cursor_in_echo_area)
9889 ++windows_or_buffers_changed;
9890 }
9891 }
9892 else if (!EQ (mini_window, selected_window))
9893 windows_or_buffers_changed++;
9894
9895 /* Last displayed message is now the current message. */
9896 echo_area_buffer[1] = echo_area_buffer[0];
9897 /* Inform read_char that we're not echoing. */
9898 echo_message_buffer = Qnil;
9899
9900 /* Prevent redisplay optimization in redisplay_internal by resetting
9901 this_line_start_pos. This is done because the mini-buffer now
9902 displays the message instead of its buffer text. */
9903 if (EQ (mini_window, selected_window))
9904 CHARPOS (this_line_start_pos) = 0;
9905
9906 return window_height_changed_p;
9907 }
9908
9909
9910 \f
9911 /***********************************************************************
9912 Mode Lines and Frame Titles
9913 ***********************************************************************/
9914
9915 /* A buffer for constructing non-propertized mode-line strings and
9916 frame titles in it; allocated from the heap in init_xdisp and
9917 resized as needed in store_mode_line_noprop_char. */
9918
9919 static char *mode_line_noprop_buf;
9920
9921 /* The buffer's end, and a current output position in it. */
9922
9923 static char *mode_line_noprop_buf_end;
9924 static char *mode_line_noprop_ptr;
9925
9926 #define MODE_LINE_NOPROP_LEN(start) \
9927 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9928
9929 static enum {
9930 MODE_LINE_DISPLAY = 0,
9931 MODE_LINE_TITLE,
9932 MODE_LINE_NOPROP,
9933 MODE_LINE_STRING
9934 } mode_line_target;
9935
9936 /* Alist that caches the results of :propertize.
9937 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9938 static Lisp_Object mode_line_proptrans_alist;
9939
9940 /* List of strings making up the mode-line. */
9941 static Lisp_Object mode_line_string_list;
9942
9943 /* Base face property when building propertized mode line string. */
9944 static Lisp_Object mode_line_string_face;
9945 static Lisp_Object mode_line_string_face_prop;
9946
9947
9948 /* Unwind data for mode line strings */
9949
9950 static Lisp_Object Vmode_line_unwind_vector;
9951
9952 static Lisp_Object
9953 format_mode_line_unwind_data (struct buffer *obuf,
9954 Lisp_Object owin,
9955 int save_proptrans)
9956 {
9957 Lisp_Object vector, tmp;
9958
9959 /* Reduce consing by keeping one vector in
9960 Vwith_echo_area_save_vector. */
9961 vector = Vmode_line_unwind_vector;
9962 Vmode_line_unwind_vector = Qnil;
9963
9964 if (NILP (vector))
9965 vector = Fmake_vector (make_number (8), Qnil);
9966
9967 ASET (vector, 0, make_number (mode_line_target));
9968 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9969 ASET (vector, 2, mode_line_string_list);
9970 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9971 ASET (vector, 4, mode_line_string_face);
9972 ASET (vector, 5, mode_line_string_face_prop);
9973
9974 if (obuf)
9975 XSETBUFFER (tmp, obuf);
9976 else
9977 tmp = Qnil;
9978 ASET (vector, 6, tmp);
9979 ASET (vector, 7, owin);
9980
9981 return vector;
9982 }
9983
9984 static Lisp_Object
9985 unwind_format_mode_line (Lisp_Object vector)
9986 {
9987 mode_line_target = XINT (AREF (vector, 0));
9988 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9989 mode_line_string_list = AREF (vector, 2);
9990 if (! EQ (AREF (vector, 3), Qt))
9991 mode_line_proptrans_alist = AREF (vector, 3);
9992 mode_line_string_face = AREF (vector, 4);
9993 mode_line_string_face_prop = AREF (vector, 5);
9994
9995 if (!NILP (AREF (vector, 7)))
9996 /* Select window before buffer, since it may change the buffer. */
9997 Fselect_window (AREF (vector, 7), Qt);
9998
9999 if (!NILP (AREF (vector, 6)))
10000 {
10001 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10002 ASET (vector, 6, Qnil);
10003 }
10004
10005 Vmode_line_unwind_vector = vector;
10006 return Qnil;
10007 }
10008
10009
10010 /* Store a single character C for the frame title in mode_line_noprop_buf.
10011 Re-allocate mode_line_noprop_buf if necessary. */
10012
10013 static void
10014 store_mode_line_noprop_char (char c)
10015 {
10016 /* If output position has reached the end of the allocated buffer,
10017 double the buffer's size. */
10018 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10019 {
10020 int len = MODE_LINE_NOPROP_LEN (0);
10021 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10022 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10023 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10024 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10025 }
10026
10027 *mode_line_noprop_ptr++ = c;
10028 }
10029
10030
10031 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10032 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10033 characters that yield more columns than PRECISION; PRECISION <= 0
10034 means copy the whole string. Pad with spaces until FIELD_WIDTH
10035 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10036 pad. Called from display_mode_element when it is used to build a
10037 frame title. */
10038
10039 static int
10040 store_mode_line_noprop (const char *string, int field_width, int precision)
10041 {
10042 const unsigned char *str = (const unsigned char *) string;
10043 int n = 0;
10044 EMACS_INT dummy, nbytes;
10045
10046 /* Copy at most PRECISION chars from STR. */
10047 nbytes = strlen (string);
10048 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10049 while (nbytes--)
10050 store_mode_line_noprop_char (*str++);
10051
10052 /* Fill up with spaces until FIELD_WIDTH reached. */
10053 while (field_width > 0
10054 && n < field_width)
10055 {
10056 store_mode_line_noprop_char (' ');
10057 ++n;
10058 }
10059
10060 return n;
10061 }
10062
10063 /***********************************************************************
10064 Frame Titles
10065 ***********************************************************************/
10066
10067 #ifdef HAVE_WINDOW_SYSTEM
10068
10069 /* Set the title of FRAME, if it has changed. The title format is
10070 Vicon_title_format if FRAME is iconified, otherwise it is
10071 frame_title_format. */
10072
10073 static void
10074 x_consider_frame_title (Lisp_Object frame)
10075 {
10076 struct frame *f = XFRAME (frame);
10077
10078 if (FRAME_WINDOW_P (f)
10079 || FRAME_MINIBUF_ONLY_P (f)
10080 || f->explicit_name)
10081 {
10082 /* Do we have more than one visible frame on this X display? */
10083 Lisp_Object tail;
10084 Lisp_Object fmt;
10085 int title_start;
10086 char *title;
10087 int len;
10088 struct it it;
10089 int count = SPECPDL_INDEX ();
10090
10091 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10092 {
10093 Lisp_Object other_frame = XCAR (tail);
10094 struct frame *tf = XFRAME (other_frame);
10095
10096 if (tf != f
10097 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10098 && !FRAME_MINIBUF_ONLY_P (tf)
10099 && !EQ (other_frame, tip_frame)
10100 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10101 break;
10102 }
10103
10104 /* Set global variable indicating that multiple frames exist. */
10105 multiple_frames = CONSP (tail);
10106
10107 /* Switch to the buffer of selected window of the frame. Set up
10108 mode_line_target so that display_mode_element will output into
10109 mode_line_noprop_buf; then display the title. */
10110 record_unwind_protect (unwind_format_mode_line,
10111 format_mode_line_unwind_data
10112 (current_buffer, selected_window, 0));
10113
10114 Fselect_window (f->selected_window, Qt);
10115 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10116 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10117
10118 mode_line_target = MODE_LINE_TITLE;
10119 title_start = MODE_LINE_NOPROP_LEN (0);
10120 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10121 NULL, DEFAULT_FACE_ID);
10122 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10123 len = MODE_LINE_NOPROP_LEN (title_start);
10124 title = mode_line_noprop_buf + title_start;
10125 unbind_to (count, Qnil);
10126
10127 /* Set the title only if it's changed. This avoids consing in
10128 the common case where it hasn't. (If it turns out that we've
10129 already wasted too much time by walking through the list with
10130 display_mode_element, then we might need to optimize at a
10131 higher level than this.) */
10132 if (! STRINGP (f->name)
10133 || SBYTES (f->name) != len
10134 || memcmp (title, SDATA (f->name), len) != 0)
10135 x_implicitly_set_name (f, make_string (title, len), Qnil);
10136 }
10137 }
10138
10139 #endif /* not HAVE_WINDOW_SYSTEM */
10140
10141
10142
10143 \f
10144 /***********************************************************************
10145 Menu Bars
10146 ***********************************************************************/
10147
10148
10149 /* Prepare for redisplay by updating menu-bar item lists when
10150 appropriate. This can call eval. */
10151
10152 void
10153 prepare_menu_bars (void)
10154 {
10155 int all_windows;
10156 struct gcpro gcpro1, gcpro2;
10157 struct frame *f;
10158 Lisp_Object tooltip_frame;
10159
10160 #ifdef HAVE_WINDOW_SYSTEM
10161 tooltip_frame = tip_frame;
10162 #else
10163 tooltip_frame = Qnil;
10164 #endif
10165
10166 /* Update all frame titles based on their buffer names, etc. We do
10167 this before the menu bars so that the buffer-menu will show the
10168 up-to-date frame titles. */
10169 #ifdef HAVE_WINDOW_SYSTEM
10170 if (windows_or_buffers_changed || update_mode_lines)
10171 {
10172 Lisp_Object tail, frame;
10173
10174 FOR_EACH_FRAME (tail, frame)
10175 {
10176 f = XFRAME (frame);
10177 if (!EQ (frame, tooltip_frame)
10178 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10179 x_consider_frame_title (frame);
10180 }
10181 }
10182 #endif /* HAVE_WINDOW_SYSTEM */
10183
10184 /* Update the menu bar item lists, if appropriate. This has to be
10185 done before any actual redisplay or generation of display lines. */
10186 all_windows = (update_mode_lines
10187 || buffer_shared > 1
10188 || windows_or_buffers_changed);
10189 if (all_windows)
10190 {
10191 Lisp_Object tail, frame;
10192 int count = SPECPDL_INDEX ();
10193 /* 1 means that update_menu_bar has run its hooks
10194 so any further calls to update_menu_bar shouldn't do so again. */
10195 int menu_bar_hooks_run = 0;
10196
10197 record_unwind_save_match_data ();
10198
10199 FOR_EACH_FRAME (tail, frame)
10200 {
10201 f = XFRAME (frame);
10202
10203 /* Ignore tooltip frame. */
10204 if (EQ (frame, tooltip_frame))
10205 continue;
10206
10207 /* If a window on this frame changed size, report that to
10208 the user and clear the size-change flag. */
10209 if (FRAME_WINDOW_SIZES_CHANGED (f))
10210 {
10211 Lisp_Object functions;
10212
10213 /* Clear flag first in case we get an error below. */
10214 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10215 functions = Vwindow_size_change_functions;
10216 GCPRO2 (tail, functions);
10217
10218 while (CONSP (functions))
10219 {
10220 if (!EQ (XCAR (functions), Qt))
10221 call1 (XCAR (functions), frame);
10222 functions = XCDR (functions);
10223 }
10224 UNGCPRO;
10225 }
10226
10227 GCPRO1 (tail);
10228 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10229 #ifdef HAVE_WINDOW_SYSTEM
10230 update_tool_bar (f, 0);
10231 #endif
10232 #ifdef HAVE_NS
10233 if (windows_or_buffers_changed
10234 && FRAME_NS_P (f))
10235 ns_set_doc_edited (f, Fbuffer_modified_p
10236 (XWINDOW (f->selected_window)->buffer));
10237 #endif
10238 UNGCPRO;
10239 }
10240
10241 unbind_to (count, Qnil);
10242 }
10243 else
10244 {
10245 struct frame *sf = SELECTED_FRAME ();
10246 update_menu_bar (sf, 1, 0);
10247 #ifdef HAVE_WINDOW_SYSTEM
10248 update_tool_bar (sf, 1);
10249 #endif
10250 }
10251 }
10252
10253
10254 /* Update the menu bar item list for frame F. This has to be done
10255 before we start to fill in any display lines, because it can call
10256 eval.
10257
10258 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10259
10260 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10261 already ran the menu bar hooks for this redisplay, so there
10262 is no need to run them again. The return value is the
10263 updated value of this flag, to pass to the next call. */
10264
10265 static int
10266 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10267 {
10268 Lisp_Object window;
10269 register struct window *w;
10270
10271 /* If called recursively during a menu update, do nothing. This can
10272 happen when, for instance, an activate-menubar-hook causes a
10273 redisplay. */
10274 if (inhibit_menubar_update)
10275 return hooks_run;
10276
10277 window = FRAME_SELECTED_WINDOW (f);
10278 w = XWINDOW (window);
10279
10280 if (FRAME_WINDOW_P (f)
10281 ?
10282 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10283 || defined (HAVE_NS) || defined (USE_GTK)
10284 FRAME_EXTERNAL_MENU_BAR (f)
10285 #else
10286 FRAME_MENU_BAR_LINES (f) > 0
10287 #endif
10288 : FRAME_MENU_BAR_LINES (f) > 0)
10289 {
10290 /* If the user has switched buffers or windows, we need to
10291 recompute to reflect the new bindings. But we'll
10292 recompute when update_mode_lines is set too; that means
10293 that people can use force-mode-line-update to request
10294 that the menu bar be recomputed. The adverse effect on
10295 the rest of the redisplay algorithm is about the same as
10296 windows_or_buffers_changed anyway. */
10297 if (windows_or_buffers_changed
10298 /* This used to test w->update_mode_line, but we believe
10299 there is no need to recompute the menu in that case. */
10300 || update_mode_lines
10301 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10302 < BUF_MODIFF (XBUFFER (w->buffer)))
10303 != !NILP (w->last_had_star))
10304 || ((!NILP (Vtransient_mark_mode)
10305 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10306 != !NILP (w->region_showing)))
10307 {
10308 struct buffer *prev = current_buffer;
10309 int count = SPECPDL_INDEX ();
10310
10311 specbind (Qinhibit_menubar_update, Qt);
10312
10313 set_buffer_internal_1 (XBUFFER (w->buffer));
10314 if (save_match_data)
10315 record_unwind_save_match_data ();
10316 if (NILP (Voverriding_local_map_menu_flag))
10317 {
10318 specbind (Qoverriding_terminal_local_map, Qnil);
10319 specbind (Qoverriding_local_map, Qnil);
10320 }
10321
10322 if (!hooks_run)
10323 {
10324 /* Run the Lucid hook. */
10325 safe_run_hooks (Qactivate_menubar_hook);
10326
10327 /* If it has changed current-menubar from previous value,
10328 really recompute the menu-bar from the value. */
10329 if (! NILP (Vlucid_menu_bar_dirty_flag))
10330 call0 (Qrecompute_lucid_menubar);
10331
10332 safe_run_hooks (Qmenu_bar_update_hook);
10333
10334 hooks_run = 1;
10335 }
10336
10337 XSETFRAME (Vmenu_updating_frame, f);
10338 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10339
10340 /* Redisplay the menu bar in case we changed it. */
10341 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10342 || defined (HAVE_NS) || defined (USE_GTK)
10343 if (FRAME_WINDOW_P (f))
10344 {
10345 #if defined (HAVE_NS)
10346 /* All frames on Mac OS share the same menubar. So only
10347 the selected frame should be allowed to set it. */
10348 if (f == SELECTED_FRAME ())
10349 #endif
10350 set_frame_menubar (f, 0, 0);
10351 }
10352 else
10353 /* On a terminal screen, the menu bar is an ordinary screen
10354 line, and this makes it get updated. */
10355 w->update_mode_line = Qt;
10356 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10357 /* In the non-toolkit version, the menu bar is an ordinary screen
10358 line, and this makes it get updated. */
10359 w->update_mode_line = Qt;
10360 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10361
10362 unbind_to (count, Qnil);
10363 set_buffer_internal_1 (prev);
10364 }
10365 }
10366
10367 return hooks_run;
10368 }
10369
10370
10371 \f
10372 /***********************************************************************
10373 Output Cursor
10374 ***********************************************************************/
10375
10376 #ifdef HAVE_WINDOW_SYSTEM
10377
10378 /* EXPORT:
10379 Nominal cursor position -- where to draw output.
10380 HPOS and VPOS are window relative glyph matrix coordinates.
10381 X and Y are window relative pixel coordinates. */
10382
10383 struct cursor_pos output_cursor;
10384
10385
10386 /* EXPORT:
10387 Set the global variable output_cursor to CURSOR. All cursor
10388 positions are relative to updated_window. */
10389
10390 void
10391 set_output_cursor (struct cursor_pos *cursor)
10392 {
10393 output_cursor.hpos = cursor->hpos;
10394 output_cursor.vpos = cursor->vpos;
10395 output_cursor.x = cursor->x;
10396 output_cursor.y = cursor->y;
10397 }
10398
10399
10400 /* EXPORT for RIF:
10401 Set a nominal cursor position.
10402
10403 HPOS and VPOS are column/row positions in a window glyph matrix. X
10404 and Y are window text area relative pixel positions.
10405
10406 If this is done during an update, updated_window will contain the
10407 window that is being updated and the position is the future output
10408 cursor position for that window. If updated_window is null, use
10409 selected_window and display the cursor at the given position. */
10410
10411 void
10412 x_cursor_to (int vpos, int hpos, int y, int x)
10413 {
10414 struct window *w;
10415
10416 /* If updated_window is not set, work on selected_window. */
10417 if (updated_window)
10418 w = updated_window;
10419 else
10420 w = XWINDOW (selected_window);
10421
10422 /* Set the output cursor. */
10423 output_cursor.hpos = hpos;
10424 output_cursor.vpos = vpos;
10425 output_cursor.x = x;
10426 output_cursor.y = y;
10427
10428 /* If not called as part of an update, really display the cursor.
10429 This will also set the cursor position of W. */
10430 if (updated_window == NULL)
10431 {
10432 BLOCK_INPUT;
10433 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10434 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10435 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10436 UNBLOCK_INPUT;
10437 }
10438 }
10439
10440 #endif /* HAVE_WINDOW_SYSTEM */
10441
10442 \f
10443 /***********************************************************************
10444 Tool-bars
10445 ***********************************************************************/
10446
10447 #ifdef HAVE_WINDOW_SYSTEM
10448
10449 /* Where the mouse was last time we reported a mouse event. */
10450
10451 FRAME_PTR last_mouse_frame;
10452
10453 /* Tool-bar item index of the item on which a mouse button was pressed
10454 or -1. */
10455
10456 int last_tool_bar_item;
10457
10458
10459 static Lisp_Object
10460 update_tool_bar_unwind (Lisp_Object frame)
10461 {
10462 selected_frame = frame;
10463 return Qnil;
10464 }
10465
10466 /* Update the tool-bar item list for frame F. This has to be done
10467 before we start to fill in any display lines. Called from
10468 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10469 and restore it here. */
10470
10471 static void
10472 update_tool_bar (struct frame *f, int save_match_data)
10473 {
10474 #if defined (USE_GTK) || defined (HAVE_NS)
10475 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10476 #else
10477 int do_update = WINDOWP (f->tool_bar_window)
10478 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10479 #endif
10480
10481 if (do_update)
10482 {
10483 Lisp_Object window;
10484 struct window *w;
10485
10486 window = FRAME_SELECTED_WINDOW (f);
10487 w = XWINDOW (window);
10488
10489 /* If the user has switched buffers or windows, we need to
10490 recompute to reflect the new bindings. But we'll
10491 recompute when update_mode_lines is set too; that means
10492 that people can use force-mode-line-update to request
10493 that the menu bar be recomputed. The adverse effect on
10494 the rest of the redisplay algorithm is about the same as
10495 windows_or_buffers_changed anyway. */
10496 if (windows_or_buffers_changed
10497 || !NILP (w->update_mode_line)
10498 || update_mode_lines
10499 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10500 < BUF_MODIFF (XBUFFER (w->buffer)))
10501 != !NILP (w->last_had_star))
10502 || ((!NILP (Vtransient_mark_mode)
10503 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10504 != !NILP (w->region_showing)))
10505 {
10506 struct buffer *prev = current_buffer;
10507 int count = SPECPDL_INDEX ();
10508 Lisp_Object frame, new_tool_bar;
10509 int new_n_tool_bar;
10510 struct gcpro gcpro1;
10511
10512 /* Set current_buffer to the buffer of the selected
10513 window of the frame, so that we get the right local
10514 keymaps. */
10515 set_buffer_internal_1 (XBUFFER (w->buffer));
10516
10517 /* Save match data, if we must. */
10518 if (save_match_data)
10519 record_unwind_save_match_data ();
10520
10521 /* Make sure that we don't accidentally use bogus keymaps. */
10522 if (NILP (Voverriding_local_map_menu_flag))
10523 {
10524 specbind (Qoverriding_terminal_local_map, Qnil);
10525 specbind (Qoverriding_local_map, Qnil);
10526 }
10527
10528 GCPRO1 (new_tool_bar);
10529
10530 /* We must temporarily set the selected frame to this frame
10531 before calling tool_bar_items, because the calculation of
10532 the tool-bar keymap uses the selected frame (see
10533 `tool-bar-make-keymap' in tool-bar.el). */
10534 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10535 XSETFRAME (frame, f);
10536 selected_frame = frame;
10537
10538 /* Build desired tool-bar items from keymaps. */
10539 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10540 &new_n_tool_bar);
10541
10542 /* Redisplay the tool-bar if we changed it. */
10543 if (new_n_tool_bar != f->n_tool_bar_items
10544 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10545 {
10546 /* Redisplay that happens asynchronously due to an expose event
10547 may access f->tool_bar_items. Make sure we update both
10548 variables within BLOCK_INPUT so no such event interrupts. */
10549 BLOCK_INPUT;
10550 f->tool_bar_items = new_tool_bar;
10551 f->n_tool_bar_items = new_n_tool_bar;
10552 w->update_mode_line = Qt;
10553 UNBLOCK_INPUT;
10554 }
10555
10556 UNGCPRO;
10557
10558 unbind_to (count, Qnil);
10559 set_buffer_internal_1 (prev);
10560 }
10561 }
10562 }
10563
10564
10565 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10566 F's desired tool-bar contents. F->tool_bar_items must have
10567 been set up previously by calling prepare_menu_bars. */
10568
10569 static void
10570 build_desired_tool_bar_string (struct frame *f)
10571 {
10572 int i, size, size_needed;
10573 struct gcpro gcpro1, gcpro2, gcpro3;
10574 Lisp_Object image, plist, props;
10575
10576 image = plist = props = Qnil;
10577 GCPRO3 (image, plist, props);
10578
10579 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10580 Otherwise, make a new string. */
10581
10582 /* The size of the string we might be able to reuse. */
10583 size = (STRINGP (f->desired_tool_bar_string)
10584 ? SCHARS (f->desired_tool_bar_string)
10585 : 0);
10586
10587 /* We need one space in the string for each image. */
10588 size_needed = f->n_tool_bar_items;
10589
10590 /* Reuse f->desired_tool_bar_string, if possible. */
10591 if (size < size_needed || NILP (f->desired_tool_bar_string))
10592 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10593 make_number (' '));
10594 else
10595 {
10596 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10597 Fremove_text_properties (make_number (0), make_number (size),
10598 props, f->desired_tool_bar_string);
10599 }
10600
10601 /* Put a `display' property on the string for the images to display,
10602 put a `menu_item' property on tool-bar items with a value that
10603 is the index of the item in F's tool-bar item vector. */
10604 for (i = 0; i < f->n_tool_bar_items; ++i)
10605 {
10606 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10607
10608 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10609 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10610 int hmargin, vmargin, relief, idx, end;
10611
10612 /* If image is a vector, choose the image according to the
10613 button state. */
10614 image = PROP (TOOL_BAR_ITEM_IMAGES);
10615 if (VECTORP (image))
10616 {
10617 if (enabled_p)
10618 idx = (selected_p
10619 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10620 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10621 else
10622 idx = (selected_p
10623 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10624 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10625
10626 xassert (ASIZE (image) >= idx);
10627 image = AREF (image, idx);
10628 }
10629 else
10630 idx = -1;
10631
10632 /* Ignore invalid image specifications. */
10633 if (!valid_image_p (image))
10634 continue;
10635
10636 /* Display the tool-bar button pressed, or depressed. */
10637 plist = Fcopy_sequence (XCDR (image));
10638
10639 /* Compute margin and relief to draw. */
10640 relief = (tool_bar_button_relief >= 0
10641 ? tool_bar_button_relief
10642 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10643 hmargin = vmargin = relief;
10644
10645 if (INTEGERP (Vtool_bar_button_margin)
10646 && XINT (Vtool_bar_button_margin) > 0)
10647 {
10648 hmargin += XFASTINT (Vtool_bar_button_margin);
10649 vmargin += XFASTINT (Vtool_bar_button_margin);
10650 }
10651 else if (CONSP (Vtool_bar_button_margin))
10652 {
10653 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10654 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10655 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10656
10657 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10658 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10659 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10660 }
10661
10662 if (auto_raise_tool_bar_buttons_p)
10663 {
10664 /* Add a `:relief' property to the image spec if the item is
10665 selected. */
10666 if (selected_p)
10667 {
10668 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10669 hmargin -= relief;
10670 vmargin -= relief;
10671 }
10672 }
10673 else
10674 {
10675 /* If image is selected, display it pressed, i.e. with a
10676 negative relief. If it's not selected, display it with a
10677 raised relief. */
10678 plist = Fplist_put (plist, QCrelief,
10679 (selected_p
10680 ? make_number (-relief)
10681 : make_number (relief)));
10682 hmargin -= relief;
10683 vmargin -= relief;
10684 }
10685
10686 /* Put a margin around the image. */
10687 if (hmargin || vmargin)
10688 {
10689 if (hmargin == vmargin)
10690 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10691 else
10692 plist = Fplist_put (plist, QCmargin,
10693 Fcons (make_number (hmargin),
10694 make_number (vmargin)));
10695 }
10696
10697 /* If button is not enabled, and we don't have special images
10698 for the disabled state, make the image appear disabled by
10699 applying an appropriate algorithm to it. */
10700 if (!enabled_p && idx < 0)
10701 plist = Fplist_put (plist, QCconversion, Qdisabled);
10702
10703 /* Put a `display' text property on the string for the image to
10704 display. Put a `menu-item' property on the string that gives
10705 the start of this item's properties in the tool-bar items
10706 vector. */
10707 image = Fcons (Qimage, plist);
10708 props = list4 (Qdisplay, image,
10709 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10710
10711 /* Let the last image hide all remaining spaces in the tool bar
10712 string. The string can be longer than needed when we reuse a
10713 previous string. */
10714 if (i + 1 == f->n_tool_bar_items)
10715 end = SCHARS (f->desired_tool_bar_string);
10716 else
10717 end = i + 1;
10718 Fadd_text_properties (make_number (i), make_number (end),
10719 props, f->desired_tool_bar_string);
10720 #undef PROP
10721 }
10722
10723 UNGCPRO;
10724 }
10725
10726
10727 /* Display one line of the tool-bar of frame IT->f.
10728
10729 HEIGHT specifies the desired height of the tool-bar line.
10730 If the actual height of the glyph row is less than HEIGHT, the
10731 row's height is increased to HEIGHT, and the icons are centered
10732 vertically in the new height.
10733
10734 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10735 count a final empty row in case the tool-bar width exactly matches
10736 the window width.
10737 */
10738
10739 static void
10740 display_tool_bar_line (struct it *it, int height)
10741 {
10742 struct glyph_row *row = it->glyph_row;
10743 int max_x = it->last_visible_x;
10744 struct glyph *last;
10745
10746 prepare_desired_row (row);
10747 row->y = it->current_y;
10748
10749 /* Note that this isn't made use of if the face hasn't a box,
10750 so there's no need to check the face here. */
10751 it->start_of_box_run_p = 1;
10752
10753 while (it->current_x < max_x)
10754 {
10755 int x, n_glyphs_before, i, nglyphs;
10756 struct it it_before;
10757
10758 /* Get the next display element. */
10759 if (!get_next_display_element (it))
10760 {
10761 /* Don't count empty row if we are counting needed tool-bar lines. */
10762 if (height < 0 && !it->hpos)
10763 return;
10764 break;
10765 }
10766
10767 /* Produce glyphs. */
10768 n_glyphs_before = row->used[TEXT_AREA];
10769 it_before = *it;
10770
10771 PRODUCE_GLYPHS (it);
10772
10773 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10774 i = 0;
10775 x = it_before.current_x;
10776 while (i < nglyphs)
10777 {
10778 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10779
10780 if (x + glyph->pixel_width > max_x)
10781 {
10782 /* Glyph doesn't fit on line. Backtrack. */
10783 row->used[TEXT_AREA] = n_glyphs_before;
10784 *it = it_before;
10785 /* If this is the only glyph on this line, it will never fit on the
10786 tool-bar, so skip it. But ensure there is at least one glyph,
10787 so we don't accidentally disable the tool-bar. */
10788 if (n_glyphs_before == 0
10789 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10790 break;
10791 goto out;
10792 }
10793
10794 ++it->hpos;
10795 x += glyph->pixel_width;
10796 ++i;
10797 }
10798
10799 /* Stop at line end. */
10800 if (ITERATOR_AT_END_OF_LINE_P (it))
10801 break;
10802
10803 set_iterator_to_next (it, 1);
10804 }
10805
10806 out:;
10807
10808 row->displays_text_p = row->used[TEXT_AREA] != 0;
10809
10810 /* Use default face for the border below the tool bar.
10811
10812 FIXME: When auto-resize-tool-bars is grow-only, there is
10813 no additional border below the possibly empty tool-bar lines.
10814 So to make the extra empty lines look "normal", we have to
10815 use the tool-bar face for the border too. */
10816 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10817 it->face_id = DEFAULT_FACE_ID;
10818
10819 extend_face_to_end_of_line (it);
10820 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10821 last->right_box_line_p = 1;
10822 if (last == row->glyphs[TEXT_AREA])
10823 last->left_box_line_p = 1;
10824
10825 /* Make line the desired height and center it vertically. */
10826 if ((height -= it->max_ascent + it->max_descent) > 0)
10827 {
10828 /* Don't add more than one line height. */
10829 height %= FRAME_LINE_HEIGHT (it->f);
10830 it->max_ascent += height / 2;
10831 it->max_descent += (height + 1) / 2;
10832 }
10833
10834 compute_line_metrics (it);
10835
10836 /* If line is empty, make it occupy the rest of the tool-bar. */
10837 if (!row->displays_text_p)
10838 {
10839 row->height = row->phys_height = it->last_visible_y - row->y;
10840 row->visible_height = row->height;
10841 row->ascent = row->phys_ascent = 0;
10842 row->extra_line_spacing = 0;
10843 }
10844
10845 row->full_width_p = 1;
10846 row->continued_p = 0;
10847 row->truncated_on_left_p = 0;
10848 row->truncated_on_right_p = 0;
10849
10850 it->current_x = it->hpos = 0;
10851 it->current_y += row->height;
10852 ++it->vpos;
10853 ++it->glyph_row;
10854 }
10855
10856
10857 /* Max tool-bar height. */
10858
10859 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10860 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10861
10862 /* Value is the number of screen lines needed to make all tool-bar
10863 items of frame F visible. The number of actual rows needed is
10864 returned in *N_ROWS if non-NULL. */
10865
10866 static int
10867 tool_bar_lines_needed (struct frame *f, int *n_rows)
10868 {
10869 struct window *w = XWINDOW (f->tool_bar_window);
10870 struct it it;
10871 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10872 the desired matrix, so use (unused) mode-line row as temporary row to
10873 avoid destroying the first tool-bar row. */
10874 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10875
10876 /* Initialize an iterator for iteration over
10877 F->desired_tool_bar_string in the tool-bar window of frame F. */
10878 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10879 it.first_visible_x = 0;
10880 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10881 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10882 it.paragraph_embedding = L2R;
10883
10884 while (!ITERATOR_AT_END_P (&it))
10885 {
10886 clear_glyph_row (temp_row);
10887 it.glyph_row = temp_row;
10888 display_tool_bar_line (&it, -1);
10889 }
10890 clear_glyph_row (temp_row);
10891
10892 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10893 if (n_rows)
10894 *n_rows = it.vpos > 0 ? it.vpos : -1;
10895
10896 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10897 }
10898
10899
10900 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10901 0, 1, 0,
10902 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10903 (Lisp_Object frame)
10904 {
10905 struct frame *f;
10906 struct window *w;
10907 int nlines = 0;
10908
10909 if (NILP (frame))
10910 frame = selected_frame;
10911 else
10912 CHECK_FRAME (frame);
10913 f = XFRAME (frame);
10914
10915 if (WINDOWP (f->tool_bar_window)
10916 || (w = XWINDOW (f->tool_bar_window),
10917 WINDOW_TOTAL_LINES (w) > 0))
10918 {
10919 update_tool_bar (f, 1);
10920 if (f->n_tool_bar_items)
10921 {
10922 build_desired_tool_bar_string (f);
10923 nlines = tool_bar_lines_needed (f, NULL);
10924 }
10925 }
10926
10927 return make_number (nlines);
10928 }
10929
10930
10931 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10932 height should be changed. */
10933
10934 static int
10935 redisplay_tool_bar (struct frame *f)
10936 {
10937 struct window *w;
10938 struct it it;
10939 struct glyph_row *row;
10940
10941 #if defined (USE_GTK) || defined (HAVE_NS)
10942 if (FRAME_EXTERNAL_TOOL_BAR (f))
10943 update_frame_tool_bar (f);
10944 return 0;
10945 #endif
10946
10947 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10948 do anything. This means you must start with tool-bar-lines
10949 non-zero to get the auto-sizing effect. Or in other words, you
10950 can turn off tool-bars by specifying tool-bar-lines zero. */
10951 if (!WINDOWP (f->tool_bar_window)
10952 || (w = XWINDOW (f->tool_bar_window),
10953 WINDOW_TOTAL_LINES (w) == 0))
10954 return 0;
10955
10956 /* Set up an iterator for the tool-bar window. */
10957 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10958 it.first_visible_x = 0;
10959 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10960 row = it.glyph_row;
10961
10962 /* Build a string that represents the contents of the tool-bar. */
10963 build_desired_tool_bar_string (f);
10964 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10965 /* FIXME: This should be controlled by a user option. But it
10966 doesn't make sense to have an R2L tool bar if the menu bar cannot
10967 be drawn also R2L, and making the menu bar R2L is tricky due to
10968 unibyte strings it uses and toolkit-specific code that implements
10969 it. If an R2L tool bar is ever supported, display_tool_bar_line
10970 should also be augmented to call unproduce_glyphs like
10971 display_line and display_string do. */
10972 it.paragraph_embedding = L2R;
10973
10974 if (f->n_tool_bar_rows == 0)
10975 {
10976 int nlines;
10977
10978 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10979 nlines != WINDOW_TOTAL_LINES (w)))
10980 {
10981 Lisp_Object frame;
10982 int old_height = WINDOW_TOTAL_LINES (w);
10983
10984 XSETFRAME (frame, f);
10985 Fmodify_frame_parameters (frame,
10986 Fcons (Fcons (Qtool_bar_lines,
10987 make_number (nlines)),
10988 Qnil));
10989 if (WINDOW_TOTAL_LINES (w) != old_height)
10990 {
10991 clear_glyph_matrix (w->desired_matrix);
10992 fonts_changed_p = 1;
10993 return 1;
10994 }
10995 }
10996 }
10997
10998 /* Display as many lines as needed to display all tool-bar items. */
10999
11000 if (f->n_tool_bar_rows > 0)
11001 {
11002 int border, rows, height, extra;
11003
11004 if (INTEGERP (Vtool_bar_border))
11005 border = XINT (Vtool_bar_border);
11006 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11007 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11008 else if (EQ (Vtool_bar_border, Qborder_width))
11009 border = f->border_width;
11010 else
11011 border = 0;
11012 if (border < 0)
11013 border = 0;
11014
11015 rows = f->n_tool_bar_rows;
11016 height = max (1, (it.last_visible_y - border) / rows);
11017 extra = it.last_visible_y - border - height * rows;
11018
11019 while (it.current_y < it.last_visible_y)
11020 {
11021 int h = 0;
11022 if (extra > 0 && rows-- > 0)
11023 {
11024 h = (extra + rows - 1) / rows;
11025 extra -= h;
11026 }
11027 display_tool_bar_line (&it, height + h);
11028 }
11029 }
11030 else
11031 {
11032 while (it.current_y < it.last_visible_y)
11033 display_tool_bar_line (&it, 0);
11034 }
11035
11036 /* It doesn't make much sense to try scrolling in the tool-bar
11037 window, so don't do it. */
11038 w->desired_matrix->no_scrolling_p = 1;
11039 w->must_be_updated_p = 1;
11040
11041 if (!NILP (Vauto_resize_tool_bars))
11042 {
11043 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11044 int change_height_p = 0;
11045
11046 /* If we couldn't display everything, change the tool-bar's
11047 height if there is room for more. */
11048 if (IT_STRING_CHARPOS (it) < it.end_charpos
11049 && it.current_y < max_tool_bar_height)
11050 change_height_p = 1;
11051
11052 row = it.glyph_row - 1;
11053
11054 /* If there are blank lines at the end, except for a partially
11055 visible blank line at the end that is smaller than
11056 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11057 if (!row->displays_text_p
11058 && row->height >= FRAME_LINE_HEIGHT (f))
11059 change_height_p = 1;
11060
11061 /* If row displays tool-bar items, but is partially visible,
11062 change the tool-bar's height. */
11063 if (row->displays_text_p
11064 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11065 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11066 change_height_p = 1;
11067
11068 /* Resize windows as needed by changing the `tool-bar-lines'
11069 frame parameter. */
11070 if (change_height_p)
11071 {
11072 Lisp_Object frame;
11073 int old_height = WINDOW_TOTAL_LINES (w);
11074 int nrows;
11075 int nlines = tool_bar_lines_needed (f, &nrows);
11076
11077 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11078 && !f->minimize_tool_bar_window_p)
11079 ? (nlines > old_height)
11080 : (nlines != old_height));
11081 f->minimize_tool_bar_window_p = 0;
11082
11083 if (change_height_p)
11084 {
11085 XSETFRAME (frame, f);
11086 Fmodify_frame_parameters (frame,
11087 Fcons (Fcons (Qtool_bar_lines,
11088 make_number (nlines)),
11089 Qnil));
11090 if (WINDOW_TOTAL_LINES (w) != old_height)
11091 {
11092 clear_glyph_matrix (w->desired_matrix);
11093 f->n_tool_bar_rows = nrows;
11094 fonts_changed_p = 1;
11095 return 1;
11096 }
11097 }
11098 }
11099 }
11100
11101 f->minimize_tool_bar_window_p = 0;
11102 return 0;
11103 }
11104
11105
11106 /* Get information about the tool-bar item which is displayed in GLYPH
11107 on frame F. Return in *PROP_IDX the index where tool-bar item
11108 properties start in F->tool_bar_items. Value is zero if
11109 GLYPH doesn't display a tool-bar item. */
11110
11111 static int
11112 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11113 {
11114 Lisp_Object prop;
11115 int success_p;
11116 int charpos;
11117
11118 /* This function can be called asynchronously, which means we must
11119 exclude any possibility that Fget_text_property signals an
11120 error. */
11121 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11122 charpos = max (0, charpos);
11123
11124 /* Get the text property `menu-item' at pos. The value of that
11125 property is the start index of this item's properties in
11126 F->tool_bar_items. */
11127 prop = Fget_text_property (make_number (charpos),
11128 Qmenu_item, f->current_tool_bar_string);
11129 if (INTEGERP (prop))
11130 {
11131 *prop_idx = XINT (prop);
11132 success_p = 1;
11133 }
11134 else
11135 success_p = 0;
11136
11137 return success_p;
11138 }
11139
11140 \f
11141 /* Get information about the tool-bar item at position X/Y on frame F.
11142 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11143 the current matrix of the tool-bar window of F, or NULL if not
11144 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11145 item in F->tool_bar_items. Value is
11146
11147 -1 if X/Y is not on a tool-bar item
11148 0 if X/Y is on the same item that was highlighted before.
11149 1 otherwise. */
11150
11151 static int
11152 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11153 int *hpos, int *vpos, int *prop_idx)
11154 {
11155 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11156 struct window *w = XWINDOW (f->tool_bar_window);
11157 int area;
11158
11159 /* Find the glyph under X/Y. */
11160 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11161 if (*glyph == NULL)
11162 return -1;
11163
11164 /* Get the start of this tool-bar item's properties in
11165 f->tool_bar_items. */
11166 if (!tool_bar_item_info (f, *glyph, prop_idx))
11167 return -1;
11168
11169 /* Is mouse on the highlighted item? */
11170 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11171 && *vpos >= hlinfo->mouse_face_beg_row
11172 && *vpos <= hlinfo->mouse_face_end_row
11173 && (*vpos > hlinfo->mouse_face_beg_row
11174 || *hpos >= hlinfo->mouse_face_beg_col)
11175 && (*vpos < hlinfo->mouse_face_end_row
11176 || *hpos < hlinfo->mouse_face_end_col
11177 || hlinfo->mouse_face_past_end))
11178 return 0;
11179
11180 return 1;
11181 }
11182
11183
11184 /* EXPORT:
11185 Handle mouse button event on the tool-bar of frame F, at
11186 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11187 0 for button release. MODIFIERS is event modifiers for button
11188 release. */
11189
11190 void
11191 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11192 unsigned int modifiers)
11193 {
11194 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11195 struct window *w = XWINDOW (f->tool_bar_window);
11196 int hpos, vpos, prop_idx;
11197 struct glyph *glyph;
11198 Lisp_Object enabled_p;
11199
11200 /* If not on the highlighted tool-bar item, return. */
11201 frame_to_window_pixel_xy (w, &x, &y);
11202 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11203 return;
11204
11205 /* If item is disabled, do nothing. */
11206 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11207 if (NILP (enabled_p))
11208 return;
11209
11210 if (down_p)
11211 {
11212 /* Show item in pressed state. */
11213 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11214 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11215 last_tool_bar_item = prop_idx;
11216 }
11217 else
11218 {
11219 Lisp_Object key, frame;
11220 struct input_event event;
11221 EVENT_INIT (event);
11222
11223 /* Show item in released state. */
11224 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11225 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11226
11227 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11228
11229 XSETFRAME (frame, f);
11230 event.kind = TOOL_BAR_EVENT;
11231 event.frame_or_window = frame;
11232 event.arg = frame;
11233 kbd_buffer_store_event (&event);
11234
11235 event.kind = TOOL_BAR_EVENT;
11236 event.frame_or_window = frame;
11237 event.arg = key;
11238 event.modifiers = modifiers;
11239 kbd_buffer_store_event (&event);
11240 last_tool_bar_item = -1;
11241 }
11242 }
11243
11244
11245 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11246 tool-bar window-relative coordinates X/Y. Called from
11247 note_mouse_highlight. */
11248
11249 static void
11250 note_tool_bar_highlight (struct frame *f, int x, int y)
11251 {
11252 Lisp_Object window = f->tool_bar_window;
11253 struct window *w = XWINDOW (window);
11254 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11255 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11256 int hpos, vpos;
11257 struct glyph *glyph;
11258 struct glyph_row *row;
11259 int i;
11260 Lisp_Object enabled_p;
11261 int prop_idx;
11262 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11263 int mouse_down_p, rc;
11264
11265 /* Function note_mouse_highlight is called with negative X/Y
11266 values when mouse moves outside of the frame. */
11267 if (x <= 0 || y <= 0)
11268 {
11269 clear_mouse_face (hlinfo);
11270 return;
11271 }
11272
11273 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11274 if (rc < 0)
11275 {
11276 /* Not on tool-bar item. */
11277 clear_mouse_face (hlinfo);
11278 return;
11279 }
11280 else if (rc == 0)
11281 /* On same tool-bar item as before. */
11282 goto set_help_echo;
11283
11284 clear_mouse_face (hlinfo);
11285
11286 /* Mouse is down, but on different tool-bar item? */
11287 mouse_down_p = (dpyinfo->grabbed
11288 && f == last_mouse_frame
11289 && FRAME_LIVE_P (f));
11290 if (mouse_down_p
11291 && last_tool_bar_item != prop_idx)
11292 return;
11293
11294 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11295 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11296
11297 /* If tool-bar item is not enabled, don't highlight it. */
11298 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11299 if (!NILP (enabled_p))
11300 {
11301 /* Compute the x-position of the glyph. In front and past the
11302 image is a space. We include this in the highlighted area. */
11303 row = MATRIX_ROW (w->current_matrix, vpos);
11304 for (i = x = 0; i < hpos; ++i)
11305 x += row->glyphs[TEXT_AREA][i].pixel_width;
11306
11307 /* Record this as the current active region. */
11308 hlinfo->mouse_face_beg_col = hpos;
11309 hlinfo->mouse_face_beg_row = vpos;
11310 hlinfo->mouse_face_beg_x = x;
11311 hlinfo->mouse_face_beg_y = row->y;
11312 hlinfo->mouse_face_past_end = 0;
11313
11314 hlinfo->mouse_face_end_col = hpos + 1;
11315 hlinfo->mouse_face_end_row = vpos;
11316 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11317 hlinfo->mouse_face_end_y = row->y;
11318 hlinfo->mouse_face_window = window;
11319 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11320
11321 /* Display it as active. */
11322 show_mouse_face (hlinfo, draw);
11323 hlinfo->mouse_face_image_state = draw;
11324 }
11325
11326 set_help_echo:
11327
11328 /* Set help_echo_string to a help string to display for this tool-bar item.
11329 XTread_socket does the rest. */
11330 help_echo_object = help_echo_window = Qnil;
11331 help_echo_pos = -1;
11332 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11333 if (NILP (help_echo_string))
11334 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11335 }
11336
11337 #endif /* HAVE_WINDOW_SYSTEM */
11338
11339
11340 \f
11341 /************************************************************************
11342 Horizontal scrolling
11343 ************************************************************************/
11344
11345 static int hscroll_window_tree (Lisp_Object);
11346 static int hscroll_windows (Lisp_Object);
11347
11348 /* For all leaf windows in the window tree rooted at WINDOW, set their
11349 hscroll value so that PT is (i) visible in the window, and (ii) so
11350 that it is not within a certain margin at the window's left and
11351 right border. Value is non-zero if any window's hscroll has been
11352 changed. */
11353
11354 static int
11355 hscroll_window_tree (Lisp_Object window)
11356 {
11357 int hscrolled_p = 0;
11358 int hscroll_relative_p = FLOATP (Vhscroll_step);
11359 int hscroll_step_abs = 0;
11360 double hscroll_step_rel = 0;
11361
11362 if (hscroll_relative_p)
11363 {
11364 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11365 if (hscroll_step_rel < 0)
11366 {
11367 hscroll_relative_p = 0;
11368 hscroll_step_abs = 0;
11369 }
11370 }
11371 else if (INTEGERP (Vhscroll_step))
11372 {
11373 hscroll_step_abs = XINT (Vhscroll_step);
11374 if (hscroll_step_abs < 0)
11375 hscroll_step_abs = 0;
11376 }
11377 else
11378 hscroll_step_abs = 0;
11379
11380 while (WINDOWP (window))
11381 {
11382 struct window *w = XWINDOW (window);
11383
11384 if (WINDOWP (w->hchild))
11385 hscrolled_p |= hscroll_window_tree (w->hchild);
11386 else if (WINDOWP (w->vchild))
11387 hscrolled_p |= hscroll_window_tree (w->vchild);
11388 else if (w->cursor.vpos >= 0)
11389 {
11390 int h_margin;
11391 int text_area_width;
11392 struct glyph_row *current_cursor_row
11393 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11394 struct glyph_row *desired_cursor_row
11395 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11396 struct glyph_row *cursor_row
11397 = (desired_cursor_row->enabled_p
11398 ? desired_cursor_row
11399 : current_cursor_row);
11400
11401 text_area_width = window_box_width (w, TEXT_AREA);
11402
11403 /* Scroll when cursor is inside this scroll margin. */
11404 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11405
11406 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11407 && ((XFASTINT (w->hscroll)
11408 && w->cursor.x <= h_margin)
11409 || (cursor_row->enabled_p
11410 && cursor_row->truncated_on_right_p
11411 && (w->cursor.x >= text_area_width - h_margin))))
11412 {
11413 struct it it;
11414 int hscroll;
11415 struct buffer *saved_current_buffer;
11416 EMACS_INT pt;
11417 int wanted_x;
11418
11419 /* Find point in a display of infinite width. */
11420 saved_current_buffer = current_buffer;
11421 current_buffer = XBUFFER (w->buffer);
11422
11423 if (w == XWINDOW (selected_window))
11424 pt = PT;
11425 else
11426 {
11427 pt = marker_position (w->pointm);
11428 pt = max (BEGV, pt);
11429 pt = min (ZV, pt);
11430 }
11431
11432 /* Move iterator to pt starting at cursor_row->start in
11433 a line with infinite width. */
11434 init_to_row_start (&it, w, cursor_row);
11435 it.last_visible_x = INFINITY;
11436 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11437 current_buffer = saved_current_buffer;
11438
11439 /* Position cursor in window. */
11440 if (!hscroll_relative_p && hscroll_step_abs == 0)
11441 hscroll = max (0, (it.current_x
11442 - (ITERATOR_AT_END_OF_LINE_P (&it)
11443 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11444 : (text_area_width / 2))))
11445 / FRAME_COLUMN_WIDTH (it.f);
11446 else if (w->cursor.x >= text_area_width - h_margin)
11447 {
11448 if (hscroll_relative_p)
11449 wanted_x = text_area_width * (1 - hscroll_step_rel)
11450 - h_margin;
11451 else
11452 wanted_x = text_area_width
11453 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11454 - h_margin;
11455 hscroll
11456 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11457 }
11458 else
11459 {
11460 if (hscroll_relative_p)
11461 wanted_x = text_area_width * hscroll_step_rel
11462 + h_margin;
11463 else
11464 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11465 + h_margin;
11466 hscroll
11467 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11468 }
11469 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11470
11471 /* Don't call Fset_window_hscroll if value hasn't
11472 changed because it will prevent redisplay
11473 optimizations. */
11474 if (XFASTINT (w->hscroll) != hscroll)
11475 {
11476 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11477 w->hscroll = make_number (hscroll);
11478 hscrolled_p = 1;
11479 }
11480 }
11481 }
11482
11483 window = w->next;
11484 }
11485
11486 /* Value is non-zero if hscroll of any leaf window has been changed. */
11487 return hscrolled_p;
11488 }
11489
11490
11491 /* Set hscroll so that cursor is visible and not inside horizontal
11492 scroll margins for all windows in the tree rooted at WINDOW. See
11493 also hscroll_window_tree above. Value is non-zero if any window's
11494 hscroll has been changed. If it has, desired matrices on the frame
11495 of WINDOW are cleared. */
11496
11497 static int
11498 hscroll_windows (Lisp_Object window)
11499 {
11500 int hscrolled_p = hscroll_window_tree (window);
11501 if (hscrolled_p)
11502 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11503 return hscrolled_p;
11504 }
11505
11506
11507 \f
11508 /************************************************************************
11509 Redisplay
11510 ************************************************************************/
11511
11512 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11513 to a non-zero value. This is sometimes handy to have in a debugger
11514 session. */
11515
11516 #if GLYPH_DEBUG
11517
11518 /* First and last unchanged row for try_window_id. */
11519
11520 int debug_first_unchanged_at_end_vpos;
11521 int debug_last_unchanged_at_beg_vpos;
11522
11523 /* Delta vpos and y. */
11524
11525 int debug_dvpos, debug_dy;
11526
11527 /* Delta in characters and bytes for try_window_id. */
11528
11529 EMACS_INT debug_delta, debug_delta_bytes;
11530
11531 /* Values of window_end_pos and window_end_vpos at the end of
11532 try_window_id. */
11533
11534 EMACS_INT debug_end_vpos;
11535
11536 /* Append a string to W->desired_matrix->method. FMT is a printf
11537 format string. A1...A9 are a supplement for a variable-length
11538 argument list. If trace_redisplay_p is non-zero also printf the
11539 resulting string to stderr. */
11540
11541 static void
11542 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11543 struct window *w;
11544 char *fmt;
11545 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11546 {
11547 char buffer[512];
11548 char *method = w->desired_matrix->method;
11549 int len = strlen (method);
11550 int size = sizeof w->desired_matrix->method;
11551 int remaining = size - len - 1;
11552
11553 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11554 if (len && remaining)
11555 {
11556 method[len] = '|';
11557 --remaining, ++len;
11558 }
11559
11560 strncpy (method + len, buffer, remaining);
11561
11562 if (trace_redisplay_p)
11563 fprintf (stderr, "%p (%s): %s\n",
11564 w,
11565 ((BUFFERP (w->buffer)
11566 && STRINGP (XBUFFER (w->buffer)->name))
11567 ? SSDATA (XBUFFER (w->buffer)->name)
11568 : "no buffer"),
11569 buffer);
11570 }
11571
11572 #endif /* GLYPH_DEBUG */
11573
11574
11575 /* Value is non-zero if all changes in window W, which displays
11576 current_buffer, are in the text between START and END. START is a
11577 buffer position, END is given as a distance from Z. Used in
11578 redisplay_internal for display optimization. */
11579
11580 static INLINE int
11581 text_outside_line_unchanged_p (struct window *w,
11582 EMACS_INT start, EMACS_INT end)
11583 {
11584 int unchanged_p = 1;
11585
11586 /* If text or overlays have changed, see where. */
11587 if (XFASTINT (w->last_modified) < MODIFF
11588 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11589 {
11590 /* Gap in the line? */
11591 if (GPT < start || Z - GPT < end)
11592 unchanged_p = 0;
11593
11594 /* Changes start in front of the line, or end after it? */
11595 if (unchanged_p
11596 && (BEG_UNCHANGED < start - 1
11597 || END_UNCHANGED < end))
11598 unchanged_p = 0;
11599
11600 /* If selective display, can't optimize if changes start at the
11601 beginning of the line. */
11602 if (unchanged_p
11603 && INTEGERP (BVAR (current_buffer, selective_display))
11604 && XINT (BVAR (current_buffer, selective_display)) > 0
11605 && (BEG_UNCHANGED < start || GPT <= start))
11606 unchanged_p = 0;
11607
11608 /* If there are overlays at the start or end of the line, these
11609 may have overlay strings with newlines in them. A change at
11610 START, for instance, may actually concern the display of such
11611 overlay strings as well, and they are displayed on different
11612 lines. So, quickly rule out this case. (For the future, it
11613 might be desirable to implement something more telling than
11614 just BEG/END_UNCHANGED.) */
11615 if (unchanged_p)
11616 {
11617 if (BEG + BEG_UNCHANGED == start
11618 && overlay_touches_p (start))
11619 unchanged_p = 0;
11620 if (END_UNCHANGED == end
11621 && overlay_touches_p (Z - end))
11622 unchanged_p = 0;
11623 }
11624
11625 /* Under bidi reordering, adding or deleting a character in the
11626 beginning of a paragraph, before the first strong directional
11627 character, can change the base direction of the paragraph (unless
11628 the buffer specifies a fixed paragraph direction), which will
11629 require to redisplay the whole paragraph. It might be worthwhile
11630 to find the paragraph limits and widen the range of redisplayed
11631 lines to that, but for now just give up this optimization. */
11632 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11633 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11634 unchanged_p = 0;
11635 }
11636
11637 return unchanged_p;
11638 }
11639
11640
11641 /* Do a frame update, taking possible shortcuts into account. This is
11642 the main external entry point for redisplay.
11643
11644 If the last redisplay displayed an echo area message and that message
11645 is no longer requested, we clear the echo area or bring back the
11646 mini-buffer if that is in use. */
11647
11648 void
11649 redisplay (void)
11650 {
11651 redisplay_internal ();
11652 }
11653
11654
11655 static Lisp_Object
11656 overlay_arrow_string_or_property (Lisp_Object var)
11657 {
11658 Lisp_Object val;
11659
11660 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11661 return val;
11662
11663 return Voverlay_arrow_string;
11664 }
11665
11666 /* Return 1 if there are any overlay-arrows in current_buffer. */
11667 static int
11668 overlay_arrow_in_current_buffer_p (void)
11669 {
11670 Lisp_Object vlist;
11671
11672 for (vlist = Voverlay_arrow_variable_list;
11673 CONSP (vlist);
11674 vlist = XCDR (vlist))
11675 {
11676 Lisp_Object var = XCAR (vlist);
11677 Lisp_Object val;
11678
11679 if (!SYMBOLP (var))
11680 continue;
11681 val = find_symbol_value (var);
11682 if (MARKERP (val)
11683 && current_buffer == XMARKER (val)->buffer)
11684 return 1;
11685 }
11686 return 0;
11687 }
11688
11689
11690 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11691 has changed. */
11692
11693 static int
11694 overlay_arrows_changed_p (void)
11695 {
11696 Lisp_Object vlist;
11697
11698 for (vlist = Voverlay_arrow_variable_list;
11699 CONSP (vlist);
11700 vlist = XCDR (vlist))
11701 {
11702 Lisp_Object var = XCAR (vlist);
11703 Lisp_Object val, pstr;
11704
11705 if (!SYMBOLP (var))
11706 continue;
11707 val = find_symbol_value (var);
11708 if (!MARKERP (val))
11709 continue;
11710 if (! EQ (COERCE_MARKER (val),
11711 Fget (var, Qlast_arrow_position))
11712 || ! (pstr = overlay_arrow_string_or_property (var),
11713 EQ (pstr, Fget (var, Qlast_arrow_string))))
11714 return 1;
11715 }
11716 return 0;
11717 }
11718
11719 /* Mark overlay arrows to be updated on next redisplay. */
11720
11721 static void
11722 update_overlay_arrows (int up_to_date)
11723 {
11724 Lisp_Object vlist;
11725
11726 for (vlist = Voverlay_arrow_variable_list;
11727 CONSP (vlist);
11728 vlist = XCDR (vlist))
11729 {
11730 Lisp_Object var = XCAR (vlist);
11731
11732 if (!SYMBOLP (var))
11733 continue;
11734
11735 if (up_to_date > 0)
11736 {
11737 Lisp_Object val = find_symbol_value (var);
11738 Fput (var, Qlast_arrow_position,
11739 COERCE_MARKER (val));
11740 Fput (var, Qlast_arrow_string,
11741 overlay_arrow_string_or_property (var));
11742 }
11743 else if (up_to_date < 0
11744 || !NILP (Fget (var, Qlast_arrow_position)))
11745 {
11746 Fput (var, Qlast_arrow_position, Qt);
11747 Fput (var, Qlast_arrow_string, Qt);
11748 }
11749 }
11750 }
11751
11752
11753 /* Return overlay arrow string to display at row.
11754 Return integer (bitmap number) for arrow bitmap in left fringe.
11755 Return nil if no overlay arrow. */
11756
11757 static Lisp_Object
11758 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11759 {
11760 Lisp_Object vlist;
11761
11762 for (vlist = Voverlay_arrow_variable_list;
11763 CONSP (vlist);
11764 vlist = XCDR (vlist))
11765 {
11766 Lisp_Object var = XCAR (vlist);
11767 Lisp_Object val;
11768
11769 if (!SYMBOLP (var))
11770 continue;
11771
11772 val = find_symbol_value (var);
11773
11774 if (MARKERP (val)
11775 && current_buffer == XMARKER (val)->buffer
11776 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11777 {
11778 if (FRAME_WINDOW_P (it->f)
11779 /* FIXME: if ROW->reversed_p is set, this should test
11780 the right fringe, not the left one. */
11781 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11782 {
11783 #ifdef HAVE_WINDOW_SYSTEM
11784 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11785 {
11786 int fringe_bitmap;
11787 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11788 return make_number (fringe_bitmap);
11789 }
11790 #endif
11791 return make_number (-1); /* Use default arrow bitmap */
11792 }
11793 return overlay_arrow_string_or_property (var);
11794 }
11795 }
11796
11797 return Qnil;
11798 }
11799
11800 /* Return 1 if point moved out of or into a composition. Otherwise
11801 return 0. PREV_BUF and PREV_PT are the last point buffer and
11802 position. BUF and PT are the current point buffer and position. */
11803
11804 static int
11805 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11806 struct buffer *buf, EMACS_INT pt)
11807 {
11808 EMACS_INT start, end;
11809 Lisp_Object prop;
11810 Lisp_Object buffer;
11811
11812 XSETBUFFER (buffer, buf);
11813 /* Check a composition at the last point if point moved within the
11814 same buffer. */
11815 if (prev_buf == buf)
11816 {
11817 if (prev_pt == pt)
11818 /* Point didn't move. */
11819 return 0;
11820
11821 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11822 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11823 && COMPOSITION_VALID_P (start, end, prop)
11824 && start < prev_pt && end > prev_pt)
11825 /* The last point was within the composition. Return 1 iff
11826 point moved out of the composition. */
11827 return (pt <= start || pt >= end);
11828 }
11829
11830 /* Check a composition at the current point. */
11831 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11832 && find_composition (pt, -1, &start, &end, &prop, buffer)
11833 && COMPOSITION_VALID_P (start, end, prop)
11834 && start < pt && end > pt);
11835 }
11836
11837
11838 /* Reconsider the setting of B->clip_changed which is displayed
11839 in window W. */
11840
11841 static INLINE void
11842 reconsider_clip_changes (struct window *w, struct buffer *b)
11843 {
11844 if (b->clip_changed
11845 && !NILP (w->window_end_valid)
11846 && w->current_matrix->buffer == b
11847 && w->current_matrix->zv == BUF_ZV (b)
11848 && w->current_matrix->begv == BUF_BEGV (b))
11849 b->clip_changed = 0;
11850
11851 /* If display wasn't paused, and W is not a tool bar window, see if
11852 point has been moved into or out of a composition. In that case,
11853 we set b->clip_changed to 1 to force updating the screen. If
11854 b->clip_changed has already been set to 1, we can skip this
11855 check. */
11856 if (!b->clip_changed
11857 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11858 {
11859 EMACS_INT pt;
11860
11861 if (w == XWINDOW (selected_window))
11862 pt = PT;
11863 else
11864 pt = marker_position (w->pointm);
11865
11866 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11867 || pt != XINT (w->last_point))
11868 && check_point_in_composition (w->current_matrix->buffer,
11869 XINT (w->last_point),
11870 XBUFFER (w->buffer), pt))
11871 b->clip_changed = 1;
11872 }
11873 }
11874 \f
11875
11876 /* Select FRAME to forward the values of frame-local variables into C
11877 variables so that the redisplay routines can access those values
11878 directly. */
11879
11880 static void
11881 select_frame_for_redisplay (Lisp_Object frame)
11882 {
11883 Lisp_Object tail, tem;
11884 Lisp_Object old = selected_frame;
11885 struct Lisp_Symbol *sym;
11886
11887 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11888
11889 selected_frame = frame;
11890
11891 do {
11892 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11893 if (CONSP (XCAR (tail))
11894 && (tem = XCAR (XCAR (tail)),
11895 SYMBOLP (tem))
11896 && (sym = indirect_variable (XSYMBOL (tem)),
11897 sym->redirect == SYMBOL_LOCALIZED)
11898 && sym->val.blv->frame_local)
11899 /* Use find_symbol_value rather than Fsymbol_value
11900 to avoid an error if it is void. */
11901 find_symbol_value (tem);
11902 } while (!EQ (frame, old) && (frame = old, 1));
11903 }
11904
11905
11906 #define STOP_POLLING \
11907 do { if (! polling_stopped_here) stop_polling (); \
11908 polling_stopped_here = 1; } while (0)
11909
11910 #define RESUME_POLLING \
11911 do { if (polling_stopped_here) start_polling (); \
11912 polling_stopped_here = 0; } while (0)
11913
11914
11915 /* Perhaps in the future avoid recentering windows if it
11916 is not necessary; currently that causes some problems. */
11917
11918 static void
11919 redisplay_internal (void)
11920 {
11921 struct window *w = XWINDOW (selected_window);
11922 struct window *sw;
11923 struct frame *fr;
11924 int pending;
11925 int must_finish = 0;
11926 struct text_pos tlbufpos, tlendpos;
11927 int number_of_visible_frames;
11928 int count, count1;
11929 struct frame *sf;
11930 int polling_stopped_here = 0;
11931 Lisp_Object old_frame = selected_frame;
11932
11933 /* Non-zero means redisplay has to consider all windows on all
11934 frames. Zero means, only selected_window is considered. */
11935 int consider_all_windows_p;
11936
11937 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11938
11939 /* No redisplay if running in batch mode or frame is not yet fully
11940 initialized, or redisplay is explicitly turned off by setting
11941 Vinhibit_redisplay. */
11942 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11943 || !NILP (Vinhibit_redisplay))
11944 return;
11945
11946 /* Don't examine these until after testing Vinhibit_redisplay.
11947 When Emacs is shutting down, perhaps because its connection to
11948 X has dropped, we should not look at them at all. */
11949 fr = XFRAME (w->frame);
11950 sf = SELECTED_FRAME ();
11951
11952 if (!fr->glyphs_initialized_p)
11953 return;
11954
11955 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11956 if (popup_activated ())
11957 return;
11958 #endif
11959
11960 /* I don't think this happens but let's be paranoid. */
11961 if (redisplaying_p)
11962 return;
11963
11964 /* Record a function that resets redisplaying_p to its old value
11965 when we leave this function. */
11966 count = SPECPDL_INDEX ();
11967 record_unwind_protect (unwind_redisplay,
11968 Fcons (make_number (redisplaying_p), selected_frame));
11969 ++redisplaying_p;
11970 specbind (Qinhibit_free_realized_faces, Qnil);
11971
11972 {
11973 Lisp_Object tail, frame;
11974
11975 FOR_EACH_FRAME (tail, frame)
11976 {
11977 struct frame *f = XFRAME (frame);
11978 f->already_hscrolled_p = 0;
11979 }
11980 }
11981
11982 retry:
11983 /* Remember the currently selected window. */
11984 sw = w;
11985
11986 if (!EQ (old_frame, selected_frame)
11987 && FRAME_LIVE_P (XFRAME (old_frame)))
11988 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11989 selected_frame and selected_window to be temporarily out-of-sync so
11990 when we come back here via `goto retry', we need to resync because we
11991 may need to run Elisp code (via prepare_menu_bars). */
11992 select_frame_for_redisplay (old_frame);
11993
11994 pending = 0;
11995 reconsider_clip_changes (w, current_buffer);
11996 last_escape_glyph_frame = NULL;
11997 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11998 last_glyphless_glyph_frame = NULL;
11999 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12000
12001 /* If new fonts have been loaded that make a glyph matrix adjustment
12002 necessary, do it. */
12003 if (fonts_changed_p)
12004 {
12005 adjust_glyphs (NULL);
12006 ++windows_or_buffers_changed;
12007 fonts_changed_p = 0;
12008 }
12009
12010 /* If face_change_count is non-zero, init_iterator will free all
12011 realized faces, which includes the faces referenced from current
12012 matrices. So, we can't reuse current matrices in this case. */
12013 if (face_change_count)
12014 ++windows_or_buffers_changed;
12015
12016 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12017 && FRAME_TTY (sf)->previous_frame != sf)
12018 {
12019 /* Since frames on a single ASCII terminal share the same
12020 display area, displaying a different frame means redisplay
12021 the whole thing. */
12022 windows_or_buffers_changed++;
12023 SET_FRAME_GARBAGED (sf);
12024 #ifndef DOS_NT
12025 set_tty_color_mode (FRAME_TTY (sf), sf);
12026 #endif
12027 FRAME_TTY (sf)->previous_frame = sf;
12028 }
12029
12030 /* Set the visible flags for all frames. Do this before checking
12031 for resized or garbaged frames; they want to know if their frames
12032 are visible. See the comment in frame.h for
12033 FRAME_SAMPLE_VISIBILITY. */
12034 {
12035 Lisp_Object tail, frame;
12036
12037 number_of_visible_frames = 0;
12038
12039 FOR_EACH_FRAME (tail, frame)
12040 {
12041 struct frame *f = XFRAME (frame);
12042
12043 FRAME_SAMPLE_VISIBILITY (f);
12044 if (FRAME_VISIBLE_P (f))
12045 ++number_of_visible_frames;
12046 clear_desired_matrices (f);
12047 }
12048 }
12049
12050 /* Notice any pending interrupt request to change frame size. */
12051 do_pending_window_change (1);
12052
12053 /* do_pending_window_change could change the selected_window due to
12054 frame resizing which makes the selected window too small. */
12055 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12056 {
12057 sw = w;
12058 reconsider_clip_changes (w, current_buffer);
12059 }
12060
12061 /* Clear frames marked as garbaged. */
12062 if (frame_garbaged)
12063 clear_garbaged_frames ();
12064
12065 /* Build menubar and tool-bar items. */
12066 if (NILP (Vmemory_full))
12067 prepare_menu_bars ();
12068
12069 if (windows_or_buffers_changed)
12070 update_mode_lines++;
12071
12072 /* Detect case that we need to write or remove a star in the mode line. */
12073 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12074 {
12075 w->update_mode_line = Qt;
12076 if (buffer_shared > 1)
12077 update_mode_lines++;
12078 }
12079
12080 /* Avoid invocation of point motion hooks by `current_column' below. */
12081 count1 = SPECPDL_INDEX ();
12082 specbind (Qinhibit_point_motion_hooks, Qt);
12083
12084 /* If %c is in the mode line, update it if needed. */
12085 if (!NILP (w->column_number_displayed)
12086 /* This alternative quickly identifies a common case
12087 where no change is needed. */
12088 && !(PT == XFASTINT (w->last_point)
12089 && XFASTINT (w->last_modified) >= MODIFF
12090 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12091 && (XFASTINT (w->column_number_displayed) != current_column ()))
12092 w->update_mode_line = Qt;
12093
12094 unbind_to (count1, Qnil);
12095
12096 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12097
12098 /* The variable buffer_shared is set in redisplay_window and
12099 indicates that we redisplay a buffer in different windows. See
12100 there. */
12101 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12102 || cursor_type_changed);
12103
12104 /* If specs for an arrow have changed, do thorough redisplay
12105 to ensure we remove any arrow that should no longer exist. */
12106 if (overlay_arrows_changed_p ())
12107 consider_all_windows_p = windows_or_buffers_changed = 1;
12108
12109 /* Normally the message* functions will have already displayed and
12110 updated the echo area, but the frame may have been trashed, or
12111 the update may have been preempted, so display the echo area
12112 again here. Checking message_cleared_p captures the case that
12113 the echo area should be cleared. */
12114 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12115 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12116 || (message_cleared_p
12117 && minibuf_level == 0
12118 /* If the mini-window is currently selected, this means the
12119 echo-area doesn't show through. */
12120 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12121 {
12122 int window_height_changed_p = echo_area_display (0);
12123 must_finish = 1;
12124
12125 /* If we don't display the current message, don't clear the
12126 message_cleared_p flag, because, if we did, we wouldn't clear
12127 the echo area in the next redisplay which doesn't preserve
12128 the echo area. */
12129 if (!display_last_displayed_message_p)
12130 message_cleared_p = 0;
12131
12132 if (fonts_changed_p)
12133 goto retry;
12134 else if (window_height_changed_p)
12135 {
12136 consider_all_windows_p = 1;
12137 ++update_mode_lines;
12138 ++windows_or_buffers_changed;
12139
12140 /* If window configuration was changed, frames may have been
12141 marked garbaged. Clear them or we will experience
12142 surprises wrt scrolling. */
12143 if (frame_garbaged)
12144 clear_garbaged_frames ();
12145 }
12146 }
12147 else if (EQ (selected_window, minibuf_window)
12148 && (current_buffer->clip_changed
12149 || XFASTINT (w->last_modified) < MODIFF
12150 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12151 && resize_mini_window (w, 0))
12152 {
12153 /* Resized active mini-window to fit the size of what it is
12154 showing if its contents might have changed. */
12155 must_finish = 1;
12156 /* FIXME: this causes all frames to be updated, which seems unnecessary
12157 since only the current frame needs to be considered. This function needs
12158 to be rewritten with two variables, consider_all_windows and
12159 consider_all_frames. */
12160 consider_all_windows_p = 1;
12161 ++windows_or_buffers_changed;
12162 ++update_mode_lines;
12163
12164 /* If window configuration was changed, frames may have been
12165 marked garbaged. Clear them or we will experience
12166 surprises wrt scrolling. */
12167 if (frame_garbaged)
12168 clear_garbaged_frames ();
12169 }
12170
12171
12172 /* If showing the region, and mark has changed, we must redisplay
12173 the whole window. The assignment to this_line_start_pos prevents
12174 the optimization directly below this if-statement. */
12175 if (((!NILP (Vtransient_mark_mode)
12176 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12177 != !NILP (w->region_showing))
12178 || (!NILP (w->region_showing)
12179 && !EQ (w->region_showing,
12180 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12181 CHARPOS (this_line_start_pos) = 0;
12182
12183 /* Optimize the case that only the line containing the cursor in the
12184 selected window has changed. Variables starting with this_ are
12185 set in display_line and record information about the line
12186 containing the cursor. */
12187 tlbufpos = this_line_start_pos;
12188 tlendpos = this_line_end_pos;
12189 if (!consider_all_windows_p
12190 && CHARPOS (tlbufpos) > 0
12191 && NILP (w->update_mode_line)
12192 && !current_buffer->clip_changed
12193 && !current_buffer->prevent_redisplay_optimizations_p
12194 && FRAME_VISIBLE_P (XFRAME (w->frame))
12195 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12196 /* Make sure recorded data applies to current buffer, etc. */
12197 && this_line_buffer == current_buffer
12198 && current_buffer == XBUFFER (w->buffer)
12199 && NILP (w->force_start)
12200 && NILP (w->optional_new_start)
12201 /* Point must be on the line that we have info recorded about. */
12202 && PT >= CHARPOS (tlbufpos)
12203 && PT <= Z - CHARPOS (tlendpos)
12204 /* All text outside that line, including its final newline,
12205 must be unchanged. */
12206 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12207 CHARPOS (tlendpos)))
12208 {
12209 if (CHARPOS (tlbufpos) > BEGV
12210 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12211 && (CHARPOS (tlbufpos) == ZV
12212 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12213 /* Former continuation line has disappeared by becoming empty. */
12214 goto cancel;
12215 else if (XFASTINT (w->last_modified) < MODIFF
12216 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12217 || MINI_WINDOW_P (w))
12218 {
12219 /* We have to handle the case of continuation around a
12220 wide-column character (see the comment in indent.c around
12221 line 1340).
12222
12223 For instance, in the following case:
12224
12225 -------- Insert --------
12226 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12227 J_I_ ==> J_I_ `^^' are cursors.
12228 ^^ ^^
12229 -------- --------
12230
12231 As we have to redraw the line above, we cannot use this
12232 optimization. */
12233
12234 struct it it;
12235 int line_height_before = this_line_pixel_height;
12236
12237 /* Note that start_display will handle the case that the
12238 line starting at tlbufpos is a continuation line. */
12239 start_display (&it, w, tlbufpos);
12240
12241 /* Implementation note: It this still necessary? */
12242 if (it.current_x != this_line_start_x)
12243 goto cancel;
12244
12245 TRACE ((stderr, "trying display optimization 1\n"));
12246 w->cursor.vpos = -1;
12247 overlay_arrow_seen = 0;
12248 it.vpos = this_line_vpos;
12249 it.current_y = this_line_y;
12250 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12251 display_line (&it);
12252
12253 /* If line contains point, is not continued,
12254 and ends at same distance from eob as before, we win. */
12255 if (w->cursor.vpos >= 0
12256 /* Line is not continued, otherwise this_line_start_pos
12257 would have been set to 0 in display_line. */
12258 && CHARPOS (this_line_start_pos)
12259 /* Line ends as before. */
12260 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12261 /* Line has same height as before. Otherwise other lines
12262 would have to be shifted up or down. */
12263 && this_line_pixel_height == line_height_before)
12264 {
12265 /* If this is not the window's last line, we must adjust
12266 the charstarts of the lines below. */
12267 if (it.current_y < it.last_visible_y)
12268 {
12269 struct glyph_row *row
12270 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12271 EMACS_INT delta, delta_bytes;
12272
12273 /* We used to distinguish between two cases here,
12274 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12275 when the line ends in a newline or the end of the
12276 buffer's accessible portion. But both cases did
12277 the same, so they were collapsed. */
12278 delta = (Z
12279 - CHARPOS (tlendpos)
12280 - MATRIX_ROW_START_CHARPOS (row));
12281 delta_bytes = (Z_BYTE
12282 - BYTEPOS (tlendpos)
12283 - MATRIX_ROW_START_BYTEPOS (row));
12284
12285 increment_matrix_positions (w->current_matrix,
12286 this_line_vpos + 1,
12287 w->current_matrix->nrows,
12288 delta, delta_bytes);
12289 }
12290
12291 /* If this row displays text now but previously didn't,
12292 or vice versa, w->window_end_vpos may have to be
12293 adjusted. */
12294 if ((it.glyph_row - 1)->displays_text_p)
12295 {
12296 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12297 XSETINT (w->window_end_vpos, this_line_vpos);
12298 }
12299 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12300 && this_line_vpos > 0)
12301 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12302 w->window_end_valid = Qnil;
12303
12304 /* Update hint: No need to try to scroll in update_window. */
12305 w->desired_matrix->no_scrolling_p = 1;
12306
12307 #if GLYPH_DEBUG
12308 *w->desired_matrix->method = 0;
12309 debug_method_add (w, "optimization 1");
12310 #endif
12311 #ifdef HAVE_WINDOW_SYSTEM
12312 update_window_fringes (w, 0);
12313 #endif
12314 goto update;
12315 }
12316 else
12317 goto cancel;
12318 }
12319 else if (/* Cursor position hasn't changed. */
12320 PT == XFASTINT (w->last_point)
12321 /* Make sure the cursor was last displayed
12322 in this window. Otherwise we have to reposition it. */
12323 && 0 <= w->cursor.vpos
12324 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12325 {
12326 if (!must_finish)
12327 {
12328 do_pending_window_change (1);
12329 /* If selected_window changed, redisplay again. */
12330 if (WINDOWP (selected_window)
12331 && (w = XWINDOW (selected_window)) != sw)
12332 goto retry;
12333
12334 /* We used to always goto end_of_redisplay here, but this
12335 isn't enough if we have a blinking cursor. */
12336 if (w->cursor_off_p == w->last_cursor_off_p)
12337 goto end_of_redisplay;
12338 }
12339 goto update;
12340 }
12341 /* If highlighting the region, or if the cursor is in the echo area,
12342 then we can't just move the cursor. */
12343 else if (! (!NILP (Vtransient_mark_mode)
12344 && !NILP (BVAR (current_buffer, mark_active)))
12345 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12346 || highlight_nonselected_windows)
12347 && NILP (w->region_showing)
12348 && NILP (Vshow_trailing_whitespace)
12349 && !cursor_in_echo_area)
12350 {
12351 struct it it;
12352 struct glyph_row *row;
12353
12354 /* Skip from tlbufpos to PT and see where it is. Note that
12355 PT may be in invisible text. If so, we will end at the
12356 next visible position. */
12357 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12358 NULL, DEFAULT_FACE_ID);
12359 it.current_x = this_line_start_x;
12360 it.current_y = this_line_y;
12361 it.vpos = this_line_vpos;
12362
12363 /* The call to move_it_to stops in front of PT, but
12364 moves over before-strings. */
12365 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12366
12367 if (it.vpos == this_line_vpos
12368 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12369 row->enabled_p))
12370 {
12371 xassert (this_line_vpos == it.vpos);
12372 xassert (this_line_y == it.current_y);
12373 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12374 #if GLYPH_DEBUG
12375 *w->desired_matrix->method = 0;
12376 debug_method_add (w, "optimization 3");
12377 #endif
12378 goto update;
12379 }
12380 else
12381 goto cancel;
12382 }
12383
12384 cancel:
12385 /* Text changed drastically or point moved off of line. */
12386 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12387 }
12388
12389 CHARPOS (this_line_start_pos) = 0;
12390 consider_all_windows_p |= buffer_shared > 1;
12391 ++clear_face_cache_count;
12392 #ifdef HAVE_WINDOW_SYSTEM
12393 ++clear_image_cache_count;
12394 #endif
12395
12396 /* Build desired matrices, and update the display. If
12397 consider_all_windows_p is non-zero, do it for all windows on all
12398 frames. Otherwise do it for selected_window, only. */
12399
12400 if (consider_all_windows_p)
12401 {
12402 Lisp_Object tail, frame;
12403
12404 FOR_EACH_FRAME (tail, frame)
12405 XFRAME (frame)->updated_p = 0;
12406
12407 /* Recompute # windows showing selected buffer. This will be
12408 incremented each time such a window is displayed. */
12409 buffer_shared = 0;
12410
12411 FOR_EACH_FRAME (tail, frame)
12412 {
12413 struct frame *f = XFRAME (frame);
12414
12415 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12416 {
12417 if (! EQ (frame, selected_frame))
12418 /* Select the frame, for the sake of frame-local
12419 variables. */
12420 select_frame_for_redisplay (frame);
12421
12422 /* Mark all the scroll bars to be removed; we'll redeem
12423 the ones we want when we redisplay their windows. */
12424 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12425 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12426
12427 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12428 redisplay_windows (FRAME_ROOT_WINDOW (f));
12429
12430 /* The X error handler may have deleted that frame. */
12431 if (!FRAME_LIVE_P (f))
12432 continue;
12433
12434 /* Any scroll bars which redisplay_windows should have
12435 nuked should now go away. */
12436 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12437 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12438
12439 /* If fonts changed, display again. */
12440 /* ??? rms: I suspect it is a mistake to jump all the way
12441 back to retry here. It should just retry this frame. */
12442 if (fonts_changed_p)
12443 goto retry;
12444
12445 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12446 {
12447 /* See if we have to hscroll. */
12448 if (!f->already_hscrolled_p)
12449 {
12450 f->already_hscrolled_p = 1;
12451 if (hscroll_windows (f->root_window))
12452 goto retry;
12453 }
12454
12455 /* Prevent various kinds of signals during display
12456 update. stdio is not robust about handling
12457 signals, which can cause an apparent I/O
12458 error. */
12459 if (interrupt_input)
12460 unrequest_sigio ();
12461 STOP_POLLING;
12462
12463 /* Update the display. */
12464 set_window_update_flags (XWINDOW (f->root_window), 1);
12465 pending |= update_frame (f, 0, 0);
12466 f->updated_p = 1;
12467 }
12468 }
12469 }
12470
12471 if (!EQ (old_frame, selected_frame)
12472 && FRAME_LIVE_P (XFRAME (old_frame)))
12473 /* We played a bit fast-and-loose above and allowed selected_frame
12474 and selected_window to be temporarily out-of-sync but let's make
12475 sure this stays contained. */
12476 select_frame_for_redisplay (old_frame);
12477 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12478
12479 if (!pending)
12480 {
12481 /* Do the mark_window_display_accurate after all windows have
12482 been redisplayed because this call resets flags in buffers
12483 which are needed for proper redisplay. */
12484 FOR_EACH_FRAME (tail, frame)
12485 {
12486 struct frame *f = XFRAME (frame);
12487 if (f->updated_p)
12488 {
12489 mark_window_display_accurate (f->root_window, 1);
12490 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12491 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12492 }
12493 }
12494 }
12495 }
12496 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12497 {
12498 Lisp_Object mini_window;
12499 struct frame *mini_frame;
12500
12501 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12502 /* Use list_of_error, not Qerror, so that
12503 we catch only errors and don't run the debugger. */
12504 internal_condition_case_1 (redisplay_window_1, selected_window,
12505 list_of_error,
12506 redisplay_window_error);
12507
12508 /* Compare desired and current matrices, perform output. */
12509
12510 update:
12511 /* If fonts changed, display again. */
12512 if (fonts_changed_p)
12513 goto retry;
12514
12515 /* Prevent various kinds of signals during display update.
12516 stdio is not robust about handling signals,
12517 which can cause an apparent I/O error. */
12518 if (interrupt_input)
12519 unrequest_sigio ();
12520 STOP_POLLING;
12521
12522 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12523 {
12524 if (hscroll_windows (selected_window))
12525 goto retry;
12526
12527 XWINDOW (selected_window)->must_be_updated_p = 1;
12528 pending = update_frame (sf, 0, 0);
12529 }
12530
12531 /* We may have called echo_area_display at the top of this
12532 function. If the echo area is on another frame, that may
12533 have put text on a frame other than the selected one, so the
12534 above call to update_frame would not have caught it. Catch
12535 it here. */
12536 mini_window = FRAME_MINIBUF_WINDOW (sf);
12537 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12538
12539 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12540 {
12541 XWINDOW (mini_window)->must_be_updated_p = 1;
12542 pending |= update_frame (mini_frame, 0, 0);
12543 if (!pending && hscroll_windows (mini_window))
12544 goto retry;
12545 }
12546 }
12547
12548 /* If display was paused because of pending input, make sure we do a
12549 thorough update the next time. */
12550 if (pending)
12551 {
12552 /* Prevent the optimization at the beginning of
12553 redisplay_internal that tries a single-line update of the
12554 line containing the cursor in the selected window. */
12555 CHARPOS (this_line_start_pos) = 0;
12556
12557 /* Let the overlay arrow be updated the next time. */
12558 update_overlay_arrows (0);
12559
12560 /* If we pause after scrolling, some rows in the current
12561 matrices of some windows are not valid. */
12562 if (!WINDOW_FULL_WIDTH_P (w)
12563 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12564 update_mode_lines = 1;
12565 }
12566 else
12567 {
12568 if (!consider_all_windows_p)
12569 {
12570 /* This has already been done above if
12571 consider_all_windows_p is set. */
12572 mark_window_display_accurate_1 (w, 1);
12573
12574 /* Say overlay arrows are up to date. */
12575 update_overlay_arrows (1);
12576
12577 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12578 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12579 }
12580
12581 update_mode_lines = 0;
12582 windows_or_buffers_changed = 0;
12583 cursor_type_changed = 0;
12584 }
12585
12586 /* Start SIGIO interrupts coming again. Having them off during the
12587 code above makes it less likely one will discard output, but not
12588 impossible, since there might be stuff in the system buffer here.
12589 But it is much hairier to try to do anything about that. */
12590 if (interrupt_input)
12591 request_sigio ();
12592 RESUME_POLLING;
12593
12594 /* If a frame has become visible which was not before, redisplay
12595 again, so that we display it. Expose events for such a frame
12596 (which it gets when becoming visible) don't call the parts of
12597 redisplay constructing glyphs, so simply exposing a frame won't
12598 display anything in this case. So, we have to display these
12599 frames here explicitly. */
12600 if (!pending)
12601 {
12602 Lisp_Object tail, frame;
12603 int new_count = 0;
12604
12605 FOR_EACH_FRAME (tail, frame)
12606 {
12607 int this_is_visible = 0;
12608
12609 if (XFRAME (frame)->visible)
12610 this_is_visible = 1;
12611 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12612 if (XFRAME (frame)->visible)
12613 this_is_visible = 1;
12614
12615 if (this_is_visible)
12616 new_count++;
12617 }
12618
12619 if (new_count != number_of_visible_frames)
12620 windows_or_buffers_changed++;
12621 }
12622
12623 /* Change frame size now if a change is pending. */
12624 do_pending_window_change (1);
12625
12626 /* If we just did a pending size change, or have additional
12627 visible frames, or selected_window changed, redisplay again. */
12628 if ((windows_or_buffers_changed && !pending)
12629 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12630 goto retry;
12631
12632 /* Clear the face and image caches.
12633
12634 We used to do this only if consider_all_windows_p. But the cache
12635 needs to be cleared if a timer creates images in the current
12636 buffer (e.g. the test case in Bug#6230). */
12637
12638 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12639 {
12640 clear_face_cache (0);
12641 clear_face_cache_count = 0;
12642 }
12643
12644 #ifdef HAVE_WINDOW_SYSTEM
12645 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12646 {
12647 clear_image_caches (Qnil);
12648 clear_image_cache_count = 0;
12649 }
12650 #endif /* HAVE_WINDOW_SYSTEM */
12651
12652 end_of_redisplay:
12653 unbind_to (count, Qnil);
12654 RESUME_POLLING;
12655 }
12656
12657
12658 /* Redisplay, but leave alone any recent echo area message unless
12659 another message has been requested in its place.
12660
12661 This is useful in situations where you need to redisplay but no
12662 user action has occurred, making it inappropriate for the message
12663 area to be cleared. See tracking_off and
12664 wait_reading_process_output for examples of these situations.
12665
12666 FROM_WHERE is an integer saying from where this function was
12667 called. This is useful for debugging. */
12668
12669 void
12670 redisplay_preserve_echo_area (int from_where)
12671 {
12672 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12673
12674 if (!NILP (echo_area_buffer[1]))
12675 {
12676 /* We have a previously displayed message, but no current
12677 message. Redisplay the previous message. */
12678 display_last_displayed_message_p = 1;
12679 redisplay_internal ();
12680 display_last_displayed_message_p = 0;
12681 }
12682 else
12683 redisplay_internal ();
12684
12685 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12686 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12687 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12688 }
12689
12690
12691 /* Function registered with record_unwind_protect in
12692 redisplay_internal. Reset redisplaying_p to the value it had
12693 before redisplay_internal was called, and clear
12694 prevent_freeing_realized_faces_p. It also selects the previously
12695 selected frame, unless it has been deleted (by an X connection
12696 failure during redisplay, for example). */
12697
12698 static Lisp_Object
12699 unwind_redisplay (Lisp_Object val)
12700 {
12701 Lisp_Object old_redisplaying_p, old_frame;
12702
12703 old_redisplaying_p = XCAR (val);
12704 redisplaying_p = XFASTINT (old_redisplaying_p);
12705 old_frame = XCDR (val);
12706 if (! EQ (old_frame, selected_frame)
12707 && FRAME_LIVE_P (XFRAME (old_frame)))
12708 select_frame_for_redisplay (old_frame);
12709 return Qnil;
12710 }
12711
12712
12713 /* Mark the display of window W as accurate or inaccurate. If
12714 ACCURATE_P is non-zero mark display of W as accurate. If
12715 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12716 redisplay_internal is called. */
12717
12718 static void
12719 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12720 {
12721 if (BUFFERP (w->buffer))
12722 {
12723 struct buffer *b = XBUFFER (w->buffer);
12724
12725 w->last_modified
12726 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12727 w->last_overlay_modified
12728 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12729 w->last_had_star
12730 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12731
12732 if (accurate_p)
12733 {
12734 b->clip_changed = 0;
12735 b->prevent_redisplay_optimizations_p = 0;
12736
12737 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12738 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12739 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12740 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12741
12742 w->current_matrix->buffer = b;
12743 w->current_matrix->begv = BUF_BEGV (b);
12744 w->current_matrix->zv = BUF_ZV (b);
12745
12746 w->last_cursor = w->cursor;
12747 w->last_cursor_off_p = w->cursor_off_p;
12748
12749 if (w == XWINDOW (selected_window))
12750 w->last_point = make_number (BUF_PT (b));
12751 else
12752 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12753 }
12754 }
12755
12756 if (accurate_p)
12757 {
12758 w->window_end_valid = w->buffer;
12759 w->update_mode_line = Qnil;
12760 }
12761 }
12762
12763
12764 /* Mark the display of windows in the window tree rooted at WINDOW as
12765 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12766 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12767 be redisplayed the next time redisplay_internal is called. */
12768
12769 void
12770 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12771 {
12772 struct window *w;
12773
12774 for (; !NILP (window); window = w->next)
12775 {
12776 w = XWINDOW (window);
12777 mark_window_display_accurate_1 (w, accurate_p);
12778
12779 if (!NILP (w->vchild))
12780 mark_window_display_accurate (w->vchild, accurate_p);
12781 if (!NILP (w->hchild))
12782 mark_window_display_accurate (w->hchild, accurate_p);
12783 }
12784
12785 if (accurate_p)
12786 {
12787 update_overlay_arrows (1);
12788 }
12789 else
12790 {
12791 /* Force a thorough redisplay the next time by setting
12792 last_arrow_position and last_arrow_string to t, which is
12793 unequal to any useful value of Voverlay_arrow_... */
12794 update_overlay_arrows (-1);
12795 }
12796 }
12797
12798
12799 /* Return value in display table DP (Lisp_Char_Table *) for character
12800 C. Since a display table doesn't have any parent, we don't have to
12801 follow parent. Do not call this function directly but use the
12802 macro DISP_CHAR_VECTOR. */
12803
12804 Lisp_Object
12805 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12806 {
12807 Lisp_Object val;
12808
12809 if (ASCII_CHAR_P (c))
12810 {
12811 val = dp->ascii;
12812 if (SUB_CHAR_TABLE_P (val))
12813 val = XSUB_CHAR_TABLE (val)->contents[c];
12814 }
12815 else
12816 {
12817 Lisp_Object table;
12818
12819 XSETCHAR_TABLE (table, dp);
12820 val = char_table_ref (table, c);
12821 }
12822 if (NILP (val))
12823 val = dp->defalt;
12824 return val;
12825 }
12826
12827
12828 \f
12829 /***********************************************************************
12830 Window Redisplay
12831 ***********************************************************************/
12832
12833 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12834
12835 static void
12836 redisplay_windows (Lisp_Object window)
12837 {
12838 while (!NILP (window))
12839 {
12840 struct window *w = XWINDOW (window);
12841
12842 if (!NILP (w->hchild))
12843 redisplay_windows (w->hchild);
12844 else if (!NILP (w->vchild))
12845 redisplay_windows (w->vchild);
12846 else if (!NILP (w->buffer))
12847 {
12848 displayed_buffer = XBUFFER (w->buffer);
12849 /* Use list_of_error, not Qerror, so that
12850 we catch only errors and don't run the debugger. */
12851 internal_condition_case_1 (redisplay_window_0, window,
12852 list_of_error,
12853 redisplay_window_error);
12854 }
12855
12856 window = w->next;
12857 }
12858 }
12859
12860 static Lisp_Object
12861 redisplay_window_error (Lisp_Object ignore)
12862 {
12863 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12864 return Qnil;
12865 }
12866
12867 static Lisp_Object
12868 redisplay_window_0 (Lisp_Object window)
12869 {
12870 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12871 redisplay_window (window, 0);
12872 return Qnil;
12873 }
12874
12875 static Lisp_Object
12876 redisplay_window_1 (Lisp_Object window)
12877 {
12878 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12879 redisplay_window (window, 1);
12880 return Qnil;
12881 }
12882 \f
12883
12884 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12885 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12886 which positions recorded in ROW differ from current buffer
12887 positions.
12888
12889 Return 0 if cursor is not on this row, 1 otherwise. */
12890
12891 static int
12892 set_cursor_from_row (struct window *w, struct glyph_row *row,
12893 struct glyph_matrix *matrix,
12894 EMACS_INT delta, EMACS_INT delta_bytes,
12895 int dy, int dvpos)
12896 {
12897 struct glyph *glyph = row->glyphs[TEXT_AREA];
12898 struct glyph *end = glyph + row->used[TEXT_AREA];
12899 struct glyph *cursor = NULL;
12900 /* The last known character position in row. */
12901 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12902 int x = row->x;
12903 EMACS_INT pt_old = PT - delta;
12904 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12905 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12906 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12907 /* A glyph beyond the edge of TEXT_AREA which we should never
12908 touch. */
12909 struct glyph *glyphs_end = end;
12910 /* Non-zero means we've found a match for cursor position, but that
12911 glyph has the avoid_cursor_p flag set. */
12912 int match_with_avoid_cursor = 0;
12913 /* Non-zero means we've seen at least one glyph that came from a
12914 display string. */
12915 int string_seen = 0;
12916 /* Largest and smalles buffer positions seen so far during scan of
12917 glyph row. */
12918 EMACS_INT bpos_max = pos_before;
12919 EMACS_INT bpos_min = pos_after;
12920 /* Last buffer position covered by an overlay string with an integer
12921 `cursor' property. */
12922 EMACS_INT bpos_covered = 0;
12923
12924 /* Skip over glyphs not having an object at the start and the end of
12925 the row. These are special glyphs like truncation marks on
12926 terminal frames. */
12927 if (row->displays_text_p)
12928 {
12929 if (!row->reversed_p)
12930 {
12931 while (glyph < end
12932 && INTEGERP (glyph->object)
12933 && glyph->charpos < 0)
12934 {
12935 x += glyph->pixel_width;
12936 ++glyph;
12937 }
12938 while (end > glyph
12939 && INTEGERP ((end - 1)->object)
12940 /* CHARPOS is zero for blanks and stretch glyphs
12941 inserted by extend_face_to_end_of_line. */
12942 && (end - 1)->charpos <= 0)
12943 --end;
12944 glyph_before = glyph - 1;
12945 glyph_after = end;
12946 }
12947 else
12948 {
12949 struct glyph *g;
12950
12951 /* If the glyph row is reversed, we need to process it from back
12952 to front, so swap the edge pointers. */
12953 glyphs_end = end = glyph - 1;
12954 glyph += row->used[TEXT_AREA] - 1;
12955
12956 while (glyph > end + 1
12957 && INTEGERP (glyph->object)
12958 && glyph->charpos < 0)
12959 {
12960 --glyph;
12961 x -= glyph->pixel_width;
12962 }
12963 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12964 --glyph;
12965 /* By default, in reversed rows we put the cursor on the
12966 rightmost (first in the reading order) glyph. */
12967 for (g = end + 1; g < glyph; g++)
12968 x += g->pixel_width;
12969 while (end < glyph
12970 && INTEGERP ((end + 1)->object)
12971 && (end + 1)->charpos <= 0)
12972 ++end;
12973 glyph_before = glyph + 1;
12974 glyph_after = end;
12975 }
12976 }
12977 else if (row->reversed_p)
12978 {
12979 /* In R2L rows that don't display text, put the cursor on the
12980 rightmost glyph. Case in point: an empty last line that is
12981 part of an R2L paragraph. */
12982 cursor = end - 1;
12983 /* Avoid placing the cursor on the last glyph of the row, where
12984 on terminal frames we hold the vertical border between
12985 adjacent windows. */
12986 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12987 && !WINDOW_RIGHTMOST_P (w)
12988 && cursor == row->glyphs[LAST_AREA] - 1)
12989 cursor--;
12990 x = -1; /* will be computed below, at label compute_x */
12991 }
12992
12993 /* Step 1: Try to find the glyph whose character position
12994 corresponds to point. If that's not possible, find 2 glyphs
12995 whose character positions are the closest to point, one before
12996 point, the other after it. */
12997 if (!row->reversed_p)
12998 while (/* not marched to end of glyph row */
12999 glyph < end
13000 /* glyph was not inserted by redisplay for internal purposes */
13001 && !INTEGERP (glyph->object))
13002 {
13003 if (BUFFERP (glyph->object))
13004 {
13005 EMACS_INT dpos = glyph->charpos - pt_old;
13006
13007 if (glyph->charpos > bpos_max)
13008 bpos_max = glyph->charpos;
13009 if (glyph->charpos < bpos_min)
13010 bpos_min = glyph->charpos;
13011 if (!glyph->avoid_cursor_p)
13012 {
13013 /* If we hit point, we've found the glyph on which to
13014 display the cursor. */
13015 if (dpos == 0)
13016 {
13017 match_with_avoid_cursor = 0;
13018 break;
13019 }
13020 /* See if we've found a better approximation to
13021 POS_BEFORE or to POS_AFTER. Note that we want the
13022 first (leftmost) glyph of all those that are the
13023 closest from below, and the last (rightmost) of all
13024 those from above. */
13025 if (0 > dpos && dpos > pos_before - pt_old)
13026 {
13027 pos_before = glyph->charpos;
13028 glyph_before = glyph;
13029 }
13030 else if (0 < dpos && dpos <= pos_after - pt_old)
13031 {
13032 pos_after = glyph->charpos;
13033 glyph_after = glyph;
13034 }
13035 }
13036 else if (dpos == 0)
13037 match_with_avoid_cursor = 1;
13038 }
13039 else if (STRINGP (glyph->object))
13040 {
13041 Lisp_Object chprop;
13042 EMACS_INT glyph_pos = glyph->charpos;
13043
13044 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13045 glyph->object);
13046 if (INTEGERP (chprop))
13047 {
13048 bpos_covered = bpos_max + XINT (chprop);
13049 /* If the `cursor' property covers buffer positions up
13050 to and including point, we should display cursor on
13051 this glyph. Note that overlays and text properties
13052 with string values stop bidi reordering, so every
13053 buffer position to the left of the string is always
13054 smaller than any position to the right of the
13055 string. Therefore, if a `cursor' property on one
13056 of the string's characters has an integer value, we
13057 will break out of the loop below _before_ we get to
13058 the position match above. IOW, integer values of
13059 the `cursor' property override the "exact match for
13060 point" strategy of positioning the cursor. */
13061 /* Implementation note: bpos_max == pt_old when, e.g.,
13062 we are in an empty line, where bpos_max is set to
13063 MATRIX_ROW_START_CHARPOS, see above. */
13064 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13065 {
13066 cursor = glyph;
13067 break;
13068 }
13069 }
13070
13071 string_seen = 1;
13072 }
13073 x += glyph->pixel_width;
13074 ++glyph;
13075 }
13076 else if (glyph > end) /* row is reversed */
13077 while (!INTEGERP (glyph->object))
13078 {
13079 if (BUFFERP (glyph->object))
13080 {
13081 EMACS_INT dpos = glyph->charpos - pt_old;
13082
13083 if (glyph->charpos > bpos_max)
13084 bpos_max = glyph->charpos;
13085 if (glyph->charpos < bpos_min)
13086 bpos_min = glyph->charpos;
13087 if (!glyph->avoid_cursor_p)
13088 {
13089 if (dpos == 0)
13090 {
13091 match_with_avoid_cursor = 0;
13092 break;
13093 }
13094 if (0 > dpos && dpos > pos_before - pt_old)
13095 {
13096 pos_before = glyph->charpos;
13097 glyph_before = glyph;
13098 }
13099 else if (0 < dpos && dpos <= pos_after - pt_old)
13100 {
13101 pos_after = glyph->charpos;
13102 glyph_after = glyph;
13103 }
13104 }
13105 else if (dpos == 0)
13106 match_with_avoid_cursor = 1;
13107 }
13108 else if (STRINGP (glyph->object))
13109 {
13110 Lisp_Object chprop;
13111 EMACS_INT glyph_pos = glyph->charpos;
13112
13113 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13114 glyph->object);
13115 if (INTEGERP (chprop))
13116 {
13117 bpos_covered = bpos_max + XINT (chprop);
13118 /* If the `cursor' property covers buffer positions up
13119 to and including point, we should display cursor on
13120 this glyph. */
13121 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13122 {
13123 cursor = glyph;
13124 break;
13125 }
13126 }
13127 string_seen = 1;
13128 }
13129 --glyph;
13130 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13131 {
13132 x--; /* can't use any pixel_width */
13133 break;
13134 }
13135 x -= glyph->pixel_width;
13136 }
13137
13138 /* Step 2: If we didn't find an exact match for point, we need to
13139 look for a proper place to put the cursor among glyphs between
13140 GLYPH_BEFORE and GLYPH_AFTER. */
13141 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13142 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13143 && bpos_covered < pt_old)
13144 {
13145 /* An empty line has a single glyph whose OBJECT is zero and
13146 whose CHARPOS is the position of a newline on that line.
13147 Note that on a TTY, there are more glyphs after that, which
13148 were produced by extend_face_to_end_of_line, but their
13149 CHARPOS is zero or negative. */
13150 int empty_line_p =
13151 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13152 && INTEGERP (glyph->object) && glyph->charpos > 0;
13153
13154 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13155 {
13156 EMACS_INT ellipsis_pos;
13157
13158 /* Scan back over the ellipsis glyphs. */
13159 if (!row->reversed_p)
13160 {
13161 ellipsis_pos = (glyph - 1)->charpos;
13162 while (glyph > row->glyphs[TEXT_AREA]
13163 && (glyph - 1)->charpos == ellipsis_pos)
13164 glyph--, x -= glyph->pixel_width;
13165 /* That loop always goes one position too far, including
13166 the glyph before the ellipsis. So scan forward over
13167 that one. */
13168 x += glyph->pixel_width;
13169 glyph++;
13170 }
13171 else /* row is reversed */
13172 {
13173 ellipsis_pos = (glyph + 1)->charpos;
13174 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13175 && (glyph + 1)->charpos == ellipsis_pos)
13176 glyph++, x += glyph->pixel_width;
13177 x -= glyph->pixel_width;
13178 glyph--;
13179 }
13180 }
13181 else if (match_with_avoid_cursor
13182 /* A truncated row may not include PT among its
13183 character positions. Setting the cursor inside the
13184 scroll margin will trigger recalculation of hscroll
13185 in hscroll_window_tree. */
13186 || (row->truncated_on_left_p && pt_old < bpos_min)
13187 || (row->truncated_on_right_p && pt_old > bpos_max)
13188 /* Zero-width characters produce no glyphs. */
13189 || (!string_seen
13190 && !empty_line_p
13191 && (row->reversed_p
13192 ? glyph_after > glyphs_end
13193 : glyph_after < glyphs_end)))
13194 {
13195 cursor = glyph_after;
13196 x = -1;
13197 }
13198 else if (string_seen)
13199 {
13200 int incr = row->reversed_p ? -1 : +1;
13201
13202 /* Need to find the glyph that came out of a string which is
13203 present at point. That glyph is somewhere between
13204 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13205 positioned between POS_BEFORE and POS_AFTER in the
13206 buffer. */
13207 struct glyph *start, *stop;
13208 EMACS_INT pos = pos_before;
13209
13210 x = -1;
13211
13212 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13213 correspond to POS_BEFORE and POS_AFTER, respectively. We
13214 need START and STOP in the order that corresponds to the
13215 row's direction as given by its reversed_p flag. If the
13216 directionality of characters between POS_BEFORE and
13217 POS_AFTER is the opposite of the row's base direction,
13218 these characters will have been reordered for display,
13219 and we need to reverse START and STOP. */
13220 if (!row->reversed_p)
13221 {
13222 start = min (glyph_before, glyph_after);
13223 stop = max (glyph_before, glyph_after);
13224 }
13225 else
13226 {
13227 start = max (glyph_before, glyph_after);
13228 stop = min (glyph_before, glyph_after);
13229 }
13230 for (glyph = start + incr;
13231 row->reversed_p ? glyph > stop : glyph < stop; )
13232 {
13233
13234 /* Any glyphs that come from the buffer are here because
13235 of bidi reordering. Skip them, and only pay
13236 attention to glyphs that came from some string. */
13237 if (STRINGP (glyph->object))
13238 {
13239 Lisp_Object str;
13240 EMACS_INT tem;
13241
13242 str = glyph->object;
13243 tem = string_buffer_position_lim (str, pos, pos_after, 0);
13244 if (tem == 0 /* from overlay */
13245 || pos <= tem)
13246 {
13247 /* If the string from which this glyph came is
13248 found in the buffer at point, then we've
13249 found the glyph we've been looking for. If
13250 it comes from an overlay (tem == 0), and it
13251 has the `cursor' property on one of its
13252 glyphs, record that glyph as a candidate for
13253 displaying the cursor. (As in the
13254 unidirectional version, we will display the
13255 cursor on the last candidate we find.) */
13256 if (tem == 0 || tem == pt_old)
13257 {
13258 /* The glyphs from this string could have
13259 been reordered. Find the one with the
13260 smallest string position. Or there could
13261 be a character in the string with the
13262 `cursor' property, which means display
13263 cursor on that character's glyph. */
13264 EMACS_INT strpos = glyph->charpos;
13265
13266 if (tem)
13267 cursor = glyph;
13268 for ( ;
13269 (row->reversed_p ? glyph > stop : glyph < stop)
13270 && EQ (glyph->object, str);
13271 glyph += incr)
13272 {
13273 Lisp_Object cprop;
13274 EMACS_INT gpos = glyph->charpos;
13275
13276 cprop = Fget_char_property (make_number (gpos),
13277 Qcursor,
13278 glyph->object);
13279 if (!NILP (cprop))
13280 {
13281 cursor = glyph;
13282 break;
13283 }
13284 if (tem && glyph->charpos < strpos)
13285 {
13286 strpos = glyph->charpos;
13287 cursor = glyph;
13288 }
13289 }
13290
13291 if (tem == pt_old)
13292 goto compute_x;
13293 }
13294 if (tem)
13295 pos = tem + 1; /* don't find previous instances */
13296 }
13297 /* This string is not what we want; skip all of the
13298 glyphs that came from it. */
13299 while ((row->reversed_p ? glyph > stop : glyph < stop)
13300 && EQ (glyph->object, str))
13301 glyph += incr;
13302 }
13303 else
13304 glyph += incr;
13305 }
13306
13307 /* If we reached the end of the line, and END was from a string,
13308 the cursor is not on this line. */
13309 if (cursor == NULL
13310 && (row->reversed_p ? glyph <= end : glyph >= end)
13311 && STRINGP (end->object)
13312 && row->continued_p)
13313 return 0;
13314 }
13315 }
13316
13317 compute_x:
13318 if (cursor != NULL)
13319 glyph = cursor;
13320 if (x < 0)
13321 {
13322 struct glyph *g;
13323
13324 /* Need to compute x that corresponds to GLYPH. */
13325 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13326 {
13327 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13328 abort ();
13329 x += g->pixel_width;
13330 }
13331 }
13332
13333 /* ROW could be part of a continued line, which, under bidi
13334 reordering, might have other rows whose start and end charpos
13335 occlude point. Only set w->cursor if we found a better
13336 approximation to the cursor position than we have from previously
13337 examined candidate rows belonging to the same continued line. */
13338 if (/* we already have a candidate row */
13339 w->cursor.vpos >= 0
13340 /* that candidate is not the row we are processing */
13341 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13342 /* the row we are processing is part of a continued line */
13343 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13344 /* Make sure cursor.vpos specifies a row whose start and end
13345 charpos occlude point. This is because some callers of this
13346 function leave cursor.vpos at the row where the cursor was
13347 displayed during the last redisplay cycle. */
13348 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13349 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13350 {
13351 struct glyph *g1 =
13352 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13353
13354 /* Don't consider glyphs that are outside TEXT_AREA. */
13355 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13356 return 0;
13357 /* Keep the candidate whose buffer position is the closest to
13358 point. */
13359 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13360 w->cursor.hpos >= 0
13361 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13362 && BUFFERP (g1->object)
13363 && (g1->charpos == pt_old /* an exact match always wins */
13364 || (BUFFERP (glyph->object)
13365 && eabs (g1->charpos - pt_old)
13366 < eabs (glyph->charpos - pt_old))))
13367 return 0;
13368 /* If this candidate gives an exact match, use that. */
13369 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13370 /* Otherwise, keep the candidate that comes from a row
13371 spanning less buffer positions. This may win when one or
13372 both candidate positions are on glyphs that came from
13373 display strings, for which we cannot compare buffer
13374 positions. */
13375 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13376 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13377 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13378 return 0;
13379 }
13380 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13381 w->cursor.x = x;
13382 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13383 w->cursor.y = row->y + dy;
13384
13385 if (w == XWINDOW (selected_window))
13386 {
13387 if (!row->continued_p
13388 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13389 && row->x == 0)
13390 {
13391 this_line_buffer = XBUFFER (w->buffer);
13392
13393 CHARPOS (this_line_start_pos)
13394 = MATRIX_ROW_START_CHARPOS (row) + delta;
13395 BYTEPOS (this_line_start_pos)
13396 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13397
13398 CHARPOS (this_line_end_pos)
13399 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13400 BYTEPOS (this_line_end_pos)
13401 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13402
13403 this_line_y = w->cursor.y;
13404 this_line_pixel_height = row->height;
13405 this_line_vpos = w->cursor.vpos;
13406 this_line_start_x = row->x;
13407 }
13408 else
13409 CHARPOS (this_line_start_pos) = 0;
13410 }
13411
13412 return 1;
13413 }
13414
13415
13416 /* Run window scroll functions, if any, for WINDOW with new window
13417 start STARTP. Sets the window start of WINDOW to that position.
13418
13419 We assume that the window's buffer is really current. */
13420
13421 static INLINE struct text_pos
13422 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13423 {
13424 struct window *w = XWINDOW (window);
13425 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13426
13427 if (current_buffer != XBUFFER (w->buffer))
13428 abort ();
13429
13430 if (!NILP (Vwindow_scroll_functions))
13431 {
13432 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13433 make_number (CHARPOS (startp)));
13434 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13435 /* In case the hook functions switch buffers. */
13436 if (current_buffer != XBUFFER (w->buffer))
13437 set_buffer_internal_1 (XBUFFER (w->buffer));
13438 }
13439
13440 return startp;
13441 }
13442
13443
13444 /* Make sure the line containing the cursor is fully visible.
13445 A value of 1 means there is nothing to be done.
13446 (Either the line is fully visible, or it cannot be made so,
13447 or we cannot tell.)
13448
13449 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13450 is higher than window.
13451
13452 A value of 0 means the caller should do scrolling
13453 as if point had gone off the screen. */
13454
13455 static int
13456 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13457 {
13458 struct glyph_matrix *matrix;
13459 struct glyph_row *row;
13460 int window_height;
13461
13462 if (!make_cursor_line_fully_visible_p)
13463 return 1;
13464
13465 /* It's not always possible to find the cursor, e.g, when a window
13466 is full of overlay strings. Don't do anything in that case. */
13467 if (w->cursor.vpos < 0)
13468 return 1;
13469
13470 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13471 row = MATRIX_ROW (matrix, w->cursor.vpos);
13472
13473 /* If the cursor row is not partially visible, there's nothing to do. */
13474 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13475 return 1;
13476
13477 /* If the row the cursor is in is taller than the window's height,
13478 it's not clear what to do, so do nothing. */
13479 window_height = window_box_height (w);
13480 if (row->height >= window_height)
13481 {
13482 if (!force_p || MINI_WINDOW_P (w)
13483 || w->vscroll || w->cursor.vpos == 0)
13484 return 1;
13485 }
13486 return 0;
13487 }
13488
13489
13490 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13491 non-zero means only WINDOW is redisplayed in redisplay_internal.
13492 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13493 in redisplay_window to bring a partially visible line into view in
13494 the case that only the cursor has moved.
13495
13496 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13497 last screen line's vertical height extends past the end of the screen.
13498
13499 Value is
13500
13501 1 if scrolling succeeded
13502
13503 0 if scrolling didn't find point.
13504
13505 -1 if new fonts have been loaded so that we must interrupt
13506 redisplay, adjust glyph matrices, and try again. */
13507
13508 enum
13509 {
13510 SCROLLING_SUCCESS,
13511 SCROLLING_FAILED,
13512 SCROLLING_NEED_LARGER_MATRICES
13513 };
13514
13515 /* If scroll-conservatively is more than this, never recenter.
13516
13517 If you change this, don't forget to update the doc string of
13518 `scroll-conservatively' and the Emacs manual. */
13519 #define SCROLL_LIMIT 100
13520
13521 static int
13522 try_scrolling (Lisp_Object window, int just_this_one_p,
13523 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13524 int temp_scroll_step, int last_line_misfit)
13525 {
13526 struct window *w = XWINDOW (window);
13527 struct frame *f = XFRAME (w->frame);
13528 struct text_pos pos, startp;
13529 struct it it;
13530 int this_scroll_margin, scroll_max, rc, height;
13531 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13532 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13533 Lisp_Object aggressive;
13534 /* We will never try scrolling more than this number of lines. */
13535 int scroll_limit = SCROLL_LIMIT;
13536
13537 #if GLYPH_DEBUG
13538 debug_method_add (w, "try_scrolling");
13539 #endif
13540
13541 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13542
13543 /* Compute scroll margin height in pixels. We scroll when point is
13544 within this distance from the top or bottom of the window. */
13545 if (scroll_margin > 0)
13546 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13547 * FRAME_LINE_HEIGHT (f);
13548 else
13549 this_scroll_margin = 0;
13550
13551 /* Force arg_scroll_conservatively to have a reasonable value, to
13552 avoid scrolling too far away with slow move_it_* functions. Note
13553 that the user can supply scroll-conservatively equal to
13554 `most-positive-fixnum', which can be larger than INT_MAX. */
13555 if (arg_scroll_conservatively > scroll_limit)
13556 {
13557 arg_scroll_conservatively = scroll_limit + 1;
13558 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13559 }
13560 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13561 /* Compute how much we should try to scroll maximally to bring
13562 point into view. */
13563 scroll_max = (max (scroll_step,
13564 max (arg_scroll_conservatively, temp_scroll_step))
13565 * FRAME_LINE_HEIGHT (f));
13566 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13567 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13568 /* We're trying to scroll because of aggressive scrolling but no
13569 scroll_step is set. Choose an arbitrary one. */
13570 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13571 else
13572 scroll_max = 0;
13573
13574 too_near_end:
13575
13576 /* Decide whether to scroll down. */
13577 if (PT > CHARPOS (startp))
13578 {
13579 int scroll_margin_y;
13580
13581 /* Compute the pixel ypos of the scroll margin, then move it to
13582 either that ypos or PT, whichever comes first. */
13583 start_display (&it, w, startp);
13584 scroll_margin_y = it.last_visible_y - this_scroll_margin
13585 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13586 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13587 (MOVE_TO_POS | MOVE_TO_Y));
13588
13589 if (PT > CHARPOS (it.current.pos))
13590 {
13591 int y0 = line_bottom_y (&it);
13592 /* Compute how many pixels below window bottom to stop searching
13593 for PT. This avoids costly search for PT that is far away if
13594 the user limited scrolling by a small number of lines, but
13595 always finds PT if scroll_conservatively is set to a large
13596 number, such as most-positive-fixnum. */
13597 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13598 int y_to_move = it.last_visible_y + slack;
13599
13600 /* Compute the distance from the scroll margin to PT or to
13601 the scroll limit, whichever comes first. This should
13602 include the height of the cursor line, to make that line
13603 fully visible. */
13604 move_it_to (&it, PT, -1, y_to_move,
13605 -1, MOVE_TO_POS | MOVE_TO_Y);
13606 dy = line_bottom_y (&it) - y0;
13607
13608 if (dy > scroll_max)
13609 return SCROLLING_FAILED;
13610
13611 scroll_down_p = 1;
13612 }
13613 }
13614
13615 if (scroll_down_p)
13616 {
13617 /* Point is in or below the bottom scroll margin, so move the
13618 window start down. If scrolling conservatively, move it just
13619 enough down to make point visible. If scroll_step is set,
13620 move it down by scroll_step. */
13621 if (arg_scroll_conservatively)
13622 amount_to_scroll
13623 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13624 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13625 else if (scroll_step || temp_scroll_step)
13626 amount_to_scroll = scroll_max;
13627 else
13628 {
13629 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13630 height = WINDOW_BOX_TEXT_HEIGHT (w);
13631 if (NUMBERP (aggressive))
13632 {
13633 double float_amount = XFLOATINT (aggressive) * height;
13634 amount_to_scroll = float_amount;
13635 if (amount_to_scroll == 0 && float_amount > 0)
13636 amount_to_scroll = 1;
13637 /* Don't let point enter the scroll margin near top of
13638 the window. */
13639 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13640 amount_to_scroll = height - 2*this_scroll_margin + dy;
13641 }
13642 }
13643
13644 if (amount_to_scroll <= 0)
13645 return SCROLLING_FAILED;
13646
13647 start_display (&it, w, startp);
13648 if (arg_scroll_conservatively <= scroll_limit)
13649 move_it_vertically (&it, amount_to_scroll);
13650 else
13651 {
13652 /* Extra precision for users who set scroll-conservatively
13653 to a large number: make sure the amount we scroll
13654 the window start is never less than amount_to_scroll,
13655 which was computed as distance from window bottom to
13656 point. This matters when lines at window top and lines
13657 below window bottom have different height. */
13658 struct it it1 = it;
13659 /* We use a temporary it1 because line_bottom_y can modify
13660 its argument, if it moves one line down; see there. */
13661 int start_y = line_bottom_y (&it1);
13662
13663 do {
13664 move_it_by_lines (&it, 1);
13665 it1 = it;
13666 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13667 }
13668
13669 /* If STARTP is unchanged, move it down another screen line. */
13670 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13671 move_it_by_lines (&it, 1);
13672 startp = it.current.pos;
13673 }
13674 else
13675 {
13676 struct text_pos scroll_margin_pos = startp;
13677
13678 /* See if point is inside the scroll margin at the top of the
13679 window. */
13680 if (this_scroll_margin)
13681 {
13682 start_display (&it, w, startp);
13683 move_it_vertically (&it, this_scroll_margin);
13684 scroll_margin_pos = it.current.pos;
13685 }
13686
13687 if (PT < CHARPOS (scroll_margin_pos))
13688 {
13689 /* Point is in the scroll margin at the top of the window or
13690 above what is displayed in the window. */
13691 int y0, y_to_move;
13692
13693 /* Compute the vertical distance from PT to the scroll
13694 margin position. Move as far as scroll_max allows, or
13695 one screenful, or 10 screen lines, whichever is largest.
13696 Give up if distance is greater than scroll_max. */
13697 SET_TEXT_POS (pos, PT, PT_BYTE);
13698 start_display (&it, w, pos);
13699 y0 = it.current_y;
13700 y_to_move = max (it.last_visible_y,
13701 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13702 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13703 y_to_move, -1,
13704 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13705 dy = it.current_y - y0;
13706 if (dy > scroll_max)
13707 return SCROLLING_FAILED;
13708
13709 /* Compute new window start. */
13710 start_display (&it, w, startp);
13711
13712 if (arg_scroll_conservatively)
13713 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13714 max (scroll_step, temp_scroll_step));
13715 else if (scroll_step || temp_scroll_step)
13716 amount_to_scroll = scroll_max;
13717 else
13718 {
13719 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13720 height = WINDOW_BOX_TEXT_HEIGHT (w);
13721 if (NUMBERP (aggressive))
13722 {
13723 double float_amount = XFLOATINT (aggressive) * height;
13724 amount_to_scroll = float_amount;
13725 if (amount_to_scroll == 0 && float_amount > 0)
13726 amount_to_scroll = 1;
13727 amount_to_scroll -=
13728 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13729 /* Don't let point enter the scroll margin near
13730 bottom of the window. */
13731 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13732 amount_to_scroll = height - 2*this_scroll_margin + dy;
13733 }
13734 }
13735
13736 if (amount_to_scroll <= 0)
13737 return SCROLLING_FAILED;
13738
13739 move_it_vertically_backward (&it, amount_to_scroll);
13740 startp = it.current.pos;
13741 }
13742 }
13743
13744 /* Run window scroll functions. */
13745 startp = run_window_scroll_functions (window, startp);
13746
13747 /* Display the window. Give up if new fonts are loaded, or if point
13748 doesn't appear. */
13749 if (!try_window (window, startp, 0))
13750 rc = SCROLLING_NEED_LARGER_MATRICES;
13751 else if (w->cursor.vpos < 0)
13752 {
13753 clear_glyph_matrix (w->desired_matrix);
13754 rc = SCROLLING_FAILED;
13755 }
13756 else
13757 {
13758 /* Maybe forget recorded base line for line number display. */
13759 if (!just_this_one_p
13760 || current_buffer->clip_changed
13761 || BEG_UNCHANGED < CHARPOS (startp))
13762 w->base_line_number = Qnil;
13763
13764 /* If cursor ends up on a partially visible line,
13765 treat that as being off the bottom of the screen. */
13766 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13767 /* It's possible that the cursor is on the first line of the
13768 buffer, which is partially obscured due to a vscroll
13769 (Bug#7537). In that case, avoid looping forever . */
13770 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13771 {
13772 clear_glyph_matrix (w->desired_matrix);
13773 ++extra_scroll_margin_lines;
13774 goto too_near_end;
13775 }
13776 rc = SCROLLING_SUCCESS;
13777 }
13778
13779 return rc;
13780 }
13781
13782
13783 /* Compute a suitable window start for window W if display of W starts
13784 on a continuation line. Value is non-zero if a new window start
13785 was computed.
13786
13787 The new window start will be computed, based on W's width, starting
13788 from the start of the continued line. It is the start of the
13789 screen line with the minimum distance from the old start W->start. */
13790
13791 static int
13792 compute_window_start_on_continuation_line (struct window *w)
13793 {
13794 struct text_pos pos, start_pos;
13795 int window_start_changed_p = 0;
13796
13797 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13798
13799 /* If window start is on a continuation line... Window start may be
13800 < BEGV in case there's invisible text at the start of the
13801 buffer (M-x rmail, for example). */
13802 if (CHARPOS (start_pos) > BEGV
13803 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13804 {
13805 struct it it;
13806 struct glyph_row *row;
13807
13808 /* Handle the case that the window start is out of range. */
13809 if (CHARPOS (start_pos) < BEGV)
13810 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13811 else if (CHARPOS (start_pos) > ZV)
13812 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13813
13814 /* Find the start of the continued line. This should be fast
13815 because scan_buffer is fast (newline cache). */
13816 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13817 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13818 row, DEFAULT_FACE_ID);
13819 reseat_at_previous_visible_line_start (&it);
13820
13821 /* If the line start is "too far" away from the window start,
13822 say it takes too much time to compute a new window start. */
13823 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13824 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13825 {
13826 int min_distance, distance;
13827
13828 /* Move forward by display lines to find the new window
13829 start. If window width was enlarged, the new start can
13830 be expected to be > the old start. If window width was
13831 decreased, the new window start will be < the old start.
13832 So, we're looking for the display line start with the
13833 minimum distance from the old window start. */
13834 pos = it.current.pos;
13835 min_distance = INFINITY;
13836 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13837 distance < min_distance)
13838 {
13839 min_distance = distance;
13840 pos = it.current.pos;
13841 move_it_by_lines (&it, 1);
13842 }
13843
13844 /* Set the window start there. */
13845 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13846 window_start_changed_p = 1;
13847 }
13848 }
13849
13850 return window_start_changed_p;
13851 }
13852
13853
13854 /* Try cursor movement in case text has not changed in window WINDOW,
13855 with window start STARTP. Value is
13856
13857 CURSOR_MOVEMENT_SUCCESS if successful
13858
13859 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13860
13861 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13862 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13863 we want to scroll as if scroll-step were set to 1. See the code.
13864
13865 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13866 which case we have to abort this redisplay, and adjust matrices
13867 first. */
13868
13869 enum
13870 {
13871 CURSOR_MOVEMENT_SUCCESS,
13872 CURSOR_MOVEMENT_CANNOT_BE_USED,
13873 CURSOR_MOVEMENT_MUST_SCROLL,
13874 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13875 };
13876
13877 static int
13878 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13879 {
13880 struct window *w = XWINDOW (window);
13881 struct frame *f = XFRAME (w->frame);
13882 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13883
13884 #if GLYPH_DEBUG
13885 if (inhibit_try_cursor_movement)
13886 return rc;
13887 #endif
13888
13889 /* Handle case where text has not changed, only point, and it has
13890 not moved off the frame. */
13891 if (/* Point may be in this window. */
13892 PT >= CHARPOS (startp)
13893 /* Selective display hasn't changed. */
13894 && !current_buffer->clip_changed
13895 /* Function force-mode-line-update is used to force a thorough
13896 redisplay. It sets either windows_or_buffers_changed or
13897 update_mode_lines. So don't take a shortcut here for these
13898 cases. */
13899 && !update_mode_lines
13900 && !windows_or_buffers_changed
13901 && !cursor_type_changed
13902 /* Can't use this case if highlighting a region. When a
13903 region exists, cursor movement has to do more than just
13904 set the cursor. */
13905 && !(!NILP (Vtransient_mark_mode)
13906 && !NILP (BVAR (current_buffer, mark_active)))
13907 && NILP (w->region_showing)
13908 && NILP (Vshow_trailing_whitespace)
13909 /* Right after splitting windows, last_point may be nil. */
13910 && INTEGERP (w->last_point)
13911 /* This code is not used for mini-buffer for the sake of the case
13912 of redisplaying to replace an echo area message; since in
13913 that case the mini-buffer contents per se are usually
13914 unchanged. This code is of no real use in the mini-buffer
13915 since the handling of this_line_start_pos, etc., in redisplay
13916 handles the same cases. */
13917 && !EQ (window, minibuf_window)
13918 /* When splitting windows or for new windows, it happens that
13919 redisplay is called with a nil window_end_vpos or one being
13920 larger than the window. This should really be fixed in
13921 window.c. I don't have this on my list, now, so we do
13922 approximately the same as the old redisplay code. --gerd. */
13923 && INTEGERP (w->window_end_vpos)
13924 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13925 && (FRAME_WINDOW_P (f)
13926 || !overlay_arrow_in_current_buffer_p ()))
13927 {
13928 int this_scroll_margin, top_scroll_margin;
13929 struct glyph_row *row = NULL;
13930
13931 #if GLYPH_DEBUG
13932 debug_method_add (w, "cursor movement");
13933 #endif
13934
13935 /* Scroll if point within this distance from the top or bottom
13936 of the window. This is a pixel value. */
13937 if (scroll_margin > 0)
13938 {
13939 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13940 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13941 }
13942 else
13943 this_scroll_margin = 0;
13944
13945 top_scroll_margin = this_scroll_margin;
13946 if (WINDOW_WANTS_HEADER_LINE_P (w))
13947 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13948
13949 /* Start with the row the cursor was displayed during the last
13950 not paused redisplay. Give up if that row is not valid. */
13951 if (w->last_cursor.vpos < 0
13952 || w->last_cursor.vpos >= w->current_matrix->nrows)
13953 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13954 else
13955 {
13956 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13957 if (row->mode_line_p)
13958 ++row;
13959 if (!row->enabled_p)
13960 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13961 }
13962
13963 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13964 {
13965 int scroll_p = 0, must_scroll = 0;
13966 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13967
13968 if (PT > XFASTINT (w->last_point))
13969 {
13970 /* Point has moved forward. */
13971 while (MATRIX_ROW_END_CHARPOS (row) < PT
13972 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13973 {
13974 xassert (row->enabled_p);
13975 ++row;
13976 }
13977
13978 /* If the end position of a row equals the start
13979 position of the next row, and PT is at that position,
13980 we would rather display cursor in the next line. */
13981 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13982 && MATRIX_ROW_END_CHARPOS (row) == PT
13983 && row < w->current_matrix->rows
13984 + w->current_matrix->nrows - 1
13985 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13986 && !cursor_row_p (row))
13987 ++row;
13988
13989 /* If within the scroll margin, scroll. Note that
13990 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13991 the next line would be drawn, and that
13992 this_scroll_margin can be zero. */
13993 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13994 || PT > MATRIX_ROW_END_CHARPOS (row)
13995 /* Line is completely visible last line in window
13996 and PT is to be set in the next line. */
13997 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13998 && PT == MATRIX_ROW_END_CHARPOS (row)
13999 && !row->ends_at_zv_p
14000 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14001 scroll_p = 1;
14002 }
14003 else if (PT < XFASTINT (w->last_point))
14004 {
14005 /* Cursor has to be moved backward. Note that PT >=
14006 CHARPOS (startp) because of the outer if-statement. */
14007 while (!row->mode_line_p
14008 && (MATRIX_ROW_START_CHARPOS (row) > PT
14009 || (MATRIX_ROW_START_CHARPOS (row) == PT
14010 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14011 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14012 row > w->current_matrix->rows
14013 && (row-1)->ends_in_newline_from_string_p))))
14014 && (row->y > top_scroll_margin
14015 || CHARPOS (startp) == BEGV))
14016 {
14017 xassert (row->enabled_p);
14018 --row;
14019 }
14020
14021 /* Consider the following case: Window starts at BEGV,
14022 there is invisible, intangible text at BEGV, so that
14023 display starts at some point START > BEGV. It can
14024 happen that we are called with PT somewhere between
14025 BEGV and START. Try to handle that case. */
14026 if (row < w->current_matrix->rows
14027 || row->mode_line_p)
14028 {
14029 row = w->current_matrix->rows;
14030 if (row->mode_line_p)
14031 ++row;
14032 }
14033
14034 /* Due to newlines in overlay strings, we may have to
14035 skip forward over overlay strings. */
14036 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14037 && MATRIX_ROW_END_CHARPOS (row) == PT
14038 && !cursor_row_p (row))
14039 ++row;
14040
14041 /* If within the scroll margin, scroll. */
14042 if (row->y < top_scroll_margin
14043 && CHARPOS (startp) != BEGV)
14044 scroll_p = 1;
14045 }
14046 else
14047 {
14048 /* Cursor did not move. So don't scroll even if cursor line
14049 is partially visible, as it was so before. */
14050 rc = CURSOR_MOVEMENT_SUCCESS;
14051 }
14052
14053 if (PT < MATRIX_ROW_START_CHARPOS (row)
14054 || PT > MATRIX_ROW_END_CHARPOS (row))
14055 {
14056 /* if PT is not in the glyph row, give up. */
14057 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14058 must_scroll = 1;
14059 }
14060 else if (rc != CURSOR_MOVEMENT_SUCCESS
14061 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14062 {
14063 /* If rows are bidi-reordered and point moved, back up
14064 until we find a row that does not belong to a
14065 continuation line. This is because we must consider
14066 all rows of a continued line as candidates for the
14067 new cursor positioning, since row start and end
14068 positions change non-linearly with vertical position
14069 in such rows. */
14070 /* FIXME: Revisit this when glyph ``spilling'' in
14071 continuation lines' rows is implemented for
14072 bidi-reordered rows. */
14073 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14074 {
14075 xassert (row->enabled_p);
14076 --row;
14077 /* If we hit the beginning of the displayed portion
14078 without finding the first row of a continued
14079 line, give up. */
14080 if (row <= w->current_matrix->rows)
14081 {
14082 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14083 break;
14084 }
14085
14086 }
14087 }
14088 if (must_scroll)
14089 ;
14090 else if (rc != CURSOR_MOVEMENT_SUCCESS
14091 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14092 && make_cursor_line_fully_visible_p)
14093 {
14094 if (PT == MATRIX_ROW_END_CHARPOS (row)
14095 && !row->ends_at_zv_p
14096 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14097 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14098 else if (row->height > window_box_height (w))
14099 {
14100 /* If we end up in a partially visible line, let's
14101 make it fully visible, except when it's taller
14102 than the window, in which case we can't do much
14103 about it. */
14104 *scroll_step = 1;
14105 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14106 }
14107 else
14108 {
14109 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14110 if (!cursor_row_fully_visible_p (w, 0, 1))
14111 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14112 else
14113 rc = CURSOR_MOVEMENT_SUCCESS;
14114 }
14115 }
14116 else if (scroll_p)
14117 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14118 else if (rc != CURSOR_MOVEMENT_SUCCESS
14119 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14120 {
14121 /* With bidi-reordered rows, there could be more than
14122 one candidate row whose start and end positions
14123 occlude point. We need to let set_cursor_from_row
14124 find the best candidate. */
14125 /* FIXME: Revisit this when glyph ``spilling'' in
14126 continuation lines' rows is implemented for
14127 bidi-reordered rows. */
14128 int rv = 0;
14129
14130 do
14131 {
14132 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14133 && PT <= MATRIX_ROW_END_CHARPOS (row)
14134 && cursor_row_p (row))
14135 rv |= set_cursor_from_row (w, row, w->current_matrix,
14136 0, 0, 0, 0);
14137 /* As soon as we've found the first suitable row
14138 whose ends_at_zv_p flag is set, we are done. */
14139 if (rv
14140 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14141 {
14142 rc = CURSOR_MOVEMENT_SUCCESS;
14143 break;
14144 }
14145 ++row;
14146 }
14147 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14148 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14149 || (MATRIX_ROW_START_CHARPOS (row) == PT
14150 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14151 /* If we didn't find any candidate rows, or exited the
14152 loop before all the candidates were examined, signal
14153 to the caller that this method failed. */
14154 if (rc != CURSOR_MOVEMENT_SUCCESS
14155 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14156 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14157 else if (rv)
14158 rc = CURSOR_MOVEMENT_SUCCESS;
14159 }
14160 else
14161 {
14162 do
14163 {
14164 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14165 {
14166 rc = CURSOR_MOVEMENT_SUCCESS;
14167 break;
14168 }
14169 ++row;
14170 }
14171 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14172 && MATRIX_ROW_START_CHARPOS (row) == PT
14173 && cursor_row_p (row));
14174 }
14175 }
14176 }
14177
14178 return rc;
14179 }
14180
14181 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14182 static
14183 #endif
14184 void
14185 set_vertical_scroll_bar (struct window *w)
14186 {
14187 EMACS_INT start, end, whole;
14188
14189 /* Calculate the start and end positions for the current window.
14190 At some point, it would be nice to choose between scrollbars
14191 which reflect the whole buffer size, with special markers
14192 indicating narrowing, and scrollbars which reflect only the
14193 visible region.
14194
14195 Note that mini-buffers sometimes aren't displaying any text. */
14196 if (!MINI_WINDOW_P (w)
14197 || (w == XWINDOW (minibuf_window)
14198 && NILP (echo_area_buffer[0])))
14199 {
14200 struct buffer *buf = XBUFFER (w->buffer);
14201 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14202 start = marker_position (w->start) - BUF_BEGV (buf);
14203 /* I don't think this is guaranteed to be right. For the
14204 moment, we'll pretend it is. */
14205 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14206
14207 if (end < start)
14208 end = start;
14209 if (whole < (end - start))
14210 whole = end - start;
14211 }
14212 else
14213 start = end = whole = 0;
14214
14215 /* Indicate what this scroll bar ought to be displaying now. */
14216 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14217 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14218 (w, end - start, whole, start);
14219 }
14220
14221
14222 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14223 selected_window is redisplayed.
14224
14225 We can return without actually redisplaying the window if
14226 fonts_changed_p is nonzero. In that case, redisplay_internal will
14227 retry. */
14228
14229 static void
14230 redisplay_window (Lisp_Object window, int just_this_one_p)
14231 {
14232 struct window *w = XWINDOW (window);
14233 struct frame *f = XFRAME (w->frame);
14234 struct buffer *buffer = XBUFFER (w->buffer);
14235 struct buffer *old = current_buffer;
14236 struct text_pos lpoint, opoint, startp;
14237 int update_mode_line;
14238 int tem;
14239 struct it it;
14240 /* Record it now because it's overwritten. */
14241 int current_matrix_up_to_date_p = 0;
14242 int used_current_matrix_p = 0;
14243 /* This is less strict than current_matrix_up_to_date_p.
14244 It indictes that the buffer contents and narrowing are unchanged. */
14245 int buffer_unchanged_p = 0;
14246 int temp_scroll_step = 0;
14247 int count = SPECPDL_INDEX ();
14248 int rc;
14249 int centering_position = -1;
14250 int last_line_misfit = 0;
14251 EMACS_INT beg_unchanged, end_unchanged;
14252
14253 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14254 opoint = lpoint;
14255
14256 /* W must be a leaf window here. */
14257 xassert (!NILP (w->buffer));
14258 #if GLYPH_DEBUG
14259 *w->desired_matrix->method = 0;
14260 #endif
14261
14262 restart:
14263 reconsider_clip_changes (w, buffer);
14264
14265 /* Has the mode line to be updated? */
14266 update_mode_line = (!NILP (w->update_mode_line)
14267 || update_mode_lines
14268 || buffer->clip_changed
14269 || buffer->prevent_redisplay_optimizations_p);
14270
14271 if (MINI_WINDOW_P (w))
14272 {
14273 if (w == XWINDOW (echo_area_window)
14274 && !NILP (echo_area_buffer[0]))
14275 {
14276 if (update_mode_line)
14277 /* We may have to update a tty frame's menu bar or a
14278 tool-bar. Example `M-x C-h C-h C-g'. */
14279 goto finish_menu_bars;
14280 else
14281 /* We've already displayed the echo area glyphs in this window. */
14282 goto finish_scroll_bars;
14283 }
14284 else if ((w != XWINDOW (minibuf_window)
14285 || minibuf_level == 0)
14286 /* When buffer is nonempty, redisplay window normally. */
14287 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14288 /* Quail displays non-mini buffers in minibuffer window.
14289 In that case, redisplay the window normally. */
14290 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14291 {
14292 /* W is a mini-buffer window, but it's not active, so clear
14293 it. */
14294 int yb = window_text_bottom_y (w);
14295 struct glyph_row *row;
14296 int y;
14297
14298 for (y = 0, row = w->desired_matrix->rows;
14299 y < yb;
14300 y += row->height, ++row)
14301 blank_row (w, row, y);
14302 goto finish_scroll_bars;
14303 }
14304
14305 clear_glyph_matrix (w->desired_matrix);
14306 }
14307
14308 /* Otherwise set up data on this window; select its buffer and point
14309 value. */
14310 /* Really select the buffer, for the sake of buffer-local
14311 variables. */
14312 set_buffer_internal_1 (XBUFFER (w->buffer));
14313
14314 current_matrix_up_to_date_p
14315 = (!NILP (w->window_end_valid)
14316 && !current_buffer->clip_changed
14317 && !current_buffer->prevent_redisplay_optimizations_p
14318 && XFASTINT (w->last_modified) >= MODIFF
14319 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14320
14321 /* Run the window-bottom-change-functions
14322 if it is possible that the text on the screen has changed
14323 (either due to modification of the text, or any other reason). */
14324 if (!current_matrix_up_to_date_p
14325 && !NILP (Vwindow_text_change_functions))
14326 {
14327 safe_run_hooks (Qwindow_text_change_functions);
14328 goto restart;
14329 }
14330
14331 beg_unchanged = BEG_UNCHANGED;
14332 end_unchanged = END_UNCHANGED;
14333
14334 SET_TEXT_POS (opoint, PT, PT_BYTE);
14335
14336 specbind (Qinhibit_point_motion_hooks, Qt);
14337
14338 buffer_unchanged_p
14339 = (!NILP (w->window_end_valid)
14340 && !current_buffer->clip_changed
14341 && XFASTINT (w->last_modified) >= MODIFF
14342 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14343
14344 /* When windows_or_buffers_changed is non-zero, we can't rely on
14345 the window end being valid, so set it to nil there. */
14346 if (windows_or_buffers_changed)
14347 {
14348 /* If window starts on a continuation line, maybe adjust the
14349 window start in case the window's width changed. */
14350 if (XMARKER (w->start)->buffer == current_buffer)
14351 compute_window_start_on_continuation_line (w);
14352
14353 w->window_end_valid = Qnil;
14354 }
14355
14356 /* Some sanity checks. */
14357 CHECK_WINDOW_END (w);
14358 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14359 abort ();
14360 if (BYTEPOS (opoint) < CHARPOS (opoint))
14361 abort ();
14362
14363 /* If %c is in mode line, update it if needed. */
14364 if (!NILP (w->column_number_displayed)
14365 /* This alternative quickly identifies a common case
14366 where no change is needed. */
14367 && !(PT == XFASTINT (w->last_point)
14368 && XFASTINT (w->last_modified) >= MODIFF
14369 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14370 && (XFASTINT (w->column_number_displayed) != current_column ()))
14371 update_mode_line = 1;
14372
14373 /* Count number of windows showing the selected buffer. An indirect
14374 buffer counts as its base buffer. */
14375 if (!just_this_one_p)
14376 {
14377 struct buffer *current_base, *window_base;
14378 current_base = current_buffer;
14379 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14380 if (current_base->base_buffer)
14381 current_base = current_base->base_buffer;
14382 if (window_base->base_buffer)
14383 window_base = window_base->base_buffer;
14384 if (current_base == window_base)
14385 buffer_shared++;
14386 }
14387
14388 /* Point refers normally to the selected window. For any other
14389 window, set up appropriate value. */
14390 if (!EQ (window, selected_window))
14391 {
14392 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14393 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14394 if (new_pt < BEGV)
14395 {
14396 new_pt = BEGV;
14397 new_pt_byte = BEGV_BYTE;
14398 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14399 }
14400 else if (new_pt > (ZV - 1))
14401 {
14402 new_pt = ZV;
14403 new_pt_byte = ZV_BYTE;
14404 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14405 }
14406
14407 /* We don't use SET_PT so that the point-motion hooks don't run. */
14408 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14409 }
14410
14411 /* If any of the character widths specified in the display table
14412 have changed, invalidate the width run cache. It's true that
14413 this may be a bit late to catch such changes, but the rest of
14414 redisplay goes (non-fatally) haywire when the display table is
14415 changed, so why should we worry about doing any better? */
14416 if (current_buffer->width_run_cache)
14417 {
14418 struct Lisp_Char_Table *disptab = buffer_display_table ();
14419
14420 if (! disptab_matches_widthtab (disptab,
14421 XVECTOR (BVAR (current_buffer, width_table))))
14422 {
14423 invalidate_region_cache (current_buffer,
14424 current_buffer->width_run_cache,
14425 BEG, Z);
14426 recompute_width_table (current_buffer, disptab);
14427 }
14428 }
14429
14430 /* If window-start is screwed up, choose a new one. */
14431 if (XMARKER (w->start)->buffer != current_buffer)
14432 goto recenter;
14433
14434 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14435
14436 /* If someone specified a new starting point but did not insist,
14437 check whether it can be used. */
14438 if (!NILP (w->optional_new_start)
14439 && CHARPOS (startp) >= BEGV
14440 && CHARPOS (startp) <= ZV)
14441 {
14442 w->optional_new_start = Qnil;
14443 start_display (&it, w, startp);
14444 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14445 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14446 if (IT_CHARPOS (it) == PT)
14447 w->force_start = Qt;
14448 /* IT may overshoot PT if text at PT is invisible. */
14449 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14450 w->force_start = Qt;
14451 }
14452
14453 force_start:
14454
14455 /* Handle case where place to start displaying has been specified,
14456 unless the specified location is outside the accessible range. */
14457 if (!NILP (w->force_start)
14458 || w->frozen_window_start_p)
14459 {
14460 /* We set this later on if we have to adjust point. */
14461 int new_vpos = -1;
14462
14463 w->force_start = Qnil;
14464 w->vscroll = 0;
14465 w->window_end_valid = Qnil;
14466
14467 /* Forget any recorded base line for line number display. */
14468 if (!buffer_unchanged_p)
14469 w->base_line_number = Qnil;
14470
14471 /* Redisplay the mode line. Select the buffer properly for that.
14472 Also, run the hook window-scroll-functions
14473 because we have scrolled. */
14474 /* Note, we do this after clearing force_start because
14475 if there's an error, it is better to forget about force_start
14476 than to get into an infinite loop calling the hook functions
14477 and having them get more errors. */
14478 if (!update_mode_line
14479 || ! NILP (Vwindow_scroll_functions))
14480 {
14481 update_mode_line = 1;
14482 w->update_mode_line = Qt;
14483 startp = run_window_scroll_functions (window, startp);
14484 }
14485
14486 w->last_modified = make_number (0);
14487 w->last_overlay_modified = make_number (0);
14488 if (CHARPOS (startp) < BEGV)
14489 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14490 else if (CHARPOS (startp) > ZV)
14491 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14492
14493 /* Redisplay, then check if cursor has been set during the
14494 redisplay. Give up if new fonts were loaded. */
14495 /* We used to issue a CHECK_MARGINS argument to try_window here,
14496 but this causes scrolling to fail when point begins inside
14497 the scroll margin (bug#148) -- cyd */
14498 if (!try_window (window, startp, 0))
14499 {
14500 w->force_start = Qt;
14501 clear_glyph_matrix (w->desired_matrix);
14502 goto need_larger_matrices;
14503 }
14504
14505 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14506 {
14507 /* If point does not appear, try to move point so it does
14508 appear. The desired matrix has been built above, so we
14509 can use it here. */
14510 new_vpos = window_box_height (w) / 2;
14511 }
14512
14513 if (!cursor_row_fully_visible_p (w, 0, 0))
14514 {
14515 /* Point does appear, but on a line partly visible at end of window.
14516 Move it back to a fully-visible line. */
14517 new_vpos = window_box_height (w);
14518 }
14519
14520 /* If we need to move point for either of the above reasons,
14521 now actually do it. */
14522 if (new_vpos >= 0)
14523 {
14524 struct glyph_row *row;
14525
14526 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14527 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14528 ++row;
14529
14530 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14531 MATRIX_ROW_START_BYTEPOS (row));
14532
14533 if (w != XWINDOW (selected_window))
14534 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14535 else if (current_buffer == old)
14536 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14537
14538 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14539
14540 /* If we are highlighting the region, then we just changed
14541 the region, so redisplay to show it. */
14542 if (!NILP (Vtransient_mark_mode)
14543 && !NILP (BVAR (current_buffer, mark_active)))
14544 {
14545 clear_glyph_matrix (w->desired_matrix);
14546 if (!try_window (window, startp, 0))
14547 goto need_larger_matrices;
14548 }
14549 }
14550
14551 #if GLYPH_DEBUG
14552 debug_method_add (w, "forced window start");
14553 #endif
14554 goto done;
14555 }
14556
14557 /* Handle case where text has not changed, only point, and it has
14558 not moved off the frame, and we are not retrying after hscroll.
14559 (current_matrix_up_to_date_p is nonzero when retrying.) */
14560 if (current_matrix_up_to_date_p
14561 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14562 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14563 {
14564 switch (rc)
14565 {
14566 case CURSOR_MOVEMENT_SUCCESS:
14567 used_current_matrix_p = 1;
14568 goto done;
14569
14570 case CURSOR_MOVEMENT_MUST_SCROLL:
14571 goto try_to_scroll;
14572
14573 default:
14574 abort ();
14575 }
14576 }
14577 /* If current starting point was originally the beginning of a line
14578 but no longer is, find a new starting point. */
14579 else if (!NILP (w->start_at_line_beg)
14580 && !(CHARPOS (startp) <= BEGV
14581 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14582 {
14583 #if GLYPH_DEBUG
14584 debug_method_add (w, "recenter 1");
14585 #endif
14586 goto recenter;
14587 }
14588
14589 /* Try scrolling with try_window_id. Value is > 0 if update has
14590 been done, it is -1 if we know that the same window start will
14591 not work. It is 0 if unsuccessful for some other reason. */
14592 else if ((tem = try_window_id (w)) != 0)
14593 {
14594 #if GLYPH_DEBUG
14595 debug_method_add (w, "try_window_id %d", tem);
14596 #endif
14597
14598 if (fonts_changed_p)
14599 goto need_larger_matrices;
14600 if (tem > 0)
14601 goto done;
14602
14603 /* Otherwise try_window_id has returned -1 which means that we
14604 don't want the alternative below this comment to execute. */
14605 }
14606 else if (CHARPOS (startp) >= BEGV
14607 && CHARPOS (startp) <= ZV
14608 && PT >= CHARPOS (startp)
14609 && (CHARPOS (startp) < ZV
14610 /* Avoid starting at end of buffer. */
14611 || CHARPOS (startp) == BEGV
14612 || (XFASTINT (w->last_modified) >= MODIFF
14613 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14614 {
14615
14616 /* If first window line is a continuation line, and window start
14617 is inside the modified region, but the first change is before
14618 current window start, we must select a new window start.
14619
14620 However, if this is the result of a down-mouse event (e.g. by
14621 extending the mouse-drag-overlay), we don't want to select a
14622 new window start, since that would change the position under
14623 the mouse, resulting in an unwanted mouse-movement rather
14624 than a simple mouse-click. */
14625 if (NILP (w->start_at_line_beg)
14626 && NILP (do_mouse_tracking)
14627 && CHARPOS (startp) > BEGV
14628 && CHARPOS (startp) > BEG + beg_unchanged
14629 && CHARPOS (startp) <= Z - end_unchanged
14630 /* Even if w->start_at_line_beg is nil, a new window may
14631 start at a line_beg, since that's how set_buffer_window
14632 sets it. So, we need to check the return value of
14633 compute_window_start_on_continuation_line. (See also
14634 bug#197). */
14635 && XMARKER (w->start)->buffer == current_buffer
14636 && compute_window_start_on_continuation_line (w))
14637 {
14638 w->force_start = Qt;
14639 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14640 goto force_start;
14641 }
14642
14643 #if GLYPH_DEBUG
14644 debug_method_add (w, "same window start");
14645 #endif
14646
14647 /* Try to redisplay starting at same place as before.
14648 If point has not moved off frame, accept the results. */
14649 if (!current_matrix_up_to_date_p
14650 /* Don't use try_window_reusing_current_matrix in this case
14651 because a window scroll function can have changed the
14652 buffer. */
14653 || !NILP (Vwindow_scroll_functions)
14654 || MINI_WINDOW_P (w)
14655 || !(used_current_matrix_p
14656 = try_window_reusing_current_matrix (w)))
14657 {
14658 IF_DEBUG (debug_method_add (w, "1"));
14659 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14660 /* -1 means we need to scroll.
14661 0 means we need new matrices, but fonts_changed_p
14662 is set in that case, so we will detect it below. */
14663 goto try_to_scroll;
14664 }
14665
14666 if (fonts_changed_p)
14667 goto need_larger_matrices;
14668
14669 if (w->cursor.vpos >= 0)
14670 {
14671 if (!just_this_one_p
14672 || current_buffer->clip_changed
14673 || BEG_UNCHANGED < CHARPOS (startp))
14674 /* Forget any recorded base line for line number display. */
14675 w->base_line_number = Qnil;
14676
14677 if (!cursor_row_fully_visible_p (w, 1, 0))
14678 {
14679 clear_glyph_matrix (w->desired_matrix);
14680 last_line_misfit = 1;
14681 }
14682 /* Drop through and scroll. */
14683 else
14684 goto done;
14685 }
14686 else
14687 clear_glyph_matrix (w->desired_matrix);
14688 }
14689
14690 try_to_scroll:
14691
14692 w->last_modified = make_number (0);
14693 w->last_overlay_modified = make_number (0);
14694
14695 /* Redisplay the mode line. Select the buffer properly for that. */
14696 if (!update_mode_line)
14697 {
14698 update_mode_line = 1;
14699 w->update_mode_line = Qt;
14700 }
14701
14702 /* Try to scroll by specified few lines. */
14703 if ((scroll_conservatively
14704 || emacs_scroll_step
14705 || temp_scroll_step
14706 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14707 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14708 && CHARPOS (startp) >= BEGV
14709 && CHARPOS (startp) <= ZV)
14710 {
14711 /* The function returns -1 if new fonts were loaded, 1 if
14712 successful, 0 if not successful. */
14713 int ss = try_scrolling (window, just_this_one_p,
14714 scroll_conservatively,
14715 emacs_scroll_step,
14716 temp_scroll_step, last_line_misfit);
14717 switch (ss)
14718 {
14719 case SCROLLING_SUCCESS:
14720 goto done;
14721
14722 case SCROLLING_NEED_LARGER_MATRICES:
14723 goto need_larger_matrices;
14724
14725 case SCROLLING_FAILED:
14726 break;
14727
14728 default:
14729 abort ();
14730 }
14731 }
14732
14733 /* Finally, just choose a place to start which positions point
14734 according to user preferences. */
14735
14736 recenter:
14737
14738 #if GLYPH_DEBUG
14739 debug_method_add (w, "recenter");
14740 #endif
14741
14742 /* w->vscroll = 0; */
14743
14744 /* Forget any previously recorded base line for line number display. */
14745 if (!buffer_unchanged_p)
14746 w->base_line_number = Qnil;
14747
14748 /* Determine the window start relative to point. */
14749 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14750 it.current_y = it.last_visible_y;
14751 if (centering_position < 0)
14752 {
14753 int margin =
14754 scroll_margin > 0
14755 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14756 : 0;
14757 EMACS_INT margin_pos = CHARPOS (startp);
14758 int scrolling_up;
14759 Lisp_Object aggressive;
14760
14761 /* If there is a scroll margin at the top of the window, find
14762 its character position. */
14763 if (margin
14764 /* Cannot call start_display if startp is not in the
14765 accessible region of the buffer. This can happen when we
14766 have just switched to a different buffer and/or changed
14767 its restriction. In that case, startp is initialized to
14768 the character position 1 (BEG) because we did not yet
14769 have chance to display the buffer even once. */
14770 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14771 {
14772 struct it it1;
14773
14774 start_display (&it1, w, startp);
14775 move_it_vertically (&it1, margin);
14776 margin_pos = IT_CHARPOS (it1);
14777 }
14778 scrolling_up = PT > margin_pos;
14779 aggressive =
14780 scrolling_up
14781 ? BVAR (current_buffer, scroll_up_aggressively)
14782 : BVAR (current_buffer, scroll_down_aggressively);
14783
14784 if (!MINI_WINDOW_P (w)
14785 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
14786 {
14787 int pt_offset = 0;
14788
14789 /* Setting scroll-conservatively overrides
14790 scroll-*-aggressively. */
14791 if (!scroll_conservatively && NUMBERP (aggressive))
14792 {
14793 double float_amount = XFLOATINT (aggressive);
14794
14795 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14796 if (pt_offset == 0 && float_amount > 0)
14797 pt_offset = 1;
14798 if (pt_offset)
14799 margin -= 1;
14800 }
14801 /* Compute how much to move the window start backward from
14802 point so that point will be displayed where the user
14803 wants it. */
14804 if (scrolling_up)
14805 {
14806 centering_position = it.last_visible_y;
14807 if (pt_offset)
14808 centering_position -= pt_offset;
14809 centering_position -=
14810 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
14811 /* Don't let point enter the scroll margin near top of
14812 the window. */
14813 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
14814 centering_position = margin * FRAME_LINE_HEIGHT (f);
14815 }
14816 else
14817 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
14818 }
14819 else
14820 /* Set the window start half the height of the window backward
14821 from point. */
14822 centering_position = window_box_height (w) / 2;
14823 }
14824 move_it_vertically_backward (&it, centering_position);
14825
14826 xassert (IT_CHARPOS (it) >= BEGV);
14827
14828 /* The function move_it_vertically_backward may move over more
14829 than the specified y-distance. If it->w is small, e.g. a
14830 mini-buffer window, we may end up in front of the window's
14831 display area. Start displaying at the start of the line
14832 containing PT in this case. */
14833 if (it.current_y <= 0)
14834 {
14835 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14836 move_it_vertically_backward (&it, 0);
14837 it.current_y = 0;
14838 }
14839
14840 it.current_x = it.hpos = 0;
14841
14842 /* Set the window start position here explicitly, to avoid an
14843 infinite loop in case the functions in window-scroll-functions
14844 get errors. */
14845 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14846
14847 /* Run scroll hooks. */
14848 startp = run_window_scroll_functions (window, it.current.pos);
14849
14850 /* Redisplay the window. */
14851 if (!current_matrix_up_to_date_p
14852 || windows_or_buffers_changed
14853 || cursor_type_changed
14854 /* Don't use try_window_reusing_current_matrix in this case
14855 because it can have changed the buffer. */
14856 || !NILP (Vwindow_scroll_functions)
14857 || !just_this_one_p
14858 || MINI_WINDOW_P (w)
14859 || !(used_current_matrix_p
14860 = try_window_reusing_current_matrix (w)))
14861 try_window (window, startp, 0);
14862
14863 /* If new fonts have been loaded (due to fontsets), give up. We
14864 have to start a new redisplay since we need to re-adjust glyph
14865 matrices. */
14866 if (fonts_changed_p)
14867 goto need_larger_matrices;
14868
14869 /* If cursor did not appear assume that the middle of the window is
14870 in the first line of the window. Do it again with the next line.
14871 (Imagine a window of height 100, displaying two lines of height
14872 60. Moving back 50 from it->last_visible_y will end in the first
14873 line.) */
14874 if (w->cursor.vpos < 0)
14875 {
14876 if (!NILP (w->window_end_valid)
14877 && PT >= Z - XFASTINT (w->window_end_pos))
14878 {
14879 clear_glyph_matrix (w->desired_matrix);
14880 move_it_by_lines (&it, 1);
14881 try_window (window, it.current.pos, 0);
14882 }
14883 else if (PT < IT_CHARPOS (it))
14884 {
14885 clear_glyph_matrix (w->desired_matrix);
14886 move_it_by_lines (&it, -1);
14887 try_window (window, it.current.pos, 0);
14888 }
14889 else
14890 {
14891 /* Not much we can do about it. */
14892 }
14893 }
14894
14895 /* Consider the following case: Window starts at BEGV, there is
14896 invisible, intangible text at BEGV, so that display starts at
14897 some point START > BEGV. It can happen that we are called with
14898 PT somewhere between BEGV and START. Try to handle that case. */
14899 if (w->cursor.vpos < 0)
14900 {
14901 struct glyph_row *row = w->current_matrix->rows;
14902 if (row->mode_line_p)
14903 ++row;
14904 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14905 }
14906
14907 if (!cursor_row_fully_visible_p (w, 0, 0))
14908 {
14909 /* If vscroll is enabled, disable it and try again. */
14910 if (w->vscroll)
14911 {
14912 w->vscroll = 0;
14913 clear_glyph_matrix (w->desired_matrix);
14914 goto recenter;
14915 }
14916
14917 /* If centering point failed to make the whole line visible,
14918 put point at the top instead. That has to make the whole line
14919 visible, if it can be done. */
14920 if (centering_position == 0)
14921 goto done;
14922
14923 clear_glyph_matrix (w->desired_matrix);
14924 centering_position = 0;
14925 goto recenter;
14926 }
14927
14928 done:
14929
14930 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14931 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14932 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14933 ? Qt : Qnil);
14934
14935 /* Display the mode line, if we must. */
14936 if ((update_mode_line
14937 /* If window not full width, must redo its mode line
14938 if (a) the window to its side is being redone and
14939 (b) we do a frame-based redisplay. This is a consequence
14940 of how inverted lines are drawn in frame-based redisplay. */
14941 || (!just_this_one_p
14942 && !FRAME_WINDOW_P (f)
14943 && !WINDOW_FULL_WIDTH_P (w))
14944 /* Line number to display. */
14945 || INTEGERP (w->base_line_pos)
14946 /* Column number is displayed and different from the one displayed. */
14947 || (!NILP (w->column_number_displayed)
14948 && (XFASTINT (w->column_number_displayed) != current_column ())))
14949 /* This means that the window has a mode line. */
14950 && (WINDOW_WANTS_MODELINE_P (w)
14951 || WINDOW_WANTS_HEADER_LINE_P (w)))
14952 {
14953 display_mode_lines (w);
14954
14955 /* If mode line height has changed, arrange for a thorough
14956 immediate redisplay using the correct mode line height. */
14957 if (WINDOW_WANTS_MODELINE_P (w)
14958 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14959 {
14960 fonts_changed_p = 1;
14961 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14962 = DESIRED_MODE_LINE_HEIGHT (w);
14963 }
14964
14965 /* If header line height has changed, arrange for a thorough
14966 immediate redisplay using the correct header line height. */
14967 if (WINDOW_WANTS_HEADER_LINE_P (w)
14968 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14969 {
14970 fonts_changed_p = 1;
14971 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14972 = DESIRED_HEADER_LINE_HEIGHT (w);
14973 }
14974
14975 if (fonts_changed_p)
14976 goto need_larger_matrices;
14977 }
14978
14979 if (!line_number_displayed
14980 && !BUFFERP (w->base_line_pos))
14981 {
14982 w->base_line_pos = Qnil;
14983 w->base_line_number = Qnil;
14984 }
14985
14986 finish_menu_bars:
14987
14988 /* When we reach a frame's selected window, redo the frame's menu bar. */
14989 if (update_mode_line
14990 && EQ (FRAME_SELECTED_WINDOW (f), window))
14991 {
14992 int redisplay_menu_p = 0;
14993
14994 if (FRAME_WINDOW_P (f))
14995 {
14996 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14997 || defined (HAVE_NS) || defined (USE_GTK)
14998 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14999 #else
15000 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15001 #endif
15002 }
15003 else
15004 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15005
15006 if (redisplay_menu_p)
15007 display_menu_bar (w);
15008
15009 #ifdef HAVE_WINDOW_SYSTEM
15010 if (FRAME_WINDOW_P (f))
15011 {
15012 #if defined (USE_GTK) || defined (HAVE_NS)
15013 if (FRAME_EXTERNAL_TOOL_BAR (f))
15014 redisplay_tool_bar (f);
15015 #else
15016 if (WINDOWP (f->tool_bar_window)
15017 && (FRAME_TOOL_BAR_LINES (f) > 0
15018 || !NILP (Vauto_resize_tool_bars))
15019 && redisplay_tool_bar (f))
15020 ignore_mouse_drag_p = 1;
15021 #endif
15022 }
15023 #endif
15024 }
15025
15026 #ifdef HAVE_WINDOW_SYSTEM
15027 if (FRAME_WINDOW_P (f)
15028 && update_window_fringes (w, (just_this_one_p
15029 || (!used_current_matrix_p && !overlay_arrow_seen)
15030 || w->pseudo_window_p)))
15031 {
15032 update_begin (f);
15033 BLOCK_INPUT;
15034 if (draw_window_fringes (w, 1))
15035 x_draw_vertical_border (w);
15036 UNBLOCK_INPUT;
15037 update_end (f);
15038 }
15039 #endif /* HAVE_WINDOW_SYSTEM */
15040
15041 /* We go to this label, with fonts_changed_p nonzero,
15042 if it is necessary to try again using larger glyph matrices.
15043 We have to redeem the scroll bar even in this case,
15044 because the loop in redisplay_internal expects that. */
15045 need_larger_matrices:
15046 ;
15047 finish_scroll_bars:
15048
15049 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15050 {
15051 /* Set the thumb's position and size. */
15052 set_vertical_scroll_bar (w);
15053
15054 /* Note that we actually used the scroll bar attached to this
15055 window, so it shouldn't be deleted at the end of redisplay. */
15056 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15057 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15058 }
15059
15060 /* Restore current_buffer and value of point in it. The window
15061 update may have changed the buffer, so first make sure `opoint'
15062 is still valid (Bug#6177). */
15063 if (CHARPOS (opoint) < BEGV)
15064 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15065 else if (CHARPOS (opoint) > ZV)
15066 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15067 else
15068 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15069
15070 set_buffer_internal_1 (old);
15071 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15072 shorter. This can be caused by log truncation in *Messages*. */
15073 if (CHARPOS (lpoint) <= ZV)
15074 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15075
15076 unbind_to (count, Qnil);
15077 }
15078
15079
15080 /* Build the complete desired matrix of WINDOW with a window start
15081 buffer position POS.
15082
15083 Value is 1 if successful. It is zero if fonts were loaded during
15084 redisplay which makes re-adjusting glyph matrices necessary, and -1
15085 if point would appear in the scroll margins.
15086 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15087 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15088 set in FLAGS.) */
15089
15090 int
15091 try_window (Lisp_Object window, struct text_pos pos, int flags)
15092 {
15093 struct window *w = XWINDOW (window);
15094 struct it it;
15095 struct glyph_row *last_text_row = NULL;
15096 struct frame *f = XFRAME (w->frame);
15097
15098 /* Make POS the new window start. */
15099 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15100
15101 /* Mark cursor position as unknown. No overlay arrow seen. */
15102 w->cursor.vpos = -1;
15103 overlay_arrow_seen = 0;
15104
15105 /* Initialize iterator and info to start at POS. */
15106 start_display (&it, w, pos);
15107
15108 /* Display all lines of W. */
15109 while (it.current_y < it.last_visible_y)
15110 {
15111 if (display_line (&it))
15112 last_text_row = it.glyph_row - 1;
15113 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15114 return 0;
15115 }
15116
15117 /* Don't let the cursor end in the scroll margins. */
15118 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15119 && !MINI_WINDOW_P (w))
15120 {
15121 int this_scroll_margin;
15122
15123 if (scroll_margin > 0)
15124 {
15125 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15126 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15127 }
15128 else
15129 this_scroll_margin = 0;
15130
15131 if ((w->cursor.y >= 0 /* not vscrolled */
15132 && w->cursor.y < this_scroll_margin
15133 && CHARPOS (pos) > BEGV
15134 && IT_CHARPOS (it) < ZV)
15135 /* rms: considering make_cursor_line_fully_visible_p here
15136 seems to give wrong results. We don't want to recenter
15137 when the last line is partly visible, we want to allow
15138 that case to be handled in the usual way. */
15139 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15140 {
15141 w->cursor.vpos = -1;
15142 clear_glyph_matrix (w->desired_matrix);
15143 return -1;
15144 }
15145 }
15146
15147 /* If bottom moved off end of frame, change mode line percentage. */
15148 if (XFASTINT (w->window_end_pos) <= 0
15149 && Z != IT_CHARPOS (it))
15150 w->update_mode_line = Qt;
15151
15152 /* Set window_end_pos to the offset of the last character displayed
15153 on the window from the end of current_buffer. Set
15154 window_end_vpos to its row number. */
15155 if (last_text_row)
15156 {
15157 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15158 w->window_end_bytepos
15159 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15160 w->window_end_pos
15161 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15162 w->window_end_vpos
15163 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15164 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15165 ->displays_text_p);
15166 }
15167 else
15168 {
15169 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15170 w->window_end_pos = make_number (Z - ZV);
15171 w->window_end_vpos = make_number (0);
15172 }
15173
15174 /* But that is not valid info until redisplay finishes. */
15175 w->window_end_valid = Qnil;
15176 return 1;
15177 }
15178
15179
15180 \f
15181 /************************************************************************
15182 Window redisplay reusing current matrix when buffer has not changed
15183 ************************************************************************/
15184
15185 /* Try redisplay of window W showing an unchanged buffer with a
15186 different window start than the last time it was displayed by
15187 reusing its current matrix. Value is non-zero if successful.
15188 W->start is the new window start. */
15189
15190 static int
15191 try_window_reusing_current_matrix (struct window *w)
15192 {
15193 struct frame *f = XFRAME (w->frame);
15194 struct glyph_row *bottom_row;
15195 struct it it;
15196 struct run run;
15197 struct text_pos start, new_start;
15198 int nrows_scrolled, i;
15199 struct glyph_row *last_text_row;
15200 struct glyph_row *last_reused_text_row;
15201 struct glyph_row *start_row;
15202 int start_vpos, min_y, max_y;
15203
15204 #if GLYPH_DEBUG
15205 if (inhibit_try_window_reusing)
15206 return 0;
15207 #endif
15208
15209 if (/* This function doesn't handle terminal frames. */
15210 !FRAME_WINDOW_P (f)
15211 /* Don't try to reuse the display if windows have been split
15212 or such. */
15213 || windows_or_buffers_changed
15214 || cursor_type_changed)
15215 return 0;
15216
15217 /* Can't do this if region may have changed. */
15218 if ((!NILP (Vtransient_mark_mode)
15219 && !NILP (BVAR (current_buffer, mark_active)))
15220 || !NILP (w->region_showing)
15221 || !NILP (Vshow_trailing_whitespace))
15222 return 0;
15223
15224 /* If top-line visibility has changed, give up. */
15225 if (WINDOW_WANTS_HEADER_LINE_P (w)
15226 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15227 return 0;
15228
15229 /* Give up if old or new display is scrolled vertically. We could
15230 make this function handle this, but right now it doesn't. */
15231 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15232 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15233 return 0;
15234
15235 /* The variable new_start now holds the new window start. The old
15236 start `start' can be determined from the current matrix. */
15237 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15238 start = start_row->minpos;
15239 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15240
15241 /* Clear the desired matrix for the display below. */
15242 clear_glyph_matrix (w->desired_matrix);
15243
15244 if (CHARPOS (new_start) <= CHARPOS (start))
15245 {
15246 /* Don't use this method if the display starts with an ellipsis
15247 displayed for invisible text. It's not easy to handle that case
15248 below, and it's certainly not worth the effort since this is
15249 not a frequent case. */
15250 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15251 return 0;
15252
15253 IF_DEBUG (debug_method_add (w, "twu1"));
15254
15255 /* Display up to a row that can be reused. The variable
15256 last_text_row is set to the last row displayed that displays
15257 text. Note that it.vpos == 0 if or if not there is a
15258 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15259 start_display (&it, w, new_start);
15260 w->cursor.vpos = -1;
15261 last_text_row = last_reused_text_row = NULL;
15262
15263 while (it.current_y < it.last_visible_y
15264 && !fonts_changed_p)
15265 {
15266 /* If we have reached into the characters in the START row,
15267 that means the line boundaries have changed. So we
15268 can't start copying with the row START. Maybe it will
15269 work to start copying with the following row. */
15270 while (IT_CHARPOS (it) > CHARPOS (start))
15271 {
15272 /* Advance to the next row as the "start". */
15273 start_row++;
15274 start = start_row->minpos;
15275 /* If there are no more rows to try, or just one, give up. */
15276 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15277 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15278 || CHARPOS (start) == ZV)
15279 {
15280 clear_glyph_matrix (w->desired_matrix);
15281 return 0;
15282 }
15283
15284 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15285 }
15286 /* If we have reached alignment,
15287 we can copy the rest of the rows. */
15288 if (IT_CHARPOS (it) == CHARPOS (start))
15289 break;
15290
15291 if (display_line (&it))
15292 last_text_row = it.glyph_row - 1;
15293 }
15294
15295 /* A value of current_y < last_visible_y means that we stopped
15296 at the previous window start, which in turn means that we
15297 have at least one reusable row. */
15298 if (it.current_y < it.last_visible_y)
15299 {
15300 struct glyph_row *row;
15301
15302 /* IT.vpos always starts from 0; it counts text lines. */
15303 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15304
15305 /* Find PT if not already found in the lines displayed. */
15306 if (w->cursor.vpos < 0)
15307 {
15308 int dy = it.current_y - start_row->y;
15309
15310 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15311 row = row_containing_pos (w, PT, row, NULL, dy);
15312 if (row)
15313 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15314 dy, nrows_scrolled);
15315 else
15316 {
15317 clear_glyph_matrix (w->desired_matrix);
15318 return 0;
15319 }
15320 }
15321
15322 /* Scroll the display. Do it before the current matrix is
15323 changed. The problem here is that update has not yet
15324 run, i.e. part of the current matrix is not up to date.
15325 scroll_run_hook will clear the cursor, and use the
15326 current matrix to get the height of the row the cursor is
15327 in. */
15328 run.current_y = start_row->y;
15329 run.desired_y = it.current_y;
15330 run.height = it.last_visible_y - it.current_y;
15331
15332 if (run.height > 0 && run.current_y != run.desired_y)
15333 {
15334 update_begin (f);
15335 FRAME_RIF (f)->update_window_begin_hook (w);
15336 FRAME_RIF (f)->clear_window_mouse_face (w);
15337 FRAME_RIF (f)->scroll_run_hook (w, &run);
15338 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15339 update_end (f);
15340 }
15341
15342 /* Shift current matrix down by nrows_scrolled lines. */
15343 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15344 rotate_matrix (w->current_matrix,
15345 start_vpos,
15346 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15347 nrows_scrolled);
15348
15349 /* Disable lines that must be updated. */
15350 for (i = 0; i < nrows_scrolled; ++i)
15351 (start_row + i)->enabled_p = 0;
15352
15353 /* Re-compute Y positions. */
15354 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15355 max_y = it.last_visible_y;
15356 for (row = start_row + nrows_scrolled;
15357 row < bottom_row;
15358 ++row)
15359 {
15360 row->y = it.current_y;
15361 row->visible_height = row->height;
15362
15363 if (row->y < min_y)
15364 row->visible_height -= min_y - row->y;
15365 if (row->y + row->height > max_y)
15366 row->visible_height -= row->y + row->height - max_y;
15367 row->redraw_fringe_bitmaps_p = 1;
15368
15369 it.current_y += row->height;
15370
15371 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15372 last_reused_text_row = row;
15373 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15374 break;
15375 }
15376
15377 /* Disable lines in the current matrix which are now
15378 below the window. */
15379 for (++row; row < bottom_row; ++row)
15380 row->enabled_p = row->mode_line_p = 0;
15381 }
15382
15383 /* Update window_end_pos etc.; last_reused_text_row is the last
15384 reused row from the current matrix containing text, if any.
15385 The value of last_text_row is the last displayed line
15386 containing text. */
15387 if (last_reused_text_row)
15388 {
15389 w->window_end_bytepos
15390 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15391 w->window_end_pos
15392 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15393 w->window_end_vpos
15394 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15395 w->current_matrix));
15396 }
15397 else if (last_text_row)
15398 {
15399 w->window_end_bytepos
15400 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15401 w->window_end_pos
15402 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15403 w->window_end_vpos
15404 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15405 }
15406 else
15407 {
15408 /* This window must be completely empty. */
15409 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15410 w->window_end_pos = make_number (Z - ZV);
15411 w->window_end_vpos = make_number (0);
15412 }
15413 w->window_end_valid = Qnil;
15414
15415 /* Update hint: don't try scrolling again in update_window. */
15416 w->desired_matrix->no_scrolling_p = 1;
15417
15418 #if GLYPH_DEBUG
15419 debug_method_add (w, "try_window_reusing_current_matrix 1");
15420 #endif
15421 return 1;
15422 }
15423 else if (CHARPOS (new_start) > CHARPOS (start))
15424 {
15425 struct glyph_row *pt_row, *row;
15426 struct glyph_row *first_reusable_row;
15427 struct glyph_row *first_row_to_display;
15428 int dy;
15429 int yb = window_text_bottom_y (w);
15430
15431 /* Find the row starting at new_start, if there is one. Don't
15432 reuse a partially visible line at the end. */
15433 first_reusable_row = start_row;
15434 while (first_reusable_row->enabled_p
15435 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15436 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15437 < CHARPOS (new_start)))
15438 ++first_reusable_row;
15439
15440 /* Give up if there is no row to reuse. */
15441 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15442 || !first_reusable_row->enabled_p
15443 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15444 != CHARPOS (new_start)))
15445 return 0;
15446
15447 /* We can reuse fully visible rows beginning with
15448 first_reusable_row to the end of the window. Set
15449 first_row_to_display to the first row that cannot be reused.
15450 Set pt_row to the row containing point, if there is any. */
15451 pt_row = NULL;
15452 for (first_row_to_display = first_reusable_row;
15453 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15454 ++first_row_to_display)
15455 {
15456 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15457 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15458 pt_row = first_row_to_display;
15459 }
15460
15461 /* Start displaying at the start of first_row_to_display. */
15462 xassert (first_row_to_display->y < yb);
15463 init_to_row_start (&it, w, first_row_to_display);
15464
15465 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15466 - start_vpos);
15467 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15468 - nrows_scrolled);
15469 it.current_y = (first_row_to_display->y - first_reusable_row->y
15470 + WINDOW_HEADER_LINE_HEIGHT (w));
15471
15472 /* Display lines beginning with first_row_to_display in the
15473 desired matrix. Set last_text_row to the last row displayed
15474 that displays text. */
15475 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15476 if (pt_row == NULL)
15477 w->cursor.vpos = -1;
15478 last_text_row = NULL;
15479 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15480 if (display_line (&it))
15481 last_text_row = it.glyph_row - 1;
15482
15483 /* If point is in a reused row, adjust y and vpos of the cursor
15484 position. */
15485 if (pt_row)
15486 {
15487 w->cursor.vpos -= nrows_scrolled;
15488 w->cursor.y -= first_reusable_row->y - start_row->y;
15489 }
15490
15491 /* Give up if point isn't in a row displayed or reused. (This
15492 also handles the case where w->cursor.vpos < nrows_scrolled
15493 after the calls to display_line, which can happen with scroll
15494 margins. See bug#1295.) */
15495 if (w->cursor.vpos < 0)
15496 {
15497 clear_glyph_matrix (w->desired_matrix);
15498 return 0;
15499 }
15500
15501 /* Scroll the display. */
15502 run.current_y = first_reusable_row->y;
15503 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15504 run.height = it.last_visible_y - run.current_y;
15505 dy = run.current_y - run.desired_y;
15506
15507 if (run.height)
15508 {
15509 update_begin (f);
15510 FRAME_RIF (f)->update_window_begin_hook (w);
15511 FRAME_RIF (f)->clear_window_mouse_face (w);
15512 FRAME_RIF (f)->scroll_run_hook (w, &run);
15513 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15514 update_end (f);
15515 }
15516
15517 /* Adjust Y positions of reused rows. */
15518 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15519 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15520 max_y = it.last_visible_y;
15521 for (row = first_reusable_row; row < first_row_to_display; ++row)
15522 {
15523 row->y -= dy;
15524 row->visible_height = row->height;
15525 if (row->y < min_y)
15526 row->visible_height -= min_y - row->y;
15527 if (row->y + row->height > max_y)
15528 row->visible_height -= row->y + row->height - max_y;
15529 row->redraw_fringe_bitmaps_p = 1;
15530 }
15531
15532 /* Scroll the current matrix. */
15533 xassert (nrows_scrolled > 0);
15534 rotate_matrix (w->current_matrix,
15535 start_vpos,
15536 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15537 -nrows_scrolled);
15538
15539 /* Disable rows not reused. */
15540 for (row -= nrows_scrolled; row < bottom_row; ++row)
15541 row->enabled_p = 0;
15542
15543 /* Point may have moved to a different line, so we cannot assume that
15544 the previous cursor position is valid; locate the correct row. */
15545 if (pt_row)
15546 {
15547 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15548 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15549 row++)
15550 {
15551 w->cursor.vpos++;
15552 w->cursor.y = row->y;
15553 }
15554 if (row < bottom_row)
15555 {
15556 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15557 struct glyph *end = glyph + row->used[TEXT_AREA];
15558
15559 /* Can't use this optimization with bidi-reordered glyph
15560 rows, unless cursor is already at point. */
15561 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15562 {
15563 if (!(w->cursor.hpos >= 0
15564 && w->cursor.hpos < row->used[TEXT_AREA]
15565 && BUFFERP (glyph->object)
15566 && glyph->charpos == PT))
15567 return 0;
15568 }
15569 else
15570 for (; glyph < end
15571 && (!BUFFERP (glyph->object)
15572 || glyph->charpos < PT);
15573 glyph++)
15574 {
15575 w->cursor.hpos++;
15576 w->cursor.x += glyph->pixel_width;
15577 }
15578 }
15579 }
15580
15581 /* Adjust window end. A null value of last_text_row means that
15582 the window end is in reused rows which in turn means that
15583 only its vpos can have changed. */
15584 if (last_text_row)
15585 {
15586 w->window_end_bytepos
15587 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15588 w->window_end_pos
15589 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15590 w->window_end_vpos
15591 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15592 }
15593 else
15594 {
15595 w->window_end_vpos
15596 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15597 }
15598
15599 w->window_end_valid = Qnil;
15600 w->desired_matrix->no_scrolling_p = 1;
15601
15602 #if GLYPH_DEBUG
15603 debug_method_add (w, "try_window_reusing_current_matrix 2");
15604 #endif
15605 return 1;
15606 }
15607
15608 return 0;
15609 }
15610
15611
15612 \f
15613 /************************************************************************
15614 Window redisplay reusing current matrix when buffer has changed
15615 ************************************************************************/
15616
15617 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15618 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15619 EMACS_INT *, EMACS_INT *);
15620 static struct glyph_row *
15621 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15622 struct glyph_row *);
15623
15624
15625 /* Return the last row in MATRIX displaying text. If row START is
15626 non-null, start searching with that row. IT gives the dimensions
15627 of the display. Value is null if matrix is empty; otherwise it is
15628 a pointer to the row found. */
15629
15630 static struct glyph_row *
15631 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15632 struct glyph_row *start)
15633 {
15634 struct glyph_row *row, *row_found;
15635
15636 /* Set row_found to the last row in IT->w's current matrix
15637 displaying text. The loop looks funny but think of partially
15638 visible lines. */
15639 row_found = NULL;
15640 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15641 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15642 {
15643 xassert (row->enabled_p);
15644 row_found = row;
15645 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15646 break;
15647 ++row;
15648 }
15649
15650 return row_found;
15651 }
15652
15653
15654 /* Return the last row in the current matrix of W that is not affected
15655 by changes at the start of current_buffer that occurred since W's
15656 current matrix was built. Value is null if no such row exists.
15657
15658 BEG_UNCHANGED us the number of characters unchanged at the start of
15659 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15660 first changed character in current_buffer. Characters at positions <
15661 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15662 when the current matrix was built. */
15663
15664 static struct glyph_row *
15665 find_last_unchanged_at_beg_row (struct window *w)
15666 {
15667 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15668 struct glyph_row *row;
15669 struct glyph_row *row_found = NULL;
15670 int yb = window_text_bottom_y (w);
15671
15672 /* Find the last row displaying unchanged text. */
15673 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15674 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15675 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15676 ++row)
15677 {
15678 if (/* If row ends before first_changed_pos, it is unchanged,
15679 except in some case. */
15680 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15681 /* When row ends in ZV and we write at ZV it is not
15682 unchanged. */
15683 && !row->ends_at_zv_p
15684 /* When first_changed_pos is the end of a continued line,
15685 row is not unchanged because it may be no longer
15686 continued. */
15687 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15688 && (row->continued_p
15689 || row->exact_window_width_line_p)))
15690 row_found = row;
15691
15692 /* Stop if last visible row. */
15693 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15694 break;
15695 }
15696
15697 return row_found;
15698 }
15699
15700
15701 /* Find the first glyph row in the current matrix of W that is not
15702 affected by changes at the end of current_buffer since the
15703 time W's current matrix was built.
15704
15705 Return in *DELTA the number of chars by which buffer positions in
15706 unchanged text at the end of current_buffer must be adjusted.
15707
15708 Return in *DELTA_BYTES the corresponding number of bytes.
15709
15710 Value is null if no such row exists, i.e. all rows are affected by
15711 changes. */
15712
15713 static struct glyph_row *
15714 find_first_unchanged_at_end_row (struct window *w,
15715 EMACS_INT *delta, EMACS_INT *delta_bytes)
15716 {
15717 struct glyph_row *row;
15718 struct glyph_row *row_found = NULL;
15719
15720 *delta = *delta_bytes = 0;
15721
15722 /* Display must not have been paused, otherwise the current matrix
15723 is not up to date. */
15724 eassert (!NILP (w->window_end_valid));
15725
15726 /* A value of window_end_pos >= END_UNCHANGED means that the window
15727 end is in the range of changed text. If so, there is no
15728 unchanged row at the end of W's current matrix. */
15729 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15730 return NULL;
15731
15732 /* Set row to the last row in W's current matrix displaying text. */
15733 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15734
15735 /* If matrix is entirely empty, no unchanged row exists. */
15736 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15737 {
15738 /* The value of row is the last glyph row in the matrix having a
15739 meaningful buffer position in it. The end position of row
15740 corresponds to window_end_pos. This allows us to translate
15741 buffer positions in the current matrix to current buffer
15742 positions for characters not in changed text. */
15743 EMACS_INT Z_old =
15744 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15745 EMACS_INT Z_BYTE_old =
15746 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15747 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15748 struct glyph_row *first_text_row
15749 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15750
15751 *delta = Z - Z_old;
15752 *delta_bytes = Z_BYTE - Z_BYTE_old;
15753
15754 /* Set last_unchanged_pos to the buffer position of the last
15755 character in the buffer that has not been changed. Z is the
15756 index + 1 of the last character in current_buffer, i.e. by
15757 subtracting END_UNCHANGED we get the index of the last
15758 unchanged character, and we have to add BEG to get its buffer
15759 position. */
15760 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15761 last_unchanged_pos_old = last_unchanged_pos - *delta;
15762
15763 /* Search backward from ROW for a row displaying a line that
15764 starts at a minimum position >= last_unchanged_pos_old. */
15765 for (; row > first_text_row; --row)
15766 {
15767 /* This used to abort, but it can happen.
15768 It is ok to just stop the search instead here. KFS. */
15769 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15770 break;
15771
15772 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15773 row_found = row;
15774 }
15775 }
15776
15777 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15778
15779 return row_found;
15780 }
15781
15782
15783 /* Make sure that glyph rows in the current matrix of window W
15784 reference the same glyph memory as corresponding rows in the
15785 frame's frame matrix. This function is called after scrolling W's
15786 current matrix on a terminal frame in try_window_id and
15787 try_window_reusing_current_matrix. */
15788
15789 static void
15790 sync_frame_with_window_matrix_rows (struct window *w)
15791 {
15792 struct frame *f = XFRAME (w->frame);
15793 struct glyph_row *window_row, *window_row_end, *frame_row;
15794
15795 /* Preconditions: W must be a leaf window and full-width. Its frame
15796 must have a frame matrix. */
15797 xassert (NILP (w->hchild) && NILP (w->vchild));
15798 xassert (WINDOW_FULL_WIDTH_P (w));
15799 xassert (!FRAME_WINDOW_P (f));
15800
15801 /* If W is a full-width window, glyph pointers in W's current matrix
15802 have, by definition, to be the same as glyph pointers in the
15803 corresponding frame matrix. Note that frame matrices have no
15804 marginal areas (see build_frame_matrix). */
15805 window_row = w->current_matrix->rows;
15806 window_row_end = window_row + w->current_matrix->nrows;
15807 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15808 while (window_row < window_row_end)
15809 {
15810 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15811 struct glyph *end = window_row->glyphs[LAST_AREA];
15812
15813 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15814 frame_row->glyphs[TEXT_AREA] = start;
15815 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15816 frame_row->glyphs[LAST_AREA] = end;
15817
15818 /* Disable frame rows whose corresponding window rows have
15819 been disabled in try_window_id. */
15820 if (!window_row->enabled_p)
15821 frame_row->enabled_p = 0;
15822
15823 ++window_row, ++frame_row;
15824 }
15825 }
15826
15827
15828 /* Find the glyph row in window W containing CHARPOS. Consider all
15829 rows between START and END (not inclusive). END null means search
15830 all rows to the end of the display area of W. Value is the row
15831 containing CHARPOS or null. */
15832
15833 struct glyph_row *
15834 row_containing_pos (struct window *w, EMACS_INT charpos,
15835 struct glyph_row *start, struct glyph_row *end, int dy)
15836 {
15837 struct glyph_row *row = start;
15838 struct glyph_row *best_row = NULL;
15839 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15840 int last_y;
15841
15842 /* If we happen to start on a header-line, skip that. */
15843 if (row->mode_line_p)
15844 ++row;
15845
15846 if ((end && row >= end) || !row->enabled_p)
15847 return NULL;
15848
15849 last_y = window_text_bottom_y (w) - dy;
15850
15851 while (1)
15852 {
15853 /* Give up if we have gone too far. */
15854 if (end && row >= end)
15855 return NULL;
15856 /* This formerly returned if they were equal.
15857 I think that both quantities are of a "last plus one" type;
15858 if so, when they are equal, the row is within the screen. -- rms. */
15859 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15860 return NULL;
15861
15862 /* If it is in this row, return this row. */
15863 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15864 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15865 /* The end position of a row equals the start
15866 position of the next row. If CHARPOS is there, we
15867 would rather display it in the next line, except
15868 when this line ends in ZV. */
15869 && !row->ends_at_zv_p
15870 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15871 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15872 {
15873 struct glyph *g;
15874
15875 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15876 || (!best_row && !row->continued_p))
15877 return row;
15878 /* In bidi-reordered rows, there could be several rows
15879 occluding point, all of them belonging to the same
15880 continued line. We need to find the row which fits
15881 CHARPOS the best. */
15882 for (g = row->glyphs[TEXT_AREA];
15883 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15884 g++)
15885 {
15886 if (!STRINGP (g->object))
15887 {
15888 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15889 {
15890 mindif = eabs (g->charpos - charpos);
15891 best_row = row;
15892 /* Exact match always wins. */
15893 if (mindif == 0)
15894 return best_row;
15895 }
15896 }
15897 }
15898 }
15899 else if (best_row && !row->continued_p)
15900 return best_row;
15901 ++row;
15902 }
15903 }
15904
15905
15906 /* Try to redisplay window W by reusing its existing display. W's
15907 current matrix must be up to date when this function is called,
15908 i.e. window_end_valid must not be nil.
15909
15910 Value is
15911
15912 1 if display has been updated
15913 0 if otherwise unsuccessful
15914 -1 if redisplay with same window start is known not to succeed
15915
15916 The following steps are performed:
15917
15918 1. Find the last row in the current matrix of W that is not
15919 affected by changes at the start of current_buffer. If no such row
15920 is found, give up.
15921
15922 2. Find the first row in W's current matrix that is not affected by
15923 changes at the end of current_buffer. Maybe there is no such row.
15924
15925 3. Display lines beginning with the row + 1 found in step 1 to the
15926 row found in step 2 or, if step 2 didn't find a row, to the end of
15927 the window.
15928
15929 4. If cursor is not known to appear on the window, give up.
15930
15931 5. If display stopped at the row found in step 2, scroll the
15932 display and current matrix as needed.
15933
15934 6. Maybe display some lines at the end of W, if we must. This can
15935 happen under various circumstances, like a partially visible line
15936 becoming fully visible, or because newly displayed lines are displayed
15937 in smaller font sizes.
15938
15939 7. Update W's window end information. */
15940
15941 static int
15942 try_window_id (struct window *w)
15943 {
15944 struct frame *f = XFRAME (w->frame);
15945 struct glyph_matrix *current_matrix = w->current_matrix;
15946 struct glyph_matrix *desired_matrix = w->desired_matrix;
15947 struct glyph_row *last_unchanged_at_beg_row;
15948 struct glyph_row *first_unchanged_at_end_row;
15949 struct glyph_row *row;
15950 struct glyph_row *bottom_row;
15951 int bottom_vpos;
15952 struct it it;
15953 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15954 int dvpos, dy;
15955 struct text_pos start_pos;
15956 struct run run;
15957 int first_unchanged_at_end_vpos = 0;
15958 struct glyph_row *last_text_row, *last_text_row_at_end;
15959 struct text_pos start;
15960 EMACS_INT first_changed_charpos, last_changed_charpos;
15961
15962 #if GLYPH_DEBUG
15963 if (inhibit_try_window_id)
15964 return 0;
15965 #endif
15966
15967 /* This is handy for debugging. */
15968 #if 0
15969 #define GIVE_UP(X) \
15970 do { \
15971 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15972 return 0; \
15973 } while (0)
15974 #else
15975 #define GIVE_UP(X) return 0
15976 #endif
15977
15978 SET_TEXT_POS_FROM_MARKER (start, w->start);
15979
15980 /* Don't use this for mini-windows because these can show
15981 messages and mini-buffers, and we don't handle that here. */
15982 if (MINI_WINDOW_P (w))
15983 GIVE_UP (1);
15984
15985 /* This flag is used to prevent redisplay optimizations. */
15986 if (windows_or_buffers_changed || cursor_type_changed)
15987 GIVE_UP (2);
15988
15989 /* Verify that narrowing has not changed.
15990 Also verify that we were not told to prevent redisplay optimizations.
15991 It would be nice to further
15992 reduce the number of cases where this prevents try_window_id. */
15993 if (current_buffer->clip_changed
15994 || current_buffer->prevent_redisplay_optimizations_p)
15995 GIVE_UP (3);
15996
15997 /* Window must either use window-based redisplay or be full width. */
15998 if (!FRAME_WINDOW_P (f)
15999 && (!FRAME_LINE_INS_DEL_OK (f)
16000 || !WINDOW_FULL_WIDTH_P (w)))
16001 GIVE_UP (4);
16002
16003 /* Give up if point is known NOT to appear in W. */
16004 if (PT < CHARPOS (start))
16005 GIVE_UP (5);
16006
16007 /* Another way to prevent redisplay optimizations. */
16008 if (XFASTINT (w->last_modified) == 0)
16009 GIVE_UP (6);
16010
16011 /* Verify that window is not hscrolled. */
16012 if (XFASTINT (w->hscroll) != 0)
16013 GIVE_UP (7);
16014
16015 /* Verify that display wasn't paused. */
16016 if (NILP (w->window_end_valid))
16017 GIVE_UP (8);
16018
16019 /* Can't use this if highlighting a region because a cursor movement
16020 will do more than just set the cursor. */
16021 if (!NILP (Vtransient_mark_mode)
16022 && !NILP (BVAR (current_buffer, mark_active)))
16023 GIVE_UP (9);
16024
16025 /* Likewise if highlighting trailing whitespace. */
16026 if (!NILP (Vshow_trailing_whitespace))
16027 GIVE_UP (11);
16028
16029 /* Likewise if showing a region. */
16030 if (!NILP (w->region_showing))
16031 GIVE_UP (10);
16032
16033 /* Can't use this if overlay arrow position and/or string have
16034 changed. */
16035 if (overlay_arrows_changed_p ())
16036 GIVE_UP (12);
16037
16038 /* When word-wrap is on, adding a space to the first word of a
16039 wrapped line can change the wrap position, altering the line
16040 above it. It might be worthwhile to handle this more
16041 intelligently, but for now just redisplay from scratch. */
16042 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16043 GIVE_UP (21);
16044
16045 /* Under bidi reordering, adding or deleting a character in the
16046 beginning of a paragraph, before the first strong directional
16047 character, can change the base direction of the paragraph (unless
16048 the buffer specifies a fixed paragraph direction), which will
16049 require to redisplay the whole paragraph. It might be worthwhile
16050 to find the paragraph limits and widen the range of redisplayed
16051 lines to that, but for now just give up this optimization and
16052 redisplay from scratch. */
16053 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16054 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16055 GIVE_UP (22);
16056
16057 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16058 only if buffer has really changed. The reason is that the gap is
16059 initially at Z for freshly visited files. The code below would
16060 set end_unchanged to 0 in that case. */
16061 if (MODIFF > SAVE_MODIFF
16062 /* This seems to happen sometimes after saving a buffer. */
16063 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16064 {
16065 if (GPT - BEG < BEG_UNCHANGED)
16066 BEG_UNCHANGED = GPT - BEG;
16067 if (Z - GPT < END_UNCHANGED)
16068 END_UNCHANGED = Z - GPT;
16069 }
16070
16071 /* The position of the first and last character that has been changed. */
16072 first_changed_charpos = BEG + BEG_UNCHANGED;
16073 last_changed_charpos = Z - END_UNCHANGED;
16074
16075 /* If window starts after a line end, and the last change is in
16076 front of that newline, then changes don't affect the display.
16077 This case happens with stealth-fontification. Note that although
16078 the display is unchanged, glyph positions in the matrix have to
16079 be adjusted, of course. */
16080 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16081 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16082 && ((last_changed_charpos < CHARPOS (start)
16083 && CHARPOS (start) == BEGV)
16084 || (last_changed_charpos < CHARPOS (start) - 1
16085 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16086 {
16087 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16088 struct glyph_row *r0;
16089
16090 /* Compute how many chars/bytes have been added to or removed
16091 from the buffer. */
16092 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16093 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16094 Z_delta = Z - Z_old;
16095 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16096
16097 /* Give up if PT is not in the window. Note that it already has
16098 been checked at the start of try_window_id that PT is not in
16099 front of the window start. */
16100 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16101 GIVE_UP (13);
16102
16103 /* If window start is unchanged, we can reuse the whole matrix
16104 as is, after adjusting glyph positions. No need to compute
16105 the window end again, since its offset from Z hasn't changed. */
16106 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16107 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16108 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16109 /* PT must not be in a partially visible line. */
16110 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16111 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16112 {
16113 /* Adjust positions in the glyph matrix. */
16114 if (Z_delta || Z_delta_bytes)
16115 {
16116 struct glyph_row *r1
16117 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16118 increment_matrix_positions (w->current_matrix,
16119 MATRIX_ROW_VPOS (r0, current_matrix),
16120 MATRIX_ROW_VPOS (r1, current_matrix),
16121 Z_delta, Z_delta_bytes);
16122 }
16123
16124 /* Set the cursor. */
16125 row = row_containing_pos (w, PT, r0, NULL, 0);
16126 if (row)
16127 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16128 else
16129 abort ();
16130 return 1;
16131 }
16132 }
16133
16134 /* Handle the case that changes are all below what is displayed in
16135 the window, and that PT is in the window. This shortcut cannot
16136 be taken if ZV is visible in the window, and text has been added
16137 there that is visible in the window. */
16138 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16139 /* ZV is not visible in the window, or there are no
16140 changes at ZV, actually. */
16141 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16142 || first_changed_charpos == last_changed_charpos))
16143 {
16144 struct glyph_row *r0;
16145
16146 /* Give up if PT is not in the window. Note that it already has
16147 been checked at the start of try_window_id that PT is not in
16148 front of the window start. */
16149 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16150 GIVE_UP (14);
16151
16152 /* If window start is unchanged, we can reuse the whole matrix
16153 as is, without changing glyph positions since no text has
16154 been added/removed in front of the window end. */
16155 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16156 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16157 /* PT must not be in a partially visible line. */
16158 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16159 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16160 {
16161 /* We have to compute the window end anew since text
16162 could have been added/removed after it. */
16163 w->window_end_pos
16164 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16165 w->window_end_bytepos
16166 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16167
16168 /* Set the cursor. */
16169 row = row_containing_pos (w, PT, r0, NULL, 0);
16170 if (row)
16171 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16172 else
16173 abort ();
16174 return 2;
16175 }
16176 }
16177
16178 /* Give up if window start is in the changed area.
16179
16180 The condition used to read
16181
16182 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16183
16184 but why that was tested escapes me at the moment. */
16185 if (CHARPOS (start) >= first_changed_charpos
16186 && CHARPOS (start) <= last_changed_charpos)
16187 GIVE_UP (15);
16188
16189 /* Check that window start agrees with the start of the first glyph
16190 row in its current matrix. Check this after we know the window
16191 start is not in changed text, otherwise positions would not be
16192 comparable. */
16193 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16194 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16195 GIVE_UP (16);
16196
16197 /* Give up if the window ends in strings. Overlay strings
16198 at the end are difficult to handle, so don't try. */
16199 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16200 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16201 GIVE_UP (20);
16202
16203 /* Compute the position at which we have to start displaying new
16204 lines. Some of the lines at the top of the window might be
16205 reusable because they are not displaying changed text. Find the
16206 last row in W's current matrix not affected by changes at the
16207 start of current_buffer. Value is null if changes start in the
16208 first line of window. */
16209 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16210 if (last_unchanged_at_beg_row)
16211 {
16212 /* Avoid starting to display in the moddle of a character, a TAB
16213 for instance. This is easier than to set up the iterator
16214 exactly, and it's not a frequent case, so the additional
16215 effort wouldn't really pay off. */
16216 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16217 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16218 && last_unchanged_at_beg_row > w->current_matrix->rows)
16219 --last_unchanged_at_beg_row;
16220
16221 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16222 GIVE_UP (17);
16223
16224 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16225 GIVE_UP (18);
16226 start_pos = it.current.pos;
16227
16228 /* Start displaying new lines in the desired matrix at the same
16229 vpos we would use in the current matrix, i.e. below
16230 last_unchanged_at_beg_row. */
16231 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16232 current_matrix);
16233 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16234 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16235
16236 xassert (it.hpos == 0 && it.current_x == 0);
16237 }
16238 else
16239 {
16240 /* There are no reusable lines at the start of the window.
16241 Start displaying in the first text line. */
16242 start_display (&it, w, start);
16243 it.vpos = it.first_vpos;
16244 start_pos = it.current.pos;
16245 }
16246
16247 /* Find the first row that is not affected by changes at the end of
16248 the buffer. Value will be null if there is no unchanged row, in
16249 which case we must redisplay to the end of the window. delta
16250 will be set to the value by which buffer positions beginning with
16251 first_unchanged_at_end_row have to be adjusted due to text
16252 changes. */
16253 first_unchanged_at_end_row
16254 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16255 IF_DEBUG (debug_delta = delta);
16256 IF_DEBUG (debug_delta_bytes = delta_bytes);
16257
16258 /* Set stop_pos to the buffer position up to which we will have to
16259 display new lines. If first_unchanged_at_end_row != NULL, this
16260 is the buffer position of the start of the line displayed in that
16261 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16262 that we don't stop at a buffer position. */
16263 stop_pos = 0;
16264 if (first_unchanged_at_end_row)
16265 {
16266 xassert (last_unchanged_at_beg_row == NULL
16267 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16268
16269 /* If this is a continuation line, move forward to the next one
16270 that isn't. Changes in lines above affect this line.
16271 Caution: this may move first_unchanged_at_end_row to a row
16272 not displaying text. */
16273 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16274 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16275 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16276 < it.last_visible_y))
16277 ++first_unchanged_at_end_row;
16278
16279 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16280 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16281 >= it.last_visible_y))
16282 first_unchanged_at_end_row = NULL;
16283 else
16284 {
16285 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16286 + delta);
16287 first_unchanged_at_end_vpos
16288 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16289 xassert (stop_pos >= Z - END_UNCHANGED);
16290 }
16291 }
16292 else if (last_unchanged_at_beg_row == NULL)
16293 GIVE_UP (19);
16294
16295
16296 #if GLYPH_DEBUG
16297
16298 /* Either there is no unchanged row at the end, or the one we have
16299 now displays text. This is a necessary condition for the window
16300 end pos calculation at the end of this function. */
16301 xassert (first_unchanged_at_end_row == NULL
16302 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16303
16304 debug_last_unchanged_at_beg_vpos
16305 = (last_unchanged_at_beg_row
16306 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16307 : -1);
16308 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16309
16310 #endif /* GLYPH_DEBUG != 0 */
16311
16312
16313 /* Display new lines. Set last_text_row to the last new line
16314 displayed which has text on it, i.e. might end up as being the
16315 line where the window_end_vpos is. */
16316 w->cursor.vpos = -1;
16317 last_text_row = NULL;
16318 overlay_arrow_seen = 0;
16319 while (it.current_y < it.last_visible_y
16320 && !fonts_changed_p
16321 && (first_unchanged_at_end_row == NULL
16322 || IT_CHARPOS (it) < stop_pos))
16323 {
16324 if (display_line (&it))
16325 last_text_row = it.glyph_row - 1;
16326 }
16327
16328 if (fonts_changed_p)
16329 return -1;
16330
16331
16332 /* Compute differences in buffer positions, y-positions etc. for
16333 lines reused at the bottom of the window. Compute what we can
16334 scroll. */
16335 if (first_unchanged_at_end_row
16336 /* No lines reused because we displayed everything up to the
16337 bottom of the window. */
16338 && it.current_y < it.last_visible_y)
16339 {
16340 dvpos = (it.vpos
16341 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16342 current_matrix));
16343 dy = it.current_y - first_unchanged_at_end_row->y;
16344 run.current_y = first_unchanged_at_end_row->y;
16345 run.desired_y = run.current_y + dy;
16346 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16347 }
16348 else
16349 {
16350 delta = delta_bytes = dvpos = dy
16351 = run.current_y = run.desired_y = run.height = 0;
16352 first_unchanged_at_end_row = NULL;
16353 }
16354 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16355
16356
16357 /* Find the cursor if not already found. We have to decide whether
16358 PT will appear on this window (it sometimes doesn't, but this is
16359 not a very frequent case.) This decision has to be made before
16360 the current matrix is altered. A value of cursor.vpos < 0 means
16361 that PT is either in one of the lines beginning at
16362 first_unchanged_at_end_row or below the window. Don't care for
16363 lines that might be displayed later at the window end; as
16364 mentioned, this is not a frequent case. */
16365 if (w->cursor.vpos < 0)
16366 {
16367 /* Cursor in unchanged rows at the top? */
16368 if (PT < CHARPOS (start_pos)
16369 && last_unchanged_at_beg_row)
16370 {
16371 row = row_containing_pos (w, PT,
16372 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16373 last_unchanged_at_beg_row + 1, 0);
16374 if (row)
16375 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16376 }
16377
16378 /* Start from first_unchanged_at_end_row looking for PT. */
16379 else if (first_unchanged_at_end_row)
16380 {
16381 row = row_containing_pos (w, PT - delta,
16382 first_unchanged_at_end_row, NULL, 0);
16383 if (row)
16384 set_cursor_from_row (w, row, w->current_matrix, delta,
16385 delta_bytes, dy, dvpos);
16386 }
16387
16388 /* Give up if cursor was not found. */
16389 if (w->cursor.vpos < 0)
16390 {
16391 clear_glyph_matrix (w->desired_matrix);
16392 return -1;
16393 }
16394 }
16395
16396 /* Don't let the cursor end in the scroll margins. */
16397 {
16398 int this_scroll_margin, cursor_height;
16399
16400 this_scroll_margin = max (0, scroll_margin);
16401 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16402 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16403 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16404
16405 if ((w->cursor.y < this_scroll_margin
16406 && CHARPOS (start) > BEGV)
16407 /* Old redisplay didn't take scroll margin into account at the bottom,
16408 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16409 || (w->cursor.y + (make_cursor_line_fully_visible_p
16410 ? cursor_height + this_scroll_margin
16411 : 1)) > it.last_visible_y)
16412 {
16413 w->cursor.vpos = -1;
16414 clear_glyph_matrix (w->desired_matrix);
16415 return -1;
16416 }
16417 }
16418
16419 /* Scroll the display. Do it before changing the current matrix so
16420 that xterm.c doesn't get confused about where the cursor glyph is
16421 found. */
16422 if (dy && run.height)
16423 {
16424 update_begin (f);
16425
16426 if (FRAME_WINDOW_P (f))
16427 {
16428 FRAME_RIF (f)->update_window_begin_hook (w);
16429 FRAME_RIF (f)->clear_window_mouse_face (w);
16430 FRAME_RIF (f)->scroll_run_hook (w, &run);
16431 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16432 }
16433 else
16434 {
16435 /* Terminal frame. In this case, dvpos gives the number of
16436 lines to scroll by; dvpos < 0 means scroll up. */
16437 int from_vpos
16438 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16439 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16440 int end = (WINDOW_TOP_EDGE_LINE (w)
16441 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16442 + window_internal_height (w));
16443
16444 #if defined (HAVE_GPM) || defined (MSDOS)
16445 x_clear_window_mouse_face (w);
16446 #endif
16447 /* Perform the operation on the screen. */
16448 if (dvpos > 0)
16449 {
16450 /* Scroll last_unchanged_at_beg_row to the end of the
16451 window down dvpos lines. */
16452 set_terminal_window (f, end);
16453
16454 /* On dumb terminals delete dvpos lines at the end
16455 before inserting dvpos empty lines. */
16456 if (!FRAME_SCROLL_REGION_OK (f))
16457 ins_del_lines (f, end - dvpos, -dvpos);
16458
16459 /* Insert dvpos empty lines in front of
16460 last_unchanged_at_beg_row. */
16461 ins_del_lines (f, from, dvpos);
16462 }
16463 else if (dvpos < 0)
16464 {
16465 /* Scroll up last_unchanged_at_beg_vpos to the end of
16466 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16467 set_terminal_window (f, end);
16468
16469 /* Delete dvpos lines in front of
16470 last_unchanged_at_beg_vpos. ins_del_lines will set
16471 the cursor to the given vpos and emit |dvpos| delete
16472 line sequences. */
16473 ins_del_lines (f, from + dvpos, dvpos);
16474
16475 /* On a dumb terminal insert dvpos empty lines at the
16476 end. */
16477 if (!FRAME_SCROLL_REGION_OK (f))
16478 ins_del_lines (f, end + dvpos, -dvpos);
16479 }
16480
16481 set_terminal_window (f, 0);
16482 }
16483
16484 update_end (f);
16485 }
16486
16487 /* Shift reused rows of the current matrix to the right position.
16488 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16489 text. */
16490 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16491 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16492 if (dvpos < 0)
16493 {
16494 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16495 bottom_vpos, dvpos);
16496 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16497 bottom_vpos, 0);
16498 }
16499 else if (dvpos > 0)
16500 {
16501 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16502 bottom_vpos, dvpos);
16503 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16504 first_unchanged_at_end_vpos + dvpos, 0);
16505 }
16506
16507 /* For frame-based redisplay, make sure that current frame and window
16508 matrix are in sync with respect to glyph memory. */
16509 if (!FRAME_WINDOW_P (f))
16510 sync_frame_with_window_matrix_rows (w);
16511
16512 /* Adjust buffer positions in reused rows. */
16513 if (delta || delta_bytes)
16514 increment_matrix_positions (current_matrix,
16515 first_unchanged_at_end_vpos + dvpos,
16516 bottom_vpos, delta, delta_bytes);
16517
16518 /* Adjust Y positions. */
16519 if (dy)
16520 shift_glyph_matrix (w, current_matrix,
16521 first_unchanged_at_end_vpos + dvpos,
16522 bottom_vpos, dy);
16523
16524 if (first_unchanged_at_end_row)
16525 {
16526 first_unchanged_at_end_row += dvpos;
16527 if (first_unchanged_at_end_row->y >= it.last_visible_y
16528 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16529 first_unchanged_at_end_row = NULL;
16530 }
16531
16532 /* If scrolling up, there may be some lines to display at the end of
16533 the window. */
16534 last_text_row_at_end = NULL;
16535 if (dy < 0)
16536 {
16537 /* Scrolling up can leave for example a partially visible line
16538 at the end of the window to be redisplayed. */
16539 /* Set last_row to the glyph row in the current matrix where the
16540 window end line is found. It has been moved up or down in
16541 the matrix by dvpos. */
16542 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16543 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16544
16545 /* If last_row is the window end line, it should display text. */
16546 xassert (last_row->displays_text_p);
16547
16548 /* If window end line was partially visible before, begin
16549 displaying at that line. Otherwise begin displaying with the
16550 line following it. */
16551 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16552 {
16553 init_to_row_start (&it, w, last_row);
16554 it.vpos = last_vpos;
16555 it.current_y = last_row->y;
16556 }
16557 else
16558 {
16559 init_to_row_end (&it, w, last_row);
16560 it.vpos = 1 + last_vpos;
16561 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16562 ++last_row;
16563 }
16564
16565 /* We may start in a continuation line. If so, we have to
16566 get the right continuation_lines_width and current_x. */
16567 it.continuation_lines_width = last_row->continuation_lines_width;
16568 it.hpos = it.current_x = 0;
16569
16570 /* Display the rest of the lines at the window end. */
16571 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16572 while (it.current_y < it.last_visible_y
16573 && !fonts_changed_p)
16574 {
16575 /* Is it always sure that the display agrees with lines in
16576 the current matrix? I don't think so, so we mark rows
16577 displayed invalid in the current matrix by setting their
16578 enabled_p flag to zero. */
16579 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16580 if (display_line (&it))
16581 last_text_row_at_end = it.glyph_row - 1;
16582 }
16583 }
16584
16585 /* Update window_end_pos and window_end_vpos. */
16586 if (first_unchanged_at_end_row
16587 && !last_text_row_at_end)
16588 {
16589 /* Window end line if one of the preserved rows from the current
16590 matrix. Set row to the last row displaying text in current
16591 matrix starting at first_unchanged_at_end_row, after
16592 scrolling. */
16593 xassert (first_unchanged_at_end_row->displays_text_p);
16594 row = find_last_row_displaying_text (w->current_matrix, &it,
16595 first_unchanged_at_end_row);
16596 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16597
16598 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16599 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16600 w->window_end_vpos
16601 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16602 xassert (w->window_end_bytepos >= 0);
16603 IF_DEBUG (debug_method_add (w, "A"));
16604 }
16605 else if (last_text_row_at_end)
16606 {
16607 w->window_end_pos
16608 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16609 w->window_end_bytepos
16610 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16611 w->window_end_vpos
16612 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16613 xassert (w->window_end_bytepos >= 0);
16614 IF_DEBUG (debug_method_add (w, "B"));
16615 }
16616 else if (last_text_row)
16617 {
16618 /* We have displayed either to the end of the window or at the
16619 end of the window, i.e. the last row with text is to be found
16620 in the desired matrix. */
16621 w->window_end_pos
16622 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16623 w->window_end_bytepos
16624 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16625 w->window_end_vpos
16626 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16627 xassert (w->window_end_bytepos >= 0);
16628 }
16629 else if (first_unchanged_at_end_row == NULL
16630 && last_text_row == NULL
16631 && last_text_row_at_end == NULL)
16632 {
16633 /* Displayed to end of window, but no line containing text was
16634 displayed. Lines were deleted at the end of the window. */
16635 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16636 int vpos = XFASTINT (w->window_end_vpos);
16637 struct glyph_row *current_row = current_matrix->rows + vpos;
16638 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16639
16640 for (row = NULL;
16641 row == NULL && vpos >= first_vpos;
16642 --vpos, --current_row, --desired_row)
16643 {
16644 if (desired_row->enabled_p)
16645 {
16646 if (desired_row->displays_text_p)
16647 row = desired_row;
16648 }
16649 else if (current_row->displays_text_p)
16650 row = current_row;
16651 }
16652
16653 xassert (row != NULL);
16654 w->window_end_vpos = make_number (vpos + 1);
16655 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16656 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16657 xassert (w->window_end_bytepos >= 0);
16658 IF_DEBUG (debug_method_add (w, "C"));
16659 }
16660 else
16661 abort ();
16662
16663 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16664 debug_end_vpos = XFASTINT (w->window_end_vpos));
16665
16666 /* Record that display has not been completed. */
16667 w->window_end_valid = Qnil;
16668 w->desired_matrix->no_scrolling_p = 1;
16669 return 3;
16670
16671 #undef GIVE_UP
16672 }
16673
16674
16675 \f
16676 /***********************************************************************
16677 More debugging support
16678 ***********************************************************************/
16679
16680 #if GLYPH_DEBUG
16681
16682 void dump_glyph_row (struct glyph_row *, int, int);
16683 void dump_glyph_matrix (struct glyph_matrix *, int);
16684 void dump_glyph (struct glyph_row *, struct glyph *, int);
16685
16686
16687 /* Dump the contents of glyph matrix MATRIX on stderr.
16688
16689 GLYPHS 0 means don't show glyph contents.
16690 GLYPHS 1 means show glyphs in short form
16691 GLYPHS > 1 means show glyphs in long form. */
16692
16693 void
16694 dump_glyph_matrix (matrix, glyphs)
16695 struct glyph_matrix *matrix;
16696 int glyphs;
16697 {
16698 int i;
16699 for (i = 0; i < matrix->nrows; ++i)
16700 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16701 }
16702
16703
16704 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16705 the glyph row and area where the glyph comes from. */
16706
16707 void
16708 dump_glyph (row, glyph, area)
16709 struct glyph_row *row;
16710 struct glyph *glyph;
16711 int area;
16712 {
16713 if (glyph->type == CHAR_GLYPH)
16714 {
16715 fprintf (stderr,
16716 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16717 glyph - row->glyphs[TEXT_AREA],
16718 'C',
16719 glyph->charpos,
16720 (BUFFERP (glyph->object)
16721 ? 'B'
16722 : (STRINGP (glyph->object)
16723 ? 'S'
16724 : '-')),
16725 glyph->pixel_width,
16726 glyph->u.ch,
16727 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16728 ? glyph->u.ch
16729 : '.'),
16730 glyph->face_id,
16731 glyph->left_box_line_p,
16732 glyph->right_box_line_p);
16733 }
16734 else if (glyph->type == STRETCH_GLYPH)
16735 {
16736 fprintf (stderr,
16737 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16738 glyph - row->glyphs[TEXT_AREA],
16739 'S',
16740 glyph->charpos,
16741 (BUFFERP (glyph->object)
16742 ? 'B'
16743 : (STRINGP (glyph->object)
16744 ? 'S'
16745 : '-')),
16746 glyph->pixel_width,
16747 0,
16748 '.',
16749 glyph->face_id,
16750 glyph->left_box_line_p,
16751 glyph->right_box_line_p);
16752 }
16753 else if (glyph->type == IMAGE_GLYPH)
16754 {
16755 fprintf (stderr,
16756 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16757 glyph - row->glyphs[TEXT_AREA],
16758 'I',
16759 glyph->charpos,
16760 (BUFFERP (glyph->object)
16761 ? 'B'
16762 : (STRINGP (glyph->object)
16763 ? 'S'
16764 : '-')),
16765 glyph->pixel_width,
16766 glyph->u.img_id,
16767 '.',
16768 glyph->face_id,
16769 glyph->left_box_line_p,
16770 glyph->right_box_line_p);
16771 }
16772 else if (glyph->type == COMPOSITE_GLYPH)
16773 {
16774 fprintf (stderr,
16775 " %5d %4c %6d %c %3d 0x%05x",
16776 glyph - row->glyphs[TEXT_AREA],
16777 '+',
16778 glyph->charpos,
16779 (BUFFERP (glyph->object)
16780 ? 'B'
16781 : (STRINGP (glyph->object)
16782 ? 'S'
16783 : '-')),
16784 glyph->pixel_width,
16785 glyph->u.cmp.id);
16786 if (glyph->u.cmp.automatic)
16787 fprintf (stderr,
16788 "[%d-%d]",
16789 glyph->slice.cmp.from, glyph->slice.cmp.to);
16790 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16791 glyph->face_id,
16792 glyph->left_box_line_p,
16793 glyph->right_box_line_p);
16794 }
16795 }
16796
16797
16798 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16799 GLYPHS 0 means don't show glyph contents.
16800 GLYPHS 1 means show glyphs in short form
16801 GLYPHS > 1 means show glyphs in long form. */
16802
16803 void
16804 dump_glyph_row (row, vpos, glyphs)
16805 struct glyph_row *row;
16806 int vpos, glyphs;
16807 {
16808 if (glyphs != 1)
16809 {
16810 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16811 fprintf (stderr, "======================================================================\n");
16812
16813 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16814 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16815 vpos,
16816 MATRIX_ROW_START_CHARPOS (row),
16817 MATRIX_ROW_END_CHARPOS (row),
16818 row->used[TEXT_AREA],
16819 row->contains_overlapping_glyphs_p,
16820 row->enabled_p,
16821 row->truncated_on_left_p,
16822 row->truncated_on_right_p,
16823 row->continued_p,
16824 MATRIX_ROW_CONTINUATION_LINE_P (row),
16825 row->displays_text_p,
16826 row->ends_at_zv_p,
16827 row->fill_line_p,
16828 row->ends_in_middle_of_char_p,
16829 row->starts_in_middle_of_char_p,
16830 row->mouse_face_p,
16831 row->x,
16832 row->y,
16833 row->pixel_width,
16834 row->height,
16835 row->visible_height,
16836 row->ascent,
16837 row->phys_ascent);
16838 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16839 row->end.overlay_string_index,
16840 row->continuation_lines_width);
16841 fprintf (stderr, "%9d %5d\n",
16842 CHARPOS (row->start.string_pos),
16843 CHARPOS (row->end.string_pos));
16844 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16845 row->end.dpvec_index);
16846 }
16847
16848 if (glyphs > 1)
16849 {
16850 int area;
16851
16852 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16853 {
16854 struct glyph *glyph = row->glyphs[area];
16855 struct glyph *glyph_end = glyph + row->used[area];
16856
16857 /* Glyph for a line end in text. */
16858 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16859 ++glyph_end;
16860
16861 if (glyph < glyph_end)
16862 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16863
16864 for (; glyph < glyph_end; ++glyph)
16865 dump_glyph (row, glyph, area);
16866 }
16867 }
16868 else if (glyphs == 1)
16869 {
16870 int area;
16871
16872 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16873 {
16874 char *s = (char *) alloca (row->used[area] + 1);
16875 int i;
16876
16877 for (i = 0; i < row->used[area]; ++i)
16878 {
16879 struct glyph *glyph = row->glyphs[area] + i;
16880 if (glyph->type == CHAR_GLYPH
16881 && glyph->u.ch < 0x80
16882 && glyph->u.ch >= ' ')
16883 s[i] = glyph->u.ch;
16884 else
16885 s[i] = '.';
16886 }
16887
16888 s[i] = '\0';
16889 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16890 }
16891 }
16892 }
16893
16894
16895 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16896 Sdump_glyph_matrix, 0, 1, "p",
16897 doc: /* Dump the current matrix of the selected window to stderr.
16898 Shows contents of glyph row structures. With non-nil
16899 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16900 glyphs in short form, otherwise show glyphs in long form. */)
16901 (Lisp_Object glyphs)
16902 {
16903 struct window *w = XWINDOW (selected_window);
16904 struct buffer *buffer = XBUFFER (w->buffer);
16905
16906 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16907 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16908 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16909 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16910 fprintf (stderr, "=============================================\n");
16911 dump_glyph_matrix (w->current_matrix,
16912 NILP (glyphs) ? 0 : XINT (glyphs));
16913 return Qnil;
16914 }
16915
16916
16917 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16918 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16919 (void)
16920 {
16921 struct frame *f = XFRAME (selected_frame);
16922 dump_glyph_matrix (f->current_matrix, 1);
16923 return Qnil;
16924 }
16925
16926
16927 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16928 doc: /* Dump glyph row ROW to stderr.
16929 GLYPH 0 means don't dump glyphs.
16930 GLYPH 1 means dump glyphs in short form.
16931 GLYPH > 1 or omitted means dump glyphs in long form. */)
16932 (Lisp_Object row, Lisp_Object glyphs)
16933 {
16934 struct glyph_matrix *matrix;
16935 int vpos;
16936
16937 CHECK_NUMBER (row);
16938 matrix = XWINDOW (selected_window)->current_matrix;
16939 vpos = XINT (row);
16940 if (vpos >= 0 && vpos < matrix->nrows)
16941 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16942 vpos,
16943 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16944 return Qnil;
16945 }
16946
16947
16948 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16949 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16950 GLYPH 0 means don't dump glyphs.
16951 GLYPH 1 means dump glyphs in short form.
16952 GLYPH > 1 or omitted means dump glyphs in long form. */)
16953 (Lisp_Object row, Lisp_Object glyphs)
16954 {
16955 struct frame *sf = SELECTED_FRAME ();
16956 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16957 int vpos;
16958
16959 CHECK_NUMBER (row);
16960 vpos = XINT (row);
16961 if (vpos >= 0 && vpos < m->nrows)
16962 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16963 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16964 return Qnil;
16965 }
16966
16967
16968 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16969 doc: /* Toggle tracing of redisplay.
16970 With ARG, turn tracing on if and only if ARG is positive. */)
16971 (Lisp_Object arg)
16972 {
16973 if (NILP (arg))
16974 trace_redisplay_p = !trace_redisplay_p;
16975 else
16976 {
16977 arg = Fprefix_numeric_value (arg);
16978 trace_redisplay_p = XINT (arg) > 0;
16979 }
16980
16981 return Qnil;
16982 }
16983
16984
16985 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16986 doc: /* Like `format', but print result to stderr.
16987 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16988 (size_t nargs, Lisp_Object *args)
16989 {
16990 Lisp_Object s = Fformat (nargs, args);
16991 fprintf (stderr, "%s", SDATA (s));
16992 return Qnil;
16993 }
16994
16995 #endif /* GLYPH_DEBUG */
16996
16997
16998 \f
16999 /***********************************************************************
17000 Building Desired Matrix Rows
17001 ***********************************************************************/
17002
17003 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17004 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17005
17006 static struct glyph_row *
17007 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17008 {
17009 struct frame *f = XFRAME (WINDOW_FRAME (w));
17010 struct buffer *buffer = XBUFFER (w->buffer);
17011 struct buffer *old = current_buffer;
17012 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17013 int arrow_len = SCHARS (overlay_arrow_string);
17014 const unsigned char *arrow_end = arrow_string + arrow_len;
17015 const unsigned char *p;
17016 struct it it;
17017 int multibyte_p;
17018 int n_glyphs_before;
17019
17020 set_buffer_temp (buffer);
17021 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17022 it.glyph_row->used[TEXT_AREA] = 0;
17023 SET_TEXT_POS (it.position, 0, 0);
17024
17025 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17026 p = arrow_string;
17027 while (p < arrow_end)
17028 {
17029 Lisp_Object face, ilisp;
17030
17031 /* Get the next character. */
17032 if (multibyte_p)
17033 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17034 else
17035 {
17036 it.c = it.char_to_display = *p, it.len = 1;
17037 if (! ASCII_CHAR_P (it.c))
17038 it.char_to_display = BYTE8_TO_CHAR (it.c);
17039 }
17040 p += it.len;
17041
17042 /* Get its face. */
17043 ilisp = make_number (p - arrow_string);
17044 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17045 it.face_id = compute_char_face (f, it.char_to_display, face);
17046
17047 /* Compute its width, get its glyphs. */
17048 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17049 SET_TEXT_POS (it.position, -1, -1);
17050 PRODUCE_GLYPHS (&it);
17051
17052 /* If this character doesn't fit any more in the line, we have
17053 to remove some glyphs. */
17054 if (it.current_x > it.last_visible_x)
17055 {
17056 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17057 break;
17058 }
17059 }
17060
17061 set_buffer_temp (old);
17062 return it.glyph_row;
17063 }
17064
17065
17066 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17067 glyphs are only inserted for terminal frames since we can't really
17068 win with truncation glyphs when partially visible glyphs are
17069 involved. Which glyphs to insert is determined by
17070 produce_special_glyphs. */
17071
17072 static void
17073 insert_left_trunc_glyphs (struct it *it)
17074 {
17075 struct it truncate_it;
17076 struct glyph *from, *end, *to, *toend;
17077
17078 xassert (!FRAME_WINDOW_P (it->f));
17079
17080 /* Get the truncation glyphs. */
17081 truncate_it = *it;
17082 truncate_it.current_x = 0;
17083 truncate_it.face_id = DEFAULT_FACE_ID;
17084 truncate_it.glyph_row = &scratch_glyph_row;
17085 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17086 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17087 truncate_it.object = make_number (0);
17088 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17089
17090 /* Overwrite glyphs from IT with truncation glyphs. */
17091 if (!it->glyph_row->reversed_p)
17092 {
17093 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17094 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17095 to = it->glyph_row->glyphs[TEXT_AREA];
17096 toend = to + it->glyph_row->used[TEXT_AREA];
17097
17098 while (from < end)
17099 *to++ = *from++;
17100
17101 /* There may be padding glyphs left over. Overwrite them too. */
17102 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17103 {
17104 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17105 while (from < end)
17106 *to++ = *from++;
17107 }
17108
17109 if (to > toend)
17110 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17111 }
17112 else
17113 {
17114 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17115 that back to front. */
17116 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17117 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17118 toend = it->glyph_row->glyphs[TEXT_AREA];
17119 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17120
17121 while (from >= end && to >= toend)
17122 *to-- = *from--;
17123 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17124 {
17125 from =
17126 truncate_it.glyph_row->glyphs[TEXT_AREA]
17127 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17128 while (from >= end && to >= toend)
17129 *to-- = *from--;
17130 }
17131 if (from >= end)
17132 {
17133 /* Need to free some room before prepending additional
17134 glyphs. */
17135 int move_by = from - end + 1;
17136 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17137 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17138
17139 for ( ; g >= g0; g--)
17140 g[move_by] = *g;
17141 while (from >= end)
17142 *to-- = *from--;
17143 it->glyph_row->used[TEXT_AREA] += move_by;
17144 }
17145 }
17146 }
17147
17148
17149 /* Compute the pixel height and width of IT->glyph_row.
17150
17151 Most of the time, ascent and height of a display line will be equal
17152 to the max_ascent and max_height values of the display iterator
17153 structure. This is not the case if
17154
17155 1. We hit ZV without displaying anything. In this case, max_ascent
17156 and max_height will be zero.
17157
17158 2. We have some glyphs that don't contribute to the line height.
17159 (The glyph row flag contributes_to_line_height_p is for future
17160 pixmap extensions).
17161
17162 The first case is easily covered by using default values because in
17163 these cases, the line height does not really matter, except that it
17164 must not be zero. */
17165
17166 static void
17167 compute_line_metrics (struct it *it)
17168 {
17169 struct glyph_row *row = it->glyph_row;
17170
17171 if (FRAME_WINDOW_P (it->f))
17172 {
17173 int i, min_y, max_y;
17174
17175 /* The line may consist of one space only, that was added to
17176 place the cursor on it. If so, the row's height hasn't been
17177 computed yet. */
17178 if (row->height == 0)
17179 {
17180 if (it->max_ascent + it->max_descent == 0)
17181 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17182 row->ascent = it->max_ascent;
17183 row->height = it->max_ascent + it->max_descent;
17184 row->phys_ascent = it->max_phys_ascent;
17185 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17186 row->extra_line_spacing = it->max_extra_line_spacing;
17187 }
17188
17189 /* Compute the width of this line. */
17190 row->pixel_width = row->x;
17191 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17192 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17193
17194 xassert (row->pixel_width >= 0);
17195 xassert (row->ascent >= 0 && row->height > 0);
17196
17197 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17198 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17199
17200 /* If first line's physical ascent is larger than its logical
17201 ascent, use the physical ascent, and make the row taller.
17202 This makes accented characters fully visible. */
17203 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17204 && row->phys_ascent > row->ascent)
17205 {
17206 row->height += row->phys_ascent - row->ascent;
17207 row->ascent = row->phys_ascent;
17208 }
17209
17210 /* Compute how much of the line is visible. */
17211 row->visible_height = row->height;
17212
17213 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17214 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17215
17216 if (row->y < min_y)
17217 row->visible_height -= min_y - row->y;
17218 if (row->y + row->height > max_y)
17219 row->visible_height -= row->y + row->height - max_y;
17220 }
17221 else
17222 {
17223 row->pixel_width = row->used[TEXT_AREA];
17224 if (row->continued_p)
17225 row->pixel_width -= it->continuation_pixel_width;
17226 else if (row->truncated_on_right_p)
17227 row->pixel_width -= it->truncation_pixel_width;
17228 row->ascent = row->phys_ascent = 0;
17229 row->height = row->phys_height = row->visible_height = 1;
17230 row->extra_line_spacing = 0;
17231 }
17232
17233 /* Compute a hash code for this row. */
17234 {
17235 int area, i;
17236 row->hash = 0;
17237 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17238 for (i = 0; i < row->used[area]; ++i)
17239 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17240 + row->glyphs[area][i].u.val
17241 + row->glyphs[area][i].face_id
17242 + row->glyphs[area][i].padding_p
17243 + (row->glyphs[area][i].type << 2));
17244 }
17245
17246 it->max_ascent = it->max_descent = 0;
17247 it->max_phys_ascent = it->max_phys_descent = 0;
17248 }
17249
17250
17251 /* Append one space to the glyph row of iterator IT if doing a
17252 window-based redisplay. The space has the same face as
17253 IT->face_id. Value is non-zero if a space was added.
17254
17255 This function is called to make sure that there is always one glyph
17256 at the end of a glyph row that the cursor can be set on under
17257 window-systems. (If there weren't such a glyph we would not know
17258 how wide and tall a box cursor should be displayed).
17259
17260 At the same time this space let's a nicely handle clearing to the
17261 end of the line if the row ends in italic text. */
17262
17263 static int
17264 append_space_for_newline (struct it *it, int default_face_p)
17265 {
17266 if (FRAME_WINDOW_P (it->f))
17267 {
17268 int n = it->glyph_row->used[TEXT_AREA];
17269
17270 if (it->glyph_row->glyphs[TEXT_AREA] + n
17271 < it->glyph_row->glyphs[1 + TEXT_AREA])
17272 {
17273 /* Save some values that must not be changed.
17274 Must save IT->c and IT->len because otherwise
17275 ITERATOR_AT_END_P wouldn't work anymore after
17276 append_space_for_newline has been called. */
17277 enum display_element_type saved_what = it->what;
17278 int saved_c = it->c, saved_len = it->len;
17279 int saved_char_to_display = it->char_to_display;
17280 int saved_x = it->current_x;
17281 int saved_face_id = it->face_id;
17282 struct text_pos saved_pos;
17283 Lisp_Object saved_object;
17284 struct face *face;
17285
17286 saved_object = it->object;
17287 saved_pos = it->position;
17288
17289 it->what = IT_CHARACTER;
17290 memset (&it->position, 0, sizeof it->position);
17291 it->object = make_number (0);
17292 it->c = it->char_to_display = ' ';
17293 it->len = 1;
17294
17295 if (default_face_p)
17296 it->face_id = DEFAULT_FACE_ID;
17297 else if (it->face_before_selective_p)
17298 it->face_id = it->saved_face_id;
17299 face = FACE_FROM_ID (it->f, it->face_id);
17300 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17301
17302 PRODUCE_GLYPHS (it);
17303
17304 it->override_ascent = -1;
17305 it->constrain_row_ascent_descent_p = 0;
17306 it->current_x = saved_x;
17307 it->object = saved_object;
17308 it->position = saved_pos;
17309 it->what = saved_what;
17310 it->face_id = saved_face_id;
17311 it->len = saved_len;
17312 it->c = saved_c;
17313 it->char_to_display = saved_char_to_display;
17314 return 1;
17315 }
17316 }
17317
17318 return 0;
17319 }
17320
17321
17322 /* Extend the face of the last glyph in the text area of IT->glyph_row
17323 to the end of the display line. Called from display_line. If the
17324 glyph row is empty, add a space glyph to it so that we know the
17325 face to draw. Set the glyph row flag fill_line_p. If the glyph
17326 row is R2L, prepend a stretch glyph to cover the empty space to the
17327 left of the leftmost glyph. */
17328
17329 static void
17330 extend_face_to_end_of_line (struct it *it)
17331 {
17332 struct face *face;
17333 struct frame *f = it->f;
17334
17335 /* If line is already filled, do nothing. Non window-system frames
17336 get a grace of one more ``pixel'' because their characters are
17337 1-``pixel'' wide, so they hit the equality too early. This grace
17338 is needed only for R2L rows that are not continued, to produce
17339 one extra blank where we could display the cursor. */
17340 if (it->current_x >= it->last_visible_x
17341 + (!FRAME_WINDOW_P (f)
17342 && it->glyph_row->reversed_p
17343 && !it->glyph_row->continued_p))
17344 return;
17345
17346 /* Face extension extends the background and box of IT->face_id
17347 to the end of the line. If the background equals the background
17348 of the frame, we don't have to do anything. */
17349 if (it->face_before_selective_p)
17350 face = FACE_FROM_ID (f, it->saved_face_id);
17351 else
17352 face = FACE_FROM_ID (f, it->face_id);
17353
17354 if (FRAME_WINDOW_P (f)
17355 && it->glyph_row->displays_text_p
17356 && face->box == FACE_NO_BOX
17357 && face->background == FRAME_BACKGROUND_PIXEL (f)
17358 && !face->stipple
17359 && !it->glyph_row->reversed_p)
17360 return;
17361
17362 /* Set the glyph row flag indicating that the face of the last glyph
17363 in the text area has to be drawn to the end of the text area. */
17364 it->glyph_row->fill_line_p = 1;
17365
17366 /* If current character of IT is not ASCII, make sure we have the
17367 ASCII face. This will be automatically undone the next time
17368 get_next_display_element returns a multibyte character. Note
17369 that the character will always be single byte in unibyte
17370 text. */
17371 if (!ASCII_CHAR_P (it->c))
17372 {
17373 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17374 }
17375
17376 if (FRAME_WINDOW_P (f))
17377 {
17378 /* If the row is empty, add a space with the current face of IT,
17379 so that we know which face to draw. */
17380 if (it->glyph_row->used[TEXT_AREA] == 0)
17381 {
17382 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17383 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17384 it->glyph_row->used[TEXT_AREA] = 1;
17385 }
17386 #ifdef HAVE_WINDOW_SYSTEM
17387 if (it->glyph_row->reversed_p)
17388 {
17389 /* Prepend a stretch glyph to the row, such that the
17390 rightmost glyph will be drawn flushed all the way to the
17391 right margin of the window. The stretch glyph that will
17392 occupy the empty space, if any, to the left of the
17393 glyphs. */
17394 struct font *font = face->font ? face->font : FRAME_FONT (f);
17395 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17396 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17397 struct glyph *g;
17398 int row_width, stretch_ascent, stretch_width;
17399 struct text_pos saved_pos;
17400 int saved_face_id, saved_avoid_cursor;
17401
17402 for (row_width = 0, g = row_start; g < row_end; g++)
17403 row_width += g->pixel_width;
17404 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17405 if (stretch_width > 0)
17406 {
17407 stretch_ascent =
17408 (((it->ascent + it->descent)
17409 * FONT_BASE (font)) / FONT_HEIGHT (font));
17410 saved_pos = it->position;
17411 memset (&it->position, 0, sizeof it->position);
17412 saved_avoid_cursor = it->avoid_cursor_p;
17413 it->avoid_cursor_p = 1;
17414 saved_face_id = it->face_id;
17415 /* The last row's stretch glyph should get the default
17416 face, to avoid painting the rest of the window with
17417 the region face, if the region ends at ZV. */
17418 if (it->glyph_row->ends_at_zv_p)
17419 it->face_id = DEFAULT_FACE_ID;
17420 else
17421 it->face_id = face->id;
17422 append_stretch_glyph (it, make_number (0), stretch_width,
17423 it->ascent + it->descent, stretch_ascent);
17424 it->position = saved_pos;
17425 it->avoid_cursor_p = saved_avoid_cursor;
17426 it->face_id = saved_face_id;
17427 }
17428 }
17429 #endif /* HAVE_WINDOW_SYSTEM */
17430 }
17431 else
17432 {
17433 /* Save some values that must not be changed. */
17434 int saved_x = it->current_x;
17435 struct text_pos saved_pos;
17436 Lisp_Object saved_object;
17437 enum display_element_type saved_what = it->what;
17438 int saved_face_id = it->face_id;
17439
17440 saved_object = it->object;
17441 saved_pos = it->position;
17442
17443 it->what = IT_CHARACTER;
17444 memset (&it->position, 0, sizeof it->position);
17445 it->object = make_number (0);
17446 it->c = it->char_to_display = ' ';
17447 it->len = 1;
17448 /* The last row's blank glyphs should get the default face, to
17449 avoid painting the rest of the window with the region face,
17450 if the region ends at ZV. */
17451 if (it->glyph_row->ends_at_zv_p)
17452 it->face_id = DEFAULT_FACE_ID;
17453 else
17454 it->face_id = face->id;
17455
17456 PRODUCE_GLYPHS (it);
17457
17458 while (it->current_x <= it->last_visible_x)
17459 PRODUCE_GLYPHS (it);
17460
17461 /* Don't count these blanks really. It would let us insert a left
17462 truncation glyph below and make us set the cursor on them, maybe. */
17463 it->current_x = saved_x;
17464 it->object = saved_object;
17465 it->position = saved_pos;
17466 it->what = saved_what;
17467 it->face_id = saved_face_id;
17468 }
17469 }
17470
17471
17472 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17473 trailing whitespace. */
17474
17475 static int
17476 trailing_whitespace_p (EMACS_INT charpos)
17477 {
17478 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17479 int c = 0;
17480
17481 while (bytepos < ZV_BYTE
17482 && (c = FETCH_CHAR (bytepos),
17483 c == ' ' || c == '\t'))
17484 ++bytepos;
17485
17486 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17487 {
17488 if (bytepos != PT_BYTE)
17489 return 1;
17490 }
17491 return 0;
17492 }
17493
17494
17495 /* Highlight trailing whitespace, if any, in ROW. */
17496
17497 static void
17498 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17499 {
17500 int used = row->used[TEXT_AREA];
17501
17502 if (used)
17503 {
17504 struct glyph *start = row->glyphs[TEXT_AREA];
17505 struct glyph *glyph = start + used - 1;
17506
17507 if (row->reversed_p)
17508 {
17509 /* Right-to-left rows need to be processed in the opposite
17510 direction, so swap the edge pointers. */
17511 glyph = start;
17512 start = row->glyphs[TEXT_AREA] + used - 1;
17513 }
17514
17515 /* Skip over glyphs inserted to display the cursor at the
17516 end of a line, for extending the face of the last glyph
17517 to the end of the line on terminals, and for truncation
17518 and continuation glyphs. */
17519 if (!row->reversed_p)
17520 {
17521 while (glyph >= start
17522 && glyph->type == CHAR_GLYPH
17523 && INTEGERP (glyph->object))
17524 --glyph;
17525 }
17526 else
17527 {
17528 while (glyph <= start
17529 && glyph->type == CHAR_GLYPH
17530 && INTEGERP (glyph->object))
17531 ++glyph;
17532 }
17533
17534 /* If last glyph is a space or stretch, and it's trailing
17535 whitespace, set the face of all trailing whitespace glyphs in
17536 IT->glyph_row to `trailing-whitespace'. */
17537 if ((row->reversed_p ? glyph <= start : glyph >= start)
17538 && BUFFERP (glyph->object)
17539 && (glyph->type == STRETCH_GLYPH
17540 || (glyph->type == CHAR_GLYPH
17541 && glyph->u.ch == ' '))
17542 && trailing_whitespace_p (glyph->charpos))
17543 {
17544 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17545 if (face_id < 0)
17546 return;
17547
17548 if (!row->reversed_p)
17549 {
17550 while (glyph >= start
17551 && BUFFERP (glyph->object)
17552 && (glyph->type == STRETCH_GLYPH
17553 || (glyph->type == CHAR_GLYPH
17554 && glyph->u.ch == ' ')))
17555 (glyph--)->face_id = face_id;
17556 }
17557 else
17558 {
17559 while (glyph <= start
17560 && BUFFERP (glyph->object)
17561 && (glyph->type == STRETCH_GLYPH
17562 || (glyph->type == CHAR_GLYPH
17563 && glyph->u.ch == ' ')))
17564 (glyph++)->face_id = face_id;
17565 }
17566 }
17567 }
17568 }
17569
17570
17571 /* Value is non-zero if glyph row ROW should be
17572 used to hold the cursor. */
17573
17574 static int
17575 cursor_row_p (struct glyph_row *row)
17576 {
17577 int result = 1;
17578
17579 if (PT == CHARPOS (row->end.pos))
17580 {
17581 /* Suppose the row ends on a string.
17582 Unless the row is continued, that means it ends on a newline
17583 in the string. If it's anything other than a display string
17584 (e.g. a before-string from an overlay), we don't want the
17585 cursor there. (This heuristic seems to give the optimal
17586 behavior for the various types of multi-line strings.) */
17587 if (CHARPOS (row->end.string_pos) >= 0)
17588 {
17589 if (row->continued_p)
17590 result = 1;
17591 else
17592 {
17593 /* Check for `display' property. */
17594 struct glyph *beg = row->glyphs[TEXT_AREA];
17595 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17596 struct glyph *glyph;
17597
17598 result = 0;
17599 for (glyph = end; glyph >= beg; --glyph)
17600 if (STRINGP (glyph->object))
17601 {
17602 Lisp_Object prop
17603 = Fget_char_property (make_number (PT),
17604 Qdisplay, Qnil);
17605 result =
17606 (!NILP (prop)
17607 && display_prop_string_p (prop, glyph->object));
17608 break;
17609 }
17610 }
17611 }
17612 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17613 {
17614 /* If the row ends in middle of a real character,
17615 and the line is continued, we want the cursor here.
17616 That's because CHARPOS (ROW->end.pos) would equal
17617 PT if PT is before the character. */
17618 if (!row->ends_in_ellipsis_p)
17619 result = row->continued_p;
17620 else
17621 /* If the row ends in an ellipsis, then
17622 CHARPOS (ROW->end.pos) will equal point after the
17623 invisible text. We want that position to be displayed
17624 after the ellipsis. */
17625 result = 0;
17626 }
17627 /* If the row ends at ZV, display the cursor at the end of that
17628 row instead of at the start of the row below. */
17629 else if (row->ends_at_zv_p)
17630 result = 1;
17631 else
17632 result = 0;
17633 }
17634
17635 return result;
17636 }
17637
17638 \f
17639
17640 /* Push the display property PROP so that it will be rendered at the
17641 current position in IT. Return 1 if PROP was successfully pushed,
17642 0 otherwise. */
17643
17644 static int
17645 push_display_prop (struct it *it, Lisp_Object prop)
17646 {
17647 push_it (it, NULL);
17648
17649 if (STRINGP (prop))
17650 {
17651 if (SCHARS (prop) == 0)
17652 {
17653 pop_it (it);
17654 return 0;
17655 }
17656
17657 it->string = prop;
17658 it->multibyte_p = STRING_MULTIBYTE (it->string);
17659 it->current.overlay_string_index = -1;
17660 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17661 it->end_charpos = it->string_nchars = SCHARS (it->string);
17662 it->method = GET_FROM_STRING;
17663 it->stop_charpos = 0;
17664 }
17665 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17666 {
17667 it->method = GET_FROM_STRETCH;
17668 it->object = prop;
17669 }
17670 #ifdef HAVE_WINDOW_SYSTEM
17671 else if (IMAGEP (prop))
17672 {
17673 it->what = IT_IMAGE;
17674 it->image_id = lookup_image (it->f, prop);
17675 it->method = GET_FROM_IMAGE;
17676 }
17677 #endif /* HAVE_WINDOW_SYSTEM */
17678 else
17679 {
17680 pop_it (it); /* bogus display property, give up */
17681 return 0;
17682 }
17683
17684 return 1;
17685 }
17686
17687 /* Return the character-property PROP at the current position in IT. */
17688
17689 static Lisp_Object
17690 get_it_property (struct it *it, Lisp_Object prop)
17691 {
17692 Lisp_Object position;
17693
17694 if (STRINGP (it->object))
17695 position = make_number (IT_STRING_CHARPOS (*it));
17696 else if (BUFFERP (it->object))
17697 position = make_number (IT_CHARPOS (*it));
17698 else
17699 return Qnil;
17700
17701 return Fget_char_property (position, prop, it->object);
17702 }
17703
17704 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17705
17706 static void
17707 handle_line_prefix (struct it *it)
17708 {
17709 Lisp_Object prefix;
17710 if (it->continuation_lines_width > 0)
17711 {
17712 prefix = get_it_property (it, Qwrap_prefix);
17713 if (NILP (prefix))
17714 prefix = Vwrap_prefix;
17715 }
17716 else
17717 {
17718 prefix = get_it_property (it, Qline_prefix);
17719 if (NILP (prefix))
17720 prefix = Vline_prefix;
17721 }
17722 if (! NILP (prefix) && push_display_prop (it, prefix))
17723 {
17724 /* If the prefix is wider than the window, and we try to wrap
17725 it, it would acquire its own wrap prefix, and so on till the
17726 iterator stack overflows. So, don't wrap the prefix. */
17727 it->line_wrap = TRUNCATE;
17728 it->avoid_cursor_p = 1;
17729 }
17730 }
17731
17732 \f
17733
17734 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17735 only for R2L lines from display_line and display_string, when they
17736 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
17737 the line/string needs to be continued on the next glyph row. */
17738 static void
17739 unproduce_glyphs (struct it *it, int n)
17740 {
17741 struct glyph *glyph, *end;
17742
17743 xassert (it->glyph_row);
17744 xassert (it->glyph_row->reversed_p);
17745 xassert (it->area == TEXT_AREA);
17746 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17747
17748 if (n > it->glyph_row->used[TEXT_AREA])
17749 n = it->glyph_row->used[TEXT_AREA];
17750 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17751 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17752 for ( ; glyph < end; glyph++)
17753 glyph[-n] = *glyph;
17754 }
17755
17756 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17757 and ROW->maxpos. */
17758 static void
17759 find_row_edges (struct it *it, struct glyph_row *row,
17760 EMACS_INT min_pos, EMACS_INT min_bpos,
17761 EMACS_INT max_pos, EMACS_INT max_bpos)
17762 {
17763 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17764 lines' rows is implemented for bidi-reordered rows. */
17765
17766 /* ROW->minpos is the value of min_pos, the minimal buffer position
17767 we have in ROW. */
17768 if (min_pos <= ZV)
17769 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17770 else
17771 /* We didn't find _any_ valid buffer positions in any of the
17772 glyphs, so we must trust the iterator's computed positions. */
17773 row->minpos = row->start.pos;
17774 if (max_pos <= 0)
17775 {
17776 max_pos = CHARPOS (it->current.pos);
17777 max_bpos = BYTEPOS (it->current.pos);
17778 }
17779
17780 /* Here are the various use-cases for ending the row, and the
17781 corresponding values for ROW->maxpos:
17782
17783 Line ends in a newline from buffer eol_pos + 1
17784 Line is continued from buffer max_pos + 1
17785 Line is truncated on right it->current.pos
17786 Line ends in a newline from string max_pos
17787 Line is continued from string max_pos
17788 Line is continued from display vector max_pos
17789 Line is entirely from a string min_pos == max_pos
17790 Line is entirely from a display vector min_pos == max_pos
17791 Line that ends at ZV ZV
17792
17793 If you discover other use-cases, please add them here as
17794 appropriate. */
17795 if (row->ends_at_zv_p)
17796 row->maxpos = it->current.pos;
17797 else if (row->used[TEXT_AREA])
17798 {
17799 if (row->ends_in_newline_from_string_p)
17800 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17801 else if (CHARPOS (it->eol_pos) > 0)
17802 SET_TEXT_POS (row->maxpos,
17803 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17804 else if (row->continued_p)
17805 {
17806 /* If max_pos is different from IT's current position, it
17807 means IT->method does not belong to the display element
17808 at max_pos. However, it also means that the display
17809 element at max_pos was displayed in its entirety on this
17810 line, which is equivalent to saying that the next line
17811 starts at the next buffer position. */
17812 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17813 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17814 else
17815 {
17816 INC_BOTH (max_pos, max_bpos);
17817 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17818 }
17819 }
17820 else if (row->truncated_on_right_p)
17821 /* display_line already called reseat_at_next_visible_line_start,
17822 which puts the iterator at the beginning of the next line, in
17823 the logical order. */
17824 row->maxpos = it->current.pos;
17825 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17826 /* A line that is entirely from a string/image/stretch... */
17827 row->maxpos = row->minpos;
17828 else
17829 abort ();
17830 }
17831 else
17832 row->maxpos = it->current.pos;
17833 }
17834
17835 /* Construct the glyph row IT->glyph_row in the desired matrix of
17836 IT->w from text at the current position of IT. See dispextern.h
17837 for an overview of struct it. Value is non-zero if
17838 IT->glyph_row displays text, as opposed to a line displaying ZV
17839 only. */
17840
17841 static int
17842 display_line (struct it *it)
17843 {
17844 struct glyph_row *row = it->glyph_row;
17845 Lisp_Object overlay_arrow_string;
17846 struct it wrap_it;
17847 int may_wrap = 0, wrap_x IF_LINT (= 0);
17848 int wrap_row_used = -1;
17849 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
17850 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
17851 int wrap_row_extra_line_spacing IF_LINT (= 0);
17852 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
17853 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
17854 int cvpos;
17855 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17856 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
17857
17858 /* We always start displaying at hpos zero even if hscrolled. */
17859 xassert (it->hpos == 0 && it->current_x == 0);
17860
17861 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17862 >= it->w->desired_matrix->nrows)
17863 {
17864 it->w->nrows_scale_factor++;
17865 fonts_changed_p = 1;
17866 return 0;
17867 }
17868
17869 /* Is IT->w showing the region? */
17870 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17871
17872 /* Clear the result glyph row and enable it. */
17873 prepare_desired_row (row);
17874
17875 row->y = it->current_y;
17876 row->start = it->start;
17877 row->continuation_lines_width = it->continuation_lines_width;
17878 row->displays_text_p = 1;
17879 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17880 it->starts_in_middle_of_char_p = 0;
17881
17882 /* Arrange the overlays nicely for our purposes. Usually, we call
17883 display_line on only one line at a time, in which case this
17884 can't really hurt too much, or we call it on lines which appear
17885 one after another in the buffer, in which case all calls to
17886 recenter_overlay_lists but the first will be pretty cheap. */
17887 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17888
17889 /* Move over display elements that are not visible because we are
17890 hscrolled. This may stop at an x-position < IT->first_visible_x
17891 if the first glyph is partially visible or if we hit a line end. */
17892 if (it->current_x < it->first_visible_x)
17893 {
17894 this_line_min_pos = row->start.pos;
17895 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17896 MOVE_TO_POS | MOVE_TO_X);
17897 /* Record the smallest positions seen while we moved over
17898 display elements that are not visible. This is needed by
17899 redisplay_internal for optimizing the case where the cursor
17900 stays inside the same line. The rest of this function only
17901 considers positions that are actually displayed, so
17902 RECORD_MAX_MIN_POS will not otherwise record positions that
17903 are hscrolled to the left of the left edge of the window. */
17904 min_pos = CHARPOS (this_line_min_pos);
17905 min_bpos = BYTEPOS (this_line_min_pos);
17906 }
17907 else
17908 {
17909 /* We only do this when not calling `move_it_in_display_line_to'
17910 above, because move_it_in_display_line_to calls
17911 handle_line_prefix itself. */
17912 handle_line_prefix (it);
17913 }
17914
17915 /* Get the initial row height. This is either the height of the
17916 text hscrolled, if there is any, or zero. */
17917 row->ascent = it->max_ascent;
17918 row->height = it->max_ascent + it->max_descent;
17919 row->phys_ascent = it->max_phys_ascent;
17920 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17921 row->extra_line_spacing = it->max_extra_line_spacing;
17922
17923 /* Utility macro to record max and min buffer positions seen until now. */
17924 #define RECORD_MAX_MIN_POS(IT) \
17925 do \
17926 { \
17927 if (IT_CHARPOS (*(IT)) < min_pos) \
17928 { \
17929 min_pos = IT_CHARPOS (*(IT)); \
17930 min_bpos = IT_BYTEPOS (*(IT)); \
17931 } \
17932 if (IT_CHARPOS (*(IT)) > max_pos) \
17933 { \
17934 max_pos = IT_CHARPOS (*(IT)); \
17935 max_bpos = IT_BYTEPOS (*(IT)); \
17936 } \
17937 } \
17938 while (0)
17939
17940 /* Loop generating characters. The loop is left with IT on the next
17941 character to display. */
17942 while (1)
17943 {
17944 int n_glyphs_before, hpos_before, x_before;
17945 int x, nglyphs;
17946 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17947
17948 /* Retrieve the next thing to display. Value is zero if end of
17949 buffer reached. */
17950 if (!get_next_display_element (it))
17951 {
17952 /* Maybe add a space at the end of this line that is used to
17953 display the cursor there under X. Set the charpos of the
17954 first glyph of blank lines not corresponding to any text
17955 to -1. */
17956 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17957 row->exact_window_width_line_p = 1;
17958 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17959 || row->used[TEXT_AREA] == 0)
17960 {
17961 row->glyphs[TEXT_AREA]->charpos = -1;
17962 row->displays_text_p = 0;
17963
17964 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
17965 && (!MINI_WINDOW_P (it->w)
17966 || (minibuf_level && EQ (it->window, minibuf_window))))
17967 row->indicate_empty_line_p = 1;
17968 }
17969
17970 it->continuation_lines_width = 0;
17971 row->ends_at_zv_p = 1;
17972 /* A row that displays right-to-left text must always have
17973 its last face extended all the way to the end of line,
17974 even if this row ends in ZV, because we still write to
17975 the screen left to right. */
17976 if (row->reversed_p)
17977 extend_face_to_end_of_line (it);
17978 break;
17979 }
17980
17981 /* Now, get the metrics of what we want to display. This also
17982 generates glyphs in `row' (which is IT->glyph_row). */
17983 n_glyphs_before = row->used[TEXT_AREA];
17984 x = it->current_x;
17985
17986 /* Remember the line height so far in case the next element doesn't
17987 fit on the line. */
17988 if (it->line_wrap != TRUNCATE)
17989 {
17990 ascent = it->max_ascent;
17991 descent = it->max_descent;
17992 phys_ascent = it->max_phys_ascent;
17993 phys_descent = it->max_phys_descent;
17994
17995 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17996 {
17997 if (IT_DISPLAYING_WHITESPACE (it))
17998 may_wrap = 1;
17999 else if (may_wrap)
18000 {
18001 wrap_it = *it;
18002 wrap_x = x;
18003 wrap_row_used = row->used[TEXT_AREA];
18004 wrap_row_ascent = row->ascent;
18005 wrap_row_height = row->height;
18006 wrap_row_phys_ascent = row->phys_ascent;
18007 wrap_row_phys_height = row->phys_height;
18008 wrap_row_extra_line_spacing = row->extra_line_spacing;
18009 wrap_row_min_pos = min_pos;
18010 wrap_row_min_bpos = min_bpos;
18011 wrap_row_max_pos = max_pos;
18012 wrap_row_max_bpos = max_bpos;
18013 may_wrap = 0;
18014 }
18015 }
18016 }
18017
18018 PRODUCE_GLYPHS (it);
18019
18020 /* If this display element was in marginal areas, continue with
18021 the next one. */
18022 if (it->area != TEXT_AREA)
18023 {
18024 row->ascent = max (row->ascent, it->max_ascent);
18025 row->height = max (row->height, it->max_ascent + it->max_descent);
18026 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18027 row->phys_height = max (row->phys_height,
18028 it->max_phys_ascent + it->max_phys_descent);
18029 row->extra_line_spacing = max (row->extra_line_spacing,
18030 it->max_extra_line_spacing);
18031 set_iterator_to_next (it, 1);
18032 continue;
18033 }
18034
18035 /* Does the display element fit on the line? If we truncate
18036 lines, we should draw past the right edge of the window. If
18037 we don't truncate, we want to stop so that we can display the
18038 continuation glyph before the right margin. If lines are
18039 continued, there are two possible strategies for characters
18040 resulting in more than 1 glyph (e.g. tabs): Display as many
18041 glyphs as possible in this line and leave the rest for the
18042 continuation line, or display the whole element in the next
18043 line. Original redisplay did the former, so we do it also. */
18044 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18045 hpos_before = it->hpos;
18046 x_before = x;
18047
18048 if (/* Not a newline. */
18049 nglyphs > 0
18050 /* Glyphs produced fit entirely in the line. */
18051 && it->current_x < it->last_visible_x)
18052 {
18053 it->hpos += nglyphs;
18054 row->ascent = max (row->ascent, it->max_ascent);
18055 row->height = max (row->height, it->max_ascent + it->max_descent);
18056 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18057 row->phys_height = max (row->phys_height,
18058 it->max_phys_ascent + it->max_phys_descent);
18059 row->extra_line_spacing = max (row->extra_line_spacing,
18060 it->max_extra_line_spacing);
18061 if (it->current_x - it->pixel_width < it->first_visible_x)
18062 row->x = x - it->first_visible_x;
18063 /* Record the maximum and minimum buffer positions seen so
18064 far in glyphs that will be displayed by this row. */
18065 if (it->bidi_p)
18066 RECORD_MAX_MIN_POS (it);
18067 }
18068 else
18069 {
18070 int i, new_x;
18071 struct glyph *glyph;
18072
18073 for (i = 0; i < nglyphs; ++i, x = new_x)
18074 {
18075 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18076 new_x = x + glyph->pixel_width;
18077
18078 if (/* Lines are continued. */
18079 it->line_wrap != TRUNCATE
18080 && (/* Glyph doesn't fit on the line. */
18081 new_x > it->last_visible_x
18082 /* Or it fits exactly on a window system frame. */
18083 || (new_x == it->last_visible_x
18084 && FRAME_WINDOW_P (it->f))))
18085 {
18086 /* End of a continued line. */
18087
18088 if (it->hpos == 0
18089 || (new_x == it->last_visible_x
18090 && FRAME_WINDOW_P (it->f)))
18091 {
18092 /* Current glyph is the only one on the line or
18093 fits exactly on the line. We must continue
18094 the line because we can't draw the cursor
18095 after the glyph. */
18096 row->continued_p = 1;
18097 it->current_x = new_x;
18098 it->continuation_lines_width += new_x;
18099 ++it->hpos;
18100 /* Record the maximum and minimum buffer
18101 positions seen so far in glyphs that will be
18102 displayed by this row. */
18103 if (it->bidi_p)
18104 RECORD_MAX_MIN_POS (it);
18105 if (i == nglyphs - 1)
18106 {
18107 /* If line-wrap is on, check if a previous
18108 wrap point was found. */
18109 if (wrap_row_used > 0
18110 /* Even if there is a previous wrap
18111 point, continue the line here as
18112 usual, if (i) the previous character
18113 was a space or tab AND (ii) the
18114 current character is not. */
18115 && (!may_wrap
18116 || IT_DISPLAYING_WHITESPACE (it)))
18117 goto back_to_wrap;
18118
18119 set_iterator_to_next (it, 1);
18120 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18121 {
18122 if (!get_next_display_element (it))
18123 {
18124 row->exact_window_width_line_p = 1;
18125 it->continuation_lines_width = 0;
18126 row->continued_p = 0;
18127 row->ends_at_zv_p = 1;
18128 }
18129 else if (ITERATOR_AT_END_OF_LINE_P (it))
18130 {
18131 row->continued_p = 0;
18132 row->exact_window_width_line_p = 1;
18133 }
18134 }
18135 }
18136 }
18137 else if (CHAR_GLYPH_PADDING_P (*glyph)
18138 && !FRAME_WINDOW_P (it->f))
18139 {
18140 /* A padding glyph that doesn't fit on this line.
18141 This means the whole character doesn't fit
18142 on the line. */
18143 if (row->reversed_p)
18144 unproduce_glyphs (it, row->used[TEXT_AREA]
18145 - n_glyphs_before);
18146 row->used[TEXT_AREA] = n_glyphs_before;
18147
18148 /* Fill the rest of the row with continuation
18149 glyphs like in 20.x. */
18150 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18151 < row->glyphs[1 + TEXT_AREA])
18152 produce_special_glyphs (it, IT_CONTINUATION);
18153
18154 row->continued_p = 1;
18155 it->current_x = x_before;
18156 it->continuation_lines_width += x_before;
18157
18158 /* Restore the height to what it was before the
18159 element not fitting on the line. */
18160 it->max_ascent = ascent;
18161 it->max_descent = descent;
18162 it->max_phys_ascent = phys_ascent;
18163 it->max_phys_descent = phys_descent;
18164 }
18165 else if (wrap_row_used > 0)
18166 {
18167 back_to_wrap:
18168 if (row->reversed_p)
18169 unproduce_glyphs (it,
18170 row->used[TEXT_AREA] - wrap_row_used);
18171 *it = wrap_it;
18172 it->continuation_lines_width += wrap_x;
18173 row->used[TEXT_AREA] = wrap_row_used;
18174 row->ascent = wrap_row_ascent;
18175 row->height = wrap_row_height;
18176 row->phys_ascent = wrap_row_phys_ascent;
18177 row->phys_height = wrap_row_phys_height;
18178 row->extra_line_spacing = wrap_row_extra_line_spacing;
18179 min_pos = wrap_row_min_pos;
18180 min_bpos = wrap_row_min_bpos;
18181 max_pos = wrap_row_max_pos;
18182 max_bpos = wrap_row_max_bpos;
18183 row->continued_p = 1;
18184 row->ends_at_zv_p = 0;
18185 row->exact_window_width_line_p = 0;
18186 it->continuation_lines_width += x;
18187
18188 /* Make sure that a non-default face is extended
18189 up to the right margin of the window. */
18190 extend_face_to_end_of_line (it);
18191 }
18192 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18193 {
18194 /* A TAB that extends past the right edge of the
18195 window. This produces a single glyph on
18196 window system frames. We leave the glyph in
18197 this row and let it fill the row, but don't
18198 consume the TAB. */
18199 it->continuation_lines_width += it->last_visible_x;
18200 row->ends_in_middle_of_char_p = 1;
18201 row->continued_p = 1;
18202 glyph->pixel_width = it->last_visible_x - x;
18203 it->starts_in_middle_of_char_p = 1;
18204 }
18205 else
18206 {
18207 /* Something other than a TAB that draws past
18208 the right edge of the window. Restore
18209 positions to values before the element. */
18210 if (row->reversed_p)
18211 unproduce_glyphs (it, row->used[TEXT_AREA]
18212 - (n_glyphs_before + i));
18213 row->used[TEXT_AREA] = n_glyphs_before + i;
18214
18215 /* Display continuation glyphs. */
18216 if (!FRAME_WINDOW_P (it->f))
18217 produce_special_glyphs (it, IT_CONTINUATION);
18218 row->continued_p = 1;
18219
18220 it->current_x = x_before;
18221 it->continuation_lines_width += x;
18222 extend_face_to_end_of_line (it);
18223
18224 if (nglyphs > 1 && i > 0)
18225 {
18226 row->ends_in_middle_of_char_p = 1;
18227 it->starts_in_middle_of_char_p = 1;
18228 }
18229
18230 /* Restore the height to what it was before the
18231 element not fitting on the line. */
18232 it->max_ascent = ascent;
18233 it->max_descent = descent;
18234 it->max_phys_ascent = phys_ascent;
18235 it->max_phys_descent = phys_descent;
18236 }
18237
18238 break;
18239 }
18240 else if (new_x > it->first_visible_x)
18241 {
18242 /* Increment number of glyphs actually displayed. */
18243 ++it->hpos;
18244
18245 /* Record the maximum and minimum buffer positions
18246 seen so far in glyphs that will be displayed by
18247 this row. */
18248 if (it->bidi_p)
18249 RECORD_MAX_MIN_POS (it);
18250
18251 if (x < it->first_visible_x)
18252 /* Glyph is partially visible, i.e. row starts at
18253 negative X position. */
18254 row->x = x - it->first_visible_x;
18255 }
18256 else
18257 {
18258 /* Glyph is completely off the left margin of the
18259 window. This should not happen because of the
18260 move_it_in_display_line at the start of this
18261 function, unless the text display area of the
18262 window is empty. */
18263 xassert (it->first_visible_x <= it->last_visible_x);
18264 }
18265 }
18266
18267 row->ascent = max (row->ascent, it->max_ascent);
18268 row->height = max (row->height, it->max_ascent + it->max_descent);
18269 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18270 row->phys_height = max (row->phys_height,
18271 it->max_phys_ascent + it->max_phys_descent);
18272 row->extra_line_spacing = max (row->extra_line_spacing,
18273 it->max_extra_line_spacing);
18274
18275 /* End of this display line if row is continued. */
18276 if (row->continued_p || row->ends_at_zv_p)
18277 break;
18278 }
18279
18280 at_end_of_line:
18281 /* Is this a line end? If yes, we're also done, after making
18282 sure that a non-default face is extended up to the right
18283 margin of the window. */
18284 if (ITERATOR_AT_END_OF_LINE_P (it))
18285 {
18286 int used_before = row->used[TEXT_AREA];
18287
18288 row->ends_in_newline_from_string_p = STRINGP (it->object);
18289
18290 /* Add a space at the end of the line that is used to
18291 display the cursor there. */
18292 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18293 append_space_for_newline (it, 0);
18294
18295 /* Extend the face to the end of the line. */
18296 extend_face_to_end_of_line (it);
18297
18298 /* Make sure we have the position. */
18299 if (used_before == 0)
18300 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18301
18302 /* Record the position of the newline, for use in
18303 find_row_edges. */
18304 it->eol_pos = it->current.pos;
18305
18306 /* Consume the line end. This skips over invisible lines. */
18307 set_iterator_to_next (it, 1);
18308 it->continuation_lines_width = 0;
18309 break;
18310 }
18311
18312 /* Proceed with next display element. Note that this skips
18313 over lines invisible because of selective display. */
18314 set_iterator_to_next (it, 1);
18315
18316 /* If we truncate lines, we are done when the last displayed
18317 glyphs reach past the right margin of the window. */
18318 if (it->line_wrap == TRUNCATE
18319 && (FRAME_WINDOW_P (it->f)
18320 ? (it->current_x >= it->last_visible_x)
18321 : (it->current_x > it->last_visible_x)))
18322 {
18323 /* Maybe add truncation glyphs. */
18324 if (!FRAME_WINDOW_P (it->f))
18325 {
18326 int i, n;
18327
18328 if (!row->reversed_p)
18329 {
18330 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18331 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18332 break;
18333 }
18334 else
18335 {
18336 for (i = 0; i < row->used[TEXT_AREA]; i++)
18337 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18338 break;
18339 /* Remove any padding glyphs at the front of ROW, to
18340 make room for the truncation glyphs we will be
18341 adding below. The loop below always inserts at
18342 least one truncation glyph, so also remove the
18343 last glyph added to ROW. */
18344 unproduce_glyphs (it, i + 1);
18345 /* Adjust i for the loop below. */
18346 i = row->used[TEXT_AREA] - (i + 1);
18347 }
18348
18349 for (n = row->used[TEXT_AREA]; i < n; ++i)
18350 {
18351 row->used[TEXT_AREA] = i;
18352 produce_special_glyphs (it, IT_TRUNCATION);
18353 }
18354 }
18355 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18356 {
18357 /* Don't truncate if we can overflow newline into fringe. */
18358 if (!get_next_display_element (it))
18359 {
18360 it->continuation_lines_width = 0;
18361 row->ends_at_zv_p = 1;
18362 row->exact_window_width_line_p = 1;
18363 break;
18364 }
18365 if (ITERATOR_AT_END_OF_LINE_P (it))
18366 {
18367 row->exact_window_width_line_p = 1;
18368 goto at_end_of_line;
18369 }
18370 }
18371
18372 row->truncated_on_right_p = 1;
18373 it->continuation_lines_width = 0;
18374 reseat_at_next_visible_line_start (it, 0);
18375 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18376 it->hpos = hpos_before;
18377 it->current_x = x_before;
18378 break;
18379 }
18380 }
18381
18382 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18383 at the left window margin. */
18384 if (it->first_visible_x
18385 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18386 {
18387 if (!FRAME_WINDOW_P (it->f))
18388 insert_left_trunc_glyphs (it);
18389 row->truncated_on_left_p = 1;
18390 }
18391
18392 /* Remember the position at which this line ends.
18393
18394 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18395 cannot be before the call to find_row_edges below, since that is
18396 where these positions are determined. */
18397 row->end = it->current;
18398 if (!it->bidi_p)
18399 {
18400 row->minpos = row->start.pos;
18401 row->maxpos = row->end.pos;
18402 }
18403 else
18404 {
18405 /* ROW->minpos and ROW->maxpos must be the smallest and
18406 `1 + the largest' buffer positions in ROW. But if ROW was
18407 bidi-reordered, these two positions can be anywhere in the
18408 row, so we must determine them now. */
18409 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18410 }
18411
18412 /* If the start of this line is the overlay arrow-position, then
18413 mark this glyph row as the one containing the overlay arrow.
18414 This is clearly a mess with variable size fonts. It would be
18415 better to let it be displayed like cursors under X. */
18416 if ((row->displays_text_p || !overlay_arrow_seen)
18417 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18418 !NILP (overlay_arrow_string)))
18419 {
18420 /* Overlay arrow in window redisplay is a fringe bitmap. */
18421 if (STRINGP (overlay_arrow_string))
18422 {
18423 struct glyph_row *arrow_row
18424 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18425 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18426 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18427 struct glyph *p = row->glyphs[TEXT_AREA];
18428 struct glyph *p2, *end;
18429
18430 /* Copy the arrow glyphs. */
18431 while (glyph < arrow_end)
18432 *p++ = *glyph++;
18433
18434 /* Throw away padding glyphs. */
18435 p2 = p;
18436 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18437 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18438 ++p2;
18439 if (p2 > p)
18440 {
18441 while (p2 < end)
18442 *p++ = *p2++;
18443 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18444 }
18445 }
18446 else
18447 {
18448 xassert (INTEGERP (overlay_arrow_string));
18449 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18450 }
18451 overlay_arrow_seen = 1;
18452 }
18453
18454 /* Compute pixel dimensions of this line. */
18455 compute_line_metrics (it);
18456
18457 /* Record whether this row ends inside an ellipsis. */
18458 row->ends_in_ellipsis_p
18459 = (it->method == GET_FROM_DISPLAY_VECTOR
18460 && it->ellipsis_p);
18461
18462 /* Save fringe bitmaps in this row. */
18463 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18464 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18465 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18466 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18467
18468 it->left_user_fringe_bitmap = 0;
18469 it->left_user_fringe_face_id = 0;
18470 it->right_user_fringe_bitmap = 0;
18471 it->right_user_fringe_face_id = 0;
18472
18473 /* Maybe set the cursor. */
18474 cvpos = it->w->cursor.vpos;
18475 if ((cvpos < 0
18476 /* In bidi-reordered rows, keep checking for proper cursor
18477 position even if one has been found already, because buffer
18478 positions in such rows change non-linearly with ROW->VPOS,
18479 when a line is continued. One exception: when we are at ZV,
18480 display cursor on the first suitable glyph row, since all
18481 the empty rows after that also have their position set to ZV. */
18482 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18483 lines' rows is implemented for bidi-reordered rows. */
18484 || (it->bidi_p
18485 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18486 && PT >= MATRIX_ROW_START_CHARPOS (row)
18487 && PT <= MATRIX_ROW_END_CHARPOS (row)
18488 && cursor_row_p (row))
18489 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18490
18491 /* Highlight trailing whitespace. */
18492 if (!NILP (Vshow_trailing_whitespace))
18493 highlight_trailing_whitespace (it->f, it->glyph_row);
18494
18495 /* Prepare for the next line. This line starts horizontally at (X
18496 HPOS) = (0 0). Vertical positions are incremented. As a
18497 convenience for the caller, IT->glyph_row is set to the next
18498 row to be used. */
18499 it->current_x = it->hpos = 0;
18500 it->current_y += row->height;
18501 SET_TEXT_POS (it->eol_pos, 0, 0);
18502 ++it->vpos;
18503 ++it->glyph_row;
18504 /* The next row should by default use the same value of the
18505 reversed_p flag as this one. set_iterator_to_next decides when
18506 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18507 the flag accordingly. */
18508 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18509 it->glyph_row->reversed_p = row->reversed_p;
18510 it->start = row->end;
18511 return row->displays_text_p;
18512
18513 #undef RECORD_MAX_MIN_POS
18514 }
18515
18516 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18517 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18518 doc: /* Return paragraph direction at point in BUFFER.
18519 Value is either `left-to-right' or `right-to-left'.
18520 If BUFFER is omitted or nil, it defaults to the current buffer.
18521
18522 Paragraph direction determines how the text in the paragraph is displayed.
18523 In left-to-right paragraphs, text begins at the left margin of the window
18524 and the reading direction is generally left to right. In right-to-left
18525 paragraphs, text begins at the right margin and is read from right to left.
18526
18527 See also `bidi-paragraph-direction'. */)
18528 (Lisp_Object buffer)
18529 {
18530 struct buffer *buf = current_buffer;
18531 struct buffer *old = buf;
18532
18533 if (! NILP (buffer))
18534 {
18535 CHECK_BUFFER (buffer);
18536 buf = XBUFFER (buffer);
18537 }
18538
18539 if (NILP (BVAR (buf, bidi_display_reordering)))
18540 return Qleft_to_right;
18541 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18542 return BVAR (buf, bidi_paragraph_direction);
18543 else
18544 {
18545 /* Determine the direction from buffer text. We could try to
18546 use current_matrix if it is up to date, but this seems fast
18547 enough as it is. */
18548 struct bidi_it itb;
18549 EMACS_INT pos = BUF_PT (buf);
18550 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18551 int c;
18552
18553 set_buffer_temp (buf);
18554 /* bidi_paragraph_init finds the base direction of the paragraph
18555 by searching forward from paragraph start. We need the base
18556 direction of the current or _previous_ paragraph, so we need
18557 to make sure we are within that paragraph. To that end, find
18558 the previous non-empty line. */
18559 if (pos >= ZV && pos > BEGV)
18560 {
18561 pos--;
18562 bytepos = CHAR_TO_BYTE (pos);
18563 }
18564 while ((c = FETCH_BYTE (bytepos)) == '\n'
18565 || c == ' ' || c == '\t' || c == '\f')
18566 {
18567 if (bytepos <= BEGV_BYTE)
18568 break;
18569 bytepos--;
18570 pos--;
18571 }
18572 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18573 bytepos--;
18574 itb.charpos = pos;
18575 itb.bytepos = bytepos;
18576 itb.nchars = -1;
18577 itb.string.s = NULL;
18578 itb.string.lstring = Qnil;
18579 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18580 itb.first_elt = 1;
18581 itb.separator_limit = -1;
18582 itb.paragraph_dir = NEUTRAL_DIR;
18583
18584 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18585 set_buffer_temp (old);
18586 switch (itb.paragraph_dir)
18587 {
18588 case L2R:
18589 return Qleft_to_right;
18590 break;
18591 case R2L:
18592 return Qright_to_left;
18593 break;
18594 default:
18595 abort ();
18596 }
18597 }
18598 }
18599
18600
18601 \f
18602 /***********************************************************************
18603 Menu Bar
18604 ***********************************************************************/
18605
18606 /* Redisplay the menu bar in the frame for window W.
18607
18608 The menu bar of X frames that don't have X toolkit support is
18609 displayed in a special window W->frame->menu_bar_window.
18610
18611 The menu bar of terminal frames is treated specially as far as
18612 glyph matrices are concerned. Menu bar lines are not part of
18613 windows, so the update is done directly on the frame matrix rows
18614 for the menu bar. */
18615
18616 static void
18617 display_menu_bar (struct window *w)
18618 {
18619 struct frame *f = XFRAME (WINDOW_FRAME (w));
18620 struct it it;
18621 Lisp_Object items;
18622 int i;
18623
18624 /* Don't do all this for graphical frames. */
18625 #ifdef HAVE_NTGUI
18626 if (FRAME_W32_P (f))
18627 return;
18628 #endif
18629 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18630 if (FRAME_X_P (f))
18631 return;
18632 #endif
18633
18634 #ifdef HAVE_NS
18635 if (FRAME_NS_P (f))
18636 return;
18637 #endif /* HAVE_NS */
18638
18639 #ifdef USE_X_TOOLKIT
18640 xassert (!FRAME_WINDOW_P (f));
18641 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18642 it.first_visible_x = 0;
18643 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18644 #else /* not USE_X_TOOLKIT */
18645 if (FRAME_WINDOW_P (f))
18646 {
18647 /* Menu bar lines are displayed in the desired matrix of the
18648 dummy window menu_bar_window. */
18649 struct window *menu_w;
18650 xassert (WINDOWP (f->menu_bar_window));
18651 menu_w = XWINDOW (f->menu_bar_window);
18652 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18653 MENU_FACE_ID);
18654 it.first_visible_x = 0;
18655 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18656 }
18657 else
18658 {
18659 /* This is a TTY frame, i.e. character hpos/vpos are used as
18660 pixel x/y. */
18661 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18662 MENU_FACE_ID);
18663 it.first_visible_x = 0;
18664 it.last_visible_x = FRAME_COLS (f);
18665 }
18666 #endif /* not USE_X_TOOLKIT */
18667
18668 /* FIXME: This should be controlled by a user option. See the
18669 comments in redisplay_tool_bar and display_mode_line about
18670 this. */
18671 it.paragraph_embedding = L2R;
18672
18673 if (! mode_line_inverse_video)
18674 /* Force the menu-bar to be displayed in the default face. */
18675 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18676
18677 /* Clear all rows of the menu bar. */
18678 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18679 {
18680 struct glyph_row *row = it.glyph_row + i;
18681 clear_glyph_row (row);
18682 row->enabled_p = 1;
18683 row->full_width_p = 1;
18684 }
18685
18686 /* Display all items of the menu bar. */
18687 items = FRAME_MENU_BAR_ITEMS (it.f);
18688 for (i = 0; i < ASIZE (items); i += 4)
18689 {
18690 Lisp_Object string;
18691
18692 /* Stop at nil string. */
18693 string = AREF (items, i + 1);
18694 if (NILP (string))
18695 break;
18696
18697 /* Remember where item was displayed. */
18698 ASET (items, i + 3, make_number (it.hpos));
18699
18700 /* Display the item, pad with one space. */
18701 if (it.current_x < it.last_visible_x)
18702 display_string (NULL, string, Qnil, 0, 0, &it,
18703 SCHARS (string) + 1, 0, 0, -1);
18704 }
18705
18706 /* Fill out the line with spaces. */
18707 if (it.current_x < it.last_visible_x)
18708 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18709
18710 /* Compute the total height of the lines. */
18711 compute_line_metrics (&it);
18712 }
18713
18714
18715 \f
18716 /***********************************************************************
18717 Mode Line
18718 ***********************************************************************/
18719
18720 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18721 FORCE is non-zero, redisplay mode lines unconditionally.
18722 Otherwise, redisplay only mode lines that are garbaged. Value is
18723 the number of windows whose mode lines were redisplayed. */
18724
18725 static int
18726 redisplay_mode_lines (Lisp_Object window, int force)
18727 {
18728 int nwindows = 0;
18729
18730 while (!NILP (window))
18731 {
18732 struct window *w = XWINDOW (window);
18733
18734 if (WINDOWP (w->hchild))
18735 nwindows += redisplay_mode_lines (w->hchild, force);
18736 else if (WINDOWP (w->vchild))
18737 nwindows += redisplay_mode_lines (w->vchild, force);
18738 else if (force
18739 || FRAME_GARBAGED_P (XFRAME (w->frame))
18740 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18741 {
18742 struct text_pos lpoint;
18743 struct buffer *old = current_buffer;
18744
18745 /* Set the window's buffer for the mode line display. */
18746 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18747 set_buffer_internal_1 (XBUFFER (w->buffer));
18748
18749 /* Point refers normally to the selected window. For any
18750 other window, set up appropriate value. */
18751 if (!EQ (window, selected_window))
18752 {
18753 struct text_pos pt;
18754
18755 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18756 if (CHARPOS (pt) < BEGV)
18757 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18758 else if (CHARPOS (pt) > (ZV - 1))
18759 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18760 else
18761 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18762 }
18763
18764 /* Display mode lines. */
18765 clear_glyph_matrix (w->desired_matrix);
18766 if (display_mode_lines (w))
18767 {
18768 ++nwindows;
18769 w->must_be_updated_p = 1;
18770 }
18771
18772 /* Restore old settings. */
18773 set_buffer_internal_1 (old);
18774 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18775 }
18776
18777 window = w->next;
18778 }
18779
18780 return nwindows;
18781 }
18782
18783
18784 /* Display the mode and/or header line of window W. Value is the
18785 sum number of mode lines and header lines displayed. */
18786
18787 static int
18788 display_mode_lines (struct window *w)
18789 {
18790 Lisp_Object old_selected_window, old_selected_frame;
18791 int n = 0;
18792
18793 old_selected_frame = selected_frame;
18794 selected_frame = w->frame;
18795 old_selected_window = selected_window;
18796 XSETWINDOW (selected_window, w);
18797
18798 /* These will be set while the mode line specs are processed. */
18799 line_number_displayed = 0;
18800 w->column_number_displayed = Qnil;
18801
18802 if (WINDOW_WANTS_MODELINE_P (w))
18803 {
18804 struct window *sel_w = XWINDOW (old_selected_window);
18805
18806 /* Select mode line face based on the real selected window. */
18807 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18808 BVAR (current_buffer, mode_line_format));
18809 ++n;
18810 }
18811
18812 if (WINDOW_WANTS_HEADER_LINE_P (w))
18813 {
18814 display_mode_line (w, HEADER_LINE_FACE_ID,
18815 BVAR (current_buffer, header_line_format));
18816 ++n;
18817 }
18818
18819 selected_frame = old_selected_frame;
18820 selected_window = old_selected_window;
18821 return n;
18822 }
18823
18824
18825 /* Display mode or header line of window W. FACE_ID specifies which
18826 line to display; it is either MODE_LINE_FACE_ID or
18827 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18828 display. Value is the pixel height of the mode/header line
18829 displayed. */
18830
18831 static int
18832 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18833 {
18834 struct it it;
18835 struct face *face;
18836 int count = SPECPDL_INDEX ();
18837
18838 init_iterator (&it, w, -1, -1, NULL, face_id);
18839 /* Don't extend on a previously drawn mode-line.
18840 This may happen if called from pos_visible_p. */
18841 it.glyph_row->enabled_p = 0;
18842 prepare_desired_row (it.glyph_row);
18843
18844 it.glyph_row->mode_line_p = 1;
18845
18846 if (! mode_line_inverse_video)
18847 /* Force the mode-line to be displayed in the default face. */
18848 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18849
18850 /* FIXME: This should be controlled by a user option. But
18851 supporting such an option is not trivial, since the mode line is
18852 made up of many separate strings, most of which are normally
18853 unibyte, and unibyte strings currently don't get reordered for
18854 display. */
18855 it.paragraph_embedding = L2R;
18856
18857 record_unwind_protect (unwind_format_mode_line,
18858 format_mode_line_unwind_data (NULL, Qnil, 0));
18859
18860 mode_line_target = MODE_LINE_DISPLAY;
18861
18862 /* Temporarily make frame's keyboard the current kboard so that
18863 kboard-local variables in the mode_line_format will get the right
18864 values. */
18865 push_kboard (FRAME_KBOARD (it.f));
18866 record_unwind_save_match_data ();
18867 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18868 pop_kboard ();
18869
18870 unbind_to (count, Qnil);
18871
18872 /* Fill up with spaces. */
18873 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18874
18875 compute_line_metrics (&it);
18876 it.glyph_row->full_width_p = 1;
18877 it.glyph_row->continued_p = 0;
18878 it.glyph_row->truncated_on_left_p = 0;
18879 it.glyph_row->truncated_on_right_p = 0;
18880
18881 /* Make a 3D mode-line have a shadow at its right end. */
18882 face = FACE_FROM_ID (it.f, face_id);
18883 extend_face_to_end_of_line (&it);
18884 if (face->box != FACE_NO_BOX)
18885 {
18886 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18887 + it.glyph_row->used[TEXT_AREA] - 1);
18888 last->right_box_line_p = 1;
18889 }
18890
18891 return it.glyph_row->height;
18892 }
18893
18894 /* Move element ELT in LIST to the front of LIST.
18895 Return the updated list. */
18896
18897 static Lisp_Object
18898 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18899 {
18900 register Lisp_Object tail, prev;
18901 register Lisp_Object tem;
18902
18903 tail = list;
18904 prev = Qnil;
18905 while (CONSP (tail))
18906 {
18907 tem = XCAR (tail);
18908
18909 if (EQ (elt, tem))
18910 {
18911 /* Splice out the link TAIL. */
18912 if (NILP (prev))
18913 list = XCDR (tail);
18914 else
18915 Fsetcdr (prev, XCDR (tail));
18916
18917 /* Now make it the first. */
18918 Fsetcdr (tail, list);
18919 return tail;
18920 }
18921 else
18922 prev = tail;
18923 tail = XCDR (tail);
18924 QUIT;
18925 }
18926
18927 /* Not found--return unchanged LIST. */
18928 return list;
18929 }
18930
18931 /* Contribute ELT to the mode line for window IT->w. How it
18932 translates into text depends on its data type.
18933
18934 IT describes the display environment in which we display, as usual.
18935
18936 DEPTH is the depth in recursion. It is used to prevent
18937 infinite recursion here.
18938
18939 FIELD_WIDTH is the number of characters the display of ELT should
18940 occupy in the mode line, and PRECISION is the maximum number of
18941 characters to display from ELT's representation. See
18942 display_string for details.
18943
18944 Returns the hpos of the end of the text generated by ELT.
18945
18946 PROPS is a property list to add to any string we encounter.
18947
18948 If RISKY is nonzero, remove (disregard) any properties in any string
18949 we encounter, and ignore :eval and :propertize.
18950
18951 The global variable `mode_line_target' determines whether the
18952 output is passed to `store_mode_line_noprop',
18953 `store_mode_line_string', or `display_string'. */
18954
18955 static int
18956 display_mode_element (struct it *it, int depth, int field_width, int precision,
18957 Lisp_Object elt, Lisp_Object props, int risky)
18958 {
18959 int n = 0, field, prec;
18960 int literal = 0;
18961
18962 tail_recurse:
18963 if (depth > 100)
18964 elt = build_string ("*too-deep*");
18965
18966 depth++;
18967
18968 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18969 {
18970 case Lisp_String:
18971 {
18972 /* A string: output it and check for %-constructs within it. */
18973 unsigned char c;
18974 EMACS_INT offset = 0;
18975
18976 if (SCHARS (elt) > 0
18977 && (!NILP (props) || risky))
18978 {
18979 Lisp_Object oprops, aelt;
18980 oprops = Ftext_properties_at (make_number (0), elt);
18981
18982 /* If the starting string's properties are not what
18983 we want, translate the string. Also, if the string
18984 is risky, do that anyway. */
18985
18986 if (NILP (Fequal (props, oprops)) || risky)
18987 {
18988 /* If the starting string has properties,
18989 merge the specified ones onto the existing ones. */
18990 if (! NILP (oprops) && !risky)
18991 {
18992 Lisp_Object tem;
18993
18994 oprops = Fcopy_sequence (oprops);
18995 tem = props;
18996 while (CONSP (tem))
18997 {
18998 oprops = Fplist_put (oprops, XCAR (tem),
18999 XCAR (XCDR (tem)));
19000 tem = XCDR (XCDR (tem));
19001 }
19002 props = oprops;
19003 }
19004
19005 aelt = Fassoc (elt, mode_line_proptrans_alist);
19006 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19007 {
19008 /* AELT is what we want. Move it to the front
19009 without consing. */
19010 elt = XCAR (aelt);
19011 mode_line_proptrans_alist
19012 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19013 }
19014 else
19015 {
19016 Lisp_Object tem;
19017
19018 /* If AELT has the wrong props, it is useless.
19019 so get rid of it. */
19020 if (! NILP (aelt))
19021 mode_line_proptrans_alist
19022 = Fdelq (aelt, mode_line_proptrans_alist);
19023
19024 elt = Fcopy_sequence (elt);
19025 Fset_text_properties (make_number (0), Flength (elt),
19026 props, elt);
19027 /* Add this item to mode_line_proptrans_alist. */
19028 mode_line_proptrans_alist
19029 = Fcons (Fcons (elt, props),
19030 mode_line_proptrans_alist);
19031 /* Truncate mode_line_proptrans_alist
19032 to at most 50 elements. */
19033 tem = Fnthcdr (make_number (50),
19034 mode_line_proptrans_alist);
19035 if (! NILP (tem))
19036 XSETCDR (tem, Qnil);
19037 }
19038 }
19039 }
19040
19041 offset = 0;
19042
19043 if (literal)
19044 {
19045 prec = precision - n;
19046 switch (mode_line_target)
19047 {
19048 case MODE_LINE_NOPROP:
19049 case MODE_LINE_TITLE:
19050 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19051 break;
19052 case MODE_LINE_STRING:
19053 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19054 break;
19055 case MODE_LINE_DISPLAY:
19056 n += display_string (NULL, elt, Qnil, 0, 0, it,
19057 0, prec, 0, STRING_MULTIBYTE (elt));
19058 break;
19059 }
19060
19061 break;
19062 }
19063
19064 /* Handle the non-literal case. */
19065
19066 while ((precision <= 0 || n < precision)
19067 && SREF (elt, offset) != 0
19068 && (mode_line_target != MODE_LINE_DISPLAY
19069 || it->current_x < it->last_visible_x))
19070 {
19071 EMACS_INT last_offset = offset;
19072
19073 /* Advance to end of string or next format specifier. */
19074 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19075 ;
19076
19077 if (offset - 1 != last_offset)
19078 {
19079 EMACS_INT nchars, nbytes;
19080
19081 /* Output to end of string or up to '%'. Field width
19082 is length of string. Don't output more than
19083 PRECISION allows us. */
19084 offset--;
19085
19086 prec = c_string_width (SDATA (elt) + last_offset,
19087 offset - last_offset, precision - n,
19088 &nchars, &nbytes);
19089
19090 switch (mode_line_target)
19091 {
19092 case MODE_LINE_NOPROP:
19093 case MODE_LINE_TITLE:
19094 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19095 break;
19096 case MODE_LINE_STRING:
19097 {
19098 EMACS_INT bytepos = last_offset;
19099 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19100 EMACS_INT endpos = (precision <= 0
19101 ? string_byte_to_char (elt, offset)
19102 : charpos + nchars);
19103
19104 n += store_mode_line_string (NULL,
19105 Fsubstring (elt, make_number (charpos),
19106 make_number (endpos)),
19107 0, 0, 0, Qnil);
19108 }
19109 break;
19110 case MODE_LINE_DISPLAY:
19111 {
19112 EMACS_INT bytepos = last_offset;
19113 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19114
19115 if (precision <= 0)
19116 nchars = string_byte_to_char (elt, offset) - charpos;
19117 n += display_string (NULL, elt, Qnil, 0, charpos,
19118 it, 0, nchars, 0,
19119 STRING_MULTIBYTE (elt));
19120 }
19121 break;
19122 }
19123 }
19124 else /* c == '%' */
19125 {
19126 EMACS_INT percent_position = offset;
19127
19128 /* Get the specified minimum width. Zero means
19129 don't pad. */
19130 field = 0;
19131 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19132 field = field * 10 + c - '0';
19133
19134 /* Don't pad beyond the total padding allowed. */
19135 if (field_width - n > 0 && field > field_width - n)
19136 field = field_width - n;
19137
19138 /* Note that either PRECISION <= 0 or N < PRECISION. */
19139 prec = precision - n;
19140
19141 if (c == 'M')
19142 n += display_mode_element (it, depth, field, prec,
19143 Vglobal_mode_string, props,
19144 risky);
19145 else if (c != 0)
19146 {
19147 int multibyte;
19148 EMACS_INT bytepos, charpos;
19149 const char *spec;
19150 Lisp_Object string;
19151
19152 bytepos = percent_position;
19153 charpos = (STRING_MULTIBYTE (elt)
19154 ? string_byte_to_char (elt, bytepos)
19155 : bytepos);
19156 spec = decode_mode_spec (it->w, c, field, &string);
19157 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19158
19159 switch (mode_line_target)
19160 {
19161 case MODE_LINE_NOPROP:
19162 case MODE_LINE_TITLE:
19163 n += store_mode_line_noprop (spec, field, prec);
19164 break;
19165 case MODE_LINE_STRING:
19166 {
19167 int len = strlen (spec);
19168 Lisp_Object tem = make_string (spec, len);
19169 props = Ftext_properties_at (make_number (charpos), elt);
19170 /* Should only keep face property in props */
19171 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19172 }
19173 break;
19174 case MODE_LINE_DISPLAY:
19175 {
19176 int nglyphs_before, nwritten;
19177
19178 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19179 nwritten = display_string (spec, string, elt,
19180 charpos, 0, it,
19181 field, prec, 0,
19182 multibyte);
19183
19184 /* Assign to the glyphs written above the
19185 string where the `%x' came from, position
19186 of the `%'. */
19187 if (nwritten > 0)
19188 {
19189 struct glyph *glyph
19190 = (it->glyph_row->glyphs[TEXT_AREA]
19191 + nglyphs_before);
19192 int i;
19193
19194 for (i = 0; i < nwritten; ++i)
19195 {
19196 glyph[i].object = elt;
19197 glyph[i].charpos = charpos;
19198 }
19199
19200 n += nwritten;
19201 }
19202 }
19203 break;
19204 }
19205 }
19206 else /* c == 0 */
19207 break;
19208 }
19209 }
19210 }
19211 break;
19212
19213 case Lisp_Symbol:
19214 /* A symbol: process the value of the symbol recursively
19215 as if it appeared here directly. Avoid error if symbol void.
19216 Special case: if value of symbol is a string, output the string
19217 literally. */
19218 {
19219 register Lisp_Object tem;
19220
19221 /* If the variable is not marked as risky to set
19222 then its contents are risky to use. */
19223 if (NILP (Fget (elt, Qrisky_local_variable)))
19224 risky = 1;
19225
19226 tem = Fboundp (elt);
19227 if (!NILP (tem))
19228 {
19229 tem = Fsymbol_value (elt);
19230 /* If value is a string, output that string literally:
19231 don't check for % within it. */
19232 if (STRINGP (tem))
19233 literal = 1;
19234
19235 if (!EQ (tem, elt))
19236 {
19237 /* Give up right away for nil or t. */
19238 elt = tem;
19239 goto tail_recurse;
19240 }
19241 }
19242 }
19243 break;
19244
19245 case Lisp_Cons:
19246 {
19247 register Lisp_Object car, tem;
19248
19249 /* A cons cell: five distinct cases.
19250 If first element is :eval or :propertize, do something special.
19251 If first element is a string or a cons, process all the elements
19252 and effectively concatenate them.
19253 If first element is a negative number, truncate displaying cdr to
19254 at most that many characters. If positive, pad (with spaces)
19255 to at least that many characters.
19256 If first element is a symbol, process the cadr or caddr recursively
19257 according to whether the symbol's value is non-nil or nil. */
19258 car = XCAR (elt);
19259 if (EQ (car, QCeval))
19260 {
19261 /* An element of the form (:eval FORM) means evaluate FORM
19262 and use the result as mode line elements. */
19263
19264 if (risky)
19265 break;
19266
19267 if (CONSP (XCDR (elt)))
19268 {
19269 Lisp_Object spec;
19270 spec = safe_eval (XCAR (XCDR (elt)));
19271 n += display_mode_element (it, depth, field_width - n,
19272 precision - n, spec, props,
19273 risky);
19274 }
19275 }
19276 else if (EQ (car, QCpropertize))
19277 {
19278 /* An element of the form (:propertize ELT PROPS...)
19279 means display ELT but applying properties PROPS. */
19280
19281 if (risky)
19282 break;
19283
19284 if (CONSP (XCDR (elt)))
19285 n += display_mode_element (it, depth, field_width - n,
19286 precision - n, XCAR (XCDR (elt)),
19287 XCDR (XCDR (elt)), risky);
19288 }
19289 else if (SYMBOLP (car))
19290 {
19291 tem = Fboundp (car);
19292 elt = XCDR (elt);
19293 if (!CONSP (elt))
19294 goto invalid;
19295 /* elt is now the cdr, and we know it is a cons cell.
19296 Use its car if CAR has a non-nil value. */
19297 if (!NILP (tem))
19298 {
19299 tem = Fsymbol_value (car);
19300 if (!NILP (tem))
19301 {
19302 elt = XCAR (elt);
19303 goto tail_recurse;
19304 }
19305 }
19306 /* Symbol's value is nil (or symbol is unbound)
19307 Get the cddr of the original list
19308 and if possible find the caddr and use that. */
19309 elt = XCDR (elt);
19310 if (NILP (elt))
19311 break;
19312 else if (!CONSP (elt))
19313 goto invalid;
19314 elt = XCAR (elt);
19315 goto tail_recurse;
19316 }
19317 else if (INTEGERP (car))
19318 {
19319 register int lim = XINT (car);
19320 elt = XCDR (elt);
19321 if (lim < 0)
19322 {
19323 /* Negative int means reduce maximum width. */
19324 if (precision <= 0)
19325 precision = -lim;
19326 else
19327 precision = min (precision, -lim);
19328 }
19329 else if (lim > 0)
19330 {
19331 /* Padding specified. Don't let it be more than
19332 current maximum. */
19333 if (precision > 0)
19334 lim = min (precision, lim);
19335
19336 /* If that's more padding than already wanted, queue it.
19337 But don't reduce padding already specified even if
19338 that is beyond the current truncation point. */
19339 field_width = max (lim, field_width);
19340 }
19341 goto tail_recurse;
19342 }
19343 else if (STRINGP (car) || CONSP (car))
19344 {
19345 Lisp_Object halftail = elt;
19346 int len = 0;
19347
19348 while (CONSP (elt)
19349 && (precision <= 0 || n < precision))
19350 {
19351 n += display_mode_element (it, depth,
19352 /* Do padding only after the last
19353 element in the list. */
19354 (! CONSP (XCDR (elt))
19355 ? field_width - n
19356 : 0),
19357 precision - n, XCAR (elt),
19358 props, risky);
19359 elt = XCDR (elt);
19360 len++;
19361 if ((len & 1) == 0)
19362 halftail = XCDR (halftail);
19363 /* Check for cycle. */
19364 if (EQ (halftail, elt))
19365 break;
19366 }
19367 }
19368 }
19369 break;
19370
19371 default:
19372 invalid:
19373 elt = build_string ("*invalid*");
19374 goto tail_recurse;
19375 }
19376
19377 /* Pad to FIELD_WIDTH. */
19378 if (field_width > 0 && n < field_width)
19379 {
19380 switch (mode_line_target)
19381 {
19382 case MODE_LINE_NOPROP:
19383 case MODE_LINE_TITLE:
19384 n += store_mode_line_noprop ("", field_width - n, 0);
19385 break;
19386 case MODE_LINE_STRING:
19387 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19388 break;
19389 case MODE_LINE_DISPLAY:
19390 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19391 0, 0, 0);
19392 break;
19393 }
19394 }
19395
19396 return n;
19397 }
19398
19399 /* Store a mode-line string element in mode_line_string_list.
19400
19401 If STRING is non-null, display that C string. Otherwise, the Lisp
19402 string LISP_STRING is displayed.
19403
19404 FIELD_WIDTH is the minimum number of output glyphs to produce.
19405 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19406 with spaces. FIELD_WIDTH <= 0 means don't pad.
19407
19408 PRECISION is the maximum number of characters to output from
19409 STRING. PRECISION <= 0 means don't truncate the string.
19410
19411 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19412 properties to the string.
19413
19414 PROPS are the properties to add to the string.
19415 The mode_line_string_face face property is always added to the string.
19416 */
19417
19418 static int
19419 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19420 int field_width, int precision, Lisp_Object props)
19421 {
19422 EMACS_INT len;
19423 int n = 0;
19424
19425 if (string != NULL)
19426 {
19427 len = strlen (string);
19428 if (precision > 0 && len > precision)
19429 len = precision;
19430 lisp_string = make_string (string, len);
19431 if (NILP (props))
19432 props = mode_line_string_face_prop;
19433 else if (!NILP (mode_line_string_face))
19434 {
19435 Lisp_Object face = Fplist_get (props, Qface);
19436 props = Fcopy_sequence (props);
19437 if (NILP (face))
19438 face = mode_line_string_face;
19439 else
19440 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19441 props = Fplist_put (props, Qface, face);
19442 }
19443 Fadd_text_properties (make_number (0), make_number (len),
19444 props, lisp_string);
19445 }
19446 else
19447 {
19448 len = XFASTINT (Flength (lisp_string));
19449 if (precision > 0 && len > precision)
19450 {
19451 len = precision;
19452 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19453 precision = -1;
19454 }
19455 if (!NILP (mode_line_string_face))
19456 {
19457 Lisp_Object face;
19458 if (NILP (props))
19459 props = Ftext_properties_at (make_number (0), lisp_string);
19460 face = Fplist_get (props, Qface);
19461 if (NILP (face))
19462 face = mode_line_string_face;
19463 else
19464 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19465 props = Fcons (Qface, Fcons (face, Qnil));
19466 if (copy_string)
19467 lisp_string = Fcopy_sequence (lisp_string);
19468 }
19469 if (!NILP (props))
19470 Fadd_text_properties (make_number (0), make_number (len),
19471 props, lisp_string);
19472 }
19473
19474 if (len > 0)
19475 {
19476 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19477 n += len;
19478 }
19479
19480 if (field_width > len)
19481 {
19482 field_width -= len;
19483 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19484 if (!NILP (props))
19485 Fadd_text_properties (make_number (0), make_number (field_width),
19486 props, lisp_string);
19487 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19488 n += field_width;
19489 }
19490
19491 return n;
19492 }
19493
19494
19495 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19496 1, 4, 0,
19497 doc: /* Format a string out of a mode line format specification.
19498 First arg FORMAT specifies the mode line format (see `mode-line-format'
19499 for details) to use.
19500
19501 By default, the format is evaluated for the currently selected window.
19502
19503 Optional second arg FACE specifies the face property to put on all
19504 characters for which no face is specified. The value nil means the
19505 default face. The value t means whatever face the window's mode line
19506 currently uses (either `mode-line' or `mode-line-inactive',
19507 depending on whether the window is the selected window or not).
19508 An integer value means the value string has no text
19509 properties.
19510
19511 Optional third and fourth args WINDOW and BUFFER specify the window
19512 and buffer to use as the context for the formatting (defaults
19513 are the selected window and the WINDOW's buffer). */)
19514 (Lisp_Object format, Lisp_Object face,
19515 Lisp_Object window, Lisp_Object buffer)
19516 {
19517 struct it it;
19518 int len;
19519 struct window *w;
19520 struct buffer *old_buffer = NULL;
19521 int face_id;
19522 int no_props = INTEGERP (face);
19523 int count = SPECPDL_INDEX ();
19524 Lisp_Object str;
19525 int string_start = 0;
19526
19527 if (NILP (window))
19528 window = selected_window;
19529 CHECK_WINDOW (window);
19530 w = XWINDOW (window);
19531
19532 if (NILP (buffer))
19533 buffer = w->buffer;
19534 CHECK_BUFFER (buffer);
19535
19536 /* Make formatting the modeline a non-op when noninteractive, otherwise
19537 there will be problems later caused by a partially initialized frame. */
19538 if (NILP (format) || noninteractive)
19539 return empty_unibyte_string;
19540
19541 if (no_props)
19542 face = Qnil;
19543
19544 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19545 : EQ (face, Qt) ? (EQ (window, selected_window)
19546 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19547 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19548 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19549 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19550 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19551 : DEFAULT_FACE_ID;
19552
19553 if (XBUFFER (buffer) != current_buffer)
19554 old_buffer = current_buffer;
19555
19556 /* Save things including mode_line_proptrans_alist,
19557 and set that to nil so that we don't alter the outer value. */
19558 record_unwind_protect (unwind_format_mode_line,
19559 format_mode_line_unwind_data
19560 (old_buffer, selected_window, 1));
19561 mode_line_proptrans_alist = Qnil;
19562
19563 Fselect_window (window, Qt);
19564 if (old_buffer)
19565 set_buffer_internal_1 (XBUFFER (buffer));
19566
19567 init_iterator (&it, w, -1, -1, NULL, face_id);
19568
19569 if (no_props)
19570 {
19571 mode_line_target = MODE_LINE_NOPROP;
19572 mode_line_string_face_prop = Qnil;
19573 mode_line_string_list = Qnil;
19574 string_start = MODE_LINE_NOPROP_LEN (0);
19575 }
19576 else
19577 {
19578 mode_line_target = MODE_LINE_STRING;
19579 mode_line_string_list = Qnil;
19580 mode_line_string_face = face;
19581 mode_line_string_face_prop
19582 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19583 }
19584
19585 push_kboard (FRAME_KBOARD (it.f));
19586 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19587 pop_kboard ();
19588
19589 if (no_props)
19590 {
19591 len = MODE_LINE_NOPROP_LEN (string_start);
19592 str = make_string (mode_line_noprop_buf + string_start, len);
19593 }
19594 else
19595 {
19596 mode_line_string_list = Fnreverse (mode_line_string_list);
19597 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19598 empty_unibyte_string);
19599 }
19600
19601 unbind_to (count, Qnil);
19602 return str;
19603 }
19604
19605 /* Write a null-terminated, right justified decimal representation of
19606 the positive integer D to BUF using a minimal field width WIDTH. */
19607
19608 static void
19609 pint2str (register char *buf, register int width, register EMACS_INT d)
19610 {
19611 register char *p = buf;
19612
19613 if (d <= 0)
19614 *p++ = '0';
19615 else
19616 {
19617 while (d > 0)
19618 {
19619 *p++ = d % 10 + '0';
19620 d /= 10;
19621 }
19622 }
19623
19624 for (width -= (int) (p - buf); width > 0; --width)
19625 *p++ = ' ';
19626 *p-- = '\0';
19627 while (p > buf)
19628 {
19629 d = *buf;
19630 *buf++ = *p;
19631 *p-- = d;
19632 }
19633 }
19634
19635 /* Write a null-terminated, right justified decimal and "human
19636 readable" representation of the nonnegative integer D to BUF using
19637 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19638
19639 static const char power_letter[] =
19640 {
19641 0, /* no letter */
19642 'k', /* kilo */
19643 'M', /* mega */
19644 'G', /* giga */
19645 'T', /* tera */
19646 'P', /* peta */
19647 'E', /* exa */
19648 'Z', /* zetta */
19649 'Y' /* yotta */
19650 };
19651
19652 static void
19653 pint2hrstr (char *buf, int width, EMACS_INT d)
19654 {
19655 /* We aim to represent the nonnegative integer D as
19656 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19657 EMACS_INT quotient = d;
19658 int remainder = 0;
19659 /* -1 means: do not use TENTHS. */
19660 int tenths = -1;
19661 int exponent = 0;
19662
19663 /* Length of QUOTIENT.TENTHS as a string. */
19664 int length;
19665
19666 char * psuffix;
19667 char * p;
19668
19669 if (1000 <= quotient)
19670 {
19671 /* Scale to the appropriate EXPONENT. */
19672 do
19673 {
19674 remainder = quotient % 1000;
19675 quotient /= 1000;
19676 exponent++;
19677 }
19678 while (1000 <= quotient);
19679
19680 /* Round to nearest and decide whether to use TENTHS or not. */
19681 if (quotient <= 9)
19682 {
19683 tenths = remainder / 100;
19684 if (50 <= remainder % 100)
19685 {
19686 if (tenths < 9)
19687 tenths++;
19688 else
19689 {
19690 quotient++;
19691 if (quotient == 10)
19692 tenths = -1;
19693 else
19694 tenths = 0;
19695 }
19696 }
19697 }
19698 else
19699 if (500 <= remainder)
19700 {
19701 if (quotient < 999)
19702 quotient++;
19703 else
19704 {
19705 quotient = 1;
19706 exponent++;
19707 tenths = 0;
19708 }
19709 }
19710 }
19711
19712 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19713 if (tenths == -1 && quotient <= 99)
19714 if (quotient <= 9)
19715 length = 1;
19716 else
19717 length = 2;
19718 else
19719 length = 3;
19720 p = psuffix = buf + max (width, length);
19721
19722 /* Print EXPONENT. */
19723 *psuffix++ = power_letter[exponent];
19724 *psuffix = '\0';
19725
19726 /* Print TENTHS. */
19727 if (tenths >= 0)
19728 {
19729 *--p = '0' + tenths;
19730 *--p = '.';
19731 }
19732
19733 /* Print QUOTIENT. */
19734 do
19735 {
19736 int digit = quotient % 10;
19737 *--p = '0' + digit;
19738 }
19739 while ((quotient /= 10) != 0);
19740
19741 /* Print leading spaces. */
19742 while (buf < p)
19743 *--p = ' ';
19744 }
19745
19746 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19747 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19748 type of CODING_SYSTEM. Return updated pointer into BUF. */
19749
19750 static unsigned char invalid_eol_type[] = "(*invalid*)";
19751
19752 static char *
19753 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19754 {
19755 Lisp_Object val;
19756 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19757 const unsigned char *eol_str;
19758 int eol_str_len;
19759 /* The EOL conversion we are using. */
19760 Lisp_Object eoltype;
19761
19762 val = CODING_SYSTEM_SPEC (coding_system);
19763 eoltype = Qnil;
19764
19765 if (!VECTORP (val)) /* Not yet decided. */
19766 {
19767 if (multibyte)
19768 *buf++ = '-';
19769 if (eol_flag)
19770 eoltype = eol_mnemonic_undecided;
19771 /* Don't mention EOL conversion if it isn't decided. */
19772 }
19773 else
19774 {
19775 Lisp_Object attrs;
19776 Lisp_Object eolvalue;
19777
19778 attrs = AREF (val, 0);
19779 eolvalue = AREF (val, 2);
19780
19781 if (multibyte)
19782 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19783
19784 if (eol_flag)
19785 {
19786 /* The EOL conversion that is normal on this system. */
19787
19788 if (NILP (eolvalue)) /* Not yet decided. */
19789 eoltype = eol_mnemonic_undecided;
19790 else if (VECTORP (eolvalue)) /* Not yet decided. */
19791 eoltype = eol_mnemonic_undecided;
19792 else /* eolvalue is Qunix, Qdos, or Qmac. */
19793 eoltype = (EQ (eolvalue, Qunix)
19794 ? eol_mnemonic_unix
19795 : (EQ (eolvalue, Qdos) == 1
19796 ? eol_mnemonic_dos : eol_mnemonic_mac));
19797 }
19798 }
19799
19800 if (eol_flag)
19801 {
19802 /* Mention the EOL conversion if it is not the usual one. */
19803 if (STRINGP (eoltype))
19804 {
19805 eol_str = SDATA (eoltype);
19806 eol_str_len = SBYTES (eoltype);
19807 }
19808 else if (CHARACTERP (eoltype))
19809 {
19810 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19811 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19812 eol_str = tmp;
19813 }
19814 else
19815 {
19816 eol_str = invalid_eol_type;
19817 eol_str_len = sizeof (invalid_eol_type) - 1;
19818 }
19819 memcpy (buf, eol_str, eol_str_len);
19820 buf += eol_str_len;
19821 }
19822
19823 return buf;
19824 }
19825
19826 /* Return a string for the output of a mode line %-spec for window W,
19827 generated by character C. FIELD_WIDTH > 0 means pad the string
19828 returned with spaces to that value. Return a Lisp string in
19829 *STRING if the resulting string is taken from that Lisp string.
19830
19831 Note we operate on the current buffer for most purposes,
19832 the exception being w->base_line_pos. */
19833
19834 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19835
19836 static const char *
19837 decode_mode_spec (struct window *w, register int c, int field_width,
19838 Lisp_Object *string)
19839 {
19840 Lisp_Object obj;
19841 struct frame *f = XFRAME (WINDOW_FRAME (w));
19842 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19843 struct buffer *b = current_buffer;
19844
19845 obj = Qnil;
19846 *string = Qnil;
19847
19848 switch (c)
19849 {
19850 case '*':
19851 if (!NILP (BVAR (b, read_only)))
19852 return "%";
19853 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19854 return "*";
19855 return "-";
19856
19857 case '+':
19858 /* This differs from %* only for a modified read-only buffer. */
19859 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19860 return "*";
19861 if (!NILP (BVAR (b, read_only)))
19862 return "%";
19863 return "-";
19864
19865 case '&':
19866 /* This differs from %* in ignoring read-only-ness. */
19867 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19868 return "*";
19869 return "-";
19870
19871 case '%':
19872 return "%";
19873
19874 case '[':
19875 {
19876 int i;
19877 char *p;
19878
19879 if (command_loop_level > 5)
19880 return "[[[... ";
19881 p = decode_mode_spec_buf;
19882 for (i = 0; i < command_loop_level; i++)
19883 *p++ = '[';
19884 *p = 0;
19885 return decode_mode_spec_buf;
19886 }
19887
19888 case ']':
19889 {
19890 int i;
19891 char *p;
19892
19893 if (command_loop_level > 5)
19894 return " ...]]]";
19895 p = decode_mode_spec_buf;
19896 for (i = 0; i < command_loop_level; i++)
19897 *p++ = ']';
19898 *p = 0;
19899 return decode_mode_spec_buf;
19900 }
19901
19902 case '-':
19903 {
19904 register int i;
19905
19906 /* Let lots_of_dashes be a string of infinite length. */
19907 if (mode_line_target == MODE_LINE_NOPROP ||
19908 mode_line_target == MODE_LINE_STRING)
19909 return "--";
19910 if (field_width <= 0
19911 || field_width > sizeof (lots_of_dashes))
19912 {
19913 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19914 decode_mode_spec_buf[i] = '-';
19915 decode_mode_spec_buf[i] = '\0';
19916 return decode_mode_spec_buf;
19917 }
19918 else
19919 return lots_of_dashes;
19920 }
19921
19922 case 'b':
19923 obj = BVAR (b, name);
19924 break;
19925
19926 case 'c':
19927 /* %c and %l are ignored in `frame-title-format'.
19928 (In redisplay_internal, the frame title is drawn _before_ the
19929 windows are updated, so the stuff which depends on actual
19930 window contents (such as %l) may fail to render properly, or
19931 even crash emacs.) */
19932 if (mode_line_target == MODE_LINE_TITLE)
19933 return "";
19934 else
19935 {
19936 EMACS_INT col = current_column ();
19937 w->column_number_displayed = make_number (col);
19938 pint2str (decode_mode_spec_buf, field_width, col);
19939 return decode_mode_spec_buf;
19940 }
19941
19942 case 'e':
19943 #ifndef SYSTEM_MALLOC
19944 {
19945 if (NILP (Vmemory_full))
19946 return "";
19947 else
19948 return "!MEM FULL! ";
19949 }
19950 #else
19951 return "";
19952 #endif
19953
19954 case 'F':
19955 /* %F displays the frame name. */
19956 if (!NILP (f->title))
19957 return SSDATA (f->title);
19958 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19959 return SSDATA (f->name);
19960 return "Emacs";
19961
19962 case 'f':
19963 obj = BVAR (b, filename);
19964 break;
19965
19966 case 'i':
19967 {
19968 EMACS_INT size = ZV - BEGV;
19969 pint2str (decode_mode_spec_buf, field_width, size);
19970 return decode_mode_spec_buf;
19971 }
19972
19973 case 'I':
19974 {
19975 EMACS_INT size = ZV - BEGV;
19976 pint2hrstr (decode_mode_spec_buf, field_width, size);
19977 return decode_mode_spec_buf;
19978 }
19979
19980 case 'l':
19981 {
19982 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19983 EMACS_INT topline, nlines, height;
19984 EMACS_INT junk;
19985
19986 /* %c and %l are ignored in `frame-title-format'. */
19987 if (mode_line_target == MODE_LINE_TITLE)
19988 return "";
19989
19990 startpos = XMARKER (w->start)->charpos;
19991 startpos_byte = marker_byte_position (w->start);
19992 height = WINDOW_TOTAL_LINES (w);
19993
19994 /* If we decided that this buffer isn't suitable for line numbers,
19995 don't forget that too fast. */
19996 if (EQ (w->base_line_pos, w->buffer))
19997 goto no_value;
19998 /* But do forget it, if the window shows a different buffer now. */
19999 else if (BUFFERP (w->base_line_pos))
20000 w->base_line_pos = Qnil;
20001
20002 /* If the buffer is very big, don't waste time. */
20003 if (INTEGERP (Vline_number_display_limit)
20004 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20005 {
20006 w->base_line_pos = Qnil;
20007 w->base_line_number = Qnil;
20008 goto no_value;
20009 }
20010
20011 if (INTEGERP (w->base_line_number)
20012 && INTEGERP (w->base_line_pos)
20013 && XFASTINT (w->base_line_pos) <= startpos)
20014 {
20015 line = XFASTINT (w->base_line_number);
20016 linepos = XFASTINT (w->base_line_pos);
20017 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20018 }
20019 else
20020 {
20021 line = 1;
20022 linepos = BUF_BEGV (b);
20023 linepos_byte = BUF_BEGV_BYTE (b);
20024 }
20025
20026 /* Count lines from base line to window start position. */
20027 nlines = display_count_lines (linepos_byte,
20028 startpos_byte,
20029 startpos, &junk);
20030
20031 topline = nlines + line;
20032
20033 /* Determine a new base line, if the old one is too close
20034 or too far away, or if we did not have one.
20035 "Too close" means it's plausible a scroll-down would
20036 go back past it. */
20037 if (startpos == BUF_BEGV (b))
20038 {
20039 w->base_line_number = make_number (topline);
20040 w->base_line_pos = make_number (BUF_BEGV (b));
20041 }
20042 else if (nlines < height + 25 || nlines > height * 3 + 50
20043 || linepos == BUF_BEGV (b))
20044 {
20045 EMACS_INT limit = BUF_BEGV (b);
20046 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20047 EMACS_INT position;
20048 EMACS_INT distance =
20049 (height * 2 + 30) * line_number_display_limit_width;
20050
20051 if (startpos - distance > limit)
20052 {
20053 limit = startpos - distance;
20054 limit_byte = CHAR_TO_BYTE (limit);
20055 }
20056
20057 nlines = display_count_lines (startpos_byte,
20058 limit_byte,
20059 - (height * 2 + 30),
20060 &position);
20061 /* If we couldn't find the lines we wanted within
20062 line_number_display_limit_width chars per line,
20063 give up on line numbers for this window. */
20064 if (position == limit_byte && limit == startpos - distance)
20065 {
20066 w->base_line_pos = w->buffer;
20067 w->base_line_number = Qnil;
20068 goto no_value;
20069 }
20070
20071 w->base_line_number = make_number (topline - nlines);
20072 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20073 }
20074
20075 /* Now count lines from the start pos to point. */
20076 nlines = display_count_lines (startpos_byte,
20077 PT_BYTE, PT, &junk);
20078
20079 /* Record that we did display the line number. */
20080 line_number_displayed = 1;
20081
20082 /* Make the string to show. */
20083 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20084 return decode_mode_spec_buf;
20085 no_value:
20086 {
20087 char* p = decode_mode_spec_buf;
20088 int pad = field_width - 2;
20089 while (pad-- > 0)
20090 *p++ = ' ';
20091 *p++ = '?';
20092 *p++ = '?';
20093 *p = '\0';
20094 return decode_mode_spec_buf;
20095 }
20096 }
20097 break;
20098
20099 case 'm':
20100 obj = BVAR (b, mode_name);
20101 break;
20102
20103 case 'n':
20104 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20105 return " Narrow";
20106 break;
20107
20108 case 'p':
20109 {
20110 EMACS_INT pos = marker_position (w->start);
20111 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20112
20113 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20114 {
20115 if (pos <= BUF_BEGV (b))
20116 return "All";
20117 else
20118 return "Bottom";
20119 }
20120 else if (pos <= BUF_BEGV (b))
20121 return "Top";
20122 else
20123 {
20124 if (total > 1000000)
20125 /* Do it differently for a large value, to avoid overflow. */
20126 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20127 else
20128 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20129 /* We can't normally display a 3-digit number,
20130 so get us a 2-digit number that is close. */
20131 if (total == 100)
20132 total = 99;
20133 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20134 return decode_mode_spec_buf;
20135 }
20136 }
20137
20138 /* Display percentage of size above the bottom of the screen. */
20139 case 'P':
20140 {
20141 EMACS_INT toppos = marker_position (w->start);
20142 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20143 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20144
20145 if (botpos >= BUF_ZV (b))
20146 {
20147 if (toppos <= BUF_BEGV (b))
20148 return "All";
20149 else
20150 return "Bottom";
20151 }
20152 else
20153 {
20154 if (total > 1000000)
20155 /* Do it differently for a large value, to avoid overflow. */
20156 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20157 else
20158 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20159 /* We can't normally display a 3-digit number,
20160 so get us a 2-digit number that is close. */
20161 if (total == 100)
20162 total = 99;
20163 if (toppos <= BUF_BEGV (b))
20164 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20165 else
20166 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20167 return decode_mode_spec_buf;
20168 }
20169 }
20170
20171 case 's':
20172 /* status of process */
20173 obj = Fget_buffer_process (Fcurrent_buffer ());
20174 if (NILP (obj))
20175 return "no process";
20176 #ifndef MSDOS
20177 obj = Fsymbol_name (Fprocess_status (obj));
20178 #endif
20179 break;
20180
20181 case '@':
20182 {
20183 int count = inhibit_garbage_collection ();
20184 Lisp_Object val = call1 (intern ("file-remote-p"),
20185 BVAR (current_buffer, directory));
20186 unbind_to (count, Qnil);
20187
20188 if (NILP (val))
20189 return "-";
20190 else
20191 return "@";
20192 }
20193
20194 case 't': /* indicate TEXT or BINARY */
20195 return "T";
20196
20197 case 'z':
20198 /* coding-system (not including end-of-line format) */
20199 case 'Z':
20200 /* coding-system (including end-of-line type) */
20201 {
20202 int eol_flag = (c == 'Z');
20203 char *p = decode_mode_spec_buf;
20204
20205 if (! FRAME_WINDOW_P (f))
20206 {
20207 /* No need to mention EOL here--the terminal never needs
20208 to do EOL conversion. */
20209 p = decode_mode_spec_coding (CODING_ID_NAME
20210 (FRAME_KEYBOARD_CODING (f)->id),
20211 p, 0);
20212 p = decode_mode_spec_coding (CODING_ID_NAME
20213 (FRAME_TERMINAL_CODING (f)->id),
20214 p, 0);
20215 }
20216 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20217 p, eol_flag);
20218
20219 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20220 #ifdef subprocesses
20221 obj = Fget_buffer_process (Fcurrent_buffer ());
20222 if (PROCESSP (obj))
20223 {
20224 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20225 p, eol_flag);
20226 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20227 p, eol_flag);
20228 }
20229 #endif /* subprocesses */
20230 #endif /* 0 */
20231 *p = 0;
20232 return decode_mode_spec_buf;
20233 }
20234 }
20235
20236 if (STRINGP (obj))
20237 {
20238 *string = obj;
20239 return SSDATA (obj);
20240 }
20241 else
20242 return "";
20243 }
20244
20245
20246 /* Count up to COUNT lines starting from START_BYTE.
20247 But don't go beyond LIMIT_BYTE.
20248 Return the number of lines thus found (always nonnegative).
20249
20250 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20251
20252 static EMACS_INT
20253 display_count_lines (EMACS_INT start_byte,
20254 EMACS_INT limit_byte, EMACS_INT count,
20255 EMACS_INT *byte_pos_ptr)
20256 {
20257 register unsigned char *cursor;
20258 unsigned char *base;
20259
20260 register EMACS_INT ceiling;
20261 register unsigned char *ceiling_addr;
20262 EMACS_INT orig_count = count;
20263
20264 /* If we are not in selective display mode,
20265 check only for newlines. */
20266 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20267 && !INTEGERP (BVAR (current_buffer, selective_display)));
20268
20269 if (count > 0)
20270 {
20271 while (start_byte < limit_byte)
20272 {
20273 ceiling = BUFFER_CEILING_OF (start_byte);
20274 ceiling = min (limit_byte - 1, ceiling);
20275 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20276 base = (cursor = BYTE_POS_ADDR (start_byte));
20277 while (1)
20278 {
20279 if (selective_display)
20280 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20281 ;
20282 else
20283 while (*cursor != '\n' && ++cursor != ceiling_addr)
20284 ;
20285
20286 if (cursor != ceiling_addr)
20287 {
20288 if (--count == 0)
20289 {
20290 start_byte += cursor - base + 1;
20291 *byte_pos_ptr = start_byte;
20292 return orig_count;
20293 }
20294 else
20295 if (++cursor == ceiling_addr)
20296 break;
20297 }
20298 else
20299 break;
20300 }
20301 start_byte += cursor - base;
20302 }
20303 }
20304 else
20305 {
20306 while (start_byte > limit_byte)
20307 {
20308 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20309 ceiling = max (limit_byte, ceiling);
20310 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20311 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20312 while (1)
20313 {
20314 if (selective_display)
20315 while (--cursor != ceiling_addr
20316 && *cursor != '\n' && *cursor != 015)
20317 ;
20318 else
20319 while (--cursor != ceiling_addr && *cursor != '\n')
20320 ;
20321
20322 if (cursor != ceiling_addr)
20323 {
20324 if (++count == 0)
20325 {
20326 start_byte += cursor - base + 1;
20327 *byte_pos_ptr = start_byte;
20328 /* When scanning backwards, we should
20329 not count the newline posterior to which we stop. */
20330 return - orig_count - 1;
20331 }
20332 }
20333 else
20334 break;
20335 }
20336 /* Here we add 1 to compensate for the last decrement
20337 of CURSOR, which took it past the valid range. */
20338 start_byte += cursor - base + 1;
20339 }
20340 }
20341
20342 *byte_pos_ptr = limit_byte;
20343
20344 if (count < 0)
20345 return - orig_count + count;
20346 return orig_count - count;
20347
20348 }
20349
20350
20351 \f
20352 /***********************************************************************
20353 Displaying strings
20354 ***********************************************************************/
20355
20356 /* Display a NUL-terminated string, starting with index START.
20357
20358 If STRING is non-null, display that C string. Otherwise, the Lisp
20359 string LISP_STRING is displayed. There's a case that STRING is
20360 non-null and LISP_STRING is not nil. It means STRING is a string
20361 data of LISP_STRING. In that case, we display LISP_STRING while
20362 ignoring its text properties.
20363
20364 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20365 FACE_STRING. Display STRING or LISP_STRING with the face at
20366 FACE_STRING_POS in FACE_STRING:
20367
20368 Display the string in the environment given by IT, but use the
20369 standard display table, temporarily.
20370
20371 FIELD_WIDTH is the minimum number of output glyphs to produce.
20372 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20373 with spaces. If STRING has more characters, more than FIELD_WIDTH
20374 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20375
20376 PRECISION is the maximum number of characters to output from
20377 STRING. PRECISION < 0 means don't truncate the string.
20378
20379 This is roughly equivalent to printf format specifiers:
20380
20381 FIELD_WIDTH PRECISION PRINTF
20382 ----------------------------------------
20383 -1 -1 %s
20384 -1 10 %.10s
20385 10 -1 %10s
20386 20 10 %20.10s
20387
20388 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20389 display them, and < 0 means obey the current buffer's value of
20390 enable_multibyte_characters.
20391
20392 Value is the number of columns displayed. */
20393
20394 static int
20395 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20396 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20397 int field_width, int precision, int max_x, int multibyte)
20398 {
20399 int hpos_at_start = it->hpos;
20400 int saved_face_id = it->face_id;
20401 struct glyph_row *row = it->glyph_row;
20402 EMACS_INT it_charpos;
20403
20404 /* Initialize the iterator IT for iteration over STRING beginning
20405 with index START. */
20406 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20407 precision, field_width, multibyte);
20408 if (string && STRINGP (lisp_string))
20409 /* LISP_STRING is the one returned by decode_mode_spec. We should
20410 ignore its text properties. */
20411 it->stop_charpos = it->end_charpos;
20412
20413 /* If displaying STRING, set up the face of the iterator from
20414 FACE_STRING, if that's given. */
20415 if (STRINGP (face_string))
20416 {
20417 EMACS_INT endptr;
20418 struct face *face;
20419
20420 it->face_id
20421 = face_at_string_position (it->w, face_string, face_string_pos,
20422 0, it->region_beg_charpos,
20423 it->region_end_charpos,
20424 &endptr, it->base_face_id, 0);
20425 face = FACE_FROM_ID (it->f, it->face_id);
20426 it->face_box_p = face->box != FACE_NO_BOX;
20427 }
20428
20429 /* Set max_x to the maximum allowed X position. Don't let it go
20430 beyond the right edge of the window. */
20431 if (max_x <= 0)
20432 max_x = it->last_visible_x;
20433 else
20434 max_x = min (max_x, it->last_visible_x);
20435
20436 /* Skip over display elements that are not visible. because IT->w is
20437 hscrolled. */
20438 if (it->current_x < it->first_visible_x)
20439 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20440 MOVE_TO_POS | MOVE_TO_X);
20441
20442 row->ascent = it->max_ascent;
20443 row->height = it->max_ascent + it->max_descent;
20444 row->phys_ascent = it->max_phys_ascent;
20445 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20446 row->extra_line_spacing = it->max_extra_line_spacing;
20447
20448 if (STRINGP (it->string))
20449 it_charpos = IT_STRING_CHARPOS (*it);
20450 else
20451 it_charpos = IT_CHARPOS (*it);
20452
20453 /* This condition is for the case that we are called with current_x
20454 past last_visible_x. */
20455 while (it->current_x < max_x)
20456 {
20457 int x_before, x, n_glyphs_before, i, nglyphs;
20458
20459 /* Get the next display element. */
20460 if (!get_next_display_element (it))
20461 break;
20462
20463 /* Produce glyphs. */
20464 x_before = it->current_x;
20465 n_glyphs_before = row->used[TEXT_AREA];
20466 PRODUCE_GLYPHS (it);
20467
20468 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20469 i = 0;
20470 x = x_before;
20471 while (i < nglyphs)
20472 {
20473 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20474
20475 if (it->line_wrap != TRUNCATE
20476 && x + glyph->pixel_width > max_x)
20477 {
20478 /* End of continued line or max_x reached. */
20479 if (CHAR_GLYPH_PADDING_P (*glyph))
20480 {
20481 /* A wide character is unbreakable. */
20482 if (row->reversed_p)
20483 unproduce_glyphs (it, row->used[TEXT_AREA]
20484 - n_glyphs_before);
20485 row->used[TEXT_AREA] = n_glyphs_before;
20486 it->current_x = x_before;
20487 }
20488 else
20489 {
20490 if (row->reversed_p)
20491 unproduce_glyphs (it, row->used[TEXT_AREA]
20492 - (n_glyphs_before + i));
20493 row->used[TEXT_AREA] = n_glyphs_before + i;
20494 it->current_x = x;
20495 }
20496 break;
20497 }
20498 else if (x + glyph->pixel_width >= it->first_visible_x)
20499 {
20500 /* Glyph is at least partially visible. */
20501 ++it->hpos;
20502 if (x < it->first_visible_x)
20503 row->x = x - it->first_visible_x;
20504 }
20505 else
20506 {
20507 /* Glyph is off the left margin of the display area.
20508 Should not happen. */
20509 abort ();
20510 }
20511
20512 row->ascent = max (row->ascent, it->max_ascent);
20513 row->height = max (row->height, it->max_ascent + it->max_descent);
20514 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20515 row->phys_height = max (row->phys_height,
20516 it->max_phys_ascent + it->max_phys_descent);
20517 row->extra_line_spacing = max (row->extra_line_spacing,
20518 it->max_extra_line_spacing);
20519 x += glyph->pixel_width;
20520 ++i;
20521 }
20522
20523 /* Stop if max_x reached. */
20524 if (i < nglyphs)
20525 break;
20526
20527 /* Stop at line ends. */
20528 if (ITERATOR_AT_END_OF_LINE_P (it))
20529 {
20530 it->continuation_lines_width = 0;
20531 break;
20532 }
20533
20534 set_iterator_to_next (it, 1);
20535 if (STRINGP (it->string))
20536 it_charpos = IT_STRING_CHARPOS (*it);
20537 else
20538 it_charpos = IT_CHARPOS (*it);
20539
20540 /* Stop if truncating at the right edge. */
20541 if (it->line_wrap == TRUNCATE
20542 && it->current_x >= it->last_visible_x)
20543 {
20544 /* Add truncation mark, but don't do it if the line is
20545 truncated at a padding space. */
20546 if (it_charpos < it->string_nchars)
20547 {
20548 if (!FRAME_WINDOW_P (it->f))
20549 {
20550 int ii, n;
20551
20552 if (it->current_x > it->last_visible_x)
20553 {
20554 if (!row->reversed_p)
20555 {
20556 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20557 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20558 break;
20559 }
20560 else
20561 {
20562 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
20563 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20564 break;
20565 unproduce_glyphs (it, ii + 1);
20566 ii = row->used[TEXT_AREA] - (ii + 1);
20567 }
20568 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20569 {
20570 row->used[TEXT_AREA] = ii;
20571 produce_special_glyphs (it, IT_TRUNCATION);
20572 }
20573 }
20574 produce_special_glyphs (it, IT_TRUNCATION);
20575 }
20576 row->truncated_on_right_p = 1;
20577 }
20578 break;
20579 }
20580 }
20581
20582 /* Maybe insert a truncation at the left. */
20583 if (it->first_visible_x
20584 && it_charpos > 0)
20585 {
20586 if (!FRAME_WINDOW_P (it->f))
20587 insert_left_trunc_glyphs (it);
20588 row->truncated_on_left_p = 1;
20589 }
20590
20591 it->face_id = saved_face_id;
20592
20593 /* Value is number of columns displayed. */
20594 return it->hpos - hpos_at_start;
20595 }
20596
20597
20598 \f
20599 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20600 appears as an element of LIST or as the car of an element of LIST.
20601 If PROPVAL is a list, compare each element against LIST in that
20602 way, and return 1/2 if any element of PROPVAL is found in LIST.
20603 Otherwise return 0. This function cannot quit.
20604 The return value is 2 if the text is invisible but with an ellipsis
20605 and 1 if it's invisible and without an ellipsis. */
20606
20607 int
20608 invisible_p (register Lisp_Object propval, Lisp_Object list)
20609 {
20610 register Lisp_Object tail, proptail;
20611
20612 for (tail = list; CONSP (tail); tail = XCDR (tail))
20613 {
20614 register Lisp_Object tem;
20615 tem = XCAR (tail);
20616 if (EQ (propval, tem))
20617 return 1;
20618 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20619 return NILP (XCDR (tem)) ? 1 : 2;
20620 }
20621
20622 if (CONSP (propval))
20623 {
20624 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20625 {
20626 Lisp_Object propelt;
20627 propelt = XCAR (proptail);
20628 for (tail = list; CONSP (tail); tail = XCDR (tail))
20629 {
20630 register Lisp_Object tem;
20631 tem = XCAR (tail);
20632 if (EQ (propelt, tem))
20633 return 1;
20634 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20635 return NILP (XCDR (tem)) ? 1 : 2;
20636 }
20637 }
20638 }
20639
20640 return 0;
20641 }
20642
20643 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20644 doc: /* Non-nil if the property makes the text invisible.
20645 POS-OR-PROP can be a marker or number, in which case it is taken to be
20646 a position in the current buffer and the value of the `invisible' property
20647 is checked; or it can be some other value, which is then presumed to be the
20648 value of the `invisible' property of the text of interest.
20649 The non-nil value returned can be t for truly invisible text or something
20650 else if the text is replaced by an ellipsis. */)
20651 (Lisp_Object pos_or_prop)
20652 {
20653 Lisp_Object prop
20654 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20655 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20656 : pos_or_prop);
20657 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20658 return (invis == 0 ? Qnil
20659 : invis == 1 ? Qt
20660 : make_number (invis));
20661 }
20662
20663 /* Calculate a width or height in pixels from a specification using
20664 the following elements:
20665
20666 SPEC ::=
20667 NUM - a (fractional) multiple of the default font width/height
20668 (NUM) - specifies exactly NUM pixels
20669 UNIT - a fixed number of pixels, see below.
20670 ELEMENT - size of a display element in pixels, see below.
20671 (NUM . SPEC) - equals NUM * SPEC
20672 (+ SPEC SPEC ...) - add pixel values
20673 (- SPEC SPEC ...) - subtract pixel values
20674 (- SPEC) - negate pixel value
20675
20676 NUM ::=
20677 INT or FLOAT - a number constant
20678 SYMBOL - use symbol's (buffer local) variable binding.
20679
20680 UNIT ::=
20681 in - pixels per inch *)
20682 mm - pixels per 1/1000 meter *)
20683 cm - pixels per 1/100 meter *)
20684 width - width of current font in pixels.
20685 height - height of current font in pixels.
20686
20687 *) using the ratio(s) defined in display-pixels-per-inch.
20688
20689 ELEMENT ::=
20690
20691 left-fringe - left fringe width in pixels
20692 right-fringe - right fringe width in pixels
20693
20694 left-margin - left margin width in pixels
20695 right-margin - right margin width in pixels
20696
20697 scroll-bar - scroll-bar area width in pixels
20698
20699 Examples:
20700
20701 Pixels corresponding to 5 inches:
20702 (5 . in)
20703
20704 Total width of non-text areas on left side of window (if scroll-bar is on left):
20705 '(space :width (+ left-fringe left-margin scroll-bar))
20706
20707 Align to first text column (in header line):
20708 '(space :align-to 0)
20709
20710 Align to middle of text area minus half the width of variable `my-image'
20711 containing a loaded image:
20712 '(space :align-to (0.5 . (- text my-image)))
20713
20714 Width of left margin minus width of 1 character in the default font:
20715 '(space :width (- left-margin 1))
20716
20717 Width of left margin minus width of 2 characters in the current font:
20718 '(space :width (- left-margin (2 . width)))
20719
20720 Center 1 character over left-margin (in header line):
20721 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20722
20723 Different ways to express width of left fringe plus left margin minus one pixel:
20724 '(space :width (- (+ left-fringe left-margin) (1)))
20725 '(space :width (+ left-fringe left-margin (- (1))))
20726 '(space :width (+ left-fringe left-margin (-1)))
20727
20728 */
20729
20730 #define NUMVAL(X) \
20731 ((INTEGERP (X) || FLOATP (X)) \
20732 ? XFLOATINT (X) \
20733 : - 1)
20734
20735 int
20736 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20737 struct font *font, int width_p, int *align_to)
20738 {
20739 double pixels;
20740
20741 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20742 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20743
20744 if (NILP (prop))
20745 return OK_PIXELS (0);
20746
20747 xassert (FRAME_LIVE_P (it->f));
20748
20749 if (SYMBOLP (prop))
20750 {
20751 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20752 {
20753 char *unit = SSDATA (SYMBOL_NAME (prop));
20754
20755 if (unit[0] == 'i' && unit[1] == 'n')
20756 pixels = 1.0;
20757 else if (unit[0] == 'm' && unit[1] == 'm')
20758 pixels = 25.4;
20759 else if (unit[0] == 'c' && unit[1] == 'm')
20760 pixels = 2.54;
20761 else
20762 pixels = 0;
20763 if (pixels > 0)
20764 {
20765 double ppi;
20766 #ifdef HAVE_WINDOW_SYSTEM
20767 if (FRAME_WINDOW_P (it->f)
20768 && (ppi = (width_p
20769 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20770 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20771 ppi > 0))
20772 return OK_PIXELS (ppi / pixels);
20773 #endif
20774
20775 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20776 || (CONSP (Vdisplay_pixels_per_inch)
20777 && (ppi = (width_p
20778 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20779 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20780 ppi > 0)))
20781 return OK_PIXELS (ppi / pixels);
20782
20783 return 0;
20784 }
20785 }
20786
20787 #ifdef HAVE_WINDOW_SYSTEM
20788 if (EQ (prop, Qheight))
20789 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20790 if (EQ (prop, Qwidth))
20791 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20792 #else
20793 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20794 return OK_PIXELS (1);
20795 #endif
20796
20797 if (EQ (prop, Qtext))
20798 return OK_PIXELS (width_p
20799 ? window_box_width (it->w, TEXT_AREA)
20800 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20801
20802 if (align_to && *align_to < 0)
20803 {
20804 *res = 0;
20805 if (EQ (prop, Qleft))
20806 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20807 if (EQ (prop, Qright))
20808 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20809 if (EQ (prop, Qcenter))
20810 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20811 + window_box_width (it->w, TEXT_AREA) / 2);
20812 if (EQ (prop, Qleft_fringe))
20813 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20814 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20815 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20816 if (EQ (prop, Qright_fringe))
20817 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20818 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20819 : window_box_right_offset (it->w, TEXT_AREA));
20820 if (EQ (prop, Qleft_margin))
20821 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20822 if (EQ (prop, Qright_margin))
20823 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20824 if (EQ (prop, Qscroll_bar))
20825 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20826 ? 0
20827 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20828 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20829 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20830 : 0)));
20831 }
20832 else
20833 {
20834 if (EQ (prop, Qleft_fringe))
20835 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20836 if (EQ (prop, Qright_fringe))
20837 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20838 if (EQ (prop, Qleft_margin))
20839 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20840 if (EQ (prop, Qright_margin))
20841 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20842 if (EQ (prop, Qscroll_bar))
20843 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20844 }
20845
20846 prop = Fbuffer_local_value (prop, it->w->buffer);
20847 }
20848
20849 if (INTEGERP (prop) || FLOATP (prop))
20850 {
20851 int base_unit = (width_p
20852 ? FRAME_COLUMN_WIDTH (it->f)
20853 : FRAME_LINE_HEIGHT (it->f));
20854 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20855 }
20856
20857 if (CONSP (prop))
20858 {
20859 Lisp_Object car = XCAR (prop);
20860 Lisp_Object cdr = XCDR (prop);
20861
20862 if (SYMBOLP (car))
20863 {
20864 #ifdef HAVE_WINDOW_SYSTEM
20865 if (FRAME_WINDOW_P (it->f)
20866 && valid_image_p (prop))
20867 {
20868 int id = lookup_image (it->f, prop);
20869 struct image *img = IMAGE_FROM_ID (it->f, id);
20870
20871 return OK_PIXELS (width_p ? img->width : img->height);
20872 }
20873 #endif
20874 if (EQ (car, Qplus) || EQ (car, Qminus))
20875 {
20876 int first = 1;
20877 double px;
20878
20879 pixels = 0;
20880 while (CONSP (cdr))
20881 {
20882 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20883 font, width_p, align_to))
20884 return 0;
20885 if (first)
20886 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20887 else
20888 pixels += px;
20889 cdr = XCDR (cdr);
20890 }
20891 if (EQ (car, Qminus))
20892 pixels = -pixels;
20893 return OK_PIXELS (pixels);
20894 }
20895
20896 car = Fbuffer_local_value (car, it->w->buffer);
20897 }
20898
20899 if (INTEGERP (car) || FLOATP (car))
20900 {
20901 double fact;
20902 pixels = XFLOATINT (car);
20903 if (NILP (cdr))
20904 return OK_PIXELS (pixels);
20905 if (calc_pixel_width_or_height (&fact, it, cdr,
20906 font, width_p, align_to))
20907 return OK_PIXELS (pixels * fact);
20908 return 0;
20909 }
20910
20911 return 0;
20912 }
20913
20914 return 0;
20915 }
20916
20917 \f
20918 /***********************************************************************
20919 Glyph Display
20920 ***********************************************************************/
20921
20922 #ifdef HAVE_WINDOW_SYSTEM
20923
20924 #if GLYPH_DEBUG
20925
20926 void
20927 dump_glyph_string (s)
20928 struct glyph_string *s;
20929 {
20930 fprintf (stderr, "glyph string\n");
20931 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20932 s->x, s->y, s->width, s->height);
20933 fprintf (stderr, " ybase = %d\n", s->ybase);
20934 fprintf (stderr, " hl = %d\n", s->hl);
20935 fprintf (stderr, " left overhang = %d, right = %d\n",
20936 s->left_overhang, s->right_overhang);
20937 fprintf (stderr, " nchars = %d\n", s->nchars);
20938 fprintf (stderr, " extends to end of line = %d\n",
20939 s->extends_to_end_of_line_p);
20940 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20941 fprintf (stderr, " bg width = %d\n", s->background_width);
20942 }
20943
20944 #endif /* GLYPH_DEBUG */
20945
20946 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20947 of XChar2b structures for S; it can't be allocated in
20948 init_glyph_string because it must be allocated via `alloca'. W
20949 is the window on which S is drawn. ROW and AREA are the glyph row
20950 and area within the row from which S is constructed. START is the
20951 index of the first glyph structure covered by S. HL is a
20952 face-override for drawing S. */
20953
20954 #ifdef HAVE_NTGUI
20955 #define OPTIONAL_HDC(hdc) HDC hdc,
20956 #define DECLARE_HDC(hdc) HDC hdc;
20957 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20958 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20959 #endif
20960
20961 #ifndef OPTIONAL_HDC
20962 #define OPTIONAL_HDC(hdc)
20963 #define DECLARE_HDC(hdc)
20964 #define ALLOCATE_HDC(hdc, f)
20965 #define RELEASE_HDC(hdc, f)
20966 #endif
20967
20968 static void
20969 init_glyph_string (struct glyph_string *s,
20970 OPTIONAL_HDC (hdc)
20971 XChar2b *char2b, struct window *w, struct glyph_row *row,
20972 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20973 {
20974 memset (s, 0, sizeof *s);
20975 s->w = w;
20976 s->f = XFRAME (w->frame);
20977 #ifdef HAVE_NTGUI
20978 s->hdc = hdc;
20979 #endif
20980 s->display = FRAME_X_DISPLAY (s->f);
20981 s->window = FRAME_X_WINDOW (s->f);
20982 s->char2b = char2b;
20983 s->hl = hl;
20984 s->row = row;
20985 s->area = area;
20986 s->first_glyph = row->glyphs[area] + start;
20987 s->height = row->height;
20988 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20989 s->ybase = s->y + row->ascent;
20990 }
20991
20992
20993 /* Append the list of glyph strings with head H and tail T to the list
20994 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20995
20996 static INLINE void
20997 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20998 struct glyph_string *h, struct glyph_string *t)
20999 {
21000 if (h)
21001 {
21002 if (*head)
21003 (*tail)->next = h;
21004 else
21005 *head = h;
21006 h->prev = *tail;
21007 *tail = t;
21008 }
21009 }
21010
21011
21012 /* Prepend the list of glyph strings with head H and tail T to the
21013 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21014 result. */
21015
21016 static INLINE void
21017 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21018 struct glyph_string *h, struct glyph_string *t)
21019 {
21020 if (h)
21021 {
21022 if (*head)
21023 (*head)->prev = t;
21024 else
21025 *tail = t;
21026 t->next = *head;
21027 *head = h;
21028 }
21029 }
21030
21031
21032 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21033 Set *HEAD and *TAIL to the resulting list. */
21034
21035 static INLINE void
21036 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21037 struct glyph_string *s)
21038 {
21039 s->next = s->prev = NULL;
21040 append_glyph_string_lists (head, tail, s, s);
21041 }
21042
21043
21044 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21045 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21046 make sure that X resources for the face returned are allocated.
21047 Value is a pointer to a realized face that is ready for display if
21048 DISPLAY_P is non-zero. */
21049
21050 static INLINE struct face *
21051 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21052 XChar2b *char2b, int display_p)
21053 {
21054 struct face *face = FACE_FROM_ID (f, face_id);
21055
21056 if (face->font)
21057 {
21058 unsigned code = face->font->driver->encode_char (face->font, c);
21059
21060 if (code != FONT_INVALID_CODE)
21061 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21062 else
21063 STORE_XCHAR2B (char2b, 0, 0);
21064 }
21065
21066 /* Make sure X resources of the face are allocated. */
21067 #ifdef HAVE_X_WINDOWS
21068 if (display_p)
21069 #endif
21070 {
21071 xassert (face != NULL);
21072 PREPARE_FACE_FOR_DISPLAY (f, face);
21073 }
21074
21075 return face;
21076 }
21077
21078
21079 /* Get face and two-byte form of character glyph GLYPH on frame F.
21080 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21081 a pointer to a realized face that is ready for display. */
21082
21083 static INLINE struct face *
21084 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21085 XChar2b *char2b, int *two_byte_p)
21086 {
21087 struct face *face;
21088
21089 xassert (glyph->type == CHAR_GLYPH);
21090 face = FACE_FROM_ID (f, glyph->face_id);
21091
21092 if (two_byte_p)
21093 *two_byte_p = 0;
21094
21095 if (face->font)
21096 {
21097 unsigned code;
21098
21099 if (CHAR_BYTE8_P (glyph->u.ch))
21100 code = CHAR_TO_BYTE8 (glyph->u.ch);
21101 else
21102 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21103
21104 if (code != FONT_INVALID_CODE)
21105 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21106 else
21107 STORE_XCHAR2B (char2b, 0, 0);
21108 }
21109
21110 /* Make sure X resources of the face are allocated. */
21111 xassert (face != NULL);
21112 PREPARE_FACE_FOR_DISPLAY (f, face);
21113 return face;
21114 }
21115
21116
21117 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21118 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21119
21120 static INLINE int
21121 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21122 {
21123 unsigned code;
21124
21125 if (CHAR_BYTE8_P (c))
21126 code = CHAR_TO_BYTE8 (c);
21127 else
21128 code = font->driver->encode_char (font, c);
21129
21130 if (code == FONT_INVALID_CODE)
21131 return 0;
21132 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21133 return 1;
21134 }
21135
21136
21137 /* Fill glyph string S with composition components specified by S->cmp.
21138
21139 BASE_FACE is the base face of the composition.
21140 S->cmp_from is the index of the first component for S.
21141
21142 OVERLAPS non-zero means S should draw the foreground only, and use
21143 its physical height for clipping. See also draw_glyphs.
21144
21145 Value is the index of a component not in S. */
21146
21147 static int
21148 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21149 int overlaps)
21150 {
21151 int i;
21152 /* For all glyphs of this composition, starting at the offset
21153 S->cmp_from, until we reach the end of the definition or encounter a
21154 glyph that requires the different face, add it to S. */
21155 struct face *face;
21156
21157 xassert (s);
21158
21159 s->for_overlaps = overlaps;
21160 s->face = NULL;
21161 s->font = NULL;
21162 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21163 {
21164 int c = COMPOSITION_GLYPH (s->cmp, i);
21165
21166 if (c != '\t')
21167 {
21168 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21169 -1, Qnil);
21170
21171 face = get_char_face_and_encoding (s->f, c, face_id,
21172 s->char2b + i, 1);
21173 if (face)
21174 {
21175 if (! s->face)
21176 {
21177 s->face = face;
21178 s->font = s->face->font;
21179 }
21180 else if (s->face != face)
21181 break;
21182 }
21183 }
21184 ++s->nchars;
21185 }
21186 s->cmp_to = i;
21187
21188 /* All glyph strings for the same composition has the same width,
21189 i.e. the width set for the first component of the composition. */
21190 s->width = s->first_glyph->pixel_width;
21191
21192 /* If the specified font could not be loaded, use the frame's
21193 default font, but record the fact that we couldn't load it in
21194 the glyph string so that we can draw rectangles for the
21195 characters of the glyph string. */
21196 if (s->font == NULL)
21197 {
21198 s->font_not_found_p = 1;
21199 s->font = FRAME_FONT (s->f);
21200 }
21201
21202 /* Adjust base line for subscript/superscript text. */
21203 s->ybase += s->first_glyph->voffset;
21204
21205 /* This glyph string must always be drawn with 16-bit functions. */
21206 s->two_byte_p = 1;
21207
21208 return s->cmp_to;
21209 }
21210
21211 static int
21212 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21213 int start, int end, int overlaps)
21214 {
21215 struct glyph *glyph, *last;
21216 Lisp_Object lgstring;
21217 int i;
21218
21219 s->for_overlaps = overlaps;
21220 glyph = s->row->glyphs[s->area] + start;
21221 last = s->row->glyphs[s->area] + end;
21222 s->cmp_id = glyph->u.cmp.id;
21223 s->cmp_from = glyph->slice.cmp.from;
21224 s->cmp_to = glyph->slice.cmp.to + 1;
21225 s->face = FACE_FROM_ID (s->f, face_id);
21226 lgstring = composition_gstring_from_id (s->cmp_id);
21227 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21228 glyph++;
21229 while (glyph < last
21230 && glyph->u.cmp.automatic
21231 && glyph->u.cmp.id == s->cmp_id
21232 && s->cmp_to == glyph->slice.cmp.from)
21233 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21234
21235 for (i = s->cmp_from; i < s->cmp_to; i++)
21236 {
21237 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21238 unsigned code = LGLYPH_CODE (lglyph);
21239
21240 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21241 }
21242 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21243 return glyph - s->row->glyphs[s->area];
21244 }
21245
21246
21247 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21248 See the comment of fill_glyph_string for arguments.
21249 Value is the index of the first glyph not in S. */
21250
21251
21252 static int
21253 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21254 int start, int end, int overlaps)
21255 {
21256 struct glyph *glyph, *last;
21257 int voffset;
21258
21259 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21260 s->for_overlaps = overlaps;
21261 glyph = s->row->glyphs[s->area] + start;
21262 last = s->row->glyphs[s->area] + end;
21263 voffset = glyph->voffset;
21264 s->face = FACE_FROM_ID (s->f, face_id);
21265 s->font = s->face->font;
21266 s->nchars = 1;
21267 s->width = glyph->pixel_width;
21268 glyph++;
21269 while (glyph < last
21270 && glyph->type == GLYPHLESS_GLYPH
21271 && glyph->voffset == voffset
21272 && glyph->face_id == face_id)
21273 {
21274 s->nchars++;
21275 s->width += glyph->pixel_width;
21276 glyph++;
21277 }
21278 s->ybase += voffset;
21279 return glyph - s->row->glyphs[s->area];
21280 }
21281
21282
21283 /* Fill glyph string S from a sequence of character glyphs.
21284
21285 FACE_ID is the face id of the string. START is the index of the
21286 first glyph to consider, END is the index of the last + 1.
21287 OVERLAPS non-zero means S should draw the foreground only, and use
21288 its physical height for clipping. See also draw_glyphs.
21289
21290 Value is the index of the first glyph not in S. */
21291
21292 static int
21293 fill_glyph_string (struct glyph_string *s, int face_id,
21294 int start, int end, int overlaps)
21295 {
21296 struct glyph *glyph, *last;
21297 int voffset;
21298 int glyph_not_available_p;
21299
21300 xassert (s->f == XFRAME (s->w->frame));
21301 xassert (s->nchars == 0);
21302 xassert (start >= 0 && end > start);
21303
21304 s->for_overlaps = overlaps;
21305 glyph = s->row->glyphs[s->area] + start;
21306 last = s->row->glyphs[s->area] + end;
21307 voffset = glyph->voffset;
21308 s->padding_p = glyph->padding_p;
21309 glyph_not_available_p = glyph->glyph_not_available_p;
21310
21311 while (glyph < last
21312 && glyph->type == CHAR_GLYPH
21313 && glyph->voffset == voffset
21314 /* Same face id implies same font, nowadays. */
21315 && glyph->face_id == face_id
21316 && glyph->glyph_not_available_p == glyph_not_available_p)
21317 {
21318 int two_byte_p;
21319
21320 s->face = get_glyph_face_and_encoding (s->f, glyph,
21321 s->char2b + s->nchars,
21322 &two_byte_p);
21323 s->two_byte_p = two_byte_p;
21324 ++s->nchars;
21325 xassert (s->nchars <= end - start);
21326 s->width += glyph->pixel_width;
21327 if (glyph++->padding_p != s->padding_p)
21328 break;
21329 }
21330
21331 s->font = s->face->font;
21332
21333 /* If the specified font could not be loaded, use the frame's font,
21334 but record the fact that we couldn't load it in
21335 S->font_not_found_p so that we can draw rectangles for the
21336 characters of the glyph string. */
21337 if (s->font == NULL || glyph_not_available_p)
21338 {
21339 s->font_not_found_p = 1;
21340 s->font = FRAME_FONT (s->f);
21341 }
21342
21343 /* Adjust base line for subscript/superscript text. */
21344 s->ybase += voffset;
21345
21346 xassert (s->face && s->face->gc);
21347 return glyph - s->row->glyphs[s->area];
21348 }
21349
21350
21351 /* Fill glyph string S from image glyph S->first_glyph. */
21352
21353 static void
21354 fill_image_glyph_string (struct glyph_string *s)
21355 {
21356 xassert (s->first_glyph->type == IMAGE_GLYPH);
21357 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21358 xassert (s->img);
21359 s->slice = s->first_glyph->slice.img;
21360 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21361 s->font = s->face->font;
21362 s->width = s->first_glyph->pixel_width;
21363
21364 /* Adjust base line for subscript/superscript text. */
21365 s->ybase += s->first_glyph->voffset;
21366 }
21367
21368
21369 /* Fill glyph string S from a sequence of stretch glyphs.
21370
21371 START is the index of the first glyph to consider,
21372 END is the index of the last + 1.
21373
21374 Value is the index of the first glyph not in S. */
21375
21376 static int
21377 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21378 {
21379 struct glyph *glyph, *last;
21380 int voffset, face_id;
21381
21382 xassert (s->first_glyph->type == STRETCH_GLYPH);
21383
21384 glyph = s->row->glyphs[s->area] + start;
21385 last = s->row->glyphs[s->area] + end;
21386 face_id = glyph->face_id;
21387 s->face = FACE_FROM_ID (s->f, face_id);
21388 s->font = s->face->font;
21389 s->width = glyph->pixel_width;
21390 s->nchars = 1;
21391 voffset = glyph->voffset;
21392
21393 for (++glyph;
21394 (glyph < last
21395 && glyph->type == STRETCH_GLYPH
21396 && glyph->voffset == voffset
21397 && glyph->face_id == face_id);
21398 ++glyph)
21399 s->width += glyph->pixel_width;
21400
21401 /* Adjust base line for subscript/superscript text. */
21402 s->ybase += voffset;
21403
21404 /* The case that face->gc == 0 is handled when drawing the glyph
21405 string by calling PREPARE_FACE_FOR_DISPLAY. */
21406 xassert (s->face);
21407 return glyph - s->row->glyphs[s->area];
21408 }
21409
21410 static struct font_metrics *
21411 get_per_char_metric (struct font *font, XChar2b *char2b)
21412 {
21413 static struct font_metrics metrics;
21414 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21415
21416 if (! font || code == FONT_INVALID_CODE)
21417 return NULL;
21418 font->driver->text_extents (font, &code, 1, &metrics);
21419 return &metrics;
21420 }
21421
21422 /* EXPORT for RIF:
21423 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21424 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21425 assumed to be zero. */
21426
21427 void
21428 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21429 {
21430 *left = *right = 0;
21431
21432 if (glyph->type == CHAR_GLYPH)
21433 {
21434 struct face *face;
21435 XChar2b char2b;
21436 struct font_metrics *pcm;
21437
21438 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21439 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21440 {
21441 if (pcm->rbearing > pcm->width)
21442 *right = pcm->rbearing - pcm->width;
21443 if (pcm->lbearing < 0)
21444 *left = -pcm->lbearing;
21445 }
21446 }
21447 else if (glyph->type == COMPOSITE_GLYPH)
21448 {
21449 if (! glyph->u.cmp.automatic)
21450 {
21451 struct composition *cmp = composition_table[glyph->u.cmp.id];
21452
21453 if (cmp->rbearing > cmp->pixel_width)
21454 *right = cmp->rbearing - cmp->pixel_width;
21455 if (cmp->lbearing < 0)
21456 *left = - cmp->lbearing;
21457 }
21458 else
21459 {
21460 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21461 struct font_metrics metrics;
21462
21463 composition_gstring_width (gstring, glyph->slice.cmp.from,
21464 glyph->slice.cmp.to + 1, &metrics);
21465 if (metrics.rbearing > metrics.width)
21466 *right = metrics.rbearing - metrics.width;
21467 if (metrics.lbearing < 0)
21468 *left = - metrics.lbearing;
21469 }
21470 }
21471 }
21472
21473
21474 /* Return the index of the first glyph preceding glyph string S that
21475 is overwritten by S because of S's left overhang. Value is -1
21476 if no glyphs are overwritten. */
21477
21478 static int
21479 left_overwritten (struct glyph_string *s)
21480 {
21481 int k;
21482
21483 if (s->left_overhang)
21484 {
21485 int x = 0, i;
21486 struct glyph *glyphs = s->row->glyphs[s->area];
21487 int first = s->first_glyph - glyphs;
21488
21489 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21490 x -= glyphs[i].pixel_width;
21491
21492 k = i + 1;
21493 }
21494 else
21495 k = -1;
21496
21497 return k;
21498 }
21499
21500
21501 /* Return the index of the first glyph preceding glyph string S that
21502 is overwriting S because of its right overhang. Value is -1 if no
21503 glyph in front of S overwrites S. */
21504
21505 static int
21506 left_overwriting (struct glyph_string *s)
21507 {
21508 int i, k, x;
21509 struct glyph *glyphs = s->row->glyphs[s->area];
21510 int first = s->first_glyph - glyphs;
21511
21512 k = -1;
21513 x = 0;
21514 for (i = first - 1; i >= 0; --i)
21515 {
21516 int left, right;
21517 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21518 if (x + right > 0)
21519 k = i;
21520 x -= glyphs[i].pixel_width;
21521 }
21522
21523 return k;
21524 }
21525
21526
21527 /* Return the index of the last glyph following glyph string S that is
21528 overwritten by S because of S's right overhang. Value is -1 if
21529 no such glyph is found. */
21530
21531 static int
21532 right_overwritten (struct glyph_string *s)
21533 {
21534 int k = -1;
21535
21536 if (s->right_overhang)
21537 {
21538 int x = 0, i;
21539 struct glyph *glyphs = s->row->glyphs[s->area];
21540 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21541 int end = s->row->used[s->area];
21542
21543 for (i = first; i < end && s->right_overhang > x; ++i)
21544 x += glyphs[i].pixel_width;
21545
21546 k = i;
21547 }
21548
21549 return k;
21550 }
21551
21552
21553 /* Return the index of the last glyph following glyph string S that
21554 overwrites S because of its left overhang. Value is negative
21555 if no such glyph is found. */
21556
21557 static int
21558 right_overwriting (struct glyph_string *s)
21559 {
21560 int i, k, x;
21561 int end = s->row->used[s->area];
21562 struct glyph *glyphs = s->row->glyphs[s->area];
21563 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21564
21565 k = -1;
21566 x = 0;
21567 for (i = first; i < end; ++i)
21568 {
21569 int left, right;
21570 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21571 if (x - left < 0)
21572 k = i;
21573 x += glyphs[i].pixel_width;
21574 }
21575
21576 return k;
21577 }
21578
21579
21580 /* Set background width of glyph string S. START is the index of the
21581 first glyph following S. LAST_X is the right-most x-position + 1
21582 in the drawing area. */
21583
21584 static INLINE void
21585 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21586 {
21587 /* If the face of this glyph string has to be drawn to the end of
21588 the drawing area, set S->extends_to_end_of_line_p. */
21589
21590 if (start == s->row->used[s->area]
21591 && s->area == TEXT_AREA
21592 && ((s->row->fill_line_p
21593 && (s->hl == DRAW_NORMAL_TEXT
21594 || s->hl == DRAW_IMAGE_RAISED
21595 || s->hl == DRAW_IMAGE_SUNKEN))
21596 || s->hl == DRAW_MOUSE_FACE))
21597 s->extends_to_end_of_line_p = 1;
21598
21599 /* If S extends its face to the end of the line, set its
21600 background_width to the distance to the right edge of the drawing
21601 area. */
21602 if (s->extends_to_end_of_line_p)
21603 s->background_width = last_x - s->x + 1;
21604 else
21605 s->background_width = s->width;
21606 }
21607
21608
21609 /* Compute overhangs and x-positions for glyph string S and its
21610 predecessors, or successors. X is the starting x-position for S.
21611 BACKWARD_P non-zero means process predecessors. */
21612
21613 static void
21614 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21615 {
21616 if (backward_p)
21617 {
21618 while (s)
21619 {
21620 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21621 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21622 x -= s->width;
21623 s->x = x;
21624 s = s->prev;
21625 }
21626 }
21627 else
21628 {
21629 while (s)
21630 {
21631 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21632 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21633 s->x = x;
21634 x += s->width;
21635 s = s->next;
21636 }
21637 }
21638 }
21639
21640
21641
21642 /* The following macros are only called from draw_glyphs below.
21643 They reference the following parameters of that function directly:
21644 `w', `row', `area', and `overlap_p'
21645 as well as the following local variables:
21646 `s', `f', and `hdc' (in W32) */
21647
21648 #ifdef HAVE_NTGUI
21649 /* On W32, silently add local `hdc' variable to argument list of
21650 init_glyph_string. */
21651 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21652 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21653 #else
21654 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21655 init_glyph_string (s, char2b, w, row, area, start, hl)
21656 #endif
21657
21658 /* Add a glyph string for a stretch glyph to the list of strings
21659 between HEAD and TAIL. START is the index of the stretch glyph in
21660 row area AREA of glyph row ROW. END is the index of the last glyph
21661 in that glyph row area. X is the current output position assigned
21662 to the new glyph string constructed. HL overrides that face of the
21663 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21664 is the right-most x-position of the drawing area. */
21665
21666 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21667 and below -- keep them on one line. */
21668 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21669 do \
21670 { \
21671 s = (struct glyph_string *) alloca (sizeof *s); \
21672 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21673 START = fill_stretch_glyph_string (s, START, END); \
21674 append_glyph_string (&HEAD, &TAIL, s); \
21675 s->x = (X); \
21676 } \
21677 while (0)
21678
21679
21680 /* Add a glyph string for an image glyph to the list of strings
21681 between HEAD and TAIL. START is the index of the image glyph in
21682 row area AREA of glyph row ROW. END is the index of the last glyph
21683 in that glyph row area. X is the current output position assigned
21684 to the new glyph string constructed. HL overrides that face of the
21685 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21686 is the right-most x-position of the drawing area. */
21687
21688 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21689 do \
21690 { \
21691 s = (struct glyph_string *) alloca (sizeof *s); \
21692 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21693 fill_image_glyph_string (s); \
21694 append_glyph_string (&HEAD, &TAIL, s); \
21695 ++START; \
21696 s->x = (X); \
21697 } \
21698 while (0)
21699
21700
21701 /* Add a glyph string for a sequence of character glyphs to the list
21702 of strings between HEAD and TAIL. START is the index of the first
21703 glyph in row area AREA of glyph row ROW that is part of the new
21704 glyph string. END is the index of the last glyph in that glyph row
21705 area. X is the current output position assigned to the new glyph
21706 string constructed. HL overrides that face of the glyph; e.g. it
21707 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21708 right-most x-position of the drawing area. */
21709
21710 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21711 do \
21712 { \
21713 int face_id; \
21714 XChar2b *char2b; \
21715 \
21716 face_id = (row)->glyphs[area][START].face_id; \
21717 \
21718 s = (struct glyph_string *) alloca (sizeof *s); \
21719 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21720 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21721 append_glyph_string (&HEAD, &TAIL, s); \
21722 s->x = (X); \
21723 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21724 } \
21725 while (0)
21726
21727
21728 /* Add a glyph string for a composite sequence to the list of strings
21729 between HEAD and TAIL. START is the index of the first glyph in
21730 row area AREA of glyph row ROW that is part of the new glyph
21731 string. END is the index of the last glyph in that glyph row area.
21732 X is the current output position assigned to the new glyph string
21733 constructed. HL overrides that face of the glyph; e.g. it is
21734 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21735 x-position of the drawing area. */
21736
21737 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21738 do { \
21739 int face_id = (row)->glyphs[area][START].face_id; \
21740 struct face *base_face = FACE_FROM_ID (f, face_id); \
21741 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21742 struct composition *cmp = composition_table[cmp_id]; \
21743 XChar2b *char2b; \
21744 struct glyph_string *first_s IF_LINT (= NULL); \
21745 int n; \
21746 \
21747 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21748 \
21749 /* Make glyph_strings for each glyph sequence that is drawable by \
21750 the same face, and append them to HEAD/TAIL. */ \
21751 for (n = 0; n < cmp->glyph_len;) \
21752 { \
21753 s = (struct glyph_string *) alloca (sizeof *s); \
21754 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21755 append_glyph_string (&(HEAD), &(TAIL), s); \
21756 s->cmp = cmp; \
21757 s->cmp_from = n; \
21758 s->x = (X); \
21759 if (n == 0) \
21760 first_s = s; \
21761 n = fill_composite_glyph_string (s, base_face, overlaps); \
21762 } \
21763 \
21764 ++START; \
21765 s = first_s; \
21766 } while (0)
21767
21768
21769 /* Add a glyph string for a glyph-string sequence to the list of strings
21770 between HEAD and TAIL. */
21771
21772 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21773 do { \
21774 int face_id; \
21775 XChar2b *char2b; \
21776 Lisp_Object gstring; \
21777 \
21778 face_id = (row)->glyphs[area][START].face_id; \
21779 gstring = (composition_gstring_from_id \
21780 ((row)->glyphs[area][START].u.cmp.id)); \
21781 s = (struct glyph_string *) alloca (sizeof *s); \
21782 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21783 * LGSTRING_GLYPH_LEN (gstring)); \
21784 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21785 append_glyph_string (&(HEAD), &(TAIL), s); \
21786 s->x = (X); \
21787 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21788 } while (0)
21789
21790
21791 /* Add a glyph string for a sequence of glyphless character's glyphs
21792 to the list of strings between HEAD and TAIL. The meanings of
21793 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21794
21795 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21796 do \
21797 { \
21798 int face_id; \
21799 \
21800 face_id = (row)->glyphs[area][START].face_id; \
21801 \
21802 s = (struct glyph_string *) alloca (sizeof *s); \
21803 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21804 append_glyph_string (&HEAD, &TAIL, s); \
21805 s->x = (X); \
21806 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21807 overlaps); \
21808 } \
21809 while (0)
21810
21811
21812 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21813 of AREA of glyph row ROW on window W between indices START and END.
21814 HL overrides the face for drawing glyph strings, e.g. it is
21815 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21816 x-positions of the drawing area.
21817
21818 This is an ugly monster macro construct because we must use alloca
21819 to allocate glyph strings (because draw_glyphs can be called
21820 asynchronously). */
21821
21822 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21823 do \
21824 { \
21825 HEAD = TAIL = NULL; \
21826 while (START < END) \
21827 { \
21828 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21829 switch (first_glyph->type) \
21830 { \
21831 case CHAR_GLYPH: \
21832 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21833 HL, X, LAST_X); \
21834 break; \
21835 \
21836 case COMPOSITE_GLYPH: \
21837 if (first_glyph->u.cmp.automatic) \
21838 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21839 HL, X, LAST_X); \
21840 else \
21841 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21842 HL, X, LAST_X); \
21843 break; \
21844 \
21845 case STRETCH_GLYPH: \
21846 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21847 HL, X, LAST_X); \
21848 break; \
21849 \
21850 case IMAGE_GLYPH: \
21851 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21852 HL, X, LAST_X); \
21853 break; \
21854 \
21855 case GLYPHLESS_GLYPH: \
21856 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21857 HL, X, LAST_X); \
21858 break; \
21859 \
21860 default: \
21861 abort (); \
21862 } \
21863 \
21864 if (s) \
21865 { \
21866 set_glyph_string_background_width (s, START, LAST_X); \
21867 (X) += s->width; \
21868 } \
21869 } \
21870 } while (0)
21871
21872
21873 /* Draw glyphs between START and END in AREA of ROW on window W,
21874 starting at x-position X. X is relative to AREA in W. HL is a
21875 face-override with the following meaning:
21876
21877 DRAW_NORMAL_TEXT draw normally
21878 DRAW_CURSOR draw in cursor face
21879 DRAW_MOUSE_FACE draw in mouse face.
21880 DRAW_INVERSE_VIDEO draw in mode line face
21881 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21882 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21883
21884 If OVERLAPS is non-zero, draw only the foreground of characters and
21885 clip to the physical height of ROW. Non-zero value also defines
21886 the overlapping part to be drawn:
21887
21888 OVERLAPS_PRED overlap with preceding rows
21889 OVERLAPS_SUCC overlap with succeeding rows
21890 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21891 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21892
21893 Value is the x-position reached, relative to AREA of W. */
21894
21895 static int
21896 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21897 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21898 enum draw_glyphs_face hl, int overlaps)
21899 {
21900 struct glyph_string *head, *tail;
21901 struct glyph_string *s;
21902 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21903 int i, j, x_reached, last_x, area_left = 0;
21904 struct frame *f = XFRAME (WINDOW_FRAME (w));
21905 DECLARE_HDC (hdc);
21906
21907 ALLOCATE_HDC (hdc, f);
21908
21909 /* Let's rather be paranoid than getting a SEGV. */
21910 end = min (end, row->used[area]);
21911 start = max (0, start);
21912 start = min (end, start);
21913
21914 /* Translate X to frame coordinates. Set last_x to the right
21915 end of the drawing area. */
21916 if (row->full_width_p)
21917 {
21918 /* X is relative to the left edge of W, without scroll bars
21919 or fringes. */
21920 area_left = WINDOW_LEFT_EDGE_X (w);
21921 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21922 }
21923 else
21924 {
21925 area_left = window_box_left (w, area);
21926 last_x = area_left + window_box_width (w, area);
21927 }
21928 x += area_left;
21929
21930 /* Build a doubly-linked list of glyph_string structures between
21931 head and tail from what we have to draw. Note that the macro
21932 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21933 the reason we use a separate variable `i'. */
21934 i = start;
21935 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21936 if (tail)
21937 x_reached = tail->x + tail->background_width;
21938 else
21939 x_reached = x;
21940
21941 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21942 the row, redraw some glyphs in front or following the glyph
21943 strings built above. */
21944 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21945 {
21946 struct glyph_string *h, *t;
21947 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21948 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
21949 int check_mouse_face = 0;
21950 int dummy_x = 0;
21951
21952 /* If mouse highlighting is on, we may need to draw adjacent
21953 glyphs using mouse-face highlighting. */
21954 if (area == TEXT_AREA && row->mouse_face_p)
21955 {
21956 struct glyph_row *mouse_beg_row, *mouse_end_row;
21957
21958 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21959 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21960
21961 if (row >= mouse_beg_row && row <= mouse_end_row)
21962 {
21963 check_mouse_face = 1;
21964 mouse_beg_col = (row == mouse_beg_row)
21965 ? hlinfo->mouse_face_beg_col : 0;
21966 mouse_end_col = (row == mouse_end_row)
21967 ? hlinfo->mouse_face_end_col
21968 : row->used[TEXT_AREA];
21969 }
21970 }
21971
21972 /* Compute overhangs for all glyph strings. */
21973 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21974 for (s = head; s; s = s->next)
21975 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21976
21977 /* Prepend glyph strings for glyphs in front of the first glyph
21978 string that are overwritten because of the first glyph
21979 string's left overhang. The background of all strings
21980 prepended must be drawn because the first glyph string
21981 draws over it. */
21982 i = left_overwritten (head);
21983 if (i >= 0)
21984 {
21985 enum draw_glyphs_face overlap_hl;
21986
21987 /* If this row contains mouse highlighting, attempt to draw
21988 the overlapped glyphs with the correct highlight. This
21989 code fails if the overlap encompasses more than one glyph
21990 and mouse-highlight spans only some of these glyphs.
21991 However, making it work perfectly involves a lot more
21992 code, and I don't know if the pathological case occurs in
21993 practice, so we'll stick to this for now. --- cyd */
21994 if (check_mouse_face
21995 && mouse_beg_col < start && mouse_end_col > i)
21996 overlap_hl = DRAW_MOUSE_FACE;
21997 else
21998 overlap_hl = DRAW_NORMAL_TEXT;
21999
22000 j = i;
22001 BUILD_GLYPH_STRINGS (j, start, h, t,
22002 overlap_hl, dummy_x, last_x);
22003 start = i;
22004 compute_overhangs_and_x (t, head->x, 1);
22005 prepend_glyph_string_lists (&head, &tail, h, t);
22006 clip_head = head;
22007 }
22008
22009 /* Prepend glyph strings for glyphs in front of the first glyph
22010 string that overwrite that glyph string because of their
22011 right overhang. For these strings, only the foreground must
22012 be drawn, because it draws over the glyph string at `head'.
22013 The background must not be drawn because this would overwrite
22014 right overhangs of preceding glyphs for which no glyph
22015 strings exist. */
22016 i = left_overwriting (head);
22017 if (i >= 0)
22018 {
22019 enum draw_glyphs_face overlap_hl;
22020
22021 if (check_mouse_face
22022 && mouse_beg_col < start && mouse_end_col > i)
22023 overlap_hl = DRAW_MOUSE_FACE;
22024 else
22025 overlap_hl = DRAW_NORMAL_TEXT;
22026
22027 clip_head = head;
22028 BUILD_GLYPH_STRINGS (i, start, h, t,
22029 overlap_hl, dummy_x, last_x);
22030 for (s = h; s; s = s->next)
22031 s->background_filled_p = 1;
22032 compute_overhangs_and_x (t, head->x, 1);
22033 prepend_glyph_string_lists (&head, &tail, h, t);
22034 }
22035
22036 /* Append glyphs strings for glyphs following the last glyph
22037 string tail that are overwritten by tail. The background of
22038 these strings has to be drawn because tail's foreground draws
22039 over it. */
22040 i = right_overwritten (tail);
22041 if (i >= 0)
22042 {
22043 enum draw_glyphs_face overlap_hl;
22044
22045 if (check_mouse_face
22046 && mouse_beg_col < i && mouse_end_col > end)
22047 overlap_hl = DRAW_MOUSE_FACE;
22048 else
22049 overlap_hl = DRAW_NORMAL_TEXT;
22050
22051 BUILD_GLYPH_STRINGS (end, i, h, t,
22052 overlap_hl, x, last_x);
22053 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22054 we don't have `end = i;' here. */
22055 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22056 append_glyph_string_lists (&head, &tail, h, t);
22057 clip_tail = tail;
22058 }
22059
22060 /* Append glyph strings for glyphs following the last glyph
22061 string tail that overwrite tail. The foreground of such
22062 glyphs has to be drawn because it writes into the background
22063 of tail. The background must not be drawn because it could
22064 paint over the foreground of following glyphs. */
22065 i = right_overwriting (tail);
22066 if (i >= 0)
22067 {
22068 enum draw_glyphs_face overlap_hl;
22069 if (check_mouse_face
22070 && mouse_beg_col < i && mouse_end_col > end)
22071 overlap_hl = DRAW_MOUSE_FACE;
22072 else
22073 overlap_hl = DRAW_NORMAL_TEXT;
22074
22075 clip_tail = tail;
22076 i++; /* We must include the Ith glyph. */
22077 BUILD_GLYPH_STRINGS (end, i, h, t,
22078 overlap_hl, x, last_x);
22079 for (s = h; s; s = s->next)
22080 s->background_filled_p = 1;
22081 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22082 append_glyph_string_lists (&head, &tail, h, t);
22083 }
22084 if (clip_head || clip_tail)
22085 for (s = head; s; s = s->next)
22086 {
22087 s->clip_head = clip_head;
22088 s->clip_tail = clip_tail;
22089 }
22090 }
22091
22092 /* Draw all strings. */
22093 for (s = head; s; s = s->next)
22094 FRAME_RIF (f)->draw_glyph_string (s);
22095
22096 #ifndef HAVE_NS
22097 /* When focus a sole frame and move horizontally, this sets on_p to 0
22098 causing a failure to erase prev cursor position. */
22099 if (area == TEXT_AREA
22100 && !row->full_width_p
22101 /* When drawing overlapping rows, only the glyph strings'
22102 foreground is drawn, which doesn't erase a cursor
22103 completely. */
22104 && !overlaps)
22105 {
22106 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22107 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22108 : (tail ? tail->x + tail->background_width : x));
22109 x0 -= area_left;
22110 x1 -= area_left;
22111
22112 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22113 row->y, MATRIX_ROW_BOTTOM_Y (row));
22114 }
22115 #endif
22116
22117 /* Value is the x-position up to which drawn, relative to AREA of W.
22118 This doesn't include parts drawn because of overhangs. */
22119 if (row->full_width_p)
22120 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22121 else
22122 x_reached -= area_left;
22123
22124 RELEASE_HDC (hdc, f);
22125
22126 return x_reached;
22127 }
22128
22129 /* Expand row matrix if too narrow. Don't expand if area
22130 is not present. */
22131
22132 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22133 { \
22134 if (!fonts_changed_p \
22135 && (it->glyph_row->glyphs[area] \
22136 < it->glyph_row->glyphs[area + 1])) \
22137 { \
22138 it->w->ncols_scale_factor++; \
22139 fonts_changed_p = 1; \
22140 } \
22141 }
22142
22143 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22144 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22145
22146 static INLINE void
22147 append_glyph (struct it *it)
22148 {
22149 struct glyph *glyph;
22150 enum glyph_row_area area = it->area;
22151
22152 xassert (it->glyph_row);
22153 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22154
22155 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22156 if (glyph < it->glyph_row->glyphs[area + 1])
22157 {
22158 /* If the glyph row is reversed, we need to prepend the glyph
22159 rather than append it. */
22160 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22161 {
22162 struct glyph *g;
22163
22164 /* Make room for the additional glyph. */
22165 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22166 g[1] = *g;
22167 glyph = it->glyph_row->glyphs[area];
22168 }
22169 glyph->charpos = CHARPOS (it->position);
22170 glyph->object = it->object;
22171 if (it->pixel_width > 0)
22172 {
22173 glyph->pixel_width = it->pixel_width;
22174 glyph->padding_p = 0;
22175 }
22176 else
22177 {
22178 /* Assure at least 1-pixel width. Otherwise, cursor can't
22179 be displayed correctly. */
22180 glyph->pixel_width = 1;
22181 glyph->padding_p = 1;
22182 }
22183 glyph->ascent = it->ascent;
22184 glyph->descent = it->descent;
22185 glyph->voffset = it->voffset;
22186 glyph->type = CHAR_GLYPH;
22187 glyph->avoid_cursor_p = it->avoid_cursor_p;
22188 glyph->multibyte_p = it->multibyte_p;
22189 glyph->left_box_line_p = it->start_of_box_run_p;
22190 glyph->right_box_line_p = it->end_of_box_run_p;
22191 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22192 || it->phys_descent > it->descent);
22193 glyph->glyph_not_available_p = it->glyph_not_available_p;
22194 glyph->face_id = it->face_id;
22195 glyph->u.ch = it->char_to_display;
22196 glyph->slice.img = null_glyph_slice;
22197 glyph->font_type = FONT_TYPE_UNKNOWN;
22198 if (it->bidi_p)
22199 {
22200 glyph->resolved_level = it->bidi_it.resolved_level;
22201 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22202 abort ();
22203 glyph->bidi_type = it->bidi_it.type;
22204 }
22205 else
22206 {
22207 glyph->resolved_level = 0;
22208 glyph->bidi_type = UNKNOWN_BT;
22209 }
22210 ++it->glyph_row->used[area];
22211 }
22212 else
22213 IT_EXPAND_MATRIX_WIDTH (it, area);
22214 }
22215
22216 /* Store one glyph for the composition IT->cmp_it.id in
22217 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22218 non-null. */
22219
22220 static INLINE void
22221 append_composite_glyph (struct it *it)
22222 {
22223 struct glyph *glyph;
22224 enum glyph_row_area area = it->area;
22225
22226 xassert (it->glyph_row);
22227
22228 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22229 if (glyph < it->glyph_row->glyphs[area + 1])
22230 {
22231 /* If the glyph row is reversed, we need to prepend the glyph
22232 rather than append it. */
22233 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22234 {
22235 struct glyph *g;
22236
22237 /* Make room for the new glyph. */
22238 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22239 g[1] = *g;
22240 glyph = it->glyph_row->glyphs[it->area];
22241 }
22242 glyph->charpos = it->cmp_it.charpos;
22243 glyph->object = it->object;
22244 glyph->pixel_width = it->pixel_width;
22245 glyph->ascent = it->ascent;
22246 glyph->descent = it->descent;
22247 glyph->voffset = it->voffset;
22248 glyph->type = COMPOSITE_GLYPH;
22249 if (it->cmp_it.ch < 0)
22250 {
22251 glyph->u.cmp.automatic = 0;
22252 glyph->u.cmp.id = it->cmp_it.id;
22253 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22254 }
22255 else
22256 {
22257 glyph->u.cmp.automatic = 1;
22258 glyph->u.cmp.id = it->cmp_it.id;
22259 glyph->slice.cmp.from = it->cmp_it.from;
22260 glyph->slice.cmp.to = it->cmp_it.to - 1;
22261 }
22262 glyph->avoid_cursor_p = it->avoid_cursor_p;
22263 glyph->multibyte_p = it->multibyte_p;
22264 glyph->left_box_line_p = it->start_of_box_run_p;
22265 glyph->right_box_line_p = it->end_of_box_run_p;
22266 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22267 || it->phys_descent > it->descent);
22268 glyph->padding_p = 0;
22269 glyph->glyph_not_available_p = 0;
22270 glyph->face_id = it->face_id;
22271 glyph->font_type = FONT_TYPE_UNKNOWN;
22272 if (it->bidi_p)
22273 {
22274 glyph->resolved_level = it->bidi_it.resolved_level;
22275 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22276 abort ();
22277 glyph->bidi_type = it->bidi_it.type;
22278 }
22279 ++it->glyph_row->used[area];
22280 }
22281 else
22282 IT_EXPAND_MATRIX_WIDTH (it, area);
22283 }
22284
22285
22286 /* Change IT->ascent and IT->height according to the setting of
22287 IT->voffset. */
22288
22289 static INLINE void
22290 take_vertical_position_into_account (struct it *it)
22291 {
22292 if (it->voffset)
22293 {
22294 if (it->voffset < 0)
22295 /* Increase the ascent so that we can display the text higher
22296 in the line. */
22297 it->ascent -= it->voffset;
22298 else
22299 /* Increase the descent so that we can display the text lower
22300 in the line. */
22301 it->descent += it->voffset;
22302 }
22303 }
22304
22305
22306 /* Produce glyphs/get display metrics for the image IT is loaded with.
22307 See the description of struct display_iterator in dispextern.h for
22308 an overview of struct display_iterator. */
22309
22310 static void
22311 produce_image_glyph (struct it *it)
22312 {
22313 struct image *img;
22314 struct face *face;
22315 int glyph_ascent, crop;
22316 struct glyph_slice slice;
22317
22318 xassert (it->what == IT_IMAGE);
22319
22320 face = FACE_FROM_ID (it->f, it->face_id);
22321 xassert (face);
22322 /* Make sure X resources of the face is loaded. */
22323 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22324
22325 if (it->image_id < 0)
22326 {
22327 /* Fringe bitmap. */
22328 it->ascent = it->phys_ascent = 0;
22329 it->descent = it->phys_descent = 0;
22330 it->pixel_width = 0;
22331 it->nglyphs = 0;
22332 return;
22333 }
22334
22335 img = IMAGE_FROM_ID (it->f, it->image_id);
22336 xassert (img);
22337 /* Make sure X resources of the image is loaded. */
22338 prepare_image_for_display (it->f, img);
22339
22340 slice.x = slice.y = 0;
22341 slice.width = img->width;
22342 slice.height = img->height;
22343
22344 if (INTEGERP (it->slice.x))
22345 slice.x = XINT (it->slice.x);
22346 else if (FLOATP (it->slice.x))
22347 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22348
22349 if (INTEGERP (it->slice.y))
22350 slice.y = XINT (it->slice.y);
22351 else if (FLOATP (it->slice.y))
22352 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22353
22354 if (INTEGERP (it->slice.width))
22355 slice.width = XINT (it->slice.width);
22356 else if (FLOATP (it->slice.width))
22357 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22358
22359 if (INTEGERP (it->slice.height))
22360 slice.height = XINT (it->slice.height);
22361 else if (FLOATP (it->slice.height))
22362 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22363
22364 if (slice.x >= img->width)
22365 slice.x = img->width;
22366 if (slice.y >= img->height)
22367 slice.y = img->height;
22368 if (slice.x + slice.width >= img->width)
22369 slice.width = img->width - slice.x;
22370 if (slice.y + slice.height > img->height)
22371 slice.height = img->height - slice.y;
22372
22373 if (slice.width == 0 || slice.height == 0)
22374 return;
22375
22376 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22377
22378 it->descent = slice.height - glyph_ascent;
22379 if (slice.y == 0)
22380 it->descent += img->vmargin;
22381 if (slice.y + slice.height == img->height)
22382 it->descent += img->vmargin;
22383 it->phys_descent = it->descent;
22384
22385 it->pixel_width = slice.width;
22386 if (slice.x == 0)
22387 it->pixel_width += img->hmargin;
22388 if (slice.x + slice.width == img->width)
22389 it->pixel_width += img->hmargin;
22390
22391 /* It's quite possible for images to have an ascent greater than
22392 their height, so don't get confused in that case. */
22393 if (it->descent < 0)
22394 it->descent = 0;
22395
22396 it->nglyphs = 1;
22397
22398 if (face->box != FACE_NO_BOX)
22399 {
22400 if (face->box_line_width > 0)
22401 {
22402 if (slice.y == 0)
22403 it->ascent += face->box_line_width;
22404 if (slice.y + slice.height == img->height)
22405 it->descent += face->box_line_width;
22406 }
22407
22408 if (it->start_of_box_run_p && slice.x == 0)
22409 it->pixel_width += eabs (face->box_line_width);
22410 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22411 it->pixel_width += eabs (face->box_line_width);
22412 }
22413
22414 take_vertical_position_into_account (it);
22415
22416 /* Automatically crop wide image glyphs at right edge so we can
22417 draw the cursor on same display row. */
22418 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22419 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22420 {
22421 it->pixel_width -= crop;
22422 slice.width -= crop;
22423 }
22424
22425 if (it->glyph_row)
22426 {
22427 struct glyph *glyph;
22428 enum glyph_row_area area = it->area;
22429
22430 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22431 if (glyph < it->glyph_row->glyphs[area + 1])
22432 {
22433 glyph->charpos = CHARPOS (it->position);
22434 glyph->object = it->object;
22435 glyph->pixel_width = it->pixel_width;
22436 glyph->ascent = glyph_ascent;
22437 glyph->descent = it->descent;
22438 glyph->voffset = it->voffset;
22439 glyph->type = IMAGE_GLYPH;
22440 glyph->avoid_cursor_p = it->avoid_cursor_p;
22441 glyph->multibyte_p = it->multibyte_p;
22442 glyph->left_box_line_p = it->start_of_box_run_p;
22443 glyph->right_box_line_p = it->end_of_box_run_p;
22444 glyph->overlaps_vertically_p = 0;
22445 glyph->padding_p = 0;
22446 glyph->glyph_not_available_p = 0;
22447 glyph->face_id = it->face_id;
22448 glyph->u.img_id = img->id;
22449 glyph->slice.img = slice;
22450 glyph->font_type = FONT_TYPE_UNKNOWN;
22451 if (it->bidi_p)
22452 {
22453 glyph->resolved_level = it->bidi_it.resolved_level;
22454 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22455 abort ();
22456 glyph->bidi_type = it->bidi_it.type;
22457 }
22458 ++it->glyph_row->used[area];
22459 }
22460 else
22461 IT_EXPAND_MATRIX_WIDTH (it, area);
22462 }
22463 }
22464
22465
22466 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22467 of the glyph, WIDTH and HEIGHT are the width and height of the
22468 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22469
22470 static void
22471 append_stretch_glyph (struct it *it, Lisp_Object object,
22472 int width, int height, int ascent)
22473 {
22474 struct glyph *glyph;
22475 enum glyph_row_area area = it->area;
22476
22477 xassert (ascent >= 0 && ascent <= height);
22478
22479 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22480 if (glyph < it->glyph_row->glyphs[area + 1])
22481 {
22482 /* If the glyph row is reversed, we need to prepend the glyph
22483 rather than append it. */
22484 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22485 {
22486 struct glyph *g;
22487
22488 /* Make room for the additional glyph. */
22489 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22490 g[1] = *g;
22491 glyph = it->glyph_row->glyphs[area];
22492 }
22493 glyph->charpos = CHARPOS (it->position);
22494 glyph->object = object;
22495 glyph->pixel_width = width;
22496 glyph->ascent = ascent;
22497 glyph->descent = height - ascent;
22498 glyph->voffset = it->voffset;
22499 glyph->type = STRETCH_GLYPH;
22500 glyph->avoid_cursor_p = it->avoid_cursor_p;
22501 glyph->multibyte_p = it->multibyte_p;
22502 glyph->left_box_line_p = it->start_of_box_run_p;
22503 glyph->right_box_line_p = it->end_of_box_run_p;
22504 glyph->overlaps_vertically_p = 0;
22505 glyph->padding_p = 0;
22506 glyph->glyph_not_available_p = 0;
22507 glyph->face_id = it->face_id;
22508 glyph->u.stretch.ascent = ascent;
22509 glyph->u.stretch.height = height;
22510 glyph->slice.img = null_glyph_slice;
22511 glyph->font_type = FONT_TYPE_UNKNOWN;
22512 if (it->bidi_p)
22513 {
22514 glyph->resolved_level = it->bidi_it.resolved_level;
22515 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22516 abort ();
22517 glyph->bidi_type = it->bidi_it.type;
22518 }
22519 else
22520 {
22521 glyph->resolved_level = 0;
22522 glyph->bidi_type = UNKNOWN_BT;
22523 }
22524 ++it->glyph_row->used[area];
22525 }
22526 else
22527 IT_EXPAND_MATRIX_WIDTH (it, area);
22528 }
22529
22530
22531 /* Produce a stretch glyph for iterator IT. IT->object is the value
22532 of the glyph property displayed. The value must be a list
22533 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22534 being recognized:
22535
22536 1. `:width WIDTH' specifies that the space should be WIDTH *
22537 canonical char width wide. WIDTH may be an integer or floating
22538 point number.
22539
22540 2. `:relative-width FACTOR' specifies that the width of the stretch
22541 should be computed from the width of the first character having the
22542 `glyph' property, and should be FACTOR times that width.
22543
22544 3. `:align-to HPOS' specifies that the space should be wide enough
22545 to reach HPOS, a value in canonical character units.
22546
22547 Exactly one of the above pairs must be present.
22548
22549 4. `:height HEIGHT' specifies that the height of the stretch produced
22550 should be HEIGHT, measured in canonical character units.
22551
22552 5. `:relative-height FACTOR' specifies that the height of the
22553 stretch should be FACTOR times the height of the characters having
22554 the glyph property.
22555
22556 Either none or exactly one of 4 or 5 must be present.
22557
22558 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22559 of the stretch should be used for the ascent of the stretch.
22560 ASCENT must be in the range 0 <= ASCENT <= 100. */
22561
22562 static void
22563 produce_stretch_glyph (struct it *it)
22564 {
22565 /* (space :width WIDTH :height HEIGHT ...) */
22566 Lisp_Object prop, plist;
22567 int width = 0, height = 0, align_to = -1;
22568 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22569 int ascent = 0;
22570 double tem;
22571 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22572 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22573
22574 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22575
22576 /* List should start with `space'. */
22577 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22578 plist = XCDR (it->object);
22579
22580 /* Compute the width of the stretch. */
22581 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22582 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22583 {
22584 /* Absolute width `:width WIDTH' specified and valid. */
22585 zero_width_ok_p = 1;
22586 width = (int)tem;
22587 }
22588 else if (prop = Fplist_get (plist, QCrelative_width),
22589 NUMVAL (prop) > 0)
22590 {
22591 /* Relative width `:relative-width FACTOR' specified and valid.
22592 Compute the width of the characters having the `glyph'
22593 property. */
22594 struct it it2;
22595 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22596
22597 it2 = *it;
22598 if (it->multibyte_p)
22599 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22600 else
22601 {
22602 it2.c = it2.char_to_display = *p, it2.len = 1;
22603 if (! ASCII_CHAR_P (it2.c))
22604 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22605 }
22606
22607 it2.glyph_row = NULL;
22608 it2.what = IT_CHARACTER;
22609 x_produce_glyphs (&it2);
22610 width = NUMVAL (prop) * it2.pixel_width;
22611 }
22612 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22613 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22614 {
22615 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22616 align_to = (align_to < 0
22617 ? 0
22618 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22619 else if (align_to < 0)
22620 align_to = window_box_left_offset (it->w, TEXT_AREA);
22621 width = max (0, (int)tem + align_to - it->current_x);
22622 zero_width_ok_p = 1;
22623 }
22624 else
22625 /* Nothing specified -> width defaults to canonical char width. */
22626 width = FRAME_COLUMN_WIDTH (it->f);
22627
22628 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22629 width = 1;
22630
22631 /* Compute height. */
22632 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22633 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22634 {
22635 height = (int)tem;
22636 zero_height_ok_p = 1;
22637 }
22638 else if (prop = Fplist_get (plist, QCrelative_height),
22639 NUMVAL (prop) > 0)
22640 height = FONT_HEIGHT (font) * NUMVAL (prop);
22641 else
22642 height = FONT_HEIGHT (font);
22643
22644 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22645 height = 1;
22646
22647 /* Compute percentage of height used for ascent. If
22648 `:ascent ASCENT' is present and valid, use that. Otherwise,
22649 derive the ascent from the font in use. */
22650 if (prop = Fplist_get (plist, QCascent),
22651 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22652 ascent = height * NUMVAL (prop) / 100.0;
22653 else if (!NILP (prop)
22654 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22655 ascent = min (max (0, (int)tem), height);
22656 else
22657 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22658
22659 if (width > 0 && it->line_wrap != TRUNCATE
22660 && it->current_x + width > it->last_visible_x)
22661 width = it->last_visible_x - it->current_x - 1;
22662
22663 if (width > 0 && height > 0 && it->glyph_row)
22664 {
22665 Lisp_Object object = it->stack[it->sp - 1].string;
22666 if (!STRINGP (object))
22667 object = it->w->buffer;
22668 append_stretch_glyph (it, object, width, height, ascent);
22669 }
22670
22671 it->pixel_width = width;
22672 it->ascent = it->phys_ascent = ascent;
22673 it->descent = it->phys_descent = height - it->ascent;
22674 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22675
22676 take_vertical_position_into_account (it);
22677 }
22678
22679 /* Calculate line-height and line-spacing properties.
22680 An integer value specifies explicit pixel value.
22681 A float value specifies relative value to current face height.
22682 A cons (float . face-name) specifies relative value to
22683 height of specified face font.
22684
22685 Returns height in pixels, or nil. */
22686
22687
22688 static Lisp_Object
22689 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22690 int boff, int override)
22691 {
22692 Lisp_Object face_name = Qnil;
22693 int ascent, descent, height;
22694
22695 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22696 return val;
22697
22698 if (CONSP (val))
22699 {
22700 face_name = XCAR (val);
22701 val = XCDR (val);
22702 if (!NUMBERP (val))
22703 val = make_number (1);
22704 if (NILP (face_name))
22705 {
22706 height = it->ascent + it->descent;
22707 goto scale;
22708 }
22709 }
22710
22711 if (NILP (face_name))
22712 {
22713 font = FRAME_FONT (it->f);
22714 boff = FRAME_BASELINE_OFFSET (it->f);
22715 }
22716 else if (EQ (face_name, Qt))
22717 {
22718 override = 0;
22719 }
22720 else
22721 {
22722 int face_id;
22723 struct face *face;
22724
22725 face_id = lookup_named_face (it->f, face_name, 0);
22726 if (face_id < 0)
22727 return make_number (-1);
22728
22729 face = FACE_FROM_ID (it->f, face_id);
22730 font = face->font;
22731 if (font == NULL)
22732 return make_number (-1);
22733 boff = font->baseline_offset;
22734 if (font->vertical_centering)
22735 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22736 }
22737
22738 ascent = FONT_BASE (font) + boff;
22739 descent = FONT_DESCENT (font) - boff;
22740
22741 if (override)
22742 {
22743 it->override_ascent = ascent;
22744 it->override_descent = descent;
22745 it->override_boff = boff;
22746 }
22747
22748 height = ascent + descent;
22749
22750 scale:
22751 if (FLOATP (val))
22752 height = (int)(XFLOAT_DATA (val) * height);
22753 else if (INTEGERP (val))
22754 height *= XINT (val);
22755
22756 return make_number (height);
22757 }
22758
22759
22760 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22761 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22762 and only if this is for a character for which no font was found.
22763
22764 If the display method (it->glyphless_method) is
22765 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22766 length of the acronym or the hexadecimal string, UPPER_XOFF and
22767 UPPER_YOFF are pixel offsets for the upper part of the string,
22768 LOWER_XOFF and LOWER_YOFF are for the lower part.
22769
22770 For the other display methods, LEN through LOWER_YOFF are zero. */
22771
22772 static void
22773 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22774 short upper_xoff, short upper_yoff,
22775 short lower_xoff, short lower_yoff)
22776 {
22777 struct glyph *glyph;
22778 enum glyph_row_area area = it->area;
22779
22780 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22781 if (glyph < it->glyph_row->glyphs[area + 1])
22782 {
22783 /* If the glyph row is reversed, we need to prepend the glyph
22784 rather than append it. */
22785 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22786 {
22787 struct glyph *g;
22788
22789 /* Make room for the additional glyph. */
22790 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22791 g[1] = *g;
22792 glyph = it->glyph_row->glyphs[area];
22793 }
22794 glyph->charpos = CHARPOS (it->position);
22795 glyph->object = it->object;
22796 glyph->pixel_width = it->pixel_width;
22797 glyph->ascent = it->ascent;
22798 glyph->descent = it->descent;
22799 glyph->voffset = it->voffset;
22800 glyph->type = GLYPHLESS_GLYPH;
22801 glyph->u.glyphless.method = it->glyphless_method;
22802 glyph->u.glyphless.for_no_font = for_no_font;
22803 glyph->u.glyphless.len = len;
22804 glyph->u.glyphless.ch = it->c;
22805 glyph->slice.glyphless.upper_xoff = upper_xoff;
22806 glyph->slice.glyphless.upper_yoff = upper_yoff;
22807 glyph->slice.glyphless.lower_xoff = lower_xoff;
22808 glyph->slice.glyphless.lower_yoff = lower_yoff;
22809 glyph->avoid_cursor_p = it->avoid_cursor_p;
22810 glyph->multibyte_p = it->multibyte_p;
22811 glyph->left_box_line_p = it->start_of_box_run_p;
22812 glyph->right_box_line_p = it->end_of_box_run_p;
22813 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22814 || it->phys_descent > it->descent);
22815 glyph->padding_p = 0;
22816 glyph->glyph_not_available_p = 0;
22817 glyph->face_id = face_id;
22818 glyph->font_type = FONT_TYPE_UNKNOWN;
22819 if (it->bidi_p)
22820 {
22821 glyph->resolved_level = it->bidi_it.resolved_level;
22822 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22823 abort ();
22824 glyph->bidi_type = it->bidi_it.type;
22825 }
22826 ++it->glyph_row->used[area];
22827 }
22828 else
22829 IT_EXPAND_MATRIX_WIDTH (it, area);
22830 }
22831
22832
22833 /* Produce a glyph for a glyphless character for iterator IT.
22834 IT->glyphless_method specifies which method to use for displaying
22835 the character. See the description of enum
22836 glyphless_display_method in dispextern.h for the detail.
22837
22838 FOR_NO_FONT is nonzero if and only if this is for a character for
22839 which no font was found. ACRONYM, if non-nil, is an acronym string
22840 for the character. */
22841
22842 static void
22843 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22844 {
22845 int face_id;
22846 struct face *face;
22847 struct font *font;
22848 int base_width, base_height, width, height;
22849 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22850 int len;
22851
22852 /* Get the metrics of the base font. We always refer to the current
22853 ASCII face. */
22854 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22855 font = face->font ? face->font : FRAME_FONT (it->f);
22856 it->ascent = FONT_BASE (font) + font->baseline_offset;
22857 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22858 base_height = it->ascent + it->descent;
22859 base_width = font->average_width;
22860
22861 /* Get a face ID for the glyph by utilizing a cache (the same way as
22862 doen for `escape-glyph' in get_next_display_element). */
22863 if (it->f == last_glyphless_glyph_frame
22864 && it->face_id == last_glyphless_glyph_face_id)
22865 {
22866 face_id = last_glyphless_glyph_merged_face_id;
22867 }
22868 else
22869 {
22870 /* Merge the `glyphless-char' face into the current face. */
22871 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22872 last_glyphless_glyph_frame = it->f;
22873 last_glyphless_glyph_face_id = it->face_id;
22874 last_glyphless_glyph_merged_face_id = face_id;
22875 }
22876
22877 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22878 {
22879 it->pixel_width = THIN_SPACE_WIDTH;
22880 len = 0;
22881 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22882 }
22883 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22884 {
22885 width = CHAR_WIDTH (it->c);
22886 if (width == 0)
22887 width = 1;
22888 else if (width > 4)
22889 width = 4;
22890 it->pixel_width = base_width * width;
22891 len = 0;
22892 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22893 }
22894 else
22895 {
22896 char buf[7];
22897 const char *str;
22898 unsigned int code[6];
22899 int upper_len;
22900 int ascent, descent;
22901 struct font_metrics metrics_upper, metrics_lower;
22902
22903 face = FACE_FROM_ID (it->f, face_id);
22904 font = face->font ? face->font : FRAME_FONT (it->f);
22905 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22906
22907 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22908 {
22909 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22910 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22911 if (CONSP (acronym))
22912 acronym = XCAR (acronym);
22913 str = STRINGP (acronym) ? SSDATA (acronym) : "";
22914 }
22915 else
22916 {
22917 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22918 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22919 str = buf;
22920 }
22921 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22922 code[len] = font->driver->encode_char (font, str[len]);
22923 upper_len = (len + 1) / 2;
22924 font->driver->text_extents (font, code, upper_len,
22925 &metrics_upper);
22926 font->driver->text_extents (font, code + upper_len, len - upper_len,
22927 &metrics_lower);
22928
22929
22930
22931 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22932 width = max (metrics_upper.width, metrics_lower.width) + 4;
22933 upper_xoff = upper_yoff = 2; /* the typical case */
22934 if (base_width >= width)
22935 {
22936 /* Align the upper to the left, the lower to the right. */
22937 it->pixel_width = base_width;
22938 lower_xoff = base_width - 2 - metrics_lower.width;
22939 }
22940 else
22941 {
22942 /* Center the shorter one. */
22943 it->pixel_width = width;
22944 if (metrics_upper.width >= metrics_lower.width)
22945 lower_xoff = (width - metrics_lower.width) / 2;
22946 else
22947 {
22948 /* FIXME: This code doesn't look right. It formerly was
22949 missing the "lower_xoff = 0;", which couldn't have
22950 been right since it left lower_xoff uninitialized. */
22951 lower_xoff = 0;
22952 upper_xoff = (width - metrics_upper.width) / 2;
22953 }
22954 }
22955
22956 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22957 top, bottom, and between upper and lower strings. */
22958 height = (metrics_upper.ascent + metrics_upper.descent
22959 + metrics_lower.ascent + metrics_lower.descent) + 5;
22960 /* Center vertically.
22961 H:base_height, D:base_descent
22962 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22963
22964 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22965 descent = D - H/2 + h/2;
22966 lower_yoff = descent - 2 - ld;
22967 upper_yoff = lower_yoff - la - 1 - ud; */
22968 ascent = - (it->descent - (base_height + height + 1) / 2);
22969 descent = it->descent - (base_height - height) / 2;
22970 lower_yoff = descent - 2 - metrics_lower.descent;
22971 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22972 - metrics_upper.descent);
22973 /* Don't make the height shorter than the base height. */
22974 if (height > base_height)
22975 {
22976 it->ascent = ascent;
22977 it->descent = descent;
22978 }
22979 }
22980
22981 it->phys_ascent = it->ascent;
22982 it->phys_descent = it->descent;
22983 if (it->glyph_row)
22984 append_glyphless_glyph (it, face_id, for_no_font, len,
22985 upper_xoff, upper_yoff,
22986 lower_xoff, lower_yoff);
22987 it->nglyphs = 1;
22988 take_vertical_position_into_account (it);
22989 }
22990
22991
22992 /* RIF:
22993 Produce glyphs/get display metrics for the display element IT is
22994 loaded with. See the description of struct it in dispextern.h
22995 for an overview of struct it. */
22996
22997 void
22998 x_produce_glyphs (struct it *it)
22999 {
23000 int extra_line_spacing = it->extra_line_spacing;
23001
23002 it->glyph_not_available_p = 0;
23003
23004 if (it->what == IT_CHARACTER)
23005 {
23006 XChar2b char2b;
23007 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23008 struct font *font = face->font;
23009 struct font_metrics *pcm = NULL;
23010 int boff; /* baseline offset */
23011
23012 if (font == NULL)
23013 {
23014 /* When no suitable font is found, display this character by
23015 the method specified in the first extra slot of
23016 Vglyphless_char_display. */
23017 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23018
23019 xassert (it->what == IT_GLYPHLESS);
23020 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23021 goto done;
23022 }
23023
23024 boff = font->baseline_offset;
23025 if (font->vertical_centering)
23026 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23027
23028 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23029 {
23030 int stretched_p;
23031
23032 it->nglyphs = 1;
23033
23034 if (it->override_ascent >= 0)
23035 {
23036 it->ascent = it->override_ascent;
23037 it->descent = it->override_descent;
23038 boff = it->override_boff;
23039 }
23040 else
23041 {
23042 it->ascent = FONT_BASE (font) + boff;
23043 it->descent = FONT_DESCENT (font) - boff;
23044 }
23045
23046 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23047 {
23048 pcm = get_per_char_metric (font, &char2b);
23049 if (pcm->width == 0
23050 && pcm->rbearing == 0 && pcm->lbearing == 0)
23051 pcm = NULL;
23052 }
23053
23054 if (pcm)
23055 {
23056 it->phys_ascent = pcm->ascent + boff;
23057 it->phys_descent = pcm->descent - boff;
23058 it->pixel_width = pcm->width;
23059 }
23060 else
23061 {
23062 it->glyph_not_available_p = 1;
23063 it->phys_ascent = it->ascent;
23064 it->phys_descent = it->descent;
23065 it->pixel_width = font->space_width;
23066 }
23067
23068 if (it->constrain_row_ascent_descent_p)
23069 {
23070 if (it->descent > it->max_descent)
23071 {
23072 it->ascent += it->descent - it->max_descent;
23073 it->descent = it->max_descent;
23074 }
23075 if (it->ascent > it->max_ascent)
23076 {
23077 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23078 it->ascent = it->max_ascent;
23079 }
23080 it->phys_ascent = min (it->phys_ascent, it->ascent);
23081 it->phys_descent = min (it->phys_descent, it->descent);
23082 extra_line_spacing = 0;
23083 }
23084
23085 /* If this is a space inside a region of text with
23086 `space-width' property, change its width. */
23087 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23088 if (stretched_p)
23089 it->pixel_width *= XFLOATINT (it->space_width);
23090
23091 /* If face has a box, add the box thickness to the character
23092 height. If character has a box line to the left and/or
23093 right, add the box line width to the character's width. */
23094 if (face->box != FACE_NO_BOX)
23095 {
23096 int thick = face->box_line_width;
23097
23098 if (thick > 0)
23099 {
23100 it->ascent += thick;
23101 it->descent += thick;
23102 }
23103 else
23104 thick = -thick;
23105
23106 if (it->start_of_box_run_p)
23107 it->pixel_width += thick;
23108 if (it->end_of_box_run_p)
23109 it->pixel_width += thick;
23110 }
23111
23112 /* If face has an overline, add the height of the overline
23113 (1 pixel) and a 1 pixel margin to the character height. */
23114 if (face->overline_p)
23115 it->ascent += overline_margin;
23116
23117 if (it->constrain_row_ascent_descent_p)
23118 {
23119 if (it->ascent > it->max_ascent)
23120 it->ascent = it->max_ascent;
23121 if (it->descent > it->max_descent)
23122 it->descent = it->max_descent;
23123 }
23124
23125 take_vertical_position_into_account (it);
23126
23127 /* If we have to actually produce glyphs, do it. */
23128 if (it->glyph_row)
23129 {
23130 if (stretched_p)
23131 {
23132 /* Translate a space with a `space-width' property
23133 into a stretch glyph. */
23134 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23135 / FONT_HEIGHT (font));
23136 append_stretch_glyph (it, it->object, it->pixel_width,
23137 it->ascent + it->descent, ascent);
23138 }
23139 else
23140 append_glyph (it);
23141
23142 /* If characters with lbearing or rbearing are displayed
23143 in this line, record that fact in a flag of the
23144 glyph row. This is used to optimize X output code. */
23145 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23146 it->glyph_row->contains_overlapping_glyphs_p = 1;
23147 }
23148 if (! stretched_p && it->pixel_width == 0)
23149 /* We assure that all visible glyphs have at least 1-pixel
23150 width. */
23151 it->pixel_width = 1;
23152 }
23153 else if (it->char_to_display == '\n')
23154 {
23155 /* A newline has no width, but we need the height of the
23156 line. But if previous part of the line sets a height,
23157 don't increase that height */
23158
23159 Lisp_Object height;
23160 Lisp_Object total_height = Qnil;
23161
23162 it->override_ascent = -1;
23163 it->pixel_width = 0;
23164 it->nglyphs = 0;
23165
23166 height = get_it_property (it, Qline_height);
23167 /* Split (line-height total-height) list */
23168 if (CONSP (height)
23169 && CONSP (XCDR (height))
23170 && NILP (XCDR (XCDR (height))))
23171 {
23172 total_height = XCAR (XCDR (height));
23173 height = XCAR (height);
23174 }
23175 height = calc_line_height_property (it, height, font, boff, 1);
23176
23177 if (it->override_ascent >= 0)
23178 {
23179 it->ascent = it->override_ascent;
23180 it->descent = it->override_descent;
23181 boff = it->override_boff;
23182 }
23183 else
23184 {
23185 it->ascent = FONT_BASE (font) + boff;
23186 it->descent = FONT_DESCENT (font) - boff;
23187 }
23188
23189 if (EQ (height, Qt))
23190 {
23191 if (it->descent > it->max_descent)
23192 {
23193 it->ascent += it->descent - it->max_descent;
23194 it->descent = it->max_descent;
23195 }
23196 if (it->ascent > it->max_ascent)
23197 {
23198 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23199 it->ascent = it->max_ascent;
23200 }
23201 it->phys_ascent = min (it->phys_ascent, it->ascent);
23202 it->phys_descent = min (it->phys_descent, it->descent);
23203 it->constrain_row_ascent_descent_p = 1;
23204 extra_line_spacing = 0;
23205 }
23206 else
23207 {
23208 Lisp_Object spacing;
23209
23210 it->phys_ascent = it->ascent;
23211 it->phys_descent = it->descent;
23212
23213 if ((it->max_ascent > 0 || it->max_descent > 0)
23214 && face->box != FACE_NO_BOX
23215 && face->box_line_width > 0)
23216 {
23217 it->ascent += face->box_line_width;
23218 it->descent += face->box_line_width;
23219 }
23220 if (!NILP (height)
23221 && XINT (height) > it->ascent + it->descent)
23222 it->ascent = XINT (height) - it->descent;
23223
23224 if (!NILP (total_height))
23225 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23226 else
23227 {
23228 spacing = get_it_property (it, Qline_spacing);
23229 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23230 }
23231 if (INTEGERP (spacing))
23232 {
23233 extra_line_spacing = XINT (spacing);
23234 if (!NILP (total_height))
23235 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23236 }
23237 }
23238 }
23239 else /* i.e. (it->char_to_display == '\t') */
23240 {
23241 if (font->space_width > 0)
23242 {
23243 int tab_width = it->tab_width * font->space_width;
23244 int x = it->current_x + it->continuation_lines_width;
23245 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23246
23247 /* If the distance from the current position to the next tab
23248 stop is less than a space character width, use the
23249 tab stop after that. */
23250 if (next_tab_x - x < font->space_width)
23251 next_tab_x += tab_width;
23252
23253 it->pixel_width = next_tab_x - x;
23254 it->nglyphs = 1;
23255 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23256 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23257
23258 if (it->glyph_row)
23259 {
23260 append_stretch_glyph (it, it->object, it->pixel_width,
23261 it->ascent + it->descent, it->ascent);
23262 }
23263 }
23264 else
23265 {
23266 it->pixel_width = 0;
23267 it->nglyphs = 1;
23268 }
23269 }
23270 }
23271 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23272 {
23273 /* A static composition.
23274
23275 Note: A composition is represented as one glyph in the
23276 glyph matrix. There are no padding glyphs.
23277
23278 Important note: pixel_width, ascent, and descent are the
23279 values of what is drawn by draw_glyphs (i.e. the values of
23280 the overall glyphs composed). */
23281 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23282 int boff; /* baseline offset */
23283 struct composition *cmp = composition_table[it->cmp_it.id];
23284 int glyph_len = cmp->glyph_len;
23285 struct font *font = face->font;
23286
23287 it->nglyphs = 1;
23288
23289 /* If we have not yet calculated pixel size data of glyphs of
23290 the composition for the current face font, calculate them
23291 now. Theoretically, we have to check all fonts for the
23292 glyphs, but that requires much time and memory space. So,
23293 here we check only the font of the first glyph. This may
23294 lead to incorrect display, but it's very rare, and C-l
23295 (recenter-top-bottom) can correct the display anyway. */
23296 if (! cmp->font || cmp->font != font)
23297 {
23298 /* Ascent and descent of the font of the first character
23299 of this composition (adjusted by baseline offset).
23300 Ascent and descent of overall glyphs should not be less
23301 than these, respectively. */
23302 int font_ascent, font_descent, font_height;
23303 /* Bounding box of the overall glyphs. */
23304 int leftmost, rightmost, lowest, highest;
23305 int lbearing, rbearing;
23306 int i, width, ascent, descent;
23307 int left_padded = 0, right_padded = 0;
23308 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23309 XChar2b char2b;
23310 struct font_metrics *pcm;
23311 int font_not_found_p;
23312 EMACS_INT pos;
23313
23314 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23315 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23316 break;
23317 if (glyph_len < cmp->glyph_len)
23318 right_padded = 1;
23319 for (i = 0; i < glyph_len; i++)
23320 {
23321 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23322 break;
23323 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23324 }
23325 if (i > 0)
23326 left_padded = 1;
23327
23328 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23329 : IT_CHARPOS (*it));
23330 /* If no suitable font is found, use the default font. */
23331 font_not_found_p = font == NULL;
23332 if (font_not_found_p)
23333 {
23334 face = face->ascii_face;
23335 font = face->font;
23336 }
23337 boff = font->baseline_offset;
23338 if (font->vertical_centering)
23339 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23340 font_ascent = FONT_BASE (font) + boff;
23341 font_descent = FONT_DESCENT (font) - boff;
23342 font_height = FONT_HEIGHT (font);
23343
23344 cmp->font = (void *) font;
23345
23346 pcm = NULL;
23347 if (! font_not_found_p)
23348 {
23349 get_char_face_and_encoding (it->f, c, it->face_id,
23350 &char2b, 0);
23351 pcm = get_per_char_metric (font, &char2b);
23352 }
23353
23354 /* Initialize the bounding box. */
23355 if (pcm)
23356 {
23357 width = pcm->width;
23358 ascent = pcm->ascent;
23359 descent = pcm->descent;
23360 lbearing = pcm->lbearing;
23361 rbearing = pcm->rbearing;
23362 }
23363 else
23364 {
23365 width = font->space_width;
23366 ascent = FONT_BASE (font);
23367 descent = FONT_DESCENT (font);
23368 lbearing = 0;
23369 rbearing = width;
23370 }
23371
23372 rightmost = width;
23373 leftmost = 0;
23374 lowest = - descent + boff;
23375 highest = ascent + boff;
23376
23377 if (! font_not_found_p
23378 && font->default_ascent
23379 && CHAR_TABLE_P (Vuse_default_ascent)
23380 && !NILP (Faref (Vuse_default_ascent,
23381 make_number (it->char_to_display))))
23382 highest = font->default_ascent + boff;
23383
23384 /* Draw the first glyph at the normal position. It may be
23385 shifted to right later if some other glyphs are drawn
23386 at the left. */
23387 cmp->offsets[i * 2] = 0;
23388 cmp->offsets[i * 2 + 1] = boff;
23389 cmp->lbearing = lbearing;
23390 cmp->rbearing = rbearing;
23391
23392 /* Set cmp->offsets for the remaining glyphs. */
23393 for (i++; i < glyph_len; i++)
23394 {
23395 int left, right, btm, top;
23396 int ch = COMPOSITION_GLYPH (cmp, i);
23397 int face_id;
23398 struct face *this_face;
23399
23400 if (ch == '\t')
23401 ch = ' ';
23402 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23403 this_face = FACE_FROM_ID (it->f, face_id);
23404 font = this_face->font;
23405
23406 if (font == NULL)
23407 pcm = NULL;
23408 else
23409 {
23410 get_char_face_and_encoding (it->f, ch, face_id,
23411 &char2b, 0);
23412 pcm = get_per_char_metric (font, &char2b);
23413 }
23414 if (! pcm)
23415 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23416 else
23417 {
23418 width = pcm->width;
23419 ascent = pcm->ascent;
23420 descent = pcm->descent;
23421 lbearing = pcm->lbearing;
23422 rbearing = pcm->rbearing;
23423 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23424 {
23425 /* Relative composition with or without
23426 alternate chars. */
23427 left = (leftmost + rightmost - width) / 2;
23428 btm = - descent + boff;
23429 if (font->relative_compose
23430 && (! CHAR_TABLE_P (Vignore_relative_composition)
23431 || NILP (Faref (Vignore_relative_composition,
23432 make_number (ch)))))
23433 {
23434
23435 if (- descent >= font->relative_compose)
23436 /* One extra pixel between two glyphs. */
23437 btm = highest + 1;
23438 else if (ascent <= 0)
23439 /* One extra pixel between two glyphs. */
23440 btm = lowest - 1 - ascent - descent;
23441 }
23442 }
23443 else
23444 {
23445 /* A composition rule is specified by an integer
23446 value that encodes global and new reference
23447 points (GREF and NREF). GREF and NREF are
23448 specified by numbers as below:
23449
23450 0---1---2 -- ascent
23451 | |
23452 | |
23453 | |
23454 9--10--11 -- center
23455 | |
23456 ---3---4---5--- baseline
23457 | |
23458 6---7---8 -- descent
23459 */
23460 int rule = COMPOSITION_RULE (cmp, i);
23461 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23462
23463 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23464 grefx = gref % 3, nrefx = nref % 3;
23465 grefy = gref / 3, nrefy = nref / 3;
23466 if (xoff)
23467 xoff = font_height * (xoff - 128) / 256;
23468 if (yoff)
23469 yoff = font_height * (yoff - 128) / 256;
23470
23471 left = (leftmost
23472 + grefx * (rightmost - leftmost) / 2
23473 - nrefx * width / 2
23474 + xoff);
23475
23476 btm = ((grefy == 0 ? highest
23477 : grefy == 1 ? 0
23478 : grefy == 2 ? lowest
23479 : (highest + lowest) / 2)
23480 - (nrefy == 0 ? ascent + descent
23481 : nrefy == 1 ? descent - boff
23482 : nrefy == 2 ? 0
23483 : (ascent + descent) / 2)
23484 + yoff);
23485 }
23486
23487 cmp->offsets[i * 2] = left;
23488 cmp->offsets[i * 2 + 1] = btm + descent;
23489
23490 /* Update the bounding box of the overall glyphs. */
23491 if (width > 0)
23492 {
23493 right = left + width;
23494 if (left < leftmost)
23495 leftmost = left;
23496 if (right > rightmost)
23497 rightmost = right;
23498 }
23499 top = btm + descent + ascent;
23500 if (top > highest)
23501 highest = top;
23502 if (btm < lowest)
23503 lowest = btm;
23504
23505 if (cmp->lbearing > left + lbearing)
23506 cmp->lbearing = left + lbearing;
23507 if (cmp->rbearing < left + rbearing)
23508 cmp->rbearing = left + rbearing;
23509 }
23510 }
23511
23512 /* If there are glyphs whose x-offsets are negative,
23513 shift all glyphs to the right and make all x-offsets
23514 non-negative. */
23515 if (leftmost < 0)
23516 {
23517 for (i = 0; i < cmp->glyph_len; i++)
23518 cmp->offsets[i * 2] -= leftmost;
23519 rightmost -= leftmost;
23520 cmp->lbearing -= leftmost;
23521 cmp->rbearing -= leftmost;
23522 }
23523
23524 if (left_padded && cmp->lbearing < 0)
23525 {
23526 for (i = 0; i < cmp->glyph_len; i++)
23527 cmp->offsets[i * 2] -= cmp->lbearing;
23528 rightmost -= cmp->lbearing;
23529 cmp->rbearing -= cmp->lbearing;
23530 cmp->lbearing = 0;
23531 }
23532 if (right_padded && rightmost < cmp->rbearing)
23533 {
23534 rightmost = cmp->rbearing;
23535 }
23536
23537 cmp->pixel_width = rightmost;
23538 cmp->ascent = highest;
23539 cmp->descent = - lowest;
23540 if (cmp->ascent < font_ascent)
23541 cmp->ascent = font_ascent;
23542 if (cmp->descent < font_descent)
23543 cmp->descent = font_descent;
23544 }
23545
23546 if (it->glyph_row
23547 && (cmp->lbearing < 0
23548 || cmp->rbearing > cmp->pixel_width))
23549 it->glyph_row->contains_overlapping_glyphs_p = 1;
23550
23551 it->pixel_width = cmp->pixel_width;
23552 it->ascent = it->phys_ascent = cmp->ascent;
23553 it->descent = it->phys_descent = cmp->descent;
23554 if (face->box != FACE_NO_BOX)
23555 {
23556 int thick = face->box_line_width;
23557
23558 if (thick > 0)
23559 {
23560 it->ascent += thick;
23561 it->descent += thick;
23562 }
23563 else
23564 thick = - thick;
23565
23566 if (it->start_of_box_run_p)
23567 it->pixel_width += thick;
23568 if (it->end_of_box_run_p)
23569 it->pixel_width += thick;
23570 }
23571
23572 /* If face has an overline, add the height of the overline
23573 (1 pixel) and a 1 pixel margin to the character height. */
23574 if (face->overline_p)
23575 it->ascent += overline_margin;
23576
23577 take_vertical_position_into_account (it);
23578 if (it->ascent < 0)
23579 it->ascent = 0;
23580 if (it->descent < 0)
23581 it->descent = 0;
23582
23583 if (it->glyph_row)
23584 append_composite_glyph (it);
23585 }
23586 else if (it->what == IT_COMPOSITION)
23587 {
23588 /* A dynamic (automatic) composition. */
23589 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23590 Lisp_Object gstring;
23591 struct font_metrics metrics;
23592
23593 gstring = composition_gstring_from_id (it->cmp_it.id);
23594 it->pixel_width
23595 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23596 &metrics);
23597 if (it->glyph_row
23598 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23599 it->glyph_row->contains_overlapping_glyphs_p = 1;
23600 it->ascent = it->phys_ascent = metrics.ascent;
23601 it->descent = it->phys_descent = metrics.descent;
23602 if (face->box != FACE_NO_BOX)
23603 {
23604 int thick = face->box_line_width;
23605
23606 if (thick > 0)
23607 {
23608 it->ascent += thick;
23609 it->descent += thick;
23610 }
23611 else
23612 thick = - thick;
23613
23614 if (it->start_of_box_run_p)
23615 it->pixel_width += thick;
23616 if (it->end_of_box_run_p)
23617 it->pixel_width += thick;
23618 }
23619 /* If face has an overline, add the height of the overline
23620 (1 pixel) and a 1 pixel margin to the character height. */
23621 if (face->overline_p)
23622 it->ascent += overline_margin;
23623 take_vertical_position_into_account (it);
23624 if (it->ascent < 0)
23625 it->ascent = 0;
23626 if (it->descent < 0)
23627 it->descent = 0;
23628
23629 if (it->glyph_row)
23630 append_composite_glyph (it);
23631 }
23632 else if (it->what == IT_GLYPHLESS)
23633 produce_glyphless_glyph (it, 0, Qnil);
23634 else if (it->what == IT_IMAGE)
23635 produce_image_glyph (it);
23636 else if (it->what == IT_STRETCH)
23637 produce_stretch_glyph (it);
23638
23639 done:
23640 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23641 because this isn't true for images with `:ascent 100'. */
23642 xassert (it->ascent >= 0 && it->descent >= 0);
23643 if (it->area == TEXT_AREA)
23644 it->current_x += it->pixel_width;
23645
23646 if (extra_line_spacing > 0)
23647 {
23648 it->descent += extra_line_spacing;
23649 if (extra_line_spacing > it->max_extra_line_spacing)
23650 it->max_extra_line_spacing = extra_line_spacing;
23651 }
23652
23653 it->max_ascent = max (it->max_ascent, it->ascent);
23654 it->max_descent = max (it->max_descent, it->descent);
23655 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23656 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23657 }
23658
23659 /* EXPORT for RIF:
23660 Output LEN glyphs starting at START at the nominal cursor position.
23661 Advance the nominal cursor over the text. The global variable
23662 updated_window contains the window being updated, updated_row is
23663 the glyph row being updated, and updated_area is the area of that
23664 row being updated. */
23665
23666 void
23667 x_write_glyphs (struct glyph *start, int len)
23668 {
23669 int x, hpos;
23670
23671 xassert (updated_window && updated_row);
23672 BLOCK_INPUT;
23673
23674 /* Write glyphs. */
23675
23676 hpos = start - updated_row->glyphs[updated_area];
23677 x = draw_glyphs (updated_window, output_cursor.x,
23678 updated_row, updated_area,
23679 hpos, hpos + len,
23680 DRAW_NORMAL_TEXT, 0);
23681
23682 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23683 if (updated_area == TEXT_AREA
23684 && updated_window->phys_cursor_on_p
23685 && updated_window->phys_cursor.vpos == output_cursor.vpos
23686 && updated_window->phys_cursor.hpos >= hpos
23687 && updated_window->phys_cursor.hpos < hpos + len)
23688 updated_window->phys_cursor_on_p = 0;
23689
23690 UNBLOCK_INPUT;
23691
23692 /* Advance the output cursor. */
23693 output_cursor.hpos += len;
23694 output_cursor.x = x;
23695 }
23696
23697
23698 /* EXPORT for RIF:
23699 Insert LEN glyphs from START at the nominal cursor position. */
23700
23701 void
23702 x_insert_glyphs (struct glyph *start, int len)
23703 {
23704 struct frame *f;
23705 struct window *w;
23706 int line_height, shift_by_width, shifted_region_width;
23707 struct glyph_row *row;
23708 struct glyph *glyph;
23709 int frame_x, frame_y;
23710 EMACS_INT hpos;
23711
23712 xassert (updated_window && updated_row);
23713 BLOCK_INPUT;
23714 w = updated_window;
23715 f = XFRAME (WINDOW_FRAME (w));
23716
23717 /* Get the height of the line we are in. */
23718 row = updated_row;
23719 line_height = row->height;
23720
23721 /* Get the width of the glyphs to insert. */
23722 shift_by_width = 0;
23723 for (glyph = start; glyph < start + len; ++glyph)
23724 shift_by_width += glyph->pixel_width;
23725
23726 /* Get the width of the region to shift right. */
23727 shifted_region_width = (window_box_width (w, updated_area)
23728 - output_cursor.x
23729 - shift_by_width);
23730
23731 /* Shift right. */
23732 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23733 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23734
23735 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23736 line_height, shift_by_width);
23737
23738 /* Write the glyphs. */
23739 hpos = start - row->glyphs[updated_area];
23740 draw_glyphs (w, output_cursor.x, row, updated_area,
23741 hpos, hpos + len,
23742 DRAW_NORMAL_TEXT, 0);
23743
23744 /* Advance the output cursor. */
23745 output_cursor.hpos += len;
23746 output_cursor.x += shift_by_width;
23747 UNBLOCK_INPUT;
23748 }
23749
23750
23751 /* EXPORT for RIF:
23752 Erase the current text line from the nominal cursor position
23753 (inclusive) to pixel column TO_X (exclusive). The idea is that
23754 everything from TO_X onward is already erased.
23755
23756 TO_X is a pixel position relative to updated_area of
23757 updated_window. TO_X == -1 means clear to the end of this area. */
23758
23759 void
23760 x_clear_end_of_line (int to_x)
23761 {
23762 struct frame *f;
23763 struct window *w = updated_window;
23764 int max_x, min_y, max_y;
23765 int from_x, from_y, to_y;
23766
23767 xassert (updated_window && updated_row);
23768 f = XFRAME (w->frame);
23769
23770 if (updated_row->full_width_p)
23771 max_x = WINDOW_TOTAL_WIDTH (w);
23772 else
23773 max_x = window_box_width (w, updated_area);
23774 max_y = window_text_bottom_y (w);
23775
23776 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23777 of window. For TO_X > 0, truncate to end of drawing area. */
23778 if (to_x == 0)
23779 return;
23780 else if (to_x < 0)
23781 to_x = max_x;
23782 else
23783 to_x = min (to_x, max_x);
23784
23785 to_y = min (max_y, output_cursor.y + updated_row->height);
23786
23787 /* Notice if the cursor will be cleared by this operation. */
23788 if (!updated_row->full_width_p)
23789 notice_overwritten_cursor (w, updated_area,
23790 output_cursor.x, -1,
23791 updated_row->y,
23792 MATRIX_ROW_BOTTOM_Y (updated_row));
23793
23794 from_x = output_cursor.x;
23795
23796 /* Translate to frame coordinates. */
23797 if (updated_row->full_width_p)
23798 {
23799 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23800 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23801 }
23802 else
23803 {
23804 int area_left = window_box_left (w, updated_area);
23805 from_x += area_left;
23806 to_x += area_left;
23807 }
23808
23809 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23810 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23811 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23812
23813 /* Prevent inadvertently clearing to end of the X window. */
23814 if (to_x > from_x && to_y > from_y)
23815 {
23816 BLOCK_INPUT;
23817 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23818 to_x - from_x, to_y - from_y);
23819 UNBLOCK_INPUT;
23820 }
23821 }
23822
23823 #endif /* HAVE_WINDOW_SYSTEM */
23824
23825
23826 \f
23827 /***********************************************************************
23828 Cursor types
23829 ***********************************************************************/
23830
23831 /* Value is the internal representation of the specified cursor type
23832 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23833 of the bar cursor. */
23834
23835 static enum text_cursor_kinds
23836 get_specified_cursor_type (Lisp_Object arg, int *width)
23837 {
23838 enum text_cursor_kinds type;
23839
23840 if (NILP (arg))
23841 return NO_CURSOR;
23842
23843 if (EQ (arg, Qbox))
23844 return FILLED_BOX_CURSOR;
23845
23846 if (EQ (arg, Qhollow))
23847 return HOLLOW_BOX_CURSOR;
23848
23849 if (EQ (arg, Qbar))
23850 {
23851 *width = 2;
23852 return BAR_CURSOR;
23853 }
23854
23855 if (CONSP (arg)
23856 && EQ (XCAR (arg), Qbar)
23857 && INTEGERP (XCDR (arg))
23858 && XINT (XCDR (arg)) >= 0)
23859 {
23860 *width = XINT (XCDR (arg));
23861 return BAR_CURSOR;
23862 }
23863
23864 if (EQ (arg, Qhbar))
23865 {
23866 *width = 2;
23867 return HBAR_CURSOR;
23868 }
23869
23870 if (CONSP (arg)
23871 && EQ (XCAR (arg), Qhbar)
23872 && INTEGERP (XCDR (arg))
23873 && XINT (XCDR (arg)) >= 0)
23874 {
23875 *width = XINT (XCDR (arg));
23876 return HBAR_CURSOR;
23877 }
23878
23879 /* Treat anything unknown as "hollow box cursor".
23880 It was bad to signal an error; people have trouble fixing
23881 .Xdefaults with Emacs, when it has something bad in it. */
23882 type = HOLLOW_BOX_CURSOR;
23883
23884 return type;
23885 }
23886
23887 /* Set the default cursor types for specified frame. */
23888 void
23889 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23890 {
23891 int width = 1;
23892 Lisp_Object tem;
23893
23894 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23895 FRAME_CURSOR_WIDTH (f) = width;
23896
23897 /* By default, set up the blink-off state depending on the on-state. */
23898
23899 tem = Fassoc (arg, Vblink_cursor_alist);
23900 if (!NILP (tem))
23901 {
23902 FRAME_BLINK_OFF_CURSOR (f)
23903 = get_specified_cursor_type (XCDR (tem), &width);
23904 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23905 }
23906 else
23907 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23908 }
23909
23910
23911 #ifdef HAVE_WINDOW_SYSTEM
23912
23913 /* Return the cursor we want to be displayed in window W. Return
23914 width of bar/hbar cursor through WIDTH arg. Return with
23915 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23916 (i.e. if the `system caret' should track this cursor).
23917
23918 In a mini-buffer window, we want the cursor only to appear if we
23919 are reading input from this window. For the selected window, we
23920 want the cursor type given by the frame parameter or buffer local
23921 setting of cursor-type. If explicitly marked off, draw no cursor.
23922 In all other cases, we want a hollow box cursor. */
23923
23924 static enum text_cursor_kinds
23925 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23926 int *active_cursor)
23927 {
23928 struct frame *f = XFRAME (w->frame);
23929 struct buffer *b = XBUFFER (w->buffer);
23930 int cursor_type = DEFAULT_CURSOR;
23931 Lisp_Object alt_cursor;
23932 int non_selected = 0;
23933
23934 *active_cursor = 1;
23935
23936 /* Echo area */
23937 if (cursor_in_echo_area
23938 && FRAME_HAS_MINIBUF_P (f)
23939 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23940 {
23941 if (w == XWINDOW (echo_area_window))
23942 {
23943 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
23944 {
23945 *width = FRAME_CURSOR_WIDTH (f);
23946 return FRAME_DESIRED_CURSOR (f);
23947 }
23948 else
23949 return get_specified_cursor_type (BVAR (b, cursor_type), width);
23950 }
23951
23952 *active_cursor = 0;
23953 non_selected = 1;
23954 }
23955
23956 /* Detect a nonselected window or nonselected frame. */
23957 else if (w != XWINDOW (f->selected_window)
23958 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23959 {
23960 *active_cursor = 0;
23961
23962 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23963 return NO_CURSOR;
23964
23965 non_selected = 1;
23966 }
23967
23968 /* Never display a cursor in a window in which cursor-type is nil. */
23969 if (NILP (BVAR (b, cursor_type)))
23970 return NO_CURSOR;
23971
23972 /* Get the normal cursor type for this window. */
23973 if (EQ (BVAR (b, cursor_type), Qt))
23974 {
23975 cursor_type = FRAME_DESIRED_CURSOR (f);
23976 *width = FRAME_CURSOR_WIDTH (f);
23977 }
23978 else
23979 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
23980
23981 /* Use cursor-in-non-selected-windows instead
23982 for non-selected window or frame. */
23983 if (non_selected)
23984 {
23985 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
23986 if (!EQ (Qt, alt_cursor))
23987 return get_specified_cursor_type (alt_cursor, width);
23988 /* t means modify the normal cursor type. */
23989 if (cursor_type == FILLED_BOX_CURSOR)
23990 cursor_type = HOLLOW_BOX_CURSOR;
23991 else if (cursor_type == BAR_CURSOR && *width > 1)
23992 --*width;
23993 return cursor_type;
23994 }
23995
23996 /* Use normal cursor if not blinked off. */
23997 if (!w->cursor_off_p)
23998 {
23999 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24000 {
24001 if (cursor_type == FILLED_BOX_CURSOR)
24002 {
24003 /* Using a block cursor on large images can be very annoying.
24004 So use a hollow cursor for "large" images.
24005 If image is not transparent (no mask), also use hollow cursor. */
24006 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24007 if (img != NULL && IMAGEP (img->spec))
24008 {
24009 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24010 where N = size of default frame font size.
24011 This should cover most of the "tiny" icons people may use. */
24012 if (!img->mask
24013 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24014 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24015 cursor_type = HOLLOW_BOX_CURSOR;
24016 }
24017 }
24018 else if (cursor_type != NO_CURSOR)
24019 {
24020 /* Display current only supports BOX and HOLLOW cursors for images.
24021 So for now, unconditionally use a HOLLOW cursor when cursor is
24022 not a solid box cursor. */
24023 cursor_type = HOLLOW_BOX_CURSOR;
24024 }
24025 }
24026 return cursor_type;
24027 }
24028
24029 /* Cursor is blinked off, so determine how to "toggle" it. */
24030
24031 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24032 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24033 return get_specified_cursor_type (XCDR (alt_cursor), width);
24034
24035 /* Then see if frame has specified a specific blink off cursor type. */
24036 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24037 {
24038 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24039 return FRAME_BLINK_OFF_CURSOR (f);
24040 }
24041
24042 #if 0
24043 /* Some people liked having a permanently visible blinking cursor,
24044 while others had very strong opinions against it. So it was
24045 decided to remove it. KFS 2003-09-03 */
24046
24047 /* Finally perform built-in cursor blinking:
24048 filled box <-> hollow box
24049 wide [h]bar <-> narrow [h]bar
24050 narrow [h]bar <-> no cursor
24051 other type <-> no cursor */
24052
24053 if (cursor_type == FILLED_BOX_CURSOR)
24054 return HOLLOW_BOX_CURSOR;
24055
24056 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24057 {
24058 *width = 1;
24059 return cursor_type;
24060 }
24061 #endif
24062
24063 return NO_CURSOR;
24064 }
24065
24066
24067 /* Notice when the text cursor of window W has been completely
24068 overwritten by a drawing operation that outputs glyphs in AREA
24069 starting at X0 and ending at X1 in the line starting at Y0 and
24070 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24071 the rest of the line after X0 has been written. Y coordinates
24072 are window-relative. */
24073
24074 static void
24075 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24076 int x0, int x1, int y0, int y1)
24077 {
24078 int cx0, cx1, cy0, cy1;
24079 struct glyph_row *row;
24080
24081 if (!w->phys_cursor_on_p)
24082 return;
24083 if (area != TEXT_AREA)
24084 return;
24085
24086 if (w->phys_cursor.vpos < 0
24087 || w->phys_cursor.vpos >= w->current_matrix->nrows
24088 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24089 !(row->enabled_p && row->displays_text_p)))
24090 return;
24091
24092 if (row->cursor_in_fringe_p)
24093 {
24094 row->cursor_in_fringe_p = 0;
24095 draw_fringe_bitmap (w, row, row->reversed_p);
24096 w->phys_cursor_on_p = 0;
24097 return;
24098 }
24099
24100 cx0 = w->phys_cursor.x;
24101 cx1 = cx0 + w->phys_cursor_width;
24102 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24103 return;
24104
24105 /* The cursor image will be completely removed from the
24106 screen if the output area intersects the cursor area in
24107 y-direction. When we draw in [y0 y1[, and some part of
24108 the cursor is at y < y0, that part must have been drawn
24109 before. When scrolling, the cursor is erased before
24110 actually scrolling, so we don't come here. When not
24111 scrolling, the rows above the old cursor row must have
24112 changed, and in this case these rows must have written
24113 over the cursor image.
24114
24115 Likewise if part of the cursor is below y1, with the
24116 exception of the cursor being in the first blank row at
24117 the buffer and window end because update_text_area
24118 doesn't draw that row. (Except when it does, but
24119 that's handled in update_text_area.) */
24120
24121 cy0 = w->phys_cursor.y;
24122 cy1 = cy0 + w->phys_cursor_height;
24123 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24124 return;
24125
24126 w->phys_cursor_on_p = 0;
24127 }
24128
24129 #endif /* HAVE_WINDOW_SYSTEM */
24130
24131 \f
24132 /************************************************************************
24133 Mouse Face
24134 ************************************************************************/
24135
24136 #ifdef HAVE_WINDOW_SYSTEM
24137
24138 /* EXPORT for RIF:
24139 Fix the display of area AREA of overlapping row ROW in window W
24140 with respect to the overlapping part OVERLAPS. */
24141
24142 void
24143 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24144 enum glyph_row_area area, int overlaps)
24145 {
24146 int i, x;
24147
24148 BLOCK_INPUT;
24149
24150 x = 0;
24151 for (i = 0; i < row->used[area];)
24152 {
24153 if (row->glyphs[area][i].overlaps_vertically_p)
24154 {
24155 int start = i, start_x = x;
24156
24157 do
24158 {
24159 x += row->glyphs[area][i].pixel_width;
24160 ++i;
24161 }
24162 while (i < row->used[area]
24163 && row->glyphs[area][i].overlaps_vertically_p);
24164
24165 draw_glyphs (w, start_x, row, area,
24166 start, i,
24167 DRAW_NORMAL_TEXT, overlaps);
24168 }
24169 else
24170 {
24171 x += row->glyphs[area][i].pixel_width;
24172 ++i;
24173 }
24174 }
24175
24176 UNBLOCK_INPUT;
24177 }
24178
24179
24180 /* EXPORT:
24181 Draw the cursor glyph of window W in glyph row ROW. See the
24182 comment of draw_glyphs for the meaning of HL. */
24183
24184 void
24185 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24186 enum draw_glyphs_face hl)
24187 {
24188 /* If cursor hpos is out of bounds, don't draw garbage. This can
24189 happen in mini-buffer windows when switching between echo area
24190 glyphs and mini-buffer. */
24191 if ((row->reversed_p
24192 ? (w->phys_cursor.hpos >= 0)
24193 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24194 {
24195 int on_p = w->phys_cursor_on_p;
24196 int x1;
24197 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24198 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24199 hl, 0);
24200 w->phys_cursor_on_p = on_p;
24201
24202 if (hl == DRAW_CURSOR)
24203 w->phys_cursor_width = x1 - w->phys_cursor.x;
24204 /* When we erase the cursor, and ROW is overlapped by other
24205 rows, make sure that these overlapping parts of other rows
24206 are redrawn. */
24207 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24208 {
24209 w->phys_cursor_width = x1 - w->phys_cursor.x;
24210
24211 if (row > w->current_matrix->rows
24212 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24213 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24214 OVERLAPS_ERASED_CURSOR);
24215
24216 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24217 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24218 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24219 OVERLAPS_ERASED_CURSOR);
24220 }
24221 }
24222 }
24223
24224
24225 /* EXPORT:
24226 Erase the image of a cursor of window W from the screen. */
24227
24228 void
24229 erase_phys_cursor (struct window *w)
24230 {
24231 struct frame *f = XFRAME (w->frame);
24232 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24233 int hpos = w->phys_cursor.hpos;
24234 int vpos = w->phys_cursor.vpos;
24235 int mouse_face_here_p = 0;
24236 struct glyph_matrix *active_glyphs = w->current_matrix;
24237 struct glyph_row *cursor_row;
24238 struct glyph *cursor_glyph;
24239 enum draw_glyphs_face hl;
24240
24241 /* No cursor displayed or row invalidated => nothing to do on the
24242 screen. */
24243 if (w->phys_cursor_type == NO_CURSOR)
24244 goto mark_cursor_off;
24245
24246 /* VPOS >= active_glyphs->nrows means that window has been resized.
24247 Don't bother to erase the cursor. */
24248 if (vpos >= active_glyphs->nrows)
24249 goto mark_cursor_off;
24250
24251 /* If row containing cursor is marked invalid, there is nothing we
24252 can do. */
24253 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24254 if (!cursor_row->enabled_p)
24255 goto mark_cursor_off;
24256
24257 /* If line spacing is > 0, old cursor may only be partially visible in
24258 window after split-window. So adjust visible height. */
24259 cursor_row->visible_height = min (cursor_row->visible_height,
24260 window_text_bottom_y (w) - cursor_row->y);
24261
24262 /* If row is completely invisible, don't attempt to delete a cursor which
24263 isn't there. This can happen if cursor is at top of a window, and
24264 we switch to a buffer with a header line in that window. */
24265 if (cursor_row->visible_height <= 0)
24266 goto mark_cursor_off;
24267
24268 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24269 if (cursor_row->cursor_in_fringe_p)
24270 {
24271 cursor_row->cursor_in_fringe_p = 0;
24272 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24273 goto mark_cursor_off;
24274 }
24275
24276 /* This can happen when the new row is shorter than the old one.
24277 In this case, either draw_glyphs or clear_end_of_line
24278 should have cleared the cursor. Note that we wouldn't be
24279 able to erase the cursor in this case because we don't have a
24280 cursor glyph at hand. */
24281 if ((cursor_row->reversed_p
24282 ? (w->phys_cursor.hpos < 0)
24283 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24284 goto mark_cursor_off;
24285
24286 /* If the cursor is in the mouse face area, redisplay that when
24287 we clear the cursor. */
24288 if (! NILP (hlinfo->mouse_face_window)
24289 && coords_in_mouse_face_p (w, hpos, vpos)
24290 /* Don't redraw the cursor's spot in mouse face if it is at the
24291 end of a line (on a newline). The cursor appears there, but
24292 mouse highlighting does not. */
24293 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24294 mouse_face_here_p = 1;
24295
24296 /* Maybe clear the display under the cursor. */
24297 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24298 {
24299 int x, y, left_x;
24300 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24301 int width;
24302
24303 cursor_glyph = get_phys_cursor_glyph (w);
24304 if (cursor_glyph == NULL)
24305 goto mark_cursor_off;
24306
24307 width = cursor_glyph->pixel_width;
24308 left_x = window_box_left_offset (w, TEXT_AREA);
24309 x = w->phys_cursor.x;
24310 if (x < left_x)
24311 width -= left_x - x;
24312 width = min (width, window_box_width (w, TEXT_AREA) - x);
24313 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24314 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24315
24316 if (width > 0)
24317 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24318 }
24319
24320 /* Erase the cursor by redrawing the character underneath it. */
24321 if (mouse_face_here_p)
24322 hl = DRAW_MOUSE_FACE;
24323 else
24324 hl = DRAW_NORMAL_TEXT;
24325 draw_phys_cursor_glyph (w, cursor_row, hl);
24326
24327 mark_cursor_off:
24328 w->phys_cursor_on_p = 0;
24329 w->phys_cursor_type = NO_CURSOR;
24330 }
24331
24332
24333 /* EXPORT:
24334 Display or clear cursor of window W. If ON is zero, clear the
24335 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24336 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24337
24338 void
24339 display_and_set_cursor (struct window *w, int on,
24340 int hpos, int vpos, int x, int y)
24341 {
24342 struct frame *f = XFRAME (w->frame);
24343 int new_cursor_type;
24344 int new_cursor_width;
24345 int active_cursor;
24346 struct glyph_row *glyph_row;
24347 struct glyph *glyph;
24348
24349 /* This is pointless on invisible frames, and dangerous on garbaged
24350 windows and frames; in the latter case, the frame or window may
24351 be in the midst of changing its size, and x and y may be off the
24352 window. */
24353 if (! FRAME_VISIBLE_P (f)
24354 || FRAME_GARBAGED_P (f)
24355 || vpos >= w->current_matrix->nrows
24356 || hpos >= w->current_matrix->matrix_w)
24357 return;
24358
24359 /* If cursor is off and we want it off, return quickly. */
24360 if (!on && !w->phys_cursor_on_p)
24361 return;
24362
24363 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24364 /* If cursor row is not enabled, we don't really know where to
24365 display the cursor. */
24366 if (!glyph_row->enabled_p)
24367 {
24368 w->phys_cursor_on_p = 0;
24369 return;
24370 }
24371
24372 glyph = NULL;
24373 if (!glyph_row->exact_window_width_line_p
24374 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24375 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24376
24377 xassert (interrupt_input_blocked);
24378
24379 /* Set new_cursor_type to the cursor we want to be displayed. */
24380 new_cursor_type = get_window_cursor_type (w, glyph,
24381 &new_cursor_width, &active_cursor);
24382
24383 /* If cursor is currently being shown and we don't want it to be or
24384 it is in the wrong place, or the cursor type is not what we want,
24385 erase it. */
24386 if (w->phys_cursor_on_p
24387 && (!on
24388 || w->phys_cursor.x != x
24389 || w->phys_cursor.y != y
24390 || new_cursor_type != w->phys_cursor_type
24391 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24392 && new_cursor_width != w->phys_cursor_width)))
24393 erase_phys_cursor (w);
24394
24395 /* Don't check phys_cursor_on_p here because that flag is only set
24396 to zero in some cases where we know that the cursor has been
24397 completely erased, to avoid the extra work of erasing the cursor
24398 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24399 still not be visible, or it has only been partly erased. */
24400 if (on)
24401 {
24402 w->phys_cursor_ascent = glyph_row->ascent;
24403 w->phys_cursor_height = glyph_row->height;
24404
24405 /* Set phys_cursor_.* before x_draw_.* is called because some
24406 of them may need the information. */
24407 w->phys_cursor.x = x;
24408 w->phys_cursor.y = glyph_row->y;
24409 w->phys_cursor.hpos = hpos;
24410 w->phys_cursor.vpos = vpos;
24411 }
24412
24413 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24414 new_cursor_type, new_cursor_width,
24415 on, active_cursor);
24416 }
24417
24418
24419 /* Switch the display of W's cursor on or off, according to the value
24420 of ON. */
24421
24422 static void
24423 update_window_cursor (struct window *w, int on)
24424 {
24425 /* Don't update cursor in windows whose frame is in the process
24426 of being deleted. */
24427 if (w->current_matrix)
24428 {
24429 BLOCK_INPUT;
24430 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24431 w->phys_cursor.x, w->phys_cursor.y);
24432 UNBLOCK_INPUT;
24433 }
24434 }
24435
24436
24437 /* Call update_window_cursor with parameter ON_P on all leaf windows
24438 in the window tree rooted at W. */
24439
24440 static void
24441 update_cursor_in_window_tree (struct window *w, int on_p)
24442 {
24443 while (w)
24444 {
24445 if (!NILP (w->hchild))
24446 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24447 else if (!NILP (w->vchild))
24448 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24449 else
24450 update_window_cursor (w, on_p);
24451
24452 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24453 }
24454 }
24455
24456
24457 /* EXPORT:
24458 Display the cursor on window W, or clear it, according to ON_P.
24459 Don't change the cursor's position. */
24460
24461 void
24462 x_update_cursor (struct frame *f, int on_p)
24463 {
24464 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24465 }
24466
24467
24468 /* EXPORT:
24469 Clear the cursor of window W to background color, and mark the
24470 cursor as not shown. This is used when the text where the cursor
24471 is about to be rewritten. */
24472
24473 void
24474 x_clear_cursor (struct window *w)
24475 {
24476 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24477 update_window_cursor (w, 0);
24478 }
24479
24480 #endif /* HAVE_WINDOW_SYSTEM */
24481
24482 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24483 and MSDOS. */
24484 static void
24485 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24486 int start_hpos, int end_hpos,
24487 enum draw_glyphs_face draw)
24488 {
24489 #ifdef HAVE_WINDOW_SYSTEM
24490 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24491 {
24492 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24493 return;
24494 }
24495 #endif
24496 #if defined (HAVE_GPM) || defined (MSDOS)
24497 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24498 #endif
24499 }
24500
24501 /* Display the active region described by mouse_face_* according to DRAW. */
24502
24503 static void
24504 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24505 {
24506 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24507 struct frame *f = XFRAME (WINDOW_FRAME (w));
24508
24509 if (/* If window is in the process of being destroyed, don't bother
24510 to do anything. */
24511 w->current_matrix != NULL
24512 /* Don't update mouse highlight if hidden */
24513 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24514 /* Recognize when we are called to operate on rows that don't exist
24515 anymore. This can happen when a window is split. */
24516 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24517 {
24518 int phys_cursor_on_p = w->phys_cursor_on_p;
24519 struct glyph_row *row, *first, *last;
24520
24521 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24522 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24523
24524 for (row = first; row <= last && row->enabled_p; ++row)
24525 {
24526 int start_hpos, end_hpos, start_x;
24527
24528 /* For all but the first row, the highlight starts at column 0. */
24529 if (row == first)
24530 {
24531 /* R2L rows have BEG and END in reversed order, but the
24532 screen drawing geometry is always left to right. So
24533 we need to mirror the beginning and end of the
24534 highlighted area in R2L rows. */
24535 if (!row->reversed_p)
24536 {
24537 start_hpos = hlinfo->mouse_face_beg_col;
24538 start_x = hlinfo->mouse_face_beg_x;
24539 }
24540 else if (row == last)
24541 {
24542 start_hpos = hlinfo->mouse_face_end_col;
24543 start_x = hlinfo->mouse_face_end_x;
24544 }
24545 else
24546 {
24547 start_hpos = 0;
24548 start_x = 0;
24549 }
24550 }
24551 else if (row->reversed_p && row == last)
24552 {
24553 start_hpos = hlinfo->mouse_face_end_col;
24554 start_x = hlinfo->mouse_face_end_x;
24555 }
24556 else
24557 {
24558 start_hpos = 0;
24559 start_x = 0;
24560 }
24561
24562 if (row == last)
24563 {
24564 if (!row->reversed_p)
24565 end_hpos = hlinfo->mouse_face_end_col;
24566 else if (row == first)
24567 end_hpos = hlinfo->mouse_face_beg_col;
24568 else
24569 {
24570 end_hpos = row->used[TEXT_AREA];
24571 if (draw == DRAW_NORMAL_TEXT)
24572 row->fill_line_p = 1; /* Clear to end of line */
24573 }
24574 }
24575 else if (row->reversed_p && row == first)
24576 end_hpos = hlinfo->mouse_face_beg_col;
24577 else
24578 {
24579 end_hpos = row->used[TEXT_AREA];
24580 if (draw == DRAW_NORMAL_TEXT)
24581 row->fill_line_p = 1; /* Clear to end of line */
24582 }
24583
24584 if (end_hpos > start_hpos)
24585 {
24586 draw_row_with_mouse_face (w, start_x, row,
24587 start_hpos, end_hpos, draw);
24588
24589 row->mouse_face_p
24590 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24591 }
24592 }
24593
24594 #ifdef HAVE_WINDOW_SYSTEM
24595 /* When we've written over the cursor, arrange for it to
24596 be displayed again. */
24597 if (FRAME_WINDOW_P (f)
24598 && phys_cursor_on_p && !w->phys_cursor_on_p)
24599 {
24600 BLOCK_INPUT;
24601 display_and_set_cursor (w, 1,
24602 w->phys_cursor.hpos, w->phys_cursor.vpos,
24603 w->phys_cursor.x, w->phys_cursor.y);
24604 UNBLOCK_INPUT;
24605 }
24606 #endif /* HAVE_WINDOW_SYSTEM */
24607 }
24608
24609 #ifdef HAVE_WINDOW_SYSTEM
24610 /* Change the mouse cursor. */
24611 if (FRAME_WINDOW_P (f))
24612 {
24613 if (draw == DRAW_NORMAL_TEXT
24614 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24615 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24616 else if (draw == DRAW_MOUSE_FACE)
24617 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24618 else
24619 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24620 }
24621 #endif /* HAVE_WINDOW_SYSTEM */
24622 }
24623
24624 /* EXPORT:
24625 Clear out the mouse-highlighted active region.
24626 Redraw it un-highlighted first. Value is non-zero if mouse
24627 face was actually drawn unhighlighted. */
24628
24629 int
24630 clear_mouse_face (Mouse_HLInfo *hlinfo)
24631 {
24632 int cleared = 0;
24633
24634 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24635 {
24636 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24637 cleared = 1;
24638 }
24639
24640 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24641 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24642 hlinfo->mouse_face_window = Qnil;
24643 hlinfo->mouse_face_overlay = Qnil;
24644 return cleared;
24645 }
24646
24647 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24648 within the mouse face on that window. */
24649 static int
24650 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24651 {
24652 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24653
24654 /* Quickly resolve the easy cases. */
24655 if (!(WINDOWP (hlinfo->mouse_face_window)
24656 && XWINDOW (hlinfo->mouse_face_window) == w))
24657 return 0;
24658 if (vpos < hlinfo->mouse_face_beg_row
24659 || vpos > hlinfo->mouse_face_end_row)
24660 return 0;
24661 if (vpos > hlinfo->mouse_face_beg_row
24662 && vpos < hlinfo->mouse_face_end_row)
24663 return 1;
24664
24665 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24666 {
24667 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24668 {
24669 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24670 return 1;
24671 }
24672 else if ((vpos == hlinfo->mouse_face_beg_row
24673 && hpos >= hlinfo->mouse_face_beg_col)
24674 || (vpos == hlinfo->mouse_face_end_row
24675 && hpos < hlinfo->mouse_face_end_col))
24676 return 1;
24677 }
24678 else
24679 {
24680 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24681 {
24682 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24683 return 1;
24684 }
24685 else if ((vpos == hlinfo->mouse_face_beg_row
24686 && hpos <= hlinfo->mouse_face_beg_col)
24687 || (vpos == hlinfo->mouse_face_end_row
24688 && hpos > hlinfo->mouse_face_end_col))
24689 return 1;
24690 }
24691 return 0;
24692 }
24693
24694
24695 /* EXPORT:
24696 Non-zero if physical cursor of window W is within mouse face. */
24697
24698 int
24699 cursor_in_mouse_face_p (struct window *w)
24700 {
24701 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24702 }
24703
24704
24705 \f
24706 /* Find the glyph rows START_ROW and END_ROW of window W that display
24707 characters between buffer positions START_CHARPOS and END_CHARPOS
24708 (excluding END_CHARPOS). This is similar to row_containing_pos,
24709 but is more accurate when bidi reordering makes buffer positions
24710 change non-linearly with glyph rows. */
24711 static void
24712 rows_from_pos_range (struct window *w,
24713 EMACS_INT start_charpos, EMACS_INT end_charpos,
24714 struct glyph_row **start, struct glyph_row **end)
24715 {
24716 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24717 int last_y = window_text_bottom_y (w);
24718 struct glyph_row *row;
24719
24720 *start = NULL;
24721 *end = NULL;
24722
24723 while (!first->enabled_p
24724 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24725 first++;
24726
24727 /* Find the START row. */
24728 for (row = first;
24729 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24730 row++)
24731 {
24732 /* A row can potentially be the START row if the range of the
24733 characters it displays intersects the range
24734 [START_CHARPOS..END_CHARPOS). */
24735 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24736 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24737 /* See the commentary in row_containing_pos, for the
24738 explanation of the complicated way to check whether
24739 some position is beyond the end of the characters
24740 displayed by a row. */
24741 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24742 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24743 && !row->ends_at_zv_p
24744 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24745 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24746 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24747 && !row->ends_at_zv_p
24748 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24749 {
24750 /* Found a candidate row. Now make sure at least one of the
24751 glyphs it displays has a charpos from the range
24752 [START_CHARPOS..END_CHARPOS).
24753
24754 This is not obvious because bidi reordering could make
24755 buffer positions of a row be 1,2,3,102,101,100, and if we
24756 want to highlight characters in [50..60), we don't want
24757 this row, even though [50..60) does intersect [1..103),
24758 the range of character positions given by the row's start
24759 and end positions. */
24760 struct glyph *g = row->glyphs[TEXT_AREA];
24761 struct glyph *e = g + row->used[TEXT_AREA];
24762
24763 while (g < e)
24764 {
24765 if (BUFFERP (g->object)
24766 && start_charpos <= g->charpos && g->charpos < end_charpos)
24767 *start = row;
24768 g++;
24769 }
24770 if (*start)
24771 break;
24772 }
24773 }
24774
24775 /* Find the END row. */
24776 if (!*start
24777 /* If the last row is partially visible, start looking for END
24778 from that row, instead of starting from FIRST. */
24779 && !(row->enabled_p
24780 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24781 row = first;
24782 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24783 {
24784 struct glyph_row *next = row + 1;
24785
24786 if (!next->enabled_p
24787 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24788 /* The first row >= START whose range of displayed characters
24789 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24790 is the row END + 1. */
24791 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24792 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24793 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24794 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24795 && !next->ends_at_zv_p
24796 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24797 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24798 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24799 && !next->ends_at_zv_p
24800 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24801 {
24802 *end = row;
24803 break;
24804 }
24805 else
24806 {
24807 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24808 but none of the characters it displays are in the range, it is
24809 also END + 1. */
24810 struct glyph *g = next->glyphs[TEXT_AREA];
24811 struct glyph *e = g + next->used[TEXT_AREA];
24812
24813 while (g < e)
24814 {
24815 if (BUFFERP (g->object)
24816 && start_charpos <= g->charpos && g->charpos < end_charpos)
24817 break;
24818 g++;
24819 }
24820 if (g == e)
24821 {
24822 *end = row;
24823 break;
24824 }
24825 }
24826 }
24827 }
24828
24829 /* This function sets the mouse_face_* elements of HLINFO, assuming
24830 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24831 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24832 for the overlay or run of text properties specifying the mouse
24833 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24834 before-string and after-string that must also be highlighted.
24835 COVER_STRING, if non-nil, is a display string that may cover some
24836 or all of the highlighted text. */
24837
24838 static void
24839 mouse_face_from_buffer_pos (Lisp_Object window,
24840 Mouse_HLInfo *hlinfo,
24841 EMACS_INT mouse_charpos,
24842 EMACS_INT start_charpos,
24843 EMACS_INT end_charpos,
24844 Lisp_Object before_string,
24845 Lisp_Object after_string,
24846 Lisp_Object cover_string)
24847 {
24848 struct window *w = XWINDOW (window);
24849 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24850 struct glyph_row *r1, *r2;
24851 struct glyph *glyph, *end;
24852 EMACS_INT ignore, pos;
24853 int x;
24854
24855 xassert (NILP (cover_string) || STRINGP (cover_string));
24856 xassert (NILP (before_string) || STRINGP (before_string));
24857 xassert (NILP (after_string) || STRINGP (after_string));
24858
24859 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24860 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24861 if (r1 == NULL)
24862 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24863 /* If the before-string or display-string contains newlines,
24864 rows_from_pos_range skips to its last row. Move back. */
24865 if (!NILP (before_string) || !NILP (cover_string))
24866 {
24867 struct glyph_row *prev;
24868 while ((prev = r1 - 1, prev >= first)
24869 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24870 && prev->used[TEXT_AREA] > 0)
24871 {
24872 struct glyph *beg = prev->glyphs[TEXT_AREA];
24873 glyph = beg + prev->used[TEXT_AREA];
24874 while (--glyph >= beg && INTEGERP (glyph->object));
24875 if (glyph < beg
24876 || !(EQ (glyph->object, before_string)
24877 || EQ (glyph->object, cover_string)))
24878 break;
24879 r1 = prev;
24880 }
24881 }
24882 if (r2 == NULL)
24883 {
24884 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24885 hlinfo->mouse_face_past_end = 1;
24886 }
24887 else if (!NILP (after_string))
24888 {
24889 /* If the after-string has newlines, advance to its last row. */
24890 struct glyph_row *next;
24891 struct glyph_row *last
24892 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24893
24894 for (next = r2 + 1;
24895 next <= last
24896 && next->used[TEXT_AREA] > 0
24897 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24898 ++next)
24899 r2 = next;
24900 }
24901 /* The rest of the display engine assumes that mouse_face_beg_row is
24902 either above below mouse_face_end_row or identical to it. But
24903 with bidi-reordered continued lines, the row for START_CHARPOS
24904 could be below the row for END_CHARPOS. If so, swap the rows and
24905 store them in correct order. */
24906 if (r1->y > r2->y)
24907 {
24908 struct glyph_row *tem = r2;
24909
24910 r2 = r1;
24911 r1 = tem;
24912 }
24913
24914 hlinfo->mouse_face_beg_y = r1->y;
24915 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24916 hlinfo->mouse_face_end_y = r2->y;
24917 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24918
24919 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24920 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
24921 could be anywhere in the row and in any order. The strategy
24922 below is to find the leftmost and the rightmost glyph that
24923 belongs to either of these 3 strings, or whose position is
24924 between START_CHARPOS and END_CHARPOS, and highlight all the
24925 glyphs between those two. This may cover more than just the text
24926 between START_CHARPOS and END_CHARPOS if the range of characters
24927 strides the bidi level boundary, e.g. if the beginning is in R2L
24928 text while the end is in L2R text or vice versa. */
24929 if (!r1->reversed_p)
24930 {
24931 /* This row is in a left to right paragraph. Scan it left to
24932 right. */
24933 glyph = r1->glyphs[TEXT_AREA];
24934 end = glyph + r1->used[TEXT_AREA];
24935 x = r1->x;
24936
24937 /* Skip truncation glyphs at the start of the glyph row. */
24938 if (r1->displays_text_p)
24939 for (; glyph < end
24940 && INTEGERP (glyph->object)
24941 && glyph->charpos < 0;
24942 ++glyph)
24943 x += glyph->pixel_width;
24944
24945 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24946 or COVER_STRING, and the first glyph from buffer whose
24947 position is between START_CHARPOS and END_CHARPOS. */
24948 for (; glyph < end
24949 && !INTEGERP (glyph->object)
24950 && !EQ (glyph->object, cover_string)
24951 && !(BUFFERP (glyph->object)
24952 && (glyph->charpos >= start_charpos
24953 && glyph->charpos < end_charpos));
24954 ++glyph)
24955 {
24956 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24957 are present at buffer positions between START_CHARPOS and
24958 END_CHARPOS, or if they come from an overlay. */
24959 if (EQ (glyph->object, before_string))
24960 {
24961 pos = string_buffer_position (before_string,
24962 start_charpos);
24963 /* If pos == 0, it means before_string came from an
24964 overlay, not from a buffer position. */
24965 if (!pos || (pos >= start_charpos && pos < end_charpos))
24966 break;
24967 }
24968 else if (EQ (glyph->object, after_string))
24969 {
24970 pos = string_buffer_position (after_string, end_charpos);
24971 if (!pos || (pos >= start_charpos && pos < end_charpos))
24972 break;
24973 }
24974 x += glyph->pixel_width;
24975 }
24976 hlinfo->mouse_face_beg_x = x;
24977 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24978 }
24979 else
24980 {
24981 /* This row is in a right to left paragraph. Scan it right to
24982 left. */
24983 struct glyph *g;
24984
24985 end = r1->glyphs[TEXT_AREA] - 1;
24986 glyph = end + r1->used[TEXT_AREA];
24987
24988 /* Skip truncation glyphs at the start of the glyph row. */
24989 if (r1->displays_text_p)
24990 for (; glyph > end
24991 && INTEGERP (glyph->object)
24992 && glyph->charpos < 0;
24993 --glyph)
24994 ;
24995
24996 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24997 or COVER_STRING, and the first glyph from buffer whose
24998 position is between START_CHARPOS and END_CHARPOS. */
24999 for (; glyph > end
25000 && !INTEGERP (glyph->object)
25001 && !EQ (glyph->object, cover_string)
25002 && !(BUFFERP (glyph->object)
25003 && (glyph->charpos >= start_charpos
25004 && glyph->charpos < end_charpos));
25005 --glyph)
25006 {
25007 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25008 are present at buffer positions between START_CHARPOS and
25009 END_CHARPOS, or if they come from an overlay. */
25010 if (EQ (glyph->object, before_string))
25011 {
25012 pos = string_buffer_position (before_string, start_charpos);
25013 /* If pos == 0, it means before_string came from an
25014 overlay, not from a buffer position. */
25015 if (!pos || (pos >= start_charpos && pos < end_charpos))
25016 break;
25017 }
25018 else if (EQ (glyph->object, after_string))
25019 {
25020 pos = string_buffer_position (after_string, end_charpos);
25021 if (!pos || (pos >= start_charpos && pos < end_charpos))
25022 break;
25023 }
25024 }
25025
25026 glyph++; /* first glyph to the right of the highlighted area */
25027 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25028 x += g->pixel_width;
25029 hlinfo->mouse_face_beg_x = x;
25030 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25031 }
25032
25033 /* If the highlight ends in a different row, compute GLYPH and END
25034 for the end row. Otherwise, reuse the values computed above for
25035 the row where the highlight begins. */
25036 if (r2 != r1)
25037 {
25038 if (!r2->reversed_p)
25039 {
25040 glyph = r2->glyphs[TEXT_AREA];
25041 end = glyph + r2->used[TEXT_AREA];
25042 x = r2->x;
25043 }
25044 else
25045 {
25046 end = r2->glyphs[TEXT_AREA] - 1;
25047 glyph = end + r2->used[TEXT_AREA];
25048 }
25049 }
25050
25051 if (!r2->reversed_p)
25052 {
25053 /* Skip truncation and continuation glyphs near the end of the
25054 row, and also blanks and stretch glyphs inserted by
25055 extend_face_to_end_of_line. */
25056 while (end > glyph
25057 && INTEGERP ((end - 1)->object)
25058 && (end - 1)->charpos <= 0)
25059 --end;
25060 /* Scan the rest of the glyph row from the end, looking for the
25061 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25062 COVER_STRING, or whose position is between START_CHARPOS
25063 and END_CHARPOS */
25064 for (--end;
25065 end > glyph
25066 && !INTEGERP (end->object)
25067 && !EQ (end->object, cover_string)
25068 && !(BUFFERP (end->object)
25069 && (end->charpos >= start_charpos
25070 && end->charpos < end_charpos));
25071 --end)
25072 {
25073 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25074 are present at buffer positions between START_CHARPOS and
25075 END_CHARPOS, or if they come from an overlay. */
25076 if (EQ (end->object, before_string))
25077 {
25078 pos = string_buffer_position (before_string, start_charpos);
25079 if (!pos || (pos >= start_charpos && pos < end_charpos))
25080 break;
25081 }
25082 else if (EQ (end->object, after_string))
25083 {
25084 pos = string_buffer_position (after_string, end_charpos);
25085 if (!pos || (pos >= start_charpos && pos < end_charpos))
25086 break;
25087 }
25088 }
25089 /* Find the X coordinate of the last glyph to be highlighted. */
25090 for (; glyph <= end; ++glyph)
25091 x += glyph->pixel_width;
25092
25093 hlinfo->mouse_face_end_x = x;
25094 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25095 }
25096 else
25097 {
25098 /* Skip truncation and continuation glyphs near the end of the
25099 row, and also blanks and stretch glyphs inserted by
25100 extend_face_to_end_of_line. */
25101 x = r2->x;
25102 end++;
25103 while (end < glyph
25104 && INTEGERP (end->object)
25105 && end->charpos <= 0)
25106 {
25107 x += end->pixel_width;
25108 ++end;
25109 }
25110 /* Scan the rest of the glyph row from the end, looking for the
25111 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25112 COVER_STRING, or whose position is between START_CHARPOS
25113 and END_CHARPOS */
25114 for ( ;
25115 end < glyph
25116 && !INTEGERP (end->object)
25117 && !EQ (end->object, cover_string)
25118 && !(BUFFERP (end->object)
25119 && (end->charpos >= start_charpos
25120 && end->charpos < end_charpos));
25121 ++end)
25122 {
25123 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25124 are present at buffer positions between START_CHARPOS and
25125 END_CHARPOS, or if they come from an overlay. */
25126 if (EQ (end->object, before_string))
25127 {
25128 pos = string_buffer_position (before_string, start_charpos);
25129 if (!pos || (pos >= start_charpos && pos < end_charpos))
25130 break;
25131 }
25132 else if (EQ (end->object, after_string))
25133 {
25134 pos = string_buffer_position (after_string, end_charpos);
25135 if (!pos || (pos >= start_charpos && pos < end_charpos))
25136 break;
25137 }
25138 x += end->pixel_width;
25139 }
25140 hlinfo->mouse_face_end_x = x;
25141 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25142 }
25143
25144 hlinfo->mouse_face_window = window;
25145 hlinfo->mouse_face_face_id
25146 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25147 mouse_charpos + 1,
25148 !hlinfo->mouse_face_hidden, -1);
25149 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25150 }
25151
25152 /* The following function is not used anymore (replaced with
25153 mouse_face_from_string_pos), but I leave it here for the time
25154 being, in case someone would. */
25155
25156 #if 0 /* not used */
25157
25158 /* Find the position of the glyph for position POS in OBJECT in
25159 window W's current matrix, and return in *X, *Y the pixel
25160 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25161
25162 RIGHT_P non-zero means return the position of the right edge of the
25163 glyph, RIGHT_P zero means return the left edge position.
25164
25165 If no glyph for POS exists in the matrix, return the position of
25166 the glyph with the next smaller position that is in the matrix, if
25167 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25168 exists in the matrix, return the position of the glyph with the
25169 next larger position in OBJECT.
25170
25171 Value is non-zero if a glyph was found. */
25172
25173 static int
25174 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25175 int *hpos, int *vpos, int *x, int *y, int right_p)
25176 {
25177 int yb = window_text_bottom_y (w);
25178 struct glyph_row *r;
25179 struct glyph *best_glyph = NULL;
25180 struct glyph_row *best_row = NULL;
25181 int best_x = 0;
25182
25183 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25184 r->enabled_p && r->y < yb;
25185 ++r)
25186 {
25187 struct glyph *g = r->glyphs[TEXT_AREA];
25188 struct glyph *e = g + r->used[TEXT_AREA];
25189 int gx;
25190
25191 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25192 if (EQ (g->object, object))
25193 {
25194 if (g->charpos == pos)
25195 {
25196 best_glyph = g;
25197 best_x = gx;
25198 best_row = r;
25199 goto found;
25200 }
25201 else if (best_glyph == NULL
25202 || ((eabs (g->charpos - pos)
25203 < eabs (best_glyph->charpos - pos))
25204 && (right_p
25205 ? g->charpos < pos
25206 : g->charpos > pos)))
25207 {
25208 best_glyph = g;
25209 best_x = gx;
25210 best_row = r;
25211 }
25212 }
25213 }
25214
25215 found:
25216
25217 if (best_glyph)
25218 {
25219 *x = best_x;
25220 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25221
25222 if (right_p)
25223 {
25224 *x += best_glyph->pixel_width;
25225 ++*hpos;
25226 }
25227
25228 *y = best_row->y;
25229 *vpos = best_row - w->current_matrix->rows;
25230 }
25231
25232 return best_glyph != NULL;
25233 }
25234 #endif /* not used */
25235
25236 /* Find the positions of the first and the last glyphs in window W's
25237 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25238 (assumed to be a string), and return in HLINFO's mouse_face_*
25239 members the pixel and column/row coordinates of those glyphs. */
25240
25241 static void
25242 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25243 Lisp_Object object,
25244 EMACS_INT startpos, EMACS_INT endpos)
25245 {
25246 int yb = window_text_bottom_y (w);
25247 struct glyph_row *r;
25248 struct glyph *g, *e;
25249 int gx;
25250 int found = 0;
25251
25252 /* Find the glyph row with at least one position in the range
25253 [STARTPOS..ENDPOS], and the first glyph in that row whose
25254 position belongs to that range. */
25255 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25256 r->enabled_p && r->y < yb;
25257 ++r)
25258 {
25259 if (!r->reversed_p)
25260 {
25261 g = r->glyphs[TEXT_AREA];
25262 e = g + r->used[TEXT_AREA];
25263 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25264 if (EQ (g->object, object)
25265 && startpos <= g->charpos && g->charpos <= endpos)
25266 {
25267 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25268 hlinfo->mouse_face_beg_y = r->y;
25269 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25270 hlinfo->mouse_face_beg_x = gx;
25271 found = 1;
25272 break;
25273 }
25274 }
25275 else
25276 {
25277 struct glyph *g1;
25278
25279 e = r->glyphs[TEXT_AREA];
25280 g = e + r->used[TEXT_AREA];
25281 for ( ; g > e; --g)
25282 if (EQ ((g-1)->object, object)
25283 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25284 {
25285 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25286 hlinfo->mouse_face_beg_y = r->y;
25287 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25288 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25289 gx += g1->pixel_width;
25290 hlinfo->mouse_face_beg_x = gx;
25291 found = 1;
25292 break;
25293 }
25294 }
25295 if (found)
25296 break;
25297 }
25298
25299 if (!found)
25300 return;
25301
25302 /* Starting with the next row, look for the first row which does NOT
25303 include any glyphs whose positions are in the range. */
25304 for (++r; r->enabled_p && r->y < yb; ++r)
25305 {
25306 g = r->glyphs[TEXT_AREA];
25307 e = g + r->used[TEXT_AREA];
25308 found = 0;
25309 for ( ; g < e; ++g)
25310 if (EQ (g->object, object)
25311 && startpos <= g->charpos && g->charpos <= endpos)
25312 {
25313 found = 1;
25314 break;
25315 }
25316 if (!found)
25317 break;
25318 }
25319
25320 /* The highlighted region ends on the previous row. */
25321 r--;
25322
25323 /* Set the end row and its vertical pixel coordinate. */
25324 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25325 hlinfo->mouse_face_end_y = r->y;
25326
25327 /* Compute and set the end column and the end column's horizontal
25328 pixel coordinate. */
25329 if (!r->reversed_p)
25330 {
25331 g = r->glyphs[TEXT_AREA];
25332 e = g + r->used[TEXT_AREA];
25333 for ( ; e > g; --e)
25334 if (EQ ((e-1)->object, object)
25335 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25336 break;
25337 hlinfo->mouse_face_end_col = e - g;
25338
25339 for (gx = r->x; g < e; ++g)
25340 gx += g->pixel_width;
25341 hlinfo->mouse_face_end_x = gx;
25342 }
25343 else
25344 {
25345 e = r->glyphs[TEXT_AREA];
25346 g = e + r->used[TEXT_AREA];
25347 for (gx = r->x ; e < g; ++e)
25348 {
25349 if (EQ (e->object, object)
25350 && startpos <= e->charpos && e->charpos <= endpos)
25351 break;
25352 gx += e->pixel_width;
25353 }
25354 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25355 hlinfo->mouse_face_end_x = gx;
25356 }
25357 }
25358
25359 #ifdef HAVE_WINDOW_SYSTEM
25360
25361 /* See if position X, Y is within a hot-spot of an image. */
25362
25363 static int
25364 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25365 {
25366 if (!CONSP (hot_spot))
25367 return 0;
25368
25369 if (EQ (XCAR (hot_spot), Qrect))
25370 {
25371 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25372 Lisp_Object rect = XCDR (hot_spot);
25373 Lisp_Object tem;
25374 if (!CONSP (rect))
25375 return 0;
25376 if (!CONSP (XCAR (rect)))
25377 return 0;
25378 if (!CONSP (XCDR (rect)))
25379 return 0;
25380 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25381 return 0;
25382 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25383 return 0;
25384 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25385 return 0;
25386 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25387 return 0;
25388 return 1;
25389 }
25390 else if (EQ (XCAR (hot_spot), Qcircle))
25391 {
25392 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25393 Lisp_Object circ = XCDR (hot_spot);
25394 Lisp_Object lr, lx0, ly0;
25395 if (CONSP (circ)
25396 && CONSP (XCAR (circ))
25397 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25398 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25399 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25400 {
25401 double r = XFLOATINT (lr);
25402 double dx = XINT (lx0) - x;
25403 double dy = XINT (ly0) - y;
25404 return (dx * dx + dy * dy <= r * r);
25405 }
25406 }
25407 else if (EQ (XCAR (hot_spot), Qpoly))
25408 {
25409 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25410 if (VECTORP (XCDR (hot_spot)))
25411 {
25412 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25413 Lisp_Object *poly = v->contents;
25414 int n = v->header.size;
25415 int i;
25416 int inside = 0;
25417 Lisp_Object lx, ly;
25418 int x0, y0;
25419
25420 /* Need an even number of coordinates, and at least 3 edges. */
25421 if (n < 6 || n & 1)
25422 return 0;
25423
25424 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25425 If count is odd, we are inside polygon. Pixels on edges
25426 may or may not be included depending on actual geometry of the
25427 polygon. */
25428 if ((lx = poly[n-2], !INTEGERP (lx))
25429 || (ly = poly[n-1], !INTEGERP (lx)))
25430 return 0;
25431 x0 = XINT (lx), y0 = XINT (ly);
25432 for (i = 0; i < n; i += 2)
25433 {
25434 int x1 = x0, y1 = y0;
25435 if ((lx = poly[i], !INTEGERP (lx))
25436 || (ly = poly[i+1], !INTEGERP (ly)))
25437 return 0;
25438 x0 = XINT (lx), y0 = XINT (ly);
25439
25440 /* Does this segment cross the X line? */
25441 if (x0 >= x)
25442 {
25443 if (x1 >= x)
25444 continue;
25445 }
25446 else if (x1 < x)
25447 continue;
25448 if (y > y0 && y > y1)
25449 continue;
25450 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25451 inside = !inside;
25452 }
25453 return inside;
25454 }
25455 }
25456 return 0;
25457 }
25458
25459 Lisp_Object
25460 find_hot_spot (Lisp_Object map, int x, int y)
25461 {
25462 while (CONSP (map))
25463 {
25464 if (CONSP (XCAR (map))
25465 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25466 return XCAR (map);
25467 map = XCDR (map);
25468 }
25469
25470 return Qnil;
25471 }
25472
25473 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25474 3, 3, 0,
25475 doc: /* Lookup in image map MAP coordinates X and Y.
25476 An image map is an alist where each element has the format (AREA ID PLIST).
25477 An AREA is specified as either a rectangle, a circle, or a polygon:
25478 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25479 pixel coordinates of the upper left and bottom right corners.
25480 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25481 and the radius of the circle; r may be a float or integer.
25482 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25483 vector describes one corner in the polygon.
25484 Returns the alist element for the first matching AREA in MAP. */)
25485 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25486 {
25487 if (NILP (map))
25488 return Qnil;
25489
25490 CHECK_NUMBER (x);
25491 CHECK_NUMBER (y);
25492
25493 return find_hot_spot (map, XINT (x), XINT (y));
25494 }
25495
25496
25497 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25498 static void
25499 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25500 {
25501 /* Do not change cursor shape while dragging mouse. */
25502 if (!NILP (do_mouse_tracking))
25503 return;
25504
25505 if (!NILP (pointer))
25506 {
25507 if (EQ (pointer, Qarrow))
25508 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25509 else if (EQ (pointer, Qhand))
25510 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25511 else if (EQ (pointer, Qtext))
25512 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25513 else if (EQ (pointer, intern ("hdrag")))
25514 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25515 #ifdef HAVE_X_WINDOWS
25516 else if (EQ (pointer, intern ("vdrag")))
25517 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25518 #endif
25519 else if (EQ (pointer, intern ("hourglass")))
25520 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25521 else if (EQ (pointer, Qmodeline))
25522 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25523 else
25524 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25525 }
25526
25527 if (cursor != No_Cursor)
25528 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25529 }
25530
25531 #endif /* HAVE_WINDOW_SYSTEM */
25532
25533 /* Take proper action when mouse has moved to the mode or header line
25534 or marginal area AREA of window W, x-position X and y-position Y.
25535 X is relative to the start of the text display area of W, so the
25536 width of bitmap areas and scroll bars must be subtracted to get a
25537 position relative to the start of the mode line. */
25538
25539 static void
25540 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25541 enum window_part area)
25542 {
25543 struct window *w = XWINDOW (window);
25544 struct frame *f = XFRAME (w->frame);
25545 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25546 #ifdef HAVE_WINDOW_SYSTEM
25547 Display_Info *dpyinfo;
25548 #endif
25549 Cursor cursor = No_Cursor;
25550 Lisp_Object pointer = Qnil;
25551 int dx, dy, width, height;
25552 EMACS_INT charpos;
25553 Lisp_Object string, object = Qnil;
25554 Lisp_Object pos, help;
25555
25556 Lisp_Object mouse_face;
25557 int original_x_pixel = x;
25558 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25559 struct glyph_row *row;
25560
25561 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25562 {
25563 int x0;
25564 struct glyph *end;
25565
25566 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25567 returns them in row/column units! */
25568 string = mode_line_string (w, area, &x, &y, &charpos,
25569 &object, &dx, &dy, &width, &height);
25570
25571 row = (area == ON_MODE_LINE
25572 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25573 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25574
25575 /* Find the glyph under the mouse pointer. */
25576 if (row->mode_line_p && row->enabled_p)
25577 {
25578 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25579 end = glyph + row->used[TEXT_AREA];
25580
25581 for (x0 = original_x_pixel;
25582 glyph < end && x0 >= glyph->pixel_width;
25583 ++glyph)
25584 x0 -= glyph->pixel_width;
25585
25586 if (glyph >= end)
25587 glyph = NULL;
25588 }
25589 }
25590 else
25591 {
25592 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25593 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25594 returns them in row/column units! */
25595 string = marginal_area_string (w, area, &x, &y, &charpos,
25596 &object, &dx, &dy, &width, &height);
25597 }
25598
25599 help = Qnil;
25600
25601 #ifdef HAVE_WINDOW_SYSTEM
25602 if (IMAGEP (object))
25603 {
25604 Lisp_Object image_map, hotspot;
25605 if ((image_map = Fplist_get (XCDR (object), QCmap),
25606 !NILP (image_map))
25607 && (hotspot = find_hot_spot (image_map, dx, dy),
25608 CONSP (hotspot))
25609 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25610 {
25611 Lisp_Object plist;
25612
25613 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25614 If so, we could look for mouse-enter, mouse-leave
25615 properties in PLIST (and do something...). */
25616 hotspot = XCDR (hotspot);
25617 if (CONSP (hotspot)
25618 && (plist = XCAR (hotspot), CONSP (plist)))
25619 {
25620 pointer = Fplist_get (plist, Qpointer);
25621 if (NILP (pointer))
25622 pointer = Qhand;
25623 help = Fplist_get (plist, Qhelp_echo);
25624 if (!NILP (help))
25625 {
25626 help_echo_string = help;
25627 /* Is this correct? ++kfs */
25628 XSETWINDOW (help_echo_window, w);
25629 help_echo_object = w->buffer;
25630 help_echo_pos = charpos;
25631 }
25632 }
25633 }
25634 if (NILP (pointer))
25635 pointer = Fplist_get (XCDR (object), QCpointer);
25636 }
25637 #endif /* HAVE_WINDOW_SYSTEM */
25638
25639 if (STRINGP (string))
25640 {
25641 pos = make_number (charpos);
25642 /* If we're on a string with `help-echo' text property, arrange
25643 for the help to be displayed. This is done by setting the
25644 global variable help_echo_string to the help string. */
25645 if (NILP (help))
25646 {
25647 help = Fget_text_property (pos, Qhelp_echo, string);
25648 if (!NILP (help))
25649 {
25650 help_echo_string = help;
25651 XSETWINDOW (help_echo_window, w);
25652 help_echo_object = string;
25653 help_echo_pos = charpos;
25654 }
25655 }
25656
25657 #ifdef HAVE_WINDOW_SYSTEM
25658 if (FRAME_WINDOW_P (f))
25659 {
25660 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25661 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25662 if (NILP (pointer))
25663 pointer = Fget_text_property (pos, Qpointer, string);
25664
25665 /* Change the mouse pointer according to what is under X/Y. */
25666 if (NILP (pointer)
25667 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25668 {
25669 Lisp_Object map;
25670 map = Fget_text_property (pos, Qlocal_map, string);
25671 if (!KEYMAPP (map))
25672 map = Fget_text_property (pos, Qkeymap, string);
25673 if (!KEYMAPP (map))
25674 cursor = dpyinfo->vertical_scroll_bar_cursor;
25675 }
25676 }
25677 #endif
25678
25679 /* Change the mouse face according to what is under X/Y. */
25680 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25681 if (!NILP (mouse_face)
25682 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25683 && glyph)
25684 {
25685 Lisp_Object b, e;
25686
25687 struct glyph * tmp_glyph;
25688
25689 int gpos;
25690 int gseq_length;
25691 int total_pixel_width;
25692 EMACS_INT begpos, endpos, ignore;
25693
25694 int vpos, hpos;
25695
25696 b = Fprevious_single_property_change (make_number (charpos + 1),
25697 Qmouse_face, string, Qnil);
25698 if (NILP (b))
25699 begpos = 0;
25700 else
25701 begpos = XINT (b);
25702
25703 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25704 if (NILP (e))
25705 endpos = SCHARS (string);
25706 else
25707 endpos = XINT (e);
25708
25709 /* Calculate the glyph position GPOS of GLYPH in the
25710 displayed string, relative to the beginning of the
25711 highlighted part of the string.
25712
25713 Note: GPOS is different from CHARPOS. CHARPOS is the
25714 position of GLYPH in the internal string object. A mode
25715 line string format has structures which are converted to
25716 a flattened string by the Emacs Lisp interpreter. The
25717 internal string is an element of those structures. The
25718 displayed string is the flattened string. */
25719 tmp_glyph = row_start_glyph;
25720 while (tmp_glyph < glyph
25721 && (!(EQ (tmp_glyph->object, glyph->object)
25722 && begpos <= tmp_glyph->charpos
25723 && tmp_glyph->charpos < endpos)))
25724 tmp_glyph++;
25725 gpos = glyph - tmp_glyph;
25726
25727 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25728 the highlighted part of the displayed string to which
25729 GLYPH belongs. Note: GSEQ_LENGTH is different from
25730 SCHARS (STRING), because the latter returns the length of
25731 the internal string. */
25732 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25733 tmp_glyph > glyph
25734 && (!(EQ (tmp_glyph->object, glyph->object)
25735 && begpos <= tmp_glyph->charpos
25736 && tmp_glyph->charpos < endpos));
25737 tmp_glyph--)
25738 ;
25739 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25740
25741 /* Calculate the total pixel width of all the glyphs between
25742 the beginning of the highlighted area and GLYPH. */
25743 total_pixel_width = 0;
25744 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25745 total_pixel_width += tmp_glyph->pixel_width;
25746
25747 /* Pre calculation of re-rendering position. Note: X is in
25748 column units here, after the call to mode_line_string or
25749 marginal_area_string. */
25750 hpos = x - gpos;
25751 vpos = (area == ON_MODE_LINE
25752 ? (w->current_matrix)->nrows - 1
25753 : 0);
25754
25755 /* If GLYPH's position is included in the region that is
25756 already drawn in mouse face, we have nothing to do. */
25757 if ( EQ (window, hlinfo->mouse_face_window)
25758 && (!row->reversed_p
25759 ? (hlinfo->mouse_face_beg_col <= hpos
25760 && hpos < hlinfo->mouse_face_end_col)
25761 /* In R2L rows we swap BEG and END, see below. */
25762 : (hlinfo->mouse_face_end_col <= hpos
25763 && hpos < hlinfo->mouse_face_beg_col))
25764 && hlinfo->mouse_face_beg_row == vpos )
25765 return;
25766
25767 if (clear_mouse_face (hlinfo))
25768 cursor = No_Cursor;
25769
25770 if (!row->reversed_p)
25771 {
25772 hlinfo->mouse_face_beg_col = hpos;
25773 hlinfo->mouse_face_beg_x = original_x_pixel
25774 - (total_pixel_width + dx);
25775 hlinfo->mouse_face_end_col = hpos + gseq_length;
25776 hlinfo->mouse_face_end_x = 0;
25777 }
25778 else
25779 {
25780 /* In R2L rows, show_mouse_face expects BEG and END
25781 coordinates to be swapped. */
25782 hlinfo->mouse_face_end_col = hpos;
25783 hlinfo->mouse_face_end_x = original_x_pixel
25784 - (total_pixel_width + dx);
25785 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25786 hlinfo->mouse_face_beg_x = 0;
25787 }
25788
25789 hlinfo->mouse_face_beg_row = vpos;
25790 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25791 hlinfo->mouse_face_beg_y = 0;
25792 hlinfo->mouse_face_end_y = 0;
25793 hlinfo->mouse_face_past_end = 0;
25794 hlinfo->mouse_face_window = window;
25795
25796 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25797 charpos,
25798 0, 0, 0,
25799 &ignore,
25800 glyph->face_id,
25801 1);
25802 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25803
25804 if (NILP (pointer))
25805 pointer = Qhand;
25806 }
25807 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25808 clear_mouse_face (hlinfo);
25809 }
25810 #ifdef HAVE_WINDOW_SYSTEM
25811 if (FRAME_WINDOW_P (f))
25812 define_frame_cursor1 (f, cursor, pointer);
25813 #endif
25814 }
25815
25816
25817 /* EXPORT:
25818 Take proper action when the mouse has moved to position X, Y on
25819 frame F as regards highlighting characters that have mouse-face
25820 properties. Also de-highlighting chars where the mouse was before.
25821 X and Y can be negative or out of range. */
25822
25823 void
25824 note_mouse_highlight (struct frame *f, int x, int y)
25825 {
25826 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25827 enum window_part part;
25828 Lisp_Object window;
25829 struct window *w;
25830 Cursor cursor = No_Cursor;
25831 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25832 struct buffer *b;
25833
25834 /* When a menu is active, don't highlight because this looks odd. */
25835 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25836 if (popup_activated ())
25837 return;
25838 #endif
25839
25840 if (NILP (Vmouse_highlight)
25841 || !f->glyphs_initialized_p
25842 || f->pointer_invisible)
25843 return;
25844
25845 hlinfo->mouse_face_mouse_x = x;
25846 hlinfo->mouse_face_mouse_y = y;
25847 hlinfo->mouse_face_mouse_frame = f;
25848
25849 if (hlinfo->mouse_face_defer)
25850 return;
25851
25852 if (gc_in_progress)
25853 {
25854 hlinfo->mouse_face_deferred_gc = 1;
25855 return;
25856 }
25857
25858 /* Which window is that in? */
25859 window = window_from_coordinates (f, x, y, &part, 1);
25860
25861 /* If we were displaying active text in another window, clear that.
25862 Also clear if we move out of text area in same window. */
25863 if (! EQ (window, hlinfo->mouse_face_window)
25864 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25865 && !NILP (hlinfo->mouse_face_window)))
25866 clear_mouse_face (hlinfo);
25867
25868 /* Not on a window -> return. */
25869 if (!WINDOWP (window))
25870 return;
25871
25872 /* Reset help_echo_string. It will get recomputed below. */
25873 help_echo_string = Qnil;
25874
25875 /* Convert to window-relative pixel coordinates. */
25876 w = XWINDOW (window);
25877 frame_to_window_pixel_xy (w, &x, &y);
25878
25879 #ifdef HAVE_WINDOW_SYSTEM
25880 /* Handle tool-bar window differently since it doesn't display a
25881 buffer. */
25882 if (EQ (window, f->tool_bar_window))
25883 {
25884 note_tool_bar_highlight (f, x, y);
25885 return;
25886 }
25887 #endif
25888
25889 /* Mouse is on the mode, header line or margin? */
25890 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25891 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25892 {
25893 note_mode_line_or_margin_highlight (window, x, y, part);
25894 return;
25895 }
25896
25897 #ifdef HAVE_WINDOW_SYSTEM
25898 if (part == ON_VERTICAL_BORDER)
25899 {
25900 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25901 help_echo_string = build_string ("drag-mouse-1: resize");
25902 }
25903 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25904 || part == ON_SCROLL_BAR)
25905 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25906 else
25907 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25908 #endif
25909
25910 /* Are we in a window whose display is up to date?
25911 And verify the buffer's text has not changed. */
25912 b = XBUFFER (w->buffer);
25913 if (part == ON_TEXT
25914 && EQ (w->window_end_valid, w->buffer)
25915 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25916 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25917 {
25918 int hpos, vpos, i, dx, dy, area;
25919 EMACS_INT pos;
25920 struct glyph *glyph;
25921 Lisp_Object object;
25922 Lisp_Object mouse_face = Qnil, position;
25923 Lisp_Object *overlay_vec = NULL;
25924 int noverlays;
25925 struct buffer *obuf;
25926 EMACS_INT obegv, ozv;
25927 int same_region;
25928
25929 /* Find the glyph under X/Y. */
25930 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25931
25932 #ifdef HAVE_WINDOW_SYSTEM
25933 /* Look for :pointer property on image. */
25934 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25935 {
25936 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25937 if (img != NULL && IMAGEP (img->spec))
25938 {
25939 Lisp_Object image_map, hotspot;
25940 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25941 !NILP (image_map))
25942 && (hotspot = find_hot_spot (image_map,
25943 glyph->slice.img.x + dx,
25944 glyph->slice.img.y + dy),
25945 CONSP (hotspot))
25946 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25947 {
25948 Lisp_Object plist;
25949
25950 /* Could check XCAR (hotspot) to see if we enter/leave
25951 this hot-spot.
25952 If so, we could look for mouse-enter, mouse-leave
25953 properties in PLIST (and do something...). */
25954 hotspot = XCDR (hotspot);
25955 if (CONSP (hotspot)
25956 && (plist = XCAR (hotspot), CONSP (plist)))
25957 {
25958 pointer = Fplist_get (plist, Qpointer);
25959 if (NILP (pointer))
25960 pointer = Qhand;
25961 help_echo_string = Fplist_get (plist, Qhelp_echo);
25962 if (!NILP (help_echo_string))
25963 {
25964 help_echo_window = window;
25965 help_echo_object = glyph->object;
25966 help_echo_pos = glyph->charpos;
25967 }
25968 }
25969 }
25970 if (NILP (pointer))
25971 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25972 }
25973 }
25974 #endif /* HAVE_WINDOW_SYSTEM */
25975
25976 /* Clear mouse face if X/Y not over text. */
25977 if (glyph == NULL
25978 || area != TEXT_AREA
25979 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25980 /* Glyph's OBJECT is an integer for glyphs inserted by the
25981 display engine for its internal purposes, like truncation
25982 and continuation glyphs and blanks beyond the end of
25983 line's text on text terminals. If we are over such a
25984 glyph, we are not over any text. */
25985 || INTEGERP (glyph->object)
25986 /* R2L rows have a stretch glyph at their front, which
25987 stands for no text, whereas L2R rows have no glyphs at
25988 all beyond the end of text. Treat such stretch glyphs
25989 like we do with NULL glyphs in L2R rows. */
25990 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25991 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25992 && glyph->type == STRETCH_GLYPH
25993 && glyph->avoid_cursor_p))
25994 {
25995 if (clear_mouse_face (hlinfo))
25996 cursor = No_Cursor;
25997 #ifdef HAVE_WINDOW_SYSTEM
25998 if (FRAME_WINDOW_P (f) && NILP (pointer))
25999 {
26000 if (area != TEXT_AREA)
26001 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26002 else
26003 pointer = Vvoid_text_area_pointer;
26004 }
26005 #endif
26006 goto set_cursor;
26007 }
26008
26009 pos = glyph->charpos;
26010 object = glyph->object;
26011 if (!STRINGP (object) && !BUFFERP (object))
26012 goto set_cursor;
26013
26014 /* If we get an out-of-range value, return now; avoid an error. */
26015 if (BUFFERP (object) && pos > BUF_Z (b))
26016 goto set_cursor;
26017
26018 /* Make the window's buffer temporarily current for
26019 overlays_at and compute_char_face. */
26020 obuf = current_buffer;
26021 current_buffer = b;
26022 obegv = BEGV;
26023 ozv = ZV;
26024 BEGV = BEG;
26025 ZV = Z;
26026
26027 /* Is this char mouse-active or does it have help-echo? */
26028 position = make_number (pos);
26029
26030 if (BUFFERP (object))
26031 {
26032 /* Put all the overlays we want in a vector in overlay_vec. */
26033 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26034 /* Sort overlays into increasing priority order. */
26035 noverlays = sort_overlays (overlay_vec, noverlays, w);
26036 }
26037 else
26038 noverlays = 0;
26039
26040 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26041
26042 if (same_region)
26043 cursor = No_Cursor;
26044
26045 /* Check mouse-face highlighting. */
26046 if (! same_region
26047 /* If there exists an overlay with mouse-face overlapping
26048 the one we are currently highlighting, we have to
26049 check if we enter the overlapping overlay, and then
26050 highlight only that. */
26051 || (OVERLAYP (hlinfo->mouse_face_overlay)
26052 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26053 {
26054 /* Find the highest priority overlay with a mouse-face. */
26055 Lisp_Object overlay = Qnil;
26056 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26057 {
26058 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26059 if (!NILP (mouse_face))
26060 overlay = overlay_vec[i];
26061 }
26062
26063 /* If we're highlighting the same overlay as before, there's
26064 no need to do that again. */
26065 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26066 goto check_help_echo;
26067 hlinfo->mouse_face_overlay = overlay;
26068
26069 /* Clear the display of the old active region, if any. */
26070 if (clear_mouse_face (hlinfo))
26071 cursor = No_Cursor;
26072
26073 /* If no overlay applies, get a text property. */
26074 if (NILP (overlay))
26075 mouse_face = Fget_text_property (position, Qmouse_face, object);
26076
26077 /* Next, compute the bounds of the mouse highlighting and
26078 display it. */
26079 if (!NILP (mouse_face) && STRINGP (object))
26080 {
26081 /* The mouse-highlighting comes from a display string
26082 with a mouse-face. */
26083 Lisp_Object s, e;
26084 EMACS_INT ignore;
26085
26086 s = Fprevious_single_property_change
26087 (make_number (pos + 1), Qmouse_face, object, Qnil);
26088 e = Fnext_single_property_change
26089 (position, Qmouse_face, object, Qnil);
26090 if (NILP (s))
26091 s = make_number (0);
26092 if (NILP (e))
26093 e = make_number (SCHARS (object) - 1);
26094 mouse_face_from_string_pos (w, hlinfo, object,
26095 XINT (s), XINT (e));
26096 hlinfo->mouse_face_past_end = 0;
26097 hlinfo->mouse_face_window = window;
26098 hlinfo->mouse_face_face_id
26099 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26100 glyph->face_id, 1);
26101 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26102 cursor = No_Cursor;
26103 }
26104 else
26105 {
26106 /* The mouse-highlighting, if any, comes from an overlay
26107 or text property in the buffer. */
26108 Lisp_Object buffer IF_LINT (= Qnil);
26109 Lisp_Object cover_string IF_LINT (= Qnil);
26110
26111 if (STRINGP (object))
26112 {
26113 /* If we are on a display string with no mouse-face,
26114 check if the text under it has one. */
26115 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26116 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26117 pos = string_buffer_position (object, start);
26118 if (pos > 0)
26119 {
26120 mouse_face = get_char_property_and_overlay
26121 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26122 buffer = w->buffer;
26123 cover_string = object;
26124 }
26125 }
26126 else
26127 {
26128 buffer = object;
26129 cover_string = Qnil;
26130 }
26131
26132 if (!NILP (mouse_face))
26133 {
26134 Lisp_Object before, after;
26135 Lisp_Object before_string, after_string;
26136 /* To correctly find the limits of mouse highlight
26137 in a bidi-reordered buffer, we must not use the
26138 optimization of limiting the search in
26139 previous-single-property-change and
26140 next-single-property-change, because
26141 rows_from_pos_range needs the real start and end
26142 positions to DTRT in this case. That's because
26143 the first row visible in a window does not
26144 necessarily display the character whose position
26145 is the smallest. */
26146 Lisp_Object lim1 =
26147 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26148 ? Fmarker_position (w->start)
26149 : Qnil;
26150 Lisp_Object lim2 =
26151 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26152 ? make_number (BUF_Z (XBUFFER (buffer))
26153 - XFASTINT (w->window_end_pos))
26154 : Qnil;
26155
26156 if (NILP (overlay))
26157 {
26158 /* Handle the text property case. */
26159 before = Fprevious_single_property_change
26160 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26161 after = Fnext_single_property_change
26162 (make_number (pos), Qmouse_face, buffer, lim2);
26163 before_string = after_string = Qnil;
26164 }
26165 else
26166 {
26167 /* Handle the overlay case. */
26168 before = Foverlay_start (overlay);
26169 after = Foverlay_end (overlay);
26170 before_string = Foverlay_get (overlay, Qbefore_string);
26171 after_string = Foverlay_get (overlay, Qafter_string);
26172
26173 if (!STRINGP (before_string)) before_string = Qnil;
26174 if (!STRINGP (after_string)) after_string = Qnil;
26175 }
26176
26177 mouse_face_from_buffer_pos (window, hlinfo, pos,
26178 XFASTINT (before),
26179 XFASTINT (after),
26180 before_string, after_string,
26181 cover_string);
26182 cursor = No_Cursor;
26183 }
26184 }
26185 }
26186
26187 check_help_echo:
26188
26189 /* Look for a `help-echo' property. */
26190 if (NILP (help_echo_string)) {
26191 Lisp_Object help, overlay;
26192
26193 /* Check overlays first. */
26194 help = overlay = Qnil;
26195 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26196 {
26197 overlay = overlay_vec[i];
26198 help = Foverlay_get (overlay, Qhelp_echo);
26199 }
26200
26201 if (!NILP (help))
26202 {
26203 help_echo_string = help;
26204 help_echo_window = window;
26205 help_echo_object = overlay;
26206 help_echo_pos = pos;
26207 }
26208 else
26209 {
26210 Lisp_Object obj = glyph->object;
26211 EMACS_INT charpos = glyph->charpos;
26212
26213 /* Try text properties. */
26214 if (STRINGP (obj)
26215 && charpos >= 0
26216 && charpos < SCHARS (obj))
26217 {
26218 help = Fget_text_property (make_number (charpos),
26219 Qhelp_echo, obj);
26220 if (NILP (help))
26221 {
26222 /* If the string itself doesn't specify a help-echo,
26223 see if the buffer text ``under'' it does. */
26224 struct glyph_row *r
26225 = MATRIX_ROW (w->current_matrix, vpos);
26226 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26227 EMACS_INT p = string_buffer_position (obj, start);
26228 if (p > 0)
26229 {
26230 help = Fget_char_property (make_number (p),
26231 Qhelp_echo, w->buffer);
26232 if (!NILP (help))
26233 {
26234 charpos = p;
26235 obj = w->buffer;
26236 }
26237 }
26238 }
26239 }
26240 else if (BUFFERP (obj)
26241 && charpos >= BEGV
26242 && charpos < ZV)
26243 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26244 obj);
26245
26246 if (!NILP (help))
26247 {
26248 help_echo_string = help;
26249 help_echo_window = window;
26250 help_echo_object = obj;
26251 help_echo_pos = charpos;
26252 }
26253 }
26254 }
26255
26256 #ifdef HAVE_WINDOW_SYSTEM
26257 /* Look for a `pointer' property. */
26258 if (FRAME_WINDOW_P (f) && NILP (pointer))
26259 {
26260 /* Check overlays first. */
26261 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26262 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26263
26264 if (NILP (pointer))
26265 {
26266 Lisp_Object obj = glyph->object;
26267 EMACS_INT charpos = glyph->charpos;
26268
26269 /* Try text properties. */
26270 if (STRINGP (obj)
26271 && charpos >= 0
26272 && charpos < SCHARS (obj))
26273 {
26274 pointer = Fget_text_property (make_number (charpos),
26275 Qpointer, obj);
26276 if (NILP (pointer))
26277 {
26278 /* If the string itself doesn't specify a pointer,
26279 see if the buffer text ``under'' it does. */
26280 struct glyph_row *r
26281 = MATRIX_ROW (w->current_matrix, vpos);
26282 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26283 EMACS_INT p = string_buffer_position (obj, start);
26284 if (p > 0)
26285 pointer = Fget_char_property (make_number (p),
26286 Qpointer, w->buffer);
26287 }
26288 }
26289 else if (BUFFERP (obj)
26290 && charpos >= BEGV
26291 && charpos < ZV)
26292 pointer = Fget_text_property (make_number (charpos),
26293 Qpointer, obj);
26294 }
26295 }
26296 #endif /* HAVE_WINDOW_SYSTEM */
26297
26298 BEGV = obegv;
26299 ZV = ozv;
26300 current_buffer = obuf;
26301 }
26302
26303 set_cursor:
26304
26305 #ifdef HAVE_WINDOW_SYSTEM
26306 if (FRAME_WINDOW_P (f))
26307 define_frame_cursor1 (f, cursor, pointer);
26308 #else
26309 /* This is here to prevent a compiler error, about "label at end of
26310 compound statement". */
26311 return;
26312 #endif
26313 }
26314
26315
26316 /* EXPORT for RIF:
26317 Clear any mouse-face on window W. This function is part of the
26318 redisplay interface, and is called from try_window_id and similar
26319 functions to ensure the mouse-highlight is off. */
26320
26321 void
26322 x_clear_window_mouse_face (struct window *w)
26323 {
26324 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26325 Lisp_Object window;
26326
26327 BLOCK_INPUT;
26328 XSETWINDOW (window, w);
26329 if (EQ (window, hlinfo->mouse_face_window))
26330 clear_mouse_face (hlinfo);
26331 UNBLOCK_INPUT;
26332 }
26333
26334
26335 /* EXPORT:
26336 Just discard the mouse face information for frame F, if any.
26337 This is used when the size of F is changed. */
26338
26339 void
26340 cancel_mouse_face (struct frame *f)
26341 {
26342 Lisp_Object window;
26343 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26344
26345 window = hlinfo->mouse_face_window;
26346 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26347 {
26348 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26349 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26350 hlinfo->mouse_face_window = Qnil;
26351 }
26352 }
26353
26354
26355 \f
26356 /***********************************************************************
26357 Exposure Events
26358 ***********************************************************************/
26359
26360 #ifdef HAVE_WINDOW_SYSTEM
26361
26362 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26363 which intersects rectangle R. R is in window-relative coordinates. */
26364
26365 static void
26366 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26367 enum glyph_row_area area)
26368 {
26369 struct glyph *first = row->glyphs[area];
26370 struct glyph *end = row->glyphs[area] + row->used[area];
26371 struct glyph *last;
26372 int first_x, start_x, x;
26373
26374 if (area == TEXT_AREA && row->fill_line_p)
26375 /* If row extends face to end of line write the whole line. */
26376 draw_glyphs (w, 0, row, area,
26377 0, row->used[area],
26378 DRAW_NORMAL_TEXT, 0);
26379 else
26380 {
26381 /* Set START_X to the window-relative start position for drawing glyphs of
26382 AREA. The first glyph of the text area can be partially visible.
26383 The first glyphs of other areas cannot. */
26384 start_x = window_box_left_offset (w, area);
26385 x = start_x;
26386 if (area == TEXT_AREA)
26387 x += row->x;
26388
26389 /* Find the first glyph that must be redrawn. */
26390 while (first < end
26391 && x + first->pixel_width < r->x)
26392 {
26393 x += first->pixel_width;
26394 ++first;
26395 }
26396
26397 /* Find the last one. */
26398 last = first;
26399 first_x = x;
26400 while (last < end
26401 && x < r->x + r->width)
26402 {
26403 x += last->pixel_width;
26404 ++last;
26405 }
26406
26407 /* Repaint. */
26408 if (last > first)
26409 draw_glyphs (w, first_x - start_x, row, area,
26410 first - row->glyphs[area], last - row->glyphs[area],
26411 DRAW_NORMAL_TEXT, 0);
26412 }
26413 }
26414
26415
26416 /* Redraw the parts of the glyph row ROW on window W intersecting
26417 rectangle R. R is in window-relative coordinates. Value is
26418 non-zero if mouse-face was overwritten. */
26419
26420 static int
26421 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26422 {
26423 xassert (row->enabled_p);
26424
26425 if (row->mode_line_p || w->pseudo_window_p)
26426 draw_glyphs (w, 0, row, TEXT_AREA,
26427 0, row->used[TEXT_AREA],
26428 DRAW_NORMAL_TEXT, 0);
26429 else
26430 {
26431 if (row->used[LEFT_MARGIN_AREA])
26432 expose_area (w, row, r, LEFT_MARGIN_AREA);
26433 if (row->used[TEXT_AREA])
26434 expose_area (w, row, r, TEXT_AREA);
26435 if (row->used[RIGHT_MARGIN_AREA])
26436 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26437 draw_row_fringe_bitmaps (w, row);
26438 }
26439
26440 return row->mouse_face_p;
26441 }
26442
26443
26444 /* Redraw those parts of glyphs rows during expose event handling that
26445 overlap other rows. Redrawing of an exposed line writes over parts
26446 of lines overlapping that exposed line; this function fixes that.
26447
26448 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26449 row in W's current matrix that is exposed and overlaps other rows.
26450 LAST_OVERLAPPING_ROW is the last such row. */
26451
26452 static void
26453 expose_overlaps (struct window *w,
26454 struct glyph_row *first_overlapping_row,
26455 struct glyph_row *last_overlapping_row,
26456 XRectangle *r)
26457 {
26458 struct glyph_row *row;
26459
26460 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26461 if (row->overlapping_p)
26462 {
26463 xassert (row->enabled_p && !row->mode_line_p);
26464
26465 row->clip = r;
26466 if (row->used[LEFT_MARGIN_AREA])
26467 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26468
26469 if (row->used[TEXT_AREA])
26470 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26471
26472 if (row->used[RIGHT_MARGIN_AREA])
26473 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26474 row->clip = NULL;
26475 }
26476 }
26477
26478
26479 /* Return non-zero if W's cursor intersects rectangle R. */
26480
26481 static int
26482 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26483 {
26484 XRectangle cr, result;
26485 struct glyph *cursor_glyph;
26486 struct glyph_row *row;
26487
26488 if (w->phys_cursor.vpos >= 0
26489 && w->phys_cursor.vpos < w->current_matrix->nrows
26490 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26491 row->enabled_p)
26492 && row->cursor_in_fringe_p)
26493 {
26494 /* Cursor is in the fringe. */
26495 cr.x = window_box_right_offset (w,
26496 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26497 ? RIGHT_MARGIN_AREA
26498 : TEXT_AREA));
26499 cr.y = row->y;
26500 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26501 cr.height = row->height;
26502 return x_intersect_rectangles (&cr, r, &result);
26503 }
26504
26505 cursor_glyph = get_phys_cursor_glyph (w);
26506 if (cursor_glyph)
26507 {
26508 /* r is relative to W's box, but w->phys_cursor.x is relative
26509 to left edge of W's TEXT area. Adjust it. */
26510 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26511 cr.y = w->phys_cursor.y;
26512 cr.width = cursor_glyph->pixel_width;
26513 cr.height = w->phys_cursor_height;
26514 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26515 I assume the effect is the same -- and this is portable. */
26516 return x_intersect_rectangles (&cr, r, &result);
26517 }
26518 /* If we don't understand the format, pretend we're not in the hot-spot. */
26519 return 0;
26520 }
26521
26522
26523 /* EXPORT:
26524 Draw a vertical window border to the right of window W if W doesn't
26525 have vertical scroll bars. */
26526
26527 void
26528 x_draw_vertical_border (struct window *w)
26529 {
26530 struct frame *f = XFRAME (WINDOW_FRAME (w));
26531
26532 /* We could do better, if we knew what type of scroll-bar the adjacent
26533 windows (on either side) have... But we don't :-(
26534 However, I think this works ok. ++KFS 2003-04-25 */
26535
26536 /* Redraw borders between horizontally adjacent windows. Don't
26537 do it for frames with vertical scroll bars because either the
26538 right scroll bar of a window, or the left scroll bar of its
26539 neighbor will suffice as a border. */
26540 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26541 return;
26542
26543 if (!WINDOW_RIGHTMOST_P (w)
26544 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26545 {
26546 int x0, x1, y0, y1;
26547
26548 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26549 y1 -= 1;
26550
26551 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26552 x1 -= 1;
26553
26554 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26555 }
26556 else if (!WINDOW_LEFTMOST_P (w)
26557 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26558 {
26559 int x0, x1, y0, y1;
26560
26561 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26562 y1 -= 1;
26563
26564 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26565 x0 -= 1;
26566
26567 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26568 }
26569 }
26570
26571
26572 /* Redraw the part of window W intersection rectangle FR. Pixel
26573 coordinates in FR are frame-relative. Call this function with
26574 input blocked. Value is non-zero if the exposure overwrites
26575 mouse-face. */
26576
26577 static int
26578 expose_window (struct window *w, XRectangle *fr)
26579 {
26580 struct frame *f = XFRAME (w->frame);
26581 XRectangle wr, r;
26582 int mouse_face_overwritten_p = 0;
26583
26584 /* If window is not yet fully initialized, do nothing. This can
26585 happen when toolkit scroll bars are used and a window is split.
26586 Reconfiguring the scroll bar will generate an expose for a newly
26587 created window. */
26588 if (w->current_matrix == NULL)
26589 return 0;
26590
26591 /* When we're currently updating the window, display and current
26592 matrix usually don't agree. Arrange for a thorough display
26593 later. */
26594 if (w == updated_window)
26595 {
26596 SET_FRAME_GARBAGED (f);
26597 return 0;
26598 }
26599
26600 /* Frame-relative pixel rectangle of W. */
26601 wr.x = WINDOW_LEFT_EDGE_X (w);
26602 wr.y = WINDOW_TOP_EDGE_Y (w);
26603 wr.width = WINDOW_TOTAL_WIDTH (w);
26604 wr.height = WINDOW_TOTAL_HEIGHT (w);
26605
26606 if (x_intersect_rectangles (fr, &wr, &r))
26607 {
26608 int yb = window_text_bottom_y (w);
26609 struct glyph_row *row;
26610 int cursor_cleared_p;
26611 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26612
26613 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26614 r.x, r.y, r.width, r.height));
26615
26616 /* Convert to window coordinates. */
26617 r.x -= WINDOW_LEFT_EDGE_X (w);
26618 r.y -= WINDOW_TOP_EDGE_Y (w);
26619
26620 /* Turn off the cursor. */
26621 if (!w->pseudo_window_p
26622 && phys_cursor_in_rect_p (w, &r))
26623 {
26624 x_clear_cursor (w);
26625 cursor_cleared_p = 1;
26626 }
26627 else
26628 cursor_cleared_p = 0;
26629
26630 /* Update lines intersecting rectangle R. */
26631 first_overlapping_row = last_overlapping_row = NULL;
26632 for (row = w->current_matrix->rows;
26633 row->enabled_p;
26634 ++row)
26635 {
26636 int y0 = row->y;
26637 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26638
26639 if ((y0 >= r.y && y0 < r.y + r.height)
26640 || (y1 > r.y && y1 < r.y + r.height)
26641 || (r.y >= y0 && r.y < y1)
26642 || (r.y + r.height > y0 && r.y + r.height < y1))
26643 {
26644 /* A header line may be overlapping, but there is no need
26645 to fix overlapping areas for them. KFS 2005-02-12 */
26646 if (row->overlapping_p && !row->mode_line_p)
26647 {
26648 if (first_overlapping_row == NULL)
26649 first_overlapping_row = row;
26650 last_overlapping_row = row;
26651 }
26652
26653 row->clip = fr;
26654 if (expose_line (w, row, &r))
26655 mouse_face_overwritten_p = 1;
26656 row->clip = NULL;
26657 }
26658 else if (row->overlapping_p)
26659 {
26660 /* We must redraw a row overlapping the exposed area. */
26661 if (y0 < r.y
26662 ? y0 + row->phys_height > r.y
26663 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26664 {
26665 if (first_overlapping_row == NULL)
26666 first_overlapping_row = row;
26667 last_overlapping_row = row;
26668 }
26669 }
26670
26671 if (y1 >= yb)
26672 break;
26673 }
26674
26675 /* Display the mode line if there is one. */
26676 if (WINDOW_WANTS_MODELINE_P (w)
26677 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26678 row->enabled_p)
26679 && row->y < r.y + r.height)
26680 {
26681 if (expose_line (w, row, &r))
26682 mouse_face_overwritten_p = 1;
26683 }
26684
26685 if (!w->pseudo_window_p)
26686 {
26687 /* Fix the display of overlapping rows. */
26688 if (first_overlapping_row)
26689 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26690 fr);
26691
26692 /* Draw border between windows. */
26693 x_draw_vertical_border (w);
26694
26695 /* Turn the cursor on again. */
26696 if (cursor_cleared_p)
26697 update_window_cursor (w, 1);
26698 }
26699 }
26700
26701 return mouse_face_overwritten_p;
26702 }
26703
26704
26705
26706 /* Redraw (parts) of all windows in the window tree rooted at W that
26707 intersect R. R contains frame pixel coordinates. Value is
26708 non-zero if the exposure overwrites mouse-face. */
26709
26710 static int
26711 expose_window_tree (struct window *w, XRectangle *r)
26712 {
26713 struct frame *f = XFRAME (w->frame);
26714 int mouse_face_overwritten_p = 0;
26715
26716 while (w && !FRAME_GARBAGED_P (f))
26717 {
26718 if (!NILP (w->hchild))
26719 mouse_face_overwritten_p
26720 |= expose_window_tree (XWINDOW (w->hchild), r);
26721 else if (!NILP (w->vchild))
26722 mouse_face_overwritten_p
26723 |= expose_window_tree (XWINDOW (w->vchild), r);
26724 else
26725 mouse_face_overwritten_p |= expose_window (w, r);
26726
26727 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26728 }
26729
26730 return mouse_face_overwritten_p;
26731 }
26732
26733
26734 /* EXPORT:
26735 Redisplay an exposed area of frame F. X and Y are the upper-left
26736 corner of the exposed rectangle. W and H are width and height of
26737 the exposed area. All are pixel values. W or H zero means redraw
26738 the entire frame. */
26739
26740 void
26741 expose_frame (struct frame *f, int x, int y, int w, int h)
26742 {
26743 XRectangle r;
26744 int mouse_face_overwritten_p = 0;
26745
26746 TRACE ((stderr, "expose_frame "));
26747
26748 /* No need to redraw if frame will be redrawn soon. */
26749 if (FRAME_GARBAGED_P (f))
26750 {
26751 TRACE ((stderr, " garbaged\n"));
26752 return;
26753 }
26754
26755 /* If basic faces haven't been realized yet, there is no point in
26756 trying to redraw anything. This can happen when we get an expose
26757 event while Emacs is starting, e.g. by moving another window. */
26758 if (FRAME_FACE_CACHE (f) == NULL
26759 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26760 {
26761 TRACE ((stderr, " no faces\n"));
26762 return;
26763 }
26764
26765 if (w == 0 || h == 0)
26766 {
26767 r.x = r.y = 0;
26768 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26769 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26770 }
26771 else
26772 {
26773 r.x = x;
26774 r.y = y;
26775 r.width = w;
26776 r.height = h;
26777 }
26778
26779 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26780 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26781
26782 if (WINDOWP (f->tool_bar_window))
26783 mouse_face_overwritten_p
26784 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26785
26786 #ifdef HAVE_X_WINDOWS
26787 #ifndef MSDOS
26788 #ifndef USE_X_TOOLKIT
26789 if (WINDOWP (f->menu_bar_window))
26790 mouse_face_overwritten_p
26791 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26792 #endif /* not USE_X_TOOLKIT */
26793 #endif
26794 #endif
26795
26796 /* Some window managers support a focus-follows-mouse style with
26797 delayed raising of frames. Imagine a partially obscured frame,
26798 and moving the mouse into partially obscured mouse-face on that
26799 frame. The visible part of the mouse-face will be highlighted,
26800 then the WM raises the obscured frame. With at least one WM, KDE
26801 2.1, Emacs is not getting any event for the raising of the frame
26802 (even tried with SubstructureRedirectMask), only Expose events.
26803 These expose events will draw text normally, i.e. not
26804 highlighted. Which means we must redo the highlight here.
26805 Subsume it under ``we love X''. --gerd 2001-08-15 */
26806 /* Included in Windows version because Windows most likely does not
26807 do the right thing if any third party tool offers
26808 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26809 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26810 {
26811 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26812 if (f == hlinfo->mouse_face_mouse_frame)
26813 {
26814 int mouse_x = hlinfo->mouse_face_mouse_x;
26815 int mouse_y = hlinfo->mouse_face_mouse_y;
26816 clear_mouse_face (hlinfo);
26817 note_mouse_highlight (f, mouse_x, mouse_y);
26818 }
26819 }
26820 }
26821
26822
26823 /* EXPORT:
26824 Determine the intersection of two rectangles R1 and R2. Return
26825 the intersection in *RESULT. Value is non-zero if RESULT is not
26826 empty. */
26827
26828 int
26829 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26830 {
26831 XRectangle *left, *right;
26832 XRectangle *upper, *lower;
26833 int intersection_p = 0;
26834
26835 /* Rearrange so that R1 is the left-most rectangle. */
26836 if (r1->x < r2->x)
26837 left = r1, right = r2;
26838 else
26839 left = r2, right = r1;
26840
26841 /* X0 of the intersection is right.x0, if this is inside R1,
26842 otherwise there is no intersection. */
26843 if (right->x <= left->x + left->width)
26844 {
26845 result->x = right->x;
26846
26847 /* The right end of the intersection is the minimum of the
26848 the right ends of left and right. */
26849 result->width = (min (left->x + left->width, right->x + right->width)
26850 - result->x);
26851
26852 /* Same game for Y. */
26853 if (r1->y < r2->y)
26854 upper = r1, lower = r2;
26855 else
26856 upper = r2, lower = r1;
26857
26858 /* The upper end of the intersection is lower.y0, if this is inside
26859 of upper. Otherwise, there is no intersection. */
26860 if (lower->y <= upper->y + upper->height)
26861 {
26862 result->y = lower->y;
26863
26864 /* The lower end of the intersection is the minimum of the lower
26865 ends of upper and lower. */
26866 result->height = (min (lower->y + lower->height,
26867 upper->y + upper->height)
26868 - result->y);
26869 intersection_p = 1;
26870 }
26871 }
26872
26873 return intersection_p;
26874 }
26875
26876 #endif /* HAVE_WINDOW_SYSTEM */
26877
26878 \f
26879 /***********************************************************************
26880 Initialization
26881 ***********************************************************************/
26882
26883 void
26884 syms_of_xdisp (void)
26885 {
26886 Vwith_echo_area_save_vector = Qnil;
26887 staticpro (&Vwith_echo_area_save_vector);
26888
26889 Vmessage_stack = Qnil;
26890 staticpro (&Vmessage_stack);
26891
26892 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26893 staticpro (&Qinhibit_redisplay);
26894
26895 message_dolog_marker1 = Fmake_marker ();
26896 staticpro (&message_dolog_marker1);
26897 message_dolog_marker2 = Fmake_marker ();
26898 staticpro (&message_dolog_marker2);
26899 message_dolog_marker3 = Fmake_marker ();
26900 staticpro (&message_dolog_marker3);
26901
26902 #if GLYPH_DEBUG
26903 defsubr (&Sdump_frame_glyph_matrix);
26904 defsubr (&Sdump_glyph_matrix);
26905 defsubr (&Sdump_glyph_row);
26906 defsubr (&Sdump_tool_bar_row);
26907 defsubr (&Strace_redisplay);
26908 defsubr (&Strace_to_stderr);
26909 #endif
26910 #ifdef HAVE_WINDOW_SYSTEM
26911 defsubr (&Stool_bar_lines_needed);
26912 defsubr (&Slookup_image_map);
26913 #endif
26914 defsubr (&Sformat_mode_line);
26915 defsubr (&Sinvisible_p);
26916 defsubr (&Scurrent_bidi_paragraph_direction);
26917
26918 staticpro (&Qmenu_bar_update_hook);
26919 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26920
26921 staticpro (&Qoverriding_terminal_local_map);
26922 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26923
26924 staticpro (&Qoverriding_local_map);
26925 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26926
26927 staticpro (&Qwindow_scroll_functions);
26928 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26929
26930 staticpro (&Qwindow_text_change_functions);
26931 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26932
26933 staticpro (&Qredisplay_end_trigger_functions);
26934 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26935
26936 staticpro (&Qinhibit_point_motion_hooks);
26937 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26938
26939 Qeval = intern_c_string ("eval");
26940 staticpro (&Qeval);
26941
26942 QCdata = intern_c_string (":data");
26943 staticpro (&QCdata);
26944 Qdisplay = intern_c_string ("display");
26945 staticpro (&Qdisplay);
26946 Qspace_width = intern_c_string ("space-width");
26947 staticpro (&Qspace_width);
26948 Qraise = intern_c_string ("raise");
26949 staticpro (&Qraise);
26950 Qslice = intern_c_string ("slice");
26951 staticpro (&Qslice);
26952 Qspace = intern_c_string ("space");
26953 staticpro (&Qspace);
26954 Qmargin = intern_c_string ("margin");
26955 staticpro (&Qmargin);
26956 Qpointer = intern_c_string ("pointer");
26957 staticpro (&Qpointer);
26958 Qleft_margin = intern_c_string ("left-margin");
26959 staticpro (&Qleft_margin);
26960 Qright_margin = intern_c_string ("right-margin");
26961 staticpro (&Qright_margin);
26962 Qcenter = intern_c_string ("center");
26963 staticpro (&Qcenter);
26964 Qline_height = intern_c_string ("line-height");
26965 staticpro (&Qline_height);
26966 QCalign_to = intern_c_string (":align-to");
26967 staticpro (&QCalign_to);
26968 QCrelative_width = intern_c_string (":relative-width");
26969 staticpro (&QCrelative_width);
26970 QCrelative_height = intern_c_string (":relative-height");
26971 staticpro (&QCrelative_height);
26972 QCeval = intern_c_string (":eval");
26973 staticpro (&QCeval);
26974 QCpropertize = intern_c_string (":propertize");
26975 staticpro (&QCpropertize);
26976 QCfile = intern_c_string (":file");
26977 staticpro (&QCfile);
26978 Qfontified = intern_c_string ("fontified");
26979 staticpro (&Qfontified);
26980 Qfontification_functions = intern_c_string ("fontification-functions");
26981 staticpro (&Qfontification_functions);
26982 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26983 staticpro (&Qtrailing_whitespace);
26984 Qescape_glyph = intern_c_string ("escape-glyph");
26985 staticpro (&Qescape_glyph);
26986 Qnobreak_space = intern_c_string ("nobreak-space");
26987 staticpro (&Qnobreak_space);
26988 Qimage = intern_c_string ("image");
26989 staticpro (&Qimage);
26990 Qtext = intern_c_string ("text");
26991 staticpro (&Qtext);
26992 Qboth = intern_c_string ("both");
26993 staticpro (&Qboth);
26994 Qboth_horiz = intern_c_string ("both-horiz");
26995 staticpro (&Qboth_horiz);
26996 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26997 staticpro (&Qtext_image_horiz);
26998 QCmap = intern_c_string (":map");
26999 staticpro (&QCmap);
27000 QCpointer = intern_c_string (":pointer");
27001 staticpro (&QCpointer);
27002 Qrect = intern_c_string ("rect");
27003 staticpro (&Qrect);
27004 Qcircle = intern_c_string ("circle");
27005 staticpro (&Qcircle);
27006 Qpoly = intern_c_string ("poly");
27007 staticpro (&Qpoly);
27008 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
27009 staticpro (&Qmessage_truncate_lines);
27010 Qgrow_only = intern_c_string ("grow-only");
27011 staticpro (&Qgrow_only);
27012 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
27013 staticpro (&Qinhibit_menubar_update);
27014 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
27015 staticpro (&Qinhibit_eval_during_redisplay);
27016 Qposition = intern_c_string ("position");
27017 staticpro (&Qposition);
27018 Qbuffer_position = intern_c_string ("buffer-position");
27019 staticpro (&Qbuffer_position);
27020 Qobject = intern_c_string ("object");
27021 staticpro (&Qobject);
27022 Qbar = intern_c_string ("bar");
27023 staticpro (&Qbar);
27024 Qhbar = intern_c_string ("hbar");
27025 staticpro (&Qhbar);
27026 Qbox = intern_c_string ("box");
27027 staticpro (&Qbox);
27028 Qhollow = intern_c_string ("hollow");
27029 staticpro (&Qhollow);
27030 Qhand = intern_c_string ("hand");
27031 staticpro (&Qhand);
27032 Qarrow = intern_c_string ("arrow");
27033 staticpro (&Qarrow);
27034 Qtext = intern_c_string ("text");
27035 staticpro (&Qtext);
27036 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
27037 staticpro (&Qinhibit_free_realized_faces);
27038
27039 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27040 Fcons (intern_c_string ("void-variable"), Qnil)),
27041 Qnil);
27042 staticpro (&list_of_error);
27043
27044 Qlast_arrow_position = intern_c_string ("last-arrow-position");
27045 staticpro (&Qlast_arrow_position);
27046 Qlast_arrow_string = intern_c_string ("last-arrow-string");
27047 staticpro (&Qlast_arrow_string);
27048
27049 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
27050 staticpro (&Qoverlay_arrow_string);
27051 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
27052 staticpro (&Qoverlay_arrow_bitmap);
27053
27054 echo_buffer[0] = echo_buffer[1] = Qnil;
27055 staticpro (&echo_buffer[0]);
27056 staticpro (&echo_buffer[1]);
27057
27058 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27059 staticpro (&echo_area_buffer[0]);
27060 staticpro (&echo_area_buffer[1]);
27061
27062 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27063 staticpro (&Vmessages_buffer_name);
27064
27065 mode_line_proptrans_alist = Qnil;
27066 staticpro (&mode_line_proptrans_alist);
27067 mode_line_string_list = Qnil;
27068 staticpro (&mode_line_string_list);
27069 mode_line_string_face = Qnil;
27070 staticpro (&mode_line_string_face);
27071 mode_line_string_face_prop = Qnil;
27072 staticpro (&mode_line_string_face_prop);
27073 Vmode_line_unwind_vector = Qnil;
27074 staticpro (&Vmode_line_unwind_vector);
27075
27076 help_echo_string = Qnil;
27077 staticpro (&help_echo_string);
27078 help_echo_object = Qnil;
27079 staticpro (&help_echo_object);
27080 help_echo_window = Qnil;
27081 staticpro (&help_echo_window);
27082 previous_help_echo_string = Qnil;
27083 staticpro (&previous_help_echo_string);
27084 help_echo_pos = -1;
27085
27086 Qright_to_left = intern_c_string ("right-to-left");
27087 staticpro (&Qright_to_left);
27088 Qleft_to_right = intern_c_string ("left-to-right");
27089 staticpro (&Qleft_to_right);
27090
27091 #ifdef HAVE_WINDOW_SYSTEM
27092 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27093 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27094 For example, if a block cursor is over a tab, it will be drawn as
27095 wide as that tab on the display. */);
27096 x_stretch_cursor_p = 0;
27097 #endif
27098
27099 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27100 doc: /* *Non-nil means highlight trailing whitespace.
27101 The face used for trailing whitespace is `trailing-whitespace'. */);
27102 Vshow_trailing_whitespace = Qnil;
27103
27104 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27105 doc: /* *Control highlighting of nobreak space and soft hyphen.
27106 A value of t means highlight the character itself (for nobreak space,
27107 use face `nobreak-space').
27108 A value of nil means no highlighting.
27109 Other values mean display the escape glyph followed by an ordinary
27110 space or ordinary hyphen. */);
27111 Vnobreak_char_display = Qt;
27112
27113 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27114 doc: /* *The pointer shape to show in void text areas.
27115 A value of nil means to show the text pointer. Other options are `arrow',
27116 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27117 Vvoid_text_area_pointer = Qarrow;
27118
27119 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27120 doc: /* Non-nil means don't actually do any redisplay.
27121 This is used for internal purposes. */);
27122 Vinhibit_redisplay = Qnil;
27123
27124 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27125 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27126 Vglobal_mode_string = Qnil;
27127
27128 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27129 doc: /* Marker for where to display an arrow on top of the buffer text.
27130 This must be the beginning of a line in order to work.
27131 See also `overlay-arrow-string'. */);
27132 Voverlay_arrow_position = Qnil;
27133
27134 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27135 doc: /* String to display as an arrow in non-window frames.
27136 See also `overlay-arrow-position'. */);
27137 Voverlay_arrow_string = make_pure_c_string ("=>");
27138
27139 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27140 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27141 The symbols on this list are examined during redisplay to determine
27142 where to display overlay arrows. */);
27143 Voverlay_arrow_variable_list
27144 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27145
27146 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27147 doc: /* *The number of lines to try scrolling a window by when point moves out.
27148 If that fails to bring point back on frame, point is centered instead.
27149 If this is zero, point is always centered after it moves off frame.
27150 If you want scrolling to always be a line at a time, you should set
27151 `scroll-conservatively' to a large value rather than set this to 1. */);
27152
27153 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27154 doc: /* *Scroll up to this many lines, to bring point back on screen.
27155 If point moves off-screen, redisplay will scroll by up to
27156 `scroll-conservatively' lines in order to bring point just barely
27157 onto the screen again. If that cannot be done, then redisplay
27158 recenters point as usual.
27159
27160 If the value is greater than 100, redisplay will never recenter point,
27161 but will always scroll just enough text to bring point into view, even
27162 if you move far away.
27163
27164 A value of zero means always recenter point if it moves off screen. */);
27165 scroll_conservatively = 0;
27166
27167 DEFVAR_INT ("scroll-margin", scroll_margin,
27168 doc: /* *Number of lines of margin at the top and bottom of a window.
27169 Recenter the window whenever point gets within this many lines
27170 of the top or bottom of the window. */);
27171 scroll_margin = 0;
27172
27173 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27174 doc: /* Pixels per inch value for non-window system displays.
27175 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27176 Vdisplay_pixels_per_inch = make_float (72.0);
27177
27178 #if GLYPH_DEBUG
27179 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27180 #endif
27181
27182 DEFVAR_LISP ("truncate-partial-width-windows",
27183 Vtruncate_partial_width_windows,
27184 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27185 For an integer value, truncate lines in each window narrower than the
27186 full frame width, provided the window width is less than that integer;
27187 otherwise, respect the value of `truncate-lines'.
27188
27189 For any other non-nil value, truncate lines in all windows that do
27190 not span the full frame width.
27191
27192 A value of nil means to respect the value of `truncate-lines'.
27193
27194 If `word-wrap' is enabled, you might want to reduce this. */);
27195 Vtruncate_partial_width_windows = make_number (50);
27196
27197 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27198 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27199 Any other value means to use the appropriate face, `mode-line',
27200 `header-line', or `menu' respectively. */);
27201 mode_line_inverse_video = 1;
27202
27203 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27204 doc: /* *Maximum buffer size for which line number should be displayed.
27205 If the buffer is bigger than this, the line number does not appear
27206 in the mode line. A value of nil means no limit. */);
27207 Vline_number_display_limit = Qnil;
27208
27209 DEFVAR_INT ("line-number-display-limit-width",
27210 line_number_display_limit_width,
27211 doc: /* *Maximum line width (in characters) for line number display.
27212 If the average length of the lines near point is bigger than this, then the
27213 line number may be omitted from the mode line. */);
27214 line_number_display_limit_width = 200;
27215
27216 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27217 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27218 highlight_nonselected_windows = 0;
27219
27220 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27221 doc: /* Non-nil if more than one frame is visible on this display.
27222 Minibuffer-only frames don't count, but iconified frames do.
27223 This variable is not guaranteed to be accurate except while processing
27224 `frame-title-format' and `icon-title-format'. */);
27225
27226 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27227 doc: /* Template for displaying the title bar of visible frames.
27228 \(Assuming the window manager supports this feature.)
27229
27230 This variable has the same structure as `mode-line-format', except that
27231 the %c and %l constructs are ignored. It is used only on frames for
27232 which no explicit name has been set \(see `modify-frame-parameters'). */);
27233
27234 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27235 doc: /* Template for displaying the title bar of an iconified frame.
27236 \(Assuming the window manager supports this feature.)
27237 This variable has the same structure as `mode-line-format' (which see),
27238 and is used only on frames for which no explicit name has been set
27239 \(see `modify-frame-parameters'). */);
27240 Vicon_title_format
27241 = Vframe_title_format
27242 = pure_cons (intern_c_string ("multiple-frames"),
27243 pure_cons (make_pure_c_string ("%b"),
27244 pure_cons (pure_cons (empty_unibyte_string,
27245 pure_cons (intern_c_string ("invocation-name"),
27246 pure_cons (make_pure_c_string ("@"),
27247 pure_cons (intern_c_string ("system-name"),
27248 Qnil)))),
27249 Qnil)));
27250
27251 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27252 doc: /* Maximum number of lines to keep in the message log buffer.
27253 If nil, disable message logging. If t, log messages but don't truncate
27254 the buffer when it becomes large. */);
27255 Vmessage_log_max = make_number (100);
27256
27257 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27258 doc: /* Functions called before redisplay, if window sizes have changed.
27259 The value should be a list of functions that take one argument.
27260 Just before redisplay, for each frame, if any of its windows have changed
27261 size since the last redisplay, or have been split or deleted,
27262 all the functions in the list are called, with the frame as argument. */);
27263 Vwindow_size_change_functions = Qnil;
27264
27265 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27266 doc: /* List of functions to call before redisplaying a window with scrolling.
27267 Each function is called with two arguments, the window and its new
27268 display-start position. Note that these functions are also called by
27269 `set-window-buffer'. Also note that the value of `window-end' is not
27270 valid when these functions are called. */);
27271 Vwindow_scroll_functions = Qnil;
27272
27273 DEFVAR_LISP ("window-text-change-functions",
27274 Vwindow_text_change_functions,
27275 doc: /* Functions to call in redisplay when text in the window might change. */);
27276 Vwindow_text_change_functions = Qnil;
27277
27278 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27279 doc: /* Functions called when redisplay of a window reaches the end trigger.
27280 Each function is called with two arguments, the window and the end trigger value.
27281 See `set-window-redisplay-end-trigger'. */);
27282 Vredisplay_end_trigger_functions = Qnil;
27283
27284 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27285 doc: /* *Non-nil means autoselect window with mouse pointer.
27286 If nil, do not autoselect windows.
27287 A positive number means delay autoselection by that many seconds: a
27288 window is autoselected only after the mouse has remained in that
27289 window for the duration of the delay.
27290 A negative number has a similar effect, but causes windows to be
27291 autoselected only after the mouse has stopped moving. \(Because of
27292 the way Emacs compares mouse events, you will occasionally wait twice
27293 that time before the window gets selected.\)
27294 Any other value means to autoselect window instantaneously when the
27295 mouse pointer enters it.
27296
27297 Autoselection selects the minibuffer only if it is active, and never
27298 unselects the minibuffer if it is active.
27299
27300 When customizing this variable make sure that the actual value of
27301 `focus-follows-mouse' matches the behavior of your window manager. */);
27302 Vmouse_autoselect_window = Qnil;
27303
27304 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27305 doc: /* *Non-nil means automatically resize tool-bars.
27306 This dynamically changes the tool-bar's height to the minimum height
27307 that is needed to make all tool-bar items visible.
27308 If value is `grow-only', the tool-bar's height is only increased
27309 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27310 Vauto_resize_tool_bars = Qt;
27311
27312 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27313 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27314 auto_raise_tool_bar_buttons_p = 1;
27315
27316 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27317 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27318 make_cursor_line_fully_visible_p = 1;
27319
27320 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27321 doc: /* *Border below tool-bar in pixels.
27322 If an integer, use it as the height of the border.
27323 If it is one of `internal-border-width' or `border-width', use the
27324 value of the corresponding frame parameter.
27325 Otherwise, no border is added below the tool-bar. */);
27326 Vtool_bar_border = Qinternal_border_width;
27327
27328 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27329 doc: /* *Margin around tool-bar buttons in pixels.
27330 If an integer, use that for both horizontal and vertical margins.
27331 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27332 HORZ specifying the horizontal margin, and VERT specifying the
27333 vertical margin. */);
27334 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27335
27336 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27337 doc: /* *Relief thickness of tool-bar buttons. */);
27338 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27339
27340 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27341 doc: /* Tool bar style to use.
27342 It can be one of
27343 image - show images only
27344 text - show text only
27345 both - show both, text below image
27346 both-horiz - show text to the right of the image
27347 text-image-horiz - show text to the left of the image
27348 any other - use system default or image if no system default. */);
27349 Vtool_bar_style = Qnil;
27350
27351 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27352 doc: /* *Maximum number of characters a label can have to be shown.
27353 The tool bar style must also show labels for this to have any effect, see
27354 `tool-bar-style'. */);
27355 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27356
27357 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27358 doc: /* List of functions to call to fontify regions of text.
27359 Each function is called with one argument POS. Functions must
27360 fontify a region starting at POS in the current buffer, and give
27361 fontified regions the property `fontified'. */);
27362 Vfontification_functions = Qnil;
27363 Fmake_variable_buffer_local (Qfontification_functions);
27364
27365 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27366 unibyte_display_via_language_environment,
27367 doc: /* *Non-nil means display unibyte text according to language environment.
27368 Specifically, this means that raw bytes in the range 160-255 decimal
27369 are displayed by converting them to the equivalent multibyte characters
27370 according to the current language environment. As a result, they are
27371 displayed according to the current fontset.
27372
27373 Note that this variable affects only how these bytes are displayed,
27374 but does not change the fact they are interpreted as raw bytes. */);
27375 unibyte_display_via_language_environment = 0;
27376
27377 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27378 doc: /* *Maximum height for resizing mini-windows.
27379 If a float, it specifies a fraction of the mini-window frame's height.
27380 If an integer, it specifies a number of lines. */);
27381 Vmax_mini_window_height = make_float (0.25);
27382
27383 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27384 doc: /* *How to resize mini-windows.
27385 A value of nil means don't automatically resize mini-windows.
27386 A value of t means resize them to fit the text displayed in them.
27387 A value of `grow-only', the default, means let mini-windows grow
27388 only, until their display becomes empty, at which point the windows
27389 go back to their normal size. */);
27390 Vresize_mini_windows = Qgrow_only;
27391
27392 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27393 doc: /* Alist specifying how to blink the cursor off.
27394 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27395 `cursor-type' frame-parameter or variable equals ON-STATE,
27396 comparing using `equal', Emacs uses OFF-STATE to specify
27397 how to blink it off. ON-STATE and OFF-STATE are values for
27398 the `cursor-type' frame parameter.
27399
27400 If a frame's ON-STATE has no entry in this list,
27401 the frame's other specifications determine how to blink the cursor off. */);
27402 Vblink_cursor_alist = Qnil;
27403
27404 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27405 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27406 If non-nil, windows are automatically scrolled horizontally to make
27407 point visible. */);
27408 automatic_hscrolling_p = 1;
27409 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
27410 staticpro (&Qauto_hscroll_mode);
27411
27412 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27413 doc: /* *How many columns away from the window edge point is allowed to get
27414 before automatic hscrolling will horizontally scroll the window. */);
27415 hscroll_margin = 5;
27416
27417 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27418 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27419 When point is less than `hscroll-margin' columns from the window
27420 edge, automatic hscrolling will scroll the window by the amount of columns
27421 determined by this variable. If its value is a positive integer, scroll that
27422 many columns. If it's a positive floating-point number, it specifies the
27423 fraction of the window's width to scroll. If it's nil or zero, point will be
27424 centered horizontally after the scroll. Any other value, including negative
27425 numbers, are treated as if the value were zero.
27426
27427 Automatic hscrolling always moves point outside the scroll margin, so if
27428 point was more than scroll step columns inside the margin, the window will
27429 scroll more than the value given by the scroll step.
27430
27431 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27432 and `scroll-right' overrides this variable's effect. */);
27433 Vhscroll_step = make_number (0);
27434
27435 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27436 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27437 Bind this around calls to `message' to let it take effect. */);
27438 message_truncate_lines = 0;
27439
27440 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27441 doc: /* Normal hook run to update the menu bar definitions.
27442 Redisplay runs this hook before it redisplays the menu bar.
27443 This is used to update submenus such as Buffers,
27444 whose contents depend on various data. */);
27445 Vmenu_bar_update_hook = Qnil;
27446
27447 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27448 doc: /* Frame for which we are updating a menu.
27449 The enable predicate for a menu binding should check this variable. */);
27450 Vmenu_updating_frame = Qnil;
27451
27452 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27453 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27454 inhibit_menubar_update = 0;
27455
27456 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27457 doc: /* Prefix prepended to all continuation lines at display time.
27458 The value may be a string, an image, or a stretch-glyph; it is
27459 interpreted in the same way as the value of a `display' text property.
27460
27461 This variable is overridden by any `wrap-prefix' text or overlay
27462 property.
27463
27464 To add a prefix to non-continuation lines, use `line-prefix'. */);
27465 Vwrap_prefix = Qnil;
27466 staticpro (&Qwrap_prefix);
27467 Qwrap_prefix = intern_c_string ("wrap-prefix");
27468 Fmake_variable_buffer_local (Qwrap_prefix);
27469
27470 DEFVAR_LISP ("line-prefix", Vline_prefix,
27471 doc: /* Prefix prepended to all non-continuation lines at display time.
27472 The value may be a string, an image, or a stretch-glyph; it is
27473 interpreted in the same way as the value of a `display' text property.
27474
27475 This variable is overridden by any `line-prefix' text or overlay
27476 property.
27477
27478 To add a prefix to continuation lines, use `wrap-prefix'. */);
27479 Vline_prefix = Qnil;
27480 staticpro (&Qline_prefix);
27481 Qline_prefix = intern_c_string ("line-prefix");
27482 Fmake_variable_buffer_local (Qline_prefix);
27483
27484 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27485 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27486 inhibit_eval_during_redisplay = 0;
27487
27488 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27489 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27490 inhibit_free_realized_faces = 0;
27491
27492 #if GLYPH_DEBUG
27493 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27494 doc: /* Inhibit try_window_id display optimization. */);
27495 inhibit_try_window_id = 0;
27496
27497 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27498 doc: /* Inhibit try_window_reusing display optimization. */);
27499 inhibit_try_window_reusing = 0;
27500
27501 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27502 doc: /* Inhibit try_cursor_movement display optimization. */);
27503 inhibit_try_cursor_movement = 0;
27504 #endif /* GLYPH_DEBUG */
27505
27506 DEFVAR_INT ("overline-margin", overline_margin,
27507 doc: /* *Space between overline and text, in pixels.
27508 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27509 margin to the caracter height. */);
27510 overline_margin = 2;
27511
27512 DEFVAR_INT ("underline-minimum-offset",
27513 underline_minimum_offset,
27514 doc: /* Minimum distance between baseline and underline.
27515 This can improve legibility of underlined text at small font sizes,
27516 particularly when using variable `x-use-underline-position-properties'
27517 with fonts that specify an UNDERLINE_POSITION relatively close to the
27518 baseline. The default value is 1. */);
27519 underline_minimum_offset = 1;
27520
27521 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27522 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27523 This feature only works when on a window system that can change
27524 cursor shapes. */);
27525 display_hourglass_p = 1;
27526
27527 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27528 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27529 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27530
27531 hourglass_atimer = NULL;
27532 hourglass_shown_p = 0;
27533
27534 DEFSYM (Qglyphless_char, "glyphless-char");
27535 DEFSYM (Qhex_code, "hex-code");
27536 DEFSYM (Qempty_box, "empty-box");
27537 DEFSYM (Qthin_space, "thin-space");
27538 DEFSYM (Qzero_width, "zero-width");
27539
27540 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27541 /* Intern this now in case it isn't already done.
27542 Setting this variable twice is harmless.
27543 But don't staticpro it here--that is done in alloc.c. */
27544 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27545 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27546
27547 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27548 doc: /* Char-table defining glyphless characters.
27549 Each element, if non-nil, should be one of the following:
27550 an ASCII acronym string: display this string in a box
27551 `hex-code': display the hexadecimal code of a character in a box
27552 `empty-box': display as an empty box
27553 `thin-space': display as 1-pixel width space
27554 `zero-width': don't display
27555 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27556 display method for graphical terminals and text terminals respectively.
27557 GRAPHICAL and TEXT should each have one of the values listed above.
27558
27559 The char-table has one extra slot to control the display of a character for
27560 which no font is found. This slot only takes effect on graphical terminals.
27561 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27562 `thin-space'. The default is `empty-box'. */);
27563 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27564 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27565 Qempty_box);
27566 }
27567
27568
27569 /* Initialize this module when Emacs starts. */
27570
27571 void
27572 init_xdisp (void)
27573 {
27574 Lisp_Object root_window;
27575 struct window *mini_w;
27576
27577 current_header_line_height = current_mode_line_height = -1;
27578
27579 CHARPOS (this_line_start_pos) = 0;
27580
27581 mini_w = XWINDOW (minibuf_window);
27582 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27583 echo_area_window = minibuf_window;
27584
27585 if (!noninteractive)
27586 {
27587 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27588 int i;
27589
27590 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27591 set_window_height (root_window,
27592 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27593 0);
27594 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27595 set_window_height (minibuf_window, 1, 0);
27596
27597 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27598 mini_w->total_cols = make_number (FRAME_COLS (f));
27599
27600 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27601 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27602 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27603
27604 /* The default ellipsis glyphs `...'. */
27605 for (i = 0; i < 3; ++i)
27606 default_invis_vector[i] = make_number ('.');
27607 }
27608
27609 {
27610 /* Allocate the buffer for frame titles.
27611 Also used for `format-mode-line'. */
27612 int size = 100;
27613 mode_line_noprop_buf = (char *) xmalloc (size);
27614 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27615 mode_line_noprop_ptr = mode_line_noprop_buf;
27616 mode_line_target = MODE_LINE_DISPLAY;
27617 }
27618
27619 help_echo_showing_p = 0;
27620 }
27621
27622 /* Since w32 does not support atimers, it defines its own implementation of
27623 the following three functions in w32fns.c. */
27624 #ifndef WINDOWSNT
27625
27626 /* Platform-independent portion of hourglass implementation. */
27627
27628 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27629 int
27630 hourglass_started (void)
27631 {
27632 return hourglass_shown_p || hourglass_atimer != NULL;
27633 }
27634
27635 /* Cancel a currently active hourglass timer, and start a new one. */
27636 void
27637 start_hourglass (void)
27638 {
27639 #if defined (HAVE_WINDOW_SYSTEM)
27640 EMACS_TIME delay;
27641 int secs, usecs = 0;
27642
27643 cancel_hourglass ();
27644
27645 if (INTEGERP (Vhourglass_delay)
27646 && XINT (Vhourglass_delay) > 0)
27647 secs = XFASTINT (Vhourglass_delay);
27648 else if (FLOATP (Vhourglass_delay)
27649 && XFLOAT_DATA (Vhourglass_delay) > 0)
27650 {
27651 Lisp_Object tem;
27652 tem = Ftruncate (Vhourglass_delay, Qnil);
27653 secs = XFASTINT (tem);
27654 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27655 }
27656 else
27657 secs = DEFAULT_HOURGLASS_DELAY;
27658
27659 EMACS_SET_SECS_USECS (delay, secs, usecs);
27660 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27661 show_hourglass, NULL);
27662 #endif
27663 }
27664
27665
27666 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27667 shown. */
27668 void
27669 cancel_hourglass (void)
27670 {
27671 #if defined (HAVE_WINDOW_SYSTEM)
27672 if (hourglass_atimer)
27673 {
27674 cancel_atimer (hourglass_atimer);
27675 hourglass_atimer = NULL;
27676 }
27677
27678 if (hourglass_shown_p)
27679 hide_hourglass ();
27680 #endif
27681 }
27682 #endif /* ! WINDOWSNT */