Text covered by `display' overlays is correctly reordered.
[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 Calls to get_next_display_element fill the iterator structure with
134 relevant information about the next thing to display. Calls to
135 set_iterator_to_next move the iterator to the next thing.
136
137 Besides this, an iterator also contains information about the
138 display environment in which glyphs for display elements are to be
139 produced. It has fields for the width and height of the display,
140 the information whether long lines are truncated or continued, a
141 current X and Y position, and lots of other stuff you can better
142 see in dispextern.h.
143
144 Glyphs in a desired matrix are normally constructed in a loop
145 calling get_next_display_element and then PRODUCE_GLYPHS. The call
146 to PRODUCE_GLYPHS will fill the iterator structure with pixel
147 information about the element being displayed and at the same time
148 produce glyphs for it. If the display element fits on the line
149 being displayed, set_iterator_to_next is called next, otherwise the
150 glyphs produced are discarded. The function display_line is the
151 workhorse of filling glyph rows in the desired matrix with glyphs.
152 In addition to producing glyphs, it also handles line truncation
153 and continuation, word wrap, and cursor positioning (for the
154 latter, see also set_cursor_from_row).
155
156 Frame matrices.
157
158 That just couldn't be all, could it? What about terminal types not
159 supporting operations on sub-windows of the screen? To update the
160 display on such a terminal, window-based glyph matrices are not
161 well suited. To be able to reuse part of the display (scrolling
162 lines up and down), we must instead have a view of the whole
163 screen. This is what `frame matrices' are for. They are a trick.
164
165 Frames on terminals like above have a glyph pool. Windows on such
166 a frame sub-allocate their glyph memory from their frame's glyph
167 pool. The frame itself is given its own glyph matrices. By
168 coincidence---or maybe something else---rows in window glyph
169 matrices are slices of corresponding rows in frame matrices. Thus
170 writing to window matrices implicitly updates a frame matrix which
171 provides us with the view of the whole screen that we originally
172 wanted to have without having to move many bytes around. To be
173 honest, there is a little bit more done, but not much more. If you
174 plan to extend that code, take a look at dispnew.c. The function
175 build_frame_matrix is a good starting point.
176
177 Bidirectional display.
178
179 Bidirectional display adds quite some hair to this already complex
180 design. The good news are that a large portion of that hairy stuff
181 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
182 reordering engine which is called by set_iterator_to_next and
183 returns the next character to display in the visual order. See
184 commentary on bidi.c for more details. As far as redisplay is
185 concerned, the effect of calling bidi_move_to_visually_next, the
186 main interface of the reordering engine, is that the iterator gets
187 magically placed on the buffer or string position that is to be
188 displayed next. In other words, a linear iteration through the
189 buffer/string is replaced with a non-linear one. All the rest of
190 the redisplay is oblivious to the bidi reordering.
191
192 Well, almost oblivious---there are still complications, most of
193 them due to the fact that buffer and string positions no longer
194 change monotonously with glyph indices in a glyph row. Moreover,
195 for continued lines, the buffer positions may not even be
196 monotonously changing with vertical positions. Also, accounting
197 for face changes, overlays, etc. becomes more complex because
198 non-linear iteration could potentially skip many positions with
199 changes, and then cross them again on the way back...
200
201 One other prominent effect of bidirectional display is that some
202 paragraphs of text need to be displayed starting at the right
203 margin of the window---the so-called right-to-left, or R2L
204 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
205 which have their reversed_p flag set. The bidi reordering engine
206 produces characters in such rows starting from the character which
207 should be the rightmost on display. PRODUCE_GLYPHS then reverses
208 the order, when it fills up the glyph row whose reversed_p flag is
209 set, by prepending each new glyph to what is already there, instead
210 of appending it. When the glyph row is complete, the function
211 extend_face_to_end_of_line fills the empty space to the left of the
212 leftmost character with special glyphs, which will display as,
213 well, empty. On text terminals, these special glyphs are simply
214 blank characters. On graphics terminals, there's a single stretch
215 glyph of a suitably computed width. Both the blanks and the
216 stretch glyph are given the face of the background of the line.
217 This way, the terminal-specific back-end can still draw the glyphs
218 left to right, even for R2L lines.
219
220 Bidirectional display and character compositions
221
222 Some scripts cannot be displayed by drawing each character
223 individually, because adjacent characters change each other's shape
224 on display. For example, Arabic and Indic scripts belong to this
225 category.
226
227 Emacs display supports this by providing "character compositions",
228 most of which is implemented in composite.c. During the buffer
229 scan that delivers characters to PRODUCE_GLYPHS, if the next
230 character to be delivered is a composed character, the iteration
231 calls composition_reseat_it and next_element_from_composition. If
232 they succeed to compose the character with one or more of the
233 following characters, the whole sequence of characters that where
234 composed is recorded in the `struct composition_it' object that is
235 part of the buffer iterator. The composed sequence could produce
236 one or more font glyphs (called "grapheme clusters") on the screen.
237 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
238 in the direction corresponding to the current bidi scan direction
239 (recorded in the scan_dir member of the `struct bidi_it' object
240 that is part of the buffer iterator). In particular, if the bidi
241 iterator currently scans the buffer backwards, the grapheme
242 clusters are delivered back to front. This reorders the grapheme
243 clusters as appropriate for the current bidi context. Note that
244 this means that the grapheme clusters are always stored in the
245 LGSTRING object (see composite.c) in the logical order.
246
247 Moving an iterator in bidirectional text
248 without producing glyphs
249
250 Note one important detail mentioned above: that the bidi reordering
251 engine, driven by the iterator, produces characters in R2L rows
252 starting at the character that will be the rightmost on display.
253 As far as the iterator is concerned, the geometry of such rows is
254 still left to right, i.e. the iterator "thinks" the first character
255 is at the leftmost pixel position. The iterator does not know that
256 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
257 delivers. This is important when functions from the the move_it_*
258 family are used to get to certain screen position or to match
259 screen coordinates with buffer coordinates: these functions use the
260 iterator geometry, which is left to right even in R2L paragraphs.
261 This works well with most callers of move_it_*, because they need
262 to get to a specific column, and columns are still numbered in the
263 reading order, i.e. the rightmost character in a R2L paragraph is
264 still column zero. But some callers do not get well with this; a
265 notable example is mouse clicks that need to find the character
266 that corresponds to certain pixel coordinates. See
267 buffer_posn_from_coords in dispnew.c for how this is handled. */
268
269 #include <config.h>
270 #include <stdio.h>
271 #include <limits.h>
272 #include <setjmp.h>
273
274 #include "lisp.h"
275 #include "keyboard.h"
276 #include "frame.h"
277 #include "window.h"
278 #include "termchar.h"
279 #include "dispextern.h"
280 #include "buffer.h"
281 #include "character.h"
282 #include "charset.h"
283 #include "indent.h"
284 #include "commands.h"
285 #include "keymap.h"
286 #include "macros.h"
287 #include "disptab.h"
288 #include "termhooks.h"
289 #include "termopts.h"
290 #include "intervals.h"
291 #include "coding.h"
292 #include "process.h"
293 #include "region-cache.h"
294 #include "font.h"
295 #include "fontset.h"
296 #include "blockinput.h"
297
298 #ifdef HAVE_X_WINDOWS
299 #include "xterm.h"
300 #endif
301 #ifdef WINDOWSNT
302 #include "w32term.h"
303 #endif
304 #ifdef HAVE_NS
305 #include "nsterm.h"
306 #endif
307 #ifdef USE_GTK
308 #include "gtkutil.h"
309 #endif
310
311 #include "font.h"
312
313 #ifndef FRAME_X_OUTPUT
314 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
315 #endif
316
317 #define INFINITY 10000000
318
319 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
320 Lisp_Object Qwindow_scroll_functions;
321 static Lisp_Object Qwindow_text_change_functions;
322 static Lisp_Object Qredisplay_end_trigger_functions;
323 Lisp_Object Qinhibit_point_motion_hooks;
324 static Lisp_Object QCeval, QCpropertize;
325 Lisp_Object QCfile, QCdata;
326 static Lisp_Object Qfontified;
327 static Lisp_Object Qgrow_only;
328 static Lisp_Object Qinhibit_eval_during_redisplay;
329 static Lisp_Object Qbuffer_position, Qposition, Qobject;
330 static Lisp_Object Qright_to_left, Qleft_to_right;
331
332 /* Cursor shapes */
333 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
334
335 /* Pointer shapes */
336 static Lisp_Object Qarrow, Qhand;
337 Lisp_Object Qtext;
338
339 /* Holds the list (error). */
340 static Lisp_Object list_of_error;
341
342 static Lisp_Object Qfontification_functions;
343
344 static Lisp_Object Qwrap_prefix;
345 static Lisp_Object Qline_prefix;
346
347 /* Non-nil means don't actually do any redisplay. */
348
349 Lisp_Object Qinhibit_redisplay;
350
351 /* Names of text properties relevant for redisplay. */
352
353 Lisp_Object Qdisplay;
354
355 Lisp_Object Qspace, QCalign_to;
356 static Lisp_Object QCrelative_width, QCrelative_height;
357 Lisp_Object Qleft_margin, Qright_margin;
358 static Lisp_Object Qspace_width, Qraise;
359 static Lisp_Object Qslice;
360 Lisp_Object Qcenter;
361 static Lisp_Object Qmargin, Qpointer;
362 static Lisp_Object Qline_height;
363
364 #ifdef HAVE_WINDOW_SYSTEM
365
366 /* Test if overflow newline into fringe. Called with iterator IT
367 at or past right window margin, and with IT->current_x set. */
368
369 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
370 (!NILP (Voverflow_newline_into_fringe) \
371 && FRAME_WINDOW_P ((IT)->f) \
372 && ((IT)->bidi_it.paragraph_dir == R2L \
373 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
374 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
375 && (IT)->current_x == (IT)->last_visible_x \
376 && (IT)->line_wrap != WORD_WRAP)
377
378 #else /* !HAVE_WINDOW_SYSTEM */
379 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
380 #endif /* HAVE_WINDOW_SYSTEM */
381
382 /* Test if the display element loaded in IT is a space or tab
383 character. This is used to determine word wrapping. */
384
385 #define IT_DISPLAYING_WHITESPACE(it) \
386 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
387
388 /* Name of the face used to highlight trailing whitespace. */
389
390 static Lisp_Object Qtrailing_whitespace;
391
392 /* Name and number of the face used to highlight escape glyphs. */
393
394 static Lisp_Object Qescape_glyph;
395
396 /* Name and number of the face used to highlight non-breaking spaces. */
397
398 static Lisp_Object Qnobreak_space;
399
400 /* The symbol `image' which is the car of the lists used to represent
401 images in Lisp. Also a tool bar style. */
402
403 Lisp_Object Qimage;
404
405 /* The image map types. */
406 Lisp_Object QCmap;
407 static Lisp_Object QCpointer;
408 static Lisp_Object Qrect, Qcircle, Qpoly;
409
410 /* Tool bar styles */
411 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
412
413 /* Non-zero means print newline to stdout before next mini-buffer
414 message. */
415
416 int noninteractive_need_newline;
417
418 /* Non-zero means print newline to message log before next message. */
419
420 static int message_log_need_newline;
421
422 /* Three markers that message_dolog uses.
423 It could allocate them itself, but that causes trouble
424 in handling memory-full errors. */
425 static Lisp_Object message_dolog_marker1;
426 static Lisp_Object message_dolog_marker2;
427 static Lisp_Object message_dolog_marker3;
428 \f
429 /* The buffer position of the first character appearing entirely or
430 partially on the line of the selected window which contains the
431 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
432 redisplay optimization in redisplay_internal. */
433
434 static struct text_pos this_line_start_pos;
435
436 /* Number of characters past the end of the line above, including the
437 terminating newline. */
438
439 static struct text_pos this_line_end_pos;
440
441 /* The vertical positions and the height of this line. */
442
443 static int this_line_vpos;
444 static int this_line_y;
445 static int this_line_pixel_height;
446
447 /* X position at which this display line starts. Usually zero;
448 negative if first character is partially visible. */
449
450 static int this_line_start_x;
451
452 /* The smallest character position seen by move_it_* functions as they
453 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
454 hscrolled lines, see display_line. */
455
456 static struct text_pos this_line_min_pos;
457
458 /* Buffer that this_line_.* variables are referring to. */
459
460 static struct buffer *this_line_buffer;
461
462
463 /* Values of those variables at last redisplay are stored as
464 properties on `overlay-arrow-position' symbol. However, if
465 Voverlay_arrow_position is a marker, last-arrow-position is its
466 numerical position. */
467
468 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
469
470 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
471 properties on a symbol in overlay-arrow-variable-list. */
472
473 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
474
475 Lisp_Object Qmenu_bar_update_hook;
476
477 /* Nonzero if an overlay arrow has been displayed in this window. */
478
479 static int overlay_arrow_seen;
480
481 /* Number of windows showing the buffer of the selected window (or
482 another buffer with the same base buffer). keyboard.c refers to
483 this. */
484
485 int buffer_shared;
486
487 /* Vector containing glyphs for an ellipsis `...'. */
488
489 static Lisp_Object default_invis_vector[3];
490
491 /* This is the window where the echo area message was displayed. It
492 is always a mini-buffer window, but it may not be the same window
493 currently active as a mini-buffer. */
494
495 Lisp_Object echo_area_window;
496
497 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
498 pushes the current message and the value of
499 message_enable_multibyte on the stack, the function restore_message
500 pops the stack and displays MESSAGE again. */
501
502 static Lisp_Object Vmessage_stack;
503
504 /* Nonzero means multibyte characters were enabled when the echo area
505 message was specified. */
506
507 static int message_enable_multibyte;
508
509 /* Nonzero if we should redraw the mode lines on the next redisplay. */
510
511 int update_mode_lines;
512
513 /* Nonzero if window sizes or contents have changed since last
514 redisplay that finished. */
515
516 int windows_or_buffers_changed;
517
518 /* Nonzero means a frame's cursor type has been changed. */
519
520 int cursor_type_changed;
521
522 /* Nonzero after display_mode_line if %l was used and it displayed a
523 line number. */
524
525 static int line_number_displayed;
526
527 /* The name of the *Messages* buffer, a string. */
528
529 static Lisp_Object Vmessages_buffer_name;
530
531 /* Current, index 0, and last displayed echo area message. Either
532 buffers from echo_buffers, or nil to indicate no message. */
533
534 Lisp_Object echo_area_buffer[2];
535
536 /* The buffers referenced from echo_area_buffer. */
537
538 static Lisp_Object echo_buffer[2];
539
540 /* A vector saved used in with_area_buffer to reduce consing. */
541
542 static Lisp_Object Vwith_echo_area_save_vector;
543
544 /* Non-zero means display_echo_area should display the last echo area
545 message again. Set by redisplay_preserve_echo_area. */
546
547 static int display_last_displayed_message_p;
548
549 /* Nonzero if echo area is being used by print; zero if being used by
550 message. */
551
552 static int message_buf_print;
553
554 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
555
556 static Lisp_Object Qinhibit_menubar_update;
557 static Lisp_Object Qmessage_truncate_lines;
558
559 /* Set to 1 in clear_message to make redisplay_internal aware
560 of an emptied echo area. */
561
562 static int message_cleared_p;
563
564 /* A scratch glyph row with contents used for generating truncation
565 glyphs. Also used in direct_output_for_insert. */
566
567 #define MAX_SCRATCH_GLYPHS 100
568 static struct glyph_row scratch_glyph_row;
569 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
570
571 /* Ascent and height of the last line processed by move_it_to. */
572
573 static int last_max_ascent, last_height;
574
575 /* Non-zero if there's a help-echo in the echo area. */
576
577 int help_echo_showing_p;
578
579 /* If >= 0, computed, exact values of mode-line and header-line height
580 to use in the macros CURRENT_MODE_LINE_HEIGHT and
581 CURRENT_HEADER_LINE_HEIGHT. */
582
583 int current_mode_line_height, current_header_line_height;
584
585 /* The maximum distance to look ahead for text properties. Values
586 that are too small let us call compute_char_face and similar
587 functions too often which is expensive. Values that are too large
588 let us call compute_char_face and alike too often because we
589 might not be interested in text properties that far away. */
590
591 #define TEXT_PROP_DISTANCE_LIMIT 100
592
593 #if GLYPH_DEBUG
594
595 /* Non-zero means print traces of redisplay if compiled with
596 GLYPH_DEBUG != 0. */
597
598 int trace_redisplay_p;
599
600 #endif /* GLYPH_DEBUG */
601
602 #ifdef DEBUG_TRACE_MOVE
603 /* Non-zero means trace with TRACE_MOVE to stderr. */
604 int trace_move;
605
606 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
607 #else
608 #define TRACE_MOVE(x) (void) 0
609 #endif
610
611 static Lisp_Object Qauto_hscroll_mode;
612
613 /* Buffer being redisplayed -- for redisplay_window_error. */
614
615 static struct buffer *displayed_buffer;
616
617 /* Value returned from text property handlers (see below). */
618
619 enum prop_handled
620 {
621 HANDLED_NORMALLY,
622 HANDLED_RECOMPUTE_PROPS,
623 HANDLED_OVERLAY_STRING_CONSUMED,
624 HANDLED_RETURN
625 };
626
627 /* A description of text properties that redisplay is interested
628 in. */
629
630 struct props
631 {
632 /* The name of the property. */
633 Lisp_Object *name;
634
635 /* A unique index for the property. */
636 enum prop_idx idx;
637
638 /* A handler function called to set up iterator IT from the property
639 at IT's current position. Value is used to steer handle_stop. */
640 enum prop_handled (*handler) (struct it *it);
641 };
642
643 static enum prop_handled handle_face_prop (struct it *);
644 static enum prop_handled handle_invisible_prop (struct it *);
645 static enum prop_handled handle_display_prop (struct it *);
646 static enum prop_handled handle_composition_prop (struct it *);
647 static enum prop_handled handle_overlay_change (struct it *);
648 static enum prop_handled handle_fontified_prop (struct it *);
649
650 /* Properties handled by iterators. */
651
652 static struct props it_props[] =
653 {
654 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
655 /* Handle `face' before `display' because some sub-properties of
656 `display' need to know the face. */
657 {&Qface, FACE_PROP_IDX, handle_face_prop},
658 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
659 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
660 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
661 {NULL, 0, NULL}
662 };
663
664 /* Value is the position described by X. If X is a marker, value is
665 the marker_position of X. Otherwise, value is X. */
666
667 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
668
669 /* Enumeration returned by some move_it_.* functions internally. */
670
671 enum move_it_result
672 {
673 /* Not used. Undefined value. */
674 MOVE_UNDEFINED,
675
676 /* Move ended at the requested buffer position or ZV. */
677 MOVE_POS_MATCH_OR_ZV,
678
679 /* Move ended at the requested X pixel position. */
680 MOVE_X_REACHED,
681
682 /* Move within a line ended at the end of a line that must be
683 continued. */
684 MOVE_LINE_CONTINUED,
685
686 /* Move within a line ended at the end of a line that would
687 be displayed truncated. */
688 MOVE_LINE_TRUNCATED,
689
690 /* Move within a line ended at a line end. */
691 MOVE_NEWLINE_OR_CR
692 };
693
694 /* This counter is used to clear the face cache every once in a while
695 in redisplay_internal. It is incremented for each redisplay.
696 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
697 cleared. */
698
699 #define CLEAR_FACE_CACHE_COUNT 500
700 static int clear_face_cache_count;
701
702 /* Similarly for the image cache. */
703
704 #ifdef HAVE_WINDOW_SYSTEM
705 #define CLEAR_IMAGE_CACHE_COUNT 101
706 static int clear_image_cache_count;
707
708 /* Null glyph slice */
709 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
710 #endif
711
712 /* Non-zero while redisplay_internal is in progress. */
713
714 int redisplaying_p;
715
716 static Lisp_Object Qinhibit_free_realized_faces;
717
718 /* If a string, XTread_socket generates an event to display that string.
719 (The display is done in read_char.) */
720
721 Lisp_Object help_echo_string;
722 Lisp_Object help_echo_window;
723 Lisp_Object help_echo_object;
724 EMACS_INT help_echo_pos;
725
726 /* Temporary variable for XTread_socket. */
727
728 Lisp_Object previous_help_echo_string;
729
730 /* Platform-independent portion of hourglass implementation. */
731
732 /* Non-zero means an hourglass cursor is currently shown. */
733 int hourglass_shown_p;
734
735 /* If non-null, an asynchronous timer that, when it expires, displays
736 an hourglass cursor on all frames. */
737 struct atimer *hourglass_atimer;
738
739 /* Name of the face used to display glyphless characters. */
740 Lisp_Object Qglyphless_char;
741
742 /* Symbol for the purpose of Vglyphless_char_display. */
743 static Lisp_Object Qglyphless_char_display;
744
745 /* Method symbols for Vglyphless_char_display. */
746 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
747
748 /* Default pixel width of `thin-space' display method. */
749 #define THIN_SPACE_WIDTH 1
750
751 /* Default number of seconds to wait before displaying an hourglass
752 cursor. */
753 #define DEFAULT_HOURGLASS_DELAY 1
754
755 \f
756 /* Function prototypes. */
757
758 static void setup_for_ellipsis (struct it *, int);
759 static void set_iterator_to_next (struct it *, int);
760 static void mark_window_display_accurate_1 (struct window *, int);
761 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
762 static int display_prop_string_p (Lisp_Object, Lisp_Object);
763 static int cursor_row_p (struct glyph_row *);
764 static int redisplay_mode_lines (Lisp_Object, int);
765 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
766
767 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
768
769 static void handle_line_prefix (struct it *);
770
771 static void pint2str (char *, int, EMACS_INT);
772 static void pint2hrstr (char *, int, EMACS_INT);
773 static struct text_pos run_window_scroll_functions (Lisp_Object,
774 struct text_pos);
775 static void reconsider_clip_changes (struct window *, struct buffer *);
776 static int text_outside_line_unchanged_p (struct window *,
777 EMACS_INT, EMACS_INT);
778 static void store_mode_line_noprop_char (char);
779 static int store_mode_line_noprop (const char *, int, int);
780 static void handle_stop (struct it *);
781 static void handle_stop_backwards (struct it *, EMACS_INT);
782 static int single_display_spec_intangible_p (Lisp_Object);
783 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
784 static void ensure_echo_area_buffers (void);
785 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
786 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
787 static int with_echo_area_buffer (struct window *, int,
788 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
789 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
790 static void clear_garbaged_frames (void);
791 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
792 static void pop_message (void);
793 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
794 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
795 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
796 static int display_echo_area (struct window *);
797 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
798 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
799 static Lisp_Object unwind_redisplay (Lisp_Object);
800 static int string_char_and_length (const unsigned char *, int *);
801 static struct text_pos display_prop_end (struct it *, Lisp_Object,
802 struct text_pos);
803 static int compute_window_start_on_continuation_line (struct window *);
804 static Lisp_Object safe_eval_handler (Lisp_Object);
805 static void insert_left_trunc_glyphs (struct it *);
806 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
807 Lisp_Object);
808 static void extend_face_to_end_of_line (struct it *);
809 static int append_space_for_newline (struct it *, int);
810 static int cursor_row_fully_visible_p (struct window *, int, int);
811 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
812 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
813 static int trailing_whitespace_p (EMACS_INT);
814 static unsigned long int message_log_check_duplicate (EMACS_INT, EMACS_INT);
815 static void push_it (struct it *);
816 static void pop_it (struct it *);
817 static void sync_frame_with_window_matrix_rows (struct window *);
818 static void select_frame_for_redisplay (Lisp_Object);
819 static void redisplay_internal (void);
820 static int echo_area_display (int);
821 static void redisplay_windows (Lisp_Object);
822 static void redisplay_window (Lisp_Object, int);
823 static Lisp_Object redisplay_window_error (Lisp_Object);
824 static Lisp_Object redisplay_window_0 (Lisp_Object);
825 static Lisp_Object redisplay_window_1 (Lisp_Object);
826 static int set_cursor_from_row (struct window *, struct glyph_row *,
827 struct glyph_matrix *, EMACS_INT, EMACS_INT,
828 int, int);
829 static int update_menu_bar (struct frame *, int, int);
830 static int try_window_reusing_current_matrix (struct window *);
831 static int try_window_id (struct window *);
832 static int display_line (struct it *);
833 static int display_mode_lines (struct window *);
834 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
835 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
836 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
837 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
838 static void display_menu_bar (struct window *);
839 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
840 EMACS_INT *);
841 static int display_string (const char *, Lisp_Object, Lisp_Object,
842 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
843 static void compute_line_metrics (struct it *);
844 static void run_redisplay_end_trigger_hook (struct it *);
845 static int get_overlay_strings (struct it *, EMACS_INT);
846 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
847 static void next_overlay_string (struct it *);
848 static void reseat (struct it *, struct text_pos, int);
849 static void reseat_1 (struct it *, struct text_pos, int);
850 static void back_to_previous_visible_line_start (struct it *);
851 void reseat_at_previous_visible_line_start (struct it *);
852 static void reseat_at_next_visible_line_start (struct it *, int);
853 static int next_element_from_ellipsis (struct it *);
854 static int next_element_from_display_vector (struct it *);
855 static int next_element_from_string (struct it *);
856 static int next_element_from_c_string (struct it *);
857 static int next_element_from_buffer (struct it *);
858 static int next_element_from_composition (struct it *);
859 static int next_element_from_image (struct it *);
860 static int next_element_from_stretch (struct it *);
861 static void load_overlay_strings (struct it *, EMACS_INT);
862 static int init_from_display_pos (struct it *, struct window *,
863 struct display_pos *);
864 static void reseat_to_string (struct it *, const char *,
865 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
866 static int get_next_display_element (struct it *);
867 static enum move_it_result
868 move_it_in_display_line_to (struct it *, EMACS_INT, int,
869 enum move_operation_enum);
870 void move_it_vertically_backward (struct it *, int);
871 static void init_to_row_start (struct it *, struct window *,
872 struct glyph_row *);
873 static int init_to_row_end (struct it *, struct window *,
874 struct glyph_row *);
875 static void back_to_previous_line_start (struct it *);
876 static int forward_to_next_line_start (struct it *, int *);
877 static struct text_pos string_pos_nchars_ahead (struct text_pos,
878 Lisp_Object, EMACS_INT);
879 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
880 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
881 static EMACS_INT number_of_chars (const char *, int);
882 static void compute_stop_pos (struct it *);
883 static void compute_string_pos (struct text_pos *, struct text_pos,
884 Lisp_Object);
885 static int face_before_or_after_it_pos (struct it *, int);
886 static EMACS_INT next_overlay_change (EMACS_INT);
887 static int handle_single_display_spec (struct it *, Lisp_Object,
888 Lisp_Object, Lisp_Object,
889 struct text_pos *, int);
890 static int underlying_face_id (struct it *);
891 static int in_ellipses_for_invisible_text_p (struct display_pos *,
892 struct window *);
893
894 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
895 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
896
897 #ifdef HAVE_WINDOW_SYSTEM
898
899 static void x_consider_frame_title (Lisp_Object);
900 static int tool_bar_lines_needed (struct frame *, int *);
901 static void update_tool_bar (struct frame *, int);
902 static void build_desired_tool_bar_string (struct frame *f);
903 static int redisplay_tool_bar (struct frame *);
904 static void display_tool_bar_line (struct it *, int);
905 static void notice_overwritten_cursor (struct window *,
906 enum glyph_row_area,
907 int, int, int, int);
908 static void append_stretch_glyph (struct it *, Lisp_Object,
909 int, int, int);
910
911
912 #endif /* HAVE_WINDOW_SYSTEM */
913
914 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
915 static int coords_in_mouse_face_p (struct window *, int, int);
916
917
918 \f
919 /***********************************************************************
920 Window display dimensions
921 ***********************************************************************/
922
923 /* Return the bottom boundary y-position for text lines in window W.
924 This is the first y position at which a line cannot start.
925 It is relative to the top of the window.
926
927 This is the height of W minus the height of a mode line, if any. */
928
929 INLINE int
930 window_text_bottom_y (struct window *w)
931 {
932 int height = WINDOW_TOTAL_HEIGHT (w);
933
934 if (WINDOW_WANTS_MODELINE_P (w))
935 height -= CURRENT_MODE_LINE_HEIGHT (w);
936 return height;
937 }
938
939 /* Return the pixel width of display area AREA of window W. AREA < 0
940 means return the total width of W, not including fringes to
941 the left and right of the window. */
942
943 INLINE int
944 window_box_width (struct window *w, int area)
945 {
946 int cols = XFASTINT (w->total_cols);
947 int pixels = 0;
948
949 if (!w->pseudo_window_p)
950 {
951 cols -= WINDOW_SCROLL_BAR_COLS (w);
952
953 if (area == TEXT_AREA)
954 {
955 if (INTEGERP (w->left_margin_cols))
956 cols -= XFASTINT (w->left_margin_cols);
957 if (INTEGERP (w->right_margin_cols))
958 cols -= XFASTINT (w->right_margin_cols);
959 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
960 }
961 else if (area == LEFT_MARGIN_AREA)
962 {
963 cols = (INTEGERP (w->left_margin_cols)
964 ? XFASTINT (w->left_margin_cols) : 0);
965 pixels = 0;
966 }
967 else if (area == RIGHT_MARGIN_AREA)
968 {
969 cols = (INTEGERP (w->right_margin_cols)
970 ? XFASTINT (w->right_margin_cols) : 0);
971 pixels = 0;
972 }
973 }
974
975 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
976 }
977
978
979 /* Return the pixel height of the display area of window W, not
980 including mode lines of W, if any. */
981
982 INLINE int
983 window_box_height (struct window *w)
984 {
985 struct frame *f = XFRAME (w->frame);
986 int height = WINDOW_TOTAL_HEIGHT (w);
987
988 xassert (height >= 0);
989
990 /* Note: the code below that determines the mode-line/header-line
991 height is essentially the same as that contained in the macro
992 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
993 the appropriate glyph row has its `mode_line_p' flag set,
994 and if it doesn't, uses estimate_mode_line_height instead. */
995
996 if (WINDOW_WANTS_MODELINE_P (w))
997 {
998 struct glyph_row *ml_row
999 = (w->current_matrix && w->current_matrix->rows
1000 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1001 : 0);
1002 if (ml_row && ml_row->mode_line_p)
1003 height -= ml_row->height;
1004 else
1005 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1006 }
1007
1008 if (WINDOW_WANTS_HEADER_LINE_P (w))
1009 {
1010 struct glyph_row *hl_row
1011 = (w->current_matrix && w->current_matrix->rows
1012 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1013 : 0);
1014 if (hl_row && hl_row->mode_line_p)
1015 height -= hl_row->height;
1016 else
1017 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1018 }
1019
1020 /* With a very small font and a mode-line that's taller than
1021 default, we might end up with a negative height. */
1022 return max (0, height);
1023 }
1024
1025 /* Return the window-relative coordinate of the left edge of display
1026 area AREA of window W. AREA < 0 means return the left edge of the
1027 whole window, to the right of the left fringe of W. */
1028
1029 INLINE int
1030 window_box_left_offset (struct window *w, int area)
1031 {
1032 int x;
1033
1034 if (w->pseudo_window_p)
1035 return 0;
1036
1037 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1038
1039 if (area == TEXT_AREA)
1040 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1041 + window_box_width (w, LEFT_MARGIN_AREA));
1042 else if (area == RIGHT_MARGIN_AREA)
1043 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1044 + window_box_width (w, LEFT_MARGIN_AREA)
1045 + window_box_width (w, TEXT_AREA)
1046 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1047 ? 0
1048 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1049 else if (area == LEFT_MARGIN_AREA
1050 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1051 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1052
1053 return x;
1054 }
1055
1056
1057 /* Return the window-relative coordinate of the right edge of display
1058 area AREA of window W. AREA < 0 means return the right edge of the
1059 whole window, to the left of the right fringe of W. */
1060
1061 INLINE int
1062 window_box_right_offset (struct window *w, int area)
1063 {
1064 return window_box_left_offset (w, area) + window_box_width (w, area);
1065 }
1066
1067 /* Return the frame-relative coordinate of the left edge of display
1068 area AREA of window W. AREA < 0 means return the left edge of the
1069 whole window, to the right of the left fringe of W. */
1070
1071 INLINE int
1072 window_box_left (struct window *w, int area)
1073 {
1074 struct frame *f = XFRAME (w->frame);
1075 int x;
1076
1077 if (w->pseudo_window_p)
1078 return FRAME_INTERNAL_BORDER_WIDTH (f);
1079
1080 x = (WINDOW_LEFT_EDGE_X (w)
1081 + window_box_left_offset (w, area));
1082
1083 return x;
1084 }
1085
1086
1087 /* Return the frame-relative coordinate of the right edge of display
1088 area AREA of window W. AREA < 0 means return the right edge of the
1089 whole window, to the left of the right fringe of W. */
1090
1091 INLINE int
1092 window_box_right (struct window *w, int area)
1093 {
1094 return window_box_left (w, area) + window_box_width (w, area);
1095 }
1096
1097 /* Get the bounding box of the display area AREA of window W, without
1098 mode lines, in frame-relative coordinates. AREA < 0 means the
1099 whole window, not including the left and right fringes of
1100 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1101 coordinates of the upper-left corner of the box. Return in
1102 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1103
1104 INLINE void
1105 window_box (struct window *w, int area, int *box_x, int *box_y,
1106 int *box_width, int *box_height)
1107 {
1108 if (box_width)
1109 *box_width = window_box_width (w, area);
1110 if (box_height)
1111 *box_height = window_box_height (w);
1112 if (box_x)
1113 *box_x = window_box_left (w, area);
1114 if (box_y)
1115 {
1116 *box_y = WINDOW_TOP_EDGE_Y (w);
1117 if (WINDOW_WANTS_HEADER_LINE_P (w))
1118 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1119 }
1120 }
1121
1122
1123 /* Get the bounding box of the display area AREA of window W, without
1124 mode lines. AREA < 0 means the whole window, not including the
1125 left and right fringe of the window. Return in *TOP_LEFT_X
1126 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1127 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1128 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1129 box. */
1130
1131 static INLINE void
1132 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1133 int *bottom_right_x, int *bottom_right_y)
1134 {
1135 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1136 bottom_right_y);
1137 *bottom_right_x += *top_left_x;
1138 *bottom_right_y += *top_left_y;
1139 }
1140
1141
1142 \f
1143 /***********************************************************************
1144 Utilities
1145 ***********************************************************************/
1146
1147 /* Return the bottom y-position of the line the iterator IT is in.
1148 This can modify IT's settings. */
1149
1150 int
1151 line_bottom_y (struct it *it)
1152 {
1153 int line_height = it->max_ascent + it->max_descent;
1154 int line_top_y = it->current_y;
1155
1156 if (line_height == 0)
1157 {
1158 if (last_height)
1159 line_height = last_height;
1160 else if (IT_CHARPOS (*it) < ZV)
1161 {
1162 move_it_by_lines (it, 1);
1163 line_height = (it->max_ascent || it->max_descent
1164 ? it->max_ascent + it->max_descent
1165 : last_height);
1166 }
1167 else
1168 {
1169 struct glyph_row *row = it->glyph_row;
1170
1171 /* Use the default character height. */
1172 it->glyph_row = NULL;
1173 it->what = IT_CHARACTER;
1174 it->c = ' ';
1175 it->len = 1;
1176 PRODUCE_GLYPHS (it);
1177 line_height = it->ascent + it->descent;
1178 it->glyph_row = row;
1179 }
1180 }
1181
1182 return line_top_y + line_height;
1183 }
1184
1185
1186 /* Return 1 if position CHARPOS is visible in window W.
1187 CHARPOS < 0 means return info about WINDOW_END position.
1188 If visible, set *X and *Y to pixel coordinates of top left corner.
1189 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1190 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1191
1192 int
1193 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1194 int *rtop, int *rbot, int *rowh, int *vpos)
1195 {
1196 struct it it;
1197 struct text_pos top;
1198 int visible_p = 0;
1199 struct buffer *old_buffer = NULL;
1200
1201 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1202 return visible_p;
1203
1204 if (XBUFFER (w->buffer) != current_buffer)
1205 {
1206 old_buffer = current_buffer;
1207 set_buffer_internal_1 (XBUFFER (w->buffer));
1208 }
1209
1210 SET_TEXT_POS_FROM_MARKER (top, w->start);
1211
1212 /* Compute exact mode line heights. */
1213 if (WINDOW_WANTS_MODELINE_P (w))
1214 current_mode_line_height
1215 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1216 BVAR (current_buffer, mode_line_format));
1217
1218 if (WINDOW_WANTS_HEADER_LINE_P (w))
1219 current_header_line_height
1220 = display_mode_line (w, HEADER_LINE_FACE_ID,
1221 BVAR (current_buffer, header_line_format));
1222
1223 start_display (&it, w, top);
1224 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1225 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1226
1227 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1228 {
1229 /* We have reached CHARPOS, or passed it. How the call to
1230 move_it_to can overshoot: (i) If CHARPOS is on invisible
1231 text, move_it_to stops at the end of the invisible text,
1232 after CHARPOS. (ii) If CHARPOS is in a display vector,
1233 move_it_to stops on its last glyph. */
1234 int top_x = it.current_x;
1235 int top_y = it.current_y;
1236 enum it_method it_method = it.method;
1237 /* Calling line_bottom_y may change it.method, it.position, etc. */
1238 int bottom_y = (last_height = 0, line_bottom_y (&it));
1239 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1240
1241 if (top_y < window_top_y)
1242 visible_p = bottom_y > window_top_y;
1243 else if (top_y < it.last_visible_y)
1244 visible_p = 1;
1245 if (visible_p)
1246 {
1247 if (it_method == GET_FROM_DISPLAY_VECTOR)
1248 {
1249 /* We stopped on the last glyph of a display vector.
1250 Try and recompute. Hack alert! */
1251 if (charpos < 2 || top.charpos >= charpos)
1252 top_x = it.glyph_row->x;
1253 else
1254 {
1255 struct it it2;
1256 start_display (&it2, w, top);
1257 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1258 get_next_display_element (&it2);
1259 PRODUCE_GLYPHS (&it2);
1260 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1261 || it2.current_x > it2.last_visible_x)
1262 top_x = it.glyph_row->x;
1263 else
1264 {
1265 top_x = it2.current_x;
1266 top_y = it2.current_y;
1267 }
1268 }
1269 }
1270
1271 *x = top_x;
1272 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1273 *rtop = max (0, window_top_y - top_y);
1274 *rbot = max (0, bottom_y - it.last_visible_y);
1275 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1276 - max (top_y, window_top_y)));
1277 *vpos = it.vpos;
1278 }
1279 }
1280 else
1281 {
1282 struct it it2;
1283
1284 it2 = it;
1285 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1286 move_it_by_lines (&it, 1);
1287 if (charpos < IT_CHARPOS (it)
1288 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1289 {
1290 visible_p = 1;
1291 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1292 *x = it2.current_x;
1293 *y = it2.current_y + it2.max_ascent - it2.ascent;
1294 *rtop = max (0, -it2.current_y);
1295 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1296 - it.last_visible_y));
1297 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1298 it.last_visible_y)
1299 - max (it2.current_y,
1300 WINDOW_HEADER_LINE_HEIGHT (w))));
1301 *vpos = it2.vpos;
1302 }
1303 }
1304
1305 if (old_buffer)
1306 set_buffer_internal_1 (old_buffer);
1307
1308 current_header_line_height = current_mode_line_height = -1;
1309
1310 if (visible_p && XFASTINT (w->hscroll) > 0)
1311 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1312
1313 #if 0
1314 /* Debugging code. */
1315 if (visible_p)
1316 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1317 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1318 else
1319 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1320 #endif
1321
1322 return visible_p;
1323 }
1324
1325
1326 /* Return the next character from STR. Return in *LEN the length of
1327 the character. This is like STRING_CHAR_AND_LENGTH but never
1328 returns an invalid character. If we find one, we return a `?', but
1329 with the length of the invalid character. */
1330
1331 static INLINE int
1332 string_char_and_length (const unsigned char *str, int *len)
1333 {
1334 int c;
1335
1336 c = STRING_CHAR_AND_LENGTH (str, *len);
1337 if (!CHAR_VALID_P (c, 1))
1338 /* We may not change the length here because other places in Emacs
1339 don't use this function, i.e. they silently accept invalid
1340 characters. */
1341 c = '?';
1342
1343 return c;
1344 }
1345
1346
1347
1348 /* Given a position POS containing a valid character and byte position
1349 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1350
1351 static struct text_pos
1352 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1353 {
1354 xassert (STRINGP (string) && nchars >= 0);
1355
1356 if (STRING_MULTIBYTE (string))
1357 {
1358 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1359 int len;
1360
1361 while (nchars--)
1362 {
1363 string_char_and_length (p, &len);
1364 p += len;
1365 CHARPOS (pos) += 1;
1366 BYTEPOS (pos) += len;
1367 }
1368 }
1369 else
1370 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1371
1372 return pos;
1373 }
1374
1375
1376 /* Value is the text position, i.e. character and byte position,
1377 for character position CHARPOS in STRING. */
1378
1379 static INLINE struct text_pos
1380 string_pos (EMACS_INT charpos, Lisp_Object string)
1381 {
1382 struct text_pos pos;
1383 xassert (STRINGP (string));
1384 xassert (charpos >= 0);
1385 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1386 return pos;
1387 }
1388
1389
1390 /* Value is a text position, i.e. character and byte position, for
1391 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1392 means recognize multibyte characters. */
1393
1394 static struct text_pos
1395 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1396 {
1397 struct text_pos pos;
1398
1399 xassert (s != NULL);
1400 xassert (charpos >= 0);
1401
1402 if (multibyte_p)
1403 {
1404 int len;
1405
1406 SET_TEXT_POS (pos, 0, 0);
1407 while (charpos--)
1408 {
1409 string_char_and_length ((const unsigned char *) s, &len);
1410 s += len;
1411 CHARPOS (pos) += 1;
1412 BYTEPOS (pos) += len;
1413 }
1414 }
1415 else
1416 SET_TEXT_POS (pos, charpos, charpos);
1417
1418 return pos;
1419 }
1420
1421
1422 /* Value is the number of characters in C string S. MULTIBYTE_P
1423 non-zero means recognize multibyte characters. */
1424
1425 static EMACS_INT
1426 number_of_chars (const char *s, int multibyte_p)
1427 {
1428 EMACS_INT nchars;
1429
1430 if (multibyte_p)
1431 {
1432 EMACS_INT rest = strlen (s);
1433 int len;
1434 const unsigned char *p = (const unsigned char *) s;
1435
1436 for (nchars = 0; rest > 0; ++nchars)
1437 {
1438 string_char_and_length (p, &len);
1439 rest -= len, p += len;
1440 }
1441 }
1442 else
1443 nchars = strlen (s);
1444
1445 return nchars;
1446 }
1447
1448
1449 /* Compute byte position NEWPOS->bytepos corresponding to
1450 NEWPOS->charpos. POS is a known position in string STRING.
1451 NEWPOS->charpos must be >= POS.charpos. */
1452
1453 static void
1454 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1455 {
1456 xassert (STRINGP (string));
1457 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1458
1459 if (STRING_MULTIBYTE (string))
1460 *newpos = string_pos_nchars_ahead (pos, string,
1461 CHARPOS (*newpos) - CHARPOS (pos));
1462 else
1463 BYTEPOS (*newpos) = CHARPOS (*newpos);
1464 }
1465
1466 /* EXPORT:
1467 Return an estimation of the pixel height of mode or header lines on
1468 frame F. FACE_ID specifies what line's height to estimate. */
1469
1470 int
1471 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1472 {
1473 #ifdef HAVE_WINDOW_SYSTEM
1474 if (FRAME_WINDOW_P (f))
1475 {
1476 int height = FONT_HEIGHT (FRAME_FONT (f));
1477
1478 /* This function is called so early when Emacs starts that the face
1479 cache and mode line face are not yet initialized. */
1480 if (FRAME_FACE_CACHE (f))
1481 {
1482 struct face *face = FACE_FROM_ID (f, face_id);
1483 if (face)
1484 {
1485 if (face->font)
1486 height = FONT_HEIGHT (face->font);
1487 if (face->box_line_width > 0)
1488 height += 2 * face->box_line_width;
1489 }
1490 }
1491
1492 return height;
1493 }
1494 #endif
1495
1496 return 1;
1497 }
1498
1499 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1500 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1501 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1502 not force the value into range. */
1503
1504 void
1505 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1506 int *x, int *y, NativeRectangle *bounds, int noclip)
1507 {
1508
1509 #ifdef HAVE_WINDOW_SYSTEM
1510 if (FRAME_WINDOW_P (f))
1511 {
1512 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1513 even for negative values. */
1514 if (pix_x < 0)
1515 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1516 if (pix_y < 0)
1517 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1518
1519 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1520 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1521
1522 if (bounds)
1523 STORE_NATIVE_RECT (*bounds,
1524 FRAME_COL_TO_PIXEL_X (f, pix_x),
1525 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1526 FRAME_COLUMN_WIDTH (f) - 1,
1527 FRAME_LINE_HEIGHT (f) - 1);
1528
1529 if (!noclip)
1530 {
1531 if (pix_x < 0)
1532 pix_x = 0;
1533 else if (pix_x > FRAME_TOTAL_COLS (f))
1534 pix_x = FRAME_TOTAL_COLS (f);
1535
1536 if (pix_y < 0)
1537 pix_y = 0;
1538 else if (pix_y > FRAME_LINES (f))
1539 pix_y = FRAME_LINES (f);
1540 }
1541 }
1542 #endif
1543
1544 *x = pix_x;
1545 *y = pix_y;
1546 }
1547
1548
1549 /* Find the glyph under window-relative coordinates X/Y in window W.
1550 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1551 strings. Return in *HPOS and *VPOS the row and column number of
1552 the glyph found. Return in *AREA the glyph area containing X.
1553 Value is a pointer to the glyph found or null if X/Y is not on
1554 text, or we can't tell because W's current matrix is not up to
1555 date. */
1556
1557 static
1558 struct glyph *
1559 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1560 int *dx, int *dy, int *area)
1561 {
1562 struct glyph *glyph, *end;
1563 struct glyph_row *row = NULL;
1564 int x0, i;
1565
1566 /* Find row containing Y. Give up if some row is not enabled. */
1567 for (i = 0; i < w->current_matrix->nrows; ++i)
1568 {
1569 row = MATRIX_ROW (w->current_matrix, i);
1570 if (!row->enabled_p)
1571 return NULL;
1572 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1573 break;
1574 }
1575
1576 *vpos = i;
1577 *hpos = 0;
1578
1579 /* Give up if Y is not in the window. */
1580 if (i == w->current_matrix->nrows)
1581 return NULL;
1582
1583 /* Get the glyph area containing X. */
1584 if (w->pseudo_window_p)
1585 {
1586 *area = TEXT_AREA;
1587 x0 = 0;
1588 }
1589 else
1590 {
1591 if (x < window_box_left_offset (w, TEXT_AREA))
1592 {
1593 *area = LEFT_MARGIN_AREA;
1594 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1595 }
1596 else if (x < window_box_right_offset (w, TEXT_AREA))
1597 {
1598 *area = TEXT_AREA;
1599 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1600 }
1601 else
1602 {
1603 *area = RIGHT_MARGIN_AREA;
1604 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1605 }
1606 }
1607
1608 /* Find glyph containing X. */
1609 glyph = row->glyphs[*area];
1610 end = glyph + row->used[*area];
1611 x -= x0;
1612 while (glyph < end && x >= glyph->pixel_width)
1613 {
1614 x -= glyph->pixel_width;
1615 ++glyph;
1616 }
1617
1618 if (glyph == end)
1619 return NULL;
1620
1621 if (dx)
1622 {
1623 *dx = x;
1624 *dy = y - (row->y + row->ascent - glyph->ascent);
1625 }
1626
1627 *hpos = glyph - row->glyphs[*area];
1628 return glyph;
1629 }
1630
1631 /* Convert frame-relative x/y to coordinates relative to window W.
1632 Takes pseudo-windows into account. */
1633
1634 static void
1635 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1636 {
1637 if (w->pseudo_window_p)
1638 {
1639 /* A pseudo-window is always full-width, and starts at the
1640 left edge of the frame, plus a frame border. */
1641 struct frame *f = XFRAME (w->frame);
1642 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1643 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1644 }
1645 else
1646 {
1647 *x -= WINDOW_LEFT_EDGE_X (w);
1648 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1649 }
1650 }
1651
1652 #ifdef HAVE_WINDOW_SYSTEM
1653
1654 /* EXPORT:
1655 Return in RECTS[] at most N clipping rectangles for glyph string S.
1656 Return the number of stored rectangles. */
1657
1658 int
1659 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1660 {
1661 XRectangle r;
1662
1663 if (n <= 0)
1664 return 0;
1665
1666 if (s->row->full_width_p)
1667 {
1668 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1669 r.x = WINDOW_LEFT_EDGE_X (s->w);
1670 r.width = WINDOW_TOTAL_WIDTH (s->w);
1671
1672 /* Unless displaying a mode or menu bar line, which are always
1673 fully visible, clip to the visible part of the row. */
1674 if (s->w->pseudo_window_p)
1675 r.height = s->row->visible_height;
1676 else
1677 r.height = s->height;
1678 }
1679 else
1680 {
1681 /* This is a text line that may be partially visible. */
1682 r.x = window_box_left (s->w, s->area);
1683 r.width = window_box_width (s->w, s->area);
1684 r.height = s->row->visible_height;
1685 }
1686
1687 if (s->clip_head)
1688 if (r.x < s->clip_head->x)
1689 {
1690 if (r.width >= s->clip_head->x - r.x)
1691 r.width -= s->clip_head->x - r.x;
1692 else
1693 r.width = 0;
1694 r.x = s->clip_head->x;
1695 }
1696 if (s->clip_tail)
1697 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1698 {
1699 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1700 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1701 else
1702 r.width = 0;
1703 }
1704
1705 /* If S draws overlapping rows, it's sufficient to use the top and
1706 bottom of the window for clipping because this glyph string
1707 intentionally draws over other lines. */
1708 if (s->for_overlaps)
1709 {
1710 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1711 r.height = window_text_bottom_y (s->w) - r.y;
1712
1713 /* Alas, the above simple strategy does not work for the
1714 environments with anti-aliased text: if the same text is
1715 drawn onto the same place multiple times, it gets thicker.
1716 If the overlap we are processing is for the erased cursor, we
1717 take the intersection with the rectagle of the cursor. */
1718 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1719 {
1720 XRectangle rc, r_save = r;
1721
1722 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1723 rc.y = s->w->phys_cursor.y;
1724 rc.width = s->w->phys_cursor_width;
1725 rc.height = s->w->phys_cursor_height;
1726
1727 x_intersect_rectangles (&r_save, &rc, &r);
1728 }
1729 }
1730 else
1731 {
1732 /* Don't use S->y for clipping because it doesn't take partially
1733 visible lines into account. For example, it can be negative for
1734 partially visible lines at the top of a window. */
1735 if (!s->row->full_width_p
1736 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1737 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1738 else
1739 r.y = max (0, s->row->y);
1740 }
1741
1742 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1743
1744 /* If drawing the cursor, don't let glyph draw outside its
1745 advertised boundaries. Cleartype does this under some circumstances. */
1746 if (s->hl == DRAW_CURSOR)
1747 {
1748 struct glyph *glyph = s->first_glyph;
1749 int height, max_y;
1750
1751 if (s->x > r.x)
1752 {
1753 r.width -= s->x - r.x;
1754 r.x = s->x;
1755 }
1756 r.width = min (r.width, glyph->pixel_width);
1757
1758 /* If r.y is below window bottom, ensure that we still see a cursor. */
1759 height = min (glyph->ascent + glyph->descent,
1760 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1761 max_y = window_text_bottom_y (s->w) - height;
1762 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1763 if (s->ybase - glyph->ascent > max_y)
1764 {
1765 r.y = max_y;
1766 r.height = height;
1767 }
1768 else
1769 {
1770 /* Don't draw cursor glyph taller than our actual glyph. */
1771 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1772 if (height < r.height)
1773 {
1774 max_y = r.y + r.height;
1775 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1776 r.height = min (max_y - r.y, height);
1777 }
1778 }
1779 }
1780
1781 if (s->row->clip)
1782 {
1783 XRectangle r_save = r;
1784
1785 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1786 r.width = 0;
1787 }
1788
1789 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1790 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1791 {
1792 #ifdef CONVERT_FROM_XRECT
1793 CONVERT_FROM_XRECT (r, *rects);
1794 #else
1795 *rects = r;
1796 #endif
1797 return 1;
1798 }
1799 else
1800 {
1801 /* If we are processing overlapping and allowed to return
1802 multiple clipping rectangles, we exclude the row of the glyph
1803 string from the clipping rectangle. This is to avoid drawing
1804 the same text on the environment with anti-aliasing. */
1805 #ifdef CONVERT_FROM_XRECT
1806 XRectangle rs[2];
1807 #else
1808 XRectangle *rs = rects;
1809 #endif
1810 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1811
1812 if (s->for_overlaps & OVERLAPS_PRED)
1813 {
1814 rs[i] = r;
1815 if (r.y + r.height > row_y)
1816 {
1817 if (r.y < row_y)
1818 rs[i].height = row_y - r.y;
1819 else
1820 rs[i].height = 0;
1821 }
1822 i++;
1823 }
1824 if (s->for_overlaps & OVERLAPS_SUCC)
1825 {
1826 rs[i] = r;
1827 if (r.y < row_y + s->row->visible_height)
1828 {
1829 if (r.y + r.height > row_y + s->row->visible_height)
1830 {
1831 rs[i].y = row_y + s->row->visible_height;
1832 rs[i].height = r.y + r.height - rs[i].y;
1833 }
1834 else
1835 rs[i].height = 0;
1836 }
1837 i++;
1838 }
1839
1840 n = i;
1841 #ifdef CONVERT_FROM_XRECT
1842 for (i = 0; i < n; i++)
1843 CONVERT_FROM_XRECT (rs[i], rects[i]);
1844 #endif
1845 return n;
1846 }
1847 }
1848
1849 /* EXPORT:
1850 Return in *NR the clipping rectangle for glyph string S. */
1851
1852 void
1853 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1854 {
1855 get_glyph_string_clip_rects (s, nr, 1);
1856 }
1857
1858
1859 /* EXPORT:
1860 Return the position and height of the phys cursor in window W.
1861 Set w->phys_cursor_width to width of phys cursor.
1862 */
1863
1864 void
1865 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1866 struct glyph *glyph, int *xp, int *yp, int *heightp)
1867 {
1868 struct frame *f = XFRAME (WINDOW_FRAME (w));
1869 int x, y, wd, h, h0, y0;
1870
1871 /* Compute the width of the rectangle to draw. If on a stretch
1872 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1873 rectangle as wide as the glyph, but use a canonical character
1874 width instead. */
1875 wd = glyph->pixel_width - 1;
1876 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1877 wd++; /* Why? */
1878 #endif
1879
1880 x = w->phys_cursor.x;
1881 if (x < 0)
1882 {
1883 wd += x;
1884 x = 0;
1885 }
1886
1887 if (glyph->type == STRETCH_GLYPH
1888 && !x_stretch_cursor_p)
1889 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1890 w->phys_cursor_width = wd;
1891
1892 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1893
1894 /* If y is below window bottom, ensure that we still see a cursor. */
1895 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1896
1897 h = max (h0, glyph->ascent + glyph->descent);
1898 h0 = min (h0, glyph->ascent + glyph->descent);
1899
1900 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1901 if (y < y0)
1902 {
1903 h = max (h - (y0 - y) + 1, h0);
1904 y = y0 - 1;
1905 }
1906 else
1907 {
1908 y0 = window_text_bottom_y (w) - h0;
1909 if (y > y0)
1910 {
1911 h += y - y0;
1912 y = y0;
1913 }
1914 }
1915
1916 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1917 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1918 *heightp = h;
1919 }
1920
1921 /*
1922 * Remember which glyph the mouse is over.
1923 */
1924
1925 void
1926 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1927 {
1928 Lisp_Object window;
1929 struct window *w;
1930 struct glyph_row *r, *gr, *end_row;
1931 enum window_part part;
1932 enum glyph_row_area area;
1933 int x, y, width, height;
1934
1935 /* Try to determine frame pixel position and size of the glyph under
1936 frame pixel coordinates X/Y on frame F. */
1937
1938 if (!f->glyphs_initialized_p
1939 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1940 NILP (window)))
1941 {
1942 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1943 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1944 goto virtual_glyph;
1945 }
1946
1947 w = XWINDOW (window);
1948 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1949 height = WINDOW_FRAME_LINE_HEIGHT (w);
1950
1951 x = window_relative_x_coord (w, part, gx);
1952 y = gy - WINDOW_TOP_EDGE_Y (w);
1953
1954 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1955 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1956
1957 if (w->pseudo_window_p)
1958 {
1959 area = TEXT_AREA;
1960 part = ON_MODE_LINE; /* Don't adjust margin. */
1961 goto text_glyph;
1962 }
1963
1964 switch (part)
1965 {
1966 case ON_LEFT_MARGIN:
1967 area = LEFT_MARGIN_AREA;
1968 goto text_glyph;
1969
1970 case ON_RIGHT_MARGIN:
1971 area = RIGHT_MARGIN_AREA;
1972 goto text_glyph;
1973
1974 case ON_HEADER_LINE:
1975 case ON_MODE_LINE:
1976 gr = (part == ON_HEADER_LINE
1977 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1978 : MATRIX_MODE_LINE_ROW (w->current_matrix));
1979 gy = gr->y;
1980 area = TEXT_AREA;
1981 goto text_glyph_row_found;
1982
1983 case ON_TEXT:
1984 area = TEXT_AREA;
1985
1986 text_glyph:
1987 gr = 0; gy = 0;
1988 for (; r <= end_row && r->enabled_p; ++r)
1989 if (r->y + r->height > y)
1990 {
1991 gr = r; gy = r->y;
1992 break;
1993 }
1994
1995 text_glyph_row_found:
1996 if (gr && gy <= y)
1997 {
1998 struct glyph *g = gr->glyphs[area];
1999 struct glyph *end = g + gr->used[area];
2000
2001 height = gr->height;
2002 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2003 if (gx + g->pixel_width > x)
2004 break;
2005
2006 if (g < end)
2007 {
2008 if (g->type == IMAGE_GLYPH)
2009 {
2010 /* Don't remember when mouse is over image, as
2011 image may have hot-spots. */
2012 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2013 return;
2014 }
2015 width = g->pixel_width;
2016 }
2017 else
2018 {
2019 /* Use nominal char spacing at end of line. */
2020 x -= gx;
2021 gx += (x / width) * width;
2022 }
2023
2024 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2025 gx += window_box_left_offset (w, area);
2026 }
2027 else
2028 {
2029 /* Use nominal line height at end of window. */
2030 gx = (x / width) * width;
2031 y -= gy;
2032 gy += (y / height) * height;
2033 }
2034 break;
2035
2036 case ON_LEFT_FRINGE:
2037 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2038 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2039 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2040 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2041 goto row_glyph;
2042
2043 case ON_RIGHT_FRINGE:
2044 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2045 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2046 : window_box_right_offset (w, TEXT_AREA));
2047 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2048 goto row_glyph;
2049
2050 case ON_SCROLL_BAR:
2051 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2052 ? 0
2053 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2054 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2055 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2056 : 0)));
2057 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2058
2059 row_glyph:
2060 gr = 0, gy = 0;
2061 for (; r <= end_row && r->enabled_p; ++r)
2062 if (r->y + r->height > y)
2063 {
2064 gr = r; gy = r->y;
2065 break;
2066 }
2067
2068 if (gr && gy <= y)
2069 height = gr->height;
2070 else
2071 {
2072 /* Use nominal line height at end of window. */
2073 y -= gy;
2074 gy += (y / height) * height;
2075 }
2076 break;
2077
2078 default:
2079 ;
2080 virtual_glyph:
2081 /* If there is no glyph under the mouse, then we divide the screen
2082 into a grid of the smallest glyph in the frame, and use that
2083 as our "glyph". */
2084
2085 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2086 round down even for negative values. */
2087 if (gx < 0)
2088 gx -= width - 1;
2089 if (gy < 0)
2090 gy -= height - 1;
2091
2092 gx = (gx / width) * width;
2093 gy = (gy / height) * height;
2094
2095 goto store_rect;
2096 }
2097
2098 gx += WINDOW_LEFT_EDGE_X (w);
2099 gy += WINDOW_TOP_EDGE_Y (w);
2100
2101 store_rect:
2102 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2103
2104 /* Visible feedback for debugging. */
2105 #if 0
2106 #if HAVE_X_WINDOWS
2107 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2108 f->output_data.x->normal_gc,
2109 gx, gy, width, height);
2110 #endif
2111 #endif
2112 }
2113
2114
2115 #endif /* HAVE_WINDOW_SYSTEM */
2116
2117 \f
2118 /***********************************************************************
2119 Lisp form evaluation
2120 ***********************************************************************/
2121
2122 /* Error handler for safe_eval and safe_call. */
2123
2124 static Lisp_Object
2125 safe_eval_handler (Lisp_Object arg)
2126 {
2127 add_to_log ("Error during redisplay: %S", arg, Qnil);
2128 return Qnil;
2129 }
2130
2131
2132 /* Evaluate SEXPR and return the result, or nil if something went
2133 wrong. Prevent redisplay during the evaluation. */
2134
2135 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2136 Return the result, or nil if something went wrong. Prevent
2137 redisplay during the evaluation. */
2138
2139 Lisp_Object
2140 safe_call (size_t nargs, Lisp_Object *args)
2141 {
2142 Lisp_Object val;
2143
2144 if (inhibit_eval_during_redisplay)
2145 val = Qnil;
2146 else
2147 {
2148 int count = SPECPDL_INDEX ();
2149 struct gcpro gcpro1;
2150
2151 GCPRO1 (args[0]);
2152 gcpro1.nvars = nargs;
2153 specbind (Qinhibit_redisplay, Qt);
2154 /* Use Qt to ensure debugger does not run,
2155 so there is no possibility of wanting to redisplay. */
2156 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2157 safe_eval_handler);
2158 UNGCPRO;
2159 val = unbind_to (count, val);
2160 }
2161
2162 return val;
2163 }
2164
2165
2166 /* Call function FN with one argument ARG.
2167 Return the result, or nil if something went wrong. */
2168
2169 Lisp_Object
2170 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2171 {
2172 Lisp_Object args[2];
2173 args[0] = fn;
2174 args[1] = arg;
2175 return safe_call (2, args);
2176 }
2177
2178 static Lisp_Object Qeval;
2179
2180 Lisp_Object
2181 safe_eval (Lisp_Object sexpr)
2182 {
2183 return safe_call1 (Qeval, sexpr);
2184 }
2185
2186 /* Call function FN with one argument ARG.
2187 Return the result, or nil if something went wrong. */
2188
2189 Lisp_Object
2190 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2191 {
2192 Lisp_Object args[3];
2193 args[0] = fn;
2194 args[1] = arg1;
2195 args[2] = arg2;
2196 return safe_call (3, args);
2197 }
2198
2199
2200 \f
2201 /***********************************************************************
2202 Debugging
2203 ***********************************************************************/
2204
2205 #if 0
2206
2207 /* Define CHECK_IT to perform sanity checks on iterators.
2208 This is for debugging. It is too slow to do unconditionally. */
2209
2210 static void
2211 check_it (it)
2212 struct it *it;
2213 {
2214 if (it->method == GET_FROM_STRING)
2215 {
2216 xassert (STRINGP (it->string));
2217 xassert (IT_STRING_CHARPOS (*it) >= 0);
2218 }
2219 else
2220 {
2221 xassert (IT_STRING_CHARPOS (*it) < 0);
2222 if (it->method == GET_FROM_BUFFER)
2223 {
2224 /* Check that character and byte positions agree. */
2225 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2226 }
2227 }
2228
2229 if (it->dpvec)
2230 xassert (it->current.dpvec_index >= 0);
2231 else
2232 xassert (it->current.dpvec_index < 0);
2233 }
2234
2235 #define CHECK_IT(IT) check_it ((IT))
2236
2237 #else /* not 0 */
2238
2239 #define CHECK_IT(IT) (void) 0
2240
2241 #endif /* not 0 */
2242
2243
2244 #if GLYPH_DEBUG
2245
2246 /* Check that the window end of window W is what we expect it
2247 to be---the last row in the current matrix displaying text. */
2248
2249 static void
2250 check_window_end (w)
2251 struct window *w;
2252 {
2253 if (!MINI_WINDOW_P (w)
2254 && !NILP (w->window_end_valid))
2255 {
2256 struct glyph_row *row;
2257 xassert ((row = MATRIX_ROW (w->current_matrix,
2258 XFASTINT (w->window_end_vpos)),
2259 !row->enabled_p
2260 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2261 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2262 }
2263 }
2264
2265 #define CHECK_WINDOW_END(W) check_window_end ((W))
2266
2267 #else /* not GLYPH_DEBUG */
2268
2269 #define CHECK_WINDOW_END(W) (void) 0
2270
2271 #endif /* not GLYPH_DEBUG */
2272
2273
2274 \f
2275 /***********************************************************************
2276 Iterator initialization
2277 ***********************************************************************/
2278
2279 /* Initialize IT for displaying current_buffer in window W, starting
2280 at character position CHARPOS. CHARPOS < 0 means that no buffer
2281 position is specified which is useful when the iterator is assigned
2282 a position later. BYTEPOS is the byte position corresponding to
2283 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2284
2285 If ROW is not null, calls to produce_glyphs with IT as parameter
2286 will produce glyphs in that row.
2287
2288 BASE_FACE_ID is the id of a base face to use. It must be one of
2289 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2290 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2291 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2292
2293 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2294 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2295 will be initialized to use the corresponding mode line glyph row of
2296 the desired matrix of W. */
2297
2298 void
2299 init_iterator (struct it *it, struct window *w,
2300 EMACS_INT charpos, EMACS_INT bytepos,
2301 struct glyph_row *row, enum face_id base_face_id)
2302 {
2303 int highlight_region_p;
2304 enum face_id remapped_base_face_id = base_face_id;
2305
2306 /* Some precondition checks. */
2307 xassert (w != NULL && it != NULL);
2308 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2309 && charpos <= ZV));
2310
2311 /* If face attributes have been changed since the last redisplay,
2312 free realized faces now because they depend on face definitions
2313 that might have changed. Don't free faces while there might be
2314 desired matrices pending which reference these faces. */
2315 if (face_change_count && !inhibit_free_realized_faces)
2316 {
2317 face_change_count = 0;
2318 free_all_realized_faces (Qnil);
2319 }
2320
2321 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2322 if (! NILP (Vface_remapping_alist))
2323 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2324
2325 /* Use one of the mode line rows of W's desired matrix if
2326 appropriate. */
2327 if (row == NULL)
2328 {
2329 if (base_face_id == MODE_LINE_FACE_ID
2330 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2331 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2332 else if (base_face_id == HEADER_LINE_FACE_ID)
2333 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2334 }
2335
2336 /* Clear IT. */
2337 memset (it, 0, sizeof *it);
2338 it->current.overlay_string_index = -1;
2339 it->current.dpvec_index = -1;
2340 it->base_face_id = remapped_base_face_id;
2341 it->string = Qnil;
2342 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2343
2344 /* The window in which we iterate over current_buffer: */
2345 XSETWINDOW (it->window, w);
2346 it->w = w;
2347 it->f = XFRAME (w->frame);
2348
2349 it->cmp_it.id = -1;
2350
2351 /* Extra space between lines (on window systems only). */
2352 if (base_face_id == DEFAULT_FACE_ID
2353 && FRAME_WINDOW_P (it->f))
2354 {
2355 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2356 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2357 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2358 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2359 * FRAME_LINE_HEIGHT (it->f));
2360 else if (it->f->extra_line_spacing > 0)
2361 it->extra_line_spacing = it->f->extra_line_spacing;
2362 it->max_extra_line_spacing = 0;
2363 }
2364
2365 /* If realized faces have been removed, e.g. because of face
2366 attribute changes of named faces, recompute them. When running
2367 in batch mode, the face cache of the initial frame is null. If
2368 we happen to get called, make a dummy face cache. */
2369 if (FRAME_FACE_CACHE (it->f) == NULL)
2370 init_frame_faces (it->f);
2371 if (FRAME_FACE_CACHE (it->f)->used == 0)
2372 recompute_basic_faces (it->f);
2373
2374 /* Current value of the `slice', `space-width', and 'height' properties. */
2375 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2376 it->space_width = Qnil;
2377 it->font_height = Qnil;
2378 it->override_ascent = -1;
2379
2380 /* Are control characters displayed as `^C'? */
2381 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2382
2383 /* -1 means everything between a CR and the following line end
2384 is invisible. >0 means lines indented more than this value are
2385 invisible. */
2386 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2387 ? XFASTINT (BVAR (current_buffer, selective_display))
2388 : (!NILP (BVAR (current_buffer, selective_display))
2389 ? -1 : 0));
2390 it->selective_display_ellipsis_p
2391 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2392
2393 /* Display table to use. */
2394 it->dp = window_display_table (w);
2395
2396 /* Are multibyte characters enabled in current_buffer? */
2397 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2398
2399 /* Do we need to reorder bidirectional text? Not if this is a
2400 unibyte buffer: by definition, none of the single-byte characters
2401 are strong R2L, so no reordering is needed. And bidi.c doesn't
2402 support unibyte buffers anyway. */
2403 it->bidi_p
2404 = !NILP (BVAR (current_buffer, bidi_display_reordering)) && it->multibyte_p;
2405
2406 /* Non-zero if we should highlight the region. */
2407 highlight_region_p
2408 = (!NILP (Vtransient_mark_mode)
2409 && !NILP (BVAR (current_buffer, mark_active))
2410 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2411
2412 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2413 start and end of a visible region in window IT->w. Set both to
2414 -1 to indicate no region. */
2415 if (highlight_region_p
2416 /* Maybe highlight only in selected window. */
2417 && (/* Either show region everywhere. */
2418 highlight_nonselected_windows
2419 /* Or show region in the selected window. */
2420 || w == XWINDOW (selected_window)
2421 /* Or show the region if we are in the mini-buffer and W is
2422 the window the mini-buffer refers to. */
2423 || (MINI_WINDOW_P (XWINDOW (selected_window))
2424 && WINDOWP (minibuf_selected_window)
2425 && w == XWINDOW (minibuf_selected_window))))
2426 {
2427 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2428 it->region_beg_charpos = min (PT, markpos);
2429 it->region_end_charpos = max (PT, markpos);
2430 }
2431 else
2432 it->region_beg_charpos = it->region_end_charpos = -1;
2433
2434 /* Get the position at which the redisplay_end_trigger hook should
2435 be run, if it is to be run at all. */
2436 if (MARKERP (w->redisplay_end_trigger)
2437 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2438 it->redisplay_end_trigger_charpos
2439 = marker_position (w->redisplay_end_trigger);
2440 else if (INTEGERP (w->redisplay_end_trigger))
2441 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2442
2443 /* Correct bogus values of tab_width. */
2444 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2445 if (it->tab_width <= 0 || it->tab_width > 1000)
2446 it->tab_width = 8;
2447
2448 /* Are lines in the display truncated? */
2449 if (base_face_id != DEFAULT_FACE_ID
2450 || XINT (it->w->hscroll)
2451 || (! WINDOW_FULL_WIDTH_P (it->w)
2452 && ((!NILP (Vtruncate_partial_width_windows)
2453 && !INTEGERP (Vtruncate_partial_width_windows))
2454 || (INTEGERP (Vtruncate_partial_width_windows)
2455 && (WINDOW_TOTAL_COLS (it->w)
2456 < XINT (Vtruncate_partial_width_windows))))))
2457 it->line_wrap = TRUNCATE;
2458 else if (NILP (BVAR (current_buffer, truncate_lines)))
2459 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2460 ? WINDOW_WRAP : WORD_WRAP;
2461 else
2462 it->line_wrap = TRUNCATE;
2463
2464 /* Get dimensions of truncation and continuation glyphs. These are
2465 displayed as fringe bitmaps under X, so we don't need them for such
2466 frames. */
2467 if (!FRAME_WINDOW_P (it->f))
2468 {
2469 if (it->line_wrap == TRUNCATE)
2470 {
2471 /* We will need the truncation glyph. */
2472 xassert (it->glyph_row == NULL);
2473 produce_special_glyphs (it, IT_TRUNCATION);
2474 it->truncation_pixel_width = it->pixel_width;
2475 }
2476 else
2477 {
2478 /* We will need the continuation glyph. */
2479 xassert (it->glyph_row == NULL);
2480 produce_special_glyphs (it, IT_CONTINUATION);
2481 it->continuation_pixel_width = it->pixel_width;
2482 }
2483
2484 /* Reset these values to zero because the produce_special_glyphs
2485 above has changed them. */
2486 it->pixel_width = it->ascent = it->descent = 0;
2487 it->phys_ascent = it->phys_descent = 0;
2488 }
2489
2490 /* Set this after getting the dimensions of truncation and
2491 continuation glyphs, so that we don't produce glyphs when calling
2492 produce_special_glyphs, above. */
2493 it->glyph_row = row;
2494 it->area = TEXT_AREA;
2495
2496 /* Forget any previous info about this row being reversed. */
2497 if (it->glyph_row)
2498 it->glyph_row->reversed_p = 0;
2499
2500 /* Get the dimensions of the display area. The display area
2501 consists of the visible window area plus a horizontally scrolled
2502 part to the left of the window. All x-values are relative to the
2503 start of this total display area. */
2504 if (base_face_id != DEFAULT_FACE_ID)
2505 {
2506 /* Mode lines, menu bar in terminal frames. */
2507 it->first_visible_x = 0;
2508 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2509 }
2510 else
2511 {
2512 it->first_visible_x
2513 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2514 it->last_visible_x = (it->first_visible_x
2515 + window_box_width (w, TEXT_AREA));
2516
2517 /* If we truncate lines, leave room for the truncator glyph(s) at
2518 the right margin. Otherwise, leave room for the continuation
2519 glyph(s). Truncation and continuation glyphs are not inserted
2520 for window-based redisplay. */
2521 if (!FRAME_WINDOW_P (it->f))
2522 {
2523 if (it->line_wrap == TRUNCATE)
2524 it->last_visible_x -= it->truncation_pixel_width;
2525 else
2526 it->last_visible_x -= it->continuation_pixel_width;
2527 }
2528
2529 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2530 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2531 }
2532
2533 /* Leave room for a border glyph. */
2534 if (!FRAME_WINDOW_P (it->f)
2535 && !WINDOW_RIGHTMOST_P (it->w))
2536 it->last_visible_x -= 1;
2537
2538 it->last_visible_y = window_text_bottom_y (w);
2539
2540 /* For mode lines and alike, arrange for the first glyph having a
2541 left box line if the face specifies a box. */
2542 if (base_face_id != DEFAULT_FACE_ID)
2543 {
2544 struct face *face;
2545
2546 it->face_id = remapped_base_face_id;
2547
2548 /* If we have a boxed mode line, make the first character appear
2549 with a left box line. */
2550 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2551 if (face->box != FACE_NO_BOX)
2552 it->start_of_box_run_p = 1;
2553 }
2554
2555 /* If we are to reorder bidirectional text, init the bidi
2556 iterator. */
2557 if (it->bidi_p)
2558 {
2559 /* Note the paragraph direction that this buffer wants to
2560 use. */
2561 if (EQ (BVAR (current_buffer, bidi_paragraph_direction), Qleft_to_right))
2562 it->paragraph_embedding = L2R;
2563 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction), Qright_to_left))
2564 it->paragraph_embedding = R2L;
2565 else
2566 it->paragraph_embedding = NEUTRAL_DIR;
2567 bidi_init_it (charpos, bytepos, &it->bidi_it);
2568 }
2569
2570 /* If a buffer position was specified, set the iterator there,
2571 getting overlays and face properties from that position. */
2572 if (charpos >= BUF_BEG (current_buffer))
2573 {
2574 it->end_charpos = ZV;
2575 it->face_id = -1;
2576 IT_CHARPOS (*it) = charpos;
2577
2578 /* Compute byte position if not specified. */
2579 if (bytepos < charpos)
2580 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2581 else
2582 IT_BYTEPOS (*it) = bytepos;
2583
2584 it->start = it->current;
2585
2586 /* Compute faces etc. */
2587 reseat (it, it->current.pos, 1);
2588 }
2589
2590 CHECK_IT (it);
2591 }
2592
2593
2594 /* Initialize IT for the display of window W with window start POS. */
2595
2596 void
2597 start_display (struct it *it, struct window *w, struct text_pos pos)
2598 {
2599 struct glyph_row *row;
2600 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2601
2602 row = w->desired_matrix->rows + first_vpos;
2603 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2604 it->first_vpos = first_vpos;
2605
2606 /* Don't reseat to previous visible line start if current start
2607 position is in a string or image. */
2608 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2609 {
2610 int start_at_line_beg_p;
2611 int first_y = it->current_y;
2612
2613 /* If window start is not at a line start, skip forward to POS to
2614 get the correct continuation lines width. */
2615 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2616 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2617 if (!start_at_line_beg_p)
2618 {
2619 int new_x;
2620
2621 reseat_at_previous_visible_line_start (it);
2622 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2623
2624 new_x = it->current_x + it->pixel_width;
2625
2626 /* If lines are continued, this line may end in the middle
2627 of a multi-glyph character (e.g. a control character
2628 displayed as \003, or in the middle of an overlay
2629 string). In this case move_it_to above will not have
2630 taken us to the start of the continuation line but to the
2631 end of the continued line. */
2632 if (it->current_x > 0
2633 && it->line_wrap != TRUNCATE /* Lines are continued. */
2634 && (/* And glyph doesn't fit on the line. */
2635 new_x > it->last_visible_x
2636 /* Or it fits exactly and we're on a window
2637 system frame. */
2638 || (new_x == it->last_visible_x
2639 && FRAME_WINDOW_P (it->f))))
2640 {
2641 if (it->current.dpvec_index >= 0
2642 || it->current.overlay_string_index >= 0)
2643 {
2644 set_iterator_to_next (it, 1);
2645 move_it_in_display_line_to (it, -1, -1, 0);
2646 }
2647
2648 it->continuation_lines_width += it->current_x;
2649 }
2650
2651 /* We're starting a new display line, not affected by the
2652 height of the continued line, so clear the appropriate
2653 fields in the iterator structure. */
2654 it->max_ascent = it->max_descent = 0;
2655 it->max_phys_ascent = it->max_phys_descent = 0;
2656
2657 it->current_y = first_y;
2658 it->vpos = 0;
2659 it->current_x = it->hpos = 0;
2660 }
2661 }
2662 }
2663
2664
2665 /* Return 1 if POS is a position in ellipses displayed for invisible
2666 text. W is the window we display, for text property lookup. */
2667
2668 static int
2669 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2670 {
2671 Lisp_Object prop, window;
2672 int ellipses_p = 0;
2673 EMACS_INT charpos = CHARPOS (pos->pos);
2674
2675 /* If POS specifies a position in a display vector, this might
2676 be for an ellipsis displayed for invisible text. We won't
2677 get the iterator set up for delivering that ellipsis unless
2678 we make sure that it gets aware of the invisible text. */
2679 if (pos->dpvec_index >= 0
2680 && pos->overlay_string_index < 0
2681 && CHARPOS (pos->string_pos) < 0
2682 && charpos > BEGV
2683 && (XSETWINDOW (window, w),
2684 prop = Fget_char_property (make_number (charpos),
2685 Qinvisible, window),
2686 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2687 {
2688 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2689 window);
2690 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2691 }
2692
2693 return ellipses_p;
2694 }
2695
2696
2697 /* Initialize IT for stepping through current_buffer in window W,
2698 starting at position POS that includes overlay string and display
2699 vector/ control character translation position information. Value
2700 is zero if there are overlay strings with newlines at POS. */
2701
2702 static int
2703 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2704 {
2705 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2706 int i, overlay_strings_with_newlines = 0;
2707
2708 /* If POS specifies a position in a display vector, this might
2709 be for an ellipsis displayed for invisible text. We won't
2710 get the iterator set up for delivering that ellipsis unless
2711 we make sure that it gets aware of the invisible text. */
2712 if (in_ellipses_for_invisible_text_p (pos, w))
2713 {
2714 --charpos;
2715 bytepos = 0;
2716 }
2717
2718 /* Keep in mind: the call to reseat in init_iterator skips invisible
2719 text, so we might end up at a position different from POS. This
2720 is only a problem when POS is a row start after a newline and an
2721 overlay starts there with an after-string, and the overlay has an
2722 invisible property. Since we don't skip invisible text in
2723 display_line and elsewhere immediately after consuming the
2724 newline before the row start, such a POS will not be in a string,
2725 but the call to init_iterator below will move us to the
2726 after-string. */
2727 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2728
2729 /* This only scans the current chunk -- it should scan all chunks.
2730 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2731 to 16 in 22.1 to make this a lesser problem. */
2732 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2733 {
2734 const char *s = SSDATA (it->overlay_strings[i]);
2735 const char *e = s + SBYTES (it->overlay_strings[i]);
2736
2737 while (s < e && *s != '\n')
2738 ++s;
2739
2740 if (s < e)
2741 {
2742 overlay_strings_with_newlines = 1;
2743 break;
2744 }
2745 }
2746
2747 /* If position is within an overlay string, set up IT to the right
2748 overlay string. */
2749 if (pos->overlay_string_index >= 0)
2750 {
2751 int relative_index;
2752
2753 /* If the first overlay string happens to have a `display'
2754 property for an image, the iterator will be set up for that
2755 image, and we have to undo that setup first before we can
2756 correct the overlay string index. */
2757 if (it->method == GET_FROM_IMAGE)
2758 pop_it (it);
2759
2760 /* We already have the first chunk of overlay strings in
2761 IT->overlay_strings. Load more until the one for
2762 pos->overlay_string_index is in IT->overlay_strings. */
2763 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2764 {
2765 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2766 it->current.overlay_string_index = 0;
2767 while (n--)
2768 {
2769 load_overlay_strings (it, 0);
2770 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2771 }
2772 }
2773
2774 it->current.overlay_string_index = pos->overlay_string_index;
2775 relative_index = (it->current.overlay_string_index
2776 % OVERLAY_STRING_CHUNK_SIZE);
2777 it->string = it->overlay_strings[relative_index];
2778 xassert (STRINGP (it->string));
2779 it->current.string_pos = pos->string_pos;
2780 it->method = GET_FROM_STRING;
2781 }
2782
2783 if (CHARPOS (pos->string_pos) >= 0)
2784 {
2785 /* Recorded position is not in an overlay string, but in another
2786 string. This can only be a string from a `display' property.
2787 IT should already be filled with that string. */
2788 it->current.string_pos = pos->string_pos;
2789 xassert (STRINGP (it->string));
2790 }
2791
2792 /* Restore position in display vector translations, control
2793 character translations or ellipses. */
2794 if (pos->dpvec_index >= 0)
2795 {
2796 if (it->dpvec == NULL)
2797 get_next_display_element (it);
2798 xassert (it->dpvec && it->current.dpvec_index == 0);
2799 it->current.dpvec_index = pos->dpvec_index;
2800 }
2801
2802 CHECK_IT (it);
2803 return !overlay_strings_with_newlines;
2804 }
2805
2806
2807 /* Initialize IT for stepping through current_buffer in window W
2808 starting at ROW->start. */
2809
2810 static void
2811 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2812 {
2813 init_from_display_pos (it, w, &row->start);
2814 it->start = row->start;
2815 it->continuation_lines_width = row->continuation_lines_width;
2816 CHECK_IT (it);
2817 }
2818
2819
2820 /* Initialize IT for stepping through current_buffer in window W
2821 starting in the line following ROW, i.e. starting at ROW->end.
2822 Value is zero if there are overlay strings with newlines at ROW's
2823 end position. */
2824
2825 static int
2826 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2827 {
2828 int success = 0;
2829
2830 if (init_from_display_pos (it, w, &row->end))
2831 {
2832 if (row->continued_p)
2833 it->continuation_lines_width
2834 = row->continuation_lines_width + row->pixel_width;
2835 CHECK_IT (it);
2836 success = 1;
2837 }
2838
2839 return success;
2840 }
2841
2842
2843
2844 \f
2845 /***********************************************************************
2846 Text properties
2847 ***********************************************************************/
2848
2849 /* Called when IT reaches IT->stop_charpos. Handle text property and
2850 overlay changes. Set IT->stop_charpos to the next position where
2851 to stop. */
2852
2853 static void
2854 handle_stop (struct it *it)
2855 {
2856 enum prop_handled handled;
2857 int handle_overlay_change_p;
2858 struct props *p;
2859
2860 it->dpvec = NULL;
2861 it->current.dpvec_index = -1;
2862 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2863 it->ignore_overlay_strings_at_pos_p = 0;
2864 it->ellipsis_p = 0;
2865
2866 /* Use face of preceding text for ellipsis (if invisible) */
2867 if (it->selective_display_ellipsis_p)
2868 it->saved_face_id = it->face_id;
2869
2870 do
2871 {
2872 handled = HANDLED_NORMALLY;
2873
2874 /* Call text property handlers. */
2875 for (p = it_props; p->handler; ++p)
2876 {
2877 handled = p->handler (it);
2878
2879 if (handled == HANDLED_RECOMPUTE_PROPS)
2880 break;
2881 else if (handled == HANDLED_RETURN)
2882 {
2883 /* We still want to show before and after strings from
2884 overlays even if the actual buffer text is replaced. */
2885 if (!handle_overlay_change_p
2886 || it->sp > 1
2887 || !get_overlay_strings_1 (it, 0, 0))
2888 {
2889 if (it->ellipsis_p)
2890 setup_for_ellipsis (it, 0);
2891 /* When handling a display spec, we might load an
2892 empty string. In that case, discard it here. We
2893 used to discard it in handle_single_display_spec,
2894 but that causes get_overlay_strings_1, above, to
2895 ignore overlay strings that we must check. */
2896 if (STRINGP (it->string) && !SCHARS (it->string))
2897 pop_it (it);
2898 return;
2899 }
2900 else if (STRINGP (it->string) && !SCHARS (it->string))
2901 pop_it (it);
2902 else
2903 {
2904 it->ignore_overlay_strings_at_pos_p = 1;
2905 it->string_from_display_prop_p = 0;
2906 handle_overlay_change_p = 0;
2907 }
2908 handled = HANDLED_RECOMPUTE_PROPS;
2909 break;
2910 }
2911 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2912 handle_overlay_change_p = 0;
2913 }
2914
2915 if (handled != HANDLED_RECOMPUTE_PROPS)
2916 {
2917 /* Don't check for overlay strings below when set to deliver
2918 characters from a display vector. */
2919 if (it->method == GET_FROM_DISPLAY_VECTOR)
2920 handle_overlay_change_p = 0;
2921
2922 /* Handle overlay changes.
2923 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2924 if it finds overlays. */
2925 if (handle_overlay_change_p)
2926 handled = handle_overlay_change (it);
2927 }
2928
2929 if (it->ellipsis_p)
2930 {
2931 setup_for_ellipsis (it, 0);
2932 break;
2933 }
2934 }
2935 while (handled == HANDLED_RECOMPUTE_PROPS);
2936
2937 /* Determine where to stop next. */
2938 if (handled == HANDLED_NORMALLY)
2939 compute_stop_pos (it);
2940 }
2941
2942
2943 /* Compute IT->stop_charpos from text property and overlay change
2944 information for IT's current position. */
2945
2946 static void
2947 compute_stop_pos (struct it *it)
2948 {
2949 register INTERVAL iv, next_iv;
2950 Lisp_Object object, limit, position;
2951 EMACS_INT charpos, bytepos;
2952
2953 /* If nowhere else, stop at the end. */
2954 it->stop_charpos = it->end_charpos;
2955
2956 if (STRINGP (it->string))
2957 {
2958 /* Strings are usually short, so don't limit the search for
2959 properties. */
2960 object = it->string;
2961 limit = Qnil;
2962 charpos = IT_STRING_CHARPOS (*it);
2963 bytepos = IT_STRING_BYTEPOS (*it);
2964 }
2965 else
2966 {
2967 EMACS_INT pos;
2968
2969 /* If next overlay change is in front of the current stop pos
2970 (which is IT->end_charpos), stop there. Note: value of
2971 next_overlay_change is point-max if no overlay change
2972 follows. */
2973 charpos = IT_CHARPOS (*it);
2974 bytepos = IT_BYTEPOS (*it);
2975 pos = next_overlay_change (charpos);
2976 if (pos < it->stop_charpos)
2977 it->stop_charpos = pos;
2978
2979 /* If showing the region, we have to stop at the region
2980 start or end because the face might change there. */
2981 if (it->region_beg_charpos > 0)
2982 {
2983 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2984 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2985 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2986 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2987 }
2988
2989 /* Set up variables for computing the stop position from text
2990 property changes. */
2991 XSETBUFFER (object, current_buffer);
2992 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2993 }
2994
2995 /* Get the interval containing IT's position. Value is a null
2996 interval if there isn't such an interval. */
2997 position = make_number (charpos);
2998 iv = validate_interval_range (object, &position, &position, 0);
2999 if (!NULL_INTERVAL_P (iv))
3000 {
3001 Lisp_Object values_here[LAST_PROP_IDX];
3002 struct props *p;
3003
3004 /* Get properties here. */
3005 for (p = it_props; p->handler; ++p)
3006 values_here[p->idx] = textget (iv->plist, *p->name);
3007
3008 /* Look for an interval following iv that has different
3009 properties. */
3010 for (next_iv = next_interval (iv);
3011 (!NULL_INTERVAL_P (next_iv)
3012 && (NILP (limit)
3013 || XFASTINT (limit) > next_iv->position));
3014 next_iv = next_interval (next_iv))
3015 {
3016 for (p = it_props; p->handler; ++p)
3017 {
3018 Lisp_Object new_value;
3019
3020 new_value = textget (next_iv->plist, *p->name);
3021 if (!EQ (values_here[p->idx], new_value))
3022 break;
3023 }
3024
3025 if (p->handler)
3026 break;
3027 }
3028
3029 if (!NULL_INTERVAL_P (next_iv))
3030 {
3031 if (INTEGERP (limit)
3032 && next_iv->position >= XFASTINT (limit))
3033 /* No text property change up to limit. */
3034 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3035 else
3036 /* Text properties change in next_iv. */
3037 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3038 }
3039 }
3040
3041 if (it->cmp_it.id < 0)
3042 {
3043 EMACS_INT stoppos = it->end_charpos;
3044
3045 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3046 stoppos = -1;
3047 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3048 stoppos, it->string);
3049 }
3050
3051 xassert (STRINGP (it->string)
3052 || (it->stop_charpos >= BEGV
3053 && it->stop_charpos >= IT_CHARPOS (*it)));
3054 }
3055
3056
3057 /* Return the position of the next overlay change after POS in
3058 current_buffer. Value is point-max if no overlay change
3059 follows. This is like `next-overlay-change' but doesn't use
3060 xmalloc. */
3061
3062 static EMACS_INT
3063 next_overlay_change (EMACS_INT pos)
3064 {
3065 int noverlays;
3066 EMACS_INT endpos;
3067 Lisp_Object *overlays;
3068 int i;
3069
3070 /* Get all overlays at the given position. */
3071 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3072
3073 /* If any of these overlays ends before endpos,
3074 use its ending point instead. */
3075 for (i = 0; i < noverlays; ++i)
3076 {
3077 Lisp_Object oend;
3078 EMACS_INT oendpos;
3079
3080 oend = OVERLAY_END (overlays[i]);
3081 oendpos = OVERLAY_POSITION (oend);
3082 endpos = min (endpos, oendpos);
3083 }
3084
3085 return endpos;
3086 }
3087
3088 /* Return the character position of a display string at or after CHARPOS.
3089 If no display string exist at or after CHARPOS, return ZV. A
3090 display string is either an overlay with `display' property whose
3091 value is a string or a `display' text property whose value is a
3092 string. */
3093 EMACS_INT
3094 compute_display_string_pos (EMACS_INT charpos)
3095 {
3096 /* FIXME: Support display properties on strings (object = Qnil means
3097 current buffer). */
3098 Lisp_Object object = Qnil;
3099 Lisp_Object pos = make_number (charpos);
3100
3101 if (charpos >= ZV)
3102 return ZV;
3103
3104 /* If the character at CHARPOS is where the display string begins,
3105 return CHARPOS. */
3106 if (!NILP (Fget_char_property (pos, Qdisplay, object))
3107 && (charpos <= BEGV
3108 || NILP (Fget_char_property (make_number (charpos - 1), Qdisplay,
3109 object))))
3110 return charpos;
3111
3112 /* Look forward for the first character where the `display' property
3113 changes from nil to non-nil. */
3114 do {
3115 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3116 } while (XFASTINT (pos) < ZV
3117 && NILP (Fget_char_property (pos, Qdisplay, object)));
3118
3119 return XFASTINT (pos);
3120 }
3121
3122 /* Return the character position of the end of the display string that
3123 started at CHARPOS. A display string is either an overlay with
3124 `display' property whose value is a string or a `display' text
3125 property whose value is a string. */
3126 EMACS_INT
3127 compute_display_string_end (EMACS_INT charpos)
3128 {
3129 /* FIXME: Support display properties on strings (object = Qnil means
3130 current buffer). */
3131 Lisp_Object object = Qnil;
3132 Lisp_Object pos = make_number (charpos);
3133
3134 if (charpos >= ZV)
3135 return ZV;
3136
3137 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3138 abort ();
3139
3140 /* Look forward for the first character where the `display' property
3141 changes. */
3142 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3143
3144 return XFASTINT (pos);
3145 }
3146
3147
3148 \f
3149 /***********************************************************************
3150 Fontification
3151 ***********************************************************************/
3152
3153 /* Handle changes in the `fontified' property of the current buffer by
3154 calling hook functions from Qfontification_functions to fontify
3155 regions of text. */
3156
3157 static enum prop_handled
3158 handle_fontified_prop (struct it *it)
3159 {
3160 Lisp_Object prop, pos;
3161 enum prop_handled handled = HANDLED_NORMALLY;
3162
3163 if (!NILP (Vmemory_full))
3164 return handled;
3165
3166 /* Get the value of the `fontified' property at IT's current buffer
3167 position. (The `fontified' property doesn't have a special
3168 meaning in strings.) If the value is nil, call functions from
3169 Qfontification_functions. */
3170 if (!STRINGP (it->string)
3171 && it->s == NULL
3172 && !NILP (Vfontification_functions)
3173 && !NILP (Vrun_hooks)
3174 && (pos = make_number (IT_CHARPOS (*it)),
3175 prop = Fget_char_property (pos, Qfontified, Qnil),
3176 /* Ignore the special cased nil value always present at EOB since
3177 no amount of fontifying will be able to change it. */
3178 NILP (prop) && IT_CHARPOS (*it) < Z))
3179 {
3180 int count = SPECPDL_INDEX ();
3181 Lisp_Object val;
3182 struct buffer *obuf = current_buffer;
3183 int begv = BEGV, zv = ZV;
3184 int old_clip_changed = current_buffer->clip_changed;
3185
3186 val = Vfontification_functions;
3187 specbind (Qfontification_functions, Qnil);
3188
3189 xassert (it->end_charpos == ZV);
3190
3191 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3192 safe_call1 (val, pos);
3193 else
3194 {
3195 Lisp_Object fns, fn;
3196 struct gcpro gcpro1, gcpro2;
3197
3198 fns = Qnil;
3199 GCPRO2 (val, fns);
3200
3201 for (; CONSP (val); val = XCDR (val))
3202 {
3203 fn = XCAR (val);
3204
3205 if (EQ (fn, Qt))
3206 {
3207 /* A value of t indicates this hook has a local
3208 binding; it means to run the global binding too.
3209 In a global value, t should not occur. If it
3210 does, we must ignore it to avoid an endless
3211 loop. */
3212 for (fns = Fdefault_value (Qfontification_functions);
3213 CONSP (fns);
3214 fns = XCDR (fns))
3215 {
3216 fn = XCAR (fns);
3217 if (!EQ (fn, Qt))
3218 safe_call1 (fn, pos);
3219 }
3220 }
3221 else
3222 safe_call1 (fn, pos);
3223 }
3224
3225 UNGCPRO;
3226 }
3227
3228 unbind_to (count, Qnil);
3229
3230 /* Fontification functions routinely call `save-restriction'.
3231 Normally, this tags clip_changed, which can confuse redisplay
3232 (see discussion in Bug#6671). Since we don't perform any
3233 special handling of fontification changes in the case where
3234 `save-restriction' isn't called, there's no point doing so in
3235 this case either. So, if the buffer's restrictions are
3236 actually left unchanged, reset clip_changed. */
3237 if (obuf == current_buffer)
3238 {
3239 if (begv == BEGV && zv == ZV)
3240 current_buffer->clip_changed = old_clip_changed;
3241 }
3242 /* There isn't much we can reasonably do to protect against
3243 misbehaving fontification, but here's a fig leaf. */
3244 else if (!NILP (BVAR (obuf, name)))
3245 set_buffer_internal_1 (obuf);
3246
3247 /* The fontification code may have added/removed text.
3248 It could do even a lot worse, but let's at least protect against
3249 the most obvious case where only the text past `pos' gets changed',
3250 as is/was done in grep.el where some escapes sequences are turned
3251 into face properties (bug#7876). */
3252 it->end_charpos = ZV;
3253
3254 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3255 something. This avoids an endless loop if they failed to
3256 fontify the text for which reason ever. */
3257 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3258 handled = HANDLED_RECOMPUTE_PROPS;
3259 }
3260
3261 return handled;
3262 }
3263
3264
3265 \f
3266 /***********************************************************************
3267 Faces
3268 ***********************************************************************/
3269
3270 /* Set up iterator IT from face properties at its current position.
3271 Called from handle_stop. */
3272
3273 static enum prop_handled
3274 handle_face_prop (struct it *it)
3275 {
3276 int new_face_id;
3277 EMACS_INT next_stop;
3278
3279 if (!STRINGP (it->string))
3280 {
3281 new_face_id
3282 = face_at_buffer_position (it->w,
3283 IT_CHARPOS (*it),
3284 it->region_beg_charpos,
3285 it->region_end_charpos,
3286 &next_stop,
3287 (IT_CHARPOS (*it)
3288 + TEXT_PROP_DISTANCE_LIMIT),
3289 0, it->base_face_id);
3290
3291 /* Is this a start of a run of characters with box face?
3292 Caveat: this can be called for a freshly initialized
3293 iterator; face_id is -1 in this case. We know that the new
3294 face will not change until limit, i.e. if the new face has a
3295 box, all characters up to limit will have one. But, as
3296 usual, we don't know whether limit is really the end. */
3297 if (new_face_id != it->face_id)
3298 {
3299 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3300
3301 /* If new face has a box but old face has not, this is
3302 the start of a run of characters with box, i.e. it has
3303 a shadow on the left side. The value of face_id of the
3304 iterator will be -1 if this is the initial call that gets
3305 the face. In this case, we have to look in front of IT's
3306 position and see whether there is a face != new_face_id. */
3307 it->start_of_box_run_p
3308 = (new_face->box != FACE_NO_BOX
3309 && (it->face_id >= 0
3310 || IT_CHARPOS (*it) == BEG
3311 || new_face_id != face_before_it_pos (it)));
3312 it->face_box_p = new_face->box != FACE_NO_BOX;
3313 }
3314 }
3315 else
3316 {
3317 int base_face_id;
3318 EMACS_INT bufpos;
3319 int i;
3320 Lisp_Object from_overlay
3321 = (it->current.overlay_string_index >= 0
3322 ? it->string_overlays[it->current.overlay_string_index]
3323 : Qnil);
3324
3325 /* See if we got to this string directly or indirectly from
3326 an overlay property. That includes the before-string or
3327 after-string of an overlay, strings in display properties
3328 provided by an overlay, their text properties, etc.
3329
3330 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3331 if (! NILP (from_overlay))
3332 for (i = it->sp - 1; i >= 0; i--)
3333 {
3334 if (it->stack[i].current.overlay_string_index >= 0)
3335 from_overlay
3336 = it->string_overlays[it->stack[i].current.overlay_string_index];
3337 else if (! NILP (it->stack[i].from_overlay))
3338 from_overlay = it->stack[i].from_overlay;
3339
3340 if (!NILP (from_overlay))
3341 break;
3342 }
3343
3344 if (! NILP (from_overlay))
3345 {
3346 bufpos = IT_CHARPOS (*it);
3347 /* For a string from an overlay, the base face depends
3348 only on text properties and ignores overlays. */
3349 base_face_id
3350 = face_for_overlay_string (it->w,
3351 IT_CHARPOS (*it),
3352 it->region_beg_charpos,
3353 it->region_end_charpos,
3354 &next_stop,
3355 (IT_CHARPOS (*it)
3356 + TEXT_PROP_DISTANCE_LIMIT),
3357 0,
3358 from_overlay);
3359 }
3360 else
3361 {
3362 bufpos = 0;
3363
3364 /* For strings from a `display' property, use the face at
3365 IT's current buffer position as the base face to merge
3366 with, so that overlay strings appear in the same face as
3367 surrounding text, unless they specify their own
3368 faces. */
3369 base_face_id = underlying_face_id (it);
3370 }
3371
3372 new_face_id = face_at_string_position (it->w,
3373 it->string,
3374 IT_STRING_CHARPOS (*it),
3375 bufpos,
3376 it->region_beg_charpos,
3377 it->region_end_charpos,
3378 &next_stop,
3379 base_face_id, 0);
3380
3381 /* Is this a start of a run of characters with box? Caveat:
3382 this can be called for a freshly allocated iterator; face_id
3383 is -1 is this case. We know that the new face will not
3384 change until the next check pos, i.e. if the new face has a
3385 box, all characters up to that position will have a
3386 box. But, as usual, we don't know whether that position
3387 is really the end. */
3388 if (new_face_id != it->face_id)
3389 {
3390 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3391 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3392
3393 /* If new face has a box but old face hasn't, this is the
3394 start of a run of characters with box, i.e. it has a
3395 shadow on the left side. */
3396 it->start_of_box_run_p
3397 = new_face->box && (old_face == NULL || !old_face->box);
3398 it->face_box_p = new_face->box != FACE_NO_BOX;
3399 }
3400 }
3401
3402 it->face_id = new_face_id;
3403 return HANDLED_NORMALLY;
3404 }
3405
3406
3407 /* Return the ID of the face ``underlying'' IT's current position,
3408 which is in a string. If the iterator is associated with a
3409 buffer, return the face at IT's current buffer position.
3410 Otherwise, use the iterator's base_face_id. */
3411
3412 static int
3413 underlying_face_id (struct it *it)
3414 {
3415 int face_id = it->base_face_id, i;
3416
3417 xassert (STRINGP (it->string));
3418
3419 for (i = it->sp - 1; i >= 0; --i)
3420 if (NILP (it->stack[i].string))
3421 face_id = it->stack[i].face_id;
3422
3423 return face_id;
3424 }
3425
3426
3427 /* Compute the face one character before or after the current position
3428 of IT. BEFORE_P non-zero means get the face in front of IT's
3429 position. Value is the id of the face. */
3430
3431 static int
3432 face_before_or_after_it_pos (struct it *it, int before_p)
3433 {
3434 int face_id, limit;
3435 EMACS_INT next_check_charpos;
3436 struct text_pos pos;
3437
3438 xassert (it->s == NULL);
3439
3440 if (STRINGP (it->string))
3441 {
3442 EMACS_INT bufpos;
3443 int base_face_id;
3444
3445 /* No face change past the end of the string (for the case
3446 we are padding with spaces). No face change before the
3447 string start. */
3448 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3449 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3450 return it->face_id;
3451
3452 /* Set pos to the position before or after IT's current position. */
3453 if (before_p)
3454 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3455 else
3456 /* For composition, we must check the character after the
3457 composition. */
3458 pos = (it->what == IT_COMPOSITION
3459 ? string_pos (IT_STRING_CHARPOS (*it)
3460 + it->cmp_it.nchars, it->string)
3461 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3462
3463 if (it->current.overlay_string_index >= 0)
3464 bufpos = IT_CHARPOS (*it);
3465 else
3466 bufpos = 0;
3467
3468 base_face_id = underlying_face_id (it);
3469
3470 /* Get the face for ASCII, or unibyte. */
3471 face_id = face_at_string_position (it->w,
3472 it->string,
3473 CHARPOS (pos),
3474 bufpos,
3475 it->region_beg_charpos,
3476 it->region_end_charpos,
3477 &next_check_charpos,
3478 base_face_id, 0);
3479
3480 /* Correct the face for charsets different from ASCII. Do it
3481 for the multibyte case only. The face returned above is
3482 suitable for unibyte text if IT->string is unibyte. */
3483 if (STRING_MULTIBYTE (it->string))
3484 {
3485 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3486 int c, len;
3487 struct face *face = FACE_FROM_ID (it->f, face_id);
3488
3489 c = string_char_and_length (p, &len);
3490 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3491 }
3492 }
3493 else
3494 {
3495 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3496 || (IT_CHARPOS (*it) <= BEGV && before_p))
3497 return it->face_id;
3498
3499 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3500 pos = it->current.pos;
3501
3502 if (before_p)
3503 DEC_TEXT_POS (pos, it->multibyte_p);
3504 else
3505 {
3506 if (it->what == IT_COMPOSITION)
3507 /* For composition, we must check the position after the
3508 composition. */
3509 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3510 else
3511 INC_TEXT_POS (pos, it->multibyte_p);
3512 }
3513
3514 /* Determine face for CHARSET_ASCII, or unibyte. */
3515 face_id = face_at_buffer_position (it->w,
3516 CHARPOS (pos),
3517 it->region_beg_charpos,
3518 it->region_end_charpos,
3519 &next_check_charpos,
3520 limit, 0, -1);
3521
3522 /* Correct the face for charsets different from ASCII. Do it
3523 for the multibyte case only. The face returned above is
3524 suitable for unibyte text if current_buffer is unibyte. */
3525 if (it->multibyte_p)
3526 {
3527 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3528 struct face *face = FACE_FROM_ID (it->f, face_id);
3529 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3530 }
3531 }
3532
3533 return face_id;
3534 }
3535
3536
3537 \f
3538 /***********************************************************************
3539 Invisible text
3540 ***********************************************************************/
3541
3542 /* Set up iterator IT from invisible properties at its current
3543 position. Called from handle_stop. */
3544
3545 static enum prop_handled
3546 handle_invisible_prop (struct it *it)
3547 {
3548 enum prop_handled handled = HANDLED_NORMALLY;
3549
3550 if (STRINGP (it->string))
3551 {
3552 Lisp_Object prop, end_charpos, limit, charpos;
3553
3554 /* Get the value of the invisible text property at the
3555 current position. Value will be nil if there is no such
3556 property. */
3557 charpos = make_number (IT_STRING_CHARPOS (*it));
3558 prop = Fget_text_property (charpos, Qinvisible, it->string);
3559
3560 if (!NILP (prop)
3561 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3562 {
3563 handled = HANDLED_RECOMPUTE_PROPS;
3564
3565 /* Get the position at which the next change of the
3566 invisible text property can be found in IT->string.
3567 Value will be nil if the property value is the same for
3568 all the rest of IT->string. */
3569 XSETINT (limit, SCHARS (it->string));
3570 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3571 it->string, limit);
3572
3573 /* Text at current position is invisible. The next
3574 change in the property is at position end_charpos.
3575 Move IT's current position to that position. */
3576 if (INTEGERP (end_charpos)
3577 && XFASTINT (end_charpos) < XFASTINT (limit))
3578 {
3579 struct text_pos old;
3580 old = it->current.string_pos;
3581 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3582 compute_string_pos (&it->current.string_pos, old, it->string);
3583 }
3584 else
3585 {
3586 /* The rest of the string is invisible. If this is an
3587 overlay string, proceed with the next overlay string
3588 or whatever comes and return a character from there. */
3589 if (it->current.overlay_string_index >= 0)
3590 {
3591 next_overlay_string (it);
3592 /* Don't check for overlay strings when we just
3593 finished processing them. */
3594 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3595 }
3596 else
3597 {
3598 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3599 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3600 }
3601 }
3602 }
3603 }
3604 else
3605 {
3606 int invis_p;
3607 EMACS_INT newpos, next_stop, start_charpos, tem;
3608 Lisp_Object pos, prop, overlay;
3609
3610 /* First of all, is there invisible text at this position? */
3611 tem = start_charpos = IT_CHARPOS (*it);
3612 pos = make_number (tem);
3613 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3614 &overlay);
3615 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3616
3617 /* If we are on invisible text, skip over it. */
3618 if (invis_p && start_charpos < it->end_charpos)
3619 {
3620 /* Record whether we have to display an ellipsis for the
3621 invisible text. */
3622 int display_ellipsis_p = invis_p == 2;
3623
3624 handled = HANDLED_RECOMPUTE_PROPS;
3625
3626 /* Loop skipping over invisible text. The loop is left at
3627 ZV or with IT on the first char being visible again. */
3628 do
3629 {
3630 /* Try to skip some invisible text. Return value is the
3631 position reached which can be equal to where we start
3632 if there is nothing invisible there. This skips both
3633 over invisible text properties and overlays with
3634 invisible property. */
3635 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3636
3637 /* If we skipped nothing at all we weren't at invisible
3638 text in the first place. If everything to the end of
3639 the buffer was skipped, end the loop. */
3640 if (newpos == tem || newpos >= ZV)
3641 invis_p = 0;
3642 else
3643 {
3644 /* We skipped some characters but not necessarily
3645 all there are. Check if we ended up on visible
3646 text. Fget_char_property returns the property of
3647 the char before the given position, i.e. if we
3648 get invis_p = 0, this means that the char at
3649 newpos is visible. */
3650 pos = make_number (newpos);
3651 prop = Fget_char_property (pos, Qinvisible, it->window);
3652 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3653 }
3654
3655 /* If we ended up on invisible text, proceed to
3656 skip starting with next_stop. */
3657 if (invis_p)
3658 tem = next_stop;
3659
3660 /* If there are adjacent invisible texts, don't lose the
3661 second one's ellipsis. */
3662 if (invis_p == 2)
3663 display_ellipsis_p = 1;
3664 }
3665 while (invis_p);
3666
3667 /* The position newpos is now either ZV or on visible text. */
3668 if (it->bidi_p && newpos < ZV)
3669 {
3670 /* With bidi iteration, the region of invisible text
3671 could start and/or end in the middle of a non-base
3672 embedding level. Therefore, we need to skip
3673 invisible text using the bidi iterator, starting at
3674 IT's current position, until we find ourselves
3675 outside the invisible text. Skipping invisible text
3676 _after_ bidi iteration avoids affecting the visual
3677 order of the displayed text when invisible properties
3678 are added or removed. */
3679 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3680 {
3681 /* If we were `reseat'ed to a new paragraph,
3682 determine the paragraph base direction. We need
3683 to do it now because next_element_from_buffer may
3684 not have a chance to do it, if we are going to
3685 skip any text at the beginning, which resets the
3686 FIRST_ELT flag. */
3687 bidi_paragraph_init (it->paragraph_embedding,
3688 &it->bidi_it, 1);
3689 }
3690 do
3691 {
3692 bidi_move_to_visually_next (&it->bidi_it);
3693 }
3694 while (it->stop_charpos <= it->bidi_it.charpos
3695 && it->bidi_it.charpos < newpos);
3696 IT_CHARPOS (*it) = it->bidi_it.charpos;
3697 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3698 /* If we overstepped NEWPOS, record its position in the
3699 iterator, so that we skip invisible text if later the
3700 bidi iteration lands us in the invisible region
3701 again. */
3702 if (IT_CHARPOS (*it) >= newpos)
3703 it->prev_stop = newpos;
3704 }
3705 else
3706 {
3707 IT_CHARPOS (*it) = newpos;
3708 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3709 }
3710
3711 /* If there are before-strings at the start of invisible
3712 text, and the text is invisible because of a text
3713 property, arrange to show before-strings because 20.x did
3714 it that way. (If the text is invisible because of an
3715 overlay property instead of a text property, this is
3716 already handled in the overlay code.) */
3717 if (NILP (overlay)
3718 && get_overlay_strings (it, it->stop_charpos))
3719 {
3720 handled = HANDLED_RECOMPUTE_PROPS;
3721 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3722 }
3723 else if (display_ellipsis_p)
3724 {
3725 /* Make sure that the glyphs of the ellipsis will get
3726 correct `charpos' values. If we would not update
3727 it->position here, the glyphs would belong to the
3728 last visible character _before_ the invisible
3729 text, which confuses `set_cursor_from_row'.
3730
3731 We use the last invisible position instead of the
3732 first because this way the cursor is always drawn on
3733 the first "." of the ellipsis, whenever PT is inside
3734 the invisible text. Otherwise the cursor would be
3735 placed _after_ the ellipsis when the point is after the
3736 first invisible character. */
3737 if (!STRINGP (it->object))
3738 {
3739 it->position.charpos = newpos - 1;
3740 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3741 }
3742 it->ellipsis_p = 1;
3743 /* Let the ellipsis display before
3744 considering any properties of the following char.
3745 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3746 handled = HANDLED_RETURN;
3747 }
3748 }
3749 }
3750
3751 return handled;
3752 }
3753
3754
3755 /* Make iterator IT return `...' next.
3756 Replaces LEN characters from buffer. */
3757
3758 static void
3759 setup_for_ellipsis (struct it *it, int len)
3760 {
3761 /* Use the display table definition for `...'. Invalid glyphs
3762 will be handled by the method returning elements from dpvec. */
3763 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3764 {
3765 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3766 it->dpvec = v->contents;
3767 it->dpend = v->contents + v->header.size;
3768 }
3769 else
3770 {
3771 /* Default `...'. */
3772 it->dpvec = default_invis_vector;
3773 it->dpend = default_invis_vector + 3;
3774 }
3775
3776 it->dpvec_char_len = len;
3777 it->current.dpvec_index = 0;
3778 it->dpvec_face_id = -1;
3779
3780 /* Remember the current face id in case glyphs specify faces.
3781 IT's face is restored in set_iterator_to_next.
3782 saved_face_id was set to preceding char's face in handle_stop. */
3783 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3784 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3785
3786 it->method = GET_FROM_DISPLAY_VECTOR;
3787 it->ellipsis_p = 1;
3788 }
3789
3790
3791 \f
3792 /***********************************************************************
3793 'display' property
3794 ***********************************************************************/
3795
3796 /* Set up iterator IT from `display' property at its current position.
3797 Called from handle_stop.
3798 We return HANDLED_RETURN if some part of the display property
3799 overrides the display of the buffer text itself.
3800 Otherwise we return HANDLED_NORMALLY. */
3801
3802 static enum prop_handled
3803 handle_display_prop (struct it *it)
3804 {
3805 Lisp_Object prop, object, overlay;
3806 struct text_pos *position;
3807 /* Nonzero if some property replaces the display of the text itself. */
3808 int display_replaced_p = 0;
3809
3810 if (STRINGP (it->string))
3811 {
3812 object = it->string;
3813 position = &it->current.string_pos;
3814 }
3815 else
3816 {
3817 XSETWINDOW (object, it->w);
3818 position = &it->current.pos;
3819 }
3820
3821 /* Reset those iterator values set from display property values. */
3822 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3823 it->space_width = Qnil;
3824 it->font_height = Qnil;
3825 it->voffset = 0;
3826
3827 /* We don't support recursive `display' properties, i.e. string
3828 values that have a string `display' property, that have a string
3829 `display' property etc. */
3830 if (!it->string_from_display_prop_p)
3831 it->area = TEXT_AREA;
3832
3833 prop = get_char_property_and_overlay (make_number (position->charpos),
3834 Qdisplay, object, &overlay);
3835 if (NILP (prop))
3836 return HANDLED_NORMALLY;
3837 /* Now OVERLAY is the overlay that gave us this property, or nil
3838 if it was a text property. */
3839
3840 if (!STRINGP (it->string))
3841 object = it->w->buffer;
3842
3843 if (CONSP (prop)
3844 /* Simple properties. */
3845 && !EQ (XCAR (prop), Qimage)
3846 && !EQ (XCAR (prop), Qspace)
3847 && !EQ (XCAR (prop), Qwhen)
3848 && !EQ (XCAR (prop), Qslice)
3849 && !EQ (XCAR (prop), Qspace_width)
3850 && !EQ (XCAR (prop), Qheight)
3851 && !EQ (XCAR (prop), Qraise)
3852 /* Marginal area specifications. */
3853 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3854 && !EQ (XCAR (prop), Qleft_fringe)
3855 && !EQ (XCAR (prop), Qright_fringe)
3856 && !NILP (XCAR (prop)))
3857 {
3858 for (; CONSP (prop); prop = XCDR (prop))
3859 {
3860 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3861 position, display_replaced_p))
3862 {
3863 display_replaced_p = 1;
3864 /* If some text in a string is replaced, `position' no
3865 longer points to the position of `object'. */
3866 if (STRINGP (object))
3867 break;
3868 }
3869 }
3870 }
3871 else if (VECTORP (prop))
3872 {
3873 int i;
3874 for (i = 0; i < ASIZE (prop); ++i)
3875 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3876 position, display_replaced_p))
3877 {
3878 display_replaced_p = 1;
3879 /* If some text in a string is replaced, `position' no
3880 longer points to the position of `object'. */
3881 if (STRINGP (object))
3882 break;
3883 }
3884 }
3885 else
3886 {
3887 if (handle_single_display_spec (it, prop, object, overlay,
3888 position, 0))
3889 display_replaced_p = 1;
3890 }
3891
3892 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3893 }
3894
3895
3896 /* Value is the position of the end of the `display' property starting
3897 at START_POS in OBJECT. */
3898
3899 static struct text_pos
3900 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
3901 {
3902 Lisp_Object end;
3903 struct text_pos end_pos;
3904
3905 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3906 Qdisplay, object, Qnil);
3907 CHARPOS (end_pos) = XFASTINT (end);
3908 if (STRINGP (object))
3909 compute_string_pos (&end_pos, start_pos, it->string);
3910 else
3911 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3912
3913 return end_pos;
3914 }
3915
3916
3917 /* Set up IT from a single `display' property specification SPEC. OBJECT
3918 is the object in which the `display' property was found. *POSITION
3919 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3920 means that we previously saw a display specification which already
3921 replaced text display with something else, for example an image;
3922 we ignore such properties after the first one has been processed.
3923
3924 OVERLAY is the overlay this `display' property came from,
3925 or nil if it was a text property.
3926
3927 If SPEC is a `space' or `image' specification, and in some other
3928 cases too, set *POSITION to the position where the `display'
3929 property ends.
3930
3931 Value is non-zero if something was found which replaces the display
3932 of buffer or string text. */
3933
3934 static int
3935 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
3936 Lisp_Object overlay, struct text_pos *position,
3937 int display_replaced_p)
3938 {
3939 Lisp_Object form;
3940 Lisp_Object location, value;
3941 struct text_pos start_pos, save_pos;
3942 int valid_p;
3943
3944 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3945 If the result is non-nil, use VALUE instead of SPEC. */
3946 form = Qt;
3947 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3948 {
3949 spec = XCDR (spec);
3950 if (!CONSP (spec))
3951 return 0;
3952 form = XCAR (spec);
3953 spec = XCDR (spec);
3954 }
3955
3956 if (!NILP (form) && !EQ (form, Qt))
3957 {
3958 int count = SPECPDL_INDEX ();
3959 struct gcpro gcpro1;
3960
3961 /* Bind `object' to the object having the `display' property, a
3962 buffer or string. Bind `position' to the position in the
3963 object where the property was found, and `buffer-position'
3964 to the current position in the buffer. */
3965 specbind (Qobject, object);
3966 specbind (Qposition, make_number (CHARPOS (*position)));
3967 specbind (Qbuffer_position,
3968 make_number (STRINGP (object)
3969 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3970 GCPRO1 (form);
3971 form = safe_eval (form);
3972 UNGCPRO;
3973 unbind_to (count, Qnil);
3974 }
3975
3976 if (NILP (form))
3977 return 0;
3978
3979 /* Handle `(height HEIGHT)' specifications. */
3980 if (CONSP (spec)
3981 && EQ (XCAR (spec), Qheight)
3982 && CONSP (XCDR (spec)))
3983 {
3984 if (!FRAME_WINDOW_P (it->f))
3985 return 0;
3986
3987 it->font_height = XCAR (XCDR (spec));
3988 if (!NILP (it->font_height))
3989 {
3990 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3991 int new_height = -1;
3992
3993 if (CONSP (it->font_height)
3994 && (EQ (XCAR (it->font_height), Qplus)
3995 || EQ (XCAR (it->font_height), Qminus))
3996 && CONSP (XCDR (it->font_height))
3997 && INTEGERP (XCAR (XCDR (it->font_height))))
3998 {
3999 /* `(+ N)' or `(- N)' where N is an integer. */
4000 int steps = XINT (XCAR (XCDR (it->font_height)));
4001 if (EQ (XCAR (it->font_height), Qplus))
4002 steps = - steps;
4003 it->face_id = smaller_face (it->f, it->face_id, steps);
4004 }
4005 else if (FUNCTIONP (it->font_height))
4006 {
4007 /* Call function with current height as argument.
4008 Value is the new height. */
4009 Lisp_Object height;
4010 height = safe_call1 (it->font_height,
4011 face->lface[LFACE_HEIGHT_INDEX]);
4012 if (NUMBERP (height))
4013 new_height = XFLOATINT (height);
4014 }
4015 else if (NUMBERP (it->font_height))
4016 {
4017 /* Value is a multiple of the canonical char height. */
4018 struct face *f;
4019
4020 f = FACE_FROM_ID (it->f,
4021 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4022 new_height = (XFLOATINT (it->font_height)
4023 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4024 }
4025 else
4026 {
4027 /* Evaluate IT->font_height with `height' bound to the
4028 current specified height to get the new height. */
4029 int count = SPECPDL_INDEX ();
4030
4031 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4032 value = safe_eval (it->font_height);
4033 unbind_to (count, Qnil);
4034
4035 if (NUMBERP (value))
4036 new_height = XFLOATINT (value);
4037 }
4038
4039 if (new_height > 0)
4040 it->face_id = face_with_height (it->f, it->face_id, new_height);
4041 }
4042
4043 return 0;
4044 }
4045
4046 /* Handle `(space-width WIDTH)'. */
4047 if (CONSP (spec)
4048 && EQ (XCAR (spec), Qspace_width)
4049 && CONSP (XCDR (spec)))
4050 {
4051 if (!FRAME_WINDOW_P (it->f))
4052 return 0;
4053
4054 value = XCAR (XCDR (spec));
4055 if (NUMBERP (value) && XFLOATINT (value) > 0)
4056 it->space_width = value;
4057
4058 return 0;
4059 }
4060
4061 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4062 if (CONSP (spec)
4063 && EQ (XCAR (spec), Qslice))
4064 {
4065 Lisp_Object tem;
4066
4067 if (!FRAME_WINDOW_P (it->f))
4068 return 0;
4069
4070 if (tem = XCDR (spec), CONSP (tem))
4071 {
4072 it->slice.x = XCAR (tem);
4073 if (tem = XCDR (tem), CONSP (tem))
4074 {
4075 it->slice.y = XCAR (tem);
4076 if (tem = XCDR (tem), CONSP (tem))
4077 {
4078 it->slice.width = XCAR (tem);
4079 if (tem = XCDR (tem), CONSP (tem))
4080 it->slice.height = XCAR (tem);
4081 }
4082 }
4083 }
4084
4085 return 0;
4086 }
4087
4088 /* Handle `(raise FACTOR)'. */
4089 if (CONSP (spec)
4090 && EQ (XCAR (spec), Qraise)
4091 && CONSP (XCDR (spec)))
4092 {
4093 if (!FRAME_WINDOW_P (it->f))
4094 return 0;
4095
4096 #ifdef HAVE_WINDOW_SYSTEM
4097 value = XCAR (XCDR (spec));
4098 if (NUMBERP (value))
4099 {
4100 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4101 it->voffset = - (XFLOATINT (value)
4102 * (FONT_HEIGHT (face->font)));
4103 }
4104 #endif /* HAVE_WINDOW_SYSTEM */
4105
4106 return 0;
4107 }
4108
4109 /* Don't handle the other kinds of display specifications
4110 inside a string that we got from a `display' property. */
4111 if (it->string_from_display_prop_p)
4112 return 0;
4113
4114 /* Characters having this form of property are not displayed, so
4115 we have to find the end of the property. */
4116 start_pos = *position;
4117 *position = display_prop_end (it, object, start_pos);
4118 value = Qnil;
4119
4120 /* Stop the scan at that end position--we assume that all
4121 text properties change there. */
4122 it->stop_charpos = position->charpos;
4123
4124 /* Handle `(left-fringe BITMAP [FACE])'
4125 and `(right-fringe BITMAP [FACE])'. */
4126 if (CONSP (spec)
4127 && (EQ (XCAR (spec), Qleft_fringe)
4128 || EQ (XCAR (spec), Qright_fringe))
4129 && CONSP (XCDR (spec)))
4130 {
4131 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4132 int fringe_bitmap;
4133
4134 if (!FRAME_WINDOW_P (it->f))
4135 /* If we return here, POSITION has been advanced
4136 across the text with this property. */
4137 return 0;
4138
4139 #ifdef HAVE_WINDOW_SYSTEM
4140 value = XCAR (XCDR (spec));
4141 if (!SYMBOLP (value)
4142 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4143 /* If we return here, POSITION has been advanced
4144 across the text with this property. */
4145 return 0;
4146
4147 if (CONSP (XCDR (XCDR (spec))))
4148 {
4149 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4150 int face_id2 = lookup_derived_face (it->f, face_name,
4151 FRINGE_FACE_ID, 0);
4152 if (face_id2 >= 0)
4153 face_id = face_id2;
4154 }
4155
4156 /* Save current settings of IT so that we can restore them
4157 when we are finished with the glyph property value. */
4158
4159 save_pos = it->position;
4160 it->position = *position;
4161 push_it (it);
4162 it->position = save_pos;
4163
4164 it->area = TEXT_AREA;
4165 it->what = IT_IMAGE;
4166 it->image_id = -1; /* no image */
4167 it->position = start_pos;
4168 it->object = NILP (object) ? it->w->buffer : object;
4169 it->method = GET_FROM_IMAGE;
4170 it->from_overlay = Qnil;
4171 it->face_id = face_id;
4172
4173 /* Say that we haven't consumed the characters with
4174 `display' property yet. The call to pop_it in
4175 set_iterator_to_next will clean this up. */
4176 *position = start_pos;
4177
4178 if (EQ (XCAR (spec), Qleft_fringe))
4179 {
4180 it->left_user_fringe_bitmap = fringe_bitmap;
4181 it->left_user_fringe_face_id = face_id;
4182 }
4183 else
4184 {
4185 it->right_user_fringe_bitmap = fringe_bitmap;
4186 it->right_user_fringe_face_id = face_id;
4187 }
4188 #endif /* HAVE_WINDOW_SYSTEM */
4189 return 1;
4190 }
4191
4192 /* Prepare to handle `((margin left-margin) ...)',
4193 `((margin right-margin) ...)' and `((margin nil) ...)'
4194 prefixes for display specifications. */
4195 location = Qunbound;
4196 if (CONSP (spec) && CONSP (XCAR (spec)))
4197 {
4198 Lisp_Object tem;
4199
4200 value = XCDR (spec);
4201 if (CONSP (value))
4202 value = XCAR (value);
4203
4204 tem = XCAR (spec);
4205 if (EQ (XCAR (tem), Qmargin)
4206 && (tem = XCDR (tem),
4207 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4208 (NILP (tem)
4209 || EQ (tem, Qleft_margin)
4210 || EQ (tem, Qright_margin))))
4211 location = tem;
4212 }
4213
4214 if (EQ (location, Qunbound))
4215 {
4216 location = Qnil;
4217 value = spec;
4218 }
4219
4220 /* After this point, VALUE is the property after any
4221 margin prefix has been stripped. It must be a string,
4222 an image specification, or `(space ...)'.
4223
4224 LOCATION specifies where to display: `left-margin',
4225 `right-margin' or nil. */
4226
4227 valid_p = (STRINGP (value)
4228 #ifdef HAVE_WINDOW_SYSTEM
4229 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4230 #endif /* not HAVE_WINDOW_SYSTEM */
4231 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4232
4233 if (valid_p && !display_replaced_p)
4234 {
4235 /* Save current settings of IT so that we can restore them
4236 when we are finished with the glyph property value. */
4237 save_pos = it->position;
4238 it->position = *position;
4239 push_it (it);
4240 it->position = save_pos;
4241 it->from_overlay = overlay;
4242
4243 if (NILP (location))
4244 it->area = TEXT_AREA;
4245 else if (EQ (location, Qleft_margin))
4246 it->area = LEFT_MARGIN_AREA;
4247 else
4248 it->area = RIGHT_MARGIN_AREA;
4249
4250 if (STRINGP (value))
4251 {
4252 it->string = value;
4253 it->multibyte_p = STRING_MULTIBYTE (it->string);
4254 it->current.overlay_string_index = -1;
4255 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4256 it->end_charpos = it->string_nchars = SCHARS (it->string);
4257 it->method = GET_FROM_STRING;
4258 it->stop_charpos = 0;
4259 it->string_from_display_prop_p = 1;
4260 /* Say that we haven't consumed the characters with
4261 `display' property yet. The call to pop_it in
4262 set_iterator_to_next will clean this up. */
4263 if (BUFFERP (object))
4264 *position = start_pos;
4265 }
4266 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4267 {
4268 it->method = GET_FROM_STRETCH;
4269 it->object = value;
4270 *position = it->position = start_pos;
4271 }
4272 #ifdef HAVE_WINDOW_SYSTEM
4273 else
4274 {
4275 it->what = IT_IMAGE;
4276 it->image_id = lookup_image (it->f, value);
4277 it->position = start_pos;
4278 it->object = NILP (object) ? it->w->buffer : object;
4279 it->method = GET_FROM_IMAGE;
4280
4281 /* Say that we haven't consumed the characters with
4282 `display' property yet. The call to pop_it in
4283 set_iterator_to_next will clean this up. */
4284 *position = start_pos;
4285 }
4286 #endif /* HAVE_WINDOW_SYSTEM */
4287
4288 return 1;
4289 }
4290
4291 /* Invalid property or property not supported. Restore
4292 POSITION to what it was before. */
4293 *position = start_pos;
4294 return 0;
4295 }
4296
4297
4298 /* Check if SPEC is a display sub-property value whose text should be
4299 treated as intangible. */
4300
4301 static int
4302 single_display_spec_intangible_p (Lisp_Object prop)
4303 {
4304 /* Skip over `when FORM'. */
4305 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4306 {
4307 prop = XCDR (prop);
4308 if (!CONSP (prop))
4309 return 0;
4310 prop = XCDR (prop);
4311 }
4312
4313 if (STRINGP (prop))
4314 return 1;
4315
4316 if (!CONSP (prop))
4317 return 0;
4318
4319 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4320 we don't need to treat text as intangible. */
4321 if (EQ (XCAR (prop), Qmargin))
4322 {
4323 prop = XCDR (prop);
4324 if (!CONSP (prop))
4325 return 0;
4326
4327 prop = XCDR (prop);
4328 if (!CONSP (prop)
4329 || EQ (XCAR (prop), Qleft_margin)
4330 || EQ (XCAR (prop), Qright_margin))
4331 return 0;
4332 }
4333
4334 return (CONSP (prop)
4335 && (EQ (XCAR (prop), Qimage)
4336 || EQ (XCAR (prop), Qspace)));
4337 }
4338
4339
4340 /* Check if PROP is a display property value whose text should be
4341 treated as intangible. */
4342
4343 int
4344 display_prop_intangible_p (Lisp_Object prop)
4345 {
4346 if (CONSP (prop)
4347 && CONSP (XCAR (prop))
4348 && !EQ (Qmargin, XCAR (XCAR (prop))))
4349 {
4350 /* A list of sub-properties. */
4351 while (CONSP (prop))
4352 {
4353 if (single_display_spec_intangible_p (XCAR (prop)))
4354 return 1;
4355 prop = XCDR (prop);
4356 }
4357 }
4358 else if (VECTORP (prop))
4359 {
4360 /* A vector of sub-properties. */
4361 int i;
4362 for (i = 0; i < ASIZE (prop); ++i)
4363 if (single_display_spec_intangible_p (AREF (prop, i)))
4364 return 1;
4365 }
4366 else
4367 return single_display_spec_intangible_p (prop);
4368
4369 return 0;
4370 }
4371
4372
4373 /* Return 1 if PROP is a display sub-property value containing STRING. */
4374
4375 static int
4376 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4377 {
4378 if (EQ (string, prop))
4379 return 1;
4380
4381 /* Skip over `when FORM'. */
4382 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4383 {
4384 prop = XCDR (prop);
4385 if (!CONSP (prop))
4386 return 0;
4387 prop = XCDR (prop);
4388 }
4389
4390 if (CONSP (prop))
4391 /* Skip over `margin LOCATION'. */
4392 if (EQ (XCAR (prop), Qmargin))
4393 {
4394 prop = XCDR (prop);
4395 if (!CONSP (prop))
4396 return 0;
4397
4398 prop = XCDR (prop);
4399 if (!CONSP (prop))
4400 return 0;
4401 }
4402
4403 return CONSP (prop) && EQ (XCAR (prop), string);
4404 }
4405
4406
4407 /* Return 1 if STRING appears in the `display' property PROP. */
4408
4409 static int
4410 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4411 {
4412 if (CONSP (prop)
4413 && CONSP (XCAR (prop))
4414 && !EQ (Qmargin, XCAR (XCAR (prop))))
4415 {
4416 /* A list of sub-properties. */
4417 while (CONSP (prop))
4418 {
4419 if (single_display_spec_string_p (XCAR (prop), string))
4420 return 1;
4421 prop = XCDR (prop);
4422 }
4423 }
4424 else if (VECTORP (prop))
4425 {
4426 /* A vector of sub-properties. */
4427 int i;
4428 for (i = 0; i < ASIZE (prop); ++i)
4429 if (single_display_spec_string_p (AREF (prop, i), string))
4430 return 1;
4431 }
4432 else
4433 return single_display_spec_string_p (prop, string);
4434
4435 return 0;
4436 }
4437
4438 /* Look for STRING in overlays and text properties in the current
4439 buffer, between character positions FROM and TO (excluding TO).
4440 BACK_P non-zero means look back (in this case, TO is supposed to be
4441 less than FROM).
4442 Value is the first character position where STRING was found, or
4443 zero if it wasn't found before hitting TO.
4444
4445 This function may only use code that doesn't eval because it is
4446 called asynchronously from note_mouse_highlight. */
4447
4448 static EMACS_INT
4449 string_buffer_position_lim (Lisp_Object string,
4450 EMACS_INT from, EMACS_INT to, int back_p)
4451 {
4452 Lisp_Object limit, prop, pos;
4453 int found = 0;
4454
4455 pos = make_number (from);
4456
4457 if (!back_p) /* looking forward */
4458 {
4459 limit = make_number (min (to, ZV));
4460 while (!found && !EQ (pos, limit))
4461 {
4462 prop = Fget_char_property (pos, Qdisplay, Qnil);
4463 if (!NILP (prop) && display_prop_string_p (prop, string))
4464 found = 1;
4465 else
4466 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4467 limit);
4468 }
4469 }
4470 else /* looking back */
4471 {
4472 limit = make_number (max (to, BEGV));
4473 while (!found && !EQ (pos, limit))
4474 {
4475 prop = Fget_char_property (pos, Qdisplay, Qnil);
4476 if (!NILP (prop) && display_prop_string_p (prop, string))
4477 found = 1;
4478 else
4479 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4480 limit);
4481 }
4482 }
4483
4484 return found ? XINT (pos) : 0;
4485 }
4486
4487 /* Determine which buffer position in current buffer STRING comes from.
4488 AROUND_CHARPOS is an approximate position where it could come from.
4489 Value is the buffer position or 0 if it couldn't be determined.
4490
4491 This function is necessary because we don't record buffer positions
4492 in glyphs generated from strings (to keep struct glyph small).
4493 This function may only use code that doesn't eval because it is
4494 called asynchronously from note_mouse_highlight. */
4495
4496 static EMACS_INT
4497 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4498 {
4499 const int MAX_DISTANCE = 1000;
4500 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4501 around_charpos + MAX_DISTANCE,
4502 0);
4503
4504 if (!found)
4505 found = string_buffer_position_lim (string, around_charpos,
4506 around_charpos - MAX_DISTANCE, 1);
4507 return found;
4508 }
4509
4510
4511 \f
4512 /***********************************************************************
4513 `composition' property
4514 ***********************************************************************/
4515
4516 /* Set up iterator IT from `composition' property at its current
4517 position. Called from handle_stop. */
4518
4519 static enum prop_handled
4520 handle_composition_prop (struct it *it)
4521 {
4522 Lisp_Object prop, string;
4523 EMACS_INT pos, pos_byte, start, end;
4524
4525 if (STRINGP (it->string))
4526 {
4527 unsigned char *s;
4528
4529 pos = IT_STRING_CHARPOS (*it);
4530 pos_byte = IT_STRING_BYTEPOS (*it);
4531 string = it->string;
4532 s = SDATA (string) + pos_byte;
4533 it->c = STRING_CHAR (s);
4534 }
4535 else
4536 {
4537 pos = IT_CHARPOS (*it);
4538 pos_byte = IT_BYTEPOS (*it);
4539 string = Qnil;
4540 it->c = FETCH_CHAR (pos_byte);
4541 }
4542
4543 /* If there's a valid composition and point is not inside of the
4544 composition (in the case that the composition is from the current
4545 buffer), draw a glyph composed from the composition components. */
4546 if (find_composition (pos, -1, &start, &end, &prop, string)
4547 && COMPOSITION_VALID_P (start, end, prop)
4548 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4549 {
4550 if (start != pos)
4551 {
4552 if (STRINGP (it->string))
4553 pos_byte = string_char_to_byte (it->string, start);
4554 else
4555 pos_byte = CHAR_TO_BYTE (start);
4556 }
4557 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4558 prop, string);
4559
4560 if (it->cmp_it.id >= 0)
4561 {
4562 it->cmp_it.ch = -1;
4563 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4564 it->cmp_it.nglyphs = -1;
4565 }
4566 }
4567
4568 return HANDLED_NORMALLY;
4569 }
4570
4571
4572 \f
4573 /***********************************************************************
4574 Overlay strings
4575 ***********************************************************************/
4576
4577 /* The following structure is used to record overlay strings for
4578 later sorting in load_overlay_strings. */
4579
4580 struct overlay_entry
4581 {
4582 Lisp_Object overlay;
4583 Lisp_Object string;
4584 int priority;
4585 int after_string_p;
4586 };
4587
4588
4589 /* Set up iterator IT from overlay strings at its current position.
4590 Called from handle_stop. */
4591
4592 static enum prop_handled
4593 handle_overlay_change (struct it *it)
4594 {
4595 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4596 return HANDLED_RECOMPUTE_PROPS;
4597 else
4598 return HANDLED_NORMALLY;
4599 }
4600
4601
4602 /* Set up the next overlay string for delivery by IT, if there is an
4603 overlay string to deliver. Called by set_iterator_to_next when the
4604 end of the current overlay string is reached. If there are more
4605 overlay strings to display, IT->string and
4606 IT->current.overlay_string_index are set appropriately here.
4607 Otherwise IT->string is set to nil. */
4608
4609 static void
4610 next_overlay_string (struct it *it)
4611 {
4612 ++it->current.overlay_string_index;
4613 if (it->current.overlay_string_index == it->n_overlay_strings)
4614 {
4615 /* No more overlay strings. Restore IT's settings to what
4616 they were before overlay strings were processed, and
4617 continue to deliver from current_buffer. */
4618
4619 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4620 pop_it (it);
4621 xassert (it->sp > 0
4622 || (NILP (it->string)
4623 && it->method == GET_FROM_BUFFER
4624 && it->stop_charpos >= BEGV
4625 && it->stop_charpos <= it->end_charpos));
4626 it->current.overlay_string_index = -1;
4627 it->n_overlay_strings = 0;
4628 it->overlay_strings_charpos = -1;
4629
4630 /* If we're at the end of the buffer, record that we have
4631 processed the overlay strings there already, so that
4632 next_element_from_buffer doesn't try it again. */
4633 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4634 it->overlay_strings_at_end_processed_p = 1;
4635 }
4636 else
4637 {
4638 /* There are more overlay strings to process. If
4639 IT->current.overlay_string_index has advanced to a position
4640 where we must load IT->overlay_strings with more strings, do
4641 it. We must load at the IT->overlay_strings_charpos where
4642 IT->n_overlay_strings was originally computed; when invisible
4643 text is present, this might not be IT_CHARPOS (Bug#7016). */
4644 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4645
4646 if (it->current.overlay_string_index && i == 0)
4647 load_overlay_strings (it, it->overlay_strings_charpos);
4648
4649 /* Initialize IT to deliver display elements from the overlay
4650 string. */
4651 it->string = it->overlay_strings[i];
4652 it->multibyte_p = STRING_MULTIBYTE (it->string);
4653 SET_TEXT_POS (it->current.string_pos, 0, 0);
4654 it->method = GET_FROM_STRING;
4655 it->stop_charpos = 0;
4656 if (it->cmp_it.stop_pos >= 0)
4657 it->cmp_it.stop_pos = 0;
4658 }
4659
4660 CHECK_IT (it);
4661 }
4662
4663
4664 /* Compare two overlay_entry structures E1 and E2. Used as a
4665 comparison function for qsort in load_overlay_strings. Overlay
4666 strings for the same position are sorted so that
4667
4668 1. All after-strings come in front of before-strings, except
4669 when they come from the same overlay.
4670
4671 2. Within after-strings, strings are sorted so that overlay strings
4672 from overlays with higher priorities come first.
4673
4674 2. Within before-strings, strings are sorted so that overlay
4675 strings from overlays with higher priorities come last.
4676
4677 Value is analogous to strcmp. */
4678
4679
4680 static int
4681 compare_overlay_entries (const void *e1, const void *e2)
4682 {
4683 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4684 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4685 int result;
4686
4687 if (entry1->after_string_p != entry2->after_string_p)
4688 {
4689 /* Let after-strings appear in front of before-strings if
4690 they come from different overlays. */
4691 if (EQ (entry1->overlay, entry2->overlay))
4692 result = entry1->after_string_p ? 1 : -1;
4693 else
4694 result = entry1->after_string_p ? -1 : 1;
4695 }
4696 else if (entry1->after_string_p)
4697 /* After-strings sorted in order of decreasing priority. */
4698 result = entry2->priority - entry1->priority;
4699 else
4700 /* Before-strings sorted in order of increasing priority. */
4701 result = entry1->priority - entry2->priority;
4702
4703 return result;
4704 }
4705
4706
4707 /* Load the vector IT->overlay_strings with overlay strings from IT's
4708 current buffer position, or from CHARPOS if that is > 0. Set
4709 IT->n_overlays to the total number of overlay strings found.
4710
4711 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4712 a time. On entry into load_overlay_strings,
4713 IT->current.overlay_string_index gives the number of overlay
4714 strings that have already been loaded by previous calls to this
4715 function.
4716
4717 IT->add_overlay_start contains an additional overlay start
4718 position to consider for taking overlay strings from, if non-zero.
4719 This position comes into play when the overlay has an `invisible'
4720 property, and both before and after-strings. When we've skipped to
4721 the end of the overlay, because of its `invisible' property, we
4722 nevertheless want its before-string to appear.
4723 IT->add_overlay_start will contain the overlay start position
4724 in this case.
4725
4726 Overlay strings are sorted so that after-string strings come in
4727 front of before-string strings. Within before and after-strings,
4728 strings are sorted by overlay priority. See also function
4729 compare_overlay_entries. */
4730
4731 static void
4732 load_overlay_strings (struct it *it, EMACS_INT charpos)
4733 {
4734 Lisp_Object overlay, window, str, invisible;
4735 struct Lisp_Overlay *ov;
4736 EMACS_INT start, end;
4737 int size = 20;
4738 int n = 0, i, j, invis_p;
4739 struct overlay_entry *entries
4740 = (struct overlay_entry *) alloca (size * sizeof *entries);
4741
4742 if (charpos <= 0)
4743 charpos = IT_CHARPOS (*it);
4744
4745 /* Append the overlay string STRING of overlay OVERLAY to vector
4746 `entries' which has size `size' and currently contains `n'
4747 elements. AFTER_P non-zero means STRING is an after-string of
4748 OVERLAY. */
4749 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4750 do \
4751 { \
4752 Lisp_Object priority; \
4753 \
4754 if (n == size) \
4755 { \
4756 int new_size = 2 * size; \
4757 struct overlay_entry *old = entries; \
4758 entries = \
4759 (struct overlay_entry *) alloca (new_size \
4760 * sizeof *entries); \
4761 memcpy (entries, old, size * sizeof *entries); \
4762 size = new_size; \
4763 } \
4764 \
4765 entries[n].string = (STRING); \
4766 entries[n].overlay = (OVERLAY); \
4767 priority = Foverlay_get ((OVERLAY), Qpriority); \
4768 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4769 entries[n].after_string_p = (AFTER_P); \
4770 ++n; \
4771 } \
4772 while (0)
4773
4774 /* Process overlay before the overlay center. */
4775 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4776 {
4777 XSETMISC (overlay, ov);
4778 xassert (OVERLAYP (overlay));
4779 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4780 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4781
4782 if (end < charpos)
4783 break;
4784
4785 /* Skip this overlay if it doesn't start or end at IT's current
4786 position. */
4787 if (end != charpos && start != charpos)
4788 continue;
4789
4790 /* Skip this overlay if it doesn't apply to IT->w. */
4791 window = Foverlay_get (overlay, Qwindow);
4792 if (WINDOWP (window) && XWINDOW (window) != it->w)
4793 continue;
4794
4795 /* If the text ``under'' the overlay is invisible, both before-
4796 and after-strings from this overlay are visible; start and
4797 end position are indistinguishable. */
4798 invisible = Foverlay_get (overlay, Qinvisible);
4799 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4800
4801 /* If overlay has a non-empty before-string, record it. */
4802 if ((start == charpos || (end == charpos && invis_p))
4803 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4804 && SCHARS (str))
4805 RECORD_OVERLAY_STRING (overlay, str, 0);
4806
4807 /* If overlay has a non-empty after-string, record it. */
4808 if ((end == charpos || (start == charpos && invis_p))
4809 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4810 && SCHARS (str))
4811 RECORD_OVERLAY_STRING (overlay, str, 1);
4812 }
4813
4814 /* Process overlays after the overlay center. */
4815 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4816 {
4817 XSETMISC (overlay, ov);
4818 xassert (OVERLAYP (overlay));
4819 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4820 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4821
4822 if (start > charpos)
4823 break;
4824
4825 /* Skip this overlay if it doesn't start or end at IT's current
4826 position. */
4827 if (end != charpos && start != charpos)
4828 continue;
4829
4830 /* Skip this overlay if it doesn't apply to IT->w. */
4831 window = Foverlay_get (overlay, Qwindow);
4832 if (WINDOWP (window) && XWINDOW (window) != it->w)
4833 continue;
4834
4835 /* If the text ``under'' the overlay is invisible, it has a zero
4836 dimension, and both before- and after-strings apply. */
4837 invisible = Foverlay_get (overlay, Qinvisible);
4838 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4839
4840 /* If overlay has a non-empty before-string, record it. */
4841 if ((start == charpos || (end == charpos && invis_p))
4842 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4843 && SCHARS (str))
4844 RECORD_OVERLAY_STRING (overlay, str, 0);
4845
4846 /* If overlay has a non-empty after-string, record it. */
4847 if ((end == charpos || (start == charpos && invis_p))
4848 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4849 && SCHARS (str))
4850 RECORD_OVERLAY_STRING (overlay, str, 1);
4851 }
4852
4853 #undef RECORD_OVERLAY_STRING
4854
4855 /* Sort entries. */
4856 if (n > 1)
4857 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4858
4859 /* Record number of overlay strings, and where we computed it. */
4860 it->n_overlay_strings = n;
4861 it->overlay_strings_charpos = charpos;
4862
4863 /* IT->current.overlay_string_index is the number of overlay strings
4864 that have already been consumed by IT. Copy some of the
4865 remaining overlay strings to IT->overlay_strings. */
4866 i = 0;
4867 j = it->current.overlay_string_index;
4868 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4869 {
4870 it->overlay_strings[i] = entries[j].string;
4871 it->string_overlays[i++] = entries[j++].overlay;
4872 }
4873
4874 CHECK_IT (it);
4875 }
4876
4877
4878 /* Get the first chunk of overlay strings at IT's current buffer
4879 position, or at CHARPOS if that is > 0. Value is non-zero if at
4880 least one overlay string was found. */
4881
4882 static int
4883 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
4884 {
4885 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4886 process. This fills IT->overlay_strings with strings, and sets
4887 IT->n_overlay_strings to the total number of strings to process.
4888 IT->pos.overlay_string_index has to be set temporarily to zero
4889 because load_overlay_strings needs this; it must be set to -1
4890 when no overlay strings are found because a zero value would
4891 indicate a position in the first overlay string. */
4892 it->current.overlay_string_index = 0;
4893 load_overlay_strings (it, charpos);
4894
4895 /* If we found overlay strings, set up IT to deliver display
4896 elements from the first one. Otherwise set up IT to deliver
4897 from current_buffer. */
4898 if (it->n_overlay_strings)
4899 {
4900 /* Make sure we know settings in current_buffer, so that we can
4901 restore meaningful values when we're done with the overlay
4902 strings. */
4903 if (compute_stop_p)
4904 compute_stop_pos (it);
4905 xassert (it->face_id >= 0);
4906
4907 /* Save IT's settings. They are restored after all overlay
4908 strings have been processed. */
4909 xassert (!compute_stop_p || it->sp == 0);
4910
4911 /* When called from handle_stop, there might be an empty display
4912 string loaded. In that case, don't bother saving it. */
4913 if (!STRINGP (it->string) || SCHARS (it->string))
4914 push_it (it);
4915
4916 /* Set up IT to deliver display elements from the first overlay
4917 string. */
4918 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4919 it->string = it->overlay_strings[0];
4920 it->from_overlay = Qnil;
4921 it->stop_charpos = 0;
4922 xassert (STRINGP (it->string));
4923 it->end_charpos = SCHARS (it->string);
4924 it->multibyte_p = STRING_MULTIBYTE (it->string);
4925 it->method = GET_FROM_STRING;
4926 return 1;
4927 }
4928
4929 it->current.overlay_string_index = -1;
4930 return 0;
4931 }
4932
4933 static int
4934 get_overlay_strings (struct it *it, EMACS_INT charpos)
4935 {
4936 it->string = Qnil;
4937 it->method = GET_FROM_BUFFER;
4938
4939 (void) get_overlay_strings_1 (it, charpos, 1);
4940
4941 CHECK_IT (it);
4942
4943 /* Value is non-zero if we found at least one overlay string. */
4944 return STRINGP (it->string);
4945 }
4946
4947
4948 \f
4949 /***********************************************************************
4950 Saving and restoring state
4951 ***********************************************************************/
4952
4953 /* Save current settings of IT on IT->stack. Called, for example,
4954 before setting up IT for an overlay string, to be able to restore
4955 IT's settings to what they were after the overlay string has been
4956 processed. */
4957
4958 static void
4959 push_it (struct it *it)
4960 {
4961 struct iterator_stack_entry *p;
4962
4963 xassert (it->sp < IT_STACK_SIZE);
4964 p = it->stack + it->sp;
4965
4966 p->stop_charpos = it->stop_charpos;
4967 p->prev_stop = it->prev_stop;
4968 p->base_level_stop = it->base_level_stop;
4969 p->cmp_it = it->cmp_it;
4970 xassert (it->face_id >= 0);
4971 p->face_id = it->face_id;
4972 p->string = it->string;
4973 p->method = it->method;
4974 p->from_overlay = it->from_overlay;
4975 switch (p->method)
4976 {
4977 case GET_FROM_IMAGE:
4978 p->u.image.object = it->object;
4979 p->u.image.image_id = it->image_id;
4980 p->u.image.slice = it->slice;
4981 break;
4982 case GET_FROM_STRETCH:
4983 p->u.stretch.object = it->object;
4984 break;
4985 }
4986 p->position = it->position;
4987 p->current = it->current;
4988 p->end_charpos = it->end_charpos;
4989 p->string_nchars = it->string_nchars;
4990 p->area = it->area;
4991 p->multibyte_p = it->multibyte_p;
4992 p->avoid_cursor_p = it->avoid_cursor_p;
4993 p->space_width = it->space_width;
4994 p->font_height = it->font_height;
4995 p->voffset = it->voffset;
4996 p->string_from_display_prop_p = it->string_from_display_prop_p;
4997 p->display_ellipsis_p = 0;
4998 p->line_wrap = it->line_wrap;
4999 ++it->sp;
5000 }
5001
5002 static void
5003 iterate_out_of_display_property (struct it *it)
5004 {
5005 /* Maybe initialize paragraph direction. If we are at the beginning
5006 of a new paragraph, next_element_from_buffer may not have a
5007 chance to do that. */
5008 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5009 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5010 /* prev_stop can be zero, so check against BEGV as well. */
5011 while (it->bidi_it.charpos >= BEGV
5012 && it->prev_stop <= it->bidi_it.charpos
5013 && it->bidi_it.charpos < CHARPOS (it->position))
5014 bidi_move_to_visually_next (&it->bidi_it);
5015 /* Record the stop_pos we just crossed, for when we cross it
5016 back, maybe. */
5017 if (it->bidi_it.charpos > CHARPOS (it->position))
5018 it->prev_stop = CHARPOS (it->position);
5019 /* If we ended up not where pop_it put us, resync IT's
5020 positional members with the bidi iterator. */
5021 if (it->bidi_it.charpos != CHARPOS (it->position))
5022 {
5023 SET_TEXT_POS (it->position,
5024 it->bidi_it.charpos, it->bidi_it.bytepos);
5025 it->current.pos = it->position;
5026 }
5027 }
5028
5029 /* Restore IT's settings from IT->stack. Called, for example, when no
5030 more overlay strings must be processed, and we return to delivering
5031 display elements from a buffer, or when the end of a string from a
5032 `display' property is reached and we return to delivering display
5033 elements from an overlay string, or from a buffer. */
5034
5035 static void
5036 pop_it (struct it *it)
5037 {
5038 struct iterator_stack_entry *p;
5039
5040 xassert (it->sp > 0);
5041 --it->sp;
5042 p = it->stack + it->sp;
5043 it->stop_charpos = p->stop_charpos;
5044 it->prev_stop = p->prev_stop;
5045 it->base_level_stop = p->base_level_stop;
5046 it->cmp_it = p->cmp_it;
5047 it->face_id = p->face_id;
5048 it->current = p->current;
5049 it->position = p->position;
5050 it->string = p->string;
5051 it->from_overlay = p->from_overlay;
5052 if (NILP (it->string))
5053 SET_TEXT_POS (it->current.string_pos, -1, -1);
5054 it->method = p->method;
5055 switch (it->method)
5056 {
5057 case GET_FROM_IMAGE:
5058 it->image_id = p->u.image.image_id;
5059 it->object = p->u.image.object;
5060 it->slice = p->u.image.slice;
5061 break;
5062 case GET_FROM_STRETCH:
5063 it->object = p->u.comp.object;
5064 break;
5065 case GET_FROM_BUFFER:
5066 it->object = it->w->buffer;
5067 if (it->bidi_p)
5068 {
5069 /* Bidi-iterate until we get out of the portion of text, if
5070 any, covered by a `display' text property or an overlay
5071 with `display' property. (We cannot just jump there,
5072 because the internal coherency of the bidi iterator state
5073 can not be preserved across such jumps.) We also must
5074 determine the paragraph base direction if the overlay we
5075 just processed is at the beginning of a new
5076 paragraph. */
5077 iterate_out_of_display_property (it);
5078 }
5079 break;
5080 case GET_FROM_STRING:
5081 it->object = it->string;
5082 break;
5083 case GET_FROM_DISPLAY_VECTOR:
5084 if (it->s)
5085 it->method = GET_FROM_C_STRING;
5086 else if (STRINGP (it->string))
5087 it->method = GET_FROM_STRING;
5088 else
5089 {
5090 it->method = GET_FROM_BUFFER;
5091 it->object = it->w->buffer;
5092 }
5093 }
5094 it->end_charpos = p->end_charpos;
5095 it->string_nchars = p->string_nchars;
5096 it->area = p->area;
5097 it->multibyte_p = p->multibyte_p;
5098 it->avoid_cursor_p = p->avoid_cursor_p;
5099 it->space_width = p->space_width;
5100 it->font_height = p->font_height;
5101 it->voffset = p->voffset;
5102 it->string_from_display_prop_p = p->string_from_display_prop_p;
5103 it->line_wrap = p->line_wrap;
5104 }
5105
5106
5107 \f
5108 /***********************************************************************
5109 Moving over lines
5110 ***********************************************************************/
5111
5112 /* Set IT's current position to the previous line start. */
5113
5114 static void
5115 back_to_previous_line_start (struct it *it)
5116 {
5117 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5118 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5119 }
5120
5121
5122 /* Move IT to the next line start.
5123
5124 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5125 we skipped over part of the text (as opposed to moving the iterator
5126 continuously over the text). Otherwise, don't change the value
5127 of *SKIPPED_P.
5128
5129 Newlines may come from buffer text, overlay strings, or strings
5130 displayed via the `display' property. That's the reason we can't
5131 simply use find_next_newline_no_quit.
5132
5133 Note that this function may not skip over invisible text that is so
5134 because of text properties and immediately follows a newline. If
5135 it would, function reseat_at_next_visible_line_start, when called
5136 from set_iterator_to_next, would effectively make invisible
5137 characters following a newline part of the wrong glyph row, which
5138 leads to wrong cursor motion. */
5139
5140 static int
5141 forward_to_next_line_start (struct it *it, int *skipped_p)
5142 {
5143 int old_selective, newline_found_p, n;
5144 const int MAX_NEWLINE_DISTANCE = 500;
5145
5146 /* If already on a newline, just consume it to avoid unintended
5147 skipping over invisible text below. */
5148 if (it->what == IT_CHARACTER
5149 && it->c == '\n'
5150 && CHARPOS (it->position) == IT_CHARPOS (*it))
5151 {
5152 set_iterator_to_next (it, 0);
5153 it->c = 0;
5154 return 1;
5155 }
5156
5157 /* Don't handle selective display in the following. It's (a)
5158 unnecessary because it's done by the caller, and (b) leads to an
5159 infinite recursion because next_element_from_ellipsis indirectly
5160 calls this function. */
5161 old_selective = it->selective;
5162 it->selective = 0;
5163
5164 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5165 from buffer text. */
5166 for (n = newline_found_p = 0;
5167 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5168 n += STRINGP (it->string) ? 0 : 1)
5169 {
5170 if (!get_next_display_element (it))
5171 return 0;
5172 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5173 set_iterator_to_next (it, 0);
5174 }
5175
5176 /* If we didn't find a newline near enough, see if we can use a
5177 short-cut. */
5178 if (!newline_found_p)
5179 {
5180 EMACS_INT start = IT_CHARPOS (*it);
5181 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5182 Lisp_Object pos;
5183
5184 xassert (!STRINGP (it->string));
5185
5186 /* If there isn't any `display' property in sight, and no
5187 overlays, we can just use the position of the newline in
5188 buffer text. */
5189 if (it->stop_charpos >= limit
5190 || ((pos = Fnext_single_property_change (make_number (start),
5191 Qdisplay,
5192 Qnil, make_number (limit)),
5193 NILP (pos))
5194 && next_overlay_change (start) == ZV))
5195 {
5196 IT_CHARPOS (*it) = limit;
5197 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5198 *skipped_p = newline_found_p = 1;
5199 }
5200 else
5201 {
5202 while (get_next_display_element (it)
5203 && !newline_found_p)
5204 {
5205 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5206 set_iterator_to_next (it, 0);
5207 }
5208 }
5209 }
5210
5211 it->selective = old_selective;
5212 return newline_found_p;
5213 }
5214
5215
5216 /* Set IT's current position to the previous visible line start. Skip
5217 invisible text that is so either due to text properties or due to
5218 selective display. Caution: this does not change IT->current_x and
5219 IT->hpos. */
5220
5221 static void
5222 back_to_previous_visible_line_start (struct it *it)
5223 {
5224 while (IT_CHARPOS (*it) > BEGV)
5225 {
5226 back_to_previous_line_start (it);
5227
5228 if (IT_CHARPOS (*it) <= BEGV)
5229 break;
5230
5231 /* If selective > 0, then lines indented more than its value are
5232 invisible. */
5233 if (it->selective > 0
5234 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5235 (double) it->selective)) /* iftc */
5236 continue;
5237
5238 /* Check the newline before point for invisibility. */
5239 {
5240 Lisp_Object prop;
5241 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5242 Qinvisible, it->window);
5243 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5244 continue;
5245 }
5246
5247 if (IT_CHARPOS (*it) <= BEGV)
5248 break;
5249
5250 {
5251 struct it it2;
5252 EMACS_INT pos;
5253 EMACS_INT beg, end;
5254 Lisp_Object val, overlay;
5255
5256 /* If newline is part of a composition, continue from start of composition */
5257 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5258 && beg < IT_CHARPOS (*it))
5259 goto replaced;
5260
5261 /* If newline is replaced by a display property, find start of overlay
5262 or interval and continue search from that point. */
5263 it2 = *it;
5264 pos = --IT_CHARPOS (it2);
5265 --IT_BYTEPOS (it2);
5266 it2.sp = 0;
5267 it2.string_from_display_prop_p = 0;
5268 if (handle_display_prop (&it2) == HANDLED_RETURN
5269 && !NILP (val = get_char_property_and_overlay
5270 (make_number (pos), Qdisplay, Qnil, &overlay))
5271 && (OVERLAYP (overlay)
5272 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5273 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5274 goto replaced;
5275
5276 /* Newline is not replaced by anything -- so we are done. */
5277 break;
5278
5279 replaced:
5280 if (beg < BEGV)
5281 beg = BEGV;
5282 IT_CHARPOS (*it) = beg;
5283 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5284 }
5285 }
5286
5287 it->continuation_lines_width = 0;
5288
5289 xassert (IT_CHARPOS (*it) >= BEGV);
5290 xassert (IT_CHARPOS (*it) == BEGV
5291 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5292 CHECK_IT (it);
5293 }
5294
5295
5296 /* Reseat iterator IT at the previous visible line start. Skip
5297 invisible text that is so either due to text properties or due to
5298 selective display. At the end, update IT's overlay information,
5299 face information etc. */
5300
5301 void
5302 reseat_at_previous_visible_line_start (struct it *it)
5303 {
5304 back_to_previous_visible_line_start (it);
5305 reseat (it, it->current.pos, 1);
5306 CHECK_IT (it);
5307 }
5308
5309
5310 /* Reseat iterator IT on the next visible line start in the current
5311 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5312 preceding the line start. Skip over invisible text that is so
5313 because of selective display. Compute faces, overlays etc at the
5314 new position. Note that this function does not skip over text that
5315 is invisible because of text properties. */
5316
5317 static void
5318 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5319 {
5320 int newline_found_p, skipped_p = 0;
5321
5322 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5323
5324 /* Skip over lines that are invisible because they are indented
5325 more than the value of IT->selective. */
5326 if (it->selective > 0)
5327 while (IT_CHARPOS (*it) < ZV
5328 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5329 (double) it->selective)) /* iftc */
5330 {
5331 xassert (IT_BYTEPOS (*it) == BEGV
5332 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5333 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5334 }
5335
5336 /* Position on the newline if that's what's requested. */
5337 if (on_newline_p && newline_found_p)
5338 {
5339 if (STRINGP (it->string))
5340 {
5341 if (IT_STRING_CHARPOS (*it) > 0)
5342 {
5343 --IT_STRING_CHARPOS (*it);
5344 --IT_STRING_BYTEPOS (*it);
5345 }
5346 }
5347 else if (IT_CHARPOS (*it) > BEGV)
5348 {
5349 --IT_CHARPOS (*it);
5350 --IT_BYTEPOS (*it);
5351 reseat (it, it->current.pos, 0);
5352 }
5353 }
5354 else if (skipped_p)
5355 reseat (it, it->current.pos, 0);
5356
5357 CHECK_IT (it);
5358 }
5359
5360
5361 \f
5362 /***********************************************************************
5363 Changing an iterator's position
5364 ***********************************************************************/
5365
5366 /* Change IT's current position to POS in current_buffer. If FORCE_P
5367 is non-zero, always check for text properties at the new position.
5368 Otherwise, text properties are only looked up if POS >=
5369 IT->check_charpos of a property. */
5370
5371 static void
5372 reseat (struct it *it, struct text_pos pos, int force_p)
5373 {
5374 EMACS_INT original_pos = IT_CHARPOS (*it);
5375
5376 reseat_1 (it, pos, 0);
5377
5378 /* Determine where to check text properties. Avoid doing it
5379 where possible because text property lookup is very expensive. */
5380 if (force_p
5381 || CHARPOS (pos) > it->stop_charpos
5382 || CHARPOS (pos) < original_pos)
5383 {
5384 if (it->bidi_p)
5385 {
5386 /* For bidi iteration, we need to prime prev_stop and
5387 base_level_stop with our best estimations. */
5388 if (CHARPOS (pos) < it->prev_stop)
5389 {
5390 handle_stop_backwards (it, BEGV);
5391 if (CHARPOS (pos) < it->base_level_stop)
5392 it->base_level_stop = 0;
5393 }
5394 else if (CHARPOS (pos) > it->stop_charpos
5395 && it->stop_charpos >= BEGV)
5396 handle_stop_backwards (it, it->stop_charpos);
5397 else /* force_p */
5398 handle_stop (it);
5399 }
5400 else
5401 {
5402 handle_stop (it);
5403 it->prev_stop = it->base_level_stop = 0;
5404 }
5405
5406 }
5407
5408 CHECK_IT (it);
5409 }
5410
5411
5412 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5413 IT->stop_pos to POS, also. */
5414
5415 static void
5416 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5417 {
5418 /* Don't call this function when scanning a C string. */
5419 xassert (it->s == NULL);
5420
5421 /* POS must be a reasonable value. */
5422 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5423
5424 it->current.pos = it->position = pos;
5425 it->end_charpos = ZV;
5426 it->dpvec = NULL;
5427 it->current.dpvec_index = -1;
5428 it->current.overlay_string_index = -1;
5429 IT_STRING_CHARPOS (*it) = -1;
5430 IT_STRING_BYTEPOS (*it) = -1;
5431 it->string = Qnil;
5432 it->string_from_display_prop_p = 0;
5433 it->method = GET_FROM_BUFFER;
5434 it->object = it->w->buffer;
5435 it->area = TEXT_AREA;
5436 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5437 it->sp = 0;
5438 it->string_from_display_prop_p = 0;
5439 it->face_before_selective_p = 0;
5440 if (it->bidi_p)
5441 {
5442 it->bidi_it.first_elt = 1;
5443 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5444 it->bidi_it.disp_pos = -1;
5445 }
5446
5447 if (set_stop_p)
5448 {
5449 it->stop_charpos = CHARPOS (pos);
5450 it->base_level_stop = CHARPOS (pos);
5451 }
5452 }
5453
5454
5455 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5456 If S is non-null, it is a C string to iterate over. Otherwise,
5457 STRING gives a Lisp string to iterate over.
5458
5459 If PRECISION > 0, don't return more then PRECISION number of
5460 characters from the string.
5461
5462 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5463 characters have been returned. FIELD_WIDTH < 0 means an infinite
5464 field width.
5465
5466 MULTIBYTE = 0 means disable processing of multibyte characters,
5467 MULTIBYTE > 0 means enable it,
5468 MULTIBYTE < 0 means use IT->multibyte_p.
5469
5470 IT must be initialized via a prior call to init_iterator before
5471 calling this function. */
5472
5473 static void
5474 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5475 EMACS_INT charpos, EMACS_INT precision, int field_width,
5476 int multibyte)
5477 {
5478 /* No region in strings. */
5479 it->region_beg_charpos = it->region_end_charpos = -1;
5480
5481 /* No text property checks performed by default, but see below. */
5482 it->stop_charpos = -1;
5483
5484 /* Set iterator position and end position. */
5485 memset (&it->current, 0, sizeof it->current);
5486 it->current.overlay_string_index = -1;
5487 it->current.dpvec_index = -1;
5488 xassert (charpos >= 0);
5489
5490 /* If STRING is specified, use its multibyteness, otherwise use the
5491 setting of MULTIBYTE, if specified. */
5492 if (multibyte >= 0)
5493 it->multibyte_p = multibyte > 0;
5494
5495 if (s == NULL)
5496 {
5497 xassert (STRINGP (string));
5498 it->string = string;
5499 it->s = NULL;
5500 it->end_charpos = it->string_nchars = SCHARS (string);
5501 it->method = GET_FROM_STRING;
5502 it->current.string_pos = string_pos (charpos, string);
5503 }
5504 else
5505 {
5506 it->s = (const unsigned char *) s;
5507 it->string = Qnil;
5508
5509 /* Note that we use IT->current.pos, not it->current.string_pos,
5510 for displaying C strings. */
5511 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5512 if (it->multibyte_p)
5513 {
5514 it->current.pos = c_string_pos (charpos, s, 1);
5515 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5516 }
5517 else
5518 {
5519 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5520 it->end_charpos = it->string_nchars = strlen (s);
5521 }
5522
5523 it->method = GET_FROM_C_STRING;
5524 }
5525
5526 /* PRECISION > 0 means don't return more than PRECISION characters
5527 from the string. */
5528 if (precision > 0 && it->end_charpos - charpos > precision)
5529 it->end_charpos = it->string_nchars = charpos + precision;
5530
5531 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5532 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5533 FIELD_WIDTH < 0 means infinite field width. This is useful for
5534 padding with `-' at the end of a mode line. */
5535 if (field_width < 0)
5536 field_width = INFINITY;
5537 if (field_width > it->end_charpos - charpos)
5538 it->end_charpos = charpos + field_width;
5539
5540 /* Use the standard display table for displaying strings. */
5541 if (DISP_TABLE_P (Vstandard_display_table))
5542 it->dp = XCHAR_TABLE (Vstandard_display_table);
5543
5544 it->stop_charpos = charpos;
5545 if (s == NULL && it->multibyte_p)
5546 {
5547 EMACS_INT endpos = SCHARS (it->string);
5548 if (endpos > it->end_charpos)
5549 endpos = it->end_charpos;
5550 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5551 it->string);
5552 }
5553 CHECK_IT (it);
5554 }
5555
5556
5557 \f
5558 /***********************************************************************
5559 Iteration
5560 ***********************************************************************/
5561
5562 /* Map enum it_method value to corresponding next_element_from_* function. */
5563
5564 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5565 {
5566 next_element_from_buffer,
5567 next_element_from_display_vector,
5568 next_element_from_string,
5569 next_element_from_c_string,
5570 next_element_from_image,
5571 next_element_from_stretch
5572 };
5573
5574 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5575
5576
5577 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5578 (possibly with the following characters). */
5579
5580 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5581 ((IT)->cmp_it.id >= 0 \
5582 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5583 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5584 END_CHARPOS, (IT)->w, \
5585 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5586 (IT)->string)))
5587
5588
5589 /* Lookup the char-table Vglyphless_char_display for character C (-1
5590 if we want information for no-font case), and return the display
5591 method symbol. By side-effect, update it->what and
5592 it->glyphless_method. This function is called from
5593 get_next_display_element for each character element, and from
5594 x_produce_glyphs when no suitable font was found. */
5595
5596 Lisp_Object
5597 lookup_glyphless_char_display (int c, struct it *it)
5598 {
5599 Lisp_Object glyphless_method = Qnil;
5600
5601 if (CHAR_TABLE_P (Vglyphless_char_display)
5602 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5603 {
5604 if (c >= 0)
5605 {
5606 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
5607 if (CONSP (glyphless_method))
5608 glyphless_method = FRAME_WINDOW_P (it->f)
5609 ? XCAR (glyphless_method)
5610 : XCDR (glyphless_method);
5611 }
5612 else
5613 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
5614 }
5615
5616 retry:
5617 if (NILP (glyphless_method))
5618 {
5619 if (c >= 0)
5620 /* The default is to display the character by a proper font. */
5621 return Qnil;
5622 /* The default for the no-font case is to display an empty box. */
5623 glyphless_method = Qempty_box;
5624 }
5625 if (EQ (glyphless_method, Qzero_width))
5626 {
5627 if (c >= 0)
5628 return glyphless_method;
5629 /* This method can't be used for the no-font case. */
5630 glyphless_method = Qempty_box;
5631 }
5632 if (EQ (glyphless_method, Qthin_space))
5633 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5634 else if (EQ (glyphless_method, Qempty_box))
5635 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5636 else if (EQ (glyphless_method, Qhex_code))
5637 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5638 else if (STRINGP (glyphless_method))
5639 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5640 else
5641 {
5642 /* Invalid value. We use the default method. */
5643 glyphless_method = Qnil;
5644 goto retry;
5645 }
5646 it->what = IT_GLYPHLESS;
5647 return glyphless_method;
5648 }
5649
5650 /* Load IT's display element fields with information about the next
5651 display element from the current position of IT. Value is zero if
5652 end of buffer (or C string) is reached. */
5653
5654 static struct frame *last_escape_glyph_frame = NULL;
5655 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5656 static int last_escape_glyph_merged_face_id = 0;
5657
5658 struct frame *last_glyphless_glyph_frame = NULL;
5659 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5660 int last_glyphless_glyph_merged_face_id = 0;
5661
5662 static int
5663 get_next_display_element (struct it *it)
5664 {
5665 /* Non-zero means that we found a display element. Zero means that
5666 we hit the end of what we iterate over. Performance note: the
5667 function pointer `method' used here turns out to be faster than
5668 using a sequence of if-statements. */
5669 int success_p;
5670
5671 get_next:
5672 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5673
5674 if (it->what == IT_CHARACTER)
5675 {
5676 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5677 and only if (a) the resolved directionality of that character
5678 is R..." */
5679 /* FIXME: Do we need an exception for characters from display
5680 tables? */
5681 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5682 it->c = bidi_mirror_char (it->c);
5683 /* Map via display table or translate control characters.
5684 IT->c, IT->len etc. have been set to the next character by
5685 the function call above. If we have a display table, and it
5686 contains an entry for IT->c, translate it. Don't do this if
5687 IT->c itself comes from a display table, otherwise we could
5688 end up in an infinite recursion. (An alternative could be to
5689 count the recursion depth of this function and signal an
5690 error when a certain maximum depth is reached.) Is it worth
5691 it? */
5692 if (success_p && it->dpvec == NULL)
5693 {
5694 Lisp_Object dv;
5695 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5696 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5697 nbsp_or_shy = char_is_other;
5698 int c = it->c; /* This is the character to display. */
5699
5700 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5701 {
5702 xassert (SINGLE_BYTE_CHAR_P (c));
5703 if (unibyte_display_via_language_environment)
5704 {
5705 c = DECODE_CHAR (unibyte, c);
5706 if (c < 0)
5707 c = BYTE8_TO_CHAR (it->c);
5708 }
5709 else
5710 c = BYTE8_TO_CHAR (it->c);
5711 }
5712
5713 if (it->dp
5714 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5715 VECTORP (dv)))
5716 {
5717 struct Lisp_Vector *v = XVECTOR (dv);
5718
5719 /* Return the first character from the display table
5720 entry, if not empty. If empty, don't display the
5721 current character. */
5722 if (v->header.size)
5723 {
5724 it->dpvec_char_len = it->len;
5725 it->dpvec = v->contents;
5726 it->dpend = v->contents + v->header.size;
5727 it->current.dpvec_index = 0;
5728 it->dpvec_face_id = -1;
5729 it->saved_face_id = it->face_id;
5730 it->method = GET_FROM_DISPLAY_VECTOR;
5731 it->ellipsis_p = 0;
5732 }
5733 else
5734 {
5735 set_iterator_to_next (it, 0);
5736 }
5737 goto get_next;
5738 }
5739
5740 if (! NILP (lookup_glyphless_char_display (c, it)))
5741 {
5742 if (it->what == IT_GLYPHLESS)
5743 goto done;
5744 /* Don't display this character. */
5745 set_iterator_to_next (it, 0);
5746 goto get_next;
5747 }
5748
5749 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5750 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5751 : c == 0xAD ? char_is_soft_hyphen
5752 : char_is_other);
5753
5754 /* Translate control characters into `\003' or `^C' form.
5755 Control characters coming from a display table entry are
5756 currently not translated because we use IT->dpvec to hold
5757 the translation. This could easily be changed but I
5758 don't believe that it is worth doing.
5759
5760 NBSP and SOFT-HYPEN are property translated too.
5761
5762 Non-printable characters and raw-byte characters are also
5763 translated to octal form. */
5764 if (((c < ' ' || c == 127) /* ASCII control chars */
5765 ? (it->area != TEXT_AREA
5766 /* In mode line, treat \n, \t like other crl chars. */
5767 || (c != '\t'
5768 && it->glyph_row
5769 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5770 || (c != '\n' && c != '\t'))
5771 : (nbsp_or_shy
5772 || CHAR_BYTE8_P (c)
5773 || ! CHAR_PRINTABLE_P (c))))
5774 {
5775 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5776 or a non-printable character which must be displayed
5777 either as '\003' or as `^C' where the '\\' and '^'
5778 can be defined in the display table. Fill
5779 IT->ctl_chars with glyphs for what we have to
5780 display. Then, set IT->dpvec to these glyphs. */
5781 Lisp_Object gc;
5782 int ctl_len;
5783 int face_id, lface_id = 0 ;
5784 int escape_glyph;
5785
5786 /* Handle control characters with ^. */
5787
5788 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5789 {
5790 int g;
5791
5792 g = '^'; /* default glyph for Control */
5793 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5794 if (it->dp
5795 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5796 && GLYPH_CODE_CHAR_VALID_P (gc))
5797 {
5798 g = GLYPH_CODE_CHAR (gc);
5799 lface_id = GLYPH_CODE_FACE (gc);
5800 }
5801 if (lface_id)
5802 {
5803 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5804 }
5805 else if (it->f == last_escape_glyph_frame
5806 && it->face_id == last_escape_glyph_face_id)
5807 {
5808 face_id = last_escape_glyph_merged_face_id;
5809 }
5810 else
5811 {
5812 /* Merge the escape-glyph face into the current face. */
5813 face_id = merge_faces (it->f, Qescape_glyph, 0,
5814 it->face_id);
5815 last_escape_glyph_frame = it->f;
5816 last_escape_glyph_face_id = it->face_id;
5817 last_escape_glyph_merged_face_id = face_id;
5818 }
5819
5820 XSETINT (it->ctl_chars[0], g);
5821 XSETINT (it->ctl_chars[1], c ^ 0100);
5822 ctl_len = 2;
5823 goto display_control;
5824 }
5825
5826 /* Handle non-break space in the mode where it only gets
5827 highlighting. */
5828
5829 if (EQ (Vnobreak_char_display, Qt)
5830 && nbsp_or_shy == char_is_nbsp)
5831 {
5832 /* Merge the no-break-space face into the current face. */
5833 face_id = merge_faces (it->f, Qnobreak_space, 0,
5834 it->face_id);
5835
5836 c = ' ';
5837 XSETINT (it->ctl_chars[0], ' ');
5838 ctl_len = 1;
5839 goto display_control;
5840 }
5841
5842 /* Handle sequences that start with the "escape glyph". */
5843
5844 /* the default escape glyph is \. */
5845 escape_glyph = '\\';
5846
5847 if (it->dp
5848 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5849 && GLYPH_CODE_CHAR_VALID_P (gc))
5850 {
5851 escape_glyph = GLYPH_CODE_CHAR (gc);
5852 lface_id = GLYPH_CODE_FACE (gc);
5853 }
5854 if (lface_id)
5855 {
5856 /* The display table specified a face.
5857 Merge it into face_id and also into escape_glyph. */
5858 face_id = merge_faces (it->f, Qt, lface_id,
5859 it->face_id);
5860 }
5861 else if (it->f == last_escape_glyph_frame
5862 && it->face_id == last_escape_glyph_face_id)
5863 {
5864 face_id = last_escape_glyph_merged_face_id;
5865 }
5866 else
5867 {
5868 /* Merge the escape-glyph face into the current face. */
5869 face_id = merge_faces (it->f, Qescape_glyph, 0,
5870 it->face_id);
5871 last_escape_glyph_frame = it->f;
5872 last_escape_glyph_face_id = it->face_id;
5873 last_escape_glyph_merged_face_id = face_id;
5874 }
5875
5876 /* Handle soft hyphens in the mode where they only get
5877 highlighting. */
5878
5879 if (EQ (Vnobreak_char_display, Qt)
5880 && nbsp_or_shy == char_is_soft_hyphen)
5881 {
5882 XSETINT (it->ctl_chars[0], '-');
5883 ctl_len = 1;
5884 goto display_control;
5885 }
5886
5887 /* Handle non-break space and soft hyphen
5888 with the escape glyph. */
5889
5890 if (nbsp_or_shy)
5891 {
5892 XSETINT (it->ctl_chars[0], escape_glyph);
5893 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5894 XSETINT (it->ctl_chars[1], c);
5895 ctl_len = 2;
5896 goto display_control;
5897 }
5898
5899 {
5900 char str[10];
5901 int len, i;
5902
5903 if (CHAR_BYTE8_P (c))
5904 /* Display \200 instead of \17777600. */
5905 c = CHAR_TO_BYTE8 (c);
5906 len = sprintf (str, "%03o", c);
5907
5908 XSETINT (it->ctl_chars[0], escape_glyph);
5909 for (i = 0; i < len; i++)
5910 XSETINT (it->ctl_chars[i + 1], str[i]);
5911 ctl_len = len + 1;
5912 }
5913
5914 display_control:
5915 /* Set up IT->dpvec and return first character from it. */
5916 it->dpvec_char_len = it->len;
5917 it->dpvec = it->ctl_chars;
5918 it->dpend = it->dpvec + ctl_len;
5919 it->current.dpvec_index = 0;
5920 it->dpvec_face_id = face_id;
5921 it->saved_face_id = it->face_id;
5922 it->method = GET_FROM_DISPLAY_VECTOR;
5923 it->ellipsis_p = 0;
5924 goto get_next;
5925 }
5926 it->char_to_display = c;
5927 }
5928 else if (success_p)
5929 {
5930 it->char_to_display = it->c;
5931 }
5932 }
5933
5934 /* Adjust face id for a multibyte character. There are no multibyte
5935 character in unibyte text. */
5936 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5937 && it->multibyte_p
5938 && success_p
5939 && FRAME_WINDOW_P (it->f))
5940 {
5941 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5942
5943 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5944 {
5945 /* Automatic composition with glyph-string. */
5946 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5947
5948 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5949 }
5950 else
5951 {
5952 EMACS_INT pos = (it->s ? -1
5953 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5954 : IT_CHARPOS (*it));
5955
5956 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
5957 it->string);
5958 }
5959 }
5960
5961 done:
5962 /* Is this character the last one of a run of characters with
5963 box? If yes, set IT->end_of_box_run_p to 1. */
5964 if (it->face_box_p
5965 && it->s == NULL)
5966 {
5967 if (it->method == GET_FROM_STRING && it->sp)
5968 {
5969 int face_id = underlying_face_id (it);
5970 struct face *face = FACE_FROM_ID (it->f, face_id);
5971
5972 if (face)
5973 {
5974 if (face->box == FACE_NO_BOX)
5975 {
5976 /* If the box comes from face properties in a
5977 display string, check faces in that string. */
5978 int string_face_id = face_after_it_pos (it);
5979 it->end_of_box_run_p
5980 = (FACE_FROM_ID (it->f, string_face_id)->box
5981 == FACE_NO_BOX);
5982 }
5983 /* Otherwise, the box comes from the underlying face.
5984 If this is the last string character displayed, check
5985 the next buffer location. */
5986 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5987 && (it->current.overlay_string_index
5988 == it->n_overlay_strings - 1))
5989 {
5990 EMACS_INT ignore;
5991 int next_face_id;
5992 struct text_pos pos = it->current.pos;
5993 INC_TEXT_POS (pos, it->multibyte_p);
5994
5995 next_face_id = face_at_buffer_position
5996 (it->w, CHARPOS (pos), it->region_beg_charpos,
5997 it->region_end_charpos, &ignore,
5998 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5999 -1);
6000 it->end_of_box_run_p
6001 = (FACE_FROM_ID (it->f, next_face_id)->box
6002 == FACE_NO_BOX);
6003 }
6004 }
6005 }
6006 else
6007 {
6008 int face_id = face_after_it_pos (it);
6009 it->end_of_box_run_p
6010 = (face_id != it->face_id
6011 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6012 }
6013 }
6014
6015 /* Value is 0 if end of buffer or string reached. */
6016 return success_p;
6017 }
6018
6019
6020 /* Move IT to the next display element.
6021
6022 RESEAT_P non-zero means if called on a newline in buffer text,
6023 skip to the next visible line start.
6024
6025 Functions get_next_display_element and set_iterator_to_next are
6026 separate because I find this arrangement easier to handle than a
6027 get_next_display_element function that also increments IT's
6028 position. The way it is we can first look at an iterator's current
6029 display element, decide whether it fits on a line, and if it does,
6030 increment the iterator position. The other way around we probably
6031 would either need a flag indicating whether the iterator has to be
6032 incremented the next time, or we would have to implement a
6033 decrement position function which would not be easy to write. */
6034
6035 void
6036 set_iterator_to_next (struct it *it, int reseat_p)
6037 {
6038 /* Reset flags indicating start and end of a sequence of characters
6039 with box. Reset them at the start of this function because
6040 moving the iterator to a new position might set them. */
6041 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6042
6043 switch (it->method)
6044 {
6045 case GET_FROM_BUFFER:
6046 /* The current display element of IT is a character from
6047 current_buffer. Advance in the buffer, and maybe skip over
6048 invisible lines that are so because of selective display. */
6049 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6050 reseat_at_next_visible_line_start (it, 0);
6051 else if (it->cmp_it.id >= 0)
6052 {
6053 /* We are currently getting glyphs from a composition. */
6054 int i;
6055
6056 if (! it->bidi_p)
6057 {
6058 IT_CHARPOS (*it) += it->cmp_it.nchars;
6059 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6060 if (it->cmp_it.to < it->cmp_it.nglyphs)
6061 {
6062 it->cmp_it.from = it->cmp_it.to;
6063 }
6064 else
6065 {
6066 it->cmp_it.id = -1;
6067 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6068 IT_BYTEPOS (*it),
6069 it->end_charpos, Qnil);
6070 }
6071 }
6072 else if (! it->cmp_it.reversed_p)
6073 {
6074 /* Composition created while scanning forward. */
6075 /* Update IT's char/byte positions to point to the first
6076 character of the next grapheme cluster, or to the
6077 character visually after the current composition. */
6078 for (i = 0; i < it->cmp_it.nchars; i++)
6079 bidi_move_to_visually_next (&it->bidi_it);
6080 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6081 IT_CHARPOS (*it) = it->bidi_it.charpos;
6082
6083 if (it->cmp_it.to < it->cmp_it.nglyphs)
6084 {
6085 /* Proceed to the next grapheme cluster. */
6086 it->cmp_it.from = it->cmp_it.to;
6087 }
6088 else
6089 {
6090 /* No more grapheme clusters in this composition.
6091 Find the next stop position. */
6092 EMACS_INT stop = it->end_charpos;
6093 if (it->bidi_it.scan_dir < 0)
6094 /* Now we are scanning backward and don't know
6095 where to stop. */
6096 stop = -1;
6097 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6098 IT_BYTEPOS (*it), stop, Qnil);
6099 }
6100 }
6101 else
6102 {
6103 /* Composition created while scanning backward. */
6104 /* Update IT's char/byte positions to point to the last
6105 character of the previous grapheme cluster, or the
6106 character visually after the current composition. */
6107 for (i = 0; i < it->cmp_it.nchars; i++)
6108 bidi_move_to_visually_next (&it->bidi_it);
6109 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6110 IT_CHARPOS (*it) = it->bidi_it.charpos;
6111 if (it->cmp_it.from > 0)
6112 {
6113 /* Proceed to the previous grapheme cluster. */
6114 it->cmp_it.to = it->cmp_it.from;
6115 }
6116 else
6117 {
6118 /* No more grapheme clusters in this composition.
6119 Find the next stop position. */
6120 EMACS_INT stop = it->end_charpos;
6121 if (it->bidi_it.scan_dir < 0)
6122 /* Now we are scanning backward and don't know
6123 where to stop. */
6124 stop = -1;
6125 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6126 IT_BYTEPOS (*it), stop, Qnil);
6127 }
6128 }
6129 }
6130 else
6131 {
6132 xassert (it->len != 0);
6133
6134 if (!it->bidi_p)
6135 {
6136 IT_BYTEPOS (*it) += it->len;
6137 IT_CHARPOS (*it) += 1;
6138 }
6139 else
6140 {
6141 int prev_scan_dir = it->bidi_it.scan_dir;
6142 /* If this is a new paragraph, determine its base
6143 direction (a.k.a. its base embedding level). */
6144 if (it->bidi_it.new_paragraph)
6145 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6146 bidi_move_to_visually_next (&it->bidi_it);
6147 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6148 IT_CHARPOS (*it) = it->bidi_it.charpos;
6149 if (prev_scan_dir != it->bidi_it.scan_dir)
6150 {
6151 /* As the scan direction was changed, we must
6152 re-compute the stop position for composition. */
6153 EMACS_INT stop = it->end_charpos;
6154 if (it->bidi_it.scan_dir < 0)
6155 stop = -1;
6156 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6157 IT_BYTEPOS (*it), stop, Qnil);
6158 }
6159 }
6160 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6161 }
6162 break;
6163
6164 case GET_FROM_C_STRING:
6165 /* Current display element of IT is from a C string. */
6166 IT_BYTEPOS (*it) += it->len;
6167 IT_CHARPOS (*it) += 1;
6168 break;
6169
6170 case GET_FROM_DISPLAY_VECTOR:
6171 /* Current display element of IT is from a display table entry.
6172 Advance in the display table definition. Reset it to null if
6173 end reached, and continue with characters from buffers/
6174 strings. */
6175 ++it->current.dpvec_index;
6176
6177 /* Restore face of the iterator to what they were before the
6178 display vector entry (these entries may contain faces). */
6179 it->face_id = it->saved_face_id;
6180
6181 if (it->dpvec + it->current.dpvec_index == it->dpend)
6182 {
6183 int recheck_faces = it->ellipsis_p;
6184
6185 if (it->s)
6186 it->method = GET_FROM_C_STRING;
6187 else if (STRINGP (it->string))
6188 it->method = GET_FROM_STRING;
6189 else
6190 {
6191 it->method = GET_FROM_BUFFER;
6192 it->object = it->w->buffer;
6193 }
6194
6195 it->dpvec = NULL;
6196 it->current.dpvec_index = -1;
6197
6198 /* Skip over characters which were displayed via IT->dpvec. */
6199 if (it->dpvec_char_len < 0)
6200 reseat_at_next_visible_line_start (it, 1);
6201 else if (it->dpvec_char_len > 0)
6202 {
6203 if (it->method == GET_FROM_STRING
6204 && it->n_overlay_strings > 0)
6205 it->ignore_overlay_strings_at_pos_p = 1;
6206 it->len = it->dpvec_char_len;
6207 set_iterator_to_next (it, reseat_p);
6208 }
6209
6210 /* Maybe recheck faces after display vector */
6211 if (recheck_faces)
6212 it->stop_charpos = IT_CHARPOS (*it);
6213 }
6214 break;
6215
6216 case GET_FROM_STRING:
6217 /* Current display element is a character from a Lisp string. */
6218 xassert (it->s == NULL && STRINGP (it->string));
6219 if (it->cmp_it.id >= 0)
6220 {
6221 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6222 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6223 if (it->cmp_it.to < it->cmp_it.nglyphs)
6224 it->cmp_it.from = it->cmp_it.to;
6225 else
6226 {
6227 it->cmp_it.id = -1;
6228 composition_compute_stop_pos (&it->cmp_it,
6229 IT_STRING_CHARPOS (*it),
6230 IT_STRING_BYTEPOS (*it),
6231 it->end_charpos, it->string);
6232 }
6233 }
6234 else
6235 {
6236 IT_STRING_BYTEPOS (*it) += it->len;
6237 IT_STRING_CHARPOS (*it) += 1;
6238 }
6239
6240 consider_string_end:
6241
6242 if (it->current.overlay_string_index >= 0)
6243 {
6244 /* IT->string is an overlay string. Advance to the
6245 next, if there is one. */
6246 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6247 {
6248 it->ellipsis_p = 0;
6249 next_overlay_string (it);
6250 if (it->ellipsis_p)
6251 setup_for_ellipsis (it, 0);
6252 }
6253 }
6254 else
6255 {
6256 /* IT->string is not an overlay string. If we reached
6257 its end, and there is something on IT->stack, proceed
6258 with what is on the stack. This can be either another
6259 string, this time an overlay string, or a buffer. */
6260 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6261 && it->sp > 0)
6262 {
6263 pop_it (it);
6264 if (it->method == GET_FROM_STRING)
6265 goto consider_string_end;
6266 }
6267 }
6268 break;
6269
6270 case GET_FROM_IMAGE:
6271 case GET_FROM_STRETCH:
6272 /* The position etc with which we have to proceed are on
6273 the stack. The position may be at the end of a string,
6274 if the `display' property takes up the whole string. */
6275 xassert (it->sp > 0);
6276 pop_it (it);
6277 if (it->method == GET_FROM_STRING)
6278 goto consider_string_end;
6279 break;
6280
6281 default:
6282 /* There are no other methods defined, so this should be a bug. */
6283 abort ();
6284 }
6285
6286 xassert (it->method != GET_FROM_STRING
6287 || (STRINGP (it->string)
6288 && IT_STRING_CHARPOS (*it) >= 0));
6289 }
6290
6291 /* Load IT's display element fields with information about the next
6292 display element which comes from a display table entry or from the
6293 result of translating a control character to one of the forms `^C'
6294 or `\003'.
6295
6296 IT->dpvec holds the glyphs to return as characters.
6297 IT->saved_face_id holds the face id before the display vector--it
6298 is restored into IT->face_id in set_iterator_to_next. */
6299
6300 static int
6301 next_element_from_display_vector (struct it *it)
6302 {
6303 Lisp_Object gc;
6304
6305 /* Precondition. */
6306 xassert (it->dpvec && it->current.dpvec_index >= 0);
6307
6308 it->face_id = it->saved_face_id;
6309
6310 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6311 That seemed totally bogus - so I changed it... */
6312 gc = it->dpvec[it->current.dpvec_index];
6313
6314 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6315 {
6316 it->c = GLYPH_CODE_CHAR (gc);
6317 it->len = CHAR_BYTES (it->c);
6318
6319 /* The entry may contain a face id to use. Such a face id is
6320 the id of a Lisp face, not a realized face. A face id of
6321 zero means no face is specified. */
6322 if (it->dpvec_face_id >= 0)
6323 it->face_id = it->dpvec_face_id;
6324 else
6325 {
6326 int lface_id = GLYPH_CODE_FACE (gc);
6327 if (lface_id > 0)
6328 it->face_id = merge_faces (it->f, Qt, lface_id,
6329 it->saved_face_id);
6330 }
6331 }
6332 else
6333 /* Display table entry is invalid. Return a space. */
6334 it->c = ' ', it->len = 1;
6335
6336 /* Don't change position and object of the iterator here. They are
6337 still the values of the character that had this display table
6338 entry or was translated, and that's what we want. */
6339 it->what = IT_CHARACTER;
6340 return 1;
6341 }
6342
6343
6344 /* Load IT with the next display element from Lisp string IT->string.
6345 IT->current.string_pos is the current position within the string.
6346 If IT->current.overlay_string_index >= 0, the Lisp string is an
6347 overlay string. */
6348
6349 static int
6350 next_element_from_string (struct it *it)
6351 {
6352 struct text_pos position;
6353
6354 xassert (STRINGP (it->string));
6355 xassert (IT_STRING_CHARPOS (*it) >= 0);
6356 position = it->current.string_pos;
6357
6358 /* Time to check for invisible text? */
6359 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6360 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6361 {
6362 handle_stop (it);
6363
6364 /* Since a handler may have changed IT->method, we must
6365 recurse here. */
6366 return GET_NEXT_DISPLAY_ELEMENT (it);
6367 }
6368
6369 if (it->current.overlay_string_index >= 0)
6370 {
6371 /* Get the next character from an overlay string. In overlay
6372 strings, There is no field width or padding with spaces to
6373 do. */
6374 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6375 {
6376 it->what = IT_EOB;
6377 return 0;
6378 }
6379 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6380 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6381 && next_element_from_composition (it))
6382 {
6383 return 1;
6384 }
6385 else if (STRING_MULTIBYTE (it->string))
6386 {
6387 const unsigned char *s = (SDATA (it->string)
6388 + IT_STRING_BYTEPOS (*it));
6389 it->c = string_char_and_length (s, &it->len);
6390 }
6391 else
6392 {
6393 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6394 it->len = 1;
6395 }
6396 }
6397 else
6398 {
6399 /* Get the next character from a Lisp string that is not an
6400 overlay string. Such strings come from the mode line, for
6401 example. We may have to pad with spaces, or truncate the
6402 string. See also next_element_from_c_string. */
6403 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6404 {
6405 it->what = IT_EOB;
6406 return 0;
6407 }
6408 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6409 {
6410 /* Pad with spaces. */
6411 it->c = ' ', it->len = 1;
6412 CHARPOS (position) = BYTEPOS (position) = -1;
6413 }
6414 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6415 IT_STRING_BYTEPOS (*it), it->string_nchars)
6416 && next_element_from_composition (it))
6417 {
6418 return 1;
6419 }
6420 else if (STRING_MULTIBYTE (it->string))
6421 {
6422 const unsigned char *s = (SDATA (it->string)
6423 + IT_STRING_BYTEPOS (*it));
6424 it->c = string_char_and_length (s, &it->len);
6425 }
6426 else
6427 {
6428 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6429 it->len = 1;
6430 }
6431 }
6432
6433 /* Record what we have and where it came from. */
6434 it->what = IT_CHARACTER;
6435 it->object = it->string;
6436 it->position = position;
6437 return 1;
6438 }
6439
6440
6441 /* Load IT with next display element from C string IT->s.
6442 IT->string_nchars is the maximum number of characters to return
6443 from the string. IT->end_charpos may be greater than
6444 IT->string_nchars when this function is called, in which case we
6445 may have to return padding spaces. Value is zero if end of string
6446 reached, including padding spaces. */
6447
6448 static int
6449 next_element_from_c_string (struct it *it)
6450 {
6451 int success_p = 1;
6452
6453 xassert (it->s);
6454 it->what = IT_CHARACTER;
6455 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6456 it->object = Qnil;
6457
6458 /* IT's position can be greater IT->string_nchars in case a field
6459 width or precision has been specified when the iterator was
6460 initialized. */
6461 if (IT_CHARPOS (*it) >= it->end_charpos)
6462 {
6463 /* End of the game. */
6464 it->what = IT_EOB;
6465 success_p = 0;
6466 }
6467 else if (IT_CHARPOS (*it) >= it->string_nchars)
6468 {
6469 /* Pad with spaces. */
6470 it->c = ' ', it->len = 1;
6471 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6472 }
6473 else if (it->multibyte_p)
6474 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6475 else
6476 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6477
6478 return success_p;
6479 }
6480
6481
6482 /* Set up IT to return characters from an ellipsis, if appropriate.
6483 The definition of the ellipsis glyphs may come from a display table
6484 entry. This function fills IT with the first glyph from the
6485 ellipsis if an ellipsis is to be displayed. */
6486
6487 static int
6488 next_element_from_ellipsis (struct it *it)
6489 {
6490 if (it->selective_display_ellipsis_p)
6491 setup_for_ellipsis (it, it->len);
6492 else
6493 {
6494 /* The face at the current position may be different from the
6495 face we find after the invisible text. Remember what it
6496 was in IT->saved_face_id, and signal that it's there by
6497 setting face_before_selective_p. */
6498 it->saved_face_id = it->face_id;
6499 it->method = GET_FROM_BUFFER;
6500 it->object = it->w->buffer;
6501 reseat_at_next_visible_line_start (it, 1);
6502 it->face_before_selective_p = 1;
6503 }
6504
6505 return GET_NEXT_DISPLAY_ELEMENT (it);
6506 }
6507
6508
6509 /* Deliver an image display element. The iterator IT is already
6510 filled with image information (done in handle_display_prop). Value
6511 is always 1. */
6512
6513
6514 static int
6515 next_element_from_image (struct it *it)
6516 {
6517 it->what = IT_IMAGE;
6518 it->ignore_overlay_strings_at_pos_p = 0;
6519 return 1;
6520 }
6521
6522
6523 /* Fill iterator IT with next display element from a stretch glyph
6524 property. IT->object is the value of the text property. Value is
6525 always 1. */
6526
6527 static int
6528 next_element_from_stretch (struct it *it)
6529 {
6530 it->what = IT_STRETCH;
6531 return 1;
6532 }
6533
6534 /* Scan forward from CHARPOS in the current buffer, until we find a
6535 stop position > current IT's position. Then handle the stop
6536 position before that. This is called when we bump into a stop
6537 position while reordering bidirectional text. CHARPOS should be
6538 the last previously processed stop_pos (or BEGV, if none were
6539 processed yet) whose position is less that IT's current
6540 position. */
6541
6542 static void
6543 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6544 {
6545 EMACS_INT where_we_are = IT_CHARPOS (*it);
6546 struct display_pos save_current = it->current;
6547 struct text_pos save_position = it->position;
6548 struct text_pos pos1;
6549 EMACS_INT next_stop;
6550
6551 /* Scan in strict logical order. */
6552 it->bidi_p = 0;
6553 do
6554 {
6555 it->prev_stop = charpos;
6556 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6557 reseat_1 (it, pos1, 0);
6558 compute_stop_pos (it);
6559 /* We must advance forward, right? */
6560 if (it->stop_charpos <= it->prev_stop)
6561 abort ();
6562 charpos = it->stop_charpos;
6563 }
6564 while (charpos <= where_we_are);
6565
6566 next_stop = it->stop_charpos;
6567 it->stop_charpos = it->prev_stop;
6568 it->bidi_p = 1;
6569 it->current = save_current;
6570 it->position = save_position;
6571 handle_stop (it);
6572 it->stop_charpos = next_stop;
6573 }
6574
6575 /* Load IT with the next display element from current_buffer. Value
6576 is zero if end of buffer reached. IT->stop_charpos is the next
6577 position at which to stop and check for text properties or buffer
6578 end. */
6579
6580 static int
6581 next_element_from_buffer (struct it *it)
6582 {
6583 int success_p = 1;
6584
6585 xassert (IT_CHARPOS (*it) >= BEGV);
6586
6587 /* With bidi reordering, the character to display might not be the
6588 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6589 we were reseat()ed to a new buffer position, which is potentially
6590 a different paragraph. */
6591 if (it->bidi_p && it->bidi_it.first_elt)
6592 {
6593 it->bidi_it.charpos = IT_CHARPOS (*it);
6594 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6595 if (it->bidi_it.bytepos == ZV_BYTE)
6596 {
6597 /* Nothing to do, but reset the FIRST_ELT flag, like
6598 bidi_paragraph_init does, because we are not going to
6599 call it. */
6600 it->bidi_it.first_elt = 0;
6601 }
6602 else if (it->bidi_it.bytepos == BEGV_BYTE
6603 /* FIXME: Should support all Unicode line separators. */
6604 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6605 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6606 {
6607 /* If we are at the beginning of a line, we can produce the
6608 next element right away. */
6609 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6610 bidi_move_to_visually_next (&it->bidi_it);
6611 }
6612 else
6613 {
6614 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6615
6616 /* We need to prime the bidi iterator starting at the line's
6617 beginning, before we will be able to produce the next
6618 element. */
6619 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6620 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6621 it->bidi_it.charpos = IT_CHARPOS (*it);
6622 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6623 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6624 do
6625 {
6626 /* Now return to buffer position where we were asked to
6627 get the next display element, and produce that. */
6628 bidi_move_to_visually_next (&it->bidi_it);
6629 }
6630 while (it->bidi_it.bytepos != orig_bytepos
6631 && it->bidi_it.bytepos < ZV_BYTE);
6632 }
6633
6634 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6635 /* Adjust IT's position information to where we ended up. */
6636 IT_CHARPOS (*it) = it->bidi_it.charpos;
6637 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6638 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6639 {
6640 EMACS_INT stop = it->end_charpos;
6641 if (it->bidi_it.scan_dir < 0)
6642 stop = -1;
6643 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6644 IT_BYTEPOS (*it), stop, Qnil);
6645 }
6646 }
6647
6648 if (IT_CHARPOS (*it) >= it->stop_charpos)
6649 {
6650 if (IT_CHARPOS (*it) >= it->end_charpos)
6651 {
6652 int overlay_strings_follow_p;
6653
6654 /* End of the game, except when overlay strings follow that
6655 haven't been returned yet. */
6656 if (it->overlay_strings_at_end_processed_p)
6657 overlay_strings_follow_p = 0;
6658 else
6659 {
6660 it->overlay_strings_at_end_processed_p = 1;
6661 overlay_strings_follow_p = get_overlay_strings (it, 0);
6662 }
6663
6664 if (overlay_strings_follow_p)
6665 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6666 else
6667 {
6668 it->what = IT_EOB;
6669 it->position = it->current.pos;
6670 success_p = 0;
6671 }
6672 }
6673 else if (!(!it->bidi_p
6674 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6675 || IT_CHARPOS (*it) == it->stop_charpos))
6676 {
6677 /* With bidi non-linear iteration, we could find ourselves
6678 far beyond the last computed stop_charpos, with several
6679 other stop positions in between that we missed. Scan
6680 them all now, in buffer's logical order, until we find
6681 and handle the last stop_charpos that precedes our
6682 current position. */
6683 handle_stop_backwards (it, it->stop_charpos);
6684 return GET_NEXT_DISPLAY_ELEMENT (it);
6685 }
6686 else
6687 {
6688 if (it->bidi_p)
6689 {
6690 /* Take note of the stop position we just moved across,
6691 for when we will move back across it. */
6692 it->prev_stop = it->stop_charpos;
6693 /* If we are at base paragraph embedding level, take
6694 note of the last stop position seen at this
6695 level. */
6696 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6697 it->base_level_stop = it->stop_charpos;
6698 }
6699 handle_stop (it);
6700 return GET_NEXT_DISPLAY_ELEMENT (it);
6701 }
6702 }
6703 else if (it->bidi_p
6704 /* We can sometimes back up for reasons that have nothing
6705 to do with bidi reordering. E.g., compositions. The
6706 code below is only needed when we are above the base
6707 embedding level, so test for that explicitly. */
6708 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6709 && IT_CHARPOS (*it) < it->prev_stop)
6710 {
6711 if (it->base_level_stop <= 0)
6712 it->base_level_stop = BEGV;
6713 if (IT_CHARPOS (*it) < it->base_level_stop)
6714 abort ();
6715 handle_stop_backwards (it, it->base_level_stop);
6716 return GET_NEXT_DISPLAY_ELEMENT (it);
6717 }
6718 else
6719 {
6720 /* No face changes, overlays etc. in sight, so just return a
6721 character from current_buffer. */
6722 unsigned char *p;
6723 EMACS_INT stop;
6724
6725 /* Maybe run the redisplay end trigger hook. Performance note:
6726 This doesn't seem to cost measurable time. */
6727 if (it->redisplay_end_trigger_charpos
6728 && it->glyph_row
6729 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6730 run_redisplay_end_trigger_hook (it);
6731
6732 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6733 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6734 stop)
6735 && next_element_from_composition (it))
6736 {
6737 return 1;
6738 }
6739
6740 /* Get the next character, maybe multibyte. */
6741 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6742 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6743 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6744 else
6745 it->c = *p, it->len = 1;
6746
6747 /* Record what we have and where it came from. */
6748 it->what = IT_CHARACTER;
6749 it->object = it->w->buffer;
6750 it->position = it->current.pos;
6751
6752 /* Normally we return the character found above, except when we
6753 really want to return an ellipsis for selective display. */
6754 if (it->selective)
6755 {
6756 if (it->c == '\n')
6757 {
6758 /* A value of selective > 0 means hide lines indented more
6759 than that number of columns. */
6760 if (it->selective > 0
6761 && IT_CHARPOS (*it) + 1 < ZV
6762 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6763 IT_BYTEPOS (*it) + 1,
6764 (double) it->selective)) /* iftc */
6765 {
6766 success_p = next_element_from_ellipsis (it);
6767 it->dpvec_char_len = -1;
6768 }
6769 }
6770 else if (it->c == '\r' && it->selective == -1)
6771 {
6772 /* A value of selective == -1 means that everything from the
6773 CR to the end of the line is invisible, with maybe an
6774 ellipsis displayed for it. */
6775 success_p = next_element_from_ellipsis (it);
6776 it->dpvec_char_len = -1;
6777 }
6778 }
6779 }
6780
6781 /* Value is zero if end of buffer reached. */
6782 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6783 return success_p;
6784 }
6785
6786
6787 /* Run the redisplay end trigger hook for IT. */
6788
6789 static void
6790 run_redisplay_end_trigger_hook (struct it *it)
6791 {
6792 Lisp_Object args[3];
6793
6794 /* IT->glyph_row should be non-null, i.e. we should be actually
6795 displaying something, or otherwise we should not run the hook. */
6796 xassert (it->glyph_row);
6797
6798 /* Set up hook arguments. */
6799 args[0] = Qredisplay_end_trigger_functions;
6800 args[1] = it->window;
6801 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6802 it->redisplay_end_trigger_charpos = 0;
6803
6804 /* Since we are *trying* to run these functions, don't try to run
6805 them again, even if they get an error. */
6806 it->w->redisplay_end_trigger = Qnil;
6807 Frun_hook_with_args (3, args);
6808
6809 /* Notice if it changed the face of the character we are on. */
6810 handle_face_prop (it);
6811 }
6812
6813
6814 /* Deliver a composition display element. Unlike the other
6815 next_element_from_XXX, this function is not registered in the array
6816 get_next_element[]. It is called from next_element_from_buffer and
6817 next_element_from_string when necessary. */
6818
6819 static int
6820 next_element_from_composition (struct it *it)
6821 {
6822 it->what = IT_COMPOSITION;
6823 it->len = it->cmp_it.nbytes;
6824 if (STRINGP (it->string))
6825 {
6826 if (it->c < 0)
6827 {
6828 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6829 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6830 return 0;
6831 }
6832 it->position = it->current.string_pos;
6833 it->object = it->string;
6834 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6835 IT_STRING_BYTEPOS (*it), it->string);
6836 }
6837 else
6838 {
6839 if (it->c < 0)
6840 {
6841 IT_CHARPOS (*it) += it->cmp_it.nchars;
6842 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6843 if (it->bidi_p)
6844 {
6845 if (it->bidi_it.new_paragraph)
6846 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6847 /* Resync the bidi iterator with IT's new position.
6848 FIXME: this doesn't support bidirectional text. */
6849 while (it->bidi_it.charpos < IT_CHARPOS (*it))
6850 bidi_move_to_visually_next (&it->bidi_it);
6851 }
6852 return 0;
6853 }
6854 it->position = it->current.pos;
6855 it->object = it->w->buffer;
6856 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6857 IT_BYTEPOS (*it), Qnil);
6858 }
6859 return 1;
6860 }
6861
6862
6863 \f
6864 /***********************************************************************
6865 Moving an iterator without producing glyphs
6866 ***********************************************************************/
6867
6868 /* Check if iterator is at a position corresponding to a valid buffer
6869 position after some move_it_ call. */
6870
6871 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6872 ((it)->method == GET_FROM_STRING \
6873 ? IT_STRING_CHARPOS (*it) == 0 \
6874 : 1)
6875
6876
6877 /* Move iterator IT to a specified buffer or X position within one
6878 line on the display without producing glyphs.
6879
6880 OP should be a bit mask including some or all of these bits:
6881 MOVE_TO_X: Stop upon reaching x-position TO_X.
6882 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6883 Regardless of OP's value, stop upon reaching the end of the display line.
6884
6885 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6886 This means, in particular, that TO_X includes window's horizontal
6887 scroll amount.
6888
6889 The return value has several possible values that
6890 say what condition caused the scan to stop:
6891
6892 MOVE_POS_MATCH_OR_ZV
6893 - when TO_POS or ZV was reached.
6894
6895 MOVE_X_REACHED
6896 -when TO_X was reached before TO_POS or ZV were reached.
6897
6898 MOVE_LINE_CONTINUED
6899 - when we reached the end of the display area and the line must
6900 be continued.
6901
6902 MOVE_LINE_TRUNCATED
6903 - when we reached the end of the display area and the line is
6904 truncated.
6905
6906 MOVE_NEWLINE_OR_CR
6907 - when we stopped at a line end, i.e. a newline or a CR and selective
6908 display is on. */
6909
6910 static enum move_it_result
6911 move_it_in_display_line_to (struct it *it,
6912 EMACS_INT to_charpos, int to_x,
6913 enum move_operation_enum op)
6914 {
6915 enum move_it_result result = MOVE_UNDEFINED;
6916 struct glyph_row *saved_glyph_row;
6917 struct it wrap_it, atpos_it, atx_it;
6918 int may_wrap = 0;
6919 enum it_method prev_method = it->method;
6920 EMACS_INT prev_pos = IT_CHARPOS (*it);
6921
6922 /* Don't produce glyphs in produce_glyphs. */
6923 saved_glyph_row = it->glyph_row;
6924 it->glyph_row = NULL;
6925
6926 /* Use wrap_it to save a copy of IT wherever a word wrap could
6927 occur. Use atpos_it to save a copy of IT at the desired buffer
6928 position, if found, so that we can scan ahead and check if the
6929 word later overshoots the window edge. Use atx_it similarly, for
6930 pixel positions. */
6931 wrap_it.sp = -1;
6932 atpos_it.sp = -1;
6933 atx_it.sp = -1;
6934
6935 #define BUFFER_POS_REACHED_P() \
6936 ((op & MOVE_TO_POS) != 0 \
6937 && BUFFERP (it->object) \
6938 && (IT_CHARPOS (*it) == to_charpos \
6939 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
6940 && (it->method == GET_FROM_BUFFER \
6941 || (it->method == GET_FROM_DISPLAY_VECTOR \
6942 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6943
6944 /* If there's a line-/wrap-prefix, handle it. */
6945 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6946 && it->current_y < it->last_visible_y)
6947 handle_line_prefix (it);
6948
6949 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
6950 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6951
6952 while (1)
6953 {
6954 int x, i, ascent = 0, descent = 0;
6955
6956 /* Utility macro to reset an iterator with x, ascent, and descent. */
6957 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6958 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6959 (IT)->max_descent = descent)
6960
6961 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6962 glyph). */
6963 if ((op & MOVE_TO_POS) != 0
6964 && BUFFERP (it->object)
6965 && it->method == GET_FROM_BUFFER
6966 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
6967 || (it->bidi_p
6968 && (prev_method == GET_FROM_IMAGE
6969 || prev_method == GET_FROM_STRETCH)
6970 /* Passed TO_CHARPOS from left to right. */
6971 && ((prev_pos < to_charpos
6972 && IT_CHARPOS (*it) > to_charpos)
6973 /* Passed TO_CHARPOS from right to left. */
6974 || (prev_pos > to_charpos
6975 && IT_CHARPOS (*it) < to_charpos)))))
6976 {
6977 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6978 {
6979 result = MOVE_POS_MATCH_OR_ZV;
6980 break;
6981 }
6982 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6983 /* If wrap_it is valid, the current position might be in a
6984 word that is wrapped. So, save the iterator in
6985 atpos_it and continue to see if wrapping happens. */
6986 atpos_it = *it;
6987 }
6988
6989 prev_method = it->method;
6990 if (it->method == GET_FROM_BUFFER)
6991 prev_pos = IT_CHARPOS (*it);
6992 /* Stop when ZV reached.
6993 We used to stop here when TO_CHARPOS reached as well, but that is
6994 too soon if this glyph does not fit on this line. So we handle it
6995 explicitly below. */
6996 if (!get_next_display_element (it))
6997 {
6998 result = MOVE_POS_MATCH_OR_ZV;
6999 break;
7000 }
7001
7002 if (it->line_wrap == TRUNCATE)
7003 {
7004 if (BUFFER_POS_REACHED_P ())
7005 {
7006 result = MOVE_POS_MATCH_OR_ZV;
7007 break;
7008 }
7009 }
7010 else
7011 {
7012 if (it->line_wrap == WORD_WRAP)
7013 {
7014 if (IT_DISPLAYING_WHITESPACE (it))
7015 may_wrap = 1;
7016 else if (may_wrap)
7017 {
7018 /* We have reached a glyph that follows one or more
7019 whitespace characters. If the position is
7020 already found, we are done. */
7021 if (atpos_it.sp >= 0)
7022 {
7023 *it = atpos_it;
7024 result = MOVE_POS_MATCH_OR_ZV;
7025 goto done;
7026 }
7027 if (atx_it.sp >= 0)
7028 {
7029 *it = atx_it;
7030 result = MOVE_X_REACHED;
7031 goto done;
7032 }
7033 /* Otherwise, we can wrap here. */
7034 wrap_it = *it;
7035 may_wrap = 0;
7036 }
7037 }
7038 }
7039
7040 /* Remember the line height for the current line, in case
7041 the next element doesn't fit on the line. */
7042 ascent = it->max_ascent;
7043 descent = it->max_descent;
7044
7045 /* The call to produce_glyphs will get the metrics of the
7046 display element IT is loaded with. Record the x-position
7047 before this display element, in case it doesn't fit on the
7048 line. */
7049 x = it->current_x;
7050
7051 PRODUCE_GLYPHS (it);
7052
7053 if (it->area != TEXT_AREA)
7054 {
7055 set_iterator_to_next (it, 1);
7056 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7057 SET_TEXT_POS (this_line_min_pos,
7058 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7059 continue;
7060 }
7061
7062 /* The number of glyphs we get back in IT->nglyphs will normally
7063 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7064 character on a terminal frame, or (iii) a line end. For the
7065 second case, IT->nglyphs - 1 padding glyphs will be present.
7066 (On X frames, there is only one glyph produced for a
7067 composite character.)
7068
7069 The behavior implemented below means, for continuation lines,
7070 that as many spaces of a TAB as fit on the current line are
7071 displayed there. For terminal frames, as many glyphs of a
7072 multi-glyph character are displayed in the current line, too.
7073 This is what the old redisplay code did, and we keep it that
7074 way. Under X, the whole shape of a complex character must
7075 fit on the line or it will be completely displayed in the
7076 next line.
7077
7078 Note that both for tabs and padding glyphs, all glyphs have
7079 the same width. */
7080 if (it->nglyphs)
7081 {
7082 /* More than one glyph or glyph doesn't fit on line. All
7083 glyphs have the same width. */
7084 int single_glyph_width = it->pixel_width / it->nglyphs;
7085 int new_x;
7086 int x_before_this_char = x;
7087 int hpos_before_this_char = it->hpos;
7088
7089 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7090 {
7091 new_x = x + single_glyph_width;
7092
7093 /* We want to leave anything reaching TO_X to the caller. */
7094 if ((op & MOVE_TO_X) && new_x > to_x)
7095 {
7096 if (BUFFER_POS_REACHED_P ())
7097 {
7098 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7099 goto buffer_pos_reached;
7100 if (atpos_it.sp < 0)
7101 {
7102 atpos_it = *it;
7103 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7104 }
7105 }
7106 else
7107 {
7108 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7109 {
7110 it->current_x = x;
7111 result = MOVE_X_REACHED;
7112 break;
7113 }
7114 if (atx_it.sp < 0)
7115 {
7116 atx_it = *it;
7117 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7118 }
7119 }
7120 }
7121
7122 if (/* Lines are continued. */
7123 it->line_wrap != TRUNCATE
7124 && (/* And glyph doesn't fit on the line. */
7125 new_x > it->last_visible_x
7126 /* Or it fits exactly and we're on a window
7127 system frame. */
7128 || (new_x == it->last_visible_x
7129 && FRAME_WINDOW_P (it->f))))
7130 {
7131 if (/* IT->hpos == 0 means the very first glyph
7132 doesn't fit on the line, e.g. a wide image. */
7133 it->hpos == 0
7134 || (new_x == it->last_visible_x
7135 && FRAME_WINDOW_P (it->f)))
7136 {
7137 ++it->hpos;
7138 it->current_x = new_x;
7139
7140 /* The character's last glyph just barely fits
7141 in this row. */
7142 if (i == it->nglyphs - 1)
7143 {
7144 /* If this is the destination position,
7145 return a position *before* it in this row,
7146 now that we know it fits in this row. */
7147 if (BUFFER_POS_REACHED_P ())
7148 {
7149 if (it->line_wrap != WORD_WRAP
7150 || wrap_it.sp < 0)
7151 {
7152 it->hpos = hpos_before_this_char;
7153 it->current_x = x_before_this_char;
7154 result = MOVE_POS_MATCH_OR_ZV;
7155 break;
7156 }
7157 if (it->line_wrap == WORD_WRAP
7158 && atpos_it.sp < 0)
7159 {
7160 atpos_it = *it;
7161 atpos_it.current_x = x_before_this_char;
7162 atpos_it.hpos = hpos_before_this_char;
7163 }
7164 }
7165
7166 set_iterator_to_next (it, 1);
7167 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7168 SET_TEXT_POS (this_line_min_pos,
7169 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7170 /* On graphical terminals, newlines may
7171 "overflow" into the fringe if
7172 overflow-newline-into-fringe is non-nil.
7173 On text-only terminals, newlines may
7174 overflow into the last glyph on the
7175 display line.*/
7176 if (!FRAME_WINDOW_P (it->f)
7177 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7178 {
7179 if (!get_next_display_element (it))
7180 {
7181 result = MOVE_POS_MATCH_OR_ZV;
7182 break;
7183 }
7184 if (BUFFER_POS_REACHED_P ())
7185 {
7186 if (ITERATOR_AT_END_OF_LINE_P (it))
7187 result = MOVE_POS_MATCH_OR_ZV;
7188 else
7189 result = MOVE_LINE_CONTINUED;
7190 break;
7191 }
7192 if (ITERATOR_AT_END_OF_LINE_P (it))
7193 {
7194 result = MOVE_NEWLINE_OR_CR;
7195 break;
7196 }
7197 }
7198 }
7199 }
7200 else
7201 IT_RESET_X_ASCENT_DESCENT (it);
7202
7203 if (wrap_it.sp >= 0)
7204 {
7205 *it = wrap_it;
7206 atpos_it.sp = -1;
7207 atx_it.sp = -1;
7208 }
7209
7210 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7211 IT_CHARPOS (*it)));
7212 result = MOVE_LINE_CONTINUED;
7213 break;
7214 }
7215
7216 if (BUFFER_POS_REACHED_P ())
7217 {
7218 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7219 goto buffer_pos_reached;
7220 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7221 {
7222 atpos_it = *it;
7223 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7224 }
7225 }
7226
7227 if (new_x > it->first_visible_x)
7228 {
7229 /* Glyph is visible. Increment number of glyphs that
7230 would be displayed. */
7231 ++it->hpos;
7232 }
7233 }
7234
7235 if (result != MOVE_UNDEFINED)
7236 break;
7237 }
7238 else if (BUFFER_POS_REACHED_P ())
7239 {
7240 buffer_pos_reached:
7241 IT_RESET_X_ASCENT_DESCENT (it);
7242 result = MOVE_POS_MATCH_OR_ZV;
7243 break;
7244 }
7245 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7246 {
7247 /* Stop when TO_X specified and reached. This check is
7248 necessary here because of lines consisting of a line end,
7249 only. The line end will not produce any glyphs and we
7250 would never get MOVE_X_REACHED. */
7251 xassert (it->nglyphs == 0);
7252 result = MOVE_X_REACHED;
7253 break;
7254 }
7255
7256 /* Is this a line end? If yes, we're done. */
7257 if (ITERATOR_AT_END_OF_LINE_P (it))
7258 {
7259 result = MOVE_NEWLINE_OR_CR;
7260 break;
7261 }
7262
7263 if (it->method == GET_FROM_BUFFER)
7264 prev_pos = IT_CHARPOS (*it);
7265 /* The current display element has been consumed. Advance
7266 to the next. */
7267 set_iterator_to_next (it, 1);
7268 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7269 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7270
7271 /* Stop if lines are truncated and IT's current x-position is
7272 past the right edge of the window now. */
7273 if (it->line_wrap == TRUNCATE
7274 && it->current_x >= it->last_visible_x)
7275 {
7276 if (!FRAME_WINDOW_P (it->f)
7277 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7278 {
7279 if (!get_next_display_element (it)
7280 || BUFFER_POS_REACHED_P ())
7281 {
7282 result = MOVE_POS_MATCH_OR_ZV;
7283 break;
7284 }
7285 if (ITERATOR_AT_END_OF_LINE_P (it))
7286 {
7287 result = MOVE_NEWLINE_OR_CR;
7288 break;
7289 }
7290 }
7291 result = MOVE_LINE_TRUNCATED;
7292 break;
7293 }
7294 #undef IT_RESET_X_ASCENT_DESCENT
7295 }
7296
7297 #undef BUFFER_POS_REACHED_P
7298
7299 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7300 restore the saved iterator. */
7301 if (atpos_it.sp >= 0)
7302 *it = atpos_it;
7303 else if (atx_it.sp >= 0)
7304 *it = atx_it;
7305
7306 done:
7307
7308 /* Restore the iterator settings altered at the beginning of this
7309 function. */
7310 it->glyph_row = saved_glyph_row;
7311 return result;
7312 }
7313
7314 /* For external use. */
7315 void
7316 move_it_in_display_line (struct it *it,
7317 EMACS_INT to_charpos, int to_x,
7318 enum move_operation_enum op)
7319 {
7320 if (it->line_wrap == WORD_WRAP
7321 && (op & MOVE_TO_X))
7322 {
7323 struct it save_it = *it;
7324 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7325 /* When word-wrap is on, TO_X may lie past the end
7326 of a wrapped line. Then it->current is the
7327 character on the next line, so backtrack to the
7328 space before the wrap point. */
7329 if (skip == MOVE_LINE_CONTINUED)
7330 {
7331 int prev_x = max (it->current_x - 1, 0);
7332 *it = save_it;
7333 move_it_in_display_line_to
7334 (it, -1, prev_x, MOVE_TO_X);
7335 }
7336 }
7337 else
7338 move_it_in_display_line_to (it, to_charpos, to_x, op);
7339 }
7340
7341
7342 /* Move IT forward until it satisfies one or more of the criteria in
7343 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7344
7345 OP is a bit-mask that specifies where to stop, and in particular,
7346 which of those four position arguments makes a difference. See the
7347 description of enum move_operation_enum.
7348
7349 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7350 screen line, this function will set IT to the next position >
7351 TO_CHARPOS. */
7352
7353 void
7354 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7355 {
7356 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7357 int line_height, line_start_x = 0, reached = 0;
7358
7359 for (;;)
7360 {
7361 if (op & MOVE_TO_VPOS)
7362 {
7363 /* If no TO_CHARPOS and no TO_X specified, stop at the
7364 start of the line TO_VPOS. */
7365 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7366 {
7367 if (it->vpos == to_vpos)
7368 {
7369 reached = 1;
7370 break;
7371 }
7372 else
7373 skip = move_it_in_display_line_to (it, -1, -1, 0);
7374 }
7375 else
7376 {
7377 /* TO_VPOS >= 0 means stop at TO_X in the line at
7378 TO_VPOS, or at TO_POS, whichever comes first. */
7379 if (it->vpos == to_vpos)
7380 {
7381 reached = 2;
7382 break;
7383 }
7384
7385 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7386
7387 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7388 {
7389 reached = 3;
7390 break;
7391 }
7392 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7393 {
7394 /* We have reached TO_X but not in the line we want. */
7395 skip = move_it_in_display_line_to (it, to_charpos,
7396 -1, MOVE_TO_POS);
7397 if (skip == MOVE_POS_MATCH_OR_ZV)
7398 {
7399 reached = 4;
7400 break;
7401 }
7402 }
7403 }
7404 }
7405 else if (op & MOVE_TO_Y)
7406 {
7407 struct it it_backup;
7408
7409 if (it->line_wrap == WORD_WRAP)
7410 it_backup = *it;
7411
7412 /* TO_Y specified means stop at TO_X in the line containing
7413 TO_Y---or at TO_CHARPOS if this is reached first. The
7414 problem is that we can't really tell whether the line
7415 contains TO_Y before we have completely scanned it, and
7416 this may skip past TO_X. What we do is to first scan to
7417 TO_X.
7418
7419 If TO_X is not specified, use a TO_X of zero. The reason
7420 is to make the outcome of this function more predictable.
7421 If we didn't use TO_X == 0, we would stop at the end of
7422 the line which is probably not what a caller would expect
7423 to happen. */
7424 skip = move_it_in_display_line_to
7425 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7426 (MOVE_TO_X | (op & MOVE_TO_POS)));
7427
7428 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7429 if (skip == MOVE_POS_MATCH_OR_ZV)
7430 reached = 5;
7431 else if (skip == MOVE_X_REACHED)
7432 {
7433 /* If TO_X was reached, we want to know whether TO_Y is
7434 in the line. We know this is the case if the already
7435 scanned glyphs make the line tall enough. Otherwise,
7436 we must check by scanning the rest of the line. */
7437 line_height = it->max_ascent + it->max_descent;
7438 if (to_y >= it->current_y
7439 && to_y < it->current_y + line_height)
7440 {
7441 reached = 6;
7442 break;
7443 }
7444 it_backup = *it;
7445 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7446 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7447 op & MOVE_TO_POS);
7448 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7449 line_height = it->max_ascent + it->max_descent;
7450 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7451
7452 if (to_y >= it->current_y
7453 && to_y < it->current_y + line_height)
7454 {
7455 /* If TO_Y is in this line and TO_X was reached
7456 above, we scanned too far. We have to restore
7457 IT's settings to the ones before skipping. */
7458 *it = it_backup;
7459 reached = 6;
7460 }
7461 else
7462 {
7463 skip = skip2;
7464 if (skip == MOVE_POS_MATCH_OR_ZV)
7465 reached = 7;
7466 }
7467 }
7468 else
7469 {
7470 /* Check whether TO_Y is in this line. */
7471 line_height = it->max_ascent + it->max_descent;
7472 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7473
7474 if (to_y >= it->current_y
7475 && to_y < it->current_y + line_height)
7476 {
7477 /* When word-wrap is on, TO_X may lie past the end
7478 of a wrapped line. Then it->current is the
7479 character on the next line, so backtrack to the
7480 space before the wrap point. */
7481 if (skip == MOVE_LINE_CONTINUED
7482 && it->line_wrap == WORD_WRAP)
7483 {
7484 int prev_x = max (it->current_x - 1, 0);
7485 *it = it_backup;
7486 skip = move_it_in_display_line_to
7487 (it, -1, prev_x, MOVE_TO_X);
7488 }
7489 reached = 6;
7490 }
7491 }
7492
7493 if (reached)
7494 break;
7495 }
7496 else if (BUFFERP (it->object)
7497 && (it->method == GET_FROM_BUFFER
7498 || it->method == GET_FROM_STRETCH)
7499 && IT_CHARPOS (*it) >= to_charpos)
7500 skip = MOVE_POS_MATCH_OR_ZV;
7501 else
7502 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7503
7504 switch (skip)
7505 {
7506 case MOVE_POS_MATCH_OR_ZV:
7507 reached = 8;
7508 goto out;
7509
7510 case MOVE_NEWLINE_OR_CR:
7511 set_iterator_to_next (it, 1);
7512 it->continuation_lines_width = 0;
7513 break;
7514
7515 case MOVE_LINE_TRUNCATED:
7516 it->continuation_lines_width = 0;
7517 reseat_at_next_visible_line_start (it, 0);
7518 if ((op & MOVE_TO_POS) != 0
7519 && IT_CHARPOS (*it) > to_charpos)
7520 {
7521 reached = 9;
7522 goto out;
7523 }
7524 break;
7525
7526 case MOVE_LINE_CONTINUED:
7527 /* For continued lines ending in a tab, some of the glyphs
7528 associated with the tab are displayed on the current
7529 line. Since it->current_x does not include these glyphs,
7530 we use it->last_visible_x instead. */
7531 if (it->c == '\t')
7532 {
7533 it->continuation_lines_width += it->last_visible_x;
7534 /* When moving by vpos, ensure that the iterator really
7535 advances to the next line (bug#847, bug#969). Fixme:
7536 do we need to do this in other circumstances? */
7537 if (it->current_x != it->last_visible_x
7538 && (op & MOVE_TO_VPOS)
7539 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7540 {
7541 line_start_x = it->current_x + it->pixel_width
7542 - it->last_visible_x;
7543 set_iterator_to_next (it, 0);
7544 }
7545 }
7546 else
7547 it->continuation_lines_width += it->current_x;
7548 break;
7549
7550 default:
7551 abort ();
7552 }
7553
7554 /* Reset/increment for the next run. */
7555 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7556 it->current_x = line_start_x;
7557 line_start_x = 0;
7558 it->hpos = 0;
7559 it->current_y += it->max_ascent + it->max_descent;
7560 ++it->vpos;
7561 last_height = it->max_ascent + it->max_descent;
7562 last_max_ascent = it->max_ascent;
7563 it->max_ascent = it->max_descent = 0;
7564 }
7565
7566 out:
7567
7568 /* On text terminals, we may stop at the end of a line in the middle
7569 of a multi-character glyph. If the glyph itself is continued,
7570 i.e. it is actually displayed on the next line, don't treat this
7571 stopping point as valid; move to the next line instead (unless
7572 that brings us offscreen). */
7573 if (!FRAME_WINDOW_P (it->f)
7574 && op & MOVE_TO_POS
7575 && IT_CHARPOS (*it) == to_charpos
7576 && it->what == IT_CHARACTER
7577 && it->nglyphs > 1
7578 && it->line_wrap == WINDOW_WRAP
7579 && it->current_x == it->last_visible_x - 1
7580 && it->c != '\n'
7581 && it->c != '\t'
7582 && it->vpos < XFASTINT (it->w->window_end_vpos))
7583 {
7584 it->continuation_lines_width += it->current_x;
7585 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7586 it->current_y += it->max_ascent + it->max_descent;
7587 ++it->vpos;
7588 last_height = it->max_ascent + it->max_descent;
7589 last_max_ascent = it->max_ascent;
7590 }
7591
7592 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7593 }
7594
7595
7596 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7597
7598 If DY > 0, move IT backward at least that many pixels. DY = 0
7599 means move IT backward to the preceding line start or BEGV. This
7600 function may move over more than DY pixels if IT->current_y - DY
7601 ends up in the middle of a line; in this case IT->current_y will be
7602 set to the top of the line moved to. */
7603
7604 void
7605 move_it_vertically_backward (struct it *it, int dy)
7606 {
7607 int nlines, h;
7608 struct it it2, it3;
7609 EMACS_INT start_pos;
7610
7611 move_further_back:
7612 xassert (dy >= 0);
7613
7614 start_pos = IT_CHARPOS (*it);
7615
7616 /* Estimate how many newlines we must move back. */
7617 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7618
7619 /* Set the iterator's position that many lines back. */
7620 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7621 back_to_previous_visible_line_start (it);
7622
7623 /* Reseat the iterator here. When moving backward, we don't want
7624 reseat to skip forward over invisible text, set up the iterator
7625 to deliver from overlay strings at the new position etc. So,
7626 use reseat_1 here. */
7627 reseat_1 (it, it->current.pos, 1);
7628
7629 /* We are now surely at a line start. */
7630 it->current_x = it->hpos = 0;
7631 it->continuation_lines_width = 0;
7632
7633 /* Move forward and see what y-distance we moved. First move to the
7634 start of the next line so that we get its height. We need this
7635 height to be able to tell whether we reached the specified
7636 y-distance. */
7637 it2 = *it;
7638 it2.max_ascent = it2.max_descent = 0;
7639 do
7640 {
7641 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7642 MOVE_TO_POS | MOVE_TO_VPOS);
7643 }
7644 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7645 xassert (IT_CHARPOS (*it) >= BEGV);
7646 it3 = it2;
7647
7648 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7649 xassert (IT_CHARPOS (*it) >= BEGV);
7650 /* H is the actual vertical distance from the position in *IT
7651 and the starting position. */
7652 h = it2.current_y - it->current_y;
7653 /* NLINES is the distance in number of lines. */
7654 nlines = it2.vpos - it->vpos;
7655
7656 /* Correct IT's y and vpos position
7657 so that they are relative to the starting point. */
7658 it->vpos -= nlines;
7659 it->current_y -= h;
7660
7661 if (dy == 0)
7662 {
7663 /* DY == 0 means move to the start of the screen line. The
7664 value of nlines is > 0 if continuation lines were involved. */
7665 if (nlines > 0)
7666 move_it_by_lines (it, nlines);
7667 }
7668 else
7669 {
7670 /* The y-position we try to reach, relative to *IT.
7671 Note that H has been subtracted in front of the if-statement. */
7672 int target_y = it->current_y + h - dy;
7673 int y0 = it3.current_y;
7674 int y1 = line_bottom_y (&it3);
7675 int line_height = y1 - y0;
7676
7677 /* If we did not reach target_y, try to move further backward if
7678 we can. If we moved too far backward, try to move forward. */
7679 if (target_y < it->current_y
7680 /* This is heuristic. In a window that's 3 lines high, with
7681 a line height of 13 pixels each, recentering with point
7682 on the bottom line will try to move -39/2 = 19 pixels
7683 backward. Try to avoid moving into the first line. */
7684 && (it->current_y - target_y
7685 > min (window_box_height (it->w), line_height * 2 / 3))
7686 && IT_CHARPOS (*it) > BEGV)
7687 {
7688 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7689 target_y - it->current_y));
7690 dy = it->current_y - target_y;
7691 goto move_further_back;
7692 }
7693 else if (target_y >= it->current_y + line_height
7694 && IT_CHARPOS (*it) < ZV)
7695 {
7696 /* Should move forward by at least one line, maybe more.
7697
7698 Note: Calling move_it_by_lines can be expensive on
7699 terminal frames, where compute_motion is used (via
7700 vmotion) to do the job, when there are very long lines
7701 and truncate-lines is nil. That's the reason for
7702 treating terminal frames specially here. */
7703
7704 if (!FRAME_WINDOW_P (it->f))
7705 move_it_vertically (it, target_y - (it->current_y + line_height));
7706 else
7707 {
7708 do
7709 {
7710 move_it_by_lines (it, 1);
7711 }
7712 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7713 }
7714 }
7715 }
7716 }
7717
7718
7719 /* Move IT by a specified amount of pixel lines DY. DY negative means
7720 move backwards. DY = 0 means move to start of screen line. At the
7721 end, IT will be on the start of a screen line. */
7722
7723 void
7724 move_it_vertically (struct it *it, int dy)
7725 {
7726 if (dy <= 0)
7727 move_it_vertically_backward (it, -dy);
7728 else
7729 {
7730 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7731 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7732 MOVE_TO_POS | MOVE_TO_Y);
7733 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7734
7735 /* If buffer ends in ZV without a newline, move to the start of
7736 the line to satisfy the post-condition. */
7737 if (IT_CHARPOS (*it) == ZV
7738 && ZV > BEGV
7739 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7740 move_it_by_lines (it, 0);
7741 }
7742 }
7743
7744
7745 /* Move iterator IT past the end of the text line it is in. */
7746
7747 void
7748 move_it_past_eol (struct it *it)
7749 {
7750 enum move_it_result rc;
7751
7752 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7753 if (rc == MOVE_NEWLINE_OR_CR)
7754 set_iterator_to_next (it, 0);
7755 }
7756
7757
7758 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7759 negative means move up. DVPOS == 0 means move to the start of the
7760 screen line.
7761
7762 Optimization idea: If we would know that IT->f doesn't use
7763 a face with proportional font, we could be faster for
7764 truncate-lines nil. */
7765
7766 void
7767 move_it_by_lines (struct it *it, int dvpos)
7768 {
7769
7770 /* The commented-out optimization uses vmotion on terminals. This
7771 gives bad results, because elements like it->what, on which
7772 callers such as pos_visible_p rely, aren't updated. */
7773 /* struct position pos;
7774 if (!FRAME_WINDOW_P (it->f))
7775 {
7776 struct text_pos textpos;
7777
7778 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7779 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7780 reseat (it, textpos, 1);
7781 it->vpos += pos.vpos;
7782 it->current_y += pos.vpos;
7783 }
7784 else */
7785
7786 if (dvpos == 0)
7787 {
7788 /* DVPOS == 0 means move to the start of the screen line. */
7789 move_it_vertically_backward (it, 0);
7790 xassert (it->current_x == 0 && it->hpos == 0);
7791 /* Let next call to line_bottom_y calculate real line height */
7792 last_height = 0;
7793 }
7794 else if (dvpos > 0)
7795 {
7796 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7797 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7798 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7799 }
7800 else
7801 {
7802 struct it it2;
7803 EMACS_INT start_charpos, i;
7804
7805 /* Start at the beginning of the screen line containing IT's
7806 position. This may actually move vertically backwards,
7807 in case of overlays, so adjust dvpos accordingly. */
7808 dvpos += it->vpos;
7809 move_it_vertically_backward (it, 0);
7810 dvpos -= it->vpos;
7811
7812 /* Go back -DVPOS visible lines and reseat the iterator there. */
7813 start_charpos = IT_CHARPOS (*it);
7814 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7815 back_to_previous_visible_line_start (it);
7816 reseat (it, it->current.pos, 1);
7817
7818 /* Move further back if we end up in a string or an image. */
7819 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7820 {
7821 /* First try to move to start of display line. */
7822 dvpos += it->vpos;
7823 move_it_vertically_backward (it, 0);
7824 dvpos -= it->vpos;
7825 if (IT_POS_VALID_AFTER_MOVE_P (it))
7826 break;
7827 /* If start of line is still in string or image,
7828 move further back. */
7829 back_to_previous_visible_line_start (it);
7830 reseat (it, it->current.pos, 1);
7831 dvpos--;
7832 }
7833
7834 it->current_x = it->hpos = 0;
7835
7836 /* Above call may have moved too far if continuation lines
7837 are involved. Scan forward and see if it did. */
7838 it2 = *it;
7839 it2.vpos = it2.current_y = 0;
7840 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7841 it->vpos -= it2.vpos;
7842 it->current_y -= it2.current_y;
7843 it->current_x = it->hpos = 0;
7844
7845 /* If we moved too far back, move IT some lines forward. */
7846 if (it2.vpos > -dvpos)
7847 {
7848 int delta = it2.vpos + dvpos;
7849 it2 = *it;
7850 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7851 /* Move back again if we got too far ahead. */
7852 if (IT_CHARPOS (*it) >= start_charpos)
7853 *it = it2;
7854 }
7855 }
7856 }
7857
7858 /* Return 1 if IT points into the middle of a display vector. */
7859
7860 int
7861 in_display_vector_p (struct it *it)
7862 {
7863 return (it->method == GET_FROM_DISPLAY_VECTOR
7864 && it->current.dpvec_index > 0
7865 && it->dpvec + it->current.dpvec_index != it->dpend);
7866 }
7867
7868 \f
7869 /***********************************************************************
7870 Messages
7871 ***********************************************************************/
7872
7873
7874 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7875 to *Messages*. */
7876
7877 void
7878 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
7879 {
7880 Lisp_Object args[3];
7881 Lisp_Object msg, fmt;
7882 char *buffer;
7883 EMACS_INT len;
7884 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7885 USE_SAFE_ALLOCA;
7886
7887 /* Do nothing if called asynchronously. Inserting text into
7888 a buffer may call after-change-functions and alike and
7889 that would means running Lisp asynchronously. */
7890 if (handling_signal)
7891 return;
7892
7893 fmt = msg = Qnil;
7894 GCPRO4 (fmt, msg, arg1, arg2);
7895
7896 args[0] = fmt = build_string (format);
7897 args[1] = arg1;
7898 args[2] = arg2;
7899 msg = Fformat (3, args);
7900
7901 len = SBYTES (msg) + 1;
7902 SAFE_ALLOCA (buffer, char *, len);
7903 memcpy (buffer, SDATA (msg), len);
7904
7905 message_dolog (buffer, len - 1, 1, 0);
7906 SAFE_FREE ();
7907
7908 UNGCPRO;
7909 }
7910
7911
7912 /* Output a newline in the *Messages* buffer if "needs" one. */
7913
7914 void
7915 message_log_maybe_newline (void)
7916 {
7917 if (message_log_need_newline)
7918 message_dolog ("", 0, 1, 0);
7919 }
7920
7921
7922 /* Add a string M of length NBYTES to the message log, optionally
7923 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7924 nonzero, means interpret the contents of M as multibyte. This
7925 function calls low-level routines in order to bypass text property
7926 hooks, etc. which might not be safe to run.
7927
7928 This may GC (insert may run before/after change hooks),
7929 so the buffer M must NOT point to a Lisp string. */
7930
7931 void
7932 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
7933 {
7934 const unsigned char *msg = (const unsigned char *) m;
7935
7936 if (!NILP (Vmemory_full))
7937 return;
7938
7939 if (!NILP (Vmessage_log_max))
7940 {
7941 struct buffer *oldbuf;
7942 Lisp_Object oldpoint, oldbegv, oldzv;
7943 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7944 EMACS_INT point_at_end = 0;
7945 EMACS_INT zv_at_end = 0;
7946 Lisp_Object old_deactivate_mark, tem;
7947 struct gcpro gcpro1;
7948
7949 old_deactivate_mark = Vdeactivate_mark;
7950 oldbuf = current_buffer;
7951 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7952 BVAR (current_buffer, undo_list) = Qt;
7953
7954 oldpoint = message_dolog_marker1;
7955 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7956 oldbegv = message_dolog_marker2;
7957 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7958 oldzv = message_dolog_marker3;
7959 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7960 GCPRO1 (old_deactivate_mark);
7961
7962 if (PT == Z)
7963 point_at_end = 1;
7964 if (ZV == Z)
7965 zv_at_end = 1;
7966
7967 BEGV = BEG;
7968 BEGV_BYTE = BEG_BYTE;
7969 ZV = Z;
7970 ZV_BYTE = Z_BYTE;
7971 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7972
7973 /* Insert the string--maybe converting multibyte to single byte
7974 or vice versa, so that all the text fits the buffer. */
7975 if (multibyte
7976 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
7977 {
7978 EMACS_INT i;
7979 int c, char_bytes;
7980 char work[1];
7981
7982 /* Convert a multibyte string to single-byte
7983 for the *Message* buffer. */
7984 for (i = 0; i < nbytes; i += char_bytes)
7985 {
7986 c = string_char_and_length (msg + i, &char_bytes);
7987 work[0] = (ASCII_CHAR_P (c)
7988 ? c
7989 : multibyte_char_to_unibyte (c));
7990 insert_1_both (work, 1, 1, 1, 0, 0);
7991 }
7992 }
7993 else if (! multibyte
7994 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
7995 {
7996 EMACS_INT i;
7997 int c, char_bytes;
7998 unsigned char str[MAX_MULTIBYTE_LENGTH];
7999 /* Convert a single-byte string to multibyte
8000 for the *Message* buffer. */
8001 for (i = 0; i < nbytes; i++)
8002 {
8003 c = msg[i];
8004 MAKE_CHAR_MULTIBYTE (c);
8005 char_bytes = CHAR_STRING (c, str);
8006 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8007 }
8008 }
8009 else if (nbytes)
8010 insert_1 (m, nbytes, 1, 0, 0);
8011
8012 if (nlflag)
8013 {
8014 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8015 unsigned long int dups;
8016 insert_1 ("\n", 1, 1, 0, 0);
8017
8018 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8019 this_bol = PT;
8020 this_bol_byte = PT_BYTE;
8021
8022 /* See if this line duplicates the previous one.
8023 If so, combine duplicates. */
8024 if (this_bol > BEG)
8025 {
8026 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8027 prev_bol = PT;
8028 prev_bol_byte = PT_BYTE;
8029
8030 dups = message_log_check_duplicate (prev_bol_byte,
8031 this_bol_byte);
8032 if (dups)
8033 {
8034 del_range_both (prev_bol, prev_bol_byte,
8035 this_bol, this_bol_byte, 0);
8036 if (dups > 1)
8037 {
8038 char dupstr[40];
8039 int duplen;
8040
8041 /* If you change this format, don't forget to also
8042 change message_log_check_duplicate. */
8043 sprintf (dupstr, " [%lu times]", dups);
8044 duplen = strlen (dupstr);
8045 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8046 insert_1 (dupstr, duplen, 1, 0, 1);
8047 }
8048 }
8049 }
8050
8051 /* If we have more than the desired maximum number of lines
8052 in the *Messages* buffer now, delete the oldest ones.
8053 This is safe because we don't have undo in this buffer. */
8054
8055 if (NATNUMP (Vmessage_log_max))
8056 {
8057 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8058 -XFASTINT (Vmessage_log_max) - 1, 0);
8059 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8060 }
8061 }
8062 BEGV = XMARKER (oldbegv)->charpos;
8063 BEGV_BYTE = marker_byte_position (oldbegv);
8064
8065 if (zv_at_end)
8066 {
8067 ZV = Z;
8068 ZV_BYTE = Z_BYTE;
8069 }
8070 else
8071 {
8072 ZV = XMARKER (oldzv)->charpos;
8073 ZV_BYTE = marker_byte_position (oldzv);
8074 }
8075
8076 if (point_at_end)
8077 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8078 else
8079 /* We can't do Fgoto_char (oldpoint) because it will run some
8080 Lisp code. */
8081 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8082 XMARKER (oldpoint)->bytepos);
8083
8084 UNGCPRO;
8085 unchain_marker (XMARKER (oldpoint));
8086 unchain_marker (XMARKER (oldbegv));
8087 unchain_marker (XMARKER (oldzv));
8088
8089 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8090 set_buffer_internal (oldbuf);
8091 if (NILP (tem))
8092 windows_or_buffers_changed = old_windows_or_buffers_changed;
8093 message_log_need_newline = !nlflag;
8094 Vdeactivate_mark = old_deactivate_mark;
8095 }
8096 }
8097
8098
8099 /* We are at the end of the buffer after just having inserted a newline.
8100 (Note: We depend on the fact we won't be crossing the gap.)
8101 Check to see if the most recent message looks a lot like the previous one.
8102 Return 0 if different, 1 if the new one should just replace it, or a
8103 value N > 1 if we should also append " [N times]". */
8104
8105 static unsigned long int
8106 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8107 {
8108 EMACS_INT i;
8109 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8110 int seen_dots = 0;
8111 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8112 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8113
8114 for (i = 0; i < len; i++)
8115 {
8116 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8117 seen_dots = 1;
8118 if (p1[i] != p2[i])
8119 return seen_dots;
8120 }
8121 p1 += len;
8122 if (*p1 == '\n')
8123 return 2;
8124 if (*p1++ == ' ' && *p1++ == '[')
8125 {
8126 char *pend;
8127 unsigned long int n = strtoul ((char *) p1, &pend, 10);
8128 if (strncmp (pend, " times]\n", 8) == 0)
8129 return n+1;
8130 }
8131 return 0;
8132 }
8133 \f
8134
8135 /* Display an echo area message M with a specified length of NBYTES
8136 bytes. The string may include null characters. If M is 0, clear
8137 out any existing message, and let the mini-buffer text show
8138 through.
8139
8140 This may GC, so the buffer M must NOT point to a Lisp string. */
8141
8142 void
8143 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8144 {
8145 /* First flush out any partial line written with print. */
8146 message_log_maybe_newline ();
8147 if (m)
8148 message_dolog (m, nbytes, 1, multibyte);
8149 message2_nolog (m, nbytes, multibyte);
8150 }
8151
8152
8153 /* The non-logging counterpart of message2. */
8154
8155 void
8156 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8157 {
8158 struct frame *sf = SELECTED_FRAME ();
8159 message_enable_multibyte = multibyte;
8160
8161 if (FRAME_INITIAL_P (sf))
8162 {
8163 if (noninteractive_need_newline)
8164 putc ('\n', stderr);
8165 noninteractive_need_newline = 0;
8166 if (m)
8167 fwrite (m, nbytes, 1, stderr);
8168 if (cursor_in_echo_area == 0)
8169 fprintf (stderr, "\n");
8170 fflush (stderr);
8171 }
8172 /* A null message buffer means that the frame hasn't really been
8173 initialized yet. Error messages get reported properly by
8174 cmd_error, so this must be just an informative message; toss it. */
8175 else if (INTERACTIVE
8176 && sf->glyphs_initialized_p
8177 && FRAME_MESSAGE_BUF (sf))
8178 {
8179 Lisp_Object mini_window;
8180 struct frame *f;
8181
8182 /* Get the frame containing the mini-buffer
8183 that the selected frame is using. */
8184 mini_window = FRAME_MINIBUF_WINDOW (sf);
8185 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8186
8187 FRAME_SAMPLE_VISIBILITY (f);
8188 if (FRAME_VISIBLE_P (sf)
8189 && ! FRAME_VISIBLE_P (f))
8190 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8191
8192 if (m)
8193 {
8194 set_message (m, Qnil, nbytes, multibyte);
8195 if (minibuffer_auto_raise)
8196 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8197 }
8198 else
8199 clear_message (1, 1);
8200
8201 do_pending_window_change (0);
8202 echo_area_display (1);
8203 do_pending_window_change (0);
8204 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8205 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8206 }
8207 }
8208
8209
8210 /* Display an echo area message M with a specified length of NBYTES
8211 bytes. The string may include null characters. If M is not a
8212 string, clear out any existing message, and let the mini-buffer
8213 text show through.
8214
8215 This function cancels echoing. */
8216
8217 void
8218 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8219 {
8220 struct gcpro gcpro1;
8221
8222 GCPRO1 (m);
8223 clear_message (1,1);
8224 cancel_echoing ();
8225
8226 /* First flush out any partial line written with print. */
8227 message_log_maybe_newline ();
8228 if (STRINGP (m))
8229 {
8230 char *buffer;
8231 USE_SAFE_ALLOCA;
8232
8233 SAFE_ALLOCA (buffer, char *, nbytes);
8234 memcpy (buffer, SDATA (m), nbytes);
8235 message_dolog (buffer, nbytes, 1, multibyte);
8236 SAFE_FREE ();
8237 }
8238 message3_nolog (m, nbytes, multibyte);
8239
8240 UNGCPRO;
8241 }
8242
8243
8244 /* The non-logging version of message3.
8245 This does not cancel echoing, because it is used for echoing.
8246 Perhaps we need to make a separate function for echoing
8247 and make this cancel echoing. */
8248
8249 void
8250 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8251 {
8252 struct frame *sf = SELECTED_FRAME ();
8253 message_enable_multibyte = multibyte;
8254
8255 if (FRAME_INITIAL_P (sf))
8256 {
8257 if (noninteractive_need_newline)
8258 putc ('\n', stderr);
8259 noninteractive_need_newline = 0;
8260 if (STRINGP (m))
8261 fwrite (SDATA (m), nbytes, 1, stderr);
8262 if (cursor_in_echo_area == 0)
8263 fprintf (stderr, "\n");
8264 fflush (stderr);
8265 }
8266 /* A null message buffer means that the frame hasn't really been
8267 initialized yet. Error messages get reported properly by
8268 cmd_error, so this must be just an informative message; toss it. */
8269 else if (INTERACTIVE
8270 && sf->glyphs_initialized_p
8271 && FRAME_MESSAGE_BUF (sf))
8272 {
8273 Lisp_Object mini_window;
8274 Lisp_Object frame;
8275 struct frame *f;
8276
8277 /* Get the frame containing the mini-buffer
8278 that the selected frame is using. */
8279 mini_window = FRAME_MINIBUF_WINDOW (sf);
8280 frame = XWINDOW (mini_window)->frame;
8281 f = XFRAME (frame);
8282
8283 FRAME_SAMPLE_VISIBILITY (f);
8284 if (FRAME_VISIBLE_P (sf)
8285 && !FRAME_VISIBLE_P (f))
8286 Fmake_frame_visible (frame);
8287
8288 if (STRINGP (m) && SCHARS (m) > 0)
8289 {
8290 set_message (NULL, m, nbytes, multibyte);
8291 if (minibuffer_auto_raise)
8292 Fraise_frame (frame);
8293 /* Assume we are not echoing.
8294 (If we are, echo_now will override this.) */
8295 echo_message_buffer = Qnil;
8296 }
8297 else
8298 clear_message (1, 1);
8299
8300 do_pending_window_change (0);
8301 echo_area_display (1);
8302 do_pending_window_change (0);
8303 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8304 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8305 }
8306 }
8307
8308
8309 /* Display a null-terminated echo area message M. If M is 0, clear
8310 out any existing message, and let the mini-buffer text show through.
8311
8312 The buffer M must continue to exist until after the echo area gets
8313 cleared or some other message gets displayed there. Do not pass
8314 text that is stored in a Lisp string. Do not pass text in a buffer
8315 that was alloca'd. */
8316
8317 void
8318 message1 (const char *m)
8319 {
8320 message2 (m, (m ? strlen (m) : 0), 0);
8321 }
8322
8323
8324 /* The non-logging counterpart of message1. */
8325
8326 void
8327 message1_nolog (const char *m)
8328 {
8329 message2_nolog (m, (m ? strlen (m) : 0), 0);
8330 }
8331
8332 /* Display a message M which contains a single %s
8333 which gets replaced with STRING. */
8334
8335 void
8336 message_with_string (const char *m, Lisp_Object string, int log)
8337 {
8338 CHECK_STRING (string);
8339
8340 if (noninteractive)
8341 {
8342 if (m)
8343 {
8344 if (noninteractive_need_newline)
8345 putc ('\n', stderr);
8346 noninteractive_need_newline = 0;
8347 fprintf (stderr, m, SDATA (string));
8348 if (!cursor_in_echo_area)
8349 fprintf (stderr, "\n");
8350 fflush (stderr);
8351 }
8352 }
8353 else if (INTERACTIVE)
8354 {
8355 /* The frame whose minibuffer we're going to display the message on.
8356 It may be larger than the selected frame, so we need
8357 to use its buffer, not the selected frame's buffer. */
8358 Lisp_Object mini_window;
8359 struct frame *f, *sf = SELECTED_FRAME ();
8360
8361 /* Get the frame containing the minibuffer
8362 that the selected frame is using. */
8363 mini_window = FRAME_MINIBUF_WINDOW (sf);
8364 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8365
8366 /* A null message buffer means that the frame hasn't really been
8367 initialized yet. Error messages get reported properly by
8368 cmd_error, so this must be just an informative message; toss it. */
8369 if (FRAME_MESSAGE_BUF (f))
8370 {
8371 Lisp_Object args[2], msg;
8372 struct gcpro gcpro1, gcpro2;
8373
8374 args[0] = build_string (m);
8375 args[1] = msg = string;
8376 GCPRO2 (args[0], msg);
8377 gcpro1.nvars = 2;
8378
8379 msg = Fformat (2, args);
8380
8381 if (log)
8382 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8383 else
8384 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8385
8386 UNGCPRO;
8387
8388 /* Print should start at the beginning of the message
8389 buffer next time. */
8390 message_buf_print = 0;
8391 }
8392 }
8393 }
8394
8395
8396 /* Dump an informative message to the minibuf. If M is 0, clear out
8397 any existing message, and let the mini-buffer text show through. */
8398
8399 static void
8400 vmessage (const char *m, va_list ap)
8401 {
8402 if (noninteractive)
8403 {
8404 if (m)
8405 {
8406 if (noninteractive_need_newline)
8407 putc ('\n', stderr);
8408 noninteractive_need_newline = 0;
8409 vfprintf (stderr, m, ap);
8410 if (cursor_in_echo_area == 0)
8411 fprintf (stderr, "\n");
8412 fflush (stderr);
8413 }
8414 }
8415 else if (INTERACTIVE)
8416 {
8417 /* The frame whose mini-buffer we're going to display the message
8418 on. It may be larger than the selected frame, so we need to
8419 use its buffer, not the selected frame's buffer. */
8420 Lisp_Object mini_window;
8421 struct frame *f, *sf = SELECTED_FRAME ();
8422
8423 /* Get the frame containing the mini-buffer
8424 that the selected frame is using. */
8425 mini_window = FRAME_MINIBUF_WINDOW (sf);
8426 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8427
8428 /* A null message buffer means that the frame hasn't really been
8429 initialized yet. Error messages get reported properly by
8430 cmd_error, so this must be just an informative message; toss
8431 it. */
8432 if (FRAME_MESSAGE_BUF (f))
8433 {
8434 if (m)
8435 {
8436 size_t len;
8437
8438 len = doprnt (FRAME_MESSAGE_BUF (f),
8439 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8440
8441 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8442 }
8443 else
8444 message1 (0);
8445
8446 /* Print should start at the beginning of the message
8447 buffer next time. */
8448 message_buf_print = 0;
8449 }
8450 }
8451 }
8452
8453 void
8454 message (const char *m, ...)
8455 {
8456 va_list ap;
8457 va_start (ap, m);
8458 vmessage (m, ap);
8459 va_end (ap);
8460 }
8461
8462
8463 #if 0
8464 /* The non-logging version of message. */
8465
8466 void
8467 message_nolog (const char *m, ...)
8468 {
8469 Lisp_Object old_log_max;
8470 va_list ap;
8471 va_start (ap, m);
8472 old_log_max = Vmessage_log_max;
8473 Vmessage_log_max = Qnil;
8474 vmessage (m, ap);
8475 Vmessage_log_max = old_log_max;
8476 va_end (ap);
8477 }
8478 #endif
8479
8480
8481 /* Display the current message in the current mini-buffer. This is
8482 only called from error handlers in process.c, and is not time
8483 critical. */
8484
8485 void
8486 update_echo_area (void)
8487 {
8488 if (!NILP (echo_area_buffer[0]))
8489 {
8490 Lisp_Object string;
8491 string = Fcurrent_message ();
8492 message3 (string, SBYTES (string),
8493 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
8494 }
8495 }
8496
8497
8498 /* Make sure echo area buffers in `echo_buffers' are live.
8499 If they aren't, make new ones. */
8500
8501 static void
8502 ensure_echo_area_buffers (void)
8503 {
8504 int i;
8505
8506 for (i = 0; i < 2; ++i)
8507 if (!BUFFERP (echo_buffer[i])
8508 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
8509 {
8510 char name[30];
8511 Lisp_Object old_buffer;
8512 int j;
8513
8514 old_buffer = echo_buffer[i];
8515 sprintf (name, " *Echo Area %d*", i);
8516 echo_buffer[i] = Fget_buffer_create (build_string (name));
8517 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
8518 /* to force word wrap in echo area -
8519 it was decided to postpone this*/
8520 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8521
8522 for (j = 0; j < 2; ++j)
8523 if (EQ (old_buffer, echo_area_buffer[j]))
8524 echo_area_buffer[j] = echo_buffer[i];
8525 }
8526 }
8527
8528
8529 /* Call FN with args A1..A4 with either the current or last displayed
8530 echo_area_buffer as current buffer.
8531
8532 WHICH zero means use the current message buffer
8533 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8534 from echo_buffer[] and clear it.
8535
8536 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8537 suitable buffer from echo_buffer[] and clear it.
8538
8539 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8540 that the current message becomes the last displayed one, make
8541 choose a suitable buffer for echo_area_buffer[0], and clear it.
8542
8543 Value is what FN returns. */
8544
8545 static int
8546 with_echo_area_buffer (struct window *w, int which,
8547 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8548 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8549 {
8550 Lisp_Object buffer;
8551 int this_one, the_other, clear_buffer_p, rc;
8552 int count = SPECPDL_INDEX ();
8553
8554 /* If buffers aren't live, make new ones. */
8555 ensure_echo_area_buffers ();
8556
8557 clear_buffer_p = 0;
8558
8559 if (which == 0)
8560 this_one = 0, the_other = 1;
8561 else if (which > 0)
8562 this_one = 1, the_other = 0;
8563 else
8564 {
8565 this_one = 0, the_other = 1;
8566 clear_buffer_p = 1;
8567
8568 /* We need a fresh one in case the current echo buffer equals
8569 the one containing the last displayed echo area message. */
8570 if (!NILP (echo_area_buffer[this_one])
8571 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8572 echo_area_buffer[this_one] = Qnil;
8573 }
8574
8575 /* Choose a suitable buffer from echo_buffer[] is we don't
8576 have one. */
8577 if (NILP (echo_area_buffer[this_one]))
8578 {
8579 echo_area_buffer[this_one]
8580 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8581 ? echo_buffer[the_other]
8582 : echo_buffer[this_one]);
8583 clear_buffer_p = 1;
8584 }
8585
8586 buffer = echo_area_buffer[this_one];
8587
8588 /* Don't get confused by reusing the buffer used for echoing
8589 for a different purpose. */
8590 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8591 cancel_echoing ();
8592
8593 record_unwind_protect (unwind_with_echo_area_buffer,
8594 with_echo_area_buffer_unwind_data (w));
8595
8596 /* Make the echo area buffer current. Note that for display
8597 purposes, it is not necessary that the displayed window's buffer
8598 == current_buffer, except for text property lookup. So, let's
8599 only set that buffer temporarily here without doing a full
8600 Fset_window_buffer. We must also change w->pointm, though,
8601 because otherwise an assertions in unshow_buffer fails, and Emacs
8602 aborts. */
8603 set_buffer_internal_1 (XBUFFER (buffer));
8604 if (w)
8605 {
8606 w->buffer = buffer;
8607 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8608 }
8609
8610 BVAR (current_buffer, undo_list) = Qt;
8611 BVAR (current_buffer, read_only) = Qnil;
8612 specbind (Qinhibit_read_only, Qt);
8613 specbind (Qinhibit_modification_hooks, Qt);
8614
8615 if (clear_buffer_p && Z > BEG)
8616 del_range (BEG, Z);
8617
8618 xassert (BEGV >= BEG);
8619 xassert (ZV <= Z && ZV >= BEGV);
8620
8621 rc = fn (a1, a2, a3, a4);
8622
8623 xassert (BEGV >= BEG);
8624 xassert (ZV <= Z && ZV >= BEGV);
8625
8626 unbind_to (count, Qnil);
8627 return rc;
8628 }
8629
8630
8631 /* Save state that should be preserved around the call to the function
8632 FN called in with_echo_area_buffer. */
8633
8634 static Lisp_Object
8635 with_echo_area_buffer_unwind_data (struct window *w)
8636 {
8637 int i = 0;
8638 Lisp_Object vector, tmp;
8639
8640 /* Reduce consing by keeping one vector in
8641 Vwith_echo_area_save_vector. */
8642 vector = Vwith_echo_area_save_vector;
8643 Vwith_echo_area_save_vector = Qnil;
8644
8645 if (NILP (vector))
8646 vector = Fmake_vector (make_number (7), Qnil);
8647
8648 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8649 ASET (vector, i, Vdeactivate_mark); ++i;
8650 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8651
8652 if (w)
8653 {
8654 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8655 ASET (vector, i, w->buffer); ++i;
8656 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8657 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8658 }
8659 else
8660 {
8661 int end = i + 4;
8662 for (; i < end; ++i)
8663 ASET (vector, i, Qnil);
8664 }
8665
8666 xassert (i == ASIZE (vector));
8667 return vector;
8668 }
8669
8670
8671 /* Restore global state from VECTOR which was created by
8672 with_echo_area_buffer_unwind_data. */
8673
8674 static Lisp_Object
8675 unwind_with_echo_area_buffer (Lisp_Object vector)
8676 {
8677 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8678 Vdeactivate_mark = AREF (vector, 1);
8679 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8680
8681 if (WINDOWP (AREF (vector, 3)))
8682 {
8683 struct window *w;
8684 Lisp_Object buffer, charpos, bytepos;
8685
8686 w = XWINDOW (AREF (vector, 3));
8687 buffer = AREF (vector, 4);
8688 charpos = AREF (vector, 5);
8689 bytepos = AREF (vector, 6);
8690
8691 w->buffer = buffer;
8692 set_marker_both (w->pointm, buffer,
8693 XFASTINT (charpos), XFASTINT (bytepos));
8694 }
8695
8696 Vwith_echo_area_save_vector = vector;
8697 return Qnil;
8698 }
8699
8700
8701 /* Set up the echo area for use by print functions. MULTIBYTE_P
8702 non-zero means we will print multibyte. */
8703
8704 void
8705 setup_echo_area_for_printing (int multibyte_p)
8706 {
8707 /* If we can't find an echo area any more, exit. */
8708 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8709 Fkill_emacs (Qnil);
8710
8711 ensure_echo_area_buffers ();
8712
8713 if (!message_buf_print)
8714 {
8715 /* A message has been output since the last time we printed.
8716 Choose a fresh echo area buffer. */
8717 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8718 echo_area_buffer[0] = echo_buffer[1];
8719 else
8720 echo_area_buffer[0] = echo_buffer[0];
8721
8722 /* Switch to that buffer and clear it. */
8723 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8724 BVAR (current_buffer, truncate_lines) = Qnil;
8725
8726 if (Z > BEG)
8727 {
8728 int count = SPECPDL_INDEX ();
8729 specbind (Qinhibit_read_only, Qt);
8730 /* Note that undo recording is always disabled. */
8731 del_range (BEG, Z);
8732 unbind_to (count, Qnil);
8733 }
8734 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8735
8736 /* Set up the buffer for the multibyteness we need. */
8737 if (multibyte_p
8738 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
8739 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8740
8741 /* Raise the frame containing the echo area. */
8742 if (minibuffer_auto_raise)
8743 {
8744 struct frame *sf = SELECTED_FRAME ();
8745 Lisp_Object mini_window;
8746 mini_window = FRAME_MINIBUF_WINDOW (sf);
8747 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8748 }
8749
8750 message_log_maybe_newline ();
8751 message_buf_print = 1;
8752 }
8753 else
8754 {
8755 if (NILP (echo_area_buffer[0]))
8756 {
8757 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8758 echo_area_buffer[0] = echo_buffer[1];
8759 else
8760 echo_area_buffer[0] = echo_buffer[0];
8761 }
8762
8763 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8764 {
8765 /* Someone switched buffers between print requests. */
8766 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8767 BVAR (current_buffer, truncate_lines) = Qnil;
8768 }
8769 }
8770 }
8771
8772
8773 /* Display an echo area message in window W. Value is non-zero if W's
8774 height is changed. If display_last_displayed_message_p is
8775 non-zero, display the message that was last displayed, otherwise
8776 display the current message. */
8777
8778 static int
8779 display_echo_area (struct window *w)
8780 {
8781 int i, no_message_p, window_height_changed_p, count;
8782
8783 /* Temporarily disable garbage collections while displaying the echo
8784 area. This is done because a GC can print a message itself.
8785 That message would modify the echo area buffer's contents while a
8786 redisplay of the buffer is going on, and seriously confuse
8787 redisplay. */
8788 count = inhibit_garbage_collection ();
8789
8790 /* If there is no message, we must call display_echo_area_1
8791 nevertheless because it resizes the window. But we will have to
8792 reset the echo_area_buffer in question to nil at the end because
8793 with_echo_area_buffer will sets it to an empty buffer. */
8794 i = display_last_displayed_message_p ? 1 : 0;
8795 no_message_p = NILP (echo_area_buffer[i]);
8796
8797 window_height_changed_p
8798 = with_echo_area_buffer (w, display_last_displayed_message_p,
8799 display_echo_area_1,
8800 (intptr_t) w, Qnil, 0, 0);
8801
8802 if (no_message_p)
8803 echo_area_buffer[i] = Qnil;
8804
8805 unbind_to (count, Qnil);
8806 return window_height_changed_p;
8807 }
8808
8809
8810 /* Helper for display_echo_area. Display the current buffer which
8811 contains the current echo area message in window W, a mini-window,
8812 a pointer to which is passed in A1. A2..A4 are currently not used.
8813 Change the height of W so that all of the message is displayed.
8814 Value is non-zero if height of W was changed. */
8815
8816 static int
8817 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8818 {
8819 intptr_t i1 = a1;
8820 struct window *w = (struct window *) i1;
8821 Lisp_Object window;
8822 struct text_pos start;
8823 int window_height_changed_p = 0;
8824
8825 /* Do this before displaying, so that we have a large enough glyph
8826 matrix for the display. If we can't get enough space for the
8827 whole text, display the last N lines. That works by setting w->start. */
8828 window_height_changed_p = resize_mini_window (w, 0);
8829
8830 /* Use the starting position chosen by resize_mini_window. */
8831 SET_TEXT_POS_FROM_MARKER (start, w->start);
8832
8833 /* Display. */
8834 clear_glyph_matrix (w->desired_matrix);
8835 XSETWINDOW (window, w);
8836 try_window (window, start, 0);
8837
8838 return window_height_changed_p;
8839 }
8840
8841
8842 /* Resize the echo area window to exactly the size needed for the
8843 currently displayed message, if there is one. If a mini-buffer
8844 is active, don't shrink it. */
8845
8846 void
8847 resize_echo_area_exactly (void)
8848 {
8849 if (BUFFERP (echo_area_buffer[0])
8850 && WINDOWP (echo_area_window))
8851 {
8852 struct window *w = XWINDOW (echo_area_window);
8853 int resized_p;
8854 Lisp_Object resize_exactly;
8855
8856 if (minibuf_level == 0)
8857 resize_exactly = Qt;
8858 else
8859 resize_exactly = Qnil;
8860
8861 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8862 (intptr_t) w, resize_exactly,
8863 0, 0);
8864 if (resized_p)
8865 {
8866 ++windows_or_buffers_changed;
8867 ++update_mode_lines;
8868 redisplay_internal ();
8869 }
8870 }
8871 }
8872
8873
8874 /* Callback function for with_echo_area_buffer, when used from
8875 resize_echo_area_exactly. A1 contains a pointer to the window to
8876 resize, EXACTLY non-nil means resize the mini-window exactly to the
8877 size of the text displayed. A3 and A4 are not used. Value is what
8878 resize_mini_window returns. */
8879
8880 static int
8881 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
8882 {
8883 intptr_t i1 = a1;
8884 return resize_mini_window ((struct window *) i1, !NILP (exactly));
8885 }
8886
8887
8888 /* Resize mini-window W to fit the size of its contents. EXACT_P
8889 means size the window exactly to the size needed. Otherwise, it's
8890 only enlarged until W's buffer is empty.
8891
8892 Set W->start to the right place to begin display. If the whole
8893 contents fit, start at the beginning. Otherwise, start so as
8894 to make the end of the contents appear. This is particularly
8895 important for y-or-n-p, but seems desirable generally.
8896
8897 Value is non-zero if the window height has been changed. */
8898
8899 int
8900 resize_mini_window (struct window *w, int exact_p)
8901 {
8902 struct frame *f = XFRAME (w->frame);
8903 int window_height_changed_p = 0;
8904
8905 xassert (MINI_WINDOW_P (w));
8906
8907 /* By default, start display at the beginning. */
8908 set_marker_both (w->start, w->buffer,
8909 BUF_BEGV (XBUFFER (w->buffer)),
8910 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8911
8912 /* Don't resize windows while redisplaying a window; it would
8913 confuse redisplay functions when the size of the window they are
8914 displaying changes from under them. Such a resizing can happen,
8915 for instance, when which-func prints a long message while
8916 we are running fontification-functions. We're running these
8917 functions with safe_call which binds inhibit-redisplay to t. */
8918 if (!NILP (Vinhibit_redisplay))
8919 return 0;
8920
8921 /* Nil means don't try to resize. */
8922 if (NILP (Vresize_mini_windows)
8923 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8924 return 0;
8925
8926 if (!FRAME_MINIBUF_ONLY_P (f))
8927 {
8928 struct it it;
8929 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8930 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8931 int height, max_height;
8932 int unit = FRAME_LINE_HEIGHT (f);
8933 struct text_pos start;
8934 struct buffer *old_current_buffer = NULL;
8935
8936 if (current_buffer != XBUFFER (w->buffer))
8937 {
8938 old_current_buffer = current_buffer;
8939 set_buffer_internal (XBUFFER (w->buffer));
8940 }
8941
8942 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8943
8944 /* Compute the max. number of lines specified by the user. */
8945 if (FLOATP (Vmax_mini_window_height))
8946 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8947 else if (INTEGERP (Vmax_mini_window_height))
8948 max_height = XINT (Vmax_mini_window_height);
8949 else
8950 max_height = total_height / 4;
8951
8952 /* Correct that max. height if it's bogus. */
8953 max_height = max (1, max_height);
8954 max_height = min (total_height, max_height);
8955
8956 /* Find out the height of the text in the window. */
8957 if (it.line_wrap == TRUNCATE)
8958 height = 1;
8959 else
8960 {
8961 last_height = 0;
8962 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8963 if (it.max_ascent == 0 && it.max_descent == 0)
8964 height = it.current_y + last_height;
8965 else
8966 height = it.current_y + it.max_ascent + it.max_descent;
8967 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8968 height = (height + unit - 1) / unit;
8969 }
8970
8971 /* Compute a suitable window start. */
8972 if (height > max_height)
8973 {
8974 height = max_height;
8975 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8976 move_it_vertically_backward (&it, (height - 1) * unit);
8977 start = it.current.pos;
8978 }
8979 else
8980 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8981 SET_MARKER_FROM_TEXT_POS (w->start, start);
8982
8983 if (EQ (Vresize_mini_windows, Qgrow_only))
8984 {
8985 /* Let it grow only, until we display an empty message, in which
8986 case the window shrinks again. */
8987 if (height > WINDOW_TOTAL_LINES (w))
8988 {
8989 int old_height = WINDOW_TOTAL_LINES (w);
8990 freeze_window_starts (f, 1);
8991 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8992 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8993 }
8994 else if (height < WINDOW_TOTAL_LINES (w)
8995 && (exact_p || BEGV == ZV))
8996 {
8997 int old_height = WINDOW_TOTAL_LINES (w);
8998 freeze_window_starts (f, 0);
8999 shrink_mini_window (w);
9000 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9001 }
9002 }
9003 else
9004 {
9005 /* Always resize to exact size needed. */
9006 if (height > WINDOW_TOTAL_LINES (w))
9007 {
9008 int old_height = WINDOW_TOTAL_LINES (w);
9009 freeze_window_starts (f, 1);
9010 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9011 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9012 }
9013 else if (height < WINDOW_TOTAL_LINES (w))
9014 {
9015 int old_height = WINDOW_TOTAL_LINES (w);
9016 freeze_window_starts (f, 0);
9017 shrink_mini_window (w);
9018
9019 if (height)
9020 {
9021 freeze_window_starts (f, 1);
9022 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9023 }
9024
9025 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9026 }
9027 }
9028
9029 if (old_current_buffer)
9030 set_buffer_internal (old_current_buffer);
9031 }
9032
9033 return window_height_changed_p;
9034 }
9035
9036
9037 /* Value is the current message, a string, or nil if there is no
9038 current message. */
9039
9040 Lisp_Object
9041 current_message (void)
9042 {
9043 Lisp_Object msg;
9044
9045 if (!BUFFERP (echo_area_buffer[0]))
9046 msg = Qnil;
9047 else
9048 {
9049 with_echo_area_buffer (0, 0, current_message_1,
9050 (intptr_t) &msg, Qnil, 0, 0);
9051 if (NILP (msg))
9052 echo_area_buffer[0] = Qnil;
9053 }
9054
9055 return msg;
9056 }
9057
9058
9059 static int
9060 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9061 {
9062 intptr_t i1 = a1;
9063 Lisp_Object *msg = (Lisp_Object *) i1;
9064
9065 if (Z > BEG)
9066 *msg = make_buffer_string (BEG, Z, 1);
9067 else
9068 *msg = Qnil;
9069 return 0;
9070 }
9071
9072
9073 /* Push the current message on Vmessage_stack for later restauration
9074 by restore_message. Value is non-zero if the current message isn't
9075 empty. This is a relatively infrequent operation, so it's not
9076 worth optimizing. */
9077
9078 int
9079 push_message (void)
9080 {
9081 Lisp_Object msg;
9082 msg = current_message ();
9083 Vmessage_stack = Fcons (msg, Vmessage_stack);
9084 return STRINGP (msg);
9085 }
9086
9087
9088 /* Restore message display from the top of Vmessage_stack. */
9089
9090 void
9091 restore_message (void)
9092 {
9093 Lisp_Object msg;
9094
9095 xassert (CONSP (Vmessage_stack));
9096 msg = XCAR (Vmessage_stack);
9097 if (STRINGP (msg))
9098 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9099 else
9100 message3_nolog (msg, 0, 0);
9101 }
9102
9103
9104 /* Handler for record_unwind_protect calling pop_message. */
9105
9106 Lisp_Object
9107 pop_message_unwind (Lisp_Object dummy)
9108 {
9109 pop_message ();
9110 return Qnil;
9111 }
9112
9113 /* Pop the top-most entry off Vmessage_stack. */
9114
9115 static void
9116 pop_message (void)
9117 {
9118 xassert (CONSP (Vmessage_stack));
9119 Vmessage_stack = XCDR (Vmessage_stack);
9120 }
9121
9122
9123 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9124 exits. If the stack is not empty, we have a missing pop_message
9125 somewhere. */
9126
9127 void
9128 check_message_stack (void)
9129 {
9130 if (!NILP (Vmessage_stack))
9131 abort ();
9132 }
9133
9134
9135 /* Truncate to NCHARS what will be displayed in the echo area the next
9136 time we display it---but don't redisplay it now. */
9137
9138 void
9139 truncate_echo_area (EMACS_INT nchars)
9140 {
9141 if (nchars == 0)
9142 echo_area_buffer[0] = Qnil;
9143 /* A null message buffer means that the frame hasn't really been
9144 initialized yet. Error messages get reported properly by
9145 cmd_error, so this must be just an informative message; toss it. */
9146 else if (!noninteractive
9147 && INTERACTIVE
9148 && !NILP (echo_area_buffer[0]))
9149 {
9150 struct frame *sf = SELECTED_FRAME ();
9151 if (FRAME_MESSAGE_BUF (sf))
9152 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9153 }
9154 }
9155
9156
9157 /* Helper function for truncate_echo_area. Truncate the current
9158 message to at most NCHARS characters. */
9159
9160 static int
9161 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9162 {
9163 if (BEG + nchars < Z)
9164 del_range (BEG + nchars, Z);
9165 if (Z == BEG)
9166 echo_area_buffer[0] = Qnil;
9167 return 0;
9168 }
9169
9170
9171 /* Set the current message to a substring of S or STRING.
9172
9173 If STRING is a Lisp string, set the message to the first NBYTES
9174 bytes from STRING. NBYTES zero means use the whole string. If
9175 STRING is multibyte, the message will be displayed multibyte.
9176
9177 If S is not null, set the message to the first LEN bytes of S. LEN
9178 zero means use the whole string. MULTIBYTE_P non-zero means S is
9179 multibyte. Display the message multibyte in that case.
9180
9181 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9182 to t before calling set_message_1 (which calls insert).
9183 */
9184
9185 static void
9186 set_message (const char *s, Lisp_Object string,
9187 EMACS_INT nbytes, int multibyte_p)
9188 {
9189 message_enable_multibyte
9190 = ((s && multibyte_p)
9191 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9192
9193 with_echo_area_buffer (0, -1, set_message_1,
9194 (intptr_t) s, string, nbytes, multibyte_p);
9195 message_buf_print = 0;
9196 help_echo_showing_p = 0;
9197 }
9198
9199
9200 /* Helper function for set_message. Arguments have the same meaning
9201 as there, with A1 corresponding to S and A2 corresponding to STRING
9202 This function is called with the echo area buffer being
9203 current. */
9204
9205 static int
9206 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9207 {
9208 intptr_t i1 = a1;
9209 const char *s = (const char *) i1;
9210 const unsigned char *msg = (const unsigned char *) s;
9211 Lisp_Object string = a2;
9212
9213 /* Change multibyteness of the echo buffer appropriately. */
9214 if (message_enable_multibyte
9215 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9216 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9217
9218 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9219 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9220 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9221
9222 /* Insert new message at BEG. */
9223 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9224
9225 if (STRINGP (string))
9226 {
9227 EMACS_INT nchars;
9228
9229 if (nbytes == 0)
9230 nbytes = SBYTES (string);
9231 nchars = string_byte_to_char (string, nbytes);
9232
9233 /* This function takes care of single/multibyte conversion. We
9234 just have to ensure that the echo area buffer has the right
9235 setting of enable_multibyte_characters. */
9236 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9237 }
9238 else if (s)
9239 {
9240 if (nbytes == 0)
9241 nbytes = strlen (s);
9242
9243 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9244 {
9245 /* Convert from multi-byte to single-byte. */
9246 EMACS_INT i;
9247 int c, n;
9248 char work[1];
9249
9250 /* Convert a multibyte string to single-byte. */
9251 for (i = 0; i < nbytes; i += n)
9252 {
9253 c = string_char_and_length (msg + i, &n);
9254 work[0] = (ASCII_CHAR_P (c)
9255 ? c
9256 : multibyte_char_to_unibyte (c));
9257 insert_1_both (work, 1, 1, 1, 0, 0);
9258 }
9259 }
9260 else if (!multibyte_p
9261 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9262 {
9263 /* Convert from single-byte to multi-byte. */
9264 EMACS_INT i;
9265 int c, n;
9266 unsigned char str[MAX_MULTIBYTE_LENGTH];
9267
9268 /* Convert a single-byte string to multibyte. */
9269 for (i = 0; i < nbytes; i++)
9270 {
9271 c = msg[i];
9272 MAKE_CHAR_MULTIBYTE (c);
9273 n = CHAR_STRING (c, str);
9274 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9275 }
9276 }
9277 else
9278 insert_1 (s, nbytes, 1, 0, 0);
9279 }
9280
9281 return 0;
9282 }
9283
9284
9285 /* Clear messages. CURRENT_P non-zero means clear the current
9286 message. LAST_DISPLAYED_P non-zero means clear the message
9287 last displayed. */
9288
9289 void
9290 clear_message (int current_p, int last_displayed_p)
9291 {
9292 if (current_p)
9293 {
9294 echo_area_buffer[0] = Qnil;
9295 message_cleared_p = 1;
9296 }
9297
9298 if (last_displayed_p)
9299 echo_area_buffer[1] = Qnil;
9300
9301 message_buf_print = 0;
9302 }
9303
9304 /* Clear garbaged frames.
9305
9306 This function is used where the old redisplay called
9307 redraw_garbaged_frames which in turn called redraw_frame which in
9308 turn called clear_frame. The call to clear_frame was a source of
9309 flickering. I believe a clear_frame is not necessary. It should
9310 suffice in the new redisplay to invalidate all current matrices,
9311 and ensure a complete redisplay of all windows. */
9312
9313 static void
9314 clear_garbaged_frames (void)
9315 {
9316 if (frame_garbaged)
9317 {
9318 Lisp_Object tail, frame;
9319 int changed_count = 0;
9320
9321 FOR_EACH_FRAME (tail, frame)
9322 {
9323 struct frame *f = XFRAME (frame);
9324
9325 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9326 {
9327 if (f->resized_p)
9328 {
9329 Fredraw_frame (frame);
9330 f->force_flush_display_p = 1;
9331 }
9332 clear_current_matrices (f);
9333 changed_count++;
9334 f->garbaged = 0;
9335 f->resized_p = 0;
9336 }
9337 }
9338
9339 frame_garbaged = 0;
9340 if (changed_count)
9341 ++windows_or_buffers_changed;
9342 }
9343 }
9344
9345
9346 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9347 is non-zero update selected_frame. Value is non-zero if the
9348 mini-windows height has been changed. */
9349
9350 static int
9351 echo_area_display (int update_frame_p)
9352 {
9353 Lisp_Object mini_window;
9354 struct window *w;
9355 struct frame *f;
9356 int window_height_changed_p = 0;
9357 struct frame *sf = SELECTED_FRAME ();
9358
9359 mini_window = FRAME_MINIBUF_WINDOW (sf);
9360 w = XWINDOW (mini_window);
9361 f = XFRAME (WINDOW_FRAME (w));
9362
9363 /* Don't display if frame is invisible or not yet initialized. */
9364 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9365 return 0;
9366
9367 #ifdef HAVE_WINDOW_SYSTEM
9368 /* When Emacs starts, selected_frame may be the initial terminal
9369 frame. If we let this through, a message would be displayed on
9370 the terminal. */
9371 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9372 return 0;
9373 #endif /* HAVE_WINDOW_SYSTEM */
9374
9375 /* Redraw garbaged frames. */
9376 if (frame_garbaged)
9377 clear_garbaged_frames ();
9378
9379 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9380 {
9381 echo_area_window = mini_window;
9382 window_height_changed_p = display_echo_area (w);
9383 w->must_be_updated_p = 1;
9384
9385 /* Update the display, unless called from redisplay_internal.
9386 Also don't update the screen during redisplay itself. The
9387 update will happen at the end of redisplay, and an update
9388 here could cause confusion. */
9389 if (update_frame_p && !redisplaying_p)
9390 {
9391 int n = 0;
9392
9393 /* If the display update has been interrupted by pending
9394 input, update mode lines in the frame. Due to the
9395 pending input, it might have been that redisplay hasn't
9396 been called, so that mode lines above the echo area are
9397 garbaged. This looks odd, so we prevent it here. */
9398 if (!display_completed)
9399 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9400
9401 if (window_height_changed_p
9402 /* Don't do this if Emacs is shutting down. Redisplay
9403 needs to run hooks. */
9404 && !NILP (Vrun_hooks))
9405 {
9406 /* Must update other windows. Likewise as in other
9407 cases, don't let this update be interrupted by
9408 pending input. */
9409 int count = SPECPDL_INDEX ();
9410 specbind (Qredisplay_dont_pause, Qt);
9411 windows_or_buffers_changed = 1;
9412 redisplay_internal ();
9413 unbind_to (count, Qnil);
9414 }
9415 else if (FRAME_WINDOW_P (f) && n == 0)
9416 {
9417 /* Window configuration is the same as before.
9418 Can do with a display update of the echo area,
9419 unless we displayed some mode lines. */
9420 update_single_window (w, 1);
9421 FRAME_RIF (f)->flush_display (f);
9422 }
9423 else
9424 update_frame (f, 1, 1);
9425
9426 /* If cursor is in the echo area, make sure that the next
9427 redisplay displays the minibuffer, so that the cursor will
9428 be replaced with what the minibuffer wants. */
9429 if (cursor_in_echo_area)
9430 ++windows_or_buffers_changed;
9431 }
9432 }
9433 else if (!EQ (mini_window, selected_window))
9434 windows_or_buffers_changed++;
9435
9436 /* Last displayed message is now the current message. */
9437 echo_area_buffer[1] = echo_area_buffer[0];
9438 /* Inform read_char that we're not echoing. */
9439 echo_message_buffer = Qnil;
9440
9441 /* Prevent redisplay optimization in redisplay_internal by resetting
9442 this_line_start_pos. This is done because the mini-buffer now
9443 displays the message instead of its buffer text. */
9444 if (EQ (mini_window, selected_window))
9445 CHARPOS (this_line_start_pos) = 0;
9446
9447 return window_height_changed_p;
9448 }
9449
9450
9451 \f
9452 /***********************************************************************
9453 Mode Lines and Frame Titles
9454 ***********************************************************************/
9455
9456 /* A buffer for constructing non-propertized mode-line strings and
9457 frame titles in it; allocated from the heap in init_xdisp and
9458 resized as needed in store_mode_line_noprop_char. */
9459
9460 static char *mode_line_noprop_buf;
9461
9462 /* The buffer's end, and a current output position in it. */
9463
9464 static char *mode_line_noprop_buf_end;
9465 static char *mode_line_noprop_ptr;
9466
9467 #define MODE_LINE_NOPROP_LEN(start) \
9468 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9469
9470 static enum {
9471 MODE_LINE_DISPLAY = 0,
9472 MODE_LINE_TITLE,
9473 MODE_LINE_NOPROP,
9474 MODE_LINE_STRING
9475 } mode_line_target;
9476
9477 /* Alist that caches the results of :propertize.
9478 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9479 static Lisp_Object mode_line_proptrans_alist;
9480
9481 /* List of strings making up the mode-line. */
9482 static Lisp_Object mode_line_string_list;
9483
9484 /* Base face property when building propertized mode line string. */
9485 static Lisp_Object mode_line_string_face;
9486 static Lisp_Object mode_line_string_face_prop;
9487
9488
9489 /* Unwind data for mode line strings */
9490
9491 static Lisp_Object Vmode_line_unwind_vector;
9492
9493 static Lisp_Object
9494 format_mode_line_unwind_data (struct buffer *obuf,
9495 Lisp_Object owin,
9496 int save_proptrans)
9497 {
9498 Lisp_Object vector, tmp;
9499
9500 /* Reduce consing by keeping one vector in
9501 Vwith_echo_area_save_vector. */
9502 vector = Vmode_line_unwind_vector;
9503 Vmode_line_unwind_vector = Qnil;
9504
9505 if (NILP (vector))
9506 vector = Fmake_vector (make_number (8), Qnil);
9507
9508 ASET (vector, 0, make_number (mode_line_target));
9509 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9510 ASET (vector, 2, mode_line_string_list);
9511 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9512 ASET (vector, 4, mode_line_string_face);
9513 ASET (vector, 5, mode_line_string_face_prop);
9514
9515 if (obuf)
9516 XSETBUFFER (tmp, obuf);
9517 else
9518 tmp = Qnil;
9519 ASET (vector, 6, tmp);
9520 ASET (vector, 7, owin);
9521
9522 return vector;
9523 }
9524
9525 static Lisp_Object
9526 unwind_format_mode_line (Lisp_Object vector)
9527 {
9528 mode_line_target = XINT (AREF (vector, 0));
9529 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9530 mode_line_string_list = AREF (vector, 2);
9531 if (! EQ (AREF (vector, 3), Qt))
9532 mode_line_proptrans_alist = AREF (vector, 3);
9533 mode_line_string_face = AREF (vector, 4);
9534 mode_line_string_face_prop = AREF (vector, 5);
9535
9536 if (!NILP (AREF (vector, 7)))
9537 /* Select window before buffer, since it may change the buffer. */
9538 Fselect_window (AREF (vector, 7), Qt);
9539
9540 if (!NILP (AREF (vector, 6)))
9541 {
9542 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9543 ASET (vector, 6, Qnil);
9544 }
9545
9546 Vmode_line_unwind_vector = vector;
9547 return Qnil;
9548 }
9549
9550
9551 /* Store a single character C for the frame title in mode_line_noprop_buf.
9552 Re-allocate mode_line_noprop_buf if necessary. */
9553
9554 static void
9555 store_mode_line_noprop_char (char c)
9556 {
9557 /* If output position has reached the end of the allocated buffer,
9558 double the buffer's size. */
9559 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9560 {
9561 int len = MODE_LINE_NOPROP_LEN (0);
9562 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9563 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9564 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9565 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9566 }
9567
9568 *mode_line_noprop_ptr++ = c;
9569 }
9570
9571
9572 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9573 mode_line_noprop_ptr. STRING is the string to store. Do not copy
9574 characters that yield more columns than PRECISION; PRECISION <= 0
9575 means copy the whole string. Pad with spaces until FIELD_WIDTH
9576 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9577 pad. Called from display_mode_element when it is used to build a
9578 frame title. */
9579
9580 static int
9581 store_mode_line_noprop (const char *string, int field_width, int precision)
9582 {
9583 const unsigned char *str = (const unsigned char *) string;
9584 int n = 0;
9585 EMACS_INT dummy, nbytes;
9586
9587 /* Copy at most PRECISION chars from STR. */
9588 nbytes = strlen (string);
9589 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9590 while (nbytes--)
9591 store_mode_line_noprop_char (*str++);
9592
9593 /* Fill up with spaces until FIELD_WIDTH reached. */
9594 while (field_width > 0
9595 && n < field_width)
9596 {
9597 store_mode_line_noprop_char (' ');
9598 ++n;
9599 }
9600
9601 return n;
9602 }
9603
9604 /***********************************************************************
9605 Frame Titles
9606 ***********************************************************************/
9607
9608 #ifdef HAVE_WINDOW_SYSTEM
9609
9610 /* Set the title of FRAME, if it has changed. The title format is
9611 Vicon_title_format if FRAME is iconified, otherwise it is
9612 frame_title_format. */
9613
9614 static void
9615 x_consider_frame_title (Lisp_Object frame)
9616 {
9617 struct frame *f = XFRAME (frame);
9618
9619 if (FRAME_WINDOW_P (f)
9620 || FRAME_MINIBUF_ONLY_P (f)
9621 || f->explicit_name)
9622 {
9623 /* Do we have more than one visible frame on this X display? */
9624 Lisp_Object tail;
9625 Lisp_Object fmt;
9626 int title_start;
9627 char *title;
9628 int len;
9629 struct it it;
9630 int count = SPECPDL_INDEX ();
9631
9632 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9633 {
9634 Lisp_Object other_frame = XCAR (tail);
9635 struct frame *tf = XFRAME (other_frame);
9636
9637 if (tf != f
9638 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9639 && !FRAME_MINIBUF_ONLY_P (tf)
9640 && !EQ (other_frame, tip_frame)
9641 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9642 break;
9643 }
9644
9645 /* Set global variable indicating that multiple frames exist. */
9646 multiple_frames = CONSP (tail);
9647
9648 /* Switch to the buffer of selected window of the frame. Set up
9649 mode_line_target so that display_mode_element will output into
9650 mode_line_noprop_buf; then display the title. */
9651 record_unwind_protect (unwind_format_mode_line,
9652 format_mode_line_unwind_data
9653 (current_buffer, selected_window, 0));
9654
9655 Fselect_window (f->selected_window, Qt);
9656 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9657 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9658
9659 mode_line_target = MODE_LINE_TITLE;
9660 title_start = MODE_LINE_NOPROP_LEN (0);
9661 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9662 NULL, DEFAULT_FACE_ID);
9663 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9664 len = MODE_LINE_NOPROP_LEN (title_start);
9665 title = mode_line_noprop_buf + title_start;
9666 unbind_to (count, Qnil);
9667
9668 /* Set the title only if it's changed. This avoids consing in
9669 the common case where it hasn't. (If it turns out that we've
9670 already wasted too much time by walking through the list with
9671 display_mode_element, then we might need to optimize at a
9672 higher level than this.) */
9673 if (! STRINGP (f->name)
9674 || SBYTES (f->name) != len
9675 || memcmp (title, SDATA (f->name), len) != 0)
9676 x_implicitly_set_name (f, make_string (title, len), Qnil);
9677 }
9678 }
9679
9680 #endif /* not HAVE_WINDOW_SYSTEM */
9681
9682
9683
9684 \f
9685 /***********************************************************************
9686 Menu Bars
9687 ***********************************************************************/
9688
9689
9690 /* Prepare for redisplay by updating menu-bar item lists when
9691 appropriate. This can call eval. */
9692
9693 void
9694 prepare_menu_bars (void)
9695 {
9696 int all_windows;
9697 struct gcpro gcpro1, gcpro2;
9698 struct frame *f;
9699 Lisp_Object tooltip_frame;
9700
9701 #ifdef HAVE_WINDOW_SYSTEM
9702 tooltip_frame = tip_frame;
9703 #else
9704 tooltip_frame = Qnil;
9705 #endif
9706
9707 /* Update all frame titles based on their buffer names, etc. We do
9708 this before the menu bars so that the buffer-menu will show the
9709 up-to-date frame titles. */
9710 #ifdef HAVE_WINDOW_SYSTEM
9711 if (windows_or_buffers_changed || update_mode_lines)
9712 {
9713 Lisp_Object tail, frame;
9714
9715 FOR_EACH_FRAME (tail, frame)
9716 {
9717 f = XFRAME (frame);
9718 if (!EQ (frame, tooltip_frame)
9719 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9720 x_consider_frame_title (frame);
9721 }
9722 }
9723 #endif /* HAVE_WINDOW_SYSTEM */
9724
9725 /* Update the menu bar item lists, if appropriate. This has to be
9726 done before any actual redisplay or generation of display lines. */
9727 all_windows = (update_mode_lines
9728 || buffer_shared > 1
9729 || windows_or_buffers_changed);
9730 if (all_windows)
9731 {
9732 Lisp_Object tail, frame;
9733 int count = SPECPDL_INDEX ();
9734 /* 1 means that update_menu_bar has run its hooks
9735 so any further calls to update_menu_bar shouldn't do so again. */
9736 int menu_bar_hooks_run = 0;
9737
9738 record_unwind_save_match_data ();
9739
9740 FOR_EACH_FRAME (tail, frame)
9741 {
9742 f = XFRAME (frame);
9743
9744 /* Ignore tooltip frame. */
9745 if (EQ (frame, tooltip_frame))
9746 continue;
9747
9748 /* If a window on this frame changed size, report that to
9749 the user and clear the size-change flag. */
9750 if (FRAME_WINDOW_SIZES_CHANGED (f))
9751 {
9752 Lisp_Object functions;
9753
9754 /* Clear flag first in case we get an error below. */
9755 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9756 functions = Vwindow_size_change_functions;
9757 GCPRO2 (tail, functions);
9758
9759 while (CONSP (functions))
9760 {
9761 if (!EQ (XCAR (functions), Qt))
9762 call1 (XCAR (functions), frame);
9763 functions = XCDR (functions);
9764 }
9765 UNGCPRO;
9766 }
9767
9768 GCPRO1 (tail);
9769 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9770 #ifdef HAVE_WINDOW_SYSTEM
9771 update_tool_bar (f, 0);
9772 #endif
9773 #ifdef HAVE_NS
9774 if (windows_or_buffers_changed
9775 && FRAME_NS_P (f))
9776 ns_set_doc_edited (f, Fbuffer_modified_p
9777 (XWINDOW (f->selected_window)->buffer));
9778 #endif
9779 UNGCPRO;
9780 }
9781
9782 unbind_to (count, Qnil);
9783 }
9784 else
9785 {
9786 struct frame *sf = SELECTED_FRAME ();
9787 update_menu_bar (sf, 1, 0);
9788 #ifdef HAVE_WINDOW_SYSTEM
9789 update_tool_bar (sf, 1);
9790 #endif
9791 }
9792 }
9793
9794
9795 /* Update the menu bar item list for frame F. This has to be done
9796 before we start to fill in any display lines, because it can call
9797 eval.
9798
9799 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9800
9801 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9802 already ran the menu bar hooks for this redisplay, so there
9803 is no need to run them again. The return value is the
9804 updated value of this flag, to pass to the next call. */
9805
9806 static int
9807 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9808 {
9809 Lisp_Object window;
9810 register struct window *w;
9811
9812 /* If called recursively during a menu update, do nothing. This can
9813 happen when, for instance, an activate-menubar-hook causes a
9814 redisplay. */
9815 if (inhibit_menubar_update)
9816 return hooks_run;
9817
9818 window = FRAME_SELECTED_WINDOW (f);
9819 w = XWINDOW (window);
9820
9821 if (FRAME_WINDOW_P (f)
9822 ?
9823 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9824 || defined (HAVE_NS) || defined (USE_GTK)
9825 FRAME_EXTERNAL_MENU_BAR (f)
9826 #else
9827 FRAME_MENU_BAR_LINES (f) > 0
9828 #endif
9829 : FRAME_MENU_BAR_LINES (f) > 0)
9830 {
9831 /* If the user has switched buffers or windows, we need to
9832 recompute to reflect the new bindings. But we'll
9833 recompute when update_mode_lines is set too; that means
9834 that people can use force-mode-line-update to request
9835 that the menu bar be recomputed. The adverse effect on
9836 the rest of the redisplay algorithm is about the same as
9837 windows_or_buffers_changed anyway. */
9838 if (windows_or_buffers_changed
9839 /* This used to test w->update_mode_line, but we believe
9840 there is no need to recompute the menu in that case. */
9841 || update_mode_lines
9842 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9843 < BUF_MODIFF (XBUFFER (w->buffer)))
9844 != !NILP (w->last_had_star))
9845 || ((!NILP (Vtransient_mark_mode)
9846 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
9847 != !NILP (w->region_showing)))
9848 {
9849 struct buffer *prev = current_buffer;
9850 int count = SPECPDL_INDEX ();
9851
9852 specbind (Qinhibit_menubar_update, Qt);
9853
9854 set_buffer_internal_1 (XBUFFER (w->buffer));
9855 if (save_match_data)
9856 record_unwind_save_match_data ();
9857 if (NILP (Voverriding_local_map_menu_flag))
9858 {
9859 specbind (Qoverriding_terminal_local_map, Qnil);
9860 specbind (Qoverriding_local_map, Qnil);
9861 }
9862
9863 if (!hooks_run)
9864 {
9865 /* Run the Lucid hook. */
9866 safe_run_hooks (Qactivate_menubar_hook);
9867
9868 /* If it has changed current-menubar from previous value,
9869 really recompute the menu-bar from the value. */
9870 if (! NILP (Vlucid_menu_bar_dirty_flag))
9871 call0 (Qrecompute_lucid_menubar);
9872
9873 safe_run_hooks (Qmenu_bar_update_hook);
9874
9875 hooks_run = 1;
9876 }
9877
9878 XSETFRAME (Vmenu_updating_frame, f);
9879 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9880
9881 /* Redisplay the menu bar in case we changed it. */
9882 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9883 || defined (HAVE_NS) || defined (USE_GTK)
9884 if (FRAME_WINDOW_P (f))
9885 {
9886 #if defined (HAVE_NS)
9887 /* All frames on Mac OS share the same menubar. So only
9888 the selected frame should be allowed to set it. */
9889 if (f == SELECTED_FRAME ())
9890 #endif
9891 set_frame_menubar (f, 0, 0);
9892 }
9893 else
9894 /* On a terminal screen, the menu bar is an ordinary screen
9895 line, and this makes it get updated. */
9896 w->update_mode_line = Qt;
9897 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9898 /* In the non-toolkit version, the menu bar is an ordinary screen
9899 line, and this makes it get updated. */
9900 w->update_mode_line = Qt;
9901 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9902
9903 unbind_to (count, Qnil);
9904 set_buffer_internal_1 (prev);
9905 }
9906 }
9907
9908 return hooks_run;
9909 }
9910
9911
9912 \f
9913 /***********************************************************************
9914 Output Cursor
9915 ***********************************************************************/
9916
9917 #ifdef HAVE_WINDOW_SYSTEM
9918
9919 /* EXPORT:
9920 Nominal cursor position -- where to draw output.
9921 HPOS and VPOS are window relative glyph matrix coordinates.
9922 X and Y are window relative pixel coordinates. */
9923
9924 struct cursor_pos output_cursor;
9925
9926
9927 /* EXPORT:
9928 Set the global variable output_cursor to CURSOR. All cursor
9929 positions are relative to updated_window. */
9930
9931 void
9932 set_output_cursor (struct cursor_pos *cursor)
9933 {
9934 output_cursor.hpos = cursor->hpos;
9935 output_cursor.vpos = cursor->vpos;
9936 output_cursor.x = cursor->x;
9937 output_cursor.y = cursor->y;
9938 }
9939
9940
9941 /* EXPORT for RIF:
9942 Set a nominal cursor position.
9943
9944 HPOS and VPOS are column/row positions in a window glyph matrix. X
9945 and Y are window text area relative pixel positions.
9946
9947 If this is done during an update, updated_window will contain the
9948 window that is being updated and the position is the future output
9949 cursor position for that window. If updated_window is null, use
9950 selected_window and display the cursor at the given position. */
9951
9952 void
9953 x_cursor_to (int vpos, int hpos, int y, int x)
9954 {
9955 struct window *w;
9956
9957 /* If updated_window is not set, work on selected_window. */
9958 if (updated_window)
9959 w = updated_window;
9960 else
9961 w = XWINDOW (selected_window);
9962
9963 /* Set the output cursor. */
9964 output_cursor.hpos = hpos;
9965 output_cursor.vpos = vpos;
9966 output_cursor.x = x;
9967 output_cursor.y = y;
9968
9969 /* If not called as part of an update, really display the cursor.
9970 This will also set the cursor position of W. */
9971 if (updated_window == NULL)
9972 {
9973 BLOCK_INPUT;
9974 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9975 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9976 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9977 UNBLOCK_INPUT;
9978 }
9979 }
9980
9981 #endif /* HAVE_WINDOW_SYSTEM */
9982
9983 \f
9984 /***********************************************************************
9985 Tool-bars
9986 ***********************************************************************/
9987
9988 #ifdef HAVE_WINDOW_SYSTEM
9989
9990 /* Where the mouse was last time we reported a mouse event. */
9991
9992 FRAME_PTR last_mouse_frame;
9993
9994 /* Tool-bar item index of the item on which a mouse button was pressed
9995 or -1. */
9996
9997 int last_tool_bar_item;
9998
9999
10000 static Lisp_Object
10001 update_tool_bar_unwind (Lisp_Object frame)
10002 {
10003 selected_frame = frame;
10004 return Qnil;
10005 }
10006
10007 /* Update the tool-bar item list for frame F. This has to be done
10008 before we start to fill in any display lines. Called from
10009 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10010 and restore it here. */
10011
10012 static void
10013 update_tool_bar (struct frame *f, int save_match_data)
10014 {
10015 #if defined (USE_GTK) || defined (HAVE_NS)
10016 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10017 #else
10018 int do_update = WINDOWP (f->tool_bar_window)
10019 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10020 #endif
10021
10022 if (do_update)
10023 {
10024 Lisp_Object window;
10025 struct window *w;
10026
10027 window = FRAME_SELECTED_WINDOW (f);
10028 w = XWINDOW (window);
10029
10030 /* If the user has switched buffers or windows, we need to
10031 recompute to reflect the new bindings. But we'll
10032 recompute when update_mode_lines is set too; that means
10033 that people can use force-mode-line-update to request
10034 that the menu bar be recomputed. The adverse effect on
10035 the rest of the redisplay algorithm is about the same as
10036 windows_or_buffers_changed anyway. */
10037 if (windows_or_buffers_changed
10038 || !NILP (w->update_mode_line)
10039 || update_mode_lines
10040 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10041 < BUF_MODIFF (XBUFFER (w->buffer)))
10042 != !NILP (w->last_had_star))
10043 || ((!NILP (Vtransient_mark_mode)
10044 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10045 != !NILP (w->region_showing)))
10046 {
10047 struct buffer *prev = current_buffer;
10048 int count = SPECPDL_INDEX ();
10049 Lisp_Object frame, new_tool_bar;
10050 int new_n_tool_bar;
10051 struct gcpro gcpro1;
10052
10053 /* Set current_buffer to the buffer of the selected
10054 window of the frame, so that we get the right local
10055 keymaps. */
10056 set_buffer_internal_1 (XBUFFER (w->buffer));
10057
10058 /* Save match data, if we must. */
10059 if (save_match_data)
10060 record_unwind_save_match_data ();
10061
10062 /* Make sure that we don't accidentally use bogus keymaps. */
10063 if (NILP (Voverriding_local_map_menu_flag))
10064 {
10065 specbind (Qoverriding_terminal_local_map, Qnil);
10066 specbind (Qoverriding_local_map, Qnil);
10067 }
10068
10069 GCPRO1 (new_tool_bar);
10070
10071 /* We must temporarily set the selected frame to this frame
10072 before calling tool_bar_items, because the calculation of
10073 the tool-bar keymap uses the selected frame (see
10074 `tool-bar-make-keymap' in tool-bar.el). */
10075 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10076 XSETFRAME (frame, f);
10077 selected_frame = frame;
10078
10079 /* Build desired tool-bar items from keymaps. */
10080 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10081 &new_n_tool_bar);
10082
10083 /* Redisplay the tool-bar if we changed it. */
10084 if (new_n_tool_bar != f->n_tool_bar_items
10085 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10086 {
10087 /* Redisplay that happens asynchronously due to an expose event
10088 may access f->tool_bar_items. Make sure we update both
10089 variables within BLOCK_INPUT so no such event interrupts. */
10090 BLOCK_INPUT;
10091 f->tool_bar_items = new_tool_bar;
10092 f->n_tool_bar_items = new_n_tool_bar;
10093 w->update_mode_line = Qt;
10094 UNBLOCK_INPUT;
10095 }
10096
10097 UNGCPRO;
10098
10099 unbind_to (count, Qnil);
10100 set_buffer_internal_1 (prev);
10101 }
10102 }
10103 }
10104
10105
10106 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10107 F's desired tool-bar contents. F->tool_bar_items must have
10108 been set up previously by calling prepare_menu_bars. */
10109
10110 static void
10111 build_desired_tool_bar_string (struct frame *f)
10112 {
10113 int i, size, size_needed;
10114 struct gcpro gcpro1, gcpro2, gcpro3;
10115 Lisp_Object image, plist, props;
10116
10117 image = plist = props = Qnil;
10118 GCPRO3 (image, plist, props);
10119
10120 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10121 Otherwise, make a new string. */
10122
10123 /* The size of the string we might be able to reuse. */
10124 size = (STRINGP (f->desired_tool_bar_string)
10125 ? SCHARS (f->desired_tool_bar_string)
10126 : 0);
10127
10128 /* We need one space in the string for each image. */
10129 size_needed = f->n_tool_bar_items;
10130
10131 /* Reuse f->desired_tool_bar_string, if possible. */
10132 if (size < size_needed || NILP (f->desired_tool_bar_string))
10133 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10134 make_number (' '));
10135 else
10136 {
10137 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10138 Fremove_text_properties (make_number (0), make_number (size),
10139 props, f->desired_tool_bar_string);
10140 }
10141
10142 /* Put a `display' property on the string for the images to display,
10143 put a `menu_item' property on tool-bar items with a value that
10144 is the index of the item in F's tool-bar item vector. */
10145 for (i = 0; i < f->n_tool_bar_items; ++i)
10146 {
10147 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10148
10149 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10150 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10151 int hmargin, vmargin, relief, idx, end;
10152
10153 /* If image is a vector, choose the image according to the
10154 button state. */
10155 image = PROP (TOOL_BAR_ITEM_IMAGES);
10156 if (VECTORP (image))
10157 {
10158 if (enabled_p)
10159 idx = (selected_p
10160 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10161 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10162 else
10163 idx = (selected_p
10164 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10165 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10166
10167 xassert (ASIZE (image) >= idx);
10168 image = AREF (image, idx);
10169 }
10170 else
10171 idx = -1;
10172
10173 /* Ignore invalid image specifications. */
10174 if (!valid_image_p (image))
10175 continue;
10176
10177 /* Display the tool-bar button pressed, or depressed. */
10178 plist = Fcopy_sequence (XCDR (image));
10179
10180 /* Compute margin and relief to draw. */
10181 relief = (tool_bar_button_relief >= 0
10182 ? tool_bar_button_relief
10183 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10184 hmargin = vmargin = relief;
10185
10186 if (INTEGERP (Vtool_bar_button_margin)
10187 && XINT (Vtool_bar_button_margin) > 0)
10188 {
10189 hmargin += XFASTINT (Vtool_bar_button_margin);
10190 vmargin += XFASTINT (Vtool_bar_button_margin);
10191 }
10192 else if (CONSP (Vtool_bar_button_margin))
10193 {
10194 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10195 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10196 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10197
10198 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10199 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10200 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10201 }
10202
10203 if (auto_raise_tool_bar_buttons_p)
10204 {
10205 /* Add a `:relief' property to the image spec if the item is
10206 selected. */
10207 if (selected_p)
10208 {
10209 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10210 hmargin -= relief;
10211 vmargin -= relief;
10212 }
10213 }
10214 else
10215 {
10216 /* If image is selected, display it pressed, i.e. with a
10217 negative relief. If it's not selected, display it with a
10218 raised relief. */
10219 plist = Fplist_put (plist, QCrelief,
10220 (selected_p
10221 ? make_number (-relief)
10222 : make_number (relief)));
10223 hmargin -= relief;
10224 vmargin -= relief;
10225 }
10226
10227 /* Put a margin around the image. */
10228 if (hmargin || vmargin)
10229 {
10230 if (hmargin == vmargin)
10231 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10232 else
10233 plist = Fplist_put (plist, QCmargin,
10234 Fcons (make_number (hmargin),
10235 make_number (vmargin)));
10236 }
10237
10238 /* If button is not enabled, and we don't have special images
10239 for the disabled state, make the image appear disabled by
10240 applying an appropriate algorithm to it. */
10241 if (!enabled_p && idx < 0)
10242 plist = Fplist_put (plist, QCconversion, Qdisabled);
10243
10244 /* Put a `display' text property on the string for the image to
10245 display. Put a `menu-item' property on the string that gives
10246 the start of this item's properties in the tool-bar items
10247 vector. */
10248 image = Fcons (Qimage, plist);
10249 props = list4 (Qdisplay, image,
10250 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10251
10252 /* Let the last image hide all remaining spaces in the tool bar
10253 string. The string can be longer than needed when we reuse a
10254 previous string. */
10255 if (i + 1 == f->n_tool_bar_items)
10256 end = SCHARS (f->desired_tool_bar_string);
10257 else
10258 end = i + 1;
10259 Fadd_text_properties (make_number (i), make_number (end),
10260 props, f->desired_tool_bar_string);
10261 #undef PROP
10262 }
10263
10264 UNGCPRO;
10265 }
10266
10267
10268 /* Display one line of the tool-bar of frame IT->f.
10269
10270 HEIGHT specifies the desired height of the tool-bar line.
10271 If the actual height of the glyph row is less than HEIGHT, the
10272 row's height is increased to HEIGHT, and the icons are centered
10273 vertically in the new height.
10274
10275 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10276 count a final empty row in case the tool-bar width exactly matches
10277 the window width.
10278 */
10279
10280 static void
10281 display_tool_bar_line (struct it *it, int height)
10282 {
10283 struct glyph_row *row = it->glyph_row;
10284 int max_x = it->last_visible_x;
10285 struct glyph *last;
10286
10287 prepare_desired_row (row);
10288 row->y = it->current_y;
10289
10290 /* Note that this isn't made use of if the face hasn't a box,
10291 so there's no need to check the face here. */
10292 it->start_of_box_run_p = 1;
10293
10294 while (it->current_x < max_x)
10295 {
10296 int x, n_glyphs_before, i, nglyphs;
10297 struct it it_before;
10298
10299 /* Get the next display element. */
10300 if (!get_next_display_element (it))
10301 {
10302 /* Don't count empty row if we are counting needed tool-bar lines. */
10303 if (height < 0 && !it->hpos)
10304 return;
10305 break;
10306 }
10307
10308 /* Produce glyphs. */
10309 n_glyphs_before = row->used[TEXT_AREA];
10310 it_before = *it;
10311
10312 PRODUCE_GLYPHS (it);
10313
10314 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10315 i = 0;
10316 x = it_before.current_x;
10317 while (i < nglyphs)
10318 {
10319 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10320
10321 if (x + glyph->pixel_width > max_x)
10322 {
10323 /* Glyph doesn't fit on line. Backtrack. */
10324 row->used[TEXT_AREA] = n_glyphs_before;
10325 *it = it_before;
10326 /* If this is the only glyph on this line, it will never fit on the
10327 tool-bar, so skip it. But ensure there is at least one glyph,
10328 so we don't accidentally disable the tool-bar. */
10329 if (n_glyphs_before == 0
10330 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10331 break;
10332 goto out;
10333 }
10334
10335 ++it->hpos;
10336 x += glyph->pixel_width;
10337 ++i;
10338 }
10339
10340 /* Stop at line ends. */
10341 if (ITERATOR_AT_END_OF_LINE_P (it))
10342 break;
10343
10344 set_iterator_to_next (it, 1);
10345 }
10346
10347 out:;
10348
10349 row->displays_text_p = row->used[TEXT_AREA] != 0;
10350
10351 /* Use default face for the border below the tool bar.
10352
10353 FIXME: When auto-resize-tool-bars is grow-only, there is
10354 no additional border below the possibly empty tool-bar lines.
10355 So to make the extra empty lines look "normal", we have to
10356 use the tool-bar face for the border too. */
10357 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10358 it->face_id = DEFAULT_FACE_ID;
10359
10360 extend_face_to_end_of_line (it);
10361 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10362 last->right_box_line_p = 1;
10363 if (last == row->glyphs[TEXT_AREA])
10364 last->left_box_line_p = 1;
10365
10366 /* Make line the desired height and center it vertically. */
10367 if ((height -= it->max_ascent + it->max_descent) > 0)
10368 {
10369 /* Don't add more than one line height. */
10370 height %= FRAME_LINE_HEIGHT (it->f);
10371 it->max_ascent += height / 2;
10372 it->max_descent += (height + 1) / 2;
10373 }
10374
10375 compute_line_metrics (it);
10376
10377 /* If line is empty, make it occupy the rest of the tool-bar. */
10378 if (!row->displays_text_p)
10379 {
10380 row->height = row->phys_height = it->last_visible_y - row->y;
10381 row->visible_height = row->height;
10382 row->ascent = row->phys_ascent = 0;
10383 row->extra_line_spacing = 0;
10384 }
10385
10386 row->full_width_p = 1;
10387 row->continued_p = 0;
10388 row->truncated_on_left_p = 0;
10389 row->truncated_on_right_p = 0;
10390
10391 it->current_x = it->hpos = 0;
10392 it->current_y += row->height;
10393 ++it->vpos;
10394 ++it->glyph_row;
10395 }
10396
10397
10398 /* Max tool-bar height. */
10399
10400 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10401 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10402
10403 /* Value is the number of screen lines needed to make all tool-bar
10404 items of frame F visible. The number of actual rows needed is
10405 returned in *N_ROWS if non-NULL. */
10406
10407 static int
10408 tool_bar_lines_needed (struct frame *f, int *n_rows)
10409 {
10410 struct window *w = XWINDOW (f->tool_bar_window);
10411 struct it it;
10412 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10413 the desired matrix, so use (unused) mode-line row as temporary row to
10414 avoid destroying the first tool-bar row. */
10415 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10416
10417 /* Initialize an iterator for iteration over
10418 F->desired_tool_bar_string in the tool-bar window of frame F. */
10419 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10420 it.first_visible_x = 0;
10421 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10422 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10423
10424 while (!ITERATOR_AT_END_P (&it))
10425 {
10426 clear_glyph_row (temp_row);
10427 it.glyph_row = temp_row;
10428 display_tool_bar_line (&it, -1);
10429 }
10430 clear_glyph_row (temp_row);
10431
10432 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10433 if (n_rows)
10434 *n_rows = it.vpos > 0 ? it.vpos : -1;
10435
10436 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10437 }
10438
10439
10440 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10441 0, 1, 0,
10442 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10443 (Lisp_Object frame)
10444 {
10445 struct frame *f;
10446 struct window *w;
10447 int nlines = 0;
10448
10449 if (NILP (frame))
10450 frame = selected_frame;
10451 else
10452 CHECK_FRAME (frame);
10453 f = XFRAME (frame);
10454
10455 if (WINDOWP (f->tool_bar_window)
10456 || (w = XWINDOW (f->tool_bar_window),
10457 WINDOW_TOTAL_LINES (w) > 0))
10458 {
10459 update_tool_bar (f, 1);
10460 if (f->n_tool_bar_items)
10461 {
10462 build_desired_tool_bar_string (f);
10463 nlines = tool_bar_lines_needed (f, NULL);
10464 }
10465 }
10466
10467 return make_number (nlines);
10468 }
10469
10470
10471 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10472 height should be changed. */
10473
10474 static int
10475 redisplay_tool_bar (struct frame *f)
10476 {
10477 struct window *w;
10478 struct it it;
10479 struct glyph_row *row;
10480
10481 #if defined (USE_GTK) || defined (HAVE_NS)
10482 if (FRAME_EXTERNAL_TOOL_BAR (f))
10483 update_frame_tool_bar (f);
10484 return 0;
10485 #endif
10486
10487 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10488 do anything. This means you must start with tool-bar-lines
10489 non-zero to get the auto-sizing effect. Or in other words, you
10490 can turn off tool-bars by specifying tool-bar-lines zero. */
10491 if (!WINDOWP (f->tool_bar_window)
10492 || (w = XWINDOW (f->tool_bar_window),
10493 WINDOW_TOTAL_LINES (w) == 0))
10494 return 0;
10495
10496 /* Set up an iterator for the tool-bar window. */
10497 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10498 it.first_visible_x = 0;
10499 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10500 row = it.glyph_row;
10501
10502 /* Build a string that represents the contents of the tool-bar. */
10503 build_desired_tool_bar_string (f);
10504 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10505
10506 if (f->n_tool_bar_rows == 0)
10507 {
10508 int nlines;
10509
10510 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10511 nlines != WINDOW_TOTAL_LINES (w)))
10512 {
10513 Lisp_Object frame;
10514 int old_height = WINDOW_TOTAL_LINES (w);
10515
10516 XSETFRAME (frame, f);
10517 Fmodify_frame_parameters (frame,
10518 Fcons (Fcons (Qtool_bar_lines,
10519 make_number (nlines)),
10520 Qnil));
10521 if (WINDOW_TOTAL_LINES (w) != old_height)
10522 {
10523 clear_glyph_matrix (w->desired_matrix);
10524 fonts_changed_p = 1;
10525 return 1;
10526 }
10527 }
10528 }
10529
10530 /* Display as many lines as needed to display all tool-bar items. */
10531
10532 if (f->n_tool_bar_rows > 0)
10533 {
10534 int border, rows, height, extra;
10535
10536 if (INTEGERP (Vtool_bar_border))
10537 border = XINT (Vtool_bar_border);
10538 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10539 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10540 else if (EQ (Vtool_bar_border, Qborder_width))
10541 border = f->border_width;
10542 else
10543 border = 0;
10544 if (border < 0)
10545 border = 0;
10546
10547 rows = f->n_tool_bar_rows;
10548 height = max (1, (it.last_visible_y - border) / rows);
10549 extra = it.last_visible_y - border - height * rows;
10550
10551 while (it.current_y < it.last_visible_y)
10552 {
10553 int h = 0;
10554 if (extra > 0 && rows-- > 0)
10555 {
10556 h = (extra + rows - 1) / rows;
10557 extra -= h;
10558 }
10559 display_tool_bar_line (&it, height + h);
10560 }
10561 }
10562 else
10563 {
10564 while (it.current_y < it.last_visible_y)
10565 display_tool_bar_line (&it, 0);
10566 }
10567
10568 /* It doesn't make much sense to try scrolling in the tool-bar
10569 window, so don't do it. */
10570 w->desired_matrix->no_scrolling_p = 1;
10571 w->must_be_updated_p = 1;
10572
10573 if (!NILP (Vauto_resize_tool_bars))
10574 {
10575 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10576 int change_height_p = 0;
10577
10578 /* If we couldn't display everything, change the tool-bar's
10579 height if there is room for more. */
10580 if (IT_STRING_CHARPOS (it) < it.end_charpos
10581 && it.current_y < max_tool_bar_height)
10582 change_height_p = 1;
10583
10584 row = it.glyph_row - 1;
10585
10586 /* If there are blank lines at the end, except for a partially
10587 visible blank line at the end that is smaller than
10588 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10589 if (!row->displays_text_p
10590 && row->height >= FRAME_LINE_HEIGHT (f))
10591 change_height_p = 1;
10592
10593 /* If row displays tool-bar items, but is partially visible,
10594 change the tool-bar's height. */
10595 if (row->displays_text_p
10596 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10597 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10598 change_height_p = 1;
10599
10600 /* Resize windows as needed by changing the `tool-bar-lines'
10601 frame parameter. */
10602 if (change_height_p)
10603 {
10604 Lisp_Object frame;
10605 int old_height = WINDOW_TOTAL_LINES (w);
10606 int nrows;
10607 int nlines = tool_bar_lines_needed (f, &nrows);
10608
10609 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10610 && !f->minimize_tool_bar_window_p)
10611 ? (nlines > old_height)
10612 : (nlines != old_height));
10613 f->minimize_tool_bar_window_p = 0;
10614
10615 if (change_height_p)
10616 {
10617 XSETFRAME (frame, f);
10618 Fmodify_frame_parameters (frame,
10619 Fcons (Fcons (Qtool_bar_lines,
10620 make_number (nlines)),
10621 Qnil));
10622 if (WINDOW_TOTAL_LINES (w) != old_height)
10623 {
10624 clear_glyph_matrix (w->desired_matrix);
10625 f->n_tool_bar_rows = nrows;
10626 fonts_changed_p = 1;
10627 return 1;
10628 }
10629 }
10630 }
10631 }
10632
10633 f->minimize_tool_bar_window_p = 0;
10634 return 0;
10635 }
10636
10637
10638 /* Get information about the tool-bar item which is displayed in GLYPH
10639 on frame F. Return in *PROP_IDX the index where tool-bar item
10640 properties start in F->tool_bar_items. Value is zero if
10641 GLYPH doesn't display a tool-bar item. */
10642
10643 static int
10644 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10645 {
10646 Lisp_Object prop;
10647 int success_p;
10648 int charpos;
10649
10650 /* This function can be called asynchronously, which means we must
10651 exclude any possibility that Fget_text_property signals an
10652 error. */
10653 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10654 charpos = max (0, charpos);
10655
10656 /* Get the text property `menu-item' at pos. The value of that
10657 property is the start index of this item's properties in
10658 F->tool_bar_items. */
10659 prop = Fget_text_property (make_number (charpos),
10660 Qmenu_item, f->current_tool_bar_string);
10661 if (INTEGERP (prop))
10662 {
10663 *prop_idx = XINT (prop);
10664 success_p = 1;
10665 }
10666 else
10667 success_p = 0;
10668
10669 return success_p;
10670 }
10671
10672 \f
10673 /* Get information about the tool-bar item at position X/Y on frame F.
10674 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10675 the current matrix of the tool-bar window of F, or NULL if not
10676 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10677 item in F->tool_bar_items. Value is
10678
10679 -1 if X/Y is not on a tool-bar item
10680 0 if X/Y is on the same item that was highlighted before.
10681 1 otherwise. */
10682
10683 static int
10684 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10685 int *hpos, int *vpos, int *prop_idx)
10686 {
10687 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10688 struct window *w = XWINDOW (f->tool_bar_window);
10689 int area;
10690
10691 /* Find the glyph under X/Y. */
10692 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10693 if (*glyph == NULL)
10694 return -1;
10695
10696 /* Get the start of this tool-bar item's properties in
10697 f->tool_bar_items. */
10698 if (!tool_bar_item_info (f, *glyph, prop_idx))
10699 return -1;
10700
10701 /* Is mouse on the highlighted item? */
10702 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10703 && *vpos >= hlinfo->mouse_face_beg_row
10704 && *vpos <= hlinfo->mouse_face_end_row
10705 && (*vpos > hlinfo->mouse_face_beg_row
10706 || *hpos >= hlinfo->mouse_face_beg_col)
10707 && (*vpos < hlinfo->mouse_face_end_row
10708 || *hpos < hlinfo->mouse_face_end_col
10709 || hlinfo->mouse_face_past_end))
10710 return 0;
10711
10712 return 1;
10713 }
10714
10715
10716 /* EXPORT:
10717 Handle mouse button event on the tool-bar of frame F, at
10718 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10719 0 for button release. MODIFIERS is event modifiers for button
10720 release. */
10721
10722 void
10723 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10724 unsigned int modifiers)
10725 {
10726 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10727 struct window *w = XWINDOW (f->tool_bar_window);
10728 int hpos, vpos, prop_idx;
10729 struct glyph *glyph;
10730 Lisp_Object enabled_p;
10731
10732 /* If not on the highlighted tool-bar item, return. */
10733 frame_to_window_pixel_xy (w, &x, &y);
10734 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10735 return;
10736
10737 /* If item is disabled, do nothing. */
10738 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10739 if (NILP (enabled_p))
10740 return;
10741
10742 if (down_p)
10743 {
10744 /* Show item in pressed state. */
10745 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10746 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10747 last_tool_bar_item = prop_idx;
10748 }
10749 else
10750 {
10751 Lisp_Object key, frame;
10752 struct input_event event;
10753 EVENT_INIT (event);
10754
10755 /* Show item in released state. */
10756 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10757 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10758
10759 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10760
10761 XSETFRAME (frame, f);
10762 event.kind = TOOL_BAR_EVENT;
10763 event.frame_or_window = frame;
10764 event.arg = frame;
10765 kbd_buffer_store_event (&event);
10766
10767 event.kind = TOOL_BAR_EVENT;
10768 event.frame_or_window = frame;
10769 event.arg = key;
10770 event.modifiers = modifiers;
10771 kbd_buffer_store_event (&event);
10772 last_tool_bar_item = -1;
10773 }
10774 }
10775
10776
10777 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10778 tool-bar window-relative coordinates X/Y. Called from
10779 note_mouse_highlight. */
10780
10781 static void
10782 note_tool_bar_highlight (struct frame *f, int x, int y)
10783 {
10784 Lisp_Object window = f->tool_bar_window;
10785 struct window *w = XWINDOW (window);
10786 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10787 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10788 int hpos, vpos;
10789 struct glyph *glyph;
10790 struct glyph_row *row;
10791 int i;
10792 Lisp_Object enabled_p;
10793 int prop_idx;
10794 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10795 int mouse_down_p, rc;
10796
10797 /* Function note_mouse_highlight is called with negative X/Y
10798 values when mouse moves outside of the frame. */
10799 if (x <= 0 || y <= 0)
10800 {
10801 clear_mouse_face (hlinfo);
10802 return;
10803 }
10804
10805 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10806 if (rc < 0)
10807 {
10808 /* Not on tool-bar item. */
10809 clear_mouse_face (hlinfo);
10810 return;
10811 }
10812 else if (rc == 0)
10813 /* On same tool-bar item as before. */
10814 goto set_help_echo;
10815
10816 clear_mouse_face (hlinfo);
10817
10818 /* Mouse is down, but on different tool-bar item? */
10819 mouse_down_p = (dpyinfo->grabbed
10820 && f == last_mouse_frame
10821 && FRAME_LIVE_P (f));
10822 if (mouse_down_p
10823 && last_tool_bar_item != prop_idx)
10824 return;
10825
10826 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10827 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10828
10829 /* If tool-bar item is not enabled, don't highlight it. */
10830 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10831 if (!NILP (enabled_p))
10832 {
10833 /* Compute the x-position of the glyph. In front and past the
10834 image is a space. We include this in the highlighted area. */
10835 row = MATRIX_ROW (w->current_matrix, vpos);
10836 for (i = x = 0; i < hpos; ++i)
10837 x += row->glyphs[TEXT_AREA][i].pixel_width;
10838
10839 /* Record this as the current active region. */
10840 hlinfo->mouse_face_beg_col = hpos;
10841 hlinfo->mouse_face_beg_row = vpos;
10842 hlinfo->mouse_face_beg_x = x;
10843 hlinfo->mouse_face_beg_y = row->y;
10844 hlinfo->mouse_face_past_end = 0;
10845
10846 hlinfo->mouse_face_end_col = hpos + 1;
10847 hlinfo->mouse_face_end_row = vpos;
10848 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
10849 hlinfo->mouse_face_end_y = row->y;
10850 hlinfo->mouse_face_window = window;
10851 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10852
10853 /* Display it as active. */
10854 show_mouse_face (hlinfo, draw);
10855 hlinfo->mouse_face_image_state = draw;
10856 }
10857
10858 set_help_echo:
10859
10860 /* Set help_echo_string to a help string to display for this tool-bar item.
10861 XTread_socket does the rest. */
10862 help_echo_object = help_echo_window = Qnil;
10863 help_echo_pos = -1;
10864 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10865 if (NILP (help_echo_string))
10866 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10867 }
10868
10869 #endif /* HAVE_WINDOW_SYSTEM */
10870
10871
10872 \f
10873 /************************************************************************
10874 Horizontal scrolling
10875 ************************************************************************/
10876
10877 static int hscroll_window_tree (Lisp_Object);
10878 static int hscroll_windows (Lisp_Object);
10879
10880 /* For all leaf windows in the window tree rooted at WINDOW, set their
10881 hscroll value so that PT is (i) visible in the window, and (ii) so
10882 that it is not within a certain margin at the window's left and
10883 right border. Value is non-zero if any window's hscroll has been
10884 changed. */
10885
10886 static int
10887 hscroll_window_tree (Lisp_Object window)
10888 {
10889 int hscrolled_p = 0;
10890 int hscroll_relative_p = FLOATP (Vhscroll_step);
10891 int hscroll_step_abs = 0;
10892 double hscroll_step_rel = 0;
10893
10894 if (hscroll_relative_p)
10895 {
10896 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10897 if (hscroll_step_rel < 0)
10898 {
10899 hscroll_relative_p = 0;
10900 hscroll_step_abs = 0;
10901 }
10902 }
10903 else if (INTEGERP (Vhscroll_step))
10904 {
10905 hscroll_step_abs = XINT (Vhscroll_step);
10906 if (hscroll_step_abs < 0)
10907 hscroll_step_abs = 0;
10908 }
10909 else
10910 hscroll_step_abs = 0;
10911
10912 while (WINDOWP (window))
10913 {
10914 struct window *w = XWINDOW (window);
10915
10916 if (WINDOWP (w->hchild))
10917 hscrolled_p |= hscroll_window_tree (w->hchild);
10918 else if (WINDOWP (w->vchild))
10919 hscrolled_p |= hscroll_window_tree (w->vchild);
10920 else if (w->cursor.vpos >= 0)
10921 {
10922 int h_margin;
10923 int text_area_width;
10924 struct glyph_row *current_cursor_row
10925 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10926 struct glyph_row *desired_cursor_row
10927 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10928 struct glyph_row *cursor_row
10929 = (desired_cursor_row->enabled_p
10930 ? desired_cursor_row
10931 : current_cursor_row);
10932
10933 text_area_width = window_box_width (w, TEXT_AREA);
10934
10935 /* Scroll when cursor is inside this scroll margin. */
10936 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10937
10938 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10939 && ((XFASTINT (w->hscroll)
10940 && w->cursor.x <= h_margin)
10941 || (cursor_row->enabled_p
10942 && cursor_row->truncated_on_right_p
10943 && (w->cursor.x >= text_area_width - h_margin))))
10944 {
10945 struct it it;
10946 int hscroll;
10947 struct buffer *saved_current_buffer;
10948 EMACS_INT pt;
10949 int wanted_x;
10950
10951 /* Find point in a display of infinite width. */
10952 saved_current_buffer = current_buffer;
10953 current_buffer = XBUFFER (w->buffer);
10954
10955 if (w == XWINDOW (selected_window))
10956 pt = PT;
10957 else
10958 {
10959 pt = marker_position (w->pointm);
10960 pt = max (BEGV, pt);
10961 pt = min (ZV, pt);
10962 }
10963
10964 /* Move iterator to pt starting at cursor_row->start in
10965 a line with infinite width. */
10966 init_to_row_start (&it, w, cursor_row);
10967 it.last_visible_x = INFINITY;
10968 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10969 current_buffer = saved_current_buffer;
10970
10971 /* Position cursor in window. */
10972 if (!hscroll_relative_p && hscroll_step_abs == 0)
10973 hscroll = max (0, (it.current_x
10974 - (ITERATOR_AT_END_OF_LINE_P (&it)
10975 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10976 : (text_area_width / 2))))
10977 / FRAME_COLUMN_WIDTH (it.f);
10978 else if (w->cursor.x >= text_area_width - h_margin)
10979 {
10980 if (hscroll_relative_p)
10981 wanted_x = text_area_width * (1 - hscroll_step_rel)
10982 - h_margin;
10983 else
10984 wanted_x = text_area_width
10985 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10986 - h_margin;
10987 hscroll
10988 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10989 }
10990 else
10991 {
10992 if (hscroll_relative_p)
10993 wanted_x = text_area_width * hscroll_step_rel
10994 + h_margin;
10995 else
10996 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10997 + h_margin;
10998 hscroll
10999 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11000 }
11001 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11002
11003 /* Don't call Fset_window_hscroll if value hasn't
11004 changed because it will prevent redisplay
11005 optimizations. */
11006 if (XFASTINT (w->hscroll) != hscroll)
11007 {
11008 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11009 w->hscroll = make_number (hscroll);
11010 hscrolled_p = 1;
11011 }
11012 }
11013 }
11014
11015 window = w->next;
11016 }
11017
11018 /* Value is non-zero if hscroll of any leaf window has been changed. */
11019 return hscrolled_p;
11020 }
11021
11022
11023 /* Set hscroll so that cursor is visible and not inside horizontal
11024 scroll margins for all windows in the tree rooted at WINDOW. See
11025 also hscroll_window_tree above. Value is non-zero if any window's
11026 hscroll has been changed. If it has, desired matrices on the frame
11027 of WINDOW are cleared. */
11028
11029 static int
11030 hscroll_windows (Lisp_Object window)
11031 {
11032 int hscrolled_p = hscroll_window_tree (window);
11033 if (hscrolled_p)
11034 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11035 return hscrolled_p;
11036 }
11037
11038
11039 \f
11040 /************************************************************************
11041 Redisplay
11042 ************************************************************************/
11043
11044 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11045 to a non-zero value. This is sometimes handy to have in a debugger
11046 session. */
11047
11048 #if GLYPH_DEBUG
11049
11050 /* First and last unchanged row for try_window_id. */
11051
11052 int debug_first_unchanged_at_end_vpos;
11053 int debug_last_unchanged_at_beg_vpos;
11054
11055 /* Delta vpos and y. */
11056
11057 int debug_dvpos, debug_dy;
11058
11059 /* Delta in characters and bytes for try_window_id. */
11060
11061 EMACS_INT debug_delta, debug_delta_bytes;
11062
11063 /* Values of window_end_pos and window_end_vpos at the end of
11064 try_window_id. */
11065
11066 EMACS_INT debug_end_vpos;
11067
11068 /* Append a string to W->desired_matrix->method. FMT is a printf
11069 format string. A1...A9 are a supplement for a variable-length
11070 argument list. If trace_redisplay_p is non-zero also printf the
11071 resulting string to stderr. */
11072
11073 static void
11074 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11075 struct window *w;
11076 char *fmt;
11077 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11078 {
11079 char buffer[512];
11080 char *method = w->desired_matrix->method;
11081 int len = strlen (method);
11082 int size = sizeof w->desired_matrix->method;
11083 int remaining = size - len - 1;
11084
11085 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11086 if (len && remaining)
11087 {
11088 method[len] = '|';
11089 --remaining, ++len;
11090 }
11091
11092 strncpy (method + len, buffer, remaining);
11093
11094 if (trace_redisplay_p)
11095 fprintf (stderr, "%p (%s): %s\n",
11096 w,
11097 ((BUFFERP (w->buffer)
11098 && STRINGP (XBUFFER (w->buffer)->name))
11099 ? SSDATA (XBUFFER (w->buffer)->name)
11100 : "no buffer"),
11101 buffer);
11102 }
11103
11104 #endif /* GLYPH_DEBUG */
11105
11106
11107 /* Value is non-zero if all changes in window W, which displays
11108 current_buffer, are in the text between START and END. START is a
11109 buffer position, END is given as a distance from Z. Used in
11110 redisplay_internal for display optimization. */
11111
11112 static INLINE int
11113 text_outside_line_unchanged_p (struct window *w,
11114 EMACS_INT start, EMACS_INT end)
11115 {
11116 int unchanged_p = 1;
11117
11118 /* If text or overlays have changed, see where. */
11119 if (XFASTINT (w->last_modified) < MODIFF
11120 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11121 {
11122 /* Gap in the line? */
11123 if (GPT < start || Z - GPT < end)
11124 unchanged_p = 0;
11125
11126 /* Changes start in front of the line, or end after it? */
11127 if (unchanged_p
11128 && (BEG_UNCHANGED < start - 1
11129 || END_UNCHANGED < end))
11130 unchanged_p = 0;
11131
11132 /* If selective display, can't optimize if changes start at the
11133 beginning of the line. */
11134 if (unchanged_p
11135 && INTEGERP (BVAR (current_buffer, selective_display))
11136 && XINT (BVAR (current_buffer, selective_display)) > 0
11137 && (BEG_UNCHANGED < start || GPT <= start))
11138 unchanged_p = 0;
11139
11140 /* If there are overlays at the start or end of the line, these
11141 may have overlay strings with newlines in them. A change at
11142 START, for instance, may actually concern the display of such
11143 overlay strings as well, and they are displayed on different
11144 lines. So, quickly rule out this case. (For the future, it
11145 might be desirable to implement something more telling than
11146 just BEG/END_UNCHANGED.) */
11147 if (unchanged_p)
11148 {
11149 if (BEG + BEG_UNCHANGED == start
11150 && overlay_touches_p (start))
11151 unchanged_p = 0;
11152 if (END_UNCHANGED == end
11153 && overlay_touches_p (Z - end))
11154 unchanged_p = 0;
11155 }
11156
11157 /* Under bidi reordering, adding or deleting a character in the
11158 beginning of a paragraph, before the first strong directional
11159 character, can change the base direction of the paragraph (unless
11160 the buffer specifies a fixed paragraph direction), which will
11161 require to redisplay the whole paragraph. It might be worthwhile
11162 to find the paragraph limits and widen the range of redisplayed
11163 lines to that, but for now just give up this optimization. */
11164 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11165 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11166 unchanged_p = 0;
11167 }
11168
11169 return unchanged_p;
11170 }
11171
11172
11173 /* Do a frame update, taking possible shortcuts into account. This is
11174 the main external entry point for redisplay.
11175
11176 If the last redisplay displayed an echo area message and that message
11177 is no longer requested, we clear the echo area or bring back the
11178 mini-buffer if that is in use. */
11179
11180 void
11181 redisplay (void)
11182 {
11183 redisplay_internal ();
11184 }
11185
11186
11187 static Lisp_Object
11188 overlay_arrow_string_or_property (Lisp_Object var)
11189 {
11190 Lisp_Object val;
11191
11192 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11193 return val;
11194
11195 return Voverlay_arrow_string;
11196 }
11197
11198 /* Return 1 if there are any overlay-arrows in current_buffer. */
11199 static int
11200 overlay_arrow_in_current_buffer_p (void)
11201 {
11202 Lisp_Object vlist;
11203
11204 for (vlist = Voverlay_arrow_variable_list;
11205 CONSP (vlist);
11206 vlist = XCDR (vlist))
11207 {
11208 Lisp_Object var = XCAR (vlist);
11209 Lisp_Object val;
11210
11211 if (!SYMBOLP (var))
11212 continue;
11213 val = find_symbol_value (var);
11214 if (MARKERP (val)
11215 && current_buffer == XMARKER (val)->buffer)
11216 return 1;
11217 }
11218 return 0;
11219 }
11220
11221
11222 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11223 has changed. */
11224
11225 static int
11226 overlay_arrows_changed_p (void)
11227 {
11228 Lisp_Object vlist;
11229
11230 for (vlist = Voverlay_arrow_variable_list;
11231 CONSP (vlist);
11232 vlist = XCDR (vlist))
11233 {
11234 Lisp_Object var = XCAR (vlist);
11235 Lisp_Object val, pstr;
11236
11237 if (!SYMBOLP (var))
11238 continue;
11239 val = find_symbol_value (var);
11240 if (!MARKERP (val))
11241 continue;
11242 if (! EQ (COERCE_MARKER (val),
11243 Fget (var, Qlast_arrow_position))
11244 || ! (pstr = overlay_arrow_string_or_property (var),
11245 EQ (pstr, Fget (var, Qlast_arrow_string))))
11246 return 1;
11247 }
11248 return 0;
11249 }
11250
11251 /* Mark overlay arrows to be updated on next redisplay. */
11252
11253 static void
11254 update_overlay_arrows (int up_to_date)
11255 {
11256 Lisp_Object vlist;
11257
11258 for (vlist = Voverlay_arrow_variable_list;
11259 CONSP (vlist);
11260 vlist = XCDR (vlist))
11261 {
11262 Lisp_Object var = XCAR (vlist);
11263
11264 if (!SYMBOLP (var))
11265 continue;
11266
11267 if (up_to_date > 0)
11268 {
11269 Lisp_Object val = find_symbol_value (var);
11270 Fput (var, Qlast_arrow_position,
11271 COERCE_MARKER (val));
11272 Fput (var, Qlast_arrow_string,
11273 overlay_arrow_string_or_property (var));
11274 }
11275 else if (up_to_date < 0
11276 || !NILP (Fget (var, Qlast_arrow_position)))
11277 {
11278 Fput (var, Qlast_arrow_position, Qt);
11279 Fput (var, Qlast_arrow_string, Qt);
11280 }
11281 }
11282 }
11283
11284
11285 /* Return overlay arrow string to display at row.
11286 Return integer (bitmap number) for arrow bitmap in left fringe.
11287 Return nil if no overlay arrow. */
11288
11289 static Lisp_Object
11290 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11291 {
11292 Lisp_Object vlist;
11293
11294 for (vlist = Voverlay_arrow_variable_list;
11295 CONSP (vlist);
11296 vlist = XCDR (vlist))
11297 {
11298 Lisp_Object var = XCAR (vlist);
11299 Lisp_Object val;
11300
11301 if (!SYMBOLP (var))
11302 continue;
11303
11304 val = find_symbol_value (var);
11305
11306 if (MARKERP (val)
11307 && current_buffer == XMARKER (val)->buffer
11308 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11309 {
11310 if (FRAME_WINDOW_P (it->f)
11311 /* FIXME: if ROW->reversed_p is set, this should test
11312 the right fringe, not the left one. */
11313 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11314 {
11315 #ifdef HAVE_WINDOW_SYSTEM
11316 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11317 {
11318 int fringe_bitmap;
11319 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11320 return make_number (fringe_bitmap);
11321 }
11322 #endif
11323 return make_number (-1); /* Use default arrow bitmap */
11324 }
11325 return overlay_arrow_string_or_property (var);
11326 }
11327 }
11328
11329 return Qnil;
11330 }
11331
11332 /* Return 1 if point moved out of or into a composition. Otherwise
11333 return 0. PREV_BUF and PREV_PT are the last point buffer and
11334 position. BUF and PT are the current point buffer and position. */
11335
11336 static int
11337 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11338 struct buffer *buf, EMACS_INT pt)
11339 {
11340 EMACS_INT start, end;
11341 Lisp_Object prop;
11342 Lisp_Object buffer;
11343
11344 XSETBUFFER (buffer, buf);
11345 /* Check a composition at the last point if point moved within the
11346 same buffer. */
11347 if (prev_buf == buf)
11348 {
11349 if (prev_pt == pt)
11350 /* Point didn't move. */
11351 return 0;
11352
11353 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11354 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11355 && COMPOSITION_VALID_P (start, end, prop)
11356 && start < prev_pt && end > prev_pt)
11357 /* The last point was within the composition. Return 1 iff
11358 point moved out of the composition. */
11359 return (pt <= start || pt >= end);
11360 }
11361
11362 /* Check a composition at the current point. */
11363 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11364 && find_composition (pt, -1, &start, &end, &prop, buffer)
11365 && COMPOSITION_VALID_P (start, end, prop)
11366 && start < pt && end > pt);
11367 }
11368
11369
11370 /* Reconsider the setting of B->clip_changed which is displayed
11371 in window W. */
11372
11373 static INLINE void
11374 reconsider_clip_changes (struct window *w, struct buffer *b)
11375 {
11376 if (b->clip_changed
11377 && !NILP (w->window_end_valid)
11378 && w->current_matrix->buffer == b
11379 && w->current_matrix->zv == BUF_ZV (b)
11380 && w->current_matrix->begv == BUF_BEGV (b))
11381 b->clip_changed = 0;
11382
11383 /* If display wasn't paused, and W is not a tool bar window, see if
11384 point has been moved into or out of a composition. In that case,
11385 we set b->clip_changed to 1 to force updating the screen. If
11386 b->clip_changed has already been set to 1, we can skip this
11387 check. */
11388 if (!b->clip_changed
11389 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11390 {
11391 EMACS_INT pt;
11392
11393 if (w == XWINDOW (selected_window))
11394 pt = PT;
11395 else
11396 pt = marker_position (w->pointm);
11397
11398 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11399 || pt != XINT (w->last_point))
11400 && check_point_in_composition (w->current_matrix->buffer,
11401 XINT (w->last_point),
11402 XBUFFER (w->buffer), pt))
11403 b->clip_changed = 1;
11404 }
11405 }
11406 \f
11407
11408 /* Select FRAME to forward the values of frame-local variables into C
11409 variables so that the redisplay routines can access those values
11410 directly. */
11411
11412 static void
11413 select_frame_for_redisplay (Lisp_Object frame)
11414 {
11415 Lisp_Object tail, tem;
11416 Lisp_Object old = selected_frame;
11417 struct Lisp_Symbol *sym;
11418
11419 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11420
11421 selected_frame = frame;
11422
11423 do {
11424 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11425 if (CONSP (XCAR (tail))
11426 && (tem = XCAR (XCAR (tail)),
11427 SYMBOLP (tem))
11428 && (sym = indirect_variable (XSYMBOL (tem)),
11429 sym->redirect == SYMBOL_LOCALIZED)
11430 && sym->val.blv->frame_local)
11431 /* Use find_symbol_value rather than Fsymbol_value
11432 to avoid an error if it is void. */
11433 find_symbol_value (tem);
11434 } while (!EQ (frame, old) && (frame = old, 1));
11435 }
11436
11437
11438 #define STOP_POLLING \
11439 do { if (! polling_stopped_here) stop_polling (); \
11440 polling_stopped_here = 1; } while (0)
11441
11442 #define RESUME_POLLING \
11443 do { if (polling_stopped_here) start_polling (); \
11444 polling_stopped_here = 0; } while (0)
11445
11446
11447 /* Perhaps in the future avoid recentering windows if it
11448 is not necessary; currently that causes some problems. */
11449
11450 static void
11451 redisplay_internal (void)
11452 {
11453 struct window *w = XWINDOW (selected_window);
11454 struct window *sw;
11455 struct frame *fr;
11456 int pending;
11457 int must_finish = 0;
11458 struct text_pos tlbufpos, tlendpos;
11459 int number_of_visible_frames;
11460 int count, count1;
11461 struct frame *sf;
11462 int polling_stopped_here = 0;
11463 Lisp_Object old_frame = selected_frame;
11464
11465 /* Non-zero means redisplay has to consider all windows on all
11466 frames. Zero means, only selected_window is considered. */
11467 int consider_all_windows_p;
11468
11469 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11470
11471 /* No redisplay if running in batch mode or frame is not yet fully
11472 initialized, or redisplay is explicitly turned off by setting
11473 Vinhibit_redisplay. */
11474 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11475 || !NILP (Vinhibit_redisplay))
11476 return;
11477
11478 /* Don't examine these until after testing Vinhibit_redisplay.
11479 When Emacs is shutting down, perhaps because its connection to
11480 X has dropped, we should not look at them at all. */
11481 fr = XFRAME (w->frame);
11482 sf = SELECTED_FRAME ();
11483
11484 if (!fr->glyphs_initialized_p)
11485 return;
11486
11487 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11488 if (popup_activated ())
11489 return;
11490 #endif
11491
11492 /* I don't think this happens but let's be paranoid. */
11493 if (redisplaying_p)
11494 return;
11495
11496 /* Record a function that resets redisplaying_p to its old value
11497 when we leave this function. */
11498 count = SPECPDL_INDEX ();
11499 record_unwind_protect (unwind_redisplay,
11500 Fcons (make_number (redisplaying_p), selected_frame));
11501 ++redisplaying_p;
11502 specbind (Qinhibit_free_realized_faces, Qnil);
11503
11504 {
11505 Lisp_Object tail, frame;
11506
11507 FOR_EACH_FRAME (tail, frame)
11508 {
11509 struct frame *f = XFRAME (frame);
11510 f->already_hscrolled_p = 0;
11511 }
11512 }
11513
11514 retry:
11515 /* Remember the currently selected window. */
11516 sw = w;
11517
11518 if (!EQ (old_frame, selected_frame)
11519 && FRAME_LIVE_P (XFRAME (old_frame)))
11520 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11521 selected_frame and selected_window to be temporarily out-of-sync so
11522 when we come back here via `goto retry', we need to resync because we
11523 may need to run Elisp code (via prepare_menu_bars). */
11524 select_frame_for_redisplay (old_frame);
11525
11526 pending = 0;
11527 reconsider_clip_changes (w, current_buffer);
11528 last_escape_glyph_frame = NULL;
11529 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11530 last_glyphless_glyph_frame = NULL;
11531 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11532
11533 /* If new fonts have been loaded that make a glyph matrix adjustment
11534 necessary, do it. */
11535 if (fonts_changed_p)
11536 {
11537 adjust_glyphs (NULL);
11538 ++windows_or_buffers_changed;
11539 fonts_changed_p = 0;
11540 }
11541
11542 /* If face_change_count is non-zero, init_iterator will free all
11543 realized faces, which includes the faces referenced from current
11544 matrices. So, we can't reuse current matrices in this case. */
11545 if (face_change_count)
11546 ++windows_or_buffers_changed;
11547
11548 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11549 && FRAME_TTY (sf)->previous_frame != sf)
11550 {
11551 /* Since frames on a single ASCII terminal share the same
11552 display area, displaying a different frame means redisplay
11553 the whole thing. */
11554 windows_or_buffers_changed++;
11555 SET_FRAME_GARBAGED (sf);
11556 #ifndef DOS_NT
11557 set_tty_color_mode (FRAME_TTY (sf), sf);
11558 #endif
11559 FRAME_TTY (sf)->previous_frame = sf;
11560 }
11561
11562 /* Set the visible flags for all frames. Do this before checking
11563 for resized or garbaged frames; they want to know if their frames
11564 are visible. See the comment in frame.h for
11565 FRAME_SAMPLE_VISIBILITY. */
11566 {
11567 Lisp_Object tail, frame;
11568
11569 number_of_visible_frames = 0;
11570
11571 FOR_EACH_FRAME (tail, frame)
11572 {
11573 struct frame *f = XFRAME (frame);
11574
11575 FRAME_SAMPLE_VISIBILITY (f);
11576 if (FRAME_VISIBLE_P (f))
11577 ++number_of_visible_frames;
11578 clear_desired_matrices (f);
11579 }
11580 }
11581
11582 /* Notice any pending interrupt request to change frame size. */
11583 do_pending_window_change (1);
11584
11585 /* do_pending_window_change could change the selected_window due to
11586 frame resizing which makes the selected window too small. */
11587 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
11588 {
11589 sw = w;
11590 reconsider_clip_changes (w, current_buffer);
11591 }
11592
11593 /* Clear frames marked as garbaged. */
11594 if (frame_garbaged)
11595 clear_garbaged_frames ();
11596
11597 /* Build menubar and tool-bar items. */
11598 if (NILP (Vmemory_full))
11599 prepare_menu_bars ();
11600
11601 if (windows_or_buffers_changed)
11602 update_mode_lines++;
11603
11604 /* Detect case that we need to write or remove a star in the mode line. */
11605 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11606 {
11607 w->update_mode_line = Qt;
11608 if (buffer_shared > 1)
11609 update_mode_lines++;
11610 }
11611
11612 /* Avoid invocation of point motion hooks by `current_column' below. */
11613 count1 = SPECPDL_INDEX ();
11614 specbind (Qinhibit_point_motion_hooks, Qt);
11615
11616 /* If %c is in the mode line, update it if needed. */
11617 if (!NILP (w->column_number_displayed)
11618 /* This alternative quickly identifies a common case
11619 where no change is needed. */
11620 && !(PT == XFASTINT (w->last_point)
11621 && XFASTINT (w->last_modified) >= MODIFF
11622 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11623 && (XFASTINT (w->column_number_displayed) != current_column ()))
11624 w->update_mode_line = Qt;
11625
11626 unbind_to (count1, Qnil);
11627
11628 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11629
11630 /* The variable buffer_shared is set in redisplay_window and
11631 indicates that we redisplay a buffer in different windows. See
11632 there. */
11633 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11634 || cursor_type_changed);
11635
11636 /* If specs for an arrow have changed, do thorough redisplay
11637 to ensure we remove any arrow that should no longer exist. */
11638 if (overlay_arrows_changed_p ())
11639 consider_all_windows_p = windows_or_buffers_changed = 1;
11640
11641 /* Normally the message* functions will have already displayed and
11642 updated the echo area, but the frame may have been trashed, or
11643 the update may have been preempted, so display the echo area
11644 again here. Checking message_cleared_p captures the case that
11645 the echo area should be cleared. */
11646 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11647 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11648 || (message_cleared_p
11649 && minibuf_level == 0
11650 /* If the mini-window is currently selected, this means the
11651 echo-area doesn't show through. */
11652 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11653 {
11654 int window_height_changed_p = echo_area_display (0);
11655 must_finish = 1;
11656
11657 /* If we don't display the current message, don't clear the
11658 message_cleared_p flag, because, if we did, we wouldn't clear
11659 the echo area in the next redisplay which doesn't preserve
11660 the echo area. */
11661 if (!display_last_displayed_message_p)
11662 message_cleared_p = 0;
11663
11664 if (fonts_changed_p)
11665 goto retry;
11666 else if (window_height_changed_p)
11667 {
11668 consider_all_windows_p = 1;
11669 ++update_mode_lines;
11670 ++windows_or_buffers_changed;
11671
11672 /* If window configuration was changed, frames may have been
11673 marked garbaged. Clear them or we will experience
11674 surprises wrt scrolling. */
11675 if (frame_garbaged)
11676 clear_garbaged_frames ();
11677 }
11678 }
11679 else if (EQ (selected_window, minibuf_window)
11680 && (current_buffer->clip_changed
11681 || XFASTINT (w->last_modified) < MODIFF
11682 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11683 && resize_mini_window (w, 0))
11684 {
11685 /* Resized active mini-window to fit the size of what it is
11686 showing if its contents might have changed. */
11687 must_finish = 1;
11688 /* FIXME: this causes all frames to be updated, which seems unnecessary
11689 since only the current frame needs to be considered. This function needs
11690 to be rewritten with two variables, consider_all_windows and
11691 consider_all_frames. */
11692 consider_all_windows_p = 1;
11693 ++windows_or_buffers_changed;
11694 ++update_mode_lines;
11695
11696 /* If window configuration was changed, frames may have been
11697 marked garbaged. Clear them or we will experience
11698 surprises wrt scrolling. */
11699 if (frame_garbaged)
11700 clear_garbaged_frames ();
11701 }
11702
11703
11704 /* If showing the region, and mark has changed, we must redisplay
11705 the whole window. The assignment to this_line_start_pos prevents
11706 the optimization directly below this if-statement. */
11707 if (((!NILP (Vtransient_mark_mode)
11708 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11709 != !NILP (w->region_showing))
11710 || (!NILP (w->region_showing)
11711 && !EQ (w->region_showing,
11712 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
11713 CHARPOS (this_line_start_pos) = 0;
11714
11715 /* Optimize the case that only the line containing the cursor in the
11716 selected window has changed. Variables starting with this_ are
11717 set in display_line and record information about the line
11718 containing the cursor. */
11719 tlbufpos = this_line_start_pos;
11720 tlendpos = this_line_end_pos;
11721 if (!consider_all_windows_p
11722 && CHARPOS (tlbufpos) > 0
11723 && NILP (w->update_mode_line)
11724 && !current_buffer->clip_changed
11725 && !current_buffer->prevent_redisplay_optimizations_p
11726 && FRAME_VISIBLE_P (XFRAME (w->frame))
11727 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11728 /* Make sure recorded data applies to current buffer, etc. */
11729 && this_line_buffer == current_buffer
11730 && current_buffer == XBUFFER (w->buffer)
11731 && NILP (w->force_start)
11732 && NILP (w->optional_new_start)
11733 /* Point must be on the line that we have info recorded about. */
11734 && PT >= CHARPOS (tlbufpos)
11735 && PT <= Z - CHARPOS (tlendpos)
11736 /* All text outside that line, including its final newline,
11737 must be unchanged. */
11738 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11739 CHARPOS (tlendpos)))
11740 {
11741 if (CHARPOS (tlbufpos) > BEGV
11742 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11743 && (CHARPOS (tlbufpos) == ZV
11744 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11745 /* Former continuation line has disappeared by becoming empty. */
11746 goto cancel;
11747 else if (XFASTINT (w->last_modified) < MODIFF
11748 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11749 || MINI_WINDOW_P (w))
11750 {
11751 /* We have to handle the case of continuation around a
11752 wide-column character (see the comment in indent.c around
11753 line 1340).
11754
11755 For instance, in the following case:
11756
11757 -------- Insert --------
11758 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11759 J_I_ ==> J_I_ `^^' are cursors.
11760 ^^ ^^
11761 -------- --------
11762
11763 As we have to redraw the line above, we cannot use this
11764 optimization. */
11765
11766 struct it it;
11767 int line_height_before = this_line_pixel_height;
11768
11769 /* Note that start_display will handle the case that the
11770 line starting at tlbufpos is a continuation line. */
11771 start_display (&it, w, tlbufpos);
11772
11773 /* Implementation note: It this still necessary? */
11774 if (it.current_x != this_line_start_x)
11775 goto cancel;
11776
11777 TRACE ((stderr, "trying display optimization 1\n"));
11778 w->cursor.vpos = -1;
11779 overlay_arrow_seen = 0;
11780 it.vpos = this_line_vpos;
11781 it.current_y = this_line_y;
11782 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11783 display_line (&it);
11784
11785 /* If line contains point, is not continued,
11786 and ends at same distance from eob as before, we win. */
11787 if (w->cursor.vpos >= 0
11788 /* Line is not continued, otherwise this_line_start_pos
11789 would have been set to 0 in display_line. */
11790 && CHARPOS (this_line_start_pos)
11791 /* Line ends as before. */
11792 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11793 /* Line has same height as before. Otherwise other lines
11794 would have to be shifted up or down. */
11795 && this_line_pixel_height == line_height_before)
11796 {
11797 /* If this is not the window's last line, we must adjust
11798 the charstarts of the lines below. */
11799 if (it.current_y < it.last_visible_y)
11800 {
11801 struct glyph_row *row
11802 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11803 EMACS_INT delta, delta_bytes;
11804
11805 /* We used to distinguish between two cases here,
11806 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11807 when the line ends in a newline or the end of the
11808 buffer's accessible portion. But both cases did
11809 the same, so they were collapsed. */
11810 delta = (Z
11811 - CHARPOS (tlendpos)
11812 - MATRIX_ROW_START_CHARPOS (row));
11813 delta_bytes = (Z_BYTE
11814 - BYTEPOS (tlendpos)
11815 - MATRIX_ROW_START_BYTEPOS (row));
11816
11817 increment_matrix_positions (w->current_matrix,
11818 this_line_vpos + 1,
11819 w->current_matrix->nrows,
11820 delta, delta_bytes);
11821 }
11822
11823 /* If this row displays text now but previously didn't,
11824 or vice versa, w->window_end_vpos may have to be
11825 adjusted. */
11826 if ((it.glyph_row - 1)->displays_text_p)
11827 {
11828 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11829 XSETINT (w->window_end_vpos, this_line_vpos);
11830 }
11831 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11832 && this_line_vpos > 0)
11833 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11834 w->window_end_valid = Qnil;
11835
11836 /* Update hint: No need to try to scroll in update_window. */
11837 w->desired_matrix->no_scrolling_p = 1;
11838
11839 #if GLYPH_DEBUG
11840 *w->desired_matrix->method = 0;
11841 debug_method_add (w, "optimization 1");
11842 #endif
11843 #ifdef HAVE_WINDOW_SYSTEM
11844 update_window_fringes (w, 0);
11845 #endif
11846 goto update;
11847 }
11848 else
11849 goto cancel;
11850 }
11851 else if (/* Cursor position hasn't changed. */
11852 PT == XFASTINT (w->last_point)
11853 /* Make sure the cursor was last displayed
11854 in this window. Otherwise we have to reposition it. */
11855 && 0 <= w->cursor.vpos
11856 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11857 {
11858 if (!must_finish)
11859 {
11860 do_pending_window_change (1);
11861 /* If selected_window changed, redisplay again. */
11862 if (WINDOWP (selected_window)
11863 && (w = XWINDOW (selected_window)) != sw)
11864 goto retry;
11865
11866 /* We used to always goto end_of_redisplay here, but this
11867 isn't enough if we have a blinking cursor. */
11868 if (w->cursor_off_p == w->last_cursor_off_p)
11869 goto end_of_redisplay;
11870 }
11871 goto update;
11872 }
11873 /* If highlighting the region, or if the cursor is in the echo area,
11874 then we can't just move the cursor. */
11875 else if (! (!NILP (Vtransient_mark_mode)
11876 && !NILP (BVAR (current_buffer, mark_active)))
11877 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
11878 || highlight_nonselected_windows)
11879 && NILP (w->region_showing)
11880 && NILP (Vshow_trailing_whitespace)
11881 && !cursor_in_echo_area)
11882 {
11883 struct it it;
11884 struct glyph_row *row;
11885
11886 /* Skip from tlbufpos to PT and see where it is. Note that
11887 PT may be in invisible text. If so, we will end at the
11888 next visible position. */
11889 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11890 NULL, DEFAULT_FACE_ID);
11891 it.current_x = this_line_start_x;
11892 it.current_y = this_line_y;
11893 it.vpos = this_line_vpos;
11894
11895 /* The call to move_it_to stops in front of PT, but
11896 moves over before-strings. */
11897 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11898
11899 if (it.vpos == this_line_vpos
11900 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11901 row->enabled_p))
11902 {
11903 xassert (this_line_vpos == it.vpos);
11904 xassert (this_line_y == it.current_y);
11905 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11906 #if GLYPH_DEBUG
11907 *w->desired_matrix->method = 0;
11908 debug_method_add (w, "optimization 3");
11909 #endif
11910 goto update;
11911 }
11912 else
11913 goto cancel;
11914 }
11915
11916 cancel:
11917 /* Text changed drastically or point moved off of line. */
11918 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11919 }
11920
11921 CHARPOS (this_line_start_pos) = 0;
11922 consider_all_windows_p |= buffer_shared > 1;
11923 ++clear_face_cache_count;
11924 #ifdef HAVE_WINDOW_SYSTEM
11925 ++clear_image_cache_count;
11926 #endif
11927
11928 /* Build desired matrices, and update the display. If
11929 consider_all_windows_p is non-zero, do it for all windows on all
11930 frames. Otherwise do it for selected_window, only. */
11931
11932 if (consider_all_windows_p)
11933 {
11934 Lisp_Object tail, frame;
11935
11936 FOR_EACH_FRAME (tail, frame)
11937 XFRAME (frame)->updated_p = 0;
11938
11939 /* Recompute # windows showing selected buffer. This will be
11940 incremented each time such a window is displayed. */
11941 buffer_shared = 0;
11942
11943 FOR_EACH_FRAME (tail, frame)
11944 {
11945 struct frame *f = XFRAME (frame);
11946
11947 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11948 {
11949 if (! EQ (frame, selected_frame))
11950 /* Select the frame, for the sake of frame-local
11951 variables. */
11952 select_frame_for_redisplay (frame);
11953
11954 /* Mark all the scroll bars to be removed; we'll redeem
11955 the ones we want when we redisplay their windows. */
11956 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11957 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11958
11959 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11960 redisplay_windows (FRAME_ROOT_WINDOW (f));
11961
11962 /* The X error handler may have deleted that frame. */
11963 if (!FRAME_LIVE_P (f))
11964 continue;
11965
11966 /* Any scroll bars which redisplay_windows should have
11967 nuked should now go away. */
11968 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11969 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11970
11971 /* If fonts changed, display again. */
11972 /* ??? rms: I suspect it is a mistake to jump all the way
11973 back to retry here. It should just retry this frame. */
11974 if (fonts_changed_p)
11975 goto retry;
11976
11977 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11978 {
11979 /* See if we have to hscroll. */
11980 if (!f->already_hscrolled_p)
11981 {
11982 f->already_hscrolled_p = 1;
11983 if (hscroll_windows (f->root_window))
11984 goto retry;
11985 }
11986
11987 /* Prevent various kinds of signals during display
11988 update. stdio is not robust about handling
11989 signals, which can cause an apparent I/O
11990 error. */
11991 if (interrupt_input)
11992 unrequest_sigio ();
11993 STOP_POLLING;
11994
11995 /* Update the display. */
11996 set_window_update_flags (XWINDOW (f->root_window), 1);
11997 pending |= update_frame (f, 0, 0);
11998 f->updated_p = 1;
11999 }
12000 }
12001 }
12002
12003 if (!EQ (old_frame, selected_frame)
12004 && FRAME_LIVE_P (XFRAME (old_frame)))
12005 /* We played a bit fast-and-loose above and allowed selected_frame
12006 and selected_window to be temporarily out-of-sync but let's make
12007 sure this stays contained. */
12008 select_frame_for_redisplay (old_frame);
12009 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12010
12011 if (!pending)
12012 {
12013 /* Do the mark_window_display_accurate after all windows have
12014 been redisplayed because this call resets flags in buffers
12015 which are needed for proper redisplay. */
12016 FOR_EACH_FRAME (tail, frame)
12017 {
12018 struct frame *f = XFRAME (frame);
12019 if (f->updated_p)
12020 {
12021 mark_window_display_accurate (f->root_window, 1);
12022 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12023 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12024 }
12025 }
12026 }
12027 }
12028 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12029 {
12030 Lisp_Object mini_window;
12031 struct frame *mini_frame;
12032
12033 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12034 /* Use list_of_error, not Qerror, so that
12035 we catch only errors and don't run the debugger. */
12036 internal_condition_case_1 (redisplay_window_1, selected_window,
12037 list_of_error,
12038 redisplay_window_error);
12039
12040 /* Compare desired and current matrices, perform output. */
12041
12042 update:
12043 /* If fonts changed, display again. */
12044 if (fonts_changed_p)
12045 goto retry;
12046
12047 /* Prevent various kinds of signals during display update.
12048 stdio is not robust about handling signals,
12049 which can cause an apparent I/O error. */
12050 if (interrupt_input)
12051 unrequest_sigio ();
12052 STOP_POLLING;
12053
12054 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12055 {
12056 if (hscroll_windows (selected_window))
12057 goto retry;
12058
12059 XWINDOW (selected_window)->must_be_updated_p = 1;
12060 pending = update_frame (sf, 0, 0);
12061 }
12062
12063 /* We may have called echo_area_display at the top of this
12064 function. If the echo area is on another frame, that may
12065 have put text on a frame other than the selected one, so the
12066 above call to update_frame would not have caught it. Catch
12067 it here. */
12068 mini_window = FRAME_MINIBUF_WINDOW (sf);
12069 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12070
12071 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12072 {
12073 XWINDOW (mini_window)->must_be_updated_p = 1;
12074 pending |= update_frame (mini_frame, 0, 0);
12075 if (!pending && hscroll_windows (mini_window))
12076 goto retry;
12077 }
12078 }
12079
12080 /* If display was paused because of pending input, make sure we do a
12081 thorough update the next time. */
12082 if (pending)
12083 {
12084 /* Prevent the optimization at the beginning of
12085 redisplay_internal that tries a single-line update of the
12086 line containing the cursor in the selected window. */
12087 CHARPOS (this_line_start_pos) = 0;
12088
12089 /* Let the overlay arrow be updated the next time. */
12090 update_overlay_arrows (0);
12091
12092 /* If we pause after scrolling, some rows in the current
12093 matrices of some windows are not valid. */
12094 if (!WINDOW_FULL_WIDTH_P (w)
12095 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12096 update_mode_lines = 1;
12097 }
12098 else
12099 {
12100 if (!consider_all_windows_p)
12101 {
12102 /* This has already been done above if
12103 consider_all_windows_p is set. */
12104 mark_window_display_accurate_1 (w, 1);
12105
12106 /* Say overlay arrows are up to date. */
12107 update_overlay_arrows (1);
12108
12109 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12110 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12111 }
12112
12113 update_mode_lines = 0;
12114 windows_or_buffers_changed = 0;
12115 cursor_type_changed = 0;
12116 }
12117
12118 /* Start SIGIO interrupts coming again. Having them off during the
12119 code above makes it less likely one will discard output, but not
12120 impossible, since there might be stuff in the system buffer here.
12121 But it is much hairier to try to do anything about that. */
12122 if (interrupt_input)
12123 request_sigio ();
12124 RESUME_POLLING;
12125
12126 /* If a frame has become visible which was not before, redisplay
12127 again, so that we display it. Expose events for such a frame
12128 (which it gets when becoming visible) don't call the parts of
12129 redisplay constructing glyphs, so simply exposing a frame won't
12130 display anything in this case. So, we have to display these
12131 frames here explicitly. */
12132 if (!pending)
12133 {
12134 Lisp_Object tail, frame;
12135 int new_count = 0;
12136
12137 FOR_EACH_FRAME (tail, frame)
12138 {
12139 int this_is_visible = 0;
12140
12141 if (XFRAME (frame)->visible)
12142 this_is_visible = 1;
12143 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12144 if (XFRAME (frame)->visible)
12145 this_is_visible = 1;
12146
12147 if (this_is_visible)
12148 new_count++;
12149 }
12150
12151 if (new_count != number_of_visible_frames)
12152 windows_or_buffers_changed++;
12153 }
12154
12155 /* Change frame size now if a change is pending. */
12156 do_pending_window_change (1);
12157
12158 /* If we just did a pending size change, or have additional
12159 visible frames, or selected_window changed, redisplay again. */
12160 if ((windows_or_buffers_changed && !pending)
12161 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12162 goto retry;
12163
12164 /* Clear the face and image caches.
12165
12166 We used to do this only if consider_all_windows_p. But the cache
12167 needs to be cleared if a timer creates images in the current
12168 buffer (e.g. the test case in Bug#6230). */
12169
12170 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12171 {
12172 clear_face_cache (0);
12173 clear_face_cache_count = 0;
12174 }
12175
12176 #ifdef HAVE_WINDOW_SYSTEM
12177 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12178 {
12179 clear_image_caches (Qnil);
12180 clear_image_cache_count = 0;
12181 }
12182 #endif /* HAVE_WINDOW_SYSTEM */
12183
12184 end_of_redisplay:
12185 unbind_to (count, Qnil);
12186 RESUME_POLLING;
12187 }
12188
12189
12190 /* Redisplay, but leave alone any recent echo area message unless
12191 another message has been requested in its place.
12192
12193 This is useful in situations where you need to redisplay but no
12194 user action has occurred, making it inappropriate for the message
12195 area to be cleared. See tracking_off and
12196 wait_reading_process_output for examples of these situations.
12197
12198 FROM_WHERE is an integer saying from where this function was
12199 called. This is useful for debugging. */
12200
12201 void
12202 redisplay_preserve_echo_area (int from_where)
12203 {
12204 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12205
12206 if (!NILP (echo_area_buffer[1]))
12207 {
12208 /* We have a previously displayed message, but no current
12209 message. Redisplay the previous message. */
12210 display_last_displayed_message_p = 1;
12211 redisplay_internal ();
12212 display_last_displayed_message_p = 0;
12213 }
12214 else
12215 redisplay_internal ();
12216
12217 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12218 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12219 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12220 }
12221
12222
12223 /* Function registered with record_unwind_protect in
12224 redisplay_internal. Reset redisplaying_p to the value it had
12225 before redisplay_internal was called, and clear
12226 prevent_freeing_realized_faces_p. It also selects the previously
12227 selected frame, unless it has been deleted (by an X connection
12228 failure during redisplay, for example). */
12229
12230 static Lisp_Object
12231 unwind_redisplay (Lisp_Object val)
12232 {
12233 Lisp_Object old_redisplaying_p, old_frame;
12234
12235 old_redisplaying_p = XCAR (val);
12236 redisplaying_p = XFASTINT (old_redisplaying_p);
12237 old_frame = XCDR (val);
12238 if (! EQ (old_frame, selected_frame)
12239 && FRAME_LIVE_P (XFRAME (old_frame)))
12240 select_frame_for_redisplay (old_frame);
12241 return Qnil;
12242 }
12243
12244
12245 /* Mark the display of window W as accurate or inaccurate. If
12246 ACCURATE_P is non-zero mark display of W as accurate. If
12247 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12248 redisplay_internal is called. */
12249
12250 static void
12251 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12252 {
12253 if (BUFFERP (w->buffer))
12254 {
12255 struct buffer *b = XBUFFER (w->buffer);
12256
12257 w->last_modified
12258 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12259 w->last_overlay_modified
12260 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12261 w->last_had_star
12262 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12263
12264 if (accurate_p)
12265 {
12266 b->clip_changed = 0;
12267 b->prevent_redisplay_optimizations_p = 0;
12268
12269 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12270 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12271 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12272 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12273
12274 w->current_matrix->buffer = b;
12275 w->current_matrix->begv = BUF_BEGV (b);
12276 w->current_matrix->zv = BUF_ZV (b);
12277
12278 w->last_cursor = w->cursor;
12279 w->last_cursor_off_p = w->cursor_off_p;
12280
12281 if (w == XWINDOW (selected_window))
12282 w->last_point = make_number (BUF_PT (b));
12283 else
12284 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12285 }
12286 }
12287
12288 if (accurate_p)
12289 {
12290 w->window_end_valid = w->buffer;
12291 w->update_mode_line = Qnil;
12292 }
12293 }
12294
12295
12296 /* Mark the display of windows in the window tree rooted at WINDOW as
12297 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12298 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12299 be redisplayed the next time redisplay_internal is called. */
12300
12301 void
12302 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12303 {
12304 struct window *w;
12305
12306 for (; !NILP (window); window = w->next)
12307 {
12308 w = XWINDOW (window);
12309 mark_window_display_accurate_1 (w, accurate_p);
12310
12311 if (!NILP (w->vchild))
12312 mark_window_display_accurate (w->vchild, accurate_p);
12313 if (!NILP (w->hchild))
12314 mark_window_display_accurate (w->hchild, accurate_p);
12315 }
12316
12317 if (accurate_p)
12318 {
12319 update_overlay_arrows (1);
12320 }
12321 else
12322 {
12323 /* Force a thorough redisplay the next time by setting
12324 last_arrow_position and last_arrow_string to t, which is
12325 unequal to any useful value of Voverlay_arrow_... */
12326 update_overlay_arrows (-1);
12327 }
12328 }
12329
12330
12331 /* Return value in display table DP (Lisp_Char_Table *) for character
12332 C. Since a display table doesn't have any parent, we don't have to
12333 follow parent. Do not call this function directly but use the
12334 macro DISP_CHAR_VECTOR. */
12335
12336 Lisp_Object
12337 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12338 {
12339 Lisp_Object val;
12340
12341 if (ASCII_CHAR_P (c))
12342 {
12343 val = dp->ascii;
12344 if (SUB_CHAR_TABLE_P (val))
12345 val = XSUB_CHAR_TABLE (val)->contents[c];
12346 }
12347 else
12348 {
12349 Lisp_Object table;
12350
12351 XSETCHAR_TABLE (table, dp);
12352 val = char_table_ref (table, c);
12353 }
12354 if (NILP (val))
12355 val = dp->defalt;
12356 return val;
12357 }
12358
12359
12360 \f
12361 /***********************************************************************
12362 Window Redisplay
12363 ***********************************************************************/
12364
12365 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12366
12367 static void
12368 redisplay_windows (Lisp_Object window)
12369 {
12370 while (!NILP (window))
12371 {
12372 struct window *w = XWINDOW (window);
12373
12374 if (!NILP (w->hchild))
12375 redisplay_windows (w->hchild);
12376 else if (!NILP (w->vchild))
12377 redisplay_windows (w->vchild);
12378 else if (!NILP (w->buffer))
12379 {
12380 displayed_buffer = XBUFFER (w->buffer);
12381 /* Use list_of_error, not Qerror, so that
12382 we catch only errors and don't run the debugger. */
12383 internal_condition_case_1 (redisplay_window_0, window,
12384 list_of_error,
12385 redisplay_window_error);
12386 }
12387
12388 window = w->next;
12389 }
12390 }
12391
12392 static Lisp_Object
12393 redisplay_window_error (Lisp_Object ignore)
12394 {
12395 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12396 return Qnil;
12397 }
12398
12399 static Lisp_Object
12400 redisplay_window_0 (Lisp_Object window)
12401 {
12402 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12403 redisplay_window (window, 0);
12404 return Qnil;
12405 }
12406
12407 static Lisp_Object
12408 redisplay_window_1 (Lisp_Object window)
12409 {
12410 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12411 redisplay_window (window, 1);
12412 return Qnil;
12413 }
12414 \f
12415
12416 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12417 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12418 which positions recorded in ROW differ from current buffer
12419 positions.
12420
12421 Return 0 if cursor is not on this row, 1 otherwise. */
12422
12423 static int
12424 set_cursor_from_row (struct window *w, struct glyph_row *row,
12425 struct glyph_matrix *matrix,
12426 EMACS_INT delta, EMACS_INT delta_bytes,
12427 int dy, int dvpos)
12428 {
12429 struct glyph *glyph = row->glyphs[TEXT_AREA];
12430 struct glyph *end = glyph + row->used[TEXT_AREA];
12431 struct glyph *cursor = NULL;
12432 /* The last known character position in row. */
12433 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12434 int x = row->x;
12435 EMACS_INT pt_old = PT - delta;
12436 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12437 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12438 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12439 /* A glyph beyond the edge of TEXT_AREA which we should never
12440 touch. */
12441 struct glyph *glyphs_end = end;
12442 /* Non-zero means we've found a match for cursor position, but that
12443 glyph has the avoid_cursor_p flag set. */
12444 int match_with_avoid_cursor = 0;
12445 /* Non-zero means we've seen at least one glyph that came from a
12446 display string. */
12447 int string_seen = 0;
12448 /* Largest and smalles buffer positions seen so far during scan of
12449 glyph row. */
12450 EMACS_INT bpos_max = pos_before;
12451 EMACS_INT bpos_min = pos_after;
12452 /* Last buffer position covered by an overlay string with an integer
12453 `cursor' property. */
12454 EMACS_INT bpos_covered = 0;
12455
12456 /* Skip over glyphs not having an object at the start and the end of
12457 the row. These are special glyphs like truncation marks on
12458 terminal frames. */
12459 if (row->displays_text_p)
12460 {
12461 if (!row->reversed_p)
12462 {
12463 while (glyph < end
12464 && INTEGERP (glyph->object)
12465 && glyph->charpos < 0)
12466 {
12467 x += glyph->pixel_width;
12468 ++glyph;
12469 }
12470 while (end > glyph
12471 && INTEGERP ((end - 1)->object)
12472 /* CHARPOS is zero for blanks and stretch glyphs
12473 inserted by extend_face_to_end_of_line. */
12474 && (end - 1)->charpos <= 0)
12475 --end;
12476 glyph_before = glyph - 1;
12477 glyph_after = end;
12478 }
12479 else
12480 {
12481 struct glyph *g;
12482
12483 /* If the glyph row is reversed, we need to process it from back
12484 to front, so swap the edge pointers. */
12485 glyphs_end = end = glyph - 1;
12486 glyph += row->used[TEXT_AREA] - 1;
12487
12488 while (glyph > end + 1
12489 && INTEGERP (glyph->object)
12490 && glyph->charpos < 0)
12491 {
12492 --glyph;
12493 x -= glyph->pixel_width;
12494 }
12495 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12496 --glyph;
12497 /* By default, in reversed rows we put the cursor on the
12498 rightmost (first in the reading order) glyph. */
12499 for (g = end + 1; g < glyph; g++)
12500 x += g->pixel_width;
12501 while (end < glyph
12502 && INTEGERP ((end + 1)->object)
12503 && (end + 1)->charpos <= 0)
12504 ++end;
12505 glyph_before = glyph + 1;
12506 glyph_after = end;
12507 }
12508 }
12509 else if (row->reversed_p)
12510 {
12511 /* In R2L rows that don't display text, put the cursor on the
12512 rightmost glyph. Case in point: an empty last line that is
12513 part of an R2L paragraph. */
12514 cursor = end - 1;
12515 /* Avoid placing the cursor on the last glyph of the row, where
12516 on terminal frames we hold the vertical border between
12517 adjacent windows. */
12518 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12519 && !WINDOW_RIGHTMOST_P (w)
12520 && cursor == row->glyphs[LAST_AREA] - 1)
12521 cursor--;
12522 x = -1; /* will be computed below, at label compute_x */
12523 }
12524
12525 /* Step 1: Try to find the glyph whose character position
12526 corresponds to point. If that's not possible, find 2 glyphs
12527 whose character positions are the closest to point, one before
12528 point, the other after it. */
12529 if (!row->reversed_p)
12530 while (/* not marched to end of glyph row */
12531 glyph < end
12532 /* glyph was not inserted by redisplay for internal purposes */
12533 && !INTEGERP (glyph->object))
12534 {
12535 if (BUFFERP (glyph->object))
12536 {
12537 EMACS_INT dpos = glyph->charpos - pt_old;
12538
12539 if (glyph->charpos > bpos_max)
12540 bpos_max = glyph->charpos;
12541 if (glyph->charpos < bpos_min)
12542 bpos_min = glyph->charpos;
12543 if (!glyph->avoid_cursor_p)
12544 {
12545 /* If we hit point, we've found the glyph on which to
12546 display the cursor. */
12547 if (dpos == 0)
12548 {
12549 match_with_avoid_cursor = 0;
12550 break;
12551 }
12552 /* See if we've found a better approximation to
12553 POS_BEFORE or to POS_AFTER. Note that we want the
12554 first (leftmost) glyph of all those that are the
12555 closest from below, and the last (rightmost) of all
12556 those from above. */
12557 if (0 > dpos && dpos > pos_before - pt_old)
12558 {
12559 pos_before = glyph->charpos;
12560 glyph_before = glyph;
12561 }
12562 else if (0 < dpos && dpos <= pos_after - pt_old)
12563 {
12564 pos_after = glyph->charpos;
12565 glyph_after = glyph;
12566 }
12567 }
12568 else if (dpos == 0)
12569 match_with_avoid_cursor = 1;
12570 }
12571 else if (STRINGP (glyph->object))
12572 {
12573 Lisp_Object chprop;
12574 EMACS_INT glyph_pos = glyph->charpos;
12575
12576 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12577 glyph->object);
12578 if (INTEGERP (chprop))
12579 {
12580 bpos_covered = bpos_max + XINT (chprop);
12581 /* If the `cursor' property covers buffer positions up
12582 to and including point, we should display cursor on
12583 this glyph. Note that overlays and text properties
12584 with string values stop bidi reordering, so every
12585 buffer position to the left of the string is always
12586 smaller than any position to the right of the
12587 string. Therefore, if a `cursor' property on one
12588 of the string's characters has an integer value, we
12589 will break out of the loop below _before_ we get to
12590 the position match above. IOW, integer values of
12591 the `cursor' property override the "exact match for
12592 point" strategy of positioning the cursor. */
12593 /* Implementation note: bpos_max == pt_old when, e.g.,
12594 we are in an empty line, where bpos_max is set to
12595 MATRIX_ROW_START_CHARPOS, see above. */
12596 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12597 {
12598 cursor = glyph;
12599 break;
12600 }
12601 }
12602
12603 string_seen = 1;
12604 }
12605 x += glyph->pixel_width;
12606 ++glyph;
12607 }
12608 else if (glyph > end) /* row is reversed */
12609 while (!INTEGERP (glyph->object))
12610 {
12611 if (BUFFERP (glyph->object))
12612 {
12613 EMACS_INT dpos = glyph->charpos - pt_old;
12614
12615 if (glyph->charpos > bpos_max)
12616 bpos_max = glyph->charpos;
12617 if (glyph->charpos < bpos_min)
12618 bpos_min = glyph->charpos;
12619 if (!glyph->avoid_cursor_p)
12620 {
12621 if (dpos == 0)
12622 {
12623 match_with_avoid_cursor = 0;
12624 break;
12625 }
12626 if (0 > dpos && dpos > pos_before - pt_old)
12627 {
12628 pos_before = glyph->charpos;
12629 glyph_before = glyph;
12630 }
12631 else if (0 < dpos && dpos <= pos_after - pt_old)
12632 {
12633 pos_after = glyph->charpos;
12634 glyph_after = glyph;
12635 }
12636 }
12637 else if (dpos == 0)
12638 match_with_avoid_cursor = 1;
12639 }
12640 else if (STRINGP (glyph->object))
12641 {
12642 Lisp_Object chprop;
12643 EMACS_INT glyph_pos = glyph->charpos;
12644
12645 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12646 glyph->object);
12647 if (INTEGERP (chprop))
12648 {
12649 bpos_covered = bpos_max + XINT (chprop);
12650 /* If the `cursor' property covers buffer positions up
12651 to and including point, we should display cursor on
12652 this glyph. */
12653 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12654 {
12655 cursor = glyph;
12656 break;
12657 }
12658 }
12659 string_seen = 1;
12660 }
12661 --glyph;
12662 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12663 {
12664 x--; /* can't use any pixel_width */
12665 break;
12666 }
12667 x -= glyph->pixel_width;
12668 }
12669
12670 /* Step 2: If we didn't find an exact match for point, we need to
12671 look for a proper place to put the cursor among glyphs between
12672 GLYPH_BEFORE and GLYPH_AFTER. */
12673 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12674 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12675 && bpos_covered < pt_old)
12676 {
12677 /* An empty line has a single glyph whose OBJECT is zero and
12678 whose CHARPOS is the position of a newline on that line.
12679 Note that on a TTY, there are more glyphs after that, which
12680 were produced by extend_face_to_end_of_line, but their
12681 CHARPOS is zero or negative. */
12682 int empty_line_p =
12683 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12684 && INTEGERP (glyph->object) && glyph->charpos > 0;
12685
12686 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12687 {
12688 EMACS_INT ellipsis_pos;
12689
12690 /* Scan back over the ellipsis glyphs. */
12691 if (!row->reversed_p)
12692 {
12693 ellipsis_pos = (glyph - 1)->charpos;
12694 while (glyph > row->glyphs[TEXT_AREA]
12695 && (glyph - 1)->charpos == ellipsis_pos)
12696 glyph--, x -= glyph->pixel_width;
12697 /* That loop always goes one position too far, including
12698 the glyph before the ellipsis. So scan forward over
12699 that one. */
12700 x += glyph->pixel_width;
12701 glyph++;
12702 }
12703 else /* row is reversed */
12704 {
12705 ellipsis_pos = (glyph + 1)->charpos;
12706 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12707 && (glyph + 1)->charpos == ellipsis_pos)
12708 glyph++, x += glyph->pixel_width;
12709 x -= glyph->pixel_width;
12710 glyph--;
12711 }
12712 }
12713 else if (match_with_avoid_cursor
12714 /* A truncated row may not include PT among its
12715 character positions. Setting the cursor inside the
12716 scroll margin will trigger recalculation of hscroll
12717 in hscroll_window_tree. */
12718 || (row->truncated_on_left_p && pt_old < bpos_min)
12719 || (row->truncated_on_right_p && pt_old > bpos_max)
12720 /* Zero-width characters produce no glyphs. */
12721 || (!string_seen
12722 && !empty_line_p
12723 && (row->reversed_p
12724 ? glyph_after > glyphs_end
12725 : glyph_after < glyphs_end)))
12726 {
12727 cursor = glyph_after;
12728 x = -1;
12729 }
12730 else if (string_seen)
12731 {
12732 int incr = row->reversed_p ? -1 : +1;
12733
12734 /* Need to find the glyph that came out of a string which is
12735 present at point. That glyph is somewhere between
12736 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12737 positioned between POS_BEFORE and POS_AFTER in the
12738 buffer. */
12739 struct glyph *stop = glyph_after;
12740 EMACS_INT pos = pos_before;
12741
12742 x = -1;
12743 for (glyph = glyph_before + incr;
12744 row->reversed_p ? glyph > stop : glyph < stop; )
12745 {
12746
12747 /* Any glyphs that come from the buffer are here because
12748 of bidi reordering. Skip them, and only pay
12749 attention to glyphs that came from some string. */
12750 if (STRINGP (glyph->object))
12751 {
12752 Lisp_Object str;
12753 EMACS_INT tem;
12754
12755 str = glyph->object;
12756 tem = string_buffer_position_lim (str, pos, pos_after, 0);
12757 if (tem == 0 /* from overlay */
12758 || pos <= tem)
12759 {
12760 /* If the string from which this glyph came is
12761 found in the buffer at point, then we've
12762 found the glyph we've been looking for. If
12763 it comes from an overlay (tem == 0), and it
12764 has the `cursor' property on one of its
12765 glyphs, record that glyph as a candidate for
12766 displaying the cursor. (As in the
12767 unidirectional version, we will display the
12768 cursor on the last candidate we find.) */
12769 if (tem == 0 || tem == pt_old)
12770 {
12771 /* The glyphs from this string could have
12772 been reordered. Find the one with the
12773 smallest string position. Or there could
12774 be a character in the string with the
12775 `cursor' property, which means display
12776 cursor on that character's glyph. */
12777 EMACS_INT strpos = glyph->charpos;
12778
12779 if (tem)
12780 cursor = glyph;
12781 for ( ;
12782 (row->reversed_p ? glyph > stop : glyph < stop)
12783 && EQ (glyph->object, str);
12784 glyph += incr)
12785 {
12786 Lisp_Object cprop;
12787 EMACS_INT gpos = glyph->charpos;
12788
12789 cprop = Fget_char_property (make_number (gpos),
12790 Qcursor,
12791 glyph->object);
12792 if (!NILP (cprop))
12793 {
12794 cursor = glyph;
12795 break;
12796 }
12797 if (tem && glyph->charpos < strpos)
12798 {
12799 strpos = glyph->charpos;
12800 cursor = glyph;
12801 }
12802 }
12803
12804 if (tem == pt_old)
12805 goto compute_x;
12806 }
12807 if (tem)
12808 pos = tem + 1; /* don't find previous instances */
12809 }
12810 /* This string is not what we want; skip all of the
12811 glyphs that came from it. */
12812 while ((row->reversed_p ? glyph > stop : glyph < stop)
12813 && EQ (glyph->object, str))
12814 glyph += incr;
12815 }
12816 else
12817 glyph += incr;
12818 }
12819
12820 /* If we reached the end of the line, and END was from a string,
12821 the cursor is not on this line. */
12822 if (cursor == NULL
12823 && (row->reversed_p ? glyph <= end : glyph >= end)
12824 && STRINGP (end->object)
12825 && row->continued_p)
12826 return 0;
12827 }
12828 }
12829
12830 compute_x:
12831 if (cursor != NULL)
12832 glyph = cursor;
12833 if (x < 0)
12834 {
12835 struct glyph *g;
12836
12837 /* Need to compute x that corresponds to GLYPH. */
12838 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12839 {
12840 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12841 abort ();
12842 x += g->pixel_width;
12843 }
12844 }
12845
12846 /* ROW could be part of a continued line, which, under bidi
12847 reordering, might have other rows whose start and end charpos
12848 occlude point. Only set w->cursor if we found a better
12849 approximation to the cursor position than we have from previously
12850 examined candidate rows belonging to the same continued line. */
12851 if (/* we already have a candidate row */
12852 w->cursor.vpos >= 0
12853 /* that candidate is not the row we are processing */
12854 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12855 /* the row we are processing is part of a continued line */
12856 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
12857 /* Make sure cursor.vpos specifies a row whose start and end
12858 charpos occlude point. This is because some callers of this
12859 function leave cursor.vpos at the row where the cursor was
12860 displayed during the last redisplay cycle. */
12861 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12862 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12863 {
12864 struct glyph *g1 =
12865 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12866
12867 /* Don't consider glyphs that are outside TEXT_AREA. */
12868 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
12869 return 0;
12870 /* Keep the candidate whose buffer position is the closest to
12871 point. */
12872 if (/* previous candidate is a glyph in TEXT_AREA of that row */
12873 w->cursor.hpos >= 0
12874 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
12875 && BUFFERP (g1->object)
12876 && (g1->charpos == pt_old /* an exact match always wins */
12877 || (BUFFERP (glyph->object)
12878 && eabs (g1->charpos - pt_old)
12879 < eabs (glyph->charpos - pt_old))))
12880 return 0;
12881 /* If this candidate gives an exact match, use that. */
12882 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12883 /* Otherwise, keep the candidate that comes from a row
12884 spanning less buffer positions. This may win when one or
12885 both candidate positions are on glyphs that came from
12886 display strings, for which we cannot compare buffer
12887 positions. */
12888 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12889 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12890 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12891 return 0;
12892 }
12893 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12894 w->cursor.x = x;
12895 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12896 w->cursor.y = row->y + dy;
12897
12898 if (w == XWINDOW (selected_window))
12899 {
12900 if (!row->continued_p
12901 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12902 && row->x == 0)
12903 {
12904 this_line_buffer = XBUFFER (w->buffer);
12905
12906 CHARPOS (this_line_start_pos)
12907 = MATRIX_ROW_START_CHARPOS (row) + delta;
12908 BYTEPOS (this_line_start_pos)
12909 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12910
12911 CHARPOS (this_line_end_pos)
12912 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12913 BYTEPOS (this_line_end_pos)
12914 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12915
12916 this_line_y = w->cursor.y;
12917 this_line_pixel_height = row->height;
12918 this_line_vpos = w->cursor.vpos;
12919 this_line_start_x = row->x;
12920 }
12921 else
12922 CHARPOS (this_line_start_pos) = 0;
12923 }
12924
12925 return 1;
12926 }
12927
12928
12929 /* Run window scroll functions, if any, for WINDOW with new window
12930 start STARTP. Sets the window start of WINDOW to that position.
12931
12932 We assume that the window's buffer is really current. */
12933
12934 static INLINE struct text_pos
12935 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
12936 {
12937 struct window *w = XWINDOW (window);
12938 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12939
12940 if (current_buffer != XBUFFER (w->buffer))
12941 abort ();
12942
12943 if (!NILP (Vwindow_scroll_functions))
12944 {
12945 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12946 make_number (CHARPOS (startp)));
12947 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12948 /* In case the hook functions switch buffers. */
12949 if (current_buffer != XBUFFER (w->buffer))
12950 set_buffer_internal_1 (XBUFFER (w->buffer));
12951 }
12952
12953 return startp;
12954 }
12955
12956
12957 /* Make sure the line containing the cursor is fully visible.
12958 A value of 1 means there is nothing to be done.
12959 (Either the line is fully visible, or it cannot be made so,
12960 or we cannot tell.)
12961
12962 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12963 is higher than window.
12964
12965 A value of 0 means the caller should do scrolling
12966 as if point had gone off the screen. */
12967
12968 static int
12969 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
12970 {
12971 struct glyph_matrix *matrix;
12972 struct glyph_row *row;
12973 int window_height;
12974
12975 if (!make_cursor_line_fully_visible_p)
12976 return 1;
12977
12978 /* It's not always possible to find the cursor, e.g, when a window
12979 is full of overlay strings. Don't do anything in that case. */
12980 if (w->cursor.vpos < 0)
12981 return 1;
12982
12983 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12984 row = MATRIX_ROW (matrix, w->cursor.vpos);
12985
12986 /* If the cursor row is not partially visible, there's nothing to do. */
12987 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12988 return 1;
12989
12990 /* If the row the cursor is in is taller than the window's height,
12991 it's not clear what to do, so do nothing. */
12992 window_height = window_box_height (w);
12993 if (row->height >= window_height)
12994 {
12995 if (!force_p || MINI_WINDOW_P (w)
12996 || w->vscroll || w->cursor.vpos == 0)
12997 return 1;
12998 }
12999 return 0;
13000 }
13001
13002
13003 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13004 non-zero means only WINDOW is redisplayed in redisplay_internal.
13005 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13006 in redisplay_window to bring a partially visible line into view in
13007 the case that only the cursor has moved.
13008
13009 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13010 last screen line's vertical height extends past the end of the screen.
13011
13012 Value is
13013
13014 1 if scrolling succeeded
13015
13016 0 if scrolling didn't find point.
13017
13018 -1 if new fonts have been loaded so that we must interrupt
13019 redisplay, adjust glyph matrices, and try again. */
13020
13021 enum
13022 {
13023 SCROLLING_SUCCESS,
13024 SCROLLING_FAILED,
13025 SCROLLING_NEED_LARGER_MATRICES
13026 };
13027
13028 /* If scroll-conservatively is more than this, never recenter.
13029
13030 If you change this, don't forget to update the doc string of
13031 `scroll-conservatively' and the Emacs manual. */
13032 #define SCROLL_LIMIT 100
13033
13034 static int
13035 try_scrolling (Lisp_Object window, int just_this_one_p,
13036 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13037 int temp_scroll_step, int last_line_misfit)
13038 {
13039 struct window *w = XWINDOW (window);
13040 struct frame *f = XFRAME (w->frame);
13041 struct text_pos pos, startp;
13042 struct it it;
13043 int this_scroll_margin, scroll_max, rc, height;
13044 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13045 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13046 Lisp_Object aggressive;
13047 /* We will never try scrolling more than this number of lines. */
13048 int scroll_limit = SCROLL_LIMIT;
13049
13050 #if GLYPH_DEBUG
13051 debug_method_add (w, "try_scrolling");
13052 #endif
13053
13054 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13055
13056 /* Compute scroll margin height in pixels. We scroll when point is
13057 within this distance from the top or bottom of the window. */
13058 if (scroll_margin > 0)
13059 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13060 * FRAME_LINE_HEIGHT (f);
13061 else
13062 this_scroll_margin = 0;
13063
13064 /* Force arg_scroll_conservatively to have a reasonable value, to
13065 avoid scrolling too far away with slow move_it_* functions. Note
13066 that the user can supply scroll-conservatively equal to
13067 `most-positive-fixnum', which can be larger than INT_MAX. */
13068 if (arg_scroll_conservatively > scroll_limit)
13069 {
13070 arg_scroll_conservatively = scroll_limit + 1;
13071 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13072 }
13073 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13074 /* Compute how much we should try to scroll maximally to bring
13075 point into view. */
13076 scroll_max = (max (scroll_step,
13077 max (arg_scroll_conservatively, temp_scroll_step))
13078 * FRAME_LINE_HEIGHT (f));
13079 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13080 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13081 /* We're trying to scroll because of aggressive scrolling but no
13082 scroll_step is set. Choose an arbitrary one. */
13083 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13084 else
13085 scroll_max = 0;
13086
13087 too_near_end:
13088
13089 /* Decide whether to scroll down. */
13090 if (PT > CHARPOS (startp))
13091 {
13092 int scroll_margin_y;
13093
13094 /* Compute the pixel ypos of the scroll margin, then move it to
13095 either that ypos or PT, whichever comes first. */
13096 start_display (&it, w, startp);
13097 scroll_margin_y = it.last_visible_y - this_scroll_margin
13098 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13099 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13100 (MOVE_TO_POS | MOVE_TO_Y));
13101
13102 if (PT > CHARPOS (it.current.pos))
13103 {
13104 int y0 = line_bottom_y (&it);
13105 /* Compute how many pixels below window bottom to stop searching
13106 for PT. This avoids costly search for PT that is far away if
13107 the user limited scrolling by a small number of lines, but
13108 always finds PT if scroll_conservatively is set to a large
13109 number, such as most-positive-fixnum. */
13110 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13111 int y_to_move = it.last_visible_y + slack;
13112
13113 /* Compute the distance from the scroll margin to PT or to
13114 the scroll limit, whichever comes first. This should
13115 include the height of the cursor line, to make that line
13116 fully visible. */
13117 move_it_to (&it, PT, -1, y_to_move,
13118 -1, MOVE_TO_POS | MOVE_TO_Y);
13119 dy = line_bottom_y (&it) - y0;
13120
13121 if (dy > scroll_max)
13122 return SCROLLING_FAILED;
13123
13124 scroll_down_p = 1;
13125 }
13126 }
13127
13128 if (scroll_down_p)
13129 {
13130 /* Point is in or below the bottom scroll margin, so move the
13131 window start down. If scrolling conservatively, move it just
13132 enough down to make point visible. If scroll_step is set,
13133 move it down by scroll_step. */
13134 if (arg_scroll_conservatively)
13135 amount_to_scroll
13136 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13137 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13138 else if (scroll_step || temp_scroll_step)
13139 amount_to_scroll = scroll_max;
13140 else
13141 {
13142 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13143 height = WINDOW_BOX_TEXT_HEIGHT (w);
13144 if (NUMBERP (aggressive))
13145 {
13146 double float_amount = XFLOATINT (aggressive) * height;
13147 amount_to_scroll = float_amount;
13148 if (amount_to_scroll == 0 && float_amount > 0)
13149 amount_to_scroll = 1;
13150 /* Don't let point enter the scroll margin near top of
13151 the window. */
13152 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13153 amount_to_scroll = height - 2*this_scroll_margin + dy;
13154 }
13155 }
13156
13157 if (amount_to_scroll <= 0)
13158 return SCROLLING_FAILED;
13159
13160 start_display (&it, w, startp);
13161 if (arg_scroll_conservatively <= scroll_limit)
13162 move_it_vertically (&it, amount_to_scroll);
13163 else
13164 {
13165 /* Extra precision for users who set scroll-conservatively
13166 to a large number: make sure the amount we scroll
13167 the window start is never less than amount_to_scroll,
13168 which was computed as distance from window bottom to
13169 point. This matters when lines at window top and lines
13170 below window bottom have different height. */
13171 struct it it1 = it;
13172 /* We use a temporary it1 because line_bottom_y can modify
13173 its argument, if it moves one line down; see there. */
13174 int start_y = line_bottom_y (&it1);
13175
13176 do {
13177 move_it_by_lines (&it, 1);
13178 it1 = it;
13179 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13180 }
13181
13182 /* If STARTP is unchanged, move it down another screen line. */
13183 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13184 move_it_by_lines (&it, 1);
13185 startp = it.current.pos;
13186 }
13187 else
13188 {
13189 struct text_pos scroll_margin_pos = startp;
13190
13191 /* See if point is inside the scroll margin at the top of the
13192 window. */
13193 if (this_scroll_margin)
13194 {
13195 start_display (&it, w, startp);
13196 move_it_vertically (&it, this_scroll_margin);
13197 scroll_margin_pos = it.current.pos;
13198 }
13199
13200 if (PT < CHARPOS (scroll_margin_pos))
13201 {
13202 /* Point is in the scroll margin at the top of the window or
13203 above what is displayed in the window. */
13204 int y0, y_to_move;
13205
13206 /* Compute the vertical distance from PT to the scroll
13207 margin position. Move as far as scroll_max allows, or
13208 one screenful, or 10 screen lines, whichever is largest.
13209 Give up if distance is greater than scroll_max. */
13210 SET_TEXT_POS (pos, PT, PT_BYTE);
13211 start_display (&it, w, pos);
13212 y0 = it.current_y;
13213 y_to_move = max (it.last_visible_y,
13214 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13215 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13216 y_to_move, -1,
13217 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13218 dy = it.current_y - y0;
13219 if (dy > scroll_max)
13220 return SCROLLING_FAILED;
13221
13222 /* Compute new window start. */
13223 start_display (&it, w, startp);
13224
13225 if (arg_scroll_conservatively)
13226 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13227 max (scroll_step, temp_scroll_step));
13228 else if (scroll_step || temp_scroll_step)
13229 amount_to_scroll = scroll_max;
13230 else
13231 {
13232 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13233 height = WINDOW_BOX_TEXT_HEIGHT (w);
13234 if (NUMBERP (aggressive))
13235 {
13236 double float_amount = XFLOATINT (aggressive) * height;
13237 amount_to_scroll = float_amount;
13238 if (amount_to_scroll == 0 && float_amount > 0)
13239 amount_to_scroll = 1;
13240 amount_to_scroll -=
13241 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13242 /* Don't let point enter the scroll margin near
13243 bottom of the window. */
13244 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13245 amount_to_scroll = height - 2*this_scroll_margin + dy;
13246 }
13247 }
13248
13249 if (amount_to_scroll <= 0)
13250 return SCROLLING_FAILED;
13251
13252 move_it_vertically_backward (&it, amount_to_scroll);
13253 startp = it.current.pos;
13254 }
13255 }
13256
13257 /* Run window scroll functions. */
13258 startp = run_window_scroll_functions (window, startp);
13259
13260 /* Display the window. Give up if new fonts are loaded, or if point
13261 doesn't appear. */
13262 if (!try_window (window, startp, 0))
13263 rc = SCROLLING_NEED_LARGER_MATRICES;
13264 else if (w->cursor.vpos < 0)
13265 {
13266 clear_glyph_matrix (w->desired_matrix);
13267 rc = SCROLLING_FAILED;
13268 }
13269 else
13270 {
13271 /* Maybe forget recorded base line for line number display. */
13272 if (!just_this_one_p
13273 || current_buffer->clip_changed
13274 || BEG_UNCHANGED < CHARPOS (startp))
13275 w->base_line_number = Qnil;
13276
13277 /* If cursor ends up on a partially visible line,
13278 treat that as being off the bottom of the screen. */
13279 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13280 /* It's possible that the cursor is on the first line of the
13281 buffer, which is partially obscured due to a vscroll
13282 (Bug#7537). In that case, avoid looping forever . */
13283 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13284 {
13285 clear_glyph_matrix (w->desired_matrix);
13286 ++extra_scroll_margin_lines;
13287 goto too_near_end;
13288 }
13289 rc = SCROLLING_SUCCESS;
13290 }
13291
13292 return rc;
13293 }
13294
13295
13296 /* Compute a suitable window start for window W if display of W starts
13297 on a continuation line. Value is non-zero if a new window start
13298 was computed.
13299
13300 The new window start will be computed, based on W's width, starting
13301 from the start of the continued line. It is the start of the
13302 screen line with the minimum distance from the old start W->start. */
13303
13304 static int
13305 compute_window_start_on_continuation_line (struct window *w)
13306 {
13307 struct text_pos pos, start_pos;
13308 int window_start_changed_p = 0;
13309
13310 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13311
13312 /* If window start is on a continuation line... Window start may be
13313 < BEGV in case there's invisible text at the start of the
13314 buffer (M-x rmail, for example). */
13315 if (CHARPOS (start_pos) > BEGV
13316 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13317 {
13318 struct it it;
13319 struct glyph_row *row;
13320
13321 /* Handle the case that the window start is out of range. */
13322 if (CHARPOS (start_pos) < BEGV)
13323 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13324 else if (CHARPOS (start_pos) > ZV)
13325 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13326
13327 /* Find the start of the continued line. This should be fast
13328 because scan_buffer is fast (newline cache). */
13329 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13330 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13331 row, DEFAULT_FACE_ID);
13332 reseat_at_previous_visible_line_start (&it);
13333
13334 /* If the line start is "too far" away from the window start,
13335 say it takes too much time to compute a new window start. */
13336 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13337 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13338 {
13339 int min_distance, distance;
13340
13341 /* Move forward by display lines to find the new window
13342 start. If window width was enlarged, the new start can
13343 be expected to be > the old start. If window width was
13344 decreased, the new window start will be < the old start.
13345 So, we're looking for the display line start with the
13346 minimum distance from the old window start. */
13347 pos = it.current.pos;
13348 min_distance = INFINITY;
13349 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13350 distance < min_distance)
13351 {
13352 min_distance = distance;
13353 pos = it.current.pos;
13354 move_it_by_lines (&it, 1);
13355 }
13356
13357 /* Set the window start there. */
13358 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13359 window_start_changed_p = 1;
13360 }
13361 }
13362
13363 return window_start_changed_p;
13364 }
13365
13366
13367 /* Try cursor movement in case text has not changed in window WINDOW,
13368 with window start STARTP. Value is
13369
13370 CURSOR_MOVEMENT_SUCCESS if successful
13371
13372 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13373
13374 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13375 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13376 we want to scroll as if scroll-step were set to 1. See the code.
13377
13378 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13379 which case we have to abort this redisplay, and adjust matrices
13380 first. */
13381
13382 enum
13383 {
13384 CURSOR_MOVEMENT_SUCCESS,
13385 CURSOR_MOVEMENT_CANNOT_BE_USED,
13386 CURSOR_MOVEMENT_MUST_SCROLL,
13387 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13388 };
13389
13390 static int
13391 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13392 {
13393 struct window *w = XWINDOW (window);
13394 struct frame *f = XFRAME (w->frame);
13395 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13396
13397 #if GLYPH_DEBUG
13398 if (inhibit_try_cursor_movement)
13399 return rc;
13400 #endif
13401
13402 /* Handle case where text has not changed, only point, and it has
13403 not moved off the frame. */
13404 if (/* Point may be in this window. */
13405 PT >= CHARPOS (startp)
13406 /* Selective display hasn't changed. */
13407 && !current_buffer->clip_changed
13408 /* Function force-mode-line-update is used to force a thorough
13409 redisplay. It sets either windows_or_buffers_changed or
13410 update_mode_lines. So don't take a shortcut here for these
13411 cases. */
13412 && !update_mode_lines
13413 && !windows_or_buffers_changed
13414 && !cursor_type_changed
13415 /* Can't use this case if highlighting a region. When a
13416 region exists, cursor movement has to do more than just
13417 set the cursor. */
13418 && !(!NILP (Vtransient_mark_mode)
13419 && !NILP (BVAR (current_buffer, mark_active)))
13420 && NILP (w->region_showing)
13421 && NILP (Vshow_trailing_whitespace)
13422 /* Right after splitting windows, last_point may be nil. */
13423 && INTEGERP (w->last_point)
13424 /* This code is not used for mini-buffer for the sake of the case
13425 of redisplaying to replace an echo area message; since in
13426 that case the mini-buffer contents per se are usually
13427 unchanged. This code is of no real use in the mini-buffer
13428 since the handling of this_line_start_pos, etc., in redisplay
13429 handles the same cases. */
13430 && !EQ (window, minibuf_window)
13431 /* When splitting windows or for new windows, it happens that
13432 redisplay is called with a nil window_end_vpos or one being
13433 larger than the window. This should really be fixed in
13434 window.c. I don't have this on my list, now, so we do
13435 approximately the same as the old redisplay code. --gerd. */
13436 && INTEGERP (w->window_end_vpos)
13437 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13438 && (FRAME_WINDOW_P (f)
13439 || !overlay_arrow_in_current_buffer_p ()))
13440 {
13441 int this_scroll_margin, top_scroll_margin;
13442 struct glyph_row *row = NULL;
13443
13444 #if GLYPH_DEBUG
13445 debug_method_add (w, "cursor movement");
13446 #endif
13447
13448 /* Scroll if point within this distance from the top or bottom
13449 of the window. This is a pixel value. */
13450 if (scroll_margin > 0)
13451 {
13452 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13453 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13454 }
13455 else
13456 this_scroll_margin = 0;
13457
13458 top_scroll_margin = this_scroll_margin;
13459 if (WINDOW_WANTS_HEADER_LINE_P (w))
13460 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13461
13462 /* Start with the row the cursor was displayed during the last
13463 not paused redisplay. Give up if that row is not valid. */
13464 if (w->last_cursor.vpos < 0
13465 || w->last_cursor.vpos >= w->current_matrix->nrows)
13466 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13467 else
13468 {
13469 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13470 if (row->mode_line_p)
13471 ++row;
13472 if (!row->enabled_p)
13473 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13474 }
13475
13476 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13477 {
13478 int scroll_p = 0, must_scroll = 0;
13479 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13480
13481 if (PT > XFASTINT (w->last_point))
13482 {
13483 /* Point has moved forward. */
13484 while (MATRIX_ROW_END_CHARPOS (row) < PT
13485 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13486 {
13487 xassert (row->enabled_p);
13488 ++row;
13489 }
13490
13491 /* If the end position of a row equals the start
13492 position of the next row, and PT is at that position,
13493 we would rather display cursor in the next line. */
13494 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13495 && MATRIX_ROW_END_CHARPOS (row) == PT
13496 && row < w->current_matrix->rows
13497 + w->current_matrix->nrows - 1
13498 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13499 && !cursor_row_p (row))
13500 ++row;
13501
13502 /* If within the scroll margin, scroll. Note that
13503 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13504 the next line would be drawn, and that
13505 this_scroll_margin can be zero. */
13506 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13507 || PT > MATRIX_ROW_END_CHARPOS (row)
13508 /* Line is completely visible last line in window
13509 and PT is to be set in the next line. */
13510 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13511 && PT == MATRIX_ROW_END_CHARPOS (row)
13512 && !row->ends_at_zv_p
13513 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13514 scroll_p = 1;
13515 }
13516 else if (PT < XFASTINT (w->last_point))
13517 {
13518 /* Cursor has to be moved backward. Note that PT >=
13519 CHARPOS (startp) because of the outer if-statement. */
13520 while (!row->mode_line_p
13521 && (MATRIX_ROW_START_CHARPOS (row) > PT
13522 || (MATRIX_ROW_START_CHARPOS (row) == PT
13523 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13524 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13525 row > w->current_matrix->rows
13526 && (row-1)->ends_in_newline_from_string_p))))
13527 && (row->y > top_scroll_margin
13528 || CHARPOS (startp) == BEGV))
13529 {
13530 xassert (row->enabled_p);
13531 --row;
13532 }
13533
13534 /* Consider the following case: Window starts at BEGV,
13535 there is invisible, intangible text at BEGV, so that
13536 display starts at some point START > BEGV. It can
13537 happen that we are called with PT somewhere between
13538 BEGV and START. Try to handle that case. */
13539 if (row < w->current_matrix->rows
13540 || row->mode_line_p)
13541 {
13542 row = w->current_matrix->rows;
13543 if (row->mode_line_p)
13544 ++row;
13545 }
13546
13547 /* Due to newlines in overlay strings, we may have to
13548 skip forward over overlay strings. */
13549 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13550 && MATRIX_ROW_END_CHARPOS (row) == PT
13551 && !cursor_row_p (row))
13552 ++row;
13553
13554 /* If within the scroll margin, scroll. */
13555 if (row->y < top_scroll_margin
13556 && CHARPOS (startp) != BEGV)
13557 scroll_p = 1;
13558 }
13559 else
13560 {
13561 /* Cursor did not move. So don't scroll even if cursor line
13562 is partially visible, as it was so before. */
13563 rc = CURSOR_MOVEMENT_SUCCESS;
13564 }
13565
13566 if (PT < MATRIX_ROW_START_CHARPOS (row)
13567 || PT > MATRIX_ROW_END_CHARPOS (row))
13568 {
13569 /* if PT is not in the glyph row, give up. */
13570 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13571 must_scroll = 1;
13572 }
13573 else if (rc != CURSOR_MOVEMENT_SUCCESS
13574 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
13575 {
13576 /* If rows are bidi-reordered and point moved, back up
13577 until we find a row that does not belong to a
13578 continuation line. This is because we must consider
13579 all rows of a continued line as candidates for the
13580 new cursor positioning, since row start and end
13581 positions change non-linearly with vertical position
13582 in such rows. */
13583 /* FIXME: Revisit this when glyph ``spilling'' in
13584 continuation lines' rows is implemented for
13585 bidi-reordered rows. */
13586 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13587 {
13588 xassert (row->enabled_p);
13589 --row;
13590 /* If we hit the beginning of the displayed portion
13591 without finding the first row of a continued
13592 line, give up. */
13593 if (row <= w->current_matrix->rows)
13594 {
13595 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13596 break;
13597 }
13598
13599 }
13600 }
13601 if (must_scroll)
13602 ;
13603 else if (rc != CURSOR_MOVEMENT_SUCCESS
13604 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13605 && make_cursor_line_fully_visible_p)
13606 {
13607 if (PT == MATRIX_ROW_END_CHARPOS (row)
13608 && !row->ends_at_zv_p
13609 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13610 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13611 else if (row->height > window_box_height (w))
13612 {
13613 /* If we end up in a partially visible line, let's
13614 make it fully visible, except when it's taller
13615 than the window, in which case we can't do much
13616 about it. */
13617 *scroll_step = 1;
13618 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13619 }
13620 else
13621 {
13622 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13623 if (!cursor_row_fully_visible_p (w, 0, 1))
13624 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13625 else
13626 rc = CURSOR_MOVEMENT_SUCCESS;
13627 }
13628 }
13629 else if (scroll_p)
13630 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13631 else if (rc != CURSOR_MOVEMENT_SUCCESS
13632 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
13633 {
13634 /* With bidi-reordered rows, there could be more than
13635 one candidate row whose start and end positions
13636 occlude point. We need to let set_cursor_from_row
13637 find the best candidate. */
13638 /* FIXME: Revisit this when glyph ``spilling'' in
13639 continuation lines' rows is implemented for
13640 bidi-reordered rows. */
13641 int rv = 0;
13642
13643 do
13644 {
13645 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13646 && PT <= MATRIX_ROW_END_CHARPOS (row)
13647 && cursor_row_p (row))
13648 rv |= set_cursor_from_row (w, row, w->current_matrix,
13649 0, 0, 0, 0);
13650 /* As soon as we've found the first suitable row
13651 whose ends_at_zv_p flag is set, we are done. */
13652 if (rv
13653 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13654 {
13655 rc = CURSOR_MOVEMENT_SUCCESS;
13656 break;
13657 }
13658 ++row;
13659 }
13660 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13661 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13662 || (MATRIX_ROW_START_CHARPOS (row) == PT
13663 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13664 /* If we didn't find any candidate rows, or exited the
13665 loop before all the candidates were examined, signal
13666 to the caller that this method failed. */
13667 if (rc != CURSOR_MOVEMENT_SUCCESS
13668 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13669 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13670 else if (rv)
13671 rc = CURSOR_MOVEMENT_SUCCESS;
13672 }
13673 else
13674 {
13675 do
13676 {
13677 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13678 {
13679 rc = CURSOR_MOVEMENT_SUCCESS;
13680 break;
13681 }
13682 ++row;
13683 }
13684 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13685 && MATRIX_ROW_START_CHARPOS (row) == PT
13686 && cursor_row_p (row));
13687 }
13688 }
13689 }
13690
13691 return rc;
13692 }
13693
13694 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
13695 static
13696 #endif
13697 void
13698 set_vertical_scroll_bar (struct window *w)
13699 {
13700 EMACS_INT start, end, whole;
13701
13702 /* Calculate the start and end positions for the current window.
13703 At some point, it would be nice to choose between scrollbars
13704 which reflect the whole buffer size, with special markers
13705 indicating narrowing, and scrollbars which reflect only the
13706 visible region.
13707
13708 Note that mini-buffers sometimes aren't displaying any text. */
13709 if (!MINI_WINDOW_P (w)
13710 || (w == XWINDOW (minibuf_window)
13711 && NILP (echo_area_buffer[0])))
13712 {
13713 struct buffer *buf = XBUFFER (w->buffer);
13714 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13715 start = marker_position (w->start) - BUF_BEGV (buf);
13716 /* I don't think this is guaranteed to be right. For the
13717 moment, we'll pretend it is. */
13718 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13719
13720 if (end < start)
13721 end = start;
13722 if (whole < (end - start))
13723 whole = end - start;
13724 }
13725 else
13726 start = end = whole = 0;
13727
13728 /* Indicate what this scroll bar ought to be displaying now. */
13729 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13730 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13731 (w, end - start, whole, start);
13732 }
13733
13734
13735 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13736 selected_window is redisplayed.
13737
13738 We can return without actually redisplaying the window if
13739 fonts_changed_p is nonzero. In that case, redisplay_internal will
13740 retry. */
13741
13742 static void
13743 redisplay_window (Lisp_Object window, int just_this_one_p)
13744 {
13745 struct window *w = XWINDOW (window);
13746 struct frame *f = XFRAME (w->frame);
13747 struct buffer *buffer = XBUFFER (w->buffer);
13748 struct buffer *old = current_buffer;
13749 struct text_pos lpoint, opoint, startp;
13750 int update_mode_line;
13751 int tem;
13752 struct it it;
13753 /* Record it now because it's overwritten. */
13754 int current_matrix_up_to_date_p = 0;
13755 int used_current_matrix_p = 0;
13756 /* This is less strict than current_matrix_up_to_date_p.
13757 It indictes that the buffer contents and narrowing are unchanged. */
13758 int buffer_unchanged_p = 0;
13759 int temp_scroll_step = 0;
13760 int count = SPECPDL_INDEX ();
13761 int rc;
13762 int centering_position = -1;
13763 int last_line_misfit = 0;
13764 EMACS_INT beg_unchanged, end_unchanged;
13765
13766 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13767 opoint = lpoint;
13768
13769 /* W must be a leaf window here. */
13770 xassert (!NILP (w->buffer));
13771 #if GLYPH_DEBUG
13772 *w->desired_matrix->method = 0;
13773 #endif
13774
13775 restart:
13776 reconsider_clip_changes (w, buffer);
13777
13778 /* Has the mode line to be updated? */
13779 update_mode_line = (!NILP (w->update_mode_line)
13780 || update_mode_lines
13781 || buffer->clip_changed
13782 || buffer->prevent_redisplay_optimizations_p);
13783
13784 if (MINI_WINDOW_P (w))
13785 {
13786 if (w == XWINDOW (echo_area_window)
13787 && !NILP (echo_area_buffer[0]))
13788 {
13789 if (update_mode_line)
13790 /* We may have to update a tty frame's menu bar or a
13791 tool-bar. Example `M-x C-h C-h C-g'. */
13792 goto finish_menu_bars;
13793 else
13794 /* We've already displayed the echo area glyphs in this window. */
13795 goto finish_scroll_bars;
13796 }
13797 else if ((w != XWINDOW (minibuf_window)
13798 || minibuf_level == 0)
13799 /* When buffer is nonempty, redisplay window normally. */
13800 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13801 /* Quail displays non-mini buffers in minibuffer window.
13802 In that case, redisplay the window normally. */
13803 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13804 {
13805 /* W is a mini-buffer window, but it's not active, so clear
13806 it. */
13807 int yb = window_text_bottom_y (w);
13808 struct glyph_row *row;
13809 int y;
13810
13811 for (y = 0, row = w->desired_matrix->rows;
13812 y < yb;
13813 y += row->height, ++row)
13814 blank_row (w, row, y);
13815 goto finish_scroll_bars;
13816 }
13817
13818 clear_glyph_matrix (w->desired_matrix);
13819 }
13820
13821 /* Otherwise set up data on this window; select its buffer and point
13822 value. */
13823 /* Really select the buffer, for the sake of buffer-local
13824 variables. */
13825 set_buffer_internal_1 (XBUFFER (w->buffer));
13826
13827 current_matrix_up_to_date_p
13828 = (!NILP (w->window_end_valid)
13829 && !current_buffer->clip_changed
13830 && !current_buffer->prevent_redisplay_optimizations_p
13831 && XFASTINT (w->last_modified) >= MODIFF
13832 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13833
13834 /* Run the window-bottom-change-functions
13835 if it is possible that the text on the screen has changed
13836 (either due to modification of the text, or any other reason). */
13837 if (!current_matrix_up_to_date_p
13838 && !NILP (Vwindow_text_change_functions))
13839 {
13840 safe_run_hooks (Qwindow_text_change_functions);
13841 goto restart;
13842 }
13843
13844 beg_unchanged = BEG_UNCHANGED;
13845 end_unchanged = END_UNCHANGED;
13846
13847 SET_TEXT_POS (opoint, PT, PT_BYTE);
13848
13849 specbind (Qinhibit_point_motion_hooks, Qt);
13850
13851 buffer_unchanged_p
13852 = (!NILP (w->window_end_valid)
13853 && !current_buffer->clip_changed
13854 && XFASTINT (w->last_modified) >= MODIFF
13855 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13856
13857 /* When windows_or_buffers_changed is non-zero, we can't rely on
13858 the window end being valid, so set it to nil there. */
13859 if (windows_or_buffers_changed)
13860 {
13861 /* If window starts on a continuation line, maybe adjust the
13862 window start in case the window's width changed. */
13863 if (XMARKER (w->start)->buffer == current_buffer)
13864 compute_window_start_on_continuation_line (w);
13865
13866 w->window_end_valid = Qnil;
13867 }
13868
13869 /* Some sanity checks. */
13870 CHECK_WINDOW_END (w);
13871 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13872 abort ();
13873 if (BYTEPOS (opoint) < CHARPOS (opoint))
13874 abort ();
13875
13876 /* If %c is in mode line, update it if needed. */
13877 if (!NILP (w->column_number_displayed)
13878 /* This alternative quickly identifies a common case
13879 where no change is needed. */
13880 && !(PT == XFASTINT (w->last_point)
13881 && XFASTINT (w->last_modified) >= MODIFF
13882 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13883 && (XFASTINT (w->column_number_displayed) != current_column ()))
13884 update_mode_line = 1;
13885
13886 /* Count number of windows showing the selected buffer. An indirect
13887 buffer counts as its base buffer. */
13888 if (!just_this_one_p)
13889 {
13890 struct buffer *current_base, *window_base;
13891 current_base = current_buffer;
13892 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13893 if (current_base->base_buffer)
13894 current_base = current_base->base_buffer;
13895 if (window_base->base_buffer)
13896 window_base = window_base->base_buffer;
13897 if (current_base == window_base)
13898 buffer_shared++;
13899 }
13900
13901 /* Point refers normally to the selected window. For any other
13902 window, set up appropriate value. */
13903 if (!EQ (window, selected_window))
13904 {
13905 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
13906 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
13907 if (new_pt < BEGV)
13908 {
13909 new_pt = BEGV;
13910 new_pt_byte = BEGV_BYTE;
13911 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13912 }
13913 else if (new_pt > (ZV - 1))
13914 {
13915 new_pt = ZV;
13916 new_pt_byte = ZV_BYTE;
13917 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13918 }
13919
13920 /* We don't use SET_PT so that the point-motion hooks don't run. */
13921 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13922 }
13923
13924 /* If any of the character widths specified in the display table
13925 have changed, invalidate the width run cache. It's true that
13926 this may be a bit late to catch such changes, but the rest of
13927 redisplay goes (non-fatally) haywire when the display table is
13928 changed, so why should we worry about doing any better? */
13929 if (current_buffer->width_run_cache)
13930 {
13931 struct Lisp_Char_Table *disptab = buffer_display_table ();
13932
13933 if (! disptab_matches_widthtab (disptab,
13934 XVECTOR (BVAR (current_buffer, width_table))))
13935 {
13936 invalidate_region_cache (current_buffer,
13937 current_buffer->width_run_cache,
13938 BEG, Z);
13939 recompute_width_table (current_buffer, disptab);
13940 }
13941 }
13942
13943 /* If window-start is screwed up, choose a new one. */
13944 if (XMARKER (w->start)->buffer != current_buffer)
13945 goto recenter;
13946
13947 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13948
13949 /* If someone specified a new starting point but did not insist,
13950 check whether it can be used. */
13951 if (!NILP (w->optional_new_start)
13952 && CHARPOS (startp) >= BEGV
13953 && CHARPOS (startp) <= ZV)
13954 {
13955 w->optional_new_start = Qnil;
13956 start_display (&it, w, startp);
13957 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13958 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13959 if (IT_CHARPOS (it) == PT)
13960 w->force_start = Qt;
13961 /* IT may overshoot PT if text at PT is invisible. */
13962 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13963 w->force_start = Qt;
13964 }
13965
13966 force_start:
13967
13968 /* Handle case where place to start displaying has been specified,
13969 unless the specified location is outside the accessible range. */
13970 if (!NILP (w->force_start)
13971 || w->frozen_window_start_p)
13972 {
13973 /* We set this later on if we have to adjust point. */
13974 int new_vpos = -1;
13975
13976 w->force_start = Qnil;
13977 w->vscroll = 0;
13978 w->window_end_valid = Qnil;
13979
13980 /* Forget any recorded base line for line number display. */
13981 if (!buffer_unchanged_p)
13982 w->base_line_number = Qnil;
13983
13984 /* Redisplay the mode line. Select the buffer properly for that.
13985 Also, run the hook window-scroll-functions
13986 because we have scrolled. */
13987 /* Note, we do this after clearing force_start because
13988 if there's an error, it is better to forget about force_start
13989 than to get into an infinite loop calling the hook functions
13990 and having them get more errors. */
13991 if (!update_mode_line
13992 || ! NILP (Vwindow_scroll_functions))
13993 {
13994 update_mode_line = 1;
13995 w->update_mode_line = Qt;
13996 startp = run_window_scroll_functions (window, startp);
13997 }
13998
13999 w->last_modified = make_number (0);
14000 w->last_overlay_modified = make_number (0);
14001 if (CHARPOS (startp) < BEGV)
14002 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14003 else if (CHARPOS (startp) > ZV)
14004 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14005
14006 /* Redisplay, then check if cursor has been set during the
14007 redisplay. Give up if new fonts were loaded. */
14008 /* We used to issue a CHECK_MARGINS argument to try_window here,
14009 but this causes scrolling to fail when point begins inside
14010 the scroll margin (bug#148) -- cyd */
14011 if (!try_window (window, startp, 0))
14012 {
14013 w->force_start = Qt;
14014 clear_glyph_matrix (w->desired_matrix);
14015 goto need_larger_matrices;
14016 }
14017
14018 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14019 {
14020 /* If point does not appear, try to move point so it does
14021 appear. The desired matrix has been built above, so we
14022 can use it here. */
14023 new_vpos = window_box_height (w) / 2;
14024 }
14025
14026 if (!cursor_row_fully_visible_p (w, 0, 0))
14027 {
14028 /* Point does appear, but on a line partly visible at end of window.
14029 Move it back to a fully-visible line. */
14030 new_vpos = window_box_height (w);
14031 }
14032
14033 /* If we need to move point for either of the above reasons,
14034 now actually do it. */
14035 if (new_vpos >= 0)
14036 {
14037 struct glyph_row *row;
14038
14039 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14040 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14041 ++row;
14042
14043 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14044 MATRIX_ROW_START_BYTEPOS (row));
14045
14046 if (w != XWINDOW (selected_window))
14047 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14048 else if (current_buffer == old)
14049 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14050
14051 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14052
14053 /* If we are highlighting the region, then we just changed
14054 the region, so redisplay to show it. */
14055 if (!NILP (Vtransient_mark_mode)
14056 && !NILP (BVAR (current_buffer, mark_active)))
14057 {
14058 clear_glyph_matrix (w->desired_matrix);
14059 if (!try_window (window, startp, 0))
14060 goto need_larger_matrices;
14061 }
14062 }
14063
14064 #if GLYPH_DEBUG
14065 debug_method_add (w, "forced window start");
14066 #endif
14067 goto done;
14068 }
14069
14070 /* Handle case where text has not changed, only point, and it has
14071 not moved off the frame, and we are not retrying after hscroll.
14072 (current_matrix_up_to_date_p is nonzero when retrying.) */
14073 if (current_matrix_up_to_date_p
14074 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14075 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14076 {
14077 switch (rc)
14078 {
14079 case CURSOR_MOVEMENT_SUCCESS:
14080 used_current_matrix_p = 1;
14081 goto done;
14082
14083 case CURSOR_MOVEMENT_MUST_SCROLL:
14084 goto try_to_scroll;
14085
14086 default:
14087 abort ();
14088 }
14089 }
14090 /* If current starting point was originally the beginning of a line
14091 but no longer is, find a new starting point. */
14092 else if (!NILP (w->start_at_line_beg)
14093 && !(CHARPOS (startp) <= BEGV
14094 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14095 {
14096 #if GLYPH_DEBUG
14097 debug_method_add (w, "recenter 1");
14098 #endif
14099 goto recenter;
14100 }
14101
14102 /* Try scrolling with try_window_id. Value is > 0 if update has
14103 been done, it is -1 if we know that the same window start will
14104 not work. It is 0 if unsuccessful for some other reason. */
14105 else if ((tem = try_window_id (w)) != 0)
14106 {
14107 #if GLYPH_DEBUG
14108 debug_method_add (w, "try_window_id %d", tem);
14109 #endif
14110
14111 if (fonts_changed_p)
14112 goto need_larger_matrices;
14113 if (tem > 0)
14114 goto done;
14115
14116 /* Otherwise try_window_id has returned -1 which means that we
14117 don't want the alternative below this comment to execute. */
14118 }
14119 else if (CHARPOS (startp) >= BEGV
14120 && CHARPOS (startp) <= ZV
14121 && PT >= CHARPOS (startp)
14122 && (CHARPOS (startp) < ZV
14123 /* Avoid starting at end of buffer. */
14124 || CHARPOS (startp) == BEGV
14125 || (XFASTINT (w->last_modified) >= MODIFF
14126 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14127 {
14128
14129 /* If first window line is a continuation line, and window start
14130 is inside the modified region, but the first change is before
14131 current window start, we must select a new window start.
14132
14133 However, if this is the result of a down-mouse event (e.g. by
14134 extending the mouse-drag-overlay), we don't want to select a
14135 new window start, since that would change the position under
14136 the mouse, resulting in an unwanted mouse-movement rather
14137 than a simple mouse-click. */
14138 if (NILP (w->start_at_line_beg)
14139 && NILP (do_mouse_tracking)
14140 && CHARPOS (startp) > BEGV
14141 && CHARPOS (startp) > BEG + beg_unchanged
14142 && CHARPOS (startp) <= Z - end_unchanged
14143 /* Even if w->start_at_line_beg is nil, a new window may
14144 start at a line_beg, since that's how set_buffer_window
14145 sets it. So, we need to check the return value of
14146 compute_window_start_on_continuation_line. (See also
14147 bug#197). */
14148 && XMARKER (w->start)->buffer == current_buffer
14149 && compute_window_start_on_continuation_line (w))
14150 {
14151 w->force_start = Qt;
14152 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14153 goto force_start;
14154 }
14155
14156 #if GLYPH_DEBUG
14157 debug_method_add (w, "same window start");
14158 #endif
14159
14160 /* Try to redisplay starting at same place as before.
14161 If point has not moved off frame, accept the results. */
14162 if (!current_matrix_up_to_date_p
14163 /* Don't use try_window_reusing_current_matrix in this case
14164 because a window scroll function can have changed the
14165 buffer. */
14166 || !NILP (Vwindow_scroll_functions)
14167 || MINI_WINDOW_P (w)
14168 || !(used_current_matrix_p
14169 = try_window_reusing_current_matrix (w)))
14170 {
14171 IF_DEBUG (debug_method_add (w, "1"));
14172 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14173 /* -1 means we need to scroll.
14174 0 means we need new matrices, but fonts_changed_p
14175 is set in that case, so we will detect it below. */
14176 goto try_to_scroll;
14177 }
14178
14179 if (fonts_changed_p)
14180 goto need_larger_matrices;
14181
14182 if (w->cursor.vpos >= 0)
14183 {
14184 if (!just_this_one_p
14185 || current_buffer->clip_changed
14186 || BEG_UNCHANGED < CHARPOS (startp))
14187 /* Forget any recorded base line for line number display. */
14188 w->base_line_number = Qnil;
14189
14190 if (!cursor_row_fully_visible_p (w, 1, 0))
14191 {
14192 clear_glyph_matrix (w->desired_matrix);
14193 last_line_misfit = 1;
14194 }
14195 /* Drop through and scroll. */
14196 else
14197 goto done;
14198 }
14199 else
14200 clear_glyph_matrix (w->desired_matrix);
14201 }
14202
14203 try_to_scroll:
14204
14205 w->last_modified = make_number (0);
14206 w->last_overlay_modified = make_number (0);
14207
14208 /* Redisplay the mode line. Select the buffer properly for that. */
14209 if (!update_mode_line)
14210 {
14211 update_mode_line = 1;
14212 w->update_mode_line = Qt;
14213 }
14214
14215 /* Try to scroll by specified few lines. */
14216 if ((scroll_conservatively
14217 || emacs_scroll_step
14218 || temp_scroll_step
14219 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14220 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14221 && CHARPOS (startp) >= BEGV
14222 && CHARPOS (startp) <= ZV)
14223 {
14224 /* The function returns -1 if new fonts were loaded, 1 if
14225 successful, 0 if not successful. */
14226 int ss = try_scrolling (window, just_this_one_p,
14227 scroll_conservatively,
14228 emacs_scroll_step,
14229 temp_scroll_step, last_line_misfit);
14230 switch (ss)
14231 {
14232 case SCROLLING_SUCCESS:
14233 goto done;
14234
14235 case SCROLLING_NEED_LARGER_MATRICES:
14236 goto need_larger_matrices;
14237
14238 case SCROLLING_FAILED:
14239 break;
14240
14241 default:
14242 abort ();
14243 }
14244 }
14245
14246 /* Finally, just choose a place to start which positions point
14247 according to user preferences. */
14248
14249 recenter:
14250
14251 #if GLYPH_DEBUG
14252 debug_method_add (w, "recenter");
14253 #endif
14254
14255 /* w->vscroll = 0; */
14256
14257 /* Forget any previously recorded base line for line number display. */
14258 if (!buffer_unchanged_p)
14259 w->base_line_number = Qnil;
14260
14261 /* Determine the window start relative to point. */
14262 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14263 it.current_y = it.last_visible_y;
14264 if (centering_position < 0)
14265 {
14266 int margin =
14267 scroll_margin > 0
14268 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14269 : 0;
14270 EMACS_INT margin_pos = CHARPOS (startp);
14271 int scrolling_up;
14272 Lisp_Object aggressive;
14273
14274 /* If there is a scroll margin at the top of the window, find
14275 its character position. */
14276 if (margin
14277 /* Cannot call start_display if startp is not in the
14278 accessible region of the buffer. This can happen when we
14279 have just switched to a different buffer and/or changed
14280 its restriction. In that case, startp is initialized to
14281 the character position 1 (BEG) because we did not yet
14282 have chance to display the buffer even once. */
14283 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14284 {
14285 struct it it1;
14286
14287 start_display (&it1, w, startp);
14288 move_it_vertically (&it1, margin);
14289 margin_pos = IT_CHARPOS (it1);
14290 }
14291 scrolling_up = PT > margin_pos;
14292 aggressive =
14293 scrolling_up
14294 ? BVAR (current_buffer, scroll_up_aggressively)
14295 : BVAR (current_buffer, scroll_down_aggressively);
14296
14297 if (!MINI_WINDOW_P (w)
14298 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
14299 {
14300 int pt_offset = 0;
14301
14302 /* Setting scroll-conservatively overrides
14303 scroll-*-aggressively. */
14304 if (!scroll_conservatively && NUMBERP (aggressive))
14305 {
14306 double float_amount = XFLOATINT (aggressive);
14307
14308 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14309 if (pt_offset == 0 && float_amount > 0)
14310 pt_offset = 1;
14311 if (pt_offset)
14312 margin -= 1;
14313 }
14314 /* Compute how much to move the window start backward from
14315 point so that point will be displayed where the user
14316 wants it. */
14317 if (scrolling_up)
14318 {
14319 centering_position = it.last_visible_y;
14320 if (pt_offset)
14321 centering_position -= pt_offset;
14322 centering_position -=
14323 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
14324 /* Don't let point enter the scroll margin near top of
14325 the window. */
14326 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
14327 centering_position = margin * FRAME_LINE_HEIGHT (f);
14328 }
14329 else
14330 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
14331 }
14332 else
14333 /* Set the window start half the height of the window backward
14334 from point. */
14335 centering_position = window_box_height (w) / 2;
14336 }
14337 move_it_vertically_backward (&it, centering_position);
14338
14339 xassert (IT_CHARPOS (it) >= BEGV);
14340
14341 /* The function move_it_vertically_backward may move over more
14342 than the specified y-distance. If it->w is small, e.g. a
14343 mini-buffer window, we may end up in front of the window's
14344 display area. Start displaying at the start of the line
14345 containing PT in this case. */
14346 if (it.current_y <= 0)
14347 {
14348 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14349 move_it_vertically_backward (&it, 0);
14350 it.current_y = 0;
14351 }
14352
14353 it.current_x = it.hpos = 0;
14354
14355 /* Set the window start position here explicitly, to avoid an
14356 infinite loop in case the functions in window-scroll-functions
14357 get errors. */
14358 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14359
14360 /* Run scroll hooks. */
14361 startp = run_window_scroll_functions (window, it.current.pos);
14362
14363 /* Redisplay the window. */
14364 if (!current_matrix_up_to_date_p
14365 || windows_or_buffers_changed
14366 || cursor_type_changed
14367 /* Don't use try_window_reusing_current_matrix in this case
14368 because it can have changed the buffer. */
14369 || !NILP (Vwindow_scroll_functions)
14370 || !just_this_one_p
14371 || MINI_WINDOW_P (w)
14372 || !(used_current_matrix_p
14373 = try_window_reusing_current_matrix (w)))
14374 try_window (window, startp, 0);
14375
14376 /* If new fonts have been loaded (due to fontsets), give up. We
14377 have to start a new redisplay since we need to re-adjust glyph
14378 matrices. */
14379 if (fonts_changed_p)
14380 goto need_larger_matrices;
14381
14382 /* If cursor did not appear assume that the middle of the window is
14383 in the first line of the window. Do it again with the next line.
14384 (Imagine a window of height 100, displaying two lines of height
14385 60. Moving back 50 from it->last_visible_y will end in the first
14386 line.) */
14387 if (w->cursor.vpos < 0)
14388 {
14389 if (!NILP (w->window_end_valid)
14390 && PT >= Z - XFASTINT (w->window_end_pos))
14391 {
14392 clear_glyph_matrix (w->desired_matrix);
14393 move_it_by_lines (&it, 1);
14394 try_window (window, it.current.pos, 0);
14395 }
14396 else if (PT < IT_CHARPOS (it))
14397 {
14398 clear_glyph_matrix (w->desired_matrix);
14399 move_it_by_lines (&it, -1);
14400 try_window (window, it.current.pos, 0);
14401 }
14402 else
14403 {
14404 /* Not much we can do about it. */
14405 }
14406 }
14407
14408 /* Consider the following case: Window starts at BEGV, there is
14409 invisible, intangible text at BEGV, so that display starts at
14410 some point START > BEGV. It can happen that we are called with
14411 PT somewhere between BEGV and START. Try to handle that case. */
14412 if (w->cursor.vpos < 0)
14413 {
14414 struct glyph_row *row = w->current_matrix->rows;
14415 if (row->mode_line_p)
14416 ++row;
14417 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14418 }
14419
14420 if (!cursor_row_fully_visible_p (w, 0, 0))
14421 {
14422 /* If vscroll is enabled, disable it and try again. */
14423 if (w->vscroll)
14424 {
14425 w->vscroll = 0;
14426 clear_glyph_matrix (w->desired_matrix);
14427 goto recenter;
14428 }
14429
14430 /* If centering point failed to make the whole line visible,
14431 put point at the top instead. That has to make the whole line
14432 visible, if it can be done. */
14433 if (centering_position == 0)
14434 goto done;
14435
14436 clear_glyph_matrix (w->desired_matrix);
14437 centering_position = 0;
14438 goto recenter;
14439 }
14440
14441 done:
14442
14443 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14444 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14445 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14446 ? Qt : Qnil);
14447
14448 /* Display the mode line, if we must. */
14449 if ((update_mode_line
14450 /* If window not full width, must redo its mode line
14451 if (a) the window to its side is being redone and
14452 (b) we do a frame-based redisplay. This is a consequence
14453 of how inverted lines are drawn in frame-based redisplay. */
14454 || (!just_this_one_p
14455 && !FRAME_WINDOW_P (f)
14456 && !WINDOW_FULL_WIDTH_P (w))
14457 /* Line number to display. */
14458 || INTEGERP (w->base_line_pos)
14459 /* Column number is displayed and different from the one displayed. */
14460 || (!NILP (w->column_number_displayed)
14461 && (XFASTINT (w->column_number_displayed) != current_column ())))
14462 /* This means that the window has a mode line. */
14463 && (WINDOW_WANTS_MODELINE_P (w)
14464 || WINDOW_WANTS_HEADER_LINE_P (w)))
14465 {
14466 display_mode_lines (w);
14467
14468 /* If mode line height has changed, arrange for a thorough
14469 immediate redisplay using the correct mode line height. */
14470 if (WINDOW_WANTS_MODELINE_P (w)
14471 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14472 {
14473 fonts_changed_p = 1;
14474 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14475 = DESIRED_MODE_LINE_HEIGHT (w);
14476 }
14477
14478 /* If header line height has changed, arrange for a thorough
14479 immediate redisplay using the correct header line height. */
14480 if (WINDOW_WANTS_HEADER_LINE_P (w)
14481 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14482 {
14483 fonts_changed_p = 1;
14484 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14485 = DESIRED_HEADER_LINE_HEIGHT (w);
14486 }
14487
14488 if (fonts_changed_p)
14489 goto need_larger_matrices;
14490 }
14491
14492 if (!line_number_displayed
14493 && !BUFFERP (w->base_line_pos))
14494 {
14495 w->base_line_pos = Qnil;
14496 w->base_line_number = Qnil;
14497 }
14498
14499 finish_menu_bars:
14500
14501 /* When we reach a frame's selected window, redo the frame's menu bar. */
14502 if (update_mode_line
14503 && EQ (FRAME_SELECTED_WINDOW (f), window))
14504 {
14505 int redisplay_menu_p = 0;
14506
14507 if (FRAME_WINDOW_P (f))
14508 {
14509 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14510 || defined (HAVE_NS) || defined (USE_GTK)
14511 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14512 #else
14513 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14514 #endif
14515 }
14516 else
14517 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14518
14519 if (redisplay_menu_p)
14520 display_menu_bar (w);
14521
14522 #ifdef HAVE_WINDOW_SYSTEM
14523 if (FRAME_WINDOW_P (f))
14524 {
14525 #if defined (USE_GTK) || defined (HAVE_NS)
14526 if (FRAME_EXTERNAL_TOOL_BAR (f))
14527 redisplay_tool_bar (f);
14528 #else
14529 if (WINDOWP (f->tool_bar_window)
14530 && (FRAME_TOOL_BAR_LINES (f) > 0
14531 || !NILP (Vauto_resize_tool_bars))
14532 && redisplay_tool_bar (f))
14533 ignore_mouse_drag_p = 1;
14534 #endif
14535 }
14536 #endif
14537 }
14538
14539 #ifdef HAVE_WINDOW_SYSTEM
14540 if (FRAME_WINDOW_P (f)
14541 && update_window_fringes (w, (just_this_one_p
14542 || (!used_current_matrix_p && !overlay_arrow_seen)
14543 || w->pseudo_window_p)))
14544 {
14545 update_begin (f);
14546 BLOCK_INPUT;
14547 if (draw_window_fringes (w, 1))
14548 x_draw_vertical_border (w);
14549 UNBLOCK_INPUT;
14550 update_end (f);
14551 }
14552 #endif /* HAVE_WINDOW_SYSTEM */
14553
14554 /* We go to this label, with fonts_changed_p nonzero,
14555 if it is necessary to try again using larger glyph matrices.
14556 We have to redeem the scroll bar even in this case,
14557 because the loop in redisplay_internal expects that. */
14558 need_larger_matrices:
14559 ;
14560 finish_scroll_bars:
14561
14562 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14563 {
14564 /* Set the thumb's position and size. */
14565 set_vertical_scroll_bar (w);
14566
14567 /* Note that we actually used the scroll bar attached to this
14568 window, so it shouldn't be deleted at the end of redisplay. */
14569 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14570 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14571 }
14572
14573 /* Restore current_buffer and value of point in it. The window
14574 update may have changed the buffer, so first make sure `opoint'
14575 is still valid (Bug#6177). */
14576 if (CHARPOS (opoint) < BEGV)
14577 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14578 else if (CHARPOS (opoint) > ZV)
14579 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14580 else
14581 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14582
14583 set_buffer_internal_1 (old);
14584 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14585 shorter. This can be caused by log truncation in *Messages*. */
14586 if (CHARPOS (lpoint) <= ZV)
14587 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14588
14589 unbind_to (count, Qnil);
14590 }
14591
14592
14593 /* Build the complete desired matrix of WINDOW with a window start
14594 buffer position POS.
14595
14596 Value is 1 if successful. It is zero if fonts were loaded during
14597 redisplay which makes re-adjusting glyph matrices necessary, and -1
14598 if point would appear in the scroll margins.
14599 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14600 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14601 set in FLAGS.) */
14602
14603 int
14604 try_window (Lisp_Object window, struct text_pos pos, int flags)
14605 {
14606 struct window *w = XWINDOW (window);
14607 struct it it;
14608 struct glyph_row *last_text_row = NULL;
14609 struct frame *f = XFRAME (w->frame);
14610
14611 /* Make POS the new window start. */
14612 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14613
14614 /* Mark cursor position as unknown. No overlay arrow seen. */
14615 w->cursor.vpos = -1;
14616 overlay_arrow_seen = 0;
14617
14618 /* Initialize iterator and info to start at POS. */
14619 start_display (&it, w, pos);
14620
14621 /* Display all lines of W. */
14622 while (it.current_y < it.last_visible_y)
14623 {
14624 if (display_line (&it))
14625 last_text_row = it.glyph_row - 1;
14626 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14627 return 0;
14628 }
14629
14630 /* Don't let the cursor end in the scroll margins. */
14631 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14632 && !MINI_WINDOW_P (w))
14633 {
14634 int this_scroll_margin;
14635
14636 if (scroll_margin > 0)
14637 {
14638 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14639 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14640 }
14641 else
14642 this_scroll_margin = 0;
14643
14644 if ((w->cursor.y >= 0 /* not vscrolled */
14645 && w->cursor.y < this_scroll_margin
14646 && CHARPOS (pos) > BEGV
14647 && IT_CHARPOS (it) < ZV)
14648 /* rms: considering make_cursor_line_fully_visible_p here
14649 seems to give wrong results. We don't want to recenter
14650 when the last line is partly visible, we want to allow
14651 that case to be handled in the usual way. */
14652 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14653 {
14654 w->cursor.vpos = -1;
14655 clear_glyph_matrix (w->desired_matrix);
14656 return -1;
14657 }
14658 }
14659
14660 /* If bottom moved off end of frame, change mode line percentage. */
14661 if (XFASTINT (w->window_end_pos) <= 0
14662 && Z != IT_CHARPOS (it))
14663 w->update_mode_line = Qt;
14664
14665 /* Set window_end_pos to the offset of the last character displayed
14666 on the window from the end of current_buffer. Set
14667 window_end_vpos to its row number. */
14668 if (last_text_row)
14669 {
14670 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14671 w->window_end_bytepos
14672 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14673 w->window_end_pos
14674 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14675 w->window_end_vpos
14676 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14677 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14678 ->displays_text_p);
14679 }
14680 else
14681 {
14682 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14683 w->window_end_pos = make_number (Z - ZV);
14684 w->window_end_vpos = make_number (0);
14685 }
14686
14687 /* But that is not valid info until redisplay finishes. */
14688 w->window_end_valid = Qnil;
14689 return 1;
14690 }
14691
14692
14693 \f
14694 /************************************************************************
14695 Window redisplay reusing current matrix when buffer has not changed
14696 ************************************************************************/
14697
14698 /* Try redisplay of window W showing an unchanged buffer with a
14699 different window start than the last time it was displayed by
14700 reusing its current matrix. Value is non-zero if successful.
14701 W->start is the new window start. */
14702
14703 static int
14704 try_window_reusing_current_matrix (struct window *w)
14705 {
14706 struct frame *f = XFRAME (w->frame);
14707 struct glyph_row *bottom_row;
14708 struct it it;
14709 struct run run;
14710 struct text_pos start, new_start;
14711 int nrows_scrolled, i;
14712 struct glyph_row *last_text_row;
14713 struct glyph_row *last_reused_text_row;
14714 struct glyph_row *start_row;
14715 int start_vpos, min_y, max_y;
14716
14717 #if GLYPH_DEBUG
14718 if (inhibit_try_window_reusing)
14719 return 0;
14720 #endif
14721
14722 if (/* This function doesn't handle terminal frames. */
14723 !FRAME_WINDOW_P (f)
14724 /* Don't try to reuse the display if windows have been split
14725 or such. */
14726 || windows_or_buffers_changed
14727 || cursor_type_changed)
14728 return 0;
14729
14730 /* Can't do this if region may have changed. */
14731 if ((!NILP (Vtransient_mark_mode)
14732 && !NILP (BVAR (current_buffer, mark_active)))
14733 || !NILP (w->region_showing)
14734 || !NILP (Vshow_trailing_whitespace))
14735 return 0;
14736
14737 /* If top-line visibility has changed, give up. */
14738 if (WINDOW_WANTS_HEADER_LINE_P (w)
14739 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14740 return 0;
14741
14742 /* Give up if old or new display is scrolled vertically. We could
14743 make this function handle this, but right now it doesn't. */
14744 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14745 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14746 return 0;
14747
14748 /* The variable new_start now holds the new window start. The old
14749 start `start' can be determined from the current matrix. */
14750 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14751 start = start_row->minpos;
14752 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14753
14754 /* Clear the desired matrix for the display below. */
14755 clear_glyph_matrix (w->desired_matrix);
14756
14757 if (CHARPOS (new_start) <= CHARPOS (start))
14758 {
14759 /* Don't use this method if the display starts with an ellipsis
14760 displayed for invisible text. It's not easy to handle that case
14761 below, and it's certainly not worth the effort since this is
14762 not a frequent case. */
14763 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14764 return 0;
14765
14766 IF_DEBUG (debug_method_add (w, "twu1"));
14767
14768 /* Display up to a row that can be reused. The variable
14769 last_text_row is set to the last row displayed that displays
14770 text. Note that it.vpos == 0 if or if not there is a
14771 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14772 start_display (&it, w, new_start);
14773 w->cursor.vpos = -1;
14774 last_text_row = last_reused_text_row = NULL;
14775
14776 while (it.current_y < it.last_visible_y
14777 && !fonts_changed_p)
14778 {
14779 /* If we have reached into the characters in the START row,
14780 that means the line boundaries have changed. So we
14781 can't start copying with the row START. Maybe it will
14782 work to start copying with the following row. */
14783 while (IT_CHARPOS (it) > CHARPOS (start))
14784 {
14785 /* Advance to the next row as the "start". */
14786 start_row++;
14787 start = start_row->minpos;
14788 /* If there are no more rows to try, or just one, give up. */
14789 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14790 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14791 || CHARPOS (start) == ZV)
14792 {
14793 clear_glyph_matrix (w->desired_matrix);
14794 return 0;
14795 }
14796
14797 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14798 }
14799 /* If we have reached alignment,
14800 we can copy the rest of the rows. */
14801 if (IT_CHARPOS (it) == CHARPOS (start))
14802 break;
14803
14804 if (display_line (&it))
14805 last_text_row = it.glyph_row - 1;
14806 }
14807
14808 /* A value of current_y < last_visible_y means that we stopped
14809 at the previous window start, which in turn means that we
14810 have at least one reusable row. */
14811 if (it.current_y < it.last_visible_y)
14812 {
14813 struct glyph_row *row;
14814
14815 /* IT.vpos always starts from 0; it counts text lines. */
14816 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14817
14818 /* Find PT if not already found in the lines displayed. */
14819 if (w->cursor.vpos < 0)
14820 {
14821 int dy = it.current_y - start_row->y;
14822
14823 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14824 row = row_containing_pos (w, PT, row, NULL, dy);
14825 if (row)
14826 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14827 dy, nrows_scrolled);
14828 else
14829 {
14830 clear_glyph_matrix (w->desired_matrix);
14831 return 0;
14832 }
14833 }
14834
14835 /* Scroll the display. Do it before the current matrix is
14836 changed. The problem here is that update has not yet
14837 run, i.e. part of the current matrix is not up to date.
14838 scroll_run_hook will clear the cursor, and use the
14839 current matrix to get the height of the row the cursor is
14840 in. */
14841 run.current_y = start_row->y;
14842 run.desired_y = it.current_y;
14843 run.height = it.last_visible_y - it.current_y;
14844
14845 if (run.height > 0 && run.current_y != run.desired_y)
14846 {
14847 update_begin (f);
14848 FRAME_RIF (f)->update_window_begin_hook (w);
14849 FRAME_RIF (f)->clear_window_mouse_face (w);
14850 FRAME_RIF (f)->scroll_run_hook (w, &run);
14851 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14852 update_end (f);
14853 }
14854
14855 /* Shift current matrix down by nrows_scrolled lines. */
14856 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14857 rotate_matrix (w->current_matrix,
14858 start_vpos,
14859 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14860 nrows_scrolled);
14861
14862 /* Disable lines that must be updated. */
14863 for (i = 0; i < nrows_scrolled; ++i)
14864 (start_row + i)->enabled_p = 0;
14865
14866 /* Re-compute Y positions. */
14867 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14868 max_y = it.last_visible_y;
14869 for (row = start_row + nrows_scrolled;
14870 row < bottom_row;
14871 ++row)
14872 {
14873 row->y = it.current_y;
14874 row->visible_height = row->height;
14875
14876 if (row->y < min_y)
14877 row->visible_height -= min_y - row->y;
14878 if (row->y + row->height > max_y)
14879 row->visible_height -= row->y + row->height - max_y;
14880 row->redraw_fringe_bitmaps_p = 1;
14881
14882 it.current_y += row->height;
14883
14884 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14885 last_reused_text_row = row;
14886 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14887 break;
14888 }
14889
14890 /* Disable lines in the current matrix which are now
14891 below the window. */
14892 for (++row; row < bottom_row; ++row)
14893 row->enabled_p = row->mode_line_p = 0;
14894 }
14895
14896 /* Update window_end_pos etc.; last_reused_text_row is the last
14897 reused row from the current matrix containing text, if any.
14898 The value of last_text_row is the last displayed line
14899 containing text. */
14900 if (last_reused_text_row)
14901 {
14902 w->window_end_bytepos
14903 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14904 w->window_end_pos
14905 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14906 w->window_end_vpos
14907 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14908 w->current_matrix));
14909 }
14910 else if (last_text_row)
14911 {
14912 w->window_end_bytepos
14913 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14914 w->window_end_pos
14915 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14916 w->window_end_vpos
14917 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14918 }
14919 else
14920 {
14921 /* This window must be completely empty. */
14922 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14923 w->window_end_pos = make_number (Z - ZV);
14924 w->window_end_vpos = make_number (0);
14925 }
14926 w->window_end_valid = Qnil;
14927
14928 /* Update hint: don't try scrolling again in update_window. */
14929 w->desired_matrix->no_scrolling_p = 1;
14930
14931 #if GLYPH_DEBUG
14932 debug_method_add (w, "try_window_reusing_current_matrix 1");
14933 #endif
14934 return 1;
14935 }
14936 else if (CHARPOS (new_start) > CHARPOS (start))
14937 {
14938 struct glyph_row *pt_row, *row;
14939 struct glyph_row *first_reusable_row;
14940 struct glyph_row *first_row_to_display;
14941 int dy;
14942 int yb = window_text_bottom_y (w);
14943
14944 /* Find the row starting at new_start, if there is one. Don't
14945 reuse a partially visible line at the end. */
14946 first_reusable_row = start_row;
14947 while (first_reusable_row->enabled_p
14948 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14949 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14950 < CHARPOS (new_start)))
14951 ++first_reusable_row;
14952
14953 /* Give up if there is no row to reuse. */
14954 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14955 || !first_reusable_row->enabled_p
14956 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14957 != CHARPOS (new_start)))
14958 return 0;
14959
14960 /* We can reuse fully visible rows beginning with
14961 first_reusable_row to the end of the window. Set
14962 first_row_to_display to the first row that cannot be reused.
14963 Set pt_row to the row containing point, if there is any. */
14964 pt_row = NULL;
14965 for (first_row_to_display = first_reusable_row;
14966 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14967 ++first_row_to_display)
14968 {
14969 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14970 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14971 pt_row = first_row_to_display;
14972 }
14973
14974 /* Start displaying at the start of first_row_to_display. */
14975 xassert (first_row_to_display->y < yb);
14976 init_to_row_start (&it, w, first_row_to_display);
14977
14978 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14979 - start_vpos);
14980 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14981 - nrows_scrolled);
14982 it.current_y = (first_row_to_display->y - first_reusable_row->y
14983 + WINDOW_HEADER_LINE_HEIGHT (w));
14984
14985 /* Display lines beginning with first_row_to_display in the
14986 desired matrix. Set last_text_row to the last row displayed
14987 that displays text. */
14988 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14989 if (pt_row == NULL)
14990 w->cursor.vpos = -1;
14991 last_text_row = NULL;
14992 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14993 if (display_line (&it))
14994 last_text_row = it.glyph_row - 1;
14995
14996 /* If point is in a reused row, adjust y and vpos of the cursor
14997 position. */
14998 if (pt_row)
14999 {
15000 w->cursor.vpos -= nrows_scrolled;
15001 w->cursor.y -= first_reusable_row->y - start_row->y;
15002 }
15003
15004 /* Give up if point isn't in a row displayed or reused. (This
15005 also handles the case where w->cursor.vpos < nrows_scrolled
15006 after the calls to display_line, which can happen with scroll
15007 margins. See bug#1295.) */
15008 if (w->cursor.vpos < 0)
15009 {
15010 clear_glyph_matrix (w->desired_matrix);
15011 return 0;
15012 }
15013
15014 /* Scroll the display. */
15015 run.current_y = first_reusable_row->y;
15016 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15017 run.height = it.last_visible_y - run.current_y;
15018 dy = run.current_y - run.desired_y;
15019
15020 if (run.height)
15021 {
15022 update_begin (f);
15023 FRAME_RIF (f)->update_window_begin_hook (w);
15024 FRAME_RIF (f)->clear_window_mouse_face (w);
15025 FRAME_RIF (f)->scroll_run_hook (w, &run);
15026 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15027 update_end (f);
15028 }
15029
15030 /* Adjust Y positions of reused rows. */
15031 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15032 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15033 max_y = it.last_visible_y;
15034 for (row = first_reusable_row; row < first_row_to_display; ++row)
15035 {
15036 row->y -= dy;
15037 row->visible_height = row->height;
15038 if (row->y < min_y)
15039 row->visible_height -= min_y - row->y;
15040 if (row->y + row->height > max_y)
15041 row->visible_height -= row->y + row->height - max_y;
15042 row->redraw_fringe_bitmaps_p = 1;
15043 }
15044
15045 /* Scroll the current matrix. */
15046 xassert (nrows_scrolled > 0);
15047 rotate_matrix (w->current_matrix,
15048 start_vpos,
15049 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15050 -nrows_scrolled);
15051
15052 /* Disable rows not reused. */
15053 for (row -= nrows_scrolled; row < bottom_row; ++row)
15054 row->enabled_p = 0;
15055
15056 /* Point may have moved to a different line, so we cannot assume that
15057 the previous cursor position is valid; locate the correct row. */
15058 if (pt_row)
15059 {
15060 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15061 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15062 row++)
15063 {
15064 w->cursor.vpos++;
15065 w->cursor.y = row->y;
15066 }
15067 if (row < bottom_row)
15068 {
15069 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15070 struct glyph *end = glyph + row->used[TEXT_AREA];
15071
15072 /* Can't use this optimization with bidi-reordered glyph
15073 rows, unless cursor is already at point. */
15074 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15075 {
15076 if (!(w->cursor.hpos >= 0
15077 && w->cursor.hpos < row->used[TEXT_AREA]
15078 && BUFFERP (glyph->object)
15079 && glyph->charpos == PT))
15080 return 0;
15081 }
15082 else
15083 for (; glyph < end
15084 && (!BUFFERP (glyph->object)
15085 || glyph->charpos < PT);
15086 glyph++)
15087 {
15088 w->cursor.hpos++;
15089 w->cursor.x += glyph->pixel_width;
15090 }
15091 }
15092 }
15093
15094 /* Adjust window end. A null value of last_text_row means that
15095 the window end is in reused rows which in turn means that
15096 only its vpos can have changed. */
15097 if (last_text_row)
15098 {
15099 w->window_end_bytepos
15100 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15101 w->window_end_pos
15102 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15103 w->window_end_vpos
15104 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15105 }
15106 else
15107 {
15108 w->window_end_vpos
15109 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15110 }
15111
15112 w->window_end_valid = Qnil;
15113 w->desired_matrix->no_scrolling_p = 1;
15114
15115 #if GLYPH_DEBUG
15116 debug_method_add (w, "try_window_reusing_current_matrix 2");
15117 #endif
15118 return 1;
15119 }
15120
15121 return 0;
15122 }
15123
15124
15125 \f
15126 /************************************************************************
15127 Window redisplay reusing current matrix when buffer has changed
15128 ************************************************************************/
15129
15130 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15131 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15132 EMACS_INT *, EMACS_INT *);
15133 static struct glyph_row *
15134 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15135 struct glyph_row *);
15136
15137
15138 /* Return the last row in MATRIX displaying text. If row START is
15139 non-null, start searching with that row. IT gives the dimensions
15140 of the display. Value is null if matrix is empty; otherwise it is
15141 a pointer to the row found. */
15142
15143 static struct glyph_row *
15144 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15145 struct glyph_row *start)
15146 {
15147 struct glyph_row *row, *row_found;
15148
15149 /* Set row_found to the last row in IT->w's current matrix
15150 displaying text. The loop looks funny but think of partially
15151 visible lines. */
15152 row_found = NULL;
15153 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15154 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15155 {
15156 xassert (row->enabled_p);
15157 row_found = row;
15158 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15159 break;
15160 ++row;
15161 }
15162
15163 return row_found;
15164 }
15165
15166
15167 /* Return the last row in the current matrix of W that is not affected
15168 by changes at the start of current_buffer that occurred since W's
15169 current matrix was built. Value is null if no such row exists.
15170
15171 BEG_UNCHANGED us the number of characters unchanged at the start of
15172 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15173 first changed character in current_buffer. Characters at positions <
15174 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15175 when the current matrix was built. */
15176
15177 static struct glyph_row *
15178 find_last_unchanged_at_beg_row (struct window *w)
15179 {
15180 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15181 struct glyph_row *row;
15182 struct glyph_row *row_found = NULL;
15183 int yb = window_text_bottom_y (w);
15184
15185 /* Find the last row displaying unchanged text. */
15186 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15187 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15188 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15189 ++row)
15190 {
15191 if (/* If row ends before first_changed_pos, it is unchanged,
15192 except in some case. */
15193 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15194 /* When row ends in ZV and we write at ZV it is not
15195 unchanged. */
15196 && !row->ends_at_zv_p
15197 /* When first_changed_pos is the end of a continued line,
15198 row is not unchanged because it may be no longer
15199 continued. */
15200 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15201 && (row->continued_p
15202 || row->exact_window_width_line_p)))
15203 row_found = row;
15204
15205 /* Stop if last visible row. */
15206 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15207 break;
15208 }
15209
15210 return row_found;
15211 }
15212
15213
15214 /* Find the first glyph row in the current matrix of W that is not
15215 affected by changes at the end of current_buffer since the
15216 time W's current matrix was built.
15217
15218 Return in *DELTA the number of chars by which buffer positions in
15219 unchanged text at the end of current_buffer must be adjusted.
15220
15221 Return in *DELTA_BYTES the corresponding number of bytes.
15222
15223 Value is null if no such row exists, i.e. all rows are affected by
15224 changes. */
15225
15226 static struct glyph_row *
15227 find_first_unchanged_at_end_row (struct window *w,
15228 EMACS_INT *delta, EMACS_INT *delta_bytes)
15229 {
15230 struct glyph_row *row;
15231 struct glyph_row *row_found = NULL;
15232
15233 *delta = *delta_bytes = 0;
15234
15235 /* Display must not have been paused, otherwise the current matrix
15236 is not up to date. */
15237 eassert (!NILP (w->window_end_valid));
15238
15239 /* A value of window_end_pos >= END_UNCHANGED means that the window
15240 end is in the range of changed text. If so, there is no
15241 unchanged row at the end of W's current matrix. */
15242 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15243 return NULL;
15244
15245 /* Set row to the last row in W's current matrix displaying text. */
15246 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15247
15248 /* If matrix is entirely empty, no unchanged row exists. */
15249 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15250 {
15251 /* The value of row is the last glyph row in the matrix having a
15252 meaningful buffer position in it. The end position of row
15253 corresponds to window_end_pos. This allows us to translate
15254 buffer positions in the current matrix to current buffer
15255 positions for characters not in changed text. */
15256 EMACS_INT Z_old =
15257 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15258 EMACS_INT Z_BYTE_old =
15259 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15260 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15261 struct glyph_row *first_text_row
15262 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15263
15264 *delta = Z - Z_old;
15265 *delta_bytes = Z_BYTE - Z_BYTE_old;
15266
15267 /* Set last_unchanged_pos to the buffer position of the last
15268 character in the buffer that has not been changed. Z is the
15269 index + 1 of the last character in current_buffer, i.e. by
15270 subtracting END_UNCHANGED we get the index of the last
15271 unchanged character, and we have to add BEG to get its buffer
15272 position. */
15273 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15274 last_unchanged_pos_old = last_unchanged_pos - *delta;
15275
15276 /* Search backward from ROW for a row displaying a line that
15277 starts at a minimum position >= last_unchanged_pos_old. */
15278 for (; row > first_text_row; --row)
15279 {
15280 /* This used to abort, but it can happen.
15281 It is ok to just stop the search instead here. KFS. */
15282 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15283 break;
15284
15285 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15286 row_found = row;
15287 }
15288 }
15289
15290 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15291
15292 return row_found;
15293 }
15294
15295
15296 /* Make sure that glyph rows in the current matrix of window W
15297 reference the same glyph memory as corresponding rows in the
15298 frame's frame matrix. This function is called after scrolling W's
15299 current matrix on a terminal frame in try_window_id and
15300 try_window_reusing_current_matrix. */
15301
15302 static void
15303 sync_frame_with_window_matrix_rows (struct window *w)
15304 {
15305 struct frame *f = XFRAME (w->frame);
15306 struct glyph_row *window_row, *window_row_end, *frame_row;
15307
15308 /* Preconditions: W must be a leaf window and full-width. Its frame
15309 must have a frame matrix. */
15310 xassert (NILP (w->hchild) && NILP (w->vchild));
15311 xassert (WINDOW_FULL_WIDTH_P (w));
15312 xassert (!FRAME_WINDOW_P (f));
15313
15314 /* If W is a full-width window, glyph pointers in W's current matrix
15315 have, by definition, to be the same as glyph pointers in the
15316 corresponding frame matrix. Note that frame matrices have no
15317 marginal areas (see build_frame_matrix). */
15318 window_row = w->current_matrix->rows;
15319 window_row_end = window_row + w->current_matrix->nrows;
15320 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15321 while (window_row < window_row_end)
15322 {
15323 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15324 struct glyph *end = window_row->glyphs[LAST_AREA];
15325
15326 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15327 frame_row->glyphs[TEXT_AREA] = start;
15328 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15329 frame_row->glyphs[LAST_AREA] = end;
15330
15331 /* Disable frame rows whose corresponding window rows have
15332 been disabled in try_window_id. */
15333 if (!window_row->enabled_p)
15334 frame_row->enabled_p = 0;
15335
15336 ++window_row, ++frame_row;
15337 }
15338 }
15339
15340
15341 /* Find the glyph row in window W containing CHARPOS. Consider all
15342 rows between START and END (not inclusive). END null means search
15343 all rows to the end of the display area of W. Value is the row
15344 containing CHARPOS or null. */
15345
15346 struct glyph_row *
15347 row_containing_pos (struct window *w, EMACS_INT charpos,
15348 struct glyph_row *start, struct glyph_row *end, int dy)
15349 {
15350 struct glyph_row *row = start;
15351 struct glyph_row *best_row = NULL;
15352 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15353 int last_y;
15354
15355 /* If we happen to start on a header-line, skip that. */
15356 if (row->mode_line_p)
15357 ++row;
15358
15359 if ((end && row >= end) || !row->enabled_p)
15360 return NULL;
15361
15362 last_y = window_text_bottom_y (w) - dy;
15363
15364 while (1)
15365 {
15366 /* Give up if we have gone too far. */
15367 if (end && row >= end)
15368 return NULL;
15369 /* This formerly returned if they were equal.
15370 I think that both quantities are of a "last plus one" type;
15371 if so, when they are equal, the row is within the screen. -- rms. */
15372 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15373 return NULL;
15374
15375 /* If it is in this row, return this row. */
15376 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15377 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15378 /* The end position of a row equals the start
15379 position of the next row. If CHARPOS is there, we
15380 would rather display it in the next line, except
15381 when this line ends in ZV. */
15382 && !row->ends_at_zv_p
15383 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15384 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15385 {
15386 struct glyph *g;
15387
15388 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15389 || (!best_row && !row->continued_p))
15390 return row;
15391 /* In bidi-reordered rows, there could be several rows
15392 occluding point, all of them belonging to the same
15393 continued line. We need to find the row which fits
15394 CHARPOS the best. */
15395 for (g = row->glyphs[TEXT_AREA];
15396 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15397 g++)
15398 {
15399 if (!STRINGP (g->object))
15400 {
15401 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15402 {
15403 mindif = eabs (g->charpos - charpos);
15404 best_row = row;
15405 /* Exact match always wins. */
15406 if (mindif == 0)
15407 return best_row;
15408 }
15409 }
15410 }
15411 }
15412 else if (best_row && !row->continued_p)
15413 return best_row;
15414 ++row;
15415 }
15416 }
15417
15418
15419 /* Try to redisplay window W by reusing its existing display. W's
15420 current matrix must be up to date when this function is called,
15421 i.e. window_end_valid must not be nil.
15422
15423 Value is
15424
15425 1 if display has been updated
15426 0 if otherwise unsuccessful
15427 -1 if redisplay with same window start is known not to succeed
15428
15429 The following steps are performed:
15430
15431 1. Find the last row in the current matrix of W that is not
15432 affected by changes at the start of current_buffer. If no such row
15433 is found, give up.
15434
15435 2. Find the first row in W's current matrix that is not affected by
15436 changes at the end of current_buffer. Maybe there is no such row.
15437
15438 3. Display lines beginning with the row + 1 found in step 1 to the
15439 row found in step 2 or, if step 2 didn't find a row, to the end of
15440 the window.
15441
15442 4. If cursor is not known to appear on the window, give up.
15443
15444 5. If display stopped at the row found in step 2, scroll the
15445 display and current matrix as needed.
15446
15447 6. Maybe display some lines at the end of W, if we must. This can
15448 happen under various circumstances, like a partially visible line
15449 becoming fully visible, or because newly displayed lines are displayed
15450 in smaller font sizes.
15451
15452 7. Update W's window end information. */
15453
15454 static int
15455 try_window_id (struct window *w)
15456 {
15457 struct frame *f = XFRAME (w->frame);
15458 struct glyph_matrix *current_matrix = w->current_matrix;
15459 struct glyph_matrix *desired_matrix = w->desired_matrix;
15460 struct glyph_row *last_unchanged_at_beg_row;
15461 struct glyph_row *first_unchanged_at_end_row;
15462 struct glyph_row *row;
15463 struct glyph_row *bottom_row;
15464 int bottom_vpos;
15465 struct it it;
15466 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15467 int dvpos, dy;
15468 struct text_pos start_pos;
15469 struct run run;
15470 int first_unchanged_at_end_vpos = 0;
15471 struct glyph_row *last_text_row, *last_text_row_at_end;
15472 struct text_pos start;
15473 EMACS_INT first_changed_charpos, last_changed_charpos;
15474
15475 #if GLYPH_DEBUG
15476 if (inhibit_try_window_id)
15477 return 0;
15478 #endif
15479
15480 /* This is handy for debugging. */
15481 #if 0
15482 #define GIVE_UP(X) \
15483 do { \
15484 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15485 return 0; \
15486 } while (0)
15487 #else
15488 #define GIVE_UP(X) return 0
15489 #endif
15490
15491 SET_TEXT_POS_FROM_MARKER (start, w->start);
15492
15493 /* Don't use this for mini-windows because these can show
15494 messages and mini-buffers, and we don't handle that here. */
15495 if (MINI_WINDOW_P (w))
15496 GIVE_UP (1);
15497
15498 /* This flag is used to prevent redisplay optimizations. */
15499 if (windows_or_buffers_changed || cursor_type_changed)
15500 GIVE_UP (2);
15501
15502 /* Verify that narrowing has not changed.
15503 Also verify that we were not told to prevent redisplay optimizations.
15504 It would be nice to further
15505 reduce the number of cases where this prevents try_window_id. */
15506 if (current_buffer->clip_changed
15507 || current_buffer->prevent_redisplay_optimizations_p)
15508 GIVE_UP (3);
15509
15510 /* Window must either use window-based redisplay or be full width. */
15511 if (!FRAME_WINDOW_P (f)
15512 && (!FRAME_LINE_INS_DEL_OK (f)
15513 || !WINDOW_FULL_WIDTH_P (w)))
15514 GIVE_UP (4);
15515
15516 /* Give up if point is known NOT to appear in W. */
15517 if (PT < CHARPOS (start))
15518 GIVE_UP (5);
15519
15520 /* Another way to prevent redisplay optimizations. */
15521 if (XFASTINT (w->last_modified) == 0)
15522 GIVE_UP (6);
15523
15524 /* Verify that window is not hscrolled. */
15525 if (XFASTINT (w->hscroll) != 0)
15526 GIVE_UP (7);
15527
15528 /* Verify that display wasn't paused. */
15529 if (NILP (w->window_end_valid))
15530 GIVE_UP (8);
15531
15532 /* Can't use this if highlighting a region because a cursor movement
15533 will do more than just set the cursor. */
15534 if (!NILP (Vtransient_mark_mode)
15535 && !NILP (BVAR (current_buffer, mark_active)))
15536 GIVE_UP (9);
15537
15538 /* Likewise if highlighting trailing whitespace. */
15539 if (!NILP (Vshow_trailing_whitespace))
15540 GIVE_UP (11);
15541
15542 /* Likewise if showing a region. */
15543 if (!NILP (w->region_showing))
15544 GIVE_UP (10);
15545
15546 /* Can't use this if overlay arrow position and/or string have
15547 changed. */
15548 if (overlay_arrows_changed_p ())
15549 GIVE_UP (12);
15550
15551 /* When word-wrap is on, adding a space to the first word of a
15552 wrapped line can change the wrap position, altering the line
15553 above it. It might be worthwhile to handle this more
15554 intelligently, but for now just redisplay from scratch. */
15555 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
15556 GIVE_UP (21);
15557
15558 /* Under bidi reordering, adding or deleting a character in the
15559 beginning of a paragraph, before the first strong directional
15560 character, can change the base direction of the paragraph (unless
15561 the buffer specifies a fixed paragraph direction), which will
15562 require to redisplay the whole paragraph. It might be worthwhile
15563 to find the paragraph limits and widen the range of redisplayed
15564 lines to that, but for now just give up this optimization and
15565 redisplay from scratch. */
15566 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15567 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
15568 GIVE_UP (22);
15569
15570 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15571 only if buffer has really changed. The reason is that the gap is
15572 initially at Z for freshly visited files. The code below would
15573 set end_unchanged to 0 in that case. */
15574 if (MODIFF > SAVE_MODIFF
15575 /* This seems to happen sometimes after saving a buffer. */
15576 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15577 {
15578 if (GPT - BEG < BEG_UNCHANGED)
15579 BEG_UNCHANGED = GPT - BEG;
15580 if (Z - GPT < END_UNCHANGED)
15581 END_UNCHANGED = Z - GPT;
15582 }
15583
15584 /* The position of the first and last character that has been changed. */
15585 first_changed_charpos = BEG + BEG_UNCHANGED;
15586 last_changed_charpos = Z - END_UNCHANGED;
15587
15588 /* If window starts after a line end, and the last change is in
15589 front of that newline, then changes don't affect the display.
15590 This case happens with stealth-fontification. Note that although
15591 the display is unchanged, glyph positions in the matrix have to
15592 be adjusted, of course. */
15593 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15594 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15595 && ((last_changed_charpos < CHARPOS (start)
15596 && CHARPOS (start) == BEGV)
15597 || (last_changed_charpos < CHARPOS (start) - 1
15598 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15599 {
15600 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
15601 struct glyph_row *r0;
15602
15603 /* Compute how many chars/bytes have been added to or removed
15604 from the buffer. */
15605 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15606 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15607 Z_delta = Z - Z_old;
15608 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
15609
15610 /* Give up if PT is not in the window. Note that it already has
15611 been checked at the start of try_window_id that PT is not in
15612 front of the window start. */
15613 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
15614 GIVE_UP (13);
15615
15616 /* If window start is unchanged, we can reuse the whole matrix
15617 as is, after adjusting glyph positions. No need to compute
15618 the window end again, since its offset from Z hasn't changed. */
15619 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15620 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
15621 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
15622 /* PT must not be in a partially visible line. */
15623 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
15624 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15625 {
15626 /* Adjust positions in the glyph matrix. */
15627 if (Z_delta || Z_delta_bytes)
15628 {
15629 struct glyph_row *r1
15630 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15631 increment_matrix_positions (w->current_matrix,
15632 MATRIX_ROW_VPOS (r0, current_matrix),
15633 MATRIX_ROW_VPOS (r1, current_matrix),
15634 Z_delta, Z_delta_bytes);
15635 }
15636
15637 /* Set the cursor. */
15638 row = row_containing_pos (w, PT, r0, NULL, 0);
15639 if (row)
15640 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15641 else
15642 abort ();
15643 return 1;
15644 }
15645 }
15646
15647 /* Handle the case that changes are all below what is displayed in
15648 the window, and that PT is in the window. This shortcut cannot
15649 be taken if ZV is visible in the window, and text has been added
15650 there that is visible in the window. */
15651 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15652 /* ZV is not visible in the window, or there are no
15653 changes at ZV, actually. */
15654 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15655 || first_changed_charpos == last_changed_charpos))
15656 {
15657 struct glyph_row *r0;
15658
15659 /* Give up if PT is not in the window. Note that it already has
15660 been checked at the start of try_window_id that PT is not in
15661 front of the window start. */
15662 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15663 GIVE_UP (14);
15664
15665 /* If window start is unchanged, we can reuse the whole matrix
15666 as is, without changing glyph positions since no text has
15667 been added/removed in front of the window end. */
15668 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15669 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15670 /* PT must not be in a partially visible line. */
15671 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15672 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15673 {
15674 /* We have to compute the window end anew since text
15675 could have been added/removed after it. */
15676 w->window_end_pos
15677 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15678 w->window_end_bytepos
15679 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15680
15681 /* Set the cursor. */
15682 row = row_containing_pos (w, PT, r0, NULL, 0);
15683 if (row)
15684 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15685 else
15686 abort ();
15687 return 2;
15688 }
15689 }
15690
15691 /* Give up if window start is in the changed area.
15692
15693 The condition used to read
15694
15695 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15696
15697 but why that was tested escapes me at the moment. */
15698 if (CHARPOS (start) >= first_changed_charpos
15699 && CHARPOS (start) <= last_changed_charpos)
15700 GIVE_UP (15);
15701
15702 /* Check that window start agrees with the start of the first glyph
15703 row in its current matrix. Check this after we know the window
15704 start is not in changed text, otherwise positions would not be
15705 comparable. */
15706 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15707 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15708 GIVE_UP (16);
15709
15710 /* Give up if the window ends in strings. Overlay strings
15711 at the end are difficult to handle, so don't try. */
15712 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15713 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15714 GIVE_UP (20);
15715
15716 /* Compute the position at which we have to start displaying new
15717 lines. Some of the lines at the top of the window might be
15718 reusable because they are not displaying changed text. Find the
15719 last row in W's current matrix not affected by changes at the
15720 start of current_buffer. Value is null if changes start in the
15721 first line of window. */
15722 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15723 if (last_unchanged_at_beg_row)
15724 {
15725 /* Avoid starting to display in the moddle of a character, a TAB
15726 for instance. This is easier than to set up the iterator
15727 exactly, and it's not a frequent case, so the additional
15728 effort wouldn't really pay off. */
15729 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15730 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15731 && last_unchanged_at_beg_row > w->current_matrix->rows)
15732 --last_unchanged_at_beg_row;
15733
15734 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15735 GIVE_UP (17);
15736
15737 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15738 GIVE_UP (18);
15739 start_pos = it.current.pos;
15740
15741 /* Start displaying new lines in the desired matrix at the same
15742 vpos we would use in the current matrix, i.e. below
15743 last_unchanged_at_beg_row. */
15744 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15745 current_matrix);
15746 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15747 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15748
15749 xassert (it.hpos == 0 && it.current_x == 0);
15750 }
15751 else
15752 {
15753 /* There are no reusable lines at the start of the window.
15754 Start displaying in the first text line. */
15755 start_display (&it, w, start);
15756 it.vpos = it.first_vpos;
15757 start_pos = it.current.pos;
15758 }
15759
15760 /* Find the first row that is not affected by changes at the end of
15761 the buffer. Value will be null if there is no unchanged row, in
15762 which case we must redisplay to the end of the window. delta
15763 will be set to the value by which buffer positions beginning with
15764 first_unchanged_at_end_row have to be adjusted due to text
15765 changes. */
15766 first_unchanged_at_end_row
15767 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15768 IF_DEBUG (debug_delta = delta);
15769 IF_DEBUG (debug_delta_bytes = delta_bytes);
15770
15771 /* Set stop_pos to the buffer position up to which we will have to
15772 display new lines. If first_unchanged_at_end_row != NULL, this
15773 is the buffer position of the start of the line displayed in that
15774 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15775 that we don't stop at a buffer position. */
15776 stop_pos = 0;
15777 if (first_unchanged_at_end_row)
15778 {
15779 xassert (last_unchanged_at_beg_row == NULL
15780 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15781
15782 /* If this is a continuation line, move forward to the next one
15783 that isn't. Changes in lines above affect this line.
15784 Caution: this may move first_unchanged_at_end_row to a row
15785 not displaying text. */
15786 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15787 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15788 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15789 < it.last_visible_y))
15790 ++first_unchanged_at_end_row;
15791
15792 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15793 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15794 >= it.last_visible_y))
15795 first_unchanged_at_end_row = NULL;
15796 else
15797 {
15798 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15799 + delta);
15800 first_unchanged_at_end_vpos
15801 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15802 xassert (stop_pos >= Z - END_UNCHANGED);
15803 }
15804 }
15805 else if (last_unchanged_at_beg_row == NULL)
15806 GIVE_UP (19);
15807
15808
15809 #if GLYPH_DEBUG
15810
15811 /* Either there is no unchanged row at the end, or the one we have
15812 now displays text. This is a necessary condition for the window
15813 end pos calculation at the end of this function. */
15814 xassert (first_unchanged_at_end_row == NULL
15815 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15816
15817 debug_last_unchanged_at_beg_vpos
15818 = (last_unchanged_at_beg_row
15819 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15820 : -1);
15821 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15822
15823 #endif /* GLYPH_DEBUG != 0 */
15824
15825
15826 /* Display new lines. Set last_text_row to the last new line
15827 displayed which has text on it, i.e. might end up as being the
15828 line where the window_end_vpos is. */
15829 w->cursor.vpos = -1;
15830 last_text_row = NULL;
15831 overlay_arrow_seen = 0;
15832 while (it.current_y < it.last_visible_y
15833 && !fonts_changed_p
15834 && (first_unchanged_at_end_row == NULL
15835 || IT_CHARPOS (it) < stop_pos))
15836 {
15837 if (display_line (&it))
15838 last_text_row = it.glyph_row - 1;
15839 }
15840
15841 if (fonts_changed_p)
15842 return -1;
15843
15844
15845 /* Compute differences in buffer positions, y-positions etc. for
15846 lines reused at the bottom of the window. Compute what we can
15847 scroll. */
15848 if (first_unchanged_at_end_row
15849 /* No lines reused because we displayed everything up to the
15850 bottom of the window. */
15851 && it.current_y < it.last_visible_y)
15852 {
15853 dvpos = (it.vpos
15854 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15855 current_matrix));
15856 dy = it.current_y - first_unchanged_at_end_row->y;
15857 run.current_y = first_unchanged_at_end_row->y;
15858 run.desired_y = run.current_y + dy;
15859 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15860 }
15861 else
15862 {
15863 delta = delta_bytes = dvpos = dy
15864 = run.current_y = run.desired_y = run.height = 0;
15865 first_unchanged_at_end_row = NULL;
15866 }
15867 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15868
15869
15870 /* Find the cursor if not already found. We have to decide whether
15871 PT will appear on this window (it sometimes doesn't, but this is
15872 not a very frequent case.) This decision has to be made before
15873 the current matrix is altered. A value of cursor.vpos < 0 means
15874 that PT is either in one of the lines beginning at
15875 first_unchanged_at_end_row or below the window. Don't care for
15876 lines that might be displayed later at the window end; as
15877 mentioned, this is not a frequent case. */
15878 if (w->cursor.vpos < 0)
15879 {
15880 /* Cursor in unchanged rows at the top? */
15881 if (PT < CHARPOS (start_pos)
15882 && last_unchanged_at_beg_row)
15883 {
15884 row = row_containing_pos (w, PT,
15885 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15886 last_unchanged_at_beg_row + 1, 0);
15887 if (row)
15888 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15889 }
15890
15891 /* Start from first_unchanged_at_end_row looking for PT. */
15892 else if (first_unchanged_at_end_row)
15893 {
15894 row = row_containing_pos (w, PT - delta,
15895 first_unchanged_at_end_row, NULL, 0);
15896 if (row)
15897 set_cursor_from_row (w, row, w->current_matrix, delta,
15898 delta_bytes, dy, dvpos);
15899 }
15900
15901 /* Give up if cursor was not found. */
15902 if (w->cursor.vpos < 0)
15903 {
15904 clear_glyph_matrix (w->desired_matrix);
15905 return -1;
15906 }
15907 }
15908
15909 /* Don't let the cursor end in the scroll margins. */
15910 {
15911 int this_scroll_margin, cursor_height;
15912
15913 this_scroll_margin = max (0, scroll_margin);
15914 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15915 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15916 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15917
15918 if ((w->cursor.y < this_scroll_margin
15919 && CHARPOS (start) > BEGV)
15920 /* Old redisplay didn't take scroll margin into account at the bottom,
15921 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15922 || (w->cursor.y + (make_cursor_line_fully_visible_p
15923 ? cursor_height + this_scroll_margin
15924 : 1)) > it.last_visible_y)
15925 {
15926 w->cursor.vpos = -1;
15927 clear_glyph_matrix (w->desired_matrix);
15928 return -1;
15929 }
15930 }
15931
15932 /* Scroll the display. Do it before changing the current matrix so
15933 that xterm.c doesn't get confused about where the cursor glyph is
15934 found. */
15935 if (dy && run.height)
15936 {
15937 update_begin (f);
15938
15939 if (FRAME_WINDOW_P (f))
15940 {
15941 FRAME_RIF (f)->update_window_begin_hook (w);
15942 FRAME_RIF (f)->clear_window_mouse_face (w);
15943 FRAME_RIF (f)->scroll_run_hook (w, &run);
15944 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15945 }
15946 else
15947 {
15948 /* Terminal frame. In this case, dvpos gives the number of
15949 lines to scroll by; dvpos < 0 means scroll up. */
15950 int from_vpos
15951 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15952 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
15953 int end = (WINDOW_TOP_EDGE_LINE (w)
15954 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15955 + window_internal_height (w));
15956
15957 #if defined (HAVE_GPM) || defined (MSDOS)
15958 x_clear_window_mouse_face (w);
15959 #endif
15960 /* Perform the operation on the screen. */
15961 if (dvpos > 0)
15962 {
15963 /* Scroll last_unchanged_at_beg_row to the end of the
15964 window down dvpos lines. */
15965 set_terminal_window (f, end);
15966
15967 /* On dumb terminals delete dvpos lines at the end
15968 before inserting dvpos empty lines. */
15969 if (!FRAME_SCROLL_REGION_OK (f))
15970 ins_del_lines (f, end - dvpos, -dvpos);
15971
15972 /* Insert dvpos empty lines in front of
15973 last_unchanged_at_beg_row. */
15974 ins_del_lines (f, from, dvpos);
15975 }
15976 else if (dvpos < 0)
15977 {
15978 /* Scroll up last_unchanged_at_beg_vpos to the end of
15979 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15980 set_terminal_window (f, end);
15981
15982 /* Delete dvpos lines in front of
15983 last_unchanged_at_beg_vpos. ins_del_lines will set
15984 the cursor to the given vpos and emit |dvpos| delete
15985 line sequences. */
15986 ins_del_lines (f, from + dvpos, dvpos);
15987
15988 /* On a dumb terminal insert dvpos empty lines at the
15989 end. */
15990 if (!FRAME_SCROLL_REGION_OK (f))
15991 ins_del_lines (f, end + dvpos, -dvpos);
15992 }
15993
15994 set_terminal_window (f, 0);
15995 }
15996
15997 update_end (f);
15998 }
15999
16000 /* Shift reused rows of the current matrix to the right position.
16001 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16002 text. */
16003 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16004 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16005 if (dvpos < 0)
16006 {
16007 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16008 bottom_vpos, dvpos);
16009 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16010 bottom_vpos, 0);
16011 }
16012 else if (dvpos > 0)
16013 {
16014 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16015 bottom_vpos, dvpos);
16016 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16017 first_unchanged_at_end_vpos + dvpos, 0);
16018 }
16019
16020 /* For frame-based redisplay, make sure that current frame and window
16021 matrix are in sync with respect to glyph memory. */
16022 if (!FRAME_WINDOW_P (f))
16023 sync_frame_with_window_matrix_rows (w);
16024
16025 /* Adjust buffer positions in reused rows. */
16026 if (delta || delta_bytes)
16027 increment_matrix_positions (current_matrix,
16028 first_unchanged_at_end_vpos + dvpos,
16029 bottom_vpos, delta, delta_bytes);
16030
16031 /* Adjust Y positions. */
16032 if (dy)
16033 shift_glyph_matrix (w, current_matrix,
16034 first_unchanged_at_end_vpos + dvpos,
16035 bottom_vpos, dy);
16036
16037 if (first_unchanged_at_end_row)
16038 {
16039 first_unchanged_at_end_row += dvpos;
16040 if (first_unchanged_at_end_row->y >= it.last_visible_y
16041 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16042 first_unchanged_at_end_row = NULL;
16043 }
16044
16045 /* If scrolling up, there may be some lines to display at the end of
16046 the window. */
16047 last_text_row_at_end = NULL;
16048 if (dy < 0)
16049 {
16050 /* Scrolling up can leave for example a partially visible line
16051 at the end of the window to be redisplayed. */
16052 /* Set last_row to the glyph row in the current matrix where the
16053 window end line is found. It has been moved up or down in
16054 the matrix by dvpos. */
16055 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16056 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16057
16058 /* If last_row is the window end line, it should display text. */
16059 xassert (last_row->displays_text_p);
16060
16061 /* If window end line was partially visible before, begin
16062 displaying at that line. Otherwise begin displaying with the
16063 line following it. */
16064 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16065 {
16066 init_to_row_start (&it, w, last_row);
16067 it.vpos = last_vpos;
16068 it.current_y = last_row->y;
16069 }
16070 else
16071 {
16072 init_to_row_end (&it, w, last_row);
16073 it.vpos = 1 + last_vpos;
16074 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16075 ++last_row;
16076 }
16077
16078 /* We may start in a continuation line. If so, we have to
16079 get the right continuation_lines_width and current_x. */
16080 it.continuation_lines_width = last_row->continuation_lines_width;
16081 it.hpos = it.current_x = 0;
16082
16083 /* Display the rest of the lines at the window end. */
16084 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16085 while (it.current_y < it.last_visible_y
16086 && !fonts_changed_p)
16087 {
16088 /* Is it always sure that the display agrees with lines in
16089 the current matrix? I don't think so, so we mark rows
16090 displayed invalid in the current matrix by setting their
16091 enabled_p flag to zero. */
16092 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16093 if (display_line (&it))
16094 last_text_row_at_end = it.glyph_row - 1;
16095 }
16096 }
16097
16098 /* Update window_end_pos and window_end_vpos. */
16099 if (first_unchanged_at_end_row
16100 && !last_text_row_at_end)
16101 {
16102 /* Window end line if one of the preserved rows from the current
16103 matrix. Set row to the last row displaying text in current
16104 matrix starting at first_unchanged_at_end_row, after
16105 scrolling. */
16106 xassert (first_unchanged_at_end_row->displays_text_p);
16107 row = find_last_row_displaying_text (w->current_matrix, &it,
16108 first_unchanged_at_end_row);
16109 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16110
16111 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16112 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16113 w->window_end_vpos
16114 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16115 xassert (w->window_end_bytepos >= 0);
16116 IF_DEBUG (debug_method_add (w, "A"));
16117 }
16118 else if (last_text_row_at_end)
16119 {
16120 w->window_end_pos
16121 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16122 w->window_end_bytepos
16123 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16124 w->window_end_vpos
16125 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16126 xassert (w->window_end_bytepos >= 0);
16127 IF_DEBUG (debug_method_add (w, "B"));
16128 }
16129 else if (last_text_row)
16130 {
16131 /* We have displayed either to the end of the window or at the
16132 end of the window, i.e. the last row with text is to be found
16133 in the desired matrix. */
16134 w->window_end_pos
16135 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16136 w->window_end_bytepos
16137 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16138 w->window_end_vpos
16139 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16140 xassert (w->window_end_bytepos >= 0);
16141 }
16142 else if (first_unchanged_at_end_row == NULL
16143 && last_text_row == NULL
16144 && last_text_row_at_end == NULL)
16145 {
16146 /* Displayed to end of window, but no line containing text was
16147 displayed. Lines were deleted at the end of the window. */
16148 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16149 int vpos = XFASTINT (w->window_end_vpos);
16150 struct glyph_row *current_row = current_matrix->rows + vpos;
16151 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16152
16153 for (row = NULL;
16154 row == NULL && vpos >= first_vpos;
16155 --vpos, --current_row, --desired_row)
16156 {
16157 if (desired_row->enabled_p)
16158 {
16159 if (desired_row->displays_text_p)
16160 row = desired_row;
16161 }
16162 else if (current_row->displays_text_p)
16163 row = current_row;
16164 }
16165
16166 xassert (row != NULL);
16167 w->window_end_vpos = make_number (vpos + 1);
16168 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16169 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16170 xassert (w->window_end_bytepos >= 0);
16171 IF_DEBUG (debug_method_add (w, "C"));
16172 }
16173 else
16174 abort ();
16175
16176 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16177 debug_end_vpos = XFASTINT (w->window_end_vpos));
16178
16179 /* Record that display has not been completed. */
16180 w->window_end_valid = Qnil;
16181 w->desired_matrix->no_scrolling_p = 1;
16182 return 3;
16183
16184 #undef GIVE_UP
16185 }
16186
16187
16188 \f
16189 /***********************************************************************
16190 More debugging support
16191 ***********************************************************************/
16192
16193 #if GLYPH_DEBUG
16194
16195 void dump_glyph_row (struct glyph_row *, int, int);
16196 void dump_glyph_matrix (struct glyph_matrix *, int);
16197 void dump_glyph (struct glyph_row *, struct glyph *, int);
16198
16199
16200 /* Dump the contents of glyph matrix MATRIX on stderr.
16201
16202 GLYPHS 0 means don't show glyph contents.
16203 GLYPHS 1 means show glyphs in short form
16204 GLYPHS > 1 means show glyphs in long form. */
16205
16206 void
16207 dump_glyph_matrix (matrix, glyphs)
16208 struct glyph_matrix *matrix;
16209 int glyphs;
16210 {
16211 int i;
16212 for (i = 0; i < matrix->nrows; ++i)
16213 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16214 }
16215
16216
16217 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16218 the glyph row and area where the glyph comes from. */
16219
16220 void
16221 dump_glyph (row, glyph, area)
16222 struct glyph_row *row;
16223 struct glyph *glyph;
16224 int area;
16225 {
16226 if (glyph->type == CHAR_GLYPH)
16227 {
16228 fprintf (stderr,
16229 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16230 glyph - row->glyphs[TEXT_AREA],
16231 'C',
16232 glyph->charpos,
16233 (BUFFERP (glyph->object)
16234 ? 'B'
16235 : (STRINGP (glyph->object)
16236 ? 'S'
16237 : '-')),
16238 glyph->pixel_width,
16239 glyph->u.ch,
16240 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16241 ? glyph->u.ch
16242 : '.'),
16243 glyph->face_id,
16244 glyph->left_box_line_p,
16245 glyph->right_box_line_p);
16246 }
16247 else if (glyph->type == STRETCH_GLYPH)
16248 {
16249 fprintf (stderr,
16250 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16251 glyph - row->glyphs[TEXT_AREA],
16252 'S',
16253 glyph->charpos,
16254 (BUFFERP (glyph->object)
16255 ? 'B'
16256 : (STRINGP (glyph->object)
16257 ? 'S'
16258 : '-')),
16259 glyph->pixel_width,
16260 0,
16261 '.',
16262 glyph->face_id,
16263 glyph->left_box_line_p,
16264 glyph->right_box_line_p);
16265 }
16266 else if (glyph->type == IMAGE_GLYPH)
16267 {
16268 fprintf (stderr,
16269 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16270 glyph - row->glyphs[TEXT_AREA],
16271 'I',
16272 glyph->charpos,
16273 (BUFFERP (glyph->object)
16274 ? 'B'
16275 : (STRINGP (glyph->object)
16276 ? 'S'
16277 : '-')),
16278 glyph->pixel_width,
16279 glyph->u.img_id,
16280 '.',
16281 glyph->face_id,
16282 glyph->left_box_line_p,
16283 glyph->right_box_line_p);
16284 }
16285 else if (glyph->type == COMPOSITE_GLYPH)
16286 {
16287 fprintf (stderr,
16288 " %5d %4c %6d %c %3d 0x%05x",
16289 glyph - row->glyphs[TEXT_AREA],
16290 '+',
16291 glyph->charpos,
16292 (BUFFERP (glyph->object)
16293 ? 'B'
16294 : (STRINGP (glyph->object)
16295 ? 'S'
16296 : '-')),
16297 glyph->pixel_width,
16298 glyph->u.cmp.id);
16299 if (glyph->u.cmp.automatic)
16300 fprintf (stderr,
16301 "[%d-%d]",
16302 glyph->slice.cmp.from, glyph->slice.cmp.to);
16303 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16304 glyph->face_id,
16305 glyph->left_box_line_p,
16306 glyph->right_box_line_p);
16307 }
16308 }
16309
16310
16311 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16312 GLYPHS 0 means don't show glyph contents.
16313 GLYPHS 1 means show glyphs in short form
16314 GLYPHS > 1 means show glyphs in long form. */
16315
16316 void
16317 dump_glyph_row (row, vpos, glyphs)
16318 struct glyph_row *row;
16319 int vpos, glyphs;
16320 {
16321 if (glyphs != 1)
16322 {
16323 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16324 fprintf (stderr, "======================================================================\n");
16325
16326 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16327 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16328 vpos,
16329 MATRIX_ROW_START_CHARPOS (row),
16330 MATRIX_ROW_END_CHARPOS (row),
16331 row->used[TEXT_AREA],
16332 row->contains_overlapping_glyphs_p,
16333 row->enabled_p,
16334 row->truncated_on_left_p,
16335 row->truncated_on_right_p,
16336 row->continued_p,
16337 MATRIX_ROW_CONTINUATION_LINE_P (row),
16338 row->displays_text_p,
16339 row->ends_at_zv_p,
16340 row->fill_line_p,
16341 row->ends_in_middle_of_char_p,
16342 row->starts_in_middle_of_char_p,
16343 row->mouse_face_p,
16344 row->x,
16345 row->y,
16346 row->pixel_width,
16347 row->height,
16348 row->visible_height,
16349 row->ascent,
16350 row->phys_ascent);
16351 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16352 row->end.overlay_string_index,
16353 row->continuation_lines_width);
16354 fprintf (stderr, "%9d %5d\n",
16355 CHARPOS (row->start.string_pos),
16356 CHARPOS (row->end.string_pos));
16357 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16358 row->end.dpvec_index);
16359 }
16360
16361 if (glyphs > 1)
16362 {
16363 int area;
16364
16365 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16366 {
16367 struct glyph *glyph = row->glyphs[area];
16368 struct glyph *glyph_end = glyph + row->used[area];
16369
16370 /* Glyph for a line end in text. */
16371 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16372 ++glyph_end;
16373
16374 if (glyph < glyph_end)
16375 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16376
16377 for (; glyph < glyph_end; ++glyph)
16378 dump_glyph (row, glyph, area);
16379 }
16380 }
16381 else if (glyphs == 1)
16382 {
16383 int area;
16384
16385 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16386 {
16387 char *s = (char *) alloca (row->used[area] + 1);
16388 int i;
16389
16390 for (i = 0; i < row->used[area]; ++i)
16391 {
16392 struct glyph *glyph = row->glyphs[area] + i;
16393 if (glyph->type == CHAR_GLYPH
16394 && glyph->u.ch < 0x80
16395 && glyph->u.ch >= ' ')
16396 s[i] = glyph->u.ch;
16397 else
16398 s[i] = '.';
16399 }
16400
16401 s[i] = '\0';
16402 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16403 }
16404 }
16405 }
16406
16407
16408 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16409 Sdump_glyph_matrix, 0, 1, "p",
16410 doc: /* Dump the current matrix of the selected window to stderr.
16411 Shows contents of glyph row structures. With non-nil
16412 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16413 glyphs in short form, otherwise show glyphs in long form. */)
16414 (Lisp_Object glyphs)
16415 {
16416 struct window *w = XWINDOW (selected_window);
16417 struct buffer *buffer = XBUFFER (w->buffer);
16418
16419 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16420 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16421 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16422 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16423 fprintf (stderr, "=============================================\n");
16424 dump_glyph_matrix (w->current_matrix,
16425 NILP (glyphs) ? 0 : XINT (glyphs));
16426 return Qnil;
16427 }
16428
16429
16430 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16431 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16432 (void)
16433 {
16434 struct frame *f = XFRAME (selected_frame);
16435 dump_glyph_matrix (f->current_matrix, 1);
16436 return Qnil;
16437 }
16438
16439
16440 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16441 doc: /* Dump glyph row ROW to stderr.
16442 GLYPH 0 means don't dump glyphs.
16443 GLYPH 1 means dump glyphs in short form.
16444 GLYPH > 1 or omitted means dump glyphs in long form. */)
16445 (Lisp_Object row, Lisp_Object glyphs)
16446 {
16447 struct glyph_matrix *matrix;
16448 int vpos;
16449
16450 CHECK_NUMBER (row);
16451 matrix = XWINDOW (selected_window)->current_matrix;
16452 vpos = XINT (row);
16453 if (vpos >= 0 && vpos < matrix->nrows)
16454 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16455 vpos,
16456 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16457 return Qnil;
16458 }
16459
16460
16461 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16462 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16463 GLYPH 0 means don't dump glyphs.
16464 GLYPH 1 means dump glyphs in short form.
16465 GLYPH > 1 or omitted means dump glyphs in long form. */)
16466 (Lisp_Object row, Lisp_Object glyphs)
16467 {
16468 struct frame *sf = SELECTED_FRAME ();
16469 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16470 int vpos;
16471
16472 CHECK_NUMBER (row);
16473 vpos = XINT (row);
16474 if (vpos >= 0 && vpos < m->nrows)
16475 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16476 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16477 return Qnil;
16478 }
16479
16480
16481 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16482 doc: /* Toggle tracing of redisplay.
16483 With ARG, turn tracing on if and only if ARG is positive. */)
16484 (Lisp_Object arg)
16485 {
16486 if (NILP (arg))
16487 trace_redisplay_p = !trace_redisplay_p;
16488 else
16489 {
16490 arg = Fprefix_numeric_value (arg);
16491 trace_redisplay_p = XINT (arg) > 0;
16492 }
16493
16494 return Qnil;
16495 }
16496
16497
16498 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16499 doc: /* Like `format', but print result to stderr.
16500 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16501 (size_t nargs, Lisp_Object *args)
16502 {
16503 Lisp_Object s = Fformat (nargs, args);
16504 fprintf (stderr, "%s", SDATA (s));
16505 return Qnil;
16506 }
16507
16508 #endif /* GLYPH_DEBUG */
16509
16510
16511 \f
16512 /***********************************************************************
16513 Building Desired Matrix Rows
16514 ***********************************************************************/
16515
16516 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16517 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16518
16519 static struct glyph_row *
16520 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16521 {
16522 struct frame *f = XFRAME (WINDOW_FRAME (w));
16523 struct buffer *buffer = XBUFFER (w->buffer);
16524 struct buffer *old = current_buffer;
16525 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16526 int arrow_len = SCHARS (overlay_arrow_string);
16527 const unsigned char *arrow_end = arrow_string + arrow_len;
16528 const unsigned char *p;
16529 struct it it;
16530 int multibyte_p;
16531 int n_glyphs_before;
16532
16533 set_buffer_temp (buffer);
16534 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16535 it.glyph_row->used[TEXT_AREA] = 0;
16536 SET_TEXT_POS (it.position, 0, 0);
16537
16538 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
16539 p = arrow_string;
16540 while (p < arrow_end)
16541 {
16542 Lisp_Object face, ilisp;
16543
16544 /* Get the next character. */
16545 if (multibyte_p)
16546 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16547 else
16548 {
16549 it.c = it.char_to_display = *p, it.len = 1;
16550 if (! ASCII_CHAR_P (it.c))
16551 it.char_to_display = BYTE8_TO_CHAR (it.c);
16552 }
16553 p += it.len;
16554
16555 /* Get its face. */
16556 ilisp = make_number (p - arrow_string);
16557 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16558 it.face_id = compute_char_face (f, it.char_to_display, face);
16559
16560 /* Compute its width, get its glyphs. */
16561 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16562 SET_TEXT_POS (it.position, -1, -1);
16563 PRODUCE_GLYPHS (&it);
16564
16565 /* If this character doesn't fit any more in the line, we have
16566 to remove some glyphs. */
16567 if (it.current_x > it.last_visible_x)
16568 {
16569 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16570 break;
16571 }
16572 }
16573
16574 set_buffer_temp (old);
16575 return it.glyph_row;
16576 }
16577
16578
16579 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16580 glyphs are only inserted for terminal frames since we can't really
16581 win with truncation glyphs when partially visible glyphs are
16582 involved. Which glyphs to insert is determined by
16583 produce_special_glyphs. */
16584
16585 static void
16586 insert_left_trunc_glyphs (struct it *it)
16587 {
16588 struct it truncate_it;
16589 struct glyph *from, *end, *to, *toend;
16590
16591 xassert (!FRAME_WINDOW_P (it->f));
16592
16593 /* Get the truncation glyphs. */
16594 truncate_it = *it;
16595 truncate_it.current_x = 0;
16596 truncate_it.face_id = DEFAULT_FACE_ID;
16597 truncate_it.glyph_row = &scratch_glyph_row;
16598 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16599 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16600 truncate_it.object = make_number (0);
16601 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16602
16603 /* Overwrite glyphs from IT with truncation glyphs. */
16604 if (!it->glyph_row->reversed_p)
16605 {
16606 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16607 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16608 to = it->glyph_row->glyphs[TEXT_AREA];
16609 toend = to + it->glyph_row->used[TEXT_AREA];
16610
16611 while (from < end)
16612 *to++ = *from++;
16613
16614 /* There may be padding glyphs left over. Overwrite them too. */
16615 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16616 {
16617 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16618 while (from < end)
16619 *to++ = *from++;
16620 }
16621
16622 if (to > toend)
16623 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16624 }
16625 else
16626 {
16627 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16628 that back to front. */
16629 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16630 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16631 toend = it->glyph_row->glyphs[TEXT_AREA];
16632 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16633
16634 while (from >= end && to >= toend)
16635 *to-- = *from--;
16636 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16637 {
16638 from =
16639 truncate_it.glyph_row->glyphs[TEXT_AREA]
16640 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16641 while (from >= end && to >= toend)
16642 *to-- = *from--;
16643 }
16644 if (from >= end)
16645 {
16646 /* Need to free some room before prepending additional
16647 glyphs. */
16648 int move_by = from - end + 1;
16649 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16650 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16651
16652 for ( ; g >= g0; g--)
16653 g[move_by] = *g;
16654 while (from >= end)
16655 *to-- = *from--;
16656 it->glyph_row->used[TEXT_AREA] += move_by;
16657 }
16658 }
16659 }
16660
16661
16662 /* Compute the pixel height and width of IT->glyph_row.
16663
16664 Most of the time, ascent and height of a display line will be equal
16665 to the max_ascent and max_height values of the display iterator
16666 structure. This is not the case if
16667
16668 1. We hit ZV without displaying anything. In this case, max_ascent
16669 and max_height will be zero.
16670
16671 2. We have some glyphs that don't contribute to the line height.
16672 (The glyph row flag contributes_to_line_height_p is for future
16673 pixmap extensions).
16674
16675 The first case is easily covered by using default values because in
16676 these cases, the line height does not really matter, except that it
16677 must not be zero. */
16678
16679 static void
16680 compute_line_metrics (struct it *it)
16681 {
16682 struct glyph_row *row = it->glyph_row;
16683
16684 if (FRAME_WINDOW_P (it->f))
16685 {
16686 int i, min_y, max_y;
16687
16688 /* The line may consist of one space only, that was added to
16689 place the cursor on it. If so, the row's height hasn't been
16690 computed yet. */
16691 if (row->height == 0)
16692 {
16693 if (it->max_ascent + it->max_descent == 0)
16694 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16695 row->ascent = it->max_ascent;
16696 row->height = it->max_ascent + it->max_descent;
16697 row->phys_ascent = it->max_phys_ascent;
16698 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16699 row->extra_line_spacing = it->max_extra_line_spacing;
16700 }
16701
16702 /* Compute the width of this line. */
16703 row->pixel_width = row->x;
16704 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16705 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16706
16707 xassert (row->pixel_width >= 0);
16708 xassert (row->ascent >= 0 && row->height > 0);
16709
16710 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16711 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16712
16713 /* If first line's physical ascent is larger than its logical
16714 ascent, use the physical ascent, and make the row taller.
16715 This makes accented characters fully visible. */
16716 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16717 && row->phys_ascent > row->ascent)
16718 {
16719 row->height += row->phys_ascent - row->ascent;
16720 row->ascent = row->phys_ascent;
16721 }
16722
16723 /* Compute how much of the line is visible. */
16724 row->visible_height = row->height;
16725
16726 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16727 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16728
16729 if (row->y < min_y)
16730 row->visible_height -= min_y - row->y;
16731 if (row->y + row->height > max_y)
16732 row->visible_height -= row->y + row->height - max_y;
16733 }
16734 else
16735 {
16736 row->pixel_width = row->used[TEXT_AREA];
16737 if (row->continued_p)
16738 row->pixel_width -= it->continuation_pixel_width;
16739 else if (row->truncated_on_right_p)
16740 row->pixel_width -= it->truncation_pixel_width;
16741 row->ascent = row->phys_ascent = 0;
16742 row->height = row->phys_height = row->visible_height = 1;
16743 row->extra_line_spacing = 0;
16744 }
16745
16746 /* Compute a hash code for this row. */
16747 {
16748 int area, i;
16749 row->hash = 0;
16750 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16751 for (i = 0; i < row->used[area]; ++i)
16752 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16753 + row->glyphs[area][i].u.val
16754 + row->glyphs[area][i].face_id
16755 + row->glyphs[area][i].padding_p
16756 + (row->glyphs[area][i].type << 2));
16757 }
16758
16759 it->max_ascent = it->max_descent = 0;
16760 it->max_phys_ascent = it->max_phys_descent = 0;
16761 }
16762
16763
16764 /* Append one space to the glyph row of iterator IT if doing a
16765 window-based redisplay. The space has the same face as
16766 IT->face_id. Value is non-zero if a space was added.
16767
16768 This function is called to make sure that there is always one glyph
16769 at the end of a glyph row that the cursor can be set on under
16770 window-systems. (If there weren't such a glyph we would not know
16771 how wide and tall a box cursor should be displayed).
16772
16773 At the same time this space let's a nicely handle clearing to the
16774 end of the line if the row ends in italic text. */
16775
16776 static int
16777 append_space_for_newline (struct it *it, int default_face_p)
16778 {
16779 if (FRAME_WINDOW_P (it->f))
16780 {
16781 int n = it->glyph_row->used[TEXT_AREA];
16782
16783 if (it->glyph_row->glyphs[TEXT_AREA] + n
16784 < it->glyph_row->glyphs[1 + TEXT_AREA])
16785 {
16786 /* Save some values that must not be changed.
16787 Must save IT->c and IT->len because otherwise
16788 ITERATOR_AT_END_P wouldn't work anymore after
16789 append_space_for_newline has been called. */
16790 enum display_element_type saved_what = it->what;
16791 int saved_c = it->c, saved_len = it->len;
16792 int saved_char_to_display = it->char_to_display;
16793 int saved_x = it->current_x;
16794 int saved_face_id = it->face_id;
16795 struct text_pos saved_pos;
16796 Lisp_Object saved_object;
16797 struct face *face;
16798
16799 saved_object = it->object;
16800 saved_pos = it->position;
16801
16802 it->what = IT_CHARACTER;
16803 memset (&it->position, 0, sizeof it->position);
16804 it->object = make_number (0);
16805 it->c = it->char_to_display = ' ';
16806 it->len = 1;
16807
16808 if (default_face_p)
16809 it->face_id = DEFAULT_FACE_ID;
16810 else if (it->face_before_selective_p)
16811 it->face_id = it->saved_face_id;
16812 face = FACE_FROM_ID (it->f, it->face_id);
16813 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16814
16815 PRODUCE_GLYPHS (it);
16816
16817 it->override_ascent = -1;
16818 it->constrain_row_ascent_descent_p = 0;
16819 it->current_x = saved_x;
16820 it->object = saved_object;
16821 it->position = saved_pos;
16822 it->what = saved_what;
16823 it->face_id = saved_face_id;
16824 it->len = saved_len;
16825 it->c = saved_c;
16826 it->char_to_display = saved_char_to_display;
16827 return 1;
16828 }
16829 }
16830
16831 return 0;
16832 }
16833
16834
16835 /* Extend the face of the last glyph in the text area of IT->glyph_row
16836 to the end of the display line. Called from display_line. If the
16837 glyph row is empty, add a space glyph to it so that we know the
16838 face to draw. Set the glyph row flag fill_line_p. If the glyph
16839 row is R2L, prepend a stretch glyph to cover the empty space to the
16840 left of the leftmost glyph. */
16841
16842 static void
16843 extend_face_to_end_of_line (struct it *it)
16844 {
16845 struct face *face;
16846 struct frame *f = it->f;
16847
16848 /* If line is already filled, do nothing. Non window-system frames
16849 get a grace of one more ``pixel'' because their characters are
16850 1-``pixel'' wide, so they hit the equality too early. This grace
16851 is needed only for R2L rows that are not continued, to produce
16852 one extra blank where we could display the cursor. */
16853 if (it->current_x >= it->last_visible_x
16854 + (!FRAME_WINDOW_P (f)
16855 && it->glyph_row->reversed_p
16856 && !it->glyph_row->continued_p))
16857 return;
16858
16859 /* Face extension extends the background and box of IT->face_id
16860 to the end of the line. If the background equals the background
16861 of the frame, we don't have to do anything. */
16862 if (it->face_before_selective_p)
16863 face = FACE_FROM_ID (f, it->saved_face_id);
16864 else
16865 face = FACE_FROM_ID (f, it->face_id);
16866
16867 if (FRAME_WINDOW_P (f)
16868 && it->glyph_row->displays_text_p
16869 && face->box == FACE_NO_BOX
16870 && face->background == FRAME_BACKGROUND_PIXEL (f)
16871 && !face->stipple
16872 && !it->glyph_row->reversed_p)
16873 return;
16874
16875 /* Set the glyph row flag indicating that the face of the last glyph
16876 in the text area has to be drawn to the end of the text area. */
16877 it->glyph_row->fill_line_p = 1;
16878
16879 /* If current character of IT is not ASCII, make sure we have the
16880 ASCII face. This will be automatically undone the next time
16881 get_next_display_element returns a multibyte character. Note
16882 that the character will always be single byte in unibyte
16883 text. */
16884 if (!ASCII_CHAR_P (it->c))
16885 {
16886 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16887 }
16888
16889 if (FRAME_WINDOW_P (f))
16890 {
16891 /* If the row is empty, add a space with the current face of IT,
16892 so that we know which face to draw. */
16893 if (it->glyph_row->used[TEXT_AREA] == 0)
16894 {
16895 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16896 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16897 it->glyph_row->used[TEXT_AREA] = 1;
16898 }
16899 #ifdef HAVE_WINDOW_SYSTEM
16900 if (it->glyph_row->reversed_p)
16901 {
16902 /* Prepend a stretch glyph to the row, such that the
16903 rightmost glyph will be drawn flushed all the way to the
16904 right margin of the window. The stretch glyph that will
16905 occupy the empty space, if any, to the left of the
16906 glyphs. */
16907 struct font *font = face->font ? face->font : FRAME_FONT (f);
16908 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16909 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16910 struct glyph *g;
16911 int row_width, stretch_ascent, stretch_width;
16912 struct text_pos saved_pos;
16913 int saved_face_id, saved_avoid_cursor;
16914
16915 for (row_width = 0, g = row_start; g < row_end; g++)
16916 row_width += g->pixel_width;
16917 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16918 if (stretch_width > 0)
16919 {
16920 stretch_ascent =
16921 (((it->ascent + it->descent)
16922 * FONT_BASE (font)) / FONT_HEIGHT (font));
16923 saved_pos = it->position;
16924 memset (&it->position, 0, sizeof it->position);
16925 saved_avoid_cursor = it->avoid_cursor_p;
16926 it->avoid_cursor_p = 1;
16927 saved_face_id = it->face_id;
16928 /* The last row's stretch glyph should get the default
16929 face, to avoid painting the rest of the window with
16930 the region face, if the region ends at ZV. */
16931 if (it->glyph_row->ends_at_zv_p)
16932 it->face_id = DEFAULT_FACE_ID;
16933 else
16934 it->face_id = face->id;
16935 append_stretch_glyph (it, make_number (0), stretch_width,
16936 it->ascent + it->descent, stretch_ascent);
16937 it->position = saved_pos;
16938 it->avoid_cursor_p = saved_avoid_cursor;
16939 it->face_id = saved_face_id;
16940 }
16941 }
16942 #endif /* HAVE_WINDOW_SYSTEM */
16943 }
16944 else
16945 {
16946 /* Save some values that must not be changed. */
16947 int saved_x = it->current_x;
16948 struct text_pos saved_pos;
16949 Lisp_Object saved_object;
16950 enum display_element_type saved_what = it->what;
16951 int saved_face_id = it->face_id;
16952
16953 saved_object = it->object;
16954 saved_pos = it->position;
16955
16956 it->what = IT_CHARACTER;
16957 memset (&it->position, 0, sizeof it->position);
16958 it->object = make_number (0);
16959 it->c = it->char_to_display = ' ';
16960 it->len = 1;
16961 /* The last row's blank glyphs should get the default face, to
16962 avoid painting the rest of the window with the region face,
16963 if the region ends at ZV. */
16964 if (it->glyph_row->ends_at_zv_p)
16965 it->face_id = DEFAULT_FACE_ID;
16966 else
16967 it->face_id = face->id;
16968
16969 PRODUCE_GLYPHS (it);
16970
16971 while (it->current_x <= it->last_visible_x)
16972 PRODUCE_GLYPHS (it);
16973
16974 /* Don't count these blanks really. It would let us insert a left
16975 truncation glyph below and make us set the cursor on them, maybe. */
16976 it->current_x = saved_x;
16977 it->object = saved_object;
16978 it->position = saved_pos;
16979 it->what = saved_what;
16980 it->face_id = saved_face_id;
16981 }
16982 }
16983
16984
16985 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16986 trailing whitespace. */
16987
16988 static int
16989 trailing_whitespace_p (EMACS_INT charpos)
16990 {
16991 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
16992 int c = 0;
16993
16994 while (bytepos < ZV_BYTE
16995 && (c = FETCH_CHAR (bytepos),
16996 c == ' ' || c == '\t'))
16997 ++bytepos;
16998
16999 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17000 {
17001 if (bytepos != PT_BYTE)
17002 return 1;
17003 }
17004 return 0;
17005 }
17006
17007
17008 /* Highlight trailing whitespace, if any, in ROW. */
17009
17010 static void
17011 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17012 {
17013 int used = row->used[TEXT_AREA];
17014
17015 if (used)
17016 {
17017 struct glyph *start = row->glyphs[TEXT_AREA];
17018 struct glyph *glyph = start + used - 1;
17019
17020 if (row->reversed_p)
17021 {
17022 /* Right-to-left rows need to be processed in the opposite
17023 direction, so swap the edge pointers. */
17024 glyph = start;
17025 start = row->glyphs[TEXT_AREA] + used - 1;
17026 }
17027
17028 /* Skip over glyphs inserted to display the cursor at the
17029 end of a line, for extending the face of the last glyph
17030 to the end of the line on terminals, and for truncation
17031 and continuation glyphs. */
17032 if (!row->reversed_p)
17033 {
17034 while (glyph >= start
17035 && glyph->type == CHAR_GLYPH
17036 && INTEGERP (glyph->object))
17037 --glyph;
17038 }
17039 else
17040 {
17041 while (glyph <= start
17042 && glyph->type == CHAR_GLYPH
17043 && INTEGERP (glyph->object))
17044 ++glyph;
17045 }
17046
17047 /* If last glyph is a space or stretch, and it's trailing
17048 whitespace, set the face of all trailing whitespace glyphs in
17049 IT->glyph_row to `trailing-whitespace'. */
17050 if ((row->reversed_p ? glyph <= start : glyph >= start)
17051 && BUFFERP (glyph->object)
17052 && (glyph->type == STRETCH_GLYPH
17053 || (glyph->type == CHAR_GLYPH
17054 && glyph->u.ch == ' '))
17055 && trailing_whitespace_p (glyph->charpos))
17056 {
17057 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17058 if (face_id < 0)
17059 return;
17060
17061 if (!row->reversed_p)
17062 {
17063 while (glyph >= start
17064 && BUFFERP (glyph->object)
17065 && (glyph->type == STRETCH_GLYPH
17066 || (glyph->type == CHAR_GLYPH
17067 && glyph->u.ch == ' ')))
17068 (glyph--)->face_id = face_id;
17069 }
17070 else
17071 {
17072 while (glyph <= start
17073 && BUFFERP (glyph->object)
17074 && (glyph->type == STRETCH_GLYPH
17075 || (glyph->type == CHAR_GLYPH
17076 && glyph->u.ch == ' ')))
17077 (glyph++)->face_id = face_id;
17078 }
17079 }
17080 }
17081 }
17082
17083
17084 /* Value is non-zero if glyph row ROW should be
17085 used to hold the cursor. */
17086
17087 static int
17088 cursor_row_p (struct glyph_row *row)
17089 {
17090 int result = 1;
17091
17092 if (PT == CHARPOS (row->end.pos))
17093 {
17094 /* Suppose the row ends on a string.
17095 Unless the row is continued, that means it ends on a newline
17096 in the string. If it's anything other than a display string
17097 (e.g. a before-string from an overlay), we don't want the
17098 cursor there. (This heuristic seems to give the optimal
17099 behavior for the various types of multi-line strings.) */
17100 if (CHARPOS (row->end.string_pos) >= 0)
17101 {
17102 if (row->continued_p)
17103 result = 1;
17104 else
17105 {
17106 /* Check for `display' property. */
17107 struct glyph *beg = row->glyphs[TEXT_AREA];
17108 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17109 struct glyph *glyph;
17110
17111 result = 0;
17112 for (glyph = end; glyph >= beg; --glyph)
17113 if (STRINGP (glyph->object))
17114 {
17115 Lisp_Object prop
17116 = Fget_char_property (make_number (PT),
17117 Qdisplay, Qnil);
17118 result =
17119 (!NILP (prop)
17120 && display_prop_string_p (prop, glyph->object));
17121 break;
17122 }
17123 }
17124 }
17125 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17126 {
17127 /* If the row ends in middle of a real character,
17128 and the line is continued, we want the cursor here.
17129 That's because CHARPOS (ROW->end.pos) would equal
17130 PT if PT is before the character. */
17131 if (!row->ends_in_ellipsis_p)
17132 result = row->continued_p;
17133 else
17134 /* If the row ends in an ellipsis, then
17135 CHARPOS (ROW->end.pos) will equal point after the
17136 invisible text. We want that position to be displayed
17137 after the ellipsis. */
17138 result = 0;
17139 }
17140 /* If the row ends at ZV, display the cursor at the end of that
17141 row instead of at the start of the row below. */
17142 else if (row->ends_at_zv_p)
17143 result = 1;
17144 else
17145 result = 0;
17146 }
17147
17148 return result;
17149 }
17150
17151 \f
17152
17153 /* Push the display property PROP so that it will be rendered at the
17154 current position in IT. Return 1 if PROP was successfully pushed,
17155 0 otherwise. */
17156
17157 static int
17158 push_display_prop (struct it *it, Lisp_Object prop)
17159 {
17160 push_it (it);
17161
17162 if (STRINGP (prop))
17163 {
17164 if (SCHARS (prop) == 0)
17165 {
17166 pop_it (it);
17167 return 0;
17168 }
17169
17170 it->string = prop;
17171 it->multibyte_p = STRING_MULTIBYTE (it->string);
17172 it->current.overlay_string_index = -1;
17173 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17174 it->end_charpos = it->string_nchars = SCHARS (it->string);
17175 it->method = GET_FROM_STRING;
17176 it->stop_charpos = 0;
17177 }
17178 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17179 {
17180 it->method = GET_FROM_STRETCH;
17181 it->object = prop;
17182 }
17183 #ifdef HAVE_WINDOW_SYSTEM
17184 else if (IMAGEP (prop))
17185 {
17186 it->what = IT_IMAGE;
17187 it->image_id = lookup_image (it->f, prop);
17188 it->method = GET_FROM_IMAGE;
17189 }
17190 #endif /* HAVE_WINDOW_SYSTEM */
17191 else
17192 {
17193 pop_it (it); /* bogus display property, give up */
17194 return 0;
17195 }
17196
17197 return 1;
17198 }
17199
17200 /* Return the character-property PROP at the current position in IT. */
17201
17202 static Lisp_Object
17203 get_it_property (struct it *it, Lisp_Object prop)
17204 {
17205 Lisp_Object position;
17206
17207 if (STRINGP (it->object))
17208 position = make_number (IT_STRING_CHARPOS (*it));
17209 else if (BUFFERP (it->object))
17210 position = make_number (IT_CHARPOS (*it));
17211 else
17212 return Qnil;
17213
17214 return Fget_char_property (position, prop, it->object);
17215 }
17216
17217 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17218
17219 static void
17220 handle_line_prefix (struct it *it)
17221 {
17222 Lisp_Object prefix;
17223 if (it->continuation_lines_width > 0)
17224 {
17225 prefix = get_it_property (it, Qwrap_prefix);
17226 if (NILP (prefix))
17227 prefix = Vwrap_prefix;
17228 }
17229 else
17230 {
17231 prefix = get_it_property (it, Qline_prefix);
17232 if (NILP (prefix))
17233 prefix = Vline_prefix;
17234 }
17235 if (! NILP (prefix) && push_display_prop (it, prefix))
17236 {
17237 /* If the prefix is wider than the window, and we try to wrap
17238 it, it would acquire its own wrap prefix, and so on till the
17239 iterator stack overflows. So, don't wrap the prefix. */
17240 it->line_wrap = TRUNCATE;
17241 it->avoid_cursor_p = 1;
17242 }
17243 }
17244
17245 \f
17246
17247 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17248 only for R2L lines from display_line, when it decides that too many
17249 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17250 continued. */
17251 static void
17252 unproduce_glyphs (struct it *it, int n)
17253 {
17254 struct glyph *glyph, *end;
17255
17256 xassert (it->glyph_row);
17257 xassert (it->glyph_row->reversed_p);
17258 xassert (it->area == TEXT_AREA);
17259 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17260
17261 if (n > it->glyph_row->used[TEXT_AREA])
17262 n = it->glyph_row->used[TEXT_AREA];
17263 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17264 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17265 for ( ; glyph < end; glyph++)
17266 glyph[-n] = *glyph;
17267 }
17268
17269 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17270 and ROW->maxpos. */
17271 static void
17272 find_row_edges (struct it *it, struct glyph_row *row,
17273 EMACS_INT min_pos, EMACS_INT min_bpos,
17274 EMACS_INT max_pos, EMACS_INT max_bpos)
17275 {
17276 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17277 lines' rows is implemented for bidi-reordered rows. */
17278
17279 /* ROW->minpos is the value of min_pos, the minimal buffer position
17280 we have in ROW. */
17281 if (min_pos <= ZV)
17282 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17283 else
17284 /* We didn't find _any_ valid buffer positions in any of the
17285 glyphs, so we must trust the iterator's computed positions. */
17286 row->minpos = row->start.pos;
17287 if (max_pos <= 0)
17288 {
17289 max_pos = CHARPOS (it->current.pos);
17290 max_bpos = BYTEPOS (it->current.pos);
17291 }
17292
17293 /* Here are the various use-cases for ending the row, and the
17294 corresponding values for ROW->maxpos:
17295
17296 Line ends in a newline from buffer eol_pos + 1
17297 Line is continued from buffer max_pos + 1
17298 Line is truncated on right it->current.pos
17299 Line ends in a newline from string max_pos
17300 Line is continued from string max_pos
17301 Line is continued from display vector max_pos
17302 Line is entirely from a string min_pos == max_pos
17303 Line is entirely from a display vector min_pos == max_pos
17304 Line that ends at ZV ZV
17305
17306 If you discover other use-cases, please add them here as
17307 appropriate. */
17308 if (row->ends_at_zv_p)
17309 row->maxpos = it->current.pos;
17310 else if (row->used[TEXT_AREA])
17311 {
17312 if (row->ends_in_newline_from_string_p)
17313 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17314 else if (CHARPOS (it->eol_pos) > 0)
17315 SET_TEXT_POS (row->maxpos,
17316 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17317 else if (row->continued_p)
17318 {
17319 /* If max_pos is different from IT's current position, it
17320 means IT->method does not belong to the display element
17321 at max_pos. However, it also means that the display
17322 element at max_pos was displayed in its entirety on this
17323 line, which is equivalent to saying that the next line
17324 starts at the next buffer position. */
17325 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17326 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17327 else
17328 {
17329 INC_BOTH (max_pos, max_bpos);
17330 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17331 }
17332 }
17333 else if (row->truncated_on_right_p)
17334 /* display_line already called reseat_at_next_visible_line_start,
17335 which puts the iterator at the beginning of the next line, in
17336 the logical order. */
17337 row->maxpos = it->current.pos;
17338 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17339 /* A line that is entirely from a string/image/stretch... */
17340 row->maxpos = row->minpos;
17341 else
17342 abort ();
17343 }
17344 else
17345 row->maxpos = it->current.pos;
17346 }
17347
17348 /* Construct the glyph row IT->glyph_row in the desired matrix of
17349 IT->w from text at the current position of IT. See dispextern.h
17350 for an overview of struct it. Value is non-zero if
17351 IT->glyph_row displays text, as opposed to a line displaying ZV
17352 only. */
17353
17354 static int
17355 display_line (struct it *it)
17356 {
17357 struct glyph_row *row = it->glyph_row;
17358 Lisp_Object overlay_arrow_string;
17359 struct it wrap_it;
17360 int may_wrap = 0, wrap_x IF_LINT (= 0);
17361 int wrap_row_used = -1;
17362 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
17363 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
17364 int wrap_row_extra_line_spacing IF_LINT (= 0);
17365 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
17366 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
17367 int cvpos;
17368 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17369 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
17370
17371 /* We always start displaying at hpos zero even if hscrolled. */
17372 xassert (it->hpos == 0 && it->current_x == 0);
17373
17374 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17375 >= it->w->desired_matrix->nrows)
17376 {
17377 it->w->nrows_scale_factor++;
17378 fonts_changed_p = 1;
17379 return 0;
17380 }
17381
17382 /* Is IT->w showing the region? */
17383 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17384
17385 /* Clear the result glyph row and enable it. */
17386 prepare_desired_row (row);
17387
17388 row->y = it->current_y;
17389 row->start = it->start;
17390 row->continuation_lines_width = it->continuation_lines_width;
17391 row->displays_text_p = 1;
17392 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17393 it->starts_in_middle_of_char_p = 0;
17394
17395 /* Arrange the overlays nicely for our purposes. Usually, we call
17396 display_line on only one line at a time, in which case this
17397 can't really hurt too much, or we call it on lines which appear
17398 one after another in the buffer, in which case all calls to
17399 recenter_overlay_lists but the first will be pretty cheap. */
17400 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17401
17402 /* Move over display elements that are not visible because we are
17403 hscrolled. This may stop at an x-position < IT->first_visible_x
17404 if the first glyph is partially visible or if we hit a line end. */
17405 if (it->current_x < it->first_visible_x)
17406 {
17407 this_line_min_pos = row->start.pos;
17408 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17409 MOVE_TO_POS | MOVE_TO_X);
17410 /* Record the smallest positions seen while we moved over
17411 display elements that are not visible. This is needed by
17412 redisplay_internal for optimizing the case where the cursor
17413 stays inside the same line. The rest of this function only
17414 considers positions that are actually displayed, so
17415 RECORD_MAX_MIN_POS will not otherwise record positions that
17416 are hscrolled to the left of the left edge of the window. */
17417 min_pos = CHARPOS (this_line_min_pos);
17418 min_bpos = BYTEPOS (this_line_min_pos);
17419 }
17420 else
17421 {
17422 /* We only do this when not calling `move_it_in_display_line_to'
17423 above, because move_it_in_display_line_to calls
17424 handle_line_prefix itself. */
17425 handle_line_prefix (it);
17426 }
17427
17428 /* Get the initial row height. This is either the height of the
17429 text hscrolled, if there is any, or zero. */
17430 row->ascent = it->max_ascent;
17431 row->height = it->max_ascent + it->max_descent;
17432 row->phys_ascent = it->max_phys_ascent;
17433 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17434 row->extra_line_spacing = it->max_extra_line_spacing;
17435
17436 /* Utility macro to record max and min buffer positions seen until now. */
17437 #define RECORD_MAX_MIN_POS(IT) \
17438 do \
17439 { \
17440 if (IT_CHARPOS (*(IT)) < min_pos) \
17441 { \
17442 min_pos = IT_CHARPOS (*(IT)); \
17443 min_bpos = IT_BYTEPOS (*(IT)); \
17444 } \
17445 if (IT_CHARPOS (*(IT)) > max_pos) \
17446 { \
17447 max_pos = IT_CHARPOS (*(IT)); \
17448 max_bpos = IT_BYTEPOS (*(IT)); \
17449 } \
17450 } \
17451 while (0)
17452
17453 /* Loop generating characters. The loop is left with IT on the next
17454 character to display. */
17455 while (1)
17456 {
17457 int n_glyphs_before, hpos_before, x_before;
17458 int x, nglyphs;
17459 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17460
17461 /* Retrieve the next thing to display. Value is zero if end of
17462 buffer reached. */
17463 if (!get_next_display_element (it))
17464 {
17465 /* Maybe add a space at the end of this line that is used to
17466 display the cursor there under X. Set the charpos of the
17467 first glyph of blank lines not corresponding to any text
17468 to -1. */
17469 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17470 row->exact_window_width_line_p = 1;
17471 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17472 || row->used[TEXT_AREA] == 0)
17473 {
17474 row->glyphs[TEXT_AREA]->charpos = -1;
17475 row->displays_text_p = 0;
17476
17477 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
17478 && (!MINI_WINDOW_P (it->w)
17479 || (minibuf_level && EQ (it->window, minibuf_window))))
17480 row->indicate_empty_line_p = 1;
17481 }
17482
17483 it->continuation_lines_width = 0;
17484 row->ends_at_zv_p = 1;
17485 /* A row that displays right-to-left text must always have
17486 its last face extended all the way to the end of line,
17487 even if this row ends in ZV, because we still write to
17488 the screen left to right. */
17489 if (row->reversed_p)
17490 extend_face_to_end_of_line (it);
17491 break;
17492 }
17493
17494 /* Now, get the metrics of what we want to display. This also
17495 generates glyphs in `row' (which is IT->glyph_row). */
17496 n_glyphs_before = row->used[TEXT_AREA];
17497 x = it->current_x;
17498
17499 /* Remember the line height so far in case the next element doesn't
17500 fit on the line. */
17501 if (it->line_wrap != TRUNCATE)
17502 {
17503 ascent = it->max_ascent;
17504 descent = it->max_descent;
17505 phys_ascent = it->max_phys_ascent;
17506 phys_descent = it->max_phys_descent;
17507
17508 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17509 {
17510 if (IT_DISPLAYING_WHITESPACE (it))
17511 may_wrap = 1;
17512 else if (may_wrap)
17513 {
17514 wrap_it = *it;
17515 wrap_x = x;
17516 wrap_row_used = row->used[TEXT_AREA];
17517 wrap_row_ascent = row->ascent;
17518 wrap_row_height = row->height;
17519 wrap_row_phys_ascent = row->phys_ascent;
17520 wrap_row_phys_height = row->phys_height;
17521 wrap_row_extra_line_spacing = row->extra_line_spacing;
17522 wrap_row_min_pos = min_pos;
17523 wrap_row_min_bpos = min_bpos;
17524 wrap_row_max_pos = max_pos;
17525 wrap_row_max_bpos = max_bpos;
17526 may_wrap = 0;
17527 }
17528 }
17529 }
17530
17531 PRODUCE_GLYPHS (it);
17532
17533 /* If this display element was in marginal areas, continue with
17534 the next one. */
17535 if (it->area != TEXT_AREA)
17536 {
17537 row->ascent = max (row->ascent, it->max_ascent);
17538 row->height = max (row->height, it->max_ascent + it->max_descent);
17539 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17540 row->phys_height = max (row->phys_height,
17541 it->max_phys_ascent + it->max_phys_descent);
17542 row->extra_line_spacing = max (row->extra_line_spacing,
17543 it->max_extra_line_spacing);
17544 set_iterator_to_next (it, 1);
17545 continue;
17546 }
17547
17548 /* Does the display element fit on the line? If we truncate
17549 lines, we should draw past the right edge of the window. If
17550 we don't truncate, we want to stop so that we can display the
17551 continuation glyph before the right margin. If lines are
17552 continued, there are two possible strategies for characters
17553 resulting in more than 1 glyph (e.g. tabs): Display as many
17554 glyphs as possible in this line and leave the rest for the
17555 continuation line, or display the whole element in the next
17556 line. Original redisplay did the former, so we do it also. */
17557 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17558 hpos_before = it->hpos;
17559 x_before = x;
17560
17561 if (/* Not a newline. */
17562 nglyphs > 0
17563 /* Glyphs produced fit entirely in the line. */
17564 && it->current_x < it->last_visible_x)
17565 {
17566 it->hpos += nglyphs;
17567 row->ascent = max (row->ascent, it->max_ascent);
17568 row->height = max (row->height, it->max_ascent + it->max_descent);
17569 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17570 row->phys_height = max (row->phys_height,
17571 it->max_phys_ascent + it->max_phys_descent);
17572 row->extra_line_spacing = max (row->extra_line_spacing,
17573 it->max_extra_line_spacing);
17574 if (it->current_x - it->pixel_width < it->first_visible_x)
17575 row->x = x - it->first_visible_x;
17576 /* Record the maximum and minimum buffer positions seen so
17577 far in glyphs that will be displayed by this row. */
17578 if (it->bidi_p)
17579 RECORD_MAX_MIN_POS (it);
17580 }
17581 else
17582 {
17583 int i, new_x;
17584 struct glyph *glyph;
17585
17586 for (i = 0; i < nglyphs; ++i, x = new_x)
17587 {
17588 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17589 new_x = x + glyph->pixel_width;
17590
17591 if (/* Lines are continued. */
17592 it->line_wrap != TRUNCATE
17593 && (/* Glyph doesn't fit on the line. */
17594 new_x > it->last_visible_x
17595 /* Or it fits exactly on a window system frame. */
17596 || (new_x == it->last_visible_x
17597 && FRAME_WINDOW_P (it->f))))
17598 {
17599 /* End of a continued line. */
17600
17601 if (it->hpos == 0
17602 || (new_x == it->last_visible_x
17603 && FRAME_WINDOW_P (it->f)))
17604 {
17605 /* Current glyph is the only one on the line or
17606 fits exactly on the line. We must continue
17607 the line because we can't draw the cursor
17608 after the glyph. */
17609 row->continued_p = 1;
17610 it->current_x = new_x;
17611 it->continuation_lines_width += new_x;
17612 ++it->hpos;
17613 /* Record the maximum and minimum buffer
17614 positions seen so far in glyphs that will be
17615 displayed by this row. */
17616 if (it->bidi_p)
17617 RECORD_MAX_MIN_POS (it);
17618 if (i == nglyphs - 1)
17619 {
17620 /* If line-wrap is on, check if a previous
17621 wrap point was found. */
17622 if (wrap_row_used > 0
17623 /* Even if there is a previous wrap
17624 point, continue the line here as
17625 usual, if (i) the previous character
17626 was a space or tab AND (ii) the
17627 current character is not. */
17628 && (!may_wrap
17629 || IT_DISPLAYING_WHITESPACE (it)))
17630 goto back_to_wrap;
17631
17632 set_iterator_to_next (it, 1);
17633 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17634 {
17635 if (!get_next_display_element (it))
17636 {
17637 row->exact_window_width_line_p = 1;
17638 it->continuation_lines_width = 0;
17639 row->continued_p = 0;
17640 row->ends_at_zv_p = 1;
17641 }
17642 else if (ITERATOR_AT_END_OF_LINE_P (it))
17643 {
17644 row->continued_p = 0;
17645 row->exact_window_width_line_p = 1;
17646 }
17647 }
17648 }
17649 }
17650 else if (CHAR_GLYPH_PADDING_P (*glyph)
17651 && !FRAME_WINDOW_P (it->f))
17652 {
17653 /* A padding glyph that doesn't fit on this line.
17654 This means the whole character doesn't fit
17655 on the line. */
17656 if (row->reversed_p)
17657 unproduce_glyphs (it, row->used[TEXT_AREA]
17658 - n_glyphs_before);
17659 row->used[TEXT_AREA] = n_glyphs_before;
17660
17661 /* Fill the rest of the row with continuation
17662 glyphs like in 20.x. */
17663 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17664 < row->glyphs[1 + TEXT_AREA])
17665 produce_special_glyphs (it, IT_CONTINUATION);
17666
17667 row->continued_p = 1;
17668 it->current_x = x_before;
17669 it->continuation_lines_width += x_before;
17670
17671 /* Restore the height to what it was before the
17672 element not fitting on the line. */
17673 it->max_ascent = ascent;
17674 it->max_descent = descent;
17675 it->max_phys_ascent = phys_ascent;
17676 it->max_phys_descent = phys_descent;
17677 }
17678 else if (wrap_row_used > 0)
17679 {
17680 back_to_wrap:
17681 if (row->reversed_p)
17682 unproduce_glyphs (it,
17683 row->used[TEXT_AREA] - wrap_row_used);
17684 *it = wrap_it;
17685 it->continuation_lines_width += wrap_x;
17686 row->used[TEXT_AREA] = wrap_row_used;
17687 row->ascent = wrap_row_ascent;
17688 row->height = wrap_row_height;
17689 row->phys_ascent = wrap_row_phys_ascent;
17690 row->phys_height = wrap_row_phys_height;
17691 row->extra_line_spacing = wrap_row_extra_line_spacing;
17692 min_pos = wrap_row_min_pos;
17693 min_bpos = wrap_row_min_bpos;
17694 max_pos = wrap_row_max_pos;
17695 max_bpos = wrap_row_max_bpos;
17696 row->continued_p = 1;
17697 row->ends_at_zv_p = 0;
17698 row->exact_window_width_line_p = 0;
17699 it->continuation_lines_width += x;
17700
17701 /* Make sure that a non-default face is extended
17702 up to the right margin of the window. */
17703 extend_face_to_end_of_line (it);
17704 }
17705 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17706 {
17707 /* A TAB that extends past the right edge of the
17708 window. This produces a single glyph on
17709 window system frames. We leave the glyph in
17710 this row and let it fill the row, but don't
17711 consume the TAB. */
17712 it->continuation_lines_width += it->last_visible_x;
17713 row->ends_in_middle_of_char_p = 1;
17714 row->continued_p = 1;
17715 glyph->pixel_width = it->last_visible_x - x;
17716 it->starts_in_middle_of_char_p = 1;
17717 }
17718 else
17719 {
17720 /* Something other than a TAB that draws past
17721 the right edge of the window. Restore
17722 positions to values before the element. */
17723 if (row->reversed_p)
17724 unproduce_glyphs (it, row->used[TEXT_AREA]
17725 - (n_glyphs_before + i));
17726 row->used[TEXT_AREA] = n_glyphs_before + i;
17727
17728 /* Display continuation glyphs. */
17729 if (!FRAME_WINDOW_P (it->f))
17730 produce_special_glyphs (it, IT_CONTINUATION);
17731 row->continued_p = 1;
17732
17733 it->current_x = x_before;
17734 it->continuation_lines_width += x;
17735 extend_face_to_end_of_line (it);
17736
17737 if (nglyphs > 1 && i > 0)
17738 {
17739 row->ends_in_middle_of_char_p = 1;
17740 it->starts_in_middle_of_char_p = 1;
17741 }
17742
17743 /* Restore the height to what it was before the
17744 element not fitting on the line. */
17745 it->max_ascent = ascent;
17746 it->max_descent = descent;
17747 it->max_phys_ascent = phys_ascent;
17748 it->max_phys_descent = phys_descent;
17749 }
17750
17751 break;
17752 }
17753 else if (new_x > it->first_visible_x)
17754 {
17755 /* Increment number of glyphs actually displayed. */
17756 ++it->hpos;
17757
17758 /* Record the maximum and minimum buffer positions
17759 seen so far in glyphs that will be displayed by
17760 this row. */
17761 if (it->bidi_p)
17762 RECORD_MAX_MIN_POS (it);
17763
17764 if (x < it->first_visible_x)
17765 /* Glyph is partially visible, i.e. row starts at
17766 negative X position. */
17767 row->x = x - it->first_visible_x;
17768 }
17769 else
17770 {
17771 /* Glyph is completely off the left margin of the
17772 window. This should not happen because of the
17773 move_it_in_display_line at the start of this
17774 function, unless the text display area of the
17775 window is empty. */
17776 xassert (it->first_visible_x <= it->last_visible_x);
17777 }
17778 }
17779
17780 row->ascent = max (row->ascent, it->max_ascent);
17781 row->height = max (row->height, it->max_ascent + it->max_descent);
17782 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17783 row->phys_height = max (row->phys_height,
17784 it->max_phys_ascent + it->max_phys_descent);
17785 row->extra_line_spacing = max (row->extra_line_spacing,
17786 it->max_extra_line_spacing);
17787
17788 /* End of this display line if row is continued. */
17789 if (row->continued_p || row->ends_at_zv_p)
17790 break;
17791 }
17792
17793 at_end_of_line:
17794 /* Is this a line end? If yes, we're also done, after making
17795 sure that a non-default face is extended up to the right
17796 margin of the window. */
17797 if (ITERATOR_AT_END_OF_LINE_P (it))
17798 {
17799 int used_before = row->used[TEXT_AREA];
17800
17801 row->ends_in_newline_from_string_p = STRINGP (it->object);
17802
17803 /* Add a space at the end of the line that is used to
17804 display the cursor there. */
17805 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17806 append_space_for_newline (it, 0);
17807
17808 /* Extend the face to the end of the line. */
17809 extend_face_to_end_of_line (it);
17810
17811 /* Make sure we have the position. */
17812 if (used_before == 0)
17813 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17814
17815 /* Record the position of the newline, for use in
17816 find_row_edges. */
17817 it->eol_pos = it->current.pos;
17818
17819 /* Consume the line end. This skips over invisible lines. */
17820 set_iterator_to_next (it, 1);
17821 it->continuation_lines_width = 0;
17822 break;
17823 }
17824
17825 /* Proceed with next display element. Note that this skips
17826 over lines invisible because of selective display. */
17827 set_iterator_to_next (it, 1);
17828
17829 /* If we truncate lines, we are done when the last displayed
17830 glyphs reach past the right margin of the window. */
17831 if (it->line_wrap == TRUNCATE
17832 && (FRAME_WINDOW_P (it->f)
17833 ? (it->current_x >= it->last_visible_x)
17834 : (it->current_x > it->last_visible_x)))
17835 {
17836 /* Maybe add truncation glyphs. */
17837 if (!FRAME_WINDOW_P (it->f))
17838 {
17839 int i, n;
17840
17841 if (!row->reversed_p)
17842 {
17843 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17844 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17845 break;
17846 }
17847 else
17848 {
17849 for (i = 0; i < row->used[TEXT_AREA]; i++)
17850 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17851 break;
17852 /* Remove any padding glyphs at the front of ROW, to
17853 make room for the truncation glyphs we will be
17854 adding below. The loop below always inserts at
17855 least one truncation glyph, so also remove the
17856 last glyph added to ROW. */
17857 unproduce_glyphs (it, i + 1);
17858 /* Adjust i for the loop below. */
17859 i = row->used[TEXT_AREA] - (i + 1);
17860 }
17861
17862 for (n = row->used[TEXT_AREA]; i < n; ++i)
17863 {
17864 row->used[TEXT_AREA] = i;
17865 produce_special_glyphs (it, IT_TRUNCATION);
17866 }
17867 }
17868 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17869 {
17870 /* Don't truncate if we can overflow newline into fringe. */
17871 if (!get_next_display_element (it))
17872 {
17873 it->continuation_lines_width = 0;
17874 row->ends_at_zv_p = 1;
17875 row->exact_window_width_line_p = 1;
17876 break;
17877 }
17878 if (ITERATOR_AT_END_OF_LINE_P (it))
17879 {
17880 row->exact_window_width_line_p = 1;
17881 goto at_end_of_line;
17882 }
17883 }
17884
17885 row->truncated_on_right_p = 1;
17886 it->continuation_lines_width = 0;
17887 reseat_at_next_visible_line_start (it, 0);
17888 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17889 it->hpos = hpos_before;
17890 it->current_x = x_before;
17891 break;
17892 }
17893 }
17894
17895 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17896 at the left window margin. */
17897 if (it->first_visible_x
17898 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17899 {
17900 if (!FRAME_WINDOW_P (it->f))
17901 insert_left_trunc_glyphs (it);
17902 row->truncated_on_left_p = 1;
17903 }
17904
17905 /* Remember the position at which this line ends.
17906
17907 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17908 cannot be before the call to find_row_edges below, since that is
17909 where these positions are determined. */
17910 row->end = it->current;
17911 if (!it->bidi_p)
17912 {
17913 row->minpos = row->start.pos;
17914 row->maxpos = row->end.pos;
17915 }
17916 else
17917 {
17918 /* ROW->minpos and ROW->maxpos must be the smallest and
17919 `1 + the largest' buffer positions in ROW. But if ROW was
17920 bidi-reordered, these two positions can be anywhere in the
17921 row, so we must determine them now. */
17922 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17923 }
17924
17925 /* If the start of this line is the overlay arrow-position, then
17926 mark this glyph row as the one containing the overlay arrow.
17927 This is clearly a mess with variable size fonts. It would be
17928 better to let it be displayed like cursors under X. */
17929 if ((row->displays_text_p || !overlay_arrow_seen)
17930 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17931 !NILP (overlay_arrow_string)))
17932 {
17933 /* Overlay arrow in window redisplay is a fringe bitmap. */
17934 if (STRINGP (overlay_arrow_string))
17935 {
17936 struct glyph_row *arrow_row
17937 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17938 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17939 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17940 struct glyph *p = row->glyphs[TEXT_AREA];
17941 struct glyph *p2, *end;
17942
17943 /* Copy the arrow glyphs. */
17944 while (glyph < arrow_end)
17945 *p++ = *glyph++;
17946
17947 /* Throw away padding glyphs. */
17948 p2 = p;
17949 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17950 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17951 ++p2;
17952 if (p2 > p)
17953 {
17954 while (p2 < end)
17955 *p++ = *p2++;
17956 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17957 }
17958 }
17959 else
17960 {
17961 xassert (INTEGERP (overlay_arrow_string));
17962 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17963 }
17964 overlay_arrow_seen = 1;
17965 }
17966
17967 /* Compute pixel dimensions of this line. */
17968 compute_line_metrics (it);
17969
17970 /* Record whether this row ends inside an ellipsis. */
17971 row->ends_in_ellipsis_p
17972 = (it->method == GET_FROM_DISPLAY_VECTOR
17973 && it->ellipsis_p);
17974
17975 /* Save fringe bitmaps in this row. */
17976 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17977 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17978 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17979 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17980
17981 it->left_user_fringe_bitmap = 0;
17982 it->left_user_fringe_face_id = 0;
17983 it->right_user_fringe_bitmap = 0;
17984 it->right_user_fringe_face_id = 0;
17985
17986 /* Maybe set the cursor. */
17987 cvpos = it->w->cursor.vpos;
17988 if ((cvpos < 0
17989 /* In bidi-reordered rows, keep checking for proper cursor
17990 position even if one has been found already, because buffer
17991 positions in such rows change non-linearly with ROW->VPOS,
17992 when a line is continued. One exception: when we are at ZV,
17993 display cursor on the first suitable glyph row, since all
17994 the empty rows after that also have their position set to ZV. */
17995 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17996 lines' rows is implemented for bidi-reordered rows. */
17997 || (it->bidi_p
17998 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17999 && PT >= MATRIX_ROW_START_CHARPOS (row)
18000 && PT <= MATRIX_ROW_END_CHARPOS (row)
18001 && cursor_row_p (row))
18002 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18003
18004 /* Highlight trailing whitespace. */
18005 if (!NILP (Vshow_trailing_whitespace))
18006 highlight_trailing_whitespace (it->f, it->glyph_row);
18007
18008 /* Prepare for the next line. This line starts horizontally at (X
18009 HPOS) = (0 0). Vertical positions are incremented. As a
18010 convenience for the caller, IT->glyph_row is set to the next
18011 row to be used. */
18012 it->current_x = it->hpos = 0;
18013 it->current_y += row->height;
18014 SET_TEXT_POS (it->eol_pos, 0, 0);
18015 ++it->vpos;
18016 ++it->glyph_row;
18017 /* The next row should by default use the same value of the
18018 reversed_p flag as this one. set_iterator_to_next decides when
18019 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18020 the flag accordingly. */
18021 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18022 it->glyph_row->reversed_p = row->reversed_p;
18023 it->start = row->end;
18024 return row->displays_text_p;
18025
18026 #undef RECORD_MAX_MIN_POS
18027 }
18028
18029 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18030 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18031 doc: /* Return paragraph direction at point in BUFFER.
18032 Value is either `left-to-right' or `right-to-left'.
18033 If BUFFER is omitted or nil, it defaults to the current buffer.
18034
18035 Paragraph direction determines how the text in the paragraph is displayed.
18036 In left-to-right paragraphs, text begins at the left margin of the window
18037 and the reading direction is generally left to right. In right-to-left
18038 paragraphs, text begins at the right margin and is read from right to left.
18039
18040 See also `bidi-paragraph-direction'. */)
18041 (Lisp_Object buffer)
18042 {
18043 struct buffer *buf = current_buffer;
18044 struct buffer *old = buf;
18045
18046 if (! NILP (buffer))
18047 {
18048 CHECK_BUFFER (buffer);
18049 buf = XBUFFER (buffer);
18050 }
18051
18052 if (NILP (BVAR (buf, bidi_display_reordering)))
18053 return Qleft_to_right;
18054 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18055 return BVAR (buf, bidi_paragraph_direction);
18056 else
18057 {
18058 /* Determine the direction from buffer text. We could try to
18059 use current_matrix if it is up to date, but this seems fast
18060 enough as it is. */
18061 struct bidi_it itb;
18062 EMACS_INT pos = BUF_PT (buf);
18063 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18064 int c;
18065
18066 set_buffer_temp (buf);
18067 /* bidi_paragraph_init finds the base direction of the paragraph
18068 by searching forward from paragraph start. We need the base
18069 direction of the current or _previous_ paragraph, so we need
18070 to make sure we are within that paragraph. To that end, find
18071 the previous non-empty line. */
18072 if (pos >= ZV && pos > BEGV)
18073 {
18074 pos--;
18075 bytepos = CHAR_TO_BYTE (pos);
18076 }
18077 while ((c = FETCH_BYTE (bytepos)) == '\n'
18078 || c == ' ' || c == '\t' || c == '\f')
18079 {
18080 if (bytepos <= BEGV_BYTE)
18081 break;
18082 bytepos--;
18083 pos--;
18084 }
18085 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18086 bytepos--;
18087 itb.charpos = pos;
18088 itb.bytepos = bytepos;
18089 itb.first_elt = 1;
18090 itb.separator_limit = -1;
18091 itb.paragraph_dir = NEUTRAL_DIR;
18092
18093 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18094 set_buffer_temp (old);
18095 switch (itb.paragraph_dir)
18096 {
18097 case L2R:
18098 return Qleft_to_right;
18099 break;
18100 case R2L:
18101 return Qright_to_left;
18102 break;
18103 default:
18104 abort ();
18105 }
18106 }
18107 }
18108
18109
18110 \f
18111 /***********************************************************************
18112 Menu Bar
18113 ***********************************************************************/
18114
18115 /* Redisplay the menu bar in the frame for window W.
18116
18117 The menu bar of X frames that don't have X toolkit support is
18118 displayed in a special window W->frame->menu_bar_window.
18119
18120 The menu bar of terminal frames is treated specially as far as
18121 glyph matrices are concerned. Menu bar lines are not part of
18122 windows, so the update is done directly on the frame matrix rows
18123 for the menu bar. */
18124
18125 static void
18126 display_menu_bar (struct window *w)
18127 {
18128 struct frame *f = XFRAME (WINDOW_FRAME (w));
18129 struct it it;
18130 Lisp_Object items;
18131 int i;
18132
18133 /* Don't do all this for graphical frames. */
18134 #ifdef HAVE_NTGUI
18135 if (FRAME_W32_P (f))
18136 return;
18137 #endif
18138 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18139 if (FRAME_X_P (f))
18140 return;
18141 #endif
18142
18143 #ifdef HAVE_NS
18144 if (FRAME_NS_P (f))
18145 return;
18146 #endif /* HAVE_NS */
18147
18148 #ifdef USE_X_TOOLKIT
18149 xassert (!FRAME_WINDOW_P (f));
18150 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18151 it.first_visible_x = 0;
18152 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18153 #else /* not USE_X_TOOLKIT */
18154 if (FRAME_WINDOW_P (f))
18155 {
18156 /* Menu bar lines are displayed in the desired matrix of the
18157 dummy window menu_bar_window. */
18158 struct window *menu_w;
18159 xassert (WINDOWP (f->menu_bar_window));
18160 menu_w = XWINDOW (f->menu_bar_window);
18161 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18162 MENU_FACE_ID);
18163 it.first_visible_x = 0;
18164 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18165 }
18166 else
18167 {
18168 /* This is a TTY frame, i.e. character hpos/vpos are used as
18169 pixel x/y. */
18170 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18171 MENU_FACE_ID);
18172 it.first_visible_x = 0;
18173 it.last_visible_x = FRAME_COLS (f);
18174 }
18175 #endif /* not USE_X_TOOLKIT */
18176
18177 if (! mode_line_inverse_video)
18178 /* Force the menu-bar to be displayed in the default face. */
18179 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18180
18181 /* Clear all rows of the menu bar. */
18182 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18183 {
18184 struct glyph_row *row = it.glyph_row + i;
18185 clear_glyph_row (row);
18186 row->enabled_p = 1;
18187 row->full_width_p = 1;
18188 }
18189
18190 /* Display all items of the menu bar. */
18191 items = FRAME_MENU_BAR_ITEMS (it.f);
18192 for (i = 0; i < ASIZE (items); i += 4)
18193 {
18194 Lisp_Object string;
18195
18196 /* Stop at nil string. */
18197 string = AREF (items, i + 1);
18198 if (NILP (string))
18199 break;
18200
18201 /* Remember where item was displayed. */
18202 ASET (items, i + 3, make_number (it.hpos));
18203
18204 /* Display the item, pad with one space. */
18205 if (it.current_x < it.last_visible_x)
18206 display_string (NULL, string, Qnil, 0, 0, &it,
18207 SCHARS (string) + 1, 0, 0, -1);
18208 }
18209
18210 /* Fill out the line with spaces. */
18211 if (it.current_x < it.last_visible_x)
18212 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18213
18214 /* Compute the total height of the lines. */
18215 compute_line_metrics (&it);
18216 }
18217
18218
18219 \f
18220 /***********************************************************************
18221 Mode Line
18222 ***********************************************************************/
18223
18224 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18225 FORCE is non-zero, redisplay mode lines unconditionally.
18226 Otherwise, redisplay only mode lines that are garbaged. Value is
18227 the number of windows whose mode lines were redisplayed. */
18228
18229 static int
18230 redisplay_mode_lines (Lisp_Object window, int force)
18231 {
18232 int nwindows = 0;
18233
18234 while (!NILP (window))
18235 {
18236 struct window *w = XWINDOW (window);
18237
18238 if (WINDOWP (w->hchild))
18239 nwindows += redisplay_mode_lines (w->hchild, force);
18240 else if (WINDOWP (w->vchild))
18241 nwindows += redisplay_mode_lines (w->vchild, force);
18242 else if (force
18243 || FRAME_GARBAGED_P (XFRAME (w->frame))
18244 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18245 {
18246 struct text_pos lpoint;
18247 struct buffer *old = current_buffer;
18248
18249 /* Set the window's buffer for the mode line display. */
18250 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18251 set_buffer_internal_1 (XBUFFER (w->buffer));
18252
18253 /* Point refers normally to the selected window. For any
18254 other window, set up appropriate value. */
18255 if (!EQ (window, selected_window))
18256 {
18257 struct text_pos pt;
18258
18259 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18260 if (CHARPOS (pt) < BEGV)
18261 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18262 else if (CHARPOS (pt) > (ZV - 1))
18263 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18264 else
18265 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18266 }
18267
18268 /* Display mode lines. */
18269 clear_glyph_matrix (w->desired_matrix);
18270 if (display_mode_lines (w))
18271 {
18272 ++nwindows;
18273 w->must_be_updated_p = 1;
18274 }
18275
18276 /* Restore old settings. */
18277 set_buffer_internal_1 (old);
18278 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18279 }
18280
18281 window = w->next;
18282 }
18283
18284 return nwindows;
18285 }
18286
18287
18288 /* Display the mode and/or header line of window W. Value is the
18289 sum number of mode lines and header lines displayed. */
18290
18291 static int
18292 display_mode_lines (struct window *w)
18293 {
18294 Lisp_Object old_selected_window, old_selected_frame;
18295 int n = 0;
18296
18297 old_selected_frame = selected_frame;
18298 selected_frame = w->frame;
18299 old_selected_window = selected_window;
18300 XSETWINDOW (selected_window, w);
18301
18302 /* These will be set while the mode line specs are processed. */
18303 line_number_displayed = 0;
18304 w->column_number_displayed = Qnil;
18305
18306 if (WINDOW_WANTS_MODELINE_P (w))
18307 {
18308 struct window *sel_w = XWINDOW (old_selected_window);
18309
18310 /* Select mode line face based on the real selected window. */
18311 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18312 BVAR (current_buffer, mode_line_format));
18313 ++n;
18314 }
18315
18316 if (WINDOW_WANTS_HEADER_LINE_P (w))
18317 {
18318 display_mode_line (w, HEADER_LINE_FACE_ID,
18319 BVAR (current_buffer, header_line_format));
18320 ++n;
18321 }
18322
18323 selected_frame = old_selected_frame;
18324 selected_window = old_selected_window;
18325 return n;
18326 }
18327
18328
18329 /* Display mode or header line of window W. FACE_ID specifies which
18330 line to display; it is either MODE_LINE_FACE_ID or
18331 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18332 display. Value is the pixel height of the mode/header line
18333 displayed. */
18334
18335 static int
18336 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18337 {
18338 struct it it;
18339 struct face *face;
18340 int count = SPECPDL_INDEX ();
18341
18342 init_iterator (&it, w, -1, -1, NULL, face_id);
18343 /* Don't extend on a previously drawn mode-line.
18344 This may happen if called from pos_visible_p. */
18345 it.glyph_row->enabled_p = 0;
18346 prepare_desired_row (it.glyph_row);
18347
18348 it.glyph_row->mode_line_p = 1;
18349
18350 if (! mode_line_inverse_video)
18351 /* Force the mode-line to be displayed in the default face. */
18352 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18353
18354 record_unwind_protect (unwind_format_mode_line,
18355 format_mode_line_unwind_data (NULL, Qnil, 0));
18356
18357 mode_line_target = MODE_LINE_DISPLAY;
18358
18359 /* Temporarily make frame's keyboard the current kboard so that
18360 kboard-local variables in the mode_line_format will get the right
18361 values. */
18362 push_kboard (FRAME_KBOARD (it.f));
18363 record_unwind_save_match_data ();
18364 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18365 pop_kboard ();
18366
18367 unbind_to (count, Qnil);
18368
18369 /* Fill up with spaces. */
18370 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18371
18372 compute_line_metrics (&it);
18373 it.glyph_row->full_width_p = 1;
18374 it.glyph_row->continued_p = 0;
18375 it.glyph_row->truncated_on_left_p = 0;
18376 it.glyph_row->truncated_on_right_p = 0;
18377
18378 /* Make a 3D mode-line have a shadow at its right end. */
18379 face = FACE_FROM_ID (it.f, face_id);
18380 extend_face_to_end_of_line (&it);
18381 if (face->box != FACE_NO_BOX)
18382 {
18383 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18384 + it.glyph_row->used[TEXT_AREA] - 1);
18385 last->right_box_line_p = 1;
18386 }
18387
18388 return it.glyph_row->height;
18389 }
18390
18391 /* Move element ELT in LIST to the front of LIST.
18392 Return the updated list. */
18393
18394 static Lisp_Object
18395 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18396 {
18397 register Lisp_Object tail, prev;
18398 register Lisp_Object tem;
18399
18400 tail = list;
18401 prev = Qnil;
18402 while (CONSP (tail))
18403 {
18404 tem = XCAR (tail);
18405
18406 if (EQ (elt, tem))
18407 {
18408 /* Splice out the link TAIL. */
18409 if (NILP (prev))
18410 list = XCDR (tail);
18411 else
18412 Fsetcdr (prev, XCDR (tail));
18413
18414 /* Now make it the first. */
18415 Fsetcdr (tail, list);
18416 return tail;
18417 }
18418 else
18419 prev = tail;
18420 tail = XCDR (tail);
18421 QUIT;
18422 }
18423
18424 /* Not found--return unchanged LIST. */
18425 return list;
18426 }
18427
18428 /* Contribute ELT to the mode line for window IT->w. How it
18429 translates into text depends on its data type.
18430
18431 IT describes the display environment in which we display, as usual.
18432
18433 DEPTH is the depth in recursion. It is used to prevent
18434 infinite recursion here.
18435
18436 FIELD_WIDTH is the number of characters the display of ELT should
18437 occupy in the mode line, and PRECISION is the maximum number of
18438 characters to display from ELT's representation. See
18439 display_string for details.
18440
18441 Returns the hpos of the end of the text generated by ELT.
18442
18443 PROPS is a property list to add to any string we encounter.
18444
18445 If RISKY is nonzero, remove (disregard) any properties in any string
18446 we encounter, and ignore :eval and :propertize.
18447
18448 The global variable `mode_line_target' determines whether the
18449 output is passed to `store_mode_line_noprop',
18450 `store_mode_line_string', or `display_string'. */
18451
18452 static int
18453 display_mode_element (struct it *it, int depth, int field_width, int precision,
18454 Lisp_Object elt, Lisp_Object props, int risky)
18455 {
18456 int n = 0, field, prec;
18457 int literal = 0;
18458
18459 tail_recurse:
18460 if (depth > 100)
18461 elt = build_string ("*too-deep*");
18462
18463 depth++;
18464
18465 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18466 {
18467 case Lisp_String:
18468 {
18469 /* A string: output it and check for %-constructs within it. */
18470 unsigned char c;
18471 EMACS_INT offset = 0;
18472
18473 if (SCHARS (elt) > 0
18474 && (!NILP (props) || risky))
18475 {
18476 Lisp_Object oprops, aelt;
18477 oprops = Ftext_properties_at (make_number (0), elt);
18478
18479 /* If the starting string's properties are not what
18480 we want, translate the string. Also, if the string
18481 is risky, do that anyway. */
18482
18483 if (NILP (Fequal (props, oprops)) || risky)
18484 {
18485 /* If the starting string has properties,
18486 merge the specified ones onto the existing ones. */
18487 if (! NILP (oprops) && !risky)
18488 {
18489 Lisp_Object tem;
18490
18491 oprops = Fcopy_sequence (oprops);
18492 tem = props;
18493 while (CONSP (tem))
18494 {
18495 oprops = Fplist_put (oprops, XCAR (tem),
18496 XCAR (XCDR (tem)));
18497 tem = XCDR (XCDR (tem));
18498 }
18499 props = oprops;
18500 }
18501
18502 aelt = Fassoc (elt, mode_line_proptrans_alist);
18503 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18504 {
18505 /* AELT is what we want. Move it to the front
18506 without consing. */
18507 elt = XCAR (aelt);
18508 mode_line_proptrans_alist
18509 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18510 }
18511 else
18512 {
18513 Lisp_Object tem;
18514
18515 /* If AELT has the wrong props, it is useless.
18516 so get rid of it. */
18517 if (! NILP (aelt))
18518 mode_line_proptrans_alist
18519 = Fdelq (aelt, mode_line_proptrans_alist);
18520
18521 elt = Fcopy_sequence (elt);
18522 Fset_text_properties (make_number (0), Flength (elt),
18523 props, elt);
18524 /* Add this item to mode_line_proptrans_alist. */
18525 mode_line_proptrans_alist
18526 = Fcons (Fcons (elt, props),
18527 mode_line_proptrans_alist);
18528 /* Truncate mode_line_proptrans_alist
18529 to at most 50 elements. */
18530 tem = Fnthcdr (make_number (50),
18531 mode_line_proptrans_alist);
18532 if (! NILP (tem))
18533 XSETCDR (tem, Qnil);
18534 }
18535 }
18536 }
18537
18538 offset = 0;
18539
18540 if (literal)
18541 {
18542 prec = precision - n;
18543 switch (mode_line_target)
18544 {
18545 case MODE_LINE_NOPROP:
18546 case MODE_LINE_TITLE:
18547 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
18548 break;
18549 case MODE_LINE_STRING:
18550 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18551 break;
18552 case MODE_LINE_DISPLAY:
18553 n += display_string (NULL, elt, Qnil, 0, 0, it,
18554 0, prec, 0, STRING_MULTIBYTE (elt));
18555 break;
18556 }
18557
18558 break;
18559 }
18560
18561 /* Handle the non-literal case. */
18562
18563 while ((precision <= 0 || n < precision)
18564 && SREF (elt, offset) != 0
18565 && (mode_line_target != MODE_LINE_DISPLAY
18566 || it->current_x < it->last_visible_x))
18567 {
18568 EMACS_INT last_offset = offset;
18569
18570 /* Advance to end of string or next format specifier. */
18571 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18572 ;
18573
18574 if (offset - 1 != last_offset)
18575 {
18576 EMACS_INT nchars, nbytes;
18577
18578 /* Output to end of string or up to '%'. Field width
18579 is length of string. Don't output more than
18580 PRECISION allows us. */
18581 offset--;
18582
18583 prec = c_string_width (SDATA (elt) + last_offset,
18584 offset - last_offset, precision - n,
18585 &nchars, &nbytes);
18586
18587 switch (mode_line_target)
18588 {
18589 case MODE_LINE_NOPROP:
18590 case MODE_LINE_TITLE:
18591 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
18592 break;
18593 case MODE_LINE_STRING:
18594 {
18595 EMACS_INT bytepos = last_offset;
18596 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18597 EMACS_INT endpos = (precision <= 0
18598 ? string_byte_to_char (elt, offset)
18599 : charpos + nchars);
18600
18601 n += store_mode_line_string (NULL,
18602 Fsubstring (elt, make_number (charpos),
18603 make_number (endpos)),
18604 0, 0, 0, Qnil);
18605 }
18606 break;
18607 case MODE_LINE_DISPLAY:
18608 {
18609 EMACS_INT bytepos = last_offset;
18610 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18611
18612 if (precision <= 0)
18613 nchars = string_byte_to_char (elt, offset) - charpos;
18614 n += display_string (NULL, elt, Qnil, 0, charpos,
18615 it, 0, nchars, 0,
18616 STRING_MULTIBYTE (elt));
18617 }
18618 break;
18619 }
18620 }
18621 else /* c == '%' */
18622 {
18623 EMACS_INT percent_position = offset;
18624
18625 /* Get the specified minimum width. Zero means
18626 don't pad. */
18627 field = 0;
18628 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18629 field = field * 10 + c - '0';
18630
18631 /* Don't pad beyond the total padding allowed. */
18632 if (field_width - n > 0 && field > field_width - n)
18633 field = field_width - n;
18634
18635 /* Note that either PRECISION <= 0 or N < PRECISION. */
18636 prec = precision - n;
18637
18638 if (c == 'M')
18639 n += display_mode_element (it, depth, field, prec,
18640 Vglobal_mode_string, props,
18641 risky);
18642 else if (c != 0)
18643 {
18644 int multibyte;
18645 EMACS_INT bytepos, charpos;
18646 const char *spec;
18647 Lisp_Object string;
18648
18649 bytepos = percent_position;
18650 charpos = (STRING_MULTIBYTE (elt)
18651 ? string_byte_to_char (elt, bytepos)
18652 : bytepos);
18653 spec = decode_mode_spec (it->w, c, field, &string);
18654 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18655
18656 switch (mode_line_target)
18657 {
18658 case MODE_LINE_NOPROP:
18659 case MODE_LINE_TITLE:
18660 n += store_mode_line_noprop (spec, field, prec);
18661 break;
18662 case MODE_LINE_STRING:
18663 {
18664 int len = strlen (spec);
18665 Lisp_Object tem = make_string (spec, len);
18666 props = Ftext_properties_at (make_number (charpos), elt);
18667 /* Should only keep face property in props */
18668 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18669 }
18670 break;
18671 case MODE_LINE_DISPLAY:
18672 {
18673 int nglyphs_before, nwritten;
18674
18675 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18676 nwritten = display_string (spec, string, elt,
18677 charpos, 0, it,
18678 field, prec, 0,
18679 multibyte);
18680
18681 /* Assign to the glyphs written above the
18682 string where the `%x' came from, position
18683 of the `%'. */
18684 if (nwritten > 0)
18685 {
18686 struct glyph *glyph
18687 = (it->glyph_row->glyphs[TEXT_AREA]
18688 + nglyphs_before);
18689 int i;
18690
18691 for (i = 0; i < nwritten; ++i)
18692 {
18693 glyph[i].object = elt;
18694 glyph[i].charpos = charpos;
18695 }
18696
18697 n += nwritten;
18698 }
18699 }
18700 break;
18701 }
18702 }
18703 else /* c == 0 */
18704 break;
18705 }
18706 }
18707 }
18708 break;
18709
18710 case Lisp_Symbol:
18711 /* A symbol: process the value of the symbol recursively
18712 as if it appeared here directly. Avoid error if symbol void.
18713 Special case: if value of symbol is a string, output the string
18714 literally. */
18715 {
18716 register Lisp_Object tem;
18717
18718 /* If the variable is not marked as risky to set
18719 then its contents are risky to use. */
18720 if (NILP (Fget (elt, Qrisky_local_variable)))
18721 risky = 1;
18722
18723 tem = Fboundp (elt);
18724 if (!NILP (tem))
18725 {
18726 tem = Fsymbol_value (elt);
18727 /* If value is a string, output that string literally:
18728 don't check for % within it. */
18729 if (STRINGP (tem))
18730 literal = 1;
18731
18732 if (!EQ (tem, elt))
18733 {
18734 /* Give up right away for nil or t. */
18735 elt = tem;
18736 goto tail_recurse;
18737 }
18738 }
18739 }
18740 break;
18741
18742 case Lisp_Cons:
18743 {
18744 register Lisp_Object car, tem;
18745
18746 /* A cons cell: five distinct cases.
18747 If first element is :eval or :propertize, do something special.
18748 If first element is a string or a cons, process all the elements
18749 and effectively concatenate them.
18750 If first element is a negative number, truncate displaying cdr to
18751 at most that many characters. If positive, pad (with spaces)
18752 to at least that many characters.
18753 If first element is a symbol, process the cadr or caddr recursively
18754 according to whether the symbol's value is non-nil or nil. */
18755 car = XCAR (elt);
18756 if (EQ (car, QCeval))
18757 {
18758 /* An element of the form (:eval FORM) means evaluate FORM
18759 and use the result as mode line elements. */
18760
18761 if (risky)
18762 break;
18763
18764 if (CONSP (XCDR (elt)))
18765 {
18766 Lisp_Object spec;
18767 spec = safe_eval (XCAR (XCDR (elt)));
18768 n += display_mode_element (it, depth, field_width - n,
18769 precision - n, spec, props,
18770 risky);
18771 }
18772 }
18773 else if (EQ (car, QCpropertize))
18774 {
18775 /* An element of the form (:propertize ELT PROPS...)
18776 means display ELT but applying properties PROPS. */
18777
18778 if (risky)
18779 break;
18780
18781 if (CONSP (XCDR (elt)))
18782 n += display_mode_element (it, depth, field_width - n,
18783 precision - n, XCAR (XCDR (elt)),
18784 XCDR (XCDR (elt)), risky);
18785 }
18786 else if (SYMBOLP (car))
18787 {
18788 tem = Fboundp (car);
18789 elt = XCDR (elt);
18790 if (!CONSP (elt))
18791 goto invalid;
18792 /* elt is now the cdr, and we know it is a cons cell.
18793 Use its car if CAR has a non-nil value. */
18794 if (!NILP (tem))
18795 {
18796 tem = Fsymbol_value (car);
18797 if (!NILP (tem))
18798 {
18799 elt = XCAR (elt);
18800 goto tail_recurse;
18801 }
18802 }
18803 /* Symbol's value is nil (or symbol is unbound)
18804 Get the cddr of the original list
18805 and if possible find the caddr and use that. */
18806 elt = XCDR (elt);
18807 if (NILP (elt))
18808 break;
18809 else if (!CONSP (elt))
18810 goto invalid;
18811 elt = XCAR (elt);
18812 goto tail_recurse;
18813 }
18814 else if (INTEGERP (car))
18815 {
18816 register int lim = XINT (car);
18817 elt = XCDR (elt);
18818 if (lim < 0)
18819 {
18820 /* Negative int means reduce maximum width. */
18821 if (precision <= 0)
18822 precision = -lim;
18823 else
18824 precision = min (precision, -lim);
18825 }
18826 else if (lim > 0)
18827 {
18828 /* Padding specified. Don't let it be more than
18829 current maximum. */
18830 if (precision > 0)
18831 lim = min (precision, lim);
18832
18833 /* If that's more padding than already wanted, queue it.
18834 But don't reduce padding already specified even if
18835 that is beyond the current truncation point. */
18836 field_width = max (lim, field_width);
18837 }
18838 goto tail_recurse;
18839 }
18840 else if (STRINGP (car) || CONSP (car))
18841 {
18842 Lisp_Object halftail = elt;
18843 int len = 0;
18844
18845 while (CONSP (elt)
18846 && (precision <= 0 || n < precision))
18847 {
18848 n += display_mode_element (it, depth,
18849 /* Do padding only after the last
18850 element in the list. */
18851 (! CONSP (XCDR (elt))
18852 ? field_width - n
18853 : 0),
18854 precision - n, XCAR (elt),
18855 props, risky);
18856 elt = XCDR (elt);
18857 len++;
18858 if ((len & 1) == 0)
18859 halftail = XCDR (halftail);
18860 /* Check for cycle. */
18861 if (EQ (halftail, elt))
18862 break;
18863 }
18864 }
18865 }
18866 break;
18867
18868 default:
18869 invalid:
18870 elt = build_string ("*invalid*");
18871 goto tail_recurse;
18872 }
18873
18874 /* Pad to FIELD_WIDTH. */
18875 if (field_width > 0 && n < field_width)
18876 {
18877 switch (mode_line_target)
18878 {
18879 case MODE_LINE_NOPROP:
18880 case MODE_LINE_TITLE:
18881 n += store_mode_line_noprop ("", field_width - n, 0);
18882 break;
18883 case MODE_LINE_STRING:
18884 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18885 break;
18886 case MODE_LINE_DISPLAY:
18887 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18888 0, 0, 0);
18889 break;
18890 }
18891 }
18892
18893 return n;
18894 }
18895
18896 /* Store a mode-line string element in mode_line_string_list.
18897
18898 If STRING is non-null, display that C string. Otherwise, the Lisp
18899 string LISP_STRING is displayed.
18900
18901 FIELD_WIDTH is the minimum number of output glyphs to produce.
18902 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18903 with spaces. FIELD_WIDTH <= 0 means don't pad.
18904
18905 PRECISION is the maximum number of characters to output from
18906 STRING. PRECISION <= 0 means don't truncate the string.
18907
18908 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18909 properties to the string.
18910
18911 PROPS are the properties to add to the string.
18912 The mode_line_string_face face property is always added to the string.
18913 */
18914
18915 static int
18916 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18917 int field_width, int precision, Lisp_Object props)
18918 {
18919 EMACS_INT len;
18920 int n = 0;
18921
18922 if (string != NULL)
18923 {
18924 len = strlen (string);
18925 if (precision > 0 && len > precision)
18926 len = precision;
18927 lisp_string = make_string (string, len);
18928 if (NILP (props))
18929 props = mode_line_string_face_prop;
18930 else if (!NILP (mode_line_string_face))
18931 {
18932 Lisp_Object face = Fplist_get (props, Qface);
18933 props = Fcopy_sequence (props);
18934 if (NILP (face))
18935 face = mode_line_string_face;
18936 else
18937 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18938 props = Fplist_put (props, Qface, face);
18939 }
18940 Fadd_text_properties (make_number (0), make_number (len),
18941 props, lisp_string);
18942 }
18943 else
18944 {
18945 len = XFASTINT (Flength (lisp_string));
18946 if (precision > 0 && len > precision)
18947 {
18948 len = precision;
18949 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18950 precision = -1;
18951 }
18952 if (!NILP (mode_line_string_face))
18953 {
18954 Lisp_Object face;
18955 if (NILP (props))
18956 props = Ftext_properties_at (make_number (0), lisp_string);
18957 face = Fplist_get (props, Qface);
18958 if (NILP (face))
18959 face = mode_line_string_face;
18960 else
18961 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18962 props = Fcons (Qface, Fcons (face, Qnil));
18963 if (copy_string)
18964 lisp_string = Fcopy_sequence (lisp_string);
18965 }
18966 if (!NILP (props))
18967 Fadd_text_properties (make_number (0), make_number (len),
18968 props, lisp_string);
18969 }
18970
18971 if (len > 0)
18972 {
18973 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18974 n += len;
18975 }
18976
18977 if (field_width > len)
18978 {
18979 field_width -= len;
18980 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18981 if (!NILP (props))
18982 Fadd_text_properties (make_number (0), make_number (field_width),
18983 props, lisp_string);
18984 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18985 n += field_width;
18986 }
18987
18988 return n;
18989 }
18990
18991
18992 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18993 1, 4, 0,
18994 doc: /* Format a string out of a mode line format specification.
18995 First arg FORMAT specifies the mode line format (see `mode-line-format'
18996 for details) to use.
18997
18998 By default, the format is evaluated for the currently selected window.
18999
19000 Optional second arg FACE specifies the face property to put on all
19001 characters for which no face is specified. The value nil means the
19002 default face. The value t means whatever face the window's mode line
19003 currently uses (either `mode-line' or `mode-line-inactive',
19004 depending on whether the window is the selected window or not).
19005 An integer value means the value string has no text
19006 properties.
19007
19008 Optional third and fourth args WINDOW and BUFFER specify the window
19009 and buffer to use as the context for the formatting (defaults
19010 are the selected window and the WINDOW's buffer). */)
19011 (Lisp_Object format, Lisp_Object face,
19012 Lisp_Object window, Lisp_Object buffer)
19013 {
19014 struct it it;
19015 int len;
19016 struct window *w;
19017 struct buffer *old_buffer = NULL;
19018 int face_id;
19019 int no_props = INTEGERP (face);
19020 int count = SPECPDL_INDEX ();
19021 Lisp_Object str;
19022 int string_start = 0;
19023
19024 if (NILP (window))
19025 window = selected_window;
19026 CHECK_WINDOW (window);
19027 w = XWINDOW (window);
19028
19029 if (NILP (buffer))
19030 buffer = w->buffer;
19031 CHECK_BUFFER (buffer);
19032
19033 /* Make formatting the modeline a non-op when noninteractive, otherwise
19034 there will be problems later caused by a partially initialized frame. */
19035 if (NILP (format) || noninteractive)
19036 return empty_unibyte_string;
19037
19038 if (no_props)
19039 face = Qnil;
19040
19041 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19042 : EQ (face, Qt) ? (EQ (window, selected_window)
19043 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19044 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19045 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19046 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19047 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19048 : DEFAULT_FACE_ID;
19049
19050 if (XBUFFER (buffer) != current_buffer)
19051 old_buffer = current_buffer;
19052
19053 /* Save things including mode_line_proptrans_alist,
19054 and set that to nil so that we don't alter the outer value. */
19055 record_unwind_protect (unwind_format_mode_line,
19056 format_mode_line_unwind_data
19057 (old_buffer, selected_window, 1));
19058 mode_line_proptrans_alist = Qnil;
19059
19060 Fselect_window (window, Qt);
19061 if (old_buffer)
19062 set_buffer_internal_1 (XBUFFER (buffer));
19063
19064 init_iterator (&it, w, -1, -1, NULL, face_id);
19065
19066 if (no_props)
19067 {
19068 mode_line_target = MODE_LINE_NOPROP;
19069 mode_line_string_face_prop = Qnil;
19070 mode_line_string_list = Qnil;
19071 string_start = MODE_LINE_NOPROP_LEN (0);
19072 }
19073 else
19074 {
19075 mode_line_target = MODE_LINE_STRING;
19076 mode_line_string_list = Qnil;
19077 mode_line_string_face = face;
19078 mode_line_string_face_prop
19079 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19080 }
19081
19082 push_kboard (FRAME_KBOARD (it.f));
19083 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19084 pop_kboard ();
19085
19086 if (no_props)
19087 {
19088 len = MODE_LINE_NOPROP_LEN (string_start);
19089 str = make_string (mode_line_noprop_buf + string_start, len);
19090 }
19091 else
19092 {
19093 mode_line_string_list = Fnreverse (mode_line_string_list);
19094 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19095 empty_unibyte_string);
19096 }
19097
19098 unbind_to (count, Qnil);
19099 return str;
19100 }
19101
19102 /* Write a null-terminated, right justified decimal representation of
19103 the positive integer D to BUF using a minimal field width WIDTH. */
19104
19105 static void
19106 pint2str (register char *buf, register int width, register EMACS_INT d)
19107 {
19108 register char *p = buf;
19109
19110 if (d <= 0)
19111 *p++ = '0';
19112 else
19113 {
19114 while (d > 0)
19115 {
19116 *p++ = d % 10 + '0';
19117 d /= 10;
19118 }
19119 }
19120
19121 for (width -= (int) (p - buf); width > 0; --width)
19122 *p++ = ' ';
19123 *p-- = '\0';
19124 while (p > buf)
19125 {
19126 d = *buf;
19127 *buf++ = *p;
19128 *p-- = d;
19129 }
19130 }
19131
19132 /* Write a null-terminated, right justified decimal and "human
19133 readable" representation of the nonnegative integer D to BUF using
19134 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19135
19136 static const char power_letter[] =
19137 {
19138 0, /* no letter */
19139 'k', /* kilo */
19140 'M', /* mega */
19141 'G', /* giga */
19142 'T', /* tera */
19143 'P', /* peta */
19144 'E', /* exa */
19145 'Z', /* zetta */
19146 'Y' /* yotta */
19147 };
19148
19149 static void
19150 pint2hrstr (char *buf, int width, EMACS_INT d)
19151 {
19152 /* We aim to represent the nonnegative integer D as
19153 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19154 EMACS_INT quotient = d;
19155 int remainder = 0;
19156 /* -1 means: do not use TENTHS. */
19157 int tenths = -1;
19158 int exponent = 0;
19159
19160 /* Length of QUOTIENT.TENTHS as a string. */
19161 int length;
19162
19163 char * psuffix;
19164 char * p;
19165
19166 if (1000 <= quotient)
19167 {
19168 /* Scale to the appropriate EXPONENT. */
19169 do
19170 {
19171 remainder = quotient % 1000;
19172 quotient /= 1000;
19173 exponent++;
19174 }
19175 while (1000 <= quotient);
19176
19177 /* Round to nearest and decide whether to use TENTHS or not. */
19178 if (quotient <= 9)
19179 {
19180 tenths = remainder / 100;
19181 if (50 <= remainder % 100)
19182 {
19183 if (tenths < 9)
19184 tenths++;
19185 else
19186 {
19187 quotient++;
19188 if (quotient == 10)
19189 tenths = -1;
19190 else
19191 tenths = 0;
19192 }
19193 }
19194 }
19195 else
19196 if (500 <= remainder)
19197 {
19198 if (quotient < 999)
19199 quotient++;
19200 else
19201 {
19202 quotient = 1;
19203 exponent++;
19204 tenths = 0;
19205 }
19206 }
19207 }
19208
19209 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19210 if (tenths == -1 && quotient <= 99)
19211 if (quotient <= 9)
19212 length = 1;
19213 else
19214 length = 2;
19215 else
19216 length = 3;
19217 p = psuffix = buf + max (width, length);
19218
19219 /* Print EXPONENT. */
19220 *psuffix++ = power_letter[exponent];
19221 *psuffix = '\0';
19222
19223 /* Print TENTHS. */
19224 if (tenths >= 0)
19225 {
19226 *--p = '0' + tenths;
19227 *--p = '.';
19228 }
19229
19230 /* Print QUOTIENT. */
19231 do
19232 {
19233 int digit = quotient % 10;
19234 *--p = '0' + digit;
19235 }
19236 while ((quotient /= 10) != 0);
19237
19238 /* Print leading spaces. */
19239 while (buf < p)
19240 *--p = ' ';
19241 }
19242
19243 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19244 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19245 type of CODING_SYSTEM. Return updated pointer into BUF. */
19246
19247 static unsigned char invalid_eol_type[] = "(*invalid*)";
19248
19249 static char *
19250 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19251 {
19252 Lisp_Object val;
19253 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19254 const unsigned char *eol_str;
19255 int eol_str_len;
19256 /* The EOL conversion we are using. */
19257 Lisp_Object eoltype;
19258
19259 val = CODING_SYSTEM_SPEC (coding_system);
19260 eoltype = Qnil;
19261
19262 if (!VECTORP (val)) /* Not yet decided. */
19263 {
19264 if (multibyte)
19265 *buf++ = '-';
19266 if (eol_flag)
19267 eoltype = eol_mnemonic_undecided;
19268 /* Don't mention EOL conversion if it isn't decided. */
19269 }
19270 else
19271 {
19272 Lisp_Object attrs;
19273 Lisp_Object eolvalue;
19274
19275 attrs = AREF (val, 0);
19276 eolvalue = AREF (val, 2);
19277
19278 if (multibyte)
19279 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19280
19281 if (eol_flag)
19282 {
19283 /* The EOL conversion that is normal on this system. */
19284
19285 if (NILP (eolvalue)) /* Not yet decided. */
19286 eoltype = eol_mnemonic_undecided;
19287 else if (VECTORP (eolvalue)) /* Not yet decided. */
19288 eoltype = eol_mnemonic_undecided;
19289 else /* eolvalue is Qunix, Qdos, or Qmac. */
19290 eoltype = (EQ (eolvalue, Qunix)
19291 ? eol_mnemonic_unix
19292 : (EQ (eolvalue, Qdos) == 1
19293 ? eol_mnemonic_dos : eol_mnemonic_mac));
19294 }
19295 }
19296
19297 if (eol_flag)
19298 {
19299 /* Mention the EOL conversion if it is not the usual one. */
19300 if (STRINGP (eoltype))
19301 {
19302 eol_str = SDATA (eoltype);
19303 eol_str_len = SBYTES (eoltype);
19304 }
19305 else if (CHARACTERP (eoltype))
19306 {
19307 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19308 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19309 eol_str = tmp;
19310 }
19311 else
19312 {
19313 eol_str = invalid_eol_type;
19314 eol_str_len = sizeof (invalid_eol_type) - 1;
19315 }
19316 memcpy (buf, eol_str, eol_str_len);
19317 buf += eol_str_len;
19318 }
19319
19320 return buf;
19321 }
19322
19323 /* Return a string for the output of a mode line %-spec for window W,
19324 generated by character C. FIELD_WIDTH > 0 means pad the string
19325 returned with spaces to that value. Return a Lisp string in
19326 *STRING if the resulting string is taken from that Lisp string.
19327
19328 Note we operate on the current buffer for most purposes,
19329 the exception being w->base_line_pos. */
19330
19331 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19332
19333 static const char *
19334 decode_mode_spec (struct window *w, register int c, int field_width,
19335 Lisp_Object *string)
19336 {
19337 Lisp_Object obj;
19338 struct frame *f = XFRAME (WINDOW_FRAME (w));
19339 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19340 struct buffer *b = current_buffer;
19341
19342 obj = Qnil;
19343 *string = Qnil;
19344
19345 switch (c)
19346 {
19347 case '*':
19348 if (!NILP (BVAR (b, read_only)))
19349 return "%";
19350 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19351 return "*";
19352 return "-";
19353
19354 case '+':
19355 /* This differs from %* only for a modified read-only buffer. */
19356 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19357 return "*";
19358 if (!NILP (BVAR (b, read_only)))
19359 return "%";
19360 return "-";
19361
19362 case '&':
19363 /* This differs from %* in ignoring read-only-ness. */
19364 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19365 return "*";
19366 return "-";
19367
19368 case '%':
19369 return "%";
19370
19371 case '[':
19372 {
19373 int i;
19374 char *p;
19375
19376 if (command_loop_level > 5)
19377 return "[[[... ";
19378 p = decode_mode_spec_buf;
19379 for (i = 0; i < command_loop_level; i++)
19380 *p++ = '[';
19381 *p = 0;
19382 return decode_mode_spec_buf;
19383 }
19384
19385 case ']':
19386 {
19387 int i;
19388 char *p;
19389
19390 if (command_loop_level > 5)
19391 return " ...]]]";
19392 p = decode_mode_spec_buf;
19393 for (i = 0; i < command_loop_level; i++)
19394 *p++ = ']';
19395 *p = 0;
19396 return decode_mode_spec_buf;
19397 }
19398
19399 case '-':
19400 {
19401 register int i;
19402
19403 /* Let lots_of_dashes be a string of infinite length. */
19404 if (mode_line_target == MODE_LINE_NOPROP ||
19405 mode_line_target == MODE_LINE_STRING)
19406 return "--";
19407 if (field_width <= 0
19408 || field_width > sizeof (lots_of_dashes))
19409 {
19410 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19411 decode_mode_spec_buf[i] = '-';
19412 decode_mode_spec_buf[i] = '\0';
19413 return decode_mode_spec_buf;
19414 }
19415 else
19416 return lots_of_dashes;
19417 }
19418
19419 case 'b':
19420 obj = BVAR (b, name);
19421 break;
19422
19423 case 'c':
19424 /* %c and %l are ignored in `frame-title-format'.
19425 (In redisplay_internal, the frame title is drawn _before_ the
19426 windows are updated, so the stuff which depends on actual
19427 window contents (such as %l) may fail to render properly, or
19428 even crash emacs.) */
19429 if (mode_line_target == MODE_LINE_TITLE)
19430 return "";
19431 else
19432 {
19433 EMACS_INT col = current_column ();
19434 w->column_number_displayed = make_number (col);
19435 pint2str (decode_mode_spec_buf, field_width, col);
19436 return decode_mode_spec_buf;
19437 }
19438
19439 case 'e':
19440 #ifndef SYSTEM_MALLOC
19441 {
19442 if (NILP (Vmemory_full))
19443 return "";
19444 else
19445 return "!MEM FULL! ";
19446 }
19447 #else
19448 return "";
19449 #endif
19450
19451 case 'F':
19452 /* %F displays the frame name. */
19453 if (!NILP (f->title))
19454 return SSDATA (f->title);
19455 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19456 return SSDATA (f->name);
19457 return "Emacs";
19458
19459 case 'f':
19460 obj = BVAR (b, filename);
19461 break;
19462
19463 case 'i':
19464 {
19465 EMACS_INT size = ZV - BEGV;
19466 pint2str (decode_mode_spec_buf, field_width, size);
19467 return decode_mode_spec_buf;
19468 }
19469
19470 case 'I':
19471 {
19472 EMACS_INT size = ZV - BEGV;
19473 pint2hrstr (decode_mode_spec_buf, field_width, size);
19474 return decode_mode_spec_buf;
19475 }
19476
19477 case 'l':
19478 {
19479 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19480 EMACS_INT topline, nlines, height;
19481 EMACS_INT junk;
19482
19483 /* %c and %l are ignored in `frame-title-format'. */
19484 if (mode_line_target == MODE_LINE_TITLE)
19485 return "";
19486
19487 startpos = XMARKER (w->start)->charpos;
19488 startpos_byte = marker_byte_position (w->start);
19489 height = WINDOW_TOTAL_LINES (w);
19490
19491 /* If we decided that this buffer isn't suitable for line numbers,
19492 don't forget that too fast. */
19493 if (EQ (w->base_line_pos, w->buffer))
19494 goto no_value;
19495 /* But do forget it, if the window shows a different buffer now. */
19496 else if (BUFFERP (w->base_line_pos))
19497 w->base_line_pos = Qnil;
19498
19499 /* If the buffer is very big, don't waste time. */
19500 if (INTEGERP (Vline_number_display_limit)
19501 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19502 {
19503 w->base_line_pos = Qnil;
19504 w->base_line_number = Qnil;
19505 goto no_value;
19506 }
19507
19508 if (INTEGERP (w->base_line_number)
19509 && INTEGERP (w->base_line_pos)
19510 && XFASTINT (w->base_line_pos) <= startpos)
19511 {
19512 line = XFASTINT (w->base_line_number);
19513 linepos = XFASTINT (w->base_line_pos);
19514 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19515 }
19516 else
19517 {
19518 line = 1;
19519 linepos = BUF_BEGV (b);
19520 linepos_byte = BUF_BEGV_BYTE (b);
19521 }
19522
19523 /* Count lines from base line to window start position. */
19524 nlines = display_count_lines (linepos_byte,
19525 startpos_byte,
19526 startpos, &junk);
19527
19528 topline = nlines + line;
19529
19530 /* Determine a new base line, if the old one is too close
19531 or too far away, or if we did not have one.
19532 "Too close" means it's plausible a scroll-down would
19533 go back past it. */
19534 if (startpos == BUF_BEGV (b))
19535 {
19536 w->base_line_number = make_number (topline);
19537 w->base_line_pos = make_number (BUF_BEGV (b));
19538 }
19539 else if (nlines < height + 25 || nlines > height * 3 + 50
19540 || linepos == BUF_BEGV (b))
19541 {
19542 EMACS_INT limit = BUF_BEGV (b);
19543 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19544 EMACS_INT position;
19545 EMACS_INT distance =
19546 (height * 2 + 30) * line_number_display_limit_width;
19547
19548 if (startpos - distance > limit)
19549 {
19550 limit = startpos - distance;
19551 limit_byte = CHAR_TO_BYTE (limit);
19552 }
19553
19554 nlines = display_count_lines (startpos_byte,
19555 limit_byte,
19556 - (height * 2 + 30),
19557 &position);
19558 /* If we couldn't find the lines we wanted within
19559 line_number_display_limit_width chars per line,
19560 give up on line numbers for this window. */
19561 if (position == limit_byte && limit == startpos - distance)
19562 {
19563 w->base_line_pos = w->buffer;
19564 w->base_line_number = Qnil;
19565 goto no_value;
19566 }
19567
19568 w->base_line_number = make_number (topline - nlines);
19569 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19570 }
19571
19572 /* Now count lines from the start pos to point. */
19573 nlines = display_count_lines (startpos_byte,
19574 PT_BYTE, PT, &junk);
19575
19576 /* Record that we did display the line number. */
19577 line_number_displayed = 1;
19578
19579 /* Make the string to show. */
19580 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19581 return decode_mode_spec_buf;
19582 no_value:
19583 {
19584 char* p = decode_mode_spec_buf;
19585 int pad = field_width - 2;
19586 while (pad-- > 0)
19587 *p++ = ' ';
19588 *p++ = '?';
19589 *p++ = '?';
19590 *p = '\0';
19591 return decode_mode_spec_buf;
19592 }
19593 }
19594 break;
19595
19596 case 'm':
19597 obj = BVAR (b, mode_name);
19598 break;
19599
19600 case 'n':
19601 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19602 return " Narrow";
19603 break;
19604
19605 case 'p':
19606 {
19607 EMACS_INT pos = marker_position (w->start);
19608 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19609
19610 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19611 {
19612 if (pos <= BUF_BEGV (b))
19613 return "All";
19614 else
19615 return "Bottom";
19616 }
19617 else if (pos <= BUF_BEGV (b))
19618 return "Top";
19619 else
19620 {
19621 if (total > 1000000)
19622 /* Do it differently for a large value, to avoid overflow. */
19623 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19624 else
19625 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19626 /* We can't normally display a 3-digit number,
19627 so get us a 2-digit number that is close. */
19628 if (total == 100)
19629 total = 99;
19630 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
19631 return decode_mode_spec_buf;
19632 }
19633 }
19634
19635 /* Display percentage of size above the bottom of the screen. */
19636 case 'P':
19637 {
19638 EMACS_INT toppos = marker_position (w->start);
19639 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19640 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19641
19642 if (botpos >= BUF_ZV (b))
19643 {
19644 if (toppos <= BUF_BEGV (b))
19645 return "All";
19646 else
19647 return "Bottom";
19648 }
19649 else
19650 {
19651 if (total > 1000000)
19652 /* Do it differently for a large value, to avoid overflow. */
19653 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19654 else
19655 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19656 /* We can't normally display a 3-digit number,
19657 so get us a 2-digit number that is close. */
19658 if (total == 100)
19659 total = 99;
19660 if (toppos <= BUF_BEGV (b))
19661 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
19662 else
19663 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
19664 return decode_mode_spec_buf;
19665 }
19666 }
19667
19668 case 's':
19669 /* status of process */
19670 obj = Fget_buffer_process (Fcurrent_buffer ());
19671 if (NILP (obj))
19672 return "no process";
19673 #ifndef MSDOS
19674 obj = Fsymbol_name (Fprocess_status (obj));
19675 #endif
19676 break;
19677
19678 case '@':
19679 {
19680 int count = inhibit_garbage_collection ();
19681 Lisp_Object val = call1 (intern ("file-remote-p"),
19682 BVAR (current_buffer, directory));
19683 unbind_to (count, Qnil);
19684
19685 if (NILP (val))
19686 return "-";
19687 else
19688 return "@";
19689 }
19690
19691 case 't': /* indicate TEXT or BINARY */
19692 return "T";
19693
19694 case 'z':
19695 /* coding-system (not including end-of-line format) */
19696 case 'Z':
19697 /* coding-system (including end-of-line type) */
19698 {
19699 int eol_flag = (c == 'Z');
19700 char *p = decode_mode_spec_buf;
19701
19702 if (! FRAME_WINDOW_P (f))
19703 {
19704 /* No need to mention EOL here--the terminal never needs
19705 to do EOL conversion. */
19706 p = decode_mode_spec_coding (CODING_ID_NAME
19707 (FRAME_KEYBOARD_CODING (f)->id),
19708 p, 0);
19709 p = decode_mode_spec_coding (CODING_ID_NAME
19710 (FRAME_TERMINAL_CODING (f)->id),
19711 p, 0);
19712 }
19713 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
19714 p, eol_flag);
19715
19716 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19717 #ifdef subprocesses
19718 obj = Fget_buffer_process (Fcurrent_buffer ());
19719 if (PROCESSP (obj))
19720 {
19721 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19722 p, eol_flag);
19723 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19724 p, eol_flag);
19725 }
19726 #endif /* subprocesses */
19727 #endif /* 0 */
19728 *p = 0;
19729 return decode_mode_spec_buf;
19730 }
19731 }
19732
19733 if (STRINGP (obj))
19734 {
19735 *string = obj;
19736 return SSDATA (obj);
19737 }
19738 else
19739 return "";
19740 }
19741
19742
19743 /* Count up to COUNT lines starting from START_BYTE.
19744 But don't go beyond LIMIT_BYTE.
19745 Return the number of lines thus found (always nonnegative).
19746
19747 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19748
19749 static EMACS_INT
19750 display_count_lines (EMACS_INT start_byte,
19751 EMACS_INT limit_byte, EMACS_INT count,
19752 EMACS_INT *byte_pos_ptr)
19753 {
19754 register unsigned char *cursor;
19755 unsigned char *base;
19756
19757 register EMACS_INT ceiling;
19758 register unsigned char *ceiling_addr;
19759 EMACS_INT orig_count = count;
19760
19761 /* If we are not in selective display mode,
19762 check only for newlines. */
19763 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
19764 && !INTEGERP (BVAR (current_buffer, selective_display)));
19765
19766 if (count > 0)
19767 {
19768 while (start_byte < limit_byte)
19769 {
19770 ceiling = BUFFER_CEILING_OF (start_byte);
19771 ceiling = min (limit_byte - 1, ceiling);
19772 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19773 base = (cursor = BYTE_POS_ADDR (start_byte));
19774 while (1)
19775 {
19776 if (selective_display)
19777 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19778 ;
19779 else
19780 while (*cursor != '\n' && ++cursor != ceiling_addr)
19781 ;
19782
19783 if (cursor != ceiling_addr)
19784 {
19785 if (--count == 0)
19786 {
19787 start_byte += cursor - base + 1;
19788 *byte_pos_ptr = start_byte;
19789 return orig_count;
19790 }
19791 else
19792 if (++cursor == ceiling_addr)
19793 break;
19794 }
19795 else
19796 break;
19797 }
19798 start_byte += cursor - base;
19799 }
19800 }
19801 else
19802 {
19803 while (start_byte > limit_byte)
19804 {
19805 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19806 ceiling = max (limit_byte, ceiling);
19807 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19808 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19809 while (1)
19810 {
19811 if (selective_display)
19812 while (--cursor != ceiling_addr
19813 && *cursor != '\n' && *cursor != 015)
19814 ;
19815 else
19816 while (--cursor != ceiling_addr && *cursor != '\n')
19817 ;
19818
19819 if (cursor != ceiling_addr)
19820 {
19821 if (++count == 0)
19822 {
19823 start_byte += cursor - base + 1;
19824 *byte_pos_ptr = start_byte;
19825 /* When scanning backwards, we should
19826 not count the newline posterior to which we stop. */
19827 return - orig_count - 1;
19828 }
19829 }
19830 else
19831 break;
19832 }
19833 /* Here we add 1 to compensate for the last decrement
19834 of CURSOR, which took it past the valid range. */
19835 start_byte += cursor - base + 1;
19836 }
19837 }
19838
19839 *byte_pos_ptr = limit_byte;
19840
19841 if (count < 0)
19842 return - orig_count + count;
19843 return orig_count - count;
19844
19845 }
19846
19847
19848 \f
19849 /***********************************************************************
19850 Displaying strings
19851 ***********************************************************************/
19852
19853 /* Display a NUL-terminated string, starting with index START.
19854
19855 If STRING is non-null, display that C string. Otherwise, the Lisp
19856 string LISP_STRING is displayed. There's a case that STRING is
19857 non-null and LISP_STRING is not nil. It means STRING is a string
19858 data of LISP_STRING. In that case, we display LISP_STRING while
19859 ignoring its text properties.
19860
19861 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19862 FACE_STRING. Display STRING or LISP_STRING with the face at
19863 FACE_STRING_POS in FACE_STRING:
19864
19865 Display the string in the environment given by IT, but use the
19866 standard display table, temporarily.
19867
19868 FIELD_WIDTH is the minimum number of output glyphs to produce.
19869 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19870 with spaces. If STRING has more characters, more than FIELD_WIDTH
19871 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19872
19873 PRECISION is the maximum number of characters to output from
19874 STRING. PRECISION < 0 means don't truncate the string.
19875
19876 This is roughly equivalent to printf format specifiers:
19877
19878 FIELD_WIDTH PRECISION PRINTF
19879 ----------------------------------------
19880 -1 -1 %s
19881 -1 10 %.10s
19882 10 -1 %10s
19883 20 10 %20.10s
19884
19885 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19886 display them, and < 0 means obey the current buffer's value of
19887 enable_multibyte_characters.
19888
19889 Value is the number of columns displayed. */
19890
19891 static int
19892 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19893 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19894 int field_width, int precision, int max_x, int multibyte)
19895 {
19896 int hpos_at_start = it->hpos;
19897 int saved_face_id = it->face_id;
19898 struct glyph_row *row = it->glyph_row;
19899
19900 /* Initialize the iterator IT for iteration over STRING beginning
19901 with index START. */
19902 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19903 precision, field_width, multibyte);
19904 if (string && STRINGP (lisp_string))
19905 /* LISP_STRING is the one returned by decode_mode_spec. We should
19906 ignore its text properties. */
19907 it->stop_charpos = -1;
19908
19909 /* If displaying STRING, set up the face of the iterator
19910 from LISP_STRING, if that's given. */
19911 if (STRINGP (face_string))
19912 {
19913 EMACS_INT endptr;
19914 struct face *face;
19915
19916 it->face_id
19917 = face_at_string_position (it->w, face_string, face_string_pos,
19918 0, it->region_beg_charpos,
19919 it->region_end_charpos,
19920 &endptr, it->base_face_id, 0);
19921 face = FACE_FROM_ID (it->f, it->face_id);
19922 it->face_box_p = face->box != FACE_NO_BOX;
19923 }
19924
19925 /* Set max_x to the maximum allowed X position. Don't let it go
19926 beyond the right edge of the window. */
19927 if (max_x <= 0)
19928 max_x = it->last_visible_x;
19929 else
19930 max_x = min (max_x, it->last_visible_x);
19931
19932 /* Skip over display elements that are not visible. because IT->w is
19933 hscrolled. */
19934 if (it->current_x < it->first_visible_x)
19935 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19936 MOVE_TO_POS | MOVE_TO_X);
19937
19938 row->ascent = it->max_ascent;
19939 row->height = it->max_ascent + it->max_descent;
19940 row->phys_ascent = it->max_phys_ascent;
19941 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19942 row->extra_line_spacing = it->max_extra_line_spacing;
19943
19944 /* This condition is for the case that we are called with current_x
19945 past last_visible_x. */
19946 while (it->current_x < max_x)
19947 {
19948 int x_before, x, n_glyphs_before, i, nglyphs;
19949
19950 /* Get the next display element. */
19951 if (!get_next_display_element (it))
19952 break;
19953
19954 /* Produce glyphs. */
19955 x_before = it->current_x;
19956 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19957 PRODUCE_GLYPHS (it);
19958
19959 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19960 i = 0;
19961 x = x_before;
19962 while (i < nglyphs)
19963 {
19964 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19965
19966 if (it->line_wrap != TRUNCATE
19967 && x + glyph->pixel_width > max_x)
19968 {
19969 /* End of continued line or max_x reached. */
19970 if (CHAR_GLYPH_PADDING_P (*glyph))
19971 {
19972 /* A wide character is unbreakable. */
19973 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19974 it->current_x = x_before;
19975 }
19976 else
19977 {
19978 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19979 it->current_x = x;
19980 }
19981 break;
19982 }
19983 else if (x + glyph->pixel_width >= it->first_visible_x)
19984 {
19985 /* Glyph is at least partially visible. */
19986 ++it->hpos;
19987 if (x < it->first_visible_x)
19988 it->glyph_row->x = x - it->first_visible_x;
19989 }
19990 else
19991 {
19992 /* Glyph is off the left margin of the display area.
19993 Should not happen. */
19994 abort ();
19995 }
19996
19997 row->ascent = max (row->ascent, it->max_ascent);
19998 row->height = max (row->height, it->max_ascent + it->max_descent);
19999 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20000 row->phys_height = max (row->phys_height,
20001 it->max_phys_ascent + it->max_phys_descent);
20002 row->extra_line_spacing = max (row->extra_line_spacing,
20003 it->max_extra_line_spacing);
20004 x += glyph->pixel_width;
20005 ++i;
20006 }
20007
20008 /* Stop if max_x reached. */
20009 if (i < nglyphs)
20010 break;
20011
20012 /* Stop at line ends. */
20013 if (ITERATOR_AT_END_OF_LINE_P (it))
20014 {
20015 it->continuation_lines_width = 0;
20016 break;
20017 }
20018
20019 set_iterator_to_next (it, 1);
20020
20021 /* Stop if truncating at the right edge. */
20022 if (it->line_wrap == TRUNCATE
20023 && it->current_x >= it->last_visible_x)
20024 {
20025 /* Add truncation mark, but don't do it if the line is
20026 truncated at a padding space. */
20027 if (IT_CHARPOS (*it) < it->string_nchars)
20028 {
20029 if (!FRAME_WINDOW_P (it->f))
20030 {
20031 int ii, n;
20032
20033 if (it->current_x > it->last_visible_x)
20034 {
20035 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20036 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20037 break;
20038 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20039 {
20040 row->used[TEXT_AREA] = ii;
20041 produce_special_glyphs (it, IT_TRUNCATION);
20042 }
20043 }
20044 produce_special_glyphs (it, IT_TRUNCATION);
20045 }
20046 it->glyph_row->truncated_on_right_p = 1;
20047 }
20048 break;
20049 }
20050 }
20051
20052 /* Maybe insert a truncation at the left. */
20053 if (it->first_visible_x
20054 && IT_CHARPOS (*it) > 0)
20055 {
20056 if (!FRAME_WINDOW_P (it->f))
20057 insert_left_trunc_glyphs (it);
20058 it->glyph_row->truncated_on_left_p = 1;
20059 }
20060
20061 it->face_id = saved_face_id;
20062
20063 /* Value is number of columns displayed. */
20064 return it->hpos - hpos_at_start;
20065 }
20066
20067
20068 \f
20069 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20070 appears as an element of LIST or as the car of an element of LIST.
20071 If PROPVAL is a list, compare each element against LIST in that
20072 way, and return 1/2 if any element of PROPVAL is found in LIST.
20073 Otherwise return 0. This function cannot quit.
20074 The return value is 2 if the text is invisible but with an ellipsis
20075 and 1 if it's invisible and without an ellipsis. */
20076
20077 int
20078 invisible_p (register Lisp_Object propval, Lisp_Object list)
20079 {
20080 register Lisp_Object tail, proptail;
20081
20082 for (tail = list; CONSP (tail); tail = XCDR (tail))
20083 {
20084 register Lisp_Object tem;
20085 tem = XCAR (tail);
20086 if (EQ (propval, tem))
20087 return 1;
20088 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20089 return NILP (XCDR (tem)) ? 1 : 2;
20090 }
20091
20092 if (CONSP (propval))
20093 {
20094 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20095 {
20096 Lisp_Object propelt;
20097 propelt = XCAR (proptail);
20098 for (tail = list; CONSP (tail); tail = XCDR (tail))
20099 {
20100 register Lisp_Object tem;
20101 tem = XCAR (tail);
20102 if (EQ (propelt, tem))
20103 return 1;
20104 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20105 return NILP (XCDR (tem)) ? 1 : 2;
20106 }
20107 }
20108 }
20109
20110 return 0;
20111 }
20112
20113 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20114 doc: /* Non-nil if the property makes the text invisible.
20115 POS-OR-PROP can be a marker or number, in which case it is taken to be
20116 a position in the current buffer and the value of the `invisible' property
20117 is checked; or it can be some other value, which is then presumed to be the
20118 value of the `invisible' property of the text of interest.
20119 The non-nil value returned can be t for truly invisible text or something
20120 else if the text is replaced by an ellipsis. */)
20121 (Lisp_Object pos_or_prop)
20122 {
20123 Lisp_Object prop
20124 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20125 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20126 : pos_or_prop);
20127 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20128 return (invis == 0 ? Qnil
20129 : invis == 1 ? Qt
20130 : make_number (invis));
20131 }
20132
20133 /* Calculate a width or height in pixels from a specification using
20134 the following elements:
20135
20136 SPEC ::=
20137 NUM - a (fractional) multiple of the default font width/height
20138 (NUM) - specifies exactly NUM pixels
20139 UNIT - a fixed number of pixels, see below.
20140 ELEMENT - size of a display element in pixels, see below.
20141 (NUM . SPEC) - equals NUM * SPEC
20142 (+ SPEC SPEC ...) - add pixel values
20143 (- SPEC SPEC ...) - subtract pixel values
20144 (- SPEC) - negate pixel value
20145
20146 NUM ::=
20147 INT or FLOAT - a number constant
20148 SYMBOL - use symbol's (buffer local) variable binding.
20149
20150 UNIT ::=
20151 in - pixels per inch *)
20152 mm - pixels per 1/1000 meter *)
20153 cm - pixels per 1/100 meter *)
20154 width - width of current font in pixels.
20155 height - height of current font in pixels.
20156
20157 *) using the ratio(s) defined in display-pixels-per-inch.
20158
20159 ELEMENT ::=
20160
20161 left-fringe - left fringe width in pixels
20162 right-fringe - right fringe width in pixels
20163
20164 left-margin - left margin width in pixels
20165 right-margin - right margin width in pixels
20166
20167 scroll-bar - scroll-bar area width in pixels
20168
20169 Examples:
20170
20171 Pixels corresponding to 5 inches:
20172 (5 . in)
20173
20174 Total width of non-text areas on left side of window (if scroll-bar is on left):
20175 '(space :width (+ left-fringe left-margin scroll-bar))
20176
20177 Align to first text column (in header line):
20178 '(space :align-to 0)
20179
20180 Align to middle of text area minus half the width of variable `my-image'
20181 containing a loaded image:
20182 '(space :align-to (0.5 . (- text my-image)))
20183
20184 Width of left margin minus width of 1 character in the default font:
20185 '(space :width (- left-margin 1))
20186
20187 Width of left margin minus width of 2 characters in the current font:
20188 '(space :width (- left-margin (2 . width)))
20189
20190 Center 1 character over left-margin (in header line):
20191 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20192
20193 Different ways to express width of left fringe plus left margin minus one pixel:
20194 '(space :width (- (+ left-fringe left-margin) (1)))
20195 '(space :width (+ left-fringe left-margin (- (1))))
20196 '(space :width (+ left-fringe left-margin (-1)))
20197
20198 */
20199
20200 #define NUMVAL(X) \
20201 ((INTEGERP (X) || FLOATP (X)) \
20202 ? XFLOATINT (X) \
20203 : - 1)
20204
20205 int
20206 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20207 struct font *font, int width_p, int *align_to)
20208 {
20209 double pixels;
20210
20211 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20212 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20213
20214 if (NILP (prop))
20215 return OK_PIXELS (0);
20216
20217 xassert (FRAME_LIVE_P (it->f));
20218
20219 if (SYMBOLP (prop))
20220 {
20221 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20222 {
20223 char *unit = SSDATA (SYMBOL_NAME (prop));
20224
20225 if (unit[0] == 'i' && unit[1] == 'n')
20226 pixels = 1.0;
20227 else if (unit[0] == 'm' && unit[1] == 'm')
20228 pixels = 25.4;
20229 else if (unit[0] == 'c' && unit[1] == 'm')
20230 pixels = 2.54;
20231 else
20232 pixels = 0;
20233 if (pixels > 0)
20234 {
20235 double ppi;
20236 #ifdef HAVE_WINDOW_SYSTEM
20237 if (FRAME_WINDOW_P (it->f)
20238 && (ppi = (width_p
20239 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20240 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20241 ppi > 0))
20242 return OK_PIXELS (ppi / pixels);
20243 #endif
20244
20245 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20246 || (CONSP (Vdisplay_pixels_per_inch)
20247 && (ppi = (width_p
20248 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20249 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20250 ppi > 0)))
20251 return OK_PIXELS (ppi / pixels);
20252
20253 return 0;
20254 }
20255 }
20256
20257 #ifdef HAVE_WINDOW_SYSTEM
20258 if (EQ (prop, Qheight))
20259 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20260 if (EQ (prop, Qwidth))
20261 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20262 #else
20263 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20264 return OK_PIXELS (1);
20265 #endif
20266
20267 if (EQ (prop, Qtext))
20268 return OK_PIXELS (width_p
20269 ? window_box_width (it->w, TEXT_AREA)
20270 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20271
20272 if (align_to && *align_to < 0)
20273 {
20274 *res = 0;
20275 if (EQ (prop, Qleft))
20276 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20277 if (EQ (prop, Qright))
20278 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20279 if (EQ (prop, Qcenter))
20280 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20281 + window_box_width (it->w, TEXT_AREA) / 2);
20282 if (EQ (prop, Qleft_fringe))
20283 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20284 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20285 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20286 if (EQ (prop, Qright_fringe))
20287 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20288 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20289 : window_box_right_offset (it->w, TEXT_AREA));
20290 if (EQ (prop, Qleft_margin))
20291 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20292 if (EQ (prop, Qright_margin))
20293 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20294 if (EQ (prop, Qscroll_bar))
20295 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20296 ? 0
20297 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20298 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20299 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20300 : 0)));
20301 }
20302 else
20303 {
20304 if (EQ (prop, Qleft_fringe))
20305 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20306 if (EQ (prop, Qright_fringe))
20307 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20308 if (EQ (prop, Qleft_margin))
20309 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20310 if (EQ (prop, Qright_margin))
20311 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20312 if (EQ (prop, Qscroll_bar))
20313 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20314 }
20315
20316 prop = Fbuffer_local_value (prop, it->w->buffer);
20317 }
20318
20319 if (INTEGERP (prop) || FLOATP (prop))
20320 {
20321 int base_unit = (width_p
20322 ? FRAME_COLUMN_WIDTH (it->f)
20323 : FRAME_LINE_HEIGHT (it->f));
20324 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20325 }
20326
20327 if (CONSP (prop))
20328 {
20329 Lisp_Object car = XCAR (prop);
20330 Lisp_Object cdr = XCDR (prop);
20331
20332 if (SYMBOLP (car))
20333 {
20334 #ifdef HAVE_WINDOW_SYSTEM
20335 if (FRAME_WINDOW_P (it->f)
20336 && valid_image_p (prop))
20337 {
20338 int id = lookup_image (it->f, prop);
20339 struct image *img = IMAGE_FROM_ID (it->f, id);
20340
20341 return OK_PIXELS (width_p ? img->width : img->height);
20342 }
20343 #endif
20344 if (EQ (car, Qplus) || EQ (car, Qminus))
20345 {
20346 int first = 1;
20347 double px;
20348
20349 pixels = 0;
20350 while (CONSP (cdr))
20351 {
20352 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20353 font, width_p, align_to))
20354 return 0;
20355 if (first)
20356 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20357 else
20358 pixels += px;
20359 cdr = XCDR (cdr);
20360 }
20361 if (EQ (car, Qminus))
20362 pixels = -pixels;
20363 return OK_PIXELS (pixels);
20364 }
20365
20366 car = Fbuffer_local_value (car, it->w->buffer);
20367 }
20368
20369 if (INTEGERP (car) || FLOATP (car))
20370 {
20371 double fact;
20372 pixels = XFLOATINT (car);
20373 if (NILP (cdr))
20374 return OK_PIXELS (pixels);
20375 if (calc_pixel_width_or_height (&fact, it, cdr,
20376 font, width_p, align_to))
20377 return OK_PIXELS (pixels * fact);
20378 return 0;
20379 }
20380
20381 return 0;
20382 }
20383
20384 return 0;
20385 }
20386
20387 \f
20388 /***********************************************************************
20389 Glyph Display
20390 ***********************************************************************/
20391
20392 #ifdef HAVE_WINDOW_SYSTEM
20393
20394 #if GLYPH_DEBUG
20395
20396 void
20397 dump_glyph_string (s)
20398 struct glyph_string *s;
20399 {
20400 fprintf (stderr, "glyph string\n");
20401 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20402 s->x, s->y, s->width, s->height);
20403 fprintf (stderr, " ybase = %d\n", s->ybase);
20404 fprintf (stderr, " hl = %d\n", s->hl);
20405 fprintf (stderr, " left overhang = %d, right = %d\n",
20406 s->left_overhang, s->right_overhang);
20407 fprintf (stderr, " nchars = %d\n", s->nchars);
20408 fprintf (stderr, " extends to end of line = %d\n",
20409 s->extends_to_end_of_line_p);
20410 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20411 fprintf (stderr, " bg width = %d\n", s->background_width);
20412 }
20413
20414 #endif /* GLYPH_DEBUG */
20415
20416 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20417 of XChar2b structures for S; it can't be allocated in
20418 init_glyph_string because it must be allocated via `alloca'. W
20419 is the window on which S is drawn. ROW and AREA are the glyph row
20420 and area within the row from which S is constructed. START is the
20421 index of the first glyph structure covered by S. HL is a
20422 face-override for drawing S. */
20423
20424 #ifdef HAVE_NTGUI
20425 #define OPTIONAL_HDC(hdc) HDC hdc,
20426 #define DECLARE_HDC(hdc) HDC hdc;
20427 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20428 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20429 #endif
20430
20431 #ifndef OPTIONAL_HDC
20432 #define OPTIONAL_HDC(hdc)
20433 #define DECLARE_HDC(hdc)
20434 #define ALLOCATE_HDC(hdc, f)
20435 #define RELEASE_HDC(hdc, f)
20436 #endif
20437
20438 static void
20439 init_glyph_string (struct glyph_string *s,
20440 OPTIONAL_HDC (hdc)
20441 XChar2b *char2b, struct window *w, struct glyph_row *row,
20442 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20443 {
20444 memset (s, 0, sizeof *s);
20445 s->w = w;
20446 s->f = XFRAME (w->frame);
20447 #ifdef HAVE_NTGUI
20448 s->hdc = hdc;
20449 #endif
20450 s->display = FRAME_X_DISPLAY (s->f);
20451 s->window = FRAME_X_WINDOW (s->f);
20452 s->char2b = char2b;
20453 s->hl = hl;
20454 s->row = row;
20455 s->area = area;
20456 s->first_glyph = row->glyphs[area] + start;
20457 s->height = row->height;
20458 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20459 s->ybase = s->y + row->ascent;
20460 }
20461
20462
20463 /* Append the list of glyph strings with head H and tail T to the list
20464 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20465
20466 static INLINE void
20467 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20468 struct glyph_string *h, struct glyph_string *t)
20469 {
20470 if (h)
20471 {
20472 if (*head)
20473 (*tail)->next = h;
20474 else
20475 *head = h;
20476 h->prev = *tail;
20477 *tail = t;
20478 }
20479 }
20480
20481
20482 /* Prepend the list of glyph strings with head H and tail T to the
20483 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20484 result. */
20485
20486 static INLINE void
20487 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20488 struct glyph_string *h, struct glyph_string *t)
20489 {
20490 if (h)
20491 {
20492 if (*head)
20493 (*head)->prev = t;
20494 else
20495 *tail = t;
20496 t->next = *head;
20497 *head = h;
20498 }
20499 }
20500
20501
20502 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20503 Set *HEAD and *TAIL to the resulting list. */
20504
20505 static INLINE void
20506 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20507 struct glyph_string *s)
20508 {
20509 s->next = s->prev = NULL;
20510 append_glyph_string_lists (head, tail, s, s);
20511 }
20512
20513
20514 /* Get face and two-byte form of character C in face FACE_ID on frame F.
20515 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
20516 make sure that X resources for the face returned are allocated.
20517 Value is a pointer to a realized face that is ready for display if
20518 DISPLAY_P is non-zero. */
20519
20520 static INLINE struct face *
20521 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20522 XChar2b *char2b, int display_p)
20523 {
20524 struct face *face = FACE_FROM_ID (f, face_id);
20525
20526 if (face->font)
20527 {
20528 unsigned code = face->font->driver->encode_char (face->font, c);
20529
20530 if (code != FONT_INVALID_CODE)
20531 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20532 else
20533 STORE_XCHAR2B (char2b, 0, 0);
20534 }
20535
20536 /* Make sure X resources of the face are allocated. */
20537 #ifdef HAVE_X_WINDOWS
20538 if (display_p)
20539 #endif
20540 {
20541 xassert (face != NULL);
20542 PREPARE_FACE_FOR_DISPLAY (f, face);
20543 }
20544
20545 return face;
20546 }
20547
20548
20549 /* Get face and two-byte form of character glyph GLYPH on frame F.
20550 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20551 a pointer to a realized face that is ready for display. */
20552
20553 static INLINE struct face *
20554 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20555 XChar2b *char2b, int *two_byte_p)
20556 {
20557 struct face *face;
20558
20559 xassert (glyph->type == CHAR_GLYPH);
20560 face = FACE_FROM_ID (f, glyph->face_id);
20561
20562 if (two_byte_p)
20563 *two_byte_p = 0;
20564
20565 if (face->font)
20566 {
20567 unsigned code;
20568
20569 if (CHAR_BYTE8_P (glyph->u.ch))
20570 code = CHAR_TO_BYTE8 (glyph->u.ch);
20571 else
20572 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20573
20574 if (code != FONT_INVALID_CODE)
20575 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20576 else
20577 STORE_XCHAR2B (char2b, 0, 0);
20578 }
20579
20580 /* Make sure X resources of the face are allocated. */
20581 xassert (face != NULL);
20582 PREPARE_FACE_FOR_DISPLAY (f, face);
20583 return face;
20584 }
20585
20586
20587 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20588 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20589
20590 static INLINE int
20591 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20592 {
20593 unsigned code;
20594
20595 if (CHAR_BYTE8_P (c))
20596 code = CHAR_TO_BYTE8 (c);
20597 else
20598 code = font->driver->encode_char (font, c);
20599
20600 if (code == FONT_INVALID_CODE)
20601 return 0;
20602 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20603 return 1;
20604 }
20605
20606
20607 /* Fill glyph string S with composition components specified by S->cmp.
20608
20609 BASE_FACE is the base face of the composition.
20610 S->cmp_from is the index of the first component for S.
20611
20612 OVERLAPS non-zero means S should draw the foreground only, and use
20613 its physical height for clipping. See also draw_glyphs.
20614
20615 Value is the index of a component not in S. */
20616
20617 static int
20618 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20619 int overlaps)
20620 {
20621 int i;
20622 /* For all glyphs of this composition, starting at the offset
20623 S->cmp_from, until we reach the end of the definition or encounter a
20624 glyph that requires the different face, add it to S. */
20625 struct face *face;
20626
20627 xassert (s);
20628
20629 s->for_overlaps = overlaps;
20630 s->face = NULL;
20631 s->font = NULL;
20632 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20633 {
20634 int c = COMPOSITION_GLYPH (s->cmp, i);
20635
20636 if (c != '\t')
20637 {
20638 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20639 -1, Qnil);
20640
20641 face = get_char_face_and_encoding (s->f, c, face_id,
20642 s->char2b + i, 1);
20643 if (face)
20644 {
20645 if (! s->face)
20646 {
20647 s->face = face;
20648 s->font = s->face->font;
20649 }
20650 else if (s->face != face)
20651 break;
20652 }
20653 }
20654 ++s->nchars;
20655 }
20656 s->cmp_to = i;
20657
20658 /* All glyph strings for the same composition has the same width,
20659 i.e. the width set for the first component of the composition. */
20660 s->width = s->first_glyph->pixel_width;
20661
20662 /* If the specified font could not be loaded, use the frame's
20663 default font, but record the fact that we couldn't load it in
20664 the glyph string so that we can draw rectangles for the
20665 characters of the glyph string. */
20666 if (s->font == NULL)
20667 {
20668 s->font_not_found_p = 1;
20669 s->font = FRAME_FONT (s->f);
20670 }
20671
20672 /* Adjust base line for subscript/superscript text. */
20673 s->ybase += s->first_glyph->voffset;
20674
20675 /* This glyph string must always be drawn with 16-bit functions. */
20676 s->two_byte_p = 1;
20677
20678 return s->cmp_to;
20679 }
20680
20681 static int
20682 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20683 int start, int end, int overlaps)
20684 {
20685 struct glyph *glyph, *last;
20686 Lisp_Object lgstring;
20687 int i;
20688
20689 s->for_overlaps = overlaps;
20690 glyph = s->row->glyphs[s->area] + start;
20691 last = s->row->glyphs[s->area] + end;
20692 s->cmp_id = glyph->u.cmp.id;
20693 s->cmp_from = glyph->slice.cmp.from;
20694 s->cmp_to = glyph->slice.cmp.to + 1;
20695 s->face = FACE_FROM_ID (s->f, face_id);
20696 lgstring = composition_gstring_from_id (s->cmp_id);
20697 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20698 glyph++;
20699 while (glyph < last
20700 && glyph->u.cmp.automatic
20701 && glyph->u.cmp.id == s->cmp_id
20702 && s->cmp_to == glyph->slice.cmp.from)
20703 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20704
20705 for (i = s->cmp_from; i < s->cmp_to; i++)
20706 {
20707 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20708 unsigned code = LGLYPH_CODE (lglyph);
20709
20710 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20711 }
20712 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20713 return glyph - s->row->glyphs[s->area];
20714 }
20715
20716
20717 /* Fill glyph string S from a sequence glyphs for glyphless characters.
20718 See the comment of fill_glyph_string for arguments.
20719 Value is the index of the first glyph not in S. */
20720
20721
20722 static int
20723 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
20724 int start, int end, int overlaps)
20725 {
20726 struct glyph *glyph, *last;
20727 int voffset;
20728
20729 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
20730 s->for_overlaps = overlaps;
20731 glyph = s->row->glyphs[s->area] + start;
20732 last = s->row->glyphs[s->area] + end;
20733 voffset = glyph->voffset;
20734 s->face = FACE_FROM_ID (s->f, face_id);
20735 s->font = s->face->font;
20736 s->nchars = 1;
20737 s->width = glyph->pixel_width;
20738 glyph++;
20739 while (glyph < last
20740 && glyph->type == GLYPHLESS_GLYPH
20741 && glyph->voffset == voffset
20742 && glyph->face_id == face_id)
20743 {
20744 s->nchars++;
20745 s->width += glyph->pixel_width;
20746 glyph++;
20747 }
20748 s->ybase += voffset;
20749 return glyph - s->row->glyphs[s->area];
20750 }
20751
20752
20753 /* Fill glyph string S from a sequence of character glyphs.
20754
20755 FACE_ID is the face id of the string. START is the index of the
20756 first glyph to consider, END is the index of the last + 1.
20757 OVERLAPS non-zero means S should draw the foreground only, and use
20758 its physical height for clipping. See also draw_glyphs.
20759
20760 Value is the index of the first glyph not in S. */
20761
20762 static int
20763 fill_glyph_string (struct glyph_string *s, int face_id,
20764 int start, int end, int overlaps)
20765 {
20766 struct glyph *glyph, *last;
20767 int voffset;
20768 int glyph_not_available_p;
20769
20770 xassert (s->f == XFRAME (s->w->frame));
20771 xassert (s->nchars == 0);
20772 xassert (start >= 0 && end > start);
20773
20774 s->for_overlaps = overlaps;
20775 glyph = s->row->glyphs[s->area] + start;
20776 last = s->row->glyphs[s->area] + end;
20777 voffset = glyph->voffset;
20778 s->padding_p = glyph->padding_p;
20779 glyph_not_available_p = glyph->glyph_not_available_p;
20780
20781 while (glyph < last
20782 && glyph->type == CHAR_GLYPH
20783 && glyph->voffset == voffset
20784 /* Same face id implies same font, nowadays. */
20785 && glyph->face_id == face_id
20786 && glyph->glyph_not_available_p == glyph_not_available_p)
20787 {
20788 int two_byte_p;
20789
20790 s->face = get_glyph_face_and_encoding (s->f, glyph,
20791 s->char2b + s->nchars,
20792 &two_byte_p);
20793 s->two_byte_p = two_byte_p;
20794 ++s->nchars;
20795 xassert (s->nchars <= end - start);
20796 s->width += glyph->pixel_width;
20797 if (glyph++->padding_p != s->padding_p)
20798 break;
20799 }
20800
20801 s->font = s->face->font;
20802
20803 /* If the specified font could not be loaded, use the frame's font,
20804 but record the fact that we couldn't load it in
20805 S->font_not_found_p so that we can draw rectangles for the
20806 characters of the glyph string. */
20807 if (s->font == NULL || glyph_not_available_p)
20808 {
20809 s->font_not_found_p = 1;
20810 s->font = FRAME_FONT (s->f);
20811 }
20812
20813 /* Adjust base line for subscript/superscript text. */
20814 s->ybase += voffset;
20815
20816 xassert (s->face && s->face->gc);
20817 return glyph - s->row->glyphs[s->area];
20818 }
20819
20820
20821 /* Fill glyph string S from image glyph S->first_glyph. */
20822
20823 static void
20824 fill_image_glyph_string (struct glyph_string *s)
20825 {
20826 xassert (s->first_glyph->type == IMAGE_GLYPH);
20827 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20828 xassert (s->img);
20829 s->slice = s->first_glyph->slice.img;
20830 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20831 s->font = s->face->font;
20832 s->width = s->first_glyph->pixel_width;
20833
20834 /* Adjust base line for subscript/superscript text. */
20835 s->ybase += s->first_glyph->voffset;
20836 }
20837
20838
20839 /* Fill glyph string S from a sequence of stretch glyphs.
20840
20841 START is the index of the first glyph to consider,
20842 END is the index of the last + 1.
20843
20844 Value is the index of the first glyph not in S. */
20845
20846 static int
20847 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
20848 {
20849 struct glyph *glyph, *last;
20850 int voffset, face_id;
20851
20852 xassert (s->first_glyph->type == STRETCH_GLYPH);
20853
20854 glyph = s->row->glyphs[s->area] + start;
20855 last = s->row->glyphs[s->area] + end;
20856 face_id = glyph->face_id;
20857 s->face = FACE_FROM_ID (s->f, face_id);
20858 s->font = s->face->font;
20859 s->width = glyph->pixel_width;
20860 s->nchars = 1;
20861 voffset = glyph->voffset;
20862
20863 for (++glyph;
20864 (glyph < last
20865 && glyph->type == STRETCH_GLYPH
20866 && glyph->voffset == voffset
20867 && glyph->face_id == face_id);
20868 ++glyph)
20869 s->width += glyph->pixel_width;
20870
20871 /* Adjust base line for subscript/superscript text. */
20872 s->ybase += voffset;
20873
20874 /* The case that face->gc == 0 is handled when drawing the glyph
20875 string by calling PREPARE_FACE_FOR_DISPLAY. */
20876 xassert (s->face);
20877 return glyph - s->row->glyphs[s->area];
20878 }
20879
20880 static struct font_metrics *
20881 get_per_char_metric (struct font *font, XChar2b *char2b)
20882 {
20883 static struct font_metrics metrics;
20884 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20885
20886 if (! font || code == FONT_INVALID_CODE)
20887 return NULL;
20888 font->driver->text_extents (font, &code, 1, &metrics);
20889 return &metrics;
20890 }
20891
20892 /* EXPORT for RIF:
20893 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20894 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20895 assumed to be zero. */
20896
20897 void
20898 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20899 {
20900 *left = *right = 0;
20901
20902 if (glyph->type == CHAR_GLYPH)
20903 {
20904 struct face *face;
20905 XChar2b char2b;
20906 struct font_metrics *pcm;
20907
20908 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20909 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
20910 {
20911 if (pcm->rbearing > pcm->width)
20912 *right = pcm->rbearing - pcm->width;
20913 if (pcm->lbearing < 0)
20914 *left = -pcm->lbearing;
20915 }
20916 }
20917 else if (glyph->type == COMPOSITE_GLYPH)
20918 {
20919 if (! glyph->u.cmp.automatic)
20920 {
20921 struct composition *cmp = composition_table[glyph->u.cmp.id];
20922
20923 if (cmp->rbearing > cmp->pixel_width)
20924 *right = cmp->rbearing - cmp->pixel_width;
20925 if (cmp->lbearing < 0)
20926 *left = - cmp->lbearing;
20927 }
20928 else
20929 {
20930 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20931 struct font_metrics metrics;
20932
20933 composition_gstring_width (gstring, glyph->slice.cmp.from,
20934 glyph->slice.cmp.to + 1, &metrics);
20935 if (metrics.rbearing > metrics.width)
20936 *right = metrics.rbearing - metrics.width;
20937 if (metrics.lbearing < 0)
20938 *left = - metrics.lbearing;
20939 }
20940 }
20941 }
20942
20943
20944 /* Return the index of the first glyph preceding glyph string S that
20945 is overwritten by S because of S's left overhang. Value is -1
20946 if no glyphs are overwritten. */
20947
20948 static int
20949 left_overwritten (struct glyph_string *s)
20950 {
20951 int k;
20952
20953 if (s->left_overhang)
20954 {
20955 int x = 0, i;
20956 struct glyph *glyphs = s->row->glyphs[s->area];
20957 int first = s->first_glyph - glyphs;
20958
20959 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20960 x -= glyphs[i].pixel_width;
20961
20962 k = i + 1;
20963 }
20964 else
20965 k = -1;
20966
20967 return k;
20968 }
20969
20970
20971 /* Return the index of the first glyph preceding glyph string S that
20972 is overwriting S because of its right overhang. Value is -1 if no
20973 glyph in front of S overwrites S. */
20974
20975 static int
20976 left_overwriting (struct glyph_string *s)
20977 {
20978 int i, k, x;
20979 struct glyph *glyphs = s->row->glyphs[s->area];
20980 int first = s->first_glyph - glyphs;
20981
20982 k = -1;
20983 x = 0;
20984 for (i = first - 1; i >= 0; --i)
20985 {
20986 int left, right;
20987 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20988 if (x + right > 0)
20989 k = i;
20990 x -= glyphs[i].pixel_width;
20991 }
20992
20993 return k;
20994 }
20995
20996
20997 /* Return the index of the last glyph following glyph string S that is
20998 overwritten by S because of S's right overhang. Value is -1 if
20999 no such glyph is found. */
21000
21001 static int
21002 right_overwritten (struct glyph_string *s)
21003 {
21004 int k = -1;
21005
21006 if (s->right_overhang)
21007 {
21008 int x = 0, i;
21009 struct glyph *glyphs = s->row->glyphs[s->area];
21010 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21011 int end = s->row->used[s->area];
21012
21013 for (i = first; i < end && s->right_overhang > x; ++i)
21014 x += glyphs[i].pixel_width;
21015
21016 k = i;
21017 }
21018
21019 return k;
21020 }
21021
21022
21023 /* Return the index of the last glyph following glyph string S that
21024 overwrites S because of its left overhang. Value is negative
21025 if no such glyph is found. */
21026
21027 static int
21028 right_overwriting (struct glyph_string *s)
21029 {
21030 int i, k, x;
21031 int end = s->row->used[s->area];
21032 struct glyph *glyphs = s->row->glyphs[s->area];
21033 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21034
21035 k = -1;
21036 x = 0;
21037 for (i = first; i < end; ++i)
21038 {
21039 int left, right;
21040 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21041 if (x - left < 0)
21042 k = i;
21043 x += glyphs[i].pixel_width;
21044 }
21045
21046 return k;
21047 }
21048
21049
21050 /* Set background width of glyph string S. START is the index of the
21051 first glyph following S. LAST_X is the right-most x-position + 1
21052 in the drawing area. */
21053
21054 static INLINE void
21055 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21056 {
21057 /* If the face of this glyph string has to be drawn to the end of
21058 the drawing area, set S->extends_to_end_of_line_p. */
21059
21060 if (start == s->row->used[s->area]
21061 && s->area == TEXT_AREA
21062 && ((s->row->fill_line_p
21063 && (s->hl == DRAW_NORMAL_TEXT
21064 || s->hl == DRAW_IMAGE_RAISED
21065 || s->hl == DRAW_IMAGE_SUNKEN))
21066 || s->hl == DRAW_MOUSE_FACE))
21067 s->extends_to_end_of_line_p = 1;
21068
21069 /* If S extends its face to the end of the line, set its
21070 background_width to the distance to the right edge of the drawing
21071 area. */
21072 if (s->extends_to_end_of_line_p)
21073 s->background_width = last_x - s->x + 1;
21074 else
21075 s->background_width = s->width;
21076 }
21077
21078
21079 /* Compute overhangs and x-positions for glyph string S and its
21080 predecessors, or successors. X is the starting x-position for S.
21081 BACKWARD_P non-zero means process predecessors. */
21082
21083 static void
21084 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21085 {
21086 if (backward_p)
21087 {
21088 while (s)
21089 {
21090 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21091 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21092 x -= s->width;
21093 s->x = x;
21094 s = s->prev;
21095 }
21096 }
21097 else
21098 {
21099 while (s)
21100 {
21101 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21102 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21103 s->x = x;
21104 x += s->width;
21105 s = s->next;
21106 }
21107 }
21108 }
21109
21110
21111
21112 /* The following macros are only called from draw_glyphs below.
21113 They reference the following parameters of that function directly:
21114 `w', `row', `area', and `overlap_p'
21115 as well as the following local variables:
21116 `s', `f', and `hdc' (in W32) */
21117
21118 #ifdef HAVE_NTGUI
21119 /* On W32, silently add local `hdc' variable to argument list of
21120 init_glyph_string. */
21121 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21122 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21123 #else
21124 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21125 init_glyph_string (s, char2b, w, row, area, start, hl)
21126 #endif
21127
21128 /* Add a glyph string for a stretch glyph to the list of strings
21129 between HEAD and TAIL. START is the index of the stretch glyph in
21130 row area AREA of glyph row ROW. END is the index of the last glyph
21131 in that glyph row area. X is the current output position assigned
21132 to the new glyph string constructed. HL overrides that face of the
21133 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21134 is the right-most x-position of the drawing area. */
21135
21136 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21137 and below -- keep them on one line. */
21138 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21139 do \
21140 { \
21141 s = (struct glyph_string *) alloca (sizeof *s); \
21142 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21143 START = fill_stretch_glyph_string (s, START, END); \
21144 append_glyph_string (&HEAD, &TAIL, s); \
21145 s->x = (X); \
21146 } \
21147 while (0)
21148
21149
21150 /* Add a glyph string for an image glyph to the list of strings
21151 between HEAD and TAIL. START is the index of the image glyph in
21152 row area AREA of glyph row ROW. END is the index of the last glyph
21153 in that glyph row area. X is the current output position assigned
21154 to the new glyph string constructed. HL overrides that face of the
21155 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21156 is the right-most x-position of the drawing area. */
21157
21158 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21159 do \
21160 { \
21161 s = (struct glyph_string *) alloca (sizeof *s); \
21162 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21163 fill_image_glyph_string (s); \
21164 append_glyph_string (&HEAD, &TAIL, s); \
21165 ++START; \
21166 s->x = (X); \
21167 } \
21168 while (0)
21169
21170
21171 /* Add a glyph string for a sequence of character glyphs to the list
21172 of strings between HEAD and TAIL. START is the index of the first
21173 glyph in row area AREA of glyph row ROW that is part of the new
21174 glyph string. END is the index of the last glyph in that glyph row
21175 area. X is the current output position assigned to the new glyph
21176 string constructed. HL overrides that face of the glyph; e.g. it
21177 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21178 right-most x-position of the drawing area. */
21179
21180 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21181 do \
21182 { \
21183 int face_id; \
21184 XChar2b *char2b; \
21185 \
21186 face_id = (row)->glyphs[area][START].face_id; \
21187 \
21188 s = (struct glyph_string *) alloca (sizeof *s); \
21189 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21190 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21191 append_glyph_string (&HEAD, &TAIL, s); \
21192 s->x = (X); \
21193 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21194 } \
21195 while (0)
21196
21197
21198 /* Add a glyph string for a composite sequence to the list of strings
21199 between HEAD and TAIL. START is the index of the first glyph in
21200 row area AREA of glyph row ROW that is part of the new glyph
21201 string. END is the index of the last glyph in that glyph row area.
21202 X is the current output position assigned to the new glyph string
21203 constructed. HL overrides that face of the glyph; e.g. it is
21204 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21205 x-position of the drawing area. */
21206
21207 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21208 do { \
21209 int face_id = (row)->glyphs[area][START].face_id; \
21210 struct face *base_face = FACE_FROM_ID (f, face_id); \
21211 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21212 struct composition *cmp = composition_table[cmp_id]; \
21213 XChar2b *char2b; \
21214 struct glyph_string *first_s IF_LINT (= NULL); \
21215 int n; \
21216 \
21217 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21218 \
21219 /* Make glyph_strings for each glyph sequence that is drawable by \
21220 the same face, and append them to HEAD/TAIL. */ \
21221 for (n = 0; n < cmp->glyph_len;) \
21222 { \
21223 s = (struct glyph_string *) alloca (sizeof *s); \
21224 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21225 append_glyph_string (&(HEAD), &(TAIL), s); \
21226 s->cmp = cmp; \
21227 s->cmp_from = n; \
21228 s->x = (X); \
21229 if (n == 0) \
21230 first_s = s; \
21231 n = fill_composite_glyph_string (s, base_face, overlaps); \
21232 } \
21233 \
21234 ++START; \
21235 s = first_s; \
21236 } while (0)
21237
21238
21239 /* Add a glyph string for a glyph-string sequence to the list of strings
21240 between HEAD and TAIL. */
21241
21242 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21243 do { \
21244 int face_id; \
21245 XChar2b *char2b; \
21246 Lisp_Object gstring; \
21247 \
21248 face_id = (row)->glyphs[area][START].face_id; \
21249 gstring = (composition_gstring_from_id \
21250 ((row)->glyphs[area][START].u.cmp.id)); \
21251 s = (struct glyph_string *) alloca (sizeof *s); \
21252 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21253 * LGSTRING_GLYPH_LEN (gstring)); \
21254 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21255 append_glyph_string (&(HEAD), &(TAIL), s); \
21256 s->x = (X); \
21257 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21258 } while (0)
21259
21260
21261 /* Add a glyph string for a sequence of glyphless character's glyphs
21262 to the list of strings between HEAD and TAIL. The meanings of
21263 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21264
21265 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21266 do \
21267 { \
21268 int face_id; \
21269 \
21270 face_id = (row)->glyphs[area][START].face_id; \
21271 \
21272 s = (struct glyph_string *) alloca (sizeof *s); \
21273 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21274 append_glyph_string (&HEAD, &TAIL, s); \
21275 s->x = (X); \
21276 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21277 overlaps); \
21278 } \
21279 while (0)
21280
21281
21282 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21283 of AREA of glyph row ROW on window W between indices START and END.
21284 HL overrides the face for drawing glyph strings, e.g. it is
21285 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21286 x-positions of the drawing area.
21287
21288 This is an ugly monster macro construct because we must use alloca
21289 to allocate glyph strings (because draw_glyphs can be called
21290 asynchronously). */
21291
21292 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21293 do \
21294 { \
21295 HEAD = TAIL = NULL; \
21296 while (START < END) \
21297 { \
21298 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21299 switch (first_glyph->type) \
21300 { \
21301 case CHAR_GLYPH: \
21302 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21303 HL, X, LAST_X); \
21304 break; \
21305 \
21306 case COMPOSITE_GLYPH: \
21307 if (first_glyph->u.cmp.automatic) \
21308 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21309 HL, X, LAST_X); \
21310 else \
21311 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21312 HL, X, LAST_X); \
21313 break; \
21314 \
21315 case STRETCH_GLYPH: \
21316 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21317 HL, X, LAST_X); \
21318 break; \
21319 \
21320 case IMAGE_GLYPH: \
21321 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21322 HL, X, LAST_X); \
21323 break; \
21324 \
21325 case GLYPHLESS_GLYPH: \
21326 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21327 HL, X, LAST_X); \
21328 break; \
21329 \
21330 default: \
21331 abort (); \
21332 } \
21333 \
21334 if (s) \
21335 { \
21336 set_glyph_string_background_width (s, START, LAST_X); \
21337 (X) += s->width; \
21338 } \
21339 } \
21340 } while (0)
21341
21342
21343 /* Draw glyphs between START and END in AREA of ROW on window W,
21344 starting at x-position X. X is relative to AREA in W. HL is a
21345 face-override with the following meaning:
21346
21347 DRAW_NORMAL_TEXT draw normally
21348 DRAW_CURSOR draw in cursor face
21349 DRAW_MOUSE_FACE draw in mouse face.
21350 DRAW_INVERSE_VIDEO draw in mode line face
21351 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21352 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21353
21354 If OVERLAPS is non-zero, draw only the foreground of characters and
21355 clip to the physical height of ROW. Non-zero value also defines
21356 the overlapping part to be drawn:
21357
21358 OVERLAPS_PRED overlap with preceding rows
21359 OVERLAPS_SUCC overlap with succeeding rows
21360 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21361 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21362
21363 Value is the x-position reached, relative to AREA of W. */
21364
21365 static int
21366 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21367 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21368 enum draw_glyphs_face hl, int overlaps)
21369 {
21370 struct glyph_string *head, *tail;
21371 struct glyph_string *s;
21372 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21373 int i, j, x_reached, last_x, area_left = 0;
21374 struct frame *f = XFRAME (WINDOW_FRAME (w));
21375 DECLARE_HDC (hdc);
21376
21377 ALLOCATE_HDC (hdc, f);
21378
21379 /* Let's rather be paranoid than getting a SEGV. */
21380 end = min (end, row->used[area]);
21381 start = max (0, start);
21382 start = min (end, start);
21383
21384 /* Translate X to frame coordinates. Set last_x to the right
21385 end of the drawing area. */
21386 if (row->full_width_p)
21387 {
21388 /* X is relative to the left edge of W, without scroll bars
21389 or fringes. */
21390 area_left = WINDOW_LEFT_EDGE_X (w);
21391 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21392 }
21393 else
21394 {
21395 area_left = window_box_left (w, area);
21396 last_x = area_left + window_box_width (w, area);
21397 }
21398 x += area_left;
21399
21400 /* Build a doubly-linked list of glyph_string structures between
21401 head and tail from what we have to draw. Note that the macro
21402 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21403 the reason we use a separate variable `i'. */
21404 i = start;
21405 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21406 if (tail)
21407 x_reached = tail->x + tail->background_width;
21408 else
21409 x_reached = x;
21410
21411 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21412 the row, redraw some glyphs in front or following the glyph
21413 strings built above. */
21414 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21415 {
21416 struct glyph_string *h, *t;
21417 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21418 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
21419 int check_mouse_face = 0;
21420 int dummy_x = 0;
21421
21422 /* If mouse highlighting is on, we may need to draw adjacent
21423 glyphs using mouse-face highlighting. */
21424 if (area == TEXT_AREA && row->mouse_face_p)
21425 {
21426 struct glyph_row *mouse_beg_row, *mouse_end_row;
21427
21428 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21429 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21430
21431 if (row >= mouse_beg_row && row <= mouse_end_row)
21432 {
21433 check_mouse_face = 1;
21434 mouse_beg_col = (row == mouse_beg_row)
21435 ? hlinfo->mouse_face_beg_col : 0;
21436 mouse_end_col = (row == mouse_end_row)
21437 ? hlinfo->mouse_face_end_col
21438 : row->used[TEXT_AREA];
21439 }
21440 }
21441
21442 /* Compute overhangs for all glyph strings. */
21443 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21444 for (s = head; s; s = s->next)
21445 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21446
21447 /* Prepend glyph strings for glyphs in front of the first glyph
21448 string that are overwritten because of the first glyph
21449 string's left overhang. The background of all strings
21450 prepended must be drawn because the first glyph string
21451 draws over it. */
21452 i = left_overwritten (head);
21453 if (i >= 0)
21454 {
21455 enum draw_glyphs_face overlap_hl;
21456
21457 /* If this row contains mouse highlighting, attempt to draw
21458 the overlapped glyphs with the correct highlight. This
21459 code fails if the overlap encompasses more than one glyph
21460 and mouse-highlight spans only some of these glyphs.
21461 However, making it work perfectly involves a lot more
21462 code, and I don't know if the pathological case occurs in
21463 practice, so we'll stick to this for now. --- cyd */
21464 if (check_mouse_face
21465 && mouse_beg_col < start && mouse_end_col > i)
21466 overlap_hl = DRAW_MOUSE_FACE;
21467 else
21468 overlap_hl = DRAW_NORMAL_TEXT;
21469
21470 j = i;
21471 BUILD_GLYPH_STRINGS (j, start, h, t,
21472 overlap_hl, dummy_x, last_x);
21473 start = i;
21474 compute_overhangs_and_x (t, head->x, 1);
21475 prepend_glyph_string_lists (&head, &tail, h, t);
21476 clip_head = head;
21477 }
21478
21479 /* Prepend glyph strings for glyphs in front of the first glyph
21480 string that overwrite that glyph string because of their
21481 right overhang. For these strings, only the foreground must
21482 be drawn, because it draws over the glyph string at `head'.
21483 The background must not be drawn because this would overwrite
21484 right overhangs of preceding glyphs for which no glyph
21485 strings exist. */
21486 i = left_overwriting (head);
21487 if (i >= 0)
21488 {
21489 enum draw_glyphs_face overlap_hl;
21490
21491 if (check_mouse_face
21492 && mouse_beg_col < start && mouse_end_col > i)
21493 overlap_hl = DRAW_MOUSE_FACE;
21494 else
21495 overlap_hl = DRAW_NORMAL_TEXT;
21496
21497 clip_head = head;
21498 BUILD_GLYPH_STRINGS (i, start, h, t,
21499 overlap_hl, dummy_x, last_x);
21500 for (s = h; s; s = s->next)
21501 s->background_filled_p = 1;
21502 compute_overhangs_and_x (t, head->x, 1);
21503 prepend_glyph_string_lists (&head, &tail, h, t);
21504 }
21505
21506 /* Append glyphs strings for glyphs following the last glyph
21507 string tail that are overwritten by tail. The background of
21508 these strings has to be drawn because tail's foreground draws
21509 over it. */
21510 i = right_overwritten (tail);
21511 if (i >= 0)
21512 {
21513 enum draw_glyphs_face overlap_hl;
21514
21515 if (check_mouse_face
21516 && mouse_beg_col < i && mouse_end_col > end)
21517 overlap_hl = DRAW_MOUSE_FACE;
21518 else
21519 overlap_hl = DRAW_NORMAL_TEXT;
21520
21521 BUILD_GLYPH_STRINGS (end, i, h, t,
21522 overlap_hl, x, last_x);
21523 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21524 we don't have `end = i;' here. */
21525 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21526 append_glyph_string_lists (&head, &tail, h, t);
21527 clip_tail = tail;
21528 }
21529
21530 /* Append glyph strings for glyphs following the last glyph
21531 string tail that overwrite tail. The foreground of such
21532 glyphs has to be drawn because it writes into the background
21533 of tail. The background must not be drawn because it could
21534 paint over the foreground of following glyphs. */
21535 i = right_overwriting (tail);
21536 if (i >= 0)
21537 {
21538 enum draw_glyphs_face overlap_hl;
21539 if (check_mouse_face
21540 && mouse_beg_col < i && mouse_end_col > end)
21541 overlap_hl = DRAW_MOUSE_FACE;
21542 else
21543 overlap_hl = DRAW_NORMAL_TEXT;
21544
21545 clip_tail = tail;
21546 i++; /* We must include the Ith glyph. */
21547 BUILD_GLYPH_STRINGS (end, i, h, t,
21548 overlap_hl, x, last_x);
21549 for (s = h; s; s = s->next)
21550 s->background_filled_p = 1;
21551 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21552 append_glyph_string_lists (&head, &tail, h, t);
21553 }
21554 if (clip_head || clip_tail)
21555 for (s = head; s; s = s->next)
21556 {
21557 s->clip_head = clip_head;
21558 s->clip_tail = clip_tail;
21559 }
21560 }
21561
21562 /* Draw all strings. */
21563 for (s = head; s; s = s->next)
21564 FRAME_RIF (f)->draw_glyph_string (s);
21565
21566 #ifndef HAVE_NS
21567 /* When focus a sole frame and move horizontally, this sets on_p to 0
21568 causing a failure to erase prev cursor position. */
21569 if (area == TEXT_AREA
21570 && !row->full_width_p
21571 /* When drawing overlapping rows, only the glyph strings'
21572 foreground is drawn, which doesn't erase a cursor
21573 completely. */
21574 && !overlaps)
21575 {
21576 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21577 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21578 : (tail ? tail->x + tail->background_width : x));
21579 x0 -= area_left;
21580 x1 -= area_left;
21581
21582 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21583 row->y, MATRIX_ROW_BOTTOM_Y (row));
21584 }
21585 #endif
21586
21587 /* Value is the x-position up to which drawn, relative to AREA of W.
21588 This doesn't include parts drawn because of overhangs. */
21589 if (row->full_width_p)
21590 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21591 else
21592 x_reached -= area_left;
21593
21594 RELEASE_HDC (hdc, f);
21595
21596 return x_reached;
21597 }
21598
21599 /* Expand row matrix if too narrow. Don't expand if area
21600 is not present. */
21601
21602 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21603 { \
21604 if (!fonts_changed_p \
21605 && (it->glyph_row->glyphs[area] \
21606 < it->glyph_row->glyphs[area + 1])) \
21607 { \
21608 it->w->ncols_scale_factor++; \
21609 fonts_changed_p = 1; \
21610 } \
21611 }
21612
21613 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21614 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21615
21616 static INLINE void
21617 append_glyph (struct it *it)
21618 {
21619 struct glyph *glyph;
21620 enum glyph_row_area area = it->area;
21621
21622 xassert (it->glyph_row);
21623 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21624
21625 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21626 if (glyph < it->glyph_row->glyphs[area + 1])
21627 {
21628 /* If the glyph row is reversed, we need to prepend the glyph
21629 rather than append it. */
21630 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21631 {
21632 struct glyph *g;
21633
21634 /* Make room for the additional glyph. */
21635 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21636 g[1] = *g;
21637 glyph = it->glyph_row->glyphs[area];
21638 }
21639 glyph->charpos = CHARPOS (it->position);
21640 glyph->object = it->object;
21641 if (it->pixel_width > 0)
21642 {
21643 glyph->pixel_width = it->pixel_width;
21644 glyph->padding_p = 0;
21645 }
21646 else
21647 {
21648 /* Assure at least 1-pixel width. Otherwise, cursor can't
21649 be displayed correctly. */
21650 glyph->pixel_width = 1;
21651 glyph->padding_p = 1;
21652 }
21653 glyph->ascent = it->ascent;
21654 glyph->descent = it->descent;
21655 glyph->voffset = it->voffset;
21656 glyph->type = CHAR_GLYPH;
21657 glyph->avoid_cursor_p = it->avoid_cursor_p;
21658 glyph->multibyte_p = it->multibyte_p;
21659 glyph->left_box_line_p = it->start_of_box_run_p;
21660 glyph->right_box_line_p = it->end_of_box_run_p;
21661 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21662 || it->phys_descent > it->descent);
21663 glyph->glyph_not_available_p = it->glyph_not_available_p;
21664 glyph->face_id = it->face_id;
21665 glyph->u.ch = it->char_to_display;
21666 glyph->slice.img = null_glyph_slice;
21667 glyph->font_type = FONT_TYPE_UNKNOWN;
21668 if (it->bidi_p)
21669 {
21670 glyph->resolved_level = it->bidi_it.resolved_level;
21671 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21672 abort ();
21673 glyph->bidi_type = it->bidi_it.type;
21674 }
21675 else
21676 {
21677 glyph->resolved_level = 0;
21678 glyph->bidi_type = UNKNOWN_BT;
21679 }
21680 ++it->glyph_row->used[area];
21681 }
21682 else
21683 IT_EXPAND_MATRIX_WIDTH (it, area);
21684 }
21685
21686 /* Store one glyph for the composition IT->cmp_it.id in
21687 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21688 non-null. */
21689
21690 static INLINE void
21691 append_composite_glyph (struct it *it)
21692 {
21693 struct glyph *glyph;
21694 enum glyph_row_area area = it->area;
21695
21696 xassert (it->glyph_row);
21697
21698 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21699 if (glyph < it->glyph_row->glyphs[area + 1])
21700 {
21701 /* If the glyph row is reversed, we need to prepend the glyph
21702 rather than append it. */
21703 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21704 {
21705 struct glyph *g;
21706
21707 /* Make room for the new glyph. */
21708 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21709 g[1] = *g;
21710 glyph = it->glyph_row->glyphs[it->area];
21711 }
21712 glyph->charpos = it->cmp_it.charpos;
21713 glyph->object = it->object;
21714 glyph->pixel_width = it->pixel_width;
21715 glyph->ascent = it->ascent;
21716 glyph->descent = it->descent;
21717 glyph->voffset = it->voffset;
21718 glyph->type = COMPOSITE_GLYPH;
21719 if (it->cmp_it.ch < 0)
21720 {
21721 glyph->u.cmp.automatic = 0;
21722 glyph->u.cmp.id = it->cmp_it.id;
21723 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21724 }
21725 else
21726 {
21727 glyph->u.cmp.automatic = 1;
21728 glyph->u.cmp.id = it->cmp_it.id;
21729 glyph->slice.cmp.from = it->cmp_it.from;
21730 glyph->slice.cmp.to = it->cmp_it.to - 1;
21731 }
21732 glyph->avoid_cursor_p = it->avoid_cursor_p;
21733 glyph->multibyte_p = it->multibyte_p;
21734 glyph->left_box_line_p = it->start_of_box_run_p;
21735 glyph->right_box_line_p = it->end_of_box_run_p;
21736 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21737 || it->phys_descent > it->descent);
21738 glyph->padding_p = 0;
21739 glyph->glyph_not_available_p = 0;
21740 glyph->face_id = it->face_id;
21741 glyph->font_type = FONT_TYPE_UNKNOWN;
21742 if (it->bidi_p)
21743 {
21744 glyph->resolved_level = it->bidi_it.resolved_level;
21745 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21746 abort ();
21747 glyph->bidi_type = it->bidi_it.type;
21748 }
21749 ++it->glyph_row->used[area];
21750 }
21751 else
21752 IT_EXPAND_MATRIX_WIDTH (it, area);
21753 }
21754
21755
21756 /* Change IT->ascent and IT->height according to the setting of
21757 IT->voffset. */
21758
21759 static INLINE void
21760 take_vertical_position_into_account (struct it *it)
21761 {
21762 if (it->voffset)
21763 {
21764 if (it->voffset < 0)
21765 /* Increase the ascent so that we can display the text higher
21766 in the line. */
21767 it->ascent -= it->voffset;
21768 else
21769 /* Increase the descent so that we can display the text lower
21770 in the line. */
21771 it->descent += it->voffset;
21772 }
21773 }
21774
21775
21776 /* Produce glyphs/get display metrics for the image IT is loaded with.
21777 See the description of struct display_iterator in dispextern.h for
21778 an overview of struct display_iterator. */
21779
21780 static void
21781 produce_image_glyph (struct it *it)
21782 {
21783 struct image *img;
21784 struct face *face;
21785 int glyph_ascent, crop;
21786 struct glyph_slice slice;
21787
21788 xassert (it->what == IT_IMAGE);
21789
21790 face = FACE_FROM_ID (it->f, it->face_id);
21791 xassert (face);
21792 /* Make sure X resources of the face is loaded. */
21793 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21794
21795 if (it->image_id < 0)
21796 {
21797 /* Fringe bitmap. */
21798 it->ascent = it->phys_ascent = 0;
21799 it->descent = it->phys_descent = 0;
21800 it->pixel_width = 0;
21801 it->nglyphs = 0;
21802 return;
21803 }
21804
21805 img = IMAGE_FROM_ID (it->f, it->image_id);
21806 xassert (img);
21807 /* Make sure X resources of the image is loaded. */
21808 prepare_image_for_display (it->f, img);
21809
21810 slice.x = slice.y = 0;
21811 slice.width = img->width;
21812 slice.height = img->height;
21813
21814 if (INTEGERP (it->slice.x))
21815 slice.x = XINT (it->slice.x);
21816 else if (FLOATP (it->slice.x))
21817 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21818
21819 if (INTEGERP (it->slice.y))
21820 slice.y = XINT (it->slice.y);
21821 else if (FLOATP (it->slice.y))
21822 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21823
21824 if (INTEGERP (it->slice.width))
21825 slice.width = XINT (it->slice.width);
21826 else if (FLOATP (it->slice.width))
21827 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21828
21829 if (INTEGERP (it->slice.height))
21830 slice.height = XINT (it->slice.height);
21831 else if (FLOATP (it->slice.height))
21832 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21833
21834 if (slice.x >= img->width)
21835 slice.x = img->width;
21836 if (slice.y >= img->height)
21837 slice.y = img->height;
21838 if (slice.x + slice.width >= img->width)
21839 slice.width = img->width - slice.x;
21840 if (slice.y + slice.height > img->height)
21841 slice.height = img->height - slice.y;
21842
21843 if (slice.width == 0 || slice.height == 0)
21844 return;
21845
21846 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21847
21848 it->descent = slice.height - glyph_ascent;
21849 if (slice.y == 0)
21850 it->descent += img->vmargin;
21851 if (slice.y + slice.height == img->height)
21852 it->descent += img->vmargin;
21853 it->phys_descent = it->descent;
21854
21855 it->pixel_width = slice.width;
21856 if (slice.x == 0)
21857 it->pixel_width += img->hmargin;
21858 if (slice.x + slice.width == img->width)
21859 it->pixel_width += img->hmargin;
21860
21861 /* It's quite possible for images to have an ascent greater than
21862 their height, so don't get confused in that case. */
21863 if (it->descent < 0)
21864 it->descent = 0;
21865
21866 it->nglyphs = 1;
21867
21868 if (face->box != FACE_NO_BOX)
21869 {
21870 if (face->box_line_width > 0)
21871 {
21872 if (slice.y == 0)
21873 it->ascent += face->box_line_width;
21874 if (slice.y + slice.height == img->height)
21875 it->descent += face->box_line_width;
21876 }
21877
21878 if (it->start_of_box_run_p && slice.x == 0)
21879 it->pixel_width += eabs (face->box_line_width);
21880 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21881 it->pixel_width += eabs (face->box_line_width);
21882 }
21883
21884 take_vertical_position_into_account (it);
21885
21886 /* Automatically crop wide image glyphs at right edge so we can
21887 draw the cursor on same display row. */
21888 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21889 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21890 {
21891 it->pixel_width -= crop;
21892 slice.width -= crop;
21893 }
21894
21895 if (it->glyph_row)
21896 {
21897 struct glyph *glyph;
21898 enum glyph_row_area area = it->area;
21899
21900 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21901 if (glyph < it->glyph_row->glyphs[area + 1])
21902 {
21903 glyph->charpos = CHARPOS (it->position);
21904 glyph->object = it->object;
21905 glyph->pixel_width = it->pixel_width;
21906 glyph->ascent = glyph_ascent;
21907 glyph->descent = it->descent;
21908 glyph->voffset = it->voffset;
21909 glyph->type = IMAGE_GLYPH;
21910 glyph->avoid_cursor_p = it->avoid_cursor_p;
21911 glyph->multibyte_p = it->multibyte_p;
21912 glyph->left_box_line_p = it->start_of_box_run_p;
21913 glyph->right_box_line_p = it->end_of_box_run_p;
21914 glyph->overlaps_vertically_p = 0;
21915 glyph->padding_p = 0;
21916 glyph->glyph_not_available_p = 0;
21917 glyph->face_id = it->face_id;
21918 glyph->u.img_id = img->id;
21919 glyph->slice.img = slice;
21920 glyph->font_type = FONT_TYPE_UNKNOWN;
21921 if (it->bidi_p)
21922 {
21923 glyph->resolved_level = it->bidi_it.resolved_level;
21924 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21925 abort ();
21926 glyph->bidi_type = it->bidi_it.type;
21927 }
21928 ++it->glyph_row->used[area];
21929 }
21930 else
21931 IT_EXPAND_MATRIX_WIDTH (it, area);
21932 }
21933 }
21934
21935
21936 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21937 of the glyph, WIDTH and HEIGHT are the width and height of the
21938 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21939
21940 static void
21941 append_stretch_glyph (struct it *it, Lisp_Object object,
21942 int width, int height, int ascent)
21943 {
21944 struct glyph *glyph;
21945 enum glyph_row_area area = it->area;
21946
21947 xassert (ascent >= 0 && ascent <= height);
21948
21949 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21950 if (glyph < it->glyph_row->glyphs[area + 1])
21951 {
21952 /* If the glyph row is reversed, we need to prepend the glyph
21953 rather than append it. */
21954 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21955 {
21956 struct glyph *g;
21957
21958 /* Make room for the additional glyph. */
21959 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21960 g[1] = *g;
21961 glyph = it->glyph_row->glyphs[area];
21962 }
21963 glyph->charpos = CHARPOS (it->position);
21964 glyph->object = object;
21965 glyph->pixel_width = width;
21966 glyph->ascent = ascent;
21967 glyph->descent = height - ascent;
21968 glyph->voffset = it->voffset;
21969 glyph->type = STRETCH_GLYPH;
21970 glyph->avoid_cursor_p = it->avoid_cursor_p;
21971 glyph->multibyte_p = it->multibyte_p;
21972 glyph->left_box_line_p = it->start_of_box_run_p;
21973 glyph->right_box_line_p = it->end_of_box_run_p;
21974 glyph->overlaps_vertically_p = 0;
21975 glyph->padding_p = 0;
21976 glyph->glyph_not_available_p = 0;
21977 glyph->face_id = it->face_id;
21978 glyph->u.stretch.ascent = ascent;
21979 glyph->u.stretch.height = height;
21980 glyph->slice.img = null_glyph_slice;
21981 glyph->font_type = FONT_TYPE_UNKNOWN;
21982 if (it->bidi_p)
21983 {
21984 glyph->resolved_level = it->bidi_it.resolved_level;
21985 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21986 abort ();
21987 glyph->bidi_type = it->bidi_it.type;
21988 }
21989 else
21990 {
21991 glyph->resolved_level = 0;
21992 glyph->bidi_type = UNKNOWN_BT;
21993 }
21994 ++it->glyph_row->used[area];
21995 }
21996 else
21997 IT_EXPAND_MATRIX_WIDTH (it, area);
21998 }
21999
22000
22001 /* Produce a stretch glyph for iterator IT. IT->object is the value
22002 of the glyph property displayed. The value must be a list
22003 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22004 being recognized:
22005
22006 1. `:width WIDTH' specifies that the space should be WIDTH *
22007 canonical char width wide. WIDTH may be an integer or floating
22008 point number.
22009
22010 2. `:relative-width FACTOR' specifies that the width of the stretch
22011 should be computed from the width of the first character having the
22012 `glyph' property, and should be FACTOR times that width.
22013
22014 3. `:align-to HPOS' specifies that the space should be wide enough
22015 to reach HPOS, a value in canonical character units.
22016
22017 Exactly one of the above pairs must be present.
22018
22019 4. `:height HEIGHT' specifies that the height of the stretch produced
22020 should be HEIGHT, measured in canonical character units.
22021
22022 5. `:relative-height FACTOR' specifies that the height of the
22023 stretch should be FACTOR times the height of the characters having
22024 the glyph property.
22025
22026 Either none or exactly one of 4 or 5 must be present.
22027
22028 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22029 of the stretch should be used for the ascent of the stretch.
22030 ASCENT must be in the range 0 <= ASCENT <= 100. */
22031
22032 static void
22033 produce_stretch_glyph (struct it *it)
22034 {
22035 /* (space :width WIDTH :height HEIGHT ...) */
22036 Lisp_Object prop, plist;
22037 int width = 0, height = 0, align_to = -1;
22038 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22039 int ascent = 0;
22040 double tem;
22041 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22042 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22043
22044 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22045
22046 /* List should start with `space'. */
22047 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22048 plist = XCDR (it->object);
22049
22050 /* Compute the width of the stretch. */
22051 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22052 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22053 {
22054 /* Absolute width `:width WIDTH' specified and valid. */
22055 zero_width_ok_p = 1;
22056 width = (int)tem;
22057 }
22058 else if (prop = Fplist_get (plist, QCrelative_width),
22059 NUMVAL (prop) > 0)
22060 {
22061 /* Relative width `:relative-width FACTOR' specified and valid.
22062 Compute the width of the characters having the `glyph'
22063 property. */
22064 struct it it2;
22065 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22066
22067 it2 = *it;
22068 if (it->multibyte_p)
22069 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22070 else
22071 {
22072 it2.c = it2.char_to_display = *p, it2.len = 1;
22073 if (! ASCII_CHAR_P (it2.c))
22074 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22075 }
22076
22077 it2.glyph_row = NULL;
22078 it2.what = IT_CHARACTER;
22079 x_produce_glyphs (&it2);
22080 width = NUMVAL (prop) * it2.pixel_width;
22081 }
22082 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22083 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22084 {
22085 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22086 align_to = (align_to < 0
22087 ? 0
22088 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22089 else if (align_to < 0)
22090 align_to = window_box_left_offset (it->w, TEXT_AREA);
22091 width = max (0, (int)tem + align_to - it->current_x);
22092 zero_width_ok_p = 1;
22093 }
22094 else
22095 /* Nothing specified -> width defaults to canonical char width. */
22096 width = FRAME_COLUMN_WIDTH (it->f);
22097
22098 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22099 width = 1;
22100
22101 /* Compute height. */
22102 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22103 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22104 {
22105 height = (int)tem;
22106 zero_height_ok_p = 1;
22107 }
22108 else if (prop = Fplist_get (plist, QCrelative_height),
22109 NUMVAL (prop) > 0)
22110 height = FONT_HEIGHT (font) * NUMVAL (prop);
22111 else
22112 height = FONT_HEIGHT (font);
22113
22114 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22115 height = 1;
22116
22117 /* Compute percentage of height used for ascent. If
22118 `:ascent ASCENT' is present and valid, use that. Otherwise,
22119 derive the ascent from the font in use. */
22120 if (prop = Fplist_get (plist, QCascent),
22121 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22122 ascent = height * NUMVAL (prop) / 100.0;
22123 else if (!NILP (prop)
22124 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22125 ascent = min (max (0, (int)tem), height);
22126 else
22127 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22128
22129 if (width > 0 && it->line_wrap != TRUNCATE
22130 && it->current_x + width > it->last_visible_x)
22131 width = it->last_visible_x - it->current_x - 1;
22132
22133 if (width > 0 && height > 0 && it->glyph_row)
22134 {
22135 Lisp_Object object = it->stack[it->sp - 1].string;
22136 if (!STRINGP (object))
22137 object = it->w->buffer;
22138 append_stretch_glyph (it, object, width, height, ascent);
22139 }
22140
22141 it->pixel_width = width;
22142 it->ascent = it->phys_ascent = ascent;
22143 it->descent = it->phys_descent = height - it->ascent;
22144 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22145
22146 take_vertical_position_into_account (it);
22147 }
22148
22149 /* Calculate line-height and line-spacing properties.
22150 An integer value specifies explicit pixel value.
22151 A float value specifies relative value to current face height.
22152 A cons (float . face-name) specifies relative value to
22153 height of specified face font.
22154
22155 Returns height in pixels, or nil. */
22156
22157
22158 static Lisp_Object
22159 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22160 int boff, int override)
22161 {
22162 Lisp_Object face_name = Qnil;
22163 int ascent, descent, height;
22164
22165 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22166 return val;
22167
22168 if (CONSP (val))
22169 {
22170 face_name = XCAR (val);
22171 val = XCDR (val);
22172 if (!NUMBERP (val))
22173 val = make_number (1);
22174 if (NILP (face_name))
22175 {
22176 height = it->ascent + it->descent;
22177 goto scale;
22178 }
22179 }
22180
22181 if (NILP (face_name))
22182 {
22183 font = FRAME_FONT (it->f);
22184 boff = FRAME_BASELINE_OFFSET (it->f);
22185 }
22186 else if (EQ (face_name, Qt))
22187 {
22188 override = 0;
22189 }
22190 else
22191 {
22192 int face_id;
22193 struct face *face;
22194
22195 face_id = lookup_named_face (it->f, face_name, 0);
22196 if (face_id < 0)
22197 return make_number (-1);
22198
22199 face = FACE_FROM_ID (it->f, face_id);
22200 font = face->font;
22201 if (font == NULL)
22202 return make_number (-1);
22203 boff = font->baseline_offset;
22204 if (font->vertical_centering)
22205 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22206 }
22207
22208 ascent = FONT_BASE (font) + boff;
22209 descent = FONT_DESCENT (font) - boff;
22210
22211 if (override)
22212 {
22213 it->override_ascent = ascent;
22214 it->override_descent = descent;
22215 it->override_boff = boff;
22216 }
22217
22218 height = ascent + descent;
22219
22220 scale:
22221 if (FLOATP (val))
22222 height = (int)(XFLOAT_DATA (val) * height);
22223 else if (INTEGERP (val))
22224 height *= XINT (val);
22225
22226 return make_number (height);
22227 }
22228
22229
22230 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22231 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22232 and only if this is for a character for which no font was found.
22233
22234 If the display method (it->glyphless_method) is
22235 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22236 length of the acronym or the hexadecimal string, UPPER_XOFF and
22237 UPPER_YOFF are pixel offsets for the upper part of the string,
22238 LOWER_XOFF and LOWER_YOFF are for the lower part.
22239
22240 For the other display methods, LEN through LOWER_YOFF are zero. */
22241
22242 static void
22243 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22244 short upper_xoff, short upper_yoff,
22245 short lower_xoff, short lower_yoff)
22246 {
22247 struct glyph *glyph;
22248 enum glyph_row_area area = it->area;
22249
22250 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22251 if (glyph < it->glyph_row->glyphs[area + 1])
22252 {
22253 /* If the glyph row is reversed, we need to prepend the glyph
22254 rather than append it. */
22255 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22256 {
22257 struct glyph *g;
22258
22259 /* Make room for the additional glyph. */
22260 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22261 g[1] = *g;
22262 glyph = it->glyph_row->glyphs[area];
22263 }
22264 glyph->charpos = CHARPOS (it->position);
22265 glyph->object = it->object;
22266 glyph->pixel_width = it->pixel_width;
22267 glyph->ascent = it->ascent;
22268 glyph->descent = it->descent;
22269 glyph->voffset = it->voffset;
22270 glyph->type = GLYPHLESS_GLYPH;
22271 glyph->u.glyphless.method = it->glyphless_method;
22272 glyph->u.glyphless.for_no_font = for_no_font;
22273 glyph->u.glyphless.len = len;
22274 glyph->u.glyphless.ch = it->c;
22275 glyph->slice.glyphless.upper_xoff = upper_xoff;
22276 glyph->slice.glyphless.upper_yoff = upper_yoff;
22277 glyph->slice.glyphless.lower_xoff = lower_xoff;
22278 glyph->slice.glyphless.lower_yoff = lower_yoff;
22279 glyph->avoid_cursor_p = it->avoid_cursor_p;
22280 glyph->multibyte_p = it->multibyte_p;
22281 glyph->left_box_line_p = it->start_of_box_run_p;
22282 glyph->right_box_line_p = it->end_of_box_run_p;
22283 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22284 || it->phys_descent > it->descent);
22285 glyph->padding_p = 0;
22286 glyph->glyph_not_available_p = 0;
22287 glyph->face_id = face_id;
22288 glyph->font_type = FONT_TYPE_UNKNOWN;
22289 if (it->bidi_p)
22290 {
22291 glyph->resolved_level = it->bidi_it.resolved_level;
22292 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22293 abort ();
22294 glyph->bidi_type = it->bidi_it.type;
22295 }
22296 ++it->glyph_row->used[area];
22297 }
22298 else
22299 IT_EXPAND_MATRIX_WIDTH (it, area);
22300 }
22301
22302
22303 /* Produce a glyph for a glyphless character for iterator IT.
22304 IT->glyphless_method specifies which method to use for displaying
22305 the character. See the description of enum
22306 glyphless_display_method in dispextern.h for the detail.
22307
22308 FOR_NO_FONT is nonzero if and only if this is for a character for
22309 which no font was found. ACRONYM, if non-nil, is an acronym string
22310 for the character. */
22311
22312 static void
22313 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22314 {
22315 int face_id;
22316 struct face *face;
22317 struct font *font;
22318 int base_width, base_height, width, height;
22319 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22320 int len;
22321
22322 /* Get the metrics of the base font. We always refer to the current
22323 ASCII face. */
22324 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22325 font = face->font ? face->font : FRAME_FONT (it->f);
22326 it->ascent = FONT_BASE (font) + font->baseline_offset;
22327 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22328 base_height = it->ascent + it->descent;
22329 base_width = font->average_width;
22330
22331 /* Get a face ID for the glyph by utilizing a cache (the same way as
22332 doen for `escape-glyph' in get_next_display_element). */
22333 if (it->f == last_glyphless_glyph_frame
22334 && it->face_id == last_glyphless_glyph_face_id)
22335 {
22336 face_id = last_glyphless_glyph_merged_face_id;
22337 }
22338 else
22339 {
22340 /* Merge the `glyphless-char' face into the current face. */
22341 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22342 last_glyphless_glyph_frame = it->f;
22343 last_glyphless_glyph_face_id = it->face_id;
22344 last_glyphless_glyph_merged_face_id = face_id;
22345 }
22346
22347 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22348 {
22349 it->pixel_width = THIN_SPACE_WIDTH;
22350 len = 0;
22351 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22352 }
22353 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22354 {
22355 width = CHAR_WIDTH (it->c);
22356 if (width == 0)
22357 width = 1;
22358 else if (width > 4)
22359 width = 4;
22360 it->pixel_width = base_width * width;
22361 len = 0;
22362 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22363 }
22364 else
22365 {
22366 char buf[7];
22367 const char *str;
22368 unsigned int code[6];
22369 int upper_len;
22370 int ascent, descent;
22371 struct font_metrics metrics_upper, metrics_lower;
22372
22373 face = FACE_FROM_ID (it->f, face_id);
22374 font = face->font ? face->font : FRAME_FONT (it->f);
22375 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22376
22377 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22378 {
22379 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22380 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22381 if (CONSP (acronym))
22382 acronym = XCAR (acronym);
22383 str = STRINGP (acronym) ? SSDATA (acronym) : "";
22384 }
22385 else
22386 {
22387 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22388 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22389 str = buf;
22390 }
22391 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22392 code[len] = font->driver->encode_char (font, str[len]);
22393 upper_len = (len + 1) / 2;
22394 font->driver->text_extents (font, code, upper_len,
22395 &metrics_upper);
22396 font->driver->text_extents (font, code + upper_len, len - upper_len,
22397 &metrics_lower);
22398
22399
22400
22401 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22402 width = max (metrics_upper.width, metrics_lower.width) + 4;
22403 upper_xoff = upper_yoff = 2; /* the typical case */
22404 if (base_width >= width)
22405 {
22406 /* Align the upper to the left, the lower to the right. */
22407 it->pixel_width = base_width;
22408 lower_xoff = base_width - 2 - metrics_lower.width;
22409 }
22410 else
22411 {
22412 /* Center the shorter one. */
22413 it->pixel_width = width;
22414 if (metrics_upper.width >= metrics_lower.width)
22415 lower_xoff = (width - metrics_lower.width) / 2;
22416 else
22417 {
22418 /* FIXME: This code doesn't look right. It formerly was
22419 missing the "lower_xoff = 0;", which couldn't have
22420 been right since it left lower_xoff uninitialized. */
22421 lower_xoff = 0;
22422 upper_xoff = (width - metrics_upper.width) / 2;
22423 }
22424 }
22425
22426 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22427 top, bottom, and between upper and lower strings. */
22428 height = (metrics_upper.ascent + metrics_upper.descent
22429 + metrics_lower.ascent + metrics_lower.descent) + 5;
22430 /* Center vertically.
22431 H:base_height, D:base_descent
22432 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22433
22434 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22435 descent = D - H/2 + h/2;
22436 lower_yoff = descent - 2 - ld;
22437 upper_yoff = lower_yoff - la - 1 - ud; */
22438 ascent = - (it->descent - (base_height + height + 1) / 2);
22439 descent = it->descent - (base_height - height) / 2;
22440 lower_yoff = descent - 2 - metrics_lower.descent;
22441 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22442 - metrics_upper.descent);
22443 /* Don't make the height shorter than the base height. */
22444 if (height > base_height)
22445 {
22446 it->ascent = ascent;
22447 it->descent = descent;
22448 }
22449 }
22450
22451 it->phys_ascent = it->ascent;
22452 it->phys_descent = it->descent;
22453 if (it->glyph_row)
22454 append_glyphless_glyph (it, face_id, for_no_font, len,
22455 upper_xoff, upper_yoff,
22456 lower_xoff, lower_yoff);
22457 it->nglyphs = 1;
22458 take_vertical_position_into_account (it);
22459 }
22460
22461
22462 /* RIF:
22463 Produce glyphs/get display metrics for the display element IT is
22464 loaded with. See the description of struct it in dispextern.h
22465 for an overview of struct it. */
22466
22467 void
22468 x_produce_glyphs (struct it *it)
22469 {
22470 int extra_line_spacing = it->extra_line_spacing;
22471
22472 it->glyph_not_available_p = 0;
22473
22474 if (it->what == IT_CHARACTER)
22475 {
22476 XChar2b char2b;
22477 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22478 struct font *font = face->font;
22479 struct font_metrics *pcm = NULL;
22480 int boff; /* baseline offset */
22481
22482 if (font == NULL)
22483 {
22484 /* When no suitable font is found, display this character by
22485 the method specified in the first extra slot of
22486 Vglyphless_char_display. */
22487 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22488
22489 xassert (it->what == IT_GLYPHLESS);
22490 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22491 goto done;
22492 }
22493
22494 boff = font->baseline_offset;
22495 if (font->vertical_centering)
22496 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22497
22498 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22499 {
22500 int stretched_p;
22501
22502 it->nglyphs = 1;
22503
22504 if (it->override_ascent >= 0)
22505 {
22506 it->ascent = it->override_ascent;
22507 it->descent = it->override_descent;
22508 boff = it->override_boff;
22509 }
22510 else
22511 {
22512 it->ascent = FONT_BASE (font) + boff;
22513 it->descent = FONT_DESCENT (font) - boff;
22514 }
22515
22516 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22517 {
22518 pcm = get_per_char_metric (font, &char2b);
22519 if (pcm->width == 0
22520 && pcm->rbearing == 0 && pcm->lbearing == 0)
22521 pcm = NULL;
22522 }
22523
22524 if (pcm)
22525 {
22526 it->phys_ascent = pcm->ascent + boff;
22527 it->phys_descent = pcm->descent - boff;
22528 it->pixel_width = pcm->width;
22529 }
22530 else
22531 {
22532 it->glyph_not_available_p = 1;
22533 it->phys_ascent = it->ascent;
22534 it->phys_descent = it->descent;
22535 it->pixel_width = font->space_width;
22536 }
22537
22538 if (it->constrain_row_ascent_descent_p)
22539 {
22540 if (it->descent > it->max_descent)
22541 {
22542 it->ascent += it->descent - it->max_descent;
22543 it->descent = it->max_descent;
22544 }
22545 if (it->ascent > it->max_ascent)
22546 {
22547 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22548 it->ascent = it->max_ascent;
22549 }
22550 it->phys_ascent = min (it->phys_ascent, it->ascent);
22551 it->phys_descent = min (it->phys_descent, it->descent);
22552 extra_line_spacing = 0;
22553 }
22554
22555 /* If this is a space inside a region of text with
22556 `space-width' property, change its width. */
22557 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22558 if (stretched_p)
22559 it->pixel_width *= XFLOATINT (it->space_width);
22560
22561 /* If face has a box, add the box thickness to the character
22562 height. If character has a box line to the left and/or
22563 right, add the box line width to the character's width. */
22564 if (face->box != FACE_NO_BOX)
22565 {
22566 int thick = face->box_line_width;
22567
22568 if (thick > 0)
22569 {
22570 it->ascent += thick;
22571 it->descent += thick;
22572 }
22573 else
22574 thick = -thick;
22575
22576 if (it->start_of_box_run_p)
22577 it->pixel_width += thick;
22578 if (it->end_of_box_run_p)
22579 it->pixel_width += thick;
22580 }
22581
22582 /* If face has an overline, add the height of the overline
22583 (1 pixel) and a 1 pixel margin to the character height. */
22584 if (face->overline_p)
22585 it->ascent += overline_margin;
22586
22587 if (it->constrain_row_ascent_descent_p)
22588 {
22589 if (it->ascent > it->max_ascent)
22590 it->ascent = it->max_ascent;
22591 if (it->descent > it->max_descent)
22592 it->descent = it->max_descent;
22593 }
22594
22595 take_vertical_position_into_account (it);
22596
22597 /* If we have to actually produce glyphs, do it. */
22598 if (it->glyph_row)
22599 {
22600 if (stretched_p)
22601 {
22602 /* Translate a space with a `space-width' property
22603 into a stretch glyph. */
22604 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22605 / FONT_HEIGHT (font));
22606 append_stretch_glyph (it, it->object, it->pixel_width,
22607 it->ascent + it->descent, ascent);
22608 }
22609 else
22610 append_glyph (it);
22611
22612 /* If characters with lbearing or rbearing are displayed
22613 in this line, record that fact in a flag of the
22614 glyph row. This is used to optimize X output code. */
22615 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22616 it->glyph_row->contains_overlapping_glyphs_p = 1;
22617 }
22618 if (! stretched_p && it->pixel_width == 0)
22619 /* We assure that all visible glyphs have at least 1-pixel
22620 width. */
22621 it->pixel_width = 1;
22622 }
22623 else if (it->char_to_display == '\n')
22624 {
22625 /* A newline has no width, but we need the height of the
22626 line. But if previous part of the line sets a height,
22627 don't increase that height */
22628
22629 Lisp_Object height;
22630 Lisp_Object total_height = Qnil;
22631
22632 it->override_ascent = -1;
22633 it->pixel_width = 0;
22634 it->nglyphs = 0;
22635
22636 height = get_it_property (it, Qline_height);
22637 /* Split (line-height total-height) list */
22638 if (CONSP (height)
22639 && CONSP (XCDR (height))
22640 && NILP (XCDR (XCDR (height))))
22641 {
22642 total_height = XCAR (XCDR (height));
22643 height = XCAR (height);
22644 }
22645 height = calc_line_height_property (it, height, font, boff, 1);
22646
22647 if (it->override_ascent >= 0)
22648 {
22649 it->ascent = it->override_ascent;
22650 it->descent = it->override_descent;
22651 boff = it->override_boff;
22652 }
22653 else
22654 {
22655 it->ascent = FONT_BASE (font) + boff;
22656 it->descent = FONT_DESCENT (font) - boff;
22657 }
22658
22659 if (EQ (height, Qt))
22660 {
22661 if (it->descent > it->max_descent)
22662 {
22663 it->ascent += it->descent - it->max_descent;
22664 it->descent = it->max_descent;
22665 }
22666 if (it->ascent > it->max_ascent)
22667 {
22668 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22669 it->ascent = it->max_ascent;
22670 }
22671 it->phys_ascent = min (it->phys_ascent, it->ascent);
22672 it->phys_descent = min (it->phys_descent, it->descent);
22673 it->constrain_row_ascent_descent_p = 1;
22674 extra_line_spacing = 0;
22675 }
22676 else
22677 {
22678 Lisp_Object spacing;
22679
22680 it->phys_ascent = it->ascent;
22681 it->phys_descent = it->descent;
22682
22683 if ((it->max_ascent > 0 || it->max_descent > 0)
22684 && face->box != FACE_NO_BOX
22685 && face->box_line_width > 0)
22686 {
22687 it->ascent += face->box_line_width;
22688 it->descent += face->box_line_width;
22689 }
22690 if (!NILP (height)
22691 && XINT (height) > it->ascent + it->descent)
22692 it->ascent = XINT (height) - it->descent;
22693
22694 if (!NILP (total_height))
22695 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22696 else
22697 {
22698 spacing = get_it_property (it, Qline_spacing);
22699 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22700 }
22701 if (INTEGERP (spacing))
22702 {
22703 extra_line_spacing = XINT (spacing);
22704 if (!NILP (total_height))
22705 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22706 }
22707 }
22708 }
22709 else /* i.e. (it->char_to_display == '\t') */
22710 {
22711 if (font->space_width > 0)
22712 {
22713 int tab_width = it->tab_width * font->space_width;
22714 int x = it->current_x + it->continuation_lines_width;
22715 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22716
22717 /* If the distance from the current position to the next tab
22718 stop is less than a space character width, use the
22719 tab stop after that. */
22720 if (next_tab_x - x < font->space_width)
22721 next_tab_x += tab_width;
22722
22723 it->pixel_width = next_tab_x - x;
22724 it->nglyphs = 1;
22725 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22726 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22727
22728 if (it->glyph_row)
22729 {
22730 append_stretch_glyph (it, it->object, it->pixel_width,
22731 it->ascent + it->descent, it->ascent);
22732 }
22733 }
22734 else
22735 {
22736 it->pixel_width = 0;
22737 it->nglyphs = 1;
22738 }
22739 }
22740 }
22741 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22742 {
22743 /* A static composition.
22744
22745 Note: A composition is represented as one glyph in the
22746 glyph matrix. There are no padding glyphs.
22747
22748 Important note: pixel_width, ascent, and descent are the
22749 values of what is drawn by draw_glyphs (i.e. the values of
22750 the overall glyphs composed). */
22751 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22752 int boff; /* baseline offset */
22753 struct composition *cmp = composition_table[it->cmp_it.id];
22754 int glyph_len = cmp->glyph_len;
22755 struct font *font = face->font;
22756
22757 it->nglyphs = 1;
22758
22759 /* If we have not yet calculated pixel size data of glyphs of
22760 the composition for the current face font, calculate them
22761 now. Theoretically, we have to check all fonts for the
22762 glyphs, but that requires much time and memory space. So,
22763 here we check only the font of the first glyph. This may
22764 lead to incorrect display, but it's very rare, and C-l
22765 (recenter-top-bottom) can correct the display anyway. */
22766 if (! cmp->font || cmp->font != font)
22767 {
22768 /* Ascent and descent of the font of the first character
22769 of this composition (adjusted by baseline offset).
22770 Ascent and descent of overall glyphs should not be less
22771 than these, respectively. */
22772 int font_ascent, font_descent, font_height;
22773 /* Bounding box of the overall glyphs. */
22774 int leftmost, rightmost, lowest, highest;
22775 int lbearing, rbearing;
22776 int i, width, ascent, descent;
22777 int left_padded = 0, right_padded = 0;
22778 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
22779 XChar2b char2b;
22780 struct font_metrics *pcm;
22781 int font_not_found_p;
22782 EMACS_INT pos;
22783
22784 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22785 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22786 break;
22787 if (glyph_len < cmp->glyph_len)
22788 right_padded = 1;
22789 for (i = 0; i < glyph_len; i++)
22790 {
22791 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22792 break;
22793 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22794 }
22795 if (i > 0)
22796 left_padded = 1;
22797
22798 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22799 : IT_CHARPOS (*it));
22800 /* If no suitable font is found, use the default font. */
22801 font_not_found_p = font == NULL;
22802 if (font_not_found_p)
22803 {
22804 face = face->ascii_face;
22805 font = face->font;
22806 }
22807 boff = font->baseline_offset;
22808 if (font->vertical_centering)
22809 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22810 font_ascent = FONT_BASE (font) + boff;
22811 font_descent = FONT_DESCENT (font) - boff;
22812 font_height = FONT_HEIGHT (font);
22813
22814 cmp->font = (void *) font;
22815
22816 pcm = NULL;
22817 if (! font_not_found_p)
22818 {
22819 get_char_face_and_encoding (it->f, c, it->face_id,
22820 &char2b, 0);
22821 pcm = get_per_char_metric (font, &char2b);
22822 }
22823
22824 /* Initialize the bounding box. */
22825 if (pcm)
22826 {
22827 width = pcm->width;
22828 ascent = pcm->ascent;
22829 descent = pcm->descent;
22830 lbearing = pcm->lbearing;
22831 rbearing = pcm->rbearing;
22832 }
22833 else
22834 {
22835 width = font->space_width;
22836 ascent = FONT_BASE (font);
22837 descent = FONT_DESCENT (font);
22838 lbearing = 0;
22839 rbearing = width;
22840 }
22841
22842 rightmost = width;
22843 leftmost = 0;
22844 lowest = - descent + boff;
22845 highest = ascent + boff;
22846
22847 if (! font_not_found_p
22848 && font->default_ascent
22849 && CHAR_TABLE_P (Vuse_default_ascent)
22850 && !NILP (Faref (Vuse_default_ascent,
22851 make_number (it->char_to_display))))
22852 highest = font->default_ascent + boff;
22853
22854 /* Draw the first glyph at the normal position. It may be
22855 shifted to right later if some other glyphs are drawn
22856 at the left. */
22857 cmp->offsets[i * 2] = 0;
22858 cmp->offsets[i * 2 + 1] = boff;
22859 cmp->lbearing = lbearing;
22860 cmp->rbearing = rbearing;
22861
22862 /* Set cmp->offsets for the remaining glyphs. */
22863 for (i++; i < glyph_len; i++)
22864 {
22865 int left, right, btm, top;
22866 int ch = COMPOSITION_GLYPH (cmp, i);
22867 int face_id;
22868 struct face *this_face;
22869
22870 if (ch == '\t')
22871 ch = ' ';
22872 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22873 this_face = FACE_FROM_ID (it->f, face_id);
22874 font = this_face->font;
22875
22876 if (font == NULL)
22877 pcm = NULL;
22878 else
22879 {
22880 get_char_face_and_encoding (it->f, ch, face_id,
22881 &char2b, 0);
22882 pcm = get_per_char_metric (font, &char2b);
22883 }
22884 if (! pcm)
22885 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22886 else
22887 {
22888 width = pcm->width;
22889 ascent = pcm->ascent;
22890 descent = pcm->descent;
22891 lbearing = pcm->lbearing;
22892 rbearing = pcm->rbearing;
22893 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22894 {
22895 /* Relative composition with or without
22896 alternate chars. */
22897 left = (leftmost + rightmost - width) / 2;
22898 btm = - descent + boff;
22899 if (font->relative_compose
22900 && (! CHAR_TABLE_P (Vignore_relative_composition)
22901 || NILP (Faref (Vignore_relative_composition,
22902 make_number (ch)))))
22903 {
22904
22905 if (- descent >= font->relative_compose)
22906 /* One extra pixel between two glyphs. */
22907 btm = highest + 1;
22908 else if (ascent <= 0)
22909 /* One extra pixel between two glyphs. */
22910 btm = lowest - 1 - ascent - descent;
22911 }
22912 }
22913 else
22914 {
22915 /* A composition rule is specified by an integer
22916 value that encodes global and new reference
22917 points (GREF and NREF). GREF and NREF are
22918 specified by numbers as below:
22919
22920 0---1---2 -- ascent
22921 | |
22922 | |
22923 | |
22924 9--10--11 -- center
22925 | |
22926 ---3---4---5--- baseline
22927 | |
22928 6---7---8 -- descent
22929 */
22930 int rule = COMPOSITION_RULE (cmp, i);
22931 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22932
22933 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22934 grefx = gref % 3, nrefx = nref % 3;
22935 grefy = gref / 3, nrefy = nref / 3;
22936 if (xoff)
22937 xoff = font_height * (xoff - 128) / 256;
22938 if (yoff)
22939 yoff = font_height * (yoff - 128) / 256;
22940
22941 left = (leftmost
22942 + grefx * (rightmost - leftmost) / 2
22943 - nrefx * width / 2
22944 + xoff);
22945
22946 btm = ((grefy == 0 ? highest
22947 : grefy == 1 ? 0
22948 : grefy == 2 ? lowest
22949 : (highest + lowest) / 2)
22950 - (nrefy == 0 ? ascent + descent
22951 : nrefy == 1 ? descent - boff
22952 : nrefy == 2 ? 0
22953 : (ascent + descent) / 2)
22954 + yoff);
22955 }
22956
22957 cmp->offsets[i * 2] = left;
22958 cmp->offsets[i * 2 + 1] = btm + descent;
22959
22960 /* Update the bounding box of the overall glyphs. */
22961 if (width > 0)
22962 {
22963 right = left + width;
22964 if (left < leftmost)
22965 leftmost = left;
22966 if (right > rightmost)
22967 rightmost = right;
22968 }
22969 top = btm + descent + ascent;
22970 if (top > highest)
22971 highest = top;
22972 if (btm < lowest)
22973 lowest = btm;
22974
22975 if (cmp->lbearing > left + lbearing)
22976 cmp->lbearing = left + lbearing;
22977 if (cmp->rbearing < left + rbearing)
22978 cmp->rbearing = left + rbearing;
22979 }
22980 }
22981
22982 /* If there are glyphs whose x-offsets are negative,
22983 shift all glyphs to the right and make all x-offsets
22984 non-negative. */
22985 if (leftmost < 0)
22986 {
22987 for (i = 0; i < cmp->glyph_len; i++)
22988 cmp->offsets[i * 2] -= leftmost;
22989 rightmost -= leftmost;
22990 cmp->lbearing -= leftmost;
22991 cmp->rbearing -= leftmost;
22992 }
22993
22994 if (left_padded && cmp->lbearing < 0)
22995 {
22996 for (i = 0; i < cmp->glyph_len; i++)
22997 cmp->offsets[i * 2] -= cmp->lbearing;
22998 rightmost -= cmp->lbearing;
22999 cmp->rbearing -= cmp->lbearing;
23000 cmp->lbearing = 0;
23001 }
23002 if (right_padded && rightmost < cmp->rbearing)
23003 {
23004 rightmost = cmp->rbearing;
23005 }
23006
23007 cmp->pixel_width = rightmost;
23008 cmp->ascent = highest;
23009 cmp->descent = - lowest;
23010 if (cmp->ascent < font_ascent)
23011 cmp->ascent = font_ascent;
23012 if (cmp->descent < font_descent)
23013 cmp->descent = font_descent;
23014 }
23015
23016 if (it->glyph_row
23017 && (cmp->lbearing < 0
23018 || cmp->rbearing > cmp->pixel_width))
23019 it->glyph_row->contains_overlapping_glyphs_p = 1;
23020
23021 it->pixel_width = cmp->pixel_width;
23022 it->ascent = it->phys_ascent = cmp->ascent;
23023 it->descent = it->phys_descent = cmp->descent;
23024 if (face->box != FACE_NO_BOX)
23025 {
23026 int thick = face->box_line_width;
23027
23028 if (thick > 0)
23029 {
23030 it->ascent += thick;
23031 it->descent += thick;
23032 }
23033 else
23034 thick = - thick;
23035
23036 if (it->start_of_box_run_p)
23037 it->pixel_width += thick;
23038 if (it->end_of_box_run_p)
23039 it->pixel_width += thick;
23040 }
23041
23042 /* If face has an overline, add the height of the overline
23043 (1 pixel) and a 1 pixel margin to the character height. */
23044 if (face->overline_p)
23045 it->ascent += overline_margin;
23046
23047 take_vertical_position_into_account (it);
23048 if (it->ascent < 0)
23049 it->ascent = 0;
23050 if (it->descent < 0)
23051 it->descent = 0;
23052
23053 if (it->glyph_row)
23054 append_composite_glyph (it);
23055 }
23056 else if (it->what == IT_COMPOSITION)
23057 {
23058 /* A dynamic (automatic) composition. */
23059 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23060 Lisp_Object gstring;
23061 struct font_metrics metrics;
23062
23063 gstring = composition_gstring_from_id (it->cmp_it.id);
23064 it->pixel_width
23065 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23066 &metrics);
23067 if (it->glyph_row
23068 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23069 it->glyph_row->contains_overlapping_glyphs_p = 1;
23070 it->ascent = it->phys_ascent = metrics.ascent;
23071 it->descent = it->phys_descent = metrics.descent;
23072 if (face->box != FACE_NO_BOX)
23073 {
23074 int thick = face->box_line_width;
23075
23076 if (thick > 0)
23077 {
23078 it->ascent += thick;
23079 it->descent += thick;
23080 }
23081 else
23082 thick = - thick;
23083
23084 if (it->start_of_box_run_p)
23085 it->pixel_width += thick;
23086 if (it->end_of_box_run_p)
23087 it->pixel_width += thick;
23088 }
23089 /* If face has an overline, add the height of the overline
23090 (1 pixel) and a 1 pixel margin to the character height. */
23091 if (face->overline_p)
23092 it->ascent += overline_margin;
23093 take_vertical_position_into_account (it);
23094 if (it->ascent < 0)
23095 it->ascent = 0;
23096 if (it->descent < 0)
23097 it->descent = 0;
23098
23099 if (it->glyph_row)
23100 append_composite_glyph (it);
23101 }
23102 else if (it->what == IT_GLYPHLESS)
23103 produce_glyphless_glyph (it, 0, Qnil);
23104 else if (it->what == IT_IMAGE)
23105 produce_image_glyph (it);
23106 else if (it->what == IT_STRETCH)
23107 produce_stretch_glyph (it);
23108
23109 done:
23110 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23111 because this isn't true for images with `:ascent 100'. */
23112 xassert (it->ascent >= 0 && it->descent >= 0);
23113 if (it->area == TEXT_AREA)
23114 it->current_x += it->pixel_width;
23115
23116 if (extra_line_spacing > 0)
23117 {
23118 it->descent += extra_line_spacing;
23119 if (extra_line_spacing > it->max_extra_line_spacing)
23120 it->max_extra_line_spacing = extra_line_spacing;
23121 }
23122
23123 it->max_ascent = max (it->max_ascent, it->ascent);
23124 it->max_descent = max (it->max_descent, it->descent);
23125 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23126 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23127 }
23128
23129 /* EXPORT for RIF:
23130 Output LEN glyphs starting at START at the nominal cursor position.
23131 Advance the nominal cursor over the text. The global variable
23132 updated_window contains the window being updated, updated_row is
23133 the glyph row being updated, and updated_area is the area of that
23134 row being updated. */
23135
23136 void
23137 x_write_glyphs (struct glyph *start, int len)
23138 {
23139 int x, hpos;
23140
23141 xassert (updated_window && updated_row);
23142 BLOCK_INPUT;
23143
23144 /* Write glyphs. */
23145
23146 hpos = start - updated_row->glyphs[updated_area];
23147 x = draw_glyphs (updated_window, output_cursor.x,
23148 updated_row, updated_area,
23149 hpos, hpos + len,
23150 DRAW_NORMAL_TEXT, 0);
23151
23152 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23153 if (updated_area == TEXT_AREA
23154 && updated_window->phys_cursor_on_p
23155 && updated_window->phys_cursor.vpos == output_cursor.vpos
23156 && updated_window->phys_cursor.hpos >= hpos
23157 && updated_window->phys_cursor.hpos < hpos + len)
23158 updated_window->phys_cursor_on_p = 0;
23159
23160 UNBLOCK_INPUT;
23161
23162 /* Advance the output cursor. */
23163 output_cursor.hpos += len;
23164 output_cursor.x = x;
23165 }
23166
23167
23168 /* EXPORT for RIF:
23169 Insert LEN glyphs from START at the nominal cursor position. */
23170
23171 void
23172 x_insert_glyphs (struct glyph *start, int len)
23173 {
23174 struct frame *f;
23175 struct window *w;
23176 int line_height, shift_by_width, shifted_region_width;
23177 struct glyph_row *row;
23178 struct glyph *glyph;
23179 int frame_x, frame_y;
23180 EMACS_INT hpos;
23181
23182 xassert (updated_window && updated_row);
23183 BLOCK_INPUT;
23184 w = updated_window;
23185 f = XFRAME (WINDOW_FRAME (w));
23186
23187 /* Get the height of the line we are in. */
23188 row = updated_row;
23189 line_height = row->height;
23190
23191 /* Get the width of the glyphs to insert. */
23192 shift_by_width = 0;
23193 for (glyph = start; glyph < start + len; ++glyph)
23194 shift_by_width += glyph->pixel_width;
23195
23196 /* Get the width of the region to shift right. */
23197 shifted_region_width = (window_box_width (w, updated_area)
23198 - output_cursor.x
23199 - shift_by_width);
23200
23201 /* Shift right. */
23202 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23203 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23204
23205 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23206 line_height, shift_by_width);
23207
23208 /* Write the glyphs. */
23209 hpos = start - row->glyphs[updated_area];
23210 draw_glyphs (w, output_cursor.x, row, updated_area,
23211 hpos, hpos + len,
23212 DRAW_NORMAL_TEXT, 0);
23213
23214 /* Advance the output cursor. */
23215 output_cursor.hpos += len;
23216 output_cursor.x += shift_by_width;
23217 UNBLOCK_INPUT;
23218 }
23219
23220
23221 /* EXPORT for RIF:
23222 Erase the current text line from the nominal cursor position
23223 (inclusive) to pixel column TO_X (exclusive). The idea is that
23224 everything from TO_X onward is already erased.
23225
23226 TO_X is a pixel position relative to updated_area of
23227 updated_window. TO_X == -1 means clear to the end of this area. */
23228
23229 void
23230 x_clear_end_of_line (int to_x)
23231 {
23232 struct frame *f;
23233 struct window *w = updated_window;
23234 int max_x, min_y, max_y;
23235 int from_x, from_y, to_y;
23236
23237 xassert (updated_window && updated_row);
23238 f = XFRAME (w->frame);
23239
23240 if (updated_row->full_width_p)
23241 max_x = WINDOW_TOTAL_WIDTH (w);
23242 else
23243 max_x = window_box_width (w, updated_area);
23244 max_y = window_text_bottom_y (w);
23245
23246 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23247 of window. For TO_X > 0, truncate to end of drawing area. */
23248 if (to_x == 0)
23249 return;
23250 else if (to_x < 0)
23251 to_x = max_x;
23252 else
23253 to_x = min (to_x, max_x);
23254
23255 to_y = min (max_y, output_cursor.y + updated_row->height);
23256
23257 /* Notice if the cursor will be cleared by this operation. */
23258 if (!updated_row->full_width_p)
23259 notice_overwritten_cursor (w, updated_area,
23260 output_cursor.x, -1,
23261 updated_row->y,
23262 MATRIX_ROW_BOTTOM_Y (updated_row));
23263
23264 from_x = output_cursor.x;
23265
23266 /* Translate to frame coordinates. */
23267 if (updated_row->full_width_p)
23268 {
23269 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23270 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23271 }
23272 else
23273 {
23274 int area_left = window_box_left (w, updated_area);
23275 from_x += area_left;
23276 to_x += area_left;
23277 }
23278
23279 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23280 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23281 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23282
23283 /* Prevent inadvertently clearing to end of the X window. */
23284 if (to_x > from_x && to_y > from_y)
23285 {
23286 BLOCK_INPUT;
23287 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23288 to_x - from_x, to_y - from_y);
23289 UNBLOCK_INPUT;
23290 }
23291 }
23292
23293 #endif /* HAVE_WINDOW_SYSTEM */
23294
23295
23296 \f
23297 /***********************************************************************
23298 Cursor types
23299 ***********************************************************************/
23300
23301 /* Value is the internal representation of the specified cursor type
23302 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23303 of the bar cursor. */
23304
23305 static enum text_cursor_kinds
23306 get_specified_cursor_type (Lisp_Object arg, int *width)
23307 {
23308 enum text_cursor_kinds type;
23309
23310 if (NILP (arg))
23311 return NO_CURSOR;
23312
23313 if (EQ (arg, Qbox))
23314 return FILLED_BOX_CURSOR;
23315
23316 if (EQ (arg, Qhollow))
23317 return HOLLOW_BOX_CURSOR;
23318
23319 if (EQ (arg, Qbar))
23320 {
23321 *width = 2;
23322 return BAR_CURSOR;
23323 }
23324
23325 if (CONSP (arg)
23326 && EQ (XCAR (arg), Qbar)
23327 && INTEGERP (XCDR (arg))
23328 && XINT (XCDR (arg)) >= 0)
23329 {
23330 *width = XINT (XCDR (arg));
23331 return BAR_CURSOR;
23332 }
23333
23334 if (EQ (arg, Qhbar))
23335 {
23336 *width = 2;
23337 return HBAR_CURSOR;
23338 }
23339
23340 if (CONSP (arg)
23341 && EQ (XCAR (arg), Qhbar)
23342 && INTEGERP (XCDR (arg))
23343 && XINT (XCDR (arg)) >= 0)
23344 {
23345 *width = XINT (XCDR (arg));
23346 return HBAR_CURSOR;
23347 }
23348
23349 /* Treat anything unknown as "hollow box cursor".
23350 It was bad to signal an error; people have trouble fixing
23351 .Xdefaults with Emacs, when it has something bad in it. */
23352 type = HOLLOW_BOX_CURSOR;
23353
23354 return type;
23355 }
23356
23357 /* Set the default cursor types for specified frame. */
23358 void
23359 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23360 {
23361 int width = 1;
23362 Lisp_Object tem;
23363
23364 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23365 FRAME_CURSOR_WIDTH (f) = width;
23366
23367 /* By default, set up the blink-off state depending on the on-state. */
23368
23369 tem = Fassoc (arg, Vblink_cursor_alist);
23370 if (!NILP (tem))
23371 {
23372 FRAME_BLINK_OFF_CURSOR (f)
23373 = get_specified_cursor_type (XCDR (tem), &width);
23374 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23375 }
23376 else
23377 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23378 }
23379
23380
23381 #ifdef HAVE_WINDOW_SYSTEM
23382
23383 /* Return the cursor we want to be displayed in window W. Return
23384 width of bar/hbar cursor through WIDTH arg. Return with
23385 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23386 (i.e. if the `system caret' should track this cursor).
23387
23388 In a mini-buffer window, we want the cursor only to appear if we
23389 are reading input from this window. For the selected window, we
23390 want the cursor type given by the frame parameter or buffer local
23391 setting of cursor-type. If explicitly marked off, draw no cursor.
23392 In all other cases, we want a hollow box cursor. */
23393
23394 static enum text_cursor_kinds
23395 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23396 int *active_cursor)
23397 {
23398 struct frame *f = XFRAME (w->frame);
23399 struct buffer *b = XBUFFER (w->buffer);
23400 int cursor_type = DEFAULT_CURSOR;
23401 Lisp_Object alt_cursor;
23402 int non_selected = 0;
23403
23404 *active_cursor = 1;
23405
23406 /* Echo area */
23407 if (cursor_in_echo_area
23408 && FRAME_HAS_MINIBUF_P (f)
23409 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23410 {
23411 if (w == XWINDOW (echo_area_window))
23412 {
23413 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
23414 {
23415 *width = FRAME_CURSOR_WIDTH (f);
23416 return FRAME_DESIRED_CURSOR (f);
23417 }
23418 else
23419 return get_specified_cursor_type (BVAR (b, cursor_type), width);
23420 }
23421
23422 *active_cursor = 0;
23423 non_selected = 1;
23424 }
23425
23426 /* Detect a nonselected window or nonselected frame. */
23427 else if (w != XWINDOW (f->selected_window)
23428 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23429 {
23430 *active_cursor = 0;
23431
23432 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23433 return NO_CURSOR;
23434
23435 non_selected = 1;
23436 }
23437
23438 /* Never display a cursor in a window in which cursor-type is nil. */
23439 if (NILP (BVAR (b, cursor_type)))
23440 return NO_CURSOR;
23441
23442 /* Get the normal cursor type for this window. */
23443 if (EQ (BVAR (b, cursor_type), Qt))
23444 {
23445 cursor_type = FRAME_DESIRED_CURSOR (f);
23446 *width = FRAME_CURSOR_WIDTH (f);
23447 }
23448 else
23449 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
23450
23451 /* Use cursor-in-non-selected-windows instead
23452 for non-selected window or frame. */
23453 if (non_selected)
23454 {
23455 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
23456 if (!EQ (Qt, alt_cursor))
23457 return get_specified_cursor_type (alt_cursor, width);
23458 /* t means modify the normal cursor type. */
23459 if (cursor_type == FILLED_BOX_CURSOR)
23460 cursor_type = HOLLOW_BOX_CURSOR;
23461 else if (cursor_type == BAR_CURSOR && *width > 1)
23462 --*width;
23463 return cursor_type;
23464 }
23465
23466 /* Use normal cursor if not blinked off. */
23467 if (!w->cursor_off_p)
23468 {
23469 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23470 {
23471 if (cursor_type == FILLED_BOX_CURSOR)
23472 {
23473 /* Using a block cursor on large images can be very annoying.
23474 So use a hollow cursor for "large" images.
23475 If image is not transparent (no mask), also use hollow cursor. */
23476 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23477 if (img != NULL && IMAGEP (img->spec))
23478 {
23479 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23480 where N = size of default frame font size.
23481 This should cover most of the "tiny" icons people may use. */
23482 if (!img->mask
23483 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23484 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23485 cursor_type = HOLLOW_BOX_CURSOR;
23486 }
23487 }
23488 else if (cursor_type != NO_CURSOR)
23489 {
23490 /* Display current only supports BOX and HOLLOW cursors for images.
23491 So for now, unconditionally use a HOLLOW cursor when cursor is
23492 not a solid box cursor. */
23493 cursor_type = HOLLOW_BOX_CURSOR;
23494 }
23495 }
23496 return cursor_type;
23497 }
23498
23499 /* Cursor is blinked off, so determine how to "toggle" it. */
23500
23501 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23502 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
23503 return get_specified_cursor_type (XCDR (alt_cursor), width);
23504
23505 /* Then see if frame has specified a specific blink off cursor type. */
23506 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23507 {
23508 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23509 return FRAME_BLINK_OFF_CURSOR (f);
23510 }
23511
23512 #if 0
23513 /* Some people liked having a permanently visible blinking cursor,
23514 while others had very strong opinions against it. So it was
23515 decided to remove it. KFS 2003-09-03 */
23516
23517 /* Finally perform built-in cursor blinking:
23518 filled box <-> hollow box
23519 wide [h]bar <-> narrow [h]bar
23520 narrow [h]bar <-> no cursor
23521 other type <-> no cursor */
23522
23523 if (cursor_type == FILLED_BOX_CURSOR)
23524 return HOLLOW_BOX_CURSOR;
23525
23526 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23527 {
23528 *width = 1;
23529 return cursor_type;
23530 }
23531 #endif
23532
23533 return NO_CURSOR;
23534 }
23535
23536
23537 /* Notice when the text cursor of window W has been completely
23538 overwritten by a drawing operation that outputs glyphs in AREA
23539 starting at X0 and ending at X1 in the line starting at Y0 and
23540 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23541 the rest of the line after X0 has been written. Y coordinates
23542 are window-relative. */
23543
23544 static void
23545 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23546 int x0, int x1, int y0, int y1)
23547 {
23548 int cx0, cx1, cy0, cy1;
23549 struct glyph_row *row;
23550
23551 if (!w->phys_cursor_on_p)
23552 return;
23553 if (area != TEXT_AREA)
23554 return;
23555
23556 if (w->phys_cursor.vpos < 0
23557 || w->phys_cursor.vpos >= w->current_matrix->nrows
23558 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23559 !(row->enabled_p && row->displays_text_p)))
23560 return;
23561
23562 if (row->cursor_in_fringe_p)
23563 {
23564 row->cursor_in_fringe_p = 0;
23565 draw_fringe_bitmap (w, row, row->reversed_p);
23566 w->phys_cursor_on_p = 0;
23567 return;
23568 }
23569
23570 cx0 = w->phys_cursor.x;
23571 cx1 = cx0 + w->phys_cursor_width;
23572 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23573 return;
23574
23575 /* The cursor image will be completely removed from the
23576 screen if the output area intersects the cursor area in
23577 y-direction. When we draw in [y0 y1[, and some part of
23578 the cursor is at y < y0, that part must have been drawn
23579 before. When scrolling, the cursor is erased before
23580 actually scrolling, so we don't come here. When not
23581 scrolling, the rows above the old cursor row must have
23582 changed, and in this case these rows must have written
23583 over the cursor image.
23584
23585 Likewise if part of the cursor is below y1, with the
23586 exception of the cursor being in the first blank row at
23587 the buffer and window end because update_text_area
23588 doesn't draw that row. (Except when it does, but
23589 that's handled in update_text_area.) */
23590
23591 cy0 = w->phys_cursor.y;
23592 cy1 = cy0 + w->phys_cursor_height;
23593 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23594 return;
23595
23596 w->phys_cursor_on_p = 0;
23597 }
23598
23599 #endif /* HAVE_WINDOW_SYSTEM */
23600
23601 \f
23602 /************************************************************************
23603 Mouse Face
23604 ************************************************************************/
23605
23606 #ifdef HAVE_WINDOW_SYSTEM
23607
23608 /* EXPORT for RIF:
23609 Fix the display of area AREA of overlapping row ROW in window W
23610 with respect to the overlapping part OVERLAPS. */
23611
23612 void
23613 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23614 enum glyph_row_area area, int overlaps)
23615 {
23616 int i, x;
23617
23618 BLOCK_INPUT;
23619
23620 x = 0;
23621 for (i = 0; i < row->used[area];)
23622 {
23623 if (row->glyphs[area][i].overlaps_vertically_p)
23624 {
23625 int start = i, start_x = x;
23626
23627 do
23628 {
23629 x += row->glyphs[area][i].pixel_width;
23630 ++i;
23631 }
23632 while (i < row->used[area]
23633 && row->glyphs[area][i].overlaps_vertically_p);
23634
23635 draw_glyphs (w, start_x, row, area,
23636 start, i,
23637 DRAW_NORMAL_TEXT, overlaps);
23638 }
23639 else
23640 {
23641 x += row->glyphs[area][i].pixel_width;
23642 ++i;
23643 }
23644 }
23645
23646 UNBLOCK_INPUT;
23647 }
23648
23649
23650 /* EXPORT:
23651 Draw the cursor glyph of window W in glyph row ROW. See the
23652 comment of draw_glyphs for the meaning of HL. */
23653
23654 void
23655 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23656 enum draw_glyphs_face hl)
23657 {
23658 /* If cursor hpos is out of bounds, don't draw garbage. This can
23659 happen in mini-buffer windows when switching between echo area
23660 glyphs and mini-buffer. */
23661 if ((row->reversed_p
23662 ? (w->phys_cursor.hpos >= 0)
23663 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23664 {
23665 int on_p = w->phys_cursor_on_p;
23666 int x1;
23667 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23668 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23669 hl, 0);
23670 w->phys_cursor_on_p = on_p;
23671
23672 if (hl == DRAW_CURSOR)
23673 w->phys_cursor_width = x1 - w->phys_cursor.x;
23674 /* When we erase the cursor, and ROW is overlapped by other
23675 rows, make sure that these overlapping parts of other rows
23676 are redrawn. */
23677 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23678 {
23679 w->phys_cursor_width = x1 - w->phys_cursor.x;
23680
23681 if (row > w->current_matrix->rows
23682 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23683 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23684 OVERLAPS_ERASED_CURSOR);
23685
23686 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23687 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23688 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23689 OVERLAPS_ERASED_CURSOR);
23690 }
23691 }
23692 }
23693
23694
23695 /* EXPORT:
23696 Erase the image of a cursor of window W from the screen. */
23697
23698 void
23699 erase_phys_cursor (struct window *w)
23700 {
23701 struct frame *f = XFRAME (w->frame);
23702 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23703 int hpos = w->phys_cursor.hpos;
23704 int vpos = w->phys_cursor.vpos;
23705 int mouse_face_here_p = 0;
23706 struct glyph_matrix *active_glyphs = w->current_matrix;
23707 struct glyph_row *cursor_row;
23708 struct glyph *cursor_glyph;
23709 enum draw_glyphs_face hl;
23710
23711 /* No cursor displayed or row invalidated => nothing to do on the
23712 screen. */
23713 if (w->phys_cursor_type == NO_CURSOR)
23714 goto mark_cursor_off;
23715
23716 /* VPOS >= active_glyphs->nrows means that window has been resized.
23717 Don't bother to erase the cursor. */
23718 if (vpos >= active_glyphs->nrows)
23719 goto mark_cursor_off;
23720
23721 /* If row containing cursor is marked invalid, there is nothing we
23722 can do. */
23723 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23724 if (!cursor_row->enabled_p)
23725 goto mark_cursor_off;
23726
23727 /* If line spacing is > 0, old cursor may only be partially visible in
23728 window after split-window. So adjust visible height. */
23729 cursor_row->visible_height = min (cursor_row->visible_height,
23730 window_text_bottom_y (w) - cursor_row->y);
23731
23732 /* If row is completely invisible, don't attempt to delete a cursor which
23733 isn't there. This can happen if cursor is at top of a window, and
23734 we switch to a buffer with a header line in that window. */
23735 if (cursor_row->visible_height <= 0)
23736 goto mark_cursor_off;
23737
23738 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23739 if (cursor_row->cursor_in_fringe_p)
23740 {
23741 cursor_row->cursor_in_fringe_p = 0;
23742 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23743 goto mark_cursor_off;
23744 }
23745
23746 /* This can happen when the new row is shorter than the old one.
23747 In this case, either draw_glyphs or clear_end_of_line
23748 should have cleared the cursor. Note that we wouldn't be
23749 able to erase the cursor in this case because we don't have a
23750 cursor glyph at hand. */
23751 if ((cursor_row->reversed_p
23752 ? (w->phys_cursor.hpos < 0)
23753 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23754 goto mark_cursor_off;
23755
23756 /* If the cursor is in the mouse face area, redisplay that when
23757 we clear the cursor. */
23758 if (! NILP (hlinfo->mouse_face_window)
23759 && coords_in_mouse_face_p (w, hpos, vpos)
23760 /* Don't redraw the cursor's spot in mouse face if it is at the
23761 end of a line (on a newline). The cursor appears there, but
23762 mouse highlighting does not. */
23763 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23764 mouse_face_here_p = 1;
23765
23766 /* Maybe clear the display under the cursor. */
23767 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23768 {
23769 int x, y, left_x;
23770 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23771 int width;
23772
23773 cursor_glyph = get_phys_cursor_glyph (w);
23774 if (cursor_glyph == NULL)
23775 goto mark_cursor_off;
23776
23777 width = cursor_glyph->pixel_width;
23778 left_x = window_box_left_offset (w, TEXT_AREA);
23779 x = w->phys_cursor.x;
23780 if (x < left_x)
23781 width -= left_x - x;
23782 width = min (width, window_box_width (w, TEXT_AREA) - x);
23783 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23784 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23785
23786 if (width > 0)
23787 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23788 }
23789
23790 /* Erase the cursor by redrawing the character underneath it. */
23791 if (mouse_face_here_p)
23792 hl = DRAW_MOUSE_FACE;
23793 else
23794 hl = DRAW_NORMAL_TEXT;
23795 draw_phys_cursor_glyph (w, cursor_row, hl);
23796
23797 mark_cursor_off:
23798 w->phys_cursor_on_p = 0;
23799 w->phys_cursor_type = NO_CURSOR;
23800 }
23801
23802
23803 /* EXPORT:
23804 Display or clear cursor of window W. If ON is zero, clear the
23805 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23806 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23807
23808 void
23809 display_and_set_cursor (struct window *w, int on,
23810 int hpos, int vpos, int x, int y)
23811 {
23812 struct frame *f = XFRAME (w->frame);
23813 int new_cursor_type;
23814 int new_cursor_width;
23815 int active_cursor;
23816 struct glyph_row *glyph_row;
23817 struct glyph *glyph;
23818
23819 /* This is pointless on invisible frames, and dangerous on garbaged
23820 windows and frames; in the latter case, the frame or window may
23821 be in the midst of changing its size, and x and y may be off the
23822 window. */
23823 if (! FRAME_VISIBLE_P (f)
23824 || FRAME_GARBAGED_P (f)
23825 || vpos >= w->current_matrix->nrows
23826 || hpos >= w->current_matrix->matrix_w)
23827 return;
23828
23829 /* If cursor is off and we want it off, return quickly. */
23830 if (!on && !w->phys_cursor_on_p)
23831 return;
23832
23833 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23834 /* If cursor row is not enabled, we don't really know where to
23835 display the cursor. */
23836 if (!glyph_row->enabled_p)
23837 {
23838 w->phys_cursor_on_p = 0;
23839 return;
23840 }
23841
23842 glyph = NULL;
23843 if (!glyph_row->exact_window_width_line_p
23844 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23845 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23846
23847 xassert (interrupt_input_blocked);
23848
23849 /* Set new_cursor_type to the cursor we want to be displayed. */
23850 new_cursor_type = get_window_cursor_type (w, glyph,
23851 &new_cursor_width, &active_cursor);
23852
23853 /* If cursor is currently being shown and we don't want it to be or
23854 it is in the wrong place, or the cursor type is not what we want,
23855 erase it. */
23856 if (w->phys_cursor_on_p
23857 && (!on
23858 || w->phys_cursor.x != x
23859 || w->phys_cursor.y != y
23860 || new_cursor_type != w->phys_cursor_type
23861 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23862 && new_cursor_width != w->phys_cursor_width)))
23863 erase_phys_cursor (w);
23864
23865 /* Don't check phys_cursor_on_p here because that flag is only set
23866 to zero in some cases where we know that the cursor has been
23867 completely erased, to avoid the extra work of erasing the cursor
23868 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23869 still not be visible, or it has only been partly erased. */
23870 if (on)
23871 {
23872 w->phys_cursor_ascent = glyph_row->ascent;
23873 w->phys_cursor_height = glyph_row->height;
23874
23875 /* Set phys_cursor_.* before x_draw_.* is called because some
23876 of them may need the information. */
23877 w->phys_cursor.x = x;
23878 w->phys_cursor.y = glyph_row->y;
23879 w->phys_cursor.hpos = hpos;
23880 w->phys_cursor.vpos = vpos;
23881 }
23882
23883 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23884 new_cursor_type, new_cursor_width,
23885 on, active_cursor);
23886 }
23887
23888
23889 /* Switch the display of W's cursor on or off, according to the value
23890 of ON. */
23891
23892 static void
23893 update_window_cursor (struct window *w, int on)
23894 {
23895 /* Don't update cursor in windows whose frame is in the process
23896 of being deleted. */
23897 if (w->current_matrix)
23898 {
23899 BLOCK_INPUT;
23900 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23901 w->phys_cursor.x, w->phys_cursor.y);
23902 UNBLOCK_INPUT;
23903 }
23904 }
23905
23906
23907 /* Call update_window_cursor with parameter ON_P on all leaf windows
23908 in the window tree rooted at W. */
23909
23910 static void
23911 update_cursor_in_window_tree (struct window *w, int on_p)
23912 {
23913 while (w)
23914 {
23915 if (!NILP (w->hchild))
23916 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23917 else if (!NILP (w->vchild))
23918 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23919 else
23920 update_window_cursor (w, on_p);
23921
23922 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23923 }
23924 }
23925
23926
23927 /* EXPORT:
23928 Display the cursor on window W, or clear it, according to ON_P.
23929 Don't change the cursor's position. */
23930
23931 void
23932 x_update_cursor (struct frame *f, int on_p)
23933 {
23934 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23935 }
23936
23937
23938 /* EXPORT:
23939 Clear the cursor of window W to background color, and mark the
23940 cursor as not shown. This is used when the text where the cursor
23941 is about to be rewritten. */
23942
23943 void
23944 x_clear_cursor (struct window *w)
23945 {
23946 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23947 update_window_cursor (w, 0);
23948 }
23949
23950 #endif /* HAVE_WINDOW_SYSTEM */
23951
23952 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
23953 and MSDOS. */
23954 static void
23955 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
23956 int start_hpos, int end_hpos,
23957 enum draw_glyphs_face draw)
23958 {
23959 #ifdef HAVE_WINDOW_SYSTEM
23960 if (FRAME_WINDOW_P (XFRAME (w->frame)))
23961 {
23962 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
23963 return;
23964 }
23965 #endif
23966 #if defined (HAVE_GPM) || defined (MSDOS)
23967 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
23968 #endif
23969 }
23970
23971 /* Display the active region described by mouse_face_* according to DRAW. */
23972
23973 static void
23974 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
23975 {
23976 struct window *w = XWINDOW (hlinfo->mouse_face_window);
23977 struct frame *f = XFRAME (WINDOW_FRAME (w));
23978
23979 if (/* If window is in the process of being destroyed, don't bother
23980 to do anything. */
23981 w->current_matrix != NULL
23982 /* Don't update mouse highlight if hidden */
23983 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
23984 /* Recognize when we are called to operate on rows that don't exist
23985 anymore. This can happen when a window is split. */
23986 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
23987 {
23988 int phys_cursor_on_p = w->phys_cursor_on_p;
23989 struct glyph_row *row, *first, *last;
23990
23991 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23992 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23993
23994 for (row = first; row <= last && row->enabled_p; ++row)
23995 {
23996 int start_hpos, end_hpos, start_x;
23997
23998 /* For all but the first row, the highlight starts at column 0. */
23999 if (row == first)
24000 {
24001 /* R2L rows have BEG and END in reversed order, but the
24002 screen drawing geometry is always left to right. So
24003 we need to mirror the beginning and end of the
24004 highlighted area in R2L rows. */
24005 if (!row->reversed_p)
24006 {
24007 start_hpos = hlinfo->mouse_face_beg_col;
24008 start_x = hlinfo->mouse_face_beg_x;
24009 }
24010 else if (row == last)
24011 {
24012 start_hpos = hlinfo->mouse_face_end_col;
24013 start_x = hlinfo->mouse_face_end_x;
24014 }
24015 else
24016 {
24017 start_hpos = 0;
24018 start_x = 0;
24019 }
24020 }
24021 else if (row->reversed_p && row == last)
24022 {
24023 start_hpos = hlinfo->mouse_face_end_col;
24024 start_x = hlinfo->mouse_face_end_x;
24025 }
24026 else
24027 {
24028 start_hpos = 0;
24029 start_x = 0;
24030 }
24031
24032 if (row == last)
24033 {
24034 if (!row->reversed_p)
24035 end_hpos = hlinfo->mouse_face_end_col;
24036 else if (row == first)
24037 end_hpos = hlinfo->mouse_face_beg_col;
24038 else
24039 {
24040 end_hpos = row->used[TEXT_AREA];
24041 if (draw == DRAW_NORMAL_TEXT)
24042 row->fill_line_p = 1; /* Clear to end of line */
24043 }
24044 }
24045 else if (row->reversed_p && row == first)
24046 end_hpos = hlinfo->mouse_face_beg_col;
24047 else
24048 {
24049 end_hpos = row->used[TEXT_AREA];
24050 if (draw == DRAW_NORMAL_TEXT)
24051 row->fill_line_p = 1; /* Clear to end of line */
24052 }
24053
24054 if (end_hpos > start_hpos)
24055 {
24056 draw_row_with_mouse_face (w, start_x, row,
24057 start_hpos, end_hpos, draw);
24058
24059 row->mouse_face_p
24060 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24061 }
24062 }
24063
24064 #ifdef HAVE_WINDOW_SYSTEM
24065 /* When we've written over the cursor, arrange for it to
24066 be displayed again. */
24067 if (FRAME_WINDOW_P (f)
24068 && phys_cursor_on_p && !w->phys_cursor_on_p)
24069 {
24070 BLOCK_INPUT;
24071 display_and_set_cursor (w, 1,
24072 w->phys_cursor.hpos, w->phys_cursor.vpos,
24073 w->phys_cursor.x, w->phys_cursor.y);
24074 UNBLOCK_INPUT;
24075 }
24076 #endif /* HAVE_WINDOW_SYSTEM */
24077 }
24078
24079 #ifdef HAVE_WINDOW_SYSTEM
24080 /* Change the mouse cursor. */
24081 if (FRAME_WINDOW_P (f))
24082 {
24083 if (draw == DRAW_NORMAL_TEXT
24084 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24085 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24086 else if (draw == DRAW_MOUSE_FACE)
24087 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24088 else
24089 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24090 }
24091 #endif /* HAVE_WINDOW_SYSTEM */
24092 }
24093
24094 /* EXPORT:
24095 Clear out the mouse-highlighted active region.
24096 Redraw it un-highlighted first. Value is non-zero if mouse
24097 face was actually drawn unhighlighted. */
24098
24099 int
24100 clear_mouse_face (Mouse_HLInfo *hlinfo)
24101 {
24102 int cleared = 0;
24103
24104 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24105 {
24106 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24107 cleared = 1;
24108 }
24109
24110 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24111 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24112 hlinfo->mouse_face_window = Qnil;
24113 hlinfo->mouse_face_overlay = Qnil;
24114 return cleared;
24115 }
24116
24117 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24118 within the mouse face on that window. */
24119 static int
24120 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24121 {
24122 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24123
24124 /* Quickly resolve the easy cases. */
24125 if (!(WINDOWP (hlinfo->mouse_face_window)
24126 && XWINDOW (hlinfo->mouse_face_window) == w))
24127 return 0;
24128 if (vpos < hlinfo->mouse_face_beg_row
24129 || vpos > hlinfo->mouse_face_end_row)
24130 return 0;
24131 if (vpos > hlinfo->mouse_face_beg_row
24132 && vpos < hlinfo->mouse_face_end_row)
24133 return 1;
24134
24135 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24136 {
24137 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24138 {
24139 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24140 return 1;
24141 }
24142 else if ((vpos == hlinfo->mouse_face_beg_row
24143 && hpos >= hlinfo->mouse_face_beg_col)
24144 || (vpos == hlinfo->mouse_face_end_row
24145 && hpos < hlinfo->mouse_face_end_col))
24146 return 1;
24147 }
24148 else
24149 {
24150 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24151 {
24152 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24153 return 1;
24154 }
24155 else if ((vpos == hlinfo->mouse_face_beg_row
24156 && hpos <= hlinfo->mouse_face_beg_col)
24157 || (vpos == hlinfo->mouse_face_end_row
24158 && hpos > hlinfo->mouse_face_end_col))
24159 return 1;
24160 }
24161 return 0;
24162 }
24163
24164
24165 /* EXPORT:
24166 Non-zero if physical cursor of window W is within mouse face. */
24167
24168 int
24169 cursor_in_mouse_face_p (struct window *w)
24170 {
24171 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24172 }
24173
24174
24175 \f
24176 /* Find the glyph rows START_ROW and END_ROW of window W that display
24177 characters between buffer positions START_CHARPOS and END_CHARPOS
24178 (excluding END_CHARPOS). This is similar to row_containing_pos,
24179 but is more accurate when bidi reordering makes buffer positions
24180 change non-linearly with glyph rows. */
24181 static void
24182 rows_from_pos_range (struct window *w,
24183 EMACS_INT start_charpos, EMACS_INT end_charpos,
24184 struct glyph_row **start, struct glyph_row **end)
24185 {
24186 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24187 int last_y = window_text_bottom_y (w);
24188 struct glyph_row *row;
24189
24190 *start = NULL;
24191 *end = NULL;
24192
24193 while (!first->enabled_p
24194 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24195 first++;
24196
24197 /* Find the START row. */
24198 for (row = first;
24199 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24200 row++)
24201 {
24202 /* A row can potentially be the START row if the range of the
24203 characters it displays intersects the range
24204 [START_CHARPOS..END_CHARPOS). */
24205 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24206 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24207 /* See the commentary in row_containing_pos, for the
24208 explanation of the complicated way to check whether
24209 some position is beyond the end of the characters
24210 displayed by a row. */
24211 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24212 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24213 && !row->ends_at_zv_p
24214 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24215 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24216 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24217 && !row->ends_at_zv_p
24218 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24219 {
24220 /* Found a candidate row. Now make sure at least one of the
24221 glyphs it displays has a charpos from the range
24222 [START_CHARPOS..END_CHARPOS).
24223
24224 This is not obvious because bidi reordering could make
24225 buffer positions of a row be 1,2,3,102,101,100, and if we
24226 want to highlight characters in [50..60), we don't want
24227 this row, even though [50..60) does intersect [1..103),
24228 the range of character positions given by the row's start
24229 and end positions. */
24230 struct glyph *g = row->glyphs[TEXT_AREA];
24231 struct glyph *e = g + row->used[TEXT_AREA];
24232
24233 while (g < e)
24234 {
24235 if (BUFFERP (g->object)
24236 && start_charpos <= g->charpos && g->charpos < end_charpos)
24237 *start = row;
24238 g++;
24239 }
24240 if (*start)
24241 break;
24242 }
24243 }
24244
24245 /* Find the END row. */
24246 if (!*start
24247 /* If the last row is partially visible, start looking for END
24248 from that row, instead of starting from FIRST. */
24249 && !(row->enabled_p
24250 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24251 row = first;
24252 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24253 {
24254 struct glyph_row *next = row + 1;
24255
24256 if (!next->enabled_p
24257 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24258 /* The first row >= START whose range of displayed characters
24259 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24260 is the row END + 1. */
24261 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24262 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24263 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24264 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24265 && !next->ends_at_zv_p
24266 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24267 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24268 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24269 && !next->ends_at_zv_p
24270 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24271 {
24272 *end = row;
24273 break;
24274 }
24275 else
24276 {
24277 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24278 but none of the characters it displays are in the range, it is
24279 also END + 1. */
24280 struct glyph *g = next->glyphs[TEXT_AREA];
24281 struct glyph *e = g + next->used[TEXT_AREA];
24282
24283 while (g < e)
24284 {
24285 if (BUFFERP (g->object)
24286 && start_charpos <= g->charpos && g->charpos < end_charpos)
24287 break;
24288 g++;
24289 }
24290 if (g == e)
24291 {
24292 *end = row;
24293 break;
24294 }
24295 }
24296 }
24297 }
24298
24299 /* This function sets the mouse_face_* elements of HLINFO, assuming
24300 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24301 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24302 for the overlay or run of text properties specifying the mouse
24303 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24304 before-string and after-string that must also be highlighted.
24305 COVER_STRING, if non-nil, is a display string that may cover some
24306 or all of the highlighted text. */
24307
24308 static void
24309 mouse_face_from_buffer_pos (Lisp_Object window,
24310 Mouse_HLInfo *hlinfo,
24311 EMACS_INT mouse_charpos,
24312 EMACS_INT start_charpos,
24313 EMACS_INT end_charpos,
24314 Lisp_Object before_string,
24315 Lisp_Object after_string,
24316 Lisp_Object cover_string)
24317 {
24318 struct window *w = XWINDOW (window);
24319 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24320 struct glyph_row *r1, *r2;
24321 struct glyph *glyph, *end;
24322 EMACS_INT ignore, pos;
24323 int x;
24324
24325 xassert (NILP (cover_string) || STRINGP (cover_string));
24326 xassert (NILP (before_string) || STRINGP (before_string));
24327 xassert (NILP (after_string) || STRINGP (after_string));
24328
24329 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24330 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24331 if (r1 == NULL)
24332 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24333 /* If the before-string or display-string contains newlines,
24334 rows_from_pos_range skips to its last row. Move back. */
24335 if (!NILP (before_string) || !NILP (cover_string))
24336 {
24337 struct glyph_row *prev;
24338 while ((prev = r1 - 1, prev >= first)
24339 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24340 && prev->used[TEXT_AREA] > 0)
24341 {
24342 struct glyph *beg = prev->glyphs[TEXT_AREA];
24343 glyph = beg + prev->used[TEXT_AREA];
24344 while (--glyph >= beg && INTEGERP (glyph->object));
24345 if (glyph < beg
24346 || !(EQ (glyph->object, before_string)
24347 || EQ (glyph->object, cover_string)))
24348 break;
24349 r1 = prev;
24350 }
24351 }
24352 if (r2 == NULL)
24353 {
24354 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24355 hlinfo->mouse_face_past_end = 1;
24356 }
24357 else if (!NILP (after_string))
24358 {
24359 /* If the after-string has newlines, advance to its last row. */
24360 struct glyph_row *next;
24361 struct glyph_row *last
24362 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24363
24364 for (next = r2 + 1;
24365 next <= last
24366 && next->used[TEXT_AREA] > 0
24367 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24368 ++next)
24369 r2 = next;
24370 }
24371 /* The rest of the display engine assumes that mouse_face_beg_row is
24372 either above below mouse_face_end_row or identical to it. But
24373 with bidi-reordered continued lines, the row for START_CHARPOS
24374 could be below the row for END_CHARPOS. If so, swap the rows and
24375 store them in correct order. */
24376 if (r1->y > r2->y)
24377 {
24378 struct glyph_row *tem = r2;
24379
24380 r2 = r1;
24381 r1 = tem;
24382 }
24383
24384 hlinfo->mouse_face_beg_y = r1->y;
24385 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24386 hlinfo->mouse_face_end_y = r2->y;
24387 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24388
24389 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24390 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
24391 could be anywhere in the row and in any order. The strategy
24392 below is to find the leftmost and the rightmost glyph that
24393 belongs to either of these 3 strings, or whose position is
24394 between START_CHARPOS and END_CHARPOS, and highlight all the
24395 glyphs between those two. This may cover more than just the text
24396 between START_CHARPOS and END_CHARPOS if the range of characters
24397 strides the bidi level boundary, e.g. if the beginning is in R2L
24398 text while the end is in L2R text or vice versa. */
24399 if (!r1->reversed_p)
24400 {
24401 /* This row is in a left to right paragraph. Scan it left to
24402 right. */
24403 glyph = r1->glyphs[TEXT_AREA];
24404 end = glyph + r1->used[TEXT_AREA];
24405 x = r1->x;
24406
24407 /* Skip truncation glyphs at the start of the glyph row. */
24408 if (r1->displays_text_p)
24409 for (; glyph < end
24410 && INTEGERP (glyph->object)
24411 && glyph->charpos < 0;
24412 ++glyph)
24413 x += glyph->pixel_width;
24414
24415 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24416 or COVER_STRING, and the first glyph from buffer whose
24417 position is between START_CHARPOS and END_CHARPOS. */
24418 for (; glyph < end
24419 && !INTEGERP (glyph->object)
24420 && !EQ (glyph->object, cover_string)
24421 && !(BUFFERP (glyph->object)
24422 && (glyph->charpos >= start_charpos
24423 && glyph->charpos < end_charpos));
24424 ++glyph)
24425 {
24426 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24427 are present at buffer positions between START_CHARPOS and
24428 END_CHARPOS, or if they come from an overlay. */
24429 if (EQ (glyph->object, before_string))
24430 {
24431 pos = string_buffer_position (before_string,
24432 start_charpos);
24433 /* If pos == 0, it means before_string came from an
24434 overlay, not from a buffer position. */
24435 if (!pos || (pos >= start_charpos && pos < end_charpos))
24436 break;
24437 }
24438 else if (EQ (glyph->object, after_string))
24439 {
24440 pos = string_buffer_position (after_string, end_charpos);
24441 if (!pos || (pos >= start_charpos && pos < end_charpos))
24442 break;
24443 }
24444 x += glyph->pixel_width;
24445 }
24446 hlinfo->mouse_face_beg_x = x;
24447 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24448 }
24449 else
24450 {
24451 /* This row is in a right to left paragraph. Scan it right to
24452 left. */
24453 struct glyph *g;
24454
24455 end = r1->glyphs[TEXT_AREA] - 1;
24456 glyph = end + r1->used[TEXT_AREA];
24457
24458 /* Skip truncation glyphs at the start of the glyph row. */
24459 if (r1->displays_text_p)
24460 for (; glyph > end
24461 && INTEGERP (glyph->object)
24462 && glyph->charpos < 0;
24463 --glyph)
24464 ;
24465
24466 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24467 or COVER_STRING, and the first glyph from buffer whose
24468 position is between START_CHARPOS and END_CHARPOS. */
24469 for (; glyph > end
24470 && !INTEGERP (glyph->object)
24471 && !EQ (glyph->object, cover_string)
24472 && !(BUFFERP (glyph->object)
24473 && (glyph->charpos >= start_charpos
24474 && glyph->charpos < end_charpos));
24475 --glyph)
24476 {
24477 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24478 are present at buffer positions between START_CHARPOS and
24479 END_CHARPOS, or if they come from an overlay. */
24480 if (EQ (glyph->object, before_string))
24481 {
24482 pos = string_buffer_position (before_string, start_charpos);
24483 /* If pos == 0, it means before_string came from an
24484 overlay, not from a buffer position. */
24485 if (!pos || (pos >= start_charpos && pos < end_charpos))
24486 break;
24487 }
24488 else if (EQ (glyph->object, after_string))
24489 {
24490 pos = string_buffer_position (after_string, end_charpos);
24491 if (!pos || (pos >= start_charpos && pos < end_charpos))
24492 break;
24493 }
24494 }
24495
24496 glyph++; /* first glyph to the right of the highlighted area */
24497 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24498 x += g->pixel_width;
24499 hlinfo->mouse_face_beg_x = x;
24500 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24501 }
24502
24503 /* If the highlight ends in a different row, compute GLYPH and END
24504 for the end row. Otherwise, reuse the values computed above for
24505 the row where the highlight begins. */
24506 if (r2 != r1)
24507 {
24508 if (!r2->reversed_p)
24509 {
24510 glyph = r2->glyphs[TEXT_AREA];
24511 end = glyph + r2->used[TEXT_AREA];
24512 x = r2->x;
24513 }
24514 else
24515 {
24516 end = r2->glyphs[TEXT_AREA] - 1;
24517 glyph = end + r2->used[TEXT_AREA];
24518 }
24519 }
24520
24521 if (!r2->reversed_p)
24522 {
24523 /* Skip truncation and continuation glyphs near the end of the
24524 row, and also blanks and stretch glyphs inserted by
24525 extend_face_to_end_of_line. */
24526 while (end > glyph
24527 && INTEGERP ((end - 1)->object)
24528 && (end - 1)->charpos <= 0)
24529 --end;
24530 /* Scan the rest of the glyph row from the end, looking for the
24531 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24532 COVER_STRING, or whose position is between START_CHARPOS
24533 and END_CHARPOS */
24534 for (--end;
24535 end > glyph
24536 && !INTEGERP (end->object)
24537 && !EQ (end->object, cover_string)
24538 && !(BUFFERP (end->object)
24539 && (end->charpos >= start_charpos
24540 && end->charpos < end_charpos));
24541 --end)
24542 {
24543 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24544 are present at buffer positions between START_CHARPOS and
24545 END_CHARPOS, or if they come from an overlay. */
24546 if (EQ (end->object, before_string))
24547 {
24548 pos = string_buffer_position (before_string, start_charpos);
24549 if (!pos || (pos >= start_charpos && pos < end_charpos))
24550 break;
24551 }
24552 else if (EQ (end->object, after_string))
24553 {
24554 pos = string_buffer_position (after_string, end_charpos);
24555 if (!pos || (pos >= start_charpos && pos < end_charpos))
24556 break;
24557 }
24558 }
24559 /* Find the X coordinate of the last glyph to be highlighted. */
24560 for (; glyph <= end; ++glyph)
24561 x += glyph->pixel_width;
24562
24563 hlinfo->mouse_face_end_x = x;
24564 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24565 }
24566 else
24567 {
24568 /* Skip truncation and continuation glyphs near the end of the
24569 row, and also blanks and stretch glyphs inserted by
24570 extend_face_to_end_of_line. */
24571 x = r2->x;
24572 end++;
24573 while (end < glyph
24574 && INTEGERP (end->object)
24575 && end->charpos <= 0)
24576 {
24577 x += end->pixel_width;
24578 ++end;
24579 }
24580 /* Scan the rest of the glyph row from the end, looking for the
24581 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24582 COVER_STRING, or whose position is between START_CHARPOS
24583 and END_CHARPOS */
24584 for ( ;
24585 end < glyph
24586 && !INTEGERP (end->object)
24587 && !EQ (end->object, cover_string)
24588 && !(BUFFERP (end->object)
24589 && (end->charpos >= start_charpos
24590 && end->charpos < end_charpos));
24591 ++end)
24592 {
24593 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24594 are present at buffer positions between START_CHARPOS and
24595 END_CHARPOS, or if they come from an overlay. */
24596 if (EQ (end->object, before_string))
24597 {
24598 pos = string_buffer_position (before_string, start_charpos);
24599 if (!pos || (pos >= start_charpos && pos < end_charpos))
24600 break;
24601 }
24602 else if (EQ (end->object, after_string))
24603 {
24604 pos = string_buffer_position (after_string, end_charpos);
24605 if (!pos || (pos >= start_charpos && pos < end_charpos))
24606 break;
24607 }
24608 x += end->pixel_width;
24609 }
24610 hlinfo->mouse_face_end_x = x;
24611 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24612 }
24613
24614 hlinfo->mouse_face_window = window;
24615 hlinfo->mouse_face_face_id
24616 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24617 mouse_charpos + 1,
24618 !hlinfo->mouse_face_hidden, -1);
24619 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24620 }
24621
24622 /* The following function is not used anymore (replaced with
24623 mouse_face_from_string_pos), but I leave it here for the time
24624 being, in case someone would. */
24625
24626 #if 0 /* not used */
24627
24628 /* Find the position of the glyph for position POS in OBJECT in
24629 window W's current matrix, and return in *X, *Y the pixel
24630 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24631
24632 RIGHT_P non-zero means return the position of the right edge of the
24633 glyph, RIGHT_P zero means return the left edge position.
24634
24635 If no glyph for POS exists in the matrix, return the position of
24636 the glyph with the next smaller position that is in the matrix, if
24637 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24638 exists in the matrix, return the position of the glyph with the
24639 next larger position in OBJECT.
24640
24641 Value is non-zero if a glyph was found. */
24642
24643 static int
24644 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24645 int *hpos, int *vpos, int *x, int *y, int right_p)
24646 {
24647 int yb = window_text_bottom_y (w);
24648 struct glyph_row *r;
24649 struct glyph *best_glyph = NULL;
24650 struct glyph_row *best_row = NULL;
24651 int best_x = 0;
24652
24653 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24654 r->enabled_p && r->y < yb;
24655 ++r)
24656 {
24657 struct glyph *g = r->glyphs[TEXT_AREA];
24658 struct glyph *e = g + r->used[TEXT_AREA];
24659 int gx;
24660
24661 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24662 if (EQ (g->object, object))
24663 {
24664 if (g->charpos == pos)
24665 {
24666 best_glyph = g;
24667 best_x = gx;
24668 best_row = r;
24669 goto found;
24670 }
24671 else if (best_glyph == NULL
24672 || ((eabs (g->charpos - pos)
24673 < eabs (best_glyph->charpos - pos))
24674 && (right_p
24675 ? g->charpos < pos
24676 : g->charpos > pos)))
24677 {
24678 best_glyph = g;
24679 best_x = gx;
24680 best_row = r;
24681 }
24682 }
24683 }
24684
24685 found:
24686
24687 if (best_glyph)
24688 {
24689 *x = best_x;
24690 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24691
24692 if (right_p)
24693 {
24694 *x += best_glyph->pixel_width;
24695 ++*hpos;
24696 }
24697
24698 *y = best_row->y;
24699 *vpos = best_row - w->current_matrix->rows;
24700 }
24701
24702 return best_glyph != NULL;
24703 }
24704 #endif /* not used */
24705
24706 /* Find the positions of the first and the last glyphs in window W's
24707 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24708 (assumed to be a string), and return in HLINFO's mouse_face_*
24709 members the pixel and column/row coordinates of those glyphs. */
24710
24711 static void
24712 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24713 Lisp_Object object,
24714 EMACS_INT startpos, EMACS_INT endpos)
24715 {
24716 int yb = window_text_bottom_y (w);
24717 struct glyph_row *r;
24718 struct glyph *g, *e;
24719 int gx;
24720 int found = 0;
24721
24722 /* Find the glyph row with at least one position in the range
24723 [STARTPOS..ENDPOS], and the first glyph in that row whose
24724 position belongs to that range. */
24725 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24726 r->enabled_p && r->y < yb;
24727 ++r)
24728 {
24729 if (!r->reversed_p)
24730 {
24731 g = r->glyphs[TEXT_AREA];
24732 e = g + r->used[TEXT_AREA];
24733 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24734 if (EQ (g->object, object)
24735 && startpos <= g->charpos && g->charpos <= endpos)
24736 {
24737 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24738 hlinfo->mouse_face_beg_y = r->y;
24739 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24740 hlinfo->mouse_face_beg_x = gx;
24741 found = 1;
24742 break;
24743 }
24744 }
24745 else
24746 {
24747 struct glyph *g1;
24748
24749 e = r->glyphs[TEXT_AREA];
24750 g = e + r->used[TEXT_AREA];
24751 for ( ; g > e; --g)
24752 if (EQ ((g-1)->object, object)
24753 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24754 {
24755 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24756 hlinfo->mouse_face_beg_y = r->y;
24757 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24758 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24759 gx += g1->pixel_width;
24760 hlinfo->mouse_face_beg_x = gx;
24761 found = 1;
24762 break;
24763 }
24764 }
24765 if (found)
24766 break;
24767 }
24768
24769 if (!found)
24770 return;
24771
24772 /* Starting with the next row, look for the first row which does NOT
24773 include any glyphs whose positions are in the range. */
24774 for (++r; r->enabled_p && r->y < yb; ++r)
24775 {
24776 g = r->glyphs[TEXT_AREA];
24777 e = g + r->used[TEXT_AREA];
24778 found = 0;
24779 for ( ; g < e; ++g)
24780 if (EQ (g->object, object)
24781 && startpos <= g->charpos && g->charpos <= endpos)
24782 {
24783 found = 1;
24784 break;
24785 }
24786 if (!found)
24787 break;
24788 }
24789
24790 /* The highlighted region ends on the previous row. */
24791 r--;
24792
24793 /* Set the end row and its vertical pixel coordinate. */
24794 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24795 hlinfo->mouse_face_end_y = r->y;
24796
24797 /* Compute and set the end column and the end column's horizontal
24798 pixel coordinate. */
24799 if (!r->reversed_p)
24800 {
24801 g = r->glyphs[TEXT_AREA];
24802 e = g + r->used[TEXT_AREA];
24803 for ( ; e > g; --e)
24804 if (EQ ((e-1)->object, object)
24805 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24806 break;
24807 hlinfo->mouse_face_end_col = e - g;
24808
24809 for (gx = r->x; g < e; ++g)
24810 gx += g->pixel_width;
24811 hlinfo->mouse_face_end_x = gx;
24812 }
24813 else
24814 {
24815 e = r->glyphs[TEXT_AREA];
24816 g = e + r->used[TEXT_AREA];
24817 for (gx = r->x ; e < g; ++e)
24818 {
24819 if (EQ (e->object, object)
24820 && startpos <= e->charpos && e->charpos <= endpos)
24821 break;
24822 gx += e->pixel_width;
24823 }
24824 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24825 hlinfo->mouse_face_end_x = gx;
24826 }
24827 }
24828
24829 #ifdef HAVE_WINDOW_SYSTEM
24830
24831 /* See if position X, Y is within a hot-spot of an image. */
24832
24833 static int
24834 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24835 {
24836 if (!CONSP (hot_spot))
24837 return 0;
24838
24839 if (EQ (XCAR (hot_spot), Qrect))
24840 {
24841 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24842 Lisp_Object rect = XCDR (hot_spot);
24843 Lisp_Object tem;
24844 if (!CONSP (rect))
24845 return 0;
24846 if (!CONSP (XCAR (rect)))
24847 return 0;
24848 if (!CONSP (XCDR (rect)))
24849 return 0;
24850 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24851 return 0;
24852 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24853 return 0;
24854 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24855 return 0;
24856 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24857 return 0;
24858 return 1;
24859 }
24860 else if (EQ (XCAR (hot_spot), Qcircle))
24861 {
24862 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24863 Lisp_Object circ = XCDR (hot_spot);
24864 Lisp_Object lr, lx0, ly0;
24865 if (CONSP (circ)
24866 && CONSP (XCAR (circ))
24867 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24868 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24869 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24870 {
24871 double r = XFLOATINT (lr);
24872 double dx = XINT (lx0) - x;
24873 double dy = XINT (ly0) - y;
24874 return (dx * dx + dy * dy <= r * r);
24875 }
24876 }
24877 else if (EQ (XCAR (hot_spot), Qpoly))
24878 {
24879 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24880 if (VECTORP (XCDR (hot_spot)))
24881 {
24882 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24883 Lisp_Object *poly = v->contents;
24884 int n = v->header.size;
24885 int i;
24886 int inside = 0;
24887 Lisp_Object lx, ly;
24888 int x0, y0;
24889
24890 /* Need an even number of coordinates, and at least 3 edges. */
24891 if (n < 6 || n & 1)
24892 return 0;
24893
24894 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24895 If count is odd, we are inside polygon. Pixels on edges
24896 may or may not be included depending on actual geometry of the
24897 polygon. */
24898 if ((lx = poly[n-2], !INTEGERP (lx))
24899 || (ly = poly[n-1], !INTEGERP (lx)))
24900 return 0;
24901 x0 = XINT (lx), y0 = XINT (ly);
24902 for (i = 0; i < n; i += 2)
24903 {
24904 int x1 = x0, y1 = y0;
24905 if ((lx = poly[i], !INTEGERP (lx))
24906 || (ly = poly[i+1], !INTEGERP (ly)))
24907 return 0;
24908 x0 = XINT (lx), y0 = XINT (ly);
24909
24910 /* Does this segment cross the X line? */
24911 if (x0 >= x)
24912 {
24913 if (x1 >= x)
24914 continue;
24915 }
24916 else if (x1 < x)
24917 continue;
24918 if (y > y0 && y > y1)
24919 continue;
24920 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24921 inside = !inside;
24922 }
24923 return inside;
24924 }
24925 }
24926 return 0;
24927 }
24928
24929 Lisp_Object
24930 find_hot_spot (Lisp_Object map, int x, int y)
24931 {
24932 while (CONSP (map))
24933 {
24934 if (CONSP (XCAR (map))
24935 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24936 return XCAR (map);
24937 map = XCDR (map);
24938 }
24939
24940 return Qnil;
24941 }
24942
24943 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24944 3, 3, 0,
24945 doc: /* Lookup in image map MAP coordinates X and Y.
24946 An image map is an alist where each element has the format (AREA ID PLIST).
24947 An AREA is specified as either a rectangle, a circle, or a polygon:
24948 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24949 pixel coordinates of the upper left and bottom right corners.
24950 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24951 and the radius of the circle; r may be a float or integer.
24952 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24953 vector describes one corner in the polygon.
24954 Returns the alist element for the first matching AREA in MAP. */)
24955 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
24956 {
24957 if (NILP (map))
24958 return Qnil;
24959
24960 CHECK_NUMBER (x);
24961 CHECK_NUMBER (y);
24962
24963 return find_hot_spot (map, XINT (x), XINT (y));
24964 }
24965
24966
24967 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24968 static void
24969 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
24970 {
24971 /* Do not change cursor shape while dragging mouse. */
24972 if (!NILP (do_mouse_tracking))
24973 return;
24974
24975 if (!NILP (pointer))
24976 {
24977 if (EQ (pointer, Qarrow))
24978 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24979 else if (EQ (pointer, Qhand))
24980 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24981 else if (EQ (pointer, Qtext))
24982 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24983 else if (EQ (pointer, intern ("hdrag")))
24984 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24985 #ifdef HAVE_X_WINDOWS
24986 else if (EQ (pointer, intern ("vdrag")))
24987 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24988 #endif
24989 else if (EQ (pointer, intern ("hourglass")))
24990 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24991 else if (EQ (pointer, Qmodeline))
24992 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24993 else
24994 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24995 }
24996
24997 if (cursor != No_Cursor)
24998 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24999 }
25000
25001 #endif /* HAVE_WINDOW_SYSTEM */
25002
25003 /* Take proper action when mouse has moved to the mode or header line
25004 or marginal area AREA of window W, x-position X and y-position Y.
25005 X is relative to the start of the text display area of W, so the
25006 width of bitmap areas and scroll bars must be subtracted to get a
25007 position relative to the start of the mode line. */
25008
25009 static void
25010 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25011 enum window_part area)
25012 {
25013 struct window *w = XWINDOW (window);
25014 struct frame *f = XFRAME (w->frame);
25015 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25016 #ifdef HAVE_WINDOW_SYSTEM
25017 Display_Info *dpyinfo;
25018 #endif
25019 Cursor cursor = No_Cursor;
25020 Lisp_Object pointer = Qnil;
25021 int dx, dy, width, height;
25022 EMACS_INT charpos;
25023 Lisp_Object string, object = Qnil;
25024 Lisp_Object pos, help;
25025
25026 Lisp_Object mouse_face;
25027 int original_x_pixel = x;
25028 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25029 struct glyph_row *row;
25030
25031 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25032 {
25033 int x0;
25034 struct glyph *end;
25035
25036 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25037 returns them in row/column units! */
25038 string = mode_line_string (w, area, &x, &y, &charpos,
25039 &object, &dx, &dy, &width, &height);
25040
25041 row = (area == ON_MODE_LINE
25042 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25043 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25044
25045 /* Find the glyph under the mouse pointer. */
25046 if (row->mode_line_p && row->enabled_p)
25047 {
25048 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25049 end = glyph + row->used[TEXT_AREA];
25050
25051 for (x0 = original_x_pixel;
25052 glyph < end && x0 >= glyph->pixel_width;
25053 ++glyph)
25054 x0 -= glyph->pixel_width;
25055
25056 if (glyph >= end)
25057 glyph = NULL;
25058 }
25059 }
25060 else
25061 {
25062 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25063 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25064 returns them in row/column units! */
25065 string = marginal_area_string (w, area, &x, &y, &charpos,
25066 &object, &dx, &dy, &width, &height);
25067 }
25068
25069 help = Qnil;
25070
25071 #ifdef HAVE_WINDOW_SYSTEM
25072 if (IMAGEP (object))
25073 {
25074 Lisp_Object image_map, hotspot;
25075 if ((image_map = Fplist_get (XCDR (object), QCmap),
25076 !NILP (image_map))
25077 && (hotspot = find_hot_spot (image_map, dx, dy),
25078 CONSP (hotspot))
25079 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25080 {
25081 Lisp_Object plist;
25082
25083 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25084 If so, we could look for mouse-enter, mouse-leave
25085 properties in PLIST (and do something...). */
25086 hotspot = XCDR (hotspot);
25087 if (CONSP (hotspot)
25088 && (plist = XCAR (hotspot), CONSP (plist)))
25089 {
25090 pointer = Fplist_get (plist, Qpointer);
25091 if (NILP (pointer))
25092 pointer = Qhand;
25093 help = Fplist_get (plist, Qhelp_echo);
25094 if (!NILP (help))
25095 {
25096 help_echo_string = help;
25097 /* Is this correct? ++kfs */
25098 XSETWINDOW (help_echo_window, w);
25099 help_echo_object = w->buffer;
25100 help_echo_pos = charpos;
25101 }
25102 }
25103 }
25104 if (NILP (pointer))
25105 pointer = Fplist_get (XCDR (object), QCpointer);
25106 }
25107 #endif /* HAVE_WINDOW_SYSTEM */
25108
25109 if (STRINGP (string))
25110 {
25111 pos = make_number (charpos);
25112 /* If we're on a string with `help-echo' text property, arrange
25113 for the help to be displayed. This is done by setting the
25114 global variable help_echo_string to the help string. */
25115 if (NILP (help))
25116 {
25117 help = Fget_text_property (pos, Qhelp_echo, string);
25118 if (!NILP (help))
25119 {
25120 help_echo_string = help;
25121 XSETWINDOW (help_echo_window, w);
25122 help_echo_object = string;
25123 help_echo_pos = charpos;
25124 }
25125 }
25126
25127 #ifdef HAVE_WINDOW_SYSTEM
25128 if (FRAME_WINDOW_P (f))
25129 {
25130 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25131 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25132 if (NILP (pointer))
25133 pointer = Fget_text_property (pos, Qpointer, string);
25134
25135 /* Change the mouse pointer according to what is under X/Y. */
25136 if (NILP (pointer)
25137 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25138 {
25139 Lisp_Object map;
25140 map = Fget_text_property (pos, Qlocal_map, string);
25141 if (!KEYMAPP (map))
25142 map = Fget_text_property (pos, Qkeymap, string);
25143 if (!KEYMAPP (map))
25144 cursor = dpyinfo->vertical_scroll_bar_cursor;
25145 }
25146 }
25147 #endif
25148
25149 /* Change the mouse face according to what is under X/Y. */
25150 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25151 if (!NILP (mouse_face)
25152 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25153 && glyph)
25154 {
25155 Lisp_Object b, e;
25156
25157 struct glyph * tmp_glyph;
25158
25159 int gpos;
25160 int gseq_length;
25161 int total_pixel_width;
25162 EMACS_INT begpos, endpos, ignore;
25163
25164 int vpos, hpos;
25165
25166 b = Fprevious_single_property_change (make_number (charpos + 1),
25167 Qmouse_face, string, Qnil);
25168 if (NILP (b))
25169 begpos = 0;
25170 else
25171 begpos = XINT (b);
25172
25173 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25174 if (NILP (e))
25175 endpos = SCHARS (string);
25176 else
25177 endpos = XINT (e);
25178
25179 /* Calculate the glyph position GPOS of GLYPH in the
25180 displayed string, relative to the beginning of the
25181 highlighted part of the string.
25182
25183 Note: GPOS is different from CHARPOS. CHARPOS is the
25184 position of GLYPH in the internal string object. A mode
25185 line string format has structures which are converted to
25186 a flattened string by the Emacs Lisp interpreter. The
25187 internal string is an element of those structures. The
25188 displayed string is the flattened string. */
25189 tmp_glyph = row_start_glyph;
25190 while (tmp_glyph < glyph
25191 && (!(EQ (tmp_glyph->object, glyph->object)
25192 && begpos <= tmp_glyph->charpos
25193 && tmp_glyph->charpos < endpos)))
25194 tmp_glyph++;
25195 gpos = glyph - tmp_glyph;
25196
25197 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25198 the highlighted part of the displayed string to which
25199 GLYPH belongs. Note: GSEQ_LENGTH is different from
25200 SCHARS (STRING), because the latter returns the length of
25201 the internal string. */
25202 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25203 tmp_glyph > glyph
25204 && (!(EQ (tmp_glyph->object, glyph->object)
25205 && begpos <= tmp_glyph->charpos
25206 && tmp_glyph->charpos < endpos));
25207 tmp_glyph--)
25208 ;
25209 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25210
25211 /* Calculate the total pixel width of all the glyphs between
25212 the beginning of the highlighted area and GLYPH. */
25213 total_pixel_width = 0;
25214 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25215 total_pixel_width += tmp_glyph->pixel_width;
25216
25217 /* Pre calculation of re-rendering position. Note: X is in
25218 column units here, after the call to mode_line_string or
25219 marginal_area_string. */
25220 hpos = x - gpos;
25221 vpos = (area == ON_MODE_LINE
25222 ? (w->current_matrix)->nrows - 1
25223 : 0);
25224
25225 /* If GLYPH's position is included in the region that is
25226 already drawn in mouse face, we have nothing to do. */
25227 if ( EQ (window, hlinfo->mouse_face_window)
25228 && (!row->reversed_p
25229 ? (hlinfo->mouse_face_beg_col <= hpos
25230 && hpos < hlinfo->mouse_face_end_col)
25231 /* In R2L rows we swap BEG and END, see below. */
25232 : (hlinfo->mouse_face_end_col <= hpos
25233 && hpos < hlinfo->mouse_face_beg_col))
25234 && hlinfo->mouse_face_beg_row == vpos )
25235 return;
25236
25237 if (clear_mouse_face (hlinfo))
25238 cursor = No_Cursor;
25239
25240 if (!row->reversed_p)
25241 {
25242 hlinfo->mouse_face_beg_col = hpos;
25243 hlinfo->mouse_face_beg_x = original_x_pixel
25244 - (total_pixel_width + dx);
25245 hlinfo->mouse_face_end_col = hpos + gseq_length;
25246 hlinfo->mouse_face_end_x = 0;
25247 }
25248 else
25249 {
25250 /* In R2L rows, show_mouse_face expects BEG and END
25251 coordinates to be swapped. */
25252 hlinfo->mouse_face_end_col = hpos;
25253 hlinfo->mouse_face_end_x = original_x_pixel
25254 - (total_pixel_width + dx);
25255 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25256 hlinfo->mouse_face_beg_x = 0;
25257 }
25258
25259 hlinfo->mouse_face_beg_row = vpos;
25260 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25261 hlinfo->mouse_face_beg_y = 0;
25262 hlinfo->mouse_face_end_y = 0;
25263 hlinfo->mouse_face_past_end = 0;
25264 hlinfo->mouse_face_window = window;
25265
25266 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25267 charpos,
25268 0, 0, 0,
25269 &ignore,
25270 glyph->face_id,
25271 1);
25272 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25273
25274 if (NILP (pointer))
25275 pointer = Qhand;
25276 }
25277 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25278 clear_mouse_face (hlinfo);
25279 }
25280 #ifdef HAVE_WINDOW_SYSTEM
25281 if (FRAME_WINDOW_P (f))
25282 define_frame_cursor1 (f, cursor, pointer);
25283 #endif
25284 }
25285
25286
25287 /* EXPORT:
25288 Take proper action when the mouse has moved to position X, Y on
25289 frame F as regards highlighting characters that have mouse-face
25290 properties. Also de-highlighting chars where the mouse was before.
25291 X and Y can be negative or out of range. */
25292
25293 void
25294 note_mouse_highlight (struct frame *f, int x, int y)
25295 {
25296 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25297 enum window_part part;
25298 Lisp_Object window;
25299 struct window *w;
25300 Cursor cursor = No_Cursor;
25301 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25302 struct buffer *b;
25303
25304 /* When a menu is active, don't highlight because this looks odd. */
25305 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25306 if (popup_activated ())
25307 return;
25308 #endif
25309
25310 if (NILP (Vmouse_highlight)
25311 || !f->glyphs_initialized_p
25312 || f->pointer_invisible)
25313 return;
25314
25315 hlinfo->mouse_face_mouse_x = x;
25316 hlinfo->mouse_face_mouse_y = y;
25317 hlinfo->mouse_face_mouse_frame = f;
25318
25319 if (hlinfo->mouse_face_defer)
25320 return;
25321
25322 if (gc_in_progress)
25323 {
25324 hlinfo->mouse_face_deferred_gc = 1;
25325 return;
25326 }
25327
25328 /* Which window is that in? */
25329 window = window_from_coordinates (f, x, y, &part, 1);
25330
25331 /* If we were displaying active text in another window, clear that.
25332 Also clear if we move out of text area in same window. */
25333 if (! EQ (window, hlinfo->mouse_face_window)
25334 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25335 && !NILP (hlinfo->mouse_face_window)))
25336 clear_mouse_face (hlinfo);
25337
25338 /* Not on a window -> return. */
25339 if (!WINDOWP (window))
25340 return;
25341
25342 /* Reset help_echo_string. It will get recomputed below. */
25343 help_echo_string = Qnil;
25344
25345 /* Convert to window-relative pixel coordinates. */
25346 w = XWINDOW (window);
25347 frame_to_window_pixel_xy (w, &x, &y);
25348
25349 #ifdef HAVE_WINDOW_SYSTEM
25350 /* Handle tool-bar window differently since it doesn't display a
25351 buffer. */
25352 if (EQ (window, f->tool_bar_window))
25353 {
25354 note_tool_bar_highlight (f, x, y);
25355 return;
25356 }
25357 #endif
25358
25359 /* Mouse is on the mode, header line or margin? */
25360 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25361 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25362 {
25363 note_mode_line_or_margin_highlight (window, x, y, part);
25364 return;
25365 }
25366
25367 #ifdef HAVE_WINDOW_SYSTEM
25368 if (part == ON_VERTICAL_BORDER)
25369 {
25370 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25371 help_echo_string = build_string ("drag-mouse-1: resize");
25372 }
25373 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25374 || part == ON_SCROLL_BAR)
25375 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25376 else
25377 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25378 #endif
25379
25380 /* Are we in a window whose display is up to date?
25381 And verify the buffer's text has not changed. */
25382 b = XBUFFER (w->buffer);
25383 if (part == ON_TEXT
25384 && EQ (w->window_end_valid, w->buffer)
25385 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25386 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25387 {
25388 int hpos, vpos, i, dx, dy, area;
25389 EMACS_INT pos;
25390 struct glyph *glyph;
25391 Lisp_Object object;
25392 Lisp_Object mouse_face = Qnil, position;
25393 Lisp_Object *overlay_vec = NULL;
25394 int noverlays;
25395 struct buffer *obuf;
25396 EMACS_INT obegv, ozv;
25397 int same_region;
25398
25399 /* Find the glyph under X/Y. */
25400 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25401
25402 #ifdef HAVE_WINDOW_SYSTEM
25403 /* Look for :pointer property on image. */
25404 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25405 {
25406 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25407 if (img != NULL && IMAGEP (img->spec))
25408 {
25409 Lisp_Object image_map, hotspot;
25410 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25411 !NILP (image_map))
25412 && (hotspot = find_hot_spot (image_map,
25413 glyph->slice.img.x + dx,
25414 glyph->slice.img.y + dy),
25415 CONSP (hotspot))
25416 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25417 {
25418 Lisp_Object plist;
25419
25420 /* Could check XCAR (hotspot) to see if we enter/leave
25421 this hot-spot.
25422 If so, we could look for mouse-enter, mouse-leave
25423 properties in PLIST (and do something...). */
25424 hotspot = XCDR (hotspot);
25425 if (CONSP (hotspot)
25426 && (plist = XCAR (hotspot), CONSP (plist)))
25427 {
25428 pointer = Fplist_get (plist, Qpointer);
25429 if (NILP (pointer))
25430 pointer = Qhand;
25431 help_echo_string = Fplist_get (plist, Qhelp_echo);
25432 if (!NILP (help_echo_string))
25433 {
25434 help_echo_window = window;
25435 help_echo_object = glyph->object;
25436 help_echo_pos = glyph->charpos;
25437 }
25438 }
25439 }
25440 if (NILP (pointer))
25441 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25442 }
25443 }
25444 #endif /* HAVE_WINDOW_SYSTEM */
25445
25446 /* Clear mouse face if X/Y not over text. */
25447 if (glyph == NULL
25448 || area != TEXT_AREA
25449 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25450 /* Glyph's OBJECT is an integer for glyphs inserted by the
25451 display engine for its internal purposes, like truncation
25452 and continuation glyphs and blanks beyond the end of
25453 line's text on text terminals. If we are over such a
25454 glyph, we are not over any text. */
25455 || INTEGERP (glyph->object)
25456 /* R2L rows have a stretch glyph at their front, which
25457 stands for no text, whereas L2R rows have no glyphs at
25458 all beyond the end of text. Treat such stretch glyphs
25459 like we do with NULL glyphs in L2R rows. */
25460 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25461 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25462 && glyph->type == STRETCH_GLYPH
25463 && glyph->avoid_cursor_p))
25464 {
25465 if (clear_mouse_face (hlinfo))
25466 cursor = No_Cursor;
25467 #ifdef HAVE_WINDOW_SYSTEM
25468 if (FRAME_WINDOW_P (f) && NILP (pointer))
25469 {
25470 if (area != TEXT_AREA)
25471 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25472 else
25473 pointer = Vvoid_text_area_pointer;
25474 }
25475 #endif
25476 goto set_cursor;
25477 }
25478
25479 pos = glyph->charpos;
25480 object = glyph->object;
25481 if (!STRINGP (object) && !BUFFERP (object))
25482 goto set_cursor;
25483
25484 /* If we get an out-of-range value, return now; avoid an error. */
25485 if (BUFFERP (object) && pos > BUF_Z (b))
25486 goto set_cursor;
25487
25488 /* Make the window's buffer temporarily current for
25489 overlays_at and compute_char_face. */
25490 obuf = current_buffer;
25491 current_buffer = b;
25492 obegv = BEGV;
25493 ozv = ZV;
25494 BEGV = BEG;
25495 ZV = Z;
25496
25497 /* Is this char mouse-active or does it have help-echo? */
25498 position = make_number (pos);
25499
25500 if (BUFFERP (object))
25501 {
25502 /* Put all the overlays we want in a vector in overlay_vec. */
25503 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25504 /* Sort overlays into increasing priority order. */
25505 noverlays = sort_overlays (overlay_vec, noverlays, w);
25506 }
25507 else
25508 noverlays = 0;
25509
25510 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25511
25512 if (same_region)
25513 cursor = No_Cursor;
25514
25515 /* Check mouse-face highlighting. */
25516 if (! same_region
25517 /* If there exists an overlay with mouse-face overlapping
25518 the one we are currently highlighting, we have to
25519 check if we enter the overlapping overlay, and then
25520 highlight only that. */
25521 || (OVERLAYP (hlinfo->mouse_face_overlay)
25522 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25523 {
25524 /* Find the highest priority overlay with a mouse-face. */
25525 Lisp_Object overlay = Qnil;
25526 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25527 {
25528 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25529 if (!NILP (mouse_face))
25530 overlay = overlay_vec[i];
25531 }
25532
25533 /* If we're highlighting the same overlay as before, there's
25534 no need to do that again. */
25535 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25536 goto check_help_echo;
25537 hlinfo->mouse_face_overlay = overlay;
25538
25539 /* Clear the display of the old active region, if any. */
25540 if (clear_mouse_face (hlinfo))
25541 cursor = No_Cursor;
25542
25543 /* If no overlay applies, get a text property. */
25544 if (NILP (overlay))
25545 mouse_face = Fget_text_property (position, Qmouse_face, object);
25546
25547 /* Next, compute the bounds of the mouse highlighting and
25548 display it. */
25549 if (!NILP (mouse_face) && STRINGP (object))
25550 {
25551 /* The mouse-highlighting comes from a display string
25552 with a mouse-face. */
25553 Lisp_Object s, e;
25554 EMACS_INT ignore;
25555
25556 s = Fprevious_single_property_change
25557 (make_number (pos + 1), Qmouse_face, object, Qnil);
25558 e = Fnext_single_property_change
25559 (position, Qmouse_face, object, Qnil);
25560 if (NILP (s))
25561 s = make_number (0);
25562 if (NILP (e))
25563 e = make_number (SCHARS (object) - 1);
25564 mouse_face_from_string_pos (w, hlinfo, object,
25565 XINT (s), XINT (e));
25566 hlinfo->mouse_face_past_end = 0;
25567 hlinfo->mouse_face_window = window;
25568 hlinfo->mouse_face_face_id
25569 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25570 glyph->face_id, 1);
25571 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25572 cursor = No_Cursor;
25573 }
25574 else
25575 {
25576 /* The mouse-highlighting, if any, comes from an overlay
25577 or text property in the buffer. */
25578 Lisp_Object buffer IF_LINT (= Qnil);
25579 Lisp_Object cover_string IF_LINT (= Qnil);
25580
25581 if (STRINGP (object))
25582 {
25583 /* If we are on a display string with no mouse-face,
25584 check if the text under it has one. */
25585 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25586 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25587 pos = string_buffer_position (object, start);
25588 if (pos > 0)
25589 {
25590 mouse_face = get_char_property_and_overlay
25591 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25592 buffer = w->buffer;
25593 cover_string = object;
25594 }
25595 }
25596 else
25597 {
25598 buffer = object;
25599 cover_string = Qnil;
25600 }
25601
25602 if (!NILP (mouse_face))
25603 {
25604 Lisp_Object before, after;
25605 Lisp_Object before_string, after_string;
25606 /* To correctly find the limits of mouse highlight
25607 in a bidi-reordered buffer, we must not use the
25608 optimization of limiting the search in
25609 previous-single-property-change and
25610 next-single-property-change, because
25611 rows_from_pos_range needs the real start and end
25612 positions to DTRT in this case. That's because
25613 the first row visible in a window does not
25614 necessarily display the character whose position
25615 is the smallest. */
25616 Lisp_Object lim1 =
25617 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
25618 ? Fmarker_position (w->start)
25619 : Qnil;
25620 Lisp_Object lim2 =
25621 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
25622 ? make_number (BUF_Z (XBUFFER (buffer))
25623 - XFASTINT (w->window_end_pos))
25624 : Qnil;
25625
25626 if (NILP (overlay))
25627 {
25628 /* Handle the text property case. */
25629 before = Fprevious_single_property_change
25630 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25631 after = Fnext_single_property_change
25632 (make_number (pos), Qmouse_face, buffer, lim2);
25633 before_string = after_string = Qnil;
25634 }
25635 else
25636 {
25637 /* Handle the overlay case. */
25638 before = Foverlay_start (overlay);
25639 after = Foverlay_end (overlay);
25640 before_string = Foverlay_get (overlay, Qbefore_string);
25641 after_string = Foverlay_get (overlay, Qafter_string);
25642
25643 if (!STRINGP (before_string)) before_string = Qnil;
25644 if (!STRINGP (after_string)) after_string = Qnil;
25645 }
25646
25647 mouse_face_from_buffer_pos (window, hlinfo, pos,
25648 XFASTINT (before),
25649 XFASTINT (after),
25650 before_string, after_string,
25651 cover_string);
25652 cursor = No_Cursor;
25653 }
25654 }
25655 }
25656
25657 check_help_echo:
25658
25659 /* Look for a `help-echo' property. */
25660 if (NILP (help_echo_string)) {
25661 Lisp_Object help, overlay;
25662
25663 /* Check overlays first. */
25664 help = overlay = Qnil;
25665 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25666 {
25667 overlay = overlay_vec[i];
25668 help = Foverlay_get (overlay, Qhelp_echo);
25669 }
25670
25671 if (!NILP (help))
25672 {
25673 help_echo_string = help;
25674 help_echo_window = window;
25675 help_echo_object = overlay;
25676 help_echo_pos = pos;
25677 }
25678 else
25679 {
25680 Lisp_Object obj = glyph->object;
25681 EMACS_INT charpos = glyph->charpos;
25682
25683 /* Try text properties. */
25684 if (STRINGP (obj)
25685 && charpos >= 0
25686 && charpos < SCHARS (obj))
25687 {
25688 help = Fget_text_property (make_number (charpos),
25689 Qhelp_echo, obj);
25690 if (NILP (help))
25691 {
25692 /* If the string itself doesn't specify a help-echo,
25693 see if the buffer text ``under'' it does. */
25694 struct glyph_row *r
25695 = MATRIX_ROW (w->current_matrix, vpos);
25696 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25697 EMACS_INT p = string_buffer_position (obj, start);
25698 if (p > 0)
25699 {
25700 help = Fget_char_property (make_number (p),
25701 Qhelp_echo, w->buffer);
25702 if (!NILP (help))
25703 {
25704 charpos = p;
25705 obj = w->buffer;
25706 }
25707 }
25708 }
25709 }
25710 else if (BUFFERP (obj)
25711 && charpos >= BEGV
25712 && charpos < ZV)
25713 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25714 obj);
25715
25716 if (!NILP (help))
25717 {
25718 help_echo_string = help;
25719 help_echo_window = window;
25720 help_echo_object = obj;
25721 help_echo_pos = charpos;
25722 }
25723 }
25724 }
25725
25726 #ifdef HAVE_WINDOW_SYSTEM
25727 /* Look for a `pointer' property. */
25728 if (FRAME_WINDOW_P (f) && NILP (pointer))
25729 {
25730 /* Check overlays first. */
25731 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25732 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25733
25734 if (NILP (pointer))
25735 {
25736 Lisp_Object obj = glyph->object;
25737 EMACS_INT charpos = glyph->charpos;
25738
25739 /* Try text properties. */
25740 if (STRINGP (obj)
25741 && charpos >= 0
25742 && charpos < SCHARS (obj))
25743 {
25744 pointer = Fget_text_property (make_number (charpos),
25745 Qpointer, obj);
25746 if (NILP (pointer))
25747 {
25748 /* If the string itself doesn't specify a pointer,
25749 see if the buffer text ``under'' it does. */
25750 struct glyph_row *r
25751 = MATRIX_ROW (w->current_matrix, vpos);
25752 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25753 EMACS_INT p = string_buffer_position (obj, start);
25754 if (p > 0)
25755 pointer = Fget_char_property (make_number (p),
25756 Qpointer, w->buffer);
25757 }
25758 }
25759 else if (BUFFERP (obj)
25760 && charpos >= BEGV
25761 && charpos < ZV)
25762 pointer = Fget_text_property (make_number (charpos),
25763 Qpointer, obj);
25764 }
25765 }
25766 #endif /* HAVE_WINDOW_SYSTEM */
25767
25768 BEGV = obegv;
25769 ZV = ozv;
25770 current_buffer = obuf;
25771 }
25772
25773 set_cursor:
25774
25775 #ifdef HAVE_WINDOW_SYSTEM
25776 if (FRAME_WINDOW_P (f))
25777 define_frame_cursor1 (f, cursor, pointer);
25778 #else
25779 /* This is here to prevent a compiler error, about "label at end of
25780 compound statement". */
25781 return;
25782 #endif
25783 }
25784
25785
25786 /* EXPORT for RIF:
25787 Clear any mouse-face on window W. This function is part of the
25788 redisplay interface, and is called from try_window_id and similar
25789 functions to ensure the mouse-highlight is off. */
25790
25791 void
25792 x_clear_window_mouse_face (struct window *w)
25793 {
25794 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25795 Lisp_Object window;
25796
25797 BLOCK_INPUT;
25798 XSETWINDOW (window, w);
25799 if (EQ (window, hlinfo->mouse_face_window))
25800 clear_mouse_face (hlinfo);
25801 UNBLOCK_INPUT;
25802 }
25803
25804
25805 /* EXPORT:
25806 Just discard the mouse face information for frame F, if any.
25807 This is used when the size of F is changed. */
25808
25809 void
25810 cancel_mouse_face (struct frame *f)
25811 {
25812 Lisp_Object window;
25813 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25814
25815 window = hlinfo->mouse_face_window;
25816 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25817 {
25818 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25819 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25820 hlinfo->mouse_face_window = Qnil;
25821 }
25822 }
25823
25824
25825 \f
25826 /***********************************************************************
25827 Exposure Events
25828 ***********************************************************************/
25829
25830 #ifdef HAVE_WINDOW_SYSTEM
25831
25832 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25833 which intersects rectangle R. R is in window-relative coordinates. */
25834
25835 static void
25836 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25837 enum glyph_row_area area)
25838 {
25839 struct glyph *first = row->glyphs[area];
25840 struct glyph *end = row->glyphs[area] + row->used[area];
25841 struct glyph *last;
25842 int first_x, start_x, x;
25843
25844 if (area == TEXT_AREA && row->fill_line_p)
25845 /* If row extends face to end of line write the whole line. */
25846 draw_glyphs (w, 0, row, area,
25847 0, row->used[area],
25848 DRAW_NORMAL_TEXT, 0);
25849 else
25850 {
25851 /* Set START_X to the window-relative start position for drawing glyphs of
25852 AREA. The first glyph of the text area can be partially visible.
25853 The first glyphs of other areas cannot. */
25854 start_x = window_box_left_offset (w, area);
25855 x = start_x;
25856 if (area == TEXT_AREA)
25857 x += row->x;
25858
25859 /* Find the first glyph that must be redrawn. */
25860 while (first < end
25861 && x + first->pixel_width < r->x)
25862 {
25863 x += first->pixel_width;
25864 ++first;
25865 }
25866
25867 /* Find the last one. */
25868 last = first;
25869 first_x = x;
25870 while (last < end
25871 && x < r->x + r->width)
25872 {
25873 x += last->pixel_width;
25874 ++last;
25875 }
25876
25877 /* Repaint. */
25878 if (last > first)
25879 draw_glyphs (w, first_x - start_x, row, area,
25880 first - row->glyphs[area], last - row->glyphs[area],
25881 DRAW_NORMAL_TEXT, 0);
25882 }
25883 }
25884
25885
25886 /* Redraw the parts of the glyph row ROW on window W intersecting
25887 rectangle R. R is in window-relative coordinates. Value is
25888 non-zero if mouse-face was overwritten. */
25889
25890 static int
25891 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25892 {
25893 xassert (row->enabled_p);
25894
25895 if (row->mode_line_p || w->pseudo_window_p)
25896 draw_glyphs (w, 0, row, TEXT_AREA,
25897 0, row->used[TEXT_AREA],
25898 DRAW_NORMAL_TEXT, 0);
25899 else
25900 {
25901 if (row->used[LEFT_MARGIN_AREA])
25902 expose_area (w, row, r, LEFT_MARGIN_AREA);
25903 if (row->used[TEXT_AREA])
25904 expose_area (w, row, r, TEXT_AREA);
25905 if (row->used[RIGHT_MARGIN_AREA])
25906 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25907 draw_row_fringe_bitmaps (w, row);
25908 }
25909
25910 return row->mouse_face_p;
25911 }
25912
25913
25914 /* Redraw those parts of glyphs rows during expose event handling that
25915 overlap other rows. Redrawing of an exposed line writes over parts
25916 of lines overlapping that exposed line; this function fixes that.
25917
25918 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25919 row in W's current matrix that is exposed and overlaps other rows.
25920 LAST_OVERLAPPING_ROW is the last such row. */
25921
25922 static void
25923 expose_overlaps (struct window *w,
25924 struct glyph_row *first_overlapping_row,
25925 struct glyph_row *last_overlapping_row,
25926 XRectangle *r)
25927 {
25928 struct glyph_row *row;
25929
25930 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25931 if (row->overlapping_p)
25932 {
25933 xassert (row->enabled_p && !row->mode_line_p);
25934
25935 row->clip = r;
25936 if (row->used[LEFT_MARGIN_AREA])
25937 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25938
25939 if (row->used[TEXT_AREA])
25940 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25941
25942 if (row->used[RIGHT_MARGIN_AREA])
25943 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
25944 row->clip = NULL;
25945 }
25946 }
25947
25948
25949 /* Return non-zero if W's cursor intersects rectangle R. */
25950
25951 static int
25952 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
25953 {
25954 XRectangle cr, result;
25955 struct glyph *cursor_glyph;
25956 struct glyph_row *row;
25957
25958 if (w->phys_cursor.vpos >= 0
25959 && w->phys_cursor.vpos < w->current_matrix->nrows
25960 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
25961 row->enabled_p)
25962 && row->cursor_in_fringe_p)
25963 {
25964 /* Cursor is in the fringe. */
25965 cr.x = window_box_right_offset (w,
25966 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
25967 ? RIGHT_MARGIN_AREA
25968 : TEXT_AREA));
25969 cr.y = row->y;
25970 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25971 cr.height = row->height;
25972 return x_intersect_rectangles (&cr, r, &result);
25973 }
25974
25975 cursor_glyph = get_phys_cursor_glyph (w);
25976 if (cursor_glyph)
25977 {
25978 /* r is relative to W's box, but w->phys_cursor.x is relative
25979 to left edge of W's TEXT area. Adjust it. */
25980 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25981 cr.y = w->phys_cursor.y;
25982 cr.width = cursor_glyph->pixel_width;
25983 cr.height = w->phys_cursor_height;
25984 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25985 I assume the effect is the same -- and this is portable. */
25986 return x_intersect_rectangles (&cr, r, &result);
25987 }
25988 /* If we don't understand the format, pretend we're not in the hot-spot. */
25989 return 0;
25990 }
25991
25992
25993 /* EXPORT:
25994 Draw a vertical window border to the right of window W if W doesn't
25995 have vertical scroll bars. */
25996
25997 void
25998 x_draw_vertical_border (struct window *w)
25999 {
26000 struct frame *f = XFRAME (WINDOW_FRAME (w));
26001
26002 /* We could do better, if we knew what type of scroll-bar the adjacent
26003 windows (on either side) have... But we don't :-(
26004 However, I think this works ok. ++KFS 2003-04-25 */
26005
26006 /* Redraw borders between horizontally adjacent windows. Don't
26007 do it for frames with vertical scroll bars because either the
26008 right scroll bar of a window, or the left scroll bar of its
26009 neighbor will suffice as a border. */
26010 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26011 return;
26012
26013 if (!WINDOW_RIGHTMOST_P (w)
26014 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26015 {
26016 int x0, x1, y0, y1;
26017
26018 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26019 y1 -= 1;
26020
26021 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26022 x1 -= 1;
26023
26024 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26025 }
26026 else if (!WINDOW_LEFTMOST_P (w)
26027 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26028 {
26029 int x0, x1, y0, y1;
26030
26031 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26032 y1 -= 1;
26033
26034 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26035 x0 -= 1;
26036
26037 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26038 }
26039 }
26040
26041
26042 /* Redraw the part of window W intersection rectangle FR. Pixel
26043 coordinates in FR are frame-relative. Call this function with
26044 input blocked. Value is non-zero if the exposure overwrites
26045 mouse-face. */
26046
26047 static int
26048 expose_window (struct window *w, XRectangle *fr)
26049 {
26050 struct frame *f = XFRAME (w->frame);
26051 XRectangle wr, r;
26052 int mouse_face_overwritten_p = 0;
26053
26054 /* If window is not yet fully initialized, do nothing. This can
26055 happen when toolkit scroll bars are used and a window is split.
26056 Reconfiguring the scroll bar will generate an expose for a newly
26057 created window. */
26058 if (w->current_matrix == NULL)
26059 return 0;
26060
26061 /* When we're currently updating the window, display and current
26062 matrix usually don't agree. Arrange for a thorough display
26063 later. */
26064 if (w == updated_window)
26065 {
26066 SET_FRAME_GARBAGED (f);
26067 return 0;
26068 }
26069
26070 /* Frame-relative pixel rectangle of W. */
26071 wr.x = WINDOW_LEFT_EDGE_X (w);
26072 wr.y = WINDOW_TOP_EDGE_Y (w);
26073 wr.width = WINDOW_TOTAL_WIDTH (w);
26074 wr.height = WINDOW_TOTAL_HEIGHT (w);
26075
26076 if (x_intersect_rectangles (fr, &wr, &r))
26077 {
26078 int yb = window_text_bottom_y (w);
26079 struct glyph_row *row;
26080 int cursor_cleared_p;
26081 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26082
26083 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26084 r.x, r.y, r.width, r.height));
26085
26086 /* Convert to window coordinates. */
26087 r.x -= WINDOW_LEFT_EDGE_X (w);
26088 r.y -= WINDOW_TOP_EDGE_Y (w);
26089
26090 /* Turn off the cursor. */
26091 if (!w->pseudo_window_p
26092 && phys_cursor_in_rect_p (w, &r))
26093 {
26094 x_clear_cursor (w);
26095 cursor_cleared_p = 1;
26096 }
26097 else
26098 cursor_cleared_p = 0;
26099
26100 /* Update lines intersecting rectangle R. */
26101 first_overlapping_row = last_overlapping_row = NULL;
26102 for (row = w->current_matrix->rows;
26103 row->enabled_p;
26104 ++row)
26105 {
26106 int y0 = row->y;
26107 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26108
26109 if ((y0 >= r.y && y0 < r.y + r.height)
26110 || (y1 > r.y && y1 < r.y + r.height)
26111 || (r.y >= y0 && r.y < y1)
26112 || (r.y + r.height > y0 && r.y + r.height < y1))
26113 {
26114 /* A header line may be overlapping, but there is no need
26115 to fix overlapping areas for them. KFS 2005-02-12 */
26116 if (row->overlapping_p && !row->mode_line_p)
26117 {
26118 if (first_overlapping_row == NULL)
26119 first_overlapping_row = row;
26120 last_overlapping_row = row;
26121 }
26122
26123 row->clip = fr;
26124 if (expose_line (w, row, &r))
26125 mouse_face_overwritten_p = 1;
26126 row->clip = NULL;
26127 }
26128 else if (row->overlapping_p)
26129 {
26130 /* We must redraw a row overlapping the exposed area. */
26131 if (y0 < r.y
26132 ? y0 + row->phys_height > r.y
26133 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26134 {
26135 if (first_overlapping_row == NULL)
26136 first_overlapping_row = row;
26137 last_overlapping_row = row;
26138 }
26139 }
26140
26141 if (y1 >= yb)
26142 break;
26143 }
26144
26145 /* Display the mode line if there is one. */
26146 if (WINDOW_WANTS_MODELINE_P (w)
26147 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26148 row->enabled_p)
26149 && row->y < r.y + r.height)
26150 {
26151 if (expose_line (w, row, &r))
26152 mouse_face_overwritten_p = 1;
26153 }
26154
26155 if (!w->pseudo_window_p)
26156 {
26157 /* Fix the display of overlapping rows. */
26158 if (first_overlapping_row)
26159 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26160 fr);
26161
26162 /* Draw border between windows. */
26163 x_draw_vertical_border (w);
26164
26165 /* Turn the cursor on again. */
26166 if (cursor_cleared_p)
26167 update_window_cursor (w, 1);
26168 }
26169 }
26170
26171 return mouse_face_overwritten_p;
26172 }
26173
26174
26175
26176 /* Redraw (parts) of all windows in the window tree rooted at W that
26177 intersect R. R contains frame pixel coordinates. Value is
26178 non-zero if the exposure overwrites mouse-face. */
26179
26180 static int
26181 expose_window_tree (struct window *w, XRectangle *r)
26182 {
26183 struct frame *f = XFRAME (w->frame);
26184 int mouse_face_overwritten_p = 0;
26185
26186 while (w && !FRAME_GARBAGED_P (f))
26187 {
26188 if (!NILP (w->hchild))
26189 mouse_face_overwritten_p
26190 |= expose_window_tree (XWINDOW (w->hchild), r);
26191 else if (!NILP (w->vchild))
26192 mouse_face_overwritten_p
26193 |= expose_window_tree (XWINDOW (w->vchild), r);
26194 else
26195 mouse_face_overwritten_p |= expose_window (w, r);
26196
26197 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26198 }
26199
26200 return mouse_face_overwritten_p;
26201 }
26202
26203
26204 /* EXPORT:
26205 Redisplay an exposed area of frame F. X and Y are the upper-left
26206 corner of the exposed rectangle. W and H are width and height of
26207 the exposed area. All are pixel values. W or H zero means redraw
26208 the entire frame. */
26209
26210 void
26211 expose_frame (struct frame *f, int x, int y, int w, int h)
26212 {
26213 XRectangle r;
26214 int mouse_face_overwritten_p = 0;
26215
26216 TRACE ((stderr, "expose_frame "));
26217
26218 /* No need to redraw if frame will be redrawn soon. */
26219 if (FRAME_GARBAGED_P (f))
26220 {
26221 TRACE ((stderr, " garbaged\n"));
26222 return;
26223 }
26224
26225 /* If basic faces haven't been realized yet, there is no point in
26226 trying to redraw anything. This can happen when we get an expose
26227 event while Emacs is starting, e.g. by moving another window. */
26228 if (FRAME_FACE_CACHE (f) == NULL
26229 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26230 {
26231 TRACE ((stderr, " no faces\n"));
26232 return;
26233 }
26234
26235 if (w == 0 || h == 0)
26236 {
26237 r.x = r.y = 0;
26238 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26239 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26240 }
26241 else
26242 {
26243 r.x = x;
26244 r.y = y;
26245 r.width = w;
26246 r.height = h;
26247 }
26248
26249 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26250 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26251
26252 if (WINDOWP (f->tool_bar_window))
26253 mouse_face_overwritten_p
26254 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26255
26256 #ifdef HAVE_X_WINDOWS
26257 #ifndef MSDOS
26258 #ifndef USE_X_TOOLKIT
26259 if (WINDOWP (f->menu_bar_window))
26260 mouse_face_overwritten_p
26261 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26262 #endif /* not USE_X_TOOLKIT */
26263 #endif
26264 #endif
26265
26266 /* Some window managers support a focus-follows-mouse style with
26267 delayed raising of frames. Imagine a partially obscured frame,
26268 and moving the mouse into partially obscured mouse-face on that
26269 frame. The visible part of the mouse-face will be highlighted,
26270 then the WM raises the obscured frame. With at least one WM, KDE
26271 2.1, Emacs is not getting any event for the raising of the frame
26272 (even tried with SubstructureRedirectMask), only Expose events.
26273 These expose events will draw text normally, i.e. not
26274 highlighted. Which means we must redo the highlight here.
26275 Subsume it under ``we love X''. --gerd 2001-08-15 */
26276 /* Included in Windows version because Windows most likely does not
26277 do the right thing if any third party tool offers
26278 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26279 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26280 {
26281 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26282 if (f == hlinfo->mouse_face_mouse_frame)
26283 {
26284 int mouse_x = hlinfo->mouse_face_mouse_x;
26285 int mouse_y = hlinfo->mouse_face_mouse_y;
26286 clear_mouse_face (hlinfo);
26287 note_mouse_highlight (f, mouse_x, mouse_y);
26288 }
26289 }
26290 }
26291
26292
26293 /* EXPORT:
26294 Determine the intersection of two rectangles R1 and R2. Return
26295 the intersection in *RESULT. Value is non-zero if RESULT is not
26296 empty. */
26297
26298 int
26299 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26300 {
26301 XRectangle *left, *right;
26302 XRectangle *upper, *lower;
26303 int intersection_p = 0;
26304
26305 /* Rearrange so that R1 is the left-most rectangle. */
26306 if (r1->x < r2->x)
26307 left = r1, right = r2;
26308 else
26309 left = r2, right = r1;
26310
26311 /* X0 of the intersection is right.x0, if this is inside R1,
26312 otherwise there is no intersection. */
26313 if (right->x <= left->x + left->width)
26314 {
26315 result->x = right->x;
26316
26317 /* The right end of the intersection is the minimum of the
26318 the right ends of left and right. */
26319 result->width = (min (left->x + left->width, right->x + right->width)
26320 - result->x);
26321
26322 /* Same game for Y. */
26323 if (r1->y < r2->y)
26324 upper = r1, lower = r2;
26325 else
26326 upper = r2, lower = r1;
26327
26328 /* The upper end of the intersection is lower.y0, if this is inside
26329 of upper. Otherwise, there is no intersection. */
26330 if (lower->y <= upper->y + upper->height)
26331 {
26332 result->y = lower->y;
26333
26334 /* The lower end of the intersection is the minimum of the lower
26335 ends of upper and lower. */
26336 result->height = (min (lower->y + lower->height,
26337 upper->y + upper->height)
26338 - result->y);
26339 intersection_p = 1;
26340 }
26341 }
26342
26343 return intersection_p;
26344 }
26345
26346 #endif /* HAVE_WINDOW_SYSTEM */
26347
26348 \f
26349 /***********************************************************************
26350 Initialization
26351 ***********************************************************************/
26352
26353 void
26354 syms_of_xdisp (void)
26355 {
26356 Vwith_echo_area_save_vector = Qnil;
26357 staticpro (&Vwith_echo_area_save_vector);
26358
26359 Vmessage_stack = Qnil;
26360 staticpro (&Vmessage_stack);
26361
26362 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26363 staticpro (&Qinhibit_redisplay);
26364
26365 message_dolog_marker1 = Fmake_marker ();
26366 staticpro (&message_dolog_marker1);
26367 message_dolog_marker2 = Fmake_marker ();
26368 staticpro (&message_dolog_marker2);
26369 message_dolog_marker3 = Fmake_marker ();
26370 staticpro (&message_dolog_marker3);
26371
26372 #if GLYPH_DEBUG
26373 defsubr (&Sdump_frame_glyph_matrix);
26374 defsubr (&Sdump_glyph_matrix);
26375 defsubr (&Sdump_glyph_row);
26376 defsubr (&Sdump_tool_bar_row);
26377 defsubr (&Strace_redisplay);
26378 defsubr (&Strace_to_stderr);
26379 #endif
26380 #ifdef HAVE_WINDOW_SYSTEM
26381 defsubr (&Stool_bar_lines_needed);
26382 defsubr (&Slookup_image_map);
26383 #endif
26384 defsubr (&Sformat_mode_line);
26385 defsubr (&Sinvisible_p);
26386 defsubr (&Scurrent_bidi_paragraph_direction);
26387
26388 staticpro (&Qmenu_bar_update_hook);
26389 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26390
26391 staticpro (&Qoverriding_terminal_local_map);
26392 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26393
26394 staticpro (&Qoverriding_local_map);
26395 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26396
26397 staticpro (&Qwindow_scroll_functions);
26398 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26399
26400 staticpro (&Qwindow_text_change_functions);
26401 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26402
26403 staticpro (&Qredisplay_end_trigger_functions);
26404 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26405
26406 staticpro (&Qinhibit_point_motion_hooks);
26407 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26408
26409 Qeval = intern_c_string ("eval");
26410 staticpro (&Qeval);
26411
26412 QCdata = intern_c_string (":data");
26413 staticpro (&QCdata);
26414 Qdisplay = intern_c_string ("display");
26415 staticpro (&Qdisplay);
26416 Qspace_width = intern_c_string ("space-width");
26417 staticpro (&Qspace_width);
26418 Qraise = intern_c_string ("raise");
26419 staticpro (&Qraise);
26420 Qslice = intern_c_string ("slice");
26421 staticpro (&Qslice);
26422 Qspace = intern_c_string ("space");
26423 staticpro (&Qspace);
26424 Qmargin = intern_c_string ("margin");
26425 staticpro (&Qmargin);
26426 Qpointer = intern_c_string ("pointer");
26427 staticpro (&Qpointer);
26428 Qleft_margin = intern_c_string ("left-margin");
26429 staticpro (&Qleft_margin);
26430 Qright_margin = intern_c_string ("right-margin");
26431 staticpro (&Qright_margin);
26432 Qcenter = intern_c_string ("center");
26433 staticpro (&Qcenter);
26434 Qline_height = intern_c_string ("line-height");
26435 staticpro (&Qline_height);
26436 QCalign_to = intern_c_string (":align-to");
26437 staticpro (&QCalign_to);
26438 QCrelative_width = intern_c_string (":relative-width");
26439 staticpro (&QCrelative_width);
26440 QCrelative_height = intern_c_string (":relative-height");
26441 staticpro (&QCrelative_height);
26442 QCeval = intern_c_string (":eval");
26443 staticpro (&QCeval);
26444 QCpropertize = intern_c_string (":propertize");
26445 staticpro (&QCpropertize);
26446 QCfile = intern_c_string (":file");
26447 staticpro (&QCfile);
26448 Qfontified = intern_c_string ("fontified");
26449 staticpro (&Qfontified);
26450 Qfontification_functions = intern_c_string ("fontification-functions");
26451 staticpro (&Qfontification_functions);
26452 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26453 staticpro (&Qtrailing_whitespace);
26454 Qescape_glyph = intern_c_string ("escape-glyph");
26455 staticpro (&Qescape_glyph);
26456 Qnobreak_space = intern_c_string ("nobreak-space");
26457 staticpro (&Qnobreak_space);
26458 Qimage = intern_c_string ("image");
26459 staticpro (&Qimage);
26460 Qtext = intern_c_string ("text");
26461 staticpro (&Qtext);
26462 Qboth = intern_c_string ("both");
26463 staticpro (&Qboth);
26464 Qboth_horiz = intern_c_string ("both-horiz");
26465 staticpro (&Qboth_horiz);
26466 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26467 staticpro (&Qtext_image_horiz);
26468 QCmap = intern_c_string (":map");
26469 staticpro (&QCmap);
26470 QCpointer = intern_c_string (":pointer");
26471 staticpro (&QCpointer);
26472 Qrect = intern_c_string ("rect");
26473 staticpro (&Qrect);
26474 Qcircle = intern_c_string ("circle");
26475 staticpro (&Qcircle);
26476 Qpoly = intern_c_string ("poly");
26477 staticpro (&Qpoly);
26478 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26479 staticpro (&Qmessage_truncate_lines);
26480 Qgrow_only = intern_c_string ("grow-only");
26481 staticpro (&Qgrow_only);
26482 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26483 staticpro (&Qinhibit_menubar_update);
26484 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26485 staticpro (&Qinhibit_eval_during_redisplay);
26486 Qposition = intern_c_string ("position");
26487 staticpro (&Qposition);
26488 Qbuffer_position = intern_c_string ("buffer-position");
26489 staticpro (&Qbuffer_position);
26490 Qobject = intern_c_string ("object");
26491 staticpro (&Qobject);
26492 Qbar = intern_c_string ("bar");
26493 staticpro (&Qbar);
26494 Qhbar = intern_c_string ("hbar");
26495 staticpro (&Qhbar);
26496 Qbox = intern_c_string ("box");
26497 staticpro (&Qbox);
26498 Qhollow = intern_c_string ("hollow");
26499 staticpro (&Qhollow);
26500 Qhand = intern_c_string ("hand");
26501 staticpro (&Qhand);
26502 Qarrow = intern_c_string ("arrow");
26503 staticpro (&Qarrow);
26504 Qtext = intern_c_string ("text");
26505 staticpro (&Qtext);
26506 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26507 staticpro (&Qinhibit_free_realized_faces);
26508
26509 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26510 Fcons (intern_c_string ("void-variable"), Qnil)),
26511 Qnil);
26512 staticpro (&list_of_error);
26513
26514 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26515 staticpro (&Qlast_arrow_position);
26516 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26517 staticpro (&Qlast_arrow_string);
26518
26519 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26520 staticpro (&Qoverlay_arrow_string);
26521 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26522 staticpro (&Qoverlay_arrow_bitmap);
26523
26524 echo_buffer[0] = echo_buffer[1] = Qnil;
26525 staticpro (&echo_buffer[0]);
26526 staticpro (&echo_buffer[1]);
26527
26528 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26529 staticpro (&echo_area_buffer[0]);
26530 staticpro (&echo_area_buffer[1]);
26531
26532 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26533 staticpro (&Vmessages_buffer_name);
26534
26535 mode_line_proptrans_alist = Qnil;
26536 staticpro (&mode_line_proptrans_alist);
26537 mode_line_string_list = Qnil;
26538 staticpro (&mode_line_string_list);
26539 mode_line_string_face = Qnil;
26540 staticpro (&mode_line_string_face);
26541 mode_line_string_face_prop = Qnil;
26542 staticpro (&mode_line_string_face_prop);
26543 Vmode_line_unwind_vector = Qnil;
26544 staticpro (&Vmode_line_unwind_vector);
26545
26546 help_echo_string = Qnil;
26547 staticpro (&help_echo_string);
26548 help_echo_object = Qnil;
26549 staticpro (&help_echo_object);
26550 help_echo_window = Qnil;
26551 staticpro (&help_echo_window);
26552 previous_help_echo_string = Qnil;
26553 staticpro (&previous_help_echo_string);
26554 help_echo_pos = -1;
26555
26556 Qright_to_left = intern_c_string ("right-to-left");
26557 staticpro (&Qright_to_left);
26558 Qleft_to_right = intern_c_string ("left-to-right");
26559 staticpro (&Qleft_to_right);
26560
26561 #ifdef HAVE_WINDOW_SYSTEM
26562 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
26563 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26564 For example, if a block cursor is over a tab, it will be drawn as
26565 wide as that tab on the display. */);
26566 x_stretch_cursor_p = 0;
26567 #endif
26568
26569 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
26570 doc: /* *Non-nil means highlight trailing whitespace.
26571 The face used for trailing whitespace is `trailing-whitespace'. */);
26572 Vshow_trailing_whitespace = Qnil;
26573
26574 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
26575 doc: /* *Control highlighting of nobreak space and soft hyphen.
26576 A value of t means highlight the character itself (for nobreak space,
26577 use face `nobreak-space').
26578 A value of nil means no highlighting.
26579 Other values mean display the escape glyph followed by an ordinary
26580 space or ordinary hyphen. */);
26581 Vnobreak_char_display = Qt;
26582
26583 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
26584 doc: /* *The pointer shape to show in void text areas.
26585 A value of nil means to show the text pointer. Other options are `arrow',
26586 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26587 Vvoid_text_area_pointer = Qarrow;
26588
26589 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
26590 doc: /* Non-nil means don't actually do any redisplay.
26591 This is used for internal purposes. */);
26592 Vinhibit_redisplay = Qnil;
26593
26594 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
26595 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26596 Vglobal_mode_string = Qnil;
26597
26598 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
26599 doc: /* Marker for where to display an arrow on top of the buffer text.
26600 This must be the beginning of a line in order to work.
26601 See also `overlay-arrow-string'. */);
26602 Voverlay_arrow_position = Qnil;
26603
26604 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
26605 doc: /* String to display as an arrow in non-window frames.
26606 See also `overlay-arrow-position'. */);
26607 Voverlay_arrow_string = make_pure_c_string ("=>");
26608
26609 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
26610 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26611 The symbols on this list are examined during redisplay to determine
26612 where to display overlay arrows. */);
26613 Voverlay_arrow_variable_list
26614 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26615
26616 DEFVAR_INT ("scroll-step", emacs_scroll_step,
26617 doc: /* *The number of lines to try scrolling a window by when point moves out.
26618 If that fails to bring point back on frame, point is centered instead.
26619 If this is zero, point is always centered after it moves off frame.
26620 If you want scrolling to always be a line at a time, you should set
26621 `scroll-conservatively' to a large value rather than set this to 1. */);
26622
26623 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
26624 doc: /* *Scroll up to this many lines, to bring point back on screen.
26625 If point moves off-screen, redisplay will scroll by up to
26626 `scroll-conservatively' lines in order to bring point just barely
26627 onto the screen again. If that cannot be done, then redisplay
26628 recenters point as usual.
26629
26630 If the value is greater than 100, redisplay will never recenter point,
26631 but will always scroll just enough text to bring point into view, even
26632 if you move far away.
26633
26634 A value of zero means always recenter point if it moves off screen. */);
26635 scroll_conservatively = 0;
26636
26637 DEFVAR_INT ("scroll-margin", scroll_margin,
26638 doc: /* *Number of lines of margin at the top and bottom of a window.
26639 Recenter the window whenever point gets within this many lines
26640 of the top or bottom of the window. */);
26641 scroll_margin = 0;
26642
26643 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
26644 doc: /* Pixels per inch value for non-window system displays.
26645 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26646 Vdisplay_pixels_per_inch = make_float (72.0);
26647
26648 #if GLYPH_DEBUG
26649 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
26650 #endif
26651
26652 DEFVAR_LISP ("truncate-partial-width-windows",
26653 Vtruncate_partial_width_windows,
26654 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26655 For an integer value, truncate lines in each window narrower than the
26656 full frame width, provided the window width is less than that integer;
26657 otherwise, respect the value of `truncate-lines'.
26658
26659 For any other non-nil value, truncate lines in all windows that do
26660 not span the full frame width.
26661
26662 A value of nil means to respect the value of `truncate-lines'.
26663
26664 If `word-wrap' is enabled, you might want to reduce this. */);
26665 Vtruncate_partial_width_windows = make_number (50);
26666
26667 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
26668 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26669 Any other value means to use the appropriate face, `mode-line',
26670 `header-line', or `menu' respectively. */);
26671 mode_line_inverse_video = 1;
26672
26673 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
26674 doc: /* *Maximum buffer size for which line number should be displayed.
26675 If the buffer is bigger than this, the line number does not appear
26676 in the mode line. A value of nil means no limit. */);
26677 Vline_number_display_limit = Qnil;
26678
26679 DEFVAR_INT ("line-number-display-limit-width",
26680 line_number_display_limit_width,
26681 doc: /* *Maximum line width (in characters) for line number display.
26682 If the average length of the lines near point is bigger than this, then the
26683 line number may be omitted from the mode line. */);
26684 line_number_display_limit_width = 200;
26685
26686 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
26687 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26688 highlight_nonselected_windows = 0;
26689
26690 DEFVAR_BOOL ("multiple-frames", multiple_frames,
26691 doc: /* Non-nil if more than one frame is visible on this display.
26692 Minibuffer-only frames don't count, but iconified frames do.
26693 This variable is not guaranteed to be accurate except while processing
26694 `frame-title-format' and `icon-title-format'. */);
26695
26696 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
26697 doc: /* Template for displaying the title bar of visible frames.
26698 \(Assuming the window manager supports this feature.)
26699
26700 This variable has the same structure as `mode-line-format', except that
26701 the %c and %l constructs are ignored. It is used only on frames for
26702 which no explicit name has been set \(see `modify-frame-parameters'). */);
26703
26704 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
26705 doc: /* Template for displaying the title bar of an iconified frame.
26706 \(Assuming the window manager supports this feature.)
26707 This variable has the same structure as `mode-line-format' (which see),
26708 and is used only on frames for which no explicit name has been set
26709 \(see `modify-frame-parameters'). */);
26710 Vicon_title_format
26711 = Vframe_title_format
26712 = pure_cons (intern_c_string ("multiple-frames"),
26713 pure_cons (make_pure_c_string ("%b"),
26714 pure_cons (pure_cons (empty_unibyte_string,
26715 pure_cons (intern_c_string ("invocation-name"),
26716 pure_cons (make_pure_c_string ("@"),
26717 pure_cons (intern_c_string ("system-name"),
26718 Qnil)))),
26719 Qnil)));
26720
26721 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
26722 doc: /* Maximum number of lines to keep in the message log buffer.
26723 If nil, disable message logging. If t, log messages but don't truncate
26724 the buffer when it becomes large. */);
26725 Vmessage_log_max = make_number (100);
26726
26727 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
26728 doc: /* Functions called before redisplay, if window sizes have changed.
26729 The value should be a list of functions that take one argument.
26730 Just before redisplay, for each frame, if any of its windows have changed
26731 size since the last redisplay, or have been split or deleted,
26732 all the functions in the list are called, with the frame as argument. */);
26733 Vwindow_size_change_functions = Qnil;
26734
26735 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
26736 doc: /* List of functions to call before redisplaying a window with scrolling.
26737 Each function is called with two arguments, the window and its new
26738 display-start position. Note that these functions are also called by
26739 `set-window-buffer'. Also note that the value of `window-end' is not
26740 valid when these functions are called. */);
26741 Vwindow_scroll_functions = Qnil;
26742
26743 DEFVAR_LISP ("window-text-change-functions",
26744 Vwindow_text_change_functions,
26745 doc: /* Functions to call in redisplay when text in the window might change. */);
26746 Vwindow_text_change_functions = Qnil;
26747
26748 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
26749 doc: /* Functions called when redisplay of a window reaches the end trigger.
26750 Each function is called with two arguments, the window and the end trigger value.
26751 See `set-window-redisplay-end-trigger'. */);
26752 Vredisplay_end_trigger_functions = Qnil;
26753
26754 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
26755 doc: /* *Non-nil means autoselect window with mouse pointer.
26756 If nil, do not autoselect windows.
26757 A positive number means delay autoselection by that many seconds: a
26758 window is autoselected only after the mouse has remained in that
26759 window for the duration of the delay.
26760 A negative number has a similar effect, but causes windows to be
26761 autoselected only after the mouse has stopped moving. \(Because of
26762 the way Emacs compares mouse events, you will occasionally wait twice
26763 that time before the window gets selected.\)
26764 Any other value means to autoselect window instantaneously when the
26765 mouse pointer enters it.
26766
26767 Autoselection selects the minibuffer only if it is active, and never
26768 unselects the minibuffer if it is active.
26769
26770 When customizing this variable make sure that the actual value of
26771 `focus-follows-mouse' matches the behavior of your window manager. */);
26772 Vmouse_autoselect_window = Qnil;
26773
26774 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
26775 doc: /* *Non-nil means automatically resize tool-bars.
26776 This dynamically changes the tool-bar's height to the minimum height
26777 that is needed to make all tool-bar items visible.
26778 If value is `grow-only', the tool-bar's height is only increased
26779 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26780 Vauto_resize_tool_bars = Qt;
26781
26782 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
26783 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26784 auto_raise_tool_bar_buttons_p = 1;
26785
26786 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
26787 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26788 make_cursor_line_fully_visible_p = 1;
26789
26790 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
26791 doc: /* *Border below tool-bar in pixels.
26792 If an integer, use it as the height of the border.
26793 If it is one of `internal-border-width' or `border-width', use the
26794 value of the corresponding frame parameter.
26795 Otherwise, no border is added below the tool-bar. */);
26796 Vtool_bar_border = Qinternal_border_width;
26797
26798 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
26799 doc: /* *Margin around tool-bar buttons in pixels.
26800 If an integer, use that for both horizontal and vertical margins.
26801 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26802 HORZ specifying the horizontal margin, and VERT specifying the
26803 vertical margin. */);
26804 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26805
26806 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
26807 doc: /* *Relief thickness of tool-bar buttons. */);
26808 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26809
26810 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
26811 doc: /* Tool bar style to use.
26812 It can be one of
26813 image - show images only
26814 text - show text only
26815 both - show both, text below image
26816 both-horiz - show text to the right of the image
26817 text-image-horiz - show text to the left of the image
26818 any other - use system default or image if no system default. */);
26819 Vtool_bar_style = Qnil;
26820
26821 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
26822 doc: /* *Maximum number of characters a label can have to be shown.
26823 The tool bar style must also show labels for this to have any effect, see
26824 `tool-bar-style'. */);
26825 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26826
26827 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
26828 doc: /* List of functions to call to fontify regions of text.
26829 Each function is called with one argument POS. Functions must
26830 fontify a region starting at POS in the current buffer, and give
26831 fontified regions the property `fontified'. */);
26832 Vfontification_functions = Qnil;
26833 Fmake_variable_buffer_local (Qfontification_functions);
26834
26835 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26836 unibyte_display_via_language_environment,
26837 doc: /* *Non-nil means display unibyte text according to language environment.
26838 Specifically, this means that raw bytes in the range 160-255 decimal
26839 are displayed by converting them to the equivalent multibyte characters
26840 according to the current language environment. As a result, they are
26841 displayed according to the current fontset.
26842
26843 Note that this variable affects only how these bytes are displayed,
26844 but does not change the fact they are interpreted as raw bytes. */);
26845 unibyte_display_via_language_environment = 0;
26846
26847 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
26848 doc: /* *Maximum height for resizing mini-windows.
26849 If a float, it specifies a fraction of the mini-window frame's height.
26850 If an integer, it specifies a number of lines. */);
26851 Vmax_mini_window_height = make_float (0.25);
26852
26853 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
26854 doc: /* *How to resize mini-windows.
26855 A value of nil means don't automatically resize mini-windows.
26856 A value of t means resize them to fit the text displayed in them.
26857 A value of `grow-only', the default, means let mini-windows grow
26858 only, until their display becomes empty, at which point the windows
26859 go back to their normal size. */);
26860 Vresize_mini_windows = Qgrow_only;
26861
26862 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
26863 doc: /* Alist specifying how to blink the cursor off.
26864 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26865 `cursor-type' frame-parameter or variable equals ON-STATE,
26866 comparing using `equal', Emacs uses OFF-STATE to specify
26867 how to blink it off. ON-STATE and OFF-STATE are values for
26868 the `cursor-type' frame parameter.
26869
26870 If a frame's ON-STATE has no entry in this list,
26871 the frame's other specifications determine how to blink the cursor off. */);
26872 Vblink_cursor_alist = Qnil;
26873
26874 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
26875 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26876 If non-nil, windows are automatically scrolled horizontally to make
26877 point visible. */);
26878 automatic_hscrolling_p = 1;
26879 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26880 staticpro (&Qauto_hscroll_mode);
26881
26882 DEFVAR_INT ("hscroll-margin", hscroll_margin,
26883 doc: /* *How many columns away from the window edge point is allowed to get
26884 before automatic hscrolling will horizontally scroll the window. */);
26885 hscroll_margin = 5;
26886
26887 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
26888 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26889 When point is less than `hscroll-margin' columns from the window
26890 edge, automatic hscrolling will scroll the window by the amount of columns
26891 determined by this variable. If its value is a positive integer, scroll that
26892 many columns. If it's a positive floating-point number, it specifies the
26893 fraction of the window's width to scroll. If it's nil or zero, point will be
26894 centered horizontally after the scroll. Any other value, including negative
26895 numbers, are treated as if the value were zero.
26896
26897 Automatic hscrolling always moves point outside the scroll margin, so if
26898 point was more than scroll step columns inside the margin, the window will
26899 scroll more than the value given by the scroll step.
26900
26901 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26902 and `scroll-right' overrides this variable's effect. */);
26903 Vhscroll_step = make_number (0);
26904
26905 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
26906 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26907 Bind this around calls to `message' to let it take effect. */);
26908 message_truncate_lines = 0;
26909
26910 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
26911 doc: /* Normal hook run to update the menu bar definitions.
26912 Redisplay runs this hook before it redisplays the menu bar.
26913 This is used to update submenus such as Buffers,
26914 whose contents depend on various data. */);
26915 Vmenu_bar_update_hook = Qnil;
26916
26917 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
26918 doc: /* Frame for which we are updating a menu.
26919 The enable predicate for a menu binding should check this variable. */);
26920 Vmenu_updating_frame = Qnil;
26921
26922 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
26923 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26924 inhibit_menubar_update = 0;
26925
26926 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
26927 doc: /* Prefix prepended to all continuation lines at display time.
26928 The value may be a string, an image, or a stretch-glyph; it is
26929 interpreted in the same way as the value of a `display' text property.
26930
26931 This variable is overridden by any `wrap-prefix' text or overlay
26932 property.
26933
26934 To add a prefix to non-continuation lines, use `line-prefix'. */);
26935 Vwrap_prefix = Qnil;
26936 staticpro (&Qwrap_prefix);
26937 Qwrap_prefix = intern_c_string ("wrap-prefix");
26938 Fmake_variable_buffer_local (Qwrap_prefix);
26939
26940 DEFVAR_LISP ("line-prefix", Vline_prefix,
26941 doc: /* Prefix prepended to all non-continuation lines at display time.
26942 The value may be a string, an image, or a stretch-glyph; it is
26943 interpreted in the same way as the value of a `display' text property.
26944
26945 This variable is overridden by any `line-prefix' text or overlay
26946 property.
26947
26948 To add a prefix to continuation lines, use `wrap-prefix'. */);
26949 Vline_prefix = Qnil;
26950 staticpro (&Qline_prefix);
26951 Qline_prefix = intern_c_string ("line-prefix");
26952 Fmake_variable_buffer_local (Qline_prefix);
26953
26954 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
26955 doc: /* Non-nil means don't eval Lisp during redisplay. */);
26956 inhibit_eval_during_redisplay = 0;
26957
26958 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
26959 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26960 inhibit_free_realized_faces = 0;
26961
26962 #if GLYPH_DEBUG
26963 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
26964 doc: /* Inhibit try_window_id display optimization. */);
26965 inhibit_try_window_id = 0;
26966
26967 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
26968 doc: /* Inhibit try_window_reusing display optimization. */);
26969 inhibit_try_window_reusing = 0;
26970
26971 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
26972 doc: /* Inhibit try_cursor_movement display optimization. */);
26973 inhibit_try_cursor_movement = 0;
26974 #endif /* GLYPH_DEBUG */
26975
26976 DEFVAR_INT ("overline-margin", overline_margin,
26977 doc: /* *Space between overline and text, in pixels.
26978 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26979 margin to the caracter height. */);
26980 overline_margin = 2;
26981
26982 DEFVAR_INT ("underline-minimum-offset",
26983 underline_minimum_offset,
26984 doc: /* Minimum distance between baseline and underline.
26985 This can improve legibility of underlined text at small font sizes,
26986 particularly when using variable `x-use-underline-position-properties'
26987 with fonts that specify an UNDERLINE_POSITION relatively close to the
26988 baseline. The default value is 1. */);
26989 underline_minimum_offset = 1;
26990
26991 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
26992 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
26993 This feature only works when on a window system that can change
26994 cursor shapes. */);
26995 display_hourglass_p = 1;
26996
26997 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
26998 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
26999 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27000
27001 hourglass_atimer = NULL;
27002 hourglass_shown_p = 0;
27003
27004 DEFSYM (Qglyphless_char, "glyphless-char");
27005 DEFSYM (Qhex_code, "hex-code");
27006 DEFSYM (Qempty_box, "empty-box");
27007 DEFSYM (Qthin_space, "thin-space");
27008 DEFSYM (Qzero_width, "zero-width");
27009
27010 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27011 /* Intern this now in case it isn't already done.
27012 Setting this variable twice is harmless.
27013 But don't staticpro it here--that is done in alloc.c. */
27014 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27015 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27016
27017 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27018 doc: /* Char-table defining glyphless characters.
27019 Each element, if non-nil, should be one of the following:
27020 an ASCII acronym string: display this string in a box
27021 `hex-code': display the hexadecimal code of a character in a box
27022 `empty-box': display as an empty box
27023 `thin-space': display as 1-pixel width space
27024 `zero-width': don't display
27025 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27026 display method for graphical terminals and text terminals respectively.
27027 GRAPHICAL and TEXT should each have one of the values listed above.
27028
27029 The char-table has one extra slot to control the display of a character for
27030 which no font is found. This slot only takes effect on graphical terminals.
27031 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27032 `thin-space'. The default is `empty-box'. */);
27033 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27034 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27035 Qempty_box);
27036 }
27037
27038
27039 /* Initialize this module when Emacs starts. */
27040
27041 void
27042 init_xdisp (void)
27043 {
27044 Lisp_Object root_window;
27045 struct window *mini_w;
27046
27047 current_header_line_height = current_mode_line_height = -1;
27048
27049 CHARPOS (this_line_start_pos) = 0;
27050
27051 mini_w = XWINDOW (minibuf_window);
27052 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27053 echo_area_window = minibuf_window;
27054
27055 if (!noninteractive)
27056 {
27057 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27058 int i;
27059
27060 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27061 set_window_height (root_window,
27062 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27063 0);
27064 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27065 set_window_height (minibuf_window, 1, 0);
27066
27067 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27068 mini_w->total_cols = make_number (FRAME_COLS (f));
27069
27070 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27071 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27072 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27073
27074 /* The default ellipsis glyphs `...'. */
27075 for (i = 0; i < 3; ++i)
27076 default_invis_vector[i] = make_number ('.');
27077 }
27078
27079 {
27080 /* Allocate the buffer for frame titles.
27081 Also used for `format-mode-line'. */
27082 int size = 100;
27083 mode_line_noprop_buf = (char *) xmalloc (size);
27084 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27085 mode_line_noprop_ptr = mode_line_noprop_buf;
27086 mode_line_target = MODE_LINE_DISPLAY;
27087 }
27088
27089 help_echo_showing_p = 0;
27090 }
27091
27092 /* Since w32 does not support atimers, it defines its own implementation of
27093 the following three functions in w32fns.c. */
27094 #ifndef WINDOWSNT
27095
27096 /* Platform-independent portion of hourglass implementation. */
27097
27098 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27099 int
27100 hourglass_started (void)
27101 {
27102 return hourglass_shown_p || hourglass_atimer != NULL;
27103 }
27104
27105 /* Cancel a currently active hourglass timer, and start a new one. */
27106 void
27107 start_hourglass (void)
27108 {
27109 #if defined (HAVE_WINDOW_SYSTEM)
27110 EMACS_TIME delay;
27111 int secs, usecs = 0;
27112
27113 cancel_hourglass ();
27114
27115 if (INTEGERP (Vhourglass_delay)
27116 && XINT (Vhourglass_delay) > 0)
27117 secs = XFASTINT (Vhourglass_delay);
27118 else if (FLOATP (Vhourglass_delay)
27119 && XFLOAT_DATA (Vhourglass_delay) > 0)
27120 {
27121 Lisp_Object tem;
27122 tem = Ftruncate (Vhourglass_delay, Qnil);
27123 secs = XFASTINT (tem);
27124 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27125 }
27126 else
27127 secs = DEFAULT_HOURGLASS_DELAY;
27128
27129 EMACS_SET_SECS_USECS (delay, secs, usecs);
27130 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27131 show_hourglass, NULL);
27132 #endif
27133 }
27134
27135
27136 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27137 shown. */
27138 void
27139 cancel_hourglass (void)
27140 {
27141 #if defined (HAVE_WINDOW_SYSTEM)
27142 if (hourglass_atimer)
27143 {
27144 cancel_atimer (hourglass_atimer);
27145 hourglass_atimer = NULL;
27146 }
27147
27148 if (hourglass_shown_p)
27149 hide_hourglass ();
27150 #endif
27151 }
27152 #endif /* ! WINDOWSNT */