* xdisp.c, lisp.h (message_nolog): Remove; unused.
[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 Lisp_Object Qwindow_text_change_functions;
322 Lisp_Object Qredisplay_end_trigger_functions;
323 Lisp_Object Qinhibit_point_motion_hooks;
324 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
325 Lisp_Object Qfontified;
326 Lisp_Object Qgrow_only;
327 Lisp_Object Qinhibit_eval_during_redisplay;
328 Lisp_Object Qbuffer_position, Qposition, Qobject;
329 Lisp_Object Qright_to_left, Qleft_to_right;
330
331 /* Cursor shapes */
332 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
333
334 /* Pointer shapes */
335 Lisp_Object Qarrow, Qhand, Qtext;
336
337 /* Holds the list (error). */
338 Lisp_Object list_of_error;
339
340 Lisp_Object Qfontification_functions;
341
342 Lisp_Object Qwrap_prefix;
343 Lisp_Object Qline_prefix;
344
345 /* Non-nil means don't actually do any redisplay. */
346
347 Lisp_Object Qinhibit_redisplay;
348
349 /* Names of text properties relevant for redisplay. */
350
351 Lisp_Object Qdisplay;
352
353 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
354 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
355 Lisp_Object Qslice;
356 Lisp_Object Qcenter;
357 Lisp_Object Qmargin, Qpointer;
358 Lisp_Object Qline_height;
359
360 #ifdef HAVE_WINDOW_SYSTEM
361
362 /* Test if overflow newline into fringe. Called with iterator IT
363 at or past right window margin, and with IT->current_x set. */
364
365 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
366 (!NILP (Voverflow_newline_into_fringe) \
367 && FRAME_WINDOW_P ((IT)->f) \
368 && ((IT)->bidi_it.paragraph_dir == R2L \
369 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
370 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
371 && (IT)->current_x == (IT)->last_visible_x \
372 && (IT)->line_wrap != WORD_WRAP)
373
374 #else /* !HAVE_WINDOW_SYSTEM */
375 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
376 #endif /* HAVE_WINDOW_SYSTEM */
377
378 /* Test if the display element loaded in IT is a space or tab
379 character. This is used to determine word wrapping. */
380
381 #define IT_DISPLAYING_WHITESPACE(it) \
382 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
383
384 /* Name of the face used to highlight trailing whitespace. */
385
386 Lisp_Object Qtrailing_whitespace;
387
388 /* Name and number of the face used to highlight escape glyphs. */
389
390 Lisp_Object Qescape_glyph;
391
392 /* Name and number of the face used to highlight non-breaking spaces. */
393
394 Lisp_Object Qnobreak_space;
395
396 /* The symbol `image' which is the car of the lists used to represent
397 images in Lisp. Also a tool bar style. */
398
399 Lisp_Object Qimage;
400
401 /* The image map types. */
402 Lisp_Object QCmap, QCpointer;
403 Lisp_Object Qrect, Qcircle, Qpoly;
404
405 /* Tool bar styles */
406 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
407
408 /* Non-zero means print newline to stdout before next mini-buffer
409 message. */
410
411 int noninteractive_need_newline;
412
413 /* Non-zero means print newline to message log before next message. */
414
415 static int message_log_need_newline;
416
417 /* Three markers that message_dolog uses.
418 It could allocate them itself, but that causes trouble
419 in handling memory-full errors. */
420 static Lisp_Object message_dolog_marker1;
421 static Lisp_Object message_dolog_marker2;
422 static Lisp_Object message_dolog_marker3;
423 \f
424 /* The buffer position of the first character appearing entirely or
425 partially on the line of the selected window which contains the
426 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
427 redisplay optimization in redisplay_internal. */
428
429 static struct text_pos this_line_start_pos;
430
431 /* Number of characters past the end of the line above, including the
432 terminating newline. */
433
434 static struct text_pos this_line_end_pos;
435
436 /* The vertical positions and the height of this line. */
437
438 static int this_line_vpos;
439 static int this_line_y;
440 static int this_line_pixel_height;
441
442 /* X position at which this display line starts. Usually zero;
443 negative if first character is partially visible. */
444
445 static int this_line_start_x;
446
447 /* The smallest character position seen by move_it_* functions as they
448 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
449 hscrolled lines, see display_line. */
450
451 static struct text_pos this_line_min_pos;
452
453 /* Buffer that this_line_.* variables are referring to. */
454
455 static struct buffer *this_line_buffer;
456
457
458 /* Values of those variables at last redisplay are stored as
459 properties on `overlay-arrow-position' symbol. However, if
460 Voverlay_arrow_position is a marker, last-arrow-position is its
461 numerical position. */
462
463 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
464
465 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
466 properties on a symbol in overlay-arrow-variable-list. */
467
468 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
469
470 Lisp_Object Qmenu_bar_update_hook;
471
472 /* Nonzero if an overlay arrow has been displayed in this window. */
473
474 static int overlay_arrow_seen;
475
476 /* Number of windows showing the buffer of the selected window (or
477 another buffer with the same base buffer). keyboard.c refers to
478 this. */
479
480 int buffer_shared;
481
482 /* Vector containing glyphs for an ellipsis `...'. */
483
484 static Lisp_Object default_invis_vector[3];
485
486 /* This is the window where the echo area message was displayed. It
487 is always a mini-buffer window, but it may not be the same window
488 currently active as a mini-buffer. */
489
490 Lisp_Object echo_area_window;
491
492 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
493 pushes the current message and the value of
494 message_enable_multibyte on the stack, the function restore_message
495 pops the stack and displays MESSAGE again. */
496
497 Lisp_Object Vmessage_stack;
498
499 /* Nonzero means multibyte characters were enabled when the echo area
500 message was specified. */
501
502 int message_enable_multibyte;
503
504 /* Nonzero if we should redraw the mode lines on the next redisplay. */
505
506 int update_mode_lines;
507
508 /* Nonzero if window sizes or contents have changed since last
509 redisplay that finished. */
510
511 int windows_or_buffers_changed;
512
513 /* Nonzero means a frame's cursor type has been changed. */
514
515 int cursor_type_changed;
516
517 /* Nonzero after display_mode_line if %l was used and it displayed a
518 line number. */
519
520 int line_number_displayed;
521
522 /* The name of the *Messages* buffer, a string. */
523
524 static Lisp_Object Vmessages_buffer_name;
525
526 /* Current, index 0, and last displayed echo area message. Either
527 buffers from echo_buffers, or nil to indicate no message. */
528
529 Lisp_Object echo_area_buffer[2];
530
531 /* The buffers referenced from echo_area_buffer. */
532
533 static Lisp_Object echo_buffer[2];
534
535 /* A vector saved used in with_area_buffer to reduce consing. */
536
537 static Lisp_Object Vwith_echo_area_save_vector;
538
539 /* Non-zero means display_echo_area should display the last echo area
540 message again. Set by redisplay_preserve_echo_area. */
541
542 static int display_last_displayed_message_p;
543
544 /* Nonzero if echo area is being used by print; zero if being used by
545 message. */
546
547 int message_buf_print;
548
549 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
550
551 Lisp_Object Qinhibit_menubar_update;
552 Lisp_Object Qmessage_truncate_lines;
553
554 /* Set to 1 in clear_message to make redisplay_internal aware
555 of an emptied echo area. */
556
557 static int message_cleared_p;
558
559 /* A scratch glyph row with contents used for generating truncation
560 glyphs. Also used in direct_output_for_insert. */
561
562 #define MAX_SCRATCH_GLYPHS 100
563 struct glyph_row scratch_glyph_row;
564 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
565
566 /* Ascent and height of the last line processed by move_it_to. */
567
568 static int last_max_ascent, last_height;
569
570 /* Non-zero if there's a help-echo in the echo area. */
571
572 int help_echo_showing_p;
573
574 /* If >= 0, computed, exact values of mode-line and header-line height
575 to use in the macros CURRENT_MODE_LINE_HEIGHT and
576 CURRENT_HEADER_LINE_HEIGHT. */
577
578 int current_mode_line_height, current_header_line_height;
579
580 /* The maximum distance to look ahead for text properties. Values
581 that are too small let us call compute_char_face and similar
582 functions too often which is expensive. Values that are too large
583 let us call compute_char_face and alike too often because we
584 might not be interested in text properties that far away. */
585
586 #define TEXT_PROP_DISTANCE_LIMIT 100
587
588 #if GLYPH_DEBUG
589
590 /* Non-zero means print traces of redisplay if compiled with
591 GLYPH_DEBUG != 0. */
592
593 int trace_redisplay_p;
594
595 #endif /* GLYPH_DEBUG */
596
597 #ifdef DEBUG_TRACE_MOVE
598 /* Non-zero means trace with TRACE_MOVE to stderr. */
599 int trace_move;
600
601 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
602 #else
603 #define TRACE_MOVE(x) (void) 0
604 #endif
605
606 Lisp_Object Qauto_hscroll_mode;
607
608 /* Buffer being redisplayed -- for redisplay_window_error. */
609
610 struct buffer *displayed_buffer;
611
612 /* Value returned from text property handlers (see below). */
613
614 enum prop_handled
615 {
616 HANDLED_NORMALLY,
617 HANDLED_RECOMPUTE_PROPS,
618 HANDLED_OVERLAY_STRING_CONSUMED,
619 HANDLED_RETURN
620 };
621
622 /* A description of text properties that redisplay is interested
623 in. */
624
625 struct props
626 {
627 /* The name of the property. */
628 Lisp_Object *name;
629
630 /* A unique index for the property. */
631 enum prop_idx idx;
632
633 /* A handler function called to set up iterator IT from the property
634 at IT's current position. Value is used to steer handle_stop. */
635 enum prop_handled (*handler) (struct it *it);
636 };
637
638 static enum prop_handled handle_face_prop (struct it *);
639 static enum prop_handled handle_invisible_prop (struct it *);
640 static enum prop_handled handle_display_prop (struct it *);
641 static enum prop_handled handle_composition_prop (struct it *);
642 static enum prop_handled handle_overlay_change (struct it *);
643 static enum prop_handled handle_fontified_prop (struct it *);
644
645 /* Properties handled by iterators. */
646
647 static struct props it_props[] =
648 {
649 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
650 /* Handle `face' before `display' because some sub-properties of
651 `display' need to know the face. */
652 {&Qface, FACE_PROP_IDX, handle_face_prop},
653 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
654 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
655 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
656 {NULL, 0, NULL}
657 };
658
659 /* Value is the position described by X. If X is a marker, value is
660 the marker_position of X. Otherwise, value is X. */
661
662 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
663
664 /* Enumeration returned by some move_it_.* functions internally. */
665
666 enum move_it_result
667 {
668 /* Not used. Undefined value. */
669 MOVE_UNDEFINED,
670
671 /* Move ended at the requested buffer position or ZV. */
672 MOVE_POS_MATCH_OR_ZV,
673
674 /* Move ended at the requested X pixel position. */
675 MOVE_X_REACHED,
676
677 /* Move within a line ended at the end of a line that must be
678 continued. */
679 MOVE_LINE_CONTINUED,
680
681 /* Move within a line ended at the end of a line that would
682 be displayed truncated. */
683 MOVE_LINE_TRUNCATED,
684
685 /* Move within a line ended at a line end. */
686 MOVE_NEWLINE_OR_CR
687 };
688
689 /* This counter is used to clear the face cache every once in a while
690 in redisplay_internal. It is incremented for each redisplay.
691 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
692 cleared. */
693
694 #define CLEAR_FACE_CACHE_COUNT 500
695 static int clear_face_cache_count;
696
697 /* Similarly for the image cache. */
698
699 #ifdef HAVE_WINDOW_SYSTEM
700 #define CLEAR_IMAGE_CACHE_COUNT 101
701 static int clear_image_cache_count;
702
703 /* Null glyph slice */
704 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
705 #endif
706
707 /* Non-zero while redisplay_internal is in progress. */
708
709 int redisplaying_p;
710
711 Lisp_Object Qinhibit_free_realized_faces;
712
713 /* If a string, XTread_socket generates an event to display that string.
714 (The display is done in read_char.) */
715
716 Lisp_Object help_echo_string;
717 Lisp_Object help_echo_window;
718 Lisp_Object help_echo_object;
719 EMACS_INT help_echo_pos;
720
721 /* Temporary variable for XTread_socket. */
722
723 Lisp_Object previous_help_echo_string;
724
725 /* Platform-independent portion of hourglass implementation. */
726
727 /* Non-zero means an hourglass cursor is currently shown. */
728 int hourglass_shown_p;
729
730 /* If non-null, an asynchronous timer that, when it expires, displays
731 an hourglass cursor on all frames. */
732 struct atimer *hourglass_atimer;
733
734 /* Name of the face used to display glyphless characters. */
735 Lisp_Object Qglyphless_char;
736
737 /* Symbol for the purpose of Vglyphless_char_display. */
738 Lisp_Object Qglyphless_char_display;
739
740 /* Method symbols for Vglyphless_char_display. */
741 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
742
743 /* Default pixel width of `thin-space' display method. */
744 #define THIN_SPACE_WIDTH 1
745
746 /* Default number of seconds to wait before displaying an hourglass
747 cursor. */
748 #define DEFAULT_HOURGLASS_DELAY 1
749
750 \f
751 /* Function prototypes. */
752
753 static void setup_for_ellipsis (struct it *, int);
754 static void mark_window_display_accurate_1 (struct window *, int);
755 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
756 static int display_prop_string_p (Lisp_Object, Lisp_Object);
757 static int cursor_row_p (struct glyph_row *);
758 static int redisplay_mode_lines (Lisp_Object, int);
759 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
760
761 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
762
763 static void handle_line_prefix (struct it *);
764
765 static void pint2str (char *, int, EMACS_INT);
766 static void pint2hrstr (char *, int, int);
767 static struct text_pos run_window_scroll_functions (Lisp_Object,
768 struct text_pos);
769 static void reconsider_clip_changes (struct window *, struct buffer *);
770 static int text_outside_line_unchanged_p (struct window *,
771 EMACS_INT, EMACS_INT);
772 static void store_mode_line_noprop_char (char);
773 static int store_mode_line_noprop (const char *, int, int);
774 static void handle_stop (struct it *);
775 static void handle_stop_backwards (struct it *, EMACS_INT);
776 static int single_display_spec_intangible_p (Lisp_Object);
777 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
778 static void ensure_echo_area_buffers (void);
779 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
780 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
781 static int with_echo_area_buffer (struct window *, int,
782 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
783 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
784 static void clear_garbaged_frames (void);
785 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
786 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
787 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
788 static int display_echo_area (struct window *);
789 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
790 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
791 static Lisp_Object unwind_redisplay (Lisp_Object);
792 static int string_char_and_length (const unsigned char *, int *);
793 static struct text_pos display_prop_end (struct it *, Lisp_Object,
794 struct text_pos);
795 static int compute_window_start_on_continuation_line (struct window *);
796 static Lisp_Object safe_eval_handler (Lisp_Object);
797 static void insert_left_trunc_glyphs (struct it *);
798 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
799 Lisp_Object);
800 static void extend_face_to_end_of_line (struct it *);
801 static int append_space_for_newline (struct it *, int);
802 static int cursor_row_fully_visible_p (struct window *, int, int);
803 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
804 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
805 static int trailing_whitespace_p (EMACS_INT);
806 static unsigned long int message_log_check_duplicate (EMACS_INT, EMACS_INT);
807 static void push_it (struct it *);
808 static void pop_it (struct it *);
809 static void sync_frame_with_window_matrix_rows (struct window *);
810 static void select_frame_for_redisplay (Lisp_Object);
811 static void redisplay_internal (void);
812 static int echo_area_display (int);
813 static void redisplay_windows (Lisp_Object);
814 static void redisplay_window (Lisp_Object, int);
815 static Lisp_Object redisplay_window_error (Lisp_Object);
816 static Lisp_Object redisplay_window_0 (Lisp_Object);
817 static Lisp_Object redisplay_window_1 (Lisp_Object);
818 static int update_menu_bar (struct frame *, int, int);
819 static int try_window_reusing_current_matrix (struct window *);
820 static int try_window_id (struct window *);
821 static int display_line (struct it *);
822 static int display_mode_lines (struct window *);
823 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
824 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
825 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
826 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
827 static void display_menu_bar (struct window *);
828 static int display_count_lines (EMACS_INT, EMACS_INT, int, EMACS_INT *);
829 static int display_string (const char *, Lisp_Object, Lisp_Object,
830 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
831 static void compute_line_metrics (struct it *);
832 static void run_redisplay_end_trigger_hook (struct it *);
833 static int get_overlay_strings (struct it *, EMACS_INT);
834 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
835 static void next_overlay_string (struct it *);
836 static void reseat (struct it *, struct text_pos, int);
837 static void reseat_1 (struct it *, struct text_pos, int);
838 static void back_to_previous_visible_line_start (struct it *);
839 void reseat_at_previous_visible_line_start (struct it *);
840 static void reseat_at_next_visible_line_start (struct it *, int);
841 static int next_element_from_ellipsis (struct it *);
842 static int next_element_from_display_vector (struct it *);
843 static int next_element_from_string (struct it *);
844 static int next_element_from_c_string (struct it *);
845 static int next_element_from_buffer (struct it *);
846 static int next_element_from_composition (struct it *);
847 static int next_element_from_image (struct it *);
848 static int next_element_from_stretch (struct it *);
849 static void load_overlay_strings (struct it *, EMACS_INT);
850 static int init_from_display_pos (struct it *, struct window *,
851 struct display_pos *);
852 static void reseat_to_string (struct it *, const char *,
853 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
854 static enum move_it_result
855 move_it_in_display_line_to (struct it *, EMACS_INT, int,
856 enum move_operation_enum);
857 void move_it_vertically_backward (struct it *, int);
858 static void init_to_row_start (struct it *, struct window *,
859 struct glyph_row *);
860 static int init_to_row_end (struct it *, struct window *,
861 struct glyph_row *);
862 static void back_to_previous_line_start (struct it *);
863 static int forward_to_next_line_start (struct it *, int *);
864 static struct text_pos string_pos_nchars_ahead (struct text_pos,
865 Lisp_Object, EMACS_INT);
866 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
867 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
868 static EMACS_INT number_of_chars (const char *, int);
869 static void compute_stop_pos (struct it *);
870 static void compute_string_pos (struct text_pos *, struct text_pos,
871 Lisp_Object);
872 static int face_before_or_after_it_pos (struct it *, int);
873 static EMACS_INT next_overlay_change (EMACS_INT);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, int);
877 static int underlying_face_id (struct it *);
878 static int in_ellipses_for_invisible_text_p (struct display_pos *,
879 struct window *);
880
881 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
882 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
883
884 #ifdef HAVE_WINDOW_SYSTEM
885
886 static void x_consider_frame_title (Lisp_Object);
887 static int tool_bar_lines_needed (struct frame *, int *);
888 static void update_tool_bar (struct frame *, int);
889 static void build_desired_tool_bar_string (struct frame *f);
890 static int redisplay_tool_bar (struct frame *);
891 static void display_tool_bar_line (struct it *, int);
892 static void notice_overwritten_cursor (struct window *,
893 enum glyph_row_area,
894 int, int, int, int);
895 static void append_stretch_glyph (struct it *, Lisp_Object,
896 int, int, int);
897
898
899 #endif /* HAVE_WINDOW_SYSTEM */
900
901 static int coords_in_mouse_face_p (struct window *, int, int);
902
903
904 \f
905 /***********************************************************************
906 Window display dimensions
907 ***********************************************************************/
908
909 /* Return the bottom boundary y-position for text lines in window W.
910 This is the first y position at which a line cannot start.
911 It is relative to the top of the window.
912
913 This is the height of W minus the height of a mode line, if any. */
914
915 INLINE int
916 window_text_bottom_y (struct window *w)
917 {
918 int height = WINDOW_TOTAL_HEIGHT (w);
919
920 if (WINDOW_WANTS_MODELINE_P (w))
921 height -= CURRENT_MODE_LINE_HEIGHT (w);
922 return height;
923 }
924
925 /* Return the pixel width of display area AREA of window W. AREA < 0
926 means return the total width of W, not including fringes to
927 the left and right of the window. */
928
929 INLINE int
930 window_box_width (struct window *w, int area)
931 {
932 int cols = XFASTINT (w->total_cols);
933 int pixels = 0;
934
935 if (!w->pseudo_window_p)
936 {
937 cols -= WINDOW_SCROLL_BAR_COLS (w);
938
939 if (area == TEXT_AREA)
940 {
941 if (INTEGERP (w->left_margin_cols))
942 cols -= XFASTINT (w->left_margin_cols);
943 if (INTEGERP (w->right_margin_cols))
944 cols -= XFASTINT (w->right_margin_cols);
945 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
946 }
947 else if (area == LEFT_MARGIN_AREA)
948 {
949 cols = (INTEGERP (w->left_margin_cols)
950 ? XFASTINT (w->left_margin_cols) : 0);
951 pixels = 0;
952 }
953 else if (area == RIGHT_MARGIN_AREA)
954 {
955 cols = (INTEGERP (w->right_margin_cols)
956 ? XFASTINT (w->right_margin_cols) : 0);
957 pixels = 0;
958 }
959 }
960
961 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
962 }
963
964
965 /* Return the pixel height of the display area of window W, not
966 including mode lines of W, if any. */
967
968 INLINE int
969 window_box_height (struct window *w)
970 {
971 struct frame *f = XFRAME (w->frame);
972 int height = WINDOW_TOTAL_HEIGHT (w);
973
974 xassert (height >= 0);
975
976 /* Note: the code below that determines the mode-line/header-line
977 height is essentially the same as that contained in the macro
978 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
979 the appropriate glyph row has its `mode_line_p' flag set,
980 and if it doesn't, uses estimate_mode_line_height instead. */
981
982 if (WINDOW_WANTS_MODELINE_P (w))
983 {
984 struct glyph_row *ml_row
985 = (w->current_matrix && w->current_matrix->rows
986 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
987 : 0);
988 if (ml_row && ml_row->mode_line_p)
989 height -= ml_row->height;
990 else
991 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
992 }
993
994 if (WINDOW_WANTS_HEADER_LINE_P (w))
995 {
996 struct glyph_row *hl_row
997 = (w->current_matrix && w->current_matrix->rows
998 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
999 : 0);
1000 if (hl_row && hl_row->mode_line_p)
1001 height -= hl_row->height;
1002 else
1003 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1004 }
1005
1006 /* With a very small font and a mode-line that's taller than
1007 default, we might end up with a negative height. */
1008 return max (0, height);
1009 }
1010
1011 /* Return the window-relative coordinate of the left edge of display
1012 area AREA of window W. AREA < 0 means return the left edge of the
1013 whole window, to the right of the left fringe of W. */
1014
1015 INLINE int
1016 window_box_left_offset (struct window *w, int area)
1017 {
1018 int x;
1019
1020 if (w->pseudo_window_p)
1021 return 0;
1022
1023 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1024
1025 if (area == TEXT_AREA)
1026 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1027 + window_box_width (w, LEFT_MARGIN_AREA));
1028 else if (area == RIGHT_MARGIN_AREA)
1029 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1030 + window_box_width (w, LEFT_MARGIN_AREA)
1031 + window_box_width (w, TEXT_AREA)
1032 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1033 ? 0
1034 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1035 else if (area == LEFT_MARGIN_AREA
1036 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1037 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1038
1039 return x;
1040 }
1041
1042
1043 /* Return the window-relative coordinate of the right edge of display
1044 area AREA of window W. AREA < 0 means return the right edge of the
1045 whole window, to the left of the right fringe of W. */
1046
1047 INLINE int
1048 window_box_right_offset (struct window *w, int area)
1049 {
1050 return window_box_left_offset (w, area) + window_box_width (w, area);
1051 }
1052
1053 /* Return the frame-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1056
1057 INLINE int
1058 window_box_left (struct window *w, int area)
1059 {
1060 struct frame *f = XFRAME (w->frame);
1061 int x;
1062
1063 if (w->pseudo_window_p)
1064 return FRAME_INTERNAL_BORDER_WIDTH (f);
1065
1066 x = (WINDOW_LEFT_EDGE_X (w)
1067 + window_box_left_offset (w, area));
1068
1069 return x;
1070 }
1071
1072
1073 /* Return the frame-relative coordinate of the right edge of display
1074 area AREA of window W. AREA < 0 means return the right edge of the
1075 whole window, to the left of the right fringe of W. */
1076
1077 INLINE int
1078 window_box_right (struct window *w, int area)
1079 {
1080 return window_box_left (w, area) + window_box_width (w, area);
1081 }
1082
1083 /* Get the bounding box of the display area AREA of window W, without
1084 mode lines, in frame-relative coordinates. AREA < 0 means the
1085 whole window, not including the left and right fringes of
1086 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1087 coordinates of the upper-left corner of the box. Return in
1088 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1089
1090 INLINE void
1091 window_box (struct window *w, int area, int *box_x, int *box_y,
1092 int *box_width, int *box_height)
1093 {
1094 if (box_width)
1095 *box_width = window_box_width (w, area);
1096 if (box_height)
1097 *box_height = window_box_height (w);
1098 if (box_x)
1099 *box_x = window_box_left (w, area);
1100 if (box_y)
1101 {
1102 *box_y = WINDOW_TOP_EDGE_Y (w);
1103 if (WINDOW_WANTS_HEADER_LINE_P (w))
1104 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1105 }
1106 }
1107
1108
1109 /* Get the bounding box of the display area AREA of window W, without
1110 mode lines. AREA < 0 means the whole window, not including the
1111 left and right fringe of the window. Return in *TOP_LEFT_X
1112 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1113 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1114 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1115 box. */
1116
1117 INLINE void
1118 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1119 int *bottom_right_x, int *bottom_right_y)
1120 {
1121 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1122 bottom_right_y);
1123 *bottom_right_x += *top_left_x;
1124 *bottom_right_y += *top_left_y;
1125 }
1126
1127
1128 \f
1129 /***********************************************************************
1130 Utilities
1131 ***********************************************************************/
1132
1133 /* Return the bottom y-position of the line the iterator IT is in.
1134 This can modify IT's settings. */
1135
1136 int
1137 line_bottom_y (struct it *it)
1138 {
1139 int line_height = it->max_ascent + it->max_descent;
1140 int line_top_y = it->current_y;
1141
1142 if (line_height == 0)
1143 {
1144 if (last_height)
1145 line_height = last_height;
1146 else if (IT_CHARPOS (*it) < ZV)
1147 {
1148 move_it_by_lines (it, 1);
1149 line_height = (it->max_ascent || it->max_descent
1150 ? it->max_ascent + it->max_descent
1151 : last_height);
1152 }
1153 else
1154 {
1155 struct glyph_row *row = it->glyph_row;
1156
1157 /* Use the default character height. */
1158 it->glyph_row = NULL;
1159 it->what = IT_CHARACTER;
1160 it->c = ' ';
1161 it->len = 1;
1162 PRODUCE_GLYPHS (it);
1163 line_height = it->ascent + it->descent;
1164 it->glyph_row = row;
1165 }
1166 }
1167
1168 return line_top_y + line_height;
1169 }
1170
1171
1172 /* Return 1 if position CHARPOS is visible in window W.
1173 CHARPOS < 0 means return info about WINDOW_END position.
1174 If visible, set *X and *Y to pixel coordinates of top left corner.
1175 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1176 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1177
1178 int
1179 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1180 int *rtop, int *rbot, int *rowh, int *vpos)
1181 {
1182 struct it it;
1183 struct text_pos top;
1184 int visible_p = 0;
1185 struct buffer *old_buffer = NULL;
1186
1187 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1188 return visible_p;
1189
1190 if (XBUFFER (w->buffer) != current_buffer)
1191 {
1192 old_buffer = current_buffer;
1193 set_buffer_internal_1 (XBUFFER (w->buffer));
1194 }
1195
1196 SET_TEXT_POS_FROM_MARKER (top, w->start);
1197
1198 /* Compute exact mode line heights. */
1199 if (WINDOW_WANTS_MODELINE_P (w))
1200 current_mode_line_height
1201 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1202 BVAR (current_buffer, mode_line_format));
1203
1204 if (WINDOW_WANTS_HEADER_LINE_P (w))
1205 current_header_line_height
1206 = display_mode_line (w, HEADER_LINE_FACE_ID,
1207 BVAR (current_buffer, header_line_format));
1208
1209 start_display (&it, w, top);
1210 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1211 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1212
1213 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1214 {
1215 /* We have reached CHARPOS, or passed it. How the call to
1216 move_it_to can overshoot: (i) If CHARPOS is on invisible
1217 text, move_it_to stops at the end of the invisible text,
1218 after CHARPOS. (ii) If CHARPOS is in a display vector,
1219 move_it_to stops on its last glyph. */
1220 int top_x = it.current_x;
1221 int top_y = it.current_y;
1222 enum it_method it_method = it.method;
1223 /* Calling line_bottom_y may change it.method, it.position, etc. */
1224 int bottom_y = (last_height = 0, line_bottom_y (&it));
1225 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1226
1227 if (top_y < window_top_y)
1228 visible_p = bottom_y > window_top_y;
1229 else if (top_y < it.last_visible_y)
1230 visible_p = 1;
1231 if (visible_p)
1232 {
1233 if (it_method == GET_FROM_DISPLAY_VECTOR)
1234 {
1235 /* We stopped on the last glyph of a display vector.
1236 Try and recompute. Hack alert! */
1237 if (charpos < 2 || top.charpos >= charpos)
1238 top_x = it.glyph_row->x;
1239 else
1240 {
1241 struct it it2;
1242 start_display (&it2, w, top);
1243 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1244 get_next_display_element (&it2);
1245 PRODUCE_GLYPHS (&it2);
1246 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1247 || it2.current_x > it2.last_visible_x)
1248 top_x = it.glyph_row->x;
1249 else
1250 {
1251 top_x = it2.current_x;
1252 top_y = it2.current_y;
1253 }
1254 }
1255 }
1256
1257 *x = top_x;
1258 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1259 *rtop = max (0, window_top_y - top_y);
1260 *rbot = max (0, bottom_y - it.last_visible_y);
1261 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1262 - max (top_y, window_top_y)));
1263 *vpos = it.vpos;
1264 }
1265 }
1266 else
1267 {
1268 struct it it2;
1269
1270 it2 = it;
1271 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1272 move_it_by_lines (&it, 1);
1273 if (charpos < IT_CHARPOS (it)
1274 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1275 {
1276 visible_p = 1;
1277 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1278 *x = it2.current_x;
1279 *y = it2.current_y + it2.max_ascent - it2.ascent;
1280 *rtop = max (0, -it2.current_y);
1281 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1282 - it.last_visible_y));
1283 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1284 it.last_visible_y)
1285 - max (it2.current_y,
1286 WINDOW_HEADER_LINE_HEIGHT (w))));
1287 *vpos = it2.vpos;
1288 }
1289 }
1290
1291 if (old_buffer)
1292 set_buffer_internal_1 (old_buffer);
1293
1294 current_header_line_height = current_mode_line_height = -1;
1295
1296 if (visible_p && XFASTINT (w->hscroll) > 0)
1297 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1298
1299 #if 0
1300 /* Debugging code. */
1301 if (visible_p)
1302 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1303 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1304 else
1305 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1306 #endif
1307
1308 return visible_p;
1309 }
1310
1311
1312 /* Return the next character from STR. Return in *LEN the length of
1313 the character. This is like STRING_CHAR_AND_LENGTH but never
1314 returns an invalid character. If we find one, we return a `?', but
1315 with the length of the invalid character. */
1316
1317 static INLINE int
1318 string_char_and_length (const unsigned char *str, int *len)
1319 {
1320 int c;
1321
1322 c = STRING_CHAR_AND_LENGTH (str, *len);
1323 if (!CHAR_VALID_P (c, 1))
1324 /* We may not change the length here because other places in Emacs
1325 don't use this function, i.e. they silently accept invalid
1326 characters. */
1327 c = '?';
1328
1329 return c;
1330 }
1331
1332
1333
1334 /* Given a position POS containing a valid character and byte position
1335 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1336
1337 static struct text_pos
1338 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1339 {
1340 xassert (STRINGP (string) && nchars >= 0);
1341
1342 if (STRING_MULTIBYTE (string))
1343 {
1344 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1345 int len;
1346
1347 while (nchars--)
1348 {
1349 string_char_and_length (p, &len);
1350 p += len;
1351 CHARPOS (pos) += 1;
1352 BYTEPOS (pos) += len;
1353 }
1354 }
1355 else
1356 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1357
1358 return pos;
1359 }
1360
1361
1362 /* Value is the text position, i.e. character and byte position,
1363 for character position CHARPOS in STRING. */
1364
1365 static INLINE struct text_pos
1366 string_pos (EMACS_INT charpos, Lisp_Object string)
1367 {
1368 struct text_pos pos;
1369 xassert (STRINGP (string));
1370 xassert (charpos >= 0);
1371 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1372 return pos;
1373 }
1374
1375
1376 /* Value is a text position, i.e. character and byte position, for
1377 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1378 means recognize multibyte characters. */
1379
1380 static struct text_pos
1381 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1382 {
1383 struct text_pos pos;
1384
1385 xassert (s != NULL);
1386 xassert (charpos >= 0);
1387
1388 if (multibyte_p)
1389 {
1390 int len;
1391
1392 SET_TEXT_POS (pos, 0, 0);
1393 while (charpos--)
1394 {
1395 string_char_and_length ((const unsigned char *) s, &len);
1396 s += len;
1397 CHARPOS (pos) += 1;
1398 BYTEPOS (pos) += len;
1399 }
1400 }
1401 else
1402 SET_TEXT_POS (pos, charpos, charpos);
1403
1404 return pos;
1405 }
1406
1407
1408 /* Value is the number of characters in C string S. MULTIBYTE_P
1409 non-zero means recognize multibyte characters. */
1410
1411 static EMACS_INT
1412 number_of_chars (const char *s, int multibyte_p)
1413 {
1414 EMACS_INT nchars;
1415
1416 if (multibyte_p)
1417 {
1418 EMACS_INT rest = strlen (s);
1419 int len;
1420 const unsigned char *p = (const unsigned char *) s;
1421
1422 for (nchars = 0; rest > 0; ++nchars)
1423 {
1424 string_char_and_length (p, &len);
1425 rest -= len, p += len;
1426 }
1427 }
1428 else
1429 nchars = strlen (s);
1430
1431 return nchars;
1432 }
1433
1434
1435 /* Compute byte position NEWPOS->bytepos corresponding to
1436 NEWPOS->charpos. POS is a known position in string STRING.
1437 NEWPOS->charpos must be >= POS.charpos. */
1438
1439 static void
1440 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1441 {
1442 xassert (STRINGP (string));
1443 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1444
1445 if (STRING_MULTIBYTE (string))
1446 *newpos = string_pos_nchars_ahead (pos, string,
1447 CHARPOS (*newpos) - CHARPOS (pos));
1448 else
1449 BYTEPOS (*newpos) = CHARPOS (*newpos);
1450 }
1451
1452 /* EXPORT:
1453 Return an estimation of the pixel height of mode or header lines on
1454 frame F. FACE_ID specifies what line's height to estimate. */
1455
1456 int
1457 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1458 {
1459 #ifdef HAVE_WINDOW_SYSTEM
1460 if (FRAME_WINDOW_P (f))
1461 {
1462 int height = FONT_HEIGHT (FRAME_FONT (f));
1463
1464 /* This function is called so early when Emacs starts that the face
1465 cache and mode line face are not yet initialized. */
1466 if (FRAME_FACE_CACHE (f))
1467 {
1468 struct face *face = FACE_FROM_ID (f, face_id);
1469 if (face)
1470 {
1471 if (face->font)
1472 height = FONT_HEIGHT (face->font);
1473 if (face->box_line_width > 0)
1474 height += 2 * face->box_line_width;
1475 }
1476 }
1477
1478 return height;
1479 }
1480 #endif
1481
1482 return 1;
1483 }
1484
1485 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1486 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1487 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1488 not force the value into range. */
1489
1490 void
1491 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1492 int *x, int *y, NativeRectangle *bounds, int noclip)
1493 {
1494
1495 #ifdef HAVE_WINDOW_SYSTEM
1496 if (FRAME_WINDOW_P (f))
1497 {
1498 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1499 even for negative values. */
1500 if (pix_x < 0)
1501 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1502 if (pix_y < 0)
1503 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1504
1505 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1506 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1507
1508 if (bounds)
1509 STORE_NATIVE_RECT (*bounds,
1510 FRAME_COL_TO_PIXEL_X (f, pix_x),
1511 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1512 FRAME_COLUMN_WIDTH (f) - 1,
1513 FRAME_LINE_HEIGHT (f) - 1);
1514
1515 if (!noclip)
1516 {
1517 if (pix_x < 0)
1518 pix_x = 0;
1519 else if (pix_x > FRAME_TOTAL_COLS (f))
1520 pix_x = FRAME_TOTAL_COLS (f);
1521
1522 if (pix_y < 0)
1523 pix_y = 0;
1524 else if (pix_y > FRAME_LINES (f))
1525 pix_y = FRAME_LINES (f);
1526 }
1527 }
1528 #endif
1529
1530 *x = pix_x;
1531 *y = pix_y;
1532 }
1533
1534
1535 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1536 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1537 can't tell the positions because W's display is not up to date,
1538 return 0. */
1539
1540 int
1541 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1542 int *frame_x, int *frame_y)
1543 {
1544 #ifdef HAVE_WINDOW_SYSTEM
1545 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1546 {
1547 int success_p;
1548
1549 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1550 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1551
1552 if (display_completed)
1553 {
1554 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1555 struct glyph *glyph = row->glyphs[TEXT_AREA];
1556 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1557
1558 hpos = row->x;
1559 vpos = row->y;
1560 while (glyph < end)
1561 {
1562 hpos += glyph->pixel_width;
1563 ++glyph;
1564 }
1565
1566 /* If first glyph is partially visible, its first visible position is still 0. */
1567 if (hpos < 0)
1568 hpos = 0;
1569
1570 success_p = 1;
1571 }
1572 else
1573 {
1574 hpos = vpos = 0;
1575 success_p = 0;
1576 }
1577
1578 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1579 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1580 return success_p;
1581 }
1582 #endif
1583
1584 *frame_x = hpos;
1585 *frame_y = vpos;
1586 return 1;
1587 }
1588
1589
1590 /* Find the glyph under window-relative coordinates X/Y in window W.
1591 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1592 strings. Return in *HPOS and *VPOS the row and column number of
1593 the glyph found. Return in *AREA the glyph area containing X.
1594 Value is a pointer to the glyph found or null if X/Y is not on
1595 text, or we can't tell because W's current matrix is not up to
1596 date. */
1597
1598 static
1599 struct glyph *
1600 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1601 int *dx, int *dy, int *area)
1602 {
1603 struct glyph *glyph, *end;
1604 struct glyph_row *row = NULL;
1605 int x0, i;
1606
1607 /* Find row containing Y. Give up if some row is not enabled. */
1608 for (i = 0; i < w->current_matrix->nrows; ++i)
1609 {
1610 row = MATRIX_ROW (w->current_matrix, i);
1611 if (!row->enabled_p)
1612 return NULL;
1613 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1614 break;
1615 }
1616
1617 *vpos = i;
1618 *hpos = 0;
1619
1620 /* Give up if Y is not in the window. */
1621 if (i == w->current_matrix->nrows)
1622 return NULL;
1623
1624 /* Get the glyph area containing X. */
1625 if (w->pseudo_window_p)
1626 {
1627 *area = TEXT_AREA;
1628 x0 = 0;
1629 }
1630 else
1631 {
1632 if (x < window_box_left_offset (w, TEXT_AREA))
1633 {
1634 *area = LEFT_MARGIN_AREA;
1635 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1636 }
1637 else if (x < window_box_right_offset (w, TEXT_AREA))
1638 {
1639 *area = TEXT_AREA;
1640 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1641 }
1642 else
1643 {
1644 *area = RIGHT_MARGIN_AREA;
1645 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1646 }
1647 }
1648
1649 /* Find glyph containing X. */
1650 glyph = row->glyphs[*area];
1651 end = glyph + row->used[*area];
1652 x -= x0;
1653 while (glyph < end && x >= glyph->pixel_width)
1654 {
1655 x -= glyph->pixel_width;
1656 ++glyph;
1657 }
1658
1659 if (glyph == end)
1660 return NULL;
1661
1662 if (dx)
1663 {
1664 *dx = x;
1665 *dy = y - (row->y + row->ascent - glyph->ascent);
1666 }
1667
1668 *hpos = glyph - row->glyphs[*area];
1669 return glyph;
1670 }
1671
1672 /* EXPORT:
1673 Convert frame-relative x/y to coordinates relative to window W.
1674 Takes pseudo-windows into account. */
1675
1676 void
1677 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1678 {
1679 if (w->pseudo_window_p)
1680 {
1681 /* A pseudo-window is always full-width, and starts at the
1682 left edge of the frame, plus a frame border. */
1683 struct frame *f = XFRAME (w->frame);
1684 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1685 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1686 }
1687 else
1688 {
1689 *x -= WINDOW_LEFT_EDGE_X (w);
1690 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1691 }
1692 }
1693
1694 #ifdef HAVE_WINDOW_SYSTEM
1695
1696 /* EXPORT:
1697 Return in RECTS[] at most N clipping rectangles for glyph string S.
1698 Return the number of stored rectangles. */
1699
1700 int
1701 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1702 {
1703 XRectangle r;
1704
1705 if (n <= 0)
1706 return 0;
1707
1708 if (s->row->full_width_p)
1709 {
1710 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1711 r.x = WINDOW_LEFT_EDGE_X (s->w);
1712 r.width = WINDOW_TOTAL_WIDTH (s->w);
1713
1714 /* Unless displaying a mode or menu bar line, which are always
1715 fully visible, clip to the visible part of the row. */
1716 if (s->w->pseudo_window_p)
1717 r.height = s->row->visible_height;
1718 else
1719 r.height = s->height;
1720 }
1721 else
1722 {
1723 /* This is a text line that may be partially visible. */
1724 r.x = window_box_left (s->w, s->area);
1725 r.width = window_box_width (s->w, s->area);
1726 r.height = s->row->visible_height;
1727 }
1728
1729 if (s->clip_head)
1730 if (r.x < s->clip_head->x)
1731 {
1732 if (r.width >= s->clip_head->x - r.x)
1733 r.width -= s->clip_head->x - r.x;
1734 else
1735 r.width = 0;
1736 r.x = s->clip_head->x;
1737 }
1738 if (s->clip_tail)
1739 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1740 {
1741 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1742 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1743 else
1744 r.width = 0;
1745 }
1746
1747 /* If S draws overlapping rows, it's sufficient to use the top and
1748 bottom of the window for clipping because this glyph string
1749 intentionally draws over other lines. */
1750 if (s->for_overlaps)
1751 {
1752 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1753 r.height = window_text_bottom_y (s->w) - r.y;
1754
1755 /* Alas, the above simple strategy does not work for the
1756 environments with anti-aliased text: if the same text is
1757 drawn onto the same place multiple times, it gets thicker.
1758 If the overlap we are processing is for the erased cursor, we
1759 take the intersection with the rectagle of the cursor. */
1760 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1761 {
1762 XRectangle rc, r_save = r;
1763
1764 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1765 rc.y = s->w->phys_cursor.y;
1766 rc.width = s->w->phys_cursor_width;
1767 rc.height = s->w->phys_cursor_height;
1768
1769 x_intersect_rectangles (&r_save, &rc, &r);
1770 }
1771 }
1772 else
1773 {
1774 /* Don't use S->y for clipping because it doesn't take partially
1775 visible lines into account. For example, it can be negative for
1776 partially visible lines at the top of a window. */
1777 if (!s->row->full_width_p
1778 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1779 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1780 else
1781 r.y = max (0, s->row->y);
1782 }
1783
1784 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1785
1786 /* If drawing the cursor, don't let glyph draw outside its
1787 advertised boundaries. Cleartype does this under some circumstances. */
1788 if (s->hl == DRAW_CURSOR)
1789 {
1790 struct glyph *glyph = s->first_glyph;
1791 int height, max_y;
1792
1793 if (s->x > r.x)
1794 {
1795 r.width -= s->x - r.x;
1796 r.x = s->x;
1797 }
1798 r.width = min (r.width, glyph->pixel_width);
1799
1800 /* If r.y is below window bottom, ensure that we still see a cursor. */
1801 height = min (glyph->ascent + glyph->descent,
1802 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1803 max_y = window_text_bottom_y (s->w) - height;
1804 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1805 if (s->ybase - glyph->ascent > max_y)
1806 {
1807 r.y = max_y;
1808 r.height = height;
1809 }
1810 else
1811 {
1812 /* Don't draw cursor glyph taller than our actual glyph. */
1813 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1814 if (height < r.height)
1815 {
1816 max_y = r.y + r.height;
1817 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1818 r.height = min (max_y - r.y, height);
1819 }
1820 }
1821 }
1822
1823 if (s->row->clip)
1824 {
1825 XRectangle r_save = r;
1826
1827 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1828 r.width = 0;
1829 }
1830
1831 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1832 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1833 {
1834 #ifdef CONVERT_FROM_XRECT
1835 CONVERT_FROM_XRECT (r, *rects);
1836 #else
1837 *rects = r;
1838 #endif
1839 return 1;
1840 }
1841 else
1842 {
1843 /* If we are processing overlapping and allowed to return
1844 multiple clipping rectangles, we exclude the row of the glyph
1845 string from the clipping rectangle. This is to avoid drawing
1846 the same text on the environment with anti-aliasing. */
1847 #ifdef CONVERT_FROM_XRECT
1848 XRectangle rs[2];
1849 #else
1850 XRectangle *rs = rects;
1851 #endif
1852 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1853
1854 if (s->for_overlaps & OVERLAPS_PRED)
1855 {
1856 rs[i] = r;
1857 if (r.y + r.height > row_y)
1858 {
1859 if (r.y < row_y)
1860 rs[i].height = row_y - r.y;
1861 else
1862 rs[i].height = 0;
1863 }
1864 i++;
1865 }
1866 if (s->for_overlaps & OVERLAPS_SUCC)
1867 {
1868 rs[i] = r;
1869 if (r.y < row_y + s->row->visible_height)
1870 {
1871 if (r.y + r.height > row_y + s->row->visible_height)
1872 {
1873 rs[i].y = row_y + s->row->visible_height;
1874 rs[i].height = r.y + r.height - rs[i].y;
1875 }
1876 else
1877 rs[i].height = 0;
1878 }
1879 i++;
1880 }
1881
1882 n = i;
1883 #ifdef CONVERT_FROM_XRECT
1884 for (i = 0; i < n; i++)
1885 CONVERT_FROM_XRECT (rs[i], rects[i]);
1886 #endif
1887 return n;
1888 }
1889 }
1890
1891 /* EXPORT:
1892 Return in *NR the clipping rectangle for glyph string S. */
1893
1894 void
1895 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1896 {
1897 get_glyph_string_clip_rects (s, nr, 1);
1898 }
1899
1900
1901 /* EXPORT:
1902 Return the position and height of the phys cursor in window W.
1903 Set w->phys_cursor_width to width of phys cursor.
1904 */
1905
1906 void
1907 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1908 struct glyph *glyph, int *xp, int *yp, int *heightp)
1909 {
1910 struct frame *f = XFRAME (WINDOW_FRAME (w));
1911 int x, y, wd, h, h0, y0;
1912
1913 /* Compute the width of the rectangle to draw. If on a stretch
1914 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1915 rectangle as wide as the glyph, but use a canonical character
1916 width instead. */
1917 wd = glyph->pixel_width - 1;
1918 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1919 wd++; /* Why? */
1920 #endif
1921
1922 x = w->phys_cursor.x;
1923 if (x < 0)
1924 {
1925 wd += x;
1926 x = 0;
1927 }
1928
1929 if (glyph->type == STRETCH_GLYPH
1930 && !x_stretch_cursor_p)
1931 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1932 w->phys_cursor_width = wd;
1933
1934 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1935
1936 /* If y is below window bottom, ensure that we still see a cursor. */
1937 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1938
1939 h = max (h0, glyph->ascent + glyph->descent);
1940 h0 = min (h0, glyph->ascent + glyph->descent);
1941
1942 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1943 if (y < y0)
1944 {
1945 h = max (h - (y0 - y) + 1, h0);
1946 y = y0 - 1;
1947 }
1948 else
1949 {
1950 y0 = window_text_bottom_y (w) - h0;
1951 if (y > y0)
1952 {
1953 h += y - y0;
1954 y = y0;
1955 }
1956 }
1957
1958 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1959 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1960 *heightp = h;
1961 }
1962
1963 /*
1964 * Remember which glyph the mouse is over.
1965 */
1966
1967 void
1968 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1969 {
1970 Lisp_Object window;
1971 struct window *w;
1972 struct glyph_row *r, *gr, *end_row;
1973 enum window_part part;
1974 enum glyph_row_area area;
1975 int x, y, width, height;
1976
1977 /* Try to determine frame pixel position and size of the glyph under
1978 frame pixel coordinates X/Y on frame F. */
1979
1980 if (!f->glyphs_initialized_p
1981 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1982 NILP (window)))
1983 {
1984 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1985 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1986 goto virtual_glyph;
1987 }
1988
1989 w = XWINDOW (window);
1990 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1991 height = WINDOW_FRAME_LINE_HEIGHT (w);
1992
1993 x = window_relative_x_coord (w, part, gx);
1994 y = gy - WINDOW_TOP_EDGE_Y (w);
1995
1996 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1997 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1998
1999 if (w->pseudo_window_p)
2000 {
2001 area = TEXT_AREA;
2002 part = ON_MODE_LINE; /* Don't adjust margin. */
2003 goto text_glyph;
2004 }
2005
2006 switch (part)
2007 {
2008 case ON_LEFT_MARGIN:
2009 area = LEFT_MARGIN_AREA;
2010 goto text_glyph;
2011
2012 case ON_RIGHT_MARGIN:
2013 area = RIGHT_MARGIN_AREA;
2014 goto text_glyph;
2015
2016 case ON_HEADER_LINE:
2017 case ON_MODE_LINE:
2018 gr = (part == ON_HEADER_LINE
2019 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2020 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2021 gy = gr->y;
2022 area = TEXT_AREA;
2023 goto text_glyph_row_found;
2024
2025 case ON_TEXT:
2026 area = TEXT_AREA;
2027
2028 text_glyph:
2029 gr = 0; gy = 0;
2030 for (; r <= end_row && r->enabled_p; ++r)
2031 if (r->y + r->height > y)
2032 {
2033 gr = r; gy = r->y;
2034 break;
2035 }
2036
2037 text_glyph_row_found:
2038 if (gr && gy <= y)
2039 {
2040 struct glyph *g = gr->glyphs[area];
2041 struct glyph *end = g + gr->used[area];
2042
2043 height = gr->height;
2044 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2045 if (gx + g->pixel_width > x)
2046 break;
2047
2048 if (g < end)
2049 {
2050 if (g->type == IMAGE_GLYPH)
2051 {
2052 /* Don't remember when mouse is over image, as
2053 image may have hot-spots. */
2054 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2055 return;
2056 }
2057 width = g->pixel_width;
2058 }
2059 else
2060 {
2061 /* Use nominal char spacing at end of line. */
2062 x -= gx;
2063 gx += (x / width) * width;
2064 }
2065
2066 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2067 gx += window_box_left_offset (w, area);
2068 }
2069 else
2070 {
2071 /* Use nominal line height at end of window. */
2072 gx = (x / width) * width;
2073 y -= gy;
2074 gy += (y / height) * height;
2075 }
2076 break;
2077
2078 case ON_LEFT_FRINGE:
2079 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2080 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2081 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2082 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2083 goto row_glyph;
2084
2085 case ON_RIGHT_FRINGE:
2086 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2087 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2088 : window_box_right_offset (w, TEXT_AREA));
2089 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2090 goto row_glyph;
2091
2092 case ON_SCROLL_BAR:
2093 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2094 ? 0
2095 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2096 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2097 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2098 : 0)));
2099 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2100
2101 row_glyph:
2102 gr = 0, gy = 0;
2103 for (; r <= end_row && r->enabled_p; ++r)
2104 if (r->y + r->height > y)
2105 {
2106 gr = r; gy = r->y;
2107 break;
2108 }
2109
2110 if (gr && gy <= y)
2111 height = gr->height;
2112 else
2113 {
2114 /* Use nominal line height at end of window. */
2115 y -= gy;
2116 gy += (y / height) * height;
2117 }
2118 break;
2119
2120 default:
2121 ;
2122 virtual_glyph:
2123 /* If there is no glyph under the mouse, then we divide the screen
2124 into a grid of the smallest glyph in the frame, and use that
2125 as our "glyph". */
2126
2127 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2128 round down even for negative values. */
2129 if (gx < 0)
2130 gx -= width - 1;
2131 if (gy < 0)
2132 gy -= height - 1;
2133
2134 gx = (gx / width) * width;
2135 gy = (gy / height) * height;
2136
2137 goto store_rect;
2138 }
2139
2140 gx += WINDOW_LEFT_EDGE_X (w);
2141 gy += WINDOW_TOP_EDGE_Y (w);
2142
2143 store_rect:
2144 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2145
2146 /* Visible feedback for debugging. */
2147 #if 0
2148 #if HAVE_X_WINDOWS
2149 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2150 f->output_data.x->normal_gc,
2151 gx, gy, width, height);
2152 #endif
2153 #endif
2154 }
2155
2156
2157 #endif /* HAVE_WINDOW_SYSTEM */
2158
2159 \f
2160 /***********************************************************************
2161 Lisp form evaluation
2162 ***********************************************************************/
2163
2164 /* Error handler for safe_eval and safe_call. */
2165
2166 static Lisp_Object
2167 safe_eval_handler (Lisp_Object arg)
2168 {
2169 add_to_log ("Error during redisplay: %S", arg, Qnil);
2170 return Qnil;
2171 }
2172
2173
2174 /* Evaluate SEXPR and return the result, or nil if something went
2175 wrong. Prevent redisplay during the evaluation. */
2176
2177 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2178 Return the result, or nil if something went wrong. Prevent
2179 redisplay during the evaluation. */
2180
2181 Lisp_Object
2182 safe_call (size_t nargs, Lisp_Object *args)
2183 {
2184 Lisp_Object val;
2185
2186 if (inhibit_eval_during_redisplay)
2187 val = Qnil;
2188 else
2189 {
2190 int count = SPECPDL_INDEX ();
2191 struct gcpro gcpro1;
2192
2193 GCPRO1 (args[0]);
2194 gcpro1.nvars = nargs;
2195 specbind (Qinhibit_redisplay, Qt);
2196 /* Use Qt to ensure debugger does not run,
2197 so there is no possibility of wanting to redisplay. */
2198 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2199 safe_eval_handler);
2200 UNGCPRO;
2201 val = unbind_to (count, val);
2202 }
2203
2204 return val;
2205 }
2206
2207
2208 /* Call function FN with one argument ARG.
2209 Return the result, or nil if something went wrong. */
2210
2211 Lisp_Object
2212 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2213 {
2214 Lisp_Object args[2];
2215 args[0] = fn;
2216 args[1] = arg;
2217 return safe_call (2, args);
2218 }
2219
2220 static Lisp_Object Qeval;
2221
2222 Lisp_Object
2223 safe_eval (Lisp_Object sexpr)
2224 {
2225 return safe_call1 (Qeval, sexpr);
2226 }
2227
2228 /* Call function FN with one argument ARG.
2229 Return the result, or nil if something went wrong. */
2230
2231 Lisp_Object
2232 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2233 {
2234 Lisp_Object args[3];
2235 args[0] = fn;
2236 args[1] = arg1;
2237 args[2] = arg2;
2238 return safe_call (3, args);
2239 }
2240
2241
2242 \f
2243 /***********************************************************************
2244 Debugging
2245 ***********************************************************************/
2246
2247 #if 0
2248
2249 /* Define CHECK_IT to perform sanity checks on iterators.
2250 This is for debugging. It is too slow to do unconditionally. */
2251
2252 static void
2253 check_it (it)
2254 struct it *it;
2255 {
2256 if (it->method == GET_FROM_STRING)
2257 {
2258 xassert (STRINGP (it->string));
2259 xassert (IT_STRING_CHARPOS (*it) >= 0);
2260 }
2261 else
2262 {
2263 xassert (IT_STRING_CHARPOS (*it) < 0);
2264 if (it->method == GET_FROM_BUFFER)
2265 {
2266 /* Check that character and byte positions agree. */
2267 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2268 }
2269 }
2270
2271 if (it->dpvec)
2272 xassert (it->current.dpvec_index >= 0);
2273 else
2274 xassert (it->current.dpvec_index < 0);
2275 }
2276
2277 #define CHECK_IT(IT) check_it ((IT))
2278
2279 #else /* not 0 */
2280
2281 #define CHECK_IT(IT) (void) 0
2282
2283 #endif /* not 0 */
2284
2285
2286 #if GLYPH_DEBUG
2287
2288 /* Check that the window end of window W is what we expect it
2289 to be---the last row in the current matrix displaying text. */
2290
2291 static void
2292 check_window_end (w)
2293 struct window *w;
2294 {
2295 if (!MINI_WINDOW_P (w)
2296 && !NILP (w->window_end_valid))
2297 {
2298 struct glyph_row *row;
2299 xassert ((row = MATRIX_ROW (w->current_matrix,
2300 XFASTINT (w->window_end_vpos)),
2301 !row->enabled_p
2302 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2303 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2304 }
2305 }
2306
2307 #define CHECK_WINDOW_END(W) check_window_end ((W))
2308
2309 #else /* not GLYPH_DEBUG */
2310
2311 #define CHECK_WINDOW_END(W) (void) 0
2312
2313 #endif /* not GLYPH_DEBUG */
2314
2315
2316 \f
2317 /***********************************************************************
2318 Iterator initialization
2319 ***********************************************************************/
2320
2321 /* Initialize IT for displaying current_buffer in window W, starting
2322 at character position CHARPOS. CHARPOS < 0 means that no buffer
2323 position is specified which is useful when the iterator is assigned
2324 a position later. BYTEPOS is the byte position corresponding to
2325 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2326
2327 If ROW is not null, calls to produce_glyphs with IT as parameter
2328 will produce glyphs in that row.
2329
2330 BASE_FACE_ID is the id of a base face to use. It must be one of
2331 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2332 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2333 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2334
2335 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2336 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2337 will be initialized to use the corresponding mode line glyph row of
2338 the desired matrix of W. */
2339
2340 void
2341 init_iterator (struct it *it, struct window *w,
2342 EMACS_INT charpos, EMACS_INT bytepos,
2343 struct glyph_row *row, enum face_id base_face_id)
2344 {
2345 int highlight_region_p;
2346 enum face_id remapped_base_face_id = base_face_id;
2347
2348 /* Some precondition checks. */
2349 xassert (w != NULL && it != NULL);
2350 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2351 && charpos <= ZV));
2352
2353 /* If face attributes have been changed since the last redisplay,
2354 free realized faces now because they depend on face definitions
2355 that might have changed. Don't free faces while there might be
2356 desired matrices pending which reference these faces. */
2357 if (face_change_count && !inhibit_free_realized_faces)
2358 {
2359 face_change_count = 0;
2360 free_all_realized_faces (Qnil);
2361 }
2362
2363 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2364 if (! NILP (Vface_remapping_alist))
2365 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2366
2367 /* Use one of the mode line rows of W's desired matrix if
2368 appropriate. */
2369 if (row == NULL)
2370 {
2371 if (base_face_id == MODE_LINE_FACE_ID
2372 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2373 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2374 else if (base_face_id == HEADER_LINE_FACE_ID)
2375 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2376 }
2377
2378 /* Clear IT. */
2379 memset (it, 0, sizeof *it);
2380 it->current.overlay_string_index = -1;
2381 it->current.dpvec_index = -1;
2382 it->base_face_id = remapped_base_face_id;
2383 it->string = Qnil;
2384 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2385
2386 /* The window in which we iterate over current_buffer: */
2387 XSETWINDOW (it->window, w);
2388 it->w = w;
2389 it->f = XFRAME (w->frame);
2390
2391 it->cmp_it.id = -1;
2392
2393 /* Extra space between lines (on window systems only). */
2394 if (base_face_id == DEFAULT_FACE_ID
2395 && FRAME_WINDOW_P (it->f))
2396 {
2397 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2398 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2399 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2400 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2401 * FRAME_LINE_HEIGHT (it->f));
2402 else if (it->f->extra_line_spacing > 0)
2403 it->extra_line_spacing = it->f->extra_line_spacing;
2404 it->max_extra_line_spacing = 0;
2405 }
2406
2407 /* If realized faces have been removed, e.g. because of face
2408 attribute changes of named faces, recompute them. When running
2409 in batch mode, the face cache of the initial frame is null. If
2410 we happen to get called, make a dummy face cache. */
2411 if (FRAME_FACE_CACHE (it->f) == NULL)
2412 init_frame_faces (it->f);
2413 if (FRAME_FACE_CACHE (it->f)->used == 0)
2414 recompute_basic_faces (it->f);
2415
2416 /* Current value of the `slice', `space-width', and 'height' properties. */
2417 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2418 it->space_width = Qnil;
2419 it->font_height = Qnil;
2420 it->override_ascent = -1;
2421
2422 /* Are control characters displayed as `^C'? */
2423 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2424
2425 /* -1 means everything between a CR and the following line end
2426 is invisible. >0 means lines indented more than this value are
2427 invisible. */
2428 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2429 ? XFASTINT (BVAR (current_buffer, selective_display))
2430 : (!NILP (BVAR (current_buffer, selective_display))
2431 ? -1 : 0));
2432 it->selective_display_ellipsis_p
2433 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2434
2435 /* Display table to use. */
2436 it->dp = window_display_table (w);
2437
2438 /* Are multibyte characters enabled in current_buffer? */
2439 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2440
2441 /* Do we need to reorder bidirectional text? Not if this is a
2442 unibyte buffer: by definition, none of the single-byte characters
2443 are strong R2L, so no reordering is needed. And bidi.c doesn't
2444 support unibyte buffers anyway. */
2445 it->bidi_p
2446 = !NILP (BVAR (current_buffer, bidi_display_reordering)) && it->multibyte_p;
2447
2448 /* Non-zero if we should highlight the region. */
2449 highlight_region_p
2450 = (!NILP (Vtransient_mark_mode)
2451 && !NILP (BVAR (current_buffer, mark_active))
2452 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2453
2454 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2455 start and end of a visible region in window IT->w. Set both to
2456 -1 to indicate no region. */
2457 if (highlight_region_p
2458 /* Maybe highlight only in selected window. */
2459 && (/* Either show region everywhere. */
2460 highlight_nonselected_windows
2461 /* Or show region in the selected window. */
2462 || w == XWINDOW (selected_window)
2463 /* Or show the region if we are in the mini-buffer and W is
2464 the window the mini-buffer refers to. */
2465 || (MINI_WINDOW_P (XWINDOW (selected_window))
2466 && WINDOWP (minibuf_selected_window)
2467 && w == XWINDOW (minibuf_selected_window))))
2468 {
2469 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2470 it->region_beg_charpos = min (PT, markpos);
2471 it->region_end_charpos = max (PT, markpos);
2472 }
2473 else
2474 it->region_beg_charpos = it->region_end_charpos = -1;
2475
2476 /* Get the position at which the redisplay_end_trigger hook should
2477 be run, if it is to be run at all. */
2478 if (MARKERP (w->redisplay_end_trigger)
2479 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2480 it->redisplay_end_trigger_charpos
2481 = marker_position (w->redisplay_end_trigger);
2482 else if (INTEGERP (w->redisplay_end_trigger))
2483 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2484
2485 /* Correct bogus values of tab_width. */
2486 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2487 if (it->tab_width <= 0 || it->tab_width > 1000)
2488 it->tab_width = 8;
2489
2490 /* Are lines in the display truncated? */
2491 if (base_face_id != DEFAULT_FACE_ID
2492 || XINT (it->w->hscroll)
2493 || (! WINDOW_FULL_WIDTH_P (it->w)
2494 && ((!NILP (Vtruncate_partial_width_windows)
2495 && !INTEGERP (Vtruncate_partial_width_windows))
2496 || (INTEGERP (Vtruncate_partial_width_windows)
2497 && (WINDOW_TOTAL_COLS (it->w)
2498 < XINT (Vtruncate_partial_width_windows))))))
2499 it->line_wrap = TRUNCATE;
2500 else if (NILP (BVAR (current_buffer, truncate_lines)))
2501 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2502 ? WINDOW_WRAP : WORD_WRAP;
2503 else
2504 it->line_wrap = TRUNCATE;
2505
2506 /* Get dimensions of truncation and continuation glyphs. These are
2507 displayed as fringe bitmaps under X, so we don't need them for such
2508 frames. */
2509 if (!FRAME_WINDOW_P (it->f))
2510 {
2511 if (it->line_wrap == TRUNCATE)
2512 {
2513 /* We will need the truncation glyph. */
2514 xassert (it->glyph_row == NULL);
2515 produce_special_glyphs (it, IT_TRUNCATION);
2516 it->truncation_pixel_width = it->pixel_width;
2517 }
2518 else
2519 {
2520 /* We will need the continuation glyph. */
2521 xassert (it->glyph_row == NULL);
2522 produce_special_glyphs (it, IT_CONTINUATION);
2523 it->continuation_pixel_width = it->pixel_width;
2524 }
2525
2526 /* Reset these values to zero because the produce_special_glyphs
2527 above has changed them. */
2528 it->pixel_width = it->ascent = it->descent = 0;
2529 it->phys_ascent = it->phys_descent = 0;
2530 }
2531
2532 /* Set this after getting the dimensions of truncation and
2533 continuation glyphs, so that we don't produce glyphs when calling
2534 produce_special_glyphs, above. */
2535 it->glyph_row = row;
2536 it->area = TEXT_AREA;
2537
2538 /* Forget any previous info about this row being reversed. */
2539 if (it->glyph_row)
2540 it->glyph_row->reversed_p = 0;
2541
2542 /* Get the dimensions of the display area. The display area
2543 consists of the visible window area plus a horizontally scrolled
2544 part to the left of the window. All x-values are relative to the
2545 start of this total display area. */
2546 if (base_face_id != DEFAULT_FACE_ID)
2547 {
2548 /* Mode lines, menu bar in terminal frames. */
2549 it->first_visible_x = 0;
2550 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2551 }
2552 else
2553 {
2554 it->first_visible_x
2555 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2556 it->last_visible_x = (it->first_visible_x
2557 + window_box_width (w, TEXT_AREA));
2558
2559 /* If we truncate lines, leave room for the truncator glyph(s) at
2560 the right margin. Otherwise, leave room for the continuation
2561 glyph(s). Truncation and continuation glyphs are not inserted
2562 for window-based redisplay. */
2563 if (!FRAME_WINDOW_P (it->f))
2564 {
2565 if (it->line_wrap == TRUNCATE)
2566 it->last_visible_x -= it->truncation_pixel_width;
2567 else
2568 it->last_visible_x -= it->continuation_pixel_width;
2569 }
2570
2571 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2572 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2573 }
2574
2575 /* Leave room for a border glyph. */
2576 if (!FRAME_WINDOW_P (it->f)
2577 && !WINDOW_RIGHTMOST_P (it->w))
2578 it->last_visible_x -= 1;
2579
2580 it->last_visible_y = window_text_bottom_y (w);
2581
2582 /* For mode lines and alike, arrange for the first glyph having a
2583 left box line if the face specifies a box. */
2584 if (base_face_id != DEFAULT_FACE_ID)
2585 {
2586 struct face *face;
2587
2588 it->face_id = remapped_base_face_id;
2589
2590 /* If we have a boxed mode line, make the first character appear
2591 with a left box line. */
2592 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2593 if (face->box != FACE_NO_BOX)
2594 it->start_of_box_run_p = 1;
2595 }
2596
2597 /* If we are to reorder bidirectional text, init the bidi
2598 iterator. */
2599 if (it->bidi_p)
2600 {
2601 /* Note the paragraph direction that this buffer wants to
2602 use. */
2603 if (EQ (BVAR (current_buffer, bidi_paragraph_direction), Qleft_to_right))
2604 it->paragraph_embedding = L2R;
2605 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction), Qright_to_left))
2606 it->paragraph_embedding = R2L;
2607 else
2608 it->paragraph_embedding = NEUTRAL_DIR;
2609 bidi_init_it (charpos, bytepos, &it->bidi_it);
2610 }
2611
2612 /* If a buffer position was specified, set the iterator there,
2613 getting overlays and face properties from that position. */
2614 if (charpos >= BUF_BEG (current_buffer))
2615 {
2616 it->end_charpos = ZV;
2617 it->face_id = -1;
2618 IT_CHARPOS (*it) = charpos;
2619
2620 /* Compute byte position if not specified. */
2621 if (bytepos < charpos)
2622 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2623 else
2624 IT_BYTEPOS (*it) = bytepos;
2625
2626 it->start = it->current;
2627
2628 /* Compute faces etc. */
2629 reseat (it, it->current.pos, 1);
2630 }
2631
2632 CHECK_IT (it);
2633 }
2634
2635
2636 /* Initialize IT for the display of window W with window start POS. */
2637
2638 void
2639 start_display (struct it *it, struct window *w, struct text_pos pos)
2640 {
2641 struct glyph_row *row;
2642 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2643
2644 row = w->desired_matrix->rows + first_vpos;
2645 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2646 it->first_vpos = first_vpos;
2647
2648 /* Don't reseat to previous visible line start if current start
2649 position is in a string or image. */
2650 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2651 {
2652 int start_at_line_beg_p;
2653 int first_y = it->current_y;
2654
2655 /* If window start is not at a line start, skip forward to POS to
2656 get the correct continuation lines width. */
2657 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2658 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2659 if (!start_at_line_beg_p)
2660 {
2661 int new_x;
2662
2663 reseat_at_previous_visible_line_start (it);
2664 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2665
2666 new_x = it->current_x + it->pixel_width;
2667
2668 /* If lines are continued, this line may end in the middle
2669 of a multi-glyph character (e.g. a control character
2670 displayed as \003, or in the middle of an overlay
2671 string). In this case move_it_to above will not have
2672 taken us to the start of the continuation line but to the
2673 end of the continued line. */
2674 if (it->current_x > 0
2675 && it->line_wrap != TRUNCATE /* Lines are continued. */
2676 && (/* And glyph doesn't fit on the line. */
2677 new_x > it->last_visible_x
2678 /* Or it fits exactly and we're on a window
2679 system frame. */
2680 || (new_x == it->last_visible_x
2681 && FRAME_WINDOW_P (it->f))))
2682 {
2683 if (it->current.dpvec_index >= 0
2684 || it->current.overlay_string_index >= 0)
2685 {
2686 set_iterator_to_next (it, 1);
2687 move_it_in_display_line_to (it, -1, -1, 0);
2688 }
2689
2690 it->continuation_lines_width += it->current_x;
2691 }
2692
2693 /* We're starting a new display line, not affected by the
2694 height of the continued line, so clear the appropriate
2695 fields in the iterator structure. */
2696 it->max_ascent = it->max_descent = 0;
2697 it->max_phys_ascent = it->max_phys_descent = 0;
2698
2699 it->current_y = first_y;
2700 it->vpos = 0;
2701 it->current_x = it->hpos = 0;
2702 }
2703 }
2704 }
2705
2706
2707 /* Return 1 if POS is a position in ellipses displayed for invisible
2708 text. W is the window we display, for text property lookup. */
2709
2710 static int
2711 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2712 {
2713 Lisp_Object prop, window;
2714 int ellipses_p = 0;
2715 EMACS_INT charpos = CHARPOS (pos->pos);
2716
2717 /* If POS specifies a position in a display vector, this might
2718 be for an ellipsis displayed for invisible text. We won't
2719 get the iterator set up for delivering that ellipsis unless
2720 we make sure that it gets aware of the invisible text. */
2721 if (pos->dpvec_index >= 0
2722 && pos->overlay_string_index < 0
2723 && CHARPOS (pos->string_pos) < 0
2724 && charpos > BEGV
2725 && (XSETWINDOW (window, w),
2726 prop = Fget_char_property (make_number (charpos),
2727 Qinvisible, window),
2728 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2729 {
2730 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2731 window);
2732 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2733 }
2734
2735 return ellipses_p;
2736 }
2737
2738
2739 /* Initialize IT for stepping through current_buffer in window W,
2740 starting at position POS that includes overlay string and display
2741 vector/ control character translation position information. Value
2742 is zero if there are overlay strings with newlines at POS. */
2743
2744 static int
2745 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2746 {
2747 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2748 int i, overlay_strings_with_newlines = 0;
2749
2750 /* If POS specifies a position in a display vector, this might
2751 be for an ellipsis displayed for invisible text. We won't
2752 get the iterator set up for delivering that ellipsis unless
2753 we make sure that it gets aware of the invisible text. */
2754 if (in_ellipses_for_invisible_text_p (pos, w))
2755 {
2756 --charpos;
2757 bytepos = 0;
2758 }
2759
2760 /* Keep in mind: the call to reseat in init_iterator skips invisible
2761 text, so we might end up at a position different from POS. This
2762 is only a problem when POS is a row start after a newline and an
2763 overlay starts there with an after-string, and the overlay has an
2764 invisible property. Since we don't skip invisible text in
2765 display_line and elsewhere immediately after consuming the
2766 newline before the row start, such a POS will not be in a string,
2767 but the call to init_iterator below will move us to the
2768 after-string. */
2769 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2770
2771 /* This only scans the current chunk -- it should scan all chunks.
2772 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2773 to 16 in 22.1 to make this a lesser problem. */
2774 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2775 {
2776 const char *s = SSDATA (it->overlay_strings[i]);
2777 const char *e = s + SBYTES (it->overlay_strings[i]);
2778
2779 while (s < e && *s != '\n')
2780 ++s;
2781
2782 if (s < e)
2783 {
2784 overlay_strings_with_newlines = 1;
2785 break;
2786 }
2787 }
2788
2789 /* If position is within an overlay string, set up IT to the right
2790 overlay string. */
2791 if (pos->overlay_string_index >= 0)
2792 {
2793 int relative_index;
2794
2795 /* If the first overlay string happens to have a `display'
2796 property for an image, the iterator will be set up for that
2797 image, and we have to undo that setup first before we can
2798 correct the overlay string index. */
2799 if (it->method == GET_FROM_IMAGE)
2800 pop_it (it);
2801
2802 /* We already have the first chunk of overlay strings in
2803 IT->overlay_strings. Load more until the one for
2804 pos->overlay_string_index is in IT->overlay_strings. */
2805 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2806 {
2807 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2808 it->current.overlay_string_index = 0;
2809 while (n--)
2810 {
2811 load_overlay_strings (it, 0);
2812 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2813 }
2814 }
2815
2816 it->current.overlay_string_index = pos->overlay_string_index;
2817 relative_index = (it->current.overlay_string_index
2818 % OVERLAY_STRING_CHUNK_SIZE);
2819 it->string = it->overlay_strings[relative_index];
2820 xassert (STRINGP (it->string));
2821 it->current.string_pos = pos->string_pos;
2822 it->method = GET_FROM_STRING;
2823 }
2824
2825 if (CHARPOS (pos->string_pos) >= 0)
2826 {
2827 /* Recorded position is not in an overlay string, but in another
2828 string. This can only be a string from a `display' property.
2829 IT should already be filled with that string. */
2830 it->current.string_pos = pos->string_pos;
2831 xassert (STRINGP (it->string));
2832 }
2833
2834 /* Restore position in display vector translations, control
2835 character translations or ellipses. */
2836 if (pos->dpvec_index >= 0)
2837 {
2838 if (it->dpvec == NULL)
2839 get_next_display_element (it);
2840 xassert (it->dpvec && it->current.dpvec_index == 0);
2841 it->current.dpvec_index = pos->dpvec_index;
2842 }
2843
2844 CHECK_IT (it);
2845 return !overlay_strings_with_newlines;
2846 }
2847
2848
2849 /* Initialize IT for stepping through current_buffer in window W
2850 starting at ROW->start. */
2851
2852 static void
2853 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2854 {
2855 init_from_display_pos (it, w, &row->start);
2856 it->start = row->start;
2857 it->continuation_lines_width = row->continuation_lines_width;
2858 CHECK_IT (it);
2859 }
2860
2861
2862 /* Initialize IT for stepping through current_buffer in window W
2863 starting in the line following ROW, i.e. starting at ROW->end.
2864 Value is zero if there are overlay strings with newlines at ROW's
2865 end position. */
2866
2867 static int
2868 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2869 {
2870 int success = 0;
2871
2872 if (init_from_display_pos (it, w, &row->end))
2873 {
2874 if (row->continued_p)
2875 it->continuation_lines_width
2876 = row->continuation_lines_width + row->pixel_width;
2877 CHECK_IT (it);
2878 success = 1;
2879 }
2880
2881 return success;
2882 }
2883
2884
2885
2886 \f
2887 /***********************************************************************
2888 Text properties
2889 ***********************************************************************/
2890
2891 /* Called when IT reaches IT->stop_charpos. Handle text property and
2892 overlay changes. Set IT->stop_charpos to the next position where
2893 to stop. */
2894
2895 static void
2896 handle_stop (struct it *it)
2897 {
2898 enum prop_handled handled;
2899 int handle_overlay_change_p;
2900 struct props *p;
2901
2902 it->dpvec = NULL;
2903 it->current.dpvec_index = -1;
2904 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2905 it->ignore_overlay_strings_at_pos_p = 0;
2906 it->ellipsis_p = 0;
2907
2908 /* Use face of preceding text for ellipsis (if invisible) */
2909 if (it->selective_display_ellipsis_p)
2910 it->saved_face_id = it->face_id;
2911
2912 do
2913 {
2914 handled = HANDLED_NORMALLY;
2915
2916 /* Call text property handlers. */
2917 for (p = it_props; p->handler; ++p)
2918 {
2919 handled = p->handler (it);
2920
2921 if (handled == HANDLED_RECOMPUTE_PROPS)
2922 break;
2923 else if (handled == HANDLED_RETURN)
2924 {
2925 /* We still want to show before and after strings from
2926 overlays even if the actual buffer text is replaced. */
2927 if (!handle_overlay_change_p
2928 || it->sp > 1
2929 || !get_overlay_strings_1 (it, 0, 0))
2930 {
2931 if (it->ellipsis_p)
2932 setup_for_ellipsis (it, 0);
2933 /* When handling a display spec, we might load an
2934 empty string. In that case, discard it here. We
2935 used to discard it in handle_single_display_spec,
2936 but that causes get_overlay_strings_1, above, to
2937 ignore overlay strings that we must check. */
2938 if (STRINGP (it->string) && !SCHARS (it->string))
2939 pop_it (it);
2940 return;
2941 }
2942 else if (STRINGP (it->string) && !SCHARS (it->string))
2943 pop_it (it);
2944 else
2945 {
2946 it->ignore_overlay_strings_at_pos_p = 1;
2947 it->string_from_display_prop_p = 0;
2948 handle_overlay_change_p = 0;
2949 }
2950 handled = HANDLED_RECOMPUTE_PROPS;
2951 break;
2952 }
2953 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2954 handle_overlay_change_p = 0;
2955 }
2956
2957 if (handled != HANDLED_RECOMPUTE_PROPS)
2958 {
2959 /* Don't check for overlay strings below when set to deliver
2960 characters from a display vector. */
2961 if (it->method == GET_FROM_DISPLAY_VECTOR)
2962 handle_overlay_change_p = 0;
2963
2964 /* Handle overlay changes.
2965 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2966 if it finds overlays. */
2967 if (handle_overlay_change_p)
2968 handled = handle_overlay_change (it);
2969 }
2970
2971 if (it->ellipsis_p)
2972 {
2973 setup_for_ellipsis (it, 0);
2974 break;
2975 }
2976 }
2977 while (handled == HANDLED_RECOMPUTE_PROPS);
2978
2979 /* Determine where to stop next. */
2980 if (handled == HANDLED_NORMALLY)
2981 compute_stop_pos (it);
2982 }
2983
2984
2985 /* Compute IT->stop_charpos from text property and overlay change
2986 information for IT's current position. */
2987
2988 static void
2989 compute_stop_pos (struct it *it)
2990 {
2991 register INTERVAL iv, next_iv;
2992 Lisp_Object object, limit, position;
2993 EMACS_INT charpos, bytepos;
2994
2995 /* If nowhere else, stop at the end. */
2996 it->stop_charpos = it->end_charpos;
2997
2998 if (STRINGP (it->string))
2999 {
3000 /* Strings are usually short, so don't limit the search for
3001 properties. */
3002 object = it->string;
3003 limit = Qnil;
3004 charpos = IT_STRING_CHARPOS (*it);
3005 bytepos = IT_STRING_BYTEPOS (*it);
3006 }
3007 else
3008 {
3009 EMACS_INT pos;
3010
3011 /* If next overlay change is in front of the current stop pos
3012 (which is IT->end_charpos), stop there. Note: value of
3013 next_overlay_change is point-max if no overlay change
3014 follows. */
3015 charpos = IT_CHARPOS (*it);
3016 bytepos = IT_BYTEPOS (*it);
3017 pos = next_overlay_change (charpos);
3018 if (pos < it->stop_charpos)
3019 it->stop_charpos = pos;
3020
3021 /* If showing the region, we have to stop at the region
3022 start or end because the face might change there. */
3023 if (it->region_beg_charpos > 0)
3024 {
3025 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3026 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3027 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3028 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3029 }
3030
3031 /* Set up variables for computing the stop position from text
3032 property changes. */
3033 XSETBUFFER (object, current_buffer);
3034 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3035 }
3036
3037 /* Get the interval containing IT's position. Value is a null
3038 interval if there isn't such an interval. */
3039 position = make_number (charpos);
3040 iv = validate_interval_range (object, &position, &position, 0);
3041 if (!NULL_INTERVAL_P (iv))
3042 {
3043 Lisp_Object values_here[LAST_PROP_IDX];
3044 struct props *p;
3045
3046 /* Get properties here. */
3047 for (p = it_props; p->handler; ++p)
3048 values_here[p->idx] = textget (iv->plist, *p->name);
3049
3050 /* Look for an interval following iv that has different
3051 properties. */
3052 for (next_iv = next_interval (iv);
3053 (!NULL_INTERVAL_P (next_iv)
3054 && (NILP (limit)
3055 || XFASTINT (limit) > next_iv->position));
3056 next_iv = next_interval (next_iv))
3057 {
3058 for (p = it_props; p->handler; ++p)
3059 {
3060 Lisp_Object new_value;
3061
3062 new_value = textget (next_iv->plist, *p->name);
3063 if (!EQ (values_here[p->idx], new_value))
3064 break;
3065 }
3066
3067 if (p->handler)
3068 break;
3069 }
3070
3071 if (!NULL_INTERVAL_P (next_iv))
3072 {
3073 if (INTEGERP (limit)
3074 && next_iv->position >= XFASTINT (limit))
3075 /* No text property change up to limit. */
3076 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3077 else
3078 /* Text properties change in next_iv. */
3079 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3080 }
3081 }
3082
3083 if (it->cmp_it.id < 0)
3084 {
3085 EMACS_INT stoppos = it->end_charpos;
3086
3087 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3088 stoppos = -1;
3089 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3090 stoppos, it->string);
3091 }
3092
3093 xassert (STRINGP (it->string)
3094 || (it->stop_charpos >= BEGV
3095 && it->stop_charpos >= IT_CHARPOS (*it)));
3096 }
3097
3098
3099 /* Return the position of the next overlay change after POS in
3100 current_buffer. Value is point-max if no overlay change
3101 follows. This is like `next-overlay-change' but doesn't use
3102 xmalloc. */
3103
3104 static EMACS_INT
3105 next_overlay_change (EMACS_INT pos)
3106 {
3107 int noverlays;
3108 EMACS_INT endpos;
3109 Lisp_Object *overlays;
3110 int i;
3111
3112 /* Get all overlays at the given position. */
3113 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3114
3115 /* If any of these overlays ends before endpos,
3116 use its ending point instead. */
3117 for (i = 0; i < noverlays; ++i)
3118 {
3119 Lisp_Object oend;
3120 EMACS_INT oendpos;
3121
3122 oend = OVERLAY_END (overlays[i]);
3123 oendpos = OVERLAY_POSITION (oend);
3124 endpos = min (endpos, oendpos);
3125 }
3126
3127 return endpos;
3128 }
3129
3130
3131 \f
3132 /***********************************************************************
3133 Fontification
3134 ***********************************************************************/
3135
3136 /* Handle changes in the `fontified' property of the current buffer by
3137 calling hook functions from Qfontification_functions to fontify
3138 regions of text. */
3139
3140 static enum prop_handled
3141 handle_fontified_prop (struct it *it)
3142 {
3143 Lisp_Object prop, pos;
3144 enum prop_handled handled = HANDLED_NORMALLY;
3145
3146 if (!NILP (Vmemory_full))
3147 return handled;
3148
3149 /* Get the value of the `fontified' property at IT's current buffer
3150 position. (The `fontified' property doesn't have a special
3151 meaning in strings.) If the value is nil, call functions from
3152 Qfontification_functions. */
3153 if (!STRINGP (it->string)
3154 && it->s == NULL
3155 && !NILP (Vfontification_functions)
3156 && !NILP (Vrun_hooks)
3157 && (pos = make_number (IT_CHARPOS (*it)),
3158 prop = Fget_char_property (pos, Qfontified, Qnil),
3159 /* Ignore the special cased nil value always present at EOB since
3160 no amount of fontifying will be able to change it. */
3161 NILP (prop) && IT_CHARPOS (*it) < Z))
3162 {
3163 int count = SPECPDL_INDEX ();
3164 Lisp_Object val;
3165 struct buffer *obuf = current_buffer;
3166 int begv = BEGV, zv = ZV;
3167 int old_clip_changed = current_buffer->clip_changed;
3168
3169 val = Vfontification_functions;
3170 specbind (Qfontification_functions, Qnil);
3171
3172 xassert (it->end_charpos == ZV);
3173
3174 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3175 safe_call1 (val, pos);
3176 else
3177 {
3178 Lisp_Object fns, fn;
3179 struct gcpro gcpro1, gcpro2;
3180
3181 fns = Qnil;
3182 GCPRO2 (val, fns);
3183
3184 for (; CONSP (val); val = XCDR (val))
3185 {
3186 fn = XCAR (val);
3187
3188 if (EQ (fn, Qt))
3189 {
3190 /* A value of t indicates this hook has a local
3191 binding; it means to run the global binding too.
3192 In a global value, t should not occur. If it
3193 does, we must ignore it to avoid an endless
3194 loop. */
3195 for (fns = Fdefault_value (Qfontification_functions);
3196 CONSP (fns);
3197 fns = XCDR (fns))
3198 {
3199 fn = XCAR (fns);
3200 if (!EQ (fn, Qt))
3201 safe_call1 (fn, pos);
3202 }
3203 }
3204 else
3205 safe_call1 (fn, pos);
3206 }
3207
3208 UNGCPRO;
3209 }
3210
3211 unbind_to (count, Qnil);
3212
3213 /* Fontification functions routinely call `save-restriction'.
3214 Normally, this tags clip_changed, which can confuse redisplay
3215 (see discussion in Bug#6671). Since we don't perform any
3216 special handling of fontification changes in the case where
3217 `save-restriction' isn't called, there's no point doing so in
3218 this case either. So, if the buffer's restrictions are
3219 actually left unchanged, reset clip_changed. */
3220 if (obuf == current_buffer)
3221 {
3222 if (begv == BEGV && zv == ZV)
3223 current_buffer->clip_changed = old_clip_changed;
3224 }
3225 /* There isn't much we can reasonably do to protect against
3226 misbehaving fontification, but here's a fig leaf. */
3227 else if (!NILP (BVAR (obuf, name)))
3228 set_buffer_internal_1 (obuf);
3229
3230 /* The fontification code may have added/removed text.
3231 It could do even a lot worse, but let's at least protect against
3232 the most obvious case where only the text past `pos' gets changed',
3233 as is/was done in grep.el where some escapes sequences are turned
3234 into face properties (bug#7876). */
3235 it->end_charpos = ZV;
3236
3237 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3238 something. This avoids an endless loop if they failed to
3239 fontify the text for which reason ever. */
3240 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3241 handled = HANDLED_RECOMPUTE_PROPS;
3242 }
3243
3244 return handled;
3245 }
3246
3247
3248 \f
3249 /***********************************************************************
3250 Faces
3251 ***********************************************************************/
3252
3253 /* Set up iterator IT from face properties at its current position.
3254 Called from handle_stop. */
3255
3256 static enum prop_handled
3257 handle_face_prop (struct it *it)
3258 {
3259 int new_face_id;
3260 EMACS_INT next_stop;
3261
3262 if (!STRINGP (it->string))
3263 {
3264 new_face_id
3265 = face_at_buffer_position (it->w,
3266 IT_CHARPOS (*it),
3267 it->region_beg_charpos,
3268 it->region_end_charpos,
3269 &next_stop,
3270 (IT_CHARPOS (*it)
3271 + TEXT_PROP_DISTANCE_LIMIT),
3272 0, it->base_face_id);
3273
3274 /* Is this a start of a run of characters with box face?
3275 Caveat: this can be called for a freshly initialized
3276 iterator; face_id is -1 in this case. We know that the new
3277 face will not change until limit, i.e. if the new face has a
3278 box, all characters up to limit will have one. But, as
3279 usual, we don't know whether limit is really the end. */
3280 if (new_face_id != it->face_id)
3281 {
3282 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3283
3284 /* If new face has a box but old face has not, this is
3285 the start of a run of characters with box, i.e. it has
3286 a shadow on the left side. The value of face_id of the
3287 iterator will be -1 if this is the initial call that gets
3288 the face. In this case, we have to look in front of IT's
3289 position and see whether there is a face != new_face_id. */
3290 it->start_of_box_run_p
3291 = (new_face->box != FACE_NO_BOX
3292 && (it->face_id >= 0
3293 || IT_CHARPOS (*it) == BEG
3294 || new_face_id != face_before_it_pos (it)));
3295 it->face_box_p = new_face->box != FACE_NO_BOX;
3296 }
3297 }
3298 else
3299 {
3300 int base_face_id;
3301 EMACS_INT bufpos;
3302 int i;
3303 Lisp_Object from_overlay
3304 = (it->current.overlay_string_index >= 0
3305 ? it->string_overlays[it->current.overlay_string_index]
3306 : Qnil);
3307
3308 /* See if we got to this string directly or indirectly from
3309 an overlay property. That includes the before-string or
3310 after-string of an overlay, strings in display properties
3311 provided by an overlay, their text properties, etc.
3312
3313 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3314 if (! NILP (from_overlay))
3315 for (i = it->sp - 1; i >= 0; i--)
3316 {
3317 if (it->stack[i].current.overlay_string_index >= 0)
3318 from_overlay
3319 = it->string_overlays[it->stack[i].current.overlay_string_index];
3320 else if (! NILP (it->stack[i].from_overlay))
3321 from_overlay = it->stack[i].from_overlay;
3322
3323 if (!NILP (from_overlay))
3324 break;
3325 }
3326
3327 if (! NILP (from_overlay))
3328 {
3329 bufpos = IT_CHARPOS (*it);
3330 /* For a string from an overlay, the base face depends
3331 only on text properties and ignores overlays. */
3332 base_face_id
3333 = face_for_overlay_string (it->w,
3334 IT_CHARPOS (*it),
3335 it->region_beg_charpos,
3336 it->region_end_charpos,
3337 &next_stop,
3338 (IT_CHARPOS (*it)
3339 + TEXT_PROP_DISTANCE_LIMIT),
3340 0,
3341 from_overlay);
3342 }
3343 else
3344 {
3345 bufpos = 0;
3346
3347 /* For strings from a `display' property, use the face at
3348 IT's current buffer position as the base face to merge
3349 with, so that overlay strings appear in the same face as
3350 surrounding text, unless they specify their own
3351 faces. */
3352 base_face_id = underlying_face_id (it);
3353 }
3354
3355 new_face_id = face_at_string_position (it->w,
3356 it->string,
3357 IT_STRING_CHARPOS (*it),
3358 bufpos,
3359 it->region_beg_charpos,
3360 it->region_end_charpos,
3361 &next_stop,
3362 base_face_id, 0);
3363
3364 /* Is this a start of a run of characters with box? Caveat:
3365 this can be called for a freshly allocated iterator; face_id
3366 is -1 is this case. We know that the new face will not
3367 change until the next check pos, i.e. if the new face has a
3368 box, all characters up to that position will have a
3369 box. But, as usual, we don't know whether that position
3370 is really the end. */
3371 if (new_face_id != it->face_id)
3372 {
3373 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3374 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3375
3376 /* If new face has a box but old face hasn't, this is the
3377 start of a run of characters with box, i.e. it has a
3378 shadow on the left side. */
3379 it->start_of_box_run_p
3380 = new_face->box && (old_face == NULL || !old_face->box);
3381 it->face_box_p = new_face->box != FACE_NO_BOX;
3382 }
3383 }
3384
3385 it->face_id = new_face_id;
3386 return HANDLED_NORMALLY;
3387 }
3388
3389
3390 /* Return the ID of the face ``underlying'' IT's current position,
3391 which is in a string. If the iterator is associated with a
3392 buffer, return the face at IT's current buffer position.
3393 Otherwise, use the iterator's base_face_id. */
3394
3395 static int
3396 underlying_face_id (struct it *it)
3397 {
3398 int face_id = it->base_face_id, i;
3399
3400 xassert (STRINGP (it->string));
3401
3402 for (i = it->sp - 1; i >= 0; --i)
3403 if (NILP (it->stack[i].string))
3404 face_id = it->stack[i].face_id;
3405
3406 return face_id;
3407 }
3408
3409
3410 /* Compute the face one character before or after the current position
3411 of IT. BEFORE_P non-zero means get the face in front of IT's
3412 position. Value is the id of the face. */
3413
3414 static int
3415 face_before_or_after_it_pos (struct it *it, int before_p)
3416 {
3417 int face_id, limit;
3418 EMACS_INT next_check_charpos;
3419 struct text_pos pos;
3420
3421 xassert (it->s == NULL);
3422
3423 if (STRINGP (it->string))
3424 {
3425 EMACS_INT bufpos;
3426 int base_face_id;
3427
3428 /* No face change past the end of the string (for the case
3429 we are padding with spaces). No face change before the
3430 string start. */
3431 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3432 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3433 return it->face_id;
3434
3435 /* Set pos to the position before or after IT's current position. */
3436 if (before_p)
3437 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3438 else
3439 /* For composition, we must check the character after the
3440 composition. */
3441 pos = (it->what == IT_COMPOSITION
3442 ? string_pos (IT_STRING_CHARPOS (*it)
3443 + it->cmp_it.nchars, it->string)
3444 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3445
3446 if (it->current.overlay_string_index >= 0)
3447 bufpos = IT_CHARPOS (*it);
3448 else
3449 bufpos = 0;
3450
3451 base_face_id = underlying_face_id (it);
3452
3453 /* Get the face for ASCII, or unibyte. */
3454 face_id = face_at_string_position (it->w,
3455 it->string,
3456 CHARPOS (pos),
3457 bufpos,
3458 it->region_beg_charpos,
3459 it->region_end_charpos,
3460 &next_check_charpos,
3461 base_face_id, 0);
3462
3463 /* Correct the face for charsets different from ASCII. Do it
3464 for the multibyte case only. The face returned above is
3465 suitable for unibyte text if IT->string is unibyte. */
3466 if (STRING_MULTIBYTE (it->string))
3467 {
3468 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3469 int c, len;
3470 struct face *face = FACE_FROM_ID (it->f, face_id);
3471
3472 c = string_char_and_length (p, &len);
3473 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3474 }
3475 }
3476 else
3477 {
3478 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3479 || (IT_CHARPOS (*it) <= BEGV && before_p))
3480 return it->face_id;
3481
3482 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3483 pos = it->current.pos;
3484
3485 if (before_p)
3486 DEC_TEXT_POS (pos, it->multibyte_p);
3487 else
3488 {
3489 if (it->what == IT_COMPOSITION)
3490 /* For composition, we must check the position after the
3491 composition. */
3492 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3493 else
3494 INC_TEXT_POS (pos, it->multibyte_p);
3495 }
3496
3497 /* Determine face for CHARSET_ASCII, or unibyte. */
3498 face_id = face_at_buffer_position (it->w,
3499 CHARPOS (pos),
3500 it->region_beg_charpos,
3501 it->region_end_charpos,
3502 &next_check_charpos,
3503 limit, 0, -1);
3504
3505 /* Correct the face for charsets different from ASCII. Do it
3506 for the multibyte case only. The face returned above is
3507 suitable for unibyte text if current_buffer is unibyte. */
3508 if (it->multibyte_p)
3509 {
3510 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3511 struct face *face = FACE_FROM_ID (it->f, face_id);
3512 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3513 }
3514 }
3515
3516 return face_id;
3517 }
3518
3519
3520 \f
3521 /***********************************************************************
3522 Invisible text
3523 ***********************************************************************/
3524
3525 /* Set up iterator IT from invisible properties at its current
3526 position. Called from handle_stop. */
3527
3528 static enum prop_handled
3529 handle_invisible_prop (struct it *it)
3530 {
3531 enum prop_handled handled = HANDLED_NORMALLY;
3532
3533 if (STRINGP (it->string))
3534 {
3535 Lisp_Object prop, end_charpos, limit, charpos;
3536
3537 /* Get the value of the invisible text property at the
3538 current position. Value will be nil if there is no such
3539 property. */
3540 charpos = make_number (IT_STRING_CHARPOS (*it));
3541 prop = Fget_text_property (charpos, Qinvisible, it->string);
3542
3543 if (!NILP (prop)
3544 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3545 {
3546 handled = HANDLED_RECOMPUTE_PROPS;
3547
3548 /* Get the position at which the next change of the
3549 invisible text property can be found in IT->string.
3550 Value will be nil if the property value is the same for
3551 all the rest of IT->string. */
3552 XSETINT (limit, SCHARS (it->string));
3553 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3554 it->string, limit);
3555
3556 /* Text at current position is invisible. The next
3557 change in the property is at position end_charpos.
3558 Move IT's current position to that position. */
3559 if (INTEGERP (end_charpos)
3560 && XFASTINT (end_charpos) < XFASTINT (limit))
3561 {
3562 struct text_pos old;
3563 old = it->current.string_pos;
3564 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3565 compute_string_pos (&it->current.string_pos, old, it->string);
3566 }
3567 else
3568 {
3569 /* The rest of the string is invisible. If this is an
3570 overlay string, proceed with the next overlay string
3571 or whatever comes and return a character from there. */
3572 if (it->current.overlay_string_index >= 0)
3573 {
3574 next_overlay_string (it);
3575 /* Don't check for overlay strings when we just
3576 finished processing them. */
3577 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3578 }
3579 else
3580 {
3581 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3582 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3583 }
3584 }
3585 }
3586 }
3587 else
3588 {
3589 int invis_p;
3590 EMACS_INT newpos, next_stop, start_charpos, tem;
3591 Lisp_Object pos, prop, overlay;
3592
3593 /* First of all, is there invisible text at this position? */
3594 tem = start_charpos = IT_CHARPOS (*it);
3595 pos = make_number (tem);
3596 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3597 &overlay);
3598 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3599
3600 /* If we are on invisible text, skip over it. */
3601 if (invis_p && start_charpos < it->end_charpos)
3602 {
3603 /* Record whether we have to display an ellipsis for the
3604 invisible text. */
3605 int display_ellipsis_p = invis_p == 2;
3606
3607 handled = HANDLED_RECOMPUTE_PROPS;
3608
3609 /* Loop skipping over invisible text. The loop is left at
3610 ZV or with IT on the first char being visible again. */
3611 do
3612 {
3613 /* Try to skip some invisible text. Return value is the
3614 position reached which can be equal to where we start
3615 if there is nothing invisible there. This skips both
3616 over invisible text properties and overlays with
3617 invisible property. */
3618 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3619
3620 /* If we skipped nothing at all we weren't at invisible
3621 text in the first place. If everything to the end of
3622 the buffer was skipped, end the loop. */
3623 if (newpos == tem || newpos >= ZV)
3624 invis_p = 0;
3625 else
3626 {
3627 /* We skipped some characters but not necessarily
3628 all there are. Check if we ended up on visible
3629 text. Fget_char_property returns the property of
3630 the char before the given position, i.e. if we
3631 get invis_p = 0, this means that the char at
3632 newpos is visible. */
3633 pos = make_number (newpos);
3634 prop = Fget_char_property (pos, Qinvisible, it->window);
3635 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3636 }
3637
3638 /* If we ended up on invisible text, proceed to
3639 skip starting with next_stop. */
3640 if (invis_p)
3641 tem = next_stop;
3642
3643 /* If there are adjacent invisible texts, don't lose the
3644 second one's ellipsis. */
3645 if (invis_p == 2)
3646 display_ellipsis_p = 1;
3647 }
3648 while (invis_p);
3649
3650 /* The position newpos is now either ZV or on visible text. */
3651 if (it->bidi_p && newpos < ZV)
3652 {
3653 /* With bidi iteration, the region of invisible text
3654 could start and/or end in the middle of a non-base
3655 embedding level. Therefore, we need to skip
3656 invisible text using the bidi iterator, starting at
3657 IT's current position, until we find ourselves
3658 outside the invisible text. Skipping invisible text
3659 _after_ bidi iteration avoids affecting the visual
3660 order of the displayed text when invisible properties
3661 are added or removed. */
3662 if (it->bidi_it.first_elt)
3663 {
3664 /* If we were `reseat'ed to a new paragraph,
3665 determine the paragraph base direction. We need
3666 to do it now because next_element_from_buffer may
3667 not have a chance to do it, if we are going to
3668 skip any text at the beginning, which resets the
3669 FIRST_ELT flag. */
3670 bidi_paragraph_init (it->paragraph_embedding,
3671 &it->bidi_it, 1);
3672 }
3673 do
3674 {
3675 bidi_move_to_visually_next (&it->bidi_it);
3676 }
3677 while (it->stop_charpos <= it->bidi_it.charpos
3678 && it->bidi_it.charpos < newpos);
3679 IT_CHARPOS (*it) = it->bidi_it.charpos;
3680 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3681 /* If we overstepped NEWPOS, record its position in the
3682 iterator, so that we skip invisible text if later the
3683 bidi iteration lands us in the invisible region
3684 again. */
3685 if (IT_CHARPOS (*it) >= newpos)
3686 it->prev_stop = newpos;
3687 }
3688 else
3689 {
3690 IT_CHARPOS (*it) = newpos;
3691 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3692 }
3693
3694 /* If there are before-strings at the start of invisible
3695 text, and the text is invisible because of a text
3696 property, arrange to show before-strings because 20.x did
3697 it that way. (If the text is invisible because of an
3698 overlay property instead of a text property, this is
3699 already handled in the overlay code.) */
3700 if (NILP (overlay)
3701 && get_overlay_strings (it, it->stop_charpos))
3702 {
3703 handled = HANDLED_RECOMPUTE_PROPS;
3704 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3705 }
3706 else if (display_ellipsis_p)
3707 {
3708 /* Make sure that the glyphs of the ellipsis will get
3709 correct `charpos' values. If we would not update
3710 it->position here, the glyphs would belong to the
3711 last visible character _before_ the invisible
3712 text, which confuses `set_cursor_from_row'.
3713
3714 We use the last invisible position instead of the
3715 first because this way the cursor is always drawn on
3716 the first "." of the ellipsis, whenever PT is inside
3717 the invisible text. Otherwise the cursor would be
3718 placed _after_ the ellipsis when the point is after the
3719 first invisible character. */
3720 if (!STRINGP (it->object))
3721 {
3722 it->position.charpos = newpos - 1;
3723 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3724 }
3725 it->ellipsis_p = 1;
3726 /* Let the ellipsis display before
3727 considering any properties of the following char.
3728 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3729 handled = HANDLED_RETURN;
3730 }
3731 }
3732 }
3733
3734 return handled;
3735 }
3736
3737
3738 /* Make iterator IT return `...' next.
3739 Replaces LEN characters from buffer. */
3740
3741 static void
3742 setup_for_ellipsis (struct it *it, int len)
3743 {
3744 /* Use the display table definition for `...'. Invalid glyphs
3745 will be handled by the method returning elements from dpvec. */
3746 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3747 {
3748 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3749 it->dpvec = v->contents;
3750 it->dpend = v->contents + v->size;
3751 }
3752 else
3753 {
3754 /* Default `...'. */
3755 it->dpvec = default_invis_vector;
3756 it->dpend = default_invis_vector + 3;
3757 }
3758
3759 it->dpvec_char_len = len;
3760 it->current.dpvec_index = 0;
3761 it->dpvec_face_id = -1;
3762
3763 /* Remember the current face id in case glyphs specify faces.
3764 IT's face is restored in set_iterator_to_next.
3765 saved_face_id was set to preceding char's face in handle_stop. */
3766 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3767 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3768
3769 it->method = GET_FROM_DISPLAY_VECTOR;
3770 it->ellipsis_p = 1;
3771 }
3772
3773
3774 \f
3775 /***********************************************************************
3776 'display' property
3777 ***********************************************************************/
3778
3779 /* Set up iterator IT from `display' property at its current position.
3780 Called from handle_stop.
3781 We return HANDLED_RETURN if some part of the display property
3782 overrides the display of the buffer text itself.
3783 Otherwise we return HANDLED_NORMALLY. */
3784
3785 static enum prop_handled
3786 handle_display_prop (struct it *it)
3787 {
3788 Lisp_Object prop, object, overlay;
3789 struct text_pos *position;
3790 /* Nonzero if some property replaces the display of the text itself. */
3791 int display_replaced_p = 0;
3792
3793 if (STRINGP (it->string))
3794 {
3795 object = it->string;
3796 position = &it->current.string_pos;
3797 }
3798 else
3799 {
3800 XSETWINDOW (object, it->w);
3801 position = &it->current.pos;
3802 }
3803
3804 /* Reset those iterator values set from display property values. */
3805 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3806 it->space_width = Qnil;
3807 it->font_height = Qnil;
3808 it->voffset = 0;
3809
3810 /* We don't support recursive `display' properties, i.e. string
3811 values that have a string `display' property, that have a string
3812 `display' property etc. */
3813 if (!it->string_from_display_prop_p)
3814 it->area = TEXT_AREA;
3815
3816 prop = get_char_property_and_overlay (make_number (position->charpos),
3817 Qdisplay, object, &overlay);
3818 if (NILP (prop))
3819 return HANDLED_NORMALLY;
3820 /* Now OVERLAY is the overlay that gave us this property, or nil
3821 if it was a text property. */
3822
3823 if (!STRINGP (it->string))
3824 object = it->w->buffer;
3825
3826 if (CONSP (prop)
3827 /* Simple properties. */
3828 && !EQ (XCAR (prop), Qimage)
3829 && !EQ (XCAR (prop), Qspace)
3830 && !EQ (XCAR (prop), Qwhen)
3831 && !EQ (XCAR (prop), Qslice)
3832 && !EQ (XCAR (prop), Qspace_width)
3833 && !EQ (XCAR (prop), Qheight)
3834 && !EQ (XCAR (prop), Qraise)
3835 /* Marginal area specifications. */
3836 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3837 && !EQ (XCAR (prop), Qleft_fringe)
3838 && !EQ (XCAR (prop), Qright_fringe)
3839 && !NILP (XCAR (prop)))
3840 {
3841 for (; CONSP (prop); prop = XCDR (prop))
3842 {
3843 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3844 position, display_replaced_p))
3845 {
3846 display_replaced_p = 1;
3847 /* If some text in a string is replaced, `position' no
3848 longer points to the position of `object'. */
3849 if (STRINGP (object))
3850 break;
3851 }
3852 }
3853 }
3854 else if (VECTORP (prop))
3855 {
3856 int i;
3857 for (i = 0; i < ASIZE (prop); ++i)
3858 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3859 position, display_replaced_p))
3860 {
3861 display_replaced_p = 1;
3862 /* If some text in a string is replaced, `position' no
3863 longer points to the position of `object'. */
3864 if (STRINGP (object))
3865 break;
3866 }
3867 }
3868 else
3869 {
3870 if (handle_single_display_spec (it, prop, object, overlay,
3871 position, 0))
3872 display_replaced_p = 1;
3873 }
3874
3875 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3876 }
3877
3878
3879 /* Value is the position of the end of the `display' property starting
3880 at START_POS in OBJECT. */
3881
3882 static struct text_pos
3883 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
3884 {
3885 Lisp_Object end;
3886 struct text_pos end_pos;
3887
3888 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3889 Qdisplay, object, Qnil);
3890 CHARPOS (end_pos) = XFASTINT (end);
3891 if (STRINGP (object))
3892 compute_string_pos (&end_pos, start_pos, it->string);
3893 else
3894 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3895
3896 return end_pos;
3897 }
3898
3899
3900 /* Set up IT from a single `display' specification PROP. OBJECT
3901 is the object in which the `display' property was found. *POSITION
3902 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3903 means that we previously saw a display specification which already
3904 replaced text display with something else, for example an image;
3905 we ignore such properties after the first one has been processed.
3906
3907 OVERLAY is the overlay this `display' property came from,
3908 or nil if it was a text property.
3909
3910 If PROP is a `space' or `image' specification, and in some other
3911 cases too, set *POSITION to the position where the `display'
3912 property ends.
3913
3914 Value is non-zero if something was found which replaces the display
3915 of buffer or string text. */
3916
3917 static int
3918 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
3919 Lisp_Object overlay, struct text_pos *position,
3920 int display_replaced_before_p)
3921 {
3922 Lisp_Object form;
3923 Lisp_Object location, value;
3924 struct text_pos start_pos, save_pos;
3925 int valid_p;
3926
3927 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3928 If the result is non-nil, use VALUE instead of SPEC. */
3929 form = Qt;
3930 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3931 {
3932 spec = XCDR (spec);
3933 if (!CONSP (spec))
3934 return 0;
3935 form = XCAR (spec);
3936 spec = XCDR (spec);
3937 }
3938
3939 if (!NILP (form) && !EQ (form, Qt))
3940 {
3941 int count = SPECPDL_INDEX ();
3942 struct gcpro gcpro1;
3943
3944 /* Bind `object' to the object having the `display' property, a
3945 buffer or string. Bind `position' to the position in the
3946 object where the property was found, and `buffer-position'
3947 to the current position in the buffer. */
3948 specbind (Qobject, object);
3949 specbind (Qposition, make_number (CHARPOS (*position)));
3950 specbind (Qbuffer_position,
3951 make_number (STRINGP (object)
3952 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3953 GCPRO1 (form);
3954 form = safe_eval (form);
3955 UNGCPRO;
3956 unbind_to (count, Qnil);
3957 }
3958
3959 if (NILP (form))
3960 return 0;
3961
3962 /* Handle `(height HEIGHT)' specifications. */
3963 if (CONSP (spec)
3964 && EQ (XCAR (spec), Qheight)
3965 && CONSP (XCDR (spec)))
3966 {
3967 if (!FRAME_WINDOW_P (it->f))
3968 return 0;
3969
3970 it->font_height = XCAR (XCDR (spec));
3971 if (!NILP (it->font_height))
3972 {
3973 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3974 int new_height = -1;
3975
3976 if (CONSP (it->font_height)
3977 && (EQ (XCAR (it->font_height), Qplus)
3978 || EQ (XCAR (it->font_height), Qminus))
3979 && CONSP (XCDR (it->font_height))
3980 && INTEGERP (XCAR (XCDR (it->font_height))))
3981 {
3982 /* `(+ N)' or `(- N)' where N is an integer. */
3983 int steps = XINT (XCAR (XCDR (it->font_height)));
3984 if (EQ (XCAR (it->font_height), Qplus))
3985 steps = - steps;
3986 it->face_id = smaller_face (it->f, it->face_id, steps);
3987 }
3988 else if (FUNCTIONP (it->font_height))
3989 {
3990 /* Call function with current height as argument.
3991 Value is the new height. */
3992 Lisp_Object height;
3993 height = safe_call1 (it->font_height,
3994 face->lface[LFACE_HEIGHT_INDEX]);
3995 if (NUMBERP (height))
3996 new_height = XFLOATINT (height);
3997 }
3998 else if (NUMBERP (it->font_height))
3999 {
4000 /* Value is a multiple of the canonical char height. */
4001 struct face *f;
4002
4003 f = FACE_FROM_ID (it->f,
4004 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4005 new_height = (XFLOATINT (it->font_height)
4006 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4007 }
4008 else
4009 {
4010 /* Evaluate IT->font_height with `height' bound to the
4011 current specified height to get the new height. */
4012 int count = SPECPDL_INDEX ();
4013
4014 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4015 value = safe_eval (it->font_height);
4016 unbind_to (count, Qnil);
4017
4018 if (NUMBERP (value))
4019 new_height = XFLOATINT (value);
4020 }
4021
4022 if (new_height > 0)
4023 it->face_id = face_with_height (it->f, it->face_id, new_height);
4024 }
4025
4026 return 0;
4027 }
4028
4029 /* Handle `(space-width WIDTH)'. */
4030 if (CONSP (spec)
4031 && EQ (XCAR (spec), Qspace_width)
4032 && CONSP (XCDR (spec)))
4033 {
4034 if (!FRAME_WINDOW_P (it->f))
4035 return 0;
4036
4037 value = XCAR (XCDR (spec));
4038 if (NUMBERP (value) && XFLOATINT (value) > 0)
4039 it->space_width = value;
4040
4041 return 0;
4042 }
4043
4044 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4045 if (CONSP (spec)
4046 && EQ (XCAR (spec), Qslice))
4047 {
4048 Lisp_Object tem;
4049
4050 if (!FRAME_WINDOW_P (it->f))
4051 return 0;
4052
4053 if (tem = XCDR (spec), CONSP (tem))
4054 {
4055 it->slice.x = XCAR (tem);
4056 if (tem = XCDR (tem), CONSP (tem))
4057 {
4058 it->slice.y = XCAR (tem);
4059 if (tem = XCDR (tem), CONSP (tem))
4060 {
4061 it->slice.width = XCAR (tem);
4062 if (tem = XCDR (tem), CONSP (tem))
4063 it->slice.height = XCAR (tem);
4064 }
4065 }
4066 }
4067
4068 return 0;
4069 }
4070
4071 /* Handle `(raise FACTOR)'. */
4072 if (CONSP (spec)
4073 && EQ (XCAR (spec), Qraise)
4074 && CONSP (XCDR (spec)))
4075 {
4076 if (!FRAME_WINDOW_P (it->f))
4077 return 0;
4078
4079 #ifdef HAVE_WINDOW_SYSTEM
4080 value = XCAR (XCDR (spec));
4081 if (NUMBERP (value))
4082 {
4083 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4084 it->voffset = - (XFLOATINT (value)
4085 * (FONT_HEIGHT (face->font)));
4086 }
4087 #endif /* HAVE_WINDOW_SYSTEM */
4088
4089 return 0;
4090 }
4091
4092 /* Don't handle the other kinds of display specifications
4093 inside a string that we got from a `display' property. */
4094 if (it->string_from_display_prop_p)
4095 return 0;
4096
4097 /* Characters having this form of property are not displayed, so
4098 we have to find the end of the property. */
4099 start_pos = *position;
4100 *position = display_prop_end (it, object, start_pos);
4101 value = Qnil;
4102
4103 /* Stop the scan at that end position--we assume that all
4104 text properties change there. */
4105 it->stop_charpos = position->charpos;
4106
4107 /* Handle `(left-fringe BITMAP [FACE])'
4108 and `(right-fringe BITMAP [FACE])'. */
4109 if (CONSP (spec)
4110 && (EQ (XCAR (spec), Qleft_fringe)
4111 || EQ (XCAR (spec), Qright_fringe))
4112 && CONSP (XCDR (spec)))
4113 {
4114 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4115 int fringe_bitmap;
4116
4117 if (!FRAME_WINDOW_P (it->f))
4118 /* If we return here, POSITION has been advanced
4119 across the text with this property. */
4120 return 0;
4121
4122 #ifdef HAVE_WINDOW_SYSTEM
4123 value = XCAR (XCDR (spec));
4124 if (!SYMBOLP (value)
4125 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4126 /* If we return here, POSITION has been advanced
4127 across the text with this property. */
4128 return 0;
4129
4130 if (CONSP (XCDR (XCDR (spec))))
4131 {
4132 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4133 int face_id2 = lookup_derived_face (it->f, face_name,
4134 FRINGE_FACE_ID, 0);
4135 if (face_id2 >= 0)
4136 face_id = face_id2;
4137 }
4138
4139 /* Save current settings of IT so that we can restore them
4140 when we are finished with the glyph property value. */
4141
4142 save_pos = it->position;
4143 it->position = *position;
4144 push_it (it);
4145 it->position = save_pos;
4146
4147 it->area = TEXT_AREA;
4148 it->what = IT_IMAGE;
4149 it->image_id = -1; /* no image */
4150 it->position = start_pos;
4151 it->object = NILP (object) ? it->w->buffer : object;
4152 it->method = GET_FROM_IMAGE;
4153 it->from_overlay = Qnil;
4154 it->face_id = face_id;
4155
4156 /* Say that we haven't consumed the characters with
4157 `display' property yet. The call to pop_it in
4158 set_iterator_to_next will clean this up. */
4159 *position = start_pos;
4160
4161 if (EQ (XCAR (spec), Qleft_fringe))
4162 {
4163 it->left_user_fringe_bitmap = fringe_bitmap;
4164 it->left_user_fringe_face_id = face_id;
4165 }
4166 else
4167 {
4168 it->right_user_fringe_bitmap = fringe_bitmap;
4169 it->right_user_fringe_face_id = face_id;
4170 }
4171 #endif /* HAVE_WINDOW_SYSTEM */
4172 return 1;
4173 }
4174
4175 /* Prepare to handle `((margin left-margin) ...)',
4176 `((margin right-margin) ...)' and `((margin nil) ...)'
4177 prefixes for display specifications. */
4178 location = Qunbound;
4179 if (CONSP (spec) && CONSP (XCAR (spec)))
4180 {
4181 Lisp_Object tem;
4182
4183 value = XCDR (spec);
4184 if (CONSP (value))
4185 value = XCAR (value);
4186
4187 tem = XCAR (spec);
4188 if (EQ (XCAR (tem), Qmargin)
4189 && (tem = XCDR (tem),
4190 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4191 (NILP (tem)
4192 || EQ (tem, Qleft_margin)
4193 || EQ (tem, Qright_margin))))
4194 location = tem;
4195 }
4196
4197 if (EQ (location, Qunbound))
4198 {
4199 location = Qnil;
4200 value = spec;
4201 }
4202
4203 /* After this point, VALUE is the property after any
4204 margin prefix has been stripped. It must be a string,
4205 an image specification, or `(space ...)'.
4206
4207 LOCATION specifies where to display: `left-margin',
4208 `right-margin' or nil. */
4209
4210 valid_p = (STRINGP (value)
4211 #ifdef HAVE_WINDOW_SYSTEM
4212 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4213 #endif /* not HAVE_WINDOW_SYSTEM */
4214 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4215
4216 if (valid_p && !display_replaced_before_p)
4217 {
4218 /* Save current settings of IT so that we can restore them
4219 when we are finished with the glyph property value. */
4220 save_pos = it->position;
4221 it->position = *position;
4222 push_it (it);
4223 it->position = save_pos;
4224 it->from_overlay = overlay;
4225
4226 if (NILP (location))
4227 it->area = TEXT_AREA;
4228 else if (EQ (location, Qleft_margin))
4229 it->area = LEFT_MARGIN_AREA;
4230 else
4231 it->area = RIGHT_MARGIN_AREA;
4232
4233 if (STRINGP (value))
4234 {
4235 it->string = value;
4236 it->multibyte_p = STRING_MULTIBYTE (it->string);
4237 it->current.overlay_string_index = -1;
4238 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4239 it->end_charpos = it->string_nchars = SCHARS (it->string);
4240 it->method = GET_FROM_STRING;
4241 it->stop_charpos = 0;
4242 it->string_from_display_prop_p = 1;
4243 /* Say that we haven't consumed the characters with
4244 `display' property yet. The call to pop_it in
4245 set_iterator_to_next will clean this up. */
4246 if (BUFFERP (object))
4247 *position = start_pos;
4248 }
4249 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4250 {
4251 it->method = GET_FROM_STRETCH;
4252 it->object = value;
4253 *position = it->position = start_pos;
4254 }
4255 #ifdef HAVE_WINDOW_SYSTEM
4256 else
4257 {
4258 it->what = IT_IMAGE;
4259 it->image_id = lookup_image (it->f, value);
4260 it->position = start_pos;
4261 it->object = NILP (object) ? it->w->buffer : object;
4262 it->method = GET_FROM_IMAGE;
4263
4264 /* Say that we haven't consumed the characters with
4265 `display' property yet. The call to pop_it in
4266 set_iterator_to_next will clean this up. */
4267 *position = start_pos;
4268 }
4269 #endif /* HAVE_WINDOW_SYSTEM */
4270
4271 return 1;
4272 }
4273
4274 /* Invalid property or property not supported. Restore
4275 POSITION to what it was before. */
4276 *position = start_pos;
4277 return 0;
4278 }
4279
4280
4281 /* Check if SPEC is a display sub-property value whose text should be
4282 treated as intangible. */
4283
4284 static int
4285 single_display_spec_intangible_p (Lisp_Object prop)
4286 {
4287 /* Skip over `when FORM'. */
4288 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4289 {
4290 prop = XCDR (prop);
4291 if (!CONSP (prop))
4292 return 0;
4293 prop = XCDR (prop);
4294 }
4295
4296 if (STRINGP (prop))
4297 return 1;
4298
4299 if (!CONSP (prop))
4300 return 0;
4301
4302 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4303 we don't need to treat text as intangible. */
4304 if (EQ (XCAR (prop), Qmargin))
4305 {
4306 prop = XCDR (prop);
4307 if (!CONSP (prop))
4308 return 0;
4309
4310 prop = XCDR (prop);
4311 if (!CONSP (prop)
4312 || EQ (XCAR (prop), Qleft_margin)
4313 || EQ (XCAR (prop), Qright_margin))
4314 return 0;
4315 }
4316
4317 return (CONSP (prop)
4318 && (EQ (XCAR (prop), Qimage)
4319 || EQ (XCAR (prop), Qspace)));
4320 }
4321
4322
4323 /* Check if PROP is a display property value whose text should be
4324 treated as intangible. */
4325
4326 int
4327 display_prop_intangible_p (Lisp_Object prop)
4328 {
4329 if (CONSP (prop)
4330 && CONSP (XCAR (prop))
4331 && !EQ (Qmargin, XCAR (XCAR (prop))))
4332 {
4333 /* A list of sub-properties. */
4334 while (CONSP (prop))
4335 {
4336 if (single_display_spec_intangible_p (XCAR (prop)))
4337 return 1;
4338 prop = XCDR (prop);
4339 }
4340 }
4341 else if (VECTORP (prop))
4342 {
4343 /* A vector of sub-properties. */
4344 int i;
4345 for (i = 0; i < ASIZE (prop); ++i)
4346 if (single_display_spec_intangible_p (AREF (prop, i)))
4347 return 1;
4348 }
4349 else
4350 return single_display_spec_intangible_p (prop);
4351
4352 return 0;
4353 }
4354
4355
4356 /* Return 1 if PROP is a display sub-property value containing STRING. */
4357
4358 static int
4359 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4360 {
4361 if (EQ (string, prop))
4362 return 1;
4363
4364 /* Skip over `when FORM'. */
4365 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4366 {
4367 prop = XCDR (prop);
4368 if (!CONSP (prop))
4369 return 0;
4370 prop = XCDR (prop);
4371 }
4372
4373 if (CONSP (prop))
4374 /* Skip over `margin LOCATION'. */
4375 if (EQ (XCAR (prop), Qmargin))
4376 {
4377 prop = XCDR (prop);
4378 if (!CONSP (prop))
4379 return 0;
4380
4381 prop = XCDR (prop);
4382 if (!CONSP (prop))
4383 return 0;
4384 }
4385
4386 return CONSP (prop) && EQ (XCAR (prop), string);
4387 }
4388
4389
4390 /* Return 1 if STRING appears in the `display' property PROP. */
4391
4392 static int
4393 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4394 {
4395 if (CONSP (prop)
4396 && CONSP (XCAR (prop))
4397 && !EQ (Qmargin, XCAR (XCAR (prop))))
4398 {
4399 /* A list of sub-properties. */
4400 while (CONSP (prop))
4401 {
4402 if (single_display_spec_string_p (XCAR (prop), string))
4403 return 1;
4404 prop = XCDR (prop);
4405 }
4406 }
4407 else if (VECTORP (prop))
4408 {
4409 /* A vector of sub-properties. */
4410 int i;
4411 for (i = 0; i < ASIZE (prop); ++i)
4412 if (single_display_spec_string_p (AREF (prop, i), string))
4413 return 1;
4414 }
4415 else
4416 return single_display_spec_string_p (prop, string);
4417
4418 return 0;
4419 }
4420
4421 /* Look for STRING in overlays and text properties in the current
4422 buffer, between character positions FROM and TO (excluding TO).
4423 BACK_P non-zero means look back (in this case, TO is supposed to be
4424 less than FROM).
4425 Value is the first character position where STRING was found, or
4426 zero if it wasn't found before hitting TO.
4427
4428 This function may only use code that doesn't eval because it is
4429 called asynchronously from note_mouse_highlight. */
4430
4431 static EMACS_INT
4432 string_buffer_position_lim (Lisp_Object string,
4433 EMACS_INT from, EMACS_INT to, int back_p)
4434 {
4435 Lisp_Object limit, prop, pos;
4436 int found = 0;
4437
4438 pos = make_number (from);
4439
4440 if (!back_p) /* looking forward */
4441 {
4442 limit = make_number (min (to, ZV));
4443 while (!found && !EQ (pos, limit))
4444 {
4445 prop = Fget_char_property (pos, Qdisplay, Qnil);
4446 if (!NILP (prop) && display_prop_string_p (prop, string))
4447 found = 1;
4448 else
4449 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4450 limit);
4451 }
4452 }
4453 else /* looking back */
4454 {
4455 limit = make_number (max (to, BEGV));
4456 while (!found && !EQ (pos, limit))
4457 {
4458 prop = Fget_char_property (pos, Qdisplay, Qnil);
4459 if (!NILP (prop) && display_prop_string_p (prop, string))
4460 found = 1;
4461 else
4462 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4463 limit);
4464 }
4465 }
4466
4467 return found ? XINT (pos) : 0;
4468 }
4469
4470 /* Determine which buffer position in current buffer STRING comes from.
4471 AROUND_CHARPOS is an approximate position where it could come from.
4472 Value is the buffer position or 0 if it couldn't be determined.
4473
4474 This function is necessary because we don't record buffer positions
4475 in glyphs generated from strings (to keep struct glyph small).
4476 This function may only use code that doesn't eval because it is
4477 called asynchronously from note_mouse_highlight. */
4478
4479 static EMACS_INT
4480 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4481 {
4482 const int MAX_DISTANCE = 1000;
4483 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4484 around_charpos + MAX_DISTANCE,
4485 0);
4486
4487 if (!found)
4488 found = string_buffer_position_lim (string, around_charpos,
4489 around_charpos - MAX_DISTANCE, 1);
4490 return found;
4491 }
4492
4493
4494 \f
4495 /***********************************************************************
4496 `composition' property
4497 ***********************************************************************/
4498
4499 /* Set up iterator IT from `composition' property at its current
4500 position. Called from handle_stop. */
4501
4502 static enum prop_handled
4503 handle_composition_prop (struct it *it)
4504 {
4505 Lisp_Object prop, string;
4506 EMACS_INT pos, pos_byte, start, end;
4507
4508 if (STRINGP (it->string))
4509 {
4510 unsigned char *s;
4511
4512 pos = IT_STRING_CHARPOS (*it);
4513 pos_byte = IT_STRING_BYTEPOS (*it);
4514 string = it->string;
4515 s = SDATA (string) + pos_byte;
4516 it->c = STRING_CHAR (s);
4517 }
4518 else
4519 {
4520 pos = IT_CHARPOS (*it);
4521 pos_byte = IT_BYTEPOS (*it);
4522 string = Qnil;
4523 it->c = FETCH_CHAR (pos_byte);
4524 }
4525
4526 /* If there's a valid composition and point is not inside of the
4527 composition (in the case that the composition is from the current
4528 buffer), draw a glyph composed from the composition components. */
4529 if (find_composition (pos, -1, &start, &end, &prop, string)
4530 && COMPOSITION_VALID_P (start, end, prop)
4531 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4532 {
4533 if (start != pos)
4534 {
4535 if (STRINGP (it->string))
4536 pos_byte = string_char_to_byte (it->string, start);
4537 else
4538 pos_byte = CHAR_TO_BYTE (start);
4539 }
4540 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4541 prop, string);
4542
4543 if (it->cmp_it.id >= 0)
4544 {
4545 it->cmp_it.ch = -1;
4546 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4547 it->cmp_it.nglyphs = -1;
4548 }
4549 }
4550
4551 return HANDLED_NORMALLY;
4552 }
4553
4554
4555 \f
4556 /***********************************************************************
4557 Overlay strings
4558 ***********************************************************************/
4559
4560 /* The following structure is used to record overlay strings for
4561 later sorting in load_overlay_strings. */
4562
4563 struct overlay_entry
4564 {
4565 Lisp_Object overlay;
4566 Lisp_Object string;
4567 int priority;
4568 int after_string_p;
4569 };
4570
4571
4572 /* Set up iterator IT from overlay strings at its current position.
4573 Called from handle_stop. */
4574
4575 static enum prop_handled
4576 handle_overlay_change (struct it *it)
4577 {
4578 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4579 return HANDLED_RECOMPUTE_PROPS;
4580 else
4581 return HANDLED_NORMALLY;
4582 }
4583
4584
4585 /* Set up the next overlay string for delivery by IT, if there is an
4586 overlay string to deliver. Called by set_iterator_to_next when the
4587 end of the current overlay string is reached. If there are more
4588 overlay strings to display, IT->string and
4589 IT->current.overlay_string_index are set appropriately here.
4590 Otherwise IT->string is set to nil. */
4591
4592 static void
4593 next_overlay_string (struct it *it)
4594 {
4595 ++it->current.overlay_string_index;
4596 if (it->current.overlay_string_index == it->n_overlay_strings)
4597 {
4598 /* No more overlay strings. Restore IT's settings to what
4599 they were before overlay strings were processed, and
4600 continue to deliver from current_buffer. */
4601
4602 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4603 pop_it (it);
4604 xassert (it->sp > 0
4605 || (NILP (it->string)
4606 && it->method == GET_FROM_BUFFER
4607 && it->stop_charpos >= BEGV
4608 && it->stop_charpos <= it->end_charpos));
4609 it->current.overlay_string_index = -1;
4610 it->n_overlay_strings = 0;
4611 it->overlay_strings_charpos = -1;
4612
4613 /* If we're at the end of the buffer, record that we have
4614 processed the overlay strings there already, so that
4615 next_element_from_buffer doesn't try it again. */
4616 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4617 it->overlay_strings_at_end_processed_p = 1;
4618 }
4619 else
4620 {
4621 /* There are more overlay strings to process. If
4622 IT->current.overlay_string_index has advanced to a position
4623 where we must load IT->overlay_strings with more strings, do
4624 it. We must load at the IT->overlay_strings_charpos where
4625 IT->n_overlay_strings was originally computed; when invisible
4626 text is present, this might not be IT_CHARPOS (Bug#7016). */
4627 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4628
4629 if (it->current.overlay_string_index && i == 0)
4630 load_overlay_strings (it, it->overlay_strings_charpos);
4631
4632 /* Initialize IT to deliver display elements from the overlay
4633 string. */
4634 it->string = it->overlay_strings[i];
4635 it->multibyte_p = STRING_MULTIBYTE (it->string);
4636 SET_TEXT_POS (it->current.string_pos, 0, 0);
4637 it->method = GET_FROM_STRING;
4638 it->stop_charpos = 0;
4639 if (it->cmp_it.stop_pos >= 0)
4640 it->cmp_it.stop_pos = 0;
4641 }
4642
4643 CHECK_IT (it);
4644 }
4645
4646
4647 /* Compare two overlay_entry structures E1 and E2. Used as a
4648 comparison function for qsort in load_overlay_strings. Overlay
4649 strings for the same position are sorted so that
4650
4651 1. All after-strings come in front of before-strings, except
4652 when they come from the same overlay.
4653
4654 2. Within after-strings, strings are sorted so that overlay strings
4655 from overlays with higher priorities come first.
4656
4657 2. Within before-strings, strings are sorted so that overlay
4658 strings from overlays with higher priorities come last.
4659
4660 Value is analogous to strcmp. */
4661
4662
4663 static int
4664 compare_overlay_entries (const void *e1, const void *e2)
4665 {
4666 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4667 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4668 int result;
4669
4670 if (entry1->after_string_p != entry2->after_string_p)
4671 {
4672 /* Let after-strings appear in front of before-strings if
4673 they come from different overlays. */
4674 if (EQ (entry1->overlay, entry2->overlay))
4675 result = entry1->after_string_p ? 1 : -1;
4676 else
4677 result = entry1->after_string_p ? -1 : 1;
4678 }
4679 else if (entry1->after_string_p)
4680 /* After-strings sorted in order of decreasing priority. */
4681 result = entry2->priority - entry1->priority;
4682 else
4683 /* Before-strings sorted in order of increasing priority. */
4684 result = entry1->priority - entry2->priority;
4685
4686 return result;
4687 }
4688
4689
4690 /* Load the vector IT->overlay_strings with overlay strings from IT's
4691 current buffer position, or from CHARPOS if that is > 0. Set
4692 IT->n_overlays to the total number of overlay strings found.
4693
4694 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4695 a time. On entry into load_overlay_strings,
4696 IT->current.overlay_string_index gives the number of overlay
4697 strings that have already been loaded by previous calls to this
4698 function.
4699
4700 IT->add_overlay_start contains an additional overlay start
4701 position to consider for taking overlay strings from, if non-zero.
4702 This position comes into play when the overlay has an `invisible'
4703 property, and both before and after-strings. When we've skipped to
4704 the end of the overlay, because of its `invisible' property, we
4705 nevertheless want its before-string to appear.
4706 IT->add_overlay_start will contain the overlay start position
4707 in this case.
4708
4709 Overlay strings are sorted so that after-string strings come in
4710 front of before-string strings. Within before and after-strings,
4711 strings are sorted by overlay priority. See also function
4712 compare_overlay_entries. */
4713
4714 static void
4715 load_overlay_strings (struct it *it, EMACS_INT charpos)
4716 {
4717 Lisp_Object overlay, window, str, invisible;
4718 struct Lisp_Overlay *ov;
4719 EMACS_INT start, end;
4720 int size = 20;
4721 int n = 0, i, j, invis_p;
4722 struct overlay_entry *entries
4723 = (struct overlay_entry *) alloca (size * sizeof *entries);
4724
4725 if (charpos <= 0)
4726 charpos = IT_CHARPOS (*it);
4727
4728 /* Append the overlay string STRING of overlay OVERLAY to vector
4729 `entries' which has size `size' and currently contains `n'
4730 elements. AFTER_P non-zero means STRING is an after-string of
4731 OVERLAY. */
4732 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4733 do \
4734 { \
4735 Lisp_Object priority; \
4736 \
4737 if (n == size) \
4738 { \
4739 int new_size = 2 * size; \
4740 struct overlay_entry *old = entries; \
4741 entries = \
4742 (struct overlay_entry *) alloca (new_size \
4743 * sizeof *entries); \
4744 memcpy (entries, old, size * sizeof *entries); \
4745 size = new_size; \
4746 } \
4747 \
4748 entries[n].string = (STRING); \
4749 entries[n].overlay = (OVERLAY); \
4750 priority = Foverlay_get ((OVERLAY), Qpriority); \
4751 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4752 entries[n].after_string_p = (AFTER_P); \
4753 ++n; \
4754 } \
4755 while (0)
4756
4757 /* Process overlay before the overlay center. */
4758 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4759 {
4760 XSETMISC (overlay, ov);
4761 xassert (OVERLAYP (overlay));
4762 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4763 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4764
4765 if (end < charpos)
4766 break;
4767
4768 /* Skip this overlay if it doesn't start or end at IT's current
4769 position. */
4770 if (end != charpos && start != charpos)
4771 continue;
4772
4773 /* Skip this overlay if it doesn't apply to IT->w. */
4774 window = Foverlay_get (overlay, Qwindow);
4775 if (WINDOWP (window) && XWINDOW (window) != it->w)
4776 continue;
4777
4778 /* If the text ``under'' the overlay is invisible, both before-
4779 and after-strings from this overlay are visible; start and
4780 end position are indistinguishable. */
4781 invisible = Foverlay_get (overlay, Qinvisible);
4782 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4783
4784 /* If overlay has a non-empty before-string, record it. */
4785 if ((start == charpos || (end == charpos && invis_p))
4786 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4787 && SCHARS (str))
4788 RECORD_OVERLAY_STRING (overlay, str, 0);
4789
4790 /* If overlay has a non-empty after-string, record it. */
4791 if ((end == charpos || (start == charpos && invis_p))
4792 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4793 && SCHARS (str))
4794 RECORD_OVERLAY_STRING (overlay, str, 1);
4795 }
4796
4797 /* Process overlays after the overlay center. */
4798 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4799 {
4800 XSETMISC (overlay, ov);
4801 xassert (OVERLAYP (overlay));
4802 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4803 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4804
4805 if (start > charpos)
4806 break;
4807
4808 /* Skip this overlay if it doesn't start or end at IT's current
4809 position. */
4810 if (end != charpos && start != charpos)
4811 continue;
4812
4813 /* Skip this overlay if it doesn't apply to IT->w. */
4814 window = Foverlay_get (overlay, Qwindow);
4815 if (WINDOWP (window) && XWINDOW (window) != it->w)
4816 continue;
4817
4818 /* If the text ``under'' the overlay is invisible, it has a zero
4819 dimension, and both before- and after-strings apply. */
4820 invisible = Foverlay_get (overlay, Qinvisible);
4821 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4822
4823 /* If overlay has a non-empty before-string, record it. */
4824 if ((start == charpos || (end == charpos && invis_p))
4825 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4826 && SCHARS (str))
4827 RECORD_OVERLAY_STRING (overlay, str, 0);
4828
4829 /* If overlay has a non-empty after-string, record it. */
4830 if ((end == charpos || (start == charpos && invis_p))
4831 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4832 && SCHARS (str))
4833 RECORD_OVERLAY_STRING (overlay, str, 1);
4834 }
4835
4836 #undef RECORD_OVERLAY_STRING
4837
4838 /* Sort entries. */
4839 if (n > 1)
4840 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4841
4842 /* Record number of overlay strings, and where we computed it. */
4843 it->n_overlay_strings = n;
4844 it->overlay_strings_charpos = charpos;
4845
4846 /* IT->current.overlay_string_index is the number of overlay strings
4847 that have already been consumed by IT. Copy some of the
4848 remaining overlay strings to IT->overlay_strings. */
4849 i = 0;
4850 j = it->current.overlay_string_index;
4851 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4852 {
4853 it->overlay_strings[i] = entries[j].string;
4854 it->string_overlays[i++] = entries[j++].overlay;
4855 }
4856
4857 CHECK_IT (it);
4858 }
4859
4860
4861 /* Get the first chunk of overlay strings at IT's current buffer
4862 position, or at CHARPOS if that is > 0. Value is non-zero if at
4863 least one overlay string was found. */
4864
4865 static int
4866 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
4867 {
4868 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4869 process. This fills IT->overlay_strings with strings, and sets
4870 IT->n_overlay_strings to the total number of strings to process.
4871 IT->pos.overlay_string_index has to be set temporarily to zero
4872 because load_overlay_strings needs this; it must be set to -1
4873 when no overlay strings are found because a zero value would
4874 indicate a position in the first overlay string. */
4875 it->current.overlay_string_index = 0;
4876 load_overlay_strings (it, charpos);
4877
4878 /* If we found overlay strings, set up IT to deliver display
4879 elements from the first one. Otherwise set up IT to deliver
4880 from current_buffer. */
4881 if (it->n_overlay_strings)
4882 {
4883 /* Make sure we know settings in current_buffer, so that we can
4884 restore meaningful values when we're done with the overlay
4885 strings. */
4886 if (compute_stop_p)
4887 compute_stop_pos (it);
4888 xassert (it->face_id >= 0);
4889
4890 /* Save IT's settings. They are restored after all overlay
4891 strings have been processed. */
4892 xassert (!compute_stop_p || it->sp == 0);
4893
4894 /* When called from handle_stop, there might be an empty display
4895 string loaded. In that case, don't bother saving it. */
4896 if (!STRINGP (it->string) || SCHARS (it->string))
4897 push_it (it);
4898
4899 /* Set up IT to deliver display elements from the first overlay
4900 string. */
4901 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4902 it->string = it->overlay_strings[0];
4903 it->from_overlay = Qnil;
4904 it->stop_charpos = 0;
4905 xassert (STRINGP (it->string));
4906 it->end_charpos = SCHARS (it->string);
4907 it->multibyte_p = STRING_MULTIBYTE (it->string);
4908 it->method = GET_FROM_STRING;
4909 return 1;
4910 }
4911
4912 it->current.overlay_string_index = -1;
4913 return 0;
4914 }
4915
4916 static int
4917 get_overlay_strings (struct it *it, EMACS_INT charpos)
4918 {
4919 it->string = Qnil;
4920 it->method = GET_FROM_BUFFER;
4921
4922 (void) get_overlay_strings_1 (it, charpos, 1);
4923
4924 CHECK_IT (it);
4925
4926 /* Value is non-zero if we found at least one overlay string. */
4927 return STRINGP (it->string);
4928 }
4929
4930
4931 \f
4932 /***********************************************************************
4933 Saving and restoring state
4934 ***********************************************************************/
4935
4936 /* Save current settings of IT on IT->stack. Called, for example,
4937 before setting up IT for an overlay string, to be able to restore
4938 IT's settings to what they were after the overlay string has been
4939 processed. */
4940
4941 static void
4942 push_it (struct it *it)
4943 {
4944 struct iterator_stack_entry *p;
4945
4946 xassert (it->sp < IT_STACK_SIZE);
4947 p = it->stack + it->sp;
4948
4949 p->stop_charpos = it->stop_charpos;
4950 p->prev_stop = it->prev_stop;
4951 p->base_level_stop = it->base_level_stop;
4952 p->cmp_it = it->cmp_it;
4953 xassert (it->face_id >= 0);
4954 p->face_id = it->face_id;
4955 p->string = it->string;
4956 p->method = it->method;
4957 p->from_overlay = it->from_overlay;
4958 switch (p->method)
4959 {
4960 case GET_FROM_IMAGE:
4961 p->u.image.object = it->object;
4962 p->u.image.image_id = it->image_id;
4963 p->u.image.slice = it->slice;
4964 break;
4965 case GET_FROM_STRETCH:
4966 p->u.stretch.object = it->object;
4967 break;
4968 }
4969 p->position = it->position;
4970 p->current = it->current;
4971 p->end_charpos = it->end_charpos;
4972 p->string_nchars = it->string_nchars;
4973 p->area = it->area;
4974 p->multibyte_p = it->multibyte_p;
4975 p->avoid_cursor_p = it->avoid_cursor_p;
4976 p->space_width = it->space_width;
4977 p->font_height = it->font_height;
4978 p->voffset = it->voffset;
4979 p->string_from_display_prop_p = it->string_from_display_prop_p;
4980 p->display_ellipsis_p = 0;
4981 p->line_wrap = it->line_wrap;
4982 ++it->sp;
4983 }
4984
4985 static void
4986 iterate_out_of_display_property (struct it *it)
4987 {
4988 /* Maybe initialize paragraph direction. If we are at the beginning
4989 of a new paragraph, next_element_from_buffer may not have a
4990 chance to do that. */
4991 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4992 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
4993 /* prev_stop can be zero, so check against BEGV as well. */
4994 while (it->bidi_it.charpos >= BEGV
4995 && it->prev_stop <= it->bidi_it.charpos
4996 && it->bidi_it.charpos < CHARPOS (it->position))
4997 bidi_move_to_visually_next (&it->bidi_it);
4998 /* Record the stop_pos we just crossed, for when we cross it
4999 back, maybe. */
5000 if (it->bidi_it.charpos > CHARPOS (it->position))
5001 it->prev_stop = CHARPOS (it->position);
5002 /* If we ended up not where pop_it put us, resync IT's
5003 positional members with the bidi iterator. */
5004 if (it->bidi_it.charpos != CHARPOS (it->position))
5005 {
5006 SET_TEXT_POS (it->position,
5007 it->bidi_it.charpos, it->bidi_it.bytepos);
5008 it->current.pos = it->position;
5009 }
5010 }
5011
5012 /* Restore IT's settings from IT->stack. Called, for example, when no
5013 more overlay strings must be processed, and we return to delivering
5014 display elements from a buffer, or when the end of a string from a
5015 `display' property is reached and we return to delivering display
5016 elements from an overlay string, or from a buffer. */
5017
5018 static void
5019 pop_it (struct it *it)
5020 {
5021 struct iterator_stack_entry *p;
5022
5023 xassert (it->sp > 0);
5024 --it->sp;
5025 p = it->stack + it->sp;
5026 it->stop_charpos = p->stop_charpos;
5027 it->prev_stop = p->prev_stop;
5028 it->base_level_stop = p->base_level_stop;
5029 it->cmp_it = p->cmp_it;
5030 it->face_id = p->face_id;
5031 it->current = p->current;
5032 it->position = p->position;
5033 it->string = p->string;
5034 it->from_overlay = p->from_overlay;
5035 if (NILP (it->string))
5036 SET_TEXT_POS (it->current.string_pos, -1, -1);
5037 it->method = p->method;
5038 switch (it->method)
5039 {
5040 case GET_FROM_IMAGE:
5041 it->image_id = p->u.image.image_id;
5042 it->object = p->u.image.object;
5043 it->slice = p->u.image.slice;
5044 break;
5045 case GET_FROM_STRETCH:
5046 it->object = p->u.comp.object;
5047 break;
5048 case GET_FROM_BUFFER:
5049 it->object = it->w->buffer;
5050 if (it->bidi_p)
5051 {
5052 /* Bidi-iterate until we get out of the portion of text, if
5053 any, covered by a `display' text property or an overlay
5054 with `display' property. (We cannot just jump there,
5055 because the internal coherency of the bidi iterator state
5056 can not be preserved across such jumps.) We also must
5057 determine the paragraph base direction if the overlay we
5058 just processed is at the beginning of a new
5059 paragraph. */
5060 iterate_out_of_display_property (it);
5061 }
5062 break;
5063 case GET_FROM_STRING:
5064 it->object = it->string;
5065 break;
5066 case GET_FROM_DISPLAY_VECTOR:
5067 if (it->s)
5068 it->method = GET_FROM_C_STRING;
5069 else if (STRINGP (it->string))
5070 it->method = GET_FROM_STRING;
5071 else
5072 {
5073 it->method = GET_FROM_BUFFER;
5074 it->object = it->w->buffer;
5075 }
5076 }
5077 it->end_charpos = p->end_charpos;
5078 it->string_nchars = p->string_nchars;
5079 it->area = p->area;
5080 it->multibyte_p = p->multibyte_p;
5081 it->avoid_cursor_p = p->avoid_cursor_p;
5082 it->space_width = p->space_width;
5083 it->font_height = p->font_height;
5084 it->voffset = p->voffset;
5085 it->string_from_display_prop_p = p->string_from_display_prop_p;
5086 it->line_wrap = p->line_wrap;
5087 }
5088
5089
5090 \f
5091 /***********************************************************************
5092 Moving over lines
5093 ***********************************************************************/
5094
5095 /* Set IT's current position to the previous line start. */
5096
5097 static void
5098 back_to_previous_line_start (struct it *it)
5099 {
5100 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5101 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5102 }
5103
5104
5105 /* Move IT to the next line start.
5106
5107 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5108 we skipped over part of the text (as opposed to moving the iterator
5109 continuously over the text). Otherwise, don't change the value
5110 of *SKIPPED_P.
5111
5112 Newlines may come from buffer text, overlay strings, or strings
5113 displayed via the `display' property. That's the reason we can't
5114 simply use find_next_newline_no_quit.
5115
5116 Note that this function may not skip over invisible text that is so
5117 because of text properties and immediately follows a newline. If
5118 it would, function reseat_at_next_visible_line_start, when called
5119 from set_iterator_to_next, would effectively make invisible
5120 characters following a newline part of the wrong glyph row, which
5121 leads to wrong cursor motion. */
5122
5123 static int
5124 forward_to_next_line_start (struct it *it, int *skipped_p)
5125 {
5126 int old_selective, newline_found_p, n;
5127 const int MAX_NEWLINE_DISTANCE = 500;
5128
5129 /* If already on a newline, just consume it to avoid unintended
5130 skipping over invisible text below. */
5131 if (it->what == IT_CHARACTER
5132 && it->c == '\n'
5133 && CHARPOS (it->position) == IT_CHARPOS (*it))
5134 {
5135 set_iterator_to_next (it, 0);
5136 it->c = 0;
5137 return 1;
5138 }
5139
5140 /* Don't handle selective display in the following. It's (a)
5141 unnecessary because it's done by the caller, and (b) leads to an
5142 infinite recursion because next_element_from_ellipsis indirectly
5143 calls this function. */
5144 old_selective = it->selective;
5145 it->selective = 0;
5146
5147 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5148 from buffer text. */
5149 for (n = newline_found_p = 0;
5150 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5151 n += STRINGP (it->string) ? 0 : 1)
5152 {
5153 if (!get_next_display_element (it))
5154 return 0;
5155 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5156 set_iterator_to_next (it, 0);
5157 }
5158
5159 /* If we didn't find a newline near enough, see if we can use a
5160 short-cut. */
5161 if (!newline_found_p)
5162 {
5163 EMACS_INT start = IT_CHARPOS (*it);
5164 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5165 Lisp_Object pos;
5166
5167 xassert (!STRINGP (it->string));
5168
5169 /* If there isn't any `display' property in sight, and no
5170 overlays, we can just use the position of the newline in
5171 buffer text. */
5172 if (it->stop_charpos >= limit
5173 || ((pos = Fnext_single_property_change (make_number (start),
5174 Qdisplay,
5175 Qnil, make_number (limit)),
5176 NILP (pos))
5177 && next_overlay_change (start) == ZV))
5178 {
5179 IT_CHARPOS (*it) = limit;
5180 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5181 *skipped_p = newline_found_p = 1;
5182 }
5183 else
5184 {
5185 while (get_next_display_element (it)
5186 && !newline_found_p)
5187 {
5188 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5189 set_iterator_to_next (it, 0);
5190 }
5191 }
5192 }
5193
5194 it->selective = old_selective;
5195 return newline_found_p;
5196 }
5197
5198
5199 /* Set IT's current position to the previous visible line start. Skip
5200 invisible text that is so either due to text properties or due to
5201 selective display. Caution: this does not change IT->current_x and
5202 IT->hpos. */
5203
5204 static void
5205 back_to_previous_visible_line_start (struct it *it)
5206 {
5207 while (IT_CHARPOS (*it) > BEGV)
5208 {
5209 back_to_previous_line_start (it);
5210
5211 if (IT_CHARPOS (*it) <= BEGV)
5212 break;
5213
5214 /* If selective > 0, then lines indented more than its value are
5215 invisible. */
5216 if (it->selective > 0
5217 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5218 (double) it->selective)) /* iftc */
5219 continue;
5220
5221 /* Check the newline before point for invisibility. */
5222 {
5223 Lisp_Object prop;
5224 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5225 Qinvisible, it->window);
5226 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5227 continue;
5228 }
5229
5230 if (IT_CHARPOS (*it) <= BEGV)
5231 break;
5232
5233 {
5234 struct it it2;
5235 EMACS_INT pos;
5236 EMACS_INT beg, end;
5237 Lisp_Object val, overlay;
5238
5239 /* If newline is part of a composition, continue from start of composition */
5240 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5241 && beg < IT_CHARPOS (*it))
5242 goto replaced;
5243
5244 /* If newline is replaced by a display property, find start of overlay
5245 or interval and continue search from that point. */
5246 it2 = *it;
5247 pos = --IT_CHARPOS (it2);
5248 --IT_BYTEPOS (it2);
5249 it2.sp = 0;
5250 it2.string_from_display_prop_p = 0;
5251 if (handle_display_prop (&it2) == HANDLED_RETURN
5252 && !NILP (val = get_char_property_and_overlay
5253 (make_number (pos), Qdisplay, Qnil, &overlay))
5254 && (OVERLAYP (overlay)
5255 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5256 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5257 goto replaced;
5258
5259 /* Newline is not replaced by anything -- so we are done. */
5260 break;
5261
5262 replaced:
5263 if (beg < BEGV)
5264 beg = BEGV;
5265 IT_CHARPOS (*it) = beg;
5266 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5267 }
5268 }
5269
5270 it->continuation_lines_width = 0;
5271
5272 xassert (IT_CHARPOS (*it) >= BEGV);
5273 xassert (IT_CHARPOS (*it) == BEGV
5274 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5275 CHECK_IT (it);
5276 }
5277
5278
5279 /* Reseat iterator IT at the previous visible line start. Skip
5280 invisible text that is so either due to text properties or due to
5281 selective display. At the end, update IT's overlay information,
5282 face information etc. */
5283
5284 void
5285 reseat_at_previous_visible_line_start (struct it *it)
5286 {
5287 back_to_previous_visible_line_start (it);
5288 reseat (it, it->current.pos, 1);
5289 CHECK_IT (it);
5290 }
5291
5292
5293 /* Reseat iterator IT on the next visible line start in the current
5294 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5295 preceding the line start. Skip over invisible text that is so
5296 because of selective display. Compute faces, overlays etc at the
5297 new position. Note that this function does not skip over text that
5298 is invisible because of text properties. */
5299
5300 static void
5301 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5302 {
5303 int newline_found_p, skipped_p = 0;
5304
5305 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5306
5307 /* Skip over lines that are invisible because they are indented
5308 more than the value of IT->selective. */
5309 if (it->selective > 0)
5310 while (IT_CHARPOS (*it) < ZV
5311 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5312 (double) it->selective)) /* iftc */
5313 {
5314 xassert (IT_BYTEPOS (*it) == BEGV
5315 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5316 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5317 }
5318
5319 /* Position on the newline if that's what's requested. */
5320 if (on_newline_p && newline_found_p)
5321 {
5322 if (STRINGP (it->string))
5323 {
5324 if (IT_STRING_CHARPOS (*it) > 0)
5325 {
5326 --IT_STRING_CHARPOS (*it);
5327 --IT_STRING_BYTEPOS (*it);
5328 }
5329 }
5330 else if (IT_CHARPOS (*it) > BEGV)
5331 {
5332 --IT_CHARPOS (*it);
5333 --IT_BYTEPOS (*it);
5334 reseat (it, it->current.pos, 0);
5335 }
5336 }
5337 else if (skipped_p)
5338 reseat (it, it->current.pos, 0);
5339
5340 CHECK_IT (it);
5341 }
5342
5343
5344 \f
5345 /***********************************************************************
5346 Changing an iterator's position
5347 ***********************************************************************/
5348
5349 /* Change IT's current position to POS in current_buffer. If FORCE_P
5350 is non-zero, always check for text properties at the new position.
5351 Otherwise, text properties are only looked up if POS >=
5352 IT->check_charpos of a property. */
5353
5354 static void
5355 reseat (struct it *it, struct text_pos pos, int force_p)
5356 {
5357 EMACS_INT original_pos = IT_CHARPOS (*it);
5358
5359 reseat_1 (it, pos, 0);
5360
5361 /* Determine where to check text properties. Avoid doing it
5362 where possible because text property lookup is very expensive. */
5363 if (force_p
5364 || CHARPOS (pos) > it->stop_charpos
5365 || CHARPOS (pos) < original_pos)
5366 {
5367 if (it->bidi_p)
5368 {
5369 /* For bidi iteration, we need to prime prev_stop and
5370 base_level_stop with our best estimations. */
5371 if (CHARPOS (pos) < it->prev_stop)
5372 {
5373 handle_stop_backwards (it, BEGV);
5374 if (CHARPOS (pos) < it->base_level_stop)
5375 it->base_level_stop = 0;
5376 }
5377 else if (CHARPOS (pos) > it->stop_charpos
5378 && it->stop_charpos >= BEGV)
5379 handle_stop_backwards (it, it->stop_charpos);
5380 else /* force_p */
5381 handle_stop (it);
5382 }
5383 else
5384 {
5385 handle_stop (it);
5386 it->prev_stop = it->base_level_stop = 0;
5387 }
5388
5389 }
5390
5391 CHECK_IT (it);
5392 }
5393
5394
5395 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5396 IT->stop_pos to POS, also. */
5397
5398 static void
5399 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5400 {
5401 /* Don't call this function when scanning a C string. */
5402 xassert (it->s == NULL);
5403
5404 /* POS must be a reasonable value. */
5405 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5406
5407 it->current.pos = it->position = pos;
5408 it->end_charpos = ZV;
5409 it->dpvec = NULL;
5410 it->current.dpvec_index = -1;
5411 it->current.overlay_string_index = -1;
5412 IT_STRING_CHARPOS (*it) = -1;
5413 IT_STRING_BYTEPOS (*it) = -1;
5414 it->string = Qnil;
5415 it->string_from_display_prop_p = 0;
5416 it->method = GET_FROM_BUFFER;
5417 it->object = it->w->buffer;
5418 it->area = TEXT_AREA;
5419 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5420 it->sp = 0;
5421 it->string_from_display_prop_p = 0;
5422 it->face_before_selective_p = 0;
5423 if (it->bidi_p)
5424 {
5425 it->bidi_it.first_elt = 1;
5426 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5427 }
5428
5429 if (set_stop_p)
5430 {
5431 it->stop_charpos = CHARPOS (pos);
5432 it->base_level_stop = CHARPOS (pos);
5433 }
5434 }
5435
5436
5437 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5438 If S is non-null, it is a C string to iterate over. Otherwise,
5439 STRING gives a Lisp string to iterate over.
5440
5441 If PRECISION > 0, don't return more then PRECISION number of
5442 characters from the string.
5443
5444 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5445 characters have been returned. FIELD_WIDTH < 0 means an infinite
5446 field width.
5447
5448 MULTIBYTE = 0 means disable processing of multibyte characters,
5449 MULTIBYTE > 0 means enable it,
5450 MULTIBYTE < 0 means use IT->multibyte_p.
5451
5452 IT must be initialized via a prior call to init_iterator before
5453 calling this function. */
5454
5455 static void
5456 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5457 EMACS_INT charpos, EMACS_INT precision, int field_width,
5458 int multibyte)
5459 {
5460 /* No region in strings. */
5461 it->region_beg_charpos = it->region_end_charpos = -1;
5462
5463 /* No text property checks performed by default, but see below. */
5464 it->stop_charpos = -1;
5465
5466 /* Set iterator position and end position. */
5467 memset (&it->current, 0, sizeof it->current);
5468 it->current.overlay_string_index = -1;
5469 it->current.dpvec_index = -1;
5470 xassert (charpos >= 0);
5471
5472 /* If STRING is specified, use its multibyteness, otherwise use the
5473 setting of MULTIBYTE, if specified. */
5474 if (multibyte >= 0)
5475 it->multibyte_p = multibyte > 0;
5476
5477 if (s == NULL)
5478 {
5479 xassert (STRINGP (string));
5480 it->string = string;
5481 it->s = NULL;
5482 it->end_charpos = it->string_nchars = SCHARS (string);
5483 it->method = GET_FROM_STRING;
5484 it->current.string_pos = string_pos (charpos, string);
5485 }
5486 else
5487 {
5488 it->s = (const unsigned char *) s;
5489 it->string = Qnil;
5490
5491 /* Note that we use IT->current.pos, not it->current.string_pos,
5492 for displaying C strings. */
5493 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5494 if (it->multibyte_p)
5495 {
5496 it->current.pos = c_string_pos (charpos, s, 1);
5497 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5498 }
5499 else
5500 {
5501 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5502 it->end_charpos = it->string_nchars = strlen (s);
5503 }
5504
5505 it->method = GET_FROM_C_STRING;
5506 }
5507
5508 /* PRECISION > 0 means don't return more than PRECISION characters
5509 from the string. */
5510 if (precision > 0 && it->end_charpos - charpos > precision)
5511 it->end_charpos = it->string_nchars = charpos + precision;
5512
5513 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5514 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5515 FIELD_WIDTH < 0 means infinite field width. This is useful for
5516 padding with `-' at the end of a mode line. */
5517 if (field_width < 0)
5518 field_width = INFINITY;
5519 if (field_width > it->end_charpos - charpos)
5520 it->end_charpos = charpos + field_width;
5521
5522 /* Use the standard display table for displaying strings. */
5523 if (DISP_TABLE_P (Vstandard_display_table))
5524 it->dp = XCHAR_TABLE (Vstandard_display_table);
5525
5526 it->stop_charpos = charpos;
5527 if (s == NULL && it->multibyte_p)
5528 {
5529 EMACS_INT endpos = SCHARS (it->string);
5530 if (endpos > it->end_charpos)
5531 endpos = it->end_charpos;
5532 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5533 it->string);
5534 }
5535 CHECK_IT (it);
5536 }
5537
5538
5539 \f
5540 /***********************************************************************
5541 Iteration
5542 ***********************************************************************/
5543
5544 /* Map enum it_method value to corresponding next_element_from_* function. */
5545
5546 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5547 {
5548 next_element_from_buffer,
5549 next_element_from_display_vector,
5550 next_element_from_string,
5551 next_element_from_c_string,
5552 next_element_from_image,
5553 next_element_from_stretch
5554 };
5555
5556 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5557
5558
5559 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5560 (possibly with the following characters). */
5561
5562 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5563 ((IT)->cmp_it.id >= 0 \
5564 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5565 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5566 END_CHARPOS, (IT)->w, \
5567 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5568 (IT)->string)))
5569
5570
5571 /* Lookup the char-table Vglyphless_char_display for character C (-1
5572 if we want information for no-font case), and return the display
5573 method symbol. By side-effect, update it->what and
5574 it->glyphless_method. This function is called from
5575 get_next_display_element for each character element, and from
5576 x_produce_glyphs when no suitable font was found. */
5577
5578 Lisp_Object
5579 lookup_glyphless_char_display (int c, struct it *it)
5580 {
5581 Lisp_Object glyphless_method = Qnil;
5582
5583 if (CHAR_TABLE_P (Vglyphless_char_display)
5584 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5585 glyphless_method = (c >= 0
5586 ? CHAR_TABLE_REF (Vglyphless_char_display, c)
5587 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
5588 retry:
5589 if (NILP (glyphless_method))
5590 {
5591 if (c >= 0)
5592 /* The default is to display the character by a proper font. */
5593 return Qnil;
5594 /* The default for the no-font case is to display an empty box. */
5595 glyphless_method = Qempty_box;
5596 }
5597 if (EQ (glyphless_method, Qzero_width))
5598 {
5599 if (c >= 0)
5600 return glyphless_method;
5601 /* This method can't be used for the no-font case. */
5602 glyphless_method = Qempty_box;
5603 }
5604 if (EQ (glyphless_method, Qthin_space))
5605 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5606 else if (EQ (glyphless_method, Qempty_box))
5607 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5608 else if (EQ (glyphless_method, Qhex_code))
5609 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5610 else if (STRINGP (glyphless_method))
5611 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5612 else
5613 {
5614 /* Invalid value. We use the default method. */
5615 glyphless_method = Qnil;
5616 goto retry;
5617 }
5618 it->what = IT_GLYPHLESS;
5619 return glyphless_method;
5620 }
5621
5622 /* Load IT's display element fields with information about the next
5623 display element from the current position of IT. Value is zero if
5624 end of buffer (or C string) is reached. */
5625
5626 static struct frame *last_escape_glyph_frame = NULL;
5627 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5628 static int last_escape_glyph_merged_face_id = 0;
5629
5630 struct frame *last_glyphless_glyph_frame = NULL;
5631 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5632 int last_glyphless_glyph_merged_face_id = 0;
5633
5634 int
5635 get_next_display_element (struct it *it)
5636 {
5637 /* Non-zero means that we found a display element. Zero means that
5638 we hit the end of what we iterate over. Performance note: the
5639 function pointer `method' used here turns out to be faster than
5640 using a sequence of if-statements. */
5641 int success_p;
5642
5643 get_next:
5644 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5645
5646 if (it->what == IT_CHARACTER)
5647 {
5648 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5649 and only if (a) the resolved directionality of that character
5650 is R..." */
5651 /* FIXME: Do we need an exception for characters from display
5652 tables? */
5653 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5654 it->c = bidi_mirror_char (it->c);
5655 /* Map via display table or translate control characters.
5656 IT->c, IT->len etc. have been set to the next character by
5657 the function call above. If we have a display table, and it
5658 contains an entry for IT->c, translate it. Don't do this if
5659 IT->c itself comes from a display table, otherwise we could
5660 end up in an infinite recursion. (An alternative could be to
5661 count the recursion depth of this function and signal an
5662 error when a certain maximum depth is reached.) Is it worth
5663 it? */
5664 if (success_p && it->dpvec == NULL)
5665 {
5666 Lisp_Object dv;
5667 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5668 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5669 nbsp_or_shy = char_is_other;
5670 int c = it->c; /* This is the character to display. */
5671
5672 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5673 {
5674 xassert (SINGLE_BYTE_CHAR_P (c));
5675 if (unibyte_display_via_language_environment)
5676 {
5677 c = DECODE_CHAR (unibyte, c);
5678 if (c < 0)
5679 c = BYTE8_TO_CHAR (it->c);
5680 }
5681 else
5682 c = BYTE8_TO_CHAR (it->c);
5683 }
5684
5685 if (it->dp
5686 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5687 VECTORP (dv)))
5688 {
5689 struct Lisp_Vector *v = XVECTOR (dv);
5690
5691 /* Return the first character from the display table
5692 entry, if not empty. If empty, don't display the
5693 current character. */
5694 if (v->size)
5695 {
5696 it->dpvec_char_len = it->len;
5697 it->dpvec = v->contents;
5698 it->dpend = v->contents + v->size;
5699 it->current.dpvec_index = 0;
5700 it->dpvec_face_id = -1;
5701 it->saved_face_id = it->face_id;
5702 it->method = GET_FROM_DISPLAY_VECTOR;
5703 it->ellipsis_p = 0;
5704 }
5705 else
5706 {
5707 set_iterator_to_next (it, 0);
5708 }
5709 goto get_next;
5710 }
5711
5712 if (! NILP (lookup_glyphless_char_display (c, it)))
5713 {
5714 if (it->what == IT_GLYPHLESS)
5715 goto done;
5716 /* Don't display this character. */
5717 set_iterator_to_next (it, 0);
5718 goto get_next;
5719 }
5720
5721 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5722 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5723 : c == 0xAD ? char_is_soft_hyphen
5724 : char_is_other);
5725
5726 /* Translate control characters into `\003' or `^C' form.
5727 Control characters coming from a display table entry are
5728 currently not translated because we use IT->dpvec to hold
5729 the translation. This could easily be changed but I
5730 don't believe that it is worth doing.
5731
5732 NBSP and SOFT-HYPEN are property translated too.
5733
5734 Non-printable characters and raw-byte characters are also
5735 translated to octal form. */
5736 if (((c < ' ' || c == 127) /* ASCII control chars */
5737 ? (it->area != TEXT_AREA
5738 /* In mode line, treat \n, \t like other crl chars. */
5739 || (c != '\t'
5740 && it->glyph_row
5741 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5742 || (c != '\n' && c != '\t'))
5743 : (nbsp_or_shy
5744 || CHAR_BYTE8_P (c)
5745 || ! CHAR_PRINTABLE_P (c))))
5746 {
5747 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5748 or a non-printable character which must be displayed
5749 either as '\003' or as `^C' where the '\\' and '^'
5750 can be defined in the display table. Fill
5751 IT->ctl_chars with glyphs for what we have to
5752 display. Then, set IT->dpvec to these glyphs. */
5753 Lisp_Object gc;
5754 int ctl_len;
5755 int face_id, lface_id = 0 ;
5756 int escape_glyph;
5757
5758 /* Handle control characters with ^. */
5759
5760 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5761 {
5762 int g;
5763
5764 g = '^'; /* default glyph for Control */
5765 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5766 if (it->dp
5767 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5768 && GLYPH_CODE_CHAR_VALID_P (gc))
5769 {
5770 g = GLYPH_CODE_CHAR (gc);
5771 lface_id = GLYPH_CODE_FACE (gc);
5772 }
5773 if (lface_id)
5774 {
5775 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5776 }
5777 else if (it->f == last_escape_glyph_frame
5778 && it->face_id == last_escape_glyph_face_id)
5779 {
5780 face_id = last_escape_glyph_merged_face_id;
5781 }
5782 else
5783 {
5784 /* Merge the escape-glyph face into the current face. */
5785 face_id = merge_faces (it->f, Qescape_glyph, 0,
5786 it->face_id);
5787 last_escape_glyph_frame = it->f;
5788 last_escape_glyph_face_id = it->face_id;
5789 last_escape_glyph_merged_face_id = face_id;
5790 }
5791
5792 XSETINT (it->ctl_chars[0], g);
5793 XSETINT (it->ctl_chars[1], c ^ 0100);
5794 ctl_len = 2;
5795 goto display_control;
5796 }
5797
5798 /* Handle non-break space in the mode where it only gets
5799 highlighting. */
5800
5801 if (EQ (Vnobreak_char_display, Qt)
5802 && nbsp_or_shy == char_is_nbsp)
5803 {
5804 /* Merge the no-break-space face into the current face. */
5805 face_id = merge_faces (it->f, Qnobreak_space, 0,
5806 it->face_id);
5807
5808 c = ' ';
5809 XSETINT (it->ctl_chars[0], ' ');
5810 ctl_len = 1;
5811 goto display_control;
5812 }
5813
5814 /* Handle sequences that start with the "escape glyph". */
5815
5816 /* the default escape glyph is \. */
5817 escape_glyph = '\\';
5818
5819 if (it->dp
5820 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5821 && GLYPH_CODE_CHAR_VALID_P (gc))
5822 {
5823 escape_glyph = GLYPH_CODE_CHAR (gc);
5824 lface_id = GLYPH_CODE_FACE (gc);
5825 }
5826 if (lface_id)
5827 {
5828 /* The display table specified a face.
5829 Merge it into face_id and also into escape_glyph. */
5830 face_id = merge_faces (it->f, Qt, lface_id,
5831 it->face_id);
5832 }
5833 else if (it->f == last_escape_glyph_frame
5834 && it->face_id == last_escape_glyph_face_id)
5835 {
5836 face_id = last_escape_glyph_merged_face_id;
5837 }
5838 else
5839 {
5840 /* Merge the escape-glyph face into the current face. */
5841 face_id = merge_faces (it->f, Qescape_glyph, 0,
5842 it->face_id);
5843 last_escape_glyph_frame = it->f;
5844 last_escape_glyph_face_id = it->face_id;
5845 last_escape_glyph_merged_face_id = face_id;
5846 }
5847
5848 /* Handle soft hyphens in the mode where they only get
5849 highlighting. */
5850
5851 if (EQ (Vnobreak_char_display, Qt)
5852 && nbsp_or_shy == char_is_soft_hyphen)
5853 {
5854 XSETINT (it->ctl_chars[0], '-');
5855 ctl_len = 1;
5856 goto display_control;
5857 }
5858
5859 /* Handle non-break space and soft hyphen
5860 with the escape glyph. */
5861
5862 if (nbsp_or_shy)
5863 {
5864 XSETINT (it->ctl_chars[0], escape_glyph);
5865 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5866 XSETINT (it->ctl_chars[1], c);
5867 ctl_len = 2;
5868 goto display_control;
5869 }
5870
5871 {
5872 char str[10];
5873 int len, i;
5874
5875 if (CHAR_BYTE8_P (c))
5876 /* Display \200 instead of \17777600. */
5877 c = CHAR_TO_BYTE8 (c);
5878 len = sprintf (str, "%03o", c);
5879
5880 XSETINT (it->ctl_chars[0], escape_glyph);
5881 for (i = 0; i < len; i++)
5882 XSETINT (it->ctl_chars[i + 1], str[i]);
5883 ctl_len = len + 1;
5884 }
5885
5886 display_control:
5887 /* Set up IT->dpvec and return first character from it. */
5888 it->dpvec_char_len = it->len;
5889 it->dpvec = it->ctl_chars;
5890 it->dpend = it->dpvec + ctl_len;
5891 it->current.dpvec_index = 0;
5892 it->dpvec_face_id = face_id;
5893 it->saved_face_id = it->face_id;
5894 it->method = GET_FROM_DISPLAY_VECTOR;
5895 it->ellipsis_p = 0;
5896 goto get_next;
5897 }
5898 it->char_to_display = c;
5899 }
5900 else if (success_p)
5901 {
5902 it->char_to_display = it->c;
5903 }
5904 }
5905
5906 #ifdef HAVE_WINDOW_SYSTEM
5907 /* Adjust face id for a multibyte character. There are no multibyte
5908 character in unibyte text. */
5909 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5910 && it->multibyte_p
5911 && success_p
5912 && FRAME_WINDOW_P (it->f))
5913 {
5914 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5915
5916 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5917 {
5918 /* Automatic composition with glyph-string. */
5919 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5920
5921 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5922 }
5923 else
5924 {
5925 EMACS_INT pos = (it->s ? -1
5926 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5927 : IT_CHARPOS (*it));
5928
5929 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
5930 it->string);
5931 }
5932 }
5933 #endif
5934
5935 done:
5936 /* Is this character the last one of a run of characters with
5937 box? If yes, set IT->end_of_box_run_p to 1. */
5938 if (it->face_box_p
5939 && it->s == NULL)
5940 {
5941 if (it->method == GET_FROM_STRING && it->sp)
5942 {
5943 int face_id = underlying_face_id (it);
5944 struct face *face = FACE_FROM_ID (it->f, face_id);
5945
5946 if (face)
5947 {
5948 if (face->box == FACE_NO_BOX)
5949 {
5950 /* If the box comes from face properties in a
5951 display string, check faces in that string. */
5952 int string_face_id = face_after_it_pos (it);
5953 it->end_of_box_run_p
5954 = (FACE_FROM_ID (it->f, string_face_id)->box
5955 == FACE_NO_BOX);
5956 }
5957 /* Otherwise, the box comes from the underlying face.
5958 If this is the last string character displayed, check
5959 the next buffer location. */
5960 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5961 && (it->current.overlay_string_index
5962 == it->n_overlay_strings - 1))
5963 {
5964 EMACS_INT ignore;
5965 int next_face_id;
5966 struct text_pos pos = it->current.pos;
5967 INC_TEXT_POS (pos, it->multibyte_p);
5968
5969 next_face_id = face_at_buffer_position
5970 (it->w, CHARPOS (pos), it->region_beg_charpos,
5971 it->region_end_charpos, &ignore,
5972 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5973 -1);
5974 it->end_of_box_run_p
5975 = (FACE_FROM_ID (it->f, next_face_id)->box
5976 == FACE_NO_BOX);
5977 }
5978 }
5979 }
5980 else
5981 {
5982 int face_id = face_after_it_pos (it);
5983 it->end_of_box_run_p
5984 = (face_id != it->face_id
5985 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
5986 }
5987 }
5988
5989 /* Value is 0 if end of buffer or string reached. */
5990 return success_p;
5991 }
5992
5993
5994 /* Move IT to the next display element.
5995
5996 RESEAT_P non-zero means if called on a newline in buffer text,
5997 skip to the next visible line start.
5998
5999 Functions get_next_display_element and set_iterator_to_next are
6000 separate because I find this arrangement easier to handle than a
6001 get_next_display_element function that also increments IT's
6002 position. The way it is we can first look at an iterator's current
6003 display element, decide whether it fits on a line, and if it does,
6004 increment the iterator position. The other way around we probably
6005 would either need a flag indicating whether the iterator has to be
6006 incremented the next time, or we would have to implement a
6007 decrement position function which would not be easy to write. */
6008
6009 void
6010 set_iterator_to_next (struct it *it, int reseat_p)
6011 {
6012 /* Reset flags indicating start and end of a sequence of characters
6013 with box. Reset them at the start of this function because
6014 moving the iterator to a new position might set them. */
6015 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6016
6017 switch (it->method)
6018 {
6019 case GET_FROM_BUFFER:
6020 /* The current display element of IT is a character from
6021 current_buffer. Advance in the buffer, and maybe skip over
6022 invisible lines that are so because of selective display. */
6023 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6024 reseat_at_next_visible_line_start (it, 0);
6025 else if (it->cmp_it.id >= 0)
6026 {
6027 /* We are currently getting glyphs from a composition. */
6028 int i;
6029
6030 if (! it->bidi_p)
6031 {
6032 IT_CHARPOS (*it) += it->cmp_it.nchars;
6033 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6034 if (it->cmp_it.to < it->cmp_it.nglyphs)
6035 {
6036 it->cmp_it.from = it->cmp_it.to;
6037 }
6038 else
6039 {
6040 it->cmp_it.id = -1;
6041 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6042 IT_BYTEPOS (*it),
6043 it->end_charpos, Qnil);
6044 }
6045 }
6046 else if (! it->cmp_it.reversed_p)
6047 {
6048 /* Composition created while scanning forward. */
6049 /* Update IT's char/byte positions to point to the first
6050 character of the next grapheme cluster, or to the
6051 character visually after the current composition. */
6052 for (i = 0; i < it->cmp_it.nchars; i++)
6053 bidi_move_to_visually_next (&it->bidi_it);
6054 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6055 IT_CHARPOS (*it) = it->bidi_it.charpos;
6056
6057 if (it->cmp_it.to < it->cmp_it.nglyphs)
6058 {
6059 /* Proceed to the next grapheme cluster. */
6060 it->cmp_it.from = it->cmp_it.to;
6061 }
6062 else
6063 {
6064 /* No more grapheme clusters in this composition.
6065 Find the next stop position. */
6066 EMACS_INT stop = it->end_charpos;
6067 if (it->bidi_it.scan_dir < 0)
6068 /* Now we are scanning backward and don't know
6069 where to stop. */
6070 stop = -1;
6071 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6072 IT_BYTEPOS (*it), stop, Qnil);
6073 }
6074 }
6075 else
6076 {
6077 /* Composition created while scanning backward. */
6078 /* Update IT's char/byte positions to point to the last
6079 character of the previous grapheme cluster, or the
6080 character visually after the current composition. */
6081 for (i = 0; i < it->cmp_it.nchars; i++)
6082 bidi_move_to_visually_next (&it->bidi_it);
6083 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6084 IT_CHARPOS (*it) = it->bidi_it.charpos;
6085 if (it->cmp_it.from > 0)
6086 {
6087 /* Proceed to the previous grapheme cluster. */
6088 it->cmp_it.to = it->cmp_it.from;
6089 }
6090 else
6091 {
6092 /* No more grapheme clusters in this composition.
6093 Find the next stop position. */
6094 EMACS_INT stop = it->end_charpos;
6095 if (it->bidi_it.scan_dir < 0)
6096 /* Now we are scanning backward and don't know
6097 where to stop. */
6098 stop = -1;
6099 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6100 IT_BYTEPOS (*it), stop, Qnil);
6101 }
6102 }
6103 }
6104 else
6105 {
6106 xassert (it->len != 0);
6107
6108 if (!it->bidi_p)
6109 {
6110 IT_BYTEPOS (*it) += it->len;
6111 IT_CHARPOS (*it) += 1;
6112 }
6113 else
6114 {
6115 int prev_scan_dir = it->bidi_it.scan_dir;
6116 /* If this is a new paragraph, determine its base
6117 direction (a.k.a. its base embedding level). */
6118 if (it->bidi_it.new_paragraph)
6119 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6120 bidi_move_to_visually_next (&it->bidi_it);
6121 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6122 IT_CHARPOS (*it) = it->bidi_it.charpos;
6123 if (prev_scan_dir != it->bidi_it.scan_dir)
6124 {
6125 /* As the scan direction was changed, we must
6126 re-compute the stop position for composition. */
6127 EMACS_INT stop = it->end_charpos;
6128 if (it->bidi_it.scan_dir < 0)
6129 stop = -1;
6130 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6131 IT_BYTEPOS (*it), stop, Qnil);
6132 }
6133 }
6134 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6135 }
6136 break;
6137
6138 case GET_FROM_C_STRING:
6139 /* Current display element of IT is from a C string. */
6140 IT_BYTEPOS (*it) += it->len;
6141 IT_CHARPOS (*it) += 1;
6142 break;
6143
6144 case GET_FROM_DISPLAY_VECTOR:
6145 /* Current display element of IT is from a display table entry.
6146 Advance in the display table definition. Reset it to null if
6147 end reached, and continue with characters from buffers/
6148 strings. */
6149 ++it->current.dpvec_index;
6150
6151 /* Restore face of the iterator to what they were before the
6152 display vector entry (these entries may contain faces). */
6153 it->face_id = it->saved_face_id;
6154
6155 if (it->dpvec + it->current.dpvec_index == it->dpend)
6156 {
6157 int recheck_faces = it->ellipsis_p;
6158
6159 if (it->s)
6160 it->method = GET_FROM_C_STRING;
6161 else if (STRINGP (it->string))
6162 it->method = GET_FROM_STRING;
6163 else
6164 {
6165 it->method = GET_FROM_BUFFER;
6166 it->object = it->w->buffer;
6167 }
6168
6169 it->dpvec = NULL;
6170 it->current.dpvec_index = -1;
6171
6172 /* Skip over characters which were displayed via IT->dpvec. */
6173 if (it->dpvec_char_len < 0)
6174 reseat_at_next_visible_line_start (it, 1);
6175 else if (it->dpvec_char_len > 0)
6176 {
6177 if (it->method == GET_FROM_STRING
6178 && it->n_overlay_strings > 0)
6179 it->ignore_overlay_strings_at_pos_p = 1;
6180 it->len = it->dpvec_char_len;
6181 set_iterator_to_next (it, reseat_p);
6182 }
6183
6184 /* Maybe recheck faces after display vector */
6185 if (recheck_faces)
6186 it->stop_charpos = IT_CHARPOS (*it);
6187 }
6188 break;
6189
6190 case GET_FROM_STRING:
6191 /* Current display element is a character from a Lisp string. */
6192 xassert (it->s == NULL && STRINGP (it->string));
6193 if (it->cmp_it.id >= 0)
6194 {
6195 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6196 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6197 if (it->cmp_it.to < it->cmp_it.nglyphs)
6198 it->cmp_it.from = it->cmp_it.to;
6199 else
6200 {
6201 it->cmp_it.id = -1;
6202 composition_compute_stop_pos (&it->cmp_it,
6203 IT_STRING_CHARPOS (*it),
6204 IT_STRING_BYTEPOS (*it),
6205 it->end_charpos, it->string);
6206 }
6207 }
6208 else
6209 {
6210 IT_STRING_BYTEPOS (*it) += it->len;
6211 IT_STRING_CHARPOS (*it) += 1;
6212 }
6213
6214 consider_string_end:
6215
6216 if (it->current.overlay_string_index >= 0)
6217 {
6218 /* IT->string is an overlay string. Advance to the
6219 next, if there is one. */
6220 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6221 {
6222 it->ellipsis_p = 0;
6223 next_overlay_string (it);
6224 if (it->ellipsis_p)
6225 setup_for_ellipsis (it, 0);
6226 }
6227 }
6228 else
6229 {
6230 /* IT->string is not an overlay string. If we reached
6231 its end, and there is something on IT->stack, proceed
6232 with what is on the stack. This can be either another
6233 string, this time an overlay string, or a buffer. */
6234 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6235 && it->sp > 0)
6236 {
6237 pop_it (it);
6238 if (it->method == GET_FROM_STRING)
6239 goto consider_string_end;
6240 }
6241 }
6242 break;
6243
6244 case GET_FROM_IMAGE:
6245 case GET_FROM_STRETCH:
6246 /* The position etc with which we have to proceed are on
6247 the stack. The position may be at the end of a string,
6248 if the `display' property takes up the whole string. */
6249 xassert (it->sp > 0);
6250 pop_it (it);
6251 if (it->method == GET_FROM_STRING)
6252 goto consider_string_end;
6253 break;
6254
6255 default:
6256 /* There are no other methods defined, so this should be a bug. */
6257 abort ();
6258 }
6259
6260 xassert (it->method != GET_FROM_STRING
6261 || (STRINGP (it->string)
6262 && IT_STRING_CHARPOS (*it) >= 0));
6263 }
6264
6265 /* Load IT's display element fields with information about the next
6266 display element which comes from a display table entry or from the
6267 result of translating a control character to one of the forms `^C'
6268 or `\003'.
6269
6270 IT->dpvec holds the glyphs to return as characters.
6271 IT->saved_face_id holds the face id before the display vector--it
6272 is restored into IT->face_id in set_iterator_to_next. */
6273
6274 static int
6275 next_element_from_display_vector (struct it *it)
6276 {
6277 Lisp_Object gc;
6278
6279 /* Precondition. */
6280 xassert (it->dpvec && it->current.dpvec_index >= 0);
6281
6282 it->face_id = it->saved_face_id;
6283
6284 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6285 That seemed totally bogus - so I changed it... */
6286 gc = it->dpvec[it->current.dpvec_index];
6287
6288 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6289 {
6290 it->c = GLYPH_CODE_CHAR (gc);
6291 it->len = CHAR_BYTES (it->c);
6292
6293 /* The entry may contain a face id to use. Such a face id is
6294 the id of a Lisp face, not a realized face. A face id of
6295 zero means no face is specified. */
6296 if (it->dpvec_face_id >= 0)
6297 it->face_id = it->dpvec_face_id;
6298 else
6299 {
6300 int lface_id = GLYPH_CODE_FACE (gc);
6301 if (lface_id > 0)
6302 it->face_id = merge_faces (it->f, Qt, lface_id,
6303 it->saved_face_id);
6304 }
6305 }
6306 else
6307 /* Display table entry is invalid. Return a space. */
6308 it->c = ' ', it->len = 1;
6309
6310 /* Don't change position and object of the iterator here. They are
6311 still the values of the character that had this display table
6312 entry or was translated, and that's what we want. */
6313 it->what = IT_CHARACTER;
6314 return 1;
6315 }
6316
6317
6318 /* Load IT with the next display element from Lisp string IT->string.
6319 IT->current.string_pos is the current position within the string.
6320 If IT->current.overlay_string_index >= 0, the Lisp string is an
6321 overlay string. */
6322
6323 static int
6324 next_element_from_string (struct it *it)
6325 {
6326 struct text_pos position;
6327
6328 xassert (STRINGP (it->string));
6329 xassert (IT_STRING_CHARPOS (*it) >= 0);
6330 position = it->current.string_pos;
6331
6332 /* Time to check for invisible text? */
6333 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6334 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6335 {
6336 handle_stop (it);
6337
6338 /* Since a handler may have changed IT->method, we must
6339 recurse here. */
6340 return GET_NEXT_DISPLAY_ELEMENT (it);
6341 }
6342
6343 if (it->current.overlay_string_index >= 0)
6344 {
6345 /* Get the next character from an overlay string. In overlay
6346 strings, There is no field width or padding with spaces to
6347 do. */
6348 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6349 {
6350 it->what = IT_EOB;
6351 return 0;
6352 }
6353 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6354 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6355 && next_element_from_composition (it))
6356 {
6357 return 1;
6358 }
6359 else if (STRING_MULTIBYTE (it->string))
6360 {
6361 const unsigned char *s = (SDATA (it->string)
6362 + IT_STRING_BYTEPOS (*it));
6363 it->c = string_char_and_length (s, &it->len);
6364 }
6365 else
6366 {
6367 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6368 it->len = 1;
6369 }
6370 }
6371 else
6372 {
6373 /* Get the next character from a Lisp string that is not an
6374 overlay string. Such strings come from the mode line, for
6375 example. We may have to pad with spaces, or truncate the
6376 string. See also next_element_from_c_string. */
6377 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6378 {
6379 it->what = IT_EOB;
6380 return 0;
6381 }
6382 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6383 {
6384 /* Pad with spaces. */
6385 it->c = ' ', it->len = 1;
6386 CHARPOS (position) = BYTEPOS (position) = -1;
6387 }
6388 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6389 IT_STRING_BYTEPOS (*it), it->string_nchars)
6390 && next_element_from_composition (it))
6391 {
6392 return 1;
6393 }
6394 else if (STRING_MULTIBYTE (it->string))
6395 {
6396 const unsigned char *s = (SDATA (it->string)
6397 + IT_STRING_BYTEPOS (*it));
6398 it->c = string_char_and_length (s, &it->len);
6399 }
6400 else
6401 {
6402 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6403 it->len = 1;
6404 }
6405 }
6406
6407 /* Record what we have and where it came from. */
6408 it->what = IT_CHARACTER;
6409 it->object = it->string;
6410 it->position = position;
6411 return 1;
6412 }
6413
6414
6415 /* Load IT with next display element from C string IT->s.
6416 IT->string_nchars is the maximum number of characters to return
6417 from the string. IT->end_charpos may be greater than
6418 IT->string_nchars when this function is called, in which case we
6419 may have to return padding spaces. Value is zero if end of string
6420 reached, including padding spaces. */
6421
6422 static int
6423 next_element_from_c_string (struct it *it)
6424 {
6425 int success_p = 1;
6426
6427 xassert (it->s);
6428 it->what = IT_CHARACTER;
6429 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6430 it->object = Qnil;
6431
6432 /* IT's position can be greater IT->string_nchars in case a field
6433 width or precision has been specified when the iterator was
6434 initialized. */
6435 if (IT_CHARPOS (*it) >= it->end_charpos)
6436 {
6437 /* End of the game. */
6438 it->what = IT_EOB;
6439 success_p = 0;
6440 }
6441 else if (IT_CHARPOS (*it) >= it->string_nchars)
6442 {
6443 /* Pad with spaces. */
6444 it->c = ' ', it->len = 1;
6445 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6446 }
6447 else if (it->multibyte_p)
6448 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6449 else
6450 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6451
6452 return success_p;
6453 }
6454
6455
6456 /* Set up IT to return characters from an ellipsis, if appropriate.
6457 The definition of the ellipsis glyphs may come from a display table
6458 entry. This function fills IT with the first glyph from the
6459 ellipsis if an ellipsis is to be displayed. */
6460
6461 static int
6462 next_element_from_ellipsis (struct it *it)
6463 {
6464 if (it->selective_display_ellipsis_p)
6465 setup_for_ellipsis (it, it->len);
6466 else
6467 {
6468 /* The face at the current position may be different from the
6469 face we find after the invisible text. Remember what it
6470 was in IT->saved_face_id, and signal that it's there by
6471 setting face_before_selective_p. */
6472 it->saved_face_id = it->face_id;
6473 it->method = GET_FROM_BUFFER;
6474 it->object = it->w->buffer;
6475 reseat_at_next_visible_line_start (it, 1);
6476 it->face_before_selective_p = 1;
6477 }
6478
6479 return GET_NEXT_DISPLAY_ELEMENT (it);
6480 }
6481
6482
6483 /* Deliver an image display element. The iterator IT is already
6484 filled with image information (done in handle_display_prop). Value
6485 is always 1. */
6486
6487
6488 static int
6489 next_element_from_image (struct it *it)
6490 {
6491 it->what = IT_IMAGE;
6492 it->ignore_overlay_strings_at_pos_p = 0;
6493 return 1;
6494 }
6495
6496
6497 /* Fill iterator IT with next display element from a stretch glyph
6498 property. IT->object is the value of the text property. Value is
6499 always 1. */
6500
6501 static int
6502 next_element_from_stretch (struct it *it)
6503 {
6504 it->what = IT_STRETCH;
6505 return 1;
6506 }
6507
6508 /* Scan forward from CHARPOS in the current buffer, until we find a
6509 stop position > current IT's position. Then handle the stop
6510 position before that. This is called when we bump into a stop
6511 position while reordering bidirectional text. CHARPOS should be
6512 the last previously processed stop_pos (or BEGV, if none were
6513 processed yet) whose position is less that IT's current
6514 position. */
6515
6516 static void
6517 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6518 {
6519 EMACS_INT where_we_are = IT_CHARPOS (*it);
6520 struct display_pos save_current = it->current;
6521 struct text_pos save_position = it->position;
6522 struct text_pos pos1;
6523 EMACS_INT next_stop;
6524
6525 /* Scan in strict logical order. */
6526 it->bidi_p = 0;
6527 do
6528 {
6529 it->prev_stop = charpos;
6530 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6531 reseat_1 (it, pos1, 0);
6532 compute_stop_pos (it);
6533 /* We must advance forward, right? */
6534 if (it->stop_charpos <= it->prev_stop)
6535 abort ();
6536 charpos = it->stop_charpos;
6537 }
6538 while (charpos <= where_we_are);
6539
6540 next_stop = it->stop_charpos;
6541 it->stop_charpos = it->prev_stop;
6542 it->bidi_p = 1;
6543 it->current = save_current;
6544 it->position = save_position;
6545 handle_stop (it);
6546 it->stop_charpos = next_stop;
6547 }
6548
6549 /* Load IT with the next display element from current_buffer. Value
6550 is zero if end of buffer reached. IT->stop_charpos is the next
6551 position at which to stop and check for text properties or buffer
6552 end. */
6553
6554 static int
6555 next_element_from_buffer (struct it *it)
6556 {
6557 int success_p = 1;
6558
6559 xassert (IT_CHARPOS (*it) >= BEGV);
6560
6561 /* With bidi reordering, the character to display might not be the
6562 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6563 we were reseat()ed to a new buffer position, which is potentially
6564 a different paragraph. */
6565 if (it->bidi_p && it->bidi_it.first_elt)
6566 {
6567 it->bidi_it.charpos = IT_CHARPOS (*it);
6568 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6569 if (it->bidi_it.bytepos == ZV_BYTE)
6570 {
6571 /* Nothing to do, but reset the FIRST_ELT flag, like
6572 bidi_paragraph_init does, because we are not going to
6573 call it. */
6574 it->bidi_it.first_elt = 0;
6575 }
6576 else if (it->bidi_it.bytepos == BEGV_BYTE
6577 /* FIXME: Should support all Unicode line separators. */
6578 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6579 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6580 {
6581 /* If we are at the beginning of a line, we can produce the
6582 next element right away. */
6583 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6584 bidi_move_to_visually_next (&it->bidi_it);
6585 }
6586 else
6587 {
6588 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6589
6590 /* We need to prime the bidi iterator starting at the line's
6591 beginning, before we will be able to produce the next
6592 element. */
6593 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6594 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6595 it->bidi_it.charpos = IT_CHARPOS (*it);
6596 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6597 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6598 do
6599 {
6600 /* Now return to buffer position where we were asked to
6601 get the next display element, and produce that. */
6602 bidi_move_to_visually_next (&it->bidi_it);
6603 }
6604 while (it->bidi_it.bytepos != orig_bytepos
6605 && it->bidi_it.bytepos < ZV_BYTE);
6606 }
6607
6608 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6609 /* Adjust IT's position information to where we ended up. */
6610 IT_CHARPOS (*it) = it->bidi_it.charpos;
6611 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6612 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6613 {
6614 EMACS_INT stop = it->end_charpos;
6615 if (it->bidi_it.scan_dir < 0)
6616 stop = -1;
6617 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6618 IT_BYTEPOS (*it), stop, Qnil);
6619 }
6620 }
6621
6622 if (IT_CHARPOS (*it) >= it->stop_charpos)
6623 {
6624 if (IT_CHARPOS (*it) >= it->end_charpos)
6625 {
6626 int overlay_strings_follow_p;
6627
6628 /* End of the game, except when overlay strings follow that
6629 haven't been returned yet. */
6630 if (it->overlay_strings_at_end_processed_p)
6631 overlay_strings_follow_p = 0;
6632 else
6633 {
6634 it->overlay_strings_at_end_processed_p = 1;
6635 overlay_strings_follow_p = get_overlay_strings (it, 0);
6636 }
6637
6638 if (overlay_strings_follow_p)
6639 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6640 else
6641 {
6642 it->what = IT_EOB;
6643 it->position = it->current.pos;
6644 success_p = 0;
6645 }
6646 }
6647 else if (!(!it->bidi_p
6648 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6649 || IT_CHARPOS (*it) == it->stop_charpos))
6650 {
6651 /* With bidi non-linear iteration, we could find ourselves
6652 far beyond the last computed stop_charpos, with several
6653 other stop positions in between that we missed. Scan
6654 them all now, in buffer's logical order, until we find
6655 and handle the last stop_charpos that precedes our
6656 current position. */
6657 handle_stop_backwards (it, it->stop_charpos);
6658 return GET_NEXT_DISPLAY_ELEMENT (it);
6659 }
6660 else
6661 {
6662 if (it->bidi_p)
6663 {
6664 /* Take note of the stop position we just moved across,
6665 for when we will move back across it. */
6666 it->prev_stop = it->stop_charpos;
6667 /* If we are at base paragraph embedding level, take
6668 note of the last stop position seen at this
6669 level. */
6670 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6671 it->base_level_stop = it->stop_charpos;
6672 }
6673 handle_stop (it);
6674 return GET_NEXT_DISPLAY_ELEMENT (it);
6675 }
6676 }
6677 else if (it->bidi_p
6678 /* We can sometimes back up for reasons that have nothing
6679 to do with bidi reordering. E.g., compositions. The
6680 code below is only needed when we are above the base
6681 embedding level, so test for that explicitly. */
6682 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6683 && IT_CHARPOS (*it) < it->prev_stop)
6684 {
6685 if (it->base_level_stop <= 0)
6686 it->base_level_stop = BEGV;
6687 if (IT_CHARPOS (*it) < it->base_level_stop)
6688 abort ();
6689 handle_stop_backwards (it, it->base_level_stop);
6690 return GET_NEXT_DISPLAY_ELEMENT (it);
6691 }
6692 else
6693 {
6694 /* No face changes, overlays etc. in sight, so just return a
6695 character from current_buffer. */
6696 unsigned char *p;
6697 EMACS_INT stop;
6698
6699 /* Maybe run the redisplay end trigger hook. Performance note:
6700 This doesn't seem to cost measurable time. */
6701 if (it->redisplay_end_trigger_charpos
6702 && it->glyph_row
6703 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6704 run_redisplay_end_trigger_hook (it);
6705
6706 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6707 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6708 stop)
6709 && next_element_from_composition (it))
6710 {
6711 return 1;
6712 }
6713
6714 /* Get the next character, maybe multibyte. */
6715 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6716 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6717 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6718 else
6719 it->c = *p, it->len = 1;
6720
6721 /* Record what we have and where it came from. */
6722 it->what = IT_CHARACTER;
6723 it->object = it->w->buffer;
6724 it->position = it->current.pos;
6725
6726 /* Normally we return the character found above, except when we
6727 really want to return an ellipsis for selective display. */
6728 if (it->selective)
6729 {
6730 if (it->c == '\n')
6731 {
6732 /* A value of selective > 0 means hide lines indented more
6733 than that number of columns. */
6734 if (it->selective > 0
6735 && IT_CHARPOS (*it) + 1 < ZV
6736 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6737 IT_BYTEPOS (*it) + 1,
6738 (double) it->selective)) /* iftc */
6739 {
6740 success_p = next_element_from_ellipsis (it);
6741 it->dpvec_char_len = -1;
6742 }
6743 }
6744 else if (it->c == '\r' && it->selective == -1)
6745 {
6746 /* A value of selective == -1 means that everything from the
6747 CR to the end of the line is invisible, with maybe an
6748 ellipsis displayed for it. */
6749 success_p = next_element_from_ellipsis (it);
6750 it->dpvec_char_len = -1;
6751 }
6752 }
6753 }
6754
6755 /* Value is zero if end of buffer reached. */
6756 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6757 return success_p;
6758 }
6759
6760
6761 /* Run the redisplay end trigger hook for IT. */
6762
6763 static void
6764 run_redisplay_end_trigger_hook (struct it *it)
6765 {
6766 Lisp_Object args[3];
6767
6768 /* IT->glyph_row should be non-null, i.e. we should be actually
6769 displaying something, or otherwise we should not run the hook. */
6770 xassert (it->glyph_row);
6771
6772 /* Set up hook arguments. */
6773 args[0] = Qredisplay_end_trigger_functions;
6774 args[1] = it->window;
6775 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6776 it->redisplay_end_trigger_charpos = 0;
6777
6778 /* Since we are *trying* to run these functions, don't try to run
6779 them again, even if they get an error. */
6780 it->w->redisplay_end_trigger = Qnil;
6781 Frun_hook_with_args (3, args);
6782
6783 /* Notice if it changed the face of the character we are on. */
6784 handle_face_prop (it);
6785 }
6786
6787
6788 /* Deliver a composition display element. Unlike the other
6789 next_element_from_XXX, this function is not registered in the array
6790 get_next_element[]. It is called from next_element_from_buffer and
6791 next_element_from_string when necessary. */
6792
6793 static int
6794 next_element_from_composition (struct it *it)
6795 {
6796 it->what = IT_COMPOSITION;
6797 it->len = it->cmp_it.nbytes;
6798 if (STRINGP (it->string))
6799 {
6800 if (it->c < 0)
6801 {
6802 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6803 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6804 return 0;
6805 }
6806 it->position = it->current.string_pos;
6807 it->object = it->string;
6808 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6809 IT_STRING_BYTEPOS (*it), it->string);
6810 }
6811 else
6812 {
6813 if (it->c < 0)
6814 {
6815 IT_CHARPOS (*it) += it->cmp_it.nchars;
6816 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6817 if (it->bidi_p)
6818 {
6819 if (it->bidi_it.new_paragraph)
6820 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6821 /* Resync the bidi iterator with IT's new position.
6822 FIXME: this doesn't support bidirectional text. */
6823 while (it->bidi_it.charpos < IT_CHARPOS (*it))
6824 bidi_move_to_visually_next (&it->bidi_it);
6825 }
6826 return 0;
6827 }
6828 it->position = it->current.pos;
6829 it->object = it->w->buffer;
6830 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6831 IT_BYTEPOS (*it), Qnil);
6832 }
6833 return 1;
6834 }
6835
6836
6837 \f
6838 /***********************************************************************
6839 Moving an iterator without producing glyphs
6840 ***********************************************************************/
6841
6842 /* Check if iterator is at a position corresponding to a valid buffer
6843 position after some move_it_ call. */
6844
6845 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6846 ((it)->method == GET_FROM_STRING \
6847 ? IT_STRING_CHARPOS (*it) == 0 \
6848 : 1)
6849
6850
6851 /* Move iterator IT to a specified buffer or X position within one
6852 line on the display without producing glyphs.
6853
6854 OP should be a bit mask including some or all of these bits:
6855 MOVE_TO_X: Stop upon reaching x-position TO_X.
6856 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6857 Regardless of OP's value, stop upon reaching the end of the display line.
6858
6859 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6860 This means, in particular, that TO_X includes window's horizontal
6861 scroll amount.
6862
6863 The return value has several possible values that
6864 say what condition caused the scan to stop:
6865
6866 MOVE_POS_MATCH_OR_ZV
6867 - when TO_POS or ZV was reached.
6868
6869 MOVE_X_REACHED
6870 -when TO_X was reached before TO_POS or ZV were reached.
6871
6872 MOVE_LINE_CONTINUED
6873 - when we reached the end of the display area and the line must
6874 be continued.
6875
6876 MOVE_LINE_TRUNCATED
6877 - when we reached the end of the display area and the line is
6878 truncated.
6879
6880 MOVE_NEWLINE_OR_CR
6881 - when we stopped at a line end, i.e. a newline or a CR and selective
6882 display is on. */
6883
6884 static enum move_it_result
6885 move_it_in_display_line_to (struct it *it,
6886 EMACS_INT to_charpos, int to_x,
6887 enum move_operation_enum op)
6888 {
6889 enum move_it_result result = MOVE_UNDEFINED;
6890 struct glyph_row *saved_glyph_row;
6891 struct it wrap_it, atpos_it, atx_it;
6892 int may_wrap = 0;
6893 enum it_method prev_method = it->method;
6894 EMACS_INT prev_pos = IT_CHARPOS (*it);
6895
6896 /* Don't produce glyphs in produce_glyphs. */
6897 saved_glyph_row = it->glyph_row;
6898 it->glyph_row = NULL;
6899
6900 /* Use wrap_it to save a copy of IT wherever a word wrap could
6901 occur. Use atpos_it to save a copy of IT at the desired buffer
6902 position, if found, so that we can scan ahead and check if the
6903 word later overshoots the window edge. Use atx_it similarly, for
6904 pixel positions. */
6905 wrap_it.sp = -1;
6906 atpos_it.sp = -1;
6907 atx_it.sp = -1;
6908
6909 #define BUFFER_POS_REACHED_P() \
6910 ((op & MOVE_TO_POS) != 0 \
6911 && BUFFERP (it->object) \
6912 && (IT_CHARPOS (*it) == to_charpos \
6913 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
6914 && (it->method == GET_FROM_BUFFER \
6915 || (it->method == GET_FROM_DISPLAY_VECTOR \
6916 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6917
6918 /* If there's a line-/wrap-prefix, handle it. */
6919 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6920 && it->current_y < it->last_visible_y)
6921 handle_line_prefix (it);
6922
6923 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
6924 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6925
6926 while (1)
6927 {
6928 int x, i, ascent = 0, descent = 0;
6929
6930 /* Utility macro to reset an iterator with x, ascent, and descent. */
6931 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6932 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6933 (IT)->max_descent = descent)
6934
6935 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6936 glyph). */
6937 if ((op & MOVE_TO_POS) != 0
6938 && BUFFERP (it->object)
6939 && it->method == GET_FROM_BUFFER
6940 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
6941 || (it->bidi_p
6942 && (prev_method == GET_FROM_IMAGE
6943 || prev_method == GET_FROM_STRETCH)
6944 /* Passed TO_CHARPOS from left to right. */
6945 && ((prev_pos < to_charpos
6946 && IT_CHARPOS (*it) > to_charpos)
6947 /* Passed TO_CHARPOS from right to left. */
6948 || (prev_pos > to_charpos
6949 && IT_CHARPOS (*it) < to_charpos)))))
6950 {
6951 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6952 {
6953 result = MOVE_POS_MATCH_OR_ZV;
6954 break;
6955 }
6956 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6957 /* If wrap_it is valid, the current position might be in a
6958 word that is wrapped. So, save the iterator in
6959 atpos_it and continue to see if wrapping happens. */
6960 atpos_it = *it;
6961 }
6962
6963 prev_method = it->method;
6964 if (it->method == GET_FROM_BUFFER)
6965 prev_pos = IT_CHARPOS (*it);
6966 /* Stop when ZV reached.
6967 We used to stop here when TO_CHARPOS reached as well, but that is
6968 too soon if this glyph does not fit on this line. So we handle it
6969 explicitly below. */
6970 if (!get_next_display_element (it))
6971 {
6972 result = MOVE_POS_MATCH_OR_ZV;
6973 break;
6974 }
6975
6976 if (it->line_wrap == TRUNCATE)
6977 {
6978 if (BUFFER_POS_REACHED_P ())
6979 {
6980 result = MOVE_POS_MATCH_OR_ZV;
6981 break;
6982 }
6983 }
6984 else
6985 {
6986 if (it->line_wrap == WORD_WRAP)
6987 {
6988 if (IT_DISPLAYING_WHITESPACE (it))
6989 may_wrap = 1;
6990 else if (may_wrap)
6991 {
6992 /* We have reached a glyph that follows one or more
6993 whitespace characters. If the position is
6994 already found, we are done. */
6995 if (atpos_it.sp >= 0)
6996 {
6997 *it = atpos_it;
6998 result = MOVE_POS_MATCH_OR_ZV;
6999 goto done;
7000 }
7001 if (atx_it.sp >= 0)
7002 {
7003 *it = atx_it;
7004 result = MOVE_X_REACHED;
7005 goto done;
7006 }
7007 /* Otherwise, we can wrap here. */
7008 wrap_it = *it;
7009 may_wrap = 0;
7010 }
7011 }
7012 }
7013
7014 /* Remember the line height for the current line, in case
7015 the next element doesn't fit on the line. */
7016 ascent = it->max_ascent;
7017 descent = it->max_descent;
7018
7019 /* The call to produce_glyphs will get the metrics of the
7020 display element IT is loaded with. Record the x-position
7021 before this display element, in case it doesn't fit on the
7022 line. */
7023 x = it->current_x;
7024
7025 PRODUCE_GLYPHS (it);
7026
7027 if (it->area != TEXT_AREA)
7028 {
7029 set_iterator_to_next (it, 1);
7030 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7031 SET_TEXT_POS (this_line_min_pos,
7032 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7033 continue;
7034 }
7035
7036 /* The number of glyphs we get back in IT->nglyphs will normally
7037 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7038 character on a terminal frame, or (iii) a line end. For the
7039 second case, IT->nglyphs - 1 padding glyphs will be present.
7040 (On X frames, there is only one glyph produced for a
7041 composite character.)
7042
7043 The behavior implemented below means, for continuation lines,
7044 that as many spaces of a TAB as fit on the current line are
7045 displayed there. For terminal frames, as many glyphs of a
7046 multi-glyph character are displayed in the current line, too.
7047 This is what the old redisplay code did, and we keep it that
7048 way. Under X, the whole shape of a complex character must
7049 fit on the line or it will be completely displayed in the
7050 next line.
7051
7052 Note that both for tabs and padding glyphs, all glyphs have
7053 the same width. */
7054 if (it->nglyphs)
7055 {
7056 /* More than one glyph or glyph doesn't fit on line. All
7057 glyphs have the same width. */
7058 int single_glyph_width = it->pixel_width / it->nglyphs;
7059 int new_x;
7060 int x_before_this_char = x;
7061 int hpos_before_this_char = it->hpos;
7062
7063 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7064 {
7065 new_x = x + single_glyph_width;
7066
7067 /* We want to leave anything reaching TO_X to the caller. */
7068 if ((op & MOVE_TO_X) && new_x > to_x)
7069 {
7070 if (BUFFER_POS_REACHED_P ())
7071 {
7072 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7073 goto buffer_pos_reached;
7074 if (atpos_it.sp < 0)
7075 {
7076 atpos_it = *it;
7077 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7078 }
7079 }
7080 else
7081 {
7082 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7083 {
7084 it->current_x = x;
7085 result = MOVE_X_REACHED;
7086 break;
7087 }
7088 if (atx_it.sp < 0)
7089 {
7090 atx_it = *it;
7091 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7092 }
7093 }
7094 }
7095
7096 if (/* Lines are continued. */
7097 it->line_wrap != TRUNCATE
7098 && (/* And glyph doesn't fit on the line. */
7099 new_x > it->last_visible_x
7100 /* Or it fits exactly and we're on a window
7101 system frame. */
7102 || (new_x == it->last_visible_x
7103 && FRAME_WINDOW_P (it->f))))
7104 {
7105 if (/* IT->hpos == 0 means the very first glyph
7106 doesn't fit on the line, e.g. a wide image. */
7107 it->hpos == 0
7108 || (new_x == it->last_visible_x
7109 && FRAME_WINDOW_P (it->f)))
7110 {
7111 ++it->hpos;
7112 it->current_x = new_x;
7113
7114 /* The character's last glyph just barely fits
7115 in this row. */
7116 if (i == it->nglyphs - 1)
7117 {
7118 /* If this is the destination position,
7119 return a position *before* it in this row,
7120 now that we know it fits in this row. */
7121 if (BUFFER_POS_REACHED_P ())
7122 {
7123 if (it->line_wrap != WORD_WRAP
7124 || wrap_it.sp < 0)
7125 {
7126 it->hpos = hpos_before_this_char;
7127 it->current_x = x_before_this_char;
7128 result = MOVE_POS_MATCH_OR_ZV;
7129 break;
7130 }
7131 if (it->line_wrap == WORD_WRAP
7132 && atpos_it.sp < 0)
7133 {
7134 atpos_it = *it;
7135 atpos_it.current_x = x_before_this_char;
7136 atpos_it.hpos = hpos_before_this_char;
7137 }
7138 }
7139
7140 set_iterator_to_next (it, 1);
7141 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7142 SET_TEXT_POS (this_line_min_pos,
7143 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7144 /* On graphical terminals, newlines may
7145 "overflow" into the fringe if
7146 overflow-newline-into-fringe is non-nil.
7147 On text-only terminals, newlines may
7148 overflow into the last glyph on the
7149 display line.*/
7150 if (!FRAME_WINDOW_P (it->f)
7151 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7152 {
7153 if (!get_next_display_element (it))
7154 {
7155 result = MOVE_POS_MATCH_OR_ZV;
7156 break;
7157 }
7158 if (BUFFER_POS_REACHED_P ())
7159 {
7160 if (ITERATOR_AT_END_OF_LINE_P (it))
7161 result = MOVE_POS_MATCH_OR_ZV;
7162 else
7163 result = MOVE_LINE_CONTINUED;
7164 break;
7165 }
7166 if (ITERATOR_AT_END_OF_LINE_P (it))
7167 {
7168 result = MOVE_NEWLINE_OR_CR;
7169 break;
7170 }
7171 }
7172 }
7173 }
7174 else
7175 IT_RESET_X_ASCENT_DESCENT (it);
7176
7177 if (wrap_it.sp >= 0)
7178 {
7179 *it = wrap_it;
7180 atpos_it.sp = -1;
7181 atx_it.sp = -1;
7182 }
7183
7184 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7185 IT_CHARPOS (*it)));
7186 result = MOVE_LINE_CONTINUED;
7187 break;
7188 }
7189
7190 if (BUFFER_POS_REACHED_P ())
7191 {
7192 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7193 goto buffer_pos_reached;
7194 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7195 {
7196 atpos_it = *it;
7197 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7198 }
7199 }
7200
7201 if (new_x > it->first_visible_x)
7202 {
7203 /* Glyph is visible. Increment number of glyphs that
7204 would be displayed. */
7205 ++it->hpos;
7206 }
7207 }
7208
7209 if (result != MOVE_UNDEFINED)
7210 break;
7211 }
7212 else if (BUFFER_POS_REACHED_P ())
7213 {
7214 buffer_pos_reached:
7215 IT_RESET_X_ASCENT_DESCENT (it);
7216 result = MOVE_POS_MATCH_OR_ZV;
7217 break;
7218 }
7219 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7220 {
7221 /* Stop when TO_X specified and reached. This check is
7222 necessary here because of lines consisting of a line end,
7223 only. The line end will not produce any glyphs and we
7224 would never get MOVE_X_REACHED. */
7225 xassert (it->nglyphs == 0);
7226 result = MOVE_X_REACHED;
7227 break;
7228 }
7229
7230 /* Is this a line end? If yes, we're done. */
7231 if (ITERATOR_AT_END_OF_LINE_P (it))
7232 {
7233 result = MOVE_NEWLINE_OR_CR;
7234 break;
7235 }
7236
7237 if (it->method == GET_FROM_BUFFER)
7238 prev_pos = IT_CHARPOS (*it);
7239 /* The current display element has been consumed. Advance
7240 to the next. */
7241 set_iterator_to_next (it, 1);
7242 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7243 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7244
7245 /* Stop if lines are truncated and IT's current x-position is
7246 past the right edge of the window now. */
7247 if (it->line_wrap == TRUNCATE
7248 && it->current_x >= it->last_visible_x)
7249 {
7250 if (!FRAME_WINDOW_P (it->f)
7251 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7252 {
7253 if (!get_next_display_element (it)
7254 || BUFFER_POS_REACHED_P ())
7255 {
7256 result = MOVE_POS_MATCH_OR_ZV;
7257 break;
7258 }
7259 if (ITERATOR_AT_END_OF_LINE_P (it))
7260 {
7261 result = MOVE_NEWLINE_OR_CR;
7262 break;
7263 }
7264 }
7265 result = MOVE_LINE_TRUNCATED;
7266 break;
7267 }
7268 #undef IT_RESET_X_ASCENT_DESCENT
7269 }
7270
7271 #undef BUFFER_POS_REACHED_P
7272
7273 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7274 restore the saved iterator. */
7275 if (atpos_it.sp >= 0)
7276 *it = atpos_it;
7277 else if (atx_it.sp >= 0)
7278 *it = atx_it;
7279
7280 done:
7281
7282 /* Restore the iterator settings altered at the beginning of this
7283 function. */
7284 it->glyph_row = saved_glyph_row;
7285 return result;
7286 }
7287
7288 /* For external use. */
7289 void
7290 move_it_in_display_line (struct it *it,
7291 EMACS_INT to_charpos, int to_x,
7292 enum move_operation_enum op)
7293 {
7294 if (it->line_wrap == WORD_WRAP
7295 && (op & MOVE_TO_X))
7296 {
7297 struct it save_it = *it;
7298 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7299 /* When word-wrap is on, TO_X may lie past the end
7300 of a wrapped line. Then it->current is the
7301 character on the next line, so backtrack to the
7302 space before the wrap point. */
7303 if (skip == MOVE_LINE_CONTINUED)
7304 {
7305 int prev_x = max (it->current_x - 1, 0);
7306 *it = save_it;
7307 move_it_in_display_line_to
7308 (it, -1, prev_x, MOVE_TO_X);
7309 }
7310 }
7311 else
7312 move_it_in_display_line_to (it, to_charpos, to_x, op);
7313 }
7314
7315
7316 /* Move IT forward until it satisfies one or more of the criteria in
7317 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7318
7319 OP is a bit-mask that specifies where to stop, and in particular,
7320 which of those four position arguments makes a difference. See the
7321 description of enum move_operation_enum.
7322
7323 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7324 screen line, this function will set IT to the next position >
7325 TO_CHARPOS. */
7326
7327 void
7328 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7329 {
7330 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7331 int line_height, line_start_x = 0, reached = 0;
7332
7333 for (;;)
7334 {
7335 if (op & MOVE_TO_VPOS)
7336 {
7337 /* If no TO_CHARPOS and no TO_X specified, stop at the
7338 start of the line TO_VPOS. */
7339 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7340 {
7341 if (it->vpos == to_vpos)
7342 {
7343 reached = 1;
7344 break;
7345 }
7346 else
7347 skip = move_it_in_display_line_to (it, -1, -1, 0);
7348 }
7349 else
7350 {
7351 /* TO_VPOS >= 0 means stop at TO_X in the line at
7352 TO_VPOS, or at TO_POS, whichever comes first. */
7353 if (it->vpos == to_vpos)
7354 {
7355 reached = 2;
7356 break;
7357 }
7358
7359 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7360
7361 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7362 {
7363 reached = 3;
7364 break;
7365 }
7366 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7367 {
7368 /* We have reached TO_X but not in the line we want. */
7369 skip = move_it_in_display_line_to (it, to_charpos,
7370 -1, MOVE_TO_POS);
7371 if (skip == MOVE_POS_MATCH_OR_ZV)
7372 {
7373 reached = 4;
7374 break;
7375 }
7376 }
7377 }
7378 }
7379 else if (op & MOVE_TO_Y)
7380 {
7381 struct it it_backup;
7382
7383 if (it->line_wrap == WORD_WRAP)
7384 it_backup = *it;
7385
7386 /* TO_Y specified means stop at TO_X in the line containing
7387 TO_Y---or at TO_CHARPOS if this is reached first. The
7388 problem is that we can't really tell whether the line
7389 contains TO_Y before we have completely scanned it, and
7390 this may skip past TO_X. What we do is to first scan to
7391 TO_X.
7392
7393 If TO_X is not specified, use a TO_X of zero. The reason
7394 is to make the outcome of this function more predictable.
7395 If we didn't use TO_X == 0, we would stop at the end of
7396 the line which is probably not what a caller would expect
7397 to happen. */
7398 skip = move_it_in_display_line_to
7399 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7400 (MOVE_TO_X | (op & MOVE_TO_POS)));
7401
7402 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7403 if (skip == MOVE_POS_MATCH_OR_ZV)
7404 reached = 5;
7405 else if (skip == MOVE_X_REACHED)
7406 {
7407 /* If TO_X was reached, we want to know whether TO_Y is
7408 in the line. We know this is the case if the already
7409 scanned glyphs make the line tall enough. Otherwise,
7410 we must check by scanning the rest of the line. */
7411 line_height = it->max_ascent + it->max_descent;
7412 if (to_y >= it->current_y
7413 && to_y < it->current_y + line_height)
7414 {
7415 reached = 6;
7416 break;
7417 }
7418 it_backup = *it;
7419 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7420 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7421 op & MOVE_TO_POS);
7422 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7423 line_height = it->max_ascent + it->max_descent;
7424 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7425
7426 if (to_y >= it->current_y
7427 && to_y < it->current_y + line_height)
7428 {
7429 /* If TO_Y is in this line and TO_X was reached
7430 above, we scanned too far. We have to restore
7431 IT's settings to the ones before skipping. */
7432 *it = it_backup;
7433 reached = 6;
7434 }
7435 else
7436 {
7437 skip = skip2;
7438 if (skip == MOVE_POS_MATCH_OR_ZV)
7439 reached = 7;
7440 }
7441 }
7442 else
7443 {
7444 /* Check whether TO_Y is in this line. */
7445 line_height = it->max_ascent + it->max_descent;
7446 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7447
7448 if (to_y >= it->current_y
7449 && to_y < it->current_y + line_height)
7450 {
7451 /* When word-wrap is on, TO_X may lie past the end
7452 of a wrapped line. Then it->current is the
7453 character on the next line, so backtrack to the
7454 space before the wrap point. */
7455 if (skip == MOVE_LINE_CONTINUED
7456 && it->line_wrap == WORD_WRAP)
7457 {
7458 int prev_x = max (it->current_x - 1, 0);
7459 *it = it_backup;
7460 skip = move_it_in_display_line_to
7461 (it, -1, prev_x, MOVE_TO_X);
7462 }
7463 reached = 6;
7464 }
7465 }
7466
7467 if (reached)
7468 break;
7469 }
7470 else if (BUFFERP (it->object)
7471 && (it->method == GET_FROM_BUFFER
7472 || it->method == GET_FROM_STRETCH)
7473 && IT_CHARPOS (*it) >= to_charpos)
7474 skip = MOVE_POS_MATCH_OR_ZV;
7475 else
7476 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7477
7478 switch (skip)
7479 {
7480 case MOVE_POS_MATCH_OR_ZV:
7481 reached = 8;
7482 goto out;
7483
7484 case MOVE_NEWLINE_OR_CR:
7485 set_iterator_to_next (it, 1);
7486 it->continuation_lines_width = 0;
7487 break;
7488
7489 case MOVE_LINE_TRUNCATED:
7490 it->continuation_lines_width = 0;
7491 reseat_at_next_visible_line_start (it, 0);
7492 if ((op & MOVE_TO_POS) != 0
7493 && IT_CHARPOS (*it) > to_charpos)
7494 {
7495 reached = 9;
7496 goto out;
7497 }
7498 break;
7499
7500 case MOVE_LINE_CONTINUED:
7501 /* For continued lines ending in a tab, some of the glyphs
7502 associated with the tab are displayed on the current
7503 line. Since it->current_x does not include these glyphs,
7504 we use it->last_visible_x instead. */
7505 if (it->c == '\t')
7506 {
7507 it->continuation_lines_width += it->last_visible_x;
7508 /* When moving by vpos, ensure that the iterator really
7509 advances to the next line (bug#847, bug#969). Fixme:
7510 do we need to do this in other circumstances? */
7511 if (it->current_x != it->last_visible_x
7512 && (op & MOVE_TO_VPOS)
7513 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7514 {
7515 line_start_x = it->current_x + it->pixel_width
7516 - it->last_visible_x;
7517 set_iterator_to_next (it, 0);
7518 }
7519 }
7520 else
7521 it->continuation_lines_width += it->current_x;
7522 break;
7523
7524 default:
7525 abort ();
7526 }
7527
7528 /* Reset/increment for the next run. */
7529 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7530 it->current_x = line_start_x;
7531 line_start_x = 0;
7532 it->hpos = 0;
7533 it->current_y += it->max_ascent + it->max_descent;
7534 ++it->vpos;
7535 last_height = it->max_ascent + it->max_descent;
7536 last_max_ascent = it->max_ascent;
7537 it->max_ascent = it->max_descent = 0;
7538 }
7539
7540 out:
7541
7542 /* On text terminals, we may stop at the end of a line in the middle
7543 of a multi-character glyph. If the glyph itself is continued,
7544 i.e. it is actually displayed on the next line, don't treat this
7545 stopping point as valid; move to the next line instead (unless
7546 that brings us offscreen). */
7547 if (!FRAME_WINDOW_P (it->f)
7548 && op & MOVE_TO_POS
7549 && IT_CHARPOS (*it) == to_charpos
7550 && it->what == IT_CHARACTER
7551 && it->nglyphs > 1
7552 && it->line_wrap == WINDOW_WRAP
7553 && it->current_x == it->last_visible_x - 1
7554 && it->c != '\n'
7555 && it->c != '\t'
7556 && it->vpos < XFASTINT (it->w->window_end_vpos))
7557 {
7558 it->continuation_lines_width += it->current_x;
7559 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7560 it->current_y += it->max_ascent + it->max_descent;
7561 ++it->vpos;
7562 last_height = it->max_ascent + it->max_descent;
7563 last_max_ascent = it->max_ascent;
7564 }
7565
7566 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7567 }
7568
7569
7570 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7571
7572 If DY > 0, move IT backward at least that many pixels. DY = 0
7573 means move IT backward to the preceding line start or BEGV. This
7574 function may move over more than DY pixels if IT->current_y - DY
7575 ends up in the middle of a line; in this case IT->current_y will be
7576 set to the top of the line moved to. */
7577
7578 void
7579 move_it_vertically_backward (struct it *it, int dy)
7580 {
7581 int nlines, h;
7582 struct it it2, it3;
7583 EMACS_INT start_pos;
7584
7585 move_further_back:
7586 xassert (dy >= 0);
7587
7588 start_pos = IT_CHARPOS (*it);
7589
7590 /* Estimate how many newlines we must move back. */
7591 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7592
7593 /* Set the iterator's position that many lines back. */
7594 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7595 back_to_previous_visible_line_start (it);
7596
7597 /* Reseat the iterator here. When moving backward, we don't want
7598 reseat to skip forward over invisible text, set up the iterator
7599 to deliver from overlay strings at the new position etc. So,
7600 use reseat_1 here. */
7601 reseat_1 (it, it->current.pos, 1);
7602
7603 /* We are now surely at a line start. */
7604 it->current_x = it->hpos = 0;
7605 it->continuation_lines_width = 0;
7606
7607 /* Move forward and see what y-distance we moved. First move to the
7608 start of the next line so that we get its height. We need this
7609 height to be able to tell whether we reached the specified
7610 y-distance. */
7611 it2 = *it;
7612 it2.max_ascent = it2.max_descent = 0;
7613 do
7614 {
7615 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7616 MOVE_TO_POS | MOVE_TO_VPOS);
7617 }
7618 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7619 xassert (IT_CHARPOS (*it) >= BEGV);
7620 it3 = it2;
7621
7622 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7623 xassert (IT_CHARPOS (*it) >= BEGV);
7624 /* H is the actual vertical distance from the position in *IT
7625 and the starting position. */
7626 h = it2.current_y - it->current_y;
7627 /* NLINES is the distance in number of lines. */
7628 nlines = it2.vpos - it->vpos;
7629
7630 /* Correct IT's y and vpos position
7631 so that they are relative to the starting point. */
7632 it->vpos -= nlines;
7633 it->current_y -= h;
7634
7635 if (dy == 0)
7636 {
7637 /* DY == 0 means move to the start of the screen line. The
7638 value of nlines is > 0 if continuation lines were involved. */
7639 if (nlines > 0)
7640 move_it_by_lines (it, nlines);
7641 }
7642 else
7643 {
7644 /* The y-position we try to reach, relative to *IT.
7645 Note that H has been subtracted in front of the if-statement. */
7646 int target_y = it->current_y + h - dy;
7647 int y0 = it3.current_y;
7648 int y1 = line_bottom_y (&it3);
7649 int line_height = y1 - y0;
7650
7651 /* If we did not reach target_y, try to move further backward if
7652 we can. If we moved too far backward, try to move forward. */
7653 if (target_y < it->current_y
7654 /* This is heuristic. In a window that's 3 lines high, with
7655 a line height of 13 pixels each, recentering with point
7656 on the bottom line will try to move -39/2 = 19 pixels
7657 backward. Try to avoid moving into the first line. */
7658 && (it->current_y - target_y
7659 > min (window_box_height (it->w), line_height * 2 / 3))
7660 && IT_CHARPOS (*it) > BEGV)
7661 {
7662 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7663 target_y - it->current_y));
7664 dy = it->current_y - target_y;
7665 goto move_further_back;
7666 }
7667 else if (target_y >= it->current_y + line_height
7668 && IT_CHARPOS (*it) < ZV)
7669 {
7670 /* Should move forward by at least one line, maybe more.
7671
7672 Note: Calling move_it_by_lines can be expensive on
7673 terminal frames, where compute_motion is used (via
7674 vmotion) to do the job, when there are very long lines
7675 and truncate-lines is nil. That's the reason for
7676 treating terminal frames specially here. */
7677
7678 if (!FRAME_WINDOW_P (it->f))
7679 move_it_vertically (it, target_y - (it->current_y + line_height));
7680 else
7681 {
7682 do
7683 {
7684 move_it_by_lines (it, 1);
7685 }
7686 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7687 }
7688 }
7689 }
7690 }
7691
7692
7693 /* Move IT by a specified amount of pixel lines DY. DY negative means
7694 move backwards. DY = 0 means move to start of screen line. At the
7695 end, IT will be on the start of a screen line. */
7696
7697 void
7698 move_it_vertically (struct it *it, int dy)
7699 {
7700 if (dy <= 0)
7701 move_it_vertically_backward (it, -dy);
7702 else
7703 {
7704 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7705 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7706 MOVE_TO_POS | MOVE_TO_Y);
7707 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7708
7709 /* If buffer ends in ZV without a newline, move to the start of
7710 the line to satisfy the post-condition. */
7711 if (IT_CHARPOS (*it) == ZV
7712 && ZV > BEGV
7713 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7714 move_it_by_lines (it, 0);
7715 }
7716 }
7717
7718
7719 /* Move iterator IT past the end of the text line it is in. */
7720
7721 void
7722 move_it_past_eol (struct it *it)
7723 {
7724 enum move_it_result rc;
7725
7726 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7727 if (rc == MOVE_NEWLINE_OR_CR)
7728 set_iterator_to_next (it, 0);
7729 }
7730
7731
7732 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7733 negative means move up. DVPOS == 0 means move to the start of the
7734 screen line.
7735
7736 Optimization idea: If we would know that IT->f doesn't use
7737 a face with proportional font, we could be faster for
7738 truncate-lines nil. */
7739
7740 void
7741 move_it_by_lines (struct it *it, int dvpos)
7742 {
7743
7744 /* The commented-out optimization uses vmotion on terminals. This
7745 gives bad results, because elements like it->what, on which
7746 callers such as pos_visible_p rely, aren't updated. */
7747 /* struct position pos;
7748 if (!FRAME_WINDOW_P (it->f))
7749 {
7750 struct text_pos textpos;
7751
7752 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7753 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7754 reseat (it, textpos, 1);
7755 it->vpos += pos.vpos;
7756 it->current_y += pos.vpos;
7757 }
7758 else */
7759
7760 if (dvpos == 0)
7761 {
7762 /* DVPOS == 0 means move to the start of the screen line. */
7763 move_it_vertically_backward (it, 0);
7764 xassert (it->current_x == 0 && it->hpos == 0);
7765 /* Let next call to line_bottom_y calculate real line height */
7766 last_height = 0;
7767 }
7768 else if (dvpos > 0)
7769 {
7770 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7771 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7772 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7773 }
7774 else
7775 {
7776 struct it it2;
7777 EMACS_INT start_charpos, i;
7778
7779 /* Start at the beginning of the screen line containing IT's
7780 position. This may actually move vertically backwards,
7781 in case of overlays, so adjust dvpos accordingly. */
7782 dvpos += it->vpos;
7783 move_it_vertically_backward (it, 0);
7784 dvpos -= it->vpos;
7785
7786 /* Go back -DVPOS visible lines and reseat the iterator there. */
7787 start_charpos = IT_CHARPOS (*it);
7788 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7789 back_to_previous_visible_line_start (it);
7790 reseat (it, it->current.pos, 1);
7791
7792 /* Move further back if we end up in a string or an image. */
7793 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7794 {
7795 /* First try to move to start of display line. */
7796 dvpos += it->vpos;
7797 move_it_vertically_backward (it, 0);
7798 dvpos -= it->vpos;
7799 if (IT_POS_VALID_AFTER_MOVE_P (it))
7800 break;
7801 /* If start of line is still in string or image,
7802 move further back. */
7803 back_to_previous_visible_line_start (it);
7804 reseat (it, it->current.pos, 1);
7805 dvpos--;
7806 }
7807
7808 it->current_x = it->hpos = 0;
7809
7810 /* Above call may have moved too far if continuation lines
7811 are involved. Scan forward and see if it did. */
7812 it2 = *it;
7813 it2.vpos = it2.current_y = 0;
7814 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7815 it->vpos -= it2.vpos;
7816 it->current_y -= it2.current_y;
7817 it->current_x = it->hpos = 0;
7818
7819 /* If we moved too far back, move IT some lines forward. */
7820 if (it2.vpos > -dvpos)
7821 {
7822 int delta = it2.vpos + dvpos;
7823 it2 = *it;
7824 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7825 /* Move back again if we got too far ahead. */
7826 if (IT_CHARPOS (*it) >= start_charpos)
7827 *it = it2;
7828 }
7829 }
7830 }
7831
7832 /* Return 1 if IT points into the middle of a display vector. */
7833
7834 int
7835 in_display_vector_p (struct it *it)
7836 {
7837 return (it->method == GET_FROM_DISPLAY_VECTOR
7838 && it->current.dpvec_index > 0
7839 && it->dpvec + it->current.dpvec_index != it->dpend);
7840 }
7841
7842 \f
7843 /***********************************************************************
7844 Messages
7845 ***********************************************************************/
7846
7847
7848 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7849 to *Messages*. */
7850
7851 void
7852 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
7853 {
7854 Lisp_Object args[3];
7855 Lisp_Object msg, fmt;
7856 char *buffer;
7857 EMACS_INT len;
7858 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7859 USE_SAFE_ALLOCA;
7860
7861 /* Do nothing if called asynchronously. Inserting text into
7862 a buffer may call after-change-functions and alike and
7863 that would means running Lisp asynchronously. */
7864 if (handling_signal)
7865 return;
7866
7867 fmt = msg = Qnil;
7868 GCPRO4 (fmt, msg, arg1, arg2);
7869
7870 args[0] = fmt = build_string (format);
7871 args[1] = arg1;
7872 args[2] = arg2;
7873 msg = Fformat (3, args);
7874
7875 len = SBYTES (msg) + 1;
7876 SAFE_ALLOCA (buffer, char *, len);
7877 memcpy (buffer, SDATA (msg), len);
7878
7879 message_dolog (buffer, len - 1, 1, 0);
7880 SAFE_FREE ();
7881
7882 UNGCPRO;
7883 }
7884
7885
7886 /* Output a newline in the *Messages* buffer if "needs" one. */
7887
7888 void
7889 message_log_maybe_newline (void)
7890 {
7891 if (message_log_need_newline)
7892 message_dolog ("", 0, 1, 0);
7893 }
7894
7895
7896 /* Add a string M of length NBYTES to the message log, optionally
7897 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7898 nonzero, means interpret the contents of M as multibyte. This
7899 function calls low-level routines in order to bypass text property
7900 hooks, etc. which might not be safe to run.
7901
7902 This may GC (insert may run before/after change hooks),
7903 so the buffer M must NOT point to a Lisp string. */
7904
7905 void
7906 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
7907 {
7908 const unsigned char *msg = (const unsigned char *) m;
7909
7910 if (!NILP (Vmemory_full))
7911 return;
7912
7913 if (!NILP (Vmessage_log_max))
7914 {
7915 struct buffer *oldbuf;
7916 Lisp_Object oldpoint, oldbegv, oldzv;
7917 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7918 EMACS_INT point_at_end = 0;
7919 EMACS_INT zv_at_end = 0;
7920 Lisp_Object old_deactivate_mark, tem;
7921 struct gcpro gcpro1;
7922
7923 old_deactivate_mark = Vdeactivate_mark;
7924 oldbuf = current_buffer;
7925 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7926 BVAR (current_buffer, undo_list) = Qt;
7927
7928 oldpoint = message_dolog_marker1;
7929 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7930 oldbegv = message_dolog_marker2;
7931 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7932 oldzv = message_dolog_marker3;
7933 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7934 GCPRO1 (old_deactivate_mark);
7935
7936 if (PT == Z)
7937 point_at_end = 1;
7938 if (ZV == Z)
7939 zv_at_end = 1;
7940
7941 BEGV = BEG;
7942 BEGV_BYTE = BEG_BYTE;
7943 ZV = Z;
7944 ZV_BYTE = Z_BYTE;
7945 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7946
7947 /* Insert the string--maybe converting multibyte to single byte
7948 or vice versa, so that all the text fits the buffer. */
7949 if (multibyte
7950 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
7951 {
7952 EMACS_INT i;
7953 int c, char_bytes;
7954 char work[1];
7955
7956 /* Convert a multibyte string to single-byte
7957 for the *Message* buffer. */
7958 for (i = 0; i < nbytes; i += char_bytes)
7959 {
7960 c = string_char_and_length (msg + i, &char_bytes);
7961 work[0] = (ASCII_CHAR_P (c)
7962 ? c
7963 : multibyte_char_to_unibyte (c));
7964 insert_1_both (work, 1, 1, 1, 0, 0);
7965 }
7966 }
7967 else if (! multibyte
7968 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
7969 {
7970 EMACS_INT i;
7971 int c, char_bytes;
7972 unsigned char str[MAX_MULTIBYTE_LENGTH];
7973 /* Convert a single-byte string to multibyte
7974 for the *Message* buffer. */
7975 for (i = 0; i < nbytes; i++)
7976 {
7977 c = msg[i];
7978 MAKE_CHAR_MULTIBYTE (c);
7979 char_bytes = CHAR_STRING (c, str);
7980 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
7981 }
7982 }
7983 else if (nbytes)
7984 insert_1 (m, nbytes, 1, 0, 0);
7985
7986 if (nlflag)
7987 {
7988 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
7989 unsigned long int dups;
7990 insert_1 ("\n", 1, 1, 0, 0);
7991
7992 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7993 this_bol = PT;
7994 this_bol_byte = PT_BYTE;
7995
7996 /* See if this line duplicates the previous one.
7997 If so, combine duplicates. */
7998 if (this_bol > BEG)
7999 {
8000 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8001 prev_bol = PT;
8002 prev_bol_byte = PT_BYTE;
8003
8004 dups = message_log_check_duplicate (prev_bol_byte,
8005 this_bol_byte);
8006 if (dups)
8007 {
8008 del_range_both (prev_bol, prev_bol_byte,
8009 this_bol, this_bol_byte, 0);
8010 if (dups > 1)
8011 {
8012 char dupstr[40];
8013 int duplen;
8014
8015 /* If you change this format, don't forget to also
8016 change message_log_check_duplicate. */
8017 sprintf (dupstr, " [%lu times]", dups);
8018 duplen = strlen (dupstr);
8019 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8020 insert_1 (dupstr, duplen, 1, 0, 1);
8021 }
8022 }
8023 }
8024
8025 /* If we have more than the desired maximum number of lines
8026 in the *Messages* buffer now, delete the oldest ones.
8027 This is safe because we don't have undo in this buffer. */
8028
8029 if (NATNUMP (Vmessage_log_max))
8030 {
8031 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8032 -XFASTINT (Vmessage_log_max) - 1, 0);
8033 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8034 }
8035 }
8036 BEGV = XMARKER (oldbegv)->charpos;
8037 BEGV_BYTE = marker_byte_position (oldbegv);
8038
8039 if (zv_at_end)
8040 {
8041 ZV = Z;
8042 ZV_BYTE = Z_BYTE;
8043 }
8044 else
8045 {
8046 ZV = XMARKER (oldzv)->charpos;
8047 ZV_BYTE = marker_byte_position (oldzv);
8048 }
8049
8050 if (point_at_end)
8051 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8052 else
8053 /* We can't do Fgoto_char (oldpoint) because it will run some
8054 Lisp code. */
8055 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8056 XMARKER (oldpoint)->bytepos);
8057
8058 UNGCPRO;
8059 unchain_marker (XMARKER (oldpoint));
8060 unchain_marker (XMARKER (oldbegv));
8061 unchain_marker (XMARKER (oldzv));
8062
8063 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8064 set_buffer_internal (oldbuf);
8065 if (NILP (tem))
8066 windows_or_buffers_changed = old_windows_or_buffers_changed;
8067 message_log_need_newline = !nlflag;
8068 Vdeactivate_mark = old_deactivate_mark;
8069 }
8070 }
8071
8072
8073 /* We are at the end of the buffer after just having inserted a newline.
8074 (Note: We depend on the fact we won't be crossing the gap.)
8075 Check to see if the most recent message looks a lot like the previous one.
8076 Return 0 if different, 1 if the new one should just replace it, or a
8077 value N > 1 if we should also append " [N times]". */
8078
8079 static unsigned long int
8080 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8081 {
8082 EMACS_INT i;
8083 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8084 int seen_dots = 0;
8085 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8086 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8087
8088 for (i = 0; i < len; i++)
8089 {
8090 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8091 seen_dots = 1;
8092 if (p1[i] != p2[i])
8093 return seen_dots;
8094 }
8095 p1 += len;
8096 if (*p1 == '\n')
8097 return 2;
8098 if (*p1++ == ' ' && *p1++ == '[')
8099 {
8100 char *pend;
8101 unsigned long int n = strtoul ((char *) p1, &pend, 10);
8102 if (strncmp (pend, " times]\n", 8) == 0)
8103 return n+1;
8104 }
8105 return 0;
8106 }
8107 \f
8108
8109 /* Display an echo area message M with a specified length of NBYTES
8110 bytes. The string may include null characters. If M is 0, clear
8111 out any existing message, and let the mini-buffer text show
8112 through.
8113
8114 This may GC, so the buffer M must NOT point to a Lisp string. */
8115
8116 void
8117 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8118 {
8119 /* First flush out any partial line written with print. */
8120 message_log_maybe_newline ();
8121 if (m)
8122 message_dolog (m, nbytes, 1, multibyte);
8123 message2_nolog (m, nbytes, multibyte);
8124 }
8125
8126
8127 /* The non-logging counterpart of message2. */
8128
8129 void
8130 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8131 {
8132 struct frame *sf = SELECTED_FRAME ();
8133 message_enable_multibyte = multibyte;
8134
8135 if (FRAME_INITIAL_P (sf))
8136 {
8137 if (noninteractive_need_newline)
8138 putc ('\n', stderr);
8139 noninteractive_need_newline = 0;
8140 if (m)
8141 fwrite (m, nbytes, 1, stderr);
8142 if (cursor_in_echo_area == 0)
8143 fprintf (stderr, "\n");
8144 fflush (stderr);
8145 }
8146 /* A null message buffer means that the frame hasn't really been
8147 initialized yet. Error messages get reported properly by
8148 cmd_error, so this must be just an informative message; toss it. */
8149 else if (INTERACTIVE
8150 && sf->glyphs_initialized_p
8151 && FRAME_MESSAGE_BUF (sf))
8152 {
8153 Lisp_Object mini_window;
8154 struct frame *f;
8155
8156 /* Get the frame containing the mini-buffer
8157 that the selected frame is using. */
8158 mini_window = FRAME_MINIBUF_WINDOW (sf);
8159 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8160
8161 FRAME_SAMPLE_VISIBILITY (f);
8162 if (FRAME_VISIBLE_P (sf)
8163 && ! FRAME_VISIBLE_P (f))
8164 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8165
8166 if (m)
8167 {
8168 set_message (m, Qnil, nbytes, multibyte);
8169 if (minibuffer_auto_raise)
8170 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8171 }
8172 else
8173 clear_message (1, 1);
8174
8175 do_pending_window_change (0);
8176 echo_area_display (1);
8177 do_pending_window_change (0);
8178 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8179 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8180 }
8181 }
8182
8183
8184 /* Display an echo area message M with a specified length of NBYTES
8185 bytes. The string may include null characters. If M is not a
8186 string, clear out any existing message, and let the mini-buffer
8187 text show through.
8188
8189 This function cancels echoing. */
8190
8191 void
8192 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8193 {
8194 struct gcpro gcpro1;
8195
8196 GCPRO1 (m);
8197 clear_message (1,1);
8198 cancel_echoing ();
8199
8200 /* First flush out any partial line written with print. */
8201 message_log_maybe_newline ();
8202 if (STRINGP (m))
8203 {
8204 char *buffer;
8205 USE_SAFE_ALLOCA;
8206
8207 SAFE_ALLOCA (buffer, char *, nbytes);
8208 memcpy (buffer, SDATA (m), nbytes);
8209 message_dolog (buffer, nbytes, 1, multibyte);
8210 SAFE_FREE ();
8211 }
8212 message3_nolog (m, nbytes, multibyte);
8213
8214 UNGCPRO;
8215 }
8216
8217
8218 /* The non-logging version of message3.
8219 This does not cancel echoing, because it is used for echoing.
8220 Perhaps we need to make a separate function for echoing
8221 and make this cancel echoing. */
8222
8223 void
8224 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8225 {
8226 struct frame *sf = SELECTED_FRAME ();
8227 message_enable_multibyte = multibyte;
8228
8229 if (FRAME_INITIAL_P (sf))
8230 {
8231 if (noninteractive_need_newline)
8232 putc ('\n', stderr);
8233 noninteractive_need_newline = 0;
8234 if (STRINGP (m))
8235 fwrite (SDATA (m), nbytes, 1, stderr);
8236 if (cursor_in_echo_area == 0)
8237 fprintf (stderr, "\n");
8238 fflush (stderr);
8239 }
8240 /* A null message buffer means that the frame hasn't really been
8241 initialized yet. Error messages get reported properly by
8242 cmd_error, so this must be just an informative message; toss it. */
8243 else if (INTERACTIVE
8244 && sf->glyphs_initialized_p
8245 && FRAME_MESSAGE_BUF (sf))
8246 {
8247 Lisp_Object mini_window;
8248 Lisp_Object frame;
8249 struct frame *f;
8250
8251 /* Get the frame containing the mini-buffer
8252 that the selected frame is using. */
8253 mini_window = FRAME_MINIBUF_WINDOW (sf);
8254 frame = XWINDOW (mini_window)->frame;
8255 f = XFRAME (frame);
8256
8257 FRAME_SAMPLE_VISIBILITY (f);
8258 if (FRAME_VISIBLE_P (sf)
8259 && !FRAME_VISIBLE_P (f))
8260 Fmake_frame_visible (frame);
8261
8262 if (STRINGP (m) && SCHARS (m) > 0)
8263 {
8264 set_message (NULL, m, nbytes, multibyte);
8265 if (minibuffer_auto_raise)
8266 Fraise_frame (frame);
8267 /* Assume we are not echoing.
8268 (If we are, echo_now will override this.) */
8269 echo_message_buffer = Qnil;
8270 }
8271 else
8272 clear_message (1, 1);
8273
8274 do_pending_window_change (0);
8275 echo_area_display (1);
8276 do_pending_window_change (0);
8277 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8278 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8279 }
8280 }
8281
8282
8283 /* Display a null-terminated echo area message M. If M is 0, clear
8284 out any existing message, and let the mini-buffer text show through.
8285
8286 The buffer M must continue to exist until after the echo area gets
8287 cleared or some other message gets displayed there. Do not pass
8288 text that is stored in a Lisp string. Do not pass text in a buffer
8289 that was alloca'd. */
8290
8291 void
8292 message1 (const char *m)
8293 {
8294 message2 (m, (m ? strlen (m) : 0), 0);
8295 }
8296
8297
8298 /* The non-logging counterpart of message1. */
8299
8300 void
8301 message1_nolog (const char *m)
8302 {
8303 message2_nolog (m, (m ? strlen (m) : 0), 0);
8304 }
8305
8306 /* Display a message M which contains a single %s
8307 which gets replaced with STRING. */
8308
8309 void
8310 message_with_string (const char *m, Lisp_Object string, int log)
8311 {
8312 CHECK_STRING (string);
8313
8314 if (noninteractive)
8315 {
8316 if (m)
8317 {
8318 if (noninteractive_need_newline)
8319 putc ('\n', stderr);
8320 noninteractive_need_newline = 0;
8321 fprintf (stderr, m, SDATA (string));
8322 if (!cursor_in_echo_area)
8323 fprintf (stderr, "\n");
8324 fflush (stderr);
8325 }
8326 }
8327 else if (INTERACTIVE)
8328 {
8329 /* The frame whose minibuffer we're going to display the message on.
8330 It may be larger than the selected frame, so we need
8331 to use its buffer, not the selected frame's buffer. */
8332 Lisp_Object mini_window;
8333 struct frame *f, *sf = SELECTED_FRAME ();
8334
8335 /* Get the frame containing the minibuffer
8336 that the selected frame is using. */
8337 mini_window = FRAME_MINIBUF_WINDOW (sf);
8338 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8339
8340 /* A null message buffer means that the frame hasn't really been
8341 initialized yet. Error messages get reported properly by
8342 cmd_error, so this must be just an informative message; toss it. */
8343 if (FRAME_MESSAGE_BUF (f))
8344 {
8345 Lisp_Object args[2], msg;
8346 struct gcpro gcpro1, gcpro2;
8347
8348 args[0] = build_string (m);
8349 args[1] = msg = string;
8350 GCPRO2 (args[0], msg);
8351 gcpro1.nvars = 2;
8352
8353 msg = Fformat (2, args);
8354
8355 if (log)
8356 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8357 else
8358 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8359
8360 UNGCPRO;
8361
8362 /* Print should start at the beginning of the message
8363 buffer next time. */
8364 message_buf_print = 0;
8365 }
8366 }
8367 }
8368
8369
8370 /* Dump an informative message to the minibuf. If M is 0, clear out
8371 any existing message, and let the mini-buffer text show through. */
8372
8373 static void
8374 vmessage (const char *m, va_list ap)
8375 {
8376 if (noninteractive)
8377 {
8378 if (m)
8379 {
8380 if (noninteractive_need_newline)
8381 putc ('\n', stderr);
8382 noninteractive_need_newline = 0;
8383 vfprintf (stderr, m, ap);
8384 if (cursor_in_echo_area == 0)
8385 fprintf (stderr, "\n");
8386 fflush (stderr);
8387 }
8388 }
8389 else if (INTERACTIVE)
8390 {
8391 /* The frame whose mini-buffer we're going to display the message
8392 on. It may be larger than the selected frame, so we need to
8393 use its buffer, not the selected frame's buffer. */
8394 Lisp_Object mini_window;
8395 struct frame *f, *sf = SELECTED_FRAME ();
8396
8397 /* Get the frame containing the mini-buffer
8398 that the selected frame is using. */
8399 mini_window = FRAME_MINIBUF_WINDOW (sf);
8400 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8401
8402 /* A null message buffer means that the frame hasn't really been
8403 initialized yet. Error messages get reported properly by
8404 cmd_error, so this must be just an informative message; toss
8405 it. */
8406 if (FRAME_MESSAGE_BUF (f))
8407 {
8408 if (m)
8409 {
8410 EMACS_INT len;
8411
8412 len = doprnt (FRAME_MESSAGE_BUF (f),
8413 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8414
8415 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8416 }
8417 else
8418 message1 (0);
8419
8420 /* Print should start at the beginning of the message
8421 buffer next time. */
8422 message_buf_print = 0;
8423 }
8424 }
8425 }
8426
8427 void
8428 message (const char *m, ...)
8429 {
8430 va_list ap;
8431 va_start (ap, m);
8432 vmessage (m, ap);
8433 va_end (ap);
8434 }
8435
8436
8437 /* Display the current message in the current mini-buffer. This is
8438 only called from error handlers in process.c, and is not time
8439 critical. */
8440
8441 void
8442 update_echo_area (void)
8443 {
8444 if (!NILP (echo_area_buffer[0]))
8445 {
8446 Lisp_Object string;
8447 string = Fcurrent_message ();
8448 message3 (string, SBYTES (string),
8449 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
8450 }
8451 }
8452
8453
8454 /* Make sure echo area buffers in `echo_buffers' are live.
8455 If they aren't, make new ones. */
8456
8457 static void
8458 ensure_echo_area_buffers (void)
8459 {
8460 int i;
8461
8462 for (i = 0; i < 2; ++i)
8463 if (!BUFFERP (echo_buffer[i])
8464 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
8465 {
8466 char name[30];
8467 Lisp_Object old_buffer;
8468 int j;
8469
8470 old_buffer = echo_buffer[i];
8471 sprintf (name, " *Echo Area %d*", i);
8472 echo_buffer[i] = Fget_buffer_create (build_string (name));
8473 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
8474 /* to force word wrap in echo area -
8475 it was decided to postpone this*/
8476 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8477
8478 for (j = 0; j < 2; ++j)
8479 if (EQ (old_buffer, echo_area_buffer[j]))
8480 echo_area_buffer[j] = echo_buffer[i];
8481 }
8482 }
8483
8484
8485 /* Call FN with args A1..A4 with either the current or last displayed
8486 echo_area_buffer as current buffer.
8487
8488 WHICH zero means use the current message buffer
8489 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8490 from echo_buffer[] and clear it.
8491
8492 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8493 suitable buffer from echo_buffer[] and clear it.
8494
8495 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8496 that the current message becomes the last displayed one, make
8497 choose a suitable buffer for echo_area_buffer[0], and clear it.
8498
8499 Value is what FN returns. */
8500
8501 static int
8502 with_echo_area_buffer (struct window *w, int which,
8503 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8504 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8505 {
8506 Lisp_Object buffer;
8507 int this_one, the_other, clear_buffer_p, rc;
8508 int count = SPECPDL_INDEX ();
8509
8510 /* If buffers aren't live, make new ones. */
8511 ensure_echo_area_buffers ();
8512
8513 clear_buffer_p = 0;
8514
8515 if (which == 0)
8516 this_one = 0, the_other = 1;
8517 else if (which > 0)
8518 this_one = 1, the_other = 0;
8519 else
8520 {
8521 this_one = 0, the_other = 1;
8522 clear_buffer_p = 1;
8523
8524 /* We need a fresh one in case the current echo buffer equals
8525 the one containing the last displayed echo area message. */
8526 if (!NILP (echo_area_buffer[this_one])
8527 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8528 echo_area_buffer[this_one] = Qnil;
8529 }
8530
8531 /* Choose a suitable buffer from echo_buffer[] is we don't
8532 have one. */
8533 if (NILP (echo_area_buffer[this_one]))
8534 {
8535 echo_area_buffer[this_one]
8536 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8537 ? echo_buffer[the_other]
8538 : echo_buffer[this_one]);
8539 clear_buffer_p = 1;
8540 }
8541
8542 buffer = echo_area_buffer[this_one];
8543
8544 /* Don't get confused by reusing the buffer used for echoing
8545 for a different purpose. */
8546 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8547 cancel_echoing ();
8548
8549 record_unwind_protect (unwind_with_echo_area_buffer,
8550 with_echo_area_buffer_unwind_data (w));
8551
8552 /* Make the echo area buffer current. Note that for display
8553 purposes, it is not necessary that the displayed window's buffer
8554 == current_buffer, except for text property lookup. So, let's
8555 only set that buffer temporarily here without doing a full
8556 Fset_window_buffer. We must also change w->pointm, though,
8557 because otherwise an assertions in unshow_buffer fails, and Emacs
8558 aborts. */
8559 set_buffer_internal_1 (XBUFFER (buffer));
8560 if (w)
8561 {
8562 w->buffer = buffer;
8563 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8564 }
8565
8566 BVAR (current_buffer, undo_list) = Qt;
8567 BVAR (current_buffer, read_only) = Qnil;
8568 specbind (Qinhibit_read_only, Qt);
8569 specbind (Qinhibit_modification_hooks, Qt);
8570
8571 if (clear_buffer_p && Z > BEG)
8572 del_range (BEG, Z);
8573
8574 xassert (BEGV >= BEG);
8575 xassert (ZV <= Z && ZV >= BEGV);
8576
8577 rc = fn (a1, a2, a3, a4);
8578
8579 xassert (BEGV >= BEG);
8580 xassert (ZV <= Z && ZV >= BEGV);
8581
8582 unbind_to (count, Qnil);
8583 return rc;
8584 }
8585
8586
8587 /* Save state that should be preserved around the call to the function
8588 FN called in with_echo_area_buffer. */
8589
8590 static Lisp_Object
8591 with_echo_area_buffer_unwind_data (struct window *w)
8592 {
8593 int i = 0;
8594 Lisp_Object vector, tmp;
8595
8596 /* Reduce consing by keeping one vector in
8597 Vwith_echo_area_save_vector. */
8598 vector = Vwith_echo_area_save_vector;
8599 Vwith_echo_area_save_vector = Qnil;
8600
8601 if (NILP (vector))
8602 vector = Fmake_vector (make_number (7), Qnil);
8603
8604 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8605 ASET (vector, i, Vdeactivate_mark); ++i;
8606 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8607
8608 if (w)
8609 {
8610 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8611 ASET (vector, i, w->buffer); ++i;
8612 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8613 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8614 }
8615 else
8616 {
8617 int end = i + 4;
8618 for (; i < end; ++i)
8619 ASET (vector, i, Qnil);
8620 }
8621
8622 xassert (i == ASIZE (vector));
8623 return vector;
8624 }
8625
8626
8627 /* Restore global state from VECTOR which was created by
8628 with_echo_area_buffer_unwind_data. */
8629
8630 static Lisp_Object
8631 unwind_with_echo_area_buffer (Lisp_Object vector)
8632 {
8633 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8634 Vdeactivate_mark = AREF (vector, 1);
8635 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8636
8637 if (WINDOWP (AREF (vector, 3)))
8638 {
8639 struct window *w;
8640 Lisp_Object buffer, charpos, bytepos;
8641
8642 w = XWINDOW (AREF (vector, 3));
8643 buffer = AREF (vector, 4);
8644 charpos = AREF (vector, 5);
8645 bytepos = AREF (vector, 6);
8646
8647 w->buffer = buffer;
8648 set_marker_both (w->pointm, buffer,
8649 XFASTINT (charpos), XFASTINT (bytepos));
8650 }
8651
8652 Vwith_echo_area_save_vector = vector;
8653 return Qnil;
8654 }
8655
8656
8657 /* Set up the echo area for use by print functions. MULTIBYTE_P
8658 non-zero means we will print multibyte. */
8659
8660 void
8661 setup_echo_area_for_printing (int multibyte_p)
8662 {
8663 /* If we can't find an echo area any more, exit. */
8664 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8665 Fkill_emacs (Qnil);
8666
8667 ensure_echo_area_buffers ();
8668
8669 if (!message_buf_print)
8670 {
8671 /* A message has been output since the last time we printed.
8672 Choose a fresh echo area buffer. */
8673 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8674 echo_area_buffer[0] = echo_buffer[1];
8675 else
8676 echo_area_buffer[0] = echo_buffer[0];
8677
8678 /* Switch to that buffer and clear it. */
8679 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8680 BVAR (current_buffer, truncate_lines) = Qnil;
8681
8682 if (Z > BEG)
8683 {
8684 int count = SPECPDL_INDEX ();
8685 specbind (Qinhibit_read_only, Qt);
8686 /* Note that undo recording is always disabled. */
8687 del_range (BEG, Z);
8688 unbind_to (count, Qnil);
8689 }
8690 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8691
8692 /* Set up the buffer for the multibyteness we need. */
8693 if (multibyte_p
8694 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
8695 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8696
8697 /* Raise the frame containing the echo area. */
8698 if (minibuffer_auto_raise)
8699 {
8700 struct frame *sf = SELECTED_FRAME ();
8701 Lisp_Object mini_window;
8702 mini_window = FRAME_MINIBUF_WINDOW (sf);
8703 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8704 }
8705
8706 message_log_maybe_newline ();
8707 message_buf_print = 1;
8708 }
8709 else
8710 {
8711 if (NILP (echo_area_buffer[0]))
8712 {
8713 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8714 echo_area_buffer[0] = echo_buffer[1];
8715 else
8716 echo_area_buffer[0] = echo_buffer[0];
8717 }
8718
8719 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8720 {
8721 /* Someone switched buffers between print requests. */
8722 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8723 BVAR (current_buffer, truncate_lines) = Qnil;
8724 }
8725 }
8726 }
8727
8728
8729 /* Display an echo area message in window W. Value is non-zero if W's
8730 height is changed. If display_last_displayed_message_p is
8731 non-zero, display the message that was last displayed, otherwise
8732 display the current message. */
8733
8734 static int
8735 display_echo_area (struct window *w)
8736 {
8737 int i, no_message_p, window_height_changed_p, count;
8738
8739 /* Temporarily disable garbage collections while displaying the echo
8740 area. This is done because a GC can print a message itself.
8741 That message would modify the echo area buffer's contents while a
8742 redisplay of the buffer is going on, and seriously confuse
8743 redisplay. */
8744 count = inhibit_garbage_collection ();
8745
8746 /* If there is no message, we must call display_echo_area_1
8747 nevertheless because it resizes the window. But we will have to
8748 reset the echo_area_buffer in question to nil at the end because
8749 with_echo_area_buffer will sets it to an empty buffer. */
8750 i = display_last_displayed_message_p ? 1 : 0;
8751 no_message_p = NILP (echo_area_buffer[i]);
8752
8753 window_height_changed_p
8754 = with_echo_area_buffer (w, display_last_displayed_message_p,
8755 display_echo_area_1,
8756 (EMACS_INT) w, Qnil, 0, 0);
8757
8758 if (no_message_p)
8759 echo_area_buffer[i] = Qnil;
8760
8761 unbind_to (count, Qnil);
8762 return window_height_changed_p;
8763 }
8764
8765
8766 /* Helper for display_echo_area. Display the current buffer which
8767 contains the current echo area message in window W, a mini-window,
8768 a pointer to which is passed in A1. A2..A4 are currently not used.
8769 Change the height of W so that all of the message is displayed.
8770 Value is non-zero if height of W was changed. */
8771
8772 static int
8773 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8774 {
8775 struct window *w = (struct window *) a1;
8776 Lisp_Object window;
8777 struct text_pos start;
8778 int window_height_changed_p = 0;
8779
8780 /* Do this before displaying, so that we have a large enough glyph
8781 matrix for the display. If we can't get enough space for the
8782 whole text, display the last N lines. That works by setting w->start. */
8783 window_height_changed_p = resize_mini_window (w, 0);
8784
8785 /* Use the starting position chosen by resize_mini_window. */
8786 SET_TEXT_POS_FROM_MARKER (start, w->start);
8787
8788 /* Display. */
8789 clear_glyph_matrix (w->desired_matrix);
8790 XSETWINDOW (window, w);
8791 try_window (window, start, 0);
8792
8793 return window_height_changed_p;
8794 }
8795
8796
8797 /* Resize the echo area window to exactly the size needed for the
8798 currently displayed message, if there is one. If a mini-buffer
8799 is active, don't shrink it. */
8800
8801 void
8802 resize_echo_area_exactly (void)
8803 {
8804 if (BUFFERP (echo_area_buffer[0])
8805 && WINDOWP (echo_area_window))
8806 {
8807 struct window *w = XWINDOW (echo_area_window);
8808 int resized_p;
8809 Lisp_Object resize_exactly;
8810
8811 if (minibuf_level == 0)
8812 resize_exactly = Qt;
8813 else
8814 resize_exactly = Qnil;
8815
8816 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8817 (EMACS_INT) w, resize_exactly, 0, 0);
8818 if (resized_p)
8819 {
8820 ++windows_or_buffers_changed;
8821 ++update_mode_lines;
8822 redisplay_internal ();
8823 }
8824 }
8825 }
8826
8827
8828 /* Callback function for with_echo_area_buffer, when used from
8829 resize_echo_area_exactly. A1 contains a pointer to the window to
8830 resize, EXACTLY non-nil means resize the mini-window exactly to the
8831 size of the text displayed. A3 and A4 are not used. Value is what
8832 resize_mini_window returns. */
8833
8834 static int
8835 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
8836 {
8837 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8838 }
8839
8840
8841 /* Resize mini-window W to fit the size of its contents. EXACT_P
8842 means size the window exactly to the size needed. Otherwise, it's
8843 only enlarged until W's buffer is empty.
8844
8845 Set W->start to the right place to begin display. If the whole
8846 contents fit, start at the beginning. Otherwise, start so as
8847 to make the end of the contents appear. This is particularly
8848 important for y-or-n-p, but seems desirable generally.
8849
8850 Value is non-zero if the window height has been changed. */
8851
8852 int
8853 resize_mini_window (struct window *w, int exact_p)
8854 {
8855 struct frame *f = XFRAME (w->frame);
8856 int window_height_changed_p = 0;
8857
8858 xassert (MINI_WINDOW_P (w));
8859
8860 /* By default, start display at the beginning. */
8861 set_marker_both (w->start, w->buffer,
8862 BUF_BEGV (XBUFFER (w->buffer)),
8863 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8864
8865 /* Don't resize windows while redisplaying a window; it would
8866 confuse redisplay functions when the size of the window they are
8867 displaying changes from under them. Such a resizing can happen,
8868 for instance, when which-func prints a long message while
8869 we are running fontification-functions. We're running these
8870 functions with safe_call which binds inhibit-redisplay to t. */
8871 if (!NILP (Vinhibit_redisplay))
8872 return 0;
8873
8874 /* Nil means don't try to resize. */
8875 if (NILP (Vresize_mini_windows)
8876 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8877 return 0;
8878
8879 if (!FRAME_MINIBUF_ONLY_P (f))
8880 {
8881 struct it it;
8882 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8883 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8884 int height, max_height;
8885 int unit = FRAME_LINE_HEIGHT (f);
8886 struct text_pos start;
8887 struct buffer *old_current_buffer = NULL;
8888
8889 if (current_buffer != XBUFFER (w->buffer))
8890 {
8891 old_current_buffer = current_buffer;
8892 set_buffer_internal (XBUFFER (w->buffer));
8893 }
8894
8895 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8896
8897 /* Compute the max. number of lines specified by the user. */
8898 if (FLOATP (Vmax_mini_window_height))
8899 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8900 else if (INTEGERP (Vmax_mini_window_height))
8901 max_height = XINT (Vmax_mini_window_height);
8902 else
8903 max_height = total_height / 4;
8904
8905 /* Correct that max. height if it's bogus. */
8906 max_height = max (1, max_height);
8907 max_height = min (total_height, max_height);
8908
8909 /* Find out the height of the text in the window. */
8910 if (it.line_wrap == TRUNCATE)
8911 height = 1;
8912 else
8913 {
8914 last_height = 0;
8915 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8916 if (it.max_ascent == 0 && it.max_descent == 0)
8917 height = it.current_y + last_height;
8918 else
8919 height = it.current_y + it.max_ascent + it.max_descent;
8920 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8921 height = (height + unit - 1) / unit;
8922 }
8923
8924 /* Compute a suitable window start. */
8925 if (height > max_height)
8926 {
8927 height = max_height;
8928 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8929 move_it_vertically_backward (&it, (height - 1) * unit);
8930 start = it.current.pos;
8931 }
8932 else
8933 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8934 SET_MARKER_FROM_TEXT_POS (w->start, start);
8935
8936 if (EQ (Vresize_mini_windows, Qgrow_only))
8937 {
8938 /* Let it grow only, until we display an empty message, in which
8939 case the window shrinks again. */
8940 if (height > WINDOW_TOTAL_LINES (w))
8941 {
8942 int old_height = WINDOW_TOTAL_LINES (w);
8943 freeze_window_starts (f, 1);
8944 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8945 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8946 }
8947 else if (height < WINDOW_TOTAL_LINES (w)
8948 && (exact_p || BEGV == ZV))
8949 {
8950 int old_height = WINDOW_TOTAL_LINES (w);
8951 freeze_window_starts (f, 0);
8952 shrink_mini_window (w);
8953 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8954 }
8955 }
8956 else
8957 {
8958 /* Always resize to exact size needed. */
8959 if (height > WINDOW_TOTAL_LINES (w))
8960 {
8961 int old_height = WINDOW_TOTAL_LINES (w);
8962 freeze_window_starts (f, 1);
8963 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8964 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8965 }
8966 else if (height < WINDOW_TOTAL_LINES (w))
8967 {
8968 int old_height = WINDOW_TOTAL_LINES (w);
8969 freeze_window_starts (f, 0);
8970 shrink_mini_window (w);
8971
8972 if (height)
8973 {
8974 freeze_window_starts (f, 1);
8975 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8976 }
8977
8978 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8979 }
8980 }
8981
8982 if (old_current_buffer)
8983 set_buffer_internal (old_current_buffer);
8984 }
8985
8986 return window_height_changed_p;
8987 }
8988
8989
8990 /* Value is the current message, a string, or nil if there is no
8991 current message. */
8992
8993 Lisp_Object
8994 current_message (void)
8995 {
8996 Lisp_Object msg;
8997
8998 if (!BUFFERP (echo_area_buffer[0]))
8999 msg = Qnil;
9000 else
9001 {
9002 with_echo_area_buffer (0, 0, current_message_1,
9003 (EMACS_INT) &msg, Qnil, 0, 0);
9004 if (NILP (msg))
9005 echo_area_buffer[0] = Qnil;
9006 }
9007
9008 return msg;
9009 }
9010
9011
9012 static int
9013 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9014 {
9015 Lisp_Object *msg = (Lisp_Object *) a1;
9016
9017 if (Z > BEG)
9018 *msg = make_buffer_string (BEG, Z, 1);
9019 else
9020 *msg = Qnil;
9021 return 0;
9022 }
9023
9024
9025 /* Push the current message on Vmessage_stack for later restauration
9026 by restore_message. Value is non-zero if the current message isn't
9027 empty. This is a relatively infrequent operation, so it's not
9028 worth optimizing. */
9029
9030 int
9031 push_message (void)
9032 {
9033 Lisp_Object msg;
9034 msg = current_message ();
9035 Vmessage_stack = Fcons (msg, Vmessage_stack);
9036 return STRINGP (msg);
9037 }
9038
9039
9040 /* Restore message display from the top of Vmessage_stack. */
9041
9042 void
9043 restore_message (void)
9044 {
9045 Lisp_Object msg;
9046
9047 xassert (CONSP (Vmessage_stack));
9048 msg = XCAR (Vmessage_stack);
9049 if (STRINGP (msg))
9050 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9051 else
9052 message3_nolog (msg, 0, 0);
9053 }
9054
9055
9056 /* Handler for record_unwind_protect calling pop_message. */
9057
9058 Lisp_Object
9059 pop_message_unwind (Lisp_Object dummy)
9060 {
9061 pop_message ();
9062 return Qnil;
9063 }
9064
9065 /* Pop the top-most entry off Vmessage_stack. */
9066
9067 void
9068 pop_message (void)
9069 {
9070 xassert (CONSP (Vmessage_stack));
9071 Vmessage_stack = XCDR (Vmessage_stack);
9072 }
9073
9074
9075 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9076 exits. If the stack is not empty, we have a missing pop_message
9077 somewhere. */
9078
9079 void
9080 check_message_stack (void)
9081 {
9082 if (!NILP (Vmessage_stack))
9083 abort ();
9084 }
9085
9086
9087 /* Truncate to NCHARS what will be displayed in the echo area the next
9088 time we display it---but don't redisplay it now. */
9089
9090 void
9091 truncate_echo_area (EMACS_INT nchars)
9092 {
9093 if (nchars == 0)
9094 echo_area_buffer[0] = Qnil;
9095 /* A null message buffer means that the frame hasn't really been
9096 initialized yet. Error messages get reported properly by
9097 cmd_error, so this must be just an informative message; toss it. */
9098 else if (!noninteractive
9099 && INTERACTIVE
9100 && !NILP (echo_area_buffer[0]))
9101 {
9102 struct frame *sf = SELECTED_FRAME ();
9103 if (FRAME_MESSAGE_BUF (sf))
9104 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9105 }
9106 }
9107
9108
9109 /* Helper function for truncate_echo_area. Truncate the current
9110 message to at most NCHARS characters. */
9111
9112 static int
9113 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9114 {
9115 if (BEG + nchars < Z)
9116 del_range (BEG + nchars, Z);
9117 if (Z == BEG)
9118 echo_area_buffer[0] = Qnil;
9119 return 0;
9120 }
9121
9122
9123 /* Set the current message to a substring of S or STRING.
9124
9125 If STRING is a Lisp string, set the message to the first NBYTES
9126 bytes from STRING. NBYTES zero means use the whole string. If
9127 STRING is multibyte, the message will be displayed multibyte.
9128
9129 If S is not null, set the message to the first LEN bytes of S. LEN
9130 zero means use the whole string. MULTIBYTE_P non-zero means S is
9131 multibyte. Display the message multibyte in that case.
9132
9133 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9134 to t before calling set_message_1 (which calls insert).
9135 */
9136
9137 void
9138 set_message (const char *s, Lisp_Object string,
9139 EMACS_INT nbytes, int multibyte_p)
9140 {
9141 message_enable_multibyte
9142 = ((s && multibyte_p)
9143 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9144
9145 with_echo_area_buffer (0, -1, set_message_1,
9146 (EMACS_INT) s, string, nbytes, multibyte_p);
9147 message_buf_print = 0;
9148 help_echo_showing_p = 0;
9149 }
9150
9151
9152 /* Helper function for set_message. Arguments have the same meaning
9153 as there, with A1 corresponding to S and A2 corresponding to STRING
9154 This function is called with the echo area buffer being
9155 current. */
9156
9157 static int
9158 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9159 {
9160 const char *s = (const char *) a1;
9161 const unsigned char *msg = (const unsigned char *) s;
9162 Lisp_Object string = a2;
9163
9164 /* Change multibyteness of the echo buffer appropriately. */
9165 if (message_enable_multibyte
9166 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9167 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9168
9169 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9170 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9171 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9172
9173 /* Insert new message at BEG. */
9174 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9175
9176 if (STRINGP (string))
9177 {
9178 EMACS_INT nchars;
9179
9180 if (nbytes == 0)
9181 nbytes = SBYTES (string);
9182 nchars = string_byte_to_char (string, nbytes);
9183
9184 /* This function takes care of single/multibyte conversion. We
9185 just have to ensure that the echo area buffer has the right
9186 setting of enable_multibyte_characters. */
9187 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9188 }
9189 else if (s)
9190 {
9191 if (nbytes == 0)
9192 nbytes = strlen (s);
9193
9194 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9195 {
9196 /* Convert from multi-byte to single-byte. */
9197 EMACS_INT i;
9198 int c, n;
9199 char work[1];
9200
9201 /* Convert a multibyte string to single-byte. */
9202 for (i = 0; i < nbytes; i += n)
9203 {
9204 c = string_char_and_length (msg + i, &n);
9205 work[0] = (ASCII_CHAR_P (c)
9206 ? c
9207 : multibyte_char_to_unibyte (c));
9208 insert_1_both (work, 1, 1, 1, 0, 0);
9209 }
9210 }
9211 else if (!multibyte_p
9212 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9213 {
9214 /* Convert from single-byte to multi-byte. */
9215 EMACS_INT i;
9216 int c, n;
9217 unsigned char str[MAX_MULTIBYTE_LENGTH];
9218
9219 /* Convert a single-byte string to multibyte. */
9220 for (i = 0; i < nbytes; i++)
9221 {
9222 c = msg[i];
9223 MAKE_CHAR_MULTIBYTE (c);
9224 n = CHAR_STRING (c, str);
9225 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9226 }
9227 }
9228 else
9229 insert_1 (s, nbytes, 1, 0, 0);
9230 }
9231
9232 return 0;
9233 }
9234
9235
9236 /* Clear messages. CURRENT_P non-zero means clear the current
9237 message. LAST_DISPLAYED_P non-zero means clear the message
9238 last displayed. */
9239
9240 void
9241 clear_message (int current_p, int last_displayed_p)
9242 {
9243 if (current_p)
9244 {
9245 echo_area_buffer[0] = Qnil;
9246 message_cleared_p = 1;
9247 }
9248
9249 if (last_displayed_p)
9250 echo_area_buffer[1] = Qnil;
9251
9252 message_buf_print = 0;
9253 }
9254
9255 /* Clear garbaged frames.
9256
9257 This function is used where the old redisplay called
9258 redraw_garbaged_frames which in turn called redraw_frame which in
9259 turn called clear_frame. The call to clear_frame was a source of
9260 flickering. I believe a clear_frame is not necessary. It should
9261 suffice in the new redisplay to invalidate all current matrices,
9262 and ensure a complete redisplay of all windows. */
9263
9264 static void
9265 clear_garbaged_frames (void)
9266 {
9267 if (frame_garbaged)
9268 {
9269 Lisp_Object tail, frame;
9270 int changed_count = 0;
9271
9272 FOR_EACH_FRAME (tail, frame)
9273 {
9274 struct frame *f = XFRAME (frame);
9275
9276 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9277 {
9278 if (f->resized_p)
9279 {
9280 Fredraw_frame (frame);
9281 f->force_flush_display_p = 1;
9282 }
9283 clear_current_matrices (f);
9284 changed_count++;
9285 f->garbaged = 0;
9286 f->resized_p = 0;
9287 }
9288 }
9289
9290 frame_garbaged = 0;
9291 if (changed_count)
9292 ++windows_or_buffers_changed;
9293 }
9294 }
9295
9296
9297 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9298 is non-zero update selected_frame. Value is non-zero if the
9299 mini-windows height has been changed. */
9300
9301 static int
9302 echo_area_display (int update_frame_p)
9303 {
9304 Lisp_Object mini_window;
9305 struct window *w;
9306 struct frame *f;
9307 int window_height_changed_p = 0;
9308 struct frame *sf = SELECTED_FRAME ();
9309
9310 mini_window = FRAME_MINIBUF_WINDOW (sf);
9311 w = XWINDOW (mini_window);
9312 f = XFRAME (WINDOW_FRAME (w));
9313
9314 /* Don't display if frame is invisible or not yet initialized. */
9315 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9316 return 0;
9317
9318 #ifdef HAVE_WINDOW_SYSTEM
9319 /* When Emacs starts, selected_frame may be the initial terminal
9320 frame. If we let this through, a message would be displayed on
9321 the terminal. */
9322 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9323 return 0;
9324 #endif /* HAVE_WINDOW_SYSTEM */
9325
9326 /* Redraw garbaged frames. */
9327 if (frame_garbaged)
9328 clear_garbaged_frames ();
9329
9330 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9331 {
9332 echo_area_window = mini_window;
9333 window_height_changed_p = display_echo_area (w);
9334 w->must_be_updated_p = 1;
9335
9336 /* Update the display, unless called from redisplay_internal.
9337 Also don't update the screen during redisplay itself. The
9338 update will happen at the end of redisplay, and an update
9339 here could cause confusion. */
9340 if (update_frame_p && !redisplaying_p)
9341 {
9342 int n = 0;
9343
9344 /* If the display update has been interrupted by pending
9345 input, update mode lines in the frame. Due to the
9346 pending input, it might have been that redisplay hasn't
9347 been called, so that mode lines above the echo area are
9348 garbaged. This looks odd, so we prevent it here. */
9349 if (!display_completed)
9350 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9351
9352 if (window_height_changed_p
9353 /* Don't do this if Emacs is shutting down. Redisplay
9354 needs to run hooks. */
9355 && !NILP (Vrun_hooks))
9356 {
9357 /* Must update other windows. Likewise as in other
9358 cases, don't let this update be interrupted by
9359 pending input. */
9360 int count = SPECPDL_INDEX ();
9361 specbind (Qredisplay_dont_pause, Qt);
9362 windows_or_buffers_changed = 1;
9363 redisplay_internal ();
9364 unbind_to (count, Qnil);
9365 }
9366 else if (FRAME_WINDOW_P (f) && n == 0)
9367 {
9368 /* Window configuration is the same as before.
9369 Can do with a display update of the echo area,
9370 unless we displayed some mode lines. */
9371 update_single_window (w, 1);
9372 FRAME_RIF (f)->flush_display (f);
9373 }
9374 else
9375 update_frame (f, 1, 1);
9376
9377 /* If cursor is in the echo area, make sure that the next
9378 redisplay displays the minibuffer, so that the cursor will
9379 be replaced with what the minibuffer wants. */
9380 if (cursor_in_echo_area)
9381 ++windows_or_buffers_changed;
9382 }
9383 }
9384 else if (!EQ (mini_window, selected_window))
9385 windows_or_buffers_changed++;
9386
9387 /* Last displayed message is now the current message. */
9388 echo_area_buffer[1] = echo_area_buffer[0];
9389 /* Inform read_char that we're not echoing. */
9390 echo_message_buffer = Qnil;
9391
9392 /* Prevent redisplay optimization in redisplay_internal by resetting
9393 this_line_start_pos. This is done because the mini-buffer now
9394 displays the message instead of its buffer text. */
9395 if (EQ (mini_window, selected_window))
9396 CHARPOS (this_line_start_pos) = 0;
9397
9398 return window_height_changed_p;
9399 }
9400
9401
9402 \f
9403 /***********************************************************************
9404 Mode Lines and Frame Titles
9405 ***********************************************************************/
9406
9407 /* A buffer for constructing non-propertized mode-line strings and
9408 frame titles in it; allocated from the heap in init_xdisp and
9409 resized as needed in store_mode_line_noprop_char. */
9410
9411 static char *mode_line_noprop_buf;
9412
9413 /* The buffer's end, and a current output position in it. */
9414
9415 static char *mode_line_noprop_buf_end;
9416 static char *mode_line_noprop_ptr;
9417
9418 #define MODE_LINE_NOPROP_LEN(start) \
9419 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9420
9421 static enum {
9422 MODE_LINE_DISPLAY = 0,
9423 MODE_LINE_TITLE,
9424 MODE_LINE_NOPROP,
9425 MODE_LINE_STRING
9426 } mode_line_target;
9427
9428 /* Alist that caches the results of :propertize.
9429 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9430 static Lisp_Object mode_line_proptrans_alist;
9431
9432 /* List of strings making up the mode-line. */
9433 static Lisp_Object mode_line_string_list;
9434
9435 /* Base face property when building propertized mode line string. */
9436 static Lisp_Object mode_line_string_face;
9437 static Lisp_Object mode_line_string_face_prop;
9438
9439
9440 /* Unwind data for mode line strings */
9441
9442 static Lisp_Object Vmode_line_unwind_vector;
9443
9444 static Lisp_Object
9445 format_mode_line_unwind_data (struct buffer *obuf,
9446 Lisp_Object owin,
9447 int save_proptrans)
9448 {
9449 Lisp_Object vector, tmp;
9450
9451 /* Reduce consing by keeping one vector in
9452 Vwith_echo_area_save_vector. */
9453 vector = Vmode_line_unwind_vector;
9454 Vmode_line_unwind_vector = Qnil;
9455
9456 if (NILP (vector))
9457 vector = Fmake_vector (make_number (8), Qnil);
9458
9459 ASET (vector, 0, make_number (mode_line_target));
9460 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9461 ASET (vector, 2, mode_line_string_list);
9462 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9463 ASET (vector, 4, mode_line_string_face);
9464 ASET (vector, 5, mode_line_string_face_prop);
9465
9466 if (obuf)
9467 XSETBUFFER (tmp, obuf);
9468 else
9469 tmp = Qnil;
9470 ASET (vector, 6, tmp);
9471 ASET (vector, 7, owin);
9472
9473 return vector;
9474 }
9475
9476 static Lisp_Object
9477 unwind_format_mode_line (Lisp_Object vector)
9478 {
9479 mode_line_target = XINT (AREF (vector, 0));
9480 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9481 mode_line_string_list = AREF (vector, 2);
9482 if (! EQ (AREF (vector, 3), Qt))
9483 mode_line_proptrans_alist = AREF (vector, 3);
9484 mode_line_string_face = AREF (vector, 4);
9485 mode_line_string_face_prop = AREF (vector, 5);
9486
9487 if (!NILP (AREF (vector, 7)))
9488 /* Select window before buffer, since it may change the buffer. */
9489 Fselect_window (AREF (vector, 7), Qt);
9490
9491 if (!NILP (AREF (vector, 6)))
9492 {
9493 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9494 ASET (vector, 6, Qnil);
9495 }
9496
9497 Vmode_line_unwind_vector = vector;
9498 return Qnil;
9499 }
9500
9501
9502 /* Store a single character C for the frame title in mode_line_noprop_buf.
9503 Re-allocate mode_line_noprop_buf if necessary. */
9504
9505 static void
9506 store_mode_line_noprop_char (char c)
9507 {
9508 /* If output position has reached the end of the allocated buffer,
9509 double the buffer's size. */
9510 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9511 {
9512 int len = MODE_LINE_NOPROP_LEN (0);
9513 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9514 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9515 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9516 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9517 }
9518
9519 *mode_line_noprop_ptr++ = c;
9520 }
9521
9522
9523 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9524 mode_line_noprop_ptr. STRING is the string to store. Do not copy
9525 characters that yield more columns than PRECISION; PRECISION <= 0
9526 means copy the whole string. Pad with spaces until FIELD_WIDTH
9527 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9528 pad. Called from display_mode_element when it is used to build a
9529 frame title. */
9530
9531 static int
9532 store_mode_line_noprop (const char *string, int field_width, int precision)
9533 {
9534 const unsigned char *str = (const unsigned char *) string;
9535 int n = 0;
9536 EMACS_INT dummy, nbytes;
9537
9538 /* Copy at most PRECISION chars from STR. */
9539 nbytes = strlen (string);
9540 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9541 while (nbytes--)
9542 store_mode_line_noprop_char (*str++);
9543
9544 /* Fill up with spaces until FIELD_WIDTH reached. */
9545 while (field_width > 0
9546 && n < field_width)
9547 {
9548 store_mode_line_noprop_char (' ');
9549 ++n;
9550 }
9551
9552 return n;
9553 }
9554
9555 /***********************************************************************
9556 Frame Titles
9557 ***********************************************************************/
9558
9559 #ifdef HAVE_WINDOW_SYSTEM
9560
9561 /* Set the title of FRAME, if it has changed. The title format is
9562 Vicon_title_format if FRAME is iconified, otherwise it is
9563 frame_title_format. */
9564
9565 static void
9566 x_consider_frame_title (Lisp_Object frame)
9567 {
9568 struct frame *f = XFRAME (frame);
9569
9570 if (FRAME_WINDOW_P (f)
9571 || FRAME_MINIBUF_ONLY_P (f)
9572 || f->explicit_name)
9573 {
9574 /* Do we have more than one visible frame on this X display? */
9575 Lisp_Object tail;
9576 Lisp_Object fmt;
9577 int title_start;
9578 char *title;
9579 int len;
9580 struct it it;
9581 int count = SPECPDL_INDEX ();
9582
9583 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9584 {
9585 Lisp_Object other_frame = XCAR (tail);
9586 struct frame *tf = XFRAME (other_frame);
9587
9588 if (tf != f
9589 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9590 && !FRAME_MINIBUF_ONLY_P (tf)
9591 && !EQ (other_frame, tip_frame)
9592 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9593 break;
9594 }
9595
9596 /* Set global variable indicating that multiple frames exist. */
9597 multiple_frames = CONSP (tail);
9598
9599 /* Switch to the buffer of selected window of the frame. Set up
9600 mode_line_target so that display_mode_element will output into
9601 mode_line_noprop_buf; then display the title. */
9602 record_unwind_protect (unwind_format_mode_line,
9603 format_mode_line_unwind_data
9604 (current_buffer, selected_window, 0));
9605
9606 Fselect_window (f->selected_window, Qt);
9607 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9608 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9609
9610 mode_line_target = MODE_LINE_TITLE;
9611 title_start = MODE_LINE_NOPROP_LEN (0);
9612 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9613 NULL, DEFAULT_FACE_ID);
9614 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9615 len = MODE_LINE_NOPROP_LEN (title_start);
9616 title = mode_line_noprop_buf + title_start;
9617 unbind_to (count, Qnil);
9618
9619 /* Set the title only if it's changed. This avoids consing in
9620 the common case where it hasn't. (If it turns out that we've
9621 already wasted too much time by walking through the list with
9622 display_mode_element, then we might need to optimize at a
9623 higher level than this.) */
9624 if (! STRINGP (f->name)
9625 || SBYTES (f->name) != len
9626 || memcmp (title, SDATA (f->name), len) != 0)
9627 x_implicitly_set_name (f, make_string (title, len), Qnil);
9628 }
9629 }
9630
9631 #endif /* not HAVE_WINDOW_SYSTEM */
9632
9633
9634
9635 \f
9636 /***********************************************************************
9637 Menu Bars
9638 ***********************************************************************/
9639
9640
9641 /* Prepare for redisplay by updating menu-bar item lists when
9642 appropriate. This can call eval. */
9643
9644 void
9645 prepare_menu_bars (void)
9646 {
9647 int all_windows;
9648 struct gcpro gcpro1, gcpro2;
9649 struct frame *f;
9650 Lisp_Object tooltip_frame;
9651
9652 #ifdef HAVE_WINDOW_SYSTEM
9653 tooltip_frame = tip_frame;
9654 #else
9655 tooltip_frame = Qnil;
9656 #endif
9657
9658 /* Update all frame titles based on their buffer names, etc. We do
9659 this before the menu bars so that the buffer-menu will show the
9660 up-to-date frame titles. */
9661 #ifdef HAVE_WINDOW_SYSTEM
9662 if (windows_or_buffers_changed || update_mode_lines)
9663 {
9664 Lisp_Object tail, frame;
9665
9666 FOR_EACH_FRAME (tail, frame)
9667 {
9668 f = XFRAME (frame);
9669 if (!EQ (frame, tooltip_frame)
9670 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9671 x_consider_frame_title (frame);
9672 }
9673 }
9674 #endif /* HAVE_WINDOW_SYSTEM */
9675
9676 /* Update the menu bar item lists, if appropriate. This has to be
9677 done before any actual redisplay or generation of display lines. */
9678 all_windows = (update_mode_lines
9679 || buffer_shared > 1
9680 || windows_or_buffers_changed);
9681 if (all_windows)
9682 {
9683 Lisp_Object tail, frame;
9684 int count = SPECPDL_INDEX ();
9685 /* 1 means that update_menu_bar has run its hooks
9686 so any further calls to update_menu_bar shouldn't do so again. */
9687 int menu_bar_hooks_run = 0;
9688
9689 record_unwind_save_match_data ();
9690
9691 FOR_EACH_FRAME (tail, frame)
9692 {
9693 f = XFRAME (frame);
9694
9695 /* Ignore tooltip frame. */
9696 if (EQ (frame, tooltip_frame))
9697 continue;
9698
9699 /* If a window on this frame changed size, report that to
9700 the user and clear the size-change flag. */
9701 if (FRAME_WINDOW_SIZES_CHANGED (f))
9702 {
9703 Lisp_Object functions;
9704
9705 /* Clear flag first in case we get an error below. */
9706 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9707 functions = Vwindow_size_change_functions;
9708 GCPRO2 (tail, functions);
9709
9710 while (CONSP (functions))
9711 {
9712 if (!EQ (XCAR (functions), Qt))
9713 call1 (XCAR (functions), frame);
9714 functions = XCDR (functions);
9715 }
9716 UNGCPRO;
9717 }
9718
9719 GCPRO1 (tail);
9720 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9721 #ifdef HAVE_WINDOW_SYSTEM
9722 update_tool_bar (f, 0);
9723 #endif
9724 #ifdef HAVE_NS
9725 if (windows_or_buffers_changed
9726 && FRAME_NS_P (f))
9727 ns_set_doc_edited (f, Fbuffer_modified_p
9728 (XWINDOW (f->selected_window)->buffer));
9729 #endif
9730 UNGCPRO;
9731 }
9732
9733 unbind_to (count, Qnil);
9734 }
9735 else
9736 {
9737 struct frame *sf = SELECTED_FRAME ();
9738 update_menu_bar (sf, 1, 0);
9739 #ifdef HAVE_WINDOW_SYSTEM
9740 update_tool_bar (sf, 1);
9741 #endif
9742 }
9743 }
9744
9745
9746 /* Update the menu bar item list for frame F. This has to be done
9747 before we start to fill in any display lines, because it can call
9748 eval.
9749
9750 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9751
9752 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9753 already ran the menu bar hooks for this redisplay, so there
9754 is no need to run them again. The return value is the
9755 updated value of this flag, to pass to the next call. */
9756
9757 static int
9758 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9759 {
9760 Lisp_Object window;
9761 register struct window *w;
9762
9763 /* If called recursively during a menu update, do nothing. This can
9764 happen when, for instance, an activate-menubar-hook causes a
9765 redisplay. */
9766 if (inhibit_menubar_update)
9767 return hooks_run;
9768
9769 window = FRAME_SELECTED_WINDOW (f);
9770 w = XWINDOW (window);
9771
9772 if (FRAME_WINDOW_P (f)
9773 ?
9774 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9775 || defined (HAVE_NS) || defined (USE_GTK)
9776 FRAME_EXTERNAL_MENU_BAR (f)
9777 #else
9778 FRAME_MENU_BAR_LINES (f) > 0
9779 #endif
9780 : FRAME_MENU_BAR_LINES (f) > 0)
9781 {
9782 /* If the user has switched buffers or windows, we need to
9783 recompute to reflect the new bindings. But we'll
9784 recompute when update_mode_lines is set too; that means
9785 that people can use force-mode-line-update to request
9786 that the menu bar be recomputed. The adverse effect on
9787 the rest of the redisplay algorithm is about the same as
9788 windows_or_buffers_changed anyway. */
9789 if (windows_or_buffers_changed
9790 /* This used to test w->update_mode_line, but we believe
9791 there is no need to recompute the menu in that case. */
9792 || update_mode_lines
9793 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9794 < BUF_MODIFF (XBUFFER (w->buffer)))
9795 != !NILP (w->last_had_star))
9796 || ((!NILP (Vtransient_mark_mode)
9797 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
9798 != !NILP (w->region_showing)))
9799 {
9800 struct buffer *prev = current_buffer;
9801 int count = SPECPDL_INDEX ();
9802
9803 specbind (Qinhibit_menubar_update, Qt);
9804
9805 set_buffer_internal_1 (XBUFFER (w->buffer));
9806 if (save_match_data)
9807 record_unwind_save_match_data ();
9808 if (NILP (Voverriding_local_map_menu_flag))
9809 {
9810 specbind (Qoverriding_terminal_local_map, Qnil);
9811 specbind (Qoverriding_local_map, Qnil);
9812 }
9813
9814 if (!hooks_run)
9815 {
9816 /* Run the Lucid hook. */
9817 safe_run_hooks (Qactivate_menubar_hook);
9818
9819 /* If it has changed current-menubar from previous value,
9820 really recompute the menu-bar from the value. */
9821 if (! NILP (Vlucid_menu_bar_dirty_flag))
9822 call0 (Qrecompute_lucid_menubar);
9823
9824 safe_run_hooks (Qmenu_bar_update_hook);
9825
9826 hooks_run = 1;
9827 }
9828
9829 XSETFRAME (Vmenu_updating_frame, f);
9830 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9831
9832 /* Redisplay the menu bar in case we changed it. */
9833 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9834 || defined (HAVE_NS) || defined (USE_GTK)
9835 if (FRAME_WINDOW_P (f))
9836 {
9837 #if defined (HAVE_NS)
9838 /* All frames on Mac OS share the same menubar. So only
9839 the selected frame should be allowed to set it. */
9840 if (f == SELECTED_FRAME ())
9841 #endif
9842 set_frame_menubar (f, 0, 0);
9843 }
9844 else
9845 /* On a terminal screen, the menu bar is an ordinary screen
9846 line, and this makes it get updated. */
9847 w->update_mode_line = Qt;
9848 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9849 /* In the non-toolkit version, the menu bar is an ordinary screen
9850 line, and this makes it get updated. */
9851 w->update_mode_line = Qt;
9852 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9853
9854 unbind_to (count, Qnil);
9855 set_buffer_internal_1 (prev);
9856 }
9857 }
9858
9859 return hooks_run;
9860 }
9861
9862
9863 \f
9864 /***********************************************************************
9865 Output Cursor
9866 ***********************************************************************/
9867
9868 #ifdef HAVE_WINDOW_SYSTEM
9869
9870 /* EXPORT:
9871 Nominal cursor position -- where to draw output.
9872 HPOS and VPOS are window relative glyph matrix coordinates.
9873 X and Y are window relative pixel coordinates. */
9874
9875 struct cursor_pos output_cursor;
9876
9877
9878 /* EXPORT:
9879 Set the global variable output_cursor to CURSOR. All cursor
9880 positions are relative to updated_window. */
9881
9882 void
9883 set_output_cursor (struct cursor_pos *cursor)
9884 {
9885 output_cursor.hpos = cursor->hpos;
9886 output_cursor.vpos = cursor->vpos;
9887 output_cursor.x = cursor->x;
9888 output_cursor.y = cursor->y;
9889 }
9890
9891
9892 /* EXPORT for RIF:
9893 Set a nominal cursor position.
9894
9895 HPOS and VPOS are column/row positions in a window glyph matrix. X
9896 and Y are window text area relative pixel positions.
9897
9898 If this is done during an update, updated_window will contain the
9899 window that is being updated and the position is the future output
9900 cursor position for that window. If updated_window is null, use
9901 selected_window and display the cursor at the given position. */
9902
9903 void
9904 x_cursor_to (int vpos, int hpos, int y, int x)
9905 {
9906 struct window *w;
9907
9908 /* If updated_window is not set, work on selected_window. */
9909 if (updated_window)
9910 w = updated_window;
9911 else
9912 w = XWINDOW (selected_window);
9913
9914 /* Set the output cursor. */
9915 output_cursor.hpos = hpos;
9916 output_cursor.vpos = vpos;
9917 output_cursor.x = x;
9918 output_cursor.y = y;
9919
9920 /* If not called as part of an update, really display the cursor.
9921 This will also set the cursor position of W. */
9922 if (updated_window == NULL)
9923 {
9924 BLOCK_INPUT;
9925 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9926 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9927 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9928 UNBLOCK_INPUT;
9929 }
9930 }
9931
9932 #endif /* HAVE_WINDOW_SYSTEM */
9933
9934 \f
9935 /***********************************************************************
9936 Tool-bars
9937 ***********************************************************************/
9938
9939 #ifdef HAVE_WINDOW_SYSTEM
9940
9941 /* Where the mouse was last time we reported a mouse event. */
9942
9943 FRAME_PTR last_mouse_frame;
9944
9945 /* Tool-bar item index of the item on which a mouse button was pressed
9946 or -1. */
9947
9948 int last_tool_bar_item;
9949
9950
9951 static Lisp_Object
9952 update_tool_bar_unwind (Lisp_Object frame)
9953 {
9954 selected_frame = frame;
9955 return Qnil;
9956 }
9957
9958 /* Update the tool-bar item list for frame F. This has to be done
9959 before we start to fill in any display lines. Called from
9960 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9961 and restore it here. */
9962
9963 static void
9964 update_tool_bar (struct frame *f, int save_match_data)
9965 {
9966 #if defined (USE_GTK) || defined (HAVE_NS)
9967 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9968 #else
9969 int do_update = WINDOWP (f->tool_bar_window)
9970 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9971 #endif
9972
9973 if (do_update)
9974 {
9975 Lisp_Object window;
9976 struct window *w;
9977
9978 window = FRAME_SELECTED_WINDOW (f);
9979 w = XWINDOW (window);
9980
9981 /* If the user has switched buffers or windows, we need to
9982 recompute to reflect the new bindings. But we'll
9983 recompute when update_mode_lines is set too; that means
9984 that people can use force-mode-line-update to request
9985 that the menu bar be recomputed. The adverse effect on
9986 the rest of the redisplay algorithm is about the same as
9987 windows_or_buffers_changed anyway. */
9988 if (windows_or_buffers_changed
9989 || !NILP (w->update_mode_line)
9990 || update_mode_lines
9991 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9992 < BUF_MODIFF (XBUFFER (w->buffer)))
9993 != !NILP (w->last_had_star))
9994 || ((!NILP (Vtransient_mark_mode)
9995 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
9996 != !NILP (w->region_showing)))
9997 {
9998 struct buffer *prev = current_buffer;
9999 int count = SPECPDL_INDEX ();
10000 Lisp_Object frame, new_tool_bar;
10001 int new_n_tool_bar;
10002 struct gcpro gcpro1;
10003
10004 /* Set current_buffer to the buffer of the selected
10005 window of the frame, so that we get the right local
10006 keymaps. */
10007 set_buffer_internal_1 (XBUFFER (w->buffer));
10008
10009 /* Save match data, if we must. */
10010 if (save_match_data)
10011 record_unwind_save_match_data ();
10012
10013 /* Make sure that we don't accidentally use bogus keymaps. */
10014 if (NILP (Voverriding_local_map_menu_flag))
10015 {
10016 specbind (Qoverriding_terminal_local_map, Qnil);
10017 specbind (Qoverriding_local_map, Qnil);
10018 }
10019
10020 GCPRO1 (new_tool_bar);
10021
10022 /* We must temporarily set the selected frame to this frame
10023 before calling tool_bar_items, because the calculation of
10024 the tool-bar keymap uses the selected frame (see
10025 `tool-bar-make-keymap' in tool-bar.el). */
10026 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10027 XSETFRAME (frame, f);
10028 selected_frame = frame;
10029
10030 /* Build desired tool-bar items from keymaps. */
10031 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10032 &new_n_tool_bar);
10033
10034 /* Redisplay the tool-bar if we changed it. */
10035 if (new_n_tool_bar != f->n_tool_bar_items
10036 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10037 {
10038 /* Redisplay that happens asynchronously due to an expose event
10039 may access f->tool_bar_items. Make sure we update both
10040 variables within BLOCK_INPUT so no such event interrupts. */
10041 BLOCK_INPUT;
10042 f->tool_bar_items = new_tool_bar;
10043 f->n_tool_bar_items = new_n_tool_bar;
10044 w->update_mode_line = Qt;
10045 UNBLOCK_INPUT;
10046 }
10047
10048 UNGCPRO;
10049
10050 unbind_to (count, Qnil);
10051 set_buffer_internal_1 (prev);
10052 }
10053 }
10054 }
10055
10056
10057 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10058 F's desired tool-bar contents. F->tool_bar_items must have
10059 been set up previously by calling prepare_menu_bars. */
10060
10061 static void
10062 build_desired_tool_bar_string (struct frame *f)
10063 {
10064 int i, size, size_needed;
10065 struct gcpro gcpro1, gcpro2, gcpro3;
10066 Lisp_Object image, plist, props;
10067
10068 image = plist = props = Qnil;
10069 GCPRO3 (image, plist, props);
10070
10071 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10072 Otherwise, make a new string. */
10073
10074 /* The size of the string we might be able to reuse. */
10075 size = (STRINGP (f->desired_tool_bar_string)
10076 ? SCHARS (f->desired_tool_bar_string)
10077 : 0);
10078
10079 /* We need one space in the string for each image. */
10080 size_needed = f->n_tool_bar_items;
10081
10082 /* Reuse f->desired_tool_bar_string, if possible. */
10083 if (size < size_needed || NILP (f->desired_tool_bar_string))
10084 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10085 make_number (' '));
10086 else
10087 {
10088 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10089 Fremove_text_properties (make_number (0), make_number (size),
10090 props, f->desired_tool_bar_string);
10091 }
10092
10093 /* Put a `display' property on the string for the images to display,
10094 put a `menu_item' property on tool-bar items with a value that
10095 is the index of the item in F's tool-bar item vector. */
10096 for (i = 0; i < f->n_tool_bar_items; ++i)
10097 {
10098 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10099
10100 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10101 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10102 int hmargin, vmargin, relief, idx, end;
10103
10104 /* If image is a vector, choose the image according to the
10105 button state. */
10106 image = PROP (TOOL_BAR_ITEM_IMAGES);
10107 if (VECTORP (image))
10108 {
10109 if (enabled_p)
10110 idx = (selected_p
10111 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10112 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10113 else
10114 idx = (selected_p
10115 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10116 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10117
10118 xassert (ASIZE (image) >= idx);
10119 image = AREF (image, idx);
10120 }
10121 else
10122 idx = -1;
10123
10124 /* Ignore invalid image specifications. */
10125 if (!valid_image_p (image))
10126 continue;
10127
10128 /* Display the tool-bar button pressed, or depressed. */
10129 plist = Fcopy_sequence (XCDR (image));
10130
10131 /* Compute margin and relief to draw. */
10132 relief = (tool_bar_button_relief >= 0
10133 ? tool_bar_button_relief
10134 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10135 hmargin = vmargin = relief;
10136
10137 if (INTEGERP (Vtool_bar_button_margin)
10138 && XINT (Vtool_bar_button_margin) > 0)
10139 {
10140 hmargin += XFASTINT (Vtool_bar_button_margin);
10141 vmargin += XFASTINT (Vtool_bar_button_margin);
10142 }
10143 else if (CONSP (Vtool_bar_button_margin))
10144 {
10145 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10146 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10147 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10148
10149 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10150 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10151 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10152 }
10153
10154 if (auto_raise_tool_bar_buttons_p)
10155 {
10156 /* Add a `:relief' property to the image spec if the item is
10157 selected. */
10158 if (selected_p)
10159 {
10160 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10161 hmargin -= relief;
10162 vmargin -= relief;
10163 }
10164 }
10165 else
10166 {
10167 /* If image is selected, display it pressed, i.e. with a
10168 negative relief. If it's not selected, display it with a
10169 raised relief. */
10170 plist = Fplist_put (plist, QCrelief,
10171 (selected_p
10172 ? make_number (-relief)
10173 : make_number (relief)));
10174 hmargin -= relief;
10175 vmargin -= relief;
10176 }
10177
10178 /* Put a margin around the image. */
10179 if (hmargin || vmargin)
10180 {
10181 if (hmargin == vmargin)
10182 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10183 else
10184 plist = Fplist_put (plist, QCmargin,
10185 Fcons (make_number (hmargin),
10186 make_number (vmargin)));
10187 }
10188
10189 /* If button is not enabled, and we don't have special images
10190 for the disabled state, make the image appear disabled by
10191 applying an appropriate algorithm to it. */
10192 if (!enabled_p && idx < 0)
10193 plist = Fplist_put (plist, QCconversion, Qdisabled);
10194
10195 /* Put a `display' text property on the string for the image to
10196 display. Put a `menu-item' property on the string that gives
10197 the start of this item's properties in the tool-bar items
10198 vector. */
10199 image = Fcons (Qimage, plist);
10200 props = list4 (Qdisplay, image,
10201 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10202
10203 /* Let the last image hide all remaining spaces in the tool bar
10204 string. The string can be longer than needed when we reuse a
10205 previous string. */
10206 if (i + 1 == f->n_tool_bar_items)
10207 end = SCHARS (f->desired_tool_bar_string);
10208 else
10209 end = i + 1;
10210 Fadd_text_properties (make_number (i), make_number (end),
10211 props, f->desired_tool_bar_string);
10212 #undef PROP
10213 }
10214
10215 UNGCPRO;
10216 }
10217
10218
10219 /* Display one line of the tool-bar of frame IT->f.
10220
10221 HEIGHT specifies the desired height of the tool-bar line.
10222 If the actual height of the glyph row is less than HEIGHT, the
10223 row's height is increased to HEIGHT, and the icons are centered
10224 vertically in the new height.
10225
10226 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10227 count a final empty row in case the tool-bar width exactly matches
10228 the window width.
10229 */
10230
10231 static void
10232 display_tool_bar_line (struct it *it, int height)
10233 {
10234 struct glyph_row *row = it->glyph_row;
10235 int max_x = it->last_visible_x;
10236 struct glyph *last;
10237
10238 prepare_desired_row (row);
10239 row->y = it->current_y;
10240
10241 /* Note that this isn't made use of if the face hasn't a box,
10242 so there's no need to check the face here. */
10243 it->start_of_box_run_p = 1;
10244
10245 while (it->current_x < max_x)
10246 {
10247 int x, n_glyphs_before, i, nglyphs;
10248 struct it it_before;
10249
10250 /* Get the next display element. */
10251 if (!get_next_display_element (it))
10252 {
10253 /* Don't count empty row if we are counting needed tool-bar lines. */
10254 if (height < 0 && !it->hpos)
10255 return;
10256 break;
10257 }
10258
10259 /* Produce glyphs. */
10260 n_glyphs_before = row->used[TEXT_AREA];
10261 it_before = *it;
10262
10263 PRODUCE_GLYPHS (it);
10264
10265 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10266 i = 0;
10267 x = it_before.current_x;
10268 while (i < nglyphs)
10269 {
10270 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10271
10272 if (x + glyph->pixel_width > max_x)
10273 {
10274 /* Glyph doesn't fit on line. Backtrack. */
10275 row->used[TEXT_AREA] = n_glyphs_before;
10276 *it = it_before;
10277 /* If this is the only glyph on this line, it will never fit on the
10278 tool-bar, so skip it. But ensure there is at least one glyph,
10279 so we don't accidentally disable the tool-bar. */
10280 if (n_glyphs_before == 0
10281 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10282 break;
10283 goto out;
10284 }
10285
10286 ++it->hpos;
10287 x += glyph->pixel_width;
10288 ++i;
10289 }
10290
10291 /* Stop at line ends. */
10292 if (ITERATOR_AT_END_OF_LINE_P (it))
10293 break;
10294
10295 set_iterator_to_next (it, 1);
10296 }
10297
10298 out:;
10299
10300 row->displays_text_p = row->used[TEXT_AREA] != 0;
10301
10302 /* Use default face for the border below the tool bar.
10303
10304 FIXME: When auto-resize-tool-bars is grow-only, there is
10305 no additional border below the possibly empty tool-bar lines.
10306 So to make the extra empty lines look "normal", we have to
10307 use the tool-bar face for the border too. */
10308 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10309 it->face_id = DEFAULT_FACE_ID;
10310
10311 extend_face_to_end_of_line (it);
10312 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10313 last->right_box_line_p = 1;
10314 if (last == row->glyphs[TEXT_AREA])
10315 last->left_box_line_p = 1;
10316
10317 /* Make line the desired height and center it vertically. */
10318 if ((height -= it->max_ascent + it->max_descent) > 0)
10319 {
10320 /* Don't add more than one line height. */
10321 height %= FRAME_LINE_HEIGHT (it->f);
10322 it->max_ascent += height / 2;
10323 it->max_descent += (height + 1) / 2;
10324 }
10325
10326 compute_line_metrics (it);
10327
10328 /* If line is empty, make it occupy the rest of the tool-bar. */
10329 if (!row->displays_text_p)
10330 {
10331 row->height = row->phys_height = it->last_visible_y - row->y;
10332 row->visible_height = row->height;
10333 row->ascent = row->phys_ascent = 0;
10334 row->extra_line_spacing = 0;
10335 }
10336
10337 row->full_width_p = 1;
10338 row->continued_p = 0;
10339 row->truncated_on_left_p = 0;
10340 row->truncated_on_right_p = 0;
10341
10342 it->current_x = it->hpos = 0;
10343 it->current_y += row->height;
10344 ++it->vpos;
10345 ++it->glyph_row;
10346 }
10347
10348
10349 /* Max tool-bar height. */
10350
10351 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10352 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10353
10354 /* Value is the number of screen lines needed to make all tool-bar
10355 items of frame F visible. The number of actual rows needed is
10356 returned in *N_ROWS if non-NULL. */
10357
10358 static int
10359 tool_bar_lines_needed (struct frame *f, int *n_rows)
10360 {
10361 struct window *w = XWINDOW (f->tool_bar_window);
10362 struct it it;
10363 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10364 the desired matrix, so use (unused) mode-line row as temporary row to
10365 avoid destroying the first tool-bar row. */
10366 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10367
10368 /* Initialize an iterator for iteration over
10369 F->desired_tool_bar_string in the tool-bar window of frame F. */
10370 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10371 it.first_visible_x = 0;
10372 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10373 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10374
10375 while (!ITERATOR_AT_END_P (&it))
10376 {
10377 clear_glyph_row (temp_row);
10378 it.glyph_row = temp_row;
10379 display_tool_bar_line (&it, -1);
10380 }
10381 clear_glyph_row (temp_row);
10382
10383 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10384 if (n_rows)
10385 *n_rows = it.vpos > 0 ? it.vpos : -1;
10386
10387 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10388 }
10389
10390
10391 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10392 0, 1, 0,
10393 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10394 (Lisp_Object frame)
10395 {
10396 struct frame *f;
10397 struct window *w;
10398 int nlines = 0;
10399
10400 if (NILP (frame))
10401 frame = selected_frame;
10402 else
10403 CHECK_FRAME (frame);
10404 f = XFRAME (frame);
10405
10406 if (WINDOWP (f->tool_bar_window)
10407 || (w = XWINDOW (f->tool_bar_window),
10408 WINDOW_TOTAL_LINES (w) > 0))
10409 {
10410 update_tool_bar (f, 1);
10411 if (f->n_tool_bar_items)
10412 {
10413 build_desired_tool_bar_string (f);
10414 nlines = tool_bar_lines_needed (f, NULL);
10415 }
10416 }
10417
10418 return make_number (nlines);
10419 }
10420
10421
10422 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10423 height should be changed. */
10424
10425 static int
10426 redisplay_tool_bar (struct frame *f)
10427 {
10428 struct window *w;
10429 struct it it;
10430 struct glyph_row *row;
10431
10432 #if defined (USE_GTK) || defined (HAVE_NS)
10433 if (FRAME_EXTERNAL_TOOL_BAR (f))
10434 update_frame_tool_bar (f);
10435 return 0;
10436 #endif
10437
10438 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10439 do anything. This means you must start with tool-bar-lines
10440 non-zero to get the auto-sizing effect. Or in other words, you
10441 can turn off tool-bars by specifying tool-bar-lines zero. */
10442 if (!WINDOWP (f->tool_bar_window)
10443 || (w = XWINDOW (f->tool_bar_window),
10444 WINDOW_TOTAL_LINES (w) == 0))
10445 return 0;
10446
10447 /* Set up an iterator for the tool-bar window. */
10448 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10449 it.first_visible_x = 0;
10450 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10451 row = it.glyph_row;
10452
10453 /* Build a string that represents the contents of the tool-bar. */
10454 build_desired_tool_bar_string (f);
10455 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10456
10457 if (f->n_tool_bar_rows == 0)
10458 {
10459 int nlines;
10460
10461 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10462 nlines != WINDOW_TOTAL_LINES (w)))
10463 {
10464 Lisp_Object frame;
10465 int old_height = WINDOW_TOTAL_LINES (w);
10466
10467 XSETFRAME (frame, f);
10468 Fmodify_frame_parameters (frame,
10469 Fcons (Fcons (Qtool_bar_lines,
10470 make_number (nlines)),
10471 Qnil));
10472 if (WINDOW_TOTAL_LINES (w) != old_height)
10473 {
10474 clear_glyph_matrix (w->desired_matrix);
10475 fonts_changed_p = 1;
10476 return 1;
10477 }
10478 }
10479 }
10480
10481 /* Display as many lines as needed to display all tool-bar items. */
10482
10483 if (f->n_tool_bar_rows > 0)
10484 {
10485 int border, rows, height, extra;
10486
10487 if (INTEGERP (Vtool_bar_border))
10488 border = XINT (Vtool_bar_border);
10489 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10490 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10491 else if (EQ (Vtool_bar_border, Qborder_width))
10492 border = f->border_width;
10493 else
10494 border = 0;
10495 if (border < 0)
10496 border = 0;
10497
10498 rows = f->n_tool_bar_rows;
10499 height = max (1, (it.last_visible_y - border) / rows);
10500 extra = it.last_visible_y - border - height * rows;
10501
10502 while (it.current_y < it.last_visible_y)
10503 {
10504 int h = 0;
10505 if (extra > 0 && rows-- > 0)
10506 {
10507 h = (extra + rows - 1) / rows;
10508 extra -= h;
10509 }
10510 display_tool_bar_line (&it, height + h);
10511 }
10512 }
10513 else
10514 {
10515 while (it.current_y < it.last_visible_y)
10516 display_tool_bar_line (&it, 0);
10517 }
10518
10519 /* It doesn't make much sense to try scrolling in the tool-bar
10520 window, so don't do it. */
10521 w->desired_matrix->no_scrolling_p = 1;
10522 w->must_be_updated_p = 1;
10523
10524 if (!NILP (Vauto_resize_tool_bars))
10525 {
10526 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10527 int change_height_p = 0;
10528
10529 /* If we couldn't display everything, change the tool-bar's
10530 height if there is room for more. */
10531 if (IT_STRING_CHARPOS (it) < it.end_charpos
10532 && it.current_y < max_tool_bar_height)
10533 change_height_p = 1;
10534
10535 row = it.glyph_row - 1;
10536
10537 /* If there are blank lines at the end, except for a partially
10538 visible blank line at the end that is smaller than
10539 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10540 if (!row->displays_text_p
10541 && row->height >= FRAME_LINE_HEIGHT (f))
10542 change_height_p = 1;
10543
10544 /* If row displays tool-bar items, but is partially visible,
10545 change the tool-bar's height. */
10546 if (row->displays_text_p
10547 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10548 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10549 change_height_p = 1;
10550
10551 /* Resize windows as needed by changing the `tool-bar-lines'
10552 frame parameter. */
10553 if (change_height_p)
10554 {
10555 Lisp_Object frame;
10556 int old_height = WINDOW_TOTAL_LINES (w);
10557 int nrows;
10558 int nlines = tool_bar_lines_needed (f, &nrows);
10559
10560 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10561 && !f->minimize_tool_bar_window_p)
10562 ? (nlines > old_height)
10563 : (nlines != old_height));
10564 f->minimize_tool_bar_window_p = 0;
10565
10566 if (change_height_p)
10567 {
10568 XSETFRAME (frame, f);
10569 Fmodify_frame_parameters (frame,
10570 Fcons (Fcons (Qtool_bar_lines,
10571 make_number (nlines)),
10572 Qnil));
10573 if (WINDOW_TOTAL_LINES (w) != old_height)
10574 {
10575 clear_glyph_matrix (w->desired_matrix);
10576 f->n_tool_bar_rows = nrows;
10577 fonts_changed_p = 1;
10578 return 1;
10579 }
10580 }
10581 }
10582 }
10583
10584 f->minimize_tool_bar_window_p = 0;
10585 return 0;
10586 }
10587
10588
10589 /* Get information about the tool-bar item which is displayed in GLYPH
10590 on frame F. Return in *PROP_IDX the index where tool-bar item
10591 properties start in F->tool_bar_items. Value is zero if
10592 GLYPH doesn't display a tool-bar item. */
10593
10594 static int
10595 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10596 {
10597 Lisp_Object prop;
10598 int success_p;
10599 int charpos;
10600
10601 /* This function can be called asynchronously, which means we must
10602 exclude any possibility that Fget_text_property signals an
10603 error. */
10604 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10605 charpos = max (0, charpos);
10606
10607 /* Get the text property `menu-item' at pos. The value of that
10608 property is the start index of this item's properties in
10609 F->tool_bar_items. */
10610 prop = Fget_text_property (make_number (charpos),
10611 Qmenu_item, f->current_tool_bar_string);
10612 if (INTEGERP (prop))
10613 {
10614 *prop_idx = XINT (prop);
10615 success_p = 1;
10616 }
10617 else
10618 success_p = 0;
10619
10620 return success_p;
10621 }
10622
10623 \f
10624 /* Get information about the tool-bar item at position X/Y on frame F.
10625 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10626 the current matrix of the tool-bar window of F, or NULL if not
10627 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10628 item in F->tool_bar_items. Value is
10629
10630 -1 if X/Y is not on a tool-bar item
10631 0 if X/Y is on the same item that was highlighted before.
10632 1 otherwise. */
10633
10634 static int
10635 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10636 int *hpos, int *vpos, int *prop_idx)
10637 {
10638 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10639 struct window *w = XWINDOW (f->tool_bar_window);
10640 int area;
10641
10642 /* Find the glyph under X/Y. */
10643 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10644 if (*glyph == NULL)
10645 return -1;
10646
10647 /* Get the start of this tool-bar item's properties in
10648 f->tool_bar_items. */
10649 if (!tool_bar_item_info (f, *glyph, prop_idx))
10650 return -1;
10651
10652 /* Is mouse on the highlighted item? */
10653 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10654 && *vpos >= hlinfo->mouse_face_beg_row
10655 && *vpos <= hlinfo->mouse_face_end_row
10656 && (*vpos > hlinfo->mouse_face_beg_row
10657 || *hpos >= hlinfo->mouse_face_beg_col)
10658 && (*vpos < hlinfo->mouse_face_end_row
10659 || *hpos < hlinfo->mouse_face_end_col
10660 || hlinfo->mouse_face_past_end))
10661 return 0;
10662
10663 return 1;
10664 }
10665
10666
10667 /* EXPORT:
10668 Handle mouse button event on the tool-bar of frame F, at
10669 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10670 0 for button release. MODIFIERS is event modifiers for button
10671 release. */
10672
10673 void
10674 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10675 unsigned int modifiers)
10676 {
10677 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10678 struct window *w = XWINDOW (f->tool_bar_window);
10679 int hpos, vpos, prop_idx;
10680 struct glyph *glyph;
10681 Lisp_Object enabled_p;
10682
10683 /* If not on the highlighted tool-bar item, return. */
10684 frame_to_window_pixel_xy (w, &x, &y);
10685 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10686 return;
10687
10688 /* If item is disabled, do nothing. */
10689 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10690 if (NILP (enabled_p))
10691 return;
10692
10693 if (down_p)
10694 {
10695 /* Show item in pressed state. */
10696 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10697 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10698 last_tool_bar_item = prop_idx;
10699 }
10700 else
10701 {
10702 Lisp_Object key, frame;
10703 struct input_event event;
10704 EVENT_INIT (event);
10705
10706 /* Show item in released state. */
10707 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10708 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10709
10710 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10711
10712 XSETFRAME (frame, f);
10713 event.kind = TOOL_BAR_EVENT;
10714 event.frame_or_window = frame;
10715 event.arg = frame;
10716 kbd_buffer_store_event (&event);
10717
10718 event.kind = TOOL_BAR_EVENT;
10719 event.frame_or_window = frame;
10720 event.arg = key;
10721 event.modifiers = modifiers;
10722 kbd_buffer_store_event (&event);
10723 last_tool_bar_item = -1;
10724 }
10725 }
10726
10727
10728 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10729 tool-bar window-relative coordinates X/Y. Called from
10730 note_mouse_highlight. */
10731
10732 static void
10733 note_tool_bar_highlight (struct frame *f, int x, int y)
10734 {
10735 Lisp_Object window = f->tool_bar_window;
10736 struct window *w = XWINDOW (window);
10737 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10738 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10739 int hpos, vpos;
10740 struct glyph *glyph;
10741 struct glyph_row *row;
10742 int i;
10743 Lisp_Object enabled_p;
10744 int prop_idx;
10745 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10746 int mouse_down_p, rc;
10747
10748 /* Function note_mouse_highlight is called with negative X/Y
10749 values when mouse moves outside of the frame. */
10750 if (x <= 0 || y <= 0)
10751 {
10752 clear_mouse_face (hlinfo);
10753 return;
10754 }
10755
10756 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10757 if (rc < 0)
10758 {
10759 /* Not on tool-bar item. */
10760 clear_mouse_face (hlinfo);
10761 return;
10762 }
10763 else if (rc == 0)
10764 /* On same tool-bar item as before. */
10765 goto set_help_echo;
10766
10767 clear_mouse_face (hlinfo);
10768
10769 /* Mouse is down, but on different tool-bar item? */
10770 mouse_down_p = (dpyinfo->grabbed
10771 && f == last_mouse_frame
10772 && FRAME_LIVE_P (f));
10773 if (mouse_down_p
10774 && last_tool_bar_item != prop_idx)
10775 return;
10776
10777 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10778 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10779
10780 /* If tool-bar item is not enabled, don't highlight it. */
10781 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10782 if (!NILP (enabled_p))
10783 {
10784 /* Compute the x-position of the glyph. In front and past the
10785 image is a space. We include this in the highlighted area. */
10786 row = MATRIX_ROW (w->current_matrix, vpos);
10787 for (i = x = 0; i < hpos; ++i)
10788 x += row->glyphs[TEXT_AREA][i].pixel_width;
10789
10790 /* Record this as the current active region. */
10791 hlinfo->mouse_face_beg_col = hpos;
10792 hlinfo->mouse_face_beg_row = vpos;
10793 hlinfo->mouse_face_beg_x = x;
10794 hlinfo->mouse_face_beg_y = row->y;
10795 hlinfo->mouse_face_past_end = 0;
10796
10797 hlinfo->mouse_face_end_col = hpos + 1;
10798 hlinfo->mouse_face_end_row = vpos;
10799 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
10800 hlinfo->mouse_face_end_y = row->y;
10801 hlinfo->mouse_face_window = window;
10802 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10803
10804 /* Display it as active. */
10805 show_mouse_face (hlinfo, draw);
10806 hlinfo->mouse_face_image_state = draw;
10807 }
10808
10809 set_help_echo:
10810
10811 /* Set help_echo_string to a help string to display for this tool-bar item.
10812 XTread_socket does the rest. */
10813 help_echo_object = help_echo_window = Qnil;
10814 help_echo_pos = -1;
10815 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10816 if (NILP (help_echo_string))
10817 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10818 }
10819
10820 #endif /* HAVE_WINDOW_SYSTEM */
10821
10822
10823 \f
10824 /************************************************************************
10825 Horizontal scrolling
10826 ************************************************************************/
10827
10828 static int hscroll_window_tree (Lisp_Object);
10829 static int hscroll_windows (Lisp_Object);
10830
10831 /* For all leaf windows in the window tree rooted at WINDOW, set their
10832 hscroll value so that PT is (i) visible in the window, and (ii) so
10833 that it is not within a certain margin at the window's left and
10834 right border. Value is non-zero if any window's hscroll has been
10835 changed. */
10836
10837 static int
10838 hscroll_window_tree (Lisp_Object window)
10839 {
10840 int hscrolled_p = 0;
10841 int hscroll_relative_p = FLOATP (Vhscroll_step);
10842 int hscroll_step_abs = 0;
10843 double hscroll_step_rel = 0;
10844
10845 if (hscroll_relative_p)
10846 {
10847 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10848 if (hscroll_step_rel < 0)
10849 {
10850 hscroll_relative_p = 0;
10851 hscroll_step_abs = 0;
10852 }
10853 }
10854 else if (INTEGERP (Vhscroll_step))
10855 {
10856 hscroll_step_abs = XINT (Vhscroll_step);
10857 if (hscroll_step_abs < 0)
10858 hscroll_step_abs = 0;
10859 }
10860 else
10861 hscroll_step_abs = 0;
10862
10863 while (WINDOWP (window))
10864 {
10865 struct window *w = XWINDOW (window);
10866
10867 if (WINDOWP (w->hchild))
10868 hscrolled_p |= hscroll_window_tree (w->hchild);
10869 else if (WINDOWP (w->vchild))
10870 hscrolled_p |= hscroll_window_tree (w->vchild);
10871 else if (w->cursor.vpos >= 0)
10872 {
10873 int h_margin;
10874 int text_area_width;
10875 struct glyph_row *current_cursor_row
10876 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10877 struct glyph_row *desired_cursor_row
10878 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10879 struct glyph_row *cursor_row
10880 = (desired_cursor_row->enabled_p
10881 ? desired_cursor_row
10882 : current_cursor_row);
10883
10884 text_area_width = window_box_width (w, TEXT_AREA);
10885
10886 /* Scroll when cursor is inside this scroll margin. */
10887 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10888
10889 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10890 && ((XFASTINT (w->hscroll)
10891 && w->cursor.x <= h_margin)
10892 || (cursor_row->enabled_p
10893 && cursor_row->truncated_on_right_p
10894 && (w->cursor.x >= text_area_width - h_margin))))
10895 {
10896 struct it it;
10897 int hscroll;
10898 struct buffer *saved_current_buffer;
10899 EMACS_INT pt;
10900 int wanted_x;
10901
10902 /* Find point in a display of infinite width. */
10903 saved_current_buffer = current_buffer;
10904 current_buffer = XBUFFER (w->buffer);
10905
10906 if (w == XWINDOW (selected_window))
10907 pt = PT;
10908 else
10909 {
10910 pt = marker_position (w->pointm);
10911 pt = max (BEGV, pt);
10912 pt = min (ZV, pt);
10913 }
10914
10915 /* Move iterator to pt starting at cursor_row->start in
10916 a line with infinite width. */
10917 init_to_row_start (&it, w, cursor_row);
10918 it.last_visible_x = INFINITY;
10919 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10920 current_buffer = saved_current_buffer;
10921
10922 /* Position cursor in window. */
10923 if (!hscroll_relative_p && hscroll_step_abs == 0)
10924 hscroll = max (0, (it.current_x
10925 - (ITERATOR_AT_END_OF_LINE_P (&it)
10926 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10927 : (text_area_width / 2))))
10928 / FRAME_COLUMN_WIDTH (it.f);
10929 else if (w->cursor.x >= text_area_width - h_margin)
10930 {
10931 if (hscroll_relative_p)
10932 wanted_x = text_area_width * (1 - hscroll_step_rel)
10933 - h_margin;
10934 else
10935 wanted_x = text_area_width
10936 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10937 - h_margin;
10938 hscroll
10939 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10940 }
10941 else
10942 {
10943 if (hscroll_relative_p)
10944 wanted_x = text_area_width * hscroll_step_rel
10945 + h_margin;
10946 else
10947 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10948 + h_margin;
10949 hscroll
10950 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10951 }
10952 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10953
10954 /* Don't call Fset_window_hscroll if value hasn't
10955 changed because it will prevent redisplay
10956 optimizations. */
10957 if (XFASTINT (w->hscroll) != hscroll)
10958 {
10959 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10960 w->hscroll = make_number (hscroll);
10961 hscrolled_p = 1;
10962 }
10963 }
10964 }
10965
10966 window = w->next;
10967 }
10968
10969 /* Value is non-zero if hscroll of any leaf window has been changed. */
10970 return hscrolled_p;
10971 }
10972
10973
10974 /* Set hscroll so that cursor is visible and not inside horizontal
10975 scroll margins for all windows in the tree rooted at WINDOW. See
10976 also hscroll_window_tree above. Value is non-zero if any window's
10977 hscroll has been changed. If it has, desired matrices on the frame
10978 of WINDOW are cleared. */
10979
10980 static int
10981 hscroll_windows (Lisp_Object window)
10982 {
10983 int hscrolled_p = hscroll_window_tree (window);
10984 if (hscrolled_p)
10985 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10986 return hscrolled_p;
10987 }
10988
10989
10990 \f
10991 /************************************************************************
10992 Redisplay
10993 ************************************************************************/
10994
10995 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10996 to a non-zero value. This is sometimes handy to have in a debugger
10997 session. */
10998
10999 #if GLYPH_DEBUG
11000
11001 /* First and last unchanged row for try_window_id. */
11002
11003 int debug_first_unchanged_at_end_vpos;
11004 int debug_last_unchanged_at_beg_vpos;
11005
11006 /* Delta vpos and y. */
11007
11008 int debug_dvpos, debug_dy;
11009
11010 /* Delta in characters and bytes for try_window_id. */
11011
11012 EMACS_INT debug_delta, debug_delta_bytes;
11013
11014 /* Values of window_end_pos and window_end_vpos at the end of
11015 try_window_id. */
11016
11017 EMACS_INT debug_end_vpos;
11018
11019 /* Append a string to W->desired_matrix->method. FMT is a printf
11020 format string. A1...A9 are a supplement for a variable-length
11021 argument list. If trace_redisplay_p is non-zero also printf the
11022 resulting string to stderr. */
11023
11024 static void
11025 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11026 struct window *w;
11027 char *fmt;
11028 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11029 {
11030 char buffer[512];
11031 char *method = w->desired_matrix->method;
11032 int len = strlen (method);
11033 int size = sizeof w->desired_matrix->method;
11034 int remaining = size - len - 1;
11035
11036 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11037 if (len && remaining)
11038 {
11039 method[len] = '|';
11040 --remaining, ++len;
11041 }
11042
11043 strncpy (method + len, buffer, remaining);
11044
11045 if (trace_redisplay_p)
11046 fprintf (stderr, "%p (%s): %s\n",
11047 w,
11048 ((BUFFERP (w->buffer)
11049 && STRINGP (XBUFFER (w->buffer)->name))
11050 ? SSDATA (XBUFFER (w->buffer)->name)
11051 : "no buffer"),
11052 buffer);
11053 }
11054
11055 #endif /* GLYPH_DEBUG */
11056
11057
11058 /* Value is non-zero if all changes in window W, which displays
11059 current_buffer, are in the text between START and END. START is a
11060 buffer position, END is given as a distance from Z. Used in
11061 redisplay_internal for display optimization. */
11062
11063 static INLINE int
11064 text_outside_line_unchanged_p (struct window *w,
11065 EMACS_INT start, EMACS_INT end)
11066 {
11067 int unchanged_p = 1;
11068
11069 /* If text or overlays have changed, see where. */
11070 if (XFASTINT (w->last_modified) < MODIFF
11071 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11072 {
11073 /* Gap in the line? */
11074 if (GPT < start || Z - GPT < end)
11075 unchanged_p = 0;
11076
11077 /* Changes start in front of the line, or end after it? */
11078 if (unchanged_p
11079 && (BEG_UNCHANGED < start - 1
11080 || END_UNCHANGED < end))
11081 unchanged_p = 0;
11082
11083 /* If selective display, can't optimize if changes start at the
11084 beginning of the line. */
11085 if (unchanged_p
11086 && INTEGERP (BVAR (current_buffer, selective_display))
11087 && XINT (BVAR (current_buffer, selective_display)) > 0
11088 && (BEG_UNCHANGED < start || GPT <= start))
11089 unchanged_p = 0;
11090
11091 /* If there are overlays at the start or end of the line, these
11092 may have overlay strings with newlines in them. A change at
11093 START, for instance, may actually concern the display of such
11094 overlay strings as well, and they are displayed on different
11095 lines. So, quickly rule out this case. (For the future, it
11096 might be desirable to implement something more telling than
11097 just BEG/END_UNCHANGED.) */
11098 if (unchanged_p)
11099 {
11100 if (BEG + BEG_UNCHANGED == start
11101 && overlay_touches_p (start))
11102 unchanged_p = 0;
11103 if (END_UNCHANGED == end
11104 && overlay_touches_p (Z - end))
11105 unchanged_p = 0;
11106 }
11107
11108 /* Under bidi reordering, adding or deleting a character in the
11109 beginning of a paragraph, before the first strong directional
11110 character, can change the base direction of the paragraph (unless
11111 the buffer specifies a fixed paragraph direction), which will
11112 require to redisplay the whole paragraph. It might be worthwhile
11113 to find the paragraph limits and widen the range of redisplayed
11114 lines to that, but for now just give up this optimization. */
11115 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11116 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11117 unchanged_p = 0;
11118 }
11119
11120 return unchanged_p;
11121 }
11122
11123
11124 /* Do a frame update, taking possible shortcuts into account. This is
11125 the main external entry point for redisplay.
11126
11127 If the last redisplay displayed an echo area message and that message
11128 is no longer requested, we clear the echo area or bring back the
11129 mini-buffer if that is in use. */
11130
11131 void
11132 redisplay (void)
11133 {
11134 redisplay_internal ();
11135 }
11136
11137
11138 static Lisp_Object
11139 overlay_arrow_string_or_property (Lisp_Object var)
11140 {
11141 Lisp_Object val;
11142
11143 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11144 return val;
11145
11146 return Voverlay_arrow_string;
11147 }
11148
11149 /* Return 1 if there are any overlay-arrows in current_buffer. */
11150 static int
11151 overlay_arrow_in_current_buffer_p (void)
11152 {
11153 Lisp_Object vlist;
11154
11155 for (vlist = Voverlay_arrow_variable_list;
11156 CONSP (vlist);
11157 vlist = XCDR (vlist))
11158 {
11159 Lisp_Object var = XCAR (vlist);
11160 Lisp_Object val;
11161
11162 if (!SYMBOLP (var))
11163 continue;
11164 val = find_symbol_value (var);
11165 if (MARKERP (val)
11166 && current_buffer == XMARKER (val)->buffer)
11167 return 1;
11168 }
11169 return 0;
11170 }
11171
11172
11173 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11174 has changed. */
11175
11176 static int
11177 overlay_arrows_changed_p (void)
11178 {
11179 Lisp_Object vlist;
11180
11181 for (vlist = Voverlay_arrow_variable_list;
11182 CONSP (vlist);
11183 vlist = XCDR (vlist))
11184 {
11185 Lisp_Object var = XCAR (vlist);
11186 Lisp_Object val, pstr;
11187
11188 if (!SYMBOLP (var))
11189 continue;
11190 val = find_symbol_value (var);
11191 if (!MARKERP (val))
11192 continue;
11193 if (! EQ (COERCE_MARKER (val),
11194 Fget (var, Qlast_arrow_position))
11195 || ! (pstr = overlay_arrow_string_or_property (var),
11196 EQ (pstr, Fget (var, Qlast_arrow_string))))
11197 return 1;
11198 }
11199 return 0;
11200 }
11201
11202 /* Mark overlay arrows to be updated on next redisplay. */
11203
11204 static void
11205 update_overlay_arrows (int up_to_date)
11206 {
11207 Lisp_Object vlist;
11208
11209 for (vlist = Voverlay_arrow_variable_list;
11210 CONSP (vlist);
11211 vlist = XCDR (vlist))
11212 {
11213 Lisp_Object var = XCAR (vlist);
11214
11215 if (!SYMBOLP (var))
11216 continue;
11217
11218 if (up_to_date > 0)
11219 {
11220 Lisp_Object val = find_symbol_value (var);
11221 Fput (var, Qlast_arrow_position,
11222 COERCE_MARKER (val));
11223 Fput (var, Qlast_arrow_string,
11224 overlay_arrow_string_or_property (var));
11225 }
11226 else if (up_to_date < 0
11227 || !NILP (Fget (var, Qlast_arrow_position)))
11228 {
11229 Fput (var, Qlast_arrow_position, Qt);
11230 Fput (var, Qlast_arrow_string, Qt);
11231 }
11232 }
11233 }
11234
11235
11236 /* Return overlay arrow string to display at row.
11237 Return integer (bitmap number) for arrow bitmap in left fringe.
11238 Return nil if no overlay arrow. */
11239
11240 static Lisp_Object
11241 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11242 {
11243 Lisp_Object vlist;
11244
11245 for (vlist = Voverlay_arrow_variable_list;
11246 CONSP (vlist);
11247 vlist = XCDR (vlist))
11248 {
11249 Lisp_Object var = XCAR (vlist);
11250 Lisp_Object val;
11251
11252 if (!SYMBOLP (var))
11253 continue;
11254
11255 val = find_symbol_value (var);
11256
11257 if (MARKERP (val)
11258 && current_buffer == XMARKER (val)->buffer
11259 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11260 {
11261 if (FRAME_WINDOW_P (it->f)
11262 /* FIXME: if ROW->reversed_p is set, this should test
11263 the right fringe, not the left one. */
11264 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11265 {
11266 #ifdef HAVE_WINDOW_SYSTEM
11267 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11268 {
11269 int fringe_bitmap;
11270 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11271 return make_number (fringe_bitmap);
11272 }
11273 #endif
11274 return make_number (-1); /* Use default arrow bitmap */
11275 }
11276 return overlay_arrow_string_or_property (var);
11277 }
11278 }
11279
11280 return Qnil;
11281 }
11282
11283 /* Return 1 if point moved out of or into a composition. Otherwise
11284 return 0. PREV_BUF and PREV_PT are the last point buffer and
11285 position. BUF and PT are the current point buffer and position. */
11286
11287 int
11288 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11289 struct buffer *buf, EMACS_INT pt)
11290 {
11291 EMACS_INT start, end;
11292 Lisp_Object prop;
11293 Lisp_Object buffer;
11294
11295 XSETBUFFER (buffer, buf);
11296 /* Check a composition at the last point if point moved within the
11297 same buffer. */
11298 if (prev_buf == buf)
11299 {
11300 if (prev_pt == pt)
11301 /* Point didn't move. */
11302 return 0;
11303
11304 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11305 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11306 && COMPOSITION_VALID_P (start, end, prop)
11307 && start < prev_pt && end > prev_pt)
11308 /* The last point was within the composition. Return 1 iff
11309 point moved out of the composition. */
11310 return (pt <= start || pt >= end);
11311 }
11312
11313 /* Check a composition at the current point. */
11314 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11315 && find_composition (pt, -1, &start, &end, &prop, buffer)
11316 && COMPOSITION_VALID_P (start, end, prop)
11317 && start < pt && end > pt);
11318 }
11319
11320
11321 /* Reconsider the setting of B->clip_changed which is displayed
11322 in window W. */
11323
11324 static INLINE void
11325 reconsider_clip_changes (struct window *w, struct buffer *b)
11326 {
11327 if (b->clip_changed
11328 && !NILP (w->window_end_valid)
11329 && w->current_matrix->buffer == b
11330 && w->current_matrix->zv == BUF_ZV (b)
11331 && w->current_matrix->begv == BUF_BEGV (b))
11332 b->clip_changed = 0;
11333
11334 /* If display wasn't paused, and W is not a tool bar window, see if
11335 point has been moved into or out of a composition. In that case,
11336 we set b->clip_changed to 1 to force updating the screen. If
11337 b->clip_changed has already been set to 1, we can skip this
11338 check. */
11339 if (!b->clip_changed
11340 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11341 {
11342 EMACS_INT pt;
11343
11344 if (w == XWINDOW (selected_window))
11345 pt = PT;
11346 else
11347 pt = marker_position (w->pointm);
11348
11349 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11350 || pt != XINT (w->last_point))
11351 && check_point_in_composition (w->current_matrix->buffer,
11352 XINT (w->last_point),
11353 XBUFFER (w->buffer), pt))
11354 b->clip_changed = 1;
11355 }
11356 }
11357 \f
11358
11359 /* Select FRAME to forward the values of frame-local variables into C
11360 variables so that the redisplay routines can access those values
11361 directly. */
11362
11363 static void
11364 select_frame_for_redisplay (Lisp_Object frame)
11365 {
11366 Lisp_Object tail, tem;
11367 Lisp_Object old = selected_frame;
11368 struct Lisp_Symbol *sym;
11369
11370 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11371
11372 selected_frame = frame;
11373
11374 do {
11375 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11376 if (CONSP (XCAR (tail))
11377 && (tem = XCAR (XCAR (tail)),
11378 SYMBOLP (tem))
11379 && (sym = indirect_variable (XSYMBOL (tem)),
11380 sym->redirect == SYMBOL_LOCALIZED)
11381 && sym->val.blv->frame_local)
11382 /* Use find_symbol_value rather than Fsymbol_value
11383 to avoid an error if it is void. */
11384 find_symbol_value (tem);
11385 } while (!EQ (frame, old) && (frame = old, 1));
11386 }
11387
11388
11389 #define STOP_POLLING \
11390 do { if (! polling_stopped_here) stop_polling (); \
11391 polling_stopped_here = 1; } while (0)
11392
11393 #define RESUME_POLLING \
11394 do { if (polling_stopped_here) start_polling (); \
11395 polling_stopped_here = 0; } while (0)
11396
11397
11398 /* Perhaps in the future avoid recentering windows if it
11399 is not necessary; currently that causes some problems. */
11400
11401 static void
11402 redisplay_internal (void)
11403 {
11404 struct window *w = XWINDOW (selected_window);
11405 struct window *sw;
11406 struct frame *fr;
11407 int pending;
11408 int must_finish = 0;
11409 struct text_pos tlbufpos, tlendpos;
11410 int number_of_visible_frames;
11411 int count, count1;
11412 struct frame *sf;
11413 int polling_stopped_here = 0;
11414 Lisp_Object old_frame = selected_frame;
11415
11416 /* Non-zero means redisplay has to consider all windows on all
11417 frames. Zero means, only selected_window is considered. */
11418 int consider_all_windows_p;
11419
11420 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11421
11422 /* No redisplay if running in batch mode or frame is not yet fully
11423 initialized, or redisplay is explicitly turned off by setting
11424 Vinhibit_redisplay. */
11425 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11426 || !NILP (Vinhibit_redisplay))
11427 return;
11428
11429 /* Don't examine these until after testing Vinhibit_redisplay.
11430 When Emacs is shutting down, perhaps because its connection to
11431 X has dropped, we should not look at them at all. */
11432 fr = XFRAME (w->frame);
11433 sf = SELECTED_FRAME ();
11434
11435 if (!fr->glyphs_initialized_p)
11436 return;
11437
11438 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11439 if (popup_activated ())
11440 return;
11441 #endif
11442
11443 /* I don't think this happens but let's be paranoid. */
11444 if (redisplaying_p)
11445 return;
11446
11447 /* Record a function that resets redisplaying_p to its old value
11448 when we leave this function. */
11449 count = SPECPDL_INDEX ();
11450 record_unwind_protect (unwind_redisplay,
11451 Fcons (make_number (redisplaying_p), selected_frame));
11452 ++redisplaying_p;
11453 specbind (Qinhibit_free_realized_faces, Qnil);
11454
11455 {
11456 Lisp_Object tail, frame;
11457
11458 FOR_EACH_FRAME (tail, frame)
11459 {
11460 struct frame *f = XFRAME (frame);
11461 f->already_hscrolled_p = 0;
11462 }
11463 }
11464
11465 retry:
11466 /* Remember the currently selected window. */
11467 sw = w;
11468
11469 if (!EQ (old_frame, selected_frame)
11470 && FRAME_LIVE_P (XFRAME (old_frame)))
11471 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11472 selected_frame and selected_window to be temporarily out-of-sync so
11473 when we come back here via `goto retry', we need to resync because we
11474 may need to run Elisp code (via prepare_menu_bars). */
11475 select_frame_for_redisplay (old_frame);
11476
11477 pending = 0;
11478 reconsider_clip_changes (w, current_buffer);
11479 last_escape_glyph_frame = NULL;
11480 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11481 last_glyphless_glyph_frame = NULL;
11482 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11483
11484 /* If new fonts have been loaded that make a glyph matrix adjustment
11485 necessary, do it. */
11486 if (fonts_changed_p)
11487 {
11488 adjust_glyphs (NULL);
11489 ++windows_or_buffers_changed;
11490 fonts_changed_p = 0;
11491 }
11492
11493 /* If face_change_count is non-zero, init_iterator will free all
11494 realized faces, which includes the faces referenced from current
11495 matrices. So, we can't reuse current matrices in this case. */
11496 if (face_change_count)
11497 ++windows_or_buffers_changed;
11498
11499 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11500 && FRAME_TTY (sf)->previous_frame != sf)
11501 {
11502 /* Since frames on a single ASCII terminal share the same
11503 display area, displaying a different frame means redisplay
11504 the whole thing. */
11505 windows_or_buffers_changed++;
11506 SET_FRAME_GARBAGED (sf);
11507 #ifndef DOS_NT
11508 set_tty_color_mode (FRAME_TTY (sf), sf);
11509 #endif
11510 FRAME_TTY (sf)->previous_frame = sf;
11511 }
11512
11513 /* Set the visible flags for all frames. Do this before checking
11514 for resized or garbaged frames; they want to know if their frames
11515 are visible. See the comment in frame.h for
11516 FRAME_SAMPLE_VISIBILITY. */
11517 {
11518 Lisp_Object tail, frame;
11519
11520 number_of_visible_frames = 0;
11521
11522 FOR_EACH_FRAME (tail, frame)
11523 {
11524 struct frame *f = XFRAME (frame);
11525
11526 FRAME_SAMPLE_VISIBILITY (f);
11527 if (FRAME_VISIBLE_P (f))
11528 ++number_of_visible_frames;
11529 clear_desired_matrices (f);
11530 }
11531 }
11532
11533 /* Notice any pending interrupt request to change frame size. */
11534 do_pending_window_change (1);
11535
11536 /* do_pending_window_change could change the selected_window due to
11537 frame resizing which makes the selected window too small. */
11538 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
11539 {
11540 sw = w;
11541 reconsider_clip_changes (w, current_buffer);
11542 }
11543
11544 /* Clear frames marked as garbaged. */
11545 if (frame_garbaged)
11546 clear_garbaged_frames ();
11547
11548 /* Build menubar and tool-bar items. */
11549 if (NILP (Vmemory_full))
11550 prepare_menu_bars ();
11551
11552 if (windows_or_buffers_changed)
11553 update_mode_lines++;
11554
11555 /* Detect case that we need to write or remove a star in the mode line. */
11556 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11557 {
11558 w->update_mode_line = Qt;
11559 if (buffer_shared > 1)
11560 update_mode_lines++;
11561 }
11562
11563 /* Avoid invocation of point motion hooks by `current_column' below. */
11564 count1 = SPECPDL_INDEX ();
11565 specbind (Qinhibit_point_motion_hooks, Qt);
11566
11567 /* If %c is in the mode line, update it if needed. */
11568 if (!NILP (w->column_number_displayed)
11569 /* This alternative quickly identifies a common case
11570 where no change is needed. */
11571 && !(PT == XFASTINT (w->last_point)
11572 && XFASTINT (w->last_modified) >= MODIFF
11573 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11574 && (XFASTINT (w->column_number_displayed) != current_column ()))
11575 w->update_mode_line = Qt;
11576
11577 unbind_to (count1, Qnil);
11578
11579 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11580
11581 /* The variable buffer_shared is set in redisplay_window and
11582 indicates that we redisplay a buffer in different windows. See
11583 there. */
11584 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11585 || cursor_type_changed);
11586
11587 /* If specs for an arrow have changed, do thorough redisplay
11588 to ensure we remove any arrow that should no longer exist. */
11589 if (overlay_arrows_changed_p ())
11590 consider_all_windows_p = windows_or_buffers_changed = 1;
11591
11592 /* Normally the message* functions will have already displayed and
11593 updated the echo area, but the frame may have been trashed, or
11594 the update may have been preempted, so display the echo area
11595 again here. Checking message_cleared_p captures the case that
11596 the echo area should be cleared. */
11597 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11598 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11599 || (message_cleared_p
11600 && minibuf_level == 0
11601 /* If the mini-window is currently selected, this means the
11602 echo-area doesn't show through. */
11603 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11604 {
11605 int window_height_changed_p = echo_area_display (0);
11606 must_finish = 1;
11607
11608 /* If we don't display the current message, don't clear the
11609 message_cleared_p flag, because, if we did, we wouldn't clear
11610 the echo area in the next redisplay which doesn't preserve
11611 the echo area. */
11612 if (!display_last_displayed_message_p)
11613 message_cleared_p = 0;
11614
11615 if (fonts_changed_p)
11616 goto retry;
11617 else if (window_height_changed_p)
11618 {
11619 consider_all_windows_p = 1;
11620 ++update_mode_lines;
11621 ++windows_or_buffers_changed;
11622
11623 /* If window configuration was changed, frames may have been
11624 marked garbaged. Clear them or we will experience
11625 surprises wrt scrolling. */
11626 if (frame_garbaged)
11627 clear_garbaged_frames ();
11628 }
11629 }
11630 else if (EQ (selected_window, minibuf_window)
11631 && (current_buffer->clip_changed
11632 || XFASTINT (w->last_modified) < MODIFF
11633 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11634 && resize_mini_window (w, 0))
11635 {
11636 /* Resized active mini-window to fit the size of what it is
11637 showing if its contents might have changed. */
11638 must_finish = 1;
11639 /* FIXME: this causes all frames to be updated, which seems unnecessary
11640 since only the current frame needs to be considered. This function needs
11641 to be rewritten with two variables, consider_all_windows and
11642 consider_all_frames. */
11643 consider_all_windows_p = 1;
11644 ++windows_or_buffers_changed;
11645 ++update_mode_lines;
11646
11647 /* If window configuration was changed, frames may have been
11648 marked garbaged. Clear them or we will experience
11649 surprises wrt scrolling. */
11650 if (frame_garbaged)
11651 clear_garbaged_frames ();
11652 }
11653
11654
11655 /* If showing the region, and mark has changed, we must redisplay
11656 the whole window. The assignment to this_line_start_pos prevents
11657 the optimization directly below this if-statement. */
11658 if (((!NILP (Vtransient_mark_mode)
11659 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11660 != !NILP (w->region_showing))
11661 || (!NILP (w->region_showing)
11662 && !EQ (w->region_showing,
11663 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
11664 CHARPOS (this_line_start_pos) = 0;
11665
11666 /* Optimize the case that only the line containing the cursor in the
11667 selected window has changed. Variables starting with this_ are
11668 set in display_line and record information about the line
11669 containing the cursor. */
11670 tlbufpos = this_line_start_pos;
11671 tlendpos = this_line_end_pos;
11672 if (!consider_all_windows_p
11673 && CHARPOS (tlbufpos) > 0
11674 && NILP (w->update_mode_line)
11675 && !current_buffer->clip_changed
11676 && !current_buffer->prevent_redisplay_optimizations_p
11677 && FRAME_VISIBLE_P (XFRAME (w->frame))
11678 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11679 /* Make sure recorded data applies to current buffer, etc. */
11680 && this_line_buffer == current_buffer
11681 && current_buffer == XBUFFER (w->buffer)
11682 && NILP (w->force_start)
11683 && NILP (w->optional_new_start)
11684 /* Point must be on the line that we have info recorded about. */
11685 && PT >= CHARPOS (tlbufpos)
11686 && PT <= Z - CHARPOS (tlendpos)
11687 /* All text outside that line, including its final newline,
11688 must be unchanged. */
11689 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11690 CHARPOS (tlendpos)))
11691 {
11692 if (CHARPOS (tlbufpos) > BEGV
11693 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11694 && (CHARPOS (tlbufpos) == ZV
11695 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11696 /* Former continuation line has disappeared by becoming empty. */
11697 goto cancel;
11698 else if (XFASTINT (w->last_modified) < MODIFF
11699 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11700 || MINI_WINDOW_P (w))
11701 {
11702 /* We have to handle the case of continuation around a
11703 wide-column character (see the comment in indent.c around
11704 line 1340).
11705
11706 For instance, in the following case:
11707
11708 -------- Insert --------
11709 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11710 J_I_ ==> J_I_ `^^' are cursors.
11711 ^^ ^^
11712 -------- --------
11713
11714 As we have to redraw the line above, we cannot use this
11715 optimization. */
11716
11717 struct it it;
11718 int line_height_before = this_line_pixel_height;
11719
11720 /* Note that start_display will handle the case that the
11721 line starting at tlbufpos is a continuation line. */
11722 start_display (&it, w, tlbufpos);
11723
11724 /* Implementation note: It this still necessary? */
11725 if (it.current_x != this_line_start_x)
11726 goto cancel;
11727
11728 TRACE ((stderr, "trying display optimization 1\n"));
11729 w->cursor.vpos = -1;
11730 overlay_arrow_seen = 0;
11731 it.vpos = this_line_vpos;
11732 it.current_y = this_line_y;
11733 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11734 display_line (&it);
11735
11736 /* If line contains point, is not continued,
11737 and ends at same distance from eob as before, we win. */
11738 if (w->cursor.vpos >= 0
11739 /* Line is not continued, otherwise this_line_start_pos
11740 would have been set to 0 in display_line. */
11741 && CHARPOS (this_line_start_pos)
11742 /* Line ends as before. */
11743 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11744 /* Line has same height as before. Otherwise other lines
11745 would have to be shifted up or down. */
11746 && this_line_pixel_height == line_height_before)
11747 {
11748 /* If this is not the window's last line, we must adjust
11749 the charstarts of the lines below. */
11750 if (it.current_y < it.last_visible_y)
11751 {
11752 struct glyph_row *row
11753 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11754 EMACS_INT delta, delta_bytes;
11755
11756 /* We used to distinguish between two cases here,
11757 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11758 when the line ends in a newline or the end of the
11759 buffer's accessible portion. But both cases did
11760 the same, so they were collapsed. */
11761 delta = (Z
11762 - CHARPOS (tlendpos)
11763 - MATRIX_ROW_START_CHARPOS (row));
11764 delta_bytes = (Z_BYTE
11765 - BYTEPOS (tlendpos)
11766 - MATRIX_ROW_START_BYTEPOS (row));
11767
11768 increment_matrix_positions (w->current_matrix,
11769 this_line_vpos + 1,
11770 w->current_matrix->nrows,
11771 delta, delta_bytes);
11772 }
11773
11774 /* If this row displays text now but previously didn't,
11775 or vice versa, w->window_end_vpos may have to be
11776 adjusted. */
11777 if ((it.glyph_row - 1)->displays_text_p)
11778 {
11779 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11780 XSETINT (w->window_end_vpos, this_line_vpos);
11781 }
11782 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11783 && this_line_vpos > 0)
11784 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11785 w->window_end_valid = Qnil;
11786
11787 /* Update hint: No need to try to scroll in update_window. */
11788 w->desired_matrix->no_scrolling_p = 1;
11789
11790 #if GLYPH_DEBUG
11791 *w->desired_matrix->method = 0;
11792 debug_method_add (w, "optimization 1");
11793 #endif
11794 #ifdef HAVE_WINDOW_SYSTEM
11795 update_window_fringes (w, 0);
11796 #endif
11797 goto update;
11798 }
11799 else
11800 goto cancel;
11801 }
11802 else if (/* Cursor position hasn't changed. */
11803 PT == XFASTINT (w->last_point)
11804 /* Make sure the cursor was last displayed
11805 in this window. Otherwise we have to reposition it. */
11806 && 0 <= w->cursor.vpos
11807 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11808 {
11809 if (!must_finish)
11810 {
11811 do_pending_window_change (1);
11812 /* If selected_window changed, redisplay again. */
11813 if (WINDOWP (selected_window)
11814 && (w = XWINDOW (selected_window)) != sw)
11815 goto retry;
11816
11817 /* We used to always goto end_of_redisplay here, but this
11818 isn't enough if we have a blinking cursor. */
11819 if (w->cursor_off_p == w->last_cursor_off_p)
11820 goto end_of_redisplay;
11821 }
11822 goto update;
11823 }
11824 /* If highlighting the region, or if the cursor is in the echo area,
11825 then we can't just move the cursor. */
11826 else if (! (!NILP (Vtransient_mark_mode)
11827 && !NILP (BVAR (current_buffer, mark_active)))
11828 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
11829 || highlight_nonselected_windows)
11830 && NILP (w->region_showing)
11831 && NILP (Vshow_trailing_whitespace)
11832 && !cursor_in_echo_area)
11833 {
11834 struct it it;
11835 struct glyph_row *row;
11836
11837 /* Skip from tlbufpos to PT and see where it is. Note that
11838 PT may be in invisible text. If so, we will end at the
11839 next visible position. */
11840 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11841 NULL, DEFAULT_FACE_ID);
11842 it.current_x = this_line_start_x;
11843 it.current_y = this_line_y;
11844 it.vpos = this_line_vpos;
11845
11846 /* The call to move_it_to stops in front of PT, but
11847 moves over before-strings. */
11848 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11849
11850 if (it.vpos == this_line_vpos
11851 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11852 row->enabled_p))
11853 {
11854 xassert (this_line_vpos == it.vpos);
11855 xassert (this_line_y == it.current_y);
11856 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11857 #if GLYPH_DEBUG
11858 *w->desired_matrix->method = 0;
11859 debug_method_add (w, "optimization 3");
11860 #endif
11861 goto update;
11862 }
11863 else
11864 goto cancel;
11865 }
11866
11867 cancel:
11868 /* Text changed drastically or point moved off of line. */
11869 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11870 }
11871
11872 CHARPOS (this_line_start_pos) = 0;
11873 consider_all_windows_p |= buffer_shared > 1;
11874 ++clear_face_cache_count;
11875 #ifdef HAVE_WINDOW_SYSTEM
11876 ++clear_image_cache_count;
11877 #endif
11878
11879 /* Build desired matrices, and update the display. If
11880 consider_all_windows_p is non-zero, do it for all windows on all
11881 frames. Otherwise do it for selected_window, only. */
11882
11883 if (consider_all_windows_p)
11884 {
11885 Lisp_Object tail, frame;
11886
11887 FOR_EACH_FRAME (tail, frame)
11888 XFRAME (frame)->updated_p = 0;
11889
11890 /* Recompute # windows showing selected buffer. This will be
11891 incremented each time such a window is displayed. */
11892 buffer_shared = 0;
11893
11894 FOR_EACH_FRAME (tail, frame)
11895 {
11896 struct frame *f = XFRAME (frame);
11897
11898 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11899 {
11900 if (! EQ (frame, selected_frame))
11901 /* Select the frame, for the sake of frame-local
11902 variables. */
11903 select_frame_for_redisplay (frame);
11904
11905 /* Mark all the scroll bars to be removed; we'll redeem
11906 the ones we want when we redisplay their windows. */
11907 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11908 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11909
11910 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11911 redisplay_windows (FRAME_ROOT_WINDOW (f));
11912
11913 /* The X error handler may have deleted that frame. */
11914 if (!FRAME_LIVE_P (f))
11915 continue;
11916
11917 /* Any scroll bars which redisplay_windows should have
11918 nuked should now go away. */
11919 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11920 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11921
11922 /* If fonts changed, display again. */
11923 /* ??? rms: I suspect it is a mistake to jump all the way
11924 back to retry here. It should just retry this frame. */
11925 if (fonts_changed_p)
11926 goto retry;
11927
11928 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11929 {
11930 /* See if we have to hscroll. */
11931 if (!f->already_hscrolled_p)
11932 {
11933 f->already_hscrolled_p = 1;
11934 if (hscroll_windows (f->root_window))
11935 goto retry;
11936 }
11937
11938 /* Prevent various kinds of signals during display
11939 update. stdio is not robust about handling
11940 signals, which can cause an apparent I/O
11941 error. */
11942 if (interrupt_input)
11943 unrequest_sigio ();
11944 STOP_POLLING;
11945
11946 /* Update the display. */
11947 set_window_update_flags (XWINDOW (f->root_window), 1);
11948 pending |= update_frame (f, 0, 0);
11949 f->updated_p = 1;
11950 }
11951 }
11952 }
11953
11954 if (!EQ (old_frame, selected_frame)
11955 && FRAME_LIVE_P (XFRAME (old_frame)))
11956 /* We played a bit fast-and-loose above and allowed selected_frame
11957 and selected_window to be temporarily out-of-sync but let's make
11958 sure this stays contained. */
11959 select_frame_for_redisplay (old_frame);
11960 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11961
11962 if (!pending)
11963 {
11964 /* Do the mark_window_display_accurate after all windows have
11965 been redisplayed because this call resets flags in buffers
11966 which are needed for proper redisplay. */
11967 FOR_EACH_FRAME (tail, frame)
11968 {
11969 struct frame *f = XFRAME (frame);
11970 if (f->updated_p)
11971 {
11972 mark_window_display_accurate (f->root_window, 1);
11973 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11974 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11975 }
11976 }
11977 }
11978 }
11979 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11980 {
11981 Lisp_Object mini_window;
11982 struct frame *mini_frame;
11983
11984 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11985 /* Use list_of_error, not Qerror, so that
11986 we catch only errors and don't run the debugger. */
11987 internal_condition_case_1 (redisplay_window_1, selected_window,
11988 list_of_error,
11989 redisplay_window_error);
11990
11991 /* Compare desired and current matrices, perform output. */
11992
11993 update:
11994 /* If fonts changed, display again. */
11995 if (fonts_changed_p)
11996 goto retry;
11997
11998 /* Prevent various kinds of signals during display update.
11999 stdio is not robust about handling signals,
12000 which can cause an apparent I/O error. */
12001 if (interrupt_input)
12002 unrequest_sigio ();
12003 STOP_POLLING;
12004
12005 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12006 {
12007 if (hscroll_windows (selected_window))
12008 goto retry;
12009
12010 XWINDOW (selected_window)->must_be_updated_p = 1;
12011 pending = update_frame (sf, 0, 0);
12012 }
12013
12014 /* We may have called echo_area_display at the top of this
12015 function. If the echo area is on another frame, that may
12016 have put text on a frame other than the selected one, so the
12017 above call to update_frame would not have caught it. Catch
12018 it here. */
12019 mini_window = FRAME_MINIBUF_WINDOW (sf);
12020 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12021
12022 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12023 {
12024 XWINDOW (mini_window)->must_be_updated_p = 1;
12025 pending |= update_frame (mini_frame, 0, 0);
12026 if (!pending && hscroll_windows (mini_window))
12027 goto retry;
12028 }
12029 }
12030
12031 /* If display was paused because of pending input, make sure we do a
12032 thorough update the next time. */
12033 if (pending)
12034 {
12035 /* Prevent the optimization at the beginning of
12036 redisplay_internal that tries a single-line update of the
12037 line containing the cursor in the selected window. */
12038 CHARPOS (this_line_start_pos) = 0;
12039
12040 /* Let the overlay arrow be updated the next time. */
12041 update_overlay_arrows (0);
12042
12043 /* If we pause after scrolling, some rows in the current
12044 matrices of some windows are not valid. */
12045 if (!WINDOW_FULL_WIDTH_P (w)
12046 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12047 update_mode_lines = 1;
12048 }
12049 else
12050 {
12051 if (!consider_all_windows_p)
12052 {
12053 /* This has already been done above if
12054 consider_all_windows_p is set. */
12055 mark_window_display_accurate_1 (w, 1);
12056
12057 /* Say overlay arrows are up to date. */
12058 update_overlay_arrows (1);
12059
12060 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12061 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12062 }
12063
12064 update_mode_lines = 0;
12065 windows_or_buffers_changed = 0;
12066 cursor_type_changed = 0;
12067 }
12068
12069 /* Start SIGIO interrupts coming again. Having them off during the
12070 code above makes it less likely one will discard output, but not
12071 impossible, since there might be stuff in the system buffer here.
12072 But it is much hairier to try to do anything about that. */
12073 if (interrupt_input)
12074 request_sigio ();
12075 RESUME_POLLING;
12076
12077 /* If a frame has become visible which was not before, redisplay
12078 again, so that we display it. Expose events for such a frame
12079 (which it gets when becoming visible) don't call the parts of
12080 redisplay constructing glyphs, so simply exposing a frame won't
12081 display anything in this case. So, we have to display these
12082 frames here explicitly. */
12083 if (!pending)
12084 {
12085 Lisp_Object tail, frame;
12086 int new_count = 0;
12087
12088 FOR_EACH_FRAME (tail, frame)
12089 {
12090 int this_is_visible = 0;
12091
12092 if (XFRAME (frame)->visible)
12093 this_is_visible = 1;
12094 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12095 if (XFRAME (frame)->visible)
12096 this_is_visible = 1;
12097
12098 if (this_is_visible)
12099 new_count++;
12100 }
12101
12102 if (new_count != number_of_visible_frames)
12103 windows_or_buffers_changed++;
12104 }
12105
12106 /* Change frame size now if a change is pending. */
12107 do_pending_window_change (1);
12108
12109 /* If we just did a pending size change, or have additional
12110 visible frames, or selected_window changed, redisplay again. */
12111 if ((windows_or_buffers_changed && !pending)
12112 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12113 goto retry;
12114
12115 /* Clear the face and image caches.
12116
12117 We used to do this only if consider_all_windows_p. But the cache
12118 needs to be cleared if a timer creates images in the current
12119 buffer (e.g. the test case in Bug#6230). */
12120
12121 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12122 {
12123 clear_face_cache (0);
12124 clear_face_cache_count = 0;
12125 }
12126
12127 #ifdef HAVE_WINDOW_SYSTEM
12128 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12129 {
12130 clear_image_caches (Qnil);
12131 clear_image_cache_count = 0;
12132 }
12133 #endif /* HAVE_WINDOW_SYSTEM */
12134
12135 end_of_redisplay:
12136 unbind_to (count, Qnil);
12137 RESUME_POLLING;
12138 }
12139
12140
12141 /* Redisplay, but leave alone any recent echo area message unless
12142 another message has been requested in its place.
12143
12144 This is useful in situations where you need to redisplay but no
12145 user action has occurred, making it inappropriate for the message
12146 area to be cleared. See tracking_off and
12147 wait_reading_process_output for examples of these situations.
12148
12149 FROM_WHERE is an integer saying from where this function was
12150 called. This is useful for debugging. */
12151
12152 void
12153 redisplay_preserve_echo_area (int from_where)
12154 {
12155 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12156
12157 if (!NILP (echo_area_buffer[1]))
12158 {
12159 /* We have a previously displayed message, but no current
12160 message. Redisplay the previous message. */
12161 display_last_displayed_message_p = 1;
12162 redisplay_internal ();
12163 display_last_displayed_message_p = 0;
12164 }
12165 else
12166 redisplay_internal ();
12167
12168 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12169 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12170 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12171 }
12172
12173
12174 /* Function registered with record_unwind_protect in
12175 redisplay_internal. Reset redisplaying_p to the value it had
12176 before redisplay_internal was called, and clear
12177 prevent_freeing_realized_faces_p. It also selects the previously
12178 selected frame, unless it has been deleted (by an X connection
12179 failure during redisplay, for example). */
12180
12181 static Lisp_Object
12182 unwind_redisplay (Lisp_Object val)
12183 {
12184 Lisp_Object old_redisplaying_p, old_frame;
12185
12186 old_redisplaying_p = XCAR (val);
12187 redisplaying_p = XFASTINT (old_redisplaying_p);
12188 old_frame = XCDR (val);
12189 if (! EQ (old_frame, selected_frame)
12190 && FRAME_LIVE_P (XFRAME (old_frame)))
12191 select_frame_for_redisplay (old_frame);
12192 return Qnil;
12193 }
12194
12195
12196 /* Mark the display of window W as accurate or inaccurate. If
12197 ACCURATE_P is non-zero mark display of W as accurate. If
12198 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12199 redisplay_internal is called. */
12200
12201 static void
12202 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12203 {
12204 if (BUFFERP (w->buffer))
12205 {
12206 struct buffer *b = XBUFFER (w->buffer);
12207
12208 w->last_modified
12209 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12210 w->last_overlay_modified
12211 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12212 w->last_had_star
12213 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12214
12215 if (accurate_p)
12216 {
12217 b->clip_changed = 0;
12218 b->prevent_redisplay_optimizations_p = 0;
12219
12220 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12221 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12222 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12223 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12224
12225 w->current_matrix->buffer = b;
12226 w->current_matrix->begv = BUF_BEGV (b);
12227 w->current_matrix->zv = BUF_ZV (b);
12228
12229 w->last_cursor = w->cursor;
12230 w->last_cursor_off_p = w->cursor_off_p;
12231
12232 if (w == XWINDOW (selected_window))
12233 w->last_point = make_number (BUF_PT (b));
12234 else
12235 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12236 }
12237 }
12238
12239 if (accurate_p)
12240 {
12241 w->window_end_valid = w->buffer;
12242 w->update_mode_line = Qnil;
12243 }
12244 }
12245
12246
12247 /* Mark the display of windows in the window tree rooted at WINDOW as
12248 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12249 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12250 be redisplayed the next time redisplay_internal is called. */
12251
12252 void
12253 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12254 {
12255 struct window *w;
12256
12257 for (; !NILP (window); window = w->next)
12258 {
12259 w = XWINDOW (window);
12260 mark_window_display_accurate_1 (w, accurate_p);
12261
12262 if (!NILP (w->vchild))
12263 mark_window_display_accurate (w->vchild, accurate_p);
12264 if (!NILP (w->hchild))
12265 mark_window_display_accurate (w->hchild, accurate_p);
12266 }
12267
12268 if (accurate_p)
12269 {
12270 update_overlay_arrows (1);
12271 }
12272 else
12273 {
12274 /* Force a thorough redisplay the next time by setting
12275 last_arrow_position and last_arrow_string to t, which is
12276 unequal to any useful value of Voverlay_arrow_... */
12277 update_overlay_arrows (-1);
12278 }
12279 }
12280
12281
12282 /* Return value in display table DP (Lisp_Char_Table *) for character
12283 C. Since a display table doesn't have any parent, we don't have to
12284 follow parent. Do not call this function directly but use the
12285 macro DISP_CHAR_VECTOR. */
12286
12287 Lisp_Object
12288 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12289 {
12290 Lisp_Object val;
12291
12292 if (ASCII_CHAR_P (c))
12293 {
12294 val = dp->ascii;
12295 if (SUB_CHAR_TABLE_P (val))
12296 val = XSUB_CHAR_TABLE (val)->contents[c];
12297 }
12298 else
12299 {
12300 Lisp_Object table;
12301
12302 XSETCHAR_TABLE (table, dp);
12303 val = char_table_ref (table, c);
12304 }
12305 if (NILP (val))
12306 val = dp->defalt;
12307 return val;
12308 }
12309
12310
12311 \f
12312 /***********************************************************************
12313 Window Redisplay
12314 ***********************************************************************/
12315
12316 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12317
12318 static void
12319 redisplay_windows (Lisp_Object window)
12320 {
12321 while (!NILP (window))
12322 {
12323 struct window *w = XWINDOW (window);
12324
12325 if (!NILP (w->hchild))
12326 redisplay_windows (w->hchild);
12327 else if (!NILP (w->vchild))
12328 redisplay_windows (w->vchild);
12329 else if (!NILP (w->buffer))
12330 {
12331 displayed_buffer = XBUFFER (w->buffer);
12332 /* Use list_of_error, not Qerror, so that
12333 we catch only errors and don't run the debugger. */
12334 internal_condition_case_1 (redisplay_window_0, window,
12335 list_of_error,
12336 redisplay_window_error);
12337 }
12338
12339 window = w->next;
12340 }
12341 }
12342
12343 static Lisp_Object
12344 redisplay_window_error (Lisp_Object ignore)
12345 {
12346 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12347 return Qnil;
12348 }
12349
12350 static Lisp_Object
12351 redisplay_window_0 (Lisp_Object window)
12352 {
12353 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12354 redisplay_window (window, 0);
12355 return Qnil;
12356 }
12357
12358 static Lisp_Object
12359 redisplay_window_1 (Lisp_Object window)
12360 {
12361 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12362 redisplay_window (window, 1);
12363 return Qnil;
12364 }
12365 \f
12366
12367 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12368 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12369 which positions recorded in ROW differ from current buffer
12370 positions.
12371
12372 Return 0 if cursor is not on this row, 1 otherwise. */
12373
12374 int
12375 set_cursor_from_row (struct window *w, struct glyph_row *row,
12376 struct glyph_matrix *matrix,
12377 EMACS_INT delta, EMACS_INT delta_bytes,
12378 int dy, int dvpos)
12379 {
12380 struct glyph *glyph = row->glyphs[TEXT_AREA];
12381 struct glyph *end = glyph + row->used[TEXT_AREA];
12382 struct glyph *cursor = NULL;
12383 /* The last known character position in row. */
12384 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12385 int x = row->x;
12386 EMACS_INT pt_old = PT - delta;
12387 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12388 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12389 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12390 /* A glyph beyond the edge of TEXT_AREA which we should never
12391 touch. */
12392 struct glyph *glyphs_end = end;
12393 /* Non-zero means we've found a match for cursor position, but that
12394 glyph has the avoid_cursor_p flag set. */
12395 int match_with_avoid_cursor = 0;
12396 /* Non-zero means we've seen at least one glyph that came from a
12397 display string. */
12398 int string_seen = 0;
12399 /* Largest and smalles buffer positions seen so far during scan of
12400 glyph row. */
12401 EMACS_INT bpos_max = pos_before;
12402 EMACS_INT bpos_min = pos_after;
12403 /* Last buffer position covered by an overlay string with an integer
12404 `cursor' property. */
12405 EMACS_INT bpos_covered = 0;
12406
12407 /* Skip over glyphs not having an object at the start and the end of
12408 the row. These are special glyphs like truncation marks on
12409 terminal frames. */
12410 if (row->displays_text_p)
12411 {
12412 if (!row->reversed_p)
12413 {
12414 while (glyph < end
12415 && INTEGERP (glyph->object)
12416 && glyph->charpos < 0)
12417 {
12418 x += glyph->pixel_width;
12419 ++glyph;
12420 }
12421 while (end > glyph
12422 && INTEGERP ((end - 1)->object)
12423 /* CHARPOS is zero for blanks and stretch glyphs
12424 inserted by extend_face_to_end_of_line. */
12425 && (end - 1)->charpos <= 0)
12426 --end;
12427 glyph_before = glyph - 1;
12428 glyph_after = end;
12429 }
12430 else
12431 {
12432 struct glyph *g;
12433
12434 /* If the glyph row is reversed, we need to process it from back
12435 to front, so swap the edge pointers. */
12436 glyphs_end = end = glyph - 1;
12437 glyph += row->used[TEXT_AREA] - 1;
12438
12439 while (glyph > end + 1
12440 && INTEGERP (glyph->object)
12441 && glyph->charpos < 0)
12442 {
12443 --glyph;
12444 x -= glyph->pixel_width;
12445 }
12446 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12447 --glyph;
12448 /* By default, in reversed rows we put the cursor on the
12449 rightmost (first in the reading order) glyph. */
12450 for (g = end + 1; g < glyph; g++)
12451 x += g->pixel_width;
12452 while (end < glyph
12453 && INTEGERP ((end + 1)->object)
12454 && (end + 1)->charpos <= 0)
12455 ++end;
12456 glyph_before = glyph + 1;
12457 glyph_after = end;
12458 }
12459 }
12460 else if (row->reversed_p)
12461 {
12462 /* In R2L rows that don't display text, put the cursor on the
12463 rightmost glyph. Case in point: an empty last line that is
12464 part of an R2L paragraph. */
12465 cursor = end - 1;
12466 /* Avoid placing the cursor on the last glyph of the row, where
12467 on terminal frames we hold the vertical border between
12468 adjacent windows. */
12469 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12470 && !WINDOW_RIGHTMOST_P (w)
12471 && cursor == row->glyphs[LAST_AREA] - 1)
12472 cursor--;
12473 x = -1; /* will be computed below, at label compute_x */
12474 }
12475
12476 /* Step 1: Try to find the glyph whose character position
12477 corresponds to point. If that's not possible, find 2 glyphs
12478 whose character positions are the closest to point, one before
12479 point, the other after it. */
12480 if (!row->reversed_p)
12481 while (/* not marched to end of glyph row */
12482 glyph < end
12483 /* glyph was not inserted by redisplay for internal purposes */
12484 && !INTEGERP (glyph->object))
12485 {
12486 if (BUFFERP (glyph->object))
12487 {
12488 EMACS_INT dpos = glyph->charpos - pt_old;
12489
12490 if (glyph->charpos > bpos_max)
12491 bpos_max = glyph->charpos;
12492 if (glyph->charpos < bpos_min)
12493 bpos_min = glyph->charpos;
12494 if (!glyph->avoid_cursor_p)
12495 {
12496 /* If we hit point, we've found the glyph on which to
12497 display the cursor. */
12498 if (dpos == 0)
12499 {
12500 match_with_avoid_cursor = 0;
12501 break;
12502 }
12503 /* See if we've found a better approximation to
12504 POS_BEFORE or to POS_AFTER. Note that we want the
12505 first (leftmost) glyph of all those that are the
12506 closest from below, and the last (rightmost) of all
12507 those from above. */
12508 if (0 > dpos && dpos > pos_before - pt_old)
12509 {
12510 pos_before = glyph->charpos;
12511 glyph_before = glyph;
12512 }
12513 else if (0 < dpos && dpos <= pos_after - pt_old)
12514 {
12515 pos_after = glyph->charpos;
12516 glyph_after = glyph;
12517 }
12518 }
12519 else if (dpos == 0)
12520 match_with_avoid_cursor = 1;
12521 }
12522 else if (STRINGP (glyph->object))
12523 {
12524 Lisp_Object chprop;
12525 EMACS_INT glyph_pos = glyph->charpos;
12526
12527 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12528 glyph->object);
12529 if (INTEGERP (chprop))
12530 {
12531 bpos_covered = bpos_max + XINT (chprop);
12532 /* If the `cursor' property covers buffer positions up
12533 to and including point, we should display cursor on
12534 this glyph. Note that overlays and text properties
12535 with string values stop bidi reordering, so every
12536 buffer position to the left of the string is always
12537 smaller than any position to the right of the
12538 string. Therefore, if a `cursor' property on one
12539 of the string's characters has an integer value, we
12540 will break out of the loop below _before_ we get to
12541 the position match above. IOW, integer values of
12542 the `cursor' property override the "exact match for
12543 point" strategy of positioning the cursor. */
12544 /* Implementation note: bpos_max == pt_old when, e.g.,
12545 we are in an empty line, where bpos_max is set to
12546 MATRIX_ROW_START_CHARPOS, see above. */
12547 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12548 {
12549 cursor = glyph;
12550 break;
12551 }
12552 }
12553
12554 string_seen = 1;
12555 }
12556 x += glyph->pixel_width;
12557 ++glyph;
12558 }
12559 else if (glyph > end) /* row is reversed */
12560 while (!INTEGERP (glyph->object))
12561 {
12562 if (BUFFERP (glyph->object))
12563 {
12564 EMACS_INT dpos = glyph->charpos - pt_old;
12565
12566 if (glyph->charpos > bpos_max)
12567 bpos_max = glyph->charpos;
12568 if (glyph->charpos < bpos_min)
12569 bpos_min = glyph->charpos;
12570 if (!glyph->avoid_cursor_p)
12571 {
12572 if (dpos == 0)
12573 {
12574 match_with_avoid_cursor = 0;
12575 break;
12576 }
12577 if (0 > dpos && dpos > pos_before - pt_old)
12578 {
12579 pos_before = glyph->charpos;
12580 glyph_before = glyph;
12581 }
12582 else if (0 < dpos && dpos <= pos_after - pt_old)
12583 {
12584 pos_after = glyph->charpos;
12585 glyph_after = glyph;
12586 }
12587 }
12588 else if (dpos == 0)
12589 match_with_avoid_cursor = 1;
12590 }
12591 else if (STRINGP (glyph->object))
12592 {
12593 Lisp_Object chprop;
12594 EMACS_INT glyph_pos = glyph->charpos;
12595
12596 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12597 glyph->object);
12598 if (INTEGERP (chprop))
12599 {
12600 bpos_covered = bpos_max + XINT (chprop);
12601 /* If the `cursor' property covers buffer positions up
12602 to and including point, we should display cursor on
12603 this glyph. */
12604 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12605 {
12606 cursor = glyph;
12607 break;
12608 }
12609 }
12610 string_seen = 1;
12611 }
12612 --glyph;
12613 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12614 {
12615 x--; /* can't use any pixel_width */
12616 break;
12617 }
12618 x -= glyph->pixel_width;
12619 }
12620
12621 /* Step 2: If we didn't find an exact match for point, we need to
12622 look for a proper place to put the cursor among glyphs between
12623 GLYPH_BEFORE and GLYPH_AFTER. */
12624 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12625 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12626 && bpos_covered < pt_old)
12627 {
12628 /* An empty line has a single glyph whose OBJECT is zero and
12629 whose CHARPOS is the position of a newline on that line.
12630 Note that on a TTY, there are more glyphs after that, which
12631 were produced by extend_face_to_end_of_line, but their
12632 CHARPOS is zero or negative. */
12633 int empty_line_p =
12634 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12635 && INTEGERP (glyph->object) && glyph->charpos > 0;
12636
12637 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12638 {
12639 EMACS_INT ellipsis_pos;
12640
12641 /* Scan back over the ellipsis glyphs. */
12642 if (!row->reversed_p)
12643 {
12644 ellipsis_pos = (glyph - 1)->charpos;
12645 while (glyph > row->glyphs[TEXT_AREA]
12646 && (glyph - 1)->charpos == ellipsis_pos)
12647 glyph--, x -= glyph->pixel_width;
12648 /* That loop always goes one position too far, including
12649 the glyph before the ellipsis. So scan forward over
12650 that one. */
12651 x += glyph->pixel_width;
12652 glyph++;
12653 }
12654 else /* row is reversed */
12655 {
12656 ellipsis_pos = (glyph + 1)->charpos;
12657 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12658 && (glyph + 1)->charpos == ellipsis_pos)
12659 glyph++, x += glyph->pixel_width;
12660 x -= glyph->pixel_width;
12661 glyph--;
12662 }
12663 }
12664 else if (match_with_avoid_cursor
12665 /* A truncated row may not include PT among its
12666 character positions. Setting the cursor inside the
12667 scroll margin will trigger recalculation of hscroll
12668 in hscroll_window_tree. */
12669 || (row->truncated_on_left_p && pt_old < bpos_min)
12670 || (row->truncated_on_right_p && pt_old > bpos_max)
12671 /* Zero-width characters produce no glyphs. */
12672 || (!string_seen
12673 && !empty_line_p
12674 && (row->reversed_p
12675 ? glyph_after > glyphs_end
12676 : glyph_after < glyphs_end)))
12677 {
12678 cursor = glyph_after;
12679 x = -1;
12680 }
12681 else if (string_seen)
12682 {
12683 int incr = row->reversed_p ? -1 : +1;
12684
12685 /* Need to find the glyph that came out of a string which is
12686 present at point. That glyph is somewhere between
12687 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12688 positioned between POS_BEFORE and POS_AFTER in the
12689 buffer. */
12690 struct glyph *stop = glyph_after;
12691 EMACS_INT pos = pos_before;
12692
12693 x = -1;
12694 for (glyph = glyph_before + incr;
12695 row->reversed_p ? glyph > stop : glyph < stop; )
12696 {
12697
12698 /* Any glyphs that come from the buffer are here because
12699 of bidi reordering. Skip them, and only pay
12700 attention to glyphs that came from some string. */
12701 if (STRINGP (glyph->object))
12702 {
12703 Lisp_Object str;
12704 EMACS_INT tem;
12705
12706 str = glyph->object;
12707 tem = string_buffer_position_lim (str, pos, pos_after, 0);
12708 if (tem == 0 /* from overlay */
12709 || pos <= tem)
12710 {
12711 /* If the string from which this glyph came is
12712 found in the buffer at point, then we've
12713 found the glyph we've been looking for. If
12714 it comes from an overlay (tem == 0), and it
12715 has the `cursor' property on one of its
12716 glyphs, record that glyph as a candidate for
12717 displaying the cursor. (As in the
12718 unidirectional version, we will display the
12719 cursor on the last candidate we find.) */
12720 if (tem == 0 || tem == pt_old)
12721 {
12722 /* The glyphs from this string could have
12723 been reordered. Find the one with the
12724 smallest string position. Or there could
12725 be a character in the string with the
12726 `cursor' property, which means display
12727 cursor on that character's glyph. */
12728 EMACS_INT strpos = glyph->charpos;
12729
12730 if (tem)
12731 cursor = glyph;
12732 for ( ;
12733 (row->reversed_p ? glyph > stop : glyph < stop)
12734 && EQ (glyph->object, str);
12735 glyph += incr)
12736 {
12737 Lisp_Object cprop;
12738 EMACS_INT gpos = glyph->charpos;
12739
12740 cprop = Fget_char_property (make_number (gpos),
12741 Qcursor,
12742 glyph->object);
12743 if (!NILP (cprop))
12744 {
12745 cursor = glyph;
12746 break;
12747 }
12748 if (tem && glyph->charpos < strpos)
12749 {
12750 strpos = glyph->charpos;
12751 cursor = glyph;
12752 }
12753 }
12754
12755 if (tem == pt_old)
12756 goto compute_x;
12757 }
12758 if (tem)
12759 pos = tem + 1; /* don't find previous instances */
12760 }
12761 /* This string is not what we want; skip all of the
12762 glyphs that came from it. */
12763 while ((row->reversed_p ? glyph > stop : glyph < stop)
12764 && EQ (glyph->object, str))
12765 glyph += incr;
12766 }
12767 else
12768 glyph += incr;
12769 }
12770
12771 /* If we reached the end of the line, and END was from a string,
12772 the cursor is not on this line. */
12773 if (cursor == NULL
12774 && (row->reversed_p ? glyph <= end : glyph >= end)
12775 && STRINGP (end->object)
12776 && row->continued_p)
12777 return 0;
12778 }
12779 }
12780
12781 compute_x:
12782 if (cursor != NULL)
12783 glyph = cursor;
12784 if (x < 0)
12785 {
12786 struct glyph *g;
12787
12788 /* Need to compute x that corresponds to GLYPH. */
12789 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12790 {
12791 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12792 abort ();
12793 x += g->pixel_width;
12794 }
12795 }
12796
12797 /* ROW could be part of a continued line, which, under bidi
12798 reordering, might have other rows whose start and end charpos
12799 occlude point. Only set w->cursor if we found a better
12800 approximation to the cursor position than we have from previously
12801 examined candidate rows belonging to the same continued line. */
12802 if (/* we already have a candidate row */
12803 w->cursor.vpos >= 0
12804 /* that candidate is not the row we are processing */
12805 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12806 /* the row we are processing is part of a continued line */
12807 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
12808 /* Make sure cursor.vpos specifies a row whose start and end
12809 charpos occlude point. This is because some callers of this
12810 function leave cursor.vpos at the row where the cursor was
12811 displayed during the last redisplay cycle. */
12812 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12813 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12814 {
12815 struct glyph *g1 =
12816 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12817
12818 /* Don't consider glyphs that are outside TEXT_AREA. */
12819 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
12820 return 0;
12821 /* Keep the candidate whose buffer position is the closest to
12822 point. */
12823 if (/* previous candidate is a glyph in TEXT_AREA of that row */
12824 w->cursor.hpos >= 0
12825 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
12826 && BUFFERP (g1->object)
12827 && (g1->charpos == pt_old /* an exact match always wins */
12828 || (BUFFERP (glyph->object)
12829 && eabs (g1->charpos - pt_old)
12830 < eabs (glyph->charpos - pt_old))))
12831 return 0;
12832 /* If this candidate gives an exact match, use that. */
12833 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12834 /* Otherwise, keep the candidate that comes from a row
12835 spanning less buffer positions. This may win when one or
12836 both candidate positions are on glyphs that came from
12837 display strings, for which we cannot compare buffer
12838 positions. */
12839 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12840 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12841 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12842 return 0;
12843 }
12844 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12845 w->cursor.x = x;
12846 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12847 w->cursor.y = row->y + dy;
12848
12849 if (w == XWINDOW (selected_window))
12850 {
12851 if (!row->continued_p
12852 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12853 && row->x == 0)
12854 {
12855 this_line_buffer = XBUFFER (w->buffer);
12856
12857 CHARPOS (this_line_start_pos)
12858 = MATRIX_ROW_START_CHARPOS (row) + delta;
12859 BYTEPOS (this_line_start_pos)
12860 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12861
12862 CHARPOS (this_line_end_pos)
12863 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12864 BYTEPOS (this_line_end_pos)
12865 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12866
12867 this_line_y = w->cursor.y;
12868 this_line_pixel_height = row->height;
12869 this_line_vpos = w->cursor.vpos;
12870 this_line_start_x = row->x;
12871 }
12872 else
12873 CHARPOS (this_line_start_pos) = 0;
12874 }
12875
12876 return 1;
12877 }
12878
12879
12880 /* Run window scroll functions, if any, for WINDOW with new window
12881 start STARTP. Sets the window start of WINDOW to that position.
12882
12883 We assume that the window's buffer is really current. */
12884
12885 static INLINE struct text_pos
12886 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
12887 {
12888 struct window *w = XWINDOW (window);
12889 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12890
12891 if (current_buffer != XBUFFER (w->buffer))
12892 abort ();
12893
12894 if (!NILP (Vwindow_scroll_functions))
12895 {
12896 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12897 make_number (CHARPOS (startp)));
12898 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12899 /* In case the hook functions switch buffers. */
12900 if (current_buffer != XBUFFER (w->buffer))
12901 set_buffer_internal_1 (XBUFFER (w->buffer));
12902 }
12903
12904 return startp;
12905 }
12906
12907
12908 /* Make sure the line containing the cursor is fully visible.
12909 A value of 1 means there is nothing to be done.
12910 (Either the line is fully visible, or it cannot be made so,
12911 or we cannot tell.)
12912
12913 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12914 is higher than window.
12915
12916 A value of 0 means the caller should do scrolling
12917 as if point had gone off the screen. */
12918
12919 static int
12920 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
12921 {
12922 struct glyph_matrix *matrix;
12923 struct glyph_row *row;
12924 int window_height;
12925
12926 if (!make_cursor_line_fully_visible_p)
12927 return 1;
12928
12929 /* It's not always possible to find the cursor, e.g, when a window
12930 is full of overlay strings. Don't do anything in that case. */
12931 if (w->cursor.vpos < 0)
12932 return 1;
12933
12934 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12935 row = MATRIX_ROW (matrix, w->cursor.vpos);
12936
12937 /* If the cursor row is not partially visible, there's nothing to do. */
12938 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12939 return 1;
12940
12941 /* If the row the cursor is in is taller than the window's height,
12942 it's not clear what to do, so do nothing. */
12943 window_height = window_box_height (w);
12944 if (row->height >= window_height)
12945 {
12946 if (!force_p || MINI_WINDOW_P (w)
12947 || w->vscroll || w->cursor.vpos == 0)
12948 return 1;
12949 }
12950 return 0;
12951 }
12952
12953
12954 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12955 non-zero means only WINDOW is redisplayed in redisplay_internal.
12956 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
12957 in redisplay_window to bring a partially visible line into view in
12958 the case that only the cursor has moved.
12959
12960 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12961 last screen line's vertical height extends past the end of the screen.
12962
12963 Value is
12964
12965 1 if scrolling succeeded
12966
12967 0 if scrolling didn't find point.
12968
12969 -1 if new fonts have been loaded so that we must interrupt
12970 redisplay, adjust glyph matrices, and try again. */
12971
12972 enum
12973 {
12974 SCROLLING_SUCCESS,
12975 SCROLLING_FAILED,
12976 SCROLLING_NEED_LARGER_MATRICES
12977 };
12978
12979 /* If scroll-conservatively is more than this, never recenter.
12980
12981 If you change this, don't forget to update the doc string of
12982 `scroll-conservatively' and the Emacs manual. */
12983 #define SCROLL_LIMIT 100
12984
12985 static int
12986 try_scrolling (Lisp_Object window, int just_this_one_p,
12987 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
12988 int temp_scroll_step, int last_line_misfit)
12989 {
12990 struct window *w = XWINDOW (window);
12991 struct frame *f = XFRAME (w->frame);
12992 struct text_pos pos, startp;
12993 struct it it;
12994 int this_scroll_margin, scroll_max, rc, height;
12995 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12996 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12997 Lisp_Object aggressive;
12998 /* We will never try scrolling more than this number of lines. */
12999 int scroll_limit = SCROLL_LIMIT;
13000
13001 #if GLYPH_DEBUG
13002 debug_method_add (w, "try_scrolling");
13003 #endif
13004
13005 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13006
13007 /* Compute scroll margin height in pixels. We scroll when point is
13008 within this distance from the top or bottom of the window. */
13009 if (scroll_margin > 0)
13010 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13011 * FRAME_LINE_HEIGHT (f);
13012 else
13013 this_scroll_margin = 0;
13014
13015 /* Force arg_scroll_conservatively to have a reasonable value, to
13016 avoid scrolling too far away with slow move_it_* functions. Note
13017 that the user can supply scroll-conservatively equal to
13018 `most-positive-fixnum', which can be larger than INT_MAX. */
13019 if (arg_scroll_conservatively > scroll_limit)
13020 {
13021 arg_scroll_conservatively = scroll_limit + 1;
13022 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13023 }
13024 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13025 /* Compute how much we should try to scroll maximally to bring
13026 point into view. */
13027 scroll_max = (max (scroll_step,
13028 max (arg_scroll_conservatively, temp_scroll_step))
13029 * FRAME_LINE_HEIGHT (f));
13030 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13031 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13032 /* We're trying to scroll because of aggressive scrolling but no
13033 scroll_step is set. Choose an arbitrary one. */
13034 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13035 else
13036 scroll_max = 0;
13037
13038 too_near_end:
13039
13040 /* Decide whether to scroll down. */
13041 if (PT > CHARPOS (startp))
13042 {
13043 int scroll_margin_y;
13044
13045 /* Compute the pixel ypos of the scroll margin, then move it to
13046 either that ypos or PT, whichever comes first. */
13047 start_display (&it, w, startp);
13048 scroll_margin_y = it.last_visible_y - this_scroll_margin
13049 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13050 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13051 (MOVE_TO_POS | MOVE_TO_Y));
13052
13053 if (PT > CHARPOS (it.current.pos))
13054 {
13055 int y0 = line_bottom_y (&it);
13056 /* Compute how many pixels below window bottom to stop searching
13057 for PT. This avoids costly search for PT that is far away if
13058 the user limited scrolling by a small number of lines, but
13059 always finds PT if scroll_conservatively is set to a large
13060 number, such as most-positive-fixnum. */
13061 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13062 int y_to_move = it.last_visible_y + slack;
13063
13064 /* Compute the distance from the scroll margin to PT or to
13065 the scroll limit, whichever comes first. This should
13066 include the height of the cursor line, to make that line
13067 fully visible. */
13068 move_it_to (&it, PT, -1, y_to_move,
13069 -1, MOVE_TO_POS | MOVE_TO_Y);
13070 dy = line_bottom_y (&it) - y0;
13071
13072 if (dy > scroll_max)
13073 return SCROLLING_FAILED;
13074
13075 scroll_down_p = 1;
13076 }
13077 }
13078
13079 if (scroll_down_p)
13080 {
13081 /* Point is in or below the bottom scroll margin, so move the
13082 window start down. If scrolling conservatively, move it just
13083 enough down to make point visible. If scroll_step is set,
13084 move it down by scroll_step. */
13085 if (arg_scroll_conservatively)
13086 amount_to_scroll
13087 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13088 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13089 else if (scroll_step || temp_scroll_step)
13090 amount_to_scroll = scroll_max;
13091 else
13092 {
13093 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13094 height = WINDOW_BOX_TEXT_HEIGHT (w);
13095 if (NUMBERP (aggressive))
13096 {
13097 double float_amount = XFLOATINT (aggressive) * height;
13098 amount_to_scroll = float_amount;
13099 if (amount_to_scroll == 0 && float_amount > 0)
13100 amount_to_scroll = 1;
13101 /* Don't let point enter the scroll margin near top of
13102 the window. */
13103 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13104 amount_to_scroll = height - 2*this_scroll_margin + dy;
13105 }
13106 }
13107
13108 if (amount_to_scroll <= 0)
13109 return SCROLLING_FAILED;
13110
13111 start_display (&it, w, startp);
13112 if (arg_scroll_conservatively <= scroll_limit)
13113 move_it_vertically (&it, amount_to_scroll);
13114 else
13115 {
13116 /* Extra precision for users who set scroll-conservatively
13117 to a large number: make sure the amount we scroll
13118 the window start is never less than amount_to_scroll,
13119 which was computed as distance from window bottom to
13120 point. This matters when lines at window top and lines
13121 below window bottom have different height. */
13122 struct it it1 = it;
13123 /* We use a temporary it1 because line_bottom_y can modify
13124 its argument, if it moves one line down; see there. */
13125 int start_y = line_bottom_y (&it1);
13126
13127 do {
13128 move_it_by_lines (&it, 1);
13129 it1 = it;
13130 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13131 }
13132
13133 /* If STARTP is unchanged, move it down another screen line. */
13134 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13135 move_it_by_lines (&it, 1);
13136 startp = it.current.pos;
13137 }
13138 else
13139 {
13140 struct text_pos scroll_margin_pos = startp;
13141
13142 /* See if point is inside the scroll margin at the top of the
13143 window. */
13144 if (this_scroll_margin)
13145 {
13146 start_display (&it, w, startp);
13147 move_it_vertically (&it, this_scroll_margin);
13148 scroll_margin_pos = it.current.pos;
13149 }
13150
13151 if (PT < CHARPOS (scroll_margin_pos))
13152 {
13153 /* Point is in the scroll margin at the top of the window or
13154 above what is displayed in the window. */
13155 int y0, y_to_move;
13156
13157 /* Compute the vertical distance from PT to the scroll
13158 margin position. Move as far as scroll_max allows, or
13159 one screenful, or 10 screen lines, whichever is largest.
13160 Give up if distance is greater than scroll_max. */
13161 SET_TEXT_POS (pos, PT, PT_BYTE);
13162 start_display (&it, w, pos);
13163 y0 = it.current_y;
13164 y_to_move = max (it.last_visible_y,
13165 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13166 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13167 y_to_move, -1,
13168 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13169 dy = it.current_y - y0;
13170 if (dy > scroll_max)
13171 return SCROLLING_FAILED;
13172
13173 /* Compute new window start. */
13174 start_display (&it, w, startp);
13175
13176 if (arg_scroll_conservatively)
13177 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13178 max (scroll_step, temp_scroll_step));
13179 else if (scroll_step || temp_scroll_step)
13180 amount_to_scroll = scroll_max;
13181 else
13182 {
13183 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13184 height = WINDOW_BOX_TEXT_HEIGHT (w);
13185 if (NUMBERP (aggressive))
13186 {
13187 double float_amount = XFLOATINT (aggressive) * height;
13188 amount_to_scroll = float_amount;
13189 if (amount_to_scroll == 0 && float_amount > 0)
13190 amount_to_scroll = 1;
13191 amount_to_scroll -=
13192 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13193 /* Don't let point enter the scroll margin near
13194 bottom of the window. */
13195 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13196 amount_to_scroll = height - 2*this_scroll_margin + dy;
13197 }
13198 }
13199
13200 if (amount_to_scroll <= 0)
13201 return SCROLLING_FAILED;
13202
13203 move_it_vertically_backward (&it, amount_to_scroll);
13204 startp = it.current.pos;
13205 }
13206 }
13207
13208 /* Run window scroll functions. */
13209 startp = run_window_scroll_functions (window, startp);
13210
13211 /* Display the window. Give up if new fonts are loaded, or if point
13212 doesn't appear. */
13213 if (!try_window (window, startp, 0))
13214 rc = SCROLLING_NEED_LARGER_MATRICES;
13215 else if (w->cursor.vpos < 0)
13216 {
13217 clear_glyph_matrix (w->desired_matrix);
13218 rc = SCROLLING_FAILED;
13219 }
13220 else
13221 {
13222 /* Maybe forget recorded base line for line number display. */
13223 if (!just_this_one_p
13224 || current_buffer->clip_changed
13225 || BEG_UNCHANGED < CHARPOS (startp))
13226 w->base_line_number = Qnil;
13227
13228 /* If cursor ends up on a partially visible line,
13229 treat that as being off the bottom of the screen. */
13230 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13231 /* It's possible that the cursor is on the first line of the
13232 buffer, which is partially obscured due to a vscroll
13233 (Bug#7537). In that case, avoid looping forever . */
13234 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13235 {
13236 clear_glyph_matrix (w->desired_matrix);
13237 ++extra_scroll_margin_lines;
13238 goto too_near_end;
13239 }
13240 rc = SCROLLING_SUCCESS;
13241 }
13242
13243 return rc;
13244 }
13245
13246
13247 /* Compute a suitable window start for window W if display of W starts
13248 on a continuation line. Value is non-zero if a new window start
13249 was computed.
13250
13251 The new window start will be computed, based on W's width, starting
13252 from the start of the continued line. It is the start of the
13253 screen line with the minimum distance from the old start W->start. */
13254
13255 static int
13256 compute_window_start_on_continuation_line (struct window *w)
13257 {
13258 struct text_pos pos, start_pos;
13259 int window_start_changed_p = 0;
13260
13261 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13262
13263 /* If window start is on a continuation line... Window start may be
13264 < BEGV in case there's invisible text at the start of the
13265 buffer (M-x rmail, for example). */
13266 if (CHARPOS (start_pos) > BEGV
13267 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13268 {
13269 struct it it;
13270 struct glyph_row *row;
13271
13272 /* Handle the case that the window start is out of range. */
13273 if (CHARPOS (start_pos) < BEGV)
13274 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13275 else if (CHARPOS (start_pos) > ZV)
13276 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13277
13278 /* Find the start of the continued line. This should be fast
13279 because scan_buffer is fast (newline cache). */
13280 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13281 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13282 row, DEFAULT_FACE_ID);
13283 reseat_at_previous_visible_line_start (&it);
13284
13285 /* If the line start is "too far" away from the window start,
13286 say it takes too much time to compute a new window start. */
13287 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13288 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13289 {
13290 int min_distance, distance;
13291
13292 /* Move forward by display lines to find the new window
13293 start. If window width was enlarged, the new start can
13294 be expected to be > the old start. If window width was
13295 decreased, the new window start will be < the old start.
13296 So, we're looking for the display line start with the
13297 minimum distance from the old window start. */
13298 pos = it.current.pos;
13299 min_distance = INFINITY;
13300 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13301 distance < min_distance)
13302 {
13303 min_distance = distance;
13304 pos = it.current.pos;
13305 move_it_by_lines (&it, 1);
13306 }
13307
13308 /* Set the window start there. */
13309 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13310 window_start_changed_p = 1;
13311 }
13312 }
13313
13314 return window_start_changed_p;
13315 }
13316
13317
13318 /* Try cursor movement in case text has not changed in window WINDOW,
13319 with window start STARTP. Value is
13320
13321 CURSOR_MOVEMENT_SUCCESS if successful
13322
13323 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13324
13325 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13326 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13327 we want to scroll as if scroll-step were set to 1. See the code.
13328
13329 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13330 which case we have to abort this redisplay, and adjust matrices
13331 first. */
13332
13333 enum
13334 {
13335 CURSOR_MOVEMENT_SUCCESS,
13336 CURSOR_MOVEMENT_CANNOT_BE_USED,
13337 CURSOR_MOVEMENT_MUST_SCROLL,
13338 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13339 };
13340
13341 static int
13342 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13343 {
13344 struct window *w = XWINDOW (window);
13345 struct frame *f = XFRAME (w->frame);
13346 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13347
13348 #if GLYPH_DEBUG
13349 if (inhibit_try_cursor_movement)
13350 return rc;
13351 #endif
13352
13353 /* Handle case where text has not changed, only point, and it has
13354 not moved off the frame. */
13355 if (/* Point may be in this window. */
13356 PT >= CHARPOS (startp)
13357 /* Selective display hasn't changed. */
13358 && !current_buffer->clip_changed
13359 /* Function force-mode-line-update is used to force a thorough
13360 redisplay. It sets either windows_or_buffers_changed or
13361 update_mode_lines. So don't take a shortcut here for these
13362 cases. */
13363 && !update_mode_lines
13364 && !windows_or_buffers_changed
13365 && !cursor_type_changed
13366 /* Can't use this case if highlighting a region. When a
13367 region exists, cursor movement has to do more than just
13368 set the cursor. */
13369 && !(!NILP (Vtransient_mark_mode)
13370 && !NILP (BVAR (current_buffer, mark_active)))
13371 && NILP (w->region_showing)
13372 && NILP (Vshow_trailing_whitespace)
13373 /* Right after splitting windows, last_point may be nil. */
13374 && INTEGERP (w->last_point)
13375 /* This code is not used for mini-buffer for the sake of the case
13376 of redisplaying to replace an echo area message; since in
13377 that case the mini-buffer contents per se are usually
13378 unchanged. This code is of no real use in the mini-buffer
13379 since the handling of this_line_start_pos, etc., in redisplay
13380 handles the same cases. */
13381 && !EQ (window, minibuf_window)
13382 /* When splitting windows or for new windows, it happens that
13383 redisplay is called with a nil window_end_vpos or one being
13384 larger than the window. This should really be fixed in
13385 window.c. I don't have this on my list, now, so we do
13386 approximately the same as the old redisplay code. --gerd. */
13387 && INTEGERP (w->window_end_vpos)
13388 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13389 && (FRAME_WINDOW_P (f)
13390 || !overlay_arrow_in_current_buffer_p ()))
13391 {
13392 int this_scroll_margin, top_scroll_margin;
13393 struct glyph_row *row = NULL;
13394
13395 #if GLYPH_DEBUG
13396 debug_method_add (w, "cursor movement");
13397 #endif
13398
13399 /* Scroll if point within this distance from the top or bottom
13400 of the window. This is a pixel value. */
13401 if (scroll_margin > 0)
13402 {
13403 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13404 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13405 }
13406 else
13407 this_scroll_margin = 0;
13408
13409 top_scroll_margin = this_scroll_margin;
13410 if (WINDOW_WANTS_HEADER_LINE_P (w))
13411 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13412
13413 /* Start with the row the cursor was displayed during the last
13414 not paused redisplay. Give up if that row is not valid. */
13415 if (w->last_cursor.vpos < 0
13416 || w->last_cursor.vpos >= w->current_matrix->nrows)
13417 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13418 else
13419 {
13420 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13421 if (row->mode_line_p)
13422 ++row;
13423 if (!row->enabled_p)
13424 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13425 }
13426
13427 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13428 {
13429 int scroll_p = 0, must_scroll = 0;
13430 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13431
13432 if (PT > XFASTINT (w->last_point))
13433 {
13434 /* Point has moved forward. */
13435 while (MATRIX_ROW_END_CHARPOS (row) < PT
13436 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13437 {
13438 xassert (row->enabled_p);
13439 ++row;
13440 }
13441
13442 /* If the end position of a row equals the start
13443 position of the next row, and PT is at that position,
13444 we would rather display cursor in the next line. */
13445 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13446 && MATRIX_ROW_END_CHARPOS (row) == PT
13447 && row < w->current_matrix->rows
13448 + w->current_matrix->nrows - 1
13449 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13450 && !cursor_row_p (row))
13451 ++row;
13452
13453 /* If within the scroll margin, scroll. Note that
13454 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13455 the next line would be drawn, and that
13456 this_scroll_margin can be zero. */
13457 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13458 || PT > MATRIX_ROW_END_CHARPOS (row)
13459 /* Line is completely visible last line in window
13460 and PT is to be set in the next line. */
13461 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13462 && PT == MATRIX_ROW_END_CHARPOS (row)
13463 && !row->ends_at_zv_p
13464 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13465 scroll_p = 1;
13466 }
13467 else if (PT < XFASTINT (w->last_point))
13468 {
13469 /* Cursor has to be moved backward. Note that PT >=
13470 CHARPOS (startp) because of the outer if-statement. */
13471 while (!row->mode_line_p
13472 && (MATRIX_ROW_START_CHARPOS (row) > PT
13473 || (MATRIX_ROW_START_CHARPOS (row) == PT
13474 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13475 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13476 row > w->current_matrix->rows
13477 && (row-1)->ends_in_newline_from_string_p))))
13478 && (row->y > top_scroll_margin
13479 || CHARPOS (startp) == BEGV))
13480 {
13481 xassert (row->enabled_p);
13482 --row;
13483 }
13484
13485 /* Consider the following case: Window starts at BEGV,
13486 there is invisible, intangible text at BEGV, so that
13487 display starts at some point START > BEGV. It can
13488 happen that we are called with PT somewhere between
13489 BEGV and START. Try to handle that case. */
13490 if (row < w->current_matrix->rows
13491 || row->mode_line_p)
13492 {
13493 row = w->current_matrix->rows;
13494 if (row->mode_line_p)
13495 ++row;
13496 }
13497
13498 /* Due to newlines in overlay strings, we may have to
13499 skip forward over overlay strings. */
13500 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13501 && MATRIX_ROW_END_CHARPOS (row) == PT
13502 && !cursor_row_p (row))
13503 ++row;
13504
13505 /* If within the scroll margin, scroll. */
13506 if (row->y < top_scroll_margin
13507 && CHARPOS (startp) != BEGV)
13508 scroll_p = 1;
13509 }
13510 else
13511 {
13512 /* Cursor did not move. So don't scroll even if cursor line
13513 is partially visible, as it was so before. */
13514 rc = CURSOR_MOVEMENT_SUCCESS;
13515 }
13516
13517 if (PT < MATRIX_ROW_START_CHARPOS (row)
13518 || PT > MATRIX_ROW_END_CHARPOS (row))
13519 {
13520 /* if PT is not in the glyph row, give up. */
13521 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13522 must_scroll = 1;
13523 }
13524 else if (rc != CURSOR_MOVEMENT_SUCCESS
13525 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
13526 {
13527 /* If rows are bidi-reordered and point moved, back up
13528 until we find a row that does not belong to a
13529 continuation line. This is because we must consider
13530 all rows of a continued line as candidates for the
13531 new cursor positioning, since row start and end
13532 positions change non-linearly with vertical position
13533 in such rows. */
13534 /* FIXME: Revisit this when glyph ``spilling'' in
13535 continuation lines' rows is implemented for
13536 bidi-reordered rows. */
13537 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13538 {
13539 xassert (row->enabled_p);
13540 --row;
13541 /* If we hit the beginning of the displayed portion
13542 without finding the first row of a continued
13543 line, give up. */
13544 if (row <= w->current_matrix->rows)
13545 {
13546 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13547 break;
13548 }
13549
13550 }
13551 }
13552 if (must_scroll)
13553 ;
13554 else if (rc != CURSOR_MOVEMENT_SUCCESS
13555 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13556 && make_cursor_line_fully_visible_p)
13557 {
13558 if (PT == MATRIX_ROW_END_CHARPOS (row)
13559 && !row->ends_at_zv_p
13560 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13561 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13562 else if (row->height > window_box_height (w))
13563 {
13564 /* If we end up in a partially visible line, let's
13565 make it fully visible, except when it's taller
13566 than the window, in which case we can't do much
13567 about it. */
13568 *scroll_step = 1;
13569 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13570 }
13571 else
13572 {
13573 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13574 if (!cursor_row_fully_visible_p (w, 0, 1))
13575 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13576 else
13577 rc = CURSOR_MOVEMENT_SUCCESS;
13578 }
13579 }
13580 else if (scroll_p)
13581 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13582 else if (rc != CURSOR_MOVEMENT_SUCCESS
13583 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
13584 {
13585 /* With bidi-reordered rows, there could be more than
13586 one candidate row whose start and end positions
13587 occlude point. We need to let set_cursor_from_row
13588 find the best candidate. */
13589 /* FIXME: Revisit this when glyph ``spilling'' in
13590 continuation lines' rows is implemented for
13591 bidi-reordered rows. */
13592 int rv = 0;
13593
13594 do
13595 {
13596 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13597 && PT <= MATRIX_ROW_END_CHARPOS (row)
13598 && cursor_row_p (row))
13599 rv |= set_cursor_from_row (w, row, w->current_matrix,
13600 0, 0, 0, 0);
13601 /* As soon as we've found the first suitable row
13602 whose ends_at_zv_p flag is set, we are done. */
13603 if (rv
13604 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13605 {
13606 rc = CURSOR_MOVEMENT_SUCCESS;
13607 break;
13608 }
13609 ++row;
13610 }
13611 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13612 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13613 || (MATRIX_ROW_START_CHARPOS (row) == PT
13614 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13615 /* If we didn't find any candidate rows, or exited the
13616 loop before all the candidates were examined, signal
13617 to the caller that this method failed. */
13618 if (rc != CURSOR_MOVEMENT_SUCCESS
13619 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13620 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13621 else if (rv)
13622 rc = CURSOR_MOVEMENT_SUCCESS;
13623 }
13624 else
13625 {
13626 do
13627 {
13628 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13629 {
13630 rc = CURSOR_MOVEMENT_SUCCESS;
13631 break;
13632 }
13633 ++row;
13634 }
13635 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13636 && MATRIX_ROW_START_CHARPOS (row) == PT
13637 && cursor_row_p (row));
13638 }
13639 }
13640 }
13641
13642 return rc;
13643 }
13644
13645 void
13646 set_vertical_scroll_bar (struct window *w)
13647 {
13648 EMACS_INT start, end, whole;
13649
13650 /* Calculate the start and end positions for the current window.
13651 At some point, it would be nice to choose between scrollbars
13652 which reflect the whole buffer size, with special markers
13653 indicating narrowing, and scrollbars which reflect only the
13654 visible region.
13655
13656 Note that mini-buffers sometimes aren't displaying any text. */
13657 if (!MINI_WINDOW_P (w)
13658 || (w == XWINDOW (minibuf_window)
13659 && NILP (echo_area_buffer[0])))
13660 {
13661 struct buffer *buf = XBUFFER (w->buffer);
13662 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13663 start = marker_position (w->start) - BUF_BEGV (buf);
13664 /* I don't think this is guaranteed to be right. For the
13665 moment, we'll pretend it is. */
13666 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13667
13668 if (end < start)
13669 end = start;
13670 if (whole < (end - start))
13671 whole = end - start;
13672 }
13673 else
13674 start = end = whole = 0;
13675
13676 /* Indicate what this scroll bar ought to be displaying now. */
13677 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13678 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13679 (w, end - start, whole, start);
13680 }
13681
13682
13683 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13684 selected_window is redisplayed.
13685
13686 We can return without actually redisplaying the window if
13687 fonts_changed_p is nonzero. In that case, redisplay_internal will
13688 retry. */
13689
13690 static void
13691 redisplay_window (Lisp_Object window, int just_this_one_p)
13692 {
13693 struct window *w = XWINDOW (window);
13694 struct frame *f = XFRAME (w->frame);
13695 struct buffer *buffer = XBUFFER (w->buffer);
13696 struct buffer *old = current_buffer;
13697 struct text_pos lpoint, opoint, startp;
13698 int update_mode_line;
13699 int tem;
13700 struct it it;
13701 /* Record it now because it's overwritten. */
13702 int current_matrix_up_to_date_p = 0;
13703 int used_current_matrix_p = 0;
13704 /* This is less strict than current_matrix_up_to_date_p.
13705 It indictes that the buffer contents and narrowing are unchanged. */
13706 int buffer_unchanged_p = 0;
13707 int temp_scroll_step = 0;
13708 int count = SPECPDL_INDEX ();
13709 int rc;
13710 int centering_position = -1;
13711 int last_line_misfit = 0;
13712 EMACS_INT beg_unchanged, end_unchanged;
13713
13714 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13715 opoint = lpoint;
13716
13717 /* W must be a leaf window here. */
13718 xassert (!NILP (w->buffer));
13719 #if GLYPH_DEBUG
13720 *w->desired_matrix->method = 0;
13721 #endif
13722
13723 restart:
13724 reconsider_clip_changes (w, buffer);
13725
13726 /* Has the mode line to be updated? */
13727 update_mode_line = (!NILP (w->update_mode_line)
13728 || update_mode_lines
13729 || buffer->clip_changed
13730 || buffer->prevent_redisplay_optimizations_p);
13731
13732 if (MINI_WINDOW_P (w))
13733 {
13734 if (w == XWINDOW (echo_area_window)
13735 && !NILP (echo_area_buffer[0]))
13736 {
13737 if (update_mode_line)
13738 /* We may have to update a tty frame's menu bar or a
13739 tool-bar. Example `M-x C-h C-h C-g'. */
13740 goto finish_menu_bars;
13741 else
13742 /* We've already displayed the echo area glyphs in this window. */
13743 goto finish_scroll_bars;
13744 }
13745 else if ((w != XWINDOW (minibuf_window)
13746 || minibuf_level == 0)
13747 /* When buffer is nonempty, redisplay window normally. */
13748 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13749 /* Quail displays non-mini buffers in minibuffer window.
13750 In that case, redisplay the window normally. */
13751 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13752 {
13753 /* W is a mini-buffer window, but it's not active, so clear
13754 it. */
13755 int yb = window_text_bottom_y (w);
13756 struct glyph_row *row;
13757 int y;
13758
13759 for (y = 0, row = w->desired_matrix->rows;
13760 y < yb;
13761 y += row->height, ++row)
13762 blank_row (w, row, y);
13763 goto finish_scroll_bars;
13764 }
13765
13766 clear_glyph_matrix (w->desired_matrix);
13767 }
13768
13769 /* Otherwise set up data on this window; select its buffer and point
13770 value. */
13771 /* Really select the buffer, for the sake of buffer-local
13772 variables. */
13773 set_buffer_internal_1 (XBUFFER (w->buffer));
13774
13775 current_matrix_up_to_date_p
13776 = (!NILP (w->window_end_valid)
13777 && !current_buffer->clip_changed
13778 && !current_buffer->prevent_redisplay_optimizations_p
13779 && XFASTINT (w->last_modified) >= MODIFF
13780 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13781
13782 /* Run the window-bottom-change-functions
13783 if it is possible that the text on the screen has changed
13784 (either due to modification of the text, or any other reason). */
13785 if (!current_matrix_up_to_date_p
13786 && !NILP (Vwindow_text_change_functions))
13787 {
13788 safe_run_hooks (Qwindow_text_change_functions);
13789 goto restart;
13790 }
13791
13792 beg_unchanged = BEG_UNCHANGED;
13793 end_unchanged = END_UNCHANGED;
13794
13795 SET_TEXT_POS (opoint, PT, PT_BYTE);
13796
13797 specbind (Qinhibit_point_motion_hooks, Qt);
13798
13799 buffer_unchanged_p
13800 = (!NILP (w->window_end_valid)
13801 && !current_buffer->clip_changed
13802 && XFASTINT (w->last_modified) >= MODIFF
13803 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13804
13805 /* When windows_or_buffers_changed is non-zero, we can't rely on
13806 the window end being valid, so set it to nil there. */
13807 if (windows_or_buffers_changed)
13808 {
13809 /* If window starts on a continuation line, maybe adjust the
13810 window start in case the window's width changed. */
13811 if (XMARKER (w->start)->buffer == current_buffer)
13812 compute_window_start_on_continuation_line (w);
13813
13814 w->window_end_valid = Qnil;
13815 }
13816
13817 /* Some sanity checks. */
13818 CHECK_WINDOW_END (w);
13819 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13820 abort ();
13821 if (BYTEPOS (opoint) < CHARPOS (opoint))
13822 abort ();
13823
13824 /* If %c is in mode line, update it if needed. */
13825 if (!NILP (w->column_number_displayed)
13826 /* This alternative quickly identifies a common case
13827 where no change is needed. */
13828 && !(PT == XFASTINT (w->last_point)
13829 && XFASTINT (w->last_modified) >= MODIFF
13830 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13831 && (XFASTINT (w->column_number_displayed) != current_column ()))
13832 update_mode_line = 1;
13833
13834 /* Count number of windows showing the selected buffer. An indirect
13835 buffer counts as its base buffer. */
13836 if (!just_this_one_p)
13837 {
13838 struct buffer *current_base, *window_base;
13839 current_base = current_buffer;
13840 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13841 if (current_base->base_buffer)
13842 current_base = current_base->base_buffer;
13843 if (window_base->base_buffer)
13844 window_base = window_base->base_buffer;
13845 if (current_base == window_base)
13846 buffer_shared++;
13847 }
13848
13849 /* Point refers normally to the selected window. For any other
13850 window, set up appropriate value. */
13851 if (!EQ (window, selected_window))
13852 {
13853 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
13854 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
13855 if (new_pt < BEGV)
13856 {
13857 new_pt = BEGV;
13858 new_pt_byte = BEGV_BYTE;
13859 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13860 }
13861 else if (new_pt > (ZV - 1))
13862 {
13863 new_pt = ZV;
13864 new_pt_byte = ZV_BYTE;
13865 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13866 }
13867
13868 /* We don't use SET_PT so that the point-motion hooks don't run. */
13869 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13870 }
13871
13872 /* If any of the character widths specified in the display table
13873 have changed, invalidate the width run cache. It's true that
13874 this may be a bit late to catch such changes, but the rest of
13875 redisplay goes (non-fatally) haywire when the display table is
13876 changed, so why should we worry about doing any better? */
13877 if (current_buffer->width_run_cache)
13878 {
13879 struct Lisp_Char_Table *disptab = buffer_display_table ();
13880
13881 if (! disptab_matches_widthtab (disptab,
13882 XVECTOR (BVAR (current_buffer, width_table))))
13883 {
13884 invalidate_region_cache (current_buffer,
13885 current_buffer->width_run_cache,
13886 BEG, Z);
13887 recompute_width_table (current_buffer, disptab);
13888 }
13889 }
13890
13891 /* If window-start is screwed up, choose a new one. */
13892 if (XMARKER (w->start)->buffer != current_buffer)
13893 goto recenter;
13894
13895 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13896
13897 /* If someone specified a new starting point but did not insist,
13898 check whether it can be used. */
13899 if (!NILP (w->optional_new_start)
13900 && CHARPOS (startp) >= BEGV
13901 && CHARPOS (startp) <= ZV)
13902 {
13903 w->optional_new_start = Qnil;
13904 start_display (&it, w, startp);
13905 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13906 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13907 if (IT_CHARPOS (it) == PT)
13908 w->force_start = Qt;
13909 /* IT may overshoot PT if text at PT is invisible. */
13910 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13911 w->force_start = Qt;
13912 }
13913
13914 force_start:
13915
13916 /* Handle case where place to start displaying has been specified,
13917 unless the specified location is outside the accessible range. */
13918 if (!NILP (w->force_start)
13919 || w->frozen_window_start_p)
13920 {
13921 /* We set this later on if we have to adjust point. */
13922 int new_vpos = -1;
13923
13924 w->force_start = Qnil;
13925 w->vscroll = 0;
13926 w->window_end_valid = Qnil;
13927
13928 /* Forget any recorded base line for line number display. */
13929 if (!buffer_unchanged_p)
13930 w->base_line_number = Qnil;
13931
13932 /* Redisplay the mode line. Select the buffer properly for that.
13933 Also, run the hook window-scroll-functions
13934 because we have scrolled. */
13935 /* Note, we do this after clearing force_start because
13936 if there's an error, it is better to forget about force_start
13937 than to get into an infinite loop calling the hook functions
13938 and having them get more errors. */
13939 if (!update_mode_line
13940 || ! NILP (Vwindow_scroll_functions))
13941 {
13942 update_mode_line = 1;
13943 w->update_mode_line = Qt;
13944 startp = run_window_scroll_functions (window, startp);
13945 }
13946
13947 w->last_modified = make_number (0);
13948 w->last_overlay_modified = make_number (0);
13949 if (CHARPOS (startp) < BEGV)
13950 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13951 else if (CHARPOS (startp) > ZV)
13952 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13953
13954 /* Redisplay, then check if cursor has been set during the
13955 redisplay. Give up if new fonts were loaded. */
13956 /* We used to issue a CHECK_MARGINS argument to try_window here,
13957 but this causes scrolling to fail when point begins inside
13958 the scroll margin (bug#148) -- cyd */
13959 if (!try_window (window, startp, 0))
13960 {
13961 w->force_start = Qt;
13962 clear_glyph_matrix (w->desired_matrix);
13963 goto need_larger_matrices;
13964 }
13965
13966 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13967 {
13968 /* If point does not appear, try to move point so it does
13969 appear. The desired matrix has been built above, so we
13970 can use it here. */
13971 new_vpos = window_box_height (w) / 2;
13972 }
13973
13974 if (!cursor_row_fully_visible_p (w, 0, 0))
13975 {
13976 /* Point does appear, but on a line partly visible at end of window.
13977 Move it back to a fully-visible line. */
13978 new_vpos = window_box_height (w);
13979 }
13980
13981 /* If we need to move point for either of the above reasons,
13982 now actually do it. */
13983 if (new_vpos >= 0)
13984 {
13985 struct glyph_row *row;
13986
13987 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13988 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13989 ++row;
13990
13991 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13992 MATRIX_ROW_START_BYTEPOS (row));
13993
13994 if (w != XWINDOW (selected_window))
13995 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13996 else if (current_buffer == old)
13997 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13998
13999 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14000
14001 /* If we are highlighting the region, then we just changed
14002 the region, so redisplay to show it. */
14003 if (!NILP (Vtransient_mark_mode)
14004 && !NILP (BVAR (current_buffer, mark_active)))
14005 {
14006 clear_glyph_matrix (w->desired_matrix);
14007 if (!try_window (window, startp, 0))
14008 goto need_larger_matrices;
14009 }
14010 }
14011
14012 #if GLYPH_DEBUG
14013 debug_method_add (w, "forced window start");
14014 #endif
14015 goto done;
14016 }
14017
14018 /* Handle case where text has not changed, only point, and it has
14019 not moved off the frame, and we are not retrying after hscroll.
14020 (current_matrix_up_to_date_p is nonzero when retrying.) */
14021 if (current_matrix_up_to_date_p
14022 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14023 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14024 {
14025 switch (rc)
14026 {
14027 case CURSOR_MOVEMENT_SUCCESS:
14028 used_current_matrix_p = 1;
14029 goto done;
14030
14031 case CURSOR_MOVEMENT_MUST_SCROLL:
14032 goto try_to_scroll;
14033
14034 default:
14035 abort ();
14036 }
14037 }
14038 /* If current starting point was originally the beginning of a line
14039 but no longer is, find a new starting point. */
14040 else if (!NILP (w->start_at_line_beg)
14041 && !(CHARPOS (startp) <= BEGV
14042 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14043 {
14044 #if GLYPH_DEBUG
14045 debug_method_add (w, "recenter 1");
14046 #endif
14047 goto recenter;
14048 }
14049
14050 /* Try scrolling with try_window_id. Value is > 0 if update has
14051 been done, it is -1 if we know that the same window start will
14052 not work. It is 0 if unsuccessful for some other reason. */
14053 else if ((tem = try_window_id (w)) != 0)
14054 {
14055 #if GLYPH_DEBUG
14056 debug_method_add (w, "try_window_id %d", tem);
14057 #endif
14058
14059 if (fonts_changed_p)
14060 goto need_larger_matrices;
14061 if (tem > 0)
14062 goto done;
14063
14064 /* Otherwise try_window_id has returned -1 which means that we
14065 don't want the alternative below this comment to execute. */
14066 }
14067 else if (CHARPOS (startp) >= BEGV
14068 && CHARPOS (startp) <= ZV
14069 && PT >= CHARPOS (startp)
14070 && (CHARPOS (startp) < ZV
14071 /* Avoid starting at end of buffer. */
14072 || CHARPOS (startp) == BEGV
14073 || (XFASTINT (w->last_modified) >= MODIFF
14074 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14075 {
14076
14077 /* If first window line is a continuation line, and window start
14078 is inside the modified region, but the first change is before
14079 current window start, we must select a new window start.
14080
14081 However, if this is the result of a down-mouse event (e.g. by
14082 extending the mouse-drag-overlay), we don't want to select a
14083 new window start, since that would change the position under
14084 the mouse, resulting in an unwanted mouse-movement rather
14085 than a simple mouse-click. */
14086 if (NILP (w->start_at_line_beg)
14087 && NILP (do_mouse_tracking)
14088 && CHARPOS (startp) > BEGV
14089 && CHARPOS (startp) > BEG + beg_unchanged
14090 && CHARPOS (startp) <= Z - end_unchanged
14091 /* Even if w->start_at_line_beg is nil, a new window may
14092 start at a line_beg, since that's how set_buffer_window
14093 sets it. So, we need to check the return value of
14094 compute_window_start_on_continuation_line. (See also
14095 bug#197). */
14096 && XMARKER (w->start)->buffer == current_buffer
14097 && compute_window_start_on_continuation_line (w))
14098 {
14099 w->force_start = Qt;
14100 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14101 goto force_start;
14102 }
14103
14104 #if GLYPH_DEBUG
14105 debug_method_add (w, "same window start");
14106 #endif
14107
14108 /* Try to redisplay starting at same place as before.
14109 If point has not moved off frame, accept the results. */
14110 if (!current_matrix_up_to_date_p
14111 /* Don't use try_window_reusing_current_matrix in this case
14112 because a window scroll function can have changed the
14113 buffer. */
14114 || !NILP (Vwindow_scroll_functions)
14115 || MINI_WINDOW_P (w)
14116 || !(used_current_matrix_p
14117 = try_window_reusing_current_matrix (w)))
14118 {
14119 IF_DEBUG (debug_method_add (w, "1"));
14120 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14121 /* -1 means we need to scroll.
14122 0 means we need new matrices, but fonts_changed_p
14123 is set in that case, so we will detect it below. */
14124 goto try_to_scroll;
14125 }
14126
14127 if (fonts_changed_p)
14128 goto need_larger_matrices;
14129
14130 if (w->cursor.vpos >= 0)
14131 {
14132 if (!just_this_one_p
14133 || current_buffer->clip_changed
14134 || BEG_UNCHANGED < CHARPOS (startp))
14135 /* Forget any recorded base line for line number display. */
14136 w->base_line_number = Qnil;
14137
14138 if (!cursor_row_fully_visible_p (w, 1, 0))
14139 {
14140 clear_glyph_matrix (w->desired_matrix);
14141 last_line_misfit = 1;
14142 }
14143 /* Drop through and scroll. */
14144 else
14145 goto done;
14146 }
14147 else
14148 clear_glyph_matrix (w->desired_matrix);
14149 }
14150
14151 try_to_scroll:
14152
14153 w->last_modified = make_number (0);
14154 w->last_overlay_modified = make_number (0);
14155
14156 /* Redisplay the mode line. Select the buffer properly for that. */
14157 if (!update_mode_line)
14158 {
14159 update_mode_line = 1;
14160 w->update_mode_line = Qt;
14161 }
14162
14163 /* Try to scroll by specified few lines. */
14164 if ((scroll_conservatively
14165 || emacs_scroll_step
14166 || temp_scroll_step
14167 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14168 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14169 && CHARPOS (startp) >= BEGV
14170 && CHARPOS (startp) <= ZV)
14171 {
14172 /* The function returns -1 if new fonts were loaded, 1 if
14173 successful, 0 if not successful. */
14174 int ss = try_scrolling (window, just_this_one_p,
14175 scroll_conservatively,
14176 emacs_scroll_step,
14177 temp_scroll_step, last_line_misfit);
14178 switch (ss)
14179 {
14180 case SCROLLING_SUCCESS:
14181 goto done;
14182
14183 case SCROLLING_NEED_LARGER_MATRICES:
14184 goto need_larger_matrices;
14185
14186 case SCROLLING_FAILED:
14187 break;
14188
14189 default:
14190 abort ();
14191 }
14192 }
14193
14194 /* Finally, just choose a place to start which positions point
14195 according to user preferences. */
14196
14197 recenter:
14198
14199 #if GLYPH_DEBUG
14200 debug_method_add (w, "recenter");
14201 #endif
14202
14203 /* w->vscroll = 0; */
14204
14205 /* Forget any previously recorded base line for line number display. */
14206 if (!buffer_unchanged_p)
14207 w->base_line_number = Qnil;
14208
14209 /* Determine the window start relative to point. */
14210 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14211 it.current_y = it.last_visible_y;
14212 if (centering_position < 0)
14213 {
14214 int margin =
14215 scroll_margin > 0
14216 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14217 : 0;
14218 EMACS_INT margin_pos = CHARPOS (startp);
14219 int scrolling_up;
14220 Lisp_Object aggressive;
14221
14222 /* If there is a scroll margin at the top of the window, find
14223 its character position. */
14224 if (margin)
14225 {
14226 struct it it1;
14227
14228 start_display (&it1, w, startp);
14229 move_it_vertically (&it1, margin);
14230 margin_pos = IT_CHARPOS (it1);
14231 }
14232 scrolling_up = PT > margin_pos;
14233 aggressive =
14234 scrolling_up
14235 ? BVAR (current_buffer, scroll_up_aggressively)
14236 : BVAR (current_buffer, scroll_down_aggressively);
14237
14238 if (!MINI_WINDOW_P (w)
14239 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
14240 {
14241 int pt_offset = 0;
14242
14243 /* Setting scroll-conservatively overrides
14244 scroll-*-aggressively. */
14245 if (!scroll_conservatively && NUMBERP (aggressive))
14246 {
14247 double float_amount = XFLOATINT (aggressive);
14248
14249 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14250 if (pt_offset == 0 && float_amount > 0)
14251 pt_offset = 1;
14252 if (pt_offset)
14253 margin -= 1;
14254 }
14255 /* Compute how much to move the window start backward from
14256 point so that point will be displayed where the user
14257 wants it. */
14258 if (scrolling_up)
14259 {
14260 centering_position = it.last_visible_y;
14261 if (pt_offset)
14262 centering_position -= pt_offset;
14263 centering_position -=
14264 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
14265 /* Don't let point enter the scroll margin near top of
14266 the window. */
14267 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
14268 centering_position = margin * FRAME_LINE_HEIGHT (f);
14269 }
14270 else
14271 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
14272 }
14273 else
14274 /* Set the window start half the height of the window backward
14275 from point. */
14276 centering_position = window_box_height (w) / 2;
14277 }
14278 move_it_vertically_backward (&it, centering_position);
14279
14280 xassert (IT_CHARPOS (it) >= BEGV);
14281
14282 /* The function move_it_vertically_backward may move over more
14283 than the specified y-distance. If it->w is small, e.g. a
14284 mini-buffer window, we may end up in front of the window's
14285 display area. Start displaying at the start of the line
14286 containing PT in this case. */
14287 if (it.current_y <= 0)
14288 {
14289 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14290 move_it_vertically_backward (&it, 0);
14291 it.current_y = 0;
14292 }
14293
14294 it.current_x = it.hpos = 0;
14295
14296 /* Set the window start position here explicitly, to avoid an
14297 infinite loop in case the functions in window-scroll-functions
14298 get errors. */
14299 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14300
14301 /* Run scroll hooks. */
14302 startp = run_window_scroll_functions (window, it.current.pos);
14303
14304 /* Redisplay the window. */
14305 if (!current_matrix_up_to_date_p
14306 || windows_or_buffers_changed
14307 || cursor_type_changed
14308 /* Don't use try_window_reusing_current_matrix in this case
14309 because it can have changed the buffer. */
14310 || !NILP (Vwindow_scroll_functions)
14311 || !just_this_one_p
14312 || MINI_WINDOW_P (w)
14313 || !(used_current_matrix_p
14314 = try_window_reusing_current_matrix (w)))
14315 try_window (window, startp, 0);
14316
14317 /* If new fonts have been loaded (due to fontsets), give up. We
14318 have to start a new redisplay since we need to re-adjust glyph
14319 matrices. */
14320 if (fonts_changed_p)
14321 goto need_larger_matrices;
14322
14323 /* If cursor did not appear assume that the middle of the window is
14324 in the first line of the window. Do it again with the next line.
14325 (Imagine a window of height 100, displaying two lines of height
14326 60. Moving back 50 from it->last_visible_y will end in the first
14327 line.) */
14328 if (w->cursor.vpos < 0)
14329 {
14330 if (!NILP (w->window_end_valid)
14331 && PT >= Z - XFASTINT (w->window_end_pos))
14332 {
14333 clear_glyph_matrix (w->desired_matrix);
14334 move_it_by_lines (&it, 1);
14335 try_window (window, it.current.pos, 0);
14336 }
14337 else if (PT < IT_CHARPOS (it))
14338 {
14339 clear_glyph_matrix (w->desired_matrix);
14340 move_it_by_lines (&it, -1);
14341 try_window (window, it.current.pos, 0);
14342 }
14343 else
14344 {
14345 /* Not much we can do about it. */
14346 }
14347 }
14348
14349 /* Consider the following case: Window starts at BEGV, there is
14350 invisible, intangible text at BEGV, so that display starts at
14351 some point START > BEGV. It can happen that we are called with
14352 PT somewhere between BEGV and START. Try to handle that case. */
14353 if (w->cursor.vpos < 0)
14354 {
14355 struct glyph_row *row = w->current_matrix->rows;
14356 if (row->mode_line_p)
14357 ++row;
14358 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14359 }
14360
14361 if (!cursor_row_fully_visible_p (w, 0, 0))
14362 {
14363 /* If vscroll is enabled, disable it and try again. */
14364 if (w->vscroll)
14365 {
14366 w->vscroll = 0;
14367 clear_glyph_matrix (w->desired_matrix);
14368 goto recenter;
14369 }
14370
14371 /* If centering point failed to make the whole line visible,
14372 put point at the top instead. That has to make the whole line
14373 visible, if it can be done. */
14374 if (centering_position == 0)
14375 goto done;
14376
14377 clear_glyph_matrix (w->desired_matrix);
14378 centering_position = 0;
14379 goto recenter;
14380 }
14381
14382 done:
14383
14384 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14385 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14386 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14387 ? Qt : Qnil);
14388
14389 /* Display the mode line, if we must. */
14390 if ((update_mode_line
14391 /* If window not full width, must redo its mode line
14392 if (a) the window to its side is being redone and
14393 (b) we do a frame-based redisplay. This is a consequence
14394 of how inverted lines are drawn in frame-based redisplay. */
14395 || (!just_this_one_p
14396 && !FRAME_WINDOW_P (f)
14397 && !WINDOW_FULL_WIDTH_P (w))
14398 /* Line number to display. */
14399 || INTEGERP (w->base_line_pos)
14400 /* Column number is displayed and different from the one displayed. */
14401 || (!NILP (w->column_number_displayed)
14402 && (XFASTINT (w->column_number_displayed) != current_column ())))
14403 /* This means that the window has a mode line. */
14404 && (WINDOW_WANTS_MODELINE_P (w)
14405 || WINDOW_WANTS_HEADER_LINE_P (w)))
14406 {
14407 display_mode_lines (w);
14408
14409 /* If mode line height has changed, arrange for a thorough
14410 immediate redisplay using the correct mode line height. */
14411 if (WINDOW_WANTS_MODELINE_P (w)
14412 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14413 {
14414 fonts_changed_p = 1;
14415 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14416 = DESIRED_MODE_LINE_HEIGHT (w);
14417 }
14418
14419 /* If header line height has changed, arrange for a thorough
14420 immediate redisplay using the correct header line height. */
14421 if (WINDOW_WANTS_HEADER_LINE_P (w)
14422 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14423 {
14424 fonts_changed_p = 1;
14425 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14426 = DESIRED_HEADER_LINE_HEIGHT (w);
14427 }
14428
14429 if (fonts_changed_p)
14430 goto need_larger_matrices;
14431 }
14432
14433 if (!line_number_displayed
14434 && !BUFFERP (w->base_line_pos))
14435 {
14436 w->base_line_pos = Qnil;
14437 w->base_line_number = Qnil;
14438 }
14439
14440 finish_menu_bars:
14441
14442 /* When we reach a frame's selected window, redo the frame's menu bar. */
14443 if (update_mode_line
14444 && EQ (FRAME_SELECTED_WINDOW (f), window))
14445 {
14446 int redisplay_menu_p = 0;
14447 int redisplay_tool_bar_p = 0;
14448
14449 if (FRAME_WINDOW_P (f))
14450 {
14451 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14452 || defined (HAVE_NS) || defined (USE_GTK)
14453 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14454 #else
14455 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14456 #endif
14457 }
14458 else
14459 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14460
14461 if (redisplay_menu_p)
14462 display_menu_bar (w);
14463
14464 #ifdef HAVE_WINDOW_SYSTEM
14465 if (FRAME_WINDOW_P (f))
14466 {
14467 #if defined (USE_GTK) || defined (HAVE_NS)
14468 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14469 #else
14470 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14471 && (FRAME_TOOL_BAR_LINES (f) > 0
14472 || !NILP (Vauto_resize_tool_bars));
14473 #endif
14474
14475 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14476 {
14477 ignore_mouse_drag_p = 1;
14478 }
14479 }
14480 #endif
14481 }
14482
14483 #ifdef HAVE_WINDOW_SYSTEM
14484 if (FRAME_WINDOW_P (f)
14485 && update_window_fringes (w, (just_this_one_p
14486 || (!used_current_matrix_p && !overlay_arrow_seen)
14487 || w->pseudo_window_p)))
14488 {
14489 update_begin (f);
14490 BLOCK_INPUT;
14491 if (draw_window_fringes (w, 1))
14492 x_draw_vertical_border (w);
14493 UNBLOCK_INPUT;
14494 update_end (f);
14495 }
14496 #endif /* HAVE_WINDOW_SYSTEM */
14497
14498 /* We go to this label, with fonts_changed_p nonzero,
14499 if it is necessary to try again using larger glyph matrices.
14500 We have to redeem the scroll bar even in this case,
14501 because the loop in redisplay_internal expects that. */
14502 need_larger_matrices:
14503 ;
14504 finish_scroll_bars:
14505
14506 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14507 {
14508 /* Set the thumb's position and size. */
14509 set_vertical_scroll_bar (w);
14510
14511 /* Note that we actually used the scroll bar attached to this
14512 window, so it shouldn't be deleted at the end of redisplay. */
14513 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14514 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14515 }
14516
14517 /* Restore current_buffer and value of point in it. The window
14518 update may have changed the buffer, so first make sure `opoint'
14519 is still valid (Bug#6177). */
14520 if (CHARPOS (opoint) < BEGV)
14521 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14522 else if (CHARPOS (opoint) > ZV)
14523 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14524 else
14525 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14526
14527 set_buffer_internal_1 (old);
14528 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14529 shorter. This can be caused by log truncation in *Messages*. */
14530 if (CHARPOS (lpoint) <= ZV)
14531 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14532
14533 unbind_to (count, Qnil);
14534 }
14535
14536
14537 /* Build the complete desired matrix of WINDOW with a window start
14538 buffer position POS.
14539
14540 Value is 1 if successful. It is zero if fonts were loaded during
14541 redisplay which makes re-adjusting glyph matrices necessary, and -1
14542 if point would appear in the scroll margins.
14543 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14544 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14545 set in FLAGS.) */
14546
14547 int
14548 try_window (Lisp_Object window, struct text_pos pos, int flags)
14549 {
14550 struct window *w = XWINDOW (window);
14551 struct it it;
14552 struct glyph_row *last_text_row = NULL;
14553 struct frame *f = XFRAME (w->frame);
14554
14555 /* Make POS the new window start. */
14556 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14557
14558 /* Mark cursor position as unknown. No overlay arrow seen. */
14559 w->cursor.vpos = -1;
14560 overlay_arrow_seen = 0;
14561
14562 /* Initialize iterator and info to start at POS. */
14563 start_display (&it, w, pos);
14564
14565 /* Display all lines of W. */
14566 while (it.current_y < it.last_visible_y)
14567 {
14568 if (display_line (&it))
14569 last_text_row = it.glyph_row - 1;
14570 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14571 return 0;
14572 }
14573
14574 /* Don't let the cursor end in the scroll margins. */
14575 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14576 && !MINI_WINDOW_P (w))
14577 {
14578 int this_scroll_margin;
14579
14580 if (scroll_margin > 0)
14581 {
14582 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14583 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14584 }
14585 else
14586 this_scroll_margin = 0;
14587
14588 if ((w->cursor.y >= 0 /* not vscrolled */
14589 && w->cursor.y < this_scroll_margin
14590 && CHARPOS (pos) > BEGV
14591 && IT_CHARPOS (it) < ZV)
14592 /* rms: considering make_cursor_line_fully_visible_p here
14593 seems to give wrong results. We don't want to recenter
14594 when the last line is partly visible, we want to allow
14595 that case to be handled in the usual way. */
14596 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14597 {
14598 w->cursor.vpos = -1;
14599 clear_glyph_matrix (w->desired_matrix);
14600 return -1;
14601 }
14602 }
14603
14604 /* If bottom moved off end of frame, change mode line percentage. */
14605 if (XFASTINT (w->window_end_pos) <= 0
14606 && Z != IT_CHARPOS (it))
14607 w->update_mode_line = Qt;
14608
14609 /* Set window_end_pos to the offset of the last character displayed
14610 on the window from the end of current_buffer. Set
14611 window_end_vpos to its row number. */
14612 if (last_text_row)
14613 {
14614 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14615 w->window_end_bytepos
14616 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14617 w->window_end_pos
14618 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14619 w->window_end_vpos
14620 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14621 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14622 ->displays_text_p);
14623 }
14624 else
14625 {
14626 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14627 w->window_end_pos = make_number (Z - ZV);
14628 w->window_end_vpos = make_number (0);
14629 }
14630
14631 /* But that is not valid info until redisplay finishes. */
14632 w->window_end_valid = Qnil;
14633 return 1;
14634 }
14635
14636
14637 \f
14638 /************************************************************************
14639 Window redisplay reusing current matrix when buffer has not changed
14640 ************************************************************************/
14641
14642 /* Try redisplay of window W showing an unchanged buffer with a
14643 different window start than the last time it was displayed by
14644 reusing its current matrix. Value is non-zero if successful.
14645 W->start is the new window start. */
14646
14647 static int
14648 try_window_reusing_current_matrix (struct window *w)
14649 {
14650 struct frame *f = XFRAME (w->frame);
14651 struct glyph_row *bottom_row;
14652 struct it it;
14653 struct run run;
14654 struct text_pos start, new_start;
14655 int nrows_scrolled, i;
14656 struct glyph_row *last_text_row;
14657 struct glyph_row *last_reused_text_row;
14658 struct glyph_row *start_row;
14659 int start_vpos, min_y, max_y;
14660
14661 #if GLYPH_DEBUG
14662 if (inhibit_try_window_reusing)
14663 return 0;
14664 #endif
14665
14666 if (/* This function doesn't handle terminal frames. */
14667 !FRAME_WINDOW_P (f)
14668 /* Don't try to reuse the display if windows have been split
14669 or such. */
14670 || windows_or_buffers_changed
14671 || cursor_type_changed)
14672 return 0;
14673
14674 /* Can't do this if region may have changed. */
14675 if ((!NILP (Vtransient_mark_mode)
14676 && !NILP (BVAR (current_buffer, mark_active)))
14677 || !NILP (w->region_showing)
14678 || !NILP (Vshow_trailing_whitespace))
14679 return 0;
14680
14681 /* If top-line visibility has changed, give up. */
14682 if (WINDOW_WANTS_HEADER_LINE_P (w)
14683 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14684 return 0;
14685
14686 /* Give up if old or new display is scrolled vertically. We could
14687 make this function handle this, but right now it doesn't. */
14688 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14689 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14690 return 0;
14691
14692 /* The variable new_start now holds the new window start. The old
14693 start `start' can be determined from the current matrix. */
14694 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14695 start = start_row->minpos;
14696 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14697
14698 /* Clear the desired matrix for the display below. */
14699 clear_glyph_matrix (w->desired_matrix);
14700
14701 if (CHARPOS (new_start) <= CHARPOS (start))
14702 {
14703 /* Don't use this method if the display starts with an ellipsis
14704 displayed for invisible text. It's not easy to handle that case
14705 below, and it's certainly not worth the effort since this is
14706 not a frequent case. */
14707 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14708 return 0;
14709
14710 IF_DEBUG (debug_method_add (w, "twu1"));
14711
14712 /* Display up to a row that can be reused. The variable
14713 last_text_row is set to the last row displayed that displays
14714 text. Note that it.vpos == 0 if or if not there is a
14715 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14716 start_display (&it, w, new_start);
14717 w->cursor.vpos = -1;
14718 last_text_row = last_reused_text_row = NULL;
14719
14720 while (it.current_y < it.last_visible_y
14721 && !fonts_changed_p)
14722 {
14723 /* If we have reached into the characters in the START row,
14724 that means the line boundaries have changed. So we
14725 can't start copying with the row START. Maybe it will
14726 work to start copying with the following row. */
14727 while (IT_CHARPOS (it) > CHARPOS (start))
14728 {
14729 /* Advance to the next row as the "start". */
14730 start_row++;
14731 start = start_row->minpos;
14732 /* If there are no more rows to try, or just one, give up. */
14733 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14734 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14735 || CHARPOS (start) == ZV)
14736 {
14737 clear_glyph_matrix (w->desired_matrix);
14738 return 0;
14739 }
14740
14741 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14742 }
14743 /* If we have reached alignment,
14744 we can copy the rest of the rows. */
14745 if (IT_CHARPOS (it) == CHARPOS (start))
14746 break;
14747
14748 if (display_line (&it))
14749 last_text_row = it.glyph_row - 1;
14750 }
14751
14752 /* A value of current_y < last_visible_y means that we stopped
14753 at the previous window start, which in turn means that we
14754 have at least one reusable row. */
14755 if (it.current_y < it.last_visible_y)
14756 {
14757 struct glyph_row *row;
14758
14759 /* IT.vpos always starts from 0; it counts text lines. */
14760 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14761
14762 /* Find PT if not already found in the lines displayed. */
14763 if (w->cursor.vpos < 0)
14764 {
14765 int dy = it.current_y - start_row->y;
14766
14767 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14768 row = row_containing_pos (w, PT, row, NULL, dy);
14769 if (row)
14770 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14771 dy, nrows_scrolled);
14772 else
14773 {
14774 clear_glyph_matrix (w->desired_matrix);
14775 return 0;
14776 }
14777 }
14778
14779 /* Scroll the display. Do it before the current matrix is
14780 changed. The problem here is that update has not yet
14781 run, i.e. part of the current matrix is not up to date.
14782 scroll_run_hook will clear the cursor, and use the
14783 current matrix to get the height of the row the cursor is
14784 in. */
14785 run.current_y = start_row->y;
14786 run.desired_y = it.current_y;
14787 run.height = it.last_visible_y - it.current_y;
14788
14789 if (run.height > 0 && run.current_y != run.desired_y)
14790 {
14791 update_begin (f);
14792 FRAME_RIF (f)->update_window_begin_hook (w);
14793 FRAME_RIF (f)->clear_window_mouse_face (w);
14794 FRAME_RIF (f)->scroll_run_hook (w, &run);
14795 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14796 update_end (f);
14797 }
14798
14799 /* Shift current matrix down by nrows_scrolled lines. */
14800 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14801 rotate_matrix (w->current_matrix,
14802 start_vpos,
14803 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14804 nrows_scrolled);
14805
14806 /* Disable lines that must be updated. */
14807 for (i = 0; i < nrows_scrolled; ++i)
14808 (start_row + i)->enabled_p = 0;
14809
14810 /* Re-compute Y positions. */
14811 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14812 max_y = it.last_visible_y;
14813 for (row = start_row + nrows_scrolled;
14814 row < bottom_row;
14815 ++row)
14816 {
14817 row->y = it.current_y;
14818 row->visible_height = row->height;
14819
14820 if (row->y < min_y)
14821 row->visible_height -= min_y - row->y;
14822 if (row->y + row->height > max_y)
14823 row->visible_height -= row->y + row->height - max_y;
14824 row->redraw_fringe_bitmaps_p = 1;
14825
14826 it.current_y += row->height;
14827
14828 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14829 last_reused_text_row = row;
14830 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14831 break;
14832 }
14833
14834 /* Disable lines in the current matrix which are now
14835 below the window. */
14836 for (++row; row < bottom_row; ++row)
14837 row->enabled_p = row->mode_line_p = 0;
14838 }
14839
14840 /* Update window_end_pos etc.; last_reused_text_row is the last
14841 reused row from the current matrix containing text, if any.
14842 The value of last_text_row is the last displayed line
14843 containing text. */
14844 if (last_reused_text_row)
14845 {
14846 w->window_end_bytepos
14847 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14848 w->window_end_pos
14849 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14850 w->window_end_vpos
14851 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14852 w->current_matrix));
14853 }
14854 else if (last_text_row)
14855 {
14856 w->window_end_bytepos
14857 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14858 w->window_end_pos
14859 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14860 w->window_end_vpos
14861 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14862 }
14863 else
14864 {
14865 /* This window must be completely empty. */
14866 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14867 w->window_end_pos = make_number (Z - ZV);
14868 w->window_end_vpos = make_number (0);
14869 }
14870 w->window_end_valid = Qnil;
14871
14872 /* Update hint: don't try scrolling again in update_window. */
14873 w->desired_matrix->no_scrolling_p = 1;
14874
14875 #if GLYPH_DEBUG
14876 debug_method_add (w, "try_window_reusing_current_matrix 1");
14877 #endif
14878 return 1;
14879 }
14880 else if (CHARPOS (new_start) > CHARPOS (start))
14881 {
14882 struct glyph_row *pt_row, *row;
14883 struct glyph_row *first_reusable_row;
14884 struct glyph_row *first_row_to_display;
14885 int dy;
14886 int yb = window_text_bottom_y (w);
14887
14888 /* Find the row starting at new_start, if there is one. Don't
14889 reuse a partially visible line at the end. */
14890 first_reusable_row = start_row;
14891 while (first_reusable_row->enabled_p
14892 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14893 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14894 < CHARPOS (new_start)))
14895 ++first_reusable_row;
14896
14897 /* Give up if there is no row to reuse. */
14898 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14899 || !first_reusable_row->enabled_p
14900 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14901 != CHARPOS (new_start)))
14902 return 0;
14903
14904 /* We can reuse fully visible rows beginning with
14905 first_reusable_row to the end of the window. Set
14906 first_row_to_display to the first row that cannot be reused.
14907 Set pt_row to the row containing point, if there is any. */
14908 pt_row = NULL;
14909 for (first_row_to_display = first_reusable_row;
14910 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14911 ++first_row_to_display)
14912 {
14913 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14914 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14915 pt_row = first_row_to_display;
14916 }
14917
14918 /* Start displaying at the start of first_row_to_display. */
14919 xassert (first_row_to_display->y < yb);
14920 init_to_row_start (&it, w, first_row_to_display);
14921
14922 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14923 - start_vpos);
14924 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14925 - nrows_scrolled);
14926 it.current_y = (first_row_to_display->y - first_reusable_row->y
14927 + WINDOW_HEADER_LINE_HEIGHT (w));
14928
14929 /* Display lines beginning with first_row_to_display in the
14930 desired matrix. Set last_text_row to the last row displayed
14931 that displays text. */
14932 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14933 if (pt_row == NULL)
14934 w->cursor.vpos = -1;
14935 last_text_row = NULL;
14936 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14937 if (display_line (&it))
14938 last_text_row = it.glyph_row - 1;
14939
14940 /* If point is in a reused row, adjust y and vpos of the cursor
14941 position. */
14942 if (pt_row)
14943 {
14944 w->cursor.vpos -= nrows_scrolled;
14945 w->cursor.y -= first_reusable_row->y - start_row->y;
14946 }
14947
14948 /* Give up if point isn't in a row displayed or reused. (This
14949 also handles the case where w->cursor.vpos < nrows_scrolled
14950 after the calls to display_line, which can happen with scroll
14951 margins. See bug#1295.) */
14952 if (w->cursor.vpos < 0)
14953 {
14954 clear_glyph_matrix (w->desired_matrix);
14955 return 0;
14956 }
14957
14958 /* Scroll the display. */
14959 run.current_y = first_reusable_row->y;
14960 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14961 run.height = it.last_visible_y - run.current_y;
14962 dy = run.current_y - run.desired_y;
14963
14964 if (run.height)
14965 {
14966 update_begin (f);
14967 FRAME_RIF (f)->update_window_begin_hook (w);
14968 FRAME_RIF (f)->clear_window_mouse_face (w);
14969 FRAME_RIF (f)->scroll_run_hook (w, &run);
14970 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14971 update_end (f);
14972 }
14973
14974 /* Adjust Y positions of reused rows. */
14975 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14976 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14977 max_y = it.last_visible_y;
14978 for (row = first_reusable_row; row < first_row_to_display; ++row)
14979 {
14980 row->y -= dy;
14981 row->visible_height = row->height;
14982 if (row->y < min_y)
14983 row->visible_height -= min_y - row->y;
14984 if (row->y + row->height > max_y)
14985 row->visible_height -= row->y + row->height - max_y;
14986 row->redraw_fringe_bitmaps_p = 1;
14987 }
14988
14989 /* Scroll the current matrix. */
14990 xassert (nrows_scrolled > 0);
14991 rotate_matrix (w->current_matrix,
14992 start_vpos,
14993 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14994 -nrows_scrolled);
14995
14996 /* Disable rows not reused. */
14997 for (row -= nrows_scrolled; row < bottom_row; ++row)
14998 row->enabled_p = 0;
14999
15000 /* Point may have moved to a different line, so we cannot assume that
15001 the previous cursor position is valid; locate the correct row. */
15002 if (pt_row)
15003 {
15004 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15005 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15006 row++)
15007 {
15008 w->cursor.vpos++;
15009 w->cursor.y = row->y;
15010 }
15011 if (row < bottom_row)
15012 {
15013 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15014 struct glyph *end = glyph + row->used[TEXT_AREA];
15015
15016 /* Can't use this optimization with bidi-reordered glyph
15017 rows, unless cursor is already at point. */
15018 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15019 {
15020 if (!(w->cursor.hpos >= 0
15021 && w->cursor.hpos < row->used[TEXT_AREA]
15022 && BUFFERP (glyph->object)
15023 && glyph->charpos == PT))
15024 return 0;
15025 }
15026 else
15027 for (; glyph < end
15028 && (!BUFFERP (glyph->object)
15029 || glyph->charpos < PT);
15030 glyph++)
15031 {
15032 w->cursor.hpos++;
15033 w->cursor.x += glyph->pixel_width;
15034 }
15035 }
15036 }
15037
15038 /* Adjust window end. A null value of last_text_row means that
15039 the window end is in reused rows which in turn means that
15040 only its vpos can have changed. */
15041 if (last_text_row)
15042 {
15043 w->window_end_bytepos
15044 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15045 w->window_end_pos
15046 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15047 w->window_end_vpos
15048 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15049 }
15050 else
15051 {
15052 w->window_end_vpos
15053 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15054 }
15055
15056 w->window_end_valid = Qnil;
15057 w->desired_matrix->no_scrolling_p = 1;
15058
15059 #if GLYPH_DEBUG
15060 debug_method_add (w, "try_window_reusing_current_matrix 2");
15061 #endif
15062 return 1;
15063 }
15064
15065 return 0;
15066 }
15067
15068
15069 \f
15070 /************************************************************************
15071 Window redisplay reusing current matrix when buffer has changed
15072 ************************************************************************/
15073
15074 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15075 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15076 EMACS_INT *, EMACS_INT *);
15077 static struct glyph_row *
15078 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15079 struct glyph_row *);
15080
15081
15082 /* Return the last row in MATRIX displaying text. If row START is
15083 non-null, start searching with that row. IT gives the dimensions
15084 of the display. Value is null if matrix is empty; otherwise it is
15085 a pointer to the row found. */
15086
15087 static struct glyph_row *
15088 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15089 struct glyph_row *start)
15090 {
15091 struct glyph_row *row, *row_found;
15092
15093 /* Set row_found to the last row in IT->w's current matrix
15094 displaying text. The loop looks funny but think of partially
15095 visible lines. */
15096 row_found = NULL;
15097 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15098 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15099 {
15100 xassert (row->enabled_p);
15101 row_found = row;
15102 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15103 break;
15104 ++row;
15105 }
15106
15107 return row_found;
15108 }
15109
15110
15111 /* Return the last row in the current matrix of W that is not affected
15112 by changes at the start of current_buffer that occurred since W's
15113 current matrix was built. Value is null if no such row exists.
15114
15115 BEG_UNCHANGED us the number of characters unchanged at the start of
15116 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15117 first changed character in current_buffer. Characters at positions <
15118 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15119 when the current matrix was built. */
15120
15121 static struct glyph_row *
15122 find_last_unchanged_at_beg_row (struct window *w)
15123 {
15124 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15125 struct glyph_row *row;
15126 struct glyph_row *row_found = NULL;
15127 int yb = window_text_bottom_y (w);
15128
15129 /* Find the last row displaying unchanged text. */
15130 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15131 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15132 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15133 ++row)
15134 {
15135 if (/* If row ends before first_changed_pos, it is unchanged,
15136 except in some case. */
15137 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15138 /* When row ends in ZV and we write at ZV it is not
15139 unchanged. */
15140 && !row->ends_at_zv_p
15141 /* When first_changed_pos is the end of a continued line,
15142 row is not unchanged because it may be no longer
15143 continued. */
15144 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15145 && (row->continued_p
15146 || row->exact_window_width_line_p)))
15147 row_found = row;
15148
15149 /* Stop if last visible row. */
15150 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15151 break;
15152 }
15153
15154 return row_found;
15155 }
15156
15157
15158 /* Find the first glyph row in the current matrix of W that is not
15159 affected by changes at the end of current_buffer since the
15160 time W's current matrix was built.
15161
15162 Return in *DELTA the number of chars by which buffer positions in
15163 unchanged text at the end of current_buffer must be adjusted.
15164
15165 Return in *DELTA_BYTES the corresponding number of bytes.
15166
15167 Value is null if no such row exists, i.e. all rows are affected by
15168 changes. */
15169
15170 static struct glyph_row *
15171 find_first_unchanged_at_end_row (struct window *w,
15172 EMACS_INT *delta, EMACS_INT *delta_bytes)
15173 {
15174 struct glyph_row *row;
15175 struct glyph_row *row_found = NULL;
15176
15177 *delta = *delta_bytes = 0;
15178
15179 /* Display must not have been paused, otherwise the current matrix
15180 is not up to date. */
15181 eassert (!NILP (w->window_end_valid));
15182
15183 /* A value of window_end_pos >= END_UNCHANGED means that the window
15184 end is in the range of changed text. If so, there is no
15185 unchanged row at the end of W's current matrix. */
15186 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15187 return NULL;
15188
15189 /* Set row to the last row in W's current matrix displaying text. */
15190 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15191
15192 /* If matrix is entirely empty, no unchanged row exists. */
15193 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15194 {
15195 /* The value of row is the last glyph row in the matrix having a
15196 meaningful buffer position in it. The end position of row
15197 corresponds to window_end_pos. This allows us to translate
15198 buffer positions in the current matrix to current buffer
15199 positions for characters not in changed text. */
15200 EMACS_INT Z_old =
15201 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15202 EMACS_INT Z_BYTE_old =
15203 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15204 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15205 struct glyph_row *first_text_row
15206 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15207
15208 *delta = Z - Z_old;
15209 *delta_bytes = Z_BYTE - Z_BYTE_old;
15210
15211 /* Set last_unchanged_pos to the buffer position of the last
15212 character in the buffer that has not been changed. Z is the
15213 index + 1 of the last character in current_buffer, i.e. by
15214 subtracting END_UNCHANGED we get the index of the last
15215 unchanged character, and we have to add BEG to get its buffer
15216 position. */
15217 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15218 last_unchanged_pos_old = last_unchanged_pos - *delta;
15219
15220 /* Search backward from ROW for a row displaying a line that
15221 starts at a minimum position >= last_unchanged_pos_old. */
15222 for (; row > first_text_row; --row)
15223 {
15224 /* This used to abort, but it can happen.
15225 It is ok to just stop the search instead here. KFS. */
15226 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15227 break;
15228
15229 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15230 row_found = row;
15231 }
15232 }
15233
15234 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15235
15236 return row_found;
15237 }
15238
15239
15240 /* Make sure that glyph rows in the current matrix of window W
15241 reference the same glyph memory as corresponding rows in the
15242 frame's frame matrix. This function is called after scrolling W's
15243 current matrix on a terminal frame in try_window_id and
15244 try_window_reusing_current_matrix. */
15245
15246 static void
15247 sync_frame_with_window_matrix_rows (struct window *w)
15248 {
15249 struct frame *f = XFRAME (w->frame);
15250 struct glyph_row *window_row, *window_row_end, *frame_row;
15251
15252 /* Preconditions: W must be a leaf window and full-width. Its frame
15253 must have a frame matrix. */
15254 xassert (NILP (w->hchild) && NILP (w->vchild));
15255 xassert (WINDOW_FULL_WIDTH_P (w));
15256 xassert (!FRAME_WINDOW_P (f));
15257
15258 /* If W is a full-width window, glyph pointers in W's current matrix
15259 have, by definition, to be the same as glyph pointers in the
15260 corresponding frame matrix. Note that frame matrices have no
15261 marginal areas (see build_frame_matrix). */
15262 window_row = w->current_matrix->rows;
15263 window_row_end = window_row + w->current_matrix->nrows;
15264 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15265 while (window_row < window_row_end)
15266 {
15267 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15268 struct glyph *end = window_row->glyphs[LAST_AREA];
15269
15270 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15271 frame_row->glyphs[TEXT_AREA] = start;
15272 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15273 frame_row->glyphs[LAST_AREA] = end;
15274
15275 /* Disable frame rows whose corresponding window rows have
15276 been disabled in try_window_id. */
15277 if (!window_row->enabled_p)
15278 frame_row->enabled_p = 0;
15279
15280 ++window_row, ++frame_row;
15281 }
15282 }
15283
15284
15285 /* Find the glyph row in window W containing CHARPOS. Consider all
15286 rows between START and END (not inclusive). END null means search
15287 all rows to the end of the display area of W. Value is the row
15288 containing CHARPOS or null. */
15289
15290 struct glyph_row *
15291 row_containing_pos (struct window *w, EMACS_INT charpos,
15292 struct glyph_row *start, struct glyph_row *end, int dy)
15293 {
15294 struct glyph_row *row = start;
15295 struct glyph_row *best_row = NULL;
15296 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15297 int last_y;
15298
15299 /* If we happen to start on a header-line, skip that. */
15300 if (row->mode_line_p)
15301 ++row;
15302
15303 if ((end && row >= end) || !row->enabled_p)
15304 return NULL;
15305
15306 last_y = window_text_bottom_y (w) - dy;
15307
15308 while (1)
15309 {
15310 /* Give up if we have gone too far. */
15311 if (end && row >= end)
15312 return NULL;
15313 /* This formerly returned if they were equal.
15314 I think that both quantities are of a "last plus one" type;
15315 if so, when they are equal, the row is within the screen. -- rms. */
15316 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15317 return NULL;
15318
15319 /* If it is in this row, return this row. */
15320 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15321 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15322 /* The end position of a row equals the start
15323 position of the next row. If CHARPOS is there, we
15324 would rather display it in the next line, except
15325 when this line ends in ZV. */
15326 && !row->ends_at_zv_p
15327 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15328 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15329 {
15330 struct glyph *g;
15331
15332 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15333 || (!best_row && !row->continued_p))
15334 return row;
15335 /* In bidi-reordered rows, there could be several rows
15336 occluding point, all of them belonging to the same
15337 continued line. We need to find the row which fits
15338 CHARPOS the best. */
15339 for (g = row->glyphs[TEXT_AREA];
15340 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15341 g++)
15342 {
15343 if (!STRINGP (g->object))
15344 {
15345 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15346 {
15347 mindif = eabs (g->charpos - charpos);
15348 best_row = row;
15349 /* Exact match always wins. */
15350 if (mindif == 0)
15351 return best_row;
15352 }
15353 }
15354 }
15355 }
15356 else if (best_row && !row->continued_p)
15357 return best_row;
15358 ++row;
15359 }
15360 }
15361
15362
15363 /* Try to redisplay window W by reusing its existing display. W's
15364 current matrix must be up to date when this function is called,
15365 i.e. window_end_valid must not be nil.
15366
15367 Value is
15368
15369 1 if display has been updated
15370 0 if otherwise unsuccessful
15371 -1 if redisplay with same window start is known not to succeed
15372
15373 The following steps are performed:
15374
15375 1. Find the last row in the current matrix of W that is not
15376 affected by changes at the start of current_buffer. If no such row
15377 is found, give up.
15378
15379 2. Find the first row in W's current matrix that is not affected by
15380 changes at the end of current_buffer. Maybe there is no such row.
15381
15382 3. Display lines beginning with the row + 1 found in step 1 to the
15383 row found in step 2 or, if step 2 didn't find a row, to the end of
15384 the window.
15385
15386 4. If cursor is not known to appear on the window, give up.
15387
15388 5. If display stopped at the row found in step 2, scroll the
15389 display and current matrix as needed.
15390
15391 6. Maybe display some lines at the end of W, if we must. This can
15392 happen under various circumstances, like a partially visible line
15393 becoming fully visible, or because newly displayed lines are displayed
15394 in smaller font sizes.
15395
15396 7. Update W's window end information. */
15397
15398 static int
15399 try_window_id (struct window *w)
15400 {
15401 struct frame *f = XFRAME (w->frame);
15402 struct glyph_matrix *current_matrix = w->current_matrix;
15403 struct glyph_matrix *desired_matrix = w->desired_matrix;
15404 struct glyph_row *last_unchanged_at_beg_row;
15405 struct glyph_row *first_unchanged_at_end_row;
15406 struct glyph_row *row;
15407 struct glyph_row *bottom_row;
15408 int bottom_vpos;
15409 struct it it;
15410 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15411 int dvpos, dy;
15412 struct text_pos start_pos;
15413 struct run run;
15414 int first_unchanged_at_end_vpos = 0;
15415 struct glyph_row *last_text_row, *last_text_row_at_end;
15416 struct text_pos start;
15417 EMACS_INT first_changed_charpos, last_changed_charpos;
15418
15419 #if GLYPH_DEBUG
15420 if (inhibit_try_window_id)
15421 return 0;
15422 #endif
15423
15424 /* This is handy for debugging. */
15425 #if 0
15426 #define GIVE_UP(X) \
15427 do { \
15428 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15429 return 0; \
15430 } while (0)
15431 #else
15432 #define GIVE_UP(X) return 0
15433 #endif
15434
15435 SET_TEXT_POS_FROM_MARKER (start, w->start);
15436
15437 /* Don't use this for mini-windows because these can show
15438 messages and mini-buffers, and we don't handle that here. */
15439 if (MINI_WINDOW_P (w))
15440 GIVE_UP (1);
15441
15442 /* This flag is used to prevent redisplay optimizations. */
15443 if (windows_or_buffers_changed || cursor_type_changed)
15444 GIVE_UP (2);
15445
15446 /* Verify that narrowing has not changed.
15447 Also verify that we were not told to prevent redisplay optimizations.
15448 It would be nice to further
15449 reduce the number of cases where this prevents try_window_id. */
15450 if (current_buffer->clip_changed
15451 || current_buffer->prevent_redisplay_optimizations_p)
15452 GIVE_UP (3);
15453
15454 /* Window must either use window-based redisplay or be full width. */
15455 if (!FRAME_WINDOW_P (f)
15456 && (!FRAME_LINE_INS_DEL_OK (f)
15457 || !WINDOW_FULL_WIDTH_P (w)))
15458 GIVE_UP (4);
15459
15460 /* Give up if point is known NOT to appear in W. */
15461 if (PT < CHARPOS (start))
15462 GIVE_UP (5);
15463
15464 /* Another way to prevent redisplay optimizations. */
15465 if (XFASTINT (w->last_modified) == 0)
15466 GIVE_UP (6);
15467
15468 /* Verify that window is not hscrolled. */
15469 if (XFASTINT (w->hscroll) != 0)
15470 GIVE_UP (7);
15471
15472 /* Verify that display wasn't paused. */
15473 if (NILP (w->window_end_valid))
15474 GIVE_UP (8);
15475
15476 /* Can't use this if highlighting a region because a cursor movement
15477 will do more than just set the cursor. */
15478 if (!NILP (Vtransient_mark_mode)
15479 && !NILP (BVAR (current_buffer, mark_active)))
15480 GIVE_UP (9);
15481
15482 /* Likewise if highlighting trailing whitespace. */
15483 if (!NILP (Vshow_trailing_whitespace))
15484 GIVE_UP (11);
15485
15486 /* Likewise if showing a region. */
15487 if (!NILP (w->region_showing))
15488 GIVE_UP (10);
15489
15490 /* Can't use this if overlay arrow position and/or string have
15491 changed. */
15492 if (overlay_arrows_changed_p ())
15493 GIVE_UP (12);
15494
15495 /* When word-wrap is on, adding a space to the first word of a
15496 wrapped line can change the wrap position, altering the line
15497 above it. It might be worthwhile to handle this more
15498 intelligently, but for now just redisplay from scratch. */
15499 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
15500 GIVE_UP (21);
15501
15502 /* Under bidi reordering, adding or deleting a character in the
15503 beginning of a paragraph, before the first strong directional
15504 character, can change the base direction of the paragraph (unless
15505 the buffer specifies a fixed paragraph direction), which will
15506 require to redisplay the whole paragraph. It might be worthwhile
15507 to find the paragraph limits and widen the range of redisplayed
15508 lines to that, but for now just give up this optimization and
15509 redisplay from scratch. */
15510 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15511 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
15512 GIVE_UP (22);
15513
15514 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15515 only if buffer has really changed. The reason is that the gap is
15516 initially at Z for freshly visited files. The code below would
15517 set end_unchanged to 0 in that case. */
15518 if (MODIFF > SAVE_MODIFF
15519 /* This seems to happen sometimes after saving a buffer. */
15520 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15521 {
15522 if (GPT - BEG < BEG_UNCHANGED)
15523 BEG_UNCHANGED = GPT - BEG;
15524 if (Z - GPT < END_UNCHANGED)
15525 END_UNCHANGED = Z - GPT;
15526 }
15527
15528 /* The position of the first and last character that has been changed. */
15529 first_changed_charpos = BEG + BEG_UNCHANGED;
15530 last_changed_charpos = Z - END_UNCHANGED;
15531
15532 /* If window starts after a line end, and the last change is in
15533 front of that newline, then changes don't affect the display.
15534 This case happens with stealth-fontification. Note that although
15535 the display is unchanged, glyph positions in the matrix have to
15536 be adjusted, of course. */
15537 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15538 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15539 && ((last_changed_charpos < CHARPOS (start)
15540 && CHARPOS (start) == BEGV)
15541 || (last_changed_charpos < CHARPOS (start) - 1
15542 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15543 {
15544 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
15545 struct glyph_row *r0;
15546
15547 /* Compute how many chars/bytes have been added to or removed
15548 from the buffer. */
15549 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15550 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15551 Z_delta = Z - Z_old;
15552 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
15553
15554 /* Give up if PT is not in the window. Note that it already has
15555 been checked at the start of try_window_id that PT is not in
15556 front of the window start. */
15557 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
15558 GIVE_UP (13);
15559
15560 /* If window start is unchanged, we can reuse the whole matrix
15561 as is, after adjusting glyph positions. No need to compute
15562 the window end again, since its offset from Z hasn't changed. */
15563 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15564 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
15565 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
15566 /* PT must not be in a partially visible line. */
15567 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
15568 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15569 {
15570 /* Adjust positions in the glyph matrix. */
15571 if (Z_delta || Z_delta_bytes)
15572 {
15573 struct glyph_row *r1
15574 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15575 increment_matrix_positions (w->current_matrix,
15576 MATRIX_ROW_VPOS (r0, current_matrix),
15577 MATRIX_ROW_VPOS (r1, current_matrix),
15578 Z_delta, Z_delta_bytes);
15579 }
15580
15581 /* Set the cursor. */
15582 row = row_containing_pos (w, PT, r0, NULL, 0);
15583 if (row)
15584 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15585 else
15586 abort ();
15587 return 1;
15588 }
15589 }
15590
15591 /* Handle the case that changes are all below what is displayed in
15592 the window, and that PT is in the window. This shortcut cannot
15593 be taken if ZV is visible in the window, and text has been added
15594 there that is visible in the window. */
15595 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15596 /* ZV is not visible in the window, or there are no
15597 changes at ZV, actually. */
15598 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15599 || first_changed_charpos == last_changed_charpos))
15600 {
15601 struct glyph_row *r0;
15602
15603 /* Give up if PT is not in the window. Note that it already has
15604 been checked at the start of try_window_id that PT is not in
15605 front of the window start. */
15606 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15607 GIVE_UP (14);
15608
15609 /* If window start is unchanged, we can reuse the whole matrix
15610 as is, without changing glyph positions since no text has
15611 been added/removed in front of the window end. */
15612 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15613 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15614 /* PT must not be in a partially visible line. */
15615 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15616 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15617 {
15618 /* We have to compute the window end anew since text
15619 could have been added/removed after it. */
15620 w->window_end_pos
15621 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15622 w->window_end_bytepos
15623 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15624
15625 /* Set the cursor. */
15626 row = row_containing_pos (w, PT, r0, NULL, 0);
15627 if (row)
15628 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15629 else
15630 abort ();
15631 return 2;
15632 }
15633 }
15634
15635 /* Give up if window start is in the changed area.
15636
15637 The condition used to read
15638
15639 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15640
15641 but why that was tested escapes me at the moment. */
15642 if (CHARPOS (start) >= first_changed_charpos
15643 && CHARPOS (start) <= last_changed_charpos)
15644 GIVE_UP (15);
15645
15646 /* Check that window start agrees with the start of the first glyph
15647 row in its current matrix. Check this after we know the window
15648 start is not in changed text, otherwise positions would not be
15649 comparable. */
15650 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15651 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15652 GIVE_UP (16);
15653
15654 /* Give up if the window ends in strings. Overlay strings
15655 at the end are difficult to handle, so don't try. */
15656 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15657 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15658 GIVE_UP (20);
15659
15660 /* Compute the position at which we have to start displaying new
15661 lines. Some of the lines at the top of the window might be
15662 reusable because they are not displaying changed text. Find the
15663 last row in W's current matrix not affected by changes at the
15664 start of current_buffer. Value is null if changes start in the
15665 first line of window. */
15666 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15667 if (last_unchanged_at_beg_row)
15668 {
15669 /* Avoid starting to display in the moddle of a character, a TAB
15670 for instance. This is easier than to set up the iterator
15671 exactly, and it's not a frequent case, so the additional
15672 effort wouldn't really pay off. */
15673 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15674 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15675 && last_unchanged_at_beg_row > w->current_matrix->rows)
15676 --last_unchanged_at_beg_row;
15677
15678 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15679 GIVE_UP (17);
15680
15681 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15682 GIVE_UP (18);
15683 start_pos = it.current.pos;
15684
15685 /* Start displaying new lines in the desired matrix at the same
15686 vpos we would use in the current matrix, i.e. below
15687 last_unchanged_at_beg_row. */
15688 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15689 current_matrix);
15690 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15691 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15692
15693 xassert (it.hpos == 0 && it.current_x == 0);
15694 }
15695 else
15696 {
15697 /* There are no reusable lines at the start of the window.
15698 Start displaying in the first text line. */
15699 start_display (&it, w, start);
15700 it.vpos = it.first_vpos;
15701 start_pos = it.current.pos;
15702 }
15703
15704 /* Find the first row that is not affected by changes at the end of
15705 the buffer. Value will be null if there is no unchanged row, in
15706 which case we must redisplay to the end of the window. delta
15707 will be set to the value by which buffer positions beginning with
15708 first_unchanged_at_end_row have to be adjusted due to text
15709 changes. */
15710 first_unchanged_at_end_row
15711 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15712 IF_DEBUG (debug_delta = delta);
15713 IF_DEBUG (debug_delta_bytes = delta_bytes);
15714
15715 /* Set stop_pos to the buffer position up to which we will have to
15716 display new lines. If first_unchanged_at_end_row != NULL, this
15717 is the buffer position of the start of the line displayed in that
15718 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15719 that we don't stop at a buffer position. */
15720 stop_pos = 0;
15721 if (first_unchanged_at_end_row)
15722 {
15723 xassert (last_unchanged_at_beg_row == NULL
15724 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15725
15726 /* If this is a continuation line, move forward to the next one
15727 that isn't. Changes in lines above affect this line.
15728 Caution: this may move first_unchanged_at_end_row to a row
15729 not displaying text. */
15730 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15731 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15732 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15733 < it.last_visible_y))
15734 ++first_unchanged_at_end_row;
15735
15736 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15737 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15738 >= it.last_visible_y))
15739 first_unchanged_at_end_row = NULL;
15740 else
15741 {
15742 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15743 + delta);
15744 first_unchanged_at_end_vpos
15745 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15746 xassert (stop_pos >= Z - END_UNCHANGED);
15747 }
15748 }
15749 else if (last_unchanged_at_beg_row == NULL)
15750 GIVE_UP (19);
15751
15752
15753 #if GLYPH_DEBUG
15754
15755 /* Either there is no unchanged row at the end, or the one we have
15756 now displays text. This is a necessary condition for the window
15757 end pos calculation at the end of this function. */
15758 xassert (first_unchanged_at_end_row == NULL
15759 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15760
15761 debug_last_unchanged_at_beg_vpos
15762 = (last_unchanged_at_beg_row
15763 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15764 : -1);
15765 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15766
15767 #endif /* GLYPH_DEBUG != 0 */
15768
15769
15770 /* Display new lines. Set last_text_row to the last new line
15771 displayed which has text on it, i.e. might end up as being the
15772 line where the window_end_vpos is. */
15773 w->cursor.vpos = -1;
15774 last_text_row = NULL;
15775 overlay_arrow_seen = 0;
15776 while (it.current_y < it.last_visible_y
15777 && !fonts_changed_p
15778 && (first_unchanged_at_end_row == NULL
15779 || IT_CHARPOS (it) < stop_pos))
15780 {
15781 if (display_line (&it))
15782 last_text_row = it.glyph_row - 1;
15783 }
15784
15785 if (fonts_changed_p)
15786 return -1;
15787
15788
15789 /* Compute differences in buffer positions, y-positions etc. for
15790 lines reused at the bottom of the window. Compute what we can
15791 scroll. */
15792 if (first_unchanged_at_end_row
15793 /* No lines reused because we displayed everything up to the
15794 bottom of the window. */
15795 && it.current_y < it.last_visible_y)
15796 {
15797 dvpos = (it.vpos
15798 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15799 current_matrix));
15800 dy = it.current_y - first_unchanged_at_end_row->y;
15801 run.current_y = first_unchanged_at_end_row->y;
15802 run.desired_y = run.current_y + dy;
15803 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15804 }
15805 else
15806 {
15807 delta = delta_bytes = dvpos = dy
15808 = run.current_y = run.desired_y = run.height = 0;
15809 first_unchanged_at_end_row = NULL;
15810 }
15811 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15812
15813
15814 /* Find the cursor if not already found. We have to decide whether
15815 PT will appear on this window (it sometimes doesn't, but this is
15816 not a very frequent case.) This decision has to be made before
15817 the current matrix is altered. A value of cursor.vpos < 0 means
15818 that PT is either in one of the lines beginning at
15819 first_unchanged_at_end_row or below the window. Don't care for
15820 lines that might be displayed later at the window end; as
15821 mentioned, this is not a frequent case. */
15822 if (w->cursor.vpos < 0)
15823 {
15824 /* Cursor in unchanged rows at the top? */
15825 if (PT < CHARPOS (start_pos)
15826 && last_unchanged_at_beg_row)
15827 {
15828 row = row_containing_pos (w, PT,
15829 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15830 last_unchanged_at_beg_row + 1, 0);
15831 if (row)
15832 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15833 }
15834
15835 /* Start from first_unchanged_at_end_row looking for PT. */
15836 else if (first_unchanged_at_end_row)
15837 {
15838 row = row_containing_pos (w, PT - delta,
15839 first_unchanged_at_end_row, NULL, 0);
15840 if (row)
15841 set_cursor_from_row (w, row, w->current_matrix, delta,
15842 delta_bytes, dy, dvpos);
15843 }
15844
15845 /* Give up if cursor was not found. */
15846 if (w->cursor.vpos < 0)
15847 {
15848 clear_glyph_matrix (w->desired_matrix);
15849 return -1;
15850 }
15851 }
15852
15853 /* Don't let the cursor end in the scroll margins. */
15854 {
15855 int this_scroll_margin, cursor_height;
15856
15857 this_scroll_margin = max (0, scroll_margin);
15858 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15859 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15860 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15861
15862 if ((w->cursor.y < this_scroll_margin
15863 && CHARPOS (start) > BEGV)
15864 /* Old redisplay didn't take scroll margin into account at the bottom,
15865 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15866 || (w->cursor.y + (make_cursor_line_fully_visible_p
15867 ? cursor_height + this_scroll_margin
15868 : 1)) > it.last_visible_y)
15869 {
15870 w->cursor.vpos = -1;
15871 clear_glyph_matrix (w->desired_matrix);
15872 return -1;
15873 }
15874 }
15875
15876 /* Scroll the display. Do it before changing the current matrix so
15877 that xterm.c doesn't get confused about where the cursor glyph is
15878 found. */
15879 if (dy && run.height)
15880 {
15881 update_begin (f);
15882
15883 if (FRAME_WINDOW_P (f))
15884 {
15885 FRAME_RIF (f)->update_window_begin_hook (w);
15886 FRAME_RIF (f)->clear_window_mouse_face (w);
15887 FRAME_RIF (f)->scroll_run_hook (w, &run);
15888 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15889 }
15890 else
15891 {
15892 /* Terminal frame. In this case, dvpos gives the number of
15893 lines to scroll by; dvpos < 0 means scroll up. */
15894 int from_vpos
15895 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15896 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
15897 int end = (WINDOW_TOP_EDGE_LINE (w)
15898 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15899 + window_internal_height (w));
15900
15901 #if defined (HAVE_GPM) || defined (MSDOS)
15902 x_clear_window_mouse_face (w);
15903 #endif
15904 /* Perform the operation on the screen. */
15905 if (dvpos > 0)
15906 {
15907 /* Scroll last_unchanged_at_beg_row to the end of the
15908 window down dvpos lines. */
15909 set_terminal_window (f, end);
15910
15911 /* On dumb terminals delete dvpos lines at the end
15912 before inserting dvpos empty lines. */
15913 if (!FRAME_SCROLL_REGION_OK (f))
15914 ins_del_lines (f, end - dvpos, -dvpos);
15915
15916 /* Insert dvpos empty lines in front of
15917 last_unchanged_at_beg_row. */
15918 ins_del_lines (f, from, dvpos);
15919 }
15920 else if (dvpos < 0)
15921 {
15922 /* Scroll up last_unchanged_at_beg_vpos to the end of
15923 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15924 set_terminal_window (f, end);
15925
15926 /* Delete dvpos lines in front of
15927 last_unchanged_at_beg_vpos. ins_del_lines will set
15928 the cursor to the given vpos and emit |dvpos| delete
15929 line sequences. */
15930 ins_del_lines (f, from + dvpos, dvpos);
15931
15932 /* On a dumb terminal insert dvpos empty lines at the
15933 end. */
15934 if (!FRAME_SCROLL_REGION_OK (f))
15935 ins_del_lines (f, end + dvpos, -dvpos);
15936 }
15937
15938 set_terminal_window (f, 0);
15939 }
15940
15941 update_end (f);
15942 }
15943
15944 /* Shift reused rows of the current matrix to the right position.
15945 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15946 text. */
15947 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15948 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15949 if (dvpos < 0)
15950 {
15951 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15952 bottom_vpos, dvpos);
15953 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15954 bottom_vpos, 0);
15955 }
15956 else if (dvpos > 0)
15957 {
15958 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15959 bottom_vpos, dvpos);
15960 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15961 first_unchanged_at_end_vpos + dvpos, 0);
15962 }
15963
15964 /* For frame-based redisplay, make sure that current frame and window
15965 matrix are in sync with respect to glyph memory. */
15966 if (!FRAME_WINDOW_P (f))
15967 sync_frame_with_window_matrix_rows (w);
15968
15969 /* Adjust buffer positions in reused rows. */
15970 if (delta || delta_bytes)
15971 increment_matrix_positions (current_matrix,
15972 first_unchanged_at_end_vpos + dvpos,
15973 bottom_vpos, delta, delta_bytes);
15974
15975 /* Adjust Y positions. */
15976 if (dy)
15977 shift_glyph_matrix (w, current_matrix,
15978 first_unchanged_at_end_vpos + dvpos,
15979 bottom_vpos, dy);
15980
15981 if (first_unchanged_at_end_row)
15982 {
15983 first_unchanged_at_end_row += dvpos;
15984 if (first_unchanged_at_end_row->y >= it.last_visible_y
15985 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15986 first_unchanged_at_end_row = NULL;
15987 }
15988
15989 /* If scrolling up, there may be some lines to display at the end of
15990 the window. */
15991 last_text_row_at_end = NULL;
15992 if (dy < 0)
15993 {
15994 /* Scrolling up can leave for example a partially visible line
15995 at the end of the window to be redisplayed. */
15996 /* Set last_row to the glyph row in the current matrix where the
15997 window end line is found. It has been moved up or down in
15998 the matrix by dvpos. */
15999 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16000 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16001
16002 /* If last_row is the window end line, it should display text. */
16003 xassert (last_row->displays_text_p);
16004
16005 /* If window end line was partially visible before, begin
16006 displaying at that line. Otherwise begin displaying with the
16007 line following it. */
16008 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16009 {
16010 init_to_row_start (&it, w, last_row);
16011 it.vpos = last_vpos;
16012 it.current_y = last_row->y;
16013 }
16014 else
16015 {
16016 init_to_row_end (&it, w, last_row);
16017 it.vpos = 1 + last_vpos;
16018 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16019 ++last_row;
16020 }
16021
16022 /* We may start in a continuation line. If so, we have to
16023 get the right continuation_lines_width and current_x. */
16024 it.continuation_lines_width = last_row->continuation_lines_width;
16025 it.hpos = it.current_x = 0;
16026
16027 /* Display the rest of the lines at the window end. */
16028 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16029 while (it.current_y < it.last_visible_y
16030 && !fonts_changed_p)
16031 {
16032 /* Is it always sure that the display agrees with lines in
16033 the current matrix? I don't think so, so we mark rows
16034 displayed invalid in the current matrix by setting their
16035 enabled_p flag to zero. */
16036 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16037 if (display_line (&it))
16038 last_text_row_at_end = it.glyph_row - 1;
16039 }
16040 }
16041
16042 /* Update window_end_pos and window_end_vpos. */
16043 if (first_unchanged_at_end_row
16044 && !last_text_row_at_end)
16045 {
16046 /* Window end line if one of the preserved rows from the current
16047 matrix. Set row to the last row displaying text in current
16048 matrix starting at first_unchanged_at_end_row, after
16049 scrolling. */
16050 xassert (first_unchanged_at_end_row->displays_text_p);
16051 row = find_last_row_displaying_text (w->current_matrix, &it,
16052 first_unchanged_at_end_row);
16053 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16054
16055 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16056 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16057 w->window_end_vpos
16058 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16059 xassert (w->window_end_bytepos >= 0);
16060 IF_DEBUG (debug_method_add (w, "A"));
16061 }
16062 else if (last_text_row_at_end)
16063 {
16064 w->window_end_pos
16065 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16066 w->window_end_bytepos
16067 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16068 w->window_end_vpos
16069 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16070 xassert (w->window_end_bytepos >= 0);
16071 IF_DEBUG (debug_method_add (w, "B"));
16072 }
16073 else if (last_text_row)
16074 {
16075 /* We have displayed either to the end of the window or at the
16076 end of the window, i.e. the last row with text is to be found
16077 in the desired matrix. */
16078 w->window_end_pos
16079 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16080 w->window_end_bytepos
16081 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16082 w->window_end_vpos
16083 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16084 xassert (w->window_end_bytepos >= 0);
16085 }
16086 else if (first_unchanged_at_end_row == NULL
16087 && last_text_row == NULL
16088 && last_text_row_at_end == NULL)
16089 {
16090 /* Displayed to end of window, but no line containing text was
16091 displayed. Lines were deleted at the end of the window. */
16092 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16093 int vpos = XFASTINT (w->window_end_vpos);
16094 struct glyph_row *current_row = current_matrix->rows + vpos;
16095 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16096
16097 for (row = NULL;
16098 row == NULL && vpos >= first_vpos;
16099 --vpos, --current_row, --desired_row)
16100 {
16101 if (desired_row->enabled_p)
16102 {
16103 if (desired_row->displays_text_p)
16104 row = desired_row;
16105 }
16106 else if (current_row->displays_text_p)
16107 row = current_row;
16108 }
16109
16110 xassert (row != NULL);
16111 w->window_end_vpos = make_number (vpos + 1);
16112 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16113 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16114 xassert (w->window_end_bytepos >= 0);
16115 IF_DEBUG (debug_method_add (w, "C"));
16116 }
16117 else
16118 abort ();
16119
16120 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16121 debug_end_vpos = XFASTINT (w->window_end_vpos));
16122
16123 /* Record that display has not been completed. */
16124 w->window_end_valid = Qnil;
16125 w->desired_matrix->no_scrolling_p = 1;
16126 return 3;
16127
16128 #undef GIVE_UP
16129 }
16130
16131
16132 \f
16133 /***********************************************************************
16134 More debugging support
16135 ***********************************************************************/
16136
16137 #if GLYPH_DEBUG
16138
16139 void dump_glyph_row (struct glyph_row *, int, int);
16140 void dump_glyph_matrix (struct glyph_matrix *, int);
16141 void dump_glyph (struct glyph_row *, struct glyph *, int);
16142
16143
16144 /* Dump the contents of glyph matrix MATRIX on stderr.
16145
16146 GLYPHS 0 means don't show glyph contents.
16147 GLYPHS 1 means show glyphs in short form
16148 GLYPHS > 1 means show glyphs in long form. */
16149
16150 void
16151 dump_glyph_matrix (matrix, glyphs)
16152 struct glyph_matrix *matrix;
16153 int glyphs;
16154 {
16155 int i;
16156 for (i = 0; i < matrix->nrows; ++i)
16157 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16158 }
16159
16160
16161 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16162 the glyph row and area where the glyph comes from. */
16163
16164 void
16165 dump_glyph (row, glyph, area)
16166 struct glyph_row *row;
16167 struct glyph *glyph;
16168 int area;
16169 {
16170 if (glyph->type == CHAR_GLYPH)
16171 {
16172 fprintf (stderr,
16173 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16174 glyph - row->glyphs[TEXT_AREA],
16175 'C',
16176 glyph->charpos,
16177 (BUFFERP (glyph->object)
16178 ? 'B'
16179 : (STRINGP (glyph->object)
16180 ? 'S'
16181 : '-')),
16182 glyph->pixel_width,
16183 glyph->u.ch,
16184 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16185 ? glyph->u.ch
16186 : '.'),
16187 glyph->face_id,
16188 glyph->left_box_line_p,
16189 glyph->right_box_line_p);
16190 }
16191 else if (glyph->type == STRETCH_GLYPH)
16192 {
16193 fprintf (stderr,
16194 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16195 glyph - row->glyphs[TEXT_AREA],
16196 'S',
16197 glyph->charpos,
16198 (BUFFERP (glyph->object)
16199 ? 'B'
16200 : (STRINGP (glyph->object)
16201 ? 'S'
16202 : '-')),
16203 glyph->pixel_width,
16204 0,
16205 '.',
16206 glyph->face_id,
16207 glyph->left_box_line_p,
16208 glyph->right_box_line_p);
16209 }
16210 else if (glyph->type == IMAGE_GLYPH)
16211 {
16212 fprintf (stderr,
16213 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16214 glyph - row->glyphs[TEXT_AREA],
16215 'I',
16216 glyph->charpos,
16217 (BUFFERP (glyph->object)
16218 ? 'B'
16219 : (STRINGP (glyph->object)
16220 ? 'S'
16221 : '-')),
16222 glyph->pixel_width,
16223 glyph->u.img_id,
16224 '.',
16225 glyph->face_id,
16226 glyph->left_box_line_p,
16227 glyph->right_box_line_p);
16228 }
16229 else if (glyph->type == COMPOSITE_GLYPH)
16230 {
16231 fprintf (stderr,
16232 " %5d %4c %6d %c %3d 0x%05x",
16233 glyph - row->glyphs[TEXT_AREA],
16234 '+',
16235 glyph->charpos,
16236 (BUFFERP (glyph->object)
16237 ? 'B'
16238 : (STRINGP (glyph->object)
16239 ? 'S'
16240 : '-')),
16241 glyph->pixel_width,
16242 glyph->u.cmp.id);
16243 if (glyph->u.cmp.automatic)
16244 fprintf (stderr,
16245 "[%d-%d]",
16246 glyph->slice.cmp.from, glyph->slice.cmp.to);
16247 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16248 glyph->face_id,
16249 glyph->left_box_line_p,
16250 glyph->right_box_line_p);
16251 }
16252 }
16253
16254
16255 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16256 GLYPHS 0 means don't show glyph contents.
16257 GLYPHS 1 means show glyphs in short form
16258 GLYPHS > 1 means show glyphs in long form. */
16259
16260 void
16261 dump_glyph_row (row, vpos, glyphs)
16262 struct glyph_row *row;
16263 int vpos, glyphs;
16264 {
16265 if (glyphs != 1)
16266 {
16267 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16268 fprintf (stderr, "======================================================================\n");
16269
16270 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16271 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16272 vpos,
16273 MATRIX_ROW_START_CHARPOS (row),
16274 MATRIX_ROW_END_CHARPOS (row),
16275 row->used[TEXT_AREA],
16276 row->contains_overlapping_glyphs_p,
16277 row->enabled_p,
16278 row->truncated_on_left_p,
16279 row->truncated_on_right_p,
16280 row->continued_p,
16281 MATRIX_ROW_CONTINUATION_LINE_P (row),
16282 row->displays_text_p,
16283 row->ends_at_zv_p,
16284 row->fill_line_p,
16285 row->ends_in_middle_of_char_p,
16286 row->starts_in_middle_of_char_p,
16287 row->mouse_face_p,
16288 row->x,
16289 row->y,
16290 row->pixel_width,
16291 row->height,
16292 row->visible_height,
16293 row->ascent,
16294 row->phys_ascent);
16295 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16296 row->end.overlay_string_index,
16297 row->continuation_lines_width);
16298 fprintf (stderr, "%9d %5d\n",
16299 CHARPOS (row->start.string_pos),
16300 CHARPOS (row->end.string_pos));
16301 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16302 row->end.dpvec_index);
16303 }
16304
16305 if (glyphs > 1)
16306 {
16307 int area;
16308
16309 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16310 {
16311 struct glyph *glyph = row->glyphs[area];
16312 struct glyph *glyph_end = glyph + row->used[area];
16313
16314 /* Glyph for a line end in text. */
16315 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16316 ++glyph_end;
16317
16318 if (glyph < glyph_end)
16319 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16320
16321 for (; glyph < glyph_end; ++glyph)
16322 dump_glyph (row, glyph, area);
16323 }
16324 }
16325 else if (glyphs == 1)
16326 {
16327 int area;
16328
16329 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16330 {
16331 char *s = (char *) alloca (row->used[area] + 1);
16332 int i;
16333
16334 for (i = 0; i < row->used[area]; ++i)
16335 {
16336 struct glyph *glyph = row->glyphs[area] + i;
16337 if (glyph->type == CHAR_GLYPH
16338 && glyph->u.ch < 0x80
16339 && glyph->u.ch >= ' ')
16340 s[i] = glyph->u.ch;
16341 else
16342 s[i] = '.';
16343 }
16344
16345 s[i] = '\0';
16346 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16347 }
16348 }
16349 }
16350
16351
16352 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16353 Sdump_glyph_matrix, 0, 1, "p",
16354 doc: /* Dump the current matrix of the selected window to stderr.
16355 Shows contents of glyph row structures. With non-nil
16356 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16357 glyphs in short form, otherwise show glyphs in long form. */)
16358 (Lisp_Object glyphs)
16359 {
16360 struct window *w = XWINDOW (selected_window);
16361 struct buffer *buffer = XBUFFER (w->buffer);
16362
16363 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16364 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16365 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16366 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16367 fprintf (stderr, "=============================================\n");
16368 dump_glyph_matrix (w->current_matrix,
16369 NILP (glyphs) ? 0 : XINT (glyphs));
16370 return Qnil;
16371 }
16372
16373
16374 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16375 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16376 (void)
16377 {
16378 struct frame *f = XFRAME (selected_frame);
16379 dump_glyph_matrix (f->current_matrix, 1);
16380 return Qnil;
16381 }
16382
16383
16384 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16385 doc: /* Dump glyph row ROW to stderr.
16386 GLYPH 0 means don't dump glyphs.
16387 GLYPH 1 means dump glyphs in short form.
16388 GLYPH > 1 or omitted means dump glyphs in long form. */)
16389 (Lisp_Object row, Lisp_Object glyphs)
16390 {
16391 struct glyph_matrix *matrix;
16392 int vpos;
16393
16394 CHECK_NUMBER (row);
16395 matrix = XWINDOW (selected_window)->current_matrix;
16396 vpos = XINT (row);
16397 if (vpos >= 0 && vpos < matrix->nrows)
16398 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16399 vpos,
16400 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16401 return Qnil;
16402 }
16403
16404
16405 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16406 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16407 GLYPH 0 means don't dump glyphs.
16408 GLYPH 1 means dump glyphs in short form.
16409 GLYPH > 1 or omitted means dump glyphs in long form. */)
16410 (Lisp_Object row, Lisp_Object glyphs)
16411 {
16412 struct frame *sf = SELECTED_FRAME ();
16413 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16414 int vpos;
16415
16416 CHECK_NUMBER (row);
16417 vpos = XINT (row);
16418 if (vpos >= 0 && vpos < m->nrows)
16419 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16420 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16421 return Qnil;
16422 }
16423
16424
16425 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16426 doc: /* Toggle tracing of redisplay.
16427 With ARG, turn tracing on if and only if ARG is positive. */)
16428 (Lisp_Object arg)
16429 {
16430 if (NILP (arg))
16431 trace_redisplay_p = !trace_redisplay_p;
16432 else
16433 {
16434 arg = Fprefix_numeric_value (arg);
16435 trace_redisplay_p = XINT (arg) > 0;
16436 }
16437
16438 return Qnil;
16439 }
16440
16441
16442 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16443 doc: /* Like `format', but print result to stderr.
16444 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16445 (size_t nargs, Lisp_Object *args)
16446 {
16447 Lisp_Object s = Fformat (nargs, args);
16448 fprintf (stderr, "%s", SDATA (s));
16449 return Qnil;
16450 }
16451
16452 #endif /* GLYPH_DEBUG */
16453
16454
16455 \f
16456 /***********************************************************************
16457 Building Desired Matrix Rows
16458 ***********************************************************************/
16459
16460 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16461 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16462
16463 static struct glyph_row *
16464 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16465 {
16466 struct frame *f = XFRAME (WINDOW_FRAME (w));
16467 struct buffer *buffer = XBUFFER (w->buffer);
16468 struct buffer *old = current_buffer;
16469 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16470 int arrow_len = SCHARS (overlay_arrow_string);
16471 const unsigned char *arrow_end = arrow_string + arrow_len;
16472 const unsigned char *p;
16473 struct it it;
16474 int multibyte_p;
16475 int n_glyphs_before;
16476
16477 set_buffer_temp (buffer);
16478 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16479 it.glyph_row->used[TEXT_AREA] = 0;
16480 SET_TEXT_POS (it.position, 0, 0);
16481
16482 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
16483 p = arrow_string;
16484 while (p < arrow_end)
16485 {
16486 Lisp_Object face, ilisp;
16487
16488 /* Get the next character. */
16489 if (multibyte_p)
16490 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16491 else
16492 {
16493 it.c = it.char_to_display = *p, it.len = 1;
16494 if (! ASCII_CHAR_P (it.c))
16495 it.char_to_display = BYTE8_TO_CHAR (it.c);
16496 }
16497 p += it.len;
16498
16499 /* Get its face. */
16500 ilisp = make_number (p - arrow_string);
16501 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16502 it.face_id = compute_char_face (f, it.char_to_display, face);
16503
16504 /* Compute its width, get its glyphs. */
16505 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16506 SET_TEXT_POS (it.position, -1, -1);
16507 PRODUCE_GLYPHS (&it);
16508
16509 /* If this character doesn't fit any more in the line, we have
16510 to remove some glyphs. */
16511 if (it.current_x > it.last_visible_x)
16512 {
16513 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16514 break;
16515 }
16516 }
16517
16518 set_buffer_temp (old);
16519 return it.glyph_row;
16520 }
16521
16522
16523 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16524 glyphs are only inserted for terminal frames since we can't really
16525 win with truncation glyphs when partially visible glyphs are
16526 involved. Which glyphs to insert is determined by
16527 produce_special_glyphs. */
16528
16529 static void
16530 insert_left_trunc_glyphs (struct it *it)
16531 {
16532 struct it truncate_it;
16533 struct glyph *from, *end, *to, *toend;
16534
16535 xassert (!FRAME_WINDOW_P (it->f));
16536
16537 /* Get the truncation glyphs. */
16538 truncate_it = *it;
16539 truncate_it.current_x = 0;
16540 truncate_it.face_id = DEFAULT_FACE_ID;
16541 truncate_it.glyph_row = &scratch_glyph_row;
16542 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16543 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16544 truncate_it.object = make_number (0);
16545 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16546
16547 /* Overwrite glyphs from IT with truncation glyphs. */
16548 if (!it->glyph_row->reversed_p)
16549 {
16550 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16551 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16552 to = it->glyph_row->glyphs[TEXT_AREA];
16553 toend = to + it->glyph_row->used[TEXT_AREA];
16554
16555 while (from < end)
16556 *to++ = *from++;
16557
16558 /* There may be padding glyphs left over. Overwrite them too. */
16559 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16560 {
16561 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16562 while (from < end)
16563 *to++ = *from++;
16564 }
16565
16566 if (to > toend)
16567 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16568 }
16569 else
16570 {
16571 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16572 that back to front. */
16573 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16574 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16575 toend = it->glyph_row->glyphs[TEXT_AREA];
16576 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16577
16578 while (from >= end && to >= toend)
16579 *to-- = *from--;
16580 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16581 {
16582 from =
16583 truncate_it.glyph_row->glyphs[TEXT_AREA]
16584 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16585 while (from >= end && to >= toend)
16586 *to-- = *from--;
16587 }
16588 if (from >= end)
16589 {
16590 /* Need to free some room before prepending additional
16591 glyphs. */
16592 int move_by = from - end + 1;
16593 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16594 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16595
16596 for ( ; g >= g0; g--)
16597 g[move_by] = *g;
16598 while (from >= end)
16599 *to-- = *from--;
16600 it->glyph_row->used[TEXT_AREA] += move_by;
16601 }
16602 }
16603 }
16604
16605
16606 /* Compute the pixel height and width of IT->glyph_row.
16607
16608 Most of the time, ascent and height of a display line will be equal
16609 to the max_ascent and max_height values of the display iterator
16610 structure. This is not the case if
16611
16612 1. We hit ZV without displaying anything. In this case, max_ascent
16613 and max_height will be zero.
16614
16615 2. We have some glyphs that don't contribute to the line height.
16616 (The glyph row flag contributes_to_line_height_p is for future
16617 pixmap extensions).
16618
16619 The first case is easily covered by using default values because in
16620 these cases, the line height does not really matter, except that it
16621 must not be zero. */
16622
16623 static void
16624 compute_line_metrics (struct it *it)
16625 {
16626 struct glyph_row *row = it->glyph_row;
16627
16628 if (FRAME_WINDOW_P (it->f))
16629 {
16630 int i, min_y, max_y;
16631
16632 /* The line may consist of one space only, that was added to
16633 place the cursor on it. If so, the row's height hasn't been
16634 computed yet. */
16635 if (row->height == 0)
16636 {
16637 if (it->max_ascent + it->max_descent == 0)
16638 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16639 row->ascent = it->max_ascent;
16640 row->height = it->max_ascent + it->max_descent;
16641 row->phys_ascent = it->max_phys_ascent;
16642 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16643 row->extra_line_spacing = it->max_extra_line_spacing;
16644 }
16645
16646 /* Compute the width of this line. */
16647 row->pixel_width = row->x;
16648 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16649 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16650
16651 xassert (row->pixel_width >= 0);
16652 xassert (row->ascent >= 0 && row->height > 0);
16653
16654 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16655 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16656
16657 /* If first line's physical ascent is larger than its logical
16658 ascent, use the physical ascent, and make the row taller.
16659 This makes accented characters fully visible. */
16660 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16661 && row->phys_ascent > row->ascent)
16662 {
16663 row->height += row->phys_ascent - row->ascent;
16664 row->ascent = row->phys_ascent;
16665 }
16666
16667 /* Compute how much of the line is visible. */
16668 row->visible_height = row->height;
16669
16670 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16671 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16672
16673 if (row->y < min_y)
16674 row->visible_height -= min_y - row->y;
16675 if (row->y + row->height > max_y)
16676 row->visible_height -= row->y + row->height - max_y;
16677 }
16678 else
16679 {
16680 row->pixel_width = row->used[TEXT_AREA];
16681 if (row->continued_p)
16682 row->pixel_width -= it->continuation_pixel_width;
16683 else if (row->truncated_on_right_p)
16684 row->pixel_width -= it->truncation_pixel_width;
16685 row->ascent = row->phys_ascent = 0;
16686 row->height = row->phys_height = row->visible_height = 1;
16687 row->extra_line_spacing = 0;
16688 }
16689
16690 /* Compute a hash code for this row. */
16691 {
16692 int area, i;
16693 row->hash = 0;
16694 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16695 for (i = 0; i < row->used[area]; ++i)
16696 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16697 + row->glyphs[area][i].u.val
16698 + row->glyphs[area][i].face_id
16699 + row->glyphs[area][i].padding_p
16700 + (row->glyphs[area][i].type << 2));
16701 }
16702
16703 it->max_ascent = it->max_descent = 0;
16704 it->max_phys_ascent = it->max_phys_descent = 0;
16705 }
16706
16707
16708 /* Append one space to the glyph row of iterator IT if doing a
16709 window-based redisplay. The space has the same face as
16710 IT->face_id. Value is non-zero if a space was added.
16711
16712 This function is called to make sure that there is always one glyph
16713 at the end of a glyph row that the cursor can be set on under
16714 window-systems. (If there weren't such a glyph we would not know
16715 how wide and tall a box cursor should be displayed).
16716
16717 At the same time this space let's a nicely handle clearing to the
16718 end of the line if the row ends in italic text. */
16719
16720 static int
16721 append_space_for_newline (struct it *it, int default_face_p)
16722 {
16723 if (FRAME_WINDOW_P (it->f))
16724 {
16725 int n = it->glyph_row->used[TEXT_AREA];
16726
16727 if (it->glyph_row->glyphs[TEXT_AREA] + n
16728 < it->glyph_row->glyphs[1 + TEXT_AREA])
16729 {
16730 /* Save some values that must not be changed.
16731 Must save IT->c and IT->len because otherwise
16732 ITERATOR_AT_END_P wouldn't work anymore after
16733 append_space_for_newline has been called. */
16734 enum display_element_type saved_what = it->what;
16735 int saved_c = it->c, saved_len = it->len;
16736 int saved_char_to_display = it->char_to_display;
16737 int saved_x = it->current_x;
16738 int saved_face_id = it->face_id;
16739 struct text_pos saved_pos;
16740 Lisp_Object saved_object;
16741 struct face *face;
16742
16743 saved_object = it->object;
16744 saved_pos = it->position;
16745
16746 it->what = IT_CHARACTER;
16747 memset (&it->position, 0, sizeof it->position);
16748 it->object = make_number (0);
16749 it->c = it->char_to_display = ' ';
16750 it->len = 1;
16751
16752 if (default_face_p)
16753 it->face_id = DEFAULT_FACE_ID;
16754 else if (it->face_before_selective_p)
16755 it->face_id = it->saved_face_id;
16756 face = FACE_FROM_ID (it->f, it->face_id);
16757 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16758
16759 PRODUCE_GLYPHS (it);
16760
16761 it->override_ascent = -1;
16762 it->constrain_row_ascent_descent_p = 0;
16763 it->current_x = saved_x;
16764 it->object = saved_object;
16765 it->position = saved_pos;
16766 it->what = saved_what;
16767 it->face_id = saved_face_id;
16768 it->len = saved_len;
16769 it->c = saved_c;
16770 it->char_to_display = saved_char_to_display;
16771 return 1;
16772 }
16773 }
16774
16775 return 0;
16776 }
16777
16778
16779 /* Extend the face of the last glyph in the text area of IT->glyph_row
16780 to the end of the display line. Called from display_line. If the
16781 glyph row is empty, add a space glyph to it so that we know the
16782 face to draw. Set the glyph row flag fill_line_p. If the glyph
16783 row is R2L, prepend a stretch glyph to cover the empty space to the
16784 left of the leftmost glyph. */
16785
16786 static void
16787 extend_face_to_end_of_line (struct it *it)
16788 {
16789 struct face *face;
16790 struct frame *f = it->f;
16791
16792 /* If line is already filled, do nothing. Non window-system frames
16793 get a grace of one more ``pixel'' because their characters are
16794 1-``pixel'' wide, so they hit the equality too early. This grace
16795 is needed only for R2L rows that are not continued, to produce
16796 one extra blank where we could display the cursor. */
16797 if (it->current_x >= it->last_visible_x
16798 + (!FRAME_WINDOW_P (f)
16799 && it->glyph_row->reversed_p
16800 && !it->glyph_row->continued_p))
16801 return;
16802
16803 /* Face extension extends the background and box of IT->face_id
16804 to the end of the line. If the background equals the background
16805 of the frame, we don't have to do anything. */
16806 if (it->face_before_selective_p)
16807 face = FACE_FROM_ID (f, it->saved_face_id);
16808 else
16809 face = FACE_FROM_ID (f, it->face_id);
16810
16811 if (FRAME_WINDOW_P (f)
16812 && it->glyph_row->displays_text_p
16813 && face->box == FACE_NO_BOX
16814 && face->background == FRAME_BACKGROUND_PIXEL (f)
16815 && !face->stipple
16816 && !it->glyph_row->reversed_p)
16817 return;
16818
16819 /* Set the glyph row flag indicating that the face of the last glyph
16820 in the text area has to be drawn to the end of the text area. */
16821 it->glyph_row->fill_line_p = 1;
16822
16823 /* If current character of IT is not ASCII, make sure we have the
16824 ASCII face. This will be automatically undone the next time
16825 get_next_display_element returns a multibyte character. Note
16826 that the character will always be single byte in unibyte
16827 text. */
16828 if (!ASCII_CHAR_P (it->c))
16829 {
16830 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16831 }
16832
16833 if (FRAME_WINDOW_P (f))
16834 {
16835 /* If the row is empty, add a space with the current face of IT,
16836 so that we know which face to draw. */
16837 if (it->glyph_row->used[TEXT_AREA] == 0)
16838 {
16839 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16840 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16841 it->glyph_row->used[TEXT_AREA] = 1;
16842 }
16843 #ifdef HAVE_WINDOW_SYSTEM
16844 if (it->glyph_row->reversed_p)
16845 {
16846 /* Prepend a stretch glyph to the row, such that the
16847 rightmost glyph will be drawn flushed all the way to the
16848 right margin of the window. The stretch glyph that will
16849 occupy the empty space, if any, to the left of the
16850 glyphs. */
16851 struct font *font = face->font ? face->font : FRAME_FONT (f);
16852 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16853 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16854 struct glyph *g;
16855 int row_width, stretch_ascent, stretch_width;
16856 struct text_pos saved_pos;
16857 int saved_face_id, saved_avoid_cursor;
16858
16859 for (row_width = 0, g = row_start; g < row_end; g++)
16860 row_width += g->pixel_width;
16861 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16862 if (stretch_width > 0)
16863 {
16864 stretch_ascent =
16865 (((it->ascent + it->descent)
16866 * FONT_BASE (font)) / FONT_HEIGHT (font));
16867 saved_pos = it->position;
16868 memset (&it->position, 0, sizeof it->position);
16869 saved_avoid_cursor = it->avoid_cursor_p;
16870 it->avoid_cursor_p = 1;
16871 saved_face_id = it->face_id;
16872 /* The last row's stretch glyph should get the default
16873 face, to avoid painting the rest of the window with
16874 the region face, if the region ends at ZV. */
16875 if (it->glyph_row->ends_at_zv_p)
16876 it->face_id = DEFAULT_FACE_ID;
16877 else
16878 it->face_id = face->id;
16879 append_stretch_glyph (it, make_number (0), stretch_width,
16880 it->ascent + it->descent, stretch_ascent);
16881 it->position = saved_pos;
16882 it->avoid_cursor_p = saved_avoid_cursor;
16883 it->face_id = saved_face_id;
16884 }
16885 }
16886 #endif /* HAVE_WINDOW_SYSTEM */
16887 }
16888 else
16889 {
16890 /* Save some values that must not be changed. */
16891 int saved_x = it->current_x;
16892 struct text_pos saved_pos;
16893 Lisp_Object saved_object;
16894 enum display_element_type saved_what = it->what;
16895 int saved_face_id = it->face_id;
16896
16897 saved_object = it->object;
16898 saved_pos = it->position;
16899
16900 it->what = IT_CHARACTER;
16901 memset (&it->position, 0, sizeof it->position);
16902 it->object = make_number (0);
16903 it->c = it->char_to_display = ' ';
16904 it->len = 1;
16905 /* The last row's blank glyphs should get the default face, to
16906 avoid painting the rest of the window with the region face,
16907 if the region ends at ZV. */
16908 if (it->glyph_row->ends_at_zv_p)
16909 it->face_id = DEFAULT_FACE_ID;
16910 else
16911 it->face_id = face->id;
16912
16913 PRODUCE_GLYPHS (it);
16914
16915 while (it->current_x <= it->last_visible_x)
16916 PRODUCE_GLYPHS (it);
16917
16918 /* Don't count these blanks really. It would let us insert a left
16919 truncation glyph below and make us set the cursor on them, maybe. */
16920 it->current_x = saved_x;
16921 it->object = saved_object;
16922 it->position = saved_pos;
16923 it->what = saved_what;
16924 it->face_id = saved_face_id;
16925 }
16926 }
16927
16928
16929 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16930 trailing whitespace. */
16931
16932 static int
16933 trailing_whitespace_p (EMACS_INT charpos)
16934 {
16935 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
16936 int c = 0;
16937
16938 while (bytepos < ZV_BYTE
16939 && (c = FETCH_CHAR (bytepos),
16940 c == ' ' || c == '\t'))
16941 ++bytepos;
16942
16943 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16944 {
16945 if (bytepos != PT_BYTE)
16946 return 1;
16947 }
16948 return 0;
16949 }
16950
16951
16952 /* Highlight trailing whitespace, if any, in ROW. */
16953
16954 void
16955 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
16956 {
16957 int used = row->used[TEXT_AREA];
16958
16959 if (used)
16960 {
16961 struct glyph *start = row->glyphs[TEXT_AREA];
16962 struct glyph *glyph = start + used - 1;
16963
16964 if (row->reversed_p)
16965 {
16966 /* Right-to-left rows need to be processed in the opposite
16967 direction, so swap the edge pointers. */
16968 glyph = start;
16969 start = row->glyphs[TEXT_AREA] + used - 1;
16970 }
16971
16972 /* Skip over glyphs inserted to display the cursor at the
16973 end of a line, for extending the face of the last glyph
16974 to the end of the line on terminals, and for truncation
16975 and continuation glyphs. */
16976 if (!row->reversed_p)
16977 {
16978 while (glyph >= start
16979 && glyph->type == CHAR_GLYPH
16980 && INTEGERP (glyph->object))
16981 --glyph;
16982 }
16983 else
16984 {
16985 while (glyph <= start
16986 && glyph->type == CHAR_GLYPH
16987 && INTEGERP (glyph->object))
16988 ++glyph;
16989 }
16990
16991 /* If last glyph is a space or stretch, and it's trailing
16992 whitespace, set the face of all trailing whitespace glyphs in
16993 IT->glyph_row to `trailing-whitespace'. */
16994 if ((row->reversed_p ? glyph <= start : glyph >= start)
16995 && BUFFERP (glyph->object)
16996 && (glyph->type == STRETCH_GLYPH
16997 || (glyph->type == CHAR_GLYPH
16998 && glyph->u.ch == ' '))
16999 && trailing_whitespace_p (glyph->charpos))
17000 {
17001 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17002 if (face_id < 0)
17003 return;
17004
17005 if (!row->reversed_p)
17006 {
17007 while (glyph >= start
17008 && BUFFERP (glyph->object)
17009 && (glyph->type == STRETCH_GLYPH
17010 || (glyph->type == CHAR_GLYPH
17011 && glyph->u.ch == ' ')))
17012 (glyph--)->face_id = face_id;
17013 }
17014 else
17015 {
17016 while (glyph <= start
17017 && BUFFERP (glyph->object)
17018 && (glyph->type == STRETCH_GLYPH
17019 || (glyph->type == CHAR_GLYPH
17020 && glyph->u.ch == ' ')))
17021 (glyph++)->face_id = face_id;
17022 }
17023 }
17024 }
17025 }
17026
17027
17028 /* Value is non-zero if glyph row ROW should be
17029 used to hold the cursor. */
17030
17031 static int
17032 cursor_row_p (struct glyph_row *row)
17033 {
17034 int result = 1;
17035
17036 if (PT == CHARPOS (row->end.pos))
17037 {
17038 /* Suppose the row ends on a string.
17039 Unless the row is continued, that means it ends on a newline
17040 in the string. If it's anything other than a display string
17041 (e.g. a before-string from an overlay), we don't want the
17042 cursor there. (This heuristic seems to give the optimal
17043 behavior for the various types of multi-line strings.) */
17044 if (CHARPOS (row->end.string_pos) >= 0)
17045 {
17046 if (row->continued_p)
17047 result = 1;
17048 else
17049 {
17050 /* Check for `display' property. */
17051 struct glyph *beg = row->glyphs[TEXT_AREA];
17052 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17053 struct glyph *glyph;
17054
17055 result = 0;
17056 for (glyph = end; glyph >= beg; --glyph)
17057 if (STRINGP (glyph->object))
17058 {
17059 Lisp_Object prop
17060 = Fget_char_property (make_number (PT),
17061 Qdisplay, Qnil);
17062 result =
17063 (!NILP (prop)
17064 && display_prop_string_p (prop, glyph->object));
17065 break;
17066 }
17067 }
17068 }
17069 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17070 {
17071 /* If the row ends in middle of a real character,
17072 and the line is continued, we want the cursor here.
17073 That's because CHARPOS (ROW->end.pos) would equal
17074 PT if PT is before the character. */
17075 if (!row->ends_in_ellipsis_p)
17076 result = row->continued_p;
17077 else
17078 /* If the row ends in an ellipsis, then
17079 CHARPOS (ROW->end.pos) will equal point after the
17080 invisible text. We want that position to be displayed
17081 after the ellipsis. */
17082 result = 0;
17083 }
17084 /* If the row ends at ZV, display the cursor at the end of that
17085 row instead of at the start of the row below. */
17086 else if (row->ends_at_zv_p)
17087 result = 1;
17088 else
17089 result = 0;
17090 }
17091
17092 return result;
17093 }
17094
17095 \f
17096
17097 /* Push the display property PROP so that it will be rendered at the
17098 current position in IT. Return 1 if PROP was successfully pushed,
17099 0 otherwise. */
17100
17101 static int
17102 push_display_prop (struct it *it, Lisp_Object prop)
17103 {
17104 push_it (it);
17105
17106 if (STRINGP (prop))
17107 {
17108 if (SCHARS (prop) == 0)
17109 {
17110 pop_it (it);
17111 return 0;
17112 }
17113
17114 it->string = prop;
17115 it->multibyte_p = STRING_MULTIBYTE (it->string);
17116 it->current.overlay_string_index = -1;
17117 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17118 it->end_charpos = it->string_nchars = SCHARS (it->string);
17119 it->method = GET_FROM_STRING;
17120 it->stop_charpos = 0;
17121 }
17122 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17123 {
17124 it->method = GET_FROM_STRETCH;
17125 it->object = prop;
17126 }
17127 #ifdef HAVE_WINDOW_SYSTEM
17128 else if (IMAGEP (prop))
17129 {
17130 it->what = IT_IMAGE;
17131 it->image_id = lookup_image (it->f, prop);
17132 it->method = GET_FROM_IMAGE;
17133 }
17134 #endif /* HAVE_WINDOW_SYSTEM */
17135 else
17136 {
17137 pop_it (it); /* bogus display property, give up */
17138 return 0;
17139 }
17140
17141 return 1;
17142 }
17143
17144 /* Return the character-property PROP at the current position in IT. */
17145
17146 static Lisp_Object
17147 get_it_property (struct it *it, Lisp_Object prop)
17148 {
17149 Lisp_Object position;
17150
17151 if (STRINGP (it->object))
17152 position = make_number (IT_STRING_CHARPOS (*it));
17153 else if (BUFFERP (it->object))
17154 position = make_number (IT_CHARPOS (*it));
17155 else
17156 return Qnil;
17157
17158 return Fget_char_property (position, prop, it->object);
17159 }
17160
17161 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17162
17163 static void
17164 handle_line_prefix (struct it *it)
17165 {
17166 Lisp_Object prefix;
17167 if (it->continuation_lines_width > 0)
17168 {
17169 prefix = get_it_property (it, Qwrap_prefix);
17170 if (NILP (prefix))
17171 prefix = Vwrap_prefix;
17172 }
17173 else
17174 {
17175 prefix = get_it_property (it, Qline_prefix);
17176 if (NILP (prefix))
17177 prefix = Vline_prefix;
17178 }
17179 if (! NILP (prefix) && push_display_prop (it, prefix))
17180 {
17181 /* If the prefix is wider than the window, and we try to wrap
17182 it, it would acquire its own wrap prefix, and so on till the
17183 iterator stack overflows. So, don't wrap the prefix. */
17184 it->line_wrap = TRUNCATE;
17185 it->avoid_cursor_p = 1;
17186 }
17187 }
17188
17189 \f
17190
17191 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17192 only for R2L lines from display_line, when it decides that too many
17193 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17194 continued. */
17195 static void
17196 unproduce_glyphs (struct it *it, int n)
17197 {
17198 struct glyph *glyph, *end;
17199
17200 xassert (it->glyph_row);
17201 xassert (it->glyph_row->reversed_p);
17202 xassert (it->area == TEXT_AREA);
17203 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17204
17205 if (n > it->glyph_row->used[TEXT_AREA])
17206 n = it->glyph_row->used[TEXT_AREA];
17207 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17208 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17209 for ( ; glyph < end; glyph++)
17210 glyph[-n] = *glyph;
17211 }
17212
17213 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17214 and ROW->maxpos. */
17215 static void
17216 find_row_edges (struct it *it, struct glyph_row *row,
17217 EMACS_INT min_pos, EMACS_INT min_bpos,
17218 EMACS_INT max_pos, EMACS_INT max_bpos)
17219 {
17220 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17221 lines' rows is implemented for bidi-reordered rows. */
17222
17223 /* ROW->minpos is the value of min_pos, the minimal buffer position
17224 we have in ROW. */
17225 if (min_pos <= ZV)
17226 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17227 else
17228 /* We didn't find _any_ valid buffer positions in any of the
17229 glyphs, so we must trust the iterator's computed positions. */
17230 row->minpos = row->start.pos;
17231 if (max_pos <= 0)
17232 {
17233 max_pos = CHARPOS (it->current.pos);
17234 max_bpos = BYTEPOS (it->current.pos);
17235 }
17236
17237 /* Here are the various use-cases for ending the row, and the
17238 corresponding values for ROW->maxpos:
17239
17240 Line ends in a newline from buffer eol_pos + 1
17241 Line is continued from buffer max_pos + 1
17242 Line is truncated on right it->current.pos
17243 Line ends in a newline from string max_pos
17244 Line is continued from string max_pos
17245 Line is continued from display vector max_pos
17246 Line is entirely from a string min_pos == max_pos
17247 Line is entirely from a display vector min_pos == max_pos
17248 Line that ends at ZV ZV
17249
17250 If you discover other use-cases, please add them here as
17251 appropriate. */
17252 if (row->ends_at_zv_p)
17253 row->maxpos = it->current.pos;
17254 else if (row->used[TEXT_AREA])
17255 {
17256 if (row->ends_in_newline_from_string_p)
17257 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17258 else if (CHARPOS (it->eol_pos) > 0)
17259 SET_TEXT_POS (row->maxpos,
17260 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17261 else if (row->continued_p)
17262 {
17263 /* If max_pos is different from IT's current position, it
17264 means IT->method does not belong to the display element
17265 at max_pos. However, it also means that the display
17266 element at max_pos was displayed in its entirety on this
17267 line, which is equivalent to saying that the next line
17268 starts at the next buffer position. */
17269 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17270 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17271 else
17272 {
17273 INC_BOTH (max_pos, max_bpos);
17274 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17275 }
17276 }
17277 else if (row->truncated_on_right_p)
17278 /* display_line already called reseat_at_next_visible_line_start,
17279 which puts the iterator at the beginning of the next line, in
17280 the logical order. */
17281 row->maxpos = it->current.pos;
17282 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17283 /* A line that is entirely from a string/image/stretch... */
17284 row->maxpos = row->minpos;
17285 else
17286 abort ();
17287 }
17288 else
17289 row->maxpos = it->current.pos;
17290 }
17291
17292 /* Construct the glyph row IT->glyph_row in the desired matrix of
17293 IT->w from text at the current position of IT. See dispextern.h
17294 for an overview of struct it. Value is non-zero if
17295 IT->glyph_row displays text, as opposed to a line displaying ZV
17296 only. */
17297
17298 static int
17299 display_line (struct it *it)
17300 {
17301 struct glyph_row *row = it->glyph_row;
17302 Lisp_Object overlay_arrow_string;
17303 struct it wrap_it;
17304 int may_wrap = 0, wrap_x IF_LINT (= 0);
17305 int wrap_row_used = -1;
17306 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
17307 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
17308 int wrap_row_extra_line_spacing IF_LINT (= 0);
17309 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
17310 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
17311 int cvpos;
17312 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17313 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
17314
17315 /* We always start displaying at hpos zero even if hscrolled. */
17316 xassert (it->hpos == 0 && it->current_x == 0);
17317
17318 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17319 >= it->w->desired_matrix->nrows)
17320 {
17321 it->w->nrows_scale_factor++;
17322 fonts_changed_p = 1;
17323 return 0;
17324 }
17325
17326 /* Is IT->w showing the region? */
17327 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17328
17329 /* Clear the result glyph row and enable it. */
17330 prepare_desired_row (row);
17331
17332 row->y = it->current_y;
17333 row->start = it->start;
17334 row->continuation_lines_width = it->continuation_lines_width;
17335 row->displays_text_p = 1;
17336 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17337 it->starts_in_middle_of_char_p = 0;
17338
17339 /* Arrange the overlays nicely for our purposes. Usually, we call
17340 display_line on only one line at a time, in which case this
17341 can't really hurt too much, or we call it on lines which appear
17342 one after another in the buffer, in which case all calls to
17343 recenter_overlay_lists but the first will be pretty cheap. */
17344 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17345
17346 /* Move over display elements that are not visible because we are
17347 hscrolled. This may stop at an x-position < IT->first_visible_x
17348 if the first glyph is partially visible or if we hit a line end. */
17349 if (it->current_x < it->first_visible_x)
17350 {
17351 this_line_min_pos = row->start.pos;
17352 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17353 MOVE_TO_POS | MOVE_TO_X);
17354 /* Record the smallest positions seen while we moved over
17355 display elements that are not visible. This is needed by
17356 redisplay_internal for optimizing the case where the cursor
17357 stays inside the same line. The rest of this function only
17358 considers positions that are actually displayed, so
17359 RECORD_MAX_MIN_POS will not otherwise record positions that
17360 are hscrolled to the left of the left edge of the window. */
17361 min_pos = CHARPOS (this_line_min_pos);
17362 min_bpos = BYTEPOS (this_line_min_pos);
17363 }
17364 else
17365 {
17366 /* We only do this when not calling `move_it_in_display_line_to'
17367 above, because move_it_in_display_line_to calls
17368 handle_line_prefix itself. */
17369 handle_line_prefix (it);
17370 }
17371
17372 /* Get the initial row height. This is either the height of the
17373 text hscrolled, if there is any, or zero. */
17374 row->ascent = it->max_ascent;
17375 row->height = it->max_ascent + it->max_descent;
17376 row->phys_ascent = it->max_phys_ascent;
17377 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17378 row->extra_line_spacing = it->max_extra_line_spacing;
17379
17380 /* Utility macro to record max and min buffer positions seen until now. */
17381 #define RECORD_MAX_MIN_POS(IT) \
17382 do \
17383 { \
17384 if (IT_CHARPOS (*(IT)) < min_pos) \
17385 { \
17386 min_pos = IT_CHARPOS (*(IT)); \
17387 min_bpos = IT_BYTEPOS (*(IT)); \
17388 } \
17389 if (IT_CHARPOS (*(IT)) > max_pos) \
17390 { \
17391 max_pos = IT_CHARPOS (*(IT)); \
17392 max_bpos = IT_BYTEPOS (*(IT)); \
17393 } \
17394 } \
17395 while (0)
17396
17397 /* Loop generating characters. The loop is left with IT on the next
17398 character to display. */
17399 while (1)
17400 {
17401 int n_glyphs_before, hpos_before, x_before;
17402 int x, nglyphs;
17403 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17404
17405 /* Retrieve the next thing to display. Value is zero if end of
17406 buffer reached. */
17407 if (!get_next_display_element (it))
17408 {
17409 /* Maybe add a space at the end of this line that is used to
17410 display the cursor there under X. Set the charpos of the
17411 first glyph of blank lines not corresponding to any text
17412 to -1. */
17413 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17414 row->exact_window_width_line_p = 1;
17415 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17416 || row->used[TEXT_AREA] == 0)
17417 {
17418 row->glyphs[TEXT_AREA]->charpos = -1;
17419 row->displays_text_p = 0;
17420
17421 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
17422 && (!MINI_WINDOW_P (it->w)
17423 || (minibuf_level && EQ (it->window, minibuf_window))))
17424 row->indicate_empty_line_p = 1;
17425 }
17426
17427 it->continuation_lines_width = 0;
17428 row->ends_at_zv_p = 1;
17429 /* A row that displays right-to-left text must always have
17430 its last face extended all the way to the end of line,
17431 even if this row ends in ZV, because we still write to
17432 the screen left to right. */
17433 if (row->reversed_p)
17434 extend_face_to_end_of_line (it);
17435 break;
17436 }
17437
17438 /* Now, get the metrics of what we want to display. This also
17439 generates glyphs in `row' (which is IT->glyph_row). */
17440 n_glyphs_before = row->used[TEXT_AREA];
17441 x = it->current_x;
17442
17443 /* Remember the line height so far in case the next element doesn't
17444 fit on the line. */
17445 if (it->line_wrap != TRUNCATE)
17446 {
17447 ascent = it->max_ascent;
17448 descent = it->max_descent;
17449 phys_ascent = it->max_phys_ascent;
17450 phys_descent = it->max_phys_descent;
17451
17452 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17453 {
17454 if (IT_DISPLAYING_WHITESPACE (it))
17455 may_wrap = 1;
17456 else if (may_wrap)
17457 {
17458 wrap_it = *it;
17459 wrap_x = x;
17460 wrap_row_used = row->used[TEXT_AREA];
17461 wrap_row_ascent = row->ascent;
17462 wrap_row_height = row->height;
17463 wrap_row_phys_ascent = row->phys_ascent;
17464 wrap_row_phys_height = row->phys_height;
17465 wrap_row_extra_line_spacing = row->extra_line_spacing;
17466 wrap_row_min_pos = min_pos;
17467 wrap_row_min_bpos = min_bpos;
17468 wrap_row_max_pos = max_pos;
17469 wrap_row_max_bpos = max_bpos;
17470 may_wrap = 0;
17471 }
17472 }
17473 }
17474
17475 PRODUCE_GLYPHS (it);
17476
17477 /* If this display element was in marginal areas, continue with
17478 the next one. */
17479 if (it->area != TEXT_AREA)
17480 {
17481 row->ascent = max (row->ascent, it->max_ascent);
17482 row->height = max (row->height, it->max_ascent + it->max_descent);
17483 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17484 row->phys_height = max (row->phys_height,
17485 it->max_phys_ascent + it->max_phys_descent);
17486 row->extra_line_spacing = max (row->extra_line_spacing,
17487 it->max_extra_line_spacing);
17488 set_iterator_to_next (it, 1);
17489 continue;
17490 }
17491
17492 /* Does the display element fit on the line? If we truncate
17493 lines, we should draw past the right edge of the window. If
17494 we don't truncate, we want to stop so that we can display the
17495 continuation glyph before the right margin. If lines are
17496 continued, there are two possible strategies for characters
17497 resulting in more than 1 glyph (e.g. tabs): Display as many
17498 glyphs as possible in this line and leave the rest for the
17499 continuation line, or display the whole element in the next
17500 line. Original redisplay did the former, so we do it also. */
17501 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17502 hpos_before = it->hpos;
17503 x_before = x;
17504
17505 if (/* Not a newline. */
17506 nglyphs > 0
17507 /* Glyphs produced fit entirely in the line. */
17508 && it->current_x < it->last_visible_x)
17509 {
17510 it->hpos += nglyphs;
17511 row->ascent = max (row->ascent, it->max_ascent);
17512 row->height = max (row->height, it->max_ascent + it->max_descent);
17513 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17514 row->phys_height = max (row->phys_height,
17515 it->max_phys_ascent + it->max_phys_descent);
17516 row->extra_line_spacing = max (row->extra_line_spacing,
17517 it->max_extra_line_spacing);
17518 if (it->current_x - it->pixel_width < it->first_visible_x)
17519 row->x = x - it->first_visible_x;
17520 /* Record the maximum and minimum buffer positions seen so
17521 far in glyphs that will be displayed by this row. */
17522 if (it->bidi_p)
17523 RECORD_MAX_MIN_POS (it);
17524 }
17525 else
17526 {
17527 int i, new_x;
17528 struct glyph *glyph;
17529
17530 for (i = 0; i < nglyphs; ++i, x = new_x)
17531 {
17532 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17533 new_x = x + glyph->pixel_width;
17534
17535 if (/* Lines are continued. */
17536 it->line_wrap != TRUNCATE
17537 && (/* Glyph doesn't fit on the line. */
17538 new_x > it->last_visible_x
17539 /* Or it fits exactly on a window system frame. */
17540 || (new_x == it->last_visible_x
17541 && FRAME_WINDOW_P (it->f))))
17542 {
17543 /* End of a continued line. */
17544
17545 if (it->hpos == 0
17546 || (new_x == it->last_visible_x
17547 && FRAME_WINDOW_P (it->f)))
17548 {
17549 /* Current glyph is the only one on the line or
17550 fits exactly on the line. We must continue
17551 the line because we can't draw the cursor
17552 after the glyph. */
17553 row->continued_p = 1;
17554 it->current_x = new_x;
17555 it->continuation_lines_width += new_x;
17556 ++it->hpos;
17557 /* Record the maximum and minimum buffer
17558 positions seen so far in glyphs that will be
17559 displayed by this row. */
17560 if (it->bidi_p)
17561 RECORD_MAX_MIN_POS (it);
17562 if (i == nglyphs - 1)
17563 {
17564 /* If line-wrap is on, check if a previous
17565 wrap point was found. */
17566 if (wrap_row_used > 0
17567 /* Even if there is a previous wrap
17568 point, continue the line here as
17569 usual, if (i) the previous character
17570 was a space or tab AND (ii) the
17571 current character is not. */
17572 && (!may_wrap
17573 || IT_DISPLAYING_WHITESPACE (it)))
17574 goto back_to_wrap;
17575
17576 set_iterator_to_next (it, 1);
17577 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17578 {
17579 if (!get_next_display_element (it))
17580 {
17581 row->exact_window_width_line_p = 1;
17582 it->continuation_lines_width = 0;
17583 row->continued_p = 0;
17584 row->ends_at_zv_p = 1;
17585 }
17586 else if (ITERATOR_AT_END_OF_LINE_P (it))
17587 {
17588 row->continued_p = 0;
17589 row->exact_window_width_line_p = 1;
17590 }
17591 }
17592 }
17593 }
17594 else if (CHAR_GLYPH_PADDING_P (*glyph)
17595 && !FRAME_WINDOW_P (it->f))
17596 {
17597 /* A padding glyph that doesn't fit on this line.
17598 This means the whole character doesn't fit
17599 on the line. */
17600 if (row->reversed_p)
17601 unproduce_glyphs (it, row->used[TEXT_AREA]
17602 - n_glyphs_before);
17603 row->used[TEXT_AREA] = n_glyphs_before;
17604
17605 /* Fill the rest of the row with continuation
17606 glyphs like in 20.x. */
17607 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17608 < row->glyphs[1 + TEXT_AREA])
17609 produce_special_glyphs (it, IT_CONTINUATION);
17610
17611 row->continued_p = 1;
17612 it->current_x = x_before;
17613 it->continuation_lines_width += x_before;
17614
17615 /* Restore the height to what it was before the
17616 element not fitting on the line. */
17617 it->max_ascent = ascent;
17618 it->max_descent = descent;
17619 it->max_phys_ascent = phys_ascent;
17620 it->max_phys_descent = phys_descent;
17621 }
17622 else if (wrap_row_used > 0)
17623 {
17624 back_to_wrap:
17625 if (row->reversed_p)
17626 unproduce_glyphs (it,
17627 row->used[TEXT_AREA] - wrap_row_used);
17628 *it = wrap_it;
17629 it->continuation_lines_width += wrap_x;
17630 row->used[TEXT_AREA] = wrap_row_used;
17631 row->ascent = wrap_row_ascent;
17632 row->height = wrap_row_height;
17633 row->phys_ascent = wrap_row_phys_ascent;
17634 row->phys_height = wrap_row_phys_height;
17635 row->extra_line_spacing = wrap_row_extra_line_spacing;
17636 min_pos = wrap_row_min_pos;
17637 min_bpos = wrap_row_min_bpos;
17638 max_pos = wrap_row_max_pos;
17639 max_bpos = wrap_row_max_bpos;
17640 row->continued_p = 1;
17641 row->ends_at_zv_p = 0;
17642 row->exact_window_width_line_p = 0;
17643 it->continuation_lines_width += x;
17644
17645 /* Make sure that a non-default face is extended
17646 up to the right margin of the window. */
17647 extend_face_to_end_of_line (it);
17648 }
17649 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17650 {
17651 /* A TAB that extends past the right edge of the
17652 window. This produces a single glyph on
17653 window system frames. We leave the glyph in
17654 this row and let it fill the row, but don't
17655 consume the TAB. */
17656 it->continuation_lines_width += it->last_visible_x;
17657 row->ends_in_middle_of_char_p = 1;
17658 row->continued_p = 1;
17659 glyph->pixel_width = it->last_visible_x - x;
17660 it->starts_in_middle_of_char_p = 1;
17661 }
17662 else
17663 {
17664 /* Something other than a TAB that draws past
17665 the right edge of the window. Restore
17666 positions to values before the element. */
17667 if (row->reversed_p)
17668 unproduce_glyphs (it, row->used[TEXT_AREA]
17669 - (n_glyphs_before + i));
17670 row->used[TEXT_AREA] = n_glyphs_before + i;
17671
17672 /* Display continuation glyphs. */
17673 if (!FRAME_WINDOW_P (it->f))
17674 produce_special_glyphs (it, IT_CONTINUATION);
17675 row->continued_p = 1;
17676
17677 it->current_x = x_before;
17678 it->continuation_lines_width += x;
17679 extend_face_to_end_of_line (it);
17680
17681 if (nglyphs > 1 && i > 0)
17682 {
17683 row->ends_in_middle_of_char_p = 1;
17684 it->starts_in_middle_of_char_p = 1;
17685 }
17686
17687 /* Restore the height to what it was before the
17688 element not fitting on the line. */
17689 it->max_ascent = ascent;
17690 it->max_descent = descent;
17691 it->max_phys_ascent = phys_ascent;
17692 it->max_phys_descent = phys_descent;
17693 }
17694
17695 break;
17696 }
17697 else if (new_x > it->first_visible_x)
17698 {
17699 /* Increment number of glyphs actually displayed. */
17700 ++it->hpos;
17701
17702 /* Record the maximum and minimum buffer positions
17703 seen so far in glyphs that will be displayed by
17704 this row. */
17705 if (it->bidi_p)
17706 RECORD_MAX_MIN_POS (it);
17707
17708 if (x < it->first_visible_x)
17709 /* Glyph is partially visible, i.e. row starts at
17710 negative X position. */
17711 row->x = x - it->first_visible_x;
17712 }
17713 else
17714 {
17715 /* Glyph is completely off the left margin of the
17716 window. This should not happen because of the
17717 move_it_in_display_line at the start of this
17718 function, unless the text display area of the
17719 window is empty. */
17720 xassert (it->first_visible_x <= it->last_visible_x);
17721 }
17722 }
17723
17724 row->ascent = max (row->ascent, it->max_ascent);
17725 row->height = max (row->height, it->max_ascent + it->max_descent);
17726 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17727 row->phys_height = max (row->phys_height,
17728 it->max_phys_ascent + it->max_phys_descent);
17729 row->extra_line_spacing = max (row->extra_line_spacing,
17730 it->max_extra_line_spacing);
17731
17732 /* End of this display line if row is continued. */
17733 if (row->continued_p || row->ends_at_zv_p)
17734 break;
17735 }
17736
17737 at_end_of_line:
17738 /* Is this a line end? If yes, we're also done, after making
17739 sure that a non-default face is extended up to the right
17740 margin of the window. */
17741 if (ITERATOR_AT_END_OF_LINE_P (it))
17742 {
17743 int used_before = row->used[TEXT_AREA];
17744
17745 row->ends_in_newline_from_string_p = STRINGP (it->object);
17746
17747 /* Add a space at the end of the line that is used to
17748 display the cursor there. */
17749 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17750 append_space_for_newline (it, 0);
17751
17752 /* Extend the face to the end of the line. */
17753 extend_face_to_end_of_line (it);
17754
17755 /* Make sure we have the position. */
17756 if (used_before == 0)
17757 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17758
17759 /* Record the position of the newline, for use in
17760 find_row_edges. */
17761 it->eol_pos = it->current.pos;
17762
17763 /* Consume the line end. This skips over invisible lines. */
17764 set_iterator_to_next (it, 1);
17765 it->continuation_lines_width = 0;
17766 break;
17767 }
17768
17769 /* Proceed with next display element. Note that this skips
17770 over lines invisible because of selective display. */
17771 set_iterator_to_next (it, 1);
17772
17773 /* If we truncate lines, we are done when the last displayed
17774 glyphs reach past the right margin of the window. */
17775 if (it->line_wrap == TRUNCATE
17776 && (FRAME_WINDOW_P (it->f)
17777 ? (it->current_x >= it->last_visible_x)
17778 : (it->current_x > it->last_visible_x)))
17779 {
17780 /* Maybe add truncation glyphs. */
17781 if (!FRAME_WINDOW_P (it->f))
17782 {
17783 int i, n;
17784
17785 if (!row->reversed_p)
17786 {
17787 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17788 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17789 break;
17790 }
17791 else
17792 {
17793 for (i = 0; i < row->used[TEXT_AREA]; i++)
17794 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17795 break;
17796 /* Remove any padding glyphs at the front of ROW, to
17797 make room for the truncation glyphs we will be
17798 adding below. The loop below always inserts at
17799 least one truncation glyph, so also remove the
17800 last glyph added to ROW. */
17801 unproduce_glyphs (it, i + 1);
17802 /* Adjust i for the loop below. */
17803 i = row->used[TEXT_AREA] - (i + 1);
17804 }
17805
17806 for (n = row->used[TEXT_AREA]; i < n; ++i)
17807 {
17808 row->used[TEXT_AREA] = i;
17809 produce_special_glyphs (it, IT_TRUNCATION);
17810 }
17811 }
17812 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17813 {
17814 /* Don't truncate if we can overflow newline into fringe. */
17815 if (!get_next_display_element (it))
17816 {
17817 it->continuation_lines_width = 0;
17818 row->ends_at_zv_p = 1;
17819 row->exact_window_width_line_p = 1;
17820 break;
17821 }
17822 if (ITERATOR_AT_END_OF_LINE_P (it))
17823 {
17824 row->exact_window_width_line_p = 1;
17825 goto at_end_of_line;
17826 }
17827 }
17828
17829 row->truncated_on_right_p = 1;
17830 it->continuation_lines_width = 0;
17831 reseat_at_next_visible_line_start (it, 0);
17832 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17833 it->hpos = hpos_before;
17834 it->current_x = x_before;
17835 break;
17836 }
17837 }
17838
17839 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17840 at the left window margin. */
17841 if (it->first_visible_x
17842 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17843 {
17844 if (!FRAME_WINDOW_P (it->f))
17845 insert_left_trunc_glyphs (it);
17846 row->truncated_on_left_p = 1;
17847 }
17848
17849 /* Remember the position at which this line ends.
17850
17851 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17852 cannot be before the call to find_row_edges below, since that is
17853 where these positions are determined. */
17854 row->end = it->current;
17855 if (!it->bidi_p)
17856 {
17857 row->minpos = row->start.pos;
17858 row->maxpos = row->end.pos;
17859 }
17860 else
17861 {
17862 /* ROW->minpos and ROW->maxpos must be the smallest and
17863 `1 + the largest' buffer positions in ROW. But if ROW was
17864 bidi-reordered, these two positions can be anywhere in the
17865 row, so we must determine them now. */
17866 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17867 }
17868
17869 /* If the start of this line is the overlay arrow-position, then
17870 mark this glyph row as the one containing the overlay arrow.
17871 This is clearly a mess with variable size fonts. It would be
17872 better to let it be displayed like cursors under X. */
17873 if ((row->displays_text_p || !overlay_arrow_seen)
17874 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17875 !NILP (overlay_arrow_string)))
17876 {
17877 /* Overlay arrow in window redisplay is a fringe bitmap. */
17878 if (STRINGP (overlay_arrow_string))
17879 {
17880 struct glyph_row *arrow_row
17881 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17882 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17883 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17884 struct glyph *p = row->glyphs[TEXT_AREA];
17885 struct glyph *p2, *end;
17886
17887 /* Copy the arrow glyphs. */
17888 while (glyph < arrow_end)
17889 *p++ = *glyph++;
17890
17891 /* Throw away padding glyphs. */
17892 p2 = p;
17893 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17894 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17895 ++p2;
17896 if (p2 > p)
17897 {
17898 while (p2 < end)
17899 *p++ = *p2++;
17900 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17901 }
17902 }
17903 else
17904 {
17905 xassert (INTEGERP (overlay_arrow_string));
17906 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17907 }
17908 overlay_arrow_seen = 1;
17909 }
17910
17911 /* Compute pixel dimensions of this line. */
17912 compute_line_metrics (it);
17913
17914 /* Record whether this row ends inside an ellipsis. */
17915 row->ends_in_ellipsis_p
17916 = (it->method == GET_FROM_DISPLAY_VECTOR
17917 && it->ellipsis_p);
17918
17919 /* Save fringe bitmaps in this row. */
17920 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17921 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17922 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17923 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17924
17925 it->left_user_fringe_bitmap = 0;
17926 it->left_user_fringe_face_id = 0;
17927 it->right_user_fringe_bitmap = 0;
17928 it->right_user_fringe_face_id = 0;
17929
17930 /* Maybe set the cursor. */
17931 cvpos = it->w->cursor.vpos;
17932 if ((cvpos < 0
17933 /* In bidi-reordered rows, keep checking for proper cursor
17934 position even if one has been found already, because buffer
17935 positions in such rows change non-linearly with ROW->VPOS,
17936 when a line is continued. One exception: when we are at ZV,
17937 display cursor on the first suitable glyph row, since all
17938 the empty rows after that also have their position set to ZV. */
17939 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17940 lines' rows is implemented for bidi-reordered rows. */
17941 || (it->bidi_p
17942 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17943 && PT >= MATRIX_ROW_START_CHARPOS (row)
17944 && PT <= MATRIX_ROW_END_CHARPOS (row)
17945 && cursor_row_p (row))
17946 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17947
17948 /* Highlight trailing whitespace. */
17949 if (!NILP (Vshow_trailing_whitespace))
17950 highlight_trailing_whitespace (it->f, it->glyph_row);
17951
17952 /* Prepare for the next line. This line starts horizontally at (X
17953 HPOS) = (0 0). Vertical positions are incremented. As a
17954 convenience for the caller, IT->glyph_row is set to the next
17955 row to be used. */
17956 it->current_x = it->hpos = 0;
17957 it->current_y += row->height;
17958 SET_TEXT_POS (it->eol_pos, 0, 0);
17959 ++it->vpos;
17960 ++it->glyph_row;
17961 /* The next row should by default use the same value of the
17962 reversed_p flag as this one. set_iterator_to_next decides when
17963 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
17964 the flag accordingly. */
17965 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
17966 it->glyph_row->reversed_p = row->reversed_p;
17967 it->start = row->end;
17968 return row->displays_text_p;
17969
17970 #undef RECORD_MAX_MIN_POS
17971 }
17972
17973 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
17974 Scurrent_bidi_paragraph_direction, 0, 1, 0,
17975 doc: /* Return paragraph direction at point in BUFFER.
17976 Value is either `left-to-right' or `right-to-left'.
17977 If BUFFER is omitted or nil, it defaults to the current buffer.
17978
17979 Paragraph direction determines how the text in the paragraph is displayed.
17980 In left-to-right paragraphs, text begins at the left margin of the window
17981 and the reading direction is generally left to right. In right-to-left
17982 paragraphs, text begins at the right margin and is read from right to left.
17983
17984 See also `bidi-paragraph-direction'. */)
17985 (Lisp_Object buffer)
17986 {
17987 struct buffer *buf = current_buffer;
17988 struct buffer *old = buf;
17989
17990 if (! NILP (buffer))
17991 {
17992 CHECK_BUFFER (buffer);
17993 buf = XBUFFER (buffer);
17994 }
17995
17996 if (NILP (BVAR (buf, bidi_display_reordering)))
17997 return Qleft_to_right;
17998 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
17999 return BVAR (buf, bidi_paragraph_direction);
18000 else
18001 {
18002 /* Determine the direction from buffer text. We could try to
18003 use current_matrix if it is up to date, but this seems fast
18004 enough as it is. */
18005 struct bidi_it itb;
18006 EMACS_INT pos = BUF_PT (buf);
18007 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18008 int c;
18009
18010 set_buffer_temp (buf);
18011 /* bidi_paragraph_init finds the base direction of the paragraph
18012 by searching forward from paragraph start. We need the base
18013 direction of the current or _previous_ paragraph, so we need
18014 to make sure we are within that paragraph. To that end, find
18015 the previous non-empty line. */
18016 if (pos >= ZV && pos > BEGV)
18017 {
18018 pos--;
18019 bytepos = CHAR_TO_BYTE (pos);
18020 }
18021 while ((c = FETCH_BYTE (bytepos)) == '\n'
18022 || c == ' ' || c == '\t' || c == '\f')
18023 {
18024 if (bytepos <= BEGV_BYTE)
18025 break;
18026 bytepos--;
18027 pos--;
18028 }
18029 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18030 bytepos--;
18031 itb.charpos = pos;
18032 itb.bytepos = bytepos;
18033 itb.first_elt = 1;
18034 itb.separator_limit = -1;
18035 itb.paragraph_dir = NEUTRAL_DIR;
18036
18037 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18038 set_buffer_temp (old);
18039 switch (itb.paragraph_dir)
18040 {
18041 case L2R:
18042 return Qleft_to_right;
18043 break;
18044 case R2L:
18045 return Qright_to_left;
18046 break;
18047 default:
18048 abort ();
18049 }
18050 }
18051 }
18052
18053
18054 \f
18055 /***********************************************************************
18056 Menu Bar
18057 ***********************************************************************/
18058
18059 /* Redisplay the menu bar in the frame for window W.
18060
18061 The menu bar of X frames that don't have X toolkit support is
18062 displayed in a special window W->frame->menu_bar_window.
18063
18064 The menu bar of terminal frames is treated specially as far as
18065 glyph matrices are concerned. Menu bar lines are not part of
18066 windows, so the update is done directly on the frame matrix rows
18067 for the menu bar. */
18068
18069 static void
18070 display_menu_bar (struct window *w)
18071 {
18072 struct frame *f = XFRAME (WINDOW_FRAME (w));
18073 struct it it;
18074 Lisp_Object items;
18075 int i;
18076
18077 /* Don't do all this for graphical frames. */
18078 #ifdef HAVE_NTGUI
18079 if (FRAME_W32_P (f))
18080 return;
18081 #endif
18082 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18083 if (FRAME_X_P (f))
18084 return;
18085 #endif
18086
18087 #ifdef HAVE_NS
18088 if (FRAME_NS_P (f))
18089 return;
18090 #endif /* HAVE_NS */
18091
18092 #ifdef USE_X_TOOLKIT
18093 xassert (!FRAME_WINDOW_P (f));
18094 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18095 it.first_visible_x = 0;
18096 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18097 #else /* not USE_X_TOOLKIT */
18098 if (FRAME_WINDOW_P (f))
18099 {
18100 /* Menu bar lines are displayed in the desired matrix of the
18101 dummy window menu_bar_window. */
18102 struct window *menu_w;
18103 xassert (WINDOWP (f->menu_bar_window));
18104 menu_w = XWINDOW (f->menu_bar_window);
18105 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18106 MENU_FACE_ID);
18107 it.first_visible_x = 0;
18108 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18109 }
18110 else
18111 {
18112 /* This is a TTY frame, i.e. character hpos/vpos are used as
18113 pixel x/y. */
18114 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18115 MENU_FACE_ID);
18116 it.first_visible_x = 0;
18117 it.last_visible_x = FRAME_COLS (f);
18118 }
18119 #endif /* not USE_X_TOOLKIT */
18120
18121 if (! mode_line_inverse_video)
18122 /* Force the menu-bar to be displayed in the default face. */
18123 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18124
18125 /* Clear all rows of the menu bar. */
18126 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18127 {
18128 struct glyph_row *row = it.glyph_row + i;
18129 clear_glyph_row (row);
18130 row->enabled_p = 1;
18131 row->full_width_p = 1;
18132 }
18133
18134 /* Display all items of the menu bar. */
18135 items = FRAME_MENU_BAR_ITEMS (it.f);
18136 for (i = 0; i < XVECTOR (items)->size; i += 4)
18137 {
18138 Lisp_Object string;
18139
18140 /* Stop at nil string. */
18141 string = AREF (items, i + 1);
18142 if (NILP (string))
18143 break;
18144
18145 /* Remember where item was displayed. */
18146 ASET (items, i + 3, make_number (it.hpos));
18147
18148 /* Display the item, pad with one space. */
18149 if (it.current_x < it.last_visible_x)
18150 display_string (NULL, string, Qnil, 0, 0, &it,
18151 SCHARS (string) + 1, 0, 0, -1);
18152 }
18153
18154 /* Fill out the line with spaces. */
18155 if (it.current_x < it.last_visible_x)
18156 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18157
18158 /* Compute the total height of the lines. */
18159 compute_line_metrics (&it);
18160 }
18161
18162
18163 \f
18164 /***********************************************************************
18165 Mode Line
18166 ***********************************************************************/
18167
18168 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18169 FORCE is non-zero, redisplay mode lines unconditionally.
18170 Otherwise, redisplay only mode lines that are garbaged. Value is
18171 the number of windows whose mode lines were redisplayed. */
18172
18173 static int
18174 redisplay_mode_lines (Lisp_Object window, int force)
18175 {
18176 int nwindows = 0;
18177
18178 while (!NILP (window))
18179 {
18180 struct window *w = XWINDOW (window);
18181
18182 if (WINDOWP (w->hchild))
18183 nwindows += redisplay_mode_lines (w->hchild, force);
18184 else if (WINDOWP (w->vchild))
18185 nwindows += redisplay_mode_lines (w->vchild, force);
18186 else if (force
18187 || FRAME_GARBAGED_P (XFRAME (w->frame))
18188 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18189 {
18190 struct text_pos lpoint;
18191 struct buffer *old = current_buffer;
18192
18193 /* Set the window's buffer for the mode line display. */
18194 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18195 set_buffer_internal_1 (XBUFFER (w->buffer));
18196
18197 /* Point refers normally to the selected window. For any
18198 other window, set up appropriate value. */
18199 if (!EQ (window, selected_window))
18200 {
18201 struct text_pos pt;
18202
18203 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18204 if (CHARPOS (pt) < BEGV)
18205 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18206 else if (CHARPOS (pt) > (ZV - 1))
18207 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18208 else
18209 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18210 }
18211
18212 /* Display mode lines. */
18213 clear_glyph_matrix (w->desired_matrix);
18214 if (display_mode_lines (w))
18215 {
18216 ++nwindows;
18217 w->must_be_updated_p = 1;
18218 }
18219
18220 /* Restore old settings. */
18221 set_buffer_internal_1 (old);
18222 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18223 }
18224
18225 window = w->next;
18226 }
18227
18228 return nwindows;
18229 }
18230
18231
18232 /* Display the mode and/or header line of window W. Value is the
18233 sum number of mode lines and header lines displayed. */
18234
18235 static int
18236 display_mode_lines (struct window *w)
18237 {
18238 Lisp_Object old_selected_window, old_selected_frame;
18239 int n = 0;
18240
18241 old_selected_frame = selected_frame;
18242 selected_frame = w->frame;
18243 old_selected_window = selected_window;
18244 XSETWINDOW (selected_window, w);
18245
18246 /* These will be set while the mode line specs are processed. */
18247 line_number_displayed = 0;
18248 w->column_number_displayed = Qnil;
18249
18250 if (WINDOW_WANTS_MODELINE_P (w))
18251 {
18252 struct window *sel_w = XWINDOW (old_selected_window);
18253
18254 /* Select mode line face based on the real selected window. */
18255 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18256 BVAR (current_buffer, mode_line_format));
18257 ++n;
18258 }
18259
18260 if (WINDOW_WANTS_HEADER_LINE_P (w))
18261 {
18262 display_mode_line (w, HEADER_LINE_FACE_ID,
18263 BVAR (current_buffer, header_line_format));
18264 ++n;
18265 }
18266
18267 selected_frame = old_selected_frame;
18268 selected_window = old_selected_window;
18269 return n;
18270 }
18271
18272
18273 /* Display mode or header line of window W. FACE_ID specifies which
18274 line to display; it is either MODE_LINE_FACE_ID or
18275 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18276 display. Value is the pixel height of the mode/header line
18277 displayed. */
18278
18279 static int
18280 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18281 {
18282 struct it it;
18283 struct face *face;
18284 int count = SPECPDL_INDEX ();
18285
18286 init_iterator (&it, w, -1, -1, NULL, face_id);
18287 /* Don't extend on a previously drawn mode-line.
18288 This may happen if called from pos_visible_p. */
18289 it.glyph_row->enabled_p = 0;
18290 prepare_desired_row (it.glyph_row);
18291
18292 it.glyph_row->mode_line_p = 1;
18293
18294 if (! mode_line_inverse_video)
18295 /* Force the mode-line to be displayed in the default face. */
18296 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18297
18298 record_unwind_protect (unwind_format_mode_line,
18299 format_mode_line_unwind_data (NULL, Qnil, 0));
18300
18301 mode_line_target = MODE_LINE_DISPLAY;
18302
18303 /* Temporarily make frame's keyboard the current kboard so that
18304 kboard-local variables in the mode_line_format will get the right
18305 values. */
18306 push_kboard (FRAME_KBOARD (it.f));
18307 record_unwind_save_match_data ();
18308 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18309 pop_kboard ();
18310
18311 unbind_to (count, Qnil);
18312
18313 /* Fill up with spaces. */
18314 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18315
18316 compute_line_metrics (&it);
18317 it.glyph_row->full_width_p = 1;
18318 it.glyph_row->continued_p = 0;
18319 it.glyph_row->truncated_on_left_p = 0;
18320 it.glyph_row->truncated_on_right_p = 0;
18321
18322 /* Make a 3D mode-line have a shadow at its right end. */
18323 face = FACE_FROM_ID (it.f, face_id);
18324 extend_face_to_end_of_line (&it);
18325 if (face->box != FACE_NO_BOX)
18326 {
18327 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18328 + it.glyph_row->used[TEXT_AREA] - 1);
18329 last->right_box_line_p = 1;
18330 }
18331
18332 return it.glyph_row->height;
18333 }
18334
18335 /* Move element ELT in LIST to the front of LIST.
18336 Return the updated list. */
18337
18338 static Lisp_Object
18339 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18340 {
18341 register Lisp_Object tail, prev;
18342 register Lisp_Object tem;
18343
18344 tail = list;
18345 prev = Qnil;
18346 while (CONSP (tail))
18347 {
18348 tem = XCAR (tail);
18349
18350 if (EQ (elt, tem))
18351 {
18352 /* Splice out the link TAIL. */
18353 if (NILP (prev))
18354 list = XCDR (tail);
18355 else
18356 Fsetcdr (prev, XCDR (tail));
18357
18358 /* Now make it the first. */
18359 Fsetcdr (tail, list);
18360 return tail;
18361 }
18362 else
18363 prev = tail;
18364 tail = XCDR (tail);
18365 QUIT;
18366 }
18367
18368 /* Not found--return unchanged LIST. */
18369 return list;
18370 }
18371
18372 /* Contribute ELT to the mode line for window IT->w. How it
18373 translates into text depends on its data type.
18374
18375 IT describes the display environment in which we display, as usual.
18376
18377 DEPTH is the depth in recursion. It is used to prevent
18378 infinite recursion here.
18379
18380 FIELD_WIDTH is the number of characters the display of ELT should
18381 occupy in the mode line, and PRECISION is the maximum number of
18382 characters to display from ELT's representation. See
18383 display_string for details.
18384
18385 Returns the hpos of the end of the text generated by ELT.
18386
18387 PROPS is a property list to add to any string we encounter.
18388
18389 If RISKY is nonzero, remove (disregard) any properties in any string
18390 we encounter, and ignore :eval and :propertize.
18391
18392 The global variable `mode_line_target' determines whether the
18393 output is passed to `store_mode_line_noprop',
18394 `store_mode_line_string', or `display_string'. */
18395
18396 static int
18397 display_mode_element (struct it *it, int depth, int field_width, int precision,
18398 Lisp_Object elt, Lisp_Object props, int risky)
18399 {
18400 int n = 0, field, prec;
18401 int literal = 0;
18402
18403 tail_recurse:
18404 if (depth > 100)
18405 elt = build_string ("*too-deep*");
18406
18407 depth++;
18408
18409 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18410 {
18411 case Lisp_String:
18412 {
18413 /* A string: output it and check for %-constructs within it. */
18414 unsigned char c;
18415 EMACS_INT offset = 0;
18416
18417 if (SCHARS (elt) > 0
18418 && (!NILP (props) || risky))
18419 {
18420 Lisp_Object oprops, aelt;
18421 oprops = Ftext_properties_at (make_number (0), elt);
18422
18423 /* If the starting string's properties are not what
18424 we want, translate the string. Also, if the string
18425 is risky, do that anyway. */
18426
18427 if (NILP (Fequal (props, oprops)) || risky)
18428 {
18429 /* If the starting string has properties,
18430 merge the specified ones onto the existing ones. */
18431 if (! NILP (oprops) && !risky)
18432 {
18433 Lisp_Object tem;
18434
18435 oprops = Fcopy_sequence (oprops);
18436 tem = props;
18437 while (CONSP (tem))
18438 {
18439 oprops = Fplist_put (oprops, XCAR (tem),
18440 XCAR (XCDR (tem)));
18441 tem = XCDR (XCDR (tem));
18442 }
18443 props = oprops;
18444 }
18445
18446 aelt = Fassoc (elt, mode_line_proptrans_alist);
18447 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18448 {
18449 /* AELT is what we want. Move it to the front
18450 without consing. */
18451 elt = XCAR (aelt);
18452 mode_line_proptrans_alist
18453 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18454 }
18455 else
18456 {
18457 Lisp_Object tem;
18458
18459 /* If AELT has the wrong props, it is useless.
18460 so get rid of it. */
18461 if (! NILP (aelt))
18462 mode_line_proptrans_alist
18463 = Fdelq (aelt, mode_line_proptrans_alist);
18464
18465 elt = Fcopy_sequence (elt);
18466 Fset_text_properties (make_number (0), Flength (elt),
18467 props, elt);
18468 /* Add this item to mode_line_proptrans_alist. */
18469 mode_line_proptrans_alist
18470 = Fcons (Fcons (elt, props),
18471 mode_line_proptrans_alist);
18472 /* Truncate mode_line_proptrans_alist
18473 to at most 50 elements. */
18474 tem = Fnthcdr (make_number (50),
18475 mode_line_proptrans_alist);
18476 if (! NILP (tem))
18477 XSETCDR (tem, Qnil);
18478 }
18479 }
18480 }
18481
18482 offset = 0;
18483
18484 if (literal)
18485 {
18486 prec = precision - n;
18487 switch (mode_line_target)
18488 {
18489 case MODE_LINE_NOPROP:
18490 case MODE_LINE_TITLE:
18491 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
18492 break;
18493 case MODE_LINE_STRING:
18494 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18495 break;
18496 case MODE_LINE_DISPLAY:
18497 n += display_string (NULL, elt, Qnil, 0, 0, it,
18498 0, prec, 0, STRING_MULTIBYTE (elt));
18499 break;
18500 }
18501
18502 break;
18503 }
18504
18505 /* Handle the non-literal case. */
18506
18507 while ((precision <= 0 || n < precision)
18508 && SREF (elt, offset) != 0
18509 && (mode_line_target != MODE_LINE_DISPLAY
18510 || it->current_x < it->last_visible_x))
18511 {
18512 EMACS_INT last_offset = offset;
18513
18514 /* Advance to end of string or next format specifier. */
18515 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18516 ;
18517
18518 if (offset - 1 != last_offset)
18519 {
18520 EMACS_INT nchars, nbytes;
18521
18522 /* Output to end of string or up to '%'. Field width
18523 is length of string. Don't output more than
18524 PRECISION allows us. */
18525 offset--;
18526
18527 prec = c_string_width (SDATA (elt) + last_offset,
18528 offset - last_offset, precision - n,
18529 &nchars, &nbytes);
18530
18531 switch (mode_line_target)
18532 {
18533 case MODE_LINE_NOPROP:
18534 case MODE_LINE_TITLE:
18535 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
18536 break;
18537 case MODE_LINE_STRING:
18538 {
18539 EMACS_INT bytepos = last_offset;
18540 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18541 EMACS_INT endpos = (precision <= 0
18542 ? string_byte_to_char (elt, offset)
18543 : charpos + nchars);
18544
18545 n += store_mode_line_string (NULL,
18546 Fsubstring (elt, make_number (charpos),
18547 make_number (endpos)),
18548 0, 0, 0, Qnil);
18549 }
18550 break;
18551 case MODE_LINE_DISPLAY:
18552 {
18553 EMACS_INT bytepos = last_offset;
18554 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18555
18556 if (precision <= 0)
18557 nchars = string_byte_to_char (elt, offset) - charpos;
18558 n += display_string (NULL, elt, Qnil, 0, charpos,
18559 it, 0, nchars, 0,
18560 STRING_MULTIBYTE (elt));
18561 }
18562 break;
18563 }
18564 }
18565 else /* c == '%' */
18566 {
18567 EMACS_INT percent_position = offset;
18568
18569 /* Get the specified minimum width. Zero means
18570 don't pad. */
18571 field = 0;
18572 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18573 field = field * 10 + c - '0';
18574
18575 /* Don't pad beyond the total padding allowed. */
18576 if (field_width - n > 0 && field > field_width - n)
18577 field = field_width - n;
18578
18579 /* Note that either PRECISION <= 0 or N < PRECISION. */
18580 prec = precision - n;
18581
18582 if (c == 'M')
18583 n += display_mode_element (it, depth, field, prec,
18584 Vglobal_mode_string, props,
18585 risky);
18586 else if (c != 0)
18587 {
18588 int multibyte;
18589 EMACS_INT bytepos, charpos;
18590 const char *spec;
18591 Lisp_Object string;
18592
18593 bytepos = percent_position;
18594 charpos = (STRING_MULTIBYTE (elt)
18595 ? string_byte_to_char (elt, bytepos)
18596 : bytepos);
18597 spec = decode_mode_spec (it->w, c, field, &string);
18598 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18599
18600 switch (mode_line_target)
18601 {
18602 case MODE_LINE_NOPROP:
18603 case MODE_LINE_TITLE:
18604 n += store_mode_line_noprop (spec, field, prec);
18605 break;
18606 case MODE_LINE_STRING:
18607 {
18608 int len = strlen (spec);
18609 Lisp_Object tem = make_string (spec, len);
18610 props = Ftext_properties_at (make_number (charpos), elt);
18611 /* Should only keep face property in props */
18612 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18613 }
18614 break;
18615 case MODE_LINE_DISPLAY:
18616 {
18617 int nglyphs_before, nwritten;
18618
18619 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18620 nwritten = display_string (spec, string, elt,
18621 charpos, 0, it,
18622 field, prec, 0,
18623 multibyte);
18624
18625 /* Assign to the glyphs written above the
18626 string where the `%x' came from, position
18627 of the `%'. */
18628 if (nwritten > 0)
18629 {
18630 struct glyph *glyph
18631 = (it->glyph_row->glyphs[TEXT_AREA]
18632 + nglyphs_before);
18633 int i;
18634
18635 for (i = 0; i < nwritten; ++i)
18636 {
18637 glyph[i].object = elt;
18638 glyph[i].charpos = charpos;
18639 }
18640
18641 n += nwritten;
18642 }
18643 }
18644 break;
18645 }
18646 }
18647 else /* c == 0 */
18648 break;
18649 }
18650 }
18651 }
18652 break;
18653
18654 case Lisp_Symbol:
18655 /* A symbol: process the value of the symbol recursively
18656 as if it appeared here directly. Avoid error if symbol void.
18657 Special case: if value of symbol is a string, output the string
18658 literally. */
18659 {
18660 register Lisp_Object tem;
18661
18662 /* If the variable is not marked as risky to set
18663 then its contents are risky to use. */
18664 if (NILP (Fget (elt, Qrisky_local_variable)))
18665 risky = 1;
18666
18667 tem = Fboundp (elt);
18668 if (!NILP (tem))
18669 {
18670 tem = Fsymbol_value (elt);
18671 /* If value is a string, output that string literally:
18672 don't check for % within it. */
18673 if (STRINGP (tem))
18674 literal = 1;
18675
18676 if (!EQ (tem, elt))
18677 {
18678 /* Give up right away for nil or t. */
18679 elt = tem;
18680 goto tail_recurse;
18681 }
18682 }
18683 }
18684 break;
18685
18686 case Lisp_Cons:
18687 {
18688 register Lisp_Object car, tem;
18689
18690 /* A cons cell: five distinct cases.
18691 If first element is :eval or :propertize, do something special.
18692 If first element is a string or a cons, process all the elements
18693 and effectively concatenate them.
18694 If first element is a negative number, truncate displaying cdr to
18695 at most that many characters. If positive, pad (with spaces)
18696 to at least that many characters.
18697 If first element is a symbol, process the cadr or caddr recursively
18698 according to whether the symbol's value is non-nil or nil. */
18699 car = XCAR (elt);
18700 if (EQ (car, QCeval))
18701 {
18702 /* An element of the form (:eval FORM) means evaluate FORM
18703 and use the result as mode line elements. */
18704
18705 if (risky)
18706 break;
18707
18708 if (CONSP (XCDR (elt)))
18709 {
18710 Lisp_Object spec;
18711 spec = safe_eval (XCAR (XCDR (elt)));
18712 n += display_mode_element (it, depth, field_width - n,
18713 precision - n, spec, props,
18714 risky);
18715 }
18716 }
18717 else if (EQ (car, QCpropertize))
18718 {
18719 /* An element of the form (:propertize ELT PROPS...)
18720 means display ELT but applying properties PROPS. */
18721
18722 if (risky)
18723 break;
18724
18725 if (CONSP (XCDR (elt)))
18726 n += display_mode_element (it, depth, field_width - n,
18727 precision - n, XCAR (XCDR (elt)),
18728 XCDR (XCDR (elt)), risky);
18729 }
18730 else if (SYMBOLP (car))
18731 {
18732 tem = Fboundp (car);
18733 elt = XCDR (elt);
18734 if (!CONSP (elt))
18735 goto invalid;
18736 /* elt is now the cdr, and we know it is a cons cell.
18737 Use its car if CAR has a non-nil value. */
18738 if (!NILP (tem))
18739 {
18740 tem = Fsymbol_value (car);
18741 if (!NILP (tem))
18742 {
18743 elt = XCAR (elt);
18744 goto tail_recurse;
18745 }
18746 }
18747 /* Symbol's value is nil (or symbol is unbound)
18748 Get the cddr of the original list
18749 and if possible find the caddr and use that. */
18750 elt = XCDR (elt);
18751 if (NILP (elt))
18752 break;
18753 else if (!CONSP (elt))
18754 goto invalid;
18755 elt = XCAR (elt);
18756 goto tail_recurse;
18757 }
18758 else if (INTEGERP (car))
18759 {
18760 register int lim = XINT (car);
18761 elt = XCDR (elt);
18762 if (lim < 0)
18763 {
18764 /* Negative int means reduce maximum width. */
18765 if (precision <= 0)
18766 precision = -lim;
18767 else
18768 precision = min (precision, -lim);
18769 }
18770 else if (lim > 0)
18771 {
18772 /* Padding specified. Don't let it be more than
18773 current maximum. */
18774 if (precision > 0)
18775 lim = min (precision, lim);
18776
18777 /* If that's more padding than already wanted, queue it.
18778 But don't reduce padding already specified even if
18779 that is beyond the current truncation point. */
18780 field_width = max (lim, field_width);
18781 }
18782 goto tail_recurse;
18783 }
18784 else if (STRINGP (car) || CONSP (car))
18785 {
18786 Lisp_Object halftail = elt;
18787 int len = 0;
18788
18789 while (CONSP (elt)
18790 && (precision <= 0 || n < precision))
18791 {
18792 n += display_mode_element (it, depth,
18793 /* Do padding only after the last
18794 element in the list. */
18795 (! CONSP (XCDR (elt))
18796 ? field_width - n
18797 : 0),
18798 precision - n, XCAR (elt),
18799 props, risky);
18800 elt = XCDR (elt);
18801 len++;
18802 if ((len & 1) == 0)
18803 halftail = XCDR (halftail);
18804 /* Check for cycle. */
18805 if (EQ (halftail, elt))
18806 break;
18807 }
18808 }
18809 }
18810 break;
18811
18812 default:
18813 invalid:
18814 elt = build_string ("*invalid*");
18815 goto tail_recurse;
18816 }
18817
18818 /* Pad to FIELD_WIDTH. */
18819 if (field_width > 0 && n < field_width)
18820 {
18821 switch (mode_line_target)
18822 {
18823 case MODE_LINE_NOPROP:
18824 case MODE_LINE_TITLE:
18825 n += store_mode_line_noprop ("", field_width - n, 0);
18826 break;
18827 case MODE_LINE_STRING:
18828 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18829 break;
18830 case MODE_LINE_DISPLAY:
18831 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18832 0, 0, 0);
18833 break;
18834 }
18835 }
18836
18837 return n;
18838 }
18839
18840 /* Store a mode-line string element in mode_line_string_list.
18841
18842 If STRING is non-null, display that C string. Otherwise, the Lisp
18843 string LISP_STRING is displayed.
18844
18845 FIELD_WIDTH is the minimum number of output glyphs to produce.
18846 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18847 with spaces. FIELD_WIDTH <= 0 means don't pad.
18848
18849 PRECISION is the maximum number of characters to output from
18850 STRING. PRECISION <= 0 means don't truncate the string.
18851
18852 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18853 properties to the string.
18854
18855 PROPS are the properties to add to the string.
18856 The mode_line_string_face face property is always added to the string.
18857 */
18858
18859 static int
18860 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18861 int field_width, int precision, Lisp_Object props)
18862 {
18863 EMACS_INT len;
18864 int n = 0;
18865
18866 if (string != NULL)
18867 {
18868 len = strlen (string);
18869 if (precision > 0 && len > precision)
18870 len = precision;
18871 lisp_string = make_string (string, len);
18872 if (NILP (props))
18873 props = mode_line_string_face_prop;
18874 else if (!NILP (mode_line_string_face))
18875 {
18876 Lisp_Object face = Fplist_get (props, Qface);
18877 props = Fcopy_sequence (props);
18878 if (NILP (face))
18879 face = mode_line_string_face;
18880 else
18881 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18882 props = Fplist_put (props, Qface, face);
18883 }
18884 Fadd_text_properties (make_number (0), make_number (len),
18885 props, lisp_string);
18886 }
18887 else
18888 {
18889 len = XFASTINT (Flength (lisp_string));
18890 if (precision > 0 && len > precision)
18891 {
18892 len = precision;
18893 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18894 precision = -1;
18895 }
18896 if (!NILP (mode_line_string_face))
18897 {
18898 Lisp_Object face;
18899 if (NILP (props))
18900 props = Ftext_properties_at (make_number (0), lisp_string);
18901 face = Fplist_get (props, Qface);
18902 if (NILP (face))
18903 face = mode_line_string_face;
18904 else
18905 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18906 props = Fcons (Qface, Fcons (face, Qnil));
18907 if (copy_string)
18908 lisp_string = Fcopy_sequence (lisp_string);
18909 }
18910 if (!NILP (props))
18911 Fadd_text_properties (make_number (0), make_number (len),
18912 props, lisp_string);
18913 }
18914
18915 if (len > 0)
18916 {
18917 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18918 n += len;
18919 }
18920
18921 if (field_width > len)
18922 {
18923 field_width -= len;
18924 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18925 if (!NILP (props))
18926 Fadd_text_properties (make_number (0), make_number (field_width),
18927 props, lisp_string);
18928 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18929 n += field_width;
18930 }
18931
18932 return n;
18933 }
18934
18935
18936 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18937 1, 4, 0,
18938 doc: /* Format a string out of a mode line format specification.
18939 First arg FORMAT specifies the mode line format (see `mode-line-format'
18940 for details) to use.
18941
18942 By default, the format is evaluated for the currently selected window.
18943
18944 Optional second arg FACE specifies the face property to put on all
18945 characters for which no face is specified. The value nil means the
18946 default face. The value t means whatever face the window's mode line
18947 currently uses (either `mode-line' or `mode-line-inactive',
18948 depending on whether the window is the selected window or not).
18949 An integer value means the value string has no text
18950 properties.
18951
18952 Optional third and fourth args WINDOW and BUFFER specify the window
18953 and buffer to use as the context for the formatting (defaults
18954 are the selected window and the WINDOW's buffer). */)
18955 (Lisp_Object format, Lisp_Object face,
18956 Lisp_Object window, Lisp_Object buffer)
18957 {
18958 struct it it;
18959 int len;
18960 struct window *w;
18961 struct buffer *old_buffer = NULL;
18962 int face_id;
18963 int no_props = INTEGERP (face);
18964 int count = SPECPDL_INDEX ();
18965 Lisp_Object str;
18966 int string_start = 0;
18967
18968 if (NILP (window))
18969 window = selected_window;
18970 CHECK_WINDOW (window);
18971 w = XWINDOW (window);
18972
18973 if (NILP (buffer))
18974 buffer = w->buffer;
18975 CHECK_BUFFER (buffer);
18976
18977 /* Make formatting the modeline a non-op when noninteractive, otherwise
18978 there will be problems later caused by a partially initialized frame. */
18979 if (NILP (format) || noninteractive)
18980 return empty_unibyte_string;
18981
18982 if (no_props)
18983 face = Qnil;
18984
18985 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
18986 : EQ (face, Qt) ? (EQ (window, selected_window)
18987 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
18988 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
18989 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
18990 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
18991 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
18992 : DEFAULT_FACE_ID;
18993
18994 if (XBUFFER (buffer) != current_buffer)
18995 old_buffer = current_buffer;
18996
18997 /* Save things including mode_line_proptrans_alist,
18998 and set that to nil so that we don't alter the outer value. */
18999 record_unwind_protect (unwind_format_mode_line,
19000 format_mode_line_unwind_data
19001 (old_buffer, selected_window, 1));
19002 mode_line_proptrans_alist = Qnil;
19003
19004 Fselect_window (window, Qt);
19005 if (old_buffer)
19006 set_buffer_internal_1 (XBUFFER (buffer));
19007
19008 init_iterator (&it, w, -1, -1, NULL, face_id);
19009
19010 if (no_props)
19011 {
19012 mode_line_target = MODE_LINE_NOPROP;
19013 mode_line_string_face_prop = Qnil;
19014 mode_line_string_list = Qnil;
19015 string_start = MODE_LINE_NOPROP_LEN (0);
19016 }
19017 else
19018 {
19019 mode_line_target = MODE_LINE_STRING;
19020 mode_line_string_list = Qnil;
19021 mode_line_string_face = face;
19022 mode_line_string_face_prop
19023 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19024 }
19025
19026 push_kboard (FRAME_KBOARD (it.f));
19027 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19028 pop_kboard ();
19029
19030 if (no_props)
19031 {
19032 len = MODE_LINE_NOPROP_LEN (string_start);
19033 str = make_string (mode_line_noprop_buf + string_start, len);
19034 }
19035 else
19036 {
19037 mode_line_string_list = Fnreverse (mode_line_string_list);
19038 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19039 empty_unibyte_string);
19040 }
19041
19042 unbind_to (count, Qnil);
19043 return str;
19044 }
19045
19046 /* Write a null-terminated, right justified decimal representation of
19047 the positive integer D to BUF using a minimal field width WIDTH. */
19048
19049 static void
19050 pint2str (register char *buf, register int width, register EMACS_INT d)
19051 {
19052 register char *p = buf;
19053
19054 if (d <= 0)
19055 *p++ = '0';
19056 else
19057 {
19058 while (d > 0)
19059 {
19060 *p++ = d % 10 + '0';
19061 d /= 10;
19062 }
19063 }
19064
19065 for (width -= (int) (p - buf); width > 0; --width)
19066 *p++ = ' ';
19067 *p-- = '\0';
19068 while (p > buf)
19069 {
19070 d = *buf;
19071 *buf++ = *p;
19072 *p-- = d;
19073 }
19074 }
19075
19076 /* Write a null-terminated, right justified decimal and "human
19077 readable" representation of the nonnegative integer D to BUF using
19078 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19079
19080 static const char power_letter[] =
19081 {
19082 0, /* no letter */
19083 'k', /* kilo */
19084 'M', /* mega */
19085 'G', /* giga */
19086 'T', /* tera */
19087 'P', /* peta */
19088 'E', /* exa */
19089 'Z', /* zetta */
19090 'Y' /* yotta */
19091 };
19092
19093 static void
19094 pint2hrstr (char *buf, int width, int d)
19095 {
19096 /* We aim to represent the nonnegative integer D as
19097 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19098 int quotient = d;
19099 int remainder = 0;
19100 /* -1 means: do not use TENTHS. */
19101 int tenths = -1;
19102 int exponent = 0;
19103
19104 /* Length of QUOTIENT.TENTHS as a string. */
19105 int length;
19106
19107 char * psuffix;
19108 char * p;
19109
19110 if (1000 <= quotient)
19111 {
19112 /* Scale to the appropriate EXPONENT. */
19113 do
19114 {
19115 remainder = quotient % 1000;
19116 quotient /= 1000;
19117 exponent++;
19118 }
19119 while (1000 <= quotient);
19120
19121 /* Round to nearest and decide whether to use TENTHS or not. */
19122 if (quotient <= 9)
19123 {
19124 tenths = remainder / 100;
19125 if (50 <= remainder % 100)
19126 {
19127 if (tenths < 9)
19128 tenths++;
19129 else
19130 {
19131 quotient++;
19132 if (quotient == 10)
19133 tenths = -1;
19134 else
19135 tenths = 0;
19136 }
19137 }
19138 }
19139 else
19140 if (500 <= remainder)
19141 {
19142 if (quotient < 999)
19143 quotient++;
19144 else
19145 {
19146 quotient = 1;
19147 exponent++;
19148 tenths = 0;
19149 }
19150 }
19151 }
19152
19153 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19154 if (tenths == -1 && quotient <= 99)
19155 if (quotient <= 9)
19156 length = 1;
19157 else
19158 length = 2;
19159 else
19160 length = 3;
19161 p = psuffix = buf + max (width, length);
19162
19163 /* Print EXPONENT. */
19164 *psuffix++ = power_letter[exponent];
19165 *psuffix = '\0';
19166
19167 /* Print TENTHS. */
19168 if (tenths >= 0)
19169 {
19170 *--p = '0' + tenths;
19171 *--p = '.';
19172 }
19173
19174 /* Print QUOTIENT. */
19175 do
19176 {
19177 int digit = quotient % 10;
19178 *--p = '0' + digit;
19179 }
19180 while ((quotient /= 10) != 0);
19181
19182 /* Print leading spaces. */
19183 while (buf < p)
19184 *--p = ' ';
19185 }
19186
19187 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19188 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19189 type of CODING_SYSTEM. Return updated pointer into BUF. */
19190
19191 static unsigned char invalid_eol_type[] = "(*invalid*)";
19192
19193 static char *
19194 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19195 {
19196 Lisp_Object val;
19197 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19198 const unsigned char *eol_str;
19199 int eol_str_len;
19200 /* The EOL conversion we are using. */
19201 Lisp_Object eoltype;
19202
19203 val = CODING_SYSTEM_SPEC (coding_system);
19204 eoltype = Qnil;
19205
19206 if (!VECTORP (val)) /* Not yet decided. */
19207 {
19208 if (multibyte)
19209 *buf++ = '-';
19210 if (eol_flag)
19211 eoltype = eol_mnemonic_undecided;
19212 /* Don't mention EOL conversion if it isn't decided. */
19213 }
19214 else
19215 {
19216 Lisp_Object attrs;
19217 Lisp_Object eolvalue;
19218
19219 attrs = AREF (val, 0);
19220 eolvalue = AREF (val, 2);
19221
19222 if (multibyte)
19223 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19224
19225 if (eol_flag)
19226 {
19227 /* The EOL conversion that is normal on this system. */
19228
19229 if (NILP (eolvalue)) /* Not yet decided. */
19230 eoltype = eol_mnemonic_undecided;
19231 else if (VECTORP (eolvalue)) /* Not yet decided. */
19232 eoltype = eol_mnemonic_undecided;
19233 else /* eolvalue is Qunix, Qdos, or Qmac. */
19234 eoltype = (EQ (eolvalue, Qunix)
19235 ? eol_mnemonic_unix
19236 : (EQ (eolvalue, Qdos) == 1
19237 ? eol_mnemonic_dos : eol_mnemonic_mac));
19238 }
19239 }
19240
19241 if (eol_flag)
19242 {
19243 /* Mention the EOL conversion if it is not the usual one. */
19244 if (STRINGP (eoltype))
19245 {
19246 eol_str = SDATA (eoltype);
19247 eol_str_len = SBYTES (eoltype);
19248 }
19249 else if (CHARACTERP (eoltype))
19250 {
19251 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19252 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19253 eol_str = tmp;
19254 }
19255 else
19256 {
19257 eol_str = invalid_eol_type;
19258 eol_str_len = sizeof (invalid_eol_type) - 1;
19259 }
19260 memcpy (buf, eol_str, eol_str_len);
19261 buf += eol_str_len;
19262 }
19263
19264 return buf;
19265 }
19266
19267 /* Return a string for the output of a mode line %-spec for window W,
19268 generated by character C. FIELD_WIDTH > 0 means pad the string
19269 returned with spaces to that value. Return a Lisp string in
19270 *STRING if the resulting string is taken from that Lisp string.
19271
19272 Note we operate on the current buffer for most purposes,
19273 the exception being w->base_line_pos. */
19274
19275 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19276
19277 static const char *
19278 decode_mode_spec (struct window *w, register int c, int field_width,
19279 Lisp_Object *string)
19280 {
19281 Lisp_Object obj;
19282 struct frame *f = XFRAME (WINDOW_FRAME (w));
19283 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19284 struct buffer *b = current_buffer;
19285
19286 obj = Qnil;
19287 *string = Qnil;
19288
19289 switch (c)
19290 {
19291 case '*':
19292 if (!NILP (BVAR (b, read_only)))
19293 return "%";
19294 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19295 return "*";
19296 return "-";
19297
19298 case '+':
19299 /* This differs from %* only for a modified read-only buffer. */
19300 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19301 return "*";
19302 if (!NILP (BVAR (b, read_only)))
19303 return "%";
19304 return "-";
19305
19306 case '&':
19307 /* This differs from %* in ignoring read-only-ness. */
19308 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19309 return "*";
19310 return "-";
19311
19312 case '%':
19313 return "%";
19314
19315 case '[':
19316 {
19317 int i;
19318 char *p;
19319
19320 if (command_loop_level > 5)
19321 return "[[[... ";
19322 p = decode_mode_spec_buf;
19323 for (i = 0; i < command_loop_level; i++)
19324 *p++ = '[';
19325 *p = 0;
19326 return decode_mode_spec_buf;
19327 }
19328
19329 case ']':
19330 {
19331 int i;
19332 char *p;
19333
19334 if (command_loop_level > 5)
19335 return " ...]]]";
19336 p = decode_mode_spec_buf;
19337 for (i = 0; i < command_loop_level; i++)
19338 *p++ = ']';
19339 *p = 0;
19340 return decode_mode_spec_buf;
19341 }
19342
19343 case '-':
19344 {
19345 register int i;
19346
19347 /* Let lots_of_dashes be a string of infinite length. */
19348 if (mode_line_target == MODE_LINE_NOPROP ||
19349 mode_line_target == MODE_LINE_STRING)
19350 return "--";
19351 if (field_width <= 0
19352 || field_width > sizeof (lots_of_dashes))
19353 {
19354 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19355 decode_mode_spec_buf[i] = '-';
19356 decode_mode_spec_buf[i] = '\0';
19357 return decode_mode_spec_buf;
19358 }
19359 else
19360 return lots_of_dashes;
19361 }
19362
19363 case 'b':
19364 obj = BVAR (b, name);
19365 break;
19366
19367 case 'c':
19368 /* %c and %l are ignored in `frame-title-format'.
19369 (In redisplay_internal, the frame title is drawn _before_ the
19370 windows are updated, so the stuff which depends on actual
19371 window contents (such as %l) may fail to render properly, or
19372 even crash emacs.) */
19373 if (mode_line_target == MODE_LINE_TITLE)
19374 return "";
19375 else
19376 {
19377 EMACS_INT col = current_column ();
19378 w->column_number_displayed = make_number (col);
19379 pint2str (decode_mode_spec_buf, field_width, col);
19380 return decode_mode_spec_buf;
19381 }
19382
19383 case 'e':
19384 #ifndef SYSTEM_MALLOC
19385 {
19386 if (NILP (Vmemory_full))
19387 return "";
19388 else
19389 return "!MEM FULL! ";
19390 }
19391 #else
19392 return "";
19393 #endif
19394
19395 case 'F':
19396 /* %F displays the frame name. */
19397 if (!NILP (f->title))
19398 return SSDATA (f->title);
19399 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19400 return SSDATA (f->name);
19401 return "Emacs";
19402
19403 case 'f':
19404 obj = BVAR (b, filename);
19405 break;
19406
19407 case 'i':
19408 {
19409 EMACS_INT size = ZV - BEGV;
19410 pint2str (decode_mode_spec_buf, field_width, size);
19411 return decode_mode_spec_buf;
19412 }
19413
19414 case 'I':
19415 {
19416 EMACS_INT size = ZV - BEGV;
19417 pint2hrstr (decode_mode_spec_buf, field_width, size);
19418 return decode_mode_spec_buf;
19419 }
19420
19421 case 'l':
19422 {
19423 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19424 int topline, nlines, height;
19425 EMACS_INT junk;
19426
19427 /* %c and %l are ignored in `frame-title-format'. */
19428 if (mode_line_target == MODE_LINE_TITLE)
19429 return "";
19430
19431 startpos = XMARKER (w->start)->charpos;
19432 startpos_byte = marker_byte_position (w->start);
19433 height = WINDOW_TOTAL_LINES (w);
19434
19435 /* If we decided that this buffer isn't suitable for line numbers,
19436 don't forget that too fast. */
19437 if (EQ (w->base_line_pos, w->buffer))
19438 goto no_value;
19439 /* But do forget it, if the window shows a different buffer now. */
19440 else if (BUFFERP (w->base_line_pos))
19441 w->base_line_pos = Qnil;
19442
19443 /* If the buffer is very big, don't waste time. */
19444 if (INTEGERP (Vline_number_display_limit)
19445 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19446 {
19447 w->base_line_pos = Qnil;
19448 w->base_line_number = Qnil;
19449 goto no_value;
19450 }
19451
19452 if (INTEGERP (w->base_line_number)
19453 && INTEGERP (w->base_line_pos)
19454 && XFASTINT (w->base_line_pos) <= startpos)
19455 {
19456 line = XFASTINT (w->base_line_number);
19457 linepos = XFASTINT (w->base_line_pos);
19458 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19459 }
19460 else
19461 {
19462 line = 1;
19463 linepos = BUF_BEGV (b);
19464 linepos_byte = BUF_BEGV_BYTE (b);
19465 }
19466
19467 /* Count lines from base line to window start position. */
19468 nlines = display_count_lines (linepos_byte,
19469 startpos_byte,
19470 startpos, &junk);
19471
19472 topline = nlines + line;
19473
19474 /* Determine a new base line, if the old one is too close
19475 or too far away, or if we did not have one.
19476 "Too close" means it's plausible a scroll-down would
19477 go back past it. */
19478 if (startpos == BUF_BEGV (b))
19479 {
19480 w->base_line_number = make_number (topline);
19481 w->base_line_pos = make_number (BUF_BEGV (b));
19482 }
19483 else if (nlines < height + 25 || nlines > height * 3 + 50
19484 || linepos == BUF_BEGV (b))
19485 {
19486 EMACS_INT limit = BUF_BEGV (b);
19487 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19488 EMACS_INT position;
19489 int distance = (height * 2 + 30) * line_number_display_limit_width;
19490
19491 if (startpos - distance > limit)
19492 {
19493 limit = startpos - distance;
19494 limit_byte = CHAR_TO_BYTE (limit);
19495 }
19496
19497 nlines = display_count_lines (startpos_byte,
19498 limit_byte,
19499 - (height * 2 + 30),
19500 &position);
19501 /* If we couldn't find the lines we wanted within
19502 line_number_display_limit_width chars per line,
19503 give up on line numbers for this window. */
19504 if (position == limit_byte && limit == startpos - distance)
19505 {
19506 w->base_line_pos = w->buffer;
19507 w->base_line_number = Qnil;
19508 goto no_value;
19509 }
19510
19511 w->base_line_number = make_number (topline - nlines);
19512 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19513 }
19514
19515 /* Now count lines from the start pos to point. */
19516 nlines = display_count_lines (startpos_byte,
19517 PT_BYTE, PT, &junk);
19518
19519 /* Record that we did display the line number. */
19520 line_number_displayed = 1;
19521
19522 /* Make the string to show. */
19523 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19524 return decode_mode_spec_buf;
19525 no_value:
19526 {
19527 char* p = decode_mode_spec_buf;
19528 int pad = field_width - 2;
19529 while (pad-- > 0)
19530 *p++ = ' ';
19531 *p++ = '?';
19532 *p++ = '?';
19533 *p = '\0';
19534 return decode_mode_spec_buf;
19535 }
19536 }
19537 break;
19538
19539 case 'm':
19540 obj = BVAR (b, mode_name);
19541 break;
19542
19543 case 'n':
19544 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19545 return " Narrow";
19546 break;
19547
19548 case 'p':
19549 {
19550 EMACS_INT pos = marker_position (w->start);
19551 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19552
19553 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19554 {
19555 if (pos <= BUF_BEGV (b))
19556 return "All";
19557 else
19558 return "Bottom";
19559 }
19560 else if (pos <= BUF_BEGV (b))
19561 return "Top";
19562 else
19563 {
19564 if (total > 1000000)
19565 /* Do it differently for a large value, to avoid overflow. */
19566 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19567 else
19568 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19569 /* We can't normally display a 3-digit number,
19570 so get us a 2-digit number that is close. */
19571 if (total == 100)
19572 total = 99;
19573 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19574 return decode_mode_spec_buf;
19575 }
19576 }
19577
19578 /* Display percentage of size above the bottom of the screen. */
19579 case 'P':
19580 {
19581 EMACS_INT toppos = marker_position (w->start);
19582 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19583 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19584
19585 if (botpos >= BUF_ZV (b))
19586 {
19587 if (toppos <= BUF_BEGV (b))
19588 return "All";
19589 else
19590 return "Bottom";
19591 }
19592 else
19593 {
19594 if (total > 1000000)
19595 /* Do it differently for a large value, to avoid overflow. */
19596 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19597 else
19598 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19599 /* We can't normally display a 3-digit number,
19600 so get us a 2-digit number that is close. */
19601 if (total == 100)
19602 total = 99;
19603 if (toppos <= BUF_BEGV (b))
19604 sprintf (decode_mode_spec_buf, "Top%2ld%%", (long)total);
19605 else
19606 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19607 return decode_mode_spec_buf;
19608 }
19609 }
19610
19611 case 's':
19612 /* status of process */
19613 obj = Fget_buffer_process (Fcurrent_buffer ());
19614 if (NILP (obj))
19615 return "no process";
19616 #ifndef MSDOS
19617 obj = Fsymbol_name (Fprocess_status (obj));
19618 #endif
19619 break;
19620
19621 case '@':
19622 {
19623 int count = inhibit_garbage_collection ();
19624 Lisp_Object val = call1 (intern ("file-remote-p"),
19625 BVAR (current_buffer, directory));
19626 unbind_to (count, Qnil);
19627
19628 if (NILP (val))
19629 return "-";
19630 else
19631 return "@";
19632 }
19633
19634 case 't': /* indicate TEXT or BINARY */
19635 return "T";
19636
19637 case 'z':
19638 /* coding-system (not including end-of-line format) */
19639 case 'Z':
19640 /* coding-system (including end-of-line type) */
19641 {
19642 int eol_flag = (c == 'Z');
19643 char *p = decode_mode_spec_buf;
19644
19645 if (! FRAME_WINDOW_P (f))
19646 {
19647 /* No need to mention EOL here--the terminal never needs
19648 to do EOL conversion. */
19649 p = decode_mode_spec_coding (CODING_ID_NAME
19650 (FRAME_KEYBOARD_CODING (f)->id),
19651 p, 0);
19652 p = decode_mode_spec_coding (CODING_ID_NAME
19653 (FRAME_TERMINAL_CODING (f)->id),
19654 p, 0);
19655 }
19656 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
19657 p, eol_flag);
19658
19659 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19660 #ifdef subprocesses
19661 obj = Fget_buffer_process (Fcurrent_buffer ());
19662 if (PROCESSP (obj))
19663 {
19664 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19665 p, eol_flag);
19666 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19667 p, eol_flag);
19668 }
19669 #endif /* subprocesses */
19670 #endif /* 0 */
19671 *p = 0;
19672 return decode_mode_spec_buf;
19673 }
19674 }
19675
19676 if (STRINGP (obj))
19677 {
19678 *string = obj;
19679 return SSDATA (obj);
19680 }
19681 else
19682 return "";
19683 }
19684
19685
19686 /* Count up to COUNT lines starting from START_BYTE.
19687 But don't go beyond LIMIT_BYTE.
19688 Return the number of lines thus found (always nonnegative).
19689
19690 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19691
19692 static int
19693 display_count_lines (EMACS_INT start_byte,
19694 EMACS_INT limit_byte, int count,
19695 EMACS_INT *byte_pos_ptr)
19696 {
19697 register unsigned char *cursor;
19698 unsigned char *base;
19699
19700 register int ceiling;
19701 register unsigned char *ceiling_addr;
19702 int orig_count = count;
19703
19704 /* If we are not in selective display mode,
19705 check only for newlines. */
19706 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
19707 && !INTEGERP (BVAR (current_buffer, selective_display)));
19708
19709 if (count > 0)
19710 {
19711 while (start_byte < limit_byte)
19712 {
19713 ceiling = BUFFER_CEILING_OF (start_byte);
19714 ceiling = min (limit_byte - 1, ceiling);
19715 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19716 base = (cursor = BYTE_POS_ADDR (start_byte));
19717 while (1)
19718 {
19719 if (selective_display)
19720 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19721 ;
19722 else
19723 while (*cursor != '\n' && ++cursor != ceiling_addr)
19724 ;
19725
19726 if (cursor != ceiling_addr)
19727 {
19728 if (--count == 0)
19729 {
19730 start_byte += cursor - base + 1;
19731 *byte_pos_ptr = start_byte;
19732 return orig_count;
19733 }
19734 else
19735 if (++cursor == ceiling_addr)
19736 break;
19737 }
19738 else
19739 break;
19740 }
19741 start_byte += cursor - base;
19742 }
19743 }
19744 else
19745 {
19746 while (start_byte > limit_byte)
19747 {
19748 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19749 ceiling = max (limit_byte, ceiling);
19750 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19751 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19752 while (1)
19753 {
19754 if (selective_display)
19755 while (--cursor != ceiling_addr
19756 && *cursor != '\n' && *cursor != 015)
19757 ;
19758 else
19759 while (--cursor != ceiling_addr && *cursor != '\n')
19760 ;
19761
19762 if (cursor != ceiling_addr)
19763 {
19764 if (++count == 0)
19765 {
19766 start_byte += cursor - base + 1;
19767 *byte_pos_ptr = start_byte;
19768 /* When scanning backwards, we should
19769 not count the newline posterior to which we stop. */
19770 return - orig_count - 1;
19771 }
19772 }
19773 else
19774 break;
19775 }
19776 /* Here we add 1 to compensate for the last decrement
19777 of CURSOR, which took it past the valid range. */
19778 start_byte += cursor - base + 1;
19779 }
19780 }
19781
19782 *byte_pos_ptr = limit_byte;
19783
19784 if (count < 0)
19785 return - orig_count + count;
19786 return orig_count - count;
19787
19788 }
19789
19790
19791 \f
19792 /***********************************************************************
19793 Displaying strings
19794 ***********************************************************************/
19795
19796 /* Display a NUL-terminated string, starting with index START.
19797
19798 If STRING is non-null, display that C string. Otherwise, the Lisp
19799 string LISP_STRING is displayed. There's a case that STRING is
19800 non-null and LISP_STRING is not nil. It means STRING is a string
19801 data of LISP_STRING. In that case, we display LISP_STRING while
19802 ignoring its text properties.
19803
19804 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19805 FACE_STRING. Display STRING or LISP_STRING with the face at
19806 FACE_STRING_POS in FACE_STRING:
19807
19808 Display the string in the environment given by IT, but use the
19809 standard display table, temporarily.
19810
19811 FIELD_WIDTH is the minimum number of output glyphs to produce.
19812 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19813 with spaces. If STRING has more characters, more than FIELD_WIDTH
19814 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19815
19816 PRECISION is the maximum number of characters to output from
19817 STRING. PRECISION < 0 means don't truncate the string.
19818
19819 This is roughly equivalent to printf format specifiers:
19820
19821 FIELD_WIDTH PRECISION PRINTF
19822 ----------------------------------------
19823 -1 -1 %s
19824 -1 10 %.10s
19825 10 -1 %10s
19826 20 10 %20.10s
19827
19828 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19829 display them, and < 0 means obey the current buffer's value of
19830 enable_multibyte_characters.
19831
19832 Value is the number of columns displayed. */
19833
19834 static int
19835 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19836 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19837 int field_width, int precision, int max_x, int multibyte)
19838 {
19839 int hpos_at_start = it->hpos;
19840 int saved_face_id = it->face_id;
19841 struct glyph_row *row = it->glyph_row;
19842
19843 /* Initialize the iterator IT for iteration over STRING beginning
19844 with index START. */
19845 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19846 precision, field_width, multibyte);
19847 if (string && STRINGP (lisp_string))
19848 /* LISP_STRING is the one returned by decode_mode_spec. We should
19849 ignore its text properties. */
19850 it->stop_charpos = -1;
19851
19852 /* If displaying STRING, set up the face of the iterator
19853 from LISP_STRING, if that's given. */
19854 if (STRINGP (face_string))
19855 {
19856 EMACS_INT endptr;
19857 struct face *face;
19858
19859 it->face_id
19860 = face_at_string_position (it->w, face_string, face_string_pos,
19861 0, it->region_beg_charpos,
19862 it->region_end_charpos,
19863 &endptr, it->base_face_id, 0);
19864 face = FACE_FROM_ID (it->f, it->face_id);
19865 it->face_box_p = face->box != FACE_NO_BOX;
19866 }
19867
19868 /* Set max_x to the maximum allowed X position. Don't let it go
19869 beyond the right edge of the window. */
19870 if (max_x <= 0)
19871 max_x = it->last_visible_x;
19872 else
19873 max_x = min (max_x, it->last_visible_x);
19874
19875 /* Skip over display elements that are not visible. because IT->w is
19876 hscrolled. */
19877 if (it->current_x < it->first_visible_x)
19878 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19879 MOVE_TO_POS | MOVE_TO_X);
19880
19881 row->ascent = it->max_ascent;
19882 row->height = it->max_ascent + it->max_descent;
19883 row->phys_ascent = it->max_phys_ascent;
19884 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19885 row->extra_line_spacing = it->max_extra_line_spacing;
19886
19887 /* This condition is for the case that we are called with current_x
19888 past last_visible_x. */
19889 while (it->current_x < max_x)
19890 {
19891 int x_before, x, n_glyphs_before, i, nglyphs;
19892
19893 /* Get the next display element. */
19894 if (!get_next_display_element (it))
19895 break;
19896
19897 /* Produce glyphs. */
19898 x_before = it->current_x;
19899 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19900 PRODUCE_GLYPHS (it);
19901
19902 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19903 i = 0;
19904 x = x_before;
19905 while (i < nglyphs)
19906 {
19907 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19908
19909 if (it->line_wrap != TRUNCATE
19910 && x + glyph->pixel_width > max_x)
19911 {
19912 /* End of continued line or max_x reached. */
19913 if (CHAR_GLYPH_PADDING_P (*glyph))
19914 {
19915 /* A wide character is unbreakable. */
19916 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19917 it->current_x = x_before;
19918 }
19919 else
19920 {
19921 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19922 it->current_x = x;
19923 }
19924 break;
19925 }
19926 else if (x + glyph->pixel_width >= it->first_visible_x)
19927 {
19928 /* Glyph is at least partially visible. */
19929 ++it->hpos;
19930 if (x < it->first_visible_x)
19931 it->glyph_row->x = x - it->first_visible_x;
19932 }
19933 else
19934 {
19935 /* Glyph is off the left margin of the display area.
19936 Should not happen. */
19937 abort ();
19938 }
19939
19940 row->ascent = max (row->ascent, it->max_ascent);
19941 row->height = max (row->height, it->max_ascent + it->max_descent);
19942 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19943 row->phys_height = max (row->phys_height,
19944 it->max_phys_ascent + it->max_phys_descent);
19945 row->extra_line_spacing = max (row->extra_line_spacing,
19946 it->max_extra_line_spacing);
19947 x += glyph->pixel_width;
19948 ++i;
19949 }
19950
19951 /* Stop if max_x reached. */
19952 if (i < nglyphs)
19953 break;
19954
19955 /* Stop at line ends. */
19956 if (ITERATOR_AT_END_OF_LINE_P (it))
19957 {
19958 it->continuation_lines_width = 0;
19959 break;
19960 }
19961
19962 set_iterator_to_next (it, 1);
19963
19964 /* Stop if truncating at the right edge. */
19965 if (it->line_wrap == TRUNCATE
19966 && it->current_x >= it->last_visible_x)
19967 {
19968 /* Add truncation mark, but don't do it if the line is
19969 truncated at a padding space. */
19970 if (IT_CHARPOS (*it) < it->string_nchars)
19971 {
19972 if (!FRAME_WINDOW_P (it->f))
19973 {
19974 int ii, n;
19975
19976 if (it->current_x > it->last_visible_x)
19977 {
19978 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
19979 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
19980 break;
19981 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
19982 {
19983 row->used[TEXT_AREA] = ii;
19984 produce_special_glyphs (it, IT_TRUNCATION);
19985 }
19986 }
19987 produce_special_glyphs (it, IT_TRUNCATION);
19988 }
19989 it->glyph_row->truncated_on_right_p = 1;
19990 }
19991 break;
19992 }
19993 }
19994
19995 /* Maybe insert a truncation at the left. */
19996 if (it->first_visible_x
19997 && IT_CHARPOS (*it) > 0)
19998 {
19999 if (!FRAME_WINDOW_P (it->f))
20000 insert_left_trunc_glyphs (it);
20001 it->glyph_row->truncated_on_left_p = 1;
20002 }
20003
20004 it->face_id = saved_face_id;
20005
20006 /* Value is number of columns displayed. */
20007 return it->hpos - hpos_at_start;
20008 }
20009
20010
20011 \f
20012 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20013 appears as an element of LIST or as the car of an element of LIST.
20014 If PROPVAL is a list, compare each element against LIST in that
20015 way, and return 1/2 if any element of PROPVAL is found in LIST.
20016 Otherwise return 0. This function cannot quit.
20017 The return value is 2 if the text is invisible but with an ellipsis
20018 and 1 if it's invisible and without an ellipsis. */
20019
20020 int
20021 invisible_p (register Lisp_Object propval, Lisp_Object list)
20022 {
20023 register Lisp_Object tail, proptail;
20024
20025 for (tail = list; CONSP (tail); tail = XCDR (tail))
20026 {
20027 register Lisp_Object tem;
20028 tem = XCAR (tail);
20029 if (EQ (propval, tem))
20030 return 1;
20031 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20032 return NILP (XCDR (tem)) ? 1 : 2;
20033 }
20034
20035 if (CONSP (propval))
20036 {
20037 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20038 {
20039 Lisp_Object propelt;
20040 propelt = XCAR (proptail);
20041 for (tail = list; CONSP (tail); tail = XCDR (tail))
20042 {
20043 register Lisp_Object tem;
20044 tem = XCAR (tail);
20045 if (EQ (propelt, tem))
20046 return 1;
20047 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20048 return NILP (XCDR (tem)) ? 1 : 2;
20049 }
20050 }
20051 }
20052
20053 return 0;
20054 }
20055
20056 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20057 doc: /* Non-nil if the property makes the text invisible.
20058 POS-OR-PROP can be a marker or number, in which case it is taken to be
20059 a position in the current buffer and the value of the `invisible' property
20060 is checked; or it can be some other value, which is then presumed to be the
20061 value of the `invisible' property of the text of interest.
20062 The non-nil value returned can be t for truly invisible text or something
20063 else if the text is replaced by an ellipsis. */)
20064 (Lisp_Object pos_or_prop)
20065 {
20066 Lisp_Object prop
20067 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20068 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20069 : pos_or_prop);
20070 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20071 return (invis == 0 ? Qnil
20072 : invis == 1 ? Qt
20073 : make_number (invis));
20074 }
20075
20076 /* Calculate a width or height in pixels from a specification using
20077 the following elements:
20078
20079 SPEC ::=
20080 NUM - a (fractional) multiple of the default font width/height
20081 (NUM) - specifies exactly NUM pixels
20082 UNIT - a fixed number of pixels, see below.
20083 ELEMENT - size of a display element in pixels, see below.
20084 (NUM . SPEC) - equals NUM * SPEC
20085 (+ SPEC SPEC ...) - add pixel values
20086 (- SPEC SPEC ...) - subtract pixel values
20087 (- SPEC) - negate pixel value
20088
20089 NUM ::=
20090 INT or FLOAT - a number constant
20091 SYMBOL - use symbol's (buffer local) variable binding.
20092
20093 UNIT ::=
20094 in - pixels per inch *)
20095 mm - pixels per 1/1000 meter *)
20096 cm - pixels per 1/100 meter *)
20097 width - width of current font in pixels.
20098 height - height of current font in pixels.
20099
20100 *) using the ratio(s) defined in display-pixels-per-inch.
20101
20102 ELEMENT ::=
20103
20104 left-fringe - left fringe width in pixels
20105 right-fringe - right fringe width in pixels
20106
20107 left-margin - left margin width in pixels
20108 right-margin - right margin width in pixels
20109
20110 scroll-bar - scroll-bar area width in pixels
20111
20112 Examples:
20113
20114 Pixels corresponding to 5 inches:
20115 (5 . in)
20116
20117 Total width of non-text areas on left side of window (if scroll-bar is on left):
20118 '(space :width (+ left-fringe left-margin scroll-bar))
20119
20120 Align to first text column (in header line):
20121 '(space :align-to 0)
20122
20123 Align to middle of text area minus half the width of variable `my-image'
20124 containing a loaded image:
20125 '(space :align-to (0.5 . (- text my-image)))
20126
20127 Width of left margin minus width of 1 character in the default font:
20128 '(space :width (- left-margin 1))
20129
20130 Width of left margin minus width of 2 characters in the current font:
20131 '(space :width (- left-margin (2 . width)))
20132
20133 Center 1 character over left-margin (in header line):
20134 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20135
20136 Different ways to express width of left fringe plus left margin minus one pixel:
20137 '(space :width (- (+ left-fringe left-margin) (1)))
20138 '(space :width (+ left-fringe left-margin (- (1))))
20139 '(space :width (+ left-fringe left-margin (-1)))
20140
20141 */
20142
20143 #define NUMVAL(X) \
20144 ((INTEGERP (X) || FLOATP (X)) \
20145 ? XFLOATINT (X) \
20146 : - 1)
20147
20148 int
20149 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20150 struct font *font, int width_p, int *align_to)
20151 {
20152 double pixels;
20153
20154 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20155 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20156
20157 if (NILP (prop))
20158 return OK_PIXELS (0);
20159
20160 xassert (FRAME_LIVE_P (it->f));
20161
20162 if (SYMBOLP (prop))
20163 {
20164 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20165 {
20166 char *unit = SSDATA (SYMBOL_NAME (prop));
20167
20168 if (unit[0] == 'i' && unit[1] == 'n')
20169 pixels = 1.0;
20170 else if (unit[0] == 'm' && unit[1] == 'm')
20171 pixels = 25.4;
20172 else if (unit[0] == 'c' && unit[1] == 'm')
20173 pixels = 2.54;
20174 else
20175 pixels = 0;
20176 if (pixels > 0)
20177 {
20178 double ppi;
20179 #ifdef HAVE_WINDOW_SYSTEM
20180 if (FRAME_WINDOW_P (it->f)
20181 && (ppi = (width_p
20182 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20183 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20184 ppi > 0))
20185 return OK_PIXELS (ppi / pixels);
20186 #endif
20187
20188 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20189 || (CONSP (Vdisplay_pixels_per_inch)
20190 && (ppi = (width_p
20191 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20192 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20193 ppi > 0)))
20194 return OK_PIXELS (ppi / pixels);
20195
20196 return 0;
20197 }
20198 }
20199
20200 #ifdef HAVE_WINDOW_SYSTEM
20201 if (EQ (prop, Qheight))
20202 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20203 if (EQ (prop, Qwidth))
20204 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20205 #else
20206 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20207 return OK_PIXELS (1);
20208 #endif
20209
20210 if (EQ (prop, Qtext))
20211 return OK_PIXELS (width_p
20212 ? window_box_width (it->w, TEXT_AREA)
20213 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20214
20215 if (align_to && *align_to < 0)
20216 {
20217 *res = 0;
20218 if (EQ (prop, Qleft))
20219 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20220 if (EQ (prop, Qright))
20221 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20222 if (EQ (prop, Qcenter))
20223 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20224 + window_box_width (it->w, TEXT_AREA) / 2);
20225 if (EQ (prop, Qleft_fringe))
20226 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20227 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20228 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20229 if (EQ (prop, Qright_fringe))
20230 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20231 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20232 : window_box_right_offset (it->w, TEXT_AREA));
20233 if (EQ (prop, Qleft_margin))
20234 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20235 if (EQ (prop, Qright_margin))
20236 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20237 if (EQ (prop, Qscroll_bar))
20238 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20239 ? 0
20240 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20241 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20242 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20243 : 0)));
20244 }
20245 else
20246 {
20247 if (EQ (prop, Qleft_fringe))
20248 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20249 if (EQ (prop, Qright_fringe))
20250 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20251 if (EQ (prop, Qleft_margin))
20252 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20253 if (EQ (prop, Qright_margin))
20254 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20255 if (EQ (prop, Qscroll_bar))
20256 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20257 }
20258
20259 prop = Fbuffer_local_value (prop, it->w->buffer);
20260 }
20261
20262 if (INTEGERP (prop) || FLOATP (prop))
20263 {
20264 int base_unit = (width_p
20265 ? FRAME_COLUMN_WIDTH (it->f)
20266 : FRAME_LINE_HEIGHT (it->f));
20267 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20268 }
20269
20270 if (CONSP (prop))
20271 {
20272 Lisp_Object car = XCAR (prop);
20273 Lisp_Object cdr = XCDR (prop);
20274
20275 if (SYMBOLP (car))
20276 {
20277 #ifdef HAVE_WINDOW_SYSTEM
20278 if (FRAME_WINDOW_P (it->f)
20279 && valid_image_p (prop))
20280 {
20281 int id = lookup_image (it->f, prop);
20282 struct image *img = IMAGE_FROM_ID (it->f, id);
20283
20284 return OK_PIXELS (width_p ? img->width : img->height);
20285 }
20286 #endif
20287 if (EQ (car, Qplus) || EQ (car, Qminus))
20288 {
20289 int first = 1;
20290 double px;
20291
20292 pixels = 0;
20293 while (CONSP (cdr))
20294 {
20295 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20296 font, width_p, align_to))
20297 return 0;
20298 if (first)
20299 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20300 else
20301 pixels += px;
20302 cdr = XCDR (cdr);
20303 }
20304 if (EQ (car, Qminus))
20305 pixels = -pixels;
20306 return OK_PIXELS (pixels);
20307 }
20308
20309 car = Fbuffer_local_value (car, it->w->buffer);
20310 }
20311
20312 if (INTEGERP (car) || FLOATP (car))
20313 {
20314 double fact;
20315 pixels = XFLOATINT (car);
20316 if (NILP (cdr))
20317 return OK_PIXELS (pixels);
20318 if (calc_pixel_width_or_height (&fact, it, cdr,
20319 font, width_p, align_to))
20320 return OK_PIXELS (pixels * fact);
20321 return 0;
20322 }
20323
20324 return 0;
20325 }
20326
20327 return 0;
20328 }
20329
20330 \f
20331 /***********************************************************************
20332 Glyph Display
20333 ***********************************************************************/
20334
20335 #ifdef HAVE_WINDOW_SYSTEM
20336
20337 #if GLYPH_DEBUG
20338
20339 void
20340 dump_glyph_string (s)
20341 struct glyph_string *s;
20342 {
20343 fprintf (stderr, "glyph string\n");
20344 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20345 s->x, s->y, s->width, s->height);
20346 fprintf (stderr, " ybase = %d\n", s->ybase);
20347 fprintf (stderr, " hl = %d\n", s->hl);
20348 fprintf (stderr, " left overhang = %d, right = %d\n",
20349 s->left_overhang, s->right_overhang);
20350 fprintf (stderr, " nchars = %d\n", s->nchars);
20351 fprintf (stderr, " extends to end of line = %d\n",
20352 s->extends_to_end_of_line_p);
20353 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20354 fprintf (stderr, " bg width = %d\n", s->background_width);
20355 }
20356
20357 #endif /* GLYPH_DEBUG */
20358
20359 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20360 of XChar2b structures for S; it can't be allocated in
20361 init_glyph_string because it must be allocated via `alloca'. W
20362 is the window on which S is drawn. ROW and AREA are the glyph row
20363 and area within the row from which S is constructed. START is the
20364 index of the first glyph structure covered by S. HL is a
20365 face-override for drawing S. */
20366
20367 #ifdef HAVE_NTGUI
20368 #define OPTIONAL_HDC(hdc) HDC hdc,
20369 #define DECLARE_HDC(hdc) HDC hdc;
20370 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20371 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20372 #endif
20373
20374 #ifndef OPTIONAL_HDC
20375 #define OPTIONAL_HDC(hdc)
20376 #define DECLARE_HDC(hdc)
20377 #define ALLOCATE_HDC(hdc, f)
20378 #define RELEASE_HDC(hdc, f)
20379 #endif
20380
20381 static void
20382 init_glyph_string (struct glyph_string *s,
20383 OPTIONAL_HDC (hdc)
20384 XChar2b *char2b, struct window *w, struct glyph_row *row,
20385 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20386 {
20387 memset (s, 0, sizeof *s);
20388 s->w = w;
20389 s->f = XFRAME (w->frame);
20390 #ifdef HAVE_NTGUI
20391 s->hdc = hdc;
20392 #endif
20393 s->display = FRAME_X_DISPLAY (s->f);
20394 s->window = FRAME_X_WINDOW (s->f);
20395 s->char2b = char2b;
20396 s->hl = hl;
20397 s->row = row;
20398 s->area = area;
20399 s->first_glyph = row->glyphs[area] + start;
20400 s->height = row->height;
20401 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20402 s->ybase = s->y + row->ascent;
20403 }
20404
20405
20406 /* Append the list of glyph strings with head H and tail T to the list
20407 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20408
20409 static INLINE void
20410 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20411 struct glyph_string *h, struct glyph_string *t)
20412 {
20413 if (h)
20414 {
20415 if (*head)
20416 (*tail)->next = h;
20417 else
20418 *head = h;
20419 h->prev = *tail;
20420 *tail = t;
20421 }
20422 }
20423
20424
20425 /* Prepend the list of glyph strings with head H and tail T to the
20426 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20427 result. */
20428
20429 static INLINE void
20430 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20431 struct glyph_string *h, struct glyph_string *t)
20432 {
20433 if (h)
20434 {
20435 if (*head)
20436 (*head)->prev = t;
20437 else
20438 *tail = t;
20439 t->next = *head;
20440 *head = h;
20441 }
20442 }
20443
20444
20445 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20446 Set *HEAD and *TAIL to the resulting list. */
20447
20448 static INLINE void
20449 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20450 struct glyph_string *s)
20451 {
20452 s->next = s->prev = NULL;
20453 append_glyph_string_lists (head, tail, s, s);
20454 }
20455
20456
20457 /* Get face and two-byte form of character C in face FACE_ID on frame F.
20458 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
20459 make sure that X resources for the face returned are allocated.
20460 Value is a pointer to a realized face that is ready for display if
20461 DISPLAY_P is non-zero. */
20462
20463 static INLINE struct face *
20464 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20465 XChar2b *char2b, int display_p)
20466 {
20467 struct face *face = FACE_FROM_ID (f, face_id);
20468
20469 if (face->font)
20470 {
20471 unsigned code = face->font->driver->encode_char (face->font, c);
20472
20473 if (code != FONT_INVALID_CODE)
20474 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20475 else
20476 STORE_XCHAR2B (char2b, 0, 0);
20477 }
20478
20479 /* Make sure X resources of the face are allocated. */
20480 #ifdef HAVE_X_WINDOWS
20481 if (display_p)
20482 #endif
20483 {
20484 xassert (face != NULL);
20485 PREPARE_FACE_FOR_DISPLAY (f, face);
20486 }
20487
20488 return face;
20489 }
20490
20491
20492 /* Get face and two-byte form of character glyph GLYPH on frame F.
20493 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20494 a pointer to a realized face that is ready for display. */
20495
20496 static INLINE struct face *
20497 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20498 XChar2b *char2b, int *two_byte_p)
20499 {
20500 struct face *face;
20501
20502 xassert (glyph->type == CHAR_GLYPH);
20503 face = FACE_FROM_ID (f, glyph->face_id);
20504
20505 if (two_byte_p)
20506 *two_byte_p = 0;
20507
20508 if (face->font)
20509 {
20510 unsigned code;
20511
20512 if (CHAR_BYTE8_P (glyph->u.ch))
20513 code = CHAR_TO_BYTE8 (glyph->u.ch);
20514 else
20515 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20516
20517 if (code != FONT_INVALID_CODE)
20518 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20519 else
20520 STORE_XCHAR2B (char2b, 0, 0);
20521 }
20522
20523 /* Make sure X resources of the face are allocated. */
20524 xassert (face != NULL);
20525 PREPARE_FACE_FOR_DISPLAY (f, face);
20526 return face;
20527 }
20528
20529
20530 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20531 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20532
20533 static INLINE int
20534 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20535 {
20536 unsigned code;
20537
20538 if (CHAR_BYTE8_P (c))
20539 code = CHAR_TO_BYTE8 (c);
20540 else
20541 code = font->driver->encode_char (font, c);
20542
20543 if (code == FONT_INVALID_CODE)
20544 return 0;
20545 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20546 return 1;
20547 }
20548
20549
20550 /* Fill glyph string S with composition components specified by S->cmp.
20551
20552 BASE_FACE is the base face of the composition.
20553 S->cmp_from is the index of the first component for S.
20554
20555 OVERLAPS non-zero means S should draw the foreground only, and use
20556 its physical height for clipping. See also draw_glyphs.
20557
20558 Value is the index of a component not in S. */
20559
20560 static int
20561 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20562 int overlaps)
20563 {
20564 int i;
20565 /* For all glyphs of this composition, starting at the offset
20566 S->cmp_from, until we reach the end of the definition or encounter a
20567 glyph that requires the different face, add it to S. */
20568 struct face *face;
20569
20570 xassert (s);
20571
20572 s->for_overlaps = overlaps;
20573 s->face = NULL;
20574 s->font = NULL;
20575 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20576 {
20577 int c = COMPOSITION_GLYPH (s->cmp, i);
20578
20579 if (c != '\t')
20580 {
20581 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20582 -1, Qnil);
20583
20584 face = get_char_face_and_encoding (s->f, c, face_id,
20585 s->char2b + i, 1);
20586 if (face)
20587 {
20588 if (! s->face)
20589 {
20590 s->face = face;
20591 s->font = s->face->font;
20592 }
20593 else if (s->face != face)
20594 break;
20595 }
20596 }
20597 ++s->nchars;
20598 }
20599 s->cmp_to = i;
20600
20601 /* All glyph strings for the same composition has the same width,
20602 i.e. the width set for the first component of the composition. */
20603 s->width = s->first_glyph->pixel_width;
20604
20605 /* If the specified font could not be loaded, use the frame's
20606 default font, but record the fact that we couldn't load it in
20607 the glyph string so that we can draw rectangles for the
20608 characters of the glyph string. */
20609 if (s->font == NULL)
20610 {
20611 s->font_not_found_p = 1;
20612 s->font = FRAME_FONT (s->f);
20613 }
20614
20615 /* Adjust base line for subscript/superscript text. */
20616 s->ybase += s->first_glyph->voffset;
20617
20618 /* This glyph string must always be drawn with 16-bit functions. */
20619 s->two_byte_p = 1;
20620
20621 return s->cmp_to;
20622 }
20623
20624 static int
20625 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20626 int start, int end, int overlaps)
20627 {
20628 struct glyph *glyph, *last;
20629 Lisp_Object lgstring;
20630 int i;
20631
20632 s->for_overlaps = overlaps;
20633 glyph = s->row->glyphs[s->area] + start;
20634 last = s->row->glyphs[s->area] + end;
20635 s->cmp_id = glyph->u.cmp.id;
20636 s->cmp_from = glyph->slice.cmp.from;
20637 s->cmp_to = glyph->slice.cmp.to + 1;
20638 s->face = FACE_FROM_ID (s->f, face_id);
20639 lgstring = composition_gstring_from_id (s->cmp_id);
20640 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20641 glyph++;
20642 while (glyph < last
20643 && glyph->u.cmp.automatic
20644 && glyph->u.cmp.id == s->cmp_id
20645 && s->cmp_to == glyph->slice.cmp.from)
20646 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20647
20648 for (i = s->cmp_from; i < s->cmp_to; i++)
20649 {
20650 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20651 unsigned code = LGLYPH_CODE (lglyph);
20652
20653 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20654 }
20655 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20656 return glyph - s->row->glyphs[s->area];
20657 }
20658
20659
20660 /* Fill glyph string S from a sequence glyphs for glyphless characters.
20661 See the comment of fill_glyph_string for arguments.
20662 Value is the index of the first glyph not in S. */
20663
20664
20665 static int
20666 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
20667 int start, int end, int overlaps)
20668 {
20669 struct glyph *glyph, *last;
20670 int voffset;
20671
20672 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
20673 s->for_overlaps = overlaps;
20674 glyph = s->row->glyphs[s->area] + start;
20675 last = s->row->glyphs[s->area] + end;
20676 voffset = glyph->voffset;
20677 s->face = FACE_FROM_ID (s->f, face_id);
20678 s->font = s->face->font;
20679 s->nchars = 1;
20680 s->width = glyph->pixel_width;
20681 glyph++;
20682 while (glyph < last
20683 && glyph->type == GLYPHLESS_GLYPH
20684 && glyph->voffset == voffset
20685 && glyph->face_id == face_id)
20686 {
20687 s->nchars++;
20688 s->width += glyph->pixel_width;
20689 glyph++;
20690 }
20691 s->ybase += voffset;
20692 return glyph - s->row->glyphs[s->area];
20693 }
20694
20695
20696 /* Fill glyph string S from a sequence of character glyphs.
20697
20698 FACE_ID is the face id of the string. START is the index of the
20699 first glyph to consider, END is the index of the last + 1.
20700 OVERLAPS non-zero means S should draw the foreground only, and use
20701 its physical height for clipping. See also draw_glyphs.
20702
20703 Value is the index of the first glyph not in S. */
20704
20705 static int
20706 fill_glyph_string (struct glyph_string *s, int face_id,
20707 int start, int end, int overlaps)
20708 {
20709 struct glyph *glyph, *last;
20710 int voffset;
20711 int glyph_not_available_p;
20712
20713 xassert (s->f == XFRAME (s->w->frame));
20714 xassert (s->nchars == 0);
20715 xassert (start >= 0 && end > start);
20716
20717 s->for_overlaps = overlaps;
20718 glyph = s->row->glyphs[s->area] + start;
20719 last = s->row->glyphs[s->area] + end;
20720 voffset = glyph->voffset;
20721 s->padding_p = glyph->padding_p;
20722 glyph_not_available_p = glyph->glyph_not_available_p;
20723
20724 while (glyph < last
20725 && glyph->type == CHAR_GLYPH
20726 && glyph->voffset == voffset
20727 /* Same face id implies same font, nowadays. */
20728 && glyph->face_id == face_id
20729 && glyph->glyph_not_available_p == glyph_not_available_p)
20730 {
20731 int two_byte_p;
20732
20733 s->face = get_glyph_face_and_encoding (s->f, glyph,
20734 s->char2b + s->nchars,
20735 &two_byte_p);
20736 s->two_byte_p = two_byte_p;
20737 ++s->nchars;
20738 xassert (s->nchars <= end - start);
20739 s->width += glyph->pixel_width;
20740 if (glyph++->padding_p != s->padding_p)
20741 break;
20742 }
20743
20744 s->font = s->face->font;
20745
20746 /* If the specified font could not be loaded, use the frame's font,
20747 but record the fact that we couldn't load it in
20748 S->font_not_found_p so that we can draw rectangles for the
20749 characters of the glyph string. */
20750 if (s->font == NULL || glyph_not_available_p)
20751 {
20752 s->font_not_found_p = 1;
20753 s->font = FRAME_FONT (s->f);
20754 }
20755
20756 /* Adjust base line for subscript/superscript text. */
20757 s->ybase += voffset;
20758
20759 xassert (s->face && s->face->gc);
20760 return glyph - s->row->glyphs[s->area];
20761 }
20762
20763
20764 /* Fill glyph string S from image glyph S->first_glyph. */
20765
20766 static void
20767 fill_image_glyph_string (struct glyph_string *s)
20768 {
20769 xassert (s->first_glyph->type == IMAGE_GLYPH);
20770 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20771 xassert (s->img);
20772 s->slice = s->first_glyph->slice.img;
20773 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20774 s->font = s->face->font;
20775 s->width = s->first_glyph->pixel_width;
20776
20777 /* Adjust base line for subscript/superscript text. */
20778 s->ybase += s->first_glyph->voffset;
20779 }
20780
20781
20782 /* Fill glyph string S from a sequence of stretch glyphs.
20783
20784 START is the index of the first glyph to consider,
20785 END is the index of the last + 1.
20786
20787 Value is the index of the first glyph not in S. */
20788
20789 static int
20790 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
20791 {
20792 struct glyph *glyph, *last;
20793 int voffset, face_id;
20794
20795 xassert (s->first_glyph->type == STRETCH_GLYPH);
20796
20797 glyph = s->row->glyphs[s->area] + start;
20798 last = s->row->glyphs[s->area] + end;
20799 face_id = glyph->face_id;
20800 s->face = FACE_FROM_ID (s->f, face_id);
20801 s->font = s->face->font;
20802 s->width = glyph->pixel_width;
20803 s->nchars = 1;
20804 voffset = glyph->voffset;
20805
20806 for (++glyph;
20807 (glyph < last
20808 && glyph->type == STRETCH_GLYPH
20809 && glyph->voffset == voffset
20810 && glyph->face_id == face_id);
20811 ++glyph)
20812 s->width += glyph->pixel_width;
20813
20814 /* Adjust base line for subscript/superscript text. */
20815 s->ybase += voffset;
20816
20817 /* The case that face->gc == 0 is handled when drawing the glyph
20818 string by calling PREPARE_FACE_FOR_DISPLAY. */
20819 xassert (s->face);
20820 return glyph - s->row->glyphs[s->area];
20821 }
20822
20823 static struct font_metrics *
20824 get_per_char_metric (struct font *font, XChar2b *char2b)
20825 {
20826 static struct font_metrics metrics;
20827 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20828
20829 if (! font || code == FONT_INVALID_CODE)
20830 return NULL;
20831 font->driver->text_extents (font, &code, 1, &metrics);
20832 return &metrics;
20833 }
20834
20835 /* EXPORT for RIF:
20836 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20837 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20838 assumed to be zero. */
20839
20840 void
20841 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20842 {
20843 *left = *right = 0;
20844
20845 if (glyph->type == CHAR_GLYPH)
20846 {
20847 struct face *face;
20848 XChar2b char2b;
20849 struct font_metrics *pcm;
20850
20851 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20852 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
20853 {
20854 if (pcm->rbearing > pcm->width)
20855 *right = pcm->rbearing - pcm->width;
20856 if (pcm->lbearing < 0)
20857 *left = -pcm->lbearing;
20858 }
20859 }
20860 else if (glyph->type == COMPOSITE_GLYPH)
20861 {
20862 if (! glyph->u.cmp.automatic)
20863 {
20864 struct composition *cmp = composition_table[glyph->u.cmp.id];
20865
20866 if (cmp->rbearing > cmp->pixel_width)
20867 *right = cmp->rbearing - cmp->pixel_width;
20868 if (cmp->lbearing < 0)
20869 *left = - cmp->lbearing;
20870 }
20871 else
20872 {
20873 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20874 struct font_metrics metrics;
20875
20876 composition_gstring_width (gstring, glyph->slice.cmp.from,
20877 glyph->slice.cmp.to + 1, &metrics);
20878 if (metrics.rbearing > metrics.width)
20879 *right = metrics.rbearing - metrics.width;
20880 if (metrics.lbearing < 0)
20881 *left = - metrics.lbearing;
20882 }
20883 }
20884 }
20885
20886
20887 /* Return the index of the first glyph preceding glyph string S that
20888 is overwritten by S because of S's left overhang. Value is -1
20889 if no glyphs are overwritten. */
20890
20891 static int
20892 left_overwritten (struct glyph_string *s)
20893 {
20894 int k;
20895
20896 if (s->left_overhang)
20897 {
20898 int x = 0, i;
20899 struct glyph *glyphs = s->row->glyphs[s->area];
20900 int first = s->first_glyph - glyphs;
20901
20902 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20903 x -= glyphs[i].pixel_width;
20904
20905 k = i + 1;
20906 }
20907 else
20908 k = -1;
20909
20910 return k;
20911 }
20912
20913
20914 /* Return the index of the first glyph preceding glyph string S that
20915 is overwriting S because of its right overhang. Value is -1 if no
20916 glyph in front of S overwrites S. */
20917
20918 static int
20919 left_overwriting (struct glyph_string *s)
20920 {
20921 int i, k, x;
20922 struct glyph *glyphs = s->row->glyphs[s->area];
20923 int first = s->first_glyph - glyphs;
20924
20925 k = -1;
20926 x = 0;
20927 for (i = first - 1; i >= 0; --i)
20928 {
20929 int left, right;
20930 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20931 if (x + right > 0)
20932 k = i;
20933 x -= glyphs[i].pixel_width;
20934 }
20935
20936 return k;
20937 }
20938
20939
20940 /* Return the index of the last glyph following glyph string S that is
20941 overwritten by S because of S's right overhang. Value is -1 if
20942 no such glyph is found. */
20943
20944 static int
20945 right_overwritten (struct glyph_string *s)
20946 {
20947 int k = -1;
20948
20949 if (s->right_overhang)
20950 {
20951 int x = 0, i;
20952 struct glyph *glyphs = s->row->glyphs[s->area];
20953 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20954 int end = s->row->used[s->area];
20955
20956 for (i = first; i < end && s->right_overhang > x; ++i)
20957 x += glyphs[i].pixel_width;
20958
20959 k = i;
20960 }
20961
20962 return k;
20963 }
20964
20965
20966 /* Return the index of the last glyph following glyph string S that
20967 overwrites S because of its left overhang. Value is negative
20968 if no such glyph is found. */
20969
20970 static int
20971 right_overwriting (struct glyph_string *s)
20972 {
20973 int i, k, x;
20974 int end = s->row->used[s->area];
20975 struct glyph *glyphs = s->row->glyphs[s->area];
20976 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20977
20978 k = -1;
20979 x = 0;
20980 for (i = first; i < end; ++i)
20981 {
20982 int left, right;
20983 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20984 if (x - left < 0)
20985 k = i;
20986 x += glyphs[i].pixel_width;
20987 }
20988
20989 return k;
20990 }
20991
20992
20993 /* Set background width of glyph string S. START is the index of the
20994 first glyph following S. LAST_X is the right-most x-position + 1
20995 in the drawing area. */
20996
20997 static INLINE void
20998 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
20999 {
21000 /* If the face of this glyph string has to be drawn to the end of
21001 the drawing area, set S->extends_to_end_of_line_p. */
21002
21003 if (start == s->row->used[s->area]
21004 && s->area == TEXT_AREA
21005 && ((s->row->fill_line_p
21006 && (s->hl == DRAW_NORMAL_TEXT
21007 || s->hl == DRAW_IMAGE_RAISED
21008 || s->hl == DRAW_IMAGE_SUNKEN))
21009 || s->hl == DRAW_MOUSE_FACE))
21010 s->extends_to_end_of_line_p = 1;
21011
21012 /* If S extends its face to the end of the line, set its
21013 background_width to the distance to the right edge of the drawing
21014 area. */
21015 if (s->extends_to_end_of_line_p)
21016 s->background_width = last_x - s->x + 1;
21017 else
21018 s->background_width = s->width;
21019 }
21020
21021
21022 /* Compute overhangs and x-positions for glyph string S and its
21023 predecessors, or successors. X is the starting x-position for S.
21024 BACKWARD_P non-zero means process predecessors. */
21025
21026 static void
21027 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21028 {
21029 if (backward_p)
21030 {
21031 while (s)
21032 {
21033 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21034 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21035 x -= s->width;
21036 s->x = x;
21037 s = s->prev;
21038 }
21039 }
21040 else
21041 {
21042 while (s)
21043 {
21044 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21045 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21046 s->x = x;
21047 x += s->width;
21048 s = s->next;
21049 }
21050 }
21051 }
21052
21053
21054
21055 /* The following macros are only called from draw_glyphs below.
21056 They reference the following parameters of that function directly:
21057 `w', `row', `area', and `overlap_p'
21058 as well as the following local variables:
21059 `s', `f', and `hdc' (in W32) */
21060
21061 #ifdef HAVE_NTGUI
21062 /* On W32, silently add local `hdc' variable to argument list of
21063 init_glyph_string. */
21064 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21065 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21066 #else
21067 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21068 init_glyph_string (s, char2b, w, row, area, start, hl)
21069 #endif
21070
21071 /* Add a glyph string for a stretch glyph to the list of strings
21072 between HEAD and TAIL. START is the index of the stretch glyph in
21073 row area AREA of glyph row ROW. END is the index of the last glyph
21074 in that glyph row area. X is the current output position assigned
21075 to the new glyph string constructed. HL overrides that face of the
21076 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21077 is the right-most x-position of the drawing area. */
21078
21079 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21080 and below -- keep them on one line. */
21081 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21082 do \
21083 { \
21084 s = (struct glyph_string *) alloca (sizeof *s); \
21085 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21086 START = fill_stretch_glyph_string (s, START, END); \
21087 append_glyph_string (&HEAD, &TAIL, s); \
21088 s->x = (X); \
21089 } \
21090 while (0)
21091
21092
21093 /* Add a glyph string for an image glyph to the list of strings
21094 between HEAD and TAIL. START is the index of the image glyph in
21095 row area AREA of glyph row ROW. END is the index of the last glyph
21096 in that glyph row area. X is the current output position assigned
21097 to the new glyph string constructed. HL overrides that face of the
21098 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21099 is the right-most x-position of the drawing area. */
21100
21101 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21102 do \
21103 { \
21104 s = (struct glyph_string *) alloca (sizeof *s); \
21105 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21106 fill_image_glyph_string (s); \
21107 append_glyph_string (&HEAD, &TAIL, s); \
21108 ++START; \
21109 s->x = (X); \
21110 } \
21111 while (0)
21112
21113
21114 /* Add a glyph string for a sequence of character glyphs to the list
21115 of strings between HEAD and TAIL. START is the index of the first
21116 glyph in row area AREA of glyph row ROW that is part of the new
21117 glyph string. END is the index of the last glyph in that glyph row
21118 area. X is the current output position assigned to the new glyph
21119 string constructed. HL overrides that face of the glyph; e.g. it
21120 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21121 right-most x-position of the drawing area. */
21122
21123 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21124 do \
21125 { \
21126 int face_id; \
21127 XChar2b *char2b; \
21128 \
21129 face_id = (row)->glyphs[area][START].face_id; \
21130 \
21131 s = (struct glyph_string *) alloca (sizeof *s); \
21132 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21133 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21134 append_glyph_string (&HEAD, &TAIL, s); \
21135 s->x = (X); \
21136 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21137 } \
21138 while (0)
21139
21140
21141 /* Add a glyph string for a composite sequence to the list of strings
21142 between HEAD and TAIL. START is the index of the first glyph in
21143 row area AREA of glyph row ROW that is part of the new glyph
21144 string. END is the index of the last glyph in that glyph row area.
21145 X is the current output position assigned to the new glyph string
21146 constructed. HL overrides that face of the glyph; e.g. it is
21147 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21148 x-position of the drawing area. */
21149
21150 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21151 do { \
21152 int face_id = (row)->glyphs[area][START].face_id; \
21153 struct face *base_face = FACE_FROM_ID (f, face_id); \
21154 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21155 struct composition *cmp = composition_table[cmp_id]; \
21156 XChar2b *char2b; \
21157 struct glyph_string *first_s IF_LINT (= NULL); \
21158 int n; \
21159 \
21160 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21161 \
21162 /* Make glyph_strings for each glyph sequence that is drawable by \
21163 the same face, and append them to HEAD/TAIL. */ \
21164 for (n = 0; n < cmp->glyph_len;) \
21165 { \
21166 s = (struct glyph_string *) alloca (sizeof *s); \
21167 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21168 append_glyph_string (&(HEAD), &(TAIL), s); \
21169 s->cmp = cmp; \
21170 s->cmp_from = n; \
21171 s->x = (X); \
21172 if (n == 0) \
21173 first_s = s; \
21174 n = fill_composite_glyph_string (s, base_face, overlaps); \
21175 } \
21176 \
21177 ++START; \
21178 s = first_s; \
21179 } while (0)
21180
21181
21182 /* Add a glyph string for a glyph-string sequence to the list of strings
21183 between HEAD and TAIL. */
21184
21185 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21186 do { \
21187 int face_id; \
21188 XChar2b *char2b; \
21189 Lisp_Object gstring; \
21190 \
21191 face_id = (row)->glyphs[area][START].face_id; \
21192 gstring = (composition_gstring_from_id \
21193 ((row)->glyphs[area][START].u.cmp.id)); \
21194 s = (struct glyph_string *) alloca (sizeof *s); \
21195 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21196 * LGSTRING_GLYPH_LEN (gstring)); \
21197 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21198 append_glyph_string (&(HEAD), &(TAIL), s); \
21199 s->x = (X); \
21200 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21201 } while (0)
21202
21203
21204 /* Add a glyph string for a sequence of glyphless character's glyphs
21205 to the list of strings between HEAD and TAIL. The meanings of
21206 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21207
21208 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21209 do \
21210 { \
21211 int face_id; \
21212 \
21213 face_id = (row)->glyphs[area][START].face_id; \
21214 \
21215 s = (struct glyph_string *) alloca (sizeof *s); \
21216 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21217 append_glyph_string (&HEAD, &TAIL, s); \
21218 s->x = (X); \
21219 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21220 overlaps); \
21221 } \
21222 while (0)
21223
21224
21225 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21226 of AREA of glyph row ROW on window W between indices START and END.
21227 HL overrides the face for drawing glyph strings, e.g. it is
21228 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21229 x-positions of the drawing area.
21230
21231 This is an ugly monster macro construct because we must use alloca
21232 to allocate glyph strings (because draw_glyphs can be called
21233 asynchronously). */
21234
21235 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21236 do \
21237 { \
21238 HEAD = TAIL = NULL; \
21239 while (START < END) \
21240 { \
21241 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21242 switch (first_glyph->type) \
21243 { \
21244 case CHAR_GLYPH: \
21245 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21246 HL, X, LAST_X); \
21247 break; \
21248 \
21249 case COMPOSITE_GLYPH: \
21250 if (first_glyph->u.cmp.automatic) \
21251 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21252 HL, X, LAST_X); \
21253 else \
21254 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21255 HL, X, LAST_X); \
21256 break; \
21257 \
21258 case STRETCH_GLYPH: \
21259 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21260 HL, X, LAST_X); \
21261 break; \
21262 \
21263 case IMAGE_GLYPH: \
21264 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21265 HL, X, LAST_X); \
21266 break; \
21267 \
21268 case GLYPHLESS_GLYPH: \
21269 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21270 HL, X, LAST_X); \
21271 break; \
21272 \
21273 default: \
21274 abort (); \
21275 } \
21276 \
21277 if (s) \
21278 { \
21279 set_glyph_string_background_width (s, START, LAST_X); \
21280 (X) += s->width; \
21281 } \
21282 } \
21283 } while (0)
21284
21285
21286 /* Draw glyphs between START and END in AREA of ROW on window W,
21287 starting at x-position X. X is relative to AREA in W. HL is a
21288 face-override with the following meaning:
21289
21290 DRAW_NORMAL_TEXT draw normally
21291 DRAW_CURSOR draw in cursor face
21292 DRAW_MOUSE_FACE draw in mouse face.
21293 DRAW_INVERSE_VIDEO draw in mode line face
21294 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21295 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21296
21297 If OVERLAPS is non-zero, draw only the foreground of characters and
21298 clip to the physical height of ROW. Non-zero value also defines
21299 the overlapping part to be drawn:
21300
21301 OVERLAPS_PRED overlap with preceding rows
21302 OVERLAPS_SUCC overlap with succeeding rows
21303 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21304 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21305
21306 Value is the x-position reached, relative to AREA of W. */
21307
21308 static int
21309 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21310 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21311 enum draw_glyphs_face hl, int overlaps)
21312 {
21313 struct glyph_string *head, *tail;
21314 struct glyph_string *s;
21315 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21316 int i, j, x_reached, last_x, area_left = 0;
21317 struct frame *f = XFRAME (WINDOW_FRAME (w));
21318 DECLARE_HDC (hdc);
21319
21320 ALLOCATE_HDC (hdc, f);
21321
21322 /* Let's rather be paranoid than getting a SEGV. */
21323 end = min (end, row->used[area]);
21324 start = max (0, start);
21325 start = min (end, start);
21326
21327 /* Translate X to frame coordinates. Set last_x to the right
21328 end of the drawing area. */
21329 if (row->full_width_p)
21330 {
21331 /* X is relative to the left edge of W, without scroll bars
21332 or fringes. */
21333 area_left = WINDOW_LEFT_EDGE_X (w);
21334 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21335 }
21336 else
21337 {
21338 area_left = window_box_left (w, area);
21339 last_x = area_left + window_box_width (w, area);
21340 }
21341 x += area_left;
21342
21343 /* Build a doubly-linked list of glyph_string structures between
21344 head and tail from what we have to draw. Note that the macro
21345 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21346 the reason we use a separate variable `i'. */
21347 i = start;
21348 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21349 if (tail)
21350 x_reached = tail->x + tail->background_width;
21351 else
21352 x_reached = x;
21353
21354 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21355 the row, redraw some glyphs in front or following the glyph
21356 strings built above. */
21357 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21358 {
21359 struct glyph_string *h, *t;
21360 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21361 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
21362 int check_mouse_face = 0;
21363 int dummy_x = 0;
21364
21365 /* If mouse highlighting is on, we may need to draw adjacent
21366 glyphs using mouse-face highlighting. */
21367 if (area == TEXT_AREA && row->mouse_face_p)
21368 {
21369 struct glyph_row *mouse_beg_row, *mouse_end_row;
21370
21371 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21372 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21373
21374 if (row >= mouse_beg_row && row <= mouse_end_row)
21375 {
21376 check_mouse_face = 1;
21377 mouse_beg_col = (row == mouse_beg_row)
21378 ? hlinfo->mouse_face_beg_col : 0;
21379 mouse_end_col = (row == mouse_end_row)
21380 ? hlinfo->mouse_face_end_col
21381 : row->used[TEXT_AREA];
21382 }
21383 }
21384
21385 /* Compute overhangs for all glyph strings. */
21386 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21387 for (s = head; s; s = s->next)
21388 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21389
21390 /* Prepend glyph strings for glyphs in front of the first glyph
21391 string that are overwritten because of the first glyph
21392 string's left overhang. The background of all strings
21393 prepended must be drawn because the first glyph string
21394 draws over it. */
21395 i = left_overwritten (head);
21396 if (i >= 0)
21397 {
21398 enum draw_glyphs_face overlap_hl;
21399
21400 /* If this row contains mouse highlighting, attempt to draw
21401 the overlapped glyphs with the correct highlight. This
21402 code fails if the overlap encompasses more than one glyph
21403 and mouse-highlight spans only some of these glyphs.
21404 However, making it work perfectly involves a lot more
21405 code, and I don't know if the pathological case occurs in
21406 practice, so we'll stick to this for now. --- cyd */
21407 if (check_mouse_face
21408 && mouse_beg_col < start && mouse_end_col > i)
21409 overlap_hl = DRAW_MOUSE_FACE;
21410 else
21411 overlap_hl = DRAW_NORMAL_TEXT;
21412
21413 j = i;
21414 BUILD_GLYPH_STRINGS (j, start, h, t,
21415 overlap_hl, dummy_x, last_x);
21416 start = i;
21417 compute_overhangs_and_x (t, head->x, 1);
21418 prepend_glyph_string_lists (&head, &tail, h, t);
21419 clip_head = head;
21420 }
21421
21422 /* Prepend glyph strings for glyphs in front of the first glyph
21423 string that overwrite that glyph string because of their
21424 right overhang. For these strings, only the foreground must
21425 be drawn, because it draws over the glyph string at `head'.
21426 The background must not be drawn because this would overwrite
21427 right overhangs of preceding glyphs for which no glyph
21428 strings exist. */
21429 i = left_overwriting (head);
21430 if (i >= 0)
21431 {
21432 enum draw_glyphs_face overlap_hl;
21433
21434 if (check_mouse_face
21435 && mouse_beg_col < start && mouse_end_col > i)
21436 overlap_hl = DRAW_MOUSE_FACE;
21437 else
21438 overlap_hl = DRAW_NORMAL_TEXT;
21439
21440 clip_head = head;
21441 BUILD_GLYPH_STRINGS (i, start, h, t,
21442 overlap_hl, dummy_x, last_x);
21443 for (s = h; s; s = s->next)
21444 s->background_filled_p = 1;
21445 compute_overhangs_and_x (t, head->x, 1);
21446 prepend_glyph_string_lists (&head, &tail, h, t);
21447 }
21448
21449 /* Append glyphs strings for glyphs following the last glyph
21450 string tail that are overwritten by tail. The background of
21451 these strings has to be drawn because tail's foreground draws
21452 over it. */
21453 i = right_overwritten (tail);
21454 if (i >= 0)
21455 {
21456 enum draw_glyphs_face overlap_hl;
21457
21458 if (check_mouse_face
21459 && mouse_beg_col < i && mouse_end_col > end)
21460 overlap_hl = DRAW_MOUSE_FACE;
21461 else
21462 overlap_hl = DRAW_NORMAL_TEXT;
21463
21464 BUILD_GLYPH_STRINGS (end, i, h, t,
21465 overlap_hl, x, last_x);
21466 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21467 we don't have `end = i;' here. */
21468 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21469 append_glyph_string_lists (&head, &tail, h, t);
21470 clip_tail = tail;
21471 }
21472
21473 /* Append glyph strings for glyphs following the last glyph
21474 string tail that overwrite tail. The foreground of such
21475 glyphs has to be drawn because it writes into the background
21476 of tail. The background must not be drawn because it could
21477 paint over the foreground of following glyphs. */
21478 i = right_overwriting (tail);
21479 if (i >= 0)
21480 {
21481 enum draw_glyphs_face overlap_hl;
21482 if (check_mouse_face
21483 && mouse_beg_col < i && mouse_end_col > end)
21484 overlap_hl = DRAW_MOUSE_FACE;
21485 else
21486 overlap_hl = DRAW_NORMAL_TEXT;
21487
21488 clip_tail = tail;
21489 i++; /* We must include the Ith glyph. */
21490 BUILD_GLYPH_STRINGS (end, i, h, t,
21491 overlap_hl, x, last_x);
21492 for (s = h; s; s = s->next)
21493 s->background_filled_p = 1;
21494 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21495 append_glyph_string_lists (&head, &tail, h, t);
21496 }
21497 if (clip_head || clip_tail)
21498 for (s = head; s; s = s->next)
21499 {
21500 s->clip_head = clip_head;
21501 s->clip_tail = clip_tail;
21502 }
21503 }
21504
21505 /* Draw all strings. */
21506 for (s = head; s; s = s->next)
21507 FRAME_RIF (f)->draw_glyph_string (s);
21508
21509 #ifndef HAVE_NS
21510 /* When focus a sole frame and move horizontally, this sets on_p to 0
21511 causing a failure to erase prev cursor position. */
21512 if (area == TEXT_AREA
21513 && !row->full_width_p
21514 /* When drawing overlapping rows, only the glyph strings'
21515 foreground is drawn, which doesn't erase a cursor
21516 completely. */
21517 && !overlaps)
21518 {
21519 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21520 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21521 : (tail ? tail->x + tail->background_width : x));
21522 x0 -= area_left;
21523 x1 -= area_left;
21524
21525 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21526 row->y, MATRIX_ROW_BOTTOM_Y (row));
21527 }
21528 #endif
21529
21530 /* Value is the x-position up to which drawn, relative to AREA of W.
21531 This doesn't include parts drawn because of overhangs. */
21532 if (row->full_width_p)
21533 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21534 else
21535 x_reached -= area_left;
21536
21537 RELEASE_HDC (hdc, f);
21538
21539 return x_reached;
21540 }
21541
21542 /* Expand row matrix if too narrow. Don't expand if area
21543 is not present. */
21544
21545 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21546 { \
21547 if (!fonts_changed_p \
21548 && (it->glyph_row->glyphs[area] \
21549 < it->glyph_row->glyphs[area + 1])) \
21550 { \
21551 it->w->ncols_scale_factor++; \
21552 fonts_changed_p = 1; \
21553 } \
21554 }
21555
21556 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21557 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21558
21559 static INLINE void
21560 append_glyph (struct it *it)
21561 {
21562 struct glyph *glyph;
21563 enum glyph_row_area area = it->area;
21564
21565 xassert (it->glyph_row);
21566 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21567
21568 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21569 if (glyph < it->glyph_row->glyphs[area + 1])
21570 {
21571 /* If the glyph row is reversed, we need to prepend the glyph
21572 rather than append it. */
21573 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21574 {
21575 struct glyph *g;
21576
21577 /* Make room for the additional glyph. */
21578 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21579 g[1] = *g;
21580 glyph = it->glyph_row->glyphs[area];
21581 }
21582 glyph->charpos = CHARPOS (it->position);
21583 glyph->object = it->object;
21584 if (it->pixel_width > 0)
21585 {
21586 glyph->pixel_width = it->pixel_width;
21587 glyph->padding_p = 0;
21588 }
21589 else
21590 {
21591 /* Assure at least 1-pixel width. Otherwise, cursor can't
21592 be displayed correctly. */
21593 glyph->pixel_width = 1;
21594 glyph->padding_p = 1;
21595 }
21596 glyph->ascent = it->ascent;
21597 glyph->descent = it->descent;
21598 glyph->voffset = it->voffset;
21599 glyph->type = CHAR_GLYPH;
21600 glyph->avoid_cursor_p = it->avoid_cursor_p;
21601 glyph->multibyte_p = it->multibyte_p;
21602 glyph->left_box_line_p = it->start_of_box_run_p;
21603 glyph->right_box_line_p = it->end_of_box_run_p;
21604 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21605 || it->phys_descent > it->descent);
21606 glyph->glyph_not_available_p = it->glyph_not_available_p;
21607 glyph->face_id = it->face_id;
21608 glyph->u.ch = it->char_to_display;
21609 glyph->slice.img = null_glyph_slice;
21610 glyph->font_type = FONT_TYPE_UNKNOWN;
21611 if (it->bidi_p)
21612 {
21613 glyph->resolved_level = it->bidi_it.resolved_level;
21614 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21615 abort ();
21616 glyph->bidi_type = it->bidi_it.type;
21617 }
21618 else
21619 {
21620 glyph->resolved_level = 0;
21621 glyph->bidi_type = UNKNOWN_BT;
21622 }
21623 ++it->glyph_row->used[area];
21624 }
21625 else
21626 IT_EXPAND_MATRIX_WIDTH (it, area);
21627 }
21628
21629 /* Store one glyph for the composition IT->cmp_it.id in
21630 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21631 non-null. */
21632
21633 static INLINE void
21634 append_composite_glyph (struct it *it)
21635 {
21636 struct glyph *glyph;
21637 enum glyph_row_area area = it->area;
21638
21639 xassert (it->glyph_row);
21640
21641 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21642 if (glyph < it->glyph_row->glyphs[area + 1])
21643 {
21644 /* If the glyph row is reversed, we need to prepend the glyph
21645 rather than append it. */
21646 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21647 {
21648 struct glyph *g;
21649
21650 /* Make room for the new glyph. */
21651 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21652 g[1] = *g;
21653 glyph = it->glyph_row->glyphs[it->area];
21654 }
21655 glyph->charpos = it->cmp_it.charpos;
21656 glyph->object = it->object;
21657 glyph->pixel_width = it->pixel_width;
21658 glyph->ascent = it->ascent;
21659 glyph->descent = it->descent;
21660 glyph->voffset = it->voffset;
21661 glyph->type = COMPOSITE_GLYPH;
21662 if (it->cmp_it.ch < 0)
21663 {
21664 glyph->u.cmp.automatic = 0;
21665 glyph->u.cmp.id = it->cmp_it.id;
21666 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21667 }
21668 else
21669 {
21670 glyph->u.cmp.automatic = 1;
21671 glyph->u.cmp.id = it->cmp_it.id;
21672 glyph->slice.cmp.from = it->cmp_it.from;
21673 glyph->slice.cmp.to = it->cmp_it.to - 1;
21674 }
21675 glyph->avoid_cursor_p = it->avoid_cursor_p;
21676 glyph->multibyte_p = it->multibyte_p;
21677 glyph->left_box_line_p = it->start_of_box_run_p;
21678 glyph->right_box_line_p = it->end_of_box_run_p;
21679 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21680 || it->phys_descent > it->descent);
21681 glyph->padding_p = 0;
21682 glyph->glyph_not_available_p = 0;
21683 glyph->face_id = it->face_id;
21684 glyph->font_type = FONT_TYPE_UNKNOWN;
21685 if (it->bidi_p)
21686 {
21687 glyph->resolved_level = it->bidi_it.resolved_level;
21688 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21689 abort ();
21690 glyph->bidi_type = it->bidi_it.type;
21691 }
21692 ++it->glyph_row->used[area];
21693 }
21694 else
21695 IT_EXPAND_MATRIX_WIDTH (it, area);
21696 }
21697
21698
21699 /* Change IT->ascent and IT->height according to the setting of
21700 IT->voffset. */
21701
21702 static INLINE void
21703 take_vertical_position_into_account (struct it *it)
21704 {
21705 if (it->voffset)
21706 {
21707 if (it->voffset < 0)
21708 /* Increase the ascent so that we can display the text higher
21709 in the line. */
21710 it->ascent -= it->voffset;
21711 else
21712 /* Increase the descent so that we can display the text lower
21713 in the line. */
21714 it->descent += it->voffset;
21715 }
21716 }
21717
21718
21719 /* Produce glyphs/get display metrics for the image IT is loaded with.
21720 See the description of struct display_iterator in dispextern.h for
21721 an overview of struct display_iterator. */
21722
21723 static void
21724 produce_image_glyph (struct it *it)
21725 {
21726 struct image *img;
21727 struct face *face;
21728 int glyph_ascent, crop;
21729 struct glyph_slice slice;
21730
21731 xassert (it->what == IT_IMAGE);
21732
21733 face = FACE_FROM_ID (it->f, it->face_id);
21734 xassert (face);
21735 /* Make sure X resources of the face is loaded. */
21736 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21737
21738 if (it->image_id < 0)
21739 {
21740 /* Fringe bitmap. */
21741 it->ascent = it->phys_ascent = 0;
21742 it->descent = it->phys_descent = 0;
21743 it->pixel_width = 0;
21744 it->nglyphs = 0;
21745 return;
21746 }
21747
21748 img = IMAGE_FROM_ID (it->f, it->image_id);
21749 xassert (img);
21750 /* Make sure X resources of the image is loaded. */
21751 prepare_image_for_display (it->f, img);
21752
21753 slice.x = slice.y = 0;
21754 slice.width = img->width;
21755 slice.height = img->height;
21756
21757 if (INTEGERP (it->slice.x))
21758 slice.x = XINT (it->slice.x);
21759 else if (FLOATP (it->slice.x))
21760 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21761
21762 if (INTEGERP (it->slice.y))
21763 slice.y = XINT (it->slice.y);
21764 else if (FLOATP (it->slice.y))
21765 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21766
21767 if (INTEGERP (it->slice.width))
21768 slice.width = XINT (it->slice.width);
21769 else if (FLOATP (it->slice.width))
21770 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21771
21772 if (INTEGERP (it->slice.height))
21773 slice.height = XINT (it->slice.height);
21774 else if (FLOATP (it->slice.height))
21775 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21776
21777 if (slice.x >= img->width)
21778 slice.x = img->width;
21779 if (slice.y >= img->height)
21780 slice.y = img->height;
21781 if (slice.x + slice.width >= img->width)
21782 slice.width = img->width - slice.x;
21783 if (slice.y + slice.height > img->height)
21784 slice.height = img->height - slice.y;
21785
21786 if (slice.width == 0 || slice.height == 0)
21787 return;
21788
21789 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21790
21791 it->descent = slice.height - glyph_ascent;
21792 if (slice.y == 0)
21793 it->descent += img->vmargin;
21794 if (slice.y + slice.height == img->height)
21795 it->descent += img->vmargin;
21796 it->phys_descent = it->descent;
21797
21798 it->pixel_width = slice.width;
21799 if (slice.x == 0)
21800 it->pixel_width += img->hmargin;
21801 if (slice.x + slice.width == img->width)
21802 it->pixel_width += img->hmargin;
21803
21804 /* It's quite possible for images to have an ascent greater than
21805 their height, so don't get confused in that case. */
21806 if (it->descent < 0)
21807 it->descent = 0;
21808
21809 it->nglyphs = 1;
21810
21811 if (face->box != FACE_NO_BOX)
21812 {
21813 if (face->box_line_width > 0)
21814 {
21815 if (slice.y == 0)
21816 it->ascent += face->box_line_width;
21817 if (slice.y + slice.height == img->height)
21818 it->descent += face->box_line_width;
21819 }
21820
21821 if (it->start_of_box_run_p && slice.x == 0)
21822 it->pixel_width += eabs (face->box_line_width);
21823 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21824 it->pixel_width += eabs (face->box_line_width);
21825 }
21826
21827 take_vertical_position_into_account (it);
21828
21829 /* Automatically crop wide image glyphs at right edge so we can
21830 draw the cursor on same display row. */
21831 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21832 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21833 {
21834 it->pixel_width -= crop;
21835 slice.width -= crop;
21836 }
21837
21838 if (it->glyph_row)
21839 {
21840 struct glyph *glyph;
21841 enum glyph_row_area area = it->area;
21842
21843 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21844 if (glyph < it->glyph_row->glyphs[area + 1])
21845 {
21846 glyph->charpos = CHARPOS (it->position);
21847 glyph->object = it->object;
21848 glyph->pixel_width = it->pixel_width;
21849 glyph->ascent = glyph_ascent;
21850 glyph->descent = it->descent;
21851 glyph->voffset = it->voffset;
21852 glyph->type = IMAGE_GLYPH;
21853 glyph->avoid_cursor_p = it->avoid_cursor_p;
21854 glyph->multibyte_p = it->multibyte_p;
21855 glyph->left_box_line_p = it->start_of_box_run_p;
21856 glyph->right_box_line_p = it->end_of_box_run_p;
21857 glyph->overlaps_vertically_p = 0;
21858 glyph->padding_p = 0;
21859 glyph->glyph_not_available_p = 0;
21860 glyph->face_id = it->face_id;
21861 glyph->u.img_id = img->id;
21862 glyph->slice.img = slice;
21863 glyph->font_type = FONT_TYPE_UNKNOWN;
21864 if (it->bidi_p)
21865 {
21866 glyph->resolved_level = it->bidi_it.resolved_level;
21867 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21868 abort ();
21869 glyph->bidi_type = it->bidi_it.type;
21870 }
21871 ++it->glyph_row->used[area];
21872 }
21873 else
21874 IT_EXPAND_MATRIX_WIDTH (it, area);
21875 }
21876 }
21877
21878
21879 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21880 of the glyph, WIDTH and HEIGHT are the width and height of the
21881 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21882
21883 static void
21884 append_stretch_glyph (struct it *it, Lisp_Object object,
21885 int width, int height, int ascent)
21886 {
21887 struct glyph *glyph;
21888 enum glyph_row_area area = it->area;
21889
21890 xassert (ascent >= 0 && ascent <= height);
21891
21892 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21893 if (glyph < it->glyph_row->glyphs[area + 1])
21894 {
21895 /* If the glyph row is reversed, we need to prepend the glyph
21896 rather than append it. */
21897 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21898 {
21899 struct glyph *g;
21900
21901 /* Make room for the additional glyph. */
21902 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21903 g[1] = *g;
21904 glyph = it->glyph_row->glyphs[area];
21905 }
21906 glyph->charpos = CHARPOS (it->position);
21907 glyph->object = object;
21908 glyph->pixel_width = width;
21909 glyph->ascent = ascent;
21910 glyph->descent = height - ascent;
21911 glyph->voffset = it->voffset;
21912 glyph->type = STRETCH_GLYPH;
21913 glyph->avoid_cursor_p = it->avoid_cursor_p;
21914 glyph->multibyte_p = it->multibyte_p;
21915 glyph->left_box_line_p = it->start_of_box_run_p;
21916 glyph->right_box_line_p = it->end_of_box_run_p;
21917 glyph->overlaps_vertically_p = 0;
21918 glyph->padding_p = 0;
21919 glyph->glyph_not_available_p = 0;
21920 glyph->face_id = it->face_id;
21921 glyph->u.stretch.ascent = ascent;
21922 glyph->u.stretch.height = height;
21923 glyph->slice.img = null_glyph_slice;
21924 glyph->font_type = FONT_TYPE_UNKNOWN;
21925 if (it->bidi_p)
21926 {
21927 glyph->resolved_level = it->bidi_it.resolved_level;
21928 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21929 abort ();
21930 glyph->bidi_type = it->bidi_it.type;
21931 }
21932 else
21933 {
21934 glyph->resolved_level = 0;
21935 glyph->bidi_type = UNKNOWN_BT;
21936 }
21937 ++it->glyph_row->used[area];
21938 }
21939 else
21940 IT_EXPAND_MATRIX_WIDTH (it, area);
21941 }
21942
21943
21944 /* Produce a stretch glyph for iterator IT. IT->object is the value
21945 of the glyph property displayed. The value must be a list
21946 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21947 being recognized:
21948
21949 1. `:width WIDTH' specifies that the space should be WIDTH *
21950 canonical char width wide. WIDTH may be an integer or floating
21951 point number.
21952
21953 2. `:relative-width FACTOR' specifies that the width of the stretch
21954 should be computed from the width of the first character having the
21955 `glyph' property, and should be FACTOR times that width.
21956
21957 3. `:align-to HPOS' specifies that the space should be wide enough
21958 to reach HPOS, a value in canonical character units.
21959
21960 Exactly one of the above pairs must be present.
21961
21962 4. `:height HEIGHT' specifies that the height of the stretch produced
21963 should be HEIGHT, measured in canonical character units.
21964
21965 5. `:relative-height FACTOR' specifies that the height of the
21966 stretch should be FACTOR times the height of the characters having
21967 the glyph property.
21968
21969 Either none or exactly one of 4 or 5 must be present.
21970
21971 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21972 of the stretch should be used for the ascent of the stretch.
21973 ASCENT must be in the range 0 <= ASCENT <= 100. */
21974
21975 static void
21976 produce_stretch_glyph (struct it *it)
21977 {
21978 /* (space :width WIDTH :height HEIGHT ...) */
21979 Lisp_Object prop, plist;
21980 int width = 0, height = 0, align_to = -1;
21981 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21982 int ascent = 0;
21983 double tem;
21984 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21985 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21986
21987 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21988
21989 /* List should start with `space'. */
21990 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21991 plist = XCDR (it->object);
21992
21993 /* Compute the width of the stretch. */
21994 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21995 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21996 {
21997 /* Absolute width `:width WIDTH' specified and valid. */
21998 zero_width_ok_p = 1;
21999 width = (int)tem;
22000 }
22001 else if (prop = Fplist_get (plist, QCrelative_width),
22002 NUMVAL (prop) > 0)
22003 {
22004 /* Relative width `:relative-width FACTOR' specified and valid.
22005 Compute the width of the characters having the `glyph'
22006 property. */
22007 struct it it2;
22008 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22009
22010 it2 = *it;
22011 if (it->multibyte_p)
22012 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22013 else
22014 {
22015 it2.c = it2.char_to_display = *p, it2.len = 1;
22016 if (! ASCII_CHAR_P (it2.c))
22017 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22018 }
22019
22020 it2.glyph_row = NULL;
22021 it2.what = IT_CHARACTER;
22022 x_produce_glyphs (&it2);
22023 width = NUMVAL (prop) * it2.pixel_width;
22024 }
22025 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22026 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22027 {
22028 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22029 align_to = (align_to < 0
22030 ? 0
22031 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22032 else if (align_to < 0)
22033 align_to = window_box_left_offset (it->w, TEXT_AREA);
22034 width = max (0, (int)tem + align_to - it->current_x);
22035 zero_width_ok_p = 1;
22036 }
22037 else
22038 /* Nothing specified -> width defaults to canonical char width. */
22039 width = FRAME_COLUMN_WIDTH (it->f);
22040
22041 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22042 width = 1;
22043
22044 /* Compute height. */
22045 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22046 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22047 {
22048 height = (int)tem;
22049 zero_height_ok_p = 1;
22050 }
22051 else if (prop = Fplist_get (plist, QCrelative_height),
22052 NUMVAL (prop) > 0)
22053 height = FONT_HEIGHT (font) * NUMVAL (prop);
22054 else
22055 height = FONT_HEIGHT (font);
22056
22057 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22058 height = 1;
22059
22060 /* Compute percentage of height used for ascent. If
22061 `:ascent ASCENT' is present and valid, use that. Otherwise,
22062 derive the ascent from the font in use. */
22063 if (prop = Fplist_get (plist, QCascent),
22064 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22065 ascent = height * NUMVAL (prop) / 100.0;
22066 else if (!NILP (prop)
22067 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22068 ascent = min (max (0, (int)tem), height);
22069 else
22070 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22071
22072 if (width > 0 && it->line_wrap != TRUNCATE
22073 && it->current_x + width > it->last_visible_x)
22074 width = it->last_visible_x - it->current_x - 1;
22075
22076 if (width > 0 && height > 0 && it->glyph_row)
22077 {
22078 Lisp_Object object = it->stack[it->sp - 1].string;
22079 if (!STRINGP (object))
22080 object = it->w->buffer;
22081 append_stretch_glyph (it, object, width, height, ascent);
22082 }
22083
22084 it->pixel_width = width;
22085 it->ascent = it->phys_ascent = ascent;
22086 it->descent = it->phys_descent = height - it->ascent;
22087 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22088
22089 take_vertical_position_into_account (it);
22090 }
22091
22092 /* Calculate line-height and line-spacing properties.
22093 An integer value specifies explicit pixel value.
22094 A float value specifies relative value to current face height.
22095 A cons (float . face-name) specifies relative value to
22096 height of specified face font.
22097
22098 Returns height in pixels, or nil. */
22099
22100
22101 static Lisp_Object
22102 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22103 int boff, int override)
22104 {
22105 Lisp_Object face_name = Qnil;
22106 int ascent, descent, height;
22107
22108 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22109 return val;
22110
22111 if (CONSP (val))
22112 {
22113 face_name = XCAR (val);
22114 val = XCDR (val);
22115 if (!NUMBERP (val))
22116 val = make_number (1);
22117 if (NILP (face_name))
22118 {
22119 height = it->ascent + it->descent;
22120 goto scale;
22121 }
22122 }
22123
22124 if (NILP (face_name))
22125 {
22126 font = FRAME_FONT (it->f);
22127 boff = FRAME_BASELINE_OFFSET (it->f);
22128 }
22129 else if (EQ (face_name, Qt))
22130 {
22131 override = 0;
22132 }
22133 else
22134 {
22135 int face_id;
22136 struct face *face;
22137
22138 face_id = lookup_named_face (it->f, face_name, 0);
22139 if (face_id < 0)
22140 return make_number (-1);
22141
22142 face = FACE_FROM_ID (it->f, face_id);
22143 font = face->font;
22144 if (font == NULL)
22145 return make_number (-1);
22146 boff = font->baseline_offset;
22147 if (font->vertical_centering)
22148 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22149 }
22150
22151 ascent = FONT_BASE (font) + boff;
22152 descent = FONT_DESCENT (font) - boff;
22153
22154 if (override)
22155 {
22156 it->override_ascent = ascent;
22157 it->override_descent = descent;
22158 it->override_boff = boff;
22159 }
22160
22161 height = ascent + descent;
22162
22163 scale:
22164 if (FLOATP (val))
22165 height = (int)(XFLOAT_DATA (val) * height);
22166 else if (INTEGERP (val))
22167 height *= XINT (val);
22168
22169 return make_number (height);
22170 }
22171
22172
22173 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22174 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22175 and only if this is for a character for which no font was found.
22176
22177 If the display method (it->glyphless_method) is
22178 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22179 length of the acronym or the hexadecimal string, UPPER_XOFF and
22180 UPPER_YOFF are pixel offsets for the upper part of the string,
22181 LOWER_XOFF and LOWER_YOFF are for the lower part.
22182
22183 For the other display methods, LEN through LOWER_YOFF are zero. */
22184
22185 static void
22186 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22187 short upper_xoff, short upper_yoff,
22188 short lower_xoff, short lower_yoff)
22189 {
22190 struct glyph *glyph;
22191 enum glyph_row_area area = it->area;
22192
22193 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22194 if (glyph < it->glyph_row->glyphs[area + 1])
22195 {
22196 /* If the glyph row is reversed, we need to prepend the glyph
22197 rather than append it. */
22198 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22199 {
22200 struct glyph *g;
22201
22202 /* Make room for the additional glyph. */
22203 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22204 g[1] = *g;
22205 glyph = it->glyph_row->glyphs[area];
22206 }
22207 glyph->charpos = CHARPOS (it->position);
22208 glyph->object = it->object;
22209 glyph->pixel_width = it->pixel_width;
22210 glyph->ascent = it->ascent;
22211 glyph->descent = it->descent;
22212 glyph->voffset = it->voffset;
22213 glyph->type = GLYPHLESS_GLYPH;
22214 glyph->u.glyphless.method = it->glyphless_method;
22215 glyph->u.glyphless.for_no_font = for_no_font;
22216 glyph->u.glyphless.len = len;
22217 glyph->u.glyphless.ch = it->c;
22218 glyph->slice.glyphless.upper_xoff = upper_xoff;
22219 glyph->slice.glyphless.upper_yoff = upper_yoff;
22220 glyph->slice.glyphless.lower_xoff = lower_xoff;
22221 glyph->slice.glyphless.lower_yoff = lower_yoff;
22222 glyph->avoid_cursor_p = it->avoid_cursor_p;
22223 glyph->multibyte_p = it->multibyte_p;
22224 glyph->left_box_line_p = it->start_of_box_run_p;
22225 glyph->right_box_line_p = it->end_of_box_run_p;
22226 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22227 || it->phys_descent > it->descent);
22228 glyph->padding_p = 0;
22229 glyph->glyph_not_available_p = 0;
22230 glyph->face_id = face_id;
22231 glyph->font_type = FONT_TYPE_UNKNOWN;
22232 if (it->bidi_p)
22233 {
22234 glyph->resolved_level = it->bidi_it.resolved_level;
22235 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22236 abort ();
22237 glyph->bidi_type = it->bidi_it.type;
22238 }
22239 ++it->glyph_row->used[area];
22240 }
22241 else
22242 IT_EXPAND_MATRIX_WIDTH (it, area);
22243 }
22244
22245
22246 /* Produce a glyph for a glyphless character for iterator IT.
22247 IT->glyphless_method specifies which method to use for displaying
22248 the character. See the description of enum
22249 glyphless_display_method in dispextern.h for the detail.
22250
22251 FOR_NO_FONT is nonzero if and only if this is for a character for
22252 which no font was found. ACRONYM, if non-nil, is an acronym string
22253 for the character. */
22254
22255 static void
22256 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22257 {
22258 int face_id;
22259 struct face *face;
22260 struct font *font;
22261 int base_width, base_height, width, height;
22262 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22263 int len;
22264
22265 /* Get the metrics of the base font. We always refer to the current
22266 ASCII face. */
22267 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22268 font = face->font ? face->font : FRAME_FONT (it->f);
22269 it->ascent = FONT_BASE (font) + font->baseline_offset;
22270 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22271 base_height = it->ascent + it->descent;
22272 base_width = font->average_width;
22273
22274 /* Get a face ID for the glyph by utilizing a cache (the same way as
22275 doen for `escape-glyph' in get_next_display_element). */
22276 if (it->f == last_glyphless_glyph_frame
22277 && it->face_id == last_glyphless_glyph_face_id)
22278 {
22279 face_id = last_glyphless_glyph_merged_face_id;
22280 }
22281 else
22282 {
22283 /* Merge the `glyphless-char' face into the current face. */
22284 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22285 last_glyphless_glyph_frame = it->f;
22286 last_glyphless_glyph_face_id = it->face_id;
22287 last_glyphless_glyph_merged_face_id = face_id;
22288 }
22289
22290 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22291 {
22292 it->pixel_width = THIN_SPACE_WIDTH;
22293 len = 0;
22294 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22295 }
22296 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22297 {
22298 width = CHAR_WIDTH (it->c);
22299 if (width == 0)
22300 width = 1;
22301 else if (width > 4)
22302 width = 4;
22303 it->pixel_width = base_width * width;
22304 len = 0;
22305 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22306 }
22307 else
22308 {
22309 char buf[7];
22310 const char *str;
22311 unsigned int code[6];
22312 int upper_len;
22313 int ascent, descent;
22314 struct font_metrics metrics_upper, metrics_lower;
22315
22316 face = FACE_FROM_ID (it->f, face_id);
22317 font = face->font ? face->font : FRAME_FONT (it->f);
22318 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22319
22320 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22321 {
22322 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22323 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22324 str = STRINGP (acronym) ? SSDATA (acronym) : "";
22325 }
22326 else
22327 {
22328 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22329 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22330 str = buf;
22331 }
22332 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22333 code[len] = font->driver->encode_char (font, str[len]);
22334 upper_len = (len + 1) / 2;
22335 font->driver->text_extents (font, code, upper_len,
22336 &metrics_upper);
22337 font->driver->text_extents (font, code + upper_len, len - upper_len,
22338 &metrics_lower);
22339
22340
22341
22342 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22343 width = max (metrics_upper.width, metrics_lower.width) + 4;
22344 upper_xoff = upper_yoff = 2; /* the typical case */
22345 if (base_width >= width)
22346 {
22347 /* Align the upper to the left, the lower to the right. */
22348 it->pixel_width = base_width;
22349 lower_xoff = base_width - 2 - metrics_lower.width;
22350 }
22351 else
22352 {
22353 /* Center the shorter one. */
22354 it->pixel_width = width;
22355 if (metrics_upper.width >= metrics_lower.width)
22356 lower_xoff = (width - metrics_lower.width) / 2;
22357 else
22358 {
22359 /* FIXME: This code doesn't look right. It formerly was
22360 missing the "lower_xoff = 0;", which couldn't have
22361 been right since it left lower_xoff uninitialized. */
22362 lower_xoff = 0;
22363 upper_xoff = (width - metrics_upper.width) / 2;
22364 }
22365 }
22366
22367 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22368 top, bottom, and between upper and lower strings. */
22369 height = (metrics_upper.ascent + metrics_upper.descent
22370 + metrics_lower.ascent + metrics_lower.descent) + 5;
22371 /* Center vertically.
22372 H:base_height, D:base_descent
22373 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22374
22375 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22376 descent = D - H/2 + h/2;
22377 lower_yoff = descent - 2 - ld;
22378 upper_yoff = lower_yoff - la - 1 - ud; */
22379 ascent = - (it->descent - (base_height + height + 1) / 2);
22380 descent = it->descent - (base_height - height) / 2;
22381 lower_yoff = descent - 2 - metrics_lower.descent;
22382 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22383 - metrics_upper.descent);
22384 /* Don't make the height shorter than the base height. */
22385 if (height > base_height)
22386 {
22387 it->ascent = ascent;
22388 it->descent = descent;
22389 }
22390 }
22391
22392 it->phys_ascent = it->ascent;
22393 it->phys_descent = it->descent;
22394 if (it->glyph_row)
22395 append_glyphless_glyph (it, face_id, for_no_font, len,
22396 upper_xoff, upper_yoff,
22397 lower_xoff, lower_yoff);
22398 it->nglyphs = 1;
22399 take_vertical_position_into_account (it);
22400 }
22401
22402
22403 /* RIF:
22404 Produce glyphs/get display metrics for the display element IT is
22405 loaded with. See the description of struct it in dispextern.h
22406 for an overview of struct it. */
22407
22408 void
22409 x_produce_glyphs (struct it *it)
22410 {
22411 int extra_line_spacing = it->extra_line_spacing;
22412
22413 it->glyph_not_available_p = 0;
22414
22415 if (it->what == IT_CHARACTER)
22416 {
22417 XChar2b char2b;
22418 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22419 struct font *font = face->font;
22420 struct font_metrics *pcm = NULL;
22421 int boff; /* baseline offset */
22422
22423 if (font == NULL)
22424 {
22425 /* When no suitable font is found, display this character by
22426 the method specified in the first extra slot of
22427 Vglyphless_char_display. */
22428 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22429
22430 xassert (it->what == IT_GLYPHLESS);
22431 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22432 goto done;
22433 }
22434
22435 boff = font->baseline_offset;
22436 if (font->vertical_centering)
22437 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22438
22439 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22440 {
22441 int stretched_p;
22442
22443 it->nglyphs = 1;
22444
22445 if (it->override_ascent >= 0)
22446 {
22447 it->ascent = it->override_ascent;
22448 it->descent = it->override_descent;
22449 boff = it->override_boff;
22450 }
22451 else
22452 {
22453 it->ascent = FONT_BASE (font) + boff;
22454 it->descent = FONT_DESCENT (font) - boff;
22455 }
22456
22457 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22458 {
22459 pcm = get_per_char_metric (font, &char2b);
22460 if (pcm->width == 0
22461 && pcm->rbearing == 0 && pcm->lbearing == 0)
22462 pcm = NULL;
22463 }
22464
22465 if (pcm)
22466 {
22467 it->phys_ascent = pcm->ascent + boff;
22468 it->phys_descent = pcm->descent - boff;
22469 it->pixel_width = pcm->width;
22470 }
22471 else
22472 {
22473 it->glyph_not_available_p = 1;
22474 it->phys_ascent = it->ascent;
22475 it->phys_descent = it->descent;
22476 it->pixel_width = font->space_width;
22477 }
22478
22479 if (it->constrain_row_ascent_descent_p)
22480 {
22481 if (it->descent > it->max_descent)
22482 {
22483 it->ascent += it->descent - it->max_descent;
22484 it->descent = it->max_descent;
22485 }
22486 if (it->ascent > it->max_ascent)
22487 {
22488 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22489 it->ascent = it->max_ascent;
22490 }
22491 it->phys_ascent = min (it->phys_ascent, it->ascent);
22492 it->phys_descent = min (it->phys_descent, it->descent);
22493 extra_line_spacing = 0;
22494 }
22495
22496 /* If this is a space inside a region of text with
22497 `space-width' property, change its width. */
22498 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22499 if (stretched_p)
22500 it->pixel_width *= XFLOATINT (it->space_width);
22501
22502 /* If face has a box, add the box thickness to the character
22503 height. If character has a box line to the left and/or
22504 right, add the box line width to the character's width. */
22505 if (face->box != FACE_NO_BOX)
22506 {
22507 int thick = face->box_line_width;
22508
22509 if (thick > 0)
22510 {
22511 it->ascent += thick;
22512 it->descent += thick;
22513 }
22514 else
22515 thick = -thick;
22516
22517 if (it->start_of_box_run_p)
22518 it->pixel_width += thick;
22519 if (it->end_of_box_run_p)
22520 it->pixel_width += thick;
22521 }
22522
22523 /* If face has an overline, add the height of the overline
22524 (1 pixel) and a 1 pixel margin to the character height. */
22525 if (face->overline_p)
22526 it->ascent += overline_margin;
22527
22528 if (it->constrain_row_ascent_descent_p)
22529 {
22530 if (it->ascent > it->max_ascent)
22531 it->ascent = it->max_ascent;
22532 if (it->descent > it->max_descent)
22533 it->descent = it->max_descent;
22534 }
22535
22536 take_vertical_position_into_account (it);
22537
22538 /* If we have to actually produce glyphs, do it. */
22539 if (it->glyph_row)
22540 {
22541 if (stretched_p)
22542 {
22543 /* Translate a space with a `space-width' property
22544 into a stretch glyph. */
22545 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22546 / FONT_HEIGHT (font));
22547 append_stretch_glyph (it, it->object, it->pixel_width,
22548 it->ascent + it->descent, ascent);
22549 }
22550 else
22551 append_glyph (it);
22552
22553 /* If characters with lbearing or rbearing are displayed
22554 in this line, record that fact in a flag of the
22555 glyph row. This is used to optimize X output code. */
22556 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22557 it->glyph_row->contains_overlapping_glyphs_p = 1;
22558 }
22559 if (! stretched_p && it->pixel_width == 0)
22560 /* We assure that all visible glyphs have at least 1-pixel
22561 width. */
22562 it->pixel_width = 1;
22563 }
22564 else if (it->char_to_display == '\n')
22565 {
22566 /* A newline has no width, but we need the height of the
22567 line. But if previous part of the line sets a height,
22568 don't increase that height */
22569
22570 Lisp_Object height;
22571 Lisp_Object total_height = Qnil;
22572
22573 it->override_ascent = -1;
22574 it->pixel_width = 0;
22575 it->nglyphs = 0;
22576
22577 height = get_it_property (it, Qline_height);
22578 /* Split (line-height total-height) list */
22579 if (CONSP (height)
22580 && CONSP (XCDR (height))
22581 && NILP (XCDR (XCDR (height))))
22582 {
22583 total_height = XCAR (XCDR (height));
22584 height = XCAR (height);
22585 }
22586 height = calc_line_height_property (it, height, font, boff, 1);
22587
22588 if (it->override_ascent >= 0)
22589 {
22590 it->ascent = it->override_ascent;
22591 it->descent = it->override_descent;
22592 boff = it->override_boff;
22593 }
22594 else
22595 {
22596 it->ascent = FONT_BASE (font) + boff;
22597 it->descent = FONT_DESCENT (font) - boff;
22598 }
22599
22600 if (EQ (height, Qt))
22601 {
22602 if (it->descent > it->max_descent)
22603 {
22604 it->ascent += it->descent - it->max_descent;
22605 it->descent = it->max_descent;
22606 }
22607 if (it->ascent > it->max_ascent)
22608 {
22609 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22610 it->ascent = it->max_ascent;
22611 }
22612 it->phys_ascent = min (it->phys_ascent, it->ascent);
22613 it->phys_descent = min (it->phys_descent, it->descent);
22614 it->constrain_row_ascent_descent_p = 1;
22615 extra_line_spacing = 0;
22616 }
22617 else
22618 {
22619 Lisp_Object spacing;
22620
22621 it->phys_ascent = it->ascent;
22622 it->phys_descent = it->descent;
22623
22624 if ((it->max_ascent > 0 || it->max_descent > 0)
22625 && face->box != FACE_NO_BOX
22626 && face->box_line_width > 0)
22627 {
22628 it->ascent += face->box_line_width;
22629 it->descent += face->box_line_width;
22630 }
22631 if (!NILP (height)
22632 && XINT (height) > it->ascent + it->descent)
22633 it->ascent = XINT (height) - it->descent;
22634
22635 if (!NILP (total_height))
22636 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22637 else
22638 {
22639 spacing = get_it_property (it, Qline_spacing);
22640 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22641 }
22642 if (INTEGERP (spacing))
22643 {
22644 extra_line_spacing = XINT (spacing);
22645 if (!NILP (total_height))
22646 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22647 }
22648 }
22649 }
22650 else /* i.e. (it->char_to_display == '\t') */
22651 {
22652 if (font->space_width > 0)
22653 {
22654 int tab_width = it->tab_width * font->space_width;
22655 int x = it->current_x + it->continuation_lines_width;
22656 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22657
22658 /* If the distance from the current position to the next tab
22659 stop is less than a space character width, use the
22660 tab stop after that. */
22661 if (next_tab_x - x < font->space_width)
22662 next_tab_x += tab_width;
22663
22664 it->pixel_width = next_tab_x - x;
22665 it->nglyphs = 1;
22666 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22667 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22668
22669 if (it->glyph_row)
22670 {
22671 append_stretch_glyph (it, it->object, it->pixel_width,
22672 it->ascent + it->descent, it->ascent);
22673 }
22674 }
22675 else
22676 {
22677 it->pixel_width = 0;
22678 it->nglyphs = 1;
22679 }
22680 }
22681 }
22682 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22683 {
22684 /* A static composition.
22685
22686 Note: A composition is represented as one glyph in the
22687 glyph matrix. There are no padding glyphs.
22688
22689 Important note: pixel_width, ascent, and descent are the
22690 values of what is drawn by draw_glyphs (i.e. the values of
22691 the overall glyphs composed). */
22692 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22693 int boff; /* baseline offset */
22694 struct composition *cmp = composition_table[it->cmp_it.id];
22695 int glyph_len = cmp->glyph_len;
22696 struct font *font = face->font;
22697
22698 it->nglyphs = 1;
22699
22700 /* If we have not yet calculated pixel size data of glyphs of
22701 the composition for the current face font, calculate them
22702 now. Theoretically, we have to check all fonts for the
22703 glyphs, but that requires much time and memory space. So,
22704 here we check only the font of the first glyph. This may
22705 lead to incorrect display, but it's very rare, and C-l
22706 (recenter-top-bottom) can correct the display anyway. */
22707 if (! cmp->font || cmp->font != font)
22708 {
22709 /* Ascent and descent of the font of the first character
22710 of this composition (adjusted by baseline offset).
22711 Ascent and descent of overall glyphs should not be less
22712 than these, respectively. */
22713 int font_ascent, font_descent, font_height;
22714 /* Bounding box of the overall glyphs. */
22715 int leftmost, rightmost, lowest, highest;
22716 int lbearing, rbearing;
22717 int i, width, ascent, descent;
22718 int left_padded = 0, right_padded = 0;
22719 int c;
22720 XChar2b char2b;
22721 struct font_metrics *pcm;
22722 int font_not_found_p;
22723 EMACS_INT pos;
22724
22725 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22726 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22727 break;
22728 if (glyph_len < cmp->glyph_len)
22729 right_padded = 1;
22730 for (i = 0; i < glyph_len; i++)
22731 {
22732 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22733 break;
22734 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22735 }
22736 if (i > 0)
22737 left_padded = 1;
22738
22739 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22740 : IT_CHARPOS (*it));
22741 /* If no suitable font is found, use the default font. */
22742 font_not_found_p = font == NULL;
22743 if (font_not_found_p)
22744 {
22745 face = face->ascii_face;
22746 font = face->font;
22747 }
22748 boff = font->baseline_offset;
22749 if (font->vertical_centering)
22750 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22751 font_ascent = FONT_BASE (font) + boff;
22752 font_descent = FONT_DESCENT (font) - boff;
22753 font_height = FONT_HEIGHT (font);
22754
22755 cmp->font = (void *) font;
22756
22757 pcm = NULL;
22758 if (! font_not_found_p)
22759 {
22760 get_char_face_and_encoding (it->f, c, it->face_id,
22761 &char2b, 0);
22762 pcm = get_per_char_metric (font, &char2b);
22763 }
22764
22765 /* Initialize the bounding box. */
22766 if (pcm)
22767 {
22768 width = pcm->width;
22769 ascent = pcm->ascent;
22770 descent = pcm->descent;
22771 lbearing = pcm->lbearing;
22772 rbearing = pcm->rbearing;
22773 }
22774 else
22775 {
22776 width = font->space_width;
22777 ascent = FONT_BASE (font);
22778 descent = FONT_DESCENT (font);
22779 lbearing = 0;
22780 rbearing = width;
22781 }
22782
22783 rightmost = width;
22784 leftmost = 0;
22785 lowest = - descent + boff;
22786 highest = ascent + boff;
22787
22788 if (! font_not_found_p
22789 && font->default_ascent
22790 && CHAR_TABLE_P (Vuse_default_ascent)
22791 && !NILP (Faref (Vuse_default_ascent,
22792 make_number (it->char_to_display))))
22793 highest = font->default_ascent + boff;
22794
22795 /* Draw the first glyph at the normal position. It may be
22796 shifted to right later if some other glyphs are drawn
22797 at the left. */
22798 cmp->offsets[i * 2] = 0;
22799 cmp->offsets[i * 2 + 1] = boff;
22800 cmp->lbearing = lbearing;
22801 cmp->rbearing = rbearing;
22802
22803 /* Set cmp->offsets for the remaining glyphs. */
22804 for (i++; i < glyph_len; i++)
22805 {
22806 int left, right, btm, top;
22807 int ch = COMPOSITION_GLYPH (cmp, i);
22808 int face_id;
22809 struct face *this_face;
22810
22811 if (ch == '\t')
22812 ch = ' ';
22813 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22814 this_face = FACE_FROM_ID (it->f, face_id);
22815 font = this_face->font;
22816
22817 if (font == NULL)
22818 pcm = NULL;
22819 else
22820 {
22821 get_char_face_and_encoding (it->f, ch, face_id,
22822 &char2b, 0);
22823 pcm = get_per_char_metric (font, &char2b);
22824 }
22825 if (! pcm)
22826 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22827 else
22828 {
22829 width = pcm->width;
22830 ascent = pcm->ascent;
22831 descent = pcm->descent;
22832 lbearing = pcm->lbearing;
22833 rbearing = pcm->rbearing;
22834 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22835 {
22836 /* Relative composition with or without
22837 alternate chars. */
22838 left = (leftmost + rightmost - width) / 2;
22839 btm = - descent + boff;
22840 if (font->relative_compose
22841 && (! CHAR_TABLE_P (Vignore_relative_composition)
22842 || NILP (Faref (Vignore_relative_composition,
22843 make_number (ch)))))
22844 {
22845
22846 if (- descent >= font->relative_compose)
22847 /* One extra pixel between two glyphs. */
22848 btm = highest + 1;
22849 else if (ascent <= 0)
22850 /* One extra pixel between two glyphs. */
22851 btm = lowest - 1 - ascent - descent;
22852 }
22853 }
22854 else
22855 {
22856 /* A composition rule is specified by an integer
22857 value that encodes global and new reference
22858 points (GREF and NREF). GREF and NREF are
22859 specified by numbers as below:
22860
22861 0---1---2 -- ascent
22862 | |
22863 | |
22864 | |
22865 9--10--11 -- center
22866 | |
22867 ---3---4---5--- baseline
22868 | |
22869 6---7---8 -- descent
22870 */
22871 int rule = COMPOSITION_RULE (cmp, i);
22872 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22873
22874 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22875 grefx = gref % 3, nrefx = nref % 3;
22876 grefy = gref / 3, nrefy = nref / 3;
22877 if (xoff)
22878 xoff = font_height * (xoff - 128) / 256;
22879 if (yoff)
22880 yoff = font_height * (yoff - 128) / 256;
22881
22882 left = (leftmost
22883 + grefx * (rightmost - leftmost) / 2
22884 - nrefx * width / 2
22885 + xoff);
22886
22887 btm = ((grefy == 0 ? highest
22888 : grefy == 1 ? 0
22889 : grefy == 2 ? lowest
22890 : (highest + lowest) / 2)
22891 - (nrefy == 0 ? ascent + descent
22892 : nrefy == 1 ? descent - boff
22893 : nrefy == 2 ? 0
22894 : (ascent + descent) / 2)
22895 + yoff);
22896 }
22897
22898 cmp->offsets[i * 2] = left;
22899 cmp->offsets[i * 2 + 1] = btm + descent;
22900
22901 /* Update the bounding box of the overall glyphs. */
22902 if (width > 0)
22903 {
22904 right = left + width;
22905 if (left < leftmost)
22906 leftmost = left;
22907 if (right > rightmost)
22908 rightmost = right;
22909 }
22910 top = btm + descent + ascent;
22911 if (top > highest)
22912 highest = top;
22913 if (btm < lowest)
22914 lowest = btm;
22915
22916 if (cmp->lbearing > left + lbearing)
22917 cmp->lbearing = left + lbearing;
22918 if (cmp->rbearing < left + rbearing)
22919 cmp->rbearing = left + rbearing;
22920 }
22921 }
22922
22923 /* If there are glyphs whose x-offsets are negative,
22924 shift all glyphs to the right and make all x-offsets
22925 non-negative. */
22926 if (leftmost < 0)
22927 {
22928 for (i = 0; i < cmp->glyph_len; i++)
22929 cmp->offsets[i * 2] -= leftmost;
22930 rightmost -= leftmost;
22931 cmp->lbearing -= leftmost;
22932 cmp->rbearing -= leftmost;
22933 }
22934
22935 if (left_padded && cmp->lbearing < 0)
22936 {
22937 for (i = 0; i < cmp->glyph_len; i++)
22938 cmp->offsets[i * 2] -= cmp->lbearing;
22939 rightmost -= cmp->lbearing;
22940 cmp->rbearing -= cmp->lbearing;
22941 cmp->lbearing = 0;
22942 }
22943 if (right_padded && rightmost < cmp->rbearing)
22944 {
22945 rightmost = cmp->rbearing;
22946 }
22947
22948 cmp->pixel_width = rightmost;
22949 cmp->ascent = highest;
22950 cmp->descent = - lowest;
22951 if (cmp->ascent < font_ascent)
22952 cmp->ascent = font_ascent;
22953 if (cmp->descent < font_descent)
22954 cmp->descent = font_descent;
22955 }
22956
22957 if (it->glyph_row
22958 && (cmp->lbearing < 0
22959 || cmp->rbearing > cmp->pixel_width))
22960 it->glyph_row->contains_overlapping_glyphs_p = 1;
22961
22962 it->pixel_width = cmp->pixel_width;
22963 it->ascent = it->phys_ascent = cmp->ascent;
22964 it->descent = it->phys_descent = cmp->descent;
22965 if (face->box != FACE_NO_BOX)
22966 {
22967 int thick = face->box_line_width;
22968
22969 if (thick > 0)
22970 {
22971 it->ascent += thick;
22972 it->descent += thick;
22973 }
22974 else
22975 thick = - thick;
22976
22977 if (it->start_of_box_run_p)
22978 it->pixel_width += thick;
22979 if (it->end_of_box_run_p)
22980 it->pixel_width += thick;
22981 }
22982
22983 /* If face has an overline, add the height of the overline
22984 (1 pixel) and a 1 pixel margin to the character height. */
22985 if (face->overline_p)
22986 it->ascent += overline_margin;
22987
22988 take_vertical_position_into_account (it);
22989 if (it->ascent < 0)
22990 it->ascent = 0;
22991 if (it->descent < 0)
22992 it->descent = 0;
22993
22994 if (it->glyph_row)
22995 append_composite_glyph (it);
22996 }
22997 else if (it->what == IT_COMPOSITION)
22998 {
22999 /* A dynamic (automatic) composition. */
23000 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23001 Lisp_Object gstring;
23002 struct font_metrics metrics;
23003
23004 gstring = composition_gstring_from_id (it->cmp_it.id);
23005 it->pixel_width
23006 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23007 &metrics);
23008 if (it->glyph_row
23009 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23010 it->glyph_row->contains_overlapping_glyphs_p = 1;
23011 it->ascent = it->phys_ascent = metrics.ascent;
23012 it->descent = it->phys_descent = metrics.descent;
23013 if (face->box != FACE_NO_BOX)
23014 {
23015 int thick = face->box_line_width;
23016
23017 if (thick > 0)
23018 {
23019 it->ascent += thick;
23020 it->descent += thick;
23021 }
23022 else
23023 thick = - thick;
23024
23025 if (it->start_of_box_run_p)
23026 it->pixel_width += thick;
23027 if (it->end_of_box_run_p)
23028 it->pixel_width += thick;
23029 }
23030 /* If face has an overline, add the height of the overline
23031 (1 pixel) and a 1 pixel margin to the character height. */
23032 if (face->overline_p)
23033 it->ascent += overline_margin;
23034 take_vertical_position_into_account (it);
23035 if (it->ascent < 0)
23036 it->ascent = 0;
23037 if (it->descent < 0)
23038 it->descent = 0;
23039
23040 if (it->glyph_row)
23041 append_composite_glyph (it);
23042 }
23043 else if (it->what == IT_GLYPHLESS)
23044 produce_glyphless_glyph (it, 0, Qnil);
23045 else if (it->what == IT_IMAGE)
23046 produce_image_glyph (it);
23047 else if (it->what == IT_STRETCH)
23048 produce_stretch_glyph (it);
23049
23050 done:
23051 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23052 because this isn't true for images with `:ascent 100'. */
23053 xassert (it->ascent >= 0 && it->descent >= 0);
23054 if (it->area == TEXT_AREA)
23055 it->current_x += it->pixel_width;
23056
23057 if (extra_line_spacing > 0)
23058 {
23059 it->descent += extra_line_spacing;
23060 if (extra_line_spacing > it->max_extra_line_spacing)
23061 it->max_extra_line_spacing = extra_line_spacing;
23062 }
23063
23064 it->max_ascent = max (it->max_ascent, it->ascent);
23065 it->max_descent = max (it->max_descent, it->descent);
23066 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23067 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23068 }
23069
23070 /* EXPORT for RIF:
23071 Output LEN glyphs starting at START at the nominal cursor position.
23072 Advance the nominal cursor over the text. The global variable
23073 updated_window contains the window being updated, updated_row is
23074 the glyph row being updated, and updated_area is the area of that
23075 row being updated. */
23076
23077 void
23078 x_write_glyphs (struct glyph *start, int len)
23079 {
23080 int x, hpos;
23081
23082 xassert (updated_window && updated_row);
23083 BLOCK_INPUT;
23084
23085 /* Write glyphs. */
23086
23087 hpos = start - updated_row->glyphs[updated_area];
23088 x = draw_glyphs (updated_window, output_cursor.x,
23089 updated_row, updated_area,
23090 hpos, hpos + len,
23091 DRAW_NORMAL_TEXT, 0);
23092
23093 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23094 if (updated_area == TEXT_AREA
23095 && updated_window->phys_cursor_on_p
23096 && updated_window->phys_cursor.vpos == output_cursor.vpos
23097 && updated_window->phys_cursor.hpos >= hpos
23098 && updated_window->phys_cursor.hpos < hpos + len)
23099 updated_window->phys_cursor_on_p = 0;
23100
23101 UNBLOCK_INPUT;
23102
23103 /* Advance the output cursor. */
23104 output_cursor.hpos += len;
23105 output_cursor.x = x;
23106 }
23107
23108
23109 /* EXPORT for RIF:
23110 Insert LEN glyphs from START at the nominal cursor position. */
23111
23112 void
23113 x_insert_glyphs (struct glyph *start, int len)
23114 {
23115 struct frame *f;
23116 struct window *w;
23117 int line_height, shift_by_width, shifted_region_width;
23118 struct glyph_row *row;
23119 struct glyph *glyph;
23120 int frame_x, frame_y;
23121 EMACS_INT hpos;
23122
23123 xassert (updated_window && updated_row);
23124 BLOCK_INPUT;
23125 w = updated_window;
23126 f = XFRAME (WINDOW_FRAME (w));
23127
23128 /* Get the height of the line we are in. */
23129 row = updated_row;
23130 line_height = row->height;
23131
23132 /* Get the width of the glyphs to insert. */
23133 shift_by_width = 0;
23134 for (glyph = start; glyph < start + len; ++glyph)
23135 shift_by_width += glyph->pixel_width;
23136
23137 /* Get the width of the region to shift right. */
23138 shifted_region_width = (window_box_width (w, updated_area)
23139 - output_cursor.x
23140 - shift_by_width);
23141
23142 /* Shift right. */
23143 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23144 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23145
23146 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23147 line_height, shift_by_width);
23148
23149 /* Write the glyphs. */
23150 hpos = start - row->glyphs[updated_area];
23151 draw_glyphs (w, output_cursor.x, row, updated_area,
23152 hpos, hpos + len,
23153 DRAW_NORMAL_TEXT, 0);
23154
23155 /* Advance the output cursor. */
23156 output_cursor.hpos += len;
23157 output_cursor.x += shift_by_width;
23158 UNBLOCK_INPUT;
23159 }
23160
23161
23162 /* EXPORT for RIF:
23163 Erase the current text line from the nominal cursor position
23164 (inclusive) to pixel column TO_X (exclusive). The idea is that
23165 everything from TO_X onward is already erased.
23166
23167 TO_X is a pixel position relative to updated_area of
23168 updated_window. TO_X == -1 means clear to the end of this area. */
23169
23170 void
23171 x_clear_end_of_line (int to_x)
23172 {
23173 struct frame *f;
23174 struct window *w = updated_window;
23175 int max_x, min_y, max_y;
23176 int from_x, from_y, to_y;
23177
23178 xassert (updated_window && updated_row);
23179 f = XFRAME (w->frame);
23180
23181 if (updated_row->full_width_p)
23182 max_x = WINDOW_TOTAL_WIDTH (w);
23183 else
23184 max_x = window_box_width (w, updated_area);
23185 max_y = window_text_bottom_y (w);
23186
23187 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23188 of window. For TO_X > 0, truncate to end of drawing area. */
23189 if (to_x == 0)
23190 return;
23191 else if (to_x < 0)
23192 to_x = max_x;
23193 else
23194 to_x = min (to_x, max_x);
23195
23196 to_y = min (max_y, output_cursor.y + updated_row->height);
23197
23198 /* Notice if the cursor will be cleared by this operation. */
23199 if (!updated_row->full_width_p)
23200 notice_overwritten_cursor (w, updated_area,
23201 output_cursor.x, -1,
23202 updated_row->y,
23203 MATRIX_ROW_BOTTOM_Y (updated_row));
23204
23205 from_x = output_cursor.x;
23206
23207 /* Translate to frame coordinates. */
23208 if (updated_row->full_width_p)
23209 {
23210 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23211 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23212 }
23213 else
23214 {
23215 int area_left = window_box_left (w, updated_area);
23216 from_x += area_left;
23217 to_x += area_left;
23218 }
23219
23220 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23221 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23222 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23223
23224 /* Prevent inadvertently clearing to end of the X window. */
23225 if (to_x > from_x && to_y > from_y)
23226 {
23227 BLOCK_INPUT;
23228 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23229 to_x - from_x, to_y - from_y);
23230 UNBLOCK_INPUT;
23231 }
23232 }
23233
23234 #endif /* HAVE_WINDOW_SYSTEM */
23235
23236
23237 \f
23238 /***********************************************************************
23239 Cursor types
23240 ***********************************************************************/
23241
23242 /* Value is the internal representation of the specified cursor type
23243 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23244 of the bar cursor. */
23245
23246 static enum text_cursor_kinds
23247 get_specified_cursor_type (Lisp_Object arg, int *width)
23248 {
23249 enum text_cursor_kinds type;
23250
23251 if (NILP (arg))
23252 return NO_CURSOR;
23253
23254 if (EQ (arg, Qbox))
23255 return FILLED_BOX_CURSOR;
23256
23257 if (EQ (arg, Qhollow))
23258 return HOLLOW_BOX_CURSOR;
23259
23260 if (EQ (arg, Qbar))
23261 {
23262 *width = 2;
23263 return BAR_CURSOR;
23264 }
23265
23266 if (CONSP (arg)
23267 && EQ (XCAR (arg), Qbar)
23268 && INTEGERP (XCDR (arg))
23269 && XINT (XCDR (arg)) >= 0)
23270 {
23271 *width = XINT (XCDR (arg));
23272 return BAR_CURSOR;
23273 }
23274
23275 if (EQ (arg, Qhbar))
23276 {
23277 *width = 2;
23278 return HBAR_CURSOR;
23279 }
23280
23281 if (CONSP (arg)
23282 && EQ (XCAR (arg), Qhbar)
23283 && INTEGERP (XCDR (arg))
23284 && XINT (XCDR (arg)) >= 0)
23285 {
23286 *width = XINT (XCDR (arg));
23287 return HBAR_CURSOR;
23288 }
23289
23290 /* Treat anything unknown as "hollow box cursor".
23291 It was bad to signal an error; people have trouble fixing
23292 .Xdefaults with Emacs, when it has something bad in it. */
23293 type = HOLLOW_BOX_CURSOR;
23294
23295 return type;
23296 }
23297
23298 /* Set the default cursor types for specified frame. */
23299 void
23300 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23301 {
23302 int width = 1;
23303 Lisp_Object tem;
23304
23305 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23306 FRAME_CURSOR_WIDTH (f) = width;
23307
23308 /* By default, set up the blink-off state depending on the on-state. */
23309
23310 tem = Fassoc (arg, Vblink_cursor_alist);
23311 if (!NILP (tem))
23312 {
23313 FRAME_BLINK_OFF_CURSOR (f)
23314 = get_specified_cursor_type (XCDR (tem), &width);
23315 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23316 }
23317 else
23318 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23319 }
23320
23321
23322 #ifdef HAVE_WINDOW_SYSTEM
23323
23324 /* Return the cursor we want to be displayed in window W. Return
23325 width of bar/hbar cursor through WIDTH arg. Return with
23326 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23327 (i.e. if the `system caret' should track this cursor).
23328
23329 In a mini-buffer window, we want the cursor only to appear if we
23330 are reading input from this window. For the selected window, we
23331 want the cursor type given by the frame parameter or buffer local
23332 setting of cursor-type. If explicitly marked off, draw no cursor.
23333 In all other cases, we want a hollow box cursor. */
23334
23335 static enum text_cursor_kinds
23336 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23337 int *active_cursor)
23338 {
23339 struct frame *f = XFRAME (w->frame);
23340 struct buffer *b = XBUFFER (w->buffer);
23341 int cursor_type = DEFAULT_CURSOR;
23342 Lisp_Object alt_cursor;
23343 int non_selected = 0;
23344
23345 *active_cursor = 1;
23346
23347 /* Echo area */
23348 if (cursor_in_echo_area
23349 && FRAME_HAS_MINIBUF_P (f)
23350 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23351 {
23352 if (w == XWINDOW (echo_area_window))
23353 {
23354 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
23355 {
23356 *width = FRAME_CURSOR_WIDTH (f);
23357 return FRAME_DESIRED_CURSOR (f);
23358 }
23359 else
23360 return get_specified_cursor_type (BVAR (b, cursor_type), width);
23361 }
23362
23363 *active_cursor = 0;
23364 non_selected = 1;
23365 }
23366
23367 /* Detect a nonselected window or nonselected frame. */
23368 else if (w != XWINDOW (f->selected_window)
23369 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23370 {
23371 *active_cursor = 0;
23372
23373 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23374 return NO_CURSOR;
23375
23376 non_selected = 1;
23377 }
23378
23379 /* Never display a cursor in a window in which cursor-type is nil. */
23380 if (NILP (BVAR (b, cursor_type)))
23381 return NO_CURSOR;
23382
23383 /* Get the normal cursor type for this window. */
23384 if (EQ (BVAR (b, cursor_type), Qt))
23385 {
23386 cursor_type = FRAME_DESIRED_CURSOR (f);
23387 *width = FRAME_CURSOR_WIDTH (f);
23388 }
23389 else
23390 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
23391
23392 /* Use cursor-in-non-selected-windows instead
23393 for non-selected window or frame. */
23394 if (non_selected)
23395 {
23396 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
23397 if (!EQ (Qt, alt_cursor))
23398 return get_specified_cursor_type (alt_cursor, width);
23399 /* t means modify the normal cursor type. */
23400 if (cursor_type == FILLED_BOX_CURSOR)
23401 cursor_type = HOLLOW_BOX_CURSOR;
23402 else if (cursor_type == BAR_CURSOR && *width > 1)
23403 --*width;
23404 return cursor_type;
23405 }
23406
23407 /* Use normal cursor if not blinked off. */
23408 if (!w->cursor_off_p)
23409 {
23410 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23411 {
23412 if (cursor_type == FILLED_BOX_CURSOR)
23413 {
23414 /* Using a block cursor on large images can be very annoying.
23415 So use a hollow cursor for "large" images.
23416 If image is not transparent (no mask), also use hollow cursor. */
23417 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23418 if (img != NULL && IMAGEP (img->spec))
23419 {
23420 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23421 where N = size of default frame font size.
23422 This should cover most of the "tiny" icons people may use. */
23423 if (!img->mask
23424 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23425 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23426 cursor_type = HOLLOW_BOX_CURSOR;
23427 }
23428 }
23429 else if (cursor_type != NO_CURSOR)
23430 {
23431 /* Display current only supports BOX and HOLLOW cursors for images.
23432 So for now, unconditionally use a HOLLOW cursor when cursor is
23433 not a solid box cursor. */
23434 cursor_type = HOLLOW_BOX_CURSOR;
23435 }
23436 }
23437 return cursor_type;
23438 }
23439
23440 /* Cursor is blinked off, so determine how to "toggle" it. */
23441
23442 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23443 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
23444 return get_specified_cursor_type (XCDR (alt_cursor), width);
23445
23446 /* Then see if frame has specified a specific blink off cursor type. */
23447 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23448 {
23449 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23450 return FRAME_BLINK_OFF_CURSOR (f);
23451 }
23452
23453 #if 0
23454 /* Some people liked having a permanently visible blinking cursor,
23455 while others had very strong opinions against it. So it was
23456 decided to remove it. KFS 2003-09-03 */
23457
23458 /* Finally perform built-in cursor blinking:
23459 filled box <-> hollow box
23460 wide [h]bar <-> narrow [h]bar
23461 narrow [h]bar <-> no cursor
23462 other type <-> no cursor */
23463
23464 if (cursor_type == FILLED_BOX_CURSOR)
23465 return HOLLOW_BOX_CURSOR;
23466
23467 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23468 {
23469 *width = 1;
23470 return cursor_type;
23471 }
23472 #endif
23473
23474 return NO_CURSOR;
23475 }
23476
23477
23478 /* Notice when the text cursor of window W has been completely
23479 overwritten by a drawing operation that outputs glyphs in AREA
23480 starting at X0 and ending at X1 in the line starting at Y0 and
23481 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23482 the rest of the line after X0 has been written. Y coordinates
23483 are window-relative. */
23484
23485 static void
23486 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23487 int x0, int x1, int y0, int y1)
23488 {
23489 int cx0, cx1, cy0, cy1;
23490 struct glyph_row *row;
23491
23492 if (!w->phys_cursor_on_p)
23493 return;
23494 if (area != TEXT_AREA)
23495 return;
23496
23497 if (w->phys_cursor.vpos < 0
23498 || w->phys_cursor.vpos >= w->current_matrix->nrows
23499 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23500 !(row->enabled_p && row->displays_text_p)))
23501 return;
23502
23503 if (row->cursor_in_fringe_p)
23504 {
23505 row->cursor_in_fringe_p = 0;
23506 draw_fringe_bitmap (w, row, row->reversed_p);
23507 w->phys_cursor_on_p = 0;
23508 return;
23509 }
23510
23511 cx0 = w->phys_cursor.x;
23512 cx1 = cx0 + w->phys_cursor_width;
23513 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23514 return;
23515
23516 /* The cursor image will be completely removed from the
23517 screen if the output area intersects the cursor area in
23518 y-direction. When we draw in [y0 y1[, and some part of
23519 the cursor is at y < y0, that part must have been drawn
23520 before. When scrolling, the cursor is erased before
23521 actually scrolling, so we don't come here. When not
23522 scrolling, the rows above the old cursor row must have
23523 changed, and in this case these rows must have written
23524 over the cursor image.
23525
23526 Likewise if part of the cursor is below y1, with the
23527 exception of the cursor being in the first blank row at
23528 the buffer and window end because update_text_area
23529 doesn't draw that row. (Except when it does, but
23530 that's handled in update_text_area.) */
23531
23532 cy0 = w->phys_cursor.y;
23533 cy1 = cy0 + w->phys_cursor_height;
23534 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23535 return;
23536
23537 w->phys_cursor_on_p = 0;
23538 }
23539
23540 #endif /* HAVE_WINDOW_SYSTEM */
23541
23542 \f
23543 /************************************************************************
23544 Mouse Face
23545 ************************************************************************/
23546
23547 #ifdef HAVE_WINDOW_SYSTEM
23548
23549 /* EXPORT for RIF:
23550 Fix the display of area AREA of overlapping row ROW in window W
23551 with respect to the overlapping part OVERLAPS. */
23552
23553 void
23554 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23555 enum glyph_row_area area, int overlaps)
23556 {
23557 int i, x;
23558
23559 BLOCK_INPUT;
23560
23561 x = 0;
23562 for (i = 0; i < row->used[area];)
23563 {
23564 if (row->glyphs[area][i].overlaps_vertically_p)
23565 {
23566 int start = i, start_x = x;
23567
23568 do
23569 {
23570 x += row->glyphs[area][i].pixel_width;
23571 ++i;
23572 }
23573 while (i < row->used[area]
23574 && row->glyphs[area][i].overlaps_vertically_p);
23575
23576 draw_glyphs (w, start_x, row, area,
23577 start, i,
23578 DRAW_NORMAL_TEXT, overlaps);
23579 }
23580 else
23581 {
23582 x += row->glyphs[area][i].pixel_width;
23583 ++i;
23584 }
23585 }
23586
23587 UNBLOCK_INPUT;
23588 }
23589
23590
23591 /* EXPORT:
23592 Draw the cursor glyph of window W in glyph row ROW. See the
23593 comment of draw_glyphs for the meaning of HL. */
23594
23595 void
23596 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23597 enum draw_glyphs_face hl)
23598 {
23599 /* If cursor hpos is out of bounds, don't draw garbage. This can
23600 happen in mini-buffer windows when switching between echo area
23601 glyphs and mini-buffer. */
23602 if ((row->reversed_p
23603 ? (w->phys_cursor.hpos >= 0)
23604 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23605 {
23606 int on_p = w->phys_cursor_on_p;
23607 int x1;
23608 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23609 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23610 hl, 0);
23611 w->phys_cursor_on_p = on_p;
23612
23613 if (hl == DRAW_CURSOR)
23614 w->phys_cursor_width = x1 - w->phys_cursor.x;
23615 /* When we erase the cursor, and ROW is overlapped by other
23616 rows, make sure that these overlapping parts of other rows
23617 are redrawn. */
23618 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23619 {
23620 w->phys_cursor_width = x1 - w->phys_cursor.x;
23621
23622 if (row > w->current_matrix->rows
23623 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23624 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23625 OVERLAPS_ERASED_CURSOR);
23626
23627 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23628 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23629 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23630 OVERLAPS_ERASED_CURSOR);
23631 }
23632 }
23633 }
23634
23635
23636 /* EXPORT:
23637 Erase the image of a cursor of window W from the screen. */
23638
23639 void
23640 erase_phys_cursor (struct window *w)
23641 {
23642 struct frame *f = XFRAME (w->frame);
23643 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23644 int hpos = w->phys_cursor.hpos;
23645 int vpos = w->phys_cursor.vpos;
23646 int mouse_face_here_p = 0;
23647 struct glyph_matrix *active_glyphs = w->current_matrix;
23648 struct glyph_row *cursor_row;
23649 struct glyph *cursor_glyph;
23650 enum draw_glyphs_face hl;
23651
23652 /* No cursor displayed or row invalidated => nothing to do on the
23653 screen. */
23654 if (w->phys_cursor_type == NO_CURSOR)
23655 goto mark_cursor_off;
23656
23657 /* VPOS >= active_glyphs->nrows means that window has been resized.
23658 Don't bother to erase the cursor. */
23659 if (vpos >= active_glyphs->nrows)
23660 goto mark_cursor_off;
23661
23662 /* If row containing cursor is marked invalid, there is nothing we
23663 can do. */
23664 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23665 if (!cursor_row->enabled_p)
23666 goto mark_cursor_off;
23667
23668 /* If line spacing is > 0, old cursor may only be partially visible in
23669 window after split-window. So adjust visible height. */
23670 cursor_row->visible_height = min (cursor_row->visible_height,
23671 window_text_bottom_y (w) - cursor_row->y);
23672
23673 /* If row is completely invisible, don't attempt to delete a cursor which
23674 isn't there. This can happen if cursor is at top of a window, and
23675 we switch to a buffer with a header line in that window. */
23676 if (cursor_row->visible_height <= 0)
23677 goto mark_cursor_off;
23678
23679 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23680 if (cursor_row->cursor_in_fringe_p)
23681 {
23682 cursor_row->cursor_in_fringe_p = 0;
23683 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23684 goto mark_cursor_off;
23685 }
23686
23687 /* This can happen when the new row is shorter than the old one.
23688 In this case, either draw_glyphs or clear_end_of_line
23689 should have cleared the cursor. Note that we wouldn't be
23690 able to erase the cursor in this case because we don't have a
23691 cursor glyph at hand. */
23692 if ((cursor_row->reversed_p
23693 ? (w->phys_cursor.hpos < 0)
23694 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23695 goto mark_cursor_off;
23696
23697 /* If the cursor is in the mouse face area, redisplay that when
23698 we clear the cursor. */
23699 if (! NILP (hlinfo->mouse_face_window)
23700 && coords_in_mouse_face_p (w, hpos, vpos)
23701 /* Don't redraw the cursor's spot in mouse face if it is at the
23702 end of a line (on a newline). The cursor appears there, but
23703 mouse highlighting does not. */
23704 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23705 mouse_face_here_p = 1;
23706
23707 /* Maybe clear the display under the cursor. */
23708 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23709 {
23710 int x, y, left_x;
23711 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23712 int width;
23713
23714 cursor_glyph = get_phys_cursor_glyph (w);
23715 if (cursor_glyph == NULL)
23716 goto mark_cursor_off;
23717
23718 width = cursor_glyph->pixel_width;
23719 left_x = window_box_left_offset (w, TEXT_AREA);
23720 x = w->phys_cursor.x;
23721 if (x < left_x)
23722 width -= left_x - x;
23723 width = min (width, window_box_width (w, TEXT_AREA) - x);
23724 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23725 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23726
23727 if (width > 0)
23728 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23729 }
23730
23731 /* Erase the cursor by redrawing the character underneath it. */
23732 if (mouse_face_here_p)
23733 hl = DRAW_MOUSE_FACE;
23734 else
23735 hl = DRAW_NORMAL_TEXT;
23736 draw_phys_cursor_glyph (w, cursor_row, hl);
23737
23738 mark_cursor_off:
23739 w->phys_cursor_on_p = 0;
23740 w->phys_cursor_type = NO_CURSOR;
23741 }
23742
23743
23744 /* EXPORT:
23745 Display or clear cursor of window W. If ON is zero, clear the
23746 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23747 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23748
23749 void
23750 display_and_set_cursor (struct window *w, int on,
23751 int hpos, int vpos, int x, int y)
23752 {
23753 struct frame *f = XFRAME (w->frame);
23754 int new_cursor_type;
23755 int new_cursor_width;
23756 int active_cursor;
23757 struct glyph_row *glyph_row;
23758 struct glyph *glyph;
23759
23760 /* This is pointless on invisible frames, and dangerous on garbaged
23761 windows and frames; in the latter case, the frame or window may
23762 be in the midst of changing its size, and x and y may be off the
23763 window. */
23764 if (! FRAME_VISIBLE_P (f)
23765 || FRAME_GARBAGED_P (f)
23766 || vpos >= w->current_matrix->nrows
23767 || hpos >= w->current_matrix->matrix_w)
23768 return;
23769
23770 /* If cursor is off and we want it off, return quickly. */
23771 if (!on && !w->phys_cursor_on_p)
23772 return;
23773
23774 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23775 /* If cursor row is not enabled, we don't really know where to
23776 display the cursor. */
23777 if (!glyph_row->enabled_p)
23778 {
23779 w->phys_cursor_on_p = 0;
23780 return;
23781 }
23782
23783 glyph = NULL;
23784 if (!glyph_row->exact_window_width_line_p
23785 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23786 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23787
23788 xassert (interrupt_input_blocked);
23789
23790 /* Set new_cursor_type to the cursor we want to be displayed. */
23791 new_cursor_type = get_window_cursor_type (w, glyph,
23792 &new_cursor_width, &active_cursor);
23793
23794 /* If cursor is currently being shown and we don't want it to be or
23795 it is in the wrong place, or the cursor type is not what we want,
23796 erase it. */
23797 if (w->phys_cursor_on_p
23798 && (!on
23799 || w->phys_cursor.x != x
23800 || w->phys_cursor.y != y
23801 || new_cursor_type != w->phys_cursor_type
23802 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23803 && new_cursor_width != w->phys_cursor_width)))
23804 erase_phys_cursor (w);
23805
23806 /* Don't check phys_cursor_on_p here because that flag is only set
23807 to zero in some cases where we know that the cursor has been
23808 completely erased, to avoid the extra work of erasing the cursor
23809 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23810 still not be visible, or it has only been partly erased. */
23811 if (on)
23812 {
23813 w->phys_cursor_ascent = glyph_row->ascent;
23814 w->phys_cursor_height = glyph_row->height;
23815
23816 /* Set phys_cursor_.* before x_draw_.* is called because some
23817 of them may need the information. */
23818 w->phys_cursor.x = x;
23819 w->phys_cursor.y = glyph_row->y;
23820 w->phys_cursor.hpos = hpos;
23821 w->phys_cursor.vpos = vpos;
23822 }
23823
23824 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23825 new_cursor_type, new_cursor_width,
23826 on, active_cursor);
23827 }
23828
23829
23830 /* Switch the display of W's cursor on or off, according to the value
23831 of ON. */
23832
23833 static void
23834 update_window_cursor (struct window *w, int on)
23835 {
23836 /* Don't update cursor in windows whose frame is in the process
23837 of being deleted. */
23838 if (w->current_matrix)
23839 {
23840 BLOCK_INPUT;
23841 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23842 w->phys_cursor.x, w->phys_cursor.y);
23843 UNBLOCK_INPUT;
23844 }
23845 }
23846
23847
23848 /* Call update_window_cursor with parameter ON_P on all leaf windows
23849 in the window tree rooted at W. */
23850
23851 static void
23852 update_cursor_in_window_tree (struct window *w, int on_p)
23853 {
23854 while (w)
23855 {
23856 if (!NILP (w->hchild))
23857 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23858 else if (!NILP (w->vchild))
23859 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23860 else
23861 update_window_cursor (w, on_p);
23862
23863 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23864 }
23865 }
23866
23867
23868 /* EXPORT:
23869 Display the cursor on window W, or clear it, according to ON_P.
23870 Don't change the cursor's position. */
23871
23872 void
23873 x_update_cursor (struct frame *f, int on_p)
23874 {
23875 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23876 }
23877
23878
23879 /* EXPORT:
23880 Clear the cursor of window W to background color, and mark the
23881 cursor as not shown. This is used when the text where the cursor
23882 is about to be rewritten. */
23883
23884 void
23885 x_clear_cursor (struct window *w)
23886 {
23887 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23888 update_window_cursor (w, 0);
23889 }
23890
23891 #endif /* HAVE_WINDOW_SYSTEM */
23892
23893 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
23894 and MSDOS. */
23895 void
23896 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
23897 int start_hpos, int end_hpos,
23898 enum draw_glyphs_face draw)
23899 {
23900 #ifdef HAVE_WINDOW_SYSTEM
23901 if (FRAME_WINDOW_P (XFRAME (w->frame)))
23902 {
23903 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
23904 return;
23905 }
23906 #endif
23907 #if defined (HAVE_GPM) || defined (MSDOS)
23908 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
23909 #endif
23910 }
23911
23912 /* EXPORT:
23913 Display the active region described by mouse_face_* according to DRAW. */
23914
23915 void
23916 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
23917 {
23918 struct window *w = XWINDOW (hlinfo->mouse_face_window);
23919 struct frame *f = XFRAME (WINDOW_FRAME (w));
23920
23921 if (/* If window is in the process of being destroyed, don't bother
23922 to do anything. */
23923 w->current_matrix != NULL
23924 /* Don't update mouse highlight if hidden */
23925 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
23926 /* Recognize when we are called to operate on rows that don't exist
23927 anymore. This can happen when a window is split. */
23928 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
23929 {
23930 int phys_cursor_on_p = w->phys_cursor_on_p;
23931 struct glyph_row *row, *first, *last;
23932
23933 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23934 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23935
23936 for (row = first; row <= last && row->enabled_p; ++row)
23937 {
23938 int start_hpos, end_hpos, start_x;
23939
23940 /* For all but the first row, the highlight starts at column 0. */
23941 if (row == first)
23942 {
23943 /* R2L rows have BEG and END in reversed order, but the
23944 screen drawing geometry is always left to right. So
23945 we need to mirror the beginning and end of the
23946 highlighted area in R2L rows. */
23947 if (!row->reversed_p)
23948 {
23949 start_hpos = hlinfo->mouse_face_beg_col;
23950 start_x = hlinfo->mouse_face_beg_x;
23951 }
23952 else if (row == last)
23953 {
23954 start_hpos = hlinfo->mouse_face_end_col;
23955 start_x = hlinfo->mouse_face_end_x;
23956 }
23957 else
23958 {
23959 start_hpos = 0;
23960 start_x = 0;
23961 }
23962 }
23963 else if (row->reversed_p && row == last)
23964 {
23965 start_hpos = hlinfo->mouse_face_end_col;
23966 start_x = hlinfo->mouse_face_end_x;
23967 }
23968 else
23969 {
23970 start_hpos = 0;
23971 start_x = 0;
23972 }
23973
23974 if (row == last)
23975 {
23976 if (!row->reversed_p)
23977 end_hpos = hlinfo->mouse_face_end_col;
23978 else if (row == first)
23979 end_hpos = hlinfo->mouse_face_beg_col;
23980 else
23981 {
23982 end_hpos = row->used[TEXT_AREA];
23983 if (draw == DRAW_NORMAL_TEXT)
23984 row->fill_line_p = 1; /* Clear to end of line */
23985 }
23986 }
23987 else if (row->reversed_p && row == first)
23988 end_hpos = hlinfo->mouse_face_beg_col;
23989 else
23990 {
23991 end_hpos = row->used[TEXT_AREA];
23992 if (draw == DRAW_NORMAL_TEXT)
23993 row->fill_line_p = 1; /* Clear to end of line */
23994 }
23995
23996 if (end_hpos > start_hpos)
23997 {
23998 draw_row_with_mouse_face (w, start_x, row,
23999 start_hpos, end_hpos, draw);
24000
24001 row->mouse_face_p
24002 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24003 }
24004 }
24005
24006 #ifdef HAVE_WINDOW_SYSTEM
24007 /* When we've written over the cursor, arrange for it to
24008 be displayed again. */
24009 if (FRAME_WINDOW_P (f)
24010 && phys_cursor_on_p && !w->phys_cursor_on_p)
24011 {
24012 BLOCK_INPUT;
24013 display_and_set_cursor (w, 1,
24014 w->phys_cursor.hpos, w->phys_cursor.vpos,
24015 w->phys_cursor.x, w->phys_cursor.y);
24016 UNBLOCK_INPUT;
24017 }
24018 #endif /* HAVE_WINDOW_SYSTEM */
24019 }
24020
24021 #ifdef HAVE_WINDOW_SYSTEM
24022 /* Change the mouse cursor. */
24023 if (FRAME_WINDOW_P (f))
24024 {
24025 if (draw == DRAW_NORMAL_TEXT
24026 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24027 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24028 else if (draw == DRAW_MOUSE_FACE)
24029 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24030 else
24031 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24032 }
24033 #endif /* HAVE_WINDOW_SYSTEM */
24034 }
24035
24036 /* EXPORT:
24037 Clear out the mouse-highlighted active region.
24038 Redraw it un-highlighted first. Value is non-zero if mouse
24039 face was actually drawn unhighlighted. */
24040
24041 int
24042 clear_mouse_face (Mouse_HLInfo *hlinfo)
24043 {
24044 int cleared = 0;
24045
24046 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24047 {
24048 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24049 cleared = 1;
24050 }
24051
24052 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24053 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24054 hlinfo->mouse_face_window = Qnil;
24055 hlinfo->mouse_face_overlay = Qnil;
24056 return cleared;
24057 }
24058
24059 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24060 within the mouse face on that window. */
24061 static int
24062 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24063 {
24064 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24065
24066 /* Quickly resolve the easy cases. */
24067 if (!(WINDOWP (hlinfo->mouse_face_window)
24068 && XWINDOW (hlinfo->mouse_face_window) == w))
24069 return 0;
24070 if (vpos < hlinfo->mouse_face_beg_row
24071 || vpos > hlinfo->mouse_face_end_row)
24072 return 0;
24073 if (vpos > hlinfo->mouse_face_beg_row
24074 && vpos < hlinfo->mouse_face_end_row)
24075 return 1;
24076
24077 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24078 {
24079 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24080 {
24081 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24082 return 1;
24083 }
24084 else if ((vpos == hlinfo->mouse_face_beg_row
24085 && hpos >= hlinfo->mouse_face_beg_col)
24086 || (vpos == hlinfo->mouse_face_end_row
24087 && hpos < hlinfo->mouse_face_end_col))
24088 return 1;
24089 }
24090 else
24091 {
24092 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24093 {
24094 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24095 return 1;
24096 }
24097 else if ((vpos == hlinfo->mouse_face_beg_row
24098 && hpos <= hlinfo->mouse_face_beg_col)
24099 || (vpos == hlinfo->mouse_face_end_row
24100 && hpos > hlinfo->mouse_face_end_col))
24101 return 1;
24102 }
24103 return 0;
24104 }
24105
24106
24107 /* EXPORT:
24108 Non-zero if physical cursor of window W is within mouse face. */
24109
24110 int
24111 cursor_in_mouse_face_p (struct window *w)
24112 {
24113 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24114 }
24115
24116
24117 \f
24118 /* Find the glyph rows START_ROW and END_ROW of window W that display
24119 characters between buffer positions START_CHARPOS and END_CHARPOS
24120 (excluding END_CHARPOS). This is similar to row_containing_pos,
24121 but is more accurate when bidi reordering makes buffer positions
24122 change non-linearly with glyph rows. */
24123 static void
24124 rows_from_pos_range (struct window *w,
24125 EMACS_INT start_charpos, EMACS_INT end_charpos,
24126 struct glyph_row **start, struct glyph_row **end)
24127 {
24128 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24129 int last_y = window_text_bottom_y (w);
24130 struct glyph_row *row;
24131
24132 *start = NULL;
24133 *end = NULL;
24134
24135 while (!first->enabled_p
24136 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24137 first++;
24138
24139 /* Find the START row. */
24140 for (row = first;
24141 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24142 row++)
24143 {
24144 /* A row can potentially be the START row if the range of the
24145 characters it displays intersects the range
24146 [START_CHARPOS..END_CHARPOS). */
24147 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24148 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24149 /* See the commentary in row_containing_pos, for the
24150 explanation of the complicated way to check whether
24151 some position is beyond the end of the characters
24152 displayed by a row. */
24153 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24154 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24155 && !row->ends_at_zv_p
24156 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24157 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24158 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24159 && !row->ends_at_zv_p
24160 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24161 {
24162 /* Found a candidate row. Now make sure at least one of the
24163 glyphs it displays has a charpos from the range
24164 [START_CHARPOS..END_CHARPOS).
24165
24166 This is not obvious because bidi reordering could make
24167 buffer positions of a row be 1,2,3,102,101,100, and if we
24168 want to highlight characters in [50..60), we don't want
24169 this row, even though [50..60) does intersect [1..103),
24170 the range of character positions given by the row's start
24171 and end positions. */
24172 struct glyph *g = row->glyphs[TEXT_AREA];
24173 struct glyph *e = g + row->used[TEXT_AREA];
24174
24175 while (g < e)
24176 {
24177 if (BUFFERP (g->object)
24178 && start_charpos <= g->charpos && g->charpos < end_charpos)
24179 *start = row;
24180 g++;
24181 }
24182 if (*start)
24183 break;
24184 }
24185 }
24186
24187 /* Find the END row. */
24188 if (!*start
24189 /* If the last row is partially visible, start looking for END
24190 from that row, instead of starting from FIRST. */
24191 && !(row->enabled_p
24192 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24193 row = first;
24194 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24195 {
24196 struct glyph_row *next = row + 1;
24197
24198 if (!next->enabled_p
24199 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24200 /* The first row >= START whose range of displayed characters
24201 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24202 is the row END + 1. */
24203 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24204 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24205 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24206 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24207 && !next->ends_at_zv_p
24208 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24209 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24210 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24211 && !next->ends_at_zv_p
24212 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24213 {
24214 *end = row;
24215 break;
24216 }
24217 else
24218 {
24219 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24220 but none of the characters it displays are in the range, it is
24221 also END + 1. */
24222 struct glyph *g = next->glyphs[TEXT_AREA];
24223 struct glyph *e = g + next->used[TEXT_AREA];
24224
24225 while (g < e)
24226 {
24227 if (BUFFERP (g->object)
24228 && start_charpos <= g->charpos && g->charpos < end_charpos)
24229 break;
24230 g++;
24231 }
24232 if (g == e)
24233 {
24234 *end = row;
24235 break;
24236 }
24237 }
24238 }
24239 }
24240
24241 /* This function sets the mouse_face_* elements of HLINFO, assuming
24242 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24243 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24244 for the overlay or run of text properties specifying the mouse
24245 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24246 before-string and after-string that must also be highlighted.
24247 COVER_STRING, if non-nil, is a display string that may cover some
24248 or all of the highlighted text. */
24249
24250 static void
24251 mouse_face_from_buffer_pos (Lisp_Object window,
24252 Mouse_HLInfo *hlinfo,
24253 EMACS_INT mouse_charpos,
24254 EMACS_INT start_charpos,
24255 EMACS_INT end_charpos,
24256 Lisp_Object before_string,
24257 Lisp_Object after_string,
24258 Lisp_Object cover_string)
24259 {
24260 struct window *w = XWINDOW (window);
24261 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24262 struct glyph_row *r1, *r2;
24263 struct glyph *glyph, *end;
24264 EMACS_INT ignore, pos;
24265 int x;
24266
24267 xassert (NILP (cover_string) || STRINGP (cover_string));
24268 xassert (NILP (before_string) || STRINGP (before_string));
24269 xassert (NILP (after_string) || STRINGP (after_string));
24270
24271 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24272 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24273 if (r1 == NULL)
24274 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24275 /* If the before-string or display-string contains newlines,
24276 rows_from_pos_range skips to its last row. Move back. */
24277 if (!NILP (before_string) || !NILP (cover_string))
24278 {
24279 struct glyph_row *prev;
24280 while ((prev = r1 - 1, prev >= first)
24281 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24282 && prev->used[TEXT_AREA] > 0)
24283 {
24284 struct glyph *beg = prev->glyphs[TEXT_AREA];
24285 glyph = beg + prev->used[TEXT_AREA];
24286 while (--glyph >= beg && INTEGERP (glyph->object));
24287 if (glyph < beg
24288 || !(EQ (glyph->object, before_string)
24289 || EQ (glyph->object, cover_string)))
24290 break;
24291 r1 = prev;
24292 }
24293 }
24294 if (r2 == NULL)
24295 {
24296 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24297 hlinfo->mouse_face_past_end = 1;
24298 }
24299 else if (!NILP (after_string))
24300 {
24301 /* If the after-string has newlines, advance to its last row. */
24302 struct glyph_row *next;
24303 struct glyph_row *last
24304 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24305
24306 for (next = r2 + 1;
24307 next <= last
24308 && next->used[TEXT_AREA] > 0
24309 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24310 ++next)
24311 r2 = next;
24312 }
24313 /* The rest of the display engine assumes that mouse_face_beg_row is
24314 either above below mouse_face_end_row or identical to it. But
24315 with bidi-reordered continued lines, the row for START_CHARPOS
24316 could be below the row for END_CHARPOS. If so, swap the rows and
24317 store them in correct order. */
24318 if (r1->y > r2->y)
24319 {
24320 struct glyph_row *tem = r2;
24321
24322 r2 = r1;
24323 r1 = tem;
24324 }
24325
24326 hlinfo->mouse_face_beg_y = r1->y;
24327 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24328 hlinfo->mouse_face_end_y = r2->y;
24329 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24330
24331 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24332 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
24333 could be anywhere in the row and in any order. The strategy
24334 below is to find the leftmost and the rightmost glyph that
24335 belongs to either of these 3 strings, or whose position is
24336 between START_CHARPOS and END_CHARPOS, and highlight all the
24337 glyphs between those two. This may cover more than just the text
24338 between START_CHARPOS and END_CHARPOS if the range of characters
24339 strides the bidi level boundary, e.g. if the beginning is in R2L
24340 text while the end is in L2R text or vice versa. */
24341 if (!r1->reversed_p)
24342 {
24343 /* This row is in a left to right paragraph. Scan it left to
24344 right. */
24345 glyph = r1->glyphs[TEXT_AREA];
24346 end = glyph + r1->used[TEXT_AREA];
24347 x = r1->x;
24348
24349 /* Skip truncation glyphs at the start of the glyph row. */
24350 if (r1->displays_text_p)
24351 for (; glyph < end
24352 && INTEGERP (glyph->object)
24353 && glyph->charpos < 0;
24354 ++glyph)
24355 x += glyph->pixel_width;
24356
24357 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24358 or COVER_STRING, and the first glyph from buffer whose
24359 position is between START_CHARPOS and END_CHARPOS. */
24360 for (; glyph < end
24361 && !INTEGERP (glyph->object)
24362 && !EQ (glyph->object, cover_string)
24363 && !(BUFFERP (glyph->object)
24364 && (glyph->charpos >= start_charpos
24365 && glyph->charpos < end_charpos));
24366 ++glyph)
24367 {
24368 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24369 are present at buffer positions between START_CHARPOS and
24370 END_CHARPOS, or if they come from an overlay. */
24371 if (EQ (glyph->object, before_string))
24372 {
24373 pos = string_buffer_position (before_string,
24374 start_charpos);
24375 /* If pos == 0, it means before_string came from an
24376 overlay, not from a buffer position. */
24377 if (!pos || (pos >= start_charpos && pos < end_charpos))
24378 break;
24379 }
24380 else if (EQ (glyph->object, after_string))
24381 {
24382 pos = string_buffer_position (after_string, end_charpos);
24383 if (!pos || (pos >= start_charpos && pos < end_charpos))
24384 break;
24385 }
24386 x += glyph->pixel_width;
24387 }
24388 hlinfo->mouse_face_beg_x = x;
24389 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24390 }
24391 else
24392 {
24393 /* This row is in a right to left paragraph. Scan it right to
24394 left. */
24395 struct glyph *g;
24396
24397 end = r1->glyphs[TEXT_AREA] - 1;
24398 glyph = end + r1->used[TEXT_AREA];
24399
24400 /* Skip truncation glyphs at the start of the glyph row. */
24401 if (r1->displays_text_p)
24402 for (; glyph > end
24403 && INTEGERP (glyph->object)
24404 && glyph->charpos < 0;
24405 --glyph)
24406 ;
24407
24408 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24409 or COVER_STRING, and the first glyph from buffer whose
24410 position is between START_CHARPOS and END_CHARPOS. */
24411 for (; glyph > end
24412 && !INTEGERP (glyph->object)
24413 && !EQ (glyph->object, cover_string)
24414 && !(BUFFERP (glyph->object)
24415 && (glyph->charpos >= start_charpos
24416 && glyph->charpos < end_charpos));
24417 --glyph)
24418 {
24419 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24420 are present at buffer positions between START_CHARPOS and
24421 END_CHARPOS, or if they come from an overlay. */
24422 if (EQ (glyph->object, before_string))
24423 {
24424 pos = string_buffer_position (before_string, start_charpos);
24425 /* If pos == 0, it means before_string came from an
24426 overlay, not from a buffer position. */
24427 if (!pos || (pos >= start_charpos && pos < end_charpos))
24428 break;
24429 }
24430 else if (EQ (glyph->object, after_string))
24431 {
24432 pos = string_buffer_position (after_string, end_charpos);
24433 if (!pos || (pos >= start_charpos && pos < end_charpos))
24434 break;
24435 }
24436 }
24437
24438 glyph++; /* first glyph to the right of the highlighted area */
24439 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24440 x += g->pixel_width;
24441 hlinfo->mouse_face_beg_x = x;
24442 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24443 }
24444
24445 /* If the highlight ends in a different row, compute GLYPH and END
24446 for the end row. Otherwise, reuse the values computed above for
24447 the row where the highlight begins. */
24448 if (r2 != r1)
24449 {
24450 if (!r2->reversed_p)
24451 {
24452 glyph = r2->glyphs[TEXT_AREA];
24453 end = glyph + r2->used[TEXT_AREA];
24454 x = r2->x;
24455 }
24456 else
24457 {
24458 end = r2->glyphs[TEXT_AREA] - 1;
24459 glyph = end + r2->used[TEXT_AREA];
24460 }
24461 }
24462
24463 if (!r2->reversed_p)
24464 {
24465 /* Skip truncation and continuation glyphs near the end of the
24466 row, and also blanks and stretch glyphs inserted by
24467 extend_face_to_end_of_line. */
24468 while (end > glyph
24469 && INTEGERP ((end - 1)->object)
24470 && (end - 1)->charpos <= 0)
24471 --end;
24472 /* Scan the rest of the glyph row from the end, looking for the
24473 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24474 COVER_STRING, or whose position is between START_CHARPOS
24475 and END_CHARPOS */
24476 for (--end;
24477 end > glyph
24478 && !INTEGERP (end->object)
24479 && !EQ (end->object, cover_string)
24480 && !(BUFFERP (end->object)
24481 && (end->charpos >= start_charpos
24482 && end->charpos < end_charpos));
24483 --end)
24484 {
24485 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24486 are present at buffer positions between START_CHARPOS and
24487 END_CHARPOS, or if they come from an overlay. */
24488 if (EQ (end->object, before_string))
24489 {
24490 pos = string_buffer_position (before_string, start_charpos);
24491 if (!pos || (pos >= start_charpos && pos < end_charpos))
24492 break;
24493 }
24494 else if (EQ (end->object, after_string))
24495 {
24496 pos = string_buffer_position (after_string, end_charpos);
24497 if (!pos || (pos >= start_charpos && pos < end_charpos))
24498 break;
24499 }
24500 }
24501 /* Find the X coordinate of the last glyph to be highlighted. */
24502 for (; glyph <= end; ++glyph)
24503 x += glyph->pixel_width;
24504
24505 hlinfo->mouse_face_end_x = x;
24506 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24507 }
24508 else
24509 {
24510 /* Skip truncation and continuation glyphs near the end of the
24511 row, and also blanks and stretch glyphs inserted by
24512 extend_face_to_end_of_line. */
24513 x = r2->x;
24514 end++;
24515 while (end < glyph
24516 && INTEGERP (end->object)
24517 && end->charpos <= 0)
24518 {
24519 x += end->pixel_width;
24520 ++end;
24521 }
24522 /* Scan the rest of the glyph row from the end, looking for the
24523 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24524 COVER_STRING, or whose position is between START_CHARPOS
24525 and END_CHARPOS */
24526 for ( ;
24527 end < glyph
24528 && !INTEGERP (end->object)
24529 && !EQ (end->object, cover_string)
24530 && !(BUFFERP (end->object)
24531 && (end->charpos >= start_charpos
24532 && end->charpos < end_charpos));
24533 ++end)
24534 {
24535 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24536 are present at buffer positions between START_CHARPOS and
24537 END_CHARPOS, or if they come from an overlay. */
24538 if (EQ (end->object, before_string))
24539 {
24540 pos = string_buffer_position (before_string, start_charpos);
24541 if (!pos || (pos >= start_charpos && pos < end_charpos))
24542 break;
24543 }
24544 else if (EQ (end->object, after_string))
24545 {
24546 pos = string_buffer_position (after_string, end_charpos);
24547 if (!pos || (pos >= start_charpos && pos < end_charpos))
24548 break;
24549 }
24550 x += end->pixel_width;
24551 }
24552 hlinfo->mouse_face_end_x = x;
24553 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24554 }
24555
24556 hlinfo->mouse_face_window = window;
24557 hlinfo->mouse_face_face_id
24558 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24559 mouse_charpos + 1,
24560 !hlinfo->mouse_face_hidden, -1);
24561 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24562 }
24563
24564 /* The following function is not used anymore (replaced with
24565 mouse_face_from_string_pos), but I leave it here for the time
24566 being, in case someone would. */
24567
24568 #if 0 /* not used */
24569
24570 /* Find the position of the glyph for position POS in OBJECT in
24571 window W's current matrix, and return in *X, *Y the pixel
24572 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24573
24574 RIGHT_P non-zero means return the position of the right edge of the
24575 glyph, RIGHT_P zero means return the left edge position.
24576
24577 If no glyph for POS exists in the matrix, return the position of
24578 the glyph with the next smaller position that is in the matrix, if
24579 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24580 exists in the matrix, return the position of the glyph with the
24581 next larger position in OBJECT.
24582
24583 Value is non-zero if a glyph was found. */
24584
24585 static int
24586 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24587 int *hpos, int *vpos, int *x, int *y, int right_p)
24588 {
24589 int yb = window_text_bottom_y (w);
24590 struct glyph_row *r;
24591 struct glyph *best_glyph = NULL;
24592 struct glyph_row *best_row = NULL;
24593 int best_x = 0;
24594
24595 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24596 r->enabled_p && r->y < yb;
24597 ++r)
24598 {
24599 struct glyph *g = r->glyphs[TEXT_AREA];
24600 struct glyph *e = g + r->used[TEXT_AREA];
24601 int gx;
24602
24603 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24604 if (EQ (g->object, object))
24605 {
24606 if (g->charpos == pos)
24607 {
24608 best_glyph = g;
24609 best_x = gx;
24610 best_row = r;
24611 goto found;
24612 }
24613 else if (best_glyph == NULL
24614 || ((eabs (g->charpos - pos)
24615 < eabs (best_glyph->charpos - pos))
24616 && (right_p
24617 ? g->charpos < pos
24618 : g->charpos > pos)))
24619 {
24620 best_glyph = g;
24621 best_x = gx;
24622 best_row = r;
24623 }
24624 }
24625 }
24626
24627 found:
24628
24629 if (best_glyph)
24630 {
24631 *x = best_x;
24632 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24633
24634 if (right_p)
24635 {
24636 *x += best_glyph->pixel_width;
24637 ++*hpos;
24638 }
24639
24640 *y = best_row->y;
24641 *vpos = best_row - w->current_matrix->rows;
24642 }
24643
24644 return best_glyph != NULL;
24645 }
24646 #endif /* not used */
24647
24648 /* Find the positions of the first and the last glyphs in window W's
24649 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24650 (assumed to be a string), and return in HLINFO's mouse_face_*
24651 members the pixel and column/row coordinates of those glyphs. */
24652
24653 static void
24654 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24655 Lisp_Object object,
24656 EMACS_INT startpos, EMACS_INT endpos)
24657 {
24658 int yb = window_text_bottom_y (w);
24659 struct glyph_row *r;
24660 struct glyph *g, *e;
24661 int gx;
24662 int found = 0;
24663
24664 /* Find the glyph row with at least one position in the range
24665 [STARTPOS..ENDPOS], and the first glyph in that row whose
24666 position belongs to that range. */
24667 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24668 r->enabled_p && r->y < yb;
24669 ++r)
24670 {
24671 if (!r->reversed_p)
24672 {
24673 g = r->glyphs[TEXT_AREA];
24674 e = g + r->used[TEXT_AREA];
24675 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24676 if (EQ (g->object, object)
24677 && startpos <= g->charpos && g->charpos <= endpos)
24678 {
24679 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24680 hlinfo->mouse_face_beg_y = r->y;
24681 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24682 hlinfo->mouse_face_beg_x = gx;
24683 found = 1;
24684 break;
24685 }
24686 }
24687 else
24688 {
24689 struct glyph *g1;
24690
24691 e = r->glyphs[TEXT_AREA];
24692 g = e + r->used[TEXT_AREA];
24693 for ( ; g > e; --g)
24694 if (EQ ((g-1)->object, object)
24695 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24696 {
24697 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24698 hlinfo->mouse_face_beg_y = r->y;
24699 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24700 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24701 gx += g1->pixel_width;
24702 hlinfo->mouse_face_beg_x = gx;
24703 found = 1;
24704 break;
24705 }
24706 }
24707 if (found)
24708 break;
24709 }
24710
24711 if (!found)
24712 return;
24713
24714 /* Starting with the next row, look for the first row which does NOT
24715 include any glyphs whose positions are in the range. */
24716 for (++r; r->enabled_p && r->y < yb; ++r)
24717 {
24718 g = r->glyphs[TEXT_AREA];
24719 e = g + r->used[TEXT_AREA];
24720 found = 0;
24721 for ( ; g < e; ++g)
24722 if (EQ (g->object, object)
24723 && startpos <= g->charpos && g->charpos <= endpos)
24724 {
24725 found = 1;
24726 break;
24727 }
24728 if (!found)
24729 break;
24730 }
24731
24732 /* The highlighted region ends on the previous row. */
24733 r--;
24734
24735 /* Set the end row and its vertical pixel coordinate. */
24736 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24737 hlinfo->mouse_face_end_y = r->y;
24738
24739 /* Compute and set the end column and the end column's horizontal
24740 pixel coordinate. */
24741 if (!r->reversed_p)
24742 {
24743 g = r->glyphs[TEXT_AREA];
24744 e = g + r->used[TEXT_AREA];
24745 for ( ; e > g; --e)
24746 if (EQ ((e-1)->object, object)
24747 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24748 break;
24749 hlinfo->mouse_face_end_col = e - g;
24750
24751 for (gx = r->x; g < e; ++g)
24752 gx += g->pixel_width;
24753 hlinfo->mouse_face_end_x = gx;
24754 }
24755 else
24756 {
24757 e = r->glyphs[TEXT_AREA];
24758 g = e + r->used[TEXT_AREA];
24759 for (gx = r->x ; e < g; ++e)
24760 {
24761 if (EQ (e->object, object)
24762 && startpos <= e->charpos && e->charpos <= endpos)
24763 break;
24764 gx += e->pixel_width;
24765 }
24766 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24767 hlinfo->mouse_face_end_x = gx;
24768 }
24769 }
24770
24771 #ifdef HAVE_WINDOW_SYSTEM
24772
24773 /* See if position X, Y is within a hot-spot of an image. */
24774
24775 static int
24776 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24777 {
24778 if (!CONSP (hot_spot))
24779 return 0;
24780
24781 if (EQ (XCAR (hot_spot), Qrect))
24782 {
24783 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24784 Lisp_Object rect = XCDR (hot_spot);
24785 Lisp_Object tem;
24786 if (!CONSP (rect))
24787 return 0;
24788 if (!CONSP (XCAR (rect)))
24789 return 0;
24790 if (!CONSP (XCDR (rect)))
24791 return 0;
24792 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24793 return 0;
24794 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24795 return 0;
24796 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24797 return 0;
24798 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24799 return 0;
24800 return 1;
24801 }
24802 else if (EQ (XCAR (hot_spot), Qcircle))
24803 {
24804 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24805 Lisp_Object circ = XCDR (hot_spot);
24806 Lisp_Object lr, lx0, ly0;
24807 if (CONSP (circ)
24808 && CONSP (XCAR (circ))
24809 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24810 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24811 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24812 {
24813 double r = XFLOATINT (lr);
24814 double dx = XINT (lx0) - x;
24815 double dy = XINT (ly0) - y;
24816 return (dx * dx + dy * dy <= r * r);
24817 }
24818 }
24819 else if (EQ (XCAR (hot_spot), Qpoly))
24820 {
24821 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24822 if (VECTORP (XCDR (hot_spot)))
24823 {
24824 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24825 Lisp_Object *poly = v->contents;
24826 int n = v->size;
24827 int i;
24828 int inside = 0;
24829 Lisp_Object lx, ly;
24830 int x0, y0;
24831
24832 /* Need an even number of coordinates, and at least 3 edges. */
24833 if (n < 6 || n & 1)
24834 return 0;
24835
24836 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24837 If count is odd, we are inside polygon. Pixels on edges
24838 may or may not be included depending on actual geometry of the
24839 polygon. */
24840 if ((lx = poly[n-2], !INTEGERP (lx))
24841 || (ly = poly[n-1], !INTEGERP (lx)))
24842 return 0;
24843 x0 = XINT (lx), y0 = XINT (ly);
24844 for (i = 0; i < n; i += 2)
24845 {
24846 int x1 = x0, y1 = y0;
24847 if ((lx = poly[i], !INTEGERP (lx))
24848 || (ly = poly[i+1], !INTEGERP (ly)))
24849 return 0;
24850 x0 = XINT (lx), y0 = XINT (ly);
24851
24852 /* Does this segment cross the X line? */
24853 if (x0 >= x)
24854 {
24855 if (x1 >= x)
24856 continue;
24857 }
24858 else if (x1 < x)
24859 continue;
24860 if (y > y0 && y > y1)
24861 continue;
24862 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24863 inside = !inside;
24864 }
24865 return inside;
24866 }
24867 }
24868 return 0;
24869 }
24870
24871 Lisp_Object
24872 find_hot_spot (Lisp_Object map, int x, int y)
24873 {
24874 while (CONSP (map))
24875 {
24876 if (CONSP (XCAR (map))
24877 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24878 return XCAR (map);
24879 map = XCDR (map);
24880 }
24881
24882 return Qnil;
24883 }
24884
24885 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24886 3, 3, 0,
24887 doc: /* Lookup in image map MAP coordinates X and Y.
24888 An image map is an alist where each element has the format (AREA ID PLIST).
24889 An AREA is specified as either a rectangle, a circle, or a polygon:
24890 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24891 pixel coordinates of the upper left and bottom right corners.
24892 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24893 and the radius of the circle; r may be a float or integer.
24894 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24895 vector describes one corner in the polygon.
24896 Returns the alist element for the first matching AREA in MAP. */)
24897 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
24898 {
24899 if (NILP (map))
24900 return Qnil;
24901
24902 CHECK_NUMBER (x);
24903 CHECK_NUMBER (y);
24904
24905 return find_hot_spot (map, XINT (x), XINT (y));
24906 }
24907
24908
24909 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24910 static void
24911 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
24912 {
24913 /* Do not change cursor shape while dragging mouse. */
24914 if (!NILP (do_mouse_tracking))
24915 return;
24916
24917 if (!NILP (pointer))
24918 {
24919 if (EQ (pointer, Qarrow))
24920 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24921 else if (EQ (pointer, Qhand))
24922 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24923 else if (EQ (pointer, Qtext))
24924 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24925 else if (EQ (pointer, intern ("hdrag")))
24926 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24927 #ifdef HAVE_X_WINDOWS
24928 else if (EQ (pointer, intern ("vdrag")))
24929 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24930 #endif
24931 else if (EQ (pointer, intern ("hourglass")))
24932 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24933 else if (EQ (pointer, Qmodeline))
24934 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24935 else
24936 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24937 }
24938
24939 if (cursor != No_Cursor)
24940 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24941 }
24942
24943 #endif /* HAVE_WINDOW_SYSTEM */
24944
24945 /* Take proper action when mouse has moved to the mode or header line
24946 or marginal area AREA of window W, x-position X and y-position Y.
24947 X is relative to the start of the text display area of W, so the
24948 width of bitmap areas and scroll bars must be subtracted to get a
24949 position relative to the start of the mode line. */
24950
24951 static void
24952 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
24953 enum window_part area)
24954 {
24955 struct window *w = XWINDOW (window);
24956 struct frame *f = XFRAME (w->frame);
24957 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24958 #ifdef HAVE_WINDOW_SYSTEM
24959 Display_Info *dpyinfo;
24960 #endif
24961 Cursor cursor = No_Cursor;
24962 Lisp_Object pointer = Qnil;
24963 int dx, dy, width, height;
24964 EMACS_INT charpos;
24965 Lisp_Object string, object = Qnil;
24966 Lisp_Object pos, help;
24967
24968 Lisp_Object mouse_face;
24969 int original_x_pixel = x;
24970 struct glyph * glyph = NULL, * row_start_glyph = NULL;
24971 struct glyph_row *row;
24972
24973 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
24974 {
24975 int x0;
24976 struct glyph *end;
24977
24978 /* Kludge alert: mode_line_string takes X/Y in pixels, but
24979 returns them in row/column units! */
24980 string = mode_line_string (w, area, &x, &y, &charpos,
24981 &object, &dx, &dy, &width, &height);
24982
24983 row = (area == ON_MODE_LINE
24984 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
24985 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
24986
24987 /* Find the glyph under the mouse pointer. */
24988 if (row->mode_line_p && row->enabled_p)
24989 {
24990 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
24991 end = glyph + row->used[TEXT_AREA];
24992
24993 for (x0 = original_x_pixel;
24994 glyph < end && x0 >= glyph->pixel_width;
24995 ++glyph)
24996 x0 -= glyph->pixel_width;
24997
24998 if (glyph >= end)
24999 glyph = NULL;
25000 }
25001 }
25002 else
25003 {
25004 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25005 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25006 returns them in row/column units! */
25007 string = marginal_area_string (w, area, &x, &y, &charpos,
25008 &object, &dx, &dy, &width, &height);
25009 }
25010
25011 help = Qnil;
25012
25013 #ifdef HAVE_WINDOW_SYSTEM
25014 if (IMAGEP (object))
25015 {
25016 Lisp_Object image_map, hotspot;
25017 if ((image_map = Fplist_get (XCDR (object), QCmap),
25018 !NILP (image_map))
25019 && (hotspot = find_hot_spot (image_map, dx, dy),
25020 CONSP (hotspot))
25021 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25022 {
25023 Lisp_Object plist;
25024
25025 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25026 If so, we could look for mouse-enter, mouse-leave
25027 properties in PLIST (and do something...). */
25028 hotspot = XCDR (hotspot);
25029 if (CONSP (hotspot)
25030 && (plist = XCAR (hotspot), CONSP (plist)))
25031 {
25032 pointer = Fplist_get (plist, Qpointer);
25033 if (NILP (pointer))
25034 pointer = Qhand;
25035 help = Fplist_get (plist, Qhelp_echo);
25036 if (!NILP (help))
25037 {
25038 help_echo_string = help;
25039 /* Is this correct? ++kfs */
25040 XSETWINDOW (help_echo_window, w);
25041 help_echo_object = w->buffer;
25042 help_echo_pos = charpos;
25043 }
25044 }
25045 }
25046 if (NILP (pointer))
25047 pointer = Fplist_get (XCDR (object), QCpointer);
25048 }
25049 #endif /* HAVE_WINDOW_SYSTEM */
25050
25051 if (STRINGP (string))
25052 {
25053 pos = make_number (charpos);
25054 /* If we're on a string with `help-echo' text property, arrange
25055 for the help to be displayed. This is done by setting the
25056 global variable help_echo_string to the help string. */
25057 if (NILP (help))
25058 {
25059 help = Fget_text_property (pos, Qhelp_echo, string);
25060 if (!NILP (help))
25061 {
25062 help_echo_string = help;
25063 XSETWINDOW (help_echo_window, w);
25064 help_echo_object = string;
25065 help_echo_pos = charpos;
25066 }
25067 }
25068
25069 #ifdef HAVE_WINDOW_SYSTEM
25070 if (FRAME_WINDOW_P (f))
25071 {
25072 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25073 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25074 if (NILP (pointer))
25075 pointer = Fget_text_property (pos, Qpointer, string);
25076
25077 /* Change the mouse pointer according to what is under X/Y. */
25078 if (NILP (pointer)
25079 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25080 {
25081 Lisp_Object map;
25082 map = Fget_text_property (pos, Qlocal_map, string);
25083 if (!KEYMAPP (map))
25084 map = Fget_text_property (pos, Qkeymap, string);
25085 if (!KEYMAPP (map))
25086 cursor = dpyinfo->vertical_scroll_bar_cursor;
25087 }
25088 }
25089 #endif
25090
25091 /* Change the mouse face according to what is under X/Y. */
25092 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25093 if (!NILP (mouse_face)
25094 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25095 && glyph)
25096 {
25097 Lisp_Object b, e;
25098
25099 struct glyph * tmp_glyph;
25100
25101 int gpos;
25102 int gseq_length;
25103 int total_pixel_width;
25104 EMACS_INT begpos, endpos, ignore;
25105
25106 int vpos, hpos;
25107
25108 b = Fprevious_single_property_change (make_number (charpos + 1),
25109 Qmouse_face, string, Qnil);
25110 if (NILP (b))
25111 begpos = 0;
25112 else
25113 begpos = XINT (b);
25114
25115 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25116 if (NILP (e))
25117 endpos = SCHARS (string);
25118 else
25119 endpos = XINT (e);
25120
25121 /* Calculate the glyph position GPOS of GLYPH in the
25122 displayed string, relative to the beginning of the
25123 highlighted part of the string.
25124
25125 Note: GPOS is different from CHARPOS. CHARPOS is the
25126 position of GLYPH in the internal string object. A mode
25127 line string format has structures which are converted to
25128 a flattened string by the Emacs Lisp interpreter. The
25129 internal string is an element of those structures. The
25130 displayed string is the flattened string. */
25131 tmp_glyph = row_start_glyph;
25132 while (tmp_glyph < glyph
25133 && (!(EQ (tmp_glyph->object, glyph->object)
25134 && begpos <= tmp_glyph->charpos
25135 && tmp_glyph->charpos < endpos)))
25136 tmp_glyph++;
25137 gpos = glyph - tmp_glyph;
25138
25139 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25140 the highlighted part of the displayed string to which
25141 GLYPH belongs. Note: GSEQ_LENGTH is different from
25142 SCHARS (STRING), because the latter returns the length of
25143 the internal string. */
25144 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25145 tmp_glyph > glyph
25146 && (!(EQ (tmp_glyph->object, glyph->object)
25147 && begpos <= tmp_glyph->charpos
25148 && tmp_glyph->charpos < endpos));
25149 tmp_glyph--)
25150 ;
25151 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25152
25153 /* Calculate the total pixel width of all the glyphs between
25154 the beginning of the highlighted area and GLYPH. */
25155 total_pixel_width = 0;
25156 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25157 total_pixel_width += tmp_glyph->pixel_width;
25158
25159 /* Pre calculation of re-rendering position. Note: X is in
25160 column units here, after the call to mode_line_string or
25161 marginal_area_string. */
25162 hpos = x - gpos;
25163 vpos = (area == ON_MODE_LINE
25164 ? (w->current_matrix)->nrows - 1
25165 : 0);
25166
25167 /* If GLYPH's position is included in the region that is
25168 already drawn in mouse face, we have nothing to do. */
25169 if ( EQ (window, hlinfo->mouse_face_window)
25170 && (!row->reversed_p
25171 ? (hlinfo->mouse_face_beg_col <= hpos
25172 && hpos < hlinfo->mouse_face_end_col)
25173 /* In R2L rows we swap BEG and END, see below. */
25174 : (hlinfo->mouse_face_end_col <= hpos
25175 && hpos < hlinfo->mouse_face_beg_col))
25176 && hlinfo->mouse_face_beg_row == vpos )
25177 return;
25178
25179 if (clear_mouse_face (hlinfo))
25180 cursor = No_Cursor;
25181
25182 if (!row->reversed_p)
25183 {
25184 hlinfo->mouse_face_beg_col = hpos;
25185 hlinfo->mouse_face_beg_x = original_x_pixel
25186 - (total_pixel_width + dx);
25187 hlinfo->mouse_face_end_col = hpos + gseq_length;
25188 hlinfo->mouse_face_end_x = 0;
25189 }
25190 else
25191 {
25192 /* In R2L rows, show_mouse_face expects BEG and END
25193 coordinates to be swapped. */
25194 hlinfo->mouse_face_end_col = hpos;
25195 hlinfo->mouse_face_end_x = original_x_pixel
25196 - (total_pixel_width + dx);
25197 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25198 hlinfo->mouse_face_beg_x = 0;
25199 }
25200
25201 hlinfo->mouse_face_beg_row = vpos;
25202 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25203 hlinfo->mouse_face_beg_y = 0;
25204 hlinfo->mouse_face_end_y = 0;
25205 hlinfo->mouse_face_past_end = 0;
25206 hlinfo->mouse_face_window = window;
25207
25208 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25209 charpos,
25210 0, 0, 0,
25211 &ignore,
25212 glyph->face_id,
25213 1);
25214 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25215
25216 if (NILP (pointer))
25217 pointer = Qhand;
25218 }
25219 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25220 clear_mouse_face (hlinfo);
25221 }
25222 #ifdef HAVE_WINDOW_SYSTEM
25223 if (FRAME_WINDOW_P (f))
25224 define_frame_cursor1 (f, cursor, pointer);
25225 #endif
25226 }
25227
25228
25229 /* EXPORT:
25230 Take proper action when the mouse has moved to position X, Y on
25231 frame F as regards highlighting characters that have mouse-face
25232 properties. Also de-highlighting chars where the mouse was before.
25233 X and Y can be negative or out of range. */
25234
25235 void
25236 note_mouse_highlight (struct frame *f, int x, int y)
25237 {
25238 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25239 enum window_part part;
25240 Lisp_Object window;
25241 struct window *w;
25242 Cursor cursor = No_Cursor;
25243 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25244 struct buffer *b;
25245
25246 /* When a menu is active, don't highlight because this looks odd. */
25247 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25248 if (popup_activated ())
25249 return;
25250 #endif
25251
25252 if (NILP (Vmouse_highlight)
25253 || !f->glyphs_initialized_p
25254 || f->pointer_invisible)
25255 return;
25256
25257 hlinfo->mouse_face_mouse_x = x;
25258 hlinfo->mouse_face_mouse_y = y;
25259 hlinfo->mouse_face_mouse_frame = f;
25260
25261 if (hlinfo->mouse_face_defer)
25262 return;
25263
25264 if (gc_in_progress)
25265 {
25266 hlinfo->mouse_face_deferred_gc = 1;
25267 return;
25268 }
25269
25270 /* Which window is that in? */
25271 window = window_from_coordinates (f, x, y, &part, 1);
25272
25273 /* If we were displaying active text in another window, clear that.
25274 Also clear if we move out of text area in same window. */
25275 if (! EQ (window, hlinfo->mouse_face_window)
25276 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25277 && !NILP (hlinfo->mouse_face_window)))
25278 clear_mouse_face (hlinfo);
25279
25280 /* Not on a window -> return. */
25281 if (!WINDOWP (window))
25282 return;
25283
25284 /* Reset help_echo_string. It will get recomputed below. */
25285 help_echo_string = Qnil;
25286
25287 /* Convert to window-relative pixel coordinates. */
25288 w = XWINDOW (window);
25289 frame_to_window_pixel_xy (w, &x, &y);
25290
25291 #ifdef HAVE_WINDOW_SYSTEM
25292 /* Handle tool-bar window differently since it doesn't display a
25293 buffer. */
25294 if (EQ (window, f->tool_bar_window))
25295 {
25296 note_tool_bar_highlight (f, x, y);
25297 return;
25298 }
25299 #endif
25300
25301 /* Mouse is on the mode, header line or margin? */
25302 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25303 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25304 {
25305 note_mode_line_or_margin_highlight (window, x, y, part);
25306 return;
25307 }
25308
25309 #ifdef HAVE_WINDOW_SYSTEM
25310 if (part == ON_VERTICAL_BORDER)
25311 {
25312 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25313 help_echo_string = build_string ("drag-mouse-1: resize");
25314 }
25315 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25316 || part == ON_SCROLL_BAR)
25317 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25318 else
25319 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25320 #endif
25321
25322 /* Are we in a window whose display is up to date?
25323 And verify the buffer's text has not changed. */
25324 b = XBUFFER (w->buffer);
25325 if (part == ON_TEXT
25326 && EQ (w->window_end_valid, w->buffer)
25327 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25328 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25329 {
25330 int hpos, vpos, i, dx, dy, area;
25331 EMACS_INT pos;
25332 struct glyph *glyph;
25333 Lisp_Object object;
25334 Lisp_Object mouse_face = Qnil, position;
25335 Lisp_Object *overlay_vec = NULL;
25336 int noverlays;
25337 struct buffer *obuf;
25338 EMACS_INT obegv, ozv;
25339 int same_region;
25340
25341 /* Find the glyph under X/Y. */
25342 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25343
25344 #ifdef HAVE_WINDOW_SYSTEM
25345 /* Look for :pointer property on image. */
25346 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25347 {
25348 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25349 if (img != NULL && IMAGEP (img->spec))
25350 {
25351 Lisp_Object image_map, hotspot;
25352 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25353 !NILP (image_map))
25354 && (hotspot = find_hot_spot (image_map,
25355 glyph->slice.img.x + dx,
25356 glyph->slice.img.y + dy),
25357 CONSP (hotspot))
25358 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25359 {
25360 Lisp_Object plist;
25361
25362 /* Could check XCAR (hotspot) to see if we enter/leave
25363 this hot-spot.
25364 If so, we could look for mouse-enter, mouse-leave
25365 properties in PLIST (and do something...). */
25366 hotspot = XCDR (hotspot);
25367 if (CONSP (hotspot)
25368 && (plist = XCAR (hotspot), CONSP (plist)))
25369 {
25370 pointer = Fplist_get (plist, Qpointer);
25371 if (NILP (pointer))
25372 pointer = Qhand;
25373 help_echo_string = Fplist_get (plist, Qhelp_echo);
25374 if (!NILP (help_echo_string))
25375 {
25376 help_echo_window = window;
25377 help_echo_object = glyph->object;
25378 help_echo_pos = glyph->charpos;
25379 }
25380 }
25381 }
25382 if (NILP (pointer))
25383 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25384 }
25385 }
25386 #endif /* HAVE_WINDOW_SYSTEM */
25387
25388 /* Clear mouse face if X/Y not over text. */
25389 if (glyph == NULL
25390 || area != TEXT_AREA
25391 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25392 /* Glyph's OBJECT is an integer for glyphs inserted by the
25393 display engine for its internal purposes, like truncation
25394 and continuation glyphs and blanks beyond the end of
25395 line's text on text terminals. If we are over such a
25396 glyph, we are not over any text. */
25397 || INTEGERP (glyph->object)
25398 /* R2L rows have a stretch glyph at their front, which
25399 stands for no text, whereas L2R rows have no glyphs at
25400 all beyond the end of text. Treat such stretch glyphs
25401 like we do with NULL glyphs in L2R rows. */
25402 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25403 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25404 && glyph->type == STRETCH_GLYPH
25405 && glyph->avoid_cursor_p))
25406 {
25407 if (clear_mouse_face (hlinfo))
25408 cursor = No_Cursor;
25409 #ifdef HAVE_WINDOW_SYSTEM
25410 if (FRAME_WINDOW_P (f) && NILP (pointer))
25411 {
25412 if (area != TEXT_AREA)
25413 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25414 else
25415 pointer = Vvoid_text_area_pointer;
25416 }
25417 #endif
25418 goto set_cursor;
25419 }
25420
25421 pos = glyph->charpos;
25422 object = glyph->object;
25423 if (!STRINGP (object) && !BUFFERP (object))
25424 goto set_cursor;
25425
25426 /* If we get an out-of-range value, return now; avoid an error. */
25427 if (BUFFERP (object) && pos > BUF_Z (b))
25428 goto set_cursor;
25429
25430 /* Make the window's buffer temporarily current for
25431 overlays_at and compute_char_face. */
25432 obuf = current_buffer;
25433 current_buffer = b;
25434 obegv = BEGV;
25435 ozv = ZV;
25436 BEGV = BEG;
25437 ZV = Z;
25438
25439 /* Is this char mouse-active or does it have help-echo? */
25440 position = make_number (pos);
25441
25442 if (BUFFERP (object))
25443 {
25444 /* Put all the overlays we want in a vector in overlay_vec. */
25445 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25446 /* Sort overlays into increasing priority order. */
25447 noverlays = sort_overlays (overlay_vec, noverlays, w);
25448 }
25449 else
25450 noverlays = 0;
25451
25452 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25453
25454 if (same_region)
25455 cursor = No_Cursor;
25456
25457 /* Check mouse-face highlighting. */
25458 if (! same_region
25459 /* If there exists an overlay with mouse-face overlapping
25460 the one we are currently highlighting, we have to
25461 check if we enter the overlapping overlay, and then
25462 highlight only that. */
25463 || (OVERLAYP (hlinfo->mouse_face_overlay)
25464 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25465 {
25466 /* Find the highest priority overlay with a mouse-face. */
25467 Lisp_Object overlay = Qnil;
25468 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25469 {
25470 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25471 if (!NILP (mouse_face))
25472 overlay = overlay_vec[i];
25473 }
25474
25475 /* If we're highlighting the same overlay as before, there's
25476 no need to do that again. */
25477 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25478 goto check_help_echo;
25479 hlinfo->mouse_face_overlay = overlay;
25480
25481 /* Clear the display of the old active region, if any. */
25482 if (clear_mouse_face (hlinfo))
25483 cursor = No_Cursor;
25484
25485 /* If no overlay applies, get a text property. */
25486 if (NILP (overlay))
25487 mouse_face = Fget_text_property (position, Qmouse_face, object);
25488
25489 /* Next, compute the bounds of the mouse highlighting and
25490 display it. */
25491 if (!NILP (mouse_face) && STRINGP (object))
25492 {
25493 /* The mouse-highlighting comes from a display string
25494 with a mouse-face. */
25495 Lisp_Object s, e;
25496 EMACS_INT ignore;
25497
25498 s = Fprevious_single_property_change
25499 (make_number (pos + 1), Qmouse_face, object, Qnil);
25500 e = Fnext_single_property_change
25501 (position, Qmouse_face, object, Qnil);
25502 if (NILP (s))
25503 s = make_number (0);
25504 if (NILP (e))
25505 e = make_number (SCHARS (object) - 1);
25506 mouse_face_from_string_pos (w, hlinfo, object,
25507 XINT (s), XINT (e));
25508 hlinfo->mouse_face_past_end = 0;
25509 hlinfo->mouse_face_window = window;
25510 hlinfo->mouse_face_face_id
25511 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25512 glyph->face_id, 1);
25513 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25514 cursor = No_Cursor;
25515 }
25516 else
25517 {
25518 /* The mouse-highlighting, if any, comes from an overlay
25519 or text property in the buffer. */
25520 Lisp_Object buffer IF_LINT (= Qnil);
25521 Lisp_Object cover_string IF_LINT (= Qnil);
25522
25523 if (STRINGP (object))
25524 {
25525 /* If we are on a display string with no mouse-face,
25526 check if the text under it has one. */
25527 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25528 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25529 pos = string_buffer_position (object, start);
25530 if (pos > 0)
25531 {
25532 mouse_face = get_char_property_and_overlay
25533 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25534 buffer = w->buffer;
25535 cover_string = object;
25536 }
25537 }
25538 else
25539 {
25540 buffer = object;
25541 cover_string = Qnil;
25542 }
25543
25544 if (!NILP (mouse_face))
25545 {
25546 Lisp_Object before, after;
25547 Lisp_Object before_string, after_string;
25548 /* To correctly find the limits of mouse highlight
25549 in a bidi-reordered buffer, we must not use the
25550 optimization of limiting the search in
25551 previous-single-property-change and
25552 next-single-property-change, because
25553 rows_from_pos_range needs the real start and end
25554 positions to DTRT in this case. That's because
25555 the first row visible in a window does not
25556 necessarily display the character whose position
25557 is the smallest. */
25558 Lisp_Object lim1 =
25559 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
25560 ? Fmarker_position (w->start)
25561 : Qnil;
25562 Lisp_Object lim2 =
25563 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
25564 ? make_number (BUF_Z (XBUFFER (buffer))
25565 - XFASTINT (w->window_end_pos))
25566 : Qnil;
25567
25568 if (NILP (overlay))
25569 {
25570 /* Handle the text property case. */
25571 before = Fprevious_single_property_change
25572 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25573 after = Fnext_single_property_change
25574 (make_number (pos), Qmouse_face, buffer, lim2);
25575 before_string = after_string = Qnil;
25576 }
25577 else
25578 {
25579 /* Handle the overlay case. */
25580 before = Foverlay_start (overlay);
25581 after = Foverlay_end (overlay);
25582 before_string = Foverlay_get (overlay, Qbefore_string);
25583 after_string = Foverlay_get (overlay, Qafter_string);
25584
25585 if (!STRINGP (before_string)) before_string = Qnil;
25586 if (!STRINGP (after_string)) after_string = Qnil;
25587 }
25588
25589 mouse_face_from_buffer_pos (window, hlinfo, pos,
25590 XFASTINT (before),
25591 XFASTINT (after),
25592 before_string, after_string,
25593 cover_string);
25594 cursor = No_Cursor;
25595 }
25596 }
25597 }
25598
25599 check_help_echo:
25600
25601 /* Look for a `help-echo' property. */
25602 if (NILP (help_echo_string)) {
25603 Lisp_Object help, overlay;
25604
25605 /* Check overlays first. */
25606 help = overlay = Qnil;
25607 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25608 {
25609 overlay = overlay_vec[i];
25610 help = Foverlay_get (overlay, Qhelp_echo);
25611 }
25612
25613 if (!NILP (help))
25614 {
25615 help_echo_string = help;
25616 help_echo_window = window;
25617 help_echo_object = overlay;
25618 help_echo_pos = pos;
25619 }
25620 else
25621 {
25622 Lisp_Object obj = glyph->object;
25623 EMACS_INT charpos = glyph->charpos;
25624
25625 /* Try text properties. */
25626 if (STRINGP (obj)
25627 && charpos >= 0
25628 && charpos < SCHARS (obj))
25629 {
25630 help = Fget_text_property (make_number (charpos),
25631 Qhelp_echo, obj);
25632 if (NILP (help))
25633 {
25634 /* If the string itself doesn't specify a help-echo,
25635 see if the buffer text ``under'' it does. */
25636 struct glyph_row *r
25637 = MATRIX_ROW (w->current_matrix, vpos);
25638 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25639 EMACS_INT p = string_buffer_position (obj, start);
25640 if (p > 0)
25641 {
25642 help = Fget_char_property (make_number (p),
25643 Qhelp_echo, w->buffer);
25644 if (!NILP (help))
25645 {
25646 charpos = p;
25647 obj = w->buffer;
25648 }
25649 }
25650 }
25651 }
25652 else if (BUFFERP (obj)
25653 && charpos >= BEGV
25654 && charpos < ZV)
25655 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25656 obj);
25657
25658 if (!NILP (help))
25659 {
25660 help_echo_string = help;
25661 help_echo_window = window;
25662 help_echo_object = obj;
25663 help_echo_pos = charpos;
25664 }
25665 }
25666 }
25667
25668 #ifdef HAVE_WINDOW_SYSTEM
25669 /* Look for a `pointer' property. */
25670 if (FRAME_WINDOW_P (f) && NILP (pointer))
25671 {
25672 /* Check overlays first. */
25673 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25674 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25675
25676 if (NILP (pointer))
25677 {
25678 Lisp_Object obj = glyph->object;
25679 EMACS_INT charpos = glyph->charpos;
25680
25681 /* Try text properties. */
25682 if (STRINGP (obj)
25683 && charpos >= 0
25684 && charpos < SCHARS (obj))
25685 {
25686 pointer = Fget_text_property (make_number (charpos),
25687 Qpointer, obj);
25688 if (NILP (pointer))
25689 {
25690 /* If the string itself doesn't specify a pointer,
25691 see if the buffer text ``under'' it does. */
25692 struct glyph_row *r
25693 = MATRIX_ROW (w->current_matrix, vpos);
25694 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25695 EMACS_INT p = string_buffer_position (obj, start);
25696 if (p > 0)
25697 pointer = Fget_char_property (make_number (p),
25698 Qpointer, w->buffer);
25699 }
25700 }
25701 else if (BUFFERP (obj)
25702 && charpos >= BEGV
25703 && charpos < ZV)
25704 pointer = Fget_text_property (make_number (charpos),
25705 Qpointer, obj);
25706 }
25707 }
25708 #endif /* HAVE_WINDOW_SYSTEM */
25709
25710 BEGV = obegv;
25711 ZV = ozv;
25712 current_buffer = obuf;
25713 }
25714
25715 set_cursor:
25716
25717 #ifdef HAVE_WINDOW_SYSTEM
25718 if (FRAME_WINDOW_P (f))
25719 define_frame_cursor1 (f, cursor, pointer);
25720 #else
25721 /* This is here to prevent a compiler error, about "label at end of
25722 compound statement". */
25723 return;
25724 #endif
25725 }
25726
25727
25728 /* EXPORT for RIF:
25729 Clear any mouse-face on window W. This function is part of the
25730 redisplay interface, and is called from try_window_id and similar
25731 functions to ensure the mouse-highlight is off. */
25732
25733 void
25734 x_clear_window_mouse_face (struct window *w)
25735 {
25736 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25737 Lisp_Object window;
25738
25739 BLOCK_INPUT;
25740 XSETWINDOW (window, w);
25741 if (EQ (window, hlinfo->mouse_face_window))
25742 clear_mouse_face (hlinfo);
25743 UNBLOCK_INPUT;
25744 }
25745
25746
25747 /* EXPORT:
25748 Just discard the mouse face information for frame F, if any.
25749 This is used when the size of F is changed. */
25750
25751 void
25752 cancel_mouse_face (struct frame *f)
25753 {
25754 Lisp_Object window;
25755 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25756
25757 window = hlinfo->mouse_face_window;
25758 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25759 {
25760 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25761 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25762 hlinfo->mouse_face_window = Qnil;
25763 }
25764 }
25765
25766
25767 \f
25768 /***********************************************************************
25769 Exposure Events
25770 ***********************************************************************/
25771
25772 #ifdef HAVE_WINDOW_SYSTEM
25773
25774 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25775 which intersects rectangle R. R is in window-relative coordinates. */
25776
25777 static void
25778 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25779 enum glyph_row_area area)
25780 {
25781 struct glyph *first = row->glyphs[area];
25782 struct glyph *end = row->glyphs[area] + row->used[area];
25783 struct glyph *last;
25784 int first_x, start_x, x;
25785
25786 if (area == TEXT_AREA && row->fill_line_p)
25787 /* If row extends face to end of line write the whole line. */
25788 draw_glyphs (w, 0, row, area,
25789 0, row->used[area],
25790 DRAW_NORMAL_TEXT, 0);
25791 else
25792 {
25793 /* Set START_X to the window-relative start position for drawing glyphs of
25794 AREA. The first glyph of the text area can be partially visible.
25795 The first glyphs of other areas cannot. */
25796 start_x = window_box_left_offset (w, area);
25797 x = start_x;
25798 if (area == TEXT_AREA)
25799 x += row->x;
25800
25801 /* Find the first glyph that must be redrawn. */
25802 while (first < end
25803 && x + first->pixel_width < r->x)
25804 {
25805 x += first->pixel_width;
25806 ++first;
25807 }
25808
25809 /* Find the last one. */
25810 last = first;
25811 first_x = x;
25812 while (last < end
25813 && x < r->x + r->width)
25814 {
25815 x += last->pixel_width;
25816 ++last;
25817 }
25818
25819 /* Repaint. */
25820 if (last > first)
25821 draw_glyphs (w, first_x - start_x, row, area,
25822 first - row->glyphs[area], last - row->glyphs[area],
25823 DRAW_NORMAL_TEXT, 0);
25824 }
25825 }
25826
25827
25828 /* Redraw the parts of the glyph row ROW on window W intersecting
25829 rectangle R. R is in window-relative coordinates. Value is
25830 non-zero if mouse-face was overwritten. */
25831
25832 static int
25833 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25834 {
25835 xassert (row->enabled_p);
25836
25837 if (row->mode_line_p || w->pseudo_window_p)
25838 draw_glyphs (w, 0, row, TEXT_AREA,
25839 0, row->used[TEXT_AREA],
25840 DRAW_NORMAL_TEXT, 0);
25841 else
25842 {
25843 if (row->used[LEFT_MARGIN_AREA])
25844 expose_area (w, row, r, LEFT_MARGIN_AREA);
25845 if (row->used[TEXT_AREA])
25846 expose_area (w, row, r, TEXT_AREA);
25847 if (row->used[RIGHT_MARGIN_AREA])
25848 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25849 draw_row_fringe_bitmaps (w, row);
25850 }
25851
25852 return row->mouse_face_p;
25853 }
25854
25855
25856 /* Redraw those parts of glyphs rows during expose event handling that
25857 overlap other rows. Redrawing of an exposed line writes over parts
25858 of lines overlapping that exposed line; this function fixes that.
25859
25860 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25861 row in W's current matrix that is exposed and overlaps other rows.
25862 LAST_OVERLAPPING_ROW is the last such row. */
25863
25864 static void
25865 expose_overlaps (struct window *w,
25866 struct glyph_row *first_overlapping_row,
25867 struct glyph_row *last_overlapping_row,
25868 XRectangle *r)
25869 {
25870 struct glyph_row *row;
25871
25872 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25873 if (row->overlapping_p)
25874 {
25875 xassert (row->enabled_p && !row->mode_line_p);
25876
25877 row->clip = r;
25878 if (row->used[LEFT_MARGIN_AREA])
25879 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25880
25881 if (row->used[TEXT_AREA])
25882 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25883
25884 if (row->used[RIGHT_MARGIN_AREA])
25885 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
25886 row->clip = NULL;
25887 }
25888 }
25889
25890
25891 /* Return non-zero if W's cursor intersects rectangle R. */
25892
25893 static int
25894 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
25895 {
25896 XRectangle cr, result;
25897 struct glyph *cursor_glyph;
25898 struct glyph_row *row;
25899
25900 if (w->phys_cursor.vpos >= 0
25901 && w->phys_cursor.vpos < w->current_matrix->nrows
25902 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
25903 row->enabled_p)
25904 && row->cursor_in_fringe_p)
25905 {
25906 /* Cursor is in the fringe. */
25907 cr.x = window_box_right_offset (w,
25908 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
25909 ? RIGHT_MARGIN_AREA
25910 : TEXT_AREA));
25911 cr.y = row->y;
25912 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25913 cr.height = row->height;
25914 return x_intersect_rectangles (&cr, r, &result);
25915 }
25916
25917 cursor_glyph = get_phys_cursor_glyph (w);
25918 if (cursor_glyph)
25919 {
25920 /* r is relative to W's box, but w->phys_cursor.x is relative
25921 to left edge of W's TEXT area. Adjust it. */
25922 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25923 cr.y = w->phys_cursor.y;
25924 cr.width = cursor_glyph->pixel_width;
25925 cr.height = w->phys_cursor_height;
25926 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25927 I assume the effect is the same -- and this is portable. */
25928 return x_intersect_rectangles (&cr, r, &result);
25929 }
25930 /* If we don't understand the format, pretend we're not in the hot-spot. */
25931 return 0;
25932 }
25933
25934
25935 /* EXPORT:
25936 Draw a vertical window border to the right of window W if W doesn't
25937 have vertical scroll bars. */
25938
25939 void
25940 x_draw_vertical_border (struct window *w)
25941 {
25942 struct frame *f = XFRAME (WINDOW_FRAME (w));
25943
25944 /* We could do better, if we knew what type of scroll-bar the adjacent
25945 windows (on either side) have... But we don't :-(
25946 However, I think this works ok. ++KFS 2003-04-25 */
25947
25948 /* Redraw borders between horizontally adjacent windows. Don't
25949 do it for frames with vertical scroll bars because either the
25950 right scroll bar of a window, or the left scroll bar of its
25951 neighbor will suffice as a border. */
25952 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
25953 return;
25954
25955 if (!WINDOW_RIGHTMOST_P (w)
25956 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
25957 {
25958 int x0, x1, y0, y1;
25959
25960 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25961 y1 -= 1;
25962
25963 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25964 x1 -= 1;
25965
25966 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
25967 }
25968 else if (!WINDOW_LEFTMOST_P (w)
25969 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
25970 {
25971 int x0, x1, y0, y1;
25972
25973 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25974 y1 -= 1;
25975
25976 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25977 x0 -= 1;
25978
25979 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
25980 }
25981 }
25982
25983
25984 /* Redraw the part of window W intersection rectangle FR. Pixel
25985 coordinates in FR are frame-relative. Call this function with
25986 input blocked. Value is non-zero if the exposure overwrites
25987 mouse-face. */
25988
25989 static int
25990 expose_window (struct window *w, XRectangle *fr)
25991 {
25992 struct frame *f = XFRAME (w->frame);
25993 XRectangle wr, r;
25994 int mouse_face_overwritten_p = 0;
25995
25996 /* If window is not yet fully initialized, do nothing. This can
25997 happen when toolkit scroll bars are used and a window is split.
25998 Reconfiguring the scroll bar will generate an expose for a newly
25999 created window. */
26000 if (w->current_matrix == NULL)
26001 return 0;
26002
26003 /* When we're currently updating the window, display and current
26004 matrix usually don't agree. Arrange for a thorough display
26005 later. */
26006 if (w == updated_window)
26007 {
26008 SET_FRAME_GARBAGED (f);
26009 return 0;
26010 }
26011
26012 /* Frame-relative pixel rectangle of W. */
26013 wr.x = WINDOW_LEFT_EDGE_X (w);
26014 wr.y = WINDOW_TOP_EDGE_Y (w);
26015 wr.width = WINDOW_TOTAL_WIDTH (w);
26016 wr.height = WINDOW_TOTAL_HEIGHT (w);
26017
26018 if (x_intersect_rectangles (fr, &wr, &r))
26019 {
26020 int yb = window_text_bottom_y (w);
26021 struct glyph_row *row;
26022 int cursor_cleared_p;
26023 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26024
26025 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26026 r.x, r.y, r.width, r.height));
26027
26028 /* Convert to window coordinates. */
26029 r.x -= WINDOW_LEFT_EDGE_X (w);
26030 r.y -= WINDOW_TOP_EDGE_Y (w);
26031
26032 /* Turn off the cursor. */
26033 if (!w->pseudo_window_p
26034 && phys_cursor_in_rect_p (w, &r))
26035 {
26036 x_clear_cursor (w);
26037 cursor_cleared_p = 1;
26038 }
26039 else
26040 cursor_cleared_p = 0;
26041
26042 /* Update lines intersecting rectangle R. */
26043 first_overlapping_row = last_overlapping_row = NULL;
26044 for (row = w->current_matrix->rows;
26045 row->enabled_p;
26046 ++row)
26047 {
26048 int y0 = row->y;
26049 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26050
26051 if ((y0 >= r.y && y0 < r.y + r.height)
26052 || (y1 > r.y && y1 < r.y + r.height)
26053 || (r.y >= y0 && r.y < y1)
26054 || (r.y + r.height > y0 && r.y + r.height < y1))
26055 {
26056 /* A header line may be overlapping, but there is no need
26057 to fix overlapping areas for them. KFS 2005-02-12 */
26058 if (row->overlapping_p && !row->mode_line_p)
26059 {
26060 if (first_overlapping_row == NULL)
26061 first_overlapping_row = row;
26062 last_overlapping_row = row;
26063 }
26064
26065 row->clip = fr;
26066 if (expose_line (w, row, &r))
26067 mouse_face_overwritten_p = 1;
26068 row->clip = NULL;
26069 }
26070 else if (row->overlapping_p)
26071 {
26072 /* We must redraw a row overlapping the exposed area. */
26073 if (y0 < r.y
26074 ? y0 + row->phys_height > r.y
26075 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26076 {
26077 if (first_overlapping_row == NULL)
26078 first_overlapping_row = row;
26079 last_overlapping_row = row;
26080 }
26081 }
26082
26083 if (y1 >= yb)
26084 break;
26085 }
26086
26087 /* Display the mode line if there is one. */
26088 if (WINDOW_WANTS_MODELINE_P (w)
26089 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26090 row->enabled_p)
26091 && row->y < r.y + r.height)
26092 {
26093 if (expose_line (w, row, &r))
26094 mouse_face_overwritten_p = 1;
26095 }
26096
26097 if (!w->pseudo_window_p)
26098 {
26099 /* Fix the display of overlapping rows. */
26100 if (first_overlapping_row)
26101 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26102 fr);
26103
26104 /* Draw border between windows. */
26105 x_draw_vertical_border (w);
26106
26107 /* Turn the cursor on again. */
26108 if (cursor_cleared_p)
26109 update_window_cursor (w, 1);
26110 }
26111 }
26112
26113 return mouse_face_overwritten_p;
26114 }
26115
26116
26117
26118 /* Redraw (parts) of all windows in the window tree rooted at W that
26119 intersect R. R contains frame pixel coordinates. Value is
26120 non-zero if the exposure overwrites mouse-face. */
26121
26122 static int
26123 expose_window_tree (struct window *w, XRectangle *r)
26124 {
26125 struct frame *f = XFRAME (w->frame);
26126 int mouse_face_overwritten_p = 0;
26127
26128 while (w && !FRAME_GARBAGED_P (f))
26129 {
26130 if (!NILP (w->hchild))
26131 mouse_face_overwritten_p
26132 |= expose_window_tree (XWINDOW (w->hchild), r);
26133 else if (!NILP (w->vchild))
26134 mouse_face_overwritten_p
26135 |= expose_window_tree (XWINDOW (w->vchild), r);
26136 else
26137 mouse_face_overwritten_p |= expose_window (w, r);
26138
26139 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26140 }
26141
26142 return mouse_face_overwritten_p;
26143 }
26144
26145
26146 /* EXPORT:
26147 Redisplay an exposed area of frame F. X and Y are the upper-left
26148 corner of the exposed rectangle. W and H are width and height of
26149 the exposed area. All are pixel values. W or H zero means redraw
26150 the entire frame. */
26151
26152 void
26153 expose_frame (struct frame *f, int x, int y, int w, int h)
26154 {
26155 XRectangle r;
26156 int mouse_face_overwritten_p = 0;
26157
26158 TRACE ((stderr, "expose_frame "));
26159
26160 /* No need to redraw if frame will be redrawn soon. */
26161 if (FRAME_GARBAGED_P (f))
26162 {
26163 TRACE ((stderr, " garbaged\n"));
26164 return;
26165 }
26166
26167 /* If basic faces haven't been realized yet, there is no point in
26168 trying to redraw anything. This can happen when we get an expose
26169 event while Emacs is starting, e.g. by moving another window. */
26170 if (FRAME_FACE_CACHE (f) == NULL
26171 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26172 {
26173 TRACE ((stderr, " no faces\n"));
26174 return;
26175 }
26176
26177 if (w == 0 || h == 0)
26178 {
26179 r.x = r.y = 0;
26180 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26181 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26182 }
26183 else
26184 {
26185 r.x = x;
26186 r.y = y;
26187 r.width = w;
26188 r.height = h;
26189 }
26190
26191 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26192 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26193
26194 if (WINDOWP (f->tool_bar_window))
26195 mouse_face_overwritten_p
26196 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26197
26198 #ifdef HAVE_X_WINDOWS
26199 #ifndef MSDOS
26200 #ifndef USE_X_TOOLKIT
26201 if (WINDOWP (f->menu_bar_window))
26202 mouse_face_overwritten_p
26203 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26204 #endif /* not USE_X_TOOLKIT */
26205 #endif
26206 #endif
26207
26208 /* Some window managers support a focus-follows-mouse style with
26209 delayed raising of frames. Imagine a partially obscured frame,
26210 and moving the mouse into partially obscured mouse-face on that
26211 frame. The visible part of the mouse-face will be highlighted,
26212 then the WM raises the obscured frame. With at least one WM, KDE
26213 2.1, Emacs is not getting any event for the raising of the frame
26214 (even tried with SubstructureRedirectMask), only Expose events.
26215 These expose events will draw text normally, i.e. not
26216 highlighted. Which means we must redo the highlight here.
26217 Subsume it under ``we love X''. --gerd 2001-08-15 */
26218 /* Included in Windows version because Windows most likely does not
26219 do the right thing if any third party tool offers
26220 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26221 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26222 {
26223 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26224 if (f == hlinfo->mouse_face_mouse_frame)
26225 {
26226 int mouse_x = hlinfo->mouse_face_mouse_x;
26227 int mouse_y = hlinfo->mouse_face_mouse_y;
26228 clear_mouse_face (hlinfo);
26229 note_mouse_highlight (f, mouse_x, mouse_y);
26230 }
26231 }
26232 }
26233
26234
26235 /* EXPORT:
26236 Determine the intersection of two rectangles R1 and R2. Return
26237 the intersection in *RESULT. Value is non-zero if RESULT is not
26238 empty. */
26239
26240 int
26241 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26242 {
26243 XRectangle *left, *right;
26244 XRectangle *upper, *lower;
26245 int intersection_p = 0;
26246
26247 /* Rearrange so that R1 is the left-most rectangle. */
26248 if (r1->x < r2->x)
26249 left = r1, right = r2;
26250 else
26251 left = r2, right = r1;
26252
26253 /* X0 of the intersection is right.x0, if this is inside R1,
26254 otherwise there is no intersection. */
26255 if (right->x <= left->x + left->width)
26256 {
26257 result->x = right->x;
26258
26259 /* The right end of the intersection is the minimum of the
26260 the right ends of left and right. */
26261 result->width = (min (left->x + left->width, right->x + right->width)
26262 - result->x);
26263
26264 /* Same game for Y. */
26265 if (r1->y < r2->y)
26266 upper = r1, lower = r2;
26267 else
26268 upper = r2, lower = r1;
26269
26270 /* The upper end of the intersection is lower.y0, if this is inside
26271 of upper. Otherwise, there is no intersection. */
26272 if (lower->y <= upper->y + upper->height)
26273 {
26274 result->y = lower->y;
26275
26276 /* The lower end of the intersection is the minimum of the lower
26277 ends of upper and lower. */
26278 result->height = (min (lower->y + lower->height,
26279 upper->y + upper->height)
26280 - result->y);
26281 intersection_p = 1;
26282 }
26283 }
26284
26285 return intersection_p;
26286 }
26287
26288 #endif /* HAVE_WINDOW_SYSTEM */
26289
26290 \f
26291 /***********************************************************************
26292 Initialization
26293 ***********************************************************************/
26294
26295 void
26296 syms_of_xdisp (void)
26297 {
26298 Vwith_echo_area_save_vector = Qnil;
26299 staticpro (&Vwith_echo_area_save_vector);
26300
26301 Vmessage_stack = Qnil;
26302 staticpro (&Vmessage_stack);
26303
26304 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26305 staticpro (&Qinhibit_redisplay);
26306
26307 message_dolog_marker1 = Fmake_marker ();
26308 staticpro (&message_dolog_marker1);
26309 message_dolog_marker2 = Fmake_marker ();
26310 staticpro (&message_dolog_marker2);
26311 message_dolog_marker3 = Fmake_marker ();
26312 staticpro (&message_dolog_marker3);
26313
26314 #if GLYPH_DEBUG
26315 defsubr (&Sdump_frame_glyph_matrix);
26316 defsubr (&Sdump_glyph_matrix);
26317 defsubr (&Sdump_glyph_row);
26318 defsubr (&Sdump_tool_bar_row);
26319 defsubr (&Strace_redisplay);
26320 defsubr (&Strace_to_stderr);
26321 #endif
26322 #ifdef HAVE_WINDOW_SYSTEM
26323 defsubr (&Stool_bar_lines_needed);
26324 defsubr (&Slookup_image_map);
26325 #endif
26326 defsubr (&Sformat_mode_line);
26327 defsubr (&Sinvisible_p);
26328 defsubr (&Scurrent_bidi_paragraph_direction);
26329
26330 staticpro (&Qmenu_bar_update_hook);
26331 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26332
26333 staticpro (&Qoverriding_terminal_local_map);
26334 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26335
26336 staticpro (&Qoverriding_local_map);
26337 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26338
26339 staticpro (&Qwindow_scroll_functions);
26340 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26341
26342 staticpro (&Qwindow_text_change_functions);
26343 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26344
26345 staticpro (&Qredisplay_end_trigger_functions);
26346 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26347
26348 staticpro (&Qinhibit_point_motion_hooks);
26349 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26350
26351 Qeval = intern_c_string ("eval");
26352 staticpro (&Qeval);
26353
26354 QCdata = intern_c_string (":data");
26355 staticpro (&QCdata);
26356 Qdisplay = intern_c_string ("display");
26357 staticpro (&Qdisplay);
26358 Qspace_width = intern_c_string ("space-width");
26359 staticpro (&Qspace_width);
26360 Qraise = intern_c_string ("raise");
26361 staticpro (&Qraise);
26362 Qslice = intern_c_string ("slice");
26363 staticpro (&Qslice);
26364 Qspace = intern_c_string ("space");
26365 staticpro (&Qspace);
26366 Qmargin = intern_c_string ("margin");
26367 staticpro (&Qmargin);
26368 Qpointer = intern_c_string ("pointer");
26369 staticpro (&Qpointer);
26370 Qleft_margin = intern_c_string ("left-margin");
26371 staticpro (&Qleft_margin);
26372 Qright_margin = intern_c_string ("right-margin");
26373 staticpro (&Qright_margin);
26374 Qcenter = intern_c_string ("center");
26375 staticpro (&Qcenter);
26376 Qline_height = intern_c_string ("line-height");
26377 staticpro (&Qline_height);
26378 QCalign_to = intern_c_string (":align-to");
26379 staticpro (&QCalign_to);
26380 QCrelative_width = intern_c_string (":relative-width");
26381 staticpro (&QCrelative_width);
26382 QCrelative_height = intern_c_string (":relative-height");
26383 staticpro (&QCrelative_height);
26384 QCeval = intern_c_string (":eval");
26385 staticpro (&QCeval);
26386 QCpropertize = intern_c_string (":propertize");
26387 staticpro (&QCpropertize);
26388 QCfile = intern_c_string (":file");
26389 staticpro (&QCfile);
26390 Qfontified = intern_c_string ("fontified");
26391 staticpro (&Qfontified);
26392 Qfontification_functions = intern_c_string ("fontification-functions");
26393 staticpro (&Qfontification_functions);
26394 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26395 staticpro (&Qtrailing_whitespace);
26396 Qescape_glyph = intern_c_string ("escape-glyph");
26397 staticpro (&Qescape_glyph);
26398 Qnobreak_space = intern_c_string ("nobreak-space");
26399 staticpro (&Qnobreak_space);
26400 Qimage = intern_c_string ("image");
26401 staticpro (&Qimage);
26402 Qtext = intern_c_string ("text");
26403 staticpro (&Qtext);
26404 Qboth = intern_c_string ("both");
26405 staticpro (&Qboth);
26406 Qboth_horiz = intern_c_string ("both-horiz");
26407 staticpro (&Qboth_horiz);
26408 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26409 staticpro (&Qtext_image_horiz);
26410 QCmap = intern_c_string (":map");
26411 staticpro (&QCmap);
26412 QCpointer = intern_c_string (":pointer");
26413 staticpro (&QCpointer);
26414 Qrect = intern_c_string ("rect");
26415 staticpro (&Qrect);
26416 Qcircle = intern_c_string ("circle");
26417 staticpro (&Qcircle);
26418 Qpoly = intern_c_string ("poly");
26419 staticpro (&Qpoly);
26420 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26421 staticpro (&Qmessage_truncate_lines);
26422 Qgrow_only = intern_c_string ("grow-only");
26423 staticpro (&Qgrow_only);
26424 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26425 staticpro (&Qinhibit_menubar_update);
26426 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26427 staticpro (&Qinhibit_eval_during_redisplay);
26428 Qposition = intern_c_string ("position");
26429 staticpro (&Qposition);
26430 Qbuffer_position = intern_c_string ("buffer-position");
26431 staticpro (&Qbuffer_position);
26432 Qobject = intern_c_string ("object");
26433 staticpro (&Qobject);
26434 Qbar = intern_c_string ("bar");
26435 staticpro (&Qbar);
26436 Qhbar = intern_c_string ("hbar");
26437 staticpro (&Qhbar);
26438 Qbox = intern_c_string ("box");
26439 staticpro (&Qbox);
26440 Qhollow = intern_c_string ("hollow");
26441 staticpro (&Qhollow);
26442 Qhand = intern_c_string ("hand");
26443 staticpro (&Qhand);
26444 Qarrow = intern_c_string ("arrow");
26445 staticpro (&Qarrow);
26446 Qtext = intern_c_string ("text");
26447 staticpro (&Qtext);
26448 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26449 staticpro (&Qinhibit_free_realized_faces);
26450
26451 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26452 Fcons (intern_c_string ("void-variable"), Qnil)),
26453 Qnil);
26454 staticpro (&list_of_error);
26455
26456 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26457 staticpro (&Qlast_arrow_position);
26458 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26459 staticpro (&Qlast_arrow_string);
26460
26461 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26462 staticpro (&Qoverlay_arrow_string);
26463 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26464 staticpro (&Qoverlay_arrow_bitmap);
26465
26466 echo_buffer[0] = echo_buffer[1] = Qnil;
26467 staticpro (&echo_buffer[0]);
26468 staticpro (&echo_buffer[1]);
26469
26470 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26471 staticpro (&echo_area_buffer[0]);
26472 staticpro (&echo_area_buffer[1]);
26473
26474 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26475 staticpro (&Vmessages_buffer_name);
26476
26477 mode_line_proptrans_alist = Qnil;
26478 staticpro (&mode_line_proptrans_alist);
26479 mode_line_string_list = Qnil;
26480 staticpro (&mode_line_string_list);
26481 mode_line_string_face = Qnil;
26482 staticpro (&mode_line_string_face);
26483 mode_line_string_face_prop = Qnil;
26484 staticpro (&mode_line_string_face_prop);
26485 Vmode_line_unwind_vector = Qnil;
26486 staticpro (&Vmode_line_unwind_vector);
26487
26488 help_echo_string = Qnil;
26489 staticpro (&help_echo_string);
26490 help_echo_object = Qnil;
26491 staticpro (&help_echo_object);
26492 help_echo_window = Qnil;
26493 staticpro (&help_echo_window);
26494 previous_help_echo_string = Qnil;
26495 staticpro (&previous_help_echo_string);
26496 help_echo_pos = -1;
26497
26498 Qright_to_left = intern_c_string ("right-to-left");
26499 staticpro (&Qright_to_left);
26500 Qleft_to_right = intern_c_string ("left-to-right");
26501 staticpro (&Qleft_to_right);
26502
26503 #ifdef HAVE_WINDOW_SYSTEM
26504 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
26505 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26506 For example, if a block cursor is over a tab, it will be drawn as
26507 wide as that tab on the display. */);
26508 x_stretch_cursor_p = 0;
26509 #endif
26510
26511 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
26512 doc: /* *Non-nil means highlight trailing whitespace.
26513 The face used for trailing whitespace is `trailing-whitespace'. */);
26514 Vshow_trailing_whitespace = Qnil;
26515
26516 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
26517 doc: /* *Control highlighting of nobreak space and soft hyphen.
26518 A value of t means highlight the character itself (for nobreak space,
26519 use face `nobreak-space').
26520 A value of nil means no highlighting.
26521 Other values mean display the escape glyph followed by an ordinary
26522 space or ordinary hyphen. */);
26523 Vnobreak_char_display = Qt;
26524
26525 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
26526 doc: /* *The pointer shape to show in void text areas.
26527 A value of nil means to show the text pointer. Other options are `arrow',
26528 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26529 Vvoid_text_area_pointer = Qarrow;
26530
26531 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
26532 doc: /* Non-nil means don't actually do any redisplay.
26533 This is used for internal purposes. */);
26534 Vinhibit_redisplay = Qnil;
26535
26536 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
26537 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26538 Vglobal_mode_string = Qnil;
26539
26540 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
26541 doc: /* Marker for where to display an arrow on top of the buffer text.
26542 This must be the beginning of a line in order to work.
26543 See also `overlay-arrow-string'. */);
26544 Voverlay_arrow_position = Qnil;
26545
26546 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
26547 doc: /* String to display as an arrow in non-window frames.
26548 See also `overlay-arrow-position'. */);
26549 Voverlay_arrow_string = make_pure_c_string ("=>");
26550
26551 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
26552 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26553 The symbols on this list are examined during redisplay to determine
26554 where to display overlay arrows. */);
26555 Voverlay_arrow_variable_list
26556 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26557
26558 DEFVAR_INT ("scroll-step", emacs_scroll_step,
26559 doc: /* *The number of lines to try scrolling a window by when point moves out.
26560 If that fails to bring point back on frame, point is centered instead.
26561 If this is zero, point is always centered after it moves off frame.
26562 If you want scrolling to always be a line at a time, you should set
26563 `scroll-conservatively' to a large value rather than set this to 1. */);
26564
26565 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
26566 doc: /* *Scroll up to this many lines, to bring point back on screen.
26567 If point moves off-screen, redisplay will scroll by up to
26568 `scroll-conservatively' lines in order to bring point just barely
26569 onto the screen again. If that cannot be done, then redisplay
26570 recenters point as usual.
26571
26572 If the value is greater than 100, redisplay will never recenter point,
26573 but will always scroll just enough text to bring point into view, even
26574 if you move far away.
26575
26576 A value of zero means always recenter point if it moves off screen. */);
26577 scroll_conservatively = 0;
26578
26579 DEFVAR_INT ("scroll-margin", scroll_margin,
26580 doc: /* *Number of lines of margin at the top and bottom of a window.
26581 Recenter the window whenever point gets within this many lines
26582 of the top or bottom of the window. */);
26583 scroll_margin = 0;
26584
26585 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
26586 doc: /* Pixels per inch value for non-window system displays.
26587 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26588 Vdisplay_pixels_per_inch = make_float (72.0);
26589
26590 #if GLYPH_DEBUG
26591 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
26592 #endif
26593
26594 DEFVAR_LISP ("truncate-partial-width-windows",
26595 Vtruncate_partial_width_windows,
26596 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26597 For an integer value, truncate lines in each window narrower than the
26598 full frame width, provided the window width is less than that integer;
26599 otherwise, respect the value of `truncate-lines'.
26600
26601 For any other non-nil value, truncate lines in all windows that do
26602 not span the full frame width.
26603
26604 A value of nil means to respect the value of `truncate-lines'.
26605
26606 If `word-wrap' is enabled, you might want to reduce this. */);
26607 Vtruncate_partial_width_windows = make_number (50);
26608
26609 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
26610 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26611 Any other value means to use the appropriate face, `mode-line',
26612 `header-line', or `menu' respectively. */);
26613 mode_line_inverse_video = 1;
26614
26615 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
26616 doc: /* *Maximum buffer size for which line number should be displayed.
26617 If the buffer is bigger than this, the line number does not appear
26618 in the mode line. A value of nil means no limit. */);
26619 Vline_number_display_limit = Qnil;
26620
26621 DEFVAR_INT ("line-number-display-limit-width",
26622 line_number_display_limit_width,
26623 doc: /* *Maximum line width (in characters) for line number display.
26624 If the average length of the lines near point is bigger than this, then the
26625 line number may be omitted from the mode line. */);
26626 line_number_display_limit_width = 200;
26627
26628 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
26629 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26630 highlight_nonselected_windows = 0;
26631
26632 DEFVAR_BOOL ("multiple-frames", multiple_frames,
26633 doc: /* Non-nil if more than one frame is visible on this display.
26634 Minibuffer-only frames don't count, but iconified frames do.
26635 This variable is not guaranteed to be accurate except while processing
26636 `frame-title-format' and `icon-title-format'. */);
26637
26638 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
26639 doc: /* Template for displaying the title bar of visible frames.
26640 \(Assuming the window manager supports this feature.)
26641
26642 This variable has the same structure as `mode-line-format', except that
26643 the %c and %l constructs are ignored. It is used only on frames for
26644 which no explicit name has been set \(see `modify-frame-parameters'). */);
26645
26646 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
26647 doc: /* Template for displaying the title bar of an iconified frame.
26648 \(Assuming the window manager supports this feature.)
26649 This variable has the same structure as `mode-line-format' (which see),
26650 and is used only on frames for which no explicit name has been set
26651 \(see `modify-frame-parameters'). */);
26652 Vicon_title_format
26653 = Vframe_title_format
26654 = pure_cons (intern_c_string ("multiple-frames"),
26655 pure_cons (make_pure_c_string ("%b"),
26656 pure_cons (pure_cons (empty_unibyte_string,
26657 pure_cons (intern_c_string ("invocation-name"),
26658 pure_cons (make_pure_c_string ("@"),
26659 pure_cons (intern_c_string ("system-name"),
26660 Qnil)))),
26661 Qnil)));
26662
26663 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
26664 doc: /* Maximum number of lines to keep in the message log buffer.
26665 If nil, disable message logging. If t, log messages but don't truncate
26666 the buffer when it becomes large. */);
26667 Vmessage_log_max = make_number (100);
26668
26669 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
26670 doc: /* Functions called before redisplay, if window sizes have changed.
26671 The value should be a list of functions that take one argument.
26672 Just before redisplay, for each frame, if any of its windows have changed
26673 size since the last redisplay, or have been split or deleted,
26674 all the functions in the list are called, with the frame as argument. */);
26675 Vwindow_size_change_functions = Qnil;
26676
26677 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
26678 doc: /* List of functions to call before redisplaying a window with scrolling.
26679 Each function is called with two arguments, the window and its new
26680 display-start position. Note that these functions are also called by
26681 `set-window-buffer'. Also note that the value of `window-end' is not
26682 valid when these functions are called. */);
26683 Vwindow_scroll_functions = Qnil;
26684
26685 DEFVAR_LISP ("window-text-change-functions",
26686 Vwindow_text_change_functions,
26687 doc: /* Functions to call in redisplay when text in the window might change. */);
26688 Vwindow_text_change_functions = Qnil;
26689
26690 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
26691 doc: /* Functions called when redisplay of a window reaches the end trigger.
26692 Each function is called with two arguments, the window and the end trigger value.
26693 See `set-window-redisplay-end-trigger'. */);
26694 Vredisplay_end_trigger_functions = Qnil;
26695
26696 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
26697 doc: /* *Non-nil means autoselect window with mouse pointer.
26698 If nil, do not autoselect windows.
26699 A positive number means delay autoselection by that many seconds: a
26700 window is autoselected only after the mouse has remained in that
26701 window for the duration of the delay.
26702 A negative number has a similar effect, but causes windows to be
26703 autoselected only after the mouse has stopped moving. \(Because of
26704 the way Emacs compares mouse events, you will occasionally wait twice
26705 that time before the window gets selected.\)
26706 Any other value means to autoselect window instantaneously when the
26707 mouse pointer enters it.
26708
26709 Autoselection selects the minibuffer only if it is active, and never
26710 unselects the minibuffer if it is active.
26711
26712 When customizing this variable make sure that the actual value of
26713 `focus-follows-mouse' matches the behavior of your window manager. */);
26714 Vmouse_autoselect_window = Qnil;
26715
26716 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
26717 doc: /* *Non-nil means automatically resize tool-bars.
26718 This dynamically changes the tool-bar's height to the minimum height
26719 that is needed to make all tool-bar items visible.
26720 If value is `grow-only', the tool-bar's height is only increased
26721 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26722 Vauto_resize_tool_bars = Qt;
26723
26724 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
26725 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26726 auto_raise_tool_bar_buttons_p = 1;
26727
26728 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
26729 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26730 make_cursor_line_fully_visible_p = 1;
26731
26732 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
26733 doc: /* *Border below tool-bar in pixels.
26734 If an integer, use it as the height of the border.
26735 If it is one of `internal-border-width' or `border-width', use the
26736 value of the corresponding frame parameter.
26737 Otherwise, no border is added below the tool-bar. */);
26738 Vtool_bar_border = Qinternal_border_width;
26739
26740 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
26741 doc: /* *Margin around tool-bar buttons in pixels.
26742 If an integer, use that for both horizontal and vertical margins.
26743 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26744 HORZ specifying the horizontal margin, and VERT specifying the
26745 vertical margin. */);
26746 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26747
26748 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
26749 doc: /* *Relief thickness of tool-bar buttons. */);
26750 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26751
26752 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
26753 doc: /* Tool bar style to use.
26754 It can be one of
26755 image - show images only
26756 text - show text only
26757 both - show both, text below image
26758 both-horiz - show text to the right of the image
26759 text-image-horiz - show text to the left of the image
26760 any other - use system default or image if no system default. */);
26761 Vtool_bar_style = Qnil;
26762
26763 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
26764 doc: /* *Maximum number of characters a label can have to be shown.
26765 The tool bar style must also show labels for this to have any effect, see
26766 `tool-bar-style'. */);
26767 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26768
26769 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
26770 doc: /* List of functions to call to fontify regions of text.
26771 Each function is called with one argument POS. Functions must
26772 fontify a region starting at POS in the current buffer, and give
26773 fontified regions the property `fontified'. */);
26774 Vfontification_functions = Qnil;
26775 Fmake_variable_buffer_local (Qfontification_functions);
26776
26777 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26778 unibyte_display_via_language_environment,
26779 doc: /* *Non-nil means display unibyte text according to language environment.
26780 Specifically, this means that raw bytes in the range 160-255 decimal
26781 are displayed by converting them to the equivalent multibyte characters
26782 according to the current language environment. As a result, they are
26783 displayed according to the current fontset.
26784
26785 Note that this variable affects only how these bytes are displayed,
26786 but does not change the fact they are interpreted as raw bytes. */);
26787 unibyte_display_via_language_environment = 0;
26788
26789 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
26790 doc: /* *Maximum height for resizing mini-windows.
26791 If a float, it specifies a fraction of the mini-window frame's height.
26792 If an integer, it specifies a number of lines. */);
26793 Vmax_mini_window_height = make_float (0.25);
26794
26795 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
26796 doc: /* *How to resize mini-windows.
26797 A value of nil means don't automatically resize mini-windows.
26798 A value of t means resize them to fit the text displayed in them.
26799 A value of `grow-only', the default, means let mini-windows grow
26800 only, until their display becomes empty, at which point the windows
26801 go back to their normal size. */);
26802 Vresize_mini_windows = Qgrow_only;
26803
26804 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
26805 doc: /* Alist specifying how to blink the cursor off.
26806 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26807 `cursor-type' frame-parameter or variable equals ON-STATE,
26808 comparing using `equal', Emacs uses OFF-STATE to specify
26809 how to blink it off. ON-STATE and OFF-STATE are values for
26810 the `cursor-type' frame parameter.
26811
26812 If a frame's ON-STATE has no entry in this list,
26813 the frame's other specifications determine how to blink the cursor off. */);
26814 Vblink_cursor_alist = Qnil;
26815
26816 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
26817 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26818 If non-nil, windows are automatically scrolled horizontally to make
26819 point visible. */);
26820 automatic_hscrolling_p = 1;
26821 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26822 staticpro (&Qauto_hscroll_mode);
26823
26824 DEFVAR_INT ("hscroll-margin", hscroll_margin,
26825 doc: /* *How many columns away from the window edge point is allowed to get
26826 before automatic hscrolling will horizontally scroll the window. */);
26827 hscroll_margin = 5;
26828
26829 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
26830 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26831 When point is less than `hscroll-margin' columns from the window
26832 edge, automatic hscrolling will scroll the window by the amount of columns
26833 determined by this variable. If its value is a positive integer, scroll that
26834 many columns. If it's a positive floating-point number, it specifies the
26835 fraction of the window's width to scroll. If it's nil or zero, point will be
26836 centered horizontally after the scroll. Any other value, including negative
26837 numbers, are treated as if the value were zero.
26838
26839 Automatic hscrolling always moves point outside the scroll margin, so if
26840 point was more than scroll step columns inside the margin, the window will
26841 scroll more than the value given by the scroll step.
26842
26843 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26844 and `scroll-right' overrides this variable's effect. */);
26845 Vhscroll_step = make_number (0);
26846
26847 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
26848 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26849 Bind this around calls to `message' to let it take effect. */);
26850 message_truncate_lines = 0;
26851
26852 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
26853 doc: /* Normal hook run to update the menu bar definitions.
26854 Redisplay runs this hook before it redisplays the menu bar.
26855 This is used to update submenus such as Buffers,
26856 whose contents depend on various data. */);
26857 Vmenu_bar_update_hook = Qnil;
26858
26859 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
26860 doc: /* Frame for which we are updating a menu.
26861 The enable predicate for a menu binding should check this variable. */);
26862 Vmenu_updating_frame = Qnil;
26863
26864 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
26865 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26866 inhibit_menubar_update = 0;
26867
26868 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
26869 doc: /* Prefix prepended to all continuation lines at display time.
26870 The value may be a string, an image, or a stretch-glyph; it is
26871 interpreted in the same way as the value of a `display' text property.
26872
26873 This variable is overridden by any `wrap-prefix' text or overlay
26874 property.
26875
26876 To add a prefix to non-continuation lines, use `line-prefix'. */);
26877 Vwrap_prefix = Qnil;
26878 staticpro (&Qwrap_prefix);
26879 Qwrap_prefix = intern_c_string ("wrap-prefix");
26880 Fmake_variable_buffer_local (Qwrap_prefix);
26881
26882 DEFVAR_LISP ("line-prefix", Vline_prefix,
26883 doc: /* Prefix prepended to all non-continuation lines at display time.
26884 The value may be a string, an image, or a stretch-glyph; it is
26885 interpreted in the same way as the value of a `display' text property.
26886
26887 This variable is overridden by any `line-prefix' text or overlay
26888 property.
26889
26890 To add a prefix to continuation lines, use `wrap-prefix'. */);
26891 Vline_prefix = Qnil;
26892 staticpro (&Qline_prefix);
26893 Qline_prefix = intern_c_string ("line-prefix");
26894 Fmake_variable_buffer_local (Qline_prefix);
26895
26896 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
26897 doc: /* Non-nil means don't eval Lisp during redisplay. */);
26898 inhibit_eval_during_redisplay = 0;
26899
26900 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
26901 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26902 inhibit_free_realized_faces = 0;
26903
26904 #if GLYPH_DEBUG
26905 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
26906 doc: /* Inhibit try_window_id display optimization. */);
26907 inhibit_try_window_id = 0;
26908
26909 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
26910 doc: /* Inhibit try_window_reusing display optimization. */);
26911 inhibit_try_window_reusing = 0;
26912
26913 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
26914 doc: /* Inhibit try_cursor_movement display optimization. */);
26915 inhibit_try_cursor_movement = 0;
26916 #endif /* GLYPH_DEBUG */
26917
26918 DEFVAR_INT ("overline-margin", overline_margin,
26919 doc: /* *Space between overline and text, in pixels.
26920 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26921 margin to the caracter height. */);
26922 overline_margin = 2;
26923
26924 DEFVAR_INT ("underline-minimum-offset",
26925 underline_minimum_offset,
26926 doc: /* Minimum distance between baseline and underline.
26927 This can improve legibility of underlined text at small font sizes,
26928 particularly when using variable `x-use-underline-position-properties'
26929 with fonts that specify an UNDERLINE_POSITION relatively close to the
26930 baseline. The default value is 1. */);
26931 underline_minimum_offset = 1;
26932
26933 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
26934 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
26935 This feature only works when on a window system that can change
26936 cursor shapes. */);
26937 display_hourglass_p = 1;
26938
26939 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
26940 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
26941 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26942
26943 hourglass_atimer = NULL;
26944 hourglass_shown_p = 0;
26945
26946 DEFSYM (Qglyphless_char, "glyphless-char");
26947 DEFSYM (Qhex_code, "hex-code");
26948 DEFSYM (Qempty_box, "empty-box");
26949 DEFSYM (Qthin_space, "thin-space");
26950 DEFSYM (Qzero_width, "zero-width");
26951
26952 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
26953 /* Intern this now in case it isn't already done.
26954 Setting this variable twice is harmless.
26955 But don't staticpro it here--that is done in alloc.c. */
26956 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
26957 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
26958
26959 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
26960 doc: /* Char-table to control displaying of glyphless characters.
26961 Each element, if non-nil, is an ASCII acronym string (displayed in a box)
26962 or one of these symbols:
26963 hex-code: display the hexadecimal code of a character in a box
26964 empty-box: display as an empty box
26965 thin-space: display as 1-pixel width space
26966 zero-width: don't display
26967
26968 It has one extra slot to control the display of a character for which
26969 no font is found. The value of the slot is `hex-code' or `empty-box'.
26970 The default is `empty-box'. */);
26971 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
26972 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
26973 Qempty_box);
26974 }
26975
26976
26977 /* Initialize this module when Emacs starts. */
26978
26979 void
26980 init_xdisp (void)
26981 {
26982 Lisp_Object root_window;
26983 struct window *mini_w;
26984
26985 current_header_line_height = current_mode_line_height = -1;
26986
26987 CHARPOS (this_line_start_pos) = 0;
26988
26989 mini_w = XWINDOW (minibuf_window);
26990 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
26991
26992 if (!noninteractive)
26993 {
26994 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
26995 int i;
26996
26997 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
26998 set_window_height (root_window,
26999 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27000 0);
27001 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27002 set_window_height (minibuf_window, 1, 0);
27003
27004 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27005 mini_w->total_cols = make_number (FRAME_COLS (f));
27006
27007 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27008 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27009 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27010
27011 /* The default ellipsis glyphs `...'. */
27012 for (i = 0; i < 3; ++i)
27013 default_invis_vector[i] = make_number ('.');
27014 }
27015
27016 {
27017 /* Allocate the buffer for frame titles.
27018 Also used for `format-mode-line'. */
27019 int size = 100;
27020 mode_line_noprop_buf = (char *) xmalloc (size);
27021 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27022 mode_line_noprop_ptr = mode_line_noprop_buf;
27023 mode_line_target = MODE_LINE_DISPLAY;
27024 }
27025
27026 help_echo_showing_p = 0;
27027 }
27028
27029 /* Since w32 does not support atimers, it defines its own implementation of
27030 the following three functions in w32fns.c. */
27031 #ifndef WINDOWSNT
27032
27033 /* Platform-independent portion of hourglass implementation. */
27034
27035 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27036 int
27037 hourglass_started (void)
27038 {
27039 return hourglass_shown_p || hourglass_atimer != NULL;
27040 }
27041
27042 /* Cancel a currently active hourglass timer, and start a new one. */
27043 void
27044 start_hourglass (void)
27045 {
27046 #if defined (HAVE_WINDOW_SYSTEM)
27047 EMACS_TIME delay;
27048 int secs, usecs = 0;
27049
27050 cancel_hourglass ();
27051
27052 if (INTEGERP (Vhourglass_delay)
27053 && XINT (Vhourglass_delay) > 0)
27054 secs = XFASTINT (Vhourglass_delay);
27055 else if (FLOATP (Vhourglass_delay)
27056 && XFLOAT_DATA (Vhourglass_delay) > 0)
27057 {
27058 Lisp_Object tem;
27059 tem = Ftruncate (Vhourglass_delay, Qnil);
27060 secs = XFASTINT (tem);
27061 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27062 }
27063 else
27064 secs = DEFAULT_HOURGLASS_DELAY;
27065
27066 EMACS_SET_SECS_USECS (delay, secs, usecs);
27067 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27068 show_hourglass, NULL);
27069 #endif
27070 }
27071
27072
27073 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27074 shown. */
27075 void
27076 cancel_hourglass (void)
27077 {
27078 #if defined (HAVE_WINDOW_SYSTEM)
27079 if (hourglass_atimer)
27080 {
27081 cancel_atimer (hourglass_atimer);
27082 hourglass_atimer = NULL;
27083 }
27084
27085 if (hourglass_shown_p)
27086 hide_hourglass ();
27087 #endif
27088 }
27089 #endif /* ! WINDOWSNT */