]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Force left-to-right paragraph direction in echo area and prog-mode buffers.
[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 if (!NILP (current_buffer->bidi_display_reordering))
9388 current_buffer->bidi_paragraph_direction = Qleft_to_right;
9389
9390 /* Insert new message at BEG. */
9391 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9392
9393 if (STRINGP (string))
9394 {
9395 EMACS_INT nchars;
9396
9397 if (nbytes == 0)
9398 nbytes = SBYTES (string);
9399 nchars = string_byte_to_char (string, nbytes);
9400
9401 /* This function takes care of single/multibyte conversion. We
9402 just have to ensure that the echo area buffer has the right
9403 setting of enable_multibyte_characters. */
9404 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9405 }
9406 else if (s)
9407 {
9408 if (nbytes == 0)
9409 nbytes = strlen (s);
9410
9411 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9412 {
9413 /* Convert from multi-byte to single-byte. */
9414 EMACS_INT i;
9415 int c, n;
9416 unsigned char work[1];
9417
9418 /* Convert a multibyte string to single-byte. */
9419 for (i = 0; i < nbytes; i += n)
9420 {
9421 c = string_char_and_length (s + i, &n);
9422 work[0] = (ASCII_CHAR_P (c)
9423 ? c
9424 : multibyte_char_to_unibyte (c, Qnil));
9425 insert_1_both (work, 1, 1, 1, 0, 0);
9426 }
9427 }
9428 else if (!multibyte_p
9429 && !NILP (current_buffer->enable_multibyte_characters))
9430 {
9431 /* Convert from single-byte to multi-byte. */
9432 EMACS_INT i;
9433 int c, n;
9434 const unsigned char *msg = (const unsigned char *) s;
9435 unsigned char str[MAX_MULTIBYTE_LENGTH];
9436
9437 /* Convert a single-byte string to multibyte. */
9438 for (i = 0; i < nbytes; i++)
9439 {
9440 c = msg[i];
9441 MAKE_CHAR_MULTIBYTE (c);
9442 n = CHAR_STRING (c, str);
9443 insert_1_both (str, 1, n, 1, 0, 0);
9444 }
9445 }
9446 else
9447 insert_1 (s, nbytes, 1, 0, 0);
9448 }
9449
9450 return 0;
9451 }
9452
9453
9454 /* Clear messages. CURRENT_P non-zero means clear the current
9455 message. LAST_DISPLAYED_P non-zero means clear the message
9456 last displayed. */
9457
9458 void
9459 clear_message (int current_p, int last_displayed_p)
9460 {
9461 if (current_p)
9462 {
9463 echo_area_buffer[0] = Qnil;
9464 message_cleared_p = 1;
9465 }
9466
9467 if (last_displayed_p)
9468 echo_area_buffer[1] = Qnil;
9469
9470 message_buf_print = 0;
9471 }
9472
9473 /* Clear garbaged frames.
9474
9475 This function is used where the old redisplay called
9476 redraw_garbaged_frames which in turn called redraw_frame which in
9477 turn called clear_frame. The call to clear_frame was a source of
9478 flickering. I believe a clear_frame is not necessary. It should
9479 suffice in the new redisplay to invalidate all current matrices,
9480 and ensure a complete redisplay of all windows. */
9481
9482 static void
9483 clear_garbaged_frames (void)
9484 {
9485 if (frame_garbaged)
9486 {
9487 Lisp_Object tail, frame;
9488 int changed_count = 0;
9489
9490 FOR_EACH_FRAME (tail, frame)
9491 {
9492 struct frame *f = XFRAME (frame);
9493
9494 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9495 {
9496 if (f->resized_p)
9497 {
9498 Fredraw_frame (frame);
9499 f->force_flush_display_p = 1;
9500 }
9501 clear_current_matrices (f);
9502 changed_count++;
9503 f->garbaged = 0;
9504 f->resized_p = 0;
9505 }
9506 }
9507
9508 frame_garbaged = 0;
9509 if (changed_count)
9510 ++windows_or_buffers_changed;
9511 }
9512 }
9513
9514
9515 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9516 is non-zero update selected_frame. Value is non-zero if the
9517 mini-windows height has been changed. */
9518
9519 static int
9520 echo_area_display (int update_frame_p)
9521 {
9522 Lisp_Object mini_window;
9523 struct window *w;
9524 struct frame *f;
9525 int window_height_changed_p = 0;
9526 struct frame *sf = SELECTED_FRAME ();
9527
9528 mini_window = FRAME_MINIBUF_WINDOW (sf);
9529 w = XWINDOW (mini_window);
9530 f = XFRAME (WINDOW_FRAME (w));
9531
9532 /* Don't display if frame is invisible or not yet initialized. */
9533 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9534 return 0;
9535
9536 #ifdef HAVE_WINDOW_SYSTEM
9537 /* When Emacs starts, selected_frame may be the initial terminal
9538 frame. If we let this through, a message would be displayed on
9539 the terminal. */
9540 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9541 return 0;
9542 #endif /* HAVE_WINDOW_SYSTEM */
9543
9544 /* Redraw garbaged frames. */
9545 if (frame_garbaged)
9546 clear_garbaged_frames ();
9547
9548 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9549 {
9550 echo_area_window = mini_window;
9551 window_height_changed_p = display_echo_area (w);
9552 w->must_be_updated_p = 1;
9553
9554 /* Update the display, unless called from redisplay_internal.
9555 Also don't update the screen during redisplay itself. The
9556 update will happen at the end of redisplay, and an update
9557 here could cause confusion. */
9558 if (update_frame_p && !redisplaying_p)
9559 {
9560 int n = 0;
9561
9562 /* If the display update has been interrupted by pending
9563 input, update mode lines in the frame. Due to the
9564 pending input, it might have been that redisplay hasn't
9565 been called, so that mode lines above the echo area are
9566 garbaged. This looks odd, so we prevent it here. */
9567 if (!display_completed)
9568 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9569
9570 if (window_height_changed_p
9571 /* Don't do this if Emacs is shutting down. Redisplay
9572 needs to run hooks. */
9573 && !NILP (Vrun_hooks))
9574 {
9575 /* Must update other windows. Likewise as in other
9576 cases, don't let this update be interrupted by
9577 pending input. */
9578 int count = SPECPDL_INDEX ();
9579 specbind (Qredisplay_dont_pause, Qt);
9580 windows_or_buffers_changed = 1;
9581 redisplay_internal (0);
9582 unbind_to (count, Qnil);
9583 }
9584 else if (FRAME_WINDOW_P (f) && n == 0)
9585 {
9586 /* Window configuration is the same as before.
9587 Can do with a display update of the echo area,
9588 unless we displayed some mode lines. */
9589 update_single_window (w, 1);
9590 FRAME_RIF (f)->flush_display (f);
9591 }
9592 else
9593 update_frame (f, 1, 1);
9594
9595 /* If cursor is in the echo area, make sure that the next
9596 redisplay displays the minibuffer, so that the cursor will
9597 be replaced with what the minibuffer wants. */
9598 if (cursor_in_echo_area)
9599 ++windows_or_buffers_changed;
9600 }
9601 }
9602 else if (!EQ (mini_window, selected_window))
9603 windows_or_buffers_changed++;
9604
9605 /* Last displayed message is now the current message. */
9606 echo_area_buffer[1] = echo_area_buffer[0];
9607 /* Inform read_char that we're not echoing. */
9608 echo_message_buffer = Qnil;
9609
9610 /* Prevent redisplay optimization in redisplay_internal by resetting
9611 this_line_start_pos. This is done because the mini-buffer now
9612 displays the message instead of its buffer text. */
9613 if (EQ (mini_window, selected_window))
9614 CHARPOS (this_line_start_pos) = 0;
9615
9616 return window_height_changed_p;
9617 }
9618
9619
9620 \f
9621 /***********************************************************************
9622 Mode Lines and Frame Titles
9623 ***********************************************************************/
9624
9625 /* A buffer for constructing non-propertized mode-line strings and
9626 frame titles in it; allocated from the heap in init_xdisp and
9627 resized as needed in store_mode_line_noprop_char. */
9628
9629 static char *mode_line_noprop_buf;
9630
9631 /* The buffer's end, and a current output position in it. */
9632
9633 static char *mode_line_noprop_buf_end;
9634 static char *mode_line_noprop_ptr;
9635
9636 #define MODE_LINE_NOPROP_LEN(start) \
9637 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9638
9639 static enum {
9640 MODE_LINE_DISPLAY = 0,
9641 MODE_LINE_TITLE,
9642 MODE_LINE_NOPROP,
9643 MODE_LINE_STRING
9644 } mode_line_target;
9645
9646 /* Alist that caches the results of :propertize.
9647 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9648 static Lisp_Object mode_line_proptrans_alist;
9649
9650 /* List of strings making up the mode-line. */
9651 static Lisp_Object mode_line_string_list;
9652
9653 /* Base face property when building propertized mode line string. */
9654 static Lisp_Object mode_line_string_face;
9655 static Lisp_Object mode_line_string_face_prop;
9656
9657
9658 /* Unwind data for mode line strings */
9659
9660 static Lisp_Object Vmode_line_unwind_vector;
9661
9662 static Lisp_Object
9663 format_mode_line_unwind_data (struct buffer *obuf,
9664 Lisp_Object owin,
9665 int save_proptrans)
9666 {
9667 Lisp_Object vector, tmp;
9668
9669 /* Reduce consing by keeping one vector in
9670 Vwith_echo_area_save_vector. */
9671 vector = Vmode_line_unwind_vector;
9672 Vmode_line_unwind_vector = Qnil;
9673
9674 if (NILP (vector))
9675 vector = Fmake_vector (make_number (8), Qnil);
9676
9677 ASET (vector, 0, make_number (mode_line_target));
9678 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9679 ASET (vector, 2, mode_line_string_list);
9680 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9681 ASET (vector, 4, mode_line_string_face);
9682 ASET (vector, 5, mode_line_string_face_prop);
9683
9684 if (obuf)
9685 XSETBUFFER (tmp, obuf);
9686 else
9687 tmp = Qnil;
9688 ASET (vector, 6, tmp);
9689 ASET (vector, 7, owin);
9690
9691 return vector;
9692 }
9693
9694 static Lisp_Object
9695 unwind_format_mode_line (Lisp_Object vector)
9696 {
9697 mode_line_target = XINT (AREF (vector, 0));
9698 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9699 mode_line_string_list = AREF (vector, 2);
9700 if (! EQ (AREF (vector, 3), Qt))
9701 mode_line_proptrans_alist = AREF (vector, 3);
9702 mode_line_string_face = AREF (vector, 4);
9703 mode_line_string_face_prop = AREF (vector, 5);
9704
9705 if (!NILP (AREF (vector, 7)))
9706 /* Select window before buffer, since it may change the buffer. */
9707 Fselect_window (AREF (vector, 7), Qt);
9708
9709 if (!NILP (AREF (vector, 6)))
9710 {
9711 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9712 ASET (vector, 6, Qnil);
9713 }
9714
9715 Vmode_line_unwind_vector = vector;
9716 return Qnil;
9717 }
9718
9719
9720 /* Store a single character C for the frame title in mode_line_noprop_buf.
9721 Re-allocate mode_line_noprop_buf if necessary. */
9722
9723 static void
9724 store_mode_line_noprop_char (char c)
9725 {
9726 /* If output position has reached the end of the allocated buffer,
9727 double the buffer's size. */
9728 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9729 {
9730 int len = MODE_LINE_NOPROP_LEN (0);
9731 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9732 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9733 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9734 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9735 }
9736
9737 *mode_line_noprop_ptr++ = c;
9738 }
9739
9740
9741 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9742 mode_line_noprop_ptr. STR is the string to store. Do not copy
9743 characters that yield more columns than PRECISION; PRECISION <= 0
9744 means copy the whole string. Pad with spaces until FIELD_WIDTH
9745 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9746 pad. Called from display_mode_element when it is used to build a
9747 frame title. */
9748
9749 static int
9750 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9751 {
9752 int n = 0;
9753 EMACS_INT dummy, nbytes;
9754
9755 /* Copy at most PRECISION chars from STR. */
9756 nbytes = strlen (str);
9757 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9758 while (nbytes--)
9759 store_mode_line_noprop_char (*str++);
9760
9761 /* Fill up with spaces until FIELD_WIDTH reached. */
9762 while (field_width > 0
9763 && n < field_width)
9764 {
9765 store_mode_line_noprop_char (' ');
9766 ++n;
9767 }
9768
9769 return n;
9770 }
9771
9772 /***********************************************************************
9773 Frame Titles
9774 ***********************************************************************/
9775
9776 #ifdef HAVE_WINDOW_SYSTEM
9777
9778 /* Set the title of FRAME, if it has changed. The title format is
9779 Vicon_title_format if FRAME is iconified, otherwise it is
9780 frame_title_format. */
9781
9782 static void
9783 x_consider_frame_title (Lisp_Object frame)
9784 {
9785 struct frame *f = XFRAME (frame);
9786
9787 if (FRAME_WINDOW_P (f)
9788 || FRAME_MINIBUF_ONLY_P (f)
9789 || f->explicit_name)
9790 {
9791 /* Do we have more than one visible frame on this X display? */
9792 Lisp_Object tail;
9793 Lisp_Object fmt;
9794 int title_start;
9795 char *title;
9796 int len;
9797 struct it it;
9798 int count = SPECPDL_INDEX ();
9799
9800 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9801 {
9802 Lisp_Object other_frame = XCAR (tail);
9803 struct frame *tf = XFRAME (other_frame);
9804
9805 if (tf != f
9806 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9807 && !FRAME_MINIBUF_ONLY_P (tf)
9808 && !EQ (other_frame, tip_frame)
9809 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9810 break;
9811 }
9812
9813 /* Set global variable indicating that multiple frames exist. */
9814 multiple_frames = CONSP (tail);
9815
9816 /* Switch to the buffer of selected window of the frame. Set up
9817 mode_line_target so that display_mode_element will output into
9818 mode_line_noprop_buf; then display the title. */
9819 record_unwind_protect (unwind_format_mode_line,
9820 format_mode_line_unwind_data
9821 (current_buffer, selected_window, 0));
9822
9823 Fselect_window (f->selected_window, Qt);
9824 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9825 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9826
9827 mode_line_target = MODE_LINE_TITLE;
9828 title_start = MODE_LINE_NOPROP_LEN (0);
9829 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9830 NULL, DEFAULT_FACE_ID);
9831 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9832 len = MODE_LINE_NOPROP_LEN (title_start);
9833 title = mode_line_noprop_buf + title_start;
9834 unbind_to (count, Qnil);
9835
9836 /* Set the title only if it's changed. This avoids consing in
9837 the common case where it hasn't. (If it turns out that we've
9838 already wasted too much time by walking through the list with
9839 display_mode_element, then we might need to optimize at a
9840 higher level than this.) */
9841 if (! STRINGP (f->name)
9842 || SBYTES (f->name) != len
9843 || memcmp (title, SDATA (f->name), len) != 0)
9844 x_implicitly_set_name (f, make_string (title, len), Qnil);
9845 }
9846 }
9847
9848 #endif /* not HAVE_WINDOW_SYSTEM */
9849
9850
9851
9852 \f
9853 /***********************************************************************
9854 Menu Bars
9855 ***********************************************************************/
9856
9857
9858 /* Prepare for redisplay by updating menu-bar item lists when
9859 appropriate. This can call eval. */
9860
9861 void
9862 prepare_menu_bars (void)
9863 {
9864 int all_windows;
9865 struct gcpro gcpro1, gcpro2;
9866 struct frame *f;
9867 Lisp_Object tooltip_frame;
9868
9869 #ifdef HAVE_WINDOW_SYSTEM
9870 tooltip_frame = tip_frame;
9871 #else
9872 tooltip_frame = Qnil;
9873 #endif
9874
9875 /* Update all frame titles based on their buffer names, etc. We do
9876 this before the menu bars so that the buffer-menu will show the
9877 up-to-date frame titles. */
9878 #ifdef HAVE_WINDOW_SYSTEM
9879 if (windows_or_buffers_changed || update_mode_lines)
9880 {
9881 Lisp_Object tail, frame;
9882
9883 FOR_EACH_FRAME (tail, frame)
9884 {
9885 f = XFRAME (frame);
9886 if (!EQ (frame, tooltip_frame)
9887 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9888 x_consider_frame_title (frame);
9889 }
9890 }
9891 #endif /* HAVE_WINDOW_SYSTEM */
9892
9893 /* Update the menu bar item lists, if appropriate. This has to be
9894 done before any actual redisplay or generation of display lines. */
9895 all_windows = (update_mode_lines
9896 || buffer_shared > 1
9897 || windows_or_buffers_changed);
9898 if (all_windows)
9899 {
9900 Lisp_Object tail, frame;
9901 int count = SPECPDL_INDEX ();
9902 /* 1 means that update_menu_bar has run its hooks
9903 so any further calls to update_menu_bar shouldn't do so again. */
9904 int menu_bar_hooks_run = 0;
9905
9906 record_unwind_save_match_data ();
9907
9908 FOR_EACH_FRAME (tail, frame)
9909 {
9910 f = XFRAME (frame);
9911
9912 /* Ignore tooltip frame. */
9913 if (EQ (frame, tooltip_frame))
9914 continue;
9915
9916 /* If a window on this frame changed size, report that to
9917 the user and clear the size-change flag. */
9918 if (FRAME_WINDOW_SIZES_CHANGED (f))
9919 {
9920 Lisp_Object functions;
9921
9922 /* Clear flag first in case we get an error below. */
9923 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9924 functions = Vwindow_size_change_functions;
9925 GCPRO2 (tail, functions);
9926
9927 while (CONSP (functions))
9928 {
9929 if (!EQ (XCAR (functions), Qt))
9930 call1 (XCAR (functions), frame);
9931 functions = XCDR (functions);
9932 }
9933 UNGCPRO;
9934 }
9935
9936 GCPRO1 (tail);
9937 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9938 #ifdef HAVE_WINDOW_SYSTEM
9939 update_tool_bar (f, 0);
9940 #endif
9941 #ifdef HAVE_NS
9942 if (windows_or_buffers_changed
9943 && FRAME_NS_P (f))
9944 ns_set_doc_edited (f, Fbuffer_modified_p
9945 (XWINDOW (f->selected_window)->buffer));
9946 #endif
9947 UNGCPRO;
9948 }
9949
9950 unbind_to (count, Qnil);
9951 }
9952 else
9953 {
9954 struct frame *sf = SELECTED_FRAME ();
9955 update_menu_bar (sf, 1, 0);
9956 #ifdef HAVE_WINDOW_SYSTEM
9957 update_tool_bar (sf, 1);
9958 #endif
9959 }
9960 }
9961
9962
9963 /* Update the menu bar item list for frame F. This has to be done
9964 before we start to fill in any display lines, because it can call
9965 eval.
9966
9967 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9968
9969 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9970 already ran the menu bar hooks for this redisplay, so there
9971 is no need to run them again. The return value is the
9972 updated value of this flag, to pass to the next call. */
9973
9974 static int
9975 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9976 {
9977 Lisp_Object window;
9978 register struct window *w;
9979
9980 /* If called recursively during a menu update, do nothing. This can
9981 happen when, for instance, an activate-menubar-hook causes a
9982 redisplay. */
9983 if (inhibit_menubar_update)
9984 return hooks_run;
9985
9986 window = FRAME_SELECTED_WINDOW (f);
9987 w = XWINDOW (window);
9988
9989 if (FRAME_WINDOW_P (f)
9990 ?
9991 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9992 || defined (HAVE_NS) || defined (USE_GTK)
9993 FRAME_EXTERNAL_MENU_BAR (f)
9994 #else
9995 FRAME_MENU_BAR_LINES (f) > 0
9996 #endif
9997 : FRAME_MENU_BAR_LINES (f) > 0)
9998 {
9999 /* If the user has switched buffers or windows, we need to
10000 recompute to reflect the new bindings. But we'll
10001 recompute when update_mode_lines is set too; that means
10002 that people can use force-mode-line-update to request
10003 that the menu bar be recomputed. The adverse effect on
10004 the rest of the redisplay algorithm is about the same as
10005 windows_or_buffers_changed anyway. */
10006 if (windows_or_buffers_changed
10007 /* This used to test w->update_mode_line, but we believe
10008 there is no need to recompute the menu in that case. */
10009 || update_mode_lines
10010 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10011 < BUF_MODIFF (XBUFFER (w->buffer)))
10012 != !NILP (w->last_had_star))
10013 || ((!NILP (Vtransient_mark_mode)
10014 && !NILP (XBUFFER (w->buffer)->mark_active))
10015 != !NILP (w->region_showing)))
10016 {
10017 struct buffer *prev = current_buffer;
10018 int count = SPECPDL_INDEX ();
10019
10020 specbind (Qinhibit_menubar_update, Qt);
10021
10022 set_buffer_internal_1 (XBUFFER (w->buffer));
10023 if (save_match_data)
10024 record_unwind_save_match_data ();
10025 if (NILP (Voverriding_local_map_menu_flag))
10026 {
10027 specbind (Qoverriding_terminal_local_map, Qnil);
10028 specbind (Qoverriding_local_map, Qnil);
10029 }
10030
10031 if (!hooks_run)
10032 {
10033 /* Run the Lucid hook. */
10034 safe_run_hooks (Qactivate_menubar_hook);
10035
10036 /* If it has changed current-menubar from previous value,
10037 really recompute the menu-bar from the value. */
10038 if (! NILP (Vlucid_menu_bar_dirty_flag))
10039 call0 (Qrecompute_lucid_menubar);
10040
10041 safe_run_hooks (Qmenu_bar_update_hook);
10042
10043 hooks_run = 1;
10044 }
10045
10046 XSETFRAME (Vmenu_updating_frame, f);
10047 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10048
10049 /* Redisplay the menu bar in case we changed it. */
10050 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10051 || defined (HAVE_NS) || defined (USE_GTK)
10052 if (FRAME_WINDOW_P (f))
10053 {
10054 #if defined (HAVE_NS)
10055 /* All frames on Mac OS share the same menubar. So only
10056 the selected frame should be allowed to set it. */
10057 if (f == SELECTED_FRAME ())
10058 #endif
10059 set_frame_menubar (f, 0, 0);
10060 }
10061 else
10062 /* On a terminal screen, the menu bar is an ordinary screen
10063 line, and this makes it get updated. */
10064 w->update_mode_line = Qt;
10065 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10066 /* In the non-toolkit version, the menu bar is an ordinary screen
10067 line, and this makes it get updated. */
10068 w->update_mode_line = Qt;
10069 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10070
10071 unbind_to (count, Qnil);
10072 set_buffer_internal_1 (prev);
10073 }
10074 }
10075
10076 return hooks_run;
10077 }
10078
10079
10080 \f
10081 /***********************************************************************
10082 Output Cursor
10083 ***********************************************************************/
10084
10085 #ifdef HAVE_WINDOW_SYSTEM
10086
10087 /* EXPORT:
10088 Nominal cursor position -- where to draw output.
10089 HPOS and VPOS are window relative glyph matrix coordinates.
10090 X and Y are window relative pixel coordinates. */
10091
10092 struct cursor_pos output_cursor;
10093
10094
10095 /* EXPORT:
10096 Set the global variable output_cursor to CURSOR. All cursor
10097 positions are relative to updated_window. */
10098
10099 void
10100 set_output_cursor (struct cursor_pos *cursor)
10101 {
10102 output_cursor.hpos = cursor->hpos;
10103 output_cursor.vpos = cursor->vpos;
10104 output_cursor.x = cursor->x;
10105 output_cursor.y = cursor->y;
10106 }
10107
10108
10109 /* EXPORT for RIF:
10110 Set a nominal cursor position.
10111
10112 HPOS and VPOS are column/row positions in a window glyph matrix. X
10113 and Y are window text area relative pixel positions.
10114
10115 If this is done during an update, updated_window will contain the
10116 window that is being updated and the position is the future output
10117 cursor position for that window. If updated_window is null, use
10118 selected_window and display the cursor at the given position. */
10119
10120 void
10121 x_cursor_to (int vpos, int hpos, int y, int x)
10122 {
10123 struct window *w;
10124
10125 /* If updated_window is not set, work on selected_window. */
10126 if (updated_window)
10127 w = updated_window;
10128 else
10129 w = XWINDOW (selected_window);
10130
10131 /* Set the output cursor. */
10132 output_cursor.hpos = hpos;
10133 output_cursor.vpos = vpos;
10134 output_cursor.x = x;
10135 output_cursor.y = y;
10136
10137 /* If not called as part of an update, really display the cursor.
10138 This will also set the cursor position of W. */
10139 if (updated_window == NULL)
10140 {
10141 BLOCK_INPUT;
10142 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10143 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10144 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10145 UNBLOCK_INPUT;
10146 }
10147 }
10148
10149 #endif /* HAVE_WINDOW_SYSTEM */
10150
10151 \f
10152 /***********************************************************************
10153 Tool-bars
10154 ***********************************************************************/
10155
10156 #ifdef HAVE_WINDOW_SYSTEM
10157
10158 /* Where the mouse was last time we reported a mouse event. */
10159
10160 FRAME_PTR last_mouse_frame;
10161
10162 /* Tool-bar item index of the item on which a mouse button was pressed
10163 or -1. */
10164
10165 int last_tool_bar_item;
10166
10167
10168 static Lisp_Object
10169 update_tool_bar_unwind (Lisp_Object frame)
10170 {
10171 selected_frame = frame;
10172 return Qnil;
10173 }
10174
10175 /* Update the tool-bar item list for frame F. This has to be done
10176 before we start to fill in any display lines. Called from
10177 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10178 and restore it here. */
10179
10180 static void
10181 update_tool_bar (struct frame *f, int save_match_data)
10182 {
10183 #if defined (USE_GTK) || defined (HAVE_NS)
10184 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10185 #else
10186 int do_update = WINDOWP (f->tool_bar_window)
10187 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10188 #endif
10189
10190 if (do_update)
10191 {
10192 Lisp_Object window;
10193 struct window *w;
10194
10195 window = FRAME_SELECTED_WINDOW (f);
10196 w = XWINDOW (window);
10197
10198 /* If the user has switched buffers or windows, we need to
10199 recompute to reflect the new bindings. But we'll
10200 recompute when update_mode_lines is set too; that means
10201 that people can use force-mode-line-update to request
10202 that the menu bar be recomputed. The adverse effect on
10203 the rest of the redisplay algorithm is about the same as
10204 windows_or_buffers_changed anyway. */
10205 if (windows_or_buffers_changed
10206 || !NILP (w->update_mode_line)
10207 || update_mode_lines
10208 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10209 < BUF_MODIFF (XBUFFER (w->buffer)))
10210 != !NILP (w->last_had_star))
10211 || ((!NILP (Vtransient_mark_mode)
10212 && !NILP (XBUFFER (w->buffer)->mark_active))
10213 != !NILP (w->region_showing)))
10214 {
10215 struct buffer *prev = current_buffer;
10216 int count = SPECPDL_INDEX ();
10217 Lisp_Object frame, new_tool_bar;
10218 int new_n_tool_bar;
10219 struct gcpro gcpro1;
10220
10221 /* Set current_buffer to the buffer of the selected
10222 window of the frame, so that we get the right local
10223 keymaps. */
10224 set_buffer_internal_1 (XBUFFER (w->buffer));
10225
10226 /* Save match data, if we must. */
10227 if (save_match_data)
10228 record_unwind_save_match_data ();
10229
10230 /* Make sure that we don't accidentally use bogus keymaps. */
10231 if (NILP (Voverriding_local_map_menu_flag))
10232 {
10233 specbind (Qoverriding_terminal_local_map, Qnil);
10234 specbind (Qoverriding_local_map, Qnil);
10235 }
10236
10237 GCPRO1 (new_tool_bar);
10238
10239 /* We must temporarily set the selected frame to this frame
10240 before calling tool_bar_items, because the calculation of
10241 the tool-bar keymap uses the selected frame (see
10242 `tool-bar-make-keymap' in tool-bar.el). */
10243 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10244 XSETFRAME (frame, f);
10245 selected_frame = frame;
10246
10247 /* Build desired tool-bar items from keymaps. */
10248 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10249 &new_n_tool_bar);
10250
10251 /* Redisplay the tool-bar if we changed it. */
10252 if (new_n_tool_bar != f->n_tool_bar_items
10253 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10254 {
10255 /* Redisplay that happens asynchronously due to an expose event
10256 may access f->tool_bar_items. Make sure we update both
10257 variables within BLOCK_INPUT so no such event interrupts. */
10258 BLOCK_INPUT;
10259 f->tool_bar_items = new_tool_bar;
10260 f->n_tool_bar_items = new_n_tool_bar;
10261 w->update_mode_line = Qt;
10262 UNBLOCK_INPUT;
10263 }
10264
10265 UNGCPRO;
10266
10267 unbind_to (count, Qnil);
10268 set_buffer_internal_1 (prev);
10269 }
10270 }
10271 }
10272
10273
10274 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10275 F's desired tool-bar contents. F->tool_bar_items must have
10276 been set up previously by calling prepare_menu_bars. */
10277
10278 static void
10279 build_desired_tool_bar_string (struct frame *f)
10280 {
10281 int i, size, size_needed;
10282 struct gcpro gcpro1, gcpro2, gcpro3;
10283 Lisp_Object image, plist, props;
10284
10285 image = plist = props = Qnil;
10286 GCPRO3 (image, plist, props);
10287
10288 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10289 Otherwise, make a new string. */
10290
10291 /* The size of the string we might be able to reuse. */
10292 size = (STRINGP (f->desired_tool_bar_string)
10293 ? SCHARS (f->desired_tool_bar_string)
10294 : 0);
10295
10296 /* We need one space in the string for each image. */
10297 size_needed = f->n_tool_bar_items;
10298
10299 /* Reuse f->desired_tool_bar_string, if possible. */
10300 if (size < size_needed || NILP (f->desired_tool_bar_string))
10301 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10302 make_number (' '));
10303 else
10304 {
10305 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10306 Fremove_text_properties (make_number (0), make_number (size),
10307 props, f->desired_tool_bar_string);
10308 }
10309
10310 /* Put a `display' property on the string for the images to display,
10311 put a `menu_item' property on tool-bar items with a value that
10312 is the index of the item in F's tool-bar item vector. */
10313 for (i = 0; i < f->n_tool_bar_items; ++i)
10314 {
10315 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10316
10317 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10318 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10319 int hmargin, vmargin, relief, idx, end;
10320
10321 /* If image is a vector, choose the image according to the
10322 button state. */
10323 image = PROP (TOOL_BAR_ITEM_IMAGES);
10324 if (VECTORP (image))
10325 {
10326 if (enabled_p)
10327 idx = (selected_p
10328 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10329 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10330 else
10331 idx = (selected_p
10332 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10333 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10334
10335 xassert (ASIZE (image) >= idx);
10336 image = AREF (image, idx);
10337 }
10338 else
10339 idx = -1;
10340
10341 /* Ignore invalid image specifications. */
10342 if (!valid_image_p (image))
10343 continue;
10344
10345 /* Display the tool-bar button pressed, or depressed. */
10346 plist = Fcopy_sequence (XCDR (image));
10347
10348 /* Compute margin and relief to draw. */
10349 relief = (tool_bar_button_relief >= 0
10350 ? tool_bar_button_relief
10351 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10352 hmargin = vmargin = relief;
10353
10354 if (INTEGERP (Vtool_bar_button_margin)
10355 && XINT (Vtool_bar_button_margin) > 0)
10356 {
10357 hmargin += XFASTINT (Vtool_bar_button_margin);
10358 vmargin += XFASTINT (Vtool_bar_button_margin);
10359 }
10360 else if (CONSP (Vtool_bar_button_margin))
10361 {
10362 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10363 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10364 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10365
10366 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10367 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10368 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10369 }
10370
10371 if (auto_raise_tool_bar_buttons_p)
10372 {
10373 /* Add a `:relief' property to the image spec if the item is
10374 selected. */
10375 if (selected_p)
10376 {
10377 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10378 hmargin -= relief;
10379 vmargin -= relief;
10380 }
10381 }
10382 else
10383 {
10384 /* If image is selected, display it pressed, i.e. with a
10385 negative relief. If it's not selected, display it with a
10386 raised relief. */
10387 plist = Fplist_put (plist, QCrelief,
10388 (selected_p
10389 ? make_number (-relief)
10390 : make_number (relief)));
10391 hmargin -= relief;
10392 vmargin -= relief;
10393 }
10394
10395 /* Put a margin around the image. */
10396 if (hmargin || vmargin)
10397 {
10398 if (hmargin == vmargin)
10399 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10400 else
10401 plist = Fplist_put (plist, QCmargin,
10402 Fcons (make_number (hmargin),
10403 make_number (vmargin)));
10404 }
10405
10406 /* If button is not enabled, and we don't have special images
10407 for the disabled state, make the image appear disabled by
10408 applying an appropriate algorithm to it. */
10409 if (!enabled_p && idx < 0)
10410 plist = Fplist_put (plist, QCconversion, Qdisabled);
10411
10412 /* Put a `display' text property on the string for the image to
10413 display. Put a `menu-item' property on the string that gives
10414 the start of this item's properties in the tool-bar items
10415 vector. */
10416 image = Fcons (Qimage, plist);
10417 props = list4 (Qdisplay, image,
10418 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10419
10420 /* Let the last image hide all remaining spaces in the tool bar
10421 string. The string can be longer than needed when we reuse a
10422 previous string. */
10423 if (i + 1 == f->n_tool_bar_items)
10424 end = SCHARS (f->desired_tool_bar_string);
10425 else
10426 end = i + 1;
10427 Fadd_text_properties (make_number (i), make_number (end),
10428 props, f->desired_tool_bar_string);
10429 #undef PROP
10430 }
10431
10432 UNGCPRO;
10433 }
10434
10435
10436 /* Display one line of the tool-bar of frame IT->f.
10437
10438 HEIGHT specifies the desired height of the tool-bar line.
10439 If the actual height of the glyph row is less than HEIGHT, the
10440 row's height is increased to HEIGHT, and the icons are centered
10441 vertically in the new height.
10442
10443 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10444 count a final empty row in case the tool-bar width exactly matches
10445 the window width.
10446 */
10447
10448 static void
10449 display_tool_bar_line (struct it *it, int height)
10450 {
10451 struct glyph_row *row = it->glyph_row;
10452 int max_x = it->last_visible_x;
10453 struct glyph *last;
10454
10455 prepare_desired_row (row);
10456 row->y = it->current_y;
10457
10458 /* Note that this isn't made use of if the face hasn't a box,
10459 so there's no need to check the face here. */
10460 it->start_of_box_run_p = 1;
10461
10462 while (it->current_x < max_x)
10463 {
10464 int x, n_glyphs_before, i, nglyphs;
10465 struct it it_before;
10466
10467 /* Get the next display element. */
10468 if (!get_next_display_element (it))
10469 {
10470 /* Don't count empty row if we are counting needed tool-bar lines. */
10471 if (height < 0 && !it->hpos)
10472 return;
10473 break;
10474 }
10475
10476 /* Produce glyphs. */
10477 n_glyphs_before = row->used[TEXT_AREA];
10478 it_before = *it;
10479
10480 PRODUCE_GLYPHS (it);
10481
10482 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10483 i = 0;
10484 x = it_before.current_x;
10485 while (i < nglyphs)
10486 {
10487 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10488
10489 if (x + glyph->pixel_width > max_x)
10490 {
10491 /* Glyph doesn't fit on line. Backtrack. */
10492 row->used[TEXT_AREA] = n_glyphs_before;
10493 *it = it_before;
10494 /* If this is the only glyph on this line, it will never fit on the
10495 toolbar, so skip it. But ensure there is at least one glyph,
10496 so we don't accidentally disable the tool-bar. */
10497 if (n_glyphs_before == 0
10498 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10499 break;
10500 goto out;
10501 }
10502
10503 ++it->hpos;
10504 x += glyph->pixel_width;
10505 ++i;
10506 }
10507
10508 /* Stop at line ends. */
10509 if (ITERATOR_AT_END_OF_LINE_P (it))
10510 break;
10511
10512 set_iterator_to_next (it, 1);
10513 }
10514
10515 out:;
10516
10517 row->displays_text_p = row->used[TEXT_AREA] != 0;
10518
10519 /* Use default face for the border below the tool bar.
10520
10521 FIXME: When auto-resize-tool-bars is grow-only, there is
10522 no additional border below the possibly empty tool-bar lines.
10523 So to make the extra empty lines look "normal", we have to
10524 use the tool-bar face for the border too. */
10525 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10526 it->face_id = DEFAULT_FACE_ID;
10527
10528 extend_face_to_end_of_line (it);
10529 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10530 last->right_box_line_p = 1;
10531 if (last == row->glyphs[TEXT_AREA])
10532 last->left_box_line_p = 1;
10533
10534 /* Make line the desired height and center it vertically. */
10535 if ((height -= it->max_ascent + it->max_descent) > 0)
10536 {
10537 /* Don't add more than one line height. */
10538 height %= FRAME_LINE_HEIGHT (it->f);
10539 it->max_ascent += height / 2;
10540 it->max_descent += (height + 1) / 2;
10541 }
10542
10543 compute_line_metrics (it);
10544
10545 /* If line is empty, make it occupy the rest of the tool-bar. */
10546 if (!row->displays_text_p)
10547 {
10548 row->height = row->phys_height = it->last_visible_y - row->y;
10549 row->visible_height = row->height;
10550 row->ascent = row->phys_ascent = 0;
10551 row->extra_line_spacing = 0;
10552 }
10553
10554 row->full_width_p = 1;
10555 row->continued_p = 0;
10556 row->truncated_on_left_p = 0;
10557 row->truncated_on_right_p = 0;
10558
10559 it->current_x = it->hpos = 0;
10560 it->current_y += row->height;
10561 ++it->vpos;
10562 ++it->glyph_row;
10563 }
10564
10565
10566 /* Max tool-bar height. */
10567
10568 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10569 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10570
10571 /* Value is the number of screen lines needed to make all tool-bar
10572 items of frame F visible. The number of actual rows needed is
10573 returned in *N_ROWS if non-NULL. */
10574
10575 static int
10576 tool_bar_lines_needed (struct frame *f, int *n_rows)
10577 {
10578 struct window *w = XWINDOW (f->tool_bar_window);
10579 struct it it;
10580 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10581 the desired matrix, so use (unused) mode-line row as temporary row to
10582 avoid destroying the first tool-bar row. */
10583 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10584
10585 /* Initialize an iterator for iteration over
10586 F->desired_tool_bar_string in the tool-bar window of frame F. */
10587 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10588 it.first_visible_x = 0;
10589 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10590 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10591
10592 while (!ITERATOR_AT_END_P (&it))
10593 {
10594 clear_glyph_row (temp_row);
10595 it.glyph_row = temp_row;
10596 display_tool_bar_line (&it, -1);
10597 }
10598 clear_glyph_row (temp_row);
10599
10600 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10601 if (n_rows)
10602 *n_rows = it.vpos > 0 ? it.vpos : -1;
10603
10604 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10605 }
10606
10607
10608 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10609 0, 1, 0,
10610 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10611 (Lisp_Object frame)
10612 {
10613 struct frame *f;
10614 struct window *w;
10615 int nlines = 0;
10616
10617 if (NILP (frame))
10618 frame = selected_frame;
10619 else
10620 CHECK_FRAME (frame);
10621 f = XFRAME (frame);
10622
10623 if (WINDOWP (f->tool_bar_window)
10624 || (w = XWINDOW (f->tool_bar_window),
10625 WINDOW_TOTAL_LINES (w) > 0))
10626 {
10627 update_tool_bar (f, 1);
10628 if (f->n_tool_bar_items)
10629 {
10630 build_desired_tool_bar_string (f);
10631 nlines = tool_bar_lines_needed (f, NULL);
10632 }
10633 }
10634
10635 return make_number (nlines);
10636 }
10637
10638
10639 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10640 height should be changed. */
10641
10642 static int
10643 redisplay_tool_bar (struct frame *f)
10644 {
10645 struct window *w;
10646 struct it it;
10647 struct glyph_row *row;
10648
10649 #if defined (USE_GTK) || defined (HAVE_NS)
10650 if (FRAME_EXTERNAL_TOOL_BAR (f))
10651 update_frame_tool_bar (f);
10652 return 0;
10653 #endif
10654
10655 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10656 do anything. This means you must start with tool-bar-lines
10657 non-zero to get the auto-sizing effect. Or in other words, you
10658 can turn off tool-bars by specifying tool-bar-lines zero. */
10659 if (!WINDOWP (f->tool_bar_window)
10660 || (w = XWINDOW (f->tool_bar_window),
10661 WINDOW_TOTAL_LINES (w) == 0))
10662 return 0;
10663
10664 /* Set up an iterator for the tool-bar window. */
10665 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10666 it.first_visible_x = 0;
10667 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10668 row = it.glyph_row;
10669
10670 /* Build a string that represents the contents of the tool-bar. */
10671 build_desired_tool_bar_string (f);
10672 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10673
10674 if (f->n_tool_bar_rows == 0)
10675 {
10676 int nlines;
10677
10678 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10679 nlines != WINDOW_TOTAL_LINES (w)))
10680 {
10681 Lisp_Object frame;
10682 int old_height = WINDOW_TOTAL_LINES (w);
10683
10684 XSETFRAME (frame, f);
10685 Fmodify_frame_parameters (frame,
10686 Fcons (Fcons (Qtool_bar_lines,
10687 make_number (nlines)),
10688 Qnil));
10689 if (WINDOW_TOTAL_LINES (w) != old_height)
10690 {
10691 clear_glyph_matrix (w->desired_matrix);
10692 fonts_changed_p = 1;
10693 return 1;
10694 }
10695 }
10696 }
10697
10698 /* Display as many lines as needed to display all tool-bar items. */
10699
10700 if (f->n_tool_bar_rows > 0)
10701 {
10702 int border, rows, height, extra;
10703
10704 if (INTEGERP (Vtool_bar_border))
10705 border = XINT (Vtool_bar_border);
10706 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10707 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10708 else if (EQ (Vtool_bar_border, Qborder_width))
10709 border = f->border_width;
10710 else
10711 border = 0;
10712 if (border < 0)
10713 border = 0;
10714
10715 rows = f->n_tool_bar_rows;
10716 height = max (1, (it.last_visible_y - border) / rows);
10717 extra = it.last_visible_y - border - height * rows;
10718
10719 while (it.current_y < it.last_visible_y)
10720 {
10721 int h = 0;
10722 if (extra > 0 && rows-- > 0)
10723 {
10724 h = (extra + rows - 1) / rows;
10725 extra -= h;
10726 }
10727 display_tool_bar_line (&it, height + h);
10728 }
10729 }
10730 else
10731 {
10732 while (it.current_y < it.last_visible_y)
10733 display_tool_bar_line (&it, 0);
10734 }
10735
10736 /* It doesn't make much sense to try scrolling in the tool-bar
10737 window, so don't do it. */
10738 w->desired_matrix->no_scrolling_p = 1;
10739 w->must_be_updated_p = 1;
10740
10741 if (!NILP (Vauto_resize_tool_bars))
10742 {
10743 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10744 int change_height_p = 0;
10745
10746 /* If we couldn't display everything, change the tool-bar's
10747 height if there is room for more. */
10748 if (IT_STRING_CHARPOS (it) < it.end_charpos
10749 && it.current_y < max_tool_bar_height)
10750 change_height_p = 1;
10751
10752 row = it.glyph_row - 1;
10753
10754 /* If there are blank lines at the end, except for a partially
10755 visible blank line at the end that is smaller than
10756 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10757 if (!row->displays_text_p
10758 && row->height >= FRAME_LINE_HEIGHT (f))
10759 change_height_p = 1;
10760
10761 /* If row displays tool-bar items, but is partially visible,
10762 change the tool-bar's height. */
10763 if (row->displays_text_p
10764 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10765 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10766 change_height_p = 1;
10767
10768 /* Resize windows as needed by changing the `tool-bar-lines'
10769 frame parameter. */
10770 if (change_height_p)
10771 {
10772 Lisp_Object frame;
10773 int old_height = WINDOW_TOTAL_LINES (w);
10774 int nrows;
10775 int nlines = tool_bar_lines_needed (f, &nrows);
10776
10777 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10778 && !f->minimize_tool_bar_window_p)
10779 ? (nlines > old_height)
10780 : (nlines != old_height));
10781 f->minimize_tool_bar_window_p = 0;
10782
10783 if (change_height_p)
10784 {
10785 XSETFRAME (frame, f);
10786 Fmodify_frame_parameters (frame,
10787 Fcons (Fcons (Qtool_bar_lines,
10788 make_number (nlines)),
10789 Qnil));
10790 if (WINDOW_TOTAL_LINES (w) != old_height)
10791 {
10792 clear_glyph_matrix (w->desired_matrix);
10793 f->n_tool_bar_rows = nrows;
10794 fonts_changed_p = 1;
10795 return 1;
10796 }
10797 }
10798 }
10799 }
10800
10801 f->minimize_tool_bar_window_p = 0;
10802 return 0;
10803 }
10804
10805
10806 /* Get information about the tool-bar item which is displayed in GLYPH
10807 on frame F. Return in *PROP_IDX the index where tool-bar item
10808 properties start in F->tool_bar_items. Value is zero if
10809 GLYPH doesn't display a tool-bar item. */
10810
10811 static int
10812 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10813 {
10814 Lisp_Object prop;
10815 int success_p;
10816 int charpos;
10817
10818 /* This function can be called asynchronously, which means we must
10819 exclude any possibility that Fget_text_property signals an
10820 error. */
10821 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10822 charpos = max (0, charpos);
10823
10824 /* Get the text property `menu-item' at pos. The value of that
10825 property is the start index of this item's properties in
10826 F->tool_bar_items. */
10827 prop = Fget_text_property (make_number (charpos),
10828 Qmenu_item, f->current_tool_bar_string);
10829 if (INTEGERP (prop))
10830 {
10831 *prop_idx = XINT (prop);
10832 success_p = 1;
10833 }
10834 else
10835 success_p = 0;
10836
10837 return success_p;
10838 }
10839
10840 \f
10841 /* Get information about the tool-bar item at position X/Y on frame F.
10842 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10843 the current matrix of the tool-bar window of F, or NULL if not
10844 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10845 item in F->tool_bar_items. Value is
10846
10847 -1 if X/Y is not on a tool-bar item
10848 0 if X/Y is on the same item that was highlighted before.
10849 1 otherwise. */
10850
10851 static int
10852 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10853 int *hpos, int *vpos, int *prop_idx)
10854 {
10855 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10856 struct window *w = XWINDOW (f->tool_bar_window);
10857 int area;
10858
10859 /* Find the glyph under X/Y. */
10860 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10861 if (*glyph == NULL)
10862 return -1;
10863
10864 /* Get the start of this tool-bar item's properties in
10865 f->tool_bar_items. */
10866 if (!tool_bar_item_info (f, *glyph, prop_idx))
10867 return -1;
10868
10869 /* Is mouse on the highlighted item? */
10870 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10871 && *vpos >= hlinfo->mouse_face_beg_row
10872 && *vpos <= hlinfo->mouse_face_end_row
10873 && (*vpos > hlinfo->mouse_face_beg_row
10874 || *hpos >= hlinfo->mouse_face_beg_col)
10875 && (*vpos < hlinfo->mouse_face_end_row
10876 || *hpos < hlinfo->mouse_face_end_col
10877 || hlinfo->mouse_face_past_end))
10878 return 0;
10879
10880 return 1;
10881 }
10882
10883
10884 /* EXPORT:
10885 Handle mouse button event on the tool-bar of frame F, at
10886 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10887 0 for button release. MODIFIERS is event modifiers for button
10888 release. */
10889
10890 void
10891 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10892 unsigned int modifiers)
10893 {
10894 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10895 struct window *w = XWINDOW (f->tool_bar_window);
10896 int hpos, vpos, prop_idx;
10897 struct glyph *glyph;
10898 Lisp_Object enabled_p;
10899
10900 /* If not on the highlighted tool-bar item, return. */
10901 frame_to_window_pixel_xy (w, &x, &y);
10902 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10903 return;
10904
10905 /* If item is disabled, do nothing. */
10906 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10907 if (NILP (enabled_p))
10908 return;
10909
10910 if (down_p)
10911 {
10912 /* Show item in pressed state. */
10913 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10914 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10915 last_tool_bar_item = prop_idx;
10916 }
10917 else
10918 {
10919 Lisp_Object key, frame;
10920 struct input_event event;
10921 EVENT_INIT (event);
10922
10923 /* Show item in released state. */
10924 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10925 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10926
10927 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10928
10929 XSETFRAME (frame, f);
10930 event.kind = TOOL_BAR_EVENT;
10931 event.frame_or_window = frame;
10932 event.arg = frame;
10933 kbd_buffer_store_event (&event);
10934
10935 event.kind = TOOL_BAR_EVENT;
10936 event.frame_or_window = frame;
10937 event.arg = key;
10938 event.modifiers = modifiers;
10939 kbd_buffer_store_event (&event);
10940 last_tool_bar_item = -1;
10941 }
10942 }
10943
10944
10945 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10946 tool-bar window-relative coordinates X/Y. Called from
10947 note_mouse_highlight. */
10948
10949 static void
10950 note_tool_bar_highlight (struct frame *f, int x, int y)
10951 {
10952 Lisp_Object window = f->tool_bar_window;
10953 struct window *w = XWINDOW (window);
10954 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10955 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10956 int hpos, vpos;
10957 struct glyph *glyph;
10958 struct glyph_row *row;
10959 int i;
10960 Lisp_Object enabled_p;
10961 int prop_idx;
10962 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10963 int mouse_down_p, rc;
10964
10965 /* Function note_mouse_highlight is called with negative X/Y
10966 values when mouse moves outside of the frame. */
10967 if (x <= 0 || y <= 0)
10968 {
10969 clear_mouse_face (hlinfo);
10970 return;
10971 }
10972
10973 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10974 if (rc < 0)
10975 {
10976 /* Not on tool-bar item. */
10977 clear_mouse_face (hlinfo);
10978 return;
10979 }
10980 else if (rc == 0)
10981 /* On same tool-bar item as before. */
10982 goto set_help_echo;
10983
10984 clear_mouse_face (hlinfo);
10985
10986 /* Mouse is down, but on different tool-bar item? */
10987 mouse_down_p = (dpyinfo->grabbed
10988 && f == last_mouse_frame
10989 && FRAME_LIVE_P (f));
10990 if (mouse_down_p
10991 && last_tool_bar_item != prop_idx)
10992 return;
10993
10994 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10995 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10996
10997 /* If tool-bar item is not enabled, don't highlight it. */
10998 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10999 if (!NILP (enabled_p))
11000 {
11001 /* Compute the x-position of the glyph. In front and past the
11002 image is a space. We include this in the highlighted area. */
11003 row = MATRIX_ROW (w->current_matrix, vpos);
11004 for (i = x = 0; i < hpos; ++i)
11005 x += row->glyphs[TEXT_AREA][i].pixel_width;
11006
11007 /* Record this as the current active region. */
11008 hlinfo->mouse_face_beg_col = hpos;
11009 hlinfo->mouse_face_beg_row = vpos;
11010 hlinfo->mouse_face_beg_x = x;
11011 hlinfo->mouse_face_beg_y = row->y;
11012 hlinfo->mouse_face_past_end = 0;
11013
11014 hlinfo->mouse_face_end_col = hpos + 1;
11015 hlinfo->mouse_face_end_row = vpos;
11016 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11017 hlinfo->mouse_face_end_y = row->y;
11018 hlinfo->mouse_face_window = window;
11019 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11020
11021 /* Display it as active. */
11022 show_mouse_face (hlinfo, draw);
11023 hlinfo->mouse_face_image_state = draw;
11024 }
11025
11026 set_help_echo:
11027
11028 /* Set help_echo_string to a help string to display for this tool-bar item.
11029 XTread_socket does the rest. */
11030 help_echo_object = help_echo_window = Qnil;
11031 help_echo_pos = -1;
11032 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11033 if (NILP (help_echo_string))
11034 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11035 }
11036
11037 #endif /* HAVE_WINDOW_SYSTEM */
11038
11039
11040 \f
11041 /************************************************************************
11042 Horizontal scrolling
11043 ************************************************************************/
11044
11045 static int hscroll_window_tree (Lisp_Object);
11046 static int hscroll_windows (Lisp_Object);
11047
11048 /* For all leaf windows in the window tree rooted at WINDOW, set their
11049 hscroll value so that PT is (i) visible in the window, and (ii) so
11050 that it is not within a certain margin at the window's left and
11051 right border. Value is non-zero if any window's hscroll has been
11052 changed. */
11053
11054 static int
11055 hscroll_window_tree (Lisp_Object window)
11056 {
11057 int hscrolled_p = 0;
11058 int hscroll_relative_p = FLOATP (Vhscroll_step);
11059 int hscroll_step_abs = 0;
11060 double hscroll_step_rel = 0;
11061
11062 if (hscroll_relative_p)
11063 {
11064 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11065 if (hscroll_step_rel < 0)
11066 {
11067 hscroll_relative_p = 0;
11068 hscroll_step_abs = 0;
11069 }
11070 }
11071 else if (INTEGERP (Vhscroll_step))
11072 {
11073 hscroll_step_abs = XINT (Vhscroll_step);
11074 if (hscroll_step_abs < 0)
11075 hscroll_step_abs = 0;
11076 }
11077 else
11078 hscroll_step_abs = 0;
11079
11080 while (WINDOWP (window))
11081 {
11082 struct window *w = XWINDOW (window);
11083
11084 if (WINDOWP (w->hchild))
11085 hscrolled_p |= hscroll_window_tree (w->hchild);
11086 else if (WINDOWP (w->vchild))
11087 hscrolled_p |= hscroll_window_tree (w->vchild);
11088 else if (w->cursor.vpos >= 0)
11089 {
11090 int h_margin;
11091 int text_area_width;
11092 struct glyph_row *current_cursor_row
11093 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11094 struct glyph_row *desired_cursor_row
11095 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11096 struct glyph_row *cursor_row
11097 = (desired_cursor_row->enabled_p
11098 ? desired_cursor_row
11099 : current_cursor_row);
11100
11101 text_area_width = window_box_width (w, TEXT_AREA);
11102
11103 /* Scroll when cursor is inside this scroll margin. */
11104 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11105
11106 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11107 && ((XFASTINT (w->hscroll)
11108 && w->cursor.x <= h_margin)
11109 || (cursor_row->enabled_p
11110 && cursor_row->truncated_on_right_p
11111 && (w->cursor.x >= text_area_width - h_margin))))
11112 {
11113 struct it it;
11114 int hscroll;
11115 struct buffer *saved_current_buffer;
11116 EMACS_INT pt;
11117 int wanted_x;
11118
11119 /* Find point in a display of infinite width. */
11120 saved_current_buffer = current_buffer;
11121 current_buffer = XBUFFER (w->buffer);
11122
11123 if (w == XWINDOW (selected_window))
11124 pt = BUF_PT (current_buffer);
11125 else
11126 {
11127 pt = marker_position (w->pointm);
11128 pt = max (BEGV, pt);
11129 pt = min (ZV, pt);
11130 }
11131
11132 /* Move iterator to pt starting at cursor_row->start in
11133 a line with infinite width. */
11134 init_to_row_start (&it, w, cursor_row);
11135 it.last_visible_x = INFINITY;
11136 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11137 current_buffer = saved_current_buffer;
11138
11139 /* Position cursor in window. */
11140 if (!hscroll_relative_p && hscroll_step_abs == 0)
11141 hscroll = max (0, (it.current_x
11142 - (ITERATOR_AT_END_OF_LINE_P (&it)
11143 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11144 : (text_area_width / 2))))
11145 / FRAME_COLUMN_WIDTH (it.f);
11146 else if (w->cursor.x >= text_area_width - h_margin)
11147 {
11148 if (hscroll_relative_p)
11149 wanted_x = text_area_width * (1 - hscroll_step_rel)
11150 - h_margin;
11151 else
11152 wanted_x = text_area_width
11153 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11154 - h_margin;
11155 hscroll
11156 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11157 }
11158 else
11159 {
11160 if (hscroll_relative_p)
11161 wanted_x = text_area_width * hscroll_step_rel
11162 + h_margin;
11163 else
11164 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11165 + h_margin;
11166 hscroll
11167 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11168 }
11169 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11170
11171 /* Don't call Fset_window_hscroll if value hasn't
11172 changed because it will prevent redisplay
11173 optimizations. */
11174 if (XFASTINT (w->hscroll) != hscroll)
11175 {
11176 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11177 w->hscroll = make_number (hscroll);
11178 hscrolled_p = 1;
11179 }
11180 }
11181 }
11182
11183 window = w->next;
11184 }
11185
11186 /* Value is non-zero if hscroll of any leaf window has been changed. */
11187 return hscrolled_p;
11188 }
11189
11190
11191 /* Set hscroll so that cursor is visible and not inside horizontal
11192 scroll margins for all windows in the tree rooted at WINDOW. See
11193 also hscroll_window_tree above. Value is non-zero if any window's
11194 hscroll has been changed. If it has, desired matrices on the frame
11195 of WINDOW are cleared. */
11196
11197 static int
11198 hscroll_windows (Lisp_Object window)
11199 {
11200 int hscrolled_p = hscroll_window_tree (window);
11201 if (hscrolled_p)
11202 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11203 return hscrolled_p;
11204 }
11205
11206
11207 \f
11208 /************************************************************************
11209 Redisplay
11210 ************************************************************************/
11211
11212 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11213 to a non-zero value. This is sometimes handy to have in a debugger
11214 session. */
11215
11216 #if GLYPH_DEBUG
11217
11218 /* First and last unchanged row for try_window_id. */
11219
11220 int debug_first_unchanged_at_end_vpos;
11221 int debug_last_unchanged_at_beg_vpos;
11222
11223 /* Delta vpos and y. */
11224
11225 int debug_dvpos, debug_dy;
11226
11227 /* Delta in characters and bytes for try_window_id. */
11228
11229 EMACS_INT debug_delta, debug_delta_bytes;
11230
11231 /* Values of window_end_pos and window_end_vpos at the end of
11232 try_window_id. */
11233
11234 EMACS_INT debug_end_pos, debug_end_vpos;
11235
11236 /* Append a string to W->desired_matrix->method. FMT is a printf
11237 format string. A1...A9 are a supplement for a variable-length
11238 argument list. If trace_redisplay_p is non-zero also printf the
11239 resulting string to stderr. */
11240
11241 static void
11242 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11243 struct window *w;
11244 char *fmt;
11245 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11246 {
11247 char buffer[512];
11248 char *method = w->desired_matrix->method;
11249 int len = strlen (method);
11250 int size = sizeof w->desired_matrix->method;
11251 int remaining = size - len - 1;
11252
11253 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11254 if (len && remaining)
11255 {
11256 method[len] = '|';
11257 --remaining, ++len;
11258 }
11259
11260 strncpy (method + len, buffer, remaining);
11261
11262 if (trace_redisplay_p)
11263 fprintf (stderr, "%p (%s): %s\n",
11264 w,
11265 ((BUFFERP (w->buffer)
11266 && STRINGP (XBUFFER (w->buffer)->name))
11267 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11268 : "no buffer"),
11269 buffer);
11270 }
11271
11272 #endif /* GLYPH_DEBUG */
11273
11274
11275 /* Value is non-zero if all changes in window W, which displays
11276 current_buffer, are in the text between START and END. START is a
11277 buffer position, END is given as a distance from Z. Used in
11278 redisplay_internal for display optimization. */
11279
11280 static INLINE int
11281 text_outside_line_unchanged_p (struct window *w,
11282 EMACS_INT start, EMACS_INT end)
11283 {
11284 int unchanged_p = 1;
11285
11286 /* If text or overlays have changed, see where. */
11287 if (XFASTINT (w->last_modified) < MODIFF
11288 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11289 {
11290 /* Gap in the line? */
11291 if (GPT < start || Z - GPT < end)
11292 unchanged_p = 0;
11293
11294 /* Changes start in front of the line, or end after it? */
11295 if (unchanged_p
11296 && (BEG_UNCHANGED < start - 1
11297 || END_UNCHANGED < end))
11298 unchanged_p = 0;
11299
11300 /* If selective display, can't optimize if changes start at the
11301 beginning of the line. */
11302 if (unchanged_p
11303 && INTEGERP (current_buffer->selective_display)
11304 && XINT (current_buffer->selective_display) > 0
11305 && (BEG_UNCHANGED < start || GPT <= start))
11306 unchanged_p = 0;
11307
11308 /* If there are overlays at the start or end of the line, these
11309 may have overlay strings with newlines in them. A change at
11310 START, for instance, may actually concern the display of such
11311 overlay strings as well, and they are displayed on different
11312 lines. So, quickly rule out this case. (For the future, it
11313 might be desirable to implement something more telling than
11314 just BEG/END_UNCHANGED.) */
11315 if (unchanged_p)
11316 {
11317 if (BEG + BEG_UNCHANGED == start
11318 && overlay_touches_p (start))
11319 unchanged_p = 0;
11320 if (END_UNCHANGED == end
11321 && overlay_touches_p (Z - end))
11322 unchanged_p = 0;
11323 }
11324
11325 /* Under bidi reordering, adding or deleting a character in the
11326 beginning of a paragraph, before the first strong directional
11327 character, can change the base direction of the paragraph (unless
11328 the buffer specifies a fixed paragraph direction), which will
11329 require to redisplay the whole paragraph. It might be worthwhile
11330 to find the paragraph limits and widen the range of redisplayed
11331 lines to that, but for now just give up this optimization. */
11332 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11333 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11334 unchanged_p = 0;
11335 }
11336
11337 return unchanged_p;
11338 }
11339
11340
11341 /* Do a frame update, taking possible shortcuts into account. This is
11342 the main external entry point for redisplay.
11343
11344 If the last redisplay displayed an echo area message and that message
11345 is no longer requested, we clear the echo area or bring back the
11346 mini-buffer if that is in use. */
11347
11348 void
11349 redisplay (void)
11350 {
11351 redisplay_internal (0);
11352 }
11353
11354
11355 static Lisp_Object
11356 overlay_arrow_string_or_property (Lisp_Object var)
11357 {
11358 Lisp_Object val;
11359
11360 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11361 return val;
11362
11363 return Voverlay_arrow_string;
11364 }
11365
11366 /* Return 1 if there are any overlay-arrows in current_buffer. */
11367 static int
11368 overlay_arrow_in_current_buffer_p (void)
11369 {
11370 Lisp_Object vlist;
11371
11372 for (vlist = Voverlay_arrow_variable_list;
11373 CONSP (vlist);
11374 vlist = XCDR (vlist))
11375 {
11376 Lisp_Object var = XCAR (vlist);
11377 Lisp_Object val;
11378
11379 if (!SYMBOLP (var))
11380 continue;
11381 val = find_symbol_value (var);
11382 if (MARKERP (val)
11383 && current_buffer == XMARKER (val)->buffer)
11384 return 1;
11385 }
11386 return 0;
11387 }
11388
11389
11390 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11391 has changed. */
11392
11393 static int
11394 overlay_arrows_changed_p (void)
11395 {
11396 Lisp_Object vlist;
11397
11398 for (vlist = Voverlay_arrow_variable_list;
11399 CONSP (vlist);
11400 vlist = XCDR (vlist))
11401 {
11402 Lisp_Object var = XCAR (vlist);
11403 Lisp_Object val, pstr;
11404
11405 if (!SYMBOLP (var))
11406 continue;
11407 val = find_symbol_value (var);
11408 if (!MARKERP (val))
11409 continue;
11410 if (! EQ (COERCE_MARKER (val),
11411 Fget (var, Qlast_arrow_position))
11412 || ! (pstr = overlay_arrow_string_or_property (var),
11413 EQ (pstr, Fget (var, Qlast_arrow_string))))
11414 return 1;
11415 }
11416 return 0;
11417 }
11418
11419 /* Mark overlay arrows to be updated on next redisplay. */
11420
11421 static void
11422 update_overlay_arrows (int up_to_date)
11423 {
11424 Lisp_Object vlist;
11425
11426 for (vlist = Voverlay_arrow_variable_list;
11427 CONSP (vlist);
11428 vlist = XCDR (vlist))
11429 {
11430 Lisp_Object var = XCAR (vlist);
11431
11432 if (!SYMBOLP (var))
11433 continue;
11434
11435 if (up_to_date > 0)
11436 {
11437 Lisp_Object val = find_symbol_value (var);
11438 Fput (var, Qlast_arrow_position,
11439 COERCE_MARKER (val));
11440 Fput (var, Qlast_arrow_string,
11441 overlay_arrow_string_or_property (var));
11442 }
11443 else if (up_to_date < 0
11444 || !NILP (Fget (var, Qlast_arrow_position)))
11445 {
11446 Fput (var, Qlast_arrow_position, Qt);
11447 Fput (var, Qlast_arrow_string, Qt);
11448 }
11449 }
11450 }
11451
11452
11453 /* Return overlay arrow string to display at row.
11454 Return integer (bitmap number) for arrow bitmap in left fringe.
11455 Return nil if no overlay arrow. */
11456
11457 static Lisp_Object
11458 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11459 {
11460 Lisp_Object vlist;
11461
11462 for (vlist = Voverlay_arrow_variable_list;
11463 CONSP (vlist);
11464 vlist = XCDR (vlist))
11465 {
11466 Lisp_Object var = XCAR (vlist);
11467 Lisp_Object val;
11468
11469 if (!SYMBOLP (var))
11470 continue;
11471
11472 val = find_symbol_value (var);
11473
11474 if (MARKERP (val)
11475 && current_buffer == XMARKER (val)->buffer
11476 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11477 {
11478 if (FRAME_WINDOW_P (it->f)
11479 /* FIXME: if ROW->reversed_p is set, this should test
11480 the right fringe, not the left one. */
11481 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11482 {
11483 #ifdef HAVE_WINDOW_SYSTEM
11484 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11485 {
11486 int fringe_bitmap;
11487 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11488 return make_number (fringe_bitmap);
11489 }
11490 #endif
11491 return make_number (-1); /* Use default arrow bitmap */
11492 }
11493 return overlay_arrow_string_or_property (var);
11494 }
11495 }
11496
11497 return Qnil;
11498 }
11499
11500 /* Return 1 if point moved out of or into a composition. Otherwise
11501 return 0. PREV_BUF and PREV_PT are the last point buffer and
11502 position. BUF and PT are the current point buffer and position. */
11503
11504 int
11505 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11506 struct buffer *buf, EMACS_INT pt)
11507 {
11508 EMACS_INT start, end;
11509 Lisp_Object prop;
11510 Lisp_Object buffer;
11511
11512 XSETBUFFER (buffer, buf);
11513 /* Check a composition at the last point if point moved within the
11514 same buffer. */
11515 if (prev_buf == buf)
11516 {
11517 if (prev_pt == pt)
11518 /* Point didn't move. */
11519 return 0;
11520
11521 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11522 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11523 && COMPOSITION_VALID_P (start, end, prop)
11524 && start < prev_pt && end > prev_pt)
11525 /* The last point was within the composition. Return 1 iff
11526 point moved out of the composition. */
11527 return (pt <= start || pt >= end);
11528 }
11529
11530 /* Check a composition at the current point. */
11531 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11532 && find_composition (pt, -1, &start, &end, &prop, buffer)
11533 && COMPOSITION_VALID_P (start, end, prop)
11534 && start < pt && end > pt);
11535 }
11536
11537
11538 /* Reconsider the setting of B->clip_changed which is displayed
11539 in window W. */
11540
11541 static INLINE void
11542 reconsider_clip_changes (struct window *w, struct buffer *b)
11543 {
11544 if (b->clip_changed
11545 && !NILP (w->window_end_valid)
11546 && w->current_matrix->buffer == b
11547 && w->current_matrix->zv == BUF_ZV (b)
11548 && w->current_matrix->begv == BUF_BEGV (b))
11549 b->clip_changed = 0;
11550
11551 /* If display wasn't paused, and W is not a tool bar window, see if
11552 point has been moved into or out of a composition. In that case,
11553 we set b->clip_changed to 1 to force updating the screen. If
11554 b->clip_changed has already been set to 1, we can skip this
11555 check. */
11556 if (!b->clip_changed
11557 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11558 {
11559 EMACS_INT pt;
11560
11561 if (w == XWINDOW (selected_window))
11562 pt = BUF_PT (current_buffer);
11563 else
11564 pt = marker_position (w->pointm);
11565
11566 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11567 || pt != XINT (w->last_point))
11568 && check_point_in_composition (w->current_matrix->buffer,
11569 XINT (w->last_point),
11570 XBUFFER (w->buffer), pt))
11571 b->clip_changed = 1;
11572 }
11573 }
11574 \f
11575
11576 /* Select FRAME to forward the values of frame-local variables into C
11577 variables so that the redisplay routines can access those values
11578 directly. */
11579
11580 static void
11581 select_frame_for_redisplay (Lisp_Object frame)
11582 {
11583 Lisp_Object tail, tem;
11584 Lisp_Object old = selected_frame;
11585 struct Lisp_Symbol *sym;
11586
11587 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11588
11589 selected_frame = frame;
11590
11591 do {
11592 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11593 if (CONSP (XCAR (tail))
11594 && (tem = XCAR (XCAR (tail)),
11595 SYMBOLP (tem))
11596 && (sym = indirect_variable (XSYMBOL (tem)),
11597 sym->redirect == SYMBOL_LOCALIZED)
11598 && sym->val.blv->frame_local)
11599 /* Use find_symbol_value rather than Fsymbol_value
11600 to avoid an error if it is void. */
11601 find_symbol_value (tem);
11602 } while (!EQ (frame, old) && (frame = old, 1));
11603 }
11604
11605
11606 #define STOP_POLLING \
11607 do { if (! polling_stopped_here) stop_polling (); \
11608 polling_stopped_here = 1; } while (0)
11609
11610 #define RESUME_POLLING \
11611 do { if (polling_stopped_here) start_polling (); \
11612 polling_stopped_here = 0; } while (0)
11613
11614
11615 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11616 response to any user action; therefore, we should preserve the echo
11617 area. (Actually, our caller does that job.) Perhaps in the future
11618 avoid recentering windows if it is not necessary; currently that
11619 causes some problems. */
11620
11621 static void
11622 redisplay_internal (int preserve_echo_area)
11623 {
11624 struct window *w = XWINDOW (selected_window);
11625 struct frame *f;
11626 int pause;
11627 int must_finish = 0;
11628 struct text_pos tlbufpos, tlendpos;
11629 int number_of_visible_frames;
11630 int count, count1;
11631 struct frame *sf;
11632 int polling_stopped_here = 0;
11633 Lisp_Object old_frame = selected_frame;
11634
11635 /* Non-zero means redisplay has to consider all windows on all
11636 frames. Zero means, only selected_window is considered. */
11637 int consider_all_windows_p;
11638
11639 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11640
11641 /* No redisplay if running in batch mode or frame is not yet fully
11642 initialized, or redisplay is explicitly turned off by setting
11643 Vinhibit_redisplay. */
11644 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11645 || !NILP (Vinhibit_redisplay))
11646 return;
11647
11648 /* Don't examine these until after testing Vinhibit_redisplay.
11649 When Emacs is shutting down, perhaps because its connection to
11650 X has dropped, we should not look at them at all. */
11651 f = XFRAME (w->frame);
11652 sf = SELECTED_FRAME ();
11653
11654 if (!f->glyphs_initialized_p)
11655 return;
11656
11657 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11658 if (popup_activated ())
11659 return;
11660 #endif
11661
11662 /* I don't think this happens but let's be paranoid. */
11663 if (redisplaying_p)
11664 return;
11665
11666 /* Record a function that resets redisplaying_p to its old value
11667 when we leave this function. */
11668 count = SPECPDL_INDEX ();
11669 record_unwind_protect (unwind_redisplay,
11670 Fcons (make_number (redisplaying_p), selected_frame));
11671 ++redisplaying_p;
11672 specbind (Qinhibit_free_realized_faces, Qnil);
11673
11674 {
11675 Lisp_Object tail, frame;
11676
11677 FOR_EACH_FRAME (tail, frame)
11678 {
11679 struct frame *f = XFRAME (frame);
11680 f->already_hscrolled_p = 0;
11681 }
11682 }
11683
11684 retry:
11685 if (!EQ (old_frame, selected_frame)
11686 && FRAME_LIVE_P (XFRAME (old_frame)))
11687 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11688 selected_frame and selected_window to be temporarily out-of-sync so
11689 when we come back here via `goto retry', we need to resync because we
11690 may need to run Elisp code (via prepare_menu_bars). */
11691 select_frame_for_redisplay (old_frame);
11692
11693 pause = 0;
11694 reconsider_clip_changes (w, current_buffer);
11695 last_escape_glyph_frame = NULL;
11696 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11697 last_glyphless_glyph_frame = NULL;
11698 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11699
11700 /* If new fonts have been loaded that make a glyph matrix adjustment
11701 necessary, do it. */
11702 if (fonts_changed_p)
11703 {
11704 adjust_glyphs (NULL);
11705 ++windows_or_buffers_changed;
11706 fonts_changed_p = 0;
11707 }
11708
11709 /* If face_change_count is non-zero, init_iterator will free all
11710 realized faces, which includes the faces referenced from current
11711 matrices. So, we can't reuse current matrices in this case. */
11712 if (face_change_count)
11713 ++windows_or_buffers_changed;
11714
11715 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11716 && FRAME_TTY (sf)->previous_frame != sf)
11717 {
11718 /* Since frames on a single ASCII terminal share the same
11719 display area, displaying a different frame means redisplay
11720 the whole thing. */
11721 windows_or_buffers_changed++;
11722 SET_FRAME_GARBAGED (sf);
11723 #ifndef DOS_NT
11724 set_tty_color_mode (FRAME_TTY (sf), sf);
11725 #endif
11726 FRAME_TTY (sf)->previous_frame = sf;
11727 }
11728
11729 /* Set the visible flags for all frames. Do this before checking
11730 for resized or garbaged frames; they want to know if their frames
11731 are visible. See the comment in frame.h for
11732 FRAME_SAMPLE_VISIBILITY. */
11733 {
11734 Lisp_Object tail, frame;
11735
11736 number_of_visible_frames = 0;
11737
11738 FOR_EACH_FRAME (tail, frame)
11739 {
11740 struct frame *f = XFRAME (frame);
11741
11742 FRAME_SAMPLE_VISIBILITY (f);
11743 if (FRAME_VISIBLE_P (f))
11744 ++number_of_visible_frames;
11745 clear_desired_matrices (f);
11746 }
11747 }
11748
11749 /* Notice any pending interrupt request to change frame size. */
11750 do_pending_window_change (1);
11751
11752 /* Clear frames marked as garbaged. */
11753 if (frame_garbaged)
11754 clear_garbaged_frames ();
11755
11756 /* Build menubar and tool-bar items. */
11757 if (NILP (Vmemory_full))
11758 prepare_menu_bars ();
11759
11760 if (windows_or_buffers_changed)
11761 update_mode_lines++;
11762
11763 /* Detect case that we need to write or remove a star in the mode line. */
11764 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11765 {
11766 w->update_mode_line = Qt;
11767 if (buffer_shared > 1)
11768 update_mode_lines++;
11769 }
11770
11771 /* Avoid invocation of point motion hooks by `current_column' below. */
11772 count1 = SPECPDL_INDEX ();
11773 specbind (Qinhibit_point_motion_hooks, Qt);
11774
11775 /* If %c is in the mode line, update it if needed. */
11776 if (!NILP (w->column_number_displayed)
11777 /* This alternative quickly identifies a common case
11778 where no change is needed. */
11779 && !(PT == XFASTINT (w->last_point)
11780 && XFASTINT (w->last_modified) >= MODIFF
11781 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11782 && (XFASTINT (w->column_number_displayed)
11783 != (int) current_column ())) /* iftc */
11784 w->update_mode_line = Qt;
11785
11786 unbind_to (count1, Qnil);
11787
11788 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11789
11790 /* The variable buffer_shared is set in redisplay_window and
11791 indicates that we redisplay a buffer in different windows. See
11792 there. */
11793 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11794 || cursor_type_changed);
11795
11796 /* If specs for an arrow have changed, do thorough redisplay
11797 to ensure we remove any arrow that should no longer exist. */
11798 if (overlay_arrows_changed_p ())
11799 consider_all_windows_p = windows_or_buffers_changed = 1;
11800
11801 /* Normally the message* functions will have already displayed and
11802 updated the echo area, but the frame may have been trashed, or
11803 the update may have been preempted, so display the echo area
11804 again here. Checking message_cleared_p captures the case that
11805 the echo area should be cleared. */
11806 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11807 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11808 || (message_cleared_p
11809 && minibuf_level == 0
11810 /* If the mini-window is currently selected, this means the
11811 echo-area doesn't show through. */
11812 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11813 {
11814 int window_height_changed_p = echo_area_display (0);
11815 must_finish = 1;
11816
11817 /* If we don't display the current message, don't clear the
11818 message_cleared_p flag, because, if we did, we wouldn't clear
11819 the echo area in the next redisplay which doesn't preserve
11820 the echo area. */
11821 if (!display_last_displayed_message_p)
11822 message_cleared_p = 0;
11823
11824 if (fonts_changed_p)
11825 goto retry;
11826 else if (window_height_changed_p)
11827 {
11828 consider_all_windows_p = 1;
11829 ++update_mode_lines;
11830 ++windows_or_buffers_changed;
11831
11832 /* If window configuration was changed, frames may have been
11833 marked garbaged. Clear them or we will experience
11834 surprises wrt scrolling. */
11835 if (frame_garbaged)
11836 clear_garbaged_frames ();
11837 }
11838 }
11839 else if (EQ (selected_window, minibuf_window)
11840 && (current_buffer->clip_changed
11841 || XFASTINT (w->last_modified) < MODIFF
11842 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11843 && resize_mini_window (w, 0))
11844 {
11845 /* Resized active mini-window to fit the size of what it is
11846 showing if its contents might have changed. */
11847 must_finish = 1;
11848 /* FIXME: this causes all frames to be updated, which seems unnecessary
11849 since only the current frame needs to be considered. This function needs
11850 to be rewritten with two variables, consider_all_windows and
11851 consider_all_frames. */
11852 consider_all_windows_p = 1;
11853 ++windows_or_buffers_changed;
11854 ++update_mode_lines;
11855
11856 /* If window configuration was changed, frames may have been
11857 marked garbaged. Clear them or we will experience
11858 surprises wrt scrolling. */
11859 if (frame_garbaged)
11860 clear_garbaged_frames ();
11861 }
11862
11863
11864 /* If showing the region, and mark has changed, we must redisplay
11865 the whole window. The assignment to this_line_start_pos prevents
11866 the optimization directly below this if-statement. */
11867 if (((!NILP (Vtransient_mark_mode)
11868 && !NILP (XBUFFER (w->buffer)->mark_active))
11869 != !NILP (w->region_showing))
11870 || (!NILP (w->region_showing)
11871 && !EQ (w->region_showing,
11872 Fmarker_position (XBUFFER (w->buffer)->mark))))
11873 CHARPOS (this_line_start_pos) = 0;
11874
11875 /* Optimize the case that only the line containing the cursor in the
11876 selected window has changed. Variables starting with this_ are
11877 set in display_line and record information about the line
11878 containing the cursor. */
11879 tlbufpos = this_line_start_pos;
11880 tlendpos = this_line_end_pos;
11881 if (!consider_all_windows_p
11882 && CHARPOS (tlbufpos) > 0
11883 && NILP (w->update_mode_line)
11884 && !current_buffer->clip_changed
11885 && !current_buffer->prevent_redisplay_optimizations_p
11886 && FRAME_VISIBLE_P (XFRAME (w->frame))
11887 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11888 /* Make sure recorded data applies to current buffer, etc. */
11889 && this_line_buffer == current_buffer
11890 && current_buffer == XBUFFER (w->buffer)
11891 && NILP (w->force_start)
11892 && NILP (w->optional_new_start)
11893 /* Point must be on the line that we have info recorded about. */
11894 && PT >= CHARPOS (tlbufpos)
11895 && PT <= Z - CHARPOS (tlendpos)
11896 /* All text outside that line, including its final newline,
11897 must be unchanged. */
11898 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11899 CHARPOS (tlendpos)))
11900 {
11901 if (CHARPOS (tlbufpos) > BEGV
11902 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11903 && (CHARPOS (tlbufpos) == ZV
11904 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11905 /* Former continuation line has disappeared by becoming empty. */
11906 goto cancel;
11907 else if (XFASTINT (w->last_modified) < MODIFF
11908 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11909 || MINI_WINDOW_P (w))
11910 {
11911 /* We have to handle the case of continuation around a
11912 wide-column character (see the comment in indent.c around
11913 line 1340).
11914
11915 For instance, in the following case:
11916
11917 -------- Insert --------
11918 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11919 J_I_ ==> J_I_ `^^' are cursors.
11920 ^^ ^^
11921 -------- --------
11922
11923 As we have to redraw the line above, we cannot use this
11924 optimization. */
11925
11926 struct it it;
11927 int line_height_before = this_line_pixel_height;
11928
11929 /* Note that start_display will handle the case that the
11930 line starting at tlbufpos is a continuation line. */
11931 start_display (&it, w, tlbufpos);
11932
11933 /* Implementation note: It this still necessary? */
11934 if (it.current_x != this_line_start_x)
11935 goto cancel;
11936
11937 TRACE ((stderr, "trying display optimization 1\n"));
11938 w->cursor.vpos = -1;
11939 overlay_arrow_seen = 0;
11940 it.vpos = this_line_vpos;
11941 it.current_y = this_line_y;
11942 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11943 display_line (&it);
11944
11945 /* If line contains point, is not continued,
11946 and ends at same distance from eob as before, we win. */
11947 if (w->cursor.vpos >= 0
11948 /* Line is not continued, otherwise this_line_start_pos
11949 would have been set to 0 in display_line. */
11950 && CHARPOS (this_line_start_pos)
11951 /* Line ends as before. */
11952 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11953 /* Line has same height as before. Otherwise other lines
11954 would have to be shifted up or down. */
11955 && this_line_pixel_height == line_height_before)
11956 {
11957 /* If this is not the window's last line, we must adjust
11958 the charstarts of the lines below. */
11959 if (it.current_y < it.last_visible_y)
11960 {
11961 struct glyph_row *row
11962 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11963 EMACS_INT delta, delta_bytes;
11964
11965 /* We used to distinguish between two cases here,
11966 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11967 when the line ends in a newline or the end of the
11968 buffer's accessible portion. But both cases did
11969 the same, so they were collapsed. */
11970 delta = (Z
11971 - CHARPOS (tlendpos)
11972 - MATRIX_ROW_START_CHARPOS (row));
11973 delta_bytes = (Z_BYTE
11974 - BYTEPOS (tlendpos)
11975 - MATRIX_ROW_START_BYTEPOS (row));
11976
11977 increment_matrix_positions (w->current_matrix,
11978 this_line_vpos + 1,
11979 w->current_matrix->nrows,
11980 delta, delta_bytes);
11981 }
11982
11983 /* If this row displays text now but previously didn't,
11984 or vice versa, w->window_end_vpos may have to be
11985 adjusted. */
11986 if ((it.glyph_row - 1)->displays_text_p)
11987 {
11988 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11989 XSETINT (w->window_end_vpos, this_line_vpos);
11990 }
11991 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11992 && this_line_vpos > 0)
11993 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11994 w->window_end_valid = Qnil;
11995
11996 /* Update hint: No need to try to scroll in update_window. */
11997 w->desired_matrix->no_scrolling_p = 1;
11998
11999 #if GLYPH_DEBUG
12000 *w->desired_matrix->method = 0;
12001 debug_method_add (w, "optimization 1");
12002 #endif
12003 #ifdef HAVE_WINDOW_SYSTEM
12004 update_window_fringes (w, 0);
12005 #endif
12006 goto update;
12007 }
12008 else
12009 goto cancel;
12010 }
12011 else if (/* Cursor position hasn't changed. */
12012 PT == XFASTINT (w->last_point)
12013 /* Make sure the cursor was last displayed
12014 in this window. Otherwise we have to reposition it. */
12015 && 0 <= w->cursor.vpos
12016 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12017 {
12018 if (!must_finish)
12019 {
12020 do_pending_window_change (1);
12021
12022 /* We used to always goto end_of_redisplay here, but this
12023 isn't enough if we have a blinking cursor. */
12024 if (w->cursor_off_p == w->last_cursor_off_p)
12025 goto end_of_redisplay;
12026 }
12027 goto update;
12028 }
12029 /* If highlighting the region, or if the cursor is in the echo area,
12030 then we can't just move the cursor. */
12031 else if (! (!NILP (Vtransient_mark_mode)
12032 && !NILP (current_buffer->mark_active))
12033 && (EQ (selected_window, current_buffer->last_selected_window)
12034 || highlight_nonselected_windows)
12035 && NILP (w->region_showing)
12036 && NILP (Vshow_trailing_whitespace)
12037 && !cursor_in_echo_area)
12038 {
12039 struct it it;
12040 struct glyph_row *row;
12041
12042 /* Skip from tlbufpos to PT and see where it is. Note that
12043 PT may be in invisible text. If so, we will end at the
12044 next visible position. */
12045 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12046 NULL, DEFAULT_FACE_ID);
12047 it.current_x = this_line_start_x;
12048 it.current_y = this_line_y;
12049 it.vpos = this_line_vpos;
12050
12051 /* The call to move_it_to stops in front of PT, but
12052 moves over before-strings. */
12053 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12054
12055 if (it.vpos == this_line_vpos
12056 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12057 row->enabled_p))
12058 {
12059 xassert (this_line_vpos == it.vpos);
12060 xassert (this_line_y == it.current_y);
12061 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12062 #if GLYPH_DEBUG
12063 *w->desired_matrix->method = 0;
12064 debug_method_add (w, "optimization 3");
12065 #endif
12066 goto update;
12067 }
12068 else
12069 goto cancel;
12070 }
12071
12072 cancel:
12073 /* Text changed drastically or point moved off of line. */
12074 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12075 }
12076
12077 CHARPOS (this_line_start_pos) = 0;
12078 consider_all_windows_p |= buffer_shared > 1;
12079 ++clear_face_cache_count;
12080 #ifdef HAVE_WINDOW_SYSTEM
12081 ++clear_image_cache_count;
12082 #endif
12083
12084 /* Build desired matrices, and update the display. If
12085 consider_all_windows_p is non-zero, do it for all windows on all
12086 frames. Otherwise do it for selected_window, only. */
12087
12088 if (consider_all_windows_p)
12089 {
12090 Lisp_Object tail, frame;
12091
12092 FOR_EACH_FRAME (tail, frame)
12093 XFRAME (frame)->updated_p = 0;
12094
12095 /* Recompute # windows showing selected buffer. This will be
12096 incremented each time such a window is displayed. */
12097 buffer_shared = 0;
12098
12099 FOR_EACH_FRAME (tail, frame)
12100 {
12101 struct frame *f = XFRAME (frame);
12102
12103 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12104 {
12105 if (! EQ (frame, selected_frame))
12106 /* Select the frame, for the sake of frame-local
12107 variables. */
12108 select_frame_for_redisplay (frame);
12109
12110 /* Mark all the scroll bars to be removed; we'll redeem
12111 the ones we want when we redisplay their windows. */
12112 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12113 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12114
12115 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12116 redisplay_windows (FRAME_ROOT_WINDOW (f));
12117
12118 /* The X error handler may have deleted that frame. */
12119 if (!FRAME_LIVE_P (f))
12120 continue;
12121
12122 /* Any scroll bars which redisplay_windows should have
12123 nuked should now go away. */
12124 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12125 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12126
12127 /* If fonts changed, display again. */
12128 /* ??? rms: I suspect it is a mistake to jump all the way
12129 back to retry here. It should just retry this frame. */
12130 if (fonts_changed_p)
12131 goto retry;
12132
12133 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12134 {
12135 /* See if we have to hscroll. */
12136 if (!f->already_hscrolled_p)
12137 {
12138 f->already_hscrolled_p = 1;
12139 if (hscroll_windows (f->root_window))
12140 goto retry;
12141 }
12142
12143 /* Prevent various kinds of signals during display
12144 update. stdio is not robust about handling
12145 signals, which can cause an apparent I/O
12146 error. */
12147 if (interrupt_input)
12148 unrequest_sigio ();
12149 STOP_POLLING;
12150
12151 /* Update the display. */
12152 set_window_update_flags (XWINDOW (f->root_window), 1);
12153 pause |= update_frame (f, 0, 0);
12154 f->updated_p = 1;
12155 }
12156 }
12157 }
12158
12159 if (!EQ (old_frame, selected_frame)
12160 && FRAME_LIVE_P (XFRAME (old_frame)))
12161 /* We played a bit fast-and-loose above and allowed selected_frame
12162 and selected_window to be temporarily out-of-sync but let's make
12163 sure this stays contained. */
12164 select_frame_for_redisplay (old_frame);
12165 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12166
12167 if (!pause)
12168 {
12169 /* Do the mark_window_display_accurate after all windows have
12170 been redisplayed because this call resets flags in buffers
12171 which are needed for proper redisplay. */
12172 FOR_EACH_FRAME (tail, frame)
12173 {
12174 struct frame *f = XFRAME (frame);
12175 if (f->updated_p)
12176 {
12177 mark_window_display_accurate (f->root_window, 1);
12178 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12179 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12180 }
12181 }
12182 }
12183 }
12184 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12185 {
12186 Lisp_Object mini_window;
12187 struct frame *mini_frame;
12188
12189 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12190 /* Use list_of_error, not Qerror, so that
12191 we catch only errors and don't run the debugger. */
12192 internal_condition_case_1 (redisplay_window_1, selected_window,
12193 list_of_error,
12194 redisplay_window_error);
12195
12196 /* Compare desired and current matrices, perform output. */
12197
12198 update:
12199 /* If fonts changed, display again. */
12200 if (fonts_changed_p)
12201 goto retry;
12202
12203 /* Prevent various kinds of signals during display update.
12204 stdio is not robust about handling signals,
12205 which can cause an apparent I/O error. */
12206 if (interrupt_input)
12207 unrequest_sigio ();
12208 STOP_POLLING;
12209
12210 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12211 {
12212 if (hscroll_windows (selected_window))
12213 goto retry;
12214
12215 XWINDOW (selected_window)->must_be_updated_p = 1;
12216 pause = update_frame (sf, 0, 0);
12217 }
12218
12219 /* We may have called echo_area_display at the top of this
12220 function. If the echo area is on another frame, that may
12221 have put text on a frame other than the selected one, so the
12222 above call to update_frame would not have caught it. Catch
12223 it here. */
12224 mini_window = FRAME_MINIBUF_WINDOW (sf);
12225 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12226
12227 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12228 {
12229 XWINDOW (mini_window)->must_be_updated_p = 1;
12230 pause |= update_frame (mini_frame, 0, 0);
12231 if (!pause && hscroll_windows (mini_window))
12232 goto retry;
12233 }
12234 }
12235
12236 /* If display was paused because of pending input, make sure we do a
12237 thorough update the next time. */
12238 if (pause)
12239 {
12240 /* Prevent the optimization at the beginning of
12241 redisplay_internal that tries a single-line update of the
12242 line containing the cursor in the selected window. */
12243 CHARPOS (this_line_start_pos) = 0;
12244
12245 /* Let the overlay arrow be updated the next time. */
12246 update_overlay_arrows (0);
12247
12248 /* If we pause after scrolling, some rows in the current
12249 matrices of some windows are not valid. */
12250 if (!WINDOW_FULL_WIDTH_P (w)
12251 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12252 update_mode_lines = 1;
12253 }
12254 else
12255 {
12256 if (!consider_all_windows_p)
12257 {
12258 /* This has already been done above if
12259 consider_all_windows_p is set. */
12260 mark_window_display_accurate_1 (w, 1);
12261
12262 /* Say overlay arrows are up to date. */
12263 update_overlay_arrows (1);
12264
12265 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12266 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12267 }
12268
12269 update_mode_lines = 0;
12270 windows_or_buffers_changed = 0;
12271 cursor_type_changed = 0;
12272 }
12273
12274 /* Start SIGIO interrupts coming again. Having them off during the
12275 code above makes it less likely one will discard output, but not
12276 impossible, since there might be stuff in the system buffer here.
12277 But it is much hairier to try to do anything about that. */
12278 if (interrupt_input)
12279 request_sigio ();
12280 RESUME_POLLING;
12281
12282 /* If a frame has become visible which was not before, redisplay
12283 again, so that we display it. Expose events for such a frame
12284 (which it gets when becoming visible) don't call the parts of
12285 redisplay constructing glyphs, so simply exposing a frame won't
12286 display anything in this case. So, we have to display these
12287 frames here explicitly. */
12288 if (!pause)
12289 {
12290 Lisp_Object tail, frame;
12291 int new_count = 0;
12292
12293 FOR_EACH_FRAME (tail, frame)
12294 {
12295 int this_is_visible = 0;
12296
12297 if (XFRAME (frame)->visible)
12298 this_is_visible = 1;
12299 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12300 if (XFRAME (frame)->visible)
12301 this_is_visible = 1;
12302
12303 if (this_is_visible)
12304 new_count++;
12305 }
12306
12307 if (new_count != number_of_visible_frames)
12308 windows_or_buffers_changed++;
12309 }
12310
12311 /* Change frame size now if a change is pending. */
12312 do_pending_window_change (1);
12313
12314 /* If we just did a pending size change, or have additional
12315 visible frames, redisplay again. */
12316 if (windows_or_buffers_changed && !pause)
12317 goto retry;
12318
12319 /* Clear the face and image caches.
12320
12321 We used to do this only if consider_all_windows_p. But the cache
12322 needs to be cleared if a timer creates images in the current
12323 buffer (e.g. the test case in Bug#6230). */
12324
12325 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12326 {
12327 clear_face_cache (0);
12328 clear_face_cache_count = 0;
12329 }
12330
12331 #ifdef HAVE_WINDOW_SYSTEM
12332 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12333 {
12334 clear_image_caches (Qnil);
12335 clear_image_cache_count = 0;
12336 }
12337 #endif /* HAVE_WINDOW_SYSTEM */
12338
12339 end_of_redisplay:
12340 unbind_to (count, Qnil);
12341 RESUME_POLLING;
12342 }
12343
12344
12345 /* Redisplay, but leave alone any recent echo area message unless
12346 another message has been requested in its place.
12347
12348 This is useful in situations where you need to redisplay but no
12349 user action has occurred, making it inappropriate for the message
12350 area to be cleared. See tracking_off and
12351 wait_reading_process_output for examples of these situations.
12352
12353 FROM_WHERE is an integer saying from where this function was
12354 called. This is useful for debugging. */
12355
12356 void
12357 redisplay_preserve_echo_area (int from_where)
12358 {
12359 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12360
12361 if (!NILP (echo_area_buffer[1]))
12362 {
12363 /* We have a previously displayed message, but no current
12364 message. Redisplay the previous message. */
12365 display_last_displayed_message_p = 1;
12366 redisplay_internal (1);
12367 display_last_displayed_message_p = 0;
12368 }
12369 else
12370 redisplay_internal (1);
12371
12372 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12373 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12374 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12375 }
12376
12377
12378 /* Function registered with record_unwind_protect in
12379 redisplay_internal. Reset redisplaying_p to the value it had
12380 before redisplay_internal was called, and clear
12381 prevent_freeing_realized_faces_p. It also selects the previously
12382 selected frame, unless it has been deleted (by an X connection
12383 failure during redisplay, for example). */
12384
12385 static Lisp_Object
12386 unwind_redisplay (Lisp_Object val)
12387 {
12388 Lisp_Object old_redisplaying_p, old_frame;
12389
12390 old_redisplaying_p = XCAR (val);
12391 redisplaying_p = XFASTINT (old_redisplaying_p);
12392 old_frame = XCDR (val);
12393 if (! EQ (old_frame, selected_frame)
12394 && FRAME_LIVE_P (XFRAME (old_frame)))
12395 select_frame_for_redisplay (old_frame);
12396 return Qnil;
12397 }
12398
12399
12400 /* Mark the display of window W as accurate or inaccurate. If
12401 ACCURATE_P is non-zero mark display of W as accurate. If
12402 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12403 redisplay_internal is called. */
12404
12405 static void
12406 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12407 {
12408 if (BUFFERP (w->buffer))
12409 {
12410 struct buffer *b = XBUFFER (w->buffer);
12411
12412 w->last_modified
12413 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12414 w->last_overlay_modified
12415 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12416 w->last_had_star
12417 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12418
12419 if (accurate_p)
12420 {
12421 b->clip_changed = 0;
12422 b->prevent_redisplay_optimizations_p = 0;
12423
12424 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12425 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12426 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12427 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12428
12429 w->current_matrix->buffer = b;
12430 w->current_matrix->begv = BUF_BEGV (b);
12431 w->current_matrix->zv = BUF_ZV (b);
12432
12433 w->last_cursor = w->cursor;
12434 w->last_cursor_off_p = w->cursor_off_p;
12435
12436 if (w == XWINDOW (selected_window))
12437 w->last_point = make_number (BUF_PT (b));
12438 else
12439 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12440 }
12441 }
12442
12443 if (accurate_p)
12444 {
12445 w->window_end_valid = w->buffer;
12446 w->update_mode_line = Qnil;
12447 }
12448 }
12449
12450
12451 /* Mark the display of windows in the window tree rooted at WINDOW as
12452 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12453 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12454 be redisplayed the next time redisplay_internal is called. */
12455
12456 void
12457 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12458 {
12459 struct window *w;
12460
12461 for (; !NILP (window); window = w->next)
12462 {
12463 w = XWINDOW (window);
12464 mark_window_display_accurate_1 (w, accurate_p);
12465
12466 if (!NILP (w->vchild))
12467 mark_window_display_accurate (w->vchild, accurate_p);
12468 if (!NILP (w->hchild))
12469 mark_window_display_accurate (w->hchild, accurate_p);
12470 }
12471
12472 if (accurate_p)
12473 {
12474 update_overlay_arrows (1);
12475 }
12476 else
12477 {
12478 /* Force a thorough redisplay the next time by setting
12479 last_arrow_position and last_arrow_string to t, which is
12480 unequal to any useful value of Voverlay_arrow_... */
12481 update_overlay_arrows (-1);
12482 }
12483 }
12484
12485
12486 /* Return value in display table DP (Lisp_Char_Table *) for character
12487 C. Since a display table doesn't have any parent, we don't have to
12488 follow parent. Do not call this function directly but use the
12489 macro DISP_CHAR_VECTOR. */
12490
12491 Lisp_Object
12492 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12493 {
12494 Lisp_Object val;
12495
12496 if (ASCII_CHAR_P (c))
12497 {
12498 val = dp->ascii;
12499 if (SUB_CHAR_TABLE_P (val))
12500 val = XSUB_CHAR_TABLE (val)->contents[c];
12501 }
12502 else
12503 {
12504 Lisp_Object table;
12505
12506 XSETCHAR_TABLE (table, dp);
12507 val = char_table_ref (table, c);
12508 }
12509 if (NILP (val))
12510 val = dp->defalt;
12511 return val;
12512 }
12513
12514
12515 \f
12516 /***********************************************************************
12517 Window Redisplay
12518 ***********************************************************************/
12519
12520 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12521
12522 static void
12523 redisplay_windows (Lisp_Object window)
12524 {
12525 while (!NILP (window))
12526 {
12527 struct window *w = XWINDOW (window);
12528
12529 if (!NILP (w->hchild))
12530 redisplay_windows (w->hchild);
12531 else if (!NILP (w->vchild))
12532 redisplay_windows (w->vchild);
12533 else if (!NILP (w->buffer))
12534 {
12535 displayed_buffer = XBUFFER (w->buffer);
12536 /* Use list_of_error, not Qerror, so that
12537 we catch only errors and don't run the debugger. */
12538 internal_condition_case_1 (redisplay_window_0, window,
12539 list_of_error,
12540 redisplay_window_error);
12541 }
12542
12543 window = w->next;
12544 }
12545 }
12546
12547 static Lisp_Object
12548 redisplay_window_error (Lisp_Object ignore)
12549 {
12550 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12551 return Qnil;
12552 }
12553
12554 static Lisp_Object
12555 redisplay_window_0 (Lisp_Object window)
12556 {
12557 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12558 redisplay_window (window, 0);
12559 return Qnil;
12560 }
12561
12562 static Lisp_Object
12563 redisplay_window_1 (Lisp_Object window)
12564 {
12565 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12566 redisplay_window (window, 1);
12567 return Qnil;
12568 }
12569 \f
12570
12571 /* Increment GLYPH until it reaches END or CONDITION fails while
12572 adding (GLYPH)->pixel_width to X. */
12573
12574 #define SKIP_GLYPHS(glyph, end, x, condition) \
12575 do \
12576 { \
12577 (x) += (glyph)->pixel_width; \
12578 ++(glyph); \
12579 } \
12580 while ((glyph) < (end) && (condition))
12581
12582
12583 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12584 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12585 which positions recorded in ROW differ from current buffer
12586 positions.
12587
12588 Return 0 if cursor is not on this row, 1 otherwise. */
12589
12590 int
12591 set_cursor_from_row (struct window *w, struct glyph_row *row,
12592 struct glyph_matrix *matrix,
12593 EMACS_INT delta, EMACS_INT delta_bytes,
12594 int dy, int dvpos)
12595 {
12596 struct glyph *glyph = row->glyphs[TEXT_AREA];
12597 struct glyph *end = glyph + row->used[TEXT_AREA];
12598 struct glyph *cursor = NULL;
12599 /* The last known character position in row. */
12600 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12601 int x = row->x;
12602 EMACS_INT pt_old = PT - delta;
12603 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12604 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12605 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12606 /* A glyph beyond the edge of TEXT_AREA which we should never
12607 touch. */
12608 struct glyph *glyphs_end = end;
12609 /* Non-zero means we've found a match for cursor position, but that
12610 glyph has the avoid_cursor_p flag set. */
12611 int match_with_avoid_cursor = 0;
12612 /* Non-zero means we've seen at least one glyph that came from a
12613 display string. */
12614 int string_seen = 0;
12615 /* Largest and smalles buffer positions seen so far during scan of
12616 glyph row. */
12617 EMACS_INT bpos_max = pos_before;
12618 EMACS_INT bpos_min = pos_after;
12619 /* Last buffer position covered by an overlay string with an integer
12620 `cursor' property. */
12621 EMACS_INT bpos_covered = 0;
12622
12623 /* Skip over glyphs not having an object at the start and the end of
12624 the row. These are special glyphs like truncation marks on
12625 terminal frames. */
12626 if (row->displays_text_p)
12627 {
12628 if (!row->reversed_p)
12629 {
12630 while (glyph < end
12631 && INTEGERP (glyph->object)
12632 && glyph->charpos < 0)
12633 {
12634 x += glyph->pixel_width;
12635 ++glyph;
12636 }
12637 while (end > glyph
12638 && INTEGERP ((end - 1)->object)
12639 /* CHARPOS is zero for blanks and stretch glyphs
12640 inserted by extend_face_to_end_of_line. */
12641 && (end - 1)->charpos <= 0)
12642 --end;
12643 glyph_before = glyph - 1;
12644 glyph_after = end;
12645 }
12646 else
12647 {
12648 struct glyph *g;
12649
12650 /* If the glyph row is reversed, we need to process it from back
12651 to front, so swap the edge pointers. */
12652 glyphs_end = end = glyph - 1;
12653 glyph += row->used[TEXT_AREA] - 1;
12654
12655 while (glyph > end + 1
12656 && INTEGERP (glyph->object)
12657 && glyph->charpos < 0)
12658 {
12659 --glyph;
12660 x -= glyph->pixel_width;
12661 }
12662 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12663 --glyph;
12664 /* By default, in reversed rows we put the cursor on the
12665 rightmost (first in the reading order) glyph. */
12666 for (g = end + 1; g < glyph; g++)
12667 x += g->pixel_width;
12668 while (end < glyph
12669 && INTEGERP ((end + 1)->object)
12670 && (end + 1)->charpos <= 0)
12671 ++end;
12672 glyph_before = glyph + 1;
12673 glyph_after = end;
12674 }
12675 }
12676 else if (row->reversed_p)
12677 {
12678 /* In R2L rows that don't display text, put the cursor on the
12679 rightmost glyph. Case in point: an empty last line that is
12680 part of an R2L paragraph. */
12681 cursor = end - 1;
12682 /* Avoid placing the cursor on the last glyph of the row, where
12683 on terminal frames we hold the vertical border between
12684 adjacent windows. */
12685 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12686 && !WINDOW_RIGHTMOST_P (w)
12687 && cursor == row->glyphs[LAST_AREA] - 1)
12688 cursor--;
12689 x = -1; /* will be computed below, at label compute_x */
12690 }
12691
12692 /* Step 1: Try to find the glyph whose character position
12693 corresponds to point. If that's not possible, find 2 glyphs
12694 whose character positions are the closest to point, one before
12695 point, the other after it. */
12696 if (!row->reversed_p)
12697 while (/* not marched to end of glyph row */
12698 glyph < end
12699 /* glyph was not inserted by redisplay for internal purposes */
12700 && !INTEGERP (glyph->object))
12701 {
12702 if (BUFFERP (glyph->object))
12703 {
12704 EMACS_INT dpos = glyph->charpos - pt_old;
12705
12706 if (glyph->charpos > bpos_max)
12707 bpos_max = glyph->charpos;
12708 if (glyph->charpos < bpos_min)
12709 bpos_min = glyph->charpos;
12710 if (!glyph->avoid_cursor_p)
12711 {
12712 /* If we hit point, we've found the glyph on which to
12713 display the cursor. */
12714 if (dpos == 0)
12715 {
12716 match_with_avoid_cursor = 0;
12717 break;
12718 }
12719 /* See if we've found a better approximation to
12720 POS_BEFORE or to POS_AFTER. Note that we want the
12721 first (leftmost) glyph of all those that are the
12722 closest from below, and the last (rightmost) of all
12723 those from above. */
12724 if (0 > dpos && dpos > pos_before - pt_old)
12725 {
12726 pos_before = glyph->charpos;
12727 glyph_before = glyph;
12728 }
12729 else if (0 < dpos && dpos <= pos_after - pt_old)
12730 {
12731 pos_after = glyph->charpos;
12732 glyph_after = glyph;
12733 }
12734 }
12735 else if (dpos == 0)
12736 match_with_avoid_cursor = 1;
12737 }
12738 else if (STRINGP (glyph->object))
12739 {
12740 Lisp_Object chprop;
12741 EMACS_INT glyph_pos = glyph->charpos;
12742
12743 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12744 glyph->object);
12745 if (INTEGERP (chprop))
12746 {
12747 bpos_covered = bpos_max + XINT (chprop);
12748 /* If the `cursor' property covers buffer positions up
12749 to and including point, we should display cursor on
12750 this glyph. Note that overlays and text properties
12751 with string values stop bidi reordering, so every
12752 buffer position to the left of the string is always
12753 smaller than any position to the right of the
12754 string. Therefore, if a `cursor' property on one
12755 of the string's characters has an integer value, we
12756 will break out of the loop below _before_ we get to
12757 the position match above. IOW, integer values of
12758 the `cursor' property override the "exact match for
12759 point" strategy of positioning the cursor. */
12760 /* Implementation note: bpos_max == pt_old when, e.g.,
12761 we are in an empty line, where bpos_max is set to
12762 MATRIX_ROW_START_CHARPOS, see above. */
12763 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12764 {
12765 cursor = glyph;
12766 break;
12767 }
12768 }
12769
12770 string_seen = 1;
12771 }
12772 x += glyph->pixel_width;
12773 ++glyph;
12774 }
12775 else if (glyph > end) /* row is reversed */
12776 while (!INTEGERP (glyph->object))
12777 {
12778 if (BUFFERP (glyph->object))
12779 {
12780 EMACS_INT dpos = glyph->charpos - pt_old;
12781
12782 if (glyph->charpos > bpos_max)
12783 bpos_max = glyph->charpos;
12784 if (glyph->charpos < bpos_min)
12785 bpos_min = glyph->charpos;
12786 if (!glyph->avoid_cursor_p)
12787 {
12788 if (dpos == 0)
12789 {
12790 match_with_avoid_cursor = 0;
12791 break;
12792 }
12793 if (0 > dpos && dpos > pos_before - pt_old)
12794 {
12795 pos_before = glyph->charpos;
12796 glyph_before = glyph;
12797 }
12798 else if (0 < dpos && dpos <= pos_after - pt_old)
12799 {
12800 pos_after = glyph->charpos;
12801 glyph_after = glyph;
12802 }
12803 }
12804 else if (dpos == 0)
12805 match_with_avoid_cursor = 1;
12806 }
12807 else if (STRINGP (glyph->object))
12808 {
12809 Lisp_Object chprop;
12810 EMACS_INT glyph_pos = glyph->charpos;
12811
12812 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12813 glyph->object);
12814 if (INTEGERP (chprop))
12815 {
12816 bpos_covered = bpos_max + XINT (chprop);
12817 /* If the `cursor' property covers buffer positions up
12818 to and including point, we should display cursor on
12819 this glyph. */
12820 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12821 {
12822 cursor = glyph;
12823 break;
12824 }
12825 }
12826 string_seen = 1;
12827 }
12828 --glyph;
12829 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12830 {
12831 x--; /* can't use any pixel_width */
12832 break;
12833 }
12834 x -= glyph->pixel_width;
12835 }
12836
12837 /* Step 2: If we didn't find an exact match for point, we need to
12838 look for a proper place to put the cursor among glyphs between
12839 GLYPH_BEFORE and GLYPH_AFTER. */
12840 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12841 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12842 && bpos_covered < pt_old)
12843 {
12844 /* An empty line has a single glyph whose OBJECT is zero and
12845 whose CHARPOS is the position of a newline on that line.
12846 Note that on a TTY, there are more glyphs after that, which
12847 were produced by extend_face_to_end_of_line, but their
12848 CHARPOS is zero or negative. */
12849 int empty_line_p =
12850 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12851 && INTEGERP (glyph->object) && glyph->charpos > 0;
12852
12853 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12854 {
12855 EMACS_INT ellipsis_pos;
12856
12857 /* Scan back over the ellipsis glyphs. */
12858 if (!row->reversed_p)
12859 {
12860 ellipsis_pos = (glyph - 1)->charpos;
12861 while (glyph > row->glyphs[TEXT_AREA]
12862 && (glyph - 1)->charpos == ellipsis_pos)
12863 glyph--, x -= glyph->pixel_width;
12864 /* That loop always goes one position too far, including
12865 the glyph before the ellipsis. So scan forward over
12866 that one. */
12867 x += glyph->pixel_width;
12868 glyph++;
12869 }
12870 else /* row is reversed */
12871 {
12872 ellipsis_pos = (glyph + 1)->charpos;
12873 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12874 && (glyph + 1)->charpos == ellipsis_pos)
12875 glyph++, x += glyph->pixel_width;
12876 x -= glyph->pixel_width;
12877 glyph--;
12878 }
12879 }
12880 else if (match_with_avoid_cursor
12881 /* A truncated row may not include PT among its
12882 character positions. Setting the cursor inside the
12883 scroll margin will trigger recalculation of hscroll
12884 in hscroll_window_tree. */
12885 || (row->truncated_on_left_p && pt_old < bpos_min)
12886 || (row->truncated_on_right_p && pt_old > bpos_max)
12887 /* Zero-width characters produce no glyphs. */
12888 || (!string_seen
12889 && !empty_line_p
12890 && (row->reversed_p
12891 ? glyph_after > glyphs_end
12892 : glyph_after < glyphs_end)))
12893 {
12894 cursor = glyph_after;
12895 x = -1;
12896 }
12897 else if (string_seen)
12898 {
12899 int incr = row->reversed_p ? -1 : +1;
12900
12901 /* Need to find the glyph that came out of a string which is
12902 present at point. That glyph is somewhere between
12903 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12904 positioned between POS_BEFORE and POS_AFTER in the
12905 buffer. */
12906 struct glyph *stop = glyph_after;
12907 EMACS_INT pos = pos_before;
12908
12909 x = -1;
12910 for (glyph = glyph_before + incr;
12911 row->reversed_p ? glyph > stop : glyph < stop; )
12912 {
12913
12914 /* Any glyphs that come from the buffer are here because
12915 of bidi reordering. Skip them, and only pay
12916 attention to glyphs that came from some string. */
12917 if (STRINGP (glyph->object))
12918 {
12919 Lisp_Object str;
12920 EMACS_INT tem;
12921
12922 str = glyph->object;
12923 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12924 if (tem == 0 /* from overlay */
12925 || pos <= tem)
12926 {
12927 /* If the string from which this glyph came is
12928 found in the buffer at point, then we've
12929 found the glyph we've been looking for. If
12930 it comes from an overlay (tem == 0), and it
12931 has the `cursor' property on one of its
12932 glyphs, record that glyph as a candidate for
12933 displaying the cursor. (As in the
12934 unidirectional version, we will display the
12935 cursor on the last candidate we find.) */
12936 if (tem == 0 || tem == pt_old)
12937 {
12938 /* The glyphs from this string could have
12939 been reordered. Find the one with the
12940 smallest string position. Or there could
12941 be a character in the string with the
12942 `cursor' property, which means display
12943 cursor on that character's glyph. */
12944 EMACS_INT strpos = glyph->charpos;
12945
12946 if (tem)
12947 cursor = glyph;
12948 for ( ;
12949 (row->reversed_p ? glyph > stop : glyph < stop)
12950 && EQ (glyph->object, str);
12951 glyph += incr)
12952 {
12953 Lisp_Object cprop;
12954 EMACS_INT gpos = glyph->charpos;
12955
12956 cprop = Fget_char_property (make_number (gpos),
12957 Qcursor,
12958 glyph->object);
12959 if (!NILP (cprop))
12960 {
12961 cursor = glyph;
12962 break;
12963 }
12964 if (tem && glyph->charpos < strpos)
12965 {
12966 strpos = glyph->charpos;
12967 cursor = glyph;
12968 }
12969 }
12970
12971 if (tem == pt_old)
12972 goto compute_x;
12973 }
12974 if (tem)
12975 pos = tem + 1; /* don't find previous instances */
12976 }
12977 /* This string is not what we want; skip all of the
12978 glyphs that came from it. */
12979 while ((row->reversed_p ? glyph > stop : glyph < stop)
12980 && EQ (glyph->object, str))
12981 glyph += incr;
12982 }
12983 else
12984 glyph += incr;
12985 }
12986
12987 /* If we reached the end of the line, and END was from a string,
12988 the cursor is not on this line. */
12989 if (cursor == NULL
12990 && (row->reversed_p ? glyph <= end : glyph >= end)
12991 && STRINGP (end->object)
12992 && row->continued_p)
12993 return 0;
12994 }
12995 }
12996
12997 compute_x:
12998 if (cursor != NULL)
12999 glyph = cursor;
13000 if (x < 0)
13001 {
13002 struct glyph *g;
13003
13004 /* Need to compute x that corresponds to GLYPH. */
13005 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13006 {
13007 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13008 abort ();
13009 x += g->pixel_width;
13010 }
13011 }
13012
13013 /* ROW could be part of a continued line, which, under bidi
13014 reordering, might have other rows whose start and end charpos
13015 occlude point. Only set w->cursor if we found a better
13016 approximation to the cursor position than we have from previously
13017 examined candidate rows belonging to the same continued line. */
13018 if (/* we already have a candidate row */
13019 w->cursor.vpos >= 0
13020 /* that candidate is not the row we are processing */
13021 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13022 /* the row we are processing is part of a continued line */
13023 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13024 /* Make sure cursor.vpos specifies a row whose start and end
13025 charpos occlude point. This is because some callers of this
13026 function leave cursor.vpos at the row where the cursor was
13027 displayed during the last redisplay cycle. */
13028 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13029 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13030 {
13031 struct glyph *g1 =
13032 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13033
13034 /* Don't consider glyphs that are outside TEXT_AREA. */
13035 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13036 return 0;
13037 /* Keep the candidate whose buffer position is the closest to
13038 point. */
13039 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13040 w->cursor.hpos >= 0
13041 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13042 && BUFFERP (g1->object)
13043 && (g1->charpos == pt_old /* an exact match always wins */
13044 || (BUFFERP (glyph->object)
13045 && eabs (g1->charpos - pt_old)
13046 < eabs (glyph->charpos - pt_old))))
13047 return 0;
13048 /* If this candidate gives an exact match, use that. */
13049 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13050 /* Otherwise, keep the candidate that comes from a row
13051 spanning less buffer positions. This may win when one or
13052 both candidate positions are on glyphs that came from
13053 display strings, for which we cannot compare buffer
13054 positions. */
13055 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13056 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13057 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13058 return 0;
13059 }
13060 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13061 w->cursor.x = x;
13062 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13063 w->cursor.y = row->y + dy;
13064
13065 if (w == XWINDOW (selected_window))
13066 {
13067 if (!row->continued_p
13068 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13069 && row->x == 0)
13070 {
13071 this_line_buffer = XBUFFER (w->buffer);
13072
13073 CHARPOS (this_line_start_pos)
13074 = MATRIX_ROW_START_CHARPOS (row) + delta;
13075 BYTEPOS (this_line_start_pos)
13076 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13077
13078 CHARPOS (this_line_end_pos)
13079 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13080 BYTEPOS (this_line_end_pos)
13081 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13082
13083 this_line_y = w->cursor.y;
13084 this_line_pixel_height = row->height;
13085 this_line_vpos = w->cursor.vpos;
13086 this_line_start_x = row->x;
13087 }
13088 else
13089 CHARPOS (this_line_start_pos) = 0;
13090 }
13091
13092 return 1;
13093 }
13094
13095
13096 /* Run window scroll functions, if any, for WINDOW with new window
13097 start STARTP. Sets the window start of WINDOW to that position.
13098
13099 We assume that the window's buffer is really current. */
13100
13101 static INLINE struct text_pos
13102 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13103 {
13104 struct window *w = XWINDOW (window);
13105 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13106
13107 if (current_buffer != XBUFFER (w->buffer))
13108 abort ();
13109
13110 if (!NILP (Vwindow_scroll_functions))
13111 {
13112 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13113 make_number (CHARPOS (startp)));
13114 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13115 /* In case the hook functions switch buffers. */
13116 if (current_buffer != XBUFFER (w->buffer))
13117 set_buffer_internal_1 (XBUFFER (w->buffer));
13118 }
13119
13120 return startp;
13121 }
13122
13123
13124 /* Make sure the line containing the cursor is fully visible.
13125 A value of 1 means there is nothing to be done.
13126 (Either the line is fully visible, or it cannot be made so,
13127 or we cannot tell.)
13128
13129 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13130 is higher than window.
13131
13132 A value of 0 means the caller should do scrolling
13133 as if point had gone off the screen. */
13134
13135 static int
13136 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13137 {
13138 struct glyph_matrix *matrix;
13139 struct glyph_row *row;
13140 int window_height;
13141
13142 if (!make_cursor_line_fully_visible_p)
13143 return 1;
13144
13145 /* It's not always possible to find the cursor, e.g, when a window
13146 is full of overlay strings. Don't do anything in that case. */
13147 if (w->cursor.vpos < 0)
13148 return 1;
13149
13150 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13151 row = MATRIX_ROW (matrix, w->cursor.vpos);
13152
13153 /* If the cursor row is not partially visible, there's nothing to do. */
13154 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13155 return 1;
13156
13157 /* If the row the cursor is in is taller than the window's height,
13158 it's not clear what to do, so do nothing. */
13159 window_height = window_box_height (w);
13160 if (row->height >= window_height)
13161 {
13162 if (!force_p || MINI_WINDOW_P (w)
13163 || w->vscroll || w->cursor.vpos == 0)
13164 return 1;
13165 }
13166 return 0;
13167 }
13168
13169
13170 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13171 non-zero means only WINDOW is redisplayed in redisplay_internal.
13172 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13173 in redisplay_window to bring a partially visible line into view in
13174 the case that only the cursor has moved.
13175
13176 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13177 last screen line's vertical height extends past the end of the screen.
13178
13179 Value is
13180
13181 1 if scrolling succeeded
13182
13183 0 if scrolling didn't find point.
13184
13185 -1 if new fonts have been loaded so that we must interrupt
13186 redisplay, adjust glyph matrices, and try again. */
13187
13188 enum
13189 {
13190 SCROLLING_SUCCESS,
13191 SCROLLING_FAILED,
13192 SCROLLING_NEED_LARGER_MATRICES
13193 };
13194
13195 static int
13196 try_scrolling (Lisp_Object window, int just_this_one_p,
13197 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13198 int temp_scroll_step, int last_line_misfit)
13199 {
13200 struct window *w = XWINDOW (window);
13201 struct frame *f = XFRAME (w->frame);
13202 struct text_pos pos, startp;
13203 struct it it;
13204 int this_scroll_margin, scroll_max, rc, height;
13205 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13206 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13207 Lisp_Object aggressive;
13208 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13209
13210 #if GLYPH_DEBUG
13211 debug_method_add (w, "try_scrolling");
13212 #endif
13213
13214 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13215
13216 /* Compute scroll margin height in pixels. We scroll when point is
13217 within this distance from the top or bottom of the window. */
13218 if (scroll_margin > 0)
13219 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13220 * FRAME_LINE_HEIGHT (f);
13221 else
13222 this_scroll_margin = 0;
13223
13224 /* Force scroll_conservatively to have a reasonable value, to avoid
13225 overflow while computing how much to scroll. Note that the user
13226 can supply scroll-conservatively equal to `most-positive-fixnum',
13227 which can be larger than INT_MAX. */
13228 if (scroll_conservatively > scroll_limit)
13229 {
13230 scroll_conservatively = scroll_limit;
13231 scroll_max = INT_MAX;
13232 }
13233 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13234 /* Compute how much we should try to scroll maximally to bring
13235 point into view. */
13236 scroll_max = (max (scroll_step,
13237 max (scroll_conservatively, temp_scroll_step))
13238 * FRAME_LINE_HEIGHT (f));
13239 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13240 || NUMBERP (current_buffer->scroll_up_aggressively))
13241 /* We're trying to scroll because of aggressive scrolling but no
13242 scroll_step is set. Choose an arbitrary one. */
13243 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13244 else
13245 scroll_max = 0;
13246
13247 too_near_end:
13248
13249 /* Decide whether to scroll down. */
13250 if (PT > CHARPOS (startp))
13251 {
13252 int scroll_margin_y;
13253
13254 /* Compute the pixel ypos of the scroll margin, then move it to
13255 either that ypos or PT, whichever comes first. */
13256 start_display (&it, w, startp);
13257 scroll_margin_y = it.last_visible_y - this_scroll_margin
13258 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13259 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13260 (MOVE_TO_POS | MOVE_TO_Y));
13261
13262 if (PT > CHARPOS (it.current.pos))
13263 {
13264 int y0 = line_bottom_y (&it);
13265 /* Compute how many pixels below window bottom to stop searching
13266 for PT. This avoids costly search for PT that is far away if
13267 the user limited scrolling by a small number of lines, but
13268 always finds PT if scroll_conservatively is set to a large
13269 number, such as most-positive-fixnum. */
13270 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13271 int y_to_move =
13272 slack >= INT_MAX - it.last_visible_y
13273 ? INT_MAX
13274 : it.last_visible_y + slack;
13275
13276 /* Compute the distance from the scroll margin to PT or to
13277 the scroll limit, whichever comes first. This should
13278 include the height of the cursor line, to make that line
13279 fully visible. */
13280 move_it_to (&it, PT, -1, y_to_move,
13281 -1, MOVE_TO_POS | MOVE_TO_Y);
13282 dy = line_bottom_y (&it) - y0;
13283
13284 if (dy > scroll_max)
13285 return SCROLLING_FAILED;
13286
13287 scroll_down_p = 1;
13288 }
13289 }
13290
13291 if (scroll_down_p)
13292 {
13293 /* Point is in or below the bottom scroll margin, so move the
13294 window start down. If scrolling conservatively, move it just
13295 enough down to make point visible. If scroll_step is set,
13296 move it down by scroll_step. */
13297 if (scroll_conservatively)
13298 amount_to_scroll
13299 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13300 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13301 else if (scroll_step || temp_scroll_step)
13302 amount_to_scroll = scroll_max;
13303 else
13304 {
13305 aggressive = current_buffer->scroll_up_aggressively;
13306 height = WINDOW_BOX_TEXT_HEIGHT (w);
13307 if (NUMBERP (aggressive))
13308 {
13309 double float_amount = XFLOATINT (aggressive) * height;
13310 amount_to_scroll = float_amount;
13311 if (amount_to_scroll == 0 && float_amount > 0)
13312 amount_to_scroll = 1;
13313 }
13314 }
13315
13316 if (amount_to_scroll <= 0)
13317 return SCROLLING_FAILED;
13318
13319 start_display (&it, w, startp);
13320 if (scroll_max < INT_MAX)
13321 move_it_vertically (&it, amount_to_scroll);
13322 else
13323 {
13324 /* Extra precision for users who set scroll-conservatively
13325 to most-positive-fixnum: make sure the amount we scroll
13326 the window start is never less than amount_to_scroll,
13327 which was computed as distance from window bottom to
13328 point. This matters when lines at window top and lines
13329 below window bottom have different height. */
13330 struct it it1 = it;
13331 /* We use a temporary it1 because line_bottom_y can modify
13332 its argument, if it moves one line down; see there. */
13333 int start_y = line_bottom_y (&it1);
13334
13335 do {
13336 move_it_by_lines (&it, 1, 1);
13337 it1 = it;
13338 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13339 }
13340
13341 /* If STARTP is unchanged, move it down another screen line. */
13342 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13343 move_it_by_lines (&it, 1, 1);
13344 startp = it.current.pos;
13345 }
13346 else
13347 {
13348 struct text_pos scroll_margin_pos = startp;
13349
13350 /* See if point is inside the scroll margin at the top of the
13351 window. */
13352 if (this_scroll_margin)
13353 {
13354 start_display (&it, w, startp);
13355 move_it_vertically (&it, this_scroll_margin);
13356 scroll_margin_pos = it.current.pos;
13357 }
13358
13359 if (PT < CHARPOS (scroll_margin_pos))
13360 {
13361 /* Point is in the scroll margin at the top of the window or
13362 above what is displayed in the window. */
13363 int y0;
13364
13365 /* Compute the vertical distance from PT to the scroll
13366 margin position. Give up if distance is greater than
13367 scroll_max. */
13368 SET_TEXT_POS (pos, PT, PT_BYTE);
13369 start_display (&it, w, pos);
13370 y0 = it.current_y;
13371 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13372 it.last_visible_y, -1,
13373 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13374 dy = it.current_y - y0;
13375 if (dy > scroll_max)
13376 return SCROLLING_FAILED;
13377
13378 /* Compute new window start. */
13379 start_display (&it, w, startp);
13380
13381 if (scroll_conservatively)
13382 amount_to_scroll
13383 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13384 else if (scroll_step || temp_scroll_step)
13385 amount_to_scroll = scroll_max;
13386 else
13387 {
13388 aggressive = current_buffer->scroll_down_aggressively;
13389 height = WINDOW_BOX_TEXT_HEIGHT (w);
13390 if (NUMBERP (aggressive))
13391 {
13392 double float_amount = XFLOATINT (aggressive) * height;
13393 amount_to_scroll = float_amount;
13394 if (amount_to_scroll == 0 && float_amount > 0)
13395 amount_to_scroll = 1;
13396 }
13397 }
13398
13399 if (amount_to_scroll <= 0)
13400 return SCROLLING_FAILED;
13401
13402 move_it_vertically_backward (&it, amount_to_scroll);
13403 startp = it.current.pos;
13404 }
13405 }
13406
13407 /* Run window scroll functions. */
13408 startp = run_window_scroll_functions (window, startp);
13409
13410 /* Display the window. Give up if new fonts are loaded, or if point
13411 doesn't appear. */
13412 if (!try_window (window, startp, 0))
13413 rc = SCROLLING_NEED_LARGER_MATRICES;
13414 else if (w->cursor.vpos < 0)
13415 {
13416 clear_glyph_matrix (w->desired_matrix);
13417 rc = SCROLLING_FAILED;
13418 }
13419 else
13420 {
13421 /* Maybe forget recorded base line for line number display. */
13422 if (!just_this_one_p
13423 || current_buffer->clip_changed
13424 || BEG_UNCHANGED < CHARPOS (startp))
13425 w->base_line_number = Qnil;
13426
13427 /* If cursor ends up on a partially visible line,
13428 treat that as being off the bottom of the screen. */
13429 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13430 {
13431 clear_glyph_matrix (w->desired_matrix);
13432 ++extra_scroll_margin_lines;
13433 goto too_near_end;
13434 }
13435 rc = SCROLLING_SUCCESS;
13436 }
13437
13438 return rc;
13439 }
13440
13441
13442 /* Compute a suitable window start for window W if display of W starts
13443 on a continuation line. Value is non-zero if a new window start
13444 was computed.
13445
13446 The new window start will be computed, based on W's width, starting
13447 from the start of the continued line. It is the start of the
13448 screen line with the minimum distance from the old start W->start. */
13449
13450 static int
13451 compute_window_start_on_continuation_line (struct window *w)
13452 {
13453 struct text_pos pos, start_pos;
13454 int window_start_changed_p = 0;
13455
13456 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13457
13458 /* If window start is on a continuation line... Window start may be
13459 < BEGV in case there's invisible text at the start of the
13460 buffer (M-x rmail, for example). */
13461 if (CHARPOS (start_pos) > BEGV
13462 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13463 {
13464 struct it it;
13465 struct glyph_row *row;
13466
13467 /* Handle the case that the window start is out of range. */
13468 if (CHARPOS (start_pos) < BEGV)
13469 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13470 else if (CHARPOS (start_pos) > ZV)
13471 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13472
13473 /* Find the start of the continued line. This should be fast
13474 because scan_buffer is fast (newline cache). */
13475 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13476 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13477 row, DEFAULT_FACE_ID);
13478 reseat_at_previous_visible_line_start (&it);
13479
13480 /* If the line start is "too far" away from the window start,
13481 say it takes too much time to compute a new window start. */
13482 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13483 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13484 {
13485 int min_distance, distance;
13486
13487 /* Move forward by display lines to find the new window
13488 start. If window width was enlarged, the new start can
13489 be expected to be > the old start. If window width was
13490 decreased, the new window start will be < the old start.
13491 So, we're looking for the display line start with the
13492 minimum distance from the old window start. */
13493 pos = it.current.pos;
13494 min_distance = INFINITY;
13495 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13496 distance < min_distance)
13497 {
13498 min_distance = distance;
13499 pos = it.current.pos;
13500 move_it_by_lines (&it, 1, 0);
13501 }
13502
13503 /* Set the window start there. */
13504 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13505 window_start_changed_p = 1;
13506 }
13507 }
13508
13509 return window_start_changed_p;
13510 }
13511
13512
13513 /* Try cursor movement in case text has not changed in window WINDOW,
13514 with window start STARTP. Value is
13515
13516 CURSOR_MOVEMENT_SUCCESS if successful
13517
13518 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13519
13520 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13521 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13522 we want to scroll as if scroll-step were set to 1. See the code.
13523
13524 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13525 which case we have to abort this redisplay, and adjust matrices
13526 first. */
13527
13528 enum
13529 {
13530 CURSOR_MOVEMENT_SUCCESS,
13531 CURSOR_MOVEMENT_CANNOT_BE_USED,
13532 CURSOR_MOVEMENT_MUST_SCROLL,
13533 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13534 };
13535
13536 static int
13537 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13538 {
13539 struct window *w = XWINDOW (window);
13540 struct frame *f = XFRAME (w->frame);
13541 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13542
13543 #if GLYPH_DEBUG
13544 if (inhibit_try_cursor_movement)
13545 return rc;
13546 #endif
13547
13548 /* Handle case where text has not changed, only point, and it has
13549 not moved off the frame. */
13550 if (/* Point may be in this window. */
13551 PT >= CHARPOS (startp)
13552 /* Selective display hasn't changed. */
13553 && !current_buffer->clip_changed
13554 /* Function force-mode-line-update is used to force a thorough
13555 redisplay. It sets either windows_or_buffers_changed or
13556 update_mode_lines. So don't take a shortcut here for these
13557 cases. */
13558 && !update_mode_lines
13559 && !windows_or_buffers_changed
13560 && !cursor_type_changed
13561 /* Can't use this case if highlighting a region. When a
13562 region exists, cursor movement has to do more than just
13563 set the cursor. */
13564 && !(!NILP (Vtransient_mark_mode)
13565 && !NILP (current_buffer->mark_active))
13566 && NILP (w->region_showing)
13567 && NILP (Vshow_trailing_whitespace)
13568 /* Right after splitting windows, last_point may be nil. */
13569 && INTEGERP (w->last_point)
13570 /* This code is not used for mini-buffer for the sake of the case
13571 of redisplaying to replace an echo area message; since in
13572 that case the mini-buffer contents per se are usually
13573 unchanged. This code is of no real use in the mini-buffer
13574 since the handling of this_line_start_pos, etc., in redisplay
13575 handles the same cases. */
13576 && !EQ (window, minibuf_window)
13577 /* When splitting windows or for new windows, it happens that
13578 redisplay is called with a nil window_end_vpos or one being
13579 larger than the window. This should really be fixed in
13580 window.c. I don't have this on my list, now, so we do
13581 approximately the same as the old redisplay code. --gerd. */
13582 && INTEGERP (w->window_end_vpos)
13583 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13584 && (FRAME_WINDOW_P (f)
13585 || !overlay_arrow_in_current_buffer_p ()))
13586 {
13587 int this_scroll_margin, top_scroll_margin;
13588 struct glyph_row *row = NULL;
13589
13590 #if GLYPH_DEBUG
13591 debug_method_add (w, "cursor movement");
13592 #endif
13593
13594 /* Scroll if point within this distance from the top or bottom
13595 of the window. This is a pixel value. */
13596 if (scroll_margin > 0)
13597 {
13598 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13599 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13600 }
13601 else
13602 this_scroll_margin = 0;
13603
13604 top_scroll_margin = this_scroll_margin;
13605 if (WINDOW_WANTS_HEADER_LINE_P (w))
13606 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13607
13608 /* Start with the row the cursor was displayed during the last
13609 not paused redisplay. Give up if that row is not valid. */
13610 if (w->last_cursor.vpos < 0
13611 || w->last_cursor.vpos >= w->current_matrix->nrows)
13612 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13613 else
13614 {
13615 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13616 if (row->mode_line_p)
13617 ++row;
13618 if (!row->enabled_p)
13619 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13620 }
13621
13622 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13623 {
13624 int scroll_p = 0, must_scroll = 0;
13625 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13626
13627 if (PT > XFASTINT (w->last_point))
13628 {
13629 /* Point has moved forward. */
13630 while (MATRIX_ROW_END_CHARPOS (row) < PT
13631 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13632 {
13633 xassert (row->enabled_p);
13634 ++row;
13635 }
13636
13637 /* If the end position of a row equals the start
13638 position of the next row, and PT is at that position,
13639 we would rather display cursor in the next line. */
13640 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13641 && MATRIX_ROW_END_CHARPOS (row) == PT
13642 && row < w->current_matrix->rows
13643 + w->current_matrix->nrows - 1
13644 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13645 && !cursor_row_p (w, row))
13646 ++row;
13647
13648 /* If within the scroll margin, scroll. Note that
13649 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13650 the next line would be drawn, and that
13651 this_scroll_margin can be zero. */
13652 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13653 || PT > MATRIX_ROW_END_CHARPOS (row)
13654 /* Line is completely visible last line in window
13655 and PT is to be set in the next line. */
13656 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13657 && PT == MATRIX_ROW_END_CHARPOS (row)
13658 && !row->ends_at_zv_p
13659 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13660 scroll_p = 1;
13661 }
13662 else if (PT < XFASTINT (w->last_point))
13663 {
13664 /* Cursor has to be moved backward. Note that PT >=
13665 CHARPOS (startp) because of the outer if-statement. */
13666 while (!row->mode_line_p
13667 && (MATRIX_ROW_START_CHARPOS (row) > PT
13668 || (MATRIX_ROW_START_CHARPOS (row) == PT
13669 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13670 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13671 row > w->current_matrix->rows
13672 && (row-1)->ends_in_newline_from_string_p))))
13673 && (row->y > top_scroll_margin
13674 || CHARPOS (startp) == BEGV))
13675 {
13676 xassert (row->enabled_p);
13677 --row;
13678 }
13679
13680 /* Consider the following case: Window starts at BEGV,
13681 there is invisible, intangible text at BEGV, so that
13682 display starts at some point START > BEGV. It can
13683 happen that we are called with PT somewhere between
13684 BEGV and START. Try to handle that case. */
13685 if (row < w->current_matrix->rows
13686 || row->mode_line_p)
13687 {
13688 row = w->current_matrix->rows;
13689 if (row->mode_line_p)
13690 ++row;
13691 }
13692
13693 /* Due to newlines in overlay strings, we may have to
13694 skip forward over overlay strings. */
13695 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13696 && MATRIX_ROW_END_CHARPOS (row) == PT
13697 && !cursor_row_p (w, row))
13698 ++row;
13699
13700 /* If within the scroll margin, scroll. */
13701 if (row->y < top_scroll_margin
13702 && CHARPOS (startp) != BEGV)
13703 scroll_p = 1;
13704 }
13705 else
13706 {
13707 /* Cursor did not move. So don't scroll even if cursor line
13708 is partially visible, as it was so before. */
13709 rc = CURSOR_MOVEMENT_SUCCESS;
13710 }
13711
13712 if (PT < MATRIX_ROW_START_CHARPOS (row)
13713 || PT > MATRIX_ROW_END_CHARPOS (row))
13714 {
13715 /* if PT is not in the glyph row, give up. */
13716 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13717 must_scroll = 1;
13718 }
13719 else if (rc != CURSOR_MOVEMENT_SUCCESS
13720 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13721 {
13722 /* If rows are bidi-reordered and point moved, back up
13723 until we find a row that does not belong to a
13724 continuation line. This is because we must consider
13725 all rows of a continued line as candidates for the
13726 new cursor positioning, since row start and end
13727 positions change non-linearly with vertical position
13728 in such rows. */
13729 /* FIXME: Revisit this when glyph ``spilling'' in
13730 continuation lines' rows is implemented for
13731 bidi-reordered rows. */
13732 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13733 {
13734 xassert (row->enabled_p);
13735 --row;
13736 /* If we hit the beginning of the displayed portion
13737 without finding the first row of a continued
13738 line, give up. */
13739 if (row <= w->current_matrix->rows)
13740 {
13741 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13742 break;
13743 }
13744
13745 }
13746 }
13747 if (must_scroll)
13748 ;
13749 else if (rc != CURSOR_MOVEMENT_SUCCESS
13750 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13751 && make_cursor_line_fully_visible_p)
13752 {
13753 if (PT == MATRIX_ROW_END_CHARPOS (row)
13754 && !row->ends_at_zv_p
13755 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13756 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13757 else if (row->height > window_box_height (w))
13758 {
13759 /* If we end up in a partially visible line, let's
13760 make it fully visible, except when it's taller
13761 than the window, in which case we can't do much
13762 about it. */
13763 *scroll_step = 1;
13764 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13765 }
13766 else
13767 {
13768 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13769 if (!cursor_row_fully_visible_p (w, 0, 1))
13770 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13771 else
13772 rc = CURSOR_MOVEMENT_SUCCESS;
13773 }
13774 }
13775 else if (scroll_p)
13776 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13777 else if (rc != CURSOR_MOVEMENT_SUCCESS
13778 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13779 {
13780 /* With bidi-reordered rows, there could be more than
13781 one candidate row whose start and end positions
13782 occlude point. We need to let set_cursor_from_row
13783 find the best candidate. */
13784 /* FIXME: Revisit this when glyph ``spilling'' in
13785 continuation lines' rows is implemented for
13786 bidi-reordered rows. */
13787 int rv = 0;
13788
13789 do
13790 {
13791 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13792 && PT <= MATRIX_ROW_END_CHARPOS (row)
13793 && cursor_row_p (w, row))
13794 rv |= set_cursor_from_row (w, row, w->current_matrix,
13795 0, 0, 0, 0);
13796 /* As soon as we've found the first suitable row
13797 whose ends_at_zv_p flag is set, we are done. */
13798 if (rv
13799 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13800 {
13801 rc = CURSOR_MOVEMENT_SUCCESS;
13802 break;
13803 }
13804 ++row;
13805 }
13806 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13807 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13808 || (MATRIX_ROW_START_CHARPOS (row) == PT
13809 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13810 /* If we didn't find any candidate rows, or exited the
13811 loop before all the candidates were examined, signal
13812 to the caller that this method failed. */
13813 if (rc != CURSOR_MOVEMENT_SUCCESS
13814 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13815 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13816 else if (rv)
13817 rc = CURSOR_MOVEMENT_SUCCESS;
13818 }
13819 else
13820 {
13821 do
13822 {
13823 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13824 {
13825 rc = CURSOR_MOVEMENT_SUCCESS;
13826 break;
13827 }
13828 ++row;
13829 }
13830 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13831 && MATRIX_ROW_START_CHARPOS (row) == PT
13832 && cursor_row_p (w, row));
13833 }
13834 }
13835 }
13836
13837 return rc;
13838 }
13839
13840 void
13841 set_vertical_scroll_bar (struct window *w)
13842 {
13843 EMACS_INT start, end, whole;
13844
13845 /* Calculate the start and end positions for the current window.
13846 At some point, it would be nice to choose between scrollbars
13847 which reflect the whole buffer size, with special markers
13848 indicating narrowing, and scrollbars which reflect only the
13849 visible region.
13850
13851 Note that mini-buffers sometimes aren't displaying any text. */
13852 if (!MINI_WINDOW_P (w)
13853 || (w == XWINDOW (minibuf_window)
13854 && NILP (echo_area_buffer[0])))
13855 {
13856 struct buffer *buf = XBUFFER (w->buffer);
13857 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13858 start = marker_position (w->start) - BUF_BEGV (buf);
13859 /* I don't think this is guaranteed to be right. For the
13860 moment, we'll pretend it is. */
13861 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13862
13863 if (end < start)
13864 end = start;
13865 if (whole < (end - start))
13866 whole = end - start;
13867 }
13868 else
13869 start = end = whole = 0;
13870
13871 /* Indicate what this scroll bar ought to be displaying now. */
13872 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13873 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13874 (w, end - start, whole, start);
13875 }
13876
13877
13878 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13879 selected_window is redisplayed.
13880
13881 We can return without actually redisplaying the window if
13882 fonts_changed_p is nonzero. In that case, redisplay_internal will
13883 retry. */
13884
13885 static void
13886 redisplay_window (Lisp_Object window, int just_this_one_p)
13887 {
13888 struct window *w = XWINDOW (window);
13889 struct frame *f = XFRAME (w->frame);
13890 struct buffer *buffer = XBUFFER (w->buffer);
13891 struct buffer *old = current_buffer;
13892 struct text_pos lpoint, opoint, startp;
13893 int update_mode_line;
13894 int tem;
13895 struct it it;
13896 /* Record it now because it's overwritten. */
13897 int current_matrix_up_to_date_p = 0;
13898 int used_current_matrix_p = 0;
13899 /* This is less strict than current_matrix_up_to_date_p.
13900 It indictes that the buffer contents and narrowing are unchanged. */
13901 int buffer_unchanged_p = 0;
13902 int temp_scroll_step = 0;
13903 int count = SPECPDL_INDEX ();
13904 int rc;
13905 int centering_position = -1;
13906 int last_line_misfit = 0;
13907 EMACS_INT beg_unchanged, end_unchanged;
13908
13909 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13910 opoint = lpoint;
13911
13912 /* W must be a leaf window here. */
13913 xassert (!NILP (w->buffer));
13914 #if GLYPH_DEBUG
13915 *w->desired_matrix->method = 0;
13916 #endif
13917
13918 restart:
13919 reconsider_clip_changes (w, buffer);
13920
13921 /* Has the mode line to be updated? */
13922 update_mode_line = (!NILP (w->update_mode_line)
13923 || update_mode_lines
13924 || buffer->clip_changed
13925 || buffer->prevent_redisplay_optimizations_p);
13926
13927 if (MINI_WINDOW_P (w))
13928 {
13929 if (w == XWINDOW (echo_area_window)
13930 && !NILP (echo_area_buffer[0]))
13931 {
13932 if (update_mode_line)
13933 /* We may have to update a tty frame's menu bar or a
13934 tool-bar. Example `M-x C-h C-h C-g'. */
13935 goto finish_menu_bars;
13936 else
13937 /* We've already displayed the echo area glyphs in this window. */
13938 goto finish_scroll_bars;
13939 }
13940 else if ((w != XWINDOW (minibuf_window)
13941 || minibuf_level == 0)
13942 /* When buffer is nonempty, redisplay window normally. */
13943 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13944 /* Quail displays non-mini buffers in minibuffer window.
13945 In that case, redisplay the window normally. */
13946 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13947 {
13948 /* W is a mini-buffer window, but it's not active, so clear
13949 it. */
13950 int yb = window_text_bottom_y (w);
13951 struct glyph_row *row;
13952 int y;
13953
13954 for (y = 0, row = w->desired_matrix->rows;
13955 y < yb;
13956 y += row->height, ++row)
13957 blank_row (w, row, y);
13958 goto finish_scroll_bars;
13959 }
13960
13961 clear_glyph_matrix (w->desired_matrix);
13962 }
13963
13964 /* Otherwise set up data on this window; select its buffer and point
13965 value. */
13966 /* Really select the buffer, for the sake of buffer-local
13967 variables. */
13968 set_buffer_internal_1 (XBUFFER (w->buffer));
13969
13970 current_matrix_up_to_date_p
13971 = (!NILP (w->window_end_valid)
13972 && !current_buffer->clip_changed
13973 && !current_buffer->prevent_redisplay_optimizations_p
13974 && XFASTINT (w->last_modified) >= MODIFF
13975 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13976
13977 /* Run the window-bottom-change-functions
13978 if it is possible that the text on the screen has changed
13979 (either due to modification of the text, or any other reason). */
13980 if (!current_matrix_up_to_date_p
13981 && !NILP (Vwindow_text_change_functions))
13982 {
13983 safe_run_hooks (Qwindow_text_change_functions);
13984 goto restart;
13985 }
13986
13987 beg_unchanged = BEG_UNCHANGED;
13988 end_unchanged = END_UNCHANGED;
13989
13990 SET_TEXT_POS (opoint, PT, PT_BYTE);
13991
13992 specbind (Qinhibit_point_motion_hooks, Qt);
13993
13994 buffer_unchanged_p
13995 = (!NILP (w->window_end_valid)
13996 && !current_buffer->clip_changed
13997 && XFASTINT (w->last_modified) >= MODIFF
13998 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13999
14000 /* When windows_or_buffers_changed is non-zero, we can't rely on
14001 the window end being valid, so set it to nil there. */
14002 if (windows_or_buffers_changed)
14003 {
14004 /* If window starts on a continuation line, maybe adjust the
14005 window start in case the window's width changed. */
14006 if (XMARKER (w->start)->buffer == current_buffer)
14007 compute_window_start_on_continuation_line (w);
14008
14009 w->window_end_valid = Qnil;
14010 }
14011
14012 /* Some sanity checks. */
14013 CHECK_WINDOW_END (w);
14014 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14015 abort ();
14016 if (BYTEPOS (opoint) < CHARPOS (opoint))
14017 abort ();
14018
14019 /* If %c is in mode line, update it if needed. */
14020 if (!NILP (w->column_number_displayed)
14021 /* This alternative quickly identifies a common case
14022 where no change is needed. */
14023 && !(PT == XFASTINT (w->last_point)
14024 && XFASTINT (w->last_modified) >= MODIFF
14025 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14026 && (XFASTINT (w->column_number_displayed)
14027 != (int) current_column ())) /* iftc */
14028 update_mode_line = 1;
14029
14030 /* Count number of windows showing the selected buffer. An indirect
14031 buffer counts as its base buffer. */
14032 if (!just_this_one_p)
14033 {
14034 struct buffer *current_base, *window_base;
14035 current_base = current_buffer;
14036 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14037 if (current_base->base_buffer)
14038 current_base = current_base->base_buffer;
14039 if (window_base->base_buffer)
14040 window_base = window_base->base_buffer;
14041 if (current_base == window_base)
14042 buffer_shared++;
14043 }
14044
14045 /* Point refers normally to the selected window. For any other
14046 window, set up appropriate value. */
14047 if (!EQ (window, selected_window))
14048 {
14049 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14050 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14051 if (new_pt < BEGV)
14052 {
14053 new_pt = BEGV;
14054 new_pt_byte = BEGV_BYTE;
14055 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14056 }
14057 else if (new_pt > (ZV - 1))
14058 {
14059 new_pt = ZV;
14060 new_pt_byte = ZV_BYTE;
14061 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14062 }
14063
14064 /* We don't use SET_PT so that the point-motion hooks don't run. */
14065 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14066 }
14067
14068 /* If any of the character widths specified in the display table
14069 have changed, invalidate the width run cache. It's true that
14070 this may be a bit late to catch such changes, but the rest of
14071 redisplay goes (non-fatally) haywire when the display table is
14072 changed, so why should we worry about doing any better? */
14073 if (current_buffer->width_run_cache)
14074 {
14075 struct Lisp_Char_Table *disptab = buffer_display_table ();
14076
14077 if (! disptab_matches_widthtab (disptab,
14078 XVECTOR (current_buffer->width_table)))
14079 {
14080 invalidate_region_cache (current_buffer,
14081 current_buffer->width_run_cache,
14082 BEG, Z);
14083 recompute_width_table (current_buffer, disptab);
14084 }
14085 }
14086
14087 /* If window-start is screwed up, choose a new one. */
14088 if (XMARKER (w->start)->buffer != current_buffer)
14089 goto recenter;
14090
14091 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14092
14093 /* If someone specified a new starting point but did not insist,
14094 check whether it can be used. */
14095 if (!NILP (w->optional_new_start)
14096 && CHARPOS (startp) >= BEGV
14097 && CHARPOS (startp) <= ZV)
14098 {
14099 w->optional_new_start = Qnil;
14100 start_display (&it, w, startp);
14101 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14102 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14103 if (IT_CHARPOS (it) == PT)
14104 w->force_start = Qt;
14105 /* IT may overshoot PT if text at PT is invisible. */
14106 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14107 w->force_start = Qt;
14108 }
14109
14110 force_start:
14111
14112 /* Handle case where place to start displaying has been specified,
14113 unless the specified location is outside the accessible range. */
14114 if (!NILP (w->force_start)
14115 || w->frozen_window_start_p)
14116 {
14117 /* We set this later on if we have to adjust point. */
14118 int new_vpos = -1;
14119
14120 w->force_start = Qnil;
14121 w->vscroll = 0;
14122 w->window_end_valid = Qnil;
14123
14124 /* Forget any recorded base line for line number display. */
14125 if (!buffer_unchanged_p)
14126 w->base_line_number = Qnil;
14127
14128 /* Redisplay the mode line. Select the buffer properly for that.
14129 Also, run the hook window-scroll-functions
14130 because we have scrolled. */
14131 /* Note, we do this after clearing force_start because
14132 if there's an error, it is better to forget about force_start
14133 than to get into an infinite loop calling the hook functions
14134 and having them get more errors. */
14135 if (!update_mode_line
14136 || ! NILP (Vwindow_scroll_functions))
14137 {
14138 update_mode_line = 1;
14139 w->update_mode_line = Qt;
14140 startp = run_window_scroll_functions (window, startp);
14141 }
14142
14143 w->last_modified = make_number (0);
14144 w->last_overlay_modified = make_number (0);
14145 if (CHARPOS (startp) < BEGV)
14146 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14147 else if (CHARPOS (startp) > ZV)
14148 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14149
14150 /* Redisplay, then check if cursor has been set during the
14151 redisplay. Give up if new fonts were loaded. */
14152 /* We used to issue a CHECK_MARGINS argument to try_window here,
14153 but this causes scrolling to fail when point begins inside
14154 the scroll margin (bug#148) -- cyd */
14155 if (!try_window (window, startp, 0))
14156 {
14157 w->force_start = Qt;
14158 clear_glyph_matrix (w->desired_matrix);
14159 goto need_larger_matrices;
14160 }
14161
14162 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14163 {
14164 /* If point does not appear, try to move point so it does
14165 appear. The desired matrix has been built above, so we
14166 can use it here. */
14167 new_vpos = window_box_height (w) / 2;
14168 }
14169
14170 if (!cursor_row_fully_visible_p (w, 0, 0))
14171 {
14172 /* Point does appear, but on a line partly visible at end of window.
14173 Move it back to a fully-visible line. */
14174 new_vpos = window_box_height (w);
14175 }
14176
14177 /* If we need to move point for either of the above reasons,
14178 now actually do it. */
14179 if (new_vpos >= 0)
14180 {
14181 struct glyph_row *row;
14182
14183 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14184 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14185 ++row;
14186
14187 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14188 MATRIX_ROW_START_BYTEPOS (row));
14189
14190 if (w != XWINDOW (selected_window))
14191 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14192 else if (current_buffer == old)
14193 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14194
14195 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14196
14197 /* If we are highlighting the region, then we just changed
14198 the region, so redisplay to show it. */
14199 if (!NILP (Vtransient_mark_mode)
14200 && !NILP (current_buffer->mark_active))
14201 {
14202 clear_glyph_matrix (w->desired_matrix);
14203 if (!try_window (window, startp, 0))
14204 goto need_larger_matrices;
14205 }
14206 }
14207
14208 #if GLYPH_DEBUG
14209 debug_method_add (w, "forced window start");
14210 #endif
14211 goto done;
14212 }
14213
14214 /* Handle case where text has not changed, only point, and it has
14215 not moved off the frame, and we are not retrying after hscroll.
14216 (current_matrix_up_to_date_p is nonzero when retrying.) */
14217 if (current_matrix_up_to_date_p
14218 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14219 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14220 {
14221 switch (rc)
14222 {
14223 case CURSOR_MOVEMENT_SUCCESS:
14224 used_current_matrix_p = 1;
14225 goto done;
14226
14227 case CURSOR_MOVEMENT_MUST_SCROLL:
14228 goto try_to_scroll;
14229
14230 default:
14231 abort ();
14232 }
14233 }
14234 /* If current starting point was originally the beginning of a line
14235 but no longer is, find a new starting point. */
14236 else if (!NILP (w->start_at_line_beg)
14237 && !(CHARPOS (startp) <= BEGV
14238 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14239 {
14240 #if GLYPH_DEBUG
14241 debug_method_add (w, "recenter 1");
14242 #endif
14243 goto recenter;
14244 }
14245
14246 /* Try scrolling with try_window_id. Value is > 0 if update has
14247 been done, it is -1 if we know that the same window start will
14248 not work. It is 0 if unsuccessful for some other reason. */
14249 else if ((tem = try_window_id (w)) != 0)
14250 {
14251 #if GLYPH_DEBUG
14252 debug_method_add (w, "try_window_id %d", tem);
14253 #endif
14254
14255 if (fonts_changed_p)
14256 goto need_larger_matrices;
14257 if (tem > 0)
14258 goto done;
14259
14260 /* Otherwise try_window_id has returned -1 which means that we
14261 don't want the alternative below this comment to execute. */
14262 }
14263 else if (CHARPOS (startp) >= BEGV
14264 && CHARPOS (startp) <= ZV
14265 && PT >= CHARPOS (startp)
14266 && (CHARPOS (startp) < ZV
14267 /* Avoid starting at end of buffer. */
14268 || CHARPOS (startp) == BEGV
14269 || (XFASTINT (w->last_modified) >= MODIFF
14270 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14271 {
14272
14273 /* If first window line is a continuation line, and window start
14274 is inside the modified region, but the first change is before
14275 current window start, we must select a new window start.
14276
14277 However, if this is the result of a down-mouse event (e.g. by
14278 extending the mouse-drag-overlay), we don't want to select a
14279 new window start, since that would change the position under
14280 the mouse, resulting in an unwanted mouse-movement rather
14281 than a simple mouse-click. */
14282 if (NILP (w->start_at_line_beg)
14283 && NILP (do_mouse_tracking)
14284 && CHARPOS (startp) > BEGV
14285 && CHARPOS (startp) > BEG + beg_unchanged
14286 && CHARPOS (startp) <= Z - end_unchanged
14287 /* Even if w->start_at_line_beg is nil, a new window may
14288 start at a line_beg, since that's how set_buffer_window
14289 sets it. So, we need to check the return value of
14290 compute_window_start_on_continuation_line. (See also
14291 bug#197). */
14292 && XMARKER (w->start)->buffer == current_buffer
14293 && compute_window_start_on_continuation_line (w))
14294 {
14295 w->force_start = Qt;
14296 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14297 goto force_start;
14298 }
14299
14300 #if GLYPH_DEBUG
14301 debug_method_add (w, "same window start");
14302 #endif
14303
14304 /* Try to redisplay starting at same place as before.
14305 If point has not moved off frame, accept the results. */
14306 if (!current_matrix_up_to_date_p
14307 /* Don't use try_window_reusing_current_matrix in this case
14308 because a window scroll function can have changed the
14309 buffer. */
14310 || !NILP (Vwindow_scroll_functions)
14311 || MINI_WINDOW_P (w)
14312 || !(used_current_matrix_p
14313 = try_window_reusing_current_matrix (w)))
14314 {
14315 IF_DEBUG (debug_method_add (w, "1"));
14316 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14317 /* -1 means we need to scroll.
14318 0 means we need new matrices, but fonts_changed_p
14319 is set in that case, so we will detect it below. */
14320 goto try_to_scroll;
14321 }
14322
14323 if (fonts_changed_p)
14324 goto need_larger_matrices;
14325
14326 if (w->cursor.vpos >= 0)
14327 {
14328 if (!just_this_one_p
14329 || current_buffer->clip_changed
14330 || BEG_UNCHANGED < CHARPOS (startp))
14331 /* Forget any recorded base line for line number display. */
14332 w->base_line_number = Qnil;
14333
14334 if (!cursor_row_fully_visible_p (w, 1, 0))
14335 {
14336 clear_glyph_matrix (w->desired_matrix);
14337 last_line_misfit = 1;
14338 }
14339 /* Drop through and scroll. */
14340 else
14341 goto done;
14342 }
14343 else
14344 clear_glyph_matrix (w->desired_matrix);
14345 }
14346
14347 try_to_scroll:
14348
14349 w->last_modified = make_number (0);
14350 w->last_overlay_modified = make_number (0);
14351
14352 /* Redisplay the mode line. Select the buffer properly for that. */
14353 if (!update_mode_line)
14354 {
14355 update_mode_line = 1;
14356 w->update_mode_line = Qt;
14357 }
14358
14359 /* Try to scroll by specified few lines. */
14360 if ((scroll_conservatively
14361 || scroll_step
14362 || temp_scroll_step
14363 || NUMBERP (current_buffer->scroll_up_aggressively)
14364 || NUMBERP (current_buffer->scroll_down_aggressively))
14365 && !current_buffer->clip_changed
14366 && CHARPOS (startp) >= BEGV
14367 && CHARPOS (startp) <= ZV)
14368 {
14369 /* The function returns -1 if new fonts were loaded, 1 if
14370 successful, 0 if not successful. */
14371 int rc = try_scrolling (window, just_this_one_p,
14372 scroll_conservatively,
14373 scroll_step,
14374 temp_scroll_step, last_line_misfit);
14375 switch (rc)
14376 {
14377 case SCROLLING_SUCCESS:
14378 goto done;
14379
14380 case SCROLLING_NEED_LARGER_MATRICES:
14381 goto need_larger_matrices;
14382
14383 case SCROLLING_FAILED:
14384 break;
14385
14386 default:
14387 abort ();
14388 }
14389 }
14390
14391 /* Finally, just choose place to start which centers point */
14392
14393 recenter:
14394 if (centering_position < 0)
14395 centering_position = window_box_height (w) / 2;
14396
14397 #if GLYPH_DEBUG
14398 debug_method_add (w, "recenter");
14399 #endif
14400
14401 /* w->vscroll = 0; */
14402
14403 /* Forget any previously recorded base line for line number display. */
14404 if (!buffer_unchanged_p)
14405 w->base_line_number = Qnil;
14406
14407 /* Move backward half the height of the window. */
14408 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14409 it.current_y = it.last_visible_y;
14410 move_it_vertically_backward (&it, centering_position);
14411 xassert (IT_CHARPOS (it) >= BEGV);
14412
14413 /* The function move_it_vertically_backward may move over more
14414 than the specified y-distance. If it->w is small, e.g. a
14415 mini-buffer window, we may end up in front of the window's
14416 display area. Start displaying at the start of the line
14417 containing PT in this case. */
14418 if (it.current_y <= 0)
14419 {
14420 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14421 move_it_vertically_backward (&it, 0);
14422 it.current_y = 0;
14423 }
14424
14425 it.current_x = it.hpos = 0;
14426
14427 /* Set startp here explicitly in case that helps avoid an infinite loop
14428 in case the window-scroll-functions functions get errors. */
14429 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14430
14431 /* Run scroll hooks. */
14432 startp = run_window_scroll_functions (window, it.current.pos);
14433
14434 /* Redisplay the window. */
14435 if (!current_matrix_up_to_date_p
14436 || windows_or_buffers_changed
14437 || cursor_type_changed
14438 /* Don't use try_window_reusing_current_matrix in this case
14439 because it can have changed the buffer. */
14440 || !NILP (Vwindow_scroll_functions)
14441 || !just_this_one_p
14442 || MINI_WINDOW_P (w)
14443 || !(used_current_matrix_p
14444 = try_window_reusing_current_matrix (w)))
14445 try_window (window, startp, 0);
14446
14447 /* If new fonts have been loaded (due to fontsets), give up. We
14448 have to start a new redisplay since we need to re-adjust glyph
14449 matrices. */
14450 if (fonts_changed_p)
14451 goto need_larger_matrices;
14452
14453 /* If cursor did not appear assume that the middle of the window is
14454 in the first line of the window. Do it again with the next line.
14455 (Imagine a window of height 100, displaying two lines of height
14456 60. Moving back 50 from it->last_visible_y will end in the first
14457 line.) */
14458 if (w->cursor.vpos < 0)
14459 {
14460 if (!NILP (w->window_end_valid)
14461 && PT >= Z - XFASTINT (w->window_end_pos))
14462 {
14463 clear_glyph_matrix (w->desired_matrix);
14464 move_it_by_lines (&it, 1, 0);
14465 try_window (window, it.current.pos, 0);
14466 }
14467 else if (PT < IT_CHARPOS (it))
14468 {
14469 clear_glyph_matrix (w->desired_matrix);
14470 move_it_by_lines (&it, -1, 0);
14471 try_window (window, it.current.pos, 0);
14472 }
14473 else
14474 {
14475 /* Not much we can do about it. */
14476 }
14477 }
14478
14479 /* Consider the following case: Window starts at BEGV, there is
14480 invisible, intangible text at BEGV, so that display starts at
14481 some point START > BEGV. It can happen that we are called with
14482 PT somewhere between BEGV and START. Try to handle that case. */
14483 if (w->cursor.vpos < 0)
14484 {
14485 struct glyph_row *row = w->current_matrix->rows;
14486 if (row->mode_line_p)
14487 ++row;
14488 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14489 }
14490
14491 if (!cursor_row_fully_visible_p (w, 0, 0))
14492 {
14493 /* If vscroll is enabled, disable it and try again. */
14494 if (w->vscroll)
14495 {
14496 w->vscroll = 0;
14497 clear_glyph_matrix (w->desired_matrix);
14498 goto recenter;
14499 }
14500
14501 /* If centering point failed to make the whole line visible,
14502 put point at the top instead. That has to make the whole line
14503 visible, if it can be done. */
14504 if (centering_position == 0)
14505 goto done;
14506
14507 clear_glyph_matrix (w->desired_matrix);
14508 centering_position = 0;
14509 goto recenter;
14510 }
14511
14512 done:
14513
14514 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14515 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14516 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14517 ? Qt : Qnil);
14518
14519 /* Display the mode line, if we must. */
14520 if ((update_mode_line
14521 /* If window not full width, must redo its mode line
14522 if (a) the window to its side is being redone and
14523 (b) we do a frame-based redisplay. This is a consequence
14524 of how inverted lines are drawn in frame-based redisplay. */
14525 || (!just_this_one_p
14526 && !FRAME_WINDOW_P (f)
14527 && !WINDOW_FULL_WIDTH_P (w))
14528 /* Line number to display. */
14529 || INTEGERP (w->base_line_pos)
14530 /* Column number is displayed and different from the one displayed. */
14531 || (!NILP (w->column_number_displayed)
14532 && (XFASTINT (w->column_number_displayed)
14533 != (int) current_column ()))) /* iftc */
14534 /* This means that the window has a mode line. */
14535 && (WINDOW_WANTS_MODELINE_P (w)
14536 || WINDOW_WANTS_HEADER_LINE_P (w)))
14537 {
14538 display_mode_lines (w);
14539
14540 /* If mode line height has changed, arrange for a thorough
14541 immediate redisplay using the correct mode line height. */
14542 if (WINDOW_WANTS_MODELINE_P (w)
14543 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14544 {
14545 fonts_changed_p = 1;
14546 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14547 = DESIRED_MODE_LINE_HEIGHT (w);
14548 }
14549
14550 /* If header line height has changed, arrange for a thorough
14551 immediate redisplay using the correct header line height. */
14552 if (WINDOW_WANTS_HEADER_LINE_P (w)
14553 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14554 {
14555 fonts_changed_p = 1;
14556 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14557 = DESIRED_HEADER_LINE_HEIGHT (w);
14558 }
14559
14560 if (fonts_changed_p)
14561 goto need_larger_matrices;
14562 }
14563
14564 if (!line_number_displayed
14565 && !BUFFERP (w->base_line_pos))
14566 {
14567 w->base_line_pos = Qnil;
14568 w->base_line_number = Qnil;
14569 }
14570
14571 finish_menu_bars:
14572
14573 /* When we reach a frame's selected window, redo the frame's menu bar. */
14574 if (update_mode_line
14575 && EQ (FRAME_SELECTED_WINDOW (f), window))
14576 {
14577 int redisplay_menu_p = 0;
14578 int redisplay_tool_bar_p = 0;
14579
14580 if (FRAME_WINDOW_P (f))
14581 {
14582 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14583 || defined (HAVE_NS) || defined (USE_GTK)
14584 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14585 #else
14586 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14587 #endif
14588 }
14589 else
14590 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14591
14592 if (redisplay_menu_p)
14593 display_menu_bar (w);
14594
14595 #ifdef HAVE_WINDOW_SYSTEM
14596 if (FRAME_WINDOW_P (f))
14597 {
14598 #if defined (USE_GTK) || defined (HAVE_NS)
14599 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14600 #else
14601 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14602 && (FRAME_TOOL_BAR_LINES (f) > 0
14603 || !NILP (Vauto_resize_tool_bars));
14604 #endif
14605
14606 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14607 {
14608 ignore_mouse_drag_p = 1;
14609 }
14610 }
14611 #endif
14612 }
14613
14614 #ifdef HAVE_WINDOW_SYSTEM
14615 if (FRAME_WINDOW_P (f)
14616 && update_window_fringes (w, (just_this_one_p
14617 || (!used_current_matrix_p && !overlay_arrow_seen)
14618 || w->pseudo_window_p)))
14619 {
14620 update_begin (f);
14621 BLOCK_INPUT;
14622 if (draw_window_fringes (w, 1))
14623 x_draw_vertical_border (w);
14624 UNBLOCK_INPUT;
14625 update_end (f);
14626 }
14627 #endif /* HAVE_WINDOW_SYSTEM */
14628
14629 /* We go to this label, with fonts_changed_p nonzero,
14630 if it is necessary to try again using larger glyph matrices.
14631 We have to redeem the scroll bar even in this case,
14632 because the loop in redisplay_internal expects that. */
14633 need_larger_matrices:
14634 ;
14635 finish_scroll_bars:
14636
14637 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14638 {
14639 /* Set the thumb's position and size. */
14640 set_vertical_scroll_bar (w);
14641
14642 /* Note that we actually used the scroll bar attached to this
14643 window, so it shouldn't be deleted at the end of redisplay. */
14644 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14645 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14646 }
14647
14648 /* Restore current_buffer and value of point in it. The window
14649 update may have changed the buffer, so first make sure `opoint'
14650 is still valid (Bug#6177). */
14651 if (CHARPOS (opoint) < BEGV)
14652 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14653 else if (CHARPOS (opoint) > ZV)
14654 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14655 else
14656 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14657
14658 set_buffer_internal_1 (old);
14659 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14660 shorter. This can be caused by log truncation in *Messages*. */
14661 if (CHARPOS (lpoint) <= ZV)
14662 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14663
14664 unbind_to (count, Qnil);
14665 }
14666
14667
14668 /* Build the complete desired matrix of WINDOW with a window start
14669 buffer position POS.
14670
14671 Value is 1 if successful. It is zero if fonts were loaded during
14672 redisplay which makes re-adjusting glyph matrices necessary, and -1
14673 if point would appear in the scroll margins.
14674 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14675 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14676 set in FLAGS.) */
14677
14678 int
14679 try_window (Lisp_Object window, struct text_pos pos, int flags)
14680 {
14681 struct window *w = XWINDOW (window);
14682 struct it it;
14683 struct glyph_row *last_text_row = NULL;
14684 struct frame *f = XFRAME (w->frame);
14685
14686 /* Make POS the new window start. */
14687 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14688
14689 /* Mark cursor position as unknown. No overlay arrow seen. */
14690 w->cursor.vpos = -1;
14691 overlay_arrow_seen = 0;
14692
14693 /* Initialize iterator and info to start at POS. */
14694 start_display (&it, w, pos);
14695
14696 /* Display all lines of W. */
14697 while (it.current_y < it.last_visible_y)
14698 {
14699 if (display_line (&it))
14700 last_text_row = it.glyph_row - 1;
14701 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14702 return 0;
14703 }
14704
14705 /* Don't let the cursor end in the scroll margins. */
14706 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14707 && !MINI_WINDOW_P (w))
14708 {
14709 int this_scroll_margin;
14710
14711 if (scroll_margin > 0)
14712 {
14713 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14714 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14715 }
14716 else
14717 this_scroll_margin = 0;
14718
14719 if ((w->cursor.y >= 0 /* not vscrolled */
14720 && w->cursor.y < this_scroll_margin
14721 && CHARPOS (pos) > BEGV
14722 && IT_CHARPOS (it) < ZV)
14723 /* rms: considering make_cursor_line_fully_visible_p here
14724 seems to give wrong results. We don't want to recenter
14725 when the last line is partly visible, we want to allow
14726 that case to be handled in the usual way. */
14727 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14728 {
14729 w->cursor.vpos = -1;
14730 clear_glyph_matrix (w->desired_matrix);
14731 return -1;
14732 }
14733 }
14734
14735 /* If bottom moved off end of frame, change mode line percentage. */
14736 if (XFASTINT (w->window_end_pos) <= 0
14737 && Z != IT_CHARPOS (it))
14738 w->update_mode_line = Qt;
14739
14740 /* Set window_end_pos to the offset of the last character displayed
14741 on the window from the end of current_buffer. Set
14742 window_end_vpos to its row number. */
14743 if (last_text_row)
14744 {
14745 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14746 w->window_end_bytepos
14747 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14748 w->window_end_pos
14749 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14750 w->window_end_vpos
14751 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14752 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14753 ->displays_text_p);
14754 }
14755 else
14756 {
14757 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14758 w->window_end_pos = make_number (Z - ZV);
14759 w->window_end_vpos = make_number (0);
14760 }
14761
14762 /* But that is not valid info until redisplay finishes. */
14763 w->window_end_valid = Qnil;
14764 return 1;
14765 }
14766
14767
14768 \f
14769 /************************************************************************
14770 Window redisplay reusing current matrix when buffer has not changed
14771 ************************************************************************/
14772
14773 /* Try redisplay of window W showing an unchanged buffer with a
14774 different window start than the last time it was displayed by
14775 reusing its current matrix. Value is non-zero if successful.
14776 W->start is the new window start. */
14777
14778 static int
14779 try_window_reusing_current_matrix (struct window *w)
14780 {
14781 struct frame *f = XFRAME (w->frame);
14782 struct glyph_row *row, *bottom_row;
14783 struct it it;
14784 struct run run;
14785 struct text_pos start, new_start;
14786 int nrows_scrolled, i;
14787 struct glyph_row *last_text_row;
14788 struct glyph_row *last_reused_text_row;
14789 struct glyph_row *start_row;
14790 int start_vpos, min_y, max_y;
14791
14792 #if GLYPH_DEBUG
14793 if (inhibit_try_window_reusing)
14794 return 0;
14795 #endif
14796
14797 if (/* This function doesn't handle terminal frames. */
14798 !FRAME_WINDOW_P (f)
14799 /* Don't try to reuse the display if windows have been split
14800 or such. */
14801 || windows_or_buffers_changed
14802 || cursor_type_changed)
14803 return 0;
14804
14805 /* Can't do this if region may have changed. */
14806 if ((!NILP (Vtransient_mark_mode)
14807 && !NILP (current_buffer->mark_active))
14808 || !NILP (w->region_showing)
14809 || !NILP (Vshow_trailing_whitespace))
14810 return 0;
14811
14812 /* If top-line visibility has changed, give up. */
14813 if (WINDOW_WANTS_HEADER_LINE_P (w)
14814 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14815 return 0;
14816
14817 /* Give up if old or new display is scrolled vertically. We could
14818 make this function handle this, but right now it doesn't. */
14819 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14820 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14821 return 0;
14822
14823 /* The variable new_start now holds the new window start. The old
14824 start `start' can be determined from the current matrix. */
14825 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14826 start = start_row->minpos;
14827 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14828
14829 /* Clear the desired matrix for the display below. */
14830 clear_glyph_matrix (w->desired_matrix);
14831
14832 if (CHARPOS (new_start) <= CHARPOS (start))
14833 {
14834 int first_row_y;
14835
14836 /* Don't use this method if the display starts with an ellipsis
14837 displayed for invisible text. It's not easy to handle that case
14838 below, and it's certainly not worth the effort since this is
14839 not a frequent case. */
14840 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14841 return 0;
14842
14843 IF_DEBUG (debug_method_add (w, "twu1"));
14844
14845 /* Display up to a row that can be reused. The variable
14846 last_text_row is set to the last row displayed that displays
14847 text. Note that it.vpos == 0 if or if not there is a
14848 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14849 start_display (&it, w, new_start);
14850 first_row_y = it.current_y;
14851 w->cursor.vpos = -1;
14852 last_text_row = last_reused_text_row = NULL;
14853
14854 while (it.current_y < it.last_visible_y
14855 && !fonts_changed_p)
14856 {
14857 /* If we have reached into the characters in the START row,
14858 that means the line boundaries have changed. So we
14859 can't start copying with the row START. Maybe it will
14860 work to start copying with the following row. */
14861 while (IT_CHARPOS (it) > CHARPOS (start))
14862 {
14863 /* Advance to the next row as the "start". */
14864 start_row++;
14865 start = start_row->minpos;
14866 /* If there are no more rows to try, or just one, give up. */
14867 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14868 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14869 || CHARPOS (start) == ZV)
14870 {
14871 clear_glyph_matrix (w->desired_matrix);
14872 return 0;
14873 }
14874
14875 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14876 }
14877 /* If we have reached alignment,
14878 we can copy the rest of the rows. */
14879 if (IT_CHARPOS (it) == CHARPOS (start))
14880 break;
14881
14882 if (display_line (&it))
14883 last_text_row = it.glyph_row - 1;
14884 }
14885
14886 /* A value of current_y < last_visible_y means that we stopped
14887 at the previous window start, which in turn means that we
14888 have at least one reusable row. */
14889 if (it.current_y < it.last_visible_y)
14890 {
14891 /* IT.vpos always starts from 0; it counts text lines. */
14892 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14893
14894 /* Find PT if not already found in the lines displayed. */
14895 if (w->cursor.vpos < 0)
14896 {
14897 int dy = it.current_y - start_row->y;
14898
14899 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14900 row = row_containing_pos (w, PT, row, NULL, dy);
14901 if (row)
14902 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14903 dy, nrows_scrolled);
14904 else
14905 {
14906 clear_glyph_matrix (w->desired_matrix);
14907 return 0;
14908 }
14909 }
14910
14911 /* Scroll the display. Do it before the current matrix is
14912 changed. The problem here is that update has not yet
14913 run, i.e. part of the current matrix is not up to date.
14914 scroll_run_hook will clear the cursor, and use the
14915 current matrix to get the height of the row the cursor is
14916 in. */
14917 run.current_y = start_row->y;
14918 run.desired_y = it.current_y;
14919 run.height = it.last_visible_y - it.current_y;
14920
14921 if (run.height > 0 && run.current_y != run.desired_y)
14922 {
14923 update_begin (f);
14924 FRAME_RIF (f)->update_window_begin_hook (w);
14925 FRAME_RIF (f)->clear_window_mouse_face (w);
14926 FRAME_RIF (f)->scroll_run_hook (w, &run);
14927 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14928 update_end (f);
14929 }
14930
14931 /* Shift current matrix down by nrows_scrolled lines. */
14932 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14933 rotate_matrix (w->current_matrix,
14934 start_vpos,
14935 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14936 nrows_scrolled);
14937
14938 /* Disable lines that must be updated. */
14939 for (i = 0; i < nrows_scrolled; ++i)
14940 (start_row + i)->enabled_p = 0;
14941
14942 /* Re-compute Y positions. */
14943 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14944 max_y = it.last_visible_y;
14945 for (row = start_row + nrows_scrolled;
14946 row < bottom_row;
14947 ++row)
14948 {
14949 row->y = it.current_y;
14950 row->visible_height = row->height;
14951
14952 if (row->y < min_y)
14953 row->visible_height -= min_y - row->y;
14954 if (row->y + row->height > max_y)
14955 row->visible_height -= row->y + row->height - max_y;
14956 row->redraw_fringe_bitmaps_p = 1;
14957
14958 it.current_y += row->height;
14959
14960 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14961 last_reused_text_row = row;
14962 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14963 break;
14964 }
14965
14966 /* Disable lines in the current matrix which are now
14967 below the window. */
14968 for (++row; row < bottom_row; ++row)
14969 row->enabled_p = row->mode_line_p = 0;
14970 }
14971
14972 /* Update window_end_pos etc.; last_reused_text_row is the last
14973 reused row from the current matrix containing text, if any.
14974 The value of last_text_row is the last displayed line
14975 containing text. */
14976 if (last_reused_text_row)
14977 {
14978 w->window_end_bytepos
14979 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14980 w->window_end_pos
14981 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14982 w->window_end_vpos
14983 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14984 w->current_matrix));
14985 }
14986 else if (last_text_row)
14987 {
14988 w->window_end_bytepos
14989 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14990 w->window_end_pos
14991 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14992 w->window_end_vpos
14993 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14994 }
14995 else
14996 {
14997 /* This window must be completely empty. */
14998 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14999 w->window_end_pos = make_number (Z - ZV);
15000 w->window_end_vpos = make_number (0);
15001 }
15002 w->window_end_valid = Qnil;
15003
15004 /* Update hint: don't try scrolling again in update_window. */
15005 w->desired_matrix->no_scrolling_p = 1;
15006
15007 #if GLYPH_DEBUG
15008 debug_method_add (w, "try_window_reusing_current_matrix 1");
15009 #endif
15010 return 1;
15011 }
15012 else if (CHARPOS (new_start) > CHARPOS (start))
15013 {
15014 struct glyph_row *pt_row, *row;
15015 struct glyph_row *first_reusable_row;
15016 struct glyph_row *first_row_to_display;
15017 int dy;
15018 int yb = window_text_bottom_y (w);
15019
15020 /* Find the row starting at new_start, if there is one. Don't
15021 reuse a partially visible line at the end. */
15022 first_reusable_row = start_row;
15023 while (first_reusable_row->enabled_p
15024 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15025 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15026 < CHARPOS (new_start)))
15027 ++first_reusable_row;
15028
15029 /* Give up if there is no row to reuse. */
15030 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15031 || !first_reusable_row->enabled_p
15032 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15033 != CHARPOS (new_start)))
15034 return 0;
15035
15036 /* We can reuse fully visible rows beginning with
15037 first_reusable_row to the end of the window. Set
15038 first_row_to_display to the first row that cannot be reused.
15039 Set pt_row to the row containing point, if there is any. */
15040 pt_row = NULL;
15041 for (first_row_to_display = first_reusable_row;
15042 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15043 ++first_row_to_display)
15044 {
15045 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15046 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15047 pt_row = first_row_to_display;
15048 }
15049
15050 /* Start displaying at the start of first_row_to_display. */
15051 xassert (first_row_to_display->y < yb);
15052 init_to_row_start (&it, w, first_row_to_display);
15053
15054 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15055 - start_vpos);
15056 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15057 - nrows_scrolled);
15058 it.current_y = (first_row_to_display->y - first_reusable_row->y
15059 + WINDOW_HEADER_LINE_HEIGHT (w));
15060
15061 /* Display lines beginning with first_row_to_display in the
15062 desired matrix. Set last_text_row to the last row displayed
15063 that displays text. */
15064 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15065 if (pt_row == NULL)
15066 w->cursor.vpos = -1;
15067 last_text_row = NULL;
15068 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15069 if (display_line (&it))
15070 last_text_row = it.glyph_row - 1;
15071
15072 /* If point is in a reused row, adjust y and vpos of the cursor
15073 position. */
15074 if (pt_row)
15075 {
15076 w->cursor.vpos -= nrows_scrolled;
15077 w->cursor.y -= first_reusable_row->y - start_row->y;
15078 }
15079
15080 /* Give up if point isn't in a row displayed or reused. (This
15081 also handles the case where w->cursor.vpos < nrows_scrolled
15082 after the calls to display_line, which can happen with scroll
15083 margins. See bug#1295.) */
15084 if (w->cursor.vpos < 0)
15085 {
15086 clear_glyph_matrix (w->desired_matrix);
15087 return 0;
15088 }
15089
15090 /* Scroll the display. */
15091 run.current_y = first_reusable_row->y;
15092 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15093 run.height = it.last_visible_y - run.current_y;
15094 dy = run.current_y - run.desired_y;
15095
15096 if (run.height)
15097 {
15098 update_begin (f);
15099 FRAME_RIF (f)->update_window_begin_hook (w);
15100 FRAME_RIF (f)->clear_window_mouse_face (w);
15101 FRAME_RIF (f)->scroll_run_hook (w, &run);
15102 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15103 update_end (f);
15104 }
15105
15106 /* Adjust Y positions of reused rows. */
15107 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15108 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15109 max_y = it.last_visible_y;
15110 for (row = first_reusable_row; row < first_row_to_display; ++row)
15111 {
15112 row->y -= dy;
15113 row->visible_height = row->height;
15114 if (row->y < min_y)
15115 row->visible_height -= min_y - row->y;
15116 if (row->y + row->height > max_y)
15117 row->visible_height -= row->y + row->height - max_y;
15118 row->redraw_fringe_bitmaps_p = 1;
15119 }
15120
15121 /* Scroll the current matrix. */
15122 xassert (nrows_scrolled > 0);
15123 rotate_matrix (w->current_matrix,
15124 start_vpos,
15125 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15126 -nrows_scrolled);
15127
15128 /* Disable rows not reused. */
15129 for (row -= nrows_scrolled; row < bottom_row; ++row)
15130 row->enabled_p = 0;
15131
15132 /* Point may have moved to a different line, so we cannot assume that
15133 the previous cursor position is valid; locate the correct row. */
15134 if (pt_row)
15135 {
15136 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15137 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15138 row++)
15139 {
15140 w->cursor.vpos++;
15141 w->cursor.y = row->y;
15142 }
15143 if (row < bottom_row)
15144 {
15145 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15146 struct glyph *end = glyph + row->used[TEXT_AREA];
15147
15148 /* Can't use this optimization with bidi-reordered glyph
15149 rows, unless cursor is already at point. */
15150 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15151 {
15152 if (!(w->cursor.hpos >= 0
15153 && w->cursor.hpos < row->used[TEXT_AREA]
15154 && BUFFERP (glyph->object)
15155 && glyph->charpos == PT))
15156 return 0;
15157 }
15158 else
15159 for (; glyph < end
15160 && (!BUFFERP (glyph->object)
15161 || glyph->charpos < PT);
15162 glyph++)
15163 {
15164 w->cursor.hpos++;
15165 w->cursor.x += glyph->pixel_width;
15166 }
15167 }
15168 }
15169
15170 /* Adjust window end. A null value of last_text_row means that
15171 the window end is in reused rows which in turn means that
15172 only its vpos can have changed. */
15173 if (last_text_row)
15174 {
15175 w->window_end_bytepos
15176 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15177 w->window_end_pos
15178 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15179 w->window_end_vpos
15180 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15181 }
15182 else
15183 {
15184 w->window_end_vpos
15185 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15186 }
15187
15188 w->window_end_valid = Qnil;
15189 w->desired_matrix->no_scrolling_p = 1;
15190
15191 #if GLYPH_DEBUG
15192 debug_method_add (w, "try_window_reusing_current_matrix 2");
15193 #endif
15194 return 1;
15195 }
15196
15197 return 0;
15198 }
15199
15200
15201 \f
15202 /************************************************************************
15203 Window redisplay reusing current matrix when buffer has changed
15204 ************************************************************************/
15205
15206 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15207 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15208 EMACS_INT *, EMACS_INT *);
15209 static struct glyph_row *
15210 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15211 struct glyph_row *);
15212
15213
15214 /* Return the last row in MATRIX displaying text. If row START is
15215 non-null, start searching with that row. IT gives the dimensions
15216 of the display. Value is null if matrix is empty; otherwise it is
15217 a pointer to the row found. */
15218
15219 static struct glyph_row *
15220 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15221 struct glyph_row *start)
15222 {
15223 struct glyph_row *row, *row_found;
15224
15225 /* Set row_found to the last row in IT->w's current matrix
15226 displaying text. The loop looks funny but think of partially
15227 visible lines. */
15228 row_found = NULL;
15229 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15230 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15231 {
15232 xassert (row->enabled_p);
15233 row_found = row;
15234 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15235 break;
15236 ++row;
15237 }
15238
15239 return row_found;
15240 }
15241
15242
15243 /* Return the last row in the current matrix of W that is not affected
15244 by changes at the start of current_buffer that occurred since W's
15245 current matrix was built. Value is null if no such row exists.
15246
15247 BEG_UNCHANGED us the number of characters unchanged at the start of
15248 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15249 first changed character in current_buffer. Characters at positions <
15250 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15251 when the current matrix was built. */
15252
15253 static struct glyph_row *
15254 find_last_unchanged_at_beg_row (struct window *w)
15255 {
15256 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15257 struct glyph_row *row;
15258 struct glyph_row *row_found = NULL;
15259 int yb = window_text_bottom_y (w);
15260
15261 /* Find the last row displaying unchanged text. */
15262 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15263 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15264 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15265 ++row)
15266 {
15267 if (/* If row ends before first_changed_pos, it is unchanged,
15268 except in some case. */
15269 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15270 /* When row ends in ZV and we write at ZV it is not
15271 unchanged. */
15272 && !row->ends_at_zv_p
15273 /* When first_changed_pos is the end of a continued line,
15274 row is not unchanged because it may be no longer
15275 continued. */
15276 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15277 && (row->continued_p
15278 || row->exact_window_width_line_p)))
15279 row_found = row;
15280
15281 /* Stop if last visible row. */
15282 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15283 break;
15284 }
15285
15286 return row_found;
15287 }
15288
15289
15290 /* Find the first glyph row in the current matrix of W that is not
15291 affected by changes at the end of current_buffer since the
15292 time W's current matrix was built.
15293
15294 Return in *DELTA the number of chars by which buffer positions in
15295 unchanged text at the end of current_buffer must be adjusted.
15296
15297 Return in *DELTA_BYTES the corresponding number of bytes.
15298
15299 Value is null if no such row exists, i.e. all rows are affected by
15300 changes. */
15301
15302 static struct glyph_row *
15303 find_first_unchanged_at_end_row (struct window *w,
15304 EMACS_INT *delta, EMACS_INT *delta_bytes)
15305 {
15306 struct glyph_row *row;
15307 struct glyph_row *row_found = NULL;
15308
15309 *delta = *delta_bytes = 0;
15310
15311 /* Display must not have been paused, otherwise the current matrix
15312 is not up to date. */
15313 eassert (!NILP (w->window_end_valid));
15314
15315 /* A value of window_end_pos >= END_UNCHANGED means that the window
15316 end is in the range of changed text. If so, there is no
15317 unchanged row at the end of W's current matrix. */
15318 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15319 return NULL;
15320
15321 /* Set row to the last row in W's current matrix displaying text. */
15322 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15323
15324 /* If matrix is entirely empty, no unchanged row exists. */
15325 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15326 {
15327 /* The value of row is the last glyph row in the matrix having a
15328 meaningful buffer position in it. The end position of row
15329 corresponds to window_end_pos. This allows us to translate
15330 buffer positions in the current matrix to current buffer
15331 positions for characters not in changed text. */
15332 EMACS_INT Z_old =
15333 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15334 EMACS_INT Z_BYTE_old =
15335 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15336 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15337 struct glyph_row *first_text_row
15338 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15339
15340 *delta = Z - Z_old;
15341 *delta_bytes = Z_BYTE - Z_BYTE_old;
15342
15343 /* Set last_unchanged_pos to the buffer position of the last
15344 character in the buffer that has not been changed. Z is the
15345 index + 1 of the last character in current_buffer, i.e. by
15346 subtracting END_UNCHANGED we get the index of the last
15347 unchanged character, and we have to add BEG to get its buffer
15348 position. */
15349 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15350 last_unchanged_pos_old = last_unchanged_pos - *delta;
15351
15352 /* Search backward from ROW for a row displaying a line that
15353 starts at a minimum position >= last_unchanged_pos_old. */
15354 for (; row > first_text_row; --row)
15355 {
15356 /* This used to abort, but it can happen.
15357 It is ok to just stop the search instead here. KFS. */
15358 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15359 break;
15360
15361 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15362 row_found = row;
15363 }
15364 }
15365
15366 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15367
15368 return row_found;
15369 }
15370
15371
15372 /* Make sure that glyph rows in the current matrix of window W
15373 reference the same glyph memory as corresponding rows in the
15374 frame's frame matrix. This function is called after scrolling W's
15375 current matrix on a terminal frame in try_window_id and
15376 try_window_reusing_current_matrix. */
15377
15378 static void
15379 sync_frame_with_window_matrix_rows (struct window *w)
15380 {
15381 struct frame *f = XFRAME (w->frame);
15382 struct glyph_row *window_row, *window_row_end, *frame_row;
15383
15384 /* Preconditions: W must be a leaf window and full-width. Its frame
15385 must have a frame matrix. */
15386 xassert (NILP (w->hchild) && NILP (w->vchild));
15387 xassert (WINDOW_FULL_WIDTH_P (w));
15388 xassert (!FRAME_WINDOW_P (f));
15389
15390 /* If W is a full-width window, glyph pointers in W's current matrix
15391 have, by definition, to be the same as glyph pointers in the
15392 corresponding frame matrix. Note that frame matrices have no
15393 marginal areas (see build_frame_matrix). */
15394 window_row = w->current_matrix->rows;
15395 window_row_end = window_row + w->current_matrix->nrows;
15396 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15397 while (window_row < window_row_end)
15398 {
15399 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15400 struct glyph *end = window_row->glyphs[LAST_AREA];
15401
15402 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15403 frame_row->glyphs[TEXT_AREA] = start;
15404 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15405 frame_row->glyphs[LAST_AREA] = end;
15406
15407 /* Disable frame rows whose corresponding window rows have
15408 been disabled in try_window_id. */
15409 if (!window_row->enabled_p)
15410 frame_row->enabled_p = 0;
15411
15412 ++window_row, ++frame_row;
15413 }
15414 }
15415
15416
15417 /* Find the glyph row in window W containing CHARPOS. Consider all
15418 rows between START and END (not inclusive). END null means search
15419 all rows to the end of the display area of W. Value is the row
15420 containing CHARPOS or null. */
15421
15422 struct glyph_row *
15423 row_containing_pos (struct window *w, EMACS_INT charpos,
15424 struct glyph_row *start, struct glyph_row *end, int dy)
15425 {
15426 struct glyph_row *row = start;
15427 struct glyph_row *best_row = NULL;
15428 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15429 int last_y;
15430
15431 /* If we happen to start on a header-line, skip that. */
15432 if (row->mode_line_p)
15433 ++row;
15434
15435 if ((end && row >= end) || !row->enabled_p)
15436 return NULL;
15437
15438 last_y = window_text_bottom_y (w) - dy;
15439
15440 while (1)
15441 {
15442 /* Give up if we have gone too far. */
15443 if (end && row >= end)
15444 return NULL;
15445 /* This formerly returned if they were equal.
15446 I think that both quantities are of a "last plus one" type;
15447 if so, when they are equal, the row is within the screen. -- rms. */
15448 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15449 return NULL;
15450
15451 /* If it is in this row, return this row. */
15452 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15453 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15454 /* The end position of a row equals the start
15455 position of the next row. If CHARPOS is there, we
15456 would rather display it in the next line, except
15457 when this line ends in ZV. */
15458 && !row->ends_at_zv_p
15459 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15460 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15461 {
15462 struct glyph *g;
15463
15464 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15465 || (!best_row && !row->continued_p))
15466 return row;
15467 /* In bidi-reordered rows, there could be several rows
15468 occluding point, all of them belonging to the same
15469 continued line. We need to find the row which fits
15470 CHARPOS the best. */
15471 for (g = row->glyphs[TEXT_AREA];
15472 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15473 g++)
15474 {
15475 if (!STRINGP (g->object))
15476 {
15477 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15478 {
15479 mindif = eabs (g->charpos - charpos);
15480 best_row = row;
15481 /* Exact match always wins. */
15482 if (mindif == 0)
15483 return best_row;
15484 }
15485 }
15486 }
15487 }
15488 else if (best_row && !row->continued_p)
15489 return best_row;
15490 ++row;
15491 }
15492 }
15493
15494
15495 /* Try to redisplay window W by reusing its existing display. W's
15496 current matrix must be up to date when this function is called,
15497 i.e. window_end_valid must not be nil.
15498
15499 Value is
15500
15501 1 if display has been updated
15502 0 if otherwise unsuccessful
15503 -1 if redisplay with same window start is known not to succeed
15504
15505 The following steps are performed:
15506
15507 1. Find the last row in the current matrix of W that is not
15508 affected by changes at the start of current_buffer. If no such row
15509 is found, give up.
15510
15511 2. Find the first row in W's current matrix that is not affected by
15512 changes at the end of current_buffer. Maybe there is no such row.
15513
15514 3. Display lines beginning with the row + 1 found in step 1 to the
15515 row found in step 2 or, if step 2 didn't find a row, to the end of
15516 the window.
15517
15518 4. If cursor is not known to appear on the window, give up.
15519
15520 5. If display stopped at the row found in step 2, scroll the
15521 display and current matrix as needed.
15522
15523 6. Maybe display some lines at the end of W, if we must. This can
15524 happen under various circumstances, like a partially visible line
15525 becoming fully visible, or because newly displayed lines are displayed
15526 in smaller font sizes.
15527
15528 7. Update W's window end information. */
15529
15530 static int
15531 try_window_id (struct window *w)
15532 {
15533 struct frame *f = XFRAME (w->frame);
15534 struct glyph_matrix *current_matrix = w->current_matrix;
15535 struct glyph_matrix *desired_matrix = w->desired_matrix;
15536 struct glyph_row *last_unchanged_at_beg_row;
15537 struct glyph_row *first_unchanged_at_end_row;
15538 struct glyph_row *row;
15539 struct glyph_row *bottom_row;
15540 int bottom_vpos;
15541 struct it it;
15542 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15543 int dvpos, dy;
15544 struct text_pos start_pos;
15545 struct run run;
15546 int first_unchanged_at_end_vpos = 0;
15547 struct glyph_row *last_text_row, *last_text_row_at_end;
15548 struct text_pos start;
15549 EMACS_INT first_changed_charpos, last_changed_charpos;
15550
15551 #if GLYPH_DEBUG
15552 if (inhibit_try_window_id)
15553 return 0;
15554 #endif
15555
15556 /* This is handy for debugging. */
15557 #if 0
15558 #define GIVE_UP(X) \
15559 do { \
15560 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15561 return 0; \
15562 } while (0)
15563 #else
15564 #define GIVE_UP(X) return 0
15565 #endif
15566
15567 SET_TEXT_POS_FROM_MARKER (start, w->start);
15568
15569 /* Don't use this for mini-windows because these can show
15570 messages and mini-buffers, and we don't handle that here. */
15571 if (MINI_WINDOW_P (w))
15572 GIVE_UP (1);
15573
15574 /* This flag is used to prevent redisplay optimizations. */
15575 if (windows_or_buffers_changed || cursor_type_changed)
15576 GIVE_UP (2);
15577
15578 /* Verify that narrowing has not changed.
15579 Also verify that we were not told to prevent redisplay optimizations.
15580 It would be nice to further
15581 reduce the number of cases where this prevents try_window_id. */
15582 if (current_buffer->clip_changed
15583 || current_buffer->prevent_redisplay_optimizations_p)
15584 GIVE_UP (3);
15585
15586 /* Window must either use window-based redisplay or be full width. */
15587 if (!FRAME_WINDOW_P (f)
15588 && (!FRAME_LINE_INS_DEL_OK (f)
15589 || !WINDOW_FULL_WIDTH_P (w)))
15590 GIVE_UP (4);
15591
15592 /* Give up if point is known NOT to appear in W. */
15593 if (PT < CHARPOS (start))
15594 GIVE_UP (5);
15595
15596 /* Another way to prevent redisplay optimizations. */
15597 if (XFASTINT (w->last_modified) == 0)
15598 GIVE_UP (6);
15599
15600 /* Verify that window is not hscrolled. */
15601 if (XFASTINT (w->hscroll) != 0)
15602 GIVE_UP (7);
15603
15604 /* Verify that display wasn't paused. */
15605 if (NILP (w->window_end_valid))
15606 GIVE_UP (8);
15607
15608 /* Can't use this if highlighting a region because a cursor movement
15609 will do more than just set the cursor. */
15610 if (!NILP (Vtransient_mark_mode)
15611 && !NILP (current_buffer->mark_active))
15612 GIVE_UP (9);
15613
15614 /* Likewise if highlighting trailing whitespace. */
15615 if (!NILP (Vshow_trailing_whitespace))
15616 GIVE_UP (11);
15617
15618 /* Likewise if showing a region. */
15619 if (!NILP (w->region_showing))
15620 GIVE_UP (10);
15621
15622 /* Can't use this if overlay arrow position and/or string have
15623 changed. */
15624 if (overlay_arrows_changed_p ())
15625 GIVE_UP (12);
15626
15627 /* When word-wrap is on, adding a space to the first word of a
15628 wrapped line can change the wrap position, altering the line
15629 above it. It might be worthwhile to handle this more
15630 intelligently, but for now just redisplay from scratch. */
15631 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15632 GIVE_UP (21);
15633
15634 /* Under bidi reordering, adding or deleting a character in the
15635 beginning of a paragraph, before the first strong directional
15636 character, can change the base direction of the paragraph (unless
15637 the buffer specifies a fixed paragraph direction), which will
15638 require to redisplay the whole paragraph. It might be worthwhile
15639 to find the paragraph limits and widen the range of redisplayed
15640 lines to that, but for now just give up this optimization and
15641 redisplay from scratch. */
15642 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15643 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15644 GIVE_UP (22);
15645
15646 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15647 only if buffer has really changed. The reason is that the gap is
15648 initially at Z for freshly visited files. The code below would
15649 set end_unchanged to 0 in that case. */
15650 if (MODIFF > SAVE_MODIFF
15651 /* This seems to happen sometimes after saving a buffer. */
15652 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15653 {
15654 if (GPT - BEG < BEG_UNCHANGED)
15655 BEG_UNCHANGED = GPT - BEG;
15656 if (Z - GPT < END_UNCHANGED)
15657 END_UNCHANGED = Z - GPT;
15658 }
15659
15660 /* The position of the first and last character that has been changed. */
15661 first_changed_charpos = BEG + BEG_UNCHANGED;
15662 last_changed_charpos = Z - END_UNCHANGED;
15663
15664 /* If window starts after a line end, and the last change is in
15665 front of that newline, then changes don't affect the display.
15666 This case happens with stealth-fontification. Note that although
15667 the display is unchanged, glyph positions in the matrix have to
15668 be adjusted, of course. */
15669 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15670 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15671 && ((last_changed_charpos < CHARPOS (start)
15672 && CHARPOS (start) == BEGV)
15673 || (last_changed_charpos < CHARPOS (start) - 1
15674 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15675 {
15676 EMACS_INT Z_old, delta, Z_BYTE_old, delta_bytes;
15677 struct glyph_row *r0;
15678
15679 /* Compute how many chars/bytes have been added to or removed
15680 from the buffer. */
15681 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15682 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15683 delta = Z - Z_old;
15684 delta_bytes = Z_BYTE - Z_BYTE_old;
15685
15686 /* Give up if PT is not in the window. Note that it already has
15687 been checked at the start of try_window_id that PT is not in
15688 front of the window start. */
15689 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15690 GIVE_UP (13);
15691
15692 /* If window start is unchanged, we can reuse the whole matrix
15693 as is, after adjusting glyph positions. No need to compute
15694 the window end again, since its offset from Z hasn't changed. */
15695 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15696 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15697 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15698 /* PT must not be in a partially visible line. */
15699 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15700 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15701 {
15702 /* Adjust positions in the glyph matrix. */
15703 if (delta || delta_bytes)
15704 {
15705 struct glyph_row *r1
15706 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15707 increment_matrix_positions (w->current_matrix,
15708 MATRIX_ROW_VPOS (r0, current_matrix),
15709 MATRIX_ROW_VPOS (r1, current_matrix),
15710 delta, delta_bytes);
15711 }
15712
15713 /* Set the cursor. */
15714 row = row_containing_pos (w, PT, r0, NULL, 0);
15715 if (row)
15716 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15717 else
15718 abort ();
15719 return 1;
15720 }
15721 }
15722
15723 /* Handle the case that changes are all below what is displayed in
15724 the window, and that PT is in the window. This shortcut cannot
15725 be taken if ZV is visible in the window, and text has been added
15726 there that is visible in the window. */
15727 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15728 /* ZV is not visible in the window, or there are no
15729 changes at ZV, actually. */
15730 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15731 || first_changed_charpos == last_changed_charpos))
15732 {
15733 struct glyph_row *r0;
15734
15735 /* Give up if PT is not in the window. Note that it already has
15736 been checked at the start of try_window_id that PT is not in
15737 front of the window start. */
15738 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15739 GIVE_UP (14);
15740
15741 /* If window start is unchanged, we can reuse the whole matrix
15742 as is, without changing glyph positions since no text has
15743 been added/removed in front of the window end. */
15744 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15745 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15746 /* PT must not be in a partially visible line. */
15747 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15748 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15749 {
15750 /* We have to compute the window end anew since text
15751 could have been added/removed after it. */
15752 w->window_end_pos
15753 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15754 w->window_end_bytepos
15755 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15756
15757 /* Set the cursor. */
15758 row = row_containing_pos (w, PT, r0, NULL, 0);
15759 if (row)
15760 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15761 else
15762 abort ();
15763 return 2;
15764 }
15765 }
15766
15767 /* Give up if window start is in the changed area.
15768
15769 The condition used to read
15770
15771 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15772
15773 but why that was tested escapes me at the moment. */
15774 if (CHARPOS (start) >= first_changed_charpos
15775 && CHARPOS (start) <= last_changed_charpos)
15776 GIVE_UP (15);
15777
15778 /* Check that window start agrees with the start of the first glyph
15779 row in its current matrix. Check this after we know the window
15780 start is not in changed text, otherwise positions would not be
15781 comparable. */
15782 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15783 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15784 GIVE_UP (16);
15785
15786 /* Give up if the window ends in strings. Overlay strings
15787 at the end are difficult to handle, so don't try. */
15788 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15789 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15790 GIVE_UP (20);
15791
15792 /* Compute the position at which we have to start displaying new
15793 lines. Some of the lines at the top of the window might be
15794 reusable because they are not displaying changed text. Find the
15795 last row in W's current matrix not affected by changes at the
15796 start of current_buffer. Value is null if changes start in the
15797 first line of window. */
15798 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15799 if (last_unchanged_at_beg_row)
15800 {
15801 /* Avoid starting to display in the moddle of a character, a TAB
15802 for instance. This is easier than to set up the iterator
15803 exactly, and it's not a frequent case, so the additional
15804 effort wouldn't really pay off. */
15805 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15806 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15807 && last_unchanged_at_beg_row > w->current_matrix->rows)
15808 --last_unchanged_at_beg_row;
15809
15810 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15811 GIVE_UP (17);
15812
15813 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15814 GIVE_UP (18);
15815 start_pos = it.current.pos;
15816
15817 /* Start displaying new lines in the desired matrix at the same
15818 vpos we would use in the current matrix, i.e. below
15819 last_unchanged_at_beg_row. */
15820 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15821 current_matrix);
15822 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15823 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15824
15825 xassert (it.hpos == 0 && it.current_x == 0);
15826 }
15827 else
15828 {
15829 /* There are no reusable lines at the start of the window.
15830 Start displaying in the first text line. */
15831 start_display (&it, w, start);
15832 it.vpos = it.first_vpos;
15833 start_pos = it.current.pos;
15834 }
15835
15836 /* Find the first row that is not affected by changes at the end of
15837 the buffer. Value will be null if there is no unchanged row, in
15838 which case we must redisplay to the end of the window. delta
15839 will be set to the value by which buffer positions beginning with
15840 first_unchanged_at_end_row have to be adjusted due to text
15841 changes. */
15842 first_unchanged_at_end_row
15843 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15844 IF_DEBUG (debug_delta = delta);
15845 IF_DEBUG (debug_delta_bytes = delta_bytes);
15846
15847 /* Set stop_pos to the buffer position up to which we will have to
15848 display new lines. If first_unchanged_at_end_row != NULL, this
15849 is the buffer position of the start of the line displayed in that
15850 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15851 that we don't stop at a buffer position. */
15852 stop_pos = 0;
15853 if (first_unchanged_at_end_row)
15854 {
15855 xassert (last_unchanged_at_beg_row == NULL
15856 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15857
15858 /* If this is a continuation line, move forward to the next one
15859 that isn't. Changes in lines above affect this line.
15860 Caution: this may move first_unchanged_at_end_row to a row
15861 not displaying text. */
15862 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15863 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15864 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15865 < it.last_visible_y))
15866 ++first_unchanged_at_end_row;
15867
15868 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15869 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15870 >= it.last_visible_y))
15871 first_unchanged_at_end_row = NULL;
15872 else
15873 {
15874 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15875 + delta);
15876 first_unchanged_at_end_vpos
15877 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15878 xassert (stop_pos >= Z - END_UNCHANGED);
15879 }
15880 }
15881 else if (last_unchanged_at_beg_row == NULL)
15882 GIVE_UP (19);
15883
15884
15885 #if GLYPH_DEBUG
15886
15887 /* Either there is no unchanged row at the end, or the one we have
15888 now displays text. This is a necessary condition for the window
15889 end pos calculation at the end of this function. */
15890 xassert (first_unchanged_at_end_row == NULL
15891 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15892
15893 debug_last_unchanged_at_beg_vpos
15894 = (last_unchanged_at_beg_row
15895 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15896 : -1);
15897 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15898
15899 #endif /* GLYPH_DEBUG != 0 */
15900
15901
15902 /* Display new lines. Set last_text_row to the last new line
15903 displayed which has text on it, i.e. might end up as being the
15904 line where the window_end_vpos is. */
15905 w->cursor.vpos = -1;
15906 last_text_row = NULL;
15907 overlay_arrow_seen = 0;
15908 while (it.current_y < it.last_visible_y
15909 && !fonts_changed_p
15910 && (first_unchanged_at_end_row == NULL
15911 || IT_CHARPOS (it) < stop_pos))
15912 {
15913 if (display_line (&it))
15914 last_text_row = it.glyph_row - 1;
15915 }
15916
15917 if (fonts_changed_p)
15918 return -1;
15919
15920
15921 /* Compute differences in buffer positions, y-positions etc. for
15922 lines reused at the bottom of the window. Compute what we can
15923 scroll. */
15924 if (first_unchanged_at_end_row
15925 /* No lines reused because we displayed everything up to the
15926 bottom of the window. */
15927 && it.current_y < it.last_visible_y)
15928 {
15929 dvpos = (it.vpos
15930 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15931 current_matrix));
15932 dy = it.current_y - first_unchanged_at_end_row->y;
15933 run.current_y = first_unchanged_at_end_row->y;
15934 run.desired_y = run.current_y + dy;
15935 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15936 }
15937 else
15938 {
15939 delta = delta_bytes = dvpos = dy
15940 = run.current_y = run.desired_y = run.height = 0;
15941 first_unchanged_at_end_row = NULL;
15942 }
15943 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15944
15945
15946 /* Find the cursor if not already found. We have to decide whether
15947 PT will appear on this window (it sometimes doesn't, but this is
15948 not a very frequent case.) This decision has to be made before
15949 the current matrix is altered. A value of cursor.vpos < 0 means
15950 that PT is either in one of the lines beginning at
15951 first_unchanged_at_end_row or below the window. Don't care for
15952 lines that might be displayed later at the window end; as
15953 mentioned, this is not a frequent case. */
15954 if (w->cursor.vpos < 0)
15955 {
15956 /* Cursor in unchanged rows at the top? */
15957 if (PT < CHARPOS (start_pos)
15958 && last_unchanged_at_beg_row)
15959 {
15960 row = row_containing_pos (w, PT,
15961 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15962 last_unchanged_at_beg_row + 1, 0);
15963 if (row)
15964 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15965 }
15966
15967 /* Start from first_unchanged_at_end_row looking for PT. */
15968 else if (first_unchanged_at_end_row)
15969 {
15970 row = row_containing_pos (w, PT - delta,
15971 first_unchanged_at_end_row, NULL, 0);
15972 if (row)
15973 set_cursor_from_row (w, row, w->current_matrix, delta,
15974 delta_bytes, dy, dvpos);
15975 }
15976
15977 /* Give up if cursor was not found. */
15978 if (w->cursor.vpos < 0)
15979 {
15980 clear_glyph_matrix (w->desired_matrix);
15981 return -1;
15982 }
15983 }
15984
15985 /* Don't let the cursor end in the scroll margins. */
15986 {
15987 int this_scroll_margin, cursor_height;
15988
15989 this_scroll_margin = max (0, scroll_margin);
15990 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15991 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15992 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15993
15994 if ((w->cursor.y < this_scroll_margin
15995 && CHARPOS (start) > BEGV)
15996 /* Old redisplay didn't take scroll margin into account at the bottom,
15997 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15998 || (w->cursor.y + (make_cursor_line_fully_visible_p
15999 ? cursor_height + this_scroll_margin
16000 : 1)) > it.last_visible_y)
16001 {
16002 w->cursor.vpos = -1;
16003 clear_glyph_matrix (w->desired_matrix);
16004 return -1;
16005 }
16006 }
16007
16008 /* Scroll the display. Do it before changing the current matrix so
16009 that xterm.c doesn't get confused about where the cursor glyph is
16010 found. */
16011 if (dy && run.height)
16012 {
16013 update_begin (f);
16014
16015 if (FRAME_WINDOW_P (f))
16016 {
16017 FRAME_RIF (f)->update_window_begin_hook (w);
16018 FRAME_RIF (f)->clear_window_mouse_face (w);
16019 FRAME_RIF (f)->scroll_run_hook (w, &run);
16020 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16021 }
16022 else
16023 {
16024 /* Terminal frame. In this case, dvpos gives the number of
16025 lines to scroll by; dvpos < 0 means scroll up. */
16026 int first_unchanged_at_end_vpos
16027 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16028 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
16029 int end = (WINDOW_TOP_EDGE_LINE (w)
16030 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16031 + window_internal_height (w));
16032
16033 #if defined (HAVE_GPM) || defined (MSDOS)
16034 x_clear_window_mouse_face (w);
16035 #endif
16036 /* Perform the operation on the screen. */
16037 if (dvpos > 0)
16038 {
16039 /* Scroll last_unchanged_at_beg_row to the end of the
16040 window down dvpos lines. */
16041 set_terminal_window (f, end);
16042
16043 /* On dumb terminals delete dvpos lines at the end
16044 before inserting dvpos empty lines. */
16045 if (!FRAME_SCROLL_REGION_OK (f))
16046 ins_del_lines (f, end - dvpos, -dvpos);
16047
16048 /* Insert dvpos empty lines in front of
16049 last_unchanged_at_beg_row. */
16050 ins_del_lines (f, from, dvpos);
16051 }
16052 else if (dvpos < 0)
16053 {
16054 /* Scroll up last_unchanged_at_beg_vpos to the end of
16055 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16056 set_terminal_window (f, end);
16057
16058 /* Delete dvpos lines in front of
16059 last_unchanged_at_beg_vpos. ins_del_lines will set
16060 the cursor to the given vpos and emit |dvpos| delete
16061 line sequences. */
16062 ins_del_lines (f, from + dvpos, dvpos);
16063
16064 /* On a dumb terminal insert dvpos empty lines at the
16065 end. */
16066 if (!FRAME_SCROLL_REGION_OK (f))
16067 ins_del_lines (f, end + dvpos, -dvpos);
16068 }
16069
16070 set_terminal_window (f, 0);
16071 }
16072
16073 update_end (f);
16074 }
16075
16076 /* Shift reused rows of the current matrix to the right position.
16077 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16078 text. */
16079 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16080 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16081 if (dvpos < 0)
16082 {
16083 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16084 bottom_vpos, dvpos);
16085 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16086 bottom_vpos, 0);
16087 }
16088 else if (dvpos > 0)
16089 {
16090 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16091 bottom_vpos, dvpos);
16092 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16093 first_unchanged_at_end_vpos + dvpos, 0);
16094 }
16095
16096 /* For frame-based redisplay, make sure that current frame and window
16097 matrix are in sync with respect to glyph memory. */
16098 if (!FRAME_WINDOW_P (f))
16099 sync_frame_with_window_matrix_rows (w);
16100
16101 /* Adjust buffer positions in reused rows. */
16102 if (delta || delta_bytes)
16103 increment_matrix_positions (current_matrix,
16104 first_unchanged_at_end_vpos + dvpos,
16105 bottom_vpos, delta, delta_bytes);
16106
16107 /* Adjust Y positions. */
16108 if (dy)
16109 shift_glyph_matrix (w, current_matrix,
16110 first_unchanged_at_end_vpos + dvpos,
16111 bottom_vpos, dy);
16112
16113 if (first_unchanged_at_end_row)
16114 {
16115 first_unchanged_at_end_row += dvpos;
16116 if (first_unchanged_at_end_row->y >= it.last_visible_y
16117 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16118 first_unchanged_at_end_row = NULL;
16119 }
16120
16121 /* If scrolling up, there may be some lines to display at the end of
16122 the window. */
16123 last_text_row_at_end = NULL;
16124 if (dy < 0)
16125 {
16126 /* Scrolling up can leave for example a partially visible line
16127 at the end of the window to be redisplayed. */
16128 /* Set last_row to the glyph row in the current matrix where the
16129 window end line is found. It has been moved up or down in
16130 the matrix by dvpos. */
16131 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16132 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16133
16134 /* If last_row is the window end line, it should display text. */
16135 xassert (last_row->displays_text_p);
16136
16137 /* If window end line was partially visible before, begin
16138 displaying at that line. Otherwise begin displaying with the
16139 line following it. */
16140 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16141 {
16142 init_to_row_start (&it, w, last_row);
16143 it.vpos = last_vpos;
16144 it.current_y = last_row->y;
16145 }
16146 else
16147 {
16148 init_to_row_end (&it, w, last_row);
16149 it.vpos = 1 + last_vpos;
16150 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16151 ++last_row;
16152 }
16153
16154 /* We may start in a continuation line. If so, we have to
16155 get the right continuation_lines_width and current_x. */
16156 it.continuation_lines_width = last_row->continuation_lines_width;
16157 it.hpos = it.current_x = 0;
16158
16159 /* Display the rest of the lines at the window end. */
16160 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16161 while (it.current_y < it.last_visible_y
16162 && !fonts_changed_p)
16163 {
16164 /* Is it always sure that the display agrees with lines in
16165 the current matrix? I don't think so, so we mark rows
16166 displayed invalid in the current matrix by setting their
16167 enabled_p flag to zero. */
16168 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16169 if (display_line (&it))
16170 last_text_row_at_end = it.glyph_row - 1;
16171 }
16172 }
16173
16174 /* Update window_end_pos and window_end_vpos. */
16175 if (first_unchanged_at_end_row
16176 && !last_text_row_at_end)
16177 {
16178 /* Window end line if one of the preserved rows from the current
16179 matrix. Set row to the last row displaying text in current
16180 matrix starting at first_unchanged_at_end_row, after
16181 scrolling. */
16182 xassert (first_unchanged_at_end_row->displays_text_p);
16183 row = find_last_row_displaying_text (w->current_matrix, &it,
16184 first_unchanged_at_end_row);
16185 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16186
16187 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16188 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16189 w->window_end_vpos
16190 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16191 xassert (w->window_end_bytepos >= 0);
16192 IF_DEBUG (debug_method_add (w, "A"));
16193 }
16194 else if (last_text_row_at_end)
16195 {
16196 w->window_end_pos
16197 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16198 w->window_end_bytepos
16199 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16200 w->window_end_vpos
16201 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16202 xassert (w->window_end_bytepos >= 0);
16203 IF_DEBUG (debug_method_add (w, "B"));
16204 }
16205 else if (last_text_row)
16206 {
16207 /* We have displayed either to the end of the window or at the
16208 end of the window, i.e. the last row with text is to be found
16209 in the desired matrix. */
16210 w->window_end_pos
16211 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16212 w->window_end_bytepos
16213 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16214 w->window_end_vpos
16215 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16216 xassert (w->window_end_bytepos >= 0);
16217 }
16218 else if (first_unchanged_at_end_row == NULL
16219 && last_text_row == NULL
16220 && last_text_row_at_end == NULL)
16221 {
16222 /* Displayed to end of window, but no line containing text was
16223 displayed. Lines were deleted at the end of the window. */
16224 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16225 int vpos = XFASTINT (w->window_end_vpos);
16226 struct glyph_row *current_row = current_matrix->rows + vpos;
16227 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16228
16229 for (row = NULL;
16230 row == NULL && vpos >= first_vpos;
16231 --vpos, --current_row, --desired_row)
16232 {
16233 if (desired_row->enabled_p)
16234 {
16235 if (desired_row->displays_text_p)
16236 row = desired_row;
16237 }
16238 else if (current_row->displays_text_p)
16239 row = current_row;
16240 }
16241
16242 xassert (row != NULL);
16243 w->window_end_vpos = make_number (vpos + 1);
16244 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16245 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16246 xassert (w->window_end_bytepos >= 0);
16247 IF_DEBUG (debug_method_add (w, "C"));
16248 }
16249 else
16250 abort ();
16251
16252 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16253 debug_end_vpos = XFASTINT (w->window_end_vpos));
16254
16255 /* Record that display has not been completed. */
16256 w->window_end_valid = Qnil;
16257 w->desired_matrix->no_scrolling_p = 1;
16258 return 3;
16259
16260 #undef GIVE_UP
16261 }
16262
16263
16264 \f
16265 /***********************************************************************
16266 More debugging support
16267 ***********************************************************************/
16268
16269 #if GLYPH_DEBUG
16270
16271 void dump_glyph_row (struct glyph_row *, int, int);
16272 void dump_glyph_matrix (struct glyph_matrix *, int);
16273 void dump_glyph (struct glyph_row *, struct glyph *, int);
16274
16275
16276 /* Dump the contents of glyph matrix MATRIX on stderr.
16277
16278 GLYPHS 0 means don't show glyph contents.
16279 GLYPHS 1 means show glyphs in short form
16280 GLYPHS > 1 means show glyphs in long form. */
16281
16282 void
16283 dump_glyph_matrix (matrix, glyphs)
16284 struct glyph_matrix *matrix;
16285 int glyphs;
16286 {
16287 int i;
16288 for (i = 0; i < matrix->nrows; ++i)
16289 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16290 }
16291
16292
16293 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16294 the glyph row and area where the glyph comes from. */
16295
16296 void
16297 dump_glyph (row, glyph, area)
16298 struct glyph_row *row;
16299 struct glyph *glyph;
16300 int area;
16301 {
16302 if (glyph->type == CHAR_GLYPH)
16303 {
16304 fprintf (stderr,
16305 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16306 glyph - row->glyphs[TEXT_AREA],
16307 'C',
16308 glyph->charpos,
16309 (BUFFERP (glyph->object)
16310 ? 'B'
16311 : (STRINGP (glyph->object)
16312 ? 'S'
16313 : '-')),
16314 glyph->pixel_width,
16315 glyph->u.ch,
16316 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16317 ? glyph->u.ch
16318 : '.'),
16319 glyph->face_id,
16320 glyph->left_box_line_p,
16321 glyph->right_box_line_p);
16322 }
16323 else if (glyph->type == STRETCH_GLYPH)
16324 {
16325 fprintf (stderr,
16326 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16327 glyph - row->glyphs[TEXT_AREA],
16328 'S',
16329 glyph->charpos,
16330 (BUFFERP (glyph->object)
16331 ? 'B'
16332 : (STRINGP (glyph->object)
16333 ? 'S'
16334 : '-')),
16335 glyph->pixel_width,
16336 0,
16337 '.',
16338 glyph->face_id,
16339 glyph->left_box_line_p,
16340 glyph->right_box_line_p);
16341 }
16342 else if (glyph->type == IMAGE_GLYPH)
16343 {
16344 fprintf (stderr,
16345 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16346 glyph - row->glyphs[TEXT_AREA],
16347 'I',
16348 glyph->charpos,
16349 (BUFFERP (glyph->object)
16350 ? 'B'
16351 : (STRINGP (glyph->object)
16352 ? 'S'
16353 : '-')),
16354 glyph->pixel_width,
16355 glyph->u.img_id,
16356 '.',
16357 glyph->face_id,
16358 glyph->left_box_line_p,
16359 glyph->right_box_line_p);
16360 }
16361 else if (glyph->type == COMPOSITE_GLYPH)
16362 {
16363 fprintf (stderr,
16364 " %5d %4c %6d %c %3d 0x%05x",
16365 glyph - row->glyphs[TEXT_AREA],
16366 '+',
16367 glyph->charpos,
16368 (BUFFERP (glyph->object)
16369 ? 'B'
16370 : (STRINGP (glyph->object)
16371 ? 'S'
16372 : '-')),
16373 glyph->pixel_width,
16374 glyph->u.cmp.id);
16375 if (glyph->u.cmp.automatic)
16376 fprintf (stderr,
16377 "[%d-%d]",
16378 glyph->slice.cmp.from, glyph->slice.cmp.to);
16379 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16380 glyph->face_id,
16381 glyph->left_box_line_p,
16382 glyph->right_box_line_p);
16383 }
16384 }
16385
16386
16387 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16388 GLYPHS 0 means don't show glyph contents.
16389 GLYPHS 1 means show glyphs in short form
16390 GLYPHS > 1 means show glyphs in long form. */
16391
16392 void
16393 dump_glyph_row (row, vpos, glyphs)
16394 struct glyph_row *row;
16395 int vpos, glyphs;
16396 {
16397 if (glyphs != 1)
16398 {
16399 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16400 fprintf (stderr, "======================================================================\n");
16401
16402 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16403 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16404 vpos,
16405 MATRIX_ROW_START_CHARPOS (row),
16406 MATRIX_ROW_END_CHARPOS (row),
16407 row->used[TEXT_AREA],
16408 row->contains_overlapping_glyphs_p,
16409 row->enabled_p,
16410 row->truncated_on_left_p,
16411 row->truncated_on_right_p,
16412 row->continued_p,
16413 MATRIX_ROW_CONTINUATION_LINE_P (row),
16414 row->displays_text_p,
16415 row->ends_at_zv_p,
16416 row->fill_line_p,
16417 row->ends_in_middle_of_char_p,
16418 row->starts_in_middle_of_char_p,
16419 row->mouse_face_p,
16420 row->x,
16421 row->y,
16422 row->pixel_width,
16423 row->height,
16424 row->visible_height,
16425 row->ascent,
16426 row->phys_ascent);
16427 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16428 row->end.overlay_string_index,
16429 row->continuation_lines_width);
16430 fprintf (stderr, "%9d %5d\n",
16431 CHARPOS (row->start.string_pos),
16432 CHARPOS (row->end.string_pos));
16433 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16434 row->end.dpvec_index);
16435 }
16436
16437 if (glyphs > 1)
16438 {
16439 int area;
16440
16441 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16442 {
16443 struct glyph *glyph = row->glyphs[area];
16444 struct glyph *glyph_end = glyph + row->used[area];
16445
16446 /* Glyph for a line end in text. */
16447 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16448 ++glyph_end;
16449
16450 if (glyph < glyph_end)
16451 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16452
16453 for (; glyph < glyph_end; ++glyph)
16454 dump_glyph (row, glyph, area);
16455 }
16456 }
16457 else if (glyphs == 1)
16458 {
16459 int area;
16460
16461 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16462 {
16463 char *s = (char *) alloca (row->used[area] + 1);
16464 int i;
16465
16466 for (i = 0; i < row->used[area]; ++i)
16467 {
16468 struct glyph *glyph = row->glyphs[area] + i;
16469 if (glyph->type == CHAR_GLYPH
16470 && glyph->u.ch < 0x80
16471 && glyph->u.ch >= ' ')
16472 s[i] = glyph->u.ch;
16473 else
16474 s[i] = '.';
16475 }
16476
16477 s[i] = '\0';
16478 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16479 }
16480 }
16481 }
16482
16483
16484 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16485 Sdump_glyph_matrix, 0, 1, "p",
16486 doc: /* Dump the current matrix of the selected window to stderr.
16487 Shows contents of glyph row structures. With non-nil
16488 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16489 glyphs in short form, otherwise show glyphs in long form. */)
16490 (Lisp_Object glyphs)
16491 {
16492 struct window *w = XWINDOW (selected_window);
16493 struct buffer *buffer = XBUFFER (w->buffer);
16494
16495 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16496 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16497 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16498 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16499 fprintf (stderr, "=============================================\n");
16500 dump_glyph_matrix (w->current_matrix,
16501 NILP (glyphs) ? 0 : XINT (glyphs));
16502 return Qnil;
16503 }
16504
16505
16506 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16507 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16508 (void)
16509 {
16510 struct frame *f = XFRAME (selected_frame);
16511 dump_glyph_matrix (f->current_matrix, 1);
16512 return Qnil;
16513 }
16514
16515
16516 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16517 doc: /* Dump glyph row ROW to stderr.
16518 GLYPH 0 means don't dump glyphs.
16519 GLYPH 1 means dump glyphs in short form.
16520 GLYPH > 1 or omitted means dump glyphs in long form. */)
16521 (Lisp_Object row, Lisp_Object glyphs)
16522 {
16523 struct glyph_matrix *matrix;
16524 int vpos;
16525
16526 CHECK_NUMBER (row);
16527 matrix = XWINDOW (selected_window)->current_matrix;
16528 vpos = XINT (row);
16529 if (vpos >= 0 && vpos < matrix->nrows)
16530 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16531 vpos,
16532 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16533 return Qnil;
16534 }
16535
16536
16537 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16538 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16539 GLYPH 0 means don't dump glyphs.
16540 GLYPH 1 means dump glyphs in short form.
16541 GLYPH > 1 or omitted means dump glyphs in long form. */)
16542 (Lisp_Object row, Lisp_Object glyphs)
16543 {
16544 struct frame *sf = SELECTED_FRAME ();
16545 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16546 int vpos;
16547
16548 CHECK_NUMBER (row);
16549 vpos = XINT (row);
16550 if (vpos >= 0 && vpos < m->nrows)
16551 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16552 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16553 return Qnil;
16554 }
16555
16556
16557 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16558 doc: /* Toggle tracing of redisplay.
16559 With ARG, turn tracing on if and only if ARG is positive. */)
16560 (Lisp_Object arg)
16561 {
16562 if (NILP (arg))
16563 trace_redisplay_p = !trace_redisplay_p;
16564 else
16565 {
16566 arg = Fprefix_numeric_value (arg);
16567 trace_redisplay_p = XINT (arg) > 0;
16568 }
16569
16570 return Qnil;
16571 }
16572
16573
16574 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16575 doc: /* Like `format', but print result to stderr.
16576 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16577 (int nargs, Lisp_Object *args)
16578 {
16579 Lisp_Object s = Fformat (nargs, args);
16580 fprintf (stderr, "%s", SDATA (s));
16581 return Qnil;
16582 }
16583
16584 #endif /* GLYPH_DEBUG */
16585
16586
16587 \f
16588 /***********************************************************************
16589 Building Desired Matrix Rows
16590 ***********************************************************************/
16591
16592 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16593 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16594
16595 static struct glyph_row *
16596 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16597 {
16598 struct frame *f = XFRAME (WINDOW_FRAME (w));
16599 struct buffer *buffer = XBUFFER (w->buffer);
16600 struct buffer *old = current_buffer;
16601 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16602 int arrow_len = SCHARS (overlay_arrow_string);
16603 const unsigned char *arrow_end = arrow_string + arrow_len;
16604 const unsigned char *p;
16605 struct it it;
16606 int multibyte_p;
16607 int n_glyphs_before;
16608
16609 set_buffer_temp (buffer);
16610 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16611 it.glyph_row->used[TEXT_AREA] = 0;
16612 SET_TEXT_POS (it.position, 0, 0);
16613
16614 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16615 p = arrow_string;
16616 while (p < arrow_end)
16617 {
16618 Lisp_Object face, ilisp;
16619
16620 /* Get the next character. */
16621 if (multibyte_p)
16622 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16623 else
16624 {
16625 it.c = it.char_to_display = *p, it.len = 1;
16626 if (! ASCII_CHAR_P (it.c))
16627 it.char_to_display = BYTE8_TO_CHAR (it.c);
16628 }
16629 p += it.len;
16630
16631 /* Get its face. */
16632 ilisp = make_number (p - arrow_string);
16633 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16634 it.face_id = compute_char_face (f, it.char_to_display, face);
16635
16636 /* Compute its width, get its glyphs. */
16637 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16638 SET_TEXT_POS (it.position, -1, -1);
16639 PRODUCE_GLYPHS (&it);
16640
16641 /* If this character doesn't fit any more in the line, we have
16642 to remove some glyphs. */
16643 if (it.current_x > it.last_visible_x)
16644 {
16645 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16646 break;
16647 }
16648 }
16649
16650 set_buffer_temp (old);
16651 return it.glyph_row;
16652 }
16653
16654
16655 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16656 glyphs are only inserted for terminal frames since we can't really
16657 win with truncation glyphs when partially visible glyphs are
16658 involved. Which glyphs to insert is determined by
16659 produce_special_glyphs. */
16660
16661 static void
16662 insert_left_trunc_glyphs (struct it *it)
16663 {
16664 struct it truncate_it;
16665 struct glyph *from, *end, *to, *toend;
16666
16667 xassert (!FRAME_WINDOW_P (it->f));
16668
16669 /* Get the truncation glyphs. */
16670 truncate_it = *it;
16671 truncate_it.current_x = 0;
16672 truncate_it.face_id = DEFAULT_FACE_ID;
16673 truncate_it.glyph_row = &scratch_glyph_row;
16674 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16675 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16676 truncate_it.object = make_number (0);
16677 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16678
16679 /* Overwrite glyphs from IT with truncation glyphs. */
16680 if (!it->glyph_row->reversed_p)
16681 {
16682 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16683 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16684 to = it->glyph_row->glyphs[TEXT_AREA];
16685 toend = to + it->glyph_row->used[TEXT_AREA];
16686
16687 while (from < end)
16688 *to++ = *from++;
16689
16690 /* There may be padding glyphs left over. Overwrite them too. */
16691 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16692 {
16693 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16694 while (from < end)
16695 *to++ = *from++;
16696 }
16697
16698 if (to > toend)
16699 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16700 }
16701 else
16702 {
16703 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16704 that back to front. */
16705 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16706 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16707 toend = it->glyph_row->glyphs[TEXT_AREA];
16708 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16709
16710 while (from >= end && to >= toend)
16711 *to-- = *from--;
16712 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16713 {
16714 from =
16715 truncate_it.glyph_row->glyphs[TEXT_AREA]
16716 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16717 while (from >= end && to >= toend)
16718 *to-- = *from--;
16719 }
16720 if (from >= end)
16721 {
16722 /* Need to free some room before prepending additional
16723 glyphs. */
16724 int move_by = from - end + 1;
16725 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16726 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16727
16728 for ( ; g >= g0; g--)
16729 g[move_by] = *g;
16730 while (from >= end)
16731 *to-- = *from--;
16732 it->glyph_row->used[TEXT_AREA] += move_by;
16733 }
16734 }
16735 }
16736
16737
16738 /* Compute the pixel height and width of IT->glyph_row.
16739
16740 Most of the time, ascent and height of a display line will be equal
16741 to the max_ascent and max_height values of the display iterator
16742 structure. This is not the case if
16743
16744 1. We hit ZV without displaying anything. In this case, max_ascent
16745 and max_height will be zero.
16746
16747 2. We have some glyphs that don't contribute to the line height.
16748 (The glyph row flag contributes_to_line_height_p is for future
16749 pixmap extensions).
16750
16751 The first case is easily covered by using default values because in
16752 these cases, the line height does not really matter, except that it
16753 must not be zero. */
16754
16755 static void
16756 compute_line_metrics (struct it *it)
16757 {
16758 struct glyph_row *row = it->glyph_row;
16759 int area, i;
16760
16761 if (FRAME_WINDOW_P (it->f))
16762 {
16763 int i, min_y, max_y;
16764
16765 /* The line may consist of one space only, that was added to
16766 place the cursor on it. If so, the row's height hasn't been
16767 computed yet. */
16768 if (row->height == 0)
16769 {
16770 if (it->max_ascent + it->max_descent == 0)
16771 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16772 row->ascent = it->max_ascent;
16773 row->height = it->max_ascent + it->max_descent;
16774 row->phys_ascent = it->max_phys_ascent;
16775 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16776 row->extra_line_spacing = it->max_extra_line_spacing;
16777 }
16778
16779 /* Compute the width of this line. */
16780 row->pixel_width = row->x;
16781 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16782 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16783
16784 xassert (row->pixel_width >= 0);
16785 xassert (row->ascent >= 0 && row->height > 0);
16786
16787 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16788 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16789
16790 /* If first line's physical ascent is larger than its logical
16791 ascent, use the physical ascent, and make the row taller.
16792 This makes accented characters fully visible. */
16793 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16794 && row->phys_ascent > row->ascent)
16795 {
16796 row->height += row->phys_ascent - row->ascent;
16797 row->ascent = row->phys_ascent;
16798 }
16799
16800 /* Compute how much of the line is visible. */
16801 row->visible_height = row->height;
16802
16803 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16804 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16805
16806 if (row->y < min_y)
16807 row->visible_height -= min_y - row->y;
16808 if (row->y + row->height > max_y)
16809 row->visible_height -= row->y + row->height - max_y;
16810 }
16811 else
16812 {
16813 row->pixel_width = row->used[TEXT_AREA];
16814 if (row->continued_p)
16815 row->pixel_width -= it->continuation_pixel_width;
16816 else if (row->truncated_on_right_p)
16817 row->pixel_width -= it->truncation_pixel_width;
16818 row->ascent = row->phys_ascent = 0;
16819 row->height = row->phys_height = row->visible_height = 1;
16820 row->extra_line_spacing = 0;
16821 }
16822
16823 /* Compute a hash code for this row. */
16824 row->hash = 0;
16825 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16826 for (i = 0; i < row->used[area]; ++i)
16827 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16828 + row->glyphs[area][i].u.val
16829 + row->glyphs[area][i].face_id
16830 + row->glyphs[area][i].padding_p
16831 + (row->glyphs[area][i].type << 2));
16832
16833 it->max_ascent = it->max_descent = 0;
16834 it->max_phys_ascent = it->max_phys_descent = 0;
16835 }
16836
16837
16838 /* Append one space to the glyph row of iterator IT if doing a
16839 window-based redisplay. The space has the same face as
16840 IT->face_id. Value is non-zero if a space was added.
16841
16842 This function is called to make sure that there is always one glyph
16843 at the end of a glyph row that the cursor can be set on under
16844 window-systems. (If there weren't such a glyph we would not know
16845 how wide and tall a box cursor should be displayed).
16846
16847 At the same time this space let's a nicely handle clearing to the
16848 end of the line if the row ends in italic text. */
16849
16850 static int
16851 append_space_for_newline (struct it *it, int default_face_p)
16852 {
16853 if (FRAME_WINDOW_P (it->f))
16854 {
16855 int n = it->glyph_row->used[TEXT_AREA];
16856
16857 if (it->glyph_row->glyphs[TEXT_AREA] + n
16858 < it->glyph_row->glyphs[1 + TEXT_AREA])
16859 {
16860 /* Save some values that must not be changed.
16861 Must save IT->c and IT->len because otherwise
16862 ITERATOR_AT_END_P wouldn't work anymore after
16863 append_space_for_newline has been called. */
16864 enum display_element_type saved_what = it->what;
16865 int saved_c = it->c, saved_len = it->len;
16866 int saved_char_to_display = it->char_to_display;
16867 int saved_x = it->current_x;
16868 int saved_face_id = it->face_id;
16869 struct text_pos saved_pos;
16870 Lisp_Object saved_object;
16871 struct face *face;
16872
16873 saved_object = it->object;
16874 saved_pos = it->position;
16875
16876 it->what = IT_CHARACTER;
16877 memset (&it->position, 0, sizeof it->position);
16878 it->object = make_number (0);
16879 it->c = it->char_to_display = ' ';
16880 it->len = 1;
16881
16882 if (default_face_p)
16883 it->face_id = DEFAULT_FACE_ID;
16884 else if (it->face_before_selective_p)
16885 it->face_id = it->saved_face_id;
16886 face = FACE_FROM_ID (it->f, it->face_id);
16887 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16888
16889 PRODUCE_GLYPHS (it);
16890
16891 it->override_ascent = -1;
16892 it->constrain_row_ascent_descent_p = 0;
16893 it->current_x = saved_x;
16894 it->object = saved_object;
16895 it->position = saved_pos;
16896 it->what = saved_what;
16897 it->face_id = saved_face_id;
16898 it->len = saved_len;
16899 it->c = saved_c;
16900 it->char_to_display = saved_char_to_display;
16901 return 1;
16902 }
16903 }
16904
16905 return 0;
16906 }
16907
16908
16909 /* Extend the face of the last glyph in the text area of IT->glyph_row
16910 to the end of the display line. Called from display_line. If the
16911 glyph row is empty, add a space glyph to it so that we know the
16912 face to draw. Set the glyph row flag fill_line_p. If the glyph
16913 row is R2L, prepend a stretch glyph to cover the empty space to the
16914 left of the leftmost glyph. */
16915
16916 static void
16917 extend_face_to_end_of_line (struct it *it)
16918 {
16919 struct face *face;
16920 struct frame *f = it->f;
16921
16922 /* If line is already filled, do nothing. Non window-system frames
16923 get a grace of one more ``pixel'' because their characters are
16924 1-``pixel'' wide, so they hit the equality too early. This grace
16925 is needed only for R2L rows that are not continued, to produce
16926 one extra blank where we could display the cursor. */
16927 if (it->current_x >= it->last_visible_x
16928 + (!FRAME_WINDOW_P (f)
16929 && it->glyph_row->reversed_p
16930 && !it->glyph_row->continued_p))
16931 return;
16932
16933 /* Face extension extends the background and box of IT->face_id
16934 to the end of the line. If the background equals the background
16935 of the frame, we don't have to do anything. */
16936 if (it->face_before_selective_p)
16937 face = FACE_FROM_ID (f, it->saved_face_id);
16938 else
16939 face = FACE_FROM_ID (f, it->face_id);
16940
16941 if (FRAME_WINDOW_P (f)
16942 && it->glyph_row->displays_text_p
16943 && face->box == FACE_NO_BOX
16944 && face->background == FRAME_BACKGROUND_PIXEL (f)
16945 && !face->stipple
16946 && !it->glyph_row->reversed_p)
16947 return;
16948
16949 /* Set the glyph row flag indicating that the face of the last glyph
16950 in the text area has to be drawn to the end of the text area. */
16951 it->glyph_row->fill_line_p = 1;
16952
16953 /* If current character of IT is not ASCII, make sure we have the
16954 ASCII face. This will be automatically undone the next time
16955 get_next_display_element returns a multibyte character. Note
16956 that the character will always be single byte in unibyte
16957 text. */
16958 if (!ASCII_CHAR_P (it->c))
16959 {
16960 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16961 }
16962
16963 if (FRAME_WINDOW_P (f))
16964 {
16965 /* If the row is empty, add a space with the current face of IT,
16966 so that we know which face to draw. */
16967 if (it->glyph_row->used[TEXT_AREA] == 0)
16968 {
16969 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16970 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16971 it->glyph_row->used[TEXT_AREA] = 1;
16972 }
16973 #ifdef HAVE_WINDOW_SYSTEM
16974 if (it->glyph_row->reversed_p)
16975 {
16976 /* Prepend a stretch glyph to the row, such that the
16977 rightmost glyph will be drawn flushed all the way to the
16978 right margin of the window. The stretch glyph that will
16979 occupy the empty space, if any, to the left of the
16980 glyphs. */
16981 struct font *font = face->font ? face->font : FRAME_FONT (f);
16982 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16983 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16984 struct glyph *g;
16985 int row_width, stretch_ascent, stretch_width;
16986 struct text_pos saved_pos;
16987 int saved_face_id, saved_avoid_cursor;
16988
16989 for (row_width = 0, g = row_start; g < row_end; g++)
16990 row_width += g->pixel_width;
16991 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16992 if (stretch_width > 0)
16993 {
16994 stretch_ascent =
16995 (((it->ascent + it->descent)
16996 * FONT_BASE (font)) / FONT_HEIGHT (font));
16997 saved_pos = it->position;
16998 memset (&it->position, 0, sizeof it->position);
16999 saved_avoid_cursor = it->avoid_cursor_p;
17000 it->avoid_cursor_p = 1;
17001 saved_face_id = it->face_id;
17002 /* The last row's stretch glyph should get the default
17003 face, to avoid painting the rest of the window with
17004 the region face, if the region ends at ZV. */
17005 if (it->glyph_row->ends_at_zv_p)
17006 it->face_id = DEFAULT_FACE_ID;
17007 else
17008 it->face_id = face->id;
17009 append_stretch_glyph (it, make_number (0), stretch_width,
17010 it->ascent + it->descent, stretch_ascent);
17011 it->position = saved_pos;
17012 it->avoid_cursor_p = saved_avoid_cursor;
17013 it->face_id = saved_face_id;
17014 }
17015 }
17016 #endif /* HAVE_WINDOW_SYSTEM */
17017 }
17018 else
17019 {
17020 /* Save some values that must not be changed. */
17021 int saved_x = it->current_x;
17022 struct text_pos saved_pos;
17023 Lisp_Object saved_object;
17024 enum display_element_type saved_what = it->what;
17025 int saved_face_id = it->face_id;
17026
17027 saved_object = it->object;
17028 saved_pos = it->position;
17029
17030 it->what = IT_CHARACTER;
17031 memset (&it->position, 0, sizeof it->position);
17032 it->object = make_number (0);
17033 it->c = it->char_to_display = ' ';
17034 it->len = 1;
17035 /* The last row's blank glyphs should get the default face, to
17036 avoid painting the rest of the window with the region face,
17037 if the region ends at ZV. */
17038 if (it->glyph_row->ends_at_zv_p)
17039 it->face_id = DEFAULT_FACE_ID;
17040 else
17041 it->face_id = face->id;
17042
17043 PRODUCE_GLYPHS (it);
17044
17045 while (it->current_x <= it->last_visible_x)
17046 PRODUCE_GLYPHS (it);
17047
17048 /* Don't count these blanks really. It would let us insert a left
17049 truncation glyph below and make us set the cursor on them, maybe. */
17050 it->current_x = saved_x;
17051 it->object = saved_object;
17052 it->position = saved_pos;
17053 it->what = saved_what;
17054 it->face_id = saved_face_id;
17055 }
17056 }
17057
17058
17059 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17060 trailing whitespace. */
17061
17062 static int
17063 trailing_whitespace_p (EMACS_INT charpos)
17064 {
17065 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17066 int c = 0;
17067
17068 while (bytepos < ZV_BYTE
17069 && (c = FETCH_CHAR (bytepos),
17070 c == ' ' || c == '\t'))
17071 ++bytepos;
17072
17073 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17074 {
17075 if (bytepos != PT_BYTE)
17076 return 1;
17077 }
17078 return 0;
17079 }
17080
17081
17082 /* Highlight trailing whitespace, if any, in ROW. */
17083
17084 void
17085 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17086 {
17087 int used = row->used[TEXT_AREA];
17088
17089 if (used)
17090 {
17091 struct glyph *start = row->glyphs[TEXT_AREA];
17092 struct glyph *glyph = start + used - 1;
17093
17094 if (row->reversed_p)
17095 {
17096 /* Right-to-left rows need to be processed in the opposite
17097 direction, so swap the edge pointers. */
17098 glyph = start;
17099 start = row->glyphs[TEXT_AREA] + used - 1;
17100 }
17101
17102 /* Skip over glyphs inserted to display the cursor at the
17103 end of a line, for extending the face of the last glyph
17104 to the end of the line on terminals, and for truncation
17105 and continuation glyphs. */
17106 if (!row->reversed_p)
17107 {
17108 while (glyph >= start
17109 && glyph->type == CHAR_GLYPH
17110 && INTEGERP (glyph->object))
17111 --glyph;
17112 }
17113 else
17114 {
17115 while (glyph <= start
17116 && glyph->type == CHAR_GLYPH
17117 && INTEGERP (glyph->object))
17118 ++glyph;
17119 }
17120
17121 /* If last glyph is a space or stretch, and it's trailing
17122 whitespace, set the face of all trailing whitespace glyphs in
17123 IT->glyph_row to `trailing-whitespace'. */
17124 if ((row->reversed_p ? glyph <= start : glyph >= start)
17125 && BUFFERP (glyph->object)
17126 && (glyph->type == STRETCH_GLYPH
17127 || (glyph->type == CHAR_GLYPH
17128 && glyph->u.ch == ' '))
17129 && trailing_whitespace_p (glyph->charpos))
17130 {
17131 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17132 if (face_id < 0)
17133 return;
17134
17135 if (!row->reversed_p)
17136 {
17137 while (glyph >= start
17138 && BUFFERP (glyph->object)
17139 && (glyph->type == STRETCH_GLYPH
17140 || (glyph->type == CHAR_GLYPH
17141 && glyph->u.ch == ' ')))
17142 (glyph--)->face_id = face_id;
17143 }
17144 else
17145 {
17146 while (glyph <= start
17147 && BUFFERP (glyph->object)
17148 && (glyph->type == STRETCH_GLYPH
17149 || (glyph->type == CHAR_GLYPH
17150 && glyph->u.ch == ' ')))
17151 (glyph++)->face_id = face_id;
17152 }
17153 }
17154 }
17155 }
17156
17157
17158 /* Value is non-zero if glyph row ROW in window W should be
17159 used to hold the cursor. */
17160
17161 static int
17162 cursor_row_p (struct window *w, struct glyph_row *row)
17163 {
17164 int cursor_row_p = 1;
17165
17166 if (PT == CHARPOS (row->end.pos))
17167 {
17168 /* Suppose the row ends on a string.
17169 Unless the row is continued, that means it ends on a newline
17170 in the string. If it's anything other than a display string
17171 (e.g. a before-string from an overlay), we don't want the
17172 cursor there. (This heuristic seems to give the optimal
17173 behavior for the various types of multi-line strings.) */
17174 if (CHARPOS (row->end.string_pos) >= 0)
17175 {
17176 if (row->continued_p)
17177 cursor_row_p = 1;
17178 else
17179 {
17180 /* Check for `display' property. */
17181 struct glyph *beg = row->glyphs[TEXT_AREA];
17182 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17183 struct glyph *glyph;
17184
17185 cursor_row_p = 0;
17186 for (glyph = end; glyph >= beg; --glyph)
17187 if (STRINGP (glyph->object))
17188 {
17189 Lisp_Object prop
17190 = Fget_char_property (make_number (PT),
17191 Qdisplay, Qnil);
17192 cursor_row_p =
17193 (!NILP (prop)
17194 && display_prop_string_p (prop, glyph->object));
17195 break;
17196 }
17197 }
17198 }
17199 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17200 {
17201 /* If the row ends in middle of a real character,
17202 and the line is continued, we want the cursor here.
17203 That's because CHARPOS (ROW->end.pos) would equal
17204 PT if PT is before the character. */
17205 if (!row->ends_in_ellipsis_p)
17206 cursor_row_p = row->continued_p;
17207 else
17208 /* If the row ends in an ellipsis, then
17209 CHARPOS (ROW->end.pos) will equal point after the
17210 invisible text. We want that position to be displayed
17211 after the ellipsis. */
17212 cursor_row_p = 0;
17213 }
17214 /* If the row ends at ZV, display the cursor at the end of that
17215 row instead of at the start of the row below. */
17216 else if (row->ends_at_zv_p)
17217 cursor_row_p = 1;
17218 else
17219 cursor_row_p = 0;
17220 }
17221
17222 return cursor_row_p;
17223 }
17224
17225 \f
17226
17227 /* Push the display property PROP so that it will be rendered at the
17228 current position in IT. Return 1 if PROP was successfully pushed,
17229 0 otherwise. */
17230
17231 static int
17232 push_display_prop (struct it *it, Lisp_Object prop)
17233 {
17234 push_it (it);
17235
17236 if (STRINGP (prop))
17237 {
17238 if (SCHARS (prop) == 0)
17239 {
17240 pop_it (it);
17241 return 0;
17242 }
17243
17244 it->string = prop;
17245 it->multibyte_p = STRING_MULTIBYTE (it->string);
17246 it->current.overlay_string_index = -1;
17247 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17248 it->end_charpos = it->string_nchars = SCHARS (it->string);
17249 it->method = GET_FROM_STRING;
17250 it->stop_charpos = 0;
17251 }
17252 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17253 {
17254 it->method = GET_FROM_STRETCH;
17255 it->object = prop;
17256 }
17257 #ifdef HAVE_WINDOW_SYSTEM
17258 else if (IMAGEP (prop))
17259 {
17260 it->what = IT_IMAGE;
17261 it->image_id = lookup_image (it->f, prop);
17262 it->method = GET_FROM_IMAGE;
17263 }
17264 #endif /* HAVE_WINDOW_SYSTEM */
17265 else
17266 {
17267 pop_it (it); /* bogus display property, give up */
17268 return 0;
17269 }
17270
17271 return 1;
17272 }
17273
17274 /* Return the character-property PROP at the current position in IT. */
17275
17276 static Lisp_Object
17277 get_it_property (struct it *it, Lisp_Object prop)
17278 {
17279 Lisp_Object position;
17280
17281 if (STRINGP (it->object))
17282 position = make_number (IT_STRING_CHARPOS (*it));
17283 else if (BUFFERP (it->object))
17284 position = make_number (IT_CHARPOS (*it));
17285 else
17286 return Qnil;
17287
17288 return Fget_char_property (position, prop, it->object);
17289 }
17290
17291 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17292
17293 static void
17294 handle_line_prefix (struct it *it)
17295 {
17296 Lisp_Object prefix;
17297 if (it->continuation_lines_width > 0)
17298 {
17299 prefix = get_it_property (it, Qwrap_prefix);
17300 if (NILP (prefix))
17301 prefix = Vwrap_prefix;
17302 }
17303 else
17304 {
17305 prefix = get_it_property (it, Qline_prefix);
17306 if (NILP (prefix))
17307 prefix = Vline_prefix;
17308 }
17309 if (! NILP (prefix) && push_display_prop (it, prefix))
17310 {
17311 /* If the prefix is wider than the window, and we try to wrap
17312 it, it would acquire its own wrap prefix, and so on till the
17313 iterator stack overflows. So, don't wrap the prefix. */
17314 it->line_wrap = TRUNCATE;
17315 it->avoid_cursor_p = 1;
17316 }
17317 }
17318
17319 \f
17320
17321 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17322 only for R2L lines from display_line, when it decides that too many
17323 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17324 continued. */
17325 static void
17326 unproduce_glyphs (struct it *it, int n)
17327 {
17328 struct glyph *glyph, *end;
17329
17330 xassert (it->glyph_row);
17331 xassert (it->glyph_row->reversed_p);
17332 xassert (it->area == TEXT_AREA);
17333 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17334
17335 if (n > it->glyph_row->used[TEXT_AREA])
17336 n = it->glyph_row->used[TEXT_AREA];
17337 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17338 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17339 for ( ; glyph < end; glyph++)
17340 glyph[-n] = *glyph;
17341 }
17342
17343 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17344 and ROW->maxpos. */
17345 static void
17346 find_row_edges (struct it *it, struct glyph_row *row,
17347 EMACS_INT min_pos, EMACS_INT min_bpos,
17348 EMACS_INT max_pos, EMACS_INT max_bpos)
17349 {
17350 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17351 lines' rows is implemented for bidi-reordered rows. */
17352
17353 /* ROW->minpos is the value of min_pos, the minimal buffer position
17354 we have in ROW. */
17355 if (min_pos <= ZV)
17356 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17357 else
17358 {
17359 /* We didn't find _any_ valid buffer positions in any of the
17360 glyphs, so we must trust the iterator's computed
17361 positions. */
17362 row->minpos = row->start.pos;
17363 max_pos = CHARPOS (it->current.pos);
17364 max_bpos = BYTEPOS (it->current.pos);
17365 }
17366
17367 if (!max_pos)
17368 abort ();
17369
17370 /* Here are the various use-cases for ending the row, and the
17371 corresponding values for ROW->maxpos:
17372
17373 Line ends in a newline from buffer eol_pos + 1
17374 Line is continued from buffer max_pos + 1
17375 Line is truncated on right it->current.pos
17376 Line ends in a newline from string max_pos
17377 Line is continued from string max_pos
17378 Line is continued from display vector max_pos
17379 Line is entirely from a string min_pos == max_pos
17380 Line is entirely from a display vector min_pos == max_pos
17381 Line that ends at ZV ZV
17382
17383 If you discover other use-cases, please add them here as
17384 appropriate. */
17385 if (row->ends_at_zv_p)
17386 row->maxpos = it->current.pos;
17387 else if (row->used[TEXT_AREA])
17388 {
17389 if (row->ends_in_newline_from_string_p)
17390 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17391 else if (CHARPOS (it->eol_pos) > 0)
17392 SET_TEXT_POS (row->maxpos,
17393 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17394 else if (row->continued_p)
17395 {
17396 /* If max_pos is different from IT's current position, it
17397 means IT->method does not belong to the display element
17398 at max_pos. However, it also means that the display
17399 element at max_pos was displayed in its entirety on this
17400 line, which is equivalent to saying that the next line
17401 starts at the next buffer position. */
17402 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17403 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17404 else
17405 {
17406 INC_BOTH (max_pos, max_bpos);
17407 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17408 }
17409 }
17410 else if (row->truncated_on_right_p)
17411 /* display_line already called reseat_at_next_visible_line_start,
17412 which puts the iterator at the beginning of the next line, in
17413 the logical order. */
17414 row->maxpos = it->current.pos;
17415 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17416 /* A line that is entirely from a string/image/stretch... */
17417 row->maxpos = row->minpos;
17418 else
17419 abort ();
17420 }
17421 else
17422 row->maxpos = it->current.pos;
17423 }
17424
17425 /* Construct the glyph row IT->glyph_row in the desired matrix of
17426 IT->w from text at the current position of IT. See dispextern.h
17427 for an overview of struct it. Value is non-zero if
17428 IT->glyph_row displays text, as opposed to a line displaying ZV
17429 only. */
17430
17431 static int
17432 display_line (struct it *it)
17433 {
17434 struct glyph_row *row = it->glyph_row;
17435 Lisp_Object overlay_arrow_string;
17436 struct it wrap_it;
17437 int may_wrap = 0, wrap_x;
17438 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17439 int wrap_row_phys_ascent, wrap_row_phys_height;
17440 int wrap_row_extra_line_spacing;
17441 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17442 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17443 int cvpos;
17444 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17445
17446 /* We always start displaying at hpos zero even if hscrolled. */
17447 xassert (it->hpos == 0 && it->current_x == 0);
17448
17449 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17450 >= it->w->desired_matrix->nrows)
17451 {
17452 it->w->nrows_scale_factor++;
17453 fonts_changed_p = 1;
17454 return 0;
17455 }
17456
17457 /* Is IT->w showing the region? */
17458 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17459
17460 /* Clear the result glyph row and enable it. */
17461 prepare_desired_row (row);
17462
17463 row->y = it->current_y;
17464 row->start = it->start;
17465 row->continuation_lines_width = it->continuation_lines_width;
17466 row->displays_text_p = 1;
17467 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17468 it->starts_in_middle_of_char_p = 0;
17469
17470 /* Arrange the overlays nicely for our purposes. Usually, we call
17471 display_line on only one line at a time, in which case this
17472 can't really hurt too much, or we call it on lines which appear
17473 one after another in the buffer, in which case all calls to
17474 recenter_overlay_lists but the first will be pretty cheap. */
17475 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17476
17477 /* Move over display elements that are not visible because we are
17478 hscrolled. This may stop at an x-position < IT->first_visible_x
17479 if the first glyph is partially visible or if we hit a line end. */
17480 if (it->current_x < it->first_visible_x)
17481 {
17482 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17483 MOVE_TO_POS | MOVE_TO_X);
17484 }
17485 else
17486 {
17487 /* We only do this when not calling `move_it_in_display_line_to'
17488 above, because move_it_in_display_line_to calls
17489 handle_line_prefix itself. */
17490 handle_line_prefix (it);
17491 }
17492
17493 /* Get the initial row height. This is either the height of the
17494 text hscrolled, if there is any, or zero. */
17495 row->ascent = it->max_ascent;
17496 row->height = it->max_ascent + it->max_descent;
17497 row->phys_ascent = it->max_phys_ascent;
17498 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17499 row->extra_line_spacing = it->max_extra_line_spacing;
17500
17501 /* Utility macro to record max and min buffer positions seen until now. */
17502 #define RECORD_MAX_MIN_POS(IT) \
17503 do \
17504 { \
17505 if (IT_CHARPOS (*(IT)) < min_pos) \
17506 { \
17507 min_pos = IT_CHARPOS (*(IT)); \
17508 min_bpos = IT_BYTEPOS (*(IT)); \
17509 } \
17510 if (IT_CHARPOS (*(IT)) > max_pos) \
17511 { \
17512 max_pos = IT_CHARPOS (*(IT)); \
17513 max_bpos = IT_BYTEPOS (*(IT)); \
17514 } \
17515 } \
17516 while (0)
17517
17518 /* Loop generating characters. The loop is left with IT on the next
17519 character to display. */
17520 while (1)
17521 {
17522 int n_glyphs_before, hpos_before, x_before;
17523 int x, i, nglyphs;
17524 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17525
17526 /* Retrieve the next thing to display. Value is zero if end of
17527 buffer reached. */
17528 if (!get_next_display_element (it))
17529 {
17530 /* Maybe add a space at the end of this line that is used to
17531 display the cursor there under X. Set the charpos of the
17532 first glyph of blank lines not corresponding to any text
17533 to -1. */
17534 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17535 row->exact_window_width_line_p = 1;
17536 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17537 || row->used[TEXT_AREA] == 0)
17538 {
17539 row->glyphs[TEXT_AREA]->charpos = -1;
17540 row->displays_text_p = 0;
17541
17542 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17543 && (!MINI_WINDOW_P (it->w)
17544 || (minibuf_level && EQ (it->window, minibuf_window))))
17545 row->indicate_empty_line_p = 1;
17546 }
17547
17548 it->continuation_lines_width = 0;
17549 row->ends_at_zv_p = 1;
17550 /* A row that displays right-to-left text must always have
17551 its last face extended all the way to the end of line,
17552 even if this row ends in ZV, because we still write to
17553 the screen left to right. */
17554 if (row->reversed_p)
17555 extend_face_to_end_of_line (it);
17556 break;
17557 }
17558
17559 /* Now, get the metrics of what we want to display. This also
17560 generates glyphs in `row' (which is IT->glyph_row). */
17561 n_glyphs_before = row->used[TEXT_AREA];
17562 x = it->current_x;
17563
17564 /* Remember the line height so far in case the next element doesn't
17565 fit on the line. */
17566 if (it->line_wrap != TRUNCATE)
17567 {
17568 ascent = it->max_ascent;
17569 descent = it->max_descent;
17570 phys_ascent = it->max_phys_ascent;
17571 phys_descent = it->max_phys_descent;
17572
17573 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17574 {
17575 if (IT_DISPLAYING_WHITESPACE (it))
17576 may_wrap = 1;
17577 else if (may_wrap)
17578 {
17579 wrap_it = *it;
17580 wrap_x = x;
17581 wrap_row_used = row->used[TEXT_AREA];
17582 wrap_row_ascent = row->ascent;
17583 wrap_row_height = row->height;
17584 wrap_row_phys_ascent = row->phys_ascent;
17585 wrap_row_phys_height = row->phys_height;
17586 wrap_row_extra_line_spacing = row->extra_line_spacing;
17587 wrap_row_min_pos = min_pos;
17588 wrap_row_min_bpos = min_bpos;
17589 wrap_row_max_pos = max_pos;
17590 wrap_row_max_bpos = max_bpos;
17591 may_wrap = 0;
17592 }
17593 }
17594 }
17595
17596 PRODUCE_GLYPHS (it);
17597
17598 /* If this display element was in marginal areas, continue with
17599 the next one. */
17600 if (it->area != TEXT_AREA)
17601 {
17602 row->ascent = max (row->ascent, it->max_ascent);
17603 row->height = max (row->height, it->max_ascent + it->max_descent);
17604 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17605 row->phys_height = max (row->phys_height,
17606 it->max_phys_ascent + it->max_phys_descent);
17607 row->extra_line_spacing = max (row->extra_line_spacing,
17608 it->max_extra_line_spacing);
17609 set_iterator_to_next (it, 1);
17610 continue;
17611 }
17612
17613 /* Does the display element fit on the line? If we truncate
17614 lines, we should draw past the right edge of the window. If
17615 we don't truncate, we want to stop so that we can display the
17616 continuation glyph before the right margin. If lines are
17617 continued, there are two possible strategies for characters
17618 resulting in more than 1 glyph (e.g. tabs): Display as many
17619 glyphs as possible in this line and leave the rest for the
17620 continuation line, or display the whole element in the next
17621 line. Original redisplay did the former, so we do it also. */
17622 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17623 hpos_before = it->hpos;
17624 x_before = x;
17625
17626 if (/* Not a newline. */
17627 nglyphs > 0
17628 /* Glyphs produced fit entirely in the line. */
17629 && it->current_x < it->last_visible_x)
17630 {
17631 it->hpos += nglyphs;
17632 row->ascent = max (row->ascent, it->max_ascent);
17633 row->height = max (row->height, it->max_ascent + it->max_descent);
17634 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17635 row->phys_height = max (row->phys_height,
17636 it->max_phys_ascent + it->max_phys_descent);
17637 row->extra_line_spacing = max (row->extra_line_spacing,
17638 it->max_extra_line_spacing);
17639 if (it->current_x - it->pixel_width < it->first_visible_x)
17640 row->x = x - it->first_visible_x;
17641 /* Record the maximum and minimum buffer positions seen so
17642 far in glyphs that will be displayed by this row. */
17643 if (it->bidi_p)
17644 RECORD_MAX_MIN_POS (it);
17645 }
17646 else
17647 {
17648 int new_x;
17649 struct glyph *glyph;
17650
17651 for (i = 0; i < nglyphs; ++i, x = new_x)
17652 {
17653 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17654 new_x = x + glyph->pixel_width;
17655
17656 if (/* Lines are continued. */
17657 it->line_wrap != TRUNCATE
17658 && (/* Glyph doesn't fit on the line. */
17659 new_x > it->last_visible_x
17660 /* Or it fits exactly on a window system frame. */
17661 || (new_x == it->last_visible_x
17662 && FRAME_WINDOW_P (it->f))))
17663 {
17664 /* End of a continued line. */
17665
17666 if (it->hpos == 0
17667 || (new_x == it->last_visible_x
17668 && FRAME_WINDOW_P (it->f)))
17669 {
17670 /* Current glyph is the only one on the line or
17671 fits exactly on the line. We must continue
17672 the line because we can't draw the cursor
17673 after the glyph. */
17674 row->continued_p = 1;
17675 it->current_x = new_x;
17676 it->continuation_lines_width += new_x;
17677 ++it->hpos;
17678 /* Record the maximum and minimum buffer
17679 positions seen so far in glyphs that will be
17680 displayed by this row. */
17681 if (it->bidi_p)
17682 RECORD_MAX_MIN_POS (it);
17683 if (i == nglyphs - 1)
17684 {
17685 /* If line-wrap is on, check if a previous
17686 wrap point was found. */
17687 if (wrap_row_used > 0
17688 /* Even if there is a previous wrap
17689 point, continue the line here as
17690 usual, if (i) the previous character
17691 was a space or tab AND (ii) the
17692 current character is not. */
17693 && (!may_wrap
17694 || IT_DISPLAYING_WHITESPACE (it)))
17695 goto back_to_wrap;
17696
17697 set_iterator_to_next (it, 1);
17698 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17699 {
17700 if (!get_next_display_element (it))
17701 {
17702 row->exact_window_width_line_p = 1;
17703 it->continuation_lines_width = 0;
17704 row->continued_p = 0;
17705 row->ends_at_zv_p = 1;
17706 }
17707 else if (ITERATOR_AT_END_OF_LINE_P (it))
17708 {
17709 row->continued_p = 0;
17710 row->exact_window_width_line_p = 1;
17711 }
17712 }
17713 }
17714 }
17715 else if (CHAR_GLYPH_PADDING_P (*glyph)
17716 && !FRAME_WINDOW_P (it->f))
17717 {
17718 /* A padding glyph that doesn't fit on this line.
17719 This means the whole character doesn't fit
17720 on the line. */
17721 if (row->reversed_p)
17722 unproduce_glyphs (it, row->used[TEXT_AREA]
17723 - n_glyphs_before);
17724 row->used[TEXT_AREA] = n_glyphs_before;
17725
17726 /* Fill the rest of the row with continuation
17727 glyphs like in 20.x. */
17728 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17729 < row->glyphs[1 + TEXT_AREA])
17730 produce_special_glyphs (it, IT_CONTINUATION);
17731
17732 row->continued_p = 1;
17733 it->current_x = x_before;
17734 it->continuation_lines_width += x_before;
17735
17736 /* Restore the height to what it was before the
17737 element not fitting on the line. */
17738 it->max_ascent = ascent;
17739 it->max_descent = descent;
17740 it->max_phys_ascent = phys_ascent;
17741 it->max_phys_descent = phys_descent;
17742 }
17743 else if (wrap_row_used > 0)
17744 {
17745 back_to_wrap:
17746 if (row->reversed_p)
17747 unproduce_glyphs (it,
17748 row->used[TEXT_AREA] - wrap_row_used);
17749 *it = wrap_it;
17750 it->continuation_lines_width += wrap_x;
17751 row->used[TEXT_AREA] = wrap_row_used;
17752 row->ascent = wrap_row_ascent;
17753 row->height = wrap_row_height;
17754 row->phys_ascent = wrap_row_phys_ascent;
17755 row->phys_height = wrap_row_phys_height;
17756 row->extra_line_spacing = wrap_row_extra_line_spacing;
17757 min_pos = wrap_row_min_pos;
17758 min_bpos = wrap_row_min_bpos;
17759 max_pos = wrap_row_max_pos;
17760 max_bpos = wrap_row_max_bpos;
17761 row->continued_p = 1;
17762 row->ends_at_zv_p = 0;
17763 row->exact_window_width_line_p = 0;
17764 it->continuation_lines_width += x;
17765
17766 /* Make sure that a non-default face is extended
17767 up to the right margin of the window. */
17768 extend_face_to_end_of_line (it);
17769 }
17770 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17771 {
17772 /* A TAB that extends past the right edge of the
17773 window. This produces a single glyph on
17774 window system frames. We leave the glyph in
17775 this row and let it fill the row, but don't
17776 consume the TAB. */
17777 it->continuation_lines_width += it->last_visible_x;
17778 row->ends_in_middle_of_char_p = 1;
17779 row->continued_p = 1;
17780 glyph->pixel_width = it->last_visible_x - x;
17781 it->starts_in_middle_of_char_p = 1;
17782 }
17783 else
17784 {
17785 /* Something other than a TAB that draws past
17786 the right edge of the window. Restore
17787 positions to values before the element. */
17788 if (row->reversed_p)
17789 unproduce_glyphs (it, row->used[TEXT_AREA]
17790 - (n_glyphs_before + i));
17791 row->used[TEXT_AREA] = n_glyphs_before + i;
17792
17793 /* Display continuation glyphs. */
17794 if (!FRAME_WINDOW_P (it->f))
17795 produce_special_glyphs (it, IT_CONTINUATION);
17796 row->continued_p = 1;
17797
17798 it->current_x = x_before;
17799 it->continuation_lines_width += x;
17800 extend_face_to_end_of_line (it);
17801
17802 if (nglyphs > 1 && i > 0)
17803 {
17804 row->ends_in_middle_of_char_p = 1;
17805 it->starts_in_middle_of_char_p = 1;
17806 }
17807
17808 /* Restore the height to what it was before the
17809 element not fitting on the line. */
17810 it->max_ascent = ascent;
17811 it->max_descent = descent;
17812 it->max_phys_ascent = phys_ascent;
17813 it->max_phys_descent = phys_descent;
17814 }
17815
17816 break;
17817 }
17818 else if (new_x > it->first_visible_x)
17819 {
17820 /* Increment number of glyphs actually displayed. */
17821 ++it->hpos;
17822
17823 /* Record the maximum and minimum buffer positions
17824 seen so far in glyphs that will be displayed by
17825 this row. */
17826 if (it->bidi_p)
17827 RECORD_MAX_MIN_POS (it);
17828
17829 if (x < it->first_visible_x)
17830 /* Glyph is partially visible, i.e. row starts at
17831 negative X position. */
17832 row->x = x - it->first_visible_x;
17833 }
17834 else
17835 {
17836 /* Glyph is completely off the left margin of the
17837 window. This should not happen because of the
17838 move_it_in_display_line at the start of this
17839 function, unless the text display area of the
17840 window is empty. */
17841 xassert (it->first_visible_x <= it->last_visible_x);
17842 }
17843 }
17844
17845 row->ascent = max (row->ascent, it->max_ascent);
17846 row->height = max (row->height, it->max_ascent + it->max_descent);
17847 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17848 row->phys_height = max (row->phys_height,
17849 it->max_phys_ascent + it->max_phys_descent);
17850 row->extra_line_spacing = max (row->extra_line_spacing,
17851 it->max_extra_line_spacing);
17852
17853 /* End of this display line if row is continued. */
17854 if (row->continued_p || row->ends_at_zv_p)
17855 break;
17856 }
17857
17858 at_end_of_line:
17859 /* Is this a line end? If yes, we're also done, after making
17860 sure that a non-default face is extended up to the right
17861 margin of the window. */
17862 if (ITERATOR_AT_END_OF_LINE_P (it))
17863 {
17864 int used_before = row->used[TEXT_AREA];
17865
17866 row->ends_in_newline_from_string_p = STRINGP (it->object);
17867
17868 /* Add a space at the end of the line that is used to
17869 display the cursor there. */
17870 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17871 append_space_for_newline (it, 0);
17872
17873 /* Extend the face to the end of the line. */
17874 extend_face_to_end_of_line (it);
17875
17876 /* Make sure we have the position. */
17877 if (used_before == 0)
17878 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17879
17880 /* Record the position of the newline, for use in
17881 find_row_edges. */
17882 it->eol_pos = it->current.pos;
17883
17884 /* Consume the line end. This skips over invisible lines. */
17885 set_iterator_to_next (it, 1);
17886 it->continuation_lines_width = 0;
17887 break;
17888 }
17889
17890 /* Proceed with next display element. Note that this skips
17891 over lines invisible because of selective display. */
17892 set_iterator_to_next (it, 1);
17893
17894 /* If we truncate lines, we are done when the last displayed
17895 glyphs reach past the right margin of the window. */
17896 if (it->line_wrap == TRUNCATE
17897 && (FRAME_WINDOW_P (it->f)
17898 ? (it->current_x >= it->last_visible_x)
17899 : (it->current_x > it->last_visible_x)))
17900 {
17901 /* Maybe add truncation glyphs. */
17902 if (!FRAME_WINDOW_P (it->f))
17903 {
17904 int i, n;
17905
17906 if (!row->reversed_p)
17907 {
17908 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17909 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17910 break;
17911 }
17912 else
17913 {
17914 for (i = 0; i < row->used[TEXT_AREA]; i++)
17915 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17916 break;
17917 /* Remove any padding glyphs at the front of ROW, to
17918 make room for the truncation glyphs we will be
17919 adding below. The loop below always inserts at
17920 least one truncation glyph, so also remove the
17921 last glyph added to ROW. */
17922 unproduce_glyphs (it, i + 1);
17923 /* Adjust i for the loop below. */
17924 i = row->used[TEXT_AREA] - (i + 1);
17925 }
17926
17927 for (n = row->used[TEXT_AREA]; i < n; ++i)
17928 {
17929 row->used[TEXT_AREA] = i;
17930 produce_special_glyphs (it, IT_TRUNCATION);
17931 }
17932 }
17933 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17934 {
17935 /* Don't truncate if we can overflow newline into fringe. */
17936 if (!get_next_display_element (it))
17937 {
17938 it->continuation_lines_width = 0;
17939 row->ends_at_zv_p = 1;
17940 row->exact_window_width_line_p = 1;
17941 break;
17942 }
17943 if (ITERATOR_AT_END_OF_LINE_P (it))
17944 {
17945 row->exact_window_width_line_p = 1;
17946 goto at_end_of_line;
17947 }
17948 }
17949
17950 row->truncated_on_right_p = 1;
17951 it->continuation_lines_width = 0;
17952 reseat_at_next_visible_line_start (it, 0);
17953 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17954 it->hpos = hpos_before;
17955 it->current_x = x_before;
17956 break;
17957 }
17958 }
17959
17960 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17961 at the left window margin. */
17962 if (it->first_visible_x
17963 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17964 {
17965 if (!FRAME_WINDOW_P (it->f))
17966 insert_left_trunc_glyphs (it);
17967 row->truncated_on_left_p = 1;
17968 }
17969
17970 /* Remember the position at which this line ends.
17971
17972 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17973 cannot be before the call to find_row_edges below, since that is
17974 where these positions are determined. */
17975 row->end = it->current;
17976 if (!it->bidi_p)
17977 {
17978 row->minpos = row->start.pos;
17979 row->maxpos = row->end.pos;
17980 }
17981 else
17982 {
17983 /* ROW->minpos and ROW->maxpos must be the smallest and
17984 `1 + the largest' buffer positions in ROW. But if ROW was
17985 bidi-reordered, these two positions can be anywhere in the
17986 row, so we must determine them now. */
17987 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17988 }
17989
17990 /* If the start of this line is the overlay arrow-position, then
17991 mark this glyph row as the one containing the overlay arrow.
17992 This is clearly a mess with variable size fonts. It would be
17993 better to let it be displayed like cursors under X. */
17994 if ((row->displays_text_p || !overlay_arrow_seen)
17995 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17996 !NILP (overlay_arrow_string)))
17997 {
17998 /* Overlay arrow in window redisplay is a fringe bitmap. */
17999 if (STRINGP (overlay_arrow_string))
18000 {
18001 struct glyph_row *arrow_row
18002 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18003 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18004 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18005 struct glyph *p = row->glyphs[TEXT_AREA];
18006 struct glyph *p2, *end;
18007
18008 /* Copy the arrow glyphs. */
18009 while (glyph < arrow_end)
18010 *p++ = *glyph++;
18011
18012 /* Throw away padding glyphs. */
18013 p2 = p;
18014 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18015 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18016 ++p2;
18017 if (p2 > p)
18018 {
18019 while (p2 < end)
18020 *p++ = *p2++;
18021 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18022 }
18023 }
18024 else
18025 {
18026 xassert (INTEGERP (overlay_arrow_string));
18027 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18028 }
18029 overlay_arrow_seen = 1;
18030 }
18031
18032 /* Compute pixel dimensions of this line. */
18033 compute_line_metrics (it);
18034
18035 /* Record whether this row ends inside an ellipsis. */
18036 row->ends_in_ellipsis_p
18037 = (it->method == GET_FROM_DISPLAY_VECTOR
18038 && it->ellipsis_p);
18039
18040 /* Save fringe bitmaps in this row. */
18041 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18042 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18043 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18044 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18045
18046 it->left_user_fringe_bitmap = 0;
18047 it->left_user_fringe_face_id = 0;
18048 it->right_user_fringe_bitmap = 0;
18049 it->right_user_fringe_face_id = 0;
18050
18051 /* Maybe set the cursor. */
18052 cvpos = it->w->cursor.vpos;
18053 if ((cvpos < 0
18054 /* In bidi-reordered rows, keep checking for proper cursor
18055 position even if one has been found already, because buffer
18056 positions in such rows change non-linearly with ROW->VPOS,
18057 when a line is continued. One exception: when we are at ZV,
18058 display cursor on the first suitable glyph row, since all
18059 the empty rows after that also have their position set to ZV. */
18060 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18061 lines' rows is implemented for bidi-reordered rows. */
18062 || (it->bidi_p
18063 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18064 && PT >= MATRIX_ROW_START_CHARPOS (row)
18065 && PT <= MATRIX_ROW_END_CHARPOS (row)
18066 && cursor_row_p (it->w, row))
18067 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18068
18069 /* Highlight trailing whitespace. */
18070 if (!NILP (Vshow_trailing_whitespace))
18071 highlight_trailing_whitespace (it->f, it->glyph_row);
18072
18073 /* Prepare for the next line. This line starts horizontally at (X
18074 HPOS) = (0 0). Vertical positions are incremented. As a
18075 convenience for the caller, IT->glyph_row is set to the next
18076 row to be used. */
18077 it->current_x = it->hpos = 0;
18078 it->current_y += row->height;
18079 SET_TEXT_POS (it->eol_pos, 0, 0);
18080 ++it->vpos;
18081 ++it->glyph_row;
18082 /* The next row should by default use the same value of the
18083 reversed_p flag as this one. set_iterator_to_next decides when
18084 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18085 the flag accordingly. */
18086 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18087 it->glyph_row->reversed_p = row->reversed_p;
18088 it->start = row->end;
18089 return row->displays_text_p;
18090
18091 #undef RECORD_MAX_MIN_POS
18092 }
18093
18094 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18095 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18096 doc: /* Return paragraph direction at point in BUFFER.
18097 Value is either `left-to-right' or `right-to-left'.
18098 If BUFFER is omitted or nil, it defaults to the current buffer.
18099
18100 Paragraph direction determines how the text in the paragraph is displayed.
18101 In left-to-right paragraphs, text begins at the left margin of the window
18102 and the reading direction is generally left to right. In right-to-left
18103 paragraphs, text begins at the right margin and is read from right to left.
18104
18105 See also `bidi-paragraph-direction'. */)
18106 (Lisp_Object buffer)
18107 {
18108 struct buffer *buf;
18109 struct buffer *old;
18110
18111 if (NILP (buffer))
18112 buf = current_buffer;
18113 else
18114 {
18115 CHECK_BUFFER (buffer);
18116 buf = XBUFFER (buffer);
18117 old = current_buffer;
18118 }
18119
18120 if (NILP (buf->bidi_display_reordering))
18121 return Qleft_to_right;
18122 else if (!NILP (buf->bidi_paragraph_direction))
18123 return buf->bidi_paragraph_direction;
18124 else
18125 {
18126 /* Determine the direction from buffer text. We could try to
18127 use current_matrix if it is up to date, but this seems fast
18128 enough as it is. */
18129 struct bidi_it itb;
18130 EMACS_INT pos = BUF_PT (buf);
18131 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18132 int c;
18133
18134 if (buf != current_buffer)
18135 set_buffer_temp (buf);
18136 /* bidi_paragraph_init finds the base direction of the paragraph
18137 by searching forward from paragraph start. We need the base
18138 direction of the current or _previous_ paragraph, so we need
18139 to make sure we are within that paragraph. To that end, find
18140 the previous non-empty line. */
18141 if (pos >= ZV && pos > BEGV)
18142 {
18143 pos--;
18144 bytepos = CHAR_TO_BYTE (pos);
18145 }
18146 while ((c = FETCH_BYTE (bytepos)) == '\n'
18147 || c == ' ' || c == '\t' || c == '\f')
18148 {
18149 if (bytepos <= BEGV_BYTE)
18150 break;
18151 bytepos--;
18152 pos--;
18153 }
18154 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18155 bytepos--;
18156 itb.charpos = pos;
18157 itb.bytepos = bytepos;
18158 itb.first_elt = 1;
18159 itb.separator_limit = -1;
18160 itb.paragraph_dir = NEUTRAL_DIR;
18161
18162 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18163 if (buf != current_buffer)
18164 set_buffer_temp (old);
18165 switch (itb.paragraph_dir)
18166 {
18167 case L2R:
18168 return Qleft_to_right;
18169 break;
18170 case R2L:
18171 return Qright_to_left;
18172 break;
18173 default:
18174 abort ();
18175 }
18176 }
18177 }
18178
18179
18180 \f
18181 /***********************************************************************
18182 Menu Bar
18183 ***********************************************************************/
18184
18185 /* Redisplay the menu bar in the frame for window W.
18186
18187 The menu bar of X frames that don't have X toolkit support is
18188 displayed in a special window W->frame->menu_bar_window.
18189
18190 The menu bar of terminal frames is treated specially as far as
18191 glyph matrices are concerned. Menu bar lines are not part of
18192 windows, so the update is done directly on the frame matrix rows
18193 for the menu bar. */
18194
18195 static void
18196 display_menu_bar (struct window *w)
18197 {
18198 struct frame *f = XFRAME (WINDOW_FRAME (w));
18199 struct it it;
18200 Lisp_Object items;
18201 int i;
18202
18203 /* Don't do all this for graphical frames. */
18204 #ifdef HAVE_NTGUI
18205 if (FRAME_W32_P (f))
18206 return;
18207 #endif
18208 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18209 if (FRAME_X_P (f))
18210 return;
18211 #endif
18212
18213 #ifdef HAVE_NS
18214 if (FRAME_NS_P (f))
18215 return;
18216 #endif /* HAVE_NS */
18217
18218 #ifdef USE_X_TOOLKIT
18219 xassert (!FRAME_WINDOW_P (f));
18220 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18221 it.first_visible_x = 0;
18222 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18223 #else /* not USE_X_TOOLKIT */
18224 if (FRAME_WINDOW_P (f))
18225 {
18226 /* Menu bar lines are displayed in the desired matrix of the
18227 dummy window menu_bar_window. */
18228 struct window *menu_w;
18229 xassert (WINDOWP (f->menu_bar_window));
18230 menu_w = XWINDOW (f->menu_bar_window);
18231 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18232 MENU_FACE_ID);
18233 it.first_visible_x = 0;
18234 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18235 }
18236 else
18237 {
18238 /* This is a TTY frame, i.e. character hpos/vpos are used as
18239 pixel x/y. */
18240 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18241 MENU_FACE_ID);
18242 it.first_visible_x = 0;
18243 it.last_visible_x = FRAME_COLS (f);
18244 }
18245 #endif /* not USE_X_TOOLKIT */
18246
18247 if (! mode_line_inverse_video)
18248 /* Force the menu-bar to be displayed in the default face. */
18249 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18250
18251 /* Clear all rows of the menu bar. */
18252 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18253 {
18254 struct glyph_row *row = it.glyph_row + i;
18255 clear_glyph_row (row);
18256 row->enabled_p = 1;
18257 row->full_width_p = 1;
18258 }
18259
18260 /* Display all items of the menu bar. */
18261 items = FRAME_MENU_BAR_ITEMS (it.f);
18262 for (i = 0; i < XVECTOR (items)->size; i += 4)
18263 {
18264 Lisp_Object string;
18265
18266 /* Stop at nil string. */
18267 string = AREF (items, i + 1);
18268 if (NILP (string))
18269 break;
18270
18271 /* Remember where item was displayed. */
18272 ASET (items, i + 3, make_number (it.hpos));
18273
18274 /* Display the item, pad with one space. */
18275 if (it.current_x < it.last_visible_x)
18276 display_string (NULL, string, Qnil, 0, 0, &it,
18277 SCHARS (string) + 1, 0, 0, -1);
18278 }
18279
18280 /* Fill out the line with spaces. */
18281 if (it.current_x < it.last_visible_x)
18282 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18283
18284 /* Compute the total height of the lines. */
18285 compute_line_metrics (&it);
18286 }
18287
18288
18289 \f
18290 /***********************************************************************
18291 Mode Line
18292 ***********************************************************************/
18293
18294 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18295 FORCE is non-zero, redisplay mode lines unconditionally.
18296 Otherwise, redisplay only mode lines that are garbaged. Value is
18297 the number of windows whose mode lines were redisplayed. */
18298
18299 static int
18300 redisplay_mode_lines (Lisp_Object window, int force)
18301 {
18302 int nwindows = 0;
18303
18304 while (!NILP (window))
18305 {
18306 struct window *w = XWINDOW (window);
18307
18308 if (WINDOWP (w->hchild))
18309 nwindows += redisplay_mode_lines (w->hchild, force);
18310 else if (WINDOWP (w->vchild))
18311 nwindows += redisplay_mode_lines (w->vchild, force);
18312 else if (force
18313 || FRAME_GARBAGED_P (XFRAME (w->frame))
18314 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18315 {
18316 struct text_pos lpoint;
18317 struct buffer *old = current_buffer;
18318
18319 /* Set the window's buffer for the mode line display. */
18320 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18321 set_buffer_internal_1 (XBUFFER (w->buffer));
18322
18323 /* Point refers normally to the selected window. For any
18324 other window, set up appropriate value. */
18325 if (!EQ (window, selected_window))
18326 {
18327 struct text_pos pt;
18328
18329 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18330 if (CHARPOS (pt) < BEGV)
18331 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18332 else if (CHARPOS (pt) > (ZV - 1))
18333 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18334 else
18335 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18336 }
18337
18338 /* Display mode lines. */
18339 clear_glyph_matrix (w->desired_matrix);
18340 if (display_mode_lines (w))
18341 {
18342 ++nwindows;
18343 w->must_be_updated_p = 1;
18344 }
18345
18346 /* Restore old settings. */
18347 set_buffer_internal_1 (old);
18348 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18349 }
18350
18351 window = w->next;
18352 }
18353
18354 return nwindows;
18355 }
18356
18357
18358 /* Display the mode and/or header line of window W. Value is the
18359 sum number of mode lines and header lines displayed. */
18360
18361 static int
18362 display_mode_lines (struct window *w)
18363 {
18364 Lisp_Object old_selected_window, old_selected_frame;
18365 int n = 0;
18366
18367 old_selected_frame = selected_frame;
18368 selected_frame = w->frame;
18369 old_selected_window = selected_window;
18370 XSETWINDOW (selected_window, w);
18371
18372 /* These will be set while the mode line specs are processed. */
18373 line_number_displayed = 0;
18374 w->column_number_displayed = Qnil;
18375
18376 if (WINDOW_WANTS_MODELINE_P (w))
18377 {
18378 struct window *sel_w = XWINDOW (old_selected_window);
18379
18380 /* Select mode line face based on the real selected window. */
18381 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18382 current_buffer->mode_line_format);
18383 ++n;
18384 }
18385
18386 if (WINDOW_WANTS_HEADER_LINE_P (w))
18387 {
18388 display_mode_line (w, HEADER_LINE_FACE_ID,
18389 current_buffer->header_line_format);
18390 ++n;
18391 }
18392
18393 selected_frame = old_selected_frame;
18394 selected_window = old_selected_window;
18395 return n;
18396 }
18397
18398
18399 /* Display mode or header line of window W. FACE_ID specifies which
18400 line to display; it is either MODE_LINE_FACE_ID or
18401 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18402 display. Value is the pixel height of the mode/header line
18403 displayed. */
18404
18405 static int
18406 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18407 {
18408 struct it it;
18409 struct face *face;
18410 int count = SPECPDL_INDEX ();
18411
18412 init_iterator (&it, w, -1, -1, NULL, face_id);
18413 /* Don't extend on a previously drawn mode-line.
18414 This may happen if called from pos_visible_p. */
18415 it.glyph_row->enabled_p = 0;
18416 prepare_desired_row (it.glyph_row);
18417
18418 it.glyph_row->mode_line_p = 1;
18419
18420 if (! mode_line_inverse_video)
18421 /* Force the mode-line to be displayed in the default face. */
18422 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18423
18424 record_unwind_protect (unwind_format_mode_line,
18425 format_mode_line_unwind_data (NULL, Qnil, 0));
18426
18427 mode_line_target = MODE_LINE_DISPLAY;
18428
18429 /* Temporarily make frame's keyboard the current kboard so that
18430 kboard-local variables in the mode_line_format will get the right
18431 values. */
18432 push_kboard (FRAME_KBOARD (it.f));
18433 record_unwind_save_match_data ();
18434 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18435 pop_kboard ();
18436
18437 unbind_to (count, Qnil);
18438
18439 /* Fill up with spaces. */
18440 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18441
18442 compute_line_metrics (&it);
18443 it.glyph_row->full_width_p = 1;
18444 it.glyph_row->continued_p = 0;
18445 it.glyph_row->truncated_on_left_p = 0;
18446 it.glyph_row->truncated_on_right_p = 0;
18447
18448 /* Make a 3D mode-line have a shadow at its right end. */
18449 face = FACE_FROM_ID (it.f, face_id);
18450 extend_face_to_end_of_line (&it);
18451 if (face->box != FACE_NO_BOX)
18452 {
18453 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18454 + it.glyph_row->used[TEXT_AREA] - 1);
18455 last->right_box_line_p = 1;
18456 }
18457
18458 return it.glyph_row->height;
18459 }
18460
18461 /* Move element ELT in LIST to the front of LIST.
18462 Return the updated list. */
18463
18464 static Lisp_Object
18465 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18466 {
18467 register Lisp_Object tail, prev;
18468 register Lisp_Object tem;
18469
18470 tail = list;
18471 prev = Qnil;
18472 while (CONSP (tail))
18473 {
18474 tem = XCAR (tail);
18475
18476 if (EQ (elt, tem))
18477 {
18478 /* Splice out the link TAIL. */
18479 if (NILP (prev))
18480 list = XCDR (tail);
18481 else
18482 Fsetcdr (prev, XCDR (tail));
18483
18484 /* Now make it the first. */
18485 Fsetcdr (tail, list);
18486 return tail;
18487 }
18488 else
18489 prev = tail;
18490 tail = XCDR (tail);
18491 QUIT;
18492 }
18493
18494 /* Not found--return unchanged LIST. */
18495 return list;
18496 }
18497
18498 /* Contribute ELT to the mode line for window IT->w. How it
18499 translates into text depends on its data type.
18500
18501 IT describes the display environment in which we display, as usual.
18502
18503 DEPTH is the depth in recursion. It is used to prevent
18504 infinite recursion here.
18505
18506 FIELD_WIDTH is the number of characters the display of ELT should
18507 occupy in the mode line, and PRECISION is the maximum number of
18508 characters to display from ELT's representation. See
18509 display_string for details.
18510
18511 Returns the hpos of the end of the text generated by ELT.
18512
18513 PROPS is a property list to add to any string we encounter.
18514
18515 If RISKY is nonzero, remove (disregard) any properties in any string
18516 we encounter, and ignore :eval and :propertize.
18517
18518 The global variable `mode_line_target' determines whether the
18519 output is passed to `store_mode_line_noprop',
18520 `store_mode_line_string', or `display_string'. */
18521
18522 static int
18523 display_mode_element (struct it *it, int depth, int field_width, int precision,
18524 Lisp_Object elt, Lisp_Object props, int risky)
18525 {
18526 int n = 0, field, prec;
18527 int literal = 0;
18528
18529 tail_recurse:
18530 if (depth > 100)
18531 elt = build_string ("*too-deep*");
18532
18533 depth++;
18534
18535 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18536 {
18537 case Lisp_String:
18538 {
18539 /* A string: output it and check for %-constructs within it. */
18540 unsigned char c;
18541 EMACS_INT offset = 0;
18542
18543 if (SCHARS (elt) > 0
18544 && (!NILP (props) || risky))
18545 {
18546 Lisp_Object oprops, aelt;
18547 oprops = Ftext_properties_at (make_number (0), elt);
18548
18549 /* If the starting string's properties are not what
18550 we want, translate the string. Also, if the string
18551 is risky, do that anyway. */
18552
18553 if (NILP (Fequal (props, oprops)) || risky)
18554 {
18555 /* If the starting string has properties,
18556 merge the specified ones onto the existing ones. */
18557 if (! NILP (oprops) && !risky)
18558 {
18559 Lisp_Object tem;
18560
18561 oprops = Fcopy_sequence (oprops);
18562 tem = props;
18563 while (CONSP (tem))
18564 {
18565 oprops = Fplist_put (oprops, XCAR (tem),
18566 XCAR (XCDR (tem)));
18567 tem = XCDR (XCDR (tem));
18568 }
18569 props = oprops;
18570 }
18571
18572 aelt = Fassoc (elt, mode_line_proptrans_alist);
18573 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18574 {
18575 /* AELT is what we want. Move it to the front
18576 without consing. */
18577 elt = XCAR (aelt);
18578 mode_line_proptrans_alist
18579 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18580 }
18581 else
18582 {
18583 Lisp_Object tem;
18584
18585 /* If AELT has the wrong props, it is useless.
18586 so get rid of it. */
18587 if (! NILP (aelt))
18588 mode_line_proptrans_alist
18589 = Fdelq (aelt, mode_line_proptrans_alist);
18590
18591 elt = Fcopy_sequence (elt);
18592 Fset_text_properties (make_number (0), Flength (elt),
18593 props, elt);
18594 /* Add this item to mode_line_proptrans_alist. */
18595 mode_line_proptrans_alist
18596 = Fcons (Fcons (elt, props),
18597 mode_line_proptrans_alist);
18598 /* Truncate mode_line_proptrans_alist
18599 to at most 50 elements. */
18600 tem = Fnthcdr (make_number (50),
18601 mode_line_proptrans_alist);
18602 if (! NILP (tem))
18603 XSETCDR (tem, Qnil);
18604 }
18605 }
18606 }
18607
18608 offset = 0;
18609
18610 if (literal)
18611 {
18612 prec = precision - n;
18613 switch (mode_line_target)
18614 {
18615 case MODE_LINE_NOPROP:
18616 case MODE_LINE_TITLE:
18617 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18618 break;
18619 case MODE_LINE_STRING:
18620 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18621 break;
18622 case MODE_LINE_DISPLAY:
18623 n += display_string (NULL, elt, Qnil, 0, 0, it,
18624 0, prec, 0, STRING_MULTIBYTE (elt));
18625 break;
18626 }
18627
18628 break;
18629 }
18630
18631 /* Handle the non-literal case. */
18632
18633 while ((precision <= 0 || n < precision)
18634 && SREF (elt, offset) != 0
18635 && (mode_line_target != MODE_LINE_DISPLAY
18636 || it->current_x < it->last_visible_x))
18637 {
18638 EMACS_INT last_offset = offset;
18639
18640 /* Advance to end of string or next format specifier. */
18641 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18642 ;
18643
18644 if (offset - 1 != last_offset)
18645 {
18646 EMACS_INT nchars, nbytes;
18647
18648 /* Output to end of string or up to '%'. Field width
18649 is length of string. Don't output more than
18650 PRECISION allows us. */
18651 offset--;
18652
18653 prec = c_string_width (SDATA (elt) + last_offset,
18654 offset - last_offset, precision - n,
18655 &nchars, &nbytes);
18656
18657 switch (mode_line_target)
18658 {
18659 case MODE_LINE_NOPROP:
18660 case MODE_LINE_TITLE:
18661 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18662 break;
18663 case MODE_LINE_STRING:
18664 {
18665 EMACS_INT bytepos = last_offset;
18666 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18667 EMACS_INT endpos = (precision <= 0
18668 ? string_byte_to_char (elt, offset)
18669 : charpos + nchars);
18670
18671 n += store_mode_line_string (NULL,
18672 Fsubstring (elt, make_number (charpos),
18673 make_number (endpos)),
18674 0, 0, 0, Qnil);
18675 }
18676 break;
18677 case MODE_LINE_DISPLAY:
18678 {
18679 EMACS_INT bytepos = last_offset;
18680 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18681
18682 if (precision <= 0)
18683 nchars = string_byte_to_char (elt, offset) - charpos;
18684 n += display_string (NULL, elt, Qnil, 0, charpos,
18685 it, 0, nchars, 0,
18686 STRING_MULTIBYTE (elt));
18687 }
18688 break;
18689 }
18690 }
18691 else /* c == '%' */
18692 {
18693 EMACS_INT percent_position = offset;
18694
18695 /* Get the specified minimum width. Zero means
18696 don't pad. */
18697 field = 0;
18698 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18699 field = field * 10 + c - '0';
18700
18701 /* Don't pad beyond the total padding allowed. */
18702 if (field_width - n > 0 && field > field_width - n)
18703 field = field_width - n;
18704
18705 /* Note that either PRECISION <= 0 or N < PRECISION. */
18706 prec = precision - n;
18707
18708 if (c == 'M')
18709 n += display_mode_element (it, depth, field, prec,
18710 Vglobal_mode_string, props,
18711 risky);
18712 else if (c != 0)
18713 {
18714 int multibyte;
18715 EMACS_INT bytepos, charpos;
18716 const unsigned char *spec;
18717 Lisp_Object string;
18718
18719 bytepos = percent_position;
18720 charpos = (STRING_MULTIBYTE (elt)
18721 ? string_byte_to_char (elt, bytepos)
18722 : bytepos);
18723 spec = decode_mode_spec (it->w, c, field, prec, &string);
18724 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18725
18726 switch (mode_line_target)
18727 {
18728 case MODE_LINE_NOPROP:
18729 case MODE_LINE_TITLE:
18730 n += store_mode_line_noprop (spec, field, prec);
18731 break;
18732 case MODE_LINE_STRING:
18733 {
18734 int len = strlen (spec);
18735 Lisp_Object tem = make_string (spec, len);
18736 props = Ftext_properties_at (make_number (charpos), elt);
18737 /* Should only keep face property in props */
18738 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18739 }
18740 break;
18741 case MODE_LINE_DISPLAY:
18742 {
18743 int nglyphs_before, nwritten;
18744
18745 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18746 nwritten = display_string (spec, string, elt,
18747 charpos, 0, it,
18748 field, prec, 0,
18749 multibyte);
18750
18751 /* Assign to the glyphs written above the
18752 string where the `%x' came from, position
18753 of the `%'. */
18754 if (nwritten > 0)
18755 {
18756 struct glyph *glyph
18757 = (it->glyph_row->glyphs[TEXT_AREA]
18758 + nglyphs_before);
18759 int i;
18760
18761 for (i = 0; i < nwritten; ++i)
18762 {
18763 glyph[i].object = elt;
18764 glyph[i].charpos = charpos;
18765 }
18766
18767 n += nwritten;
18768 }
18769 }
18770 break;
18771 }
18772 }
18773 else /* c == 0 */
18774 break;
18775 }
18776 }
18777 }
18778 break;
18779
18780 case Lisp_Symbol:
18781 /* A symbol: process the value of the symbol recursively
18782 as if it appeared here directly. Avoid error if symbol void.
18783 Special case: if value of symbol is a string, output the string
18784 literally. */
18785 {
18786 register Lisp_Object tem;
18787
18788 /* If the variable is not marked as risky to set
18789 then its contents are risky to use. */
18790 if (NILP (Fget (elt, Qrisky_local_variable)))
18791 risky = 1;
18792
18793 tem = Fboundp (elt);
18794 if (!NILP (tem))
18795 {
18796 tem = Fsymbol_value (elt);
18797 /* If value is a string, output that string literally:
18798 don't check for % within it. */
18799 if (STRINGP (tem))
18800 literal = 1;
18801
18802 if (!EQ (tem, elt))
18803 {
18804 /* Give up right away for nil or t. */
18805 elt = tem;
18806 goto tail_recurse;
18807 }
18808 }
18809 }
18810 break;
18811
18812 case Lisp_Cons:
18813 {
18814 register Lisp_Object car, tem;
18815
18816 /* A cons cell: five distinct cases.
18817 If first element is :eval or :propertize, do something special.
18818 If first element is a string or a cons, process all the elements
18819 and effectively concatenate them.
18820 If first element is a negative number, truncate displaying cdr to
18821 at most that many characters. If positive, pad (with spaces)
18822 to at least that many characters.
18823 If first element is a symbol, process the cadr or caddr recursively
18824 according to whether the symbol's value is non-nil or nil. */
18825 car = XCAR (elt);
18826 if (EQ (car, QCeval))
18827 {
18828 /* An element of the form (:eval FORM) means evaluate FORM
18829 and use the result as mode line elements. */
18830
18831 if (risky)
18832 break;
18833
18834 if (CONSP (XCDR (elt)))
18835 {
18836 Lisp_Object spec;
18837 spec = safe_eval (XCAR (XCDR (elt)));
18838 n += display_mode_element (it, depth, field_width - n,
18839 precision - n, spec, props,
18840 risky);
18841 }
18842 }
18843 else if (EQ (car, QCpropertize))
18844 {
18845 /* An element of the form (:propertize ELT PROPS...)
18846 means display ELT but applying properties PROPS. */
18847
18848 if (risky)
18849 break;
18850
18851 if (CONSP (XCDR (elt)))
18852 n += display_mode_element (it, depth, field_width - n,
18853 precision - n, XCAR (XCDR (elt)),
18854 XCDR (XCDR (elt)), risky);
18855 }
18856 else if (SYMBOLP (car))
18857 {
18858 tem = Fboundp (car);
18859 elt = XCDR (elt);
18860 if (!CONSP (elt))
18861 goto invalid;
18862 /* elt is now the cdr, and we know it is a cons cell.
18863 Use its car if CAR has a non-nil value. */
18864 if (!NILP (tem))
18865 {
18866 tem = Fsymbol_value (car);
18867 if (!NILP (tem))
18868 {
18869 elt = XCAR (elt);
18870 goto tail_recurse;
18871 }
18872 }
18873 /* Symbol's value is nil (or symbol is unbound)
18874 Get the cddr of the original list
18875 and if possible find the caddr and use that. */
18876 elt = XCDR (elt);
18877 if (NILP (elt))
18878 break;
18879 else if (!CONSP (elt))
18880 goto invalid;
18881 elt = XCAR (elt);
18882 goto tail_recurse;
18883 }
18884 else if (INTEGERP (car))
18885 {
18886 register int lim = XINT (car);
18887 elt = XCDR (elt);
18888 if (lim < 0)
18889 {
18890 /* Negative int means reduce maximum width. */
18891 if (precision <= 0)
18892 precision = -lim;
18893 else
18894 precision = min (precision, -lim);
18895 }
18896 else if (lim > 0)
18897 {
18898 /* Padding specified. Don't let it be more than
18899 current maximum. */
18900 if (precision > 0)
18901 lim = min (precision, lim);
18902
18903 /* If that's more padding than already wanted, queue it.
18904 But don't reduce padding already specified even if
18905 that is beyond the current truncation point. */
18906 field_width = max (lim, field_width);
18907 }
18908 goto tail_recurse;
18909 }
18910 else if (STRINGP (car) || CONSP (car))
18911 {
18912 Lisp_Object halftail = elt;
18913 int len = 0;
18914
18915 while (CONSP (elt)
18916 && (precision <= 0 || n < precision))
18917 {
18918 n += display_mode_element (it, depth,
18919 /* Do padding only after the last
18920 element in the list. */
18921 (! CONSP (XCDR (elt))
18922 ? field_width - n
18923 : 0),
18924 precision - n, XCAR (elt),
18925 props, risky);
18926 elt = XCDR (elt);
18927 len++;
18928 if ((len & 1) == 0)
18929 halftail = XCDR (halftail);
18930 /* Check for cycle. */
18931 if (EQ (halftail, elt))
18932 break;
18933 }
18934 }
18935 }
18936 break;
18937
18938 default:
18939 invalid:
18940 elt = build_string ("*invalid*");
18941 goto tail_recurse;
18942 }
18943
18944 /* Pad to FIELD_WIDTH. */
18945 if (field_width > 0 && n < field_width)
18946 {
18947 switch (mode_line_target)
18948 {
18949 case MODE_LINE_NOPROP:
18950 case MODE_LINE_TITLE:
18951 n += store_mode_line_noprop ("", field_width - n, 0);
18952 break;
18953 case MODE_LINE_STRING:
18954 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18955 break;
18956 case MODE_LINE_DISPLAY:
18957 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18958 0, 0, 0);
18959 break;
18960 }
18961 }
18962
18963 return n;
18964 }
18965
18966 /* Store a mode-line string element in mode_line_string_list.
18967
18968 If STRING is non-null, display that C string. Otherwise, the Lisp
18969 string LISP_STRING is displayed.
18970
18971 FIELD_WIDTH is the minimum number of output glyphs to produce.
18972 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18973 with spaces. FIELD_WIDTH <= 0 means don't pad.
18974
18975 PRECISION is the maximum number of characters to output from
18976 STRING. PRECISION <= 0 means don't truncate the string.
18977
18978 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18979 properties to the string.
18980
18981 PROPS are the properties to add to the string.
18982 The mode_line_string_face face property is always added to the string.
18983 */
18984
18985 static int
18986 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18987 int field_width, int precision, Lisp_Object props)
18988 {
18989 EMACS_INT len;
18990 int n = 0;
18991
18992 if (string != NULL)
18993 {
18994 len = strlen (string);
18995 if (precision > 0 && len > precision)
18996 len = precision;
18997 lisp_string = make_string (string, len);
18998 if (NILP (props))
18999 props = mode_line_string_face_prop;
19000 else if (!NILP (mode_line_string_face))
19001 {
19002 Lisp_Object face = Fplist_get (props, Qface);
19003 props = Fcopy_sequence (props);
19004 if (NILP (face))
19005 face = mode_line_string_face;
19006 else
19007 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19008 props = Fplist_put (props, Qface, face);
19009 }
19010 Fadd_text_properties (make_number (0), make_number (len),
19011 props, lisp_string);
19012 }
19013 else
19014 {
19015 len = XFASTINT (Flength (lisp_string));
19016 if (precision > 0 && len > precision)
19017 {
19018 len = precision;
19019 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19020 precision = -1;
19021 }
19022 if (!NILP (mode_line_string_face))
19023 {
19024 Lisp_Object face;
19025 if (NILP (props))
19026 props = Ftext_properties_at (make_number (0), lisp_string);
19027 face = Fplist_get (props, Qface);
19028 if (NILP (face))
19029 face = mode_line_string_face;
19030 else
19031 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19032 props = Fcons (Qface, Fcons (face, Qnil));
19033 if (copy_string)
19034 lisp_string = Fcopy_sequence (lisp_string);
19035 }
19036 if (!NILP (props))
19037 Fadd_text_properties (make_number (0), make_number (len),
19038 props, lisp_string);
19039 }
19040
19041 if (len > 0)
19042 {
19043 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19044 n += len;
19045 }
19046
19047 if (field_width > len)
19048 {
19049 field_width -= len;
19050 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19051 if (!NILP (props))
19052 Fadd_text_properties (make_number (0), make_number (field_width),
19053 props, lisp_string);
19054 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19055 n += field_width;
19056 }
19057
19058 return n;
19059 }
19060
19061
19062 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19063 1, 4, 0,
19064 doc: /* Format a string out of a mode line format specification.
19065 First arg FORMAT specifies the mode line format (see `mode-line-format'
19066 for details) to use.
19067
19068 Optional second arg FACE specifies the face property to put
19069 on all characters for which no face is specified.
19070 The value t means whatever face the window's mode line currently uses
19071 \(either `mode-line' or `mode-line-inactive', depending).
19072 A value of nil means the default is no face property.
19073 If FACE is an integer, the value string has no text properties.
19074
19075 Optional third and fourth args WINDOW and BUFFER specify the window
19076 and buffer to use as the context for the formatting (defaults
19077 are the selected window and the window's buffer). */)
19078 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
19079 {
19080 struct it it;
19081 int len;
19082 struct window *w;
19083 struct buffer *old_buffer = NULL;
19084 int face_id = -1;
19085 int no_props = INTEGERP (face);
19086 int count = SPECPDL_INDEX ();
19087 Lisp_Object str;
19088 int string_start = 0;
19089
19090 if (NILP (window))
19091 window = selected_window;
19092 CHECK_WINDOW (window);
19093 w = XWINDOW (window);
19094
19095 if (NILP (buffer))
19096 buffer = w->buffer;
19097 CHECK_BUFFER (buffer);
19098
19099 /* Make formatting the modeline a non-op when noninteractive, otherwise
19100 there will be problems later caused by a partially initialized frame. */
19101 if (NILP (format) || noninteractive)
19102 return empty_unibyte_string;
19103
19104 if (no_props)
19105 face = Qnil;
19106
19107 if (!NILP (face))
19108 {
19109 if (EQ (face, Qt))
19110 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
19111 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
19112 }
19113
19114 if (face_id < 0)
19115 face_id = DEFAULT_FACE_ID;
19116
19117 if (XBUFFER (buffer) != current_buffer)
19118 old_buffer = current_buffer;
19119
19120 /* Save things including mode_line_proptrans_alist,
19121 and set that to nil so that we don't alter the outer value. */
19122 record_unwind_protect (unwind_format_mode_line,
19123 format_mode_line_unwind_data
19124 (old_buffer, selected_window, 1));
19125 mode_line_proptrans_alist = Qnil;
19126
19127 Fselect_window (window, Qt);
19128 if (old_buffer)
19129 set_buffer_internal_1 (XBUFFER (buffer));
19130
19131 init_iterator (&it, w, -1, -1, NULL, face_id);
19132
19133 if (no_props)
19134 {
19135 mode_line_target = MODE_LINE_NOPROP;
19136 mode_line_string_face_prop = Qnil;
19137 mode_line_string_list = Qnil;
19138 string_start = MODE_LINE_NOPROP_LEN (0);
19139 }
19140 else
19141 {
19142 mode_line_target = MODE_LINE_STRING;
19143 mode_line_string_list = Qnil;
19144 mode_line_string_face = face;
19145 mode_line_string_face_prop
19146 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19147 }
19148
19149 push_kboard (FRAME_KBOARD (it.f));
19150 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19151 pop_kboard ();
19152
19153 if (no_props)
19154 {
19155 len = MODE_LINE_NOPROP_LEN (string_start);
19156 str = make_string (mode_line_noprop_buf + string_start, len);
19157 }
19158 else
19159 {
19160 mode_line_string_list = Fnreverse (mode_line_string_list);
19161 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19162 empty_unibyte_string);
19163 }
19164
19165 unbind_to (count, Qnil);
19166 return str;
19167 }
19168
19169 /* Write a null-terminated, right justified decimal representation of
19170 the positive integer D to BUF using a minimal field width WIDTH. */
19171
19172 static void
19173 pint2str (register char *buf, register int width, register int d)
19174 {
19175 register char *p = buf;
19176
19177 if (d <= 0)
19178 *p++ = '0';
19179 else
19180 {
19181 while (d > 0)
19182 {
19183 *p++ = d % 10 + '0';
19184 d /= 10;
19185 }
19186 }
19187
19188 for (width -= (int) (p - buf); width > 0; --width)
19189 *p++ = ' ';
19190 *p-- = '\0';
19191 while (p > buf)
19192 {
19193 d = *buf;
19194 *buf++ = *p;
19195 *p-- = d;
19196 }
19197 }
19198
19199 /* Write a null-terminated, right justified decimal and "human
19200 readable" representation of the nonnegative integer D to BUF using
19201 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19202
19203 static const char power_letter[] =
19204 {
19205 0, /* not used */
19206 'k', /* kilo */
19207 'M', /* mega */
19208 'G', /* giga */
19209 'T', /* tera */
19210 'P', /* peta */
19211 'E', /* exa */
19212 'Z', /* zetta */
19213 'Y' /* yotta */
19214 };
19215
19216 static void
19217 pint2hrstr (char *buf, int width, int d)
19218 {
19219 /* We aim to represent the nonnegative integer D as
19220 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19221 int quotient = d;
19222 int remainder = 0;
19223 /* -1 means: do not use TENTHS. */
19224 int tenths = -1;
19225 int exponent = 0;
19226
19227 /* Length of QUOTIENT.TENTHS as a string. */
19228 int length;
19229
19230 char * psuffix;
19231 char * p;
19232
19233 if (1000 <= quotient)
19234 {
19235 /* Scale to the appropriate EXPONENT. */
19236 do
19237 {
19238 remainder = quotient % 1000;
19239 quotient /= 1000;
19240 exponent++;
19241 }
19242 while (1000 <= quotient);
19243
19244 /* Round to nearest and decide whether to use TENTHS or not. */
19245 if (quotient <= 9)
19246 {
19247 tenths = remainder / 100;
19248 if (50 <= remainder % 100)
19249 {
19250 if (tenths < 9)
19251 tenths++;
19252 else
19253 {
19254 quotient++;
19255 if (quotient == 10)
19256 tenths = -1;
19257 else
19258 tenths = 0;
19259 }
19260 }
19261 }
19262 else
19263 if (500 <= remainder)
19264 {
19265 if (quotient < 999)
19266 quotient++;
19267 else
19268 {
19269 quotient = 1;
19270 exponent++;
19271 tenths = 0;
19272 }
19273 }
19274 }
19275
19276 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19277 if (tenths == -1 && quotient <= 99)
19278 if (quotient <= 9)
19279 length = 1;
19280 else
19281 length = 2;
19282 else
19283 length = 3;
19284 p = psuffix = buf + max (width, length);
19285
19286 /* Print EXPONENT. */
19287 if (exponent)
19288 *psuffix++ = power_letter[exponent];
19289 *psuffix = '\0';
19290
19291 /* Print TENTHS. */
19292 if (tenths >= 0)
19293 {
19294 *--p = '0' + tenths;
19295 *--p = '.';
19296 }
19297
19298 /* Print QUOTIENT. */
19299 do
19300 {
19301 int digit = quotient % 10;
19302 *--p = '0' + digit;
19303 }
19304 while ((quotient /= 10) != 0);
19305
19306 /* Print leading spaces. */
19307 while (buf < p)
19308 *--p = ' ';
19309 }
19310
19311 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19312 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19313 type of CODING_SYSTEM. Return updated pointer into BUF. */
19314
19315 static unsigned char invalid_eol_type[] = "(*invalid*)";
19316
19317 static char *
19318 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19319 {
19320 Lisp_Object val;
19321 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19322 const unsigned char *eol_str;
19323 int eol_str_len;
19324 /* The EOL conversion we are using. */
19325 Lisp_Object eoltype;
19326
19327 val = CODING_SYSTEM_SPEC (coding_system);
19328 eoltype = Qnil;
19329
19330 if (!VECTORP (val)) /* Not yet decided. */
19331 {
19332 if (multibyte)
19333 *buf++ = '-';
19334 if (eol_flag)
19335 eoltype = eol_mnemonic_undecided;
19336 /* Don't mention EOL conversion if it isn't decided. */
19337 }
19338 else
19339 {
19340 Lisp_Object attrs;
19341 Lisp_Object eolvalue;
19342
19343 attrs = AREF (val, 0);
19344 eolvalue = AREF (val, 2);
19345
19346 if (multibyte)
19347 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19348
19349 if (eol_flag)
19350 {
19351 /* The EOL conversion that is normal on this system. */
19352
19353 if (NILP (eolvalue)) /* Not yet decided. */
19354 eoltype = eol_mnemonic_undecided;
19355 else if (VECTORP (eolvalue)) /* Not yet decided. */
19356 eoltype = eol_mnemonic_undecided;
19357 else /* eolvalue is Qunix, Qdos, or Qmac. */
19358 eoltype = (EQ (eolvalue, Qunix)
19359 ? eol_mnemonic_unix
19360 : (EQ (eolvalue, Qdos) == 1
19361 ? eol_mnemonic_dos : eol_mnemonic_mac));
19362 }
19363 }
19364
19365 if (eol_flag)
19366 {
19367 /* Mention the EOL conversion if it is not the usual one. */
19368 if (STRINGP (eoltype))
19369 {
19370 eol_str = SDATA (eoltype);
19371 eol_str_len = SBYTES (eoltype);
19372 }
19373 else if (CHARACTERP (eoltype))
19374 {
19375 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19376 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19377 eol_str = tmp;
19378 }
19379 else
19380 {
19381 eol_str = invalid_eol_type;
19382 eol_str_len = sizeof (invalid_eol_type) - 1;
19383 }
19384 memcpy (buf, eol_str, eol_str_len);
19385 buf += eol_str_len;
19386 }
19387
19388 return buf;
19389 }
19390
19391 /* Return a string for the output of a mode line %-spec for window W,
19392 generated by character C. PRECISION >= 0 means don't return a
19393 string longer than that value. FIELD_WIDTH > 0 means pad the
19394 string returned with spaces to that value. Return a Lisp string in
19395 *STRING if the resulting string is taken from that Lisp string.
19396
19397 Note we operate on the current buffer for most purposes,
19398 the exception being w->base_line_pos. */
19399
19400 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19401
19402 static const char *
19403 decode_mode_spec (struct window *w, register int c, int field_width,
19404 int precision, Lisp_Object *string)
19405 {
19406 Lisp_Object obj;
19407 struct frame *f = XFRAME (WINDOW_FRAME (w));
19408 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19409 struct buffer *b = current_buffer;
19410
19411 obj = Qnil;
19412 *string = Qnil;
19413
19414 switch (c)
19415 {
19416 case '*':
19417 if (!NILP (b->read_only))
19418 return "%";
19419 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19420 return "*";
19421 return "-";
19422
19423 case '+':
19424 /* This differs from %* only for a modified read-only buffer. */
19425 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19426 return "*";
19427 if (!NILP (b->read_only))
19428 return "%";
19429 return "-";
19430
19431 case '&':
19432 /* This differs from %* in ignoring read-only-ness. */
19433 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19434 return "*";
19435 return "-";
19436
19437 case '%':
19438 return "%";
19439
19440 case '[':
19441 {
19442 int i;
19443 char *p;
19444
19445 if (command_loop_level > 5)
19446 return "[[[... ";
19447 p = decode_mode_spec_buf;
19448 for (i = 0; i < command_loop_level; i++)
19449 *p++ = '[';
19450 *p = 0;
19451 return decode_mode_spec_buf;
19452 }
19453
19454 case ']':
19455 {
19456 int i;
19457 char *p;
19458
19459 if (command_loop_level > 5)
19460 return " ...]]]";
19461 p = decode_mode_spec_buf;
19462 for (i = 0; i < command_loop_level; i++)
19463 *p++ = ']';
19464 *p = 0;
19465 return decode_mode_spec_buf;
19466 }
19467
19468 case '-':
19469 {
19470 register int i;
19471
19472 /* Let lots_of_dashes be a string of infinite length. */
19473 if (mode_line_target == MODE_LINE_NOPROP ||
19474 mode_line_target == MODE_LINE_STRING)
19475 return "--";
19476 if (field_width <= 0
19477 || field_width > sizeof (lots_of_dashes))
19478 {
19479 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19480 decode_mode_spec_buf[i] = '-';
19481 decode_mode_spec_buf[i] = '\0';
19482 return decode_mode_spec_buf;
19483 }
19484 else
19485 return lots_of_dashes;
19486 }
19487
19488 case 'b':
19489 obj = b->name;
19490 break;
19491
19492 case 'c':
19493 /* %c and %l are ignored in `frame-title-format'.
19494 (In redisplay_internal, the frame title is drawn _before_ the
19495 windows are updated, so the stuff which depends on actual
19496 window contents (such as %l) may fail to render properly, or
19497 even crash emacs.) */
19498 if (mode_line_target == MODE_LINE_TITLE)
19499 return "";
19500 else
19501 {
19502 int col = (int) current_column (); /* iftc */
19503 w->column_number_displayed = make_number (col);
19504 pint2str (decode_mode_spec_buf, field_width, col);
19505 return decode_mode_spec_buf;
19506 }
19507
19508 case 'e':
19509 #ifndef SYSTEM_MALLOC
19510 {
19511 if (NILP (Vmemory_full))
19512 return "";
19513 else
19514 return "!MEM FULL! ";
19515 }
19516 #else
19517 return "";
19518 #endif
19519
19520 case 'F':
19521 /* %F displays the frame name. */
19522 if (!NILP (f->title))
19523 return (char *) SDATA (f->title);
19524 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19525 return (char *) SDATA (f->name);
19526 return "Emacs";
19527
19528 case 'f':
19529 obj = b->filename;
19530 break;
19531
19532 case 'i':
19533 {
19534 EMACS_INT size = ZV - BEGV;
19535 pint2str (decode_mode_spec_buf, field_width, size);
19536 return decode_mode_spec_buf;
19537 }
19538
19539 case 'I':
19540 {
19541 EMACS_INT size = ZV - BEGV;
19542 pint2hrstr (decode_mode_spec_buf, field_width, size);
19543 return decode_mode_spec_buf;
19544 }
19545
19546 case 'l':
19547 {
19548 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19549 int topline, nlines, height;
19550 EMACS_INT junk;
19551
19552 /* %c and %l are ignored in `frame-title-format'. */
19553 if (mode_line_target == MODE_LINE_TITLE)
19554 return "";
19555
19556 startpos = XMARKER (w->start)->charpos;
19557 startpos_byte = marker_byte_position (w->start);
19558 height = WINDOW_TOTAL_LINES (w);
19559
19560 /* If we decided that this buffer isn't suitable for line numbers,
19561 don't forget that too fast. */
19562 if (EQ (w->base_line_pos, w->buffer))
19563 goto no_value;
19564 /* But do forget it, if the window shows a different buffer now. */
19565 else if (BUFFERP (w->base_line_pos))
19566 w->base_line_pos = Qnil;
19567
19568 /* If the buffer is very big, don't waste time. */
19569 if (INTEGERP (Vline_number_display_limit)
19570 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19571 {
19572 w->base_line_pos = Qnil;
19573 w->base_line_number = Qnil;
19574 goto no_value;
19575 }
19576
19577 if (INTEGERP (w->base_line_number)
19578 && INTEGERP (w->base_line_pos)
19579 && XFASTINT (w->base_line_pos) <= startpos)
19580 {
19581 line = XFASTINT (w->base_line_number);
19582 linepos = XFASTINT (w->base_line_pos);
19583 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19584 }
19585 else
19586 {
19587 line = 1;
19588 linepos = BUF_BEGV (b);
19589 linepos_byte = BUF_BEGV_BYTE (b);
19590 }
19591
19592 /* Count lines from base line to window start position. */
19593 nlines = display_count_lines (linepos, linepos_byte,
19594 startpos_byte,
19595 startpos, &junk);
19596
19597 topline = nlines + line;
19598
19599 /* Determine a new base line, if the old one is too close
19600 or too far away, or if we did not have one.
19601 "Too close" means it's plausible a scroll-down would
19602 go back past it. */
19603 if (startpos == BUF_BEGV (b))
19604 {
19605 w->base_line_number = make_number (topline);
19606 w->base_line_pos = make_number (BUF_BEGV (b));
19607 }
19608 else if (nlines < height + 25 || nlines > height * 3 + 50
19609 || linepos == BUF_BEGV (b))
19610 {
19611 EMACS_INT limit = BUF_BEGV (b);
19612 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19613 EMACS_INT position;
19614 int distance = (height * 2 + 30) * line_number_display_limit_width;
19615
19616 if (startpos - distance > limit)
19617 {
19618 limit = startpos - distance;
19619 limit_byte = CHAR_TO_BYTE (limit);
19620 }
19621
19622 nlines = display_count_lines (startpos, startpos_byte,
19623 limit_byte,
19624 - (height * 2 + 30),
19625 &position);
19626 /* If we couldn't find the lines we wanted within
19627 line_number_display_limit_width chars per line,
19628 give up on line numbers for this window. */
19629 if (position == limit_byte && limit == startpos - distance)
19630 {
19631 w->base_line_pos = w->buffer;
19632 w->base_line_number = Qnil;
19633 goto no_value;
19634 }
19635
19636 w->base_line_number = make_number (topline - nlines);
19637 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19638 }
19639
19640 /* Now count lines from the start pos to point. */
19641 nlines = display_count_lines (startpos, startpos_byte,
19642 PT_BYTE, PT, &junk);
19643
19644 /* Record that we did display the line number. */
19645 line_number_displayed = 1;
19646
19647 /* Make the string to show. */
19648 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19649 return decode_mode_spec_buf;
19650 no_value:
19651 {
19652 char* p = decode_mode_spec_buf;
19653 int pad = field_width - 2;
19654 while (pad-- > 0)
19655 *p++ = ' ';
19656 *p++ = '?';
19657 *p++ = '?';
19658 *p = '\0';
19659 return decode_mode_spec_buf;
19660 }
19661 }
19662 break;
19663
19664 case 'm':
19665 obj = b->mode_name;
19666 break;
19667
19668 case 'n':
19669 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19670 return " Narrow";
19671 break;
19672
19673 case 'p':
19674 {
19675 EMACS_INT pos = marker_position (w->start);
19676 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19677
19678 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19679 {
19680 if (pos <= BUF_BEGV (b))
19681 return "All";
19682 else
19683 return "Bottom";
19684 }
19685 else if (pos <= BUF_BEGV (b))
19686 return "Top";
19687 else
19688 {
19689 if (total > 1000000)
19690 /* Do it differently for a large value, to avoid overflow. */
19691 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19692 else
19693 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19694 /* We can't normally display a 3-digit number,
19695 so get us a 2-digit number that is close. */
19696 if (total == 100)
19697 total = 99;
19698 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19699 return decode_mode_spec_buf;
19700 }
19701 }
19702
19703 /* Display percentage of size above the bottom of the screen. */
19704 case 'P':
19705 {
19706 EMACS_INT toppos = marker_position (w->start);
19707 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19708 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19709
19710 if (botpos >= BUF_ZV (b))
19711 {
19712 if (toppos <= BUF_BEGV (b))
19713 return "All";
19714 else
19715 return "Bottom";
19716 }
19717 else
19718 {
19719 if (total > 1000000)
19720 /* Do it differently for a large value, to avoid overflow. */
19721 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19722 else
19723 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19724 /* We can't normally display a 3-digit number,
19725 so get us a 2-digit number that is close. */
19726 if (total == 100)
19727 total = 99;
19728 if (toppos <= BUF_BEGV (b))
19729 sprintf (decode_mode_spec_buf, "Top%2ld%%", (long)total);
19730 else
19731 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19732 return decode_mode_spec_buf;
19733 }
19734 }
19735
19736 case 's':
19737 /* status of process */
19738 obj = Fget_buffer_process (Fcurrent_buffer ());
19739 if (NILP (obj))
19740 return "no process";
19741 #ifndef MSDOS
19742 obj = Fsymbol_name (Fprocess_status (obj));
19743 #endif
19744 break;
19745
19746 case '@':
19747 {
19748 int count = inhibit_garbage_collection ();
19749 Lisp_Object val = call1 (intern ("file-remote-p"),
19750 current_buffer->directory);
19751 unbind_to (count, Qnil);
19752
19753 if (NILP (val))
19754 return "-";
19755 else
19756 return "@";
19757 }
19758
19759 case 't': /* indicate TEXT or BINARY */
19760 #ifdef MODE_LINE_BINARY_TEXT
19761 return MODE_LINE_BINARY_TEXT (b);
19762 #else
19763 return "T";
19764 #endif
19765
19766 case 'z':
19767 /* coding-system (not including end-of-line format) */
19768 case 'Z':
19769 /* coding-system (including end-of-line type) */
19770 {
19771 int eol_flag = (c == 'Z');
19772 char *p = decode_mode_spec_buf;
19773
19774 if (! FRAME_WINDOW_P (f))
19775 {
19776 /* No need to mention EOL here--the terminal never needs
19777 to do EOL conversion. */
19778 p = decode_mode_spec_coding (CODING_ID_NAME
19779 (FRAME_KEYBOARD_CODING (f)->id),
19780 p, 0);
19781 p = decode_mode_spec_coding (CODING_ID_NAME
19782 (FRAME_TERMINAL_CODING (f)->id),
19783 p, 0);
19784 }
19785 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19786 p, eol_flag);
19787
19788 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19789 #ifdef subprocesses
19790 obj = Fget_buffer_process (Fcurrent_buffer ());
19791 if (PROCESSP (obj))
19792 {
19793 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19794 p, eol_flag);
19795 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19796 p, eol_flag);
19797 }
19798 #endif /* subprocesses */
19799 #endif /* 0 */
19800 *p = 0;
19801 return decode_mode_spec_buf;
19802 }
19803 }
19804
19805 if (STRINGP (obj))
19806 {
19807 *string = obj;
19808 return (char *) SDATA (obj);
19809 }
19810 else
19811 return "";
19812 }
19813
19814
19815 /* Count up to COUNT lines starting from START / START_BYTE.
19816 But don't go beyond LIMIT_BYTE.
19817 Return the number of lines thus found (always nonnegative).
19818
19819 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19820
19821 static int
19822 display_count_lines (EMACS_INT start, EMACS_INT start_byte,
19823 EMACS_INT limit_byte, int count,
19824 EMACS_INT *byte_pos_ptr)
19825 {
19826 register unsigned char *cursor;
19827 unsigned char *base;
19828
19829 register int ceiling;
19830 register unsigned char *ceiling_addr;
19831 int orig_count = count;
19832
19833 /* If we are not in selective display mode,
19834 check only for newlines. */
19835 int selective_display = (!NILP (current_buffer->selective_display)
19836 && !INTEGERP (current_buffer->selective_display));
19837
19838 if (count > 0)
19839 {
19840 while (start_byte < limit_byte)
19841 {
19842 ceiling = BUFFER_CEILING_OF (start_byte);
19843 ceiling = min (limit_byte - 1, ceiling);
19844 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19845 base = (cursor = BYTE_POS_ADDR (start_byte));
19846 while (1)
19847 {
19848 if (selective_display)
19849 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19850 ;
19851 else
19852 while (*cursor != '\n' && ++cursor != ceiling_addr)
19853 ;
19854
19855 if (cursor != ceiling_addr)
19856 {
19857 if (--count == 0)
19858 {
19859 start_byte += cursor - base + 1;
19860 *byte_pos_ptr = start_byte;
19861 return orig_count;
19862 }
19863 else
19864 if (++cursor == ceiling_addr)
19865 break;
19866 }
19867 else
19868 break;
19869 }
19870 start_byte += cursor - base;
19871 }
19872 }
19873 else
19874 {
19875 while (start_byte > limit_byte)
19876 {
19877 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19878 ceiling = max (limit_byte, ceiling);
19879 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19880 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19881 while (1)
19882 {
19883 if (selective_display)
19884 while (--cursor != ceiling_addr
19885 && *cursor != '\n' && *cursor != 015)
19886 ;
19887 else
19888 while (--cursor != ceiling_addr && *cursor != '\n')
19889 ;
19890
19891 if (cursor != ceiling_addr)
19892 {
19893 if (++count == 0)
19894 {
19895 start_byte += cursor - base + 1;
19896 *byte_pos_ptr = start_byte;
19897 /* When scanning backwards, we should
19898 not count the newline posterior to which we stop. */
19899 return - orig_count - 1;
19900 }
19901 }
19902 else
19903 break;
19904 }
19905 /* Here we add 1 to compensate for the last decrement
19906 of CURSOR, which took it past the valid range. */
19907 start_byte += cursor - base + 1;
19908 }
19909 }
19910
19911 *byte_pos_ptr = limit_byte;
19912
19913 if (count < 0)
19914 return - orig_count + count;
19915 return orig_count - count;
19916
19917 }
19918
19919
19920 \f
19921 /***********************************************************************
19922 Displaying strings
19923 ***********************************************************************/
19924
19925 /* Display a NUL-terminated string, starting with index START.
19926
19927 If STRING is non-null, display that C string. Otherwise, the Lisp
19928 string LISP_STRING is displayed. There's a case that STRING is
19929 non-null and LISP_STRING is not nil. It means STRING is a string
19930 data of LISP_STRING. In that case, we display LISP_STRING while
19931 ignoring its text properties.
19932
19933 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19934 FACE_STRING. Display STRING or LISP_STRING with the face at
19935 FACE_STRING_POS in FACE_STRING:
19936
19937 Display the string in the environment given by IT, but use the
19938 standard display table, temporarily.
19939
19940 FIELD_WIDTH is the minimum number of output glyphs to produce.
19941 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19942 with spaces. If STRING has more characters, more than FIELD_WIDTH
19943 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19944
19945 PRECISION is the maximum number of characters to output from
19946 STRING. PRECISION < 0 means don't truncate the string.
19947
19948 This is roughly equivalent to printf format specifiers:
19949
19950 FIELD_WIDTH PRECISION PRINTF
19951 ----------------------------------------
19952 -1 -1 %s
19953 -1 10 %.10s
19954 10 -1 %10s
19955 20 10 %20.10s
19956
19957 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19958 display them, and < 0 means obey the current buffer's value of
19959 enable_multibyte_characters.
19960
19961 Value is the number of columns displayed. */
19962
19963 static int
19964 display_string (const unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19965 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19966 int field_width, int precision, int max_x, int multibyte)
19967 {
19968 int hpos_at_start = it->hpos;
19969 int saved_face_id = it->face_id;
19970 struct glyph_row *row = it->glyph_row;
19971
19972 /* Initialize the iterator IT for iteration over STRING beginning
19973 with index START. */
19974 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19975 precision, field_width, multibyte);
19976 if (string && STRINGP (lisp_string))
19977 /* LISP_STRING is the one returned by decode_mode_spec. We should
19978 ignore its text properties. */
19979 it->stop_charpos = -1;
19980
19981 /* If displaying STRING, set up the face of the iterator
19982 from LISP_STRING, if that's given. */
19983 if (STRINGP (face_string))
19984 {
19985 EMACS_INT endptr;
19986 struct face *face;
19987
19988 it->face_id
19989 = face_at_string_position (it->w, face_string, face_string_pos,
19990 0, it->region_beg_charpos,
19991 it->region_end_charpos,
19992 &endptr, it->base_face_id, 0);
19993 face = FACE_FROM_ID (it->f, it->face_id);
19994 it->face_box_p = face->box != FACE_NO_BOX;
19995 }
19996
19997 /* Set max_x to the maximum allowed X position. Don't let it go
19998 beyond the right edge of the window. */
19999 if (max_x <= 0)
20000 max_x = it->last_visible_x;
20001 else
20002 max_x = min (max_x, it->last_visible_x);
20003
20004 /* Skip over display elements that are not visible. because IT->w is
20005 hscrolled. */
20006 if (it->current_x < it->first_visible_x)
20007 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20008 MOVE_TO_POS | MOVE_TO_X);
20009
20010 row->ascent = it->max_ascent;
20011 row->height = it->max_ascent + it->max_descent;
20012 row->phys_ascent = it->max_phys_ascent;
20013 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20014 row->extra_line_spacing = it->max_extra_line_spacing;
20015
20016 /* This condition is for the case that we are called with current_x
20017 past last_visible_x. */
20018 while (it->current_x < max_x)
20019 {
20020 int x_before, x, n_glyphs_before, i, nglyphs;
20021
20022 /* Get the next display element. */
20023 if (!get_next_display_element (it))
20024 break;
20025
20026 /* Produce glyphs. */
20027 x_before = it->current_x;
20028 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
20029 PRODUCE_GLYPHS (it);
20030
20031 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
20032 i = 0;
20033 x = x_before;
20034 while (i < nglyphs)
20035 {
20036 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20037
20038 if (it->line_wrap != TRUNCATE
20039 && x + glyph->pixel_width > max_x)
20040 {
20041 /* End of continued line or max_x reached. */
20042 if (CHAR_GLYPH_PADDING_P (*glyph))
20043 {
20044 /* A wide character is unbreakable. */
20045 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
20046 it->current_x = x_before;
20047 }
20048 else
20049 {
20050 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
20051 it->current_x = x;
20052 }
20053 break;
20054 }
20055 else if (x + glyph->pixel_width >= it->first_visible_x)
20056 {
20057 /* Glyph is at least partially visible. */
20058 ++it->hpos;
20059 if (x < it->first_visible_x)
20060 it->glyph_row->x = x - it->first_visible_x;
20061 }
20062 else
20063 {
20064 /* Glyph is off the left margin of the display area.
20065 Should not happen. */
20066 abort ();
20067 }
20068
20069 row->ascent = max (row->ascent, it->max_ascent);
20070 row->height = max (row->height, it->max_ascent + it->max_descent);
20071 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20072 row->phys_height = max (row->phys_height,
20073 it->max_phys_ascent + it->max_phys_descent);
20074 row->extra_line_spacing = max (row->extra_line_spacing,
20075 it->max_extra_line_spacing);
20076 x += glyph->pixel_width;
20077 ++i;
20078 }
20079
20080 /* Stop if max_x reached. */
20081 if (i < nglyphs)
20082 break;
20083
20084 /* Stop at line ends. */
20085 if (ITERATOR_AT_END_OF_LINE_P (it))
20086 {
20087 it->continuation_lines_width = 0;
20088 break;
20089 }
20090
20091 set_iterator_to_next (it, 1);
20092
20093 /* Stop if truncating at the right edge. */
20094 if (it->line_wrap == TRUNCATE
20095 && it->current_x >= it->last_visible_x)
20096 {
20097 /* Add truncation mark, but don't do it if the line is
20098 truncated at a padding space. */
20099 if (IT_CHARPOS (*it) < it->string_nchars)
20100 {
20101 if (!FRAME_WINDOW_P (it->f))
20102 {
20103 int i, n;
20104
20105 if (it->current_x > it->last_visible_x)
20106 {
20107 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20108 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20109 break;
20110 for (n = row->used[TEXT_AREA]; i < n; ++i)
20111 {
20112 row->used[TEXT_AREA] = i;
20113 produce_special_glyphs (it, IT_TRUNCATION);
20114 }
20115 }
20116 produce_special_glyphs (it, IT_TRUNCATION);
20117 }
20118 it->glyph_row->truncated_on_right_p = 1;
20119 }
20120 break;
20121 }
20122 }
20123
20124 /* Maybe insert a truncation at the left. */
20125 if (it->first_visible_x
20126 && IT_CHARPOS (*it) > 0)
20127 {
20128 if (!FRAME_WINDOW_P (it->f))
20129 insert_left_trunc_glyphs (it);
20130 it->glyph_row->truncated_on_left_p = 1;
20131 }
20132
20133 it->face_id = saved_face_id;
20134
20135 /* Value is number of columns displayed. */
20136 return it->hpos - hpos_at_start;
20137 }
20138
20139
20140 \f
20141 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20142 appears as an element of LIST or as the car of an element of LIST.
20143 If PROPVAL is a list, compare each element against LIST in that
20144 way, and return 1/2 if any element of PROPVAL is found in LIST.
20145 Otherwise return 0. This function cannot quit.
20146 The return value is 2 if the text is invisible but with an ellipsis
20147 and 1 if it's invisible and without an ellipsis. */
20148
20149 int
20150 invisible_p (register Lisp_Object propval, Lisp_Object list)
20151 {
20152 register Lisp_Object tail, proptail;
20153
20154 for (tail = list; CONSP (tail); tail = XCDR (tail))
20155 {
20156 register Lisp_Object tem;
20157 tem = XCAR (tail);
20158 if (EQ (propval, tem))
20159 return 1;
20160 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20161 return NILP (XCDR (tem)) ? 1 : 2;
20162 }
20163
20164 if (CONSP (propval))
20165 {
20166 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20167 {
20168 Lisp_Object propelt;
20169 propelt = XCAR (proptail);
20170 for (tail = list; CONSP (tail); tail = XCDR (tail))
20171 {
20172 register Lisp_Object tem;
20173 tem = XCAR (tail);
20174 if (EQ (propelt, tem))
20175 return 1;
20176 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20177 return NILP (XCDR (tem)) ? 1 : 2;
20178 }
20179 }
20180 }
20181
20182 return 0;
20183 }
20184
20185 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20186 doc: /* Non-nil if the property makes the text invisible.
20187 POS-OR-PROP can be a marker or number, in which case it is taken to be
20188 a position in the current buffer and the value of the `invisible' property
20189 is checked; or it can be some other value, which is then presumed to be the
20190 value of the `invisible' property of the text of interest.
20191 The non-nil value returned can be t for truly invisible text or something
20192 else if the text is replaced by an ellipsis. */)
20193 (Lisp_Object pos_or_prop)
20194 {
20195 Lisp_Object prop
20196 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20197 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20198 : pos_or_prop);
20199 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20200 return (invis == 0 ? Qnil
20201 : invis == 1 ? Qt
20202 : make_number (invis));
20203 }
20204
20205 /* Calculate a width or height in pixels from a specification using
20206 the following elements:
20207
20208 SPEC ::=
20209 NUM - a (fractional) multiple of the default font width/height
20210 (NUM) - specifies exactly NUM pixels
20211 UNIT - a fixed number of pixels, see below.
20212 ELEMENT - size of a display element in pixels, see below.
20213 (NUM . SPEC) - equals NUM * SPEC
20214 (+ SPEC SPEC ...) - add pixel values
20215 (- SPEC SPEC ...) - subtract pixel values
20216 (- SPEC) - negate pixel value
20217
20218 NUM ::=
20219 INT or FLOAT - a number constant
20220 SYMBOL - use symbol's (buffer local) variable binding.
20221
20222 UNIT ::=
20223 in - pixels per inch *)
20224 mm - pixels per 1/1000 meter *)
20225 cm - pixels per 1/100 meter *)
20226 width - width of current font in pixels.
20227 height - height of current font in pixels.
20228
20229 *) using the ratio(s) defined in display-pixels-per-inch.
20230
20231 ELEMENT ::=
20232
20233 left-fringe - left fringe width in pixels
20234 right-fringe - right fringe width in pixels
20235
20236 left-margin - left margin width in pixels
20237 right-margin - right margin width in pixels
20238
20239 scroll-bar - scroll-bar area width in pixels
20240
20241 Examples:
20242
20243 Pixels corresponding to 5 inches:
20244 (5 . in)
20245
20246 Total width of non-text areas on left side of window (if scroll-bar is on left):
20247 '(space :width (+ left-fringe left-margin scroll-bar))
20248
20249 Align to first text column (in header line):
20250 '(space :align-to 0)
20251
20252 Align to middle of text area minus half the width of variable `my-image'
20253 containing a loaded image:
20254 '(space :align-to (0.5 . (- text my-image)))
20255
20256 Width of left margin minus width of 1 character in the default font:
20257 '(space :width (- left-margin 1))
20258
20259 Width of left margin minus width of 2 characters in the current font:
20260 '(space :width (- left-margin (2 . width)))
20261
20262 Center 1 character over left-margin (in header line):
20263 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20264
20265 Different ways to express width of left fringe plus left margin minus one pixel:
20266 '(space :width (- (+ left-fringe left-margin) (1)))
20267 '(space :width (+ left-fringe left-margin (- (1))))
20268 '(space :width (+ left-fringe left-margin (-1)))
20269
20270 */
20271
20272 #define NUMVAL(X) \
20273 ((INTEGERP (X) || FLOATP (X)) \
20274 ? XFLOATINT (X) \
20275 : - 1)
20276
20277 int
20278 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20279 struct font *font, int width_p, int *align_to)
20280 {
20281 double pixels;
20282
20283 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20284 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20285
20286 if (NILP (prop))
20287 return OK_PIXELS (0);
20288
20289 xassert (FRAME_LIVE_P (it->f));
20290
20291 if (SYMBOLP (prop))
20292 {
20293 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20294 {
20295 char *unit = SDATA (SYMBOL_NAME (prop));
20296
20297 if (unit[0] == 'i' && unit[1] == 'n')
20298 pixels = 1.0;
20299 else if (unit[0] == 'm' && unit[1] == 'm')
20300 pixels = 25.4;
20301 else if (unit[0] == 'c' && unit[1] == 'm')
20302 pixels = 2.54;
20303 else
20304 pixels = 0;
20305 if (pixels > 0)
20306 {
20307 double ppi;
20308 #ifdef HAVE_WINDOW_SYSTEM
20309 if (FRAME_WINDOW_P (it->f)
20310 && (ppi = (width_p
20311 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20312 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20313 ppi > 0))
20314 return OK_PIXELS (ppi / pixels);
20315 #endif
20316
20317 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20318 || (CONSP (Vdisplay_pixels_per_inch)
20319 && (ppi = (width_p
20320 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20321 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20322 ppi > 0)))
20323 return OK_PIXELS (ppi / pixels);
20324
20325 return 0;
20326 }
20327 }
20328
20329 #ifdef HAVE_WINDOW_SYSTEM
20330 if (EQ (prop, Qheight))
20331 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20332 if (EQ (prop, Qwidth))
20333 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20334 #else
20335 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20336 return OK_PIXELS (1);
20337 #endif
20338
20339 if (EQ (prop, Qtext))
20340 return OK_PIXELS (width_p
20341 ? window_box_width (it->w, TEXT_AREA)
20342 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20343
20344 if (align_to && *align_to < 0)
20345 {
20346 *res = 0;
20347 if (EQ (prop, Qleft))
20348 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20349 if (EQ (prop, Qright))
20350 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20351 if (EQ (prop, Qcenter))
20352 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20353 + window_box_width (it->w, TEXT_AREA) / 2);
20354 if (EQ (prop, Qleft_fringe))
20355 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20356 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20357 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20358 if (EQ (prop, Qright_fringe))
20359 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20360 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20361 : window_box_right_offset (it->w, TEXT_AREA));
20362 if (EQ (prop, Qleft_margin))
20363 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20364 if (EQ (prop, Qright_margin))
20365 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20366 if (EQ (prop, Qscroll_bar))
20367 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20368 ? 0
20369 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20370 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20371 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20372 : 0)));
20373 }
20374 else
20375 {
20376 if (EQ (prop, Qleft_fringe))
20377 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20378 if (EQ (prop, Qright_fringe))
20379 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20380 if (EQ (prop, Qleft_margin))
20381 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20382 if (EQ (prop, Qright_margin))
20383 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20384 if (EQ (prop, Qscroll_bar))
20385 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20386 }
20387
20388 prop = Fbuffer_local_value (prop, it->w->buffer);
20389 }
20390
20391 if (INTEGERP (prop) || FLOATP (prop))
20392 {
20393 int base_unit = (width_p
20394 ? FRAME_COLUMN_WIDTH (it->f)
20395 : FRAME_LINE_HEIGHT (it->f));
20396 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20397 }
20398
20399 if (CONSP (prop))
20400 {
20401 Lisp_Object car = XCAR (prop);
20402 Lisp_Object cdr = XCDR (prop);
20403
20404 if (SYMBOLP (car))
20405 {
20406 #ifdef HAVE_WINDOW_SYSTEM
20407 if (FRAME_WINDOW_P (it->f)
20408 && valid_image_p (prop))
20409 {
20410 int id = lookup_image (it->f, prop);
20411 struct image *img = IMAGE_FROM_ID (it->f, id);
20412
20413 return OK_PIXELS (width_p ? img->width : img->height);
20414 }
20415 #endif
20416 if (EQ (car, Qplus) || EQ (car, Qminus))
20417 {
20418 int first = 1;
20419 double px;
20420
20421 pixels = 0;
20422 while (CONSP (cdr))
20423 {
20424 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20425 font, width_p, align_to))
20426 return 0;
20427 if (first)
20428 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20429 else
20430 pixels += px;
20431 cdr = XCDR (cdr);
20432 }
20433 if (EQ (car, Qminus))
20434 pixels = -pixels;
20435 return OK_PIXELS (pixels);
20436 }
20437
20438 car = Fbuffer_local_value (car, it->w->buffer);
20439 }
20440
20441 if (INTEGERP (car) || FLOATP (car))
20442 {
20443 double fact;
20444 pixels = XFLOATINT (car);
20445 if (NILP (cdr))
20446 return OK_PIXELS (pixels);
20447 if (calc_pixel_width_or_height (&fact, it, cdr,
20448 font, width_p, align_to))
20449 return OK_PIXELS (pixels * fact);
20450 return 0;
20451 }
20452
20453 return 0;
20454 }
20455
20456 return 0;
20457 }
20458
20459 \f
20460 /***********************************************************************
20461 Glyph Display
20462 ***********************************************************************/
20463
20464 #ifdef HAVE_WINDOW_SYSTEM
20465
20466 #if GLYPH_DEBUG
20467
20468 void
20469 dump_glyph_string (s)
20470 struct glyph_string *s;
20471 {
20472 fprintf (stderr, "glyph string\n");
20473 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20474 s->x, s->y, s->width, s->height);
20475 fprintf (stderr, " ybase = %d\n", s->ybase);
20476 fprintf (stderr, " hl = %d\n", s->hl);
20477 fprintf (stderr, " left overhang = %d, right = %d\n",
20478 s->left_overhang, s->right_overhang);
20479 fprintf (stderr, " nchars = %d\n", s->nchars);
20480 fprintf (stderr, " extends to end of line = %d\n",
20481 s->extends_to_end_of_line_p);
20482 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20483 fprintf (stderr, " bg width = %d\n", s->background_width);
20484 }
20485
20486 #endif /* GLYPH_DEBUG */
20487
20488 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20489 of XChar2b structures for S; it can't be allocated in
20490 init_glyph_string because it must be allocated via `alloca'. W
20491 is the window on which S is drawn. ROW and AREA are the glyph row
20492 and area within the row from which S is constructed. START is the
20493 index of the first glyph structure covered by S. HL is a
20494 face-override for drawing S. */
20495
20496 #ifdef HAVE_NTGUI
20497 #define OPTIONAL_HDC(hdc) HDC hdc,
20498 #define DECLARE_HDC(hdc) HDC hdc;
20499 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20500 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20501 #endif
20502
20503 #ifndef OPTIONAL_HDC
20504 #define OPTIONAL_HDC(hdc)
20505 #define DECLARE_HDC(hdc)
20506 #define ALLOCATE_HDC(hdc, f)
20507 #define RELEASE_HDC(hdc, f)
20508 #endif
20509
20510 static void
20511 init_glyph_string (struct glyph_string *s,
20512 OPTIONAL_HDC (hdc)
20513 XChar2b *char2b, struct window *w, struct glyph_row *row,
20514 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20515 {
20516 memset (s, 0, sizeof *s);
20517 s->w = w;
20518 s->f = XFRAME (w->frame);
20519 #ifdef HAVE_NTGUI
20520 s->hdc = hdc;
20521 #endif
20522 s->display = FRAME_X_DISPLAY (s->f);
20523 s->window = FRAME_X_WINDOW (s->f);
20524 s->char2b = char2b;
20525 s->hl = hl;
20526 s->row = row;
20527 s->area = area;
20528 s->first_glyph = row->glyphs[area] + start;
20529 s->height = row->height;
20530 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20531 s->ybase = s->y + row->ascent;
20532 }
20533
20534
20535 /* Append the list of glyph strings with head H and tail T to the list
20536 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20537
20538 static INLINE void
20539 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20540 struct glyph_string *h, struct glyph_string *t)
20541 {
20542 if (h)
20543 {
20544 if (*head)
20545 (*tail)->next = h;
20546 else
20547 *head = h;
20548 h->prev = *tail;
20549 *tail = t;
20550 }
20551 }
20552
20553
20554 /* Prepend the list of glyph strings with head H and tail T to the
20555 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20556 result. */
20557
20558 static INLINE void
20559 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20560 struct glyph_string *h, struct glyph_string *t)
20561 {
20562 if (h)
20563 {
20564 if (*head)
20565 (*head)->prev = t;
20566 else
20567 *tail = t;
20568 t->next = *head;
20569 *head = h;
20570 }
20571 }
20572
20573
20574 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20575 Set *HEAD and *TAIL to the resulting list. */
20576
20577 static INLINE void
20578 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20579 struct glyph_string *s)
20580 {
20581 s->next = s->prev = NULL;
20582 append_glyph_string_lists (head, tail, s, s);
20583 }
20584
20585
20586 /* Get face and two-byte form of character C in face FACE_ID on frame
20587 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20588 means we want to display multibyte text. DISPLAY_P non-zero means
20589 make sure that X resources for the face returned are allocated.
20590 Value is a pointer to a realized face that is ready for display if
20591 DISPLAY_P is non-zero. */
20592
20593 static INLINE struct face *
20594 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20595 XChar2b *char2b, int multibyte_p, int display_p)
20596 {
20597 struct face *face = FACE_FROM_ID (f, face_id);
20598
20599 if (face->font)
20600 {
20601 unsigned code = face->font->driver->encode_char (face->font, c);
20602
20603 if (code != FONT_INVALID_CODE)
20604 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20605 else
20606 STORE_XCHAR2B (char2b, 0, 0);
20607 }
20608
20609 /* Make sure X resources of the face are allocated. */
20610 #ifdef HAVE_X_WINDOWS
20611 if (display_p)
20612 #endif
20613 {
20614 xassert (face != NULL);
20615 PREPARE_FACE_FOR_DISPLAY (f, face);
20616 }
20617
20618 return face;
20619 }
20620
20621
20622 /* Get face and two-byte form of character glyph GLYPH on frame F.
20623 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20624 a pointer to a realized face that is ready for display. */
20625
20626 static INLINE struct face *
20627 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20628 XChar2b *char2b, int *two_byte_p)
20629 {
20630 struct face *face;
20631
20632 xassert (glyph->type == CHAR_GLYPH);
20633 face = FACE_FROM_ID (f, glyph->face_id);
20634
20635 if (two_byte_p)
20636 *two_byte_p = 0;
20637
20638 if (face->font)
20639 {
20640 unsigned code;
20641
20642 if (CHAR_BYTE8_P (glyph->u.ch))
20643 code = CHAR_TO_BYTE8 (glyph->u.ch);
20644 else
20645 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20646
20647 if (code != FONT_INVALID_CODE)
20648 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20649 else
20650 STORE_XCHAR2B (char2b, 0, 0);
20651 }
20652
20653 /* Make sure X resources of the face are allocated. */
20654 xassert (face != NULL);
20655 PREPARE_FACE_FOR_DISPLAY (f, face);
20656 return face;
20657 }
20658
20659
20660 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20661 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20662
20663 static INLINE int
20664 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20665 {
20666 unsigned code;
20667
20668 if (CHAR_BYTE8_P (c))
20669 code = CHAR_TO_BYTE8 (c);
20670 else
20671 code = font->driver->encode_char (font, c);
20672
20673 if (code == FONT_INVALID_CODE)
20674 return 0;
20675 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20676 return 1;
20677 }
20678
20679
20680 /* Fill glyph string S with composition components specified by S->cmp.
20681
20682 BASE_FACE is the base face of the composition.
20683 S->cmp_from is the index of the first component for S.
20684
20685 OVERLAPS non-zero means S should draw the foreground only, and use
20686 its physical height for clipping. See also draw_glyphs.
20687
20688 Value is the index of a component not in S. */
20689
20690 static int
20691 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20692 int overlaps)
20693 {
20694 int i;
20695 /* For all glyphs of this composition, starting at the offset
20696 S->cmp_from, until we reach the end of the definition or encounter a
20697 glyph that requires the different face, add it to S. */
20698 struct face *face;
20699
20700 xassert (s);
20701
20702 s->for_overlaps = overlaps;
20703 s->face = NULL;
20704 s->font = NULL;
20705 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20706 {
20707 int c = COMPOSITION_GLYPH (s->cmp, i);
20708
20709 if (c != '\t')
20710 {
20711 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20712 -1, Qnil);
20713
20714 face = get_char_face_and_encoding (s->f, c, face_id,
20715 s->char2b + i, 1, 1);
20716 if (face)
20717 {
20718 if (! s->face)
20719 {
20720 s->face = face;
20721 s->font = s->face->font;
20722 }
20723 else if (s->face != face)
20724 break;
20725 }
20726 }
20727 ++s->nchars;
20728 }
20729 s->cmp_to = i;
20730
20731 /* All glyph strings for the same composition has the same width,
20732 i.e. the width set for the first component of the composition. */
20733 s->width = s->first_glyph->pixel_width;
20734
20735 /* If the specified font could not be loaded, use the frame's
20736 default font, but record the fact that we couldn't load it in
20737 the glyph string so that we can draw rectangles for the
20738 characters of the glyph string. */
20739 if (s->font == NULL)
20740 {
20741 s->font_not_found_p = 1;
20742 s->font = FRAME_FONT (s->f);
20743 }
20744
20745 /* Adjust base line for subscript/superscript text. */
20746 s->ybase += s->first_glyph->voffset;
20747
20748 /* This glyph string must always be drawn with 16-bit functions. */
20749 s->two_byte_p = 1;
20750
20751 return s->cmp_to;
20752 }
20753
20754 static int
20755 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20756 int start, int end, int overlaps)
20757 {
20758 struct glyph *glyph, *last;
20759 Lisp_Object lgstring;
20760 int i;
20761
20762 s->for_overlaps = overlaps;
20763 glyph = s->row->glyphs[s->area] + start;
20764 last = s->row->glyphs[s->area] + end;
20765 s->cmp_id = glyph->u.cmp.id;
20766 s->cmp_from = glyph->slice.cmp.from;
20767 s->cmp_to = glyph->slice.cmp.to + 1;
20768 s->face = FACE_FROM_ID (s->f, face_id);
20769 lgstring = composition_gstring_from_id (s->cmp_id);
20770 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20771 glyph++;
20772 while (glyph < last
20773 && glyph->u.cmp.automatic
20774 && glyph->u.cmp.id == s->cmp_id
20775 && s->cmp_to == glyph->slice.cmp.from)
20776 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20777
20778 for (i = s->cmp_from; i < s->cmp_to; i++)
20779 {
20780 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20781 unsigned code = LGLYPH_CODE (lglyph);
20782
20783 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20784 }
20785 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20786 return glyph - s->row->glyphs[s->area];
20787 }
20788
20789
20790 /* Fill glyph string S from a sequence glyphs for glyphless characters.
20791 See the comment of fill_glyph_string for arguments.
20792 Value is the index of the first glyph not in S. */
20793
20794
20795 static int
20796 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
20797 int start, int end, int overlaps)
20798 {
20799 struct glyph *glyph, *last;
20800 int voffset;
20801
20802 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
20803 s->for_overlaps = overlaps;
20804 glyph = s->row->glyphs[s->area] + start;
20805 last = s->row->glyphs[s->area] + end;
20806 voffset = glyph->voffset;
20807 s->face = FACE_FROM_ID (s->f, face_id);
20808 s->font = s->face->font;
20809 s->nchars = 1;
20810 s->width = glyph->pixel_width;
20811 glyph++;
20812 while (glyph < last
20813 && glyph->type == GLYPHLESS_GLYPH
20814 && glyph->voffset == voffset
20815 && glyph->face_id == face_id)
20816 {
20817 s->nchars++;
20818 s->width += glyph->pixel_width;
20819 glyph++;
20820 }
20821 s->ybase += voffset;
20822 return glyph - s->row->glyphs[s->area];
20823 }
20824
20825
20826 /* Fill glyph string S from a sequence of character glyphs.
20827
20828 FACE_ID is the face id of the string. START is the index of the
20829 first glyph to consider, END is the index of the last + 1.
20830 OVERLAPS non-zero means S should draw the foreground only, and use
20831 its physical height for clipping. See also draw_glyphs.
20832
20833 Value is the index of the first glyph not in S. */
20834
20835 static int
20836 fill_glyph_string (struct glyph_string *s, int face_id,
20837 int start, int end, int overlaps)
20838 {
20839 struct glyph *glyph, *last;
20840 int voffset;
20841 int glyph_not_available_p;
20842
20843 xassert (s->f == XFRAME (s->w->frame));
20844 xassert (s->nchars == 0);
20845 xassert (start >= 0 && end > start);
20846
20847 s->for_overlaps = overlaps;
20848 glyph = s->row->glyphs[s->area] + start;
20849 last = s->row->glyphs[s->area] + end;
20850 voffset = glyph->voffset;
20851 s->padding_p = glyph->padding_p;
20852 glyph_not_available_p = glyph->glyph_not_available_p;
20853
20854 while (glyph < last
20855 && glyph->type == CHAR_GLYPH
20856 && glyph->voffset == voffset
20857 /* Same face id implies same font, nowadays. */
20858 && glyph->face_id == face_id
20859 && glyph->glyph_not_available_p == glyph_not_available_p)
20860 {
20861 int two_byte_p;
20862
20863 s->face = get_glyph_face_and_encoding (s->f, glyph,
20864 s->char2b + s->nchars,
20865 &two_byte_p);
20866 s->two_byte_p = two_byte_p;
20867 ++s->nchars;
20868 xassert (s->nchars <= end - start);
20869 s->width += glyph->pixel_width;
20870 if (glyph++->padding_p != s->padding_p)
20871 break;
20872 }
20873
20874 s->font = s->face->font;
20875
20876 /* If the specified font could not be loaded, use the frame's font,
20877 but record the fact that we couldn't load it in
20878 S->font_not_found_p so that we can draw rectangles for the
20879 characters of the glyph string. */
20880 if (s->font == NULL || glyph_not_available_p)
20881 {
20882 s->font_not_found_p = 1;
20883 s->font = FRAME_FONT (s->f);
20884 }
20885
20886 /* Adjust base line for subscript/superscript text. */
20887 s->ybase += voffset;
20888
20889 xassert (s->face && s->face->gc);
20890 return glyph - s->row->glyphs[s->area];
20891 }
20892
20893
20894 /* Fill glyph string S from image glyph S->first_glyph. */
20895
20896 static void
20897 fill_image_glyph_string (struct glyph_string *s)
20898 {
20899 xassert (s->first_glyph->type == IMAGE_GLYPH);
20900 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20901 xassert (s->img);
20902 s->slice = s->first_glyph->slice.img;
20903 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20904 s->font = s->face->font;
20905 s->width = s->first_glyph->pixel_width;
20906
20907 /* Adjust base line for subscript/superscript text. */
20908 s->ybase += s->first_glyph->voffset;
20909 }
20910
20911
20912 /* Fill glyph string S from a sequence of stretch glyphs.
20913
20914 ROW is the glyph row in which the glyphs are found, AREA is the
20915 area within the row. START is the index of the first glyph to
20916 consider, END is the index of the last + 1.
20917
20918 Value is the index of the first glyph not in S. */
20919
20920 static int
20921 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20922 enum glyph_row_area area, int start, int end)
20923 {
20924 struct glyph *glyph, *last;
20925 int voffset, face_id;
20926
20927 xassert (s->first_glyph->type == STRETCH_GLYPH);
20928
20929 glyph = s->row->glyphs[s->area] + start;
20930 last = s->row->glyphs[s->area] + end;
20931 face_id = glyph->face_id;
20932 s->face = FACE_FROM_ID (s->f, face_id);
20933 s->font = s->face->font;
20934 s->width = glyph->pixel_width;
20935 s->nchars = 1;
20936 voffset = glyph->voffset;
20937
20938 for (++glyph;
20939 (glyph < last
20940 && glyph->type == STRETCH_GLYPH
20941 && glyph->voffset == voffset
20942 && glyph->face_id == face_id);
20943 ++glyph)
20944 s->width += glyph->pixel_width;
20945
20946 /* Adjust base line for subscript/superscript text. */
20947 s->ybase += voffset;
20948
20949 /* The case that face->gc == 0 is handled when drawing the glyph
20950 string by calling PREPARE_FACE_FOR_DISPLAY. */
20951 xassert (s->face);
20952 return glyph - s->row->glyphs[s->area];
20953 }
20954
20955 static struct font_metrics *
20956 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20957 {
20958 static struct font_metrics metrics;
20959 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20960
20961 if (! font || code == FONT_INVALID_CODE)
20962 return NULL;
20963 font->driver->text_extents (font, &code, 1, &metrics);
20964 return &metrics;
20965 }
20966
20967 /* EXPORT for RIF:
20968 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20969 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20970 assumed to be zero. */
20971
20972 void
20973 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20974 {
20975 *left = *right = 0;
20976
20977 if (glyph->type == CHAR_GLYPH)
20978 {
20979 struct face *face;
20980 XChar2b char2b;
20981 struct font_metrics *pcm;
20982
20983 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20984 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20985 {
20986 if (pcm->rbearing > pcm->width)
20987 *right = pcm->rbearing - pcm->width;
20988 if (pcm->lbearing < 0)
20989 *left = -pcm->lbearing;
20990 }
20991 }
20992 else if (glyph->type == COMPOSITE_GLYPH)
20993 {
20994 if (! glyph->u.cmp.automatic)
20995 {
20996 struct composition *cmp = composition_table[glyph->u.cmp.id];
20997
20998 if (cmp->rbearing > cmp->pixel_width)
20999 *right = cmp->rbearing - cmp->pixel_width;
21000 if (cmp->lbearing < 0)
21001 *left = - cmp->lbearing;
21002 }
21003 else
21004 {
21005 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21006 struct font_metrics metrics;
21007
21008 composition_gstring_width (gstring, glyph->slice.cmp.from,
21009 glyph->slice.cmp.to + 1, &metrics);
21010 if (metrics.rbearing > metrics.width)
21011 *right = metrics.rbearing - metrics.width;
21012 if (metrics.lbearing < 0)
21013 *left = - metrics.lbearing;
21014 }
21015 }
21016 }
21017
21018
21019 /* Return the index of the first glyph preceding glyph string S that
21020 is overwritten by S because of S's left overhang. Value is -1
21021 if no glyphs are overwritten. */
21022
21023 static int
21024 left_overwritten (struct glyph_string *s)
21025 {
21026 int k;
21027
21028 if (s->left_overhang)
21029 {
21030 int x = 0, i;
21031 struct glyph *glyphs = s->row->glyphs[s->area];
21032 int first = s->first_glyph - glyphs;
21033
21034 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21035 x -= glyphs[i].pixel_width;
21036
21037 k = i + 1;
21038 }
21039 else
21040 k = -1;
21041
21042 return k;
21043 }
21044
21045
21046 /* Return the index of the first glyph preceding glyph string S that
21047 is overwriting S because of its right overhang. Value is -1 if no
21048 glyph in front of S overwrites S. */
21049
21050 static int
21051 left_overwriting (struct glyph_string *s)
21052 {
21053 int i, k, x;
21054 struct glyph *glyphs = s->row->glyphs[s->area];
21055 int first = s->first_glyph - glyphs;
21056
21057 k = -1;
21058 x = 0;
21059 for (i = first - 1; i >= 0; --i)
21060 {
21061 int left, right;
21062 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21063 if (x + right > 0)
21064 k = i;
21065 x -= glyphs[i].pixel_width;
21066 }
21067
21068 return k;
21069 }
21070
21071
21072 /* Return the index of the last glyph following glyph string S that is
21073 overwritten by S because of S's right overhang. Value is -1 if
21074 no such glyph is found. */
21075
21076 static int
21077 right_overwritten (struct glyph_string *s)
21078 {
21079 int k = -1;
21080
21081 if (s->right_overhang)
21082 {
21083 int x = 0, i;
21084 struct glyph *glyphs = s->row->glyphs[s->area];
21085 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21086 int end = s->row->used[s->area];
21087
21088 for (i = first; i < end && s->right_overhang > x; ++i)
21089 x += glyphs[i].pixel_width;
21090
21091 k = i;
21092 }
21093
21094 return k;
21095 }
21096
21097
21098 /* Return the index of the last glyph following glyph string S that
21099 overwrites S because of its left overhang. Value is negative
21100 if no such glyph is found. */
21101
21102 static int
21103 right_overwriting (struct glyph_string *s)
21104 {
21105 int i, k, x;
21106 int end = s->row->used[s->area];
21107 struct glyph *glyphs = s->row->glyphs[s->area];
21108 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21109
21110 k = -1;
21111 x = 0;
21112 for (i = first; i < end; ++i)
21113 {
21114 int left, right;
21115 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21116 if (x - left < 0)
21117 k = i;
21118 x += glyphs[i].pixel_width;
21119 }
21120
21121 return k;
21122 }
21123
21124
21125 /* Set background width of glyph string S. START is the index of the
21126 first glyph following S. LAST_X is the right-most x-position + 1
21127 in the drawing area. */
21128
21129 static INLINE void
21130 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21131 {
21132 /* If the face of this glyph string has to be drawn to the end of
21133 the drawing area, set S->extends_to_end_of_line_p. */
21134
21135 if (start == s->row->used[s->area]
21136 && s->area == TEXT_AREA
21137 && ((s->row->fill_line_p
21138 && (s->hl == DRAW_NORMAL_TEXT
21139 || s->hl == DRAW_IMAGE_RAISED
21140 || s->hl == DRAW_IMAGE_SUNKEN))
21141 || s->hl == DRAW_MOUSE_FACE))
21142 s->extends_to_end_of_line_p = 1;
21143
21144 /* If S extends its face to the end of the line, set its
21145 background_width to the distance to the right edge of the drawing
21146 area. */
21147 if (s->extends_to_end_of_line_p)
21148 s->background_width = last_x - s->x + 1;
21149 else
21150 s->background_width = s->width;
21151 }
21152
21153
21154 /* Compute overhangs and x-positions for glyph string S and its
21155 predecessors, or successors. X is the starting x-position for S.
21156 BACKWARD_P non-zero means process predecessors. */
21157
21158 static void
21159 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21160 {
21161 if (backward_p)
21162 {
21163 while (s)
21164 {
21165 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21166 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21167 x -= s->width;
21168 s->x = x;
21169 s = s->prev;
21170 }
21171 }
21172 else
21173 {
21174 while (s)
21175 {
21176 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21177 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21178 s->x = x;
21179 x += s->width;
21180 s = s->next;
21181 }
21182 }
21183 }
21184
21185
21186
21187 /* The following macros are only called from draw_glyphs below.
21188 They reference the following parameters of that function directly:
21189 `w', `row', `area', and `overlap_p'
21190 as well as the following local variables:
21191 `s', `f', and `hdc' (in W32) */
21192
21193 #ifdef HAVE_NTGUI
21194 /* On W32, silently add local `hdc' variable to argument list of
21195 init_glyph_string. */
21196 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21197 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21198 #else
21199 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21200 init_glyph_string (s, char2b, w, row, area, start, hl)
21201 #endif
21202
21203 /* Add a glyph string for a stretch glyph to the list of strings
21204 between HEAD and TAIL. START is the index of the stretch glyph in
21205 row area AREA of glyph row ROW. END is the index of the last glyph
21206 in that glyph row area. X is the current output position assigned
21207 to the new glyph string constructed. HL overrides that face of the
21208 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21209 is the right-most x-position of the drawing area. */
21210
21211 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21212 and below -- keep them on one line. */
21213 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21214 do \
21215 { \
21216 s = (struct glyph_string *) alloca (sizeof *s); \
21217 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21218 START = fill_stretch_glyph_string (s, row, area, START, END); \
21219 append_glyph_string (&HEAD, &TAIL, s); \
21220 s->x = (X); \
21221 } \
21222 while (0)
21223
21224
21225 /* Add a glyph string for an image glyph to the list of strings
21226 between HEAD and TAIL. START is the index of the image glyph in
21227 row area AREA of glyph row ROW. END is the index of the last glyph
21228 in that glyph row area. X is the current output position assigned
21229 to the new glyph string constructed. HL overrides that face of the
21230 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21231 is the right-most x-position of the drawing area. */
21232
21233 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21234 do \
21235 { \
21236 s = (struct glyph_string *) alloca (sizeof *s); \
21237 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21238 fill_image_glyph_string (s); \
21239 append_glyph_string (&HEAD, &TAIL, s); \
21240 ++START; \
21241 s->x = (X); \
21242 } \
21243 while (0)
21244
21245
21246 /* Add a glyph string for a sequence of character glyphs to the list
21247 of strings between HEAD and TAIL. START is the index of the first
21248 glyph in row area AREA of glyph row ROW that is part of the new
21249 glyph string. END is the index of the last glyph in that glyph row
21250 area. X is the current output position assigned to the new glyph
21251 string constructed. HL overrides that face of the glyph; e.g. it
21252 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21253 right-most x-position of the drawing area. */
21254
21255 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21256 do \
21257 { \
21258 int face_id; \
21259 XChar2b *char2b; \
21260 \
21261 face_id = (row)->glyphs[area][START].face_id; \
21262 \
21263 s = (struct glyph_string *) alloca (sizeof *s); \
21264 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21265 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21266 append_glyph_string (&HEAD, &TAIL, s); \
21267 s->x = (X); \
21268 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21269 } \
21270 while (0)
21271
21272
21273 /* Add a glyph string for a composite sequence to the list of strings
21274 between HEAD and TAIL. START is the index of the first glyph in
21275 row area AREA of glyph row ROW that is part of the new glyph
21276 string. END is the index of the last glyph in that glyph row area.
21277 X is the current output position assigned to the new glyph string
21278 constructed. HL overrides that face of the glyph; e.g. it is
21279 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21280 x-position of the drawing area. */
21281
21282 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21283 do { \
21284 int face_id = (row)->glyphs[area][START].face_id; \
21285 struct face *base_face = FACE_FROM_ID (f, face_id); \
21286 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21287 struct composition *cmp = composition_table[cmp_id]; \
21288 XChar2b *char2b; \
21289 struct glyph_string *first_s; \
21290 int n; \
21291 \
21292 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21293 \
21294 /* Make glyph_strings for each glyph sequence that is drawable by \
21295 the same face, and append them to HEAD/TAIL. */ \
21296 for (n = 0; n < cmp->glyph_len;) \
21297 { \
21298 s = (struct glyph_string *) alloca (sizeof *s); \
21299 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21300 append_glyph_string (&(HEAD), &(TAIL), s); \
21301 s->cmp = cmp; \
21302 s->cmp_from = n; \
21303 s->x = (X); \
21304 if (n == 0) \
21305 first_s = s; \
21306 n = fill_composite_glyph_string (s, base_face, overlaps); \
21307 } \
21308 \
21309 ++START; \
21310 s = first_s; \
21311 } while (0)
21312
21313
21314 /* Add a glyph string for a glyph-string sequence to the list of strings
21315 between HEAD and TAIL. */
21316
21317 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21318 do { \
21319 int face_id; \
21320 XChar2b *char2b; \
21321 Lisp_Object gstring; \
21322 \
21323 face_id = (row)->glyphs[area][START].face_id; \
21324 gstring = (composition_gstring_from_id \
21325 ((row)->glyphs[area][START].u.cmp.id)); \
21326 s = (struct glyph_string *) alloca (sizeof *s); \
21327 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21328 * LGSTRING_GLYPH_LEN (gstring)); \
21329 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21330 append_glyph_string (&(HEAD), &(TAIL), s); \
21331 s->x = (X); \
21332 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21333 } while (0)
21334
21335
21336 /* Add a glyph string for a sequence of glyphless character's glyphs
21337 to the list of strings between HEAD and TAIL. The meanings of
21338 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21339
21340 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21341 do \
21342 { \
21343 int face_id; \
21344 XChar2b *char2b; \
21345 \
21346 face_id = (row)->glyphs[area][START].face_id; \
21347 \
21348 s = (struct glyph_string *) alloca (sizeof *s); \
21349 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21350 append_glyph_string (&HEAD, &TAIL, s); \
21351 s->x = (X); \
21352 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21353 overlaps); \
21354 } \
21355 while (0)
21356
21357
21358 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21359 of AREA of glyph row ROW on window W between indices START and END.
21360 HL overrides the face for drawing glyph strings, e.g. it is
21361 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21362 x-positions of the drawing area.
21363
21364 This is an ugly monster macro construct because we must use alloca
21365 to allocate glyph strings (because draw_glyphs can be called
21366 asynchronously). */
21367
21368 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21369 do \
21370 { \
21371 HEAD = TAIL = NULL; \
21372 while (START < END) \
21373 { \
21374 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21375 switch (first_glyph->type) \
21376 { \
21377 case CHAR_GLYPH: \
21378 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21379 HL, X, LAST_X); \
21380 break; \
21381 \
21382 case COMPOSITE_GLYPH: \
21383 if (first_glyph->u.cmp.automatic) \
21384 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21385 HL, X, LAST_X); \
21386 else \
21387 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21388 HL, X, LAST_X); \
21389 break; \
21390 \
21391 case STRETCH_GLYPH: \
21392 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21393 HL, X, LAST_X); \
21394 break; \
21395 \
21396 case IMAGE_GLYPH: \
21397 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21398 HL, X, LAST_X); \
21399 break; \
21400 \
21401 case GLYPHLESS_GLYPH: \
21402 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21403 HL, X, LAST_X); \
21404 break; \
21405 \
21406 default: \
21407 abort (); \
21408 } \
21409 \
21410 if (s) \
21411 { \
21412 set_glyph_string_background_width (s, START, LAST_X); \
21413 (X) += s->width; \
21414 } \
21415 } \
21416 } while (0)
21417
21418
21419 /* Draw glyphs between START and END in AREA of ROW on window W,
21420 starting at x-position X. X is relative to AREA in W. HL is a
21421 face-override with the following meaning:
21422
21423 DRAW_NORMAL_TEXT draw normally
21424 DRAW_CURSOR draw in cursor face
21425 DRAW_MOUSE_FACE draw in mouse face.
21426 DRAW_INVERSE_VIDEO draw in mode line face
21427 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21428 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21429
21430 If OVERLAPS is non-zero, draw only the foreground of characters and
21431 clip to the physical height of ROW. Non-zero value also defines
21432 the overlapping part to be drawn:
21433
21434 OVERLAPS_PRED overlap with preceding rows
21435 OVERLAPS_SUCC overlap with succeeding rows
21436 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21437 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21438
21439 Value is the x-position reached, relative to AREA of W. */
21440
21441 static int
21442 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21443 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21444 enum draw_glyphs_face hl, int overlaps)
21445 {
21446 struct glyph_string *head, *tail;
21447 struct glyph_string *s;
21448 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21449 int i, j, x_reached, last_x, area_left = 0;
21450 struct frame *f = XFRAME (WINDOW_FRAME (w));
21451 DECLARE_HDC (hdc);
21452
21453 ALLOCATE_HDC (hdc, f);
21454
21455 /* Let's rather be paranoid than getting a SEGV. */
21456 end = min (end, row->used[area]);
21457 start = max (0, start);
21458 start = min (end, start);
21459
21460 /* Translate X to frame coordinates. Set last_x to the right
21461 end of the drawing area. */
21462 if (row->full_width_p)
21463 {
21464 /* X is relative to the left edge of W, without scroll bars
21465 or fringes. */
21466 area_left = WINDOW_LEFT_EDGE_X (w);
21467 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21468 }
21469 else
21470 {
21471 area_left = window_box_left (w, area);
21472 last_x = area_left + window_box_width (w, area);
21473 }
21474 x += area_left;
21475
21476 /* Build a doubly-linked list of glyph_string structures between
21477 head and tail from what we have to draw. Note that the macro
21478 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21479 the reason we use a separate variable `i'. */
21480 i = start;
21481 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21482 if (tail)
21483 x_reached = tail->x + tail->background_width;
21484 else
21485 x_reached = x;
21486
21487 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21488 the row, redraw some glyphs in front or following the glyph
21489 strings built above. */
21490 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21491 {
21492 struct glyph_string *h, *t;
21493 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21494 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21495 int dummy_x = 0;
21496
21497 /* If mouse highlighting is on, we may need to draw adjacent
21498 glyphs using mouse-face highlighting. */
21499 if (area == TEXT_AREA && row->mouse_face_p)
21500 {
21501 struct glyph_row *mouse_beg_row, *mouse_end_row;
21502
21503 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21504 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21505
21506 if (row >= mouse_beg_row && row <= mouse_end_row)
21507 {
21508 check_mouse_face = 1;
21509 mouse_beg_col = (row == mouse_beg_row)
21510 ? hlinfo->mouse_face_beg_col : 0;
21511 mouse_end_col = (row == mouse_end_row)
21512 ? hlinfo->mouse_face_end_col
21513 : row->used[TEXT_AREA];
21514 }
21515 }
21516
21517 /* Compute overhangs for all glyph strings. */
21518 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21519 for (s = head; s; s = s->next)
21520 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21521
21522 /* Prepend glyph strings for glyphs in front of the first glyph
21523 string that are overwritten because of the first glyph
21524 string's left overhang. The background of all strings
21525 prepended must be drawn because the first glyph string
21526 draws over it. */
21527 i = left_overwritten (head);
21528 if (i >= 0)
21529 {
21530 enum draw_glyphs_face overlap_hl;
21531
21532 /* If this row contains mouse highlighting, attempt to draw
21533 the overlapped glyphs with the correct highlight. This
21534 code fails if the overlap encompasses more than one glyph
21535 and mouse-highlight spans only some of these glyphs.
21536 However, making it work perfectly involves a lot more
21537 code, and I don't know if the pathological case occurs in
21538 practice, so we'll stick to this for now. --- cyd */
21539 if (check_mouse_face
21540 && mouse_beg_col < start && mouse_end_col > i)
21541 overlap_hl = DRAW_MOUSE_FACE;
21542 else
21543 overlap_hl = DRAW_NORMAL_TEXT;
21544
21545 j = i;
21546 BUILD_GLYPH_STRINGS (j, start, h, t,
21547 overlap_hl, dummy_x, last_x);
21548 start = i;
21549 compute_overhangs_and_x (t, head->x, 1);
21550 prepend_glyph_string_lists (&head, &tail, h, t);
21551 clip_head = head;
21552 }
21553
21554 /* Prepend glyph strings for glyphs in front of the first glyph
21555 string that overwrite that glyph string because of their
21556 right overhang. For these strings, only the foreground must
21557 be drawn, because it draws over the glyph string at `head'.
21558 The background must not be drawn because this would overwrite
21559 right overhangs of preceding glyphs for which no glyph
21560 strings exist. */
21561 i = left_overwriting (head);
21562 if (i >= 0)
21563 {
21564 enum draw_glyphs_face overlap_hl;
21565
21566 if (check_mouse_face
21567 && mouse_beg_col < start && mouse_end_col > i)
21568 overlap_hl = DRAW_MOUSE_FACE;
21569 else
21570 overlap_hl = DRAW_NORMAL_TEXT;
21571
21572 clip_head = head;
21573 BUILD_GLYPH_STRINGS (i, start, h, t,
21574 overlap_hl, dummy_x, last_x);
21575 for (s = h; s; s = s->next)
21576 s->background_filled_p = 1;
21577 compute_overhangs_and_x (t, head->x, 1);
21578 prepend_glyph_string_lists (&head, &tail, h, t);
21579 }
21580
21581 /* Append glyphs strings for glyphs following the last glyph
21582 string tail that are overwritten by tail. The background of
21583 these strings has to be drawn because tail's foreground draws
21584 over it. */
21585 i = right_overwritten (tail);
21586 if (i >= 0)
21587 {
21588 enum draw_glyphs_face overlap_hl;
21589
21590 if (check_mouse_face
21591 && mouse_beg_col < i && mouse_end_col > end)
21592 overlap_hl = DRAW_MOUSE_FACE;
21593 else
21594 overlap_hl = DRAW_NORMAL_TEXT;
21595
21596 BUILD_GLYPH_STRINGS (end, i, h, t,
21597 overlap_hl, x, last_x);
21598 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21599 we don't have `end = i;' here. */
21600 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21601 append_glyph_string_lists (&head, &tail, h, t);
21602 clip_tail = tail;
21603 }
21604
21605 /* Append glyph strings for glyphs following the last glyph
21606 string tail that overwrite tail. The foreground of such
21607 glyphs has to be drawn because it writes into the background
21608 of tail. The background must not be drawn because it could
21609 paint over the foreground of following glyphs. */
21610 i = right_overwriting (tail);
21611 if (i >= 0)
21612 {
21613 enum draw_glyphs_face overlap_hl;
21614 if (check_mouse_face
21615 && mouse_beg_col < i && mouse_end_col > end)
21616 overlap_hl = DRAW_MOUSE_FACE;
21617 else
21618 overlap_hl = DRAW_NORMAL_TEXT;
21619
21620 clip_tail = tail;
21621 i++; /* We must include the Ith glyph. */
21622 BUILD_GLYPH_STRINGS (end, i, h, t,
21623 overlap_hl, x, last_x);
21624 for (s = h; s; s = s->next)
21625 s->background_filled_p = 1;
21626 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21627 append_glyph_string_lists (&head, &tail, h, t);
21628 }
21629 if (clip_head || clip_tail)
21630 for (s = head; s; s = s->next)
21631 {
21632 s->clip_head = clip_head;
21633 s->clip_tail = clip_tail;
21634 }
21635 }
21636
21637 /* Draw all strings. */
21638 for (s = head; s; s = s->next)
21639 FRAME_RIF (f)->draw_glyph_string (s);
21640
21641 #ifndef HAVE_NS
21642 /* When focus a sole frame and move horizontally, this sets on_p to 0
21643 causing a failure to erase prev cursor position. */
21644 if (area == TEXT_AREA
21645 && !row->full_width_p
21646 /* When drawing overlapping rows, only the glyph strings'
21647 foreground is drawn, which doesn't erase a cursor
21648 completely. */
21649 && !overlaps)
21650 {
21651 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21652 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21653 : (tail ? tail->x + tail->background_width : x));
21654 x0 -= area_left;
21655 x1 -= area_left;
21656
21657 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21658 row->y, MATRIX_ROW_BOTTOM_Y (row));
21659 }
21660 #endif
21661
21662 /* Value is the x-position up to which drawn, relative to AREA of W.
21663 This doesn't include parts drawn because of overhangs. */
21664 if (row->full_width_p)
21665 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21666 else
21667 x_reached -= area_left;
21668
21669 RELEASE_HDC (hdc, f);
21670
21671 return x_reached;
21672 }
21673
21674 /* Expand row matrix if too narrow. Don't expand if area
21675 is not present. */
21676
21677 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21678 { \
21679 if (!fonts_changed_p \
21680 && (it->glyph_row->glyphs[area] \
21681 < it->glyph_row->glyphs[area + 1])) \
21682 { \
21683 it->w->ncols_scale_factor++; \
21684 fonts_changed_p = 1; \
21685 } \
21686 }
21687
21688 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21689 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21690
21691 static INLINE void
21692 append_glyph (struct it *it)
21693 {
21694 struct glyph *glyph;
21695 enum glyph_row_area area = it->area;
21696
21697 xassert (it->glyph_row);
21698 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21699
21700 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21701 if (glyph < it->glyph_row->glyphs[area + 1])
21702 {
21703 /* If the glyph row is reversed, we need to prepend the glyph
21704 rather than append it. */
21705 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21706 {
21707 struct glyph *g;
21708
21709 /* Make room for the additional glyph. */
21710 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21711 g[1] = *g;
21712 glyph = it->glyph_row->glyphs[area];
21713 }
21714 glyph->charpos = CHARPOS (it->position);
21715 glyph->object = it->object;
21716 if (it->pixel_width > 0)
21717 {
21718 glyph->pixel_width = it->pixel_width;
21719 glyph->padding_p = 0;
21720 }
21721 else
21722 {
21723 /* Assure at least 1-pixel width. Otherwise, cursor can't
21724 be displayed correctly. */
21725 glyph->pixel_width = 1;
21726 glyph->padding_p = 1;
21727 }
21728 glyph->ascent = it->ascent;
21729 glyph->descent = it->descent;
21730 glyph->voffset = it->voffset;
21731 glyph->type = CHAR_GLYPH;
21732 glyph->avoid_cursor_p = it->avoid_cursor_p;
21733 glyph->multibyte_p = it->multibyte_p;
21734 glyph->left_box_line_p = it->start_of_box_run_p;
21735 glyph->right_box_line_p = it->end_of_box_run_p;
21736 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21737 || it->phys_descent > it->descent);
21738 glyph->glyph_not_available_p = it->glyph_not_available_p;
21739 glyph->face_id = it->face_id;
21740 glyph->u.ch = it->char_to_display;
21741 glyph->slice.img = null_glyph_slice;
21742 glyph->font_type = FONT_TYPE_UNKNOWN;
21743 if (it->bidi_p)
21744 {
21745 glyph->resolved_level = it->bidi_it.resolved_level;
21746 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21747 abort ();
21748 glyph->bidi_type = it->bidi_it.type;
21749 }
21750 else
21751 {
21752 glyph->resolved_level = 0;
21753 glyph->bidi_type = UNKNOWN_BT;
21754 }
21755 ++it->glyph_row->used[area];
21756 }
21757 else
21758 IT_EXPAND_MATRIX_WIDTH (it, area);
21759 }
21760
21761 /* Store one glyph for the composition IT->cmp_it.id in
21762 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21763 non-null. */
21764
21765 static INLINE void
21766 append_composite_glyph (struct it *it)
21767 {
21768 struct glyph *glyph;
21769 enum glyph_row_area area = it->area;
21770
21771 xassert (it->glyph_row);
21772
21773 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21774 if (glyph < it->glyph_row->glyphs[area + 1])
21775 {
21776 /* If the glyph row is reversed, we need to prepend the glyph
21777 rather than append it. */
21778 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21779 {
21780 struct glyph *g;
21781
21782 /* Make room for the new glyph. */
21783 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21784 g[1] = *g;
21785 glyph = it->glyph_row->glyphs[it->area];
21786 }
21787 glyph->charpos = it->cmp_it.charpos;
21788 glyph->object = it->object;
21789 glyph->pixel_width = it->pixel_width;
21790 glyph->ascent = it->ascent;
21791 glyph->descent = it->descent;
21792 glyph->voffset = it->voffset;
21793 glyph->type = COMPOSITE_GLYPH;
21794 if (it->cmp_it.ch < 0)
21795 {
21796 glyph->u.cmp.automatic = 0;
21797 glyph->u.cmp.id = it->cmp_it.id;
21798 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21799 }
21800 else
21801 {
21802 glyph->u.cmp.automatic = 1;
21803 glyph->u.cmp.id = it->cmp_it.id;
21804 glyph->slice.cmp.from = it->cmp_it.from;
21805 glyph->slice.cmp.to = it->cmp_it.to - 1;
21806 }
21807 glyph->avoid_cursor_p = it->avoid_cursor_p;
21808 glyph->multibyte_p = it->multibyte_p;
21809 glyph->left_box_line_p = it->start_of_box_run_p;
21810 glyph->right_box_line_p = it->end_of_box_run_p;
21811 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21812 || it->phys_descent > it->descent);
21813 glyph->padding_p = 0;
21814 glyph->glyph_not_available_p = 0;
21815 glyph->face_id = it->face_id;
21816 glyph->font_type = FONT_TYPE_UNKNOWN;
21817 if (it->bidi_p)
21818 {
21819 glyph->resolved_level = it->bidi_it.resolved_level;
21820 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21821 abort ();
21822 glyph->bidi_type = it->bidi_it.type;
21823 }
21824 ++it->glyph_row->used[area];
21825 }
21826 else
21827 IT_EXPAND_MATRIX_WIDTH (it, area);
21828 }
21829
21830
21831 /* Change IT->ascent and IT->height according to the setting of
21832 IT->voffset. */
21833
21834 static INLINE void
21835 take_vertical_position_into_account (struct it *it)
21836 {
21837 if (it->voffset)
21838 {
21839 if (it->voffset < 0)
21840 /* Increase the ascent so that we can display the text higher
21841 in the line. */
21842 it->ascent -= it->voffset;
21843 else
21844 /* Increase the descent so that we can display the text lower
21845 in the line. */
21846 it->descent += it->voffset;
21847 }
21848 }
21849
21850
21851 /* Produce glyphs/get display metrics for the image IT is loaded with.
21852 See the description of struct display_iterator in dispextern.h for
21853 an overview of struct display_iterator. */
21854
21855 static void
21856 produce_image_glyph (struct it *it)
21857 {
21858 struct image *img;
21859 struct face *face;
21860 int glyph_ascent, crop;
21861 struct glyph_slice slice;
21862
21863 xassert (it->what == IT_IMAGE);
21864
21865 face = FACE_FROM_ID (it->f, it->face_id);
21866 xassert (face);
21867 /* Make sure X resources of the face is loaded. */
21868 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21869
21870 if (it->image_id < 0)
21871 {
21872 /* Fringe bitmap. */
21873 it->ascent = it->phys_ascent = 0;
21874 it->descent = it->phys_descent = 0;
21875 it->pixel_width = 0;
21876 it->nglyphs = 0;
21877 return;
21878 }
21879
21880 img = IMAGE_FROM_ID (it->f, it->image_id);
21881 xassert (img);
21882 /* Make sure X resources of the image is loaded. */
21883 prepare_image_for_display (it->f, img);
21884
21885 slice.x = slice.y = 0;
21886 slice.width = img->width;
21887 slice.height = img->height;
21888
21889 if (INTEGERP (it->slice.x))
21890 slice.x = XINT (it->slice.x);
21891 else if (FLOATP (it->slice.x))
21892 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21893
21894 if (INTEGERP (it->slice.y))
21895 slice.y = XINT (it->slice.y);
21896 else if (FLOATP (it->slice.y))
21897 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21898
21899 if (INTEGERP (it->slice.width))
21900 slice.width = XINT (it->slice.width);
21901 else if (FLOATP (it->slice.width))
21902 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21903
21904 if (INTEGERP (it->slice.height))
21905 slice.height = XINT (it->slice.height);
21906 else if (FLOATP (it->slice.height))
21907 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21908
21909 if (slice.x >= img->width)
21910 slice.x = img->width;
21911 if (slice.y >= img->height)
21912 slice.y = img->height;
21913 if (slice.x + slice.width >= img->width)
21914 slice.width = img->width - slice.x;
21915 if (slice.y + slice.height > img->height)
21916 slice.height = img->height - slice.y;
21917
21918 if (slice.width == 0 || slice.height == 0)
21919 return;
21920
21921 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21922
21923 it->descent = slice.height - glyph_ascent;
21924 if (slice.y == 0)
21925 it->descent += img->vmargin;
21926 if (slice.y + slice.height == img->height)
21927 it->descent += img->vmargin;
21928 it->phys_descent = it->descent;
21929
21930 it->pixel_width = slice.width;
21931 if (slice.x == 0)
21932 it->pixel_width += img->hmargin;
21933 if (slice.x + slice.width == img->width)
21934 it->pixel_width += img->hmargin;
21935
21936 /* It's quite possible for images to have an ascent greater than
21937 their height, so don't get confused in that case. */
21938 if (it->descent < 0)
21939 it->descent = 0;
21940
21941 it->nglyphs = 1;
21942
21943 if (face->box != FACE_NO_BOX)
21944 {
21945 if (face->box_line_width > 0)
21946 {
21947 if (slice.y == 0)
21948 it->ascent += face->box_line_width;
21949 if (slice.y + slice.height == img->height)
21950 it->descent += face->box_line_width;
21951 }
21952
21953 if (it->start_of_box_run_p && slice.x == 0)
21954 it->pixel_width += eabs (face->box_line_width);
21955 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21956 it->pixel_width += eabs (face->box_line_width);
21957 }
21958
21959 take_vertical_position_into_account (it);
21960
21961 /* Automatically crop wide image glyphs at right edge so we can
21962 draw the cursor on same display row. */
21963 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21964 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21965 {
21966 it->pixel_width -= crop;
21967 slice.width -= crop;
21968 }
21969
21970 if (it->glyph_row)
21971 {
21972 struct glyph *glyph;
21973 enum glyph_row_area area = it->area;
21974
21975 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21976 if (glyph < it->glyph_row->glyphs[area + 1])
21977 {
21978 glyph->charpos = CHARPOS (it->position);
21979 glyph->object = it->object;
21980 glyph->pixel_width = it->pixel_width;
21981 glyph->ascent = glyph_ascent;
21982 glyph->descent = it->descent;
21983 glyph->voffset = it->voffset;
21984 glyph->type = IMAGE_GLYPH;
21985 glyph->avoid_cursor_p = it->avoid_cursor_p;
21986 glyph->multibyte_p = it->multibyte_p;
21987 glyph->left_box_line_p = it->start_of_box_run_p;
21988 glyph->right_box_line_p = it->end_of_box_run_p;
21989 glyph->overlaps_vertically_p = 0;
21990 glyph->padding_p = 0;
21991 glyph->glyph_not_available_p = 0;
21992 glyph->face_id = it->face_id;
21993 glyph->u.img_id = img->id;
21994 glyph->slice.img = slice;
21995 glyph->font_type = FONT_TYPE_UNKNOWN;
21996 if (it->bidi_p)
21997 {
21998 glyph->resolved_level = it->bidi_it.resolved_level;
21999 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22000 abort ();
22001 glyph->bidi_type = it->bidi_it.type;
22002 }
22003 ++it->glyph_row->used[area];
22004 }
22005 else
22006 IT_EXPAND_MATRIX_WIDTH (it, area);
22007 }
22008 }
22009
22010
22011 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22012 of the glyph, WIDTH and HEIGHT are the width and height of the
22013 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22014
22015 static void
22016 append_stretch_glyph (struct it *it, Lisp_Object object,
22017 int width, int height, int ascent)
22018 {
22019 struct glyph *glyph;
22020 enum glyph_row_area area = it->area;
22021
22022 xassert (ascent >= 0 && ascent <= height);
22023
22024 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22025 if (glyph < it->glyph_row->glyphs[area + 1])
22026 {
22027 /* If the glyph row is reversed, we need to prepend the glyph
22028 rather than append it. */
22029 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22030 {
22031 struct glyph *g;
22032
22033 /* Make room for the additional glyph. */
22034 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22035 g[1] = *g;
22036 glyph = it->glyph_row->glyphs[area];
22037 }
22038 glyph->charpos = CHARPOS (it->position);
22039 glyph->object = object;
22040 glyph->pixel_width = width;
22041 glyph->ascent = ascent;
22042 glyph->descent = height - ascent;
22043 glyph->voffset = it->voffset;
22044 glyph->type = STRETCH_GLYPH;
22045 glyph->avoid_cursor_p = it->avoid_cursor_p;
22046 glyph->multibyte_p = it->multibyte_p;
22047 glyph->left_box_line_p = it->start_of_box_run_p;
22048 glyph->right_box_line_p = it->end_of_box_run_p;
22049 glyph->overlaps_vertically_p = 0;
22050 glyph->padding_p = 0;
22051 glyph->glyph_not_available_p = 0;
22052 glyph->face_id = it->face_id;
22053 glyph->u.stretch.ascent = ascent;
22054 glyph->u.stretch.height = height;
22055 glyph->slice.img = null_glyph_slice;
22056 glyph->font_type = FONT_TYPE_UNKNOWN;
22057 if (it->bidi_p)
22058 {
22059 glyph->resolved_level = it->bidi_it.resolved_level;
22060 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22061 abort ();
22062 glyph->bidi_type = it->bidi_it.type;
22063 }
22064 else
22065 {
22066 glyph->resolved_level = 0;
22067 glyph->bidi_type = UNKNOWN_BT;
22068 }
22069 ++it->glyph_row->used[area];
22070 }
22071 else
22072 IT_EXPAND_MATRIX_WIDTH (it, area);
22073 }
22074
22075
22076 /* Produce a stretch glyph for iterator IT. IT->object is the value
22077 of the glyph property displayed. The value must be a list
22078 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22079 being recognized:
22080
22081 1. `:width WIDTH' specifies that the space should be WIDTH *
22082 canonical char width wide. WIDTH may be an integer or floating
22083 point number.
22084
22085 2. `:relative-width FACTOR' specifies that the width of the stretch
22086 should be computed from the width of the first character having the
22087 `glyph' property, and should be FACTOR times that width.
22088
22089 3. `:align-to HPOS' specifies that the space should be wide enough
22090 to reach HPOS, a value in canonical character units.
22091
22092 Exactly one of the above pairs must be present.
22093
22094 4. `:height HEIGHT' specifies that the height of the stretch produced
22095 should be HEIGHT, measured in canonical character units.
22096
22097 5. `:relative-height FACTOR' specifies that the height of the
22098 stretch should be FACTOR times the height of the characters having
22099 the glyph property.
22100
22101 Either none or exactly one of 4 or 5 must be present.
22102
22103 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22104 of the stretch should be used for the ascent of the stretch.
22105 ASCENT must be in the range 0 <= ASCENT <= 100. */
22106
22107 static void
22108 produce_stretch_glyph (struct it *it)
22109 {
22110 /* (space :width WIDTH :height HEIGHT ...) */
22111 Lisp_Object prop, plist;
22112 int width = 0, height = 0, align_to = -1;
22113 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22114 int ascent = 0;
22115 double tem;
22116 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22117 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22118
22119 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22120
22121 /* List should start with `space'. */
22122 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22123 plist = XCDR (it->object);
22124
22125 /* Compute the width of the stretch. */
22126 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22127 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22128 {
22129 /* Absolute width `:width WIDTH' specified and valid. */
22130 zero_width_ok_p = 1;
22131 width = (int)tem;
22132 }
22133 else if (prop = Fplist_get (plist, QCrelative_width),
22134 NUMVAL (prop) > 0)
22135 {
22136 /* Relative width `:relative-width FACTOR' specified and valid.
22137 Compute the width of the characters having the `glyph'
22138 property. */
22139 struct it it2;
22140 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22141
22142 it2 = *it;
22143 if (it->multibyte_p)
22144 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22145 else
22146 {
22147 it2.c = it2.char_to_display = *p, it2.len = 1;
22148 if (! ASCII_CHAR_P (it2.c))
22149 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22150 }
22151
22152 it2.glyph_row = NULL;
22153 it2.what = IT_CHARACTER;
22154 x_produce_glyphs (&it2);
22155 width = NUMVAL (prop) * it2.pixel_width;
22156 }
22157 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22158 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22159 {
22160 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22161 align_to = (align_to < 0
22162 ? 0
22163 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22164 else if (align_to < 0)
22165 align_to = window_box_left_offset (it->w, TEXT_AREA);
22166 width = max (0, (int)tem + align_to - it->current_x);
22167 zero_width_ok_p = 1;
22168 }
22169 else
22170 /* Nothing specified -> width defaults to canonical char width. */
22171 width = FRAME_COLUMN_WIDTH (it->f);
22172
22173 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22174 width = 1;
22175
22176 /* Compute height. */
22177 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22178 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22179 {
22180 height = (int)tem;
22181 zero_height_ok_p = 1;
22182 }
22183 else if (prop = Fplist_get (plist, QCrelative_height),
22184 NUMVAL (prop) > 0)
22185 height = FONT_HEIGHT (font) * NUMVAL (prop);
22186 else
22187 height = FONT_HEIGHT (font);
22188
22189 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22190 height = 1;
22191
22192 /* Compute percentage of height used for ascent. If
22193 `:ascent ASCENT' is present and valid, use that. Otherwise,
22194 derive the ascent from the font in use. */
22195 if (prop = Fplist_get (plist, QCascent),
22196 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22197 ascent = height * NUMVAL (prop) / 100.0;
22198 else if (!NILP (prop)
22199 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22200 ascent = min (max (0, (int)tem), height);
22201 else
22202 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22203
22204 if (width > 0 && it->line_wrap != TRUNCATE
22205 && it->current_x + width > it->last_visible_x)
22206 width = it->last_visible_x - it->current_x - 1;
22207
22208 if (width > 0 && height > 0 && it->glyph_row)
22209 {
22210 Lisp_Object object = it->stack[it->sp - 1].string;
22211 if (!STRINGP (object))
22212 object = it->w->buffer;
22213 append_stretch_glyph (it, object, width, height, ascent);
22214 }
22215
22216 it->pixel_width = width;
22217 it->ascent = it->phys_ascent = ascent;
22218 it->descent = it->phys_descent = height - it->ascent;
22219 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22220
22221 take_vertical_position_into_account (it);
22222 }
22223
22224 /* Calculate line-height and line-spacing properties.
22225 An integer value specifies explicit pixel value.
22226 A float value specifies relative value to current face height.
22227 A cons (float . face-name) specifies relative value to
22228 height of specified face font.
22229
22230 Returns height in pixels, or nil. */
22231
22232
22233 static Lisp_Object
22234 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22235 int boff, int override)
22236 {
22237 Lisp_Object face_name = Qnil;
22238 int ascent, descent, height;
22239
22240 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22241 return val;
22242
22243 if (CONSP (val))
22244 {
22245 face_name = XCAR (val);
22246 val = XCDR (val);
22247 if (!NUMBERP (val))
22248 val = make_number (1);
22249 if (NILP (face_name))
22250 {
22251 height = it->ascent + it->descent;
22252 goto scale;
22253 }
22254 }
22255
22256 if (NILP (face_name))
22257 {
22258 font = FRAME_FONT (it->f);
22259 boff = FRAME_BASELINE_OFFSET (it->f);
22260 }
22261 else if (EQ (face_name, Qt))
22262 {
22263 override = 0;
22264 }
22265 else
22266 {
22267 int face_id;
22268 struct face *face;
22269
22270 face_id = lookup_named_face (it->f, face_name, 0);
22271 if (face_id < 0)
22272 return make_number (-1);
22273
22274 face = FACE_FROM_ID (it->f, face_id);
22275 font = face->font;
22276 if (font == NULL)
22277 return make_number (-1);
22278 boff = font->baseline_offset;
22279 if (font->vertical_centering)
22280 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22281 }
22282
22283 ascent = FONT_BASE (font) + boff;
22284 descent = FONT_DESCENT (font) - boff;
22285
22286 if (override)
22287 {
22288 it->override_ascent = ascent;
22289 it->override_descent = descent;
22290 it->override_boff = boff;
22291 }
22292
22293 height = ascent + descent;
22294
22295 scale:
22296 if (FLOATP (val))
22297 height = (int)(XFLOAT_DATA (val) * height);
22298 else if (INTEGERP (val))
22299 height *= XINT (val);
22300
22301 return make_number (height);
22302 }
22303
22304
22305 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22306 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22307 and only if this is for a character for which no font was found.
22308
22309 If the display method (it->glyphless_method) is
22310 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22311 length of the acronym or the hexadecimal string, UPPER_XOFF and
22312 UPPER_YOFF are pixel offsets for the upper part of the string,
22313 LOWER_XOFF and LOWER_YOFF are for the lower part.
22314
22315 For the other display methods, LEN through LOWER_YOFF are zero. */
22316
22317 static void
22318 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22319 short upper_xoff, short upper_yoff,
22320 short lower_xoff, short lower_yoff)
22321 {
22322 struct glyph *glyph;
22323 enum glyph_row_area area = it->area;
22324
22325 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22326 if (glyph < it->glyph_row->glyphs[area + 1])
22327 {
22328 /* If the glyph row is reversed, we need to prepend the glyph
22329 rather than append it. */
22330 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22331 {
22332 struct glyph *g;
22333
22334 /* Make room for the additional glyph. */
22335 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22336 g[1] = *g;
22337 glyph = it->glyph_row->glyphs[area];
22338 }
22339 glyph->charpos = CHARPOS (it->position);
22340 glyph->object = it->object;
22341 glyph->pixel_width = it->pixel_width;
22342 glyph->ascent = it->ascent;
22343 glyph->descent = it->descent;
22344 glyph->voffset = it->voffset;
22345 glyph->type = GLYPHLESS_GLYPH;
22346 glyph->u.glyphless.method = it->glyphless_method;
22347 glyph->u.glyphless.for_no_font = for_no_font;
22348 glyph->u.glyphless.len = len;
22349 glyph->u.glyphless.ch = it->c;
22350 glyph->slice.glyphless.upper_xoff = upper_xoff;
22351 glyph->slice.glyphless.upper_yoff = upper_yoff;
22352 glyph->slice.glyphless.lower_xoff = lower_xoff;
22353 glyph->slice.glyphless.lower_yoff = lower_yoff;
22354 glyph->avoid_cursor_p = it->avoid_cursor_p;
22355 glyph->multibyte_p = it->multibyte_p;
22356 glyph->left_box_line_p = it->start_of_box_run_p;
22357 glyph->right_box_line_p = it->end_of_box_run_p;
22358 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22359 || it->phys_descent > it->descent);
22360 glyph->padding_p = 0;
22361 glyph->glyph_not_available_p = 0;
22362 glyph->face_id = face_id;
22363 glyph->font_type = FONT_TYPE_UNKNOWN;
22364 if (it->bidi_p)
22365 {
22366 glyph->resolved_level = it->bidi_it.resolved_level;
22367 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22368 abort ();
22369 glyph->bidi_type = it->bidi_it.type;
22370 }
22371 ++it->glyph_row->used[area];
22372 }
22373 else
22374 IT_EXPAND_MATRIX_WIDTH (it, area);
22375 }
22376
22377
22378 /* Produce a glyph for a glyphless character for iterator IT.
22379 IT->glyphless_method specifies which method to use for displaying
22380 the character. See the description of enum
22381 glyphless_display_method in dispextern.h for the detail.
22382
22383 FOR_NO_FONT is nonzero if and only if this is for a character for
22384 which no font was found. ACRONYM, if non-nil, is an acronym string
22385 for the character. */
22386
22387 static void
22388 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22389 {
22390 int face_id;
22391 struct face *face;
22392 struct font *font;
22393 int base_width, base_height, width, height;
22394 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22395 int len;
22396
22397 /* Get the metrics of the base font. We always refer to the current
22398 ASCII face. */
22399 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22400 font = face->font ? face->font : FRAME_FONT (it->f);
22401 it->ascent = FONT_BASE (font) + font->baseline_offset;
22402 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22403 base_height = it->ascent + it->descent;
22404 base_width = font->average_width;
22405
22406 /* Get a face ID for the glyph by utilizing a cache (the same way as
22407 doen for `escape-glyph' in get_next_display_element). */
22408 if (it->f == last_glyphless_glyph_frame
22409 && it->face_id == last_glyphless_glyph_face_id)
22410 {
22411 face_id = last_glyphless_glyph_merged_face_id;
22412 }
22413 else
22414 {
22415 /* Merge the `glyphless-char' face into the current face. */
22416 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22417 last_glyphless_glyph_frame = it->f;
22418 last_glyphless_glyph_face_id = it->face_id;
22419 last_glyphless_glyph_merged_face_id = face_id;
22420 }
22421
22422 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22423 {
22424 it->pixel_width = THIN_SPACE_WIDTH;
22425 len = 0;
22426 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22427 }
22428 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22429 {
22430 width = CHAR_WIDTH (it->c);
22431 if (width == 0)
22432 width = 1;
22433 else if (width > 4)
22434 width = 4;
22435 it->pixel_width = base_width * width;
22436 len = 0;
22437 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22438 }
22439 else
22440 {
22441 char buf[7], *str;
22442 unsigned int code[6];
22443 int upper_len;
22444 int ascent, descent;
22445 struct font_metrics metrics_upper, metrics_lower;
22446
22447 face = FACE_FROM_ID (it->f, face_id);
22448 font = face->font ? face->font : FRAME_FONT (it->f);
22449 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22450
22451 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22452 {
22453 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22454 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22455 str = STRINGP (acronym) ? (char *) SDATA (acronym) : "";
22456 }
22457 else
22458 {
22459 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22460 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22461 str = buf;
22462 }
22463 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22464 code[len] = font->driver->encode_char (font, str[len]);
22465 upper_len = (len + 1) / 2;
22466 font->driver->text_extents (font, code, upper_len,
22467 &metrics_upper);
22468 font->driver->text_extents (font, code + upper_len, len - upper_len,
22469 &metrics_lower);
22470
22471
22472
22473 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22474 width = max (metrics_upper.width, metrics_lower.width) + 4;
22475 upper_xoff = upper_yoff = 2; /* the typical case */
22476 if (base_width >= width)
22477 {
22478 /* Align the upper to the left, the lower to the right. */
22479 it->pixel_width = base_width;
22480 lower_xoff = base_width - 2 - metrics_lower.width;
22481 }
22482 else
22483 {
22484 /* Center the shorter one. */
22485 it->pixel_width = width;
22486 if (metrics_upper.width >= metrics_lower.width)
22487 lower_xoff = (width - metrics_lower.width) / 2;
22488 else
22489 upper_xoff = (width - metrics_upper.width) / 2;
22490 }
22491
22492 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22493 top, bottom, and between upper and lower strings. */
22494 height = (metrics_upper.ascent + metrics_upper.descent
22495 + metrics_lower.ascent + metrics_lower.descent) + 5;
22496 /* Center vertically.
22497 H:base_height, D:base_descent
22498 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22499
22500 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22501 descent = D - H/2 + h/2;
22502 lower_yoff = descent - 2 - ld;
22503 upper_yoff = lower_yoff - la - 1 - ud; */
22504 ascent = - (it->descent - (base_height + height + 1) / 2);
22505 descent = it->descent - (base_height - height) / 2;
22506 lower_yoff = descent - 2 - metrics_lower.descent;
22507 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22508 - metrics_upper.descent);
22509 /* Don't make the height shorter than the base height. */
22510 if (height > base_height)
22511 {
22512 it->ascent = ascent;
22513 it->descent = descent;
22514 }
22515 }
22516
22517 it->phys_ascent = it->ascent;
22518 it->phys_descent = it->descent;
22519 if (it->glyph_row)
22520 append_glyphless_glyph (it, face_id, for_no_font, len,
22521 upper_xoff, upper_yoff,
22522 lower_xoff, lower_yoff);
22523 it->nglyphs = 1;
22524 take_vertical_position_into_account (it);
22525 }
22526
22527
22528 /* RIF:
22529 Produce glyphs/get display metrics for the display element IT is
22530 loaded with. See the description of struct it in dispextern.h
22531 for an overview of struct it. */
22532
22533 void
22534 x_produce_glyphs (struct it *it)
22535 {
22536 int extra_line_spacing = it->extra_line_spacing;
22537
22538 it->glyph_not_available_p = 0;
22539
22540 if (it->what == IT_CHARACTER)
22541 {
22542 XChar2b char2b;
22543 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22544 struct font *font = face->font;
22545 struct font_metrics *pcm = NULL;
22546 int boff; /* baseline offset */
22547
22548 if (font == NULL)
22549 {
22550 /* When no suitable font is found, display this character by
22551 the method specified in the first extra slot of
22552 Vglyphless_char_display. */
22553 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22554
22555 xassert (it->what == IT_GLYPHLESS);
22556 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22557 goto done;
22558 }
22559
22560 boff = font->baseline_offset;
22561 if (font->vertical_centering)
22562 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22563
22564 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22565 {
22566 int stretched_p;
22567
22568 it->nglyphs = 1;
22569
22570 if (it->override_ascent >= 0)
22571 {
22572 it->ascent = it->override_ascent;
22573 it->descent = it->override_descent;
22574 boff = it->override_boff;
22575 }
22576 else
22577 {
22578 it->ascent = FONT_BASE (font) + boff;
22579 it->descent = FONT_DESCENT (font) - boff;
22580 }
22581
22582 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22583 {
22584 pcm = get_per_char_metric (it->f, font, &char2b);
22585 if (pcm->width == 0
22586 && pcm->rbearing == 0 && pcm->lbearing == 0)
22587 pcm = NULL;
22588 }
22589
22590 if (pcm)
22591 {
22592 it->phys_ascent = pcm->ascent + boff;
22593 it->phys_descent = pcm->descent - boff;
22594 it->pixel_width = pcm->width;
22595 }
22596 else
22597 {
22598 it->glyph_not_available_p = 1;
22599 it->phys_ascent = it->ascent;
22600 it->phys_descent = it->descent;
22601 it->pixel_width = font->space_width;
22602 }
22603
22604 if (it->constrain_row_ascent_descent_p)
22605 {
22606 if (it->descent > it->max_descent)
22607 {
22608 it->ascent += it->descent - it->max_descent;
22609 it->descent = it->max_descent;
22610 }
22611 if (it->ascent > it->max_ascent)
22612 {
22613 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22614 it->ascent = it->max_ascent;
22615 }
22616 it->phys_ascent = min (it->phys_ascent, it->ascent);
22617 it->phys_descent = min (it->phys_descent, it->descent);
22618 extra_line_spacing = 0;
22619 }
22620
22621 /* If this is a space inside a region of text with
22622 `space-width' property, change its width. */
22623 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22624 if (stretched_p)
22625 it->pixel_width *= XFLOATINT (it->space_width);
22626
22627 /* If face has a box, add the box thickness to the character
22628 height. If character has a box line to the left and/or
22629 right, add the box line width to the character's width. */
22630 if (face->box != FACE_NO_BOX)
22631 {
22632 int thick = face->box_line_width;
22633
22634 if (thick > 0)
22635 {
22636 it->ascent += thick;
22637 it->descent += thick;
22638 }
22639 else
22640 thick = -thick;
22641
22642 if (it->start_of_box_run_p)
22643 it->pixel_width += thick;
22644 if (it->end_of_box_run_p)
22645 it->pixel_width += thick;
22646 }
22647
22648 /* If face has an overline, add the height of the overline
22649 (1 pixel) and a 1 pixel margin to the character height. */
22650 if (face->overline_p)
22651 it->ascent += overline_margin;
22652
22653 if (it->constrain_row_ascent_descent_p)
22654 {
22655 if (it->ascent > it->max_ascent)
22656 it->ascent = it->max_ascent;
22657 if (it->descent > it->max_descent)
22658 it->descent = it->max_descent;
22659 }
22660
22661 take_vertical_position_into_account (it);
22662
22663 /* If we have to actually produce glyphs, do it. */
22664 if (it->glyph_row)
22665 {
22666 if (stretched_p)
22667 {
22668 /* Translate a space with a `space-width' property
22669 into a stretch glyph. */
22670 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22671 / FONT_HEIGHT (font));
22672 append_stretch_glyph (it, it->object, it->pixel_width,
22673 it->ascent + it->descent, ascent);
22674 }
22675 else
22676 append_glyph (it);
22677
22678 /* If characters with lbearing or rbearing are displayed
22679 in this line, record that fact in a flag of the
22680 glyph row. This is used to optimize X output code. */
22681 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22682 it->glyph_row->contains_overlapping_glyphs_p = 1;
22683 }
22684 if (! stretched_p && it->pixel_width == 0)
22685 /* We assure that all visible glyphs have at least 1-pixel
22686 width. */
22687 it->pixel_width = 1;
22688 }
22689 else if (it->char_to_display == '\n')
22690 {
22691 /* A newline has no width, but we need the height of the
22692 line. But if previous part of the line sets a height,
22693 don't increase that height */
22694
22695 Lisp_Object height;
22696 Lisp_Object total_height = Qnil;
22697
22698 it->override_ascent = -1;
22699 it->pixel_width = 0;
22700 it->nglyphs = 0;
22701
22702 height = get_it_property (it, Qline_height);
22703 /* Split (line-height total-height) list */
22704 if (CONSP (height)
22705 && CONSP (XCDR (height))
22706 && NILP (XCDR (XCDR (height))))
22707 {
22708 total_height = XCAR (XCDR (height));
22709 height = XCAR (height);
22710 }
22711 height = calc_line_height_property (it, height, font, boff, 1);
22712
22713 if (it->override_ascent >= 0)
22714 {
22715 it->ascent = it->override_ascent;
22716 it->descent = it->override_descent;
22717 boff = it->override_boff;
22718 }
22719 else
22720 {
22721 it->ascent = FONT_BASE (font) + boff;
22722 it->descent = FONT_DESCENT (font) - boff;
22723 }
22724
22725 if (EQ (height, Qt))
22726 {
22727 if (it->descent > it->max_descent)
22728 {
22729 it->ascent += it->descent - it->max_descent;
22730 it->descent = it->max_descent;
22731 }
22732 if (it->ascent > it->max_ascent)
22733 {
22734 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22735 it->ascent = it->max_ascent;
22736 }
22737 it->phys_ascent = min (it->phys_ascent, it->ascent);
22738 it->phys_descent = min (it->phys_descent, it->descent);
22739 it->constrain_row_ascent_descent_p = 1;
22740 extra_line_spacing = 0;
22741 }
22742 else
22743 {
22744 Lisp_Object spacing;
22745
22746 it->phys_ascent = it->ascent;
22747 it->phys_descent = it->descent;
22748
22749 if ((it->max_ascent > 0 || it->max_descent > 0)
22750 && face->box != FACE_NO_BOX
22751 && face->box_line_width > 0)
22752 {
22753 it->ascent += face->box_line_width;
22754 it->descent += face->box_line_width;
22755 }
22756 if (!NILP (height)
22757 && XINT (height) > it->ascent + it->descent)
22758 it->ascent = XINT (height) - it->descent;
22759
22760 if (!NILP (total_height))
22761 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22762 else
22763 {
22764 spacing = get_it_property (it, Qline_spacing);
22765 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22766 }
22767 if (INTEGERP (spacing))
22768 {
22769 extra_line_spacing = XINT (spacing);
22770 if (!NILP (total_height))
22771 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22772 }
22773 }
22774 }
22775 else /* i.e. (it->char_to_display == '\t') */
22776 {
22777 if (font->space_width > 0)
22778 {
22779 int tab_width = it->tab_width * font->space_width;
22780 int x = it->current_x + it->continuation_lines_width;
22781 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22782
22783 /* If the distance from the current position to the next tab
22784 stop is less than a space character width, use the
22785 tab stop after that. */
22786 if (next_tab_x - x < font->space_width)
22787 next_tab_x += tab_width;
22788
22789 it->pixel_width = next_tab_x - x;
22790 it->nglyphs = 1;
22791 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22792 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22793
22794 if (it->glyph_row)
22795 {
22796 append_stretch_glyph (it, it->object, it->pixel_width,
22797 it->ascent + it->descent, it->ascent);
22798 }
22799 }
22800 else
22801 {
22802 it->pixel_width = 0;
22803 it->nglyphs = 1;
22804 }
22805 }
22806 }
22807 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22808 {
22809 /* A static composition.
22810
22811 Note: A composition is represented as one glyph in the
22812 glyph matrix. There are no padding glyphs.
22813
22814 Important note: pixel_width, ascent, and descent are the
22815 values of what is drawn by draw_glyphs (i.e. the values of
22816 the overall glyphs composed). */
22817 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22818 int boff; /* baseline offset */
22819 struct composition *cmp = composition_table[it->cmp_it.id];
22820 int glyph_len = cmp->glyph_len;
22821 struct font *font = face->font;
22822
22823 it->nglyphs = 1;
22824
22825 /* If we have not yet calculated pixel size data of glyphs of
22826 the composition for the current face font, calculate them
22827 now. Theoretically, we have to check all fonts for the
22828 glyphs, but that requires much time and memory space. So,
22829 here we check only the font of the first glyph. This may
22830 lead to incorrect display, but it's very rare, and C-l
22831 (recenter-top-bottom) can correct the display anyway. */
22832 if (! cmp->font || cmp->font != font)
22833 {
22834 /* Ascent and descent of the font of the first character
22835 of this composition (adjusted by baseline offset).
22836 Ascent and descent of overall glyphs should not be less
22837 than these, respectively. */
22838 int font_ascent, font_descent, font_height;
22839 /* Bounding box of the overall glyphs. */
22840 int leftmost, rightmost, lowest, highest;
22841 int lbearing, rbearing;
22842 int i, width, ascent, descent;
22843 int left_padded = 0, right_padded = 0;
22844 int c;
22845 XChar2b char2b;
22846 struct font_metrics *pcm;
22847 int font_not_found_p;
22848 EMACS_INT pos;
22849
22850 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22851 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22852 break;
22853 if (glyph_len < cmp->glyph_len)
22854 right_padded = 1;
22855 for (i = 0; i < glyph_len; i++)
22856 {
22857 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22858 break;
22859 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22860 }
22861 if (i > 0)
22862 left_padded = 1;
22863
22864 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22865 : IT_CHARPOS (*it));
22866 /* If no suitable font is found, use the default font. */
22867 font_not_found_p = font == NULL;
22868 if (font_not_found_p)
22869 {
22870 face = face->ascii_face;
22871 font = face->font;
22872 }
22873 boff = font->baseline_offset;
22874 if (font->vertical_centering)
22875 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22876 font_ascent = FONT_BASE (font) + boff;
22877 font_descent = FONT_DESCENT (font) - boff;
22878 font_height = FONT_HEIGHT (font);
22879
22880 cmp->font = (void *) font;
22881
22882 pcm = NULL;
22883 if (! font_not_found_p)
22884 {
22885 get_char_face_and_encoding (it->f, c, it->face_id,
22886 &char2b, it->multibyte_p, 0);
22887 pcm = get_per_char_metric (it->f, font, &char2b);
22888 }
22889
22890 /* Initialize the bounding box. */
22891 if (pcm)
22892 {
22893 width = pcm->width;
22894 ascent = pcm->ascent;
22895 descent = pcm->descent;
22896 lbearing = pcm->lbearing;
22897 rbearing = pcm->rbearing;
22898 }
22899 else
22900 {
22901 width = font->space_width;
22902 ascent = FONT_BASE (font);
22903 descent = FONT_DESCENT (font);
22904 lbearing = 0;
22905 rbearing = width;
22906 }
22907
22908 rightmost = width;
22909 leftmost = 0;
22910 lowest = - descent + boff;
22911 highest = ascent + boff;
22912
22913 if (! font_not_found_p
22914 && font->default_ascent
22915 && CHAR_TABLE_P (Vuse_default_ascent)
22916 && !NILP (Faref (Vuse_default_ascent,
22917 make_number (it->char_to_display))))
22918 highest = font->default_ascent + boff;
22919
22920 /* Draw the first glyph at the normal position. It may be
22921 shifted to right later if some other glyphs are drawn
22922 at the left. */
22923 cmp->offsets[i * 2] = 0;
22924 cmp->offsets[i * 2 + 1] = boff;
22925 cmp->lbearing = lbearing;
22926 cmp->rbearing = rbearing;
22927
22928 /* Set cmp->offsets for the remaining glyphs. */
22929 for (i++; i < glyph_len; i++)
22930 {
22931 int left, right, btm, top;
22932 int ch = COMPOSITION_GLYPH (cmp, i);
22933 int face_id;
22934 struct face *this_face;
22935 int this_boff;
22936
22937 if (ch == '\t')
22938 ch = ' ';
22939 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22940 this_face = FACE_FROM_ID (it->f, face_id);
22941 font = this_face->font;
22942
22943 if (font == NULL)
22944 pcm = NULL;
22945 else
22946 {
22947 this_boff = font->baseline_offset;
22948 if (font->vertical_centering)
22949 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22950 get_char_face_and_encoding (it->f, ch, face_id,
22951 &char2b, it->multibyte_p, 0);
22952 pcm = get_per_char_metric (it->f, font, &char2b);
22953 }
22954 if (! pcm)
22955 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22956 else
22957 {
22958 width = pcm->width;
22959 ascent = pcm->ascent;
22960 descent = pcm->descent;
22961 lbearing = pcm->lbearing;
22962 rbearing = pcm->rbearing;
22963 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22964 {
22965 /* Relative composition with or without
22966 alternate chars. */
22967 left = (leftmost + rightmost - width) / 2;
22968 btm = - descent + boff;
22969 if (font->relative_compose
22970 && (! CHAR_TABLE_P (Vignore_relative_composition)
22971 || NILP (Faref (Vignore_relative_composition,
22972 make_number (ch)))))
22973 {
22974
22975 if (- descent >= font->relative_compose)
22976 /* One extra pixel between two glyphs. */
22977 btm = highest + 1;
22978 else if (ascent <= 0)
22979 /* One extra pixel between two glyphs. */
22980 btm = lowest - 1 - ascent - descent;
22981 }
22982 }
22983 else
22984 {
22985 /* A composition rule is specified by an integer
22986 value that encodes global and new reference
22987 points (GREF and NREF). GREF and NREF are
22988 specified by numbers as below:
22989
22990 0---1---2 -- ascent
22991 | |
22992 | |
22993 | |
22994 9--10--11 -- center
22995 | |
22996 ---3---4---5--- baseline
22997 | |
22998 6---7---8 -- descent
22999 */
23000 int rule = COMPOSITION_RULE (cmp, i);
23001 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23002
23003 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23004 grefx = gref % 3, nrefx = nref % 3;
23005 grefy = gref / 3, nrefy = nref / 3;
23006 if (xoff)
23007 xoff = font_height * (xoff - 128) / 256;
23008 if (yoff)
23009 yoff = font_height * (yoff - 128) / 256;
23010
23011 left = (leftmost
23012 + grefx * (rightmost - leftmost) / 2
23013 - nrefx * width / 2
23014 + xoff);
23015
23016 btm = ((grefy == 0 ? highest
23017 : grefy == 1 ? 0
23018 : grefy == 2 ? lowest
23019 : (highest + lowest) / 2)
23020 - (nrefy == 0 ? ascent + descent
23021 : nrefy == 1 ? descent - boff
23022 : nrefy == 2 ? 0
23023 : (ascent + descent) / 2)
23024 + yoff);
23025 }
23026
23027 cmp->offsets[i * 2] = left;
23028 cmp->offsets[i * 2 + 1] = btm + descent;
23029
23030 /* Update the bounding box of the overall glyphs. */
23031 if (width > 0)
23032 {
23033 right = left + width;
23034 if (left < leftmost)
23035 leftmost = left;
23036 if (right > rightmost)
23037 rightmost = right;
23038 }
23039 top = btm + descent + ascent;
23040 if (top > highest)
23041 highest = top;
23042 if (btm < lowest)
23043 lowest = btm;
23044
23045 if (cmp->lbearing > left + lbearing)
23046 cmp->lbearing = left + lbearing;
23047 if (cmp->rbearing < left + rbearing)
23048 cmp->rbearing = left + rbearing;
23049 }
23050 }
23051
23052 /* If there are glyphs whose x-offsets are negative,
23053 shift all glyphs to the right and make all x-offsets
23054 non-negative. */
23055 if (leftmost < 0)
23056 {
23057 for (i = 0; i < cmp->glyph_len; i++)
23058 cmp->offsets[i * 2] -= leftmost;
23059 rightmost -= leftmost;
23060 cmp->lbearing -= leftmost;
23061 cmp->rbearing -= leftmost;
23062 }
23063
23064 if (left_padded && cmp->lbearing < 0)
23065 {
23066 for (i = 0; i < cmp->glyph_len; i++)
23067 cmp->offsets[i * 2] -= cmp->lbearing;
23068 rightmost -= cmp->lbearing;
23069 cmp->rbearing -= cmp->lbearing;
23070 cmp->lbearing = 0;
23071 }
23072 if (right_padded && rightmost < cmp->rbearing)
23073 {
23074 rightmost = cmp->rbearing;
23075 }
23076
23077 cmp->pixel_width = rightmost;
23078 cmp->ascent = highest;
23079 cmp->descent = - lowest;
23080 if (cmp->ascent < font_ascent)
23081 cmp->ascent = font_ascent;
23082 if (cmp->descent < font_descent)
23083 cmp->descent = font_descent;
23084 }
23085
23086 if (it->glyph_row
23087 && (cmp->lbearing < 0
23088 || cmp->rbearing > cmp->pixel_width))
23089 it->glyph_row->contains_overlapping_glyphs_p = 1;
23090
23091 it->pixel_width = cmp->pixel_width;
23092 it->ascent = it->phys_ascent = cmp->ascent;
23093 it->descent = it->phys_descent = cmp->descent;
23094 if (face->box != FACE_NO_BOX)
23095 {
23096 int thick = face->box_line_width;
23097
23098 if (thick > 0)
23099 {
23100 it->ascent += thick;
23101 it->descent += thick;
23102 }
23103 else
23104 thick = - thick;
23105
23106 if (it->start_of_box_run_p)
23107 it->pixel_width += thick;
23108 if (it->end_of_box_run_p)
23109 it->pixel_width += thick;
23110 }
23111
23112 /* If face has an overline, add the height of the overline
23113 (1 pixel) and a 1 pixel margin to the character height. */
23114 if (face->overline_p)
23115 it->ascent += overline_margin;
23116
23117 take_vertical_position_into_account (it);
23118 if (it->ascent < 0)
23119 it->ascent = 0;
23120 if (it->descent < 0)
23121 it->descent = 0;
23122
23123 if (it->glyph_row)
23124 append_composite_glyph (it);
23125 }
23126 else if (it->what == IT_COMPOSITION)
23127 {
23128 /* A dynamic (automatic) composition. */
23129 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23130 Lisp_Object gstring;
23131 struct font_metrics metrics;
23132
23133 gstring = composition_gstring_from_id (it->cmp_it.id);
23134 it->pixel_width
23135 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23136 &metrics);
23137 if (it->glyph_row
23138 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23139 it->glyph_row->contains_overlapping_glyphs_p = 1;
23140 it->ascent = it->phys_ascent = metrics.ascent;
23141 it->descent = it->phys_descent = metrics.descent;
23142 if (face->box != FACE_NO_BOX)
23143 {
23144 int thick = face->box_line_width;
23145
23146 if (thick > 0)
23147 {
23148 it->ascent += thick;
23149 it->descent += thick;
23150 }
23151 else
23152 thick = - thick;
23153
23154 if (it->start_of_box_run_p)
23155 it->pixel_width += thick;
23156 if (it->end_of_box_run_p)
23157 it->pixel_width += thick;
23158 }
23159 /* If face has an overline, add the height of the overline
23160 (1 pixel) and a 1 pixel margin to the character height. */
23161 if (face->overline_p)
23162 it->ascent += overline_margin;
23163 take_vertical_position_into_account (it);
23164 if (it->ascent < 0)
23165 it->ascent = 0;
23166 if (it->descent < 0)
23167 it->descent = 0;
23168
23169 if (it->glyph_row)
23170 append_composite_glyph (it);
23171 }
23172 else if (it->what == IT_GLYPHLESS)
23173 produce_glyphless_glyph (it, 0, Qnil);
23174 else if (it->what == IT_IMAGE)
23175 produce_image_glyph (it);
23176 else if (it->what == IT_STRETCH)
23177 produce_stretch_glyph (it);
23178
23179 done:
23180 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23181 because this isn't true for images with `:ascent 100'. */
23182 xassert (it->ascent >= 0 && it->descent >= 0);
23183 if (it->area == TEXT_AREA)
23184 it->current_x += it->pixel_width;
23185
23186 if (extra_line_spacing > 0)
23187 {
23188 it->descent += extra_line_spacing;
23189 if (extra_line_spacing > it->max_extra_line_spacing)
23190 it->max_extra_line_spacing = extra_line_spacing;
23191 }
23192
23193 it->max_ascent = max (it->max_ascent, it->ascent);
23194 it->max_descent = max (it->max_descent, it->descent);
23195 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23196 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23197 }
23198
23199 /* EXPORT for RIF:
23200 Output LEN glyphs starting at START at the nominal cursor position.
23201 Advance the nominal cursor over the text. The global variable
23202 updated_window contains the window being updated, updated_row is
23203 the glyph row being updated, and updated_area is the area of that
23204 row being updated. */
23205
23206 void
23207 x_write_glyphs (struct glyph *start, int len)
23208 {
23209 int x, hpos;
23210
23211 xassert (updated_window && updated_row);
23212 BLOCK_INPUT;
23213
23214 /* Write glyphs. */
23215
23216 hpos = start - updated_row->glyphs[updated_area];
23217 x = draw_glyphs (updated_window, output_cursor.x,
23218 updated_row, updated_area,
23219 hpos, hpos + len,
23220 DRAW_NORMAL_TEXT, 0);
23221
23222 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23223 if (updated_area == TEXT_AREA
23224 && updated_window->phys_cursor_on_p
23225 && updated_window->phys_cursor.vpos == output_cursor.vpos
23226 && updated_window->phys_cursor.hpos >= hpos
23227 && updated_window->phys_cursor.hpos < hpos + len)
23228 updated_window->phys_cursor_on_p = 0;
23229
23230 UNBLOCK_INPUT;
23231
23232 /* Advance the output cursor. */
23233 output_cursor.hpos += len;
23234 output_cursor.x = x;
23235 }
23236
23237
23238 /* EXPORT for RIF:
23239 Insert LEN glyphs from START at the nominal cursor position. */
23240
23241 void
23242 x_insert_glyphs (struct glyph *start, int len)
23243 {
23244 struct frame *f;
23245 struct window *w;
23246 int line_height, shift_by_width, shifted_region_width;
23247 struct glyph_row *row;
23248 struct glyph *glyph;
23249 int frame_x, frame_y;
23250 EMACS_INT hpos;
23251
23252 xassert (updated_window && updated_row);
23253 BLOCK_INPUT;
23254 w = updated_window;
23255 f = XFRAME (WINDOW_FRAME (w));
23256
23257 /* Get the height of the line we are in. */
23258 row = updated_row;
23259 line_height = row->height;
23260
23261 /* Get the width of the glyphs to insert. */
23262 shift_by_width = 0;
23263 for (glyph = start; glyph < start + len; ++glyph)
23264 shift_by_width += glyph->pixel_width;
23265
23266 /* Get the width of the region to shift right. */
23267 shifted_region_width = (window_box_width (w, updated_area)
23268 - output_cursor.x
23269 - shift_by_width);
23270
23271 /* Shift right. */
23272 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23273 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23274
23275 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23276 line_height, shift_by_width);
23277
23278 /* Write the glyphs. */
23279 hpos = start - row->glyphs[updated_area];
23280 draw_glyphs (w, output_cursor.x, row, updated_area,
23281 hpos, hpos + len,
23282 DRAW_NORMAL_TEXT, 0);
23283
23284 /* Advance the output cursor. */
23285 output_cursor.hpos += len;
23286 output_cursor.x += shift_by_width;
23287 UNBLOCK_INPUT;
23288 }
23289
23290
23291 /* EXPORT for RIF:
23292 Erase the current text line from the nominal cursor position
23293 (inclusive) to pixel column TO_X (exclusive). The idea is that
23294 everything from TO_X onward is already erased.
23295
23296 TO_X is a pixel position relative to updated_area of
23297 updated_window. TO_X == -1 means clear to the end of this area. */
23298
23299 void
23300 x_clear_end_of_line (int to_x)
23301 {
23302 struct frame *f;
23303 struct window *w = updated_window;
23304 int max_x, min_y, max_y;
23305 int from_x, from_y, to_y;
23306
23307 xassert (updated_window && updated_row);
23308 f = XFRAME (w->frame);
23309
23310 if (updated_row->full_width_p)
23311 max_x = WINDOW_TOTAL_WIDTH (w);
23312 else
23313 max_x = window_box_width (w, updated_area);
23314 max_y = window_text_bottom_y (w);
23315
23316 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23317 of window. For TO_X > 0, truncate to end of drawing area. */
23318 if (to_x == 0)
23319 return;
23320 else if (to_x < 0)
23321 to_x = max_x;
23322 else
23323 to_x = min (to_x, max_x);
23324
23325 to_y = min (max_y, output_cursor.y + updated_row->height);
23326
23327 /* Notice if the cursor will be cleared by this operation. */
23328 if (!updated_row->full_width_p)
23329 notice_overwritten_cursor (w, updated_area,
23330 output_cursor.x, -1,
23331 updated_row->y,
23332 MATRIX_ROW_BOTTOM_Y (updated_row));
23333
23334 from_x = output_cursor.x;
23335
23336 /* Translate to frame coordinates. */
23337 if (updated_row->full_width_p)
23338 {
23339 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23340 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23341 }
23342 else
23343 {
23344 int area_left = window_box_left (w, updated_area);
23345 from_x += area_left;
23346 to_x += area_left;
23347 }
23348
23349 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23350 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23351 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23352
23353 /* Prevent inadvertently clearing to end of the X window. */
23354 if (to_x > from_x && to_y > from_y)
23355 {
23356 BLOCK_INPUT;
23357 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23358 to_x - from_x, to_y - from_y);
23359 UNBLOCK_INPUT;
23360 }
23361 }
23362
23363 #endif /* HAVE_WINDOW_SYSTEM */
23364
23365
23366 \f
23367 /***********************************************************************
23368 Cursor types
23369 ***********************************************************************/
23370
23371 /* Value is the internal representation of the specified cursor type
23372 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23373 of the bar cursor. */
23374
23375 static enum text_cursor_kinds
23376 get_specified_cursor_type (Lisp_Object arg, int *width)
23377 {
23378 enum text_cursor_kinds type;
23379
23380 if (NILP (arg))
23381 return NO_CURSOR;
23382
23383 if (EQ (arg, Qbox))
23384 return FILLED_BOX_CURSOR;
23385
23386 if (EQ (arg, Qhollow))
23387 return HOLLOW_BOX_CURSOR;
23388
23389 if (EQ (arg, Qbar))
23390 {
23391 *width = 2;
23392 return BAR_CURSOR;
23393 }
23394
23395 if (CONSP (arg)
23396 && EQ (XCAR (arg), Qbar)
23397 && INTEGERP (XCDR (arg))
23398 && XINT (XCDR (arg)) >= 0)
23399 {
23400 *width = XINT (XCDR (arg));
23401 return BAR_CURSOR;
23402 }
23403
23404 if (EQ (arg, Qhbar))
23405 {
23406 *width = 2;
23407 return HBAR_CURSOR;
23408 }
23409
23410 if (CONSP (arg)
23411 && EQ (XCAR (arg), Qhbar)
23412 && INTEGERP (XCDR (arg))
23413 && XINT (XCDR (arg)) >= 0)
23414 {
23415 *width = XINT (XCDR (arg));
23416 return HBAR_CURSOR;
23417 }
23418
23419 /* Treat anything unknown as "hollow box cursor".
23420 It was bad to signal an error; people have trouble fixing
23421 .Xdefaults with Emacs, when it has something bad in it. */
23422 type = HOLLOW_BOX_CURSOR;
23423
23424 return type;
23425 }
23426
23427 /* Set the default cursor types for specified frame. */
23428 void
23429 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23430 {
23431 int width;
23432 Lisp_Object tem;
23433
23434 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23435 FRAME_CURSOR_WIDTH (f) = width;
23436
23437 /* By default, set up the blink-off state depending on the on-state. */
23438
23439 tem = Fassoc (arg, Vblink_cursor_alist);
23440 if (!NILP (tem))
23441 {
23442 FRAME_BLINK_OFF_CURSOR (f)
23443 = get_specified_cursor_type (XCDR (tem), &width);
23444 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23445 }
23446 else
23447 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23448 }
23449
23450
23451 #ifdef HAVE_WINDOW_SYSTEM
23452
23453 /* Return the cursor we want to be displayed in window W. Return
23454 width of bar/hbar cursor through WIDTH arg. Return with
23455 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23456 (i.e. if the `system caret' should track this cursor).
23457
23458 In a mini-buffer window, we want the cursor only to appear if we
23459 are reading input from this window. For the selected window, we
23460 want the cursor type given by the frame parameter or buffer local
23461 setting of cursor-type. If explicitly marked off, draw no cursor.
23462 In all other cases, we want a hollow box cursor. */
23463
23464 static enum text_cursor_kinds
23465 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23466 int *active_cursor)
23467 {
23468 struct frame *f = XFRAME (w->frame);
23469 struct buffer *b = XBUFFER (w->buffer);
23470 int cursor_type = DEFAULT_CURSOR;
23471 Lisp_Object alt_cursor;
23472 int non_selected = 0;
23473
23474 *active_cursor = 1;
23475
23476 /* Echo area */
23477 if (cursor_in_echo_area
23478 && FRAME_HAS_MINIBUF_P (f)
23479 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23480 {
23481 if (w == XWINDOW (echo_area_window))
23482 {
23483 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23484 {
23485 *width = FRAME_CURSOR_WIDTH (f);
23486 return FRAME_DESIRED_CURSOR (f);
23487 }
23488 else
23489 return get_specified_cursor_type (b->cursor_type, width);
23490 }
23491
23492 *active_cursor = 0;
23493 non_selected = 1;
23494 }
23495
23496 /* Detect a nonselected window or nonselected frame. */
23497 else if (w != XWINDOW (f->selected_window)
23498 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23499 {
23500 *active_cursor = 0;
23501
23502 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23503 return NO_CURSOR;
23504
23505 non_selected = 1;
23506 }
23507
23508 /* Never display a cursor in a window in which cursor-type is nil. */
23509 if (NILP (b->cursor_type))
23510 return NO_CURSOR;
23511
23512 /* Get the normal cursor type for this window. */
23513 if (EQ (b->cursor_type, Qt))
23514 {
23515 cursor_type = FRAME_DESIRED_CURSOR (f);
23516 *width = FRAME_CURSOR_WIDTH (f);
23517 }
23518 else
23519 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23520
23521 /* Use cursor-in-non-selected-windows instead
23522 for non-selected window or frame. */
23523 if (non_selected)
23524 {
23525 alt_cursor = b->cursor_in_non_selected_windows;
23526 if (!EQ (Qt, alt_cursor))
23527 return get_specified_cursor_type (alt_cursor, width);
23528 /* t means modify the normal cursor type. */
23529 if (cursor_type == FILLED_BOX_CURSOR)
23530 cursor_type = HOLLOW_BOX_CURSOR;
23531 else if (cursor_type == BAR_CURSOR && *width > 1)
23532 --*width;
23533 return cursor_type;
23534 }
23535
23536 /* Use normal cursor if not blinked off. */
23537 if (!w->cursor_off_p)
23538 {
23539 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23540 {
23541 if (cursor_type == FILLED_BOX_CURSOR)
23542 {
23543 /* Using a block cursor on large images can be very annoying.
23544 So use a hollow cursor for "large" images.
23545 If image is not transparent (no mask), also use hollow cursor. */
23546 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23547 if (img != NULL && IMAGEP (img->spec))
23548 {
23549 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23550 where N = size of default frame font size.
23551 This should cover most of the "tiny" icons people may use. */
23552 if (!img->mask
23553 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23554 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23555 cursor_type = HOLLOW_BOX_CURSOR;
23556 }
23557 }
23558 else if (cursor_type != NO_CURSOR)
23559 {
23560 /* Display current only supports BOX and HOLLOW cursors for images.
23561 So for now, unconditionally use a HOLLOW cursor when cursor is
23562 not a solid box cursor. */
23563 cursor_type = HOLLOW_BOX_CURSOR;
23564 }
23565 }
23566 return cursor_type;
23567 }
23568
23569 /* Cursor is blinked off, so determine how to "toggle" it. */
23570
23571 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23572 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23573 return get_specified_cursor_type (XCDR (alt_cursor), width);
23574
23575 /* Then see if frame has specified a specific blink off cursor type. */
23576 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23577 {
23578 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23579 return FRAME_BLINK_OFF_CURSOR (f);
23580 }
23581
23582 #if 0
23583 /* Some people liked having a permanently visible blinking cursor,
23584 while others had very strong opinions against it. So it was
23585 decided to remove it. KFS 2003-09-03 */
23586
23587 /* Finally perform built-in cursor blinking:
23588 filled box <-> hollow box
23589 wide [h]bar <-> narrow [h]bar
23590 narrow [h]bar <-> no cursor
23591 other type <-> no cursor */
23592
23593 if (cursor_type == FILLED_BOX_CURSOR)
23594 return HOLLOW_BOX_CURSOR;
23595
23596 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23597 {
23598 *width = 1;
23599 return cursor_type;
23600 }
23601 #endif
23602
23603 return NO_CURSOR;
23604 }
23605
23606
23607 /* Notice when the text cursor of window W has been completely
23608 overwritten by a drawing operation that outputs glyphs in AREA
23609 starting at X0 and ending at X1 in the line starting at Y0 and
23610 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23611 the rest of the line after X0 has been written. Y coordinates
23612 are window-relative. */
23613
23614 static void
23615 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23616 int x0, int x1, int y0, int y1)
23617 {
23618 int cx0, cx1, cy0, cy1;
23619 struct glyph_row *row;
23620
23621 if (!w->phys_cursor_on_p)
23622 return;
23623 if (area != TEXT_AREA)
23624 return;
23625
23626 if (w->phys_cursor.vpos < 0
23627 || w->phys_cursor.vpos >= w->current_matrix->nrows
23628 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23629 !(row->enabled_p && row->displays_text_p)))
23630 return;
23631
23632 if (row->cursor_in_fringe_p)
23633 {
23634 row->cursor_in_fringe_p = 0;
23635 draw_fringe_bitmap (w, row, row->reversed_p);
23636 w->phys_cursor_on_p = 0;
23637 return;
23638 }
23639
23640 cx0 = w->phys_cursor.x;
23641 cx1 = cx0 + w->phys_cursor_width;
23642 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23643 return;
23644
23645 /* The cursor image will be completely removed from the
23646 screen if the output area intersects the cursor area in
23647 y-direction. When we draw in [y0 y1[, and some part of
23648 the cursor is at y < y0, that part must have been drawn
23649 before. When scrolling, the cursor is erased before
23650 actually scrolling, so we don't come here. When not
23651 scrolling, the rows above the old cursor row must have
23652 changed, and in this case these rows must have written
23653 over the cursor image.
23654
23655 Likewise if part of the cursor is below y1, with the
23656 exception of the cursor being in the first blank row at
23657 the buffer and window end because update_text_area
23658 doesn't draw that row. (Except when it does, but
23659 that's handled in update_text_area.) */
23660
23661 cy0 = w->phys_cursor.y;
23662 cy1 = cy0 + w->phys_cursor_height;
23663 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23664 return;
23665
23666 w->phys_cursor_on_p = 0;
23667 }
23668
23669 #endif /* HAVE_WINDOW_SYSTEM */
23670
23671 \f
23672 /************************************************************************
23673 Mouse Face
23674 ************************************************************************/
23675
23676 #ifdef HAVE_WINDOW_SYSTEM
23677
23678 /* EXPORT for RIF:
23679 Fix the display of area AREA of overlapping row ROW in window W
23680 with respect to the overlapping part OVERLAPS. */
23681
23682 void
23683 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23684 enum glyph_row_area area, int overlaps)
23685 {
23686 int i, x;
23687
23688 BLOCK_INPUT;
23689
23690 x = 0;
23691 for (i = 0; i < row->used[area];)
23692 {
23693 if (row->glyphs[area][i].overlaps_vertically_p)
23694 {
23695 int start = i, start_x = x;
23696
23697 do
23698 {
23699 x += row->glyphs[area][i].pixel_width;
23700 ++i;
23701 }
23702 while (i < row->used[area]
23703 && row->glyphs[area][i].overlaps_vertically_p);
23704
23705 draw_glyphs (w, start_x, row, area,
23706 start, i,
23707 DRAW_NORMAL_TEXT, overlaps);
23708 }
23709 else
23710 {
23711 x += row->glyphs[area][i].pixel_width;
23712 ++i;
23713 }
23714 }
23715
23716 UNBLOCK_INPUT;
23717 }
23718
23719
23720 /* EXPORT:
23721 Draw the cursor glyph of window W in glyph row ROW. See the
23722 comment of draw_glyphs for the meaning of HL. */
23723
23724 void
23725 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23726 enum draw_glyphs_face hl)
23727 {
23728 /* If cursor hpos is out of bounds, don't draw garbage. This can
23729 happen in mini-buffer windows when switching between echo area
23730 glyphs and mini-buffer. */
23731 if ((row->reversed_p
23732 ? (w->phys_cursor.hpos >= 0)
23733 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23734 {
23735 int on_p = w->phys_cursor_on_p;
23736 int x1;
23737 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23738 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23739 hl, 0);
23740 w->phys_cursor_on_p = on_p;
23741
23742 if (hl == DRAW_CURSOR)
23743 w->phys_cursor_width = x1 - w->phys_cursor.x;
23744 /* When we erase the cursor, and ROW is overlapped by other
23745 rows, make sure that these overlapping parts of other rows
23746 are redrawn. */
23747 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23748 {
23749 w->phys_cursor_width = x1 - w->phys_cursor.x;
23750
23751 if (row > w->current_matrix->rows
23752 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23753 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23754 OVERLAPS_ERASED_CURSOR);
23755
23756 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23757 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23758 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23759 OVERLAPS_ERASED_CURSOR);
23760 }
23761 }
23762 }
23763
23764
23765 /* EXPORT:
23766 Erase the image of a cursor of window W from the screen. */
23767
23768 void
23769 erase_phys_cursor (struct window *w)
23770 {
23771 struct frame *f = XFRAME (w->frame);
23772 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23773 int hpos = w->phys_cursor.hpos;
23774 int vpos = w->phys_cursor.vpos;
23775 int mouse_face_here_p = 0;
23776 struct glyph_matrix *active_glyphs = w->current_matrix;
23777 struct glyph_row *cursor_row;
23778 struct glyph *cursor_glyph;
23779 enum draw_glyphs_face hl;
23780
23781 /* No cursor displayed or row invalidated => nothing to do on the
23782 screen. */
23783 if (w->phys_cursor_type == NO_CURSOR)
23784 goto mark_cursor_off;
23785
23786 /* VPOS >= active_glyphs->nrows means that window has been resized.
23787 Don't bother to erase the cursor. */
23788 if (vpos >= active_glyphs->nrows)
23789 goto mark_cursor_off;
23790
23791 /* If row containing cursor is marked invalid, there is nothing we
23792 can do. */
23793 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23794 if (!cursor_row->enabled_p)
23795 goto mark_cursor_off;
23796
23797 /* If line spacing is > 0, old cursor may only be partially visible in
23798 window after split-window. So adjust visible height. */
23799 cursor_row->visible_height = min (cursor_row->visible_height,
23800 window_text_bottom_y (w) - cursor_row->y);
23801
23802 /* If row is completely invisible, don't attempt to delete a cursor which
23803 isn't there. This can happen if cursor is at top of a window, and
23804 we switch to a buffer with a header line in that window. */
23805 if (cursor_row->visible_height <= 0)
23806 goto mark_cursor_off;
23807
23808 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23809 if (cursor_row->cursor_in_fringe_p)
23810 {
23811 cursor_row->cursor_in_fringe_p = 0;
23812 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23813 goto mark_cursor_off;
23814 }
23815
23816 /* This can happen when the new row is shorter than the old one.
23817 In this case, either draw_glyphs or clear_end_of_line
23818 should have cleared the cursor. Note that we wouldn't be
23819 able to erase the cursor in this case because we don't have a
23820 cursor glyph at hand. */
23821 if ((cursor_row->reversed_p
23822 ? (w->phys_cursor.hpos < 0)
23823 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23824 goto mark_cursor_off;
23825
23826 /* If the cursor is in the mouse face area, redisplay that when
23827 we clear the cursor. */
23828 if (! NILP (hlinfo->mouse_face_window)
23829 && coords_in_mouse_face_p (w, hpos, vpos)
23830 /* Don't redraw the cursor's spot in mouse face if it is at the
23831 end of a line (on a newline). The cursor appears there, but
23832 mouse highlighting does not. */
23833 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23834 mouse_face_here_p = 1;
23835
23836 /* Maybe clear the display under the cursor. */
23837 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23838 {
23839 int x, y, left_x;
23840 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23841 int width;
23842
23843 cursor_glyph = get_phys_cursor_glyph (w);
23844 if (cursor_glyph == NULL)
23845 goto mark_cursor_off;
23846
23847 width = cursor_glyph->pixel_width;
23848 left_x = window_box_left_offset (w, TEXT_AREA);
23849 x = w->phys_cursor.x;
23850 if (x < left_x)
23851 width -= left_x - x;
23852 width = min (width, window_box_width (w, TEXT_AREA) - x);
23853 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23854 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23855
23856 if (width > 0)
23857 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23858 }
23859
23860 /* Erase the cursor by redrawing the character underneath it. */
23861 if (mouse_face_here_p)
23862 hl = DRAW_MOUSE_FACE;
23863 else
23864 hl = DRAW_NORMAL_TEXT;
23865 draw_phys_cursor_glyph (w, cursor_row, hl);
23866
23867 mark_cursor_off:
23868 w->phys_cursor_on_p = 0;
23869 w->phys_cursor_type = NO_CURSOR;
23870 }
23871
23872
23873 /* EXPORT:
23874 Display or clear cursor of window W. If ON is zero, clear the
23875 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23876 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23877
23878 void
23879 display_and_set_cursor (struct window *w, int on,
23880 int hpos, int vpos, int x, int y)
23881 {
23882 struct frame *f = XFRAME (w->frame);
23883 int new_cursor_type;
23884 int new_cursor_width;
23885 int active_cursor;
23886 struct glyph_row *glyph_row;
23887 struct glyph *glyph;
23888
23889 /* This is pointless on invisible frames, and dangerous on garbaged
23890 windows and frames; in the latter case, the frame or window may
23891 be in the midst of changing its size, and x and y may be off the
23892 window. */
23893 if (! FRAME_VISIBLE_P (f)
23894 || FRAME_GARBAGED_P (f)
23895 || vpos >= w->current_matrix->nrows
23896 || hpos >= w->current_matrix->matrix_w)
23897 return;
23898
23899 /* If cursor is off and we want it off, return quickly. */
23900 if (!on && !w->phys_cursor_on_p)
23901 return;
23902
23903 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23904 /* If cursor row is not enabled, we don't really know where to
23905 display the cursor. */
23906 if (!glyph_row->enabled_p)
23907 {
23908 w->phys_cursor_on_p = 0;
23909 return;
23910 }
23911
23912 glyph = NULL;
23913 if (!glyph_row->exact_window_width_line_p
23914 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23915 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23916
23917 xassert (interrupt_input_blocked);
23918
23919 /* Set new_cursor_type to the cursor we want to be displayed. */
23920 new_cursor_type = get_window_cursor_type (w, glyph,
23921 &new_cursor_width, &active_cursor);
23922
23923 /* If cursor is currently being shown and we don't want it to be or
23924 it is in the wrong place, or the cursor type is not what we want,
23925 erase it. */
23926 if (w->phys_cursor_on_p
23927 && (!on
23928 || w->phys_cursor.x != x
23929 || w->phys_cursor.y != y
23930 || new_cursor_type != w->phys_cursor_type
23931 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23932 && new_cursor_width != w->phys_cursor_width)))
23933 erase_phys_cursor (w);
23934
23935 /* Don't check phys_cursor_on_p here because that flag is only set
23936 to zero in some cases where we know that the cursor has been
23937 completely erased, to avoid the extra work of erasing the cursor
23938 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23939 still not be visible, or it has only been partly erased. */
23940 if (on)
23941 {
23942 w->phys_cursor_ascent = glyph_row->ascent;
23943 w->phys_cursor_height = glyph_row->height;
23944
23945 /* Set phys_cursor_.* before x_draw_.* is called because some
23946 of them may need the information. */
23947 w->phys_cursor.x = x;
23948 w->phys_cursor.y = glyph_row->y;
23949 w->phys_cursor.hpos = hpos;
23950 w->phys_cursor.vpos = vpos;
23951 }
23952
23953 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23954 new_cursor_type, new_cursor_width,
23955 on, active_cursor);
23956 }
23957
23958
23959 /* Switch the display of W's cursor on or off, according to the value
23960 of ON. */
23961
23962 void
23963 update_window_cursor (struct window *w, int on)
23964 {
23965 /* Don't update cursor in windows whose frame is in the process
23966 of being deleted. */
23967 if (w->current_matrix)
23968 {
23969 BLOCK_INPUT;
23970 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23971 w->phys_cursor.x, w->phys_cursor.y);
23972 UNBLOCK_INPUT;
23973 }
23974 }
23975
23976
23977 /* Call update_window_cursor with parameter ON_P on all leaf windows
23978 in the window tree rooted at W. */
23979
23980 static void
23981 update_cursor_in_window_tree (struct window *w, int on_p)
23982 {
23983 while (w)
23984 {
23985 if (!NILP (w->hchild))
23986 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23987 else if (!NILP (w->vchild))
23988 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23989 else
23990 update_window_cursor (w, on_p);
23991
23992 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23993 }
23994 }
23995
23996
23997 /* EXPORT:
23998 Display the cursor on window W, or clear it, according to ON_P.
23999 Don't change the cursor's position. */
24000
24001 void
24002 x_update_cursor (struct frame *f, int on_p)
24003 {
24004 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24005 }
24006
24007
24008 /* EXPORT:
24009 Clear the cursor of window W to background color, and mark the
24010 cursor as not shown. This is used when the text where the cursor
24011 is about to be rewritten. */
24012
24013 void
24014 x_clear_cursor (struct window *w)
24015 {
24016 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24017 update_window_cursor (w, 0);
24018 }
24019
24020 #endif /* HAVE_WINDOW_SYSTEM */
24021
24022 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24023 and MSDOS. */
24024 void
24025 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24026 int start_hpos, int end_hpos,
24027 enum draw_glyphs_face draw)
24028 {
24029 #ifdef HAVE_WINDOW_SYSTEM
24030 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24031 {
24032 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24033 return;
24034 }
24035 #endif
24036 #if defined (HAVE_GPM) || defined (MSDOS)
24037 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24038 #endif
24039 }
24040
24041 /* EXPORT:
24042 Display the active region described by mouse_face_* according to DRAW. */
24043
24044 void
24045 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24046 {
24047 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24048 struct frame *f = XFRAME (WINDOW_FRAME (w));
24049
24050 if (/* If window is in the process of being destroyed, don't bother
24051 to do anything. */
24052 w->current_matrix != NULL
24053 /* Don't update mouse highlight if hidden */
24054 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24055 /* Recognize when we are called to operate on rows that don't exist
24056 anymore. This can happen when a window is split. */
24057 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24058 {
24059 int phys_cursor_on_p = w->phys_cursor_on_p;
24060 struct glyph_row *row, *first, *last;
24061
24062 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24063 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24064
24065 for (row = first; row <= last && row->enabled_p; ++row)
24066 {
24067 int start_hpos, end_hpos, start_x;
24068
24069 /* For all but the first row, the highlight starts at column 0. */
24070 if (row == first)
24071 {
24072 /* R2L rows have BEG and END in reversed order, but the
24073 screen drawing geometry is always left to right. So
24074 we need to mirror the beginning and end of the
24075 highlighted area in R2L rows. */
24076 if (!row->reversed_p)
24077 {
24078 start_hpos = hlinfo->mouse_face_beg_col;
24079 start_x = hlinfo->mouse_face_beg_x;
24080 }
24081 else if (row == last)
24082 {
24083 start_hpos = hlinfo->mouse_face_end_col;
24084 start_x = hlinfo->mouse_face_end_x;
24085 }
24086 else
24087 {
24088 start_hpos = 0;
24089 start_x = 0;
24090 }
24091 }
24092 else if (row->reversed_p && row == last)
24093 {
24094 start_hpos = hlinfo->mouse_face_end_col;
24095 start_x = hlinfo->mouse_face_end_x;
24096 }
24097 else
24098 {
24099 start_hpos = 0;
24100 start_x = 0;
24101 }
24102
24103 if (row == last)
24104 {
24105 if (!row->reversed_p)
24106 end_hpos = hlinfo->mouse_face_end_col;
24107 else if (row == first)
24108 end_hpos = hlinfo->mouse_face_beg_col;
24109 else
24110 {
24111 end_hpos = row->used[TEXT_AREA];
24112 if (draw == DRAW_NORMAL_TEXT)
24113 row->fill_line_p = 1; /* Clear to end of line */
24114 }
24115 }
24116 else if (row->reversed_p && row == first)
24117 end_hpos = hlinfo->mouse_face_beg_col;
24118 else
24119 {
24120 end_hpos = row->used[TEXT_AREA];
24121 if (draw == DRAW_NORMAL_TEXT)
24122 row->fill_line_p = 1; /* Clear to end of line */
24123 }
24124
24125 if (end_hpos > start_hpos)
24126 {
24127 draw_row_with_mouse_face (w, start_x, row,
24128 start_hpos, end_hpos, draw);
24129
24130 row->mouse_face_p
24131 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24132 }
24133 }
24134
24135 #ifdef HAVE_WINDOW_SYSTEM
24136 /* When we've written over the cursor, arrange for it to
24137 be displayed again. */
24138 if (FRAME_WINDOW_P (f)
24139 && phys_cursor_on_p && !w->phys_cursor_on_p)
24140 {
24141 BLOCK_INPUT;
24142 display_and_set_cursor (w, 1,
24143 w->phys_cursor.hpos, w->phys_cursor.vpos,
24144 w->phys_cursor.x, w->phys_cursor.y);
24145 UNBLOCK_INPUT;
24146 }
24147 #endif /* HAVE_WINDOW_SYSTEM */
24148 }
24149
24150 #ifdef HAVE_WINDOW_SYSTEM
24151 /* Change the mouse cursor. */
24152 if (FRAME_WINDOW_P (f))
24153 {
24154 if (draw == DRAW_NORMAL_TEXT
24155 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24156 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24157 else if (draw == DRAW_MOUSE_FACE)
24158 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24159 else
24160 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24161 }
24162 #endif /* HAVE_WINDOW_SYSTEM */
24163 }
24164
24165 /* EXPORT:
24166 Clear out the mouse-highlighted active region.
24167 Redraw it un-highlighted first. Value is non-zero if mouse
24168 face was actually drawn unhighlighted. */
24169
24170 int
24171 clear_mouse_face (Mouse_HLInfo *hlinfo)
24172 {
24173 int cleared = 0;
24174
24175 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24176 {
24177 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24178 cleared = 1;
24179 }
24180
24181 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24182 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24183 hlinfo->mouse_face_window = Qnil;
24184 hlinfo->mouse_face_overlay = Qnil;
24185 return cleared;
24186 }
24187
24188 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24189 within the mouse face on that window. */
24190 static int
24191 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24192 {
24193 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24194
24195 /* Quickly resolve the easy cases. */
24196 if (!(WINDOWP (hlinfo->mouse_face_window)
24197 && XWINDOW (hlinfo->mouse_face_window) == w))
24198 return 0;
24199 if (vpos < hlinfo->mouse_face_beg_row
24200 || vpos > hlinfo->mouse_face_end_row)
24201 return 0;
24202 if (vpos > hlinfo->mouse_face_beg_row
24203 && vpos < hlinfo->mouse_face_end_row)
24204 return 1;
24205
24206 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24207 {
24208 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24209 {
24210 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24211 return 1;
24212 }
24213 else if ((vpos == hlinfo->mouse_face_beg_row
24214 && hpos >= hlinfo->mouse_face_beg_col)
24215 || (vpos == hlinfo->mouse_face_end_row
24216 && hpos < hlinfo->mouse_face_end_col))
24217 return 1;
24218 }
24219 else
24220 {
24221 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24222 {
24223 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24224 return 1;
24225 }
24226 else if ((vpos == hlinfo->mouse_face_beg_row
24227 && hpos <= hlinfo->mouse_face_beg_col)
24228 || (vpos == hlinfo->mouse_face_end_row
24229 && hpos > hlinfo->mouse_face_end_col))
24230 return 1;
24231 }
24232 return 0;
24233 }
24234
24235
24236 /* EXPORT:
24237 Non-zero if physical cursor of window W is within mouse face. */
24238
24239 int
24240 cursor_in_mouse_face_p (struct window *w)
24241 {
24242 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24243 }
24244
24245
24246 \f
24247 /* Find the glyph rows START_ROW and END_ROW of window W that display
24248 characters between buffer positions START_CHARPOS and END_CHARPOS
24249 (excluding END_CHARPOS). This is similar to row_containing_pos,
24250 but is more accurate when bidi reordering makes buffer positions
24251 change non-linearly with glyph rows. */
24252 static void
24253 rows_from_pos_range (struct window *w,
24254 EMACS_INT start_charpos, EMACS_INT end_charpos,
24255 struct glyph_row **start, struct glyph_row **end)
24256 {
24257 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24258 int last_y = window_text_bottom_y (w);
24259 struct glyph_row *row;
24260
24261 *start = NULL;
24262 *end = NULL;
24263
24264 while (!first->enabled_p
24265 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24266 first++;
24267
24268 /* Find the START row. */
24269 for (row = first;
24270 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24271 row++)
24272 {
24273 /* A row can potentially be the START row if the range of the
24274 characters it displays intersects the range
24275 [START_CHARPOS..END_CHARPOS). */
24276 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24277 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24278 /* See the commentary in row_containing_pos, for the
24279 explanation of the complicated way to check whether
24280 some position is beyond the end of the characters
24281 displayed by a row. */
24282 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24283 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24284 && !row->ends_at_zv_p
24285 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24286 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24287 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24288 && !row->ends_at_zv_p
24289 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24290 {
24291 /* Found a candidate row. Now make sure at least one of the
24292 glyphs it displays has a charpos from the range
24293 [START_CHARPOS..END_CHARPOS).
24294
24295 This is not obvious because bidi reordering could make
24296 buffer positions of a row be 1,2,3,102,101,100, and if we
24297 want to highlight characters in [50..60), we don't want
24298 this row, even though [50..60) does intersect [1..103),
24299 the range of character positions given by the row's start
24300 and end positions. */
24301 struct glyph *g = row->glyphs[TEXT_AREA];
24302 struct glyph *e = g + row->used[TEXT_AREA];
24303
24304 while (g < e)
24305 {
24306 if (BUFFERP (g->object)
24307 && start_charpos <= g->charpos && g->charpos < end_charpos)
24308 *start = row;
24309 g++;
24310 }
24311 if (*start)
24312 break;
24313 }
24314 }
24315
24316 /* Find the END row. */
24317 if (!*start
24318 /* If the last row is partially visible, start looking for END
24319 from that row, instead of starting from FIRST. */
24320 && !(row->enabled_p
24321 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24322 row = first;
24323 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24324 {
24325 struct glyph_row *next = row + 1;
24326
24327 if (!next->enabled_p
24328 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24329 /* The first row >= START whose range of displayed characters
24330 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24331 is the row END + 1. */
24332 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24333 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24334 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24335 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24336 && !next->ends_at_zv_p
24337 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24338 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24339 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24340 && !next->ends_at_zv_p
24341 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24342 {
24343 *end = row;
24344 break;
24345 }
24346 else
24347 {
24348 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24349 but none of the characters it displays are in the range, it is
24350 also END + 1. */
24351 struct glyph *g = next->glyphs[TEXT_AREA];
24352 struct glyph *e = g + next->used[TEXT_AREA];
24353
24354 while (g < e)
24355 {
24356 if (BUFFERP (g->object)
24357 && start_charpos <= g->charpos && g->charpos < end_charpos)
24358 break;
24359 g++;
24360 }
24361 if (g == e)
24362 {
24363 *end = row;
24364 break;
24365 }
24366 }
24367 }
24368 }
24369
24370 /* This function sets the mouse_face_* elements of HLINFO, assuming
24371 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24372 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24373 for the overlay or run of text properties specifying the mouse
24374 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24375 before-string and after-string that must also be highlighted.
24376 DISPLAY_STRING, if non-nil, is a display string that may cover some
24377 or all of the highlighted text. */
24378
24379 static void
24380 mouse_face_from_buffer_pos (Lisp_Object window,
24381 Mouse_HLInfo *hlinfo,
24382 EMACS_INT mouse_charpos,
24383 EMACS_INT start_charpos,
24384 EMACS_INT end_charpos,
24385 Lisp_Object before_string,
24386 Lisp_Object after_string,
24387 Lisp_Object display_string)
24388 {
24389 struct window *w = XWINDOW (window);
24390 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24391 struct glyph_row *r1, *r2;
24392 struct glyph *glyph, *end;
24393 EMACS_INT ignore, pos;
24394 int x;
24395
24396 xassert (NILP (display_string) || STRINGP (display_string));
24397 xassert (NILP (before_string) || STRINGP (before_string));
24398 xassert (NILP (after_string) || STRINGP (after_string));
24399
24400 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24401 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24402 if (r1 == NULL)
24403 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24404 /* If the before-string or display-string contains newlines,
24405 rows_from_pos_range skips to its last row. Move back. */
24406 if (!NILP (before_string) || !NILP (display_string))
24407 {
24408 struct glyph_row *prev;
24409 while ((prev = r1 - 1, prev >= first)
24410 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24411 && prev->used[TEXT_AREA] > 0)
24412 {
24413 struct glyph *beg = prev->glyphs[TEXT_AREA];
24414 glyph = beg + prev->used[TEXT_AREA];
24415 while (--glyph >= beg && INTEGERP (glyph->object));
24416 if (glyph < beg
24417 || !(EQ (glyph->object, before_string)
24418 || EQ (glyph->object, display_string)))
24419 break;
24420 r1 = prev;
24421 }
24422 }
24423 if (r2 == NULL)
24424 {
24425 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24426 hlinfo->mouse_face_past_end = 1;
24427 }
24428 else if (!NILP (after_string))
24429 {
24430 /* If the after-string has newlines, advance to its last row. */
24431 struct glyph_row *next;
24432 struct glyph_row *last
24433 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24434
24435 for (next = r2 + 1;
24436 next <= last
24437 && next->used[TEXT_AREA] > 0
24438 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24439 ++next)
24440 r2 = next;
24441 }
24442 /* The rest of the display engine assumes that mouse_face_beg_row is
24443 either above below mouse_face_end_row or identical to it. But
24444 with bidi-reordered continued lines, the row for START_CHARPOS
24445 could be below the row for END_CHARPOS. If so, swap the rows and
24446 store them in correct order. */
24447 if (r1->y > r2->y)
24448 {
24449 struct glyph_row *tem = r2;
24450
24451 r2 = r1;
24452 r1 = tem;
24453 }
24454
24455 hlinfo->mouse_face_beg_y = r1->y;
24456 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24457 hlinfo->mouse_face_end_y = r2->y;
24458 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24459
24460 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24461 AFTER_STRING, DISPLAY_STRING, START_CHARPOS, and END_CHARPOS
24462 could be anywhere in the row and in any order. The strategy
24463 below is to find the leftmost and the rightmost glyph that
24464 belongs to either of these 3 strings, or whose position is
24465 between START_CHARPOS and END_CHARPOS, and highlight all the
24466 glyphs between those two. This may cover more than just the text
24467 between START_CHARPOS and END_CHARPOS if the range of characters
24468 strides the bidi level boundary, e.g. if the beginning is in R2L
24469 text while the end is in L2R text or vice versa. */
24470 if (!r1->reversed_p)
24471 {
24472 /* This row is in a left to right paragraph. Scan it left to
24473 right. */
24474 glyph = r1->glyphs[TEXT_AREA];
24475 end = glyph + r1->used[TEXT_AREA];
24476 x = r1->x;
24477
24478 /* Skip truncation glyphs at the start of the glyph row. */
24479 if (r1->displays_text_p)
24480 for (; glyph < end
24481 && INTEGERP (glyph->object)
24482 && glyph->charpos < 0;
24483 ++glyph)
24484 x += glyph->pixel_width;
24485
24486 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24487 or DISPLAY_STRING, and the first glyph from buffer whose
24488 position is between START_CHARPOS and END_CHARPOS. */
24489 for (; glyph < end
24490 && !INTEGERP (glyph->object)
24491 && !EQ (glyph->object, display_string)
24492 && !(BUFFERP (glyph->object)
24493 && (glyph->charpos >= start_charpos
24494 && glyph->charpos < end_charpos));
24495 ++glyph)
24496 {
24497 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24498 are present at buffer positions between START_CHARPOS and
24499 END_CHARPOS, or if they come from an overlay. */
24500 if (EQ (glyph->object, before_string))
24501 {
24502 pos = string_buffer_position (w, before_string,
24503 start_charpos);
24504 /* If pos == 0, it means before_string came from an
24505 overlay, not from a buffer position. */
24506 if (!pos || (pos >= start_charpos && pos < end_charpos))
24507 break;
24508 }
24509 else if (EQ (glyph->object, after_string))
24510 {
24511 pos = string_buffer_position (w, after_string, end_charpos);
24512 if (!pos || (pos >= start_charpos && pos < end_charpos))
24513 break;
24514 }
24515 x += glyph->pixel_width;
24516 }
24517 hlinfo->mouse_face_beg_x = x;
24518 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24519 }
24520 else
24521 {
24522 /* This row is in a right to left paragraph. Scan it right to
24523 left. */
24524 struct glyph *g;
24525
24526 end = r1->glyphs[TEXT_AREA] - 1;
24527 glyph = end + r1->used[TEXT_AREA];
24528
24529 /* Skip truncation glyphs at the start of the glyph row. */
24530 if (r1->displays_text_p)
24531 for (; glyph > end
24532 && INTEGERP (glyph->object)
24533 && glyph->charpos < 0;
24534 --glyph)
24535 ;
24536
24537 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24538 or DISPLAY_STRING, and the first glyph from buffer whose
24539 position is between START_CHARPOS and END_CHARPOS. */
24540 for (; glyph > end
24541 && !INTEGERP (glyph->object)
24542 && !EQ (glyph->object, display_string)
24543 && !(BUFFERP (glyph->object)
24544 && (glyph->charpos >= start_charpos
24545 && glyph->charpos < end_charpos));
24546 --glyph)
24547 {
24548 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24549 are present at buffer positions between START_CHARPOS and
24550 END_CHARPOS, or if they come from an overlay. */
24551 if (EQ (glyph->object, before_string))
24552 {
24553 pos = string_buffer_position (w, before_string, start_charpos);
24554 /* If pos == 0, it means before_string came from an
24555 overlay, not from a buffer position. */
24556 if (!pos || (pos >= start_charpos && pos < end_charpos))
24557 break;
24558 }
24559 else if (EQ (glyph->object, after_string))
24560 {
24561 pos = string_buffer_position (w, after_string, end_charpos);
24562 if (!pos || (pos >= start_charpos && pos < end_charpos))
24563 break;
24564 }
24565 }
24566
24567 glyph++; /* first glyph to the right of the highlighted area */
24568 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24569 x += g->pixel_width;
24570 hlinfo->mouse_face_beg_x = x;
24571 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24572 }
24573
24574 /* If the highlight ends in a different row, compute GLYPH and END
24575 for the end row. Otherwise, reuse the values computed above for
24576 the row where the highlight begins. */
24577 if (r2 != r1)
24578 {
24579 if (!r2->reversed_p)
24580 {
24581 glyph = r2->glyphs[TEXT_AREA];
24582 end = glyph + r2->used[TEXT_AREA];
24583 x = r2->x;
24584 }
24585 else
24586 {
24587 end = r2->glyphs[TEXT_AREA] - 1;
24588 glyph = end + r2->used[TEXT_AREA];
24589 }
24590 }
24591
24592 if (!r2->reversed_p)
24593 {
24594 /* Skip truncation and continuation glyphs near the end of the
24595 row, and also blanks and stretch glyphs inserted by
24596 extend_face_to_end_of_line. */
24597 while (end > glyph
24598 && INTEGERP ((end - 1)->object)
24599 && (end - 1)->charpos <= 0)
24600 --end;
24601 /* Scan the rest of the glyph row from the end, looking for the
24602 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24603 DISPLAY_STRING, or whose position is between START_CHARPOS
24604 and END_CHARPOS */
24605 for (--end;
24606 end > glyph
24607 && !INTEGERP (end->object)
24608 && !EQ (end->object, display_string)
24609 && !(BUFFERP (end->object)
24610 && (end->charpos >= start_charpos
24611 && end->charpos < end_charpos));
24612 --end)
24613 {
24614 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24615 are present at buffer positions between START_CHARPOS and
24616 END_CHARPOS, or if they come from an overlay. */
24617 if (EQ (end->object, before_string))
24618 {
24619 pos = string_buffer_position (w, before_string, start_charpos);
24620 if (!pos || (pos >= start_charpos && pos < end_charpos))
24621 break;
24622 }
24623 else if (EQ (end->object, after_string))
24624 {
24625 pos = string_buffer_position (w, after_string, end_charpos);
24626 if (!pos || (pos >= start_charpos && pos < end_charpos))
24627 break;
24628 }
24629 }
24630 /* Find the X coordinate of the last glyph to be highlighted. */
24631 for (; glyph <= end; ++glyph)
24632 x += glyph->pixel_width;
24633
24634 hlinfo->mouse_face_end_x = x;
24635 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24636 }
24637 else
24638 {
24639 /* Skip truncation and continuation glyphs near the end of the
24640 row, and also blanks and stretch glyphs inserted by
24641 extend_face_to_end_of_line. */
24642 x = r2->x;
24643 end++;
24644 while (end < glyph
24645 && INTEGERP (end->object)
24646 && end->charpos <= 0)
24647 {
24648 x += end->pixel_width;
24649 ++end;
24650 }
24651 /* Scan the rest of the glyph row from the end, looking for the
24652 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24653 DISPLAY_STRING, or whose position is between START_CHARPOS
24654 and END_CHARPOS */
24655 for ( ;
24656 end < glyph
24657 && !INTEGERP (end->object)
24658 && !EQ (end->object, display_string)
24659 && !(BUFFERP (end->object)
24660 && (end->charpos >= start_charpos
24661 && end->charpos < end_charpos));
24662 ++end)
24663 {
24664 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24665 are present at buffer positions between START_CHARPOS and
24666 END_CHARPOS, or if they come from an overlay. */
24667 if (EQ (end->object, before_string))
24668 {
24669 pos = string_buffer_position (w, before_string, start_charpos);
24670 if (!pos || (pos >= start_charpos && pos < end_charpos))
24671 break;
24672 }
24673 else if (EQ (end->object, after_string))
24674 {
24675 pos = string_buffer_position (w, after_string, end_charpos);
24676 if (!pos || (pos >= start_charpos && pos < end_charpos))
24677 break;
24678 }
24679 x += end->pixel_width;
24680 }
24681 hlinfo->mouse_face_end_x = x;
24682 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24683 }
24684
24685 hlinfo->mouse_face_window = window;
24686 hlinfo->mouse_face_face_id
24687 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24688 mouse_charpos + 1,
24689 !hlinfo->mouse_face_hidden, -1);
24690 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24691 }
24692
24693 /* The following function is not used anymore (replaced with
24694 mouse_face_from_string_pos), but I leave it here for the time
24695 being, in case someone would. */
24696
24697 #if 0 /* not used */
24698
24699 /* Find the position of the glyph for position POS in OBJECT in
24700 window W's current matrix, and return in *X, *Y the pixel
24701 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24702
24703 RIGHT_P non-zero means return the position of the right edge of the
24704 glyph, RIGHT_P zero means return the left edge position.
24705
24706 If no glyph for POS exists in the matrix, return the position of
24707 the glyph with the next smaller position that is in the matrix, if
24708 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24709 exists in the matrix, return the position of the glyph with the
24710 next larger position in OBJECT.
24711
24712 Value is non-zero if a glyph was found. */
24713
24714 static int
24715 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24716 int *hpos, int *vpos, int *x, int *y, int right_p)
24717 {
24718 int yb = window_text_bottom_y (w);
24719 struct glyph_row *r;
24720 struct glyph *best_glyph = NULL;
24721 struct glyph_row *best_row = NULL;
24722 int best_x = 0;
24723
24724 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24725 r->enabled_p && r->y < yb;
24726 ++r)
24727 {
24728 struct glyph *g = r->glyphs[TEXT_AREA];
24729 struct glyph *e = g + r->used[TEXT_AREA];
24730 int gx;
24731
24732 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24733 if (EQ (g->object, object))
24734 {
24735 if (g->charpos == pos)
24736 {
24737 best_glyph = g;
24738 best_x = gx;
24739 best_row = r;
24740 goto found;
24741 }
24742 else if (best_glyph == NULL
24743 || ((eabs (g->charpos - pos)
24744 < eabs (best_glyph->charpos - pos))
24745 && (right_p
24746 ? g->charpos < pos
24747 : g->charpos > pos)))
24748 {
24749 best_glyph = g;
24750 best_x = gx;
24751 best_row = r;
24752 }
24753 }
24754 }
24755
24756 found:
24757
24758 if (best_glyph)
24759 {
24760 *x = best_x;
24761 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24762
24763 if (right_p)
24764 {
24765 *x += best_glyph->pixel_width;
24766 ++*hpos;
24767 }
24768
24769 *y = best_row->y;
24770 *vpos = best_row - w->current_matrix->rows;
24771 }
24772
24773 return best_glyph != NULL;
24774 }
24775 #endif /* not used */
24776
24777 /* Find the positions of the first and the last glyphs in window W's
24778 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24779 (assumed to be a string), and return in HLINFO's mouse_face_*
24780 members the pixel and column/row coordinates of those glyphs. */
24781
24782 static void
24783 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24784 Lisp_Object object,
24785 EMACS_INT startpos, EMACS_INT endpos)
24786 {
24787 int yb = window_text_bottom_y (w);
24788 struct glyph_row *r;
24789 struct glyph *g, *e;
24790 int gx;
24791 int found = 0;
24792
24793 /* Find the glyph row with at least one position in the range
24794 [STARTPOS..ENDPOS], and the first glyph in that row whose
24795 position belongs to that range. */
24796 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24797 r->enabled_p && r->y < yb;
24798 ++r)
24799 {
24800 if (!r->reversed_p)
24801 {
24802 g = r->glyphs[TEXT_AREA];
24803 e = g + r->used[TEXT_AREA];
24804 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24805 if (EQ (g->object, object)
24806 && startpos <= g->charpos && g->charpos <= endpos)
24807 {
24808 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24809 hlinfo->mouse_face_beg_y = r->y;
24810 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24811 hlinfo->mouse_face_beg_x = gx;
24812 found = 1;
24813 break;
24814 }
24815 }
24816 else
24817 {
24818 struct glyph *g1;
24819
24820 e = r->glyphs[TEXT_AREA];
24821 g = e + r->used[TEXT_AREA];
24822 for ( ; g > e; --g)
24823 if (EQ ((g-1)->object, object)
24824 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24825 {
24826 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24827 hlinfo->mouse_face_beg_y = r->y;
24828 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24829 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24830 gx += g1->pixel_width;
24831 hlinfo->mouse_face_beg_x = gx;
24832 found = 1;
24833 break;
24834 }
24835 }
24836 if (found)
24837 break;
24838 }
24839
24840 if (!found)
24841 return;
24842
24843 /* Starting with the next row, look for the first row which does NOT
24844 include any glyphs whose positions are in the range. */
24845 for (++r; r->enabled_p && r->y < yb; ++r)
24846 {
24847 g = r->glyphs[TEXT_AREA];
24848 e = g + r->used[TEXT_AREA];
24849 found = 0;
24850 for ( ; g < e; ++g)
24851 if (EQ (g->object, object)
24852 && startpos <= g->charpos && g->charpos <= endpos)
24853 {
24854 found = 1;
24855 break;
24856 }
24857 if (!found)
24858 break;
24859 }
24860
24861 /* The highlighted region ends on the previous row. */
24862 r--;
24863
24864 /* Set the end row and its vertical pixel coordinate. */
24865 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24866 hlinfo->mouse_face_end_y = r->y;
24867
24868 /* Compute and set the end column and the end column's horizontal
24869 pixel coordinate. */
24870 if (!r->reversed_p)
24871 {
24872 g = r->glyphs[TEXT_AREA];
24873 e = g + r->used[TEXT_AREA];
24874 for ( ; e > g; --e)
24875 if (EQ ((e-1)->object, object)
24876 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24877 break;
24878 hlinfo->mouse_face_end_col = e - g;
24879
24880 for (gx = r->x; g < e; ++g)
24881 gx += g->pixel_width;
24882 hlinfo->mouse_face_end_x = gx;
24883 }
24884 else
24885 {
24886 e = r->glyphs[TEXT_AREA];
24887 g = e + r->used[TEXT_AREA];
24888 for (gx = r->x ; e < g; ++e)
24889 {
24890 if (EQ (e->object, object)
24891 && startpos <= e->charpos && e->charpos <= endpos)
24892 break;
24893 gx += e->pixel_width;
24894 }
24895 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24896 hlinfo->mouse_face_end_x = gx;
24897 }
24898 }
24899
24900 #ifdef HAVE_WINDOW_SYSTEM
24901
24902 /* See if position X, Y is within a hot-spot of an image. */
24903
24904 static int
24905 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24906 {
24907 if (!CONSP (hot_spot))
24908 return 0;
24909
24910 if (EQ (XCAR (hot_spot), Qrect))
24911 {
24912 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24913 Lisp_Object rect = XCDR (hot_spot);
24914 Lisp_Object tem;
24915 if (!CONSP (rect))
24916 return 0;
24917 if (!CONSP (XCAR (rect)))
24918 return 0;
24919 if (!CONSP (XCDR (rect)))
24920 return 0;
24921 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24922 return 0;
24923 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24924 return 0;
24925 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24926 return 0;
24927 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24928 return 0;
24929 return 1;
24930 }
24931 else if (EQ (XCAR (hot_spot), Qcircle))
24932 {
24933 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24934 Lisp_Object circ = XCDR (hot_spot);
24935 Lisp_Object lr, lx0, ly0;
24936 if (CONSP (circ)
24937 && CONSP (XCAR (circ))
24938 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24939 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24940 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24941 {
24942 double r = XFLOATINT (lr);
24943 double dx = XINT (lx0) - x;
24944 double dy = XINT (ly0) - y;
24945 return (dx * dx + dy * dy <= r * r);
24946 }
24947 }
24948 else if (EQ (XCAR (hot_spot), Qpoly))
24949 {
24950 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24951 if (VECTORP (XCDR (hot_spot)))
24952 {
24953 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24954 Lisp_Object *poly = v->contents;
24955 int n = v->size;
24956 int i;
24957 int inside = 0;
24958 Lisp_Object lx, ly;
24959 int x0, y0;
24960
24961 /* Need an even number of coordinates, and at least 3 edges. */
24962 if (n < 6 || n & 1)
24963 return 0;
24964
24965 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24966 If count is odd, we are inside polygon. Pixels on edges
24967 may or may not be included depending on actual geometry of the
24968 polygon. */
24969 if ((lx = poly[n-2], !INTEGERP (lx))
24970 || (ly = poly[n-1], !INTEGERP (lx)))
24971 return 0;
24972 x0 = XINT (lx), y0 = XINT (ly);
24973 for (i = 0; i < n; i += 2)
24974 {
24975 int x1 = x0, y1 = y0;
24976 if ((lx = poly[i], !INTEGERP (lx))
24977 || (ly = poly[i+1], !INTEGERP (ly)))
24978 return 0;
24979 x0 = XINT (lx), y0 = XINT (ly);
24980
24981 /* Does this segment cross the X line? */
24982 if (x0 >= x)
24983 {
24984 if (x1 >= x)
24985 continue;
24986 }
24987 else if (x1 < x)
24988 continue;
24989 if (y > y0 && y > y1)
24990 continue;
24991 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24992 inside = !inside;
24993 }
24994 return inside;
24995 }
24996 }
24997 return 0;
24998 }
24999
25000 Lisp_Object
25001 find_hot_spot (Lisp_Object map, int x, int y)
25002 {
25003 while (CONSP (map))
25004 {
25005 if (CONSP (XCAR (map))
25006 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25007 return XCAR (map);
25008 map = XCDR (map);
25009 }
25010
25011 return Qnil;
25012 }
25013
25014 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25015 3, 3, 0,
25016 doc: /* Lookup in image map MAP coordinates X and Y.
25017 An image map is an alist where each element has the format (AREA ID PLIST).
25018 An AREA is specified as either a rectangle, a circle, or a polygon:
25019 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25020 pixel coordinates of the upper left and bottom right corners.
25021 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25022 and the radius of the circle; r may be a float or integer.
25023 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25024 vector describes one corner in the polygon.
25025 Returns the alist element for the first matching AREA in MAP. */)
25026 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25027 {
25028 if (NILP (map))
25029 return Qnil;
25030
25031 CHECK_NUMBER (x);
25032 CHECK_NUMBER (y);
25033
25034 return find_hot_spot (map, XINT (x), XINT (y));
25035 }
25036
25037
25038 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25039 static void
25040 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25041 {
25042 /* Do not change cursor shape while dragging mouse. */
25043 if (!NILP (do_mouse_tracking))
25044 return;
25045
25046 if (!NILP (pointer))
25047 {
25048 if (EQ (pointer, Qarrow))
25049 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25050 else if (EQ (pointer, Qhand))
25051 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25052 else if (EQ (pointer, Qtext))
25053 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25054 else if (EQ (pointer, intern ("hdrag")))
25055 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25056 #ifdef HAVE_X_WINDOWS
25057 else if (EQ (pointer, intern ("vdrag")))
25058 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25059 #endif
25060 else if (EQ (pointer, intern ("hourglass")))
25061 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25062 else if (EQ (pointer, Qmodeline))
25063 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25064 else
25065 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25066 }
25067
25068 if (cursor != No_Cursor)
25069 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25070 }
25071
25072 #endif /* HAVE_WINDOW_SYSTEM */
25073
25074 /* Take proper action when mouse has moved to the mode or header line
25075 or marginal area AREA of window W, x-position X and y-position Y.
25076 X is relative to the start of the text display area of W, so the
25077 width of bitmap areas and scroll bars must be subtracted to get a
25078 position relative to the start of the mode line. */
25079
25080 static void
25081 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25082 enum window_part area)
25083 {
25084 struct window *w = XWINDOW (window);
25085 struct frame *f = XFRAME (w->frame);
25086 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25087 #ifdef HAVE_WINDOW_SYSTEM
25088 Display_Info *dpyinfo;
25089 #endif
25090 Cursor cursor = No_Cursor;
25091 Lisp_Object pointer = Qnil;
25092 int dx, dy, width, height;
25093 EMACS_INT charpos;
25094 Lisp_Object string, object = Qnil;
25095 Lisp_Object pos, help;
25096
25097 Lisp_Object mouse_face;
25098 int original_x_pixel = x;
25099 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25100 struct glyph_row *row;
25101
25102 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25103 {
25104 int x0;
25105 struct glyph *end;
25106
25107 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25108 returns them in row/column units! */
25109 string = mode_line_string (w, area, &x, &y, &charpos,
25110 &object, &dx, &dy, &width, &height);
25111
25112 row = (area == ON_MODE_LINE
25113 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25114 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25115
25116 /* Find the glyph under the mouse pointer. */
25117 if (row->mode_line_p && row->enabled_p)
25118 {
25119 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25120 end = glyph + row->used[TEXT_AREA];
25121
25122 for (x0 = original_x_pixel;
25123 glyph < end && x0 >= glyph->pixel_width;
25124 ++glyph)
25125 x0 -= glyph->pixel_width;
25126
25127 if (glyph >= end)
25128 glyph = NULL;
25129 }
25130 }
25131 else
25132 {
25133 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25134 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25135 returns them in row/column units! */
25136 string = marginal_area_string (w, area, &x, &y, &charpos,
25137 &object, &dx, &dy, &width, &height);
25138 }
25139
25140 help = Qnil;
25141
25142 #ifdef HAVE_WINDOW_SYSTEM
25143 if (IMAGEP (object))
25144 {
25145 Lisp_Object image_map, hotspot;
25146 if ((image_map = Fplist_get (XCDR (object), QCmap),
25147 !NILP (image_map))
25148 && (hotspot = find_hot_spot (image_map, dx, dy),
25149 CONSP (hotspot))
25150 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25151 {
25152 Lisp_Object area_id, plist;
25153
25154 area_id = XCAR (hotspot);
25155 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25156 If so, we could look for mouse-enter, mouse-leave
25157 properties in PLIST (and do something...). */
25158 hotspot = XCDR (hotspot);
25159 if (CONSP (hotspot)
25160 && (plist = XCAR (hotspot), CONSP (plist)))
25161 {
25162 pointer = Fplist_get (plist, Qpointer);
25163 if (NILP (pointer))
25164 pointer = Qhand;
25165 help = Fplist_get (plist, Qhelp_echo);
25166 if (!NILP (help))
25167 {
25168 help_echo_string = help;
25169 /* Is this correct? ++kfs */
25170 XSETWINDOW (help_echo_window, w);
25171 help_echo_object = w->buffer;
25172 help_echo_pos = charpos;
25173 }
25174 }
25175 }
25176 if (NILP (pointer))
25177 pointer = Fplist_get (XCDR (object), QCpointer);
25178 }
25179 #endif /* HAVE_WINDOW_SYSTEM */
25180
25181 if (STRINGP (string))
25182 {
25183 pos = make_number (charpos);
25184 /* If we're on a string with `help-echo' text property, arrange
25185 for the help to be displayed. This is done by setting the
25186 global variable help_echo_string to the help string. */
25187 if (NILP (help))
25188 {
25189 help = Fget_text_property (pos, Qhelp_echo, string);
25190 if (!NILP (help))
25191 {
25192 help_echo_string = help;
25193 XSETWINDOW (help_echo_window, w);
25194 help_echo_object = string;
25195 help_echo_pos = charpos;
25196 }
25197 }
25198
25199 #ifdef HAVE_WINDOW_SYSTEM
25200 if (FRAME_WINDOW_P (f))
25201 {
25202 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25203 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25204 if (NILP (pointer))
25205 pointer = Fget_text_property (pos, Qpointer, string);
25206
25207 /* Change the mouse pointer according to what is under X/Y. */
25208 if (NILP (pointer)
25209 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25210 {
25211 Lisp_Object map;
25212 map = Fget_text_property (pos, Qlocal_map, string);
25213 if (!KEYMAPP (map))
25214 map = Fget_text_property (pos, Qkeymap, string);
25215 if (!KEYMAPP (map))
25216 cursor = dpyinfo->vertical_scroll_bar_cursor;
25217 }
25218 }
25219 #endif
25220
25221 /* Change the mouse face according to what is under X/Y. */
25222 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25223 if (!NILP (mouse_face)
25224 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25225 && glyph)
25226 {
25227 Lisp_Object b, e;
25228
25229 struct glyph * tmp_glyph;
25230
25231 int gpos;
25232 int gseq_length;
25233 int total_pixel_width;
25234 EMACS_INT begpos, endpos, ignore;
25235
25236 int vpos, hpos;
25237
25238 b = Fprevious_single_property_change (make_number (charpos + 1),
25239 Qmouse_face, string, Qnil);
25240 if (NILP (b))
25241 begpos = 0;
25242 else
25243 begpos = XINT (b);
25244
25245 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25246 if (NILP (e))
25247 endpos = SCHARS (string);
25248 else
25249 endpos = XINT (e);
25250
25251 /* Calculate the glyph position GPOS of GLYPH in the
25252 displayed string, relative to the beginning of the
25253 highlighted part of the string.
25254
25255 Note: GPOS is different from CHARPOS. CHARPOS is the
25256 position of GLYPH in the internal string object. A mode
25257 line string format has structures which are converted to
25258 a flattened string by the Emacs Lisp interpreter. The
25259 internal string is an element of those structures. The
25260 displayed string is the flattened string. */
25261 tmp_glyph = row_start_glyph;
25262 while (tmp_glyph < glyph
25263 && (!(EQ (tmp_glyph->object, glyph->object)
25264 && begpos <= tmp_glyph->charpos
25265 && tmp_glyph->charpos < endpos)))
25266 tmp_glyph++;
25267 gpos = glyph - tmp_glyph;
25268
25269 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25270 the highlighted part of the displayed string to which
25271 GLYPH belongs. Note: GSEQ_LENGTH is different from
25272 SCHARS (STRING), because the latter returns the length of
25273 the internal string. */
25274 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25275 tmp_glyph > glyph
25276 && (!(EQ (tmp_glyph->object, glyph->object)
25277 && begpos <= tmp_glyph->charpos
25278 && tmp_glyph->charpos < endpos));
25279 tmp_glyph--)
25280 ;
25281 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25282
25283 /* Calculate the total pixel width of all the glyphs between
25284 the beginning of the highlighted area and GLYPH. */
25285 total_pixel_width = 0;
25286 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25287 total_pixel_width += tmp_glyph->pixel_width;
25288
25289 /* Pre calculation of re-rendering position. Note: X is in
25290 column units here, after the call to mode_line_string or
25291 marginal_area_string. */
25292 hpos = x - gpos;
25293 vpos = (area == ON_MODE_LINE
25294 ? (w->current_matrix)->nrows - 1
25295 : 0);
25296
25297 /* If GLYPH's position is included in the region that is
25298 already drawn in mouse face, we have nothing to do. */
25299 if ( EQ (window, hlinfo->mouse_face_window)
25300 && (!row->reversed_p
25301 ? (hlinfo->mouse_face_beg_col <= hpos
25302 && hpos < hlinfo->mouse_face_end_col)
25303 /* In R2L rows we swap BEG and END, see below. */
25304 : (hlinfo->mouse_face_end_col <= hpos
25305 && hpos < hlinfo->mouse_face_beg_col))
25306 && hlinfo->mouse_face_beg_row == vpos )
25307 return;
25308
25309 if (clear_mouse_face (hlinfo))
25310 cursor = No_Cursor;
25311
25312 if (!row->reversed_p)
25313 {
25314 hlinfo->mouse_face_beg_col = hpos;
25315 hlinfo->mouse_face_beg_x = original_x_pixel
25316 - (total_pixel_width + dx);
25317 hlinfo->mouse_face_end_col = hpos + gseq_length;
25318 hlinfo->mouse_face_end_x = 0;
25319 }
25320 else
25321 {
25322 /* In R2L rows, show_mouse_face expects BEG and END
25323 coordinates to be swapped. */
25324 hlinfo->mouse_face_end_col = hpos;
25325 hlinfo->mouse_face_end_x = original_x_pixel
25326 - (total_pixel_width + dx);
25327 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25328 hlinfo->mouse_face_beg_x = 0;
25329 }
25330
25331 hlinfo->mouse_face_beg_row = vpos;
25332 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25333 hlinfo->mouse_face_beg_y = 0;
25334 hlinfo->mouse_face_end_y = 0;
25335 hlinfo->mouse_face_past_end = 0;
25336 hlinfo->mouse_face_window = window;
25337
25338 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25339 charpos,
25340 0, 0, 0,
25341 &ignore,
25342 glyph->face_id,
25343 1);
25344 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25345
25346 if (NILP (pointer))
25347 pointer = Qhand;
25348 }
25349 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25350 clear_mouse_face (hlinfo);
25351 }
25352 #ifdef HAVE_WINDOW_SYSTEM
25353 if (FRAME_WINDOW_P (f))
25354 define_frame_cursor1 (f, cursor, pointer);
25355 #endif
25356 }
25357
25358
25359 /* EXPORT:
25360 Take proper action when the mouse has moved to position X, Y on
25361 frame F as regards highlighting characters that have mouse-face
25362 properties. Also de-highlighting chars where the mouse was before.
25363 X and Y can be negative or out of range. */
25364
25365 void
25366 note_mouse_highlight (struct frame *f, int x, int y)
25367 {
25368 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25369 enum window_part part;
25370 Lisp_Object window;
25371 struct window *w;
25372 Cursor cursor = No_Cursor;
25373 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25374 struct buffer *b;
25375
25376 /* When a menu is active, don't highlight because this looks odd. */
25377 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25378 if (popup_activated ())
25379 return;
25380 #endif
25381
25382 if (NILP (Vmouse_highlight)
25383 || !f->glyphs_initialized_p
25384 || f->pointer_invisible)
25385 return;
25386
25387 hlinfo->mouse_face_mouse_x = x;
25388 hlinfo->mouse_face_mouse_y = y;
25389 hlinfo->mouse_face_mouse_frame = f;
25390
25391 if (hlinfo->mouse_face_defer)
25392 return;
25393
25394 if (gc_in_progress)
25395 {
25396 hlinfo->mouse_face_deferred_gc = 1;
25397 return;
25398 }
25399
25400 /* Which window is that in? */
25401 window = window_from_coordinates (f, x, y, &part, 1);
25402
25403 /* If we were displaying active text in another window, clear that.
25404 Also clear if we move out of text area in same window. */
25405 if (! EQ (window, hlinfo->mouse_face_window)
25406 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25407 && !NILP (hlinfo->mouse_face_window)))
25408 clear_mouse_face (hlinfo);
25409
25410 /* Not on a window -> return. */
25411 if (!WINDOWP (window))
25412 return;
25413
25414 /* Reset help_echo_string. It will get recomputed below. */
25415 help_echo_string = Qnil;
25416
25417 /* Convert to window-relative pixel coordinates. */
25418 w = XWINDOW (window);
25419 frame_to_window_pixel_xy (w, &x, &y);
25420
25421 #ifdef HAVE_WINDOW_SYSTEM
25422 /* Handle tool-bar window differently since it doesn't display a
25423 buffer. */
25424 if (EQ (window, f->tool_bar_window))
25425 {
25426 note_tool_bar_highlight (f, x, y);
25427 return;
25428 }
25429 #endif
25430
25431 /* Mouse is on the mode, header line or margin? */
25432 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25433 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25434 {
25435 note_mode_line_or_margin_highlight (window, x, y, part);
25436 return;
25437 }
25438
25439 #ifdef HAVE_WINDOW_SYSTEM
25440 if (part == ON_VERTICAL_BORDER)
25441 {
25442 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25443 help_echo_string = build_string ("drag-mouse-1: resize");
25444 }
25445 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25446 || part == ON_SCROLL_BAR)
25447 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25448 else
25449 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25450 #endif
25451
25452 /* Are we in a window whose display is up to date?
25453 And verify the buffer's text has not changed. */
25454 b = XBUFFER (w->buffer);
25455 if (part == ON_TEXT
25456 && EQ (w->window_end_valid, w->buffer)
25457 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25458 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25459 {
25460 int hpos, vpos, i, dx, dy, area;
25461 EMACS_INT pos;
25462 struct glyph *glyph;
25463 Lisp_Object object;
25464 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
25465 Lisp_Object *overlay_vec = NULL;
25466 int noverlays;
25467 struct buffer *obuf;
25468 EMACS_INT obegv, ozv;
25469 int same_region;
25470
25471 /* Find the glyph under X/Y. */
25472 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25473
25474 #ifdef HAVE_WINDOW_SYSTEM
25475 /* Look for :pointer property on image. */
25476 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25477 {
25478 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25479 if (img != NULL && IMAGEP (img->spec))
25480 {
25481 Lisp_Object image_map, hotspot;
25482 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25483 !NILP (image_map))
25484 && (hotspot = find_hot_spot (image_map,
25485 glyph->slice.img.x + dx,
25486 glyph->slice.img.y + dy),
25487 CONSP (hotspot))
25488 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25489 {
25490 Lisp_Object area_id, plist;
25491
25492 area_id = XCAR (hotspot);
25493 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25494 If so, we could look for mouse-enter, mouse-leave
25495 properties in PLIST (and do something...). */
25496 hotspot = XCDR (hotspot);
25497 if (CONSP (hotspot)
25498 && (plist = XCAR (hotspot), CONSP (plist)))
25499 {
25500 pointer = Fplist_get (plist, Qpointer);
25501 if (NILP (pointer))
25502 pointer = Qhand;
25503 help_echo_string = Fplist_get (plist, Qhelp_echo);
25504 if (!NILP (help_echo_string))
25505 {
25506 help_echo_window = window;
25507 help_echo_object = glyph->object;
25508 help_echo_pos = glyph->charpos;
25509 }
25510 }
25511 }
25512 if (NILP (pointer))
25513 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25514 }
25515 }
25516 #endif /* HAVE_WINDOW_SYSTEM */
25517
25518 /* Clear mouse face if X/Y not over text. */
25519 if (glyph == NULL
25520 || area != TEXT_AREA
25521 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25522 /* Glyph's OBJECT is an integer for glyphs inserted by the
25523 display engine for its internal purposes, like truncation
25524 and continuation glyphs and blanks beyond the end of
25525 line's text on text terminals. If we are over such a
25526 glyph, we are not over any text. */
25527 || INTEGERP (glyph->object)
25528 /* R2L rows have a stretch glyph at their front, which
25529 stands for no text, whereas L2R rows have no glyphs at
25530 all beyond the end of text. Treat such stretch glyphs
25531 like we do with NULL glyphs in L2R rows. */
25532 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25533 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25534 && glyph->type == STRETCH_GLYPH
25535 && glyph->avoid_cursor_p))
25536 {
25537 if (clear_mouse_face (hlinfo))
25538 cursor = No_Cursor;
25539 #ifdef HAVE_WINDOW_SYSTEM
25540 if (FRAME_WINDOW_P (f) && NILP (pointer))
25541 {
25542 if (area != TEXT_AREA)
25543 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25544 else
25545 pointer = Vvoid_text_area_pointer;
25546 }
25547 #endif
25548 goto set_cursor;
25549 }
25550
25551 pos = glyph->charpos;
25552 object = glyph->object;
25553 if (!STRINGP (object) && !BUFFERP (object))
25554 goto set_cursor;
25555
25556 /* If we get an out-of-range value, return now; avoid an error. */
25557 if (BUFFERP (object) && pos > BUF_Z (b))
25558 goto set_cursor;
25559
25560 /* Make the window's buffer temporarily current for
25561 overlays_at and compute_char_face. */
25562 obuf = current_buffer;
25563 current_buffer = b;
25564 obegv = BEGV;
25565 ozv = ZV;
25566 BEGV = BEG;
25567 ZV = Z;
25568
25569 /* Is this char mouse-active or does it have help-echo? */
25570 position = make_number (pos);
25571
25572 if (BUFFERP (object))
25573 {
25574 /* Put all the overlays we want in a vector in overlay_vec. */
25575 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25576 /* Sort overlays into increasing priority order. */
25577 noverlays = sort_overlays (overlay_vec, noverlays, w);
25578 }
25579 else
25580 noverlays = 0;
25581
25582 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25583
25584 if (same_region)
25585 cursor = No_Cursor;
25586
25587 /* Check mouse-face highlighting. */
25588 if (! same_region
25589 /* If there exists an overlay with mouse-face overlapping
25590 the one we are currently highlighting, we have to
25591 check if we enter the overlapping overlay, and then
25592 highlight only that. */
25593 || (OVERLAYP (hlinfo->mouse_face_overlay)
25594 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25595 {
25596 /* Find the highest priority overlay with a mouse-face. */
25597 overlay = Qnil;
25598 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25599 {
25600 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25601 if (!NILP (mouse_face))
25602 overlay = overlay_vec[i];
25603 }
25604
25605 /* If we're highlighting the same overlay as before, there's
25606 no need to do that again. */
25607 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25608 goto check_help_echo;
25609 hlinfo->mouse_face_overlay = overlay;
25610
25611 /* Clear the display of the old active region, if any. */
25612 if (clear_mouse_face (hlinfo))
25613 cursor = No_Cursor;
25614
25615 /* If no overlay applies, get a text property. */
25616 if (NILP (overlay))
25617 mouse_face = Fget_text_property (position, Qmouse_face, object);
25618
25619 /* Next, compute the bounds of the mouse highlighting and
25620 display it. */
25621 if (!NILP (mouse_face) && STRINGP (object))
25622 {
25623 /* The mouse-highlighting comes from a display string
25624 with a mouse-face. */
25625 Lisp_Object b, e;
25626 EMACS_INT ignore;
25627
25628 b = Fprevious_single_property_change
25629 (make_number (pos + 1), Qmouse_face, object, Qnil);
25630 e = Fnext_single_property_change
25631 (position, Qmouse_face, object, Qnil);
25632 if (NILP (b))
25633 b = make_number (0);
25634 if (NILP (e))
25635 e = make_number (SCHARS (object) - 1);
25636 mouse_face_from_string_pos (w, hlinfo, object,
25637 XINT (b), XINT (e));
25638 hlinfo->mouse_face_past_end = 0;
25639 hlinfo->mouse_face_window = window;
25640 hlinfo->mouse_face_face_id
25641 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25642 glyph->face_id, 1);
25643 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25644 cursor = No_Cursor;
25645 }
25646 else
25647 {
25648 /* The mouse-highlighting, if any, comes from an overlay
25649 or text property in the buffer. */
25650 Lisp_Object buffer, display_string;
25651
25652 if (STRINGP (object))
25653 {
25654 /* If we are on a display string with no mouse-face,
25655 check if the text under it has one. */
25656 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25657 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25658 pos = string_buffer_position (w, object, start);
25659 if (pos > 0)
25660 {
25661 mouse_face = get_char_property_and_overlay
25662 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25663 buffer = w->buffer;
25664 display_string = object;
25665 }
25666 }
25667 else
25668 {
25669 buffer = object;
25670 display_string = Qnil;
25671 }
25672
25673 if (!NILP (mouse_face))
25674 {
25675 Lisp_Object before, after;
25676 Lisp_Object before_string, after_string;
25677 /* To correctly find the limits of mouse highlight
25678 in a bidi-reordered buffer, we must not use the
25679 optimization of limiting the search in
25680 previous-single-property-change and
25681 next-single-property-change, because
25682 rows_from_pos_range needs the real start and end
25683 positions to DTRT in this case. That's because
25684 the first row visible in a window does not
25685 necessarily display the character whose position
25686 is the smallest. */
25687 Lisp_Object lim1 =
25688 NILP (XBUFFER (buffer)->bidi_display_reordering)
25689 ? Fmarker_position (w->start)
25690 : Qnil;
25691 Lisp_Object lim2 =
25692 NILP (XBUFFER (buffer)->bidi_display_reordering)
25693 ? make_number (BUF_Z (XBUFFER (buffer))
25694 - XFASTINT (w->window_end_pos))
25695 : Qnil;
25696
25697 if (NILP (overlay))
25698 {
25699 /* Handle the text property case. */
25700 before = Fprevious_single_property_change
25701 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25702 after = Fnext_single_property_change
25703 (make_number (pos), Qmouse_face, buffer, lim2);
25704 before_string = after_string = Qnil;
25705 }
25706 else
25707 {
25708 /* Handle the overlay case. */
25709 before = Foverlay_start (overlay);
25710 after = Foverlay_end (overlay);
25711 before_string = Foverlay_get (overlay, Qbefore_string);
25712 after_string = Foverlay_get (overlay, Qafter_string);
25713
25714 if (!STRINGP (before_string)) before_string = Qnil;
25715 if (!STRINGP (after_string)) after_string = Qnil;
25716 }
25717
25718 mouse_face_from_buffer_pos (window, hlinfo, pos,
25719 XFASTINT (before),
25720 XFASTINT (after),
25721 before_string, after_string,
25722 display_string);
25723 cursor = No_Cursor;
25724 }
25725 }
25726 }
25727
25728 check_help_echo:
25729
25730 /* Look for a `help-echo' property. */
25731 if (NILP (help_echo_string)) {
25732 Lisp_Object help, overlay;
25733
25734 /* Check overlays first. */
25735 help = overlay = Qnil;
25736 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25737 {
25738 overlay = overlay_vec[i];
25739 help = Foverlay_get (overlay, Qhelp_echo);
25740 }
25741
25742 if (!NILP (help))
25743 {
25744 help_echo_string = help;
25745 help_echo_window = window;
25746 help_echo_object = overlay;
25747 help_echo_pos = pos;
25748 }
25749 else
25750 {
25751 Lisp_Object object = glyph->object;
25752 EMACS_INT charpos = glyph->charpos;
25753
25754 /* Try text properties. */
25755 if (STRINGP (object)
25756 && charpos >= 0
25757 && charpos < SCHARS (object))
25758 {
25759 help = Fget_text_property (make_number (charpos),
25760 Qhelp_echo, object);
25761 if (NILP (help))
25762 {
25763 /* If the string itself doesn't specify a help-echo,
25764 see if the buffer text ``under'' it does. */
25765 struct glyph_row *r
25766 = MATRIX_ROW (w->current_matrix, vpos);
25767 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25768 EMACS_INT pos = string_buffer_position (w, object, start);
25769 if (pos > 0)
25770 {
25771 help = Fget_char_property (make_number (pos),
25772 Qhelp_echo, w->buffer);
25773 if (!NILP (help))
25774 {
25775 charpos = pos;
25776 object = w->buffer;
25777 }
25778 }
25779 }
25780 }
25781 else if (BUFFERP (object)
25782 && charpos >= BEGV
25783 && charpos < ZV)
25784 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25785 object);
25786
25787 if (!NILP (help))
25788 {
25789 help_echo_string = help;
25790 help_echo_window = window;
25791 help_echo_object = object;
25792 help_echo_pos = charpos;
25793 }
25794 }
25795 }
25796
25797 #ifdef HAVE_WINDOW_SYSTEM
25798 /* Look for a `pointer' property. */
25799 if (FRAME_WINDOW_P (f) && NILP (pointer))
25800 {
25801 /* Check overlays first. */
25802 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25803 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25804
25805 if (NILP (pointer))
25806 {
25807 Lisp_Object object = glyph->object;
25808 EMACS_INT charpos = glyph->charpos;
25809
25810 /* Try text properties. */
25811 if (STRINGP (object)
25812 && charpos >= 0
25813 && charpos < SCHARS (object))
25814 {
25815 pointer = Fget_text_property (make_number (charpos),
25816 Qpointer, object);
25817 if (NILP (pointer))
25818 {
25819 /* If the string itself doesn't specify a pointer,
25820 see if the buffer text ``under'' it does. */
25821 struct glyph_row *r
25822 = MATRIX_ROW (w->current_matrix, vpos);
25823 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25824 EMACS_INT pos = string_buffer_position (w, object,
25825 start);
25826 if (pos > 0)
25827 pointer = Fget_char_property (make_number (pos),
25828 Qpointer, w->buffer);
25829 }
25830 }
25831 else if (BUFFERP (object)
25832 && charpos >= BEGV
25833 && charpos < ZV)
25834 pointer = Fget_text_property (make_number (charpos),
25835 Qpointer, object);
25836 }
25837 }
25838 #endif /* HAVE_WINDOW_SYSTEM */
25839
25840 BEGV = obegv;
25841 ZV = ozv;
25842 current_buffer = obuf;
25843 }
25844
25845 set_cursor:
25846
25847 #ifdef HAVE_WINDOW_SYSTEM
25848 if (FRAME_WINDOW_P (f))
25849 define_frame_cursor1 (f, cursor, pointer);
25850 #else
25851 /* This is here to prevent a compiler error, about "label at end of
25852 compound statement". */
25853 return;
25854 #endif
25855 }
25856
25857
25858 /* EXPORT for RIF:
25859 Clear any mouse-face on window W. This function is part of the
25860 redisplay interface, and is called from try_window_id and similar
25861 functions to ensure the mouse-highlight is off. */
25862
25863 void
25864 x_clear_window_mouse_face (struct window *w)
25865 {
25866 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25867 Lisp_Object window;
25868
25869 BLOCK_INPUT;
25870 XSETWINDOW (window, w);
25871 if (EQ (window, hlinfo->mouse_face_window))
25872 clear_mouse_face (hlinfo);
25873 UNBLOCK_INPUT;
25874 }
25875
25876
25877 /* EXPORT:
25878 Just discard the mouse face information for frame F, if any.
25879 This is used when the size of F is changed. */
25880
25881 void
25882 cancel_mouse_face (struct frame *f)
25883 {
25884 Lisp_Object window;
25885 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25886
25887 window = hlinfo->mouse_face_window;
25888 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25889 {
25890 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25891 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25892 hlinfo->mouse_face_window = Qnil;
25893 }
25894 }
25895
25896
25897 \f
25898 /***********************************************************************
25899 Exposure Events
25900 ***********************************************************************/
25901
25902 #ifdef HAVE_WINDOW_SYSTEM
25903
25904 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25905 which intersects rectangle R. R is in window-relative coordinates. */
25906
25907 static void
25908 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25909 enum glyph_row_area area)
25910 {
25911 struct glyph *first = row->glyphs[area];
25912 struct glyph *end = row->glyphs[area] + row->used[area];
25913 struct glyph *last;
25914 int first_x, start_x, x;
25915
25916 if (area == TEXT_AREA && row->fill_line_p)
25917 /* If row extends face to end of line write the whole line. */
25918 draw_glyphs (w, 0, row, area,
25919 0, row->used[area],
25920 DRAW_NORMAL_TEXT, 0);
25921 else
25922 {
25923 /* Set START_X to the window-relative start position for drawing glyphs of
25924 AREA. The first glyph of the text area can be partially visible.
25925 The first glyphs of other areas cannot. */
25926 start_x = window_box_left_offset (w, area);
25927 x = start_x;
25928 if (area == TEXT_AREA)
25929 x += row->x;
25930
25931 /* Find the first glyph that must be redrawn. */
25932 while (first < end
25933 && x + first->pixel_width < r->x)
25934 {
25935 x += first->pixel_width;
25936 ++first;
25937 }
25938
25939 /* Find the last one. */
25940 last = first;
25941 first_x = x;
25942 while (last < end
25943 && x < r->x + r->width)
25944 {
25945 x += last->pixel_width;
25946 ++last;
25947 }
25948
25949 /* Repaint. */
25950 if (last > first)
25951 draw_glyphs (w, first_x - start_x, row, area,
25952 first - row->glyphs[area], last - row->glyphs[area],
25953 DRAW_NORMAL_TEXT, 0);
25954 }
25955 }
25956
25957
25958 /* Redraw the parts of the glyph row ROW on window W intersecting
25959 rectangle R. R is in window-relative coordinates. Value is
25960 non-zero if mouse-face was overwritten. */
25961
25962 static int
25963 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25964 {
25965 xassert (row->enabled_p);
25966
25967 if (row->mode_line_p || w->pseudo_window_p)
25968 draw_glyphs (w, 0, row, TEXT_AREA,
25969 0, row->used[TEXT_AREA],
25970 DRAW_NORMAL_TEXT, 0);
25971 else
25972 {
25973 if (row->used[LEFT_MARGIN_AREA])
25974 expose_area (w, row, r, LEFT_MARGIN_AREA);
25975 if (row->used[TEXT_AREA])
25976 expose_area (w, row, r, TEXT_AREA);
25977 if (row->used[RIGHT_MARGIN_AREA])
25978 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25979 draw_row_fringe_bitmaps (w, row);
25980 }
25981
25982 return row->mouse_face_p;
25983 }
25984
25985
25986 /* Redraw those parts of glyphs rows during expose event handling that
25987 overlap other rows. Redrawing of an exposed line writes over parts
25988 of lines overlapping that exposed line; this function fixes that.
25989
25990 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25991 row in W's current matrix that is exposed and overlaps other rows.
25992 LAST_OVERLAPPING_ROW is the last such row. */
25993
25994 static void
25995 expose_overlaps (struct window *w,
25996 struct glyph_row *first_overlapping_row,
25997 struct glyph_row *last_overlapping_row,
25998 XRectangle *r)
25999 {
26000 struct glyph_row *row;
26001
26002 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26003 if (row->overlapping_p)
26004 {
26005 xassert (row->enabled_p && !row->mode_line_p);
26006
26007 row->clip = r;
26008 if (row->used[LEFT_MARGIN_AREA])
26009 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26010
26011 if (row->used[TEXT_AREA])
26012 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26013
26014 if (row->used[RIGHT_MARGIN_AREA])
26015 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26016 row->clip = NULL;
26017 }
26018 }
26019
26020
26021 /* Return non-zero if W's cursor intersects rectangle R. */
26022
26023 static int
26024 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26025 {
26026 XRectangle cr, result;
26027 struct glyph *cursor_glyph;
26028 struct glyph_row *row;
26029
26030 if (w->phys_cursor.vpos >= 0
26031 && w->phys_cursor.vpos < w->current_matrix->nrows
26032 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26033 row->enabled_p)
26034 && row->cursor_in_fringe_p)
26035 {
26036 /* Cursor is in the fringe. */
26037 cr.x = window_box_right_offset (w,
26038 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26039 ? RIGHT_MARGIN_AREA
26040 : TEXT_AREA));
26041 cr.y = row->y;
26042 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26043 cr.height = row->height;
26044 return x_intersect_rectangles (&cr, r, &result);
26045 }
26046
26047 cursor_glyph = get_phys_cursor_glyph (w);
26048 if (cursor_glyph)
26049 {
26050 /* r is relative to W's box, but w->phys_cursor.x is relative
26051 to left edge of W's TEXT area. Adjust it. */
26052 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26053 cr.y = w->phys_cursor.y;
26054 cr.width = cursor_glyph->pixel_width;
26055 cr.height = w->phys_cursor_height;
26056 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26057 I assume the effect is the same -- and this is portable. */
26058 return x_intersect_rectangles (&cr, r, &result);
26059 }
26060 /* If we don't understand the format, pretend we're not in the hot-spot. */
26061 return 0;
26062 }
26063
26064
26065 /* EXPORT:
26066 Draw a vertical window border to the right of window W if W doesn't
26067 have vertical scroll bars. */
26068
26069 void
26070 x_draw_vertical_border (struct window *w)
26071 {
26072 struct frame *f = XFRAME (WINDOW_FRAME (w));
26073
26074 /* We could do better, if we knew what type of scroll-bar the adjacent
26075 windows (on either side) have... But we don't :-(
26076 However, I think this works ok. ++KFS 2003-04-25 */
26077
26078 /* Redraw borders between horizontally adjacent windows. Don't
26079 do it for frames with vertical scroll bars because either the
26080 right scroll bar of a window, or the left scroll bar of its
26081 neighbor will suffice as a border. */
26082 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26083 return;
26084
26085 if (!WINDOW_RIGHTMOST_P (w)
26086 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26087 {
26088 int x0, x1, y0, y1;
26089
26090 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26091 y1 -= 1;
26092
26093 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26094 x1 -= 1;
26095
26096 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26097 }
26098 else if (!WINDOW_LEFTMOST_P (w)
26099 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26100 {
26101 int x0, x1, y0, y1;
26102
26103 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26104 y1 -= 1;
26105
26106 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26107 x0 -= 1;
26108
26109 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26110 }
26111 }
26112
26113
26114 /* Redraw the part of window W intersection rectangle FR. Pixel
26115 coordinates in FR are frame-relative. Call this function with
26116 input blocked. Value is non-zero if the exposure overwrites
26117 mouse-face. */
26118
26119 static int
26120 expose_window (struct window *w, XRectangle *fr)
26121 {
26122 struct frame *f = XFRAME (w->frame);
26123 XRectangle wr, r;
26124 int mouse_face_overwritten_p = 0;
26125
26126 /* If window is not yet fully initialized, do nothing. This can
26127 happen when toolkit scroll bars are used and a window is split.
26128 Reconfiguring the scroll bar will generate an expose for a newly
26129 created window. */
26130 if (w->current_matrix == NULL)
26131 return 0;
26132
26133 /* When we're currently updating the window, display and current
26134 matrix usually don't agree. Arrange for a thorough display
26135 later. */
26136 if (w == updated_window)
26137 {
26138 SET_FRAME_GARBAGED (f);
26139 return 0;
26140 }
26141
26142 /* Frame-relative pixel rectangle of W. */
26143 wr.x = WINDOW_LEFT_EDGE_X (w);
26144 wr.y = WINDOW_TOP_EDGE_Y (w);
26145 wr.width = WINDOW_TOTAL_WIDTH (w);
26146 wr.height = WINDOW_TOTAL_HEIGHT (w);
26147
26148 if (x_intersect_rectangles (fr, &wr, &r))
26149 {
26150 int yb = window_text_bottom_y (w);
26151 struct glyph_row *row;
26152 int cursor_cleared_p;
26153 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26154
26155 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26156 r.x, r.y, r.width, r.height));
26157
26158 /* Convert to window coordinates. */
26159 r.x -= WINDOW_LEFT_EDGE_X (w);
26160 r.y -= WINDOW_TOP_EDGE_Y (w);
26161
26162 /* Turn off the cursor. */
26163 if (!w->pseudo_window_p
26164 && phys_cursor_in_rect_p (w, &r))
26165 {
26166 x_clear_cursor (w);
26167 cursor_cleared_p = 1;
26168 }
26169 else
26170 cursor_cleared_p = 0;
26171
26172 /* Update lines intersecting rectangle R. */
26173 first_overlapping_row = last_overlapping_row = NULL;
26174 for (row = w->current_matrix->rows;
26175 row->enabled_p;
26176 ++row)
26177 {
26178 int y0 = row->y;
26179 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26180
26181 if ((y0 >= r.y && y0 < r.y + r.height)
26182 || (y1 > r.y && y1 < r.y + r.height)
26183 || (r.y >= y0 && r.y < y1)
26184 || (r.y + r.height > y0 && r.y + r.height < y1))
26185 {
26186 /* A header line may be overlapping, but there is no need
26187 to fix overlapping areas for them. KFS 2005-02-12 */
26188 if (row->overlapping_p && !row->mode_line_p)
26189 {
26190 if (first_overlapping_row == NULL)
26191 first_overlapping_row = row;
26192 last_overlapping_row = row;
26193 }
26194
26195 row->clip = fr;
26196 if (expose_line (w, row, &r))
26197 mouse_face_overwritten_p = 1;
26198 row->clip = NULL;
26199 }
26200 else if (row->overlapping_p)
26201 {
26202 /* We must redraw a row overlapping the exposed area. */
26203 if (y0 < r.y
26204 ? y0 + row->phys_height > r.y
26205 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26206 {
26207 if (first_overlapping_row == NULL)
26208 first_overlapping_row = row;
26209 last_overlapping_row = row;
26210 }
26211 }
26212
26213 if (y1 >= yb)
26214 break;
26215 }
26216
26217 /* Display the mode line if there is one. */
26218 if (WINDOW_WANTS_MODELINE_P (w)
26219 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26220 row->enabled_p)
26221 && row->y < r.y + r.height)
26222 {
26223 if (expose_line (w, row, &r))
26224 mouse_face_overwritten_p = 1;
26225 }
26226
26227 if (!w->pseudo_window_p)
26228 {
26229 /* Fix the display of overlapping rows. */
26230 if (first_overlapping_row)
26231 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26232 fr);
26233
26234 /* Draw border between windows. */
26235 x_draw_vertical_border (w);
26236
26237 /* Turn the cursor on again. */
26238 if (cursor_cleared_p)
26239 update_window_cursor (w, 1);
26240 }
26241 }
26242
26243 return mouse_face_overwritten_p;
26244 }
26245
26246
26247
26248 /* Redraw (parts) of all windows in the window tree rooted at W that
26249 intersect R. R contains frame pixel coordinates. Value is
26250 non-zero if the exposure overwrites mouse-face. */
26251
26252 static int
26253 expose_window_tree (struct window *w, XRectangle *r)
26254 {
26255 struct frame *f = XFRAME (w->frame);
26256 int mouse_face_overwritten_p = 0;
26257
26258 while (w && !FRAME_GARBAGED_P (f))
26259 {
26260 if (!NILP (w->hchild))
26261 mouse_face_overwritten_p
26262 |= expose_window_tree (XWINDOW (w->hchild), r);
26263 else if (!NILP (w->vchild))
26264 mouse_face_overwritten_p
26265 |= expose_window_tree (XWINDOW (w->vchild), r);
26266 else
26267 mouse_face_overwritten_p |= expose_window (w, r);
26268
26269 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26270 }
26271
26272 return mouse_face_overwritten_p;
26273 }
26274
26275
26276 /* EXPORT:
26277 Redisplay an exposed area of frame F. X and Y are the upper-left
26278 corner of the exposed rectangle. W and H are width and height of
26279 the exposed area. All are pixel values. W or H zero means redraw
26280 the entire frame. */
26281
26282 void
26283 expose_frame (struct frame *f, int x, int y, int w, int h)
26284 {
26285 XRectangle r;
26286 int mouse_face_overwritten_p = 0;
26287
26288 TRACE ((stderr, "expose_frame "));
26289
26290 /* No need to redraw if frame will be redrawn soon. */
26291 if (FRAME_GARBAGED_P (f))
26292 {
26293 TRACE ((stderr, " garbaged\n"));
26294 return;
26295 }
26296
26297 /* If basic faces haven't been realized yet, there is no point in
26298 trying to redraw anything. This can happen when we get an expose
26299 event while Emacs is starting, e.g. by moving another window. */
26300 if (FRAME_FACE_CACHE (f) == NULL
26301 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26302 {
26303 TRACE ((stderr, " no faces\n"));
26304 return;
26305 }
26306
26307 if (w == 0 || h == 0)
26308 {
26309 r.x = r.y = 0;
26310 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26311 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26312 }
26313 else
26314 {
26315 r.x = x;
26316 r.y = y;
26317 r.width = w;
26318 r.height = h;
26319 }
26320
26321 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26322 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26323
26324 if (WINDOWP (f->tool_bar_window))
26325 mouse_face_overwritten_p
26326 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26327
26328 #ifdef HAVE_X_WINDOWS
26329 #ifndef MSDOS
26330 #ifndef USE_X_TOOLKIT
26331 if (WINDOWP (f->menu_bar_window))
26332 mouse_face_overwritten_p
26333 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26334 #endif /* not USE_X_TOOLKIT */
26335 #endif
26336 #endif
26337
26338 /* Some window managers support a focus-follows-mouse style with
26339 delayed raising of frames. Imagine a partially obscured frame,
26340 and moving the mouse into partially obscured mouse-face on that
26341 frame. The visible part of the mouse-face will be highlighted,
26342 then the WM raises the obscured frame. With at least one WM, KDE
26343 2.1, Emacs is not getting any event for the raising of the frame
26344 (even tried with SubstructureRedirectMask), only Expose events.
26345 These expose events will draw text normally, i.e. not
26346 highlighted. Which means we must redo the highlight here.
26347 Subsume it under ``we love X''. --gerd 2001-08-15 */
26348 /* Included in Windows version because Windows most likely does not
26349 do the right thing if any third party tool offers
26350 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26351 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26352 {
26353 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26354 if (f == hlinfo->mouse_face_mouse_frame)
26355 {
26356 int x = hlinfo->mouse_face_mouse_x;
26357 int y = hlinfo->mouse_face_mouse_y;
26358 clear_mouse_face (hlinfo);
26359 note_mouse_highlight (f, x, y);
26360 }
26361 }
26362 }
26363
26364
26365 /* EXPORT:
26366 Determine the intersection of two rectangles R1 and R2. Return
26367 the intersection in *RESULT. Value is non-zero if RESULT is not
26368 empty. */
26369
26370 int
26371 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26372 {
26373 XRectangle *left, *right;
26374 XRectangle *upper, *lower;
26375 int intersection_p = 0;
26376
26377 /* Rearrange so that R1 is the left-most rectangle. */
26378 if (r1->x < r2->x)
26379 left = r1, right = r2;
26380 else
26381 left = r2, right = r1;
26382
26383 /* X0 of the intersection is right.x0, if this is inside R1,
26384 otherwise there is no intersection. */
26385 if (right->x <= left->x + left->width)
26386 {
26387 result->x = right->x;
26388
26389 /* The right end of the intersection is the minimum of the
26390 the right ends of left and right. */
26391 result->width = (min (left->x + left->width, right->x + right->width)
26392 - result->x);
26393
26394 /* Same game for Y. */
26395 if (r1->y < r2->y)
26396 upper = r1, lower = r2;
26397 else
26398 upper = r2, lower = r1;
26399
26400 /* The upper end of the intersection is lower.y0, if this is inside
26401 of upper. Otherwise, there is no intersection. */
26402 if (lower->y <= upper->y + upper->height)
26403 {
26404 result->y = lower->y;
26405
26406 /* The lower end of the intersection is the minimum of the lower
26407 ends of upper and lower. */
26408 result->height = (min (lower->y + lower->height,
26409 upper->y + upper->height)
26410 - result->y);
26411 intersection_p = 1;
26412 }
26413 }
26414
26415 return intersection_p;
26416 }
26417
26418 #endif /* HAVE_WINDOW_SYSTEM */
26419
26420 \f
26421 /***********************************************************************
26422 Initialization
26423 ***********************************************************************/
26424
26425 void
26426 syms_of_xdisp (void)
26427 {
26428 Vwith_echo_area_save_vector = Qnil;
26429 staticpro (&Vwith_echo_area_save_vector);
26430
26431 Vmessage_stack = Qnil;
26432 staticpro (&Vmessage_stack);
26433
26434 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26435 staticpro (&Qinhibit_redisplay);
26436
26437 message_dolog_marker1 = Fmake_marker ();
26438 staticpro (&message_dolog_marker1);
26439 message_dolog_marker2 = Fmake_marker ();
26440 staticpro (&message_dolog_marker2);
26441 message_dolog_marker3 = Fmake_marker ();
26442 staticpro (&message_dolog_marker3);
26443
26444 #if GLYPH_DEBUG
26445 defsubr (&Sdump_frame_glyph_matrix);
26446 defsubr (&Sdump_glyph_matrix);
26447 defsubr (&Sdump_glyph_row);
26448 defsubr (&Sdump_tool_bar_row);
26449 defsubr (&Strace_redisplay);
26450 defsubr (&Strace_to_stderr);
26451 #endif
26452 #ifdef HAVE_WINDOW_SYSTEM
26453 defsubr (&Stool_bar_lines_needed);
26454 defsubr (&Slookup_image_map);
26455 #endif
26456 defsubr (&Sformat_mode_line);
26457 defsubr (&Sinvisible_p);
26458 defsubr (&Scurrent_bidi_paragraph_direction);
26459
26460 staticpro (&Qmenu_bar_update_hook);
26461 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26462
26463 staticpro (&Qoverriding_terminal_local_map);
26464 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26465
26466 staticpro (&Qoverriding_local_map);
26467 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26468
26469 staticpro (&Qwindow_scroll_functions);
26470 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26471
26472 staticpro (&Qwindow_text_change_functions);
26473 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26474
26475 staticpro (&Qredisplay_end_trigger_functions);
26476 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26477
26478 staticpro (&Qinhibit_point_motion_hooks);
26479 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26480
26481 Qeval = intern_c_string ("eval");
26482 staticpro (&Qeval);
26483
26484 QCdata = intern_c_string (":data");
26485 staticpro (&QCdata);
26486 Qdisplay = intern_c_string ("display");
26487 staticpro (&Qdisplay);
26488 Qspace_width = intern_c_string ("space-width");
26489 staticpro (&Qspace_width);
26490 Qraise = intern_c_string ("raise");
26491 staticpro (&Qraise);
26492 Qslice = intern_c_string ("slice");
26493 staticpro (&Qslice);
26494 Qspace = intern_c_string ("space");
26495 staticpro (&Qspace);
26496 Qmargin = intern_c_string ("margin");
26497 staticpro (&Qmargin);
26498 Qpointer = intern_c_string ("pointer");
26499 staticpro (&Qpointer);
26500 Qleft_margin = intern_c_string ("left-margin");
26501 staticpro (&Qleft_margin);
26502 Qright_margin = intern_c_string ("right-margin");
26503 staticpro (&Qright_margin);
26504 Qcenter = intern_c_string ("center");
26505 staticpro (&Qcenter);
26506 Qline_height = intern_c_string ("line-height");
26507 staticpro (&Qline_height);
26508 QCalign_to = intern_c_string (":align-to");
26509 staticpro (&QCalign_to);
26510 QCrelative_width = intern_c_string (":relative-width");
26511 staticpro (&QCrelative_width);
26512 QCrelative_height = intern_c_string (":relative-height");
26513 staticpro (&QCrelative_height);
26514 QCeval = intern_c_string (":eval");
26515 staticpro (&QCeval);
26516 QCpropertize = intern_c_string (":propertize");
26517 staticpro (&QCpropertize);
26518 QCfile = intern_c_string (":file");
26519 staticpro (&QCfile);
26520 Qfontified = intern_c_string ("fontified");
26521 staticpro (&Qfontified);
26522 Qfontification_functions = intern_c_string ("fontification-functions");
26523 staticpro (&Qfontification_functions);
26524 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26525 staticpro (&Qtrailing_whitespace);
26526 Qescape_glyph = intern_c_string ("escape-glyph");
26527 staticpro (&Qescape_glyph);
26528 Qnobreak_space = intern_c_string ("nobreak-space");
26529 staticpro (&Qnobreak_space);
26530 Qimage = intern_c_string ("image");
26531 staticpro (&Qimage);
26532 Qtext = intern_c_string ("text");
26533 staticpro (&Qtext);
26534 Qboth = intern_c_string ("both");
26535 staticpro (&Qboth);
26536 Qboth_horiz = intern_c_string ("both-horiz");
26537 staticpro (&Qboth_horiz);
26538 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26539 staticpro (&Qtext_image_horiz);
26540 QCmap = intern_c_string (":map");
26541 staticpro (&QCmap);
26542 QCpointer = intern_c_string (":pointer");
26543 staticpro (&QCpointer);
26544 Qrect = intern_c_string ("rect");
26545 staticpro (&Qrect);
26546 Qcircle = intern_c_string ("circle");
26547 staticpro (&Qcircle);
26548 Qpoly = intern_c_string ("poly");
26549 staticpro (&Qpoly);
26550 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26551 staticpro (&Qmessage_truncate_lines);
26552 Qgrow_only = intern_c_string ("grow-only");
26553 staticpro (&Qgrow_only);
26554 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26555 staticpro (&Qinhibit_menubar_update);
26556 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26557 staticpro (&Qinhibit_eval_during_redisplay);
26558 Qposition = intern_c_string ("position");
26559 staticpro (&Qposition);
26560 Qbuffer_position = intern_c_string ("buffer-position");
26561 staticpro (&Qbuffer_position);
26562 Qobject = intern_c_string ("object");
26563 staticpro (&Qobject);
26564 Qbar = intern_c_string ("bar");
26565 staticpro (&Qbar);
26566 Qhbar = intern_c_string ("hbar");
26567 staticpro (&Qhbar);
26568 Qbox = intern_c_string ("box");
26569 staticpro (&Qbox);
26570 Qhollow = intern_c_string ("hollow");
26571 staticpro (&Qhollow);
26572 Qhand = intern_c_string ("hand");
26573 staticpro (&Qhand);
26574 Qarrow = intern_c_string ("arrow");
26575 staticpro (&Qarrow);
26576 Qtext = intern_c_string ("text");
26577 staticpro (&Qtext);
26578 Qrisky_local_variable = intern_c_string ("risky-local-variable");
26579 staticpro (&Qrisky_local_variable);
26580 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26581 staticpro (&Qinhibit_free_realized_faces);
26582
26583 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26584 Fcons (intern_c_string ("void-variable"), Qnil)),
26585 Qnil);
26586 staticpro (&list_of_error);
26587
26588 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26589 staticpro (&Qlast_arrow_position);
26590 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26591 staticpro (&Qlast_arrow_string);
26592
26593 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26594 staticpro (&Qoverlay_arrow_string);
26595 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26596 staticpro (&Qoverlay_arrow_bitmap);
26597
26598 echo_buffer[0] = echo_buffer[1] = Qnil;
26599 staticpro (&echo_buffer[0]);
26600 staticpro (&echo_buffer[1]);
26601
26602 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26603 staticpro (&echo_area_buffer[0]);
26604 staticpro (&echo_area_buffer[1]);
26605
26606 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26607 staticpro (&Vmessages_buffer_name);
26608
26609 mode_line_proptrans_alist = Qnil;
26610 staticpro (&mode_line_proptrans_alist);
26611 mode_line_string_list = Qnil;
26612 staticpro (&mode_line_string_list);
26613 mode_line_string_face = Qnil;
26614 staticpro (&mode_line_string_face);
26615 mode_line_string_face_prop = Qnil;
26616 staticpro (&mode_line_string_face_prop);
26617 Vmode_line_unwind_vector = Qnil;
26618 staticpro (&Vmode_line_unwind_vector);
26619
26620 help_echo_string = Qnil;
26621 staticpro (&help_echo_string);
26622 help_echo_object = Qnil;
26623 staticpro (&help_echo_object);
26624 help_echo_window = Qnil;
26625 staticpro (&help_echo_window);
26626 previous_help_echo_string = Qnil;
26627 staticpro (&previous_help_echo_string);
26628 help_echo_pos = -1;
26629
26630 Qright_to_left = intern_c_string ("right-to-left");
26631 staticpro (&Qright_to_left);
26632 Qleft_to_right = intern_c_string ("left-to-right");
26633 staticpro (&Qleft_to_right);
26634
26635 #ifdef HAVE_WINDOW_SYSTEM
26636 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
26637 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26638 For example, if a block cursor is over a tab, it will be drawn as
26639 wide as that tab on the display. */);
26640 x_stretch_cursor_p = 0;
26641 #endif
26642
26643 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
26644 doc: /* *Non-nil means highlight trailing whitespace.
26645 The face used for trailing whitespace is `trailing-whitespace'. */);
26646 Vshow_trailing_whitespace = Qnil;
26647
26648 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
26649 doc: /* *Control highlighting of nobreak space and soft hyphen.
26650 A value of t means highlight the character itself (for nobreak space,
26651 use face `nobreak-space').
26652 A value of nil means no highlighting.
26653 Other values mean display the escape glyph followed by an ordinary
26654 space or ordinary hyphen. */);
26655 Vnobreak_char_display = Qt;
26656
26657 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
26658 doc: /* *The pointer shape to show in void text areas.
26659 A value of nil means to show the text pointer. Other options are `arrow',
26660 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26661 Vvoid_text_area_pointer = Qarrow;
26662
26663 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
26664 doc: /* Non-nil means don't actually do any redisplay.
26665 This is used for internal purposes. */);
26666 Vinhibit_redisplay = Qnil;
26667
26668 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
26669 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26670 Vglobal_mode_string = Qnil;
26671
26672 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
26673 doc: /* Marker for where to display an arrow on top of the buffer text.
26674 This must be the beginning of a line in order to work.
26675 See also `overlay-arrow-string'. */);
26676 Voverlay_arrow_position = Qnil;
26677
26678 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
26679 doc: /* String to display as an arrow in non-window frames.
26680 See also `overlay-arrow-position'. */);
26681 Voverlay_arrow_string = make_pure_c_string ("=>");
26682
26683 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
26684 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26685 The symbols on this list are examined during redisplay to determine
26686 where to display overlay arrows. */);
26687 Voverlay_arrow_variable_list
26688 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26689
26690 DEFVAR_INT ("scroll-step", &scroll_step,
26691 doc: /* *The number of lines to try scrolling a window by when point moves out.
26692 If that fails to bring point back on frame, point is centered instead.
26693 If this is zero, point is always centered after it moves off frame.
26694 If you want scrolling to always be a line at a time, you should set
26695 `scroll-conservatively' to a large value rather than set this to 1. */);
26696
26697 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
26698 doc: /* *Scroll up to this many lines, to bring point back on screen.
26699 If point moves off-screen, redisplay will scroll by up to
26700 `scroll-conservatively' lines in order to bring point just barely
26701 onto the screen again. If that cannot be done, then redisplay
26702 recenters point as usual.
26703
26704 A value of zero means always recenter point if it moves off screen. */);
26705 scroll_conservatively = 0;
26706
26707 DEFVAR_INT ("scroll-margin", &scroll_margin,
26708 doc: /* *Number of lines of margin at the top and bottom of a window.
26709 Recenter the window whenever point gets within this many lines
26710 of the top or bottom of the window. */);
26711 scroll_margin = 0;
26712
26713 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
26714 doc: /* Pixels per inch value for non-window system displays.
26715 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26716 Vdisplay_pixels_per_inch = make_float (72.0);
26717
26718 #if GLYPH_DEBUG
26719 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
26720 #endif
26721
26722 DEFVAR_LISP ("truncate-partial-width-windows",
26723 &Vtruncate_partial_width_windows,
26724 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26725 For an integer value, truncate lines in each window narrower than the
26726 full frame width, provided the window width is less than that integer;
26727 otherwise, respect the value of `truncate-lines'.
26728
26729 For any other non-nil value, truncate lines in all windows that do
26730 not span the full frame width.
26731
26732 A value of nil means to respect the value of `truncate-lines'.
26733
26734 If `word-wrap' is enabled, you might want to reduce this. */);
26735 Vtruncate_partial_width_windows = make_number (50);
26736
26737 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
26738 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26739 Any other value means to use the appropriate face, `mode-line',
26740 `header-line', or `menu' respectively. */);
26741 mode_line_inverse_video = 1;
26742
26743 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
26744 doc: /* *Maximum buffer size for which line number should be displayed.
26745 If the buffer is bigger than this, the line number does not appear
26746 in the mode line. A value of nil means no limit. */);
26747 Vline_number_display_limit = Qnil;
26748
26749 DEFVAR_INT ("line-number-display-limit-width",
26750 &line_number_display_limit_width,
26751 doc: /* *Maximum line width (in characters) for line number display.
26752 If the average length of the lines near point is bigger than this, then the
26753 line number may be omitted from the mode line. */);
26754 line_number_display_limit_width = 200;
26755
26756 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
26757 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26758 highlight_nonselected_windows = 0;
26759
26760 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
26761 doc: /* Non-nil if more than one frame is visible on this display.
26762 Minibuffer-only frames don't count, but iconified frames do.
26763 This variable is not guaranteed to be accurate except while processing
26764 `frame-title-format' and `icon-title-format'. */);
26765
26766 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
26767 doc: /* Template for displaying the title bar of visible frames.
26768 \(Assuming the window manager supports this feature.)
26769
26770 This variable has the same structure as `mode-line-format', except that
26771 the %c and %l constructs are ignored. It is used only on frames for
26772 which no explicit name has been set \(see `modify-frame-parameters'). */);
26773
26774 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
26775 doc: /* Template for displaying the title bar of an iconified frame.
26776 \(Assuming the window manager supports this feature.)
26777 This variable has the same structure as `mode-line-format' (which see),
26778 and is used only on frames for which no explicit name has been set
26779 \(see `modify-frame-parameters'). */);
26780 Vicon_title_format
26781 = Vframe_title_format
26782 = pure_cons (intern_c_string ("multiple-frames"),
26783 pure_cons (make_pure_c_string ("%b"),
26784 pure_cons (pure_cons (empty_unibyte_string,
26785 pure_cons (intern_c_string ("invocation-name"),
26786 pure_cons (make_pure_c_string ("@"),
26787 pure_cons (intern_c_string ("system-name"),
26788 Qnil)))),
26789 Qnil)));
26790
26791 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
26792 doc: /* Maximum number of lines to keep in the message log buffer.
26793 If nil, disable message logging. If t, log messages but don't truncate
26794 the buffer when it becomes large. */);
26795 Vmessage_log_max = make_number (100);
26796
26797 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
26798 doc: /* Functions called before redisplay, if window sizes have changed.
26799 The value should be a list of functions that take one argument.
26800 Just before redisplay, for each frame, if any of its windows have changed
26801 size since the last redisplay, or have been split or deleted,
26802 all the functions in the list are called, with the frame as argument. */);
26803 Vwindow_size_change_functions = Qnil;
26804
26805 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
26806 doc: /* List of functions to call before redisplaying a window with scrolling.
26807 Each function is called with two arguments, the window and its new
26808 display-start position. Note that these functions are also called by
26809 `set-window-buffer'. Also note that the value of `window-end' is not
26810 valid when these functions are called. */);
26811 Vwindow_scroll_functions = Qnil;
26812
26813 DEFVAR_LISP ("window-text-change-functions",
26814 &Vwindow_text_change_functions,
26815 doc: /* Functions to call in redisplay when text in the window might change. */);
26816 Vwindow_text_change_functions = Qnil;
26817
26818 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
26819 doc: /* Functions called when redisplay of a window reaches the end trigger.
26820 Each function is called with two arguments, the window and the end trigger value.
26821 See `set-window-redisplay-end-trigger'. */);
26822 Vredisplay_end_trigger_functions = Qnil;
26823
26824 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
26825 doc: /* *Non-nil means autoselect window with mouse pointer.
26826 If nil, do not autoselect windows.
26827 A positive number means delay autoselection by that many seconds: a
26828 window is autoselected only after the mouse has remained in that
26829 window for the duration of the delay.
26830 A negative number has a similar effect, but causes windows to be
26831 autoselected only after the mouse has stopped moving. \(Because of
26832 the way Emacs compares mouse events, you will occasionally wait twice
26833 that time before the window gets selected.\)
26834 Any other value means to autoselect window instantaneously when the
26835 mouse pointer enters it.
26836
26837 Autoselection selects the minibuffer only if it is active, and never
26838 unselects the minibuffer if it is active.
26839
26840 When customizing this variable make sure that the actual value of
26841 `focus-follows-mouse' matches the behavior of your window manager. */);
26842 Vmouse_autoselect_window = Qnil;
26843
26844 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
26845 doc: /* *Non-nil means automatically resize tool-bars.
26846 This dynamically changes the tool-bar's height to the minimum height
26847 that is needed to make all tool-bar items visible.
26848 If value is `grow-only', the tool-bar's height is only increased
26849 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26850 Vauto_resize_tool_bars = Qt;
26851
26852 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
26853 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26854 auto_raise_tool_bar_buttons_p = 1;
26855
26856 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
26857 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26858 make_cursor_line_fully_visible_p = 1;
26859
26860 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
26861 doc: /* *Border below tool-bar in pixels.
26862 If an integer, use it as the height of the border.
26863 If it is one of `internal-border-width' or `border-width', use the
26864 value of the corresponding frame parameter.
26865 Otherwise, no border is added below the tool-bar. */);
26866 Vtool_bar_border = Qinternal_border_width;
26867
26868 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
26869 doc: /* *Margin around tool-bar buttons in pixels.
26870 If an integer, use that for both horizontal and vertical margins.
26871 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26872 HORZ specifying the horizontal margin, and VERT specifying the
26873 vertical margin. */);
26874 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26875
26876 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
26877 doc: /* *Relief thickness of tool-bar buttons. */);
26878 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26879
26880 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
26881 doc: /* *Tool bar style to use.
26882 It can be one of
26883 image - show images only
26884 text - show text only
26885 both - show both, text below image
26886 both-horiz - show text to the right of the image
26887 text-image-horiz - show text to the left of the image
26888 any other - use system default or image if no system default. */);
26889 Vtool_bar_style = Qnil;
26890
26891 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
26892 doc: /* *Maximum number of characters a label can have to be shown.
26893 The tool bar style must also show labels for this to have any effect, see
26894 `tool-bar-style'. */);
26895 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26896
26897 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
26898 doc: /* List of functions to call to fontify regions of text.
26899 Each function is called with one argument POS. Functions must
26900 fontify a region starting at POS in the current buffer, and give
26901 fontified regions the property `fontified'. */);
26902 Vfontification_functions = Qnil;
26903 Fmake_variable_buffer_local (Qfontification_functions);
26904
26905 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26906 &unibyte_display_via_language_environment,
26907 doc: /* *Non-nil means display unibyte text according to language environment.
26908 Specifically, this means that raw bytes in the range 160-255 decimal
26909 are displayed by converting them to the equivalent multibyte characters
26910 according to the current language environment. As a result, they are
26911 displayed according to the current fontset.
26912
26913 Note that this variable affects only how these bytes are displayed,
26914 but does not change the fact they are interpreted as raw bytes. */);
26915 unibyte_display_via_language_environment = 0;
26916
26917 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
26918 doc: /* *Maximum height for resizing mini-windows.
26919 If a float, it specifies a fraction of the mini-window frame's height.
26920 If an integer, it specifies a number of lines. */);
26921 Vmax_mini_window_height = make_float (0.25);
26922
26923 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26924 doc: /* *How to resize mini-windows.
26925 A value of nil means don't automatically resize mini-windows.
26926 A value of t means resize them to fit the text displayed in them.
26927 A value of `grow-only', the default, means let mini-windows grow
26928 only, until their display becomes empty, at which point the windows
26929 go back to their normal size. */);
26930 Vresize_mini_windows = Qgrow_only;
26931
26932 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26933 doc: /* Alist specifying how to blink the cursor off.
26934 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26935 `cursor-type' frame-parameter or variable equals ON-STATE,
26936 comparing using `equal', Emacs uses OFF-STATE to specify
26937 how to blink it off. ON-STATE and OFF-STATE are values for
26938 the `cursor-type' frame parameter.
26939
26940 If a frame's ON-STATE has no entry in this list,
26941 the frame's other specifications determine how to blink the cursor off. */);
26942 Vblink_cursor_alist = Qnil;
26943
26944 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26945 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26946 If non-nil, windows are automatically scrolled horizontally to make
26947 point visible. */);
26948 automatic_hscrolling_p = 1;
26949 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26950 staticpro (&Qauto_hscroll_mode);
26951
26952 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26953 doc: /* *How many columns away from the window edge point is allowed to get
26954 before automatic hscrolling will horizontally scroll the window. */);
26955 hscroll_margin = 5;
26956
26957 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26958 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26959 When point is less than `hscroll-margin' columns from the window
26960 edge, automatic hscrolling will scroll the window by the amount of columns
26961 determined by this variable. If its value is a positive integer, scroll that
26962 many columns. If it's a positive floating-point number, it specifies the
26963 fraction of the window's width to scroll. If it's nil or zero, point will be
26964 centered horizontally after the scroll. Any other value, including negative
26965 numbers, are treated as if the value were zero.
26966
26967 Automatic hscrolling always moves point outside the scroll margin, so if
26968 point was more than scroll step columns inside the margin, the window will
26969 scroll more than the value given by the scroll step.
26970
26971 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26972 and `scroll-right' overrides this variable's effect. */);
26973 Vhscroll_step = make_number (0);
26974
26975 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26976 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26977 Bind this around calls to `message' to let it take effect. */);
26978 message_truncate_lines = 0;
26979
26980 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26981 doc: /* Normal hook run to update the menu bar definitions.
26982 Redisplay runs this hook before it redisplays the menu bar.
26983 This is used to update submenus such as Buffers,
26984 whose contents depend on various data. */);
26985 Vmenu_bar_update_hook = Qnil;
26986
26987 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26988 doc: /* Frame for which we are updating a menu.
26989 The enable predicate for a menu binding should check this variable. */);
26990 Vmenu_updating_frame = Qnil;
26991
26992 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26993 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26994 inhibit_menubar_update = 0;
26995
26996 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26997 doc: /* Prefix prepended to all continuation lines at display time.
26998 The value may be a string, an image, or a stretch-glyph; it is
26999 interpreted in the same way as the value of a `display' text property.
27000
27001 This variable is overridden by any `wrap-prefix' text or overlay
27002 property.
27003
27004 To add a prefix to non-continuation lines, use `line-prefix'. */);
27005 Vwrap_prefix = Qnil;
27006 staticpro (&Qwrap_prefix);
27007 Qwrap_prefix = intern_c_string ("wrap-prefix");
27008 Fmake_variable_buffer_local (Qwrap_prefix);
27009
27010 DEFVAR_LISP ("line-prefix", &Vline_prefix,
27011 doc: /* Prefix prepended to all non-continuation lines at display time.
27012 The value may be a string, an image, or a stretch-glyph; it is
27013 interpreted in the same way as the value of a `display' text property.
27014
27015 This variable is overridden by any `line-prefix' text or overlay
27016 property.
27017
27018 To add a prefix to continuation lines, use `wrap-prefix'. */);
27019 Vline_prefix = Qnil;
27020 staticpro (&Qline_prefix);
27021 Qline_prefix = intern_c_string ("line-prefix");
27022 Fmake_variable_buffer_local (Qline_prefix);
27023
27024 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
27025 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27026 inhibit_eval_during_redisplay = 0;
27027
27028 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
27029 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27030 inhibit_free_realized_faces = 0;
27031
27032 #if GLYPH_DEBUG
27033 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
27034 doc: /* Inhibit try_window_id display optimization. */);
27035 inhibit_try_window_id = 0;
27036
27037 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
27038 doc: /* Inhibit try_window_reusing display optimization. */);
27039 inhibit_try_window_reusing = 0;
27040
27041 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
27042 doc: /* Inhibit try_cursor_movement display optimization. */);
27043 inhibit_try_cursor_movement = 0;
27044 #endif /* GLYPH_DEBUG */
27045
27046 DEFVAR_INT ("overline-margin", &overline_margin,
27047 doc: /* *Space between overline and text, in pixels.
27048 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27049 margin to the caracter height. */);
27050 overline_margin = 2;
27051
27052 DEFVAR_INT ("underline-minimum-offset",
27053 &underline_minimum_offset,
27054 doc: /* Minimum distance between baseline and underline.
27055 This can improve legibility of underlined text at small font sizes,
27056 particularly when using variable `x-use-underline-position-properties'
27057 with fonts that specify an UNDERLINE_POSITION relatively close to the
27058 baseline. The default value is 1. */);
27059 underline_minimum_offset = 1;
27060
27061 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
27062 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27063 This feature only works when on a window system that can change
27064 cursor shapes. */);
27065 display_hourglass_p = 1;
27066
27067 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
27068 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27069 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27070
27071 hourglass_atimer = NULL;
27072 hourglass_shown_p = 0;
27073
27074 DEFSYM (Qglyphless_char, "glyphless-char");
27075 DEFSYM (Qhex_code, "hex-code");
27076 DEFSYM (Qempty_box, "empty-box");
27077 DEFSYM (Qthin_space, "thin-space");
27078 DEFSYM (Qzero_width, "zero-width");
27079
27080 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27081 /* Intern this now in case it isn't already done.
27082 Setting this variable twice is harmless.
27083 But don't staticpro it here--that is done in alloc.c. */
27084 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27085 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27086
27087 DEFVAR_LISP ("glyphless-char-display", &Vglyphless_char_display,
27088 doc: /* Char-table to control displaying of glyphless characters.
27089 Each element, if non-nil, is an ASCII acronym string (displayed in a box)
27090 or one of these symbols:
27091 hex-code: display the hexadecimal code of a character in a box
27092 empty-box: display as an empty box
27093 thin-space: display as 1-pixel width space
27094 zero-width: don't display
27095
27096 It has one extra slot to control the display of a character for which
27097 no font is found. The value of the slot is `hex-code' or `empty-box'.
27098 The default is `empty-box'. */);
27099 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27100 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27101 Qempty_box);
27102 }
27103
27104
27105 /* Initialize this module when Emacs starts. */
27106
27107 void
27108 init_xdisp (void)
27109 {
27110 Lisp_Object root_window;
27111 struct window *mini_w;
27112
27113 current_header_line_height = current_mode_line_height = -1;
27114
27115 CHARPOS (this_line_start_pos) = 0;
27116
27117 mini_w = XWINDOW (minibuf_window);
27118 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27119
27120 if (!noninteractive)
27121 {
27122 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27123 int i;
27124
27125 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27126 set_window_height (root_window,
27127 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27128 0);
27129 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27130 set_window_height (minibuf_window, 1, 0);
27131
27132 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27133 mini_w->total_cols = make_number (FRAME_COLS (f));
27134
27135 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27136 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27137 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27138
27139 /* The default ellipsis glyphs `...'. */
27140 for (i = 0; i < 3; ++i)
27141 default_invis_vector[i] = make_number ('.');
27142 }
27143
27144 {
27145 /* Allocate the buffer for frame titles.
27146 Also used for `format-mode-line'. */
27147 int size = 100;
27148 mode_line_noprop_buf = (char *) xmalloc (size);
27149 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27150 mode_line_noprop_ptr = mode_line_noprop_buf;
27151 mode_line_target = MODE_LINE_DISPLAY;
27152 }
27153
27154 help_echo_showing_p = 0;
27155 }
27156
27157 /* Since w32 does not support atimers, it defines its own implementation of
27158 the following three functions in w32fns.c. */
27159 #ifndef WINDOWSNT
27160
27161 /* Platform-independent portion of hourglass implementation. */
27162
27163 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27164 int
27165 hourglass_started (void)
27166 {
27167 return hourglass_shown_p || hourglass_atimer != NULL;
27168 }
27169
27170 /* Cancel a currently active hourglass timer, and start a new one. */
27171 void
27172 start_hourglass (void)
27173 {
27174 #if defined (HAVE_WINDOW_SYSTEM)
27175 EMACS_TIME delay;
27176 int secs, usecs = 0;
27177
27178 cancel_hourglass ();
27179
27180 if (INTEGERP (Vhourglass_delay)
27181 && XINT (Vhourglass_delay) > 0)
27182 secs = XFASTINT (Vhourglass_delay);
27183 else if (FLOATP (Vhourglass_delay)
27184 && XFLOAT_DATA (Vhourglass_delay) > 0)
27185 {
27186 Lisp_Object tem;
27187 tem = Ftruncate (Vhourglass_delay, Qnil);
27188 secs = XFASTINT (tem);
27189 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27190 }
27191 else
27192 secs = DEFAULT_HOURGLASS_DELAY;
27193
27194 EMACS_SET_SECS_USECS (delay, secs, usecs);
27195 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27196 show_hourglass, NULL);
27197 #endif
27198 }
27199
27200
27201 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27202 shown. */
27203 void
27204 cancel_hourglass (void)
27205 {
27206 #if defined (HAVE_WINDOW_SYSTEM)
27207 if (hourglass_atimer)
27208 {
27209 cancel_atimer (hourglass_atimer);
27210 hourglass_atimer = NULL;
27211 }
27212
27213 if (hourglass_shown_p)
27214 hide_hourglass ();
27215 #endif
27216 }
27217 #endif /* ! WINDOWSNT */
27218