]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from emacs-23
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 2010 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code.
36
37 The following diagram shows how redisplay code is invoked. As you
38 can see, Lisp calls redisplay and vice versa. Under window systems
39 like X, some portions of the redisplay code are also called
40 asynchronously during mouse movement or expose events. It is very
41 important that these code parts do NOT use the C library (malloc,
42 free) because many C libraries under Unix are not reentrant. They
43 may also NOT call functions of the Lisp interpreter which could
44 change the interpreter's state. If you don't follow these rules,
45 you will encounter bugs which are very hard to explain.
46
47 +--------------+ redisplay +----------------+
48 | Lisp machine |---------------->| Redisplay code |<--+
49 +--------------+ (xdisp.c) +----------------+ |
50 ^ | |
51 +----------------------------------+ |
52 Don't use this path when called |
53 asynchronously! |
54 |
55 expose_window (asynchronous) |
56 |
57 X expose events -----+
58
59 What does redisplay do? Obviously, it has to figure out somehow what
60 has been changed since the last time the display has been updated,
61 and to make these changes visible. Preferably it would do that in
62 a moderately intelligent way, i.e. fast.
63
64 Changes in buffer text can be deduced from window and buffer
65 structures, and from some global variables like `beg_unchanged' and
66 `end_unchanged'. The contents of the display are additionally
67 recorded in a `glyph matrix', a two-dimensional matrix of glyph
68 structures. Each row in such a matrix corresponds to a line on the
69 display, and each glyph in a row corresponds to a column displaying
70 a character, an image, or what else. This matrix is called the
71 `current glyph matrix' or `current matrix' in redisplay
72 terminology.
73
74 For buffer parts that have been changed since the last update, a
75 second glyph matrix is constructed, the so called `desired glyph
76 matrix' or short `desired matrix'. Current and desired matrix are
77 then compared to find a cheap way to update the display, e.g. by
78 reusing part of the display by scrolling lines.
79
80 You will find a lot of redisplay optimizations when you start
81 looking at the innards of redisplay. The overall goal of all these
82 optimizations is to make redisplay fast because it is done
83 frequently. Some of these optimizations are implemented by the
84 following functions:
85
86 . try_cursor_movement
87
88 This function tries to update the display if the text in the
89 window did not change and did not scroll, only point moved, and
90 it did not move off the displayed portion of the text.
91
92 . try_window_reusing_current_matrix
93
94 This function reuses the current matrix of a window when text
95 has not changed, but the window start changed (e.g., due to
96 scrolling).
97
98 . try_window_id
99
100 This function attempts to redisplay a window by reusing parts of
101 its existing display. It finds and reuses the part that was not
102 changed, and redraws the rest.
103
104 . try_window
105
106 This function performs the full redisplay of a single window
107 assuming that its fonts were not changed and that the cursor
108 will not end up in the scroll margins. (Loading fonts requires
109 re-adjustment of dimensions of glyph matrices, which makes this
110 method impossible to use.)
111
112 These optimizations are tried in sequence (some can be skipped if
113 it is known that they are not applicable). If none of the
114 optimizations were successful, redisplay calls redisplay_windows,
115 which performs a full redisplay of all windows.
116
117 Desired matrices.
118
119 Desired matrices are always built per Emacs window. The function
120 `display_line' is the central function to look at if you are
121 interested. It constructs one row in a desired matrix given an
122 iterator structure containing both a buffer position and a
123 description of the environment in which the text is to be
124 displayed. But this is too early, read on.
125
126 Characters and pixmaps displayed for a range of buffer text depend
127 on various settings of buffers and windows, on overlays and text
128 properties, on display tables, on selective display. The good news
129 is that all this hairy stuff is hidden behind a small set of
130 interface functions taking an iterator structure (struct it)
131 argument.
132
133 Iteration over things to be displayed is then simple. It is
134 started by initializing an iterator with a call to init_iterator.
135 Calls to get_next_display_element fill the iterator structure with
136 relevant information about the next thing to display. Calls to
137 set_iterator_to_next move the iterator to the next thing.
138
139 Besides this, an iterator also contains information about the
140 display environment in which glyphs for display elements are to be
141 produced. It has fields for the width and height of the display,
142 the information whether long lines are truncated or continued, a
143 current X and Y position, and lots of other stuff you can better
144 see in dispextern.h.
145
146 Glyphs in a desired matrix are normally constructed in a loop
147 calling get_next_display_element and then PRODUCE_GLYPHS. The call
148 to PRODUCE_GLYPHS will fill the iterator structure with pixel
149 information about the element being displayed and at the same time
150 produce glyphs for it. If the display element fits on the line
151 being displayed, set_iterator_to_next is called next, otherwise the
152 glyphs produced are discarded. The function display_line is the
153 workhorse of filling glyph rows in the desired matrix with glyphs.
154 In addition to producing glyphs, it also handles line truncation
155 and continuation, word wrap, and cursor positioning (for the
156 latter, see also set_cursor_from_row).
157
158 Frame matrices.
159
160 That just couldn't be all, could it? What about terminal types not
161 supporting operations on sub-windows of the screen? To update the
162 display on such a terminal, window-based glyph matrices are not
163 well suited. To be able to reuse part of the display (scrolling
164 lines up and down), we must instead have a view of the whole
165 screen. This is what `frame matrices' are for. They are a trick.
166
167 Frames on terminals like above have a glyph pool. Windows on such
168 a frame sub-allocate their glyph memory from their frame's glyph
169 pool. The frame itself is given its own glyph matrices. By
170 coincidence---or maybe something else---rows in window glyph
171 matrices are slices of corresponding rows in frame matrices. Thus
172 writing to window matrices implicitly updates a frame matrix which
173 provides us with the view of the whole screen that we originally
174 wanted to have without having to move many bytes around. To be
175 honest, there is a little bit more done, but not much more. If you
176 plan to extend that code, take a look at dispnew.c. The function
177 build_frame_matrix is a good starting point.
178
179 Bidirectional display.
180
181 Bidirectional display adds quite some hair to this already complex
182 design. The good news are that a large portion of that hairy stuff
183 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
184 reordering engine which is called by set_iterator_to_next and
185 returns the next character to display in the visual order. See
186 commentary on bidi.c for more details. As far as redisplay is
187 concerned, the effect of calling bidi_move_to_visually_next, the
188 main interface of the reordering engine, is that the iterator gets
189 magically placed on the buffer or string position that is to be
190 displayed next. In other words, a linear iteration through the
191 buffer/string is replaced with a non-linear one. All the rest of
192 the redisplay is oblivious to the bidi reordering.
193
194 Well, almost oblivious---there are still complications, most of
195 them due to the fact that buffer and string positions no longer
196 change monotonously with glyph indices in a glyph row. Moreover,
197 for continued lines, the buffer positions may not even be
198 monotonously changing with vertical positions. Also, accounting
199 for face changes, overlays, etc. becomes more complex because
200 non-linear iteration could potentially skip many positions with
201 changes, and then cross them again on the way back...
202
203 One other prominent effect of bidirectional display is that some
204 paragraphs of text need to be displayed starting at the right
205 margin of the window---the so-called right-to-left, or R2L
206 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
207 which have their reversed_p flag set. The bidi reordering engine
208 produces characters in such rows starting from the character which
209 should be the rightmost on display. PRODUCE_GLYPHS then reverses
210 the order, when it fills up the glyph row whose reversed_p flag is
211 set, by prepending each new glyph to what is already there, instead
212 of appending it. When the glyph row is complete, the function
213 extend_face_to_end_of_line fills the empty space to the left of the
214 leftmost character with special glyphs, which will display as,
215 well, empty. On text terminals, these special glyphs are simply
216 blank characters. On graphics terminals, there's a single stretch
217 glyph of a suitably computed width. Both the blanks and the
218 stretch glyph are given the face of the background of the line.
219 This way, the terminal-specific back-end can still draw the glyphs
220 left to right, even for R2L lines.
221
222 Bidirectional display and character compositions
223
224 Some scripts cannot be displayed by drawing each character
225 individually, because adjacent characters change each other's shape
226 on display. For example, Arabic and Indic scripts belong to this
227 category.
228
229 Emacs display supports this by providing "character compositions",
230 most of which is implemented in composite.c. During the buffer
231 scan that delivers characters to PRODUCE_GLYPHS, if the next
232 character to be delivered is a composed character, the iteration
233 calls composition_reseat_it and next_element_from_composition. If
234 they succeed to compose the character with one or more of the
235 following characters, the whole sequence of characters that where
236 composed is recorded in the `struct composition_it' object that is
237 part of the buffer iterator. The composed sequence could produce
238 one or more font glyphs (called "grapheme clusters") on the screen.
239 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
240 in the direction corresponding to the current bidi scan direction
241 (recorded in the scan_dir member of the `struct bidi_it' object
242 that is part of the buffer iterator). In particular, if the bidi
243 iterator currently scans the buffer backwards, the grapheme
244 clusters are delivered back to front. This reorders the grapheme
245 clusters as appropriate for the current bidi context. Note that
246 this means that the grapheme clusters are always stored in the
247 LGSTRING object (see composite.c) in the logical order.
248
249 Moving an iterator in bidirectional text
250 without producing glyphs
251
252 Note one important detail mentioned above: that the bidi reordering
253 engine, driven by the iterator, produces characters in R2L rows
254 starting at the character that will be the rightmost on display.
255 As far as the iterator is concerned, the geometry of such rows is
256 still left to right, i.e. the iterator "thinks" the first character
257 is at the leftmost pixel position. The iterator does not know that
258 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
259 delivers. This is important when functions from the the move_it_*
260 family are used to get to certain screen position or to match
261 screen coordinates with buffer coordinates: these functions use the
262 iterator geometry, which is left to right even in R2L paragraphs.
263 This works well with most callers of move_it_*, because they need
264 to get to a specific column, and columns are still numbered in the
265 reading order, i.e. the rightmost character in a R2L paragraph is
266 still column zero. But some callers do not get well with this; a
267 notable example is mouse clicks that need to find the character
268 that corresponds to certain pixel coordinates. See
269 buffer_posn_from_coords in dispnew.c for how this is handled. */
270
271 #include <config.h>
272 #include <stdio.h>
273 #include <limits.h>
274 #include <setjmp.h>
275
276 #include "lisp.h"
277 #include "keyboard.h"
278 #include "frame.h"
279 #include "window.h"
280 #include "termchar.h"
281 #include "dispextern.h"
282 #include "buffer.h"
283 #include "character.h"
284 #include "charset.h"
285 #include "indent.h"
286 #include "commands.h"
287 #include "keymap.h"
288 #include "macros.h"
289 #include "disptab.h"
290 #include "termhooks.h"
291 #include "termopts.h"
292 #include "intervals.h"
293 #include "coding.h"
294 #include "process.h"
295 #include "region-cache.h"
296 #include "font.h"
297 #include "fontset.h"
298 #include "blockinput.h"
299
300 #ifdef HAVE_X_WINDOWS
301 #include "xterm.h"
302 #endif
303 #ifdef WINDOWSNT
304 #include "w32term.h"
305 #endif
306 #ifdef HAVE_NS
307 #include "nsterm.h"
308 #endif
309 #ifdef USE_GTK
310 #include "gtkutil.h"
311 #endif
312
313 #include "font.h"
314
315 #ifndef FRAME_X_OUTPUT
316 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
317 #endif
318
319 #define INFINITY 10000000
320
321 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
322 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
323 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
324 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
325 Lisp_Object Qinhibit_point_motion_hooks;
326 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
327 Lisp_Object Qfontified;
328 Lisp_Object Qgrow_only;
329 Lisp_Object Qinhibit_eval_during_redisplay;
330 Lisp_Object Qbuffer_position, Qposition, Qobject;
331 Lisp_Object Qright_to_left, Qleft_to_right;
332
333 /* Cursor shapes */
334 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
335
336 /* Pointer shapes */
337 Lisp_Object Qarrow, Qhand, Qtext;
338
339 Lisp_Object Qrisky_local_variable;
340
341 /* Holds the list (error). */
342 Lisp_Object list_of_error;
343
344 /* Functions called to fontify regions of text. */
345
346 Lisp_Object Vfontification_functions;
347 Lisp_Object Qfontification_functions;
348
349 /* Non-nil means automatically select any window when the mouse
350 cursor moves into it. */
351 Lisp_Object Vmouse_autoselect_window;
352
353 Lisp_Object Vwrap_prefix, Qwrap_prefix;
354 Lisp_Object Vline_prefix, Qline_prefix;
355
356 /* Non-zero means draw tool bar buttons raised when the mouse moves
357 over them. */
358
359 int auto_raise_tool_bar_buttons_p;
360
361 /* Non-zero means to reposition window if cursor line is only partially visible. */
362
363 int make_cursor_line_fully_visible_p;
364
365 /* Margin below tool bar in pixels. 0 or nil means no margin.
366 If value is `internal-border-width' or `border-width',
367 the corresponding frame parameter is used. */
368
369 Lisp_Object Vtool_bar_border;
370
371 /* Margin around tool bar buttons in pixels. */
372
373 Lisp_Object Vtool_bar_button_margin;
374
375 /* Thickness of shadow to draw around tool bar buttons. */
376
377 EMACS_INT tool_bar_button_relief;
378
379 /* Non-nil means automatically resize tool-bars so that all tool-bar
380 items are visible, and no blank lines remain.
381
382 If value is `grow-only', only make tool-bar bigger. */
383
384 Lisp_Object Vauto_resize_tool_bars;
385
386 /* Type of tool bar. Can be symbols image, text, both or both-hroiz. */
387
388 Lisp_Object Vtool_bar_style;
389
390 /* Maximum number of characters a label can have to be shown. */
391
392 EMACS_INT tool_bar_max_label_size;
393
394 /* Non-zero means draw block and hollow cursor as wide as the glyph
395 under it. For example, if a block cursor is over a tab, it will be
396 drawn as wide as that tab on the display. */
397
398 int x_stretch_cursor_p;
399
400 /* Non-nil means don't actually do any redisplay. */
401
402 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
403
404 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
405
406 int inhibit_eval_during_redisplay;
407
408 /* Names of text properties relevant for redisplay. */
409
410 Lisp_Object Qdisplay;
411
412 /* Symbols used in text property values. */
413
414 Lisp_Object Vdisplay_pixels_per_inch;
415 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
416 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
417 Lisp_Object Qslice;
418 Lisp_Object Qcenter;
419 Lisp_Object Qmargin, Qpointer;
420 Lisp_Object Qline_height;
421
422 /* Non-nil means highlight trailing whitespace. */
423
424 Lisp_Object Vshow_trailing_whitespace;
425
426 /* Non-nil means escape non-break space and hyphens. */
427
428 Lisp_Object Vnobreak_char_display;
429
430 #ifdef HAVE_WINDOW_SYSTEM
431
432 /* Test if overflow newline into fringe. Called with iterator IT
433 at or past right window margin, and with IT->current_x set. */
434
435 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
436 (!NILP (Voverflow_newline_into_fringe) \
437 && FRAME_WINDOW_P ((IT)->f) \
438 && ((IT)->bidi_it.paragraph_dir == R2L \
439 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
440 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
441 && (IT)->current_x == (IT)->last_visible_x \
442 && (IT)->line_wrap != WORD_WRAP)
443
444 #else /* !HAVE_WINDOW_SYSTEM */
445 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
446 #endif /* HAVE_WINDOW_SYSTEM */
447
448 /* Test if the display element loaded in IT is a space or tab
449 character. This is used to determine word wrapping. */
450
451 #define IT_DISPLAYING_WHITESPACE(it) \
452 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
453
454 /* Non-nil means show the text cursor in void text areas
455 i.e. in blank areas after eol and eob. This used to be
456 the default in 21.3. */
457
458 Lisp_Object Vvoid_text_area_pointer;
459
460 /* Name of the face used to highlight trailing whitespace. */
461
462 Lisp_Object Qtrailing_whitespace;
463
464 /* Name and number of the face used to highlight escape glyphs. */
465
466 Lisp_Object Qescape_glyph;
467
468 /* Name and number of the face used to highlight non-breaking spaces. */
469
470 Lisp_Object Qnobreak_space;
471
472 /* The symbol `image' which is the car of the lists used to represent
473 images in Lisp. Also a tool bar style. */
474
475 Lisp_Object Qimage;
476
477 /* The image map types. */
478 Lisp_Object QCmap, QCpointer;
479 Lisp_Object Qrect, Qcircle, Qpoly;
480
481 /* Tool bar styles */
482 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
483
484 /* Non-zero means print newline to stdout before next mini-buffer
485 message. */
486
487 int noninteractive_need_newline;
488
489 /* Non-zero means print newline to message log before next message. */
490
491 static int message_log_need_newline;
492
493 /* Three markers that message_dolog uses.
494 It could allocate them itself, but that causes trouble
495 in handling memory-full errors. */
496 static Lisp_Object message_dolog_marker1;
497 static Lisp_Object message_dolog_marker2;
498 static Lisp_Object message_dolog_marker3;
499 \f
500 /* The buffer position of the first character appearing entirely or
501 partially on the line of the selected window which contains the
502 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
503 redisplay optimization in redisplay_internal. */
504
505 static struct text_pos this_line_start_pos;
506
507 /* Number of characters past the end of the line above, including the
508 terminating newline. */
509
510 static struct text_pos this_line_end_pos;
511
512 /* The vertical positions and the height of this line. */
513
514 static int this_line_vpos;
515 static int this_line_y;
516 static int this_line_pixel_height;
517
518 /* X position at which this display line starts. Usually zero;
519 negative if first character is partially visible. */
520
521 static int this_line_start_x;
522
523 /* Buffer that this_line_.* variables are referring to. */
524
525 static struct buffer *this_line_buffer;
526
527 /* Nonzero means truncate lines in all windows less wide than the
528 frame. */
529
530 Lisp_Object Vtruncate_partial_width_windows;
531
532 /* A flag to control how to display unibyte 8-bit character. */
533
534 int unibyte_display_via_language_environment;
535
536 /* Nonzero means we have more than one non-mini-buffer-only frame.
537 Not guaranteed to be accurate except while parsing
538 frame-title-format. */
539
540 int multiple_frames;
541
542 Lisp_Object Vglobal_mode_string;
543
544
545 /* List of variables (symbols) which hold markers for overlay arrows.
546 The symbols on this list are examined during redisplay to determine
547 where to display overlay arrows. */
548
549 Lisp_Object Voverlay_arrow_variable_list;
550
551 /* Marker for where to display an arrow on top of the buffer text. */
552
553 Lisp_Object Voverlay_arrow_position;
554
555 /* String to display for the arrow. Only used on terminal frames. */
556
557 Lisp_Object Voverlay_arrow_string;
558
559 /* Values of those variables at last redisplay are stored as
560 properties on `overlay-arrow-position' symbol. However, if
561 Voverlay_arrow_position is a marker, last-arrow-position is its
562 numerical position. */
563
564 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
565
566 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
567 properties on a symbol in overlay-arrow-variable-list. */
568
569 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
570
571 /* Like mode-line-format, but for the title bar on a visible frame. */
572
573 Lisp_Object Vframe_title_format;
574
575 /* Like mode-line-format, but for the title bar on an iconified frame. */
576
577 Lisp_Object Vicon_title_format;
578
579 /* List of functions to call when a window's size changes. These
580 functions get one arg, a frame on which one or more windows' sizes
581 have changed. */
582
583 static Lisp_Object Vwindow_size_change_functions;
584
585 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
586
587 /* Nonzero if an overlay arrow has been displayed in this window. */
588
589 static int overlay_arrow_seen;
590
591 /* Nonzero means highlight the region even in nonselected windows. */
592
593 int highlight_nonselected_windows;
594
595 /* If cursor motion alone moves point off frame, try scrolling this
596 many lines up or down if that will bring it back. */
597
598 static EMACS_INT scroll_step;
599
600 /* Nonzero means scroll just far enough to bring point back on the
601 screen, when appropriate. */
602
603 static EMACS_INT scroll_conservatively;
604
605 /* Recenter the window whenever point gets within this many lines of
606 the top or bottom of the window. This value is translated into a
607 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
608 that there is really a fixed pixel height scroll margin. */
609
610 EMACS_INT scroll_margin;
611
612 /* Number of windows showing the buffer of the selected window (or
613 another buffer with the same base buffer). keyboard.c refers to
614 this. */
615
616 int buffer_shared;
617
618 /* Vector containing glyphs for an ellipsis `...'. */
619
620 static Lisp_Object default_invis_vector[3];
621
622 /* Zero means display the mode-line/header-line/menu-bar in the default face
623 (this slightly odd definition is for compatibility with previous versions
624 of emacs), non-zero means display them using their respective faces.
625
626 This variable is deprecated. */
627
628 int mode_line_inverse_video;
629
630 /* Prompt to display in front of the mini-buffer contents. */
631
632 Lisp_Object minibuf_prompt;
633
634 /* Width of current mini-buffer prompt. Only set after display_line
635 of the line that contains the prompt. */
636
637 int minibuf_prompt_width;
638
639 /* This is the window where the echo area message was displayed. It
640 is always a mini-buffer window, but it may not be the same window
641 currently active as a mini-buffer. */
642
643 Lisp_Object echo_area_window;
644
645 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
646 pushes the current message and the value of
647 message_enable_multibyte on the stack, the function restore_message
648 pops the stack and displays MESSAGE again. */
649
650 Lisp_Object Vmessage_stack;
651
652 /* Nonzero means multibyte characters were enabled when the echo area
653 message was specified. */
654
655 int message_enable_multibyte;
656
657 /* Nonzero if we should redraw the mode lines on the next redisplay. */
658
659 int update_mode_lines;
660
661 /* Nonzero if window sizes or contents have changed since last
662 redisplay that finished. */
663
664 int windows_or_buffers_changed;
665
666 /* Nonzero means a frame's cursor type has been changed. */
667
668 int cursor_type_changed;
669
670 /* Nonzero after display_mode_line if %l was used and it displayed a
671 line number. */
672
673 int line_number_displayed;
674
675 /* Maximum buffer size for which to display line numbers. */
676
677 Lisp_Object Vline_number_display_limit;
678
679 /* Line width to consider when repositioning for line number display. */
680
681 static EMACS_INT line_number_display_limit_width;
682
683 /* Number of lines to keep in the message log buffer. t means
684 infinite. nil means don't log at all. */
685
686 Lisp_Object Vmessage_log_max;
687
688 /* The name of the *Messages* buffer, a string. */
689
690 static Lisp_Object Vmessages_buffer_name;
691
692 /* Current, index 0, and last displayed echo area message. Either
693 buffers from echo_buffers, or nil to indicate no message. */
694
695 Lisp_Object echo_area_buffer[2];
696
697 /* The buffers referenced from echo_area_buffer. */
698
699 static Lisp_Object echo_buffer[2];
700
701 /* A vector saved used in with_area_buffer to reduce consing. */
702
703 static Lisp_Object Vwith_echo_area_save_vector;
704
705 /* Non-zero means display_echo_area should display the last echo area
706 message again. Set by redisplay_preserve_echo_area. */
707
708 static int display_last_displayed_message_p;
709
710 /* Nonzero if echo area is being used by print; zero if being used by
711 message. */
712
713 int message_buf_print;
714
715 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
716
717 Lisp_Object Qinhibit_menubar_update;
718 int inhibit_menubar_update;
719
720 /* When evaluating expressions from menu bar items (enable conditions,
721 for instance), this is the frame they are being processed for. */
722
723 Lisp_Object Vmenu_updating_frame;
724
725 /* Maximum height for resizing mini-windows. Either a float
726 specifying a fraction of the available height, or an integer
727 specifying a number of lines. */
728
729 Lisp_Object Vmax_mini_window_height;
730
731 /* Non-zero means messages should be displayed with truncated
732 lines instead of being continued. */
733
734 int message_truncate_lines;
735 Lisp_Object Qmessage_truncate_lines;
736
737 /* Set to 1 in clear_message to make redisplay_internal aware
738 of an emptied echo area. */
739
740 static int message_cleared_p;
741
742 /* How to blink the default frame cursor off. */
743 Lisp_Object Vblink_cursor_alist;
744
745 /* A scratch glyph row with contents used for generating truncation
746 glyphs. Also used in direct_output_for_insert. */
747
748 #define MAX_SCRATCH_GLYPHS 100
749 struct glyph_row scratch_glyph_row;
750 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
751
752 /* Ascent and height of the last line processed by move_it_to. */
753
754 static int last_max_ascent, last_height;
755
756 /* Non-zero if there's a help-echo in the echo area. */
757
758 int help_echo_showing_p;
759
760 /* If >= 0, computed, exact values of mode-line and header-line height
761 to use in the macros CURRENT_MODE_LINE_HEIGHT and
762 CURRENT_HEADER_LINE_HEIGHT. */
763
764 int current_mode_line_height, current_header_line_height;
765
766 /* The maximum distance to look ahead for text properties. Values
767 that are too small let us call compute_char_face and similar
768 functions too often which is expensive. Values that are too large
769 let us call compute_char_face and alike too often because we
770 might not be interested in text properties that far away. */
771
772 #define TEXT_PROP_DISTANCE_LIMIT 100
773
774 #if GLYPH_DEBUG
775
776 /* Variables to turn off display optimizations from Lisp. */
777
778 int inhibit_try_window_id, inhibit_try_window_reusing;
779 int inhibit_try_cursor_movement;
780
781 /* Non-zero means print traces of redisplay if compiled with
782 GLYPH_DEBUG != 0. */
783
784 int trace_redisplay_p;
785
786 #endif /* GLYPH_DEBUG */
787
788 #ifdef DEBUG_TRACE_MOVE
789 /* Non-zero means trace with TRACE_MOVE to stderr. */
790 int trace_move;
791
792 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
793 #else
794 #define TRACE_MOVE(x) (void) 0
795 #endif
796
797 /* Non-zero means automatically scroll windows horizontally to make
798 point visible. */
799
800 int automatic_hscrolling_p;
801 Lisp_Object Qauto_hscroll_mode;
802
803 /* How close to the margin can point get before the window is scrolled
804 horizontally. */
805 EMACS_INT hscroll_margin;
806
807 /* How much to scroll horizontally when point is inside the above margin. */
808 Lisp_Object Vhscroll_step;
809
810 /* The variable `resize-mini-windows'. If nil, don't resize
811 mini-windows. If t, always resize them to fit the text they
812 display. If `grow-only', let mini-windows grow only until they
813 become empty. */
814
815 Lisp_Object Vresize_mini_windows;
816
817 /* Buffer being redisplayed -- for redisplay_window_error. */
818
819 struct buffer *displayed_buffer;
820
821 /* Space between overline and text. */
822
823 EMACS_INT overline_margin;
824
825 /* Require underline to be at least this many screen pixels below baseline
826 This to avoid underline "merging" with the base of letters at small
827 font sizes, particularly when x_use_underline_position_properties is on. */
828
829 EMACS_INT underline_minimum_offset;
830
831 /* Value returned from text property handlers (see below). */
832
833 enum prop_handled
834 {
835 HANDLED_NORMALLY,
836 HANDLED_RECOMPUTE_PROPS,
837 HANDLED_OVERLAY_STRING_CONSUMED,
838 HANDLED_RETURN
839 };
840
841 /* A description of text properties that redisplay is interested
842 in. */
843
844 struct props
845 {
846 /* The name of the property. */
847 Lisp_Object *name;
848
849 /* A unique index for the property. */
850 enum prop_idx idx;
851
852 /* A handler function called to set up iterator IT from the property
853 at IT's current position. Value is used to steer handle_stop. */
854 enum prop_handled (*handler) (struct it *it);
855 };
856
857 static enum prop_handled handle_face_prop (struct it *);
858 static enum prop_handled handle_invisible_prop (struct it *);
859 static enum prop_handled handle_display_prop (struct it *);
860 static enum prop_handled handle_composition_prop (struct it *);
861 static enum prop_handled handle_overlay_change (struct it *);
862 static enum prop_handled handle_fontified_prop (struct it *);
863
864 /* Properties handled by iterators. */
865
866 static struct props it_props[] =
867 {
868 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
869 /* Handle `face' before `display' because some sub-properties of
870 `display' need to know the face. */
871 {&Qface, FACE_PROP_IDX, handle_face_prop},
872 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
873 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
874 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
875 {NULL, 0, NULL}
876 };
877
878 /* Value is the position described by X. If X is a marker, value is
879 the marker_position of X. Otherwise, value is X. */
880
881 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
882
883 /* Enumeration returned by some move_it_.* functions internally. */
884
885 enum move_it_result
886 {
887 /* Not used. Undefined value. */
888 MOVE_UNDEFINED,
889
890 /* Move ended at the requested buffer position or ZV. */
891 MOVE_POS_MATCH_OR_ZV,
892
893 /* Move ended at the requested X pixel position. */
894 MOVE_X_REACHED,
895
896 /* Move within a line ended at the end of a line that must be
897 continued. */
898 MOVE_LINE_CONTINUED,
899
900 /* Move within a line ended at the end of a line that would
901 be displayed truncated. */
902 MOVE_LINE_TRUNCATED,
903
904 /* Move within a line ended at a line end. */
905 MOVE_NEWLINE_OR_CR
906 };
907
908 /* This counter is used to clear the face cache every once in a while
909 in redisplay_internal. It is incremented for each redisplay.
910 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
911 cleared. */
912
913 #define CLEAR_FACE_CACHE_COUNT 500
914 static int clear_face_cache_count;
915
916 /* Similarly for the image cache. */
917
918 #ifdef HAVE_WINDOW_SYSTEM
919 #define CLEAR_IMAGE_CACHE_COUNT 101
920 static int clear_image_cache_count;
921
922 /* Null glyph slice */
923 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
924 #endif
925
926 /* Non-zero while redisplay_internal is in progress. */
927
928 int redisplaying_p;
929
930 /* Non-zero means don't free realized faces. Bound while freeing
931 realized faces is dangerous because glyph matrices might still
932 reference them. */
933
934 int inhibit_free_realized_faces;
935 Lisp_Object Qinhibit_free_realized_faces;
936
937 /* If a string, XTread_socket generates an event to display that string.
938 (The display is done in read_char.) */
939
940 Lisp_Object help_echo_string;
941 Lisp_Object help_echo_window;
942 Lisp_Object help_echo_object;
943 EMACS_INT help_echo_pos;
944
945 /* Temporary variable for XTread_socket. */
946
947 Lisp_Object previous_help_echo_string;
948
949 /* Platform-independent portion of hourglass implementation. */
950
951 /* Non-zero means we're allowed to display a hourglass pointer. */
952 int display_hourglass_p;
953
954 /* Non-zero means an hourglass cursor is currently shown. */
955 int hourglass_shown_p;
956
957 /* If non-null, an asynchronous timer that, when it expires, displays
958 an hourglass cursor on all frames. */
959 struct atimer *hourglass_atimer;
960
961 /* Number of seconds to wait before displaying an hourglass cursor. */
962 Lisp_Object Vhourglass_delay;
963
964 /* Name of the face used to display glyphless characters. */
965 Lisp_Object Qglyphless_char;
966
967 /* Char-table to control the display of glyphless characters. */
968 Lisp_Object Vglyphless_char_display;
969
970 /* Symbol for the purpose of Vglyphless_char_display. */
971 Lisp_Object Qglyphless_char_display;
972
973 /* Method symbols for Vglyphless_char_display. */
974 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
975
976 /* Default pixel width of `thin-space' display method. */
977 #define THIN_SPACE_WIDTH 1
978
979 /* Default number of seconds to wait before displaying an hourglass
980 cursor. */
981 #define DEFAULT_HOURGLASS_DELAY 1
982
983 \f
984 /* Function prototypes. */
985
986 static void setup_for_ellipsis (struct it *, int);
987 static void mark_window_display_accurate_1 (struct window *, int);
988 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
989 static int display_prop_string_p (Lisp_Object, Lisp_Object);
990 static int cursor_row_p (struct window *, struct glyph_row *);
991 static int redisplay_mode_lines (Lisp_Object, int);
992 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
993
994 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
995
996 static void handle_line_prefix (struct it *);
997
998 static void pint2str (char *, int, int);
999 static void pint2hrstr (char *, int, int);
1000 static struct text_pos run_window_scroll_functions (Lisp_Object,
1001 struct text_pos);
1002 static void reconsider_clip_changes (struct window *, struct buffer *);
1003 static int text_outside_line_unchanged_p (struct window *,
1004 EMACS_INT, EMACS_INT);
1005 static void store_mode_line_noprop_char (char);
1006 static int store_mode_line_noprop (const unsigned char *, int, int);
1007 static void handle_stop (struct it *);
1008 static void handle_stop_backwards (struct it *, EMACS_INT);
1009 static int single_display_spec_intangible_p (Lisp_Object);
1010 static void ensure_echo_area_buffers (void);
1011 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
1012 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
1013 static int with_echo_area_buffer (struct window *, int,
1014 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
1015 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1016 static void clear_garbaged_frames (void);
1017 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1018 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1019 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1020 static int display_echo_area (struct window *);
1021 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1022 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1023 static Lisp_Object unwind_redisplay (Lisp_Object);
1024 static int string_char_and_length (const unsigned char *, int *);
1025 static struct text_pos display_prop_end (struct it *, Lisp_Object,
1026 struct text_pos);
1027 static int compute_window_start_on_continuation_line (struct window *);
1028 static Lisp_Object safe_eval_handler (Lisp_Object);
1029 static void insert_left_trunc_glyphs (struct it *);
1030 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
1031 Lisp_Object);
1032 static void extend_face_to_end_of_line (struct it *);
1033 static int append_space_for_newline (struct it *, int);
1034 static int cursor_row_fully_visible_p (struct window *, int, int);
1035 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
1036 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
1037 static int trailing_whitespace_p (EMACS_INT);
1038 static int message_log_check_duplicate (EMACS_INT, EMACS_INT,
1039 EMACS_INT, EMACS_INT);
1040 static void push_it (struct it *);
1041 static void pop_it (struct it *);
1042 static void sync_frame_with_window_matrix_rows (struct window *);
1043 static void select_frame_for_redisplay (Lisp_Object);
1044 static void redisplay_internal (int);
1045 static int echo_area_display (int);
1046 static void redisplay_windows (Lisp_Object);
1047 static void redisplay_window (Lisp_Object, int);
1048 static Lisp_Object redisplay_window_error (Lisp_Object);
1049 static Lisp_Object redisplay_window_0 (Lisp_Object);
1050 static Lisp_Object redisplay_window_1 (Lisp_Object);
1051 static int update_menu_bar (struct frame *, int, int);
1052 static int try_window_reusing_current_matrix (struct window *);
1053 static int try_window_id (struct window *);
1054 static int display_line (struct it *);
1055 static int display_mode_lines (struct window *);
1056 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
1057 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
1058 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
1059 static const char *decode_mode_spec (struct window *, int, int, int,
1060 Lisp_Object *);
1061 static void display_menu_bar (struct window *);
1062 static int display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT, int,
1063 EMACS_INT *);
1064 static int display_string (const unsigned char *, Lisp_Object, Lisp_Object,
1065 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
1066 static void compute_line_metrics (struct it *);
1067 static void run_redisplay_end_trigger_hook (struct it *);
1068 static int get_overlay_strings (struct it *, EMACS_INT);
1069 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
1070 static void next_overlay_string (struct it *);
1071 static void reseat (struct it *, struct text_pos, int);
1072 static void reseat_1 (struct it *, struct text_pos, int);
1073 static void back_to_previous_visible_line_start (struct it *);
1074 void reseat_at_previous_visible_line_start (struct it *);
1075 static void reseat_at_next_visible_line_start (struct it *, int);
1076 static int next_element_from_ellipsis (struct it *);
1077 static int next_element_from_display_vector (struct it *);
1078 static int next_element_from_string (struct it *);
1079 static int next_element_from_c_string (struct it *);
1080 static int next_element_from_buffer (struct it *);
1081 static int next_element_from_composition (struct it *);
1082 static int next_element_from_image (struct it *);
1083 static int next_element_from_stretch (struct it *);
1084 static void load_overlay_strings (struct it *, EMACS_INT);
1085 static int init_from_display_pos (struct it *, struct window *,
1086 struct display_pos *);
1087 static void reseat_to_string (struct it *, const unsigned char *,
1088 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
1089 static enum move_it_result
1090 move_it_in_display_line_to (struct it *, EMACS_INT, int,
1091 enum move_operation_enum);
1092 void move_it_vertically_backward (struct it *, int);
1093 static void init_to_row_start (struct it *, struct window *,
1094 struct glyph_row *);
1095 static int init_to_row_end (struct it *, struct window *,
1096 struct glyph_row *);
1097 static void back_to_previous_line_start (struct it *);
1098 static int forward_to_next_line_start (struct it *, int *);
1099 static struct text_pos string_pos_nchars_ahead (struct text_pos,
1100 Lisp_Object, EMACS_INT);
1101 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
1102 static struct text_pos c_string_pos (EMACS_INT, const unsigned char *, int);
1103 static EMACS_INT number_of_chars (const unsigned char *, int);
1104 static void compute_stop_pos (struct it *);
1105 static void compute_string_pos (struct text_pos *, struct text_pos,
1106 Lisp_Object);
1107 static int face_before_or_after_it_pos (struct it *, int);
1108 static EMACS_INT next_overlay_change (EMACS_INT);
1109 static int handle_single_display_spec (struct it *, Lisp_Object,
1110 Lisp_Object, Lisp_Object,
1111 struct text_pos *, int);
1112 static int underlying_face_id (struct it *);
1113 static int in_ellipses_for_invisible_text_p (struct display_pos *,
1114 struct window *);
1115
1116 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1117 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1118
1119 #ifdef HAVE_WINDOW_SYSTEM
1120
1121 static void x_consider_frame_title (Lisp_Object);
1122 static int tool_bar_lines_needed (struct frame *, int *);
1123 static void update_tool_bar (struct frame *, int);
1124 static void build_desired_tool_bar_string (struct frame *f);
1125 static int redisplay_tool_bar (struct frame *);
1126 static void display_tool_bar_line (struct it *, int);
1127 static void notice_overwritten_cursor (struct window *,
1128 enum glyph_row_area,
1129 int, int, int, int);
1130 static void append_stretch_glyph (struct it *, Lisp_Object,
1131 int, int, int);
1132
1133
1134 #endif /* HAVE_WINDOW_SYSTEM */
1135
1136 static int coords_in_mouse_face_p (struct window *, int, int);
1137
1138
1139 \f
1140 /***********************************************************************
1141 Window display dimensions
1142 ***********************************************************************/
1143
1144 /* Return the bottom boundary y-position for text lines in window W.
1145 This is the first y position at which a line cannot start.
1146 It is relative to the top of the window.
1147
1148 This is the height of W minus the height of a mode line, if any. */
1149
1150 INLINE int
1151 window_text_bottom_y (struct window *w)
1152 {
1153 int height = WINDOW_TOTAL_HEIGHT (w);
1154
1155 if (WINDOW_WANTS_MODELINE_P (w))
1156 height -= CURRENT_MODE_LINE_HEIGHT (w);
1157 return height;
1158 }
1159
1160 /* Return the pixel width of display area AREA of window W. AREA < 0
1161 means return the total width of W, not including fringes to
1162 the left and right of the window. */
1163
1164 INLINE int
1165 window_box_width (struct window *w, int area)
1166 {
1167 int cols = XFASTINT (w->total_cols);
1168 int pixels = 0;
1169
1170 if (!w->pseudo_window_p)
1171 {
1172 cols -= WINDOW_SCROLL_BAR_COLS (w);
1173
1174 if (area == TEXT_AREA)
1175 {
1176 if (INTEGERP (w->left_margin_cols))
1177 cols -= XFASTINT (w->left_margin_cols);
1178 if (INTEGERP (w->right_margin_cols))
1179 cols -= XFASTINT (w->right_margin_cols);
1180 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1181 }
1182 else if (area == LEFT_MARGIN_AREA)
1183 {
1184 cols = (INTEGERP (w->left_margin_cols)
1185 ? XFASTINT (w->left_margin_cols) : 0);
1186 pixels = 0;
1187 }
1188 else if (area == RIGHT_MARGIN_AREA)
1189 {
1190 cols = (INTEGERP (w->right_margin_cols)
1191 ? XFASTINT (w->right_margin_cols) : 0);
1192 pixels = 0;
1193 }
1194 }
1195
1196 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1197 }
1198
1199
1200 /* Return the pixel height of the display area of window W, not
1201 including mode lines of W, if any. */
1202
1203 INLINE int
1204 window_box_height (struct window *w)
1205 {
1206 struct frame *f = XFRAME (w->frame);
1207 int height = WINDOW_TOTAL_HEIGHT (w);
1208
1209 xassert (height >= 0);
1210
1211 /* Note: the code below that determines the mode-line/header-line
1212 height is essentially the same as that contained in the macro
1213 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1214 the appropriate glyph row has its `mode_line_p' flag set,
1215 and if it doesn't, uses estimate_mode_line_height instead. */
1216
1217 if (WINDOW_WANTS_MODELINE_P (w))
1218 {
1219 struct glyph_row *ml_row
1220 = (w->current_matrix && w->current_matrix->rows
1221 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1222 : 0);
1223 if (ml_row && ml_row->mode_line_p)
1224 height -= ml_row->height;
1225 else
1226 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1227 }
1228
1229 if (WINDOW_WANTS_HEADER_LINE_P (w))
1230 {
1231 struct glyph_row *hl_row
1232 = (w->current_matrix && w->current_matrix->rows
1233 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1234 : 0);
1235 if (hl_row && hl_row->mode_line_p)
1236 height -= hl_row->height;
1237 else
1238 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1239 }
1240
1241 /* With a very small font and a mode-line that's taller than
1242 default, we might end up with a negative height. */
1243 return max (0, height);
1244 }
1245
1246 /* Return the window-relative coordinate of the left edge of display
1247 area AREA of window W. AREA < 0 means return the left edge of the
1248 whole window, to the right of the left fringe of W. */
1249
1250 INLINE int
1251 window_box_left_offset (struct window *w, int area)
1252 {
1253 int x;
1254
1255 if (w->pseudo_window_p)
1256 return 0;
1257
1258 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1259
1260 if (area == TEXT_AREA)
1261 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1262 + window_box_width (w, LEFT_MARGIN_AREA));
1263 else if (area == RIGHT_MARGIN_AREA)
1264 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1265 + window_box_width (w, LEFT_MARGIN_AREA)
1266 + window_box_width (w, TEXT_AREA)
1267 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1268 ? 0
1269 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1270 else if (area == LEFT_MARGIN_AREA
1271 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1272 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1273
1274 return x;
1275 }
1276
1277
1278 /* Return the window-relative coordinate of the right edge of display
1279 area AREA of window W. AREA < 0 means return the right edge of the
1280 whole window, to the left of the right fringe of W. */
1281
1282 INLINE int
1283 window_box_right_offset (struct window *w, int area)
1284 {
1285 return window_box_left_offset (w, area) + window_box_width (w, area);
1286 }
1287
1288 /* Return the frame-relative coordinate of the left edge of display
1289 area AREA of window W. AREA < 0 means return the left edge of the
1290 whole window, to the right of the left fringe of W. */
1291
1292 INLINE int
1293 window_box_left (struct window *w, int area)
1294 {
1295 struct frame *f = XFRAME (w->frame);
1296 int x;
1297
1298 if (w->pseudo_window_p)
1299 return FRAME_INTERNAL_BORDER_WIDTH (f);
1300
1301 x = (WINDOW_LEFT_EDGE_X (w)
1302 + window_box_left_offset (w, area));
1303
1304 return x;
1305 }
1306
1307
1308 /* Return the frame-relative coordinate of the right edge of display
1309 area AREA of window W. AREA < 0 means return the right edge of the
1310 whole window, to the left of the right fringe of W. */
1311
1312 INLINE int
1313 window_box_right (struct window *w, int area)
1314 {
1315 return window_box_left (w, area) + window_box_width (w, area);
1316 }
1317
1318 /* Get the bounding box of the display area AREA of window W, without
1319 mode lines, in frame-relative coordinates. AREA < 0 means the
1320 whole window, not including the left and right fringes of
1321 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1322 coordinates of the upper-left corner of the box. Return in
1323 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1324
1325 INLINE void
1326 window_box (struct window *w, int area, int *box_x, int *box_y,
1327 int *box_width, int *box_height)
1328 {
1329 if (box_width)
1330 *box_width = window_box_width (w, area);
1331 if (box_height)
1332 *box_height = window_box_height (w);
1333 if (box_x)
1334 *box_x = window_box_left (w, area);
1335 if (box_y)
1336 {
1337 *box_y = WINDOW_TOP_EDGE_Y (w);
1338 if (WINDOW_WANTS_HEADER_LINE_P (w))
1339 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1340 }
1341 }
1342
1343
1344 /* Get the bounding box of the display area AREA of window W, without
1345 mode lines. AREA < 0 means the whole window, not including the
1346 left and right fringe of the window. Return in *TOP_LEFT_X
1347 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1348 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1349 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1350 box. */
1351
1352 INLINE void
1353 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1354 int *bottom_right_x, int *bottom_right_y)
1355 {
1356 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1357 bottom_right_y);
1358 *bottom_right_x += *top_left_x;
1359 *bottom_right_y += *top_left_y;
1360 }
1361
1362
1363 \f
1364 /***********************************************************************
1365 Utilities
1366 ***********************************************************************/
1367
1368 /* Return the bottom y-position of the line the iterator IT is in.
1369 This can modify IT's settings. */
1370
1371 int
1372 line_bottom_y (struct it *it)
1373 {
1374 int line_height = it->max_ascent + it->max_descent;
1375 int line_top_y = it->current_y;
1376
1377 if (line_height == 0)
1378 {
1379 if (last_height)
1380 line_height = last_height;
1381 else if (IT_CHARPOS (*it) < ZV)
1382 {
1383 move_it_by_lines (it, 1, 1);
1384 line_height = (it->max_ascent || it->max_descent
1385 ? it->max_ascent + it->max_descent
1386 : last_height);
1387 }
1388 else
1389 {
1390 struct glyph_row *row = it->glyph_row;
1391
1392 /* Use the default character height. */
1393 it->glyph_row = NULL;
1394 it->what = IT_CHARACTER;
1395 it->c = ' ';
1396 it->len = 1;
1397 PRODUCE_GLYPHS (it);
1398 line_height = it->ascent + it->descent;
1399 it->glyph_row = row;
1400 }
1401 }
1402
1403 return line_top_y + line_height;
1404 }
1405
1406
1407 /* Return 1 if position CHARPOS is visible in window W.
1408 CHARPOS < 0 means return info about WINDOW_END position.
1409 If visible, set *X and *Y to pixel coordinates of top left corner.
1410 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1411 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1412
1413 int
1414 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1415 int *rtop, int *rbot, int *rowh, int *vpos)
1416 {
1417 struct it it;
1418 struct text_pos top;
1419 int visible_p = 0;
1420 struct buffer *old_buffer = NULL;
1421
1422 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1423 return visible_p;
1424
1425 if (XBUFFER (w->buffer) != current_buffer)
1426 {
1427 old_buffer = current_buffer;
1428 set_buffer_internal_1 (XBUFFER (w->buffer));
1429 }
1430
1431 SET_TEXT_POS_FROM_MARKER (top, w->start);
1432
1433 /* Compute exact mode line heights. */
1434 if (WINDOW_WANTS_MODELINE_P (w))
1435 current_mode_line_height
1436 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1437 current_buffer->mode_line_format);
1438
1439 if (WINDOW_WANTS_HEADER_LINE_P (w))
1440 current_header_line_height
1441 = display_mode_line (w, HEADER_LINE_FACE_ID,
1442 current_buffer->header_line_format);
1443
1444 start_display (&it, w, top);
1445 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1446 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1447
1448 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1449 {
1450 /* We have reached CHARPOS, or passed it. How the call to
1451 move_it_to can overshoot: (i) If CHARPOS is on invisible
1452 text, move_it_to stops at the end of the invisible text,
1453 after CHARPOS. (ii) If CHARPOS is in a display vector,
1454 move_it_to stops on its last glyph. */
1455 int top_x = it.current_x;
1456 int top_y = it.current_y;
1457 enum it_method it_method = it.method;
1458 /* Calling line_bottom_y may change it.method, it.position, etc. */
1459 int bottom_y = (last_height = 0, line_bottom_y (&it));
1460 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1461
1462 if (top_y < window_top_y)
1463 visible_p = bottom_y > window_top_y;
1464 else if (top_y < it.last_visible_y)
1465 visible_p = 1;
1466 if (visible_p)
1467 {
1468 if (it_method == GET_FROM_DISPLAY_VECTOR)
1469 {
1470 /* We stopped on the last glyph of a display vector.
1471 Try and recompute. Hack alert! */
1472 if (charpos < 2 || top.charpos >= charpos)
1473 top_x = it.glyph_row->x;
1474 else
1475 {
1476 struct it it2;
1477 start_display (&it2, w, top);
1478 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1479 get_next_display_element (&it2);
1480 PRODUCE_GLYPHS (&it2);
1481 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1482 || it2.current_x > it2.last_visible_x)
1483 top_x = it.glyph_row->x;
1484 else
1485 {
1486 top_x = it2.current_x;
1487 top_y = it2.current_y;
1488 }
1489 }
1490 }
1491
1492 *x = top_x;
1493 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1494 *rtop = max (0, window_top_y - top_y);
1495 *rbot = max (0, bottom_y - it.last_visible_y);
1496 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1497 - max (top_y, window_top_y)));
1498 *vpos = it.vpos;
1499 }
1500 }
1501 else
1502 {
1503 struct it it2;
1504
1505 it2 = it;
1506 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1507 move_it_by_lines (&it, 1, 0);
1508 if (charpos < IT_CHARPOS (it)
1509 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1510 {
1511 visible_p = 1;
1512 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1513 *x = it2.current_x;
1514 *y = it2.current_y + it2.max_ascent - it2.ascent;
1515 *rtop = max (0, -it2.current_y);
1516 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1517 - it.last_visible_y));
1518 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1519 it.last_visible_y)
1520 - max (it2.current_y,
1521 WINDOW_HEADER_LINE_HEIGHT (w))));
1522 *vpos = it2.vpos;
1523 }
1524 }
1525
1526 if (old_buffer)
1527 set_buffer_internal_1 (old_buffer);
1528
1529 current_header_line_height = current_mode_line_height = -1;
1530
1531 if (visible_p && XFASTINT (w->hscroll) > 0)
1532 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1533
1534 #if 0
1535 /* Debugging code. */
1536 if (visible_p)
1537 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1538 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1539 else
1540 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1541 #endif
1542
1543 return visible_p;
1544 }
1545
1546
1547 /* Return the next character from STR which is MAXLEN bytes long.
1548 Return in *LEN the length of the character. This is like
1549 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1550 we find one, we return a `?', but with the length of the invalid
1551 character. */
1552
1553 static INLINE int
1554 string_char_and_length (const unsigned char *str, int *len)
1555 {
1556 int c;
1557
1558 c = STRING_CHAR_AND_LENGTH (str, *len);
1559 if (!CHAR_VALID_P (c, 1))
1560 /* We may not change the length here because other places in Emacs
1561 don't use this function, i.e. they silently accept invalid
1562 characters. */
1563 c = '?';
1564
1565 return c;
1566 }
1567
1568
1569
1570 /* Given a position POS containing a valid character and byte position
1571 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1572
1573 static struct text_pos
1574 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1575 {
1576 xassert (STRINGP (string) && nchars >= 0);
1577
1578 if (STRING_MULTIBYTE (string))
1579 {
1580 EMACS_INT rest = SBYTES (string) - BYTEPOS (pos);
1581 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1582 int len;
1583
1584 while (nchars--)
1585 {
1586 string_char_and_length (p, &len);
1587 p += len, rest -= len;
1588 xassert (rest >= 0);
1589 CHARPOS (pos) += 1;
1590 BYTEPOS (pos) += len;
1591 }
1592 }
1593 else
1594 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1595
1596 return pos;
1597 }
1598
1599
1600 /* Value is the text position, i.e. character and byte position,
1601 for character position CHARPOS in STRING. */
1602
1603 static INLINE struct text_pos
1604 string_pos (EMACS_INT charpos, Lisp_Object string)
1605 {
1606 struct text_pos pos;
1607 xassert (STRINGP (string));
1608 xassert (charpos >= 0);
1609 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1610 return pos;
1611 }
1612
1613
1614 /* Value is a text position, i.e. character and byte position, for
1615 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1616 means recognize multibyte characters. */
1617
1618 static struct text_pos
1619 c_string_pos (EMACS_INT charpos, const unsigned char *s, int multibyte_p)
1620 {
1621 struct text_pos pos;
1622
1623 xassert (s != NULL);
1624 xassert (charpos >= 0);
1625
1626 if (multibyte_p)
1627 {
1628 EMACS_INT rest = strlen (s);
1629 int len;
1630
1631 SET_TEXT_POS (pos, 0, 0);
1632 while (charpos--)
1633 {
1634 string_char_and_length (s, &len);
1635 s += len, rest -= len;
1636 xassert (rest >= 0);
1637 CHARPOS (pos) += 1;
1638 BYTEPOS (pos) += len;
1639 }
1640 }
1641 else
1642 SET_TEXT_POS (pos, charpos, charpos);
1643
1644 return pos;
1645 }
1646
1647
1648 /* Value is the number of characters in C string S. MULTIBYTE_P
1649 non-zero means recognize multibyte characters. */
1650
1651 static EMACS_INT
1652 number_of_chars (const unsigned char *s, int multibyte_p)
1653 {
1654 EMACS_INT nchars;
1655
1656 if (multibyte_p)
1657 {
1658 EMACS_INT rest = strlen (s);
1659 int len;
1660 unsigned char *p = (unsigned char *) s;
1661
1662 for (nchars = 0; rest > 0; ++nchars)
1663 {
1664 string_char_and_length (p, &len);
1665 rest -= len, p += len;
1666 }
1667 }
1668 else
1669 nchars = strlen (s);
1670
1671 return nchars;
1672 }
1673
1674
1675 /* Compute byte position NEWPOS->bytepos corresponding to
1676 NEWPOS->charpos. POS is a known position in string STRING.
1677 NEWPOS->charpos must be >= POS.charpos. */
1678
1679 static void
1680 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1681 {
1682 xassert (STRINGP (string));
1683 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1684
1685 if (STRING_MULTIBYTE (string))
1686 *newpos = string_pos_nchars_ahead (pos, string,
1687 CHARPOS (*newpos) - CHARPOS (pos));
1688 else
1689 BYTEPOS (*newpos) = CHARPOS (*newpos);
1690 }
1691
1692 /* EXPORT:
1693 Return an estimation of the pixel height of mode or header lines on
1694 frame F. FACE_ID specifies what line's height to estimate. */
1695
1696 int
1697 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1698 {
1699 #ifdef HAVE_WINDOW_SYSTEM
1700 if (FRAME_WINDOW_P (f))
1701 {
1702 int height = FONT_HEIGHT (FRAME_FONT (f));
1703
1704 /* This function is called so early when Emacs starts that the face
1705 cache and mode line face are not yet initialized. */
1706 if (FRAME_FACE_CACHE (f))
1707 {
1708 struct face *face = FACE_FROM_ID (f, face_id);
1709 if (face)
1710 {
1711 if (face->font)
1712 height = FONT_HEIGHT (face->font);
1713 if (face->box_line_width > 0)
1714 height += 2 * face->box_line_width;
1715 }
1716 }
1717
1718 return height;
1719 }
1720 #endif
1721
1722 return 1;
1723 }
1724
1725 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1726 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1727 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1728 not force the value into range. */
1729
1730 void
1731 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1732 int *x, int *y, NativeRectangle *bounds, int noclip)
1733 {
1734
1735 #ifdef HAVE_WINDOW_SYSTEM
1736 if (FRAME_WINDOW_P (f))
1737 {
1738 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1739 even for negative values. */
1740 if (pix_x < 0)
1741 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1742 if (pix_y < 0)
1743 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1744
1745 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1746 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1747
1748 if (bounds)
1749 STORE_NATIVE_RECT (*bounds,
1750 FRAME_COL_TO_PIXEL_X (f, pix_x),
1751 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1752 FRAME_COLUMN_WIDTH (f) - 1,
1753 FRAME_LINE_HEIGHT (f) - 1);
1754
1755 if (!noclip)
1756 {
1757 if (pix_x < 0)
1758 pix_x = 0;
1759 else if (pix_x > FRAME_TOTAL_COLS (f))
1760 pix_x = FRAME_TOTAL_COLS (f);
1761
1762 if (pix_y < 0)
1763 pix_y = 0;
1764 else if (pix_y > FRAME_LINES (f))
1765 pix_y = FRAME_LINES (f);
1766 }
1767 }
1768 #endif
1769
1770 *x = pix_x;
1771 *y = pix_y;
1772 }
1773
1774
1775 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1776 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1777 can't tell the positions because W's display is not up to date,
1778 return 0. */
1779
1780 int
1781 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1782 int *frame_x, int *frame_y)
1783 {
1784 #ifdef HAVE_WINDOW_SYSTEM
1785 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1786 {
1787 int success_p;
1788
1789 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1790 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1791
1792 if (display_completed)
1793 {
1794 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1795 struct glyph *glyph = row->glyphs[TEXT_AREA];
1796 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1797
1798 hpos = row->x;
1799 vpos = row->y;
1800 while (glyph < end)
1801 {
1802 hpos += glyph->pixel_width;
1803 ++glyph;
1804 }
1805
1806 /* If first glyph is partially visible, its first visible position is still 0. */
1807 if (hpos < 0)
1808 hpos = 0;
1809
1810 success_p = 1;
1811 }
1812 else
1813 {
1814 hpos = vpos = 0;
1815 success_p = 0;
1816 }
1817
1818 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1819 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1820 return success_p;
1821 }
1822 #endif
1823
1824 *frame_x = hpos;
1825 *frame_y = vpos;
1826 return 1;
1827 }
1828
1829
1830 /* Find the glyph under window-relative coordinates X/Y in window W.
1831 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1832 strings. Return in *HPOS and *VPOS the row and column number of
1833 the glyph found. Return in *AREA the glyph area containing X.
1834 Value is a pointer to the glyph found or null if X/Y is not on
1835 text, or we can't tell because W's current matrix is not up to
1836 date. */
1837
1838 static
1839 struct glyph *
1840 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1841 int *dx, int *dy, int *area)
1842 {
1843 struct glyph *glyph, *end;
1844 struct glyph_row *row = NULL;
1845 int x0, i;
1846
1847 /* Find row containing Y. Give up if some row is not enabled. */
1848 for (i = 0; i < w->current_matrix->nrows; ++i)
1849 {
1850 row = MATRIX_ROW (w->current_matrix, i);
1851 if (!row->enabled_p)
1852 return NULL;
1853 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1854 break;
1855 }
1856
1857 *vpos = i;
1858 *hpos = 0;
1859
1860 /* Give up if Y is not in the window. */
1861 if (i == w->current_matrix->nrows)
1862 return NULL;
1863
1864 /* Get the glyph area containing X. */
1865 if (w->pseudo_window_p)
1866 {
1867 *area = TEXT_AREA;
1868 x0 = 0;
1869 }
1870 else
1871 {
1872 if (x < window_box_left_offset (w, TEXT_AREA))
1873 {
1874 *area = LEFT_MARGIN_AREA;
1875 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1876 }
1877 else if (x < window_box_right_offset (w, TEXT_AREA))
1878 {
1879 *area = TEXT_AREA;
1880 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1881 }
1882 else
1883 {
1884 *area = RIGHT_MARGIN_AREA;
1885 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1886 }
1887 }
1888
1889 /* Find glyph containing X. */
1890 glyph = row->glyphs[*area];
1891 end = glyph + row->used[*area];
1892 x -= x0;
1893 while (glyph < end && x >= glyph->pixel_width)
1894 {
1895 x -= glyph->pixel_width;
1896 ++glyph;
1897 }
1898
1899 if (glyph == end)
1900 return NULL;
1901
1902 if (dx)
1903 {
1904 *dx = x;
1905 *dy = y - (row->y + row->ascent - glyph->ascent);
1906 }
1907
1908 *hpos = glyph - row->glyphs[*area];
1909 return glyph;
1910 }
1911
1912 /* EXPORT:
1913 Convert frame-relative x/y to coordinates relative to window W.
1914 Takes pseudo-windows into account. */
1915
1916 void
1917 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1918 {
1919 if (w->pseudo_window_p)
1920 {
1921 /* A pseudo-window is always full-width, and starts at the
1922 left edge of the frame, plus a frame border. */
1923 struct frame *f = XFRAME (w->frame);
1924 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1925 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1926 }
1927 else
1928 {
1929 *x -= WINDOW_LEFT_EDGE_X (w);
1930 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1931 }
1932 }
1933
1934 #ifdef HAVE_WINDOW_SYSTEM
1935
1936 /* EXPORT:
1937 Return in RECTS[] at most N clipping rectangles for glyph string S.
1938 Return the number of stored rectangles. */
1939
1940 int
1941 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1942 {
1943 XRectangle r;
1944
1945 if (n <= 0)
1946 return 0;
1947
1948 if (s->row->full_width_p)
1949 {
1950 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1951 r.x = WINDOW_LEFT_EDGE_X (s->w);
1952 r.width = WINDOW_TOTAL_WIDTH (s->w);
1953
1954 /* Unless displaying a mode or menu bar line, which are always
1955 fully visible, clip to the visible part of the row. */
1956 if (s->w->pseudo_window_p)
1957 r.height = s->row->visible_height;
1958 else
1959 r.height = s->height;
1960 }
1961 else
1962 {
1963 /* This is a text line that may be partially visible. */
1964 r.x = window_box_left (s->w, s->area);
1965 r.width = window_box_width (s->w, s->area);
1966 r.height = s->row->visible_height;
1967 }
1968
1969 if (s->clip_head)
1970 if (r.x < s->clip_head->x)
1971 {
1972 if (r.width >= s->clip_head->x - r.x)
1973 r.width -= s->clip_head->x - r.x;
1974 else
1975 r.width = 0;
1976 r.x = s->clip_head->x;
1977 }
1978 if (s->clip_tail)
1979 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1980 {
1981 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1982 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1983 else
1984 r.width = 0;
1985 }
1986
1987 /* If S draws overlapping rows, it's sufficient to use the top and
1988 bottom of the window for clipping because this glyph string
1989 intentionally draws over other lines. */
1990 if (s->for_overlaps)
1991 {
1992 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1993 r.height = window_text_bottom_y (s->w) - r.y;
1994
1995 /* Alas, the above simple strategy does not work for the
1996 environments with anti-aliased text: if the same text is
1997 drawn onto the same place multiple times, it gets thicker.
1998 If the overlap we are processing is for the erased cursor, we
1999 take the intersection with the rectagle of the cursor. */
2000 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2001 {
2002 XRectangle rc, r_save = r;
2003
2004 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2005 rc.y = s->w->phys_cursor.y;
2006 rc.width = s->w->phys_cursor_width;
2007 rc.height = s->w->phys_cursor_height;
2008
2009 x_intersect_rectangles (&r_save, &rc, &r);
2010 }
2011 }
2012 else
2013 {
2014 /* Don't use S->y for clipping because it doesn't take partially
2015 visible lines into account. For example, it can be negative for
2016 partially visible lines at the top of a window. */
2017 if (!s->row->full_width_p
2018 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2019 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2020 else
2021 r.y = max (0, s->row->y);
2022 }
2023
2024 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2025
2026 /* If drawing the cursor, don't let glyph draw outside its
2027 advertised boundaries. Cleartype does this under some circumstances. */
2028 if (s->hl == DRAW_CURSOR)
2029 {
2030 struct glyph *glyph = s->first_glyph;
2031 int height, max_y;
2032
2033 if (s->x > r.x)
2034 {
2035 r.width -= s->x - r.x;
2036 r.x = s->x;
2037 }
2038 r.width = min (r.width, glyph->pixel_width);
2039
2040 /* If r.y is below window bottom, ensure that we still see a cursor. */
2041 height = min (glyph->ascent + glyph->descent,
2042 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2043 max_y = window_text_bottom_y (s->w) - height;
2044 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2045 if (s->ybase - glyph->ascent > max_y)
2046 {
2047 r.y = max_y;
2048 r.height = height;
2049 }
2050 else
2051 {
2052 /* Don't draw cursor glyph taller than our actual glyph. */
2053 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2054 if (height < r.height)
2055 {
2056 max_y = r.y + r.height;
2057 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2058 r.height = min (max_y - r.y, height);
2059 }
2060 }
2061 }
2062
2063 if (s->row->clip)
2064 {
2065 XRectangle r_save = r;
2066
2067 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2068 r.width = 0;
2069 }
2070
2071 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2072 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2073 {
2074 #ifdef CONVERT_FROM_XRECT
2075 CONVERT_FROM_XRECT (r, *rects);
2076 #else
2077 *rects = r;
2078 #endif
2079 return 1;
2080 }
2081 else
2082 {
2083 /* If we are processing overlapping and allowed to return
2084 multiple clipping rectangles, we exclude the row of the glyph
2085 string from the clipping rectangle. This is to avoid drawing
2086 the same text on the environment with anti-aliasing. */
2087 #ifdef CONVERT_FROM_XRECT
2088 XRectangle rs[2];
2089 #else
2090 XRectangle *rs = rects;
2091 #endif
2092 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2093
2094 if (s->for_overlaps & OVERLAPS_PRED)
2095 {
2096 rs[i] = r;
2097 if (r.y + r.height > row_y)
2098 {
2099 if (r.y < row_y)
2100 rs[i].height = row_y - r.y;
2101 else
2102 rs[i].height = 0;
2103 }
2104 i++;
2105 }
2106 if (s->for_overlaps & OVERLAPS_SUCC)
2107 {
2108 rs[i] = r;
2109 if (r.y < row_y + s->row->visible_height)
2110 {
2111 if (r.y + r.height > row_y + s->row->visible_height)
2112 {
2113 rs[i].y = row_y + s->row->visible_height;
2114 rs[i].height = r.y + r.height - rs[i].y;
2115 }
2116 else
2117 rs[i].height = 0;
2118 }
2119 i++;
2120 }
2121
2122 n = i;
2123 #ifdef CONVERT_FROM_XRECT
2124 for (i = 0; i < n; i++)
2125 CONVERT_FROM_XRECT (rs[i], rects[i]);
2126 #endif
2127 return n;
2128 }
2129 }
2130
2131 /* EXPORT:
2132 Return in *NR the clipping rectangle for glyph string S. */
2133
2134 void
2135 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2136 {
2137 get_glyph_string_clip_rects (s, nr, 1);
2138 }
2139
2140
2141 /* EXPORT:
2142 Return the position and height of the phys cursor in window W.
2143 Set w->phys_cursor_width to width of phys cursor.
2144 */
2145
2146 void
2147 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2148 struct glyph *glyph, int *xp, int *yp, int *heightp)
2149 {
2150 struct frame *f = XFRAME (WINDOW_FRAME (w));
2151 int x, y, wd, h, h0, y0;
2152
2153 /* Compute the width of the rectangle to draw. If on a stretch
2154 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2155 rectangle as wide as the glyph, but use a canonical character
2156 width instead. */
2157 wd = glyph->pixel_width - 1;
2158 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2159 wd++; /* Why? */
2160 #endif
2161
2162 x = w->phys_cursor.x;
2163 if (x < 0)
2164 {
2165 wd += x;
2166 x = 0;
2167 }
2168
2169 if (glyph->type == STRETCH_GLYPH
2170 && !x_stretch_cursor_p)
2171 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2172 w->phys_cursor_width = wd;
2173
2174 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2175
2176 /* If y is below window bottom, ensure that we still see a cursor. */
2177 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2178
2179 h = max (h0, glyph->ascent + glyph->descent);
2180 h0 = min (h0, glyph->ascent + glyph->descent);
2181
2182 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2183 if (y < y0)
2184 {
2185 h = max (h - (y0 - y) + 1, h0);
2186 y = y0 - 1;
2187 }
2188 else
2189 {
2190 y0 = window_text_bottom_y (w) - h0;
2191 if (y > y0)
2192 {
2193 h += y - y0;
2194 y = y0;
2195 }
2196 }
2197
2198 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2199 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2200 *heightp = h;
2201 }
2202
2203 /*
2204 * Remember which glyph the mouse is over.
2205 */
2206
2207 void
2208 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2209 {
2210 Lisp_Object window;
2211 struct window *w;
2212 struct glyph_row *r, *gr, *end_row;
2213 enum window_part part;
2214 enum glyph_row_area area;
2215 int x, y, width, height;
2216
2217 /* Try to determine frame pixel position and size of the glyph under
2218 frame pixel coordinates X/Y on frame F. */
2219
2220 if (!f->glyphs_initialized_p
2221 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2222 NILP (window)))
2223 {
2224 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2225 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2226 goto virtual_glyph;
2227 }
2228
2229 w = XWINDOW (window);
2230 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2231 height = WINDOW_FRAME_LINE_HEIGHT (w);
2232
2233 x = window_relative_x_coord (w, part, gx);
2234 y = gy - WINDOW_TOP_EDGE_Y (w);
2235
2236 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2237 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2238
2239 if (w->pseudo_window_p)
2240 {
2241 area = TEXT_AREA;
2242 part = ON_MODE_LINE; /* Don't adjust margin. */
2243 goto text_glyph;
2244 }
2245
2246 switch (part)
2247 {
2248 case ON_LEFT_MARGIN:
2249 area = LEFT_MARGIN_AREA;
2250 goto text_glyph;
2251
2252 case ON_RIGHT_MARGIN:
2253 area = RIGHT_MARGIN_AREA;
2254 goto text_glyph;
2255
2256 case ON_HEADER_LINE:
2257 case ON_MODE_LINE:
2258 gr = (part == ON_HEADER_LINE
2259 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2260 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2261 gy = gr->y;
2262 area = TEXT_AREA;
2263 goto text_glyph_row_found;
2264
2265 case ON_TEXT:
2266 area = TEXT_AREA;
2267
2268 text_glyph:
2269 gr = 0; gy = 0;
2270 for (; r <= end_row && r->enabled_p; ++r)
2271 if (r->y + r->height > y)
2272 {
2273 gr = r; gy = r->y;
2274 break;
2275 }
2276
2277 text_glyph_row_found:
2278 if (gr && gy <= y)
2279 {
2280 struct glyph *g = gr->glyphs[area];
2281 struct glyph *end = g + gr->used[area];
2282
2283 height = gr->height;
2284 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2285 if (gx + g->pixel_width > x)
2286 break;
2287
2288 if (g < end)
2289 {
2290 if (g->type == IMAGE_GLYPH)
2291 {
2292 /* Don't remember when mouse is over image, as
2293 image may have hot-spots. */
2294 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2295 return;
2296 }
2297 width = g->pixel_width;
2298 }
2299 else
2300 {
2301 /* Use nominal char spacing at end of line. */
2302 x -= gx;
2303 gx += (x / width) * width;
2304 }
2305
2306 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2307 gx += window_box_left_offset (w, area);
2308 }
2309 else
2310 {
2311 /* Use nominal line height at end of window. */
2312 gx = (x / width) * width;
2313 y -= gy;
2314 gy += (y / height) * height;
2315 }
2316 break;
2317
2318 case ON_LEFT_FRINGE:
2319 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2320 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2321 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2322 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2323 goto row_glyph;
2324
2325 case ON_RIGHT_FRINGE:
2326 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2327 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2328 : window_box_right_offset (w, TEXT_AREA));
2329 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2330 goto row_glyph;
2331
2332 case ON_SCROLL_BAR:
2333 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2334 ? 0
2335 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2336 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2337 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2338 : 0)));
2339 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2340
2341 row_glyph:
2342 gr = 0, gy = 0;
2343 for (; r <= end_row && r->enabled_p; ++r)
2344 if (r->y + r->height > y)
2345 {
2346 gr = r; gy = r->y;
2347 break;
2348 }
2349
2350 if (gr && gy <= y)
2351 height = gr->height;
2352 else
2353 {
2354 /* Use nominal line height at end of window. */
2355 y -= gy;
2356 gy += (y / height) * height;
2357 }
2358 break;
2359
2360 default:
2361 ;
2362 virtual_glyph:
2363 /* If there is no glyph under the mouse, then we divide the screen
2364 into a grid of the smallest glyph in the frame, and use that
2365 as our "glyph". */
2366
2367 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2368 round down even for negative values. */
2369 if (gx < 0)
2370 gx -= width - 1;
2371 if (gy < 0)
2372 gy -= height - 1;
2373
2374 gx = (gx / width) * width;
2375 gy = (gy / height) * height;
2376
2377 goto store_rect;
2378 }
2379
2380 gx += WINDOW_LEFT_EDGE_X (w);
2381 gy += WINDOW_TOP_EDGE_Y (w);
2382
2383 store_rect:
2384 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2385
2386 /* Visible feedback for debugging. */
2387 #if 0
2388 #if HAVE_X_WINDOWS
2389 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2390 f->output_data.x->normal_gc,
2391 gx, gy, width, height);
2392 #endif
2393 #endif
2394 }
2395
2396
2397 #endif /* HAVE_WINDOW_SYSTEM */
2398
2399 \f
2400 /***********************************************************************
2401 Lisp form evaluation
2402 ***********************************************************************/
2403
2404 /* Error handler for safe_eval and safe_call. */
2405
2406 static Lisp_Object
2407 safe_eval_handler (Lisp_Object arg)
2408 {
2409 add_to_log ("Error during redisplay: %s", arg, Qnil);
2410 return Qnil;
2411 }
2412
2413
2414 /* Evaluate SEXPR and return the result, or nil if something went
2415 wrong. Prevent redisplay during the evaluation. */
2416
2417 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2418 Return the result, or nil if something went wrong. Prevent
2419 redisplay during the evaluation. */
2420
2421 Lisp_Object
2422 safe_call (int nargs, Lisp_Object *args)
2423 {
2424 Lisp_Object val;
2425
2426 if (inhibit_eval_during_redisplay)
2427 val = Qnil;
2428 else
2429 {
2430 int count = SPECPDL_INDEX ();
2431 struct gcpro gcpro1;
2432
2433 GCPRO1 (args[0]);
2434 gcpro1.nvars = nargs;
2435 specbind (Qinhibit_redisplay, Qt);
2436 /* Use Qt to ensure debugger does not run,
2437 so there is no possibility of wanting to redisplay. */
2438 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2439 safe_eval_handler);
2440 UNGCPRO;
2441 val = unbind_to (count, val);
2442 }
2443
2444 return val;
2445 }
2446
2447
2448 /* Call function FN with one argument ARG.
2449 Return the result, or nil if something went wrong. */
2450
2451 Lisp_Object
2452 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2453 {
2454 Lisp_Object args[2];
2455 args[0] = fn;
2456 args[1] = arg;
2457 return safe_call (2, args);
2458 }
2459
2460 static Lisp_Object Qeval;
2461
2462 Lisp_Object
2463 safe_eval (Lisp_Object sexpr)
2464 {
2465 return safe_call1 (Qeval, sexpr);
2466 }
2467
2468 /* Call function FN with one argument ARG.
2469 Return the result, or nil if something went wrong. */
2470
2471 Lisp_Object
2472 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2473 {
2474 Lisp_Object args[3];
2475 args[0] = fn;
2476 args[1] = arg1;
2477 args[2] = arg2;
2478 return safe_call (3, args);
2479 }
2480
2481
2482 \f
2483 /***********************************************************************
2484 Debugging
2485 ***********************************************************************/
2486
2487 #if 0
2488
2489 /* Define CHECK_IT to perform sanity checks on iterators.
2490 This is for debugging. It is too slow to do unconditionally. */
2491
2492 static void
2493 check_it (it)
2494 struct it *it;
2495 {
2496 if (it->method == GET_FROM_STRING)
2497 {
2498 xassert (STRINGP (it->string));
2499 xassert (IT_STRING_CHARPOS (*it) >= 0);
2500 }
2501 else
2502 {
2503 xassert (IT_STRING_CHARPOS (*it) < 0);
2504 if (it->method == GET_FROM_BUFFER)
2505 {
2506 /* Check that character and byte positions agree. */
2507 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2508 }
2509 }
2510
2511 if (it->dpvec)
2512 xassert (it->current.dpvec_index >= 0);
2513 else
2514 xassert (it->current.dpvec_index < 0);
2515 }
2516
2517 #define CHECK_IT(IT) check_it ((IT))
2518
2519 #else /* not 0 */
2520
2521 #define CHECK_IT(IT) (void) 0
2522
2523 #endif /* not 0 */
2524
2525
2526 #if GLYPH_DEBUG
2527
2528 /* Check that the window end of window W is what we expect it
2529 to be---the last row in the current matrix displaying text. */
2530
2531 static void
2532 check_window_end (w)
2533 struct window *w;
2534 {
2535 if (!MINI_WINDOW_P (w)
2536 && !NILP (w->window_end_valid))
2537 {
2538 struct glyph_row *row;
2539 xassert ((row = MATRIX_ROW (w->current_matrix,
2540 XFASTINT (w->window_end_vpos)),
2541 !row->enabled_p
2542 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2543 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2544 }
2545 }
2546
2547 #define CHECK_WINDOW_END(W) check_window_end ((W))
2548
2549 #else /* not GLYPH_DEBUG */
2550
2551 #define CHECK_WINDOW_END(W) (void) 0
2552
2553 #endif /* not GLYPH_DEBUG */
2554
2555
2556 \f
2557 /***********************************************************************
2558 Iterator initialization
2559 ***********************************************************************/
2560
2561 /* Initialize IT for displaying current_buffer in window W, starting
2562 at character position CHARPOS. CHARPOS < 0 means that no buffer
2563 position is specified which is useful when the iterator is assigned
2564 a position later. BYTEPOS is the byte position corresponding to
2565 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2566
2567 If ROW is not null, calls to produce_glyphs with IT as parameter
2568 will produce glyphs in that row.
2569
2570 BASE_FACE_ID is the id of a base face to use. It must be one of
2571 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2572 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2573 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2574
2575 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2576 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2577 will be initialized to use the corresponding mode line glyph row of
2578 the desired matrix of W. */
2579
2580 void
2581 init_iterator (struct it *it, struct window *w,
2582 EMACS_INT charpos, EMACS_INT bytepos,
2583 struct glyph_row *row, enum face_id base_face_id)
2584 {
2585 int highlight_region_p;
2586 enum face_id remapped_base_face_id = base_face_id;
2587
2588 /* Some precondition checks. */
2589 xassert (w != NULL && it != NULL);
2590 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2591 && charpos <= ZV));
2592
2593 /* If face attributes have been changed since the last redisplay,
2594 free realized faces now because they depend on face definitions
2595 that might have changed. Don't free faces while there might be
2596 desired matrices pending which reference these faces. */
2597 if (face_change_count && !inhibit_free_realized_faces)
2598 {
2599 face_change_count = 0;
2600 free_all_realized_faces (Qnil);
2601 }
2602
2603 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2604 if (! NILP (Vface_remapping_alist))
2605 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2606
2607 /* Use one of the mode line rows of W's desired matrix if
2608 appropriate. */
2609 if (row == NULL)
2610 {
2611 if (base_face_id == MODE_LINE_FACE_ID
2612 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2613 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2614 else if (base_face_id == HEADER_LINE_FACE_ID)
2615 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2616 }
2617
2618 /* Clear IT. */
2619 memset (it, 0, sizeof *it);
2620 it->current.overlay_string_index = -1;
2621 it->current.dpvec_index = -1;
2622 it->base_face_id = remapped_base_face_id;
2623 it->string = Qnil;
2624 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2625
2626 /* The window in which we iterate over current_buffer: */
2627 XSETWINDOW (it->window, w);
2628 it->w = w;
2629 it->f = XFRAME (w->frame);
2630
2631 it->cmp_it.id = -1;
2632
2633 /* Extra space between lines (on window systems only). */
2634 if (base_face_id == DEFAULT_FACE_ID
2635 && FRAME_WINDOW_P (it->f))
2636 {
2637 if (NATNUMP (current_buffer->extra_line_spacing))
2638 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2639 else if (FLOATP (current_buffer->extra_line_spacing))
2640 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2641 * FRAME_LINE_HEIGHT (it->f));
2642 else if (it->f->extra_line_spacing > 0)
2643 it->extra_line_spacing = it->f->extra_line_spacing;
2644 it->max_extra_line_spacing = 0;
2645 }
2646
2647 /* If realized faces have been removed, e.g. because of face
2648 attribute changes of named faces, recompute them. When running
2649 in batch mode, the face cache of the initial frame is null. If
2650 we happen to get called, make a dummy face cache. */
2651 if (FRAME_FACE_CACHE (it->f) == NULL)
2652 init_frame_faces (it->f);
2653 if (FRAME_FACE_CACHE (it->f)->used == 0)
2654 recompute_basic_faces (it->f);
2655
2656 /* Current value of the `slice', `space-width', and 'height' properties. */
2657 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2658 it->space_width = Qnil;
2659 it->font_height = Qnil;
2660 it->override_ascent = -1;
2661
2662 /* Are control characters displayed as `^C'? */
2663 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2664
2665 /* -1 means everything between a CR and the following line end
2666 is invisible. >0 means lines indented more than this value are
2667 invisible. */
2668 it->selective = (INTEGERP (current_buffer->selective_display)
2669 ? XFASTINT (current_buffer->selective_display)
2670 : (!NILP (current_buffer->selective_display)
2671 ? -1 : 0));
2672 it->selective_display_ellipsis_p
2673 = !NILP (current_buffer->selective_display_ellipses);
2674
2675 /* Display table to use. */
2676 it->dp = window_display_table (w);
2677
2678 /* Are multibyte characters enabled in current_buffer? */
2679 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2680
2681 /* Do we need to reorder bidirectional text? Not if this is a
2682 unibyte buffer: by definition, none of the single-byte characters
2683 are strong R2L, so no reordering is needed. And bidi.c doesn't
2684 support unibyte buffers anyway. */
2685 it->bidi_p
2686 = !NILP (current_buffer->bidi_display_reordering) && it->multibyte_p;
2687
2688 /* Non-zero if we should highlight the region. */
2689 highlight_region_p
2690 = (!NILP (Vtransient_mark_mode)
2691 && !NILP (current_buffer->mark_active)
2692 && XMARKER (current_buffer->mark)->buffer != 0);
2693
2694 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2695 start and end of a visible region in window IT->w. Set both to
2696 -1 to indicate no region. */
2697 if (highlight_region_p
2698 /* Maybe highlight only in selected window. */
2699 && (/* Either show region everywhere. */
2700 highlight_nonselected_windows
2701 /* Or show region in the selected window. */
2702 || w == XWINDOW (selected_window)
2703 /* Or show the region if we are in the mini-buffer and W is
2704 the window the mini-buffer refers to. */
2705 || (MINI_WINDOW_P (XWINDOW (selected_window))
2706 && WINDOWP (minibuf_selected_window)
2707 && w == XWINDOW (minibuf_selected_window))))
2708 {
2709 EMACS_INT charpos = marker_position (current_buffer->mark);
2710 it->region_beg_charpos = min (PT, charpos);
2711 it->region_end_charpos = max (PT, charpos);
2712 }
2713 else
2714 it->region_beg_charpos = it->region_end_charpos = -1;
2715
2716 /* Get the position at which the redisplay_end_trigger hook should
2717 be run, if it is to be run at all. */
2718 if (MARKERP (w->redisplay_end_trigger)
2719 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2720 it->redisplay_end_trigger_charpos
2721 = marker_position (w->redisplay_end_trigger);
2722 else if (INTEGERP (w->redisplay_end_trigger))
2723 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2724
2725 /* Correct bogus values of tab_width. */
2726 it->tab_width = XINT (current_buffer->tab_width);
2727 if (it->tab_width <= 0 || it->tab_width > 1000)
2728 it->tab_width = 8;
2729
2730 /* Are lines in the display truncated? */
2731 if (base_face_id != DEFAULT_FACE_ID
2732 || XINT (it->w->hscroll)
2733 || (! WINDOW_FULL_WIDTH_P (it->w)
2734 && ((!NILP (Vtruncate_partial_width_windows)
2735 && !INTEGERP (Vtruncate_partial_width_windows))
2736 || (INTEGERP (Vtruncate_partial_width_windows)
2737 && (WINDOW_TOTAL_COLS (it->w)
2738 < XINT (Vtruncate_partial_width_windows))))))
2739 it->line_wrap = TRUNCATE;
2740 else if (NILP (current_buffer->truncate_lines))
2741 it->line_wrap = NILP (current_buffer->word_wrap)
2742 ? WINDOW_WRAP : WORD_WRAP;
2743 else
2744 it->line_wrap = TRUNCATE;
2745
2746 /* Get dimensions of truncation and continuation glyphs. These are
2747 displayed as fringe bitmaps under X, so we don't need them for such
2748 frames. */
2749 if (!FRAME_WINDOW_P (it->f))
2750 {
2751 if (it->line_wrap == TRUNCATE)
2752 {
2753 /* We will need the truncation glyph. */
2754 xassert (it->glyph_row == NULL);
2755 produce_special_glyphs (it, IT_TRUNCATION);
2756 it->truncation_pixel_width = it->pixel_width;
2757 }
2758 else
2759 {
2760 /* We will need the continuation glyph. */
2761 xassert (it->glyph_row == NULL);
2762 produce_special_glyphs (it, IT_CONTINUATION);
2763 it->continuation_pixel_width = it->pixel_width;
2764 }
2765
2766 /* Reset these values to zero because the produce_special_glyphs
2767 above has changed them. */
2768 it->pixel_width = it->ascent = it->descent = 0;
2769 it->phys_ascent = it->phys_descent = 0;
2770 }
2771
2772 /* Set this after getting the dimensions of truncation and
2773 continuation glyphs, so that we don't produce glyphs when calling
2774 produce_special_glyphs, above. */
2775 it->glyph_row = row;
2776 it->area = TEXT_AREA;
2777
2778 /* Forget any previous info about this row being reversed. */
2779 if (it->glyph_row)
2780 it->glyph_row->reversed_p = 0;
2781
2782 /* Get the dimensions of the display area. The display area
2783 consists of the visible window area plus a horizontally scrolled
2784 part to the left of the window. All x-values are relative to the
2785 start of this total display area. */
2786 if (base_face_id != DEFAULT_FACE_ID)
2787 {
2788 /* Mode lines, menu bar in terminal frames. */
2789 it->first_visible_x = 0;
2790 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2791 }
2792 else
2793 {
2794 it->first_visible_x
2795 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2796 it->last_visible_x = (it->first_visible_x
2797 + window_box_width (w, TEXT_AREA));
2798
2799 /* If we truncate lines, leave room for the truncator glyph(s) at
2800 the right margin. Otherwise, leave room for the continuation
2801 glyph(s). Truncation and continuation glyphs are not inserted
2802 for window-based redisplay. */
2803 if (!FRAME_WINDOW_P (it->f))
2804 {
2805 if (it->line_wrap == TRUNCATE)
2806 it->last_visible_x -= it->truncation_pixel_width;
2807 else
2808 it->last_visible_x -= it->continuation_pixel_width;
2809 }
2810
2811 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2812 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2813 }
2814
2815 /* Leave room for a border glyph. */
2816 if (!FRAME_WINDOW_P (it->f)
2817 && !WINDOW_RIGHTMOST_P (it->w))
2818 it->last_visible_x -= 1;
2819
2820 it->last_visible_y = window_text_bottom_y (w);
2821
2822 /* For mode lines and alike, arrange for the first glyph having a
2823 left box line if the face specifies a box. */
2824 if (base_face_id != DEFAULT_FACE_ID)
2825 {
2826 struct face *face;
2827
2828 it->face_id = remapped_base_face_id;
2829
2830 /* If we have a boxed mode line, make the first character appear
2831 with a left box line. */
2832 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2833 if (face->box != FACE_NO_BOX)
2834 it->start_of_box_run_p = 1;
2835 }
2836
2837 /* If we are to reorder bidirectional text, init the bidi
2838 iterator. */
2839 if (it->bidi_p)
2840 {
2841 /* Note the paragraph direction that this buffer wants to
2842 use. */
2843 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2844 it->paragraph_embedding = L2R;
2845 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2846 it->paragraph_embedding = R2L;
2847 else
2848 it->paragraph_embedding = NEUTRAL_DIR;
2849 bidi_init_it (charpos, bytepos, &it->bidi_it);
2850 }
2851
2852 /* If a buffer position was specified, set the iterator there,
2853 getting overlays and face properties from that position. */
2854 if (charpos >= BUF_BEG (current_buffer))
2855 {
2856 it->end_charpos = ZV;
2857 it->face_id = -1;
2858 IT_CHARPOS (*it) = charpos;
2859
2860 /* Compute byte position if not specified. */
2861 if (bytepos < charpos)
2862 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2863 else
2864 IT_BYTEPOS (*it) = bytepos;
2865
2866 it->start = it->current;
2867
2868 /* Compute faces etc. */
2869 reseat (it, it->current.pos, 1);
2870 }
2871
2872 CHECK_IT (it);
2873 }
2874
2875
2876 /* Initialize IT for the display of window W with window start POS. */
2877
2878 void
2879 start_display (struct it *it, struct window *w, struct text_pos pos)
2880 {
2881 struct glyph_row *row;
2882 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2883
2884 row = w->desired_matrix->rows + first_vpos;
2885 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2886 it->first_vpos = first_vpos;
2887
2888 /* Don't reseat to previous visible line start if current start
2889 position is in a string or image. */
2890 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2891 {
2892 int start_at_line_beg_p;
2893 int first_y = it->current_y;
2894
2895 /* If window start is not at a line start, skip forward to POS to
2896 get the correct continuation lines width. */
2897 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2898 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2899 if (!start_at_line_beg_p)
2900 {
2901 int new_x;
2902
2903 reseat_at_previous_visible_line_start (it);
2904 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2905
2906 new_x = it->current_x + it->pixel_width;
2907
2908 /* If lines are continued, this line may end in the middle
2909 of a multi-glyph character (e.g. a control character
2910 displayed as \003, or in the middle of an overlay
2911 string). In this case move_it_to above will not have
2912 taken us to the start of the continuation line but to the
2913 end of the continued line. */
2914 if (it->current_x > 0
2915 && it->line_wrap != TRUNCATE /* Lines are continued. */
2916 && (/* And glyph doesn't fit on the line. */
2917 new_x > it->last_visible_x
2918 /* Or it fits exactly and we're on a window
2919 system frame. */
2920 || (new_x == it->last_visible_x
2921 && FRAME_WINDOW_P (it->f))))
2922 {
2923 if (it->current.dpvec_index >= 0
2924 || it->current.overlay_string_index >= 0)
2925 {
2926 set_iterator_to_next (it, 1);
2927 move_it_in_display_line_to (it, -1, -1, 0);
2928 }
2929
2930 it->continuation_lines_width += it->current_x;
2931 }
2932
2933 /* We're starting a new display line, not affected by the
2934 height of the continued line, so clear the appropriate
2935 fields in the iterator structure. */
2936 it->max_ascent = it->max_descent = 0;
2937 it->max_phys_ascent = it->max_phys_descent = 0;
2938
2939 it->current_y = first_y;
2940 it->vpos = 0;
2941 it->current_x = it->hpos = 0;
2942 }
2943 }
2944 }
2945
2946
2947 /* Return 1 if POS is a position in ellipses displayed for invisible
2948 text. W is the window we display, for text property lookup. */
2949
2950 static int
2951 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2952 {
2953 Lisp_Object prop, window;
2954 int ellipses_p = 0;
2955 EMACS_INT charpos = CHARPOS (pos->pos);
2956
2957 /* If POS specifies a position in a display vector, this might
2958 be for an ellipsis displayed for invisible text. We won't
2959 get the iterator set up for delivering that ellipsis unless
2960 we make sure that it gets aware of the invisible text. */
2961 if (pos->dpvec_index >= 0
2962 && pos->overlay_string_index < 0
2963 && CHARPOS (pos->string_pos) < 0
2964 && charpos > BEGV
2965 && (XSETWINDOW (window, w),
2966 prop = Fget_char_property (make_number (charpos),
2967 Qinvisible, window),
2968 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2969 {
2970 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2971 window);
2972 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2973 }
2974
2975 return ellipses_p;
2976 }
2977
2978
2979 /* Initialize IT for stepping through current_buffer in window W,
2980 starting at position POS that includes overlay string and display
2981 vector/ control character translation position information. Value
2982 is zero if there are overlay strings with newlines at POS. */
2983
2984 static int
2985 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2986 {
2987 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2988 int i, overlay_strings_with_newlines = 0;
2989
2990 /* If POS specifies a position in a display vector, this might
2991 be for an ellipsis displayed for invisible text. We won't
2992 get the iterator set up for delivering that ellipsis unless
2993 we make sure that it gets aware of the invisible text. */
2994 if (in_ellipses_for_invisible_text_p (pos, w))
2995 {
2996 --charpos;
2997 bytepos = 0;
2998 }
2999
3000 /* Keep in mind: the call to reseat in init_iterator skips invisible
3001 text, so we might end up at a position different from POS. This
3002 is only a problem when POS is a row start after a newline and an
3003 overlay starts there with an after-string, and the overlay has an
3004 invisible property. Since we don't skip invisible text in
3005 display_line and elsewhere immediately after consuming the
3006 newline before the row start, such a POS will not be in a string,
3007 but the call to init_iterator below will move us to the
3008 after-string. */
3009 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3010
3011 /* This only scans the current chunk -- it should scan all chunks.
3012 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3013 to 16 in 22.1 to make this a lesser problem. */
3014 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3015 {
3016 const char *s = SDATA (it->overlay_strings[i]);
3017 const char *e = s + SBYTES (it->overlay_strings[i]);
3018
3019 while (s < e && *s != '\n')
3020 ++s;
3021
3022 if (s < e)
3023 {
3024 overlay_strings_with_newlines = 1;
3025 break;
3026 }
3027 }
3028
3029 /* If position is within an overlay string, set up IT to the right
3030 overlay string. */
3031 if (pos->overlay_string_index >= 0)
3032 {
3033 int relative_index;
3034
3035 /* If the first overlay string happens to have a `display'
3036 property for an image, the iterator will be set up for that
3037 image, and we have to undo that setup first before we can
3038 correct the overlay string index. */
3039 if (it->method == GET_FROM_IMAGE)
3040 pop_it (it);
3041
3042 /* We already have the first chunk of overlay strings in
3043 IT->overlay_strings. Load more until the one for
3044 pos->overlay_string_index is in IT->overlay_strings. */
3045 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3046 {
3047 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3048 it->current.overlay_string_index = 0;
3049 while (n--)
3050 {
3051 load_overlay_strings (it, 0);
3052 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3053 }
3054 }
3055
3056 it->current.overlay_string_index = pos->overlay_string_index;
3057 relative_index = (it->current.overlay_string_index
3058 % OVERLAY_STRING_CHUNK_SIZE);
3059 it->string = it->overlay_strings[relative_index];
3060 xassert (STRINGP (it->string));
3061 it->current.string_pos = pos->string_pos;
3062 it->method = GET_FROM_STRING;
3063 }
3064
3065 if (CHARPOS (pos->string_pos) >= 0)
3066 {
3067 /* Recorded position is not in an overlay string, but in another
3068 string. This can only be a string from a `display' property.
3069 IT should already be filled with that string. */
3070 it->current.string_pos = pos->string_pos;
3071 xassert (STRINGP (it->string));
3072 }
3073
3074 /* Restore position in display vector translations, control
3075 character translations or ellipses. */
3076 if (pos->dpvec_index >= 0)
3077 {
3078 if (it->dpvec == NULL)
3079 get_next_display_element (it);
3080 xassert (it->dpvec && it->current.dpvec_index == 0);
3081 it->current.dpvec_index = pos->dpvec_index;
3082 }
3083
3084 CHECK_IT (it);
3085 return !overlay_strings_with_newlines;
3086 }
3087
3088
3089 /* Initialize IT for stepping through current_buffer in window W
3090 starting at ROW->start. */
3091
3092 static void
3093 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3094 {
3095 init_from_display_pos (it, w, &row->start);
3096 it->start = row->start;
3097 it->continuation_lines_width = row->continuation_lines_width;
3098 CHECK_IT (it);
3099 }
3100
3101
3102 /* Initialize IT for stepping through current_buffer in window W
3103 starting in the line following ROW, i.e. starting at ROW->end.
3104 Value is zero if there are overlay strings with newlines at ROW's
3105 end position. */
3106
3107 static int
3108 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3109 {
3110 int success = 0;
3111
3112 if (init_from_display_pos (it, w, &row->end))
3113 {
3114 if (row->continued_p)
3115 it->continuation_lines_width
3116 = row->continuation_lines_width + row->pixel_width;
3117 CHECK_IT (it);
3118 success = 1;
3119 }
3120
3121 return success;
3122 }
3123
3124
3125
3126 \f
3127 /***********************************************************************
3128 Text properties
3129 ***********************************************************************/
3130
3131 /* Called when IT reaches IT->stop_charpos. Handle text property and
3132 overlay changes. Set IT->stop_charpos to the next position where
3133 to stop. */
3134
3135 static void
3136 handle_stop (struct it *it)
3137 {
3138 enum prop_handled handled;
3139 int handle_overlay_change_p;
3140 struct props *p;
3141
3142 it->dpvec = NULL;
3143 it->current.dpvec_index = -1;
3144 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3145 it->ignore_overlay_strings_at_pos_p = 0;
3146 it->ellipsis_p = 0;
3147
3148 /* Use face of preceding text for ellipsis (if invisible) */
3149 if (it->selective_display_ellipsis_p)
3150 it->saved_face_id = it->face_id;
3151
3152 do
3153 {
3154 handled = HANDLED_NORMALLY;
3155
3156 /* Call text property handlers. */
3157 for (p = it_props; p->handler; ++p)
3158 {
3159 handled = p->handler (it);
3160
3161 if (handled == HANDLED_RECOMPUTE_PROPS)
3162 break;
3163 else if (handled == HANDLED_RETURN)
3164 {
3165 /* We still want to show before and after strings from
3166 overlays even if the actual buffer text is replaced. */
3167 if (!handle_overlay_change_p
3168 || it->sp > 1
3169 || !get_overlay_strings_1 (it, 0, 0))
3170 {
3171 if (it->ellipsis_p)
3172 setup_for_ellipsis (it, 0);
3173 /* When handling a display spec, we might load an
3174 empty string. In that case, discard it here. We
3175 used to discard it in handle_single_display_spec,
3176 but that causes get_overlay_strings_1, above, to
3177 ignore overlay strings that we must check. */
3178 if (STRINGP (it->string) && !SCHARS (it->string))
3179 pop_it (it);
3180 return;
3181 }
3182 else if (STRINGP (it->string) && !SCHARS (it->string))
3183 pop_it (it);
3184 else
3185 {
3186 it->ignore_overlay_strings_at_pos_p = 1;
3187 it->string_from_display_prop_p = 0;
3188 handle_overlay_change_p = 0;
3189 }
3190 handled = HANDLED_RECOMPUTE_PROPS;
3191 break;
3192 }
3193 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3194 handle_overlay_change_p = 0;
3195 }
3196
3197 if (handled != HANDLED_RECOMPUTE_PROPS)
3198 {
3199 /* Don't check for overlay strings below when set to deliver
3200 characters from a display vector. */
3201 if (it->method == GET_FROM_DISPLAY_VECTOR)
3202 handle_overlay_change_p = 0;
3203
3204 /* Handle overlay changes.
3205 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3206 if it finds overlays. */
3207 if (handle_overlay_change_p)
3208 handled = handle_overlay_change (it);
3209 }
3210
3211 if (it->ellipsis_p)
3212 {
3213 setup_for_ellipsis (it, 0);
3214 break;
3215 }
3216 }
3217 while (handled == HANDLED_RECOMPUTE_PROPS);
3218
3219 /* Determine where to stop next. */
3220 if (handled == HANDLED_NORMALLY)
3221 compute_stop_pos (it);
3222 }
3223
3224
3225 /* Compute IT->stop_charpos from text property and overlay change
3226 information for IT's current position. */
3227
3228 static void
3229 compute_stop_pos (struct it *it)
3230 {
3231 register INTERVAL iv, next_iv;
3232 Lisp_Object object, limit, position;
3233 EMACS_INT charpos, bytepos;
3234
3235 /* If nowhere else, stop at the end. */
3236 it->stop_charpos = it->end_charpos;
3237
3238 if (STRINGP (it->string))
3239 {
3240 /* Strings are usually short, so don't limit the search for
3241 properties. */
3242 object = it->string;
3243 limit = Qnil;
3244 charpos = IT_STRING_CHARPOS (*it);
3245 bytepos = IT_STRING_BYTEPOS (*it);
3246 }
3247 else
3248 {
3249 EMACS_INT pos;
3250
3251 /* If next overlay change is in front of the current stop pos
3252 (which is IT->end_charpos), stop there. Note: value of
3253 next_overlay_change is point-max if no overlay change
3254 follows. */
3255 charpos = IT_CHARPOS (*it);
3256 bytepos = IT_BYTEPOS (*it);
3257 pos = next_overlay_change (charpos);
3258 if (pos < it->stop_charpos)
3259 it->stop_charpos = pos;
3260
3261 /* If showing the region, we have to stop at the region
3262 start or end because the face might change there. */
3263 if (it->region_beg_charpos > 0)
3264 {
3265 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3266 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3267 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3268 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3269 }
3270
3271 /* Set up variables for computing the stop position from text
3272 property changes. */
3273 XSETBUFFER (object, current_buffer);
3274 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3275 }
3276
3277 /* Get the interval containing IT's position. Value is a null
3278 interval if there isn't such an interval. */
3279 position = make_number (charpos);
3280 iv = validate_interval_range (object, &position, &position, 0);
3281 if (!NULL_INTERVAL_P (iv))
3282 {
3283 Lisp_Object values_here[LAST_PROP_IDX];
3284 struct props *p;
3285
3286 /* Get properties here. */
3287 for (p = it_props; p->handler; ++p)
3288 values_here[p->idx] = textget (iv->plist, *p->name);
3289
3290 /* Look for an interval following iv that has different
3291 properties. */
3292 for (next_iv = next_interval (iv);
3293 (!NULL_INTERVAL_P (next_iv)
3294 && (NILP (limit)
3295 || XFASTINT (limit) > next_iv->position));
3296 next_iv = next_interval (next_iv))
3297 {
3298 for (p = it_props; p->handler; ++p)
3299 {
3300 Lisp_Object new_value;
3301
3302 new_value = textget (next_iv->plist, *p->name);
3303 if (!EQ (values_here[p->idx], new_value))
3304 break;
3305 }
3306
3307 if (p->handler)
3308 break;
3309 }
3310
3311 if (!NULL_INTERVAL_P (next_iv))
3312 {
3313 if (INTEGERP (limit)
3314 && next_iv->position >= XFASTINT (limit))
3315 /* No text property change up to limit. */
3316 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3317 else
3318 /* Text properties change in next_iv. */
3319 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3320 }
3321 }
3322
3323 if (it->cmp_it.id < 0)
3324 {
3325 EMACS_INT stoppos = it->end_charpos;
3326
3327 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3328 stoppos = -1;
3329 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3330 stoppos, it->string);
3331 }
3332
3333 xassert (STRINGP (it->string)
3334 || (it->stop_charpos >= BEGV
3335 && it->stop_charpos >= IT_CHARPOS (*it)));
3336 }
3337
3338
3339 /* Return the position of the next overlay change after POS in
3340 current_buffer. Value is point-max if no overlay change
3341 follows. This is like `next-overlay-change' but doesn't use
3342 xmalloc. */
3343
3344 static EMACS_INT
3345 next_overlay_change (EMACS_INT pos)
3346 {
3347 int noverlays;
3348 EMACS_INT endpos;
3349 Lisp_Object *overlays;
3350 int i;
3351
3352 /* Get all overlays at the given position. */
3353 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3354
3355 /* If any of these overlays ends before endpos,
3356 use its ending point instead. */
3357 for (i = 0; i < noverlays; ++i)
3358 {
3359 Lisp_Object oend;
3360 EMACS_INT oendpos;
3361
3362 oend = OVERLAY_END (overlays[i]);
3363 oendpos = OVERLAY_POSITION (oend);
3364 endpos = min (endpos, oendpos);
3365 }
3366
3367 return endpos;
3368 }
3369
3370
3371 \f
3372 /***********************************************************************
3373 Fontification
3374 ***********************************************************************/
3375
3376 /* Handle changes in the `fontified' property of the current buffer by
3377 calling hook functions from Qfontification_functions to fontify
3378 regions of text. */
3379
3380 static enum prop_handled
3381 handle_fontified_prop (struct it *it)
3382 {
3383 Lisp_Object prop, pos;
3384 enum prop_handled handled = HANDLED_NORMALLY;
3385
3386 if (!NILP (Vmemory_full))
3387 return handled;
3388
3389 /* Get the value of the `fontified' property at IT's current buffer
3390 position. (The `fontified' property doesn't have a special
3391 meaning in strings.) If the value is nil, call functions from
3392 Qfontification_functions. */
3393 if (!STRINGP (it->string)
3394 && it->s == NULL
3395 && !NILP (Vfontification_functions)
3396 && !NILP (Vrun_hooks)
3397 && (pos = make_number (IT_CHARPOS (*it)),
3398 prop = Fget_char_property (pos, Qfontified, Qnil),
3399 /* Ignore the special cased nil value always present at EOB since
3400 no amount of fontifying will be able to change it. */
3401 NILP (prop) && IT_CHARPOS (*it) < Z))
3402 {
3403 int count = SPECPDL_INDEX ();
3404 Lisp_Object val;
3405
3406 val = Vfontification_functions;
3407 specbind (Qfontification_functions, Qnil);
3408
3409 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3410 safe_call1 (val, pos);
3411 else
3412 {
3413 Lisp_Object globals, fn;
3414 struct gcpro gcpro1, gcpro2;
3415
3416 globals = Qnil;
3417 GCPRO2 (val, globals);
3418
3419 for (; CONSP (val); val = XCDR (val))
3420 {
3421 fn = XCAR (val);
3422
3423 if (EQ (fn, Qt))
3424 {
3425 /* A value of t indicates this hook has a local
3426 binding; it means to run the global binding too.
3427 In a global value, t should not occur. If it
3428 does, we must ignore it to avoid an endless
3429 loop. */
3430 for (globals = Fdefault_value (Qfontification_functions);
3431 CONSP (globals);
3432 globals = XCDR (globals))
3433 {
3434 fn = XCAR (globals);
3435 if (!EQ (fn, Qt))
3436 safe_call1 (fn, pos);
3437 }
3438 }
3439 else
3440 safe_call1 (fn, pos);
3441 }
3442
3443 UNGCPRO;
3444 }
3445
3446 unbind_to (count, Qnil);
3447
3448 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3449 something. This avoids an endless loop if they failed to
3450 fontify the text for which reason ever. */
3451 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3452 handled = HANDLED_RECOMPUTE_PROPS;
3453 }
3454
3455 return handled;
3456 }
3457
3458
3459 \f
3460 /***********************************************************************
3461 Faces
3462 ***********************************************************************/
3463
3464 /* Set up iterator IT from face properties at its current position.
3465 Called from handle_stop. */
3466
3467 static enum prop_handled
3468 handle_face_prop (struct it *it)
3469 {
3470 int new_face_id;
3471 EMACS_INT next_stop;
3472
3473 if (!STRINGP (it->string))
3474 {
3475 new_face_id
3476 = face_at_buffer_position (it->w,
3477 IT_CHARPOS (*it),
3478 it->region_beg_charpos,
3479 it->region_end_charpos,
3480 &next_stop,
3481 (IT_CHARPOS (*it)
3482 + TEXT_PROP_DISTANCE_LIMIT),
3483 0, it->base_face_id);
3484
3485 /* Is this a start of a run of characters with box face?
3486 Caveat: this can be called for a freshly initialized
3487 iterator; face_id is -1 in this case. We know that the new
3488 face will not change until limit, i.e. if the new face has a
3489 box, all characters up to limit will have one. But, as
3490 usual, we don't know whether limit is really the end. */
3491 if (new_face_id != it->face_id)
3492 {
3493 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3494
3495 /* If new face has a box but old face has not, this is
3496 the start of a run of characters with box, i.e. it has
3497 a shadow on the left side. The value of face_id of the
3498 iterator will be -1 if this is the initial call that gets
3499 the face. In this case, we have to look in front of IT's
3500 position and see whether there is a face != new_face_id. */
3501 it->start_of_box_run_p
3502 = (new_face->box != FACE_NO_BOX
3503 && (it->face_id >= 0
3504 || IT_CHARPOS (*it) == BEG
3505 || new_face_id != face_before_it_pos (it)));
3506 it->face_box_p = new_face->box != FACE_NO_BOX;
3507 }
3508 }
3509 else
3510 {
3511 int base_face_id;
3512 EMACS_INT bufpos;
3513 int i;
3514 Lisp_Object from_overlay
3515 = (it->current.overlay_string_index >= 0
3516 ? it->string_overlays[it->current.overlay_string_index]
3517 : Qnil);
3518
3519 /* See if we got to this string directly or indirectly from
3520 an overlay property. That includes the before-string or
3521 after-string of an overlay, strings in display properties
3522 provided by an overlay, their text properties, etc.
3523
3524 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3525 if (! NILP (from_overlay))
3526 for (i = it->sp - 1; i >= 0; i--)
3527 {
3528 if (it->stack[i].current.overlay_string_index >= 0)
3529 from_overlay
3530 = it->string_overlays[it->stack[i].current.overlay_string_index];
3531 else if (! NILP (it->stack[i].from_overlay))
3532 from_overlay = it->stack[i].from_overlay;
3533
3534 if (!NILP (from_overlay))
3535 break;
3536 }
3537
3538 if (! NILP (from_overlay))
3539 {
3540 bufpos = IT_CHARPOS (*it);
3541 /* For a string from an overlay, the base face depends
3542 only on text properties and ignores overlays. */
3543 base_face_id
3544 = face_for_overlay_string (it->w,
3545 IT_CHARPOS (*it),
3546 it->region_beg_charpos,
3547 it->region_end_charpos,
3548 &next_stop,
3549 (IT_CHARPOS (*it)
3550 + TEXT_PROP_DISTANCE_LIMIT),
3551 0,
3552 from_overlay);
3553 }
3554 else
3555 {
3556 bufpos = 0;
3557
3558 /* For strings from a `display' property, use the face at
3559 IT's current buffer position as the base face to merge
3560 with, so that overlay strings appear in the same face as
3561 surrounding text, unless they specify their own
3562 faces. */
3563 base_face_id = underlying_face_id (it);
3564 }
3565
3566 new_face_id = face_at_string_position (it->w,
3567 it->string,
3568 IT_STRING_CHARPOS (*it),
3569 bufpos,
3570 it->region_beg_charpos,
3571 it->region_end_charpos,
3572 &next_stop,
3573 base_face_id, 0);
3574
3575 /* Is this a start of a run of characters with box? Caveat:
3576 this can be called for a freshly allocated iterator; face_id
3577 is -1 is this case. We know that the new face will not
3578 change until the next check pos, i.e. if the new face has a
3579 box, all characters up to that position will have a
3580 box. But, as usual, we don't know whether that position
3581 is really the end. */
3582 if (new_face_id != it->face_id)
3583 {
3584 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3585 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3586
3587 /* If new face has a box but old face hasn't, this is the
3588 start of a run of characters with box, i.e. it has a
3589 shadow on the left side. */
3590 it->start_of_box_run_p
3591 = new_face->box && (old_face == NULL || !old_face->box);
3592 it->face_box_p = new_face->box != FACE_NO_BOX;
3593 }
3594 }
3595
3596 it->face_id = new_face_id;
3597 return HANDLED_NORMALLY;
3598 }
3599
3600
3601 /* Return the ID of the face ``underlying'' IT's current position,
3602 which is in a string. If the iterator is associated with a
3603 buffer, return the face at IT's current buffer position.
3604 Otherwise, use the iterator's base_face_id. */
3605
3606 static int
3607 underlying_face_id (struct it *it)
3608 {
3609 int face_id = it->base_face_id, i;
3610
3611 xassert (STRINGP (it->string));
3612
3613 for (i = it->sp - 1; i >= 0; --i)
3614 if (NILP (it->stack[i].string))
3615 face_id = it->stack[i].face_id;
3616
3617 return face_id;
3618 }
3619
3620
3621 /* Compute the face one character before or after the current position
3622 of IT. BEFORE_P non-zero means get the face in front of IT's
3623 position. Value is the id of the face. */
3624
3625 static int
3626 face_before_or_after_it_pos (struct it *it, int before_p)
3627 {
3628 int face_id, limit;
3629 EMACS_INT next_check_charpos;
3630 struct text_pos pos;
3631
3632 xassert (it->s == NULL);
3633
3634 if (STRINGP (it->string))
3635 {
3636 EMACS_INT bufpos;
3637 int base_face_id;
3638
3639 /* No face change past the end of the string (for the case
3640 we are padding with spaces). No face change before the
3641 string start. */
3642 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3643 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3644 return it->face_id;
3645
3646 /* Set pos to the position before or after IT's current position. */
3647 if (before_p)
3648 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3649 else
3650 /* For composition, we must check the character after the
3651 composition. */
3652 pos = (it->what == IT_COMPOSITION
3653 ? string_pos (IT_STRING_CHARPOS (*it)
3654 + it->cmp_it.nchars, it->string)
3655 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3656
3657 if (it->current.overlay_string_index >= 0)
3658 bufpos = IT_CHARPOS (*it);
3659 else
3660 bufpos = 0;
3661
3662 base_face_id = underlying_face_id (it);
3663
3664 /* Get the face for ASCII, or unibyte. */
3665 face_id = face_at_string_position (it->w,
3666 it->string,
3667 CHARPOS (pos),
3668 bufpos,
3669 it->region_beg_charpos,
3670 it->region_end_charpos,
3671 &next_check_charpos,
3672 base_face_id, 0);
3673
3674 /* Correct the face for charsets different from ASCII. Do it
3675 for the multibyte case only. The face returned above is
3676 suitable for unibyte text if IT->string is unibyte. */
3677 if (STRING_MULTIBYTE (it->string))
3678 {
3679 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3680 int c, len;
3681 struct face *face = FACE_FROM_ID (it->f, face_id);
3682
3683 c = string_char_and_length (p, &len);
3684 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3685 }
3686 }
3687 else
3688 {
3689 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3690 || (IT_CHARPOS (*it) <= BEGV && before_p))
3691 return it->face_id;
3692
3693 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3694 pos = it->current.pos;
3695
3696 if (before_p)
3697 DEC_TEXT_POS (pos, it->multibyte_p);
3698 else
3699 {
3700 if (it->what == IT_COMPOSITION)
3701 /* For composition, we must check the position after the
3702 composition. */
3703 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3704 else
3705 INC_TEXT_POS (pos, it->multibyte_p);
3706 }
3707
3708 /* Determine face for CHARSET_ASCII, or unibyte. */
3709 face_id = face_at_buffer_position (it->w,
3710 CHARPOS (pos),
3711 it->region_beg_charpos,
3712 it->region_end_charpos,
3713 &next_check_charpos,
3714 limit, 0, -1);
3715
3716 /* Correct the face for charsets different from ASCII. Do it
3717 for the multibyte case only. The face returned above is
3718 suitable for unibyte text if current_buffer is unibyte. */
3719 if (it->multibyte_p)
3720 {
3721 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3722 struct face *face = FACE_FROM_ID (it->f, face_id);
3723 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3724 }
3725 }
3726
3727 return face_id;
3728 }
3729
3730
3731 \f
3732 /***********************************************************************
3733 Invisible text
3734 ***********************************************************************/
3735
3736 /* Set up iterator IT from invisible properties at its current
3737 position. Called from handle_stop. */
3738
3739 static enum prop_handled
3740 handle_invisible_prop (struct it *it)
3741 {
3742 enum prop_handled handled = HANDLED_NORMALLY;
3743
3744 if (STRINGP (it->string))
3745 {
3746 Lisp_Object prop, end_charpos, limit, charpos;
3747
3748 /* Get the value of the invisible text property at the
3749 current position. Value will be nil if there is no such
3750 property. */
3751 charpos = make_number (IT_STRING_CHARPOS (*it));
3752 prop = Fget_text_property (charpos, Qinvisible, it->string);
3753
3754 if (!NILP (prop)
3755 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3756 {
3757 handled = HANDLED_RECOMPUTE_PROPS;
3758
3759 /* Get the position at which the next change of the
3760 invisible text property can be found in IT->string.
3761 Value will be nil if the property value is the same for
3762 all the rest of IT->string. */
3763 XSETINT (limit, SCHARS (it->string));
3764 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3765 it->string, limit);
3766
3767 /* Text at current position is invisible. The next
3768 change in the property is at position end_charpos.
3769 Move IT's current position to that position. */
3770 if (INTEGERP (end_charpos)
3771 && XFASTINT (end_charpos) < XFASTINT (limit))
3772 {
3773 struct text_pos old;
3774 old = it->current.string_pos;
3775 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3776 compute_string_pos (&it->current.string_pos, old, it->string);
3777 }
3778 else
3779 {
3780 /* The rest of the string is invisible. If this is an
3781 overlay string, proceed with the next overlay string
3782 or whatever comes and return a character from there. */
3783 if (it->current.overlay_string_index >= 0)
3784 {
3785 next_overlay_string (it);
3786 /* Don't check for overlay strings when we just
3787 finished processing them. */
3788 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3789 }
3790 else
3791 {
3792 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3793 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3794 }
3795 }
3796 }
3797 }
3798 else
3799 {
3800 int invis_p;
3801 EMACS_INT newpos, next_stop, start_charpos, tem;
3802 Lisp_Object pos, prop, overlay;
3803
3804 /* First of all, is there invisible text at this position? */
3805 tem = start_charpos = IT_CHARPOS (*it);
3806 pos = make_number (tem);
3807 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3808 &overlay);
3809 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3810
3811 /* If we are on invisible text, skip over it. */
3812 if (invis_p && start_charpos < it->end_charpos)
3813 {
3814 /* Record whether we have to display an ellipsis for the
3815 invisible text. */
3816 int display_ellipsis_p = invis_p == 2;
3817
3818 handled = HANDLED_RECOMPUTE_PROPS;
3819
3820 /* Loop skipping over invisible text. The loop is left at
3821 ZV or with IT on the first char being visible again. */
3822 do
3823 {
3824 /* Try to skip some invisible text. Return value is the
3825 position reached which can be equal to where we start
3826 if there is nothing invisible there. This skips both
3827 over invisible text properties and overlays with
3828 invisible property. */
3829 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3830
3831 /* If we skipped nothing at all we weren't at invisible
3832 text in the first place. If everything to the end of
3833 the buffer was skipped, end the loop. */
3834 if (newpos == tem || newpos >= ZV)
3835 invis_p = 0;
3836 else
3837 {
3838 /* We skipped some characters but not necessarily
3839 all there are. Check if we ended up on visible
3840 text. Fget_char_property returns the property of
3841 the char before the given position, i.e. if we
3842 get invis_p = 0, this means that the char at
3843 newpos is visible. */
3844 pos = make_number (newpos);
3845 prop = Fget_char_property (pos, Qinvisible, it->window);
3846 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3847 }
3848
3849 /* If we ended up on invisible text, proceed to
3850 skip starting with next_stop. */
3851 if (invis_p)
3852 tem = next_stop;
3853
3854 /* If there are adjacent invisible texts, don't lose the
3855 second one's ellipsis. */
3856 if (invis_p == 2)
3857 display_ellipsis_p = 1;
3858 }
3859 while (invis_p);
3860
3861 /* The position newpos is now either ZV or on visible text. */
3862 if (it->bidi_p && newpos < ZV)
3863 {
3864 /* With bidi iteration, the region of invisible text
3865 could start and/or end in the middle of a non-base
3866 embedding level. Therefore, we need to skip
3867 invisible text using the bidi iterator, starting at
3868 IT's current position, until we find ourselves
3869 outside the invisible text. Skipping invisible text
3870 _after_ bidi iteration avoids affecting the visual
3871 order of the displayed text when invisible properties
3872 are added or removed. */
3873 if (it->bidi_it.first_elt)
3874 {
3875 /* If we were `reseat'ed to a new paragraph,
3876 determine the paragraph base direction. We need
3877 to do it now because next_element_from_buffer may
3878 not have a chance to do it, if we are going to
3879 skip any text at the beginning, which resets the
3880 FIRST_ELT flag. */
3881 bidi_paragraph_init (it->paragraph_embedding,
3882 &it->bidi_it, 1);
3883 }
3884 do
3885 {
3886 bidi_move_to_visually_next (&it->bidi_it);
3887 }
3888 while (it->stop_charpos <= it->bidi_it.charpos
3889 && it->bidi_it.charpos < newpos);
3890 IT_CHARPOS (*it) = it->bidi_it.charpos;
3891 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3892 /* If we overstepped NEWPOS, record its position in the
3893 iterator, so that we skip invisible text if later the
3894 bidi iteration lands us in the invisible region
3895 again. */
3896 if (IT_CHARPOS (*it) >= newpos)
3897 it->prev_stop = newpos;
3898 }
3899 else
3900 {
3901 IT_CHARPOS (*it) = newpos;
3902 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3903 }
3904
3905 /* If there are before-strings at the start of invisible
3906 text, and the text is invisible because of a text
3907 property, arrange to show before-strings because 20.x did
3908 it that way. (If the text is invisible because of an
3909 overlay property instead of a text property, this is
3910 already handled in the overlay code.) */
3911 if (NILP (overlay)
3912 && get_overlay_strings (it, it->stop_charpos))
3913 {
3914 handled = HANDLED_RECOMPUTE_PROPS;
3915 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3916 }
3917 else if (display_ellipsis_p)
3918 {
3919 /* Make sure that the glyphs of the ellipsis will get
3920 correct `charpos' values. If we would not update
3921 it->position here, the glyphs would belong to the
3922 last visible character _before_ the invisible
3923 text, which confuses `set_cursor_from_row'.
3924
3925 We use the last invisible position instead of the
3926 first because this way the cursor is always drawn on
3927 the first "." of the ellipsis, whenever PT is inside
3928 the invisible text. Otherwise the cursor would be
3929 placed _after_ the ellipsis when the point is after the
3930 first invisible character. */
3931 if (!STRINGP (it->object))
3932 {
3933 it->position.charpos = newpos - 1;
3934 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3935 }
3936 it->ellipsis_p = 1;
3937 /* Let the ellipsis display before
3938 considering any properties of the following char.
3939 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3940 handled = HANDLED_RETURN;
3941 }
3942 }
3943 }
3944
3945 return handled;
3946 }
3947
3948
3949 /* Make iterator IT return `...' next.
3950 Replaces LEN characters from buffer. */
3951
3952 static void
3953 setup_for_ellipsis (struct it *it, int len)
3954 {
3955 /* Use the display table definition for `...'. Invalid glyphs
3956 will be handled by the method returning elements from dpvec. */
3957 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3958 {
3959 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3960 it->dpvec = v->contents;
3961 it->dpend = v->contents + v->size;
3962 }
3963 else
3964 {
3965 /* Default `...'. */
3966 it->dpvec = default_invis_vector;
3967 it->dpend = default_invis_vector + 3;
3968 }
3969
3970 it->dpvec_char_len = len;
3971 it->current.dpvec_index = 0;
3972 it->dpvec_face_id = -1;
3973
3974 /* Remember the current face id in case glyphs specify faces.
3975 IT's face is restored in set_iterator_to_next.
3976 saved_face_id was set to preceding char's face in handle_stop. */
3977 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3978 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3979
3980 it->method = GET_FROM_DISPLAY_VECTOR;
3981 it->ellipsis_p = 1;
3982 }
3983
3984
3985 \f
3986 /***********************************************************************
3987 'display' property
3988 ***********************************************************************/
3989
3990 /* Set up iterator IT from `display' property at its current position.
3991 Called from handle_stop.
3992 We return HANDLED_RETURN if some part of the display property
3993 overrides the display of the buffer text itself.
3994 Otherwise we return HANDLED_NORMALLY. */
3995
3996 static enum prop_handled
3997 handle_display_prop (struct it *it)
3998 {
3999 Lisp_Object prop, object, overlay;
4000 struct text_pos *position;
4001 /* Nonzero if some property replaces the display of the text itself. */
4002 int display_replaced_p = 0;
4003
4004 if (STRINGP (it->string))
4005 {
4006 object = it->string;
4007 position = &it->current.string_pos;
4008 }
4009 else
4010 {
4011 XSETWINDOW (object, it->w);
4012 position = &it->current.pos;
4013 }
4014
4015 /* Reset those iterator values set from display property values. */
4016 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4017 it->space_width = Qnil;
4018 it->font_height = Qnil;
4019 it->voffset = 0;
4020
4021 /* We don't support recursive `display' properties, i.e. string
4022 values that have a string `display' property, that have a string
4023 `display' property etc. */
4024 if (!it->string_from_display_prop_p)
4025 it->area = TEXT_AREA;
4026
4027 prop = get_char_property_and_overlay (make_number (position->charpos),
4028 Qdisplay, object, &overlay);
4029 if (NILP (prop))
4030 return HANDLED_NORMALLY;
4031 /* Now OVERLAY is the overlay that gave us this property, or nil
4032 if it was a text property. */
4033
4034 if (!STRINGP (it->string))
4035 object = it->w->buffer;
4036
4037 if (CONSP (prop)
4038 /* Simple properties. */
4039 && !EQ (XCAR (prop), Qimage)
4040 && !EQ (XCAR (prop), Qspace)
4041 && !EQ (XCAR (prop), Qwhen)
4042 && !EQ (XCAR (prop), Qslice)
4043 && !EQ (XCAR (prop), Qspace_width)
4044 && !EQ (XCAR (prop), Qheight)
4045 && !EQ (XCAR (prop), Qraise)
4046 /* Marginal area specifications. */
4047 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4048 && !EQ (XCAR (prop), Qleft_fringe)
4049 && !EQ (XCAR (prop), Qright_fringe)
4050 && !NILP (XCAR (prop)))
4051 {
4052 for (; CONSP (prop); prop = XCDR (prop))
4053 {
4054 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4055 position, display_replaced_p))
4056 {
4057 display_replaced_p = 1;
4058 /* If some text in a string is replaced, `position' no
4059 longer points to the position of `object'. */
4060 if (STRINGP (object))
4061 break;
4062 }
4063 }
4064 }
4065 else if (VECTORP (prop))
4066 {
4067 int i;
4068 for (i = 0; i < ASIZE (prop); ++i)
4069 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4070 position, display_replaced_p))
4071 {
4072 display_replaced_p = 1;
4073 /* If some text in a string is replaced, `position' no
4074 longer points to the position of `object'. */
4075 if (STRINGP (object))
4076 break;
4077 }
4078 }
4079 else
4080 {
4081 if (handle_single_display_spec (it, prop, object, overlay,
4082 position, 0))
4083 display_replaced_p = 1;
4084 }
4085
4086 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4087 }
4088
4089
4090 /* Value is the position of the end of the `display' property starting
4091 at START_POS in OBJECT. */
4092
4093 static struct text_pos
4094 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4095 {
4096 Lisp_Object end;
4097 struct text_pos end_pos;
4098
4099 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4100 Qdisplay, object, Qnil);
4101 CHARPOS (end_pos) = XFASTINT (end);
4102 if (STRINGP (object))
4103 compute_string_pos (&end_pos, start_pos, it->string);
4104 else
4105 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4106
4107 return end_pos;
4108 }
4109
4110
4111 /* Set up IT from a single `display' specification PROP. OBJECT
4112 is the object in which the `display' property was found. *POSITION
4113 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4114 means that we previously saw a display specification which already
4115 replaced text display with something else, for example an image;
4116 we ignore such properties after the first one has been processed.
4117
4118 OVERLAY is the overlay this `display' property came from,
4119 or nil if it was a text property.
4120
4121 If PROP is a `space' or `image' specification, and in some other
4122 cases too, set *POSITION to the position where the `display'
4123 property ends.
4124
4125 Value is non-zero if something was found which replaces the display
4126 of buffer or string text. */
4127
4128 static int
4129 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4130 Lisp_Object overlay, struct text_pos *position,
4131 int display_replaced_before_p)
4132 {
4133 Lisp_Object form;
4134 Lisp_Object location, value;
4135 struct text_pos start_pos, save_pos;
4136 int valid_p;
4137
4138 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4139 If the result is non-nil, use VALUE instead of SPEC. */
4140 form = Qt;
4141 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4142 {
4143 spec = XCDR (spec);
4144 if (!CONSP (spec))
4145 return 0;
4146 form = XCAR (spec);
4147 spec = XCDR (spec);
4148 }
4149
4150 if (!NILP (form) && !EQ (form, Qt))
4151 {
4152 int count = SPECPDL_INDEX ();
4153 struct gcpro gcpro1;
4154
4155 /* Bind `object' to the object having the `display' property, a
4156 buffer or string. Bind `position' to the position in the
4157 object where the property was found, and `buffer-position'
4158 to the current position in the buffer. */
4159 specbind (Qobject, object);
4160 specbind (Qposition, make_number (CHARPOS (*position)));
4161 specbind (Qbuffer_position,
4162 make_number (STRINGP (object)
4163 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4164 GCPRO1 (form);
4165 form = safe_eval (form);
4166 UNGCPRO;
4167 unbind_to (count, Qnil);
4168 }
4169
4170 if (NILP (form))
4171 return 0;
4172
4173 /* Handle `(height HEIGHT)' specifications. */
4174 if (CONSP (spec)
4175 && EQ (XCAR (spec), Qheight)
4176 && CONSP (XCDR (spec)))
4177 {
4178 if (!FRAME_WINDOW_P (it->f))
4179 return 0;
4180
4181 it->font_height = XCAR (XCDR (spec));
4182 if (!NILP (it->font_height))
4183 {
4184 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4185 int new_height = -1;
4186
4187 if (CONSP (it->font_height)
4188 && (EQ (XCAR (it->font_height), Qplus)
4189 || EQ (XCAR (it->font_height), Qminus))
4190 && CONSP (XCDR (it->font_height))
4191 && INTEGERP (XCAR (XCDR (it->font_height))))
4192 {
4193 /* `(+ N)' or `(- N)' where N is an integer. */
4194 int steps = XINT (XCAR (XCDR (it->font_height)));
4195 if (EQ (XCAR (it->font_height), Qplus))
4196 steps = - steps;
4197 it->face_id = smaller_face (it->f, it->face_id, steps);
4198 }
4199 else if (FUNCTIONP (it->font_height))
4200 {
4201 /* Call function with current height as argument.
4202 Value is the new height. */
4203 Lisp_Object height;
4204 height = safe_call1 (it->font_height,
4205 face->lface[LFACE_HEIGHT_INDEX]);
4206 if (NUMBERP (height))
4207 new_height = XFLOATINT (height);
4208 }
4209 else if (NUMBERP (it->font_height))
4210 {
4211 /* Value is a multiple of the canonical char height. */
4212 struct face *face;
4213
4214 face = FACE_FROM_ID (it->f,
4215 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4216 new_height = (XFLOATINT (it->font_height)
4217 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4218 }
4219 else
4220 {
4221 /* Evaluate IT->font_height with `height' bound to the
4222 current specified height to get the new height. */
4223 int count = SPECPDL_INDEX ();
4224
4225 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4226 value = safe_eval (it->font_height);
4227 unbind_to (count, Qnil);
4228
4229 if (NUMBERP (value))
4230 new_height = XFLOATINT (value);
4231 }
4232
4233 if (new_height > 0)
4234 it->face_id = face_with_height (it->f, it->face_id, new_height);
4235 }
4236
4237 return 0;
4238 }
4239
4240 /* Handle `(space-width WIDTH)'. */
4241 if (CONSP (spec)
4242 && EQ (XCAR (spec), Qspace_width)
4243 && CONSP (XCDR (spec)))
4244 {
4245 if (!FRAME_WINDOW_P (it->f))
4246 return 0;
4247
4248 value = XCAR (XCDR (spec));
4249 if (NUMBERP (value) && XFLOATINT (value) > 0)
4250 it->space_width = value;
4251
4252 return 0;
4253 }
4254
4255 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4256 if (CONSP (spec)
4257 && EQ (XCAR (spec), Qslice))
4258 {
4259 Lisp_Object tem;
4260
4261 if (!FRAME_WINDOW_P (it->f))
4262 return 0;
4263
4264 if (tem = XCDR (spec), CONSP (tem))
4265 {
4266 it->slice.x = XCAR (tem);
4267 if (tem = XCDR (tem), CONSP (tem))
4268 {
4269 it->slice.y = XCAR (tem);
4270 if (tem = XCDR (tem), CONSP (tem))
4271 {
4272 it->slice.width = XCAR (tem);
4273 if (tem = XCDR (tem), CONSP (tem))
4274 it->slice.height = XCAR (tem);
4275 }
4276 }
4277 }
4278
4279 return 0;
4280 }
4281
4282 /* Handle `(raise FACTOR)'. */
4283 if (CONSP (spec)
4284 && EQ (XCAR (spec), Qraise)
4285 && CONSP (XCDR (spec)))
4286 {
4287 if (!FRAME_WINDOW_P (it->f))
4288 return 0;
4289
4290 #ifdef HAVE_WINDOW_SYSTEM
4291 value = XCAR (XCDR (spec));
4292 if (NUMBERP (value))
4293 {
4294 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4295 it->voffset = - (XFLOATINT (value)
4296 * (FONT_HEIGHT (face->font)));
4297 }
4298 #endif /* HAVE_WINDOW_SYSTEM */
4299
4300 return 0;
4301 }
4302
4303 /* Don't handle the other kinds of display specifications
4304 inside a string that we got from a `display' property. */
4305 if (it->string_from_display_prop_p)
4306 return 0;
4307
4308 /* Characters having this form of property are not displayed, so
4309 we have to find the end of the property. */
4310 start_pos = *position;
4311 *position = display_prop_end (it, object, start_pos);
4312 value = Qnil;
4313
4314 /* Stop the scan at that end position--we assume that all
4315 text properties change there. */
4316 it->stop_charpos = position->charpos;
4317
4318 /* Handle `(left-fringe BITMAP [FACE])'
4319 and `(right-fringe BITMAP [FACE])'. */
4320 if (CONSP (spec)
4321 && (EQ (XCAR (spec), Qleft_fringe)
4322 || EQ (XCAR (spec), Qright_fringe))
4323 && CONSP (XCDR (spec)))
4324 {
4325 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4326 int fringe_bitmap;
4327
4328 if (!FRAME_WINDOW_P (it->f))
4329 /* If we return here, POSITION has been advanced
4330 across the text with this property. */
4331 return 0;
4332
4333 #ifdef HAVE_WINDOW_SYSTEM
4334 value = XCAR (XCDR (spec));
4335 if (!SYMBOLP (value)
4336 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4337 /* If we return here, POSITION has been advanced
4338 across the text with this property. */
4339 return 0;
4340
4341 if (CONSP (XCDR (XCDR (spec))))
4342 {
4343 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4344 int face_id2 = lookup_derived_face (it->f, face_name,
4345 FRINGE_FACE_ID, 0);
4346 if (face_id2 >= 0)
4347 face_id = face_id2;
4348 }
4349
4350 /* Save current settings of IT so that we can restore them
4351 when we are finished with the glyph property value. */
4352
4353 save_pos = it->position;
4354 it->position = *position;
4355 push_it (it);
4356 it->position = save_pos;
4357
4358 it->area = TEXT_AREA;
4359 it->what = IT_IMAGE;
4360 it->image_id = -1; /* no image */
4361 it->position = start_pos;
4362 it->object = NILP (object) ? it->w->buffer : object;
4363 it->method = GET_FROM_IMAGE;
4364 it->from_overlay = Qnil;
4365 it->face_id = face_id;
4366
4367 /* Say that we haven't consumed the characters with
4368 `display' property yet. The call to pop_it in
4369 set_iterator_to_next will clean this up. */
4370 *position = start_pos;
4371
4372 if (EQ (XCAR (spec), Qleft_fringe))
4373 {
4374 it->left_user_fringe_bitmap = fringe_bitmap;
4375 it->left_user_fringe_face_id = face_id;
4376 }
4377 else
4378 {
4379 it->right_user_fringe_bitmap = fringe_bitmap;
4380 it->right_user_fringe_face_id = face_id;
4381 }
4382 #endif /* HAVE_WINDOW_SYSTEM */
4383 return 1;
4384 }
4385
4386 /* Prepare to handle `((margin left-margin) ...)',
4387 `((margin right-margin) ...)' and `((margin nil) ...)'
4388 prefixes for display specifications. */
4389 location = Qunbound;
4390 if (CONSP (spec) && CONSP (XCAR (spec)))
4391 {
4392 Lisp_Object tem;
4393
4394 value = XCDR (spec);
4395 if (CONSP (value))
4396 value = XCAR (value);
4397
4398 tem = XCAR (spec);
4399 if (EQ (XCAR (tem), Qmargin)
4400 && (tem = XCDR (tem),
4401 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4402 (NILP (tem)
4403 || EQ (tem, Qleft_margin)
4404 || EQ (tem, Qright_margin))))
4405 location = tem;
4406 }
4407
4408 if (EQ (location, Qunbound))
4409 {
4410 location = Qnil;
4411 value = spec;
4412 }
4413
4414 /* After this point, VALUE is the property after any
4415 margin prefix has been stripped. It must be a string,
4416 an image specification, or `(space ...)'.
4417
4418 LOCATION specifies where to display: `left-margin',
4419 `right-margin' or nil. */
4420
4421 valid_p = (STRINGP (value)
4422 #ifdef HAVE_WINDOW_SYSTEM
4423 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4424 #endif /* not HAVE_WINDOW_SYSTEM */
4425 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4426
4427 if (valid_p && !display_replaced_before_p)
4428 {
4429 /* Save current settings of IT so that we can restore them
4430 when we are finished with the glyph property value. */
4431 save_pos = it->position;
4432 it->position = *position;
4433 push_it (it);
4434 it->position = save_pos;
4435 it->from_overlay = overlay;
4436
4437 if (NILP (location))
4438 it->area = TEXT_AREA;
4439 else if (EQ (location, Qleft_margin))
4440 it->area = LEFT_MARGIN_AREA;
4441 else
4442 it->area = RIGHT_MARGIN_AREA;
4443
4444 if (STRINGP (value))
4445 {
4446 it->string = value;
4447 it->multibyte_p = STRING_MULTIBYTE (it->string);
4448 it->current.overlay_string_index = -1;
4449 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4450 it->end_charpos = it->string_nchars = SCHARS (it->string);
4451 it->method = GET_FROM_STRING;
4452 it->stop_charpos = 0;
4453 it->string_from_display_prop_p = 1;
4454 /* Say that we haven't consumed the characters with
4455 `display' property yet. The call to pop_it in
4456 set_iterator_to_next will clean this up. */
4457 if (BUFFERP (object))
4458 *position = start_pos;
4459 }
4460 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4461 {
4462 it->method = GET_FROM_STRETCH;
4463 it->object = value;
4464 *position = it->position = start_pos;
4465 }
4466 #ifdef HAVE_WINDOW_SYSTEM
4467 else
4468 {
4469 it->what = IT_IMAGE;
4470 it->image_id = lookup_image (it->f, value);
4471 it->position = start_pos;
4472 it->object = NILP (object) ? it->w->buffer : object;
4473 it->method = GET_FROM_IMAGE;
4474
4475 /* Say that we haven't consumed the characters with
4476 `display' property yet. The call to pop_it in
4477 set_iterator_to_next will clean this up. */
4478 *position = start_pos;
4479 }
4480 #endif /* HAVE_WINDOW_SYSTEM */
4481
4482 return 1;
4483 }
4484
4485 /* Invalid property or property not supported. Restore
4486 POSITION to what it was before. */
4487 *position = start_pos;
4488 return 0;
4489 }
4490
4491
4492 /* Check if SPEC is a display sub-property value whose text should be
4493 treated as intangible. */
4494
4495 static int
4496 single_display_spec_intangible_p (Lisp_Object prop)
4497 {
4498 /* Skip over `when FORM'. */
4499 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4500 {
4501 prop = XCDR (prop);
4502 if (!CONSP (prop))
4503 return 0;
4504 prop = XCDR (prop);
4505 }
4506
4507 if (STRINGP (prop))
4508 return 1;
4509
4510 if (!CONSP (prop))
4511 return 0;
4512
4513 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4514 we don't need to treat text as intangible. */
4515 if (EQ (XCAR (prop), Qmargin))
4516 {
4517 prop = XCDR (prop);
4518 if (!CONSP (prop))
4519 return 0;
4520
4521 prop = XCDR (prop);
4522 if (!CONSP (prop)
4523 || EQ (XCAR (prop), Qleft_margin)
4524 || EQ (XCAR (prop), Qright_margin))
4525 return 0;
4526 }
4527
4528 return (CONSP (prop)
4529 && (EQ (XCAR (prop), Qimage)
4530 || EQ (XCAR (prop), Qspace)));
4531 }
4532
4533
4534 /* Check if PROP is a display property value whose text should be
4535 treated as intangible. */
4536
4537 int
4538 display_prop_intangible_p (Lisp_Object prop)
4539 {
4540 if (CONSP (prop)
4541 && CONSP (XCAR (prop))
4542 && !EQ (Qmargin, XCAR (XCAR (prop))))
4543 {
4544 /* A list of sub-properties. */
4545 while (CONSP (prop))
4546 {
4547 if (single_display_spec_intangible_p (XCAR (prop)))
4548 return 1;
4549 prop = XCDR (prop);
4550 }
4551 }
4552 else if (VECTORP (prop))
4553 {
4554 /* A vector of sub-properties. */
4555 int i;
4556 for (i = 0; i < ASIZE (prop); ++i)
4557 if (single_display_spec_intangible_p (AREF (prop, i)))
4558 return 1;
4559 }
4560 else
4561 return single_display_spec_intangible_p (prop);
4562
4563 return 0;
4564 }
4565
4566
4567 /* Return 1 if PROP is a display sub-property value containing STRING. */
4568
4569 static int
4570 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4571 {
4572 if (EQ (string, prop))
4573 return 1;
4574
4575 /* Skip over `when FORM'. */
4576 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4577 {
4578 prop = XCDR (prop);
4579 if (!CONSP (prop))
4580 return 0;
4581 prop = XCDR (prop);
4582 }
4583
4584 if (CONSP (prop))
4585 /* Skip over `margin LOCATION'. */
4586 if (EQ (XCAR (prop), Qmargin))
4587 {
4588 prop = XCDR (prop);
4589 if (!CONSP (prop))
4590 return 0;
4591
4592 prop = XCDR (prop);
4593 if (!CONSP (prop))
4594 return 0;
4595 }
4596
4597 return CONSP (prop) && EQ (XCAR (prop), string);
4598 }
4599
4600
4601 /* Return 1 if STRING appears in the `display' property PROP. */
4602
4603 static int
4604 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4605 {
4606 if (CONSP (prop)
4607 && CONSP (XCAR (prop))
4608 && !EQ (Qmargin, XCAR (XCAR (prop))))
4609 {
4610 /* A list of sub-properties. */
4611 while (CONSP (prop))
4612 {
4613 if (single_display_spec_string_p (XCAR (prop), string))
4614 return 1;
4615 prop = XCDR (prop);
4616 }
4617 }
4618 else if (VECTORP (prop))
4619 {
4620 /* A vector of sub-properties. */
4621 int i;
4622 for (i = 0; i < ASIZE (prop); ++i)
4623 if (single_display_spec_string_p (AREF (prop, i), string))
4624 return 1;
4625 }
4626 else
4627 return single_display_spec_string_p (prop, string);
4628
4629 return 0;
4630 }
4631
4632 /* Look for STRING in overlays and text properties in W's buffer,
4633 between character positions FROM and TO (excluding TO).
4634 BACK_P non-zero means look back (in this case, TO is supposed to be
4635 less than FROM).
4636 Value is the first character position where STRING was found, or
4637 zero if it wasn't found before hitting TO.
4638
4639 W's buffer must be current.
4640
4641 This function may only use code that doesn't eval because it is
4642 called asynchronously from note_mouse_highlight. */
4643
4644 static EMACS_INT
4645 string_buffer_position_lim (struct window *w, Lisp_Object string,
4646 EMACS_INT from, EMACS_INT to, int back_p)
4647 {
4648 Lisp_Object limit, prop, pos;
4649 int found = 0;
4650
4651 pos = make_number (from);
4652
4653 if (!back_p) /* looking forward */
4654 {
4655 limit = make_number (min (to, ZV));
4656 while (!found && !EQ (pos, limit))
4657 {
4658 prop = Fget_char_property (pos, Qdisplay, Qnil);
4659 if (!NILP (prop) && display_prop_string_p (prop, string))
4660 found = 1;
4661 else
4662 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4663 limit);
4664 }
4665 }
4666 else /* looking back */
4667 {
4668 limit = make_number (max (to, BEGV));
4669 while (!found && !EQ (pos, limit))
4670 {
4671 prop = Fget_char_property (pos, Qdisplay, Qnil);
4672 if (!NILP (prop) && display_prop_string_p (prop, string))
4673 found = 1;
4674 else
4675 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4676 limit);
4677 }
4678 }
4679
4680 return found ? XINT (pos) : 0;
4681 }
4682
4683 /* Determine which buffer position in W's buffer STRING comes from.
4684 AROUND_CHARPOS is an approximate position where it could come from.
4685 Value is the buffer position or 0 if it couldn't be determined.
4686
4687 W's buffer must be current.
4688
4689 This function is necessary because we don't record buffer positions
4690 in glyphs generated from strings (to keep struct glyph small).
4691 This function may only use code that doesn't eval because it is
4692 called asynchronously from note_mouse_highlight. */
4693
4694 EMACS_INT
4695 string_buffer_position (struct window *w, Lisp_Object string, EMACS_INT around_charpos)
4696 {
4697 const int MAX_DISTANCE = 1000;
4698 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4699 around_charpos + MAX_DISTANCE,
4700 0);
4701
4702 if (!found)
4703 found = string_buffer_position_lim (w, string, around_charpos,
4704 around_charpos - MAX_DISTANCE, 1);
4705 return found;
4706 }
4707
4708
4709 \f
4710 /***********************************************************************
4711 `composition' property
4712 ***********************************************************************/
4713
4714 /* Set up iterator IT from `composition' property at its current
4715 position. Called from handle_stop. */
4716
4717 static enum prop_handled
4718 handle_composition_prop (struct it *it)
4719 {
4720 Lisp_Object prop, string;
4721 EMACS_INT pos, pos_byte, start, end;
4722
4723 if (STRINGP (it->string))
4724 {
4725 unsigned char *s;
4726
4727 pos = IT_STRING_CHARPOS (*it);
4728 pos_byte = IT_STRING_BYTEPOS (*it);
4729 string = it->string;
4730 s = SDATA (string) + pos_byte;
4731 it->c = STRING_CHAR (s);
4732 }
4733 else
4734 {
4735 pos = IT_CHARPOS (*it);
4736 pos_byte = IT_BYTEPOS (*it);
4737 string = Qnil;
4738 it->c = FETCH_CHAR (pos_byte);
4739 }
4740
4741 /* If there's a valid composition and point is not inside of the
4742 composition (in the case that the composition is from the current
4743 buffer), draw a glyph composed from the composition components. */
4744 if (find_composition (pos, -1, &start, &end, &prop, string)
4745 && COMPOSITION_VALID_P (start, end, prop)
4746 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4747 {
4748 if (start != pos)
4749 {
4750 if (STRINGP (it->string))
4751 pos_byte = string_char_to_byte (it->string, start);
4752 else
4753 pos_byte = CHAR_TO_BYTE (start);
4754 }
4755 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4756 prop, string);
4757
4758 if (it->cmp_it.id >= 0)
4759 {
4760 it->cmp_it.ch = -1;
4761 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4762 it->cmp_it.nglyphs = -1;
4763 }
4764 }
4765
4766 return HANDLED_NORMALLY;
4767 }
4768
4769
4770 \f
4771 /***********************************************************************
4772 Overlay strings
4773 ***********************************************************************/
4774
4775 /* The following structure is used to record overlay strings for
4776 later sorting in load_overlay_strings. */
4777
4778 struct overlay_entry
4779 {
4780 Lisp_Object overlay;
4781 Lisp_Object string;
4782 int priority;
4783 int after_string_p;
4784 };
4785
4786
4787 /* Set up iterator IT from overlay strings at its current position.
4788 Called from handle_stop. */
4789
4790 static enum prop_handled
4791 handle_overlay_change (struct it *it)
4792 {
4793 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4794 return HANDLED_RECOMPUTE_PROPS;
4795 else
4796 return HANDLED_NORMALLY;
4797 }
4798
4799
4800 /* Set up the next overlay string for delivery by IT, if there is an
4801 overlay string to deliver. Called by set_iterator_to_next when the
4802 end of the current overlay string is reached. If there are more
4803 overlay strings to display, IT->string and
4804 IT->current.overlay_string_index are set appropriately here.
4805 Otherwise IT->string is set to nil. */
4806
4807 static void
4808 next_overlay_string (struct it *it)
4809 {
4810 ++it->current.overlay_string_index;
4811 if (it->current.overlay_string_index == it->n_overlay_strings)
4812 {
4813 /* No more overlay strings. Restore IT's settings to what
4814 they were before overlay strings were processed, and
4815 continue to deliver from current_buffer. */
4816
4817 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4818 pop_it (it);
4819 xassert (it->sp > 0
4820 || (NILP (it->string)
4821 && it->method == GET_FROM_BUFFER
4822 && it->stop_charpos >= BEGV
4823 && it->stop_charpos <= it->end_charpos));
4824 it->current.overlay_string_index = -1;
4825 it->n_overlay_strings = 0;
4826
4827 /* If we're at the end of the buffer, record that we have
4828 processed the overlay strings there already, so that
4829 next_element_from_buffer doesn't try it again. */
4830 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4831 it->overlay_strings_at_end_processed_p = 1;
4832 }
4833 else
4834 {
4835 /* There are more overlay strings to process. If
4836 IT->current.overlay_string_index has advanced to a position
4837 where we must load IT->overlay_strings with more strings, do
4838 it. */
4839 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4840
4841 if (it->current.overlay_string_index && i == 0)
4842 load_overlay_strings (it, 0);
4843
4844 /* Initialize IT to deliver display elements from the overlay
4845 string. */
4846 it->string = it->overlay_strings[i];
4847 it->multibyte_p = STRING_MULTIBYTE (it->string);
4848 SET_TEXT_POS (it->current.string_pos, 0, 0);
4849 it->method = GET_FROM_STRING;
4850 it->stop_charpos = 0;
4851 if (it->cmp_it.stop_pos >= 0)
4852 it->cmp_it.stop_pos = 0;
4853 }
4854
4855 CHECK_IT (it);
4856 }
4857
4858
4859 /* Compare two overlay_entry structures E1 and E2. Used as a
4860 comparison function for qsort in load_overlay_strings. Overlay
4861 strings for the same position are sorted so that
4862
4863 1. All after-strings come in front of before-strings, except
4864 when they come from the same overlay.
4865
4866 2. Within after-strings, strings are sorted so that overlay strings
4867 from overlays with higher priorities come first.
4868
4869 2. Within before-strings, strings are sorted so that overlay
4870 strings from overlays with higher priorities come last.
4871
4872 Value is analogous to strcmp. */
4873
4874
4875 static int
4876 compare_overlay_entries (const void *e1, const void *e2)
4877 {
4878 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4879 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4880 int result;
4881
4882 if (entry1->after_string_p != entry2->after_string_p)
4883 {
4884 /* Let after-strings appear in front of before-strings if
4885 they come from different overlays. */
4886 if (EQ (entry1->overlay, entry2->overlay))
4887 result = entry1->after_string_p ? 1 : -1;
4888 else
4889 result = entry1->after_string_p ? -1 : 1;
4890 }
4891 else if (entry1->after_string_p)
4892 /* After-strings sorted in order of decreasing priority. */
4893 result = entry2->priority - entry1->priority;
4894 else
4895 /* Before-strings sorted in order of increasing priority. */
4896 result = entry1->priority - entry2->priority;
4897
4898 return result;
4899 }
4900
4901
4902 /* Load the vector IT->overlay_strings with overlay strings from IT's
4903 current buffer position, or from CHARPOS if that is > 0. Set
4904 IT->n_overlays to the total number of overlay strings found.
4905
4906 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4907 a time. On entry into load_overlay_strings,
4908 IT->current.overlay_string_index gives the number of overlay
4909 strings that have already been loaded by previous calls to this
4910 function.
4911
4912 IT->add_overlay_start contains an additional overlay start
4913 position to consider for taking overlay strings from, if non-zero.
4914 This position comes into play when the overlay has an `invisible'
4915 property, and both before and after-strings. When we've skipped to
4916 the end of the overlay, because of its `invisible' property, we
4917 nevertheless want its before-string to appear.
4918 IT->add_overlay_start will contain the overlay start position
4919 in this case.
4920
4921 Overlay strings are sorted so that after-string strings come in
4922 front of before-string strings. Within before and after-strings,
4923 strings are sorted by overlay priority. See also function
4924 compare_overlay_entries. */
4925
4926 static void
4927 load_overlay_strings (struct it *it, EMACS_INT charpos)
4928 {
4929 Lisp_Object overlay, window, str, invisible;
4930 struct Lisp_Overlay *ov;
4931 EMACS_INT start, end;
4932 int size = 20;
4933 int n = 0, i, j, invis_p;
4934 struct overlay_entry *entries
4935 = (struct overlay_entry *) alloca (size * sizeof *entries);
4936
4937 if (charpos <= 0)
4938 charpos = IT_CHARPOS (*it);
4939
4940 /* Append the overlay string STRING of overlay OVERLAY to vector
4941 `entries' which has size `size' and currently contains `n'
4942 elements. AFTER_P non-zero means STRING is an after-string of
4943 OVERLAY. */
4944 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4945 do \
4946 { \
4947 Lisp_Object priority; \
4948 \
4949 if (n == size) \
4950 { \
4951 int new_size = 2 * size; \
4952 struct overlay_entry *old = entries; \
4953 entries = \
4954 (struct overlay_entry *) alloca (new_size \
4955 * sizeof *entries); \
4956 memcpy (entries, old, size * sizeof *entries); \
4957 size = new_size; \
4958 } \
4959 \
4960 entries[n].string = (STRING); \
4961 entries[n].overlay = (OVERLAY); \
4962 priority = Foverlay_get ((OVERLAY), Qpriority); \
4963 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4964 entries[n].after_string_p = (AFTER_P); \
4965 ++n; \
4966 } \
4967 while (0)
4968
4969 /* Process overlay before the overlay center. */
4970 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4971 {
4972 XSETMISC (overlay, ov);
4973 xassert (OVERLAYP (overlay));
4974 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4975 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4976
4977 if (end < charpos)
4978 break;
4979
4980 /* Skip this overlay if it doesn't start or end at IT's current
4981 position. */
4982 if (end != charpos && start != charpos)
4983 continue;
4984
4985 /* Skip this overlay if it doesn't apply to IT->w. */
4986 window = Foverlay_get (overlay, Qwindow);
4987 if (WINDOWP (window) && XWINDOW (window) != it->w)
4988 continue;
4989
4990 /* If the text ``under'' the overlay is invisible, both before-
4991 and after-strings from this overlay are visible; start and
4992 end position are indistinguishable. */
4993 invisible = Foverlay_get (overlay, Qinvisible);
4994 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4995
4996 /* If overlay has a non-empty before-string, record it. */
4997 if ((start == charpos || (end == charpos && invis_p))
4998 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4999 && SCHARS (str))
5000 RECORD_OVERLAY_STRING (overlay, str, 0);
5001
5002 /* If overlay has a non-empty after-string, record it. */
5003 if ((end == charpos || (start == charpos && invis_p))
5004 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5005 && SCHARS (str))
5006 RECORD_OVERLAY_STRING (overlay, str, 1);
5007 }
5008
5009 /* Process overlays after the overlay center. */
5010 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5011 {
5012 XSETMISC (overlay, ov);
5013 xassert (OVERLAYP (overlay));
5014 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5015 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5016
5017 if (start > charpos)
5018 break;
5019
5020 /* Skip this overlay if it doesn't start or end at IT's current
5021 position. */
5022 if (end != charpos && start != charpos)
5023 continue;
5024
5025 /* Skip this overlay if it doesn't apply to IT->w. */
5026 window = Foverlay_get (overlay, Qwindow);
5027 if (WINDOWP (window) && XWINDOW (window) != it->w)
5028 continue;
5029
5030 /* If the text ``under'' the overlay is invisible, it has a zero
5031 dimension, and both before- and after-strings apply. */
5032 invisible = Foverlay_get (overlay, Qinvisible);
5033 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5034
5035 /* If overlay has a non-empty before-string, record it. */
5036 if ((start == charpos || (end == charpos && invis_p))
5037 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5038 && SCHARS (str))
5039 RECORD_OVERLAY_STRING (overlay, str, 0);
5040
5041 /* If overlay has a non-empty after-string, record it. */
5042 if ((end == charpos || (start == charpos && invis_p))
5043 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5044 && SCHARS (str))
5045 RECORD_OVERLAY_STRING (overlay, str, 1);
5046 }
5047
5048 #undef RECORD_OVERLAY_STRING
5049
5050 /* Sort entries. */
5051 if (n > 1)
5052 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5053
5054 /* Record the total number of strings to process. */
5055 it->n_overlay_strings = n;
5056
5057 /* IT->current.overlay_string_index is the number of overlay strings
5058 that have already been consumed by IT. Copy some of the
5059 remaining overlay strings to IT->overlay_strings. */
5060 i = 0;
5061 j = it->current.overlay_string_index;
5062 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5063 {
5064 it->overlay_strings[i] = entries[j].string;
5065 it->string_overlays[i++] = entries[j++].overlay;
5066 }
5067
5068 CHECK_IT (it);
5069 }
5070
5071
5072 /* Get the first chunk of overlay strings at IT's current buffer
5073 position, or at CHARPOS if that is > 0. Value is non-zero if at
5074 least one overlay string was found. */
5075
5076 static int
5077 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5078 {
5079 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5080 process. This fills IT->overlay_strings with strings, and sets
5081 IT->n_overlay_strings to the total number of strings to process.
5082 IT->pos.overlay_string_index has to be set temporarily to zero
5083 because load_overlay_strings needs this; it must be set to -1
5084 when no overlay strings are found because a zero value would
5085 indicate a position in the first overlay string. */
5086 it->current.overlay_string_index = 0;
5087 load_overlay_strings (it, charpos);
5088
5089 /* If we found overlay strings, set up IT to deliver display
5090 elements from the first one. Otherwise set up IT to deliver
5091 from current_buffer. */
5092 if (it->n_overlay_strings)
5093 {
5094 /* Make sure we know settings in current_buffer, so that we can
5095 restore meaningful values when we're done with the overlay
5096 strings. */
5097 if (compute_stop_p)
5098 compute_stop_pos (it);
5099 xassert (it->face_id >= 0);
5100
5101 /* Save IT's settings. They are restored after all overlay
5102 strings have been processed. */
5103 xassert (!compute_stop_p || it->sp == 0);
5104
5105 /* When called from handle_stop, there might be an empty display
5106 string loaded. In that case, don't bother saving it. */
5107 if (!STRINGP (it->string) || SCHARS (it->string))
5108 push_it (it);
5109
5110 /* Set up IT to deliver display elements from the first overlay
5111 string. */
5112 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5113 it->string = it->overlay_strings[0];
5114 it->from_overlay = Qnil;
5115 it->stop_charpos = 0;
5116 xassert (STRINGP (it->string));
5117 it->end_charpos = SCHARS (it->string);
5118 it->multibyte_p = STRING_MULTIBYTE (it->string);
5119 it->method = GET_FROM_STRING;
5120 return 1;
5121 }
5122
5123 it->current.overlay_string_index = -1;
5124 return 0;
5125 }
5126
5127 static int
5128 get_overlay_strings (struct it *it, EMACS_INT charpos)
5129 {
5130 it->string = Qnil;
5131 it->method = GET_FROM_BUFFER;
5132
5133 (void) get_overlay_strings_1 (it, charpos, 1);
5134
5135 CHECK_IT (it);
5136
5137 /* Value is non-zero if we found at least one overlay string. */
5138 return STRINGP (it->string);
5139 }
5140
5141
5142 \f
5143 /***********************************************************************
5144 Saving and restoring state
5145 ***********************************************************************/
5146
5147 /* Save current settings of IT on IT->stack. Called, for example,
5148 before setting up IT for an overlay string, to be able to restore
5149 IT's settings to what they were after the overlay string has been
5150 processed. */
5151
5152 static void
5153 push_it (struct it *it)
5154 {
5155 struct iterator_stack_entry *p;
5156
5157 xassert (it->sp < IT_STACK_SIZE);
5158 p = it->stack + it->sp;
5159
5160 p->stop_charpos = it->stop_charpos;
5161 p->prev_stop = it->prev_stop;
5162 p->base_level_stop = it->base_level_stop;
5163 p->cmp_it = it->cmp_it;
5164 xassert (it->face_id >= 0);
5165 p->face_id = it->face_id;
5166 p->string = it->string;
5167 p->method = it->method;
5168 p->from_overlay = it->from_overlay;
5169 switch (p->method)
5170 {
5171 case GET_FROM_IMAGE:
5172 p->u.image.object = it->object;
5173 p->u.image.image_id = it->image_id;
5174 p->u.image.slice = it->slice;
5175 break;
5176 case GET_FROM_STRETCH:
5177 p->u.stretch.object = it->object;
5178 break;
5179 }
5180 p->position = it->position;
5181 p->current = it->current;
5182 p->end_charpos = it->end_charpos;
5183 p->string_nchars = it->string_nchars;
5184 p->area = it->area;
5185 p->multibyte_p = it->multibyte_p;
5186 p->avoid_cursor_p = it->avoid_cursor_p;
5187 p->space_width = it->space_width;
5188 p->font_height = it->font_height;
5189 p->voffset = it->voffset;
5190 p->string_from_display_prop_p = it->string_from_display_prop_p;
5191 p->display_ellipsis_p = 0;
5192 p->line_wrap = it->line_wrap;
5193 ++it->sp;
5194 }
5195
5196 static void
5197 iterate_out_of_display_property (struct it *it)
5198 {
5199 /* Maybe initialize paragraph direction. If we are at the beginning
5200 of a new paragraph, next_element_from_buffer may not have a
5201 chance to do that. */
5202 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5203 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5204 /* prev_stop can be zero, so check against BEGV as well. */
5205 while (it->bidi_it.charpos >= BEGV
5206 && it->prev_stop <= it->bidi_it.charpos
5207 && it->bidi_it.charpos < CHARPOS (it->position))
5208 bidi_move_to_visually_next (&it->bidi_it);
5209 /* Record the stop_pos we just crossed, for when we cross it
5210 back, maybe. */
5211 if (it->bidi_it.charpos > CHARPOS (it->position))
5212 it->prev_stop = CHARPOS (it->position);
5213 /* If we ended up not where pop_it put us, resync IT's
5214 positional members with the bidi iterator. */
5215 if (it->bidi_it.charpos != CHARPOS (it->position))
5216 {
5217 SET_TEXT_POS (it->position,
5218 it->bidi_it.charpos, it->bidi_it.bytepos);
5219 it->current.pos = it->position;
5220 }
5221 }
5222
5223 /* Restore IT's settings from IT->stack. Called, for example, when no
5224 more overlay strings must be processed, and we return to delivering
5225 display elements from a buffer, or when the end of a string from a
5226 `display' property is reached and we return to delivering display
5227 elements from an overlay string, or from a buffer. */
5228
5229 static void
5230 pop_it (struct it *it)
5231 {
5232 struct iterator_stack_entry *p;
5233
5234 xassert (it->sp > 0);
5235 --it->sp;
5236 p = it->stack + it->sp;
5237 it->stop_charpos = p->stop_charpos;
5238 it->prev_stop = p->prev_stop;
5239 it->base_level_stop = p->base_level_stop;
5240 it->cmp_it = p->cmp_it;
5241 it->face_id = p->face_id;
5242 it->current = p->current;
5243 it->position = p->position;
5244 it->string = p->string;
5245 it->from_overlay = p->from_overlay;
5246 if (NILP (it->string))
5247 SET_TEXT_POS (it->current.string_pos, -1, -1);
5248 it->method = p->method;
5249 switch (it->method)
5250 {
5251 case GET_FROM_IMAGE:
5252 it->image_id = p->u.image.image_id;
5253 it->object = p->u.image.object;
5254 it->slice = p->u.image.slice;
5255 break;
5256 case GET_FROM_STRETCH:
5257 it->object = p->u.comp.object;
5258 break;
5259 case GET_FROM_BUFFER:
5260 it->object = it->w->buffer;
5261 if (it->bidi_p)
5262 {
5263 /* Bidi-iterate until we get out of the portion of text, if
5264 any, covered by a `display' text property or an overlay
5265 with `display' property. (We cannot just jump there,
5266 because the internal coherency of the bidi iterator state
5267 can not be preserved across such jumps.) We also must
5268 determine the paragraph base direction if the overlay we
5269 just processed is at the beginning of a new
5270 paragraph. */
5271 iterate_out_of_display_property (it);
5272 }
5273 break;
5274 case GET_FROM_STRING:
5275 it->object = it->string;
5276 break;
5277 case GET_FROM_DISPLAY_VECTOR:
5278 if (it->s)
5279 it->method = GET_FROM_C_STRING;
5280 else if (STRINGP (it->string))
5281 it->method = GET_FROM_STRING;
5282 else
5283 {
5284 it->method = GET_FROM_BUFFER;
5285 it->object = it->w->buffer;
5286 }
5287 }
5288 it->end_charpos = p->end_charpos;
5289 it->string_nchars = p->string_nchars;
5290 it->area = p->area;
5291 it->multibyte_p = p->multibyte_p;
5292 it->avoid_cursor_p = p->avoid_cursor_p;
5293 it->space_width = p->space_width;
5294 it->font_height = p->font_height;
5295 it->voffset = p->voffset;
5296 it->string_from_display_prop_p = p->string_from_display_prop_p;
5297 it->line_wrap = p->line_wrap;
5298 }
5299
5300
5301 \f
5302 /***********************************************************************
5303 Moving over lines
5304 ***********************************************************************/
5305
5306 /* Set IT's current position to the previous line start. */
5307
5308 static void
5309 back_to_previous_line_start (struct it *it)
5310 {
5311 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5312 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5313 }
5314
5315
5316 /* Move IT to the next line start.
5317
5318 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5319 we skipped over part of the text (as opposed to moving the iterator
5320 continuously over the text). Otherwise, don't change the value
5321 of *SKIPPED_P.
5322
5323 Newlines may come from buffer text, overlay strings, or strings
5324 displayed via the `display' property. That's the reason we can't
5325 simply use find_next_newline_no_quit.
5326
5327 Note that this function may not skip over invisible text that is so
5328 because of text properties and immediately follows a newline. If
5329 it would, function reseat_at_next_visible_line_start, when called
5330 from set_iterator_to_next, would effectively make invisible
5331 characters following a newline part of the wrong glyph row, which
5332 leads to wrong cursor motion. */
5333
5334 static int
5335 forward_to_next_line_start (struct it *it, int *skipped_p)
5336 {
5337 int old_selective, newline_found_p, n;
5338 const int MAX_NEWLINE_DISTANCE = 500;
5339
5340 /* If already on a newline, just consume it to avoid unintended
5341 skipping over invisible text below. */
5342 if (it->what == IT_CHARACTER
5343 && it->c == '\n'
5344 && CHARPOS (it->position) == IT_CHARPOS (*it))
5345 {
5346 set_iterator_to_next (it, 0);
5347 it->c = 0;
5348 return 1;
5349 }
5350
5351 /* Don't handle selective display in the following. It's (a)
5352 unnecessary because it's done by the caller, and (b) leads to an
5353 infinite recursion because next_element_from_ellipsis indirectly
5354 calls this function. */
5355 old_selective = it->selective;
5356 it->selective = 0;
5357
5358 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5359 from buffer text. */
5360 for (n = newline_found_p = 0;
5361 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5362 n += STRINGP (it->string) ? 0 : 1)
5363 {
5364 if (!get_next_display_element (it))
5365 return 0;
5366 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5367 set_iterator_to_next (it, 0);
5368 }
5369
5370 /* If we didn't find a newline near enough, see if we can use a
5371 short-cut. */
5372 if (!newline_found_p)
5373 {
5374 EMACS_INT start = IT_CHARPOS (*it);
5375 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5376 Lisp_Object pos;
5377
5378 xassert (!STRINGP (it->string));
5379
5380 /* If there isn't any `display' property in sight, and no
5381 overlays, we can just use the position of the newline in
5382 buffer text. */
5383 if (it->stop_charpos >= limit
5384 || ((pos = Fnext_single_property_change (make_number (start),
5385 Qdisplay,
5386 Qnil, make_number (limit)),
5387 NILP (pos))
5388 && next_overlay_change (start) == ZV))
5389 {
5390 IT_CHARPOS (*it) = limit;
5391 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5392 *skipped_p = newline_found_p = 1;
5393 }
5394 else
5395 {
5396 while (get_next_display_element (it)
5397 && !newline_found_p)
5398 {
5399 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5400 set_iterator_to_next (it, 0);
5401 }
5402 }
5403 }
5404
5405 it->selective = old_selective;
5406 return newline_found_p;
5407 }
5408
5409
5410 /* Set IT's current position to the previous visible line start. Skip
5411 invisible text that is so either due to text properties or due to
5412 selective display. Caution: this does not change IT->current_x and
5413 IT->hpos. */
5414
5415 static void
5416 back_to_previous_visible_line_start (struct it *it)
5417 {
5418 while (IT_CHARPOS (*it) > BEGV)
5419 {
5420 back_to_previous_line_start (it);
5421
5422 if (IT_CHARPOS (*it) <= BEGV)
5423 break;
5424
5425 /* If selective > 0, then lines indented more than its value are
5426 invisible. */
5427 if (it->selective > 0
5428 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5429 (double) it->selective)) /* iftc */
5430 continue;
5431
5432 /* Check the newline before point for invisibility. */
5433 {
5434 Lisp_Object prop;
5435 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5436 Qinvisible, it->window);
5437 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5438 continue;
5439 }
5440
5441 if (IT_CHARPOS (*it) <= BEGV)
5442 break;
5443
5444 {
5445 struct it it2;
5446 EMACS_INT pos;
5447 EMACS_INT beg, end;
5448 Lisp_Object val, overlay;
5449
5450 /* If newline is part of a composition, continue from start of composition */
5451 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5452 && beg < IT_CHARPOS (*it))
5453 goto replaced;
5454
5455 /* If newline is replaced by a display property, find start of overlay
5456 or interval and continue search from that point. */
5457 it2 = *it;
5458 pos = --IT_CHARPOS (it2);
5459 --IT_BYTEPOS (it2);
5460 it2.sp = 0;
5461 it2.string_from_display_prop_p = 0;
5462 if (handle_display_prop (&it2) == HANDLED_RETURN
5463 && !NILP (val = get_char_property_and_overlay
5464 (make_number (pos), Qdisplay, Qnil, &overlay))
5465 && (OVERLAYP (overlay)
5466 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5467 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5468 goto replaced;
5469
5470 /* Newline is not replaced by anything -- so we are done. */
5471 break;
5472
5473 replaced:
5474 if (beg < BEGV)
5475 beg = BEGV;
5476 IT_CHARPOS (*it) = beg;
5477 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5478 }
5479 }
5480
5481 it->continuation_lines_width = 0;
5482
5483 xassert (IT_CHARPOS (*it) >= BEGV);
5484 xassert (IT_CHARPOS (*it) == BEGV
5485 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5486 CHECK_IT (it);
5487 }
5488
5489
5490 /* Reseat iterator IT at the previous visible line start. Skip
5491 invisible text that is so either due to text properties or due to
5492 selective display. At the end, update IT's overlay information,
5493 face information etc. */
5494
5495 void
5496 reseat_at_previous_visible_line_start (struct it *it)
5497 {
5498 back_to_previous_visible_line_start (it);
5499 reseat (it, it->current.pos, 1);
5500 CHECK_IT (it);
5501 }
5502
5503
5504 /* Reseat iterator IT on the next visible line start in the current
5505 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5506 preceding the line start. Skip over invisible text that is so
5507 because of selective display. Compute faces, overlays etc at the
5508 new position. Note that this function does not skip over text that
5509 is invisible because of text properties. */
5510
5511 static void
5512 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5513 {
5514 int newline_found_p, skipped_p = 0;
5515
5516 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5517
5518 /* Skip over lines that are invisible because they are indented
5519 more than the value of IT->selective. */
5520 if (it->selective > 0)
5521 while (IT_CHARPOS (*it) < ZV
5522 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5523 (double) it->selective)) /* iftc */
5524 {
5525 xassert (IT_BYTEPOS (*it) == BEGV
5526 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5527 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5528 }
5529
5530 /* Position on the newline if that's what's requested. */
5531 if (on_newline_p && newline_found_p)
5532 {
5533 if (STRINGP (it->string))
5534 {
5535 if (IT_STRING_CHARPOS (*it) > 0)
5536 {
5537 --IT_STRING_CHARPOS (*it);
5538 --IT_STRING_BYTEPOS (*it);
5539 }
5540 }
5541 else if (IT_CHARPOS (*it) > BEGV)
5542 {
5543 --IT_CHARPOS (*it);
5544 --IT_BYTEPOS (*it);
5545 reseat (it, it->current.pos, 0);
5546 }
5547 }
5548 else if (skipped_p)
5549 reseat (it, it->current.pos, 0);
5550
5551 CHECK_IT (it);
5552 }
5553
5554
5555 \f
5556 /***********************************************************************
5557 Changing an iterator's position
5558 ***********************************************************************/
5559
5560 /* Change IT's current position to POS in current_buffer. If FORCE_P
5561 is non-zero, always check for text properties at the new position.
5562 Otherwise, text properties are only looked up if POS >=
5563 IT->check_charpos of a property. */
5564
5565 static void
5566 reseat (struct it *it, struct text_pos pos, int force_p)
5567 {
5568 EMACS_INT original_pos = IT_CHARPOS (*it);
5569
5570 reseat_1 (it, pos, 0);
5571
5572 /* Determine where to check text properties. Avoid doing it
5573 where possible because text property lookup is very expensive. */
5574 if (force_p
5575 || CHARPOS (pos) > it->stop_charpos
5576 || CHARPOS (pos) < original_pos)
5577 {
5578 if (it->bidi_p)
5579 {
5580 /* For bidi iteration, we need to prime prev_stop and
5581 base_level_stop with our best estimations. */
5582 if (CHARPOS (pos) < it->prev_stop)
5583 {
5584 handle_stop_backwards (it, BEGV);
5585 if (CHARPOS (pos) < it->base_level_stop)
5586 it->base_level_stop = 0;
5587 }
5588 else if (CHARPOS (pos) > it->stop_charpos
5589 && it->stop_charpos >= BEGV)
5590 handle_stop_backwards (it, it->stop_charpos);
5591 else /* force_p */
5592 handle_stop (it);
5593 }
5594 else
5595 {
5596 handle_stop (it);
5597 it->prev_stop = it->base_level_stop = 0;
5598 }
5599
5600 }
5601
5602 CHECK_IT (it);
5603 }
5604
5605
5606 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5607 IT->stop_pos to POS, also. */
5608
5609 static void
5610 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5611 {
5612 /* Don't call this function when scanning a C string. */
5613 xassert (it->s == NULL);
5614
5615 /* POS must be a reasonable value. */
5616 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5617
5618 it->current.pos = it->position = pos;
5619 it->end_charpos = ZV;
5620 it->dpvec = NULL;
5621 it->current.dpvec_index = -1;
5622 it->current.overlay_string_index = -1;
5623 IT_STRING_CHARPOS (*it) = -1;
5624 IT_STRING_BYTEPOS (*it) = -1;
5625 it->string = Qnil;
5626 it->string_from_display_prop_p = 0;
5627 it->method = GET_FROM_BUFFER;
5628 it->object = it->w->buffer;
5629 it->area = TEXT_AREA;
5630 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5631 it->sp = 0;
5632 it->string_from_display_prop_p = 0;
5633 it->face_before_selective_p = 0;
5634 if (it->bidi_p)
5635 {
5636 it->bidi_it.first_elt = 1;
5637 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5638 }
5639
5640 if (set_stop_p)
5641 {
5642 it->stop_charpos = CHARPOS (pos);
5643 it->base_level_stop = CHARPOS (pos);
5644 }
5645 }
5646
5647
5648 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5649 If S is non-null, it is a C string to iterate over. Otherwise,
5650 STRING gives a Lisp string to iterate over.
5651
5652 If PRECISION > 0, don't return more then PRECISION number of
5653 characters from the string.
5654
5655 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5656 characters have been returned. FIELD_WIDTH < 0 means an infinite
5657 field width.
5658
5659 MULTIBYTE = 0 means disable processing of multibyte characters,
5660 MULTIBYTE > 0 means enable it,
5661 MULTIBYTE < 0 means use IT->multibyte_p.
5662
5663 IT must be initialized via a prior call to init_iterator before
5664 calling this function. */
5665
5666 static void
5667 reseat_to_string (struct it *it, const unsigned char *s, Lisp_Object string,
5668 EMACS_INT charpos, EMACS_INT precision, int field_width,
5669 int multibyte)
5670 {
5671 /* No region in strings. */
5672 it->region_beg_charpos = it->region_end_charpos = -1;
5673
5674 /* No text property checks performed by default, but see below. */
5675 it->stop_charpos = -1;
5676
5677 /* Set iterator position and end position. */
5678 memset (&it->current, 0, sizeof it->current);
5679 it->current.overlay_string_index = -1;
5680 it->current.dpvec_index = -1;
5681 xassert (charpos >= 0);
5682
5683 /* If STRING is specified, use its multibyteness, otherwise use the
5684 setting of MULTIBYTE, if specified. */
5685 if (multibyte >= 0)
5686 it->multibyte_p = multibyte > 0;
5687
5688 if (s == NULL)
5689 {
5690 xassert (STRINGP (string));
5691 it->string = string;
5692 it->s = NULL;
5693 it->end_charpos = it->string_nchars = SCHARS (string);
5694 it->method = GET_FROM_STRING;
5695 it->current.string_pos = string_pos (charpos, string);
5696 }
5697 else
5698 {
5699 it->s = s;
5700 it->string = Qnil;
5701
5702 /* Note that we use IT->current.pos, not it->current.string_pos,
5703 for displaying C strings. */
5704 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5705 if (it->multibyte_p)
5706 {
5707 it->current.pos = c_string_pos (charpos, s, 1);
5708 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5709 }
5710 else
5711 {
5712 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5713 it->end_charpos = it->string_nchars = strlen (s);
5714 }
5715
5716 it->method = GET_FROM_C_STRING;
5717 }
5718
5719 /* PRECISION > 0 means don't return more than PRECISION characters
5720 from the string. */
5721 if (precision > 0 && it->end_charpos - charpos > precision)
5722 it->end_charpos = it->string_nchars = charpos + precision;
5723
5724 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5725 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5726 FIELD_WIDTH < 0 means infinite field width. This is useful for
5727 padding with `-' at the end of a mode line. */
5728 if (field_width < 0)
5729 field_width = INFINITY;
5730 if (field_width > it->end_charpos - charpos)
5731 it->end_charpos = charpos + field_width;
5732
5733 /* Use the standard display table for displaying strings. */
5734 if (DISP_TABLE_P (Vstandard_display_table))
5735 it->dp = XCHAR_TABLE (Vstandard_display_table);
5736
5737 it->stop_charpos = charpos;
5738 if (s == NULL && it->multibyte_p)
5739 {
5740 EMACS_INT endpos = SCHARS (it->string);
5741 if (endpos > it->end_charpos)
5742 endpos = it->end_charpos;
5743 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5744 it->string);
5745 }
5746 CHECK_IT (it);
5747 }
5748
5749
5750 \f
5751 /***********************************************************************
5752 Iteration
5753 ***********************************************************************/
5754
5755 /* Map enum it_method value to corresponding next_element_from_* function. */
5756
5757 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5758 {
5759 next_element_from_buffer,
5760 next_element_from_display_vector,
5761 next_element_from_string,
5762 next_element_from_c_string,
5763 next_element_from_image,
5764 next_element_from_stretch
5765 };
5766
5767 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5768
5769
5770 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5771 (possibly with the following characters). */
5772
5773 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5774 ((IT)->cmp_it.id >= 0 \
5775 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5776 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5777 END_CHARPOS, (IT)->w, \
5778 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5779 (IT)->string)))
5780
5781
5782 /* Lookup the char-table Vglyphless_char_display for character C (-1
5783 if we want information for no-font case), and return the display
5784 method symbol. By side-effect, update it->what and
5785 it->glyphless_method. This function is called from
5786 get_next_display_element for each character element, and from
5787 x_produce_glyphs when no suitable font was found. */
5788
5789 Lisp_Object
5790 lookup_glyphless_char_display (int c, struct it *it)
5791 {
5792 Lisp_Object glyphless_method = Qnil;
5793
5794 if (CHAR_TABLE_P (Vglyphless_char_display)
5795 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5796 glyphless_method = (c >= 0
5797 ? CHAR_TABLE_REF (Vglyphless_char_display, c)
5798 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
5799 retry:
5800 if (NILP (glyphless_method))
5801 {
5802 if (c >= 0)
5803 /* The default is to display the character by a proper font. */
5804 return Qnil;
5805 /* The default for the no-font case is to display an empty box. */
5806 glyphless_method = Qempty_box;
5807 }
5808 if (EQ (glyphless_method, Qzero_width))
5809 {
5810 if (c >= 0)
5811 return glyphless_method;
5812 /* This method can't be used for the no-font case. */
5813 glyphless_method = Qempty_box;
5814 }
5815 if (EQ (glyphless_method, Qthin_space))
5816 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5817 else if (EQ (glyphless_method, Qempty_box))
5818 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5819 else if (EQ (glyphless_method, Qhex_code))
5820 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5821 else if (STRINGP (glyphless_method))
5822 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5823 else
5824 {
5825 /* Invalid value. We use the default method. */
5826 glyphless_method = Qnil;
5827 goto retry;
5828 }
5829 it->what = IT_GLYPHLESS;
5830 return glyphless_method;
5831 }
5832
5833 /* Load IT's display element fields with information about the next
5834 display element from the current position of IT. Value is zero if
5835 end of buffer (or C string) is reached. */
5836
5837 static struct frame *last_escape_glyph_frame = NULL;
5838 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5839 static int last_escape_glyph_merged_face_id = 0;
5840
5841 struct frame *last_glyphless_glyph_frame = NULL;
5842 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5843 int last_glyphless_glyph_merged_face_id = 0;
5844
5845 int
5846 get_next_display_element (struct it *it)
5847 {
5848 /* Non-zero means that we found a display element. Zero means that
5849 we hit the end of what we iterate over. Performance note: the
5850 function pointer `method' used here turns out to be faster than
5851 using a sequence of if-statements. */
5852 int success_p;
5853
5854 get_next:
5855 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5856
5857 if (it->what == IT_CHARACTER)
5858 {
5859 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5860 and only if (a) the resolved directionality of that character
5861 is R..." */
5862 /* FIXME: Do we need an exception for characters from display
5863 tables? */
5864 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5865 it->c = bidi_mirror_char (it->c);
5866 /* Map via display table or translate control characters.
5867 IT->c, IT->len etc. have been set to the next character by
5868 the function call above. If we have a display table, and it
5869 contains an entry for IT->c, translate it. Don't do this if
5870 IT->c itself comes from a display table, otherwise we could
5871 end up in an infinite recursion. (An alternative could be to
5872 count the recursion depth of this function and signal an
5873 error when a certain maximum depth is reached.) Is it worth
5874 it? */
5875 if (success_p && it->dpvec == NULL)
5876 {
5877 Lisp_Object dv;
5878 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5879 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5880 nbsp_or_shy = char_is_other;
5881 int c = it->c; /* This is the character to display. */
5882
5883 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5884 {
5885 xassert (SINGLE_BYTE_CHAR_P (c));
5886 if (unibyte_display_via_language_environment)
5887 {
5888 c = DECODE_CHAR (unibyte, c);
5889 if (c < 0)
5890 c = BYTE8_TO_CHAR (it->c);
5891 }
5892 else
5893 c = BYTE8_TO_CHAR (it->c);
5894 }
5895
5896 if (it->dp
5897 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5898 VECTORP (dv)))
5899 {
5900 struct Lisp_Vector *v = XVECTOR (dv);
5901
5902 /* Return the first character from the display table
5903 entry, if not empty. If empty, don't display the
5904 current character. */
5905 if (v->size)
5906 {
5907 it->dpvec_char_len = it->len;
5908 it->dpvec = v->contents;
5909 it->dpend = v->contents + v->size;
5910 it->current.dpvec_index = 0;
5911 it->dpvec_face_id = -1;
5912 it->saved_face_id = it->face_id;
5913 it->method = GET_FROM_DISPLAY_VECTOR;
5914 it->ellipsis_p = 0;
5915 }
5916 else
5917 {
5918 set_iterator_to_next (it, 0);
5919 }
5920 goto get_next;
5921 }
5922
5923 if (! NILP (lookup_glyphless_char_display (c, it)))
5924 {
5925 if (it->what == IT_GLYPHLESS)
5926 goto done;
5927 /* Don't display this character. */
5928 set_iterator_to_next (it, 0);
5929 goto get_next;
5930 }
5931
5932 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5933 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5934 : c == 0xAD ? char_is_soft_hyphen
5935 : char_is_other);
5936
5937 /* Translate control characters into `\003' or `^C' form.
5938 Control characters coming from a display table entry are
5939 currently not translated because we use IT->dpvec to hold
5940 the translation. This could easily be changed but I
5941 don't believe that it is worth doing.
5942
5943 NBSP and SOFT-HYPEN are property translated too.
5944
5945 Non-printable characters and raw-byte characters are also
5946 translated to octal form. */
5947 if (((c < ' ' || c == 127) /* ASCII control chars */
5948 ? (it->area != TEXT_AREA
5949 /* In mode line, treat \n, \t like other crl chars. */
5950 || (c != '\t'
5951 && it->glyph_row
5952 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5953 || (c != '\n' && c != '\t'))
5954 : (nbsp_or_shy
5955 || CHAR_BYTE8_P (c)
5956 || ! CHAR_PRINTABLE_P (c))))
5957 {
5958 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5959 or a non-printable character which must be displayed
5960 either as '\003' or as `^C' where the '\\' and '^'
5961 can be defined in the display table. Fill
5962 IT->ctl_chars with glyphs for what we have to
5963 display. Then, set IT->dpvec to these glyphs. */
5964 Lisp_Object gc;
5965 int ctl_len;
5966 int face_id, lface_id = 0 ;
5967 int escape_glyph;
5968
5969 /* Handle control characters with ^. */
5970
5971 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5972 {
5973 int g;
5974
5975 g = '^'; /* default glyph for Control */
5976 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5977 if (it->dp
5978 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5979 && GLYPH_CODE_CHAR_VALID_P (gc))
5980 {
5981 g = GLYPH_CODE_CHAR (gc);
5982 lface_id = GLYPH_CODE_FACE (gc);
5983 }
5984 if (lface_id)
5985 {
5986 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5987 }
5988 else if (it->f == last_escape_glyph_frame
5989 && it->face_id == last_escape_glyph_face_id)
5990 {
5991 face_id = last_escape_glyph_merged_face_id;
5992 }
5993 else
5994 {
5995 /* Merge the escape-glyph face into the current face. */
5996 face_id = merge_faces (it->f, Qescape_glyph, 0,
5997 it->face_id);
5998 last_escape_glyph_frame = it->f;
5999 last_escape_glyph_face_id = it->face_id;
6000 last_escape_glyph_merged_face_id = face_id;
6001 }
6002
6003 XSETINT (it->ctl_chars[0], g);
6004 XSETINT (it->ctl_chars[1], c ^ 0100);
6005 ctl_len = 2;
6006 goto display_control;
6007 }
6008
6009 /* Handle non-break space in the mode where it only gets
6010 highlighting. */
6011
6012 if (EQ (Vnobreak_char_display, Qt)
6013 && nbsp_or_shy == char_is_nbsp)
6014 {
6015 /* Merge the no-break-space face into the current face. */
6016 face_id = merge_faces (it->f, Qnobreak_space, 0,
6017 it->face_id);
6018
6019 c = ' ';
6020 XSETINT (it->ctl_chars[0], ' ');
6021 ctl_len = 1;
6022 goto display_control;
6023 }
6024
6025 /* Handle sequences that start with the "escape glyph". */
6026
6027 /* the default escape glyph is \. */
6028 escape_glyph = '\\';
6029
6030 if (it->dp
6031 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6032 && GLYPH_CODE_CHAR_VALID_P (gc))
6033 {
6034 escape_glyph = GLYPH_CODE_CHAR (gc);
6035 lface_id = GLYPH_CODE_FACE (gc);
6036 }
6037 if (lface_id)
6038 {
6039 /* The display table specified a face.
6040 Merge it into face_id and also into escape_glyph. */
6041 face_id = merge_faces (it->f, Qt, lface_id,
6042 it->face_id);
6043 }
6044 else if (it->f == last_escape_glyph_frame
6045 && it->face_id == last_escape_glyph_face_id)
6046 {
6047 face_id = last_escape_glyph_merged_face_id;
6048 }
6049 else
6050 {
6051 /* Merge the escape-glyph face into the current face. */
6052 face_id = merge_faces (it->f, Qescape_glyph, 0,
6053 it->face_id);
6054 last_escape_glyph_frame = it->f;
6055 last_escape_glyph_face_id = it->face_id;
6056 last_escape_glyph_merged_face_id = face_id;
6057 }
6058
6059 /* Handle soft hyphens in the mode where they only get
6060 highlighting. */
6061
6062 if (EQ (Vnobreak_char_display, Qt)
6063 && nbsp_or_shy == char_is_soft_hyphen)
6064 {
6065 XSETINT (it->ctl_chars[0], '-');
6066 ctl_len = 1;
6067 goto display_control;
6068 }
6069
6070 /* Handle non-break space and soft hyphen
6071 with the escape glyph. */
6072
6073 if (nbsp_or_shy)
6074 {
6075 XSETINT (it->ctl_chars[0], escape_glyph);
6076 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6077 XSETINT (it->ctl_chars[1], c);
6078 ctl_len = 2;
6079 goto display_control;
6080 }
6081
6082 {
6083 char str[10];
6084 int len, i;
6085
6086 if (CHAR_BYTE8_P (c))
6087 /* Display \200 instead of \17777600. */
6088 c = CHAR_TO_BYTE8 (c);
6089 len = sprintf (str, "%03o", c);
6090
6091 XSETINT (it->ctl_chars[0], escape_glyph);
6092 for (i = 0; i < len; i++)
6093 XSETINT (it->ctl_chars[i + 1], str[i]);
6094 ctl_len = len + 1;
6095 }
6096
6097 display_control:
6098 /* Set up IT->dpvec and return first character from it. */
6099 it->dpvec_char_len = it->len;
6100 it->dpvec = it->ctl_chars;
6101 it->dpend = it->dpvec + ctl_len;
6102 it->current.dpvec_index = 0;
6103 it->dpvec_face_id = face_id;
6104 it->saved_face_id = it->face_id;
6105 it->method = GET_FROM_DISPLAY_VECTOR;
6106 it->ellipsis_p = 0;
6107 goto get_next;
6108 }
6109 it->char_to_display = c;
6110 }
6111 else if (success_p)
6112 {
6113 it->char_to_display = it->c;
6114 }
6115 }
6116
6117 #ifdef HAVE_WINDOW_SYSTEM
6118 /* Adjust face id for a multibyte character. There are no multibyte
6119 character in unibyte text. */
6120 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6121 && it->multibyte_p
6122 && success_p
6123 && FRAME_WINDOW_P (it->f))
6124 {
6125 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6126
6127 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6128 {
6129 /* Automatic composition with glyph-string. */
6130 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6131
6132 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6133 }
6134 else
6135 {
6136 EMACS_INT pos = (it->s ? -1
6137 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6138 : IT_CHARPOS (*it));
6139
6140 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6141 it->string);
6142 }
6143 }
6144 #endif
6145
6146 done:
6147 /* Is this character the last one of a run of characters with
6148 box? If yes, set IT->end_of_box_run_p to 1. */
6149 if (it->face_box_p
6150 && it->s == NULL)
6151 {
6152 if (it->method == GET_FROM_STRING && it->sp)
6153 {
6154 int face_id = underlying_face_id (it);
6155 struct face *face = FACE_FROM_ID (it->f, face_id);
6156
6157 if (face)
6158 {
6159 if (face->box == FACE_NO_BOX)
6160 {
6161 /* If the box comes from face properties in a
6162 display string, check faces in that string. */
6163 int string_face_id = face_after_it_pos (it);
6164 it->end_of_box_run_p
6165 = (FACE_FROM_ID (it->f, string_face_id)->box
6166 == FACE_NO_BOX);
6167 }
6168 /* Otherwise, the box comes from the underlying face.
6169 If this is the last string character displayed, check
6170 the next buffer location. */
6171 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6172 && (it->current.overlay_string_index
6173 == it->n_overlay_strings - 1))
6174 {
6175 EMACS_INT ignore;
6176 int next_face_id;
6177 struct text_pos pos = it->current.pos;
6178 INC_TEXT_POS (pos, it->multibyte_p);
6179
6180 next_face_id = face_at_buffer_position
6181 (it->w, CHARPOS (pos), it->region_beg_charpos,
6182 it->region_end_charpos, &ignore,
6183 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6184 -1);
6185 it->end_of_box_run_p
6186 = (FACE_FROM_ID (it->f, next_face_id)->box
6187 == FACE_NO_BOX);
6188 }
6189 }
6190 }
6191 else
6192 {
6193 int face_id = face_after_it_pos (it);
6194 it->end_of_box_run_p
6195 = (face_id != it->face_id
6196 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6197 }
6198 }
6199
6200 /* Value is 0 if end of buffer or string reached. */
6201 return success_p;
6202 }
6203
6204
6205 /* Move IT to the next display element.
6206
6207 RESEAT_P non-zero means if called on a newline in buffer text,
6208 skip to the next visible line start.
6209
6210 Functions get_next_display_element and set_iterator_to_next are
6211 separate because I find this arrangement easier to handle than a
6212 get_next_display_element function that also increments IT's
6213 position. The way it is we can first look at an iterator's current
6214 display element, decide whether it fits on a line, and if it does,
6215 increment the iterator position. The other way around we probably
6216 would either need a flag indicating whether the iterator has to be
6217 incremented the next time, or we would have to implement a
6218 decrement position function which would not be easy to write. */
6219
6220 void
6221 set_iterator_to_next (struct it *it, int reseat_p)
6222 {
6223 /* Reset flags indicating start and end of a sequence of characters
6224 with box. Reset them at the start of this function because
6225 moving the iterator to a new position might set them. */
6226 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6227
6228 switch (it->method)
6229 {
6230 case GET_FROM_BUFFER:
6231 /* The current display element of IT is a character from
6232 current_buffer. Advance in the buffer, and maybe skip over
6233 invisible lines that are so because of selective display. */
6234 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6235 reseat_at_next_visible_line_start (it, 0);
6236 else if (it->cmp_it.id >= 0)
6237 {
6238 /* We are currently getting glyphs from a composition. */
6239 int i;
6240
6241 if (! it->bidi_p)
6242 {
6243 IT_CHARPOS (*it) += it->cmp_it.nchars;
6244 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6245 if (it->cmp_it.to < it->cmp_it.nglyphs)
6246 {
6247 it->cmp_it.from = it->cmp_it.to;
6248 }
6249 else
6250 {
6251 it->cmp_it.id = -1;
6252 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6253 IT_BYTEPOS (*it),
6254 it->end_charpos, Qnil);
6255 }
6256 }
6257 else if (! it->cmp_it.reversed_p)
6258 {
6259 /* Composition created while scanning forward. */
6260 /* Update IT's char/byte positions to point to the first
6261 character of the next grapheme cluster, or to the
6262 character visually after the current composition. */
6263 for (i = 0; i < it->cmp_it.nchars; i++)
6264 bidi_move_to_visually_next (&it->bidi_it);
6265 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6266 IT_CHARPOS (*it) = it->bidi_it.charpos;
6267
6268 if (it->cmp_it.to < it->cmp_it.nglyphs)
6269 {
6270 /* Proceed to the next grapheme cluster. */
6271 it->cmp_it.from = it->cmp_it.to;
6272 }
6273 else
6274 {
6275 /* No more grapheme clusters in this composition.
6276 Find the next stop position. */
6277 EMACS_INT stop = it->end_charpos;
6278 if (it->bidi_it.scan_dir < 0)
6279 /* Now we are scanning backward and don't know
6280 where to stop. */
6281 stop = -1;
6282 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6283 IT_BYTEPOS (*it), stop, Qnil);
6284 }
6285 }
6286 else
6287 {
6288 /* Composition created while scanning backward. */
6289 /* Update IT's char/byte positions to point to the last
6290 character of the previous grapheme cluster, or the
6291 character visually after the current composition. */
6292 for (i = 0; i < it->cmp_it.nchars; i++)
6293 bidi_move_to_visually_next (&it->bidi_it);
6294 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6295 IT_CHARPOS (*it) = it->bidi_it.charpos;
6296 if (it->cmp_it.from > 0)
6297 {
6298 /* Proceed to the previous grapheme cluster. */
6299 it->cmp_it.to = it->cmp_it.from;
6300 }
6301 else
6302 {
6303 /* No more grapheme clusters in this composition.
6304 Find the next stop position. */
6305 EMACS_INT stop = it->end_charpos;
6306 if (it->bidi_it.scan_dir < 0)
6307 /* Now we are scanning backward and don't know
6308 where to stop. */
6309 stop = -1;
6310 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6311 IT_BYTEPOS (*it), stop, Qnil);
6312 }
6313 }
6314 }
6315 else
6316 {
6317 xassert (it->len != 0);
6318
6319 if (!it->bidi_p)
6320 {
6321 IT_BYTEPOS (*it) += it->len;
6322 IT_CHARPOS (*it) += 1;
6323 }
6324 else
6325 {
6326 int prev_scan_dir = it->bidi_it.scan_dir;
6327 /* If this is a new paragraph, determine its base
6328 direction (a.k.a. its base embedding level). */
6329 if (it->bidi_it.new_paragraph)
6330 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6331 bidi_move_to_visually_next (&it->bidi_it);
6332 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6333 IT_CHARPOS (*it) = it->bidi_it.charpos;
6334 if (prev_scan_dir != it->bidi_it.scan_dir)
6335 {
6336 /* As the scan direction was changed, we must
6337 re-compute the stop position for composition. */
6338 EMACS_INT stop = it->end_charpos;
6339 if (it->bidi_it.scan_dir < 0)
6340 stop = -1;
6341 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6342 IT_BYTEPOS (*it), stop, Qnil);
6343 }
6344 }
6345 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6346 }
6347 break;
6348
6349 case GET_FROM_C_STRING:
6350 /* Current display element of IT is from a C string. */
6351 IT_BYTEPOS (*it) += it->len;
6352 IT_CHARPOS (*it) += 1;
6353 break;
6354
6355 case GET_FROM_DISPLAY_VECTOR:
6356 /* Current display element of IT is from a display table entry.
6357 Advance in the display table definition. Reset it to null if
6358 end reached, and continue with characters from buffers/
6359 strings. */
6360 ++it->current.dpvec_index;
6361
6362 /* Restore face of the iterator to what they were before the
6363 display vector entry (these entries may contain faces). */
6364 it->face_id = it->saved_face_id;
6365
6366 if (it->dpvec + it->current.dpvec_index == it->dpend)
6367 {
6368 int recheck_faces = it->ellipsis_p;
6369
6370 if (it->s)
6371 it->method = GET_FROM_C_STRING;
6372 else if (STRINGP (it->string))
6373 it->method = GET_FROM_STRING;
6374 else
6375 {
6376 it->method = GET_FROM_BUFFER;
6377 it->object = it->w->buffer;
6378 }
6379
6380 it->dpvec = NULL;
6381 it->current.dpvec_index = -1;
6382
6383 /* Skip over characters which were displayed via IT->dpvec. */
6384 if (it->dpvec_char_len < 0)
6385 reseat_at_next_visible_line_start (it, 1);
6386 else if (it->dpvec_char_len > 0)
6387 {
6388 if (it->method == GET_FROM_STRING
6389 && it->n_overlay_strings > 0)
6390 it->ignore_overlay_strings_at_pos_p = 1;
6391 it->len = it->dpvec_char_len;
6392 set_iterator_to_next (it, reseat_p);
6393 }
6394
6395 /* Maybe recheck faces after display vector */
6396 if (recheck_faces)
6397 it->stop_charpos = IT_CHARPOS (*it);
6398 }
6399 break;
6400
6401 case GET_FROM_STRING:
6402 /* Current display element is a character from a Lisp string. */
6403 xassert (it->s == NULL && STRINGP (it->string));
6404 if (it->cmp_it.id >= 0)
6405 {
6406 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6407 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6408 if (it->cmp_it.to < it->cmp_it.nglyphs)
6409 it->cmp_it.from = it->cmp_it.to;
6410 else
6411 {
6412 it->cmp_it.id = -1;
6413 composition_compute_stop_pos (&it->cmp_it,
6414 IT_STRING_CHARPOS (*it),
6415 IT_STRING_BYTEPOS (*it),
6416 it->end_charpos, it->string);
6417 }
6418 }
6419 else
6420 {
6421 IT_STRING_BYTEPOS (*it) += it->len;
6422 IT_STRING_CHARPOS (*it) += 1;
6423 }
6424
6425 consider_string_end:
6426
6427 if (it->current.overlay_string_index >= 0)
6428 {
6429 /* IT->string is an overlay string. Advance to the
6430 next, if there is one. */
6431 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6432 {
6433 it->ellipsis_p = 0;
6434 next_overlay_string (it);
6435 if (it->ellipsis_p)
6436 setup_for_ellipsis (it, 0);
6437 }
6438 }
6439 else
6440 {
6441 /* IT->string is not an overlay string. If we reached
6442 its end, and there is something on IT->stack, proceed
6443 with what is on the stack. This can be either another
6444 string, this time an overlay string, or a buffer. */
6445 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6446 && it->sp > 0)
6447 {
6448 pop_it (it);
6449 if (it->method == GET_FROM_STRING)
6450 goto consider_string_end;
6451 }
6452 }
6453 break;
6454
6455 case GET_FROM_IMAGE:
6456 case GET_FROM_STRETCH:
6457 /* The position etc with which we have to proceed are on
6458 the stack. The position may be at the end of a string,
6459 if the `display' property takes up the whole string. */
6460 xassert (it->sp > 0);
6461 pop_it (it);
6462 if (it->method == GET_FROM_STRING)
6463 goto consider_string_end;
6464 break;
6465
6466 default:
6467 /* There are no other methods defined, so this should be a bug. */
6468 abort ();
6469 }
6470
6471 xassert (it->method != GET_FROM_STRING
6472 || (STRINGP (it->string)
6473 && IT_STRING_CHARPOS (*it) >= 0));
6474 }
6475
6476 /* Load IT's display element fields with information about the next
6477 display element which comes from a display table entry or from the
6478 result of translating a control character to one of the forms `^C'
6479 or `\003'.
6480
6481 IT->dpvec holds the glyphs to return as characters.
6482 IT->saved_face_id holds the face id before the display vector--it
6483 is restored into IT->face_id in set_iterator_to_next. */
6484
6485 static int
6486 next_element_from_display_vector (struct it *it)
6487 {
6488 Lisp_Object gc;
6489
6490 /* Precondition. */
6491 xassert (it->dpvec && it->current.dpvec_index >= 0);
6492
6493 it->face_id = it->saved_face_id;
6494
6495 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6496 That seemed totally bogus - so I changed it... */
6497 gc = it->dpvec[it->current.dpvec_index];
6498
6499 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6500 {
6501 it->c = GLYPH_CODE_CHAR (gc);
6502 it->len = CHAR_BYTES (it->c);
6503
6504 /* The entry may contain a face id to use. Such a face id is
6505 the id of a Lisp face, not a realized face. A face id of
6506 zero means no face is specified. */
6507 if (it->dpvec_face_id >= 0)
6508 it->face_id = it->dpvec_face_id;
6509 else
6510 {
6511 int lface_id = GLYPH_CODE_FACE (gc);
6512 if (lface_id > 0)
6513 it->face_id = merge_faces (it->f, Qt, lface_id,
6514 it->saved_face_id);
6515 }
6516 }
6517 else
6518 /* Display table entry is invalid. Return a space. */
6519 it->c = ' ', it->len = 1;
6520
6521 /* Don't change position and object of the iterator here. They are
6522 still the values of the character that had this display table
6523 entry or was translated, and that's what we want. */
6524 it->what = IT_CHARACTER;
6525 return 1;
6526 }
6527
6528
6529 /* Load IT with the next display element from Lisp string IT->string.
6530 IT->current.string_pos is the current position within the string.
6531 If IT->current.overlay_string_index >= 0, the Lisp string is an
6532 overlay string. */
6533
6534 static int
6535 next_element_from_string (struct it *it)
6536 {
6537 struct text_pos position;
6538
6539 xassert (STRINGP (it->string));
6540 xassert (IT_STRING_CHARPOS (*it) >= 0);
6541 position = it->current.string_pos;
6542
6543 /* Time to check for invisible text? */
6544 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6545 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6546 {
6547 handle_stop (it);
6548
6549 /* Since a handler may have changed IT->method, we must
6550 recurse here. */
6551 return GET_NEXT_DISPLAY_ELEMENT (it);
6552 }
6553
6554 if (it->current.overlay_string_index >= 0)
6555 {
6556 /* Get the next character from an overlay string. In overlay
6557 strings, There is no field width or padding with spaces to
6558 do. */
6559 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6560 {
6561 it->what = IT_EOB;
6562 return 0;
6563 }
6564 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6565 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6566 && next_element_from_composition (it))
6567 {
6568 return 1;
6569 }
6570 else if (STRING_MULTIBYTE (it->string))
6571 {
6572 const unsigned char *s = (SDATA (it->string)
6573 + IT_STRING_BYTEPOS (*it));
6574 it->c = string_char_and_length (s, &it->len);
6575 }
6576 else
6577 {
6578 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6579 it->len = 1;
6580 }
6581 }
6582 else
6583 {
6584 /* Get the next character from a Lisp string that is not an
6585 overlay string. Such strings come from the mode line, for
6586 example. We may have to pad with spaces, or truncate the
6587 string. See also next_element_from_c_string. */
6588 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6589 {
6590 it->what = IT_EOB;
6591 return 0;
6592 }
6593 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6594 {
6595 /* Pad with spaces. */
6596 it->c = ' ', it->len = 1;
6597 CHARPOS (position) = BYTEPOS (position) = -1;
6598 }
6599 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6600 IT_STRING_BYTEPOS (*it), it->string_nchars)
6601 && next_element_from_composition (it))
6602 {
6603 return 1;
6604 }
6605 else if (STRING_MULTIBYTE (it->string))
6606 {
6607 const unsigned char *s = (SDATA (it->string)
6608 + IT_STRING_BYTEPOS (*it));
6609 it->c = string_char_and_length (s, &it->len);
6610 }
6611 else
6612 {
6613 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6614 it->len = 1;
6615 }
6616 }
6617
6618 /* Record what we have and where it came from. */
6619 it->what = IT_CHARACTER;
6620 it->object = it->string;
6621 it->position = position;
6622 return 1;
6623 }
6624
6625
6626 /* Load IT with next display element from C string IT->s.
6627 IT->string_nchars is the maximum number of characters to return
6628 from the string. IT->end_charpos may be greater than
6629 IT->string_nchars when this function is called, in which case we
6630 may have to return padding spaces. Value is zero if end of string
6631 reached, including padding spaces. */
6632
6633 static int
6634 next_element_from_c_string (struct it *it)
6635 {
6636 int success_p = 1;
6637
6638 xassert (it->s);
6639 it->what = IT_CHARACTER;
6640 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6641 it->object = Qnil;
6642
6643 /* IT's position can be greater IT->string_nchars in case a field
6644 width or precision has been specified when the iterator was
6645 initialized. */
6646 if (IT_CHARPOS (*it) >= it->end_charpos)
6647 {
6648 /* End of the game. */
6649 it->what = IT_EOB;
6650 success_p = 0;
6651 }
6652 else if (IT_CHARPOS (*it) >= it->string_nchars)
6653 {
6654 /* Pad with spaces. */
6655 it->c = ' ', it->len = 1;
6656 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6657 }
6658 else if (it->multibyte_p)
6659 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6660 else
6661 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6662
6663 return success_p;
6664 }
6665
6666
6667 /* Set up IT to return characters from an ellipsis, if appropriate.
6668 The definition of the ellipsis glyphs may come from a display table
6669 entry. This function fills IT with the first glyph from the
6670 ellipsis if an ellipsis is to be displayed. */
6671
6672 static int
6673 next_element_from_ellipsis (struct it *it)
6674 {
6675 if (it->selective_display_ellipsis_p)
6676 setup_for_ellipsis (it, it->len);
6677 else
6678 {
6679 /* The face at the current position may be different from the
6680 face we find after the invisible text. Remember what it
6681 was in IT->saved_face_id, and signal that it's there by
6682 setting face_before_selective_p. */
6683 it->saved_face_id = it->face_id;
6684 it->method = GET_FROM_BUFFER;
6685 it->object = it->w->buffer;
6686 reseat_at_next_visible_line_start (it, 1);
6687 it->face_before_selective_p = 1;
6688 }
6689
6690 return GET_NEXT_DISPLAY_ELEMENT (it);
6691 }
6692
6693
6694 /* Deliver an image display element. The iterator IT is already
6695 filled with image information (done in handle_display_prop). Value
6696 is always 1. */
6697
6698
6699 static int
6700 next_element_from_image (struct it *it)
6701 {
6702 it->what = IT_IMAGE;
6703 it->ignore_overlay_strings_at_pos_p = 0;
6704 return 1;
6705 }
6706
6707
6708 /* Fill iterator IT with next display element from a stretch glyph
6709 property. IT->object is the value of the text property. Value is
6710 always 1. */
6711
6712 static int
6713 next_element_from_stretch (struct it *it)
6714 {
6715 it->what = IT_STRETCH;
6716 return 1;
6717 }
6718
6719 /* Scan forward from CHARPOS in the current buffer, until we find a
6720 stop position > current IT's position. Then handle the stop
6721 position before that. This is called when we bump into a stop
6722 position while reordering bidirectional text. CHARPOS should be
6723 the last previously processed stop_pos (or BEGV, if none were
6724 processed yet) whose position is less that IT's current
6725 position. */
6726
6727 static void
6728 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6729 {
6730 EMACS_INT where_we_are = IT_CHARPOS (*it);
6731 struct display_pos save_current = it->current;
6732 struct text_pos save_position = it->position;
6733 struct text_pos pos1;
6734 EMACS_INT next_stop;
6735
6736 /* Scan in strict logical order. */
6737 it->bidi_p = 0;
6738 do
6739 {
6740 it->prev_stop = charpos;
6741 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6742 reseat_1 (it, pos1, 0);
6743 compute_stop_pos (it);
6744 /* We must advance forward, right? */
6745 if (it->stop_charpos <= it->prev_stop)
6746 abort ();
6747 charpos = it->stop_charpos;
6748 }
6749 while (charpos <= where_we_are);
6750
6751 next_stop = it->stop_charpos;
6752 it->stop_charpos = it->prev_stop;
6753 it->bidi_p = 1;
6754 it->current = save_current;
6755 it->position = save_position;
6756 handle_stop (it);
6757 it->stop_charpos = next_stop;
6758 }
6759
6760 /* Load IT with the next display element from current_buffer. Value
6761 is zero if end of buffer reached. IT->stop_charpos is the next
6762 position at which to stop and check for text properties or buffer
6763 end. */
6764
6765 static int
6766 next_element_from_buffer (struct it *it)
6767 {
6768 int success_p = 1;
6769
6770 xassert (IT_CHARPOS (*it) >= BEGV);
6771
6772 /* With bidi reordering, the character to display might not be the
6773 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6774 we were reseat()ed to a new buffer position, which is potentially
6775 a different paragraph. */
6776 if (it->bidi_p && it->bidi_it.first_elt)
6777 {
6778 it->bidi_it.charpos = IT_CHARPOS (*it);
6779 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6780 if (it->bidi_it.bytepos == ZV_BYTE)
6781 {
6782 /* Nothing to do, but reset the FIRST_ELT flag, like
6783 bidi_paragraph_init does, because we are not going to
6784 call it. */
6785 it->bidi_it.first_elt = 0;
6786 }
6787 else if (it->bidi_it.bytepos == BEGV_BYTE
6788 /* FIXME: Should support all Unicode line separators. */
6789 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6790 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6791 {
6792 /* If we are at the beginning of a line, we can produce the
6793 next element right away. */
6794 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6795 bidi_move_to_visually_next (&it->bidi_it);
6796 }
6797 else
6798 {
6799 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6800
6801 /* We need to prime the bidi iterator starting at the line's
6802 beginning, before we will be able to produce the next
6803 element. */
6804 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6805 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6806 it->bidi_it.charpos = IT_CHARPOS (*it);
6807 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6808 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6809 do
6810 {
6811 /* Now return to buffer position where we were asked to
6812 get the next display element, and produce that. */
6813 bidi_move_to_visually_next (&it->bidi_it);
6814 }
6815 while (it->bidi_it.bytepos != orig_bytepos
6816 && it->bidi_it.bytepos < ZV_BYTE);
6817 }
6818
6819 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6820 /* Adjust IT's position information to where we ended up. */
6821 IT_CHARPOS (*it) = it->bidi_it.charpos;
6822 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6823 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6824 {
6825 EMACS_INT stop = it->end_charpos;
6826 if (it->bidi_it.scan_dir < 0)
6827 stop = -1;
6828 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6829 IT_BYTEPOS (*it), stop, Qnil);
6830 }
6831 }
6832
6833 if (IT_CHARPOS (*it) >= it->stop_charpos)
6834 {
6835 if (IT_CHARPOS (*it) >= it->end_charpos)
6836 {
6837 int overlay_strings_follow_p;
6838
6839 /* End of the game, except when overlay strings follow that
6840 haven't been returned yet. */
6841 if (it->overlay_strings_at_end_processed_p)
6842 overlay_strings_follow_p = 0;
6843 else
6844 {
6845 it->overlay_strings_at_end_processed_p = 1;
6846 overlay_strings_follow_p = get_overlay_strings (it, 0);
6847 }
6848
6849 if (overlay_strings_follow_p)
6850 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6851 else
6852 {
6853 it->what = IT_EOB;
6854 it->position = it->current.pos;
6855 success_p = 0;
6856 }
6857 }
6858 else if (!(!it->bidi_p
6859 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6860 || IT_CHARPOS (*it) == it->stop_charpos))
6861 {
6862 /* With bidi non-linear iteration, we could find ourselves
6863 far beyond the last computed stop_charpos, with several
6864 other stop positions in between that we missed. Scan
6865 them all now, in buffer's logical order, until we find
6866 and handle the last stop_charpos that precedes our
6867 current position. */
6868 handle_stop_backwards (it, it->stop_charpos);
6869 return GET_NEXT_DISPLAY_ELEMENT (it);
6870 }
6871 else
6872 {
6873 if (it->bidi_p)
6874 {
6875 /* Take note of the stop position we just moved across,
6876 for when we will move back across it. */
6877 it->prev_stop = it->stop_charpos;
6878 /* If we are at base paragraph embedding level, take
6879 note of the last stop position seen at this
6880 level. */
6881 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6882 it->base_level_stop = it->stop_charpos;
6883 }
6884 handle_stop (it);
6885 return GET_NEXT_DISPLAY_ELEMENT (it);
6886 }
6887 }
6888 else if (it->bidi_p
6889 /* We can sometimes back up for reasons that have nothing
6890 to do with bidi reordering. E.g., compositions. The
6891 code below is only needed when we are above the base
6892 embedding level, so test for that explicitly. */
6893 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6894 && IT_CHARPOS (*it) < it->prev_stop)
6895 {
6896 if (it->base_level_stop <= 0)
6897 it->base_level_stop = BEGV;
6898 if (IT_CHARPOS (*it) < it->base_level_stop)
6899 abort ();
6900 handle_stop_backwards (it, it->base_level_stop);
6901 return GET_NEXT_DISPLAY_ELEMENT (it);
6902 }
6903 else
6904 {
6905 /* No face changes, overlays etc. in sight, so just return a
6906 character from current_buffer. */
6907 unsigned char *p;
6908 EMACS_INT stop;
6909
6910 /* Maybe run the redisplay end trigger hook. Performance note:
6911 This doesn't seem to cost measurable time. */
6912 if (it->redisplay_end_trigger_charpos
6913 && it->glyph_row
6914 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6915 run_redisplay_end_trigger_hook (it);
6916
6917 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6918 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6919 stop)
6920 && next_element_from_composition (it))
6921 {
6922 return 1;
6923 }
6924
6925 /* Get the next character, maybe multibyte. */
6926 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6927 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6928 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6929 else
6930 it->c = *p, it->len = 1;
6931
6932 /* Record what we have and where it came from. */
6933 it->what = IT_CHARACTER;
6934 it->object = it->w->buffer;
6935 it->position = it->current.pos;
6936
6937 /* Normally we return the character found above, except when we
6938 really want to return an ellipsis for selective display. */
6939 if (it->selective)
6940 {
6941 if (it->c == '\n')
6942 {
6943 /* A value of selective > 0 means hide lines indented more
6944 than that number of columns. */
6945 if (it->selective > 0
6946 && IT_CHARPOS (*it) + 1 < ZV
6947 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6948 IT_BYTEPOS (*it) + 1,
6949 (double) it->selective)) /* iftc */
6950 {
6951 success_p = next_element_from_ellipsis (it);
6952 it->dpvec_char_len = -1;
6953 }
6954 }
6955 else if (it->c == '\r' && it->selective == -1)
6956 {
6957 /* A value of selective == -1 means that everything from the
6958 CR to the end of the line is invisible, with maybe an
6959 ellipsis displayed for it. */
6960 success_p = next_element_from_ellipsis (it);
6961 it->dpvec_char_len = -1;
6962 }
6963 }
6964 }
6965
6966 /* Value is zero if end of buffer reached. */
6967 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6968 return success_p;
6969 }
6970
6971
6972 /* Run the redisplay end trigger hook for IT. */
6973
6974 static void
6975 run_redisplay_end_trigger_hook (struct it *it)
6976 {
6977 Lisp_Object args[3];
6978
6979 /* IT->glyph_row should be non-null, i.e. we should be actually
6980 displaying something, or otherwise we should not run the hook. */
6981 xassert (it->glyph_row);
6982
6983 /* Set up hook arguments. */
6984 args[0] = Qredisplay_end_trigger_functions;
6985 args[1] = it->window;
6986 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6987 it->redisplay_end_trigger_charpos = 0;
6988
6989 /* Since we are *trying* to run these functions, don't try to run
6990 them again, even if they get an error. */
6991 it->w->redisplay_end_trigger = Qnil;
6992 Frun_hook_with_args (3, args);
6993
6994 /* Notice if it changed the face of the character we are on. */
6995 handle_face_prop (it);
6996 }
6997
6998
6999 /* Deliver a composition display element. Unlike the other
7000 next_element_from_XXX, this function is not registered in the array
7001 get_next_element[]. It is called from next_element_from_buffer and
7002 next_element_from_string when necessary. */
7003
7004 static int
7005 next_element_from_composition (struct it *it)
7006 {
7007 it->what = IT_COMPOSITION;
7008 it->len = it->cmp_it.nbytes;
7009 if (STRINGP (it->string))
7010 {
7011 if (it->c < 0)
7012 {
7013 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7014 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7015 return 0;
7016 }
7017 it->position = it->current.string_pos;
7018 it->object = it->string;
7019 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7020 IT_STRING_BYTEPOS (*it), it->string);
7021 }
7022 else
7023 {
7024 if (it->c < 0)
7025 {
7026 IT_CHARPOS (*it) += it->cmp_it.nchars;
7027 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7028 if (it->bidi_p)
7029 {
7030 if (it->bidi_it.new_paragraph)
7031 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7032 /* Resync the bidi iterator with IT's new position.
7033 FIXME: this doesn't support bidirectional text. */
7034 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7035 bidi_move_to_visually_next (&it->bidi_it);
7036 }
7037 return 0;
7038 }
7039 it->position = it->current.pos;
7040 it->object = it->w->buffer;
7041 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7042 IT_BYTEPOS (*it), Qnil);
7043 }
7044 return 1;
7045 }
7046
7047
7048 \f
7049 /***********************************************************************
7050 Moving an iterator without producing glyphs
7051 ***********************************************************************/
7052
7053 /* Check if iterator is at a position corresponding to a valid buffer
7054 position after some move_it_ call. */
7055
7056 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7057 ((it)->method == GET_FROM_STRING \
7058 ? IT_STRING_CHARPOS (*it) == 0 \
7059 : 1)
7060
7061
7062 /* Move iterator IT to a specified buffer or X position within one
7063 line on the display without producing glyphs.
7064
7065 OP should be a bit mask including some or all of these bits:
7066 MOVE_TO_X: Stop upon reaching x-position TO_X.
7067 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7068 Regardless of OP's value, stop upon reaching the end of the display line.
7069
7070 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7071 This means, in particular, that TO_X includes window's horizontal
7072 scroll amount.
7073
7074 The return value has several possible values that
7075 say what condition caused the scan to stop:
7076
7077 MOVE_POS_MATCH_OR_ZV
7078 - when TO_POS or ZV was reached.
7079
7080 MOVE_X_REACHED
7081 -when TO_X was reached before TO_POS or ZV were reached.
7082
7083 MOVE_LINE_CONTINUED
7084 - when we reached the end of the display area and the line must
7085 be continued.
7086
7087 MOVE_LINE_TRUNCATED
7088 - when we reached the end of the display area and the line is
7089 truncated.
7090
7091 MOVE_NEWLINE_OR_CR
7092 - when we stopped at a line end, i.e. a newline or a CR and selective
7093 display is on. */
7094
7095 static enum move_it_result
7096 move_it_in_display_line_to (struct it *it,
7097 EMACS_INT to_charpos, int to_x,
7098 enum move_operation_enum op)
7099 {
7100 enum move_it_result result = MOVE_UNDEFINED;
7101 struct glyph_row *saved_glyph_row;
7102 struct it wrap_it, atpos_it, atx_it;
7103 int may_wrap = 0;
7104 enum it_method prev_method = it->method;
7105 EMACS_INT prev_pos = IT_CHARPOS (*it);
7106
7107 /* Don't produce glyphs in produce_glyphs. */
7108 saved_glyph_row = it->glyph_row;
7109 it->glyph_row = NULL;
7110
7111 /* Use wrap_it to save a copy of IT wherever a word wrap could
7112 occur. Use atpos_it to save a copy of IT at the desired buffer
7113 position, if found, so that we can scan ahead and check if the
7114 word later overshoots the window edge. Use atx_it similarly, for
7115 pixel positions. */
7116 wrap_it.sp = -1;
7117 atpos_it.sp = -1;
7118 atx_it.sp = -1;
7119
7120 #define BUFFER_POS_REACHED_P() \
7121 ((op & MOVE_TO_POS) != 0 \
7122 && BUFFERP (it->object) \
7123 && (IT_CHARPOS (*it) == to_charpos \
7124 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7125 && (it->method == GET_FROM_BUFFER \
7126 || (it->method == GET_FROM_DISPLAY_VECTOR \
7127 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7128
7129 /* If there's a line-/wrap-prefix, handle it. */
7130 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7131 && it->current_y < it->last_visible_y)
7132 handle_line_prefix (it);
7133
7134 while (1)
7135 {
7136 int x, i, ascent = 0, descent = 0;
7137
7138 /* Utility macro to reset an iterator with x, ascent, and descent. */
7139 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7140 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7141 (IT)->max_descent = descent)
7142
7143 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7144 glyph). */
7145 if ((op & MOVE_TO_POS) != 0
7146 && BUFFERP (it->object)
7147 && it->method == GET_FROM_BUFFER
7148 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7149 || (it->bidi_p
7150 && (prev_method == GET_FROM_IMAGE
7151 || prev_method == GET_FROM_STRETCH)
7152 /* Passed TO_CHARPOS from left to right. */
7153 && ((prev_pos < to_charpos
7154 && IT_CHARPOS (*it) > to_charpos)
7155 /* Passed TO_CHARPOS from right to left. */
7156 || (prev_pos > to_charpos
7157 && IT_CHARPOS (*it) < to_charpos)))))
7158 {
7159 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7160 {
7161 result = MOVE_POS_MATCH_OR_ZV;
7162 break;
7163 }
7164 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7165 /* If wrap_it is valid, the current position might be in a
7166 word that is wrapped. So, save the iterator in
7167 atpos_it and continue to see if wrapping happens. */
7168 atpos_it = *it;
7169 }
7170
7171 prev_method = it->method;
7172 if (it->method == GET_FROM_BUFFER)
7173 prev_pos = IT_CHARPOS (*it);
7174 /* Stop when ZV reached.
7175 We used to stop here when TO_CHARPOS reached as well, but that is
7176 too soon if this glyph does not fit on this line. So we handle it
7177 explicitly below. */
7178 if (!get_next_display_element (it))
7179 {
7180 result = MOVE_POS_MATCH_OR_ZV;
7181 break;
7182 }
7183
7184 if (it->line_wrap == TRUNCATE)
7185 {
7186 if (BUFFER_POS_REACHED_P ())
7187 {
7188 result = MOVE_POS_MATCH_OR_ZV;
7189 break;
7190 }
7191 }
7192 else
7193 {
7194 if (it->line_wrap == WORD_WRAP)
7195 {
7196 if (IT_DISPLAYING_WHITESPACE (it))
7197 may_wrap = 1;
7198 else if (may_wrap)
7199 {
7200 /* We have reached a glyph that follows one or more
7201 whitespace characters. If the position is
7202 already found, we are done. */
7203 if (atpos_it.sp >= 0)
7204 {
7205 *it = atpos_it;
7206 result = MOVE_POS_MATCH_OR_ZV;
7207 goto done;
7208 }
7209 if (atx_it.sp >= 0)
7210 {
7211 *it = atx_it;
7212 result = MOVE_X_REACHED;
7213 goto done;
7214 }
7215 /* Otherwise, we can wrap here. */
7216 wrap_it = *it;
7217 may_wrap = 0;
7218 }
7219 }
7220 }
7221
7222 /* Remember the line height for the current line, in case
7223 the next element doesn't fit on the line. */
7224 ascent = it->max_ascent;
7225 descent = it->max_descent;
7226
7227 /* The call to produce_glyphs will get the metrics of the
7228 display element IT is loaded with. Record the x-position
7229 before this display element, in case it doesn't fit on the
7230 line. */
7231 x = it->current_x;
7232
7233 PRODUCE_GLYPHS (it);
7234
7235 if (it->area != TEXT_AREA)
7236 {
7237 set_iterator_to_next (it, 1);
7238 continue;
7239 }
7240
7241 /* The number of glyphs we get back in IT->nglyphs will normally
7242 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7243 character on a terminal frame, or (iii) a line end. For the
7244 second case, IT->nglyphs - 1 padding glyphs will be present.
7245 (On X frames, there is only one glyph produced for a
7246 composite character.)
7247
7248 The behavior implemented below means, for continuation lines,
7249 that as many spaces of a TAB as fit on the current line are
7250 displayed there. For terminal frames, as many glyphs of a
7251 multi-glyph character are displayed in the current line, too.
7252 This is what the old redisplay code did, and we keep it that
7253 way. Under X, the whole shape of a complex character must
7254 fit on the line or it will be completely displayed in the
7255 next line.
7256
7257 Note that both for tabs and padding glyphs, all glyphs have
7258 the same width. */
7259 if (it->nglyphs)
7260 {
7261 /* More than one glyph or glyph doesn't fit on line. All
7262 glyphs have the same width. */
7263 int single_glyph_width = it->pixel_width / it->nglyphs;
7264 int new_x;
7265 int x_before_this_char = x;
7266 int hpos_before_this_char = it->hpos;
7267
7268 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7269 {
7270 new_x = x + single_glyph_width;
7271
7272 /* We want to leave anything reaching TO_X to the caller. */
7273 if ((op & MOVE_TO_X) && new_x > to_x)
7274 {
7275 if (BUFFER_POS_REACHED_P ())
7276 {
7277 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7278 goto buffer_pos_reached;
7279 if (atpos_it.sp < 0)
7280 {
7281 atpos_it = *it;
7282 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7283 }
7284 }
7285 else
7286 {
7287 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7288 {
7289 it->current_x = x;
7290 result = MOVE_X_REACHED;
7291 break;
7292 }
7293 if (atx_it.sp < 0)
7294 {
7295 atx_it = *it;
7296 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7297 }
7298 }
7299 }
7300
7301 if (/* Lines are continued. */
7302 it->line_wrap != TRUNCATE
7303 && (/* And glyph doesn't fit on the line. */
7304 new_x > it->last_visible_x
7305 /* Or it fits exactly and we're on a window
7306 system frame. */
7307 || (new_x == it->last_visible_x
7308 && FRAME_WINDOW_P (it->f))))
7309 {
7310 if (/* IT->hpos == 0 means the very first glyph
7311 doesn't fit on the line, e.g. a wide image. */
7312 it->hpos == 0
7313 || (new_x == it->last_visible_x
7314 && FRAME_WINDOW_P (it->f)))
7315 {
7316 ++it->hpos;
7317 it->current_x = new_x;
7318
7319 /* The character's last glyph just barely fits
7320 in this row. */
7321 if (i == it->nglyphs - 1)
7322 {
7323 /* If this is the destination position,
7324 return a position *before* it in this row,
7325 now that we know it fits in this row. */
7326 if (BUFFER_POS_REACHED_P ())
7327 {
7328 if (it->line_wrap != WORD_WRAP
7329 || wrap_it.sp < 0)
7330 {
7331 it->hpos = hpos_before_this_char;
7332 it->current_x = x_before_this_char;
7333 result = MOVE_POS_MATCH_OR_ZV;
7334 break;
7335 }
7336 if (it->line_wrap == WORD_WRAP
7337 && atpos_it.sp < 0)
7338 {
7339 atpos_it = *it;
7340 atpos_it.current_x = x_before_this_char;
7341 atpos_it.hpos = hpos_before_this_char;
7342 }
7343 }
7344
7345 set_iterator_to_next (it, 1);
7346 /* On graphical terminals, newlines may
7347 "overflow" into the fringe if
7348 overflow-newline-into-fringe is non-nil.
7349 On text-only terminals, newlines may
7350 overflow into the last glyph on the
7351 display line.*/
7352 if (!FRAME_WINDOW_P (it->f)
7353 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7354 {
7355 if (!get_next_display_element (it))
7356 {
7357 result = MOVE_POS_MATCH_OR_ZV;
7358 break;
7359 }
7360 if (BUFFER_POS_REACHED_P ())
7361 {
7362 if (ITERATOR_AT_END_OF_LINE_P (it))
7363 result = MOVE_POS_MATCH_OR_ZV;
7364 else
7365 result = MOVE_LINE_CONTINUED;
7366 break;
7367 }
7368 if (ITERATOR_AT_END_OF_LINE_P (it))
7369 {
7370 result = MOVE_NEWLINE_OR_CR;
7371 break;
7372 }
7373 }
7374 }
7375 }
7376 else
7377 IT_RESET_X_ASCENT_DESCENT (it);
7378
7379 if (wrap_it.sp >= 0)
7380 {
7381 *it = wrap_it;
7382 atpos_it.sp = -1;
7383 atx_it.sp = -1;
7384 }
7385
7386 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7387 IT_CHARPOS (*it)));
7388 result = MOVE_LINE_CONTINUED;
7389 break;
7390 }
7391
7392 if (BUFFER_POS_REACHED_P ())
7393 {
7394 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7395 goto buffer_pos_reached;
7396 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7397 {
7398 atpos_it = *it;
7399 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7400 }
7401 }
7402
7403 if (new_x > it->first_visible_x)
7404 {
7405 /* Glyph is visible. Increment number of glyphs that
7406 would be displayed. */
7407 ++it->hpos;
7408 }
7409 }
7410
7411 if (result != MOVE_UNDEFINED)
7412 break;
7413 }
7414 else if (BUFFER_POS_REACHED_P ())
7415 {
7416 buffer_pos_reached:
7417 IT_RESET_X_ASCENT_DESCENT (it);
7418 result = MOVE_POS_MATCH_OR_ZV;
7419 break;
7420 }
7421 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7422 {
7423 /* Stop when TO_X specified and reached. This check is
7424 necessary here because of lines consisting of a line end,
7425 only. The line end will not produce any glyphs and we
7426 would never get MOVE_X_REACHED. */
7427 xassert (it->nglyphs == 0);
7428 result = MOVE_X_REACHED;
7429 break;
7430 }
7431
7432 /* Is this a line end? If yes, we're done. */
7433 if (ITERATOR_AT_END_OF_LINE_P (it))
7434 {
7435 result = MOVE_NEWLINE_OR_CR;
7436 break;
7437 }
7438
7439 if (it->method == GET_FROM_BUFFER)
7440 prev_pos = IT_CHARPOS (*it);
7441 /* The current display element has been consumed. Advance
7442 to the next. */
7443 set_iterator_to_next (it, 1);
7444
7445 /* Stop if lines are truncated and IT's current x-position is
7446 past the right edge of the window now. */
7447 if (it->line_wrap == TRUNCATE
7448 && it->current_x >= it->last_visible_x)
7449 {
7450 if (!FRAME_WINDOW_P (it->f)
7451 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7452 {
7453 if (!get_next_display_element (it)
7454 || BUFFER_POS_REACHED_P ())
7455 {
7456 result = MOVE_POS_MATCH_OR_ZV;
7457 break;
7458 }
7459 if (ITERATOR_AT_END_OF_LINE_P (it))
7460 {
7461 result = MOVE_NEWLINE_OR_CR;
7462 break;
7463 }
7464 }
7465 result = MOVE_LINE_TRUNCATED;
7466 break;
7467 }
7468 #undef IT_RESET_X_ASCENT_DESCENT
7469 }
7470
7471 #undef BUFFER_POS_REACHED_P
7472
7473 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7474 restore the saved iterator. */
7475 if (atpos_it.sp >= 0)
7476 *it = atpos_it;
7477 else if (atx_it.sp >= 0)
7478 *it = atx_it;
7479
7480 done:
7481
7482 /* Restore the iterator settings altered at the beginning of this
7483 function. */
7484 it->glyph_row = saved_glyph_row;
7485 return result;
7486 }
7487
7488 /* For external use. */
7489 void
7490 move_it_in_display_line (struct it *it,
7491 EMACS_INT to_charpos, int to_x,
7492 enum move_operation_enum op)
7493 {
7494 if (it->line_wrap == WORD_WRAP
7495 && (op & MOVE_TO_X))
7496 {
7497 struct it save_it = *it;
7498 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7499 /* When word-wrap is on, TO_X may lie past the end
7500 of a wrapped line. Then it->current is the
7501 character on the next line, so backtrack to the
7502 space before the wrap point. */
7503 if (skip == MOVE_LINE_CONTINUED)
7504 {
7505 int prev_x = max (it->current_x - 1, 0);
7506 *it = save_it;
7507 move_it_in_display_line_to
7508 (it, -1, prev_x, MOVE_TO_X);
7509 }
7510 }
7511 else
7512 move_it_in_display_line_to (it, to_charpos, to_x, op);
7513 }
7514
7515
7516 /* Move IT forward until it satisfies one or more of the criteria in
7517 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7518
7519 OP is a bit-mask that specifies where to stop, and in particular,
7520 which of those four position arguments makes a difference. See the
7521 description of enum move_operation_enum.
7522
7523 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7524 screen line, this function will set IT to the next position >
7525 TO_CHARPOS. */
7526
7527 void
7528 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7529 {
7530 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7531 int line_height, line_start_x = 0, reached = 0;
7532
7533 for (;;)
7534 {
7535 if (op & MOVE_TO_VPOS)
7536 {
7537 /* If no TO_CHARPOS and no TO_X specified, stop at the
7538 start of the line TO_VPOS. */
7539 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7540 {
7541 if (it->vpos == to_vpos)
7542 {
7543 reached = 1;
7544 break;
7545 }
7546 else
7547 skip = move_it_in_display_line_to (it, -1, -1, 0);
7548 }
7549 else
7550 {
7551 /* TO_VPOS >= 0 means stop at TO_X in the line at
7552 TO_VPOS, or at TO_POS, whichever comes first. */
7553 if (it->vpos == to_vpos)
7554 {
7555 reached = 2;
7556 break;
7557 }
7558
7559 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7560
7561 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7562 {
7563 reached = 3;
7564 break;
7565 }
7566 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7567 {
7568 /* We have reached TO_X but not in the line we want. */
7569 skip = move_it_in_display_line_to (it, to_charpos,
7570 -1, MOVE_TO_POS);
7571 if (skip == MOVE_POS_MATCH_OR_ZV)
7572 {
7573 reached = 4;
7574 break;
7575 }
7576 }
7577 }
7578 }
7579 else if (op & MOVE_TO_Y)
7580 {
7581 struct it it_backup;
7582
7583 if (it->line_wrap == WORD_WRAP)
7584 it_backup = *it;
7585
7586 /* TO_Y specified means stop at TO_X in the line containing
7587 TO_Y---or at TO_CHARPOS if this is reached first. The
7588 problem is that we can't really tell whether the line
7589 contains TO_Y before we have completely scanned it, and
7590 this may skip past TO_X. What we do is to first scan to
7591 TO_X.
7592
7593 If TO_X is not specified, use a TO_X of zero. The reason
7594 is to make the outcome of this function more predictable.
7595 If we didn't use TO_X == 0, we would stop at the end of
7596 the line which is probably not what a caller would expect
7597 to happen. */
7598 skip = move_it_in_display_line_to
7599 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7600 (MOVE_TO_X | (op & MOVE_TO_POS)));
7601
7602 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7603 if (skip == MOVE_POS_MATCH_OR_ZV)
7604 reached = 5;
7605 else if (skip == MOVE_X_REACHED)
7606 {
7607 /* If TO_X was reached, we want to know whether TO_Y is
7608 in the line. We know this is the case if the already
7609 scanned glyphs make the line tall enough. Otherwise,
7610 we must check by scanning the rest of the line. */
7611 line_height = it->max_ascent + it->max_descent;
7612 if (to_y >= it->current_y
7613 && to_y < it->current_y + line_height)
7614 {
7615 reached = 6;
7616 break;
7617 }
7618 it_backup = *it;
7619 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7620 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7621 op & MOVE_TO_POS);
7622 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7623 line_height = it->max_ascent + it->max_descent;
7624 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7625
7626 if (to_y >= it->current_y
7627 && to_y < it->current_y + line_height)
7628 {
7629 /* If TO_Y is in this line and TO_X was reached
7630 above, we scanned too far. We have to restore
7631 IT's settings to the ones before skipping. */
7632 *it = it_backup;
7633 reached = 6;
7634 }
7635 else
7636 {
7637 skip = skip2;
7638 if (skip == MOVE_POS_MATCH_OR_ZV)
7639 reached = 7;
7640 }
7641 }
7642 else
7643 {
7644 /* Check whether TO_Y is in this line. */
7645 line_height = it->max_ascent + it->max_descent;
7646 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7647
7648 if (to_y >= it->current_y
7649 && to_y < it->current_y + line_height)
7650 {
7651 /* When word-wrap is on, TO_X may lie past the end
7652 of a wrapped line. Then it->current is the
7653 character on the next line, so backtrack to the
7654 space before the wrap point. */
7655 if (skip == MOVE_LINE_CONTINUED
7656 && it->line_wrap == WORD_WRAP)
7657 {
7658 int prev_x = max (it->current_x - 1, 0);
7659 *it = it_backup;
7660 skip = move_it_in_display_line_to
7661 (it, -1, prev_x, MOVE_TO_X);
7662 }
7663 reached = 6;
7664 }
7665 }
7666
7667 if (reached)
7668 break;
7669 }
7670 else if (BUFFERP (it->object)
7671 && (it->method == GET_FROM_BUFFER
7672 || it->method == GET_FROM_STRETCH)
7673 && IT_CHARPOS (*it) >= to_charpos)
7674 skip = MOVE_POS_MATCH_OR_ZV;
7675 else
7676 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7677
7678 switch (skip)
7679 {
7680 case MOVE_POS_MATCH_OR_ZV:
7681 reached = 8;
7682 goto out;
7683
7684 case MOVE_NEWLINE_OR_CR:
7685 set_iterator_to_next (it, 1);
7686 it->continuation_lines_width = 0;
7687 break;
7688
7689 case MOVE_LINE_TRUNCATED:
7690 it->continuation_lines_width = 0;
7691 reseat_at_next_visible_line_start (it, 0);
7692 if ((op & MOVE_TO_POS) != 0
7693 && IT_CHARPOS (*it) > to_charpos)
7694 {
7695 reached = 9;
7696 goto out;
7697 }
7698 break;
7699
7700 case MOVE_LINE_CONTINUED:
7701 /* For continued lines ending in a tab, some of the glyphs
7702 associated with the tab are displayed on the current
7703 line. Since it->current_x does not include these glyphs,
7704 we use it->last_visible_x instead. */
7705 if (it->c == '\t')
7706 {
7707 it->continuation_lines_width += it->last_visible_x;
7708 /* When moving by vpos, ensure that the iterator really
7709 advances to the next line (bug#847, bug#969). Fixme:
7710 do we need to do this in other circumstances? */
7711 if (it->current_x != it->last_visible_x
7712 && (op & MOVE_TO_VPOS)
7713 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7714 {
7715 line_start_x = it->current_x + it->pixel_width
7716 - it->last_visible_x;
7717 set_iterator_to_next (it, 0);
7718 }
7719 }
7720 else
7721 it->continuation_lines_width += it->current_x;
7722 break;
7723
7724 default:
7725 abort ();
7726 }
7727
7728 /* Reset/increment for the next run. */
7729 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7730 it->current_x = line_start_x;
7731 line_start_x = 0;
7732 it->hpos = 0;
7733 it->current_y += it->max_ascent + it->max_descent;
7734 ++it->vpos;
7735 last_height = it->max_ascent + it->max_descent;
7736 last_max_ascent = it->max_ascent;
7737 it->max_ascent = it->max_descent = 0;
7738 }
7739
7740 out:
7741
7742 /* On text terminals, we may stop at the end of a line in the middle
7743 of a multi-character glyph. If the glyph itself is continued,
7744 i.e. it is actually displayed on the next line, don't treat this
7745 stopping point as valid; move to the next line instead (unless
7746 that brings us offscreen). */
7747 if (!FRAME_WINDOW_P (it->f)
7748 && op & MOVE_TO_POS
7749 && IT_CHARPOS (*it) == to_charpos
7750 && it->what == IT_CHARACTER
7751 && it->nglyphs > 1
7752 && it->line_wrap == WINDOW_WRAP
7753 && it->current_x == it->last_visible_x - 1
7754 && it->c != '\n'
7755 && it->c != '\t'
7756 && it->vpos < XFASTINT (it->w->window_end_vpos))
7757 {
7758 it->continuation_lines_width += it->current_x;
7759 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7760 it->current_y += it->max_ascent + it->max_descent;
7761 ++it->vpos;
7762 last_height = it->max_ascent + it->max_descent;
7763 last_max_ascent = it->max_ascent;
7764 }
7765
7766 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7767 }
7768
7769
7770 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7771
7772 If DY > 0, move IT backward at least that many pixels. DY = 0
7773 means move IT backward to the preceding line start or BEGV. This
7774 function may move over more than DY pixels if IT->current_y - DY
7775 ends up in the middle of a line; in this case IT->current_y will be
7776 set to the top of the line moved to. */
7777
7778 void
7779 move_it_vertically_backward (struct it *it, int dy)
7780 {
7781 int nlines, h;
7782 struct it it2, it3;
7783 EMACS_INT start_pos;
7784
7785 move_further_back:
7786 xassert (dy >= 0);
7787
7788 start_pos = IT_CHARPOS (*it);
7789
7790 /* Estimate how many newlines we must move back. */
7791 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7792
7793 /* Set the iterator's position that many lines back. */
7794 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7795 back_to_previous_visible_line_start (it);
7796
7797 /* Reseat the iterator here. When moving backward, we don't want
7798 reseat to skip forward over invisible text, set up the iterator
7799 to deliver from overlay strings at the new position etc. So,
7800 use reseat_1 here. */
7801 reseat_1 (it, it->current.pos, 1);
7802
7803 /* We are now surely at a line start. */
7804 it->current_x = it->hpos = 0;
7805 it->continuation_lines_width = 0;
7806
7807 /* Move forward and see what y-distance we moved. First move to the
7808 start of the next line so that we get its height. We need this
7809 height to be able to tell whether we reached the specified
7810 y-distance. */
7811 it2 = *it;
7812 it2.max_ascent = it2.max_descent = 0;
7813 do
7814 {
7815 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7816 MOVE_TO_POS | MOVE_TO_VPOS);
7817 }
7818 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7819 xassert (IT_CHARPOS (*it) >= BEGV);
7820 it3 = it2;
7821
7822 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7823 xassert (IT_CHARPOS (*it) >= BEGV);
7824 /* H is the actual vertical distance from the position in *IT
7825 and the starting position. */
7826 h = it2.current_y - it->current_y;
7827 /* NLINES is the distance in number of lines. */
7828 nlines = it2.vpos - it->vpos;
7829
7830 /* Correct IT's y and vpos position
7831 so that they are relative to the starting point. */
7832 it->vpos -= nlines;
7833 it->current_y -= h;
7834
7835 if (dy == 0)
7836 {
7837 /* DY == 0 means move to the start of the screen line. The
7838 value of nlines is > 0 if continuation lines were involved. */
7839 if (nlines > 0)
7840 move_it_by_lines (it, nlines, 1);
7841 }
7842 else
7843 {
7844 /* The y-position we try to reach, relative to *IT.
7845 Note that H has been subtracted in front of the if-statement. */
7846 int target_y = it->current_y + h - dy;
7847 int y0 = it3.current_y;
7848 int y1 = line_bottom_y (&it3);
7849 int line_height = y1 - y0;
7850
7851 /* If we did not reach target_y, try to move further backward if
7852 we can. If we moved too far backward, try to move forward. */
7853 if (target_y < it->current_y
7854 /* This is heuristic. In a window that's 3 lines high, with
7855 a line height of 13 pixels each, recentering with point
7856 on the bottom line will try to move -39/2 = 19 pixels
7857 backward. Try to avoid moving into the first line. */
7858 && (it->current_y - target_y
7859 > min (window_box_height (it->w), line_height * 2 / 3))
7860 && IT_CHARPOS (*it) > BEGV)
7861 {
7862 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7863 target_y - it->current_y));
7864 dy = it->current_y - target_y;
7865 goto move_further_back;
7866 }
7867 else if (target_y >= it->current_y + line_height
7868 && IT_CHARPOS (*it) < ZV)
7869 {
7870 /* Should move forward by at least one line, maybe more.
7871
7872 Note: Calling move_it_by_lines can be expensive on
7873 terminal frames, where compute_motion is used (via
7874 vmotion) to do the job, when there are very long lines
7875 and truncate-lines is nil. That's the reason for
7876 treating terminal frames specially here. */
7877
7878 if (!FRAME_WINDOW_P (it->f))
7879 move_it_vertically (it, target_y - (it->current_y + line_height));
7880 else
7881 {
7882 do
7883 {
7884 move_it_by_lines (it, 1, 1);
7885 }
7886 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7887 }
7888 }
7889 }
7890 }
7891
7892
7893 /* Move IT by a specified amount of pixel lines DY. DY negative means
7894 move backwards. DY = 0 means move to start of screen line. At the
7895 end, IT will be on the start of a screen line. */
7896
7897 void
7898 move_it_vertically (struct it *it, int dy)
7899 {
7900 if (dy <= 0)
7901 move_it_vertically_backward (it, -dy);
7902 else
7903 {
7904 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7905 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7906 MOVE_TO_POS | MOVE_TO_Y);
7907 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7908
7909 /* If buffer ends in ZV without a newline, move to the start of
7910 the line to satisfy the post-condition. */
7911 if (IT_CHARPOS (*it) == ZV
7912 && ZV > BEGV
7913 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7914 move_it_by_lines (it, 0, 0);
7915 }
7916 }
7917
7918
7919 /* Move iterator IT past the end of the text line it is in. */
7920
7921 void
7922 move_it_past_eol (struct it *it)
7923 {
7924 enum move_it_result rc;
7925
7926 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7927 if (rc == MOVE_NEWLINE_OR_CR)
7928 set_iterator_to_next (it, 0);
7929 }
7930
7931
7932 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7933 negative means move up. DVPOS == 0 means move to the start of the
7934 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7935 NEED_Y_P is zero, IT->current_y will be left unchanged.
7936
7937 Further optimization ideas: If we would know that IT->f doesn't use
7938 a face with proportional font, we could be faster for
7939 truncate-lines nil. */
7940
7941 void
7942 move_it_by_lines (struct it *it, int dvpos, int need_y_p)
7943 {
7944
7945 /* The commented-out optimization uses vmotion on terminals. This
7946 gives bad results, because elements like it->what, on which
7947 callers such as pos_visible_p rely, aren't updated. */
7948 /* struct position pos;
7949 if (!FRAME_WINDOW_P (it->f))
7950 {
7951 struct text_pos textpos;
7952
7953 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7954 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7955 reseat (it, textpos, 1);
7956 it->vpos += pos.vpos;
7957 it->current_y += pos.vpos;
7958 }
7959 else */
7960
7961 if (dvpos == 0)
7962 {
7963 /* DVPOS == 0 means move to the start of the screen line. */
7964 move_it_vertically_backward (it, 0);
7965 xassert (it->current_x == 0 && it->hpos == 0);
7966 /* Let next call to line_bottom_y calculate real line height */
7967 last_height = 0;
7968 }
7969 else if (dvpos > 0)
7970 {
7971 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7972 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7973 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7974 }
7975 else
7976 {
7977 struct it it2;
7978 EMACS_INT start_charpos, i;
7979
7980 /* Start at the beginning of the screen line containing IT's
7981 position. This may actually move vertically backwards,
7982 in case of overlays, so adjust dvpos accordingly. */
7983 dvpos += it->vpos;
7984 move_it_vertically_backward (it, 0);
7985 dvpos -= it->vpos;
7986
7987 /* Go back -DVPOS visible lines and reseat the iterator there. */
7988 start_charpos = IT_CHARPOS (*it);
7989 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7990 back_to_previous_visible_line_start (it);
7991 reseat (it, it->current.pos, 1);
7992
7993 /* Move further back if we end up in a string or an image. */
7994 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7995 {
7996 /* First try to move to start of display line. */
7997 dvpos += it->vpos;
7998 move_it_vertically_backward (it, 0);
7999 dvpos -= it->vpos;
8000 if (IT_POS_VALID_AFTER_MOVE_P (it))
8001 break;
8002 /* If start of line is still in string or image,
8003 move further back. */
8004 back_to_previous_visible_line_start (it);
8005 reseat (it, it->current.pos, 1);
8006 dvpos--;
8007 }
8008
8009 it->current_x = it->hpos = 0;
8010
8011 /* Above call may have moved too far if continuation lines
8012 are involved. Scan forward and see if it did. */
8013 it2 = *it;
8014 it2.vpos = it2.current_y = 0;
8015 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8016 it->vpos -= it2.vpos;
8017 it->current_y -= it2.current_y;
8018 it->current_x = it->hpos = 0;
8019
8020 /* If we moved too far back, move IT some lines forward. */
8021 if (it2.vpos > -dvpos)
8022 {
8023 int delta = it2.vpos + dvpos;
8024 it2 = *it;
8025 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8026 /* Move back again if we got too far ahead. */
8027 if (IT_CHARPOS (*it) >= start_charpos)
8028 *it = it2;
8029 }
8030 }
8031 }
8032
8033 /* Return 1 if IT points into the middle of a display vector. */
8034
8035 int
8036 in_display_vector_p (struct it *it)
8037 {
8038 return (it->method == GET_FROM_DISPLAY_VECTOR
8039 && it->current.dpvec_index > 0
8040 && it->dpvec + it->current.dpvec_index != it->dpend);
8041 }
8042
8043 \f
8044 /***********************************************************************
8045 Messages
8046 ***********************************************************************/
8047
8048
8049 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8050 to *Messages*. */
8051
8052 void
8053 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8054 {
8055 Lisp_Object args[3];
8056 Lisp_Object msg, fmt;
8057 char *buffer;
8058 EMACS_INT len;
8059 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8060 USE_SAFE_ALLOCA;
8061
8062 /* Do nothing if called asynchronously. Inserting text into
8063 a buffer may call after-change-functions and alike and
8064 that would means running Lisp asynchronously. */
8065 if (handling_signal)
8066 return;
8067
8068 fmt = msg = Qnil;
8069 GCPRO4 (fmt, msg, arg1, arg2);
8070
8071 args[0] = fmt = build_string (format);
8072 args[1] = arg1;
8073 args[2] = arg2;
8074 msg = Fformat (3, args);
8075
8076 len = SBYTES (msg) + 1;
8077 SAFE_ALLOCA (buffer, char *, len);
8078 memcpy (buffer, SDATA (msg), len);
8079
8080 message_dolog (buffer, len - 1, 1, 0);
8081 SAFE_FREE ();
8082
8083 UNGCPRO;
8084 }
8085
8086
8087 /* Output a newline in the *Messages* buffer if "needs" one. */
8088
8089 void
8090 message_log_maybe_newline (void)
8091 {
8092 if (message_log_need_newline)
8093 message_dolog ("", 0, 1, 0);
8094 }
8095
8096
8097 /* Add a string M of length NBYTES to the message log, optionally
8098 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8099 nonzero, means interpret the contents of M as multibyte. This
8100 function calls low-level routines in order to bypass text property
8101 hooks, etc. which might not be safe to run.
8102
8103 This may GC (insert may run before/after change hooks),
8104 so the buffer M must NOT point to a Lisp string. */
8105
8106 void
8107 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8108 {
8109 if (!NILP (Vmemory_full))
8110 return;
8111
8112 if (!NILP (Vmessage_log_max))
8113 {
8114 struct buffer *oldbuf;
8115 Lisp_Object oldpoint, oldbegv, oldzv;
8116 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8117 EMACS_INT point_at_end = 0;
8118 EMACS_INT zv_at_end = 0;
8119 Lisp_Object old_deactivate_mark, tem;
8120 struct gcpro gcpro1;
8121
8122 old_deactivate_mark = Vdeactivate_mark;
8123 oldbuf = current_buffer;
8124 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8125 current_buffer->undo_list = Qt;
8126
8127 oldpoint = message_dolog_marker1;
8128 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8129 oldbegv = message_dolog_marker2;
8130 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8131 oldzv = message_dolog_marker3;
8132 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8133 GCPRO1 (old_deactivate_mark);
8134
8135 if (PT == Z)
8136 point_at_end = 1;
8137 if (ZV == Z)
8138 zv_at_end = 1;
8139
8140 BEGV = BEG;
8141 BEGV_BYTE = BEG_BYTE;
8142 ZV = Z;
8143 ZV_BYTE = Z_BYTE;
8144 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8145
8146 /* Insert the string--maybe converting multibyte to single byte
8147 or vice versa, so that all the text fits the buffer. */
8148 if (multibyte
8149 && NILP (current_buffer->enable_multibyte_characters))
8150 {
8151 EMACS_INT i;
8152 int c, char_bytes;
8153 unsigned char work[1];
8154
8155 /* Convert a multibyte string to single-byte
8156 for the *Message* buffer. */
8157 for (i = 0; i < nbytes; i += char_bytes)
8158 {
8159 c = string_char_and_length (m + i, &char_bytes);
8160 work[0] = (ASCII_CHAR_P (c)
8161 ? c
8162 : multibyte_char_to_unibyte (c, Qnil));
8163 insert_1_both (work, 1, 1, 1, 0, 0);
8164 }
8165 }
8166 else if (! multibyte
8167 && ! NILP (current_buffer->enable_multibyte_characters))
8168 {
8169 EMACS_INT i;
8170 int c, char_bytes;
8171 unsigned char *msg = (unsigned char *) m;
8172 unsigned char str[MAX_MULTIBYTE_LENGTH];
8173 /* Convert a single-byte string to multibyte
8174 for the *Message* buffer. */
8175 for (i = 0; i < nbytes; i++)
8176 {
8177 c = msg[i];
8178 MAKE_CHAR_MULTIBYTE (c);
8179 char_bytes = CHAR_STRING (c, str);
8180 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8181 }
8182 }
8183 else if (nbytes)
8184 insert_1 (m, nbytes, 1, 0, 0);
8185
8186 if (nlflag)
8187 {
8188 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8189 int dup;
8190 insert_1 ("\n", 1, 1, 0, 0);
8191
8192 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8193 this_bol = PT;
8194 this_bol_byte = PT_BYTE;
8195
8196 /* See if this line duplicates the previous one.
8197 If so, combine duplicates. */
8198 if (this_bol > BEG)
8199 {
8200 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8201 prev_bol = PT;
8202 prev_bol_byte = PT_BYTE;
8203
8204 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8205 this_bol, this_bol_byte);
8206 if (dup)
8207 {
8208 del_range_both (prev_bol, prev_bol_byte,
8209 this_bol, this_bol_byte, 0);
8210 if (dup > 1)
8211 {
8212 char dupstr[40];
8213 int duplen;
8214
8215 /* If you change this format, don't forget to also
8216 change message_log_check_duplicate. */
8217 sprintf (dupstr, " [%d times]", dup);
8218 duplen = strlen (dupstr);
8219 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8220 insert_1 (dupstr, duplen, 1, 0, 1);
8221 }
8222 }
8223 }
8224
8225 /* If we have more than the desired maximum number of lines
8226 in the *Messages* buffer now, delete the oldest ones.
8227 This is safe because we don't have undo in this buffer. */
8228
8229 if (NATNUMP (Vmessage_log_max))
8230 {
8231 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8232 -XFASTINT (Vmessage_log_max) - 1, 0);
8233 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8234 }
8235 }
8236 BEGV = XMARKER (oldbegv)->charpos;
8237 BEGV_BYTE = marker_byte_position (oldbegv);
8238
8239 if (zv_at_end)
8240 {
8241 ZV = Z;
8242 ZV_BYTE = Z_BYTE;
8243 }
8244 else
8245 {
8246 ZV = XMARKER (oldzv)->charpos;
8247 ZV_BYTE = marker_byte_position (oldzv);
8248 }
8249
8250 if (point_at_end)
8251 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8252 else
8253 /* We can't do Fgoto_char (oldpoint) because it will run some
8254 Lisp code. */
8255 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8256 XMARKER (oldpoint)->bytepos);
8257
8258 UNGCPRO;
8259 unchain_marker (XMARKER (oldpoint));
8260 unchain_marker (XMARKER (oldbegv));
8261 unchain_marker (XMARKER (oldzv));
8262
8263 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8264 set_buffer_internal (oldbuf);
8265 if (NILP (tem))
8266 windows_or_buffers_changed = old_windows_or_buffers_changed;
8267 message_log_need_newline = !nlflag;
8268 Vdeactivate_mark = old_deactivate_mark;
8269 }
8270 }
8271
8272
8273 /* We are at the end of the buffer after just having inserted a newline.
8274 (Note: We depend on the fact we won't be crossing the gap.)
8275 Check to see if the most recent message looks a lot like the previous one.
8276 Return 0 if different, 1 if the new one should just replace it, or a
8277 value N > 1 if we should also append " [N times]". */
8278
8279 static int
8280 message_log_check_duplicate (EMACS_INT prev_bol, EMACS_INT prev_bol_byte,
8281 EMACS_INT this_bol, EMACS_INT this_bol_byte)
8282 {
8283 EMACS_INT i;
8284 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8285 int seen_dots = 0;
8286 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8287 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8288
8289 for (i = 0; i < len; i++)
8290 {
8291 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8292 seen_dots = 1;
8293 if (p1[i] != p2[i])
8294 return seen_dots;
8295 }
8296 p1 += len;
8297 if (*p1 == '\n')
8298 return 2;
8299 if (*p1++ == ' ' && *p1++ == '[')
8300 {
8301 int n = 0;
8302 while (*p1 >= '0' && *p1 <= '9')
8303 n = n * 10 + *p1++ - '0';
8304 if (strncmp (p1, " times]\n", 8) == 0)
8305 return n+1;
8306 }
8307 return 0;
8308 }
8309 \f
8310
8311 /* Display an echo area message M with a specified length of NBYTES
8312 bytes. The string may include null characters. If M is 0, clear
8313 out any existing message, and let the mini-buffer text show
8314 through.
8315
8316 This may GC, so the buffer M must NOT point to a Lisp string. */
8317
8318 void
8319 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8320 {
8321 /* First flush out any partial line written with print. */
8322 message_log_maybe_newline ();
8323 if (m)
8324 message_dolog (m, nbytes, 1, multibyte);
8325 message2_nolog (m, nbytes, multibyte);
8326 }
8327
8328
8329 /* The non-logging counterpart of message2. */
8330
8331 void
8332 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8333 {
8334 struct frame *sf = SELECTED_FRAME ();
8335 message_enable_multibyte = multibyte;
8336
8337 if (FRAME_INITIAL_P (sf))
8338 {
8339 if (noninteractive_need_newline)
8340 putc ('\n', stderr);
8341 noninteractive_need_newline = 0;
8342 if (m)
8343 fwrite (m, nbytes, 1, stderr);
8344 if (cursor_in_echo_area == 0)
8345 fprintf (stderr, "\n");
8346 fflush (stderr);
8347 }
8348 /* A null message buffer means that the frame hasn't really been
8349 initialized yet. Error messages get reported properly by
8350 cmd_error, so this must be just an informative message; toss it. */
8351 else if (INTERACTIVE
8352 && sf->glyphs_initialized_p
8353 && FRAME_MESSAGE_BUF (sf))
8354 {
8355 Lisp_Object mini_window;
8356 struct frame *f;
8357
8358 /* Get the frame containing the mini-buffer
8359 that the selected frame is using. */
8360 mini_window = FRAME_MINIBUF_WINDOW (sf);
8361 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8362
8363 FRAME_SAMPLE_VISIBILITY (f);
8364 if (FRAME_VISIBLE_P (sf)
8365 && ! FRAME_VISIBLE_P (f))
8366 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8367
8368 if (m)
8369 {
8370 set_message (m, Qnil, nbytes, multibyte);
8371 if (minibuffer_auto_raise)
8372 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8373 }
8374 else
8375 clear_message (1, 1);
8376
8377 do_pending_window_change (0);
8378 echo_area_display (1);
8379 do_pending_window_change (0);
8380 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8381 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8382 }
8383 }
8384
8385
8386 /* Display an echo area message M with a specified length of NBYTES
8387 bytes. The string may include null characters. If M is not a
8388 string, clear out any existing message, and let the mini-buffer
8389 text show through.
8390
8391 This function cancels echoing. */
8392
8393 void
8394 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8395 {
8396 struct gcpro gcpro1;
8397
8398 GCPRO1 (m);
8399 clear_message (1,1);
8400 cancel_echoing ();
8401
8402 /* First flush out any partial line written with print. */
8403 message_log_maybe_newline ();
8404 if (STRINGP (m))
8405 {
8406 char *buffer;
8407 USE_SAFE_ALLOCA;
8408
8409 SAFE_ALLOCA (buffer, char *, nbytes);
8410 memcpy (buffer, SDATA (m), nbytes);
8411 message_dolog (buffer, nbytes, 1, multibyte);
8412 SAFE_FREE ();
8413 }
8414 message3_nolog (m, nbytes, multibyte);
8415
8416 UNGCPRO;
8417 }
8418
8419
8420 /* The non-logging version of message3.
8421 This does not cancel echoing, because it is used for echoing.
8422 Perhaps we need to make a separate function for echoing
8423 and make this cancel echoing. */
8424
8425 void
8426 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8427 {
8428 struct frame *sf = SELECTED_FRAME ();
8429 message_enable_multibyte = multibyte;
8430
8431 if (FRAME_INITIAL_P (sf))
8432 {
8433 if (noninteractive_need_newline)
8434 putc ('\n', stderr);
8435 noninteractive_need_newline = 0;
8436 if (STRINGP (m))
8437 fwrite (SDATA (m), nbytes, 1, stderr);
8438 if (cursor_in_echo_area == 0)
8439 fprintf (stderr, "\n");
8440 fflush (stderr);
8441 }
8442 /* A null message buffer means that the frame hasn't really been
8443 initialized yet. Error messages get reported properly by
8444 cmd_error, so this must be just an informative message; toss it. */
8445 else if (INTERACTIVE
8446 && sf->glyphs_initialized_p
8447 && FRAME_MESSAGE_BUF (sf))
8448 {
8449 Lisp_Object mini_window;
8450 Lisp_Object frame;
8451 struct frame *f;
8452
8453 /* Get the frame containing the mini-buffer
8454 that the selected frame is using. */
8455 mini_window = FRAME_MINIBUF_WINDOW (sf);
8456 frame = XWINDOW (mini_window)->frame;
8457 f = XFRAME (frame);
8458
8459 FRAME_SAMPLE_VISIBILITY (f);
8460 if (FRAME_VISIBLE_P (sf)
8461 && !FRAME_VISIBLE_P (f))
8462 Fmake_frame_visible (frame);
8463
8464 if (STRINGP (m) && SCHARS (m) > 0)
8465 {
8466 set_message (NULL, m, nbytes, multibyte);
8467 if (minibuffer_auto_raise)
8468 Fraise_frame (frame);
8469 /* Assume we are not echoing.
8470 (If we are, echo_now will override this.) */
8471 echo_message_buffer = Qnil;
8472 }
8473 else
8474 clear_message (1, 1);
8475
8476 do_pending_window_change (0);
8477 echo_area_display (1);
8478 do_pending_window_change (0);
8479 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8480 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8481 }
8482 }
8483
8484
8485 /* Display a null-terminated echo area message M. If M is 0, clear
8486 out any existing message, and let the mini-buffer text show through.
8487
8488 The buffer M must continue to exist until after the echo area gets
8489 cleared or some other message gets displayed there. Do not pass
8490 text that is stored in a Lisp string. Do not pass text in a buffer
8491 that was alloca'd. */
8492
8493 void
8494 message1 (const char *m)
8495 {
8496 message2 (m, (m ? strlen (m) : 0), 0);
8497 }
8498
8499
8500 /* The non-logging counterpart of message1. */
8501
8502 void
8503 message1_nolog (const char *m)
8504 {
8505 message2_nolog (m, (m ? strlen (m) : 0), 0);
8506 }
8507
8508 /* Display a message M which contains a single %s
8509 which gets replaced with STRING. */
8510
8511 void
8512 message_with_string (const char *m, Lisp_Object string, int log)
8513 {
8514 CHECK_STRING (string);
8515
8516 if (noninteractive)
8517 {
8518 if (m)
8519 {
8520 if (noninteractive_need_newline)
8521 putc ('\n', stderr);
8522 noninteractive_need_newline = 0;
8523 fprintf (stderr, m, SDATA (string));
8524 if (!cursor_in_echo_area)
8525 fprintf (stderr, "\n");
8526 fflush (stderr);
8527 }
8528 }
8529 else if (INTERACTIVE)
8530 {
8531 /* The frame whose minibuffer we're going to display the message on.
8532 It may be larger than the selected frame, so we need
8533 to use its buffer, not the selected frame's buffer. */
8534 Lisp_Object mini_window;
8535 struct frame *f, *sf = SELECTED_FRAME ();
8536
8537 /* Get the frame containing the minibuffer
8538 that the selected frame is using. */
8539 mini_window = FRAME_MINIBUF_WINDOW (sf);
8540 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8541
8542 /* A null message buffer means that the frame hasn't really been
8543 initialized yet. Error messages get reported properly by
8544 cmd_error, so this must be just an informative message; toss it. */
8545 if (FRAME_MESSAGE_BUF (f))
8546 {
8547 Lisp_Object args[2], message;
8548 struct gcpro gcpro1, gcpro2;
8549
8550 args[0] = build_string (m);
8551 args[1] = message = string;
8552 GCPRO2 (args[0], message);
8553 gcpro1.nvars = 2;
8554
8555 message = Fformat (2, args);
8556
8557 if (log)
8558 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8559 else
8560 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8561
8562 UNGCPRO;
8563
8564 /* Print should start at the beginning of the message
8565 buffer next time. */
8566 message_buf_print = 0;
8567 }
8568 }
8569 }
8570
8571
8572 /* Dump an informative message to the minibuf. If M is 0, clear out
8573 any existing message, and let the mini-buffer text show through. */
8574
8575 static void
8576 vmessage (const char *m, va_list ap)
8577 {
8578 if (noninteractive)
8579 {
8580 if (m)
8581 {
8582 if (noninteractive_need_newline)
8583 putc ('\n', stderr);
8584 noninteractive_need_newline = 0;
8585 vfprintf (stderr, m, ap);
8586 if (cursor_in_echo_area == 0)
8587 fprintf (stderr, "\n");
8588 fflush (stderr);
8589 }
8590 }
8591 else if (INTERACTIVE)
8592 {
8593 /* The frame whose mini-buffer we're going to display the message
8594 on. It may be larger than the selected frame, so we need to
8595 use its buffer, not the selected frame's buffer. */
8596 Lisp_Object mini_window;
8597 struct frame *f, *sf = SELECTED_FRAME ();
8598
8599 /* Get the frame containing the mini-buffer
8600 that the selected frame is using. */
8601 mini_window = FRAME_MINIBUF_WINDOW (sf);
8602 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8603
8604 /* A null message buffer means that the frame hasn't really been
8605 initialized yet. Error messages get reported properly by
8606 cmd_error, so this must be just an informative message; toss
8607 it. */
8608 if (FRAME_MESSAGE_BUF (f))
8609 {
8610 if (m)
8611 {
8612 EMACS_INT len;
8613
8614 len = doprnt (FRAME_MESSAGE_BUF (f),
8615 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8616
8617 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8618 }
8619 else
8620 message1 (0);
8621
8622 /* Print should start at the beginning of the message
8623 buffer next time. */
8624 message_buf_print = 0;
8625 }
8626 }
8627 }
8628
8629 void
8630 message (const char *m, ...)
8631 {
8632 va_list ap;
8633 va_start (ap, m);
8634 vmessage (m, ap);
8635 va_end (ap);
8636 }
8637
8638
8639 /* The non-logging version of message. */
8640
8641 void
8642 message_nolog (const char *m, ...)
8643 {
8644 Lisp_Object old_log_max;
8645 va_list ap;
8646 va_start (ap, m);
8647 old_log_max = Vmessage_log_max;
8648 Vmessage_log_max = Qnil;
8649 vmessage (m, ap);
8650 Vmessage_log_max = old_log_max;
8651 va_end (ap);
8652 }
8653
8654
8655 /* Display the current message in the current mini-buffer. This is
8656 only called from error handlers in process.c, and is not time
8657 critical. */
8658
8659 void
8660 update_echo_area (void)
8661 {
8662 if (!NILP (echo_area_buffer[0]))
8663 {
8664 Lisp_Object string;
8665 string = Fcurrent_message ();
8666 message3 (string, SBYTES (string),
8667 !NILP (current_buffer->enable_multibyte_characters));
8668 }
8669 }
8670
8671
8672 /* Make sure echo area buffers in `echo_buffers' are live.
8673 If they aren't, make new ones. */
8674
8675 static void
8676 ensure_echo_area_buffers (void)
8677 {
8678 int i;
8679
8680 for (i = 0; i < 2; ++i)
8681 if (!BUFFERP (echo_buffer[i])
8682 || NILP (XBUFFER (echo_buffer[i])->name))
8683 {
8684 char name[30];
8685 Lisp_Object old_buffer;
8686 int j;
8687
8688 old_buffer = echo_buffer[i];
8689 sprintf (name, " *Echo Area %d*", i);
8690 echo_buffer[i] = Fget_buffer_create (build_string (name));
8691 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8692 /* to force word wrap in echo area -
8693 it was decided to postpone this*/
8694 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8695
8696 for (j = 0; j < 2; ++j)
8697 if (EQ (old_buffer, echo_area_buffer[j]))
8698 echo_area_buffer[j] = echo_buffer[i];
8699 }
8700 }
8701
8702
8703 /* Call FN with args A1..A4 with either the current or last displayed
8704 echo_area_buffer as current buffer.
8705
8706 WHICH zero means use the current message buffer
8707 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8708 from echo_buffer[] and clear it.
8709
8710 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8711 suitable buffer from echo_buffer[] and clear it.
8712
8713 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8714 that the current message becomes the last displayed one, make
8715 choose a suitable buffer for echo_area_buffer[0], and clear it.
8716
8717 Value is what FN returns. */
8718
8719 static int
8720 with_echo_area_buffer (struct window *w, int which,
8721 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8722 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8723 {
8724 Lisp_Object buffer;
8725 int this_one, the_other, clear_buffer_p, rc;
8726 int count = SPECPDL_INDEX ();
8727
8728 /* If buffers aren't live, make new ones. */
8729 ensure_echo_area_buffers ();
8730
8731 clear_buffer_p = 0;
8732
8733 if (which == 0)
8734 this_one = 0, the_other = 1;
8735 else if (which > 0)
8736 this_one = 1, the_other = 0;
8737 else
8738 {
8739 this_one = 0, the_other = 1;
8740 clear_buffer_p = 1;
8741
8742 /* We need a fresh one in case the current echo buffer equals
8743 the one containing the last displayed echo area message. */
8744 if (!NILP (echo_area_buffer[this_one])
8745 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8746 echo_area_buffer[this_one] = Qnil;
8747 }
8748
8749 /* Choose a suitable buffer from echo_buffer[] is we don't
8750 have one. */
8751 if (NILP (echo_area_buffer[this_one]))
8752 {
8753 echo_area_buffer[this_one]
8754 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8755 ? echo_buffer[the_other]
8756 : echo_buffer[this_one]);
8757 clear_buffer_p = 1;
8758 }
8759
8760 buffer = echo_area_buffer[this_one];
8761
8762 /* Don't get confused by reusing the buffer used for echoing
8763 for a different purpose. */
8764 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8765 cancel_echoing ();
8766
8767 record_unwind_protect (unwind_with_echo_area_buffer,
8768 with_echo_area_buffer_unwind_data (w));
8769
8770 /* Make the echo area buffer current. Note that for display
8771 purposes, it is not necessary that the displayed window's buffer
8772 == current_buffer, except for text property lookup. So, let's
8773 only set that buffer temporarily here without doing a full
8774 Fset_window_buffer. We must also change w->pointm, though,
8775 because otherwise an assertions in unshow_buffer fails, and Emacs
8776 aborts. */
8777 set_buffer_internal_1 (XBUFFER (buffer));
8778 if (w)
8779 {
8780 w->buffer = buffer;
8781 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8782 }
8783
8784 current_buffer->undo_list = Qt;
8785 current_buffer->read_only = Qnil;
8786 specbind (Qinhibit_read_only, Qt);
8787 specbind (Qinhibit_modification_hooks, Qt);
8788
8789 if (clear_buffer_p && Z > BEG)
8790 del_range (BEG, Z);
8791
8792 xassert (BEGV >= BEG);
8793 xassert (ZV <= Z && ZV >= BEGV);
8794
8795 rc = fn (a1, a2, a3, a4);
8796
8797 xassert (BEGV >= BEG);
8798 xassert (ZV <= Z && ZV >= BEGV);
8799
8800 unbind_to (count, Qnil);
8801 return rc;
8802 }
8803
8804
8805 /* Save state that should be preserved around the call to the function
8806 FN called in with_echo_area_buffer. */
8807
8808 static Lisp_Object
8809 with_echo_area_buffer_unwind_data (struct window *w)
8810 {
8811 int i = 0;
8812 Lisp_Object vector, tmp;
8813
8814 /* Reduce consing by keeping one vector in
8815 Vwith_echo_area_save_vector. */
8816 vector = Vwith_echo_area_save_vector;
8817 Vwith_echo_area_save_vector = Qnil;
8818
8819 if (NILP (vector))
8820 vector = Fmake_vector (make_number (7), Qnil);
8821
8822 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8823 ASET (vector, i, Vdeactivate_mark); ++i;
8824 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8825
8826 if (w)
8827 {
8828 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8829 ASET (vector, i, w->buffer); ++i;
8830 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8831 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8832 }
8833 else
8834 {
8835 int end = i + 4;
8836 for (; i < end; ++i)
8837 ASET (vector, i, Qnil);
8838 }
8839
8840 xassert (i == ASIZE (vector));
8841 return vector;
8842 }
8843
8844
8845 /* Restore global state from VECTOR which was created by
8846 with_echo_area_buffer_unwind_data. */
8847
8848 static Lisp_Object
8849 unwind_with_echo_area_buffer (Lisp_Object vector)
8850 {
8851 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8852 Vdeactivate_mark = AREF (vector, 1);
8853 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8854
8855 if (WINDOWP (AREF (vector, 3)))
8856 {
8857 struct window *w;
8858 Lisp_Object buffer, charpos, bytepos;
8859
8860 w = XWINDOW (AREF (vector, 3));
8861 buffer = AREF (vector, 4);
8862 charpos = AREF (vector, 5);
8863 bytepos = AREF (vector, 6);
8864
8865 w->buffer = buffer;
8866 set_marker_both (w->pointm, buffer,
8867 XFASTINT (charpos), XFASTINT (bytepos));
8868 }
8869
8870 Vwith_echo_area_save_vector = vector;
8871 return Qnil;
8872 }
8873
8874
8875 /* Set up the echo area for use by print functions. MULTIBYTE_P
8876 non-zero means we will print multibyte. */
8877
8878 void
8879 setup_echo_area_for_printing (int multibyte_p)
8880 {
8881 /* If we can't find an echo area any more, exit. */
8882 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8883 Fkill_emacs (Qnil);
8884
8885 ensure_echo_area_buffers ();
8886
8887 if (!message_buf_print)
8888 {
8889 /* A message has been output since the last time we printed.
8890 Choose a fresh echo area buffer. */
8891 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8892 echo_area_buffer[0] = echo_buffer[1];
8893 else
8894 echo_area_buffer[0] = echo_buffer[0];
8895
8896 /* Switch to that buffer and clear it. */
8897 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8898 current_buffer->truncate_lines = Qnil;
8899
8900 if (Z > BEG)
8901 {
8902 int count = SPECPDL_INDEX ();
8903 specbind (Qinhibit_read_only, Qt);
8904 /* Note that undo recording is always disabled. */
8905 del_range (BEG, Z);
8906 unbind_to (count, Qnil);
8907 }
8908 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8909
8910 /* Set up the buffer for the multibyteness we need. */
8911 if (multibyte_p
8912 != !NILP (current_buffer->enable_multibyte_characters))
8913 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8914
8915 /* Raise the frame containing the echo area. */
8916 if (minibuffer_auto_raise)
8917 {
8918 struct frame *sf = SELECTED_FRAME ();
8919 Lisp_Object mini_window;
8920 mini_window = FRAME_MINIBUF_WINDOW (sf);
8921 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8922 }
8923
8924 message_log_maybe_newline ();
8925 message_buf_print = 1;
8926 }
8927 else
8928 {
8929 if (NILP (echo_area_buffer[0]))
8930 {
8931 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8932 echo_area_buffer[0] = echo_buffer[1];
8933 else
8934 echo_area_buffer[0] = echo_buffer[0];
8935 }
8936
8937 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8938 {
8939 /* Someone switched buffers between print requests. */
8940 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8941 current_buffer->truncate_lines = Qnil;
8942 }
8943 }
8944 }
8945
8946
8947 /* Display an echo area message in window W. Value is non-zero if W's
8948 height is changed. If display_last_displayed_message_p is
8949 non-zero, display the message that was last displayed, otherwise
8950 display the current message. */
8951
8952 static int
8953 display_echo_area (struct window *w)
8954 {
8955 int i, no_message_p, window_height_changed_p, count;
8956
8957 /* Temporarily disable garbage collections while displaying the echo
8958 area. This is done because a GC can print a message itself.
8959 That message would modify the echo area buffer's contents while a
8960 redisplay of the buffer is going on, and seriously confuse
8961 redisplay. */
8962 count = inhibit_garbage_collection ();
8963
8964 /* If there is no message, we must call display_echo_area_1
8965 nevertheless because it resizes the window. But we will have to
8966 reset the echo_area_buffer in question to nil at the end because
8967 with_echo_area_buffer will sets it to an empty buffer. */
8968 i = display_last_displayed_message_p ? 1 : 0;
8969 no_message_p = NILP (echo_area_buffer[i]);
8970
8971 window_height_changed_p
8972 = with_echo_area_buffer (w, display_last_displayed_message_p,
8973 display_echo_area_1,
8974 (EMACS_INT) w, Qnil, 0, 0);
8975
8976 if (no_message_p)
8977 echo_area_buffer[i] = Qnil;
8978
8979 unbind_to (count, Qnil);
8980 return window_height_changed_p;
8981 }
8982
8983
8984 /* Helper for display_echo_area. Display the current buffer which
8985 contains the current echo area message in window W, a mini-window,
8986 a pointer to which is passed in A1. A2..A4 are currently not used.
8987 Change the height of W so that all of the message is displayed.
8988 Value is non-zero if height of W was changed. */
8989
8990 static int
8991 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8992 {
8993 struct window *w = (struct window *) a1;
8994 Lisp_Object window;
8995 struct text_pos start;
8996 int window_height_changed_p = 0;
8997
8998 /* Do this before displaying, so that we have a large enough glyph
8999 matrix for the display. If we can't get enough space for the
9000 whole text, display the last N lines. That works by setting w->start. */
9001 window_height_changed_p = resize_mini_window (w, 0);
9002
9003 /* Use the starting position chosen by resize_mini_window. */
9004 SET_TEXT_POS_FROM_MARKER (start, w->start);
9005
9006 /* Display. */
9007 clear_glyph_matrix (w->desired_matrix);
9008 XSETWINDOW (window, w);
9009 try_window (window, start, 0);
9010
9011 return window_height_changed_p;
9012 }
9013
9014
9015 /* Resize the echo area window to exactly the size needed for the
9016 currently displayed message, if there is one. If a mini-buffer
9017 is active, don't shrink it. */
9018
9019 void
9020 resize_echo_area_exactly (void)
9021 {
9022 if (BUFFERP (echo_area_buffer[0])
9023 && WINDOWP (echo_area_window))
9024 {
9025 struct window *w = XWINDOW (echo_area_window);
9026 int resized_p;
9027 Lisp_Object resize_exactly;
9028
9029 if (minibuf_level == 0)
9030 resize_exactly = Qt;
9031 else
9032 resize_exactly = Qnil;
9033
9034 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9035 (EMACS_INT) w, resize_exactly, 0, 0);
9036 if (resized_p)
9037 {
9038 ++windows_or_buffers_changed;
9039 ++update_mode_lines;
9040 redisplay_internal (0);
9041 }
9042 }
9043 }
9044
9045
9046 /* Callback function for with_echo_area_buffer, when used from
9047 resize_echo_area_exactly. A1 contains a pointer to the window to
9048 resize, EXACTLY non-nil means resize the mini-window exactly to the
9049 size of the text displayed. A3 and A4 are not used. Value is what
9050 resize_mini_window returns. */
9051
9052 static int
9053 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9054 {
9055 return resize_mini_window ((struct window *) a1, !NILP (exactly));
9056 }
9057
9058
9059 /* Resize mini-window W to fit the size of its contents. EXACT_P
9060 means size the window exactly to the size needed. Otherwise, it's
9061 only enlarged until W's buffer is empty.
9062
9063 Set W->start to the right place to begin display. If the whole
9064 contents fit, start at the beginning. Otherwise, start so as
9065 to make the end of the contents appear. This is particularly
9066 important for y-or-n-p, but seems desirable generally.
9067
9068 Value is non-zero if the window height has been changed. */
9069
9070 int
9071 resize_mini_window (struct window *w, int exact_p)
9072 {
9073 struct frame *f = XFRAME (w->frame);
9074 int window_height_changed_p = 0;
9075
9076 xassert (MINI_WINDOW_P (w));
9077
9078 /* By default, start display at the beginning. */
9079 set_marker_both (w->start, w->buffer,
9080 BUF_BEGV (XBUFFER (w->buffer)),
9081 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9082
9083 /* Don't resize windows while redisplaying a window; it would
9084 confuse redisplay functions when the size of the window they are
9085 displaying changes from under them. Such a resizing can happen,
9086 for instance, when which-func prints a long message while
9087 we are running fontification-functions. We're running these
9088 functions with safe_call which binds inhibit-redisplay to t. */
9089 if (!NILP (Vinhibit_redisplay))
9090 return 0;
9091
9092 /* Nil means don't try to resize. */
9093 if (NILP (Vresize_mini_windows)
9094 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9095 return 0;
9096
9097 if (!FRAME_MINIBUF_ONLY_P (f))
9098 {
9099 struct it it;
9100 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9101 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9102 int height, max_height;
9103 int unit = FRAME_LINE_HEIGHT (f);
9104 struct text_pos start;
9105 struct buffer *old_current_buffer = NULL;
9106
9107 if (current_buffer != XBUFFER (w->buffer))
9108 {
9109 old_current_buffer = current_buffer;
9110 set_buffer_internal (XBUFFER (w->buffer));
9111 }
9112
9113 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9114
9115 /* Compute the max. number of lines specified by the user. */
9116 if (FLOATP (Vmax_mini_window_height))
9117 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9118 else if (INTEGERP (Vmax_mini_window_height))
9119 max_height = XINT (Vmax_mini_window_height);
9120 else
9121 max_height = total_height / 4;
9122
9123 /* Correct that max. height if it's bogus. */
9124 max_height = max (1, max_height);
9125 max_height = min (total_height, max_height);
9126
9127 /* Find out the height of the text in the window. */
9128 if (it.line_wrap == TRUNCATE)
9129 height = 1;
9130 else
9131 {
9132 last_height = 0;
9133 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9134 if (it.max_ascent == 0 && it.max_descent == 0)
9135 height = it.current_y + last_height;
9136 else
9137 height = it.current_y + it.max_ascent + it.max_descent;
9138 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9139 height = (height + unit - 1) / unit;
9140 }
9141
9142 /* Compute a suitable window start. */
9143 if (height > max_height)
9144 {
9145 height = max_height;
9146 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9147 move_it_vertically_backward (&it, (height - 1) * unit);
9148 start = it.current.pos;
9149 }
9150 else
9151 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9152 SET_MARKER_FROM_TEXT_POS (w->start, start);
9153
9154 if (EQ (Vresize_mini_windows, Qgrow_only))
9155 {
9156 /* Let it grow only, until we display an empty message, in which
9157 case the window shrinks again. */
9158 if (height > WINDOW_TOTAL_LINES (w))
9159 {
9160 int old_height = WINDOW_TOTAL_LINES (w);
9161 freeze_window_starts (f, 1);
9162 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9163 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9164 }
9165 else if (height < WINDOW_TOTAL_LINES (w)
9166 && (exact_p || BEGV == ZV))
9167 {
9168 int old_height = WINDOW_TOTAL_LINES (w);
9169 freeze_window_starts (f, 0);
9170 shrink_mini_window (w);
9171 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9172 }
9173 }
9174 else
9175 {
9176 /* Always resize to exact size needed. */
9177 if (height > WINDOW_TOTAL_LINES (w))
9178 {
9179 int old_height = WINDOW_TOTAL_LINES (w);
9180 freeze_window_starts (f, 1);
9181 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9182 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9183 }
9184 else if (height < WINDOW_TOTAL_LINES (w))
9185 {
9186 int old_height = WINDOW_TOTAL_LINES (w);
9187 freeze_window_starts (f, 0);
9188 shrink_mini_window (w);
9189
9190 if (height)
9191 {
9192 freeze_window_starts (f, 1);
9193 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9194 }
9195
9196 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9197 }
9198 }
9199
9200 if (old_current_buffer)
9201 set_buffer_internal (old_current_buffer);
9202 }
9203
9204 return window_height_changed_p;
9205 }
9206
9207
9208 /* Value is the current message, a string, or nil if there is no
9209 current message. */
9210
9211 Lisp_Object
9212 current_message (void)
9213 {
9214 Lisp_Object msg;
9215
9216 if (!BUFFERP (echo_area_buffer[0]))
9217 msg = Qnil;
9218 else
9219 {
9220 with_echo_area_buffer (0, 0, current_message_1,
9221 (EMACS_INT) &msg, Qnil, 0, 0);
9222 if (NILP (msg))
9223 echo_area_buffer[0] = Qnil;
9224 }
9225
9226 return msg;
9227 }
9228
9229
9230 static int
9231 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9232 {
9233 Lisp_Object *msg = (Lisp_Object *) a1;
9234
9235 if (Z > BEG)
9236 *msg = make_buffer_string (BEG, Z, 1);
9237 else
9238 *msg = Qnil;
9239 return 0;
9240 }
9241
9242
9243 /* Push the current message on Vmessage_stack for later restauration
9244 by restore_message. Value is non-zero if the current message isn't
9245 empty. This is a relatively infrequent operation, so it's not
9246 worth optimizing. */
9247
9248 int
9249 push_message (void)
9250 {
9251 Lisp_Object msg;
9252 msg = current_message ();
9253 Vmessage_stack = Fcons (msg, Vmessage_stack);
9254 return STRINGP (msg);
9255 }
9256
9257
9258 /* Restore message display from the top of Vmessage_stack. */
9259
9260 void
9261 restore_message (void)
9262 {
9263 Lisp_Object msg;
9264
9265 xassert (CONSP (Vmessage_stack));
9266 msg = XCAR (Vmessage_stack);
9267 if (STRINGP (msg))
9268 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9269 else
9270 message3_nolog (msg, 0, 0);
9271 }
9272
9273
9274 /* Handler for record_unwind_protect calling pop_message. */
9275
9276 Lisp_Object
9277 pop_message_unwind (Lisp_Object dummy)
9278 {
9279 pop_message ();
9280 return Qnil;
9281 }
9282
9283 /* Pop the top-most entry off Vmessage_stack. */
9284
9285 void
9286 pop_message (void)
9287 {
9288 xassert (CONSP (Vmessage_stack));
9289 Vmessage_stack = XCDR (Vmessage_stack);
9290 }
9291
9292
9293 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9294 exits. If the stack is not empty, we have a missing pop_message
9295 somewhere. */
9296
9297 void
9298 check_message_stack (void)
9299 {
9300 if (!NILP (Vmessage_stack))
9301 abort ();
9302 }
9303
9304
9305 /* Truncate to NCHARS what will be displayed in the echo area the next
9306 time we display it---but don't redisplay it now. */
9307
9308 void
9309 truncate_echo_area (EMACS_INT nchars)
9310 {
9311 if (nchars == 0)
9312 echo_area_buffer[0] = Qnil;
9313 /* A null message buffer means that the frame hasn't really been
9314 initialized yet. Error messages get reported properly by
9315 cmd_error, so this must be just an informative message; toss it. */
9316 else if (!noninteractive
9317 && INTERACTIVE
9318 && !NILP (echo_area_buffer[0]))
9319 {
9320 struct frame *sf = SELECTED_FRAME ();
9321 if (FRAME_MESSAGE_BUF (sf))
9322 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9323 }
9324 }
9325
9326
9327 /* Helper function for truncate_echo_area. Truncate the current
9328 message to at most NCHARS characters. */
9329
9330 static int
9331 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9332 {
9333 if (BEG + nchars < Z)
9334 del_range (BEG + nchars, Z);
9335 if (Z == BEG)
9336 echo_area_buffer[0] = Qnil;
9337 return 0;
9338 }
9339
9340
9341 /* Set the current message to a substring of S or STRING.
9342
9343 If STRING is a Lisp string, set the message to the first NBYTES
9344 bytes from STRING. NBYTES zero means use the whole string. If
9345 STRING is multibyte, the message will be displayed multibyte.
9346
9347 If S is not null, set the message to the first LEN bytes of S. LEN
9348 zero means use the whole string. MULTIBYTE_P non-zero means S is
9349 multibyte. Display the message multibyte in that case.
9350
9351 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9352 to t before calling set_message_1 (which calls insert).
9353 */
9354
9355 void
9356 set_message (const char *s, Lisp_Object string,
9357 EMACS_INT nbytes, int multibyte_p)
9358 {
9359 message_enable_multibyte
9360 = ((s && multibyte_p)
9361 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9362
9363 with_echo_area_buffer (0, -1, set_message_1,
9364 (EMACS_INT) s, string, nbytes, multibyte_p);
9365 message_buf_print = 0;
9366 help_echo_showing_p = 0;
9367 }
9368
9369
9370 /* Helper function for set_message. Arguments have the same meaning
9371 as there, with A1 corresponding to S and A2 corresponding to STRING
9372 This function is called with the echo area buffer being
9373 current. */
9374
9375 static int
9376 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9377 {
9378 const char *s = (const char *) a1;
9379 Lisp_Object string = a2;
9380
9381 /* Change multibyteness of the echo buffer appropriately. */
9382 if (message_enable_multibyte
9383 != !NILP (current_buffer->enable_multibyte_characters))
9384 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9385
9386 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9387
9388 /* Insert new message at BEG. */
9389 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9390
9391 if (STRINGP (string))
9392 {
9393 EMACS_INT nchars;
9394
9395 if (nbytes == 0)
9396 nbytes = SBYTES (string);
9397 nchars = string_byte_to_char (string, nbytes);
9398
9399 /* This function takes care of single/multibyte conversion. We
9400 just have to ensure that the echo area buffer has the right
9401 setting of enable_multibyte_characters. */
9402 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9403 }
9404 else if (s)
9405 {
9406 if (nbytes == 0)
9407 nbytes = strlen (s);
9408
9409 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9410 {
9411 /* Convert from multi-byte to single-byte. */
9412 EMACS_INT i;
9413 int c, n;
9414 unsigned char work[1];
9415
9416 /* Convert a multibyte string to single-byte. */
9417 for (i = 0; i < nbytes; i += n)
9418 {
9419 c = string_char_and_length (s + i, &n);
9420 work[0] = (ASCII_CHAR_P (c)
9421 ? c
9422 : multibyte_char_to_unibyte (c, Qnil));
9423 insert_1_both (work, 1, 1, 1, 0, 0);
9424 }
9425 }
9426 else if (!multibyte_p
9427 && !NILP (current_buffer->enable_multibyte_characters))
9428 {
9429 /* Convert from single-byte to multi-byte. */
9430 EMACS_INT i;
9431 int c, n;
9432 const unsigned char *msg = (const unsigned char *) s;
9433 unsigned char str[MAX_MULTIBYTE_LENGTH];
9434
9435 /* Convert a single-byte string to multibyte. */
9436 for (i = 0; i < nbytes; i++)
9437 {
9438 c = msg[i];
9439 MAKE_CHAR_MULTIBYTE (c);
9440 n = CHAR_STRING (c, str);
9441 insert_1_both (str, 1, n, 1, 0, 0);
9442 }
9443 }
9444 else
9445 insert_1 (s, nbytes, 1, 0, 0);
9446 }
9447
9448 return 0;
9449 }
9450
9451
9452 /* Clear messages. CURRENT_P non-zero means clear the current
9453 message. LAST_DISPLAYED_P non-zero means clear the message
9454 last displayed. */
9455
9456 void
9457 clear_message (int current_p, int last_displayed_p)
9458 {
9459 if (current_p)
9460 {
9461 echo_area_buffer[0] = Qnil;
9462 message_cleared_p = 1;
9463 }
9464
9465 if (last_displayed_p)
9466 echo_area_buffer[1] = Qnil;
9467
9468 message_buf_print = 0;
9469 }
9470
9471 /* Clear garbaged frames.
9472
9473 This function is used where the old redisplay called
9474 redraw_garbaged_frames which in turn called redraw_frame which in
9475 turn called clear_frame. The call to clear_frame was a source of
9476 flickering. I believe a clear_frame is not necessary. It should
9477 suffice in the new redisplay to invalidate all current matrices,
9478 and ensure a complete redisplay of all windows. */
9479
9480 static void
9481 clear_garbaged_frames (void)
9482 {
9483 if (frame_garbaged)
9484 {
9485 Lisp_Object tail, frame;
9486 int changed_count = 0;
9487
9488 FOR_EACH_FRAME (tail, frame)
9489 {
9490 struct frame *f = XFRAME (frame);
9491
9492 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9493 {
9494 if (f->resized_p)
9495 {
9496 Fredraw_frame (frame);
9497 f->force_flush_display_p = 1;
9498 }
9499 clear_current_matrices (f);
9500 changed_count++;
9501 f->garbaged = 0;
9502 f->resized_p = 0;
9503 }
9504 }
9505
9506 frame_garbaged = 0;
9507 if (changed_count)
9508 ++windows_or_buffers_changed;
9509 }
9510 }
9511
9512
9513 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9514 is non-zero update selected_frame. Value is non-zero if the
9515 mini-windows height has been changed. */
9516
9517 static int
9518 echo_area_display (int update_frame_p)
9519 {
9520 Lisp_Object mini_window;
9521 struct window *w;
9522 struct frame *f;
9523 int window_height_changed_p = 0;
9524 struct frame *sf = SELECTED_FRAME ();
9525
9526 mini_window = FRAME_MINIBUF_WINDOW (sf);
9527 w = XWINDOW (mini_window);
9528 f = XFRAME (WINDOW_FRAME (w));
9529
9530 /* Don't display if frame is invisible or not yet initialized. */
9531 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9532 return 0;
9533
9534 #ifdef HAVE_WINDOW_SYSTEM
9535 /* When Emacs starts, selected_frame may be the initial terminal
9536 frame. If we let this through, a message would be displayed on
9537 the terminal. */
9538 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9539 return 0;
9540 #endif /* HAVE_WINDOW_SYSTEM */
9541
9542 /* Redraw garbaged frames. */
9543 if (frame_garbaged)
9544 clear_garbaged_frames ();
9545
9546 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9547 {
9548 echo_area_window = mini_window;
9549 window_height_changed_p = display_echo_area (w);
9550 w->must_be_updated_p = 1;
9551
9552 /* Update the display, unless called from redisplay_internal.
9553 Also don't update the screen during redisplay itself. The
9554 update will happen at the end of redisplay, and an update
9555 here could cause confusion. */
9556 if (update_frame_p && !redisplaying_p)
9557 {
9558 int n = 0;
9559
9560 /* If the display update has been interrupted by pending
9561 input, update mode lines in the frame. Due to the
9562 pending input, it might have been that redisplay hasn't
9563 been called, so that mode lines above the echo area are
9564 garbaged. This looks odd, so we prevent it here. */
9565 if (!display_completed)
9566 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9567
9568 if (window_height_changed_p
9569 /* Don't do this if Emacs is shutting down. Redisplay
9570 needs to run hooks. */
9571 && !NILP (Vrun_hooks))
9572 {
9573 /* Must update other windows. Likewise as in other
9574 cases, don't let this update be interrupted by
9575 pending input. */
9576 int count = SPECPDL_INDEX ();
9577 specbind (Qredisplay_dont_pause, Qt);
9578 windows_or_buffers_changed = 1;
9579 redisplay_internal (0);
9580 unbind_to (count, Qnil);
9581 }
9582 else if (FRAME_WINDOW_P (f) && n == 0)
9583 {
9584 /* Window configuration is the same as before.
9585 Can do with a display update of the echo area,
9586 unless we displayed some mode lines. */
9587 update_single_window (w, 1);
9588 FRAME_RIF (f)->flush_display (f);
9589 }
9590 else
9591 update_frame (f, 1, 1);
9592
9593 /* If cursor is in the echo area, make sure that the next
9594 redisplay displays the minibuffer, so that the cursor will
9595 be replaced with what the minibuffer wants. */
9596 if (cursor_in_echo_area)
9597 ++windows_or_buffers_changed;
9598 }
9599 }
9600 else if (!EQ (mini_window, selected_window))
9601 windows_or_buffers_changed++;
9602
9603 /* Last displayed message is now the current message. */
9604 echo_area_buffer[1] = echo_area_buffer[0];
9605 /* Inform read_char that we're not echoing. */
9606 echo_message_buffer = Qnil;
9607
9608 /* Prevent redisplay optimization in redisplay_internal by resetting
9609 this_line_start_pos. This is done because the mini-buffer now
9610 displays the message instead of its buffer text. */
9611 if (EQ (mini_window, selected_window))
9612 CHARPOS (this_line_start_pos) = 0;
9613
9614 return window_height_changed_p;
9615 }
9616
9617
9618 \f
9619 /***********************************************************************
9620 Mode Lines and Frame Titles
9621 ***********************************************************************/
9622
9623 /* A buffer for constructing non-propertized mode-line strings and
9624 frame titles in it; allocated from the heap in init_xdisp and
9625 resized as needed in store_mode_line_noprop_char. */
9626
9627 static char *mode_line_noprop_buf;
9628
9629 /* The buffer's end, and a current output position in it. */
9630
9631 static char *mode_line_noprop_buf_end;
9632 static char *mode_line_noprop_ptr;
9633
9634 #define MODE_LINE_NOPROP_LEN(start) \
9635 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9636
9637 static enum {
9638 MODE_LINE_DISPLAY = 0,
9639 MODE_LINE_TITLE,
9640 MODE_LINE_NOPROP,
9641 MODE_LINE_STRING
9642 } mode_line_target;
9643
9644 /* Alist that caches the results of :propertize.
9645 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9646 static Lisp_Object mode_line_proptrans_alist;
9647
9648 /* List of strings making up the mode-line. */
9649 static Lisp_Object mode_line_string_list;
9650
9651 /* Base face property when building propertized mode line string. */
9652 static Lisp_Object mode_line_string_face;
9653 static Lisp_Object mode_line_string_face_prop;
9654
9655
9656 /* Unwind data for mode line strings */
9657
9658 static Lisp_Object Vmode_line_unwind_vector;
9659
9660 static Lisp_Object
9661 format_mode_line_unwind_data (struct buffer *obuf,
9662 Lisp_Object owin,
9663 int save_proptrans)
9664 {
9665 Lisp_Object vector, tmp;
9666
9667 /* Reduce consing by keeping one vector in
9668 Vwith_echo_area_save_vector. */
9669 vector = Vmode_line_unwind_vector;
9670 Vmode_line_unwind_vector = Qnil;
9671
9672 if (NILP (vector))
9673 vector = Fmake_vector (make_number (8), Qnil);
9674
9675 ASET (vector, 0, make_number (mode_line_target));
9676 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9677 ASET (vector, 2, mode_line_string_list);
9678 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9679 ASET (vector, 4, mode_line_string_face);
9680 ASET (vector, 5, mode_line_string_face_prop);
9681
9682 if (obuf)
9683 XSETBUFFER (tmp, obuf);
9684 else
9685 tmp = Qnil;
9686 ASET (vector, 6, tmp);
9687 ASET (vector, 7, owin);
9688
9689 return vector;
9690 }
9691
9692 static Lisp_Object
9693 unwind_format_mode_line (Lisp_Object vector)
9694 {
9695 mode_line_target = XINT (AREF (vector, 0));
9696 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9697 mode_line_string_list = AREF (vector, 2);
9698 if (! EQ (AREF (vector, 3), Qt))
9699 mode_line_proptrans_alist = AREF (vector, 3);
9700 mode_line_string_face = AREF (vector, 4);
9701 mode_line_string_face_prop = AREF (vector, 5);
9702
9703 if (!NILP (AREF (vector, 7)))
9704 /* Select window before buffer, since it may change the buffer. */
9705 Fselect_window (AREF (vector, 7), Qt);
9706
9707 if (!NILP (AREF (vector, 6)))
9708 {
9709 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9710 ASET (vector, 6, Qnil);
9711 }
9712
9713 Vmode_line_unwind_vector = vector;
9714 return Qnil;
9715 }
9716
9717
9718 /* Store a single character C for the frame title in mode_line_noprop_buf.
9719 Re-allocate mode_line_noprop_buf if necessary. */
9720
9721 static void
9722 store_mode_line_noprop_char (char c)
9723 {
9724 /* If output position has reached the end of the allocated buffer,
9725 double the buffer's size. */
9726 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9727 {
9728 int len = MODE_LINE_NOPROP_LEN (0);
9729 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9730 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9731 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9732 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9733 }
9734
9735 *mode_line_noprop_ptr++ = c;
9736 }
9737
9738
9739 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9740 mode_line_noprop_ptr. STR is the string to store. Do not copy
9741 characters that yield more columns than PRECISION; PRECISION <= 0
9742 means copy the whole string. Pad with spaces until FIELD_WIDTH
9743 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9744 pad. Called from display_mode_element when it is used to build a
9745 frame title. */
9746
9747 static int
9748 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9749 {
9750 int n = 0;
9751 EMACS_INT dummy, nbytes;
9752
9753 /* Copy at most PRECISION chars from STR. */
9754 nbytes = strlen (str);
9755 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9756 while (nbytes--)
9757 store_mode_line_noprop_char (*str++);
9758
9759 /* Fill up with spaces until FIELD_WIDTH reached. */
9760 while (field_width > 0
9761 && n < field_width)
9762 {
9763 store_mode_line_noprop_char (' ');
9764 ++n;
9765 }
9766
9767 return n;
9768 }
9769
9770 /***********************************************************************
9771 Frame Titles
9772 ***********************************************************************/
9773
9774 #ifdef HAVE_WINDOW_SYSTEM
9775
9776 /* Set the title of FRAME, if it has changed. The title format is
9777 Vicon_title_format if FRAME is iconified, otherwise it is
9778 frame_title_format. */
9779
9780 static void
9781 x_consider_frame_title (Lisp_Object frame)
9782 {
9783 struct frame *f = XFRAME (frame);
9784
9785 if (FRAME_WINDOW_P (f)
9786 || FRAME_MINIBUF_ONLY_P (f)
9787 || f->explicit_name)
9788 {
9789 /* Do we have more than one visible frame on this X display? */
9790 Lisp_Object tail;
9791 Lisp_Object fmt;
9792 int title_start;
9793 char *title;
9794 int len;
9795 struct it it;
9796 int count = SPECPDL_INDEX ();
9797
9798 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9799 {
9800 Lisp_Object other_frame = XCAR (tail);
9801 struct frame *tf = XFRAME (other_frame);
9802
9803 if (tf != f
9804 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9805 && !FRAME_MINIBUF_ONLY_P (tf)
9806 && !EQ (other_frame, tip_frame)
9807 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9808 break;
9809 }
9810
9811 /* Set global variable indicating that multiple frames exist. */
9812 multiple_frames = CONSP (tail);
9813
9814 /* Switch to the buffer of selected window of the frame. Set up
9815 mode_line_target so that display_mode_element will output into
9816 mode_line_noprop_buf; then display the title. */
9817 record_unwind_protect (unwind_format_mode_line,
9818 format_mode_line_unwind_data
9819 (current_buffer, selected_window, 0));
9820
9821 Fselect_window (f->selected_window, Qt);
9822 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9823 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9824
9825 mode_line_target = MODE_LINE_TITLE;
9826 title_start = MODE_LINE_NOPROP_LEN (0);
9827 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9828 NULL, DEFAULT_FACE_ID);
9829 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9830 len = MODE_LINE_NOPROP_LEN (title_start);
9831 title = mode_line_noprop_buf + title_start;
9832 unbind_to (count, Qnil);
9833
9834 /* Set the title only if it's changed. This avoids consing in
9835 the common case where it hasn't. (If it turns out that we've
9836 already wasted too much time by walking through the list with
9837 display_mode_element, then we might need to optimize at a
9838 higher level than this.) */
9839 if (! STRINGP (f->name)
9840 || SBYTES (f->name) != len
9841 || memcmp (title, SDATA (f->name), len) != 0)
9842 x_implicitly_set_name (f, make_string (title, len), Qnil);
9843 }
9844 }
9845
9846 #endif /* not HAVE_WINDOW_SYSTEM */
9847
9848
9849
9850 \f
9851 /***********************************************************************
9852 Menu Bars
9853 ***********************************************************************/
9854
9855
9856 /* Prepare for redisplay by updating menu-bar item lists when
9857 appropriate. This can call eval. */
9858
9859 void
9860 prepare_menu_bars (void)
9861 {
9862 int all_windows;
9863 struct gcpro gcpro1, gcpro2;
9864 struct frame *f;
9865 Lisp_Object tooltip_frame;
9866
9867 #ifdef HAVE_WINDOW_SYSTEM
9868 tooltip_frame = tip_frame;
9869 #else
9870 tooltip_frame = Qnil;
9871 #endif
9872
9873 /* Update all frame titles based on their buffer names, etc. We do
9874 this before the menu bars so that the buffer-menu will show the
9875 up-to-date frame titles. */
9876 #ifdef HAVE_WINDOW_SYSTEM
9877 if (windows_or_buffers_changed || update_mode_lines)
9878 {
9879 Lisp_Object tail, frame;
9880
9881 FOR_EACH_FRAME (tail, frame)
9882 {
9883 f = XFRAME (frame);
9884 if (!EQ (frame, tooltip_frame)
9885 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9886 x_consider_frame_title (frame);
9887 }
9888 }
9889 #endif /* HAVE_WINDOW_SYSTEM */
9890
9891 /* Update the menu bar item lists, if appropriate. This has to be
9892 done before any actual redisplay or generation of display lines. */
9893 all_windows = (update_mode_lines
9894 || buffer_shared > 1
9895 || windows_or_buffers_changed);
9896 if (all_windows)
9897 {
9898 Lisp_Object tail, frame;
9899 int count = SPECPDL_INDEX ();
9900 /* 1 means that update_menu_bar has run its hooks
9901 so any further calls to update_menu_bar shouldn't do so again. */
9902 int menu_bar_hooks_run = 0;
9903
9904 record_unwind_save_match_data ();
9905
9906 FOR_EACH_FRAME (tail, frame)
9907 {
9908 f = XFRAME (frame);
9909
9910 /* Ignore tooltip frame. */
9911 if (EQ (frame, tooltip_frame))
9912 continue;
9913
9914 /* If a window on this frame changed size, report that to
9915 the user and clear the size-change flag. */
9916 if (FRAME_WINDOW_SIZES_CHANGED (f))
9917 {
9918 Lisp_Object functions;
9919
9920 /* Clear flag first in case we get an error below. */
9921 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9922 functions = Vwindow_size_change_functions;
9923 GCPRO2 (tail, functions);
9924
9925 while (CONSP (functions))
9926 {
9927 if (!EQ (XCAR (functions), Qt))
9928 call1 (XCAR (functions), frame);
9929 functions = XCDR (functions);
9930 }
9931 UNGCPRO;
9932 }
9933
9934 GCPRO1 (tail);
9935 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9936 #ifdef HAVE_WINDOW_SYSTEM
9937 update_tool_bar (f, 0);
9938 #endif
9939 #ifdef HAVE_NS
9940 if (windows_or_buffers_changed
9941 && FRAME_NS_P (f))
9942 ns_set_doc_edited (f, Fbuffer_modified_p
9943 (XWINDOW (f->selected_window)->buffer));
9944 #endif
9945 UNGCPRO;
9946 }
9947
9948 unbind_to (count, Qnil);
9949 }
9950 else
9951 {
9952 struct frame *sf = SELECTED_FRAME ();
9953 update_menu_bar (sf, 1, 0);
9954 #ifdef HAVE_WINDOW_SYSTEM
9955 update_tool_bar (sf, 1);
9956 #endif
9957 }
9958 }
9959
9960
9961 /* Update the menu bar item list for frame F. This has to be done
9962 before we start to fill in any display lines, because it can call
9963 eval.
9964
9965 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9966
9967 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9968 already ran the menu bar hooks for this redisplay, so there
9969 is no need to run them again. The return value is the
9970 updated value of this flag, to pass to the next call. */
9971
9972 static int
9973 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9974 {
9975 Lisp_Object window;
9976 register struct window *w;
9977
9978 /* If called recursively during a menu update, do nothing. This can
9979 happen when, for instance, an activate-menubar-hook causes a
9980 redisplay. */
9981 if (inhibit_menubar_update)
9982 return hooks_run;
9983
9984 window = FRAME_SELECTED_WINDOW (f);
9985 w = XWINDOW (window);
9986
9987 if (FRAME_WINDOW_P (f)
9988 ?
9989 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9990 || defined (HAVE_NS) || defined (USE_GTK)
9991 FRAME_EXTERNAL_MENU_BAR (f)
9992 #else
9993 FRAME_MENU_BAR_LINES (f) > 0
9994 #endif
9995 : FRAME_MENU_BAR_LINES (f) > 0)
9996 {
9997 /* If the user has switched buffers or windows, we need to
9998 recompute to reflect the new bindings. But we'll
9999 recompute when update_mode_lines is set too; that means
10000 that people can use force-mode-line-update to request
10001 that the menu bar be recomputed. The adverse effect on
10002 the rest of the redisplay algorithm is about the same as
10003 windows_or_buffers_changed anyway. */
10004 if (windows_or_buffers_changed
10005 /* This used to test w->update_mode_line, but we believe
10006 there is no need to recompute the menu in that case. */
10007 || update_mode_lines
10008 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10009 < BUF_MODIFF (XBUFFER (w->buffer)))
10010 != !NILP (w->last_had_star))
10011 || ((!NILP (Vtransient_mark_mode)
10012 && !NILP (XBUFFER (w->buffer)->mark_active))
10013 != !NILP (w->region_showing)))
10014 {
10015 struct buffer *prev = current_buffer;
10016 int count = SPECPDL_INDEX ();
10017
10018 specbind (Qinhibit_menubar_update, Qt);
10019
10020 set_buffer_internal_1 (XBUFFER (w->buffer));
10021 if (save_match_data)
10022 record_unwind_save_match_data ();
10023 if (NILP (Voverriding_local_map_menu_flag))
10024 {
10025 specbind (Qoverriding_terminal_local_map, Qnil);
10026 specbind (Qoverriding_local_map, Qnil);
10027 }
10028
10029 if (!hooks_run)
10030 {
10031 /* Run the Lucid hook. */
10032 safe_run_hooks (Qactivate_menubar_hook);
10033
10034 /* If it has changed current-menubar from previous value,
10035 really recompute the menu-bar from the value. */
10036 if (! NILP (Vlucid_menu_bar_dirty_flag))
10037 call0 (Qrecompute_lucid_menubar);
10038
10039 safe_run_hooks (Qmenu_bar_update_hook);
10040
10041 hooks_run = 1;
10042 }
10043
10044 XSETFRAME (Vmenu_updating_frame, f);
10045 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10046
10047 /* Redisplay the menu bar in case we changed it. */
10048 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10049 || defined (HAVE_NS) || defined (USE_GTK)
10050 if (FRAME_WINDOW_P (f))
10051 {
10052 #if defined (HAVE_NS)
10053 /* All frames on Mac OS share the same menubar. So only
10054 the selected frame should be allowed to set it. */
10055 if (f == SELECTED_FRAME ())
10056 #endif
10057 set_frame_menubar (f, 0, 0);
10058 }
10059 else
10060 /* On a terminal screen, the menu bar is an ordinary screen
10061 line, and this makes it get updated. */
10062 w->update_mode_line = Qt;
10063 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10064 /* In the non-toolkit version, the menu bar is an ordinary screen
10065 line, and this makes it get updated. */
10066 w->update_mode_line = Qt;
10067 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10068
10069 unbind_to (count, Qnil);
10070 set_buffer_internal_1 (prev);
10071 }
10072 }
10073
10074 return hooks_run;
10075 }
10076
10077
10078 \f
10079 /***********************************************************************
10080 Output Cursor
10081 ***********************************************************************/
10082
10083 #ifdef HAVE_WINDOW_SYSTEM
10084
10085 /* EXPORT:
10086 Nominal cursor position -- where to draw output.
10087 HPOS and VPOS are window relative glyph matrix coordinates.
10088 X and Y are window relative pixel coordinates. */
10089
10090 struct cursor_pos output_cursor;
10091
10092
10093 /* EXPORT:
10094 Set the global variable output_cursor to CURSOR. All cursor
10095 positions are relative to updated_window. */
10096
10097 void
10098 set_output_cursor (struct cursor_pos *cursor)
10099 {
10100 output_cursor.hpos = cursor->hpos;
10101 output_cursor.vpos = cursor->vpos;
10102 output_cursor.x = cursor->x;
10103 output_cursor.y = cursor->y;
10104 }
10105
10106
10107 /* EXPORT for RIF:
10108 Set a nominal cursor position.
10109
10110 HPOS and VPOS are column/row positions in a window glyph matrix. X
10111 and Y are window text area relative pixel positions.
10112
10113 If this is done during an update, updated_window will contain the
10114 window that is being updated and the position is the future output
10115 cursor position for that window. If updated_window is null, use
10116 selected_window and display the cursor at the given position. */
10117
10118 void
10119 x_cursor_to (int vpos, int hpos, int y, int x)
10120 {
10121 struct window *w;
10122
10123 /* If updated_window is not set, work on selected_window. */
10124 if (updated_window)
10125 w = updated_window;
10126 else
10127 w = XWINDOW (selected_window);
10128
10129 /* Set the output cursor. */
10130 output_cursor.hpos = hpos;
10131 output_cursor.vpos = vpos;
10132 output_cursor.x = x;
10133 output_cursor.y = y;
10134
10135 /* If not called as part of an update, really display the cursor.
10136 This will also set the cursor position of W. */
10137 if (updated_window == NULL)
10138 {
10139 BLOCK_INPUT;
10140 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10141 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10142 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10143 UNBLOCK_INPUT;
10144 }
10145 }
10146
10147 #endif /* HAVE_WINDOW_SYSTEM */
10148
10149 \f
10150 /***********************************************************************
10151 Tool-bars
10152 ***********************************************************************/
10153
10154 #ifdef HAVE_WINDOW_SYSTEM
10155
10156 /* Where the mouse was last time we reported a mouse event. */
10157
10158 FRAME_PTR last_mouse_frame;
10159
10160 /* Tool-bar item index of the item on which a mouse button was pressed
10161 or -1. */
10162
10163 int last_tool_bar_item;
10164
10165
10166 static Lisp_Object
10167 update_tool_bar_unwind (Lisp_Object frame)
10168 {
10169 selected_frame = frame;
10170 return Qnil;
10171 }
10172
10173 /* Update the tool-bar item list for frame F. This has to be done
10174 before we start to fill in any display lines. Called from
10175 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10176 and restore it here. */
10177
10178 static void
10179 update_tool_bar (struct frame *f, int save_match_data)
10180 {
10181 #if defined (USE_GTK) || defined (HAVE_NS)
10182 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10183 #else
10184 int do_update = WINDOWP (f->tool_bar_window)
10185 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10186 #endif
10187
10188 if (do_update)
10189 {
10190 Lisp_Object window;
10191 struct window *w;
10192
10193 window = FRAME_SELECTED_WINDOW (f);
10194 w = XWINDOW (window);
10195
10196 /* If the user has switched buffers or windows, we need to
10197 recompute to reflect the new bindings. But we'll
10198 recompute when update_mode_lines is set too; that means
10199 that people can use force-mode-line-update to request
10200 that the menu bar be recomputed. The adverse effect on
10201 the rest of the redisplay algorithm is about the same as
10202 windows_or_buffers_changed anyway. */
10203 if (windows_or_buffers_changed
10204 || !NILP (w->update_mode_line)
10205 || update_mode_lines
10206 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10207 < BUF_MODIFF (XBUFFER (w->buffer)))
10208 != !NILP (w->last_had_star))
10209 || ((!NILP (Vtransient_mark_mode)
10210 && !NILP (XBUFFER (w->buffer)->mark_active))
10211 != !NILP (w->region_showing)))
10212 {
10213 struct buffer *prev = current_buffer;
10214 int count = SPECPDL_INDEX ();
10215 Lisp_Object frame, new_tool_bar;
10216 int new_n_tool_bar;
10217 struct gcpro gcpro1;
10218
10219 /* Set current_buffer to the buffer of the selected
10220 window of the frame, so that we get the right local
10221 keymaps. */
10222 set_buffer_internal_1 (XBUFFER (w->buffer));
10223
10224 /* Save match data, if we must. */
10225 if (save_match_data)
10226 record_unwind_save_match_data ();
10227
10228 /* Make sure that we don't accidentally use bogus keymaps. */
10229 if (NILP (Voverriding_local_map_menu_flag))
10230 {
10231 specbind (Qoverriding_terminal_local_map, Qnil);
10232 specbind (Qoverriding_local_map, Qnil);
10233 }
10234
10235 GCPRO1 (new_tool_bar);
10236
10237 /* We must temporarily set the selected frame to this frame
10238 before calling tool_bar_items, because the calculation of
10239 the tool-bar keymap uses the selected frame (see
10240 `tool-bar-make-keymap' in tool-bar.el). */
10241 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10242 XSETFRAME (frame, f);
10243 selected_frame = frame;
10244
10245 /* Build desired tool-bar items from keymaps. */
10246 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10247 &new_n_tool_bar);
10248
10249 /* Redisplay the tool-bar if we changed it. */
10250 if (new_n_tool_bar != f->n_tool_bar_items
10251 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10252 {
10253 /* Redisplay that happens asynchronously due to an expose event
10254 may access f->tool_bar_items. Make sure we update both
10255 variables within BLOCK_INPUT so no such event interrupts. */
10256 BLOCK_INPUT;
10257 f->tool_bar_items = new_tool_bar;
10258 f->n_tool_bar_items = new_n_tool_bar;
10259 w->update_mode_line = Qt;
10260 UNBLOCK_INPUT;
10261 }
10262
10263 UNGCPRO;
10264
10265 unbind_to (count, Qnil);
10266 set_buffer_internal_1 (prev);
10267 }
10268 }
10269 }
10270
10271
10272 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10273 F's desired tool-bar contents. F->tool_bar_items must have
10274 been set up previously by calling prepare_menu_bars. */
10275
10276 static void
10277 build_desired_tool_bar_string (struct frame *f)
10278 {
10279 int i, size, size_needed;
10280 struct gcpro gcpro1, gcpro2, gcpro3;
10281 Lisp_Object image, plist, props;
10282
10283 image = plist = props = Qnil;
10284 GCPRO3 (image, plist, props);
10285
10286 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10287 Otherwise, make a new string. */
10288
10289 /* The size of the string we might be able to reuse. */
10290 size = (STRINGP (f->desired_tool_bar_string)
10291 ? SCHARS (f->desired_tool_bar_string)
10292 : 0);
10293
10294 /* We need one space in the string for each image. */
10295 size_needed = f->n_tool_bar_items;
10296
10297 /* Reuse f->desired_tool_bar_string, if possible. */
10298 if (size < size_needed || NILP (f->desired_tool_bar_string))
10299 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10300 make_number (' '));
10301 else
10302 {
10303 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10304 Fremove_text_properties (make_number (0), make_number (size),
10305 props, f->desired_tool_bar_string);
10306 }
10307
10308 /* Put a `display' property on the string for the images to display,
10309 put a `menu_item' property on tool-bar items with a value that
10310 is the index of the item in F's tool-bar item vector. */
10311 for (i = 0; i < f->n_tool_bar_items; ++i)
10312 {
10313 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10314
10315 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10316 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10317 int hmargin, vmargin, relief, idx, end;
10318
10319 /* If image is a vector, choose the image according to the
10320 button state. */
10321 image = PROP (TOOL_BAR_ITEM_IMAGES);
10322 if (VECTORP (image))
10323 {
10324 if (enabled_p)
10325 idx = (selected_p
10326 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10327 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10328 else
10329 idx = (selected_p
10330 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10331 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10332
10333 xassert (ASIZE (image) >= idx);
10334 image = AREF (image, idx);
10335 }
10336 else
10337 idx = -1;
10338
10339 /* Ignore invalid image specifications. */
10340 if (!valid_image_p (image))
10341 continue;
10342
10343 /* Display the tool-bar button pressed, or depressed. */
10344 plist = Fcopy_sequence (XCDR (image));
10345
10346 /* Compute margin and relief to draw. */
10347 relief = (tool_bar_button_relief >= 0
10348 ? tool_bar_button_relief
10349 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10350 hmargin = vmargin = relief;
10351
10352 if (INTEGERP (Vtool_bar_button_margin)
10353 && XINT (Vtool_bar_button_margin) > 0)
10354 {
10355 hmargin += XFASTINT (Vtool_bar_button_margin);
10356 vmargin += XFASTINT (Vtool_bar_button_margin);
10357 }
10358 else if (CONSP (Vtool_bar_button_margin))
10359 {
10360 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10361 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10362 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10363
10364 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10365 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10366 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10367 }
10368
10369 if (auto_raise_tool_bar_buttons_p)
10370 {
10371 /* Add a `:relief' property to the image spec if the item is
10372 selected. */
10373 if (selected_p)
10374 {
10375 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10376 hmargin -= relief;
10377 vmargin -= relief;
10378 }
10379 }
10380 else
10381 {
10382 /* If image is selected, display it pressed, i.e. with a
10383 negative relief. If it's not selected, display it with a
10384 raised relief. */
10385 plist = Fplist_put (plist, QCrelief,
10386 (selected_p
10387 ? make_number (-relief)
10388 : make_number (relief)));
10389 hmargin -= relief;
10390 vmargin -= relief;
10391 }
10392
10393 /* Put a margin around the image. */
10394 if (hmargin || vmargin)
10395 {
10396 if (hmargin == vmargin)
10397 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10398 else
10399 plist = Fplist_put (plist, QCmargin,
10400 Fcons (make_number (hmargin),
10401 make_number (vmargin)));
10402 }
10403
10404 /* If button is not enabled, and we don't have special images
10405 for the disabled state, make the image appear disabled by
10406 applying an appropriate algorithm to it. */
10407 if (!enabled_p && idx < 0)
10408 plist = Fplist_put (plist, QCconversion, Qdisabled);
10409
10410 /* Put a `display' text property on the string for the image to
10411 display. Put a `menu-item' property on the string that gives
10412 the start of this item's properties in the tool-bar items
10413 vector. */
10414 image = Fcons (Qimage, plist);
10415 props = list4 (Qdisplay, image,
10416 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10417
10418 /* Let the last image hide all remaining spaces in the tool bar
10419 string. The string can be longer than needed when we reuse a
10420 previous string. */
10421 if (i + 1 == f->n_tool_bar_items)
10422 end = SCHARS (f->desired_tool_bar_string);
10423 else
10424 end = i + 1;
10425 Fadd_text_properties (make_number (i), make_number (end),
10426 props, f->desired_tool_bar_string);
10427 #undef PROP
10428 }
10429
10430 UNGCPRO;
10431 }
10432
10433
10434 /* Display one line of the tool-bar of frame IT->f.
10435
10436 HEIGHT specifies the desired height of the tool-bar line.
10437 If the actual height of the glyph row is less than HEIGHT, the
10438 row's height is increased to HEIGHT, and the icons are centered
10439 vertically in the new height.
10440
10441 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10442 count a final empty row in case the tool-bar width exactly matches
10443 the window width.
10444 */
10445
10446 static void
10447 display_tool_bar_line (struct it *it, int height)
10448 {
10449 struct glyph_row *row = it->glyph_row;
10450 int max_x = it->last_visible_x;
10451 struct glyph *last;
10452
10453 prepare_desired_row (row);
10454 row->y = it->current_y;
10455
10456 /* Note that this isn't made use of if the face hasn't a box,
10457 so there's no need to check the face here. */
10458 it->start_of_box_run_p = 1;
10459
10460 while (it->current_x < max_x)
10461 {
10462 int x, n_glyphs_before, i, nglyphs;
10463 struct it it_before;
10464
10465 /* Get the next display element. */
10466 if (!get_next_display_element (it))
10467 {
10468 /* Don't count empty row if we are counting needed tool-bar lines. */
10469 if (height < 0 && !it->hpos)
10470 return;
10471 break;
10472 }
10473
10474 /* Produce glyphs. */
10475 n_glyphs_before = row->used[TEXT_AREA];
10476 it_before = *it;
10477
10478 PRODUCE_GLYPHS (it);
10479
10480 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10481 i = 0;
10482 x = it_before.current_x;
10483 while (i < nglyphs)
10484 {
10485 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10486
10487 if (x + glyph->pixel_width > max_x)
10488 {
10489 /* Glyph doesn't fit on line. Backtrack. */
10490 row->used[TEXT_AREA] = n_glyphs_before;
10491 *it = it_before;
10492 /* If this is the only glyph on this line, it will never fit on the
10493 toolbar, so skip it. But ensure there is at least one glyph,
10494 so we don't accidentally disable the tool-bar. */
10495 if (n_glyphs_before == 0
10496 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10497 break;
10498 goto out;
10499 }
10500
10501 ++it->hpos;
10502 x += glyph->pixel_width;
10503 ++i;
10504 }
10505
10506 /* Stop at line ends. */
10507 if (ITERATOR_AT_END_OF_LINE_P (it))
10508 break;
10509
10510 set_iterator_to_next (it, 1);
10511 }
10512
10513 out:;
10514
10515 row->displays_text_p = row->used[TEXT_AREA] != 0;
10516
10517 /* Use default face for the border below the tool bar.
10518
10519 FIXME: When auto-resize-tool-bars is grow-only, there is
10520 no additional border below the possibly empty tool-bar lines.
10521 So to make the extra empty lines look "normal", we have to
10522 use the tool-bar face for the border too. */
10523 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10524 it->face_id = DEFAULT_FACE_ID;
10525
10526 extend_face_to_end_of_line (it);
10527 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10528 last->right_box_line_p = 1;
10529 if (last == row->glyphs[TEXT_AREA])
10530 last->left_box_line_p = 1;
10531
10532 /* Make line the desired height and center it vertically. */
10533 if ((height -= it->max_ascent + it->max_descent) > 0)
10534 {
10535 /* Don't add more than one line height. */
10536 height %= FRAME_LINE_HEIGHT (it->f);
10537 it->max_ascent += height / 2;
10538 it->max_descent += (height + 1) / 2;
10539 }
10540
10541 compute_line_metrics (it);
10542
10543 /* If line is empty, make it occupy the rest of the tool-bar. */
10544 if (!row->displays_text_p)
10545 {
10546 row->height = row->phys_height = it->last_visible_y - row->y;
10547 row->visible_height = row->height;
10548 row->ascent = row->phys_ascent = 0;
10549 row->extra_line_spacing = 0;
10550 }
10551
10552 row->full_width_p = 1;
10553 row->continued_p = 0;
10554 row->truncated_on_left_p = 0;
10555 row->truncated_on_right_p = 0;
10556
10557 it->current_x = it->hpos = 0;
10558 it->current_y += row->height;
10559 ++it->vpos;
10560 ++it->glyph_row;
10561 }
10562
10563
10564 /* Max tool-bar height. */
10565
10566 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10567 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10568
10569 /* Value is the number of screen lines needed to make all tool-bar
10570 items of frame F visible. The number of actual rows needed is
10571 returned in *N_ROWS if non-NULL. */
10572
10573 static int
10574 tool_bar_lines_needed (struct frame *f, int *n_rows)
10575 {
10576 struct window *w = XWINDOW (f->tool_bar_window);
10577 struct it it;
10578 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10579 the desired matrix, so use (unused) mode-line row as temporary row to
10580 avoid destroying the first tool-bar row. */
10581 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10582
10583 /* Initialize an iterator for iteration over
10584 F->desired_tool_bar_string in the tool-bar window of frame F. */
10585 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10586 it.first_visible_x = 0;
10587 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10588 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10589
10590 while (!ITERATOR_AT_END_P (&it))
10591 {
10592 clear_glyph_row (temp_row);
10593 it.glyph_row = temp_row;
10594 display_tool_bar_line (&it, -1);
10595 }
10596 clear_glyph_row (temp_row);
10597
10598 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10599 if (n_rows)
10600 *n_rows = it.vpos > 0 ? it.vpos : -1;
10601
10602 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10603 }
10604
10605
10606 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10607 0, 1, 0,
10608 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10609 (Lisp_Object frame)
10610 {
10611 struct frame *f;
10612 struct window *w;
10613 int nlines = 0;
10614
10615 if (NILP (frame))
10616 frame = selected_frame;
10617 else
10618 CHECK_FRAME (frame);
10619 f = XFRAME (frame);
10620
10621 if (WINDOWP (f->tool_bar_window)
10622 || (w = XWINDOW (f->tool_bar_window),
10623 WINDOW_TOTAL_LINES (w) > 0))
10624 {
10625 update_tool_bar (f, 1);
10626 if (f->n_tool_bar_items)
10627 {
10628 build_desired_tool_bar_string (f);
10629 nlines = tool_bar_lines_needed (f, NULL);
10630 }
10631 }
10632
10633 return make_number (nlines);
10634 }
10635
10636
10637 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10638 height should be changed. */
10639
10640 static int
10641 redisplay_tool_bar (struct frame *f)
10642 {
10643 struct window *w;
10644 struct it it;
10645 struct glyph_row *row;
10646
10647 #if defined (USE_GTK) || defined (HAVE_NS)
10648 if (FRAME_EXTERNAL_TOOL_BAR (f))
10649 update_frame_tool_bar (f);
10650 return 0;
10651 #endif
10652
10653 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10654 do anything. This means you must start with tool-bar-lines
10655 non-zero to get the auto-sizing effect. Or in other words, you
10656 can turn off tool-bars by specifying tool-bar-lines zero. */
10657 if (!WINDOWP (f->tool_bar_window)
10658 || (w = XWINDOW (f->tool_bar_window),
10659 WINDOW_TOTAL_LINES (w) == 0))
10660 return 0;
10661
10662 /* Set up an iterator for the tool-bar window. */
10663 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10664 it.first_visible_x = 0;
10665 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10666 row = it.glyph_row;
10667
10668 /* Build a string that represents the contents of the tool-bar. */
10669 build_desired_tool_bar_string (f);
10670 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10671
10672 if (f->n_tool_bar_rows == 0)
10673 {
10674 int nlines;
10675
10676 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10677 nlines != WINDOW_TOTAL_LINES (w)))
10678 {
10679 Lisp_Object frame;
10680 int old_height = WINDOW_TOTAL_LINES (w);
10681
10682 XSETFRAME (frame, f);
10683 Fmodify_frame_parameters (frame,
10684 Fcons (Fcons (Qtool_bar_lines,
10685 make_number (nlines)),
10686 Qnil));
10687 if (WINDOW_TOTAL_LINES (w) != old_height)
10688 {
10689 clear_glyph_matrix (w->desired_matrix);
10690 fonts_changed_p = 1;
10691 return 1;
10692 }
10693 }
10694 }
10695
10696 /* Display as many lines as needed to display all tool-bar items. */
10697
10698 if (f->n_tool_bar_rows > 0)
10699 {
10700 int border, rows, height, extra;
10701
10702 if (INTEGERP (Vtool_bar_border))
10703 border = XINT (Vtool_bar_border);
10704 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10705 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10706 else if (EQ (Vtool_bar_border, Qborder_width))
10707 border = f->border_width;
10708 else
10709 border = 0;
10710 if (border < 0)
10711 border = 0;
10712
10713 rows = f->n_tool_bar_rows;
10714 height = max (1, (it.last_visible_y - border) / rows);
10715 extra = it.last_visible_y - border - height * rows;
10716
10717 while (it.current_y < it.last_visible_y)
10718 {
10719 int h = 0;
10720 if (extra > 0 && rows-- > 0)
10721 {
10722 h = (extra + rows - 1) / rows;
10723 extra -= h;
10724 }
10725 display_tool_bar_line (&it, height + h);
10726 }
10727 }
10728 else
10729 {
10730 while (it.current_y < it.last_visible_y)
10731 display_tool_bar_line (&it, 0);
10732 }
10733
10734 /* It doesn't make much sense to try scrolling in the tool-bar
10735 window, so don't do it. */
10736 w->desired_matrix->no_scrolling_p = 1;
10737 w->must_be_updated_p = 1;
10738
10739 if (!NILP (Vauto_resize_tool_bars))
10740 {
10741 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10742 int change_height_p = 0;
10743
10744 /* If we couldn't display everything, change the tool-bar's
10745 height if there is room for more. */
10746 if (IT_STRING_CHARPOS (it) < it.end_charpos
10747 && it.current_y < max_tool_bar_height)
10748 change_height_p = 1;
10749
10750 row = it.glyph_row - 1;
10751
10752 /* If there are blank lines at the end, except for a partially
10753 visible blank line at the end that is smaller than
10754 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10755 if (!row->displays_text_p
10756 && row->height >= FRAME_LINE_HEIGHT (f))
10757 change_height_p = 1;
10758
10759 /* If row displays tool-bar items, but is partially visible,
10760 change the tool-bar's height. */
10761 if (row->displays_text_p
10762 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10763 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10764 change_height_p = 1;
10765
10766 /* Resize windows as needed by changing the `tool-bar-lines'
10767 frame parameter. */
10768 if (change_height_p)
10769 {
10770 Lisp_Object frame;
10771 int old_height = WINDOW_TOTAL_LINES (w);
10772 int nrows;
10773 int nlines = tool_bar_lines_needed (f, &nrows);
10774
10775 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10776 && !f->minimize_tool_bar_window_p)
10777 ? (nlines > old_height)
10778 : (nlines != old_height));
10779 f->minimize_tool_bar_window_p = 0;
10780
10781 if (change_height_p)
10782 {
10783 XSETFRAME (frame, f);
10784 Fmodify_frame_parameters (frame,
10785 Fcons (Fcons (Qtool_bar_lines,
10786 make_number (nlines)),
10787 Qnil));
10788 if (WINDOW_TOTAL_LINES (w) != old_height)
10789 {
10790 clear_glyph_matrix (w->desired_matrix);
10791 f->n_tool_bar_rows = nrows;
10792 fonts_changed_p = 1;
10793 return 1;
10794 }
10795 }
10796 }
10797 }
10798
10799 f->minimize_tool_bar_window_p = 0;
10800 return 0;
10801 }
10802
10803
10804 /* Get information about the tool-bar item which is displayed in GLYPH
10805 on frame F. Return in *PROP_IDX the index where tool-bar item
10806 properties start in F->tool_bar_items. Value is zero if
10807 GLYPH doesn't display a tool-bar item. */
10808
10809 static int
10810 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10811 {
10812 Lisp_Object prop;
10813 int success_p;
10814 int charpos;
10815
10816 /* This function can be called asynchronously, which means we must
10817 exclude any possibility that Fget_text_property signals an
10818 error. */
10819 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10820 charpos = max (0, charpos);
10821
10822 /* Get the text property `menu-item' at pos. The value of that
10823 property is the start index of this item's properties in
10824 F->tool_bar_items. */
10825 prop = Fget_text_property (make_number (charpos),
10826 Qmenu_item, f->current_tool_bar_string);
10827 if (INTEGERP (prop))
10828 {
10829 *prop_idx = XINT (prop);
10830 success_p = 1;
10831 }
10832 else
10833 success_p = 0;
10834
10835 return success_p;
10836 }
10837
10838 \f
10839 /* Get information about the tool-bar item at position X/Y on frame F.
10840 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10841 the current matrix of the tool-bar window of F, or NULL if not
10842 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10843 item in F->tool_bar_items. Value is
10844
10845 -1 if X/Y is not on a tool-bar item
10846 0 if X/Y is on the same item that was highlighted before.
10847 1 otherwise. */
10848
10849 static int
10850 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10851 int *hpos, int *vpos, int *prop_idx)
10852 {
10853 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10854 struct window *w = XWINDOW (f->tool_bar_window);
10855 int area;
10856
10857 /* Find the glyph under X/Y. */
10858 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10859 if (*glyph == NULL)
10860 return -1;
10861
10862 /* Get the start of this tool-bar item's properties in
10863 f->tool_bar_items. */
10864 if (!tool_bar_item_info (f, *glyph, prop_idx))
10865 return -1;
10866
10867 /* Is mouse on the highlighted item? */
10868 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10869 && *vpos >= hlinfo->mouse_face_beg_row
10870 && *vpos <= hlinfo->mouse_face_end_row
10871 && (*vpos > hlinfo->mouse_face_beg_row
10872 || *hpos >= hlinfo->mouse_face_beg_col)
10873 && (*vpos < hlinfo->mouse_face_end_row
10874 || *hpos < hlinfo->mouse_face_end_col
10875 || hlinfo->mouse_face_past_end))
10876 return 0;
10877
10878 return 1;
10879 }
10880
10881
10882 /* EXPORT:
10883 Handle mouse button event on the tool-bar of frame F, at
10884 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10885 0 for button release. MODIFIERS is event modifiers for button
10886 release. */
10887
10888 void
10889 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10890 unsigned int modifiers)
10891 {
10892 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10893 struct window *w = XWINDOW (f->tool_bar_window);
10894 int hpos, vpos, prop_idx;
10895 struct glyph *glyph;
10896 Lisp_Object enabled_p;
10897
10898 /* If not on the highlighted tool-bar item, return. */
10899 frame_to_window_pixel_xy (w, &x, &y);
10900 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10901 return;
10902
10903 /* If item is disabled, do nothing. */
10904 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10905 if (NILP (enabled_p))
10906 return;
10907
10908 if (down_p)
10909 {
10910 /* Show item in pressed state. */
10911 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10912 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10913 last_tool_bar_item = prop_idx;
10914 }
10915 else
10916 {
10917 Lisp_Object key, frame;
10918 struct input_event event;
10919 EVENT_INIT (event);
10920
10921 /* Show item in released state. */
10922 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10923 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10924
10925 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10926
10927 XSETFRAME (frame, f);
10928 event.kind = TOOL_BAR_EVENT;
10929 event.frame_or_window = frame;
10930 event.arg = frame;
10931 kbd_buffer_store_event (&event);
10932
10933 event.kind = TOOL_BAR_EVENT;
10934 event.frame_or_window = frame;
10935 event.arg = key;
10936 event.modifiers = modifiers;
10937 kbd_buffer_store_event (&event);
10938 last_tool_bar_item = -1;
10939 }
10940 }
10941
10942
10943 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10944 tool-bar window-relative coordinates X/Y. Called from
10945 note_mouse_highlight. */
10946
10947 static void
10948 note_tool_bar_highlight (struct frame *f, int x, int y)
10949 {
10950 Lisp_Object window = f->tool_bar_window;
10951 struct window *w = XWINDOW (window);
10952 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10953 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10954 int hpos, vpos;
10955 struct glyph *glyph;
10956 struct glyph_row *row;
10957 int i;
10958 Lisp_Object enabled_p;
10959 int prop_idx;
10960 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10961 int mouse_down_p, rc;
10962
10963 /* Function note_mouse_highlight is called with negative X/Y
10964 values when mouse moves outside of the frame. */
10965 if (x <= 0 || y <= 0)
10966 {
10967 clear_mouse_face (hlinfo);
10968 return;
10969 }
10970
10971 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10972 if (rc < 0)
10973 {
10974 /* Not on tool-bar item. */
10975 clear_mouse_face (hlinfo);
10976 return;
10977 }
10978 else if (rc == 0)
10979 /* On same tool-bar item as before. */
10980 goto set_help_echo;
10981
10982 clear_mouse_face (hlinfo);
10983
10984 /* Mouse is down, but on different tool-bar item? */
10985 mouse_down_p = (dpyinfo->grabbed
10986 && f == last_mouse_frame
10987 && FRAME_LIVE_P (f));
10988 if (mouse_down_p
10989 && last_tool_bar_item != prop_idx)
10990 return;
10991
10992 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10993 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10994
10995 /* If tool-bar item is not enabled, don't highlight it. */
10996 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10997 if (!NILP (enabled_p))
10998 {
10999 /* Compute the x-position of the glyph. In front and past the
11000 image is a space. We include this in the highlighted area. */
11001 row = MATRIX_ROW (w->current_matrix, vpos);
11002 for (i = x = 0; i < hpos; ++i)
11003 x += row->glyphs[TEXT_AREA][i].pixel_width;
11004
11005 /* Record this as the current active region. */
11006 hlinfo->mouse_face_beg_col = hpos;
11007 hlinfo->mouse_face_beg_row = vpos;
11008 hlinfo->mouse_face_beg_x = x;
11009 hlinfo->mouse_face_beg_y = row->y;
11010 hlinfo->mouse_face_past_end = 0;
11011
11012 hlinfo->mouse_face_end_col = hpos + 1;
11013 hlinfo->mouse_face_end_row = vpos;
11014 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11015 hlinfo->mouse_face_end_y = row->y;
11016 hlinfo->mouse_face_window = window;
11017 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11018
11019 /* Display it as active. */
11020 show_mouse_face (hlinfo, draw);
11021 hlinfo->mouse_face_image_state = draw;
11022 }
11023
11024 set_help_echo:
11025
11026 /* Set help_echo_string to a help string to display for this tool-bar item.
11027 XTread_socket does the rest. */
11028 help_echo_object = help_echo_window = Qnil;
11029 help_echo_pos = -1;
11030 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11031 if (NILP (help_echo_string))
11032 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11033 }
11034
11035 #endif /* HAVE_WINDOW_SYSTEM */
11036
11037
11038 \f
11039 /************************************************************************
11040 Horizontal scrolling
11041 ************************************************************************/
11042
11043 static int hscroll_window_tree (Lisp_Object);
11044 static int hscroll_windows (Lisp_Object);
11045
11046 /* For all leaf windows in the window tree rooted at WINDOW, set their
11047 hscroll value so that PT is (i) visible in the window, and (ii) so
11048 that it is not within a certain margin at the window's left and
11049 right border. Value is non-zero if any window's hscroll has been
11050 changed. */
11051
11052 static int
11053 hscroll_window_tree (Lisp_Object window)
11054 {
11055 int hscrolled_p = 0;
11056 int hscroll_relative_p = FLOATP (Vhscroll_step);
11057 int hscroll_step_abs = 0;
11058 double hscroll_step_rel = 0;
11059
11060 if (hscroll_relative_p)
11061 {
11062 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11063 if (hscroll_step_rel < 0)
11064 {
11065 hscroll_relative_p = 0;
11066 hscroll_step_abs = 0;
11067 }
11068 }
11069 else if (INTEGERP (Vhscroll_step))
11070 {
11071 hscroll_step_abs = XINT (Vhscroll_step);
11072 if (hscroll_step_abs < 0)
11073 hscroll_step_abs = 0;
11074 }
11075 else
11076 hscroll_step_abs = 0;
11077
11078 while (WINDOWP (window))
11079 {
11080 struct window *w = XWINDOW (window);
11081
11082 if (WINDOWP (w->hchild))
11083 hscrolled_p |= hscroll_window_tree (w->hchild);
11084 else if (WINDOWP (w->vchild))
11085 hscrolled_p |= hscroll_window_tree (w->vchild);
11086 else if (w->cursor.vpos >= 0)
11087 {
11088 int h_margin;
11089 int text_area_width;
11090 struct glyph_row *current_cursor_row
11091 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11092 struct glyph_row *desired_cursor_row
11093 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11094 struct glyph_row *cursor_row
11095 = (desired_cursor_row->enabled_p
11096 ? desired_cursor_row
11097 : current_cursor_row);
11098
11099 text_area_width = window_box_width (w, TEXT_AREA);
11100
11101 /* Scroll when cursor is inside this scroll margin. */
11102 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11103
11104 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11105 && ((XFASTINT (w->hscroll)
11106 && w->cursor.x <= h_margin)
11107 || (cursor_row->enabled_p
11108 && cursor_row->truncated_on_right_p
11109 && (w->cursor.x >= text_area_width - h_margin))))
11110 {
11111 struct it it;
11112 int hscroll;
11113 struct buffer *saved_current_buffer;
11114 EMACS_INT pt;
11115 int wanted_x;
11116
11117 /* Find point in a display of infinite width. */
11118 saved_current_buffer = current_buffer;
11119 current_buffer = XBUFFER (w->buffer);
11120
11121 if (w == XWINDOW (selected_window))
11122 pt = BUF_PT (current_buffer);
11123 else
11124 {
11125 pt = marker_position (w->pointm);
11126 pt = max (BEGV, pt);
11127 pt = min (ZV, pt);
11128 }
11129
11130 /* Move iterator to pt starting at cursor_row->start in
11131 a line with infinite width. */
11132 init_to_row_start (&it, w, cursor_row);
11133 it.last_visible_x = INFINITY;
11134 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11135 current_buffer = saved_current_buffer;
11136
11137 /* Position cursor in window. */
11138 if (!hscroll_relative_p && hscroll_step_abs == 0)
11139 hscroll = max (0, (it.current_x
11140 - (ITERATOR_AT_END_OF_LINE_P (&it)
11141 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11142 : (text_area_width / 2))))
11143 / FRAME_COLUMN_WIDTH (it.f);
11144 else if (w->cursor.x >= text_area_width - h_margin)
11145 {
11146 if (hscroll_relative_p)
11147 wanted_x = text_area_width * (1 - hscroll_step_rel)
11148 - h_margin;
11149 else
11150 wanted_x = text_area_width
11151 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11152 - h_margin;
11153 hscroll
11154 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11155 }
11156 else
11157 {
11158 if (hscroll_relative_p)
11159 wanted_x = text_area_width * hscroll_step_rel
11160 + h_margin;
11161 else
11162 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11163 + h_margin;
11164 hscroll
11165 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11166 }
11167 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11168
11169 /* Don't call Fset_window_hscroll if value hasn't
11170 changed because it will prevent redisplay
11171 optimizations. */
11172 if (XFASTINT (w->hscroll) != hscroll)
11173 {
11174 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11175 w->hscroll = make_number (hscroll);
11176 hscrolled_p = 1;
11177 }
11178 }
11179 }
11180
11181 window = w->next;
11182 }
11183
11184 /* Value is non-zero if hscroll of any leaf window has been changed. */
11185 return hscrolled_p;
11186 }
11187
11188
11189 /* Set hscroll so that cursor is visible and not inside horizontal
11190 scroll margins for all windows in the tree rooted at WINDOW. See
11191 also hscroll_window_tree above. Value is non-zero if any window's
11192 hscroll has been changed. If it has, desired matrices on the frame
11193 of WINDOW are cleared. */
11194
11195 static int
11196 hscroll_windows (Lisp_Object window)
11197 {
11198 int hscrolled_p = hscroll_window_tree (window);
11199 if (hscrolled_p)
11200 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11201 return hscrolled_p;
11202 }
11203
11204
11205 \f
11206 /************************************************************************
11207 Redisplay
11208 ************************************************************************/
11209
11210 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11211 to a non-zero value. This is sometimes handy to have in a debugger
11212 session. */
11213
11214 #if GLYPH_DEBUG
11215
11216 /* First and last unchanged row for try_window_id. */
11217
11218 int debug_first_unchanged_at_end_vpos;
11219 int debug_last_unchanged_at_beg_vpos;
11220
11221 /* Delta vpos and y. */
11222
11223 int debug_dvpos, debug_dy;
11224
11225 /* Delta in characters and bytes for try_window_id. */
11226
11227 EMACS_INT debug_delta, debug_delta_bytes;
11228
11229 /* Values of window_end_pos and window_end_vpos at the end of
11230 try_window_id. */
11231
11232 EMACS_INT debug_end_pos, debug_end_vpos;
11233
11234 /* Append a string to W->desired_matrix->method. FMT is a printf
11235 format string. A1...A9 are a supplement for a variable-length
11236 argument list. If trace_redisplay_p is non-zero also printf the
11237 resulting string to stderr. */
11238
11239 static void
11240 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11241 struct window *w;
11242 char *fmt;
11243 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11244 {
11245 char buffer[512];
11246 char *method = w->desired_matrix->method;
11247 int len = strlen (method);
11248 int size = sizeof w->desired_matrix->method;
11249 int remaining = size - len - 1;
11250
11251 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11252 if (len && remaining)
11253 {
11254 method[len] = '|';
11255 --remaining, ++len;
11256 }
11257
11258 strncpy (method + len, buffer, remaining);
11259
11260 if (trace_redisplay_p)
11261 fprintf (stderr, "%p (%s): %s\n",
11262 w,
11263 ((BUFFERP (w->buffer)
11264 && STRINGP (XBUFFER (w->buffer)->name))
11265 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11266 : "no buffer"),
11267 buffer);
11268 }
11269
11270 #endif /* GLYPH_DEBUG */
11271
11272
11273 /* Value is non-zero if all changes in window W, which displays
11274 current_buffer, are in the text between START and END. START is a
11275 buffer position, END is given as a distance from Z. Used in
11276 redisplay_internal for display optimization. */
11277
11278 static INLINE int
11279 text_outside_line_unchanged_p (struct window *w,
11280 EMACS_INT start, EMACS_INT end)
11281 {
11282 int unchanged_p = 1;
11283
11284 /* If text or overlays have changed, see where. */
11285 if (XFASTINT (w->last_modified) < MODIFF
11286 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11287 {
11288 /* Gap in the line? */
11289 if (GPT < start || Z - GPT < end)
11290 unchanged_p = 0;
11291
11292 /* Changes start in front of the line, or end after it? */
11293 if (unchanged_p
11294 && (BEG_UNCHANGED < start - 1
11295 || END_UNCHANGED < end))
11296 unchanged_p = 0;
11297
11298 /* If selective display, can't optimize if changes start at the
11299 beginning of the line. */
11300 if (unchanged_p
11301 && INTEGERP (current_buffer->selective_display)
11302 && XINT (current_buffer->selective_display) > 0
11303 && (BEG_UNCHANGED < start || GPT <= start))
11304 unchanged_p = 0;
11305
11306 /* If there are overlays at the start or end of the line, these
11307 may have overlay strings with newlines in them. A change at
11308 START, for instance, may actually concern the display of such
11309 overlay strings as well, and they are displayed on different
11310 lines. So, quickly rule out this case. (For the future, it
11311 might be desirable to implement something more telling than
11312 just BEG/END_UNCHANGED.) */
11313 if (unchanged_p)
11314 {
11315 if (BEG + BEG_UNCHANGED == start
11316 && overlay_touches_p (start))
11317 unchanged_p = 0;
11318 if (END_UNCHANGED == end
11319 && overlay_touches_p (Z - end))
11320 unchanged_p = 0;
11321 }
11322
11323 /* Under bidi reordering, adding or deleting a character in the
11324 beginning of a paragraph, before the first strong directional
11325 character, can change the base direction of the paragraph (unless
11326 the buffer specifies a fixed paragraph direction), which will
11327 require to redisplay the whole paragraph. It might be worthwhile
11328 to find the paragraph limits and widen the range of redisplayed
11329 lines to that, but for now just give up this optimization. */
11330 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11331 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11332 unchanged_p = 0;
11333 }
11334
11335 return unchanged_p;
11336 }
11337
11338
11339 /* Do a frame update, taking possible shortcuts into account. This is
11340 the main external entry point for redisplay.
11341
11342 If the last redisplay displayed an echo area message and that message
11343 is no longer requested, we clear the echo area or bring back the
11344 mini-buffer if that is in use. */
11345
11346 void
11347 redisplay (void)
11348 {
11349 redisplay_internal (0);
11350 }
11351
11352
11353 static Lisp_Object
11354 overlay_arrow_string_or_property (Lisp_Object var)
11355 {
11356 Lisp_Object val;
11357
11358 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11359 return val;
11360
11361 return Voverlay_arrow_string;
11362 }
11363
11364 /* Return 1 if there are any overlay-arrows in current_buffer. */
11365 static int
11366 overlay_arrow_in_current_buffer_p (void)
11367 {
11368 Lisp_Object vlist;
11369
11370 for (vlist = Voverlay_arrow_variable_list;
11371 CONSP (vlist);
11372 vlist = XCDR (vlist))
11373 {
11374 Lisp_Object var = XCAR (vlist);
11375 Lisp_Object val;
11376
11377 if (!SYMBOLP (var))
11378 continue;
11379 val = find_symbol_value (var);
11380 if (MARKERP (val)
11381 && current_buffer == XMARKER (val)->buffer)
11382 return 1;
11383 }
11384 return 0;
11385 }
11386
11387
11388 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11389 has changed. */
11390
11391 static int
11392 overlay_arrows_changed_p (void)
11393 {
11394 Lisp_Object vlist;
11395
11396 for (vlist = Voverlay_arrow_variable_list;
11397 CONSP (vlist);
11398 vlist = XCDR (vlist))
11399 {
11400 Lisp_Object var = XCAR (vlist);
11401 Lisp_Object val, pstr;
11402
11403 if (!SYMBOLP (var))
11404 continue;
11405 val = find_symbol_value (var);
11406 if (!MARKERP (val))
11407 continue;
11408 if (! EQ (COERCE_MARKER (val),
11409 Fget (var, Qlast_arrow_position))
11410 || ! (pstr = overlay_arrow_string_or_property (var),
11411 EQ (pstr, Fget (var, Qlast_arrow_string))))
11412 return 1;
11413 }
11414 return 0;
11415 }
11416
11417 /* Mark overlay arrows to be updated on next redisplay. */
11418
11419 static void
11420 update_overlay_arrows (int up_to_date)
11421 {
11422 Lisp_Object vlist;
11423
11424 for (vlist = Voverlay_arrow_variable_list;
11425 CONSP (vlist);
11426 vlist = XCDR (vlist))
11427 {
11428 Lisp_Object var = XCAR (vlist);
11429
11430 if (!SYMBOLP (var))
11431 continue;
11432
11433 if (up_to_date > 0)
11434 {
11435 Lisp_Object val = find_symbol_value (var);
11436 Fput (var, Qlast_arrow_position,
11437 COERCE_MARKER (val));
11438 Fput (var, Qlast_arrow_string,
11439 overlay_arrow_string_or_property (var));
11440 }
11441 else if (up_to_date < 0
11442 || !NILP (Fget (var, Qlast_arrow_position)))
11443 {
11444 Fput (var, Qlast_arrow_position, Qt);
11445 Fput (var, Qlast_arrow_string, Qt);
11446 }
11447 }
11448 }
11449
11450
11451 /* Return overlay arrow string to display at row.
11452 Return integer (bitmap number) for arrow bitmap in left fringe.
11453 Return nil if no overlay arrow. */
11454
11455 static Lisp_Object
11456 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11457 {
11458 Lisp_Object vlist;
11459
11460 for (vlist = Voverlay_arrow_variable_list;
11461 CONSP (vlist);
11462 vlist = XCDR (vlist))
11463 {
11464 Lisp_Object var = XCAR (vlist);
11465 Lisp_Object val;
11466
11467 if (!SYMBOLP (var))
11468 continue;
11469
11470 val = find_symbol_value (var);
11471
11472 if (MARKERP (val)
11473 && current_buffer == XMARKER (val)->buffer
11474 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11475 {
11476 if (FRAME_WINDOW_P (it->f)
11477 /* FIXME: if ROW->reversed_p is set, this should test
11478 the right fringe, not the left one. */
11479 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11480 {
11481 #ifdef HAVE_WINDOW_SYSTEM
11482 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11483 {
11484 int fringe_bitmap;
11485 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11486 return make_number (fringe_bitmap);
11487 }
11488 #endif
11489 return make_number (-1); /* Use default arrow bitmap */
11490 }
11491 return overlay_arrow_string_or_property (var);
11492 }
11493 }
11494
11495 return Qnil;
11496 }
11497
11498 /* Return 1 if point moved out of or into a composition. Otherwise
11499 return 0. PREV_BUF and PREV_PT are the last point buffer and
11500 position. BUF and PT are the current point buffer and position. */
11501
11502 int
11503 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11504 struct buffer *buf, EMACS_INT pt)
11505 {
11506 EMACS_INT start, end;
11507 Lisp_Object prop;
11508 Lisp_Object buffer;
11509
11510 XSETBUFFER (buffer, buf);
11511 /* Check a composition at the last point if point moved within the
11512 same buffer. */
11513 if (prev_buf == buf)
11514 {
11515 if (prev_pt == pt)
11516 /* Point didn't move. */
11517 return 0;
11518
11519 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11520 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11521 && COMPOSITION_VALID_P (start, end, prop)
11522 && start < prev_pt && end > prev_pt)
11523 /* The last point was within the composition. Return 1 iff
11524 point moved out of the composition. */
11525 return (pt <= start || pt >= end);
11526 }
11527
11528 /* Check a composition at the current point. */
11529 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11530 && find_composition (pt, -1, &start, &end, &prop, buffer)
11531 && COMPOSITION_VALID_P (start, end, prop)
11532 && start < pt && end > pt);
11533 }
11534
11535
11536 /* Reconsider the setting of B->clip_changed which is displayed
11537 in window W. */
11538
11539 static INLINE void
11540 reconsider_clip_changes (struct window *w, struct buffer *b)
11541 {
11542 if (b->clip_changed
11543 && !NILP (w->window_end_valid)
11544 && w->current_matrix->buffer == b
11545 && w->current_matrix->zv == BUF_ZV (b)
11546 && w->current_matrix->begv == BUF_BEGV (b))
11547 b->clip_changed = 0;
11548
11549 /* If display wasn't paused, and W is not a tool bar window, see if
11550 point has been moved into or out of a composition. In that case,
11551 we set b->clip_changed to 1 to force updating the screen. If
11552 b->clip_changed has already been set to 1, we can skip this
11553 check. */
11554 if (!b->clip_changed
11555 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11556 {
11557 EMACS_INT pt;
11558
11559 if (w == XWINDOW (selected_window))
11560 pt = BUF_PT (current_buffer);
11561 else
11562 pt = marker_position (w->pointm);
11563
11564 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11565 || pt != XINT (w->last_point))
11566 && check_point_in_composition (w->current_matrix->buffer,
11567 XINT (w->last_point),
11568 XBUFFER (w->buffer), pt))
11569 b->clip_changed = 1;
11570 }
11571 }
11572 \f
11573
11574 /* Select FRAME to forward the values of frame-local variables into C
11575 variables so that the redisplay routines can access those values
11576 directly. */
11577
11578 static void
11579 select_frame_for_redisplay (Lisp_Object frame)
11580 {
11581 Lisp_Object tail, tem;
11582 Lisp_Object old = selected_frame;
11583 struct Lisp_Symbol *sym;
11584
11585 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11586
11587 selected_frame = frame;
11588
11589 do {
11590 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11591 if (CONSP (XCAR (tail))
11592 && (tem = XCAR (XCAR (tail)),
11593 SYMBOLP (tem))
11594 && (sym = indirect_variable (XSYMBOL (tem)),
11595 sym->redirect == SYMBOL_LOCALIZED)
11596 && sym->val.blv->frame_local)
11597 /* Use find_symbol_value rather than Fsymbol_value
11598 to avoid an error if it is void. */
11599 find_symbol_value (tem);
11600 } while (!EQ (frame, old) && (frame = old, 1));
11601 }
11602
11603
11604 #define STOP_POLLING \
11605 do { if (! polling_stopped_here) stop_polling (); \
11606 polling_stopped_here = 1; } while (0)
11607
11608 #define RESUME_POLLING \
11609 do { if (polling_stopped_here) start_polling (); \
11610 polling_stopped_here = 0; } while (0)
11611
11612
11613 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11614 response to any user action; therefore, we should preserve the echo
11615 area. (Actually, our caller does that job.) Perhaps in the future
11616 avoid recentering windows if it is not necessary; currently that
11617 causes some problems. */
11618
11619 static void
11620 redisplay_internal (int preserve_echo_area)
11621 {
11622 struct window *w = XWINDOW (selected_window);
11623 struct frame *f;
11624 int pause;
11625 int must_finish = 0;
11626 struct text_pos tlbufpos, tlendpos;
11627 int number_of_visible_frames;
11628 int count, count1;
11629 struct frame *sf;
11630 int polling_stopped_here = 0;
11631 Lisp_Object old_frame = selected_frame;
11632
11633 /* Non-zero means redisplay has to consider all windows on all
11634 frames. Zero means, only selected_window is considered. */
11635 int consider_all_windows_p;
11636
11637 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11638
11639 /* No redisplay if running in batch mode or frame is not yet fully
11640 initialized, or redisplay is explicitly turned off by setting
11641 Vinhibit_redisplay. */
11642 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11643 || !NILP (Vinhibit_redisplay))
11644 return;
11645
11646 /* Don't examine these until after testing Vinhibit_redisplay.
11647 When Emacs is shutting down, perhaps because its connection to
11648 X has dropped, we should not look at them at all. */
11649 f = XFRAME (w->frame);
11650 sf = SELECTED_FRAME ();
11651
11652 if (!f->glyphs_initialized_p)
11653 return;
11654
11655 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11656 if (popup_activated ())
11657 return;
11658 #endif
11659
11660 /* I don't think this happens but let's be paranoid. */
11661 if (redisplaying_p)
11662 return;
11663
11664 /* Record a function that resets redisplaying_p to its old value
11665 when we leave this function. */
11666 count = SPECPDL_INDEX ();
11667 record_unwind_protect (unwind_redisplay,
11668 Fcons (make_number (redisplaying_p), selected_frame));
11669 ++redisplaying_p;
11670 specbind (Qinhibit_free_realized_faces, Qnil);
11671
11672 {
11673 Lisp_Object tail, frame;
11674
11675 FOR_EACH_FRAME (tail, frame)
11676 {
11677 struct frame *f = XFRAME (frame);
11678 f->already_hscrolled_p = 0;
11679 }
11680 }
11681
11682 retry:
11683 if (!EQ (old_frame, selected_frame)
11684 && FRAME_LIVE_P (XFRAME (old_frame)))
11685 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11686 selected_frame and selected_window to be temporarily out-of-sync so
11687 when we come back here via `goto retry', we need to resync because we
11688 may need to run Elisp code (via prepare_menu_bars). */
11689 select_frame_for_redisplay (old_frame);
11690
11691 pause = 0;
11692 reconsider_clip_changes (w, current_buffer);
11693 last_escape_glyph_frame = NULL;
11694 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11695 last_glyphless_glyph_frame = NULL;
11696 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11697
11698 /* If new fonts have been loaded that make a glyph matrix adjustment
11699 necessary, do it. */
11700 if (fonts_changed_p)
11701 {
11702 adjust_glyphs (NULL);
11703 ++windows_or_buffers_changed;
11704 fonts_changed_p = 0;
11705 }
11706
11707 /* If face_change_count is non-zero, init_iterator will free all
11708 realized faces, which includes the faces referenced from current
11709 matrices. So, we can't reuse current matrices in this case. */
11710 if (face_change_count)
11711 ++windows_or_buffers_changed;
11712
11713 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11714 && FRAME_TTY (sf)->previous_frame != sf)
11715 {
11716 /* Since frames on a single ASCII terminal share the same
11717 display area, displaying a different frame means redisplay
11718 the whole thing. */
11719 windows_or_buffers_changed++;
11720 SET_FRAME_GARBAGED (sf);
11721 #ifndef DOS_NT
11722 set_tty_color_mode (FRAME_TTY (sf), sf);
11723 #endif
11724 FRAME_TTY (sf)->previous_frame = sf;
11725 }
11726
11727 /* Set the visible flags for all frames. Do this before checking
11728 for resized or garbaged frames; they want to know if their frames
11729 are visible. See the comment in frame.h for
11730 FRAME_SAMPLE_VISIBILITY. */
11731 {
11732 Lisp_Object tail, frame;
11733
11734 number_of_visible_frames = 0;
11735
11736 FOR_EACH_FRAME (tail, frame)
11737 {
11738 struct frame *f = XFRAME (frame);
11739
11740 FRAME_SAMPLE_VISIBILITY (f);
11741 if (FRAME_VISIBLE_P (f))
11742 ++number_of_visible_frames;
11743 clear_desired_matrices (f);
11744 }
11745 }
11746
11747 /* Notice any pending interrupt request to change frame size. */
11748 do_pending_window_change (1);
11749
11750 /* Clear frames marked as garbaged. */
11751 if (frame_garbaged)
11752 clear_garbaged_frames ();
11753
11754 /* Build menubar and tool-bar items. */
11755 if (NILP (Vmemory_full))
11756 prepare_menu_bars ();
11757
11758 if (windows_or_buffers_changed)
11759 update_mode_lines++;
11760
11761 /* Detect case that we need to write or remove a star in the mode line. */
11762 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11763 {
11764 w->update_mode_line = Qt;
11765 if (buffer_shared > 1)
11766 update_mode_lines++;
11767 }
11768
11769 /* Avoid invocation of point motion hooks by `current_column' below. */
11770 count1 = SPECPDL_INDEX ();
11771 specbind (Qinhibit_point_motion_hooks, Qt);
11772
11773 /* If %c is in the mode line, update it if needed. */
11774 if (!NILP (w->column_number_displayed)
11775 /* This alternative quickly identifies a common case
11776 where no change is needed. */
11777 && !(PT == XFASTINT (w->last_point)
11778 && XFASTINT (w->last_modified) >= MODIFF
11779 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11780 && (XFASTINT (w->column_number_displayed)
11781 != (int) current_column ())) /* iftc */
11782 w->update_mode_line = Qt;
11783
11784 unbind_to (count1, Qnil);
11785
11786 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11787
11788 /* The variable buffer_shared is set in redisplay_window and
11789 indicates that we redisplay a buffer in different windows. See
11790 there. */
11791 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11792 || cursor_type_changed);
11793
11794 /* If specs for an arrow have changed, do thorough redisplay
11795 to ensure we remove any arrow that should no longer exist. */
11796 if (overlay_arrows_changed_p ())
11797 consider_all_windows_p = windows_or_buffers_changed = 1;
11798
11799 /* Normally the message* functions will have already displayed and
11800 updated the echo area, but the frame may have been trashed, or
11801 the update may have been preempted, so display the echo area
11802 again here. Checking message_cleared_p captures the case that
11803 the echo area should be cleared. */
11804 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11805 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11806 || (message_cleared_p
11807 && minibuf_level == 0
11808 /* If the mini-window is currently selected, this means the
11809 echo-area doesn't show through. */
11810 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11811 {
11812 int window_height_changed_p = echo_area_display (0);
11813 must_finish = 1;
11814
11815 /* If we don't display the current message, don't clear the
11816 message_cleared_p flag, because, if we did, we wouldn't clear
11817 the echo area in the next redisplay which doesn't preserve
11818 the echo area. */
11819 if (!display_last_displayed_message_p)
11820 message_cleared_p = 0;
11821
11822 if (fonts_changed_p)
11823 goto retry;
11824 else if (window_height_changed_p)
11825 {
11826 consider_all_windows_p = 1;
11827 ++update_mode_lines;
11828 ++windows_or_buffers_changed;
11829
11830 /* If window configuration was changed, frames may have been
11831 marked garbaged. Clear them or we will experience
11832 surprises wrt scrolling. */
11833 if (frame_garbaged)
11834 clear_garbaged_frames ();
11835 }
11836 }
11837 else if (EQ (selected_window, minibuf_window)
11838 && (current_buffer->clip_changed
11839 || XFASTINT (w->last_modified) < MODIFF
11840 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11841 && resize_mini_window (w, 0))
11842 {
11843 /* Resized active mini-window to fit the size of what it is
11844 showing if its contents might have changed. */
11845 must_finish = 1;
11846 /* FIXME: this causes all frames to be updated, which seems unnecessary
11847 since only the current frame needs to be considered. This function needs
11848 to be rewritten with two variables, consider_all_windows and
11849 consider_all_frames. */
11850 consider_all_windows_p = 1;
11851 ++windows_or_buffers_changed;
11852 ++update_mode_lines;
11853
11854 /* If window configuration was changed, frames may have been
11855 marked garbaged. Clear them or we will experience
11856 surprises wrt scrolling. */
11857 if (frame_garbaged)
11858 clear_garbaged_frames ();
11859 }
11860
11861
11862 /* If showing the region, and mark has changed, we must redisplay
11863 the whole window. The assignment to this_line_start_pos prevents
11864 the optimization directly below this if-statement. */
11865 if (((!NILP (Vtransient_mark_mode)
11866 && !NILP (XBUFFER (w->buffer)->mark_active))
11867 != !NILP (w->region_showing))
11868 || (!NILP (w->region_showing)
11869 && !EQ (w->region_showing,
11870 Fmarker_position (XBUFFER (w->buffer)->mark))))
11871 CHARPOS (this_line_start_pos) = 0;
11872
11873 /* Optimize the case that only the line containing the cursor in the
11874 selected window has changed. Variables starting with this_ are
11875 set in display_line and record information about the line
11876 containing the cursor. */
11877 tlbufpos = this_line_start_pos;
11878 tlendpos = this_line_end_pos;
11879 if (!consider_all_windows_p
11880 && CHARPOS (tlbufpos) > 0
11881 && NILP (w->update_mode_line)
11882 && !current_buffer->clip_changed
11883 && !current_buffer->prevent_redisplay_optimizations_p
11884 && FRAME_VISIBLE_P (XFRAME (w->frame))
11885 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11886 /* Make sure recorded data applies to current buffer, etc. */
11887 && this_line_buffer == current_buffer
11888 && current_buffer == XBUFFER (w->buffer)
11889 && NILP (w->force_start)
11890 && NILP (w->optional_new_start)
11891 /* Point must be on the line that we have info recorded about. */
11892 && PT >= CHARPOS (tlbufpos)
11893 && PT <= Z - CHARPOS (tlendpos)
11894 /* All text outside that line, including its final newline,
11895 must be unchanged. */
11896 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11897 CHARPOS (tlendpos)))
11898 {
11899 if (CHARPOS (tlbufpos) > BEGV
11900 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11901 && (CHARPOS (tlbufpos) == ZV
11902 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11903 /* Former continuation line has disappeared by becoming empty. */
11904 goto cancel;
11905 else if (XFASTINT (w->last_modified) < MODIFF
11906 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11907 || MINI_WINDOW_P (w))
11908 {
11909 /* We have to handle the case of continuation around a
11910 wide-column character (see the comment in indent.c around
11911 line 1340).
11912
11913 For instance, in the following case:
11914
11915 -------- Insert --------
11916 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11917 J_I_ ==> J_I_ `^^' are cursors.
11918 ^^ ^^
11919 -------- --------
11920
11921 As we have to redraw the line above, we cannot use this
11922 optimization. */
11923
11924 struct it it;
11925 int line_height_before = this_line_pixel_height;
11926
11927 /* Note that start_display will handle the case that the
11928 line starting at tlbufpos is a continuation line. */
11929 start_display (&it, w, tlbufpos);
11930
11931 /* Implementation note: It this still necessary? */
11932 if (it.current_x != this_line_start_x)
11933 goto cancel;
11934
11935 TRACE ((stderr, "trying display optimization 1\n"));
11936 w->cursor.vpos = -1;
11937 overlay_arrow_seen = 0;
11938 it.vpos = this_line_vpos;
11939 it.current_y = this_line_y;
11940 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11941 display_line (&it);
11942
11943 /* If line contains point, is not continued,
11944 and ends at same distance from eob as before, we win. */
11945 if (w->cursor.vpos >= 0
11946 /* Line is not continued, otherwise this_line_start_pos
11947 would have been set to 0 in display_line. */
11948 && CHARPOS (this_line_start_pos)
11949 /* Line ends as before. */
11950 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11951 /* Line has same height as before. Otherwise other lines
11952 would have to be shifted up or down. */
11953 && this_line_pixel_height == line_height_before)
11954 {
11955 /* If this is not the window's last line, we must adjust
11956 the charstarts of the lines below. */
11957 if (it.current_y < it.last_visible_y)
11958 {
11959 struct glyph_row *row
11960 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11961 EMACS_INT delta, delta_bytes;
11962
11963 /* We used to distinguish between two cases here,
11964 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11965 when the line ends in a newline or the end of the
11966 buffer's accessible portion. But both cases did
11967 the same, so they were collapsed. */
11968 delta = (Z
11969 - CHARPOS (tlendpos)
11970 - MATRIX_ROW_START_CHARPOS (row));
11971 delta_bytes = (Z_BYTE
11972 - BYTEPOS (tlendpos)
11973 - MATRIX_ROW_START_BYTEPOS (row));
11974
11975 increment_matrix_positions (w->current_matrix,
11976 this_line_vpos + 1,
11977 w->current_matrix->nrows,
11978 delta, delta_bytes);
11979 }
11980
11981 /* If this row displays text now but previously didn't,
11982 or vice versa, w->window_end_vpos may have to be
11983 adjusted. */
11984 if ((it.glyph_row - 1)->displays_text_p)
11985 {
11986 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11987 XSETINT (w->window_end_vpos, this_line_vpos);
11988 }
11989 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11990 && this_line_vpos > 0)
11991 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11992 w->window_end_valid = Qnil;
11993
11994 /* Update hint: No need to try to scroll in update_window. */
11995 w->desired_matrix->no_scrolling_p = 1;
11996
11997 #if GLYPH_DEBUG
11998 *w->desired_matrix->method = 0;
11999 debug_method_add (w, "optimization 1");
12000 #endif
12001 #ifdef HAVE_WINDOW_SYSTEM
12002 update_window_fringes (w, 0);
12003 #endif
12004 goto update;
12005 }
12006 else
12007 goto cancel;
12008 }
12009 else if (/* Cursor position hasn't changed. */
12010 PT == XFASTINT (w->last_point)
12011 /* Make sure the cursor was last displayed
12012 in this window. Otherwise we have to reposition it. */
12013 && 0 <= w->cursor.vpos
12014 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12015 {
12016 if (!must_finish)
12017 {
12018 do_pending_window_change (1);
12019
12020 /* We used to always goto end_of_redisplay here, but this
12021 isn't enough if we have a blinking cursor. */
12022 if (w->cursor_off_p == w->last_cursor_off_p)
12023 goto end_of_redisplay;
12024 }
12025 goto update;
12026 }
12027 /* If highlighting the region, or if the cursor is in the echo area,
12028 then we can't just move the cursor. */
12029 else if (! (!NILP (Vtransient_mark_mode)
12030 && !NILP (current_buffer->mark_active))
12031 && (EQ (selected_window, current_buffer->last_selected_window)
12032 || highlight_nonselected_windows)
12033 && NILP (w->region_showing)
12034 && NILP (Vshow_trailing_whitespace)
12035 && !cursor_in_echo_area)
12036 {
12037 struct it it;
12038 struct glyph_row *row;
12039
12040 /* Skip from tlbufpos to PT and see where it is. Note that
12041 PT may be in invisible text. If so, we will end at the
12042 next visible position. */
12043 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12044 NULL, DEFAULT_FACE_ID);
12045 it.current_x = this_line_start_x;
12046 it.current_y = this_line_y;
12047 it.vpos = this_line_vpos;
12048
12049 /* The call to move_it_to stops in front of PT, but
12050 moves over before-strings. */
12051 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12052
12053 if (it.vpos == this_line_vpos
12054 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12055 row->enabled_p))
12056 {
12057 xassert (this_line_vpos == it.vpos);
12058 xassert (this_line_y == it.current_y);
12059 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12060 #if GLYPH_DEBUG
12061 *w->desired_matrix->method = 0;
12062 debug_method_add (w, "optimization 3");
12063 #endif
12064 goto update;
12065 }
12066 else
12067 goto cancel;
12068 }
12069
12070 cancel:
12071 /* Text changed drastically or point moved off of line. */
12072 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12073 }
12074
12075 CHARPOS (this_line_start_pos) = 0;
12076 consider_all_windows_p |= buffer_shared > 1;
12077 ++clear_face_cache_count;
12078 #ifdef HAVE_WINDOW_SYSTEM
12079 ++clear_image_cache_count;
12080 #endif
12081
12082 /* Build desired matrices, and update the display. If
12083 consider_all_windows_p is non-zero, do it for all windows on all
12084 frames. Otherwise do it for selected_window, only. */
12085
12086 if (consider_all_windows_p)
12087 {
12088 Lisp_Object tail, frame;
12089
12090 FOR_EACH_FRAME (tail, frame)
12091 XFRAME (frame)->updated_p = 0;
12092
12093 /* Recompute # windows showing selected buffer. This will be
12094 incremented each time such a window is displayed. */
12095 buffer_shared = 0;
12096
12097 FOR_EACH_FRAME (tail, frame)
12098 {
12099 struct frame *f = XFRAME (frame);
12100
12101 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12102 {
12103 if (! EQ (frame, selected_frame))
12104 /* Select the frame, for the sake of frame-local
12105 variables. */
12106 select_frame_for_redisplay (frame);
12107
12108 /* Mark all the scroll bars to be removed; we'll redeem
12109 the ones we want when we redisplay their windows. */
12110 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12111 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12112
12113 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12114 redisplay_windows (FRAME_ROOT_WINDOW (f));
12115
12116 /* The X error handler may have deleted that frame. */
12117 if (!FRAME_LIVE_P (f))
12118 continue;
12119
12120 /* Any scroll bars which redisplay_windows should have
12121 nuked should now go away. */
12122 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12123 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12124
12125 /* If fonts changed, display again. */
12126 /* ??? rms: I suspect it is a mistake to jump all the way
12127 back to retry here. It should just retry this frame. */
12128 if (fonts_changed_p)
12129 goto retry;
12130
12131 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12132 {
12133 /* See if we have to hscroll. */
12134 if (!f->already_hscrolled_p)
12135 {
12136 f->already_hscrolled_p = 1;
12137 if (hscroll_windows (f->root_window))
12138 goto retry;
12139 }
12140
12141 /* Prevent various kinds of signals during display
12142 update. stdio is not robust about handling
12143 signals, which can cause an apparent I/O
12144 error. */
12145 if (interrupt_input)
12146 unrequest_sigio ();
12147 STOP_POLLING;
12148
12149 /* Update the display. */
12150 set_window_update_flags (XWINDOW (f->root_window), 1);
12151 pause |= update_frame (f, 0, 0);
12152 f->updated_p = 1;
12153 }
12154 }
12155 }
12156
12157 if (!EQ (old_frame, selected_frame)
12158 && FRAME_LIVE_P (XFRAME (old_frame)))
12159 /* We played a bit fast-and-loose above and allowed selected_frame
12160 and selected_window to be temporarily out-of-sync but let's make
12161 sure this stays contained. */
12162 select_frame_for_redisplay (old_frame);
12163 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12164
12165 if (!pause)
12166 {
12167 /* Do the mark_window_display_accurate after all windows have
12168 been redisplayed because this call resets flags in buffers
12169 which are needed for proper redisplay. */
12170 FOR_EACH_FRAME (tail, frame)
12171 {
12172 struct frame *f = XFRAME (frame);
12173 if (f->updated_p)
12174 {
12175 mark_window_display_accurate (f->root_window, 1);
12176 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12177 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12178 }
12179 }
12180 }
12181 }
12182 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12183 {
12184 Lisp_Object mini_window;
12185 struct frame *mini_frame;
12186
12187 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12188 /* Use list_of_error, not Qerror, so that
12189 we catch only errors and don't run the debugger. */
12190 internal_condition_case_1 (redisplay_window_1, selected_window,
12191 list_of_error,
12192 redisplay_window_error);
12193
12194 /* Compare desired and current matrices, perform output. */
12195
12196 update:
12197 /* If fonts changed, display again. */
12198 if (fonts_changed_p)
12199 goto retry;
12200
12201 /* Prevent various kinds of signals during display update.
12202 stdio is not robust about handling signals,
12203 which can cause an apparent I/O error. */
12204 if (interrupt_input)
12205 unrequest_sigio ();
12206 STOP_POLLING;
12207
12208 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12209 {
12210 if (hscroll_windows (selected_window))
12211 goto retry;
12212
12213 XWINDOW (selected_window)->must_be_updated_p = 1;
12214 pause = update_frame (sf, 0, 0);
12215 }
12216
12217 /* We may have called echo_area_display at the top of this
12218 function. If the echo area is on another frame, that may
12219 have put text on a frame other than the selected one, so the
12220 above call to update_frame would not have caught it. Catch
12221 it here. */
12222 mini_window = FRAME_MINIBUF_WINDOW (sf);
12223 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12224
12225 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12226 {
12227 XWINDOW (mini_window)->must_be_updated_p = 1;
12228 pause |= update_frame (mini_frame, 0, 0);
12229 if (!pause && hscroll_windows (mini_window))
12230 goto retry;
12231 }
12232 }
12233
12234 /* If display was paused because of pending input, make sure we do a
12235 thorough update the next time. */
12236 if (pause)
12237 {
12238 /* Prevent the optimization at the beginning of
12239 redisplay_internal that tries a single-line update of the
12240 line containing the cursor in the selected window. */
12241 CHARPOS (this_line_start_pos) = 0;
12242
12243 /* Let the overlay arrow be updated the next time. */
12244 update_overlay_arrows (0);
12245
12246 /* If we pause after scrolling, some rows in the current
12247 matrices of some windows are not valid. */
12248 if (!WINDOW_FULL_WIDTH_P (w)
12249 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12250 update_mode_lines = 1;
12251 }
12252 else
12253 {
12254 if (!consider_all_windows_p)
12255 {
12256 /* This has already been done above if
12257 consider_all_windows_p is set. */
12258 mark_window_display_accurate_1 (w, 1);
12259
12260 /* Say overlay arrows are up to date. */
12261 update_overlay_arrows (1);
12262
12263 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12264 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12265 }
12266
12267 update_mode_lines = 0;
12268 windows_or_buffers_changed = 0;
12269 cursor_type_changed = 0;
12270 }
12271
12272 /* Start SIGIO interrupts coming again. Having them off during the
12273 code above makes it less likely one will discard output, but not
12274 impossible, since there might be stuff in the system buffer here.
12275 But it is much hairier to try to do anything about that. */
12276 if (interrupt_input)
12277 request_sigio ();
12278 RESUME_POLLING;
12279
12280 /* If a frame has become visible which was not before, redisplay
12281 again, so that we display it. Expose events for such a frame
12282 (which it gets when becoming visible) don't call the parts of
12283 redisplay constructing glyphs, so simply exposing a frame won't
12284 display anything in this case. So, we have to display these
12285 frames here explicitly. */
12286 if (!pause)
12287 {
12288 Lisp_Object tail, frame;
12289 int new_count = 0;
12290
12291 FOR_EACH_FRAME (tail, frame)
12292 {
12293 int this_is_visible = 0;
12294
12295 if (XFRAME (frame)->visible)
12296 this_is_visible = 1;
12297 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12298 if (XFRAME (frame)->visible)
12299 this_is_visible = 1;
12300
12301 if (this_is_visible)
12302 new_count++;
12303 }
12304
12305 if (new_count != number_of_visible_frames)
12306 windows_or_buffers_changed++;
12307 }
12308
12309 /* Change frame size now if a change is pending. */
12310 do_pending_window_change (1);
12311
12312 /* If we just did a pending size change, or have additional
12313 visible frames, redisplay again. */
12314 if (windows_or_buffers_changed && !pause)
12315 goto retry;
12316
12317 /* Clear the face and image caches.
12318
12319 We used to do this only if consider_all_windows_p. But the cache
12320 needs to be cleared if a timer creates images in the current
12321 buffer (e.g. the test case in Bug#6230). */
12322
12323 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12324 {
12325 clear_face_cache (0);
12326 clear_face_cache_count = 0;
12327 }
12328
12329 #ifdef HAVE_WINDOW_SYSTEM
12330 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12331 {
12332 clear_image_caches (Qnil);
12333 clear_image_cache_count = 0;
12334 }
12335 #endif /* HAVE_WINDOW_SYSTEM */
12336
12337 end_of_redisplay:
12338 unbind_to (count, Qnil);
12339 RESUME_POLLING;
12340 }
12341
12342
12343 /* Redisplay, but leave alone any recent echo area message unless
12344 another message has been requested in its place.
12345
12346 This is useful in situations where you need to redisplay but no
12347 user action has occurred, making it inappropriate for the message
12348 area to be cleared. See tracking_off and
12349 wait_reading_process_output for examples of these situations.
12350
12351 FROM_WHERE is an integer saying from where this function was
12352 called. This is useful for debugging. */
12353
12354 void
12355 redisplay_preserve_echo_area (int from_where)
12356 {
12357 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12358
12359 if (!NILP (echo_area_buffer[1]))
12360 {
12361 /* We have a previously displayed message, but no current
12362 message. Redisplay the previous message. */
12363 display_last_displayed_message_p = 1;
12364 redisplay_internal (1);
12365 display_last_displayed_message_p = 0;
12366 }
12367 else
12368 redisplay_internal (1);
12369
12370 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12371 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12372 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12373 }
12374
12375
12376 /* Function registered with record_unwind_protect in
12377 redisplay_internal. Reset redisplaying_p to the value it had
12378 before redisplay_internal was called, and clear
12379 prevent_freeing_realized_faces_p. It also selects the previously
12380 selected frame, unless it has been deleted (by an X connection
12381 failure during redisplay, for example). */
12382
12383 static Lisp_Object
12384 unwind_redisplay (Lisp_Object val)
12385 {
12386 Lisp_Object old_redisplaying_p, old_frame;
12387
12388 old_redisplaying_p = XCAR (val);
12389 redisplaying_p = XFASTINT (old_redisplaying_p);
12390 old_frame = XCDR (val);
12391 if (! EQ (old_frame, selected_frame)
12392 && FRAME_LIVE_P (XFRAME (old_frame)))
12393 select_frame_for_redisplay (old_frame);
12394 return Qnil;
12395 }
12396
12397
12398 /* Mark the display of window W as accurate or inaccurate. If
12399 ACCURATE_P is non-zero mark display of W as accurate. If
12400 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12401 redisplay_internal is called. */
12402
12403 static void
12404 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12405 {
12406 if (BUFFERP (w->buffer))
12407 {
12408 struct buffer *b = XBUFFER (w->buffer);
12409
12410 w->last_modified
12411 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12412 w->last_overlay_modified
12413 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12414 w->last_had_star
12415 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12416
12417 if (accurate_p)
12418 {
12419 b->clip_changed = 0;
12420 b->prevent_redisplay_optimizations_p = 0;
12421
12422 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12423 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12424 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12425 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12426
12427 w->current_matrix->buffer = b;
12428 w->current_matrix->begv = BUF_BEGV (b);
12429 w->current_matrix->zv = BUF_ZV (b);
12430
12431 w->last_cursor = w->cursor;
12432 w->last_cursor_off_p = w->cursor_off_p;
12433
12434 if (w == XWINDOW (selected_window))
12435 w->last_point = make_number (BUF_PT (b));
12436 else
12437 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12438 }
12439 }
12440
12441 if (accurate_p)
12442 {
12443 w->window_end_valid = w->buffer;
12444 w->update_mode_line = Qnil;
12445 }
12446 }
12447
12448
12449 /* Mark the display of windows in the window tree rooted at WINDOW as
12450 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12451 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12452 be redisplayed the next time redisplay_internal is called. */
12453
12454 void
12455 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12456 {
12457 struct window *w;
12458
12459 for (; !NILP (window); window = w->next)
12460 {
12461 w = XWINDOW (window);
12462 mark_window_display_accurate_1 (w, accurate_p);
12463
12464 if (!NILP (w->vchild))
12465 mark_window_display_accurate (w->vchild, accurate_p);
12466 if (!NILP (w->hchild))
12467 mark_window_display_accurate (w->hchild, accurate_p);
12468 }
12469
12470 if (accurate_p)
12471 {
12472 update_overlay_arrows (1);
12473 }
12474 else
12475 {
12476 /* Force a thorough redisplay the next time by setting
12477 last_arrow_position and last_arrow_string to t, which is
12478 unequal to any useful value of Voverlay_arrow_... */
12479 update_overlay_arrows (-1);
12480 }
12481 }
12482
12483
12484 /* Return value in display table DP (Lisp_Char_Table *) for character
12485 C. Since a display table doesn't have any parent, we don't have to
12486 follow parent. Do not call this function directly but use the
12487 macro DISP_CHAR_VECTOR. */
12488
12489 Lisp_Object
12490 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12491 {
12492 Lisp_Object val;
12493
12494 if (ASCII_CHAR_P (c))
12495 {
12496 val = dp->ascii;
12497 if (SUB_CHAR_TABLE_P (val))
12498 val = XSUB_CHAR_TABLE (val)->contents[c];
12499 }
12500 else
12501 {
12502 Lisp_Object table;
12503
12504 XSETCHAR_TABLE (table, dp);
12505 val = char_table_ref (table, c);
12506 }
12507 if (NILP (val))
12508 val = dp->defalt;
12509 return val;
12510 }
12511
12512
12513 \f
12514 /***********************************************************************
12515 Window Redisplay
12516 ***********************************************************************/
12517
12518 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12519
12520 static void
12521 redisplay_windows (Lisp_Object window)
12522 {
12523 while (!NILP (window))
12524 {
12525 struct window *w = XWINDOW (window);
12526
12527 if (!NILP (w->hchild))
12528 redisplay_windows (w->hchild);
12529 else if (!NILP (w->vchild))
12530 redisplay_windows (w->vchild);
12531 else if (!NILP (w->buffer))
12532 {
12533 displayed_buffer = XBUFFER (w->buffer);
12534 /* Use list_of_error, not Qerror, so that
12535 we catch only errors and don't run the debugger. */
12536 internal_condition_case_1 (redisplay_window_0, window,
12537 list_of_error,
12538 redisplay_window_error);
12539 }
12540
12541 window = w->next;
12542 }
12543 }
12544
12545 static Lisp_Object
12546 redisplay_window_error (Lisp_Object ignore)
12547 {
12548 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12549 return Qnil;
12550 }
12551
12552 static Lisp_Object
12553 redisplay_window_0 (Lisp_Object window)
12554 {
12555 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12556 redisplay_window (window, 0);
12557 return Qnil;
12558 }
12559
12560 static Lisp_Object
12561 redisplay_window_1 (Lisp_Object window)
12562 {
12563 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12564 redisplay_window (window, 1);
12565 return Qnil;
12566 }
12567 \f
12568
12569 /* Increment GLYPH until it reaches END or CONDITION fails while
12570 adding (GLYPH)->pixel_width to X. */
12571
12572 #define SKIP_GLYPHS(glyph, end, x, condition) \
12573 do \
12574 { \
12575 (x) += (glyph)->pixel_width; \
12576 ++(glyph); \
12577 } \
12578 while ((glyph) < (end) && (condition))
12579
12580
12581 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12582 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12583 which positions recorded in ROW differ from current buffer
12584 positions.
12585
12586 Return 0 if cursor is not on this row, 1 otherwise. */
12587
12588 int
12589 set_cursor_from_row (struct window *w, struct glyph_row *row,
12590 struct glyph_matrix *matrix,
12591 EMACS_INT delta, EMACS_INT delta_bytes,
12592 int dy, int dvpos)
12593 {
12594 struct glyph *glyph = row->glyphs[TEXT_AREA];
12595 struct glyph *end = glyph + row->used[TEXT_AREA];
12596 struct glyph *cursor = NULL;
12597 /* The last known character position in row. */
12598 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12599 int x = row->x;
12600 EMACS_INT pt_old = PT - delta;
12601 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12602 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12603 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12604 /* A glyph beyond the edge of TEXT_AREA which we should never
12605 touch. */
12606 struct glyph *glyphs_end = end;
12607 /* Non-zero means we've found a match for cursor position, but that
12608 glyph has the avoid_cursor_p flag set. */
12609 int match_with_avoid_cursor = 0;
12610 /* Non-zero means we've seen at least one glyph that came from a
12611 display string. */
12612 int string_seen = 0;
12613 /* Largest and smalles buffer positions seen so far during scan of
12614 glyph row. */
12615 EMACS_INT bpos_max = pos_before;
12616 EMACS_INT bpos_min = pos_after;
12617 /* Last buffer position covered by an overlay string with an integer
12618 `cursor' property. */
12619 EMACS_INT bpos_covered = 0;
12620
12621 /* Skip over glyphs not having an object at the start and the end of
12622 the row. These are special glyphs like truncation marks on
12623 terminal frames. */
12624 if (row->displays_text_p)
12625 {
12626 if (!row->reversed_p)
12627 {
12628 while (glyph < end
12629 && INTEGERP (glyph->object)
12630 && glyph->charpos < 0)
12631 {
12632 x += glyph->pixel_width;
12633 ++glyph;
12634 }
12635 while (end > glyph
12636 && INTEGERP ((end - 1)->object)
12637 /* CHARPOS is zero for blanks and stretch glyphs
12638 inserted by extend_face_to_end_of_line. */
12639 && (end - 1)->charpos <= 0)
12640 --end;
12641 glyph_before = glyph - 1;
12642 glyph_after = end;
12643 }
12644 else
12645 {
12646 struct glyph *g;
12647
12648 /* If the glyph row is reversed, we need to process it from back
12649 to front, so swap the edge pointers. */
12650 glyphs_end = end = glyph - 1;
12651 glyph += row->used[TEXT_AREA] - 1;
12652
12653 while (glyph > end + 1
12654 && INTEGERP (glyph->object)
12655 && glyph->charpos < 0)
12656 {
12657 --glyph;
12658 x -= glyph->pixel_width;
12659 }
12660 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12661 --glyph;
12662 /* By default, in reversed rows we put the cursor on the
12663 rightmost (first in the reading order) glyph. */
12664 for (g = end + 1; g < glyph; g++)
12665 x += g->pixel_width;
12666 while (end < glyph
12667 && INTEGERP ((end + 1)->object)
12668 && (end + 1)->charpos <= 0)
12669 ++end;
12670 glyph_before = glyph + 1;
12671 glyph_after = end;
12672 }
12673 }
12674 else if (row->reversed_p)
12675 {
12676 /* In R2L rows that don't display text, put the cursor on the
12677 rightmost glyph. Case in point: an empty last line that is
12678 part of an R2L paragraph. */
12679 cursor = end - 1;
12680 /* Avoid placing the cursor on the last glyph of the row, where
12681 on terminal frames we hold the vertical border between
12682 adjacent windows. */
12683 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12684 && !WINDOW_RIGHTMOST_P (w)
12685 && cursor == row->glyphs[LAST_AREA] - 1)
12686 cursor--;
12687 x = -1; /* will be computed below, at label compute_x */
12688 }
12689
12690 /* Step 1: Try to find the glyph whose character position
12691 corresponds to point. If that's not possible, find 2 glyphs
12692 whose character positions are the closest to point, one before
12693 point, the other after it. */
12694 if (!row->reversed_p)
12695 while (/* not marched to end of glyph row */
12696 glyph < end
12697 /* glyph was not inserted by redisplay for internal purposes */
12698 && !INTEGERP (glyph->object))
12699 {
12700 if (BUFFERP (glyph->object))
12701 {
12702 EMACS_INT dpos = glyph->charpos - pt_old;
12703
12704 if (glyph->charpos > bpos_max)
12705 bpos_max = glyph->charpos;
12706 if (glyph->charpos < bpos_min)
12707 bpos_min = glyph->charpos;
12708 if (!glyph->avoid_cursor_p)
12709 {
12710 /* If we hit point, we've found the glyph on which to
12711 display the cursor. */
12712 if (dpos == 0)
12713 {
12714 match_with_avoid_cursor = 0;
12715 break;
12716 }
12717 /* See if we've found a better approximation to
12718 POS_BEFORE or to POS_AFTER. Note that we want the
12719 first (leftmost) glyph of all those that are the
12720 closest from below, and the last (rightmost) of all
12721 those from above. */
12722 if (0 > dpos && dpos > pos_before - pt_old)
12723 {
12724 pos_before = glyph->charpos;
12725 glyph_before = glyph;
12726 }
12727 else if (0 < dpos && dpos <= pos_after - pt_old)
12728 {
12729 pos_after = glyph->charpos;
12730 glyph_after = glyph;
12731 }
12732 }
12733 else if (dpos == 0)
12734 match_with_avoid_cursor = 1;
12735 }
12736 else if (STRINGP (glyph->object))
12737 {
12738 Lisp_Object chprop;
12739 EMACS_INT glyph_pos = glyph->charpos;
12740
12741 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12742 glyph->object);
12743 if (INTEGERP (chprop))
12744 {
12745 bpos_covered = bpos_max + XINT (chprop);
12746 /* If the `cursor' property covers buffer positions up
12747 to and including point, we should display cursor on
12748 this glyph. Note that overlays and text properties
12749 with string values stop bidi reordering, so every
12750 buffer position to the left of the string is always
12751 smaller than any position to the right of the
12752 string. Therefore, if a `cursor' property on one
12753 of the string's characters has an integer value, we
12754 will break out of the loop below _before_ we get to
12755 the position match above. IOW, integer values of
12756 the `cursor' property override the "exact match for
12757 point" strategy of positioning the cursor. */
12758 /* Implementation note: bpos_max == pt_old when, e.g.,
12759 we are in an empty line, where bpos_max is set to
12760 MATRIX_ROW_START_CHARPOS, see above. */
12761 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12762 {
12763 cursor = glyph;
12764 break;
12765 }
12766 }
12767
12768 string_seen = 1;
12769 }
12770 x += glyph->pixel_width;
12771 ++glyph;
12772 }
12773 else if (glyph > end) /* row is reversed */
12774 while (!INTEGERP (glyph->object))
12775 {
12776 if (BUFFERP (glyph->object))
12777 {
12778 EMACS_INT dpos = glyph->charpos - pt_old;
12779
12780 if (glyph->charpos > bpos_max)
12781 bpos_max = glyph->charpos;
12782 if (glyph->charpos < bpos_min)
12783 bpos_min = glyph->charpos;
12784 if (!glyph->avoid_cursor_p)
12785 {
12786 if (dpos == 0)
12787 {
12788 match_with_avoid_cursor = 0;
12789 break;
12790 }
12791 if (0 > dpos && dpos > pos_before - pt_old)
12792 {
12793 pos_before = glyph->charpos;
12794 glyph_before = glyph;
12795 }
12796 else if (0 < dpos && dpos <= pos_after - pt_old)
12797 {
12798 pos_after = glyph->charpos;
12799 glyph_after = glyph;
12800 }
12801 }
12802 else if (dpos == 0)
12803 match_with_avoid_cursor = 1;
12804 }
12805 else if (STRINGP (glyph->object))
12806 {
12807 Lisp_Object chprop;
12808 EMACS_INT glyph_pos = glyph->charpos;
12809
12810 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12811 glyph->object);
12812 if (INTEGERP (chprop))
12813 {
12814 bpos_covered = bpos_max + XINT (chprop);
12815 /* If the `cursor' property covers buffer positions up
12816 to and including point, we should display cursor on
12817 this glyph. */
12818 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12819 {
12820 cursor = glyph;
12821 break;
12822 }
12823 }
12824 string_seen = 1;
12825 }
12826 --glyph;
12827 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12828 {
12829 x--; /* can't use any pixel_width */
12830 break;
12831 }
12832 x -= glyph->pixel_width;
12833 }
12834
12835 /* Step 2: If we didn't find an exact match for point, we need to
12836 look for a proper place to put the cursor among glyphs between
12837 GLYPH_BEFORE and GLYPH_AFTER. */
12838 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12839 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12840 && bpos_covered < pt_old)
12841 {
12842 /* An empty line has a single glyph whose OBJECT is zero and
12843 whose CHARPOS is the position of a newline on that line.
12844 Note that on a TTY, there are more glyphs after that, which
12845 were produced by extend_face_to_end_of_line, but their
12846 CHARPOS is zero or negative. */
12847 int empty_line_p =
12848 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12849 && INTEGERP (glyph->object) && glyph->charpos > 0;
12850
12851 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12852 {
12853 EMACS_INT ellipsis_pos;
12854
12855 /* Scan back over the ellipsis glyphs. */
12856 if (!row->reversed_p)
12857 {
12858 ellipsis_pos = (glyph - 1)->charpos;
12859 while (glyph > row->glyphs[TEXT_AREA]
12860 && (glyph - 1)->charpos == ellipsis_pos)
12861 glyph--, x -= glyph->pixel_width;
12862 /* That loop always goes one position too far, including
12863 the glyph before the ellipsis. So scan forward over
12864 that one. */
12865 x += glyph->pixel_width;
12866 glyph++;
12867 }
12868 else /* row is reversed */
12869 {
12870 ellipsis_pos = (glyph + 1)->charpos;
12871 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12872 && (glyph + 1)->charpos == ellipsis_pos)
12873 glyph++, x += glyph->pixel_width;
12874 x -= glyph->pixel_width;
12875 glyph--;
12876 }
12877 }
12878 else if (match_with_avoid_cursor
12879 /* A truncated row may not include PT among its
12880 character positions. Setting the cursor inside the
12881 scroll margin will trigger recalculation of hscroll
12882 in hscroll_window_tree. */
12883 || (row->truncated_on_left_p && pt_old < bpos_min)
12884 || (row->truncated_on_right_p && pt_old > bpos_max)
12885 /* Zero-width characters produce no glyphs. */
12886 || (!string_seen
12887 && !empty_line_p
12888 && (row->reversed_p
12889 ? glyph_after > glyphs_end
12890 : glyph_after < glyphs_end)))
12891 {
12892 cursor = glyph_after;
12893 x = -1;
12894 }
12895 else if (string_seen)
12896 {
12897 int incr = row->reversed_p ? -1 : +1;
12898
12899 /* Need to find the glyph that came out of a string which is
12900 present at point. That glyph is somewhere between
12901 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12902 positioned between POS_BEFORE and POS_AFTER in the
12903 buffer. */
12904 struct glyph *stop = glyph_after;
12905 EMACS_INT pos = pos_before;
12906
12907 x = -1;
12908 for (glyph = glyph_before + incr;
12909 row->reversed_p ? glyph > stop : glyph < stop; )
12910 {
12911
12912 /* Any glyphs that come from the buffer are here because
12913 of bidi reordering. Skip them, and only pay
12914 attention to glyphs that came from some string. */
12915 if (STRINGP (glyph->object))
12916 {
12917 Lisp_Object str;
12918 EMACS_INT tem;
12919
12920 str = glyph->object;
12921 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12922 if (tem == 0 /* from overlay */
12923 || pos <= tem)
12924 {
12925 /* If the string from which this glyph came is
12926 found in the buffer at point, then we've
12927 found the glyph we've been looking for. If
12928 it comes from an overlay (tem == 0), and it
12929 has the `cursor' property on one of its
12930 glyphs, record that glyph as a candidate for
12931 displaying the cursor. (As in the
12932 unidirectional version, we will display the
12933 cursor on the last candidate we find.) */
12934 if (tem == 0 || tem == pt_old)
12935 {
12936 /* The glyphs from this string could have
12937 been reordered. Find the one with the
12938 smallest string position. Or there could
12939 be a character in the string with the
12940 `cursor' property, which means display
12941 cursor on that character's glyph. */
12942 EMACS_INT strpos = glyph->charpos;
12943
12944 cursor = glyph;
12945 for (glyph += incr;
12946 (row->reversed_p ? glyph > stop : glyph < stop)
12947 && EQ (glyph->object, str);
12948 glyph += incr)
12949 {
12950 Lisp_Object cprop;
12951 EMACS_INT gpos = glyph->charpos;
12952
12953 cprop = Fget_char_property (make_number (gpos),
12954 Qcursor,
12955 glyph->object);
12956 if (!NILP (cprop))
12957 {
12958 cursor = glyph;
12959 break;
12960 }
12961 if (glyph->charpos < strpos)
12962 {
12963 strpos = glyph->charpos;
12964 cursor = glyph;
12965 }
12966 }
12967
12968 if (tem == pt_old)
12969 goto compute_x;
12970 }
12971 if (tem)
12972 pos = tem + 1; /* don't find previous instances */
12973 }
12974 /* This string is not what we want; skip all of the
12975 glyphs that came from it. */
12976 do
12977 glyph += incr;
12978 while ((row->reversed_p ? glyph > stop : glyph < stop)
12979 && EQ (glyph->object, str));
12980 }
12981 else
12982 glyph += incr;
12983 }
12984
12985 /* If we reached the end of the line, and END was from a string,
12986 the cursor is not on this line. */
12987 if (cursor == NULL
12988 && (row->reversed_p ? glyph <= end : glyph >= end)
12989 && STRINGP (end->object)
12990 && row->continued_p)
12991 return 0;
12992 }
12993 }
12994
12995 compute_x:
12996 if (cursor != NULL)
12997 glyph = cursor;
12998 if (x < 0)
12999 {
13000 struct glyph *g;
13001
13002 /* Need to compute x that corresponds to GLYPH. */
13003 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13004 {
13005 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13006 abort ();
13007 x += g->pixel_width;
13008 }
13009 }
13010
13011 /* ROW could be part of a continued line, which, under bidi
13012 reordering, might have other rows whose start and end charpos
13013 occlude point. Only set w->cursor if we found a better
13014 approximation to the cursor position than we have from previously
13015 examined candidate rows belonging to the same continued line. */
13016 if (/* we already have a candidate row */
13017 w->cursor.vpos >= 0
13018 /* that candidate is not the row we are processing */
13019 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13020 /* the row we are processing is part of a continued line */
13021 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13022 /* Make sure cursor.vpos specifies a row whose start and end
13023 charpos occlude point. This is because some callers of this
13024 function leave cursor.vpos at the row where the cursor was
13025 displayed during the last redisplay cycle. */
13026 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13027 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13028 {
13029 struct glyph *g1 =
13030 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13031
13032 /* Don't consider glyphs that are outside TEXT_AREA. */
13033 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13034 return 0;
13035 /* Keep the candidate whose buffer position is the closest to
13036 point. */
13037 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13038 w->cursor.hpos >= 0
13039 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13040 && BUFFERP (g1->object)
13041 && (g1->charpos == pt_old /* an exact match always wins */
13042 || (BUFFERP (glyph->object)
13043 && eabs (g1->charpos - pt_old)
13044 < eabs (glyph->charpos - pt_old))))
13045 return 0;
13046 /* If this candidate gives an exact match, use that. */
13047 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13048 /* Otherwise, keep the candidate that comes from a row
13049 spanning less buffer positions. This may win when one or
13050 both candidate positions are on glyphs that came from
13051 display strings, for which we cannot compare buffer
13052 positions. */
13053 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13054 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13055 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13056 return 0;
13057 }
13058 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13059 w->cursor.x = x;
13060 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13061 w->cursor.y = row->y + dy;
13062
13063 if (w == XWINDOW (selected_window))
13064 {
13065 if (!row->continued_p
13066 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13067 && row->x == 0)
13068 {
13069 this_line_buffer = XBUFFER (w->buffer);
13070
13071 CHARPOS (this_line_start_pos)
13072 = MATRIX_ROW_START_CHARPOS (row) + delta;
13073 BYTEPOS (this_line_start_pos)
13074 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13075
13076 CHARPOS (this_line_end_pos)
13077 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13078 BYTEPOS (this_line_end_pos)
13079 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13080
13081 this_line_y = w->cursor.y;
13082 this_line_pixel_height = row->height;
13083 this_line_vpos = w->cursor.vpos;
13084 this_line_start_x = row->x;
13085 }
13086 else
13087 CHARPOS (this_line_start_pos) = 0;
13088 }
13089
13090 return 1;
13091 }
13092
13093
13094 /* Run window scroll functions, if any, for WINDOW with new window
13095 start STARTP. Sets the window start of WINDOW to that position.
13096
13097 We assume that the window's buffer is really current. */
13098
13099 static INLINE struct text_pos
13100 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13101 {
13102 struct window *w = XWINDOW (window);
13103 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13104
13105 if (current_buffer != XBUFFER (w->buffer))
13106 abort ();
13107
13108 if (!NILP (Vwindow_scroll_functions))
13109 {
13110 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13111 make_number (CHARPOS (startp)));
13112 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13113 /* In case the hook functions switch buffers. */
13114 if (current_buffer != XBUFFER (w->buffer))
13115 set_buffer_internal_1 (XBUFFER (w->buffer));
13116 }
13117
13118 return startp;
13119 }
13120
13121
13122 /* Make sure the line containing the cursor is fully visible.
13123 A value of 1 means there is nothing to be done.
13124 (Either the line is fully visible, or it cannot be made so,
13125 or we cannot tell.)
13126
13127 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13128 is higher than window.
13129
13130 A value of 0 means the caller should do scrolling
13131 as if point had gone off the screen. */
13132
13133 static int
13134 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13135 {
13136 struct glyph_matrix *matrix;
13137 struct glyph_row *row;
13138 int window_height;
13139
13140 if (!make_cursor_line_fully_visible_p)
13141 return 1;
13142
13143 /* It's not always possible to find the cursor, e.g, when a window
13144 is full of overlay strings. Don't do anything in that case. */
13145 if (w->cursor.vpos < 0)
13146 return 1;
13147
13148 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13149 row = MATRIX_ROW (matrix, w->cursor.vpos);
13150
13151 /* If the cursor row is not partially visible, there's nothing to do. */
13152 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13153 return 1;
13154
13155 /* If the row the cursor is in is taller than the window's height,
13156 it's not clear what to do, so do nothing. */
13157 window_height = window_box_height (w);
13158 if (row->height >= window_height)
13159 {
13160 if (!force_p || MINI_WINDOW_P (w)
13161 || w->vscroll || w->cursor.vpos == 0)
13162 return 1;
13163 }
13164 return 0;
13165 }
13166
13167
13168 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13169 non-zero means only WINDOW is redisplayed in redisplay_internal.
13170 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13171 in redisplay_window to bring a partially visible line into view in
13172 the case that only the cursor has moved.
13173
13174 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13175 last screen line's vertical height extends past the end of the screen.
13176
13177 Value is
13178
13179 1 if scrolling succeeded
13180
13181 0 if scrolling didn't find point.
13182
13183 -1 if new fonts have been loaded so that we must interrupt
13184 redisplay, adjust glyph matrices, and try again. */
13185
13186 enum
13187 {
13188 SCROLLING_SUCCESS,
13189 SCROLLING_FAILED,
13190 SCROLLING_NEED_LARGER_MATRICES
13191 };
13192
13193 static int
13194 try_scrolling (Lisp_Object window, int just_this_one_p,
13195 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13196 int temp_scroll_step, int last_line_misfit)
13197 {
13198 struct window *w = XWINDOW (window);
13199 struct frame *f = XFRAME (w->frame);
13200 struct text_pos pos, startp;
13201 struct it it;
13202 int this_scroll_margin, scroll_max, rc, height;
13203 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13204 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13205 Lisp_Object aggressive;
13206 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13207
13208 #if GLYPH_DEBUG
13209 debug_method_add (w, "try_scrolling");
13210 #endif
13211
13212 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13213
13214 /* Compute scroll margin height in pixels. We scroll when point is
13215 within this distance from the top or bottom of the window. */
13216 if (scroll_margin > 0)
13217 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13218 * FRAME_LINE_HEIGHT (f);
13219 else
13220 this_scroll_margin = 0;
13221
13222 /* Force scroll_conservatively to have a reasonable value, to avoid
13223 overflow while computing how much to scroll. Note that the user
13224 can supply scroll-conservatively equal to `most-positive-fixnum',
13225 which can be larger than INT_MAX. */
13226 if (scroll_conservatively > scroll_limit)
13227 {
13228 scroll_conservatively = scroll_limit;
13229 scroll_max = INT_MAX;
13230 }
13231 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13232 /* Compute how much we should try to scroll maximally to bring
13233 point into view. */
13234 scroll_max = (max (scroll_step,
13235 max (scroll_conservatively, temp_scroll_step))
13236 * FRAME_LINE_HEIGHT (f));
13237 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13238 || NUMBERP (current_buffer->scroll_up_aggressively))
13239 /* We're trying to scroll because of aggressive scrolling but no
13240 scroll_step is set. Choose an arbitrary one. */
13241 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13242 else
13243 scroll_max = 0;
13244
13245 too_near_end:
13246
13247 /* Decide whether to scroll down. */
13248 if (PT > CHARPOS (startp))
13249 {
13250 int scroll_margin_y;
13251
13252 /* Compute the pixel ypos of the scroll margin, then move it to
13253 either that ypos or PT, whichever comes first. */
13254 start_display (&it, w, startp);
13255 scroll_margin_y = it.last_visible_y - this_scroll_margin
13256 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13257 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13258 (MOVE_TO_POS | MOVE_TO_Y));
13259
13260 if (PT > CHARPOS (it.current.pos))
13261 {
13262 int y0 = line_bottom_y (&it);
13263 /* Compute how many pixels below window bottom to stop searching
13264 for PT. This avoids costly search for PT that is far away if
13265 the user limited scrolling by a small number of lines, but
13266 always finds PT if scroll_conservatively is set to a large
13267 number, such as most-positive-fixnum. */
13268 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13269 int y_to_move =
13270 slack >= INT_MAX - it.last_visible_y
13271 ? INT_MAX
13272 : it.last_visible_y + slack;
13273
13274 /* Compute the distance from the scroll margin to PT or to
13275 the scroll limit, whichever comes first. This should
13276 include the height of the cursor line, to make that line
13277 fully visible. */
13278 move_it_to (&it, PT, -1, y_to_move,
13279 -1, MOVE_TO_POS | MOVE_TO_Y);
13280 dy = line_bottom_y (&it) - y0;
13281
13282 if (dy > scroll_max)
13283 return SCROLLING_FAILED;
13284
13285 scroll_down_p = 1;
13286 }
13287 }
13288
13289 if (scroll_down_p)
13290 {
13291 /* Point is in or below the bottom scroll margin, so move the
13292 window start down. If scrolling conservatively, move it just
13293 enough down to make point visible. If scroll_step is set,
13294 move it down by scroll_step. */
13295 if (scroll_conservatively)
13296 amount_to_scroll
13297 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13298 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13299 else if (scroll_step || temp_scroll_step)
13300 amount_to_scroll = scroll_max;
13301 else
13302 {
13303 aggressive = current_buffer->scroll_up_aggressively;
13304 height = WINDOW_BOX_TEXT_HEIGHT (w);
13305 if (NUMBERP (aggressive))
13306 {
13307 double float_amount = XFLOATINT (aggressive) * height;
13308 amount_to_scroll = float_amount;
13309 if (amount_to_scroll == 0 && float_amount > 0)
13310 amount_to_scroll = 1;
13311 }
13312 }
13313
13314 if (amount_to_scroll <= 0)
13315 return SCROLLING_FAILED;
13316
13317 start_display (&it, w, startp);
13318 if (scroll_max < INT_MAX)
13319 move_it_vertically (&it, amount_to_scroll);
13320 else
13321 {
13322 /* Extra precision for users who set scroll-conservatively
13323 to most-positive-fixnum: make sure the amount we scroll
13324 the window start is never less than amount_to_scroll,
13325 which was computed as distance from window bottom to
13326 point. This matters when lines at window top and lines
13327 below window bottom have different height. */
13328 struct it it1 = it;
13329 /* We use a temporary it1 because line_bottom_y can modify
13330 its argument, if it moves one line down; see there. */
13331 int start_y = line_bottom_y (&it1);
13332
13333 do {
13334 move_it_by_lines (&it, 1, 1);
13335 it1 = it;
13336 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13337 }
13338
13339 /* If STARTP is unchanged, move it down another screen line. */
13340 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13341 move_it_by_lines (&it, 1, 1);
13342 startp = it.current.pos;
13343 }
13344 else
13345 {
13346 struct text_pos scroll_margin_pos = startp;
13347
13348 /* See if point is inside the scroll margin at the top of the
13349 window. */
13350 if (this_scroll_margin)
13351 {
13352 start_display (&it, w, startp);
13353 move_it_vertically (&it, this_scroll_margin);
13354 scroll_margin_pos = it.current.pos;
13355 }
13356
13357 if (PT < CHARPOS (scroll_margin_pos))
13358 {
13359 /* Point is in the scroll margin at the top of the window or
13360 above what is displayed in the window. */
13361 int y0;
13362
13363 /* Compute the vertical distance from PT to the scroll
13364 margin position. Give up if distance is greater than
13365 scroll_max. */
13366 SET_TEXT_POS (pos, PT, PT_BYTE);
13367 start_display (&it, w, pos);
13368 y0 = it.current_y;
13369 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13370 it.last_visible_y, -1,
13371 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13372 dy = it.current_y - y0;
13373 if (dy > scroll_max)
13374 return SCROLLING_FAILED;
13375
13376 /* Compute new window start. */
13377 start_display (&it, w, startp);
13378
13379 if (scroll_conservatively)
13380 amount_to_scroll
13381 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13382 else if (scroll_step || temp_scroll_step)
13383 amount_to_scroll = scroll_max;
13384 else
13385 {
13386 aggressive = current_buffer->scroll_down_aggressively;
13387 height = WINDOW_BOX_TEXT_HEIGHT (w);
13388 if (NUMBERP (aggressive))
13389 {
13390 double float_amount = XFLOATINT (aggressive) * height;
13391 amount_to_scroll = float_amount;
13392 if (amount_to_scroll == 0 && float_amount > 0)
13393 amount_to_scroll = 1;
13394 }
13395 }
13396
13397 if (amount_to_scroll <= 0)
13398 return SCROLLING_FAILED;
13399
13400 move_it_vertically_backward (&it, amount_to_scroll);
13401 startp = it.current.pos;
13402 }
13403 }
13404
13405 /* Run window scroll functions. */
13406 startp = run_window_scroll_functions (window, startp);
13407
13408 /* Display the window. Give up if new fonts are loaded, or if point
13409 doesn't appear. */
13410 if (!try_window (window, startp, 0))
13411 rc = SCROLLING_NEED_LARGER_MATRICES;
13412 else if (w->cursor.vpos < 0)
13413 {
13414 clear_glyph_matrix (w->desired_matrix);
13415 rc = SCROLLING_FAILED;
13416 }
13417 else
13418 {
13419 /* Maybe forget recorded base line for line number display. */
13420 if (!just_this_one_p
13421 || current_buffer->clip_changed
13422 || BEG_UNCHANGED < CHARPOS (startp))
13423 w->base_line_number = Qnil;
13424
13425 /* If cursor ends up on a partially visible line,
13426 treat that as being off the bottom of the screen. */
13427 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13428 {
13429 clear_glyph_matrix (w->desired_matrix);
13430 ++extra_scroll_margin_lines;
13431 goto too_near_end;
13432 }
13433 rc = SCROLLING_SUCCESS;
13434 }
13435
13436 return rc;
13437 }
13438
13439
13440 /* Compute a suitable window start for window W if display of W starts
13441 on a continuation line. Value is non-zero if a new window start
13442 was computed.
13443
13444 The new window start will be computed, based on W's width, starting
13445 from the start of the continued line. It is the start of the
13446 screen line with the minimum distance from the old start W->start. */
13447
13448 static int
13449 compute_window_start_on_continuation_line (struct window *w)
13450 {
13451 struct text_pos pos, start_pos;
13452 int window_start_changed_p = 0;
13453
13454 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13455
13456 /* If window start is on a continuation line... Window start may be
13457 < BEGV in case there's invisible text at the start of the
13458 buffer (M-x rmail, for example). */
13459 if (CHARPOS (start_pos) > BEGV
13460 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13461 {
13462 struct it it;
13463 struct glyph_row *row;
13464
13465 /* Handle the case that the window start is out of range. */
13466 if (CHARPOS (start_pos) < BEGV)
13467 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13468 else if (CHARPOS (start_pos) > ZV)
13469 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13470
13471 /* Find the start of the continued line. This should be fast
13472 because scan_buffer is fast (newline cache). */
13473 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13474 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13475 row, DEFAULT_FACE_ID);
13476 reseat_at_previous_visible_line_start (&it);
13477
13478 /* If the line start is "too far" away from the window start,
13479 say it takes too much time to compute a new window start. */
13480 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13481 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13482 {
13483 int min_distance, distance;
13484
13485 /* Move forward by display lines to find the new window
13486 start. If window width was enlarged, the new start can
13487 be expected to be > the old start. If window width was
13488 decreased, the new window start will be < the old start.
13489 So, we're looking for the display line start with the
13490 minimum distance from the old window start. */
13491 pos = it.current.pos;
13492 min_distance = INFINITY;
13493 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13494 distance < min_distance)
13495 {
13496 min_distance = distance;
13497 pos = it.current.pos;
13498 move_it_by_lines (&it, 1, 0);
13499 }
13500
13501 /* Set the window start there. */
13502 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13503 window_start_changed_p = 1;
13504 }
13505 }
13506
13507 return window_start_changed_p;
13508 }
13509
13510
13511 /* Try cursor movement in case text has not changed in window WINDOW,
13512 with window start STARTP. Value is
13513
13514 CURSOR_MOVEMENT_SUCCESS if successful
13515
13516 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13517
13518 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13519 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13520 we want to scroll as if scroll-step were set to 1. See the code.
13521
13522 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13523 which case we have to abort this redisplay, and adjust matrices
13524 first. */
13525
13526 enum
13527 {
13528 CURSOR_MOVEMENT_SUCCESS,
13529 CURSOR_MOVEMENT_CANNOT_BE_USED,
13530 CURSOR_MOVEMENT_MUST_SCROLL,
13531 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13532 };
13533
13534 static int
13535 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13536 {
13537 struct window *w = XWINDOW (window);
13538 struct frame *f = XFRAME (w->frame);
13539 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13540
13541 #if GLYPH_DEBUG
13542 if (inhibit_try_cursor_movement)
13543 return rc;
13544 #endif
13545
13546 /* Handle case where text has not changed, only point, and it has
13547 not moved off the frame. */
13548 if (/* Point may be in this window. */
13549 PT >= CHARPOS (startp)
13550 /* Selective display hasn't changed. */
13551 && !current_buffer->clip_changed
13552 /* Function force-mode-line-update is used to force a thorough
13553 redisplay. It sets either windows_or_buffers_changed or
13554 update_mode_lines. So don't take a shortcut here for these
13555 cases. */
13556 && !update_mode_lines
13557 && !windows_or_buffers_changed
13558 && !cursor_type_changed
13559 /* Can't use this case if highlighting a region. When a
13560 region exists, cursor movement has to do more than just
13561 set the cursor. */
13562 && !(!NILP (Vtransient_mark_mode)
13563 && !NILP (current_buffer->mark_active))
13564 && NILP (w->region_showing)
13565 && NILP (Vshow_trailing_whitespace)
13566 /* Right after splitting windows, last_point may be nil. */
13567 && INTEGERP (w->last_point)
13568 /* This code is not used for mini-buffer for the sake of the case
13569 of redisplaying to replace an echo area message; since in
13570 that case the mini-buffer contents per se are usually
13571 unchanged. This code is of no real use in the mini-buffer
13572 since the handling of this_line_start_pos, etc., in redisplay
13573 handles the same cases. */
13574 && !EQ (window, minibuf_window)
13575 /* When splitting windows or for new windows, it happens that
13576 redisplay is called with a nil window_end_vpos or one being
13577 larger than the window. This should really be fixed in
13578 window.c. I don't have this on my list, now, so we do
13579 approximately the same as the old redisplay code. --gerd. */
13580 && INTEGERP (w->window_end_vpos)
13581 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13582 && (FRAME_WINDOW_P (f)
13583 || !overlay_arrow_in_current_buffer_p ()))
13584 {
13585 int this_scroll_margin, top_scroll_margin;
13586 struct glyph_row *row = NULL;
13587
13588 #if GLYPH_DEBUG
13589 debug_method_add (w, "cursor movement");
13590 #endif
13591
13592 /* Scroll if point within this distance from the top or bottom
13593 of the window. This is a pixel value. */
13594 if (scroll_margin > 0)
13595 {
13596 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13597 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13598 }
13599 else
13600 this_scroll_margin = 0;
13601
13602 top_scroll_margin = this_scroll_margin;
13603 if (WINDOW_WANTS_HEADER_LINE_P (w))
13604 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13605
13606 /* Start with the row the cursor was displayed during the last
13607 not paused redisplay. Give up if that row is not valid. */
13608 if (w->last_cursor.vpos < 0
13609 || w->last_cursor.vpos >= w->current_matrix->nrows)
13610 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13611 else
13612 {
13613 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13614 if (row->mode_line_p)
13615 ++row;
13616 if (!row->enabled_p)
13617 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13618 }
13619
13620 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13621 {
13622 int scroll_p = 0, must_scroll = 0;
13623 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13624
13625 if (PT > XFASTINT (w->last_point))
13626 {
13627 /* Point has moved forward. */
13628 while (MATRIX_ROW_END_CHARPOS (row) < PT
13629 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13630 {
13631 xassert (row->enabled_p);
13632 ++row;
13633 }
13634
13635 /* If the end position of a row equals the start
13636 position of the next row, and PT is at that position,
13637 we would rather display cursor in the next line. */
13638 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13639 && MATRIX_ROW_END_CHARPOS (row) == PT
13640 && row < w->current_matrix->rows
13641 + w->current_matrix->nrows - 1
13642 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13643 && !cursor_row_p (w, row))
13644 ++row;
13645
13646 /* If within the scroll margin, scroll. Note that
13647 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13648 the next line would be drawn, and that
13649 this_scroll_margin can be zero. */
13650 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13651 || PT > MATRIX_ROW_END_CHARPOS (row)
13652 /* Line is completely visible last line in window
13653 and PT is to be set in the next line. */
13654 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13655 && PT == MATRIX_ROW_END_CHARPOS (row)
13656 && !row->ends_at_zv_p
13657 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13658 scroll_p = 1;
13659 }
13660 else if (PT < XFASTINT (w->last_point))
13661 {
13662 /* Cursor has to be moved backward. Note that PT >=
13663 CHARPOS (startp) because of the outer if-statement. */
13664 while (!row->mode_line_p
13665 && (MATRIX_ROW_START_CHARPOS (row) > PT
13666 || (MATRIX_ROW_START_CHARPOS (row) == PT
13667 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13668 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13669 row > w->current_matrix->rows
13670 && (row-1)->ends_in_newline_from_string_p))))
13671 && (row->y > top_scroll_margin
13672 || CHARPOS (startp) == BEGV))
13673 {
13674 xassert (row->enabled_p);
13675 --row;
13676 }
13677
13678 /* Consider the following case: Window starts at BEGV,
13679 there is invisible, intangible text at BEGV, so that
13680 display starts at some point START > BEGV. It can
13681 happen that we are called with PT somewhere between
13682 BEGV and START. Try to handle that case. */
13683 if (row < w->current_matrix->rows
13684 || row->mode_line_p)
13685 {
13686 row = w->current_matrix->rows;
13687 if (row->mode_line_p)
13688 ++row;
13689 }
13690
13691 /* Due to newlines in overlay strings, we may have to
13692 skip forward over overlay strings. */
13693 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13694 && MATRIX_ROW_END_CHARPOS (row) == PT
13695 && !cursor_row_p (w, row))
13696 ++row;
13697
13698 /* If within the scroll margin, scroll. */
13699 if (row->y < top_scroll_margin
13700 && CHARPOS (startp) != BEGV)
13701 scroll_p = 1;
13702 }
13703 else
13704 {
13705 /* Cursor did not move. So don't scroll even if cursor line
13706 is partially visible, as it was so before. */
13707 rc = CURSOR_MOVEMENT_SUCCESS;
13708 }
13709
13710 if (PT < MATRIX_ROW_START_CHARPOS (row)
13711 || PT > MATRIX_ROW_END_CHARPOS (row))
13712 {
13713 /* if PT is not in the glyph row, give up. */
13714 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13715 must_scroll = 1;
13716 }
13717 else if (rc != CURSOR_MOVEMENT_SUCCESS
13718 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13719 {
13720 /* If rows are bidi-reordered and point moved, back up
13721 until we find a row that does not belong to a
13722 continuation line. This is because we must consider
13723 all rows of a continued line as candidates for the
13724 new cursor positioning, since row start and end
13725 positions change non-linearly with vertical position
13726 in such rows. */
13727 /* FIXME: Revisit this when glyph ``spilling'' in
13728 continuation lines' rows is implemented for
13729 bidi-reordered rows. */
13730 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13731 {
13732 xassert (row->enabled_p);
13733 --row;
13734 /* If we hit the beginning of the displayed portion
13735 without finding the first row of a continued
13736 line, give up. */
13737 if (row <= w->current_matrix->rows)
13738 {
13739 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13740 break;
13741 }
13742
13743 }
13744 }
13745 if (must_scroll)
13746 ;
13747 else if (rc != CURSOR_MOVEMENT_SUCCESS
13748 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13749 && make_cursor_line_fully_visible_p)
13750 {
13751 if (PT == MATRIX_ROW_END_CHARPOS (row)
13752 && !row->ends_at_zv_p
13753 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13754 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13755 else if (row->height > window_box_height (w))
13756 {
13757 /* If we end up in a partially visible line, let's
13758 make it fully visible, except when it's taller
13759 than the window, in which case we can't do much
13760 about it. */
13761 *scroll_step = 1;
13762 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13763 }
13764 else
13765 {
13766 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13767 if (!cursor_row_fully_visible_p (w, 0, 1))
13768 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13769 else
13770 rc = CURSOR_MOVEMENT_SUCCESS;
13771 }
13772 }
13773 else if (scroll_p)
13774 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13775 else if (rc != CURSOR_MOVEMENT_SUCCESS
13776 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13777 {
13778 /* With bidi-reordered rows, there could be more than
13779 one candidate row whose start and end positions
13780 occlude point. We need to let set_cursor_from_row
13781 find the best candidate. */
13782 /* FIXME: Revisit this when glyph ``spilling'' in
13783 continuation lines' rows is implemented for
13784 bidi-reordered rows. */
13785 int rv = 0;
13786
13787 do
13788 {
13789 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13790 && PT <= MATRIX_ROW_END_CHARPOS (row)
13791 && cursor_row_p (w, row))
13792 rv |= set_cursor_from_row (w, row, w->current_matrix,
13793 0, 0, 0, 0);
13794 /* As soon as we've found the first suitable row
13795 whose ends_at_zv_p flag is set, we are done. */
13796 if (rv
13797 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13798 {
13799 rc = CURSOR_MOVEMENT_SUCCESS;
13800 break;
13801 }
13802 ++row;
13803 }
13804 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13805 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13806 || (MATRIX_ROW_START_CHARPOS (row) == PT
13807 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13808 /* If we didn't find any candidate rows, or exited the
13809 loop before all the candidates were examined, signal
13810 to the caller that this method failed. */
13811 if (rc != CURSOR_MOVEMENT_SUCCESS
13812 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13813 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13814 else if (rv)
13815 rc = CURSOR_MOVEMENT_SUCCESS;
13816 }
13817 else
13818 {
13819 do
13820 {
13821 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13822 {
13823 rc = CURSOR_MOVEMENT_SUCCESS;
13824 break;
13825 }
13826 ++row;
13827 }
13828 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13829 && MATRIX_ROW_START_CHARPOS (row) == PT
13830 && cursor_row_p (w, row));
13831 }
13832 }
13833 }
13834
13835 return rc;
13836 }
13837
13838 void
13839 set_vertical_scroll_bar (struct window *w)
13840 {
13841 EMACS_INT start, end, whole;
13842
13843 /* Calculate the start and end positions for the current window.
13844 At some point, it would be nice to choose between scrollbars
13845 which reflect the whole buffer size, with special markers
13846 indicating narrowing, and scrollbars which reflect only the
13847 visible region.
13848
13849 Note that mini-buffers sometimes aren't displaying any text. */
13850 if (!MINI_WINDOW_P (w)
13851 || (w == XWINDOW (minibuf_window)
13852 && NILP (echo_area_buffer[0])))
13853 {
13854 struct buffer *buf = XBUFFER (w->buffer);
13855 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13856 start = marker_position (w->start) - BUF_BEGV (buf);
13857 /* I don't think this is guaranteed to be right. For the
13858 moment, we'll pretend it is. */
13859 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13860
13861 if (end < start)
13862 end = start;
13863 if (whole < (end - start))
13864 whole = end - start;
13865 }
13866 else
13867 start = end = whole = 0;
13868
13869 /* Indicate what this scroll bar ought to be displaying now. */
13870 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13871 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13872 (w, end - start, whole, start);
13873 }
13874
13875
13876 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13877 selected_window is redisplayed.
13878
13879 We can return without actually redisplaying the window if
13880 fonts_changed_p is nonzero. In that case, redisplay_internal will
13881 retry. */
13882
13883 static void
13884 redisplay_window (Lisp_Object window, int just_this_one_p)
13885 {
13886 struct window *w = XWINDOW (window);
13887 struct frame *f = XFRAME (w->frame);
13888 struct buffer *buffer = XBUFFER (w->buffer);
13889 struct buffer *old = current_buffer;
13890 struct text_pos lpoint, opoint, startp;
13891 int update_mode_line;
13892 int tem;
13893 struct it it;
13894 /* Record it now because it's overwritten. */
13895 int current_matrix_up_to_date_p = 0;
13896 int used_current_matrix_p = 0;
13897 /* This is less strict than current_matrix_up_to_date_p.
13898 It indictes that the buffer contents and narrowing are unchanged. */
13899 int buffer_unchanged_p = 0;
13900 int temp_scroll_step = 0;
13901 int count = SPECPDL_INDEX ();
13902 int rc;
13903 int centering_position = -1;
13904 int last_line_misfit = 0;
13905 EMACS_INT beg_unchanged, end_unchanged;
13906
13907 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13908 opoint = lpoint;
13909
13910 /* W must be a leaf window here. */
13911 xassert (!NILP (w->buffer));
13912 #if GLYPH_DEBUG
13913 *w->desired_matrix->method = 0;
13914 #endif
13915
13916 restart:
13917 reconsider_clip_changes (w, buffer);
13918
13919 /* Has the mode line to be updated? */
13920 update_mode_line = (!NILP (w->update_mode_line)
13921 || update_mode_lines
13922 || buffer->clip_changed
13923 || buffer->prevent_redisplay_optimizations_p);
13924
13925 if (MINI_WINDOW_P (w))
13926 {
13927 if (w == XWINDOW (echo_area_window)
13928 && !NILP (echo_area_buffer[0]))
13929 {
13930 if (update_mode_line)
13931 /* We may have to update a tty frame's menu bar or a
13932 tool-bar. Example `M-x C-h C-h C-g'. */
13933 goto finish_menu_bars;
13934 else
13935 /* We've already displayed the echo area glyphs in this window. */
13936 goto finish_scroll_bars;
13937 }
13938 else if ((w != XWINDOW (minibuf_window)
13939 || minibuf_level == 0)
13940 /* When buffer is nonempty, redisplay window normally. */
13941 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13942 /* Quail displays non-mini buffers in minibuffer window.
13943 In that case, redisplay the window normally. */
13944 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13945 {
13946 /* W is a mini-buffer window, but it's not active, so clear
13947 it. */
13948 int yb = window_text_bottom_y (w);
13949 struct glyph_row *row;
13950 int y;
13951
13952 for (y = 0, row = w->desired_matrix->rows;
13953 y < yb;
13954 y += row->height, ++row)
13955 blank_row (w, row, y);
13956 goto finish_scroll_bars;
13957 }
13958
13959 clear_glyph_matrix (w->desired_matrix);
13960 }
13961
13962 /* Otherwise set up data on this window; select its buffer and point
13963 value. */
13964 /* Really select the buffer, for the sake of buffer-local
13965 variables. */
13966 set_buffer_internal_1 (XBUFFER (w->buffer));
13967
13968 current_matrix_up_to_date_p
13969 = (!NILP (w->window_end_valid)
13970 && !current_buffer->clip_changed
13971 && !current_buffer->prevent_redisplay_optimizations_p
13972 && XFASTINT (w->last_modified) >= MODIFF
13973 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13974
13975 /* Run the window-bottom-change-functions
13976 if it is possible that the text on the screen has changed
13977 (either due to modification of the text, or any other reason). */
13978 if (!current_matrix_up_to_date_p
13979 && !NILP (Vwindow_text_change_functions))
13980 {
13981 safe_run_hooks (Qwindow_text_change_functions);
13982 goto restart;
13983 }
13984
13985 beg_unchanged = BEG_UNCHANGED;
13986 end_unchanged = END_UNCHANGED;
13987
13988 SET_TEXT_POS (opoint, PT, PT_BYTE);
13989
13990 specbind (Qinhibit_point_motion_hooks, Qt);
13991
13992 buffer_unchanged_p
13993 = (!NILP (w->window_end_valid)
13994 && !current_buffer->clip_changed
13995 && XFASTINT (w->last_modified) >= MODIFF
13996 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13997
13998 /* When windows_or_buffers_changed is non-zero, we can't rely on
13999 the window end being valid, so set it to nil there. */
14000 if (windows_or_buffers_changed)
14001 {
14002 /* If window starts on a continuation line, maybe adjust the
14003 window start in case the window's width changed. */
14004 if (XMARKER (w->start)->buffer == current_buffer)
14005 compute_window_start_on_continuation_line (w);
14006
14007 w->window_end_valid = Qnil;
14008 }
14009
14010 /* Some sanity checks. */
14011 CHECK_WINDOW_END (w);
14012 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14013 abort ();
14014 if (BYTEPOS (opoint) < CHARPOS (opoint))
14015 abort ();
14016
14017 /* If %c is in mode line, update it if needed. */
14018 if (!NILP (w->column_number_displayed)
14019 /* This alternative quickly identifies a common case
14020 where no change is needed. */
14021 && !(PT == XFASTINT (w->last_point)
14022 && XFASTINT (w->last_modified) >= MODIFF
14023 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14024 && (XFASTINT (w->column_number_displayed)
14025 != (int) current_column ())) /* iftc */
14026 update_mode_line = 1;
14027
14028 /* Count number of windows showing the selected buffer. An indirect
14029 buffer counts as its base buffer. */
14030 if (!just_this_one_p)
14031 {
14032 struct buffer *current_base, *window_base;
14033 current_base = current_buffer;
14034 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14035 if (current_base->base_buffer)
14036 current_base = current_base->base_buffer;
14037 if (window_base->base_buffer)
14038 window_base = window_base->base_buffer;
14039 if (current_base == window_base)
14040 buffer_shared++;
14041 }
14042
14043 /* Point refers normally to the selected window. For any other
14044 window, set up appropriate value. */
14045 if (!EQ (window, selected_window))
14046 {
14047 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14048 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14049 if (new_pt < BEGV)
14050 {
14051 new_pt = BEGV;
14052 new_pt_byte = BEGV_BYTE;
14053 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14054 }
14055 else if (new_pt > (ZV - 1))
14056 {
14057 new_pt = ZV;
14058 new_pt_byte = ZV_BYTE;
14059 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14060 }
14061
14062 /* We don't use SET_PT so that the point-motion hooks don't run. */
14063 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14064 }
14065
14066 /* If any of the character widths specified in the display table
14067 have changed, invalidate the width run cache. It's true that
14068 this may be a bit late to catch such changes, but the rest of
14069 redisplay goes (non-fatally) haywire when the display table is
14070 changed, so why should we worry about doing any better? */
14071 if (current_buffer->width_run_cache)
14072 {
14073 struct Lisp_Char_Table *disptab = buffer_display_table ();
14074
14075 if (! disptab_matches_widthtab (disptab,
14076 XVECTOR (current_buffer->width_table)))
14077 {
14078 invalidate_region_cache (current_buffer,
14079 current_buffer->width_run_cache,
14080 BEG, Z);
14081 recompute_width_table (current_buffer, disptab);
14082 }
14083 }
14084
14085 /* If window-start is screwed up, choose a new one. */
14086 if (XMARKER (w->start)->buffer != current_buffer)
14087 goto recenter;
14088
14089 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14090
14091 /* If someone specified a new starting point but did not insist,
14092 check whether it can be used. */
14093 if (!NILP (w->optional_new_start)
14094 && CHARPOS (startp) >= BEGV
14095 && CHARPOS (startp) <= ZV)
14096 {
14097 w->optional_new_start = Qnil;
14098 start_display (&it, w, startp);
14099 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14100 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14101 if (IT_CHARPOS (it) == PT)
14102 w->force_start = Qt;
14103 /* IT may overshoot PT if text at PT is invisible. */
14104 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14105 w->force_start = Qt;
14106 }
14107
14108 force_start:
14109
14110 /* Handle case where place to start displaying has been specified,
14111 unless the specified location is outside the accessible range. */
14112 if (!NILP (w->force_start)
14113 || w->frozen_window_start_p)
14114 {
14115 /* We set this later on if we have to adjust point. */
14116 int new_vpos = -1;
14117
14118 w->force_start = Qnil;
14119 w->vscroll = 0;
14120 w->window_end_valid = Qnil;
14121
14122 /* Forget any recorded base line for line number display. */
14123 if (!buffer_unchanged_p)
14124 w->base_line_number = Qnil;
14125
14126 /* Redisplay the mode line. Select the buffer properly for that.
14127 Also, run the hook window-scroll-functions
14128 because we have scrolled. */
14129 /* Note, we do this after clearing force_start because
14130 if there's an error, it is better to forget about force_start
14131 than to get into an infinite loop calling the hook functions
14132 and having them get more errors. */
14133 if (!update_mode_line
14134 || ! NILP (Vwindow_scroll_functions))
14135 {
14136 update_mode_line = 1;
14137 w->update_mode_line = Qt;
14138 startp = run_window_scroll_functions (window, startp);
14139 }
14140
14141 w->last_modified = make_number (0);
14142 w->last_overlay_modified = make_number (0);
14143 if (CHARPOS (startp) < BEGV)
14144 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14145 else if (CHARPOS (startp) > ZV)
14146 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14147
14148 /* Redisplay, then check if cursor has been set during the
14149 redisplay. Give up if new fonts were loaded. */
14150 /* We used to issue a CHECK_MARGINS argument to try_window here,
14151 but this causes scrolling to fail when point begins inside
14152 the scroll margin (bug#148) -- cyd */
14153 if (!try_window (window, startp, 0))
14154 {
14155 w->force_start = Qt;
14156 clear_glyph_matrix (w->desired_matrix);
14157 goto need_larger_matrices;
14158 }
14159
14160 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14161 {
14162 /* If point does not appear, try to move point so it does
14163 appear. The desired matrix has been built above, so we
14164 can use it here. */
14165 new_vpos = window_box_height (w) / 2;
14166 }
14167
14168 if (!cursor_row_fully_visible_p (w, 0, 0))
14169 {
14170 /* Point does appear, but on a line partly visible at end of window.
14171 Move it back to a fully-visible line. */
14172 new_vpos = window_box_height (w);
14173 }
14174
14175 /* If we need to move point for either of the above reasons,
14176 now actually do it. */
14177 if (new_vpos >= 0)
14178 {
14179 struct glyph_row *row;
14180
14181 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14182 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14183 ++row;
14184
14185 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14186 MATRIX_ROW_START_BYTEPOS (row));
14187
14188 if (w != XWINDOW (selected_window))
14189 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14190 else if (current_buffer == old)
14191 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14192
14193 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14194
14195 /* If we are highlighting the region, then we just changed
14196 the region, so redisplay to show it. */
14197 if (!NILP (Vtransient_mark_mode)
14198 && !NILP (current_buffer->mark_active))
14199 {
14200 clear_glyph_matrix (w->desired_matrix);
14201 if (!try_window (window, startp, 0))
14202 goto need_larger_matrices;
14203 }
14204 }
14205
14206 #if GLYPH_DEBUG
14207 debug_method_add (w, "forced window start");
14208 #endif
14209 goto done;
14210 }
14211
14212 /* Handle case where text has not changed, only point, and it has
14213 not moved off the frame, and we are not retrying after hscroll.
14214 (current_matrix_up_to_date_p is nonzero when retrying.) */
14215 if (current_matrix_up_to_date_p
14216 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14217 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14218 {
14219 switch (rc)
14220 {
14221 case CURSOR_MOVEMENT_SUCCESS:
14222 used_current_matrix_p = 1;
14223 goto done;
14224
14225 case CURSOR_MOVEMENT_MUST_SCROLL:
14226 goto try_to_scroll;
14227
14228 default:
14229 abort ();
14230 }
14231 }
14232 /* If current starting point was originally the beginning of a line
14233 but no longer is, find a new starting point. */
14234 else if (!NILP (w->start_at_line_beg)
14235 && !(CHARPOS (startp) <= BEGV
14236 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14237 {
14238 #if GLYPH_DEBUG
14239 debug_method_add (w, "recenter 1");
14240 #endif
14241 goto recenter;
14242 }
14243
14244 /* Try scrolling with try_window_id. Value is > 0 if update has
14245 been done, it is -1 if we know that the same window start will
14246 not work. It is 0 if unsuccessful for some other reason. */
14247 else if ((tem = try_window_id (w)) != 0)
14248 {
14249 #if GLYPH_DEBUG
14250 debug_method_add (w, "try_window_id %d", tem);
14251 #endif
14252
14253 if (fonts_changed_p)
14254 goto need_larger_matrices;
14255 if (tem > 0)
14256 goto done;
14257
14258 /* Otherwise try_window_id has returned -1 which means that we
14259 don't want the alternative below this comment to execute. */
14260 }
14261 else if (CHARPOS (startp) >= BEGV
14262 && CHARPOS (startp) <= ZV
14263 && PT >= CHARPOS (startp)
14264 && (CHARPOS (startp) < ZV
14265 /* Avoid starting at end of buffer. */
14266 || CHARPOS (startp) == BEGV
14267 || (XFASTINT (w->last_modified) >= MODIFF
14268 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14269 {
14270
14271 /* If first window line is a continuation line, and window start
14272 is inside the modified region, but the first change is before
14273 current window start, we must select a new window start.
14274
14275 However, if this is the result of a down-mouse event (e.g. by
14276 extending the mouse-drag-overlay), we don't want to select a
14277 new window start, since that would change the position under
14278 the mouse, resulting in an unwanted mouse-movement rather
14279 than a simple mouse-click. */
14280 if (NILP (w->start_at_line_beg)
14281 && NILP (do_mouse_tracking)
14282 && CHARPOS (startp) > BEGV
14283 && CHARPOS (startp) > BEG + beg_unchanged
14284 && CHARPOS (startp) <= Z - end_unchanged
14285 /* Even if w->start_at_line_beg is nil, a new window may
14286 start at a line_beg, since that's how set_buffer_window
14287 sets it. So, we need to check the return value of
14288 compute_window_start_on_continuation_line. (See also
14289 bug#197). */
14290 && XMARKER (w->start)->buffer == current_buffer
14291 && compute_window_start_on_continuation_line (w))
14292 {
14293 w->force_start = Qt;
14294 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14295 goto force_start;
14296 }
14297
14298 #if GLYPH_DEBUG
14299 debug_method_add (w, "same window start");
14300 #endif
14301
14302 /* Try to redisplay starting at same place as before.
14303 If point has not moved off frame, accept the results. */
14304 if (!current_matrix_up_to_date_p
14305 /* Don't use try_window_reusing_current_matrix in this case
14306 because a window scroll function can have changed the
14307 buffer. */
14308 || !NILP (Vwindow_scroll_functions)
14309 || MINI_WINDOW_P (w)
14310 || !(used_current_matrix_p
14311 = try_window_reusing_current_matrix (w)))
14312 {
14313 IF_DEBUG (debug_method_add (w, "1"));
14314 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14315 /* -1 means we need to scroll.
14316 0 means we need new matrices, but fonts_changed_p
14317 is set in that case, so we will detect it below. */
14318 goto try_to_scroll;
14319 }
14320
14321 if (fonts_changed_p)
14322 goto need_larger_matrices;
14323
14324 if (w->cursor.vpos >= 0)
14325 {
14326 if (!just_this_one_p
14327 || current_buffer->clip_changed
14328 || BEG_UNCHANGED < CHARPOS (startp))
14329 /* Forget any recorded base line for line number display. */
14330 w->base_line_number = Qnil;
14331
14332 if (!cursor_row_fully_visible_p (w, 1, 0))
14333 {
14334 clear_glyph_matrix (w->desired_matrix);
14335 last_line_misfit = 1;
14336 }
14337 /* Drop through and scroll. */
14338 else
14339 goto done;
14340 }
14341 else
14342 clear_glyph_matrix (w->desired_matrix);
14343 }
14344
14345 try_to_scroll:
14346
14347 w->last_modified = make_number (0);
14348 w->last_overlay_modified = make_number (0);
14349
14350 /* Redisplay the mode line. Select the buffer properly for that. */
14351 if (!update_mode_line)
14352 {
14353 update_mode_line = 1;
14354 w->update_mode_line = Qt;
14355 }
14356
14357 /* Try to scroll by specified few lines. */
14358 if ((scroll_conservatively
14359 || scroll_step
14360 || temp_scroll_step
14361 || NUMBERP (current_buffer->scroll_up_aggressively)
14362 || NUMBERP (current_buffer->scroll_down_aggressively))
14363 && !current_buffer->clip_changed
14364 && CHARPOS (startp) >= BEGV
14365 && CHARPOS (startp) <= ZV)
14366 {
14367 /* The function returns -1 if new fonts were loaded, 1 if
14368 successful, 0 if not successful. */
14369 int rc = try_scrolling (window, just_this_one_p,
14370 scroll_conservatively,
14371 scroll_step,
14372 temp_scroll_step, last_line_misfit);
14373 switch (rc)
14374 {
14375 case SCROLLING_SUCCESS:
14376 goto done;
14377
14378 case SCROLLING_NEED_LARGER_MATRICES:
14379 goto need_larger_matrices;
14380
14381 case SCROLLING_FAILED:
14382 break;
14383
14384 default:
14385 abort ();
14386 }
14387 }
14388
14389 /* Finally, just choose place to start which centers point */
14390
14391 recenter:
14392 if (centering_position < 0)
14393 centering_position = window_box_height (w) / 2;
14394
14395 #if GLYPH_DEBUG
14396 debug_method_add (w, "recenter");
14397 #endif
14398
14399 /* w->vscroll = 0; */
14400
14401 /* Forget any previously recorded base line for line number display. */
14402 if (!buffer_unchanged_p)
14403 w->base_line_number = Qnil;
14404
14405 /* Move backward half the height of the window. */
14406 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14407 it.current_y = it.last_visible_y;
14408 move_it_vertically_backward (&it, centering_position);
14409 xassert (IT_CHARPOS (it) >= BEGV);
14410
14411 /* The function move_it_vertically_backward may move over more
14412 than the specified y-distance. If it->w is small, e.g. a
14413 mini-buffer window, we may end up in front of the window's
14414 display area. Start displaying at the start of the line
14415 containing PT in this case. */
14416 if (it.current_y <= 0)
14417 {
14418 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14419 move_it_vertically_backward (&it, 0);
14420 it.current_y = 0;
14421 }
14422
14423 it.current_x = it.hpos = 0;
14424
14425 /* Set startp here explicitly in case that helps avoid an infinite loop
14426 in case the window-scroll-functions functions get errors. */
14427 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14428
14429 /* Run scroll hooks. */
14430 startp = run_window_scroll_functions (window, it.current.pos);
14431
14432 /* Redisplay the window. */
14433 if (!current_matrix_up_to_date_p
14434 || windows_or_buffers_changed
14435 || cursor_type_changed
14436 /* Don't use try_window_reusing_current_matrix in this case
14437 because it can have changed the buffer. */
14438 || !NILP (Vwindow_scroll_functions)
14439 || !just_this_one_p
14440 || MINI_WINDOW_P (w)
14441 || !(used_current_matrix_p
14442 = try_window_reusing_current_matrix (w)))
14443 try_window (window, startp, 0);
14444
14445 /* If new fonts have been loaded (due to fontsets), give up. We
14446 have to start a new redisplay since we need to re-adjust glyph
14447 matrices. */
14448 if (fonts_changed_p)
14449 goto need_larger_matrices;
14450
14451 /* If cursor did not appear assume that the middle of the window is
14452 in the first line of the window. Do it again with the next line.
14453 (Imagine a window of height 100, displaying two lines of height
14454 60. Moving back 50 from it->last_visible_y will end in the first
14455 line.) */
14456 if (w->cursor.vpos < 0)
14457 {
14458 if (!NILP (w->window_end_valid)
14459 && PT >= Z - XFASTINT (w->window_end_pos))
14460 {
14461 clear_glyph_matrix (w->desired_matrix);
14462 move_it_by_lines (&it, 1, 0);
14463 try_window (window, it.current.pos, 0);
14464 }
14465 else if (PT < IT_CHARPOS (it))
14466 {
14467 clear_glyph_matrix (w->desired_matrix);
14468 move_it_by_lines (&it, -1, 0);
14469 try_window (window, it.current.pos, 0);
14470 }
14471 else
14472 {
14473 /* Not much we can do about it. */
14474 }
14475 }
14476
14477 /* Consider the following case: Window starts at BEGV, there is
14478 invisible, intangible text at BEGV, so that display starts at
14479 some point START > BEGV. It can happen that we are called with
14480 PT somewhere between BEGV and START. Try to handle that case. */
14481 if (w->cursor.vpos < 0)
14482 {
14483 struct glyph_row *row = w->current_matrix->rows;
14484 if (row->mode_line_p)
14485 ++row;
14486 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14487 }
14488
14489 if (!cursor_row_fully_visible_p (w, 0, 0))
14490 {
14491 /* If vscroll is enabled, disable it and try again. */
14492 if (w->vscroll)
14493 {
14494 w->vscroll = 0;
14495 clear_glyph_matrix (w->desired_matrix);
14496 goto recenter;
14497 }
14498
14499 /* If centering point failed to make the whole line visible,
14500 put point at the top instead. That has to make the whole line
14501 visible, if it can be done. */
14502 if (centering_position == 0)
14503 goto done;
14504
14505 clear_glyph_matrix (w->desired_matrix);
14506 centering_position = 0;
14507 goto recenter;
14508 }
14509
14510 done:
14511
14512 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14513 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14514 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14515 ? Qt : Qnil);
14516
14517 /* Display the mode line, if we must. */
14518 if ((update_mode_line
14519 /* If window not full width, must redo its mode line
14520 if (a) the window to its side is being redone and
14521 (b) we do a frame-based redisplay. This is a consequence
14522 of how inverted lines are drawn in frame-based redisplay. */
14523 || (!just_this_one_p
14524 && !FRAME_WINDOW_P (f)
14525 && !WINDOW_FULL_WIDTH_P (w))
14526 /* Line number to display. */
14527 || INTEGERP (w->base_line_pos)
14528 /* Column number is displayed and different from the one displayed. */
14529 || (!NILP (w->column_number_displayed)
14530 && (XFASTINT (w->column_number_displayed)
14531 != (int) current_column ()))) /* iftc */
14532 /* This means that the window has a mode line. */
14533 && (WINDOW_WANTS_MODELINE_P (w)
14534 || WINDOW_WANTS_HEADER_LINE_P (w)))
14535 {
14536 display_mode_lines (w);
14537
14538 /* If mode line height has changed, arrange for a thorough
14539 immediate redisplay using the correct mode line height. */
14540 if (WINDOW_WANTS_MODELINE_P (w)
14541 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14542 {
14543 fonts_changed_p = 1;
14544 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14545 = DESIRED_MODE_LINE_HEIGHT (w);
14546 }
14547
14548 /* If header line height has changed, arrange for a thorough
14549 immediate redisplay using the correct header line height. */
14550 if (WINDOW_WANTS_HEADER_LINE_P (w)
14551 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14552 {
14553 fonts_changed_p = 1;
14554 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14555 = DESIRED_HEADER_LINE_HEIGHT (w);
14556 }
14557
14558 if (fonts_changed_p)
14559 goto need_larger_matrices;
14560 }
14561
14562 if (!line_number_displayed
14563 && !BUFFERP (w->base_line_pos))
14564 {
14565 w->base_line_pos = Qnil;
14566 w->base_line_number = Qnil;
14567 }
14568
14569 finish_menu_bars:
14570
14571 /* When we reach a frame's selected window, redo the frame's menu bar. */
14572 if (update_mode_line
14573 && EQ (FRAME_SELECTED_WINDOW (f), window))
14574 {
14575 int redisplay_menu_p = 0;
14576 int redisplay_tool_bar_p = 0;
14577
14578 if (FRAME_WINDOW_P (f))
14579 {
14580 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14581 || defined (HAVE_NS) || defined (USE_GTK)
14582 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14583 #else
14584 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14585 #endif
14586 }
14587 else
14588 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14589
14590 if (redisplay_menu_p)
14591 display_menu_bar (w);
14592
14593 #ifdef HAVE_WINDOW_SYSTEM
14594 if (FRAME_WINDOW_P (f))
14595 {
14596 #if defined (USE_GTK) || defined (HAVE_NS)
14597 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14598 #else
14599 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14600 && (FRAME_TOOL_BAR_LINES (f) > 0
14601 || !NILP (Vauto_resize_tool_bars));
14602 #endif
14603
14604 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14605 {
14606 ignore_mouse_drag_p = 1;
14607 }
14608 }
14609 #endif
14610 }
14611
14612 #ifdef HAVE_WINDOW_SYSTEM
14613 if (FRAME_WINDOW_P (f)
14614 && update_window_fringes (w, (just_this_one_p
14615 || (!used_current_matrix_p && !overlay_arrow_seen)
14616 || w->pseudo_window_p)))
14617 {
14618 update_begin (f);
14619 BLOCK_INPUT;
14620 if (draw_window_fringes (w, 1))
14621 x_draw_vertical_border (w);
14622 UNBLOCK_INPUT;
14623 update_end (f);
14624 }
14625 #endif /* HAVE_WINDOW_SYSTEM */
14626
14627 /* We go to this label, with fonts_changed_p nonzero,
14628 if it is necessary to try again using larger glyph matrices.
14629 We have to redeem the scroll bar even in this case,
14630 because the loop in redisplay_internal expects that. */
14631 need_larger_matrices:
14632 ;
14633 finish_scroll_bars:
14634
14635 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14636 {
14637 /* Set the thumb's position and size. */
14638 set_vertical_scroll_bar (w);
14639
14640 /* Note that we actually used the scroll bar attached to this
14641 window, so it shouldn't be deleted at the end of redisplay. */
14642 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14643 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14644 }
14645
14646 /* Restore current_buffer and value of point in it. The window
14647 update may have changed the buffer, so first make sure `opoint'
14648 is still valid (Bug#6177). */
14649 if (CHARPOS (opoint) < BEGV)
14650 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14651 else if (CHARPOS (opoint) > ZV)
14652 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14653 else
14654 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14655
14656 set_buffer_internal_1 (old);
14657 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14658 shorter. This can be caused by log truncation in *Messages*. */
14659 if (CHARPOS (lpoint) <= ZV)
14660 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14661
14662 unbind_to (count, Qnil);
14663 }
14664
14665
14666 /* Build the complete desired matrix of WINDOW with a window start
14667 buffer position POS.
14668
14669 Value is 1 if successful. It is zero if fonts were loaded during
14670 redisplay which makes re-adjusting glyph matrices necessary, and -1
14671 if point would appear in the scroll margins.
14672 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14673 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14674 set in FLAGS.) */
14675
14676 int
14677 try_window (Lisp_Object window, struct text_pos pos, int flags)
14678 {
14679 struct window *w = XWINDOW (window);
14680 struct it it;
14681 struct glyph_row *last_text_row = NULL;
14682 struct frame *f = XFRAME (w->frame);
14683
14684 /* Make POS the new window start. */
14685 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14686
14687 /* Mark cursor position as unknown. No overlay arrow seen. */
14688 w->cursor.vpos = -1;
14689 overlay_arrow_seen = 0;
14690
14691 /* Initialize iterator and info to start at POS. */
14692 start_display (&it, w, pos);
14693
14694 /* Display all lines of W. */
14695 while (it.current_y < it.last_visible_y)
14696 {
14697 if (display_line (&it))
14698 last_text_row = it.glyph_row - 1;
14699 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14700 return 0;
14701 }
14702
14703 /* Don't let the cursor end in the scroll margins. */
14704 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14705 && !MINI_WINDOW_P (w))
14706 {
14707 int this_scroll_margin;
14708
14709 if (scroll_margin > 0)
14710 {
14711 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14712 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14713 }
14714 else
14715 this_scroll_margin = 0;
14716
14717 if ((w->cursor.y >= 0 /* not vscrolled */
14718 && w->cursor.y < this_scroll_margin
14719 && CHARPOS (pos) > BEGV
14720 && IT_CHARPOS (it) < ZV)
14721 /* rms: considering make_cursor_line_fully_visible_p here
14722 seems to give wrong results. We don't want to recenter
14723 when the last line is partly visible, we want to allow
14724 that case to be handled in the usual way. */
14725 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14726 {
14727 w->cursor.vpos = -1;
14728 clear_glyph_matrix (w->desired_matrix);
14729 return -1;
14730 }
14731 }
14732
14733 /* If bottom moved off end of frame, change mode line percentage. */
14734 if (XFASTINT (w->window_end_pos) <= 0
14735 && Z != IT_CHARPOS (it))
14736 w->update_mode_line = Qt;
14737
14738 /* Set window_end_pos to the offset of the last character displayed
14739 on the window from the end of current_buffer. Set
14740 window_end_vpos to its row number. */
14741 if (last_text_row)
14742 {
14743 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14744 w->window_end_bytepos
14745 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14746 w->window_end_pos
14747 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14748 w->window_end_vpos
14749 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14750 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14751 ->displays_text_p);
14752 }
14753 else
14754 {
14755 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14756 w->window_end_pos = make_number (Z - ZV);
14757 w->window_end_vpos = make_number (0);
14758 }
14759
14760 /* But that is not valid info until redisplay finishes. */
14761 w->window_end_valid = Qnil;
14762 return 1;
14763 }
14764
14765
14766 \f
14767 /************************************************************************
14768 Window redisplay reusing current matrix when buffer has not changed
14769 ************************************************************************/
14770
14771 /* Try redisplay of window W showing an unchanged buffer with a
14772 different window start than the last time it was displayed by
14773 reusing its current matrix. Value is non-zero if successful.
14774 W->start is the new window start. */
14775
14776 static int
14777 try_window_reusing_current_matrix (struct window *w)
14778 {
14779 struct frame *f = XFRAME (w->frame);
14780 struct glyph_row *row, *bottom_row;
14781 struct it it;
14782 struct run run;
14783 struct text_pos start, new_start;
14784 int nrows_scrolled, i;
14785 struct glyph_row *last_text_row;
14786 struct glyph_row *last_reused_text_row;
14787 struct glyph_row *start_row;
14788 int start_vpos, min_y, max_y;
14789
14790 #if GLYPH_DEBUG
14791 if (inhibit_try_window_reusing)
14792 return 0;
14793 #endif
14794
14795 if (/* This function doesn't handle terminal frames. */
14796 !FRAME_WINDOW_P (f)
14797 /* Don't try to reuse the display if windows have been split
14798 or such. */
14799 || windows_or_buffers_changed
14800 || cursor_type_changed)
14801 return 0;
14802
14803 /* Can't do this if region may have changed. */
14804 if ((!NILP (Vtransient_mark_mode)
14805 && !NILP (current_buffer->mark_active))
14806 || !NILP (w->region_showing)
14807 || !NILP (Vshow_trailing_whitespace))
14808 return 0;
14809
14810 /* If top-line visibility has changed, give up. */
14811 if (WINDOW_WANTS_HEADER_LINE_P (w)
14812 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14813 return 0;
14814
14815 /* Give up if old or new display is scrolled vertically. We could
14816 make this function handle this, but right now it doesn't. */
14817 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14818 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14819 return 0;
14820
14821 /* The variable new_start now holds the new window start. The old
14822 start `start' can be determined from the current matrix. */
14823 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14824 start = start_row->minpos;
14825 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14826
14827 /* Clear the desired matrix for the display below. */
14828 clear_glyph_matrix (w->desired_matrix);
14829
14830 if (CHARPOS (new_start) <= CHARPOS (start))
14831 {
14832 int first_row_y;
14833
14834 /* Don't use this method if the display starts with an ellipsis
14835 displayed for invisible text. It's not easy to handle that case
14836 below, and it's certainly not worth the effort since this is
14837 not a frequent case. */
14838 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14839 return 0;
14840
14841 IF_DEBUG (debug_method_add (w, "twu1"));
14842
14843 /* Display up to a row that can be reused. The variable
14844 last_text_row is set to the last row displayed that displays
14845 text. Note that it.vpos == 0 if or if not there is a
14846 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14847 start_display (&it, w, new_start);
14848 first_row_y = it.current_y;
14849 w->cursor.vpos = -1;
14850 last_text_row = last_reused_text_row = NULL;
14851
14852 while (it.current_y < it.last_visible_y
14853 && !fonts_changed_p)
14854 {
14855 /* If we have reached into the characters in the START row,
14856 that means the line boundaries have changed. So we
14857 can't start copying with the row START. Maybe it will
14858 work to start copying with the following row. */
14859 while (IT_CHARPOS (it) > CHARPOS (start))
14860 {
14861 /* Advance to the next row as the "start". */
14862 start_row++;
14863 start = start_row->minpos;
14864 /* If there are no more rows to try, or just one, give up. */
14865 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14866 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14867 || CHARPOS (start) == ZV)
14868 {
14869 clear_glyph_matrix (w->desired_matrix);
14870 return 0;
14871 }
14872
14873 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14874 }
14875 /* If we have reached alignment,
14876 we can copy the rest of the rows. */
14877 if (IT_CHARPOS (it) == CHARPOS (start))
14878 break;
14879
14880 if (display_line (&it))
14881 last_text_row = it.glyph_row - 1;
14882 }
14883
14884 /* A value of current_y < last_visible_y means that we stopped
14885 at the previous window start, which in turn means that we
14886 have at least one reusable row. */
14887 if (it.current_y < it.last_visible_y)
14888 {
14889 /* IT.vpos always starts from 0; it counts text lines. */
14890 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14891
14892 /* Find PT if not already found in the lines displayed. */
14893 if (w->cursor.vpos < 0)
14894 {
14895 int dy = it.current_y - start_row->y;
14896
14897 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14898 row = row_containing_pos (w, PT, row, NULL, dy);
14899 if (row)
14900 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14901 dy, nrows_scrolled);
14902 else
14903 {
14904 clear_glyph_matrix (w->desired_matrix);
14905 return 0;
14906 }
14907 }
14908
14909 /* Scroll the display. Do it before the current matrix is
14910 changed. The problem here is that update has not yet
14911 run, i.e. part of the current matrix is not up to date.
14912 scroll_run_hook will clear the cursor, and use the
14913 current matrix to get the height of the row the cursor is
14914 in. */
14915 run.current_y = start_row->y;
14916 run.desired_y = it.current_y;
14917 run.height = it.last_visible_y - it.current_y;
14918
14919 if (run.height > 0 && run.current_y != run.desired_y)
14920 {
14921 update_begin (f);
14922 FRAME_RIF (f)->update_window_begin_hook (w);
14923 FRAME_RIF (f)->clear_window_mouse_face (w);
14924 FRAME_RIF (f)->scroll_run_hook (w, &run);
14925 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14926 update_end (f);
14927 }
14928
14929 /* Shift current matrix down by nrows_scrolled lines. */
14930 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14931 rotate_matrix (w->current_matrix,
14932 start_vpos,
14933 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14934 nrows_scrolled);
14935
14936 /* Disable lines that must be updated. */
14937 for (i = 0; i < nrows_scrolled; ++i)
14938 (start_row + i)->enabled_p = 0;
14939
14940 /* Re-compute Y positions. */
14941 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14942 max_y = it.last_visible_y;
14943 for (row = start_row + nrows_scrolled;
14944 row < bottom_row;
14945 ++row)
14946 {
14947 row->y = it.current_y;
14948 row->visible_height = row->height;
14949
14950 if (row->y < min_y)
14951 row->visible_height -= min_y - row->y;
14952 if (row->y + row->height > max_y)
14953 row->visible_height -= row->y + row->height - max_y;
14954 row->redraw_fringe_bitmaps_p = 1;
14955
14956 it.current_y += row->height;
14957
14958 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14959 last_reused_text_row = row;
14960 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14961 break;
14962 }
14963
14964 /* Disable lines in the current matrix which are now
14965 below the window. */
14966 for (++row; row < bottom_row; ++row)
14967 row->enabled_p = row->mode_line_p = 0;
14968 }
14969
14970 /* Update window_end_pos etc.; last_reused_text_row is the last
14971 reused row from the current matrix containing text, if any.
14972 The value of last_text_row is the last displayed line
14973 containing text. */
14974 if (last_reused_text_row)
14975 {
14976 w->window_end_bytepos
14977 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14978 w->window_end_pos
14979 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14980 w->window_end_vpos
14981 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14982 w->current_matrix));
14983 }
14984 else if (last_text_row)
14985 {
14986 w->window_end_bytepos
14987 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14988 w->window_end_pos
14989 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14990 w->window_end_vpos
14991 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14992 }
14993 else
14994 {
14995 /* This window must be completely empty. */
14996 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14997 w->window_end_pos = make_number (Z - ZV);
14998 w->window_end_vpos = make_number (0);
14999 }
15000 w->window_end_valid = Qnil;
15001
15002 /* Update hint: don't try scrolling again in update_window. */
15003 w->desired_matrix->no_scrolling_p = 1;
15004
15005 #if GLYPH_DEBUG
15006 debug_method_add (w, "try_window_reusing_current_matrix 1");
15007 #endif
15008 return 1;
15009 }
15010 else if (CHARPOS (new_start) > CHARPOS (start))
15011 {
15012 struct glyph_row *pt_row, *row;
15013 struct glyph_row *first_reusable_row;
15014 struct glyph_row *first_row_to_display;
15015 int dy;
15016 int yb = window_text_bottom_y (w);
15017
15018 /* Find the row starting at new_start, if there is one. Don't
15019 reuse a partially visible line at the end. */
15020 first_reusable_row = start_row;
15021 while (first_reusable_row->enabled_p
15022 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15023 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15024 < CHARPOS (new_start)))
15025 ++first_reusable_row;
15026
15027 /* Give up if there is no row to reuse. */
15028 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15029 || !first_reusable_row->enabled_p
15030 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15031 != CHARPOS (new_start)))
15032 return 0;
15033
15034 /* We can reuse fully visible rows beginning with
15035 first_reusable_row to the end of the window. Set
15036 first_row_to_display to the first row that cannot be reused.
15037 Set pt_row to the row containing point, if there is any. */
15038 pt_row = NULL;
15039 for (first_row_to_display = first_reusable_row;
15040 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15041 ++first_row_to_display)
15042 {
15043 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15044 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15045 pt_row = first_row_to_display;
15046 }
15047
15048 /* Start displaying at the start of first_row_to_display. */
15049 xassert (first_row_to_display->y < yb);
15050 init_to_row_start (&it, w, first_row_to_display);
15051
15052 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15053 - start_vpos);
15054 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15055 - nrows_scrolled);
15056 it.current_y = (first_row_to_display->y - first_reusable_row->y
15057 + WINDOW_HEADER_LINE_HEIGHT (w));
15058
15059 /* Display lines beginning with first_row_to_display in the
15060 desired matrix. Set last_text_row to the last row displayed
15061 that displays text. */
15062 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15063 if (pt_row == NULL)
15064 w->cursor.vpos = -1;
15065 last_text_row = NULL;
15066 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15067 if (display_line (&it))
15068 last_text_row = it.glyph_row - 1;
15069
15070 /* If point is in a reused row, adjust y and vpos of the cursor
15071 position. */
15072 if (pt_row)
15073 {
15074 w->cursor.vpos -= nrows_scrolled;
15075 w->cursor.y -= first_reusable_row->y - start_row->y;
15076 }
15077
15078 /* Give up if point isn't in a row displayed or reused. (This
15079 also handles the case where w->cursor.vpos < nrows_scrolled
15080 after the calls to display_line, which can happen with scroll
15081 margins. See bug#1295.) */
15082 if (w->cursor.vpos < 0)
15083 {
15084 clear_glyph_matrix (w->desired_matrix);
15085 return 0;
15086 }
15087
15088 /* Scroll the display. */
15089 run.current_y = first_reusable_row->y;
15090 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15091 run.height = it.last_visible_y - run.current_y;
15092 dy = run.current_y - run.desired_y;
15093
15094 if (run.height)
15095 {
15096 update_begin (f);
15097 FRAME_RIF (f)->update_window_begin_hook (w);
15098 FRAME_RIF (f)->clear_window_mouse_face (w);
15099 FRAME_RIF (f)->scroll_run_hook (w, &run);
15100 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15101 update_end (f);
15102 }
15103
15104 /* Adjust Y positions of reused rows. */
15105 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15106 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15107 max_y = it.last_visible_y;
15108 for (row = first_reusable_row; row < first_row_to_display; ++row)
15109 {
15110 row->y -= dy;
15111 row->visible_height = row->height;
15112 if (row->y < min_y)
15113 row->visible_height -= min_y - row->y;
15114 if (row->y + row->height > max_y)
15115 row->visible_height -= row->y + row->height - max_y;
15116 row->redraw_fringe_bitmaps_p = 1;
15117 }
15118
15119 /* Scroll the current matrix. */
15120 xassert (nrows_scrolled > 0);
15121 rotate_matrix (w->current_matrix,
15122 start_vpos,
15123 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15124 -nrows_scrolled);
15125
15126 /* Disable rows not reused. */
15127 for (row -= nrows_scrolled; row < bottom_row; ++row)
15128 row->enabled_p = 0;
15129
15130 /* Point may have moved to a different line, so we cannot assume that
15131 the previous cursor position is valid; locate the correct row. */
15132 if (pt_row)
15133 {
15134 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15135 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15136 row++)
15137 {
15138 w->cursor.vpos++;
15139 w->cursor.y = row->y;
15140 }
15141 if (row < bottom_row)
15142 {
15143 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15144 struct glyph *end = glyph + row->used[TEXT_AREA];
15145
15146 /* Can't use this optimization with bidi-reordered glyph
15147 rows, unless cursor is already at point. */
15148 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15149 {
15150 if (!(w->cursor.hpos >= 0
15151 && w->cursor.hpos < row->used[TEXT_AREA]
15152 && BUFFERP (glyph->object)
15153 && glyph->charpos == PT))
15154 return 0;
15155 }
15156 else
15157 for (; glyph < end
15158 && (!BUFFERP (glyph->object)
15159 || glyph->charpos < PT);
15160 glyph++)
15161 {
15162 w->cursor.hpos++;
15163 w->cursor.x += glyph->pixel_width;
15164 }
15165 }
15166 }
15167
15168 /* Adjust window end. A null value of last_text_row means that
15169 the window end is in reused rows which in turn means that
15170 only its vpos can have changed. */
15171 if (last_text_row)
15172 {
15173 w->window_end_bytepos
15174 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15175 w->window_end_pos
15176 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15177 w->window_end_vpos
15178 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15179 }
15180 else
15181 {
15182 w->window_end_vpos
15183 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15184 }
15185
15186 w->window_end_valid = Qnil;
15187 w->desired_matrix->no_scrolling_p = 1;
15188
15189 #if GLYPH_DEBUG
15190 debug_method_add (w, "try_window_reusing_current_matrix 2");
15191 #endif
15192 return 1;
15193 }
15194
15195 return 0;
15196 }
15197
15198
15199 \f
15200 /************************************************************************
15201 Window redisplay reusing current matrix when buffer has changed
15202 ************************************************************************/
15203
15204 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15205 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15206 EMACS_INT *, EMACS_INT *);
15207 static struct glyph_row *
15208 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15209 struct glyph_row *);
15210
15211
15212 /* Return the last row in MATRIX displaying text. If row START is
15213 non-null, start searching with that row. IT gives the dimensions
15214 of the display. Value is null if matrix is empty; otherwise it is
15215 a pointer to the row found. */
15216
15217 static struct glyph_row *
15218 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15219 struct glyph_row *start)
15220 {
15221 struct glyph_row *row, *row_found;
15222
15223 /* Set row_found to the last row in IT->w's current matrix
15224 displaying text. The loop looks funny but think of partially
15225 visible lines. */
15226 row_found = NULL;
15227 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15228 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15229 {
15230 xassert (row->enabled_p);
15231 row_found = row;
15232 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15233 break;
15234 ++row;
15235 }
15236
15237 return row_found;
15238 }
15239
15240
15241 /* Return the last row in the current matrix of W that is not affected
15242 by changes at the start of current_buffer that occurred since W's
15243 current matrix was built. Value is null if no such row exists.
15244
15245 BEG_UNCHANGED us the number of characters unchanged at the start of
15246 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15247 first changed character in current_buffer. Characters at positions <
15248 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15249 when the current matrix was built. */
15250
15251 static struct glyph_row *
15252 find_last_unchanged_at_beg_row (struct window *w)
15253 {
15254 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15255 struct glyph_row *row;
15256 struct glyph_row *row_found = NULL;
15257 int yb = window_text_bottom_y (w);
15258
15259 /* Find the last row displaying unchanged text. */
15260 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15261 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15262 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15263 ++row)
15264 {
15265 if (/* If row ends before first_changed_pos, it is unchanged,
15266 except in some case. */
15267 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15268 /* When row ends in ZV and we write at ZV it is not
15269 unchanged. */
15270 && !row->ends_at_zv_p
15271 /* When first_changed_pos is the end of a continued line,
15272 row is not unchanged because it may be no longer
15273 continued. */
15274 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15275 && (row->continued_p
15276 || row->exact_window_width_line_p)))
15277 row_found = row;
15278
15279 /* Stop if last visible row. */
15280 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15281 break;
15282 }
15283
15284 return row_found;
15285 }
15286
15287
15288 /* Find the first glyph row in the current matrix of W that is not
15289 affected by changes at the end of current_buffer since the
15290 time W's current matrix was built.
15291
15292 Return in *DELTA the number of chars by which buffer positions in
15293 unchanged text at the end of current_buffer must be adjusted.
15294
15295 Return in *DELTA_BYTES the corresponding number of bytes.
15296
15297 Value is null if no such row exists, i.e. all rows are affected by
15298 changes. */
15299
15300 static struct glyph_row *
15301 find_first_unchanged_at_end_row (struct window *w,
15302 EMACS_INT *delta, EMACS_INT *delta_bytes)
15303 {
15304 struct glyph_row *row;
15305 struct glyph_row *row_found = NULL;
15306
15307 *delta = *delta_bytes = 0;
15308
15309 /* Display must not have been paused, otherwise the current matrix
15310 is not up to date. */
15311 eassert (!NILP (w->window_end_valid));
15312
15313 /* A value of window_end_pos >= END_UNCHANGED means that the window
15314 end is in the range of changed text. If so, there is no
15315 unchanged row at the end of W's current matrix. */
15316 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15317 return NULL;
15318
15319 /* Set row to the last row in W's current matrix displaying text. */
15320 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15321
15322 /* If matrix is entirely empty, no unchanged row exists. */
15323 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15324 {
15325 /* The value of row is the last glyph row in the matrix having a
15326 meaningful buffer position in it. The end position of row
15327 corresponds to window_end_pos. This allows us to translate
15328 buffer positions in the current matrix to current buffer
15329 positions for characters not in changed text. */
15330 EMACS_INT Z_old =
15331 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15332 EMACS_INT Z_BYTE_old =
15333 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15334 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15335 struct glyph_row *first_text_row
15336 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15337
15338 *delta = Z - Z_old;
15339 *delta_bytes = Z_BYTE - Z_BYTE_old;
15340
15341 /* Set last_unchanged_pos to the buffer position of the last
15342 character in the buffer that has not been changed. Z is the
15343 index + 1 of the last character in current_buffer, i.e. by
15344 subtracting END_UNCHANGED we get the index of the last
15345 unchanged character, and we have to add BEG to get its buffer
15346 position. */
15347 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15348 last_unchanged_pos_old = last_unchanged_pos - *delta;
15349
15350 /* Search backward from ROW for a row displaying a line that
15351 starts at a minimum position >= last_unchanged_pos_old. */
15352 for (; row > first_text_row; --row)
15353 {
15354 /* This used to abort, but it can happen.
15355 It is ok to just stop the search instead here. KFS. */
15356 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15357 break;
15358
15359 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15360 row_found = row;
15361 }
15362 }
15363
15364 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15365
15366 return row_found;
15367 }
15368
15369
15370 /* Make sure that glyph rows in the current matrix of window W
15371 reference the same glyph memory as corresponding rows in the
15372 frame's frame matrix. This function is called after scrolling W's
15373 current matrix on a terminal frame in try_window_id and
15374 try_window_reusing_current_matrix. */
15375
15376 static void
15377 sync_frame_with_window_matrix_rows (struct window *w)
15378 {
15379 struct frame *f = XFRAME (w->frame);
15380 struct glyph_row *window_row, *window_row_end, *frame_row;
15381
15382 /* Preconditions: W must be a leaf window and full-width. Its frame
15383 must have a frame matrix. */
15384 xassert (NILP (w->hchild) && NILP (w->vchild));
15385 xassert (WINDOW_FULL_WIDTH_P (w));
15386 xassert (!FRAME_WINDOW_P (f));
15387
15388 /* If W is a full-width window, glyph pointers in W's current matrix
15389 have, by definition, to be the same as glyph pointers in the
15390 corresponding frame matrix. Note that frame matrices have no
15391 marginal areas (see build_frame_matrix). */
15392 window_row = w->current_matrix->rows;
15393 window_row_end = window_row + w->current_matrix->nrows;
15394 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15395 while (window_row < window_row_end)
15396 {
15397 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15398 struct glyph *end = window_row->glyphs[LAST_AREA];
15399
15400 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15401 frame_row->glyphs[TEXT_AREA] = start;
15402 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15403 frame_row->glyphs[LAST_AREA] = end;
15404
15405 /* Disable frame rows whose corresponding window rows have
15406 been disabled in try_window_id. */
15407 if (!window_row->enabled_p)
15408 frame_row->enabled_p = 0;
15409
15410 ++window_row, ++frame_row;
15411 }
15412 }
15413
15414
15415 /* Find the glyph row in window W containing CHARPOS. Consider all
15416 rows between START and END (not inclusive). END null means search
15417 all rows to the end of the display area of W. Value is the row
15418 containing CHARPOS or null. */
15419
15420 struct glyph_row *
15421 row_containing_pos (struct window *w, EMACS_INT charpos,
15422 struct glyph_row *start, struct glyph_row *end, int dy)
15423 {
15424 struct glyph_row *row = start;
15425 struct glyph_row *best_row = NULL;
15426 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15427 int last_y;
15428
15429 /* If we happen to start on a header-line, skip that. */
15430 if (row->mode_line_p)
15431 ++row;
15432
15433 if ((end && row >= end) || !row->enabled_p)
15434 return NULL;
15435
15436 last_y = window_text_bottom_y (w) - dy;
15437
15438 while (1)
15439 {
15440 /* Give up if we have gone too far. */
15441 if (end && row >= end)
15442 return NULL;
15443 /* This formerly returned if they were equal.
15444 I think that both quantities are of a "last plus one" type;
15445 if so, when they are equal, the row is within the screen. -- rms. */
15446 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15447 return NULL;
15448
15449 /* If it is in this row, return this row. */
15450 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15451 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15452 /* The end position of a row equals the start
15453 position of the next row. If CHARPOS is there, we
15454 would rather display it in the next line, except
15455 when this line ends in ZV. */
15456 && !row->ends_at_zv_p
15457 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15458 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15459 {
15460 struct glyph *g;
15461
15462 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15463 || (!best_row && !row->continued_p))
15464 return row;
15465 /* In bidi-reordered rows, there could be several rows
15466 occluding point, all of them belonging to the same
15467 continued line. We need to find the row which fits
15468 CHARPOS the best. */
15469 for (g = row->glyphs[TEXT_AREA];
15470 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15471 g++)
15472 {
15473 if (!STRINGP (g->object))
15474 {
15475 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15476 {
15477 mindif = eabs (g->charpos - charpos);
15478 best_row = row;
15479 /* Exact match always wins. */
15480 if (mindif == 0)
15481 return best_row;
15482 }
15483 }
15484 }
15485 }
15486 else if (best_row && !row->continued_p)
15487 return best_row;
15488 ++row;
15489 }
15490 }
15491
15492
15493 /* Try to redisplay window W by reusing its existing display. W's
15494 current matrix must be up to date when this function is called,
15495 i.e. window_end_valid must not be nil.
15496
15497 Value is
15498
15499 1 if display has been updated
15500 0 if otherwise unsuccessful
15501 -1 if redisplay with same window start is known not to succeed
15502
15503 The following steps are performed:
15504
15505 1. Find the last row in the current matrix of W that is not
15506 affected by changes at the start of current_buffer. If no such row
15507 is found, give up.
15508
15509 2. Find the first row in W's current matrix that is not affected by
15510 changes at the end of current_buffer. Maybe there is no such row.
15511
15512 3. Display lines beginning with the row + 1 found in step 1 to the
15513 row found in step 2 or, if step 2 didn't find a row, to the end of
15514 the window.
15515
15516 4. If cursor is not known to appear on the window, give up.
15517
15518 5. If display stopped at the row found in step 2, scroll the
15519 display and current matrix as needed.
15520
15521 6. Maybe display some lines at the end of W, if we must. This can
15522 happen under various circumstances, like a partially visible line
15523 becoming fully visible, or because newly displayed lines are displayed
15524 in smaller font sizes.
15525
15526 7. Update W's window end information. */
15527
15528 static int
15529 try_window_id (struct window *w)
15530 {
15531 struct frame *f = XFRAME (w->frame);
15532 struct glyph_matrix *current_matrix = w->current_matrix;
15533 struct glyph_matrix *desired_matrix = w->desired_matrix;
15534 struct glyph_row *last_unchanged_at_beg_row;
15535 struct glyph_row *first_unchanged_at_end_row;
15536 struct glyph_row *row;
15537 struct glyph_row *bottom_row;
15538 int bottom_vpos;
15539 struct it it;
15540 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15541 int dvpos, dy;
15542 struct text_pos start_pos;
15543 struct run run;
15544 int first_unchanged_at_end_vpos = 0;
15545 struct glyph_row *last_text_row, *last_text_row_at_end;
15546 struct text_pos start;
15547 EMACS_INT first_changed_charpos, last_changed_charpos;
15548
15549 #if GLYPH_DEBUG
15550 if (inhibit_try_window_id)
15551 return 0;
15552 #endif
15553
15554 /* This is handy for debugging. */
15555 #if 0
15556 #define GIVE_UP(X) \
15557 do { \
15558 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15559 return 0; \
15560 } while (0)
15561 #else
15562 #define GIVE_UP(X) return 0
15563 #endif
15564
15565 SET_TEXT_POS_FROM_MARKER (start, w->start);
15566
15567 /* Don't use this for mini-windows because these can show
15568 messages and mini-buffers, and we don't handle that here. */
15569 if (MINI_WINDOW_P (w))
15570 GIVE_UP (1);
15571
15572 /* This flag is used to prevent redisplay optimizations. */
15573 if (windows_or_buffers_changed || cursor_type_changed)
15574 GIVE_UP (2);
15575
15576 /* Verify that narrowing has not changed.
15577 Also verify that we were not told to prevent redisplay optimizations.
15578 It would be nice to further
15579 reduce the number of cases where this prevents try_window_id. */
15580 if (current_buffer->clip_changed
15581 || current_buffer->prevent_redisplay_optimizations_p)
15582 GIVE_UP (3);
15583
15584 /* Window must either use window-based redisplay or be full width. */
15585 if (!FRAME_WINDOW_P (f)
15586 && (!FRAME_LINE_INS_DEL_OK (f)
15587 || !WINDOW_FULL_WIDTH_P (w)))
15588 GIVE_UP (4);
15589
15590 /* Give up if point is known NOT to appear in W. */
15591 if (PT < CHARPOS (start))
15592 GIVE_UP (5);
15593
15594 /* Another way to prevent redisplay optimizations. */
15595 if (XFASTINT (w->last_modified) == 0)
15596 GIVE_UP (6);
15597
15598 /* Verify that window is not hscrolled. */
15599 if (XFASTINT (w->hscroll) != 0)
15600 GIVE_UP (7);
15601
15602 /* Verify that display wasn't paused. */
15603 if (NILP (w->window_end_valid))
15604 GIVE_UP (8);
15605
15606 /* Can't use this if highlighting a region because a cursor movement
15607 will do more than just set the cursor. */
15608 if (!NILP (Vtransient_mark_mode)
15609 && !NILP (current_buffer->mark_active))
15610 GIVE_UP (9);
15611
15612 /* Likewise if highlighting trailing whitespace. */
15613 if (!NILP (Vshow_trailing_whitespace))
15614 GIVE_UP (11);
15615
15616 /* Likewise if showing a region. */
15617 if (!NILP (w->region_showing))
15618 GIVE_UP (10);
15619
15620 /* Can't use this if overlay arrow position and/or string have
15621 changed. */
15622 if (overlay_arrows_changed_p ())
15623 GIVE_UP (12);
15624
15625 /* When word-wrap is on, adding a space to the first word of a
15626 wrapped line can change the wrap position, altering the line
15627 above it. It might be worthwhile to handle this more
15628 intelligently, but for now just redisplay from scratch. */
15629 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15630 GIVE_UP (21);
15631
15632 /* Under bidi reordering, adding or deleting a character in the
15633 beginning of a paragraph, before the first strong directional
15634 character, can change the base direction of the paragraph (unless
15635 the buffer specifies a fixed paragraph direction), which will
15636 require to redisplay the whole paragraph. It might be worthwhile
15637 to find the paragraph limits and widen the range of redisplayed
15638 lines to that, but for now just give up this optimization and
15639 redisplay from scratch. */
15640 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15641 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15642 GIVE_UP (22);
15643
15644 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15645 only if buffer has really changed. The reason is that the gap is
15646 initially at Z for freshly visited files. The code below would
15647 set end_unchanged to 0 in that case. */
15648 if (MODIFF > SAVE_MODIFF
15649 /* This seems to happen sometimes after saving a buffer. */
15650 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15651 {
15652 if (GPT - BEG < BEG_UNCHANGED)
15653 BEG_UNCHANGED = GPT - BEG;
15654 if (Z - GPT < END_UNCHANGED)
15655 END_UNCHANGED = Z - GPT;
15656 }
15657
15658 /* The position of the first and last character that has been changed. */
15659 first_changed_charpos = BEG + BEG_UNCHANGED;
15660 last_changed_charpos = Z - END_UNCHANGED;
15661
15662 /* If window starts after a line end, and the last change is in
15663 front of that newline, then changes don't affect the display.
15664 This case happens with stealth-fontification. Note that although
15665 the display is unchanged, glyph positions in the matrix have to
15666 be adjusted, of course. */
15667 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15668 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15669 && ((last_changed_charpos < CHARPOS (start)
15670 && CHARPOS (start) == BEGV)
15671 || (last_changed_charpos < CHARPOS (start) - 1
15672 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15673 {
15674 EMACS_INT Z_old, delta, Z_BYTE_old, delta_bytes;
15675 struct glyph_row *r0;
15676
15677 /* Compute how many chars/bytes have been added to or removed
15678 from the buffer. */
15679 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15680 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15681 delta = Z - Z_old;
15682 delta_bytes = Z_BYTE - Z_BYTE_old;
15683
15684 /* Give up if PT is not in the window. Note that it already has
15685 been checked at the start of try_window_id that PT is not in
15686 front of the window start. */
15687 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15688 GIVE_UP (13);
15689
15690 /* If window start is unchanged, we can reuse the whole matrix
15691 as is, after adjusting glyph positions. No need to compute
15692 the window end again, since its offset from Z hasn't changed. */
15693 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15694 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15695 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15696 /* PT must not be in a partially visible line. */
15697 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15698 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15699 {
15700 /* Adjust positions in the glyph matrix. */
15701 if (delta || delta_bytes)
15702 {
15703 struct glyph_row *r1
15704 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15705 increment_matrix_positions (w->current_matrix,
15706 MATRIX_ROW_VPOS (r0, current_matrix),
15707 MATRIX_ROW_VPOS (r1, current_matrix),
15708 delta, delta_bytes);
15709 }
15710
15711 /* Set the cursor. */
15712 row = row_containing_pos (w, PT, r0, NULL, 0);
15713 if (row)
15714 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15715 else
15716 abort ();
15717 return 1;
15718 }
15719 }
15720
15721 /* Handle the case that changes are all below what is displayed in
15722 the window, and that PT is in the window. This shortcut cannot
15723 be taken if ZV is visible in the window, and text has been added
15724 there that is visible in the window. */
15725 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15726 /* ZV is not visible in the window, or there are no
15727 changes at ZV, actually. */
15728 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15729 || first_changed_charpos == last_changed_charpos))
15730 {
15731 struct glyph_row *r0;
15732
15733 /* Give up if PT is not in the window. Note that it already has
15734 been checked at the start of try_window_id that PT is not in
15735 front of the window start. */
15736 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15737 GIVE_UP (14);
15738
15739 /* If window start is unchanged, we can reuse the whole matrix
15740 as is, without changing glyph positions since no text has
15741 been added/removed in front of the window end. */
15742 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15743 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15744 /* PT must not be in a partially visible line. */
15745 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15746 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15747 {
15748 /* We have to compute the window end anew since text
15749 could have been added/removed after it. */
15750 w->window_end_pos
15751 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15752 w->window_end_bytepos
15753 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15754
15755 /* Set the cursor. */
15756 row = row_containing_pos (w, PT, r0, NULL, 0);
15757 if (row)
15758 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15759 else
15760 abort ();
15761 return 2;
15762 }
15763 }
15764
15765 /* Give up if window start is in the changed area.
15766
15767 The condition used to read
15768
15769 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15770
15771 but why that was tested escapes me at the moment. */
15772 if (CHARPOS (start) >= first_changed_charpos
15773 && CHARPOS (start) <= last_changed_charpos)
15774 GIVE_UP (15);
15775
15776 /* Check that window start agrees with the start of the first glyph
15777 row in its current matrix. Check this after we know the window
15778 start is not in changed text, otherwise positions would not be
15779 comparable. */
15780 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15781 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15782 GIVE_UP (16);
15783
15784 /* Give up if the window ends in strings. Overlay strings
15785 at the end are difficult to handle, so don't try. */
15786 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15787 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15788 GIVE_UP (20);
15789
15790 /* Compute the position at which we have to start displaying new
15791 lines. Some of the lines at the top of the window might be
15792 reusable because they are not displaying changed text. Find the
15793 last row in W's current matrix not affected by changes at the
15794 start of current_buffer. Value is null if changes start in the
15795 first line of window. */
15796 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15797 if (last_unchanged_at_beg_row)
15798 {
15799 /* Avoid starting to display in the moddle of a character, a TAB
15800 for instance. This is easier than to set up the iterator
15801 exactly, and it's not a frequent case, so the additional
15802 effort wouldn't really pay off. */
15803 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15804 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15805 && last_unchanged_at_beg_row > w->current_matrix->rows)
15806 --last_unchanged_at_beg_row;
15807
15808 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15809 GIVE_UP (17);
15810
15811 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15812 GIVE_UP (18);
15813 start_pos = it.current.pos;
15814
15815 /* Start displaying new lines in the desired matrix at the same
15816 vpos we would use in the current matrix, i.e. below
15817 last_unchanged_at_beg_row. */
15818 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15819 current_matrix);
15820 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15821 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15822
15823 xassert (it.hpos == 0 && it.current_x == 0);
15824 }
15825 else
15826 {
15827 /* There are no reusable lines at the start of the window.
15828 Start displaying in the first text line. */
15829 start_display (&it, w, start);
15830 it.vpos = it.first_vpos;
15831 start_pos = it.current.pos;
15832 }
15833
15834 /* Find the first row that is not affected by changes at the end of
15835 the buffer. Value will be null if there is no unchanged row, in
15836 which case we must redisplay to the end of the window. delta
15837 will be set to the value by which buffer positions beginning with
15838 first_unchanged_at_end_row have to be adjusted due to text
15839 changes. */
15840 first_unchanged_at_end_row
15841 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15842 IF_DEBUG (debug_delta = delta);
15843 IF_DEBUG (debug_delta_bytes = delta_bytes);
15844
15845 /* Set stop_pos to the buffer position up to which we will have to
15846 display new lines. If first_unchanged_at_end_row != NULL, this
15847 is the buffer position of the start of the line displayed in that
15848 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15849 that we don't stop at a buffer position. */
15850 stop_pos = 0;
15851 if (first_unchanged_at_end_row)
15852 {
15853 xassert (last_unchanged_at_beg_row == NULL
15854 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15855
15856 /* If this is a continuation line, move forward to the next one
15857 that isn't. Changes in lines above affect this line.
15858 Caution: this may move first_unchanged_at_end_row to a row
15859 not displaying text. */
15860 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15861 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15862 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15863 < it.last_visible_y))
15864 ++first_unchanged_at_end_row;
15865
15866 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15867 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15868 >= it.last_visible_y))
15869 first_unchanged_at_end_row = NULL;
15870 else
15871 {
15872 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15873 + delta);
15874 first_unchanged_at_end_vpos
15875 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15876 xassert (stop_pos >= Z - END_UNCHANGED);
15877 }
15878 }
15879 else if (last_unchanged_at_beg_row == NULL)
15880 GIVE_UP (19);
15881
15882
15883 #if GLYPH_DEBUG
15884
15885 /* Either there is no unchanged row at the end, or the one we have
15886 now displays text. This is a necessary condition for the window
15887 end pos calculation at the end of this function. */
15888 xassert (first_unchanged_at_end_row == NULL
15889 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15890
15891 debug_last_unchanged_at_beg_vpos
15892 = (last_unchanged_at_beg_row
15893 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15894 : -1);
15895 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15896
15897 #endif /* GLYPH_DEBUG != 0 */
15898
15899
15900 /* Display new lines. Set last_text_row to the last new line
15901 displayed which has text on it, i.e. might end up as being the
15902 line where the window_end_vpos is. */
15903 w->cursor.vpos = -1;
15904 last_text_row = NULL;
15905 overlay_arrow_seen = 0;
15906 while (it.current_y < it.last_visible_y
15907 && !fonts_changed_p
15908 && (first_unchanged_at_end_row == NULL
15909 || IT_CHARPOS (it) < stop_pos))
15910 {
15911 if (display_line (&it))
15912 last_text_row = it.glyph_row - 1;
15913 }
15914
15915 if (fonts_changed_p)
15916 return -1;
15917
15918
15919 /* Compute differences in buffer positions, y-positions etc. for
15920 lines reused at the bottom of the window. Compute what we can
15921 scroll. */
15922 if (first_unchanged_at_end_row
15923 /* No lines reused because we displayed everything up to the
15924 bottom of the window. */
15925 && it.current_y < it.last_visible_y)
15926 {
15927 dvpos = (it.vpos
15928 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15929 current_matrix));
15930 dy = it.current_y - first_unchanged_at_end_row->y;
15931 run.current_y = first_unchanged_at_end_row->y;
15932 run.desired_y = run.current_y + dy;
15933 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15934 }
15935 else
15936 {
15937 delta = delta_bytes = dvpos = dy
15938 = run.current_y = run.desired_y = run.height = 0;
15939 first_unchanged_at_end_row = NULL;
15940 }
15941 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15942
15943
15944 /* Find the cursor if not already found. We have to decide whether
15945 PT will appear on this window (it sometimes doesn't, but this is
15946 not a very frequent case.) This decision has to be made before
15947 the current matrix is altered. A value of cursor.vpos < 0 means
15948 that PT is either in one of the lines beginning at
15949 first_unchanged_at_end_row or below the window. Don't care for
15950 lines that might be displayed later at the window end; as
15951 mentioned, this is not a frequent case. */
15952 if (w->cursor.vpos < 0)
15953 {
15954 /* Cursor in unchanged rows at the top? */
15955 if (PT < CHARPOS (start_pos)
15956 && last_unchanged_at_beg_row)
15957 {
15958 row = row_containing_pos (w, PT,
15959 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15960 last_unchanged_at_beg_row + 1, 0);
15961 if (row)
15962 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15963 }
15964
15965 /* Start from first_unchanged_at_end_row looking for PT. */
15966 else if (first_unchanged_at_end_row)
15967 {
15968 row = row_containing_pos (w, PT - delta,
15969 first_unchanged_at_end_row, NULL, 0);
15970 if (row)
15971 set_cursor_from_row (w, row, w->current_matrix, delta,
15972 delta_bytes, dy, dvpos);
15973 }
15974
15975 /* Give up if cursor was not found. */
15976 if (w->cursor.vpos < 0)
15977 {
15978 clear_glyph_matrix (w->desired_matrix);
15979 return -1;
15980 }
15981 }
15982
15983 /* Don't let the cursor end in the scroll margins. */
15984 {
15985 int this_scroll_margin, cursor_height;
15986
15987 this_scroll_margin = max (0, scroll_margin);
15988 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15989 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15990 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15991
15992 if ((w->cursor.y < this_scroll_margin
15993 && CHARPOS (start) > BEGV)
15994 /* Old redisplay didn't take scroll margin into account at the bottom,
15995 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15996 || (w->cursor.y + (make_cursor_line_fully_visible_p
15997 ? cursor_height + this_scroll_margin
15998 : 1)) > it.last_visible_y)
15999 {
16000 w->cursor.vpos = -1;
16001 clear_glyph_matrix (w->desired_matrix);
16002 return -1;
16003 }
16004 }
16005
16006 /* Scroll the display. Do it before changing the current matrix so
16007 that xterm.c doesn't get confused about where the cursor glyph is
16008 found. */
16009 if (dy && run.height)
16010 {
16011 update_begin (f);
16012
16013 if (FRAME_WINDOW_P (f))
16014 {
16015 FRAME_RIF (f)->update_window_begin_hook (w);
16016 FRAME_RIF (f)->clear_window_mouse_face (w);
16017 FRAME_RIF (f)->scroll_run_hook (w, &run);
16018 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16019 }
16020 else
16021 {
16022 /* Terminal frame. In this case, dvpos gives the number of
16023 lines to scroll by; dvpos < 0 means scroll up. */
16024 int first_unchanged_at_end_vpos
16025 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16026 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
16027 int end = (WINDOW_TOP_EDGE_LINE (w)
16028 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16029 + window_internal_height (w));
16030
16031 #if defined (HAVE_GPM) || defined (MSDOS)
16032 x_clear_window_mouse_face (w);
16033 #endif
16034 /* Perform the operation on the screen. */
16035 if (dvpos > 0)
16036 {
16037 /* Scroll last_unchanged_at_beg_row to the end of the
16038 window down dvpos lines. */
16039 set_terminal_window (f, end);
16040
16041 /* On dumb terminals delete dvpos lines at the end
16042 before inserting dvpos empty lines. */
16043 if (!FRAME_SCROLL_REGION_OK (f))
16044 ins_del_lines (f, end - dvpos, -dvpos);
16045
16046 /* Insert dvpos empty lines in front of
16047 last_unchanged_at_beg_row. */
16048 ins_del_lines (f, from, dvpos);
16049 }
16050 else if (dvpos < 0)
16051 {
16052 /* Scroll up last_unchanged_at_beg_vpos to the end of
16053 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16054 set_terminal_window (f, end);
16055
16056 /* Delete dvpos lines in front of
16057 last_unchanged_at_beg_vpos. ins_del_lines will set
16058 the cursor to the given vpos and emit |dvpos| delete
16059 line sequences. */
16060 ins_del_lines (f, from + dvpos, dvpos);
16061
16062 /* On a dumb terminal insert dvpos empty lines at the
16063 end. */
16064 if (!FRAME_SCROLL_REGION_OK (f))
16065 ins_del_lines (f, end + dvpos, -dvpos);
16066 }
16067
16068 set_terminal_window (f, 0);
16069 }
16070
16071 update_end (f);
16072 }
16073
16074 /* Shift reused rows of the current matrix to the right position.
16075 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16076 text. */
16077 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16078 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16079 if (dvpos < 0)
16080 {
16081 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16082 bottom_vpos, dvpos);
16083 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16084 bottom_vpos, 0);
16085 }
16086 else if (dvpos > 0)
16087 {
16088 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16089 bottom_vpos, dvpos);
16090 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16091 first_unchanged_at_end_vpos + dvpos, 0);
16092 }
16093
16094 /* For frame-based redisplay, make sure that current frame and window
16095 matrix are in sync with respect to glyph memory. */
16096 if (!FRAME_WINDOW_P (f))
16097 sync_frame_with_window_matrix_rows (w);
16098
16099 /* Adjust buffer positions in reused rows. */
16100 if (delta || delta_bytes)
16101 increment_matrix_positions (current_matrix,
16102 first_unchanged_at_end_vpos + dvpos,
16103 bottom_vpos, delta, delta_bytes);
16104
16105 /* Adjust Y positions. */
16106 if (dy)
16107 shift_glyph_matrix (w, current_matrix,
16108 first_unchanged_at_end_vpos + dvpos,
16109 bottom_vpos, dy);
16110
16111 if (first_unchanged_at_end_row)
16112 {
16113 first_unchanged_at_end_row += dvpos;
16114 if (first_unchanged_at_end_row->y >= it.last_visible_y
16115 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16116 first_unchanged_at_end_row = NULL;
16117 }
16118
16119 /* If scrolling up, there may be some lines to display at the end of
16120 the window. */
16121 last_text_row_at_end = NULL;
16122 if (dy < 0)
16123 {
16124 /* Scrolling up can leave for example a partially visible line
16125 at the end of the window to be redisplayed. */
16126 /* Set last_row to the glyph row in the current matrix where the
16127 window end line is found. It has been moved up or down in
16128 the matrix by dvpos. */
16129 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16130 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16131
16132 /* If last_row is the window end line, it should display text. */
16133 xassert (last_row->displays_text_p);
16134
16135 /* If window end line was partially visible before, begin
16136 displaying at that line. Otherwise begin displaying with the
16137 line following it. */
16138 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16139 {
16140 init_to_row_start (&it, w, last_row);
16141 it.vpos = last_vpos;
16142 it.current_y = last_row->y;
16143 }
16144 else
16145 {
16146 init_to_row_end (&it, w, last_row);
16147 it.vpos = 1 + last_vpos;
16148 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16149 ++last_row;
16150 }
16151
16152 /* We may start in a continuation line. If so, we have to
16153 get the right continuation_lines_width and current_x. */
16154 it.continuation_lines_width = last_row->continuation_lines_width;
16155 it.hpos = it.current_x = 0;
16156
16157 /* Display the rest of the lines at the window end. */
16158 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16159 while (it.current_y < it.last_visible_y
16160 && !fonts_changed_p)
16161 {
16162 /* Is it always sure that the display agrees with lines in
16163 the current matrix? I don't think so, so we mark rows
16164 displayed invalid in the current matrix by setting their
16165 enabled_p flag to zero. */
16166 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16167 if (display_line (&it))
16168 last_text_row_at_end = it.glyph_row - 1;
16169 }
16170 }
16171
16172 /* Update window_end_pos and window_end_vpos. */
16173 if (first_unchanged_at_end_row
16174 && !last_text_row_at_end)
16175 {
16176 /* Window end line if one of the preserved rows from the current
16177 matrix. Set row to the last row displaying text in current
16178 matrix starting at first_unchanged_at_end_row, after
16179 scrolling. */
16180 xassert (first_unchanged_at_end_row->displays_text_p);
16181 row = find_last_row_displaying_text (w->current_matrix, &it,
16182 first_unchanged_at_end_row);
16183 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16184
16185 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16186 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16187 w->window_end_vpos
16188 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16189 xassert (w->window_end_bytepos >= 0);
16190 IF_DEBUG (debug_method_add (w, "A"));
16191 }
16192 else if (last_text_row_at_end)
16193 {
16194 w->window_end_pos
16195 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16196 w->window_end_bytepos
16197 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16198 w->window_end_vpos
16199 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16200 xassert (w->window_end_bytepos >= 0);
16201 IF_DEBUG (debug_method_add (w, "B"));
16202 }
16203 else if (last_text_row)
16204 {
16205 /* We have displayed either to the end of the window or at the
16206 end of the window, i.e. the last row with text is to be found
16207 in the desired matrix. */
16208 w->window_end_pos
16209 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16210 w->window_end_bytepos
16211 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16212 w->window_end_vpos
16213 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16214 xassert (w->window_end_bytepos >= 0);
16215 }
16216 else if (first_unchanged_at_end_row == NULL
16217 && last_text_row == NULL
16218 && last_text_row_at_end == NULL)
16219 {
16220 /* Displayed to end of window, but no line containing text was
16221 displayed. Lines were deleted at the end of the window. */
16222 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16223 int vpos = XFASTINT (w->window_end_vpos);
16224 struct glyph_row *current_row = current_matrix->rows + vpos;
16225 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16226
16227 for (row = NULL;
16228 row == NULL && vpos >= first_vpos;
16229 --vpos, --current_row, --desired_row)
16230 {
16231 if (desired_row->enabled_p)
16232 {
16233 if (desired_row->displays_text_p)
16234 row = desired_row;
16235 }
16236 else if (current_row->displays_text_p)
16237 row = current_row;
16238 }
16239
16240 xassert (row != NULL);
16241 w->window_end_vpos = make_number (vpos + 1);
16242 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16243 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16244 xassert (w->window_end_bytepos >= 0);
16245 IF_DEBUG (debug_method_add (w, "C"));
16246 }
16247 else
16248 abort ();
16249
16250 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16251 debug_end_vpos = XFASTINT (w->window_end_vpos));
16252
16253 /* Record that display has not been completed. */
16254 w->window_end_valid = Qnil;
16255 w->desired_matrix->no_scrolling_p = 1;
16256 return 3;
16257
16258 #undef GIVE_UP
16259 }
16260
16261
16262 \f
16263 /***********************************************************************
16264 More debugging support
16265 ***********************************************************************/
16266
16267 #if GLYPH_DEBUG
16268
16269 void dump_glyph_row (struct glyph_row *, int, int);
16270 void dump_glyph_matrix (struct glyph_matrix *, int);
16271 void dump_glyph (struct glyph_row *, struct glyph *, int);
16272
16273
16274 /* Dump the contents of glyph matrix MATRIX on stderr.
16275
16276 GLYPHS 0 means don't show glyph contents.
16277 GLYPHS 1 means show glyphs in short form
16278 GLYPHS > 1 means show glyphs in long form. */
16279
16280 void
16281 dump_glyph_matrix (matrix, glyphs)
16282 struct glyph_matrix *matrix;
16283 int glyphs;
16284 {
16285 int i;
16286 for (i = 0; i < matrix->nrows; ++i)
16287 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16288 }
16289
16290
16291 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16292 the glyph row and area where the glyph comes from. */
16293
16294 void
16295 dump_glyph (row, glyph, area)
16296 struct glyph_row *row;
16297 struct glyph *glyph;
16298 int area;
16299 {
16300 if (glyph->type == CHAR_GLYPH)
16301 {
16302 fprintf (stderr,
16303 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16304 glyph - row->glyphs[TEXT_AREA],
16305 'C',
16306 glyph->charpos,
16307 (BUFFERP (glyph->object)
16308 ? 'B'
16309 : (STRINGP (glyph->object)
16310 ? 'S'
16311 : '-')),
16312 glyph->pixel_width,
16313 glyph->u.ch,
16314 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16315 ? glyph->u.ch
16316 : '.'),
16317 glyph->face_id,
16318 glyph->left_box_line_p,
16319 glyph->right_box_line_p);
16320 }
16321 else if (glyph->type == STRETCH_GLYPH)
16322 {
16323 fprintf (stderr,
16324 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16325 glyph - row->glyphs[TEXT_AREA],
16326 'S',
16327 glyph->charpos,
16328 (BUFFERP (glyph->object)
16329 ? 'B'
16330 : (STRINGP (glyph->object)
16331 ? 'S'
16332 : '-')),
16333 glyph->pixel_width,
16334 0,
16335 '.',
16336 glyph->face_id,
16337 glyph->left_box_line_p,
16338 glyph->right_box_line_p);
16339 }
16340 else if (glyph->type == IMAGE_GLYPH)
16341 {
16342 fprintf (stderr,
16343 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16344 glyph - row->glyphs[TEXT_AREA],
16345 'I',
16346 glyph->charpos,
16347 (BUFFERP (glyph->object)
16348 ? 'B'
16349 : (STRINGP (glyph->object)
16350 ? 'S'
16351 : '-')),
16352 glyph->pixel_width,
16353 glyph->u.img_id,
16354 '.',
16355 glyph->face_id,
16356 glyph->left_box_line_p,
16357 glyph->right_box_line_p);
16358 }
16359 else if (glyph->type == COMPOSITE_GLYPH)
16360 {
16361 fprintf (stderr,
16362 " %5d %4c %6d %c %3d 0x%05x",
16363 glyph - row->glyphs[TEXT_AREA],
16364 '+',
16365 glyph->charpos,
16366 (BUFFERP (glyph->object)
16367 ? 'B'
16368 : (STRINGP (glyph->object)
16369 ? 'S'
16370 : '-')),
16371 glyph->pixel_width,
16372 glyph->u.cmp.id);
16373 if (glyph->u.cmp.automatic)
16374 fprintf (stderr,
16375 "[%d-%d]",
16376 glyph->slice.cmp.from, glyph->slice.cmp.to);
16377 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16378 glyph->face_id,
16379 glyph->left_box_line_p,
16380 glyph->right_box_line_p);
16381 }
16382 }
16383
16384
16385 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16386 GLYPHS 0 means don't show glyph contents.
16387 GLYPHS 1 means show glyphs in short form
16388 GLYPHS > 1 means show glyphs in long form. */
16389
16390 void
16391 dump_glyph_row (row, vpos, glyphs)
16392 struct glyph_row *row;
16393 int vpos, glyphs;
16394 {
16395 if (glyphs != 1)
16396 {
16397 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16398 fprintf (stderr, "======================================================================\n");
16399
16400 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16401 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16402 vpos,
16403 MATRIX_ROW_START_CHARPOS (row),
16404 MATRIX_ROW_END_CHARPOS (row),
16405 row->used[TEXT_AREA],
16406 row->contains_overlapping_glyphs_p,
16407 row->enabled_p,
16408 row->truncated_on_left_p,
16409 row->truncated_on_right_p,
16410 row->continued_p,
16411 MATRIX_ROW_CONTINUATION_LINE_P (row),
16412 row->displays_text_p,
16413 row->ends_at_zv_p,
16414 row->fill_line_p,
16415 row->ends_in_middle_of_char_p,
16416 row->starts_in_middle_of_char_p,
16417 row->mouse_face_p,
16418 row->x,
16419 row->y,
16420 row->pixel_width,
16421 row->height,
16422 row->visible_height,
16423 row->ascent,
16424 row->phys_ascent);
16425 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16426 row->end.overlay_string_index,
16427 row->continuation_lines_width);
16428 fprintf (stderr, "%9d %5d\n",
16429 CHARPOS (row->start.string_pos),
16430 CHARPOS (row->end.string_pos));
16431 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16432 row->end.dpvec_index);
16433 }
16434
16435 if (glyphs > 1)
16436 {
16437 int area;
16438
16439 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16440 {
16441 struct glyph *glyph = row->glyphs[area];
16442 struct glyph *glyph_end = glyph + row->used[area];
16443
16444 /* Glyph for a line end in text. */
16445 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16446 ++glyph_end;
16447
16448 if (glyph < glyph_end)
16449 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16450
16451 for (; glyph < glyph_end; ++glyph)
16452 dump_glyph (row, glyph, area);
16453 }
16454 }
16455 else if (glyphs == 1)
16456 {
16457 int area;
16458
16459 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16460 {
16461 char *s = (char *) alloca (row->used[area] + 1);
16462 int i;
16463
16464 for (i = 0; i < row->used[area]; ++i)
16465 {
16466 struct glyph *glyph = row->glyphs[area] + i;
16467 if (glyph->type == CHAR_GLYPH
16468 && glyph->u.ch < 0x80
16469 && glyph->u.ch >= ' ')
16470 s[i] = glyph->u.ch;
16471 else
16472 s[i] = '.';
16473 }
16474
16475 s[i] = '\0';
16476 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16477 }
16478 }
16479 }
16480
16481
16482 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16483 Sdump_glyph_matrix, 0, 1, "p",
16484 doc: /* Dump the current matrix of the selected window to stderr.
16485 Shows contents of glyph row structures. With non-nil
16486 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16487 glyphs in short form, otherwise show glyphs in long form. */)
16488 (Lisp_Object glyphs)
16489 {
16490 struct window *w = XWINDOW (selected_window);
16491 struct buffer *buffer = XBUFFER (w->buffer);
16492
16493 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16494 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16495 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16496 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16497 fprintf (stderr, "=============================================\n");
16498 dump_glyph_matrix (w->current_matrix,
16499 NILP (glyphs) ? 0 : XINT (glyphs));
16500 return Qnil;
16501 }
16502
16503
16504 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16505 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16506 (void)
16507 {
16508 struct frame *f = XFRAME (selected_frame);
16509 dump_glyph_matrix (f->current_matrix, 1);
16510 return Qnil;
16511 }
16512
16513
16514 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16515 doc: /* Dump glyph row ROW to stderr.
16516 GLYPH 0 means don't dump glyphs.
16517 GLYPH 1 means dump glyphs in short form.
16518 GLYPH > 1 or omitted means dump glyphs in long form. */)
16519 (Lisp_Object row, Lisp_Object glyphs)
16520 {
16521 struct glyph_matrix *matrix;
16522 int vpos;
16523
16524 CHECK_NUMBER (row);
16525 matrix = XWINDOW (selected_window)->current_matrix;
16526 vpos = XINT (row);
16527 if (vpos >= 0 && vpos < matrix->nrows)
16528 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16529 vpos,
16530 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16531 return Qnil;
16532 }
16533
16534
16535 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16536 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16537 GLYPH 0 means don't dump glyphs.
16538 GLYPH 1 means dump glyphs in short form.
16539 GLYPH > 1 or omitted means dump glyphs in long form. */)
16540 (Lisp_Object row, Lisp_Object glyphs)
16541 {
16542 struct frame *sf = SELECTED_FRAME ();
16543 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16544 int vpos;
16545
16546 CHECK_NUMBER (row);
16547 vpos = XINT (row);
16548 if (vpos >= 0 && vpos < m->nrows)
16549 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16550 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16551 return Qnil;
16552 }
16553
16554
16555 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16556 doc: /* Toggle tracing of redisplay.
16557 With ARG, turn tracing on if and only if ARG is positive. */)
16558 (Lisp_Object arg)
16559 {
16560 if (NILP (arg))
16561 trace_redisplay_p = !trace_redisplay_p;
16562 else
16563 {
16564 arg = Fprefix_numeric_value (arg);
16565 trace_redisplay_p = XINT (arg) > 0;
16566 }
16567
16568 return Qnil;
16569 }
16570
16571
16572 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16573 doc: /* Like `format', but print result to stderr.
16574 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16575 (int nargs, Lisp_Object *args)
16576 {
16577 Lisp_Object s = Fformat (nargs, args);
16578 fprintf (stderr, "%s", SDATA (s));
16579 return Qnil;
16580 }
16581
16582 #endif /* GLYPH_DEBUG */
16583
16584
16585 \f
16586 /***********************************************************************
16587 Building Desired Matrix Rows
16588 ***********************************************************************/
16589
16590 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16591 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16592
16593 static struct glyph_row *
16594 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16595 {
16596 struct frame *f = XFRAME (WINDOW_FRAME (w));
16597 struct buffer *buffer = XBUFFER (w->buffer);
16598 struct buffer *old = current_buffer;
16599 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16600 int arrow_len = SCHARS (overlay_arrow_string);
16601 const unsigned char *arrow_end = arrow_string + arrow_len;
16602 const unsigned char *p;
16603 struct it it;
16604 int multibyte_p;
16605 int n_glyphs_before;
16606
16607 set_buffer_temp (buffer);
16608 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16609 it.glyph_row->used[TEXT_AREA] = 0;
16610 SET_TEXT_POS (it.position, 0, 0);
16611
16612 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16613 p = arrow_string;
16614 while (p < arrow_end)
16615 {
16616 Lisp_Object face, ilisp;
16617
16618 /* Get the next character. */
16619 if (multibyte_p)
16620 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16621 else
16622 {
16623 it.c = it.char_to_display = *p, it.len = 1;
16624 if (! ASCII_CHAR_P (it.c))
16625 it.char_to_display = BYTE8_TO_CHAR (it.c);
16626 }
16627 p += it.len;
16628
16629 /* Get its face. */
16630 ilisp = make_number (p - arrow_string);
16631 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16632 it.face_id = compute_char_face (f, it.char_to_display, face);
16633
16634 /* Compute its width, get its glyphs. */
16635 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16636 SET_TEXT_POS (it.position, -1, -1);
16637 PRODUCE_GLYPHS (&it);
16638
16639 /* If this character doesn't fit any more in the line, we have
16640 to remove some glyphs. */
16641 if (it.current_x > it.last_visible_x)
16642 {
16643 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16644 break;
16645 }
16646 }
16647
16648 set_buffer_temp (old);
16649 return it.glyph_row;
16650 }
16651
16652
16653 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16654 glyphs are only inserted for terminal frames since we can't really
16655 win with truncation glyphs when partially visible glyphs are
16656 involved. Which glyphs to insert is determined by
16657 produce_special_glyphs. */
16658
16659 static void
16660 insert_left_trunc_glyphs (struct it *it)
16661 {
16662 struct it truncate_it;
16663 struct glyph *from, *end, *to, *toend;
16664
16665 xassert (!FRAME_WINDOW_P (it->f));
16666
16667 /* Get the truncation glyphs. */
16668 truncate_it = *it;
16669 truncate_it.current_x = 0;
16670 truncate_it.face_id = DEFAULT_FACE_ID;
16671 truncate_it.glyph_row = &scratch_glyph_row;
16672 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16673 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16674 truncate_it.object = make_number (0);
16675 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16676
16677 /* Overwrite glyphs from IT with truncation glyphs. */
16678 if (!it->glyph_row->reversed_p)
16679 {
16680 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16681 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16682 to = it->glyph_row->glyphs[TEXT_AREA];
16683 toend = to + it->glyph_row->used[TEXT_AREA];
16684
16685 while (from < end)
16686 *to++ = *from++;
16687
16688 /* There may be padding glyphs left over. Overwrite them too. */
16689 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16690 {
16691 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16692 while (from < end)
16693 *to++ = *from++;
16694 }
16695
16696 if (to > toend)
16697 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16698 }
16699 else
16700 {
16701 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16702 that back to front. */
16703 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16704 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16705 toend = it->glyph_row->glyphs[TEXT_AREA];
16706 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16707
16708 while (from >= end && to >= toend)
16709 *to-- = *from--;
16710 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16711 {
16712 from =
16713 truncate_it.glyph_row->glyphs[TEXT_AREA]
16714 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16715 while (from >= end && to >= toend)
16716 *to-- = *from--;
16717 }
16718 if (from >= end)
16719 {
16720 /* Need to free some room before prepending additional
16721 glyphs. */
16722 int move_by = from - end + 1;
16723 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16724 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16725
16726 for ( ; g >= g0; g--)
16727 g[move_by] = *g;
16728 while (from >= end)
16729 *to-- = *from--;
16730 it->glyph_row->used[TEXT_AREA] += move_by;
16731 }
16732 }
16733 }
16734
16735
16736 /* Compute the pixel height and width of IT->glyph_row.
16737
16738 Most of the time, ascent and height of a display line will be equal
16739 to the max_ascent and max_height values of the display iterator
16740 structure. This is not the case if
16741
16742 1. We hit ZV without displaying anything. In this case, max_ascent
16743 and max_height will be zero.
16744
16745 2. We have some glyphs that don't contribute to the line height.
16746 (The glyph row flag contributes_to_line_height_p is for future
16747 pixmap extensions).
16748
16749 The first case is easily covered by using default values because in
16750 these cases, the line height does not really matter, except that it
16751 must not be zero. */
16752
16753 static void
16754 compute_line_metrics (struct it *it)
16755 {
16756 struct glyph_row *row = it->glyph_row;
16757 int area, i;
16758
16759 if (FRAME_WINDOW_P (it->f))
16760 {
16761 int i, min_y, max_y;
16762
16763 /* The line may consist of one space only, that was added to
16764 place the cursor on it. If so, the row's height hasn't been
16765 computed yet. */
16766 if (row->height == 0)
16767 {
16768 if (it->max_ascent + it->max_descent == 0)
16769 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16770 row->ascent = it->max_ascent;
16771 row->height = it->max_ascent + it->max_descent;
16772 row->phys_ascent = it->max_phys_ascent;
16773 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16774 row->extra_line_spacing = it->max_extra_line_spacing;
16775 }
16776
16777 /* Compute the width of this line. */
16778 row->pixel_width = row->x;
16779 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16780 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16781
16782 xassert (row->pixel_width >= 0);
16783 xassert (row->ascent >= 0 && row->height > 0);
16784
16785 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16786 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16787
16788 /* If first line's physical ascent is larger than its logical
16789 ascent, use the physical ascent, and make the row taller.
16790 This makes accented characters fully visible. */
16791 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16792 && row->phys_ascent > row->ascent)
16793 {
16794 row->height += row->phys_ascent - row->ascent;
16795 row->ascent = row->phys_ascent;
16796 }
16797
16798 /* Compute how much of the line is visible. */
16799 row->visible_height = row->height;
16800
16801 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16802 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16803
16804 if (row->y < min_y)
16805 row->visible_height -= min_y - row->y;
16806 if (row->y + row->height > max_y)
16807 row->visible_height -= row->y + row->height - max_y;
16808 }
16809 else
16810 {
16811 row->pixel_width = row->used[TEXT_AREA];
16812 if (row->continued_p)
16813 row->pixel_width -= it->continuation_pixel_width;
16814 else if (row->truncated_on_right_p)
16815 row->pixel_width -= it->truncation_pixel_width;
16816 row->ascent = row->phys_ascent = 0;
16817 row->height = row->phys_height = row->visible_height = 1;
16818 row->extra_line_spacing = 0;
16819 }
16820
16821 /* Compute a hash code for this row. */
16822 row->hash = 0;
16823 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16824 for (i = 0; i < row->used[area]; ++i)
16825 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16826 + row->glyphs[area][i].u.val
16827 + row->glyphs[area][i].face_id
16828 + row->glyphs[area][i].padding_p
16829 + (row->glyphs[area][i].type << 2));
16830
16831 it->max_ascent = it->max_descent = 0;
16832 it->max_phys_ascent = it->max_phys_descent = 0;
16833 }
16834
16835
16836 /* Append one space to the glyph row of iterator IT if doing a
16837 window-based redisplay. The space has the same face as
16838 IT->face_id. Value is non-zero if a space was added.
16839
16840 This function is called to make sure that there is always one glyph
16841 at the end of a glyph row that the cursor can be set on under
16842 window-systems. (If there weren't such a glyph we would not know
16843 how wide and tall a box cursor should be displayed).
16844
16845 At the same time this space let's a nicely handle clearing to the
16846 end of the line if the row ends in italic text. */
16847
16848 static int
16849 append_space_for_newline (struct it *it, int default_face_p)
16850 {
16851 if (FRAME_WINDOW_P (it->f))
16852 {
16853 int n = it->glyph_row->used[TEXT_AREA];
16854
16855 if (it->glyph_row->glyphs[TEXT_AREA] + n
16856 < it->glyph_row->glyphs[1 + TEXT_AREA])
16857 {
16858 /* Save some values that must not be changed.
16859 Must save IT->c and IT->len because otherwise
16860 ITERATOR_AT_END_P wouldn't work anymore after
16861 append_space_for_newline has been called. */
16862 enum display_element_type saved_what = it->what;
16863 int saved_c = it->c, saved_len = it->len;
16864 int saved_char_to_display = it->char_to_display;
16865 int saved_x = it->current_x;
16866 int saved_face_id = it->face_id;
16867 struct text_pos saved_pos;
16868 Lisp_Object saved_object;
16869 struct face *face;
16870
16871 saved_object = it->object;
16872 saved_pos = it->position;
16873
16874 it->what = IT_CHARACTER;
16875 memset (&it->position, 0, sizeof it->position);
16876 it->object = make_number (0);
16877 it->c = it->char_to_display = ' ';
16878 it->len = 1;
16879
16880 if (default_face_p)
16881 it->face_id = DEFAULT_FACE_ID;
16882 else if (it->face_before_selective_p)
16883 it->face_id = it->saved_face_id;
16884 face = FACE_FROM_ID (it->f, it->face_id);
16885 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16886
16887 PRODUCE_GLYPHS (it);
16888
16889 it->override_ascent = -1;
16890 it->constrain_row_ascent_descent_p = 0;
16891 it->current_x = saved_x;
16892 it->object = saved_object;
16893 it->position = saved_pos;
16894 it->what = saved_what;
16895 it->face_id = saved_face_id;
16896 it->len = saved_len;
16897 it->c = saved_c;
16898 it->char_to_display = saved_char_to_display;
16899 return 1;
16900 }
16901 }
16902
16903 return 0;
16904 }
16905
16906
16907 /* Extend the face of the last glyph in the text area of IT->glyph_row
16908 to the end of the display line. Called from display_line. If the
16909 glyph row is empty, add a space glyph to it so that we know the
16910 face to draw. Set the glyph row flag fill_line_p. If the glyph
16911 row is R2L, prepend a stretch glyph to cover the empty space to the
16912 left of the leftmost glyph. */
16913
16914 static void
16915 extend_face_to_end_of_line (struct it *it)
16916 {
16917 struct face *face;
16918 struct frame *f = it->f;
16919
16920 /* If line is already filled, do nothing. Non window-system frames
16921 get a grace of one more ``pixel'' because their characters are
16922 1-``pixel'' wide, so they hit the equality too early. This grace
16923 is needed only for R2L rows that are not continued, to produce
16924 one extra blank where we could display the cursor. */
16925 if (it->current_x >= it->last_visible_x
16926 + (!FRAME_WINDOW_P (f)
16927 && it->glyph_row->reversed_p
16928 && !it->glyph_row->continued_p))
16929 return;
16930
16931 /* Face extension extends the background and box of IT->face_id
16932 to the end of the line. If the background equals the background
16933 of the frame, we don't have to do anything. */
16934 if (it->face_before_selective_p)
16935 face = FACE_FROM_ID (f, it->saved_face_id);
16936 else
16937 face = FACE_FROM_ID (f, it->face_id);
16938
16939 if (FRAME_WINDOW_P (f)
16940 && it->glyph_row->displays_text_p
16941 && face->box == FACE_NO_BOX
16942 && face->background == FRAME_BACKGROUND_PIXEL (f)
16943 && !face->stipple
16944 && !it->glyph_row->reversed_p)
16945 return;
16946
16947 /* Set the glyph row flag indicating that the face of the last glyph
16948 in the text area has to be drawn to the end of the text area. */
16949 it->glyph_row->fill_line_p = 1;
16950
16951 /* If current character of IT is not ASCII, make sure we have the
16952 ASCII face. This will be automatically undone the next time
16953 get_next_display_element returns a multibyte character. Note
16954 that the character will always be single byte in unibyte
16955 text. */
16956 if (!ASCII_CHAR_P (it->c))
16957 {
16958 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16959 }
16960
16961 if (FRAME_WINDOW_P (f))
16962 {
16963 /* If the row is empty, add a space with the current face of IT,
16964 so that we know which face to draw. */
16965 if (it->glyph_row->used[TEXT_AREA] == 0)
16966 {
16967 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16968 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16969 it->glyph_row->used[TEXT_AREA] = 1;
16970 }
16971 #ifdef HAVE_WINDOW_SYSTEM
16972 if (it->glyph_row->reversed_p)
16973 {
16974 /* Prepend a stretch glyph to the row, such that the
16975 rightmost glyph will be drawn flushed all the way to the
16976 right margin of the window. The stretch glyph that will
16977 occupy the empty space, if any, to the left of the
16978 glyphs. */
16979 struct font *font = face->font ? face->font : FRAME_FONT (f);
16980 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16981 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16982 struct glyph *g;
16983 int row_width, stretch_ascent, stretch_width;
16984 struct text_pos saved_pos;
16985 int saved_face_id, saved_avoid_cursor;
16986
16987 for (row_width = 0, g = row_start; g < row_end; g++)
16988 row_width += g->pixel_width;
16989 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16990 if (stretch_width > 0)
16991 {
16992 stretch_ascent =
16993 (((it->ascent + it->descent)
16994 * FONT_BASE (font)) / FONT_HEIGHT (font));
16995 saved_pos = it->position;
16996 memset (&it->position, 0, sizeof it->position);
16997 saved_avoid_cursor = it->avoid_cursor_p;
16998 it->avoid_cursor_p = 1;
16999 saved_face_id = it->face_id;
17000 /* The last row's stretch glyph should get the default
17001 face, to avoid painting the rest of the window with
17002 the region face, if the region ends at ZV. */
17003 if (it->glyph_row->ends_at_zv_p)
17004 it->face_id = DEFAULT_FACE_ID;
17005 else
17006 it->face_id = face->id;
17007 append_stretch_glyph (it, make_number (0), stretch_width,
17008 it->ascent + it->descent, stretch_ascent);
17009 it->position = saved_pos;
17010 it->avoid_cursor_p = saved_avoid_cursor;
17011 it->face_id = saved_face_id;
17012 }
17013 }
17014 #endif /* HAVE_WINDOW_SYSTEM */
17015 }
17016 else
17017 {
17018 /* Save some values that must not be changed. */
17019 int saved_x = it->current_x;
17020 struct text_pos saved_pos;
17021 Lisp_Object saved_object;
17022 enum display_element_type saved_what = it->what;
17023 int saved_face_id = it->face_id;
17024
17025 saved_object = it->object;
17026 saved_pos = it->position;
17027
17028 it->what = IT_CHARACTER;
17029 memset (&it->position, 0, sizeof it->position);
17030 it->object = make_number (0);
17031 it->c = it->char_to_display = ' ';
17032 it->len = 1;
17033 /* The last row's blank glyphs should get the default face, to
17034 avoid painting the rest of the window with the region face,
17035 if the region ends at ZV. */
17036 if (it->glyph_row->ends_at_zv_p)
17037 it->face_id = DEFAULT_FACE_ID;
17038 else
17039 it->face_id = face->id;
17040
17041 PRODUCE_GLYPHS (it);
17042
17043 while (it->current_x <= it->last_visible_x)
17044 PRODUCE_GLYPHS (it);
17045
17046 /* Don't count these blanks really. It would let us insert a left
17047 truncation glyph below and make us set the cursor on them, maybe. */
17048 it->current_x = saved_x;
17049 it->object = saved_object;
17050 it->position = saved_pos;
17051 it->what = saved_what;
17052 it->face_id = saved_face_id;
17053 }
17054 }
17055
17056
17057 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17058 trailing whitespace. */
17059
17060 static int
17061 trailing_whitespace_p (EMACS_INT charpos)
17062 {
17063 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17064 int c = 0;
17065
17066 while (bytepos < ZV_BYTE
17067 && (c = FETCH_CHAR (bytepos),
17068 c == ' ' || c == '\t'))
17069 ++bytepos;
17070
17071 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17072 {
17073 if (bytepos != PT_BYTE)
17074 return 1;
17075 }
17076 return 0;
17077 }
17078
17079
17080 /* Highlight trailing whitespace, if any, in ROW. */
17081
17082 void
17083 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17084 {
17085 int used = row->used[TEXT_AREA];
17086
17087 if (used)
17088 {
17089 struct glyph *start = row->glyphs[TEXT_AREA];
17090 struct glyph *glyph = start + used - 1;
17091
17092 if (row->reversed_p)
17093 {
17094 /* Right-to-left rows need to be processed in the opposite
17095 direction, so swap the edge pointers. */
17096 glyph = start;
17097 start = row->glyphs[TEXT_AREA] + used - 1;
17098 }
17099
17100 /* Skip over glyphs inserted to display the cursor at the
17101 end of a line, for extending the face of the last glyph
17102 to the end of the line on terminals, and for truncation
17103 and continuation glyphs. */
17104 if (!row->reversed_p)
17105 {
17106 while (glyph >= start
17107 && glyph->type == CHAR_GLYPH
17108 && INTEGERP (glyph->object))
17109 --glyph;
17110 }
17111 else
17112 {
17113 while (glyph <= start
17114 && glyph->type == CHAR_GLYPH
17115 && INTEGERP (glyph->object))
17116 ++glyph;
17117 }
17118
17119 /* If last glyph is a space or stretch, and it's trailing
17120 whitespace, set the face of all trailing whitespace glyphs in
17121 IT->glyph_row to `trailing-whitespace'. */
17122 if ((row->reversed_p ? glyph <= start : glyph >= start)
17123 && BUFFERP (glyph->object)
17124 && (glyph->type == STRETCH_GLYPH
17125 || (glyph->type == CHAR_GLYPH
17126 && glyph->u.ch == ' '))
17127 && trailing_whitespace_p (glyph->charpos))
17128 {
17129 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17130 if (face_id < 0)
17131 return;
17132
17133 if (!row->reversed_p)
17134 {
17135 while (glyph >= start
17136 && BUFFERP (glyph->object)
17137 && (glyph->type == STRETCH_GLYPH
17138 || (glyph->type == CHAR_GLYPH
17139 && glyph->u.ch == ' ')))
17140 (glyph--)->face_id = face_id;
17141 }
17142 else
17143 {
17144 while (glyph <= start
17145 && BUFFERP (glyph->object)
17146 && (glyph->type == STRETCH_GLYPH
17147 || (glyph->type == CHAR_GLYPH
17148 && glyph->u.ch == ' ')))
17149 (glyph++)->face_id = face_id;
17150 }
17151 }
17152 }
17153 }
17154
17155
17156 /* Value is non-zero if glyph row ROW in window W should be
17157 used to hold the cursor. */
17158
17159 static int
17160 cursor_row_p (struct window *w, struct glyph_row *row)
17161 {
17162 int cursor_row_p = 1;
17163
17164 if (PT == CHARPOS (row->end.pos))
17165 {
17166 /* Suppose the row ends on a string.
17167 Unless the row is continued, that means it ends on a newline
17168 in the string. If it's anything other than a display string
17169 (e.g. a before-string from an overlay), we don't want the
17170 cursor there. (This heuristic seems to give the optimal
17171 behavior for the various types of multi-line strings.) */
17172 if (CHARPOS (row->end.string_pos) >= 0)
17173 {
17174 if (row->continued_p)
17175 cursor_row_p = 1;
17176 else
17177 {
17178 /* Check for `display' property. */
17179 struct glyph *beg = row->glyphs[TEXT_AREA];
17180 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17181 struct glyph *glyph;
17182
17183 cursor_row_p = 0;
17184 for (glyph = end; glyph >= beg; --glyph)
17185 if (STRINGP (glyph->object))
17186 {
17187 Lisp_Object prop
17188 = Fget_char_property (make_number (PT),
17189 Qdisplay, Qnil);
17190 cursor_row_p =
17191 (!NILP (prop)
17192 && display_prop_string_p (prop, glyph->object));
17193 break;
17194 }
17195 }
17196 }
17197 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17198 {
17199 /* If the row ends in middle of a real character,
17200 and the line is continued, we want the cursor here.
17201 That's because CHARPOS (ROW->end.pos) would equal
17202 PT if PT is before the character. */
17203 if (!row->ends_in_ellipsis_p)
17204 cursor_row_p = row->continued_p;
17205 else
17206 /* If the row ends in an ellipsis, then
17207 CHARPOS (ROW->end.pos) will equal point after the
17208 invisible text. We want that position to be displayed
17209 after the ellipsis. */
17210 cursor_row_p = 0;
17211 }
17212 /* If the row ends at ZV, display the cursor at the end of that
17213 row instead of at the start of the row below. */
17214 else if (row->ends_at_zv_p)
17215 cursor_row_p = 1;
17216 else
17217 cursor_row_p = 0;
17218 }
17219
17220 return cursor_row_p;
17221 }
17222
17223 \f
17224
17225 /* Push the display property PROP so that it will be rendered at the
17226 current position in IT. Return 1 if PROP was successfully pushed,
17227 0 otherwise. */
17228
17229 static int
17230 push_display_prop (struct it *it, Lisp_Object prop)
17231 {
17232 push_it (it);
17233
17234 if (STRINGP (prop))
17235 {
17236 if (SCHARS (prop) == 0)
17237 {
17238 pop_it (it);
17239 return 0;
17240 }
17241
17242 it->string = prop;
17243 it->multibyte_p = STRING_MULTIBYTE (it->string);
17244 it->current.overlay_string_index = -1;
17245 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17246 it->end_charpos = it->string_nchars = SCHARS (it->string);
17247 it->method = GET_FROM_STRING;
17248 it->stop_charpos = 0;
17249 }
17250 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17251 {
17252 it->method = GET_FROM_STRETCH;
17253 it->object = prop;
17254 }
17255 #ifdef HAVE_WINDOW_SYSTEM
17256 else if (IMAGEP (prop))
17257 {
17258 it->what = IT_IMAGE;
17259 it->image_id = lookup_image (it->f, prop);
17260 it->method = GET_FROM_IMAGE;
17261 }
17262 #endif /* HAVE_WINDOW_SYSTEM */
17263 else
17264 {
17265 pop_it (it); /* bogus display property, give up */
17266 return 0;
17267 }
17268
17269 return 1;
17270 }
17271
17272 /* Return the character-property PROP at the current position in IT. */
17273
17274 static Lisp_Object
17275 get_it_property (struct it *it, Lisp_Object prop)
17276 {
17277 Lisp_Object position;
17278
17279 if (STRINGP (it->object))
17280 position = make_number (IT_STRING_CHARPOS (*it));
17281 else if (BUFFERP (it->object))
17282 position = make_number (IT_CHARPOS (*it));
17283 else
17284 return Qnil;
17285
17286 return Fget_char_property (position, prop, it->object);
17287 }
17288
17289 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17290
17291 static void
17292 handle_line_prefix (struct it *it)
17293 {
17294 Lisp_Object prefix;
17295 if (it->continuation_lines_width > 0)
17296 {
17297 prefix = get_it_property (it, Qwrap_prefix);
17298 if (NILP (prefix))
17299 prefix = Vwrap_prefix;
17300 }
17301 else
17302 {
17303 prefix = get_it_property (it, Qline_prefix);
17304 if (NILP (prefix))
17305 prefix = Vline_prefix;
17306 }
17307 if (! NILP (prefix) && push_display_prop (it, prefix))
17308 {
17309 /* If the prefix is wider than the window, and we try to wrap
17310 it, it would acquire its own wrap prefix, and so on till the
17311 iterator stack overflows. So, don't wrap the prefix. */
17312 it->line_wrap = TRUNCATE;
17313 it->avoid_cursor_p = 1;
17314 }
17315 }
17316
17317 \f
17318
17319 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17320 only for R2L lines from display_line, when it decides that too many
17321 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17322 continued. */
17323 static void
17324 unproduce_glyphs (struct it *it, int n)
17325 {
17326 struct glyph *glyph, *end;
17327
17328 xassert (it->glyph_row);
17329 xassert (it->glyph_row->reversed_p);
17330 xassert (it->area == TEXT_AREA);
17331 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17332
17333 if (n > it->glyph_row->used[TEXT_AREA])
17334 n = it->glyph_row->used[TEXT_AREA];
17335 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17336 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17337 for ( ; glyph < end; glyph++)
17338 glyph[-n] = *glyph;
17339 }
17340
17341 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17342 and ROW->maxpos. */
17343 static void
17344 find_row_edges (struct it *it, struct glyph_row *row,
17345 EMACS_INT min_pos, EMACS_INT min_bpos,
17346 EMACS_INT max_pos, EMACS_INT max_bpos)
17347 {
17348 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17349 lines' rows is implemented for bidi-reordered rows. */
17350
17351 /* ROW->minpos is the value of min_pos, the minimal buffer position
17352 we have in ROW. */
17353 if (min_pos <= ZV)
17354 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17355 else
17356 {
17357 /* We didn't find _any_ valid buffer positions in any of the
17358 glyphs, so we must trust the iterator's computed
17359 positions. */
17360 row->minpos = row->start.pos;
17361 max_pos = CHARPOS (it->current.pos);
17362 max_bpos = BYTEPOS (it->current.pos);
17363 }
17364
17365 if (!max_pos)
17366 abort ();
17367
17368 /* Here are the various use-cases for ending the row, and the
17369 corresponding values for ROW->maxpos:
17370
17371 Line ends in a newline from buffer eol_pos + 1
17372 Line is continued from buffer max_pos + 1
17373 Line is truncated on right it->current.pos
17374 Line ends in a newline from string max_pos
17375 Line is continued from string max_pos
17376 Line is continued from display vector max_pos
17377 Line is entirely from a string min_pos == max_pos
17378 Line is entirely from a display vector min_pos == max_pos
17379 Line that ends at ZV ZV
17380
17381 If you discover other use-cases, please add them here as
17382 appropriate. */
17383 if (row->ends_at_zv_p)
17384 row->maxpos = it->current.pos;
17385 else if (row->used[TEXT_AREA])
17386 {
17387 if (row->ends_in_newline_from_string_p)
17388 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17389 else if (CHARPOS (it->eol_pos) > 0)
17390 SET_TEXT_POS (row->maxpos,
17391 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17392 else if (row->continued_p)
17393 {
17394 /* If max_pos is different from IT's current position, it
17395 means IT->method does not belong to the display element
17396 at max_pos. However, it also means that the display
17397 element at max_pos was displayed in its entirety on this
17398 line, which is equivalent to saying that the next line
17399 starts at the next buffer position. */
17400 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17401 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17402 else
17403 {
17404 INC_BOTH (max_pos, max_bpos);
17405 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17406 }
17407 }
17408 else if (row->truncated_on_right_p)
17409 /* display_line already called reseat_at_next_visible_line_start,
17410 which puts the iterator at the beginning of the next line, in
17411 the logical order. */
17412 row->maxpos = it->current.pos;
17413 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17414 /* A line that is entirely from a string/image/stretch... */
17415 row->maxpos = row->minpos;
17416 else
17417 abort ();
17418 }
17419 else
17420 row->maxpos = it->current.pos;
17421 }
17422
17423 /* Construct the glyph row IT->glyph_row in the desired matrix of
17424 IT->w from text at the current position of IT. See dispextern.h
17425 for an overview of struct it. Value is non-zero if
17426 IT->glyph_row displays text, as opposed to a line displaying ZV
17427 only. */
17428
17429 static int
17430 display_line (struct it *it)
17431 {
17432 struct glyph_row *row = it->glyph_row;
17433 Lisp_Object overlay_arrow_string;
17434 struct it wrap_it;
17435 int may_wrap = 0, wrap_x;
17436 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17437 int wrap_row_phys_ascent, wrap_row_phys_height;
17438 int wrap_row_extra_line_spacing;
17439 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17440 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17441 int cvpos;
17442 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17443
17444 /* We always start displaying at hpos zero even if hscrolled. */
17445 xassert (it->hpos == 0 && it->current_x == 0);
17446
17447 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17448 >= it->w->desired_matrix->nrows)
17449 {
17450 it->w->nrows_scale_factor++;
17451 fonts_changed_p = 1;
17452 return 0;
17453 }
17454
17455 /* Is IT->w showing the region? */
17456 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17457
17458 /* Clear the result glyph row and enable it. */
17459 prepare_desired_row (row);
17460
17461 row->y = it->current_y;
17462 row->start = it->start;
17463 row->continuation_lines_width = it->continuation_lines_width;
17464 row->displays_text_p = 1;
17465 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17466 it->starts_in_middle_of_char_p = 0;
17467
17468 /* Arrange the overlays nicely for our purposes. Usually, we call
17469 display_line on only one line at a time, in which case this
17470 can't really hurt too much, or we call it on lines which appear
17471 one after another in the buffer, in which case all calls to
17472 recenter_overlay_lists but the first will be pretty cheap. */
17473 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17474
17475 /* Move over display elements that are not visible because we are
17476 hscrolled. This may stop at an x-position < IT->first_visible_x
17477 if the first glyph is partially visible or if we hit a line end. */
17478 if (it->current_x < it->first_visible_x)
17479 {
17480 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17481 MOVE_TO_POS | MOVE_TO_X);
17482 }
17483 else
17484 {
17485 /* We only do this when not calling `move_it_in_display_line_to'
17486 above, because move_it_in_display_line_to calls
17487 handle_line_prefix itself. */
17488 handle_line_prefix (it);
17489 }
17490
17491 /* Get the initial row height. This is either the height of the
17492 text hscrolled, if there is any, or zero. */
17493 row->ascent = it->max_ascent;
17494 row->height = it->max_ascent + it->max_descent;
17495 row->phys_ascent = it->max_phys_ascent;
17496 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17497 row->extra_line_spacing = it->max_extra_line_spacing;
17498
17499 /* Utility macro to record max and min buffer positions seen until now. */
17500 #define RECORD_MAX_MIN_POS(IT) \
17501 do \
17502 { \
17503 if (IT_CHARPOS (*(IT)) < min_pos) \
17504 { \
17505 min_pos = IT_CHARPOS (*(IT)); \
17506 min_bpos = IT_BYTEPOS (*(IT)); \
17507 } \
17508 if (IT_CHARPOS (*(IT)) > max_pos) \
17509 { \
17510 max_pos = IT_CHARPOS (*(IT)); \
17511 max_bpos = IT_BYTEPOS (*(IT)); \
17512 } \
17513 } \
17514 while (0)
17515
17516 /* Loop generating characters. The loop is left with IT on the next
17517 character to display. */
17518 while (1)
17519 {
17520 int n_glyphs_before, hpos_before, x_before;
17521 int x, i, nglyphs;
17522 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17523
17524 /* Retrieve the next thing to display. Value is zero if end of
17525 buffer reached. */
17526 if (!get_next_display_element (it))
17527 {
17528 /* Maybe add a space at the end of this line that is used to
17529 display the cursor there under X. Set the charpos of the
17530 first glyph of blank lines not corresponding to any text
17531 to -1. */
17532 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17533 row->exact_window_width_line_p = 1;
17534 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17535 || row->used[TEXT_AREA] == 0)
17536 {
17537 row->glyphs[TEXT_AREA]->charpos = -1;
17538 row->displays_text_p = 0;
17539
17540 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17541 && (!MINI_WINDOW_P (it->w)
17542 || (minibuf_level && EQ (it->window, minibuf_window))))
17543 row->indicate_empty_line_p = 1;
17544 }
17545
17546 it->continuation_lines_width = 0;
17547 row->ends_at_zv_p = 1;
17548 /* A row that displays right-to-left text must always have
17549 its last face extended all the way to the end of line,
17550 even if this row ends in ZV, because we still write to
17551 the screen left to right. */
17552 if (row->reversed_p)
17553 extend_face_to_end_of_line (it);
17554 break;
17555 }
17556
17557 /* Now, get the metrics of what we want to display. This also
17558 generates glyphs in `row' (which is IT->glyph_row). */
17559 n_glyphs_before = row->used[TEXT_AREA];
17560 x = it->current_x;
17561
17562 /* Remember the line height so far in case the next element doesn't
17563 fit on the line. */
17564 if (it->line_wrap != TRUNCATE)
17565 {
17566 ascent = it->max_ascent;
17567 descent = it->max_descent;
17568 phys_ascent = it->max_phys_ascent;
17569 phys_descent = it->max_phys_descent;
17570
17571 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17572 {
17573 if (IT_DISPLAYING_WHITESPACE (it))
17574 may_wrap = 1;
17575 else if (may_wrap)
17576 {
17577 wrap_it = *it;
17578 wrap_x = x;
17579 wrap_row_used = row->used[TEXT_AREA];
17580 wrap_row_ascent = row->ascent;
17581 wrap_row_height = row->height;
17582 wrap_row_phys_ascent = row->phys_ascent;
17583 wrap_row_phys_height = row->phys_height;
17584 wrap_row_extra_line_spacing = row->extra_line_spacing;
17585 wrap_row_min_pos = min_pos;
17586 wrap_row_min_bpos = min_bpos;
17587 wrap_row_max_pos = max_pos;
17588 wrap_row_max_bpos = max_bpos;
17589 may_wrap = 0;
17590 }
17591 }
17592 }
17593
17594 PRODUCE_GLYPHS (it);
17595
17596 /* If this display element was in marginal areas, continue with
17597 the next one. */
17598 if (it->area != TEXT_AREA)
17599 {
17600 row->ascent = max (row->ascent, it->max_ascent);
17601 row->height = max (row->height, it->max_ascent + it->max_descent);
17602 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17603 row->phys_height = max (row->phys_height,
17604 it->max_phys_ascent + it->max_phys_descent);
17605 row->extra_line_spacing = max (row->extra_line_spacing,
17606 it->max_extra_line_spacing);
17607 set_iterator_to_next (it, 1);
17608 continue;
17609 }
17610
17611 /* Does the display element fit on the line? If we truncate
17612 lines, we should draw past the right edge of the window. If
17613 we don't truncate, we want to stop so that we can display the
17614 continuation glyph before the right margin. If lines are
17615 continued, there are two possible strategies for characters
17616 resulting in more than 1 glyph (e.g. tabs): Display as many
17617 glyphs as possible in this line and leave the rest for the
17618 continuation line, or display the whole element in the next
17619 line. Original redisplay did the former, so we do it also. */
17620 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17621 hpos_before = it->hpos;
17622 x_before = x;
17623
17624 if (/* Not a newline. */
17625 nglyphs > 0
17626 /* Glyphs produced fit entirely in the line. */
17627 && it->current_x < it->last_visible_x)
17628 {
17629 it->hpos += nglyphs;
17630 row->ascent = max (row->ascent, it->max_ascent);
17631 row->height = max (row->height, it->max_ascent + it->max_descent);
17632 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17633 row->phys_height = max (row->phys_height,
17634 it->max_phys_ascent + it->max_phys_descent);
17635 row->extra_line_spacing = max (row->extra_line_spacing,
17636 it->max_extra_line_spacing);
17637 if (it->current_x - it->pixel_width < it->first_visible_x)
17638 row->x = x - it->first_visible_x;
17639 /* Record the maximum and minimum buffer positions seen so
17640 far in glyphs that will be displayed by this row. */
17641 if (it->bidi_p)
17642 RECORD_MAX_MIN_POS (it);
17643 }
17644 else
17645 {
17646 int new_x;
17647 struct glyph *glyph;
17648
17649 for (i = 0; i < nglyphs; ++i, x = new_x)
17650 {
17651 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17652 new_x = x + glyph->pixel_width;
17653
17654 if (/* Lines are continued. */
17655 it->line_wrap != TRUNCATE
17656 && (/* Glyph doesn't fit on the line. */
17657 new_x > it->last_visible_x
17658 /* Or it fits exactly on a window system frame. */
17659 || (new_x == it->last_visible_x
17660 && FRAME_WINDOW_P (it->f))))
17661 {
17662 /* End of a continued line. */
17663
17664 if (it->hpos == 0
17665 || (new_x == it->last_visible_x
17666 && FRAME_WINDOW_P (it->f)))
17667 {
17668 /* Current glyph is the only one on the line or
17669 fits exactly on the line. We must continue
17670 the line because we can't draw the cursor
17671 after the glyph. */
17672 row->continued_p = 1;
17673 it->current_x = new_x;
17674 it->continuation_lines_width += new_x;
17675 ++it->hpos;
17676 /* Record the maximum and minimum buffer
17677 positions seen so far in glyphs that will be
17678 displayed by this row. */
17679 if (it->bidi_p)
17680 RECORD_MAX_MIN_POS (it);
17681 if (i == nglyphs - 1)
17682 {
17683 /* If line-wrap is on, check if a previous
17684 wrap point was found. */
17685 if (wrap_row_used > 0
17686 /* Even if there is a previous wrap
17687 point, continue the line here as
17688 usual, if (i) the previous character
17689 was a space or tab AND (ii) the
17690 current character is not. */
17691 && (!may_wrap
17692 || IT_DISPLAYING_WHITESPACE (it)))
17693 goto back_to_wrap;
17694
17695 set_iterator_to_next (it, 1);
17696 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17697 {
17698 if (!get_next_display_element (it))
17699 {
17700 row->exact_window_width_line_p = 1;
17701 it->continuation_lines_width = 0;
17702 row->continued_p = 0;
17703 row->ends_at_zv_p = 1;
17704 }
17705 else if (ITERATOR_AT_END_OF_LINE_P (it))
17706 {
17707 row->continued_p = 0;
17708 row->exact_window_width_line_p = 1;
17709 }
17710 }
17711 }
17712 }
17713 else if (CHAR_GLYPH_PADDING_P (*glyph)
17714 && !FRAME_WINDOW_P (it->f))
17715 {
17716 /* A padding glyph that doesn't fit on this line.
17717 This means the whole character doesn't fit
17718 on the line. */
17719 if (row->reversed_p)
17720 unproduce_glyphs (it, row->used[TEXT_AREA]
17721 - n_glyphs_before);
17722 row->used[TEXT_AREA] = n_glyphs_before;
17723
17724 /* Fill the rest of the row with continuation
17725 glyphs like in 20.x. */
17726 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17727 < row->glyphs[1 + TEXT_AREA])
17728 produce_special_glyphs (it, IT_CONTINUATION);
17729
17730 row->continued_p = 1;
17731 it->current_x = x_before;
17732 it->continuation_lines_width += x_before;
17733
17734 /* Restore the height to what it was before the
17735 element not fitting on the line. */
17736 it->max_ascent = ascent;
17737 it->max_descent = descent;
17738 it->max_phys_ascent = phys_ascent;
17739 it->max_phys_descent = phys_descent;
17740 }
17741 else if (wrap_row_used > 0)
17742 {
17743 back_to_wrap:
17744 if (row->reversed_p)
17745 unproduce_glyphs (it,
17746 row->used[TEXT_AREA] - wrap_row_used);
17747 *it = wrap_it;
17748 it->continuation_lines_width += wrap_x;
17749 row->used[TEXT_AREA] = wrap_row_used;
17750 row->ascent = wrap_row_ascent;
17751 row->height = wrap_row_height;
17752 row->phys_ascent = wrap_row_phys_ascent;
17753 row->phys_height = wrap_row_phys_height;
17754 row->extra_line_spacing = wrap_row_extra_line_spacing;
17755 min_pos = wrap_row_min_pos;
17756 min_bpos = wrap_row_min_bpos;
17757 max_pos = wrap_row_max_pos;
17758 max_bpos = wrap_row_max_bpos;
17759 row->continued_p = 1;
17760 row->ends_at_zv_p = 0;
17761 row->exact_window_width_line_p = 0;
17762 it->continuation_lines_width += x;
17763
17764 /* Make sure that a non-default face is extended
17765 up to the right margin of the window. */
17766 extend_face_to_end_of_line (it);
17767 }
17768 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17769 {
17770 /* A TAB that extends past the right edge of the
17771 window. This produces a single glyph on
17772 window system frames. We leave the glyph in
17773 this row and let it fill the row, but don't
17774 consume the TAB. */
17775 it->continuation_lines_width += it->last_visible_x;
17776 row->ends_in_middle_of_char_p = 1;
17777 row->continued_p = 1;
17778 glyph->pixel_width = it->last_visible_x - x;
17779 it->starts_in_middle_of_char_p = 1;
17780 }
17781 else
17782 {
17783 /* Something other than a TAB that draws past
17784 the right edge of the window. Restore
17785 positions to values before the element. */
17786 if (row->reversed_p)
17787 unproduce_glyphs (it, row->used[TEXT_AREA]
17788 - (n_glyphs_before + i));
17789 row->used[TEXT_AREA] = n_glyphs_before + i;
17790
17791 /* Display continuation glyphs. */
17792 if (!FRAME_WINDOW_P (it->f))
17793 produce_special_glyphs (it, IT_CONTINUATION);
17794 row->continued_p = 1;
17795
17796 it->current_x = x_before;
17797 it->continuation_lines_width += x;
17798 extend_face_to_end_of_line (it);
17799
17800 if (nglyphs > 1 && i > 0)
17801 {
17802 row->ends_in_middle_of_char_p = 1;
17803 it->starts_in_middle_of_char_p = 1;
17804 }
17805
17806 /* Restore the height to what it was before the
17807 element not fitting on the line. */
17808 it->max_ascent = ascent;
17809 it->max_descent = descent;
17810 it->max_phys_ascent = phys_ascent;
17811 it->max_phys_descent = phys_descent;
17812 }
17813
17814 break;
17815 }
17816 else if (new_x > it->first_visible_x)
17817 {
17818 /* Increment number of glyphs actually displayed. */
17819 ++it->hpos;
17820
17821 /* Record the maximum and minimum buffer positions
17822 seen so far in glyphs that will be displayed by
17823 this row. */
17824 if (it->bidi_p)
17825 RECORD_MAX_MIN_POS (it);
17826
17827 if (x < it->first_visible_x)
17828 /* Glyph is partially visible, i.e. row starts at
17829 negative X position. */
17830 row->x = x - it->first_visible_x;
17831 }
17832 else
17833 {
17834 /* Glyph is completely off the left margin of the
17835 window. This should not happen because of the
17836 move_it_in_display_line at the start of this
17837 function, unless the text display area of the
17838 window is empty. */
17839 xassert (it->first_visible_x <= it->last_visible_x);
17840 }
17841 }
17842
17843 row->ascent = max (row->ascent, it->max_ascent);
17844 row->height = max (row->height, it->max_ascent + it->max_descent);
17845 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17846 row->phys_height = max (row->phys_height,
17847 it->max_phys_ascent + it->max_phys_descent);
17848 row->extra_line_spacing = max (row->extra_line_spacing,
17849 it->max_extra_line_spacing);
17850
17851 /* End of this display line if row is continued. */
17852 if (row->continued_p || row->ends_at_zv_p)
17853 break;
17854 }
17855
17856 at_end_of_line:
17857 /* Is this a line end? If yes, we're also done, after making
17858 sure that a non-default face is extended up to the right
17859 margin of the window. */
17860 if (ITERATOR_AT_END_OF_LINE_P (it))
17861 {
17862 int used_before = row->used[TEXT_AREA];
17863
17864 row->ends_in_newline_from_string_p = STRINGP (it->object);
17865
17866 /* Add a space at the end of the line that is used to
17867 display the cursor there. */
17868 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17869 append_space_for_newline (it, 0);
17870
17871 /* Extend the face to the end of the line. */
17872 extend_face_to_end_of_line (it);
17873
17874 /* Make sure we have the position. */
17875 if (used_before == 0)
17876 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17877
17878 /* Record the position of the newline, for use in
17879 find_row_edges. */
17880 it->eol_pos = it->current.pos;
17881
17882 /* Consume the line end. This skips over invisible lines. */
17883 set_iterator_to_next (it, 1);
17884 it->continuation_lines_width = 0;
17885 break;
17886 }
17887
17888 /* Proceed with next display element. Note that this skips
17889 over lines invisible because of selective display. */
17890 set_iterator_to_next (it, 1);
17891
17892 /* If we truncate lines, we are done when the last displayed
17893 glyphs reach past the right margin of the window. */
17894 if (it->line_wrap == TRUNCATE
17895 && (FRAME_WINDOW_P (it->f)
17896 ? (it->current_x >= it->last_visible_x)
17897 : (it->current_x > it->last_visible_x)))
17898 {
17899 /* Maybe add truncation glyphs. */
17900 if (!FRAME_WINDOW_P (it->f))
17901 {
17902 int i, n;
17903
17904 if (!row->reversed_p)
17905 {
17906 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17907 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17908 break;
17909 }
17910 else
17911 {
17912 for (i = 0; i < row->used[TEXT_AREA]; i++)
17913 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17914 break;
17915 /* Remove any padding glyphs at the front of ROW, to
17916 make room for the truncation glyphs we will be
17917 adding below. The loop below always inserts at
17918 least one truncation glyph, so also remove the
17919 last glyph added to ROW. */
17920 unproduce_glyphs (it, i + 1);
17921 /* Adjust i for the loop below. */
17922 i = row->used[TEXT_AREA] - (i + 1);
17923 }
17924
17925 for (n = row->used[TEXT_AREA]; i < n; ++i)
17926 {
17927 row->used[TEXT_AREA] = i;
17928 produce_special_glyphs (it, IT_TRUNCATION);
17929 }
17930 }
17931 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17932 {
17933 /* Don't truncate if we can overflow newline into fringe. */
17934 if (!get_next_display_element (it))
17935 {
17936 it->continuation_lines_width = 0;
17937 row->ends_at_zv_p = 1;
17938 row->exact_window_width_line_p = 1;
17939 break;
17940 }
17941 if (ITERATOR_AT_END_OF_LINE_P (it))
17942 {
17943 row->exact_window_width_line_p = 1;
17944 goto at_end_of_line;
17945 }
17946 }
17947
17948 row->truncated_on_right_p = 1;
17949 it->continuation_lines_width = 0;
17950 reseat_at_next_visible_line_start (it, 0);
17951 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17952 it->hpos = hpos_before;
17953 it->current_x = x_before;
17954 break;
17955 }
17956 }
17957
17958 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17959 at the left window margin. */
17960 if (it->first_visible_x
17961 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17962 {
17963 if (!FRAME_WINDOW_P (it->f))
17964 insert_left_trunc_glyphs (it);
17965 row->truncated_on_left_p = 1;
17966 }
17967
17968 /* Remember the position at which this line ends.
17969
17970 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17971 cannot be before the call to find_row_edges below, since that is
17972 where these positions are determined. */
17973 row->end = it->current;
17974 if (!it->bidi_p)
17975 {
17976 row->minpos = row->start.pos;
17977 row->maxpos = row->end.pos;
17978 }
17979 else
17980 {
17981 /* ROW->minpos and ROW->maxpos must be the smallest and
17982 `1 + the largest' buffer positions in ROW. But if ROW was
17983 bidi-reordered, these two positions can be anywhere in the
17984 row, so we must determine them now. */
17985 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17986 }
17987
17988 /* If the start of this line is the overlay arrow-position, then
17989 mark this glyph row as the one containing the overlay arrow.
17990 This is clearly a mess with variable size fonts. It would be
17991 better to let it be displayed like cursors under X. */
17992 if ((row->displays_text_p || !overlay_arrow_seen)
17993 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17994 !NILP (overlay_arrow_string)))
17995 {
17996 /* Overlay arrow in window redisplay is a fringe bitmap. */
17997 if (STRINGP (overlay_arrow_string))
17998 {
17999 struct glyph_row *arrow_row
18000 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18001 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18002 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18003 struct glyph *p = row->glyphs[TEXT_AREA];
18004 struct glyph *p2, *end;
18005
18006 /* Copy the arrow glyphs. */
18007 while (glyph < arrow_end)
18008 *p++ = *glyph++;
18009
18010 /* Throw away padding glyphs. */
18011 p2 = p;
18012 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18013 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18014 ++p2;
18015 if (p2 > p)
18016 {
18017 while (p2 < end)
18018 *p++ = *p2++;
18019 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18020 }
18021 }
18022 else
18023 {
18024 xassert (INTEGERP (overlay_arrow_string));
18025 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18026 }
18027 overlay_arrow_seen = 1;
18028 }
18029
18030 /* Compute pixel dimensions of this line. */
18031 compute_line_metrics (it);
18032
18033 /* Record whether this row ends inside an ellipsis. */
18034 row->ends_in_ellipsis_p
18035 = (it->method == GET_FROM_DISPLAY_VECTOR
18036 && it->ellipsis_p);
18037
18038 /* Save fringe bitmaps in this row. */
18039 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18040 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18041 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18042 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18043
18044 it->left_user_fringe_bitmap = 0;
18045 it->left_user_fringe_face_id = 0;
18046 it->right_user_fringe_bitmap = 0;
18047 it->right_user_fringe_face_id = 0;
18048
18049 /* Maybe set the cursor. */
18050 cvpos = it->w->cursor.vpos;
18051 if ((cvpos < 0
18052 /* In bidi-reordered rows, keep checking for proper cursor
18053 position even if one has been found already, because buffer
18054 positions in such rows change non-linearly with ROW->VPOS,
18055 when a line is continued. One exception: when we are at ZV,
18056 display cursor on the first suitable glyph row, since all
18057 the empty rows after that also have their position set to ZV. */
18058 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18059 lines' rows is implemented for bidi-reordered rows. */
18060 || (it->bidi_p
18061 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18062 && PT >= MATRIX_ROW_START_CHARPOS (row)
18063 && PT <= MATRIX_ROW_END_CHARPOS (row)
18064 && cursor_row_p (it->w, row))
18065 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18066
18067 /* Highlight trailing whitespace. */
18068 if (!NILP (Vshow_trailing_whitespace))
18069 highlight_trailing_whitespace (it->f, it->glyph_row);
18070
18071 /* Prepare for the next line. This line starts horizontally at (X
18072 HPOS) = (0 0). Vertical positions are incremented. As a
18073 convenience for the caller, IT->glyph_row is set to the next
18074 row to be used. */
18075 it->current_x = it->hpos = 0;
18076 it->current_y += row->height;
18077 SET_TEXT_POS (it->eol_pos, 0, 0);
18078 ++it->vpos;
18079 ++it->glyph_row;
18080 /* The next row should by default use the same value of the
18081 reversed_p flag as this one. set_iterator_to_next decides when
18082 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18083 the flag accordingly. */
18084 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18085 it->glyph_row->reversed_p = row->reversed_p;
18086 it->start = row->end;
18087 return row->displays_text_p;
18088
18089 #undef RECORD_MAX_MIN_POS
18090 }
18091
18092 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18093 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18094 doc: /* Return paragraph direction at point in BUFFER.
18095 Value is either `left-to-right' or `right-to-left'.
18096 If BUFFER is omitted or nil, it defaults to the current buffer.
18097
18098 Paragraph direction determines how the text in the paragraph is displayed.
18099 In left-to-right paragraphs, text begins at the left margin of the window
18100 and the reading direction is generally left to right. In right-to-left
18101 paragraphs, text begins at the right margin and is read from right to left.
18102
18103 See also `bidi-paragraph-direction'. */)
18104 (Lisp_Object buffer)
18105 {
18106 struct buffer *buf;
18107 struct buffer *old;
18108
18109 if (NILP (buffer))
18110 buf = current_buffer;
18111 else
18112 {
18113 CHECK_BUFFER (buffer);
18114 buf = XBUFFER (buffer);
18115 old = current_buffer;
18116 }
18117
18118 if (NILP (buf->bidi_display_reordering))
18119 return Qleft_to_right;
18120 else if (!NILP (buf->bidi_paragraph_direction))
18121 return buf->bidi_paragraph_direction;
18122 else
18123 {
18124 /* Determine the direction from buffer text. We could try to
18125 use current_matrix if it is up to date, but this seems fast
18126 enough as it is. */
18127 struct bidi_it itb;
18128 EMACS_INT pos = BUF_PT (buf);
18129 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18130 int c;
18131
18132 if (buf != current_buffer)
18133 set_buffer_temp (buf);
18134 /* bidi_paragraph_init finds the base direction of the paragraph
18135 by searching forward from paragraph start. We need the base
18136 direction of the current or _previous_ paragraph, so we need
18137 to make sure we are within that paragraph. To that end, find
18138 the previous non-empty line. */
18139 if (pos >= ZV && pos > BEGV)
18140 {
18141 pos--;
18142 bytepos = CHAR_TO_BYTE (pos);
18143 }
18144 while ((c = FETCH_BYTE (bytepos)) == '\n'
18145 || c == ' ' || c == '\t' || c == '\f')
18146 {
18147 if (bytepos <= BEGV_BYTE)
18148 break;
18149 bytepos--;
18150 pos--;
18151 }
18152 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18153 bytepos--;
18154 itb.charpos = pos;
18155 itb.bytepos = bytepos;
18156 itb.first_elt = 1;
18157 itb.separator_limit = -1;
18158 itb.paragraph_dir = NEUTRAL_DIR;
18159
18160 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18161 if (buf != current_buffer)
18162 set_buffer_temp (old);
18163 switch (itb.paragraph_dir)
18164 {
18165 case L2R:
18166 return Qleft_to_right;
18167 break;
18168 case R2L:
18169 return Qright_to_left;
18170 break;
18171 default:
18172 abort ();
18173 }
18174 }
18175 }
18176
18177
18178 \f
18179 /***********************************************************************
18180 Menu Bar
18181 ***********************************************************************/
18182
18183 /* Redisplay the menu bar in the frame for window W.
18184
18185 The menu bar of X frames that don't have X toolkit support is
18186 displayed in a special window W->frame->menu_bar_window.
18187
18188 The menu bar of terminal frames is treated specially as far as
18189 glyph matrices are concerned. Menu bar lines are not part of
18190 windows, so the update is done directly on the frame matrix rows
18191 for the menu bar. */
18192
18193 static void
18194 display_menu_bar (struct window *w)
18195 {
18196 struct frame *f = XFRAME (WINDOW_FRAME (w));
18197 struct it it;
18198 Lisp_Object items;
18199 int i;
18200
18201 /* Don't do all this for graphical frames. */
18202 #ifdef HAVE_NTGUI
18203 if (FRAME_W32_P (f))
18204 return;
18205 #endif
18206 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18207 if (FRAME_X_P (f))
18208 return;
18209 #endif
18210
18211 #ifdef HAVE_NS
18212 if (FRAME_NS_P (f))
18213 return;
18214 #endif /* HAVE_NS */
18215
18216 #ifdef USE_X_TOOLKIT
18217 xassert (!FRAME_WINDOW_P (f));
18218 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18219 it.first_visible_x = 0;
18220 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18221 #else /* not USE_X_TOOLKIT */
18222 if (FRAME_WINDOW_P (f))
18223 {
18224 /* Menu bar lines are displayed in the desired matrix of the
18225 dummy window menu_bar_window. */
18226 struct window *menu_w;
18227 xassert (WINDOWP (f->menu_bar_window));
18228 menu_w = XWINDOW (f->menu_bar_window);
18229 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18230 MENU_FACE_ID);
18231 it.first_visible_x = 0;
18232 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18233 }
18234 else
18235 {
18236 /* This is a TTY frame, i.e. character hpos/vpos are used as
18237 pixel x/y. */
18238 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18239 MENU_FACE_ID);
18240 it.first_visible_x = 0;
18241 it.last_visible_x = FRAME_COLS (f);
18242 }
18243 #endif /* not USE_X_TOOLKIT */
18244
18245 if (! mode_line_inverse_video)
18246 /* Force the menu-bar to be displayed in the default face. */
18247 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18248
18249 /* Clear all rows of the menu bar. */
18250 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18251 {
18252 struct glyph_row *row = it.glyph_row + i;
18253 clear_glyph_row (row);
18254 row->enabled_p = 1;
18255 row->full_width_p = 1;
18256 }
18257
18258 /* Display all items of the menu bar. */
18259 items = FRAME_MENU_BAR_ITEMS (it.f);
18260 for (i = 0; i < XVECTOR (items)->size; i += 4)
18261 {
18262 Lisp_Object string;
18263
18264 /* Stop at nil string. */
18265 string = AREF (items, i + 1);
18266 if (NILP (string))
18267 break;
18268
18269 /* Remember where item was displayed. */
18270 ASET (items, i + 3, make_number (it.hpos));
18271
18272 /* Display the item, pad with one space. */
18273 if (it.current_x < it.last_visible_x)
18274 display_string (NULL, string, Qnil, 0, 0, &it,
18275 SCHARS (string) + 1, 0, 0, -1);
18276 }
18277
18278 /* Fill out the line with spaces. */
18279 if (it.current_x < it.last_visible_x)
18280 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18281
18282 /* Compute the total height of the lines. */
18283 compute_line_metrics (&it);
18284 }
18285
18286
18287 \f
18288 /***********************************************************************
18289 Mode Line
18290 ***********************************************************************/
18291
18292 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18293 FORCE is non-zero, redisplay mode lines unconditionally.
18294 Otherwise, redisplay only mode lines that are garbaged. Value is
18295 the number of windows whose mode lines were redisplayed. */
18296
18297 static int
18298 redisplay_mode_lines (Lisp_Object window, int force)
18299 {
18300 int nwindows = 0;
18301
18302 while (!NILP (window))
18303 {
18304 struct window *w = XWINDOW (window);
18305
18306 if (WINDOWP (w->hchild))
18307 nwindows += redisplay_mode_lines (w->hchild, force);
18308 else if (WINDOWP (w->vchild))
18309 nwindows += redisplay_mode_lines (w->vchild, force);
18310 else if (force
18311 || FRAME_GARBAGED_P (XFRAME (w->frame))
18312 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18313 {
18314 struct text_pos lpoint;
18315 struct buffer *old = current_buffer;
18316
18317 /* Set the window's buffer for the mode line display. */
18318 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18319 set_buffer_internal_1 (XBUFFER (w->buffer));
18320
18321 /* Point refers normally to the selected window. For any
18322 other window, set up appropriate value. */
18323 if (!EQ (window, selected_window))
18324 {
18325 struct text_pos pt;
18326
18327 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18328 if (CHARPOS (pt) < BEGV)
18329 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18330 else if (CHARPOS (pt) > (ZV - 1))
18331 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18332 else
18333 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18334 }
18335
18336 /* Display mode lines. */
18337 clear_glyph_matrix (w->desired_matrix);
18338 if (display_mode_lines (w))
18339 {
18340 ++nwindows;
18341 w->must_be_updated_p = 1;
18342 }
18343
18344 /* Restore old settings. */
18345 set_buffer_internal_1 (old);
18346 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18347 }
18348
18349 window = w->next;
18350 }
18351
18352 return nwindows;
18353 }
18354
18355
18356 /* Display the mode and/or header line of window W. Value is the
18357 sum number of mode lines and header lines displayed. */
18358
18359 static int
18360 display_mode_lines (struct window *w)
18361 {
18362 Lisp_Object old_selected_window, old_selected_frame;
18363 int n = 0;
18364
18365 old_selected_frame = selected_frame;
18366 selected_frame = w->frame;
18367 old_selected_window = selected_window;
18368 XSETWINDOW (selected_window, w);
18369
18370 /* These will be set while the mode line specs are processed. */
18371 line_number_displayed = 0;
18372 w->column_number_displayed = Qnil;
18373
18374 if (WINDOW_WANTS_MODELINE_P (w))
18375 {
18376 struct window *sel_w = XWINDOW (old_selected_window);
18377
18378 /* Select mode line face based on the real selected window. */
18379 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18380 current_buffer->mode_line_format);
18381 ++n;
18382 }
18383
18384 if (WINDOW_WANTS_HEADER_LINE_P (w))
18385 {
18386 display_mode_line (w, HEADER_LINE_FACE_ID,
18387 current_buffer->header_line_format);
18388 ++n;
18389 }
18390
18391 selected_frame = old_selected_frame;
18392 selected_window = old_selected_window;
18393 return n;
18394 }
18395
18396
18397 /* Display mode or header line of window W. FACE_ID specifies which
18398 line to display; it is either MODE_LINE_FACE_ID or
18399 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18400 display. Value is the pixel height of the mode/header line
18401 displayed. */
18402
18403 static int
18404 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18405 {
18406 struct it it;
18407 struct face *face;
18408 int count = SPECPDL_INDEX ();
18409
18410 init_iterator (&it, w, -1, -1, NULL, face_id);
18411 /* Don't extend on a previously drawn mode-line.
18412 This may happen if called from pos_visible_p. */
18413 it.glyph_row->enabled_p = 0;
18414 prepare_desired_row (it.glyph_row);
18415
18416 it.glyph_row->mode_line_p = 1;
18417
18418 if (! mode_line_inverse_video)
18419 /* Force the mode-line to be displayed in the default face. */
18420 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18421
18422 record_unwind_protect (unwind_format_mode_line,
18423 format_mode_line_unwind_data (NULL, Qnil, 0));
18424
18425 mode_line_target = MODE_LINE_DISPLAY;
18426
18427 /* Temporarily make frame's keyboard the current kboard so that
18428 kboard-local variables in the mode_line_format will get the right
18429 values. */
18430 push_kboard (FRAME_KBOARD (it.f));
18431 record_unwind_save_match_data ();
18432 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18433 pop_kboard ();
18434
18435 unbind_to (count, Qnil);
18436
18437 /* Fill up with spaces. */
18438 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18439
18440 compute_line_metrics (&it);
18441 it.glyph_row->full_width_p = 1;
18442 it.glyph_row->continued_p = 0;
18443 it.glyph_row->truncated_on_left_p = 0;
18444 it.glyph_row->truncated_on_right_p = 0;
18445
18446 /* Make a 3D mode-line have a shadow at its right end. */
18447 face = FACE_FROM_ID (it.f, face_id);
18448 extend_face_to_end_of_line (&it);
18449 if (face->box != FACE_NO_BOX)
18450 {
18451 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18452 + it.glyph_row->used[TEXT_AREA] - 1);
18453 last->right_box_line_p = 1;
18454 }
18455
18456 return it.glyph_row->height;
18457 }
18458
18459 /* Move element ELT in LIST to the front of LIST.
18460 Return the updated list. */
18461
18462 static Lisp_Object
18463 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18464 {
18465 register Lisp_Object tail, prev;
18466 register Lisp_Object tem;
18467
18468 tail = list;
18469 prev = Qnil;
18470 while (CONSP (tail))
18471 {
18472 tem = XCAR (tail);
18473
18474 if (EQ (elt, tem))
18475 {
18476 /* Splice out the link TAIL. */
18477 if (NILP (prev))
18478 list = XCDR (tail);
18479 else
18480 Fsetcdr (prev, XCDR (tail));
18481
18482 /* Now make it the first. */
18483 Fsetcdr (tail, list);
18484 return tail;
18485 }
18486 else
18487 prev = tail;
18488 tail = XCDR (tail);
18489 QUIT;
18490 }
18491
18492 /* Not found--return unchanged LIST. */
18493 return list;
18494 }
18495
18496 /* Contribute ELT to the mode line for window IT->w. How it
18497 translates into text depends on its data type.
18498
18499 IT describes the display environment in which we display, as usual.
18500
18501 DEPTH is the depth in recursion. It is used to prevent
18502 infinite recursion here.
18503
18504 FIELD_WIDTH is the number of characters the display of ELT should
18505 occupy in the mode line, and PRECISION is the maximum number of
18506 characters to display from ELT's representation. See
18507 display_string for details.
18508
18509 Returns the hpos of the end of the text generated by ELT.
18510
18511 PROPS is a property list to add to any string we encounter.
18512
18513 If RISKY is nonzero, remove (disregard) any properties in any string
18514 we encounter, and ignore :eval and :propertize.
18515
18516 The global variable `mode_line_target' determines whether the
18517 output is passed to `store_mode_line_noprop',
18518 `store_mode_line_string', or `display_string'. */
18519
18520 static int
18521 display_mode_element (struct it *it, int depth, int field_width, int precision,
18522 Lisp_Object elt, Lisp_Object props, int risky)
18523 {
18524 int n = 0, field, prec;
18525 int literal = 0;
18526
18527 tail_recurse:
18528 if (depth > 100)
18529 elt = build_string ("*too-deep*");
18530
18531 depth++;
18532
18533 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18534 {
18535 case Lisp_String:
18536 {
18537 /* A string: output it and check for %-constructs within it. */
18538 unsigned char c;
18539 EMACS_INT offset = 0;
18540
18541 if (SCHARS (elt) > 0
18542 && (!NILP (props) || risky))
18543 {
18544 Lisp_Object oprops, aelt;
18545 oprops = Ftext_properties_at (make_number (0), elt);
18546
18547 /* If the starting string's properties are not what
18548 we want, translate the string. Also, if the string
18549 is risky, do that anyway. */
18550
18551 if (NILP (Fequal (props, oprops)) || risky)
18552 {
18553 /* If the starting string has properties,
18554 merge the specified ones onto the existing ones. */
18555 if (! NILP (oprops) && !risky)
18556 {
18557 Lisp_Object tem;
18558
18559 oprops = Fcopy_sequence (oprops);
18560 tem = props;
18561 while (CONSP (tem))
18562 {
18563 oprops = Fplist_put (oprops, XCAR (tem),
18564 XCAR (XCDR (tem)));
18565 tem = XCDR (XCDR (tem));
18566 }
18567 props = oprops;
18568 }
18569
18570 aelt = Fassoc (elt, mode_line_proptrans_alist);
18571 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18572 {
18573 /* AELT is what we want. Move it to the front
18574 without consing. */
18575 elt = XCAR (aelt);
18576 mode_line_proptrans_alist
18577 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18578 }
18579 else
18580 {
18581 Lisp_Object tem;
18582
18583 /* If AELT has the wrong props, it is useless.
18584 so get rid of it. */
18585 if (! NILP (aelt))
18586 mode_line_proptrans_alist
18587 = Fdelq (aelt, mode_line_proptrans_alist);
18588
18589 elt = Fcopy_sequence (elt);
18590 Fset_text_properties (make_number (0), Flength (elt),
18591 props, elt);
18592 /* Add this item to mode_line_proptrans_alist. */
18593 mode_line_proptrans_alist
18594 = Fcons (Fcons (elt, props),
18595 mode_line_proptrans_alist);
18596 /* Truncate mode_line_proptrans_alist
18597 to at most 50 elements. */
18598 tem = Fnthcdr (make_number (50),
18599 mode_line_proptrans_alist);
18600 if (! NILP (tem))
18601 XSETCDR (tem, Qnil);
18602 }
18603 }
18604 }
18605
18606 offset = 0;
18607
18608 if (literal)
18609 {
18610 prec = precision - n;
18611 switch (mode_line_target)
18612 {
18613 case MODE_LINE_NOPROP:
18614 case MODE_LINE_TITLE:
18615 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18616 break;
18617 case MODE_LINE_STRING:
18618 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18619 break;
18620 case MODE_LINE_DISPLAY:
18621 n += display_string (NULL, elt, Qnil, 0, 0, it,
18622 0, prec, 0, STRING_MULTIBYTE (elt));
18623 break;
18624 }
18625
18626 break;
18627 }
18628
18629 /* Handle the non-literal case. */
18630
18631 while ((precision <= 0 || n < precision)
18632 && SREF (elt, offset) != 0
18633 && (mode_line_target != MODE_LINE_DISPLAY
18634 || it->current_x < it->last_visible_x))
18635 {
18636 EMACS_INT last_offset = offset;
18637
18638 /* Advance to end of string or next format specifier. */
18639 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18640 ;
18641
18642 if (offset - 1 != last_offset)
18643 {
18644 EMACS_INT nchars, nbytes;
18645
18646 /* Output to end of string or up to '%'. Field width
18647 is length of string. Don't output more than
18648 PRECISION allows us. */
18649 offset--;
18650
18651 prec = c_string_width (SDATA (elt) + last_offset,
18652 offset - last_offset, precision - n,
18653 &nchars, &nbytes);
18654
18655 switch (mode_line_target)
18656 {
18657 case MODE_LINE_NOPROP:
18658 case MODE_LINE_TITLE:
18659 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18660 break;
18661 case MODE_LINE_STRING:
18662 {
18663 EMACS_INT bytepos = last_offset;
18664 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18665 EMACS_INT endpos = (precision <= 0
18666 ? string_byte_to_char (elt, offset)
18667 : charpos + nchars);
18668
18669 n += store_mode_line_string (NULL,
18670 Fsubstring (elt, make_number (charpos),
18671 make_number (endpos)),
18672 0, 0, 0, Qnil);
18673 }
18674 break;
18675 case MODE_LINE_DISPLAY:
18676 {
18677 EMACS_INT bytepos = last_offset;
18678 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18679
18680 if (precision <= 0)
18681 nchars = string_byte_to_char (elt, offset) - charpos;
18682 n += display_string (NULL, elt, Qnil, 0, charpos,
18683 it, 0, nchars, 0,
18684 STRING_MULTIBYTE (elt));
18685 }
18686 break;
18687 }
18688 }
18689 else /* c == '%' */
18690 {
18691 EMACS_INT percent_position = offset;
18692
18693 /* Get the specified minimum width. Zero means
18694 don't pad. */
18695 field = 0;
18696 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18697 field = field * 10 + c - '0';
18698
18699 /* Don't pad beyond the total padding allowed. */
18700 if (field_width - n > 0 && field > field_width - n)
18701 field = field_width - n;
18702
18703 /* Note that either PRECISION <= 0 or N < PRECISION. */
18704 prec = precision - n;
18705
18706 if (c == 'M')
18707 n += display_mode_element (it, depth, field, prec,
18708 Vglobal_mode_string, props,
18709 risky);
18710 else if (c != 0)
18711 {
18712 int multibyte;
18713 EMACS_INT bytepos, charpos;
18714 const unsigned char *spec;
18715 Lisp_Object string;
18716
18717 bytepos = percent_position;
18718 charpos = (STRING_MULTIBYTE (elt)
18719 ? string_byte_to_char (elt, bytepos)
18720 : bytepos);
18721 spec = decode_mode_spec (it->w, c, field, prec, &string);
18722 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18723
18724 switch (mode_line_target)
18725 {
18726 case MODE_LINE_NOPROP:
18727 case MODE_LINE_TITLE:
18728 n += store_mode_line_noprop (spec, field, prec);
18729 break;
18730 case MODE_LINE_STRING:
18731 {
18732 int len = strlen (spec);
18733 Lisp_Object tem = make_string (spec, len);
18734 props = Ftext_properties_at (make_number (charpos), elt);
18735 /* Should only keep face property in props */
18736 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18737 }
18738 break;
18739 case MODE_LINE_DISPLAY:
18740 {
18741 int nglyphs_before, nwritten;
18742
18743 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18744 nwritten = display_string (spec, string, elt,
18745 charpos, 0, it,
18746 field, prec, 0,
18747 multibyte);
18748
18749 /* Assign to the glyphs written above the
18750 string where the `%x' came from, position
18751 of the `%'. */
18752 if (nwritten > 0)
18753 {
18754 struct glyph *glyph
18755 = (it->glyph_row->glyphs[TEXT_AREA]
18756 + nglyphs_before);
18757 int i;
18758
18759 for (i = 0; i < nwritten; ++i)
18760 {
18761 glyph[i].object = elt;
18762 glyph[i].charpos = charpos;
18763 }
18764
18765 n += nwritten;
18766 }
18767 }
18768 break;
18769 }
18770 }
18771 else /* c == 0 */
18772 break;
18773 }
18774 }
18775 }
18776 break;
18777
18778 case Lisp_Symbol:
18779 /* A symbol: process the value of the symbol recursively
18780 as if it appeared here directly. Avoid error if symbol void.
18781 Special case: if value of symbol is a string, output the string
18782 literally. */
18783 {
18784 register Lisp_Object tem;
18785
18786 /* If the variable is not marked as risky to set
18787 then its contents are risky to use. */
18788 if (NILP (Fget (elt, Qrisky_local_variable)))
18789 risky = 1;
18790
18791 tem = Fboundp (elt);
18792 if (!NILP (tem))
18793 {
18794 tem = Fsymbol_value (elt);
18795 /* If value is a string, output that string literally:
18796 don't check for % within it. */
18797 if (STRINGP (tem))
18798 literal = 1;
18799
18800 if (!EQ (tem, elt))
18801 {
18802 /* Give up right away for nil or t. */
18803 elt = tem;
18804 goto tail_recurse;
18805 }
18806 }
18807 }
18808 break;
18809
18810 case Lisp_Cons:
18811 {
18812 register Lisp_Object car, tem;
18813
18814 /* A cons cell: five distinct cases.
18815 If first element is :eval or :propertize, do something special.
18816 If first element is a string or a cons, process all the elements
18817 and effectively concatenate them.
18818 If first element is a negative number, truncate displaying cdr to
18819 at most that many characters. If positive, pad (with spaces)
18820 to at least that many characters.
18821 If first element is a symbol, process the cadr or caddr recursively
18822 according to whether the symbol's value is non-nil or nil. */
18823 car = XCAR (elt);
18824 if (EQ (car, QCeval))
18825 {
18826 /* An element of the form (:eval FORM) means evaluate FORM
18827 and use the result as mode line elements. */
18828
18829 if (risky)
18830 break;
18831
18832 if (CONSP (XCDR (elt)))
18833 {
18834 Lisp_Object spec;
18835 spec = safe_eval (XCAR (XCDR (elt)));
18836 n += display_mode_element (it, depth, field_width - n,
18837 precision - n, spec, props,
18838 risky);
18839 }
18840 }
18841 else if (EQ (car, QCpropertize))
18842 {
18843 /* An element of the form (:propertize ELT PROPS...)
18844 means display ELT but applying properties PROPS. */
18845
18846 if (risky)
18847 break;
18848
18849 if (CONSP (XCDR (elt)))
18850 n += display_mode_element (it, depth, field_width - n,
18851 precision - n, XCAR (XCDR (elt)),
18852 XCDR (XCDR (elt)), risky);
18853 }
18854 else if (SYMBOLP (car))
18855 {
18856 tem = Fboundp (car);
18857 elt = XCDR (elt);
18858 if (!CONSP (elt))
18859 goto invalid;
18860 /* elt is now the cdr, and we know it is a cons cell.
18861 Use its car if CAR has a non-nil value. */
18862 if (!NILP (tem))
18863 {
18864 tem = Fsymbol_value (car);
18865 if (!NILP (tem))
18866 {
18867 elt = XCAR (elt);
18868 goto tail_recurse;
18869 }
18870 }
18871 /* Symbol's value is nil (or symbol is unbound)
18872 Get the cddr of the original list
18873 and if possible find the caddr and use that. */
18874 elt = XCDR (elt);
18875 if (NILP (elt))
18876 break;
18877 else if (!CONSP (elt))
18878 goto invalid;
18879 elt = XCAR (elt);
18880 goto tail_recurse;
18881 }
18882 else if (INTEGERP (car))
18883 {
18884 register int lim = XINT (car);
18885 elt = XCDR (elt);
18886 if (lim < 0)
18887 {
18888 /* Negative int means reduce maximum width. */
18889 if (precision <= 0)
18890 precision = -lim;
18891 else
18892 precision = min (precision, -lim);
18893 }
18894 else if (lim > 0)
18895 {
18896 /* Padding specified. Don't let it be more than
18897 current maximum. */
18898 if (precision > 0)
18899 lim = min (precision, lim);
18900
18901 /* If that's more padding than already wanted, queue it.
18902 But don't reduce padding already specified even if
18903 that is beyond the current truncation point. */
18904 field_width = max (lim, field_width);
18905 }
18906 goto tail_recurse;
18907 }
18908 else if (STRINGP (car) || CONSP (car))
18909 {
18910 Lisp_Object halftail = elt;
18911 int len = 0;
18912
18913 while (CONSP (elt)
18914 && (precision <= 0 || n < precision))
18915 {
18916 n += display_mode_element (it, depth,
18917 /* Do padding only after the last
18918 element in the list. */
18919 (! CONSP (XCDR (elt))
18920 ? field_width - n
18921 : 0),
18922 precision - n, XCAR (elt),
18923 props, risky);
18924 elt = XCDR (elt);
18925 len++;
18926 if ((len & 1) == 0)
18927 halftail = XCDR (halftail);
18928 /* Check for cycle. */
18929 if (EQ (halftail, elt))
18930 break;
18931 }
18932 }
18933 }
18934 break;
18935
18936 default:
18937 invalid:
18938 elt = build_string ("*invalid*");
18939 goto tail_recurse;
18940 }
18941
18942 /* Pad to FIELD_WIDTH. */
18943 if (field_width > 0 && n < field_width)
18944 {
18945 switch (mode_line_target)
18946 {
18947 case MODE_LINE_NOPROP:
18948 case MODE_LINE_TITLE:
18949 n += store_mode_line_noprop ("", field_width - n, 0);
18950 break;
18951 case MODE_LINE_STRING:
18952 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18953 break;
18954 case MODE_LINE_DISPLAY:
18955 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18956 0, 0, 0);
18957 break;
18958 }
18959 }
18960
18961 return n;
18962 }
18963
18964 /* Store a mode-line string element in mode_line_string_list.
18965
18966 If STRING is non-null, display that C string. Otherwise, the Lisp
18967 string LISP_STRING is displayed.
18968
18969 FIELD_WIDTH is the minimum number of output glyphs to produce.
18970 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18971 with spaces. FIELD_WIDTH <= 0 means don't pad.
18972
18973 PRECISION is the maximum number of characters to output from
18974 STRING. PRECISION <= 0 means don't truncate the string.
18975
18976 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18977 properties to the string.
18978
18979 PROPS are the properties to add to the string.
18980 The mode_line_string_face face property is always added to the string.
18981 */
18982
18983 static int
18984 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18985 int field_width, int precision, Lisp_Object props)
18986 {
18987 EMACS_INT len;
18988 int n = 0;
18989
18990 if (string != NULL)
18991 {
18992 len = strlen (string);
18993 if (precision > 0 && len > precision)
18994 len = precision;
18995 lisp_string = make_string (string, len);
18996 if (NILP (props))
18997 props = mode_line_string_face_prop;
18998 else if (!NILP (mode_line_string_face))
18999 {
19000 Lisp_Object face = Fplist_get (props, Qface);
19001 props = Fcopy_sequence (props);
19002 if (NILP (face))
19003 face = mode_line_string_face;
19004 else
19005 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19006 props = Fplist_put (props, Qface, face);
19007 }
19008 Fadd_text_properties (make_number (0), make_number (len),
19009 props, lisp_string);
19010 }
19011 else
19012 {
19013 len = XFASTINT (Flength (lisp_string));
19014 if (precision > 0 && len > precision)
19015 {
19016 len = precision;
19017 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19018 precision = -1;
19019 }
19020 if (!NILP (mode_line_string_face))
19021 {
19022 Lisp_Object face;
19023 if (NILP (props))
19024 props = Ftext_properties_at (make_number (0), lisp_string);
19025 face = Fplist_get (props, Qface);
19026 if (NILP (face))
19027 face = mode_line_string_face;
19028 else
19029 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19030 props = Fcons (Qface, Fcons (face, Qnil));
19031 if (copy_string)
19032 lisp_string = Fcopy_sequence (lisp_string);
19033 }
19034 if (!NILP (props))
19035 Fadd_text_properties (make_number (0), make_number (len),
19036 props, lisp_string);
19037 }
19038
19039 if (len > 0)
19040 {
19041 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19042 n += len;
19043 }
19044
19045 if (field_width > len)
19046 {
19047 field_width -= len;
19048 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19049 if (!NILP (props))
19050 Fadd_text_properties (make_number (0), make_number (field_width),
19051 props, lisp_string);
19052 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19053 n += field_width;
19054 }
19055
19056 return n;
19057 }
19058
19059
19060 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19061 1, 4, 0,
19062 doc: /* Format a string out of a mode line format specification.
19063 First arg FORMAT specifies the mode line format (see `mode-line-format'
19064 for details) to use.
19065
19066 Optional second arg FACE specifies the face property to put
19067 on all characters for which no face is specified.
19068 The value t means whatever face the window's mode line currently uses
19069 \(either `mode-line' or `mode-line-inactive', depending).
19070 A value of nil means the default is no face property.
19071 If FACE is an integer, the value string has no text properties.
19072
19073 Optional third and fourth args WINDOW and BUFFER specify the window
19074 and buffer to use as the context for the formatting (defaults
19075 are the selected window and the window's buffer). */)
19076 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
19077 {
19078 struct it it;
19079 int len;
19080 struct window *w;
19081 struct buffer *old_buffer = NULL;
19082 int face_id = -1;
19083 int no_props = INTEGERP (face);
19084 int count = SPECPDL_INDEX ();
19085 Lisp_Object str;
19086 int string_start = 0;
19087
19088 if (NILP (window))
19089 window = selected_window;
19090 CHECK_WINDOW (window);
19091 w = XWINDOW (window);
19092
19093 if (NILP (buffer))
19094 buffer = w->buffer;
19095 CHECK_BUFFER (buffer);
19096
19097 /* Make formatting the modeline a non-op when noninteractive, otherwise
19098 there will be problems later caused by a partially initialized frame. */
19099 if (NILP (format) || noninteractive)
19100 return empty_unibyte_string;
19101
19102 if (no_props)
19103 face = Qnil;
19104
19105 if (!NILP (face))
19106 {
19107 if (EQ (face, Qt))
19108 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
19109 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
19110 }
19111
19112 if (face_id < 0)
19113 face_id = DEFAULT_FACE_ID;
19114
19115 if (XBUFFER (buffer) != current_buffer)
19116 old_buffer = current_buffer;
19117
19118 /* Save things including mode_line_proptrans_alist,
19119 and set that to nil so that we don't alter the outer value. */
19120 record_unwind_protect (unwind_format_mode_line,
19121 format_mode_line_unwind_data
19122 (old_buffer, selected_window, 1));
19123 mode_line_proptrans_alist = Qnil;
19124
19125 Fselect_window (window, Qt);
19126 if (old_buffer)
19127 set_buffer_internal_1 (XBUFFER (buffer));
19128
19129 init_iterator (&it, w, -1, -1, NULL, face_id);
19130
19131 if (no_props)
19132 {
19133 mode_line_target = MODE_LINE_NOPROP;
19134 mode_line_string_face_prop = Qnil;
19135 mode_line_string_list = Qnil;
19136 string_start = MODE_LINE_NOPROP_LEN (0);
19137 }
19138 else
19139 {
19140 mode_line_target = MODE_LINE_STRING;
19141 mode_line_string_list = Qnil;
19142 mode_line_string_face = face;
19143 mode_line_string_face_prop
19144 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19145 }
19146
19147 push_kboard (FRAME_KBOARD (it.f));
19148 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19149 pop_kboard ();
19150
19151 if (no_props)
19152 {
19153 len = MODE_LINE_NOPROP_LEN (string_start);
19154 str = make_string (mode_line_noprop_buf + string_start, len);
19155 }
19156 else
19157 {
19158 mode_line_string_list = Fnreverse (mode_line_string_list);
19159 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19160 empty_unibyte_string);
19161 }
19162
19163 unbind_to (count, Qnil);
19164 return str;
19165 }
19166
19167 /* Write a null-terminated, right justified decimal representation of
19168 the positive integer D to BUF using a minimal field width WIDTH. */
19169
19170 static void
19171 pint2str (register char *buf, register int width, register int d)
19172 {
19173 register char *p = buf;
19174
19175 if (d <= 0)
19176 *p++ = '0';
19177 else
19178 {
19179 while (d > 0)
19180 {
19181 *p++ = d % 10 + '0';
19182 d /= 10;
19183 }
19184 }
19185
19186 for (width -= (int) (p - buf); width > 0; --width)
19187 *p++ = ' ';
19188 *p-- = '\0';
19189 while (p > buf)
19190 {
19191 d = *buf;
19192 *buf++ = *p;
19193 *p-- = d;
19194 }
19195 }
19196
19197 /* Write a null-terminated, right justified decimal and "human
19198 readable" representation of the nonnegative integer D to BUF using
19199 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19200
19201 static const char power_letter[] =
19202 {
19203 0, /* not used */
19204 'k', /* kilo */
19205 'M', /* mega */
19206 'G', /* giga */
19207 'T', /* tera */
19208 'P', /* peta */
19209 'E', /* exa */
19210 'Z', /* zetta */
19211 'Y' /* yotta */
19212 };
19213
19214 static void
19215 pint2hrstr (char *buf, int width, int d)
19216 {
19217 /* We aim to represent the nonnegative integer D as
19218 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19219 int quotient = d;
19220 int remainder = 0;
19221 /* -1 means: do not use TENTHS. */
19222 int tenths = -1;
19223 int exponent = 0;
19224
19225 /* Length of QUOTIENT.TENTHS as a string. */
19226 int length;
19227
19228 char * psuffix;
19229 char * p;
19230
19231 if (1000 <= quotient)
19232 {
19233 /* Scale to the appropriate EXPONENT. */
19234 do
19235 {
19236 remainder = quotient % 1000;
19237 quotient /= 1000;
19238 exponent++;
19239 }
19240 while (1000 <= quotient);
19241
19242 /* Round to nearest and decide whether to use TENTHS or not. */
19243 if (quotient <= 9)
19244 {
19245 tenths = remainder / 100;
19246 if (50 <= remainder % 100)
19247 {
19248 if (tenths < 9)
19249 tenths++;
19250 else
19251 {
19252 quotient++;
19253 if (quotient == 10)
19254 tenths = -1;
19255 else
19256 tenths = 0;
19257 }
19258 }
19259 }
19260 else
19261 if (500 <= remainder)
19262 {
19263 if (quotient < 999)
19264 quotient++;
19265 else
19266 {
19267 quotient = 1;
19268 exponent++;
19269 tenths = 0;
19270 }
19271 }
19272 }
19273
19274 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19275 if (tenths == -1 && quotient <= 99)
19276 if (quotient <= 9)
19277 length = 1;
19278 else
19279 length = 2;
19280 else
19281 length = 3;
19282 p = psuffix = buf + max (width, length);
19283
19284 /* Print EXPONENT. */
19285 if (exponent)
19286 *psuffix++ = power_letter[exponent];
19287 *psuffix = '\0';
19288
19289 /* Print TENTHS. */
19290 if (tenths >= 0)
19291 {
19292 *--p = '0' + tenths;
19293 *--p = '.';
19294 }
19295
19296 /* Print QUOTIENT. */
19297 do
19298 {
19299 int digit = quotient % 10;
19300 *--p = '0' + digit;
19301 }
19302 while ((quotient /= 10) != 0);
19303
19304 /* Print leading spaces. */
19305 while (buf < p)
19306 *--p = ' ';
19307 }
19308
19309 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19310 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19311 type of CODING_SYSTEM. Return updated pointer into BUF. */
19312
19313 static unsigned char invalid_eol_type[] = "(*invalid*)";
19314
19315 static char *
19316 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19317 {
19318 Lisp_Object val;
19319 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19320 const unsigned char *eol_str;
19321 int eol_str_len;
19322 /* The EOL conversion we are using. */
19323 Lisp_Object eoltype;
19324
19325 val = CODING_SYSTEM_SPEC (coding_system);
19326 eoltype = Qnil;
19327
19328 if (!VECTORP (val)) /* Not yet decided. */
19329 {
19330 if (multibyte)
19331 *buf++ = '-';
19332 if (eol_flag)
19333 eoltype = eol_mnemonic_undecided;
19334 /* Don't mention EOL conversion if it isn't decided. */
19335 }
19336 else
19337 {
19338 Lisp_Object attrs;
19339 Lisp_Object eolvalue;
19340
19341 attrs = AREF (val, 0);
19342 eolvalue = AREF (val, 2);
19343
19344 if (multibyte)
19345 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19346
19347 if (eol_flag)
19348 {
19349 /* The EOL conversion that is normal on this system. */
19350
19351 if (NILP (eolvalue)) /* Not yet decided. */
19352 eoltype = eol_mnemonic_undecided;
19353 else if (VECTORP (eolvalue)) /* Not yet decided. */
19354 eoltype = eol_mnemonic_undecided;
19355 else /* eolvalue is Qunix, Qdos, or Qmac. */
19356 eoltype = (EQ (eolvalue, Qunix)
19357 ? eol_mnemonic_unix
19358 : (EQ (eolvalue, Qdos) == 1
19359 ? eol_mnemonic_dos : eol_mnemonic_mac));
19360 }
19361 }
19362
19363 if (eol_flag)
19364 {
19365 /* Mention the EOL conversion if it is not the usual one. */
19366 if (STRINGP (eoltype))
19367 {
19368 eol_str = SDATA (eoltype);
19369 eol_str_len = SBYTES (eoltype);
19370 }
19371 else if (CHARACTERP (eoltype))
19372 {
19373 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19374 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19375 eol_str = tmp;
19376 }
19377 else
19378 {
19379 eol_str = invalid_eol_type;
19380 eol_str_len = sizeof (invalid_eol_type) - 1;
19381 }
19382 memcpy (buf, eol_str, eol_str_len);
19383 buf += eol_str_len;
19384 }
19385
19386 return buf;
19387 }
19388
19389 /* Return a string for the output of a mode line %-spec for window W,
19390 generated by character C. PRECISION >= 0 means don't return a
19391 string longer than that value. FIELD_WIDTH > 0 means pad the
19392 string returned with spaces to that value. Return a Lisp string in
19393 *STRING if the resulting string is taken from that Lisp string.
19394
19395 Note we operate on the current buffer for most purposes,
19396 the exception being w->base_line_pos. */
19397
19398 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19399
19400 static const char *
19401 decode_mode_spec (struct window *w, register int c, int field_width,
19402 int precision, Lisp_Object *string)
19403 {
19404 Lisp_Object obj;
19405 struct frame *f = XFRAME (WINDOW_FRAME (w));
19406 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19407 struct buffer *b = current_buffer;
19408
19409 obj = Qnil;
19410 *string = Qnil;
19411
19412 switch (c)
19413 {
19414 case '*':
19415 if (!NILP (b->read_only))
19416 return "%";
19417 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19418 return "*";
19419 return "-";
19420
19421 case '+':
19422 /* This differs from %* only for a modified read-only buffer. */
19423 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19424 return "*";
19425 if (!NILP (b->read_only))
19426 return "%";
19427 return "-";
19428
19429 case '&':
19430 /* This differs from %* in ignoring read-only-ness. */
19431 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19432 return "*";
19433 return "-";
19434
19435 case '%':
19436 return "%";
19437
19438 case '[':
19439 {
19440 int i;
19441 char *p;
19442
19443 if (command_loop_level > 5)
19444 return "[[[... ";
19445 p = decode_mode_spec_buf;
19446 for (i = 0; i < command_loop_level; i++)
19447 *p++ = '[';
19448 *p = 0;
19449 return decode_mode_spec_buf;
19450 }
19451
19452 case ']':
19453 {
19454 int i;
19455 char *p;
19456
19457 if (command_loop_level > 5)
19458 return " ...]]]";
19459 p = decode_mode_spec_buf;
19460 for (i = 0; i < command_loop_level; i++)
19461 *p++ = ']';
19462 *p = 0;
19463 return decode_mode_spec_buf;
19464 }
19465
19466 case '-':
19467 {
19468 register int i;
19469
19470 /* Let lots_of_dashes be a string of infinite length. */
19471 if (mode_line_target == MODE_LINE_NOPROP ||
19472 mode_line_target == MODE_LINE_STRING)
19473 return "--";
19474 if (field_width <= 0
19475 || field_width > sizeof (lots_of_dashes))
19476 {
19477 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19478 decode_mode_spec_buf[i] = '-';
19479 decode_mode_spec_buf[i] = '\0';
19480 return decode_mode_spec_buf;
19481 }
19482 else
19483 return lots_of_dashes;
19484 }
19485
19486 case 'b':
19487 obj = b->name;
19488 break;
19489
19490 case 'c':
19491 /* %c and %l are ignored in `frame-title-format'.
19492 (In redisplay_internal, the frame title is drawn _before_ the
19493 windows are updated, so the stuff which depends on actual
19494 window contents (such as %l) may fail to render properly, or
19495 even crash emacs.) */
19496 if (mode_line_target == MODE_LINE_TITLE)
19497 return "";
19498 else
19499 {
19500 int col = (int) current_column (); /* iftc */
19501 w->column_number_displayed = make_number (col);
19502 pint2str (decode_mode_spec_buf, field_width, col);
19503 return decode_mode_spec_buf;
19504 }
19505
19506 case 'e':
19507 #ifndef SYSTEM_MALLOC
19508 {
19509 if (NILP (Vmemory_full))
19510 return "";
19511 else
19512 return "!MEM FULL! ";
19513 }
19514 #else
19515 return "";
19516 #endif
19517
19518 case 'F':
19519 /* %F displays the frame name. */
19520 if (!NILP (f->title))
19521 return (char *) SDATA (f->title);
19522 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19523 return (char *) SDATA (f->name);
19524 return "Emacs";
19525
19526 case 'f':
19527 obj = b->filename;
19528 break;
19529
19530 case 'i':
19531 {
19532 EMACS_INT size = ZV - BEGV;
19533 pint2str (decode_mode_spec_buf, field_width, size);
19534 return decode_mode_spec_buf;
19535 }
19536
19537 case 'I':
19538 {
19539 EMACS_INT size = ZV - BEGV;
19540 pint2hrstr (decode_mode_spec_buf, field_width, size);
19541 return decode_mode_spec_buf;
19542 }
19543
19544 case 'l':
19545 {
19546 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19547 int topline, nlines, height;
19548 EMACS_INT junk;
19549
19550 /* %c and %l are ignored in `frame-title-format'. */
19551 if (mode_line_target == MODE_LINE_TITLE)
19552 return "";
19553
19554 startpos = XMARKER (w->start)->charpos;
19555 startpos_byte = marker_byte_position (w->start);
19556 height = WINDOW_TOTAL_LINES (w);
19557
19558 /* If we decided that this buffer isn't suitable for line numbers,
19559 don't forget that too fast. */
19560 if (EQ (w->base_line_pos, w->buffer))
19561 goto no_value;
19562 /* But do forget it, if the window shows a different buffer now. */
19563 else if (BUFFERP (w->base_line_pos))
19564 w->base_line_pos = Qnil;
19565
19566 /* If the buffer is very big, don't waste time. */
19567 if (INTEGERP (Vline_number_display_limit)
19568 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19569 {
19570 w->base_line_pos = Qnil;
19571 w->base_line_number = Qnil;
19572 goto no_value;
19573 }
19574
19575 if (INTEGERP (w->base_line_number)
19576 && INTEGERP (w->base_line_pos)
19577 && XFASTINT (w->base_line_pos) <= startpos)
19578 {
19579 line = XFASTINT (w->base_line_number);
19580 linepos = XFASTINT (w->base_line_pos);
19581 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19582 }
19583 else
19584 {
19585 line = 1;
19586 linepos = BUF_BEGV (b);
19587 linepos_byte = BUF_BEGV_BYTE (b);
19588 }
19589
19590 /* Count lines from base line to window start position. */
19591 nlines = display_count_lines (linepos, linepos_byte,
19592 startpos_byte,
19593 startpos, &junk);
19594
19595 topline = nlines + line;
19596
19597 /* Determine a new base line, if the old one is too close
19598 or too far away, or if we did not have one.
19599 "Too close" means it's plausible a scroll-down would
19600 go back past it. */
19601 if (startpos == BUF_BEGV (b))
19602 {
19603 w->base_line_number = make_number (topline);
19604 w->base_line_pos = make_number (BUF_BEGV (b));
19605 }
19606 else if (nlines < height + 25 || nlines > height * 3 + 50
19607 || linepos == BUF_BEGV (b))
19608 {
19609 EMACS_INT limit = BUF_BEGV (b);
19610 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19611 EMACS_INT position;
19612 int distance = (height * 2 + 30) * line_number_display_limit_width;
19613
19614 if (startpos - distance > limit)
19615 {
19616 limit = startpos - distance;
19617 limit_byte = CHAR_TO_BYTE (limit);
19618 }
19619
19620 nlines = display_count_lines (startpos, startpos_byte,
19621 limit_byte,
19622 - (height * 2 + 30),
19623 &position);
19624 /* If we couldn't find the lines we wanted within
19625 line_number_display_limit_width chars per line,
19626 give up on line numbers for this window. */
19627 if (position == limit_byte && limit == startpos - distance)
19628 {
19629 w->base_line_pos = w->buffer;
19630 w->base_line_number = Qnil;
19631 goto no_value;
19632 }
19633
19634 w->base_line_number = make_number (topline - nlines);
19635 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19636 }
19637
19638 /* Now count lines from the start pos to point. */
19639 nlines = display_count_lines (startpos, startpos_byte,
19640 PT_BYTE, PT, &junk);
19641
19642 /* Record that we did display the line number. */
19643 line_number_displayed = 1;
19644
19645 /* Make the string to show. */
19646 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19647 return decode_mode_spec_buf;
19648 no_value:
19649 {
19650 char* p = decode_mode_spec_buf;
19651 int pad = field_width - 2;
19652 while (pad-- > 0)
19653 *p++ = ' ';
19654 *p++ = '?';
19655 *p++ = '?';
19656 *p = '\0';
19657 return decode_mode_spec_buf;
19658 }
19659 }
19660 break;
19661
19662 case 'm':
19663 obj = b->mode_name;
19664 break;
19665
19666 case 'n':
19667 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19668 return " Narrow";
19669 break;
19670
19671 case 'p':
19672 {
19673 EMACS_INT pos = marker_position (w->start);
19674 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19675
19676 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19677 {
19678 if (pos <= BUF_BEGV (b))
19679 return "All";
19680 else
19681 return "Bottom";
19682 }
19683 else if (pos <= BUF_BEGV (b))
19684 return "Top";
19685 else
19686 {
19687 if (total > 1000000)
19688 /* Do it differently for a large value, to avoid overflow. */
19689 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19690 else
19691 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19692 /* We can't normally display a 3-digit number,
19693 so get us a 2-digit number that is close. */
19694 if (total == 100)
19695 total = 99;
19696 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19697 return decode_mode_spec_buf;
19698 }
19699 }
19700
19701 /* Display percentage of size above the bottom of the screen. */
19702 case 'P':
19703 {
19704 EMACS_INT toppos = marker_position (w->start);
19705 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19706 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19707
19708 if (botpos >= BUF_ZV (b))
19709 {
19710 if (toppos <= BUF_BEGV (b))
19711 return "All";
19712 else
19713 return "Bottom";
19714 }
19715 else
19716 {
19717 if (total > 1000000)
19718 /* Do it differently for a large value, to avoid overflow. */
19719 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19720 else
19721 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19722 /* We can't normally display a 3-digit number,
19723 so get us a 2-digit number that is close. */
19724 if (total == 100)
19725 total = 99;
19726 if (toppos <= BUF_BEGV (b))
19727 sprintf (decode_mode_spec_buf, "Top%2ld%%", (long)total);
19728 else
19729 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19730 return decode_mode_spec_buf;
19731 }
19732 }
19733
19734 case 's':
19735 /* status of process */
19736 obj = Fget_buffer_process (Fcurrent_buffer ());
19737 if (NILP (obj))
19738 return "no process";
19739 #ifndef MSDOS
19740 obj = Fsymbol_name (Fprocess_status (obj));
19741 #endif
19742 break;
19743
19744 case '@':
19745 {
19746 int count = inhibit_garbage_collection ();
19747 Lisp_Object val = call1 (intern ("file-remote-p"),
19748 current_buffer->directory);
19749 unbind_to (count, Qnil);
19750
19751 if (NILP (val))
19752 return "-";
19753 else
19754 return "@";
19755 }
19756
19757 case 't': /* indicate TEXT or BINARY */
19758 #ifdef MODE_LINE_BINARY_TEXT
19759 return MODE_LINE_BINARY_TEXT (b);
19760 #else
19761 return "T";
19762 #endif
19763
19764 case 'z':
19765 /* coding-system (not including end-of-line format) */
19766 case 'Z':
19767 /* coding-system (including end-of-line type) */
19768 {
19769 int eol_flag = (c == 'Z');
19770 char *p = decode_mode_spec_buf;
19771
19772 if (! FRAME_WINDOW_P (f))
19773 {
19774 /* No need to mention EOL here--the terminal never needs
19775 to do EOL conversion. */
19776 p = decode_mode_spec_coding (CODING_ID_NAME
19777 (FRAME_KEYBOARD_CODING (f)->id),
19778 p, 0);
19779 p = decode_mode_spec_coding (CODING_ID_NAME
19780 (FRAME_TERMINAL_CODING (f)->id),
19781 p, 0);
19782 }
19783 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19784 p, eol_flag);
19785
19786 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19787 #ifdef subprocesses
19788 obj = Fget_buffer_process (Fcurrent_buffer ());
19789 if (PROCESSP (obj))
19790 {
19791 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19792 p, eol_flag);
19793 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19794 p, eol_flag);
19795 }
19796 #endif /* subprocesses */
19797 #endif /* 0 */
19798 *p = 0;
19799 return decode_mode_spec_buf;
19800 }
19801 }
19802
19803 if (STRINGP (obj))
19804 {
19805 *string = obj;
19806 return (char *) SDATA (obj);
19807 }
19808 else
19809 return "";
19810 }
19811
19812
19813 /* Count up to COUNT lines starting from START / START_BYTE.
19814 But don't go beyond LIMIT_BYTE.
19815 Return the number of lines thus found (always nonnegative).
19816
19817 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19818
19819 static int
19820 display_count_lines (EMACS_INT start, EMACS_INT start_byte,
19821 EMACS_INT limit_byte, int count,
19822 EMACS_INT *byte_pos_ptr)
19823 {
19824 register unsigned char *cursor;
19825 unsigned char *base;
19826
19827 register int ceiling;
19828 register unsigned char *ceiling_addr;
19829 int orig_count = count;
19830
19831 /* If we are not in selective display mode,
19832 check only for newlines. */
19833 int selective_display = (!NILP (current_buffer->selective_display)
19834 && !INTEGERP (current_buffer->selective_display));
19835
19836 if (count > 0)
19837 {
19838 while (start_byte < limit_byte)
19839 {
19840 ceiling = BUFFER_CEILING_OF (start_byte);
19841 ceiling = min (limit_byte - 1, ceiling);
19842 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19843 base = (cursor = BYTE_POS_ADDR (start_byte));
19844 while (1)
19845 {
19846 if (selective_display)
19847 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19848 ;
19849 else
19850 while (*cursor != '\n' && ++cursor != ceiling_addr)
19851 ;
19852
19853 if (cursor != ceiling_addr)
19854 {
19855 if (--count == 0)
19856 {
19857 start_byte += cursor - base + 1;
19858 *byte_pos_ptr = start_byte;
19859 return orig_count;
19860 }
19861 else
19862 if (++cursor == ceiling_addr)
19863 break;
19864 }
19865 else
19866 break;
19867 }
19868 start_byte += cursor - base;
19869 }
19870 }
19871 else
19872 {
19873 while (start_byte > limit_byte)
19874 {
19875 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19876 ceiling = max (limit_byte, ceiling);
19877 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19878 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19879 while (1)
19880 {
19881 if (selective_display)
19882 while (--cursor != ceiling_addr
19883 && *cursor != '\n' && *cursor != 015)
19884 ;
19885 else
19886 while (--cursor != ceiling_addr && *cursor != '\n')
19887 ;
19888
19889 if (cursor != ceiling_addr)
19890 {
19891 if (++count == 0)
19892 {
19893 start_byte += cursor - base + 1;
19894 *byte_pos_ptr = start_byte;
19895 /* When scanning backwards, we should
19896 not count the newline posterior to which we stop. */
19897 return - orig_count - 1;
19898 }
19899 }
19900 else
19901 break;
19902 }
19903 /* Here we add 1 to compensate for the last decrement
19904 of CURSOR, which took it past the valid range. */
19905 start_byte += cursor - base + 1;
19906 }
19907 }
19908
19909 *byte_pos_ptr = limit_byte;
19910
19911 if (count < 0)
19912 return - orig_count + count;
19913 return orig_count - count;
19914
19915 }
19916
19917
19918 \f
19919 /***********************************************************************
19920 Displaying strings
19921 ***********************************************************************/
19922
19923 /* Display a NUL-terminated string, starting with index START.
19924
19925 If STRING is non-null, display that C string. Otherwise, the Lisp
19926 string LISP_STRING is displayed. There's a case that STRING is
19927 non-null and LISP_STRING is not nil. It means STRING is a string
19928 data of LISP_STRING. In that case, we display LISP_STRING while
19929 ignoring its text properties.
19930
19931 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19932 FACE_STRING. Display STRING or LISP_STRING with the face at
19933 FACE_STRING_POS in FACE_STRING:
19934
19935 Display the string in the environment given by IT, but use the
19936 standard display table, temporarily.
19937
19938 FIELD_WIDTH is the minimum number of output glyphs to produce.
19939 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19940 with spaces. If STRING has more characters, more than FIELD_WIDTH
19941 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19942
19943 PRECISION is the maximum number of characters to output from
19944 STRING. PRECISION < 0 means don't truncate the string.
19945
19946 This is roughly equivalent to printf format specifiers:
19947
19948 FIELD_WIDTH PRECISION PRINTF
19949 ----------------------------------------
19950 -1 -1 %s
19951 -1 10 %.10s
19952 10 -1 %10s
19953 20 10 %20.10s
19954
19955 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19956 display them, and < 0 means obey the current buffer's value of
19957 enable_multibyte_characters.
19958
19959 Value is the number of columns displayed. */
19960
19961 static int
19962 display_string (const unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19963 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19964 int field_width, int precision, int max_x, int multibyte)
19965 {
19966 int hpos_at_start = it->hpos;
19967 int saved_face_id = it->face_id;
19968 struct glyph_row *row = it->glyph_row;
19969
19970 /* Initialize the iterator IT for iteration over STRING beginning
19971 with index START. */
19972 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19973 precision, field_width, multibyte);
19974 if (string && STRINGP (lisp_string))
19975 /* LISP_STRING is the one returned by decode_mode_spec. We should
19976 ignore its text properties. */
19977 it->stop_charpos = -1;
19978
19979 /* If displaying STRING, set up the face of the iterator
19980 from LISP_STRING, if that's given. */
19981 if (STRINGP (face_string))
19982 {
19983 EMACS_INT endptr;
19984 struct face *face;
19985
19986 it->face_id
19987 = face_at_string_position (it->w, face_string, face_string_pos,
19988 0, it->region_beg_charpos,
19989 it->region_end_charpos,
19990 &endptr, it->base_face_id, 0);
19991 face = FACE_FROM_ID (it->f, it->face_id);
19992 it->face_box_p = face->box != FACE_NO_BOX;
19993 }
19994
19995 /* Set max_x to the maximum allowed X position. Don't let it go
19996 beyond the right edge of the window. */
19997 if (max_x <= 0)
19998 max_x = it->last_visible_x;
19999 else
20000 max_x = min (max_x, it->last_visible_x);
20001
20002 /* Skip over display elements that are not visible. because IT->w is
20003 hscrolled. */
20004 if (it->current_x < it->first_visible_x)
20005 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20006 MOVE_TO_POS | MOVE_TO_X);
20007
20008 row->ascent = it->max_ascent;
20009 row->height = it->max_ascent + it->max_descent;
20010 row->phys_ascent = it->max_phys_ascent;
20011 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20012 row->extra_line_spacing = it->max_extra_line_spacing;
20013
20014 /* This condition is for the case that we are called with current_x
20015 past last_visible_x. */
20016 while (it->current_x < max_x)
20017 {
20018 int x_before, x, n_glyphs_before, i, nglyphs;
20019
20020 /* Get the next display element. */
20021 if (!get_next_display_element (it))
20022 break;
20023
20024 /* Produce glyphs. */
20025 x_before = it->current_x;
20026 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
20027 PRODUCE_GLYPHS (it);
20028
20029 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
20030 i = 0;
20031 x = x_before;
20032 while (i < nglyphs)
20033 {
20034 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20035
20036 if (it->line_wrap != TRUNCATE
20037 && x + glyph->pixel_width > max_x)
20038 {
20039 /* End of continued line or max_x reached. */
20040 if (CHAR_GLYPH_PADDING_P (*glyph))
20041 {
20042 /* A wide character is unbreakable. */
20043 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
20044 it->current_x = x_before;
20045 }
20046 else
20047 {
20048 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
20049 it->current_x = x;
20050 }
20051 break;
20052 }
20053 else if (x + glyph->pixel_width >= it->first_visible_x)
20054 {
20055 /* Glyph is at least partially visible. */
20056 ++it->hpos;
20057 if (x < it->first_visible_x)
20058 it->glyph_row->x = x - it->first_visible_x;
20059 }
20060 else
20061 {
20062 /* Glyph is off the left margin of the display area.
20063 Should not happen. */
20064 abort ();
20065 }
20066
20067 row->ascent = max (row->ascent, it->max_ascent);
20068 row->height = max (row->height, it->max_ascent + it->max_descent);
20069 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20070 row->phys_height = max (row->phys_height,
20071 it->max_phys_ascent + it->max_phys_descent);
20072 row->extra_line_spacing = max (row->extra_line_spacing,
20073 it->max_extra_line_spacing);
20074 x += glyph->pixel_width;
20075 ++i;
20076 }
20077
20078 /* Stop if max_x reached. */
20079 if (i < nglyphs)
20080 break;
20081
20082 /* Stop at line ends. */
20083 if (ITERATOR_AT_END_OF_LINE_P (it))
20084 {
20085 it->continuation_lines_width = 0;
20086 break;
20087 }
20088
20089 set_iterator_to_next (it, 1);
20090
20091 /* Stop if truncating at the right edge. */
20092 if (it->line_wrap == TRUNCATE
20093 && it->current_x >= it->last_visible_x)
20094 {
20095 /* Add truncation mark, but don't do it if the line is
20096 truncated at a padding space. */
20097 if (IT_CHARPOS (*it) < it->string_nchars)
20098 {
20099 if (!FRAME_WINDOW_P (it->f))
20100 {
20101 int i, n;
20102
20103 if (it->current_x > it->last_visible_x)
20104 {
20105 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20106 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20107 break;
20108 for (n = row->used[TEXT_AREA]; i < n; ++i)
20109 {
20110 row->used[TEXT_AREA] = i;
20111 produce_special_glyphs (it, IT_TRUNCATION);
20112 }
20113 }
20114 produce_special_glyphs (it, IT_TRUNCATION);
20115 }
20116 it->glyph_row->truncated_on_right_p = 1;
20117 }
20118 break;
20119 }
20120 }
20121
20122 /* Maybe insert a truncation at the left. */
20123 if (it->first_visible_x
20124 && IT_CHARPOS (*it) > 0)
20125 {
20126 if (!FRAME_WINDOW_P (it->f))
20127 insert_left_trunc_glyphs (it);
20128 it->glyph_row->truncated_on_left_p = 1;
20129 }
20130
20131 it->face_id = saved_face_id;
20132
20133 /* Value is number of columns displayed. */
20134 return it->hpos - hpos_at_start;
20135 }
20136
20137
20138 \f
20139 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20140 appears as an element of LIST or as the car of an element of LIST.
20141 If PROPVAL is a list, compare each element against LIST in that
20142 way, and return 1/2 if any element of PROPVAL is found in LIST.
20143 Otherwise return 0. This function cannot quit.
20144 The return value is 2 if the text is invisible but with an ellipsis
20145 and 1 if it's invisible and without an ellipsis. */
20146
20147 int
20148 invisible_p (register Lisp_Object propval, Lisp_Object list)
20149 {
20150 register Lisp_Object tail, proptail;
20151
20152 for (tail = list; CONSP (tail); tail = XCDR (tail))
20153 {
20154 register Lisp_Object tem;
20155 tem = XCAR (tail);
20156 if (EQ (propval, tem))
20157 return 1;
20158 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20159 return NILP (XCDR (tem)) ? 1 : 2;
20160 }
20161
20162 if (CONSP (propval))
20163 {
20164 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20165 {
20166 Lisp_Object propelt;
20167 propelt = XCAR (proptail);
20168 for (tail = list; CONSP (tail); tail = XCDR (tail))
20169 {
20170 register Lisp_Object tem;
20171 tem = XCAR (tail);
20172 if (EQ (propelt, tem))
20173 return 1;
20174 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20175 return NILP (XCDR (tem)) ? 1 : 2;
20176 }
20177 }
20178 }
20179
20180 return 0;
20181 }
20182
20183 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20184 doc: /* Non-nil if the property makes the text invisible.
20185 POS-OR-PROP can be a marker or number, in which case it is taken to be
20186 a position in the current buffer and the value of the `invisible' property
20187 is checked; or it can be some other value, which is then presumed to be the
20188 value of the `invisible' property of the text of interest.
20189 The non-nil value returned can be t for truly invisible text or something
20190 else if the text is replaced by an ellipsis. */)
20191 (Lisp_Object pos_or_prop)
20192 {
20193 Lisp_Object prop
20194 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20195 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20196 : pos_or_prop);
20197 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20198 return (invis == 0 ? Qnil
20199 : invis == 1 ? Qt
20200 : make_number (invis));
20201 }
20202
20203 /* Calculate a width or height in pixels from a specification using
20204 the following elements:
20205
20206 SPEC ::=
20207 NUM - a (fractional) multiple of the default font width/height
20208 (NUM) - specifies exactly NUM pixels
20209 UNIT - a fixed number of pixels, see below.
20210 ELEMENT - size of a display element in pixels, see below.
20211 (NUM . SPEC) - equals NUM * SPEC
20212 (+ SPEC SPEC ...) - add pixel values
20213 (- SPEC SPEC ...) - subtract pixel values
20214 (- SPEC) - negate pixel value
20215
20216 NUM ::=
20217 INT or FLOAT - a number constant
20218 SYMBOL - use symbol's (buffer local) variable binding.
20219
20220 UNIT ::=
20221 in - pixels per inch *)
20222 mm - pixels per 1/1000 meter *)
20223 cm - pixels per 1/100 meter *)
20224 width - width of current font in pixels.
20225 height - height of current font in pixels.
20226
20227 *) using the ratio(s) defined in display-pixels-per-inch.
20228
20229 ELEMENT ::=
20230
20231 left-fringe - left fringe width in pixels
20232 right-fringe - right fringe width in pixels
20233
20234 left-margin - left margin width in pixels
20235 right-margin - right margin width in pixels
20236
20237 scroll-bar - scroll-bar area width in pixels
20238
20239 Examples:
20240
20241 Pixels corresponding to 5 inches:
20242 (5 . in)
20243
20244 Total width of non-text areas on left side of window (if scroll-bar is on left):
20245 '(space :width (+ left-fringe left-margin scroll-bar))
20246
20247 Align to first text column (in header line):
20248 '(space :align-to 0)
20249
20250 Align to middle of text area minus half the width of variable `my-image'
20251 containing a loaded image:
20252 '(space :align-to (0.5 . (- text my-image)))
20253
20254 Width of left margin minus width of 1 character in the default font:
20255 '(space :width (- left-margin 1))
20256
20257 Width of left margin minus width of 2 characters in the current font:
20258 '(space :width (- left-margin (2 . width)))
20259
20260 Center 1 character over left-margin (in header line):
20261 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20262
20263 Different ways to express width of left fringe plus left margin minus one pixel:
20264 '(space :width (- (+ left-fringe left-margin) (1)))
20265 '(space :width (+ left-fringe left-margin (- (1))))
20266 '(space :width (+ left-fringe left-margin (-1)))
20267
20268 */
20269
20270 #define NUMVAL(X) \
20271 ((INTEGERP (X) || FLOATP (X)) \
20272 ? XFLOATINT (X) \
20273 : - 1)
20274
20275 int
20276 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20277 struct font *font, int width_p, int *align_to)
20278 {
20279 double pixels;
20280
20281 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20282 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20283
20284 if (NILP (prop))
20285 return OK_PIXELS (0);
20286
20287 xassert (FRAME_LIVE_P (it->f));
20288
20289 if (SYMBOLP (prop))
20290 {
20291 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20292 {
20293 char *unit = SDATA (SYMBOL_NAME (prop));
20294
20295 if (unit[0] == 'i' && unit[1] == 'n')
20296 pixels = 1.0;
20297 else if (unit[0] == 'm' && unit[1] == 'm')
20298 pixels = 25.4;
20299 else if (unit[0] == 'c' && unit[1] == 'm')
20300 pixels = 2.54;
20301 else
20302 pixels = 0;
20303 if (pixels > 0)
20304 {
20305 double ppi;
20306 #ifdef HAVE_WINDOW_SYSTEM
20307 if (FRAME_WINDOW_P (it->f)
20308 && (ppi = (width_p
20309 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20310 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20311 ppi > 0))
20312 return OK_PIXELS (ppi / pixels);
20313 #endif
20314
20315 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20316 || (CONSP (Vdisplay_pixels_per_inch)
20317 && (ppi = (width_p
20318 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20319 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20320 ppi > 0)))
20321 return OK_PIXELS (ppi / pixels);
20322
20323 return 0;
20324 }
20325 }
20326
20327 #ifdef HAVE_WINDOW_SYSTEM
20328 if (EQ (prop, Qheight))
20329 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20330 if (EQ (prop, Qwidth))
20331 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20332 #else
20333 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20334 return OK_PIXELS (1);
20335 #endif
20336
20337 if (EQ (prop, Qtext))
20338 return OK_PIXELS (width_p
20339 ? window_box_width (it->w, TEXT_AREA)
20340 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20341
20342 if (align_to && *align_to < 0)
20343 {
20344 *res = 0;
20345 if (EQ (prop, Qleft))
20346 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20347 if (EQ (prop, Qright))
20348 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20349 if (EQ (prop, Qcenter))
20350 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20351 + window_box_width (it->w, TEXT_AREA) / 2);
20352 if (EQ (prop, Qleft_fringe))
20353 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20354 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20355 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20356 if (EQ (prop, Qright_fringe))
20357 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20358 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20359 : window_box_right_offset (it->w, TEXT_AREA));
20360 if (EQ (prop, Qleft_margin))
20361 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20362 if (EQ (prop, Qright_margin))
20363 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20364 if (EQ (prop, Qscroll_bar))
20365 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20366 ? 0
20367 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20368 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20369 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20370 : 0)));
20371 }
20372 else
20373 {
20374 if (EQ (prop, Qleft_fringe))
20375 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20376 if (EQ (prop, Qright_fringe))
20377 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20378 if (EQ (prop, Qleft_margin))
20379 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20380 if (EQ (prop, Qright_margin))
20381 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20382 if (EQ (prop, Qscroll_bar))
20383 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20384 }
20385
20386 prop = Fbuffer_local_value (prop, it->w->buffer);
20387 }
20388
20389 if (INTEGERP (prop) || FLOATP (prop))
20390 {
20391 int base_unit = (width_p
20392 ? FRAME_COLUMN_WIDTH (it->f)
20393 : FRAME_LINE_HEIGHT (it->f));
20394 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20395 }
20396
20397 if (CONSP (prop))
20398 {
20399 Lisp_Object car = XCAR (prop);
20400 Lisp_Object cdr = XCDR (prop);
20401
20402 if (SYMBOLP (car))
20403 {
20404 #ifdef HAVE_WINDOW_SYSTEM
20405 if (FRAME_WINDOW_P (it->f)
20406 && valid_image_p (prop))
20407 {
20408 int id = lookup_image (it->f, prop);
20409 struct image *img = IMAGE_FROM_ID (it->f, id);
20410
20411 return OK_PIXELS (width_p ? img->width : img->height);
20412 }
20413 #endif
20414 if (EQ (car, Qplus) || EQ (car, Qminus))
20415 {
20416 int first = 1;
20417 double px;
20418
20419 pixels = 0;
20420 while (CONSP (cdr))
20421 {
20422 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20423 font, width_p, align_to))
20424 return 0;
20425 if (first)
20426 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20427 else
20428 pixels += px;
20429 cdr = XCDR (cdr);
20430 }
20431 if (EQ (car, Qminus))
20432 pixels = -pixels;
20433 return OK_PIXELS (pixels);
20434 }
20435
20436 car = Fbuffer_local_value (car, it->w->buffer);
20437 }
20438
20439 if (INTEGERP (car) || FLOATP (car))
20440 {
20441 double fact;
20442 pixels = XFLOATINT (car);
20443 if (NILP (cdr))
20444 return OK_PIXELS (pixels);
20445 if (calc_pixel_width_or_height (&fact, it, cdr,
20446 font, width_p, align_to))
20447 return OK_PIXELS (pixels * fact);
20448 return 0;
20449 }
20450
20451 return 0;
20452 }
20453
20454 return 0;
20455 }
20456
20457 \f
20458 /***********************************************************************
20459 Glyph Display
20460 ***********************************************************************/
20461
20462 #ifdef HAVE_WINDOW_SYSTEM
20463
20464 #if GLYPH_DEBUG
20465
20466 void
20467 dump_glyph_string (s)
20468 struct glyph_string *s;
20469 {
20470 fprintf (stderr, "glyph string\n");
20471 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20472 s->x, s->y, s->width, s->height);
20473 fprintf (stderr, " ybase = %d\n", s->ybase);
20474 fprintf (stderr, " hl = %d\n", s->hl);
20475 fprintf (stderr, " left overhang = %d, right = %d\n",
20476 s->left_overhang, s->right_overhang);
20477 fprintf (stderr, " nchars = %d\n", s->nchars);
20478 fprintf (stderr, " extends to end of line = %d\n",
20479 s->extends_to_end_of_line_p);
20480 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20481 fprintf (stderr, " bg width = %d\n", s->background_width);
20482 }
20483
20484 #endif /* GLYPH_DEBUG */
20485
20486 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20487 of XChar2b structures for S; it can't be allocated in
20488 init_glyph_string because it must be allocated via `alloca'. W
20489 is the window on which S is drawn. ROW and AREA are the glyph row
20490 and area within the row from which S is constructed. START is the
20491 index of the first glyph structure covered by S. HL is a
20492 face-override for drawing S. */
20493
20494 #ifdef HAVE_NTGUI
20495 #define OPTIONAL_HDC(hdc) HDC hdc,
20496 #define DECLARE_HDC(hdc) HDC hdc;
20497 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20498 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20499 #endif
20500
20501 #ifndef OPTIONAL_HDC
20502 #define OPTIONAL_HDC(hdc)
20503 #define DECLARE_HDC(hdc)
20504 #define ALLOCATE_HDC(hdc, f)
20505 #define RELEASE_HDC(hdc, f)
20506 #endif
20507
20508 static void
20509 init_glyph_string (struct glyph_string *s,
20510 OPTIONAL_HDC (hdc)
20511 XChar2b *char2b, struct window *w, struct glyph_row *row,
20512 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20513 {
20514 memset (s, 0, sizeof *s);
20515 s->w = w;
20516 s->f = XFRAME (w->frame);
20517 #ifdef HAVE_NTGUI
20518 s->hdc = hdc;
20519 #endif
20520 s->display = FRAME_X_DISPLAY (s->f);
20521 s->window = FRAME_X_WINDOW (s->f);
20522 s->char2b = char2b;
20523 s->hl = hl;
20524 s->row = row;
20525 s->area = area;
20526 s->first_glyph = row->glyphs[area] + start;
20527 s->height = row->height;
20528 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20529 s->ybase = s->y + row->ascent;
20530 }
20531
20532
20533 /* Append the list of glyph strings with head H and tail T to the list
20534 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20535
20536 static INLINE void
20537 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20538 struct glyph_string *h, struct glyph_string *t)
20539 {
20540 if (h)
20541 {
20542 if (*head)
20543 (*tail)->next = h;
20544 else
20545 *head = h;
20546 h->prev = *tail;
20547 *tail = t;
20548 }
20549 }
20550
20551
20552 /* Prepend the list of glyph strings with head H and tail T to the
20553 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20554 result. */
20555
20556 static INLINE void
20557 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20558 struct glyph_string *h, struct glyph_string *t)
20559 {
20560 if (h)
20561 {
20562 if (*head)
20563 (*head)->prev = t;
20564 else
20565 *tail = t;
20566 t->next = *head;
20567 *head = h;
20568 }
20569 }
20570
20571
20572 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20573 Set *HEAD and *TAIL to the resulting list. */
20574
20575 static INLINE void
20576 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20577 struct glyph_string *s)
20578 {
20579 s->next = s->prev = NULL;
20580 append_glyph_string_lists (head, tail, s, s);
20581 }
20582
20583
20584 /* Get face and two-byte form of character C in face FACE_ID on frame
20585 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20586 means we want to display multibyte text. DISPLAY_P non-zero means
20587 make sure that X resources for the face returned are allocated.
20588 Value is a pointer to a realized face that is ready for display if
20589 DISPLAY_P is non-zero. */
20590
20591 static INLINE struct face *
20592 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20593 XChar2b *char2b, int multibyte_p, int display_p)
20594 {
20595 struct face *face = FACE_FROM_ID (f, face_id);
20596
20597 if (face->font)
20598 {
20599 unsigned code = face->font->driver->encode_char (face->font, c);
20600
20601 if (code != FONT_INVALID_CODE)
20602 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20603 else
20604 STORE_XCHAR2B (char2b, 0, 0);
20605 }
20606
20607 /* Make sure X resources of the face are allocated. */
20608 #ifdef HAVE_X_WINDOWS
20609 if (display_p)
20610 #endif
20611 {
20612 xassert (face != NULL);
20613 PREPARE_FACE_FOR_DISPLAY (f, face);
20614 }
20615
20616 return face;
20617 }
20618
20619
20620 /* Get face and two-byte form of character glyph GLYPH on frame F.
20621 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20622 a pointer to a realized face that is ready for display. */
20623
20624 static INLINE struct face *
20625 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20626 XChar2b *char2b, int *two_byte_p)
20627 {
20628 struct face *face;
20629
20630 xassert (glyph->type == CHAR_GLYPH);
20631 face = FACE_FROM_ID (f, glyph->face_id);
20632
20633 if (two_byte_p)
20634 *two_byte_p = 0;
20635
20636 if (face->font)
20637 {
20638 unsigned code;
20639
20640 if (CHAR_BYTE8_P (glyph->u.ch))
20641 code = CHAR_TO_BYTE8 (glyph->u.ch);
20642 else
20643 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20644
20645 if (code != FONT_INVALID_CODE)
20646 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20647 else
20648 STORE_XCHAR2B (char2b, 0, 0);
20649 }
20650
20651 /* Make sure X resources of the face are allocated. */
20652 xassert (face != NULL);
20653 PREPARE_FACE_FOR_DISPLAY (f, face);
20654 return face;
20655 }
20656
20657
20658 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20659 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20660
20661 static INLINE int
20662 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20663 {
20664 unsigned code;
20665
20666 if (CHAR_BYTE8_P (c))
20667 code = CHAR_TO_BYTE8 (c);
20668 else
20669 code = font->driver->encode_char (font, c);
20670
20671 if (code == FONT_INVALID_CODE)
20672 return 0;
20673 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20674 return 1;
20675 }
20676
20677
20678 /* Fill glyph string S with composition components specified by S->cmp.
20679
20680 BASE_FACE is the base face of the composition.
20681 S->cmp_from is the index of the first component for S.
20682
20683 OVERLAPS non-zero means S should draw the foreground only, and use
20684 its physical height for clipping. See also draw_glyphs.
20685
20686 Value is the index of a component not in S. */
20687
20688 static int
20689 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20690 int overlaps)
20691 {
20692 int i;
20693 /* For all glyphs of this composition, starting at the offset
20694 S->cmp_from, until we reach the end of the definition or encounter a
20695 glyph that requires the different face, add it to S. */
20696 struct face *face;
20697
20698 xassert (s);
20699
20700 s->for_overlaps = overlaps;
20701 s->face = NULL;
20702 s->font = NULL;
20703 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20704 {
20705 int c = COMPOSITION_GLYPH (s->cmp, i);
20706
20707 if (c != '\t')
20708 {
20709 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20710 -1, Qnil);
20711
20712 face = get_char_face_and_encoding (s->f, c, face_id,
20713 s->char2b + i, 1, 1);
20714 if (face)
20715 {
20716 if (! s->face)
20717 {
20718 s->face = face;
20719 s->font = s->face->font;
20720 }
20721 else if (s->face != face)
20722 break;
20723 }
20724 }
20725 ++s->nchars;
20726 }
20727 s->cmp_to = i;
20728
20729 /* All glyph strings for the same composition has the same width,
20730 i.e. the width set for the first component of the composition. */
20731 s->width = s->first_glyph->pixel_width;
20732
20733 /* If the specified font could not be loaded, use the frame's
20734 default font, but record the fact that we couldn't load it in
20735 the glyph string so that we can draw rectangles for the
20736 characters of the glyph string. */
20737 if (s->font == NULL)
20738 {
20739 s->font_not_found_p = 1;
20740 s->font = FRAME_FONT (s->f);
20741 }
20742
20743 /* Adjust base line for subscript/superscript text. */
20744 s->ybase += s->first_glyph->voffset;
20745
20746 /* This glyph string must always be drawn with 16-bit functions. */
20747 s->two_byte_p = 1;
20748
20749 return s->cmp_to;
20750 }
20751
20752 static int
20753 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20754 int start, int end, int overlaps)
20755 {
20756 struct glyph *glyph, *last;
20757 Lisp_Object lgstring;
20758 int i;
20759
20760 s->for_overlaps = overlaps;
20761 glyph = s->row->glyphs[s->area] + start;
20762 last = s->row->glyphs[s->area] + end;
20763 s->cmp_id = glyph->u.cmp.id;
20764 s->cmp_from = glyph->slice.cmp.from;
20765 s->cmp_to = glyph->slice.cmp.to + 1;
20766 s->face = FACE_FROM_ID (s->f, face_id);
20767 lgstring = composition_gstring_from_id (s->cmp_id);
20768 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20769 glyph++;
20770 while (glyph < last
20771 && glyph->u.cmp.automatic
20772 && glyph->u.cmp.id == s->cmp_id
20773 && s->cmp_to == glyph->slice.cmp.from)
20774 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20775
20776 for (i = s->cmp_from; i < s->cmp_to; i++)
20777 {
20778 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20779 unsigned code = LGLYPH_CODE (lglyph);
20780
20781 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20782 }
20783 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20784 return glyph - s->row->glyphs[s->area];
20785 }
20786
20787
20788 /* Fill glyph string S from a sequence glyphs for glyphless characters.
20789 See the comment of fill_glyph_string for arguments.
20790 Value is the index of the first glyph not in S. */
20791
20792
20793 static int
20794 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
20795 int start, int end, int overlaps)
20796 {
20797 struct glyph *glyph, *last;
20798 int voffset;
20799
20800 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
20801 s->for_overlaps = overlaps;
20802 glyph = s->row->glyphs[s->area] + start;
20803 last = s->row->glyphs[s->area] + end;
20804 voffset = glyph->voffset;
20805 s->face = FACE_FROM_ID (s->f, face_id);
20806 s->font = s->face->font;
20807 s->nchars = 1;
20808 s->width = glyph->pixel_width;
20809 glyph++;
20810 while (glyph < last
20811 && glyph->type == GLYPHLESS_GLYPH
20812 && glyph->voffset == voffset
20813 && glyph->face_id == face_id)
20814 {
20815 s->nchars++;
20816 s->width += glyph->pixel_width;
20817 glyph++;
20818 }
20819 s->ybase += voffset;
20820 return glyph - s->row->glyphs[s->area];
20821 }
20822
20823
20824 /* Fill glyph string S from a sequence of character glyphs.
20825
20826 FACE_ID is the face id of the string. START is the index of the
20827 first glyph to consider, END is the index of the last + 1.
20828 OVERLAPS non-zero means S should draw the foreground only, and use
20829 its physical height for clipping. See also draw_glyphs.
20830
20831 Value is the index of the first glyph not in S. */
20832
20833 static int
20834 fill_glyph_string (struct glyph_string *s, int face_id,
20835 int start, int end, int overlaps)
20836 {
20837 struct glyph *glyph, *last;
20838 int voffset;
20839 int glyph_not_available_p;
20840
20841 xassert (s->f == XFRAME (s->w->frame));
20842 xassert (s->nchars == 0);
20843 xassert (start >= 0 && end > start);
20844
20845 s->for_overlaps = overlaps;
20846 glyph = s->row->glyphs[s->area] + start;
20847 last = s->row->glyphs[s->area] + end;
20848 voffset = glyph->voffset;
20849 s->padding_p = glyph->padding_p;
20850 glyph_not_available_p = glyph->glyph_not_available_p;
20851
20852 while (glyph < last
20853 && glyph->type == CHAR_GLYPH
20854 && glyph->voffset == voffset
20855 /* Same face id implies same font, nowadays. */
20856 && glyph->face_id == face_id
20857 && glyph->glyph_not_available_p == glyph_not_available_p)
20858 {
20859 int two_byte_p;
20860
20861 s->face = get_glyph_face_and_encoding (s->f, glyph,
20862 s->char2b + s->nchars,
20863 &two_byte_p);
20864 s->two_byte_p = two_byte_p;
20865 ++s->nchars;
20866 xassert (s->nchars <= end - start);
20867 s->width += glyph->pixel_width;
20868 if (glyph++->padding_p != s->padding_p)
20869 break;
20870 }
20871
20872 s->font = s->face->font;
20873
20874 /* If the specified font could not be loaded, use the frame's font,
20875 but record the fact that we couldn't load it in
20876 S->font_not_found_p so that we can draw rectangles for the
20877 characters of the glyph string. */
20878 if (s->font == NULL || glyph_not_available_p)
20879 {
20880 s->font_not_found_p = 1;
20881 s->font = FRAME_FONT (s->f);
20882 }
20883
20884 /* Adjust base line for subscript/superscript text. */
20885 s->ybase += voffset;
20886
20887 xassert (s->face && s->face->gc);
20888 return glyph - s->row->glyphs[s->area];
20889 }
20890
20891
20892 /* Fill glyph string S from image glyph S->first_glyph. */
20893
20894 static void
20895 fill_image_glyph_string (struct glyph_string *s)
20896 {
20897 xassert (s->first_glyph->type == IMAGE_GLYPH);
20898 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20899 xassert (s->img);
20900 s->slice = s->first_glyph->slice.img;
20901 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20902 s->font = s->face->font;
20903 s->width = s->first_glyph->pixel_width;
20904
20905 /* Adjust base line for subscript/superscript text. */
20906 s->ybase += s->first_glyph->voffset;
20907 }
20908
20909
20910 /* Fill glyph string S from a sequence of stretch glyphs.
20911
20912 ROW is the glyph row in which the glyphs are found, AREA is the
20913 area within the row. START is the index of the first glyph to
20914 consider, END is the index of the last + 1.
20915
20916 Value is the index of the first glyph not in S. */
20917
20918 static int
20919 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20920 enum glyph_row_area area, int start, int end)
20921 {
20922 struct glyph *glyph, *last;
20923 int voffset, face_id;
20924
20925 xassert (s->first_glyph->type == STRETCH_GLYPH);
20926
20927 glyph = s->row->glyphs[s->area] + start;
20928 last = s->row->glyphs[s->area] + end;
20929 face_id = glyph->face_id;
20930 s->face = FACE_FROM_ID (s->f, face_id);
20931 s->font = s->face->font;
20932 s->width = glyph->pixel_width;
20933 s->nchars = 1;
20934 voffset = glyph->voffset;
20935
20936 for (++glyph;
20937 (glyph < last
20938 && glyph->type == STRETCH_GLYPH
20939 && glyph->voffset == voffset
20940 && glyph->face_id == face_id);
20941 ++glyph)
20942 s->width += glyph->pixel_width;
20943
20944 /* Adjust base line for subscript/superscript text. */
20945 s->ybase += voffset;
20946
20947 /* The case that face->gc == 0 is handled when drawing the glyph
20948 string by calling PREPARE_FACE_FOR_DISPLAY. */
20949 xassert (s->face);
20950 return glyph - s->row->glyphs[s->area];
20951 }
20952
20953 static struct font_metrics *
20954 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20955 {
20956 static struct font_metrics metrics;
20957 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20958
20959 if (! font || code == FONT_INVALID_CODE)
20960 return NULL;
20961 font->driver->text_extents (font, &code, 1, &metrics);
20962 return &metrics;
20963 }
20964
20965 /* EXPORT for RIF:
20966 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20967 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20968 assumed to be zero. */
20969
20970 void
20971 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20972 {
20973 *left = *right = 0;
20974
20975 if (glyph->type == CHAR_GLYPH)
20976 {
20977 struct face *face;
20978 XChar2b char2b;
20979 struct font_metrics *pcm;
20980
20981 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20982 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20983 {
20984 if (pcm->rbearing > pcm->width)
20985 *right = pcm->rbearing - pcm->width;
20986 if (pcm->lbearing < 0)
20987 *left = -pcm->lbearing;
20988 }
20989 }
20990 else if (glyph->type == COMPOSITE_GLYPH)
20991 {
20992 if (! glyph->u.cmp.automatic)
20993 {
20994 struct composition *cmp = composition_table[glyph->u.cmp.id];
20995
20996 if (cmp->rbearing > cmp->pixel_width)
20997 *right = cmp->rbearing - cmp->pixel_width;
20998 if (cmp->lbearing < 0)
20999 *left = - cmp->lbearing;
21000 }
21001 else
21002 {
21003 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21004 struct font_metrics metrics;
21005
21006 composition_gstring_width (gstring, glyph->slice.cmp.from,
21007 glyph->slice.cmp.to + 1, &metrics);
21008 if (metrics.rbearing > metrics.width)
21009 *right = metrics.rbearing - metrics.width;
21010 if (metrics.lbearing < 0)
21011 *left = - metrics.lbearing;
21012 }
21013 }
21014 }
21015
21016
21017 /* Return the index of the first glyph preceding glyph string S that
21018 is overwritten by S because of S's left overhang. Value is -1
21019 if no glyphs are overwritten. */
21020
21021 static int
21022 left_overwritten (struct glyph_string *s)
21023 {
21024 int k;
21025
21026 if (s->left_overhang)
21027 {
21028 int x = 0, i;
21029 struct glyph *glyphs = s->row->glyphs[s->area];
21030 int first = s->first_glyph - glyphs;
21031
21032 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21033 x -= glyphs[i].pixel_width;
21034
21035 k = i + 1;
21036 }
21037 else
21038 k = -1;
21039
21040 return k;
21041 }
21042
21043
21044 /* Return the index of the first glyph preceding glyph string S that
21045 is overwriting S because of its right overhang. Value is -1 if no
21046 glyph in front of S overwrites S. */
21047
21048 static int
21049 left_overwriting (struct glyph_string *s)
21050 {
21051 int i, k, x;
21052 struct glyph *glyphs = s->row->glyphs[s->area];
21053 int first = s->first_glyph - glyphs;
21054
21055 k = -1;
21056 x = 0;
21057 for (i = first - 1; i >= 0; --i)
21058 {
21059 int left, right;
21060 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21061 if (x + right > 0)
21062 k = i;
21063 x -= glyphs[i].pixel_width;
21064 }
21065
21066 return k;
21067 }
21068
21069
21070 /* Return the index of the last glyph following glyph string S that is
21071 overwritten by S because of S's right overhang. Value is -1 if
21072 no such glyph is found. */
21073
21074 static int
21075 right_overwritten (struct glyph_string *s)
21076 {
21077 int k = -1;
21078
21079 if (s->right_overhang)
21080 {
21081 int x = 0, i;
21082 struct glyph *glyphs = s->row->glyphs[s->area];
21083 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21084 int end = s->row->used[s->area];
21085
21086 for (i = first; i < end && s->right_overhang > x; ++i)
21087 x += glyphs[i].pixel_width;
21088
21089 k = i;
21090 }
21091
21092 return k;
21093 }
21094
21095
21096 /* Return the index of the last glyph following glyph string S that
21097 overwrites S because of its left overhang. Value is negative
21098 if no such glyph is found. */
21099
21100 static int
21101 right_overwriting (struct glyph_string *s)
21102 {
21103 int i, k, x;
21104 int end = s->row->used[s->area];
21105 struct glyph *glyphs = s->row->glyphs[s->area];
21106 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21107
21108 k = -1;
21109 x = 0;
21110 for (i = first; i < end; ++i)
21111 {
21112 int left, right;
21113 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21114 if (x - left < 0)
21115 k = i;
21116 x += glyphs[i].pixel_width;
21117 }
21118
21119 return k;
21120 }
21121
21122
21123 /* Set background width of glyph string S. START is the index of the
21124 first glyph following S. LAST_X is the right-most x-position + 1
21125 in the drawing area. */
21126
21127 static INLINE void
21128 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21129 {
21130 /* If the face of this glyph string has to be drawn to the end of
21131 the drawing area, set S->extends_to_end_of_line_p. */
21132
21133 if (start == s->row->used[s->area]
21134 && s->area == TEXT_AREA
21135 && ((s->row->fill_line_p
21136 && (s->hl == DRAW_NORMAL_TEXT
21137 || s->hl == DRAW_IMAGE_RAISED
21138 || s->hl == DRAW_IMAGE_SUNKEN))
21139 || s->hl == DRAW_MOUSE_FACE))
21140 s->extends_to_end_of_line_p = 1;
21141
21142 /* If S extends its face to the end of the line, set its
21143 background_width to the distance to the right edge of the drawing
21144 area. */
21145 if (s->extends_to_end_of_line_p)
21146 s->background_width = last_x - s->x + 1;
21147 else
21148 s->background_width = s->width;
21149 }
21150
21151
21152 /* Compute overhangs and x-positions for glyph string S and its
21153 predecessors, or successors. X is the starting x-position for S.
21154 BACKWARD_P non-zero means process predecessors. */
21155
21156 static void
21157 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21158 {
21159 if (backward_p)
21160 {
21161 while (s)
21162 {
21163 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21164 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21165 x -= s->width;
21166 s->x = x;
21167 s = s->prev;
21168 }
21169 }
21170 else
21171 {
21172 while (s)
21173 {
21174 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21175 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21176 s->x = x;
21177 x += s->width;
21178 s = s->next;
21179 }
21180 }
21181 }
21182
21183
21184
21185 /* The following macros are only called from draw_glyphs below.
21186 They reference the following parameters of that function directly:
21187 `w', `row', `area', and `overlap_p'
21188 as well as the following local variables:
21189 `s', `f', and `hdc' (in W32) */
21190
21191 #ifdef HAVE_NTGUI
21192 /* On W32, silently add local `hdc' variable to argument list of
21193 init_glyph_string. */
21194 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21195 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21196 #else
21197 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21198 init_glyph_string (s, char2b, w, row, area, start, hl)
21199 #endif
21200
21201 /* Add a glyph string for a stretch glyph to the list of strings
21202 between HEAD and TAIL. START is the index of the stretch glyph in
21203 row area AREA of glyph row ROW. END is the index of the last glyph
21204 in that glyph row area. X is the current output position assigned
21205 to the new glyph string constructed. HL overrides that face of the
21206 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21207 is the right-most x-position of the drawing area. */
21208
21209 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21210 and below -- keep them on one line. */
21211 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21212 do \
21213 { \
21214 s = (struct glyph_string *) alloca (sizeof *s); \
21215 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21216 START = fill_stretch_glyph_string (s, row, area, START, END); \
21217 append_glyph_string (&HEAD, &TAIL, s); \
21218 s->x = (X); \
21219 } \
21220 while (0)
21221
21222
21223 /* Add a glyph string for an image glyph to the list of strings
21224 between HEAD and TAIL. START is the index of the image glyph in
21225 row area AREA of glyph row ROW. END is the index of the last glyph
21226 in that glyph row area. X is the current output position assigned
21227 to the new glyph string constructed. HL overrides that face of the
21228 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21229 is the right-most x-position of the drawing area. */
21230
21231 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21232 do \
21233 { \
21234 s = (struct glyph_string *) alloca (sizeof *s); \
21235 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21236 fill_image_glyph_string (s); \
21237 append_glyph_string (&HEAD, &TAIL, s); \
21238 ++START; \
21239 s->x = (X); \
21240 } \
21241 while (0)
21242
21243
21244 /* Add a glyph string for a sequence of character glyphs to the list
21245 of strings between HEAD and TAIL. START is the index of the first
21246 glyph in row area AREA of glyph row ROW that is part of the new
21247 glyph string. END is the index of the last glyph in that glyph row
21248 area. X is the current output position assigned to the new glyph
21249 string constructed. HL overrides that face of the glyph; e.g. it
21250 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21251 right-most x-position of the drawing area. */
21252
21253 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21254 do \
21255 { \
21256 int face_id; \
21257 XChar2b *char2b; \
21258 \
21259 face_id = (row)->glyphs[area][START].face_id; \
21260 \
21261 s = (struct glyph_string *) alloca (sizeof *s); \
21262 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21263 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21264 append_glyph_string (&HEAD, &TAIL, s); \
21265 s->x = (X); \
21266 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21267 } \
21268 while (0)
21269
21270
21271 /* Add a glyph string for a composite sequence to the list of strings
21272 between HEAD and TAIL. START is the index of the first glyph in
21273 row area AREA of glyph row ROW that is part of the new glyph
21274 string. END is the index of the last glyph in that glyph row area.
21275 X is the current output position assigned to the new glyph string
21276 constructed. HL overrides that face of the glyph; e.g. it is
21277 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21278 x-position of the drawing area. */
21279
21280 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21281 do { \
21282 int face_id = (row)->glyphs[area][START].face_id; \
21283 struct face *base_face = FACE_FROM_ID (f, face_id); \
21284 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21285 struct composition *cmp = composition_table[cmp_id]; \
21286 XChar2b *char2b; \
21287 struct glyph_string *first_s; \
21288 int n; \
21289 \
21290 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21291 \
21292 /* Make glyph_strings for each glyph sequence that is drawable by \
21293 the same face, and append them to HEAD/TAIL. */ \
21294 for (n = 0; n < cmp->glyph_len;) \
21295 { \
21296 s = (struct glyph_string *) alloca (sizeof *s); \
21297 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21298 append_glyph_string (&(HEAD), &(TAIL), s); \
21299 s->cmp = cmp; \
21300 s->cmp_from = n; \
21301 s->x = (X); \
21302 if (n == 0) \
21303 first_s = s; \
21304 n = fill_composite_glyph_string (s, base_face, overlaps); \
21305 } \
21306 \
21307 ++START; \
21308 s = first_s; \
21309 } while (0)
21310
21311
21312 /* Add a glyph string for a glyph-string sequence to the list of strings
21313 between HEAD and TAIL. */
21314
21315 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21316 do { \
21317 int face_id; \
21318 XChar2b *char2b; \
21319 Lisp_Object gstring; \
21320 \
21321 face_id = (row)->glyphs[area][START].face_id; \
21322 gstring = (composition_gstring_from_id \
21323 ((row)->glyphs[area][START].u.cmp.id)); \
21324 s = (struct glyph_string *) alloca (sizeof *s); \
21325 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21326 * LGSTRING_GLYPH_LEN (gstring)); \
21327 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21328 append_glyph_string (&(HEAD), &(TAIL), s); \
21329 s->x = (X); \
21330 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21331 } while (0)
21332
21333
21334 /* Add a glyph string for a sequence of glyphless character's glyphs
21335 to the list of strings between HEAD and TAIL. The meanings of
21336 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21337
21338 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21339 do \
21340 { \
21341 int face_id; \
21342 XChar2b *char2b; \
21343 \
21344 face_id = (row)->glyphs[area][START].face_id; \
21345 \
21346 s = (struct glyph_string *) alloca (sizeof *s); \
21347 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21348 append_glyph_string (&HEAD, &TAIL, s); \
21349 s->x = (X); \
21350 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21351 overlaps); \
21352 } \
21353 while (0)
21354
21355
21356 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21357 of AREA of glyph row ROW on window W between indices START and END.
21358 HL overrides the face for drawing glyph strings, e.g. it is
21359 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21360 x-positions of the drawing area.
21361
21362 This is an ugly monster macro construct because we must use alloca
21363 to allocate glyph strings (because draw_glyphs can be called
21364 asynchronously). */
21365
21366 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21367 do \
21368 { \
21369 HEAD = TAIL = NULL; \
21370 while (START < END) \
21371 { \
21372 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21373 switch (first_glyph->type) \
21374 { \
21375 case CHAR_GLYPH: \
21376 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21377 HL, X, LAST_X); \
21378 break; \
21379 \
21380 case COMPOSITE_GLYPH: \
21381 if (first_glyph->u.cmp.automatic) \
21382 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21383 HL, X, LAST_X); \
21384 else \
21385 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21386 HL, X, LAST_X); \
21387 break; \
21388 \
21389 case STRETCH_GLYPH: \
21390 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21391 HL, X, LAST_X); \
21392 break; \
21393 \
21394 case IMAGE_GLYPH: \
21395 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21396 HL, X, LAST_X); \
21397 break; \
21398 \
21399 case GLYPHLESS_GLYPH: \
21400 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21401 HL, X, LAST_X); \
21402 break; \
21403 \
21404 default: \
21405 abort (); \
21406 } \
21407 \
21408 if (s) \
21409 { \
21410 set_glyph_string_background_width (s, START, LAST_X); \
21411 (X) += s->width; \
21412 } \
21413 } \
21414 } while (0)
21415
21416
21417 /* Draw glyphs between START and END in AREA of ROW on window W,
21418 starting at x-position X. X is relative to AREA in W. HL is a
21419 face-override with the following meaning:
21420
21421 DRAW_NORMAL_TEXT draw normally
21422 DRAW_CURSOR draw in cursor face
21423 DRAW_MOUSE_FACE draw in mouse face.
21424 DRAW_INVERSE_VIDEO draw in mode line face
21425 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21426 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21427
21428 If OVERLAPS is non-zero, draw only the foreground of characters and
21429 clip to the physical height of ROW. Non-zero value also defines
21430 the overlapping part to be drawn:
21431
21432 OVERLAPS_PRED overlap with preceding rows
21433 OVERLAPS_SUCC overlap with succeeding rows
21434 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21435 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21436
21437 Value is the x-position reached, relative to AREA of W. */
21438
21439 static int
21440 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21441 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21442 enum draw_glyphs_face hl, int overlaps)
21443 {
21444 struct glyph_string *head, *tail;
21445 struct glyph_string *s;
21446 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21447 int i, j, x_reached, last_x, area_left = 0;
21448 struct frame *f = XFRAME (WINDOW_FRAME (w));
21449 DECLARE_HDC (hdc);
21450
21451 ALLOCATE_HDC (hdc, f);
21452
21453 /* Let's rather be paranoid than getting a SEGV. */
21454 end = min (end, row->used[area]);
21455 start = max (0, start);
21456 start = min (end, start);
21457
21458 /* Translate X to frame coordinates. Set last_x to the right
21459 end of the drawing area. */
21460 if (row->full_width_p)
21461 {
21462 /* X is relative to the left edge of W, without scroll bars
21463 or fringes. */
21464 area_left = WINDOW_LEFT_EDGE_X (w);
21465 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21466 }
21467 else
21468 {
21469 area_left = window_box_left (w, area);
21470 last_x = area_left + window_box_width (w, area);
21471 }
21472 x += area_left;
21473
21474 /* Build a doubly-linked list of glyph_string structures between
21475 head and tail from what we have to draw. Note that the macro
21476 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21477 the reason we use a separate variable `i'. */
21478 i = start;
21479 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21480 if (tail)
21481 x_reached = tail->x + tail->background_width;
21482 else
21483 x_reached = x;
21484
21485 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21486 the row, redraw some glyphs in front or following the glyph
21487 strings built above. */
21488 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21489 {
21490 struct glyph_string *h, *t;
21491 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21492 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21493 int dummy_x = 0;
21494
21495 /* If mouse highlighting is on, we may need to draw adjacent
21496 glyphs using mouse-face highlighting. */
21497 if (area == TEXT_AREA && row->mouse_face_p)
21498 {
21499 struct glyph_row *mouse_beg_row, *mouse_end_row;
21500
21501 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21502 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21503
21504 if (row >= mouse_beg_row && row <= mouse_end_row)
21505 {
21506 check_mouse_face = 1;
21507 mouse_beg_col = (row == mouse_beg_row)
21508 ? hlinfo->mouse_face_beg_col : 0;
21509 mouse_end_col = (row == mouse_end_row)
21510 ? hlinfo->mouse_face_end_col
21511 : row->used[TEXT_AREA];
21512 }
21513 }
21514
21515 /* Compute overhangs for all glyph strings. */
21516 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21517 for (s = head; s; s = s->next)
21518 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21519
21520 /* Prepend glyph strings for glyphs in front of the first glyph
21521 string that are overwritten because of the first glyph
21522 string's left overhang. The background of all strings
21523 prepended must be drawn because the first glyph string
21524 draws over it. */
21525 i = left_overwritten (head);
21526 if (i >= 0)
21527 {
21528 enum draw_glyphs_face overlap_hl;
21529
21530 /* If this row contains mouse highlighting, attempt to draw
21531 the overlapped glyphs with the correct highlight. This
21532 code fails if the overlap encompasses more than one glyph
21533 and mouse-highlight spans only some of these glyphs.
21534 However, making it work perfectly involves a lot more
21535 code, and I don't know if the pathological case occurs in
21536 practice, so we'll stick to this for now. --- cyd */
21537 if (check_mouse_face
21538 && mouse_beg_col < start && mouse_end_col > i)
21539 overlap_hl = DRAW_MOUSE_FACE;
21540 else
21541 overlap_hl = DRAW_NORMAL_TEXT;
21542
21543 j = i;
21544 BUILD_GLYPH_STRINGS (j, start, h, t,
21545 overlap_hl, dummy_x, last_x);
21546 start = i;
21547 compute_overhangs_and_x (t, head->x, 1);
21548 prepend_glyph_string_lists (&head, &tail, h, t);
21549 clip_head = head;
21550 }
21551
21552 /* Prepend glyph strings for glyphs in front of the first glyph
21553 string that overwrite that glyph string because of their
21554 right overhang. For these strings, only the foreground must
21555 be drawn, because it draws over the glyph string at `head'.
21556 The background must not be drawn because this would overwrite
21557 right overhangs of preceding glyphs for which no glyph
21558 strings exist. */
21559 i = left_overwriting (head);
21560 if (i >= 0)
21561 {
21562 enum draw_glyphs_face overlap_hl;
21563
21564 if (check_mouse_face
21565 && mouse_beg_col < start && mouse_end_col > i)
21566 overlap_hl = DRAW_MOUSE_FACE;
21567 else
21568 overlap_hl = DRAW_NORMAL_TEXT;
21569
21570 clip_head = head;
21571 BUILD_GLYPH_STRINGS (i, start, h, t,
21572 overlap_hl, dummy_x, last_x);
21573 for (s = h; s; s = s->next)
21574 s->background_filled_p = 1;
21575 compute_overhangs_and_x (t, head->x, 1);
21576 prepend_glyph_string_lists (&head, &tail, h, t);
21577 }
21578
21579 /* Append glyphs strings for glyphs following the last glyph
21580 string tail that are overwritten by tail. The background of
21581 these strings has to be drawn because tail's foreground draws
21582 over it. */
21583 i = right_overwritten (tail);
21584 if (i >= 0)
21585 {
21586 enum draw_glyphs_face overlap_hl;
21587
21588 if (check_mouse_face
21589 && mouse_beg_col < i && mouse_end_col > end)
21590 overlap_hl = DRAW_MOUSE_FACE;
21591 else
21592 overlap_hl = DRAW_NORMAL_TEXT;
21593
21594 BUILD_GLYPH_STRINGS (end, i, h, t,
21595 overlap_hl, x, last_x);
21596 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21597 we don't have `end = i;' here. */
21598 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21599 append_glyph_string_lists (&head, &tail, h, t);
21600 clip_tail = tail;
21601 }
21602
21603 /* Append glyph strings for glyphs following the last glyph
21604 string tail that overwrite tail. The foreground of such
21605 glyphs has to be drawn because it writes into the background
21606 of tail. The background must not be drawn because it could
21607 paint over the foreground of following glyphs. */
21608 i = right_overwriting (tail);
21609 if (i >= 0)
21610 {
21611 enum draw_glyphs_face overlap_hl;
21612 if (check_mouse_face
21613 && mouse_beg_col < i && mouse_end_col > end)
21614 overlap_hl = DRAW_MOUSE_FACE;
21615 else
21616 overlap_hl = DRAW_NORMAL_TEXT;
21617
21618 clip_tail = tail;
21619 i++; /* We must include the Ith glyph. */
21620 BUILD_GLYPH_STRINGS (end, i, h, t,
21621 overlap_hl, x, last_x);
21622 for (s = h; s; s = s->next)
21623 s->background_filled_p = 1;
21624 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21625 append_glyph_string_lists (&head, &tail, h, t);
21626 }
21627 if (clip_head || clip_tail)
21628 for (s = head; s; s = s->next)
21629 {
21630 s->clip_head = clip_head;
21631 s->clip_tail = clip_tail;
21632 }
21633 }
21634
21635 /* Draw all strings. */
21636 for (s = head; s; s = s->next)
21637 FRAME_RIF (f)->draw_glyph_string (s);
21638
21639 #ifndef HAVE_NS
21640 /* When focus a sole frame and move horizontally, this sets on_p to 0
21641 causing a failure to erase prev cursor position. */
21642 if (area == TEXT_AREA
21643 && !row->full_width_p
21644 /* When drawing overlapping rows, only the glyph strings'
21645 foreground is drawn, which doesn't erase a cursor
21646 completely. */
21647 && !overlaps)
21648 {
21649 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21650 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21651 : (tail ? tail->x + tail->background_width : x));
21652 x0 -= area_left;
21653 x1 -= area_left;
21654
21655 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21656 row->y, MATRIX_ROW_BOTTOM_Y (row));
21657 }
21658 #endif
21659
21660 /* Value is the x-position up to which drawn, relative to AREA of W.
21661 This doesn't include parts drawn because of overhangs. */
21662 if (row->full_width_p)
21663 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21664 else
21665 x_reached -= area_left;
21666
21667 RELEASE_HDC (hdc, f);
21668
21669 return x_reached;
21670 }
21671
21672 /* Expand row matrix if too narrow. Don't expand if area
21673 is not present. */
21674
21675 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21676 { \
21677 if (!fonts_changed_p \
21678 && (it->glyph_row->glyphs[area] \
21679 < it->glyph_row->glyphs[area + 1])) \
21680 { \
21681 it->w->ncols_scale_factor++; \
21682 fonts_changed_p = 1; \
21683 } \
21684 }
21685
21686 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21687 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21688
21689 static INLINE void
21690 append_glyph (struct it *it)
21691 {
21692 struct glyph *glyph;
21693 enum glyph_row_area area = it->area;
21694
21695 xassert (it->glyph_row);
21696 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21697
21698 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21699 if (glyph < it->glyph_row->glyphs[area + 1])
21700 {
21701 /* If the glyph row is reversed, we need to prepend the glyph
21702 rather than append it. */
21703 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21704 {
21705 struct glyph *g;
21706
21707 /* Make room for the additional glyph. */
21708 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21709 g[1] = *g;
21710 glyph = it->glyph_row->glyphs[area];
21711 }
21712 glyph->charpos = CHARPOS (it->position);
21713 glyph->object = it->object;
21714 if (it->pixel_width > 0)
21715 {
21716 glyph->pixel_width = it->pixel_width;
21717 glyph->padding_p = 0;
21718 }
21719 else
21720 {
21721 /* Assure at least 1-pixel width. Otherwise, cursor can't
21722 be displayed correctly. */
21723 glyph->pixel_width = 1;
21724 glyph->padding_p = 1;
21725 }
21726 glyph->ascent = it->ascent;
21727 glyph->descent = it->descent;
21728 glyph->voffset = it->voffset;
21729 glyph->type = CHAR_GLYPH;
21730 glyph->avoid_cursor_p = it->avoid_cursor_p;
21731 glyph->multibyte_p = it->multibyte_p;
21732 glyph->left_box_line_p = it->start_of_box_run_p;
21733 glyph->right_box_line_p = it->end_of_box_run_p;
21734 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21735 || it->phys_descent > it->descent);
21736 glyph->glyph_not_available_p = it->glyph_not_available_p;
21737 glyph->face_id = it->face_id;
21738 glyph->u.ch = it->char_to_display;
21739 glyph->slice.img = null_glyph_slice;
21740 glyph->font_type = FONT_TYPE_UNKNOWN;
21741 if (it->bidi_p)
21742 {
21743 glyph->resolved_level = it->bidi_it.resolved_level;
21744 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21745 abort ();
21746 glyph->bidi_type = it->bidi_it.type;
21747 }
21748 else
21749 {
21750 glyph->resolved_level = 0;
21751 glyph->bidi_type = UNKNOWN_BT;
21752 }
21753 ++it->glyph_row->used[area];
21754 }
21755 else
21756 IT_EXPAND_MATRIX_WIDTH (it, area);
21757 }
21758
21759 /* Store one glyph for the composition IT->cmp_it.id in
21760 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21761 non-null. */
21762
21763 static INLINE void
21764 append_composite_glyph (struct it *it)
21765 {
21766 struct glyph *glyph;
21767 enum glyph_row_area area = it->area;
21768
21769 xassert (it->glyph_row);
21770
21771 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21772 if (glyph < it->glyph_row->glyphs[area + 1])
21773 {
21774 /* If the glyph row is reversed, we need to prepend the glyph
21775 rather than append it. */
21776 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21777 {
21778 struct glyph *g;
21779
21780 /* Make room for the new glyph. */
21781 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21782 g[1] = *g;
21783 glyph = it->glyph_row->glyphs[it->area];
21784 }
21785 glyph->charpos = it->cmp_it.charpos;
21786 glyph->object = it->object;
21787 glyph->pixel_width = it->pixel_width;
21788 glyph->ascent = it->ascent;
21789 glyph->descent = it->descent;
21790 glyph->voffset = it->voffset;
21791 glyph->type = COMPOSITE_GLYPH;
21792 if (it->cmp_it.ch < 0)
21793 {
21794 glyph->u.cmp.automatic = 0;
21795 glyph->u.cmp.id = it->cmp_it.id;
21796 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21797 }
21798 else
21799 {
21800 glyph->u.cmp.automatic = 1;
21801 glyph->u.cmp.id = it->cmp_it.id;
21802 glyph->slice.cmp.from = it->cmp_it.from;
21803 glyph->slice.cmp.to = it->cmp_it.to - 1;
21804 }
21805 glyph->avoid_cursor_p = it->avoid_cursor_p;
21806 glyph->multibyte_p = it->multibyte_p;
21807 glyph->left_box_line_p = it->start_of_box_run_p;
21808 glyph->right_box_line_p = it->end_of_box_run_p;
21809 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21810 || it->phys_descent > it->descent);
21811 glyph->padding_p = 0;
21812 glyph->glyph_not_available_p = 0;
21813 glyph->face_id = it->face_id;
21814 glyph->font_type = FONT_TYPE_UNKNOWN;
21815 if (it->bidi_p)
21816 {
21817 glyph->resolved_level = it->bidi_it.resolved_level;
21818 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21819 abort ();
21820 glyph->bidi_type = it->bidi_it.type;
21821 }
21822 ++it->glyph_row->used[area];
21823 }
21824 else
21825 IT_EXPAND_MATRIX_WIDTH (it, area);
21826 }
21827
21828
21829 /* Change IT->ascent and IT->height according to the setting of
21830 IT->voffset. */
21831
21832 static INLINE void
21833 take_vertical_position_into_account (struct it *it)
21834 {
21835 if (it->voffset)
21836 {
21837 if (it->voffset < 0)
21838 /* Increase the ascent so that we can display the text higher
21839 in the line. */
21840 it->ascent -= it->voffset;
21841 else
21842 /* Increase the descent so that we can display the text lower
21843 in the line. */
21844 it->descent += it->voffset;
21845 }
21846 }
21847
21848
21849 /* Produce glyphs/get display metrics for the image IT is loaded with.
21850 See the description of struct display_iterator in dispextern.h for
21851 an overview of struct display_iterator. */
21852
21853 static void
21854 produce_image_glyph (struct it *it)
21855 {
21856 struct image *img;
21857 struct face *face;
21858 int glyph_ascent, crop;
21859 struct glyph_slice slice;
21860
21861 xassert (it->what == IT_IMAGE);
21862
21863 face = FACE_FROM_ID (it->f, it->face_id);
21864 xassert (face);
21865 /* Make sure X resources of the face is loaded. */
21866 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21867
21868 if (it->image_id < 0)
21869 {
21870 /* Fringe bitmap. */
21871 it->ascent = it->phys_ascent = 0;
21872 it->descent = it->phys_descent = 0;
21873 it->pixel_width = 0;
21874 it->nglyphs = 0;
21875 return;
21876 }
21877
21878 img = IMAGE_FROM_ID (it->f, it->image_id);
21879 xassert (img);
21880 /* Make sure X resources of the image is loaded. */
21881 prepare_image_for_display (it->f, img);
21882
21883 slice.x = slice.y = 0;
21884 slice.width = img->width;
21885 slice.height = img->height;
21886
21887 if (INTEGERP (it->slice.x))
21888 slice.x = XINT (it->slice.x);
21889 else if (FLOATP (it->slice.x))
21890 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21891
21892 if (INTEGERP (it->slice.y))
21893 slice.y = XINT (it->slice.y);
21894 else if (FLOATP (it->slice.y))
21895 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21896
21897 if (INTEGERP (it->slice.width))
21898 slice.width = XINT (it->slice.width);
21899 else if (FLOATP (it->slice.width))
21900 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21901
21902 if (INTEGERP (it->slice.height))
21903 slice.height = XINT (it->slice.height);
21904 else if (FLOATP (it->slice.height))
21905 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21906
21907 if (slice.x >= img->width)
21908 slice.x = img->width;
21909 if (slice.y >= img->height)
21910 slice.y = img->height;
21911 if (slice.x + slice.width >= img->width)
21912 slice.width = img->width - slice.x;
21913 if (slice.y + slice.height > img->height)
21914 slice.height = img->height - slice.y;
21915
21916 if (slice.width == 0 || slice.height == 0)
21917 return;
21918
21919 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21920
21921 it->descent = slice.height - glyph_ascent;
21922 if (slice.y == 0)
21923 it->descent += img->vmargin;
21924 if (slice.y + slice.height == img->height)
21925 it->descent += img->vmargin;
21926 it->phys_descent = it->descent;
21927
21928 it->pixel_width = slice.width;
21929 if (slice.x == 0)
21930 it->pixel_width += img->hmargin;
21931 if (slice.x + slice.width == img->width)
21932 it->pixel_width += img->hmargin;
21933
21934 /* It's quite possible for images to have an ascent greater than
21935 their height, so don't get confused in that case. */
21936 if (it->descent < 0)
21937 it->descent = 0;
21938
21939 it->nglyphs = 1;
21940
21941 if (face->box != FACE_NO_BOX)
21942 {
21943 if (face->box_line_width > 0)
21944 {
21945 if (slice.y == 0)
21946 it->ascent += face->box_line_width;
21947 if (slice.y + slice.height == img->height)
21948 it->descent += face->box_line_width;
21949 }
21950
21951 if (it->start_of_box_run_p && slice.x == 0)
21952 it->pixel_width += eabs (face->box_line_width);
21953 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21954 it->pixel_width += eabs (face->box_line_width);
21955 }
21956
21957 take_vertical_position_into_account (it);
21958
21959 /* Automatically crop wide image glyphs at right edge so we can
21960 draw the cursor on same display row. */
21961 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21962 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21963 {
21964 it->pixel_width -= crop;
21965 slice.width -= crop;
21966 }
21967
21968 if (it->glyph_row)
21969 {
21970 struct glyph *glyph;
21971 enum glyph_row_area area = it->area;
21972
21973 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21974 if (glyph < it->glyph_row->glyphs[area + 1])
21975 {
21976 glyph->charpos = CHARPOS (it->position);
21977 glyph->object = it->object;
21978 glyph->pixel_width = it->pixel_width;
21979 glyph->ascent = glyph_ascent;
21980 glyph->descent = it->descent;
21981 glyph->voffset = it->voffset;
21982 glyph->type = IMAGE_GLYPH;
21983 glyph->avoid_cursor_p = it->avoid_cursor_p;
21984 glyph->multibyte_p = it->multibyte_p;
21985 glyph->left_box_line_p = it->start_of_box_run_p;
21986 glyph->right_box_line_p = it->end_of_box_run_p;
21987 glyph->overlaps_vertically_p = 0;
21988 glyph->padding_p = 0;
21989 glyph->glyph_not_available_p = 0;
21990 glyph->face_id = it->face_id;
21991 glyph->u.img_id = img->id;
21992 glyph->slice.img = slice;
21993 glyph->font_type = FONT_TYPE_UNKNOWN;
21994 if (it->bidi_p)
21995 {
21996 glyph->resolved_level = it->bidi_it.resolved_level;
21997 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21998 abort ();
21999 glyph->bidi_type = it->bidi_it.type;
22000 }
22001 ++it->glyph_row->used[area];
22002 }
22003 else
22004 IT_EXPAND_MATRIX_WIDTH (it, area);
22005 }
22006 }
22007
22008
22009 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22010 of the glyph, WIDTH and HEIGHT are the width and height of the
22011 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22012
22013 static void
22014 append_stretch_glyph (struct it *it, Lisp_Object object,
22015 int width, int height, int ascent)
22016 {
22017 struct glyph *glyph;
22018 enum glyph_row_area area = it->area;
22019
22020 xassert (ascent >= 0 && ascent <= height);
22021
22022 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22023 if (glyph < it->glyph_row->glyphs[area + 1])
22024 {
22025 /* If the glyph row is reversed, we need to prepend the glyph
22026 rather than append it. */
22027 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22028 {
22029 struct glyph *g;
22030
22031 /* Make room for the additional glyph. */
22032 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22033 g[1] = *g;
22034 glyph = it->glyph_row->glyphs[area];
22035 }
22036 glyph->charpos = CHARPOS (it->position);
22037 glyph->object = object;
22038 glyph->pixel_width = width;
22039 glyph->ascent = ascent;
22040 glyph->descent = height - ascent;
22041 glyph->voffset = it->voffset;
22042 glyph->type = STRETCH_GLYPH;
22043 glyph->avoid_cursor_p = it->avoid_cursor_p;
22044 glyph->multibyte_p = it->multibyte_p;
22045 glyph->left_box_line_p = it->start_of_box_run_p;
22046 glyph->right_box_line_p = it->end_of_box_run_p;
22047 glyph->overlaps_vertically_p = 0;
22048 glyph->padding_p = 0;
22049 glyph->glyph_not_available_p = 0;
22050 glyph->face_id = it->face_id;
22051 glyph->u.stretch.ascent = ascent;
22052 glyph->u.stretch.height = height;
22053 glyph->slice.img = null_glyph_slice;
22054 glyph->font_type = FONT_TYPE_UNKNOWN;
22055 if (it->bidi_p)
22056 {
22057 glyph->resolved_level = it->bidi_it.resolved_level;
22058 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22059 abort ();
22060 glyph->bidi_type = it->bidi_it.type;
22061 }
22062 else
22063 {
22064 glyph->resolved_level = 0;
22065 glyph->bidi_type = UNKNOWN_BT;
22066 }
22067 ++it->glyph_row->used[area];
22068 }
22069 else
22070 IT_EXPAND_MATRIX_WIDTH (it, area);
22071 }
22072
22073
22074 /* Produce a stretch glyph for iterator IT. IT->object is the value
22075 of the glyph property displayed. The value must be a list
22076 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22077 being recognized:
22078
22079 1. `:width WIDTH' specifies that the space should be WIDTH *
22080 canonical char width wide. WIDTH may be an integer or floating
22081 point number.
22082
22083 2. `:relative-width FACTOR' specifies that the width of the stretch
22084 should be computed from the width of the first character having the
22085 `glyph' property, and should be FACTOR times that width.
22086
22087 3. `:align-to HPOS' specifies that the space should be wide enough
22088 to reach HPOS, a value in canonical character units.
22089
22090 Exactly one of the above pairs must be present.
22091
22092 4. `:height HEIGHT' specifies that the height of the stretch produced
22093 should be HEIGHT, measured in canonical character units.
22094
22095 5. `:relative-height FACTOR' specifies that the height of the
22096 stretch should be FACTOR times the height of the characters having
22097 the glyph property.
22098
22099 Either none or exactly one of 4 or 5 must be present.
22100
22101 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22102 of the stretch should be used for the ascent of the stretch.
22103 ASCENT must be in the range 0 <= ASCENT <= 100. */
22104
22105 static void
22106 produce_stretch_glyph (struct it *it)
22107 {
22108 /* (space :width WIDTH :height HEIGHT ...) */
22109 Lisp_Object prop, plist;
22110 int width = 0, height = 0, align_to = -1;
22111 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22112 int ascent = 0;
22113 double tem;
22114 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22115 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22116
22117 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22118
22119 /* List should start with `space'. */
22120 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22121 plist = XCDR (it->object);
22122
22123 /* Compute the width of the stretch. */
22124 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22125 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22126 {
22127 /* Absolute width `:width WIDTH' specified and valid. */
22128 zero_width_ok_p = 1;
22129 width = (int)tem;
22130 }
22131 else if (prop = Fplist_get (plist, QCrelative_width),
22132 NUMVAL (prop) > 0)
22133 {
22134 /* Relative width `:relative-width FACTOR' specified and valid.
22135 Compute the width of the characters having the `glyph'
22136 property. */
22137 struct it it2;
22138 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22139
22140 it2 = *it;
22141 if (it->multibyte_p)
22142 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22143 else
22144 {
22145 it2.c = it2.char_to_display = *p, it2.len = 1;
22146 if (! ASCII_CHAR_P (it2.c))
22147 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22148 }
22149
22150 it2.glyph_row = NULL;
22151 it2.what = IT_CHARACTER;
22152 x_produce_glyphs (&it2);
22153 width = NUMVAL (prop) * it2.pixel_width;
22154 }
22155 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22156 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22157 {
22158 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22159 align_to = (align_to < 0
22160 ? 0
22161 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22162 else if (align_to < 0)
22163 align_to = window_box_left_offset (it->w, TEXT_AREA);
22164 width = max (0, (int)tem + align_to - it->current_x);
22165 zero_width_ok_p = 1;
22166 }
22167 else
22168 /* Nothing specified -> width defaults to canonical char width. */
22169 width = FRAME_COLUMN_WIDTH (it->f);
22170
22171 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22172 width = 1;
22173
22174 /* Compute height. */
22175 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22176 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22177 {
22178 height = (int)tem;
22179 zero_height_ok_p = 1;
22180 }
22181 else if (prop = Fplist_get (plist, QCrelative_height),
22182 NUMVAL (prop) > 0)
22183 height = FONT_HEIGHT (font) * NUMVAL (prop);
22184 else
22185 height = FONT_HEIGHT (font);
22186
22187 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22188 height = 1;
22189
22190 /* Compute percentage of height used for ascent. If
22191 `:ascent ASCENT' is present and valid, use that. Otherwise,
22192 derive the ascent from the font in use. */
22193 if (prop = Fplist_get (plist, QCascent),
22194 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22195 ascent = height * NUMVAL (prop) / 100.0;
22196 else if (!NILP (prop)
22197 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22198 ascent = min (max (0, (int)tem), height);
22199 else
22200 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22201
22202 if (width > 0 && it->line_wrap != TRUNCATE
22203 && it->current_x + width > it->last_visible_x)
22204 width = it->last_visible_x - it->current_x - 1;
22205
22206 if (width > 0 && height > 0 && it->glyph_row)
22207 {
22208 Lisp_Object object = it->stack[it->sp - 1].string;
22209 if (!STRINGP (object))
22210 object = it->w->buffer;
22211 append_stretch_glyph (it, object, width, height, ascent);
22212 }
22213
22214 it->pixel_width = width;
22215 it->ascent = it->phys_ascent = ascent;
22216 it->descent = it->phys_descent = height - it->ascent;
22217 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22218
22219 take_vertical_position_into_account (it);
22220 }
22221
22222 /* Calculate line-height and line-spacing properties.
22223 An integer value specifies explicit pixel value.
22224 A float value specifies relative value to current face height.
22225 A cons (float . face-name) specifies relative value to
22226 height of specified face font.
22227
22228 Returns height in pixels, or nil. */
22229
22230
22231 static Lisp_Object
22232 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22233 int boff, int override)
22234 {
22235 Lisp_Object face_name = Qnil;
22236 int ascent, descent, height;
22237
22238 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22239 return val;
22240
22241 if (CONSP (val))
22242 {
22243 face_name = XCAR (val);
22244 val = XCDR (val);
22245 if (!NUMBERP (val))
22246 val = make_number (1);
22247 if (NILP (face_name))
22248 {
22249 height = it->ascent + it->descent;
22250 goto scale;
22251 }
22252 }
22253
22254 if (NILP (face_name))
22255 {
22256 font = FRAME_FONT (it->f);
22257 boff = FRAME_BASELINE_OFFSET (it->f);
22258 }
22259 else if (EQ (face_name, Qt))
22260 {
22261 override = 0;
22262 }
22263 else
22264 {
22265 int face_id;
22266 struct face *face;
22267
22268 face_id = lookup_named_face (it->f, face_name, 0);
22269 if (face_id < 0)
22270 return make_number (-1);
22271
22272 face = FACE_FROM_ID (it->f, face_id);
22273 font = face->font;
22274 if (font == NULL)
22275 return make_number (-1);
22276 boff = font->baseline_offset;
22277 if (font->vertical_centering)
22278 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22279 }
22280
22281 ascent = FONT_BASE (font) + boff;
22282 descent = FONT_DESCENT (font) - boff;
22283
22284 if (override)
22285 {
22286 it->override_ascent = ascent;
22287 it->override_descent = descent;
22288 it->override_boff = boff;
22289 }
22290
22291 height = ascent + descent;
22292
22293 scale:
22294 if (FLOATP (val))
22295 height = (int)(XFLOAT_DATA (val) * height);
22296 else if (INTEGERP (val))
22297 height *= XINT (val);
22298
22299 return make_number (height);
22300 }
22301
22302
22303 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22304 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22305 and only if this is for a character for which no font was found.
22306
22307 If the display method (it->glyphless_method) is
22308 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22309 length of the acronym or the hexadecimal string, UPPER_XOFF and
22310 UPPER_YOFF are pixel offsets for the upper part of the string,
22311 LOWER_XOFF and LOWER_YOFF are for the lower part.
22312
22313 For the other display methods, LEN through LOWER_YOFF are zero. */
22314
22315 static void
22316 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22317 short upper_xoff, short upper_yoff,
22318 short lower_xoff, short lower_yoff)
22319 {
22320 struct glyph *glyph;
22321 enum glyph_row_area area = it->area;
22322
22323 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22324 if (glyph < it->glyph_row->glyphs[area + 1])
22325 {
22326 /* If the glyph row is reversed, we need to prepend the glyph
22327 rather than append it. */
22328 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22329 {
22330 struct glyph *g;
22331
22332 /* Make room for the additional glyph. */
22333 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22334 g[1] = *g;
22335 glyph = it->glyph_row->glyphs[area];
22336 }
22337 glyph->charpos = CHARPOS (it->position);
22338 glyph->object = it->object;
22339 glyph->pixel_width = it->pixel_width;
22340 glyph->ascent = it->ascent;
22341 glyph->descent = it->descent;
22342 glyph->voffset = it->voffset;
22343 glyph->type = GLYPHLESS_GLYPH;
22344 glyph->u.glyphless.method = it->glyphless_method;
22345 glyph->u.glyphless.for_no_font = for_no_font;
22346 glyph->u.glyphless.len = len;
22347 glyph->u.glyphless.ch = it->c;
22348 glyph->slice.glyphless.upper_xoff = upper_xoff;
22349 glyph->slice.glyphless.upper_yoff = upper_yoff;
22350 glyph->slice.glyphless.lower_xoff = lower_xoff;
22351 glyph->slice.glyphless.lower_yoff = lower_yoff;
22352 glyph->avoid_cursor_p = it->avoid_cursor_p;
22353 glyph->multibyte_p = it->multibyte_p;
22354 glyph->left_box_line_p = it->start_of_box_run_p;
22355 glyph->right_box_line_p = it->end_of_box_run_p;
22356 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22357 || it->phys_descent > it->descent);
22358 glyph->padding_p = 0;
22359 glyph->glyph_not_available_p = 0;
22360 glyph->face_id = face_id;
22361 glyph->font_type = FONT_TYPE_UNKNOWN;
22362 if (it->bidi_p)
22363 {
22364 glyph->resolved_level = it->bidi_it.resolved_level;
22365 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22366 abort ();
22367 glyph->bidi_type = it->bidi_it.type;
22368 }
22369 ++it->glyph_row->used[area];
22370 }
22371 else
22372 IT_EXPAND_MATRIX_WIDTH (it, area);
22373 }
22374
22375
22376 /* Produce a glyph for a glyphless character for iterator IT.
22377 IT->glyphless_method specifies which method to use for displaying
22378 the character. See the description of enum
22379 glyphless_display_method in dispextern.h for the detail.
22380
22381 FOR_NO_FONT is nonzero if and only if this is for a character for
22382 which no font was found. ACRONYM, if non-nil, is an acronym string
22383 for the character. */
22384
22385 static void
22386 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22387 {
22388 int face_id;
22389 struct face *face;
22390 struct font *font;
22391 int base_width, base_height, width, height;
22392 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22393 int len;
22394
22395 /* Get the metrics of the base font. We always refer to the current
22396 ASCII face. */
22397 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22398 font = face->font ? face->font : FRAME_FONT (it->f);
22399 it->ascent = FONT_BASE (font) + font->baseline_offset;
22400 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22401 base_height = it->ascent + it->descent;
22402 base_width = font->average_width;
22403
22404 /* Get a face ID for the glyph by utilizing a cache (the same way as
22405 doen for `escape-glyph' in get_next_display_element). */
22406 if (it->f == last_glyphless_glyph_frame
22407 && it->face_id == last_glyphless_glyph_face_id)
22408 {
22409 face_id = last_glyphless_glyph_merged_face_id;
22410 }
22411 else
22412 {
22413 /* Merge the `glyphless-char' face into the current face. */
22414 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22415 last_glyphless_glyph_frame = it->f;
22416 last_glyphless_glyph_face_id = it->face_id;
22417 last_glyphless_glyph_merged_face_id = face_id;
22418 }
22419
22420 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22421 {
22422 it->pixel_width = THIN_SPACE_WIDTH;
22423 len = 0;
22424 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22425 }
22426 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22427 {
22428 width = CHAR_WIDTH (it->c);
22429 if (width == 0)
22430 width = 1;
22431 else if (width > 4)
22432 width = 4;
22433 it->pixel_width = base_width * width;
22434 len = 0;
22435 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22436 }
22437 else
22438 {
22439 char buf[7], *str;
22440 unsigned int code[6];
22441 int upper_len;
22442 int ascent, descent;
22443 struct font_metrics metrics_upper, metrics_lower;
22444
22445 face = FACE_FROM_ID (it->f, face_id);
22446 font = face->font ? face->font : FRAME_FONT (it->f);
22447 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22448
22449 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22450 {
22451 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22452 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22453 str = STRINGP (acronym) ? (char *) SDATA (acronym) : "";
22454 }
22455 else
22456 {
22457 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22458 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22459 str = buf;
22460 }
22461 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22462 code[len] = font->driver->encode_char (font, str[len]);
22463 upper_len = (len + 1) / 2;
22464 font->driver->text_extents (font, code, upper_len,
22465 &metrics_upper);
22466 font->driver->text_extents (font, code + upper_len, len - upper_len,
22467 &metrics_lower);
22468
22469
22470
22471 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22472 width = max (metrics_upper.width, metrics_lower.width) + 4;
22473 upper_xoff = upper_yoff = 2; /* the typical case */
22474 if (base_width >= width)
22475 {
22476 /* Align the upper to the left, the lower to the right. */
22477 it->pixel_width = base_width;
22478 lower_xoff = base_width - 2 - metrics_lower.width;
22479 }
22480 else
22481 {
22482 /* Center the shorter one. */
22483 it->pixel_width = width;
22484 if (metrics_upper.width >= metrics_lower.width)
22485 lower_xoff = (width - metrics_lower.width) / 2;
22486 else
22487 upper_xoff = (width - metrics_upper.width) / 2;
22488 }
22489
22490 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22491 top, bottom, and between upper and lower strings. */
22492 height = (metrics_upper.ascent + metrics_upper.descent
22493 + metrics_lower.ascent + metrics_lower.descent) + 5;
22494 /* Center vertically.
22495 H:base_height, D:base_descent
22496 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22497
22498 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22499 descent = D - H/2 + h/2;
22500 lower_yoff = descent - 2 - ld;
22501 upper_yoff = lower_yoff - la - 1 - ud; */
22502 ascent = - (it->descent - (base_height + height + 1) / 2);
22503 descent = it->descent - (base_height - height) / 2;
22504 lower_yoff = descent - 2 - metrics_lower.descent;
22505 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22506 - metrics_upper.descent);
22507 /* Don't make the height shorter than the base height. */
22508 if (height > base_height)
22509 {
22510 it->ascent = ascent;
22511 it->descent = descent;
22512 }
22513 }
22514
22515 it->phys_ascent = it->ascent;
22516 it->phys_descent = it->descent;
22517 if (it->glyph_row)
22518 append_glyphless_glyph (it, face_id, for_no_font, len,
22519 upper_xoff, upper_yoff,
22520 lower_xoff, lower_yoff);
22521 it->nglyphs = 1;
22522 take_vertical_position_into_account (it);
22523 }
22524
22525
22526 /* RIF:
22527 Produce glyphs/get display metrics for the display element IT is
22528 loaded with. See the description of struct it in dispextern.h
22529 for an overview of struct it. */
22530
22531 void
22532 x_produce_glyphs (struct it *it)
22533 {
22534 int extra_line_spacing = it->extra_line_spacing;
22535
22536 it->glyph_not_available_p = 0;
22537
22538 if (it->what == IT_CHARACTER)
22539 {
22540 XChar2b char2b;
22541 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22542 struct font *font = face->font;
22543 struct font_metrics *pcm = NULL;
22544 int boff; /* baseline offset */
22545
22546 if (font == NULL)
22547 {
22548 /* When no suitable font is found, display this character by
22549 the method specified in the first extra slot of
22550 Vglyphless_char_display. */
22551 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22552
22553 xassert (it->what == IT_GLYPHLESS);
22554 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22555 goto done;
22556 }
22557
22558 boff = font->baseline_offset;
22559 if (font->vertical_centering)
22560 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22561
22562 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22563 {
22564 int stretched_p;
22565
22566 it->nglyphs = 1;
22567
22568 if (it->override_ascent >= 0)
22569 {
22570 it->ascent = it->override_ascent;
22571 it->descent = it->override_descent;
22572 boff = it->override_boff;
22573 }
22574 else
22575 {
22576 it->ascent = FONT_BASE (font) + boff;
22577 it->descent = FONT_DESCENT (font) - boff;
22578 }
22579
22580 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22581 {
22582 pcm = get_per_char_metric (it->f, font, &char2b);
22583 if (pcm->width == 0
22584 && pcm->rbearing == 0 && pcm->lbearing == 0)
22585 pcm = NULL;
22586 }
22587
22588 if (pcm)
22589 {
22590 it->phys_ascent = pcm->ascent + boff;
22591 it->phys_descent = pcm->descent - boff;
22592 it->pixel_width = pcm->width;
22593 }
22594 else
22595 {
22596 it->glyph_not_available_p = 1;
22597 it->phys_ascent = it->ascent;
22598 it->phys_descent = it->descent;
22599 it->pixel_width = font->space_width;
22600 }
22601
22602 if (it->constrain_row_ascent_descent_p)
22603 {
22604 if (it->descent > it->max_descent)
22605 {
22606 it->ascent += it->descent - it->max_descent;
22607 it->descent = it->max_descent;
22608 }
22609 if (it->ascent > it->max_ascent)
22610 {
22611 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22612 it->ascent = it->max_ascent;
22613 }
22614 it->phys_ascent = min (it->phys_ascent, it->ascent);
22615 it->phys_descent = min (it->phys_descent, it->descent);
22616 extra_line_spacing = 0;
22617 }
22618
22619 /* If this is a space inside a region of text with
22620 `space-width' property, change its width. */
22621 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22622 if (stretched_p)
22623 it->pixel_width *= XFLOATINT (it->space_width);
22624
22625 /* If face has a box, add the box thickness to the character
22626 height. If character has a box line to the left and/or
22627 right, add the box line width to the character's width. */
22628 if (face->box != FACE_NO_BOX)
22629 {
22630 int thick = face->box_line_width;
22631
22632 if (thick > 0)
22633 {
22634 it->ascent += thick;
22635 it->descent += thick;
22636 }
22637 else
22638 thick = -thick;
22639
22640 if (it->start_of_box_run_p)
22641 it->pixel_width += thick;
22642 if (it->end_of_box_run_p)
22643 it->pixel_width += thick;
22644 }
22645
22646 /* If face has an overline, add the height of the overline
22647 (1 pixel) and a 1 pixel margin to the character height. */
22648 if (face->overline_p)
22649 it->ascent += overline_margin;
22650
22651 if (it->constrain_row_ascent_descent_p)
22652 {
22653 if (it->ascent > it->max_ascent)
22654 it->ascent = it->max_ascent;
22655 if (it->descent > it->max_descent)
22656 it->descent = it->max_descent;
22657 }
22658
22659 take_vertical_position_into_account (it);
22660
22661 /* If we have to actually produce glyphs, do it. */
22662 if (it->glyph_row)
22663 {
22664 if (stretched_p)
22665 {
22666 /* Translate a space with a `space-width' property
22667 into a stretch glyph. */
22668 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22669 / FONT_HEIGHT (font));
22670 append_stretch_glyph (it, it->object, it->pixel_width,
22671 it->ascent + it->descent, ascent);
22672 }
22673 else
22674 append_glyph (it);
22675
22676 /* If characters with lbearing or rbearing are displayed
22677 in this line, record that fact in a flag of the
22678 glyph row. This is used to optimize X output code. */
22679 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22680 it->glyph_row->contains_overlapping_glyphs_p = 1;
22681 }
22682 if (! stretched_p && it->pixel_width == 0)
22683 /* We assure that all visible glyphs have at least 1-pixel
22684 width. */
22685 it->pixel_width = 1;
22686 }
22687 else if (it->char_to_display == '\n')
22688 {
22689 /* A newline has no width, but we need the height of the
22690 line. But if previous part of the line sets a height,
22691 don't increase that height */
22692
22693 Lisp_Object height;
22694 Lisp_Object total_height = Qnil;
22695
22696 it->override_ascent = -1;
22697 it->pixel_width = 0;
22698 it->nglyphs = 0;
22699
22700 height = get_it_property (it, Qline_height);
22701 /* Split (line-height total-height) list */
22702 if (CONSP (height)
22703 && CONSP (XCDR (height))
22704 && NILP (XCDR (XCDR (height))))
22705 {
22706 total_height = XCAR (XCDR (height));
22707 height = XCAR (height);
22708 }
22709 height = calc_line_height_property (it, height, font, boff, 1);
22710
22711 if (it->override_ascent >= 0)
22712 {
22713 it->ascent = it->override_ascent;
22714 it->descent = it->override_descent;
22715 boff = it->override_boff;
22716 }
22717 else
22718 {
22719 it->ascent = FONT_BASE (font) + boff;
22720 it->descent = FONT_DESCENT (font) - boff;
22721 }
22722
22723 if (EQ (height, Qt))
22724 {
22725 if (it->descent > it->max_descent)
22726 {
22727 it->ascent += it->descent - it->max_descent;
22728 it->descent = it->max_descent;
22729 }
22730 if (it->ascent > it->max_ascent)
22731 {
22732 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22733 it->ascent = it->max_ascent;
22734 }
22735 it->phys_ascent = min (it->phys_ascent, it->ascent);
22736 it->phys_descent = min (it->phys_descent, it->descent);
22737 it->constrain_row_ascent_descent_p = 1;
22738 extra_line_spacing = 0;
22739 }
22740 else
22741 {
22742 Lisp_Object spacing;
22743
22744 it->phys_ascent = it->ascent;
22745 it->phys_descent = it->descent;
22746
22747 if ((it->max_ascent > 0 || it->max_descent > 0)
22748 && face->box != FACE_NO_BOX
22749 && face->box_line_width > 0)
22750 {
22751 it->ascent += face->box_line_width;
22752 it->descent += face->box_line_width;
22753 }
22754 if (!NILP (height)
22755 && XINT (height) > it->ascent + it->descent)
22756 it->ascent = XINT (height) - it->descent;
22757
22758 if (!NILP (total_height))
22759 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22760 else
22761 {
22762 spacing = get_it_property (it, Qline_spacing);
22763 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22764 }
22765 if (INTEGERP (spacing))
22766 {
22767 extra_line_spacing = XINT (spacing);
22768 if (!NILP (total_height))
22769 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22770 }
22771 }
22772 }
22773 else /* i.e. (it->char_to_display == '\t') */
22774 {
22775 if (font->space_width > 0)
22776 {
22777 int tab_width = it->tab_width * font->space_width;
22778 int x = it->current_x + it->continuation_lines_width;
22779 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22780
22781 /* If the distance from the current position to the next tab
22782 stop is less than a space character width, use the
22783 tab stop after that. */
22784 if (next_tab_x - x < font->space_width)
22785 next_tab_x += tab_width;
22786
22787 it->pixel_width = next_tab_x - x;
22788 it->nglyphs = 1;
22789 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22790 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22791
22792 if (it->glyph_row)
22793 {
22794 append_stretch_glyph (it, it->object, it->pixel_width,
22795 it->ascent + it->descent, it->ascent);
22796 }
22797 }
22798 else
22799 {
22800 it->pixel_width = 0;
22801 it->nglyphs = 1;
22802 }
22803 }
22804 }
22805 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22806 {
22807 /* A static composition.
22808
22809 Note: A composition is represented as one glyph in the
22810 glyph matrix. There are no padding glyphs.
22811
22812 Important note: pixel_width, ascent, and descent are the
22813 values of what is drawn by draw_glyphs (i.e. the values of
22814 the overall glyphs composed). */
22815 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22816 int boff; /* baseline offset */
22817 struct composition *cmp = composition_table[it->cmp_it.id];
22818 int glyph_len = cmp->glyph_len;
22819 struct font *font = face->font;
22820
22821 it->nglyphs = 1;
22822
22823 /* If we have not yet calculated pixel size data of glyphs of
22824 the composition for the current face font, calculate them
22825 now. Theoretically, we have to check all fonts for the
22826 glyphs, but that requires much time and memory space. So,
22827 here we check only the font of the first glyph. This may
22828 lead to incorrect display, but it's very rare, and C-l
22829 (recenter-top-bottom) can correct the display anyway. */
22830 if (! cmp->font || cmp->font != font)
22831 {
22832 /* Ascent and descent of the font of the first character
22833 of this composition (adjusted by baseline offset).
22834 Ascent and descent of overall glyphs should not be less
22835 than these, respectively. */
22836 int font_ascent, font_descent, font_height;
22837 /* Bounding box of the overall glyphs. */
22838 int leftmost, rightmost, lowest, highest;
22839 int lbearing, rbearing;
22840 int i, width, ascent, descent;
22841 int left_padded = 0, right_padded = 0;
22842 int c;
22843 XChar2b char2b;
22844 struct font_metrics *pcm;
22845 int font_not_found_p;
22846 EMACS_INT pos;
22847
22848 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22849 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22850 break;
22851 if (glyph_len < cmp->glyph_len)
22852 right_padded = 1;
22853 for (i = 0; i < glyph_len; i++)
22854 {
22855 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22856 break;
22857 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22858 }
22859 if (i > 0)
22860 left_padded = 1;
22861
22862 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22863 : IT_CHARPOS (*it));
22864 /* If no suitable font is found, use the default font. */
22865 font_not_found_p = font == NULL;
22866 if (font_not_found_p)
22867 {
22868 face = face->ascii_face;
22869 font = face->font;
22870 }
22871 boff = font->baseline_offset;
22872 if (font->vertical_centering)
22873 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22874 font_ascent = FONT_BASE (font) + boff;
22875 font_descent = FONT_DESCENT (font) - boff;
22876 font_height = FONT_HEIGHT (font);
22877
22878 cmp->font = (void *) font;
22879
22880 pcm = NULL;
22881 if (! font_not_found_p)
22882 {
22883 get_char_face_and_encoding (it->f, c, it->face_id,
22884 &char2b, it->multibyte_p, 0);
22885 pcm = get_per_char_metric (it->f, font, &char2b);
22886 }
22887
22888 /* Initialize the bounding box. */
22889 if (pcm)
22890 {
22891 width = pcm->width;
22892 ascent = pcm->ascent;
22893 descent = pcm->descent;
22894 lbearing = pcm->lbearing;
22895 rbearing = pcm->rbearing;
22896 }
22897 else
22898 {
22899 width = font->space_width;
22900 ascent = FONT_BASE (font);
22901 descent = FONT_DESCENT (font);
22902 lbearing = 0;
22903 rbearing = width;
22904 }
22905
22906 rightmost = width;
22907 leftmost = 0;
22908 lowest = - descent + boff;
22909 highest = ascent + boff;
22910
22911 if (! font_not_found_p
22912 && font->default_ascent
22913 && CHAR_TABLE_P (Vuse_default_ascent)
22914 && !NILP (Faref (Vuse_default_ascent,
22915 make_number (it->char_to_display))))
22916 highest = font->default_ascent + boff;
22917
22918 /* Draw the first glyph at the normal position. It may be
22919 shifted to right later if some other glyphs are drawn
22920 at the left. */
22921 cmp->offsets[i * 2] = 0;
22922 cmp->offsets[i * 2 + 1] = boff;
22923 cmp->lbearing = lbearing;
22924 cmp->rbearing = rbearing;
22925
22926 /* Set cmp->offsets for the remaining glyphs. */
22927 for (i++; i < glyph_len; i++)
22928 {
22929 int left, right, btm, top;
22930 int ch = COMPOSITION_GLYPH (cmp, i);
22931 int face_id;
22932 struct face *this_face;
22933 int this_boff;
22934
22935 if (ch == '\t')
22936 ch = ' ';
22937 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22938 this_face = FACE_FROM_ID (it->f, face_id);
22939 font = this_face->font;
22940
22941 if (font == NULL)
22942 pcm = NULL;
22943 else
22944 {
22945 this_boff = font->baseline_offset;
22946 if (font->vertical_centering)
22947 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22948 get_char_face_and_encoding (it->f, ch, face_id,
22949 &char2b, it->multibyte_p, 0);
22950 pcm = get_per_char_metric (it->f, font, &char2b);
22951 }
22952 if (! pcm)
22953 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22954 else
22955 {
22956 width = pcm->width;
22957 ascent = pcm->ascent;
22958 descent = pcm->descent;
22959 lbearing = pcm->lbearing;
22960 rbearing = pcm->rbearing;
22961 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22962 {
22963 /* Relative composition with or without
22964 alternate chars. */
22965 left = (leftmost + rightmost - width) / 2;
22966 btm = - descent + boff;
22967 if (font->relative_compose
22968 && (! CHAR_TABLE_P (Vignore_relative_composition)
22969 || NILP (Faref (Vignore_relative_composition,
22970 make_number (ch)))))
22971 {
22972
22973 if (- descent >= font->relative_compose)
22974 /* One extra pixel between two glyphs. */
22975 btm = highest + 1;
22976 else if (ascent <= 0)
22977 /* One extra pixel between two glyphs. */
22978 btm = lowest - 1 - ascent - descent;
22979 }
22980 }
22981 else
22982 {
22983 /* A composition rule is specified by an integer
22984 value that encodes global and new reference
22985 points (GREF and NREF). GREF and NREF are
22986 specified by numbers as below:
22987
22988 0---1---2 -- ascent
22989 | |
22990 | |
22991 | |
22992 9--10--11 -- center
22993 | |
22994 ---3---4---5--- baseline
22995 | |
22996 6---7---8 -- descent
22997 */
22998 int rule = COMPOSITION_RULE (cmp, i);
22999 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23000
23001 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23002 grefx = gref % 3, nrefx = nref % 3;
23003 grefy = gref / 3, nrefy = nref / 3;
23004 if (xoff)
23005 xoff = font_height * (xoff - 128) / 256;
23006 if (yoff)
23007 yoff = font_height * (yoff - 128) / 256;
23008
23009 left = (leftmost
23010 + grefx * (rightmost - leftmost) / 2
23011 - nrefx * width / 2
23012 + xoff);
23013
23014 btm = ((grefy == 0 ? highest
23015 : grefy == 1 ? 0
23016 : grefy == 2 ? lowest
23017 : (highest + lowest) / 2)
23018 - (nrefy == 0 ? ascent + descent
23019 : nrefy == 1 ? descent - boff
23020 : nrefy == 2 ? 0
23021 : (ascent + descent) / 2)
23022 + yoff);
23023 }
23024
23025 cmp->offsets[i * 2] = left;
23026 cmp->offsets[i * 2 + 1] = btm + descent;
23027
23028 /* Update the bounding box of the overall glyphs. */
23029 if (width > 0)
23030 {
23031 right = left + width;
23032 if (left < leftmost)
23033 leftmost = left;
23034 if (right > rightmost)
23035 rightmost = right;
23036 }
23037 top = btm + descent + ascent;
23038 if (top > highest)
23039 highest = top;
23040 if (btm < lowest)
23041 lowest = btm;
23042
23043 if (cmp->lbearing > left + lbearing)
23044 cmp->lbearing = left + lbearing;
23045 if (cmp->rbearing < left + rbearing)
23046 cmp->rbearing = left + rbearing;
23047 }
23048 }
23049
23050 /* If there are glyphs whose x-offsets are negative,
23051 shift all glyphs to the right and make all x-offsets
23052 non-negative. */
23053 if (leftmost < 0)
23054 {
23055 for (i = 0; i < cmp->glyph_len; i++)
23056 cmp->offsets[i * 2] -= leftmost;
23057 rightmost -= leftmost;
23058 cmp->lbearing -= leftmost;
23059 cmp->rbearing -= leftmost;
23060 }
23061
23062 if (left_padded && cmp->lbearing < 0)
23063 {
23064 for (i = 0; i < cmp->glyph_len; i++)
23065 cmp->offsets[i * 2] -= cmp->lbearing;
23066 rightmost -= cmp->lbearing;
23067 cmp->rbearing -= cmp->lbearing;
23068 cmp->lbearing = 0;
23069 }
23070 if (right_padded && rightmost < cmp->rbearing)
23071 {
23072 rightmost = cmp->rbearing;
23073 }
23074
23075 cmp->pixel_width = rightmost;
23076 cmp->ascent = highest;
23077 cmp->descent = - lowest;
23078 if (cmp->ascent < font_ascent)
23079 cmp->ascent = font_ascent;
23080 if (cmp->descent < font_descent)
23081 cmp->descent = font_descent;
23082 }
23083
23084 if (it->glyph_row
23085 && (cmp->lbearing < 0
23086 || cmp->rbearing > cmp->pixel_width))
23087 it->glyph_row->contains_overlapping_glyphs_p = 1;
23088
23089 it->pixel_width = cmp->pixel_width;
23090 it->ascent = it->phys_ascent = cmp->ascent;
23091 it->descent = it->phys_descent = cmp->descent;
23092 if (face->box != FACE_NO_BOX)
23093 {
23094 int thick = face->box_line_width;
23095
23096 if (thick > 0)
23097 {
23098 it->ascent += thick;
23099 it->descent += thick;
23100 }
23101 else
23102 thick = - thick;
23103
23104 if (it->start_of_box_run_p)
23105 it->pixel_width += thick;
23106 if (it->end_of_box_run_p)
23107 it->pixel_width += thick;
23108 }
23109
23110 /* If face has an overline, add the height of the overline
23111 (1 pixel) and a 1 pixel margin to the character height. */
23112 if (face->overline_p)
23113 it->ascent += overline_margin;
23114
23115 take_vertical_position_into_account (it);
23116 if (it->ascent < 0)
23117 it->ascent = 0;
23118 if (it->descent < 0)
23119 it->descent = 0;
23120
23121 if (it->glyph_row)
23122 append_composite_glyph (it);
23123 }
23124 else if (it->what == IT_COMPOSITION)
23125 {
23126 /* A dynamic (automatic) composition. */
23127 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23128 Lisp_Object gstring;
23129 struct font_metrics metrics;
23130
23131 gstring = composition_gstring_from_id (it->cmp_it.id);
23132 it->pixel_width
23133 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23134 &metrics);
23135 if (it->glyph_row
23136 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23137 it->glyph_row->contains_overlapping_glyphs_p = 1;
23138 it->ascent = it->phys_ascent = metrics.ascent;
23139 it->descent = it->phys_descent = metrics.descent;
23140 if (face->box != FACE_NO_BOX)
23141 {
23142 int thick = face->box_line_width;
23143
23144 if (thick > 0)
23145 {
23146 it->ascent += thick;
23147 it->descent += thick;
23148 }
23149 else
23150 thick = - thick;
23151
23152 if (it->start_of_box_run_p)
23153 it->pixel_width += thick;
23154 if (it->end_of_box_run_p)
23155 it->pixel_width += thick;
23156 }
23157 /* If face has an overline, add the height of the overline
23158 (1 pixel) and a 1 pixel margin to the character height. */
23159 if (face->overline_p)
23160 it->ascent += overline_margin;
23161 take_vertical_position_into_account (it);
23162 if (it->ascent < 0)
23163 it->ascent = 0;
23164 if (it->descent < 0)
23165 it->descent = 0;
23166
23167 if (it->glyph_row)
23168 append_composite_glyph (it);
23169 }
23170 else if (it->what == IT_GLYPHLESS)
23171 produce_glyphless_glyph (it, 0, Qnil);
23172 else if (it->what == IT_IMAGE)
23173 produce_image_glyph (it);
23174 else if (it->what == IT_STRETCH)
23175 produce_stretch_glyph (it);
23176
23177 done:
23178 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23179 because this isn't true for images with `:ascent 100'. */
23180 xassert (it->ascent >= 0 && it->descent >= 0);
23181 if (it->area == TEXT_AREA)
23182 it->current_x += it->pixel_width;
23183
23184 if (extra_line_spacing > 0)
23185 {
23186 it->descent += extra_line_spacing;
23187 if (extra_line_spacing > it->max_extra_line_spacing)
23188 it->max_extra_line_spacing = extra_line_spacing;
23189 }
23190
23191 it->max_ascent = max (it->max_ascent, it->ascent);
23192 it->max_descent = max (it->max_descent, it->descent);
23193 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23194 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23195 }
23196
23197 /* EXPORT for RIF:
23198 Output LEN glyphs starting at START at the nominal cursor position.
23199 Advance the nominal cursor over the text. The global variable
23200 updated_window contains the window being updated, updated_row is
23201 the glyph row being updated, and updated_area is the area of that
23202 row being updated. */
23203
23204 void
23205 x_write_glyphs (struct glyph *start, int len)
23206 {
23207 int x, hpos;
23208
23209 xassert (updated_window && updated_row);
23210 BLOCK_INPUT;
23211
23212 /* Write glyphs. */
23213
23214 hpos = start - updated_row->glyphs[updated_area];
23215 x = draw_glyphs (updated_window, output_cursor.x,
23216 updated_row, updated_area,
23217 hpos, hpos + len,
23218 DRAW_NORMAL_TEXT, 0);
23219
23220 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23221 if (updated_area == TEXT_AREA
23222 && updated_window->phys_cursor_on_p
23223 && updated_window->phys_cursor.vpos == output_cursor.vpos
23224 && updated_window->phys_cursor.hpos >= hpos
23225 && updated_window->phys_cursor.hpos < hpos + len)
23226 updated_window->phys_cursor_on_p = 0;
23227
23228 UNBLOCK_INPUT;
23229
23230 /* Advance the output cursor. */
23231 output_cursor.hpos += len;
23232 output_cursor.x = x;
23233 }
23234
23235
23236 /* EXPORT for RIF:
23237 Insert LEN glyphs from START at the nominal cursor position. */
23238
23239 void
23240 x_insert_glyphs (struct glyph *start, int len)
23241 {
23242 struct frame *f;
23243 struct window *w;
23244 int line_height, shift_by_width, shifted_region_width;
23245 struct glyph_row *row;
23246 struct glyph *glyph;
23247 int frame_x, frame_y;
23248 EMACS_INT hpos;
23249
23250 xassert (updated_window && updated_row);
23251 BLOCK_INPUT;
23252 w = updated_window;
23253 f = XFRAME (WINDOW_FRAME (w));
23254
23255 /* Get the height of the line we are in. */
23256 row = updated_row;
23257 line_height = row->height;
23258
23259 /* Get the width of the glyphs to insert. */
23260 shift_by_width = 0;
23261 for (glyph = start; glyph < start + len; ++glyph)
23262 shift_by_width += glyph->pixel_width;
23263
23264 /* Get the width of the region to shift right. */
23265 shifted_region_width = (window_box_width (w, updated_area)
23266 - output_cursor.x
23267 - shift_by_width);
23268
23269 /* Shift right. */
23270 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23271 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23272
23273 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23274 line_height, shift_by_width);
23275
23276 /* Write the glyphs. */
23277 hpos = start - row->glyphs[updated_area];
23278 draw_glyphs (w, output_cursor.x, row, updated_area,
23279 hpos, hpos + len,
23280 DRAW_NORMAL_TEXT, 0);
23281
23282 /* Advance the output cursor. */
23283 output_cursor.hpos += len;
23284 output_cursor.x += shift_by_width;
23285 UNBLOCK_INPUT;
23286 }
23287
23288
23289 /* EXPORT for RIF:
23290 Erase the current text line from the nominal cursor position
23291 (inclusive) to pixel column TO_X (exclusive). The idea is that
23292 everything from TO_X onward is already erased.
23293
23294 TO_X is a pixel position relative to updated_area of
23295 updated_window. TO_X == -1 means clear to the end of this area. */
23296
23297 void
23298 x_clear_end_of_line (int to_x)
23299 {
23300 struct frame *f;
23301 struct window *w = updated_window;
23302 int max_x, min_y, max_y;
23303 int from_x, from_y, to_y;
23304
23305 xassert (updated_window && updated_row);
23306 f = XFRAME (w->frame);
23307
23308 if (updated_row->full_width_p)
23309 max_x = WINDOW_TOTAL_WIDTH (w);
23310 else
23311 max_x = window_box_width (w, updated_area);
23312 max_y = window_text_bottom_y (w);
23313
23314 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23315 of window. For TO_X > 0, truncate to end of drawing area. */
23316 if (to_x == 0)
23317 return;
23318 else if (to_x < 0)
23319 to_x = max_x;
23320 else
23321 to_x = min (to_x, max_x);
23322
23323 to_y = min (max_y, output_cursor.y + updated_row->height);
23324
23325 /* Notice if the cursor will be cleared by this operation. */
23326 if (!updated_row->full_width_p)
23327 notice_overwritten_cursor (w, updated_area,
23328 output_cursor.x, -1,
23329 updated_row->y,
23330 MATRIX_ROW_BOTTOM_Y (updated_row));
23331
23332 from_x = output_cursor.x;
23333
23334 /* Translate to frame coordinates. */
23335 if (updated_row->full_width_p)
23336 {
23337 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23338 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23339 }
23340 else
23341 {
23342 int area_left = window_box_left (w, updated_area);
23343 from_x += area_left;
23344 to_x += area_left;
23345 }
23346
23347 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23348 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23349 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23350
23351 /* Prevent inadvertently clearing to end of the X window. */
23352 if (to_x > from_x && to_y > from_y)
23353 {
23354 BLOCK_INPUT;
23355 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23356 to_x - from_x, to_y - from_y);
23357 UNBLOCK_INPUT;
23358 }
23359 }
23360
23361 #endif /* HAVE_WINDOW_SYSTEM */
23362
23363
23364 \f
23365 /***********************************************************************
23366 Cursor types
23367 ***********************************************************************/
23368
23369 /* Value is the internal representation of the specified cursor type
23370 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23371 of the bar cursor. */
23372
23373 static enum text_cursor_kinds
23374 get_specified_cursor_type (Lisp_Object arg, int *width)
23375 {
23376 enum text_cursor_kinds type;
23377
23378 if (NILP (arg))
23379 return NO_CURSOR;
23380
23381 if (EQ (arg, Qbox))
23382 return FILLED_BOX_CURSOR;
23383
23384 if (EQ (arg, Qhollow))
23385 return HOLLOW_BOX_CURSOR;
23386
23387 if (EQ (arg, Qbar))
23388 {
23389 *width = 2;
23390 return BAR_CURSOR;
23391 }
23392
23393 if (CONSP (arg)
23394 && EQ (XCAR (arg), Qbar)
23395 && INTEGERP (XCDR (arg))
23396 && XINT (XCDR (arg)) >= 0)
23397 {
23398 *width = XINT (XCDR (arg));
23399 return BAR_CURSOR;
23400 }
23401
23402 if (EQ (arg, Qhbar))
23403 {
23404 *width = 2;
23405 return HBAR_CURSOR;
23406 }
23407
23408 if (CONSP (arg)
23409 && EQ (XCAR (arg), Qhbar)
23410 && INTEGERP (XCDR (arg))
23411 && XINT (XCDR (arg)) >= 0)
23412 {
23413 *width = XINT (XCDR (arg));
23414 return HBAR_CURSOR;
23415 }
23416
23417 /* Treat anything unknown as "hollow box cursor".
23418 It was bad to signal an error; people have trouble fixing
23419 .Xdefaults with Emacs, when it has something bad in it. */
23420 type = HOLLOW_BOX_CURSOR;
23421
23422 return type;
23423 }
23424
23425 /* Set the default cursor types for specified frame. */
23426 void
23427 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23428 {
23429 int width;
23430 Lisp_Object tem;
23431
23432 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23433 FRAME_CURSOR_WIDTH (f) = width;
23434
23435 /* By default, set up the blink-off state depending on the on-state. */
23436
23437 tem = Fassoc (arg, Vblink_cursor_alist);
23438 if (!NILP (tem))
23439 {
23440 FRAME_BLINK_OFF_CURSOR (f)
23441 = get_specified_cursor_type (XCDR (tem), &width);
23442 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23443 }
23444 else
23445 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23446 }
23447
23448
23449 #ifdef HAVE_WINDOW_SYSTEM
23450
23451 /* Return the cursor we want to be displayed in window W. Return
23452 width of bar/hbar cursor through WIDTH arg. Return with
23453 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23454 (i.e. if the `system caret' should track this cursor).
23455
23456 In a mini-buffer window, we want the cursor only to appear if we
23457 are reading input from this window. For the selected window, we
23458 want the cursor type given by the frame parameter or buffer local
23459 setting of cursor-type. If explicitly marked off, draw no cursor.
23460 In all other cases, we want a hollow box cursor. */
23461
23462 static enum text_cursor_kinds
23463 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23464 int *active_cursor)
23465 {
23466 struct frame *f = XFRAME (w->frame);
23467 struct buffer *b = XBUFFER (w->buffer);
23468 int cursor_type = DEFAULT_CURSOR;
23469 Lisp_Object alt_cursor;
23470 int non_selected = 0;
23471
23472 *active_cursor = 1;
23473
23474 /* Echo area */
23475 if (cursor_in_echo_area
23476 && FRAME_HAS_MINIBUF_P (f)
23477 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23478 {
23479 if (w == XWINDOW (echo_area_window))
23480 {
23481 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23482 {
23483 *width = FRAME_CURSOR_WIDTH (f);
23484 return FRAME_DESIRED_CURSOR (f);
23485 }
23486 else
23487 return get_specified_cursor_type (b->cursor_type, width);
23488 }
23489
23490 *active_cursor = 0;
23491 non_selected = 1;
23492 }
23493
23494 /* Detect a nonselected window or nonselected frame. */
23495 else if (w != XWINDOW (f->selected_window)
23496 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23497 {
23498 *active_cursor = 0;
23499
23500 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23501 return NO_CURSOR;
23502
23503 non_selected = 1;
23504 }
23505
23506 /* Never display a cursor in a window in which cursor-type is nil. */
23507 if (NILP (b->cursor_type))
23508 return NO_CURSOR;
23509
23510 /* Get the normal cursor type for this window. */
23511 if (EQ (b->cursor_type, Qt))
23512 {
23513 cursor_type = FRAME_DESIRED_CURSOR (f);
23514 *width = FRAME_CURSOR_WIDTH (f);
23515 }
23516 else
23517 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23518
23519 /* Use cursor-in-non-selected-windows instead
23520 for non-selected window or frame. */
23521 if (non_selected)
23522 {
23523 alt_cursor = b->cursor_in_non_selected_windows;
23524 if (!EQ (Qt, alt_cursor))
23525 return get_specified_cursor_type (alt_cursor, width);
23526 /* t means modify the normal cursor type. */
23527 if (cursor_type == FILLED_BOX_CURSOR)
23528 cursor_type = HOLLOW_BOX_CURSOR;
23529 else if (cursor_type == BAR_CURSOR && *width > 1)
23530 --*width;
23531 return cursor_type;
23532 }
23533
23534 /* Use normal cursor if not blinked off. */
23535 if (!w->cursor_off_p)
23536 {
23537 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23538 {
23539 if (cursor_type == FILLED_BOX_CURSOR)
23540 {
23541 /* Using a block cursor on large images can be very annoying.
23542 So use a hollow cursor for "large" images.
23543 If image is not transparent (no mask), also use hollow cursor. */
23544 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23545 if (img != NULL && IMAGEP (img->spec))
23546 {
23547 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23548 where N = size of default frame font size.
23549 This should cover most of the "tiny" icons people may use. */
23550 if (!img->mask
23551 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23552 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23553 cursor_type = HOLLOW_BOX_CURSOR;
23554 }
23555 }
23556 else if (cursor_type != NO_CURSOR)
23557 {
23558 /* Display current only supports BOX and HOLLOW cursors for images.
23559 So for now, unconditionally use a HOLLOW cursor when cursor is
23560 not a solid box cursor. */
23561 cursor_type = HOLLOW_BOX_CURSOR;
23562 }
23563 }
23564 return cursor_type;
23565 }
23566
23567 /* Cursor is blinked off, so determine how to "toggle" it. */
23568
23569 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23570 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23571 return get_specified_cursor_type (XCDR (alt_cursor), width);
23572
23573 /* Then see if frame has specified a specific blink off cursor type. */
23574 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23575 {
23576 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23577 return FRAME_BLINK_OFF_CURSOR (f);
23578 }
23579
23580 #if 0
23581 /* Some people liked having a permanently visible blinking cursor,
23582 while others had very strong opinions against it. So it was
23583 decided to remove it. KFS 2003-09-03 */
23584
23585 /* Finally perform built-in cursor blinking:
23586 filled box <-> hollow box
23587 wide [h]bar <-> narrow [h]bar
23588 narrow [h]bar <-> no cursor
23589 other type <-> no cursor */
23590
23591 if (cursor_type == FILLED_BOX_CURSOR)
23592 return HOLLOW_BOX_CURSOR;
23593
23594 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23595 {
23596 *width = 1;
23597 return cursor_type;
23598 }
23599 #endif
23600
23601 return NO_CURSOR;
23602 }
23603
23604
23605 /* Notice when the text cursor of window W has been completely
23606 overwritten by a drawing operation that outputs glyphs in AREA
23607 starting at X0 and ending at X1 in the line starting at Y0 and
23608 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23609 the rest of the line after X0 has been written. Y coordinates
23610 are window-relative. */
23611
23612 static void
23613 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23614 int x0, int x1, int y0, int y1)
23615 {
23616 int cx0, cx1, cy0, cy1;
23617 struct glyph_row *row;
23618
23619 if (!w->phys_cursor_on_p)
23620 return;
23621 if (area != TEXT_AREA)
23622 return;
23623
23624 if (w->phys_cursor.vpos < 0
23625 || w->phys_cursor.vpos >= w->current_matrix->nrows
23626 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23627 !(row->enabled_p && row->displays_text_p)))
23628 return;
23629
23630 if (row->cursor_in_fringe_p)
23631 {
23632 row->cursor_in_fringe_p = 0;
23633 draw_fringe_bitmap (w, row, row->reversed_p);
23634 w->phys_cursor_on_p = 0;
23635 return;
23636 }
23637
23638 cx0 = w->phys_cursor.x;
23639 cx1 = cx0 + w->phys_cursor_width;
23640 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23641 return;
23642
23643 /* The cursor image will be completely removed from the
23644 screen if the output area intersects the cursor area in
23645 y-direction. When we draw in [y0 y1[, and some part of
23646 the cursor is at y < y0, that part must have been drawn
23647 before. When scrolling, the cursor is erased before
23648 actually scrolling, so we don't come here. When not
23649 scrolling, the rows above the old cursor row must have
23650 changed, and in this case these rows must have written
23651 over the cursor image.
23652
23653 Likewise if part of the cursor is below y1, with the
23654 exception of the cursor being in the first blank row at
23655 the buffer and window end because update_text_area
23656 doesn't draw that row. (Except when it does, but
23657 that's handled in update_text_area.) */
23658
23659 cy0 = w->phys_cursor.y;
23660 cy1 = cy0 + w->phys_cursor_height;
23661 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23662 return;
23663
23664 w->phys_cursor_on_p = 0;
23665 }
23666
23667 #endif /* HAVE_WINDOW_SYSTEM */
23668
23669 \f
23670 /************************************************************************
23671 Mouse Face
23672 ************************************************************************/
23673
23674 #ifdef HAVE_WINDOW_SYSTEM
23675
23676 /* EXPORT for RIF:
23677 Fix the display of area AREA of overlapping row ROW in window W
23678 with respect to the overlapping part OVERLAPS. */
23679
23680 void
23681 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23682 enum glyph_row_area area, int overlaps)
23683 {
23684 int i, x;
23685
23686 BLOCK_INPUT;
23687
23688 x = 0;
23689 for (i = 0; i < row->used[area];)
23690 {
23691 if (row->glyphs[area][i].overlaps_vertically_p)
23692 {
23693 int start = i, start_x = x;
23694
23695 do
23696 {
23697 x += row->glyphs[area][i].pixel_width;
23698 ++i;
23699 }
23700 while (i < row->used[area]
23701 && row->glyphs[area][i].overlaps_vertically_p);
23702
23703 draw_glyphs (w, start_x, row, area,
23704 start, i,
23705 DRAW_NORMAL_TEXT, overlaps);
23706 }
23707 else
23708 {
23709 x += row->glyphs[area][i].pixel_width;
23710 ++i;
23711 }
23712 }
23713
23714 UNBLOCK_INPUT;
23715 }
23716
23717
23718 /* EXPORT:
23719 Draw the cursor glyph of window W in glyph row ROW. See the
23720 comment of draw_glyphs for the meaning of HL. */
23721
23722 void
23723 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23724 enum draw_glyphs_face hl)
23725 {
23726 /* If cursor hpos is out of bounds, don't draw garbage. This can
23727 happen in mini-buffer windows when switching between echo area
23728 glyphs and mini-buffer. */
23729 if ((row->reversed_p
23730 ? (w->phys_cursor.hpos >= 0)
23731 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23732 {
23733 int on_p = w->phys_cursor_on_p;
23734 int x1;
23735 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23736 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23737 hl, 0);
23738 w->phys_cursor_on_p = on_p;
23739
23740 if (hl == DRAW_CURSOR)
23741 w->phys_cursor_width = x1 - w->phys_cursor.x;
23742 /* When we erase the cursor, and ROW is overlapped by other
23743 rows, make sure that these overlapping parts of other rows
23744 are redrawn. */
23745 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23746 {
23747 w->phys_cursor_width = x1 - w->phys_cursor.x;
23748
23749 if (row > w->current_matrix->rows
23750 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23751 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23752 OVERLAPS_ERASED_CURSOR);
23753
23754 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23755 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23756 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23757 OVERLAPS_ERASED_CURSOR);
23758 }
23759 }
23760 }
23761
23762
23763 /* EXPORT:
23764 Erase the image of a cursor of window W from the screen. */
23765
23766 void
23767 erase_phys_cursor (struct window *w)
23768 {
23769 struct frame *f = XFRAME (w->frame);
23770 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23771 int hpos = w->phys_cursor.hpos;
23772 int vpos = w->phys_cursor.vpos;
23773 int mouse_face_here_p = 0;
23774 struct glyph_matrix *active_glyphs = w->current_matrix;
23775 struct glyph_row *cursor_row;
23776 struct glyph *cursor_glyph;
23777 enum draw_glyphs_face hl;
23778
23779 /* No cursor displayed or row invalidated => nothing to do on the
23780 screen. */
23781 if (w->phys_cursor_type == NO_CURSOR)
23782 goto mark_cursor_off;
23783
23784 /* VPOS >= active_glyphs->nrows means that window has been resized.
23785 Don't bother to erase the cursor. */
23786 if (vpos >= active_glyphs->nrows)
23787 goto mark_cursor_off;
23788
23789 /* If row containing cursor is marked invalid, there is nothing we
23790 can do. */
23791 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23792 if (!cursor_row->enabled_p)
23793 goto mark_cursor_off;
23794
23795 /* If line spacing is > 0, old cursor may only be partially visible in
23796 window after split-window. So adjust visible height. */
23797 cursor_row->visible_height = min (cursor_row->visible_height,
23798 window_text_bottom_y (w) - cursor_row->y);
23799
23800 /* If row is completely invisible, don't attempt to delete a cursor which
23801 isn't there. This can happen if cursor is at top of a window, and
23802 we switch to a buffer with a header line in that window. */
23803 if (cursor_row->visible_height <= 0)
23804 goto mark_cursor_off;
23805
23806 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23807 if (cursor_row->cursor_in_fringe_p)
23808 {
23809 cursor_row->cursor_in_fringe_p = 0;
23810 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23811 goto mark_cursor_off;
23812 }
23813
23814 /* This can happen when the new row is shorter than the old one.
23815 In this case, either draw_glyphs or clear_end_of_line
23816 should have cleared the cursor. Note that we wouldn't be
23817 able to erase the cursor in this case because we don't have a
23818 cursor glyph at hand. */
23819 if ((cursor_row->reversed_p
23820 ? (w->phys_cursor.hpos < 0)
23821 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23822 goto mark_cursor_off;
23823
23824 /* If the cursor is in the mouse face area, redisplay that when
23825 we clear the cursor. */
23826 if (! NILP (hlinfo->mouse_face_window)
23827 && coords_in_mouse_face_p (w, hpos, vpos)
23828 /* Don't redraw the cursor's spot in mouse face if it is at the
23829 end of a line (on a newline). The cursor appears there, but
23830 mouse highlighting does not. */
23831 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23832 mouse_face_here_p = 1;
23833
23834 /* Maybe clear the display under the cursor. */
23835 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23836 {
23837 int x, y, left_x;
23838 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23839 int width;
23840
23841 cursor_glyph = get_phys_cursor_glyph (w);
23842 if (cursor_glyph == NULL)
23843 goto mark_cursor_off;
23844
23845 width = cursor_glyph->pixel_width;
23846 left_x = window_box_left_offset (w, TEXT_AREA);
23847 x = w->phys_cursor.x;
23848 if (x < left_x)
23849 width -= left_x - x;
23850 width = min (width, window_box_width (w, TEXT_AREA) - x);
23851 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23852 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23853
23854 if (width > 0)
23855 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23856 }
23857
23858 /* Erase the cursor by redrawing the character underneath it. */
23859 if (mouse_face_here_p)
23860 hl = DRAW_MOUSE_FACE;
23861 else
23862 hl = DRAW_NORMAL_TEXT;
23863 draw_phys_cursor_glyph (w, cursor_row, hl);
23864
23865 mark_cursor_off:
23866 w->phys_cursor_on_p = 0;
23867 w->phys_cursor_type = NO_CURSOR;
23868 }
23869
23870
23871 /* EXPORT:
23872 Display or clear cursor of window W. If ON is zero, clear the
23873 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23874 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23875
23876 void
23877 display_and_set_cursor (struct window *w, int on,
23878 int hpos, int vpos, int x, int y)
23879 {
23880 struct frame *f = XFRAME (w->frame);
23881 int new_cursor_type;
23882 int new_cursor_width;
23883 int active_cursor;
23884 struct glyph_row *glyph_row;
23885 struct glyph *glyph;
23886
23887 /* This is pointless on invisible frames, and dangerous on garbaged
23888 windows and frames; in the latter case, the frame or window may
23889 be in the midst of changing its size, and x and y may be off the
23890 window. */
23891 if (! FRAME_VISIBLE_P (f)
23892 || FRAME_GARBAGED_P (f)
23893 || vpos >= w->current_matrix->nrows
23894 || hpos >= w->current_matrix->matrix_w)
23895 return;
23896
23897 /* If cursor is off and we want it off, return quickly. */
23898 if (!on && !w->phys_cursor_on_p)
23899 return;
23900
23901 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23902 /* If cursor row is not enabled, we don't really know where to
23903 display the cursor. */
23904 if (!glyph_row->enabled_p)
23905 {
23906 w->phys_cursor_on_p = 0;
23907 return;
23908 }
23909
23910 glyph = NULL;
23911 if (!glyph_row->exact_window_width_line_p
23912 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23913 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23914
23915 xassert (interrupt_input_blocked);
23916
23917 /* Set new_cursor_type to the cursor we want to be displayed. */
23918 new_cursor_type = get_window_cursor_type (w, glyph,
23919 &new_cursor_width, &active_cursor);
23920
23921 /* If cursor is currently being shown and we don't want it to be or
23922 it is in the wrong place, or the cursor type is not what we want,
23923 erase it. */
23924 if (w->phys_cursor_on_p
23925 && (!on
23926 || w->phys_cursor.x != x
23927 || w->phys_cursor.y != y
23928 || new_cursor_type != w->phys_cursor_type
23929 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23930 && new_cursor_width != w->phys_cursor_width)))
23931 erase_phys_cursor (w);
23932
23933 /* Don't check phys_cursor_on_p here because that flag is only set
23934 to zero in some cases where we know that the cursor has been
23935 completely erased, to avoid the extra work of erasing the cursor
23936 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23937 still not be visible, or it has only been partly erased. */
23938 if (on)
23939 {
23940 w->phys_cursor_ascent = glyph_row->ascent;
23941 w->phys_cursor_height = glyph_row->height;
23942
23943 /* Set phys_cursor_.* before x_draw_.* is called because some
23944 of them may need the information. */
23945 w->phys_cursor.x = x;
23946 w->phys_cursor.y = glyph_row->y;
23947 w->phys_cursor.hpos = hpos;
23948 w->phys_cursor.vpos = vpos;
23949 }
23950
23951 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23952 new_cursor_type, new_cursor_width,
23953 on, active_cursor);
23954 }
23955
23956
23957 /* Switch the display of W's cursor on or off, according to the value
23958 of ON. */
23959
23960 void
23961 update_window_cursor (struct window *w, int on)
23962 {
23963 /* Don't update cursor in windows whose frame is in the process
23964 of being deleted. */
23965 if (w->current_matrix)
23966 {
23967 BLOCK_INPUT;
23968 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23969 w->phys_cursor.x, w->phys_cursor.y);
23970 UNBLOCK_INPUT;
23971 }
23972 }
23973
23974
23975 /* Call update_window_cursor with parameter ON_P on all leaf windows
23976 in the window tree rooted at W. */
23977
23978 static void
23979 update_cursor_in_window_tree (struct window *w, int on_p)
23980 {
23981 while (w)
23982 {
23983 if (!NILP (w->hchild))
23984 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23985 else if (!NILP (w->vchild))
23986 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23987 else
23988 update_window_cursor (w, on_p);
23989
23990 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23991 }
23992 }
23993
23994
23995 /* EXPORT:
23996 Display the cursor on window W, or clear it, according to ON_P.
23997 Don't change the cursor's position. */
23998
23999 void
24000 x_update_cursor (struct frame *f, int on_p)
24001 {
24002 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24003 }
24004
24005
24006 /* EXPORT:
24007 Clear the cursor of window W to background color, and mark the
24008 cursor as not shown. This is used when the text where the cursor
24009 is about to be rewritten. */
24010
24011 void
24012 x_clear_cursor (struct window *w)
24013 {
24014 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24015 update_window_cursor (w, 0);
24016 }
24017
24018 #endif /* HAVE_WINDOW_SYSTEM */
24019
24020 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24021 and MSDOS. */
24022 void
24023 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24024 int start_hpos, int end_hpos,
24025 enum draw_glyphs_face draw)
24026 {
24027 #ifdef HAVE_WINDOW_SYSTEM
24028 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24029 {
24030 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24031 return;
24032 }
24033 #endif
24034 #if defined (HAVE_GPM) || defined (MSDOS)
24035 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24036 #endif
24037 }
24038
24039 /* EXPORT:
24040 Display the active region described by mouse_face_* according to DRAW. */
24041
24042 void
24043 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24044 {
24045 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24046 struct frame *f = XFRAME (WINDOW_FRAME (w));
24047
24048 if (/* If window is in the process of being destroyed, don't bother
24049 to do anything. */
24050 w->current_matrix != NULL
24051 /* Don't update mouse highlight if hidden */
24052 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24053 /* Recognize when we are called to operate on rows that don't exist
24054 anymore. This can happen when a window is split. */
24055 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24056 {
24057 int phys_cursor_on_p = w->phys_cursor_on_p;
24058 struct glyph_row *row, *first, *last;
24059
24060 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24061 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24062
24063 for (row = first; row <= last && row->enabled_p; ++row)
24064 {
24065 int start_hpos, end_hpos, start_x;
24066
24067 /* For all but the first row, the highlight starts at column 0. */
24068 if (row == first)
24069 {
24070 /* R2L rows have BEG and END in reversed order, but the
24071 screen drawing geometry is always left to right. So
24072 we need to mirror the beginning and end of the
24073 highlighted area in R2L rows. */
24074 if (!row->reversed_p)
24075 {
24076 start_hpos = hlinfo->mouse_face_beg_col;
24077 start_x = hlinfo->mouse_face_beg_x;
24078 }
24079 else if (row == last)
24080 {
24081 start_hpos = hlinfo->mouse_face_end_col;
24082 start_x = hlinfo->mouse_face_end_x;
24083 }
24084 else
24085 {
24086 start_hpos = 0;
24087 start_x = 0;
24088 }
24089 }
24090 else if (row->reversed_p && row == last)
24091 {
24092 start_hpos = hlinfo->mouse_face_end_col;
24093 start_x = hlinfo->mouse_face_end_x;
24094 }
24095 else
24096 {
24097 start_hpos = 0;
24098 start_x = 0;
24099 }
24100
24101 if (row == last)
24102 {
24103 if (!row->reversed_p)
24104 end_hpos = hlinfo->mouse_face_end_col;
24105 else if (row == first)
24106 end_hpos = hlinfo->mouse_face_beg_col;
24107 else
24108 {
24109 end_hpos = row->used[TEXT_AREA];
24110 if (draw == DRAW_NORMAL_TEXT)
24111 row->fill_line_p = 1; /* Clear to end of line */
24112 }
24113 }
24114 else if (row->reversed_p && row == first)
24115 end_hpos = hlinfo->mouse_face_beg_col;
24116 else
24117 {
24118 end_hpos = row->used[TEXT_AREA];
24119 if (draw == DRAW_NORMAL_TEXT)
24120 row->fill_line_p = 1; /* Clear to end of line */
24121 }
24122
24123 if (end_hpos > start_hpos)
24124 {
24125 draw_row_with_mouse_face (w, start_x, row,
24126 start_hpos, end_hpos, draw);
24127
24128 row->mouse_face_p
24129 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24130 }
24131 }
24132
24133 #ifdef HAVE_WINDOW_SYSTEM
24134 /* When we've written over the cursor, arrange for it to
24135 be displayed again. */
24136 if (FRAME_WINDOW_P (f)
24137 && phys_cursor_on_p && !w->phys_cursor_on_p)
24138 {
24139 BLOCK_INPUT;
24140 display_and_set_cursor (w, 1,
24141 w->phys_cursor.hpos, w->phys_cursor.vpos,
24142 w->phys_cursor.x, w->phys_cursor.y);
24143 UNBLOCK_INPUT;
24144 }
24145 #endif /* HAVE_WINDOW_SYSTEM */
24146 }
24147
24148 #ifdef HAVE_WINDOW_SYSTEM
24149 /* Change the mouse cursor. */
24150 if (FRAME_WINDOW_P (f))
24151 {
24152 if (draw == DRAW_NORMAL_TEXT
24153 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24154 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24155 else if (draw == DRAW_MOUSE_FACE)
24156 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24157 else
24158 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24159 }
24160 #endif /* HAVE_WINDOW_SYSTEM */
24161 }
24162
24163 /* EXPORT:
24164 Clear out the mouse-highlighted active region.
24165 Redraw it un-highlighted first. Value is non-zero if mouse
24166 face was actually drawn unhighlighted. */
24167
24168 int
24169 clear_mouse_face (Mouse_HLInfo *hlinfo)
24170 {
24171 int cleared = 0;
24172
24173 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24174 {
24175 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24176 cleared = 1;
24177 }
24178
24179 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24180 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24181 hlinfo->mouse_face_window = Qnil;
24182 hlinfo->mouse_face_overlay = Qnil;
24183 return cleared;
24184 }
24185
24186 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24187 within the mouse face on that window. */
24188 static int
24189 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24190 {
24191 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24192
24193 /* Quickly resolve the easy cases. */
24194 if (!(WINDOWP (hlinfo->mouse_face_window)
24195 && XWINDOW (hlinfo->mouse_face_window) == w))
24196 return 0;
24197 if (vpos < hlinfo->mouse_face_beg_row
24198 || vpos > hlinfo->mouse_face_end_row)
24199 return 0;
24200 if (vpos > hlinfo->mouse_face_beg_row
24201 && vpos < hlinfo->mouse_face_end_row)
24202 return 1;
24203
24204 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24205 {
24206 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24207 {
24208 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24209 return 1;
24210 }
24211 else if ((vpos == hlinfo->mouse_face_beg_row
24212 && hpos >= hlinfo->mouse_face_beg_col)
24213 || (vpos == hlinfo->mouse_face_end_row
24214 && hpos < hlinfo->mouse_face_end_col))
24215 return 1;
24216 }
24217 else
24218 {
24219 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24220 {
24221 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24222 return 1;
24223 }
24224 else if ((vpos == hlinfo->mouse_face_beg_row
24225 && hpos <= hlinfo->mouse_face_beg_col)
24226 || (vpos == hlinfo->mouse_face_end_row
24227 && hpos > hlinfo->mouse_face_end_col))
24228 return 1;
24229 }
24230 return 0;
24231 }
24232
24233
24234 /* EXPORT:
24235 Non-zero if physical cursor of window W is within mouse face. */
24236
24237 int
24238 cursor_in_mouse_face_p (struct window *w)
24239 {
24240 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24241 }
24242
24243
24244 \f
24245 /* Find the glyph rows START_ROW and END_ROW of window W that display
24246 characters between buffer positions START_CHARPOS and END_CHARPOS
24247 (excluding END_CHARPOS). This is similar to row_containing_pos,
24248 but is more accurate when bidi reordering makes buffer positions
24249 change non-linearly with glyph rows. */
24250 static void
24251 rows_from_pos_range (struct window *w,
24252 EMACS_INT start_charpos, EMACS_INT end_charpos,
24253 struct glyph_row **start, struct glyph_row **end)
24254 {
24255 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24256 int last_y = window_text_bottom_y (w);
24257 struct glyph_row *row;
24258
24259 *start = NULL;
24260 *end = NULL;
24261
24262 while (!first->enabled_p
24263 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24264 first++;
24265
24266 /* Find the START row. */
24267 for (row = first;
24268 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24269 row++)
24270 {
24271 /* A row can potentially be the START row if the range of the
24272 characters it displays intersects the range
24273 [START_CHARPOS..END_CHARPOS). */
24274 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24275 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24276 /* See the commentary in row_containing_pos, for the
24277 explanation of the complicated way to check whether
24278 some position is beyond the end of the characters
24279 displayed by a row. */
24280 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24281 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24282 && !row->ends_at_zv_p
24283 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24284 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24285 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24286 && !row->ends_at_zv_p
24287 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24288 {
24289 /* Found a candidate row. Now make sure at least one of the
24290 glyphs it displays has a charpos from the range
24291 [START_CHARPOS..END_CHARPOS).
24292
24293 This is not obvious because bidi reordering could make
24294 buffer positions of a row be 1,2,3,102,101,100, and if we
24295 want to highlight characters in [50..60), we don't want
24296 this row, even though [50..60) does intersect [1..103),
24297 the range of character positions given by the row's start
24298 and end positions. */
24299 struct glyph *g = row->glyphs[TEXT_AREA];
24300 struct glyph *e = g + row->used[TEXT_AREA];
24301
24302 while (g < e)
24303 {
24304 if (BUFFERP (g->object)
24305 && start_charpos <= g->charpos && g->charpos < end_charpos)
24306 *start = row;
24307 g++;
24308 }
24309 if (*start)
24310 break;
24311 }
24312 }
24313
24314 /* Find the END row. */
24315 if (!*start
24316 /* If the last row is partially visible, start looking for END
24317 from that row, instead of starting from FIRST. */
24318 && !(row->enabled_p
24319 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24320 row = first;
24321 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24322 {
24323 struct glyph_row *next = row + 1;
24324
24325 if (!next->enabled_p
24326 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24327 /* The first row >= START whose range of displayed characters
24328 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24329 is the row END + 1. */
24330 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24331 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24332 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24333 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24334 && !next->ends_at_zv_p
24335 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24336 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24337 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24338 && !next->ends_at_zv_p
24339 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24340 {
24341 *end = row;
24342 break;
24343 }
24344 else
24345 {
24346 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24347 but none of the characters it displays are in the range, it is
24348 also END + 1. */
24349 struct glyph *g = next->glyphs[TEXT_AREA];
24350 struct glyph *e = g + next->used[TEXT_AREA];
24351
24352 while (g < e)
24353 {
24354 if (BUFFERP (g->object)
24355 && start_charpos <= g->charpos && g->charpos < end_charpos)
24356 break;
24357 g++;
24358 }
24359 if (g == e)
24360 {
24361 *end = row;
24362 break;
24363 }
24364 }
24365 }
24366 }
24367
24368 /* This function sets the mouse_face_* elements of HLINFO, assuming
24369 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24370 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24371 for the overlay or run of text properties specifying the mouse
24372 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24373 before-string and after-string that must also be highlighted.
24374 DISPLAY_STRING, if non-nil, is a display string that may cover some
24375 or all of the highlighted text. */
24376
24377 static void
24378 mouse_face_from_buffer_pos (Lisp_Object window,
24379 Mouse_HLInfo *hlinfo,
24380 EMACS_INT mouse_charpos,
24381 EMACS_INT start_charpos,
24382 EMACS_INT end_charpos,
24383 Lisp_Object before_string,
24384 Lisp_Object after_string,
24385 Lisp_Object display_string)
24386 {
24387 struct window *w = XWINDOW (window);
24388 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24389 struct glyph_row *r1, *r2;
24390 struct glyph *glyph, *end;
24391 EMACS_INT ignore, pos;
24392 int x;
24393
24394 xassert (NILP (display_string) || STRINGP (display_string));
24395 xassert (NILP (before_string) || STRINGP (before_string));
24396 xassert (NILP (after_string) || STRINGP (after_string));
24397
24398 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24399 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24400 if (r1 == NULL)
24401 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24402 /* If the before-string or display-string contains newlines,
24403 rows_from_pos_range skips to its last row. Move back. */
24404 if (!NILP (before_string) || !NILP (display_string))
24405 {
24406 struct glyph_row *prev;
24407 while ((prev = r1 - 1, prev >= first)
24408 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24409 && prev->used[TEXT_AREA] > 0)
24410 {
24411 struct glyph *beg = prev->glyphs[TEXT_AREA];
24412 glyph = beg + prev->used[TEXT_AREA];
24413 while (--glyph >= beg && INTEGERP (glyph->object));
24414 if (glyph < beg
24415 || !(EQ (glyph->object, before_string)
24416 || EQ (glyph->object, display_string)))
24417 break;
24418 r1 = prev;
24419 }
24420 }
24421 if (r2 == NULL)
24422 {
24423 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24424 hlinfo->mouse_face_past_end = 1;
24425 }
24426 else if (!NILP (after_string))
24427 {
24428 /* If the after-string has newlines, advance to its last row. */
24429 struct glyph_row *next;
24430 struct glyph_row *last
24431 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24432
24433 for (next = r2 + 1;
24434 next <= last
24435 && next->used[TEXT_AREA] > 0
24436 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24437 ++next)
24438 r2 = next;
24439 }
24440 /* The rest of the display engine assumes that mouse_face_beg_row is
24441 either above below mouse_face_end_row or identical to it. But
24442 with bidi-reordered continued lines, the row for START_CHARPOS
24443 could be below the row for END_CHARPOS. If so, swap the rows and
24444 store them in correct order. */
24445 if (r1->y > r2->y)
24446 {
24447 struct glyph_row *tem = r2;
24448
24449 r2 = r1;
24450 r1 = tem;
24451 }
24452
24453 hlinfo->mouse_face_beg_y = r1->y;
24454 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24455 hlinfo->mouse_face_end_y = r2->y;
24456 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24457
24458 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24459 AFTER_STRING, DISPLAY_STRING, START_CHARPOS, and END_CHARPOS
24460 could be anywhere in the row and in any order. The strategy
24461 below is to find the leftmost and the rightmost glyph that
24462 belongs to either of these 3 strings, or whose position is
24463 between START_CHARPOS and END_CHARPOS, and highlight all the
24464 glyphs between those two. This may cover more than just the text
24465 between START_CHARPOS and END_CHARPOS if the range of characters
24466 strides the bidi level boundary, e.g. if the beginning is in R2L
24467 text while the end is in L2R text or vice versa. */
24468 if (!r1->reversed_p)
24469 {
24470 /* This row is in a left to right paragraph. Scan it left to
24471 right. */
24472 glyph = r1->glyphs[TEXT_AREA];
24473 end = glyph + r1->used[TEXT_AREA];
24474 x = r1->x;
24475
24476 /* Skip truncation glyphs at the start of the glyph row. */
24477 if (r1->displays_text_p)
24478 for (; glyph < end
24479 && INTEGERP (glyph->object)
24480 && glyph->charpos < 0;
24481 ++glyph)
24482 x += glyph->pixel_width;
24483
24484 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24485 or DISPLAY_STRING, and the first glyph from buffer whose
24486 position is between START_CHARPOS and END_CHARPOS. */
24487 for (; glyph < end
24488 && !INTEGERP (glyph->object)
24489 && !EQ (glyph->object, display_string)
24490 && !(BUFFERP (glyph->object)
24491 && (glyph->charpos >= start_charpos
24492 && glyph->charpos < end_charpos));
24493 ++glyph)
24494 {
24495 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24496 are present at buffer positions between START_CHARPOS and
24497 END_CHARPOS, or if they come from an overlay. */
24498 if (EQ (glyph->object, before_string))
24499 {
24500 pos = string_buffer_position (w, before_string,
24501 start_charpos);
24502 /* If pos == 0, it means before_string came from an
24503 overlay, not from a buffer position. */
24504 if (!pos || (pos >= start_charpos && pos < end_charpos))
24505 break;
24506 }
24507 else if (EQ (glyph->object, after_string))
24508 {
24509 pos = string_buffer_position (w, after_string, end_charpos);
24510 if (!pos || (pos >= start_charpos && pos < end_charpos))
24511 break;
24512 }
24513 x += glyph->pixel_width;
24514 }
24515 hlinfo->mouse_face_beg_x = x;
24516 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24517 }
24518 else
24519 {
24520 /* This row is in a right to left paragraph. Scan it right to
24521 left. */
24522 struct glyph *g;
24523
24524 end = r1->glyphs[TEXT_AREA] - 1;
24525 glyph = end + r1->used[TEXT_AREA];
24526
24527 /* Skip truncation glyphs at the start of the glyph row. */
24528 if (r1->displays_text_p)
24529 for (; glyph > end
24530 && INTEGERP (glyph->object)
24531 && glyph->charpos < 0;
24532 --glyph)
24533 ;
24534
24535 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24536 or DISPLAY_STRING, and the first glyph from buffer whose
24537 position is between START_CHARPOS and END_CHARPOS. */
24538 for (; glyph > end
24539 && !INTEGERP (glyph->object)
24540 && !EQ (glyph->object, display_string)
24541 && !(BUFFERP (glyph->object)
24542 && (glyph->charpos >= start_charpos
24543 && glyph->charpos < end_charpos));
24544 --glyph)
24545 {
24546 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24547 are present at buffer positions between START_CHARPOS and
24548 END_CHARPOS, or if they come from an overlay. */
24549 if (EQ (glyph->object, before_string))
24550 {
24551 pos = string_buffer_position (w, before_string, start_charpos);
24552 /* If pos == 0, it means before_string came from an
24553 overlay, not from a buffer position. */
24554 if (!pos || (pos >= start_charpos && pos < end_charpos))
24555 break;
24556 }
24557 else if (EQ (glyph->object, after_string))
24558 {
24559 pos = string_buffer_position (w, after_string, end_charpos);
24560 if (!pos || (pos >= start_charpos && pos < end_charpos))
24561 break;
24562 }
24563 }
24564
24565 glyph++; /* first glyph to the right of the highlighted area */
24566 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24567 x += g->pixel_width;
24568 hlinfo->mouse_face_beg_x = x;
24569 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24570 }
24571
24572 /* If the highlight ends in a different row, compute GLYPH and END
24573 for the end row. Otherwise, reuse the values computed above for
24574 the row where the highlight begins. */
24575 if (r2 != r1)
24576 {
24577 if (!r2->reversed_p)
24578 {
24579 glyph = r2->glyphs[TEXT_AREA];
24580 end = glyph + r2->used[TEXT_AREA];
24581 x = r2->x;
24582 }
24583 else
24584 {
24585 end = r2->glyphs[TEXT_AREA] - 1;
24586 glyph = end + r2->used[TEXT_AREA];
24587 }
24588 }
24589
24590 if (!r2->reversed_p)
24591 {
24592 /* Skip truncation and continuation glyphs near the end of the
24593 row, and also blanks and stretch glyphs inserted by
24594 extend_face_to_end_of_line. */
24595 while (end > glyph
24596 && INTEGERP ((end - 1)->object)
24597 && (end - 1)->charpos <= 0)
24598 --end;
24599 /* Scan the rest of the glyph row from the end, looking for the
24600 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24601 DISPLAY_STRING, or whose position is between START_CHARPOS
24602 and END_CHARPOS */
24603 for (--end;
24604 end > glyph
24605 && !INTEGERP (end->object)
24606 && !EQ (end->object, display_string)
24607 && !(BUFFERP (end->object)
24608 && (end->charpos >= start_charpos
24609 && end->charpos < end_charpos));
24610 --end)
24611 {
24612 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24613 are present at buffer positions between START_CHARPOS and
24614 END_CHARPOS, or if they come from an overlay. */
24615 if (EQ (end->object, before_string))
24616 {
24617 pos = string_buffer_position (w, before_string, start_charpos);
24618 if (!pos || (pos >= start_charpos && pos < end_charpos))
24619 break;
24620 }
24621 else if (EQ (end->object, after_string))
24622 {
24623 pos = string_buffer_position (w, after_string, end_charpos);
24624 if (!pos || (pos >= start_charpos && pos < end_charpos))
24625 break;
24626 }
24627 }
24628 /* Find the X coordinate of the last glyph to be highlighted. */
24629 for (; glyph <= end; ++glyph)
24630 x += glyph->pixel_width;
24631
24632 hlinfo->mouse_face_end_x = x;
24633 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24634 }
24635 else
24636 {
24637 /* Skip truncation and continuation glyphs near the end of the
24638 row, and also blanks and stretch glyphs inserted by
24639 extend_face_to_end_of_line. */
24640 x = r2->x;
24641 end++;
24642 while (end < glyph
24643 && INTEGERP (end->object)
24644 && end->charpos <= 0)
24645 {
24646 x += end->pixel_width;
24647 ++end;
24648 }
24649 /* Scan the rest of the glyph row from the end, looking for the
24650 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24651 DISPLAY_STRING, or whose position is between START_CHARPOS
24652 and END_CHARPOS */
24653 for ( ;
24654 end < glyph
24655 && !INTEGERP (end->object)
24656 && !EQ (end->object, display_string)
24657 && !(BUFFERP (end->object)
24658 && (end->charpos >= start_charpos
24659 && end->charpos < end_charpos));
24660 ++end)
24661 {
24662 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24663 are present at buffer positions between START_CHARPOS and
24664 END_CHARPOS, or if they come from an overlay. */
24665 if (EQ (end->object, before_string))
24666 {
24667 pos = string_buffer_position (w, before_string, start_charpos);
24668 if (!pos || (pos >= start_charpos && pos < end_charpos))
24669 break;
24670 }
24671 else if (EQ (end->object, after_string))
24672 {
24673 pos = string_buffer_position (w, after_string, end_charpos);
24674 if (!pos || (pos >= start_charpos && pos < end_charpos))
24675 break;
24676 }
24677 x += end->pixel_width;
24678 }
24679 hlinfo->mouse_face_end_x = x;
24680 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24681 }
24682
24683 hlinfo->mouse_face_window = window;
24684 hlinfo->mouse_face_face_id
24685 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24686 mouse_charpos + 1,
24687 !hlinfo->mouse_face_hidden, -1);
24688 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24689 }
24690
24691 /* The following function is not used anymore (replaced with
24692 mouse_face_from_string_pos), but I leave it here for the time
24693 being, in case someone would. */
24694
24695 #if 0 /* not used */
24696
24697 /* Find the position of the glyph for position POS in OBJECT in
24698 window W's current matrix, and return in *X, *Y the pixel
24699 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24700
24701 RIGHT_P non-zero means return the position of the right edge of the
24702 glyph, RIGHT_P zero means return the left edge position.
24703
24704 If no glyph for POS exists in the matrix, return the position of
24705 the glyph with the next smaller position that is in the matrix, if
24706 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24707 exists in the matrix, return the position of the glyph with the
24708 next larger position in OBJECT.
24709
24710 Value is non-zero if a glyph was found. */
24711
24712 static int
24713 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24714 int *hpos, int *vpos, int *x, int *y, int right_p)
24715 {
24716 int yb = window_text_bottom_y (w);
24717 struct glyph_row *r;
24718 struct glyph *best_glyph = NULL;
24719 struct glyph_row *best_row = NULL;
24720 int best_x = 0;
24721
24722 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24723 r->enabled_p && r->y < yb;
24724 ++r)
24725 {
24726 struct glyph *g = r->glyphs[TEXT_AREA];
24727 struct glyph *e = g + r->used[TEXT_AREA];
24728 int gx;
24729
24730 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24731 if (EQ (g->object, object))
24732 {
24733 if (g->charpos == pos)
24734 {
24735 best_glyph = g;
24736 best_x = gx;
24737 best_row = r;
24738 goto found;
24739 }
24740 else if (best_glyph == NULL
24741 || ((eabs (g->charpos - pos)
24742 < eabs (best_glyph->charpos - pos))
24743 && (right_p
24744 ? g->charpos < pos
24745 : g->charpos > pos)))
24746 {
24747 best_glyph = g;
24748 best_x = gx;
24749 best_row = r;
24750 }
24751 }
24752 }
24753
24754 found:
24755
24756 if (best_glyph)
24757 {
24758 *x = best_x;
24759 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24760
24761 if (right_p)
24762 {
24763 *x += best_glyph->pixel_width;
24764 ++*hpos;
24765 }
24766
24767 *y = best_row->y;
24768 *vpos = best_row - w->current_matrix->rows;
24769 }
24770
24771 return best_glyph != NULL;
24772 }
24773 #endif /* not used */
24774
24775 /* Find the positions of the first and the last glyphs in window W's
24776 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24777 (assumed to be a string), and return in HLINFO's mouse_face_*
24778 members the pixel and column/row coordinates of those glyphs. */
24779
24780 static void
24781 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24782 Lisp_Object object,
24783 EMACS_INT startpos, EMACS_INT endpos)
24784 {
24785 int yb = window_text_bottom_y (w);
24786 struct glyph_row *r;
24787 struct glyph *g, *e;
24788 int gx;
24789 int found = 0;
24790
24791 /* Find the glyph row with at least one position in the range
24792 [STARTPOS..ENDPOS], and the first glyph in that row whose
24793 position belongs to that range. */
24794 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24795 r->enabled_p && r->y < yb;
24796 ++r)
24797 {
24798 if (!r->reversed_p)
24799 {
24800 g = r->glyphs[TEXT_AREA];
24801 e = g + r->used[TEXT_AREA];
24802 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24803 if (EQ (g->object, object)
24804 && startpos <= g->charpos && g->charpos <= endpos)
24805 {
24806 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24807 hlinfo->mouse_face_beg_y = r->y;
24808 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24809 hlinfo->mouse_face_beg_x = gx;
24810 found = 1;
24811 break;
24812 }
24813 }
24814 else
24815 {
24816 struct glyph *g1;
24817
24818 e = r->glyphs[TEXT_AREA];
24819 g = e + r->used[TEXT_AREA];
24820 for ( ; g > e; --g)
24821 if (EQ ((g-1)->object, object)
24822 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24823 {
24824 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24825 hlinfo->mouse_face_beg_y = r->y;
24826 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24827 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24828 gx += g1->pixel_width;
24829 hlinfo->mouse_face_beg_x = gx;
24830 found = 1;
24831 break;
24832 }
24833 }
24834 if (found)
24835 break;
24836 }
24837
24838 if (!found)
24839 return;
24840
24841 /* Starting with the next row, look for the first row which does NOT
24842 include any glyphs whose positions are in the range. */
24843 for (++r; r->enabled_p && r->y < yb; ++r)
24844 {
24845 g = r->glyphs[TEXT_AREA];
24846 e = g + r->used[TEXT_AREA];
24847 found = 0;
24848 for ( ; g < e; ++g)
24849 if (EQ (g->object, object)
24850 && startpos <= g->charpos && g->charpos <= endpos)
24851 {
24852 found = 1;
24853 break;
24854 }
24855 if (!found)
24856 break;
24857 }
24858
24859 /* The highlighted region ends on the previous row. */
24860 r--;
24861
24862 /* Set the end row and its vertical pixel coordinate. */
24863 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24864 hlinfo->mouse_face_end_y = r->y;
24865
24866 /* Compute and set the end column and the end column's horizontal
24867 pixel coordinate. */
24868 if (!r->reversed_p)
24869 {
24870 g = r->glyphs[TEXT_AREA];
24871 e = g + r->used[TEXT_AREA];
24872 for ( ; e > g; --e)
24873 if (EQ ((e-1)->object, object)
24874 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24875 break;
24876 hlinfo->mouse_face_end_col = e - g;
24877
24878 for (gx = r->x; g < e; ++g)
24879 gx += g->pixel_width;
24880 hlinfo->mouse_face_end_x = gx;
24881 }
24882 else
24883 {
24884 e = r->glyphs[TEXT_AREA];
24885 g = e + r->used[TEXT_AREA];
24886 for (gx = r->x ; e < g; ++e)
24887 {
24888 if (EQ (e->object, object)
24889 && startpos <= e->charpos && e->charpos <= endpos)
24890 break;
24891 gx += e->pixel_width;
24892 }
24893 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24894 hlinfo->mouse_face_end_x = gx;
24895 }
24896 }
24897
24898 #ifdef HAVE_WINDOW_SYSTEM
24899
24900 /* See if position X, Y is within a hot-spot of an image. */
24901
24902 static int
24903 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24904 {
24905 if (!CONSP (hot_spot))
24906 return 0;
24907
24908 if (EQ (XCAR (hot_spot), Qrect))
24909 {
24910 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24911 Lisp_Object rect = XCDR (hot_spot);
24912 Lisp_Object tem;
24913 if (!CONSP (rect))
24914 return 0;
24915 if (!CONSP (XCAR (rect)))
24916 return 0;
24917 if (!CONSP (XCDR (rect)))
24918 return 0;
24919 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24920 return 0;
24921 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24922 return 0;
24923 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24924 return 0;
24925 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24926 return 0;
24927 return 1;
24928 }
24929 else if (EQ (XCAR (hot_spot), Qcircle))
24930 {
24931 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24932 Lisp_Object circ = XCDR (hot_spot);
24933 Lisp_Object lr, lx0, ly0;
24934 if (CONSP (circ)
24935 && CONSP (XCAR (circ))
24936 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24937 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24938 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24939 {
24940 double r = XFLOATINT (lr);
24941 double dx = XINT (lx0) - x;
24942 double dy = XINT (ly0) - y;
24943 return (dx * dx + dy * dy <= r * r);
24944 }
24945 }
24946 else if (EQ (XCAR (hot_spot), Qpoly))
24947 {
24948 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24949 if (VECTORP (XCDR (hot_spot)))
24950 {
24951 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24952 Lisp_Object *poly = v->contents;
24953 int n = v->size;
24954 int i;
24955 int inside = 0;
24956 Lisp_Object lx, ly;
24957 int x0, y0;
24958
24959 /* Need an even number of coordinates, and at least 3 edges. */
24960 if (n < 6 || n & 1)
24961 return 0;
24962
24963 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24964 If count is odd, we are inside polygon. Pixels on edges
24965 may or may not be included depending on actual geometry of the
24966 polygon. */
24967 if ((lx = poly[n-2], !INTEGERP (lx))
24968 || (ly = poly[n-1], !INTEGERP (lx)))
24969 return 0;
24970 x0 = XINT (lx), y0 = XINT (ly);
24971 for (i = 0; i < n; i += 2)
24972 {
24973 int x1 = x0, y1 = y0;
24974 if ((lx = poly[i], !INTEGERP (lx))
24975 || (ly = poly[i+1], !INTEGERP (ly)))
24976 return 0;
24977 x0 = XINT (lx), y0 = XINT (ly);
24978
24979 /* Does this segment cross the X line? */
24980 if (x0 >= x)
24981 {
24982 if (x1 >= x)
24983 continue;
24984 }
24985 else if (x1 < x)
24986 continue;
24987 if (y > y0 && y > y1)
24988 continue;
24989 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24990 inside = !inside;
24991 }
24992 return inside;
24993 }
24994 }
24995 return 0;
24996 }
24997
24998 Lisp_Object
24999 find_hot_spot (Lisp_Object map, int x, int y)
25000 {
25001 while (CONSP (map))
25002 {
25003 if (CONSP (XCAR (map))
25004 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25005 return XCAR (map);
25006 map = XCDR (map);
25007 }
25008
25009 return Qnil;
25010 }
25011
25012 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25013 3, 3, 0,
25014 doc: /* Lookup in image map MAP coordinates X and Y.
25015 An image map is an alist where each element has the format (AREA ID PLIST).
25016 An AREA is specified as either a rectangle, a circle, or a polygon:
25017 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25018 pixel coordinates of the upper left and bottom right corners.
25019 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25020 and the radius of the circle; r may be a float or integer.
25021 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25022 vector describes one corner in the polygon.
25023 Returns the alist element for the first matching AREA in MAP. */)
25024 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25025 {
25026 if (NILP (map))
25027 return Qnil;
25028
25029 CHECK_NUMBER (x);
25030 CHECK_NUMBER (y);
25031
25032 return find_hot_spot (map, XINT (x), XINT (y));
25033 }
25034
25035
25036 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25037 static void
25038 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25039 {
25040 /* Do not change cursor shape while dragging mouse. */
25041 if (!NILP (do_mouse_tracking))
25042 return;
25043
25044 if (!NILP (pointer))
25045 {
25046 if (EQ (pointer, Qarrow))
25047 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25048 else if (EQ (pointer, Qhand))
25049 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25050 else if (EQ (pointer, Qtext))
25051 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25052 else if (EQ (pointer, intern ("hdrag")))
25053 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25054 #ifdef HAVE_X_WINDOWS
25055 else if (EQ (pointer, intern ("vdrag")))
25056 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25057 #endif
25058 else if (EQ (pointer, intern ("hourglass")))
25059 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25060 else if (EQ (pointer, Qmodeline))
25061 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25062 else
25063 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25064 }
25065
25066 if (cursor != No_Cursor)
25067 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25068 }
25069
25070 #endif /* HAVE_WINDOW_SYSTEM */
25071
25072 /* Take proper action when mouse has moved to the mode or header line
25073 or marginal area AREA of window W, x-position X and y-position Y.
25074 X is relative to the start of the text display area of W, so the
25075 width of bitmap areas and scroll bars must be subtracted to get a
25076 position relative to the start of the mode line. */
25077
25078 static void
25079 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25080 enum window_part area)
25081 {
25082 struct window *w = XWINDOW (window);
25083 struct frame *f = XFRAME (w->frame);
25084 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25085 #ifdef HAVE_WINDOW_SYSTEM
25086 Display_Info *dpyinfo;
25087 #endif
25088 Cursor cursor = No_Cursor;
25089 Lisp_Object pointer = Qnil;
25090 int dx, dy, width, height;
25091 EMACS_INT charpos;
25092 Lisp_Object string, object = Qnil;
25093 Lisp_Object pos, help;
25094
25095 Lisp_Object mouse_face;
25096 int original_x_pixel = x;
25097 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25098 struct glyph_row *row;
25099
25100 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25101 {
25102 int x0;
25103 struct glyph *end;
25104
25105 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25106 returns them in row/column units! */
25107 string = mode_line_string (w, area, &x, &y, &charpos,
25108 &object, &dx, &dy, &width, &height);
25109
25110 row = (area == ON_MODE_LINE
25111 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25112 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25113
25114 /* Find the glyph under the mouse pointer. */
25115 if (row->mode_line_p && row->enabled_p)
25116 {
25117 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25118 end = glyph + row->used[TEXT_AREA];
25119
25120 for (x0 = original_x_pixel;
25121 glyph < end && x0 >= glyph->pixel_width;
25122 ++glyph)
25123 x0 -= glyph->pixel_width;
25124
25125 if (glyph >= end)
25126 glyph = NULL;
25127 }
25128 }
25129 else
25130 {
25131 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25132 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25133 returns them in row/column units! */
25134 string = marginal_area_string (w, area, &x, &y, &charpos,
25135 &object, &dx, &dy, &width, &height);
25136 }
25137
25138 help = Qnil;
25139
25140 #ifdef HAVE_WINDOW_SYSTEM
25141 if (IMAGEP (object))
25142 {
25143 Lisp_Object image_map, hotspot;
25144 if ((image_map = Fplist_get (XCDR (object), QCmap),
25145 !NILP (image_map))
25146 && (hotspot = find_hot_spot (image_map, dx, dy),
25147 CONSP (hotspot))
25148 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25149 {
25150 Lisp_Object area_id, plist;
25151
25152 area_id = XCAR (hotspot);
25153 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25154 If so, we could look for mouse-enter, mouse-leave
25155 properties in PLIST (and do something...). */
25156 hotspot = XCDR (hotspot);
25157 if (CONSP (hotspot)
25158 && (plist = XCAR (hotspot), CONSP (plist)))
25159 {
25160 pointer = Fplist_get (plist, Qpointer);
25161 if (NILP (pointer))
25162 pointer = Qhand;
25163 help = Fplist_get (plist, Qhelp_echo);
25164 if (!NILP (help))
25165 {
25166 help_echo_string = help;
25167 /* Is this correct? ++kfs */
25168 XSETWINDOW (help_echo_window, w);
25169 help_echo_object = w->buffer;
25170 help_echo_pos = charpos;
25171 }
25172 }
25173 }
25174 if (NILP (pointer))
25175 pointer = Fplist_get (XCDR (object), QCpointer);
25176 }
25177 #endif /* HAVE_WINDOW_SYSTEM */
25178
25179 if (STRINGP (string))
25180 {
25181 pos = make_number (charpos);
25182 /* If we're on a string with `help-echo' text property, arrange
25183 for the help to be displayed. This is done by setting the
25184 global variable help_echo_string to the help string. */
25185 if (NILP (help))
25186 {
25187 help = Fget_text_property (pos, Qhelp_echo, string);
25188 if (!NILP (help))
25189 {
25190 help_echo_string = help;
25191 XSETWINDOW (help_echo_window, w);
25192 help_echo_object = string;
25193 help_echo_pos = charpos;
25194 }
25195 }
25196
25197 #ifdef HAVE_WINDOW_SYSTEM
25198 if (FRAME_WINDOW_P (f))
25199 {
25200 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25201 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25202 if (NILP (pointer))
25203 pointer = Fget_text_property (pos, Qpointer, string);
25204
25205 /* Change the mouse pointer according to what is under X/Y. */
25206 if (NILP (pointer)
25207 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25208 {
25209 Lisp_Object map;
25210 map = Fget_text_property (pos, Qlocal_map, string);
25211 if (!KEYMAPP (map))
25212 map = Fget_text_property (pos, Qkeymap, string);
25213 if (!KEYMAPP (map))
25214 cursor = dpyinfo->vertical_scroll_bar_cursor;
25215 }
25216 }
25217 #endif
25218
25219 /* Change the mouse face according to what is under X/Y. */
25220 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25221 if (!NILP (mouse_face)
25222 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25223 && glyph)
25224 {
25225 Lisp_Object b, e;
25226
25227 struct glyph * tmp_glyph;
25228
25229 int gpos;
25230 int gseq_length;
25231 int total_pixel_width;
25232 EMACS_INT begpos, endpos, ignore;
25233
25234 int vpos, hpos;
25235
25236 b = Fprevious_single_property_change (make_number (charpos + 1),
25237 Qmouse_face, string, Qnil);
25238 if (NILP (b))
25239 begpos = 0;
25240 else
25241 begpos = XINT (b);
25242
25243 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25244 if (NILP (e))
25245 endpos = SCHARS (string);
25246 else
25247 endpos = XINT (e);
25248
25249 /* Calculate the glyph position GPOS of GLYPH in the
25250 displayed string, relative to the beginning of the
25251 highlighted part of the string.
25252
25253 Note: GPOS is different from CHARPOS. CHARPOS is the
25254 position of GLYPH in the internal string object. A mode
25255 line string format has structures which are converted to
25256 a flattened string by the Emacs Lisp interpreter. The
25257 internal string is an element of those structures. The
25258 displayed string is the flattened string. */
25259 tmp_glyph = row_start_glyph;
25260 while (tmp_glyph < glyph
25261 && (!(EQ (tmp_glyph->object, glyph->object)
25262 && begpos <= tmp_glyph->charpos
25263 && tmp_glyph->charpos < endpos)))
25264 tmp_glyph++;
25265 gpos = glyph - tmp_glyph;
25266
25267 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25268 the highlighted part of the displayed string to which
25269 GLYPH belongs. Note: GSEQ_LENGTH is different from
25270 SCHARS (STRING), because the latter returns the length of
25271 the internal string. */
25272 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25273 tmp_glyph > glyph
25274 && (!(EQ (tmp_glyph->object, glyph->object)
25275 && begpos <= tmp_glyph->charpos
25276 && tmp_glyph->charpos < endpos));
25277 tmp_glyph--)
25278 ;
25279 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25280
25281 /* Calculate the total pixel width of all the glyphs between
25282 the beginning of the highlighted area and GLYPH. */
25283 total_pixel_width = 0;
25284 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25285 total_pixel_width += tmp_glyph->pixel_width;
25286
25287 /* Pre calculation of re-rendering position. Note: X is in
25288 column units here, after the call to mode_line_string or
25289 marginal_area_string. */
25290 hpos = x - gpos;
25291 vpos = (area == ON_MODE_LINE
25292 ? (w->current_matrix)->nrows - 1
25293 : 0);
25294
25295 /* If GLYPH's position is included in the region that is
25296 already drawn in mouse face, we have nothing to do. */
25297 if ( EQ (window, hlinfo->mouse_face_window)
25298 && (!row->reversed_p
25299 ? (hlinfo->mouse_face_beg_col <= hpos
25300 && hpos < hlinfo->mouse_face_end_col)
25301 /* In R2L rows we swap BEG and END, see below. */
25302 : (hlinfo->mouse_face_end_col <= hpos
25303 && hpos < hlinfo->mouse_face_beg_col))
25304 && hlinfo->mouse_face_beg_row == vpos )
25305 return;
25306
25307 if (clear_mouse_face (hlinfo))
25308 cursor = No_Cursor;
25309
25310 if (!row->reversed_p)
25311 {
25312 hlinfo->mouse_face_beg_col = hpos;
25313 hlinfo->mouse_face_beg_x = original_x_pixel
25314 - (total_pixel_width + dx);
25315 hlinfo->mouse_face_end_col = hpos + gseq_length;
25316 hlinfo->mouse_face_end_x = 0;
25317 }
25318 else
25319 {
25320 /* In R2L rows, show_mouse_face expects BEG and END
25321 coordinates to be swapped. */
25322 hlinfo->mouse_face_end_col = hpos;
25323 hlinfo->mouse_face_end_x = original_x_pixel
25324 - (total_pixel_width + dx);
25325 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25326 hlinfo->mouse_face_beg_x = 0;
25327 }
25328
25329 hlinfo->mouse_face_beg_row = vpos;
25330 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25331 hlinfo->mouse_face_beg_y = 0;
25332 hlinfo->mouse_face_end_y = 0;
25333 hlinfo->mouse_face_past_end = 0;
25334 hlinfo->mouse_face_window = window;
25335
25336 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25337 charpos,
25338 0, 0, 0,
25339 &ignore,
25340 glyph->face_id,
25341 1);
25342 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25343
25344 if (NILP (pointer))
25345 pointer = Qhand;
25346 }
25347 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25348 clear_mouse_face (hlinfo);
25349 }
25350 #ifdef HAVE_WINDOW_SYSTEM
25351 if (FRAME_WINDOW_P (f))
25352 define_frame_cursor1 (f, cursor, pointer);
25353 #endif
25354 }
25355
25356
25357 /* EXPORT:
25358 Take proper action when the mouse has moved to position X, Y on
25359 frame F as regards highlighting characters that have mouse-face
25360 properties. Also de-highlighting chars where the mouse was before.
25361 X and Y can be negative or out of range. */
25362
25363 void
25364 note_mouse_highlight (struct frame *f, int x, int y)
25365 {
25366 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25367 enum window_part part;
25368 Lisp_Object window;
25369 struct window *w;
25370 Cursor cursor = No_Cursor;
25371 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25372 struct buffer *b;
25373
25374 /* When a menu is active, don't highlight because this looks odd. */
25375 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25376 if (popup_activated ())
25377 return;
25378 #endif
25379
25380 if (NILP (Vmouse_highlight)
25381 || !f->glyphs_initialized_p
25382 || f->pointer_invisible)
25383 return;
25384
25385 hlinfo->mouse_face_mouse_x = x;
25386 hlinfo->mouse_face_mouse_y = y;
25387 hlinfo->mouse_face_mouse_frame = f;
25388
25389 if (hlinfo->mouse_face_defer)
25390 return;
25391
25392 if (gc_in_progress)
25393 {
25394 hlinfo->mouse_face_deferred_gc = 1;
25395 return;
25396 }
25397
25398 /* Which window is that in? */
25399 window = window_from_coordinates (f, x, y, &part, 1);
25400
25401 /* If we were displaying active text in another window, clear that.
25402 Also clear if we move out of text area in same window. */
25403 if (! EQ (window, hlinfo->mouse_face_window)
25404 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25405 && !NILP (hlinfo->mouse_face_window)))
25406 clear_mouse_face (hlinfo);
25407
25408 /* Not on a window -> return. */
25409 if (!WINDOWP (window))
25410 return;
25411
25412 /* Reset help_echo_string. It will get recomputed below. */
25413 help_echo_string = Qnil;
25414
25415 /* Convert to window-relative pixel coordinates. */
25416 w = XWINDOW (window);
25417 frame_to_window_pixel_xy (w, &x, &y);
25418
25419 #ifdef HAVE_WINDOW_SYSTEM
25420 /* Handle tool-bar window differently since it doesn't display a
25421 buffer. */
25422 if (EQ (window, f->tool_bar_window))
25423 {
25424 note_tool_bar_highlight (f, x, y);
25425 return;
25426 }
25427 #endif
25428
25429 /* Mouse is on the mode, header line or margin? */
25430 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25431 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25432 {
25433 note_mode_line_or_margin_highlight (window, x, y, part);
25434 return;
25435 }
25436
25437 #ifdef HAVE_WINDOW_SYSTEM
25438 if (part == ON_VERTICAL_BORDER)
25439 {
25440 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25441 help_echo_string = build_string ("drag-mouse-1: resize");
25442 }
25443 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25444 || part == ON_SCROLL_BAR)
25445 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25446 else
25447 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25448 #endif
25449
25450 /* Are we in a window whose display is up to date?
25451 And verify the buffer's text has not changed. */
25452 b = XBUFFER (w->buffer);
25453 if (part == ON_TEXT
25454 && EQ (w->window_end_valid, w->buffer)
25455 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25456 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25457 {
25458 int hpos, vpos, i, dx, dy, area;
25459 EMACS_INT pos;
25460 struct glyph *glyph;
25461 Lisp_Object object;
25462 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
25463 Lisp_Object *overlay_vec = NULL;
25464 int noverlays;
25465 struct buffer *obuf;
25466 EMACS_INT obegv, ozv;
25467 int same_region;
25468
25469 /* Find the glyph under X/Y. */
25470 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25471
25472 #ifdef HAVE_WINDOW_SYSTEM
25473 /* Look for :pointer property on image. */
25474 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25475 {
25476 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25477 if (img != NULL && IMAGEP (img->spec))
25478 {
25479 Lisp_Object image_map, hotspot;
25480 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25481 !NILP (image_map))
25482 && (hotspot = find_hot_spot (image_map,
25483 glyph->slice.img.x + dx,
25484 glyph->slice.img.y + dy),
25485 CONSP (hotspot))
25486 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25487 {
25488 Lisp_Object area_id, plist;
25489
25490 area_id = XCAR (hotspot);
25491 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25492 If so, we could look for mouse-enter, mouse-leave
25493 properties in PLIST (and do something...). */
25494 hotspot = XCDR (hotspot);
25495 if (CONSP (hotspot)
25496 && (plist = XCAR (hotspot), CONSP (plist)))
25497 {
25498 pointer = Fplist_get (plist, Qpointer);
25499 if (NILP (pointer))
25500 pointer = Qhand;
25501 help_echo_string = Fplist_get (plist, Qhelp_echo);
25502 if (!NILP (help_echo_string))
25503 {
25504 help_echo_window = window;
25505 help_echo_object = glyph->object;
25506 help_echo_pos = glyph->charpos;
25507 }
25508 }
25509 }
25510 if (NILP (pointer))
25511 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25512 }
25513 }
25514 #endif /* HAVE_WINDOW_SYSTEM */
25515
25516 /* Clear mouse face if X/Y not over text. */
25517 if (glyph == NULL
25518 || area != TEXT_AREA
25519 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25520 /* Glyph's OBJECT is an integer for glyphs inserted by the
25521 display engine for its internal purposes, like truncation
25522 and continuation glyphs and blanks beyond the end of
25523 line's text on text terminals. If we are over such a
25524 glyph, we are not over any text. */
25525 || INTEGERP (glyph->object)
25526 /* R2L rows have a stretch glyph at their front, which
25527 stands for no text, whereas L2R rows have no glyphs at
25528 all beyond the end of text. Treat such stretch glyphs
25529 like we do with NULL glyphs in L2R rows. */
25530 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25531 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25532 && glyph->type == STRETCH_GLYPH
25533 && glyph->avoid_cursor_p))
25534 {
25535 if (clear_mouse_face (hlinfo))
25536 cursor = No_Cursor;
25537 #ifdef HAVE_WINDOW_SYSTEM
25538 if (FRAME_WINDOW_P (f) && NILP (pointer))
25539 {
25540 if (area != TEXT_AREA)
25541 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25542 else
25543 pointer = Vvoid_text_area_pointer;
25544 }
25545 #endif
25546 goto set_cursor;
25547 }
25548
25549 pos = glyph->charpos;
25550 object = glyph->object;
25551 if (!STRINGP (object) && !BUFFERP (object))
25552 goto set_cursor;
25553
25554 /* If we get an out-of-range value, return now; avoid an error. */
25555 if (BUFFERP (object) && pos > BUF_Z (b))
25556 goto set_cursor;
25557
25558 /* Make the window's buffer temporarily current for
25559 overlays_at and compute_char_face. */
25560 obuf = current_buffer;
25561 current_buffer = b;
25562 obegv = BEGV;
25563 ozv = ZV;
25564 BEGV = BEG;
25565 ZV = Z;
25566
25567 /* Is this char mouse-active or does it have help-echo? */
25568 position = make_number (pos);
25569
25570 if (BUFFERP (object))
25571 {
25572 /* Put all the overlays we want in a vector in overlay_vec. */
25573 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25574 /* Sort overlays into increasing priority order. */
25575 noverlays = sort_overlays (overlay_vec, noverlays, w);
25576 }
25577 else
25578 noverlays = 0;
25579
25580 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25581
25582 if (same_region)
25583 cursor = No_Cursor;
25584
25585 /* Check mouse-face highlighting. */
25586 if (! same_region
25587 /* If there exists an overlay with mouse-face overlapping
25588 the one we are currently highlighting, we have to
25589 check if we enter the overlapping overlay, and then
25590 highlight only that. */
25591 || (OVERLAYP (hlinfo->mouse_face_overlay)
25592 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25593 {
25594 /* Find the highest priority overlay with a mouse-face. */
25595 overlay = Qnil;
25596 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25597 {
25598 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25599 if (!NILP (mouse_face))
25600 overlay = overlay_vec[i];
25601 }
25602
25603 /* If we're highlighting the same overlay as before, there's
25604 no need to do that again. */
25605 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25606 goto check_help_echo;
25607 hlinfo->mouse_face_overlay = overlay;
25608
25609 /* Clear the display of the old active region, if any. */
25610 if (clear_mouse_face (hlinfo))
25611 cursor = No_Cursor;
25612
25613 /* If no overlay applies, get a text property. */
25614 if (NILP (overlay))
25615 mouse_face = Fget_text_property (position, Qmouse_face, object);
25616
25617 /* Next, compute the bounds of the mouse highlighting and
25618 display it. */
25619 if (!NILP (mouse_face) && STRINGP (object))
25620 {
25621 /* The mouse-highlighting comes from a display string
25622 with a mouse-face. */
25623 Lisp_Object b, e;
25624 EMACS_INT ignore;
25625
25626 b = Fprevious_single_property_change
25627 (make_number (pos + 1), Qmouse_face, object, Qnil);
25628 e = Fnext_single_property_change
25629 (position, Qmouse_face, object, Qnil);
25630 if (NILP (b))
25631 b = make_number (0);
25632 if (NILP (e))
25633 e = make_number (SCHARS (object) - 1);
25634 mouse_face_from_string_pos (w, hlinfo, object,
25635 XINT (b), XINT (e));
25636 hlinfo->mouse_face_past_end = 0;
25637 hlinfo->mouse_face_window = window;
25638 hlinfo->mouse_face_face_id
25639 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25640 glyph->face_id, 1);
25641 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25642 cursor = No_Cursor;
25643 }
25644 else
25645 {
25646 /* The mouse-highlighting, if any, comes from an overlay
25647 or text property in the buffer. */
25648 Lisp_Object buffer, display_string;
25649
25650 if (STRINGP (object))
25651 {
25652 /* If we are on a display string with no mouse-face,
25653 check if the text under it has one. */
25654 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25655 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25656 pos = string_buffer_position (w, object, start);
25657 if (pos > 0)
25658 {
25659 mouse_face = get_char_property_and_overlay
25660 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25661 buffer = w->buffer;
25662 display_string = object;
25663 }
25664 }
25665 else
25666 {
25667 buffer = object;
25668 display_string = Qnil;
25669 }
25670
25671 if (!NILP (mouse_face))
25672 {
25673 Lisp_Object before, after;
25674 Lisp_Object before_string, after_string;
25675 /* To correctly find the limits of mouse highlight
25676 in a bidi-reordered buffer, we must not use the
25677 optimization of limiting the search in
25678 previous-single-property-change and
25679 next-single-property-change, because
25680 rows_from_pos_range needs the real start and end
25681 positions to DTRT in this case. That's because
25682 the first row visible in a window does not
25683 necessarily display the character whose position
25684 is the smallest. */
25685 Lisp_Object lim1 =
25686 NILP (XBUFFER (buffer)->bidi_display_reordering)
25687 ? Fmarker_position (w->start)
25688 : Qnil;
25689 Lisp_Object lim2 =
25690 NILP (XBUFFER (buffer)->bidi_display_reordering)
25691 ? make_number (BUF_Z (XBUFFER (buffer))
25692 - XFASTINT (w->window_end_pos))
25693 : Qnil;
25694
25695 if (NILP (overlay))
25696 {
25697 /* Handle the text property case. */
25698 before = Fprevious_single_property_change
25699 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25700 after = Fnext_single_property_change
25701 (make_number (pos), Qmouse_face, buffer, lim2);
25702 before_string = after_string = Qnil;
25703 }
25704 else
25705 {
25706 /* Handle the overlay case. */
25707 before = Foverlay_start (overlay);
25708 after = Foverlay_end (overlay);
25709 before_string = Foverlay_get (overlay, Qbefore_string);
25710 after_string = Foverlay_get (overlay, Qafter_string);
25711
25712 if (!STRINGP (before_string)) before_string = Qnil;
25713 if (!STRINGP (after_string)) after_string = Qnil;
25714 }
25715
25716 mouse_face_from_buffer_pos (window, hlinfo, pos,
25717 XFASTINT (before),
25718 XFASTINT (after),
25719 before_string, after_string,
25720 display_string);
25721 cursor = No_Cursor;
25722 }
25723 }
25724 }
25725
25726 check_help_echo:
25727
25728 /* Look for a `help-echo' property. */
25729 if (NILP (help_echo_string)) {
25730 Lisp_Object help, overlay;
25731
25732 /* Check overlays first. */
25733 help = overlay = Qnil;
25734 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25735 {
25736 overlay = overlay_vec[i];
25737 help = Foverlay_get (overlay, Qhelp_echo);
25738 }
25739
25740 if (!NILP (help))
25741 {
25742 help_echo_string = help;
25743 help_echo_window = window;
25744 help_echo_object = overlay;
25745 help_echo_pos = pos;
25746 }
25747 else
25748 {
25749 Lisp_Object object = glyph->object;
25750 EMACS_INT charpos = glyph->charpos;
25751
25752 /* Try text properties. */
25753 if (STRINGP (object)
25754 && charpos >= 0
25755 && charpos < SCHARS (object))
25756 {
25757 help = Fget_text_property (make_number (charpos),
25758 Qhelp_echo, object);
25759 if (NILP (help))
25760 {
25761 /* If the string itself doesn't specify a help-echo,
25762 see if the buffer text ``under'' it does. */
25763 struct glyph_row *r
25764 = MATRIX_ROW (w->current_matrix, vpos);
25765 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25766 EMACS_INT pos = string_buffer_position (w, object, start);
25767 if (pos > 0)
25768 {
25769 help = Fget_char_property (make_number (pos),
25770 Qhelp_echo, w->buffer);
25771 if (!NILP (help))
25772 {
25773 charpos = pos;
25774 object = w->buffer;
25775 }
25776 }
25777 }
25778 }
25779 else if (BUFFERP (object)
25780 && charpos >= BEGV
25781 && charpos < ZV)
25782 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25783 object);
25784
25785 if (!NILP (help))
25786 {
25787 help_echo_string = help;
25788 help_echo_window = window;
25789 help_echo_object = object;
25790 help_echo_pos = charpos;
25791 }
25792 }
25793 }
25794
25795 #ifdef HAVE_WINDOW_SYSTEM
25796 /* Look for a `pointer' property. */
25797 if (FRAME_WINDOW_P (f) && NILP (pointer))
25798 {
25799 /* Check overlays first. */
25800 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25801 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25802
25803 if (NILP (pointer))
25804 {
25805 Lisp_Object object = glyph->object;
25806 EMACS_INT charpos = glyph->charpos;
25807
25808 /* Try text properties. */
25809 if (STRINGP (object)
25810 && charpos >= 0
25811 && charpos < SCHARS (object))
25812 {
25813 pointer = Fget_text_property (make_number (charpos),
25814 Qpointer, object);
25815 if (NILP (pointer))
25816 {
25817 /* If the string itself doesn't specify a pointer,
25818 see if the buffer text ``under'' it does. */
25819 struct glyph_row *r
25820 = MATRIX_ROW (w->current_matrix, vpos);
25821 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25822 EMACS_INT pos = string_buffer_position (w, object,
25823 start);
25824 if (pos > 0)
25825 pointer = Fget_char_property (make_number (pos),
25826 Qpointer, w->buffer);
25827 }
25828 }
25829 else if (BUFFERP (object)
25830 && charpos >= BEGV
25831 && charpos < ZV)
25832 pointer = Fget_text_property (make_number (charpos),
25833 Qpointer, object);
25834 }
25835 }
25836 #endif /* HAVE_WINDOW_SYSTEM */
25837
25838 BEGV = obegv;
25839 ZV = ozv;
25840 current_buffer = obuf;
25841 }
25842
25843 set_cursor:
25844
25845 #ifdef HAVE_WINDOW_SYSTEM
25846 if (FRAME_WINDOW_P (f))
25847 define_frame_cursor1 (f, cursor, pointer);
25848 #else
25849 /* This is here to prevent a compiler error, about "label at end of
25850 compound statement". */
25851 return;
25852 #endif
25853 }
25854
25855
25856 /* EXPORT for RIF:
25857 Clear any mouse-face on window W. This function is part of the
25858 redisplay interface, and is called from try_window_id and similar
25859 functions to ensure the mouse-highlight is off. */
25860
25861 void
25862 x_clear_window_mouse_face (struct window *w)
25863 {
25864 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25865 Lisp_Object window;
25866
25867 BLOCK_INPUT;
25868 XSETWINDOW (window, w);
25869 if (EQ (window, hlinfo->mouse_face_window))
25870 clear_mouse_face (hlinfo);
25871 UNBLOCK_INPUT;
25872 }
25873
25874
25875 /* EXPORT:
25876 Just discard the mouse face information for frame F, if any.
25877 This is used when the size of F is changed. */
25878
25879 void
25880 cancel_mouse_face (struct frame *f)
25881 {
25882 Lisp_Object window;
25883 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25884
25885 window = hlinfo->mouse_face_window;
25886 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25887 {
25888 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25889 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25890 hlinfo->mouse_face_window = Qnil;
25891 }
25892 }
25893
25894
25895 \f
25896 /***********************************************************************
25897 Exposure Events
25898 ***********************************************************************/
25899
25900 #ifdef HAVE_WINDOW_SYSTEM
25901
25902 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25903 which intersects rectangle R. R is in window-relative coordinates. */
25904
25905 static void
25906 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25907 enum glyph_row_area area)
25908 {
25909 struct glyph *first = row->glyphs[area];
25910 struct glyph *end = row->glyphs[area] + row->used[area];
25911 struct glyph *last;
25912 int first_x, start_x, x;
25913
25914 if (area == TEXT_AREA && row->fill_line_p)
25915 /* If row extends face to end of line write the whole line. */
25916 draw_glyphs (w, 0, row, area,
25917 0, row->used[area],
25918 DRAW_NORMAL_TEXT, 0);
25919 else
25920 {
25921 /* Set START_X to the window-relative start position for drawing glyphs of
25922 AREA. The first glyph of the text area can be partially visible.
25923 The first glyphs of other areas cannot. */
25924 start_x = window_box_left_offset (w, area);
25925 x = start_x;
25926 if (area == TEXT_AREA)
25927 x += row->x;
25928
25929 /* Find the first glyph that must be redrawn. */
25930 while (first < end
25931 && x + first->pixel_width < r->x)
25932 {
25933 x += first->pixel_width;
25934 ++first;
25935 }
25936
25937 /* Find the last one. */
25938 last = first;
25939 first_x = x;
25940 while (last < end
25941 && x < r->x + r->width)
25942 {
25943 x += last->pixel_width;
25944 ++last;
25945 }
25946
25947 /* Repaint. */
25948 if (last > first)
25949 draw_glyphs (w, first_x - start_x, row, area,
25950 first - row->glyphs[area], last - row->glyphs[area],
25951 DRAW_NORMAL_TEXT, 0);
25952 }
25953 }
25954
25955
25956 /* Redraw the parts of the glyph row ROW on window W intersecting
25957 rectangle R. R is in window-relative coordinates. Value is
25958 non-zero if mouse-face was overwritten. */
25959
25960 static int
25961 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25962 {
25963 xassert (row->enabled_p);
25964
25965 if (row->mode_line_p || w->pseudo_window_p)
25966 draw_glyphs (w, 0, row, TEXT_AREA,
25967 0, row->used[TEXT_AREA],
25968 DRAW_NORMAL_TEXT, 0);
25969 else
25970 {
25971 if (row->used[LEFT_MARGIN_AREA])
25972 expose_area (w, row, r, LEFT_MARGIN_AREA);
25973 if (row->used[TEXT_AREA])
25974 expose_area (w, row, r, TEXT_AREA);
25975 if (row->used[RIGHT_MARGIN_AREA])
25976 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25977 draw_row_fringe_bitmaps (w, row);
25978 }
25979
25980 return row->mouse_face_p;
25981 }
25982
25983
25984 /* Redraw those parts of glyphs rows during expose event handling that
25985 overlap other rows. Redrawing of an exposed line writes over parts
25986 of lines overlapping that exposed line; this function fixes that.
25987
25988 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25989 row in W's current matrix that is exposed and overlaps other rows.
25990 LAST_OVERLAPPING_ROW is the last such row. */
25991
25992 static void
25993 expose_overlaps (struct window *w,
25994 struct glyph_row *first_overlapping_row,
25995 struct glyph_row *last_overlapping_row,
25996 XRectangle *r)
25997 {
25998 struct glyph_row *row;
25999
26000 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26001 if (row->overlapping_p)
26002 {
26003 xassert (row->enabled_p && !row->mode_line_p);
26004
26005 row->clip = r;
26006 if (row->used[LEFT_MARGIN_AREA])
26007 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26008
26009 if (row->used[TEXT_AREA])
26010 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26011
26012 if (row->used[RIGHT_MARGIN_AREA])
26013 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26014 row->clip = NULL;
26015 }
26016 }
26017
26018
26019 /* Return non-zero if W's cursor intersects rectangle R. */
26020
26021 static int
26022 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26023 {
26024 XRectangle cr, result;
26025 struct glyph *cursor_glyph;
26026 struct glyph_row *row;
26027
26028 if (w->phys_cursor.vpos >= 0
26029 && w->phys_cursor.vpos < w->current_matrix->nrows
26030 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26031 row->enabled_p)
26032 && row->cursor_in_fringe_p)
26033 {
26034 /* Cursor is in the fringe. */
26035 cr.x = window_box_right_offset (w,
26036 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26037 ? RIGHT_MARGIN_AREA
26038 : TEXT_AREA));
26039 cr.y = row->y;
26040 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26041 cr.height = row->height;
26042 return x_intersect_rectangles (&cr, r, &result);
26043 }
26044
26045 cursor_glyph = get_phys_cursor_glyph (w);
26046 if (cursor_glyph)
26047 {
26048 /* r is relative to W's box, but w->phys_cursor.x is relative
26049 to left edge of W's TEXT area. Adjust it. */
26050 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26051 cr.y = w->phys_cursor.y;
26052 cr.width = cursor_glyph->pixel_width;
26053 cr.height = w->phys_cursor_height;
26054 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26055 I assume the effect is the same -- and this is portable. */
26056 return x_intersect_rectangles (&cr, r, &result);
26057 }
26058 /* If we don't understand the format, pretend we're not in the hot-spot. */
26059 return 0;
26060 }
26061
26062
26063 /* EXPORT:
26064 Draw a vertical window border to the right of window W if W doesn't
26065 have vertical scroll bars. */
26066
26067 void
26068 x_draw_vertical_border (struct window *w)
26069 {
26070 struct frame *f = XFRAME (WINDOW_FRAME (w));
26071
26072 /* We could do better, if we knew what type of scroll-bar the adjacent
26073 windows (on either side) have... But we don't :-(
26074 However, I think this works ok. ++KFS 2003-04-25 */
26075
26076 /* Redraw borders between horizontally adjacent windows. Don't
26077 do it for frames with vertical scroll bars because either the
26078 right scroll bar of a window, or the left scroll bar of its
26079 neighbor will suffice as a border. */
26080 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26081 return;
26082
26083 if (!WINDOW_RIGHTMOST_P (w)
26084 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26085 {
26086 int x0, x1, y0, y1;
26087
26088 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26089 y1 -= 1;
26090
26091 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26092 x1 -= 1;
26093
26094 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26095 }
26096 else if (!WINDOW_LEFTMOST_P (w)
26097 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26098 {
26099 int x0, x1, y0, y1;
26100
26101 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26102 y1 -= 1;
26103
26104 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26105 x0 -= 1;
26106
26107 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26108 }
26109 }
26110
26111
26112 /* Redraw the part of window W intersection rectangle FR. Pixel
26113 coordinates in FR are frame-relative. Call this function with
26114 input blocked. Value is non-zero if the exposure overwrites
26115 mouse-face. */
26116
26117 static int
26118 expose_window (struct window *w, XRectangle *fr)
26119 {
26120 struct frame *f = XFRAME (w->frame);
26121 XRectangle wr, r;
26122 int mouse_face_overwritten_p = 0;
26123
26124 /* If window is not yet fully initialized, do nothing. This can
26125 happen when toolkit scroll bars are used and a window is split.
26126 Reconfiguring the scroll bar will generate an expose for a newly
26127 created window. */
26128 if (w->current_matrix == NULL)
26129 return 0;
26130
26131 /* When we're currently updating the window, display and current
26132 matrix usually don't agree. Arrange for a thorough display
26133 later. */
26134 if (w == updated_window)
26135 {
26136 SET_FRAME_GARBAGED (f);
26137 return 0;
26138 }
26139
26140 /* Frame-relative pixel rectangle of W. */
26141 wr.x = WINDOW_LEFT_EDGE_X (w);
26142 wr.y = WINDOW_TOP_EDGE_Y (w);
26143 wr.width = WINDOW_TOTAL_WIDTH (w);
26144 wr.height = WINDOW_TOTAL_HEIGHT (w);
26145
26146 if (x_intersect_rectangles (fr, &wr, &r))
26147 {
26148 int yb = window_text_bottom_y (w);
26149 struct glyph_row *row;
26150 int cursor_cleared_p;
26151 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26152
26153 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26154 r.x, r.y, r.width, r.height));
26155
26156 /* Convert to window coordinates. */
26157 r.x -= WINDOW_LEFT_EDGE_X (w);
26158 r.y -= WINDOW_TOP_EDGE_Y (w);
26159
26160 /* Turn off the cursor. */
26161 if (!w->pseudo_window_p
26162 && phys_cursor_in_rect_p (w, &r))
26163 {
26164 x_clear_cursor (w);
26165 cursor_cleared_p = 1;
26166 }
26167 else
26168 cursor_cleared_p = 0;
26169
26170 /* Update lines intersecting rectangle R. */
26171 first_overlapping_row = last_overlapping_row = NULL;
26172 for (row = w->current_matrix->rows;
26173 row->enabled_p;
26174 ++row)
26175 {
26176 int y0 = row->y;
26177 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26178
26179 if ((y0 >= r.y && y0 < r.y + r.height)
26180 || (y1 > r.y && y1 < r.y + r.height)
26181 || (r.y >= y0 && r.y < y1)
26182 || (r.y + r.height > y0 && r.y + r.height < y1))
26183 {
26184 /* A header line may be overlapping, but there is no need
26185 to fix overlapping areas for them. KFS 2005-02-12 */
26186 if (row->overlapping_p && !row->mode_line_p)
26187 {
26188 if (first_overlapping_row == NULL)
26189 first_overlapping_row = row;
26190 last_overlapping_row = row;
26191 }
26192
26193 row->clip = fr;
26194 if (expose_line (w, row, &r))
26195 mouse_face_overwritten_p = 1;
26196 row->clip = NULL;
26197 }
26198 else if (row->overlapping_p)
26199 {
26200 /* We must redraw a row overlapping the exposed area. */
26201 if (y0 < r.y
26202 ? y0 + row->phys_height > r.y
26203 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26204 {
26205 if (first_overlapping_row == NULL)
26206 first_overlapping_row = row;
26207 last_overlapping_row = row;
26208 }
26209 }
26210
26211 if (y1 >= yb)
26212 break;
26213 }
26214
26215 /* Display the mode line if there is one. */
26216 if (WINDOW_WANTS_MODELINE_P (w)
26217 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26218 row->enabled_p)
26219 && row->y < r.y + r.height)
26220 {
26221 if (expose_line (w, row, &r))
26222 mouse_face_overwritten_p = 1;
26223 }
26224
26225 if (!w->pseudo_window_p)
26226 {
26227 /* Fix the display of overlapping rows. */
26228 if (first_overlapping_row)
26229 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26230 fr);
26231
26232 /* Draw border between windows. */
26233 x_draw_vertical_border (w);
26234
26235 /* Turn the cursor on again. */
26236 if (cursor_cleared_p)
26237 update_window_cursor (w, 1);
26238 }
26239 }
26240
26241 return mouse_face_overwritten_p;
26242 }
26243
26244
26245
26246 /* Redraw (parts) of all windows in the window tree rooted at W that
26247 intersect R. R contains frame pixel coordinates. Value is
26248 non-zero if the exposure overwrites mouse-face. */
26249
26250 static int
26251 expose_window_tree (struct window *w, XRectangle *r)
26252 {
26253 struct frame *f = XFRAME (w->frame);
26254 int mouse_face_overwritten_p = 0;
26255
26256 while (w && !FRAME_GARBAGED_P (f))
26257 {
26258 if (!NILP (w->hchild))
26259 mouse_face_overwritten_p
26260 |= expose_window_tree (XWINDOW (w->hchild), r);
26261 else if (!NILP (w->vchild))
26262 mouse_face_overwritten_p
26263 |= expose_window_tree (XWINDOW (w->vchild), r);
26264 else
26265 mouse_face_overwritten_p |= expose_window (w, r);
26266
26267 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26268 }
26269
26270 return mouse_face_overwritten_p;
26271 }
26272
26273
26274 /* EXPORT:
26275 Redisplay an exposed area of frame F. X and Y are the upper-left
26276 corner of the exposed rectangle. W and H are width and height of
26277 the exposed area. All are pixel values. W or H zero means redraw
26278 the entire frame. */
26279
26280 void
26281 expose_frame (struct frame *f, int x, int y, int w, int h)
26282 {
26283 XRectangle r;
26284 int mouse_face_overwritten_p = 0;
26285
26286 TRACE ((stderr, "expose_frame "));
26287
26288 /* No need to redraw if frame will be redrawn soon. */
26289 if (FRAME_GARBAGED_P (f))
26290 {
26291 TRACE ((stderr, " garbaged\n"));
26292 return;
26293 }
26294
26295 /* If basic faces haven't been realized yet, there is no point in
26296 trying to redraw anything. This can happen when we get an expose
26297 event while Emacs is starting, e.g. by moving another window. */
26298 if (FRAME_FACE_CACHE (f) == NULL
26299 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26300 {
26301 TRACE ((stderr, " no faces\n"));
26302 return;
26303 }
26304
26305 if (w == 0 || h == 0)
26306 {
26307 r.x = r.y = 0;
26308 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26309 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26310 }
26311 else
26312 {
26313 r.x = x;
26314 r.y = y;
26315 r.width = w;
26316 r.height = h;
26317 }
26318
26319 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26320 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26321
26322 if (WINDOWP (f->tool_bar_window))
26323 mouse_face_overwritten_p
26324 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26325
26326 #ifdef HAVE_X_WINDOWS
26327 #ifndef MSDOS
26328 #ifndef USE_X_TOOLKIT
26329 if (WINDOWP (f->menu_bar_window))
26330 mouse_face_overwritten_p
26331 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26332 #endif /* not USE_X_TOOLKIT */
26333 #endif
26334 #endif
26335
26336 /* Some window managers support a focus-follows-mouse style with
26337 delayed raising of frames. Imagine a partially obscured frame,
26338 and moving the mouse into partially obscured mouse-face on that
26339 frame. The visible part of the mouse-face will be highlighted,
26340 then the WM raises the obscured frame. With at least one WM, KDE
26341 2.1, Emacs is not getting any event for the raising of the frame
26342 (even tried with SubstructureRedirectMask), only Expose events.
26343 These expose events will draw text normally, i.e. not
26344 highlighted. Which means we must redo the highlight here.
26345 Subsume it under ``we love X''. --gerd 2001-08-15 */
26346 /* Included in Windows version because Windows most likely does not
26347 do the right thing if any third party tool offers
26348 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26349 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26350 {
26351 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26352 if (f == hlinfo->mouse_face_mouse_frame)
26353 {
26354 int x = hlinfo->mouse_face_mouse_x;
26355 int y = hlinfo->mouse_face_mouse_y;
26356 clear_mouse_face (hlinfo);
26357 note_mouse_highlight (f, x, y);
26358 }
26359 }
26360 }
26361
26362
26363 /* EXPORT:
26364 Determine the intersection of two rectangles R1 and R2. Return
26365 the intersection in *RESULT. Value is non-zero if RESULT is not
26366 empty. */
26367
26368 int
26369 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26370 {
26371 XRectangle *left, *right;
26372 XRectangle *upper, *lower;
26373 int intersection_p = 0;
26374
26375 /* Rearrange so that R1 is the left-most rectangle. */
26376 if (r1->x < r2->x)
26377 left = r1, right = r2;
26378 else
26379 left = r2, right = r1;
26380
26381 /* X0 of the intersection is right.x0, if this is inside R1,
26382 otherwise there is no intersection. */
26383 if (right->x <= left->x + left->width)
26384 {
26385 result->x = right->x;
26386
26387 /* The right end of the intersection is the minimum of the
26388 the right ends of left and right. */
26389 result->width = (min (left->x + left->width, right->x + right->width)
26390 - result->x);
26391
26392 /* Same game for Y. */
26393 if (r1->y < r2->y)
26394 upper = r1, lower = r2;
26395 else
26396 upper = r2, lower = r1;
26397
26398 /* The upper end of the intersection is lower.y0, if this is inside
26399 of upper. Otherwise, there is no intersection. */
26400 if (lower->y <= upper->y + upper->height)
26401 {
26402 result->y = lower->y;
26403
26404 /* The lower end of the intersection is the minimum of the lower
26405 ends of upper and lower. */
26406 result->height = (min (lower->y + lower->height,
26407 upper->y + upper->height)
26408 - result->y);
26409 intersection_p = 1;
26410 }
26411 }
26412
26413 return intersection_p;
26414 }
26415
26416 #endif /* HAVE_WINDOW_SYSTEM */
26417
26418 \f
26419 /***********************************************************************
26420 Initialization
26421 ***********************************************************************/
26422
26423 void
26424 syms_of_xdisp (void)
26425 {
26426 Vwith_echo_area_save_vector = Qnil;
26427 staticpro (&Vwith_echo_area_save_vector);
26428
26429 Vmessage_stack = Qnil;
26430 staticpro (&Vmessage_stack);
26431
26432 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26433 staticpro (&Qinhibit_redisplay);
26434
26435 message_dolog_marker1 = Fmake_marker ();
26436 staticpro (&message_dolog_marker1);
26437 message_dolog_marker2 = Fmake_marker ();
26438 staticpro (&message_dolog_marker2);
26439 message_dolog_marker3 = Fmake_marker ();
26440 staticpro (&message_dolog_marker3);
26441
26442 #if GLYPH_DEBUG
26443 defsubr (&Sdump_frame_glyph_matrix);
26444 defsubr (&Sdump_glyph_matrix);
26445 defsubr (&Sdump_glyph_row);
26446 defsubr (&Sdump_tool_bar_row);
26447 defsubr (&Strace_redisplay);
26448 defsubr (&Strace_to_stderr);
26449 #endif
26450 #ifdef HAVE_WINDOW_SYSTEM
26451 defsubr (&Stool_bar_lines_needed);
26452 defsubr (&Slookup_image_map);
26453 #endif
26454 defsubr (&Sformat_mode_line);
26455 defsubr (&Sinvisible_p);
26456 defsubr (&Scurrent_bidi_paragraph_direction);
26457
26458 staticpro (&Qmenu_bar_update_hook);
26459 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26460
26461 staticpro (&Qoverriding_terminal_local_map);
26462 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26463
26464 staticpro (&Qoverriding_local_map);
26465 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26466
26467 staticpro (&Qwindow_scroll_functions);
26468 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26469
26470 staticpro (&Qwindow_text_change_functions);
26471 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26472
26473 staticpro (&Qredisplay_end_trigger_functions);
26474 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26475
26476 staticpro (&Qinhibit_point_motion_hooks);
26477 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26478
26479 Qeval = intern_c_string ("eval");
26480 staticpro (&Qeval);
26481
26482 QCdata = intern_c_string (":data");
26483 staticpro (&QCdata);
26484 Qdisplay = intern_c_string ("display");
26485 staticpro (&Qdisplay);
26486 Qspace_width = intern_c_string ("space-width");
26487 staticpro (&Qspace_width);
26488 Qraise = intern_c_string ("raise");
26489 staticpro (&Qraise);
26490 Qslice = intern_c_string ("slice");
26491 staticpro (&Qslice);
26492 Qspace = intern_c_string ("space");
26493 staticpro (&Qspace);
26494 Qmargin = intern_c_string ("margin");
26495 staticpro (&Qmargin);
26496 Qpointer = intern_c_string ("pointer");
26497 staticpro (&Qpointer);
26498 Qleft_margin = intern_c_string ("left-margin");
26499 staticpro (&Qleft_margin);
26500 Qright_margin = intern_c_string ("right-margin");
26501 staticpro (&Qright_margin);
26502 Qcenter = intern_c_string ("center");
26503 staticpro (&Qcenter);
26504 Qline_height = intern_c_string ("line-height");
26505 staticpro (&Qline_height);
26506 QCalign_to = intern_c_string (":align-to");
26507 staticpro (&QCalign_to);
26508 QCrelative_width = intern_c_string (":relative-width");
26509 staticpro (&QCrelative_width);
26510 QCrelative_height = intern_c_string (":relative-height");
26511 staticpro (&QCrelative_height);
26512 QCeval = intern_c_string (":eval");
26513 staticpro (&QCeval);
26514 QCpropertize = intern_c_string (":propertize");
26515 staticpro (&QCpropertize);
26516 QCfile = intern_c_string (":file");
26517 staticpro (&QCfile);
26518 Qfontified = intern_c_string ("fontified");
26519 staticpro (&Qfontified);
26520 Qfontification_functions = intern_c_string ("fontification-functions");
26521 staticpro (&Qfontification_functions);
26522 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26523 staticpro (&Qtrailing_whitespace);
26524 Qescape_glyph = intern_c_string ("escape-glyph");
26525 staticpro (&Qescape_glyph);
26526 Qnobreak_space = intern_c_string ("nobreak-space");
26527 staticpro (&Qnobreak_space);
26528 Qimage = intern_c_string ("image");
26529 staticpro (&Qimage);
26530 Qtext = intern_c_string ("text");
26531 staticpro (&Qtext);
26532 Qboth = intern_c_string ("both");
26533 staticpro (&Qboth);
26534 Qboth_horiz = intern_c_string ("both-horiz");
26535 staticpro (&Qboth_horiz);
26536 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26537 staticpro (&Qtext_image_horiz);
26538 QCmap = intern_c_string (":map");
26539 staticpro (&QCmap);
26540 QCpointer = intern_c_string (":pointer");
26541 staticpro (&QCpointer);
26542 Qrect = intern_c_string ("rect");
26543 staticpro (&Qrect);
26544 Qcircle = intern_c_string ("circle");
26545 staticpro (&Qcircle);
26546 Qpoly = intern_c_string ("poly");
26547 staticpro (&Qpoly);
26548 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26549 staticpro (&Qmessage_truncate_lines);
26550 Qgrow_only = intern_c_string ("grow-only");
26551 staticpro (&Qgrow_only);
26552 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26553 staticpro (&Qinhibit_menubar_update);
26554 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26555 staticpro (&Qinhibit_eval_during_redisplay);
26556 Qposition = intern_c_string ("position");
26557 staticpro (&Qposition);
26558 Qbuffer_position = intern_c_string ("buffer-position");
26559 staticpro (&Qbuffer_position);
26560 Qobject = intern_c_string ("object");
26561 staticpro (&Qobject);
26562 Qbar = intern_c_string ("bar");
26563 staticpro (&Qbar);
26564 Qhbar = intern_c_string ("hbar");
26565 staticpro (&Qhbar);
26566 Qbox = intern_c_string ("box");
26567 staticpro (&Qbox);
26568 Qhollow = intern_c_string ("hollow");
26569 staticpro (&Qhollow);
26570 Qhand = intern_c_string ("hand");
26571 staticpro (&Qhand);
26572 Qarrow = intern_c_string ("arrow");
26573 staticpro (&Qarrow);
26574 Qtext = intern_c_string ("text");
26575 staticpro (&Qtext);
26576 Qrisky_local_variable = intern_c_string ("risky-local-variable");
26577 staticpro (&Qrisky_local_variable);
26578 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26579 staticpro (&Qinhibit_free_realized_faces);
26580
26581 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26582 Fcons (intern_c_string ("void-variable"), Qnil)),
26583 Qnil);
26584 staticpro (&list_of_error);
26585
26586 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26587 staticpro (&Qlast_arrow_position);
26588 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26589 staticpro (&Qlast_arrow_string);
26590
26591 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26592 staticpro (&Qoverlay_arrow_string);
26593 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26594 staticpro (&Qoverlay_arrow_bitmap);
26595
26596 echo_buffer[0] = echo_buffer[1] = Qnil;
26597 staticpro (&echo_buffer[0]);
26598 staticpro (&echo_buffer[1]);
26599
26600 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26601 staticpro (&echo_area_buffer[0]);
26602 staticpro (&echo_area_buffer[1]);
26603
26604 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26605 staticpro (&Vmessages_buffer_name);
26606
26607 mode_line_proptrans_alist = Qnil;
26608 staticpro (&mode_line_proptrans_alist);
26609 mode_line_string_list = Qnil;
26610 staticpro (&mode_line_string_list);
26611 mode_line_string_face = Qnil;
26612 staticpro (&mode_line_string_face);
26613 mode_line_string_face_prop = Qnil;
26614 staticpro (&mode_line_string_face_prop);
26615 Vmode_line_unwind_vector = Qnil;
26616 staticpro (&Vmode_line_unwind_vector);
26617
26618 help_echo_string = Qnil;
26619 staticpro (&help_echo_string);
26620 help_echo_object = Qnil;
26621 staticpro (&help_echo_object);
26622 help_echo_window = Qnil;
26623 staticpro (&help_echo_window);
26624 previous_help_echo_string = Qnil;
26625 staticpro (&previous_help_echo_string);
26626 help_echo_pos = -1;
26627
26628 Qright_to_left = intern_c_string ("right-to-left");
26629 staticpro (&Qright_to_left);
26630 Qleft_to_right = intern_c_string ("left-to-right");
26631 staticpro (&Qleft_to_right);
26632
26633 #ifdef HAVE_WINDOW_SYSTEM
26634 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
26635 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26636 For example, if a block cursor is over a tab, it will be drawn as
26637 wide as that tab on the display. */);
26638 x_stretch_cursor_p = 0;
26639 #endif
26640
26641 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
26642 doc: /* *Non-nil means highlight trailing whitespace.
26643 The face used for trailing whitespace is `trailing-whitespace'. */);
26644 Vshow_trailing_whitespace = Qnil;
26645
26646 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
26647 doc: /* *Control highlighting of nobreak space and soft hyphen.
26648 A value of t means highlight the character itself (for nobreak space,
26649 use face `nobreak-space').
26650 A value of nil means no highlighting.
26651 Other values mean display the escape glyph followed by an ordinary
26652 space or ordinary hyphen. */);
26653 Vnobreak_char_display = Qt;
26654
26655 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
26656 doc: /* *The pointer shape to show in void text areas.
26657 A value of nil means to show the text pointer. Other options are `arrow',
26658 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26659 Vvoid_text_area_pointer = Qarrow;
26660
26661 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
26662 doc: /* Non-nil means don't actually do any redisplay.
26663 This is used for internal purposes. */);
26664 Vinhibit_redisplay = Qnil;
26665
26666 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
26667 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26668 Vglobal_mode_string = Qnil;
26669
26670 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
26671 doc: /* Marker for where to display an arrow on top of the buffer text.
26672 This must be the beginning of a line in order to work.
26673 See also `overlay-arrow-string'. */);
26674 Voverlay_arrow_position = Qnil;
26675
26676 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
26677 doc: /* String to display as an arrow in non-window frames.
26678 See also `overlay-arrow-position'. */);
26679 Voverlay_arrow_string = make_pure_c_string ("=>");
26680
26681 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
26682 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26683 The symbols on this list are examined during redisplay to determine
26684 where to display overlay arrows. */);
26685 Voverlay_arrow_variable_list
26686 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26687
26688 DEFVAR_INT ("scroll-step", &scroll_step,
26689 doc: /* *The number of lines to try scrolling a window by when point moves out.
26690 If that fails to bring point back on frame, point is centered instead.
26691 If this is zero, point is always centered after it moves off frame.
26692 If you want scrolling to always be a line at a time, you should set
26693 `scroll-conservatively' to a large value rather than set this to 1. */);
26694
26695 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
26696 doc: /* *Scroll up to this many lines, to bring point back on screen.
26697 If point moves off-screen, redisplay will scroll by up to
26698 `scroll-conservatively' lines in order to bring point just barely
26699 onto the screen again. If that cannot be done, then redisplay
26700 recenters point as usual.
26701
26702 A value of zero means always recenter point if it moves off screen. */);
26703 scroll_conservatively = 0;
26704
26705 DEFVAR_INT ("scroll-margin", &scroll_margin,
26706 doc: /* *Number of lines of margin at the top and bottom of a window.
26707 Recenter the window whenever point gets within this many lines
26708 of the top or bottom of the window. */);
26709 scroll_margin = 0;
26710
26711 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
26712 doc: /* Pixels per inch value for non-window system displays.
26713 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26714 Vdisplay_pixels_per_inch = make_float (72.0);
26715
26716 #if GLYPH_DEBUG
26717 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
26718 #endif
26719
26720 DEFVAR_LISP ("truncate-partial-width-windows",
26721 &Vtruncate_partial_width_windows,
26722 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26723 For an integer value, truncate lines in each window narrower than the
26724 full frame width, provided the window width is less than that integer;
26725 otherwise, respect the value of `truncate-lines'.
26726
26727 For any other non-nil value, truncate lines in all windows that do
26728 not span the full frame width.
26729
26730 A value of nil means to respect the value of `truncate-lines'.
26731
26732 If `word-wrap' is enabled, you might want to reduce this. */);
26733 Vtruncate_partial_width_windows = make_number (50);
26734
26735 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
26736 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26737 Any other value means to use the appropriate face, `mode-line',
26738 `header-line', or `menu' respectively. */);
26739 mode_line_inverse_video = 1;
26740
26741 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
26742 doc: /* *Maximum buffer size for which line number should be displayed.
26743 If the buffer is bigger than this, the line number does not appear
26744 in the mode line. A value of nil means no limit. */);
26745 Vline_number_display_limit = Qnil;
26746
26747 DEFVAR_INT ("line-number-display-limit-width",
26748 &line_number_display_limit_width,
26749 doc: /* *Maximum line width (in characters) for line number display.
26750 If the average length of the lines near point is bigger than this, then the
26751 line number may be omitted from the mode line. */);
26752 line_number_display_limit_width = 200;
26753
26754 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
26755 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26756 highlight_nonselected_windows = 0;
26757
26758 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
26759 doc: /* Non-nil if more than one frame is visible on this display.
26760 Minibuffer-only frames don't count, but iconified frames do.
26761 This variable is not guaranteed to be accurate except while processing
26762 `frame-title-format' and `icon-title-format'. */);
26763
26764 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
26765 doc: /* Template for displaying the title bar of visible frames.
26766 \(Assuming the window manager supports this feature.)
26767
26768 This variable has the same structure as `mode-line-format', except that
26769 the %c and %l constructs are ignored. It is used only on frames for
26770 which no explicit name has been set \(see `modify-frame-parameters'). */);
26771
26772 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
26773 doc: /* Template for displaying the title bar of an iconified frame.
26774 \(Assuming the window manager supports this feature.)
26775 This variable has the same structure as `mode-line-format' (which see),
26776 and is used only on frames for which no explicit name has been set
26777 \(see `modify-frame-parameters'). */);
26778 Vicon_title_format
26779 = Vframe_title_format
26780 = pure_cons (intern_c_string ("multiple-frames"),
26781 pure_cons (make_pure_c_string ("%b"),
26782 pure_cons (pure_cons (empty_unibyte_string,
26783 pure_cons (intern_c_string ("invocation-name"),
26784 pure_cons (make_pure_c_string ("@"),
26785 pure_cons (intern_c_string ("system-name"),
26786 Qnil)))),
26787 Qnil)));
26788
26789 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
26790 doc: /* Maximum number of lines to keep in the message log buffer.
26791 If nil, disable message logging. If t, log messages but don't truncate
26792 the buffer when it becomes large. */);
26793 Vmessage_log_max = make_number (100);
26794
26795 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
26796 doc: /* Functions called before redisplay, if window sizes have changed.
26797 The value should be a list of functions that take one argument.
26798 Just before redisplay, for each frame, if any of its windows have changed
26799 size since the last redisplay, or have been split or deleted,
26800 all the functions in the list are called, with the frame as argument. */);
26801 Vwindow_size_change_functions = Qnil;
26802
26803 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
26804 doc: /* List of functions to call before redisplaying a window with scrolling.
26805 Each function is called with two arguments, the window and its new
26806 display-start position. Note that these functions are also called by
26807 `set-window-buffer'. Also note that the value of `window-end' is not
26808 valid when these functions are called. */);
26809 Vwindow_scroll_functions = Qnil;
26810
26811 DEFVAR_LISP ("window-text-change-functions",
26812 &Vwindow_text_change_functions,
26813 doc: /* Functions to call in redisplay when text in the window might change. */);
26814 Vwindow_text_change_functions = Qnil;
26815
26816 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
26817 doc: /* Functions called when redisplay of a window reaches the end trigger.
26818 Each function is called with two arguments, the window and the end trigger value.
26819 See `set-window-redisplay-end-trigger'. */);
26820 Vredisplay_end_trigger_functions = Qnil;
26821
26822 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
26823 doc: /* *Non-nil means autoselect window with mouse pointer.
26824 If nil, do not autoselect windows.
26825 A positive number means delay autoselection by that many seconds: a
26826 window is autoselected only after the mouse has remained in that
26827 window for the duration of the delay.
26828 A negative number has a similar effect, but causes windows to be
26829 autoselected only after the mouse has stopped moving. \(Because of
26830 the way Emacs compares mouse events, you will occasionally wait twice
26831 that time before the window gets selected.\)
26832 Any other value means to autoselect window instantaneously when the
26833 mouse pointer enters it.
26834
26835 Autoselection selects the minibuffer only if it is active, and never
26836 unselects the minibuffer if it is active.
26837
26838 When customizing this variable make sure that the actual value of
26839 `focus-follows-mouse' matches the behavior of your window manager. */);
26840 Vmouse_autoselect_window = Qnil;
26841
26842 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
26843 doc: /* *Non-nil means automatically resize tool-bars.
26844 This dynamically changes the tool-bar's height to the minimum height
26845 that is needed to make all tool-bar items visible.
26846 If value is `grow-only', the tool-bar's height is only increased
26847 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26848 Vauto_resize_tool_bars = Qt;
26849
26850 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
26851 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26852 auto_raise_tool_bar_buttons_p = 1;
26853
26854 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
26855 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26856 make_cursor_line_fully_visible_p = 1;
26857
26858 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
26859 doc: /* *Border below tool-bar in pixels.
26860 If an integer, use it as the height of the border.
26861 If it is one of `internal-border-width' or `border-width', use the
26862 value of the corresponding frame parameter.
26863 Otherwise, no border is added below the tool-bar. */);
26864 Vtool_bar_border = Qinternal_border_width;
26865
26866 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
26867 doc: /* *Margin around tool-bar buttons in pixels.
26868 If an integer, use that for both horizontal and vertical margins.
26869 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26870 HORZ specifying the horizontal margin, and VERT specifying the
26871 vertical margin. */);
26872 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26873
26874 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
26875 doc: /* *Relief thickness of tool-bar buttons. */);
26876 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26877
26878 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
26879 doc: /* *Tool bar style to use.
26880 It can be one of
26881 image - show images only
26882 text - show text only
26883 both - show both, text below image
26884 both-horiz - show text to the right of the image
26885 text-image-horiz - show text to the left of the image
26886 any other - use system default or image if no system default. */);
26887 Vtool_bar_style = Qnil;
26888
26889 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
26890 doc: /* *Maximum number of characters a label can have to be shown.
26891 The tool bar style must also show labels for this to have any effect, see
26892 `tool-bar-style'. */);
26893 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26894
26895 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
26896 doc: /* List of functions to call to fontify regions of text.
26897 Each function is called with one argument POS. Functions must
26898 fontify a region starting at POS in the current buffer, and give
26899 fontified regions the property `fontified'. */);
26900 Vfontification_functions = Qnil;
26901 Fmake_variable_buffer_local (Qfontification_functions);
26902
26903 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26904 &unibyte_display_via_language_environment,
26905 doc: /* *Non-nil means display unibyte text according to language environment.
26906 Specifically, this means that raw bytes in the range 160-255 decimal
26907 are displayed by converting them to the equivalent multibyte characters
26908 according to the current language environment. As a result, they are
26909 displayed according to the current fontset.
26910
26911 Note that this variable affects only how these bytes are displayed,
26912 but does not change the fact they are interpreted as raw bytes. */);
26913 unibyte_display_via_language_environment = 0;
26914
26915 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
26916 doc: /* *Maximum height for resizing mini-windows.
26917 If a float, it specifies a fraction of the mini-window frame's height.
26918 If an integer, it specifies a number of lines. */);
26919 Vmax_mini_window_height = make_float (0.25);
26920
26921 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26922 doc: /* *How to resize mini-windows.
26923 A value of nil means don't automatically resize mini-windows.
26924 A value of t means resize them to fit the text displayed in them.
26925 A value of `grow-only', the default, means let mini-windows grow
26926 only, until their display becomes empty, at which point the windows
26927 go back to their normal size. */);
26928 Vresize_mini_windows = Qgrow_only;
26929
26930 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26931 doc: /* Alist specifying how to blink the cursor off.
26932 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26933 `cursor-type' frame-parameter or variable equals ON-STATE,
26934 comparing using `equal', Emacs uses OFF-STATE to specify
26935 how to blink it off. ON-STATE and OFF-STATE are values for
26936 the `cursor-type' frame parameter.
26937
26938 If a frame's ON-STATE has no entry in this list,
26939 the frame's other specifications determine how to blink the cursor off. */);
26940 Vblink_cursor_alist = Qnil;
26941
26942 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26943 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26944 If non-nil, windows are automatically scrolled horizontally to make
26945 point visible. */);
26946 automatic_hscrolling_p = 1;
26947 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26948 staticpro (&Qauto_hscroll_mode);
26949
26950 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26951 doc: /* *How many columns away from the window edge point is allowed to get
26952 before automatic hscrolling will horizontally scroll the window. */);
26953 hscroll_margin = 5;
26954
26955 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26956 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26957 When point is less than `hscroll-margin' columns from the window
26958 edge, automatic hscrolling will scroll the window by the amount of columns
26959 determined by this variable. If its value is a positive integer, scroll that
26960 many columns. If it's a positive floating-point number, it specifies the
26961 fraction of the window's width to scroll. If it's nil or zero, point will be
26962 centered horizontally after the scroll. Any other value, including negative
26963 numbers, are treated as if the value were zero.
26964
26965 Automatic hscrolling always moves point outside the scroll margin, so if
26966 point was more than scroll step columns inside the margin, the window will
26967 scroll more than the value given by the scroll step.
26968
26969 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26970 and `scroll-right' overrides this variable's effect. */);
26971 Vhscroll_step = make_number (0);
26972
26973 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26974 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26975 Bind this around calls to `message' to let it take effect. */);
26976 message_truncate_lines = 0;
26977
26978 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26979 doc: /* Normal hook run to update the menu bar definitions.
26980 Redisplay runs this hook before it redisplays the menu bar.
26981 This is used to update submenus such as Buffers,
26982 whose contents depend on various data. */);
26983 Vmenu_bar_update_hook = Qnil;
26984
26985 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26986 doc: /* Frame for which we are updating a menu.
26987 The enable predicate for a menu binding should check this variable. */);
26988 Vmenu_updating_frame = Qnil;
26989
26990 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26991 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26992 inhibit_menubar_update = 0;
26993
26994 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26995 doc: /* Prefix prepended to all continuation lines at display time.
26996 The value may be a string, an image, or a stretch-glyph; it is
26997 interpreted in the same way as the value of a `display' text property.
26998
26999 This variable is overridden by any `wrap-prefix' text or overlay
27000 property.
27001
27002 To add a prefix to non-continuation lines, use `line-prefix'. */);
27003 Vwrap_prefix = Qnil;
27004 staticpro (&Qwrap_prefix);
27005 Qwrap_prefix = intern_c_string ("wrap-prefix");
27006 Fmake_variable_buffer_local (Qwrap_prefix);
27007
27008 DEFVAR_LISP ("line-prefix", &Vline_prefix,
27009 doc: /* Prefix prepended to all non-continuation lines at display time.
27010 The value may be a string, an image, or a stretch-glyph; it is
27011 interpreted in the same way as the value of a `display' text property.
27012
27013 This variable is overridden by any `line-prefix' text or overlay
27014 property.
27015
27016 To add a prefix to continuation lines, use `wrap-prefix'. */);
27017 Vline_prefix = Qnil;
27018 staticpro (&Qline_prefix);
27019 Qline_prefix = intern_c_string ("line-prefix");
27020 Fmake_variable_buffer_local (Qline_prefix);
27021
27022 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
27023 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27024 inhibit_eval_during_redisplay = 0;
27025
27026 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
27027 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27028 inhibit_free_realized_faces = 0;
27029
27030 #if GLYPH_DEBUG
27031 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
27032 doc: /* Inhibit try_window_id display optimization. */);
27033 inhibit_try_window_id = 0;
27034
27035 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
27036 doc: /* Inhibit try_window_reusing display optimization. */);
27037 inhibit_try_window_reusing = 0;
27038
27039 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
27040 doc: /* Inhibit try_cursor_movement display optimization. */);
27041 inhibit_try_cursor_movement = 0;
27042 #endif /* GLYPH_DEBUG */
27043
27044 DEFVAR_INT ("overline-margin", &overline_margin,
27045 doc: /* *Space between overline and text, in pixels.
27046 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27047 margin to the caracter height. */);
27048 overline_margin = 2;
27049
27050 DEFVAR_INT ("underline-minimum-offset",
27051 &underline_minimum_offset,
27052 doc: /* Minimum distance between baseline and underline.
27053 This can improve legibility of underlined text at small font sizes,
27054 particularly when using variable `x-use-underline-position-properties'
27055 with fonts that specify an UNDERLINE_POSITION relatively close to the
27056 baseline. The default value is 1. */);
27057 underline_minimum_offset = 1;
27058
27059 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
27060 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27061 This feature only works when on a window system that can change
27062 cursor shapes. */);
27063 display_hourglass_p = 1;
27064
27065 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
27066 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27067 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27068
27069 hourglass_atimer = NULL;
27070 hourglass_shown_p = 0;
27071
27072 DEFSYM (Qglyphless_char, "glyphless-char");
27073 DEFSYM (Qhex_code, "hex-code");
27074 DEFSYM (Qempty_box, "empty-box");
27075 DEFSYM (Qthin_space, "thin-space");
27076 DEFSYM (Qzero_width, "zero-width");
27077
27078 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27079 /* Intern this now in case it isn't already done.
27080 Setting this variable twice is harmless.
27081 But don't staticpro it here--that is done in alloc.c. */
27082 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27083 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27084
27085 DEFVAR_LISP ("glyphless-char-display", &Vglyphless_char_display,
27086 doc: /* Char-table to control displaying of glyphless characters.
27087 Each element, if non-nil, is an ASCII acronym string (displayed in a box)
27088 or one of these symbols:
27089 hex-code: display the hexadecimal code of a character in a box
27090 empty-box: display as an empty box
27091 thin-space: display as 1-pixel width space
27092 zero-width: don't display
27093
27094 It has one extra slot to control the display of a character for which
27095 no font is found. The value of the slot is `hex-code' or `empty-box'.
27096 The default is `empty-box'. */);
27097 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27098 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27099 Qempty_box);
27100 }
27101
27102
27103 /* Initialize this module when Emacs starts. */
27104
27105 void
27106 init_xdisp (void)
27107 {
27108 Lisp_Object root_window;
27109 struct window *mini_w;
27110
27111 current_header_line_height = current_mode_line_height = -1;
27112
27113 CHARPOS (this_line_start_pos) = 0;
27114
27115 mini_w = XWINDOW (minibuf_window);
27116 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27117
27118 if (!noninteractive)
27119 {
27120 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27121 int i;
27122
27123 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27124 set_window_height (root_window,
27125 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27126 0);
27127 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27128 set_window_height (minibuf_window, 1, 0);
27129
27130 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27131 mini_w->total_cols = make_number (FRAME_COLS (f));
27132
27133 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27134 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27135 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27136
27137 /* The default ellipsis glyphs `...'. */
27138 for (i = 0; i < 3; ++i)
27139 default_invis_vector[i] = make_number ('.');
27140 }
27141
27142 {
27143 /* Allocate the buffer for frame titles.
27144 Also used for `format-mode-line'. */
27145 int size = 100;
27146 mode_line_noprop_buf = (char *) xmalloc (size);
27147 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27148 mode_line_noprop_ptr = mode_line_noprop_buf;
27149 mode_line_target = MODE_LINE_DISPLAY;
27150 }
27151
27152 help_echo_showing_p = 0;
27153 }
27154
27155 /* Since w32 does not support atimers, it defines its own implementation of
27156 the following three functions in w32fns.c. */
27157 #ifndef WINDOWSNT
27158
27159 /* Platform-independent portion of hourglass implementation. */
27160
27161 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27162 int
27163 hourglass_started (void)
27164 {
27165 return hourglass_shown_p || hourglass_atimer != NULL;
27166 }
27167
27168 /* Cancel a currently active hourglass timer, and start a new one. */
27169 void
27170 start_hourglass (void)
27171 {
27172 #if defined (HAVE_WINDOW_SYSTEM)
27173 EMACS_TIME delay;
27174 int secs, usecs = 0;
27175
27176 cancel_hourglass ();
27177
27178 if (INTEGERP (Vhourglass_delay)
27179 && XINT (Vhourglass_delay) > 0)
27180 secs = XFASTINT (Vhourglass_delay);
27181 else if (FLOATP (Vhourglass_delay)
27182 && XFLOAT_DATA (Vhourglass_delay) > 0)
27183 {
27184 Lisp_Object tem;
27185 tem = Ftruncate (Vhourglass_delay, Qnil);
27186 secs = XFASTINT (tem);
27187 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27188 }
27189 else
27190 secs = DEFAULT_HOURGLASS_DELAY;
27191
27192 EMACS_SET_SECS_USECS (delay, secs, usecs);
27193 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27194 show_hourglass, NULL);
27195 #endif
27196 }
27197
27198
27199 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27200 shown. */
27201 void
27202 cancel_hourglass (void)
27203 {
27204 #if defined (HAVE_WINDOW_SYSTEM)
27205 if (hourglass_atimer)
27206 {
27207 cancel_atimer (hourglass_atimer);
27208 hourglass_atimer = NULL;
27209 }
27210
27211 if (hourglass_shown_p)
27212 hide_hourglass ();
27213 #endif
27214 }
27215 #endif /* ! WINDOWSNT */
27216